Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * change.c: functions related to changing text |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * If the file is readonly, give a warning message with the first change. |
| 18 | * Don't do this for autocommands. |
| 19 | * Doesn't use emsg(), because it flushes the macro buffer. |
| 20 | * If we have undone all changes b_changed will be FALSE, but "b_did_warn" |
| 21 | * will be TRUE. |
| 22 | * "col" is the column for the message; non-zero when in insert mode and |
| 23 | * 'showmode' is on. |
| 24 | * Careful: may trigger autocommands that reload the buffer. |
| 25 | */ |
| 26 | void |
| 27 | change_warning(int col) |
| 28 | { |
| 29 | static char *w_readonly = N_("W10: Warning: Changing a readonly file"); |
| 30 | |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 31 | if (curbuf->b_did_warn |
| 32 | || curbufIsChanged() |
| 33 | || autocmd_busy |
| 34 | || !curbuf->b_p_ro) |
| 35 | return; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 36 | |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 37 | ++curbuf_lock; |
| 38 | apply_autocmds(EVENT_FILECHANGEDRO, NULL, NULL, FALSE, curbuf); |
| 39 | --curbuf_lock; |
| 40 | if (!curbuf->b_p_ro) |
| 41 | return; |
| 42 | |
| 43 | // Do what msg() does, but with a column offset if the warning should |
| 44 | // be after the mode message. |
| 45 | msg_start(); |
| 46 | if (msg_row == Rows - 1) |
| 47 | msg_col = col; |
| 48 | msg_source(HL_ATTR(HLF_W)); |
| 49 | msg_puts_attr(_(w_readonly), HL_ATTR(HLF_W) | MSG_HIST); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 50 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 51 | set_vim_var_string(VV_WARNINGMSG, (char_u *)_(w_readonly), -1); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 52 | #endif |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 53 | msg_clr_eos(); |
| 54 | (void)msg_end(); |
| 55 | if (msg_silent == 0 && !silent_mode |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 56 | #ifdef FEAT_EVAL |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 57 | && time_for_testing != 1 |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 58 | #endif |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 59 | ) |
| 60 | { |
| 61 | out_flush(); |
| 62 | ui_delay(1002L, TRUE); // give the user time to think about it |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 63 | } |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 64 | curbuf->b_did_warn = TRUE; |
| 65 | redraw_cmdline = FALSE; // don't redraw and erase the message |
| 66 | if (msg_row < Rows - 1) |
| 67 | showmode(); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Call this function when something in the current buffer is changed. |
| 72 | * |
| 73 | * Most often called through changed_bytes() and changed_lines(), which also |
| 74 | * mark the area of the display to be redrawn. |
| 75 | * |
| 76 | * Careful: may trigger autocommands that reload the buffer. |
| 77 | */ |
| 78 | void |
| 79 | changed(void) |
| 80 | { |
| 81 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 82 | if (p_imst == IM_ON_THE_SPOT) |
| 83 | { |
| 84 | // The text of the preediting area is inserted, but this doesn't |
| 85 | // mean a change of the buffer yet. That is delayed until the |
| 86 | // text is committed. (this means preedit becomes empty) |
| 87 | if (im_is_preediting() && !xim_changed_while_preediting) |
| 88 | return; |
| 89 | xim_changed_while_preediting = FALSE; |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | if (!curbuf->b_changed) |
| 94 | { |
| 95 | int save_msg_scroll = msg_scroll; |
| 96 | |
| 97 | // Give a warning about changing a read-only file. This may also |
| 98 | // check-out the file, thus change "curbuf"! |
| 99 | change_warning(0); |
| 100 | |
| 101 | // Create a swap file if that is wanted. |
| 102 | // Don't do this for "nofile" and "nowrite" buffer types. |
Bram Moolenaar | 6d4b2f5 | 2022-08-25 15:11:15 +0100 | [diff] [blame] | 103 | if (curbuf->b_may_swap && !bt_dontwrite(curbuf)) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 104 | { |
| 105 | int save_need_wait_return = need_wait_return; |
| 106 | |
| 107 | need_wait_return = FALSE; |
| 108 | ml_open_file(curbuf); |
| 109 | |
| 110 | // The ml_open_file() can cause an ATTENTION message. |
| 111 | // Wait two seconds, to make sure the user reads this unexpected |
| 112 | // message. Since we could be anywhere, call wait_return() now, |
| 113 | // and don't let the emsg() set msg_scroll. |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 114 | if (need_wait_return && emsg_silent == 0 && !in_assert_fails) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 115 | { |
| 116 | out_flush(); |
Bram Moolenaar | eda1da0 | 2019-11-17 17:06:33 +0100 | [diff] [blame] | 117 | ui_delay(2002L, TRUE); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 118 | wait_return(TRUE); |
| 119 | msg_scroll = save_msg_scroll; |
| 120 | } |
| 121 | else |
| 122 | need_wait_return = save_need_wait_return; |
| 123 | } |
| 124 | changed_internal(); |
| 125 | } |
| 126 | ++CHANGEDTICK(curbuf); |
| 127 | |
| 128 | #ifdef FEAT_SEARCH_EXTRA |
| 129 | // If a pattern is highlighted, the position may now be invalid. |
| 130 | highlight_match = FALSE; |
| 131 | #endif |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Internal part of changed(), no user interaction. |
| 136 | * Also used for recovery. |
| 137 | */ |
| 138 | void |
| 139 | changed_internal(void) |
| 140 | { |
| 141 | curbuf->b_changed = TRUE; |
| 142 | ml_setflags(curbuf); |
| 143 | check_status(curbuf); |
| 144 | redraw_tabline = TRUE; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 145 | need_maketitle = TRUE; // set window title later |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 148 | #ifdef FEAT_EVAL |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 149 | static long next_listener_id = 0; |
| 150 | |
| 151 | /* |
Bram Moolenaar | 4a0a161 | 2019-07-17 22:00:19 +0200 | [diff] [blame] | 152 | * Check if the change at "lnum" is above or overlaps with an existing |
| 153 | * change. If above then flush changes and invoke listeners. |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 154 | */ |
Bram Moolenaar | 55737c2 | 2022-02-14 14:51:22 +0000 | [diff] [blame] | 155 | static void |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 156 | check_recorded_changes( |
| 157 | buf_T *buf, |
| 158 | linenr_T lnum, |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 159 | linenr_T lnume, |
Bram Moolenaar | 4a0a161 | 2019-07-17 22:00:19 +0200 | [diff] [blame] | 160 | long xtra) |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 161 | { |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 162 | if (buf->b_recorded_changes == NULL || xtra == 0) |
| 163 | return; |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 164 | |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 165 | listitem_T *li; |
| 166 | linenr_T prev_lnum; |
| 167 | linenr_T prev_lnume; |
| 168 | |
| 169 | FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li) |
| 170 | { |
| 171 | prev_lnum = (linenr_T)dict_get_number( |
| 172 | li->li_tv.vval.v_dict, "lnum"); |
| 173 | prev_lnume = (linenr_T)dict_get_number( |
| 174 | li->li_tv.vval.v_dict, "end"); |
| 175 | if (prev_lnum >= lnum || prev_lnum > lnume || prev_lnume >= lnum) |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 176 | { |
Yegappan Lakshmanan | 3f0092c | 2022-10-16 21:43:07 +0100 | [diff] [blame] | 177 | // the current change is going to make the line number in |
| 178 | // the older change invalid, flush now |
| 179 | invoke_listeners(curbuf); |
| 180 | break; |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 181 | } |
| 182 | } |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 186 | * Record a change for listeners added with listener_add(). |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 187 | * Always for the current buffer. |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 188 | */ |
| 189 | static void |
| 190 | may_record_change( |
| 191 | linenr_T lnum, |
| 192 | colnr_T col, |
| 193 | linenr_T lnume, |
| 194 | long xtra) |
| 195 | { |
| 196 | dict_T *dict; |
| 197 | |
| 198 | if (curbuf->b_listener == NULL) |
| 199 | return; |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 200 | |
| 201 | // If the new change is going to change the line numbers in already listed |
| 202 | // changes, then flush. |
Bram Moolenaar | 55737c2 | 2022-02-14 14:51:22 +0000 | [diff] [blame] | 203 | check_recorded_changes(curbuf, lnum, lnume, xtra); |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 204 | |
| 205 | if (curbuf->b_recorded_changes == NULL) |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 206 | { |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 207 | curbuf->b_recorded_changes = list_alloc(); |
| 208 | if (curbuf->b_recorded_changes == NULL) // out of memory |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 209 | return; |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 210 | ++curbuf->b_recorded_changes->lv_refcount; |
| 211 | curbuf->b_recorded_changes->lv_lock = VAR_FIXED; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | dict = dict_alloc(); |
| 215 | if (dict == NULL) |
| 216 | return; |
| 217 | dict_add_number(dict, "lnum", (varnumber_T)lnum); |
| 218 | dict_add_number(dict, "end", (varnumber_T)lnume); |
| 219 | dict_add_number(dict, "added", (varnumber_T)xtra); |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 220 | dict_add_number(dict, "col", (varnumber_T)col + 1); |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 221 | |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 222 | list_append_dict(curbuf->b_recorded_changes, dict); |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /* |
| 226 | * listener_add() function |
| 227 | */ |
| 228 | void |
| 229 | f_listener_add(typval_T *argvars, typval_T *rettv) |
| 230 | { |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 231 | callback_T callback; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 232 | listener_T *lnr; |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 233 | buf_T *buf = curbuf; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 234 | |
Yegappan Lakshmanan | 5bca906 | 2021-07-24 21:33:26 +0200 | [diff] [blame] | 235 | if (in_vim9script() && check_for_opt_buffer_arg(argvars, 1) == FAIL) |
| 236 | return; |
| 237 | |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 238 | callback = get_callback(&argvars[0]); |
| 239 | if (callback.cb_name == NULL) |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 240 | return; |
| 241 | |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 242 | if (argvars[1].v_type != VAR_UNKNOWN) |
| 243 | { |
| 244 | buf = get_buf_arg(&argvars[1]); |
| 245 | if (buf == NULL) |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 246 | { |
| 247 | free_callback(&callback); |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 248 | return; |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 249 | } |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Bram Moolenaar | c799fe2 | 2019-05-28 23:08:19 +0200 | [diff] [blame] | 252 | lnr = ALLOC_CLEAR_ONE(listener_T); |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 253 | if (lnr == NULL) |
| 254 | { |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 255 | free_callback(&callback); |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 256 | return; |
| 257 | } |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 258 | lnr->lr_next = buf->b_listener; |
| 259 | buf->b_listener = lnr; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 260 | |
Bram Moolenaar | 3a97bb3 | 2019-06-01 13:28:35 +0200 | [diff] [blame] | 261 | set_callback(&lnr->lr_callback, &callback); |
Bram Moolenaar | c96b7f5 | 2022-12-02 15:58:38 +0000 | [diff] [blame] | 262 | if (callback.cb_free_name) |
| 263 | vim_free(callback.cb_name); |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 264 | |
| 265 | lnr->lr_id = ++next_listener_id; |
| 266 | rettv->vval.v_number = lnr->lr_id; |
| 267 | } |
| 268 | |
| 269 | /* |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 270 | * listener_flush() function |
| 271 | */ |
| 272 | void |
| 273 | f_listener_flush(typval_T *argvars, typval_T *rettv UNUSED) |
| 274 | { |
| 275 | buf_T *buf = curbuf; |
| 276 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 277 | if (in_vim9script() && check_for_opt_buffer_arg(argvars, 0) == FAIL) |
| 278 | return; |
| 279 | |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 280 | if (argvars[0].v_type != VAR_UNKNOWN) |
| 281 | { |
| 282 | buf = get_buf_arg(&argvars[0]); |
| 283 | if (buf == NULL) |
| 284 | return; |
| 285 | } |
| 286 | invoke_listeners(buf); |
| 287 | } |
| 288 | |
Bram Moolenaar | 4b4b1b8 | 2021-09-11 15:06:44 +0200 | [diff] [blame] | 289 | |
| 290 | static void |
| 291 | remove_listener(buf_T *buf, listener_T *lnr, listener_T *prev) |
| 292 | { |
| 293 | if (prev != NULL) |
| 294 | prev->lr_next = lnr->lr_next; |
| 295 | else |
| 296 | buf->b_listener = lnr->lr_next; |
| 297 | free_callback(&lnr->lr_callback); |
| 298 | vim_free(lnr); |
| 299 | } |
| 300 | |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 301 | /* |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 302 | * listener_remove() function |
| 303 | */ |
| 304 | void |
Bram Moolenaar | 7b73f91 | 2019-07-13 13:03:02 +0200 | [diff] [blame] | 305 | f_listener_remove(typval_T *argvars, typval_T *rettv) |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 306 | { |
| 307 | listener_T *lnr; |
| 308 | listener_T *next; |
Bram Moolenaar | 7b73f91 | 2019-07-13 13:03:02 +0200 | [diff] [blame] | 309 | listener_T *prev; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 310 | int id; |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 311 | buf_T *buf; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 312 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 313 | if (in_vim9script() && check_for_number_arg(argvars, 0) == FAIL) |
| 314 | return; |
| 315 | |
| 316 | id = tv_get_number(argvars); |
Bram Moolenaar | 8617348 | 2019-10-01 17:02:16 +0200 | [diff] [blame] | 317 | FOR_ALL_BUFFERS(buf) |
Bram Moolenaar | 7b73f91 | 2019-07-13 13:03:02 +0200 | [diff] [blame] | 318 | { |
| 319 | prev = NULL; |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 320 | for (lnr = buf->b_listener; lnr != NULL; lnr = next) |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 321 | { |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 322 | next = lnr->lr_next; |
| 323 | if (lnr->lr_id == id) |
| 324 | { |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 325 | if (textlock > 0) |
Bram Moolenaar | 4b4b1b8 | 2021-09-11 15:06:44 +0200 | [diff] [blame] | 326 | { |
| 327 | // in invoke_listeners(), clear ID and delete later |
| 328 | lnr->lr_id = 0; |
| 329 | return; |
| 330 | } |
| 331 | remove_listener(buf, lnr, prev); |
Bram Moolenaar | 7b73f91 | 2019-07-13 13:03:02 +0200 | [diff] [blame] | 332 | rettv->vval.v_number = 1; |
| 333 | return; |
Bram Moolenaar | a334772 | 2019-05-11 21:14:24 +0200 | [diff] [blame] | 334 | } |
| 335 | prev = lnr; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 336 | } |
Bram Moolenaar | 7b73f91 | 2019-07-13 13:03:02 +0200 | [diff] [blame] | 337 | } |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 341 | * Called before inserting a line above "lnum"/"lnum3" or deleting line "lnum" |
| 342 | * to "lnume". |
| 343 | */ |
| 344 | void |
| 345 | may_invoke_listeners(buf_T *buf, linenr_T lnum, linenr_T lnume, int added) |
| 346 | { |
Bram Moolenaar | 4a0a161 | 2019-07-17 22:00:19 +0200 | [diff] [blame] | 347 | check_recorded_changes(buf, lnum, lnume, added); |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | /* |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 351 | * Called when a sequence of changes is done: invoke listeners added with |
| 352 | * listener_add(). |
| 353 | */ |
| 354 | void |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 355 | invoke_listeners(buf_T *buf) |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 356 | { |
| 357 | listener_T *lnr; |
| 358 | typval_T rettv; |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 359 | typval_T argv[6]; |
| 360 | listitem_T *li; |
| 361 | linenr_T start = MAXLNUM; |
| 362 | linenr_T end = 0; |
| 363 | linenr_T added = 0; |
Bram Moolenaar | 68a4b04 | 2019-05-29 22:28:29 +0200 | [diff] [blame] | 364 | int save_updating_screen = updating_screen; |
| 365 | static int recursive = FALSE; |
Bram Moolenaar | 4b4b1b8 | 2021-09-11 15:06:44 +0200 | [diff] [blame] | 366 | listener_T *next; |
Yegappan Lakshmanan | 956be46 | 2022-09-02 17:12:07 +0100 | [diff] [blame] | 367 | listener_T *prev; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 368 | |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 369 | if (buf->b_recorded_changes == NULL // nothing changed |
Bram Moolenaar | 68a4b04 | 2019-05-29 22:28:29 +0200 | [diff] [blame] | 370 | || buf->b_listener == NULL // no listeners |
| 371 | || recursive) // already busy |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 372 | return; |
Bram Moolenaar | 68a4b04 | 2019-05-29 22:28:29 +0200 | [diff] [blame] | 373 | recursive = TRUE; |
| 374 | |
| 375 | // Block messages on channels from being handled, so that they don't make |
| 376 | // text changes here. |
| 377 | ++updating_screen; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 378 | |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 379 | argv[0].v_type = VAR_NUMBER; |
| 380 | argv[0].vval.v_number = buf->b_fnum; // a:bufnr |
| 381 | |
Bram Moolenaar | aeea721 | 2020-04-02 18:50:46 +0200 | [diff] [blame] | 382 | FOR_ALL_LIST_ITEMS(buf->b_recorded_changes, li) |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 383 | { |
| 384 | varnumber_T lnum; |
| 385 | |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 386 | lnum = dict_get_number(li->li_tv.vval.v_dict, "lnum"); |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 387 | if (start > lnum) |
| 388 | start = lnum; |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 389 | lnum = dict_get_number(li->li_tv.vval.v_dict, "end"); |
Bram Moolenaar | 336bf2b | 2019-10-24 20:07:07 +0200 | [diff] [blame] | 390 | if (end < lnum) |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 391 | end = lnum; |
Bram Moolenaar | d61efa5 | 2022-07-23 09:52:04 +0100 | [diff] [blame] | 392 | added += dict_get_number(li->li_tv.vval.v_dict, "added"); |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 393 | } |
| 394 | argv[1].v_type = VAR_NUMBER; |
| 395 | argv[1].vval.v_number = start; |
| 396 | argv[2].v_type = VAR_NUMBER; |
| 397 | argv[2].vval.v_number = end; |
| 398 | argv[3].v_type = VAR_NUMBER; |
| 399 | argv[3].vval.v_number = added; |
| 400 | |
| 401 | argv[4].v_type = VAR_LIST; |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 402 | argv[4].vval.v_list = buf->b_recorded_changes; |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 403 | ++textlock; |
Bram Moolenaar | fe1ade0 | 2019-05-14 21:20:36 +0200 | [diff] [blame] | 404 | |
| 405 | for (lnr = buf->b_listener; lnr != NULL; lnr = lnr->lr_next) |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 406 | { |
Bram Moolenaar | c6538bc | 2019-08-03 18:17:11 +0200 | [diff] [blame] | 407 | call_callback(&lnr->lr_callback, -1, &rettv, 5, argv); |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 408 | clear_tv(&rettv); |
| 409 | } |
| 410 | |
Bram Moolenaar | 4b4b1b8 | 2021-09-11 15:06:44 +0200 | [diff] [blame] | 411 | // If f_listener_remove() was called may have to remove a listener now. |
Yegappan Lakshmanan | 956be46 | 2022-09-02 17:12:07 +0100 | [diff] [blame] | 412 | prev = NULL; |
Bram Moolenaar | 4b4b1b8 | 2021-09-11 15:06:44 +0200 | [diff] [blame] | 413 | for (lnr = buf->b_listener; lnr != NULL; lnr = next) |
| 414 | { |
Bram Moolenaar | 4b4b1b8 | 2021-09-11 15:06:44 +0200 | [diff] [blame] | 415 | next = lnr->lr_next; |
| 416 | if (lnr->lr_id == 0) |
| 417 | remove_listener(buf, lnr, prev); |
| 418 | else |
| 419 | prev = lnr; |
| 420 | } |
| 421 | |
zeertzjq | cfe4565 | 2022-05-27 17:26:55 +0100 | [diff] [blame] | 422 | --textlock; |
Bram Moolenaar | dda4144 | 2019-05-16 22:11:47 +0200 | [diff] [blame] | 423 | list_unref(buf->b_recorded_changes); |
| 424 | buf->b_recorded_changes = NULL; |
Bram Moolenaar | 68a4b04 | 2019-05-29 22:28:29 +0200 | [diff] [blame] | 425 | |
| 426 | if (save_updating_screen) |
| 427 | updating_screen = TRUE; |
| 428 | else |
| 429 | after_updating_screen(TRUE); |
| 430 | recursive = FALSE; |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 431 | } |
Bram Moolenaar | 8617348 | 2019-10-01 17:02:16 +0200 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Remove all listeners associated with "buf". |
| 435 | */ |
| 436 | void |
| 437 | remove_listeners(buf_T *buf) |
| 438 | { |
| 439 | listener_T *lnr; |
| 440 | listener_T *next; |
| 441 | |
| 442 | for (lnr = buf->b_listener; lnr != NULL; lnr = next) |
| 443 | { |
| 444 | next = lnr->lr_next; |
| 445 | free_callback(&lnr->lr_callback); |
| 446 | vim_free(lnr); |
| 447 | } |
| 448 | buf->b_listener = NULL; |
| 449 | } |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 450 | #endif |
| 451 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 452 | /* |
| 453 | * Common code for when a change was made. |
| 454 | * See changed_lines() for the arguments. |
| 455 | * Careful: may trigger autocommands that reload the buffer. |
| 456 | */ |
| 457 | static void |
| 458 | changed_common( |
| 459 | linenr_T lnum, |
| 460 | colnr_T col, |
| 461 | linenr_T lnume, |
| 462 | long xtra) |
| 463 | { |
| 464 | win_T *wp; |
| 465 | tabpage_T *tp; |
| 466 | int i; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 467 | int cols; |
| 468 | pos_T *p; |
| 469 | int add; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 470 | |
| 471 | // mark the buffer as modified |
| 472 | changed(); |
| 473 | |
Bram Moolenaar | 6d2399b | 2019-05-11 19:14:16 +0200 | [diff] [blame] | 474 | #ifdef FEAT_EVAL |
| 475 | may_record_change(lnum, col, lnume, xtra); |
| 476 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 477 | #ifdef FEAT_DIFF |
| 478 | if (curwin->w_p_diff && diff_internal()) |
| 479 | curtab->tp_diff_update = TRUE; |
| 480 | #endif |
| 481 | |
| 482 | // set the '. mark |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 483 | if ((cmdmod.cmod_flags & CMOD_KEEPJUMPS) == 0) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 484 | { |
| 485 | curbuf->b_last_change.lnum = lnum; |
| 486 | curbuf->b_last_change.col = col; |
| 487 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 488 | // Create a new entry if a new undo-able change was started or we |
| 489 | // don't have an entry yet. |
| 490 | if (curbuf->b_new_change || curbuf->b_changelistlen == 0) |
| 491 | { |
| 492 | if (curbuf->b_changelistlen == 0) |
| 493 | add = TRUE; |
| 494 | else |
| 495 | { |
| 496 | // Don't create a new entry when the line number is the same |
| 497 | // as the last one and the column is not too far away. Avoids |
| 498 | // creating many entries for typing "xxxxx". |
| 499 | p = &curbuf->b_changelist[curbuf->b_changelistlen - 1]; |
| 500 | if (p->lnum != lnum) |
| 501 | add = TRUE; |
| 502 | else |
| 503 | { |
| 504 | cols = comp_textwidth(FALSE); |
| 505 | if (cols == 0) |
| 506 | cols = 79; |
| 507 | add = (p->col + cols < col || col + cols < p->col); |
| 508 | } |
| 509 | } |
| 510 | if (add) |
| 511 | { |
| 512 | // This is the first of a new sequence of undo-able changes |
| 513 | // and it's at some distance of the last change. Use a new |
| 514 | // position in the changelist. |
| 515 | curbuf->b_new_change = FALSE; |
| 516 | |
| 517 | if (curbuf->b_changelistlen == JUMPLISTSIZE) |
| 518 | { |
| 519 | // changelist is full: remove oldest entry |
| 520 | curbuf->b_changelistlen = JUMPLISTSIZE - 1; |
| 521 | mch_memmove(curbuf->b_changelist, curbuf->b_changelist + 1, |
| 522 | sizeof(pos_T) * (JUMPLISTSIZE - 1)); |
| 523 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 524 | { |
| 525 | // Correct position in changelist for other windows on |
| 526 | // this buffer. |
| 527 | if (wp->w_buffer == curbuf && wp->w_changelistidx > 0) |
| 528 | --wp->w_changelistidx; |
| 529 | } |
| 530 | } |
| 531 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 532 | { |
| 533 | // For other windows, if the position in the changelist is |
| 534 | // at the end it stays at the end. |
| 535 | if (wp->w_buffer == curbuf |
| 536 | && wp->w_changelistidx == curbuf->b_changelistlen) |
| 537 | ++wp->w_changelistidx; |
| 538 | } |
| 539 | ++curbuf->b_changelistlen; |
| 540 | } |
| 541 | } |
| 542 | curbuf->b_changelist[curbuf->b_changelistlen - 1] = |
| 543 | curbuf->b_last_change; |
| 544 | // The current window is always after the last change, so that "g," |
| 545 | // takes you back to it. |
| 546 | curwin->w_changelistidx = curbuf->b_changelistlen; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Bram Moolenaar | 7ce5b2b | 2022-05-16 19:40:59 +0100 | [diff] [blame] | 549 | if (VIsual_active) |
| 550 | check_visual_pos(); |
| 551 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 552 | FOR_ALL_TAB_WINDOWS(tp, wp) |
| 553 | { |
| 554 | if (wp->w_buffer == curbuf) |
| 555 | { |
Bram Moolenaar | 9437737 | 2022-02-16 20:30:52 +0000 | [diff] [blame] | 556 | linenr_T last = lnume + xtra - 1; // last line after the change |
Luuk van Baal | 5d01f86 | 2023-05-11 19:24:20 +0100 | [diff] [blame] | 557 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 558 | // Mark this window to be redrawn later. |
Bram Moolenaar | 471c0fa | 2022-08-22 15:19:16 +0100 | [diff] [blame] | 559 | if (!redraw_not_allowed && wp->w_redr_type < UPD_VALID) |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 560 | wp->w_redr_type = UPD_VALID; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 561 | |
zeertzjq | f2d90a3 | 2024-02-12 20:28:01 +0100 | [diff] [blame] | 562 | // When inserting/deleting lines and the window has specific lines |
| 563 | // to be redrawn, w_redraw_top and w_redraw_bot may now be invalid, |
| 564 | // so just redraw everything. |
| 565 | if (xtra != 0 && wp->w_redraw_top != 0) |
| 566 | redraw_win_later(wp, UPD_NOT_VALID); |
| 567 | |
Luuk van Baal | 5d01f86 | 2023-05-11 19:24:20 +0100 | [diff] [blame] | 568 | // Reset "w_skipcol" if the topline length has become smaller to |
| 569 | // such a degree that nothing will be visible anymore, accounting |
| 570 | // for 'smoothscroll' <<< or 'listchars' "precedes" marker. |
| 571 | if (wp->w_skipcol > 0 |
| 572 | && (last < wp->w_topline |
| 573 | || (wp->w_topline >= lnum |
| 574 | && wp->w_topline < lnume |
| 575 | && win_linetabsize(wp, wp->w_topline, |
| 576 | ml_get(wp->w_topline), (colnr_T)MAXCOL) |
Luuk van Baal | 24b62ec | 2023-05-13 14:12:15 +0100 | [diff] [blame] | 577 | <= wp->w_skipcol + sms_marker_overlap(wp, |
| 578 | win_col_off(wp) - win_col_off2(wp))))) |
Luuk van Baal | 5d01f86 | 2023-05-11 19:24:20 +0100 | [diff] [blame] | 579 | wp->w_skipcol = 0; |
| 580 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 581 | // Check if a change in the buffer has invalidated the cached |
| 582 | // values for the cursor. |
| 583 | #ifdef FEAT_FOLDING |
| 584 | // Update the folds for this window. Can't postpone this, because |
| 585 | // a following operator might work on the whole fold: ">>dd". |
Bram Moolenaar | 9437737 | 2022-02-16 20:30:52 +0000 | [diff] [blame] | 586 | foldUpdate(wp, lnum, last); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 587 | |
| 588 | // The change may cause lines above or below the change to become |
| 589 | // included in a fold. Set lnum/lnume to the first/last line that |
| 590 | // might be displayed differently. |
| 591 | // Set w_cline_folded here as an efficient way to update it when |
| 592 | // inserting lines just above a closed fold. |
| 593 | i = hasFoldingWin(wp, lnum, &lnum, NULL, FALSE, NULL); |
| 594 | if (wp->w_cursor.lnum == lnum) |
| 595 | wp->w_cline_folded = i; |
Bram Moolenaar | 9437737 | 2022-02-16 20:30:52 +0000 | [diff] [blame] | 596 | i = hasFoldingWin(wp, last, NULL, &last, FALSE, NULL); |
| 597 | if (wp->w_cursor.lnum == last) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 598 | wp->w_cline_folded = i; |
| 599 | |
| 600 | // If the changed line is in a range of previously folded lines, |
| 601 | // compare with the first line in that range. |
| 602 | if (wp->w_cursor.lnum <= lnum) |
| 603 | { |
| 604 | i = find_wl_entry(wp, lnum); |
| 605 | if (i >= 0 && wp->w_cursor.lnum > wp->w_lines[i].wl_lnum) |
| 606 | changed_line_abv_curs_win(wp); |
| 607 | } |
| 608 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 609 | if (wp->w_cursor.lnum > lnum) |
| 610 | changed_line_abv_curs_win(wp); |
| 611 | else if (wp->w_cursor.lnum == lnum && wp->w_cursor.col >= col) |
| 612 | changed_cline_bef_curs_win(wp); |
| 613 | if (wp->w_botline >= lnum) |
| 614 | { |
Bram Moolenaar | 5bea41d | 2021-07-13 22:21:44 +0200 | [diff] [blame] | 615 | if (xtra < 0) |
| 616 | invalidate_botline_win(wp); |
| 617 | else |
| 618 | // Assume that botline doesn't change (inserted lines make |
| 619 | // other lines scroll down below botline). |
| 620 | approximate_botline_win(wp); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // Check if any w_lines[] entries have become invalid. |
| 624 | // For entries below the change: Correct the lnums for |
| 625 | // inserted/deleted lines. Makes it possible to stop displaying |
| 626 | // after the change. |
| 627 | for (i = 0; i < wp->w_lines_valid; ++i) |
| 628 | if (wp->w_lines[i].wl_valid) |
| 629 | { |
| 630 | if (wp->w_lines[i].wl_lnum >= lnum) |
| 631 | { |
Bram Moolenaar | 61fdbfa | 2023-02-04 13:57:55 +0000 | [diff] [blame] | 632 | // Do not change wl_lnum at index zero, it is used to |
| 633 | // compare with w_topline. Invalidate it instead. |
| 634 | if (wp->w_lines[i].wl_lnum < lnume || i == 0) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 635 | { |
| 636 | // line included in change |
| 637 | wp->w_lines[i].wl_valid = FALSE; |
| 638 | } |
| 639 | else if (xtra != 0) |
| 640 | { |
| 641 | // line below change |
| 642 | wp->w_lines[i].wl_lnum += xtra; |
| 643 | #ifdef FEAT_FOLDING |
| 644 | wp->w_lines[i].wl_lastlnum += xtra; |
| 645 | #endif |
| 646 | } |
| 647 | } |
| 648 | #ifdef FEAT_FOLDING |
| 649 | else if (wp->w_lines[i].wl_lastlnum >= lnum) |
| 650 | { |
| 651 | // change somewhere inside this range of folded lines, |
| 652 | // may need to be redrawn |
| 653 | wp->w_lines[i].wl_valid = FALSE; |
| 654 | } |
| 655 | #endif |
| 656 | } |
| 657 | |
| 658 | #ifdef FEAT_FOLDING |
| 659 | // Take care of side effects for setting w_topline when folds have |
| 660 | // changed. Esp. when the buffer was changed in another window. |
| 661 | if (hasAnyFolding(wp)) |
| 662 | set_topline(wp, wp->w_topline); |
| 663 | #endif |
zeertzjq | 8c97960 | 2022-04-07 15:08:01 +0100 | [diff] [blame] | 664 | // If lines have been added or removed, relative numbering always |
zeertzjq | 7ce34c9 | 2024-02-08 11:34:55 +0100 | [diff] [blame] | 665 | // requires an update even if cursor didn't move. |
Lewis Russell | 1624639 | 2022-03-29 11:38:17 +0100 | [diff] [blame] | 666 | if (wp->w_p_rnu && xtra != 0) |
zeertzjq | 8c97960 | 2022-04-07 15:08:01 +0100 | [diff] [blame] | 667 | wp->w_last_cursor_lnum_rnu = 0; |
zeertzjq | 7ce34c9 | 2024-02-08 11:34:55 +0100 | [diff] [blame] | 668 | |
Bram Moolenaar | 11a58af | 2019-10-24 22:32:31 +0200 | [diff] [blame] | 669 | #ifdef FEAT_SYN_HL |
zeertzjq | 7ce34c9 | 2024-02-08 11:34:55 +0100 | [diff] [blame] | 670 | if (wp->w_p_cul && wp->w_last_cursorline >= lnum) |
Bram Moolenaar | 11a58af | 2019-10-24 22:32:31 +0200 | [diff] [blame] | 671 | { |
zeertzjq | 7ce34c9 | 2024-02-08 11:34:55 +0100 | [diff] [blame] | 672 | if (wp->w_last_cursorline < lnume) |
| 673 | // If 'cursorline' was inside the change, it has already |
| 674 | // been invalidated in w_lines[] by the loop above. |
| 675 | wp->w_last_cursorline = 0; |
| 676 | else |
| 677 | // If 'cursorline' was below the change, adjust its lnum. |
| 678 | wp->w_last_cursorline += xtra; |
Bram Moolenaar | 11a58af | 2019-10-24 22:32:31 +0200 | [diff] [blame] | 679 | } |
| 680 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 681 | } |
Bram Moolenaar | 368137a | 2022-05-31 13:43:12 +0100 | [diff] [blame] | 682 | #ifdef FEAT_SEARCH_EXTRA |
| 683 | if (wp == curwin && xtra != 0 && search_hl_has_cursor_lnum >= lnum) |
| 684 | search_hl_has_cursor_lnum += xtra; |
| 685 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // Call update_screen() later, which checks out what needs to be redrawn, |
| 689 | // since it notices b_mod_set and then uses b_mod_*. |
Bram Moolenaar | 471c0fa | 2022-08-22 15:19:16 +0100 | [diff] [blame] | 690 | set_must_redraw(UPD_VALID); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 691 | |
| 692 | // when the cursor line is changed always trigger CursorMoved |
| 693 | if (lnum <= curwin->w_cursor.lnum |
| 694 | && lnume + (xtra < 0 ? -xtra : xtra) > curwin->w_cursor.lnum) |
| 695 | last_cursormoved.lnum = 0; |
| 696 | } |
| 697 | |
| 698 | static void |
| 699 | changedOneline(buf_T *buf, linenr_T lnum) |
| 700 | { |
| 701 | if (buf->b_mod_set) |
| 702 | { |
| 703 | // find the maximum area that must be redisplayed |
| 704 | if (lnum < buf->b_mod_top) |
| 705 | buf->b_mod_top = lnum; |
| 706 | else if (lnum >= buf->b_mod_bot) |
| 707 | buf->b_mod_bot = lnum + 1; |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | // set the area that must be redisplayed to one line |
| 712 | buf->b_mod_set = TRUE; |
| 713 | buf->b_mod_top = lnum; |
| 714 | buf->b_mod_bot = lnum + 1; |
| 715 | buf->b_mod_xlines = 0; |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | /* |
| 720 | * Changed bytes within a single line for the current buffer. |
| 721 | * - marks the windows on this buffer to be redisplayed |
| 722 | * - marks the buffer changed by calling changed() |
| 723 | * - invalidates cached values |
| 724 | * Careful: may trigger autocommands that reload the buffer. |
| 725 | */ |
| 726 | void |
| 727 | changed_bytes(linenr_T lnum, colnr_T col) |
| 728 | { |
| 729 | changedOneline(curbuf, lnum); |
| 730 | changed_common(lnum, col, lnum + 1, 0L); |
| 731 | |
Bram Moolenaar | 26f09ea | 2022-09-27 16:29:38 +0100 | [diff] [blame] | 732 | #ifdef FEAT_SPELL |
| 733 | // When text has been changed at the end of the line, possibly the start of |
| 734 | // the next line may have SpellCap that should be removed or it needs to be |
| 735 | // displayed. Schedule the next line for redrawing just in case. |
Bram Moolenaar | f3ef026 | 2022-10-05 13:29:15 +0100 | [diff] [blame] | 736 | // Don't do this when displaying '$' at the end of changed text. |
| 737 | if (spell_check_window(curwin) |
| 738 | && lnum < curbuf->b_ml.ml_line_count |
| 739 | && vim_strchr(p_cpo, CPO_DOLLAR) == NULL) |
Bram Moolenaar | 26f09ea | 2022-09-27 16:29:38 +0100 | [diff] [blame] | 740 | redrawWinline(curwin, lnum + 1); |
| 741 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 742 | #ifdef FEAT_DIFF |
| 743 | // Diff highlighting in other diff windows may need to be updated too. |
| 744 | if (curwin->w_p_diff) |
| 745 | { |
| 746 | win_T *wp; |
| 747 | linenr_T wlnum; |
| 748 | |
| 749 | FOR_ALL_WINDOWS(wp) |
| 750 | if (wp->w_p_diff && wp != curwin) |
| 751 | { |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 752 | redraw_win_later(wp, UPD_VALID); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 753 | wlnum = diff_lnum_win(lnum, wp); |
| 754 | if (wlnum > 0) |
| 755 | changedOneline(wp->w_buffer, wlnum); |
| 756 | } |
| 757 | } |
| 758 | #endif |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Like changed_bytes() but also adjust text properties for "added" bytes. |
| 763 | * When "added" is negative text was deleted. |
| 764 | */ |
Bram Moolenaar | 8b51b7f | 2020-09-15 21:34:18 +0200 | [diff] [blame] | 765 | void |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 766 | inserted_bytes(linenr_T lnum, colnr_T col, int added UNUSED) |
| 767 | { |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 768 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 769 | if (curbuf->b_has_textprop && added != 0) |
Bram Moolenaar | f3333b0 | 2019-05-19 22:53:40 +0200 | [diff] [blame] | 770 | adjust_prop_columns(lnum, col, added, 0); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 771 | #endif |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 772 | |
| 773 | changed_bytes(lnum, col); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* |
| 777 | * Appended "count" lines below line "lnum" in the current buffer. |
| 778 | * Must be called AFTER the change and after mark_adjust(). |
| 779 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 780 | */ |
| 781 | void |
| 782 | appended_lines(linenr_T lnum, long count) |
| 783 | { |
| 784 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | * Like appended_lines(), but adjust marks first. |
| 789 | */ |
| 790 | void |
| 791 | appended_lines_mark(linenr_T lnum, long count) |
| 792 | { |
Brandon Simmons | da3dd7d | 2023-01-17 19:48:07 +0000 | [diff] [blame] | 793 | mark_adjust(lnum + 1, (linenr_T)MAXLNUM, count, 0L); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 794 | changed_lines(lnum + 1, 0, lnum + 1, count); |
| 795 | } |
| 796 | |
| 797 | /* |
| 798 | * Deleted "count" lines at line "lnum" in the current buffer. |
| 799 | * Must be called AFTER the change and after mark_adjust(). |
| 800 | * Takes care of marking the buffer to be redrawn and sets the changed flag. |
| 801 | */ |
| 802 | void |
| 803 | deleted_lines(linenr_T lnum, long count) |
| 804 | { |
| 805 | changed_lines(lnum, 0, lnum + count, -count); |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Like deleted_lines(), but adjust marks first. |
| 810 | * Make sure the cursor is on a valid line before calling, a GUI callback may |
| 811 | * be triggered to display the cursor. |
| 812 | */ |
| 813 | void |
| 814 | deleted_lines_mark(linenr_T lnum, long count) |
| 815 | { |
| 816 | mark_adjust(lnum, (linenr_T)(lnum + count - 1), (long)MAXLNUM, -count); |
| 817 | changed_lines(lnum, 0, lnum + count, -count); |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Marks the area to be redrawn after a change. |
Bram Moolenaar | 326c5d3 | 2022-08-12 13:05:49 +0100 | [diff] [blame] | 822 | * Consider also calling changed_line_display_buf(). |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 823 | */ |
Bram Moolenaar | 1764faa | 2021-05-16 20:18:57 +0200 | [diff] [blame] | 824 | void |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 825 | changed_lines_buf( |
| 826 | buf_T *buf, |
| 827 | linenr_T lnum, // first line with change |
| 828 | linenr_T lnume, // line below last changed line |
| 829 | long xtra) // number of extra lines (negative when deleting) |
| 830 | { |
| 831 | if (buf->b_mod_set) |
| 832 | { |
| 833 | // find the maximum area that must be redisplayed |
| 834 | if (lnum < buf->b_mod_top) |
| 835 | buf->b_mod_top = lnum; |
| 836 | if (lnum < buf->b_mod_bot) |
| 837 | { |
| 838 | // adjust old bot position for xtra lines |
| 839 | buf->b_mod_bot += xtra; |
| 840 | if (buf->b_mod_bot < lnum) |
| 841 | buf->b_mod_bot = lnum; |
| 842 | } |
| 843 | if (lnume + xtra > buf->b_mod_bot) |
| 844 | buf->b_mod_bot = lnume + xtra; |
| 845 | buf->b_mod_xlines += xtra; |
| 846 | } |
| 847 | else |
| 848 | { |
| 849 | // set the area that must be redisplayed |
| 850 | buf->b_mod_set = TRUE; |
| 851 | buf->b_mod_top = lnum; |
| 852 | buf->b_mod_bot = lnume + xtra; |
| 853 | buf->b_mod_xlines = xtra; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* |
| 858 | * Changed lines for the current buffer. |
| 859 | * Must be called AFTER the change and after mark_adjust(). |
| 860 | * - mark the buffer changed by calling changed() |
| 861 | * - mark the windows on this buffer to be redisplayed |
| 862 | * - invalidate cached values |
| 863 | * "lnum" is the first line that needs displaying, "lnume" the first line |
| 864 | * below the changed lines (BEFORE the change). |
| 865 | * When only inserting lines, "lnum" and "lnume" are equal. |
| 866 | * Takes care of calling changed() and updating b_mod_*. |
| 867 | * Careful: may trigger autocommands that reload the buffer. |
| 868 | */ |
| 869 | void |
| 870 | changed_lines( |
| 871 | linenr_T lnum, // first line with change |
| 872 | colnr_T col, // column in first line with change |
| 873 | linenr_T lnume, // line below last changed line |
| 874 | long xtra) // number of extra lines (negative when deleting) |
| 875 | { |
| 876 | changed_lines_buf(curbuf, lnum, lnume, xtra); |
| 877 | |
| 878 | #ifdef FEAT_DIFF |
| 879 | if (xtra == 0 && curwin->w_p_diff && !diff_internal()) |
| 880 | { |
| 881 | // When the number of lines doesn't change then mark_adjust() isn't |
| 882 | // called and other diff buffers still need to be marked for |
| 883 | // displaying. |
| 884 | win_T *wp; |
| 885 | linenr_T wlnum; |
| 886 | |
| 887 | FOR_ALL_WINDOWS(wp) |
| 888 | if (wp->w_p_diff && wp != curwin) |
| 889 | { |
Bram Moolenaar | a4d158b | 2022-08-14 14:17:45 +0100 | [diff] [blame] | 890 | redraw_win_later(wp, UPD_VALID); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 891 | wlnum = diff_lnum_win(lnum, wp); |
| 892 | if (wlnum > 0) |
| 893 | changed_lines_buf(wp->w_buffer, wlnum, |
| 894 | lnume - lnum + wlnum, 0L); |
| 895 | } |
| 896 | } |
| 897 | #endif |
| 898 | |
| 899 | changed_common(lnum, col, lnume, xtra); |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Called when the changed flag must be reset for buffer "buf". |
| 904 | * When "ff" is TRUE also reset 'fileformat'. |
Bram Moolenaar | c024b46 | 2019-06-08 18:07:21 +0200 | [diff] [blame] | 905 | * When "always_inc_changedtick" is TRUE b:changedtick is incremented also when |
| 906 | * the changed flag was off. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 907 | */ |
| 908 | void |
Bram Moolenaar | c024b46 | 2019-06-08 18:07:21 +0200 | [diff] [blame] | 909 | unchanged(buf_T *buf, int ff, int always_inc_changedtick) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 910 | { |
| 911 | if (buf->b_changed || (ff && file_ff_differs(buf, FALSE))) |
| 912 | { |
| 913 | buf->b_changed = 0; |
| 914 | ml_setflags(buf); |
| 915 | if (ff) |
| 916 | save_file_ff(buf); |
| 917 | check_status(buf); |
| 918 | redraw_tabline = TRUE; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 919 | need_maketitle = TRUE; // set window title later |
Bram Moolenaar | c024b46 | 2019-06-08 18:07:21 +0200 | [diff] [blame] | 920 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 921 | } |
Bram Moolenaar | c024b46 | 2019-06-08 18:07:21 +0200 | [diff] [blame] | 922 | else if (always_inc_changedtick) |
| 923 | ++CHANGEDTICK(buf); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 924 | #ifdef FEAT_NETBEANS_INTG |
| 925 | netbeans_unmodified(buf); |
| 926 | #endif |
| 927 | } |
| 928 | |
| 929 | /* |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 930 | * Save the current values of 'fileformat' and 'fileencoding', so that we know |
| 931 | * the file must be considered changed when the value is different. |
| 932 | */ |
| 933 | void |
| 934 | save_file_ff(buf_T *buf) |
| 935 | { |
| 936 | buf->b_start_ffc = *buf->b_p_ff; |
Bram Moolenaar | 1577537 | 2022-10-29 20:01:52 +0100 | [diff] [blame] | 937 | buf->b_start_eof = buf->b_p_eof; |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 938 | buf->b_start_eol = buf->b_p_eol; |
| 939 | buf->b_start_bomb = buf->b_p_bomb; |
| 940 | |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 941 | // Only use free/alloc when necessary, they take time. |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 942 | if (buf->b_start_fenc == NULL |
| 943 | || STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0) |
| 944 | { |
| 945 | vim_free(buf->b_start_fenc); |
| 946 | buf->b_start_fenc = vim_strsave(buf->b_p_fenc); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | /* |
| 951 | * Return TRUE if 'fileformat' and/or 'fileencoding' has a different value |
| 952 | * from when editing started (save_file_ff() called). |
| 953 | * Also when 'endofline' was changed and 'binary' is set, or when 'bomb' was |
| 954 | * changed and 'binary' is not set. |
| 955 | * Also when 'endofline' was changed and 'fixeol' is not set. |
| 956 | * When "ignore_empty" is true don't consider a new, empty buffer to be |
| 957 | * changed. |
| 958 | */ |
| 959 | int |
| 960 | file_ff_differs(buf_T *buf, int ignore_empty) |
| 961 | { |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 962 | // In a buffer that was never loaded the options are not valid. |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 963 | if (buf->b_flags & BF_NEVERLOADED) |
| 964 | return FALSE; |
| 965 | if (ignore_empty |
| 966 | && (buf->b_flags & BF_NEW) |
| 967 | && buf->b_ml.ml_line_count == 1 |
| 968 | && *ml_get_buf(buf, (linenr_T)1, FALSE) == NUL) |
| 969 | return FALSE; |
| 970 | if (buf->b_start_ffc != *buf->b_p_ff) |
| 971 | return TRUE; |
Bram Moolenaar | 1577537 | 2022-10-29 20:01:52 +0100 | [diff] [blame] | 972 | if ((buf->b_p_bin || !buf->b_p_fixeol) |
| 973 | && (buf->b_start_eof != buf->b_p_eof |
| 974 | || buf->b_start_eol != buf->b_p_eol)) |
Bram Moolenaar | 7bae0b1 | 2019-11-21 22:14:18 +0100 | [diff] [blame] | 975 | return TRUE; |
| 976 | if (!buf->b_p_bin && buf->b_start_bomb != buf->b_p_bomb) |
| 977 | return TRUE; |
| 978 | if (buf->b_start_fenc == NULL) |
| 979 | return (*buf->b_p_fenc != NUL); |
| 980 | return (STRCMP(buf->b_start_fenc, buf->b_p_fenc) != 0); |
| 981 | } |
| 982 | |
| 983 | /* |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 984 | * Insert string "p" at the cursor position. Stops at a NUL byte. |
| 985 | * Handles Replace mode and multi-byte characters. |
| 986 | */ |
| 987 | void |
| 988 | ins_bytes(char_u *p) |
| 989 | { |
| 990 | ins_bytes_len(p, (int)STRLEN(p)); |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Insert string "p" with length "len" at the cursor position. |
| 995 | * Handles Replace mode and multi-byte characters. |
| 996 | */ |
| 997 | void |
| 998 | ins_bytes_len(char_u *p, int len) |
| 999 | { |
| 1000 | int i; |
| 1001 | int n; |
| 1002 | |
| 1003 | if (has_mbyte) |
| 1004 | for (i = 0; i < len; i += n) |
| 1005 | { |
| 1006 | if (enc_utf8) |
| 1007 | // avoid reading past p[len] |
| 1008 | n = utfc_ptr2len_len(p + i, len - i); |
| 1009 | else |
| 1010 | n = (*mb_ptr2len)(p + i); |
| 1011 | ins_char_bytes(p + i, n); |
| 1012 | } |
| 1013 | else |
| 1014 | for (i = 0; i < len; ++i) |
| 1015 | ins_char(p[i]); |
| 1016 | } |
| 1017 | |
| 1018 | /* |
| 1019 | * Insert or replace a single character at the cursor position. |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1020 | * When in MODE_REPLACE or MODE_VREPLACE state, replace any existing character. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1021 | * Caller must have prepared for undo. |
| 1022 | * For multi-byte characters we get the whole character, the caller must |
| 1023 | * convert bytes to a character. |
| 1024 | */ |
| 1025 | void |
| 1026 | ins_char(int c) |
| 1027 | { |
| 1028 | char_u buf[MB_MAXBYTES + 1]; |
| 1029 | int n = (*mb_char2bytes)(c, buf); |
| 1030 | |
| 1031 | // When "c" is 0x100, 0x200, etc. we don't want to insert a NUL byte. |
| 1032 | // Happens for CTRL-Vu9900. |
| 1033 | if (buf[0] == 0) |
| 1034 | buf[0] = '\n'; |
| 1035 | |
| 1036 | ins_char_bytes(buf, n); |
| 1037 | } |
| 1038 | |
| 1039 | void |
| 1040 | ins_char_bytes(char_u *buf, int charlen) |
| 1041 | { |
| 1042 | int c = buf[0]; |
| 1043 | int newlen; // nr of bytes inserted |
| 1044 | int oldlen; // nr of bytes deleted (0 when not replacing) |
| 1045 | char_u *p; |
| 1046 | char_u *newp; |
| 1047 | char_u *oldp; |
| 1048 | int linelen; // length of old line including NUL |
| 1049 | colnr_T col; |
| 1050 | linenr_T lnum = curwin->w_cursor.lnum; |
| 1051 | int i; |
| 1052 | |
| 1053 | // Break tabs if needed. |
| 1054 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 1055 | coladvance_force(getviscol()); |
| 1056 | |
| 1057 | col = curwin->w_cursor.col; |
| 1058 | oldp = ml_get(lnum); |
| 1059 | linelen = (int)STRLEN(oldp) + 1; |
| 1060 | |
| 1061 | // The lengths default to the values for when not replacing. |
| 1062 | oldlen = 0; |
| 1063 | newlen = charlen; |
| 1064 | |
| 1065 | if (State & REPLACE_FLAG) |
| 1066 | { |
| 1067 | if (State & VREPLACE_FLAG) |
| 1068 | { |
| 1069 | colnr_T new_vcol = 0; // init for GCC |
| 1070 | colnr_T vcol; |
| 1071 | int old_list; |
| 1072 | |
| 1073 | // Disable 'list' temporarily, unless 'cpo' contains the 'L' flag. |
| 1074 | // Returns the old value of list, so when finished, |
| 1075 | // curwin->w_p_list should be set back to this. |
| 1076 | old_list = curwin->w_p_list; |
| 1077 | if (old_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 1078 | curwin->w_p_list = FALSE; |
| 1079 | |
| 1080 | // In virtual replace mode each character may replace one or more |
| 1081 | // characters (zero if it's a TAB). Count the number of bytes to |
| 1082 | // be deleted to make room for the new character, counting screen |
| 1083 | // cells. May result in adding spaces to fill a gap. |
| 1084 | getvcol(curwin, &curwin->w_cursor, NULL, &vcol, NULL); |
| 1085 | new_vcol = vcol + chartabsize(buf, vcol); |
| 1086 | while (oldp[col + oldlen] != NUL && vcol < new_vcol) |
| 1087 | { |
| 1088 | vcol += chartabsize(oldp + col + oldlen, vcol); |
| 1089 | // Don't need to remove a TAB that takes us to the right |
| 1090 | // position. |
| 1091 | if (vcol > new_vcol && oldp[col + oldlen] == TAB) |
| 1092 | break; |
| 1093 | oldlen += (*mb_ptr2len)(oldp + col + oldlen); |
| 1094 | // Deleted a bit too much, insert spaces. |
| 1095 | if (vcol > new_vcol) |
| 1096 | newlen += vcol - new_vcol; |
| 1097 | } |
| 1098 | curwin->w_p_list = old_list; |
| 1099 | } |
| 1100 | else if (oldp[col] != NUL) |
| 1101 | { |
| 1102 | // normal replace |
| 1103 | oldlen = (*mb_ptr2len)(oldp + col); |
| 1104 | } |
| 1105 | |
| 1106 | |
| 1107 | // Push the replaced bytes onto the replace stack, so that they can be |
| 1108 | // put back when BS is used. The bytes of a multi-byte character are |
| 1109 | // done the other way around, so that the first byte is popped off |
| 1110 | // first (it tells the byte length of the character). |
| 1111 | replace_push(NUL); |
| 1112 | for (i = 0; i < oldlen; ++i) |
| 1113 | { |
| 1114 | if (has_mbyte) |
| 1115 | i += replace_push_mb(oldp + col + i) - 1; |
| 1116 | else |
| 1117 | replace_push(oldp[col + i]); |
| 1118 | } |
| 1119 | } |
| 1120 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1121 | newp = alloc(linelen + newlen - oldlen); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1122 | if (newp == NULL) |
| 1123 | return; |
| 1124 | |
| 1125 | // Copy bytes before the cursor. |
| 1126 | if (col > 0) |
| 1127 | mch_memmove(newp, oldp, (size_t)col); |
| 1128 | |
| 1129 | // Copy bytes after the changed character(s). |
| 1130 | p = newp + col; |
| 1131 | if (linelen > col + oldlen) |
| 1132 | mch_memmove(p + newlen, oldp + col + oldlen, |
| 1133 | (size_t)(linelen - col - oldlen)); |
| 1134 | |
| 1135 | // Insert or overwrite the new character. |
| 1136 | mch_memmove(p, buf, charlen); |
| 1137 | i = charlen; |
| 1138 | |
| 1139 | // Fill with spaces when necessary. |
| 1140 | while (i < newlen) |
| 1141 | p[i++] = ' '; |
| 1142 | |
| 1143 | // Replace the line in the buffer. |
| 1144 | ml_replace(lnum, newp, FALSE); |
| 1145 | |
| 1146 | // mark the buffer as changed and prepare for displaying |
LemonBoy | 0d534d9 | 2022-05-21 11:20:42 +0100 | [diff] [blame] | 1147 | changed_bytes(lnum, col); |
| 1148 | #ifdef FEAT_PROP_POPUP |
| 1149 | if (curbuf->b_has_textprop && newlen != oldlen) |
| 1150 | adjust_prop_columns(lnum, col, newlen - oldlen, |
| 1151 | State & REPLACE_FLAG ? APC_SUBSTITUTE : 0); |
| 1152 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1153 | |
| 1154 | // If we're in Insert or Replace mode and 'showmatch' is set, then briefly |
| 1155 | // show the match for right parens and braces. |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1156 | if (p_sm && (State & MODE_INSERT) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1157 | && msg_silent == 0 |
Bram Moolenaar | e2c453d | 2019-08-21 14:37:09 +0200 | [diff] [blame] | 1158 | && !ins_compl_active()) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1159 | { |
| 1160 | if (has_mbyte) |
| 1161 | showmatch(mb_ptr2char(buf)); |
| 1162 | else |
| 1163 | showmatch(c); |
| 1164 | } |
| 1165 | |
| 1166 | #ifdef FEAT_RIGHTLEFT |
| 1167 | if (!p_ri || (State & REPLACE_FLAG)) |
| 1168 | #endif |
| 1169 | { |
| 1170 | // Normal insert: move cursor right |
| 1171 | curwin->w_cursor.col += charlen; |
| 1172 | } |
| 1173 | |
| 1174 | // TODO: should try to update w_row here, to avoid recomputing it later. |
| 1175 | } |
| 1176 | |
| 1177 | /* |
| 1178 | * Insert a string at the cursor position. |
| 1179 | * Note: Does NOT handle Replace mode. |
| 1180 | * Caller must have prepared for undo. |
| 1181 | */ |
| 1182 | void |
| 1183 | ins_str(char_u *s) |
| 1184 | { |
| 1185 | char_u *oldp, *newp; |
| 1186 | int newlen = (int)STRLEN(s); |
| 1187 | int oldlen; |
| 1188 | colnr_T col; |
| 1189 | linenr_T lnum = curwin->w_cursor.lnum; |
| 1190 | |
| 1191 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 1192 | coladvance_force(getviscol()); |
| 1193 | |
| 1194 | col = curwin->w_cursor.col; |
| 1195 | oldp = ml_get(lnum); |
| 1196 | oldlen = (int)STRLEN(oldp); |
| 1197 | |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1198 | newp = alloc(oldlen + newlen + 1); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1199 | if (newp == NULL) |
| 1200 | return; |
| 1201 | if (col > 0) |
| 1202 | mch_memmove(newp, oldp, (size_t)col); |
| 1203 | mch_memmove(newp + col, s, (size_t)newlen); |
| 1204 | mch_memmove(newp + col + newlen, oldp + col, (size_t)(oldlen - col + 1)); |
| 1205 | ml_replace(lnum, newp, FALSE); |
| 1206 | inserted_bytes(lnum, col, newlen); |
| 1207 | curwin->w_cursor.col += newlen; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * Delete one character under the cursor. |
| 1212 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 1213 | * Caller must have prepared for undo. |
| 1214 | * |
| 1215 | * return FAIL for failure, OK otherwise |
| 1216 | */ |
| 1217 | int |
| 1218 | del_char(int fixpos) |
| 1219 | { |
| 1220 | if (has_mbyte) |
| 1221 | { |
| 1222 | // Make sure the cursor is at the start of a character. |
| 1223 | mb_adjust_cursor(); |
| 1224 | if (*ml_get_cursor() == NUL) |
| 1225 | return FAIL; |
| 1226 | return del_chars(1L, fixpos); |
| 1227 | } |
| 1228 | return del_bytes(1L, fixpos, TRUE); |
| 1229 | } |
| 1230 | |
| 1231 | /* |
| 1232 | * Like del_bytes(), but delete characters instead of bytes. |
| 1233 | */ |
| 1234 | int |
| 1235 | del_chars(long count, int fixpos) |
| 1236 | { |
| 1237 | long bytes = 0; |
| 1238 | long i; |
| 1239 | char_u *p; |
| 1240 | int l; |
| 1241 | |
| 1242 | p = ml_get_cursor(); |
| 1243 | for (i = 0; i < count && *p != NUL; ++i) |
| 1244 | { |
| 1245 | l = (*mb_ptr2len)(p); |
| 1246 | bytes += l; |
| 1247 | p += l; |
| 1248 | } |
| 1249 | return del_bytes(bytes, fixpos, TRUE); |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * Delete "count" bytes under the cursor. |
| 1254 | * If "fixpos" is TRUE, don't leave the cursor on the NUL after the line. |
| 1255 | * Caller must have prepared for undo. |
| 1256 | * |
| 1257 | * Return FAIL for failure, OK otherwise. |
| 1258 | */ |
| 1259 | int |
| 1260 | del_bytes( |
| 1261 | long count, |
| 1262 | int fixpos_arg, |
| 1263 | int use_delcombine UNUSED) // 'delcombine' option applies |
| 1264 | { |
| 1265 | char_u *oldp, *newp; |
| 1266 | colnr_T oldlen; |
| 1267 | colnr_T newlen; |
| 1268 | linenr_T lnum = curwin->w_cursor.lnum; |
| 1269 | colnr_T col = curwin->w_cursor.col; |
| 1270 | int alloc_newp; |
| 1271 | long movelen; |
| 1272 | int fixpos = fixpos_arg; |
| 1273 | |
| 1274 | oldp = ml_get(lnum); |
| 1275 | oldlen = (int)STRLEN(oldp); |
| 1276 | |
| 1277 | // Can't do anything when the cursor is on the NUL after the line. |
| 1278 | if (col >= oldlen) |
| 1279 | return FAIL; |
| 1280 | |
| 1281 | // If "count" is zero there is nothing to do. |
| 1282 | if (count == 0) |
| 1283 | return OK; |
| 1284 | |
| 1285 | // If "count" is negative the caller must be doing something wrong. |
| 1286 | if (count < 1) |
| 1287 | { |
Bram Moolenaar | 9a846fb | 2022-01-01 21:59:18 +0000 | [diff] [blame] | 1288 | siemsg(e_invalid_count_for_del_bytes_nr, count); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1289 | return FAIL; |
| 1290 | } |
| 1291 | |
| 1292 | // If 'delcombine' is set and deleting (less than) one character, only |
| 1293 | // delete the last combining character. |
| 1294 | if (p_deco && use_delcombine && enc_utf8 |
| 1295 | && utfc_ptr2len(oldp + col) >= count) |
| 1296 | { |
| 1297 | int cc[MAX_MCO]; |
| 1298 | int n; |
| 1299 | |
| 1300 | (void)utfc_ptr2char(oldp + col, cc); |
| 1301 | if (cc[0] != NUL) |
| 1302 | { |
| 1303 | // Find the last composing char, there can be several. |
| 1304 | n = col; |
| 1305 | do |
| 1306 | { |
| 1307 | col = n; |
| 1308 | count = utf_ptr2len(oldp + n); |
| 1309 | n += count; |
| 1310 | } while (UTF_COMPOSINGLIKE(oldp + col, oldp + n)); |
| 1311 | fixpos = 0; |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | // When count is too big, reduce it. |
| 1316 | movelen = (long)oldlen - (long)col - count + 1; // includes trailing NUL |
| 1317 | if (movelen <= 1) |
| 1318 | { |
| 1319 | // If we just took off the last character of a non-blank line, and |
| 1320 | // fixpos is TRUE, we don't want to end up positioned at the NUL, |
| 1321 | // unless "restart_edit" is set or 'virtualedit' contains "onemore". |
| 1322 | if (col > 0 && fixpos && restart_edit == 0 |
Bram Moolenaar | 113d9de | 2022-08-08 15:49:18 +0100 | [diff] [blame] | 1323 | && (get_ve_flags() & VE_ONEMORE) == 0) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1324 | { |
| 1325 | --curwin->w_cursor.col; |
| 1326 | curwin->w_cursor.coladd = 0; |
| 1327 | if (has_mbyte) |
| 1328 | curwin->w_cursor.col -= |
| 1329 | (*mb_head_off)(oldp, oldp + curwin->w_cursor.col); |
| 1330 | } |
| 1331 | count = oldlen - col; |
| 1332 | movelen = 1; |
| 1333 | } |
| 1334 | newlen = oldlen - count; |
| 1335 | |
| 1336 | // If the old line has been allocated the deletion can be done in the |
| 1337 | // existing line. Otherwise a new line has to be allocated |
| 1338 | // Can't do this when using Netbeans, because we would need to invoke |
| 1339 | // netbeans_removed(), which deallocates the line. Let ml_replace() take |
| 1340 | // care of notifying Netbeans. |
| 1341 | #ifdef FEAT_NETBEANS_INTG |
| 1342 | if (netbeans_active()) |
| 1343 | alloc_newp = TRUE; |
| 1344 | else |
| 1345 | #endif |
| 1346 | alloc_newp = !ml_line_alloced(); // check if oldp was allocated |
| 1347 | if (!alloc_newp) |
| 1348 | newp = oldp; // use same allocated memory |
| 1349 | else |
| 1350 | { // need to allocate a new line |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 1351 | newp = alloc(newlen + 1); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1352 | if (newp == NULL) |
| 1353 | return FAIL; |
| 1354 | mch_memmove(newp, oldp, (size_t)col); |
| 1355 | } |
| 1356 | mch_memmove(newp + col, oldp + col + count, (size_t)movelen); |
| 1357 | if (alloc_newp) |
| 1358 | ml_replace(lnum, newp, FALSE); |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 1359 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1360 | else |
| 1361 | { |
| 1362 | // Also move any following text properties. |
| 1363 | if (oldlen + 1 < curbuf->b_ml.ml_line_len) |
| 1364 | mch_memmove(newp + newlen + 1, oldp + oldlen + 1, |
| 1365 | (size_t)curbuf->b_ml.ml_line_len - oldlen - 1); |
| 1366 | curbuf->b_ml.ml_line_len -= count; |
zeertzjq | 82e079d | 2024-03-10 08:55:42 +0100 | [diff] [blame] | 1367 | curbuf->b_ml.ml_line_textlen = 0; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1368 | } |
| 1369 | #endif |
| 1370 | |
| 1371 | // mark the buffer as changed and prepare for displaying |
Bram Moolenaar | 12f2003 | 2020-02-26 22:06:00 +0100 | [diff] [blame] | 1372 | inserted_bytes(lnum, col, -count); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1373 | |
| 1374 | return OK; |
| 1375 | } |
| 1376 | |
| 1377 | /* |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1378 | * open_line: Add a new line below or above the current line. |
| 1379 | * |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1380 | * For MODE_VREPLACE state, we only add a new line when we get to the end of |
| 1381 | * the file, otherwise we just start replacing the next line. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1382 | * |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1383 | * Caller must take care of undo. Since MODE_VREPLACE may affect any number of |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1384 | * lines however, it may call u_save_cursor() again when starting to change a |
| 1385 | * new line. |
| 1386 | * "flags": OPENLINE_DELSPACES delete spaces after cursor |
| 1387 | * OPENLINE_DO_COM format comments |
| 1388 | * OPENLINE_KEEPTRAIL keep trailing spaces |
| 1389 | * OPENLINE_MARKFIX adjust mark positions after the line break |
| 1390 | * OPENLINE_COM_LIST format comments with list or 2nd line indent |
| 1391 | * |
| 1392 | * "second_line_indent": indent for after ^^D in Insert mode or if flag |
| 1393 | * OPENLINE_COM_LIST |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1394 | * "did_do_comment" is set to TRUE when intentionally putting the comment |
dundargoc | c57b5bc | 2022-11-02 13:30:51 +0000 | [diff] [blame] | 1395 | * leader in front of the new line. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1396 | * |
| 1397 | * Return OK for success, FAIL for failure |
| 1398 | */ |
| 1399 | int |
| 1400 | open_line( |
| 1401 | int dir, // FORWARD or BACKWARD |
| 1402 | int flags, |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1403 | int second_line_indent, |
| 1404 | int *did_do_comment UNUSED) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1405 | { |
| 1406 | char_u *saved_line; // copy of the original line |
| 1407 | char_u *next_line = NULL; // copy of the next line |
| 1408 | char_u *p_extra = NULL; // what goes to next line |
| 1409 | int less_cols = 0; // less columns for mark in new line |
LemonBoy | 788c06a | 2022-05-14 18:48:05 +0100 | [diff] [blame] | 1410 | int less_cols_off = 0; // columns to skip for mark and |
| 1411 | // textprop adjustment |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1412 | pos_T old_cursor; // old cursor position |
| 1413 | int newcol = 0; // new cursor column |
| 1414 | int newindent = 0; // auto-indent of the new line |
| 1415 | int n; |
| 1416 | int trunc_line = FALSE; // truncate current line afterwards |
| 1417 | int retval = FAIL; // return value |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1418 | int extra_len = 0; // length of p_extra string |
| 1419 | int lead_len; // length of comment leader |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1420 | int comment_start = 0; // start index of the comment leader |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1421 | char_u *lead_flags; // position in 'comments' for comment leader |
| 1422 | char_u *leader = NULL; // copy of comment leader |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1423 | char_u *allocated = NULL; // allocated memory |
| 1424 | char_u *p; |
| 1425 | int saved_char = NUL; // init for GCC |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1426 | pos_T *pos; |
Bram Moolenaar | d2439e0 | 2021-12-12 19:10:44 +0000 | [diff] [blame] | 1427 | int do_cindent; |
Bram Moolenaar | de5cf28 | 2022-05-14 11:52:23 +0100 | [diff] [blame] | 1428 | int do_si = may_do_si(); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1429 | int no_si = FALSE; // reset did_si afterwards |
| 1430 | int first_char = NUL; // init for GCC |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1431 | int vreplace_mode; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1432 | int did_append; // appended a new line |
| 1433 | int saved_pi = curbuf->b_p_pi; // copy of preserveindent setting |
Bram Moolenaar | ebd0e8b | 2022-09-14 22:13:59 +0100 | [diff] [blame] | 1434 | #ifdef FEAT_PROP_POPUP |
| 1435 | int at_eol; // cursor after last character |
| 1436 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1437 | |
| 1438 | // make a copy of the current line so we can mess with it |
| 1439 | saved_line = vim_strsave(ml_get_curline()); |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1440 | if (saved_line == NULL) // out of memory! |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1441 | return FALSE; |
| 1442 | |
Bram Moolenaar | ebd0e8b | 2022-09-14 22:13:59 +0100 | [diff] [blame] | 1443 | #ifdef FEAT_PROP_POPUP |
| 1444 | at_eol = curwin->w_cursor.col >= (int)STRLEN(saved_line); |
| 1445 | #endif |
| 1446 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1447 | if (State & VREPLACE_FLAG) |
| 1448 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1449 | // With MODE_VREPLACE we make a copy of the next line, which we will be |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1450 | // starting to replace. First make the new line empty and let vim play |
| 1451 | // with the indenting and comment leader to its heart's content. Then |
| 1452 | // we grab what it ended up putting on the new line, put back the |
| 1453 | // original line, and call ins_char() to put each new character onto |
| 1454 | // the line, replacing what was there before and pushing the right |
| 1455 | // stuff onto the replace stack. -- webb. |
| 1456 | if (curwin->w_cursor.lnum < orig_line_count) |
| 1457 | next_line = vim_strsave(ml_get(curwin->w_cursor.lnum + 1)); |
| 1458 | else |
| 1459 | next_line = vim_strsave((char_u *)""); |
| 1460 | if (next_line == NULL) // out of memory! |
| 1461 | goto theend; |
| 1462 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1463 | // In MODE_VREPLACE state, a NL replaces the rest of the line, and |
| 1464 | // starts replacing the next line, so push all of the characters left |
| 1465 | // on the line onto the replace stack. We'll push any other characters |
| 1466 | // that might be replaced at the start of the next line (due to |
| 1467 | // autoindent etc) a bit later. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1468 | replace_push(NUL); // Call twice because BS over NL expects it |
| 1469 | replace_push(NUL); |
| 1470 | p = saved_line + curwin->w_cursor.col; |
| 1471 | while (*p != NUL) |
| 1472 | { |
| 1473 | if (has_mbyte) |
| 1474 | p += replace_push_mb(p); |
| 1475 | else |
| 1476 | replace_push(*p++); |
| 1477 | } |
| 1478 | saved_line[curwin->w_cursor.col] = NUL; |
| 1479 | } |
| 1480 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 1481 | if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1482 | { |
| 1483 | p_extra = saved_line + curwin->w_cursor.col; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1484 | if (do_si) // need first char after new line break |
| 1485 | { |
| 1486 | p = skipwhite(p_extra); |
| 1487 | first_char = *p; |
| 1488 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1489 | extra_len = (int)STRLEN(p_extra); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1490 | saved_char = *p_extra; |
| 1491 | *p_extra = NUL; |
| 1492 | } |
| 1493 | |
| 1494 | u_clearline(); // cannot do "U" command when adding lines |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1495 | did_si = FALSE; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1496 | ai_col = 0; |
| 1497 | |
| 1498 | // If we just did an auto-indent, then we didn't type anything on |
| 1499 | // the prior line, and it should be truncated. Do this even if 'ai' is not |
| 1500 | // set because automatically inserting a comment leader also sets did_ai. |
| 1501 | if (dir == FORWARD && did_ai) |
| 1502 | trunc_line = TRUE; |
| 1503 | |
| 1504 | // If 'autoindent' and/or 'smartindent' is set, try to figure out what |
| 1505 | // indent to use for the new line. |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 1506 | if (curbuf->b_p_ai || do_si) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1507 | { |
| 1508 | // count white space on current line |
| 1509 | #ifdef FEAT_VARTABS |
| 1510 | newindent = get_indent_str_vtab(saved_line, curbuf->b_p_ts, |
| 1511 | curbuf->b_p_vts_array, FALSE); |
| 1512 | #else |
| 1513 | newindent = get_indent_str(saved_line, (int)curbuf->b_p_ts, FALSE); |
| 1514 | #endif |
| 1515 | if (newindent == 0 && !(flags & OPENLINE_COM_LIST)) |
| 1516 | newindent = second_line_indent; // for ^^D command in insert mode |
| 1517 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1518 | // Do smart indenting. |
| 1519 | // In insert/replace mode (only when dir == FORWARD) |
| 1520 | // we may move some text to the next line. If it starts with '{' |
| 1521 | // don't add an indent. Fixes inserting a NL before '{' in line |
| 1522 | // "if (condition) {" |
| 1523 | if (!trunc_line && do_si && *saved_line != NUL |
| 1524 | && (p_extra == NULL || first_char != '{')) |
| 1525 | { |
| 1526 | char_u *ptr; |
| 1527 | char_u last_char; |
| 1528 | |
| 1529 | old_cursor = curwin->w_cursor; |
| 1530 | ptr = saved_line; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1531 | if (flags & OPENLINE_DO_COM) |
| 1532 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
| 1533 | else |
| 1534 | lead_len = 0; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1535 | if (dir == FORWARD) |
| 1536 | { |
| 1537 | // Skip preprocessor directives, unless they are |
| 1538 | // recognised as comments. |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 1539 | if ( lead_len == 0 && ptr[0] == '#') |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1540 | { |
| 1541 | while (ptr[0] == '#' && curwin->w_cursor.lnum > 1) |
| 1542 | ptr = ml_get(--curwin->w_cursor.lnum); |
| 1543 | newindent = get_indent(); |
| 1544 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1545 | if (flags & OPENLINE_DO_COM) |
| 1546 | lead_len = get_leader_len(ptr, NULL, FALSE, TRUE); |
| 1547 | else |
| 1548 | lead_len = 0; |
| 1549 | if (lead_len > 0) |
| 1550 | { |
| 1551 | // This case gets the following right: |
| 1552 | // /* |
| 1553 | // * A comment (read '\' as '/'). |
| 1554 | // */ |
| 1555 | // #define IN_THE_WAY |
| 1556 | // This should line up here; |
| 1557 | p = skipwhite(ptr); |
| 1558 | if (p[0] == '/' && p[1] == '*') |
| 1559 | p++; |
| 1560 | if (p[0] == '*') |
| 1561 | { |
| 1562 | for (p++; *p; p++) |
| 1563 | { |
| 1564 | if (p[0] == '/' && p[-1] == '*') |
| 1565 | { |
| 1566 | // End of C comment, indent should line up |
| 1567 | // with the line containing the start of |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 1568 | // the comment. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1569 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 1570 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 1571 | { |
| 1572 | curwin->w_cursor.lnum = pos->lnum; |
| 1573 | newindent = get_indent(); |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 1574 | break; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1575 | } |
Bram Moolenaar | fa4873c | 2022-06-30 22:13:59 +0100 | [diff] [blame] | 1576 | // this may make "ptr" invalid, get it again |
| 1577 | ptr = ml_get(curwin->w_cursor.lnum); |
| 1578 | p = ptr + curwin->w_cursor.col; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | else // Not a comment line |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1584 | { |
| 1585 | // Find last non-blank in line |
| 1586 | p = ptr + STRLEN(ptr) - 1; |
| 1587 | while (p > ptr && VIM_ISWHITE(*p)) |
| 1588 | --p; |
| 1589 | last_char = *p; |
| 1590 | |
| 1591 | // find the character just before the '{' or ';' |
| 1592 | if (last_char == '{' || last_char == ';') |
| 1593 | { |
| 1594 | if (p > ptr) |
| 1595 | --p; |
| 1596 | while (p > ptr && VIM_ISWHITE(*p)) |
| 1597 | --p; |
| 1598 | } |
| 1599 | // Try to catch lines that are split over multiple |
| 1600 | // lines. eg: |
| 1601 | // if (condition && |
| 1602 | // condition) { |
| 1603 | // Should line up here! |
| 1604 | // } |
| 1605 | if (*p == ')') |
| 1606 | { |
| 1607 | curwin->w_cursor.col = (colnr_T)(p - ptr); |
| 1608 | if ((pos = findmatch(NULL, '(')) != NULL) |
| 1609 | { |
| 1610 | curwin->w_cursor.lnum = pos->lnum; |
| 1611 | newindent = get_indent(); |
| 1612 | ptr = ml_get_curline(); |
| 1613 | } |
| 1614 | } |
| 1615 | // If last character is '{' do indent, without |
| 1616 | // checking for "if" and the like. |
| 1617 | if (last_char == '{') |
| 1618 | { |
| 1619 | did_si = TRUE; // do indent |
| 1620 | no_si = TRUE; // don't delete it when '{' typed |
| 1621 | } |
| 1622 | // Look for "if" and the like, use 'cinwords'. |
| 1623 | // Don't do this if the previous line ended in ';' or |
| 1624 | // '}'. |
| 1625 | else if (last_char != ';' && last_char != '}' |
| 1626 | && cin_is_cinword(ptr)) |
| 1627 | did_si = TRUE; |
| 1628 | } |
| 1629 | } |
| 1630 | else // dir == BACKWARD |
| 1631 | { |
| 1632 | // Skip preprocessor directives, unless they are |
| 1633 | // recognised as comments. |
Bram Moolenaar | 8c96af9 | 2019-09-28 19:05:57 +0200 | [diff] [blame] | 1634 | if (lead_len == 0 && ptr[0] == '#') |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1635 | { |
| 1636 | int was_backslashed = FALSE; |
| 1637 | |
| 1638 | while ((ptr[0] == '#' || was_backslashed) && |
| 1639 | curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 1640 | { |
| 1641 | if (*ptr && ptr[STRLEN(ptr) - 1] == '\\') |
| 1642 | was_backslashed = TRUE; |
| 1643 | else |
| 1644 | was_backslashed = FALSE; |
| 1645 | ptr = ml_get(++curwin->w_cursor.lnum); |
| 1646 | } |
| 1647 | if (was_backslashed) |
| 1648 | newindent = 0; // Got to end of file |
| 1649 | else |
| 1650 | newindent = get_indent(); |
| 1651 | } |
| 1652 | p = skipwhite(ptr); |
| 1653 | if (*p == '}') // if line starts with '}': do indent |
| 1654 | did_si = TRUE; |
| 1655 | else // can delete indent when '{' typed |
| 1656 | can_si_back = TRUE; |
| 1657 | } |
| 1658 | curwin->w_cursor = old_cursor; |
| 1659 | } |
| 1660 | if (do_si) |
| 1661 | can_si = TRUE; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1662 | |
| 1663 | did_ai = TRUE; |
| 1664 | } |
| 1665 | |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1666 | // May do indenting after opening a new line. |
| 1667 | do_cindent = !p_paste && (curbuf->b_p_cin |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 1668 | #ifdef FEAT_EVAL |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1669 | || *curbuf->b_p_inde != NUL |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 1670 | #endif |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1671 | ) |
| 1672 | && in_cinkeys(dir == FORWARD |
| 1673 | ? KEY_OPEN_FORW |
| 1674 | : KEY_OPEN_BACK, ' ', linewhite(curwin->w_cursor.lnum)); |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1675 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1676 | // Find out if the current line starts with a comment leader. |
| 1677 | // This may then be inserted in front of the new line. |
| 1678 | end_comment_pending = NUL; |
| 1679 | if (flags & OPENLINE_DO_COM) |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1680 | { |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1681 | lead_len = get_leader_len(saved_line, &lead_flags, |
| 1682 | dir == BACKWARD, TRUE); |
Bram Moolenaar | 2bf875f | 2022-05-07 14:54:11 +0100 | [diff] [blame] | 1683 | if (lead_len == 0 && curbuf->b_p_cin && do_cindent && dir == FORWARD |
Bram Moolenaar | 7e66778 | 2022-05-23 13:10:48 +0100 | [diff] [blame] | 1684 | && (!has_format_option(FO_NO_OPEN_COMS) |
| 1685 | || (flags & OPENLINE_FORMAT))) |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1686 | { |
Bram Moolenaar | 5ea5f37 | 2021-12-29 15:15:47 +0000 | [diff] [blame] | 1687 | // Check for a line comment after code. |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1688 | comment_start = check_linecomment(saved_line); |
| 1689 | if (comment_start != MAXCOL) |
| 1690 | { |
| 1691 | lead_len = get_leader_len(saved_line + comment_start, |
Bram Moolenaar | 5ea5f37 | 2021-12-29 15:15:47 +0000 | [diff] [blame] | 1692 | &lead_flags, FALSE, TRUE); |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1693 | if (lead_len != 0) |
| 1694 | { |
| 1695 | lead_len += comment_start; |
| 1696 | if (did_do_comment != NULL) |
| 1697 | *did_do_comment = TRUE; |
| 1698 | } |
| 1699 | } |
| 1700 | } |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1701 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1702 | else |
| 1703 | lead_len = 0; |
| 1704 | if (lead_len > 0) |
| 1705 | { |
| 1706 | char_u *lead_repl = NULL; // replaces comment leader |
| 1707 | int lead_repl_len = 0; // length of *lead_repl |
| 1708 | char_u lead_middle[COM_MAX_LEN]; // middle-comment string |
| 1709 | char_u lead_end[COM_MAX_LEN]; // end-comment string |
| 1710 | char_u *comment_end = NULL; // where lead_end has been found |
| 1711 | int extra_space = FALSE; // append extra space |
| 1712 | int current_flag; |
| 1713 | int require_blank = FALSE; // requires blank after middle |
| 1714 | char_u *p2; |
| 1715 | |
| 1716 | // If the comment leader has the start, middle or end flag, it may not |
| 1717 | // be used or may be replaced with the middle leader. |
| 1718 | for (p = lead_flags; *p && *p != ':'; ++p) |
| 1719 | { |
| 1720 | if (*p == COM_BLANK) |
| 1721 | { |
| 1722 | require_blank = TRUE; |
| 1723 | continue; |
| 1724 | } |
| 1725 | if (*p == COM_START || *p == COM_MIDDLE) |
| 1726 | { |
| 1727 | current_flag = *p; |
| 1728 | if (*p == COM_START) |
| 1729 | { |
| 1730 | // Doing "O" on a start of comment does not insert leader. |
| 1731 | if (dir == BACKWARD) |
| 1732 | { |
| 1733 | lead_len = 0; |
| 1734 | break; |
| 1735 | } |
| 1736 | |
| 1737 | // find start of middle part |
| 1738 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1739 | require_blank = FALSE; |
| 1740 | } |
| 1741 | |
| 1742 | // Isolate the strings of the middle and end leader. |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 1743 | while (*p && p[-1] != ':') // find end of middle flags |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1744 | { |
| 1745 | if (*p == COM_BLANK) |
| 1746 | require_blank = TRUE; |
| 1747 | ++p; |
| 1748 | } |
| 1749 | (void)copy_option_part(&p, lead_middle, COM_MAX_LEN, ","); |
| 1750 | |
| 1751 | while (*p && p[-1] != ':') // find end of end flags |
| 1752 | { |
| 1753 | // Check whether we allow automatic ending of comments |
| 1754 | if (*p == COM_AUTO_END) |
| 1755 | end_comment_pending = -1; // means we want to set it |
| 1756 | ++p; |
| 1757 | } |
| 1758 | n = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 1759 | |
| 1760 | if (end_comment_pending == -1) // we can set it now |
| 1761 | end_comment_pending = lead_end[n - 1]; |
| 1762 | |
| 1763 | // If the end of the comment is in the same line, don't use |
| 1764 | // the comment leader. |
| 1765 | if (dir == FORWARD) |
| 1766 | { |
| 1767 | for (p = saved_line + lead_len; *p; ++p) |
| 1768 | if (STRNCMP(p, lead_end, n) == 0) |
| 1769 | { |
| 1770 | comment_end = p; |
| 1771 | lead_len = 0; |
| 1772 | break; |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | // Doing "o" on a start of comment inserts the middle leader. |
| 1777 | if (lead_len > 0) |
| 1778 | { |
| 1779 | if (current_flag == COM_START) |
| 1780 | { |
| 1781 | lead_repl = lead_middle; |
| 1782 | lead_repl_len = (int)STRLEN(lead_middle); |
| 1783 | } |
| 1784 | |
| 1785 | // If we have hit RETURN immediately after the start |
| 1786 | // comment leader, then put a space after the middle |
| 1787 | // comment leader on the next line. |
| 1788 | if (!VIM_ISWHITE(saved_line[lead_len - 1]) |
| 1789 | && ((p_extra != NULL |
| 1790 | && (int)curwin->w_cursor.col == lead_len) |
| 1791 | || (p_extra == NULL |
| 1792 | && saved_line[lead_len] == NUL) |
| 1793 | || require_blank)) |
| 1794 | extra_space = TRUE; |
| 1795 | } |
| 1796 | break; |
| 1797 | } |
| 1798 | if (*p == COM_END) |
| 1799 | { |
| 1800 | // Doing "o" on the end of a comment does not insert leader. |
| 1801 | // Remember where the end is, might want to use it to find the |
| 1802 | // start (for C-comments). |
| 1803 | if (dir == FORWARD) |
| 1804 | { |
| 1805 | comment_end = skipwhite(saved_line); |
| 1806 | lead_len = 0; |
| 1807 | break; |
| 1808 | } |
| 1809 | |
| 1810 | // Doing "O" on the end of a comment inserts the middle leader. |
| 1811 | // Find the string for the middle leader, searching backwards. |
| 1812 | while (p > curbuf->b_p_com && *p != ',') |
| 1813 | --p; |
| 1814 | for (lead_repl = p; lead_repl > curbuf->b_p_com |
| 1815 | && lead_repl[-1] != ':'; --lead_repl) |
| 1816 | ; |
| 1817 | lead_repl_len = (int)(p - lead_repl); |
| 1818 | |
| 1819 | // We can probably always add an extra space when doing "O" on |
| 1820 | // the comment-end |
| 1821 | extra_space = TRUE; |
| 1822 | |
| 1823 | // Check whether we allow automatic ending of comments |
| 1824 | for (p2 = p; *p2 && *p2 != ':'; p2++) |
| 1825 | { |
| 1826 | if (*p2 == COM_AUTO_END) |
| 1827 | end_comment_pending = -1; // means we want to set it |
| 1828 | } |
| 1829 | if (end_comment_pending == -1) |
| 1830 | { |
| 1831 | // Find last character in end-comment string |
| 1832 | while (*p2 && *p2 != ',') |
| 1833 | p2++; |
| 1834 | end_comment_pending = p2[-1]; |
| 1835 | } |
| 1836 | break; |
| 1837 | } |
| 1838 | if (*p == COM_FIRST) |
| 1839 | { |
| 1840 | // Comment leader for first line only: Don't repeat leader |
| 1841 | // when using "O", blank out leader when using "o". |
| 1842 | if (dir == BACKWARD) |
| 1843 | lead_len = 0; |
| 1844 | else |
| 1845 | { |
| 1846 | lead_repl = (char_u *)""; |
| 1847 | lead_repl_len = 0; |
| 1848 | } |
| 1849 | break; |
| 1850 | } |
| 1851 | } |
| 1852 | if (lead_len) |
| 1853 | { |
| 1854 | // allocate buffer (may concatenate p_extra later) |
| 1855 | leader = alloc(lead_len + lead_repl_len + extra_space + extra_len |
| 1856 | + (second_line_indent > 0 ? second_line_indent : 0) + 1); |
| 1857 | allocated = leader; // remember to free it later |
| 1858 | |
| 1859 | if (leader == NULL) |
| 1860 | lead_len = 0; |
| 1861 | else |
| 1862 | { |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1863 | int li; |
| 1864 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1865 | vim_strncpy(leader, saved_line, lead_len); |
| 1866 | |
Bram Moolenaar | 6e371ec | 2021-12-12 14:16:39 +0000 | [diff] [blame] | 1867 | // TODO: handle multi-byte and double width chars |
| 1868 | for (li = 0; li < comment_start; ++li) |
| 1869 | if (!VIM_ISWHITE(leader[li])) |
| 1870 | leader[li] = ' '; |
| 1871 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 1872 | // Replace leader with lead_repl, right or left adjusted |
| 1873 | if (lead_repl != NULL) |
| 1874 | { |
| 1875 | int c = 0; |
| 1876 | int off = 0; |
| 1877 | |
| 1878 | for (p = lead_flags; *p != NUL && *p != ':'; ) |
| 1879 | { |
| 1880 | if (*p == COM_RIGHT || *p == COM_LEFT) |
| 1881 | c = *p++; |
| 1882 | else if (VIM_ISDIGIT(*p) || *p == '-') |
| 1883 | off = getdigits(&p); |
| 1884 | else |
| 1885 | ++p; |
| 1886 | } |
| 1887 | if (c == COM_RIGHT) // right adjusted leader |
| 1888 | { |
| 1889 | // find last non-white in the leader to line up with |
| 1890 | for (p = leader + lead_len - 1; p > leader |
| 1891 | && VIM_ISWHITE(*p); --p) |
| 1892 | ; |
| 1893 | ++p; |
| 1894 | |
| 1895 | // Compute the length of the replaced characters in |
| 1896 | // screen characters, not bytes. |
| 1897 | { |
| 1898 | int repl_size = vim_strnsize(lead_repl, |
| 1899 | lead_repl_len); |
| 1900 | int old_size = 0; |
| 1901 | char_u *endp = p; |
| 1902 | int l; |
| 1903 | |
| 1904 | while (old_size < repl_size && p > leader) |
| 1905 | { |
| 1906 | MB_PTR_BACK(leader, p); |
| 1907 | old_size += ptr2cells(p); |
| 1908 | } |
| 1909 | l = lead_repl_len - (int)(endp - p); |
| 1910 | if (l != 0) |
| 1911 | mch_memmove(endp + l, endp, |
| 1912 | (size_t)((leader + lead_len) - endp)); |
| 1913 | lead_len += l; |
| 1914 | } |
| 1915 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1916 | if (p + lead_repl_len > leader + lead_len) |
| 1917 | p[lead_repl_len] = NUL; |
| 1918 | |
| 1919 | // blank-out any other chars from the old leader. |
| 1920 | while (--p >= leader) |
| 1921 | { |
| 1922 | int l = mb_head_off(leader, p); |
| 1923 | |
| 1924 | if (l > 1) |
| 1925 | { |
| 1926 | p -= l; |
| 1927 | if (ptr2cells(p) > 1) |
| 1928 | { |
| 1929 | p[1] = ' '; |
| 1930 | --l; |
| 1931 | } |
| 1932 | mch_memmove(p + 1, p + l + 1, |
| 1933 | (size_t)((leader + lead_len) - (p + l + 1))); |
| 1934 | lead_len -= l; |
| 1935 | *p = ' '; |
| 1936 | } |
| 1937 | else if (!VIM_ISWHITE(*p)) |
| 1938 | *p = ' '; |
| 1939 | } |
| 1940 | } |
| 1941 | else // left adjusted leader |
| 1942 | { |
| 1943 | p = skipwhite(leader); |
| 1944 | |
| 1945 | // Compute the length of the replaced characters in |
| 1946 | // screen characters, not bytes. Move the part that is |
| 1947 | // not to be overwritten. |
| 1948 | { |
| 1949 | int repl_size = vim_strnsize(lead_repl, |
| 1950 | lead_repl_len); |
| 1951 | int i; |
| 1952 | int l; |
| 1953 | |
| 1954 | for (i = 0; i < lead_len && p[i] != NUL; i += l) |
| 1955 | { |
| 1956 | l = (*mb_ptr2len)(p + i); |
| 1957 | if (vim_strnsize(p, i + l) > repl_size) |
| 1958 | break; |
| 1959 | } |
| 1960 | if (i != lead_repl_len) |
| 1961 | { |
| 1962 | mch_memmove(p + lead_repl_len, p + i, |
| 1963 | (size_t)(lead_len - i - (p - leader))); |
| 1964 | lead_len += lead_repl_len - i; |
| 1965 | } |
| 1966 | } |
| 1967 | mch_memmove(p, lead_repl, (size_t)lead_repl_len); |
| 1968 | |
| 1969 | // Replace any remaining non-white chars in the old |
| 1970 | // leader by spaces. Keep Tabs, the indent must |
| 1971 | // remain the same. |
| 1972 | for (p += lead_repl_len; p < leader + lead_len; ++p) |
| 1973 | if (!VIM_ISWHITE(*p)) |
| 1974 | { |
| 1975 | // Don't put a space before a TAB. |
| 1976 | if (p + 1 < leader + lead_len && p[1] == TAB) |
| 1977 | { |
| 1978 | --lead_len; |
| 1979 | mch_memmove(p, p + 1, |
| 1980 | (leader + lead_len) - p); |
| 1981 | } |
| 1982 | else |
| 1983 | { |
| 1984 | int l = (*mb_ptr2len)(p); |
| 1985 | |
| 1986 | if (l > 1) |
| 1987 | { |
| 1988 | if (ptr2cells(p) > 1) |
| 1989 | { |
| 1990 | // Replace a double-wide char with |
| 1991 | // two spaces |
| 1992 | --l; |
| 1993 | *p++ = ' '; |
| 1994 | } |
| 1995 | mch_memmove(p + 1, p + l, |
| 1996 | (leader + lead_len) - p); |
| 1997 | lead_len -= l - 1; |
| 1998 | } |
| 1999 | *p = ' '; |
| 2000 | } |
| 2001 | } |
| 2002 | *p = NUL; |
| 2003 | } |
| 2004 | |
| 2005 | // Recompute the indent, it may have changed. |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2006 | if (curbuf->b_p_ai || do_si) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2007 | #ifdef FEAT_VARTABS |
| 2008 | newindent = get_indent_str_vtab(leader, curbuf->b_p_ts, |
| 2009 | curbuf->b_p_vts_array, FALSE); |
| 2010 | #else |
| 2011 | newindent = get_indent_str(leader, |
| 2012 | (int)curbuf->b_p_ts, FALSE); |
| 2013 | #endif |
| 2014 | |
| 2015 | // Add the indent offset |
| 2016 | if (newindent + off < 0) |
| 2017 | { |
| 2018 | off = -newindent; |
| 2019 | newindent = 0; |
| 2020 | } |
| 2021 | else |
| 2022 | newindent += off; |
| 2023 | |
| 2024 | // Correct trailing spaces for the shift, so that |
| 2025 | // alignment remains equal. |
| 2026 | while (off > 0 && lead_len > 0 |
| 2027 | && leader[lead_len - 1] == ' ') |
| 2028 | { |
| 2029 | // Don't do it when there is a tab before the space |
| 2030 | if (vim_strchr(skipwhite(leader), '\t') != NULL) |
| 2031 | break; |
| 2032 | --lead_len; |
| 2033 | --off; |
| 2034 | } |
| 2035 | |
| 2036 | // If the leader ends in white space, don't add an |
| 2037 | // extra space |
| 2038 | if (lead_len > 0 && VIM_ISWHITE(leader[lead_len - 1])) |
| 2039 | extra_space = FALSE; |
| 2040 | leader[lead_len] = NUL; |
| 2041 | } |
| 2042 | |
| 2043 | if (extra_space) |
| 2044 | { |
| 2045 | leader[lead_len++] = ' '; |
| 2046 | leader[lead_len] = NUL; |
| 2047 | } |
| 2048 | |
| 2049 | newcol = lead_len; |
| 2050 | |
| 2051 | // if a new indent will be set below, remove the indent that |
| 2052 | // is in the comment leader |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2053 | if (newindent || did_si) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2054 | { |
| 2055 | while (lead_len && VIM_ISWHITE(*leader)) |
| 2056 | { |
| 2057 | --lead_len; |
| 2058 | --newcol; |
| 2059 | ++leader; |
| 2060 | } |
| 2061 | } |
| 2062 | |
| 2063 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2064 | did_si = can_si = FALSE; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2065 | } |
| 2066 | else if (comment_end != NULL) |
| 2067 | { |
| 2068 | // We have finished a comment, so we don't use the leader. |
| 2069 | // If this was a C-comment and 'ai' or 'si' is set do a normal |
| 2070 | // indent to align with the line containing the start of the |
| 2071 | // comment. |
| 2072 | if (comment_end[0] == '*' && comment_end[1] == '/' && |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2073 | (curbuf->b_p_ai || do_si)) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2074 | { |
| 2075 | old_cursor = curwin->w_cursor; |
| 2076 | curwin->w_cursor.col = (colnr_T)(comment_end - saved_line); |
| 2077 | if ((pos = findmatch(NULL, NUL)) != NULL) |
| 2078 | { |
| 2079 | curwin->w_cursor.lnum = pos->lnum; |
| 2080 | newindent = get_indent(); |
| 2081 | } |
| 2082 | curwin->w_cursor = old_cursor; |
| 2083 | } |
| 2084 | } |
| 2085 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2086 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2087 | // (State == MODE_INSERT || State == MODE_REPLACE), only when dir == FORWARD |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2088 | if (p_extra != NULL) |
| 2089 | { |
| 2090 | *p_extra = saved_char; // restore char that NUL replaced |
| 2091 | |
| 2092 | // When 'ai' set or "flags" has OPENLINE_DELSPACES, skip to the first |
| 2093 | // non-blank. |
| 2094 | // |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2095 | // When in MODE_REPLACE state, put the deleted blanks on the replace |
| 2096 | // stack, preceded by a NUL, so they can be put back when a BS is |
| 2097 | // entered. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2098 | if (REPLACE_NORMAL(State)) |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2099 | replace_push(NUL); // end of extra blanks |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2100 | if (curbuf->b_p_ai || (flags & OPENLINE_DELSPACES)) |
| 2101 | { |
| 2102 | while ((*p_extra == ' ' || *p_extra == '\t') |
| 2103 | && (!enc_utf8 |
| 2104 | || !utf_iscomposing(utf_ptr2char(p_extra + 1)))) |
| 2105 | { |
| 2106 | if (REPLACE_NORMAL(State)) |
| 2107 | replace_push(*p_extra); |
| 2108 | ++p_extra; |
| 2109 | ++less_cols_off; |
| 2110 | } |
| 2111 | } |
| 2112 | |
| 2113 | // columns for marks adjusted for removed columns |
| 2114 | less_cols = (int)(p_extra - saved_line); |
| 2115 | } |
| 2116 | |
| 2117 | if (p_extra == NULL) |
| 2118 | p_extra = (char_u *)""; // append empty line |
| 2119 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2120 | // concatenate leader and p_extra, if there is a leader |
| 2121 | if (lead_len) |
| 2122 | { |
| 2123 | if (flags & OPENLINE_COM_LIST && second_line_indent > 0) |
| 2124 | { |
| 2125 | int i; |
| 2126 | int padding = second_line_indent |
| 2127 | - (newindent + (int)STRLEN(leader)); |
| 2128 | |
| 2129 | // Here whitespace is inserted after the comment char. |
| 2130 | // Below, set_indent(newindent, SIN_INSERT) will insert the |
| 2131 | // whitespace needed before the comment char. |
| 2132 | for (i = 0; i < padding; i++) |
| 2133 | { |
| 2134 | STRCAT(leader, " "); |
| 2135 | less_cols--; |
| 2136 | newcol++; |
| 2137 | } |
| 2138 | } |
| 2139 | STRCAT(leader, p_extra); |
| 2140 | p_extra = leader; |
| 2141 | did_ai = TRUE; // So truncating blanks works with comments |
| 2142 | less_cols -= lead_len; |
| 2143 | } |
| 2144 | else |
| 2145 | end_comment_pending = NUL; // turns out there was no leader |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2146 | |
| 2147 | old_cursor = curwin->w_cursor; |
| 2148 | if (dir == BACKWARD) |
| 2149 | --curwin->w_cursor.lnum; |
| 2150 | if (!(State & VREPLACE_FLAG) || old_cursor.lnum >= orig_line_count) |
| 2151 | { |
| 2152 | if (ml_append(curwin->w_cursor.lnum, p_extra, (colnr_T)0, FALSE) |
| 2153 | == FAIL) |
| 2154 | goto theend; |
| 2155 | // Postpone calling changed_lines(), because it would mess up folding |
| 2156 | // with markers. |
Brandon Simmons | da3dd7d | 2023-01-17 19:48:07 +0000 | [diff] [blame] | 2157 | mark_adjust(curwin->w_cursor.lnum + 1, (linenr_T)MAXLNUM, 1L, 0L); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2158 | did_append = TRUE; |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 2159 | #ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2160 | if ((State & MODE_INSERT) && (State & VREPLACE_FLAG) == 0) |
LemonBoy | 788c06a | 2022-05-14 18:48:05 +0100 | [diff] [blame] | 2161 | // Properties after the split move to the next line. |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2162 | adjust_props_for_split(curwin->w_cursor.lnum, curwin->w_cursor.lnum, |
Bram Moolenaar | ebd0e8b | 2022-09-14 22:13:59 +0100 | [diff] [blame] | 2163 | curwin->w_cursor.col + 1, 0, at_eol); |
Bram Moolenaar | 45dd07f | 2019-05-15 22:45:37 +0200 | [diff] [blame] | 2164 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2165 | } |
| 2166 | else |
| 2167 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2168 | // In MODE_VREPLACE state we are starting to replace the next line. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2169 | curwin->w_cursor.lnum++; |
| 2170 | if (curwin->w_cursor.lnum >= Insstart.lnum + vr_lines_changed) |
| 2171 | { |
| 2172 | // In case we NL to a new line, BS to the previous one, and NL |
| 2173 | // again, we don't want to save the new line for undo twice. |
Bram Moolenaar | c667da5 | 2019-11-30 20:52:27 +0100 | [diff] [blame] | 2174 | (void)u_save_cursor(); // errors are ignored! |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2175 | vr_lines_changed++; |
| 2176 | } |
| 2177 | ml_replace(curwin->w_cursor.lnum, p_extra, TRUE); |
| 2178 | changed_bytes(curwin->w_cursor.lnum, 0); |
| 2179 | curwin->w_cursor.lnum--; |
| 2180 | did_append = FALSE; |
| 2181 | } |
| 2182 | |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2183 | if (newindent || did_si) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2184 | { |
| 2185 | ++curwin->w_cursor.lnum; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2186 | if (did_si) |
| 2187 | { |
| 2188 | int sw = (int)get_sw_value(curbuf); |
| 2189 | |
| 2190 | if (p_sr) |
| 2191 | newindent -= newindent % sw; |
| 2192 | newindent += sw; |
| 2193 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2194 | // Copy the indent |
| 2195 | if (curbuf->b_p_ci) |
| 2196 | { |
| 2197 | (void)copy_indent(newindent, saved_line); |
| 2198 | |
| 2199 | // Set the 'preserveindent' option so that any further screwing |
| 2200 | // with the line doesn't entirely destroy our efforts to preserve |
| 2201 | // it. It gets restored at the function end. |
| 2202 | curbuf->b_p_pi = TRUE; |
| 2203 | } |
| 2204 | else |
| 2205 | (void)set_indent(newindent, SIN_INSERT); |
| 2206 | less_cols -= curwin->w_cursor.col; |
| 2207 | |
| 2208 | ai_col = curwin->w_cursor.col; |
| 2209 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2210 | // In MODE_REPLACE state, for each character in the new indent, there |
| 2211 | // must be a NUL on the replace stack, for when it is deleted with BS |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2212 | if (REPLACE_NORMAL(State)) |
| 2213 | for (n = 0; n < (int)curwin->w_cursor.col; ++n) |
| 2214 | replace_push(NUL); |
| 2215 | newcol += curwin->w_cursor.col; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2216 | if (no_si) |
| 2217 | did_si = FALSE; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2218 | } |
| 2219 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2220 | // In MODE_REPLACE state, for each character in the extra leader, there |
| 2221 | // must be a NUL on the replace stack, for when it is deleted with BS. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2222 | if (REPLACE_NORMAL(State)) |
| 2223 | while (lead_len-- > 0) |
| 2224 | replace_push(NUL); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2225 | |
| 2226 | curwin->w_cursor = old_cursor; |
| 2227 | |
| 2228 | if (dir == FORWARD) |
| 2229 | { |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2230 | if (trunc_line || (State & MODE_INSERT)) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2231 | { |
| 2232 | // truncate current line at cursor |
| 2233 | saved_line[curwin->w_cursor.col] = NUL; |
| 2234 | // Remove trailing white space, unless OPENLINE_KEEPTRAIL used. |
| 2235 | if (trunc_line && !(flags & OPENLINE_KEEPTRAIL)) |
| 2236 | truncate_spaces(saved_line); |
| 2237 | ml_replace(curwin->w_cursor.lnum, saved_line, FALSE); |
| 2238 | saved_line = NULL; |
| 2239 | if (did_append) |
| 2240 | { |
| 2241 | changed_lines(curwin->w_cursor.lnum, curwin->w_cursor.col, |
| 2242 | curwin->w_cursor.lnum + 1, 1L); |
| 2243 | did_append = FALSE; |
| 2244 | |
| 2245 | // Move marks after the line break to the new line. |
| 2246 | if (flags & OPENLINE_MARKFIX) |
| 2247 | mark_col_adjust(curwin->w_cursor.lnum, |
| 2248 | curwin->w_cursor.col + less_cols_off, |
| 2249 | 1L, (long)-less_cols, 0); |
LemonBoy | 788c06a | 2022-05-14 18:48:05 +0100 | [diff] [blame] | 2250 | #ifdef FEAT_PROP_POPUP |
| 2251 | // Keep into account the deleted blanks on the new line. |
| 2252 | if (curbuf->b_has_textprop && less_cols_off != 0) |
| 2253 | adjust_prop_columns(curwin->w_cursor.lnum + 1, 0, |
| 2254 | -less_cols_off, 0); |
| 2255 | #endif |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2256 | } |
| 2257 | else |
| 2258 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 2259 | } |
| 2260 | |
| 2261 | // Put the cursor on the new line. Careful: the scrollup() above may |
| 2262 | // have moved w_cursor, we must use old_cursor. |
| 2263 | curwin->w_cursor.lnum = old_cursor.lnum + 1; |
| 2264 | } |
| 2265 | if (did_append) |
| 2266 | changed_lines(curwin->w_cursor.lnum, 0, curwin->w_cursor.lnum, 1L); |
| 2267 | |
| 2268 | curwin->w_cursor.col = newcol; |
| 2269 | curwin->w_cursor.coladd = 0; |
| 2270 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2271 | // In MODE_VREPLACE state, we are handling the replace stack ourselves, so |
| 2272 | // stop fixthisline() from doing it (via change_indent()) by telling it |
| 2273 | // we're in normal MODE_INSERT state. |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2274 | if (State & VREPLACE_FLAG) |
| 2275 | { |
| 2276 | vreplace_mode = State; // So we know to put things right later |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2277 | State = MODE_INSERT; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2278 | } |
| 2279 | else |
| 2280 | vreplace_mode = 0; |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2281 | |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 2282 | if (!p_paste) |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2283 | { |
Bram Moolenaar | 49846fb | 2022-10-15 16:05:33 +0100 | [diff] [blame] | 2284 | if (leader == NULL |
| 2285 | && !use_indentexpr_for_lisp() |
| 2286 | && curbuf->b_p_lisp |
| 2287 | && curbuf->b_p_ai) |
| 2288 | { |
| 2289 | // do lisp indenting |
| 2290 | fixthisline(get_lisp_indent); |
| 2291 | ai_col = (colnr_T)getwhitecols_curline(); |
| 2292 | } |
| 2293 | else if (do_cindent || (curbuf->b_p_ai && use_indentexpr_for_lisp())) |
| 2294 | { |
| 2295 | // do 'cindent' or 'indentexpr' indenting |
| 2296 | do_c_expr_indent(); |
| 2297 | ai_col = (colnr_T)getwhitecols_curline(); |
| 2298 | } |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2299 | } |
Bram Moolenaar | 8e145b8 | 2022-05-21 20:17:31 +0100 | [diff] [blame] | 2300 | |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2301 | if (vreplace_mode != 0) |
| 2302 | State = vreplace_mode; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2303 | |
Bram Moolenaar | 2495910 | 2022-05-07 20:01:16 +0100 | [diff] [blame] | 2304 | // Finally, MODE_VREPLACE gets the stuff on the new line, then puts back |
| 2305 | // the original line, and inserts the new stuff char by char, pushing old |
| 2306 | // stuff onto the replace stack (via ins_char()). |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2307 | if (State & VREPLACE_FLAG) |
| 2308 | { |
| 2309 | // Put new line in p_extra |
| 2310 | p_extra = vim_strsave(ml_get_curline()); |
| 2311 | if (p_extra == NULL) |
| 2312 | goto theend; |
| 2313 | |
| 2314 | // Put back original line |
| 2315 | ml_replace(curwin->w_cursor.lnum, next_line, FALSE); |
| 2316 | |
| 2317 | // Insert new stuff into line again |
| 2318 | curwin->w_cursor.col = 0; |
| 2319 | curwin->w_cursor.coladd = 0; |
| 2320 | ins_bytes(p_extra); // will call changed_bytes() |
| 2321 | vim_free(p_extra); |
| 2322 | next_line = NULL; |
| 2323 | } |
| 2324 | |
| 2325 | retval = OK; // success! |
| 2326 | theend: |
| 2327 | curbuf->b_p_pi = saved_pi; |
| 2328 | vim_free(saved_line); |
| 2329 | vim_free(next_line); |
| 2330 | vim_free(allocated); |
| 2331 | return retval; |
| 2332 | } |
| 2333 | |
| 2334 | /* |
| 2335 | * Delete from cursor to end of line. |
| 2336 | * Caller must have prepared for undo. |
| 2337 | * If "fixpos" is TRUE fix the cursor position when done. |
| 2338 | * |
| 2339 | * Return FAIL for failure, OK otherwise. |
| 2340 | */ |
| 2341 | int |
| 2342 | truncate_line(int fixpos) |
| 2343 | { |
| 2344 | char_u *newp; |
| 2345 | linenr_T lnum = curwin->w_cursor.lnum; |
| 2346 | colnr_T col = curwin->w_cursor.col; |
LemonBoy | d0b1a09 | 2022-05-12 18:45:18 +0100 | [diff] [blame] | 2347 | char_u *old_line; |
| 2348 | int deleted; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2349 | |
LemonBoy | d0b1a09 | 2022-05-12 18:45:18 +0100 | [diff] [blame] | 2350 | old_line = ml_get(lnum); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2351 | if (col == 0) |
| 2352 | newp = vim_strsave((char_u *)""); |
| 2353 | else |
LemonBoy | d0b1a09 | 2022-05-12 18:45:18 +0100 | [diff] [blame] | 2354 | newp = vim_strnsave(old_line, col); |
| 2355 | deleted = (int)STRLEN(old_line) - col; |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2356 | |
| 2357 | if (newp == NULL) |
| 2358 | return FAIL; |
| 2359 | |
| 2360 | ml_replace(lnum, newp, FALSE); |
| 2361 | |
| 2362 | // mark the buffer as changed and prepare for displaying |
LemonBoy | d0b1a09 | 2022-05-12 18:45:18 +0100 | [diff] [blame] | 2363 | inserted_bytes(lnum, curwin->w_cursor.col, -deleted); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2364 | |
| 2365 | // If "fixpos" is TRUE we don't want to end up positioned at the NUL. |
| 2366 | if (fixpos && curwin->w_cursor.col > 0) |
| 2367 | --curwin->w_cursor.col; |
| 2368 | |
| 2369 | return OK; |
| 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * Delete "nlines" lines at the cursor. |
| 2374 | * Saves the lines for undo first if "undo" is TRUE. |
| 2375 | */ |
| 2376 | void |
| 2377 | del_lines(long nlines, int undo) |
| 2378 | { |
| 2379 | long n; |
| 2380 | linenr_T first = curwin->w_cursor.lnum; |
| 2381 | |
| 2382 | if (nlines <= 0) |
| 2383 | return; |
| 2384 | |
| 2385 | // save the deleted lines for undo |
| 2386 | if (undo && u_savedel(first, nlines) == FAIL) |
| 2387 | return; |
| 2388 | |
| 2389 | for (n = 0; n < nlines; ) |
| 2390 | { |
| 2391 | if (curbuf->b_ml.ml_flags & ML_EMPTY) // nothing to delete |
| 2392 | break; |
| 2393 | |
Bram Moolenaar | ca70c07 | 2020-05-30 20:30:46 +0200 | [diff] [blame] | 2394 | ml_delete_flags(first, ML_DEL_MESSAGE); |
Bram Moolenaar | 3f86ca0 | 2019-05-11 18:30:00 +0200 | [diff] [blame] | 2395 | ++n; |
| 2396 | |
| 2397 | // If we delete the last line in the file, stop |
| 2398 | if (first > curbuf->b_ml.ml_line_count) |
| 2399 | break; |
| 2400 | } |
| 2401 | |
| 2402 | // Correct the cursor position before calling deleted_lines_mark(), it may |
| 2403 | // trigger a callback to display the cursor. |
| 2404 | curwin->w_cursor.col = 0; |
| 2405 | check_cursor_lnum(); |
| 2406 | |
| 2407 | // adjust marks, mark the buffer as changed and prepare for displaying |
| 2408 | deleted_lines_mark(first, n); |
| 2409 | } |