Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to Move if the src is not in the file system #55

Closed
liushapku opened this issue Oct 24, 2018 · 1 comment
Closed

Failed to Move if the src is not in the file system #55

liushapku opened this issue Oct 24, 2018 · 1 comment

Comments

@liushapku
Copy link

Occasionally when I am editing a file, the whole folder containing the file is moved to another place. This makes the buffer being edited loss its underlying file. Then I will want to move the buffer to the new place.

A simple :saveas! newplace will not work perfectly since the old buffer will still be a listed buffer. Now I come across this very helpful plugin and try to use Move to achieve the goal. However, I found the Move doesn't work either since the source file is already deleted and it ends up with an error msg Failed to rename ..., see definition for command Move.

To reproduce the error, simply enter an empty folder and type vim a and then in vim do :Move b. Since at this time there is no file named a in the file system, the Move is not allowed.

A tried the following solution and it seems to work. It just checks whether the source still exists before perform the real moving. Would you please review the code or give some other suggestions? I can also create the pull request if you feel acceptable. Thank you very much.

command! -bar -nargs=1 -bang -complete=file Move
      \ let s:src = expand('%:p') |
      \ let s:dst = expand(<q-args>) |
      \ if s:fcall('isdirectory', s:dst) || s:dst[-1:-1] =~# '[\\/]' |
      \   let s:dst .= (s:dst[-1:-1] =~# '[\\/]' ? '' : s:separator()) .
      \     fnamemodify(s:src, ':t') |
      \ endif |
      \ call s:mkdir_p(fnamemodify(s:dst, ':h')) |
      \ let s:dst = substitute(s:fcall('simplify', s:dst), '^\.\'.s:separator(), '', '') |
      \ if <bang>1 && s:fcall('filereadable', s:dst) |
      \   exe 'keepalt saveas '.s:fnameescape(s:dst) |
      \   if !s:fcall('filereadable', s:src) |
      \     execute 'bwipe '.s:fnameescape(s:src) |
      \   endif |
      \ elseif s:fcall('filereadable', s:src) && EunuchRename(s:src, s:dst) |
      \   echoerr 'Failed to rename "'.s:src.'" to "'.s:dst.'"' |
      \ else |
      \   setlocal modified |
      \   exe 'keepalt saveas! '.s:fnameescape(s:dst) |
      \   if s:src !=# expand('%:p') |
      \     execute 'bwipe '.s:fnameescape(s:src) |
      \   endif |
      \   filetype detect |
      \ endif |
      \ unlet s:src |
      \ unlet s:dst |
      \ filetype detect
@liushapku
Copy link
Author

or do we do the bwipe unconditionally? After Move, I think it is not so reasonable to still keep the old buffer.

chkno added a commit to chkno/vim-haskell-module-name that referenced this issue Oct 14, 2019
This can be renamed from ".vim.BROKEN" to ".vim" when
tpope/vim-eunuch#55 is resolved.
@tpope tpope closed this as completed in 36c5974 Aug 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant