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