blob: 1b04e62237ac516e1fe2a6e1d0d5d21a2ee2197e [file] [log] [blame]
Bram Moolenaard7663c22019-08-06 21:59:57 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * cmdhist.c: Functions for the history of the command-line.
12 */
13
14#include "vim.h"
15
16static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
17static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; // lastused entry
18static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
19 // identifying (unique) number of newest history entry
20static int hislen = 0; // actual length of history tables
21
22/*
23 * Return the length of the history tables
24 */
25 int
26get_hislen(void)
27{
28 return hislen;
29}
30
31/*
32 * Return a pointer to a specified history table
33 */
34 histentry_T *
35get_histentry(int hist_type)
36{
37 return history[hist_type];
38}
39
40 void
41set_histentry(int hist_type, histentry_T *entry)
42{
43 history[hist_type] = entry;
44}
45
46 int *
47get_hisidx(int hist_type)
48{
49 return &hisidx[hist_type];
50}
51
52 int *
53get_hisnum(int hist_type)
54{
55 return &hisnum[hist_type];
56}
57
58/*
59 * Translate a history character to the associated type number.
60 */
61 int
62hist_char2type(int c)
63{
64 if (c == ':')
65 return HIST_CMD;
66 if (c == '=')
67 return HIST_EXPR;
68 if (c == '@')
69 return HIST_INPUT;
70 if (c == '>')
71 return HIST_DEBUG;
72 return HIST_SEARCH; // must be '?' or '/'
73}
74
75/*
76 * Table of history names.
77 * These names are used in :history and various hist...() functions.
78 * It is sufficient to give the significant prefix of a history name.
79 */
80
81static char *(history_names[]) =
82{
83 "cmd",
84 "search",
85 "expr",
86 "input",
87 "debug",
88 NULL
89};
90
Bram Moolenaard7663c22019-08-06 21:59:57 +020091/*
92 * Function given to ExpandGeneric() to obtain the possible first
93 * arguments of the ":history command.
94 */
95 char_u *
96get_history_arg(expand_T *xp UNUSED, int idx)
97{
98 static char_u compl[2] = { NUL, NUL };
99 char *short_names = ":=@>?/";
100 int short_names_count = (int)STRLEN(short_names);
K.Takataeeec2542021-06-02 13:28:16 +0200101 int history_name_count = ARRAY_LENGTH(history_names) - 1;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200102
103 if (idx < short_names_count)
104 {
105 compl[0] = (char_u)short_names[idx];
106 return compl;
107 }
108 if (idx < short_names_count + history_name_count)
109 return (char_u *)history_names[idx - short_names_count];
110 if (idx == short_names_count + history_name_count)
111 return (char_u *)"all";
112 return NULL;
113}
Bram Moolenaard7663c22019-08-06 21:59:57 +0200114
115/*
116 * init_history() - Initialize the command line history.
117 * Also used to re-allocate the history when the size changes.
118 */
119 void
120init_history(void)
121{
122 int newlen; // new length of history table
123 histentry_T *temp;
124 int i;
125 int j;
126 int type;
127
128 // If size of history table changed, reallocate it
129 newlen = (int)p_hi;
130 if (newlen != hislen) // history length changed
131 {
132 for (type = 0; type < HIST_COUNT; ++type) // adjust the tables
133 {
134 if (newlen)
135 {
136 temp = ALLOC_MULT(histentry_T, newlen);
137 if (temp == NULL) // out of memory!
138 {
139 if (type == 0) // first one: just keep the old length
140 {
141 newlen = hislen;
142 break;
143 }
144 // Already changed one table, now we can only have zero
145 // length for all tables.
146 newlen = 0;
147 type = -1;
148 continue;
149 }
150 }
151 else
152 temp = NULL;
153 if (newlen == 0 || temp != NULL)
154 {
155 if (hisidx[type] < 0) // there are no entries yet
156 {
157 for (i = 0; i < newlen; ++i)
158 clear_hist_entry(&temp[i]);
159 }
160 else if (newlen > hislen) // array becomes bigger
161 {
162 for (i = 0; i <= hisidx[type]; ++i)
163 temp[i] = history[type][i];
164 j = i;
165 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
166 clear_hist_entry(&temp[i]);
167 for ( ; j < hislen; ++i, ++j)
168 temp[i] = history[type][j];
169 }
170 else // array becomes smaller or 0
171 {
172 j = hisidx[type];
173 for (i = newlen - 1; ; --i)
174 {
175 if (i >= 0) // copy newest entries
176 temp[i] = history[type][j];
177 else // remove older entries
178 vim_free(history[type][j].hisstr);
179 if (--j < 0)
180 j = hislen - 1;
181 if (j == hisidx[type])
182 break;
183 }
184 hisidx[type] = newlen - 1;
185 }
186 vim_free(history[type]);
187 history[type] = temp;
188 }
189 }
190 hislen = newlen;
191 }
192}
193
194 void
195clear_hist_entry(histentry_T *hisptr)
196{
197 hisptr->hisnum = 0;
198 hisptr->viminfo = FALSE;
199 hisptr->hisstr = NULL;
200 hisptr->time_set = 0;
201}
202
203/*
204 * Check if command line 'str' is already in history.
205 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
206 */
207 int
208in_history(
209 int type,
210 char_u *str,
211 int move_to_front, // Move the entry to the front if it exists
212 int sep,
213 int writing) // ignore entries read from viminfo
214{
215 int i;
216 int last_i = -1;
217 char_u *p;
218
219 if (hisidx[type] < 0)
220 return FALSE;
221 i = hisidx[type];
222 do
223 {
224 if (history[type][i].hisstr == NULL)
225 return FALSE;
226
227 // For search history, check that the separator character matches as
228 // well.
229 p = history[type][i].hisstr;
230 if (STRCMP(str, p) == 0
231 && !(writing && history[type][i].viminfo)
232 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
233 {
234 if (!move_to_front)
235 return TRUE;
236 last_i = i;
237 break;
238 }
239 if (--i < 0)
240 i = hislen - 1;
241 } while (i != hisidx[type]);
242
243 if (last_i >= 0)
244 {
245 str = history[type][i].hisstr;
246 while (i != hisidx[type])
247 {
248 if (++i >= hislen)
249 i = 0;
250 history[type][last_i] = history[type][i];
251 last_i = i;
252 }
253 history[type][i].hisnum = ++hisnum[type];
254 history[type][i].viminfo = FALSE;
255 history[type][i].hisstr = str;
256 history[type][i].time_set = vim_time();
257 return TRUE;
258 }
259 return FALSE;
260}
261
262/*
263 * Convert history name (from table above) to its HIST_ equivalent.
264 * When "name" is empty, return "cmd" history.
265 * Returns -1 for unknown history name.
266 */
267 static int
268get_histtype(char_u *name)
269{
270 int i;
271 int len = (int)STRLEN(name);
272
273 // No argument: use current history.
274 if (len == 0)
275 return hist_char2type(get_cmdline_firstc());
276
277 for (i = 0; history_names[i] != NULL; ++i)
278 if (STRNICMP(name, history_names[i], len) == 0)
279 return i;
280
281 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
282 return hist_char2type(name[0]);
283
284 return -1;
285}
286
287static int last_maptick = -1; // last seen maptick
288
289/*
290 * Add the given string to the given history. If the string is already in the
291 * history then it is moved to the front. "histype" may be one of he HIST_
292 * values.
293 */
294 void
295add_to_history(
296 int histype,
297 char_u *new_entry,
298 int in_map, // consider maptick when inside a mapping
299 int sep) // separator character used (search hist)
300{
301 histentry_T *hisptr;
302 int len;
303
304 if (hislen == 0) // no history
305 return;
306
Bram Moolenaare1004402020-10-24 20:49:43 +0200307 if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) && histype == HIST_SEARCH)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200308 return;
309
310 // Searches inside the same mapping overwrite each other, so that only
311 // the last line is kept. Be careful not to remove a line that was moved
312 // down, only lines that were added.
313 if (histype == HIST_SEARCH && in_map)
314 {
315 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
316 {
317 // Current line is from the same mapping, remove it
318 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
319 vim_free(hisptr->hisstr);
320 clear_hist_entry(hisptr);
321 --hisnum[histype];
322 if (--hisidx[HIST_SEARCH] < 0)
323 hisidx[HIST_SEARCH] = hislen - 1;
324 }
325 last_maptick = -1;
326 }
327 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
328 {
329 if (++hisidx[histype] == hislen)
330 hisidx[histype] = 0;
331 hisptr = &history[histype][hisidx[histype]];
332 vim_free(hisptr->hisstr);
333
334 // Store the separator after the NUL of the string.
335 len = (int)STRLEN(new_entry);
336 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
337 if (hisptr->hisstr != NULL)
338 hisptr->hisstr[len + 1] = sep;
339
340 hisptr->hisnum = ++hisnum[histype];
341 hisptr->viminfo = FALSE;
342 hisptr->time_set = vim_time();
343 if (histype == HIST_SEARCH && in_map)
344 last_maptick = maptick;
345 }
346}
347
348#if defined(FEAT_EVAL) || defined(PROTO)
349
350/*
351 * Get identifier of newest history entry.
352 * "histype" may be one of the HIST_ values.
353 */
354 static int
355get_history_idx(int histype)
356{
357 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
358 || hisidx[histype] < 0)
359 return -1;
360
361 return history[histype][hisidx[histype]].hisnum;
362}
363
364/*
365 * Calculate history index from a number:
366 * num > 0: seen as identifying number of a history entry
367 * num < 0: relative position in history wrt newest entry
368 * "histype" may be one of the HIST_ values.
369 */
370 static int
371calc_hist_idx(int histype, int num)
372{
373 int i;
374 histentry_T *hist;
375 int wrapped = FALSE;
376
377 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
378 || (i = hisidx[histype]) < 0 || num == 0)
379 return -1;
380
381 hist = history[histype];
382 if (num > 0)
383 {
384 while (hist[i].hisnum > num)
385 if (--i < 0)
386 {
387 if (wrapped)
388 break;
389 i += hislen;
390 wrapped = TRUE;
391 }
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100392 if (i >= 0 && hist[i].hisnum == num && hist[i].hisstr != NULL)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200393 return i;
394 }
395 else if (-num <= hislen)
396 {
397 i += num + 1;
398 if (i < 0)
399 i += hislen;
400 if (hist[i].hisstr != NULL)
401 return i;
402 }
403 return -1;
404}
405
406/*
407 * Get a history entry by its index.
408 * "histype" may be one of the HIST_ values.
409 */
410 static char_u *
411get_history_entry(int histype, int idx)
412{
413 idx = calc_hist_idx(histype, idx);
414 if (idx >= 0)
415 return history[histype][idx].hisstr;
416 else
417 return (char_u *)"";
418}
419
420/*
421 * Clear all entries of a history.
422 * "histype" may be one of the HIST_ values.
423 */
424 static int
425clr_history(int histype)
426{
427 int i;
428 histentry_T *hisptr;
429
430 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
431 {
432 hisptr = history[histype];
433 for (i = hislen; i--;)
434 {
435 vim_free(hisptr->hisstr);
436 clear_hist_entry(hisptr);
437 hisptr++;
438 }
439 hisidx[histype] = -1; // mark history as cleared
440 hisnum[histype] = 0; // reset identifier counter
441 return OK;
442 }
443 return FAIL;
444}
445
446/*
447 * Remove all entries matching {str} from a history.
448 * "histype" may be one of the HIST_ values.
449 */
450 static int
451del_history_entry(int histype, char_u *str)
452{
453 regmatch_T regmatch;
454 histentry_T *hisptr;
455 int idx;
456 int i;
457 int last;
458 int found = FALSE;
459
460 regmatch.regprog = NULL;
461 regmatch.rm_ic = FALSE; // always match case
462 if (hislen != 0
463 && histype >= 0
464 && histype < HIST_COUNT
465 && *str != NUL
466 && (idx = hisidx[histype]) >= 0
467 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
468 != NULL)
469 {
470 i = last = idx;
471 do
472 {
473 hisptr = &history[histype][i];
474 if (hisptr->hisstr == NULL)
475 break;
476 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
477 {
478 found = TRUE;
479 vim_free(hisptr->hisstr);
480 clear_hist_entry(hisptr);
481 }
482 else
483 {
484 if (i != last)
485 {
486 history[histype][last] = *hisptr;
487 clear_hist_entry(hisptr);
488 }
489 if (--last < 0)
490 last += hislen;
491 }
492 if (--i < 0)
493 i += hislen;
494 } while (i != idx);
495 if (history[histype][idx].hisstr == NULL)
496 hisidx[histype] = -1;
497 }
498 vim_regfree(regmatch.regprog);
499 return found;
500}
501
502/*
503 * Remove an indexed entry from a history.
504 * "histype" may be one of the HIST_ values.
505 */
506 static int
507del_history_idx(int histype, int idx)
508{
509 int i, j;
510
511 i = calc_hist_idx(histype, idx);
512 if (i < 0)
513 return FALSE;
514 idx = hisidx[histype];
515 vim_free(history[histype][i].hisstr);
516
517 // When deleting the last added search string in a mapping, reset
518 // last_maptick, so that the last added search string isn't deleted again.
519 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
520 last_maptick = -1;
521
522 while (i != idx)
523 {
524 j = (i + 1) % hislen;
525 history[histype][i] = history[histype][j];
526 i = j;
527 }
528 clear_hist_entry(&history[histype][i]);
529 if (--i < 0)
530 i += hislen;
531 hisidx[histype] = i;
532 return TRUE;
533}
534
535/*
536 * "histadd()" function
537 */
538 void
539f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
540{
541 int histype;
542 char_u *str;
543 char_u buf[NUMBUFLEN];
544
545 rettv->vval.v_number = FALSE;
546 if (check_secure())
547 return;
548 str = tv_get_string_chk(&argvars[0]); // NULL on type error
549 histype = str != NULL ? get_histtype(str) : -1;
550 if (histype >= 0)
551 {
552 str = tv_get_string_buf(&argvars[1], buf);
553 if (*str != NUL)
554 {
555 init_history();
556 add_to_history(histype, str, FALSE, NUL);
557 rettv->vval.v_number = TRUE;
558 return;
559 }
560 }
561}
562
563/*
564 * "histdel()" function
565 */
566 void
567f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
568{
569 int n;
570 char_u buf[NUMBUFLEN];
571 char_u *str;
572
573 str = tv_get_string_chk(&argvars[0]); // NULL on type error
574 if (str == NULL)
575 n = 0;
576 else if (argvars[1].v_type == VAR_UNKNOWN)
577 // only one argument: clear entire history
578 n = clr_history(get_histtype(str));
579 else if (argvars[1].v_type == VAR_NUMBER)
580 // index given: remove that entry
581 n = del_history_idx(get_histtype(str),
582 (int)tv_get_number(&argvars[1]));
583 else
584 // string given: remove all matching entries
585 n = del_history_entry(get_histtype(str),
586 tv_get_string_buf(&argvars[1], buf));
587 rettv->vval.v_number = n;
588}
589
590/*
591 * "histget()" function
592 */
593 void
594f_histget(typval_T *argvars UNUSED, typval_T *rettv)
595{
596 int type;
597 int idx;
598 char_u *str;
599
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200600 if (in_vim9script()
601 && (check_for_string_arg(argvars, 0) == FAIL
602 || (argvars[1].v_type != VAR_UNKNOWN
603 && check_for_number_arg(argvars, 1) == FAIL)))
604 return;
605
Bram Moolenaard7663c22019-08-06 21:59:57 +0200606 str = tv_get_string_chk(&argvars[0]); // NULL on type error
607 if (str == NULL)
608 rettv->vval.v_string = NULL;
609 else
610 {
611 type = get_histtype(str);
612 if (argvars[1].v_type == VAR_UNKNOWN)
613 idx = get_history_idx(type);
614 else
615 idx = (int)tv_get_number_chk(&argvars[1], NULL);
616 // -1 on type error
617 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
618 }
619 rettv->v_type = VAR_STRING;
620}
621
622/*
623 * "histnr()" function
624 */
625 void
626f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
627{
628 int i;
629
630 char_u *histname = tv_get_string_chk(&argvars[0]);
631
632 i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname);
633 if (i >= HIST_CMD && i < HIST_COUNT)
634 i = get_history_idx(i);
635 else
636 i = -1;
637 rettv->vval.v_number = i;
638}
639#endif // FEAT_EVAL
640
641#if defined(FEAT_CRYPT) || defined(PROTO)
642/*
643 * Very specific function to remove the value in ":set key=val" from the
644 * history.
645 */
646 void
647remove_key_from_history(void)
648{
649 char_u *p;
650 int i;
651
652 i = hisidx[HIST_CMD];
653 if (i < 0)
654 return;
655 p = history[HIST_CMD][i].hisstr;
656 if (p != NULL)
657 for ( ; *p; ++p)
658 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
659 {
660 p = vim_strchr(p + 3, '=');
661 if (p == NULL)
662 break;
663 ++p;
664 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
665 if (p[i] == '\\' && p[i + 1])
666 ++i;
667 STRMOVE(p, p + i);
668 --p;
669 }
670}
671#endif
672
673/*
674 * :history command - print a history
675 */
676 void
677ex_history(exarg_T *eap)
678{
679 histentry_T *hist;
680 int histype1 = HIST_CMD;
681 int histype2 = HIST_CMD;
682 int hisidx1 = 1;
683 int hisidx2 = -1;
684 int idx;
685 int i, j, k;
686 char_u *end;
687 char_u *arg = eap->arg;
688
689 if (hislen == 0)
690 {
691 msg(_("'history' option is zero"));
692 return;
693 }
694
695 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
696 {
697 end = arg;
698 while (ASCII_ISALPHA(*end)
699 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
700 end++;
701 i = *end;
702 *end = NUL;
703 histype1 = get_histtype(arg);
704 if (histype1 == -1)
705 {
706 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
707 {
708 histype1 = 0;
709 histype2 = HIST_COUNT-1;
710 }
711 else
712 {
713 *end = i;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200714 semsg(_(e_trailing_arg), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200715 return;
716 }
717 }
718 else
719 histype2 = histype1;
720 *end = i;
721 }
722 else
723 end = arg;
724 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
725 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200726 semsg(_(e_trailing_arg), end);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200727 return;
728 }
729
730 for (; !got_int && histype1 <= histype2; ++histype1)
731 {
732 STRCPY(IObuff, "\n # ");
733 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
734 msg_puts_title((char *)IObuff);
735 idx = hisidx[histype1];
736 hist = history[histype1];
737 j = hisidx1;
738 k = hisidx2;
739 if (j < 0)
740 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
741 if (k < 0)
742 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
743 if (idx >= 0 && j <= k)
744 for (i = idx + 1; !got_int; ++i)
745 {
746 if (i == hislen)
747 i = 0;
748 if (hist[i].hisstr != NULL
749 && hist[i].hisnum >= j && hist[i].hisnum <= k)
750 {
751 msg_putchar('\n');
752 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
753 hist[i].hisnum);
754 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
755 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
756 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
757 else
758 STRCAT(IObuff, hist[i].hisstr);
759 msg_outtrans(IObuff);
760 out_flush();
761 }
762 if (i == idx)
763 break;
764 }
765 }
766}