To: vim_dev@googlegroups.com Subject: Patch 7.4a.027 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 7.4a.027 Problem: When Python adds lines to another buffer the cursor position is wrong, it might be below the last line causing ml_get errors. (Vlad Irnov) Solution: Temporarily change the current window, so that marks are corrected properly. Files: src/if_py_both.h, src/window.c, src/proto/buffer.pro *** ../vim-7.4a.026/src/if_py_both.h 2013-07-09 17:42:42.000000000 +0200 --- src/if_py_both.h 2013-07-17 17:03:30.000000000 +0200 *************** *** 3997,4026 **** static int InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) { /* First of all, we check the type of the supplied Python object. * It must be a string or a list, or the call is in error. */ if (PyBytes_Check(lines) || PyUnicode_Check(lines)) { ! char *str = StringToLine(lines); ! buf_T *savebuf; if (str == NULL) return FAIL; PyErr_Clear(); VimTryStart(); ! switch_buffer(&savebuf, buf); ! if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) RAISE_UNDO_FAIL; else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) RAISE_INSERT_LINE_FAIL; ! else appended_lines_mark((linenr_T)n, 1L); vim_free(str); ! restore_buffer(savebuf); update_screen(VALID); if (VimTryEnd()) --- 3997,4039 ---- static int InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) { + buf_T *save_curbuf = NULL; + win_T *wp; + win_T *save_curwin = NULL; + tabpage_T *tp; + tabpage_T *save_curtab = NULL; + /* First of all, we check the type of the supplied Python object. * It must be a string or a list, or the call is in error. */ if (PyBytes_Check(lines) || PyUnicode_Check(lines)) { ! char *str = StringToLine(lines); if (str == NULL) return FAIL; PyErr_Clear(); VimTryStart(); ! if (find_win_for_buf(buf, &wp, &tp) == FAIL ! || switch_win(&save_curwin, &save_curtab, wp, tp, TRUE) ! == FAIL) ! switch_buffer(&save_curbuf, buf); ! if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) RAISE_UNDO_FAIL; else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) RAISE_INSERT_LINE_FAIL; ! else if (save_curbuf == NULL) ! /* Only adjust marks if we managed to switch to a window that ! * holds the buffer, otherwise line numbers will be invalid. */ appended_lines_mark((linenr_T)n, 1L); vim_free(str); ! if (save_curbuf == NULL) ! restore_win(save_curwin, save_curtab, TRUE); ! else ! restore_buffer(save_curbuf); update_screen(VALID); if (VimTryEnd()) *************** *** 4036,4042 **** PyInt i; PyInt size = PyList_Size(lines); char **array; - buf_T *savebuf; array = PyMem_New(char *, size); if (array == NULL) --- 4049,4054 ---- *************** *** 4061,4067 **** PyErr_Clear(); VimTryStart(); ! switch_buffer(&savebuf, buf); if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) RAISE_UNDO_FAIL; --- 4073,4082 ---- PyErr_Clear(); VimTryStart(); ! if (find_win_for_buf(buf, &wp, &tp) == FAIL ! || switch_win(&save_curwin, &save_curtab, wp, tp, TRUE) ! == FAIL) ! switch_buffer(&save_curbuf, buf); if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) RAISE_UNDO_FAIL; *************** *** 4087,4097 **** } /* Free the array of lines. All of its contents have now ! * been freed. ! */ PyMem_Free(array); ! restore_buffer(savebuf); update_screen(VALID); if (VimTryEnd()) --- 4102,4114 ---- } /* Free the array of lines. All of its contents have now ! * been freed. */ PyMem_Free(array); ! if (save_curbuf == NULL) ! restore_win(save_curwin, save_curtab, TRUE); ! else ! restore_buffer(save_curbuf); update_screen(VALID); if (VimTryEnd()) *** ../vim-7.4a.026/src/window.c 2013-07-03 13:53:59.000000000 +0200 --- src/window.c 2013-07-17 16:39:10.000000000 +0200 *************** *** 6577,6583 **** #endif ! #if defined(FEAT_EVAL) || defined(PROTO) /* * Set "win" to be the curwin and "tp" to be the current tab page. * restore_win() MUST be called to undo. --- 6577,6584 ---- #endif ! #if defined(FEAT_EVAL) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \ ! || defined(PROTO) /* * Set "win" to be the curwin and "tp" to be the current tab page. * restore_win() MUST be called to undo. *** ../vim-7.4a.026/src/proto/buffer.pro 2013-07-06 14:14:57.000000000 +0200 --- src/proto/buffer.pro 2013-07-17 16:39:14.000000000 +0200 *************** *** 52,57 **** --- 52,58 ---- int read_viminfo_bufferlist __ARGS((vir_T *virp, int writing)); void write_viminfo_bufferlist __ARGS((FILE *fp)); char_u *buf_spname __ARGS((buf_T *buf)); + int find_win_for_buf __ARGS((buf_T *buf, win_T **wp, tabpage_T **tp)); void buf_addsign __ARGS((buf_T *buf, int id, linenr_T lnum, int typenr)); linenr_T buf_change_sign_type __ARGS((buf_T *buf, int markId, int typenr)); int buf_getsigntype __ARGS((buf_T *buf, linenr_T lnum, int type)); *** ../vim-7.4a.026/src/version.c 2013-07-17 13:43:36.000000000 +0200 --- src/version.c 2013-07-17 17:13:08.000000000 +0200 *************** *** 729,730 **** --- 729,732 ---- { /* Add new patch number below this line */ + /**/ + 27, /**/ -- Engineers are widely recognized as superior marriage material: intelligent, dependable, employed, honest, and handy around the house. (Scott Adams - The Dilbert principle) /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///