Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 | * edit.c: functions for Insert mode |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #ifdef FEAT_INS_EXPAND |
| 17 | /* |
| 18 | * definitions used for CTRL-X submode |
| 19 | */ |
| 20 | #define CTRL_X_WANT_IDENT 0x100 |
| 21 | |
| 22 | #define CTRL_X_NOT_DEFINED_YET 1 |
| 23 | #define CTRL_X_SCROLL 2 |
| 24 | #define CTRL_X_WHOLE_LINE 3 |
| 25 | #define CTRL_X_FILES 4 |
| 26 | #define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT) |
| 27 | #define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT) |
| 28 | #define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT) |
| 29 | #define CTRL_X_FINISHED 8 |
| 30 | #define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT) |
| 31 | #define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT) |
| 32 | #define CTRL_X_CMDLINE 11 |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 33 | #define CTRL_X_FUNCTION 12 |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 34 | #define CTRL_X_OMNI 13 |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 35 | #define CTRL_X_SPELL 14 |
| 36 | #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] |
| 39 | |
| 40 | static char *ctrl_x_msgs[] = |
| 41 | { |
| 42 | N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 43 | N_(" ^X mode (^]^D^E^F^I^K^L^N^O^P^S^U^V^Y)"), |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 44 | NULL, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | N_(" Whole line completion (^L^N^P)"), |
| 46 | N_(" File name completion (^F^N^P)"), |
| 47 | N_(" Tag completion (^]^N^P)"), |
| 48 | N_(" Path pattern completion (^N^P)"), |
| 49 | N_(" Definition completion (^D^N^P)"), |
| 50 | NULL, |
| 51 | N_(" Dictionary completion (^K^N^P)"), |
| 52 | N_(" Thesaurus completion (^T^N^P)"), |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 53 | N_(" Command-line completion (^V^N^P)"), |
| 54 | N_(" User defined completion (^U^N^P)"), |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 55 | N_(" Omni completion (^O^N^P)"), |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 56 | N_(" Spelling suggestion (^S^N^P)"), |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 57 | N_(" Keyword Local completion (^N^P)"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | static char_u e_hitend[] = N_("Hit end of paragraph"); |
| 61 | |
| 62 | /* |
| 63 | * Structure used to store one match for insert completion. |
| 64 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 65 | typedef struct Completion compl_T; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | struct Completion |
| 67 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 68 | compl_T *cp_next; |
| 69 | compl_T *cp_prev; |
| 70 | char_u *cp_str; /* matched text */ |
| 71 | char_u *cp_fname; /* file containing the match */ |
| 72 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 73 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 76 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | #define FREE_FNAME (2) |
| 78 | |
| 79 | /* |
| 80 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 81 | * "compl_first_match" points to the start of the list. |
| 82 | * "compl_curr_match" points to the currently selected entry. |
| 83 | * "compl_shown_match" is different from compl_curr_match during |
| 84 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 85 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 86 | static compl_T *compl_first_match = NULL; |
| 87 | static compl_T *compl_curr_match = NULL; |
| 88 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 90 | /* When the first completion is done "compl_started" is set. When it's |
| 91 | * FALSE the word to be completed must be located. */ |
| 92 | static int compl_started = FALSE; |
| 93 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 94 | static int compl_matches = 0; |
| 95 | static char_u *compl_pattern = NULL; |
| 96 | static int compl_direction = FORWARD; |
| 97 | static int compl_shows_dir = FORWARD; |
| 98 | static int compl_pending = FALSE; |
| 99 | static pos_T compl_startpos; |
| 100 | static colnr_T compl_col = 0; /* column where the text starts |
| 101 | * that is being completed */ |
| 102 | static int save_sm = -1; |
| 103 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 104 | * completion started */ |
| 105 | static int compl_cont_mode = 0; |
| 106 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 108 | static void ins_ctrl_x __ARGS((void)); |
| 109 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 110 | static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int dir)); |
| 111 | static int ins_compl_make_cyclic __ARGS((void)); |
| 112 | static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int dir, int flags, int thesaurus)); |
| 113 | static void ins_compl_free __ARGS((void)); |
| 114 | static void ins_compl_clear __ARGS((void)); |
| 115 | static void ins_compl_prep __ARGS((int c)); |
| 116 | static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag)); |
| 117 | static int ins_compl_get_exp __ARGS((pos_T *ini, int dir)); |
| 118 | static void ins_compl_delete __ARGS((void)); |
| 119 | static void ins_compl_insert __ARGS((void)); |
| 120 | static int ins_compl_next __ARGS((int allow_get_expansion)); |
| 121 | static int ins_complete __ARGS((int c)); |
| 122 | static int quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
| 123 | #endif /* FEAT_INS_EXPAND */ |
| 124 | |
| 125 | #define BACKSPACE_CHAR 1 |
| 126 | #define BACKSPACE_WORD 2 |
| 127 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 128 | #define BACKSPACE_LINE 4 |
| 129 | |
| 130 | static void ins_redraw __ARGS((void)); |
| 131 | static void ins_ctrl_v __ARGS((void)); |
| 132 | static void undisplay_dollar __ARGS((void)); |
| 133 | static void insert_special __ARGS((int, int, int)); |
| 134 | static void check_auto_format __ARGS((int)); |
| 135 | static void redo_literal __ARGS((int c)); |
| 136 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 137 | #ifdef FEAT_SYN_HL |
| 138 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 139 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 140 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 141 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 142 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc)); |
| 143 | static int echeck_abbr __ARGS((int)); |
| 144 | static void replace_push_off __ARGS((int c)); |
| 145 | static int replace_pop __ARGS((void)); |
| 146 | static void replace_join __ARGS((int off)); |
| 147 | static void replace_pop_ins __ARGS((void)); |
| 148 | #ifdef FEAT_MBYTE |
| 149 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 150 | #endif |
| 151 | static void replace_flush __ARGS((void)); |
| 152 | static void replace_do_bs __ARGS((void)); |
| 153 | #ifdef FEAT_CINDENT |
| 154 | static int cindent_on __ARGS((void)); |
| 155 | #endif |
| 156 | static void ins_reg __ARGS((void)); |
| 157 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 158 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 159 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | #ifdef FEAT_RIGHTLEFT |
| 161 | static void ins_ctrl_ __ARGS((void)); |
| 162 | #endif |
| 163 | #ifdef FEAT_VISUAL |
| 164 | static int ins_start_select __ARGS((int c)); |
| 165 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 166 | static void ins_insert __ARGS((int replaceState)); |
| 167 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | static void ins_shift __ARGS((int c, int lastc)); |
| 169 | static void ins_del __ARGS((void)); |
| 170 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 171 | #ifdef FEAT_MOUSE |
| 172 | static void ins_mouse __ARGS((int c)); |
| 173 | static void ins_mousescroll __ARGS((int up)); |
| 174 | #endif |
| 175 | static void ins_left __ARGS((void)); |
| 176 | static void ins_home __ARGS((int c)); |
| 177 | static void ins_end __ARGS((int c)); |
| 178 | static void ins_s_left __ARGS((void)); |
| 179 | static void ins_right __ARGS((void)); |
| 180 | static void ins_s_right __ARGS((void)); |
| 181 | static void ins_up __ARGS((int startcol)); |
| 182 | static void ins_pageup __ARGS((void)); |
| 183 | static void ins_down __ARGS((int startcol)); |
| 184 | static void ins_pagedown __ARGS((void)); |
| 185 | #ifdef FEAT_DND |
| 186 | static void ins_drop __ARGS((void)); |
| 187 | #endif |
| 188 | static int ins_tab __ARGS((void)); |
| 189 | static int ins_eol __ARGS((int c)); |
| 190 | #ifdef FEAT_DIGRAPHS |
| 191 | static int ins_digraph __ARGS((void)); |
| 192 | #endif |
| 193 | static int ins_copychar __ARGS((linenr_T lnum)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 194 | static int ins_ctrl_ey __ARGS((int tc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | #ifdef FEAT_SMARTINDENT |
| 196 | static void ins_try_si __ARGS((int c)); |
| 197 | #endif |
| 198 | static colnr_T get_nolist_virtcol __ARGS((void)); |
| 199 | |
| 200 | static colnr_T Insstart_textlen; /* length of line when insert started */ |
| 201 | static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ |
| 202 | |
| 203 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 204 | K_SPECIAL and CSI are escaped */ |
| 205 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 206 | static int new_insert_skip; /* nr of chars in front of current insert */ |
| 207 | |
| 208 | #ifdef FEAT_CINDENT |
| 209 | static int can_cindent; /* may do cindenting on this line */ |
| 210 | #endif |
| 211 | |
| 212 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 213 | |
| 214 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 215 | static int revins_on; /* reverse insert mode on */ |
| 216 | static int revins_chars; /* how much to skip after edit */ |
| 217 | static int revins_legal; /* was the last char 'legal'? */ |
| 218 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 219 | #endif |
| 220 | |
| 221 | #if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC) |
| 222 | static short previous_script = smRoman; |
| 223 | #endif |
| 224 | |
| 225 | static int ins_need_undo; /* call u_save() before inserting a |
| 226 | char. Set when edit() is called. |
| 227 | after that arrow_used is used. */ |
| 228 | |
| 229 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 230 | under the cursor */ |
| 231 | |
| 232 | /* |
| 233 | * edit(): Start inserting text. |
| 234 | * |
| 235 | * "cmdchar" can be: |
| 236 | * 'i' normal insert command |
| 237 | * 'a' normal append command |
| 238 | * 'R' replace command |
| 239 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 240 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 241 | * 'g' "gI" command. |
| 242 | * 'V' "gR" command for Virtual Replace mode. |
| 243 | * 'v' "gr" command for single character Virtual Replace mode. |
| 244 | * |
| 245 | * This function is not called recursively. For CTRL-O commands, it returns |
| 246 | * and lets the caller handle the Normal-mode command. |
| 247 | * |
| 248 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 249 | */ |
| 250 | int |
| 251 | edit(cmdchar, startln, count) |
| 252 | int cmdchar; |
| 253 | int startln; /* if set, insert at start of line */ |
| 254 | long count; |
| 255 | { |
| 256 | int c = 0; |
| 257 | char_u *ptr; |
| 258 | int lastc; |
| 259 | colnr_T mincol; |
| 260 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | int i; |
| 262 | int did_backspace = TRUE; /* previous char was backspace */ |
| 263 | #ifdef FEAT_CINDENT |
| 264 | int line_is_white = FALSE; /* line is empty before insert */ |
| 265 | #endif |
| 266 | linenr_T old_topline = 0; /* topline before insertion */ |
| 267 | #ifdef FEAT_DIFF |
| 268 | int old_topfill = -1; |
| 269 | #endif |
| 270 | int inserted_space = FALSE; /* just inserted a space */ |
| 271 | int replaceState = REPLACE; |
| 272 | int did_restart_edit = restart_edit; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 273 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | |
| 275 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 276 | * error message */ |
| 277 | check_for_delay(TRUE); |
| 278 | |
| 279 | #ifdef HAVE_SANDBOX |
| 280 | /* Don't allow inserting in the sandbox. */ |
| 281 | if (sandbox != 0) |
| 282 | { |
| 283 | EMSG(_(e_sandbox)); |
| 284 | return FALSE; |
| 285 | } |
| 286 | #endif |
| 287 | |
| 288 | #ifdef FEAT_INS_EXPAND |
| 289 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 290 | #endif |
| 291 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 292 | #ifdef FEAT_AUTOCMD |
| 293 | /* |
| 294 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 295 | */ |
| 296 | if (cmdchar != 'r' && cmdchar != 'v') |
| 297 | { |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 298 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 299 | if (cmdchar == 'R') |
| 300 | ptr = (char_u *)"r"; |
| 301 | else if (cmdchar == 'V') |
| 302 | ptr = (char_u *)"v"; |
| 303 | else |
| 304 | ptr = (char_u *)"i"; |
| 305 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 306 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 307 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
| 308 | } |
| 309 | #endif |
| 310 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 311 | #ifdef FEAT_MOUSE |
| 312 | /* |
| 313 | * When doing a paste with the middle mouse button, Insstart is set to |
| 314 | * where the paste started. |
| 315 | */ |
| 316 | if (where_paste_started.lnum != 0) |
| 317 | Insstart = where_paste_started; |
| 318 | else |
| 319 | #endif |
| 320 | { |
| 321 | Insstart = curwin->w_cursor; |
| 322 | if (startln) |
| 323 | Insstart.col = 0; |
| 324 | } |
| 325 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 326 | Insstart_blank_vcol = MAXCOL; |
| 327 | if (!did_ai) |
| 328 | ai_col = 0; |
| 329 | |
| 330 | if (cmdchar != NUL && restart_edit == 0) |
| 331 | { |
| 332 | ResetRedobuff(); |
| 333 | AppendNumberToRedobuff(count); |
| 334 | #ifdef FEAT_VREPLACE |
| 335 | if (cmdchar == 'V' || cmdchar == 'v') |
| 336 | { |
| 337 | /* "gR" or "gr" command */ |
| 338 | AppendCharToRedobuff('g'); |
| 339 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 340 | } |
| 341 | else |
| 342 | #endif |
| 343 | { |
| 344 | AppendCharToRedobuff(cmdchar); |
| 345 | if (cmdchar == 'g') /* "gI" command */ |
| 346 | AppendCharToRedobuff('I'); |
| 347 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 348 | count = 1; /* insert only one <CR> */ |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | if (cmdchar == 'R') |
| 353 | { |
| 354 | #ifdef FEAT_FKMAP |
| 355 | if (p_fkmap && p_ri) |
| 356 | { |
| 357 | beep_flush(); |
| 358 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 359 | State = INSERT; |
| 360 | } |
| 361 | else |
| 362 | #endif |
| 363 | State = REPLACE; |
| 364 | } |
| 365 | #ifdef FEAT_VREPLACE |
| 366 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 367 | { |
| 368 | State = VREPLACE; |
| 369 | replaceState = VREPLACE; |
| 370 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 371 | vr_lines_changed = 1; |
| 372 | } |
| 373 | #endif |
| 374 | else |
| 375 | State = INSERT; |
| 376 | |
| 377 | stop_insert_mode = FALSE; |
| 378 | |
| 379 | /* |
| 380 | * Need to recompute the cursor position, it might move when the cursor is |
| 381 | * on a TAB or special character. |
| 382 | */ |
| 383 | curs_columns(TRUE); |
| 384 | |
| 385 | /* |
| 386 | * Enable langmap or IME, indicated by 'iminsert'. |
| 387 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 388 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 389 | * when hitting <Esc>. |
| 390 | */ |
| 391 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 392 | State |= LANGMAP; |
| 393 | #ifdef USE_IM_CONTROL |
| 394 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 395 | #endif |
| 396 | |
| 397 | #if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC) |
| 398 | KeyScript(previous_script); |
| 399 | #endif |
| 400 | |
| 401 | #ifdef FEAT_MOUSE |
| 402 | setmouse(); |
| 403 | #endif |
| 404 | #ifdef FEAT_CMDL_INFO |
| 405 | clear_showcmd(); |
| 406 | #endif |
| 407 | #ifdef FEAT_RIGHTLEFT |
| 408 | /* there is no reverse replace mode */ |
| 409 | revins_on = (State == INSERT && p_ri); |
| 410 | if (revins_on) |
| 411 | undisplay_dollar(); |
| 412 | revins_chars = 0; |
| 413 | revins_legal = 0; |
| 414 | revins_scol = -1; |
| 415 | #endif |
| 416 | |
| 417 | /* |
| 418 | * Handle restarting Insert mode. |
| 419 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 420 | * restart_edit non-zero, and something in the stuff buffer. |
| 421 | */ |
| 422 | if (restart_edit != 0 && stuff_empty()) |
| 423 | { |
| 424 | #ifdef FEAT_MOUSE |
| 425 | /* |
| 426 | * After a paste we consider text typed to be part of the insert for |
| 427 | * the pasted text. You can backspace over the pasted text too. |
| 428 | */ |
| 429 | if (where_paste_started.lnum) |
| 430 | arrow_used = FALSE; |
| 431 | else |
| 432 | #endif |
| 433 | arrow_used = TRUE; |
| 434 | restart_edit = 0; |
| 435 | |
| 436 | /* |
| 437 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 438 | * now at the end-of-line, put it after the end-of-line (this is not |
| 439 | * correct in very rare cases). |
| 440 | * Also do this if curswant is greater than the current virtual |
| 441 | * column. Eg after "^O$" or "^O80|". |
| 442 | */ |
| 443 | validate_virtcol(); |
| 444 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 445 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 446 | || curwin->w_curswant > curwin->w_virtcol) |
| 447 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 448 | { |
| 449 | if (ptr[1] == NUL) |
| 450 | ++curwin->w_cursor.col; |
| 451 | #ifdef FEAT_MBYTE |
| 452 | else if (has_mbyte) |
| 453 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 454 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 455 | if (ptr[i] == NUL) |
| 456 | curwin->w_cursor.col += i; |
| 457 | } |
| 458 | #endif |
| 459 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 460 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 461 | } |
| 462 | else |
| 463 | arrow_used = FALSE; |
| 464 | |
| 465 | /* we are in insert mode now, don't need to start it anymore */ |
| 466 | need_start_insertmode = FALSE; |
| 467 | |
| 468 | /* Need to save the line for undo before inserting the first char. */ |
| 469 | ins_need_undo = TRUE; |
| 470 | |
| 471 | #ifdef FEAT_MOUSE |
| 472 | where_paste_started.lnum = 0; |
| 473 | #endif |
| 474 | #ifdef FEAT_CINDENT |
| 475 | can_cindent = TRUE; |
| 476 | #endif |
| 477 | #ifdef FEAT_FOLDING |
| 478 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 479 | * restarting. */ |
| 480 | if (!p_im && did_restart_edit == 0) |
| 481 | foldOpenCursor(); |
| 482 | #endif |
| 483 | |
| 484 | /* |
| 485 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 486 | * A warning message for changing a readonly file is given here, before |
| 487 | * actually changing anything. It's put after the mode, if any. |
| 488 | */ |
| 489 | i = 0; |
| 490 | if (p_smd) |
| 491 | i = showmode(); |
| 492 | |
| 493 | if (!p_im && did_restart_edit == 0) |
| 494 | change_warning(i + 1); |
| 495 | |
| 496 | #ifdef CURSOR_SHAPE |
| 497 | ui_cursor_shape(); /* may show different cursor shape */ |
| 498 | #endif |
| 499 | #ifdef FEAT_DIGRAPHS |
| 500 | do_digraph(-1); /* clear digraphs */ |
| 501 | #endif |
| 502 | |
| 503 | /* |
| 504 | * Get the current length of the redo buffer, those characters have to be |
| 505 | * skipped if we want to get to the inserted characters. |
| 506 | */ |
| 507 | ptr = get_inserted(); |
| 508 | if (ptr == NULL) |
| 509 | new_insert_skip = 0; |
| 510 | else |
| 511 | { |
| 512 | new_insert_skip = (int)STRLEN(ptr); |
| 513 | vim_free(ptr); |
| 514 | } |
| 515 | |
| 516 | old_indent = 0; |
| 517 | |
| 518 | /* |
| 519 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 520 | */ |
| 521 | for (;;) |
| 522 | { |
| 523 | #ifdef FEAT_RIGHTLEFT |
| 524 | if (!revins_legal) |
| 525 | revins_scol = -1; /* reset on illegal motions */ |
| 526 | else |
| 527 | revins_legal = 0; |
| 528 | #endif |
| 529 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 530 | count = 0; |
| 531 | |
| 532 | if (stop_insert_mode) |
| 533 | { |
| 534 | /* ":stopinsert" used or 'insertmode' reset */ |
| 535 | count = 0; |
| 536 | goto doESCkey; |
| 537 | } |
| 538 | |
| 539 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 540 | if (!arrow_used) |
| 541 | curwin->w_set_curswant = TRUE; |
| 542 | |
| 543 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 544 | * menu invoked a shell command). */ |
| 545 | if (stuff_empty()) |
| 546 | { |
| 547 | did_check_timestamps = FALSE; |
| 548 | if (need_check_timestamps) |
| 549 | check_timestamps(FALSE); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * When emsg() was called msg_scroll will have been set. |
| 554 | */ |
| 555 | msg_scroll = FALSE; |
| 556 | |
| 557 | #ifdef FEAT_GUI |
| 558 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 559 | * another window. "need_mouse_correct" may then be set because of an |
| 560 | * autocommand. */ |
| 561 | if (need_mouse_correct) |
| 562 | gui_mouse_correct(); |
| 563 | #endif |
| 564 | |
| 565 | #ifdef FEAT_FOLDING |
| 566 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 567 | if (fdo_flags & FDO_INSERT) |
| 568 | foldOpenCursor(); |
| 569 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 570 | if (!char_avail()) |
| 571 | foldCheckClose(); |
| 572 | #endif |
| 573 | |
| 574 | /* |
| 575 | * If we inserted a character at the last position of the last line in |
| 576 | * the window, scroll the window one line up. This avoids an extra |
| 577 | * redraw. |
| 578 | * This is detected when the cursor column is smaller after inserting |
| 579 | * something. |
| 580 | * Don't do this when the topline changed already, it has |
| 581 | * already been adjusted (by insertchar() calling open_line())). |
| 582 | */ |
| 583 | if (curbuf->b_mod_set |
| 584 | && curwin->w_p_wrap |
| 585 | && !did_backspace |
| 586 | && curwin->w_topline == old_topline |
| 587 | #ifdef FEAT_DIFF |
| 588 | && curwin->w_topfill == old_topfill |
| 589 | #endif |
| 590 | ) |
| 591 | { |
| 592 | mincol = curwin->w_wcol; |
| 593 | validate_cursor_col(); |
| 594 | |
| 595 | if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts |
| 596 | && curwin->w_wrow == W_WINROW(curwin) |
| 597 | + curwin->w_height - 1 - p_so |
| 598 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 599 | #ifdef FEAT_DIFF |
| 600 | || curwin->w_topfill > 0 |
| 601 | #endif |
| 602 | )) |
| 603 | { |
| 604 | #ifdef FEAT_DIFF |
| 605 | if (curwin->w_topfill > 0) |
| 606 | --curwin->w_topfill; |
| 607 | else |
| 608 | #endif |
| 609 | #ifdef FEAT_FOLDING |
| 610 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 611 | set_topline(curwin, old_topline + 1); |
| 612 | else |
| 613 | #endif |
| 614 | set_topline(curwin, curwin->w_topline + 1); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* May need to adjust w_topline to show the cursor. */ |
| 619 | update_topline(); |
| 620 | |
| 621 | did_backspace = FALSE; |
| 622 | |
| 623 | validate_cursor(); /* may set must_redraw */ |
| 624 | |
| 625 | /* |
| 626 | * Redraw the display when no characters are waiting. |
| 627 | * Also shows mode, ruler and positions cursor. |
| 628 | */ |
| 629 | ins_redraw(); |
| 630 | |
| 631 | #ifdef FEAT_SCROLLBIND |
| 632 | if (curwin->w_p_scb) |
| 633 | do_check_scrollbind(TRUE); |
| 634 | #endif |
| 635 | |
| 636 | update_curswant(); |
| 637 | old_topline = curwin->w_topline; |
| 638 | #ifdef FEAT_DIFF |
| 639 | old_topfill = curwin->w_topfill; |
| 640 | #endif |
| 641 | |
| 642 | #ifdef USE_ON_FLY_SCROLL |
| 643 | dont_scroll = FALSE; /* allow scrolling here */ |
| 644 | #endif |
| 645 | |
| 646 | /* |
| 647 | * Get a character for Insert mode. |
| 648 | */ |
| 649 | lastc = c; /* remember previous char for CTRL-D */ |
| 650 | c = safe_vgetc(); |
| 651 | |
| 652 | #ifdef FEAT_RIGHTLEFT |
| 653 | if (p_hkmap && KeyTyped) |
| 654 | c = hkmap(c); /* Hebrew mode mapping */ |
| 655 | #endif |
| 656 | #ifdef FEAT_FKMAP |
| 657 | if (p_fkmap && KeyTyped) |
| 658 | c = fkmap(c); /* Farsi mode mapping */ |
| 659 | #endif |
| 660 | |
| 661 | #ifdef FEAT_INS_EXPAND |
| 662 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 663 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 664 | if (c != K_IGNORE) |
| 665 | ins_compl_prep(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | #endif |
| 667 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 668 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 669 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 670 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 671 | if (c == Ctrl_BSL) |
| 672 | { |
| 673 | /* may need to redraw when no more chars available now */ |
| 674 | ins_redraw(); |
| 675 | ++no_mapping; |
| 676 | ++allow_keys; |
| 677 | c = safe_vgetc(); |
| 678 | --no_mapping; |
| 679 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 680 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 681 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 682 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 683 | vungetc(c); |
| 684 | c = Ctrl_BSL; |
| 685 | } |
| 686 | else if (c == Ctrl_G && p_im) |
| 687 | continue; |
| 688 | else |
| 689 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 690 | if (c == Ctrl_O) |
| 691 | { |
| 692 | ins_ctrl_o(); |
| 693 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 694 | nomove = TRUE; |
| 695 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 696 | count = 0; |
| 697 | goto doESCkey; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | #ifdef FEAT_DIGRAPHS |
| 702 | c = do_digraph(c); |
| 703 | #endif |
| 704 | |
| 705 | #ifdef FEAT_INS_EXPAND |
| 706 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 707 | goto docomplete; |
| 708 | #endif |
| 709 | if (c == Ctrl_V || c == Ctrl_Q) |
| 710 | { |
| 711 | ins_ctrl_v(); |
| 712 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | #ifdef FEAT_CINDENT |
| 717 | if (cindent_on() |
| 718 | # ifdef FEAT_INS_EXPAND |
| 719 | && ctrl_x_mode == 0 |
| 720 | # endif |
| 721 | ) |
| 722 | { |
| 723 | /* A key name preceded by a bang means this key is not to be |
| 724 | * inserted. Skip ahead to the re-indenting below. |
| 725 | * A key name preceded by a star means that indenting has to be |
| 726 | * done before inserting the key. */ |
| 727 | line_is_white = inindent(0); |
| 728 | if (in_cinkeys(c, '!', line_is_white)) |
| 729 | goto force_cindent; |
| 730 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 731 | && stop_arrow() == OK) |
| 732 | do_c_expr_indent(); |
| 733 | } |
| 734 | #endif |
| 735 | |
| 736 | #ifdef FEAT_RIGHTLEFT |
| 737 | if (curwin->w_p_rl) |
| 738 | switch (c) |
| 739 | { |
| 740 | case K_LEFT: c = K_RIGHT; break; |
| 741 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 742 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 743 | case K_RIGHT: c = K_LEFT; break; |
| 744 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 745 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 746 | } |
| 747 | #endif |
| 748 | |
| 749 | #ifdef FEAT_VISUAL |
| 750 | /* |
| 751 | * If 'keymodel' contains "startsel", may start selection. If it |
| 752 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 753 | * characters. |
| 754 | */ |
| 755 | if (ins_start_select(c)) |
| 756 | continue; |
| 757 | #endif |
| 758 | |
| 759 | /* |
| 760 | * The big switch to handle a character in insert mode. |
| 761 | */ |
| 762 | switch (c) |
| 763 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 764 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 765 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 766 | break; |
| 767 | /*FALLTHROUGH*/ |
| 768 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 769 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | #ifdef FEAT_CMDWIN |
| 771 | if (c == Ctrl_C && cmdwin_type != 0) |
| 772 | { |
| 773 | /* Close the cmdline window. */ |
| 774 | cmdwin_result = K_IGNORE; |
| 775 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
| 776 | goto doESCkey; |
| 777 | } |
| 778 | #endif |
| 779 | |
| 780 | #ifdef UNIX |
| 781 | do_intr: |
| 782 | #endif |
| 783 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 784 | * Insert mode */ |
| 785 | if (goto_im()) |
| 786 | { |
| 787 | if (got_int) |
| 788 | { |
| 789 | (void)vgetc(); /* flush all buffers */ |
| 790 | got_int = FALSE; |
| 791 | } |
| 792 | else |
| 793 | vim_beep(); |
| 794 | break; |
| 795 | } |
| 796 | doESCkey: |
| 797 | /* |
| 798 | * This is the ONLY return from edit()! |
| 799 | */ |
| 800 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 801 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 802 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 803 | o_lnum = curwin->w_cursor.lnum; |
| 804 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 805 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 806 | { |
| 807 | #ifdef FEAT_AUTOCMD |
| 808 | if (cmdchar != 'r' && cmdchar != 'v') |
| 809 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 810 | FALSE, curbuf); |
| 811 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 812 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 813 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | continue; |
| 815 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 816 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 817 | if (!p_im) |
| 818 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 819 | stuffReadbuff((char_u *)":st\r"); |
| 820 | c = Ctrl_O; |
| 821 | /*FALLTHROUGH*/ |
| 822 | |
| 823 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 824 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 825 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 826 | goto docomplete; |
| 827 | #endif |
| 828 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 829 | break; |
| 830 | ins_ctrl_o(); |
| 831 | count = 0; |
| 832 | goto doESCkey; |
| 833 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 834 | case K_INS: /* toggle insert/replace mode */ |
| 835 | case K_KINS: |
| 836 | ins_insert(replaceState); |
| 837 | break; |
| 838 | |
| 839 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 840 | break; |
| 841 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 842 | #ifdef FEAT_SNIFF |
| 843 | case K_SNIFF: /* Sniff command received */ |
| 844 | stuffcharReadbuff(K_SNIFF); |
| 845 | goto doESCkey; |
| 846 | #endif |
| 847 | |
| 848 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 849 | case K_F1: |
| 850 | case K_XF1: |
| 851 | stuffcharReadbuff(K_HELP); |
| 852 | if (p_im) |
| 853 | need_start_insertmode = TRUE; |
| 854 | goto doESCkey; |
| 855 | |
| 856 | #ifdef FEAT_NETBEANS_INTG |
| 857 | case K_F21: /* NetBeans command */ |
| 858 | ++no_mapping; /* don't map the next key hits */ |
| 859 | i = safe_vgetc(); |
| 860 | --no_mapping; |
| 861 | netbeans_keycommand(i); |
| 862 | break; |
| 863 | #endif |
| 864 | |
| 865 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 866 | case NUL: |
| 867 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 868 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 869 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 870 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 871 | && c != Ctrl_A && !p_im) |
| 872 | goto doESCkey; /* quit insert mode */ |
| 873 | inserted_space = FALSE; |
| 874 | break; |
| 875 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 876 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 877 | ins_reg(); |
| 878 | auto_format(FALSE, TRUE); |
| 879 | inserted_space = FALSE; |
| 880 | break; |
| 881 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 882 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | ins_ctrl_g(); |
| 884 | break; |
| 885 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 886 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 887 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 888 | break; |
| 889 | |
| 890 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 891 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | if (!p_ari) |
| 893 | goto normalchar; |
| 894 | ins_ctrl_(); |
| 895 | break; |
| 896 | #endif |
| 897 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 898 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 899 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 900 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 901 | goto docomplete; |
| 902 | #endif |
| 903 | /* FALLTHROUGH */ |
| 904 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 905 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 906 | # ifdef FEAT_INS_EXPAND |
| 907 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 908 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 909 | if (has_compl_option(FALSE)) |
| 910 | goto docomplete; |
| 911 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 912 | } |
| 913 | # endif |
| 914 | ins_shift(c, lastc); |
| 915 | auto_format(FALSE, TRUE); |
| 916 | inserted_space = FALSE; |
| 917 | break; |
| 918 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 919 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 920 | case K_KDEL: |
| 921 | ins_del(); |
| 922 | auto_format(FALSE, TRUE); |
| 923 | break; |
| 924 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 925 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 926 | case Ctrl_H: |
| 927 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 928 | auto_format(FALSE, TRUE); |
| 929 | break; |
| 930 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 931 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 932 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 933 | auto_format(FALSE, TRUE); |
| 934 | break; |
| 935 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 936 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 937 | # ifdef FEAT_COMPL_FUNC |
| 938 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 939 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 940 | goto docomplete; |
| 941 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 943 | auto_format(FALSE, TRUE); |
| 944 | inserted_space = FALSE; |
| 945 | break; |
| 946 | |
| 947 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 948 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 949 | case K_LEFTMOUSE_NM: |
| 950 | case K_LEFTDRAG: |
| 951 | case K_LEFTRELEASE: |
| 952 | case K_LEFTRELEASE_NM: |
| 953 | case K_MIDDLEMOUSE: |
| 954 | case K_MIDDLEDRAG: |
| 955 | case K_MIDDLERELEASE: |
| 956 | case K_RIGHTMOUSE: |
| 957 | case K_RIGHTDRAG: |
| 958 | case K_RIGHTRELEASE: |
| 959 | case K_X1MOUSE: |
| 960 | case K_X1DRAG: |
| 961 | case K_X1RELEASE: |
| 962 | case K_X2MOUSE: |
| 963 | case K_X2DRAG: |
| 964 | case K_X2RELEASE: |
| 965 | ins_mouse(c); |
| 966 | break; |
| 967 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 968 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 969 | ins_mousescroll(FALSE); |
| 970 | break; |
| 971 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 972 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 973 | ins_mousescroll(TRUE); |
| 974 | break; |
| 975 | #endif |
| 976 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 977 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 978 | break; |
| 979 | |
| 980 | #ifdef FEAT_GUI |
| 981 | case K_VER_SCROLLBAR: |
| 982 | ins_scroll(); |
| 983 | break; |
| 984 | |
| 985 | case K_HOR_SCROLLBAR: |
| 986 | ins_horscroll(); |
| 987 | break; |
| 988 | #endif |
| 989 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 990 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 991 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 992 | case K_S_HOME: |
| 993 | case K_C_HOME: |
| 994 | ins_home(c); |
| 995 | break; |
| 996 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 997 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 998 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | case K_S_END: |
| 1000 | case K_C_END: |
| 1001 | ins_end(c); |
| 1002 | break; |
| 1003 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1004 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1005 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1006 | ins_s_left(); |
| 1007 | else |
| 1008 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1011 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1012 | case K_C_LEFT: |
| 1013 | ins_s_left(); |
| 1014 | break; |
| 1015 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1016 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1017 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1018 | ins_s_right(); |
| 1019 | else |
| 1020 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1021 | break; |
| 1022 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1023 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | case K_C_RIGHT: |
| 1025 | ins_s_right(); |
| 1026 | break; |
| 1027 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1028 | case K_UP: /* <Up> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1029 | if (mod_mask & MOD_MASK_SHIFT) |
| 1030 | ins_pageup(); |
| 1031 | else |
| 1032 | ins_up(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | break; |
| 1034 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1035 | case K_S_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1036 | case K_PAGEUP: |
| 1037 | case K_KPAGEUP: |
| 1038 | ins_pageup(); |
| 1039 | break; |
| 1040 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1041 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1042 | if (mod_mask & MOD_MASK_SHIFT) |
| 1043 | ins_pagedown(); |
| 1044 | else |
| 1045 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1046 | break; |
| 1047 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1048 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1049 | case K_PAGEDOWN: |
| 1050 | case K_KPAGEDOWN: |
| 1051 | ins_pagedown(); |
| 1052 | break; |
| 1053 | |
| 1054 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1055 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | ins_drop(); |
| 1057 | break; |
| 1058 | #endif |
| 1059 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1060 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1061 | c = TAB; |
| 1062 | /* FALLTHROUGH */ |
| 1063 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1064 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1065 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1066 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1067 | goto docomplete; |
| 1068 | #endif |
| 1069 | inserted_space = FALSE; |
| 1070 | if (ins_tab()) |
| 1071 | goto normalchar; /* insert TAB as a normal char */ |
| 1072 | auto_format(FALSE, TRUE); |
| 1073 | break; |
| 1074 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1075 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1076 | c = CAR; |
| 1077 | /* FALLTHROUGH */ |
| 1078 | case CAR: |
| 1079 | case NL: |
| 1080 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1081 | /* In a quickfix window a <CR> jumps to the error under the |
| 1082 | * cursor. */ |
| 1083 | if (bt_quickfix(curbuf) && c == CAR) |
| 1084 | { |
| 1085 | do_cmdline_cmd((char_u *)".cc"); |
| 1086 | break; |
| 1087 | } |
| 1088 | #endif |
| 1089 | #ifdef FEAT_CMDWIN |
| 1090 | if (cmdwin_type != 0) |
| 1091 | { |
| 1092 | /* Execute the command in the cmdline window. */ |
| 1093 | cmdwin_result = CAR; |
| 1094 | goto doESCkey; |
| 1095 | } |
| 1096 | #endif |
| 1097 | if (ins_eol(c) && !p_im) |
| 1098 | goto doESCkey; /* out of memory */ |
| 1099 | auto_format(FALSE, FALSE); |
| 1100 | inserted_space = FALSE; |
| 1101 | break; |
| 1102 | |
| 1103 | #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1104 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | # ifdef FEAT_INS_EXPAND |
| 1106 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1107 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1108 | if (has_compl_option(TRUE)) |
| 1109 | goto docomplete; |
| 1110 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1111 | } |
| 1112 | # endif |
| 1113 | # ifdef FEAT_DIGRAPHS |
| 1114 | c = ins_digraph(); |
| 1115 | if (c == NUL) |
| 1116 | break; |
| 1117 | # endif |
| 1118 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1119 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1120 | |
| 1121 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1122 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1123 | ins_ctrl_x(); |
| 1124 | break; |
| 1125 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1126 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1127 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1128 | goto normalchar; |
| 1129 | goto docomplete; |
| 1130 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1131 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1133 | goto normalchar; |
| 1134 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1135 | |
| 1136 | case 's': /* Spelling completion after ^X */ |
| 1137 | case Ctrl_S: |
| 1138 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1139 | goto normalchar; |
| 1140 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1141 | #endif |
| 1142 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1143 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1144 | #ifdef FEAT_INS_EXPAND |
| 1145 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1146 | #endif |
| 1147 | { |
| 1148 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1149 | if (p_im) |
| 1150 | { |
| 1151 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1152 | break; |
| 1153 | goto doESCkey; |
| 1154 | } |
| 1155 | goto normalchar; |
| 1156 | } |
| 1157 | #ifdef FEAT_INS_EXPAND |
| 1158 | /* FALLTHROUGH */ |
| 1159 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1160 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | case Ctrl_N: |
| 1162 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1163 | * but it is under other ^X modes */ |
| 1164 | if (*curbuf->b_p_cpt == NUL |
| 1165 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1166 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1167 | goto normalchar; |
| 1168 | |
| 1169 | docomplete: |
| 1170 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1171 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1172 | break; |
| 1173 | #endif /* FEAT_INS_EXPAND */ |
| 1174 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1175 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1176 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1177 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | |
| 1180 | default: |
| 1181 | #ifdef UNIX |
| 1182 | if (c == intr_char) /* special interrupt char */ |
| 1183 | goto do_intr; |
| 1184 | #endif |
| 1185 | |
| 1186 | /* |
| 1187 | * Insert a nomal character. |
| 1188 | */ |
| 1189 | normalchar: |
| 1190 | #ifdef FEAT_SMARTINDENT |
| 1191 | /* Try to perform smart-indenting. */ |
| 1192 | ins_try_si(c); |
| 1193 | #endif |
| 1194 | |
| 1195 | if (c == ' ') |
| 1196 | { |
| 1197 | inserted_space = TRUE; |
| 1198 | #ifdef FEAT_CINDENT |
| 1199 | if (inindent(0)) |
| 1200 | can_cindent = FALSE; |
| 1201 | #endif |
| 1202 | if (Insstart_blank_vcol == MAXCOL |
| 1203 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1204 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1205 | } |
| 1206 | |
| 1207 | if (vim_iswordc(c) || !echeck_abbr( |
| 1208 | #ifdef FEAT_MBYTE |
| 1209 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1210 | * what check_abbr() expects. */ |
| 1211 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1212 | #endif |
| 1213 | c)) |
| 1214 | { |
| 1215 | insert_special(c, FALSE, FALSE); |
| 1216 | #ifdef FEAT_RIGHTLEFT |
| 1217 | revins_legal++; |
| 1218 | revins_chars++; |
| 1219 | #endif |
| 1220 | } |
| 1221 | |
| 1222 | auto_format(FALSE, TRUE); |
| 1223 | |
| 1224 | #ifdef FEAT_FOLDING |
| 1225 | /* When inserting a character the cursor line must never be in a |
| 1226 | * closed fold. */ |
| 1227 | foldOpenCursor(); |
| 1228 | #endif |
| 1229 | break; |
| 1230 | } /* end of switch (c) */ |
| 1231 | |
| 1232 | /* If the cursor was moved we didn't just insert a space */ |
| 1233 | if (arrow_used) |
| 1234 | inserted_space = FALSE; |
| 1235 | |
| 1236 | #ifdef FEAT_CINDENT |
| 1237 | if (can_cindent && cindent_on() |
| 1238 | # ifdef FEAT_INS_EXPAND |
| 1239 | && ctrl_x_mode == 0 |
| 1240 | # endif |
| 1241 | ) |
| 1242 | { |
| 1243 | force_cindent: |
| 1244 | /* |
| 1245 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1246 | */ |
| 1247 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1248 | { |
| 1249 | if (stop_arrow() == OK) |
| 1250 | /* re-indent the current line */ |
| 1251 | do_c_expr_indent(); |
| 1252 | } |
| 1253 | } |
| 1254 | #endif /* FEAT_CINDENT */ |
| 1255 | |
| 1256 | } /* for (;;) */ |
| 1257 | /* NOTREACHED */ |
| 1258 | } |
| 1259 | |
| 1260 | /* |
| 1261 | * Redraw for Insert mode. |
| 1262 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1263 | * option work correctly. |
| 1264 | * Only redraw when there are no characters available. This speeds up |
| 1265 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1266 | */ |
| 1267 | static void |
| 1268 | ins_redraw() |
| 1269 | { |
| 1270 | if (!char_avail()) |
| 1271 | { |
| 1272 | if (must_redraw) |
| 1273 | update_screen(0); |
| 1274 | else if (clear_cmdline || redraw_cmdline) |
| 1275 | showmode(); /* clear cmdline and show mode */ |
| 1276 | showruler(FALSE); |
| 1277 | setcursor(); |
| 1278 | emsg_on_display = FALSE; /* may remove error message now */ |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1284 | */ |
| 1285 | static void |
| 1286 | ins_ctrl_v() |
| 1287 | { |
| 1288 | int c; |
| 1289 | |
| 1290 | /* may need to redraw when no more chars available now */ |
| 1291 | ins_redraw(); |
| 1292 | |
| 1293 | if (redrawing() && !char_avail()) |
| 1294 | edit_putchar('^', TRUE); |
| 1295 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1296 | |
| 1297 | #ifdef FEAT_CMDL_INFO |
| 1298 | add_to_showcmd_c(Ctrl_V); |
| 1299 | #endif |
| 1300 | |
| 1301 | c = get_literal(); |
| 1302 | #ifdef FEAT_CMDL_INFO |
| 1303 | clear_showcmd(); |
| 1304 | #endif |
| 1305 | insert_special(c, FALSE, TRUE); |
| 1306 | #ifdef FEAT_RIGHTLEFT |
| 1307 | revins_chars++; |
| 1308 | revins_legal++; |
| 1309 | #endif |
| 1310 | } |
| 1311 | |
| 1312 | /* |
| 1313 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1314 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1315 | */ |
| 1316 | static int pc_status; |
| 1317 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1318 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1319 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1320 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
| 1321 | #ifdef FEAT_MBYTE |
| 1322 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
| 1323 | #else |
| 1324 | static char_u pc_bytes[2]; /* saved bytes */ |
| 1325 | #endif |
| 1326 | static int pc_attr; |
| 1327 | static int pc_row; |
| 1328 | static int pc_col; |
| 1329 | |
| 1330 | void |
| 1331 | edit_putchar(c, highlight) |
| 1332 | int c; |
| 1333 | int highlight; |
| 1334 | { |
| 1335 | int attr; |
| 1336 | |
| 1337 | if (ScreenLines != NULL) |
| 1338 | { |
| 1339 | update_topline(); /* just in case w_topline isn't valid */ |
| 1340 | validate_cursor(); |
| 1341 | if (highlight) |
| 1342 | attr = hl_attr(HLF_8); |
| 1343 | else |
| 1344 | attr = 0; |
| 1345 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1346 | pc_col = W_WINCOL(curwin); |
| 1347 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1348 | pc_status = PC_STATUS_UNSET; |
| 1349 | #endif |
| 1350 | #ifdef FEAT_RIGHTLEFT |
| 1351 | if (curwin->w_p_rl) |
| 1352 | { |
| 1353 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1354 | # ifdef FEAT_MBYTE |
| 1355 | if (has_mbyte) |
| 1356 | { |
| 1357 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1358 | |
| 1359 | if (fix_col != pc_col) |
| 1360 | { |
| 1361 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1362 | --curwin->w_wcol; |
| 1363 | pc_status = PC_STATUS_RIGHT; |
| 1364 | } |
| 1365 | } |
| 1366 | # endif |
| 1367 | } |
| 1368 | else |
| 1369 | #endif |
| 1370 | { |
| 1371 | pc_col += curwin->w_wcol; |
| 1372 | #ifdef FEAT_MBYTE |
| 1373 | if (mb_lefthalve(pc_row, pc_col)) |
| 1374 | pc_status = PC_STATUS_LEFT; |
| 1375 | #endif |
| 1376 | } |
| 1377 | |
| 1378 | /* save the character to be able to put it back */ |
| 1379 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1380 | if (pc_status == PC_STATUS_UNSET) |
| 1381 | #endif |
| 1382 | { |
| 1383 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1384 | pc_status = PC_STATUS_SET; |
| 1385 | } |
| 1386 | screen_putchar(c, pc_row, pc_col, attr); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | /* |
| 1391 | * Undo the previous edit_putchar(). |
| 1392 | */ |
| 1393 | void |
| 1394 | edit_unputchar() |
| 1395 | { |
| 1396 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1397 | { |
| 1398 | #if defined(FEAT_MBYTE) |
| 1399 | if (pc_status == PC_STATUS_RIGHT) |
| 1400 | ++curwin->w_wcol; |
| 1401 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1402 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1403 | else |
| 1404 | #endif |
| 1405 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1411 | * Only works when cursor is in the line that changes. |
| 1412 | */ |
| 1413 | void |
| 1414 | display_dollar(col) |
| 1415 | colnr_T col; |
| 1416 | { |
| 1417 | colnr_T save_col; |
| 1418 | |
| 1419 | if (!redrawing()) |
| 1420 | return; |
| 1421 | |
| 1422 | cursor_off(); |
| 1423 | save_col = curwin->w_cursor.col; |
| 1424 | curwin->w_cursor.col = col; |
| 1425 | #ifdef FEAT_MBYTE |
| 1426 | if (has_mbyte) |
| 1427 | { |
| 1428 | char_u *p; |
| 1429 | |
| 1430 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1431 | p = ml_get_curline(); |
| 1432 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1433 | } |
| 1434 | #endif |
| 1435 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1436 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1437 | { |
| 1438 | edit_putchar('$', FALSE); |
| 1439 | dollar_vcol = curwin->w_virtcol; |
| 1440 | } |
| 1441 | curwin->w_cursor.col = save_col; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | * Call this function before moving the cursor from the normal insert position |
| 1446 | * in insert mode. |
| 1447 | */ |
| 1448 | static void |
| 1449 | undisplay_dollar() |
| 1450 | { |
| 1451 | if (dollar_vcol) |
| 1452 | { |
| 1453 | dollar_vcol = 0; |
| 1454 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1460 | * Keep the cursor on the same character. |
| 1461 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1462 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1463 | * type == INDENT_SET set indent to "amount" |
| 1464 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1465 | */ |
| 1466 | void |
| 1467 | change_indent(type, amount, round, replaced) |
| 1468 | int type; |
| 1469 | int amount; |
| 1470 | int round; |
| 1471 | int replaced; /* replaced character, put on replace stack */ |
| 1472 | { |
| 1473 | int vcol; |
| 1474 | int last_vcol; |
| 1475 | int insstart_less; /* reduction for Insstart.col */ |
| 1476 | int new_cursor_col; |
| 1477 | int i; |
| 1478 | char_u *ptr; |
| 1479 | int save_p_list; |
| 1480 | int start_col; |
| 1481 | colnr_T vc; |
| 1482 | #ifdef FEAT_VREPLACE |
| 1483 | colnr_T orig_col = 0; /* init for GCC */ |
| 1484 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1485 | |
| 1486 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1487 | if (State & VREPLACE_FLAG) |
| 1488 | { |
| 1489 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1490 | orig_col = curwin->w_cursor.col; |
| 1491 | } |
| 1492 | #endif |
| 1493 | |
| 1494 | /* for the following tricks we don't want list mode */ |
| 1495 | save_p_list = curwin->w_p_list; |
| 1496 | curwin->w_p_list = FALSE; |
| 1497 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1498 | vcol = vc; |
| 1499 | |
| 1500 | /* |
| 1501 | * For Replace mode we need to fix the replace stack later, which is only |
| 1502 | * possible when the cursor is in the indent. Remember the number of |
| 1503 | * characters before the cursor if it's possible. |
| 1504 | */ |
| 1505 | start_col = curwin->w_cursor.col; |
| 1506 | |
| 1507 | /* determine offset from first non-blank */ |
| 1508 | new_cursor_col = curwin->w_cursor.col; |
| 1509 | beginline(BL_WHITE); |
| 1510 | new_cursor_col -= curwin->w_cursor.col; |
| 1511 | |
| 1512 | insstart_less = curwin->w_cursor.col; |
| 1513 | |
| 1514 | /* |
| 1515 | * If the cursor is in the indent, compute how many screen columns the |
| 1516 | * cursor is to the left of the first non-blank. |
| 1517 | */ |
| 1518 | if (new_cursor_col < 0) |
| 1519 | vcol = get_indent() - vcol; |
| 1520 | |
| 1521 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1522 | start_col = -1; |
| 1523 | |
| 1524 | /* |
| 1525 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1526 | */ |
| 1527 | if (type == INDENT_SET) |
| 1528 | (void)set_indent(amount, SIN_CHANGED); |
| 1529 | else |
| 1530 | { |
| 1531 | #ifdef FEAT_VREPLACE |
| 1532 | int save_State = State; |
| 1533 | |
| 1534 | /* Avoid being called recursively. */ |
| 1535 | if (State & VREPLACE_FLAG) |
| 1536 | State = INSERT; |
| 1537 | #endif |
| 1538 | shift_line(type == INDENT_DEC, round, 1); |
| 1539 | #ifdef FEAT_VREPLACE |
| 1540 | State = save_State; |
| 1541 | #endif |
| 1542 | } |
| 1543 | insstart_less -= curwin->w_cursor.col; |
| 1544 | |
| 1545 | /* |
| 1546 | * Try to put cursor on same character. |
| 1547 | * If the cursor is at or after the first non-blank in the line, |
| 1548 | * compute the cursor column relative to the column of the first |
| 1549 | * non-blank character. |
| 1550 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1551 | * If the cursor is before the first non-blank, position it relative |
| 1552 | * to the first non-blank, counted in screen columns. |
| 1553 | */ |
| 1554 | if (new_cursor_col >= 0) |
| 1555 | { |
| 1556 | /* |
| 1557 | * When changing the indent while the cursor is touching it, reset |
| 1558 | * Insstart_col to 0. |
| 1559 | */ |
| 1560 | if (new_cursor_col == 0) |
| 1561 | insstart_less = MAXCOL; |
| 1562 | new_cursor_col += curwin->w_cursor.col; |
| 1563 | } |
| 1564 | else if (!(State & INSERT)) |
| 1565 | new_cursor_col = curwin->w_cursor.col; |
| 1566 | else |
| 1567 | { |
| 1568 | /* |
| 1569 | * Compute the screen column where the cursor should be. |
| 1570 | */ |
| 1571 | vcol = get_indent() - vcol; |
| 1572 | curwin->w_virtcol = (vcol < 0) ? 0 : vcol; |
| 1573 | |
| 1574 | /* |
| 1575 | * Advance the cursor until we reach the right screen column. |
| 1576 | */ |
| 1577 | vcol = last_vcol = 0; |
| 1578 | new_cursor_col = -1; |
| 1579 | ptr = ml_get_curline(); |
| 1580 | while (vcol <= (int)curwin->w_virtcol) |
| 1581 | { |
| 1582 | last_vcol = vcol; |
| 1583 | #ifdef FEAT_MBYTE |
| 1584 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1585 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1586 | else |
| 1587 | #endif |
| 1588 | ++new_cursor_col; |
| 1589 | vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol); |
| 1590 | } |
| 1591 | vcol = last_vcol; |
| 1592 | |
| 1593 | /* |
| 1594 | * May need to insert spaces to be able to position the cursor on |
| 1595 | * the right screen column. |
| 1596 | */ |
| 1597 | if (vcol != (int)curwin->w_virtcol) |
| 1598 | { |
| 1599 | curwin->w_cursor.col = new_cursor_col; |
| 1600 | i = (int)curwin->w_virtcol - vcol; |
| 1601 | ptr = alloc(i + 1); |
| 1602 | if (ptr != NULL) |
| 1603 | { |
| 1604 | new_cursor_col += i; |
| 1605 | ptr[i] = NUL; |
| 1606 | while (--i >= 0) |
| 1607 | ptr[i] = ' '; |
| 1608 | ins_str(ptr); |
| 1609 | vim_free(ptr); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * When changing the indent while the cursor is in it, reset |
| 1615 | * Insstart_col to 0. |
| 1616 | */ |
| 1617 | insstart_less = MAXCOL; |
| 1618 | } |
| 1619 | |
| 1620 | curwin->w_p_list = save_p_list; |
| 1621 | |
| 1622 | if (new_cursor_col <= 0) |
| 1623 | curwin->w_cursor.col = 0; |
| 1624 | else |
| 1625 | curwin->w_cursor.col = new_cursor_col; |
| 1626 | curwin->w_set_curswant = TRUE; |
| 1627 | changed_cline_bef_curs(); |
| 1628 | |
| 1629 | /* |
| 1630 | * May have to adjust the start of the insert. |
| 1631 | */ |
| 1632 | if (State & INSERT) |
| 1633 | { |
| 1634 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1635 | { |
| 1636 | if ((int)Insstart.col <= insstart_less) |
| 1637 | Insstart.col = 0; |
| 1638 | else |
| 1639 | Insstart.col -= insstart_less; |
| 1640 | } |
| 1641 | if ((int)ai_col <= insstart_less) |
| 1642 | ai_col = 0; |
| 1643 | else |
| 1644 | ai_col -= insstart_less; |
| 1645 | } |
| 1646 | |
| 1647 | /* |
| 1648 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 1649 | * If the number of characters before the cursor decreased, need to pop a |
| 1650 | * few characters from the replace stack. |
| 1651 | * If the number of characters before the cursor increased, need to push a |
| 1652 | * few NULs onto the replace stack. |
| 1653 | */ |
| 1654 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1655 | { |
| 1656 | while (start_col > (int)curwin->w_cursor.col) |
| 1657 | { |
| 1658 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1659 | --start_col; |
| 1660 | } |
| 1661 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1662 | { |
| 1663 | replace_push(NUL); |
| 1664 | if (replaced) |
| 1665 | { |
| 1666 | replace_push(replaced); |
| 1667 | replaced = NUL; |
| 1668 | } |
| 1669 | ++start_col; |
| 1670 | } |
| 1671 | } |
| 1672 | |
| 1673 | #ifdef FEAT_VREPLACE |
| 1674 | /* |
| 1675 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 1676 | * it is always possible because we backspace over the whole line and then |
| 1677 | * put it back again the way we wanted it. |
| 1678 | */ |
| 1679 | if (State & VREPLACE_FLAG) |
| 1680 | { |
| 1681 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 1682 | * even if you can't backspace. */ |
| 1683 | if (orig_line == NULL) |
| 1684 | return; |
| 1685 | |
| 1686 | /* Save new line */ |
| 1687 | new_line = vim_strsave(ml_get_curline()); |
| 1688 | if (new_line == NULL) |
| 1689 | return; |
| 1690 | |
| 1691 | /* We only put back the new line up to the cursor */ |
| 1692 | new_line[curwin->w_cursor.col] = NUL; |
| 1693 | |
| 1694 | /* Put back original line */ |
| 1695 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 1696 | curwin->w_cursor.col = orig_col; |
| 1697 | |
| 1698 | /* Backspace from cursor to start of line */ |
| 1699 | backspace_until_column(0); |
| 1700 | |
| 1701 | /* Insert new stuff into line again */ |
| 1702 | ins_bytes(new_line); |
| 1703 | |
| 1704 | vim_free(new_line); |
| 1705 | } |
| 1706 | #endif |
| 1707 | } |
| 1708 | |
| 1709 | /* |
| 1710 | * Truncate the space at the end of a line. This is to be used only in an |
| 1711 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 1712 | * modes. |
| 1713 | */ |
| 1714 | void |
| 1715 | truncate_spaces(line) |
| 1716 | char_u *line; |
| 1717 | { |
| 1718 | int i; |
| 1719 | |
| 1720 | /* find start of trailing white space */ |
| 1721 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 1722 | { |
| 1723 | if (State & REPLACE_FLAG) |
| 1724 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1725 | } |
| 1726 | line[i + 1] = NUL; |
| 1727 | } |
| 1728 | |
| 1729 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 1730 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 1731 | /* |
| 1732 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 1733 | * modes correctly. May also be used when not in insert mode at all. |
| 1734 | */ |
| 1735 | void |
| 1736 | backspace_until_column(col) |
| 1737 | int col; |
| 1738 | { |
| 1739 | while ((int)curwin->w_cursor.col > col) |
| 1740 | { |
| 1741 | curwin->w_cursor.col--; |
| 1742 | if (State & REPLACE_FLAG) |
| 1743 | replace_do_bs(); |
| 1744 | else |
| 1745 | (void)del_char(FALSE); |
| 1746 | } |
| 1747 | } |
| 1748 | #endif |
| 1749 | |
| 1750 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 1751 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1752 | * CTRL-X pressed in Insert mode. |
| 1753 | */ |
| 1754 | static void |
| 1755 | ins_ctrl_x() |
| 1756 | { |
| 1757 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 1758 | * CTRL-V works like CTRL-N */ |
| 1759 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 1760 | { |
| 1761 | /* if the next ^X<> won't ADD nothing, then reset |
| 1762 | * compl_cont_status */ |
| 1763 | if (compl_cont_status & CONT_N_ADDS) |
| 1764 | compl_cont_status = (compl_cont_status | CONT_INTRPT); |
| 1765 | else |
| 1766 | compl_cont_status = 0; |
| 1767 | /* We're not sure which CTRL-X mode it will be yet */ |
| 1768 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 1769 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 1770 | edit_submode_pre = NULL; |
| 1771 | showmode(); |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 1777 | */ |
| 1778 | static int |
| 1779 | has_compl_option(dict_opt) |
| 1780 | int dict_opt; |
| 1781 | { |
| 1782 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL) |
| 1783 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 1784 | { |
| 1785 | ctrl_x_mode = 0; |
| 1786 | edit_submode = NULL; |
| 1787 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 1788 | : (char_u *)_("'thesaurus' option is empty"), |
| 1789 | hl_attr(HLF_E)); |
| 1790 | if (emsg_silent == 0) |
| 1791 | { |
| 1792 | vim_beep(); |
| 1793 | setcursor(); |
| 1794 | out_flush(); |
| 1795 | ui_delay(2000L, FALSE); |
| 1796 | } |
| 1797 | return FALSE; |
| 1798 | } |
| 1799 | return TRUE; |
| 1800 | } |
| 1801 | |
| 1802 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1803 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 1804 | * This depends on the current mode. |
| 1805 | */ |
| 1806 | int |
| 1807 | vim_is_ctrl_x_key(c) |
| 1808 | int c; |
| 1809 | { |
| 1810 | /* Always allow ^R - let it's results then be checked */ |
| 1811 | if (c == Ctrl_R) |
| 1812 | return TRUE; |
| 1813 | |
| 1814 | switch (ctrl_x_mode) |
| 1815 | { |
| 1816 | case 0: /* Not in any CTRL-X mode */ |
| 1817 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 1818 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1819 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1820 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 1821 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 1822 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1823 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
| 1824 | || c == Ctrl_S || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1825 | case CTRL_X_SCROLL: |
| 1826 | return (c == Ctrl_Y || c == Ctrl_E); |
| 1827 | case CTRL_X_WHOLE_LINE: |
| 1828 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 1829 | case CTRL_X_FILES: |
| 1830 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 1831 | case CTRL_X_DICTIONARY: |
| 1832 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 1833 | case CTRL_X_THESAURUS: |
| 1834 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 1835 | case CTRL_X_TAGS: |
| 1836 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 1837 | #ifdef FEAT_FIND_ID |
| 1838 | case CTRL_X_PATH_PATTERNS: |
| 1839 | return (c == Ctrl_P || c == Ctrl_N); |
| 1840 | case CTRL_X_PATH_DEFINES: |
| 1841 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 1842 | #endif |
| 1843 | case CTRL_X_CMDLINE: |
| 1844 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 1845 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1846 | #ifdef FEAT_COMPL_FUNC |
| 1847 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1848 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 1849 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1850 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1851 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1852 | case CTRL_X_SPELL: |
| 1853 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1854 | } |
| 1855 | EMSG(_(e_internal)); |
| 1856 | return FALSE; |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * This is like ins_compl_add(), but if ic and inf are set, then the |
| 1861 | * case of the originally typed text is used, and the case of the completed |
| 1862 | * text is infered, ie this tries to work out what case you probably wanted |
| 1863 | * the rest of the word to be in -- webb |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1864 | * TODO: make this work for multi-byte characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1865 | */ |
| 1866 | int |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1867 | ins_compl_add_infercase(str, len, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | char_u *str; |
| 1869 | int len; |
| 1870 | char_u *fname; |
| 1871 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1872 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1873 | { |
| 1874 | int has_lower = FALSE; |
| 1875 | int was_letter = FALSE; |
| 1876 | int idx; |
| 1877 | |
| 1878 | if (p_ic && curbuf->b_p_inf && len < IOSIZE) |
| 1879 | { |
| 1880 | /* Infer case of completed part -- webb */ |
| 1881 | /* Use IObuff, str would change text in buffer! */ |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 1882 | vim_strncpy(IObuff, str, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1883 | |
| 1884 | /* Rule 1: Were any chars converted to lower? */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1885 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1886 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1887 | if (islower(compl_orig_text[idx])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1888 | { |
| 1889 | has_lower = TRUE; |
| 1890 | if (isupper(IObuff[idx])) |
| 1891 | { |
| 1892 | /* Rule 1 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1893 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1894 | IObuff[idx] = TOLOWER_LOC(IObuff[idx]); |
| 1895 | break; |
| 1896 | } |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /* |
| 1901 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 1902 | * upper case. |
| 1903 | */ |
| 1904 | if (!has_lower) |
| 1905 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1906 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1907 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1908 | if (was_letter && isupper(compl_orig_text[idx]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1909 | && islower(IObuff[idx])) |
| 1910 | { |
| 1911 | /* Rule 2 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1912 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1913 | IObuff[idx] = TOUPPER_LOC(IObuff[idx]); |
| 1914 | break; |
| 1915 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1916 | was_letter = isalpha(compl_orig_text[idx]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1917 | } |
| 1918 | } |
| 1919 | |
| 1920 | /* Copy the original case of the part we typed */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1921 | STRNCPY(IObuff, compl_orig_text, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1922 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1923 | return ins_compl_add(IObuff, len, fname, dir, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1924 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1925 | return ins_compl_add(str, len, fname, dir, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Add a match to the list of matches. |
| 1930 | * If the given string is already in the list of completions, then return |
| 1931 | * FAIL, otherwise add it to the list and return OK. If there is an error, |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1932 | * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb. |
| 1933 | * |
| 1934 | * New: |
| 1935 | * If the given string is already in the list of completions, then return |
| 1936 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 1937 | * maybe because alloc() returns NULL, then FAIL is returned -- webb. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1938 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1939 | int |
| 1940 | ins_compl_add(str, len, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1941 | char_u *str; |
| 1942 | int len; |
| 1943 | char_u *fname; |
| 1944 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1945 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1946 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1947 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | |
| 1949 | ui_breakcheck(); |
| 1950 | if (got_int) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1951 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1952 | if (len < 0) |
| 1953 | len = (int)STRLEN(str); |
| 1954 | |
| 1955 | /* |
| 1956 | * If the same match is already present, don't add it. |
| 1957 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1958 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1960 | match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | do |
| 1962 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1963 | if ( !(match->cp_flags & ORIGINAL_TEXT) |
| 1964 | && STRNCMP(match->cp_str, str, (size_t)len) == 0 |
| 1965 | && match->cp_str[len] == NUL) |
| 1966 | return NOTDONE; |
| 1967 | match = match->cp_next; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1968 | } while (match != NULL && match != compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /* |
| 1972 | * Allocate a new match structure. |
| 1973 | * Copy the values to the new match structure. |
| 1974 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1975 | match = (compl_T *)alloc((unsigned)sizeof(compl_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1976 | if (match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1977 | return FAIL; |
| 1978 | match->cp_number = -1; |
| 1979 | if (flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1980 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1981 | match->cp_number = 0; |
| 1982 | match->cp_str = compl_orig_text; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1983 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1984 | else if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1985 | { |
| 1986 | vim_free(match); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1987 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1988 | } |
| 1989 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1990 | * - compl_curr_match->cp_fname if it is a string equal to fname. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1991 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 1992 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1993 | if (fname && compl_curr_match && compl_curr_match->cp_fname |
| 1994 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
| 1995 | match->cp_fname = compl_curr_match->cp_fname; |
| 1996 | else if (fname && (match->cp_fname = vim_strsave(fname)) != NULL) |
| 1997 | flags |= FREE_FNAME; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1998 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1999 | match->cp_fname = NULL; |
| 2000 | match->cp_flags = flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | |
| 2002 | /* |
| 2003 | * Link the new match structure in the list of matches. |
| 2004 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2005 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2006 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2007 | else if (dir == FORWARD) |
| 2008 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2009 | match->cp_next = compl_curr_match->cp_next; |
| 2010 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2011 | } |
| 2012 | else /* BACKWARD */ |
| 2013 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2014 | match->cp_next = compl_curr_match; |
| 2015 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2016 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2017 | if (match->cp_next) |
| 2018 | match->cp_next->cp_prev = match; |
| 2019 | if (match->cp_prev) |
| 2020 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2021 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2022 | compl_first_match = match; |
| 2023 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2024 | |
| 2025 | return OK; |
| 2026 | } |
| 2027 | |
| 2028 | /* |
| 2029 | * Add an array of matches to the list of matches. |
| 2030 | * Frees matches[]. |
| 2031 | */ |
| 2032 | static void |
| 2033 | ins_compl_add_matches(num_matches, matches, dir) |
| 2034 | int num_matches; |
| 2035 | char_u **matches; |
| 2036 | int dir; |
| 2037 | { |
| 2038 | int i; |
| 2039 | int add_r = OK; |
| 2040 | int ldir = dir; |
| 2041 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2042 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2043 | if ((add_r = ins_compl_add(matches[i], -1, NULL, ldir, 0)) == OK) |
| 2044 | /* if dir was BACKWARD then honor it just once */ |
| 2045 | ldir = FORWARD; |
| 2046 | FreeWild(num_matches, matches); |
| 2047 | } |
| 2048 | |
| 2049 | /* Make the completion list cyclic. |
| 2050 | * Return the number of matches (excluding the original). |
| 2051 | */ |
| 2052 | static int |
| 2053 | ins_compl_make_cyclic() |
| 2054 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2055 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | int count = 0; |
| 2057 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2058 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2059 | { |
| 2060 | /* |
| 2061 | * Find the end of the list. |
| 2062 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2063 | match = compl_first_match; |
| 2064 | /* there's always an entry for the compl_orig_text, it doesn't count. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2065 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2066 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2067 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | ++count; |
| 2069 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2070 | match->cp_next = compl_first_match; |
| 2071 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2072 | } |
| 2073 | return count; |
| 2074 | } |
| 2075 | |
| 2076 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 2077 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
| 2078 | /* |
| 2079 | * Add any identifiers that match the given pattern to the list of |
| 2080 | * completions. |
| 2081 | */ |
| 2082 | static void |
| 2083 | ins_compl_dictionaries(dict, pat, dir, flags, thesaurus) |
| 2084 | char_u *dict; |
| 2085 | char_u *pat; |
| 2086 | int dir; |
| 2087 | int flags; |
| 2088 | int thesaurus; |
| 2089 | { |
| 2090 | char_u *ptr; |
| 2091 | char_u *buf; |
| 2092 | FILE *fp; |
| 2093 | regmatch_T regmatch; |
| 2094 | int add_r; |
| 2095 | char_u **files; |
| 2096 | int count; |
| 2097 | int i; |
| 2098 | int save_p_scs; |
| 2099 | |
| 2100 | buf = alloc(LSIZE); |
| 2101 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 2102 | save_p_scs = p_scs; |
| 2103 | if (curbuf->b_p_inf) |
| 2104 | p_scs = FALSE; |
| 2105 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 2106 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 2107 | regmatch.rm_ic = ignorecase(pat); |
| 2108 | while (buf != NULL && regmatch.regprog != NULL && *dict != NUL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2109 | && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2110 | { |
| 2111 | /* copy one dictionary file name into buf */ |
| 2112 | if (flags == DICT_EXACT) |
| 2113 | { |
| 2114 | count = 1; |
| 2115 | files = &dict; |
| 2116 | } |
| 2117 | else |
| 2118 | { |
| 2119 | /* Expand wildcards in the dictionary name, but do not allow |
| 2120 | * backticks (for security, the 'dict' option may have been set in |
| 2121 | * a modeline). */ |
| 2122 | copy_option_part(&dict, buf, LSIZE, ","); |
| 2123 | if (vim_strchr(buf, '`') != NULL |
| 2124 | || expand_wildcards(1, &buf, &count, &files, |
| 2125 | EW_FILE|EW_SILENT) != OK) |
| 2126 | count = 0; |
| 2127 | } |
| 2128 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2129 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2130 | { |
| 2131 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 2132 | if (flags != DICT_EXACT) |
| 2133 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 2134 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2135 | _("Scanning dictionary: %s"), (char *)files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 2137 | } |
| 2138 | |
| 2139 | if (fp != NULL) |
| 2140 | { |
| 2141 | /* |
| 2142 | * Read dictionary file line by line. |
| 2143 | * Check each line for a match. |
| 2144 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2145 | while (!got_int && !compl_interrupted |
| 2146 | && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2147 | { |
| 2148 | ptr = buf; |
| 2149 | while (vim_regexec(®match, buf, (colnr_T)(ptr - buf))) |
| 2150 | { |
| 2151 | ptr = regmatch.startp[0]; |
| 2152 | ptr = find_word_end(ptr); |
| 2153 | add_r = ins_compl_add_infercase(regmatch.startp[0], |
| 2154 | (int)(ptr - regmatch.startp[0]), |
| 2155 | files[i], dir, 0); |
| 2156 | if (thesaurus) |
| 2157 | { |
| 2158 | char_u *wstart; |
| 2159 | |
| 2160 | /* |
| 2161 | * Add the other matches on the line |
| 2162 | */ |
| 2163 | while (!got_int) |
| 2164 | { |
| 2165 | /* Find start of the next word. Skip white |
| 2166 | * space and punctuation. */ |
| 2167 | ptr = find_word_start(ptr); |
| 2168 | if (*ptr == NUL || *ptr == NL) |
| 2169 | break; |
| 2170 | wstart = ptr; |
| 2171 | |
| 2172 | /* Find end of the word and add it. */ |
| 2173 | #ifdef FEAT_MBYTE |
| 2174 | if (has_mbyte) |
| 2175 | /* Japanese words may have characters in |
| 2176 | * different classes, only separate words |
| 2177 | * with single-byte non-word characters. */ |
| 2178 | while (*ptr != NUL) |
| 2179 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2180 | int l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | |
| 2182 | if (l < 2 && !vim_iswordc(*ptr)) |
| 2183 | break; |
| 2184 | ptr += l; |
| 2185 | } |
| 2186 | else |
| 2187 | #endif |
| 2188 | ptr = find_word_end(ptr); |
| 2189 | add_r = ins_compl_add_infercase(wstart, |
| 2190 | (int)(ptr - wstart), files[i], dir, 0); |
| 2191 | } |
| 2192 | } |
| 2193 | if (add_r == OK) |
| 2194 | /* if dir was BACKWARD then honor it just once */ |
| 2195 | dir = FORWARD; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2196 | else if (add_r == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2197 | break; |
| 2198 | /* avoid expensive call to vim_regexec() when at end |
| 2199 | * of line */ |
| 2200 | if (*ptr == '\n' || got_int) |
| 2201 | break; |
| 2202 | } |
| 2203 | line_breakcheck(); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2204 | ins_compl_check_keys(50); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2205 | } |
| 2206 | fclose(fp); |
| 2207 | } |
| 2208 | } |
| 2209 | if (flags != DICT_EXACT) |
| 2210 | FreeWild(count, files); |
| 2211 | if (flags) |
| 2212 | break; |
| 2213 | } |
| 2214 | p_scs = save_p_scs; |
| 2215 | vim_free(regmatch.regprog); |
| 2216 | vim_free(buf); |
| 2217 | } |
| 2218 | |
| 2219 | /* |
| 2220 | * Find the start of the next word. |
| 2221 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 2222 | */ |
| 2223 | char_u * |
| 2224 | find_word_start(ptr) |
| 2225 | char_u *ptr; |
| 2226 | { |
| 2227 | #ifdef FEAT_MBYTE |
| 2228 | if (has_mbyte) |
| 2229 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2230 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | else |
| 2232 | #endif |
| 2233 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 2234 | ++ptr; |
| 2235 | return ptr; |
| 2236 | } |
| 2237 | |
| 2238 | /* |
| 2239 | * Find the end of the word. Assumes it starts inside a word. |
| 2240 | * Returns a pointer to just after the word. |
| 2241 | */ |
| 2242 | char_u * |
| 2243 | find_word_end(ptr) |
| 2244 | char_u *ptr; |
| 2245 | { |
| 2246 | #ifdef FEAT_MBYTE |
| 2247 | int start_class; |
| 2248 | |
| 2249 | if (has_mbyte) |
| 2250 | { |
| 2251 | start_class = mb_get_class(ptr); |
| 2252 | if (start_class > 1) |
| 2253 | while (*ptr != NUL) |
| 2254 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2255 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | if (mb_get_class(ptr) != start_class) |
| 2257 | break; |
| 2258 | } |
| 2259 | } |
| 2260 | else |
| 2261 | #endif |
| 2262 | while (vim_iswordc(*ptr)) |
| 2263 | ++ptr; |
| 2264 | return ptr; |
| 2265 | } |
| 2266 | |
| 2267 | /* |
| 2268 | * Free the list of completions |
| 2269 | */ |
| 2270 | static void |
| 2271 | ins_compl_free() |
| 2272 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2273 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2274 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2275 | vim_free(compl_pattern); |
| 2276 | compl_pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2278 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | return; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2280 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | do |
| 2282 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2283 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2284 | compl_curr_match = compl_curr_match->cp_next; |
| 2285 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2286 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2287 | if (match->cp_flags & FREE_FNAME) |
| 2288 | vim_free(match->cp_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2290 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 2291 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | static void |
| 2295 | ins_compl_clear() |
| 2296 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2297 | compl_cont_status = 0; |
| 2298 | compl_started = FALSE; |
| 2299 | compl_matches = 0; |
| 2300 | vim_free(compl_pattern); |
| 2301 | compl_pattern = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | save_sm = -1; |
| 2303 | edit_submode_extra = NULL; |
| 2304 | } |
| 2305 | |
| 2306 | /* |
| 2307 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2308 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2309 | */ |
| 2310 | static void |
| 2311 | ins_compl_prep(c) |
| 2312 | int c; |
| 2313 | { |
| 2314 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | int temp; |
| 2316 | int want_cindent; |
| 2317 | |
| 2318 | /* Forget any previous 'special' messages if this is actually |
| 2319 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2320 | */ |
| 2321 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2322 | edit_submode_extra = NULL; |
| 2323 | |
| 2324 | /* Ignore end of Select mode mapping */ |
| 2325 | if (c == K_SELECT) |
| 2326 | return; |
| 2327 | |
| 2328 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 2329 | { |
| 2330 | /* |
| 2331 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2332 | * it will be yet. Now we decide. |
| 2333 | */ |
| 2334 | switch (c) |
| 2335 | { |
| 2336 | case Ctrl_E: |
| 2337 | case Ctrl_Y: |
| 2338 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2339 | if (!(State & REPLACE_FLAG)) |
| 2340 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2341 | else |
| 2342 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2343 | edit_submode_pre = NULL; |
| 2344 | showmode(); |
| 2345 | break; |
| 2346 | case Ctrl_L: |
| 2347 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2348 | break; |
| 2349 | case Ctrl_F: |
| 2350 | ctrl_x_mode = CTRL_X_FILES; |
| 2351 | break; |
| 2352 | case Ctrl_K: |
| 2353 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2354 | break; |
| 2355 | case Ctrl_R: |
| 2356 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 2357 | break; |
| 2358 | case Ctrl_T: |
| 2359 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2360 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2361 | #ifdef FEAT_COMPL_FUNC |
| 2362 | case Ctrl_U: |
| 2363 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2364 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2365 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2366 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2367 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2368 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2369 | case 's': |
| 2370 | case Ctrl_S: |
| 2371 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 2372 | #ifdef FEAT_SYN_HL |
| 2373 | spell_back_to_badword(); |
| 2374 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2375 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2376 | case Ctrl_RSB: |
| 2377 | ctrl_x_mode = CTRL_X_TAGS; |
| 2378 | break; |
| 2379 | #ifdef FEAT_FIND_ID |
| 2380 | case Ctrl_I: |
| 2381 | case K_S_TAB: |
| 2382 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2383 | break; |
| 2384 | case Ctrl_D: |
| 2385 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2386 | break; |
| 2387 | #endif |
| 2388 | case Ctrl_V: |
| 2389 | case Ctrl_Q: |
| 2390 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2391 | break; |
| 2392 | case Ctrl_P: |
| 2393 | case Ctrl_N: |
| 2394 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2395 | * just started ^X mode, or there were enough ^X's to cancel |
| 2396 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2397 | * do normal expansion when interrupting a different mode (say |
| 2398 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 2399 | * nothing changes if interrupting mode 0, (eg, the flag |
| 2400 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2401 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2402 | compl_cont_status |= CONT_LOCAL; |
| 2403 | else if (compl_cont_mode != 0) |
| 2404 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2405 | /* FALLTHROUGH */ |
| 2406 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2407 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 2408 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 2409 | * mode). |
| 2410 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 2411 | * value, in both cases ^X^X can be used to restart the same |
| 2412 | * mode (avoiding ADDING mode). |
| 2413 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 2414 | * 'complete' and local ^P expansions respectively. |
| 2415 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 2416 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2417 | if (c == Ctrl_X) |
| 2418 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2419 | if (compl_cont_mode != 0) |
| 2420 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2421 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2422 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | } |
| 2424 | ctrl_x_mode = 0; |
| 2425 | edit_submode = NULL; |
| 2426 | showmode(); |
| 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | else if (ctrl_x_mode != 0) |
| 2431 | { |
| 2432 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 2433 | if (!vim_is_ctrl_x_key(c)) |
| 2434 | { |
| 2435 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 2436 | ctrl_x_mode = 0; |
| 2437 | else |
| 2438 | ctrl_x_mode = CTRL_X_FINISHED; |
| 2439 | edit_submode = NULL; |
| 2440 | } |
| 2441 | showmode(); |
| 2442 | } |
| 2443 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2444 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | { |
| 2446 | /* Show error message from attempted keyword completion (probably |
| 2447 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2448 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2449 | showmode(); |
| 2450 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R) |
| 2451 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 2452 | { |
| 2453 | /* Get here when we have finished typing a sequence of ^N and |
| 2454 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2455 | * memory that was used, and make sure we can redo the insert. */ |
| 2456 | if (compl_curr_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2457 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2458 | char_u *p; |
| 2459 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2460 | /* |
| 2461 | * If any of the original typed text has been changed, |
| 2462 | * eg when ignorecase is set, we must add back-spaces to |
| 2463 | * the redo buffer. We add as few as necessary to delete |
| 2464 | * just the part of the original text that has changed. |
| 2465 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2466 | ptr = compl_curr_match->cp_str; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2467 | p = compl_orig_text; |
| 2468 | while (*p && *p == *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2469 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2470 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | ++ptr; |
| 2472 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2473 | for (temp = 0; p[temp]; ++temp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2474 | AppendCharToRedobuff(K_BS); |
| 2475 | AppendToRedobuffLit(ptr); |
| 2476 | } |
| 2477 | |
| 2478 | #ifdef FEAT_CINDENT |
| 2479 | want_cindent = (can_cindent && cindent_on()); |
| 2480 | #endif |
| 2481 | /* |
| 2482 | * When completing whole lines: fix indent for 'cindent'. |
| 2483 | * Otherwise, break line if it's too long. |
| 2484 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2485 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | { |
| 2487 | #ifdef FEAT_CINDENT |
| 2488 | /* re-indent the current line */ |
| 2489 | if (want_cindent) |
| 2490 | { |
| 2491 | do_c_expr_indent(); |
| 2492 | want_cindent = FALSE; /* don't do it again */ |
| 2493 | } |
| 2494 | #endif |
| 2495 | } |
| 2496 | else |
| 2497 | { |
| 2498 | /* put the cursor on the last char, for 'tw' formatting */ |
| 2499 | curwin->w_cursor.col--; |
| 2500 | if (stop_arrow() == OK) |
| 2501 | insertchar(NUL, 0, -1); |
| 2502 | curwin->w_cursor.col++; |
| 2503 | } |
| 2504 | |
| 2505 | auto_format(FALSE, TRUE); |
| 2506 | |
| 2507 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2508 | compl_started = FALSE; |
| 2509 | compl_matches = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 2511 | ctrl_x_mode = 0; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2512 | if (save_sm >= 0) |
| 2513 | p_sm = save_sm; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2514 | if (edit_submode != NULL) |
| 2515 | { |
| 2516 | edit_submode = NULL; |
| 2517 | showmode(); |
| 2518 | } |
| 2519 | |
| 2520 | #ifdef FEAT_CINDENT |
| 2521 | /* |
| 2522 | * Indent now if a key was typed that is in 'cinkeys'. |
| 2523 | */ |
| 2524 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 2525 | do_c_expr_indent(); |
| 2526 | #endif |
| 2527 | } |
| 2528 | } |
| 2529 | |
| 2530 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 2531 | * (re)set properly in ins_complete() */ |
| 2532 | if (!vim_is_ctrl_x_key(c)) |
| 2533 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2534 | compl_cont_status = 0; |
| 2535 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2536 | } |
| 2537 | } |
| 2538 | |
| 2539 | /* |
| 2540 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 2541 | * (depending on flag) starting from buf and looking for a non-scanned |
| 2542 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 2543 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 2544 | * |
| 2545 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 2546 | */ |
| 2547 | static buf_T * |
| 2548 | ins_compl_next_buf(buf, flag) |
| 2549 | buf_T *buf; |
| 2550 | int flag; |
| 2551 | { |
| 2552 | #ifdef FEAT_WINDOWS |
| 2553 | static win_T *wp; |
| 2554 | #endif |
| 2555 | |
| 2556 | if (flag == 'w') /* just windows */ |
| 2557 | { |
| 2558 | #ifdef FEAT_WINDOWS |
| 2559 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 2560 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 2561 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2562 | && wp->w_buffer->b_scanned) |
| 2563 | ; |
| 2564 | buf = wp->w_buffer; |
| 2565 | #else |
| 2566 | buf = curbuf; |
| 2567 | #endif |
| 2568 | } |
| 2569 | else |
| 2570 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 2571 | * (unlisted buffers) |
| 2572 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 2573 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2574 | && ((flag == 'U' |
| 2575 | ? buf->b_p_bl |
| 2576 | : (!buf->b_p_bl |
| 2577 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
| 2578 | || buf->b_scanned |
| 2579 | || (buf->b_ml.ml_mfp == NULL |
| 2580 | && ctrl_x_mode == CTRL_X_WHOLE_LINE))) |
| 2581 | ; |
| 2582 | return buf; |
| 2583 | } |
| 2584 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2585 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2586 | static int expand_by_function __ARGS((int type, char_u *base, char_u ***matches)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2587 | |
| 2588 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2589 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2590 | * get matches in "matches". |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2591 | * Return value is number of matches. |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2592 | */ |
| 2593 | static int |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2594 | expand_by_function(type, base, matches) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2595 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2596 | char_u *base; |
| 2597 | char_u ***matches; |
| 2598 | { |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2599 | list_T *matchlist; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2600 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2601 | listitem_T *li; |
| 2602 | garray_T ga; |
| 2603 | char_u *p; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2604 | char_u *funcname; |
| 2605 | pos_T pos; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2606 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2607 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 2608 | if (*funcname == NUL) |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2609 | return 0; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2610 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2611 | /* Call 'completefunc' to obtain the list of matches. */ |
| 2612 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2613 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2614 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2615 | pos = curwin->w_cursor; |
| 2616 | matchlist = call_func_retlist(funcname, 2, args, FALSE); |
| 2617 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2618 | if (matchlist == NULL) |
| 2619 | return 0; |
| 2620 | |
| 2621 | /* Go through the List with matches and put them in an array. */ |
| 2622 | ga_init2(&ga, (int)sizeof(char_u *), 8); |
| 2623 | for (li = matchlist->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2624 | { |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2625 | p = get_tv_string_chk(&li->li_tv); |
| 2626 | if (p != NULL && *p != NUL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2627 | { |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2628 | if (ga_grow(&ga, 1) == FAIL) |
| 2629 | break; |
| 2630 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strsave(p); |
| 2631 | ++ga.ga_len; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2632 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2633 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 2634 | |
| 2635 | list_unref(matchlist); |
| 2636 | *matches = (char_u **)ga.ga_data; |
| 2637 | return ga.ga_len; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2638 | } |
| 2639 | #endif /* FEAT_COMPL_FUNC */ |
| 2640 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2641 | /* |
| 2642 | * Get the next expansion(s), using "compl_pattern". |
| 2643 | * The search starts at position "ini" in curbuf and in the direction dir. |
| 2644 | * When "compl_started" is FALSE start at that position, otherwise |
| 2645 | * continue where we stopped searching before. |
| 2646 | * This may return before finding all the matches. |
| 2647 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2648 | */ |
| 2649 | static int |
| 2650 | ins_compl_get_exp(ini, dir) |
| 2651 | pos_T *ini; |
| 2652 | int dir; |
| 2653 | { |
| 2654 | static pos_T first_match_pos; |
| 2655 | static pos_T last_match_pos; |
| 2656 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2657 | static int found_all = FALSE; /* Found all matches of a |
| 2658 | certain type. */ |
| 2659 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2661 | pos_T *pos; |
| 2662 | char_u **matches; |
| 2663 | int save_p_scs; |
| 2664 | int save_p_ws; |
| 2665 | int save_p_ic; |
| 2666 | int i; |
| 2667 | int num_matches; |
| 2668 | int len; |
| 2669 | int found_new_match; |
| 2670 | int type = ctrl_x_mode; |
| 2671 | char_u *ptr; |
| 2672 | char_u *dict = NULL; |
| 2673 | int dict_f = 0; |
| 2674 | compl_T *old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2675 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2676 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | { |
| 2678 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 2679 | ins_buf->b_scanned = 0; |
| 2680 | found_all = FALSE; |
| 2681 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2682 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2683 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2684 | last_match_pos = first_match_pos = *ini; |
| 2685 | } |
| 2686 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2687 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2688 | pos = (dir == FORWARD) ? &last_match_pos : &first_match_pos; |
| 2689 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 2690 | for (;;) |
| 2691 | { |
| 2692 | found_new_match = FAIL; |
| 2693 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2694 | /* For ^N/^P pick a new entry from e_cpt if compl_started is off, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2695 | * or if found_all says this entry is done. For ^X^L only use the |
| 2696 | * entries from 'complete' that look in loaded buffers. */ |
| 2697 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2698 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2699 | { |
| 2700 | found_all = FALSE; |
| 2701 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 2702 | e_cpt++; |
| 2703 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 2704 | { |
| 2705 | ins_buf = curbuf; |
| 2706 | first_match_pos = *ini; |
| 2707 | /* So that ^N can match word immediately after cursor */ |
| 2708 | if (ctrl_x_mode == 0) |
| 2709 | dec(&first_match_pos); |
| 2710 | last_match_pos = first_match_pos; |
| 2711 | type = 0; |
| 2712 | } |
| 2713 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 2714 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 2715 | { |
| 2716 | /* Scan a buffer, but not the current one. */ |
| 2717 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 2718 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2719 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | first_match_pos.col = last_match_pos.col = 0; |
| 2721 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 2722 | last_match_pos.lnum = 0; |
| 2723 | type = 0; |
| 2724 | } |
| 2725 | else /* unloaded buffer, scan like dictionary */ |
| 2726 | { |
| 2727 | found_all = TRUE; |
| 2728 | if (ins_buf->b_fname == NULL) |
| 2729 | continue; |
| 2730 | type = CTRL_X_DICTIONARY; |
| 2731 | dict = ins_buf->b_fname; |
| 2732 | dict_f = DICT_EXACT; |
| 2733 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 2734 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | ins_buf->b_fname == NULL |
| 2736 | ? buf_spname(ins_buf) |
| 2737 | : ins_buf->b_sfname == NULL |
| 2738 | ? (char *)ins_buf->b_fname |
| 2739 | : (char *)ins_buf->b_sfname); |
| 2740 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 2741 | } |
| 2742 | else if (*e_cpt == NUL) |
| 2743 | break; |
| 2744 | else |
| 2745 | { |
| 2746 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2747 | type = -1; |
| 2748 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 2749 | { |
| 2750 | if (*e_cpt == 'k') |
| 2751 | type = CTRL_X_DICTIONARY; |
| 2752 | else |
| 2753 | type = CTRL_X_THESAURUS; |
| 2754 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 2755 | { |
| 2756 | dict = e_cpt; |
| 2757 | dict_f = DICT_FIRST; |
| 2758 | } |
| 2759 | } |
| 2760 | #ifdef FEAT_FIND_ID |
| 2761 | else if (*e_cpt == 'i') |
| 2762 | type = CTRL_X_PATH_PATTERNS; |
| 2763 | else if (*e_cpt == 'd') |
| 2764 | type = CTRL_X_PATH_DEFINES; |
| 2765 | #endif |
| 2766 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 2767 | { |
| 2768 | type = CTRL_X_TAGS; |
| 2769 | sprintf((char*)IObuff, _("Scanning tags.")); |
| 2770 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 2771 | } |
| 2772 | else |
| 2773 | type = -1; |
| 2774 | |
| 2775 | /* in any case e_cpt is advanced to the next entry */ |
| 2776 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 2777 | |
| 2778 | found_all = TRUE; |
| 2779 | if (type == -1) |
| 2780 | continue; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | switch (type) |
| 2785 | { |
| 2786 | case -1: |
| 2787 | break; |
| 2788 | #ifdef FEAT_FIND_ID |
| 2789 | case CTRL_X_PATH_PATTERNS: |
| 2790 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2791 | find_pattern_in_path(compl_pattern, dir, |
| 2792 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2793 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2794 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2795 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 2796 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 2797 | break; |
| 2798 | #endif |
| 2799 | |
| 2800 | case CTRL_X_DICTIONARY: |
| 2801 | case CTRL_X_THESAURUS: |
| 2802 | ins_compl_dictionaries( |
| 2803 | dict ? dict |
| 2804 | : (type == CTRL_X_THESAURUS |
| 2805 | ? (*curbuf->b_p_tsr == NUL |
| 2806 | ? p_tsr |
| 2807 | : curbuf->b_p_tsr) |
| 2808 | : (*curbuf->b_p_dict == NUL |
| 2809 | ? p_dict |
| 2810 | : curbuf->b_p_dict)), |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2811 | compl_pattern, dir, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2812 | dict ? dict_f : 0, type == CTRL_X_THESAURUS); |
| 2813 | dict = NULL; |
| 2814 | break; |
| 2815 | |
| 2816 | case CTRL_X_TAGS: |
| 2817 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 2818 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2819 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2820 | |
| 2821 | /* Find up to TAG_MANY matches. Avoids that an enourmous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2822 | * of matches is found when compl_pattern is empty */ |
| 2823 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2824 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 2825 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 2826 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 2827 | { |
| 2828 | ins_compl_add_matches(num_matches, matches, dir); |
| 2829 | } |
| 2830 | p_ic = save_p_ic; |
| 2831 | break; |
| 2832 | |
| 2833 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2834 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 2836 | { |
| 2837 | |
| 2838 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2839 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2840 | ins_compl_add_matches(num_matches, matches, dir); |
| 2841 | } |
| 2842 | break; |
| 2843 | |
| 2844 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2845 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 2846 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2847 | &num_matches, &matches) == EXPAND_OK) |
| 2848 | ins_compl_add_matches(num_matches, matches, dir); |
| 2849 | break; |
| 2850 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2851 | #ifdef FEAT_COMPL_FUNC |
| 2852 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2853 | case CTRL_X_OMNI: |
| 2854 | if (*compl_pattern == NUL) |
| 2855 | num_matches = 0; |
| 2856 | else |
| 2857 | num_matches = expand_by_function(type, compl_pattern, &matches); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2858 | if (num_matches > 0) |
| 2859 | ins_compl_add_matches(num_matches, matches, dir); |
| 2860 | break; |
| 2861 | #endif |
| 2862 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2863 | case CTRL_X_SPELL: |
| 2864 | #ifdef FEAT_SYN_HL |
| 2865 | num_matches = expand_spelling(first_match_pos.lnum, |
| 2866 | first_match_pos.col, compl_pattern, &matches); |
| 2867 | if (num_matches > 0) |
| 2868 | ins_compl_add_matches(num_matches, matches, dir); |
| 2869 | #endif |
| 2870 | break; |
| 2871 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2872 | default: /* normal ^P/^N and ^X^L */ |
| 2873 | /* |
| 2874 | * If 'infercase' is set, don't use 'smartcase' here |
| 2875 | */ |
| 2876 | save_p_scs = p_scs; |
| 2877 | if (ins_buf->b_p_inf) |
| 2878 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2879 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2880 | /* buffers other than curbuf are scanned from the beginning or the |
| 2881 | * end but never from the middle, thus setting nowrapscan in this |
| 2882 | * buffers is a good idea, on the other hand, we always set |
| 2883 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 2884 | save_p_ws = p_ws; |
| 2885 | if (ins_buf != curbuf) |
| 2886 | p_ws = FALSE; |
| 2887 | else if (*e_cpt == '.') |
| 2888 | p_ws = TRUE; |
| 2889 | for (;;) |
| 2890 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2891 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2892 | |
| 2893 | /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that has |
| 2894 | * added a word that was at the beginning of the line */ |
| 2895 | if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2896 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2897 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2898 | dir, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2899 | else |
| 2900 | found_new_match = searchit(NULL, ins_buf, pos, dir, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2901 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | RE_LAST); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2903 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2904 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2905 | /* set compl_started even on fail */ |
| 2906 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2907 | first_match_pos = *pos; |
| 2908 | last_match_pos = *pos; |
| 2909 | } |
| 2910 | else if (first_match_pos.lnum == last_match_pos.lnum |
| 2911 | && first_match_pos.col == last_match_pos.col) |
| 2912 | found_new_match = FAIL; |
| 2913 | if (found_new_match == FAIL) |
| 2914 | { |
| 2915 | if (ins_buf == curbuf) |
| 2916 | found_all = TRUE; |
| 2917 | break; |
| 2918 | } |
| 2919 | |
| 2920 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2921 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2922 | && ini->lnum == pos->lnum |
| 2923 | && ini->col == pos->col) |
| 2924 | continue; |
| 2925 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 2926 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2927 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2928 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2929 | { |
| 2930 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 2931 | continue; |
| 2932 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 2933 | if (!p_paste) |
| 2934 | ptr = skipwhite(ptr); |
| 2935 | } |
| 2936 | len = (int)STRLEN(ptr); |
| 2937 | } |
| 2938 | else |
| 2939 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2940 | char_u *tmp_ptr = ptr; |
| 2941 | |
| 2942 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2943 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2944 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2945 | /* Skip if already inside a word. */ |
| 2946 | if (vim_iswordp(tmp_ptr)) |
| 2947 | continue; |
| 2948 | /* Find start of next word. */ |
| 2949 | tmp_ptr = find_word_start(tmp_ptr); |
| 2950 | } |
| 2951 | /* Find end of this word. */ |
| 2952 | tmp_ptr = find_word_end(tmp_ptr); |
| 2953 | len = (int)(tmp_ptr - ptr); |
| 2954 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2955 | if ((compl_cont_status & CONT_ADDING) |
| 2956 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2957 | { |
| 2958 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 2959 | { |
| 2960 | /* Try next line, if any. the new word will be |
| 2961 | * "join" as if the normal command "J" was used. |
| 2962 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2963 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2964 | * works -- Acevedo */ |
| 2965 | STRNCPY(IObuff, ptr, len); |
| 2966 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 2967 | tmp_ptr = ptr = skipwhite(ptr); |
| 2968 | /* Find start of next word. */ |
| 2969 | tmp_ptr = find_word_start(tmp_ptr); |
| 2970 | /* Find end of next word. */ |
| 2971 | tmp_ptr = find_word_end(tmp_ptr); |
| 2972 | if (tmp_ptr > ptr) |
| 2973 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2974 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2975 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2976 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2977 | IObuff[len++] = ' '; |
| 2978 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 2979 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2980 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 2982 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2983 | && (IObuff[len - 2] == '?' |
| 2984 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2985 | IObuff[len++] = ' '; |
| 2986 | } |
| 2987 | /* copy as much as posible of the new word */ |
| 2988 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 2989 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 2990 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 2991 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2992 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2993 | } |
| 2994 | IObuff[len] = NUL; |
| 2995 | ptr = IObuff; |
| 2996 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2997 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2998 | continue; |
| 2999 | } |
| 3000 | } |
| 3001 | if (ins_compl_add_infercase(ptr, len, |
| 3002 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3003 | dir, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3004 | { |
| 3005 | found_new_match = OK; |
| 3006 | break; |
| 3007 | } |
| 3008 | } |
| 3009 | p_scs = save_p_scs; |
| 3010 | p_ws = save_p_ws; |
| 3011 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3012 | /* check if compl_curr_match has changed, (e.g. other type of |
| 3013 | * expansion added somenthing) */ |
| 3014 | if (compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3015 | found_new_match = OK; |
| 3016 | |
| 3017 | /* break the loop for specialized modes (use 'complete' just for the |
| 3018 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
| 3019 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3020 | || found_new_match != FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | break; |
| 3022 | |
| 3023 | /* Mark a buffer scanned when it has been scanned completely */ |
| 3024 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 3025 | ins_buf->b_scanned = TRUE; |
| 3026 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3027 | compl_started = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3028 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3029 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3030 | |
| 3031 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3032 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 3033 | found_new_match = FAIL; |
| 3034 | |
| 3035 | i = -1; /* total of matches, unknown */ |
| 3036 | if (found_new_match == FAIL |
| 3037 | || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) |
| 3038 | i = ins_compl_make_cyclic(); |
| 3039 | |
| 3040 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3041 | * just been made cyclic then we have to move compl_curr_match to the next |
| 3042 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3043 | compl_curr_match = dir == FORWARD ? old_match->cp_next : old_match->cp_prev; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3044 | if (compl_curr_match == NULL) |
| 3045 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3046 | return i; |
| 3047 | } |
| 3048 | |
| 3049 | /* Delete the old text being completed. */ |
| 3050 | static void |
| 3051 | ins_compl_delete() |
| 3052 | { |
| 3053 | int i; |
| 3054 | |
| 3055 | /* |
| 3056 | * In insert mode: Delete the typed part. |
| 3057 | * In replace mode: Put the old characters back, if any. |
| 3058 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3059 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3060 | backspace_until_column(i); |
| 3061 | changed_cline_bef_curs(); |
| 3062 | } |
| 3063 | |
| 3064 | /* Insert the new text being completed. */ |
| 3065 | static void |
| 3066 | ins_compl_insert() |
| 3067 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3068 | ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | /* |
| 3072 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3073 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 3074 | * get more completions. If it is FALSE, then we just do nothing when there |
| 3075 | * are no more completions in a given direction. The latter case is used when |
| 3076 | * we are still in the middle of finding completions, to allow browsing |
| 3077 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3078 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 3079 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3080 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 3081 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3082 | * |
| 3083 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3084 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 3085 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3086 | */ |
| 3087 | static int |
| 3088 | ins_compl_next(allow_get_expansion) |
| 3089 | int allow_get_expansion; |
| 3090 | { |
| 3091 | int num_matches = -1; |
| 3092 | int i; |
| 3093 | |
| 3094 | if (allow_get_expansion) |
| 3095 | { |
| 3096 | /* Delete old text to be replaced */ |
| 3097 | ins_compl_delete(); |
| 3098 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3099 | compl_pending = FALSE; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3100 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
| 3101 | compl_shown_match = compl_shown_match->cp_next; |
| 3102 | else if (compl_shows_dir == BACKWARD && compl_shown_match->cp_prev != NULL) |
| 3103 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3104 | else |
| 3105 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3106 | compl_pending = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3107 | if (allow_get_expansion) |
| 3108 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3109 | num_matches = ins_compl_get_exp(&compl_startpos, |
| 3110 | compl_direction); |
| 3111 | if (compl_pending) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3112 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3113 | if (compl_direction == compl_shows_dir) |
| 3114 | compl_shown_match = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3115 | } |
| 3116 | } |
| 3117 | else |
| 3118 | return -1; |
| 3119 | } |
| 3120 | |
| 3121 | /* Insert the text of the new completion */ |
| 3122 | ins_compl_insert(); |
| 3123 | |
| 3124 | if (!allow_get_expansion) |
| 3125 | { |
| 3126 | /* Display the current match. */ |
| 3127 | update_screen(0); |
| 3128 | |
| 3129 | /* Delete old text to be replaced, since we're still searching and |
| 3130 | * don't want to match ourselves! */ |
| 3131 | ins_compl_delete(); |
| 3132 | } |
| 3133 | |
| 3134 | /* |
| 3135 | * Show the file name for the match (if any) |
| 3136 | * Truncate the file name to avoid a wait for return. |
| 3137 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3138 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3139 | { |
| 3140 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3141 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3142 | if (i <= 0) |
| 3143 | i = 0; |
| 3144 | else |
| 3145 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3146 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3147 | msg(IObuff); |
| 3148 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 3149 | } |
| 3150 | |
| 3151 | return num_matches; |
| 3152 | } |
| 3153 | |
| 3154 | /* |
| 3155 | * Call this while finding completions, to check whether the user has hit a key |
| 3156 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3157 | * mode. Also, when compl_pending is TRUE, show a completion as soon as |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3158 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3159 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3160 | */ |
| 3161 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3162 | ins_compl_check_keys(frequency) |
| 3163 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3164 | { |
| 3165 | static int count = 0; |
| 3166 | |
| 3167 | int c; |
| 3168 | |
| 3169 | /* Don't check when reading keys from a script. That would break the test |
| 3170 | * scripts */ |
| 3171 | if (using_script()) |
| 3172 | return; |
| 3173 | |
| 3174 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3175 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3176 | return; |
| 3177 | count = 0; |
| 3178 | |
| 3179 | ++no_mapping; |
| 3180 | c = vpeekc_any(); |
| 3181 | --no_mapping; |
| 3182 | if (c != NUL) |
| 3183 | { |
| 3184 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 3185 | { |
| 3186 | c = safe_vgetc(); /* Eat the character */ |
| 3187 | if (c == Ctrl_P || c == Ctrl_L) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3188 | compl_shows_dir = BACKWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3189 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3190 | compl_shows_dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3191 | (void)ins_compl_next(FALSE); |
| 3192 | } |
| 3193 | else if (c != Ctrl_R) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3194 | compl_interrupted = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3195 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3196 | if (compl_pending && !got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3197 | (void)ins_compl_next(FALSE); |
| 3198 | } |
| 3199 | |
| 3200 | /* |
| 3201 | * Do Insert mode completion. |
| 3202 | * Called when character "c" was typed, which has a meaning for completion. |
| 3203 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 3204 | */ |
| 3205 | static int |
| 3206 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3207 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3208 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3209 | char_u *line; |
| 3210 | int startcol = 0; /* column where searched text starts */ |
| 3211 | colnr_T curs_col; /* cursor column */ |
| 3212 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3213 | |
| 3214 | if (c == Ctrl_P || c == Ctrl_L) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3215 | compl_direction = BACKWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3216 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3217 | compl_direction = FORWARD; |
| 3218 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3219 | { |
| 3220 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 3221 | |
| 3222 | /* Turn off 'sm' so we don't show matches with ^X^L */ |
| 3223 | save_sm = p_sm; |
| 3224 | p_sm = FALSE; |
| 3225 | |
| 3226 | did_ai = FALSE; |
| 3227 | #ifdef FEAT_SMARTINDENT |
| 3228 | did_si = FALSE; |
| 3229 | can_si = FALSE; |
| 3230 | can_si_back = FALSE; |
| 3231 | #endif |
| 3232 | if (stop_arrow() == FAIL) |
| 3233 | return FAIL; |
| 3234 | |
| 3235 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3236 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* if this same ctrl_x_mode has been interrupted use the text from |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3239 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 3240 | * instead of expand the one before the cursor, in word-wise if |
| 3241 | * "compl_startpos" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3242 | * is not in the same line as the cursor then fix it (the line has |
| 3243 | * been split because it was longer than 'tw'). if SOL is set then |
| 3244 | * skip the previous pattern, a word at the beginning of the line has |
| 3245 | * been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3246 | if ((compl_cont_status & CONT_INTRPT) && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3247 | { |
| 3248 | /* |
| 3249 | * it is a continued search |
| 3250 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3251 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3252 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 3253 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 3254 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3255 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3256 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3257 | /* line (probably) wrapped, set compl_startpos to the |
| 3258 | * first non_blank in the line, if it is not a wordchar |
| 3259 | * include it to get a better pattern, but then we don't |
| 3260 | * want the "\\<" prefix, check it bellow */ |
| 3261 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 3262 | compl_startpos.col = compl_col; |
| 3263 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 3264 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | } |
| 3266 | else |
| 3267 | { |
| 3268 | /* S_IPOS was set when we inserted a word that was at the |
| 3269 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3270 | * mode but first we need to redefine compl_startpos */ |
| 3271 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3272 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3273 | compl_cont_status |= CONT_SOL; |
| 3274 | compl_startpos.col = (colnr_T)(skipwhite( |
| 3275 | line + compl_length |
| 3276 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3277 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3278 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3279 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3280 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3281 | /* IObuff is used to add a "word from the next line" would we |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3282 | * have enough space? just being paranoic */ |
| 3283 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3284 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3285 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3286 | compl_cont_status &= ~CONT_SOL; |
| 3287 | compl_length = (IOSIZE - MIN_SPACE); |
| 3288 | compl_col = curwin->w_cursor.col - compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3289 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3290 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 3291 | if (compl_length < 1) |
| 3292 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3293 | } |
| 3294 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3295 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3296 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3297 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3298 | } |
| 3299 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3300 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3301 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3302 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3303 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3304 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3305 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3306 | compl_cont_status = 0; |
| 3307 | compl_cont_status |= CONT_N_ADDS; |
| 3308 | compl_startpos = curwin->w_cursor; |
| 3309 | startcol = (int)curs_col; |
| 3310 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | /* Work out completion pattern and original text -- webb */ |
| 3314 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 3315 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3316 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3317 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 3318 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3319 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3320 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3321 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3322 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3323 | compl_col += ++startcol; |
| 3324 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3325 | } |
| 3326 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3327 | compl_pattern = str_foldcase(line + compl_col, |
| 3328 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3329 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3330 | compl_pattern = vim_strnsave(line + compl_col, |
| 3331 | compl_length); |
| 3332 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3333 | return FAIL; |
| 3334 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3335 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3336 | { |
| 3337 | char_u *prefix = (char_u *)"\\<"; |
| 3338 | |
| 3339 | /* we need 3 extra chars, 1 for the NUL and |
| 3340 | * 2 >= strlen(prefix) -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3341 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 3342 | compl_length) + 3); |
| 3343 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3344 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3345 | if (!vim_iswordp(line + compl_col) |
| 3346 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3347 | && ( |
| 3348 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3349 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3350 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3351 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3352 | #endif |
| 3353 | ))) |
| 3354 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3355 | STRCPY((char *)compl_pattern, prefix); |
| 3356 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 3357 | line + compl_col, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3358 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3359 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3360 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3361 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3362 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3363 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3364 | #endif |
| 3365 | ) |
| 3366 | { |
| 3367 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3368 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 3369 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3370 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3371 | compl_col += curs_col; |
| 3372 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3373 | } |
| 3374 | else |
| 3375 | { |
| 3376 | #ifdef FEAT_MBYTE |
| 3377 | /* Search the point of change class of multibyte character |
| 3378 | * or not a word single byte character backward. */ |
| 3379 | if (has_mbyte) |
| 3380 | { |
| 3381 | int base_class; |
| 3382 | int head_off; |
| 3383 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3384 | startcol -= (*mb_head_off)(line, line + startcol); |
| 3385 | base_class = mb_get_class(line + startcol); |
| 3386 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3387 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3388 | head_off = (*mb_head_off)(line, line + startcol); |
| 3389 | if (base_class != mb_get_class(line + startcol |
| 3390 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3391 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3392 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } |
| 3395 | else |
| 3396 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3397 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3398 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3399 | compl_col += ++startcol; |
| 3400 | compl_length = (int)curs_col - startcol; |
| 3401 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3402 | { |
| 3403 | /* Only match word with at least two chars -- webb |
| 3404 | * there's no need to call quote_meta, |
| 3405 | * alloc(7) is enough -- Acevedo |
| 3406 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3407 | compl_pattern = alloc(7); |
| 3408 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3409 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3410 | STRCPY((char *)compl_pattern, "\\<"); |
| 3411 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 3412 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3413 | } |
| 3414 | else |
| 3415 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3416 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 3417 | compl_length) + 3); |
| 3418 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3420 | STRCPY((char *)compl_pattern, "\\<"); |
| 3421 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 3422 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3423 | } |
| 3424 | } |
| 3425 | } |
| 3426 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3427 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3428 | compl_col = skipwhite(line) - line; |
| 3429 | compl_length = (int)curs_col - (int)compl_col; |
| 3430 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 3431 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3433 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 3434 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3435 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3436 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 3437 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3438 | return FAIL; |
| 3439 | } |
| 3440 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 3441 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3442 | while (--startcol >= 0 && vim_isfilec(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3443 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3444 | compl_col += ++startcol; |
| 3445 | compl_length = (int)curs_col - startcol; |
| 3446 | compl_pattern = addstar(line + compl_col, compl_length, |
| 3447 | EXPAND_FILES); |
| 3448 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | return FAIL; |
| 3450 | } |
| 3451 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 3452 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3453 | compl_pattern = vim_strnsave(line, curs_col); |
| 3454 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3455 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3456 | set_cmd_context(&compl_xp, compl_pattern, |
| 3457 | (int)STRLEN(compl_pattern), curs_col); |
| 3458 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 3459 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3460 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3461 | startcol = (int)(compl_xp.xp_pattern - compl_pattern); |
| 3462 | compl_col = startcol; |
| 3463 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3464 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3465 | else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3466 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3467 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3468 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3469 | * Call user defined function 'completefunc' with "a:findstart" |
| 3470 | * set to 1 to obtain the length of text to use for completion. |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3471 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3472 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3473 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3474 | char_u *funcname; |
| 3475 | pos_T pos; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3476 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3477 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3478 | * string */ |
| 3479 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 3480 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3481 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3482 | { |
| 3483 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 3484 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3485 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3486 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3487 | |
| 3488 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3489 | args[1] = NULL; |
| 3490 | pos = curwin->w_cursor; |
| 3491 | col = call_func_retnr(funcname, 2, args, FALSE); |
| 3492 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3493 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3494 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3495 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3496 | compl_col = col; |
| 3497 | if ((colnr_T)compl_col > curs_col) |
| 3498 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3499 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3500 | /* Setup variables for completion. Need to obtain "line" again, |
| 3501 | * it may have become invalid. */ |
| 3502 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3503 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3504 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 3505 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3506 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3507 | return FAIL; |
| 3508 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3509 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 3510 | { |
| 3511 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 3512 | if (spell_bad_len > 0) |
| 3513 | compl_col = curs_col - spell_bad_len; |
| 3514 | else |
| 3515 | compl_col = spell_word_start(startcol); |
| 3516 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3517 | return FAIL; |
Bram Moolenaar | c54b8a7 | 2005-09-30 21:20:29 +0000 | [diff] [blame] | 3518 | spell_expand_check_cap(compl_col); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3519 | compl_length = (int)curs_col - compl_col; |
| 3520 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 3521 | if (compl_pattern == NULL) |
| 3522 | #endif |
| 3523 | return FAIL; |
| 3524 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3525 | else |
| 3526 | { |
| 3527 | EMSG2(_(e_intern2), "ins_complete()"); |
| 3528 | return FAIL; |
| 3529 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3530 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3531 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3532 | { |
| 3533 | edit_submode_pre = (char_u *)_(" Adding"); |
| 3534 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3535 | { |
| 3536 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 3537 | #ifdef FEAT_COMMENTS |
| 3538 | char_u *old = curbuf->b_p_com; |
| 3539 | |
| 3540 | curbuf->b_p_com = (char_u *)""; |
| 3541 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3542 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 3543 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3544 | ins_eol('\r'); |
| 3545 | #ifdef FEAT_COMMENTS |
| 3546 | curbuf->b_p_com = old; |
| 3547 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3548 | compl_length = 0; |
| 3549 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3550 | } |
| 3551 | } |
| 3552 | else |
| 3553 | { |
| 3554 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3555 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3558 | if (compl_cont_status & CONT_LOCAL) |
| 3559 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3560 | else |
| 3561 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 3562 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3563 | /* Always add completion for the original text. Note that |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3564 | * "compl_orig_text" itself (not a copy) is added, it will be freed |
| 3565 | * when the list of matches is freed. */ |
| 3566 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 3567 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
| 3568 | -1, NULL, 0, ORIGINAL_TEXT) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3569 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3570 | vim_free(compl_pattern); |
| 3571 | compl_pattern = NULL; |
| 3572 | vim_free(compl_orig_text); |
| 3573 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3574 | return FAIL; |
| 3575 | } |
| 3576 | |
| 3577 | /* showmode might reset the internal line pointers, so it must |
| 3578 | * be called before line = ml_get(), or when this address is no |
| 3579 | * longer needed. -- Acevedo. |
| 3580 | */ |
| 3581 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 3582 | edit_submode_highl = HLF_COUNT; |
| 3583 | showmode(); |
| 3584 | edit_submode_extra = NULL; |
| 3585 | out_flush(); |
| 3586 | } |
| 3587 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3588 | compl_shown_match = compl_curr_match; |
| 3589 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3590 | |
| 3591 | /* |
| 3592 | * Find next match. |
| 3593 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3594 | n = ins_compl_next(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3595 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3596 | if (n > 1) /* all matches have been found */ |
| 3597 | compl_matches = n; |
| 3598 | compl_curr_match = compl_shown_match; |
| 3599 | compl_direction = compl_shows_dir; |
| 3600 | compl_interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | |
| 3602 | /* eat the ESC to avoid leaving insert mode */ |
| 3603 | if (got_int && !global_busy) |
| 3604 | { |
| 3605 | (void)vgetc(); |
| 3606 | got_int = FALSE; |
| 3607 | } |
| 3608 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3609 | /* we found no match if the list has only the "compl_orig_text"-entry */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3610 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3611 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3612 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 3613 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3614 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 3615 | edit_submode_highl = HLF_E; |
| 3616 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 3617 | * because we couldn't expand anything at first place, but if we used |
| 3618 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 3619 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3620 | if ( compl_length > 1 |
| 3621 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3622 | || (ctrl_x_mode != 0 |
| 3623 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 3624 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3625 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3628 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3629 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3630 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3631 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3632 | |
| 3633 | if (edit_submode_extra == NULL) |
| 3634 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3635 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3636 | { |
| 3637 | edit_submode_extra = (char_u *)_("Back at original"); |
| 3638 | edit_submode_highl = HLF_W; |
| 3639 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3640 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3641 | { |
| 3642 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 3643 | edit_submode_highl = HLF_COUNT; |
| 3644 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3645 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3646 | { |
| 3647 | edit_submode_extra = (char_u *)_("The only match"); |
| 3648 | edit_submode_highl = HLF_COUNT; |
| 3649 | } |
| 3650 | else |
| 3651 | { |
| 3652 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3653 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3654 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3655 | int number = 0; |
| 3656 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3658 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3659 | { |
| 3660 | /* search backwards for the first valid (!= -1) number. |
| 3661 | * This should normally succeed already at the first loop |
| 3662 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3663 | for (match = compl_curr_match->cp_prev; match != NULL |
| 3664 | && match != compl_first_match; |
| 3665 | match = match->cp_prev) |
| 3666 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3667 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3668 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3669 | break; |
| 3670 | } |
| 3671 | if (match != NULL) |
| 3672 | /* go up and assign all numbers which are not assigned |
| 3673 | * yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3674 | for (match = match->cp_next; match |
| 3675 | && match->cp_number == -1; |
| 3676 | match = match->cp_next) |
| 3677 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3678 | } |
| 3679 | else /* BACKWARD */ |
| 3680 | { |
| 3681 | /* search forwards (upwards) for the first valid (!= -1) |
| 3682 | * number. This should normally succeed already at the |
| 3683 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3684 | for (match = compl_curr_match->cp_next; match != NULL |
| 3685 | && match != compl_first_match; |
| 3686 | match = match->cp_next) |
| 3687 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3688 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3689 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | break; |
| 3691 | } |
| 3692 | if (match != NULL) |
| 3693 | /* go down and assign all numbers which are not |
| 3694 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3695 | for (match = match->cp_prev; match |
| 3696 | && match->cp_number == -1; |
| 3697 | match = match->cp_prev) |
| 3698 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3699 | } |
| 3700 | } |
| 3701 | |
| 3702 | /* The match should always have a sequnce number now, this is just |
| 3703 | * a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3704 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3705 | { |
| 3706 | /* Space for 10 text chars. + 2x10-digit no.s */ |
| 3707 | static char_u match_ref[31]; |
| 3708 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3709 | if (compl_matches > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3710 | sprintf((char *)IObuff, _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3711 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3712 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3713 | sprintf((char *)IObuff, _("match %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3714 | compl_curr_match->cp_number); |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3715 | vim_strncpy(match_ref, IObuff, 30); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3716 | edit_submode_extra = match_ref; |
| 3717 | edit_submode_highl = HLF_R; |
| 3718 | if (dollar_vcol) |
| 3719 | curs_columns(FALSE); |
| 3720 | } |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | /* Show a message about what (completion) mode we're in. */ |
| 3725 | showmode(); |
| 3726 | if (edit_submode_extra != NULL) |
| 3727 | { |
| 3728 | if (!p_smd) |
| 3729 | msg_attr(edit_submode_extra, |
| 3730 | edit_submode_highl < HLF_COUNT |
| 3731 | ? hl_attr(edit_submode_highl) : 0); |
| 3732 | } |
| 3733 | else |
| 3734 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 3735 | |
| 3736 | return OK; |
| 3737 | } |
| 3738 | |
| 3739 | /* |
| 3740 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 3741 | * If dest is not NULL the chars. are copied there quoting (with |
| 3742 | * a backslash) the metachars, and dest would be NUL terminated. |
| 3743 | * Returns the length (needed) of dest |
| 3744 | */ |
| 3745 | static int |
| 3746 | quote_meta(dest, src, len) |
| 3747 | char_u *dest; |
| 3748 | char_u *src; |
| 3749 | int len; |
| 3750 | { |
| 3751 | int m; |
| 3752 | |
| 3753 | for (m = len; --len >= 0; src++) |
| 3754 | { |
| 3755 | switch (*src) |
| 3756 | { |
| 3757 | case '.': |
| 3758 | case '*': |
| 3759 | case '[': |
| 3760 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 3761 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 3762 | break; |
| 3763 | case '~': |
| 3764 | if (!p_magic) /* quote these only if magic is set */ |
| 3765 | break; |
| 3766 | case '\\': |
| 3767 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 3768 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 3769 | break; |
| 3770 | case '^': /* currently it's not needed. */ |
| 3771 | case '$': |
| 3772 | m++; |
| 3773 | if (dest != NULL) |
| 3774 | *dest++ = '\\'; |
| 3775 | break; |
| 3776 | } |
| 3777 | if (dest != NULL) |
| 3778 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3779 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3780 | /* Copy remaining bytes of a multibyte character. */ |
| 3781 | if (has_mbyte) |
| 3782 | { |
| 3783 | int i, mb_len; |
| 3784 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3785 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3786 | if (mb_len > 0 && len >= mb_len) |
| 3787 | for (i = 0; i < mb_len; ++i) |
| 3788 | { |
| 3789 | --len; |
| 3790 | ++src; |
| 3791 | if (dest != NULL) |
| 3792 | *dest++ = *src; |
| 3793 | } |
| 3794 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3795 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3796 | } |
| 3797 | if (dest != NULL) |
| 3798 | *dest = NUL; |
| 3799 | |
| 3800 | return m; |
| 3801 | } |
| 3802 | #endif /* FEAT_INS_EXPAND */ |
| 3803 | |
| 3804 | /* |
| 3805 | * Next character is interpreted literally. |
| 3806 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 3807 | * If one or two digits are entered, the next character is given to vungetc(). |
| 3808 | * For Unicode a character > 255 may be returned. |
| 3809 | */ |
| 3810 | int |
| 3811 | get_literal() |
| 3812 | { |
| 3813 | int cc; |
| 3814 | int nc; |
| 3815 | int i; |
| 3816 | int hex = FALSE; |
| 3817 | int octal = FALSE; |
| 3818 | #ifdef FEAT_MBYTE |
| 3819 | int unicode = 0; |
| 3820 | #endif |
| 3821 | |
| 3822 | if (got_int) |
| 3823 | return Ctrl_C; |
| 3824 | |
| 3825 | #ifdef FEAT_GUI |
| 3826 | /* |
| 3827 | * In GUI there is no point inserting the internal code for a special key. |
| 3828 | * It is more useful to insert the string "<KEY>" instead. This would |
| 3829 | * probably be useful in a text window too, but it would not be |
| 3830 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 3831 | */ |
| 3832 | if (gui.in_use) |
| 3833 | ++allow_keys; |
| 3834 | #endif |
| 3835 | #ifdef USE_ON_FLY_SCROLL |
| 3836 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 3837 | #endif |
| 3838 | ++no_mapping; /* don't map the next key hits */ |
| 3839 | cc = 0; |
| 3840 | i = 0; |
| 3841 | for (;;) |
| 3842 | { |
| 3843 | do |
| 3844 | nc = safe_vgetc(); |
| 3845 | while (nc == K_IGNORE || nc == K_VER_SCROLLBAR |
| 3846 | || nc == K_HOR_SCROLLBAR); |
| 3847 | #ifdef FEAT_CMDL_INFO |
| 3848 | if (!(State & CMDLINE) |
| 3849 | # ifdef FEAT_MBYTE |
| 3850 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 3851 | # endif |
| 3852 | ) |
| 3853 | add_to_showcmd(nc); |
| 3854 | #endif |
| 3855 | if (nc == 'x' || nc == 'X') |
| 3856 | hex = TRUE; |
| 3857 | else if (nc == 'o' || nc == 'O') |
| 3858 | octal = TRUE; |
| 3859 | #ifdef FEAT_MBYTE |
| 3860 | else if (nc == 'u' || nc == 'U') |
| 3861 | unicode = nc; |
| 3862 | #endif |
| 3863 | else |
| 3864 | { |
| 3865 | if (hex |
| 3866 | #ifdef FEAT_MBYTE |
| 3867 | || unicode != 0 |
| 3868 | #endif |
| 3869 | ) |
| 3870 | { |
| 3871 | if (!vim_isxdigit(nc)) |
| 3872 | break; |
| 3873 | cc = cc * 16 + hex2nr(nc); |
| 3874 | } |
| 3875 | else if (octal) |
| 3876 | { |
| 3877 | if (nc < '0' || nc > '7') |
| 3878 | break; |
| 3879 | cc = cc * 8 + nc - '0'; |
| 3880 | } |
| 3881 | else |
| 3882 | { |
| 3883 | if (!VIM_ISDIGIT(nc)) |
| 3884 | break; |
| 3885 | cc = cc * 10 + nc - '0'; |
| 3886 | } |
| 3887 | |
| 3888 | ++i; |
| 3889 | } |
| 3890 | |
| 3891 | if (cc > 255 |
| 3892 | #ifdef FEAT_MBYTE |
| 3893 | && unicode == 0 |
| 3894 | #endif |
| 3895 | ) |
| 3896 | cc = 255; /* limit range to 0-255 */ |
| 3897 | nc = 0; |
| 3898 | |
| 3899 | if (hex) /* hex: up to two chars */ |
| 3900 | { |
| 3901 | if (i >= 2) |
| 3902 | break; |
| 3903 | } |
| 3904 | #ifdef FEAT_MBYTE |
| 3905 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 3906 | { |
| 3907 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 3908 | break; |
| 3909 | } |
| 3910 | #endif |
| 3911 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 3912 | break; |
| 3913 | } |
| 3914 | if (i == 0) /* no number entered */ |
| 3915 | { |
| 3916 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 3917 | { |
| 3918 | cc = '\n'; |
| 3919 | nc = 0; |
| 3920 | } |
| 3921 | else |
| 3922 | { |
| 3923 | cc = nc; |
| 3924 | nc = 0; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | if (cc == 0) /* NUL is stored as NL */ |
| 3929 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 3930 | #ifdef FEAT_MBYTE |
| 3931 | if (enc_dbcs && (cc & 0xff) == 0) |
| 3932 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 3933 | second byte will cause trouble! */ |
| 3934 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3935 | |
| 3936 | --no_mapping; |
| 3937 | #ifdef FEAT_GUI |
| 3938 | if (gui.in_use) |
| 3939 | --allow_keys; |
| 3940 | #endif |
| 3941 | if (nc) |
| 3942 | vungetc(nc); |
| 3943 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 3944 | return cc; |
| 3945 | } |
| 3946 | |
| 3947 | /* |
| 3948 | * Insert character, taking care of special keys and mod_mask |
| 3949 | */ |
| 3950 | static void |
| 3951 | insert_special(c, allow_modmask, ctrlv) |
| 3952 | int c; |
| 3953 | int allow_modmask; |
| 3954 | int ctrlv; /* c was typed after CTRL-V */ |
| 3955 | { |
| 3956 | char_u *p; |
| 3957 | int len; |
| 3958 | |
| 3959 | /* |
| 3960 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 3961 | * inserted with ins_str(), so as not to replace characters in replace |
| 3962 | * mode. |
| 3963 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 3964 | * unless 'allow_modmask' is TRUE. |
| 3965 | */ |
| 3966 | #ifdef MACOS |
| 3967 | /* Command-key never produces a normal key */ |
| 3968 | if (mod_mask & MOD_MASK_CMD) |
| 3969 | allow_modmask = TRUE; |
| 3970 | #endif |
| 3971 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 3972 | { |
| 3973 | p = get_special_key_name(c, mod_mask); |
| 3974 | len = (int)STRLEN(p); |
| 3975 | c = p[len - 1]; |
| 3976 | if (len > 2) |
| 3977 | { |
| 3978 | if (stop_arrow() == FAIL) |
| 3979 | return; |
| 3980 | p[len - 1] = NUL; |
| 3981 | ins_str(p); |
| 3982 | AppendToRedobuffLit(p); |
| 3983 | ctrlv = FALSE; |
| 3984 | } |
| 3985 | } |
| 3986 | if (stop_arrow() == OK) |
| 3987 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 3988 | } |
| 3989 | |
| 3990 | /* |
| 3991 | * Special characters in this context are those that need processing other |
| 3992 | * than the simple insertion that can be performed here. This includes ESC |
| 3993 | * which terminates the insert, and CR/NL which need special processing to |
| 3994 | * open up a new line. This routine tries to optimize insertions performed by |
| 3995 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 3996 | * stop and defer processing to the "normal" mechanism. |
| 3997 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 3998 | */ |
| 3999 | #ifdef EBCDIC |
| 4000 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 4001 | #else |
| 4002 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 4003 | #endif |
| 4004 | |
| 4005 | #ifdef FEAT_MBYTE |
| 4006 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 4007 | #else |
| 4008 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 4009 | #endif |
| 4010 | |
| 4011 | void |
| 4012 | insertchar(c, flags, second_indent) |
| 4013 | int c; /* character to insert or NUL */ |
| 4014 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 4015 | int second_indent; /* indent for second line if >= 0 */ |
| 4016 | { |
| 4017 | int haveto_redraw = FALSE; |
| 4018 | int textwidth; |
| 4019 | #ifdef FEAT_COMMENTS |
| 4020 | colnr_T leader_len; |
| 4021 | char_u *p; |
| 4022 | int no_leader = FALSE; |
| 4023 | int do_comments = (flags & INSCHAR_DO_COM); |
| 4024 | #endif |
| 4025 | int fo_white_par; |
| 4026 | int first_line = TRUE; |
| 4027 | int fo_ins_blank; |
| 4028 | #ifdef FEAT_MBYTE |
| 4029 | int fo_multibyte; |
| 4030 | #endif |
| 4031 | int save_char = NUL; |
| 4032 | int cc; |
| 4033 | |
| 4034 | textwidth = comp_textwidth(flags & INSCHAR_FORMAT); |
| 4035 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 4036 | #ifdef FEAT_MBYTE |
| 4037 | fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 4038 | #endif |
| 4039 | fo_white_par = has_format_option(FO_WHITE_PAR); |
| 4040 | |
| 4041 | /* |
| 4042 | * Try to break the line in two or more pieces when: |
| 4043 | * - Always do this if we have been called to do formatting only. |
| 4044 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 4045 | * ends in white space. |
| 4046 | * - Otherwise: |
| 4047 | * - Don't do this if inserting a blank |
| 4048 | * - Don't do this if an existing character is being replaced, unless |
| 4049 | * we're in VREPLACE mode. |
| 4050 | * - Do this if the cursor is not on the line where insert started |
| 4051 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 4052 | * before the insert. |
| 4053 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 4054 | * before 'textwidth' |
| 4055 | */ |
| 4056 | if (textwidth |
| 4057 | && ((flags & INSCHAR_FORMAT) |
| 4058 | || (!vim_iswhite(c) |
| 4059 | && !((State & REPLACE_FLAG) |
| 4060 | #ifdef FEAT_VREPLACE |
| 4061 | && !(State & VREPLACE_FLAG) |
| 4062 | #endif |
| 4063 | && *ml_get_cursor() != NUL) |
| 4064 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 4065 | || ((!has_format_option(FO_INS_LONG) |
| 4066 | || Insstart_textlen <= (colnr_T)textwidth) |
| 4067 | && (!fo_ins_blank |
| 4068 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 4069 | )))))) |
| 4070 | { |
| 4071 | /* |
| 4072 | * When 'ai' is off we don't want a space under the cursor to be |
| 4073 | * deleted. Replace it with an 'x' temporarily. |
| 4074 | */ |
| 4075 | if (!curbuf->b_p_ai) |
| 4076 | { |
| 4077 | cc = gchar_cursor(); |
| 4078 | if (vim_iswhite(cc)) |
| 4079 | { |
| 4080 | save_char = cc; |
| 4081 | pchar_cursor('x'); |
| 4082 | } |
| 4083 | } |
| 4084 | |
| 4085 | /* |
| 4086 | * Repeat breaking lines, until the current line is not too long. |
| 4087 | */ |
| 4088 | while (!got_int) |
| 4089 | { |
| 4090 | int startcol; /* Cursor column at entry */ |
| 4091 | int wantcol; /* column at textwidth border */ |
| 4092 | int foundcol; /* column for start of spaces */ |
| 4093 | int end_foundcol = 0; /* column for start of word */ |
| 4094 | colnr_T len; |
| 4095 | colnr_T virtcol; |
| 4096 | #ifdef FEAT_VREPLACE |
| 4097 | int orig_col = 0; |
| 4098 | char_u *saved_text = NULL; |
| 4099 | #endif |
| 4100 | colnr_T col; |
| 4101 | |
| 4102 | virtcol = get_nolist_virtcol(); |
| 4103 | if (virtcol < (colnr_T)textwidth) |
| 4104 | break; |
| 4105 | |
| 4106 | #ifdef FEAT_COMMENTS |
| 4107 | if (no_leader) |
| 4108 | do_comments = FALSE; |
| 4109 | else if (!(flags & INSCHAR_FORMAT) |
| 4110 | && has_format_option(FO_WRAP_COMS)) |
| 4111 | do_comments = TRUE; |
| 4112 | |
| 4113 | /* Don't break until after the comment leader */ |
| 4114 | if (do_comments) |
| 4115 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE); |
| 4116 | else |
| 4117 | leader_len = 0; |
| 4118 | |
| 4119 | /* If the line doesn't start with a comment leader, then don't |
| 4120 | * start one in a following broken line. Avoids that a %word |
| 4121 | * moved to the start of the next line causes all following lines |
| 4122 | * to start with %. */ |
| 4123 | if (leader_len == 0) |
| 4124 | no_leader = TRUE; |
| 4125 | #endif |
| 4126 | if (!(flags & INSCHAR_FORMAT) |
| 4127 | #ifdef FEAT_COMMENTS |
| 4128 | && leader_len == 0 |
| 4129 | #endif |
| 4130 | && !has_format_option(FO_WRAP)) |
| 4131 | |
| 4132 | { |
| 4133 | textwidth = 0; |
| 4134 | break; |
| 4135 | } |
| 4136 | if ((startcol = curwin->w_cursor.col) == 0) |
| 4137 | break; |
| 4138 | |
| 4139 | /* find column of textwidth border */ |
| 4140 | coladvance((colnr_T)textwidth); |
| 4141 | wantcol = curwin->w_cursor.col; |
| 4142 | |
| 4143 | curwin->w_cursor.col = startcol - 1; |
| 4144 | #ifdef FEAT_MBYTE |
| 4145 | /* Correct cursor for multi-byte character. */ |
| 4146 | if (has_mbyte) |
| 4147 | mb_adjust_cursor(); |
| 4148 | #endif |
| 4149 | foundcol = 0; |
| 4150 | |
| 4151 | /* |
| 4152 | * Find position to break at. |
| 4153 | * Stop at first entered white when 'formatoptions' has 'v' |
| 4154 | */ |
| 4155 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
| 4156 | || curwin->w_cursor.lnum != Insstart.lnum |
| 4157 | || curwin->w_cursor.col >= Insstart.col) |
| 4158 | { |
| 4159 | cc = gchar_cursor(); |
| 4160 | if (WHITECHAR(cc)) |
| 4161 | { |
| 4162 | /* remember position of blank just before text */ |
| 4163 | end_foundcol = curwin->w_cursor.col; |
| 4164 | |
| 4165 | /* find start of sequence of blanks */ |
| 4166 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 4167 | { |
| 4168 | dec_cursor(); |
| 4169 | cc = gchar_cursor(); |
| 4170 | } |
| 4171 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 4172 | break; /* only spaces in front of text */ |
| 4173 | #ifdef FEAT_COMMENTS |
| 4174 | /* Don't break until after the comment leader */ |
| 4175 | if (curwin->w_cursor.col < leader_len) |
| 4176 | break; |
| 4177 | #endif |
| 4178 | if (has_format_option(FO_ONE_LETTER)) |
| 4179 | { |
| 4180 | /* do not break after one-letter words */ |
| 4181 | if (curwin->w_cursor.col == 0) |
| 4182 | break; /* one-letter word at begin */ |
| 4183 | |
| 4184 | col = curwin->w_cursor.col; |
| 4185 | dec_cursor(); |
| 4186 | cc = gchar_cursor(); |
| 4187 | |
| 4188 | if (WHITECHAR(cc)) |
| 4189 | continue; /* one-letter, continue */ |
| 4190 | curwin->w_cursor.col = col; |
| 4191 | } |
| 4192 | #ifdef FEAT_MBYTE |
| 4193 | if (has_mbyte) |
| 4194 | foundcol = curwin->w_cursor.col |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4195 | + (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4196 | else |
| 4197 | #endif |
| 4198 | foundcol = curwin->w_cursor.col + 1; |
| 4199 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 4200 | break; |
| 4201 | } |
| 4202 | #ifdef FEAT_MBYTE |
| 4203 | else if (cc >= 0x100 && fo_multibyte |
| 4204 | && curwin->w_cursor.col <= (colnr_T)wantcol) |
| 4205 | { |
| 4206 | /* Break after or before a multi-byte character. */ |
| 4207 | foundcol = curwin->w_cursor.col; |
| 4208 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 4209 | foundcol += (*mb_char2len)(cc); |
| 4210 | end_foundcol = foundcol; |
| 4211 | break; |
| 4212 | } |
| 4213 | #endif |
| 4214 | if (curwin->w_cursor.col == 0) |
| 4215 | break; |
| 4216 | dec_cursor(); |
| 4217 | } |
| 4218 | |
| 4219 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 4220 | { |
| 4221 | curwin->w_cursor.col = startcol; |
| 4222 | break; |
| 4223 | } |
| 4224 | |
| 4225 | /* Going to break the line, remove any "$" now. */ |
| 4226 | undisplay_dollar(); |
| 4227 | |
| 4228 | /* |
| 4229 | * Offset between cursor position and line break is used by replace |
| 4230 | * stack functions. VREPLACE does not use this, and backspaces |
| 4231 | * over the text instead. |
| 4232 | */ |
| 4233 | #ifdef FEAT_VREPLACE |
| 4234 | if (State & VREPLACE_FLAG) |
| 4235 | orig_col = startcol; /* Will start backspacing from here */ |
| 4236 | else |
| 4237 | #endif |
| 4238 | replace_offset = startcol - end_foundcol - 1; |
| 4239 | |
| 4240 | /* |
| 4241 | * adjust startcol for spaces that will be deleted and |
| 4242 | * characters that will remain on top line |
| 4243 | */ |
| 4244 | curwin->w_cursor.col = foundcol; |
| 4245 | while (cc = gchar_cursor(), WHITECHAR(cc)) |
| 4246 | inc_cursor(); |
| 4247 | startcol -= curwin->w_cursor.col; |
| 4248 | if (startcol < 0) |
| 4249 | startcol = 0; |
| 4250 | |
| 4251 | #ifdef FEAT_VREPLACE |
| 4252 | if (State & VREPLACE_FLAG) |
| 4253 | { |
| 4254 | /* |
| 4255 | * In VREPLACE mode, we will backspace over the text to be |
| 4256 | * wrapped, so save a copy now to put on the next line. |
| 4257 | */ |
| 4258 | saved_text = vim_strsave(ml_get_cursor()); |
| 4259 | curwin->w_cursor.col = orig_col; |
| 4260 | if (saved_text == NULL) |
| 4261 | break; /* Can't do it, out of memory */ |
| 4262 | saved_text[startcol] = NUL; |
| 4263 | |
| 4264 | /* Backspace over characters that will move to the next line */ |
| 4265 | if (!fo_white_par) |
| 4266 | backspace_until_column(foundcol); |
| 4267 | } |
| 4268 | else |
| 4269 | #endif |
| 4270 | { |
| 4271 | /* put cursor after pos. to break line */ |
| 4272 | if (!fo_white_par) |
| 4273 | curwin->w_cursor.col = foundcol; |
| 4274 | } |
| 4275 | |
| 4276 | /* |
| 4277 | * Split the line just before the margin. |
| 4278 | * Only insert/delete lines, but don't really redraw the window. |
| 4279 | */ |
| 4280 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 4281 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 4282 | #ifdef FEAT_COMMENTS |
| 4283 | + (do_comments ? OPENLINE_DO_COM : 0) |
| 4284 | #endif |
| 4285 | , old_indent); |
| 4286 | old_indent = 0; |
| 4287 | |
| 4288 | replace_offset = 0; |
| 4289 | if (first_line) |
| 4290 | { |
| 4291 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
| 4292 | second_indent = get_number_indent(curwin->w_cursor.lnum -1); |
| 4293 | if (second_indent >= 0) |
| 4294 | { |
| 4295 | #ifdef FEAT_VREPLACE |
| 4296 | if (State & VREPLACE_FLAG) |
| 4297 | change_indent(INDENT_SET, second_indent, FALSE, NUL); |
| 4298 | else |
| 4299 | #endif |
| 4300 | (void)set_indent(second_indent, SIN_CHANGED); |
| 4301 | } |
| 4302 | first_line = FALSE; |
| 4303 | } |
| 4304 | |
| 4305 | #ifdef FEAT_VREPLACE |
| 4306 | if (State & VREPLACE_FLAG) |
| 4307 | { |
| 4308 | /* |
| 4309 | * In VREPLACE mode we have backspaced over the text to be |
| 4310 | * moved, now we re-insert it into the new line. |
| 4311 | */ |
| 4312 | ins_bytes(saved_text); |
| 4313 | vim_free(saved_text); |
| 4314 | } |
| 4315 | else |
| 4316 | #endif |
| 4317 | { |
| 4318 | /* |
| 4319 | * Check if cursor is not past the NUL off the line, cindent |
| 4320 | * may have added or removed indent. |
| 4321 | */ |
| 4322 | curwin->w_cursor.col += startcol; |
| 4323 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 4324 | if (curwin->w_cursor.col > len) |
| 4325 | curwin->w_cursor.col = len; |
| 4326 | } |
| 4327 | |
| 4328 | haveto_redraw = TRUE; |
| 4329 | #ifdef FEAT_CINDENT |
| 4330 | can_cindent = TRUE; |
| 4331 | #endif |
| 4332 | /* moved the cursor, don't autoindent or cindent now */ |
| 4333 | did_ai = FALSE; |
| 4334 | #ifdef FEAT_SMARTINDENT |
| 4335 | did_si = FALSE; |
| 4336 | can_si = FALSE; |
| 4337 | can_si_back = FALSE; |
| 4338 | #endif |
| 4339 | line_breakcheck(); |
| 4340 | } |
| 4341 | |
| 4342 | if (save_char) /* put back space after cursor */ |
| 4343 | pchar_cursor(save_char); |
| 4344 | |
| 4345 | if (c == NUL) /* formatting only */ |
| 4346 | return; |
| 4347 | if (haveto_redraw) |
| 4348 | { |
| 4349 | update_topline(); |
| 4350 | redraw_curbuf_later(VALID); |
| 4351 | } |
| 4352 | } |
| 4353 | if (c == NUL) /* only formatting was wanted */ |
| 4354 | return; |
| 4355 | |
| 4356 | #ifdef FEAT_COMMENTS |
| 4357 | /* Check whether this character should end a comment. */ |
| 4358 | if (did_ai && (int)c == end_comment_pending) |
| 4359 | { |
| 4360 | char_u *line; |
| 4361 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 4362 | int middle_len, end_len; |
| 4363 | int i; |
| 4364 | |
| 4365 | /* |
| 4366 | * Need to remove existing (middle) comment leader and insert end |
| 4367 | * comment leader. First, check what comment leader we can find. |
| 4368 | */ |
| 4369 | i = get_leader_len(line = ml_get_curline(), &p, FALSE); |
| 4370 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 4371 | { |
| 4372 | /* Skip middle-comment string */ |
| 4373 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 4374 | ++p; |
| 4375 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 4376 | /* Don't count trailing white space for middle_len */ |
| 4377 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 4378 | --middle_len; |
| 4379 | |
| 4380 | /* Find the end-comment string */ |
| 4381 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 4382 | ++p; |
| 4383 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 4384 | |
| 4385 | /* Skip white space before the cursor */ |
| 4386 | i = curwin->w_cursor.col; |
| 4387 | while (--i >= 0 && vim_iswhite(line[i])) |
| 4388 | ; |
| 4389 | i++; |
| 4390 | |
| 4391 | /* Skip to before the middle leader */ |
| 4392 | i -= middle_len; |
| 4393 | |
| 4394 | /* Check some expected things before we go on */ |
| 4395 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 4396 | { |
| 4397 | /* Backspace over all the stuff we want to replace */ |
| 4398 | backspace_until_column(i); |
| 4399 | |
| 4400 | /* |
| 4401 | * Insert the end-comment string, except for the last |
| 4402 | * character, which will get inserted as normal later. |
| 4403 | */ |
| 4404 | ins_bytes_len(lead_end, end_len - 1); |
| 4405 | } |
| 4406 | } |
| 4407 | } |
| 4408 | end_comment_pending = NUL; |
| 4409 | #endif |
| 4410 | |
| 4411 | did_ai = FALSE; |
| 4412 | #ifdef FEAT_SMARTINDENT |
| 4413 | did_si = FALSE; |
| 4414 | can_si = FALSE; |
| 4415 | can_si_back = FALSE; |
| 4416 | #endif |
| 4417 | |
| 4418 | /* |
| 4419 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 4420 | * This speeds up normal text input considerably. |
| 4421 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 4422 | * need to re-indent at a ':', or any other character (but not what |
| 4423 | * 'paste' is set).. |
| 4424 | */ |
| 4425 | #ifdef USE_ON_FLY_SCROLL |
| 4426 | dont_scroll = FALSE; /* allow scrolling here */ |
| 4427 | #endif |
| 4428 | |
| 4429 | if ( !ISSPECIAL(c) |
| 4430 | #ifdef FEAT_MBYTE |
| 4431 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 4432 | #endif |
| 4433 | && vpeekc() != NUL |
| 4434 | && !(State & REPLACE_FLAG) |
| 4435 | #ifdef FEAT_CINDENT |
| 4436 | && !cindent_on() |
| 4437 | #endif |
| 4438 | #ifdef FEAT_RIGHTLEFT |
| 4439 | && !p_ri |
| 4440 | #endif |
| 4441 | ) |
| 4442 | { |
| 4443 | #define INPUT_BUFLEN 100 |
| 4444 | char_u buf[INPUT_BUFLEN + 1]; |
| 4445 | int i; |
| 4446 | colnr_T virtcol = 0; |
| 4447 | |
| 4448 | buf[0] = c; |
| 4449 | i = 1; |
| 4450 | if (textwidth) |
| 4451 | virtcol = get_nolist_virtcol(); |
| 4452 | /* |
| 4453 | * Stop the string when: |
| 4454 | * - no more chars available |
| 4455 | * - finding a special character (command key) |
| 4456 | * - buffer is full |
| 4457 | * - running into the 'textwidth' boundary |
| 4458 | * - need to check for abbreviation: A non-word char after a word-char |
| 4459 | */ |
| 4460 | while ( (c = vpeekc()) != NUL |
| 4461 | && !ISSPECIAL(c) |
| 4462 | #ifdef FEAT_MBYTE |
| 4463 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 4464 | #endif |
| 4465 | && i < INPUT_BUFLEN |
| 4466 | && (textwidth == 0 |
| 4467 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 4468 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 4469 | { |
| 4470 | #ifdef FEAT_RIGHTLEFT |
| 4471 | c = vgetc(); |
| 4472 | if (p_hkmap && KeyTyped) |
| 4473 | c = hkmap(c); /* Hebrew mode mapping */ |
| 4474 | # ifdef FEAT_FKMAP |
| 4475 | if (p_fkmap && KeyTyped) |
| 4476 | c = fkmap(c); /* Farsi mode mapping */ |
| 4477 | # endif |
| 4478 | buf[i++] = c; |
| 4479 | #else |
| 4480 | buf[i++] = vgetc(); |
| 4481 | #endif |
| 4482 | } |
| 4483 | |
| 4484 | #ifdef FEAT_DIGRAPHS |
| 4485 | do_digraph(-1); /* clear digraphs */ |
| 4486 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 4487 | #endif |
| 4488 | buf[i] = NUL; |
| 4489 | ins_str(buf); |
| 4490 | if (flags & INSCHAR_CTRLV) |
| 4491 | { |
| 4492 | redo_literal(*buf); |
| 4493 | i = 1; |
| 4494 | } |
| 4495 | else |
| 4496 | i = 0; |
| 4497 | if (buf[i] != NUL) |
| 4498 | AppendToRedobuffLit(buf + i); |
| 4499 | } |
| 4500 | else |
| 4501 | { |
| 4502 | #ifdef FEAT_MBYTE |
| 4503 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 4504 | { |
| 4505 | char_u buf[MB_MAXBYTES + 1]; |
| 4506 | |
| 4507 | (*mb_char2bytes)(c, buf); |
| 4508 | buf[cc] = NUL; |
| 4509 | ins_char_bytes(buf, cc); |
| 4510 | AppendCharToRedobuff(c); |
| 4511 | } |
| 4512 | else |
| 4513 | #endif |
| 4514 | { |
| 4515 | ins_char(c); |
| 4516 | if (flags & INSCHAR_CTRLV) |
| 4517 | redo_literal(c); |
| 4518 | else |
| 4519 | AppendCharToRedobuff(c); |
| 4520 | } |
| 4521 | } |
| 4522 | } |
| 4523 | |
| 4524 | /* |
| 4525 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 4526 | * 'a' flag format from the current line until the end of the paragraph. |
| 4527 | * Keep the cursor at the same position relative to the text. |
| 4528 | * The caller must have saved the cursor line for undo, following ones will be |
| 4529 | * saved here. |
| 4530 | */ |
| 4531 | void |
| 4532 | auto_format(trailblank, prev_line) |
| 4533 | int trailblank; /* when TRUE also format with trailing blank */ |
| 4534 | int prev_line; /* may start in previous line */ |
| 4535 | { |
| 4536 | pos_T pos; |
| 4537 | colnr_T len; |
| 4538 | char_u *old; |
| 4539 | char_u *new, *pnew; |
| 4540 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 4541 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4542 | |
| 4543 | if (!has_format_option(FO_AUTO)) |
| 4544 | return; |
| 4545 | |
| 4546 | pos = curwin->w_cursor; |
| 4547 | old = ml_get_curline(); |
| 4548 | |
| 4549 | /* may remove added space */ |
| 4550 | check_auto_format(FALSE); |
| 4551 | |
| 4552 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 4553 | * user might insert normal text next. Also skip formatting when "1" is |
| 4554 | * in 'formatoptions' and there is a single character before the cursor. |
| 4555 | * Otherwise the line would be broken and when typing another non-white |
| 4556 | * next they are not joined back together. */ |
| 4557 | wasatend = (pos.col == STRLEN(old)); |
| 4558 | if (*old != NUL && !trailblank && wasatend) |
| 4559 | { |
| 4560 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 4561 | cc = gchar_cursor(); |
| 4562 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 4563 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4564 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 4565 | cc = gchar_cursor(); |
| 4566 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4567 | { |
| 4568 | curwin->w_cursor = pos; |
| 4569 | return; |
| 4570 | } |
| 4571 | curwin->w_cursor = pos; |
| 4572 | } |
| 4573 | |
| 4574 | #ifdef FEAT_COMMENTS |
| 4575 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 4576 | * comments. */ |
| 4577 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
| 4578 | && get_leader_len(old, NULL, FALSE) == 0) |
| 4579 | return; |
| 4580 | #endif |
| 4581 | |
| 4582 | /* |
| 4583 | * May start formatting in a previous line, so that after "x" a word is |
| 4584 | * moved to the previous line if it fits there now. Only when this is not |
| 4585 | * the start of a paragraph. |
| 4586 | */ |
| 4587 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 4588 | { |
| 4589 | --curwin->w_cursor.lnum; |
| 4590 | if (u_save_cursor() == FAIL) |
| 4591 | return; |
| 4592 | } |
| 4593 | |
| 4594 | /* |
| 4595 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 4596 | * be adjusted for the text formatting. |
| 4597 | */ |
| 4598 | saved_cursor = pos; |
| 4599 | format_lines((linenr_T)-1); |
| 4600 | curwin->w_cursor = saved_cursor; |
| 4601 | saved_cursor.lnum = 0; |
| 4602 | |
| 4603 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 4604 | { |
| 4605 | /* "cannot happen" */ |
| 4606 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 4607 | coladvance((colnr_T)MAXCOL); |
| 4608 | } |
| 4609 | else |
| 4610 | check_cursor_col(); |
| 4611 | |
| 4612 | /* Insert mode: If the cursor is now after the end of the line while it |
| 4613 | * previously wasn't, the line was broken. Because of the rule above we |
| 4614 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 4615 | * formatted. */ |
| 4616 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 4617 | { |
| 4618 | new = ml_get_curline(); |
| 4619 | len = STRLEN(new); |
| 4620 | if (curwin->w_cursor.col == len) |
| 4621 | { |
| 4622 | pnew = vim_strnsave(new, len + 2); |
| 4623 | pnew[len] = ' '; |
| 4624 | pnew[len + 1] = NUL; |
| 4625 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 4626 | /* remove the space later */ |
| 4627 | did_add_space = TRUE; |
| 4628 | } |
| 4629 | else |
| 4630 | /* may remove added space */ |
| 4631 | check_auto_format(FALSE); |
| 4632 | } |
| 4633 | |
| 4634 | check_cursor(); |
| 4635 | } |
| 4636 | |
| 4637 | /* |
| 4638 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 4639 | * delete it now. The space must be under the cursor, just after the insert |
| 4640 | * position. |
| 4641 | */ |
| 4642 | static void |
| 4643 | check_auto_format(end_insert) |
| 4644 | int end_insert; /* TRUE when ending Insert mode */ |
| 4645 | { |
| 4646 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 4647 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4648 | |
| 4649 | if (did_add_space) |
| 4650 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 4651 | cc = gchar_cursor(); |
| 4652 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4653 | /* Somehow the space was removed already. */ |
| 4654 | did_add_space = FALSE; |
| 4655 | else |
| 4656 | { |
| 4657 | if (!end_insert) |
| 4658 | { |
| 4659 | inc_cursor(); |
| 4660 | c = gchar_cursor(); |
| 4661 | dec_cursor(); |
| 4662 | } |
| 4663 | if (c != NUL) |
| 4664 | { |
| 4665 | /* The space is no longer at the end of the line, delete it. */ |
| 4666 | del_char(FALSE); |
| 4667 | did_add_space = FALSE; |
| 4668 | } |
| 4669 | } |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | /* |
| 4674 | * Find out textwidth to be used for formatting: |
| 4675 | * if 'textwidth' option is set, use it |
| 4676 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 4677 | * if invalid value, use 0. |
| 4678 | * Set default to window width (maximum 79) for "gq" operator. |
| 4679 | */ |
| 4680 | int |
| 4681 | comp_textwidth(ff) |
| 4682 | int ff; /* force formatting (for "Q" command) */ |
| 4683 | { |
| 4684 | int textwidth; |
| 4685 | |
| 4686 | textwidth = curbuf->b_p_tw; |
| 4687 | if (textwidth == 0 && curbuf->b_p_wm) |
| 4688 | { |
| 4689 | /* The width is the window width minus 'wrapmargin' minus all the |
| 4690 | * things that add to the margin. */ |
| 4691 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 4692 | #ifdef FEAT_CMDWIN |
| 4693 | if (cmdwin_type != 0) |
| 4694 | textwidth -= 1; |
| 4695 | #endif |
| 4696 | #ifdef FEAT_FOLDING |
| 4697 | textwidth -= curwin->w_p_fdc; |
| 4698 | #endif |
| 4699 | #ifdef FEAT_SIGNS |
| 4700 | if (curwin->w_buffer->b_signlist != NULL |
| 4701 | # ifdef FEAT_NETBEANS_INTG |
| 4702 | || usingNetbeans |
| 4703 | # endif |
| 4704 | ) |
| 4705 | textwidth -= 1; |
| 4706 | #endif |
| 4707 | if (curwin->w_p_nu) |
| 4708 | textwidth -= 8; |
| 4709 | } |
| 4710 | if (textwidth < 0) |
| 4711 | textwidth = 0; |
| 4712 | if (ff && textwidth == 0) |
| 4713 | { |
| 4714 | textwidth = W_WIDTH(curwin) - 1; |
| 4715 | if (textwidth > 79) |
| 4716 | textwidth = 79; |
| 4717 | } |
| 4718 | return textwidth; |
| 4719 | } |
| 4720 | |
| 4721 | /* |
| 4722 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 4723 | */ |
| 4724 | static void |
| 4725 | redo_literal(c) |
| 4726 | int c; |
| 4727 | { |
| 4728 | char_u buf[10]; |
| 4729 | |
| 4730 | /* Only digits need special treatment. Translate them into a string of |
| 4731 | * three digits. */ |
| 4732 | if (VIM_ISDIGIT(c)) |
| 4733 | { |
| 4734 | sprintf((char *)buf, "%03d", c); |
| 4735 | AppendToRedobuff(buf); |
| 4736 | } |
| 4737 | else |
| 4738 | AppendCharToRedobuff(c); |
| 4739 | } |
| 4740 | |
| 4741 | /* |
| 4742 | * start_arrow() is called when an arrow key is used in insert mode. |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 4743 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4744 | */ |
| 4745 | static void |
| 4746 | start_arrow(end_insert_pos) |
| 4747 | pos_T *end_insert_pos; |
| 4748 | { |
| 4749 | if (!arrow_used) /* something has been inserted */ |
| 4750 | { |
| 4751 | AppendToRedobuff(ESC_STR); |
| 4752 | stop_insert(end_insert_pos, FALSE); |
| 4753 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 4754 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 4755 | #ifdef FEAT_SYN_HL |
| 4756 | check_spell_redraw(); |
| 4757 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 4760 | #ifdef FEAT_SYN_HL |
| 4761 | /* |
| 4762 | * If we skipped highlighting word at cursor, do it now. |
| 4763 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 4764 | */ |
| 4765 | static void |
| 4766 | check_spell_redraw() |
| 4767 | { |
| 4768 | if (spell_redraw_lnum != 0) |
| 4769 | { |
| 4770 | linenr_T lnum = spell_redraw_lnum; |
| 4771 | |
| 4772 | spell_redraw_lnum = 0; |
| 4773 | redrawWinline(lnum, FALSE); |
| 4774 | } |
| 4775 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 4776 | |
| 4777 | /* |
| 4778 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 4779 | * spelled word, if there is one. |
| 4780 | */ |
| 4781 | static void |
| 4782 | spell_back_to_badword() |
| 4783 | { |
| 4784 | pos_T tpos = curwin->w_cursor; |
| 4785 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 4786 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 4787 | if (curwin->w_cursor.col != tpos.col) |
| 4788 | start_arrow(&tpos); |
| 4789 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 4790 | #endif |
| 4791 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4792 | /* |
| 4793 | * stop_arrow() is called before a change is made in insert mode. |
| 4794 | * If an arrow key has been used, start a new insertion. |
| 4795 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 4796 | */ |
| 4797 | int |
| 4798 | stop_arrow() |
| 4799 | { |
| 4800 | if (arrow_used) |
| 4801 | { |
| 4802 | if (u_save_cursor() == OK) |
| 4803 | { |
| 4804 | arrow_used = FALSE; |
| 4805 | ins_need_undo = FALSE; |
| 4806 | } |
| 4807 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
| 4808 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 4809 | ai_col = 0; |
| 4810 | #ifdef FEAT_VREPLACE |
| 4811 | if (State & VREPLACE_FLAG) |
| 4812 | { |
| 4813 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 4814 | vr_lines_changed = 1; |
| 4815 | } |
| 4816 | #endif |
| 4817 | ResetRedobuff(); |
| 4818 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
| 4819 | } |
| 4820 | else if (ins_need_undo) |
| 4821 | { |
| 4822 | if (u_save_cursor() == OK) |
| 4823 | ins_need_undo = FALSE; |
| 4824 | } |
| 4825 | |
| 4826 | #ifdef FEAT_FOLDING |
| 4827 | /* Always open fold at the cursor line when inserting something. */ |
| 4828 | foldOpenCursor(); |
| 4829 | #endif |
| 4830 | |
| 4831 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 4832 | } |
| 4833 | |
| 4834 | /* |
| 4835 | * do a few things to stop inserting |
| 4836 | */ |
| 4837 | static void |
| 4838 | stop_insert(end_insert_pos, esc) |
| 4839 | pos_T *end_insert_pos; /* where insert ended */ |
| 4840 | int esc; /* called by ins_esc() */ |
| 4841 | { |
| 4842 | int cc; |
| 4843 | |
| 4844 | stop_redo_ins(); |
| 4845 | replace_flush(); /* abandon replace stack */ |
| 4846 | |
| 4847 | /* |
| 4848 | * save the inserted text for later redo with ^@ |
| 4849 | */ |
| 4850 | vim_free(last_insert); |
| 4851 | last_insert = get_inserted(); |
| 4852 | last_insert_skip = new_insert_skip; |
| 4853 | |
| 4854 | if (!arrow_used) |
| 4855 | { |
| 4856 | /* Auto-format now. It may seem strange to do this when stopping an |
| 4857 | * insertion (or moving the cursor), but it's required when appending |
| 4858 | * a line and having it end in a space. But only do it when something |
| 4859 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4860 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4861 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4862 | pos_T tpos = curwin->w_cursor; |
| 4863 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4864 | /* When the cursor is at the end of the line after a space the |
| 4865 | * formatting will move it to the following word. Avoid that by |
| 4866 | * moving the cursor onto the space. */ |
| 4867 | cc = 'x'; |
| 4868 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 4869 | { |
| 4870 | dec_cursor(); |
| 4871 | cc = gchar_cursor(); |
| 4872 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4873 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | auto_format(TRUE, FALSE); |
| 4877 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4878 | if (vim_iswhite(cc)) |
| 4879 | { |
| 4880 | if (gchar_cursor() != NUL) |
| 4881 | inc_cursor(); |
| 4882 | #ifdef FEAT_VIRTUALEDIT |
| 4883 | /* If the cursor is still at the same character, also keep |
| 4884 | * the "coladd". */ |
| 4885 | if (gchar_cursor() == NUL |
| 4886 | && curwin->w_cursor.lnum == tpos.lnum |
| 4887 | && curwin->w_cursor.col == tpos.col) |
| 4888 | curwin->w_cursor.coladd = tpos.coladd; |
| 4889 | #endif |
| 4890 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 4894 | check_auto_format(TRUE); |
| 4895 | |
| 4896 | /* If we just did an auto-indent, remove the white space from the end |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4897 | * of the line, and put the cursor back. |
| 4898 | * Do this when ESC was used or moving the cursor up/down. */ |
| 4899 | if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
| 4900 | && curwin->w_cursor.lnum != end_insert_pos->lnum))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4901 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4902 | pos_T tpos = curwin->w_cursor; |
| 4903 | |
| 4904 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 4906 | --curwin->w_cursor.col; |
| 4907 | while (cc = gchar_cursor(), vim_iswhite(cc)) |
| 4908 | (void)del_char(TRUE); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 4909 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 4910 | curwin->w_cursor = tpos; |
| 4911 | else if (cc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4912 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 4913 | |
| 4914 | #ifdef FEAT_VISUAL |
| 4915 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 4916 | * deleted characters. */ |
| 4917 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 4918 | { |
| 4919 | cc = STRLEN(ml_get_curline()); |
| 4920 | if (VIsual.col > (colnr_T)cc) |
| 4921 | { |
| 4922 | VIsual.col = cc; |
| 4923 | # ifdef FEAT_VIRTUALEDIT |
| 4924 | VIsual.coladd = 0; |
| 4925 | # endif |
| 4926 | } |
| 4927 | } |
| 4928 | #endif |
| 4929 | } |
| 4930 | } |
| 4931 | did_ai = FALSE; |
| 4932 | #ifdef FEAT_SMARTINDENT |
| 4933 | did_si = FALSE; |
| 4934 | can_si = FALSE; |
| 4935 | can_si_back = FALSE; |
| 4936 | #endif |
| 4937 | |
| 4938 | /* set '[ and '] to the inserted text */ |
| 4939 | curbuf->b_op_start = Insstart; |
| 4940 | curbuf->b_op_end = *end_insert_pos; |
| 4941 | } |
| 4942 | |
| 4943 | /* |
| 4944 | * Set the last inserted text to a single character. |
| 4945 | * Used for the replace command. |
| 4946 | */ |
| 4947 | void |
| 4948 | set_last_insert(c) |
| 4949 | int c; |
| 4950 | { |
| 4951 | char_u *s; |
| 4952 | |
| 4953 | vim_free(last_insert); |
| 4954 | #ifdef FEAT_MBYTE |
| 4955 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
| 4956 | #else |
| 4957 | last_insert = alloc(6); |
| 4958 | #endif |
| 4959 | if (last_insert != NULL) |
| 4960 | { |
| 4961 | s = last_insert; |
| 4962 | /* Use the CTRL-V only when entering a special char */ |
| 4963 | if (c < ' ' || c == DEL) |
| 4964 | *s++ = Ctrl_V; |
| 4965 | s = add_char2buf(c, s); |
| 4966 | *s++ = ESC; |
| 4967 | *s++ = NUL; |
| 4968 | last_insert_skip = 0; |
| 4969 | } |
| 4970 | } |
| 4971 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 4972 | #if defined(EXITFREE) || defined(PROTO) |
| 4973 | void |
| 4974 | free_last_insert() |
| 4975 | { |
| 4976 | vim_free(last_insert); |
| 4977 | last_insert = NULL; |
| 4978 | } |
| 4979 | #endif |
| 4980 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4981 | /* |
| 4982 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 4983 | * and CSI. Handle multi-byte characters. |
| 4984 | * Returns a pointer to after the added bytes. |
| 4985 | */ |
| 4986 | char_u * |
| 4987 | add_char2buf(c, s) |
| 4988 | int c; |
| 4989 | char_u *s; |
| 4990 | { |
| 4991 | #ifdef FEAT_MBYTE |
| 4992 | char_u temp[MB_MAXBYTES]; |
| 4993 | int i; |
| 4994 | int len; |
| 4995 | |
| 4996 | len = (*mb_char2bytes)(c, temp); |
| 4997 | for (i = 0; i < len; ++i) |
| 4998 | { |
| 4999 | c = temp[i]; |
| 5000 | #endif |
| 5001 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 5002 | if (c == K_SPECIAL) |
| 5003 | { |
| 5004 | *s++ = K_SPECIAL; |
| 5005 | *s++ = KS_SPECIAL; |
| 5006 | *s++ = KE_FILLER; |
| 5007 | } |
| 5008 | #ifdef FEAT_GUI |
| 5009 | else if (c == CSI) |
| 5010 | { |
| 5011 | *s++ = CSI; |
| 5012 | *s++ = KS_EXTRA; |
| 5013 | *s++ = (int)KE_CSI; |
| 5014 | } |
| 5015 | #endif |
| 5016 | else |
| 5017 | *s++ = c; |
| 5018 | #ifdef FEAT_MBYTE |
| 5019 | } |
| 5020 | #endif |
| 5021 | return s; |
| 5022 | } |
| 5023 | |
| 5024 | /* |
| 5025 | * move cursor to start of line |
| 5026 | * if flags & BL_WHITE move to first non-white |
| 5027 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 5028 | * otherwise keep "curswant" column |
| 5029 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 5030 | */ |
| 5031 | void |
| 5032 | beginline(flags) |
| 5033 | int flags; |
| 5034 | { |
| 5035 | if ((flags & BL_SOL) && !p_sol) |
| 5036 | coladvance(curwin->w_curswant); |
| 5037 | else |
| 5038 | { |
| 5039 | curwin->w_cursor.col = 0; |
| 5040 | #ifdef FEAT_VIRTUALEDIT |
| 5041 | curwin->w_cursor.coladd = 0; |
| 5042 | #endif |
| 5043 | |
| 5044 | if (flags & (BL_WHITE | BL_SOL)) |
| 5045 | { |
| 5046 | char_u *ptr; |
| 5047 | |
| 5048 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 5049 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 5050 | ++curwin->w_cursor.col; |
| 5051 | } |
| 5052 | curwin->w_set_curswant = TRUE; |
| 5053 | } |
| 5054 | } |
| 5055 | |
| 5056 | /* |
| 5057 | * oneright oneleft cursor_down cursor_up |
| 5058 | * |
| 5059 | * Move one char {right,left,down,up}. |
| 5060 | * Doesn't move onto the NUL past the end of the line. |
| 5061 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 5062 | */ |
| 5063 | |
| 5064 | int |
| 5065 | oneright() |
| 5066 | { |
| 5067 | char_u *ptr; |
| 5068 | #ifdef FEAT_MBYTE |
| 5069 | int l; |
| 5070 | #endif |
| 5071 | |
| 5072 | #ifdef FEAT_VIRTUALEDIT |
| 5073 | if (virtual_active()) |
| 5074 | { |
| 5075 | pos_T prevpos = curwin->w_cursor; |
| 5076 | |
| 5077 | /* Adjust for multi-wide char (excluding TAB) */ |
| 5078 | ptr = ml_get_cursor(); |
| 5079 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
| 5080 | #ifdef FEAT_MBYTE |
| 5081 | (*mb_ptr2char)(ptr) |
| 5082 | #else |
| 5083 | *ptr |
| 5084 | #endif |
| 5085 | )) |
| 5086 | ? ptr2cells(ptr) : 1)); |
| 5087 | curwin->w_set_curswant = TRUE; |
| 5088 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 5089 | return (prevpos.col != curwin->w_cursor.col |
| 5090 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 5091 | } |
| 5092 | #endif |
| 5093 | |
| 5094 | ptr = ml_get_cursor(); |
| 5095 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5096 | if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5097 | { |
| 5098 | /* The character under the cursor is a multi-byte character, move |
| 5099 | * several bytes right, but don't end up on the NUL. */ |
| 5100 | if (ptr[l] == NUL) |
| 5101 | return FAIL; |
| 5102 | curwin->w_cursor.col += l; |
| 5103 | } |
| 5104 | else |
| 5105 | #endif |
| 5106 | { |
| 5107 | if (*ptr++ == NUL || *ptr == NUL) |
| 5108 | return FAIL; |
| 5109 | ++curwin->w_cursor.col; |
| 5110 | } |
| 5111 | |
| 5112 | curwin->w_set_curswant = TRUE; |
| 5113 | return OK; |
| 5114 | } |
| 5115 | |
| 5116 | int |
| 5117 | oneleft() |
| 5118 | { |
| 5119 | #ifdef FEAT_VIRTUALEDIT |
| 5120 | if (virtual_active()) |
| 5121 | { |
| 5122 | int width; |
| 5123 | int v = getviscol(); |
| 5124 | |
| 5125 | if (v == 0) |
| 5126 | return FAIL; |
| 5127 | |
| 5128 | # ifdef FEAT_LINEBREAK |
| 5129 | /* We might get stuck on 'showbreak', skip over it. */ |
| 5130 | width = 1; |
| 5131 | for (;;) |
| 5132 | { |
| 5133 | coladvance(v - width); |
| 5134 | /* getviscol() is slow, skip it when 'showbreak' is empty and |
| 5135 | * there are no multi-byte characters */ |
| 5136 | if ((*p_sbr == NUL |
| 5137 | # ifdef FEAT_MBYTE |
| 5138 | && !has_mbyte |
| 5139 | # endif |
| 5140 | ) || getviscol() < v) |
| 5141 | break; |
| 5142 | ++width; |
| 5143 | } |
| 5144 | # else |
| 5145 | coladvance(v - 1); |
| 5146 | # endif |
| 5147 | |
| 5148 | if (curwin->w_cursor.coladd == 1) |
| 5149 | { |
| 5150 | char_u *ptr; |
| 5151 | |
| 5152 | /* Adjust for multi-wide char (not a TAB) */ |
| 5153 | ptr = ml_get_cursor(); |
| 5154 | if (*ptr != TAB && vim_isprintc( |
| 5155 | # ifdef FEAT_MBYTE |
| 5156 | (*mb_ptr2char)(ptr) |
| 5157 | # else |
| 5158 | *ptr |
| 5159 | # endif |
| 5160 | ) && ptr2cells(ptr) > 1) |
| 5161 | curwin->w_cursor.coladd = 0; |
| 5162 | } |
| 5163 | |
| 5164 | curwin->w_set_curswant = TRUE; |
| 5165 | return OK; |
| 5166 | } |
| 5167 | #endif |
| 5168 | |
| 5169 | if (curwin->w_cursor.col == 0) |
| 5170 | return FAIL; |
| 5171 | |
| 5172 | curwin->w_set_curswant = TRUE; |
| 5173 | --curwin->w_cursor.col; |
| 5174 | |
| 5175 | #ifdef FEAT_MBYTE |
| 5176 | /* if the character on the left of the current cursor is a multi-byte |
| 5177 | * character, move to its first byte */ |
| 5178 | if (has_mbyte) |
| 5179 | mb_adjust_cursor(); |
| 5180 | #endif |
| 5181 | return OK; |
| 5182 | } |
| 5183 | |
| 5184 | int |
| 5185 | cursor_up(n, upd_topline) |
| 5186 | long n; |
| 5187 | int upd_topline; /* When TRUE: update topline */ |
| 5188 | { |
| 5189 | linenr_T lnum; |
| 5190 | |
| 5191 | if (n > 0) |
| 5192 | { |
| 5193 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 5194 | /* This fails if the cursor is already in the first line or the count |
| 5195 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 5196 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5197 | return FAIL; |
| 5198 | if (n >= lnum) |
| 5199 | lnum = 1; |
| 5200 | else |
| 5201 | #ifdef FEAT_FOLDING |
| 5202 | if (hasAnyFolding(curwin)) |
| 5203 | { |
| 5204 | /* |
| 5205 | * Count each sequence of folded lines as one logical line. |
| 5206 | */ |
| 5207 | /* go to the the start of the current fold */ |
| 5208 | (void)hasFolding(lnum, &lnum, NULL); |
| 5209 | |
| 5210 | while (n--) |
| 5211 | { |
| 5212 | /* move up one line */ |
| 5213 | --lnum; |
| 5214 | if (lnum <= 1) |
| 5215 | break; |
| 5216 | /* If we entered a fold, move to the beginning, unless in |
| 5217 | * Insert mode or when 'foldopen' contains "all": it will open |
| 5218 | * in a moment. */ |
| 5219 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 5220 | (void)hasFolding(lnum, &lnum, NULL); |
| 5221 | } |
| 5222 | if (lnum < 1) |
| 5223 | lnum = 1; |
| 5224 | } |
| 5225 | else |
| 5226 | #endif |
| 5227 | lnum -= n; |
| 5228 | curwin->w_cursor.lnum = lnum; |
| 5229 | } |
| 5230 | |
| 5231 | /* try to advance to the column we want to be at */ |
| 5232 | coladvance(curwin->w_curswant); |
| 5233 | |
| 5234 | if (upd_topline) |
| 5235 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 5236 | |
| 5237 | return OK; |
| 5238 | } |
| 5239 | |
| 5240 | /* |
| 5241 | * Cursor down a number of logical lines. |
| 5242 | */ |
| 5243 | int |
| 5244 | cursor_down(n, upd_topline) |
| 5245 | long n; |
| 5246 | int upd_topline; /* When TRUE: update topline */ |
| 5247 | { |
| 5248 | linenr_T lnum; |
| 5249 | |
| 5250 | if (n > 0) |
| 5251 | { |
| 5252 | lnum = curwin->w_cursor.lnum; |
| 5253 | #ifdef FEAT_FOLDING |
| 5254 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 5255 | (void)hasFolding(lnum, NULL, &lnum); |
| 5256 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 5257 | /* This fails if the cursor is already in the last line or would move |
| 5258 | * beyound the last line and '-' is in 'cpoptions' */ |
| 5259 | if (lnum >= curbuf->b_ml.ml_line_count |
| 5260 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 5261 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5262 | return FAIL; |
| 5263 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 5264 | lnum = curbuf->b_ml.ml_line_count; |
| 5265 | else |
| 5266 | #ifdef FEAT_FOLDING |
| 5267 | if (hasAnyFolding(curwin)) |
| 5268 | { |
| 5269 | linenr_T last; |
| 5270 | |
| 5271 | /* count each sequence of folded lines as one logical line */ |
| 5272 | while (n--) |
| 5273 | { |
| 5274 | if (hasFolding(lnum, NULL, &last)) |
| 5275 | lnum = last + 1; |
| 5276 | else |
| 5277 | ++lnum; |
| 5278 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 5279 | break; |
| 5280 | } |
| 5281 | if (lnum > curbuf->b_ml.ml_line_count) |
| 5282 | lnum = curbuf->b_ml.ml_line_count; |
| 5283 | } |
| 5284 | else |
| 5285 | #endif |
| 5286 | lnum += n; |
| 5287 | curwin->w_cursor.lnum = lnum; |
| 5288 | } |
| 5289 | |
| 5290 | /* try to advance to the column we want to be at */ |
| 5291 | coladvance(curwin->w_curswant); |
| 5292 | |
| 5293 | if (upd_topline) |
| 5294 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 5295 | |
| 5296 | return OK; |
| 5297 | } |
| 5298 | |
| 5299 | /* |
| 5300 | * Stuff the last inserted text in the read buffer. |
| 5301 | * Last_insert actually is a copy of the redo buffer, so we |
| 5302 | * first have to remove the command. |
| 5303 | */ |
| 5304 | int |
| 5305 | stuff_inserted(c, count, no_esc) |
| 5306 | int c; /* Command character to be inserted */ |
| 5307 | long count; /* Repeat this many times */ |
| 5308 | int no_esc; /* Don't add an ESC at the end */ |
| 5309 | { |
| 5310 | char_u *esc_ptr; |
| 5311 | char_u *ptr; |
| 5312 | char_u *last_ptr; |
| 5313 | char_u last = NUL; |
| 5314 | |
| 5315 | ptr = get_last_insert(); |
| 5316 | if (ptr == NULL) |
| 5317 | { |
| 5318 | EMSG(_(e_noinstext)); |
| 5319 | return FAIL; |
| 5320 | } |
| 5321 | |
| 5322 | /* may want to stuff the command character, to start Insert mode */ |
| 5323 | if (c != NUL) |
| 5324 | stuffcharReadbuff(c); |
| 5325 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 5326 | *esc_ptr = NUL; /* remove the ESC */ |
| 5327 | |
| 5328 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 5329 | * comes after it OR if it will inserted more than once and "ptr" |
| 5330 | * starts with ^D. -- Acevedo |
| 5331 | */ |
| 5332 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 5333 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 5334 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 5335 | { |
| 5336 | last = *last_ptr; |
| 5337 | *last_ptr = NUL; |
| 5338 | } |
| 5339 | |
| 5340 | do |
| 5341 | { |
| 5342 | stuffReadbuff(ptr); |
| 5343 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 5344 | if (last) |
| 5345 | stuffReadbuff((char_u *)(last == '0' |
| 5346 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 5347 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 5348 | } |
| 5349 | while (--count > 0); |
| 5350 | |
| 5351 | if (last) |
| 5352 | *last_ptr = last; |
| 5353 | |
| 5354 | if (esc_ptr != NULL) |
| 5355 | *esc_ptr = ESC; /* put the ESC back */ |
| 5356 | |
| 5357 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 5358 | if (!no_esc) |
| 5359 | stuffcharReadbuff(ESC); |
| 5360 | |
| 5361 | return OK; |
| 5362 | } |
| 5363 | |
| 5364 | char_u * |
| 5365 | get_last_insert() |
| 5366 | { |
| 5367 | if (last_insert == NULL) |
| 5368 | return NULL; |
| 5369 | return last_insert + last_insert_skip; |
| 5370 | } |
| 5371 | |
| 5372 | /* |
| 5373 | * Get last inserted string, and remove trailing <Esc>. |
| 5374 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 5375 | */ |
| 5376 | char_u * |
| 5377 | get_last_insert_save() |
| 5378 | { |
| 5379 | char_u *s; |
| 5380 | int len; |
| 5381 | |
| 5382 | if (last_insert == NULL) |
| 5383 | return NULL; |
| 5384 | s = vim_strsave(last_insert + last_insert_skip); |
| 5385 | if (s != NULL) |
| 5386 | { |
| 5387 | len = (int)STRLEN(s); |
| 5388 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 5389 | s[len - 1] = NUL; |
| 5390 | } |
| 5391 | return s; |
| 5392 | } |
| 5393 | |
| 5394 | /* |
| 5395 | * Check the word in front of the cursor for an abbreviation. |
| 5396 | * Called when the non-id character "c" has been entered. |
| 5397 | * When an abbreviation is recognized it is removed from the text and |
| 5398 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 5399 | */ |
| 5400 | static int |
| 5401 | echeck_abbr(c) |
| 5402 | int c; |
| 5403 | { |
| 5404 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 5405 | * after moving around with cursor keys. */ |
| 5406 | if (p_paste || no_abbr || arrow_used) |
| 5407 | return FALSE; |
| 5408 | |
| 5409 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 5410 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 5411 | } |
| 5412 | |
| 5413 | /* |
| 5414 | * replace-stack functions |
| 5415 | * |
| 5416 | * When replacing characters, the replaced characters are remembered for each |
| 5417 | * new character. This is used to re-insert the old text when backspacing. |
| 5418 | * |
| 5419 | * There is a NUL headed list of characters for each character that is |
| 5420 | * currently in the file after the insertion point. When BS is used, one NUL |
| 5421 | * headed list is put back for the deleted character. |
| 5422 | * |
| 5423 | * For a newline, there are two NUL headed lists. One contains the characters |
| 5424 | * that the NL replaced. The extra one stores the characters after the cursor |
| 5425 | * that were deleted (always white space). |
| 5426 | * |
| 5427 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 5428 | * character at the end of the stack. If replace_offset is not 0, that many |
| 5429 | * characters will be left on the stack above the newly inserted character. |
| 5430 | */ |
| 5431 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 5432 | static char_u *replace_stack = NULL; |
| 5433 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 5434 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | |
| 5436 | void |
| 5437 | replace_push(c) |
| 5438 | int c; /* character that is replaced (NUL is none) */ |
| 5439 | { |
| 5440 | char_u *p; |
| 5441 | |
| 5442 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 5443 | return; |
| 5444 | if (replace_stack_len <= replace_stack_nr) |
| 5445 | { |
| 5446 | replace_stack_len += 50; |
| 5447 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 5448 | if (p == NULL) /* out of memory */ |
| 5449 | { |
| 5450 | replace_stack_len -= 50; |
| 5451 | return; |
| 5452 | } |
| 5453 | if (replace_stack != NULL) |
| 5454 | { |
| 5455 | mch_memmove(p, replace_stack, |
| 5456 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 5457 | vim_free(replace_stack); |
| 5458 | } |
| 5459 | replace_stack = p; |
| 5460 | } |
| 5461 | p = replace_stack + replace_stack_nr - replace_offset; |
| 5462 | if (replace_offset) |
| 5463 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 5464 | *p = c; |
| 5465 | ++replace_stack_nr; |
| 5466 | } |
| 5467 | |
| 5468 | /* |
| 5469 | * call replace_push(c) with replace_offset set to the first NUL. |
| 5470 | */ |
| 5471 | static void |
| 5472 | replace_push_off(c) |
| 5473 | int c; |
| 5474 | { |
| 5475 | char_u *p; |
| 5476 | |
| 5477 | p = replace_stack + replace_stack_nr; |
| 5478 | for (replace_offset = 1; replace_offset < replace_stack_nr; |
| 5479 | ++replace_offset) |
| 5480 | if (*--p == NUL) |
| 5481 | break; |
| 5482 | replace_push(c); |
| 5483 | replace_offset = 0; |
| 5484 | } |
| 5485 | |
| 5486 | /* |
| 5487 | * Pop one item from the replace stack. |
| 5488 | * return -1 if stack empty |
| 5489 | * return replaced character or NUL otherwise |
| 5490 | */ |
| 5491 | static int |
| 5492 | replace_pop() |
| 5493 | { |
| 5494 | if (replace_stack_nr == 0) |
| 5495 | return -1; |
| 5496 | return (int)replace_stack[--replace_stack_nr]; |
| 5497 | } |
| 5498 | |
| 5499 | /* |
| 5500 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 5501 | * encountered. |
| 5502 | */ |
| 5503 | static void |
| 5504 | replace_join(off) |
| 5505 | int off; /* offset for which NUL to remove */ |
| 5506 | { |
| 5507 | int i; |
| 5508 | |
| 5509 | for (i = replace_stack_nr; --i >= 0; ) |
| 5510 | if (replace_stack[i] == NUL && off-- <= 0) |
| 5511 | { |
| 5512 | --replace_stack_nr; |
| 5513 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 5514 | (size_t)(replace_stack_nr - i)); |
| 5515 | return; |
| 5516 | } |
| 5517 | } |
| 5518 | |
| 5519 | /* |
| 5520 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 5521 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 5522 | */ |
| 5523 | static void |
| 5524 | replace_pop_ins() |
| 5525 | { |
| 5526 | int cc; |
| 5527 | int oldState = State; |
| 5528 | |
| 5529 | State = NORMAL; /* don't want REPLACE here */ |
| 5530 | while ((cc = replace_pop()) > 0) |
| 5531 | { |
| 5532 | #ifdef FEAT_MBYTE |
| 5533 | mb_replace_pop_ins(cc); |
| 5534 | #else |
| 5535 | ins_char(cc); |
| 5536 | #endif |
| 5537 | dec_cursor(); |
| 5538 | } |
| 5539 | State = oldState; |
| 5540 | } |
| 5541 | |
| 5542 | #ifdef FEAT_MBYTE |
| 5543 | /* |
| 5544 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 5545 | * indicates a multi-byte char, pop the other bytes too. |
| 5546 | */ |
| 5547 | static void |
| 5548 | mb_replace_pop_ins(cc) |
| 5549 | int cc; |
| 5550 | { |
| 5551 | int n; |
| 5552 | char_u buf[MB_MAXBYTES]; |
| 5553 | int i; |
| 5554 | int c; |
| 5555 | |
| 5556 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 5557 | { |
| 5558 | buf[0] = cc; |
| 5559 | for (i = 1; i < n; ++i) |
| 5560 | buf[i] = replace_pop(); |
| 5561 | ins_bytes_len(buf, n); |
| 5562 | } |
| 5563 | else |
| 5564 | ins_char(cc); |
| 5565 | |
| 5566 | if (enc_utf8) |
| 5567 | /* Handle composing chars. */ |
| 5568 | for (;;) |
| 5569 | { |
| 5570 | c = replace_pop(); |
| 5571 | if (c == -1) /* stack empty */ |
| 5572 | break; |
| 5573 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 5574 | { |
| 5575 | /* Not a multi-byte char, put it back. */ |
| 5576 | replace_push(c); |
| 5577 | break; |
| 5578 | } |
| 5579 | else |
| 5580 | { |
| 5581 | buf[0] = c; |
| 5582 | for (i = 1; i < n; ++i) |
| 5583 | buf[i] = replace_pop(); |
| 5584 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 5585 | ins_bytes_len(buf, n); |
| 5586 | else |
| 5587 | { |
| 5588 | /* Not a composing char, put it back. */ |
| 5589 | for (i = n - 1; i >= 0; --i) |
| 5590 | replace_push(buf[i]); |
| 5591 | break; |
| 5592 | } |
| 5593 | } |
| 5594 | } |
| 5595 | } |
| 5596 | #endif |
| 5597 | |
| 5598 | /* |
| 5599 | * make the replace stack empty |
| 5600 | * (called when exiting replace mode) |
| 5601 | */ |
| 5602 | static void |
| 5603 | replace_flush() |
| 5604 | { |
| 5605 | vim_free(replace_stack); |
| 5606 | replace_stack = NULL; |
| 5607 | replace_stack_len = 0; |
| 5608 | replace_stack_nr = 0; |
| 5609 | } |
| 5610 | |
| 5611 | /* |
| 5612 | * Handle doing a BS for one character. |
| 5613 | * cc < 0: replace stack empty, just move cursor |
| 5614 | * cc == 0: character was inserted, delete it |
| 5615 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 5616 | * and check for more characters to be put back |
| 5617 | */ |
| 5618 | static void |
| 5619 | replace_do_bs() |
| 5620 | { |
| 5621 | int cc; |
| 5622 | #ifdef FEAT_VREPLACE |
| 5623 | int orig_len = 0; |
| 5624 | int ins_len; |
| 5625 | int orig_vcols = 0; |
| 5626 | colnr_T start_vcol; |
| 5627 | char_u *p; |
| 5628 | int i; |
| 5629 | int vcol; |
| 5630 | #endif |
| 5631 | |
| 5632 | cc = replace_pop(); |
| 5633 | if (cc > 0) |
| 5634 | { |
| 5635 | #ifdef FEAT_VREPLACE |
| 5636 | if (State & VREPLACE_FLAG) |
| 5637 | { |
| 5638 | /* Get the number of screen cells used by the character we are |
| 5639 | * going to delete. */ |
| 5640 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 5641 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 5642 | } |
| 5643 | #endif |
| 5644 | #ifdef FEAT_MBYTE |
| 5645 | if (has_mbyte) |
| 5646 | { |
| 5647 | del_char(FALSE); |
| 5648 | # ifdef FEAT_VREPLACE |
| 5649 | if (State & VREPLACE_FLAG) |
| 5650 | orig_len = STRLEN(ml_get_cursor()); |
| 5651 | # endif |
| 5652 | replace_push(cc); |
| 5653 | } |
| 5654 | else |
| 5655 | #endif |
| 5656 | { |
| 5657 | pchar_cursor(cc); |
| 5658 | #ifdef FEAT_VREPLACE |
| 5659 | if (State & VREPLACE_FLAG) |
| 5660 | orig_len = STRLEN(ml_get_cursor()) - 1; |
| 5661 | #endif |
| 5662 | } |
| 5663 | replace_pop_ins(); |
| 5664 | |
| 5665 | #ifdef FEAT_VREPLACE |
| 5666 | if (State & VREPLACE_FLAG) |
| 5667 | { |
| 5668 | /* Get the number of screen cells used by the inserted characters */ |
| 5669 | p = ml_get_cursor(); |
| 5670 | ins_len = STRLEN(p) - orig_len; |
| 5671 | vcol = start_vcol; |
| 5672 | for (i = 0; i < ins_len; ++i) |
| 5673 | { |
| 5674 | vcol += chartabsize(p + i, vcol); |
| 5675 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5676 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5677 | #endif |
| 5678 | } |
| 5679 | vcol -= start_vcol; |
| 5680 | |
| 5681 | /* Delete spaces that were inserted after the cursor to keep the |
| 5682 | * text aligned. */ |
| 5683 | curwin->w_cursor.col += ins_len; |
| 5684 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 5685 | { |
| 5686 | del_char(FALSE); |
| 5687 | ++orig_vcols; |
| 5688 | } |
| 5689 | curwin->w_cursor.col -= ins_len; |
| 5690 | } |
| 5691 | #endif |
| 5692 | |
| 5693 | /* mark the buffer as changed and prepare for displaying */ |
| 5694 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 5695 | } |
| 5696 | else if (cc == 0) |
| 5697 | (void)del_char(FALSE); |
| 5698 | } |
| 5699 | |
| 5700 | #ifdef FEAT_CINDENT |
| 5701 | /* |
| 5702 | * Return TRUE if C-indenting is on. |
| 5703 | */ |
| 5704 | static int |
| 5705 | cindent_on() |
| 5706 | { |
| 5707 | return (!p_paste && (curbuf->b_p_cin |
| 5708 | # ifdef FEAT_EVAL |
| 5709 | || *curbuf->b_p_inde != NUL |
| 5710 | # endif |
| 5711 | )); |
| 5712 | } |
| 5713 | #endif |
| 5714 | |
| 5715 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 5716 | /* |
| 5717 | * Re-indent the current line, based on the current contents of it and the |
| 5718 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 5719 | * confused what all the part that handles Control-T is doing that I'm not. |
| 5720 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 5721 | */ |
| 5722 | |
| 5723 | void |
| 5724 | fixthisline(get_the_indent) |
| 5725 | int (*get_the_indent) __ARGS((void)); |
| 5726 | { |
| 5727 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0); |
| 5728 | if (linewhite(curwin->w_cursor.lnum)) |
| 5729 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 5730 | } |
| 5731 | |
| 5732 | void |
| 5733 | fix_indent() |
| 5734 | { |
| 5735 | if (p_paste) |
| 5736 | return; |
| 5737 | # ifdef FEAT_LISP |
| 5738 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 5739 | fixthisline(get_lisp_indent); |
| 5740 | # endif |
| 5741 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 5742 | else |
| 5743 | # endif |
| 5744 | # ifdef FEAT_CINDENT |
| 5745 | if (cindent_on()) |
| 5746 | do_c_expr_indent(); |
| 5747 | # endif |
| 5748 | } |
| 5749 | |
| 5750 | #endif |
| 5751 | |
| 5752 | #ifdef FEAT_CINDENT |
| 5753 | /* |
| 5754 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 5755 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 5756 | * when == '!': Only if key is prededed with '!' (don't insert) |
| 5757 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 5758 | * |
| 5759 | * "keytyped" can have a few special values: |
| 5760 | * KEY_OPEN_FORW |
| 5761 | * KEY_OPEN_BACK |
| 5762 | * KEY_COMPLETE just finished completion. |
| 5763 | * |
| 5764 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 5765 | */ |
| 5766 | int |
| 5767 | in_cinkeys(keytyped, when, line_is_empty) |
| 5768 | int keytyped; |
| 5769 | int when; |
| 5770 | int line_is_empty; |
| 5771 | { |
| 5772 | char_u *look; |
| 5773 | int try_match; |
| 5774 | int try_match_word; |
| 5775 | char_u *p; |
| 5776 | char_u *line; |
| 5777 | int icase; |
| 5778 | int i; |
| 5779 | |
| 5780 | #ifdef FEAT_EVAL |
| 5781 | if (*curbuf->b_p_inde != NUL) |
| 5782 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 5783 | else |
| 5784 | #endif |
| 5785 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 5786 | while (*look) |
| 5787 | { |
| 5788 | /* |
| 5789 | * Find out if we want to try a match with this key, depending on |
| 5790 | * 'when' and a '*' or '!' before the key. |
| 5791 | */ |
| 5792 | switch (when) |
| 5793 | { |
| 5794 | case '*': try_match = (*look == '*'); break; |
| 5795 | case '!': try_match = (*look == '!'); break; |
| 5796 | default: try_match = (*look != '*'); break; |
| 5797 | } |
| 5798 | if (*look == '*' || *look == '!') |
| 5799 | ++look; |
| 5800 | |
| 5801 | /* |
| 5802 | * If there is a '0', only accept a match if the line is empty. |
| 5803 | * But may still match when typing last char of a word. |
| 5804 | */ |
| 5805 | if (*look == '0') |
| 5806 | { |
| 5807 | try_match_word = try_match; |
| 5808 | if (!line_is_empty) |
| 5809 | try_match = FALSE; |
| 5810 | ++look; |
| 5811 | } |
| 5812 | else |
| 5813 | try_match_word = FALSE; |
| 5814 | |
| 5815 | /* |
| 5816 | * does it look like a control character? |
| 5817 | */ |
| 5818 | if (*look == '^' |
| 5819 | #ifdef EBCDIC |
| 5820 | && (Ctrl_chr(look[1]) != 0) |
| 5821 | #else |
| 5822 | && look[1] >= '?' && look[1] <= '_' |
| 5823 | #endif |
| 5824 | ) |
| 5825 | { |
| 5826 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 5827 | return TRUE; |
| 5828 | look += 2; |
| 5829 | } |
| 5830 | /* |
| 5831 | * 'o' means "o" command, open forward. |
| 5832 | * 'O' means "O" command, open backward. |
| 5833 | */ |
| 5834 | else if (*look == 'o') |
| 5835 | { |
| 5836 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 5837 | return TRUE; |
| 5838 | ++look; |
| 5839 | } |
| 5840 | else if (*look == 'O') |
| 5841 | { |
| 5842 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 5843 | return TRUE; |
| 5844 | ++look; |
| 5845 | } |
| 5846 | |
| 5847 | /* |
| 5848 | * 'e' means to check for "else" at start of line and just before the |
| 5849 | * cursor. |
| 5850 | */ |
| 5851 | else if (*look == 'e') |
| 5852 | { |
| 5853 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 5854 | { |
| 5855 | p = ml_get_curline(); |
| 5856 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 5857 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 5858 | return TRUE; |
| 5859 | } |
| 5860 | ++look; |
| 5861 | } |
| 5862 | |
| 5863 | /* |
| 5864 | * ':' only causes an indent if it is at the end of a label or case |
| 5865 | * statement, or when it was before typing the ':' (to fix |
| 5866 | * class::method for C++). |
| 5867 | */ |
| 5868 | else if (*look == ':') |
| 5869 | { |
| 5870 | if (try_match && keytyped == ':') |
| 5871 | { |
| 5872 | p = ml_get_curline(); |
| 5873 | if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30)) |
| 5874 | return TRUE; |
| 5875 | if (curwin->w_cursor.col > 2 |
| 5876 | && p[curwin->w_cursor.col - 1] == ':' |
| 5877 | && p[curwin->w_cursor.col - 2] == ':') |
| 5878 | { |
| 5879 | p[curwin->w_cursor.col - 1] = ' '; |
| 5880 | i = (cin_iscase(p) || cin_isscopedecl(p) |
| 5881 | || cin_islabel(30)); |
| 5882 | p = ml_get_curline(); |
| 5883 | p[curwin->w_cursor.col - 1] = ':'; |
| 5884 | if (i) |
| 5885 | return TRUE; |
| 5886 | } |
| 5887 | } |
| 5888 | ++look; |
| 5889 | } |
| 5890 | |
| 5891 | |
| 5892 | /* |
| 5893 | * Is it a key in <>, maybe? |
| 5894 | */ |
| 5895 | else if (*look == '<') |
| 5896 | { |
| 5897 | if (try_match) |
| 5898 | { |
| 5899 | /* |
| 5900 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 5901 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 5902 | * >, *, : and ! keys if they really really want to. |
| 5903 | */ |
| 5904 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 5905 | && keytyped == look[1]) |
| 5906 | return TRUE; |
| 5907 | |
| 5908 | if (keytyped == get_special_key_code(look + 1)) |
| 5909 | return TRUE; |
| 5910 | } |
| 5911 | while (*look && *look != '>') |
| 5912 | look++; |
| 5913 | while (*look == '>') |
| 5914 | look++; |
| 5915 | } |
| 5916 | |
| 5917 | /* |
| 5918 | * Is it a word: "=word"? |
| 5919 | */ |
| 5920 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 5921 | { |
| 5922 | ++look; |
| 5923 | if (*look == '~') |
| 5924 | { |
| 5925 | icase = TRUE; |
| 5926 | ++look; |
| 5927 | } |
| 5928 | else |
| 5929 | icase = FALSE; |
| 5930 | p = vim_strchr(look, ','); |
| 5931 | if (p == NULL) |
| 5932 | p = look + STRLEN(look); |
| 5933 | if ((try_match || try_match_word) |
| 5934 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 5935 | { |
| 5936 | int match = FALSE; |
| 5937 | |
| 5938 | #ifdef FEAT_INS_EXPAND |
| 5939 | if (keytyped == KEY_COMPLETE) |
| 5940 | { |
| 5941 | char_u *s; |
| 5942 | |
| 5943 | /* Just completed a word, check if it starts with "look". |
| 5944 | * search back for the start of a word. */ |
| 5945 | line = ml_get_curline(); |
| 5946 | # ifdef FEAT_MBYTE |
| 5947 | if (has_mbyte) |
| 5948 | { |
| 5949 | char_u *n; |
| 5950 | |
| 5951 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 5952 | { |
| 5953 | n = mb_prevptr(line, s); |
| 5954 | if (!vim_iswordp(n)) |
| 5955 | break; |
| 5956 | } |
| 5957 | } |
| 5958 | else |
| 5959 | # endif |
| 5960 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 5961 | if (!vim_iswordc(s[-1])) |
| 5962 | break; |
| 5963 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 5964 | && (icase |
| 5965 | ? MB_STRNICMP(s, look, p - look) |
| 5966 | : STRNCMP(s, look, p - look)) == 0) |
| 5967 | match = TRUE; |
| 5968 | } |
| 5969 | else |
| 5970 | #endif |
| 5971 | /* TODO: multi-byte */ |
| 5972 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 5973 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 5974 | { |
| 5975 | line = ml_get_cursor(); |
| 5976 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 5977 | || !vim_iswordc(line[-(p - look) - 1])) |
| 5978 | && (icase |
| 5979 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 5980 | : STRNCMP(line - (p - look), look, p - look)) |
| 5981 | == 0) |
| 5982 | match = TRUE; |
| 5983 | } |
| 5984 | if (match && try_match_word && !try_match) |
| 5985 | { |
| 5986 | /* "0=word": Check if there are only blanks before the |
| 5987 | * word. */ |
| 5988 | line = ml_get_curline(); |
| 5989 | if ((int)(skipwhite(line) - line) != |
| 5990 | (int)(curwin->w_cursor.col - (p - look))) |
| 5991 | match = FALSE; |
| 5992 | } |
| 5993 | if (match) |
| 5994 | return TRUE; |
| 5995 | } |
| 5996 | look = p; |
| 5997 | } |
| 5998 | |
| 5999 | /* |
| 6000 | * ok, it's a boring generic character. |
| 6001 | */ |
| 6002 | else |
| 6003 | { |
| 6004 | if (try_match && *look == keytyped) |
| 6005 | return TRUE; |
| 6006 | ++look; |
| 6007 | } |
| 6008 | |
| 6009 | /* |
| 6010 | * Skip over ", ". |
| 6011 | */ |
| 6012 | look = skip_to_option_part(look); |
| 6013 | } |
| 6014 | return FALSE; |
| 6015 | } |
| 6016 | #endif /* FEAT_CINDENT */ |
| 6017 | |
| 6018 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 6019 | /* |
| 6020 | * Map Hebrew keyboard when in hkmap mode. |
| 6021 | */ |
| 6022 | int |
| 6023 | hkmap(c) |
| 6024 | int c; |
| 6025 | { |
| 6026 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 6027 | { |
| 6028 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 6029 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 6030 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 6031 | static char_u map[26] = |
| 6032 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 6033 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 6034 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 6035 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 6036 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 6037 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 6038 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 6039 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 6040 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 6041 | |
| 6042 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 6043 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 6044 | /* '-1'='sofit' */ |
| 6045 | else if (c == 'x') |
| 6046 | return 'X'; |
| 6047 | else if (c == 'q') |
| 6048 | return '\''; /* {geresh}={'} */ |
| 6049 | else if (c == 246) |
| 6050 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 6051 | else if (c == 228) |
| 6052 | return ' '; /* \"a --> ' ' -- / -- */ |
| 6053 | else if (c == 252) |
| 6054 | return ' '; /* \"u --> ' ' -- / -- */ |
| 6055 | #ifdef EBCDIC |
| 6056 | else if (islower(c)) |
| 6057 | #else |
| 6058 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 6059 | * do this the same was as 5.7 and previous, so it works correctly on |
| 6060 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 6061 | * munged and won't work if e.g. searching for Hebrew text. |
| 6062 | */ |
| 6063 | else if (c >= 'a' && c <= 'z') |
| 6064 | #endif |
| 6065 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 6066 | else |
| 6067 | return c; |
| 6068 | } |
| 6069 | else |
| 6070 | { |
| 6071 | switch (c) |
| 6072 | { |
| 6073 | case '`': return ';'; |
| 6074 | case '/': return '.'; |
| 6075 | case '\'': return ','; |
| 6076 | case 'q': return '/'; |
| 6077 | case 'w': return '\''; |
| 6078 | |
| 6079 | /* Hebrew letters - set offset from 'a' */ |
| 6080 | case ',': c = '{'; break; |
| 6081 | case '.': c = 'v'; break; |
| 6082 | case ';': c = 't'; break; |
| 6083 | default: { |
| 6084 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 6085 | |
| 6086 | #ifdef EBCDIC |
| 6087 | /* see note about islower() above */ |
| 6088 | if (!islower(c)) |
| 6089 | #else |
| 6090 | if (c < 'a' || c > 'z') |
| 6091 | #endif |
| 6092 | return c; |
| 6093 | c = str[CharOrdLow(c)]; |
| 6094 | break; |
| 6095 | } |
| 6096 | } |
| 6097 | |
| 6098 | return (int)(CharOrdLow(c) + p_aleph); |
| 6099 | } |
| 6100 | } |
| 6101 | #endif |
| 6102 | |
| 6103 | static void |
| 6104 | ins_reg() |
| 6105 | { |
| 6106 | int need_redraw = FALSE; |
| 6107 | int regname; |
| 6108 | int literally = 0; |
| 6109 | |
| 6110 | /* |
| 6111 | * If we are going to wait for a character, show a '"'. |
| 6112 | */ |
| 6113 | pc_status = PC_STATUS_UNSET; |
| 6114 | if (redrawing() && !char_avail()) |
| 6115 | { |
| 6116 | /* may need to redraw when no more chars available now */ |
| 6117 | ins_redraw(); |
| 6118 | |
| 6119 | edit_putchar('"', TRUE); |
| 6120 | #ifdef FEAT_CMDL_INFO |
| 6121 | add_to_showcmd_c(Ctrl_R); |
| 6122 | #endif |
| 6123 | } |
| 6124 | |
| 6125 | #ifdef USE_ON_FLY_SCROLL |
| 6126 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 6127 | #endif |
| 6128 | |
| 6129 | /* |
| 6130 | * Don't map the register name. This also prevents the mode message to be |
| 6131 | * deleted when ESC is hit. |
| 6132 | */ |
| 6133 | ++no_mapping; |
| 6134 | regname = safe_vgetc(); |
| 6135 | #ifdef FEAT_LANGMAP |
| 6136 | LANGMAP_ADJUST(regname, TRUE); |
| 6137 | #endif |
| 6138 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 6139 | { |
| 6140 | /* Get a third key for literal register insertion */ |
| 6141 | literally = regname; |
| 6142 | #ifdef FEAT_CMDL_INFO |
| 6143 | add_to_showcmd_c(literally); |
| 6144 | #endif |
| 6145 | regname = safe_vgetc(); |
| 6146 | #ifdef FEAT_LANGMAP |
| 6147 | LANGMAP_ADJUST(regname, TRUE); |
| 6148 | #endif |
| 6149 | } |
| 6150 | --no_mapping; |
| 6151 | |
| 6152 | #ifdef FEAT_EVAL |
| 6153 | /* |
| 6154 | * Don't call u_sync() while getting the expression, |
| 6155 | * evaluating it or giving an error message for it! |
| 6156 | */ |
| 6157 | ++no_u_sync; |
| 6158 | if (regname == '=') |
| 6159 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6160 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6161 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6162 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6163 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6164 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6165 | /* Restore the Input Method. */ |
| 6166 | if (im_on) |
| 6167 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6168 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6169 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 6170 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 6171 | { |
| 6172 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6173 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 6174 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6175 | else |
| 6176 | { |
| 6177 | #endif |
| 6178 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 6179 | { |
| 6180 | /* Append the command to the redo buffer. */ |
| 6181 | AppendCharToRedobuff(Ctrl_R); |
| 6182 | AppendCharToRedobuff(literally); |
| 6183 | AppendCharToRedobuff(regname); |
| 6184 | |
| 6185 | do_put(regname, BACKWARD, 1L, |
| 6186 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 6187 | } |
| 6188 | else if (insert_reg(regname, literally) == FAIL) |
| 6189 | { |
| 6190 | vim_beep(); |
| 6191 | need_redraw = TRUE; /* remove the '"' */ |
| 6192 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6193 | else if (stop_insert_mode) |
| 6194 | /* When the '=' register was used and a function was invoked that |
| 6195 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 6196 | * insert anything, need to remove the '"' */ |
| 6197 | need_redraw = TRUE; |
| 6198 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6199 | #ifdef FEAT_EVAL |
| 6200 | } |
| 6201 | --no_u_sync; |
| 6202 | #endif |
| 6203 | #ifdef FEAT_CMDL_INFO |
| 6204 | clear_showcmd(); |
| 6205 | #endif |
| 6206 | |
| 6207 | /* If the inserted register is empty, we need to remove the '"' */ |
| 6208 | if (need_redraw || stuff_empty()) |
| 6209 | edit_unputchar(); |
| 6210 | } |
| 6211 | |
| 6212 | /* |
| 6213 | * CTRL-G commands in Insert mode. |
| 6214 | */ |
| 6215 | static void |
| 6216 | ins_ctrl_g() |
| 6217 | { |
| 6218 | int c; |
| 6219 | |
| 6220 | #ifdef FEAT_INS_EXPAND |
| 6221 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 6222 | setcursor(); |
| 6223 | #endif |
| 6224 | |
| 6225 | /* |
| 6226 | * Don't map the second key. This also prevents the mode message to be |
| 6227 | * deleted when ESC is hit. |
| 6228 | */ |
| 6229 | ++no_mapping; |
| 6230 | c = safe_vgetc(); |
| 6231 | --no_mapping; |
| 6232 | switch (c) |
| 6233 | { |
| 6234 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 6235 | case K_UP: |
| 6236 | case Ctrl_K: |
| 6237 | case 'k': ins_up(TRUE); |
| 6238 | break; |
| 6239 | |
| 6240 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 6241 | case K_DOWN: |
| 6242 | case Ctrl_J: |
| 6243 | case 'j': ins_down(TRUE); |
| 6244 | break; |
| 6245 | |
| 6246 | /* CTRL-G u: start new undoable edit */ |
| 6247 | case 'u': u_sync(); |
| 6248 | ins_need_undo = TRUE; |
| 6249 | break; |
| 6250 | |
| 6251 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 6252 | default: vim_beep(); |
| 6253 | } |
| 6254 | } |
| 6255 | |
| 6256 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 6257 | * CTRL-^ in Insert mode. |
| 6258 | */ |
| 6259 | static void |
| 6260 | ins_ctrl_hat() |
| 6261 | { |
| 6262 | if (map_to_exists_mode((char_u *)"", LANGMAP)) |
| 6263 | { |
| 6264 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 6265 | if (State & LANGMAP) |
| 6266 | { |
| 6267 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 6268 | State &= ~LANGMAP; |
| 6269 | } |
| 6270 | else |
| 6271 | { |
| 6272 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 6273 | State |= LANGMAP; |
| 6274 | #ifdef USE_IM_CONTROL |
| 6275 | im_set_active(FALSE); |
| 6276 | #endif |
| 6277 | } |
| 6278 | } |
| 6279 | #ifdef USE_IM_CONTROL |
| 6280 | else |
| 6281 | { |
| 6282 | /* There are no ":lmap" mappings, toggle IM */ |
| 6283 | if (im_get_status()) |
| 6284 | { |
| 6285 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 6286 | im_set_active(FALSE); |
| 6287 | } |
| 6288 | else |
| 6289 | { |
| 6290 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 6291 | State &= ~LANGMAP; |
| 6292 | im_set_active(TRUE); |
| 6293 | } |
| 6294 | } |
| 6295 | #endif |
| 6296 | set_iminsert_global(); |
| 6297 | showmode(); |
| 6298 | #ifdef FEAT_GUI |
| 6299 | /* may show different cursor shape or color */ |
| 6300 | if (gui.in_use) |
| 6301 | gui_update_cursor(TRUE, FALSE); |
| 6302 | #endif |
| 6303 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 6304 | /* Show/unshow value of 'keymap' in status lines. */ |
| 6305 | status_redraw_curbuf(); |
| 6306 | #endif |
| 6307 | } |
| 6308 | |
| 6309 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6310 | * Handle ESC in insert mode. |
| 6311 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 6312 | * insert. |
| 6313 | */ |
| 6314 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6315 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6316 | long *count; |
| 6317 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6318 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6319 | { |
| 6320 | int temp; |
| 6321 | static int disabled_redraw = FALSE; |
| 6322 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 6323 | #ifdef FEAT_SYN_HL |
| 6324 | check_spell_redraw(); |
| 6325 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6326 | #if defined(FEAT_HANGULIN) |
| 6327 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 6328 | hangul_input_state_set(0); |
| 6329 | # endif |
| 6330 | if (composing_hangul) |
| 6331 | { |
| 6332 | push_raw_key(composing_hangul_buffer, 2); |
| 6333 | composing_hangul = 0; |
| 6334 | } |
| 6335 | #endif |
| 6336 | #if defined(FEAT_MBYTE) && defined(MACOS_CLASSIC) |
| 6337 | previous_script = GetScriptManagerVariable(smKeyScript); |
| 6338 | KeyScript(smKeyRoman); /* or smKeySysScript */ |
| 6339 | #endif |
| 6340 | |
| 6341 | temp = curwin->w_cursor.col; |
| 6342 | if (disabled_redraw) |
| 6343 | { |
| 6344 | --RedrawingDisabled; |
| 6345 | disabled_redraw = FALSE; |
| 6346 | } |
| 6347 | if (!arrow_used) |
| 6348 | { |
| 6349 | /* |
| 6350 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 6351 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 6352 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6353 | */ |
| 6354 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 6355 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6356 | |
| 6357 | /* |
| 6358 | * Repeating insert may take a long time. Check for |
| 6359 | * interrupt now and then. |
| 6360 | */ |
| 6361 | if (*count > 0) |
| 6362 | { |
| 6363 | line_breakcheck(); |
| 6364 | if (got_int) |
| 6365 | *count = 0; |
| 6366 | } |
| 6367 | |
| 6368 | if (--*count > 0) /* repeat what was typed */ |
| 6369 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 6370 | /* Vi repeats the insert without replacing characters. */ |
| 6371 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 6372 | State &= ~REPLACE_FLAG; |
| 6373 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6374 | (void)start_redo_ins(); |
| 6375 | if (cmdchar == 'r' || cmdchar == 'v') |
| 6376 | stuffReadbuff(ESC_STR); /* no ESC in redo buffer */ |
| 6377 | ++RedrawingDisabled; |
| 6378 | disabled_redraw = TRUE; |
| 6379 | return FALSE; /* repeat the insert */ |
| 6380 | } |
| 6381 | stop_insert(&curwin->w_cursor, TRUE); |
| 6382 | undisplay_dollar(); |
| 6383 | } |
| 6384 | |
| 6385 | /* When an autoindent was removed, curswant stays after the |
| 6386 | * indent */ |
| 6387 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 6388 | curwin->w_set_curswant = TRUE; |
| 6389 | |
| 6390 | /* Remember the last Insert position in the '^ mark. */ |
| 6391 | if (!cmdmod.keepjumps) |
| 6392 | curbuf->b_last_insert = curwin->w_cursor; |
| 6393 | |
| 6394 | /* |
| 6395 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6396 | * Don't do it for CTRL-O, unless past the end of the line. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6397 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6398 | if (!nomove |
| 6399 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6400 | #ifdef FEAT_VIRTUALEDIT |
| 6401 | || curwin->w_cursor.coladd > 0 |
| 6402 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6403 | ) |
| 6404 | && (restart_edit == NUL |
| 6405 | || (gchar_cursor() == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6406 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6407 | && !VIsual_active |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6408 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 6409 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6410 | #ifdef FEAT_RIGHTLEFT |
| 6411 | && !revins_on |
| 6412 | #endif |
| 6413 | ) |
| 6414 | { |
| 6415 | #ifdef FEAT_VIRTUALEDIT |
| 6416 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 6417 | { |
| 6418 | oneleft(); |
| 6419 | if (restart_edit != NUL) |
| 6420 | ++curwin->w_cursor.coladd; |
| 6421 | } |
| 6422 | else |
| 6423 | #endif |
| 6424 | { |
| 6425 | --curwin->w_cursor.col; |
| 6426 | #ifdef FEAT_MBYTE |
| 6427 | /* Correct cursor for multi-byte character. */ |
| 6428 | if (has_mbyte) |
| 6429 | mb_adjust_cursor(); |
| 6430 | #endif |
| 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | #ifdef USE_IM_CONTROL |
| 6435 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 6436 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 6437 | * well). */ |
| 6438 | if (!(State & LANGMAP)) |
| 6439 | im_save_status(&curbuf->b_p_iminsert); |
| 6440 | im_set_active(FALSE); |
| 6441 | #endif |
| 6442 | |
| 6443 | State = NORMAL; |
| 6444 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 6445 | changed_cline_bef_curs(); |
| 6446 | |
| 6447 | #ifdef FEAT_MOUSE |
| 6448 | setmouse(); |
| 6449 | #endif |
| 6450 | #ifdef CURSOR_SHAPE |
| 6451 | ui_cursor_shape(); /* may show different cursor shape */ |
| 6452 | #endif |
| 6453 | |
| 6454 | /* |
| 6455 | * When recording or for CTRL-O, need to display the new mode. |
| 6456 | * Otherwise remove the mode message. |
| 6457 | */ |
| 6458 | if (Recording || restart_edit != NUL) |
| 6459 | showmode(); |
| 6460 | else if (p_smd) |
| 6461 | MSG(""); |
| 6462 | |
| 6463 | return TRUE; /* exit Insert mode */ |
| 6464 | } |
| 6465 | |
| 6466 | #ifdef FEAT_RIGHTLEFT |
| 6467 | /* |
| 6468 | * Toggle language: hkmap and revins_on. |
| 6469 | * Move to end of reverse inserted text. |
| 6470 | */ |
| 6471 | static void |
| 6472 | ins_ctrl_() |
| 6473 | { |
| 6474 | if (revins_on && revins_chars && revins_scol >= 0) |
| 6475 | { |
| 6476 | while (gchar_cursor() != NUL && revins_chars--) |
| 6477 | ++curwin->w_cursor.col; |
| 6478 | } |
| 6479 | p_ri = !p_ri; |
| 6480 | revins_on = (State == INSERT && p_ri); |
| 6481 | if (revins_on) |
| 6482 | { |
| 6483 | revins_scol = curwin->w_cursor.col; |
| 6484 | revins_legal++; |
| 6485 | revins_chars = 0; |
| 6486 | undisplay_dollar(); |
| 6487 | } |
| 6488 | else |
| 6489 | revins_scol = -1; |
| 6490 | #ifdef FEAT_FKMAP |
| 6491 | if (p_altkeymap) |
| 6492 | { |
| 6493 | /* |
| 6494 | * to be consistent also for redo command, using '.' |
| 6495 | * set arrow_used to true and stop it - causing to redo |
| 6496 | * characters entered in one mode (normal/reverse insert). |
| 6497 | */ |
| 6498 | arrow_used = TRUE; |
| 6499 | (void)stop_arrow(); |
| 6500 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 6501 | if (p_fkmap && p_ri) |
| 6502 | State = INSERT; |
| 6503 | } |
| 6504 | else |
| 6505 | #endif |
| 6506 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 6507 | showmode(); |
| 6508 | } |
| 6509 | #endif |
| 6510 | |
| 6511 | #ifdef FEAT_VISUAL |
| 6512 | /* |
| 6513 | * If 'keymodel' contains "startsel", may start selection. |
| 6514 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 6515 | */ |
| 6516 | static int |
| 6517 | ins_start_select(c) |
| 6518 | int c; |
| 6519 | { |
| 6520 | if (km_startsel) |
| 6521 | switch (c) |
| 6522 | { |
| 6523 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6525 | case K_PAGEUP: |
| 6526 | case K_KPAGEUP: |
| 6527 | case K_PAGEDOWN: |
| 6528 | case K_KPAGEDOWN: |
| 6529 | # ifdef MACOS |
| 6530 | case K_LEFT: |
| 6531 | case K_RIGHT: |
| 6532 | case K_UP: |
| 6533 | case K_DOWN: |
| 6534 | case K_END: |
| 6535 | case K_HOME: |
| 6536 | # endif |
| 6537 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 6538 | break; |
| 6539 | /* FALLTHROUGH */ |
| 6540 | case K_S_LEFT: |
| 6541 | case K_S_RIGHT: |
| 6542 | case K_S_UP: |
| 6543 | case K_S_DOWN: |
| 6544 | case K_S_END: |
| 6545 | case K_S_HOME: |
| 6546 | /* Start selection right away, the cursor can move with |
| 6547 | * CTRL-O when beyond the end of the line. */ |
| 6548 | start_selection(); |
| 6549 | |
| 6550 | /* Execute the key in (insert) Select mode. */ |
| 6551 | stuffcharReadbuff(Ctrl_O); |
| 6552 | if (mod_mask) |
| 6553 | { |
| 6554 | char_u buf[4]; |
| 6555 | |
| 6556 | buf[0] = K_SPECIAL; |
| 6557 | buf[1] = KS_MODIFIER; |
| 6558 | buf[2] = mod_mask; |
| 6559 | buf[3] = NUL; |
| 6560 | stuffReadbuff(buf); |
| 6561 | } |
| 6562 | stuffcharReadbuff(c); |
| 6563 | return TRUE; |
| 6564 | } |
| 6565 | return FALSE; |
| 6566 | } |
| 6567 | #endif |
| 6568 | |
| 6569 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 6570 | * <Insert> key in Insert mode: toggle insert/remplace mode. |
| 6571 | */ |
| 6572 | static void |
| 6573 | ins_insert(replaceState) |
| 6574 | int replaceState; |
| 6575 | { |
| 6576 | #ifdef FEAT_FKMAP |
| 6577 | if (p_fkmap && p_ri) |
| 6578 | { |
| 6579 | beep_flush(); |
| 6580 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 6581 | return; |
| 6582 | } |
| 6583 | #endif |
| 6584 | |
| 6585 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6586 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 6587 | set_vim_var_string(VV_INSERTMODE, |
| 6588 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
| 6589 | replaceState == VREPLACE ? "v" : "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 6590 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 6591 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 6592 | #endif |
| 6593 | if (State & REPLACE_FLAG) |
| 6594 | State = INSERT | (State & LANGMAP); |
| 6595 | else |
| 6596 | State = replaceState | (State & LANGMAP); |
| 6597 | AppendCharToRedobuff(K_INS); |
| 6598 | showmode(); |
| 6599 | #ifdef CURSOR_SHAPE |
| 6600 | ui_cursor_shape(); /* may show different cursor shape */ |
| 6601 | #endif |
| 6602 | } |
| 6603 | |
| 6604 | /* |
| 6605 | * Pressed CTRL-O in Insert mode. |
| 6606 | */ |
| 6607 | static void |
| 6608 | ins_ctrl_o() |
| 6609 | { |
| 6610 | #ifdef FEAT_VREPLACE |
| 6611 | if (State & VREPLACE_FLAG) |
| 6612 | restart_edit = 'V'; |
| 6613 | else |
| 6614 | #endif |
| 6615 | if (State & REPLACE_FLAG) |
| 6616 | restart_edit = 'R'; |
| 6617 | else |
| 6618 | restart_edit = 'I'; |
| 6619 | #ifdef FEAT_VIRTUALEDIT |
| 6620 | if (virtual_active()) |
| 6621 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 6622 | else |
| 6623 | #endif |
| 6624 | ins_at_eol = (gchar_cursor() == NUL); |
| 6625 | } |
| 6626 | |
| 6627 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6628 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 6629 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
| 6630 | * Always round the indent to 'shiftwith', this is compatible |
| 6631 | * with vi. But vi only supports ^T and ^D after an |
| 6632 | * autoindent, we support it everywhere. |
| 6633 | */ |
| 6634 | static void |
| 6635 | ins_shift(c, lastc) |
| 6636 | int c; |
| 6637 | int lastc; |
| 6638 | { |
| 6639 | if (stop_arrow() == FAIL) |
| 6640 | return; |
| 6641 | AppendCharToRedobuff(c); |
| 6642 | |
| 6643 | /* |
| 6644 | * 0^D and ^^D: remove all indent. |
| 6645 | */ |
| 6646 | if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col) |
| 6647 | { |
| 6648 | --curwin->w_cursor.col; |
| 6649 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 6650 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 6651 | if (State & REPLACE_FLAG) |
| 6652 | replace_pop_ins(); |
| 6653 | if (lastc == '^') |
| 6654 | old_indent = get_indent(); /* remember curr. indent */ |
| 6655 | change_indent(INDENT_SET, 0, TRUE, 0); |
| 6656 | } |
| 6657 | else |
| 6658 | change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0); |
| 6659 | |
| 6660 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 6661 | did_ai = FALSE; |
| 6662 | #ifdef FEAT_SMARTINDENT |
| 6663 | did_si = FALSE; |
| 6664 | can_si = FALSE; |
| 6665 | can_si_back = FALSE; |
| 6666 | #endif |
| 6667 | #ifdef FEAT_CINDENT |
| 6668 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 6669 | #endif |
| 6670 | } |
| 6671 | |
| 6672 | static void |
| 6673 | ins_del() |
| 6674 | { |
| 6675 | int temp; |
| 6676 | |
| 6677 | if (stop_arrow() == FAIL) |
| 6678 | return; |
| 6679 | if (gchar_cursor() == NUL) /* delete newline */ |
| 6680 | { |
| 6681 | temp = curwin->w_cursor.col; |
| 6682 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
| 6683 | || u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 6684 | (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL |
| 6685 | || do_join(FALSE) == FAIL) |
| 6686 | vim_beep(); |
| 6687 | else |
| 6688 | curwin->w_cursor.col = temp; |
| 6689 | } |
| 6690 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 6691 | vim_beep(); |
| 6692 | did_ai = FALSE; |
| 6693 | #ifdef FEAT_SMARTINDENT |
| 6694 | did_si = FALSE; |
| 6695 | can_si = FALSE; |
| 6696 | can_si_back = FALSE; |
| 6697 | #endif |
| 6698 | AppendCharToRedobuff(K_DEL); |
| 6699 | } |
| 6700 | |
| 6701 | /* |
| 6702 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 6703 | * Return TRUE when backspace was actually used. |
| 6704 | */ |
| 6705 | static int |
| 6706 | ins_bs(c, mode, inserted_space_p) |
| 6707 | int c; |
| 6708 | int mode; |
| 6709 | int *inserted_space_p; |
| 6710 | { |
| 6711 | linenr_T lnum; |
| 6712 | int cc; |
| 6713 | int temp = 0; /* init for GCC */ |
| 6714 | colnr_T mincol; |
| 6715 | int did_backspace = FALSE; |
| 6716 | int in_indent; |
| 6717 | int oldState; |
| 6718 | #ifdef FEAT_MBYTE |
| 6719 | int p1, p2; |
| 6720 | #endif |
| 6721 | |
| 6722 | /* |
| 6723 | * can't delete anything in an empty file |
| 6724 | * can't backup past first character in buffer |
| 6725 | * can't backup past starting point unless 'backspace' > 1 |
| 6726 | * can backup to a previous line if 'backspace' == 0 |
| 6727 | */ |
| 6728 | if ( bufempty() |
| 6729 | || ( |
| 6730 | #ifdef FEAT_RIGHTLEFT |
| 6731 | !revins_on && |
| 6732 | #endif |
| 6733 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 6734 | || (!can_bs(BS_START) |
| 6735 | && (arrow_used |
| 6736 | || (curwin->w_cursor.lnum == Insstart.lnum |
| 6737 | && curwin->w_cursor.col <= Insstart.col))) |
| 6738 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 6739 | && curwin->w_cursor.col <= ai_col) |
| 6740 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 6741 | { |
| 6742 | vim_beep(); |
| 6743 | return FALSE; |
| 6744 | } |
| 6745 | |
| 6746 | if (stop_arrow() == FAIL) |
| 6747 | return FALSE; |
| 6748 | in_indent = inindent(0); |
| 6749 | #ifdef FEAT_CINDENT |
| 6750 | if (in_indent) |
| 6751 | can_cindent = FALSE; |
| 6752 | #endif |
| 6753 | #ifdef FEAT_COMMENTS |
| 6754 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 6755 | #endif |
| 6756 | #ifdef FEAT_RIGHTLEFT |
| 6757 | if (revins_on) /* put cursor after last inserted char */ |
| 6758 | inc_cursor(); |
| 6759 | #endif |
| 6760 | |
| 6761 | #ifdef FEAT_VIRTUALEDIT |
| 6762 | /* Virtualedit: |
| 6763 | * BACKSPACE_CHAR eats a virtual space |
| 6764 | * BACKSPACE_WORD eats all coladd |
| 6765 | * BACKSPACE_LINE eats all coladd and keeps going |
| 6766 | */ |
| 6767 | if (curwin->w_cursor.coladd > 0) |
| 6768 | { |
| 6769 | if (mode == BACKSPACE_CHAR) |
| 6770 | { |
| 6771 | --curwin->w_cursor.coladd; |
| 6772 | return TRUE; |
| 6773 | } |
| 6774 | if (mode == BACKSPACE_WORD) |
| 6775 | { |
| 6776 | curwin->w_cursor.coladd = 0; |
| 6777 | return TRUE; |
| 6778 | } |
| 6779 | curwin->w_cursor.coladd = 0; |
| 6780 | } |
| 6781 | #endif |
| 6782 | |
| 6783 | /* |
| 6784 | * delete newline! |
| 6785 | */ |
| 6786 | if (curwin->w_cursor.col == 0) |
| 6787 | { |
| 6788 | lnum = Insstart.lnum; |
| 6789 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 6790 | #ifdef FEAT_RIGHTLEFT |
| 6791 | || revins_on |
| 6792 | #endif |
| 6793 | ) |
| 6794 | { |
| 6795 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 6796 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 6797 | return FALSE; |
| 6798 | --Insstart.lnum; |
| 6799 | Insstart.col = MAXCOL; |
| 6800 | } |
| 6801 | /* |
| 6802 | * In replace mode: |
| 6803 | * cc < 0: NL was inserted, delete it |
| 6804 | * cc >= 0: NL was replaced, put original characters back |
| 6805 | */ |
| 6806 | cc = -1; |
| 6807 | if (State & REPLACE_FLAG) |
| 6808 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 6809 | /* |
| 6810 | * In replace mode, in the line we started replacing, we only move the |
| 6811 | * cursor. |
| 6812 | */ |
| 6813 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 6814 | { |
| 6815 | dec_cursor(); |
| 6816 | } |
| 6817 | else |
| 6818 | { |
| 6819 | #ifdef FEAT_VREPLACE |
| 6820 | if (!(State & VREPLACE_FLAG) |
| 6821 | || curwin->w_cursor.lnum > orig_line_count) |
| 6822 | #endif |
| 6823 | { |
| 6824 | temp = gchar_cursor(); /* remember current char */ |
| 6825 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 6826 | |
| 6827 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 6828 | * the end of the line, otherwise the line will be broken |
| 6829 | * again when auto-formatting. */ |
| 6830 | if (has_format_option(FO_AUTO) |
| 6831 | && has_format_option(FO_WHITE_PAR)) |
| 6832 | { |
| 6833 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 6834 | TRUE); |
| 6835 | int len; |
| 6836 | |
| 6837 | len = STRLEN(ptr); |
| 6838 | if (len > 0 && ptr[len - 1] == ' ') |
| 6839 | ptr[len - 1] = NUL; |
| 6840 | } |
| 6841 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6842 | (void)do_join(FALSE); |
| 6843 | if (temp == NUL && gchar_cursor() != NUL) |
| 6844 | inc_cursor(); |
| 6845 | } |
| 6846 | #ifdef FEAT_VREPLACE |
| 6847 | else |
| 6848 | dec_cursor(); |
| 6849 | #endif |
| 6850 | |
| 6851 | /* |
| 6852 | * In REPLACE mode we have to put back the text that was replaced |
| 6853 | * by the NL. On the replace stack is first a NUL-terminated |
| 6854 | * sequence of characters that were deleted and then the |
| 6855 | * characters that NL replaced. |
| 6856 | */ |
| 6857 | if (State & REPLACE_FLAG) |
| 6858 | { |
| 6859 | /* |
| 6860 | * Do the next ins_char() in NORMAL state, to |
| 6861 | * prevent ins_char() from replacing characters and |
| 6862 | * avoiding showmatch(). |
| 6863 | */ |
| 6864 | oldState = State; |
| 6865 | State = NORMAL; |
| 6866 | /* |
| 6867 | * restore characters (blanks) deleted after cursor |
| 6868 | */ |
| 6869 | while (cc > 0) |
| 6870 | { |
| 6871 | temp = curwin->w_cursor.col; |
| 6872 | #ifdef FEAT_MBYTE |
| 6873 | mb_replace_pop_ins(cc); |
| 6874 | #else |
| 6875 | ins_char(cc); |
| 6876 | #endif |
| 6877 | curwin->w_cursor.col = temp; |
| 6878 | cc = replace_pop(); |
| 6879 | } |
| 6880 | /* restore the characters that NL replaced */ |
| 6881 | replace_pop_ins(); |
| 6882 | State = oldState; |
| 6883 | } |
| 6884 | } |
| 6885 | did_ai = FALSE; |
| 6886 | } |
| 6887 | else |
| 6888 | { |
| 6889 | /* |
| 6890 | * Delete character(s) before the cursor. |
| 6891 | */ |
| 6892 | #ifdef FEAT_RIGHTLEFT |
| 6893 | if (revins_on) /* put cursor on last inserted char */ |
| 6894 | dec_cursor(); |
| 6895 | #endif |
| 6896 | mincol = 0; |
| 6897 | /* keep indent */ |
| 6898 | if (mode == BACKSPACE_LINE && curbuf->b_p_ai |
| 6899 | #ifdef FEAT_RIGHTLEFT |
| 6900 | && !revins_on |
| 6901 | #endif |
| 6902 | ) |
| 6903 | { |
| 6904 | temp = curwin->w_cursor.col; |
| 6905 | beginline(BL_WHITE); |
| 6906 | if (curwin->w_cursor.col < (colnr_T)temp) |
| 6907 | mincol = curwin->w_cursor.col; |
| 6908 | curwin->w_cursor.col = temp; |
| 6909 | } |
| 6910 | |
| 6911 | /* |
| 6912 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 6913 | */ |
| 6914 | if ( mode == BACKSPACE_CHAR |
| 6915 | && ((p_sta && in_indent) |
| 6916 | || (curbuf->b_p_sts |
| 6917 | && (*(ml_get_cursor() - 1) == TAB |
| 6918 | || (*(ml_get_cursor() - 1) == ' ' |
| 6919 | && (!*inserted_space_p |
| 6920 | || arrow_used)))))) |
| 6921 | { |
| 6922 | int ts; |
| 6923 | colnr_T vcol; |
| 6924 | colnr_T want_vcol; |
| 6925 | int extra = 0; |
| 6926 | |
| 6927 | *inserted_space_p = FALSE; |
| 6928 | if (p_sta) |
| 6929 | ts = curbuf->b_p_sw; |
| 6930 | else |
| 6931 | ts = curbuf->b_p_sts; |
| 6932 | /* Compute the virtual column where we want to be. Since |
| 6933 | * 'showbreak' may get in the way, need to get the last column of |
| 6934 | * the previous character. */ |
| 6935 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6936 | dec_cursor(); |
| 6937 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 6938 | inc_cursor(); |
| 6939 | want_vcol = (want_vcol / ts) * ts; |
| 6940 | |
| 6941 | /* delete characters until we are at or before want_vcol */ |
| 6942 | while (vcol > want_vcol |
| 6943 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
| 6944 | { |
| 6945 | dec_cursor(); |
| 6946 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6947 | if (State & REPLACE_FLAG) |
| 6948 | { |
| 6949 | /* Don't delete characters before the insert point when in |
| 6950 | * Replace mode */ |
| 6951 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 6952 | || curwin->w_cursor.col >= Insstart.col) |
| 6953 | { |
| 6954 | #if 0 /* what was this for? It causes problems when sw != ts. */ |
| 6955 | if (State == REPLACE && (int)vcol < want_vcol) |
| 6956 | { |
| 6957 | (void)del_char(FALSE); |
| 6958 | extra = 2; /* don't pop too much */ |
| 6959 | } |
| 6960 | else |
| 6961 | #endif |
| 6962 | replace_do_bs(); |
| 6963 | } |
| 6964 | } |
| 6965 | else |
| 6966 | (void)del_char(FALSE); |
| 6967 | } |
| 6968 | |
| 6969 | /* insert extra spaces until we are at want_vcol */ |
| 6970 | while (vcol < want_vcol) |
| 6971 | { |
| 6972 | /* Remember the first char we inserted */ |
| 6973 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 6974 | && curwin->w_cursor.col < Insstart.col) |
| 6975 | Insstart.col = curwin->w_cursor.col; |
| 6976 | |
| 6977 | #ifdef FEAT_VREPLACE |
| 6978 | if (State & VREPLACE_FLAG) |
| 6979 | ins_char(' '); |
| 6980 | else |
| 6981 | #endif |
| 6982 | { |
| 6983 | ins_str((char_u *)" "); |
| 6984 | if ((State & REPLACE_FLAG) && extra <= 1) |
| 6985 | { |
| 6986 | if (extra) |
| 6987 | replace_push_off(NUL); |
| 6988 | else |
| 6989 | replace_push(NUL); |
| 6990 | } |
| 6991 | if (extra == 2) |
| 6992 | extra = 1; |
| 6993 | } |
| 6994 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 6995 | } |
| 6996 | } |
| 6997 | |
| 6998 | /* |
| 6999 | * Delete upto starting point, start of line or previous word. |
| 7000 | */ |
| 7001 | else do |
| 7002 | { |
| 7003 | #ifdef FEAT_RIGHTLEFT |
| 7004 | if (!revins_on) /* put cursor on char to be deleted */ |
| 7005 | #endif |
| 7006 | dec_cursor(); |
| 7007 | |
| 7008 | /* start of word? */ |
| 7009 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 7010 | { |
| 7011 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 7012 | temp = vim_iswordc(gchar_cursor()); |
| 7013 | } |
| 7014 | /* end of word? */ |
| 7015 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 7016 | && (vim_isspace(cc = gchar_cursor()) |
| 7017 | || vim_iswordc(cc) != temp)) |
| 7018 | { |
| 7019 | #ifdef FEAT_RIGHTLEFT |
| 7020 | if (!revins_on) |
| 7021 | #endif |
| 7022 | inc_cursor(); |
| 7023 | #ifdef FEAT_RIGHTLEFT |
| 7024 | else if (State & REPLACE_FLAG) |
| 7025 | dec_cursor(); |
| 7026 | #endif |
| 7027 | break; |
| 7028 | } |
| 7029 | if (State & REPLACE_FLAG) |
| 7030 | replace_do_bs(); |
| 7031 | else |
| 7032 | { |
| 7033 | #ifdef FEAT_MBYTE |
| 7034 | if (enc_utf8 && p_deco) |
| 7035 | (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2); |
| 7036 | #endif |
| 7037 | (void)del_char(FALSE); |
| 7038 | #ifdef FEAT_MBYTE |
| 7039 | /* |
| 7040 | * If p1 or p2 is non-zero, there are combining characters we |
| 7041 | * need to take account of. Don't back up before the base |
| 7042 | * character. |
| 7043 | */ |
| 7044 | if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL)) |
| 7045 | inc_cursor(); |
| 7046 | #endif |
| 7047 | #ifdef FEAT_RIGHTLEFT |
| 7048 | if (revins_chars) |
| 7049 | { |
| 7050 | revins_chars--; |
| 7051 | revins_legal++; |
| 7052 | } |
| 7053 | if (revins_on && gchar_cursor() == NUL) |
| 7054 | break; |
| 7055 | #endif |
| 7056 | } |
| 7057 | /* Just a single backspace?: */ |
| 7058 | if (mode == BACKSPACE_CHAR) |
| 7059 | break; |
| 7060 | } while ( |
| 7061 | #ifdef FEAT_RIGHTLEFT |
| 7062 | revins_on || |
| 7063 | #endif |
| 7064 | (curwin->w_cursor.col > mincol |
| 7065 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 7066 | || curwin->w_cursor.col != Insstart.col))); |
| 7067 | did_backspace = TRUE; |
| 7068 | } |
| 7069 | #ifdef FEAT_SMARTINDENT |
| 7070 | did_si = FALSE; |
| 7071 | can_si = FALSE; |
| 7072 | can_si_back = FALSE; |
| 7073 | #endif |
| 7074 | if (curwin->w_cursor.col <= 1) |
| 7075 | did_ai = FALSE; |
| 7076 | /* |
| 7077 | * It's a little strange to put backspaces into the redo |
| 7078 | * buffer, but it makes auto-indent a lot easier to deal |
| 7079 | * with. |
| 7080 | */ |
| 7081 | AppendCharToRedobuff(c); |
| 7082 | |
| 7083 | /* If deleted before the insertion point, adjust it */ |
| 7084 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7085 | && curwin->w_cursor.col < Insstart.col) |
| 7086 | Insstart.col = curwin->w_cursor.col; |
| 7087 | |
| 7088 | /* vi behaviour: the cursor moves backward but the character that |
| 7089 | * was there remains visible |
| 7090 | * Vim behaviour: the cursor moves backward and the character that |
| 7091 | * was there is erased from the screen. |
| 7092 | * We can emulate the vi behaviour by pretending there is a dollar |
| 7093 | * displayed even when there isn't. |
| 7094 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
| 7095 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0) |
| 7096 | dollar_vcol = curwin->w_virtcol; |
| 7097 | |
| 7098 | return did_backspace; |
| 7099 | } |
| 7100 | |
| 7101 | #ifdef FEAT_MOUSE |
| 7102 | static void |
| 7103 | ins_mouse(c) |
| 7104 | int c; |
| 7105 | { |
| 7106 | pos_T tpos; |
| 7107 | |
| 7108 | # ifdef FEAT_GUI |
| 7109 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 7110 | if (!gui.in_use) |
| 7111 | # endif |
| 7112 | if (!mouse_has(MOUSE_INSERT)) |
| 7113 | return; |
| 7114 | |
| 7115 | undisplay_dollar(); |
| 7116 | tpos = curwin->w_cursor; |
| 7117 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 7118 | { |
| 7119 | start_arrow(&tpos); |
| 7120 | # ifdef FEAT_CINDENT |
| 7121 | can_cindent = TRUE; |
| 7122 | # endif |
| 7123 | } |
| 7124 | |
| 7125 | #ifdef FEAT_WINDOWS |
| 7126 | /* redraw status lines (in case another window became active) */ |
| 7127 | redraw_statuslines(); |
| 7128 | #endif |
| 7129 | } |
| 7130 | |
| 7131 | static void |
| 7132 | ins_mousescroll(up) |
| 7133 | int up; |
| 7134 | { |
| 7135 | pos_T tpos; |
| 7136 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7137 | win_T *old_curwin; |
| 7138 | # endif |
| 7139 | |
| 7140 | tpos = curwin->w_cursor; |
| 7141 | |
| 7142 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7143 | old_curwin = curwin; |
| 7144 | |
| 7145 | /* Currently the mouse coordinates are only known in the GUI. */ |
| 7146 | if (gui.in_use && mouse_row >= 0 && mouse_col >= 0) |
| 7147 | { |
| 7148 | int row, col; |
| 7149 | |
| 7150 | row = mouse_row; |
| 7151 | col = mouse_col; |
| 7152 | |
| 7153 | /* find the window at the pointer coordinates */ |
| 7154 | curwin = mouse_find_win(&row, &col); |
| 7155 | curbuf = curwin->w_buffer; |
| 7156 | } |
| 7157 | if (curwin == old_curwin) |
| 7158 | # endif |
| 7159 | undisplay_dollar(); |
| 7160 | |
| 7161 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 7162 | scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline)); |
| 7163 | else |
| 7164 | scroll_redraw(up, 3L); |
| 7165 | |
| 7166 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7167 | curwin->w_redr_status = TRUE; |
| 7168 | |
| 7169 | curwin = old_curwin; |
| 7170 | curbuf = curwin->w_buffer; |
| 7171 | # endif |
| 7172 | |
| 7173 | if (!equalpos(curwin->w_cursor, tpos)) |
| 7174 | { |
| 7175 | start_arrow(&tpos); |
| 7176 | # ifdef FEAT_CINDENT |
| 7177 | can_cindent = TRUE; |
| 7178 | # endif |
| 7179 | } |
| 7180 | } |
| 7181 | #endif |
| 7182 | |
| 7183 | #ifdef FEAT_GUI |
| 7184 | void |
| 7185 | ins_scroll() |
| 7186 | { |
| 7187 | pos_T tpos; |
| 7188 | |
| 7189 | undisplay_dollar(); |
| 7190 | tpos = curwin->w_cursor; |
| 7191 | if (gui_do_scroll()) |
| 7192 | { |
| 7193 | start_arrow(&tpos); |
| 7194 | # ifdef FEAT_CINDENT |
| 7195 | can_cindent = TRUE; |
| 7196 | # endif |
| 7197 | } |
| 7198 | } |
| 7199 | |
| 7200 | void |
| 7201 | ins_horscroll() |
| 7202 | { |
| 7203 | pos_T tpos; |
| 7204 | |
| 7205 | undisplay_dollar(); |
| 7206 | tpos = curwin->w_cursor; |
| 7207 | if (gui_do_horiz_scroll()) |
| 7208 | { |
| 7209 | start_arrow(&tpos); |
| 7210 | # ifdef FEAT_CINDENT |
| 7211 | can_cindent = TRUE; |
| 7212 | # endif |
| 7213 | } |
| 7214 | } |
| 7215 | #endif |
| 7216 | |
| 7217 | static void |
| 7218 | ins_left() |
| 7219 | { |
| 7220 | pos_T tpos; |
| 7221 | |
| 7222 | #ifdef FEAT_FOLDING |
| 7223 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7224 | foldOpenCursor(); |
| 7225 | #endif |
| 7226 | undisplay_dollar(); |
| 7227 | tpos = curwin->w_cursor; |
| 7228 | if (oneleft() == OK) |
| 7229 | { |
| 7230 | start_arrow(&tpos); |
| 7231 | #ifdef FEAT_RIGHTLEFT |
| 7232 | /* If exit reversed string, position is fixed */ |
| 7233 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 7234 | revins_legal++; |
| 7235 | revins_chars++; |
| 7236 | #endif |
| 7237 | } |
| 7238 | |
| 7239 | /* |
| 7240 | * if 'whichwrap' set for cursor in insert mode may go to |
| 7241 | * previous line |
| 7242 | */ |
| 7243 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 7244 | { |
| 7245 | start_arrow(&tpos); |
| 7246 | --(curwin->w_cursor.lnum); |
| 7247 | coladvance((colnr_T)MAXCOL); |
| 7248 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 7249 | } |
| 7250 | else |
| 7251 | vim_beep(); |
| 7252 | } |
| 7253 | |
| 7254 | static void |
| 7255 | ins_home(c) |
| 7256 | int c; |
| 7257 | { |
| 7258 | pos_T tpos; |
| 7259 | |
| 7260 | #ifdef FEAT_FOLDING |
| 7261 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7262 | foldOpenCursor(); |
| 7263 | #endif |
| 7264 | undisplay_dollar(); |
| 7265 | tpos = curwin->w_cursor; |
| 7266 | if (c == K_C_HOME) |
| 7267 | curwin->w_cursor.lnum = 1; |
| 7268 | curwin->w_cursor.col = 0; |
| 7269 | #ifdef FEAT_VIRTUALEDIT |
| 7270 | curwin->w_cursor.coladd = 0; |
| 7271 | #endif |
| 7272 | curwin->w_curswant = 0; |
| 7273 | start_arrow(&tpos); |
| 7274 | } |
| 7275 | |
| 7276 | static void |
| 7277 | ins_end(c) |
| 7278 | int c; |
| 7279 | { |
| 7280 | pos_T tpos; |
| 7281 | |
| 7282 | #ifdef FEAT_FOLDING |
| 7283 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7284 | foldOpenCursor(); |
| 7285 | #endif |
| 7286 | undisplay_dollar(); |
| 7287 | tpos = curwin->w_cursor; |
| 7288 | if (c == K_C_END) |
| 7289 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7290 | coladvance((colnr_T)MAXCOL); |
| 7291 | curwin->w_curswant = MAXCOL; |
| 7292 | |
| 7293 | start_arrow(&tpos); |
| 7294 | } |
| 7295 | |
| 7296 | static void |
| 7297 | ins_s_left() |
| 7298 | { |
| 7299 | #ifdef FEAT_FOLDING |
| 7300 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7301 | foldOpenCursor(); |
| 7302 | #endif |
| 7303 | undisplay_dollar(); |
| 7304 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 7305 | { |
| 7306 | start_arrow(&curwin->w_cursor); |
| 7307 | (void)bck_word(1L, FALSE, FALSE); |
| 7308 | curwin->w_set_curswant = TRUE; |
| 7309 | } |
| 7310 | else |
| 7311 | vim_beep(); |
| 7312 | } |
| 7313 | |
| 7314 | static void |
| 7315 | ins_right() |
| 7316 | { |
| 7317 | #ifdef FEAT_FOLDING |
| 7318 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7319 | foldOpenCursor(); |
| 7320 | #endif |
| 7321 | undisplay_dollar(); |
| 7322 | if (gchar_cursor() != NUL || virtual_active() |
| 7323 | ) |
| 7324 | { |
| 7325 | start_arrow(&curwin->w_cursor); |
| 7326 | curwin->w_set_curswant = TRUE; |
| 7327 | #ifdef FEAT_VIRTUALEDIT |
| 7328 | if (virtual_active()) |
| 7329 | oneright(); |
| 7330 | else |
| 7331 | #endif |
| 7332 | { |
| 7333 | #ifdef FEAT_MBYTE |
| 7334 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7335 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7336 | else |
| 7337 | #endif |
| 7338 | ++curwin->w_cursor.col; |
| 7339 | } |
| 7340 | |
| 7341 | #ifdef FEAT_RIGHTLEFT |
| 7342 | revins_legal++; |
| 7343 | if (revins_chars) |
| 7344 | revins_chars--; |
| 7345 | #endif |
| 7346 | } |
| 7347 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 7348 | * cursor to the next line */ |
| 7349 | else if (vim_strchr(p_ww, ']') != NULL |
| 7350 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 7351 | { |
| 7352 | start_arrow(&curwin->w_cursor); |
| 7353 | curwin->w_set_curswant = TRUE; |
| 7354 | ++curwin->w_cursor.lnum; |
| 7355 | curwin->w_cursor.col = 0; |
| 7356 | } |
| 7357 | else |
| 7358 | vim_beep(); |
| 7359 | } |
| 7360 | |
| 7361 | static void |
| 7362 | ins_s_right() |
| 7363 | { |
| 7364 | #ifdef FEAT_FOLDING |
| 7365 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7366 | foldOpenCursor(); |
| 7367 | #endif |
| 7368 | undisplay_dollar(); |
| 7369 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 7370 | || gchar_cursor() != NUL) |
| 7371 | { |
| 7372 | start_arrow(&curwin->w_cursor); |
| 7373 | (void)fwd_word(1L, FALSE, 0); |
| 7374 | curwin->w_set_curswant = TRUE; |
| 7375 | } |
| 7376 | else |
| 7377 | vim_beep(); |
| 7378 | } |
| 7379 | |
| 7380 | static void |
| 7381 | ins_up(startcol) |
| 7382 | int startcol; /* when TRUE move to Insstart.col */ |
| 7383 | { |
| 7384 | pos_T tpos; |
| 7385 | linenr_T old_topline = curwin->w_topline; |
| 7386 | #ifdef FEAT_DIFF |
| 7387 | int old_topfill = curwin->w_topfill; |
| 7388 | #endif |
| 7389 | |
| 7390 | undisplay_dollar(); |
| 7391 | tpos = curwin->w_cursor; |
| 7392 | if (cursor_up(1L, TRUE) == OK) |
| 7393 | { |
| 7394 | if (startcol) |
| 7395 | coladvance(getvcol_nolist(&Insstart)); |
| 7396 | if (old_topline != curwin->w_topline |
| 7397 | #ifdef FEAT_DIFF |
| 7398 | || old_topfill != curwin->w_topfill |
| 7399 | #endif |
| 7400 | ) |
| 7401 | redraw_later(VALID); |
| 7402 | start_arrow(&tpos); |
| 7403 | #ifdef FEAT_CINDENT |
| 7404 | can_cindent = TRUE; |
| 7405 | #endif |
| 7406 | } |
| 7407 | else |
| 7408 | vim_beep(); |
| 7409 | } |
| 7410 | |
| 7411 | static void |
| 7412 | ins_pageup() |
| 7413 | { |
| 7414 | pos_T tpos; |
| 7415 | |
| 7416 | undisplay_dollar(); |
| 7417 | tpos = curwin->w_cursor; |
| 7418 | if (onepage(BACKWARD, 1L) == OK) |
| 7419 | { |
| 7420 | start_arrow(&tpos); |
| 7421 | #ifdef FEAT_CINDENT |
| 7422 | can_cindent = TRUE; |
| 7423 | #endif |
| 7424 | } |
| 7425 | else |
| 7426 | vim_beep(); |
| 7427 | } |
| 7428 | |
| 7429 | static void |
| 7430 | ins_down(startcol) |
| 7431 | int startcol; /* when TRUE move to Insstart.col */ |
| 7432 | { |
| 7433 | pos_T tpos; |
| 7434 | linenr_T old_topline = curwin->w_topline; |
| 7435 | #ifdef FEAT_DIFF |
| 7436 | int old_topfill = curwin->w_topfill; |
| 7437 | #endif |
| 7438 | |
| 7439 | undisplay_dollar(); |
| 7440 | tpos = curwin->w_cursor; |
| 7441 | if (cursor_down(1L, TRUE) == OK) |
| 7442 | { |
| 7443 | if (startcol) |
| 7444 | coladvance(getvcol_nolist(&Insstart)); |
| 7445 | if (old_topline != curwin->w_topline |
| 7446 | #ifdef FEAT_DIFF |
| 7447 | || old_topfill != curwin->w_topfill |
| 7448 | #endif |
| 7449 | ) |
| 7450 | redraw_later(VALID); |
| 7451 | start_arrow(&tpos); |
| 7452 | #ifdef FEAT_CINDENT |
| 7453 | can_cindent = TRUE; |
| 7454 | #endif |
| 7455 | } |
| 7456 | else |
| 7457 | vim_beep(); |
| 7458 | } |
| 7459 | |
| 7460 | static void |
| 7461 | ins_pagedown() |
| 7462 | { |
| 7463 | pos_T tpos; |
| 7464 | |
| 7465 | undisplay_dollar(); |
| 7466 | tpos = curwin->w_cursor; |
| 7467 | if (onepage(FORWARD, 1L) == OK) |
| 7468 | { |
| 7469 | start_arrow(&tpos); |
| 7470 | #ifdef FEAT_CINDENT |
| 7471 | can_cindent = TRUE; |
| 7472 | #endif |
| 7473 | } |
| 7474 | else |
| 7475 | vim_beep(); |
| 7476 | } |
| 7477 | |
| 7478 | #ifdef FEAT_DND |
| 7479 | static void |
| 7480 | ins_drop() |
| 7481 | { |
| 7482 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 7483 | } |
| 7484 | #endif |
| 7485 | |
| 7486 | /* |
| 7487 | * Handle TAB in Insert or Replace mode. |
| 7488 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 7489 | */ |
| 7490 | static int |
| 7491 | ins_tab() |
| 7492 | { |
| 7493 | int ind; |
| 7494 | int i; |
| 7495 | int temp; |
| 7496 | |
| 7497 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 7498 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 7499 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 7500 | return FALSE; |
| 7501 | |
| 7502 | ind = inindent(0); |
| 7503 | #ifdef FEAT_CINDENT |
| 7504 | if (ind) |
| 7505 | can_cindent = FALSE; |
| 7506 | #endif |
| 7507 | |
| 7508 | /* |
| 7509 | * When nothing special, insert TAB like a normal character |
| 7510 | */ |
| 7511 | if (!curbuf->b_p_et |
| 7512 | && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw) |
| 7513 | && curbuf->b_p_sts == 0) |
| 7514 | return TRUE; |
| 7515 | |
| 7516 | if (stop_arrow() == FAIL) |
| 7517 | return TRUE; |
| 7518 | |
| 7519 | did_ai = FALSE; |
| 7520 | #ifdef FEAT_SMARTINDENT |
| 7521 | did_si = FALSE; |
| 7522 | can_si = FALSE; |
| 7523 | can_si_back = FALSE; |
| 7524 | #endif |
| 7525 | AppendToRedobuff((char_u *)"\t"); |
| 7526 | |
| 7527 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
| 7528 | temp = (int)curbuf->b_p_sw; |
| 7529 | else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */ |
| 7530 | temp = (int)curbuf->b_p_sts; |
| 7531 | else /* otherwise use 'tabstop' */ |
| 7532 | temp = (int)curbuf->b_p_ts; |
| 7533 | temp -= get_nolist_virtcol() % temp; |
| 7534 | |
| 7535 | /* |
| 7536 | * Insert the first space with ins_char(). It will delete one char in |
| 7537 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 7538 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 7539 | */ |
| 7540 | ins_char(' '); |
| 7541 | while (--temp > 0) |
| 7542 | { |
| 7543 | #ifdef FEAT_VREPLACE |
| 7544 | if (State & VREPLACE_FLAG) |
| 7545 | ins_char(' '); |
| 7546 | else |
| 7547 | #endif |
| 7548 | { |
| 7549 | ins_str((char_u *)" "); |
| 7550 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 7551 | replace_push(NUL); |
| 7552 | } |
| 7553 | } |
| 7554 | |
| 7555 | /* |
| 7556 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 7557 | */ |
| 7558 | if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind))) |
| 7559 | { |
| 7560 | char_u *ptr; |
| 7561 | #ifdef FEAT_VREPLACE |
| 7562 | char_u *saved_line = NULL; /* init for GCC */ |
| 7563 | pos_T pos; |
| 7564 | #endif |
| 7565 | pos_T fpos; |
| 7566 | pos_T *cursor; |
| 7567 | colnr_T want_vcol, vcol; |
| 7568 | int change_col = -1; |
| 7569 | int save_list = curwin->w_p_list; |
| 7570 | |
| 7571 | /* |
| 7572 | * Get the current line. For VREPLACE mode, don't make real changes |
| 7573 | * yet, just work on a copy of the line. |
| 7574 | */ |
| 7575 | #ifdef FEAT_VREPLACE |
| 7576 | if (State & VREPLACE_FLAG) |
| 7577 | { |
| 7578 | pos = curwin->w_cursor; |
| 7579 | cursor = &pos; |
| 7580 | saved_line = vim_strsave(ml_get_curline()); |
| 7581 | if (saved_line == NULL) |
| 7582 | return FALSE; |
| 7583 | ptr = saved_line + pos.col; |
| 7584 | } |
| 7585 | else |
| 7586 | #endif |
| 7587 | { |
| 7588 | ptr = ml_get_cursor(); |
| 7589 | cursor = &curwin->w_cursor; |
| 7590 | } |
| 7591 | |
| 7592 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 7593 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 7594 | curwin->w_p_list = FALSE; |
| 7595 | |
| 7596 | /* Find first white before the cursor */ |
| 7597 | fpos = curwin->w_cursor; |
| 7598 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 7599 | { |
| 7600 | --fpos.col; |
| 7601 | --ptr; |
| 7602 | } |
| 7603 | |
| 7604 | /* In Replace mode, don't change characters before the insert point. */ |
| 7605 | if ((State & REPLACE_FLAG) |
| 7606 | && fpos.lnum == Insstart.lnum |
| 7607 | && fpos.col < Insstart.col) |
| 7608 | { |
| 7609 | ptr += Insstart.col - fpos.col; |
| 7610 | fpos.col = Insstart.col; |
| 7611 | } |
| 7612 | |
| 7613 | /* compute virtual column numbers of first white and cursor */ |
| 7614 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 7615 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 7616 | |
| 7617 | /* Use as many TABs as possible. Beware of 'showbreak' and |
| 7618 | * 'linebreak' adding extra virtual columns. */ |
| 7619 | while (vim_iswhite(*ptr)) |
| 7620 | { |
| 7621 | i = lbr_chartabsize((char_u *)"\t", vcol); |
| 7622 | if (vcol + i > want_vcol) |
| 7623 | break; |
| 7624 | if (*ptr != TAB) |
| 7625 | { |
| 7626 | *ptr = TAB; |
| 7627 | if (change_col < 0) |
| 7628 | { |
| 7629 | change_col = fpos.col; /* Column of first change */ |
| 7630 | /* May have to adjust Insstart */ |
| 7631 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 7632 | Insstart.col = fpos.col; |
| 7633 | } |
| 7634 | } |
| 7635 | ++fpos.col; |
| 7636 | ++ptr; |
| 7637 | vcol += i; |
| 7638 | } |
| 7639 | |
| 7640 | if (change_col >= 0) |
| 7641 | { |
| 7642 | int repl_off = 0; |
| 7643 | |
| 7644 | /* Skip over the spaces we need. */ |
| 7645 | while (vcol < want_vcol && *ptr == ' ') |
| 7646 | { |
| 7647 | vcol += lbr_chartabsize(ptr, vcol); |
| 7648 | ++ptr; |
| 7649 | ++repl_off; |
| 7650 | } |
| 7651 | if (vcol > want_vcol) |
| 7652 | { |
| 7653 | /* Must have a char with 'showbreak' just before it. */ |
| 7654 | --ptr; |
| 7655 | --repl_off; |
| 7656 | } |
| 7657 | fpos.col += repl_off; |
| 7658 | |
| 7659 | /* Delete following spaces. */ |
| 7660 | i = cursor->col - fpos.col; |
| 7661 | if (i > 0) |
| 7662 | { |
| 7663 | mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1); |
| 7664 | /* correct replace stack. */ |
| 7665 | if ((State & REPLACE_FLAG) |
| 7666 | #ifdef FEAT_VREPLACE |
| 7667 | && !(State & VREPLACE_FLAG) |
| 7668 | #endif |
| 7669 | ) |
| 7670 | for (temp = i; --temp >= 0; ) |
| 7671 | replace_join(repl_off); |
| 7672 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 7673 | #ifdef FEAT_NETBEANS_INTG |
| 7674 | if (usingNetbeans) |
| 7675 | { |
| 7676 | netbeans_removed(curbuf, fpos.lnum, cursor->col, |
| 7677 | (long)(i + 1)); |
| 7678 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 7679 | (char_u *)"\t", 1); |
| 7680 | } |
| 7681 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7682 | cursor->col -= i; |
| 7683 | |
| 7684 | #ifdef FEAT_VREPLACE |
| 7685 | /* |
| 7686 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 7687 | * backspacing over the changed spacing and then inserting the new |
| 7688 | * spacing. |
| 7689 | */ |
| 7690 | if (State & VREPLACE_FLAG) |
| 7691 | { |
| 7692 | /* Backspace from real cursor to change_col */ |
| 7693 | backspace_until_column(change_col); |
| 7694 | |
| 7695 | /* Insert each char in saved_line from changed_col to |
| 7696 | * ptr-cursor */ |
| 7697 | ins_bytes_len(saved_line + change_col, |
| 7698 | cursor->col - change_col); |
| 7699 | } |
| 7700 | #endif |
| 7701 | } |
| 7702 | |
| 7703 | #ifdef FEAT_VREPLACE |
| 7704 | if (State & VREPLACE_FLAG) |
| 7705 | vim_free(saved_line); |
| 7706 | #endif |
| 7707 | curwin->w_p_list = save_list; |
| 7708 | } |
| 7709 | |
| 7710 | return FALSE; |
| 7711 | } |
| 7712 | |
| 7713 | /* |
| 7714 | * Handle CR or NL in insert mode. |
| 7715 | * Return TRUE when out of memory or can't undo. |
| 7716 | */ |
| 7717 | static int |
| 7718 | ins_eol(c) |
| 7719 | int c; |
| 7720 | { |
| 7721 | int i; |
| 7722 | |
| 7723 | if (echeck_abbr(c + ABBR_OFF)) |
| 7724 | return FALSE; |
| 7725 | if (stop_arrow() == FAIL) |
| 7726 | return TRUE; |
| 7727 | undisplay_dollar(); |
| 7728 | |
| 7729 | /* |
| 7730 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 7731 | * character under the cursor. Only push a NUL on the replace stack, |
| 7732 | * nothing to put back when the NL is deleted. |
| 7733 | */ |
| 7734 | if ((State & REPLACE_FLAG) |
| 7735 | #ifdef FEAT_VREPLACE |
| 7736 | && !(State & VREPLACE_FLAG) |
| 7737 | #endif |
| 7738 | ) |
| 7739 | replace_push(NUL); |
| 7740 | |
| 7741 | /* |
| 7742 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 7743 | * replacing the next line, so we push all of the characters left on the |
| 7744 | * line onto the replace stack. This is not done here though, it is done |
| 7745 | * in open_line(). |
| 7746 | */ |
| 7747 | |
| 7748 | #ifdef FEAT_RIGHTLEFT |
| 7749 | # ifdef FEAT_FKMAP |
| 7750 | if (p_altkeymap && p_fkmap) |
| 7751 | fkmap(NL); |
| 7752 | # endif |
| 7753 | /* NL in reverse insert will always start in the end of |
| 7754 | * current line. */ |
| 7755 | if (revins_on) |
| 7756 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 7757 | #endif |
| 7758 | |
| 7759 | AppendToRedobuff(NL_STR); |
| 7760 | i = open_line(FORWARD, |
| 7761 | #ifdef FEAT_COMMENTS |
| 7762 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 7763 | #endif |
| 7764 | 0, old_indent); |
| 7765 | old_indent = 0; |
| 7766 | #ifdef FEAT_CINDENT |
| 7767 | can_cindent = TRUE; |
| 7768 | #endif |
| 7769 | |
| 7770 | return (!i); |
| 7771 | } |
| 7772 | |
| 7773 | #ifdef FEAT_DIGRAPHS |
| 7774 | /* |
| 7775 | * Handle digraph in insert mode. |
| 7776 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 7777 | * done. |
| 7778 | */ |
| 7779 | static int |
| 7780 | ins_digraph() |
| 7781 | { |
| 7782 | int c; |
| 7783 | int cc; |
| 7784 | |
| 7785 | pc_status = PC_STATUS_UNSET; |
| 7786 | if (redrawing() && !char_avail()) |
| 7787 | { |
| 7788 | /* may need to redraw when no more chars available now */ |
| 7789 | ins_redraw(); |
| 7790 | |
| 7791 | edit_putchar('?', TRUE); |
| 7792 | #ifdef FEAT_CMDL_INFO |
| 7793 | add_to_showcmd_c(Ctrl_K); |
| 7794 | #endif |
| 7795 | } |
| 7796 | |
| 7797 | #ifdef USE_ON_FLY_SCROLL |
| 7798 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 7799 | #endif |
| 7800 | |
| 7801 | /* don't map the digraph chars. This also prevents the |
| 7802 | * mode message to be deleted when ESC is hit */ |
| 7803 | ++no_mapping; |
| 7804 | ++allow_keys; |
| 7805 | c = safe_vgetc(); |
| 7806 | --no_mapping; |
| 7807 | --allow_keys; |
| 7808 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 7809 | { |
| 7810 | #ifdef FEAT_CMDL_INFO |
| 7811 | clear_showcmd(); |
| 7812 | #endif |
| 7813 | insert_special(c, TRUE, FALSE); |
| 7814 | return NUL; |
| 7815 | } |
| 7816 | if (c != ESC) |
| 7817 | { |
| 7818 | if (redrawing() && !char_avail()) |
| 7819 | { |
| 7820 | /* may need to redraw when no more chars available now */ |
| 7821 | ins_redraw(); |
| 7822 | |
| 7823 | if (char2cells(c) == 1) |
| 7824 | { |
| 7825 | /* first remove the '?', otherwise it's restored when typing |
| 7826 | * an ESC next */ |
| 7827 | edit_unputchar(); |
| 7828 | ins_redraw(); |
| 7829 | edit_putchar(c, TRUE); |
| 7830 | } |
| 7831 | #ifdef FEAT_CMDL_INFO |
| 7832 | add_to_showcmd_c(c); |
| 7833 | #endif |
| 7834 | } |
| 7835 | ++no_mapping; |
| 7836 | ++allow_keys; |
| 7837 | cc = safe_vgetc(); |
| 7838 | --no_mapping; |
| 7839 | --allow_keys; |
| 7840 | if (cc != ESC) |
| 7841 | { |
| 7842 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 7843 | c = getdigraph(c, cc, TRUE); |
| 7844 | #ifdef FEAT_CMDL_INFO |
| 7845 | clear_showcmd(); |
| 7846 | #endif |
| 7847 | return c; |
| 7848 | } |
| 7849 | } |
| 7850 | edit_unputchar(); |
| 7851 | #ifdef FEAT_CMDL_INFO |
| 7852 | clear_showcmd(); |
| 7853 | #endif |
| 7854 | return NUL; |
| 7855 | } |
| 7856 | #endif |
| 7857 | |
| 7858 | /* |
| 7859 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 7860 | * Returns the char to be inserted, or NUL if none found. |
| 7861 | */ |
| 7862 | static int |
| 7863 | ins_copychar(lnum) |
| 7864 | linenr_T lnum; |
| 7865 | { |
| 7866 | int c; |
| 7867 | int temp; |
| 7868 | char_u *ptr, *prev_ptr; |
| 7869 | |
| 7870 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 7871 | { |
| 7872 | vim_beep(); |
| 7873 | return NUL; |
| 7874 | } |
| 7875 | |
| 7876 | /* try to advance to the cursor column */ |
| 7877 | temp = 0; |
| 7878 | ptr = ml_get(lnum); |
| 7879 | prev_ptr = ptr; |
| 7880 | validate_virtcol(); |
| 7881 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 7882 | { |
| 7883 | prev_ptr = ptr; |
| 7884 | temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp); |
| 7885 | } |
| 7886 | if ((colnr_T)temp > curwin->w_virtcol) |
| 7887 | ptr = prev_ptr; |
| 7888 | |
| 7889 | #ifdef FEAT_MBYTE |
| 7890 | c = (*mb_ptr2char)(ptr); |
| 7891 | #else |
| 7892 | c = *ptr; |
| 7893 | #endif |
| 7894 | if (c == NUL) |
| 7895 | vim_beep(); |
| 7896 | return c; |
| 7897 | } |
| 7898 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7899 | /* |
| 7900 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 7901 | */ |
| 7902 | static int |
| 7903 | ins_ctrl_ey(tc) |
| 7904 | int tc; |
| 7905 | { |
| 7906 | int c = tc; |
| 7907 | |
| 7908 | #ifdef FEAT_INS_EXPAND |
| 7909 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 7910 | { |
| 7911 | if (c == Ctrl_Y) |
| 7912 | scrolldown_clamp(); |
| 7913 | else |
| 7914 | scrollup_clamp(); |
| 7915 | redraw_later(VALID); |
| 7916 | } |
| 7917 | else |
| 7918 | #endif |
| 7919 | { |
| 7920 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 7921 | if (c != NUL) |
| 7922 | { |
| 7923 | long tw_save; |
| 7924 | |
| 7925 | /* The character must be taken literally, insert like it |
| 7926 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 7927 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 7928 | * CTRL-V, don't use it for these. */ |
| 7929 | if (c < 256 && !isalnum(c)) |
| 7930 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 7931 | tw_save = curbuf->b_p_tw; |
| 7932 | curbuf->b_p_tw = -1; |
| 7933 | insert_special(c, TRUE, FALSE); |
| 7934 | curbuf->b_p_tw = tw_save; |
| 7935 | #ifdef FEAT_RIGHTLEFT |
| 7936 | revins_chars++; |
| 7937 | revins_legal++; |
| 7938 | #endif |
| 7939 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 7940 | auto_format(FALSE, TRUE); |
| 7941 | } |
| 7942 | } |
| 7943 | return c; |
| 7944 | } |
| 7945 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7946 | #ifdef FEAT_SMARTINDENT |
| 7947 | /* |
| 7948 | * Try to do some very smart auto-indenting. |
| 7949 | * Used when inserting a "normal" character. |
| 7950 | */ |
| 7951 | static void |
| 7952 | ins_try_si(c) |
| 7953 | int c; |
| 7954 | { |
| 7955 | pos_T *pos, old_pos; |
| 7956 | char_u *ptr; |
| 7957 | int i; |
| 7958 | int temp; |
| 7959 | |
| 7960 | /* |
| 7961 | * do some very smart indenting when entering '{' or '}' |
| 7962 | */ |
| 7963 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 7964 | { |
| 7965 | /* |
| 7966 | * for '}' set indent equal to indent of line containing matching '{' |
| 7967 | */ |
| 7968 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 7969 | { |
| 7970 | old_pos = curwin->w_cursor; |
| 7971 | /* |
| 7972 | * If the matching '{' has a ')' immediately before it (ignoring |
| 7973 | * white-space), then line up with the start of the line |
| 7974 | * containing the matching '(' if there is one. This handles the |
| 7975 | * case where an "if (..\n..) {" statement continues over multiple |
| 7976 | * lines -- webb |
| 7977 | */ |
| 7978 | ptr = ml_get(pos->lnum); |
| 7979 | i = pos->col; |
| 7980 | if (i > 0) /* skip blanks before '{' */ |
| 7981 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 7982 | ; |
| 7983 | curwin->w_cursor.lnum = pos->lnum; |
| 7984 | curwin->w_cursor.col = i; |
| 7985 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 7986 | curwin->w_cursor = *pos; |
| 7987 | i = get_indent(); |
| 7988 | curwin->w_cursor = old_pos; |
| 7989 | #ifdef FEAT_VREPLACE |
| 7990 | if (State & VREPLACE_FLAG) |
| 7991 | change_indent(INDENT_SET, i, FALSE, NUL); |
| 7992 | else |
| 7993 | #endif |
| 7994 | (void)set_indent(i, SIN_CHANGED); |
| 7995 | } |
| 7996 | else if (curwin->w_cursor.col > 0) |
| 7997 | { |
| 7998 | /* |
| 7999 | * when inserting '{' after "O" reduce indent, but not |
| 8000 | * more than indent of previous line |
| 8001 | */ |
| 8002 | temp = TRUE; |
| 8003 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 8004 | { |
| 8005 | old_pos = curwin->w_cursor; |
| 8006 | i = get_indent(); |
| 8007 | while (curwin->w_cursor.lnum > 1) |
| 8008 | { |
| 8009 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 8010 | |
| 8011 | /* ignore empty lines and lines starting with '#'. */ |
| 8012 | if (*ptr != '#' && *ptr != NUL) |
| 8013 | break; |
| 8014 | } |
| 8015 | if (get_indent() >= i) |
| 8016 | temp = FALSE; |
| 8017 | curwin->w_cursor = old_pos; |
| 8018 | } |
| 8019 | if (temp) |
| 8020 | shift_line(TRUE, FALSE, 1); |
| 8021 | } |
| 8022 | } |
| 8023 | |
| 8024 | /* |
| 8025 | * set indent of '#' always to 0 |
| 8026 | */ |
| 8027 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 8028 | { |
| 8029 | /* remember current indent for next line */ |
| 8030 | old_indent = get_indent(); |
| 8031 | (void)set_indent(0, SIN_CHANGED); |
| 8032 | } |
| 8033 | |
| 8034 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 8035 | if (ai_col > curwin->w_cursor.col) |
| 8036 | ai_col = curwin->w_cursor.col; |
| 8037 | } |
| 8038 | #endif |
| 8039 | |
| 8040 | /* |
| 8041 | * Get the value that w_virtcol would have when 'list' is off. |
| 8042 | * Unless 'cpo' contains the 'L' flag. |
| 8043 | */ |
| 8044 | static colnr_T |
| 8045 | get_nolist_virtcol() |
| 8046 | { |
| 8047 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 8048 | return getvcol_nolist(&curwin->w_cursor); |
| 8049 | validate_virtcol(); |
| 8050 | return curwin->w_virtcol; |
| 8051 | } |