Mercurial > vim
comparison src/if_py_both.h @ 5202:8edba3805d78 v7.4a.027
updated for version 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.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 17 Jul 2013 17:15:25 +0200 |
parents | 467efeee8f9e |
children | 6fa64615c8d3 |
comparison
equal
deleted
inserted
replaced
5201:b2ed17f5d19c | 5202:8edba3805d78 |
---|---|
3995 * is set to the change in the buffer length. | 3995 * is set to the change in the buffer length. |
3996 */ | 3996 */ |
3997 static int | 3997 static int |
3998 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) | 3998 InsertBufferLines(buf_T *buf, PyInt n, PyObject *lines, PyInt *len_change) |
3999 { | 3999 { |
4000 buf_T *save_curbuf = NULL; | |
4001 win_T *wp; | |
4002 win_T *save_curwin = NULL; | |
4003 tabpage_T *tp; | |
4004 tabpage_T *save_curtab = NULL; | |
4005 | |
4000 /* First of all, we check the type of the supplied Python object. | 4006 /* First of all, we check the type of the supplied Python object. |
4001 * It must be a string or a list, or the call is in error. | 4007 * It must be a string or a list, or the call is in error. |
4002 */ | 4008 */ |
4003 if (PyBytes_Check(lines) || PyUnicode_Check(lines)) | 4009 if (PyBytes_Check(lines) || PyUnicode_Check(lines)) |
4004 { | 4010 { |
4005 char *str = StringToLine(lines); | 4011 char *str = StringToLine(lines); |
4006 buf_T *savebuf; | |
4007 | 4012 |
4008 if (str == NULL) | 4013 if (str == NULL) |
4009 return FAIL; | 4014 return FAIL; |
4010 | 4015 |
4011 PyErr_Clear(); | 4016 PyErr_Clear(); |
4012 VimTryStart(); | 4017 VimTryStart(); |
4013 switch_buffer(&savebuf, buf); | 4018 if (find_win_for_buf(buf, &wp, &tp) == FAIL |
4014 | 4019 || switch_win(&save_curwin, &save_curtab, wp, tp, TRUE) |
4015 if (u_save((linenr_T)n, (linenr_T)(n+1)) == FAIL) | 4020 == FAIL) |
4021 switch_buffer(&save_curbuf, buf); | |
4022 | |
4023 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) | |
4016 RAISE_UNDO_FAIL; | 4024 RAISE_UNDO_FAIL; |
4017 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) | 4025 else if (ml_append((linenr_T)n, (char_u *)str, 0, FALSE) == FAIL) |
4018 RAISE_INSERT_LINE_FAIL; | 4026 RAISE_INSERT_LINE_FAIL; |
4027 else if (save_curbuf == NULL) | |
4028 /* Only adjust marks if we managed to switch to a window that | |
4029 * holds the buffer, otherwise line numbers will be invalid. */ | |
4030 appended_lines_mark((linenr_T)n, 1L); | |
4031 | |
4032 vim_free(str); | |
4033 if (save_curbuf == NULL) | |
4034 restore_win(save_curwin, save_curtab, TRUE); | |
4019 else | 4035 else |
4020 appended_lines_mark((linenr_T)n, 1L); | 4036 restore_buffer(save_curbuf); |
4021 | |
4022 vim_free(str); | |
4023 restore_buffer(savebuf); | |
4024 update_screen(VALID); | 4037 update_screen(VALID); |
4025 | 4038 |
4026 if (VimTryEnd()) | 4039 if (VimTryEnd()) |
4027 return FAIL; | 4040 return FAIL; |
4028 | 4041 |
4034 else if (PyList_Check(lines)) | 4047 else if (PyList_Check(lines)) |
4035 { | 4048 { |
4036 PyInt i; | 4049 PyInt i; |
4037 PyInt size = PyList_Size(lines); | 4050 PyInt size = PyList_Size(lines); |
4038 char **array; | 4051 char **array; |
4039 buf_T *savebuf; | |
4040 | 4052 |
4041 array = PyMem_New(char *, size); | 4053 array = PyMem_New(char *, size); |
4042 if (array == NULL) | 4054 if (array == NULL) |
4043 { | 4055 { |
4044 PyErr_NoMemory(); | 4056 PyErr_NoMemory(); |
4059 } | 4071 } |
4060 } | 4072 } |
4061 | 4073 |
4062 PyErr_Clear(); | 4074 PyErr_Clear(); |
4063 VimTryStart(); | 4075 VimTryStart(); |
4064 switch_buffer(&savebuf, buf); | 4076 if (find_win_for_buf(buf, &wp, &tp) == FAIL |
4077 || switch_win(&save_curwin, &save_curtab, wp, tp, TRUE) | |
4078 == FAIL) | |
4079 switch_buffer(&save_curbuf, buf); | |
4065 | 4080 |
4066 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) | 4081 if (u_save((linenr_T)n, (linenr_T)(n + 1)) == FAIL) |
4067 RAISE_UNDO_FAIL; | 4082 RAISE_UNDO_FAIL; |
4068 else | 4083 else |
4069 { | 4084 { |
4085 if (i > 0) | 4100 if (i > 0) |
4086 appended_lines_mark((linenr_T)n, (long)i); | 4101 appended_lines_mark((linenr_T)n, (long)i); |
4087 } | 4102 } |
4088 | 4103 |
4089 /* Free the array of lines. All of its contents have now | 4104 /* Free the array of lines. All of its contents have now |
4090 * been freed. | 4105 * been freed. */ |
4091 */ | |
4092 PyMem_Free(array); | 4106 PyMem_Free(array); |
4093 | 4107 |
4094 restore_buffer(savebuf); | 4108 if (save_curbuf == NULL) |
4109 restore_win(save_curwin, save_curtab, TRUE); | |
4110 else | |
4111 restore_buffer(save_curbuf); | |
4095 update_screen(VALID); | 4112 update_screen(VALID); |
4096 | 4113 |
4097 if (VimTryEnd()) | 4114 if (VimTryEnd()) |
4098 return FAIL; | 4115 return FAIL; |
4099 | 4116 |