cmdbuf.c (009e81b16465ea457c0e63fd49fe77f47cc27a5a) cmdbuf.c (1ea316270f1f75922ac53976d5d8808a41442f46)
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information, see the README file.
8 */

--- 18 unchanged lines hidden (view full) ---

27static int cmd_col; /* Current column of the cursor */
28static int prompt_col; /* Column of cursor just after prompt */
29static char *cp; /* Pointer into cmdbuf */
30static int cmd_offset; /* Index into cmdbuf of first displayed char */
31static int literal; /* Next input char should not be interpreted */
32static int updown_match = -1; /* Prefix length in up/down movement */
33
34#if TAB_COMPLETE_FILENAME
1/*
2 * Copyright (C) 1984-2015 Mark Nudelman
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
6 *
7 * For more information, see the README file.
8 */

--- 18 unchanged lines hidden (view full) ---

27static int cmd_col; /* Current column of the cursor */
28static int prompt_col; /* Column of cursor just after prompt */
29static char *cp; /* Pointer into cmdbuf */
30static int cmd_offset; /* Index into cmdbuf of first displayed char */
31static int literal; /* Next input char should not be interpreted */
32static int updown_match = -1; /* Prefix length in up/down movement */
33
34#if TAB_COMPLETE_FILENAME
35static int cmd_complete();
35static int cmd_complete(int action);
36/*
37 * These variables are statics used by cmd_complete.
38 */
39static int in_completion = 0;
40static char *tk_text;
41static char *tk_original;
42static char *tk_ipoint;
43static char *tk_trial;

--- 65 unchanged lines hidden (view full) ---

109static int cmd_mbc_buf_len;
110static int cmd_mbc_buf_index;
111
112
113/*
114 * Reset command buffer (to empty).
115 */
116 public void
36/*
37 * These variables are statics used by cmd_complete.
38 */
39static int in_completion = 0;
40static char *tk_text;
41static char *tk_original;
42static char *tk_ipoint;
43static char *tk_trial;

--- 65 unchanged lines hidden (view full) ---

