> Python: Adding line to buffer other than the current one doesn't work
> correctly. (Rozbujnik, 2010 Dec 19)

The following patch fixes things, but don’t ask me why. Just taken code from 
f_setbufvar for saving/restoring curbuf.

Maybe code somewhere uses curwin to get current buffer in place of curbuf.

# HG changeset patch
# User ZyX <[email protected]>
# Date 1368677693 -14400
# Branch python-extended-2
# Node ID dbd9fe86da1020ad6b9bdadf332ccf43a4e1e6d6
# Parent  5f8adb1d88b905d73dc2b4d106740759a66113b3
Use aucmd_prepbuf/aucmd_restbuf everywhere in place of setting curbuf directly

diff -r 5f8adb1d88b9 -r dbd9fe86da10 src/if_py_both.h
--- a/src/if_py_both.h  Thu May 16 06:49:12 2013 +0400
+++ b/src/if_py_both.h  Thu May 16 08:14:53 2013 +0400
@@ -2316,10 +2316,10 @@
      */
     if (line == Py_None || line == NULL)
     {
-       buf_T *savebuf = curbuf;
+       aco_save_T      aco;
 
        PyErr_Clear();
-       curbuf = buf;
+       aucmd_prepbuf(&aco, buf);
 
        if (u_savedel((linenr_T)n, 1L) == FAIL)
            PyErr_SetVim(_("cannot save undo information"));
@@ -2332,7 +2332,7 @@
            deleted_lines_mark((linenr_T)n, 1L);
        }
 
-       curbuf = savebuf;
+       aucmd_restbuf(&aco);
 
        if (PyErr_Occurred() || VimErrorCheck())
            return FAIL;
@@ -2345,14 +2345,14 @@
     else if (PyString_Check(line))
     {
        char *save = StringToLine(line);
-       buf_T *savebuf = curbuf;
+       aco_save_T      aco;
 
        if (save == NULL)
            return FAIL;
 
        /* We do not need to free "save" if ml_replace() consumes it. */
        PyErr_Clear();
-       curbuf = buf;
+       aucmd_prepbuf(&aco, buf);
 
        if (u_savesub((linenr_T)n) == FAIL)
        {
@@ -2367,7 +2367,7 @@
        else
            changed_bytes((linenr_T)n, 0);
 
-       curbuf = savebuf;
+       aucmd_restbuf(&aco);
 
        /* Check that the cursor is not beyond the end of the line now. */
        if (buf == curwin->w_buffer)
@@ -2409,10 +2409,10 @@
     {
        PyInt   i;
        PyInt   n = (int)(hi - lo);
-       buf_T   *savebuf = curbuf;
+       aco_save_T      aco;
 
        PyErr_Clear();
-       curbuf = buf;
+       aucmd_prepbuf(&aco, buf);
 
        if (u_savedel((linenr_T)lo, (long)n) == FAIL)
            PyErr_SetVim(_("cannot save undo information"));
@@ -2431,7 +2431,7 @@
            deleted_lines_mark((linenr_T)lo, (long)i);
        }
 
-       curbuf = savebuf;
+       aucmd_restbuf(&aco);
 
        if (PyErr_Occurred() || VimErrorCheck())
            return FAIL;
@@ -2448,7 +2448,7 @@
        PyInt   old_len = hi - lo;
        PyInt   extra = 0;      /* lines added to text, can be negative */
        char    **array;
-       buf_T   *savebuf;
+       aco_save_T      aco;
 
        if (new_len == 0)       /* avoid allocating zero bytes */
            array = NULL;
@@ -2476,10 +2476,8 @@
            }
        }
 
-       savebuf = curbuf;
-
        PyErr_Clear();
-       curbuf = buf;
+       aucmd_prepbuf(&aco, buf);
 
        if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
            PyErr_SetVim(_("cannot save undo information"));
@@ -2559,7 +2557,7 @@
        if (buf == curwin->w_buffer)
            py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
 
-       curbuf = savebuf;
+       aucmd_restbuf(&aco);
 
        if (PyErr_Occurred() || VimErrorCheck())
            return FAIL;
@@ -2593,15 +2591,13 @@
     if (PyString_Check(lines))
     {
        char    *str = StringToLine(lines);
-       buf_T   *savebuf;
+       aco_save_T      aco;
 
        if (str == NULL)
            return FAIL;
 
-       savebuf = curbuf;
-
        PyErr_Clear();
-       curbuf = buf;
+       aucmd_prepbuf(&aco, buf);
 
        if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
            PyErr_SetVim(_("cannot save undo information"));
@@ -2611,7 +2607,7 @@
            appended_lines_mark((linenr_T)n, 1L);
 
        vim_free(str);
-       curbuf = savebuf;
+       aucmd_restbuf(&aco);
        update_screen(VALID);
 
        if (PyErr_Occurred() || VimErrorCheck())
@@ -2627,7 +2623,7 @@
        PyInt   i;
        PyInt   size = PyList_Size(lines);
        char    **array;
-       buf_T   *savebuf;
+       aco_save_T      aco;
 
        array = (char **)alloc((unsigned)(size * sizeof(char *)));
        if (array == NULL)
@@ -2650,10 +2646,8 @@
            }
        }
 
