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