("" < 3.4) always evaluates to true, which unconditionally
[dragonfly.git] / contrib / less-381 / cmdbuf.c
blobdbf5f2afccca0c09975bbfb53e4ac61b54669a74
1 /*
2 * Copyright (C) 1984-2002 Mark Nudelman
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.
7 * For more information about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Functions which manipulate the command buffer.
14 * Used only by command() and related functions.
17 #include "less.h"
18 #include "cmd.h"
20 extern int sc_width;
22 static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
23 static int cmd_col; /* Current column of the cursor */
24 static int prompt_col; /* Column of cursor just after prompt */
25 static char *cp; /* Pointer into cmdbuf */
26 static int cmd_offset; /* Index into cmdbuf of first displayed char */
27 static int literal; /* Next input char should not be interpreted */
29 #if TAB_COMPLETE_FILENAME
30 static int cmd_complete();
32 * These variables are statics used by cmd_complete.
34 static int in_completion = 0;
35 static char *tk_text;
36 static char *tk_original;
37 static char *tk_ipoint;
38 static char *tk_trial;
39 static struct textlist tk_tlist;
40 #endif
42 static int cmd_left();
43 static int cmd_right();
45 #if SPACES_IN_FILENAMES
46 public char openquote = '"';
47 public char closequote = '"';
48 #endif
50 #if CMD_HISTORY
52 * A mlist structure represents a command history.
54 struct mlist
56 struct mlist *next;
57 struct mlist *prev;
58 struct mlist *curr_mp;
59 char *string;
63 * These are the various command histories that exist.
65 struct mlist mlist_search =
66 { &mlist_search, &mlist_search, &mlist_search, NULL };
67 public void * constant ml_search = (void *) &mlist_search;
69 struct mlist mlist_examine =
70 { &mlist_examine, &mlist_examine, &mlist_examine, NULL };
71 public void * constant ml_examine = (void *) &mlist_examine;
73 #if SHELL_ESCAPE || PIPEC
74 struct mlist mlist_shell =
75 { &mlist_shell, &mlist_shell, &mlist_shell, NULL };
76 public void * constant ml_shell = (void *) &mlist_shell;
77 #endif
79 #else /* CMD_HISTORY */
81 /* If CMD_HISTORY is off, these are just flags. */
82 public void * constant ml_search = (void *)1;
83 public void * constant ml_examine = (void *)2;
84 #if SHELL_ESCAPE || PIPEC
85 public void * constant ml_shell = (void *)3;
86 #endif
88 #endif /* CMD_HISTORY */
91 * History for the current command.
93 static struct mlist *curr_mlist = NULL;
94 static int curr_cmdflags;
98 * Reset command buffer (to empty).
100 public void
101 cmd_reset()
103 cp = cmdbuf;
104 *cp = '\0';
105 cmd_col = 0;
106 cmd_offset = 0;
107 literal = 0;
111 * Clear command line on display.
113 public void
114 clear_cmd()
116 clear_bot();
117 cmd_col = prompt_col = 0;
121 * Display a string, usually as a prompt for input into the command buffer.
123 public void
124 cmd_putstr(s)
125 char *s;
127 putstr(s);
128 cmd_col += strlen(s);
129 prompt_col += strlen(s);
133 * How many characters are in the command buffer?
135 public int
136 len_cmdbuf()
138 return (strlen(cmdbuf));
142 * Repaint the line from cp onwards.
143 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
145 static void
146 cmd_repaint(old_cp)
147 char *old_cp;
149 char *p;
152 * Repaint the line from the current position.
154 clear_eol();
155 for ( ; *cp != '\0'; cp++)
157 p = prchar(*cp);
158 if (cmd_col + (int)strlen(p) >= sc_width)
159 break;
160 putstr(p);
161 cmd_col += strlen(p);
165 * Back up the cursor to the correct position.
167 while (cp > old_cp)
168 cmd_left();
172 * Put the cursor at "home" (just after the prompt),
173 * and set cp to the corresponding char in cmdbuf.
175 static void
176 cmd_home()
178 while (cmd_col > prompt_col)
180 putbs();
181 cmd_col--;
184 cp = &cmdbuf[cmd_offset];
188 * Shift the cmdbuf display left a half-screen.
190 static void
191 cmd_lshift()
193 char *s;
194 char *save_cp;
195 int cols;
198 * Start at the first displayed char, count how far to the
199 * right we'd have to move to reach the center of the screen.
201 s = cmdbuf + cmd_offset;
202 cols = 0;
203 while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
204 cols += strlen(prchar(*s++));
206 cmd_offset = s - cmdbuf;
207 save_cp = cp;
208 cmd_home();
209 cmd_repaint(save_cp);
213 * Shift the cmdbuf display right a half-screen.
215 static void
216 cmd_rshift()
218 char *s;
219 char *p;
220 char *save_cp;
221 int cols;
224 * Start at the first displayed char, count how far to the
225 * left we'd have to move to traverse a half-screen width
226 * of displayed characters.
228 s = cmdbuf + cmd_offset;
229 cols = 0;
230 while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
232 p = prchar(*--s);
233 cols += strlen(p);
236 cmd_offset = s - cmdbuf;
237 save_cp = cp;
238 cmd_home();
239 cmd_repaint(save_cp);
243 * Move cursor right one character.
245 static int
246 cmd_right()
248 char *p;
250 if (*cp == '\0')
253 * Already at the end of the line.
255 return (CC_OK);
257 p = prchar(*cp);
258 if (cmd_col + (int)strlen(p) >= sc_width)
259 cmd_lshift();
260 else if (cmd_col + (int)strlen(p) == sc_width - 1 && cp[1] != '\0')
261 cmd_lshift();
262 cp++;
263 putstr(p);
264 cmd_col += strlen(p);
265 return (CC_OK);
269 * Move cursor left one character.
271 static int
272 cmd_left()
274 char *p;
276 if (cp <= cmdbuf)
278 /* Already at the beginning of the line */
279 return (CC_OK);
281 p = prchar(cp[-1]);
282 if (cmd_col < prompt_col + (int)strlen(p))
283 cmd_rshift();
284 cp--;
285 cmd_col -= strlen(p);
286 while (*p++ != '\0')
287 putbs();
288 return (CC_OK);
292 * Insert a char into the command buffer, at the current position.
294 static int
295 cmd_ichar(c)
296 int c;
298 char *s;
300 if (strlen(cmdbuf) >= sizeof(cmdbuf)-2)
303 * No room in the command buffer for another char.
305 bell();
306 return (CC_ERROR);
310 * Insert the character into the buffer.
312 for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
313 s[1] = s[0];
314 *cp = c;
316 * Reprint the tail of the line from the inserted char.
318 cmd_repaint(cp);
319 cmd_right();
320 return (CC_OK);
324 * Backspace in the command buffer.
325 * Delete the char to the left of the cursor.
327 static int
328 cmd_erase()
330 register char *s;
332 if (cp == cmdbuf)
335 * Backspace past beginning of the buffer:
336 * this usually means abort the command.
338 return (CC_QUIT);
341 * Move cursor left (to the char being erased).
343 cmd_left();
345 * Remove the char from the buffer (shift the buffer left).
347 for (s = cp; *s != '\0'; s++)
348 s[0] = s[1];
350 * Repaint the buffer after the erased char.
352 cmd_repaint(cp);
355 * We say that erasing the entire command string causes us
356 * to abort the current command, if CF_QUIT_ON_ERASE is set.
358 if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
359 return (CC_QUIT);
360 return (CC_OK);
364 * Delete the char under the cursor.
366 static int
367 cmd_delete()
369 if (*cp == '\0')
372 * At end of string; there is no char under the cursor.
374 return (CC_OK);
377 * Move right, then use cmd_erase.
379 cmd_right();
380 cmd_erase();
381 return (CC_OK);
385 * Delete the "word" to the left of the cursor.
387 static int
388 cmd_werase()
390 if (cp > cmdbuf && cp[-1] == ' ')
393 * If the char left of cursor is a space,
394 * erase all the spaces left of cursor (to the first non-space).
396 while (cp > cmdbuf && cp[-1] == ' ')
397 (void) cmd_erase();
398 } else
401 * If the char left of cursor is not a space,
402 * erase all the nonspaces left of cursor (the whole "word").
404 while (cp > cmdbuf && cp[-1] != ' ')
405 (void) cmd_erase();
407 return (CC_OK);
411 * Delete the "word" under the cursor.
413 static int
414 cmd_wdelete()
416 if (*cp == ' ')
419 * If the char under the cursor is a space,
420 * delete it and all the spaces right of cursor.
422 while (*cp == ' ')
423 (void) cmd_delete();
424 } else
427 * If the char under the cursor is not a space,
428 * delete it and all nonspaces right of cursor (the whole word).
430 while (*cp != ' ' && *cp != '\0')
431 (void) cmd_delete();
433 return (CC_OK);
437 * Delete all chars in the command buffer.
439 static int
440 cmd_kill()
442 if (cmdbuf[0] == '\0')
445 * Buffer is already empty; abort the current command.
447 return (CC_QUIT);
449 cmd_offset = 0;
450 cmd_home();
451 *cp = '\0';
452 cmd_repaint(cp);
455 * We say that erasing the entire command string causes us
456 * to abort the current command, if CF_QUIT_ON_ERASE is set.
458 if (curr_cmdflags & CF_QUIT_ON_ERASE)
459 return (CC_QUIT);
460 return (CC_OK);
464 * Select an mlist structure to be the current command history.
466 public void
467 set_mlist(mlist, cmdflags)
468 void *mlist;
469 int cmdflags;
471 curr_mlist = (struct mlist *) mlist;
472 curr_cmdflags = cmdflags;
475 #if CMD_HISTORY
477 * Move up or down in the currently selected command history list.
479 static int
480 cmd_updown(action)
481 int action;
483 char *s;
485 if (curr_mlist == NULL)
488 * The current command has no history list.
490 bell();
491 return (CC_OK);
493 cmd_home();
494 clear_eol();
496 * Move curr_mp to the next/prev entry.
498 if (action == EC_UP)
499 curr_mlist->curr_mp = curr_mlist->curr_mp->prev;
500 else
501 curr_mlist->curr_mp = curr_mlist->curr_mp->next;
503 * Copy the entry into cmdbuf and echo it on the screen.
505 s = curr_mlist->curr_mp->string;
506 if (s == NULL)
507 s = "";
508 for (cp = cmdbuf; *s != '\0'; s++)
510 *cp = *s;
511 cmd_right();
513 *cp = '\0';
514 return (CC_OK);
516 #endif
519 * Add a string to a history list.
521 public void
522 cmd_addhist(mlist, cmd)
523 struct mlist *mlist;
524 char *cmd;
526 #if CMD_HISTORY
527 struct mlist *ml;
530 * Don't save a trivial command.
532 if (strlen(cmd) == 0)
533 return;
535 * Don't save if a duplicate of a command which is already
536 * in the history.
537 * But select the one already in the history to be current.
539 for (ml = mlist->next; ml != mlist; ml = ml->next)
541 if (strcmp(ml->string, cmd) == 0)
542 break;
544 if (ml == mlist)
547 * Did not find command in history.
548 * Save the command and put it at the end of the history list.
550 ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
551 ml->string = save(cmd);
552 ml->next = mlist;
553 ml->prev = mlist->prev;
554 mlist->prev->next = ml;
555 mlist->prev = ml;
558 * Point to the cmd just after the just-accepted command.
559 * Thus, an UPARROW will always retrieve the previous command.
561 mlist->curr_mp = ml->next;
562 #endif
566 * Accept the command in the command buffer.
567 * Add it to the currently selected history list.
569 public void
570 cmd_accept()
572 #if CMD_HISTORY
574 * Nothing to do if there is no currently selected history list.
576 if (curr_mlist == NULL)
577 return;
578 cmd_addhist(curr_mlist, cmdbuf);
579 #endif
583 * Try to perform a line-edit function on the command buffer,
584 * using a specified char as a line-editing command.
585 * Returns:
586 * CC_PASS The char does not invoke a line edit function.
587 * CC_OK Line edit function done.
588 * CC_QUIT The char requests the current command to be aborted.
590 static int
591 cmd_edit(c)
592 int c;
594 int action;
595 int flags;
597 #if TAB_COMPLETE_FILENAME
598 #define not_in_completion() in_completion = 0
599 #else
600 #define not_in_completion()
601 #endif
604 * See if the char is indeed a line-editing command.
606 flags = 0;
607 #if CMD_HISTORY
608 if (curr_mlist == NULL)
610 * No current history; don't accept history manipulation cmds.
612 flags |= EC_NOHISTORY;
613 #endif
614 #if TAB_COMPLETE_FILENAME
615 if (curr_mlist == ml_search)
617 * In a search command; don't accept file-completion cmds.
619 flags |= EC_NOCOMPLETE;
620 #endif
622 action = editchar(c, flags);
624 switch (action)
626 case EC_RIGHT:
627 not_in_completion();
628 return (cmd_right());
629 case EC_LEFT:
630 not_in_completion();
631 return (cmd_left());
632 case EC_W_RIGHT:
633 not_in_completion();
634 while (*cp != '\0' && *cp != ' ')
635 cmd_right();
636 while (*cp == ' ')
637 cmd_right();
638 return (CC_OK);
639 case EC_W_LEFT:
640 not_in_completion();
641 while (cp > cmdbuf && cp[-1] == ' ')
642 cmd_left();
643 while (cp > cmdbuf && cp[-1] != ' ')
644 cmd_left();
645 return (CC_OK);
646 case EC_HOME:
647 not_in_completion();
648 cmd_offset = 0;
649 cmd_home();
650 cmd_repaint(cp);
651 return (CC_OK);
652 case EC_END:
653 not_in_completion();
654 while (*cp != '\0')
655 cmd_right();
656 return (CC_OK);
657 case EC_INSERT:
658 not_in_completion();
659 return (CC_OK);
660 case EC_BACKSPACE:
661 not_in_completion();
662 return (cmd_erase());
663 case EC_LINEKILL:
664 not_in_completion();
665 return (cmd_kill());
666 case EC_W_BACKSPACE:
667 not_in_completion();
668 return (cmd_werase());
669 case EC_DELETE:
670 not_in_completion();
671 return (cmd_delete());
672 case EC_W_DELETE:
673 not_in_completion();
674 return (cmd_wdelete());
675 case EC_LITERAL:
676 literal = 1;
677 return (CC_OK);
678 #if CMD_HISTORY
679 case EC_UP:
680 case EC_DOWN:
681 not_in_completion();
682 return (cmd_updown(action));
683 #endif
684 #if TAB_COMPLETE_FILENAME
685 case EC_F_COMPLETE:
686 case EC_B_COMPLETE:
687 case EC_EXPAND:
688 return (cmd_complete(action));
689 #endif
690 case EC_NOACTION:
691 return (CC_OK);
692 default:
693 not_in_completion();
694 return (CC_PASS);
698 #if TAB_COMPLETE_FILENAME
700 * Insert a string into the command buffer, at the current position.
702 static int
703 cmd_istr(str)
704 char *str;
706 char *s;
707 int action;
709 for (s = str; *s != '\0'; s++)
711 action = cmd_ichar(*s);
712 if (action != CC_OK)
714 bell();
715 return (action);
718 return (CC_OK);
722 * Find the beginning and end of the "current" word.
723 * This is the word which the cursor (cp) is inside or at the end of.
724 * Return pointer to the beginning of the word and put the
725 * cursor at the end of the word.
727 static char *
728 delimit_word()
730 char *word;
731 #if SPACES_IN_FILENAMES
732 char *p;
733 int delim_quoted = 0;
734 int meta_quoted = 0;
735 char *esc = get_meta_escape();
736 int esclen = strlen(esc);
737 #endif
740 * Move cursor to end of word.
742 if (*cp != ' ' && *cp != '\0')
745 * Cursor is on a nonspace.
746 * Move cursor right to the next space.
748 while (*cp != ' ' && *cp != '\0')
749 cmd_right();
750 } else if (cp > cmdbuf && cp[-1] != ' ')
753 * Cursor is on a space, and char to the left is a nonspace.
754 * We're already at the end of the word.
757 #if 0
758 } else
761 * Cursor is on a space and char to the left is a space.
762 * Huh? There's no word here.
764 return (NULL);
765 #endif
768 * Find the beginning of the word which the cursor is in.
770 if (cp == cmdbuf)
771 return (NULL);
772 #if SPACES_IN_FILENAMES
774 * If we have an unbalanced quote (that is, an open quote
775 * without a corresponding close quote), we return everything
776 * from the open quote, including spaces.
778 for (word = cmdbuf; word < cp; word++)
779 if (*word != ' ')
780 break;
781 if (word >= cp)
782 return (cp);
783 for (p = cmdbuf; p < cp; p++)
785 if (meta_quoted)
787 meta_quoted = 0;
788 } else if (esclen > 0 && p + esclen < cp &&
789 strncmp(p, esc, esclen) == 0)
791 meta_quoted = 1;
792 p += esclen - 1;
793 } else if (delim_quoted)
795 if (*p == closequote)
796 delim_quoted = 0;
797 } else /* (!delim_quoted) */
799 if (*p == openquote)
800 delim_quoted = 1;
801 else if (*p == ' ')
802 word = p+1;
805 #endif
806 return (word);
810 * Set things up to enter completion mode.
811 * Expand the word under the cursor into a list of filenames
812 * which start with that word, and set tk_text to that list.
814 static void
815 init_compl()
817 char *word;
818 char c;
821 * Get rid of any previous tk_text.
823 if (tk_text != NULL)
825 free(tk_text);
826 tk_text = NULL;
829 * Find the original (uncompleted) word in the command buffer.
831 word = delimit_word();
832 if (word == NULL)
833 return;
835 * Set the insertion point to the point in the command buffer
836 * where the original (uncompleted) word now sits.
838 tk_ipoint = word;
840 * Save the original (uncompleted) word
842 if (tk_original != NULL)
843 free(tk_original);
844 tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
845 strncpy(tk_original, word, cp-word);
847 * Get the expanded filename.
848 * This may result in a single filename, or
849 * a blank-separated list of filenames.
851 c = *cp;
852 *cp = '\0';
853 if (*word != openquote)
855 tk_text = fcomplete(word);
856 } else
858 char *qword = shell_quote(word+1);
859 if (qword == NULL)
860 tk_text = fcomplete(word+1);
861 else
863 tk_text = fcomplete(qword);
864 free(qword);
867 *cp = c;
871 * Return the next word in the current completion list.
873 static char *
874 next_compl(action, prev)
875 int action;
876 char *prev;
878 switch (action)
880 case EC_F_COMPLETE:
881 return (forw_textlist(&tk_tlist, prev));
882 case EC_B_COMPLETE:
883 return (back_textlist(&tk_tlist, prev));
885 /* Cannot happen */
886 return ("?");
890 * Complete the filename before (or under) the cursor.
891 * cmd_complete may be called multiple times. The global in_completion
892 * remembers whether this call is the first time (create the list),
893 * or a subsequent time (step thru the list).
895 static int
896 cmd_complete(action)
897 int action;
899 char *s;
901 if (!in_completion || action == EC_EXPAND)
904 * Expand the word under the cursor and
905 * use the first word in the expansion
906 * (or the entire expansion if we're doing EC_EXPAND).
908 init_compl();
909 if (tk_text == NULL)
911 bell();
912 return (CC_OK);
914 if (action == EC_EXPAND)
917 * Use the whole list.
919 tk_trial = tk_text;
920 } else
923 * Use the first filename in the list.
925 in_completion = 1;
926 init_textlist(&tk_tlist, tk_text);
927 tk_trial = next_compl(action, (char*)NULL);
929 } else
932 * We already have a completion list.
933 * Use the next/previous filename from the list.
935 tk_trial = next_compl(action, tk_trial);
939 * Remove the original word, or the previous trial completion.
941 while (cp > tk_ipoint)
942 (void) cmd_erase();
944 if (tk_trial == NULL)
947 * There are no more trial completions.
948 * Insert the original (uncompleted) filename.
950 in_completion = 0;
951 if (cmd_istr(tk_original) != CC_OK)
952 goto fail;
953 } else
956 * Insert trial completion.
958 if (cmd_istr(tk_trial) != CC_OK)
959 goto fail;
961 * If it is a directory, append a slash.
963 if (is_dir(tk_trial))
965 if (cp > cmdbuf && cp[-1] == closequote)
966 (void) cmd_erase();
967 s = lgetenv("LESSSEPARATOR");
968 if (s == NULL)
969 s = PATHNAME_SEP;
970 if (cmd_istr(s) != CC_OK)
971 goto fail;
975 return (CC_OK);
977 fail:
978 in_completion = 0;
979 bell();
980 return (CC_OK);
983 #endif /* TAB_COMPLETE_FILENAME */
986 * Process a single character of a multi-character command, such as
987 * a number, or the pattern of a search command.
988 * Returns:
989 * CC_OK The char was accepted.
990 * CC_QUIT The char requests the command to be aborted.
991 * CC_ERROR The char could not be accepted due to an error.
993 public int
994 cmd_char(c)
995 int c;
997 int action;
999 if (literal)
1002 * Insert the char, even if it is a line-editing char.
1004 literal = 0;
1005 return (cmd_ichar(c));
1009 * See if it is a special line-editing character.
1011 if (in_mca())
1013 action = cmd_edit(c);
1014 switch (action)
1016 case CC_OK:
1017 case CC_QUIT:
1018 return (action);
1019 case CC_PASS:
1020 break;
1025 * Insert the char into the command buffer.
1027 return (cmd_ichar(c));
1031 * Return the number currently in the command buffer.
1033 public LINENUM
1034 cmd_int()
1036 register char *p;
1037 LINENUM n = 0;
1039 for (p = cmdbuf; *p != '\0'; p++)
1040 n = (10 * n) + (*p - '0');
1041 return (n);
1045 * Return a pointer to the command buffer.
1047 public char *
1048 get_cmdbuf()
1050 return (cmdbuf);