2 * ========================================================================
3 * Copyright 2006-2007 University of Washington
4 * Copyright 2013-2022 Eduardo Chappa
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
14 * Program: Key binding routines
17 /* This file is for functions having to do with key bindings,
18 descriptions, help commands, and command line execution.
20 written 11-feb-86 by Daniel Lawrence
25 int arraylen(char **);
28 * help - help function for pico (UW pared down version of uemacs).
29 * this function will intentionally garbage with helpful
30 * tips and then wait for a ' ' to be hit to then update the
35 static char *helptext
[] = {
36 /* TRANSLATORS: The next several lines go together as help text.
37 Leave the ~ characters where they are, they cause the following
38 character to be printed in boldface as long as the first character
39 of the line is also a ~. */
40 N_(" Pico Help Text"),
42 N_(" Pico is designed to be a simple, easy-to-use text editor with a"),
43 N_(" layout very similar to the Alpine mailer. The status line at the"),
44 N_(" top of the display shows pico's version, the current file being"),
45 N_(" edited and whether or not there are outstanding modifications"),
46 N_(" that have not been saved. The third line from the bottom is used"),
47 N_(" to report informational messages and for additional command input."),
48 N_(" The bottom two lines list the available editing commands."),
50 N_(" Each character typed is automatically inserted into the buffer"),
51 N_(" at the current cursor position. Editing commands and cursor"),
52 N_(" movement (besides arrow keys) are given to pico by typing"),
53 N_(" special control-key sequences. A caret, '^', is used to denote"),
54 N_("~ the control key, sometimes marked \"CTRL\", so the ~C~T~R~L~-~q key"),
55 N_("~ combination is written as ~^~Q."),
57 N_(" The following functions are available in pico (where applicable,"),
58 N_(" corresponding function key commands are in parentheses)."),
60 N_("~ ~^~G (~F~1) Display this help text."),
62 N_("~ ~^~F move Forward a character."),
63 N_("~ ~^~B move Backward a character."),
64 N_("~ ~^~P move to the Previous line."),
65 N_("~ ~^~N move to the Next line."),
66 N_("~ ~^~A move to the beginning of the current line."),
67 N_("~ ~^~E move to the End of the current line."),
68 N_("~ ~^~V (~F~8) move forward a page of text."),
69 N_("~ ~^~Y (~F~7) move backward a page of text."),
71 N_("~ ~^~W (~F~6) Search for (where is) text, neglecting case."),
72 N_("~ ~^~L Refresh the display."),
74 N_("~ ~^~D Delete the character at the cursor position."),
75 N_("~ ~^~^ Mark cursor position as beginning of selected text."),
76 N_(" Note: Setting mark when already set unselects text."),
77 N_("~ ~^~K (~F~9) Cut selected text (displayed in inverse characters)."),
78 N_(" Note: The selected text's boundary on the cursor side"),
79 N_(" ends at the left edge of the cursor. So, with "),
80 N_(" selected text to the left of the cursor, the "),
81 N_(" character under the cursor is not selected."),
82 N_("~ ~^~U (~F~1~0) Uncut (paste) last cut text inserting it at the"),
83 N_(" current cursor position."),
84 N_("~ ~^~I Insert a tab at the current cursor position."),
86 N_("~ ~^~J (~F~4) Format (justify) the current paragraph."),
87 N_(" Note: paragraphs delimited by blank lines or indentation."),
88 N_("~ ~^~T (~F~1~2) To invoke the spelling checker"),
89 N_("~ ~^~C (~F~1~1) Report current cursor position"),
91 N_("~ ~^~R (~F~5) Insert an external file at the current cursor position."),
92 N_("~ ~^~O (~F~3) Output the current buffer to a file, saving it."),
93 N_("~ ~^~X (~F~2) Exit pico, saving buffer."),
102 * arraylen - return the number of bytes in an array of char
105 arraylen(char **array
)
109 while(array
[i
++] != NULL
) ;
115 * whelp - display help text for the composer and pico
120 if(term
.t_mrow
== 0){ /* blank keymenu in effect */
122 /* cause keymenu display */
132 VARS_TO_SAVE
*saved_state
;
134 saved_state
= save_pico_state();
135 (*Pmaster
->helper
)(Pmaster
->composer_help
,
137 ? _("Help for the Alpine Composer")
138 : _("Help for Signature Editor"),
141 restore_pico_state(saved_state
);
142 free_pico_state(saved_state
);
146 picosigs(); /* restore any altered handlers */
147 curwp
->w_flag
|= WFMODE
;
148 if(km_popped
) /* this will unpop us */
149 curwp
->w_flag
|= WFHARD
;
152 int mrow_was_zero
= 0;
154 /* always want keyhelp showing during help */
155 if(term
.t_mrow
== 0){
160 /* TRANSLATORS: Pico is the name of a program */
161 pico_help(helptext
, _("Help for Pico"), 1);
162 /* put it back the way it was */
171 static KEYMENU menu_scroll
[] = {
172 {NULL
, NULL
, KS_NONE
}, {NULL
, NULL
, KS_NONE
},
173 {NULL
, NULL
, KS_NONE
}, {NULL
, NULL
, KS_NONE
},
174 {NULL
, NULL
, KS_NONE
}, {NULL
, NULL
, KS_NONE
},
175 {"^X", N_("Exit Help"), KS_NONE
}, {NULL
, NULL
, KS_NONE
},
176 {NULL
, NULL
, KS_NONE
}, {NULL
, NULL
, KS_NONE
},
177 {NULL
, NULL
, KS_NONE
}, {NULL
, NULL
, KS_NONE
}
183 #define OVERLAP 2 /* displayed page overlap */
186 * scrollw - takes beginning row and ending row to display an array
187 * of text lines. returns either 0 if scrolling terminated
188 * normally or the value of a ctrl character typed to end it.
191 * 01/11/89 - added stripe call if 1st char is tilde - '~'
195 wscrollw(int begrow
, int endrow
, char *utf8textp
[], int textlen
)
197 register int loffset
= 0;
198 register int prevoffset
= -1;
202 register int done
= 0;
206 dlines
= endrow
- begrow
- 1;
209 * display a page loop ...
211 if(prevoffset
!= loffset
){
212 for(i
= 0; i
< dlines
; i
++){
213 movecursor(i
+ begrow
, 0);
215 if((loffset
+i
) < textlen
){
216 buf
= _(&(utf8textp
[loffset
+i
][0]));
219 wstripe(begrow
+i
, 0, buf
, '~');
227 * put up the options prompt
229 movecursor(begrow
+ dlines
, 0);
230 cont
= (loffset
+dlines
< textlen
);
231 if(cont
){ /* continue ? */
232 menu_scroll
[NEXT_KEY
].name
= "^V";
233 /* TRANSLATORS: Next Page, a command key label */
234 menu_scroll
[NEXT_KEY
].label
= N_("Next Pg");
237 menu_scroll
[NEXT_KEY
].name
= NULL
;
240 menu_scroll
[PREV_KEY
].name
= "^Y";
241 /* TRANSLATORS: Previous Page */
242 menu_scroll
[PREV_KEY
].label
= N_("Prev Pg");
245 menu_scroll
[PREV_KEY
].name
= NULL
;
247 wkeyhelp(menu_scroll
);
254 prevoffset
= loffset
;
256 case (CTRL
|'X') : /* quit */
260 case (CTRL
|'Y') : /* prev page */
261 case F7
: /* prev page */
262 if((loffset
-dlines
-OVERLAP
) > 0){
263 loffset
-= (dlines
-OVERLAP
);
275 case (CTRL
|'V') : /* next page */
278 loffset
+= (dlines
-OVERLAP
);
284 case '\016' : /* prev-line */
291 case '\020' : /* prev-line */
298 case '\014' : /* refresh */
299 case (CTRL
|'L') : /* refresh */
308 * We don't handle window resize events correctly when in pico help.
309 * resize_pico() redraws the edit window instead of the help window.
310 * A ^L will redraw the help text. What we'd like is something like
311 * a KEY_RESIZE return from GetKey. If we had that we could exit
312 * wscrollw with a FALSE return value and have that cause us to loop
313 * back into wscrollw with the adjusted size. That would still mean
314 * the edit text would be redrawn first...
328 * normalize_cmd - given a char and list of function key to command key
329 * mappings, return, depending on gmode, the right command.
330 * The list is an array of (Fkey, command-key) pairs.
331 * sc is the index in the array that means to ignore fkey
332 * vs. command key mapping
334 * rules: 1. if c not in table (either fkey or command), let it thru
335 * 2. if c matches, but in other mode, punt it
338 normalize_cmd(UCS c
, UCS list
[][2], int sc
)
342 for(i
=0; i
< 12; i
++){
343 if(c
== list
[i
][(FUNC
&c
) ? 0 : 1]){ /* in table? */
344 if(i
== sc
) /* SPECIAL CASE! */
347 if(list
[i
][1] == NODATA
) /* no mapping ! */
350 if(((FUNC
&c
) == FUNC
) && !((gmode
&MDFKEY
) == MDFKEY
))
351 return(c
); /* not allowed, let caller handle it */
353 return(list
[i
][1]); /* never return func keys */
362 * rebind - replace the first function with the second
365 rebindfunc(int (*a
)(int, int), int (*b
)(int, int))
369 kp
= (Pmaster
) ? &keytab
[0] : &pkeytab
[0];
371 while(kp
->k_fp
!= NULL
){ /* go thru whole list, and */
373 kp
->k_fp
= b
; /* replace all occurrences */
380 * bindtokey - bind function f to command c
383 bindtokey(UCS c
, int (*f
)(int, int))
385 KEYTAB
*kp
, *ktab
= (Pmaster
) ? &keytab
[0] : &pkeytab
[0];
387 for(kp
= ktab
; kp
->k_fp
; kp
++)
389 kp
->k_fp
= f
; /* set to new function */
393 /* not found? create new binding */
394 if(!kp
->k_fp
&& kp
< &ktab
[NBINDS
]){
395 kp
->k_code
= c
; /* assign new code and function */
397 (++kp
)->k_code
= 0; /* and null out next slot */