comparison src/cmdhist.c @ 17652:9efb4dda9720

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