csope/src/readline.c

144 lines
3.5 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-04 13:49:03 -04:00
extern int LINES; // XXX
2023-08-01 13:36:53 -04:00
2023-08-04 13:12:06 -04:00
static int input_available = 0;
static char input_char;
char input_line[PATLEN + 1];
bool do_terminate = false;
2023-08-04 13:12:06 -04:00
bool interpret(int c){
2023-08-04 13:49:03 -04:00
input_char = c;
input_available = 1;
rl_callback_read_char();
2023-08-04 13:12:06 -04:00
2023-08-04 13:49:03 -04:00
return do_terminate;
2023-08-04 13:12:06 -04:00
}
static int getc_function(FILE* ignore){
2023-08-04 13:49:03 -04:00
input_available = 0;
return (int)input_char;
2023-08-01 13:36:53 -04:00
}
2023-08-04 13:12:06 -04:00
static int input_available_hook(){
2023-08-04 13:49:03 -04:00
return input_available;
2023-08-01 13:36:53 -04:00
}
static void redisplay_function(){
2023-08-04 13:49:03 -04:00
window_change |= CH_INPUT;
2023-08-01 13:36:53 -04:00
}
static void callback_handler(char* line){
2023-08-04 13:49:03 -04:00
strncpy(input_line, line, PATLEN);
search();
2023-08-01 13:36:53 -04:00
}
2023-08-04 13:12:06 -04:00
static int interpret_break(){
do_terminate = true;
2023-08-04 13:12:06 -04:00
}
static int ctrl_z(){
2023-08-04 13:49:03 -04:00
kill(0, SIGTSTP);
2023-08-04 13:12:06 -04:00
}
static int toggle_caseless(){
if (caseless == false) {
caseless = true;
2023-08-04 13:49:03 -04:00
postmsg2("Caseless mode is now ON");
} else {
caseless = false;
2023-08-04 13:49:03 -04:00
postmsg2("Caseless mode is now OFF");
}
egrepcaseless(caseless); /* turn on/off -i flag */
2023-08-04 13:12:06 -04:00
}
static int rebuild_reference(){
if (isuptodate == true) {
2023-08-04 13:49:03 -04:00
postmsg("The -d option prevents rebuilding the symbol database");
return(false);
2023-08-04 13:49:03 -04:00
}
exitcurses();
2023-08-04 14:05:31 -04:00
freefilelist(); /* remake the source file list */
2023-08-04 13:49:03 -04:00
makefilelist();
rebuild();
if (errorsfound == true) {
errorsfound = false;
2023-08-04 13:49:03 -04:00
askforreturn();
}
2023-08-04 13:49:03 -04:00
entercurses();
2023-08-04 14:05:31 -04:00
clearmsg(); /* clear any previous message */
2023-08-04 13:49:03 -04:00
totallines = 0;
disprefs = 0;
2023-08-04 13:49:03 -04:00
topline = nextline = 1;
return(true);
2023-08-04 13:12:06 -04:00
}
static int process_mouse(){
2023-08-04 13:49:03 -04:00
int i;
MOUSE* p;
if ((p = getmouseaction(DUMMYCHAR)) == NULL) {
return(false); /* unknown control sequence */
2023-08-04 13:49:03 -04:00
}
/* if the button number is a scrollbar tag */
if (p->button == '0') {
//scrollbar(p); // XXX
return(false);
}
2023-08-04 13:49:03 -04:00
/* ignore a sweep */
if (p->x2 >= 0) {
return(false);
2023-08-04 13:49:03 -04:00
}
/* if this is a line selection */
if (p->y1 > FLDLINE) {
/* find the selected line */
/* note: the selection is forced into range */
for (i = disprefs - 1; i > 0; --i) {
if (p->y1 >= displine[i]) {
return(false);
2023-08-04 13:49:03 -04:00
}
}
/* display it in the file with the editor */
editref(i);
} else { /* this is an input field selection */
field = p->y1 - FLDLINE;
/* force it into range */
if (field >= FIELDS) {
field = FIELDS - 1;
}
resetcmd();
return(false);
2023-08-04 13:49:03 -04:00
}
2023-08-04 13:12:06 -04:00
}
2023-08-01 13:36:53 -04:00
void rlinit(){
2023-08-04 13:49:03 -04:00
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);
2023-08-04 16:35:56 -04:00
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, exit);
2023-08-04 13:49:03 -04:00
rl_bind_key(ctrl('D'), interpret_break); //XXX: why the fuck does it not work if its the first char?
rl_bind_key(ctrl('Z'), ctrl_z);
rl_bind_key(ctrl('Z'), toggle_caseless);
rl_bind_key(ctrl('R'), rebuild_reference);
2023-08-04 14:05:31 -04:00
rl_bind_key(ESC, process_mouse); /* possible unixpc mouse selection */
2023-08-04 13:49:03 -04:00
rl_bind_key(ctrl('X'), process_mouse); /* mouse selection */
2023-08-04 14:05:31 -04:00
//rl_bind_key(ctrl('\\'), /**/); /* bypass bindings */
2023-08-01 13:36:53 -04:00
}