blob: 74089b20efd2770c18575ecfc9f1c8fa03004ff7 [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;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200128 int type;
129
130 // If size of history table changed, reallocate it
131 newlen = (int)p_hi;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000132 if (newlen == hislen) // history length didn't change
133 return;
134
135 // history length changed
136 for (type = 0; type < HIST_COUNT; ++type) // adjust the tables
Bram Moolenaard7663c22019-08-06 21:59:57 +0200137 {
Bram Moolenaarb298fe62022-11-14 14:36:41 +0000138 if (newlen > 0)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200139 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000140 temp = ALLOC_MULT(histentry_T, newlen);
141 if (temp == NULL) // out of memory!
Bram Moolenaard7663c22019-08-06 21:59:57 +0200142 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000143 if (type == 0) // first one: just keep the old length
Bram Moolenaard7663c22019-08-06 21:59:57 +0200144 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000145 newlen = hislen;
146 break;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200147 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000148 // Already changed one table, now we can only have zero
149 // length for all tables.
150 newlen = 0;
151 type = -1;
152 continue;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200153 }
154 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000155 else
156 temp = NULL;
157
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000158 if (hisidx[type] < 0) // there are no entries yet
159 {
John Marriott8df07d02024-10-21 22:37:07 +0200160 int i;
161
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000162 for (i = 0; i < newlen; ++i)
163 clear_hist_entry(&temp[i]);
164 }
165 else if (newlen > hislen) // array becomes bigger
166 {
John Marriott8df07d02024-10-21 22:37:07 +0200167 int i;
168 int j;
169
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000170 for (i = 0; i <= hisidx[type]; ++i)
171 temp[i] = history[type][i];
172 j = i;
173 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
174 clear_hist_entry(&temp[i]);
175 for ( ; j < hislen; ++i, ++j)
176 temp[i] = history[type][j];
177 }
178 else // array becomes smaller or 0
179 {
John Marriott8df07d02024-10-21 22:37:07 +0200180 int i;
181 int j;
182
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000183 j = hisidx[type];
184 for (i = newlen - 1; ; --i)
185 {
186 if (i >= 0) // copy newest entries
187 temp[i] = history[type][j];
188 else // remove older entries
John Marriott8df07d02024-10-21 22:37:07 +0200189 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000190 vim_free(history[type][j].hisstr);
John Marriott8df07d02024-10-21 22:37:07 +0200191 history[type][j].hisstrlen = 0;
192 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000193 if (--j < 0)
194 j = hislen - 1;
195 if (j == hisidx[type])
196 break;
197 }
198 hisidx[type] = newlen - 1;
199 }
200 vim_free(history[type]);
201 history[type] = temp;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200202 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000203 hislen = newlen;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200204}
205
206 void
207clear_hist_entry(histentry_T *hisptr)
208{
209 hisptr->hisnum = 0;
210 hisptr->viminfo = FALSE;
211 hisptr->hisstr = NULL;
John Marriott8df07d02024-10-21 22:37:07 +0200212 hisptr->hisstrlen = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200213 hisptr->time_set = 0;
214}
215
216/*
217 * Check if command line 'str' is already in history.
218 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
219 */
220 int
221in_history(
222 int type,
223 char_u *str,
224 int move_to_front, // Move the entry to the front if it exists
225 int sep,
226 int writing) // ignore entries read from viminfo
227{
228 int i;
229 int last_i = -1;
230 char_u *p;
John Marriott8df07d02024-10-21 22:37:07 +0200231 size_t len;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200232
233 if (hisidx[type] < 0)
234 return FALSE;
235 i = hisidx[type];
236 do
237 {
238 if (history[type][i].hisstr == NULL)
239 return FALSE;
240
241 // For search history, check that the separator character matches as
242 // well.
243 p = history[type][i].hisstr;
244 if (STRCMP(str, p) == 0
245 && !(writing && history[type][i].viminfo)
John Marriott8df07d02024-10-21 22:37:07 +0200246 && (type != HIST_SEARCH || sep == p[history[type][i].hisstrlen + 1]))
Bram Moolenaard7663c22019-08-06 21:59:57 +0200247 {
248 if (!move_to_front)
249 return TRUE;
250 last_i = i;
251 break;
252 }
253 if (--i < 0)
254 i = hislen - 1;
255 } while (i != hisidx[type]);
256
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000257 if (last_i < 0)
258 return FALSE;
259
260 str = history[type][i].hisstr;
John Marriott8df07d02024-10-21 22:37:07 +0200261 len = history[type][i].hisstrlen;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000262 while (i != hisidx[type])
Bram Moolenaard7663c22019-08-06 21:59:57 +0200263 {
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000264 if (++i >= hislen)
265 i = 0;
266 history[type][last_i] = history[type][i];
267 last_i = i;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200268 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000269 history[type][i].hisnum = ++hisnum[type];
270 history[type][i].viminfo = FALSE;
271 history[type][i].hisstr = str;
John Marriott8df07d02024-10-21 22:37:07 +0200272 history[type][i].hisstrlen = len;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000273 history[type][i].time_set = vim_time();
274 return TRUE;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200275}
276
277/*
278 * Convert history name (from table above) to its HIST_ equivalent.
279 * When "name" is empty, return "cmd" history.
280 * Returns -1 for unknown history name.
281 */
282 static int
283get_histtype(char_u *name)
284{
285 int i;
286 int len = (int)STRLEN(name);
287
288 // No argument: use current history.
289 if (len == 0)
290 return hist_char2type(get_cmdline_firstc());
291
292 for (i = 0; history_names[i] != NULL; ++i)
293 if (STRNICMP(name, history_names[i], len) == 0)
294 return i;
295
296 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
297 return hist_char2type(name[0]);
298
299 return -1;
300}
301
302static int last_maptick = -1; // last seen maptick
303
304/*
305 * Add the given string to the given history. If the string is already in the
306 * history then it is moved to the front. "histype" may be one of he HIST_
307 * values.
308 */
309 void
310add_to_history(
311 int histype,
312 char_u *new_entry,
John Marriott8c85a2a2024-05-20 19:18:26 +0200313 size_t new_entrylen,
Bram Moolenaard7663c22019-08-06 21:59:57 +0200314 int in_map, // consider maptick when inside a mapping
315 int sep) // separator character used (search hist)
316{
317 histentry_T *hisptr;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200318
319 if (hislen == 0) // no history
320 return;
321
Bram Moolenaare1004402020-10-24 20:49:43 +0200322 if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) && histype == HIST_SEARCH)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200323 return;
324
325 // Searches inside the same mapping overwrite each other, so that only
326 // the last line is kept. Be careful not to remove a line that was moved
327 // down, only lines that were added.
328 if (histype == HIST_SEARCH && in_map)
329 {
330 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
331 {
332 // Current line is from the same mapping, remove it
333 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
334 vim_free(hisptr->hisstr);
335 clear_hist_entry(hisptr);
336 --hisnum[histype];
337 if (--hisidx[HIST_SEARCH] < 0)
338 hisidx[HIST_SEARCH] = hislen - 1;
339 }
340 last_maptick = -1;
341 }
Bram Moolenaard7663c22019-08-06 21:59:57 +0200342
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000343 if (in_history(histype, new_entry, TRUE, sep, FALSE))
344 return;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200345
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000346 if (++hisidx[histype] == hislen)
347 hisidx[histype] = 0;
348 hisptr = &history[histype][hisidx[histype]];
349 vim_free(hisptr->hisstr);
350
351 // Store the separator after the NUL of the string.
John Marriott8c85a2a2024-05-20 19:18:26 +0200352 hisptr->hisstr = vim_strnsave(new_entry, new_entrylen + 2);
John Marriott8df07d02024-10-21 22:37:07 +0200353 if (hisptr->hisstr == NULL)
354 hisptr->hisstrlen = 0;
355 else
356 {
John Marriott8c85a2a2024-05-20 19:18:26 +0200357 hisptr->hisstr[new_entrylen + 1] = sep;
John Marriott8df07d02024-10-21 22:37:07 +0200358 hisptr->hisstrlen = new_entrylen;
359 }
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000360
361 hisptr->hisnum = ++hisnum[histype];
362 hisptr->viminfo = FALSE;
363 hisptr->time_set = vim_time();
364 if (histype == HIST_SEARCH && in_map)
365 last_maptick = maptick;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200366}
367
368#if defined(FEAT_EVAL) || defined(PROTO)
369
370/*
371 * Get identifier of newest history entry.
372 * "histype" may be one of the HIST_ values.
373 */
374 static int
375get_history_idx(int histype)
376{
377 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
378 || hisidx[histype] < 0)
379 return -1;
380
381 return history[histype][hisidx[histype]].hisnum;
382}
383
384/*
385 * Calculate history index from a number:
386 * num > 0: seen as identifying number of a history entry
387 * num < 0: relative position in history wrt newest entry
388 * "histype" may be one of the HIST_ values.
389 */
390 static int
391calc_hist_idx(int histype, int num)
392{
393 int i;
394 histentry_T *hist;
395 int wrapped = FALSE;
396
397 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
398 || (i = hisidx[histype]) < 0 || num == 0)
399 return -1;
400
401 hist = history[histype];
402 if (num > 0)
403 {
404 while (hist[i].hisnum > num)
405 if (--i < 0)
406 {
407 if (wrapped)
408 break;
409 i += hislen;
410 wrapped = TRUE;
411 }
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100412 if (i >= 0 && hist[i].hisnum == num && hist[i].hisstr != NULL)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200413 return i;
414 }
415 else if (-num <= hislen)
416 {
417 i += num + 1;
418 if (i < 0)
419 i += hislen;
420 if (hist[i].hisstr != NULL)
421 return i;
422 }
423 return -1;
424}
425
426/*
Bram Moolenaard7663c22019-08-06 21:59:57 +0200427 * Clear all entries of a history.
428 * "histype" may be one of the HIST_ values.
429 */
430 static int
431clr_history(int histype)
432{
433 int i;
434 histentry_T *hisptr;
435
436 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
437 {
438 hisptr = history[histype];
439 for (i = hislen; i--;)
440 {
441 vim_free(hisptr->hisstr);
442 clear_hist_entry(hisptr);
443 hisptr++;
444 }
445 hisidx[histype] = -1; // mark history as cleared
446 hisnum[histype] = 0; // reset identifier counter
447 return OK;
448 }
449 return FAIL;
450}
451
452/*
453 * Remove all entries matching {str} from a history.
454 * "histype" may be one of the HIST_ values.
455 */
456 static int
457del_history_entry(int histype, char_u *str)
458{
459 regmatch_T regmatch;
460 histentry_T *hisptr;
461 int idx;
462 int i;
463 int last;
464 int found = FALSE;
465
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000466 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT || *str == NUL
467 || hisidx[histype] < 0)
468 return FALSE;
469
470 idx = hisidx[histype];
471 regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING);
472 if (regmatch.regprog == NULL)
473 return FALSE;
474
Bram Moolenaard7663c22019-08-06 21:59:57 +0200475 regmatch.rm_ic = FALSE; // always match case
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000476
477 i = last = idx;
478 do
Bram Moolenaard7663c22019-08-06 21:59:57 +0200479 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000480 hisptr = &history[histype][i];
481 if (hisptr->hisstr == NULL)
482 break;
483 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
Bram Moolenaard7663c22019-08-06 21:59:57 +0200484 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000485 found = TRUE;
486 vim_free(hisptr->hisstr);
487 clear_hist_entry(hisptr);
488 }
489 else
490 {
491 if (i != last)
Bram Moolenaard7663c22019-08-06 21:59:57 +0200492 {
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000493 history[histype][last] = *hisptr;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200494 clear_hist_entry(hisptr);
495 }
Yegappan Lakshmanan465de3a2022-12-26 12:50:04 +0000496 if (--last < 0)
497 last += hislen;
498 }
499 if (--i < 0)
500 i += hislen;
501 } while (i != idx);
502
503 if (history[histype][idx].hisstr == NULL)
504 hisidx[histype] = -1;
505
Bram Moolenaard7663c22019-08-06 21:59:57 +0200506 vim_regfree(regmatch.regprog);
507 return found;
508}
509
510/*
511 * Remove an indexed entry from a history.
512 * "histype" may be one of the HIST_ values.
513 */
514 static int
515del_history_idx(int histype, int idx)
516{
517 int i, j;
518
519 i = calc_hist_idx(histype, idx);
520 if (i < 0)
521 return FALSE;
522 idx = hisidx[histype];
523 vim_free(history[histype][i].hisstr);
John Marriott8df07d02024-10-21 22:37:07 +0200524 history[histype][i].hisstrlen = 0;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200525
526 // When deleting the last added search string in a mapping, reset
527 // last_maptick, so that the last added search string isn't deleted again.
528 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
529 last_maptick = -1;
530
531 while (i != idx)
532 {
533 j = (i + 1) % hislen;
534 history[histype][i] = history[histype][j];
535 i = j;
536 }
537 clear_hist_entry(&history[histype][i]);
538 if (--i < 0)
539 i += hislen;
540 hisidx[histype] = i;
541 return TRUE;
542}
543
544/*
545 * "histadd()" function
546 */
547 void
548f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
549{
550 int histype;
551 char_u *str;
552 char_u buf[NUMBUFLEN];
553
554 rettv->vval.v_number = FALSE;
555 if (check_secure())
556 return;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200557
558 if (in_vim9script()
559 && (check_for_string_arg(argvars, 0) == FAIL
560 || check_for_string_arg(argvars, 1) == FAIL))
561 return;
562
Bram Moolenaard7663c22019-08-06 21:59:57 +0200563 str = tv_get_string_chk(&argvars[0]); // NULL on type error
564 histype = str != NULL ? get_histtype(str) : -1;
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000565 if (histype < 0)
566 return;
567
568 str = tv_get_string_buf(&argvars[1], buf);
569 if (*str == NUL)
570 return;
571
572 init_history();
John Marriott8c85a2a2024-05-20 19:18:26 +0200573 add_to_history(histype, str, STRLEN(str), FALSE, NUL);
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000574 rettv->vval.v_number = TRUE;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200575}
576
577/*
578 * "histdel()" function
579 */
580 void
581f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
582{
583 int n;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200584 char_u *str;
585
Yegappan Lakshmanan0ad871d2021-07-23 20:37:56 +0200586 if (in_vim9script()
587 && (check_for_string_arg(argvars, 0) == FAIL
588 || check_for_opt_string_or_number_arg(argvars, 1) == FAIL))
589 return;
590
Bram Moolenaard7663c22019-08-06 21:59:57 +0200591 str = tv_get_string_chk(&argvars[0]); // NULL on type error
592 if (str == NULL)
593 n = 0;
594 else if (argvars[1].v_type == VAR_UNKNOWN)
595 // only one argument: clear entire history
596 n = clr_history(get_histtype(str));
597 else if (argvars[1].v_type == VAR_NUMBER)
598 // index given: remove that entry
599 n = del_history_idx(get_histtype(str),
600 (int)tv_get_number(&argvars[1]));
601 else
John Marriott8df07d02024-10-21 22:37:07 +0200602 {
603 char_u buf[NUMBUFLEN];
604
Bram Moolenaard7663c22019-08-06 21:59:57 +0200605 // string given: remove all matching entries
606 n = del_history_entry(get_histtype(str),
607 tv_get_string_buf(&argvars[1], buf));
John Marriott8df07d02024-10-21 22:37:07 +0200608 }
609
Bram Moolenaard7663c22019-08-06 21:59:57 +0200610 rettv->vval.v_number = n;
611}
612
613/*
614 * "histget()" function
615 */
616 void
617f_histget(typval_T *argvars UNUSED, typval_T *rettv)
618{
John Marriott8df07d02024-10-21 22:37:07 +0200619 char_u *str;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200620
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200621 if (in_vim9script()
622 && (check_for_string_arg(argvars, 0) == FAIL
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +0200623 || check_for_opt_number_arg(argvars, 1) == FAIL))
Yegappan Lakshmanan1a71d312021-07-15 12:49:58 +0200624 return;
625
Bram Moolenaard7663c22019-08-06 21:59:57 +0200626 str = tv_get_string_chk(&argvars[0]); // NULL on type error
627 if (str == NULL)
628 rettv->vval.v_string = NULL;
629 else
630 {
John Marriott8df07d02024-10-21 22:37:07 +0200631 int type;
632 int idx;
633
Bram Moolenaard7663c22019-08-06 21:59:57 +0200634 type = get_histtype(str);
635 if (argvars[1].v_type == VAR_UNKNOWN)
636 idx = get_history_idx(type);
637 else
638 idx = (int)tv_get_number_chk(&argvars[1], NULL);
639 // -1 on type error
John Marriott8df07d02024-10-21 22:37:07 +0200640
641 idx = calc_hist_idx(type, idx);
642 if (idx < 0)
643 rettv->vval.v_string = vim_strnsave((char_u *)"", 0);
644 else
645 rettv->vval.v_string = vim_strnsave(history[type][idx].hisstr, history[type][idx].hisstrlen);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200646 }
647 rettv->v_type = VAR_STRING;
648}
649
650/*
651 * "histnr()" function
652 */
653 void
654f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
655{
656 int i;
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200657 char_u *histname;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200658
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200659 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
660 return;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200661
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +0200662 histname = tv_get_string_chk(&argvars[0]);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200663 i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname);
664 if (i >= HIST_CMD && i < HIST_COUNT)
John Marriott8df07d02024-10-21 22:37:07 +0200665 rettv->vval.v_number = get_history_idx(i);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200666 else
John Marriott8df07d02024-10-21 22:37:07 +0200667 rettv->vval.v_number = -1;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200668}
669#endif // FEAT_EVAL
670
671#if defined(FEAT_CRYPT) || defined(PROTO)
672/*
673 * Very specific function to remove the value in ":set key=val" from the
674 * history.
675 */
676 void
677remove_key_from_history(void)
678{
John Marriott8df07d02024-10-21 22:37:07 +0200679 char_u *p_start;
680 char_u *p_end;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200681 char_u *p;
682 int i;
683
684 i = hisidx[HIST_CMD];
685 if (i < 0)
686 return;
John Marriott8df07d02024-10-21 22:37:07 +0200687 p_start = history[HIST_CMD][i].hisstr;
688 if (p_start == NULL)
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000689 return;
690
John Marriott8df07d02024-10-21 22:37:07 +0200691 p_end = p_start + history[HIST_CMD][i].hisstrlen;
692 for (p = p_start; *p; ++p)
693 {
Keith Thompson184f71c2024-01-04 21:19:04 +0100694 if (STRNCMP(p, "key", 3) == 0 && !SAFE_isalpha(p[3]))
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000695 {
696 p = vim_strchr(p + 3, '=');
697 if (p == NULL)
698 break;
699 ++p;
700 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
701 if (p[i] == '\\' && p[i + 1])
702 ++i;
John Marriott8df07d02024-10-21 22:37:07 +0200703
704 mch_memmove(p, p + i, (p_end - (p + i)) + 1); // +1 for the NUL
705 p_end -= i; // adjust p_end for shortened string
Yegappan Lakshmanan623e94e2022-11-13 18:11:17 +0000706 --p;
707 }
John Marriott8df07d02024-10-21 22:37:07 +0200708 }
709
710 history[HIST_CMD][i].hisstrlen = (size_t)(p_end - p_start);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200711}
712#endif
713
714/*
715 * :history command - print a history
716 */
717 void
718ex_history(exarg_T *eap)
719{
720 histentry_T *hist;
721 int histype1 = HIST_CMD;
722 int histype2 = HIST_CMD;
723 int hisidx1 = 1;
724 int hisidx2 = -1;
725 int idx;
726 int i, j, k;
727 char_u *end;
728 char_u *arg = eap->arg;
729
730 if (hislen == 0)
731 {
732 msg(_("'history' option is zero"));
733 return;
734 }
735
736 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
737 {
738 end = arg;
739 while (ASCII_ISALPHA(*end)
740 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
741 end++;
742 i = *end;
743 *end = NUL;
744 histype1 = get_histtype(arg);
745 if (histype1 == -1)
746 {
747 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
748 {
749 histype1 = 0;
750 histype2 = HIST_COUNT-1;
751 }
752 else
753 {
754 *end = i;
Bram Moolenaar74409f62022-01-01 15:58:22 +0000755 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200756 return;
757 }
758 }
759 else
760 histype2 = histype1;
761 *end = i;
762 }
763 else
764 end = arg;
765 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
766 {
Christian Brabandt9198c1f2023-10-26 21:29:32 +0200767 if (*end != NUL)
768 semsg(_(e_trailing_characters_str), end);
769 else
770 semsg(_(e_val_too_large), arg);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200771 return;
772 }
773
774 for (; !got_int && histype1 <= histype2; ++histype1)
775 {
John Marriott8df07d02024-10-21 22:37:07 +0200776 vim_snprintf((char *)IObuff, IOSIZE, "\n # %s history", history_names[histype1]);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200777 msg_puts_title((char *)IObuff);
778 idx = hisidx[histype1];
779 hist = history[histype1];
780 j = hisidx1;
781 k = hisidx2;
782 if (j < 0)
John Marriott8df07d02024-10-21 22:37:07 +0200783 j = (-j > hislen) ? 0 : hist[(hislen + j + idx + 1) % hislen].hisnum;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200784 if (k < 0)
John Marriott8df07d02024-10-21 22:37:07 +0200785 k = (-k > hislen) ? 0 : hist[(hislen + k + idx + 1) % hislen].hisnum;
Bram Moolenaard7663c22019-08-06 21:59:57 +0200786 if (idx >= 0 && j <= k)
787 for (i = idx + 1; !got_int; ++i)
788 {
789 if (i == hislen)
790 i = 0;
791 if (hist[i].hisstr != NULL
Christian Brabandt42a5b5a2024-05-24 07:39:34 +0200792 && hist[i].hisnum >= j && hist[i].hisnum <= k
793 && !message_filtered(hist[i].hisstr))
Bram Moolenaard7663c22019-08-06 21:59:57 +0200794 {
John Marriott8df07d02024-10-21 22:37:07 +0200795 int len;
796
Bram Moolenaard7663c22019-08-06 21:59:57 +0200797 msg_putchar('\n');
John Marriott8df07d02024-10-21 22:37:07 +0200798 len = vim_snprintf((char *)IObuff, IOSIZE,
799 "%c%6d ", i == idx ? '>' : ' ', hist[i].hisnum);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200800 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
John Marriott8df07d02024-10-21 22:37:07 +0200801 trunc_string(hist[i].hisstr, IObuff + len,
802 (int)Columns - 10, IOSIZE - (int)len);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200803 else
John Marriott8df07d02024-10-21 22:37:07 +0200804 STRCPY(IObuff + len, hist[i].hisstr);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200805 msg_outtrans(IObuff);
806 out_flush();
807 }
808 if (i == idx)
809 break;
810 }
811 }
812}