|
1 | 1 | " File: mru.vim |
2 | 2 | " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) |
3 | | -" Version: 3.0 |
4 | | -" Last Modified: January 12, 2008 |
| 3 | +" Version: 3.1 |
| 4 | +" Last Modified: February 17, 2008 |
5 | 5 | " |
6 | 6 | " Overview |
7 | 7 | " -------- |
|
19 | 19 | " |
20 | 20 | " Installation |
21 | 21 | " ------------ |
22 | | -" 1. Copy the mru.vim script to the $HOME/.vim/plugin or the |
23 | | -" $HOME/vimfiles/plugin or the $VIM/vimfiles directory. Refer to the |
24 | | -" ':help add-plugin', ':help add-global-plugin' and ':help runtimepath' |
25 | | -" topics for more details about Vim plugins. |
| 22 | +" 1. Copy the mru.vim file to one of the following directories: |
| 23 | +" |
| 24 | +" $HOME/.vim/plugin - Unix like systems |
| 25 | +" $HOME/vimfiles/plugin - MS-Windows |
| 26 | +" $VIM:vimfiles:plugin - Macintosh |
| 27 | +" $VIM/vimfiles/plugin - All |
| 28 | +" |
| 29 | +" Refer to the following Vim help topics for more information about Vim |
| 30 | +" plugins: |
| 31 | +" |
| 32 | +" :help add-plugin |
| 33 | +" :help add-global-plugin |
| 34 | +" :help runtimepath |
| 35 | +" |
26 | 36 | " 2. Set the MRU_File Vim variable in the .vimrc file to the location of a |
27 | 37 | " file to store the most recently edited file names. This step is needed |
28 | 38 | " only if you want to change the default MRU filename. |
|
31 | 41 | " In GUI Vim, you can use the 'File->Recent Files' menu to access the |
32 | 42 | " recently used files. |
33 | 43 | " |
| 44 | +" To uninstall this plugin, remove this file (mru.vim) from the |
| 45 | +" $HOME/.vim/plugin or $HOME/vimfiles/plugin or the $VIM/vimfile/plugin |
| 46 | +" directory. |
| 47 | +" |
34 | 48 | " Usage |
35 | 49 | " ----- |
36 | 50 | " You can use the ":MRU" command to list all the most recently edited file |
|
57 | 71 | " |
58 | 72 | " To open a file from the MRU window in a new tab, press the 't' key. If the |
59 | 73 | " file is already opened in a window in the current or in another tab, then |
60 | | -" the cursor is moved to that tab. Othewrise, a new tab is opened. |
| 74 | +" the cursor is moved to that tab. Otherwise, a new tab is opened. |
61 | 75 | " |
62 | 76 | " You can press the 'u' key in the MRU window to update the file list. This is |
63 | 77 | " useful if you keep the MRU window open always. |
|
68 | 82 | " To display only files matching a pattern from the MRU list in the MRU |
69 | 83 | " window, you can specify a pattern to the ":MRU" command. For example, to |
70 | 84 | " display only file names containing "vim" in them, you can use the following |
71 | | -" command ":MRU vim" |
| 85 | +" command ":MRU vim". When you specify a partial file name and only one |
| 86 | +" matching filename is found, then the ":MRU" command will edit that file. |
72 | 87 | " |
73 | | -" You can use the ":MRUedit" command to edit files from the MRU list. This |
74 | | -" command supports completion of file names from the MRU list. You can specify |
75 | | -" a file either by the name or by the location in the MRU list. |
| 88 | +" The ":MRU" command supports command-line completion of file names from |
| 89 | +" the MRU list. You can enter a partial file name and then press <Tab> |
| 90 | +" or <Ctrl-D> to complete or list all the matching file names. |
76 | 91 | " |
77 | 92 | " Whenever the MRU list changes, the MRU file is updated with the latest MRU |
78 | 93 | " list. When you have multiple instances of Vim running at the same time, the |
|
132 | 147 | " |
133 | 148 | " let MRU_Auto_Close = 0 |
134 | 149 | " |
| 150 | +" If you don't use the "File->Recent Files" menu and want to disable it, |
| 151 | +" then you can set the 'MRU_Add_Menu' variable to zero. By default, the |
| 152 | +" menu is enabled. |
| 153 | +" |
| 154 | +" let MRU_Add_Menu = 0 |
| 155 | +" |
135 | 156 | " ****************** Do not modify after this line ************************ |
136 | | -if exists('loaded_mru_autoload') |
| 157 | +if exists('loaded_mru') |
137 | 158 | finish |
138 | 159 | endif |
139 | | -let loaded_mru_autoload=1 |
| 160 | +let loaded_mru=1 |
140 | 161 |
|
141 | 162 | if v:version < 700 |
142 | 163 | finish |
@@ -178,6 +199,11 @@ if !exists('MRU_File') |
178 | 199 | endif |
179 | 200 | endif |
180 | 201 |
|
| 202 | +" Option for enabling or disabling the MRU menu |
| 203 | +if !exists('MRU_Add_Menu') |
| 204 | + let MRU_Add_Menu = 1 |
| 205 | +endif |
| 206 | + |
181 | 207 | " MRU_LoadList |
182 | 208 | " Load the latest MRU file list from the MRU file |
183 | 209 | function! s:MRU_LoadList() |
@@ -276,7 +302,7 @@ endfunction |
276 | 302 | " MRU_Edit_File |
277 | 303 | " Edit the specified file |
278 | 304 | function! s:MRU_Edit_File(filename) |
279 | | - let fname = escape(a:filename, ' %#') |
| 305 | + let fname = escape(a:filename, ' %#"') |
280 | 306 | " If the file is already open in one of the windows, jump to it |
281 | 307 | let winnum = bufwinnr('^' . fname . '$') |
282 | 308 | if winnum != -1 |
@@ -306,7 +332,7 @@ function! s:MRU_Window_Edit_File(win_opt) |
306 | 332 | return |
307 | 333 | endif |
308 | 334 |
|
309 | | - let fname = escape(fname, ' %#') |
| 335 | + let fname = escape(fname, ' %#"') |
310 | 336 |
|
311 | 337 | if a:win_opt == 'newwin' |
312 | 338 | " Edit the file in a new window |
@@ -349,7 +375,7 @@ function! s:MRU_Window_Edit_File(win_opt) |
349 | 375 | exe 'tabnext ' . i |
350 | 376 | else |
351 | 377 | " Open a new tab as the last tab page |
352 | | - exe '999tabnew ' . escape(fname, ' ') |
| 378 | + exe '999tabnew ' . fname |
353 | 379 | endif |
354 | 380 | endif |
355 | 381 |
|
@@ -409,6 +435,14 @@ function! s:MRU_Window_Edit_File(win_opt) |
409 | 435 | endif |
410 | 436 | endfunction |
411 | 437 |
|
| 438 | +" MRU_Warn_Msg |
| 439 | +" Display a warning message |
| 440 | +function! s:MRU_Warn_Msg(msg) |
| 441 | + echohl WarningMsg |
| 442 | + echo a:msg |
| 443 | + echohl None |
| 444 | +endfunction |
| 445 | + |
412 | 446 | " MRU_Open_Window |
413 | 447 | " Display the Most Recently Used file list in a temporary window. |
414 | 448 | function! s:MRU_Open_Window(...) |
@@ -418,7 +452,7 @@ function! s:MRU_Open_Window(...) |
418 | 452 |
|
419 | 453 | " Empty MRU list |
420 | 454 | if empty(s:MRU_files) |
421 | | - echohl WarningMsg | echo 'MRU file list is empty' | echohl None |
| 455 | + call s:MRU_Warn_Msg('MRU file list is empty') |
422 | 456 | return |
423 | 457 | endif |
424 | 458 |
|
@@ -521,9 +555,9 @@ function! s:MRU_Open_Window(...) |
521 | 555 | setlocal nomodifiable |
522 | 556 | endfunction |
523 | 557 |
|
524 | | -" MRU_Edit_Complete |
525 | | -" Command-line completion function used by :MRUedit command |
526 | | -function! s:MRU_Edit_Complete(ArgLead, CmdLine, CursorPos) |
| 558 | +" MRU_Complete |
| 559 | +" Command-line completion function used by :MRU command |
| 560 | +function! s:MRU_Complete(ArgLead, CmdLine, CursorPos) |
527 | 561 | if a:ArgLead == '' |
528 | 562 | " Return the list of MRU files |
529 | 563 | return s:MRU_files |
@@ -532,67 +566,79 @@ function! s:MRU_Edit_Complete(ArgLead, CmdLine, CursorPos) |
532 | 566 | endif |
533 | 567 | endfunction |
534 | 568 |
|
535 | | -" MRU_Edit_File_Cmd |
536 | | -" Function to handle the MRUedit command |
537 | | -function! s:MRU_Edit_File_Cmd(fspec) |
538 | | - if a:fspec == '' |
| 569 | +" MRU_Cmd |
| 570 | +" Function to handle the MRU command |
| 571 | +function! s:MRU_Cmd(pat) |
| 572 | + if a:pat == '' |
| 573 | + " No arguments specified. Open the MRU window |
| 574 | + call s:MRU_Open_Window() |
539 | 575 | return |
540 | 576 | endif |
541 | 577 |
|
542 | 578 | " Load the latest MRU file |
543 | 579 | call s:MRU_LoadList() |
544 | 580 |
|
545 | | - " User can specify either a number or a partial file name to |
546 | | - " edit a file from the MRU list |
547 | | - if a:fspec =~ '^\d\+$' |
548 | | - " A number is specified |
549 | | - let fnum = a:fspec |
550 | | - if fnum <= 0 || fnum > len(s:MRU_files) |
551 | | - echohl WarningMsg |
552 | | - echo 'Error: valid range of values is 1 - ' . len(s:MRU_files) |
553 | | - echohl None |
554 | | - return |
555 | | - endif |
| 581 | + " Empty MRU list |
| 582 | + if empty(s:MRU_files) |
| 583 | + call s:MRU_Warn_Msg('MRU file list is empty') |
| 584 | + return |
| 585 | + endif |
556 | 586 |
|
557 | | - " User index is 1 based, but the internal index is 0 based |
558 | | - let fnum -= 1 |
| 587 | + " First use the specified string as a literal string and search for |
| 588 | + " filenames containing the string. If only one filename is found, |
| 589 | + " then edit it. |
| 590 | + let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1') |
| 591 | + if len(m) == 1 |
| 592 | + call s:MRU_Edit_File(m[0]) |
| 593 | + return |
| 594 | + endif |
559 | 595 |
|
560 | | - let fname = s:MRU_files[fnum] |
561 | | - if fname != '' |
562 | | - call s:MRU_Edit_File(fname) |
563 | | - endif |
564 | | - else |
565 | | - " Locate the file name matching the supplied partial name |
| 596 | + " Use the specified string as a regular expression pattern and search |
| 597 | + " for filenames matching the pattern |
| 598 | + let m = filter(copy(s:MRU_files), 'v:val =~? a:pat') |
566 | 599 |
|
567 | | - " Escape backslash in the partial filename, otherwise regexp |
568 | | - " comparison will fail |
569 | | - let fpat = escape(a:fspec, '\') |
| 600 | + if len(m) == 0 |
| 601 | + " No filenames matching the specified pattern are found |
| 602 | + call s:MRU_Warn_Msg("MRU file list doesn't contain " . |
| 603 | + \ "files containing " . a:pat) |
| 604 | + return |
| 605 | + endif |
570 | 606 |
|
571 | | - let l = filter(copy(s:MRU_files), 'v:val =~ fpat') |
572 | | - if len(l) == 0 |
573 | | - echohl WarningMsg |
574 | | - echo 'Error: No matching file for ' . a:fspec |
575 | | - echohl None |
576 | | - return |
577 | | - endif |
| 607 | + if len(m) == 1 |
| 608 | + call s:MRU_Edit_File(m[0]) |
| 609 | + return |
| 610 | + endif |
578 | 611 |
|
579 | | - if len(l) > 1 |
580 | | - echohl WarningMsg |
581 | | - echo 'Error: More than one match for ' . a:fspec |
582 | | - echohl None |
583 | | - return |
584 | | - endif |
| 612 | + call s:MRU_Open_Window(a:pat) |
| 613 | +endfunction |
585 | 614 |
|
586 | | - let fname = l[0] |
| 615 | +function! s:MRU_add_files_to_menu(prefix, file_list) |
| 616 | + for fname in a:file_list |
| 617 | + " Escape special characters in the filename |
| 618 | + let esc_fname = escape(fnamemodify(fname, ':t'), ". \\|\t%#") |
587 | 619 |
|
588 | | - call s:MRU_Edit_File(fname) |
589 | | - endif |
| 620 | + " Truncate the directory name if it is long |
| 621 | + let dir_name = fnamemodify(fname, ':h') |
| 622 | + let len = strlen(dir_name) |
| 623 | + " Shorten long file names by adding only few characters from |
| 624 | + " the beginning and end. |
| 625 | + if len > 30 |
| 626 | + let dir_name = strpart(dir_name, 0, 10) . |
| 627 | + \ '...' . |
| 628 | + \ strpart(dir_name, len - 20) |
| 629 | + endif |
| 630 | + let esc_dir_name = escape(dir_name, ". \\|\t") |
| 631 | + |
| 632 | + exe 'anoremenu <silent> &File.Recent\ Files.' . a:prefix . esc_fname . |
| 633 | + \ '\ (' . esc_dir_name . ')' . |
| 634 | + \ " :call <SID>MRU_Edit_File('" . fname . "')<CR>" |
| 635 | + endfor |
590 | 636 | endfunction |
591 | 637 |
|
592 | 638 | " MRU_Refresh_Menu() |
593 | 639 | " Refresh the MRU menu |
594 | 640 | function! s:MRU_Refresh_Menu() |
595 | | - if !has('menu') |
| 641 | + if !has('menu') || !g:MRU_Add_Menu |
596 | 642 | " No support for menus |
597 | 643 | return |
598 | 644 | endif |
@@ -604,34 +650,32 @@ function! s:MRU_Refresh_Menu() |
604 | 650 | " Remove the MRU menu |
605 | 651 | " To retain the teared-off MRU menu, we need to add a dummy entry |
606 | 652 | silent! unmenu &File.Recent\ Files |
607 | | - noremenu &File.Recent\ Files.Dummy <Nop> |
| 653 | + " The menu priority of the File menu is 10. If the MRU plugin runs |
| 654 | + " first before menu.vim, the File menu order may not be correct. |
| 655 | + " So specify the priority of the File menu here. |
| 656 | + 10noremenu &File.Recent\ Files.Dummy <Nop> |
608 | 657 | silent! unmenu! &File.Recent\ Files |
609 | 658 |
|
610 | 659 | anoremenu <silent> &File.Recent\ Files.Refresh\ list |
611 | 660 | \ :call <SID>MRU_LoadList()<CR> |
612 | 661 | anoremenu File.Recent\ Files.-SEP1- : |
613 | 662 |
|
614 | 663 | " Add the filenames in the MRU list to the menu |
615 | | - for fname in s:MRU_files |
616 | | - " Escape special characters in the filename |
617 | | - let esc_fname = escape(fnamemodify(fname, ':t'), ". \\|\t%#") |
618 | | - |
619 | | - " Truncate the directory name if it is long |
620 | | - let dir_name = fnamemodify(fname, ':h') |
621 | | - let len = strlen(dir_name) |
622 | | - " Shorten long file names by adding only few characters from |
623 | | - " the beginning and end. |
624 | | - if len > 30 |
625 | | - let dir_name = strpart(dir_name, 0, 10) . |
626 | | - \ '...' . |
627 | | - \ strpart(dir_name, len - 20) |
628 | | - endif |
629 | | - let esc_dir_name = escape(dir_name, ". \\|\t") |
630 | | - |
631 | | - exe 'anoremenu <silent> &File.Recent\ Files.' . esc_fname . |
632 | | - \ '\ (' . esc_dir_name . ')' . |
633 | | - \ " :call <SID>MRU_Edit_File('" . fname . "')<CR>" |
634 | | - endfor |
| 664 | + let entry_cnt = len(s:MRU_files) |
| 665 | + if entry_cnt > 10 |
| 666 | + for start_idx in range(0, entry_cnt, 10) |
| 667 | + let last_idx = start_idx + 9 |
| 668 | + if last_idx >= entry_cnt |
| 669 | + let last_idx = entry_cnt - 1 |
| 670 | + endif |
| 671 | + let prefix = 'Files\ (' . (start_idx + 1) . '\.\.\.' . |
| 672 | + \ (last_idx + 1) . ').' |
| 673 | + call s:MRU_add_files_to_menu(prefix, |
| 674 | + \ s:MRU_files[start_idx : last_idx]) |
| 675 | + endfor |
| 676 | + else |
| 677 | + call s:MRU_add_files_to_menu('', s:MRU_files) |
| 678 | + endif |
635 | 679 |
|
636 | 680 | " Remove the dummy menu entry |
637 | 681 | unmenu &File.Recent\ Files.Dummy |
@@ -649,9 +693,8 @@ autocmd BufNewFile * call s:MRU_AddFile(expand('<abuf>')) |
649 | 693 | autocmd BufWritePost * call s:MRU_AddFile(expand('<abuf>')) |
650 | 694 |
|
651 | 695 | " Command to open the MRU window |
652 | | -command! -nargs=? MRU call s:MRU_Open_Window(<q-args>) |
653 | | -command! -nargs=1 -complete=customlist,s:MRU_Edit_Complete MRUedit |
654 | | - \ call s:MRU_Edit_File_Cmd(<q-args>) |
| 696 | +command! -nargs=? -complete=customlist,s:MRU_Complete MRU |
| 697 | + \ call s:MRU_Cmd(<q-args>) |
655 | 698 |
|
656 | 699 | " restore 'cpo' |
657 | 700 | let &cpo = s:cpo_save |
|