blob: ea955478ef083d7b122c4d981474c1167dacbdad [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
463 regmatch.regprog = NULL;
464 regmatch.rm_ic = FALSE; // always match case
465 if (hislen != 0
466 && histype >= 0
467 && histype < HIST_COUNT
468 && *str != NUL
469 && (idx = hisidx[histype]) >= 0
470 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
471 != NULL)
472 {
473 i = last = idx;
474 do
475 {
476 hisptr = &history[histype][i];
477 if (hisptr->hisstr == NULL)
478 break;
479 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
480 {
481 found = TRUE;
482 vim_free(hisptr->hisstr);
483 clear_hist_entry(hisptr);
484 }
485 else
486 {
487 if (i != last)
488 {
489 history[histype][last] = *hisptr;
490 clear_hist_entry(hisptr);
491 }
492 if (--last < 0)
493 last += hislen;
494 }
495 if (--i < 0)
496 i += hislen;
497 } while (i != idx);
498 if (history[histype][idx].hisstr == NULL)
499 hisidx[histype] = -1;
500 }
501 vim_regfree(regmatch.regprog);
502 return found;
503}
504
505/*
506 * Remove an indexed entry from a history.
507 * "histype" may be one of the HIST_ values.
508 */
509 static int
510del_history_idx(int histype, int idx)
511{
512 int i, j;
513
514 i = calc_hist_idx(histype, idx);
515 if (i < 0)
516 return FALSE;
517 idx = hisidx[histype];
518 vim_free(history[histype][i].hisstr);
519
520 // When deleting the last added search string in a mapping, reset
521 // last_maptick, so that the last added search string isn't deleted again.
522 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
523 last_maptick = -1;
524
525 while (i != idx)
526 {
527 j = (i + 1) % hislen;
528 history[histype][i] = history[histype][j];
529 i = j;
530 }
531 clear_hist_entry(&history[histype][i]);
532 if (--i < 0)
533 i += hislen;
534 hisidx[histype] = i;
535 return TRUE;
536}
537
538/*
539 * "histadd()" function
540 */
541 void
542f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
543{
544 int histype;
545 char_u *str;
546 char_u buf[NUMBUFLEN];
547
548 rettv->vval.v_number = FALSE;
549 if (check_secure())
550 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200551
552 if (in_vim9script()
553 && (check_for_string_arg(argvars, 0) == FAIL
554 || check_for_string_arg(argvars, 1) == FAIL))
555 return;
556
Bram Moolenaard7663c22019-08-06 21:59:57 +0200557 str = tv_get_string_chk(&argvars[0]); // NULL on type error
558 histype = str != NULL ? get_histtype(str) : -1;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000559 if (histype < 0)
560 return;
561
562 str = tv_get_string_buf(&argvars[1], buf);
563 if (*str == NUL)
564 return;
565
566 init_history();
567 add_to_history(histype, str, FALSE, NUL);
568 rettv->vval.v_number = TRUE;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200569}
570
571/*
572 * "histdel()" function
573 */
574 void
575f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
576{
577 int n;
578 char_u buf[NUMBUFLEN];
579 char_u *str;
580
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200581 if (in_vim9script()
582 && (check_for_string_arg(argvars, 0) == FAIL
583 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
584 return;
585
Bram Moolenaard7663c22019-08-06 21:59:57 +0200586 str = tv_get_string_chk(&argvars[0]); // NULL on type error
587 if (str == NULL)
588 n = 0;
589 else if (argvars[1].v_type == VAR_UNKNOWN)
590 // only one argument: clear entire history
591 n = clr_history(get_histtype(str));
592 else if (argvars[1].v_type == VAR_NUMBER)
593 // index given: remove that entry
594 n = del_history_idx(get_histtype(str),
595 (int)tv_get_number(&argvars[1]));
596 else
597 // string given: remove all matching entries
598 n = del_history_entry(get_histtype(str),
599 tv_get_string_buf(&argvars[1], buf));
600 rettv->vval.v_number = n;
601}
602
603/*
604 * "histget()" function
605 */
606 void
607f_histget(typval_T *argvars UNUSED, typval_T *rettv)
608{
609 int type;
610 int idx;
611 char_u *str;
612
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200613 if (in_vim9script()
614 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200615 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200616 return;
617
Bram Moolenaard7663c22019-08-06 21:59:57 +0200618 str = tv_get_string_chk(&argvars[0]); // NULL on type error
619 if (str == NULL)
620 rettv->vval.v_string = NULL;
621 else
622 {
623 type = get_histtype(str);
624 if (argvars[1].v_type == VAR_UNKNOWN)
625 idx = get_history_idx(type);
626 else
627 idx = (int)tv_get_number_chk(&argvars[1], NULL);
628 // -1 on type error
629 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
630 }
631 rettv->v_type = VAR_STRING;
632}
633
634/*
635 * "histnr()" function
636 */
637 void
638f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
639{
640 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200641 char_u *histname;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200642
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200643 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
644 return;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200645
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200646 histname = tv_get_string_chk(&argvars[0]);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200647 i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname);
648 if (i >= HIST_CMD && i < HIST_COUNT)
649 i = get_history_idx(i);
650 else
651 i = -1;
652 rettv->vval.v_number = i;
653}
654#endif // FEAT_EVAL
655
656#if defined(FEAT_CRYPT) || defined(PROTO)
657/*
658 * Very specific function to remove the value in ":set key=val" from the
659 * history.
660 */
661 void
662remove_key_from_history(void)
663{
664 char_u *p;
665 int i;
666
667 i = hisidx[HIST_CMD];
668 if (i < 0)
669 return;
670 p = history[HIST_CMD][i].hisstr;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000671 if (p == NULL)
672 return;
673
674 for ( ; *p; ++p)
675 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
676 {
677 p = vim_strchr(p + 3, '=');
678 if (p == NULL)
679 break;
680 ++p;
681 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
682 if (p[i] == '\\' && p[i + 1])
683 ++i;
684 STRMOVE(p, p + i);
685 --p;
686 }
Bram Moolenaard7663c22019-08-06 21:59:57 +0200687}
688#endif
689
690/*
691 * :history command - print a history
692 */
693 void
694ex_history(exarg_T *eap)
695{
696 histentry_T *hist;
697 int histype1 = HIST_CMD;
698 int histype2 = HIST_CMD;
699 int hisidx1 = 1;
700 int hisidx2 = -1;
701 int idx;
702 int i, j, k;
703 char_u *end;
704 char_u *arg = eap->arg;
705
706 if (hislen == 0)
707 {
708 msg(_("'history' option is zero"));
709 return;
710 }
711
712 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
713 {
714 end = arg;
715 while (ASCII_ISALPHA(*end)
716 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
717 end++;
718 i = *end;
719 *end = NUL;
720 histype1 = get_histtype(arg);
721 if (histype1 == -1)
722 {
723 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
724 {
725 histype1 = 0;
726 histype2 = HIST_COUNT-1;
727 }
728 else
729 {
730 *end = i;
Bram Moolenaar74409f62022-01-01 15:58:22 +0000731 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200732 return;
733 }
734 }
735 else
736 histype2 = histype1;
737 *end = i;
738 }
739 else
740 end = arg;
741 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
742 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000743 semsg(_(e_trailing_characters_str), end);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200744 return;
745 }
746
747 for (; !got_int && histype1 <= histype2; ++histype1)
748 {
749 STRCPY(IObuff, "\n # ");
750 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
751 msg_puts_title((char *)IObuff);
752 idx = hisidx[histype1];
753 hist = history[histype1];
754 j = hisidx1;
755 k = hisidx2;
756 if (j < 0)
757 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
758 if (k < 0)
759 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
760 if (idx >= 0 && j <= k)
761 for (i = idx + 1; !got_int; ++i)
762 {
763 if (i == hislen)
764 i = 0;
765 if (hist[i].hisstr != NULL
766 && hist[i].hisnum >= j && hist[i].hisnum <= k)
767 {
768 msg_putchar('\n');
769 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
770 hist[i].hisnum);
771 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
772 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
773 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
774 else
775 STRCAT(IObuff, hist[i].hisstr);
776 msg_outtrans(IObuff);
777 out_flush();
778 }
779 if (i == idx)
780 break;
781 }
782 }
783}