GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Version 3.4 · vim-scripts/mru.vim@c701834

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • plugin
    • mru.vim
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎plugin/mru.vim‎

Copy file name to clipboardExpand all lines: plugin/mru.vim
+109-52Lines changed: 109 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
" File: mru.vim
22
" 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
66
" Permission is hereby granted to use and distribute this code,
77
" with or without modifications, provided that this copyright
88
" notice is copied with it. Like anything else that's free,
@@ -97,7 +97,7 @@
9797
"
9898
" To display only files matching a pattern from the MRU list in the MRU
9999
" 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
101101
" command ":MRU vim". When you specify a partial file name and only one
102102
" matching filename is found, then the ":MRU" command will edit that file.
103103
"
@@ -280,6 +280,22 @@ if !exists('MRU_Max_Submenu_Entries')
280280
let MRU_Max_Submenu_Entries = 10
281281
endif
282282

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+
283299
" Control to temporarily lock the MRU list. Used to prevent files from
284300
" getting added to the MRU list when the ':vimgrep' command is executed.
285301
let s:mru_list_locked = 0
@@ -409,14 +425,23 @@ endfunction
409425
" MRU_Edit_File {{{1
410426
" Edit the specified file
411427
" 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
413429
" characters or not.
414430
function! s:MRU_Edit_File(filename, sanitized)
415431
if !a:sanitized
416432
let esc_fname = s:MRU_escape_filename(a:filename)
417433
else
418434
let esc_fname = a:filename
419435
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+
420445
" If the file is already open in one of the windows, jump to it
421446
let winnum = bufwinnr('^' . a:filename . '$')
422447
if winnum != -1
@@ -434,6 +459,42 @@ function! s:MRU_Edit_File(filename, sanitized)
434459
endif
435460
endfunction
436461

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+
437498
" MRU_Window_Edit_File {{{1
438499
" fname : Name of the file to edit. May specify single or multiple
439500
" files.
@@ -447,44 +508,25 @@ endfunction
447508
" useopen - If the file is already present in a window, then
448509
" jump to that window. Otherwise, open the file in
449510
" 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.
451513
" newtab - Open the file in a new tab. If the file is already
452514
" opened in a tab, then jump to that tab.
453515
function! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type)
454516
let esc_fname = s:MRU_escape_filename(a:fname)
455517

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)
488530
else
489531
" If the selected file is already open in one of the windows,
490532
" jump to it
@@ -551,10 +593,11 @@ endfunction
551593
"
552594
" 'opt' has two values separated by comma. The first value specifies how to
553595
" 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.
558601
" If multiple file names are selected using visual mode, then open multiple
559602
" files (either in split windows or tabs)
560603
function! s:MRU_Select_File_Cmd(opt) range
@@ -575,7 +618,10 @@ function! s:MRU_Select_File_Cmd(opt) range
575618
continue
576619
endif
577620

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)
579625

580626
if a:firstline != a:lastline
581627
" Opening multiple files
@@ -680,9 +726,13 @@ function! s:MRU_Open_Window(...)
680726
vnoremap <buffer> <silent> <CR>
681727
\ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
682728
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>
684730
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>
686736
nnoremap <buffer> <silent> t
687737
\ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR>
688738
vnoremap <buffer> <silent> t
@@ -700,7 +750,7 @@ function! s:MRU_Open_Window(...)
700750
" Display the MRU list
701751
if a:0 == 0
702752
" No search pattern specified. Display the complete list
703-
silent! 0put =s:MRU_files
753+
let m = copy(s:MRU_files)
704754
else
705755
" Display only the entries matching the specified pattern
706756
" First try using it as a literal pattern
@@ -709,9 +759,16 @@ function! s:MRU_Open_Window(...)
709759
" No match. Try using it as a regular expression
710760
let m = filter(copy(s:MRU_files), 'v:val =~# a:1')
711761
endif
712-
silent! 0put =m
713762
endif
714763

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+
715772
" Move the cursor to the beginning of the file
716773
normal! gg
717774

@@ -751,17 +808,17 @@ function! s:MRU_Cmd(pat)
751808

752809
" First use the specified string as a literal string and search for
753810
" 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)
755812
let m = filter(copy(s:MRU_files), 'stridx(v:val, a:pat) != -1')
756813
if len(m) > 0
757-
if len(m) == 1
814+
if len(m) == 1 && !g:MRU_Window_Open_Always
758815
call s:MRU_Edit_File(m[0], 0)
759816
return
760817
endif
761818

762819
" More than one file matches. Try find an accurate match
763820
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
765822
call s:MRU_Edit_File(new_m[0], 0)
766823
return
767824
endif
@@ -786,11 +843,11 @@ function! s:MRU_Cmd(pat)
786843

787844
" No filenames matching the specified pattern are found
788845
call s:MRU_Warn_Msg("MRU file list doesn't contain " .
789-
\ "files containing " . a:pat)
846+
\ "files matching " . a:pat)
790847
return
791848
endif
792849

793-
if len(m) == 1
850+
if len(m) == 1 && !g:MRU_Window_Open_Always
794851
call s:MRU_Edit_File(m[0], 0)
795852
return
796853
endif

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.