-       savebuf = curbuf;
-
        PyErr_Clear();
-       curbuf = buf;
+       aucmd_prepbuf(&aco, buf);
 
        if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
            PyErr_SetVim(_("cannot save undo information"));
@@ -2683,7 +2677,7 @@
         */
        vim_free(array);
 
-       curbuf = savebuf;
+       aucmd_restbuf(&aco);
        update_screen(VALID);
 
        if (PyErr_Occurred() || VimErrorCheck())
@@ -3099,7 +3093,7 @@
     pos_T      *posp;
     char       *pmark;
     char       mark;
-    buf_T      *curbuf_save;
+    aco_save_T aco;
 
     if (CheckBuffer((BufferObject *)(self)))
        return NULL;
@@ -3108,10 +3102,9 @@
        return NULL;
     mark = *pmark;
 
-    curbuf_save = curbuf;
-    curbuf = ((BufferObject *)(self))->buf;
+    aucmd_prepbuf(&aco, ((BufferObject *)(self))->buf);
     posp = getmark(mark, FALSE);
-    curbuf = curbuf_save;
+    aucmd_restbuf(&aco);
 
     if (posp == NULL)
     {

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


*** /tmp/extdiff.esnaIC/vim.5f8adb1d88b9/src/if_py_both.h	2013-05-16 08:15:01.575099800 +0400
--- vim.dbd9fe86da10/src/if_py_both.h	2013-05-16 08:15:01.580099748 +0400
***************
*** 2316,2325 ****
       */
      if (line == Py_None || line == NULL)
      {
! 	buf_T *savebuf = curbuf;
  
  	PyErr_Clear();
! 	curbuf = buf;
  
  	if (u_savedel((linenr_T)n, 1L) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
--- 2316,2325 ----
       */
      if (line == Py_None || line == NULL)
      {
! 	aco_save_T	aco;
  
  	PyErr_Clear();
! 	aucmd_prepbuf(&aco, buf);
  
  	if (u_savedel((linenr_T)n, 1L) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
***************
*** 2332,2338 ****
  	    deleted_lines_mark((linenr_T)n, 1L);
  	}
  
! 	curbuf = savebuf;
  
  	if (PyErr_Occurred() || VimErrorCheck())
  	    return FAIL;
--- 2332,2338 ----
  	    deleted_lines_mark((linenr_T)n, 1L);
  	}
  
! 	aucmd_restbuf(&aco);
  
  	if (PyErr_Occurred() || VimErrorCheck())
  	    return FAIL;
***************
*** 2345,2358 ****
      else if (PyString_Check(line))
      {
  	char *save = StringToLine(line);
! 	buf_T *savebuf = curbuf;
  
  	if (save == NULL)
  	    return FAIL;
  
  	/* We do not need to free "save" if ml_replace() consumes it. */
  	PyErr_Clear();
! 	curbuf = buf;
  
  	if (u_savesub((linenr_T)n) == FAIL)
  	{
--- 2345,2358 ----
      else if (PyString_Check(line))
      {
  	char *save = StringToLine(line);
! 	aco_save_T	aco;
  
  	if (save == NULL)
  	    return FAIL;
  
  	/* We do not need to free "save" if ml_replace() consumes it. */
  	PyErr_Clear();
! 	aucmd_prepbuf(&aco, buf);
  
  	if (u_savesub((linenr_T)n) == FAIL)
  	{
***************
*** 2367,2373 ****
  	else
  	    changed_bytes((linenr_T)n, 0);
  
! 	curbuf = savebuf;
  
  	/* Check that the cursor is not beyond the end of the line now. */
  	if (buf == curwin->w_buffer)
--- 2367,2373 ----
  	else
  	    changed_bytes((linenr_T)n, 0);
  
! 	aucmd_restbuf(&aco);
  
  	/* Check that the cursor is not beyond the end of the line now. */
  	if (buf == curwin->w_buffer)
***************
*** 2409,2418 ****
      {
  	PyInt	i;
  	PyInt	n = (int)(hi - lo);
! 	buf_T	*savebuf = curbuf;
  
  	PyErr_Clear();
! 	curbuf = buf;
  
  	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
--- 2409,2418 ----
      {
  	PyInt	i;
  	PyInt	n = (int)(hi - lo);
! 	aco_save_T	aco;
  
  	PyErr_Clear();
! 	aucmd_prepbuf(&aco, buf);
  
  	if (u_savedel((linenr_T)lo, (long)n) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
***************
*** 2431,2437 ****
  	    deleted_lines_mark((linenr_T)lo, (long)i);
  	}
  
! 	curbuf = savebuf;
  
  	if (PyErr_Occurred() || VimErrorCheck())
  	    return FAIL;
--- 2431,2437 ----
  	    deleted_lines_mark((linenr_T)lo, (long)i);
  	}
  
! 	aucmd_restbuf(&aco);
  
  	if (PyErr_Occurred() || VimErrorCheck())
  	    return FAIL;
***************
*** 2448,2454 ****
  	PyInt	old_len = hi - lo;
  	PyInt	extra = 0;	/* lines added to text, can be negative */
  	char	**array;
! 	buf_T	*savebuf;
  
  	if (new_len == 0)	/* avoid allocating zero bytes */
  	    array = NULL;
--- 2448,2454 ----
  	PyInt	old_len = hi - lo;
  	PyInt	extra = 0;	/* lines added to text, can be negative */
  	char	**array;
! 	aco_save_T	aco;
  
  	if (new_len == 0)	/* avoid allocating zero bytes */
  	    array = NULL;
***************
*** 2476,2485 ****
  	    }
  	}
  
- 	savebuf = curbuf;
- 
  	PyErr_Clear();
! 	curbuf = buf;
  
  	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
--- 2476,2483 ----
  	    }
  	}
  
  	PyErr_Clear();
! 	aucmd_prepbuf(&aco, buf);
  
  	if (u_save((linenr_T)(lo-1), (linenr_T)hi) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
***************
*** 2559,2565 ****
  	if (buf == curwin->w_buffer)
  	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
  
! 	curbuf = savebuf;
  
  	if (PyErr_Occurred() || VimErrorCheck())
  	    return FAIL;
--- 2557,2563 ----
  	if (buf == curwin->w_buffer)
  	    py_fix_cursor((linenr_T)lo, (linenr_T)hi, (linenr_T)extra);
  
! 	aucmd_restbuf(&aco);
  
  	if (PyErr_Occurred() || VimErrorCheck())
  	    return FAIL;
***************
*** 2593,2607 ****
      if (PyString_Check(lines))
      {
  	char	*str = StringToLine(lines);
! 	buf_T	*savebuf;
  
  	if (str == NULL)
  	    return FAIL;
  
- 	savebuf = curbuf;
- 
  	PyErr_Clear();
! 	curbuf = buf;
  
  	if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
--- 2591,2603 ----
      if (PyString_Check(lines))
      {
  	char	*str = StringToLine(lines);
! 	aco_save_T	aco;
  
  	if (str == NULL)
  	    return FAIL;
  
  	PyErr_Clear();
! 	aucmd_prepbuf(&aco, buf);
  
  	if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
***************
*** 2611,2617 ****
  	    appended_lines_mark((linenr_T)n, 1L);
  
  	vim_free(str);
! 	curbuf = savebuf;
  	update_screen(VALID);
  
  	if (PyErr_Occurred() || VimErrorCheck())
--- 2607,2613 ----
  	    appended_lines_mark((linenr_T)n, 1L);
  
  	vim_free(str);
! 	aucmd_restbuf(&aco);
  	update_screen(VALID);
  
  	if (PyErr_Occurred() || VimErrorCheck())
***************
*** 2627,2633 ****
  	PyInt	i;
  	PyInt	size = PyList_Size(lines);
  	char	**array;
! 	buf_T	*savebuf;
  
  	array = (char **)alloc((unsigned)(size * sizeof(char *)));
  	if (array == NULL)
--- 2623,2629 ----
  	PyInt	i;
  	PyInt	size = PyList_Size(lines);
  	char	**array;
! 	aco_save_T	aco;
  
  	array = (char **)alloc((unsigned)(size * sizeof(char *)));
  	if (array == NULL)
***************
*** 2650,2659 ****
  	    }
  	}
  
- 	savebuf = curbuf;
- 
  	PyErr_Clear();
! 	curbuf = buf;
  
  	if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
--- 2646,2653 ----
  	    }
  	}
  
  	PyErr_Clear();
! 	aucmd_prepbuf(&aco, buf);
  
  	if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL)
  	    PyErr_SetVim(_("cannot save undo information"));
***************
*** 2683,2689 ****
  	 */
  	vim_free(array);
  
! 	curbuf = savebuf;
  	update_screen(VALID);
  
  	if (PyErr_Occurred() || VimErrorCheck())
--- 2677,2683 ----
  	 */
  	vim_free(array);
  
! 	aucmd_restbuf(&aco);
  	update_screen(VALID);
  
  	if (PyErr_Occurred() || VimErrorCheck())
***************
*** 3099,3105 ****
      pos_T	*posp;
      char	*pmark;
      char	mark;
!     buf_T	*curbuf_save;
  
      if (CheckBuffer((BufferObject *)(self)))
  	return NULL;
--- 3093,3099 ----
      pos_T	*posp;
      char	*pmark;
      char	mark;
!     aco_save_T	aco;
  
      if (CheckBuffer((BufferObject *)(self)))
  	return NULL;
***************
*** 3108,3117 ****
  	return NULL;
      mark = *pmark;
  
!     curbuf_save = curbuf;
!     curbuf = ((BufferObject *)(self))->buf;
      posp = getmark(mark, FALSE);
!     curbuf = curbuf_save;
  
      if (posp == NULL)
      {
--- 3102,3110 ----
  	return NULL;
      mark = *pmark;
  
!     aucmd_prepbuf(&aco, ((BufferObject *)(self))->buf);
      posp = getmark(mark, FALSE);
!     aucmd_restbuf(&aco);
  
      if (posp == NULL)
      {

Raspunde prin e-mail lui