csope/src/readline.c

127 lines
2.6 KiB
C
Raw Normal View History

2023-08-04 13:12:06 -04:00
#include <stdio.h>
2023-08-01 13:36:53 -04:00
#include <readline/readline.h>
#include "global.h"
2023-08-04 13:12:06 -04:00
#include "build.h"
2023-08-04 16:35:56 -04:00
#include <ncurses.h>
2023-08-01 13:36:53 -04:00
2023-08-13 05:21:52 -04:00
static int input_available = 0;
2023-08-04 13:12:06 -04:00
static char input_char;
2023-08-13 05:21:52 -04:00
char input_line[PATLEN + 1];
2023-08-04 13:12:06 -04:00
2023-08-13 05:21:52 -04:00
bool interpret(int c) {
input_char = c;
input_available = 1;
rl_callback_read_char();
2023-08-04 13:12:06 -04:00
2023-08-13 05:21:52 -04:00
return 0;
2023-08-04 13:12:06 -04:00
}
2023-08-13 05:21:52 -04:00
static int getc_function(FILE *ignore) {
2023-08-09 07:49:11 -04:00
UNUSED(ignore);
2023-08-13 05:21:52 -04:00
input_available = 0;
return (int)input_char;
2023-08-01 13:36:53 -04:00
}
2023-08-13 05:21:52 -04:00
static int input_available_hook() {
return input_available;
2023-08-01 13:36:53 -04:00
}
2023-08-13 05:21:52 -04:00
static void redisplay_function() {
window_change |= CH_INPUT;
2023-08-01 13:36:53 -04:00
}
2023-08-13 05:21:52 -04:00
static void callback_handler(char *line) {
if(!line) { return; }
2023-08-08 14:34:15 -04:00
2023-08-13 05:21:52 -04:00
switch(input_mode) {
2023-08-10 08:36:16 -04:00
case INPUT_NORMAL:
2023-08-13 05:21:52 -04:00
strncpy(input_line, line, PATLEN);
2023-08-10 15:06:17 -04:00
search(input_line);
2023-08-13 15:22:44 -04:00
horswp_field();
2023-08-10 15:06:17 -04:00
curdispline = 0;
current_page = 0;
2023-08-13 15:22:44 -04:00
PCS_reset();
2023-08-10 08:36:16 -04:00
break;
2023-08-10 15:06:17 -04:00
case INPUT_CHANGE_TO:
2023-08-13 05:21:52 -04:00
strncpy(newpat, line, PATLEN);
change = calloc(totallines, sizeof(*change));
2023-08-10 15:06:17 -04:00
input_mode = INPUT_CHANGE;
horswp_field();
return;
2023-08-10 08:36:16 -04:00
}
2023-08-09 07:49:11 -04:00
2023-08-13 05:21:52 -04:00
switch(field) {
2023-08-10 08:36:16 -04:00
case CHANGE:
2023-08-10 15:06:17 -04:00
input_mode = INPUT_CHANGE_TO;
2023-08-10 08:36:16 -04:00
break;
case DEFINITION:
case FILENAME:
2023-08-13 05:21:52 -04:00
if(totallines == 1) { editref(0); }
2023-08-10 08:36:16 -04:00
break;
2023-08-09 07:49:11 -04:00
}
2023-08-01 13:36:53 -04:00
}
2023-08-13 05:21:52 -04:00
static int ctrl_z() {
kill(0, SIGTSTP);
2023-08-05 05:30:18 -04:00
return 0;
2023-08-04 13:12:06 -04:00
}
2023-08-13 05:21:52 -04:00
static int toggle_caseless() {
if(caseless == false) {
caseless = true;
postmsg2("Caseless mode is now ON");
} else {
caseless = false;
postmsg2("Caseless mode is now OFF");
}
egrepcaseless(caseless); /* turn on/off -i flag */
2023-08-05 05:30:18 -04:00
return 0;
2023-08-04 13:12:06 -04:00
}
2023-08-13 05:21:52 -04:00
static int rebuild_reference() {
if(isuptodate == true) {
postmsg("The -d option prevents rebuilding the symbol database");
return (false);
}
exitcurses();
freefilelist(); /* remake the source file list */
makefilelist();
rebuild();
if(errorsfound == true) {
errorsfound = false;
askforreturn();
}
entercurses();
postmsg(""); /* clear any previous message */
totallines = 0;
disprefs = 0;
return (true);
2023-08-04 13:12:06 -04:00
}
2023-08-13 05:21:52 -04:00
void rlinit() {
rl_catch_signals = 0;
rl_catch_sigwinch = 0;
rl_prep_term_function = NULL;
rl_deprep_term_function = NULL;
rl_change_environment = 0;
rl_getc_function = getc_function;
rl_input_available_hook = input_available_hook;
rl_redisplay_function = redisplay_function;
rl_callback_handler_install("", callback_handler);
rl_bind_key(7,
rl_rubout); // XXX: 7 is backspace for some reason (on my system anyways?)
rl_bind_key(KEY_BACKSPACE, rl_rubout);
rl_bind_key(EOF, myexit);
rl_bind_key(ctrl('Z'), ctrl_z);
rl_bind_key(ctrl('Z'), toggle_caseless);
rl_bind_key(ctrl('R'), rebuild_reference);
rl_bind_key(ESC, process_mouse); /* possible unixpc mouse selection */
rl_bind_key(ctrl('X'), process_mouse); /* mouse selection */
// rl_bind_key(ctrl('\\'), /**/); /* bypass bindings */
2023-08-01 13:36:53 -04:00
}