Просмотр исходного кода

Add FileCopy to vim, fix rename not creating folders

We also change the `delete()` function to use `rm -rf` instead of
`delete(something, 'rf')`, because older versions of `delete()` do not
have the second argument.
Weiyi Lou 9 лет назад
Родитель
Сommit
2816af3b87
1 измененных файлов с 29 добавлено и 12 удалено
  1. 29 12
      vim/settings/filebrowser.vim

+ 29 - 12
vim/settings/filebrowser.vim

@@ -20,42 +20,58 @@ augroup dirvishCustomisation
 
   " Add/Adjust mappings.
   " - 'o' to (o)pen and 'i' to spl(i)t.
-  " - Create, Delete, Rename with 'mc', 'md', 'mr'.
+  " - Add, Copy, Delete, Rename with 'ma', 'mc', 'md', 'mr'.
   autocmd FileType dirvish let s:nowait = (v:version > 703 ? '<nowait>' : '')
         \| execute 'nnoremap '.s:nowait.'<buffer><silent> o :<C-U>.call dirvish#open("edit", 0)<CR>'
         \| execute 'nnoremap '.s:nowait.'<buffer><silent> i :<C-U>.call dirvish#open("split", 1)<CR>'
         \| execute 'xnoremap '.s:nowait.'<buffer><silent> O :call dirvish#open("edit", 0)<CR>'
         \| execute 'xnoremap '.s:nowait.'<buffer><silent> I :call dirvish#open("split", 1)<CR>'
-        \| execute 'nnoremap '.s:nowait.'<buffer><silent> mc :<C-U>.call FileCreate()<CR>'
+        \| execute 'nnoremap '.s:nowait.'<buffer><silent> ma :<C-U>.call FileAdd()<CR>'
+        \| execute 'nnoremap '.s:nowait.'<buffer><silent> mc :<C-U>.call FileCopy()<CR>'
         \| execute 'nnoremap '.s:nowait.'<buffer><silent> md :<C-U>.call FileDelete()<CR>'
         \| execute 'nnoremap '.s:nowait.'<buffer><silent> mr :<C-U>.call FileRename()<CR>'
 augroup END
 
-" Create files/directories in dirvish buffers.
-function! FileCreate()
+" Create any directories required.
+function! CreateDirs(filename)
   if &ft != "dirvish" | return | endif
-  let filename = inputdialog('New File: ', expand('%'), 0)
-  if filename == "0" | return | endif
-  " Create any directories required.
-  let dirname = fnamemodify(filename, ':p:h')
+  let dirname = fnamemodify(a:filename, ':p:h')
   if dirname != expand('%:h') | call system('mkdir -p '.dirname) | endif
-  " Edit the file if it is a file.
-  if !isdirectory(filename)
-    execute 'edit' filename
+endfunction
+
+" Create files/directories in dirvish buffers.
+function! FileAdd()
+  if &ft != "dirvish" | return | endif
+  let newfile = inputdialog('New File: ', expand('%'), 0)
+  if newfile == "0" | return | endif
+  call CreateDirs(newfile)
+  if !isdirectory(newfile)
+    execute 'edit' newfile
   else
     normal R
   endif
 endfunction
 
+" Rename files/directories in dirvish buffers.
+function! FileCopy()
+  if &ft != "dirvish" | return | endif
+  let current = getline('.')
+  let newname = inputdialog('Copy: ', current, 0)
+  if newname == "0" | return | endif
+  call CreateDirs(newname)
+  call system('cp '.current.' '.newname) | normal R
+endfunction
+
 " Delete files/directories in dirvish buffers.
 function! FileDelete()
   if &ft != "dirvish" | return | endif
   let current = getline('.')
+  if current == '/' | return | endif " Do not do the silly thing.
   let delcheck = confirm('Delete `'.current.'`?', "&Yes\n&No", 2)
   if delcheck != 1 | return | endif
   let delcheck = confirm('Are you SURE?', "&Yes\n&No", 2)
   if delcheck != 1 | return | endif
-  call delete(current, 'rf') | normal R
+  call system('rm -rf '.current) | normal R
 endfunction
 
 " Rename files/directories in dirvish buffers.
@@ -64,6 +80,7 @@ function! FileRename()
   let current = getline('.')
   let newname = inputdialog('Rename: ', current, 0)
   if newname == "0" | return | endif
+  call CreateDirs(newname)
   call rename(current, newname) | normal R
 endfunction