blob: d398ca7a687b560938327cf65111d2096caedf00 [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,
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
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.
339 len = (int)STRLEN(new_entry);
340 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
341 if (hisptr->hisstr != NULL)
342 hisptr->hisstr[len + 1] = sep;
343
344 hisptr->hisnum = ++hisnum[histype];
345 hisptr->viminfo = FALSE;
346 hisptr->time_set = vim_time();
347 if (histype == HIST_SEARCH && in_map)
348 last_maptick = maptick;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200349}
350
351#if defined(FEAT_EVAL) || defined(PROTO)
352
353/*
354 * Get identifier of newest history entry.
355 * "histype" may be one of the HIST_ values.
356 */
357 static int
358get_history_idx(int histype)
359{
360 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
361 || hisidx[histype] < 0)
362 return -1;
363
364 return history[histype][hisidx[histype]].hisnum;
365}
366
367/*
368 * Calculate history index from a number:
369 * num > 0: seen as identifying number of a history entry
370 * num < 0: relative position in history wrt newest entry
371 * "histype" may be one of the HIST_ values.
372 */
373 static int
374calc_hist_idx(int histype, int num)
375{
376 int i;
377 histentry_T *hist;
378 int wrapped = FALSE;
379
380 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
381 || (i = hisidx[histype]) < 0 || num == 0)
382 return -1;
383
384 hist = history[histype];
385 if (num > 0)
386 {
387 while (hist[i].hisnum > num)
388 if (--i < 0)
389 {
390 if (wrapped)
391 break;
392 i += hislen;
393 wrapped = TRUE;
394 }
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100395 if (i >= 0 && hist[i].hisnum == num && hist[i].hisstr != NULL)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200396 return i;
397 }
398 else if (-num <= hislen)
399 {
400 i += num + 1;
401 if (i < 0)
402 i += hislen;
403 if (hist[i].hisstr != NULL)
404 return i;
405 }
406 return -1;
407}
408
409/*
410 * Get a history entry by its index.
411 * "histype" may be one of the HIST_ values.
412 */
413 static char_u *
414get_history_entry(int histype, int idx)
415{
416 idx = calc_hist_idx(histype, idx);
417 if (idx >= 0)
418 return history[histype][idx].hisstr;
419 else
420 return (char_u *)"";
421}
422
423/*
424 * Clear all entries of a history.
425 * "histype" may be one of the HIST_ values.
426 */
427 static int
428clr_history(int histype)
429{
430 int i;
431 histentry_T *hisptr;
432
433 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
434 {
435 hisptr = history[histype];
436 for (i = hislen; i--;)
437 {
438 vim_free(hisptr->hisstr);
439 clear_hist_entry(hisptr);
440 hisptr++;
441 }
442 hisidx[histype] = -1; // mark history as cleared
443 hisnum[histype] = 0; // reset identifier counter
444 return OK;
445 }
446 return FAIL;
447}
448
449/*
450 * Remove all entries matching {str} from a history.
451 * "histype" may be one of the HIST_ values.
452 */
453 static int
454del_history_entry(int histype, char_u *str)
455{
456 regmatch_T regmatch;
457 histentry_T *hisptr;
458 int idx;
459 int i;
460 int last;
461 int found = FALSE;
462
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000463 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT || *str == NUL
464 || hisidx[histype] < 0)
465 return FALSE;
466
467 idx = hisidx[histype];
468 regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING);
469 if (regmatch.regprog == NULL)
470 return FALSE;
471
Bram Moolenaard7663c22019-08-06 21:59:57 +0200472 regmatch.rm_ic = FALSE; // always match case
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000473
474 i = last = idx;
475 do
Bram Moolenaard7663c22019-08-06 21:59:57 +0200476 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000477 hisptr = &history[histype][i];
478 if (hisptr->hisstr == NULL)
479 break;
480 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
Bram Moolenaard7663c22019-08-06 21:59:57 +0200481 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000482 found = TRUE;
483 vim_free(hisptr->hisstr);
484 clear_hist_entry(hisptr);
485 }
486 else
487 {
488 if (i != last)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200489 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000490 history[histype][last] = *hisptr;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200491 clear_hist_entry(hisptr);
492 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000493 if (--last < 0)
494 last += hislen;
495 }
496 if (--i < 0)
497 i += hislen;
498 } while (i != idx);
499
500 if (history[histype][idx].hisstr == NULL)
501 hisidx[histype] = -1;
502
Bram Moolenaard7663c22019-08-06 21:59:57 +0200503 vim_regfree(regmatch.regprog);
504 return found;
505}
506
507/*
508 * Remove an indexed entry from a history.
509 * "histype" may be one of the HIST_ values.
510 */
511 static int
512del_history_idx(int histype, int idx)
513{
514 int i, j;
515
516 i = calc_hist_idx(histype, idx);
517 if (i < 0)
518 return FALSE;
519 idx = hisidx[histype];
520 vim_free(history[histype][i].hisstr);
521
522 // When deleting the last added search string in a mapping, reset
523 // last_maptick, so that the last added search string isn't deleted again.
524 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
525 last_maptick = -1;
526
527 while (i != idx)
528 {
529 j = (i + 1) % hislen;
530 history[histype][i] = history[histype][j];
531 i = j;
532 }
533 clear_hist_entry(&history[histype][i]);
534 if (--i < 0)
535 i += hislen;
536 hisidx[histype] = i;
537 return TRUE;
538}
539
540/*
541 * "histadd()" function
542 */
543 void
544f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
545{
546 int histype;
547 char_u *str;
548 char_u buf[NUMBUFLEN];
549
550 rettv->vval.v_number = FALSE;
551 if (check_secure())
552 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200553
554 if (in_vim9script()
555 && (check_for_string_arg(argvars, 0) == FAIL
556 || check_for_string_arg(argvars, 1) == FAIL))
557 return;
558
Bram Moolenaard7663c22019-08-06 21:59:57 +0200559 str = tv_get_string_chk(&argvars[0]); // NULL on type error
560 histype = str != NULL ? get_histtype(str) : -1;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000561 if (histype < 0)
562 return;
563
564 str = tv_get_string_buf(&argvars[1], buf);
565 if (*str == NUL)
566 return;
567
568 init_history();
569 add_to_history(histype, str, FALSE, NUL);
570 rettv->vval.v_number = TRUE;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200571}
572
573/*
574 * "histdel()" function
575 */
576 void
577f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
578{
579 int n;
580 char_u buf[NUMBUFLEN];
581 char_u *str;
582
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200583 if (in_vim9script()
584 && (check_for_string_arg(argvars, 0) == FAIL
585 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
586 return;
587
Bram Moolenaard7663c22019-08-06 21:59:57 +0200588 str = tv_get_string_chk(&argvars[0]); // NULL on type error
589 if (str == NULL)
590 n = 0;
591 else if (argvars[1].v_type == VAR_UNKNOWN)
592 // only one argument: clear entire history
593 n = clr_history(get_histtype(str));
594 else if (argvars[1].v_type == VAR_NUMBER)
595 // index given: remove that entry
596 n = del_history_idx(get_histtype(str),
597 (int)tv_get_number(&argvars[1]));
598 else
599 // string given: remove all matching entries
600 n = del_history_entry(get_histtype(str),
601 tv_get_string_buf(&argvars[1], buf));
602 rettv->vval.v_number = n;
603}
604
605/*
606 * "histget()" function
607 */
608 void
609f_histget(typval_T *argvars UNUSED, typval_T *rettv)
610{
611 int type;
612 int idx;
613 char_u *str;
614
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200615 if (in_vim9script()
616 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200617 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200618 return;
619
Bram Moolenaard7663c22019-08-06 21:59:57 +0200620 str = tv_get_string_chk(&argvars[0]); // NULL on type error
621 if (str == NULL)
622 rettv->vval.v_string = NULL;
623 else
624 {
625 type = get_histtype(str);
626 if (argvars[1].v_type == VAR_UNKNOWN)
627 idx = get_history_idx(type);
628 else
629 idx = (int)tv_get_number_chk(&argvars[1], NULL);
630 // -1 on type error
631 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
632 }
633 rettv->v_type = VAR_STRING;
634}
635
636/*
637 * "histnr()" function
638 */
639 void
640f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
641{
642 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200643 char_u *histname;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200644
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200645 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
646 return;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200647
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200648 histname = tv_get_string_chk(&argvars[0]);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200649 i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname);
650 if (i >= HIST_CMD && i < HIST_COUNT)
651 i = get_history_idx(i);
652 else
653 i = -1;
654 rettv->vval.v_number = i;
655}
656#endif // FEAT_EVAL
657
658#if defined(FEAT_CRYPT) || defined(PROTO)
659/*
660 * Very specific function to remove the value in ":set key=val" from the
661 * history.
662 */
663 void
664remove_key_from_history(void)
665{
666 char_u *p;
667 int i;
668
669 i = hisidx[HIST_CMD];
670 if (i < 0)
671 return;
672 p = history[HIST_CMD][i].hisstr;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000673 if (p == NULL)
674 return;
675
676 for ( ; *p; ++p)
677 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
678 {
679 p = vim_strchr(p + 3, '=');
680 if (p == NULL)
681 break;
682 ++p;
683 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
684 if (p[i] == '\\' && p[i + 1])
685 ++i;
686 STRMOVE(p, p + i);
687 --p;
688 }
Bram Moolenaard7663c22019-08-06 21:59:57 +0200689}
690#endif
691
692/*
693 * :history command - print a history
694 */
695 void
696ex_history(exarg_T *eap)
697{
698 histentry_T *hist;
699 int histype1 = HIST_CMD;
700 int histype2 = HIST_CMD;
701 int hisidx1 = 1;
702 int hisidx2 = -1;
703 int idx;
704 int i, j, k;
705 char_u *end;
706 char_u *arg = eap->arg;
707
708 if (hislen == 0)
709 {
710 msg(_("'history' option is zero"));
711 return;
712 }
713
714 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
715 {
716 end = arg;
717 while (ASCII_ISALPHA(*end)
718 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
719 end++;
720 i = *end;
721 *end = NUL;
722 histype1 = get_histtype(arg);
723 if (histype1 == -1)
724 {
725 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
726 {
727 histype1 = 0;
728 histype2 = HIST_COUNT-1;
729 }
730 else
731 {
732 *end = i;
Bram Moolenaar74409f62022-01-01 15:58:22 +0000733 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200734 return;
735 }
736 }
737 else
738 histype2 = histype1;
739 *end = i;
740 }
741 else
742 end = arg;
743 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
744 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000745 semsg(_(e_trailing_characters_str), end);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200746 return;
747 }
748
749 for (; !got_int && histype1 <= histype2; ++histype1)
750 {
751 STRCPY(IObuff, "\n # ");
752 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
753 msg_puts_title((char *)IObuff);
754 idx = hisidx[histype1];
755 hist = history[histype1];
756 j = hisidx1;
757 k = hisidx2;
758 if (j < 0)
759 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
760 if (k < 0)
761 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
762 if (idx >= 0 && j <= k)
763 for (i = idx + 1; !got_int; ++i)
764 {
765 if (i == hislen)
766 i = 0;
767 if (hist[i].hisstr != NULL
768 && hist[i].hisnum >= j && hist[i].hisnum <= k)
769 {
770 msg_putchar('\n');
771 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
772 hist[i].hisnum);
773 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
774 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
775 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
776 else
777 STRCAT(IObuff, hist[i].hisstr);
778 msg_outtrans(IObuff);
779 out_flush();
780 }
781 if (i == idx)
782 break;
783 }
784 }
785}