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 = sizeof(history_names) / sizeof(char *) - 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 str = tv_get_string_chk(&argvars[0]); // NULL on type error 549 histype = str != NULL ? get_histtype(str) : -1; 550 if (histype >= 0) 551 { 552 str = tv_get_string_buf(&argvars[1], buf); 553 if (*str != NUL) 554 { 555 init_history(); 556 add_to_history(histype, str, FALSE, NUL); 557 rettv->vval.v_number = TRUE; 558 return; 559 } 560 } 561 } 562 563 /* 564 * "histdel()" function 565 */ 566 void 567 f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED) 568 { 569 int n; 570 char_u buf[NUMBUFLEN]; 571 char_u *str; 572 573 str = tv_get_string_chk(&argvars[0]); // NULL on type error 574 if (str == NULL) 575 n = 0; 576 else if (argvars[1].v_type == VAR_UNKNOWN) 577 // only one argument: clear entire history 578 n = clr_history(get_histtype(str)); 579 else if (argvars[1].v_type == VAR_NUMBER) 580 // index given: remove that entry 581 n = del_history_idx(get_histtype(str), 582 (int)tv_get_number(&argvars[1])); 583 else 584 // string given: remove all matching entries 585 n = del_history_entry(get_histtype(str), 586 tv_get_string_buf(&argvars[1], buf)); 587 rettv->vval.v_number = n; 588 } 589 590 /* 591 * "histget()" function 592 */ 593 void 594 f_histget(typval_T *argvars UNUSED, typval_T *rettv) 595 { 596 int type; 597 int idx; 598 char_u *str; 599 600 str = tv_get_string_chk(&argvars[0]); // NULL on type error 601 if (str == NULL) 602 rettv->vval.v_string = NULL; 603 else 604 { 605 type = get_histtype(str); 606 if (argvars[1].v_type == VAR_UNKNOWN) 607 idx = get_history_idx(type); 608 else 609 idx = (int)tv_get_number_chk(&argvars[1], NULL); 610 // -1 on type error 611 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx)); 612 } 613 rettv->v_type = VAR_STRING; 614 } 615 616 /* 617 * "histnr()" function 618 */ 619 void 620 f_histnr(typval_T *argvars UNUSED, typval_T *rettv) 621 { 622 int i; 623 624 char_u *histname = tv_get_string_chk(&argvars[0]); 625 626 i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname); 627 if (i >= HIST_CMD && i < HIST_COUNT) 628 i = get_history_idx(i); 629 else 630 i = -1; 631 rettv->vval.v_number = i; 632 } 633 #endif // FEAT_EVAL 634 635 #if defined(FEAT_CRYPT) || defined(PROTO) 636 /* 637 * Very specific function to remove the value in ":set key=val" from the 638 * history. 639 */ 640 void 641 remove_key_from_history(void) 642 { 643 char_u *p; 644 int i; 645 646 i = hisidx[HIST_CMD]; 647 if (i < 0) 648 return; 649 p = history[HIST_CMD][i].hisstr; 650 if (p != NULL) 651 for ( ; *p; ++p) 652 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) 653 { 654 p = vim_strchr(p + 3, '='); 655 if (p == NULL) 656 break; 657 ++p; 658 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) 659 if (p[i] == '\\' && p[i + 1]) 660 ++i; 661 STRMOVE(p, p + i); 662 --p; 663 } 664 } 665 #endif 666 667 /* 668 * :history command - print a history 669 */ 670 void 671 ex_history(exarg_T *eap) 672 { 673 histentry_T *hist; 674 int histype1 = HIST_CMD; 675 int histype2 = HIST_CMD; 676 int hisidx1 = 1; 677 int hisidx2 = -1; 678 int idx; 679 int i, j, k; 680 char_u *end; 681 char_u *arg = eap->arg; 682 683 if (hislen == 0) 684 { 685 msg(_("'history' option is zero")); 686 return; 687 } 688 689 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) 690 { 691 end = arg; 692 while (ASCII_ISALPHA(*end) 693 || vim_strchr((char_u *)":=@>/?", *end) != NULL) 694 end++; 695 i = *end; 696 *end = NUL; 697 histype1 = get_histtype(arg); 698 if (histype1 == -1) 699 { 700 if (STRNICMP(arg, "all", STRLEN(arg)) == 0) 701 { 702 histype1 = 0; 703 histype2 = HIST_COUNT-1; 704 } 705 else 706 { 707 *end = i; 708 semsg(_(e_trailing_arg), arg); 709 return; 710 } 711 } 712 else 713 histype2 = histype1; 714 *end = i; 715 } 716 else 717 end = arg; 718 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) 719 { 720 semsg(_(e_trailing_arg), end); 721 return; 722 } 723 724 for (; !got_int && histype1 <= histype2; ++histype1) 725 { 726 STRCPY(IObuff, "\n # "); 727 STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); 728 msg_puts_title((char *)IObuff); 729 idx = hisidx[histype1]; 730 hist = history[histype1]; 731 j = hisidx1; 732 k = hisidx2; 733 if (j < 0) 734 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; 735 if (k < 0) 736 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; 737 if (idx >= 0 && j <= k) 738 for (i = idx + 1; !got_int; ++i) 739 { 740 if (i == hislen) 741 i = 0; 742 if (hist[i].hisstr != NULL 743 && hist[i].hisnum >= j && hist[i].hisnum <= k) 744 { 745 msg_putchar('\n'); 746 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', 747 hist[i].hisnum); 748 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) 749 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), 750 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); 751 else 752 STRCAT(IObuff, hist[i].hisstr); 753 msg_outtrans(IObuff); 754 out_flush(); 755 } 756 if (i == idx) 757 break; 758 } 759 } 760 } 761