|
1 | 1 | " File: mru.vim |
2 | 2 | " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) |
3 | | -" Version: 3.3 |
4 | | -" Last Modified: December 18, 2009 |
5 | | -" Copyright: Copyright (C) 2003-2009 Yegappan Lakshmanan |
| 3 | +" Version: 3.4 |
| 4 | +" Last Modified: April 13, 2012 |
| 5 | +" Copyright: Copyright (C) 2003-2012 Yegappan Lakshmanan |
6 | 6 | " Permission is hereby granted to use and distribute this code, |
7 | 7 | " with or without modifications, provided that this copyright |
8 | 8 | " notice is copied with it. Like anything else that's free, |
|
97 | 97 | " |
98 | 98 | " To display only files matching a pattern from the MRU list in the MRU |
99 | 99 | " window, you can specify a pattern to the ":MRU" command. For example, to |
100 | | -" display only file names containing "vim" in them, you can use the following |
| 100 | +" display only file names matching "vim" in them, you can use the following |
101 | 101 | " command ":MRU vim". When you specify a partial file name and only one |
102 | 102 | " matching filename is found, then the ":MRU" command will edit that file. |
103 | 103 | " |
@@ -280,6 +280,22 @@ if !exists('MRU_Max_Submenu_Entries') |
280 | 280 | let MRU_Max_Submenu_Entries = 10 |
281 | 281 | endif |
282 | 282 |
|
| 283 | +" When only a single matching filename is found in the MRU list, the following |
| 284 | +" option controls whether the file name is displayed in the MRU window or the |
| 285 | +" file is directly opened. When this variable is set to 0 and a single |
| 286 | +" matching file name is found, then the file is directly opened. |
| 287 | +if !exists('MRU_Window_Open_Always') |
| 288 | + let MRU_Window_Open_Always = 0 |
| 289 | +endif |
| 290 | + |
| 291 | +" When opening a file from the MRU list, the file is opened in the current |
| 292 | +" tab. If the selected file has to be opened in a tab always, then set the |
| 293 | +" following variable to 1. If the file is already opened in a tab, then the |
| 294 | +" cursor will be moved to that tab. |
| 295 | +if !exists('MRU_Open_File_Use_Tabs') |
| 296 | + let MRU_Open_File_Use_Tabs = 0 |
| 297 | +endif |
| 298 | + |
283 | 299 | " Control to temporarily lock the MRU list. Used to prevent files from |
284 | 300 | " getting added to the MRU list when the ':vimgrep' command is executed. |
285 | 301 | let s:mru_list_locked = 0 |
@@ -409,14 +425,23 @@ endfunction |
409 | 425 | " MRU_Edit_File {{{1 |
410 | 426 | " Edit the specified file |
411 | 427 | " filename - Name of the file to edit |
412 | | -" sanitized - Specifies whether the filename is already escape for special |
| 428 | +" sanitized - Specifies whether the filename is already escaped for special |
413 | 429 | " characters or not. |
414 | 430 | function! s:MRU_Edit_File(filename, sanitized) |
415 | 431 | if !a:sanitized |
416 | 432 | let esc_fname = s:MRU_escape_filename(a:filename) |
417 | 433 | else |
418 | 434 | let esc_fname = a:filename |
419 | 435 | endif |
| 436 | + |
| 437 | + " If the user wants to always open the file in a tab, then open the file |
| 438 | + " in a tab. If it is already opened in a tab, then the cursor will be |
| 439 | + " moved to that tab. |
| 440 | + if g:MRU_Open_File_Use_Tabs |
| 441 | + call s:MRU_Open_File_In_Tab(a:filename, esc_fname) |
| 442 | + return |
| 443 | + endif |
| 444 | + |
420 | 445 | " If the file is already open in one of the windows, jump to it |
421 | 446 | let winnum = bufwinnr('^' . a:filename . '$') |
422 | 447 | if winnum != -1 |
@@ -434,6 +459,42 @@ function! s:MRU_Edit_File(filename, sanitized) |
434 | 459 | endif |
435 | 460 | endfunction |
436 | 461 |
|
| 462 | +" MRU_Open_File_In_Tab |
| 463 | +" Open a file in a tab. If the file is already opened in a tab, jump to the |
| 464 | +" tab. Otherwise, create a new tab and open the file. |
| 465 | +" fname : Name of the file to open |
| 466 | +" esc_fname : File name with special characters escaped |
| 467 | +function! s:MRU_Open_File_In_Tab(fname, esc_fname) |
| 468 | + " If the selected file is already open in the current tab or in |
| 469 | + " another tab, jump to it. Otherwise open it in a new tab |
| 470 | + if bufwinnr('^' . a:fname . '$') == -1 |
| 471 | + let tabnum = -1 |
| 472 | + let i = 1 |
| 473 | + let bnum = bufnr('^' . a:fname . '$') |
| 474 | + while i <= tabpagenr('$') |
| 475 | + if index(tabpagebuflist(i), bnum) != -1 |
| 476 | + let tabnum = i |
| 477 | + break |
| 478 | + endif |
| 479 | + let i += 1 |
| 480 | + endwhile |
| 481 | + |
| 482 | + if tabnum != -1 |
| 483 | + " Goto the tab containing the file |
| 484 | + exe 'tabnext ' . i |
| 485 | + else |
| 486 | + " Open a new tab as the last tab page |
| 487 | + exe '999tabnew ' . a:esc_fname |
| 488 | + endif |
| 489 | + endif |
| 490 | + |
| 491 | + " Jump to the window containing the file |
| 492 | + let winnum = bufwinnr('^' . a:fname . '$') |
| 493 | + if winnum != winnr() |
| 494 | + exe winnum . 'wincmd w' |
| 495 | + endif |
| 496 | +endfunction |
| 497 | + |
437 | 498 | " MRU_Window_Edit_File {{{1 |
438 | 499 | " fname : Name of the file to edit. May specify single or multiple |
439 | 500 | " files. |
@@ -447,44 +508,25 @@ endfunction |
447 | 508 | " useopen - If the file is already present in a window, then |
448 | 509 | " jump to that window. Otherwise, open the file in |
449 | 510 | " the previous window. |
450 | | -" newwin - Open the file in a new window. |
| 511 | +" newwin_horiz - Open the file in a new horizontal window. |
| 512 | +" newwin_vert - Open the file in a new vertical window. |
451 | 513 | " newtab - Open the file in a new tab. If the file is already |
452 | 514 | " opened in a tab, then jump to that tab. |
453 | 515 | function! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type) |
454 | 516 | let esc_fname = s:MRU_escape_filename(a:fname) |
455 | 517 |
|
456 | | - if a:open_type == 'newwin' |
457 | | - " Edit the file in a new window |
458 | | - exe 'leftabove new ' . esc_fname |
459 | | - elseif a:open_type == 'newtab' |
460 | | - " If the selected file is already open in the current tab or in |
461 | | - " another tab, jump to it. Otherwise open it in a new tab |
462 | | - if bufwinnr('^' . a:fname . '$') == -1 |
463 | | - let tabnum = -1 |
464 | | - let i = 1 |
465 | | - let bnum = bufnr('^' . a:fname . '$') |
466 | | - while i <= tabpagenr('$') |
467 | | - if index(tabpagebuflist(i), bnum) != -1 |
468 | | - let tabnum = i |
469 | | - break |
470 | | - endif |
471 | | - let i += 1 |
472 | | - endwhile |
473 | | - |
474 | | - if tabnum != -1 |
475 | | - " Goto the tab containing the file |
476 | | - exe 'tabnext ' . i |
477 | | - else |
478 | | - " Open a new tab as the last tab page |
479 | | - exe '999tabnew ' . esc_fname |
480 | | - endif |
481 | | - endif |
482 | | - |
483 | | - " Jump to the window containing the file |
484 | | - let winnum = bufwinnr('^' . a:fname . '$') |
485 | | - if winnum != winnr() |
486 | | - exe winnum . 'wincmd w' |
487 | | - endif |
| 518 | + if a:open_type == 'newwin_horiz' |
| 519 | + " Edit the file in a new horizontally split window above the previous |
| 520 | + " window |
| 521 | + wincmd p |
| 522 | + exe 'belowright new ' . esc_fname |
| 523 | + elseif a:open_type == 'newwin_vert' |
| 524 | + " Edit the file in a new vertically split window above the previous |
| 525 | + " window |
| 526 | + wincmd p |
| 527 | + exe 'belowright vnew ' . esc_fname |
| 528 | + elseif a:open_type == 'newtab' || g:MRU_Open_File_Use_Tabs |
| 529 | + call s:MRU_Open_File_In_Tab(a:fname, esc_fname) |
488 | 530 | else |
489 | 531 | " If the selected file is already open in one of the windows, |
490 | 532 | " jump to it |
@@ -551,10 +593,11 @@ endfunction |
551 | 593 | " |
552 | 594 | " 'opt' has two values separated by comma. The first value specifies how to |
553 | 595 | " edit the file and can be either 'edit' or 'view'. The second value |
554 | | -" specifies where to open the file. It can be 'useopen' to open file in the |
555 | | -" previous window. It can be 'newwin' to open the file in a new window. It |
556 | | -" can be 'newtab' to open the file in a new tab. |
557 | | -" |
| 596 | +" specifies where to open the file. It can take one of the following values: |
| 597 | +" 'useopen' to open file in the previous window |
| 598 | +" 'newwin_horiz' to open the file in a new horizontal split window |
| 599 | +" 'newwin_vert' to open the file in a new vertical split window. |
| 600 | +" 'newtab' to open the file in a new tab. |
558 | 601 | " If multiple file names are selected using visual mode, then open multiple |
559 | 602 | " files (either in split windows or tabs) |
560 | 603 | function! s:MRU_Select_File_Cmd(opt) range |
@@ -575,7 +618,10 @@ function! s:MRU_Select_File_Cmd(opt) range |
575 | 618 | continue |
576 | 619 | endif |
577 | 620 |
|
578 | | - call s:MRU_Window_Edit_File(f, multi, edit_type, open_type) |
| 621 | + " The text in the MRU window contains the filename in parenthesis |
| 622 | + let file = matchstr(f, '(\zs.*\ze)') |
| 623 | + |
| 624 | + call s:MRU_Window_Edit_File(file, multi, edit_type, open_type) |
579 | 625 |
|
580 | 626 | if a:firstline != a:lastline |
581 | 627 | " Opening multiple files |
@@ -680,9 +726,13 @@ function! s:MRU_Open_Window(...) |
680 | 726 | vnoremap <buffer> <silent> <CR> |
681 | 727 | \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR> |
682 | 728 | nnoremap <buffer> <silent> o |
683 | | - \ :call <SID>MRU_Select_File_Cmd('edit,newwin')<CR> |
| 729 | + \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR> |
684 | 730 | vnoremap <buffer> <silent> o |
685 | | - \ :call <SID>MRU_Select_File_Cmd('edit,newwin')<CR> |
| 731 | + \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR> |
| 732 | + nnoremap <buffer> <silent> O |
| 733 | + \ :call <SID>MRU_Select_File_Cmd('edit,newwin_vert')<CR> |
| 734 | + vnoremap <buffer> <silent> O |
| 735 | + \ :call <SID>MRU_Select_File_Cmd('edit,newwin_vert')<CR> |
686 | 736 | nnoremap <buffer> <silent> t |
687 | 737 | \ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR> |
688 | 738 | vnoremap <buffer> <silent> t |
@@ -700,7 +750,7 @@ function! s:MRU_Open_Window(...) |
700 | 750 | " Display the MRU list |
701 | 751 | if a:0 == 0 |
702 | 752 | " No search pattern specified. Display the complete list |
703 | | - silent! 0put =s:MRU_files |
| 753 | + let m = copy(s:MRU_files) |
704 | 754 | else |
705 | 755 | " Display only the entries matching the specified pattern |
706 | 756 | " First try using it as a literal pattern |
@@ -709,9 +759,16 @@ function! s:MRU_Open_Window(...) |
709 | 759 | " No match. Try using it as a regular expression |
710 | 760 | let m = filter(copy(s:MRU_files), 'v:val =~# a:1') |
711 | 761 | endif |
712 | | - silent! 0put =m |
713 | 762 | endif |
714 | 763 |
|
| 764 | + " Get the tail part of the file name (without the directory) and display |
| 765 | + " it along with the full path |
| 766 | + let output = map(m, 'fnamemodify(v:val, ":t") . " (" . v:val . ")"') |
| 767 | + silent! 0put =output |
| 768 | + |
| 769 | + " Delete the empty line at the end of the buffer |
| 770 | + $delete |
| 771 | + |
715 | 772 | " Move the cursor to the beginning of the file |
716 | 773 | normal! gg |
717 | 774 |
|
@@ -751,17 +808,17 @@ function! s:MRU_Cmd(pat) |
751 | 808 |
|
752 | 809 | " First use the specified string as a literal string and search for |
753 | 810 | " filenames containing the string. If only one filename is found, |
754 | | - " then edit it. |
| 811 | + " then edit it (unless the user wants to open the MRU window always) |
755 | 812 | let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1') |
756 | 813 | if len(m) > 0 |
757 | | - if len(m) == 1 |
| 814 | + if len(m) == 1 && !g:MRU_Window_Open_Always |
758 | 815 | call s:MRU_Edit_File(m[0], 0) |
759 | 816 | return |
760 | 817 | endif |
761 | 818 |
|
762 | 819 | " More than one file matches. Try find an accurate match |
763 | 820 | let new_m = filter(m, 'v:val ==# a:pat') |
764 | | - if len(new_m) == 1 |
| 821 | + if len(new_m) == 1 && !g:MRU_Window_Open_Always |
765 | 822 | call s:MRU_Edit_File(new_m[0], 0) |
766 | 823 | return |
767 | 824 | endif |
@@ -786,11 +843,11 @@ function! s:MRU_Cmd(pat) |
786 | 843 |
|
787 | 844 | " No filenames matching the specified pattern are found |
788 | 845 | call s:MRU_Warn_Msg("MRU file list doesn't contain " . |
789 | | - \ "files containing " . a:pat) |
| 846 | + \ "files matching " . a:pat) |
790 | 847 | return |
791 | 848 | endif |
792 | 849 |
|
793 | | - if len(m) == 1 |
| 850 | + if len(m) == 1 && !g:MRU_Window_Open_Always |
794 | 851 | call s:MRU_Edit_File(m[0], 0) |
795 | 852 | return |
796 | 853 | endif |
|