blob: 684c08e704faa5e973d5d7ca40d38bd87361f0a5 [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
Dominique Pelle748b3082022-01-08 12:41:16 +000040#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaard7663c22019-08-06 21:59:57 +020041 void
42set_histentry(int hist_type, histentry_T *entry)
43{
44 history[hist_type] = entry;
45}
Dominique Pelle748b3082022-01-08 12:41:16 +000046#endif
Bram Moolenaard7663c22019-08-06 21:59:57 +020047
48 int *
49get_hisidx(int hist_type)
50{
51 return &hisidx[hist_type];
52}
53
Dominique Pelle748b3082022-01-08 12:41:16 +000054#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaard7663c22019-08-06 21:59:57 +020055 int *
56get_hisnum(int hist_type)
57{
58 return &hisnum[hist_type];
59}
Dominique Pelle748b3082022-01-08 12:41:16 +000060#endif
Bram Moolenaard7663c22019-08-06 21:59:57 +020061
62/*
63 * Translate a history character to the associated type number.
64 */
65 int
66hist_char2type(int c)
67{
68 if (c == ':')
69 return HIST_CMD;
70 if (c == '=')
71 return HIST_EXPR;
72 if (c == '@')
73 return HIST_INPUT;
74 if (c == '>')
75 return HIST_DEBUG;
76 return HIST_SEARCH; // must be '?' or '/'
77}
78
79/*
80 * Table of history names.
81 * These names are used in :history and various hist...() functions.
82 * It is sufficient to give the significant prefix of a history name.
83 */
84
85static char *(history_names[]) =
86{
87 "cmd",
88 "search",
89 "expr",
90 "input",
91 "debug",
92 NULL
93};
94
Bram Moolenaard7663c22019-08-06 21:59:57 +020095/*
96 * Function given to ExpandGeneric() to obtain the possible first
97 * arguments of the ":history command.
98 */
99 char_u *
100get_history_arg(expand_T *xp UNUSED, int idx)
101{
Bram Moolenaar5ff595d2022-08-26 22:36:41 +0100102 char *short_names = ":=@>?/";
103 int short_names_count = (int)STRLEN(short_names);
104 int history_name_count = ARRAY_LENGTH(history_names) - 1;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200105
106 if (idx < short_names_count)
107 {
Bram Moolenaar5ff595d2022-08-26 22:36:41 +0100108 xp->xp_buf[0] = (char_u)short_names[idx];
109 xp->xp_buf[1] = NUL;
110 return xp->xp_buf;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200111 }
112 if (idx < short_names_count + history_name_count)
113 return (char_u *)history_names[idx - short_names_count];
114 if (idx == short_names_count + history_name_count)
115 return (char_u *)"all";
116 return NULL;
117}
Bram Moolenaard7663c22019-08-06 21:59:57 +0200118
119/*
120 * init_history() - Initialize the command line history.
121 * Also used to re-allocate the history when the size changes.
122 */
123 void
124init_history(void)
125{
126 int newlen; // new length of history table
127 histentry_T *temp;
128 int i;
129 int j;
130 int type;
131
132 // If size of history table changed, reallocate it
133 newlen = (int)p_hi;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000134 if (newlen == hislen) // history length didn't change
135 return;
136
137 // history length changed
138 for (type = 0; type < HIST_COUNT; ++type) // adjust the tables
Bram Moolenaard7663c22019-08-06 21:59:57 +0200139 {
Bram Moolenaarb298fe62022-11-14 14:36:41 +0000140 if (newlen > 0)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200141 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000142 temp = ALLOC_MULT(histentry_T, newlen);
143 if (temp == NULL) // out of memory!
Bram Moolenaard7663c22019-08-06 21:59:57 +0200144 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000145 if (type == 0) // first one: just keep the old length
Bram Moolenaard7663c22019-08-06 21:59:57 +0200146 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000147 newlen = hislen;
148 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200149 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000150 // Already changed one table, now we can only have zero
151 // length for all tables.
152 newlen = 0;
153 type = -1;
154 continue;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200155 }
156 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000157 else
158 temp = NULL;
159
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000160 if (hisidx[type] < 0) // there are no entries yet
161 {
162 for (i = 0; i < newlen; ++i)
163 clear_hist_entry(&temp[i]);
164 }
165 else if (newlen > hislen) // array becomes bigger
166 {
167 for (i = 0; i <= hisidx[type]; ++i)
168 temp[i] = history[type][i];
169 j = i;
170 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
171 clear_hist_entry(&temp[i]);
172 for ( ; j < hislen; ++i, ++j)
173 temp[i] = history[type][j];
174 }
175 else // array becomes smaller or 0
176 {
177 j = hisidx[type];
178 for (i = newlen - 1; ; --i)
179 {
180 if (i >= 0) // copy newest entries
181 temp[i] = history[type][j];
182 else // remove older entries
183 vim_free(history[type][j].hisstr);
184 if (--j < 0)
185 j = hislen - 1;
186 if (j == hisidx[type])
187 break;
188 }
189 hisidx[type] = newlen - 1;
190 }
191 vim_free(history[type]);
192 history[type] = temp;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200193 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000194 hislen = newlen;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200195}
196
197 void
198clear_hist_entry(histentry_T *hisptr)
199{
200 hisptr->hisnum = 0;
201 hisptr->viminfo = FALSE;
202 hisptr->hisstr = NULL;
203 hisptr->time_set = 0;
204}
205
206/*
207 * Check if command line 'str' is already in history.
208 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
209 */
210 int
211in_history(
212 int type,
213 char_u *str,
214 int move_to_front, // Move the entry to the front if it exists
215 int sep,
216 int writing) // ignore entries read from viminfo
217{
218 int i;
219 int last_i = -1;
220 char_u *p;
221
222 if (hisidx[type] < 0)
223 return FALSE;
224 i = hisidx[type];
225 do
226 {
227 if (history[type][i].hisstr == NULL)
228 return FALSE;
229
230 // For search history, check that the separator character matches as
231 // well.
232 p = history[type][i].hisstr;
233 if (STRCMP(str, p) == 0
234 && !(writing && history[type][i].viminfo)
235 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
236 {
237 if (!move_to_front)
238 return TRUE;
239 last_i = i;
240 break;
241 }
242 if (--i < 0)
243 i = hislen - 1;
244 } while (i != hisidx[type]);
245
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000246 if (last_i < 0)
247 return FALSE;
248
249 str = history[type][i].hisstr;
250 while (i != hisidx[type])
Bram Moolenaard7663c22019-08-06 21:59:57 +0200251 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000252 if (++i >= hislen)
253 i = 0;
254 history[type][last_i] = history[type][i];
255 last_i = i;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200256 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000257 history[type][i].hisnum = ++hisnum[type];
258 history[type][i].viminfo = FALSE;
259 history[type][i].hisstr = str;
260 history[type][i].time_set = vim_time();
261 return TRUE;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200262}
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
270get_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
289static 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
297add_to_history(
298 int histype,
299 char_u *new_entry,
John Marriott8c85a2a2024-05-20 19:18:26 +0200300 size_t new_entrylen,
Bram Moolenaard7663c22019-08-06 21:59:57 +0200301 int in_map, // consider maptick when inside a mapping
302 int sep) // separator character used (search hist)
303{
304 histentry_T *hisptr;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200305
306 if (hislen == 0) // no history
307 return;
308
Bram Moolenaare1004402020-10-24 20:49:43 +0200309 if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) && histype == HIST_SEARCH)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200310 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 }
Bram Moolenaard7663c22019-08-06 21:59:57 +0200329
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000330 if (in_history(histype, new_entry, TRUE, sep, FALSE))
331 return;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200332
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000333 if (++hisidx[histype] == hislen)
334 hisidx[histype] = 0;
335 hisptr = &history[histype][hisidx[histype]];
336 vim_free(hisptr->hisstr);
337
338 // Store the separator after the NUL of the string.
John Marriott8c85a2a2024-05-20 19:18:26 +0200339 hisptr->hisstr = vim_strnsave(new_entry, new_entrylen + 2);
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000340 if (hisptr->hisstr != NULL)
John Marriott8c85a2a2024-05-20 19:18:26 +0200341 hisptr->hisstr[new_entrylen + 1] = sep;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000342
343 hisptr->hisnum = ++hisnum[histype];
344 hisptr->viminfo = FALSE;
345 hisptr->time_set = vim_time();
346 if (histype == HIST_SEARCH && in_map)
347 last_maptick = maptick;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200348}
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
357get_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
373calc_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 }
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100394 if (i >= 0 && hist[i].hisnum == num && hist[i].hisstr != NULL)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200395 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 *
413get_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
427clr_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
453del_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
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000462 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT || *str == NUL
463 || hisidx[histype] < 0)
464 return FALSE;
465
466 idx = hisidx[histype];
467 regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING);
468 if (regmatch.regprog == NULL)
469 return FALSE;
470
Bram Moolenaard7663c22019-08-06 21:59:57 +0200471 regmatch.rm_ic = FALSE; // always match case
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000472
473 i = last = idx;
474 do
Bram Moolenaard7663c22019-08-06 21:59:57 +0200475 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000476 hisptr = &history[histype][i];
477 if (hisptr->hisstr == NULL)
478 break;
479 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
Bram Moolenaard7663c22019-08-06 21:59:57 +0200480 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000481 found = TRUE;
482 vim_free(hisptr->hisstr);
483 clear_hist_entry(hisptr);
484 }
485 else
486 {
487 if (i != last)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200488 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000489 history[histype][last] = *hisptr;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200490 clear_hist_entry(hisptr);
491 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000492 if (--last < 0)
493 last += hislen;
494 }
495 if (--i < 0)
496 i += hislen;
497 } while (i != idx);
498
499 if (history[histype][idx].hisstr == NULL)
500 hisidx[histype] = -1;
501
Bram Moolenaard7663c22019-08-06 21:59:57 +0200502 vim_regfree(regmatch.regprog);
503 return found;
504}
505
506/*
507 * Remove an indexed entry from a history.
508 * "histype" may be one of the HIST_ values.
509 */
510 static int
511del_history_idx(int histype, int idx)
512{
513 int i, j;
514
515 i = calc_hist_idx(histype, idx);
516 if (i < 0)
517 return FALSE;
518 idx = hisidx[histype];
519 vim_free(history[histype][i].hisstr);
520
521 // When deleting the last added search string in a mapping, reset
522 // last_maptick, so that the last added search string isn't deleted again.
523 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
524 last_maptick = -1;
525
526 while (i != idx)
527 {
528 j = (i + 1) % hislen;
529 history[histype][i] = history[histype][j];
530 i = j;
531 }
532 clear_hist_entry(&history[histype][i]);
533 if (--i < 0)
534 i += hislen;
535 hisidx[histype] = i;
536 return TRUE;
537}
538
539/*
540 * "histadd()" function
541 */
542 void
543f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
544{
545 int histype;
546 char_u *str;
547 char_u buf[NUMBUFLEN];
548
549 rettv->vval.v_number = FALSE;
550 if (check_secure())
551 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200552
553 if (in_vim9script()
554 && (check_for_string_arg(argvars, 0) == FAIL
555 || check_for_string_arg(argvars, 1) == FAIL))
556 return;
557
Bram Moolenaard7663c22019-08-06 21:59:57 +0200558 str = tv_get_string_chk(&argvars[0]); // NULL on type error
559 histype = str != NULL ? get_histtype(str) : -1;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000560 if (histype < 0)
561 return;
562
563 str = tv_get_string_buf(&argvars[1], buf);
564 if (*str == NUL)
565 return;
566
567 init_history();
John Marriott8c85a2a2024-05-20 19:18:26 +0200568 add_to_history(histype, str, STRLEN(str), FALSE, NUL);
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000569 rettv->vval.v_number = TRUE;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200570}
571
572/*
573 * "histdel()" function
574 */
575 void
576f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
577{
578 int n;
579 char_u buf[NUMBUFLEN];
580 char_u *str;
581
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200582 if (in_vim9script()
583 && (check_for_string_arg(argvars, 0) == FAIL
584 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
585 return;
586
Bram Moolenaard7663c22019-08-06 21:59:57 +0200587 str = tv_get_string_chk(&argvars[0]); // NULL on type error
588 if (str == NULL)
589 n = 0;
590 else if (argvars[1].v_type == VAR_UNKNOWN)
591 // only one argument: clear entire history
592 n = clr_history(get_histtype(str));
593 else if (argvars[1].v_type == VAR_NUMBER)
594 // index given: remove that entry
595 n = del_history_idx(get_histtype(str),
596 (int)tv_get_number(&argvars[1]));
597 else
598 // string given: remove all matching entries
599 n = del_history_entry(get_histtype(str),
600 tv_get_string_buf(&argvars[1], buf));
601 rettv->vval.v_number = n;
602}
603
604/*
605 * "histget()" function
606 */
607 void
608f_histget(typval_T *argvars UNUSED, typval_T *rettv)
609{
610 int type;
611 int idx;
612 char_u *str;
613
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200614 if (in_vim9script()
615 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200616 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200617 return;
618
Bram Moolenaard7663c22019-08-06 21:59:57 +0200619 str = tv_get_string_chk(&argvars[0]); // NULL on type error
620 if (str == NULL)
621 rettv->vval.v_string = NULL;
622 else
623 {
624 type = get_histtype(str);
625 if (argvars[1].v_type == VAR_UNKNOWN)
626 idx = get_history_idx(type);
627 else
628 idx = (int)tv_get_number_chk(&argvars[1], NULL);
629 // -1 on type error
630 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
631 }
632 rettv->v_type = VAR_STRING;
633}
634
635/*
636 * "histnr()" function
637 */
638 void
639f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
640{
641 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200642 char_u *histname;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200643
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200644 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
645 return;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200646
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200647 histname = tv_get_string_chk(&argvars[0]);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200648 i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname);
649 if (i >= HIST_CMD && i < HIST_COUNT)
650 i = get_history_idx(i);
651 else
652 i = -1;
653 rettv->vval.v_number = i;
654}
655#endif // FEAT_EVAL
656
657#if defined(FEAT_CRYPT) || defined(PROTO)
658/*
659 * Very specific function to remove the value in ":set key=val" from the
660 * history.
661 */
662 void
663remove_key_from_history(void)
664{
665 char_u *p;
666 int i;
667
668 i = hisidx[HIST_CMD];
669 if (i < 0)
670 return;
671 p = history[HIST_CMD][i].hisstr;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000672 if (p == NULL)
673 return;
674
675 for ( ; *p; ++p)
Keith Thompson184f71c2024-01-04 21:19:04 +0100676 if (STRNCMP(p, "key", 3) == 0 && !SAFE_isalpha(p[3]))
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000677 {
678 p = vim_strchr(p + 3, '=');
679 if (p == NULL)
680 break;
681 ++p;
682 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
683 if (p[i] == '\\' && p[i + 1])
684 ++i;
685 STRMOVE(p, p + i);
686 --p;
687 }
Bram Moolenaard7663c22019-08-06 21:59:57 +0200688}
689#endif
690
691/*
692 * :history command - print a history
693 */
694 void
695ex_history(exarg_T *eap)
696{
697 histentry_T *hist;
698 int histype1 = HIST_CMD;
699 int histype2 = HIST_CMD;
700 int hisidx1 = 1;
701 int hisidx2 = -1;
702 int idx;
703 int i, j, k;
704 char_u *end;
705 char_u *arg = eap->arg;
706
707 if (hislen == 0)
708 {
709 msg(_("'history' option is zero"));
710 return;
711 }
712
713 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
714 {
715 end = arg;
716 while (ASCII_ISALPHA(*end)
717 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
718 end++;
719 i = *end;
720 *end = NUL;
721 histype1 = get_histtype(arg);
722 if (histype1 == -1)
723 {
724 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
725 {
726 histype1 = 0;
727 histype2 = HIST_COUNT-1;
728 }
729 else
730 {
731 *end = i;
Bram Moolenaar74409f62022-01-01 15:58:22 +0000732 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200733 return;
734 }
735 }
736 else
737 histype2 = histype1;
738 *end = i;
739 }
740 else
741 end = arg;
742 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
743 {
Christian Brabandt9198c1f2023-10-26 21:29:32 +0200744 if (*end != NUL)
745 semsg(_(e_trailing_characters_str), end);
746 else
747 semsg(_(e_val_too_large), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200748 return;
749 }
750
751 for (; !got_int && histype1 <= histype2; ++histype1)
752 {
753 STRCPY(IObuff, "\n # ");
754 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
755 msg_puts_title((char *)IObuff);
756 idx = hisidx[histype1];
757 hist = history[histype1];
758 j = hisidx1;
759 k = hisidx2;
760 if (j < 0)
761 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
762 if (k < 0)
763 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
764 if (idx >= 0 && j <= k)
765 for (i = idx + 1; !got_int; ++i)
766 {
767 if (i == hislen)
768 i = 0;
769 if (hist[i].hisstr != NULL
Christian Brabandt42a5b5a2024-05-24 07:39:34 +0200770 && hist[i].hisnum >= j && hist[i].hisnum <= k
771 && !message_filtered(hist[i].hisstr))
Bram Moolenaard7663c22019-08-06 21:59:57 +0200772 {
773 msg_putchar('\n');
774 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
775 hist[i].hisnum);
776 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
777 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
778 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
779 else
780 STRCAT(IObuff, hist[i].hisstr);
781 msg_outtrans(IObuff);
782 out_flush();
783 }
784 if (i == idx)
785 break;
786 }
787 }
788}