Browse Source

Add support for using command modifiers with the :MRU command and a file name

Yegappan Lakshmanan 4 years ago
parent
commit
6de2e9e5bf
3 changed files with 100 additions and 15 deletions
  1. 17 1
      doc/mru.txt
  2. 18 14
      plugin/mru.vim
  3. 65 0
      test/unit_tests.vim

+ 17 - 1
doc/mru.txt

@@ -2,7 +2,7 @@
 
 Author: Yegappan Lakshmanan  (yegappan AT yahoo DOT com)
 For Vim version 7.0 and above
-Last change: Feb 10, 2021
+Last change: August 13, 2021
 
 ==============================================================================
 						*mru-license*
@@ -191,6 +191,22 @@ or <Ctrl-D> to complete or list all the matching file names. Note that
 after typing the |:MRU| command, you have to enter a space before completing
 the file names with <Tab>.
 
+When the search pattern supplied to the |:MRU| command matches only one file,
+then the file will be opened in the current window if it contains an
+unmodified buffer. Otherwise it will be opend in a new window.  You can use
+command-line completion to select a file and directly open the file without
+opening the MRU window. If you are using Vim version 8.0 and above, you can
+use the command modifiers (|:leftabove|, |:rightbelow|, |:vertical|, |:tab|,
+etc.) to open the file in a horizontally or vertically split window or a tab
+page.  Example: >
+
+    :topleft MRU myfile.py		" horizontally split topmost window
+    :botright MRU myfile.py		" horizontally split bottommost window
+    :vertical MRU myfile.py		" vertically split window
+    :vertical topleft MRU myfile.py	" vertically split far-left window
+    :vertical botright MRU myfile.py	" vertically split far-right window
+    :tab MRU myfile.py			" new tab page
+<
 When a file supplied to the |:MRU| command is not present in the MRU list,
 but it is a readable file, then the file will be opened (even though it is
 not present in the MRU list). This is useful if you want to open a file

+ 18 - 14
plugin/mru.vim

@@ -1,7 +1,7 @@
 " File: mru.vim
 " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
-" Version: 3.10.1
-" Last Modified: Feb 13, 2021
+" Version: 3.10.2
+" Last Modified: August 13, 2021
 " Copyright: Copyright (C) 2003-2021 Yegappan Lakshmanan
 " License:   Permission is hereby granted to use and distribute this code,
 "            with or without modifications, provided that this copyright
@@ -269,8 +269,9 @@ endfunc
 "   filename - Name of the file to edit
 "   sanitized - Specifies whether the filename is already escaped for special
 "   characters or not.
+"   splitdir - command modifier for a split (topleft, belowright, etc.)
 " Used by the :MRU command and the "Recent Files" menu item
-func! s:MRU_Edit_File(filename, sanitized) abort
+func! s:MRU_Edit_File(filename, sanitized, splitdir) abort
   if !a:sanitized
     let esc_fname = s:MRU_escape_filename(a:filename)
   else
@@ -292,14 +293,17 @@ func! s:MRU_Edit_File(filename, sanitized) abort
       exe winnum . 'wincmd w'
     endif
   else
-    if !&hidden && (&modified || &buftype != '' || &previewwindow)
-      " Current buffer has unsaved changes or is a special buffer or is
-      " the preview window.  The 'hidden' option is also not set.
-      " So open the file in a new window.
+    if a:splitdir != '' || (!&hidden && (&modified || &buftype != ''
+	  \ || &previewwindow))
+      " If a split command modifier is specified, always open the file
+      " in a new window.
+      " Or if the current buffer has unsaved changes or is a special buffer or
+      " is the preview window.  The 'hidden' option is also not set.  So open
+      " the file in a new window.
       if bufexists(esc_fname)
-	exe 'sbuffer ' . esc_fname
+	exe a:splitdir .. ' sbuffer ' . esc_fname
       else
-	exe 'split ' . esc_fname
+	exe a:splitdir .. ' split ' . esc_fname
       endif
     else
       " The current file can be replaced with the selected file.
