editexisting.vim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. " Vim Plugin: Edit the file with an existing Vim if possible
  2. " Maintainer: Bram Moolenaar
  3. " Last Change: 2014 Dec 06
  4. " This is a plugin, drop it in your (Unix) ~/.vim/plugin or (Win32)
  5. " $VIM/vimfiles/plugin directory. Or make a symbolic link, so that you
  6. " automatically use the latest version.
  7. " This plugin serves two purposes:
  8. " 1. On startup, if we were invoked with one file name argument and the file
  9. " is not modified then try to find another Vim instance that is editing
  10. " this file. If there is one then bring it to the foreground and exit.
  11. " 2. When a file is edited and a swap file exists for it, try finding that
  12. " other Vim and bring it to the foreground. Requires Vim 7, because it
  13. " uses the SwapExists autocommand event.
  14. if v:version < 700
  15. finish
  16. endif
  17. " Function that finds the Vim instance that is editing "filename" and brings
  18. " it to the foreground.
  19. func s:EditElsewhere(filename)
  20. let fname_esc = substitute(a:filename, "'", "''", "g")
  21. let servers = serverlist()
  22. while servers != ''
  23. " Get next server name in "servername"; remove it from "servers".
  24. let i = match(servers, "\n")
  25. if i == -1
  26. let servername = servers
  27. let servers = ''
  28. else
  29. let servername = strpart(servers, 0, i)
  30. let servers = strpart(servers, i + 1)
  31. endif
  32. " Skip ourselves.
  33. if servername ==? v:servername
  34. continue
  35. endif
  36. " Check if this server is editing our file.
  37. if remote_expr(servername, "bufloaded('" . fname_esc . "')")
  38. " Yes, bring it to the foreground.
  39. if has("win32")
  40. call remote_foreground(servername)
  41. endif
  42. call remote_expr(servername, "foreground()")
  43. if remote_expr(servername, "exists('*EditExisting')")
  44. " Make sure the file is visible in a window (not hidden).
  45. " If v:swapcommand exists and is set, send it to the server.
  46. if exists("v:swapcommand")
  47. let c = substitute(v:swapcommand, "'", "''", "g")
  48. call remote_expr(servername, "EditExisting('" . fname_esc . "', '" . c . "')")
  49. else
  50. call remote_expr(servername, "EditExisting('" . fname_esc . "', '')")
  51. endif
  52. endif
  53. if !(has('vim_starting') && has('gui_running') && has('gui_win32'))
  54. " Tell the user what is happening. Not when the GUI is starting
  55. " though, it would result in a message box.
  56. echomsg "File is being edited by " . servername
  57. sleep 2
  58. endif
  59. return 'q'
  60. endif
  61. endwhile
  62. return ''
  63. endfunc
  64. " When the plugin is loaded and there is one file name argument: Find another
  65. " Vim server that is editing this file right now.
  66. if argc() == 1 && !&modified
  67. if s:EditElsewhere(expand("%:p")) == 'q'
  68. quit
  69. endif
  70. endif
  71. " Setup for handling the situation that an existing swap file is found.
  72. try
  73. au! SwapExists * let v:swapchoice = s:EditElsewhere(expand("<afile>:p"))
  74. catch
  75. " Without SwapExists we don't do anything for ":edit" commands
  76. endtry
  77. " Function used on the server to make the file visible and possibly execute a
  78. " command.
  79. func! EditExisting(fname, command)
  80. " Get the window number of the file in the current tab page.
  81. let winnr = bufwinnr(a:fname)
  82. if winnr <= 0
  83. " Not found, look in other tab pages.
  84. let bufnr = bufnr(a:fname)
  85. for i in range(tabpagenr('$'))
  86. if index(tabpagebuflist(i + 1), bufnr) >= 0
  87. " Make this tab page the current one and find the window number.
  88. exe 'tabnext ' . (i + 1)
  89. let winnr = bufwinnr(a:fname)
  90. break
  91. endif
  92. endfor
  93. endif
  94. if winnr > 0
  95. exe winnr . "wincmd w"
  96. elseif exists('*fnameescape')
  97. exe "split " . fnameescape(a:fname)
  98. else
  99. exe "split " . escape(a:fname, " \t\n*?[{`$\\%#'\"|!<")
  100. endif
  101. if a:command != ''
  102. exe "normal! " . a:command
  103. endif
  104. redraw
  105. endfunc