109static int cmd_mbc_buf_len;
110static int cmd_mbc_buf_index;
111
112
113/*
114 * Reset command buffer (to empty).
115 */
116 public void
117cmd_reset()
117cmd_reset(void)
118{
119 cp = cmdbuf;
120 *cp = '\0';
121 cmd_col = 0;
122 cmd_offset = 0;
123 literal = 0;
124 cmd_mbc_buf_len = 0;
125 updown_match = -1;
126}
127
128/*
129 * Clear command line.
130 */
131 public void
118{
119 cp = cmdbuf;
120 *cp = '\0';
121 cmd_col = 0;
122 cmd_offset = 0;
123 literal = 0;
124 cmd_mbc_buf_len = 0;
125 updown_match = -1;
126}
127
128/*
129 * Clear command line.
130 */
131 public void
132clear_cmd()
132clear_cmd(void)
133{
134 cmd_col = prompt_col = 0;
135 cmd_mbc_buf_len = 0;
136 updown_match = -1;
137}
138
139/*
140 * Display a string, usually as a prompt for input into the command buffer.
141 */
142 public void
133{
134 cmd_col = prompt_col = 0;
135 cmd_mbc_buf_len = 0;
136 updown_match = -1;
137}
138
139/*
140 * Display a string, usually as a prompt for input into the command buffer.
141 */
142 public void
143cmd_putstr(s)
144 char *s;
143cmd_putstr(constant char *s)
145{
146 LWCHAR prev_ch = 0;
147 LWCHAR ch;
144{
145 LWCHAR prev_ch = 0;
146 LWCHAR ch;
148 char *endline = s + strlen(s);
147 constant char *endline = s + strlen(s);
149 while (*s != '\0')
150 {
148 while (*s != '\0')
149 {
151 char *ns = s;
150 constant char *ns = s;
152 ch = step_char(&ns, +1, endline);
153 while (s < ns)
154 putchr(*s++);
155 if (!utf_mode)
156 {
157 cmd_col++;
158 prompt_col++;
159 } else if (!is_composing_char(ch) &&

--- 6 unchanged lines hidden (view full) ---

166 prev_ch = ch;
167 }
168}
169
170/*
171 * How many characters are in the command buffer?
172 */
173 public int
151 ch = step_char(&ns, +1, endline);
152 while (s < ns)
153 putchr(*s++);
154 if (!utf_mode)
155 {
156 cmd_col++;
157 prompt_col++;
158 } else if (!is_composing_char(ch) &&

--- 6 unchanged lines hidden (view full) ---

165 prev_ch = ch;
166 }
167}
168
169/*
170 * How many characters are in the command buffer?
171 */
172 public int
174len_cmdbuf()
173len_cmdbuf(void)
175{
174{
176 char *s = cmdbuf;
177 char *endline = s + strlen(s);
175 constant char *s = cmdbuf;
176 constant char *endline = s + strlen(s);
178 int len = 0;
179
180 while (*s != '\0')
181 {
182 step_char(&s, +1, endline);
183 len++;
184 }
185 return (len);
186}
187
188/*
189 * Common part of cmd_step_right() and cmd_step_left().
190 */
191 static char *
177 int len = 0;
178
179 while (*s != '\0')
180 {
181 step_char(&s, +1, endline);
182 len++;
183 }
184 return (len);
185}
186
187/*
188 * Common part of cmd_step_right() and cmd_step_left().
189 */
190 static char *
192cmd_step_common(p, ch, len, pwidth, bswidth)
193 char *p;
194 LWCHAR ch;
195 int len;
196 int *pwidth;
197 int *bswidth;
191cmd_step_common(constant char *p, LWCHAR ch, int len, int *pwidth, int *bswidth)
198{
199 char *pr;
200
201 if (len == 1)
202 {
203 pr = prchar((int) ch);
204 if (pwidth != NULL || bswidth != NULL)
205 {

--- 45 unchanged lines hidden (view full) ---

251
252 return (pr);
253}
254
255/*
256 * Step a pointer one character right in the command buffer.
257 */
258 static char *
192{
193 char *pr;
194
195 if (len == 1)
196 {
197 pr = prchar((int) ch);
198 if (pwidth != NULL || bswidth != NULL)
199 {

--- 45 unchanged lines hidden (view full) ---

245
246 return (pr);
247}
248
249/*
250 * Step a pointer one character right in the command buffer.
251 */
252 static char *
259cmd_step_right(pp, pwidth, bswidth)
260 char **pp;
261 int *pwidth;
262 int *bswidth;
253cmd_step_right(char **pp, int *pwidth, int *bswidth)
263{
264 char *p = *pp;
254{
255 char *p = *pp;
265 LWCHAR ch = step_char(pp, +1, p + strlen(p));
256 LWCHAR ch = step_char((constant char **)pp, +1, p + strlen(p));
266
267 return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
268}
269
270/*
271 * Step a pointer one character left in the command buffer.
272 */
273 static char *
257
258 return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
259}
260
261/*
262 * Step a pointer one character left in the command buffer.
263 */
264 static char *
274cmd_step_left(pp, pwidth, bswidth)
275 char **pp;
276 int *pwidth;
277 int *bswidth;
265cmd_step_left(char **pp, int *pwidth, int *bswidth)
278{
279 char *p = *pp;
266{
267 char *p = *pp;
280 LWCHAR ch = step_char(pp, -1, cmdbuf);
268 LWCHAR ch = step_char((constant char **)pp, -1, cmdbuf);
281
282 return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
283}
284
285/*
286 * Repaint the line from cp onwards.
287 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
288 */
289 static void
269
270 return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
271}
272
273/*
274 * Repaint the line from cp onwards.
275 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
276 */
277 static void
290cmd_repaint(old_cp)
291 char *old_cp;
278cmd_repaint(char *old_cp)
292{
293 /*
294 * Repaint the line from the current position.
295 */
296 clear_eol();
297 while (*cp != '\0')
298 {
299 char *np = cp;
300 int width;
279{
280 /*
281 * Repaint the line from the current position.
282 */
283 clear_eol();
284 while (*cp != '\0')
285 {
286 char *np = cp;
287 int width;
301 char *pr = cmd_step_right(&np, &width, NULL);
288 constant char *pr = cmd_step_right(&np, &width, NULL);
302 if (cmd_col + width >= sc_width)
303 break;
304 cp = np;
305 putstr(pr);
306 cmd_col += width;
307 }
308 while (*cp != '\0')
309 {

--- 13 unchanged lines hidden (view full) ---

323 cmd_left();
324}
325
326/*
327 * Put the cursor at "home" (just after the prompt),
328 * and set cp to the corresponding char in cmdbuf.
329 */
330 static void
289 if (cmd_col + width >= sc_width)
290 break;
291 cp = np;
292 putstr(pr);
293 cmd_col += width;
294 }
295 while (*cp != '\0')
296 {

--- 13 unchanged lines hidden (view full) ---

310 cmd_left();
311}
312
313/*
314 * Put the cursor at "home" (just after the prompt),
315 * and set cp to the corresponding char in cmdbuf.
316 */
317 static void
331cmd_home()
318cmd_home(void)
332{
333 while (cmd_col > prompt_col)
334 {
335 int width, bswidth;
336
337 cmd_step_left(&cp, &width, &bswidth);
338 while (bswidth-- > 0)
339 putbs();
340 cmd_col -= width;
341 }
342
343 cp = &cmdbuf[cmd_offset];
344}
345
346/*
347 * Shift the cmdbuf display left a half-screen.
348 */
349 static void
319{
320 while (cmd_col > prompt_col)
321 {
322 int width, bswidth;
323
324 cmd_step_left(&cp, &width, &bswidth);
325 while (bswidth-- > 0)
326 putbs();
327 cmd_col -= width;
328 }
329
330 cp = &cmdbuf[cmd_offset];
331}
332
333/*
334 * Shift the cmdbuf display left a half-screen.
335 */
336 static void
350cmd_lshift()
337cmd_lshift(void)
351{
352 char *s;
353 char *save_cp;
354 int cols;
355
356 /*
357 * Start at the first displayed char, count how far to the
358 * right we'd have to move to reach the center of the screen.

--- 21 unchanged lines hidden (view full) ---

380 cmd_home();
381 cmd_repaint(save_cp);
382}
383
384/*
385 * Shift the cmdbuf display right a half-screen.
386 */
387 static void
338{
339 char *s;
340 char *save_cp;
341 int cols;
342
343 /*
344 * Start at the first displayed char, count how far to the
345 * right we'd have to move to reach the center of the screen.

--- 21 unchanged lines hidden (view full) ---

367 cmd_home();
368 cmd_repaint(save_cp);
369}
370
371/*
372 * Shift the cmdbuf display right a half-screen.
373 */
374 static void
388cmd_rshift()
375cmd_rshift(void)
389{
390 char *s;
391 char *save_cp;
392 int cols;
393
394 /*
395 * Start at the first displayed char, count how far to the
396 * left we'd have to move to traverse a half-screen width

--- 13 unchanged lines hidden (view full) ---

410 cmd_home();
411 cmd_repaint(save_cp);
412}
413
414/*
415 * Move cursor right one character.
416 */
417 static int
376{
377 char *s;
378 char *save_cp;
379 int cols;
380
381 /*
382 * Start at the first displayed char, count how far to the
383 * left we'd have to move to traverse a half-screen width

--- 13 unchanged lines hidden (view full) ---

397 cmd_home();
398 cmd_repaint(save_cp);
399}
400
401/*
402 * Move cursor right one character.
403 */
404 static int
418cmd_right()
405cmd_right(void)
419{
420 char *pr;
421 char *ncp;
422 int width;
423
424 if (*cp == '\0')
425 {
426 /* Already at the end of the line. */

--- 18 unchanged lines hidden (view full) ---

445 }
446 return (CC_OK);
447}
448
449/*
450 * Move cursor left one character.
451 */
452 static int
406{
407 char *pr;
408 char *ncp;
409 int width;
410
411 if (*cp == '\0')
412 {
413 /* Already at the end of the line. */

--- 18 unchanged lines hidden (view full) ---

432 }
433 return (CC_OK);
434}
435
436/*
437 * Move cursor left one character.
438 */
439 static int
453cmd_left()
440cmd_left(void)
454{
455 char *ncp;
456 int width, bswidth;
457
458 if (cp <= cmdbuf)
459 {
460 /* Already at the beginning of the line */
461 return (CC_OK);

--- 13 unchanged lines hidden (view full) ---

475 putbs();
476 return (CC_OK);
477}
478
479/*
480 * Insert a char into the command buffer, at the current position.
481 */
482 static int
441{
442 char *ncp;
443 int width, bswidth;
444
445 if (cp <= cmdbuf)
446 {
447 /* Already at the beginning of the line */
448 return (CC_OK);

--- 13 unchanged lines hidden (view full) ---

462 putbs();
463 return (CC_OK);
464}
465
466/*
467 * Insert a char into the command buffer, at the current position.
468 */
469 static int
483cmd_ichar(cs, clen)
484 char *cs;
485 int clen;
470cmd_ichar(char *cs, int clen)
486{
487 char *s;
488
489 if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
490 {
491 /* No room in the command buffer for another char. */
492 bell();
493 return (CC_ERROR);

--- 18 unchanged lines hidden (view full) ---

512 return (CC_OK);
513}
514
515/*
516 * Backspace in the command buffer.
517 * Delete the char to the left of the cursor.
518 */
519 static int
471{
472 char *s;
473
474 if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
475 {
476 /* No room in the command buffer for another char. */
477 bell();
478 return (CC_ERROR);

--- 18 unchanged lines hidden (view full) ---

497 return (CC_OK);
498}
499
500/*
501 * Backspace in the command buffer.
502 * Delete the char to the left of the cursor.
503 */
504 static int
520cmd_erase()
505cmd_erase(void)
521{
506{
522 register char *s;
507 char *s;
523 int clen;
524
525 if (cp == cmdbuf)
526 {
527 /*
528 * Backspace past beginning of the buffer:
529 * this usually means abort the command.
530 */

--- 30 unchanged lines hidden (view full) ---

561 return (CC_QUIT);
562 return (CC_OK);
563}
564
565/*
566 * Delete the char under the cursor.
567 */
568 static int
508 int clen;
509
510 if (cp == cmdbuf)
511 {
512 /*
513 * Backspace past beginning of the buffer:
514 * this usually means abort the command.
515 */

--- 30 unchanged lines hidden (view full) ---

546 return (CC_QUIT);
547 return (CC_OK);
548}
549
550/*
551 * Delete the char under the cursor.
552 */
553 static int
569cmd_delete()
554cmd_delete(void)
570{
571 if (*cp == '\0')
572 {
573 /* At end of string; there is no char under the cursor. */
574 return (CC_OK);
575 }
576 /*
577 * Move right, then use cmd_erase.
578 */
579 cmd_right();
580 cmd_erase();
581 return (CC_OK);
582}
583
584/*
585 * Delete the "word" to the left of the cursor.
586 */
587 static int
555{
556 if (*cp == '\0')
557 {
558 /* At end of string; there is no char under the cursor. */
559 return (CC_OK);
560 }
561 /*
562 * Move right, then use cmd_erase.
563 */
564 cmd_right();
565 cmd_erase();
566 return (CC_OK);
567}
568
569/*
570 * Delete the "word" to the left of the cursor.
571 */
572 static int
588cmd_werase()
573cmd_werase(void)
589{
590 if (cp > cmdbuf && cp[-1] == ' ')
591 {
592 /*
593 * If the char left of cursor is a space,
594 * erase all the spaces left of cursor (to the first non-space).
595 */
596 while (cp > cmdbuf && cp[-1] == ' ')

--- 9 unchanged lines hidden (view full) ---

606 }
607 return (CC_OK);
608}
609
610/*
611 * Delete the "word" under the cursor.
612 */
613 static int
574{
575 if (cp > cmdbuf && cp[-1] == ' ')
576 {
577 /*
578 * If the char left of cursor is a space,
579 * erase all the spaces left of cursor (to the first non-space).
580 */
581 while (cp > cmdbuf && cp[-1] == ' ')

--- 9 unchanged lines hidden (view full) ---

591 }
592 return (CC_OK);
593}
594
595/*
596 * Delete the "word" under the cursor.
597 */
598 static int
614cmd_wdelete()
599cmd_wdelete(void)
615{
616 if (*cp == ' ')
617 {
618 /*
619 * If the char under the cursor is a space,
620 * delete it and all the spaces right of cursor.
621 */
622 while (*cp == ' ')

--- 9 unchanged lines hidden (view full) ---

632 }
633 return (CC_OK);
634}
635
636/*
637 * Delete all chars in the command buffer.
638 */
639 static int
600{
601 if (*cp == ' ')
602 {
603 /*
604 * If the char under the cursor is a space,
605 * delete it and all the spaces right of cursor.
606 */
607 while (*cp == ' ')

--- 9 unchanged lines hidden (view full) ---

617 }
618 return (CC_OK);
619}
620
621/*
622 * Delete all chars in the command buffer.
623 */
624 static int
640cmd_kill()
625cmd_kill(void)
641{
642 if (cmdbuf[0] == '\0')
643 {
644 /* Buffer is already empty; abort the current command. */
645 return (CC_QUIT);
646 }
647 cmd_offset = 0;
648 cmd_home();

--- 9 unchanged lines hidden (view full) ---

658 return (CC_QUIT);
659 return (CC_OK);
660}
661
662/*
663 * Select an mlist structure to be the current command history.
664 */
665 public void
626{
627 if (cmdbuf[0] == '\0')
628 {
629 /* Buffer is already empty; abort the current command. */
630 return (CC_QUIT);
631 }
632 cmd_offset = 0;
633 cmd_home();

--- 9 unchanged lines hidden (view full) ---

643 return (CC_QUIT);
644 return (CC_OK);
645}
646
647/*
648 * Select an mlist structure to be the current command history.
649 */
650 public void
666set_mlist(mlist, cmdflags)
667 void *mlist;
668 int cmdflags;
651set_mlist(constant void *mlist, int cmdflags)
669{
670#if CMD_HISTORY
671 curr_mlist = (struct mlist *) mlist;
672 curr_cmdflags = cmdflags;
673
674 /* Make sure the next up-arrow moves to the last string in the mlist. */
675 if (curr_mlist != NULL)
676 curr_mlist->curr_mp = curr_mlist;
677#endif
678}
679
680#if CMD_HISTORY
681/*
682 * Move up or down in the currently selected command history list.
683 * Only consider entries whose first updown_match chars are equal to
684 * cmdbuf's corresponding chars.
685 */
686 static int
652{
653#if CMD_HISTORY
654 curr_mlist = (struct mlist *) mlist;
655 curr_cmdflags = cmdflags;
656
657 /* Make sure the next up-arrow moves to the last string in the mlist. */
658 if (curr_mlist != NULL)
659 curr_mlist->curr_mp = curr_mlist;
660#endif
661}
662
663#if CMD_HISTORY
664/*
665 * Move up or down in the currently selected command history list.
666 * Only consider entries whose first updown_match chars are equal to
667 * cmdbuf's corresponding chars.
668 */
669 static int
687cmd_updown(action)
688 int action;
670cmd_updown(int action)
689{
690 char *s;
691 struct mlist *ml;
692
693 if (curr_mlist == NULL)
694 {
695 /*
696 * The current command has no history list.

--- 45 unchanged lines hidden (view full) ---

742 return (CC_OK);
743}
744#endif
745
746/*
747 * Add a string to an mlist.
748 */
749 public void
671{
672 char *s;
673 struct mlist *ml;
674
675 if (curr_mlist == NULL)
676 {
677 /*
678 * The current command has no history list.

--- 45 unchanged lines hidden (view full) ---

724 return (CC_OK);
725}
726#endif
727
728/*
729 * Add a string to an mlist.
730 */
731 public void
750cmd_addhist(mlist, cmd, modified)
751 struct mlist *mlist;
752 char *cmd;
753 int modified;
732cmd_addhist(struct mlist *constant mlist, char *cmd, int modified)
754{
755#if CMD_HISTORY
756 struct mlist *ml;
757
758 /*
759 * Don't save a trivial command.
760 */
761 if (strlen(cmd) == 0)

--- 26 unchanged lines hidden (view full) ---

788#endif
789}
790
791/*
792 * Accept the command in the command buffer.
793 * Add it to the currently selected history list.
794 */
795 public void
733{
734#if CMD_HISTORY
735 struct mlist *ml;
736
737 /*
738 * Don't save a trivial command.
739 */
740 if (strlen(cmd) == 0)

--- 26 unchanged lines hidden (view full) ---

767#endif
768}
769
770/*
771 * Accept the command in the command buffer.
772 * Add it to the currently selected history list.
773 */
774 public void
796cmd_accept()
775cmd_accept(void)
797{
798#if CMD_HISTORY
799 /*
800 * Nothing to do if there is no currently selected history list.
801 */
802 if (curr_mlist == NULL)
803 return;
804 cmd_addhist(curr_mlist, cmdbuf, 1);

--- 5 unchanged lines hidden (view full) ---

810 * Try to perform a line-edit function on the command buffer,
811 * using a specified char as a line-editing command.
812 * Returns:
813 * CC_PASS The char does not invoke a line edit function.
814 * CC_OK Line edit function done.
815 * CC_QUIT The char requests the current command to be aborted.
816 */
817 static int
776{
777#if CMD_HISTORY
778 /*
779 * Nothing to do if there is no currently selected history list.
780 */
781 if (curr_mlist == NULL)
782 return;
783 cmd_addhist(curr_mlist, cmdbuf, 1);

--- 5 unchanged lines hidden (view full) ---

789 * Try to perform a line-edit function on the command buffer,
790 * using a specified char as a line-editing command.
791 * Returns:
792 * CC_PASS The char does not invoke a line edit function.
793 * CC_OK Line edit function done.
794 * CC_QUIT The char requests the current command to be aborted.
795 */
796 static int
818cmd_edit(c)
819 int c;
797cmd_edit(int c)
820{
821 int action;
822 int flags;
823
824#if TAB_COMPLETE_FILENAME
825#define not_in_completion() in_completion = 0
826#else
827#define not_in_completion()

--- 98 unchanged lines hidden (view full) ---

926 }
927}
928
929#if TAB_COMPLETE_FILENAME
930/*
931 * Insert a string into the command buffer, at the current position.
932 */
933 static int
798{
799 int action;
800 int flags;
801
802#if TAB_COMPLETE_FILENAME
803#define not_in_completion() in_completion = 0
804#else
805#define not_in_completion()

--- 98 unchanged lines hidden (view full) ---

904 }
905}
906
907#if TAB_COMPLETE_FILENAME
908/*
909 * Insert a string into the command buffer, at the current position.
910 */
911 static int
934cmd_istr(str)
935 char *str;
912cmd_istr(char *str)
936{
937 char *s;
938 int action;
939 char *endline = str + strlen(str);
940
941 for (s = str; *s != '\0'; )
942 {
943 char *os = s;
913{
914 char *s;
915 int action;
916 char *endline = str + strlen(str);
917
918 for (s = str; *s != '\0'; )
919 {
920 char *os = s;
944 step_char(&s, +1, endline);
921 step_char((constant char **)&s, +1, endline);
945 action = cmd_ichar(os, s - os);
946 if (action != CC_OK)
947 {
948 bell();
949 return (action);
950 }
951 }
952 return (CC_OK);
953}
954
955/*
956 * Find the beginning and end of the "current" word.
957 * This is the word which the cursor (cp) is inside or at the end of.
958 * Return pointer to the beginning of the word and put the
959 * cursor at the end of the word.
960 */
961 static char *
922 action = cmd_ichar(os, s - os);
923 if (action != CC_OK)
924 {
925 bell();
926 return (action);
927 }
928 }
929 return (CC_OK);
930}
931
932/*
933 * Find the beginning and end of the "current" word.
934 * This is the word which the cursor (cp) is inside or at the end of.
935 * Return pointer to the beginning of the word and put the
936 * cursor at the end of the word.
937 */
938 static char *
962delimit_word()
939delimit_word(void)
963{
964 char *word;
965#if SPACES_IN_FILENAMES
966 char *p;
967 int delim_quoted = 0;
968 int meta_quoted = 0;
969 char *esc = get_meta_escape();
970 int esclen = (int) strlen(esc);

--- 70 unchanged lines hidden (view full) ---

1041}
1042
1043/*
1044 * Set things up to enter completion mode.
1045 * Expand the word under the cursor into a list of filenames
1046 * which start with that word, and set tk_text to that list.
1047 */
1048 static void
940{
941 char *word;
942#if SPACES_IN_FILENAMES
943 char *p;
944 int delim_quoted = 0;
945 int meta_quoted = 0;
946 char *esc = get_meta_escape();
947 int esclen = (int) strlen(esc);

--- 70 unchanged lines hidden (view full) ---

1018}
1019
1020/*
1021 * Set things up to enter completion mode.
1022 * Expand the word under the cursor into a list of filenames
1023 * which start with that word, and set tk_text to that list.
1024 */
1025 static void
1049init_compl()
1026init_compl(void)
1050{
1051 char *word;
1052 char c;
1053
1054 /*
1055 * Get rid of any previous tk_text.
1056 */
1057 if (tk_text != NULL)

--- 46 unchanged lines hidden (view full) ---

1104 }
1105 *cp = c;
1106}
1107
1108/*
1109 * Return the next word in the current completion list.
1110 */
1111 static char *
1027{
1028 char *word;
1029 char c;
1030
1031 /*
1032 * Get rid of any previous tk_text.
1033 */
1034 if (tk_text != NULL)

--- 46 unchanged lines hidden (view full) ---

1081 }
1082 *cp = c;
1083}
1084
1085/*
1086 * Return the next word in the current completion list.
1087 */
1088 static char *
1112next_compl(action, prev)
1113 int action;
1114 char *prev;
1089next_compl(int action, char *prev)
1115{
1116 switch (action)
1117 {
1118 case EC_F_COMPLETE:
1119 return (forw_textlist(&tk_tlist, prev));
1120 case EC_B_COMPLETE:
1121 return (back_textlist(&tk_tlist, prev));
1122 }
1123 /* Cannot happen */
1124 return ("?");
1125}
1126
1127/*
1128 * Complete the filename before (or under) the cursor.
1129 * cmd_complete may be called multiple times. The global in_completion
1130 * remembers whether this call is the first time (create the list),
1131 * or a subsequent time (step thru the list).
1132 */
1133 static int
1090{
1091 switch (action)
1092 {
1093 case EC_F_COMPLETE:
1094 return (forw_textlist(&tk_tlist, prev));
1095 case EC_B_COMPLETE:
1096 return (back_textlist(&tk_tlist, prev));
1097 }
1098 /* Cannot happen */
1099 return ("?");
1100}
1101
1102/*
1103 * Complete the filename before (or under) the cursor.
1104 * cmd_complete may be called multiple times. The global in_completion
1105 * remembers whether this call is the first time (create the list),
1106 * or a subsequent time (step thru the list).
1107 */
1108 static int
1134cmd_complete(action)
1135 int action;
1109cmd_complete(int action)
1136{
1137 char *s;
1138
1139 if (!in_completion || action == EC_EXPAND)
1140 {
1141 /*
1142 * Expand the word under the cursor and
1143 * use the first word in the expansion

--- 80 unchanged lines hidden (view full) ---

1224 * Process a single character of a multi-character command, such as
1225 * a number, or the pattern of a search command.
1226 * Returns:
1227 * CC_OK The char was accepted.
1228 * CC_QUIT The char requests the command to be aborted.
1229 * CC_ERROR The char could not be accepted due to an error.
1230 */
1231 public int
1110{
1111 char *s;
1112
1113 if (!in_completion || action == EC_EXPAND)
1114 {
1115 /*
1116 * Expand the word under the cursor and
1117 * use the first word in the expansion

--- 80 unchanged lines hidden (view full) ---

1198 * Process a single character of a multi-character command, such as
1199 * a number, or the pattern of a search command.
1200 * Returns:
1201 * CC_OK The char was accepted.
1202 * CC_QUIT The char requests the command to be aborted.
1203 * CC_ERROR The char could not be accepted due to an error.
1204 */
1205 public int
1232cmd_char(c)
1233 int c;
1206cmd_char(int c)
1234{
1235 int action;
1236 int len;
1237
1238 if (!utf_mode)
1239 {
1240 cmd_mbc_buf[0] = c;
1241 len = 1;

--- 72 unchanged lines hidden (view full) ---

1314 */
1315 return (cmd_ichar(cmd_mbc_buf, len));
1316}
1317
1318/*
1319 * Return the number currently in the command buffer.
1320 */
1321 public LINENUM
1207{
1208 int action;
1209 int len;
1210
1211 if (!utf_mode)
1212 {
1213 cmd_mbc_buf[0] = c;
1214 len = 1;

--- 72 unchanged lines hidden (view full) ---

1287 */
1288 return (cmd_ichar(cmd_mbc_buf, len));
1289}
1290
1291/*
1292 * Return the number currently in the command buffer.
1293 */
1294 public LINENUM
1322cmd_int(frac)
1323 long *frac;
1295cmd_int(long *frac)
1324{
1325 char *p;
1326 LINENUM n = 0;
1327 int err;
1328
1329 for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
1330 n = (n * 10) + (*p - '0');
1331 *frac = 0;

--- 4 unchanged lines hidden (view full) ---

1336 }
1337 return (n);
1338}
1339
1340/*
1341 * Return a pointer to the command buffer.
1342 */
1343 public char *
1296{
1297 char *p;
1298 LINENUM n = 0;
1299 int err;
1300
1301 for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
1302 n = (n * 10) + (*p - '0');
1303 *frac = 0;

--- 4 unchanged lines hidden (view full) ---

1308 }
1309 return (n);
1310}
1311
1312/*
1313 * Return a pointer to the command buffer.
1314 */
1315 public char *
1344get_cmdbuf()
1316get_cmdbuf(void)
1345{
1346 return (cmdbuf);
1347}
1348
1349#if CMD_HISTORY
1350/*
1351 * Return the last (most recent) string in the current command history.
1352 */
1353 public char *
1317{
1318 return (cmdbuf);
1319}
1320
1321#if CMD_HISTORY
1322/*
1323 * Return the last (most recent) string in the current command history.
1324 */
1325 public char *
1354cmd_lastpattern()
1326cmd_lastpattern(void)
1355{
1356 if (curr_mlist == NULL)
1357 return (NULL);
1358 return (curr_mlist->curr_mp->prev->string);
1359}
1360#endif
1361
1362#if CMD_HISTORY
1363/*
1364 */
1365 static int
1327{
1328 if (curr_mlist == NULL)
1329 return (NULL);
1330 return (curr_mlist->curr_mp->prev->string);
1331}
1332#endif
1333
1334#if CMD_HISTORY
1335/*
1336 */
1337 static int
1366mlist_size(ml)
1367 struct mlist *ml;
1338mlist_size(struct mlist *ml)
1368{
1369 int size = 0;
1370 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1371 ++size;
1372 return size;
1373}
1374
1375/*
1376 * Get the name of the history file.
1377 */
1378 static char *
1339{
1340 int size = 0;
1341 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1342 ++size;
1343 return size;
1344}
1345
1346/*
1347 * Get the name of the history file.
1348 */
1349 static char *
1379histfile_name()
1350histfile_name(void)
1380{
1381 char *home;
1382 char *name;
1383 int len;
1384
1385 /* See if filename is explicitly specified by $LESSHISTFILE. */
1386 name = lgetenv("LESSHISTFILE");
1387 if (name != NULL && *name != '\0')

--- 23 unchanged lines hidden (view full) ---

1411 SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
1412 return (name);
1413}
1414
1415/*
1416 * Read a .lesshst file and call a callback for each line in the file.
1417 */
1418 static void
1351{
1352 char *home;
1353 char *name;
1354 int len;
1355
1356 /* See if filename is explicitly specified by $LESSHISTFILE. */
1357 name = lgetenv("LESSHISTFILE");
1358 if (name != NULL && *name != '\0')

--- 23 unchanged lines hidden (view full) ---

1382 SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
1383 return (name);
1384}
1385
1386/*
1387 * Read a .lesshst file and call a callback for each line in the file.
1388 */
1389 static void
1419read_cmdhist2(action, uparam, skip_search, skip_shell)
1420 void (*action)(void*,struct mlist*,char*);
1421 void *uparam;
1422 int skip_search;
1423 int skip_shell;
1390read_cmdhist2(void (*action)(void*,struct mlist*,char*), void *uparam,
1391 int skip_search, int skip_shell)
1424{
1425 struct mlist *ml = NULL;
1426 char line[CMDBUF_SIZE];
1427 char *filename;
1428 FILE *f;
1429 char *p;
1430 int *skip = NULL;
1431

--- 43 unchanged lines hidden (view full) ---

1475 (*action)(uparam, ml, line+1);
1476 }
1477 }
1478 }
1479 fclose(f);
1480}
1481
1482 static void
1392{
1393 struct mlist *ml = NULL;
1394 char line[CMDBUF_SIZE];
1395 char *filename;
1396 FILE *f;
1397 char *p;
1398 int *skip = NULL;
1399

--- 43 unchanged lines hidden (view full) ---

1443 (*action)(uparam, ml, line+1);
1444 }
1445 }
1446 }
1447 fclose(f);
1448}
1449
1450 static void
1483read_cmdhist(action, uparam, skip_search, skip_shell)
1484 void (*action)(void*,struct mlist*,char*);
1485 void *uparam;
1486 int skip_search;
1487 int skip_shell;
1451read_cmdhist(void (*action)(void*,struct mlist*,char*), void *uparam,
1452 int skip_search, int skip_shell)
1488{
1489 read_cmdhist2(action, uparam, skip_search, skip_shell);
1490 (*action)(uparam, NULL, NULL); /* signal end of file */
1491}
1492
1493 static void
1494addhist_init(void *uparam, struct mlist *ml, char *string)
1495{
1496 if (ml == NULL || string == NULL)
1497 return;
1498 cmd_addhist(ml, string, 0);
1499}
1500#endif /* CMD_HISTORY */
1501
1502/*
1503 * Initialize history from a .lesshist file.
1504 */
1505 public void
1453{
1454 read_cmdhist2(action, uparam, skip_search, skip_shell);
1455 (*action)(uparam, NULL, NULL); /* signal end of file */
1456}
1457
1458 static void
1459addhist_init(void *uparam, struct mlist *ml, char *string)
1460{
1461 if (ml == NULL || string == NULL)
1462 return;
1463 cmd_addhist(ml, string, 0);
1464}
1465#endif /* CMD_HISTORY */
1466
1467/*
1468 * Initialize history from a .lesshist file.
1469 */
1470 public void
1506init_cmdhist()
1471init_cmdhist(void)
1507{
1508#if CMD_HISTORY
1509 read_cmdhist(&addhist_init, NULL, 0, 0);
1510#endif /* CMD_HISTORY */
1511}
1512
1513/*
1514 * Write the header for a section of the history file.
1515 */
1516#if CMD_HISTORY
1517 static void
1472{
1473#if CMD_HISTORY
1474 read_cmdhist(&addhist_init, NULL, 0, 0);
1475#endif /* CMD_HISTORY */
1476}
1477
1478/*
1479 * Write the header for a section of the history file.
1480 */
1481#if CMD_HISTORY
1482 static void
1518write_mlist_header(ml, f)
1519 struct mlist *ml;
1520 FILE *f;
1483write_mlist_header(struct mlist *ml, FILE *f)
1521{
1522 if (ml == &mlist_search)
1523 fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1524#if SHELL_ESCAPE || PIPEC
1525 else if (ml == &mlist_shell)
1526 fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1527#endif
1528}
1529
1530/*
1531 * Write all modified entries in an mlist to the history file.
1532 */
1533 static void
1484{
1485 if (ml == &mlist_search)
1486 fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1487#if SHELL_ESCAPE || PIPEC
1488 else if (ml == &mlist_shell)
1489 fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1490#endif
1491}
1492
1493/*
1494 * Write all modified entries in an mlist to the history file.
1495 */
1496 static void
1534write_mlist(ml, f)
1535 struct mlist *ml;
1536 FILE *f;
1497write_mlist(struct mlist *ml, FILE *f)
1537{
1538 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1539 {
1540 if (!ml->modified)
1541 continue;
1542 fprintf(f, "\"%s\n", ml->string);
1543 ml->modified = 0;
1544 }
1545 ml->modified = 0; /* entire mlist is now unmodified */
1546}
1547
1548/*
1549 * Make a temp name in the same directory as filename.
1550 */
1551 static char *
1498{
1499 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1500 {
1501 if (!ml->modified)
1502 continue;
1503 fprintf(f, "\"%s\n", ml->string);
1504 ml->modified = 0;
1505 }
1506 ml->modified = 0; /* entire mlist is now unmodified */
1507}
1508
1509/*
1510 * Make a temp name in the same directory as filename.
1511 */
1512 static char *
1552make_tempname(filename)
1553 char *filename;
1513make_tempname(char *filename)
1554{
1555 char lastch;
1556 char *tempname = ecalloc(1, strlen(filename)+1);
1557 strcpy(tempname, filename);
1558 lastch = tempname[strlen(tempname)-1];
1559 tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
1560 return tempname;
1561}

--- 46 unchanged lines hidden (view full) ---

1608 }
1609}
1610#endif /* CMD_HISTORY */
1611
1612/*
1613 * Make a file readable only by its owner.
1614 */
1615 static void
1514{
1515 char lastch;
1516 char *tempname = ecalloc(1, strlen(filename)+1);
1517 strcpy(tempname, filename);
1518 lastch = tempname[strlen(tempname)-1];
1519 tempname[strlen(tempname)-1] = (lastch == 'Q') ? 'Z' : 'Q';
1520 return tempname;
1521}

--- 46 unchanged lines hidden (view full) ---

1568 }
1569}
1570#endif /* CMD_HISTORY */
1571
1572/*
1573 * Make a file readable only by its owner.
1574 */
1575 static void
1616make_file_private(f)
1617 FILE *f;
1576make_file_private(FILE *f)
1618{
1619#if HAVE_FCHMOD
1620 int do_chmod = 1;
1621#if HAVE_STAT
1622 struct stat statbuf;
1623 int r = fstat(fileno(f), &statbuf);
1624 if (r < 0 || !S_ISREG(statbuf.st_mode))
1625 /* Don't chmod if not a regular file. */
1626 do_chmod = 0;
1627#endif
1628 if (do_chmod)
1629 fchmod(fileno(f), 0600);
1630#endif
1631}
1632
1633/*
1634 * Does the history file need to be updated?
1635 */
1636 static int
1577{
1578#if HAVE_FCHMOD
1579 int do_chmod = 1;
1580#if HAVE_STAT
1581 struct stat statbuf;
1582 int r = fstat(fileno(f), &statbuf);
1583 if (r < 0 || !S_ISREG(statbuf.st_mode))
1584 /* Don't chmod if not a regular file. */
1585 do_chmod = 0;
1586#endif
1587 if (do_chmod)
1588 fchmod(fileno(f), 0600);
1589#endif
1590}
1591
1592/*
1593 * Does the history file need to be updated?
1594 */
1595 static int
1637histfile_modified()
1596histfile_modified(void)
1638{
1639 if (mlist_search.modified)
1640 return 1;
1641#if SHELL_ESCAPE || PIPEC
1642 if (mlist_shell.modified)
1643 return 1;
1644#endif
1645 return 0;
1646}
1647
1648/*
1649 * Update the .lesshst file.
1650 */
1651 public void
1597{
1598 if (mlist_search.modified)
1599 return 1;
1600#if SHELL_ESCAPE || PIPEC
1601 if (mlist_shell.modified)
1602 return 1;
1603#endif
1604 return 0;
1605}
1606
1607/*
1608 * Update the .lesshst file.
1609 */
1610 public void
1652save_cmdhist()
1611save_cmdhist(void)
1653{
1654#if CMD_HISTORY
1655 char *histname;
1656 char *tempname;
1657 int skip_search;
1658 int skip_shell;
1659 struct save_ctx ctx;
1660 char *s;

--- 40 unchanged lines hidden ---
1612{
1613#if CMD_HISTORY
1614 char *histname;
1615 char *tempname;
1616 int skip_search;
1617 int skip_shell;
1618 struct save_ctx ctx;
1619 char *s;

--- 40 unchanged lines hidden ---