@@ -789,14 +793,14 @@ func! s:MRU_Cmd(pat, splitdir, winsz) abort
   endif
   if len(m) > 0
     if len(m) == 1 && !g:MRU_Window_Open_Always
-      call s:MRU_Edit_File(m[0], 0)
+      call s:MRU_Edit_File(m[0], 0, a:splitdir)
       return
     endif
 
     " More than one file matches. Try to find an accurate match
     let new_m = filter(m, 'v:val ==# a:pat')
     if len(new_m) == 1 && !g:MRU_Window_Open_Always
-      call s:MRU_Edit_File(new_m[0], 0)
+      call s:MRU_Edit_File(new_m[0], 0, a:splitdir)
       return
     endif
 
@@ -814,7 +818,7 @@ func! s:MRU_Cmd(pat, splitdir, winsz) abort
     " If an existing file (not present in the MRU list) is specified,
     " then open the file.
     if filereadable(a:pat)
-      call s:MRU_Edit_File(a:pat, 0)
+      call s:MRU_Edit_File(a:pat, 0, a:splitdir)
       return
     endif
 
@@ -825,7 +829,7 @@ func! s:MRU_Cmd(pat, splitdir, winsz) abort
   endif
 
   if len(m) == 1 && !g:MRU_Window_Open_Always
-    call s:MRU_Edit_File(m[0], 0)
+    call s:MRU_Edit_File(m[0], 0, a:splitdir)
     return
   endif
 
@@ -871,7 +875,7 @@ func! s:MRU_add_files_to_menu(prefix, file_list) abort
 	  \ '\ (' . esc_dir_name . ')'
     let esc_mfname = s:MRU_escape_filename(fname)
     exe 'anoremenu <silent> ' . menu_path .
-	  \ " :call <SID>MRU_Edit_File('" . esc_mfname . "', 1)<CR>"
+	  \ " :call <SID>MRU_Edit_File('" . esc_mfname . "', 1, '')<CR>"
     exe 'tmenu ' . menu_path . ' Edit file ' . esc_mfname
   endfor
 endfunc

+ 65 - 0
test/unit_tests.vim

@@ -1496,6 +1496,71 @@ func Test_52()
   endif
 endfunc
 
+" ==========================================================================
+" Test53
+" Test for using a command modifier when directly opening a file using the
+" MRU command.
+" ==========================================================================
+func Test_53()
+  let test_name = 'test53'
+  %bw!
+  topleft MRU file2.txt
+  if winnr('$') == 2 && winnr() == 1 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
+    wincmd j
+    if winnr() == 2
+      call LogResult(test_name, 'pass')
+    else
+      call LogResult(test_name, 'FAIL 1')
+    endif
+  else
+    call LogResult(test_name, 'FAIL 2')
+  endif
+  %bw
+  belowright MRU file2.txt
+  if winnr('$') == 2 && winnr() == 2 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
+    wincmd k
+    if winnr() == 1
+      call LogResult(test_name, 'pass')
+    else
+      call LogResult(test_name, 'FAIL 3')
+    endif
+  else
+    call LogResult(test_name, 'FAIL 4')
+  endif
+  %bw
+  vertical topleft MRU file2.txt
+  if winnr('$') == 2 && winnr() == 1 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
+    wincmd l
+    if winnr() == 2
+      call LogResult(test_name, 'pass')
+    else
+      call LogResult(test_name, 'FAIL 5')
+    endif
+  else
+    call LogResult(test_name, 'FAIL 6')
+  endif
+  %bw
+  vertical belowright MRU file2.txt
+  if winnr('$') == 2 && winnr() == 2 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
+    wincmd h
+    if winnr() == 1
+      call LogResult(test_name, 'pass')
+    else
+      call LogResult(test_name, 'FAIL 7')
+    endif
+  else
+    call LogResult(test_name, 'FAIL 8')
+  endif
+  %bw
+  tab MRU file2.txt
+  if tabpagenr() == 2 && fnamemodify(@%, ':p:t') ==# 'file2.txt'
+    call LogResult(test_name, 'pass')
+  else
+    call LogResult(test_name, 'FAIL 9')
+  endif
+  %bw
+endfunc
+
 " ==========================================================================
 
 " Create the files used by the tests