Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
/* Copyright (C) 2000 The PARI group.12This file is part of the PARI/GP package.34PARI/GP is free software; you can redistribute it and/or modify it under the5terms of the GNU General Public License as published by the Free Software6Foundation; either version 2 of the License, or (at your option) any later7version. It is distributed in the hope that it will be useful, but WITHOUT8ANY WARRANTY WHATSOEVER.910Check the License for details. You should have received a copy of it, along11with the package; see the file 'COPYING'. If not, write to the Free Software12Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */1314/*******************************************************************/15/* */16/* INTERFACE TO READLINE COMPLETION */17/* */18/*******************************************************************/19#include "pari.h"2021#ifdef READLINE2223#include "paripriv.h"24#include "gp.h"2526typedef int (*RLCI)(int, int); /* rl_complete and rl_insert functions */2728BEGINEXTERN29/* otherwise C++ compilers will choke on rl_message() prototype */30#define USE_VARARGS31#define PREFER_STDARG32#include <readline/readline.h>33#include <readline/history.h>34ENDEXTERN3536/**************************************************************************/37static pari_rl_interface pari_rl;38static int did_init_matched = 0;3940static int41change_state(const char *msg, ulong flag, int count)42{43int c = (GP_DATA->readline_state & flag) != 0;44ulong state = GP_DATA->readline_state;4546switch(count)47{48default: c = 0; break; /* off */49case -1: c = 1; break; /* on */50case -2: c = 1 - c; /* toggle */51}52if (c)53GP_DATA->readline_state |= flag;54else {55GP_DATA->readline_state &= ~flag;56if (!GP_DATA->readline_state && state) GP_DATA->readline_state = 1;57}58rl_save_prompt();59rl_message("[%s: %s] ", msg, c? "on": "off");60c = rl_read_key();61rl_restore_prompt();62rl_clear_message();63rl_stuff_char(c); return 1;64}6566/* Wrapper around rl_complete to allow toggling insertion of arguments */67static int68pari_rl_complete(int count, int key)69{70int ret;7172pari_rl.back = 0;73if (count <= 0)74return change_state("complete args", DO_ARGS_COMPLETE, count);7576rl_begin_undo_group();77if (rl_last_func == pari_rl_complete)78rl_last_func = (RLCI) rl_complete; /* Make repeated TABs different */79ret = ((RLCI)rl_complete)(count,key);80if (pari_rl.back && (pari_rl.back <= rl_point))81rl_point -= pari_rl.back;82rl_end_undo_group(); return ret;83}8485static int did_matched_insert;8687static int88pari_rl_matched_insert_suspend(int count, int key)89{90ulong state = GP_DATA->readline_state;91(void)count; (void)key;9293did_matched_insert = (GP_DATA->readline_state & DO_MATCHED_INSERT);94GP_DATA->readline_state &= ~DO_MATCHED_INSERT;95if (!GP_DATA->readline_state && state) GP_DATA->readline_state = 1;96return 1;97}9899static int100pari_rl_matched_insert_restore(int count, int key)101{102(void)count; (void)key;103if (did_matched_insert)104GP_DATA->readline_state |= DO_MATCHED_INSERT;105return 1;106}107108static const char paropen[] = "([{";109static const char parclose[] = ")]}";110111/* To allow insertion of () with a point in between. */112static int113pari_rl_matched_insert(int count, int key)114{115int i = 0, ret;116117if (count <= 0)118return change_state("electric parens", DO_MATCHED_INSERT, count);119while (paropen[i] && paropen[i] != key) i++;120if (!paropen[i] || !(GP_DATA->readline_state & DO_MATCHED_INSERT) || GP_DATA->flags & gpd_EMACS)121return ((RLCI)rl_insert)(count,key);122rl_begin_undo_group();123((RLCI)rl_insert)(count,key);124ret = ((RLCI)rl_insert)(count,parclose[i]);125rl_point -= count;126rl_end_undo_group(); return ret;127}128129static int130pari_rl_default_matched_insert(int count, int key)131{132if (!did_init_matched) {133did_init_matched = 1;134GP_DATA->readline_state |= DO_MATCHED_INSERT;135}136return pari_rl_matched_insert(count, key);137}138139static int140pari_rl_forward_sexp(int count, int key)141{142int deep = 0, dir = 1, move_point = 0, lfail;143144(void)key;145if (count < 0)146{147count = -count; dir = -1;148if (!rl_point) goto fail;149rl_point--;150}151while (count || deep)152{153move_point = 1; /* Need to move point if moving left. */154lfail = 0; /* Do not need to fail left movement yet. */155while ( !is_keyword_char(rl_line_buffer[rl_point])156&& !strchr("\"([{}])",rl_line_buffer[rl_point])157&& !( (dir == 1)158? (rl_point >= rl_end)159: (rl_point <= 0 && (lfail = 1))))160rl_point += dir;161if (lfail || !rl_line_buffer[rl_point]) goto fail;162163if (is_keyword_char(rl_line_buffer[rl_point]))164{165while ( is_keyword_char(rl_line_buffer[rl_point])166&& (!((dir == 1) ? (rl_point >= rl_end) : (rl_point <= 0 && (lfail = 1)))167|| (move_point = 0)))168rl_point += dir;169if (deep && lfail) goto fail;170if (!deep) count--;171}172else if (strchr(paropen,rl_line_buffer[rl_point]))173{174if (deep == 0 && dir == -1) goto fail; /* We are already out of pars. */175rl_point += dir;176deep++; if (!deep) count--;177}178else if (strchr(parclose,rl_line_buffer[rl_point]))179{180if (deep == 0 && dir == 1)181{182rl_point++; goto fail; /* Get out of pars. */183}184rl_point += dir;185deep--; if (!deep) count--;186}187else if (rl_line_buffer[rl_point] == '\"')188{189int bad = 1;190191rl_point += dir;192while ( ((rl_line_buffer[rl_point] != '\"') || (bad = 0))193&& (!((dir == 1) ? (rl_point >= rl_end) : (rl_point <= 0))194|| (move_point = 0)) )195rl_point += dir;196if (bad) goto fail;197rl_point += dir; /* Skip the other delimiter */198if (!deep) count--;199}200else201{202fail: rl_ding(); return 1;203}204}205if (dir != 1 && move_point) rl_point++;206return 1;207}208209static int210pari_rl_backward_sexp(int count, int key)211{212return pari_rl_forward_sexp(-count, key);213}214215static void216rl_print_aide(char *s, int flag)217{218int p = rl_point, e = rl_end;219FILE *save = pari_outfile;220221rl_point = 0; rl_end = 0; pari_outfile = rl_outstream;222rl_save_prompt();223rl_message("%s",""); /* rl_message("") ==> "zero length format" warning */224gp_help(s, flag);225rl_restore_prompt();226rl_point = p; rl_end = e; pari_outfile = save;227rl_clear_message();228rl_refresh_line(0,0);229}230231/* long help if count < 0 */232static int233rl_short_help(int count, int key)234{235int flag = h_RL;236char *s = rl_line_buffer + rl_point;237238(void)key;239/* func() with cursor on ')', e.g. following completion */240if (s > rl_line_buffer && *s == ')' && s[-1] == '(') s--;241while (s > rl_line_buffer && is_keyword_char(s[-1])) s--;242/* check for '\c' */243if (s > rl_line_buffer && s[-1] == '\\') s--;244245if (count < 0 || rl_last_func == rl_short_help) flag |= h_LONG;246rl_print_aide(s, flag); return 0;247}248249static int250rl_long_help(int count, int key) { (void)count; return rl_short_help(-1,key); }251252static void253init_histfile(void)254{255char *h = GP_DATA->histfile;256if (h && read_history(h)) write_history(h);257}258259/*******************************************************************/260/* */261/* GET LINE FROM READLINE */262/* */263/*******************************************************************/264static int265history_is_new(char *s)266{267HIST_ENTRY *e;268if (!*s) return 0;269if (!history_length) return 1;270e = history_get(history_length);271/* paranoia: e != NULL, unless readline is in a weird state */272return e? strcmp(s, e->line): 0;273}274275static void276gp_add_history(char *s)277{278if (history_is_new(s)) { add_history(s); append_history(1,GP_DATA->histfile); }279}280281/* Read line; returns a malloc()ed string of the user input or NULL on EOF.282Increments the buffer size appropriately if needed; fix *endp if so. */283static char *284gprl_input(char **endp, int first, input_method *IM, filtre_t *F)285{286pari_sp av = avma;287Buffer *b = F->buf;288ulong used = *endp - b->buf;289ulong left = b->len - used, l;290const char *p;291char *s, *t;292293if (first) p = IM->prompt;294else {295p = F->in_comment ? GP_DATA->prompt_comment: IM->prompt_cont;296p = gp_format_prompt(p);297}298if (! (s = readline(p)) ) { set_avma(av); return NULL; } /* EOF */299gp_add_history(s); /* Makes a copy */300l = strlen(s) + 1;301/* put back \n that readline stripped. This is needed for302* { print("a303* b"); }304* and conforms with the other input methods anyway. */305t = (char*)pari_malloc(l + 1);306memcpy(t, s, l-1);307t[l-1] = '\n';308t[l] = 0; /* equivalent to sprintf(t,"%s\n", s) */309if (left < l)310{311ulong incr = b->len;312if (incr < l) incr = l;313fix_buffer(b, b->len + incr);314*endp = b->buf + used;315}316set_avma(av); return t;317}318319/* request one line interactively.320* Return 0: EOF321* 1: got one line from readline or pari_infile */322int323get_line_from_readline(const char *prompt, const char *prompt_cont, filtre_t *F)324{325const int index = history_length;326char *s;327input_method IM;328329if (!GP_DATA->use_readline)330{331pari_puts(prompt); pari_flush();332return get_line_from_file(prompt, F, pari_infile);333}334335IM.prompt = prompt;336IM.prompt_cont = prompt_cont;337IM.getline = &gprl_input;338IM.free = 1;339if (! input_loop(F,&IM)) { pari_puts("\n"); return 0; }340341s = F->buf->buf;342if (*s)343{344if (history_length > index+1)345{ /* Multi-line input. Remove incomplete lines */346int i = history_length;347while (i > index) {348HIST_ENTRY *e = remove_history(--i);349pari_free(e->line); pari_free(e);350}351gp_add_history(s);352}353gp_echo_and_log(prompt, s);354}355return 1;356}357358static char**359gp_completion(char *text, int START, int END)360{361return pari_completion(&pari_rl, text, START, END);362}363364void365init_readline(void)366{367static int init_done = 0;368369if (init_done) return;370371pari_use_readline(pari_rl);372373if (! GP_DATA->use_readline) GP_DATA->readline_state = 0;374init_done = 1;375init_histfile();376cb_pari_init_histfile = init_histfile;377cb_pari_get_line_interactive = get_line_from_readline;378379/* Allow conditional parsing of the ~/.inputrc file. */380rl_readline_name = "Pari-GP";381382/* added ~, ? and , */383rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(?~";384rl_special_prefixes = "~";385386/* custom completer */387rl_attempted_completion_function = (rl_completion_func_t*) gp_completion;388389/* we always want the whole list of completions under emacs */390if (GP_DATA->flags & gpd_EMACS) rl_completion_query_items = 0x8fff;391392rl_add_defun("short-help", rl_short_help, -1);393rl_add_defun("long-help", rl_long_help, -1);394rl_add_defun("pari-complete", pari_rl_complete, '\t');395rl_add_defun("pari-matched-insert", pari_rl_default_matched_insert, -1);396rl_add_defun("pari-matched-insert-suspend", pari_rl_matched_insert_suspend, -1);397rl_add_defun("pari-matched-insert-restore", pari_rl_matched_insert_restore, -1);398rl_add_defun("pari-forward-sexp", pari_rl_forward_sexp, -1);399rl_add_defun("pari-backward-sexp", pari_rl_backward_sexp, -1);400401rl_bind_key_in_map('h', rl_short_help, emacs_meta_keymap);402rl_bind_key_in_map('H', rl_long_help, emacs_meta_keymap);403404#define KSbind(s,f,k) rl_generic_bind(ISFUNC, (s), (char*)(f), (k))405406KSbind("OP", rl_short_help, emacs_meta_keymap); /* f1, vt100 */407KSbind("[11~", rl_short_help, emacs_meta_keymap); /* f1, xterm */408KSbind("OP", rl_short_help, vi_movement_keymap); /* f1, vt100 */409KSbind("[11~", rl_short_help, vi_movement_keymap); /* f1, xterm */410/* XTerm may signal start/end of paste by emitting F200/F201411* TODO: check to what extent this patch has been applied */412/* FIXME: For vi mode something more intelligent is needed - to switch to the413insert mode - and back when restoring. */414KSbind("[200~", pari_rl_matched_insert_suspend, emacs_meta_keymap); /* pre-paste xterm */415KSbind("[200~", pari_rl_matched_insert_suspend, vi_movement_keymap); /* pre-paste xterm */416KSbind("[201~", pari_rl_matched_insert_restore, emacs_meta_keymap); /* post-paste xterm */417KSbind("[201~", pari_rl_matched_insert_restore, vi_movement_keymap); /* post-paste xterm */418rl_bind_key_in_map('(', pari_rl_matched_insert, emacs_standard_keymap);419rl_bind_key_in_map('[', pari_rl_matched_insert, emacs_standard_keymap);420rl_bind_key_in_map(6, pari_rl_forward_sexp, emacs_meta_keymap); /* M-C-f */421rl_bind_key_in_map(2, pari_rl_backward_sexp, emacs_meta_keymap); /* M-C-b */422}423#endif424425426