Browse Source

vim: MRU++

master
Maxim Likhachev 11 years ago
parent
commit
1b0fad0eb4
  1. 114
      etc/soft/vim/vim/+plugins/MRU/plugin/mru.vim

114
etc/soft/vim/vim/+plugins/MRU/plugin/mru.vim

@ -1,14 +1,14 @@
" File: mru.vim " File: mru.vim
" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) " Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com)
" Version: 3.5 " Version: 3.8
" Last Modified: May 8, 2013 " Last Modified: March 5, 2014
" Copyright: Copyright (C) 2003-2013 Yegappan Lakshmanan " Copyright: Copyright (C) 2003-2014 Yegappan Lakshmanan
" Permission is hereby granted to use and distribute this code, " License: Permission is hereby granted to use and distribute this code,
" with or without modifications, provided that this copyright " with or without modifications, provided that this copyright
" notice is copied with it. Like anything else that's free, " notice is copied with it. Like anything else that's free,
" mru.vim is provided *as is* and comes with no warranty of any " mru.vim is provided *as is* and comes with no warranty of any
" kind, either expressed or implied. In no event will the copyright " kind, either expressed or implied. In no event will the copyright
" holder be liable for any damamges resulting from the use of this " holder be liable for any damages resulting from the use of this
" software. " software.
" "
" Overview " Overview
@ -25,6 +25,10 @@
" The recently used filenames are stored in a file specified by the Vim " The recently used filenames are stored in a file specified by the Vim
" MRU_File variable. " MRU_File variable.
" "
" The Github repository for the MRU plugin is available at:
"
" http://github.com/yegappan/mru
"
" Installation " Installation
" ------------ " ------------
" 1. Copy the mru.vim file to one of the following directories: " 1. Copy the mru.vim file to one of the following directories:
@ -75,7 +79,8 @@
" some other plugin, then the file is opened in a new window. " some other plugin, then the file is opened in a new window.
" "
" You can press the 'o' key to open the file name under the cursor in the " You can press the 'o' key to open the file name under the cursor in the
" MRU window in a new window. " MRU window in a new window. You can also press <Shift-Enter> instead of 'o'
" to open the file in a new window.
" "
" To open a file from the MRU window in read-only mode (view), press the 'v' " To open a file from the MRU window in read-only mode (view), press the 'v'
" key. " key.
@ -85,15 +90,16 @@
" the cursor is moved to that tab. Otherwise, a new tab is opened. " the cursor is moved to that tab. Otherwise, a new tab is opened.
" "
" You can open multiple files from the MRU window by specifying a count before " You can open multiple files from the MRU window by specifying a count before
" pressing '<Enter>' or 'v' or 'o' or 't'. You can also visually select " pressing '<Enter>' or 'v' or 'o' or 't'. You can also visually (using
" multiple filenames and invoke the commands to open the files. Each selected " linewise visual mode) select multiple filenames and invoke the commands to
" file will be opened in a separate window or tab. " open the files. Each selected file will be opened in a separate window or
" tab.
" "
" You can press the 'u' key in the MRU window to update the file list. This is " You can press the 'u' key in the MRU window to update the file list. This is
" useful if you keep the MRU window open always. " useful if you keep the MRU window open always.
" "
" You can close the MRU window by pressing the 'q' key or using one of the Vim " You can close the MRU window by pressing the 'q' key or the <Esc> key or
" window commands. " using one of the Vim window commands.
" "
" To display only files matching a pattern from the MRU list in the MRU " To display only files matching a pattern from the MRU list in the MRU
" window, you can specify a pattern to the ":MRU" command. For example, to " window, you can specify a pattern to the ":MRU" command. For example, to
@ -118,6 +124,16 @@
" list. When you have multiple instances of Vim running at the same time, the " list. When you have multiple instances of Vim running at the same time, the
" latest MRU list will show up in all the instances of Vim. " latest MRU list will show up in all the instances of Vim.
" "
" The MRUFilename syntax group is used to highlight the file names in the MRU
" window. By default, this syntax group is linked to the Identifier highlight
" group. You can change the highlight group by adding the following line in
" your .vimrc:
"
" highlight link MRUFileName LineNr
"
" The MRU buffer uses the 'mru file type. You can use this file type to add
" custom auto commands, syntax highlighting, etc.
"
" Configuration " Configuration
" ------------- " -------------
" By changing the following variables you can configure the behavior of this " By changing the following variables you can configure the behavior of this
@ -203,6 +219,16 @@
" "
" let MRU_Max_Submenu_Entries = 15 " let MRU_Max_Submenu_Entries = 15
" "
" In the MRU window, the filenames are displayed in two parts. The first part
" contains the file name without the path and the second part contains the
" full path to the file in parenthesis. This format is controlled by the
" MRU_Filename_Format variable. If you prefer to change this to some other
" format, then you can modify the MRU_Filename_Format variable. For example,
" to display the full path without splitting it, you can set this variable
" as shown below:
"
" let MRU_Filename_Format={'formatter':'v:val', 'parser':'.*'}
"
" ****************** Do not modify after this line ************************ " ****************** Do not modify after this line ************************
if exists('loaded_mru') if exists('loaded_mru')
finish finish
@ -296,6 +322,19 @@ if !exists('MRU_Open_File_Use_Tabs')
let MRU_Open_File_Use_Tabs = 0 let MRU_Open_File_Use_Tabs = 0
endif endif
" Format of the file names displayed in the MRU window.
" The default is to display the filename followed by the complete path to the
" file in parenthesis. This variable controls the expressions used to format
" and parse the path. This can be changed to display the filenames in a
" different format. The 'formatter' specifies how to split/format the filename
" and 'parser' specifies how to read the filename back.
if !exists('MRU_Filename_Format')
let MRU_Filename_Format = {
\ 'formatter': 'fnamemodify(v:val, ":t") . " (" . v:val . ")"',
\ 'parser': '(\zs.*\ze)'
\}
endif
" Control to temporarily lock the MRU list. Used to prevent files from " Control to temporarily lock the MRU list. Used to prevent files from
" getting added to the MRU list when the ':vimgrep' command is executed. " getting added to the MRU list when the ':vimgrep' command is executed.
let s:mru_list_locked = 0 let s:mru_list_locked = 0
@ -419,7 +458,11 @@ endfunction
" that should be escaped (for security reasons) " that should be escaped (for security reasons)
let s:esc_filename_chars = ' *?[{`$%#"|!<>();&' . "'\t\n" let s:esc_filename_chars = ' *?[{`$%#"|!<>();&' . "'\t\n"
function! s:MRU_escape_filename(fname) function! s:MRU_escape_filename(fname)
if exists("*fnameescape")
return fnameescape(a:fname)
else
return escape(a:fname, s:esc_filename_chars) return escape(a:fname, s:esc_filename_chars)
endif
endfunction endfunction
" MRU_Edit_File {{{1 " MRU_Edit_File {{{1
@ -503,8 +546,7 @@ endfunction
" 'edit' - Edit the file as a regular file " 'edit' - Edit the file as a regular file
" multi : Specifies whether a single file or multiple files need to be " multi : Specifies whether a single file or multiple files need to be
" opened. " opened.
" open_type : Specifies where to open the file. Can be one of 'useopen' or " open_type : Specifies where to open the file.
" 'newwin' or 'newtab'.
" useopen - If the file is already present in a window, then " useopen - If the file is already present in a window, then
" jump to that window. Otherwise, open the file in " jump to that window. Otherwise, open the file in
" the previous window. " the previous window.
@ -512,21 +554,25 @@ endfunction
" newwin_vert - Open the file in a new vertical window. " newwin_vert - Open the file in a new vertical window.
" newtab - Open the file in a new tab. If the file is already " newtab - Open the file in a new tab. If the file is already
" opened in a tab, then jump to that tab. " opened in a tab, then jump to that tab.
" preview - Open the file in the preview window
function! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type) function! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type)
let esc_fname = s:MRU_escape_filename(a:fname) let esc_fname = s:MRU_escape_filename(a:fname)
if a:open_type == 'newwin_horiz' if a:open_type ==# 'newwin_horiz'
" Edit the file in a new horizontally split window above the previous " Edit the file in a new horizontally split window above the previous
" window " window
wincmd p wincmd p
exe 'belowright new ' . esc_fname exe 'belowright new ' . esc_fname
elseif a:open_type == 'newwin_vert' elseif a:open_type ==# 'newwin_vert'
" Edit the file in a new vertically split window above the previous " Edit the file in a new vertically split window above the previous
" window " window
wincmd p wincmd p
exe 'belowright vnew ' . esc_fname exe 'belowright vnew ' . esc_fname
elseif a:open_type == 'newtab' || g:MRU_Open_File_Use_Tabs elseif a:open_type ==# 'newtab' || g:MRU_Open_File_Use_Tabs
call s:MRU_Open_File_In_Tab(a:fname, esc_fname) call s:MRU_Open_File_In_Tab(a:fname, esc_fname)
elseif a:open_type ==# 'preview'
" Edit the file in the preview window
exe 'topleft pedit ' . esc_fname
else else
" If the selected file is already open in one of the windows, " If the selected file is already open in one of the windows,
" jump to it " jump to it
@ -572,13 +618,13 @@ function! s:MRU_Window_Edit_File(fname, multi, edit_type, open_type)
if split_window if split_window
" Current buffer has unsaved changes or is a special buffer or " Current buffer has unsaved changes or is a special buffer or
" is the preview window. So open the file in a new window " is the preview window. So open the file in a new window
if a:edit_type == 'edit' if a:edit_type ==# 'edit'
exe 'split ' . esc_fname exe 'split ' . esc_fname
else else
exe 'sview ' . esc_fname exe 'sview ' . esc_fname
endif endif
else else
if a:edit_type == 'edit' if a:edit_type ==# 'edit'
exe 'edit ' . esc_fname exe 'edit ' . esc_fname
else else
exe 'view ' . esc_fname exe 'view ' . esc_fname
@ -619,7 +665,7 @@ function! s:MRU_Select_File_Cmd(opt) range
endif endif
" The text in the MRU window contains the filename in parenthesis " The text in the MRU window contains the filename in parenthesis
let file = matchstr(f, '(\zs.*\ze)') let file = matchstr(f, g:MRU_Filename_Format.parser)
call s:MRU_Window_Edit_File(file, multi, edit_type, open_type) call s:MRU_Window_Edit_File(file, multi, edit_type, open_type)
@ -668,8 +714,6 @@ function! s:MRU_Open_Window(...)
exe winnum . 'wincmd w' exe winnum . 'wincmd w'
endif endif
setlocal modifiable
" Delete the contents of the buffer to the black-hole register " Delete the contents of the buffer to the black-hole register
silent! %delete _ silent! %delete _
else else
@ -707,12 +751,17 @@ function! s:MRU_Open_Window(...)
endif endif
endif endif
setlocal modifiable
" Mark the buffer as scratch " Mark the buffer as scratch
setlocal buftype=nofile setlocal buftype=nofile
setlocal bufhidden=delete setlocal bufhidden=delete
setlocal noswapfile setlocal noswapfile
setlocal nowrap setlocal nowrap
setlocal nobuflisted setlocal nobuflisted
" Set the 'filetype' to 'mru'. This allows the user to apply custom
" syntax highlighting or other changes to the MRU bufer.
setlocal filetype=mru
" Use fixed height for the MRU window " Use fixed height for the MRU window
setlocal winfixheight setlocal winfixheight
@ -729,6 +778,10 @@ function! s:MRU_Open_Window(...)
\ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR> \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
vnoremap <buffer> <silent> o vnoremap <buffer> <silent> o
\ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR> \ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
nnoremap <buffer> <silent> <S-CR>
\ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
vnoremap <buffer> <silent> <S-CR>
\ :call <SID>MRU_Select_File_Cmd('edit,newwin_horiz')<CR>
nnoremap <buffer> <silent> O nnoremap <buffer> <silent> O
\ :call <SID>MRU_Select_File_Cmd('edit,newwin_vert')<CR> \ :call <SID>MRU_Select_File_Cmd('edit,newwin_vert')<CR>
vnoremap <buffer> <silent> O vnoremap <buffer> <silent> O
@ -739,10 +792,19 @@ function! s:MRU_Open_Window(...)
\ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR> \ :call <SID>MRU_Select_File_Cmd('edit,newtab')<CR>
nnoremap <buffer> <silent> v nnoremap <buffer> <silent> v
\ :call <SID>MRU_Select_File_Cmd('view,useopen')<CR> \ :call <SID>MRU_Select_File_Cmd('view,useopen')<CR>
nnoremap <buffer> <silent> p
\ :call <SID>MRU_Select_File_Cmd('view,preview')<CR>
vnoremap <buffer> <silent> p
\ :<C-u>if line("'<") == line("'>")<Bar>
\ call <SID>MRU_Select_File_Cmd('open,preview')<Bar>
\ else<Bar>
\ echoerr "Only a single file can be previewed"<Bar>
\ endif<CR>
nnoremap <buffer> <silent> u :MRU<CR> nnoremap <buffer> <silent> u :MRU<CR>
nnoremap <buffer> <silent> <2-LeftMouse> nnoremap <buffer> <silent> <2-LeftMouse>
\ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR> \ :call <SID>MRU_Select_File_Cmd('edit,useopen')<CR>
nnoremap <buffer> <silent> q :close<CR> nnoremap <buffer> <silent> q :close<CR>
nnoremap <buffer> <silent> <Esc> :close<CR>
" Restore the previous cpoptions settings " Restore the previous cpoptions settings
let &cpoptions = old_cpoptions let &cpoptions = old_cpoptions
@ -762,16 +824,20 @@ function! s:MRU_Open_Window(...)
endif endif
" Get the tail part of the file name (without the directory) and display " Get the tail part of the file name (without the directory) and display
" it along with the full path " it along with the full path in parenthesis.
let output = map(m, 'fnamemodify(v:val, ":t") . " (" . v:val . ")"') let output = map(m, g:MRU_Filename_Format.formatter)
silent! 0put =output silent! 0put =output
" Delete the empty line at the end of the buffer " Delete the empty line at the end of the buffer
$delete silent! $delete _
" Move the cursor to the beginning of the file " Move the cursor to the beginning of the file
normal! gg normal! gg
" Add syntax highlighting for the file names
syntax match MRUFileName '^.\{-}\ze('
highlight default link MRUFileName Identifier
setlocal nomodifiable setlocal nomodifiable
endfunction endfunction

Loading…
Cancel
Save