summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/macros/README.txt8
-rw-r--r--runtime/macros/editexisting.vim97
-rw-r--r--src/fileio.c33
-rw-r--r--src/tag.c15
4 files changed, 139 insertions, 14 deletions
diff --git a/runtime/macros/README.txt b/runtime/macros/README.txt
index 61066c5a5..ce61cd361 100644
--- a/runtime/macros/README.txt
+++ b/runtime/macros/README.txt
@@ -12,11 +12,19 @@ The other files contain some handy utilities. They also serve as examples for
how to use Vi and Vim functionality.
dvorak for when you use a Dvorak keyboard
+
justify.vim user function for justifying text
+
matchit.vim + matchit.txt make % match if-fi, HTML tags, and much more
+
less.sh + less.vim make Vim work like less (or more)
+
shellmenu.vim menus for editing shell scripts in the GUI version
+
swapmous.vim swap left and right mouse buttons
+editexisting.vim when editing a file that is already edited with
+ another Vim instance
+
This one is only for Unix. It can be found in the extra archive:
file_select.vim macros that make a handy file selector
diff --git a/runtime/macros/editexisting.vim b/runtime/macros/editexisting.vim
new file mode 100644
index 000000000..2f28874aa
--- /dev/null
+++ b/runtime/macros/editexisting.vim
@@ -0,0 +1,97 @@
+" Vim Plugin: Edit the file with an existing Vim if possible
+" Maintainer: Bram Moolenaar
+" Last Change: 2005 Dec 11
+
+" This is a plugin, drop it in your (Unix) ~/.vim/plugin or (Win32)
+" $VIM/vimfiles/plugin directory.
+
+" This plugin serves two purposes:
+" 1. On startup, if we were invoked with one file name argument and the file
+" is not modified then try to find another Vim instance that is editing
+" this file. If there is one then bring it to the foreground and exit.
+" 2. When a file is edited and a swap file exists for it, try finding that
+" other Vim and bring it to the foreground. Requires Vim 7, because it
+" uses the SwapExists autocommand event.
+
+" Function that finds the Vim instance that is editing "filename" and brings
+" it to the foreground.
+func s:EditElsewhere(filename)
+ let fname_esc = substitute(a:filename, "'", "''", "g")
+
+ let servers = serverlist()
+ while servers != ''
+ " Get next server name in "servername"; remove it from "servers".
+ let i = match(servers, "\n")
+ if i == -1
+ let servername = servers
+ let servers = ''
+ else
+ let servername = strpart(servers, 0, i)
+ let servers = strpart(servers, i + 1)
+ endif
+
+ " Skip ourselves.
+ if servername ==? v:servername
+ continue
+ endif
+
+ " Check if this server is editing our file.
+ if remote_expr(servername, "bufloaded('" . fname_esc . "')")
+ " Yes, bring it to the foreground.
+ if has("win32")
+ call remote_foreground(servername)
+ endif
+ call remote_expr(servername, "foreground()")
+
+ " Make sure the file is visible in a window (not hidden).
+ " If v:swapcommand exists and is set, send it to the server.
+ if exists("v:swapcommand")
+ let c = substitute(v:swapcommand, "'", "''", "g")
+ call remote_expr(servername, "EditExisting('" . fname_esc . "', '" . c . "')")
+ else
+ call remote_expr(servername, "EditExisting('" . fname_esc . "', '')")
+ endif
+
+ if !(has('vim_starting') && has('gui_running') && has('gui_win32'))
+ " Tell the user what is happening. Not when the GUI is starting
+ " though, it would result in a message box.
+ echomsg "File is being edited by " . servername
+ sleep 2
+ endif
+ return 'q'
+ endif
+ endwhile
+ return ''
+endfunc
+
+" When the plugin is loaded and there is one file name argument: Find another
+" Vim server that is editing this file right now.
+if argc() == 1 && !&modified
+ if s:EditElsewhere(expand("%:p")) == 'q'
+ quit
+ endif
+endif
+
+" Setup for handling the situation that an existing swap file is found.
+try
+ au! SwapExists * let v:swapchoice = s:EditElsewhere(expand("<afile>:p"))
+catch
+ " Without SwapExists we don't do anything for ":edit" commands
+endtry
+
+" Function used on the server to make the file visible and possibly execute a
+" command.
+func! EditExisting(fname, command)
+ let n = bufwinnr(a:fname)
+ if n > 0
+ exe n . "wincmd w"
+ else
+ exe "split " . escape(a:fname, ' #%"|')
+ endif
+
+ if a:command != ''
+ exe "normal " . a:command
+ endif
+
+ redraw
+endfunc
diff --git a/src/fileio.c b/src/fileio.c
index 3126143fe..a573a5b6f 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -2534,15 +2534,16 @@ set_file_time(fname, atime, mtime)
#endif
/*
- * buf_write() - write to file 'fname' lines 'start' through 'end'
+ * buf_write() - write to file "fname" lines "start" through "end"
*
* We do our own buffering here because fwrite() is so slow.
*
- * If forceit is true, we don't care for errors when attempting backups (jw).
- * In case of an error everything possible is done to restore the original file.
- * But when forceit is TRUE, we risk loosing it.
- * When reset_changed is TRUE and start == 1 and end ==
- * curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
+ * If "forceit" is true, we don't care for errors when attempting backups.
+ * In case of an error everything possible is done to restore the original
+ * file. But when "forceit" is TRUE, we risk loosing it.
+ *
+ * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and
+ * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
*
* This function must NOT use NameBuff (because it's called by autowrite()).
*
@@ -2557,7 +2558,7 @@ buf_write(buf, fname, sfname, start, end, eap, append, forceit,
linenr_T start, end;
exarg_T *eap; /* for forced 'ff' and 'fenc', can be
NULL! */
- int append;
+ int append; /* append to the file */
int forceit;
int reset_changed;
int filtering;
@@ -2652,7 +2653,8 @@ buf_write(buf, fname, sfname, start, end, eap, append, forceit,
* If there is no file name yet, use the one for the written file.
* BF_NOTEDITED is set to reflect this (in case the write fails).
* Don't do this when the write is for a filter command.
- * Only do this when 'cpoptions' contains the 'f' flag.
+ * Don't do this when appending.
+ * Only do this when 'cpoptions' contains the 'F' flag.
*/
if (reset_changed
&& whole
@@ -2662,6 +2664,7 @@ buf_write(buf, fname, sfname, start, end, eap, append, forceit,
#endif
&& buf->b_ffname == NULL
&& !filtering
+ && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
&& vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
{
#ifdef FEAT_AUTOCMD
@@ -2826,9 +2829,12 @@ buf_write(buf, fname, sfname, start, end, eap, append, forceit,
{
/* Assume the buffer was written, update the timestamp. */
ml_timestamp(buf);
- buf->b_flags &= ~BF_WRITE_MASK;
+ if (append)
+ buf->b_flags &= ~BF_NEW;
+ else
+ buf->b_flags &= ~BF_WRITE_MASK;
}
- if (reset_changed && buf->b_changed
+ if (reset_changed && buf->b_changed && !append
&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
/* Buffer still changed, the autocommands didn't work
* properly. */
@@ -4232,7 +4238,7 @@ restore_backup:
/* When written everything correctly: reset 'modified'. Unless not
* writing to the original file and '+' is not in 'cpoptions'. */
- if (reset_changed && whole
+ if (reset_changed && whole && !append
#ifdef FEAT_MBYTE
&& !write_info.bw_conv_error
#endif
@@ -4250,7 +4256,10 @@ restore_backup:
if (overwriting)
{
ml_timestamp(buf);
- buf->b_flags &= ~BF_WRITE_MASK;
+ if (append)
+ buf->b_flags &= ~BF_NEW;
+ else
+ buf->b_flags &= ~BF_WRITE_MASK;
}
/*
diff --git a/src/tag.c b/src/tag.c
index 4a19ba810..440e220e8 100644
--- a/src/tag.c
+++ b/src/tag.c
@@ -843,11 +843,22 @@ do_tag(tag, type, count, forceit, verbose)
}
}
+#ifdef FEAT_AUTOCMD
+ /* Let the SwapExists event know what tag we are jumping to. */
+ vim_snprintf((char *)IObuff, IOSIZE, ":ta %s\r", name);
+ set_vim_var_string(VV_SWAPCOMMAND, IObuff, -1);
+#endif
+
/*
* Jump to the desired match.
*/
- if (jumpto_tag(matches[cur_match], forceit, type != DT_CSCOPE)
- == NOTAGFILE)
+ i = jumpto_tag(matches[cur_match], forceit, type != DT_CSCOPE);
+
+#ifdef FEAT_AUTOCMD
+ set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
+#endif
+
+ if (i == NOTAGFILE)
{
/* File not found: try again with another matching tag */
if ((type == DT_PREV && cur_match > 0)