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