2 * Copyright (C) 1984-2011 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.
13 * Functions which manipulate the command buffer.
14 * Used only by command() and related functions.
27 static char cmdbuf[CMDBUF_SIZE]; /* Buffer for holding a multi-char command */
28 static int cmd_col; /* Current column of the cursor */
29 static int prompt_col; /* Column of cursor just after prompt */
30 static char *cp; /* Pointer into cmdbuf */
31 static int cmd_offset; /* Index into cmdbuf of first displayed char */
32 static int literal; /* Next input char should not be interpreted */
34 #if TAB_COMPLETE_FILENAME
35 static int cmd_complete();
37 * These variables are statics used by cmd_complete.
39 static int in_completion = 0;
41 static char *tk_original;
42 static char *tk_ipoint;
43 static char *tk_trial;
44 static struct textlist tk_tlist;
47 static int cmd_left();
48 static int cmd_right();
50 #if SPACES_IN_FILENAMES
51 public char openquote = '"';
52 public char closequote = '"';
58 #define HISTFILE_FIRST_LINE ".less-history-file:"
59 #define HISTFILE_SEARCH_SECTION ".search"
60 #define HISTFILE_SHELL_SECTION ".shell"
63 * A mlist structure represents a command history.
69 struct mlist *curr_mp;
75 * These are the various command histories that exist.
77 struct mlist mlist_search =
78 { &mlist_search, &mlist_search, &mlist_search, NULL, 0 };
79 public void * constant ml_search = (void *) &mlist_search;
81 struct mlist mlist_examine =
82 { &mlist_examine, &mlist_examine, &mlist_examine, NULL, 0 };
83 public void * constant ml_examine = (void *) &mlist_examine;
85 #if SHELL_ESCAPE || PIPEC
86 struct mlist mlist_shell =
87 { &mlist_shell, &mlist_shell, &mlist_shell, NULL, 0 };
88 public void * constant ml_shell = (void *) &mlist_shell;
91 #else /* CMD_HISTORY */
93 /* If CMD_HISTORY is off, these are just flags. */
94 public void * constant ml_search = (void *)1;
95 public void * constant ml_examine = (void *)2;
96 #if SHELL_ESCAPE || PIPEC
97 public void * constant ml_shell = (void *)3;
100 #endif /* CMD_HISTORY */
103 * History for the current command.
105 static struct mlist *curr_mlist = NULL;
106 static int curr_cmdflags;
108 static char cmd_mbc_buf[MAX_UTF_CHAR_LEN];
109 static int cmd_mbc_buf_len;
110 static int cmd_mbc_buf_index;
114 * Reset command buffer (to empty).
128 * Clear command line.
133 cmd_col = prompt_col = 0;
138 * Display a string, usually as a prompt for input into the command buffer.
146 char *endline = s + strlen(s);
150 ch = step_char(&ns, +1, endline);
157 } else if (!is_composing_char(ch) &&
158 !is_combining_char(prev_ch, ch))
160 int width = is_wide_char(ch) ? 2 : 1;
169 * How many characters are in the command buffer?
175 char *endline = s + strlen(s);
180 step_char(&s, +1, endline);
187 * Common part of cmd_step_right() and cmd_step_left().
190 cmd_step_common(p, ch, len, pwidth, bswidth)
201 pr = prchar((int) ch);
202 if (pwidth != NULL || bswidth != NULL)
204 int len = strlen(pr);
213 if (pwidth != NULL || bswidth != NULL)
215 if (is_composing_char(ch))
221 } else if (is_ubin_char(ch))
223 int len = strlen(pr);
230 LWCHAR prev_ch = step_char(&p, -1, cmdbuf);
231 if (is_combining_char(prev_ch, ch))
240 *pwidth = is_wide_char(ch)
254 * Step a pointer one character right in the command buffer.
257 cmd_step_right(pp, pwidth, bswidth)
263 LWCHAR ch = step_char(pp, +1, p + strlen(p));
265 return cmd_step_common(p, ch, *pp - p, pwidth, bswidth);
269 * Step a pointer one character left in the command buffer.
272 cmd_step_left(pp, pwidth, bswidth)
278 LWCHAR ch = step_char(pp, -1, cmdbuf);
280 return cmd_step_common(*pp, ch, p - *pp, pwidth, bswidth);
284 * Repaint the line from cp onwards.
285 * Then position the cursor just after the char old_cp (a pointer into cmdbuf).
292 * Repaint the line from the current position.
299 char *pr = cmd_step_right(&np, &width, NULL);
300 if (cmd_col + width >= sc_width)
310 char *pr = cmd_step_right(&np, &width, NULL);
318 * Back up the cursor to the correct position.
325 * Put the cursor at "home" (just after the prompt),
326 * and set cp to the corresponding char in cmdbuf.
331 while (cmd_col > prompt_col)
335 cmd_step_left(&cp, &width, &bswidth);
336 while (bswidth-- > 0)
341 cp = &cmdbuf[cmd_offset];
345 * Shift the cmdbuf display left a half-screen.
355 * Start at the first displayed char, count how far to the
356 * right we'd have to move to reach the center of the screen.
358 s = cmdbuf + cmd_offset;
360 while (cols < (sc_width - prompt_col) / 2 && *s != '\0')
363 cmd_step_right(&s, &width, NULL);
370 cmd_step_right(&ns, &width, NULL);
376 cmd_offset = s - cmdbuf;
379 cmd_repaint(save_cp);
383 * Shift the cmdbuf display right a half-screen.
393 * Start at the first displayed char, count how far to the
394 * left we'd have to move to traverse a half-screen width
395 * of displayed characters.
397 s = cmdbuf + cmd_offset;
399 while (cols < (sc_width - prompt_col) / 2 && s > cmdbuf)
402 cmd_step_left(&s, &width, NULL);
406 cmd_offset = s - cmdbuf;
409 cmd_repaint(save_cp);
413 * Move cursor right one character.
424 /* Already at the end of the line. */
428 pr = cmd_step_right(&ncp, &width, NULL);
429 if (cmd_col + width >= sc_width)
431 else if (cmd_col + width == sc_width - 1 && cp[1] != '\0')
438 pr = cmd_step_right(&ncp, &width, NULL);
448 * Move cursor left one character.
458 /* Already at the beginning of the line */
464 cmd_step_left(&ncp, &width, &bswidth);
468 if (cmd_col < prompt_col + width)
472 while (bswidth-- > 0)
478 * Insert a char into the command buffer, at the current position.
487 if (strlen(cmdbuf) + clen >= sizeof(cmdbuf)-1)
489 /* No room in the command buffer for another char. */
495 * Make room for the new character (shift the tail of the buffer right).
497 for (s = &cmdbuf[strlen(cmdbuf)]; s >= cp; s--)
500 * Insert the character into the buffer.
502 for (s = cp; s < cp + clen; s++)
505 * Reprint the tail of the line from the inserted char.
513 * Backspace in the command buffer.
514 * Delete the char to the left of the cursor.
525 * Backspace past beginning of the buffer:
526 * this usually means abort the command.
531 * Move cursor left (to the char being erased).
538 * Remove the char from the buffer (shift the buffer left).
548 * Repaint the buffer after the erased char.
553 * We say that erasing the entire command string causes us
554 * to abort the current command, if CF_QUIT_ON_ERASE is set.
556 if ((curr_cmdflags & CF_QUIT_ON_ERASE) && cp == cmdbuf && *cp == '\0')
562 * Delete the char under the cursor.
569 /* At end of string; there is no char under the cursor. */
573 * Move right, then use cmd_erase.
581 * Delete the "word" to the left of the cursor.
586 if (cp > cmdbuf && cp[-1] == ' ')
589 * If the char left of cursor is a space,
590 * erase all the spaces left of cursor (to the first non-space).
592 while (cp > cmdbuf && cp[-1] == ' ')
597 * If the char left of cursor is not a space,
598 * erase all the nonspaces left of cursor (the whole "word").
600 while (cp > cmdbuf && cp[-1] != ' ')
607 * Delete the "word" under the cursor.
615 * If the char under the cursor is a space,
616 * delete it and all the spaces right of cursor.
623 * If the char under the cursor is not a space,
624 * delete it and all nonspaces right of cursor (the whole word).
626 while (*cp != ' ' && *cp != '\0')
633 * Delete all chars in the command buffer.
638 if (cmdbuf[0] == '\0')
640 /* Buffer is already empty; abort the current command. */
649 * We say that erasing the entire command string causes us
650 * to abort the current command, if CF_QUIT_ON_ERASE is set.
652 if (curr_cmdflags & CF_QUIT_ON_ERASE)
658 * Select an mlist structure to be the current command history.
661 set_mlist(mlist, cmdflags)
666 curr_mlist = (struct mlist *) mlist;
667 curr_cmdflags = cmdflags;
669 /* Make sure the next up-arrow moves to the last string in the mlist. */
670 if (curr_mlist != NULL)
671 curr_mlist->curr_mp = curr_mlist;
677 * Move up or down in the currently selected command history list.
685 if (curr_mlist == NULL)
688 * The current command has no history list.
696 * Move curr_mp to the next/prev entry.
699 curr_mlist->curr_mp = curr_mlist->curr_mp->prev;
701 curr_mlist->curr_mp = curr_mlist->curr_mp->next;
703 * Copy the entry into cmdbuf and echo it on the screen.
705 s = curr_mlist->curr_mp->string;
709 for (cp = cmdbuf; *cp != '\0'; )
716 * Add a string to a history list.
719 cmd_addhist(mlist, cmd)
727 * Don't save a trivial command.
729 if (strlen(cmd) == 0)
733 * Save the command unless it's a duplicate of the
734 * last command in the history.
737 if (ml == mlist || strcmp(ml->string, cmd) != 0)
740 * Did not find command in history.
741 * Save the command and put it at the end of the history list.
743 ml = (struct mlist *) ecalloc(1, sizeof(struct mlist));
744 ml->string = save(cmd);
746 ml->prev = mlist->prev;
747 mlist->prev->next = ml;
751 * Point to the cmd just after the just-accepted command.
752 * Thus, an UPARROW will always retrieve the previous command.
754 mlist->curr_mp = ml->next;
759 * Accept the command in the command buffer.
760 * Add it to the currently selected history list.
767 * Nothing to do if there is no currently selected history list.
769 if (curr_mlist == NULL)
771 cmd_addhist(curr_mlist, cmdbuf);
772 curr_mlist->modified = 1;
777 * Try to perform a line-edit function on the command buffer,
778 * using a specified char as a line-editing command.
780 * CC_PASS The char does not invoke a line edit function.
781 * CC_OK Line edit function done.
782 * CC_QUIT The char requests the current command to be aborted.
791 #if TAB_COMPLETE_FILENAME
792 #define not_in_completion() in_completion = 0
794 #define not_in_completion()
798 * See if the char is indeed a line-editing command.
802 if (curr_mlist == NULL)
804 * No current history; don't accept history manipulation cmds.
806 flags |= EC_NOHISTORY;
808 #if TAB_COMPLETE_FILENAME
809 if (curr_mlist == ml_search)
811 * In a search command; don't accept file-completion cmds.
813 flags |= EC_NOCOMPLETE;
816 action = editchar(c, flags);
822 return (cmd_right());
828 while (*cp != '\0' && *cp != ' ')
835 while (cp > cmdbuf && cp[-1] == ' ')
837 while (cp > cmdbuf && cp[-1] != ' ')
856 return (cmd_erase());
866 return (cmd_werase());
869 return (cmd_delete());
872 return (cmd_wdelete());
880 return (cmd_updown(action));
882 #if TAB_COMPLETE_FILENAME
886 return (cmd_complete(action));
896 #if TAB_COMPLETE_FILENAME
898 * Insert a string into the command buffer, at the current position.
906 char *endline = str + strlen(str);
908 for (s = str; *s != '\0'; )
911 step_char(&s, +1, endline);
912 action = cmd_ichar(os, s - os);
923 * Find the beginning and end of the "current" word.
924 * This is the word which the cursor (cp) is inside or at the end of.
925 * Return pointer to the beginning of the word and put the
926 * cursor at the end of the word.
932 #if SPACES_IN_FILENAMES
934 int delim_quoted = 0;
936 char *esc = get_meta_escape();
937 int esclen = strlen(esc);
941 * Move cursor to end of word.
943 if (*cp != ' ' && *cp != '\0')
946 * Cursor is on a nonspace.
947 * Move cursor right to the next space.
949 while (*cp != ' ' && *cp != '\0')
951 } else if (cp > cmdbuf && cp[-1] != ' ')
954 * Cursor is on a space, and char to the left is a nonspace.
955 * We're already at the end of the word.
962 * Cursor is on a space and char to the left is a space.
963 * Huh? There's no word here.
969 * Find the beginning of the word which the cursor is in.
973 #if SPACES_IN_FILENAMES
975 * If we have an unbalanced quote (that is, an open quote
976 * without a corresponding close quote), we return everything
977 * from the open quote, including spaces.
979 for (word = cmdbuf; word < cp; word++)
984 for (p = cmdbuf; p < cp; p++)
989 } else if (esclen > 0 && p + esclen < cp &&
990 strncmp(p, esc, esclen) == 0)
994 } else if (delim_quoted)
996 if (*p == closequote)
998 } else /* (!delim_quoted) */
1000 if (*p == openquote)
1011 * Set things up to enter completion mode.
1012 * Expand the word under the cursor into a list of filenames
1013 * which start with that word, and set tk_text to that list.
1022 * Get rid of any previous tk_text.
1024 if (tk_text != NULL)
1030 * Find the original (uncompleted) word in the command buffer.
1032 word = delimit_word();
1036 * Set the insertion point to the point in the command buffer
1037 * where the original (uncompleted) word now sits.
1041 * Save the original (uncompleted) word
1043 if (tk_original != NULL)
1045 tk_original = (char *) ecalloc(cp-word+1, sizeof(char));
1046 strncpy(tk_original, word, cp-word);
1048 * Get the expanded filename.
1049 * This may result in a single filename, or
1050 * a blank-separated list of filenames.
1054 if (*word != openquote)
1056 tk_text = fcomplete(word);
1059 char *qword = shell_quote(word+1);
1061 tk_text = fcomplete(word+1);
1064 tk_text = fcomplete(qword);
1072 * Return the next word in the current completion list.
1075 next_compl(action, prev)
1082 return (forw_textlist(&tk_tlist, prev));
1084 return (back_textlist(&tk_tlist, prev));
1091 * Complete the filename before (or under) the cursor.
1092 * cmd_complete may be called multiple times. The global in_completion
1093 * remembers whether this call is the first time (create the list),
1094 * or a subsequent time (step thru the list).
1097 cmd_complete(action)
1102 if (!in_completion || action == EC_EXPAND)
1105 * Expand the word under the cursor and
1106 * use the first word in the expansion
1107 * (or the entire expansion if we're doing EC_EXPAND).
1110 if (tk_text == NULL)
1115 if (action == EC_EXPAND)
1118 * Use the whole list.
1124 * Use the first filename in the list.
1127 init_textlist(&tk_tlist, tk_text);
1128 tk_trial = next_compl(action, (char*)NULL);
1133 * We already have a completion list.
1134 * Use the next/previous filename from the list.
1136 tk_trial = next_compl(action, tk_trial);
1140 * Remove the original word, or the previous trial completion.
1142 while (cp > tk_ipoint)
1145 if (tk_trial == NULL)
1148 * There are no more trial completions.
1149 * Insert the original (uncompleted) filename.
1152 if (cmd_istr(tk_original) != CC_OK)
1157 * Insert trial completion.
1159 if (cmd_istr(tk_trial) != CC_OK)
1162 * If it is a directory, append a slash.
1164 if (is_dir(tk_trial))
1166 if (cp > cmdbuf && cp[-1] == closequote)
1168 s = lgetenv("LESSSEPARATOR");
1171 if (cmd_istr(s) != CC_OK)
1184 #endif /* TAB_COMPLETE_FILENAME */
1187 * Process a single character of a multi-character command, such as
1188 * a number, or the pattern of a search command.
1190 * CC_OK The char was accepted.
1191 * CC_QUIT The char requests the command to be aborted.
1192 * CC_ERROR The char could not be accepted due to an error.
1207 /* Perform strict validation in all possible cases. */
1208 if (cmd_mbc_buf_len == 0)
1211 cmd_mbc_buf_index = 1;
1213 if (IS_ASCII_OCTET(c))
1214 cmd_mbc_buf_len = 1;
1215 else if (IS_UTF8_LEAD(c))
1217 cmd_mbc_buf_len = utf_len(c);
1221 /* UTF8_INVALID or stray UTF8_TRAIL */
1225 } else if (IS_UTF8_TRAIL(c))
1227 cmd_mbc_buf[cmd_mbc_buf_index++] = c;
1228 if (cmd_mbc_buf_index < cmd_mbc_buf_len)
1230 if (!is_utf8_well_formed(cmd_mbc_buf))
1232 /* complete, but not well formed (non-shortest form), sequence */
1233 cmd_mbc_buf_len = 0;
1239 /* Flush incomplete (truncated) sequence. */
1240 cmd_mbc_buf_len = 0;
1242 /* Handle new char. */
1246 len = cmd_mbc_buf_len;
1247 cmd_mbc_buf_len = 0;
1253 * Insert the char, even if it is a line-editing char.
1256 return (cmd_ichar(cmd_mbc_buf, len));
1260 * See if it is a line-editing character.
1262 if (in_mca() && len == 1)
1264 action = cmd_edit(c);
1276 * Insert the char into the command buffer.
1278 return (cmd_ichar(cmd_mbc_buf, len));
1282 * Return the number currently in the command buffer.
1292 for (p = cmdbuf; *p >= '0' && *p <= '9'; p++)
1293 n = (n * 10) + (*p - '0');
1297 *frac = getfraction(&p, NULL, &err);
1298 /* {{ do something if err is set? }} */
1304 * Return a pointer to the command buffer.
1314 * Return the last (most recent) string in the current command history.
1319 if (curr_mlist == NULL)
1321 return (curr_mlist->curr_mp->prev->string);
1327 * Get the name of the history file.
1336 /* See if filename is explicitly specified by $LESSHISTFILE. */
1337 name = lgetenv("LESSHISTFILE");
1338 if (name != NULL && *name != '\0')
1340 if (strcmp(name, "-") == 0 || strcmp(name, "/dev/null") == 0)
1341 /* $LESSHISTFILE == "-" means don't use a history file. */
1343 return (save(name));
1346 /* Otherwise, file is in $HOME. */
1347 home = lgetenv("HOME");
1348 if (home == NULL || *home == '\0')
1351 home = lgetenv("INIT");
1352 if (home == NULL || *home == '\0')
1356 len = strlen(home) + strlen(LESSHISTFILE) + 2;
1357 name = (char *) ecalloc(len, sizeof(char));
1358 SNPRINTF2(name, len, "%s/%s", home, LESSHISTFILE);
1361 #endif /* CMD_HISTORY */
1364 * Initialize history from a .lesshist file.
1370 struct mlist *ml = NULL;
1371 char line[CMDBUF_SIZE];
1376 filename = histfile_name();
1377 if (filename == NULL)
1379 f = fopen(filename, "r");
1383 if (fgets(line, sizeof(line), f) == NULL ||
1384 strncmp(line, HISTFILE_FIRST_LINE, strlen(HISTFILE_FIRST_LINE)) != 0)
1389 while (fgets(line, sizeof(line), f) != NULL)
1391 for (p = line; *p != '\0'; p++)
1393 if (*p == '\n' || *p == '\r')
1399 if (strcmp(line, HISTFILE_SEARCH_SECTION) == 0)
1401 else if (strcmp(line, HISTFILE_SHELL_SECTION) == 0)
1403 #if SHELL_ESCAPE || PIPEC
1408 } else if (*line == '"')
1411 cmd_addhist(ml, line+1);
1415 #endif /* CMD_HISTORY */
1431 s = lgetenv("LESSHISTSIZE");
1438 for (n = 0; n < histsize; n++)
1440 if (ml->string == NULL)
1444 for (ml = ml->next; ml->string != NULL; ml = ml->next)
1445 fprintf(f, "\"%s\n", ml->string);
1447 #endif /* CMD_HISTORY */
1460 filename = histfile_name();
1461 if (filename == NULL)
1463 if (mlist_search.modified)
1465 #if SHELL_ESCAPE || PIPEC
1466 if (mlist_shell.modified)
1471 f = fopen(filename, "w");
1477 /* Make history file readable only by owner. */
1480 struct stat statbuf;
1481 int r = fstat(fileno(f), &statbuf);
1482 if (r < 0 || !S_ISREG(statbuf.st_mode))
1483 /* Don't chmod if not a regular file. */
1487 fchmod(fileno(f), 0600);
1491 fprintf(f, "%s\n", HISTFILE_FIRST_LINE);
1493 fprintf(f, "%s\n", HISTFILE_SEARCH_SECTION);
1494 save_mlist(&mlist_search, f);
1496 #if SHELL_ESCAPE || PIPEC
1497 fprintf(f, "%s\n", HISTFILE_SHELL_SECTION);
1498 save_mlist(&mlist_shell, f);
1502 #endif /* CMD_HISTORY */