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 */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 71 | char_u *cp_extra; /* extra menu text (allocated, can be NULL) */ |
| 72 | char_u *cp_info; /* verbose info (can be NULL) */ |
| 73 | char_u cp_kind; /* kind of match, single letter, or NUL */ |
| 74 | char_u *cp_fname; /* file containing the match, allocated when |
| 75 | * cp_flags has FREE_FNAME */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 76 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 77 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 80 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | #define FREE_FNAME (2) |
| 82 | |
| 83 | /* |
| 84 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 85 | * "compl_first_match" points to the start of the list. |
| 86 | * "compl_curr_match" points to the currently selected entry. |
| 87 | * "compl_shown_match" is different from compl_curr_match during |
| 88 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 90 | static compl_T *compl_first_match = NULL; |
| 91 | static compl_T *compl_curr_match = NULL; |
| 92 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 94 | /* When "compl_leader" is not NULL only matches that start with this string |
| 95 | * are used. */ |
| 96 | static char_u *compl_leader = NULL; |
| 97 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 98 | static int compl_get_longest = FALSE; /* put longest common string |
| 99 | in compl_leader */ |
| 100 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 101 | static int compl_used_match; /* Selected one of the matches. When |
| 102 | FALSE the match was edited or using |
| 103 | the longest common string. */ |
| 104 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 105 | /* When the first completion is done "compl_started" is set. When it's |
| 106 | * FALSE the word to be completed must be located. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 107 | static int compl_started = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 108 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 109 | static int compl_matches = 0; |
| 110 | static char_u *compl_pattern = NULL; |
| 111 | static int compl_direction = FORWARD; |
| 112 | static int compl_shows_dir = FORWARD; |
| 113 | static int compl_pending = FALSE; |
| 114 | static pos_T compl_startpos; |
| 115 | static colnr_T compl_col = 0; /* column where the text starts |
| 116 | * that is being completed */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 117 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 118 | * completion started */ |
| 119 | static int compl_cont_mode = 0; |
| 120 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 122 | static void ins_ctrl_x __ARGS((void)); |
| 123 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 124 | static void ins_compl_longest_match __ARGS((compl_T *match)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 125 | static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 126 | static int ins_compl_make_cyclic __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 127 | static void ins_compl_upd_pum __ARGS((void)); |
| 128 | static void ins_compl_del_pum __ARGS((void)); |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 129 | static int pum_wanted __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 130 | static int pum_two_or_more __ARGS((void)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 131 | static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 132 | static char_u *find_line_end __ARGS((char_u *ptr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 133 | static void ins_compl_free __ARGS((void)); |
| 134 | static void ins_compl_clear __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 135 | static int ins_compl_bs __ARGS((void)); |
| 136 | static void ins_compl_addleader __ARGS((int c)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 137 | static void ins_compl_addfrommatch __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 138 | static int ins_compl_prep __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 139 | static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 140 | static int ins_compl_get_exp __ARGS((pos_T *ini)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | static void ins_compl_delete __ARGS((void)); |
| 142 | static void ins_compl_insert __ARGS((void)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 143 | static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 144 | static int ins_compl_key2dir __ARGS((int c)); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 145 | static int ins_compl_pum_key __ARGS((int c)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 146 | static int ins_compl_key2count __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | static int ins_complete __ARGS((int c)); |
| 148 | static int quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
| 149 | #endif /* FEAT_INS_EXPAND */ |
| 150 | |
| 151 | #define BACKSPACE_CHAR 1 |
| 152 | #define BACKSPACE_WORD 2 |
| 153 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 154 | #define BACKSPACE_LINE 4 |
| 155 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 156 | static void ins_redraw __ARGS((int ready)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 157 | static void ins_ctrl_v __ARGS((void)); |
| 158 | static void undisplay_dollar __ARGS((void)); |
| 159 | static void insert_special __ARGS((int, int, int)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 160 | static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | static void check_auto_format __ARGS((int)); |
| 162 | static void redo_literal __ARGS((int c)); |
| 163 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 164 | #ifdef FEAT_SYN_HL |
| 165 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 166 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 167 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 168 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 169 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc)); |
| 170 | static int echeck_abbr __ARGS((int)); |
| 171 | static void replace_push_off __ARGS((int c)); |
| 172 | static int replace_pop __ARGS((void)); |
| 173 | static void replace_join __ARGS((int off)); |
| 174 | static void replace_pop_ins __ARGS((void)); |
| 175 | #ifdef FEAT_MBYTE |
| 176 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 177 | #endif |
| 178 | static void replace_flush __ARGS((void)); |
| 179 | static void replace_do_bs __ARGS((void)); |
| 180 | #ifdef FEAT_CINDENT |
| 181 | static int cindent_on __ARGS((void)); |
| 182 | #endif |
| 183 | static void ins_reg __ARGS((void)); |
| 184 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 185 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 186 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | #ifdef FEAT_RIGHTLEFT |
| 188 | static void ins_ctrl_ __ARGS((void)); |
| 189 | #endif |
| 190 | #ifdef FEAT_VISUAL |
| 191 | static int ins_start_select __ARGS((int c)); |
| 192 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 193 | static void ins_insert __ARGS((int replaceState)); |
| 194 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 195 | static void ins_shift __ARGS((int c, int lastc)); |
| 196 | static void ins_del __ARGS((void)); |
| 197 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 198 | #ifdef FEAT_MOUSE |
| 199 | static void ins_mouse __ARGS((int c)); |
| 200 | static void ins_mousescroll __ARGS((int up)); |
| 201 | #endif |
| 202 | static void ins_left __ARGS((void)); |
| 203 | static void ins_home __ARGS((int c)); |
| 204 | static void ins_end __ARGS((int c)); |
| 205 | static void ins_s_left __ARGS((void)); |
| 206 | static void ins_right __ARGS((void)); |
| 207 | static void ins_s_right __ARGS((void)); |
| 208 | static void ins_up __ARGS((int startcol)); |
| 209 | static void ins_pageup __ARGS((void)); |
| 210 | static void ins_down __ARGS((int startcol)); |
| 211 | static void ins_pagedown __ARGS((void)); |
| 212 | #ifdef FEAT_DND |
| 213 | static void ins_drop __ARGS((void)); |
| 214 | #endif |
| 215 | static int ins_tab __ARGS((void)); |
| 216 | static int ins_eol __ARGS((int c)); |
| 217 | #ifdef FEAT_DIGRAPHS |
| 218 | static int ins_digraph __ARGS((void)); |
| 219 | #endif |
| 220 | static int ins_copychar __ARGS((linenr_T lnum)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 221 | static int ins_ctrl_ey __ARGS((int tc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 222 | #ifdef FEAT_SMARTINDENT |
| 223 | static void ins_try_si __ARGS((int c)); |
| 224 | #endif |
| 225 | static colnr_T get_nolist_virtcol __ARGS((void)); |
| 226 | |
| 227 | static colnr_T Insstart_textlen; /* length of line when insert started */ |
| 228 | static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ |
| 229 | |
| 230 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 231 | K_SPECIAL and CSI are escaped */ |
| 232 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 233 | static int new_insert_skip; /* nr of chars in front of current insert */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 234 | static int did_restart_edit; /* "restart_edit" when calling edit() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | |
| 236 | #ifdef FEAT_CINDENT |
| 237 | static int can_cindent; /* may do cindenting on this line */ |
| 238 | #endif |
| 239 | |
| 240 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 241 | |
| 242 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 243 | static int revins_on; /* reverse insert mode on */ |
| 244 | static int revins_chars; /* how much to skip after edit */ |
| 245 | static int revins_legal; /* was the last char 'legal'? */ |
| 246 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 247 | #endif |
| 248 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | static int ins_need_undo; /* call u_save() before inserting a |
| 250 | char. Set when edit() is called. |
| 251 | after that arrow_used is used. */ |
| 252 | |
| 253 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 254 | under the cursor */ |
| 255 | |
| 256 | /* |
| 257 | * edit(): Start inserting text. |
| 258 | * |
| 259 | * "cmdchar" can be: |
| 260 | * 'i' normal insert command |
| 261 | * 'a' normal append command |
| 262 | * 'R' replace command |
| 263 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 264 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 265 | * 'g' "gI" command. |
| 266 | * 'V' "gR" command for Virtual Replace mode. |
| 267 | * 'v' "gr" command for single character Virtual Replace mode. |
| 268 | * |
| 269 | * This function is not called recursively. For CTRL-O commands, it returns |
| 270 | * and lets the caller handle the Normal-mode command. |
| 271 | * |
| 272 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 273 | */ |
| 274 | int |
| 275 | edit(cmdchar, startln, count) |
| 276 | int cmdchar; |
| 277 | int startln; /* if set, insert at start of line */ |
| 278 | long count; |
| 279 | { |
| 280 | int c = 0; |
| 281 | char_u *ptr; |
| 282 | int lastc; |
| 283 | colnr_T mincol; |
| 284 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | int i; |
| 286 | int did_backspace = TRUE; /* previous char was backspace */ |
| 287 | #ifdef FEAT_CINDENT |
| 288 | int line_is_white = FALSE; /* line is empty before insert */ |
| 289 | #endif |
| 290 | linenr_T old_topline = 0; /* topline before insertion */ |
| 291 | #ifdef FEAT_DIFF |
| 292 | int old_topfill = -1; |
| 293 | #endif |
| 294 | int inserted_space = FALSE; /* just inserted a space */ |
| 295 | int replaceState = REPLACE; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 296 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 297 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 298 | /* Remember whether editing was restarted after CTRL-O. */ |
| 299 | did_restart_edit = restart_edit; |
| 300 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 301 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 302 | * error message */ |
| 303 | check_for_delay(TRUE); |
| 304 | |
| 305 | #ifdef HAVE_SANDBOX |
| 306 | /* Don't allow inserting in the sandbox. */ |
| 307 | if (sandbox != 0) |
| 308 | { |
| 309 | EMSG(_(e_sandbox)); |
| 310 | return FALSE; |
| 311 | } |
| 312 | #endif |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 313 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 314 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 315 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 316 | { |
| 317 | EMSG(_(e_secure)); |
| 318 | return FALSE; |
| 319 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | |
| 321 | #ifdef FEAT_INS_EXPAND |
| 322 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 323 | #endif |
| 324 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 325 | #ifdef FEAT_AUTOCMD |
| 326 | /* |
| 327 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 328 | */ |
| 329 | if (cmdchar != 'r' && cmdchar != 'v') |
| 330 | { |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 331 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 332 | if (cmdchar == 'R') |
| 333 | ptr = (char_u *)"r"; |
| 334 | else if (cmdchar == 'V') |
| 335 | ptr = (char_u *)"v"; |
| 336 | else |
| 337 | ptr = (char_u *)"i"; |
| 338 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 339 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 340 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
| 341 | } |
| 342 | #endif |
| 343 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 344 | #ifdef FEAT_MOUSE |
| 345 | /* |
| 346 | * When doing a paste with the middle mouse button, Insstart is set to |
| 347 | * where the paste started. |
| 348 | */ |
| 349 | if (where_paste_started.lnum != 0) |
| 350 | Insstart = where_paste_started; |
| 351 | else |
| 352 | #endif |
| 353 | { |
| 354 | Insstart = curwin->w_cursor; |
| 355 | if (startln) |
| 356 | Insstart.col = 0; |
| 357 | } |
| 358 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 359 | Insstart_blank_vcol = MAXCOL; |
| 360 | if (!did_ai) |
| 361 | ai_col = 0; |
| 362 | |
| 363 | if (cmdchar != NUL && restart_edit == 0) |
| 364 | { |
| 365 | ResetRedobuff(); |
| 366 | AppendNumberToRedobuff(count); |
| 367 | #ifdef FEAT_VREPLACE |
| 368 | if (cmdchar == 'V' || cmdchar == 'v') |
| 369 | { |
| 370 | /* "gR" or "gr" command */ |
| 371 | AppendCharToRedobuff('g'); |
| 372 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 373 | } |
| 374 | else |
| 375 | #endif |
| 376 | { |
| 377 | AppendCharToRedobuff(cmdchar); |
| 378 | if (cmdchar == 'g') /* "gI" command */ |
| 379 | AppendCharToRedobuff('I'); |
| 380 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 381 | count = 1; /* insert only one <CR> */ |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (cmdchar == 'R') |
| 386 | { |
| 387 | #ifdef FEAT_FKMAP |
| 388 | if (p_fkmap && p_ri) |
| 389 | { |
| 390 | beep_flush(); |
| 391 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 392 | State = INSERT; |
| 393 | } |
| 394 | else |
| 395 | #endif |
| 396 | State = REPLACE; |
| 397 | } |
| 398 | #ifdef FEAT_VREPLACE |
| 399 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 400 | { |
| 401 | State = VREPLACE; |
| 402 | replaceState = VREPLACE; |
| 403 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 404 | vr_lines_changed = 1; |
| 405 | } |
| 406 | #endif |
| 407 | else |
| 408 | State = INSERT; |
| 409 | |
| 410 | stop_insert_mode = FALSE; |
| 411 | |
| 412 | /* |
| 413 | * Need to recompute the cursor position, it might move when the cursor is |
| 414 | * on a TAB or special character. |
| 415 | */ |
| 416 | curs_columns(TRUE); |
| 417 | |
| 418 | /* |
| 419 | * Enable langmap or IME, indicated by 'iminsert'. |
| 420 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 421 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 422 | * when hitting <Esc>. |
| 423 | */ |
| 424 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 425 | State |= LANGMAP; |
| 426 | #ifdef USE_IM_CONTROL |
| 427 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 428 | #endif |
| 429 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 430 | #ifdef FEAT_MOUSE |
| 431 | setmouse(); |
| 432 | #endif |
| 433 | #ifdef FEAT_CMDL_INFO |
| 434 | clear_showcmd(); |
| 435 | #endif |
| 436 | #ifdef FEAT_RIGHTLEFT |
| 437 | /* there is no reverse replace mode */ |
| 438 | revins_on = (State == INSERT && p_ri); |
| 439 | if (revins_on) |
| 440 | undisplay_dollar(); |
| 441 | revins_chars = 0; |
| 442 | revins_legal = 0; |
| 443 | revins_scol = -1; |
| 444 | #endif |
| 445 | |
| 446 | /* |
| 447 | * Handle restarting Insert mode. |
| 448 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 449 | * restart_edit non-zero, and something in the stuff buffer. |
| 450 | */ |
| 451 | if (restart_edit != 0 && stuff_empty()) |
| 452 | { |
| 453 | #ifdef FEAT_MOUSE |
| 454 | /* |
| 455 | * After a paste we consider text typed to be part of the insert for |
| 456 | * the pasted text. You can backspace over the pasted text too. |
| 457 | */ |
| 458 | if (where_paste_started.lnum) |
| 459 | arrow_used = FALSE; |
| 460 | else |
| 461 | #endif |
| 462 | arrow_used = TRUE; |
| 463 | restart_edit = 0; |
| 464 | |
| 465 | /* |
| 466 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 467 | * now at the end-of-line, put it after the end-of-line (this is not |
| 468 | * correct in very rare cases). |
| 469 | * Also do this if curswant is greater than the current virtual |
| 470 | * column. Eg after "^O$" or "^O80|". |
| 471 | */ |
| 472 | validate_virtcol(); |
| 473 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 474 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 475 | || curwin->w_curswant > curwin->w_virtcol) |
| 476 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 477 | { |
| 478 | if (ptr[1] == NUL) |
| 479 | ++curwin->w_cursor.col; |
| 480 | #ifdef FEAT_MBYTE |
| 481 | else if (has_mbyte) |
| 482 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 483 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 484 | if (ptr[i] == NUL) |
| 485 | curwin->w_cursor.col += i; |
| 486 | } |
| 487 | #endif |
| 488 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 489 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 490 | } |
| 491 | else |
| 492 | arrow_used = FALSE; |
| 493 | |
| 494 | /* we are in insert mode now, don't need to start it anymore */ |
| 495 | need_start_insertmode = FALSE; |
| 496 | |
| 497 | /* Need to save the line for undo before inserting the first char. */ |
| 498 | ins_need_undo = TRUE; |
| 499 | |
| 500 | #ifdef FEAT_MOUSE |
| 501 | where_paste_started.lnum = 0; |
| 502 | #endif |
| 503 | #ifdef FEAT_CINDENT |
| 504 | can_cindent = TRUE; |
| 505 | #endif |
| 506 | #ifdef FEAT_FOLDING |
| 507 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 508 | * restarting. */ |
| 509 | if (!p_im && did_restart_edit == 0) |
| 510 | foldOpenCursor(); |
| 511 | #endif |
| 512 | |
| 513 | /* |
| 514 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 515 | * A warning message for changing a readonly file is given here, before |
| 516 | * actually changing anything. It's put after the mode, if any. |
| 517 | */ |
| 518 | i = 0; |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 519 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | i = showmode(); |
| 521 | |
| 522 | if (!p_im && did_restart_edit == 0) |
| 523 | change_warning(i + 1); |
| 524 | |
| 525 | #ifdef CURSOR_SHAPE |
| 526 | ui_cursor_shape(); /* may show different cursor shape */ |
| 527 | #endif |
| 528 | #ifdef FEAT_DIGRAPHS |
| 529 | do_digraph(-1); /* clear digraphs */ |
| 530 | #endif |
| 531 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 532 | /* |
| 533 | * Get the current length of the redo buffer, those characters have to be |
| 534 | * skipped if we want to get to the inserted characters. |
| 535 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 536 | ptr = get_inserted(); |
| 537 | if (ptr == NULL) |
| 538 | new_insert_skip = 0; |
| 539 | else |
| 540 | { |
| 541 | new_insert_skip = (int)STRLEN(ptr); |
| 542 | vim_free(ptr); |
| 543 | } |
| 544 | |
| 545 | old_indent = 0; |
| 546 | |
| 547 | /* |
| 548 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 549 | */ |
| 550 | for (;;) |
| 551 | { |
| 552 | #ifdef FEAT_RIGHTLEFT |
| 553 | if (!revins_legal) |
| 554 | revins_scol = -1; /* reset on illegal motions */ |
| 555 | else |
| 556 | revins_legal = 0; |
| 557 | #endif |
| 558 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 559 | count = 0; |
| 560 | |
| 561 | if (stop_insert_mode) |
| 562 | { |
| 563 | /* ":stopinsert" used or 'insertmode' reset */ |
| 564 | count = 0; |
| 565 | goto doESCkey; |
| 566 | } |
| 567 | |
| 568 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 569 | if (!arrow_used) |
| 570 | curwin->w_set_curswant = TRUE; |
| 571 | |
| 572 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 573 | * menu invoked a shell command). */ |
| 574 | if (stuff_empty()) |
| 575 | { |
| 576 | did_check_timestamps = FALSE; |
| 577 | if (need_check_timestamps) |
| 578 | check_timestamps(FALSE); |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | * When emsg() was called msg_scroll will have been set. |
| 583 | */ |
| 584 | msg_scroll = FALSE; |
| 585 | |
| 586 | #ifdef FEAT_GUI |
| 587 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 588 | * another window. "need_mouse_correct" may then be set because of an |
| 589 | * autocommand. */ |
| 590 | if (need_mouse_correct) |
| 591 | gui_mouse_correct(); |
| 592 | #endif |
| 593 | |
| 594 | #ifdef FEAT_FOLDING |
| 595 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 596 | if (fdo_flags & FDO_INSERT) |
| 597 | foldOpenCursor(); |
| 598 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 599 | if (!char_avail()) |
| 600 | foldCheckClose(); |
| 601 | #endif |
| 602 | |
| 603 | /* |
| 604 | * If we inserted a character at the last position of the last line in |
| 605 | * the window, scroll the window one line up. This avoids an extra |
| 606 | * redraw. |
| 607 | * This is detected when the cursor column is smaller after inserting |
| 608 | * something. |
| 609 | * Don't do this when the topline changed already, it has |
| 610 | * already been adjusted (by insertchar() calling open_line())). |
| 611 | */ |
| 612 | if (curbuf->b_mod_set |
| 613 | && curwin->w_p_wrap |
| 614 | && !did_backspace |
| 615 | && curwin->w_topline == old_topline |
| 616 | #ifdef FEAT_DIFF |
| 617 | && curwin->w_topfill == old_topfill |
| 618 | #endif |
| 619 | ) |
| 620 | { |
| 621 | mincol = curwin->w_wcol; |
| 622 | validate_cursor_col(); |
| 623 | |
| 624 | if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts |
| 625 | && curwin->w_wrow == W_WINROW(curwin) |
| 626 | + curwin->w_height - 1 - p_so |
| 627 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 628 | #ifdef FEAT_DIFF |
| 629 | || curwin->w_topfill > 0 |
| 630 | #endif |
| 631 | )) |
| 632 | { |
| 633 | #ifdef FEAT_DIFF |
| 634 | if (curwin->w_topfill > 0) |
| 635 | --curwin->w_topfill; |
| 636 | else |
| 637 | #endif |
| 638 | #ifdef FEAT_FOLDING |
| 639 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 640 | set_topline(curwin, old_topline + 1); |
| 641 | else |
| 642 | #endif |
| 643 | set_topline(curwin, curwin->w_topline + 1); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | /* May need to adjust w_topline to show the cursor. */ |
| 648 | update_topline(); |
| 649 | |
| 650 | did_backspace = FALSE; |
| 651 | |
| 652 | validate_cursor(); /* may set must_redraw */ |
| 653 | |
| 654 | /* |
| 655 | * Redraw the display when no characters are waiting. |
| 656 | * Also shows mode, ruler and positions cursor. |
| 657 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 658 | ins_redraw(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 659 | |
| 660 | #ifdef FEAT_SCROLLBIND |
| 661 | if (curwin->w_p_scb) |
| 662 | do_check_scrollbind(TRUE); |
| 663 | #endif |
| 664 | |
| 665 | update_curswant(); |
| 666 | old_topline = curwin->w_topline; |
| 667 | #ifdef FEAT_DIFF |
| 668 | old_topfill = curwin->w_topfill; |
| 669 | #endif |
| 670 | |
| 671 | #ifdef USE_ON_FLY_SCROLL |
| 672 | dont_scroll = FALSE; /* allow scrolling here */ |
| 673 | #endif |
| 674 | |
| 675 | /* |
| 676 | * Get a character for Insert mode. |
| 677 | */ |
| 678 | lastc = c; /* remember previous char for CTRL-D */ |
| 679 | c = safe_vgetc(); |
| 680 | |
| 681 | #ifdef FEAT_RIGHTLEFT |
| 682 | if (p_hkmap && KeyTyped) |
| 683 | c = hkmap(c); /* Hebrew mode mapping */ |
| 684 | #endif |
| 685 | #ifdef FEAT_FKMAP |
| 686 | if (p_fkmap && KeyTyped) |
| 687 | c = fkmap(c); /* Farsi mode mapping */ |
| 688 | #endif |
| 689 | |
| 690 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 691 | /* |
| 692 | * Special handling of keys while the popup menu is visible or wanted |
| 693 | * and the cursor is still in the completed word. |
| 694 | */ |
| 695 | if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 696 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 697 | /* BS: Delete one character from "compl_leader". */ |
| 698 | if ((c == K_BS || c == Ctrl_H) |
| 699 | && curwin->w_cursor.col > compl_col && ins_compl_bs()) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 700 | continue; |
| 701 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 702 | /* When no match was selected or it was edited. */ |
| 703 | if (!compl_used_match) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 704 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 705 | /* CTRL-L: Add one character from the current match to |
| 706 | * "compl_leader". */ |
| 707 | if (c == Ctrl_L) |
| 708 | { |
| 709 | ins_compl_addfrommatch(); |
| 710 | continue; |
| 711 | } |
| 712 | |
| 713 | /* A printable character: Add it to "compl_leader". */ |
| 714 | if (vim_isprintc(c)) |
| 715 | { |
| 716 | ins_compl_addleader(c); |
| 717 | continue; |
| 718 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 719 | |
| 720 | /* Pressing Enter selects the current match. */ |
| 721 | if (c == CAR || c == K_KENTER || c == NL) |
| 722 | { |
| 723 | ins_compl_delete(); |
| 724 | ins_compl_insert(); |
| 725 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 729 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 730 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 731 | compl_get_longest = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 732 | if (c != K_IGNORE && ins_compl_prep(c)) |
| 733 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | #endif |
| 735 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 736 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 737 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 738 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 739 | if (c == Ctrl_BSL) |
| 740 | { |
| 741 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 742 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | ++no_mapping; |
| 744 | ++allow_keys; |
| 745 | c = safe_vgetc(); |
| 746 | --no_mapping; |
| 747 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 748 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 749 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 750 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 751 | vungetc(c); |
| 752 | c = Ctrl_BSL; |
| 753 | } |
| 754 | else if (c == Ctrl_G && p_im) |
| 755 | continue; |
| 756 | else |
| 757 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 758 | if (c == Ctrl_O) |
| 759 | { |
| 760 | ins_ctrl_o(); |
| 761 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 762 | nomove = TRUE; |
| 763 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 764 | count = 0; |
| 765 | goto doESCkey; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | #ifdef FEAT_DIGRAPHS |
| 770 | c = do_digraph(c); |
| 771 | #endif |
| 772 | |
| 773 | #ifdef FEAT_INS_EXPAND |
| 774 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 775 | goto docomplete; |
| 776 | #endif |
| 777 | if (c == Ctrl_V || c == Ctrl_Q) |
| 778 | { |
| 779 | ins_ctrl_v(); |
| 780 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 781 | continue; |
| 782 | } |
| 783 | |
| 784 | #ifdef FEAT_CINDENT |
| 785 | if (cindent_on() |
| 786 | # ifdef FEAT_INS_EXPAND |
| 787 | && ctrl_x_mode == 0 |
| 788 | # endif |
| 789 | ) |
| 790 | { |
| 791 | /* A key name preceded by a bang means this key is not to be |
| 792 | * inserted. Skip ahead to the re-indenting below. |
| 793 | * A key name preceded by a star means that indenting has to be |
| 794 | * done before inserting the key. */ |
| 795 | line_is_white = inindent(0); |
| 796 | if (in_cinkeys(c, '!', line_is_white)) |
| 797 | goto force_cindent; |
| 798 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 799 | && stop_arrow() == OK) |
| 800 | do_c_expr_indent(); |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | #ifdef FEAT_RIGHTLEFT |
| 805 | if (curwin->w_p_rl) |
| 806 | switch (c) |
| 807 | { |
| 808 | case K_LEFT: c = K_RIGHT; break; |
| 809 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 810 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 811 | case K_RIGHT: c = K_LEFT; break; |
| 812 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 813 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 814 | } |
| 815 | #endif |
| 816 | |
| 817 | #ifdef FEAT_VISUAL |
| 818 | /* |
| 819 | * If 'keymodel' contains "startsel", may start selection. If it |
| 820 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 821 | * characters. |
| 822 | */ |
| 823 | if (ins_start_select(c)) |
| 824 | continue; |
| 825 | #endif |
| 826 | |
| 827 | /* |
| 828 | * The big switch to handle a character in insert mode. |
| 829 | */ |
| 830 | switch (c) |
| 831 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 832 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 833 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 834 | break; |
| 835 | /*FALLTHROUGH*/ |
| 836 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 837 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 838 | #ifdef FEAT_CMDWIN |
| 839 | if (c == Ctrl_C && cmdwin_type != 0) |
| 840 | { |
| 841 | /* Close the cmdline window. */ |
| 842 | cmdwin_result = K_IGNORE; |
| 843 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
| 844 | goto doESCkey; |
| 845 | } |
| 846 | #endif |
| 847 | |
| 848 | #ifdef UNIX |
| 849 | do_intr: |
| 850 | #endif |
| 851 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 852 | * Insert mode */ |
| 853 | if (goto_im()) |
| 854 | { |
| 855 | if (got_int) |
| 856 | { |
| 857 | (void)vgetc(); /* flush all buffers */ |
| 858 | got_int = FALSE; |
| 859 | } |
| 860 | else |
| 861 | vim_beep(); |
| 862 | break; |
| 863 | } |
| 864 | doESCkey: |
| 865 | /* |
| 866 | * This is the ONLY return from edit()! |
| 867 | */ |
| 868 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 869 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 870 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 871 | o_lnum = curwin->w_cursor.lnum; |
| 872 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 873 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 874 | { |
| 875 | #ifdef FEAT_AUTOCMD |
| 876 | if (cmdchar != 'r' && cmdchar != 'v') |
| 877 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 878 | FALSE, curbuf); |
| 879 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 880 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 881 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 882 | continue; |
| 883 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 884 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 885 | if (!p_im) |
| 886 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 887 | stuffReadbuff((char_u *)":st\r"); |
| 888 | c = Ctrl_O; |
| 889 | /*FALLTHROUGH*/ |
| 890 | |
| 891 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 892 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 893 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 894 | goto docomplete; |
| 895 | #endif |
| 896 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 897 | break; |
| 898 | ins_ctrl_o(); |
| 899 | count = 0; |
| 900 | goto doESCkey; |
| 901 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 902 | case K_INS: /* toggle insert/replace mode */ |
| 903 | case K_KINS: |
| 904 | ins_insert(replaceState); |
| 905 | break; |
| 906 | |
| 907 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 908 | break; |
| 909 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 910 | #ifdef FEAT_SNIFF |
| 911 | case K_SNIFF: /* Sniff command received */ |
| 912 | stuffcharReadbuff(K_SNIFF); |
| 913 | goto doESCkey; |
| 914 | #endif |
| 915 | |
| 916 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 917 | case K_F1: |
| 918 | case K_XF1: |
| 919 | stuffcharReadbuff(K_HELP); |
| 920 | if (p_im) |
| 921 | need_start_insertmode = TRUE; |
| 922 | goto doESCkey; |
| 923 | |
| 924 | #ifdef FEAT_NETBEANS_INTG |
| 925 | case K_F21: /* NetBeans command */ |
| 926 | ++no_mapping; /* don't map the next key hits */ |
| 927 | i = safe_vgetc(); |
| 928 | --no_mapping; |
| 929 | netbeans_keycommand(i); |
| 930 | break; |
| 931 | #endif |
| 932 | |
| 933 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 934 | case NUL: |
| 935 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 936 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 937 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 938 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 939 | && c != Ctrl_A && !p_im) |
| 940 | goto doESCkey; /* quit insert mode */ |
| 941 | inserted_space = FALSE; |
| 942 | break; |
| 943 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 944 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 945 | ins_reg(); |
| 946 | auto_format(FALSE, TRUE); |
| 947 | inserted_space = FALSE; |
| 948 | break; |
| 949 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 950 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 951 | ins_ctrl_g(); |
| 952 | break; |
| 953 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 954 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 955 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 956 | break; |
| 957 | |
| 958 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 959 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 960 | if (!p_ari) |
| 961 | goto normalchar; |
| 962 | ins_ctrl_(); |
| 963 | break; |
| 964 | #endif |
| 965 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 966 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 968 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 969 | goto docomplete; |
| 970 | #endif |
| 971 | /* FALLTHROUGH */ |
| 972 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 973 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | # ifdef FEAT_INS_EXPAND |
| 975 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 976 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 977 | if (has_compl_option(FALSE)) |
| 978 | goto docomplete; |
| 979 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 980 | } |
| 981 | # endif |
| 982 | ins_shift(c, lastc); |
| 983 | auto_format(FALSE, TRUE); |
| 984 | inserted_space = FALSE; |
| 985 | break; |
| 986 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 987 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 988 | case K_KDEL: |
| 989 | ins_del(); |
| 990 | auto_format(FALSE, TRUE); |
| 991 | break; |
| 992 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 993 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 994 | case Ctrl_H: |
| 995 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 996 | auto_format(FALSE, TRUE); |
| 997 | break; |
| 998 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 999 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1000 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 1001 | auto_format(FALSE, TRUE); |
| 1002 | break; |
| 1003 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1004 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1005 | # ifdef FEAT_COMPL_FUNC |
| 1006 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1007 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1008 | goto docomplete; |
| 1009 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 1011 | auto_format(FALSE, TRUE); |
| 1012 | inserted_space = FALSE; |
| 1013 | break; |
| 1014 | |
| 1015 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1016 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1017 | case K_LEFTMOUSE_NM: |
| 1018 | case K_LEFTDRAG: |
| 1019 | case K_LEFTRELEASE: |
| 1020 | case K_LEFTRELEASE_NM: |
| 1021 | case K_MIDDLEMOUSE: |
| 1022 | case K_MIDDLEDRAG: |
| 1023 | case K_MIDDLERELEASE: |
| 1024 | case K_RIGHTMOUSE: |
| 1025 | case K_RIGHTDRAG: |
| 1026 | case K_RIGHTRELEASE: |
| 1027 | case K_X1MOUSE: |
| 1028 | case K_X1DRAG: |
| 1029 | case K_X1RELEASE: |
| 1030 | case K_X2MOUSE: |
| 1031 | case K_X2DRAG: |
| 1032 | case K_X2RELEASE: |
| 1033 | ins_mouse(c); |
| 1034 | break; |
| 1035 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1036 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | ins_mousescroll(FALSE); |
| 1038 | break; |
| 1039 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1040 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1041 | ins_mousescroll(TRUE); |
| 1042 | break; |
| 1043 | #endif |
| 1044 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1045 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1046 | break; |
| 1047 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1048 | #ifdef FEAT_AUTOCMD |
| 1049 | case K_CURSORHOLD: /* Didn't type something for a while. */ |
| 1050 | apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); |
| 1051 | did_cursorhold = TRUE; |
| 1052 | break; |
| 1053 | #endif |
| 1054 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1055 | #ifdef FEAT_GUI_W32 |
| 1056 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1057 | * cancelled. */ |
| 1058 | case K_F4: |
| 1059 | if (mod_mask != MOD_MASK_ALT) |
| 1060 | goto normalchar; |
| 1061 | break; |
| 1062 | #endif |
| 1063 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1064 | #ifdef FEAT_GUI |
| 1065 | case K_VER_SCROLLBAR: |
| 1066 | ins_scroll(); |
| 1067 | break; |
| 1068 | |
| 1069 | case K_HOR_SCROLLBAR: |
| 1070 | ins_horscroll(); |
| 1071 | break; |
| 1072 | #endif |
| 1073 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1074 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1075 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1076 | case K_S_HOME: |
| 1077 | case K_C_HOME: |
| 1078 | ins_home(c); |
| 1079 | break; |
| 1080 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1081 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1082 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | case K_S_END: |
| 1084 | case K_C_END: |
| 1085 | ins_end(c); |
| 1086 | break; |
| 1087 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1088 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1089 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1090 | ins_s_left(); |
| 1091 | else |
| 1092 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1093 | break; |
| 1094 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1095 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | case K_C_LEFT: |
| 1097 | ins_s_left(); |
| 1098 | break; |
| 1099 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1100 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1101 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1102 | ins_s_right(); |
| 1103 | else |
| 1104 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1107 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | case K_C_RIGHT: |
| 1109 | ins_s_right(); |
| 1110 | break; |
| 1111 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1112 | case K_UP: /* <Up> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1113 | #ifdef FEAT_INS_EXPAND |
| 1114 | if (pum_visible()) |
| 1115 | goto docomplete; |
| 1116 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1117 | if (mod_mask & MOD_MASK_SHIFT) |
| 1118 | ins_pageup(); |
| 1119 | else |
| 1120 | ins_up(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1121 | break; |
| 1122 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1123 | case K_S_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1124 | case K_PAGEUP: |
| 1125 | case K_KPAGEUP: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1126 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1127 | if (pum_visible()) |
| 1128 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1129 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1130 | ins_pageup(); |
| 1131 | break; |
| 1132 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1133 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1134 | #ifdef FEAT_INS_EXPAND |
| 1135 | if (pum_visible()) |
| 1136 | goto docomplete; |
| 1137 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1138 | if (mod_mask & MOD_MASK_SHIFT) |
| 1139 | ins_pagedown(); |
| 1140 | else |
| 1141 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1142 | break; |
| 1143 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1144 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1145 | case K_PAGEDOWN: |
| 1146 | case K_KPAGEDOWN: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1147 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1148 | if (pum_visible()) |
| 1149 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1150 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | ins_pagedown(); |
| 1152 | break; |
| 1153 | |
| 1154 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1155 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1156 | ins_drop(); |
| 1157 | break; |
| 1158 | #endif |
| 1159 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1160 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | c = TAB; |
| 1162 | /* FALLTHROUGH */ |
| 1163 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1164 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1165 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1166 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1167 | goto docomplete; |
| 1168 | #endif |
| 1169 | inserted_space = FALSE; |
| 1170 | if (ins_tab()) |
| 1171 | goto normalchar; /* insert TAB as a normal char */ |
| 1172 | auto_format(FALSE, TRUE); |
| 1173 | break; |
| 1174 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1175 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1176 | c = CAR; |
| 1177 | /* FALLTHROUGH */ |
| 1178 | case CAR: |
| 1179 | case NL: |
| 1180 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1181 | /* In a quickfix window a <CR> jumps to the error under the |
| 1182 | * cursor. */ |
| 1183 | if (bt_quickfix(curbuf) && c == CAR) |
| 1184 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1185 | if (curwin->w_llist_ref == NULL) /* quickfix window */ |
| 1186 | do_cmdline_cmd((char_u *)".cc"); |
| 1187 | else /* location list window */ |
| 1188 | do_cmdline_cmd((char_u *)".ll"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | break; |
| 1190 | } |
| 1191 | #endif |
| 1192 | #ifdef FEAT_CMDWIN |
| 1193 | if (cmdwin_type != 0) |
| 1194 | { |
| 1195 | /* Execute the command in the cmdline window. */ |
| 1196 | cmdwin_result = CAR; |
| 1197 | goto doESCkey; |
| 1198 | } |
| 1199 | #endif |
| 1200 | if (ins_eol(c) && !p_im) |
| 1201 | goto doESCkey; /* out of memory */ |
| 1202 | auto_format(FALSE, FALSE); |
| 1203 | inserted_space = FALSE; |
| 1204 | break; |
| 1205 | |
| 1206 | #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1207 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1208 | # ifdef FEAT_INS_EXPAND |
| 1209 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1210 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1211 | if (has_compl_option(TRUE)) |
| 1212 | goto docomplete; |
| 1213 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1214 | } |
| 1215 | # endif |
| 1216 | # ifdef FEAT_DIGRAPHS |
| 1217 | c = ins_digraph(); |
| 1218 | if (c == NUL) |
| 1219 | break; |
| 1220 | # endif |
| 1221 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1222 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | |
| 1224 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1225 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1226 | ins_ctrl_x(); |
| 1227 | break; |
| 1228 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1229 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1231 | goto normalchar; |
| 1232 | goto docomplete; |
| 1233 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1234 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1235 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1236 | goto normalchar; |
| 1237 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1238 | |
| 1239 | case 's': /* Spelling completion after ^X */ |
| 1240 | case Ctrl_S: |
| 1241 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1242 | goto normalchar; |
| 1243 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1244 | #endif |
| 1245 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1246 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | #ifdef FEAT_INS_EXPAND |
| 1248 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1249 | #endif |
| 1250 | { |
| 1251 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1252 | if (p_im) |
| 1253 | { |
| 1254 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1255 | break; |
| 1256 | goto doESCkey; |
| 1257 | } |
| 1258 | goto normalchar; |
| 1259 | } |
| 1260 | #ifdef FEAT_INS_EXPAND |
| 1261 | /* FALLTHROUGH */ |
| 1262 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1263 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | case Ctrl_N: |
| 1265 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1266 | * but it is under other ^X modes */ |
| 1267 | if (*curbuf->b_p_cpt == NUL |
| 1268 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1269 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1270 | goto normalchar; |
| 1271 | |
| 1272 | docomplete: |
| 1273 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1274 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | break; |
| 1276 | #endif /* FEAT_INS_EXPAND */ |
| 1277 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1278 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1279 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1280 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1281 | break; |
| 1282 | |
| 1283 | default: |
| 1284 | #ifdef UNIX |
| 1285 | if (c == intr_char) /* special interrupt char */ |
| 1286 | goto do_intr; |
| 1287 | #endif |
| 1288 | |
| 1289 | /* |
| 1290 | * Insert a nomal character. |
| 1291 | */ |
| 1292 | normalchar: |
| 1293 | #ifdef FEAT_SMARTINDENT |
| 1294 | /* Try to perform smart-indenting. */ |
| 1295 | ins_try_si(c); |
| 1296 | #endif |
| 1297 | |
| 1298 | if (c == ' ') |
| 1299 | { |
| 1300 | inserted_space = TRUE; |
| 1301 | #ifdef FEAT_CINDENT |
| 1302 | if (inindent(0)) |
| 1303 | can_cindent = FALSE; |
| 1304 | #endif |
| 1305 | if (Insstart_blank_vcol == MAXCOL |
| 1306 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1307 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1308 | } |
| 1309 | |
| 1310 | if (vim_iswordc(c) || !echeck_abbr( |
| 1311 | #ifdef FEAT_MBYTE |
| 1312 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1313 | * what check_abbr() expects. */ |
| 1314 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1315 | #endif |
| 1316 | c)) |
| 1317 | { |
| 1318 | insert_special(c, FALSE, FALSE); |
| 1319 | #ifdef FEAT_RIGHTLEFT |
| 1320 | revins_legal++; |
| 1321 | revins_chars++; |
| 1322 | #endif |
| 1323 | } |
| 1324 | |
| 1325 | auto_format(FALSE, TRUE); |
| 1326 | |
| 1327 | #ifdef FEAT_FOLDING |
| 1328 | /* When inserting a character the cursor line must never be in a |
| 1329 | * closed fold. */ |
| 1330 | foldOpenCursor(); |
| 1331 | #endif |
| 1332 | break; |
| 1333 | } /* end of switch (c) */ |
| 1334 | |
| 1335 | /* If the cursor was moved we didn't just insert a space */ |
| 1336 | if (arrow_used) |
| 1337 | inserted_space = FALSE; |
| 1338 | |
| 1339 | #ifdef FEAT_CINDENT |
| 1340 | if (can_cindent && cindent_on() |
| 1341 | # ifdef FEAT_INS_EXPAND |
| 1342 | && ctrl_x_mode == 0 |
| 1343 | # endif |
| 1344 | ) |
| 1345 | { |
| 1346 | force_cindent: |
| 1347 | /* |
| 1348 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1349 | */ |
| 1350 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1351 | { |
| 1352 | if (stop_arrow() == OK) |
| 1353 | /* re-indent the current line */ |
| 1354 | do_c_expr_indent(); |
| 1355 | } |
| 1356 | } |
| 1357 | #endif /* FEAT_CINDENT */ |
| 1358 | |
| 1359 | } /* for (;;) */ |
| 1360 | /* NOTREACHED */ |
| 1361 | } |
| 1362 | |
| 1363 | /* |
| 1364 | * Redraw for Insert mode. |
| 1365 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1366 | * option work correctly. |
| 1367 | * Only redraw when there are no characters available. This speeds up |
| 1368 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1369 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1370 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | static void |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1372 | ins_redraw(ready) |
| 1373 | int ready; /* not busy with something */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1374 | { |
| 1375 | if (!char_avail()) |
| 1376 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1377 | #ifdef FEAT_AUTOCMD |
| 1378 | /* Trigger CursorMoved if the cursor moved. */ |
| 1379 | if (ready && has_cursormovedI() |
| 1380 | && !equalpos(last_cursormoved, curwin->w_cursor)) |
| 1381 | { |
| 1382 | apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); |
| 1383 | last_cursormoved = curwin->w_cursor; |
| 1384 | } |
| 1385 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | if (must_redraw) |
| 1387 | update_screen(0); |
| 1388 | else if (clear_cmdline || redraw_cmdline) |
| 1389 | showmode(); /* clear cmdline and show mode */ |
| 1390 | showruler(FALSE); |
| 1391 | setcursor(); |
| 1392 | emsg_on_display = FALSE; /* may remove error message now */ |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | /* |
| 1397 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1398 | */ |
| 1399 | static void |
| 1400 | ins_ctrl_v() |
| 1401 | { |
| 1402 | int c; |
| 1403 | |
| 1404 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1405 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1406 | |
| 1407 | if (redrawing() && !char_avail()) |
| 1408 | edit_putchar('^', TRUE); |
| 1409 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1410 | |
| 1411 | #ifdef FEAT_CMDL_INFO |
| 1412 | add_to_showcmd_c(Ctrl_V); |
| 1413 | #endif |
| 1414 | |
| 1415 | c = get_literal(); |
| 1416 | #ifdef FEAT_CMDL_INFO |
| 1417 | clear_showcmd(); |
| 1418 | #endif |
| 1419 | insert_special(c, FALSE, TRUE); |
| 1420 | #ifdef FEAT_RIGHTLEFT |
| 1421 | revins_chars++; |
| 1422 | revins_legal++; |
| 1423 | #endif |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1428 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1429 | */ |
| 1430 | static int pc_status; |
| 1431 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1432 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1433 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1434 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
| 1435 | #ifdef FEAT_MBYTE |
| 1436 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
| 1437 | #else |
| 1438 | static char_u pc_bytes[2]; /* saved bytes */ |
| 1439 | #endif |
| 1440 | static int pc_attr; |
| 1441 | static int pc_row; |
| 1442 | static int pc_col; |
| 1443 | |
| 1444 | void |
| 1445 | edit_putchar(c, highlight) |
| 1446 | int c; |
| 1447 | int highlight; |
| 1448 | { |
| 1449 | int attr; |
| 1450 | |
| 1451 | if (ScreenLines != NULL) |
| 1452 | { |
| 1453 | update_topline(); /* just in case w_topline isn't valid */ |
| 1454 | validate_cursor(); |
| 1455 | if (highlight) |
| 1456 | attr = hl_attr(HLF_8); |
| 1457 | else |
| 1458 | attr = 0; |
| 1459 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1460 | pc_col = W_WINCOL(curwin); |
| 1461 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1462 | pc_status = PC_STATUS_UNSET; |
| 1463 | #endif |
| 1464 | #ifdef FEAT_RIGHTLEFT |
| 1465 | if (curwin->w_p_rl) |
| 1466 | { |
| 1467 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1468 | # ifdef FEAT_MBYTE |
| 1469 | if (has_mbyte) |
| 1470 | { |
| 1471 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1472 | |
| 1473 | if (fix_col != pc_col) |
| 1474 | { |
| 1475 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1476 | --curwin->w_wcol; |
| 1477 | pc_status = PC_STATUS_RIGHT; |
| 1478 | } |
| 1479 | } |
| 1480 | # endif |
| 1481 | } |
| 1482 | else |
| 1483 | #endif |
| 1484 | { |
| 1485 | pc_col += curwin->w_wcol; |
| 1486 | #ifdef FEAT_MBYTE |
| 1487 | if (mb_lefthalve(pc_row, pc_col)) |
| 1488 | pc_status = PC_STATUS_LEFT; |
| 1489 | #endif |
| 1490 | } |
| 1491 | |
| 1492 | /* save the character to be able to put it back */ |
| 1493 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1494 | if (pc_status == PC_STATUS_UNSET) |
| 1495 | #endif |
| 1496 | { |
| 1497 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1498 | pc_status = PC_STATUS_SET; |
| 1499 | } |
| 1500 | screen_putchar(c, pc_row, pc_col, attr); |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * Undo the previous edit_putchar(). |
| 1506 | */ |
| 1507 | void |
| 1508 | edit_unputchar() |
| 1509 | { |
| 1510 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1511 | { |
| 1512 | #if defined(FEAT_MBYTE) |
| 1513 | if (pc_status == PC_STATUS_RIGHT) |
| 1514 | ++curwin->w_wcol; |
| 1515 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1516 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1517 | else |
| 1518 | #endif |
| 1519 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* |
| 1524 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1525 | * Only works when cursor is in the line that changes. |
| 1526 | */ |
| 1527 | void |
| 1528 | display_dollar(col) |
| 1529 | colnr_T col; |
| 1530 | { |
| 1531 | colnr_T save_col; |
| 1532 | |
| 1533 | if (!redrawing()) |
| 1534 | return; |
| 1535 | |
| 1536 | cursor_off(); |
| 1537 | save_col = curwin->w_cursor.col; |
| 1538 | curwin->w_cursor.col = col; |
| 1539 | #ifdef FEAT_MBYTE |
| 1540 | if (has_mbyte) |
| 1541 | { |
| 1542 | char_u *p; |
| 1543 | |
| 1544 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1545 | p = ml_get_curline(); |
| 1546 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1547 | } |
| 1548 | #endif |
| 1549 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1550 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1551 | { |
| 1552 | edit_putchar('$', FALSE); |
| 1553 | dollar_vcol = curwin->w_virtcol; |
| 1554 | } |
| 1555 | curwin->w_cursor.col = save_col; |
| 1556 | } |
| 1557 | |
| 1558 | /* |
| 1559 | * Call this function before moving the cursor from the normal insert position |
| 1560 | * in insert mode. |
| 1561 | */ |
| 1562 | static void |
| 1563 | undisplay_dollar() |
| 1564 | { |
| 1565 | if (dollar_vcol) |
| 1566 | { |
| 1567 | dollar_vcol = 0; |
| 1568 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1569 | } |
| 1570 | } |
| 1571 | |
| 1572 | /* |
| 1573 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1574 | * Keep the cursor on the same character. |
| 1575 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1576 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1577 | * type == INDENT_SET set indent to "amount" |
| 1578 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1579 | */ |
| 1580 | void |
| 1581 | change_indent(type, amount, round, replaced) |
| 1582 | int type; |
| 1583 | int amount; |
| 1584 | int round; |
| 1585 | int replaced; /* replaced character, put on replace stack */ |
| 1586 | { |
| 1587 | int vcol; |
| 1588 | int last_vcol; |
| 1589 | int insstart_less; /* reduction for Insstart.col */ |
| 1590 | int new_cursor_col; |
| 1591 | int i; |
| 1592 | char_u *ptr; |
| 1593 | int save_p_list; |
| 1594 | int start_col; |
| 1595 | colnr_T vc; |
| 1596 | #ifdef FEAT_VREPLACE |
| 1597 | colnr_T orig_col = 0; /* init for GCC */ |
| 1598 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1599 | |
| 1600 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1601 | if (State & VREPLACE_FLAG) |
| 1602 | { |
| 1603 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1604 | orig_col = curwin->w_cursor.col; |
| 1605 | } |
| 1606 | #endif |
| 1607 | |
| 1608 | /* for the following tricks we don't want list mode */ |
| 1609 | save_p_list = curwin->w_p_list; |
| 1610 | curwin->w_p_list = FALSE; |
| 1611 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1612 | vcol = vc; |
| 1613 | |
| 1614 | /* |
| 1615 | * For Replace mode we need to fix the replace stack later, which is only |
| 1616 | * possible when the cursor is in the indent. Remember the number of |
| 1617 | * characters before the cursor if it's possible. |
| 1618 | */ |
| 1619 | start_col = curwin->w_cursor.col; |
| 1620 | |
| 1621 | /* determine offset from first non-blank */ |
| 1622 | new_cursor_col = curwin->w_cursor.col; |
| 1623 | beginline(BL_WHITE); |
| 1624 | new_cursor_col -= curwin->w_cursor.col; |
| 1625 | |
| 1626 | insstart_less = curwin->w_cursor.col; |
| 1627 | |
| 1628 | /* |
| 1629 | * If the cursor is in the indent, compute how many screen columns the |
| 1630 | * cursor is to the left of the first non-blank. |
| 1631 | */ |
| 1632 | if (new_cursor_col < 0) |
| 1633 | vcol = get_indent() - vcol; |
| 1634 | |
| 1635 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1636 | start_col = -1; |
| 1637 | |
| 1638 | /* |
| 1639 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1640 | */ |
| 1641 | if (type == INDENT_SET) |
| 1642 | (void)set_indent(amount, SIN_CHANGED); |
| 1643 | else |
| 1644 | { |
| 1645 | #ifdef FEAT_VREPLACE |
| 1646 | int save_State = State; |
| 1647 | |
| 1648 | /* Avoid being called recursively. */ |
| 1649 | if (State & VREPLACE_FLAG) |
| 1650 | State = INSERT; |
| 1651 | #endif |
| 1652 | shift_line(type == INDENT_DEC, round, 1); |
| 1653 | #ifdef FEAT_VREPLACE |
| 1654 | State = save_State; |
| 1655 | #endif |
| 1656 | } |
| 1657 | insstart_less -= curwin->w_cursor.col; |
| 1658 | |
| 1659 | /* |
| 1660 | * Try to put cursor on same character. |
| 1661 | * If the cursor is at or after the first non-blank in the line, |
| 1662 | * compute the cursor column relative to the column of the first |
| 1663 | * non-blank character. |
| 1664 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1665 | * If the cursor is before the first non-blank, position it relative |
| 1666 | * to the first non-blank, counted in screen columns. |
| 1667 | */ |
| 1668 | if (new_cursor_col >= 0) |
| 1669 | { |
| 1670 | /* |
| 1671 | * When changing the indent while the cursor is touching it, reset |
| 1672 | * Insstart_col to 0. |
| 1673 | */ |
| 1674 | if (new_cursor_col == 0) |
| 1675 | insstart_less = MAXCOL; |
| 1676 | new_cursor_col += curwin->w_cursor.col; |
| 1677 | } |
| 1678 | else if (!(State & INSERT)) |
| 1679 | new_cursor_col = curwin->w_cursor.col; |
| 1680 | else |
| 1681 | { |
| 1682 | /* |
| 1683 | * Compute the screen column where the cursor should be. |
| 1684 | */ |
| 1685 | vcol = get_indent() - vcol; |
| 1686 | curwin->w_virtcol = (vcol < 0) ? 0 : vcol; |
| 1687 | |
| 1688 | /* |
| 1689 | * Advance the cursor until we reach the right screen column. |
| 1690 | */ |
| 1691 | vcol = last_vcol = 0; |
| 1692 | new_cursor_col = -1; |
| 1693 | ptr = ml_get_curline(); |
| 1694 | while (vcol <= (int)curwin->w_virtcol) |
| 1695 | { |
| 1696 | last_vcol = vcol; |
| 1697 | #ifdef FEAT_MBYTE |
| 1698 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1699 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1700 | else |
| 1701 | #endif |
| 1702 | ++new_cursor_col; |
| 1703 | vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol); |
| 1704 | } |
| 1705 | vcol = last_vcol; |
| 1706 | |
| 1707 | /* |
| 1708 | * May need to insert spaces to be able to position the cursor on |
| 1709 | * the right screen column. |
| 1710 | */ |
| 1711 | if (vcol != (int)curwin->w_virtcol) |
| 1712 | { |
| 1713 | curwin->w_cursor.col = new_cursor_col; |
| 1714 | i = (int)curwin->w_virtcol - vcol; |
| 1715 | ptr = alloc(i + 1); |
| 1716 | if (ptr != NULL) |
| 1717 | { |
| 1718 | new_cursor_col += i; |
| 1719 | ptr[i] = NUL; |
| 1720 | while (--i >= 0) |
| 1721 | ptr[i] = ' '; |
| 1722 | ins_str(ptr); |
| 1723 | vim_free(ptr); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * When changing the indent while the cursor is in it, reset |
| 1729 | * Insstart_col to 0. |
| 1730 | */ |
| 1731 | insstart_less = MAXCOL; |
| 1732 | } |
| 1733 | |
| 1734 | curwin->w_p_list = save_p_list; |
| 1735 | |
| 1736 | if (new_cursor_col <= 0) |
| 1737 | curwin->w_cursor.col = 0; |
| 1738 | else |
| 1739 | curwin->w_cursor.col = new_cursor_col; |
| 1740 | curwin->w_set_curswant = TRUE; |
| 1741 | changed_cline_bef_curs(); |
| 1742 | |
| 1743 | /* |
| 1744 | * May have to adjust the start of the insert. |
| 1745 | */ |
| 1746 | if (State & INSERT) |
| 1747 | { |
| 1748 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1749 | { |
| 1750 | if ((int)Insstart.col <= insstart_less) |
| 1751 | Insstart.col = 0; |
| 1752 | else |
| 1753 | Insstart.col -= insstart_less; |
| 1754 | } |
| 1755 | if ((int)ai_col <= insstart_less) |
| 1756 | ai_col = 0; |
| 1757 | else |
| 1758 | ai_col -= insstart_less; |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 1763 | * If the number of characters before the cursor decreased, need to pop a |
| 1764 | * few characters from the replace stack. |
| 1765 | * If the number of characters before the cursor increased, need to push a |
| 1766 | * few NULs onto the replace stack. |
| 1767 | */ |
| 1768 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1769 | { |
| 1770 | while (start_col > (int)curwin->w_cursor.col) |
| 1771 | { |
| 1772 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1773 | --start_col; |
| 1774 | } |
| 1775 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1776 | { |
| 1777 | replace_push(NUL); |
| 1778 | if (replaced) |
| 1779 | { |
| 1780 | replace_push(replaced); |
| 1781 | replaced = NUL; |
| 1782 | } |
| 1783 | ++start_col; |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | #ifdef FEAT_VREPLACE |
| 1788 | /* |
| 1789 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 1790 | * it is always possible because we backspace over the whole line and then |
| 1791 | * put it back again the way we wanted it. |
| 1792 | */ |
| 1793 | if (State & VREPLACE_FLAG) |
| 1794 | { |
| 1795 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 1796 | * even if you can't backspace. */ |
| 1797 | if (orig_line == NULL) |
| 1798 | return; |
| 1799 | |
| 1800 | /* Save new line */ |
| 1801 | new_line = vim_strsave(ml_get_curline()); |
| 1802 | if (new_line == NULL) |
| 1803 | return; |
| 1804 | |
| 1805 | /* We only put back the new line up to the cursor */ |
| 1806 | new_line[curwin->w_cursor.col] = NUL; |
| 1807 | |
| 1808 | /* Put back original line */ |
| 1809 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 1810 | curwin->w_cursor.col = orig_col; |
| 1811 | |
| 1812 | /* Backspace from cursor to start of line */ |
| 1813 | backspace_until_column(0); |
| 1814 | |
| 1815 | /* Insert new stuff into line again */ |
| 1816 | ins_bytes(new_line); |
| 1817 | |
| 1818 | vim_free(new_line); |
| 1819 | } |
| 1820 | #endif |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * Truncate the space at the end of a line. This is to be used only in an |
| 1825 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 1826 | * modes. |
| 1827 | */ |
| 1828 | void |
| 1829 | truncate_spaces(line) |
| 1830 | char_u *line; |
| 1831 | { |
| 1832 | int i; |
| 1833 | |
| 1834 | /* find start of trailing white space */ |
| 1835 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 1836 | { |
| 1837 | if (State & REPLACE_FLAG) |
| 1838 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1839 | } |
| 1840 | line[i + 1] = NUL; |
| 1841 | } |
| 1842 | |
| 1843 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 1844 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 1845 | /* |
| 1846 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 1847 | * modes correctly. May also be used when not in insert mode at all. |
| 1848 | */ |
| 1849 | void |
| 1850 | backspace_until_column(col) |
| 1851 | int col; |
| 1852 | { |
| 1853 | while ((int)curwin->w_cursor.col > col) |
| 1854 | { |
| 1855 | curwin->w_cursor.col--; |
| 1856 | if (State & REPLACE_FLAG) |
| 1857 | replace_do_bs(); |
| 1858 | else |
| 1859 | (void)del_char(FALSE); |
| 1860 | } |
| 1861 | } |
| 1862 | #endif |
| 1863 | |
| 1864 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 1865 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1866 | * CTRL-X pressed in Insert mode. |
| 1867 | */ |
| 1868 | static void |
| 1869 | ins_ctrl_x() |
| 1870 | { |
| 1871 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 1872 | * CTRL-V works like CTRL-N */ |
| 1873 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 1874 | { |
| 1875 | /* if the next ^X<> won't ADD nothing, then reset |
| 1876 | * compl_cont_status */ |
| 1877 | if (compl_cont_status & CONT_N_ADDS) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1878 | compl_cont_status |= CONT_INTRPT; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1879 | else |
| 1880 | compl_cont_status = 0; |
| 1881 | /* We're not sure which CTRL-X mode it will be yet */ |
| 1882 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 1883 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 1884 | edit_submode_pre = NULL; |
| 1885 | showmode(); |
| 1886 | } |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 1891 | */ |
| 1892 | static int |
| 1893 | has_compl_option(dict_opt) |
| 1894 | int dict_opt; |
| 1895 | { |
| 1896 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL) |
| 1897 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 1898 | { |
| 1899 | ctrl_x_mode = 0; |
| 1900 | edit_submode = NULL; |
| 1901 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 1902 | : (char_u *)_("'thesaurus' option is empty"), |
| 1903 | hl_attr(HLF_E)); |
| 1904 | if (emsg_silent == 0) |
| 1905 | { |
| 1906 | vim_beep(); |
| 1907 | setcursor(); |
| 1908 | out_flush(); |
| 1909 | ui_delay(2000L, FALSE); |
| 1910 | } |
| 1911 | return FALSE; |
| 1912 | } |
| 1913 | return TRUE; |
| 1914 | } |
| 1915 | |
| 1916 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1917 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 1918 | * This depends on the current mode. |
| 1919 | */ |
| 1920 | int |
| 1921 | vim_is_ctrl_x_key(c) |
| 1922 | int c; |
| 1923 | { |
| 1924 | /* Always allow ^R - let it's results then be checked */ |
| 1925 | if (c == Ctrl_R) |
| 1926 | return TRUE; |
| 1927 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1928 | /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1929 | if (ins_compl_pum_key(c)) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1930 | return TRUE; |
| 1931 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | switch (ctrl_x_mode) |
| 1933 | { |
| 1934 | case 0: /* Not in any CTRL-X mode */ |
| 1935 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 1936 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1937 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1938 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 1939 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 1940 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1941 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
| 1942 | || c == Ctrl_S || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1943 | case CTRL_X_SCROLL: |
| 1944 | return (c == Ctrl_Y || c == Ctrl_E); |
| 1945 | case CTRL_X_WHOLE_LINE: |
| 1946 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 1947 | case CTRL_X_FILES: |
| 1948 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 1949 | case CTRL_X_DICTIONARY: |
| 1950 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 1951 | case CTRL_X_THESAURUS: |
| 1952 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 1953 | case CTRL_X_TAGS: |
| 1954 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 1955 | #ifdef FEAT_FIND_ID |
| 1956 | case CTRL_X_PATH_PATTERNS: |
| 1957 | return (c == Ctrl_P || c == Ctrl_N); |
| 1958 | case CTRL_X_PATH_DEFINES: |
| 1959 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 1960 | #endif |
| 1961 | case CTRL_X_CMDLINE: |
| 1962 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 1963 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1964 | #ifdef FEAT_COMPL_FUNC |
| 1965 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1966 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 1967 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1968 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1969 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1970 | case CTRL_X_SPELL: |
| 1971 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1972 | } |
| 1973 | EMSG(_(e_internal)); |
| 1974 | return FALSE; |
| 1975 | } |
| 1976 | |
| 1977 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 1978 | * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1979 | * case of the originally typed text is used, and the case of the completed |
| 1980 | * text is infered, ie this tries to work out what case you probably wanted |
| 1981 | * the rest of the word to be in -- webb |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1982 | * TODO: make this work for multi-byte characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1983 | */ |
| 1984 | int |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1985 | ins_compl_add_infercase(str, len, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1986 | char_u *str; |
| 1987 | int len; |
| 1988 | char_u *fname; |
| 1989 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1990 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1991 | { |
| 1992 | int has_lower = FALSE; |
| 1993 | int was_letter = FALSE; |
| 1994 | int idx; |
| 1995 | |
| 1996 | if (p_ic && curbuf->b_p_inf && len < IOSIZE) |
| 1997 | { |
| 1998 | /* Infer case of completed part -- webb */ |
| 1999 | /* Use IObuff, str would change text in buffer! */ |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2000 | vim_strncpy(IObuff, str, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2001 | |
| 2002 | /* Rule 1: Were any chars converted to lower? */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2003 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2004 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2005 | if (islower(compl_orig_text[idx])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2006 | { |
| 2007 | has_lower = TRUE; |
| 2008 | if (isupper(IObuff[idx])) |
| 2009 | { |
| 2010 | /* Rule 1 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2011 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2012 | IObuff[idx] = TOLOWER_LOC(IObuff[idx]); |
| 2013 | break; |
| 2014 | } |
| 2015 | } |
| 2016 | } |
| 2017 | |
| 2018 | /* |
| 2019 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 2020 | * upper case. |
| 2021 | */ |
| 2022 | if (!has_lower) |
| 2023 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2024 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2025 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2026 | if (was_letter && isupper(compl_orig_text[idx]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2027 | && islower(IObuff[idx])) |
| 2028 | { |
| 2029 | /* Rule 2 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2030 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | IObuff[idx] = TOUPPER_LOC(IObuff[idx]); |
| 2032 | break; |
| 2033 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2034 | was_letter = isalpha(compl_orig_text[idx]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | /* Copy the original case of the part we typed */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2039 | STRNCPY(IObuff, compl_orig_text, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2041 | return ins_compl_add(IObuff, len, fname, NULL, dir, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2043 | return ins_compl_add(str, len, fname, NULL, dir, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * Add a match to the list of matches. |
| 2048 | * If the given string is already in the list of completions, then return |
| 2049 | * 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] | 2050 | * maybe because alloc() returns NULL, then RET_ERROR is returned -- webb. |
| 2051 | * |
| 2052 | * New: |
| 2053 | * If the given string is already in the list of completions, then return |
| 2054 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 2055 | * maybe because alloc() returns NULL, then FAIL is returned -- webb. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2056 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2057 | int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2058 | ins_compl_add(str, len, fname, extra, cdir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2059 | char_u *str; |
| 2060 | int len; |
| 2061 | char_u *fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2062 | char_u *extra; /* extra text for popup menu or NULL */ |
| 2063 | int cdir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2064 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2066 | compl_T *match; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2067 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | |
| 2069 | ui_breakcheck(); |
| 2070 | if (got_int) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2071 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2072 | if (len < 0) |
| 2073 | len = (int)STRLEN(str); |
| 2074 | |
| 2075 | /* |
| 2076 | * If the same match is already present, don't add it. |
| 2077 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2078 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2079 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2080 | match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | do |
| 2082 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2083 | if ( !(match->cp_flags & ORIGINAL_TEXT) |
| 2084 | && STRNCMP(match->cp_str, str, (size_t)len) == 0 |
| 2085 | && match->cp_str[len] == NUL) |
| 2086 | return NOTDONE; |
| 2087 | match = match->cp_next; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2088 | } while (match != NULL && match != compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2089 | } |
| 2090 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2091 | /* Remove any popup menu before changing the list of matches. */ |
| 2092 | ins_compl_del_pum(); |
| 2093 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2094 | /* |
| 2095 | * Allocate a new match structure. |
| 2096 | * Copy the values to the new match structure. |
| 2097 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2098 | match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2099 | if (match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2100 | return FAIL; |
| 2101 | match->cp_number = -1; |
| 2102 | if (flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2103 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2104 | match->cp_number = 0; |
| 2105 | match->cp_str = compl_orig_text; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2106 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2107 | else if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2108 | { |
| 2109 | vim_free(match); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2110 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2111 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2112 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2113 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2114 | * - 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] | 2115 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 2116 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2117 | if (fname != NULL |
| 2118 | && compl_curr_match |
| 2119 | && compl_curr_match->cp_fname != NULL |
| 2120 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2121 | match->cp_fname = compl_curr_match->cp_fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2122 | else if (fname != NULL) |
| 2123 | { |
| 2124 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2125 | flags |= FREE_FNAME; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2126 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2127 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2128 | match->cp_fname = NULL; |
| 2129 | match->cp_flags = flags; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2130 | if (extra != NULL) |
| 2131 | match->cp_extra = vim_strsave(extra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2132 | |
| 2133 | /* |
| 2134 | * Link the new match structure in the list of matches. |
| 2135 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2136 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2137 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2138 | else if (dir == FORWARD) |
| 2139 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2140 | match->cp_next = compl_curr_match->cp_next; |
| 2141 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | } |
| 2143 | else /* BACKWARD */ |
| 2144 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2145 | match->cp_next = compl_curr_match; |
| 2146 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2147 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2148 | if (match->cp_next) |
| 2149 | match->cp_next->cp_prev = match; |
| 2150 | if (match->cp_prev) |
| 2151 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2152 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2153 | compl_first_match = match; |
| 2154 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2155 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2156 | /* |
| 2157 | * Find the longest common string if still doing that. |
| 2158 | */ |
| 2159 | if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) |
| 2160 | ins_compl_longest_match(match); |
| 2161 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2162 | return OK; |
| 2163 | } |
| 2164 | |
| 2165 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2166 | * Reduce the longest common string for match "match". |
| 2167 | */ |
| 2168 | static void |
| 2169 | ins_compl_longest_match(match) |
| 2170 | compl_T *match; |
| 2171 | { |
| 2172 | char_u *p, *s; |
| 2173 | int l; |
| 2174 | int had_match; |
| 2175 | |
| 2176 | if (compl_leader == NULL) |
| 2177 | /* First match, use it as a whole. */ |
| 2178 | compl_leader = vim_strsave(match->cp_str); |
| 2179 | else |
| 2180 | { |
| 2181 | /* Reduce the text if this match differs from compl_leader. */ |
| 2182 | for (p = compl_leader, s = match->cp_str; *p != NUL; p += l, s += l) |
| 2183 | { |
| 2184 | #ifdef FEAT_MBYTE |
| 2185 | if (has_mbyte) |
| 2186 | { |
| 2187 | l = mb_ptr2len(p); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2188 | if (STRNICMP(p, s, l) != 0) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2189 | break; |
| 2190 | } |
| 2191 | else |
| 2192 | #endif |
| 2193 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2194 | if (MB_TOLOWER(*p) != MB_TOLOWER(*s)) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2195 | break; |
| 2196 | l = 1; |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | if (*p != NUL) |
| 2201 | { |
| 2202 | /* Leader was shortened, need to change the inserted text. */ |
| 2203 | *p = NUL; |
| 2204 | had_match = (curwin->w_cursor.col > compl_col); |
| 2205 | ins_compl_delete(); |
| 2206 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2207 | ins_redraw(FALSE); |
| 2208 | |
| 2209 | /* When the match isn't there (to avoid matching itself) remove it |
| 2210 | * again after redrawing. */ |
| 2211 | if (!had_match) |
| 2212 | ins_compl_delete(); |
| 2213 | } |
| 2214 | |
| 2215 | compl_used_match = FALSE; |
| 2216 | } |
| 2217 | } |
| 2218 | |
| 2219 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2220 | * Add an array of matches to the list of matches. |
| 2221 | * Frees matches[]. |
| 2222 | */ |
| 2223 | static void |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2224 | ins_compl_add_matches(num_matches, matches) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2225 | int num_matches; |
| 2226 | char_u **matches; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | { |
| 2228 | int i; |
| 2229 | int add_r = OK; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2230 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2231 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2232 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2233 | if ((add_r = ins_compl_add(matches[i], -1, NULL, NULL, dir, 0)) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2234 | /* if dir was BACKWARD then honor it just once */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2235 | dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2236 | FreeWild(num_matches, matches); |
| 2237 | } |
| 2238 | |
| 2239 | /* Make the completion list cyclic. |
| 2240 | * Return the number of matches (excluding the original). |
| 2241 | */ |
| 2242 | static int |
| 2243 | ins_compl_make_cyclic() |
| 2244 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2245 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2246 | int count = 0; |
| 2247 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2248 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2249 | { |
| 2250 | /* |
| 2251 | * Find the end of the list. |
| 2252 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2253 | match = compl_first_match; |
| 2254 | /* 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] | 2255 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2257 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | ++count; |
| 2259 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2260 | match->cp_next = compl_first_match; |
| 2261 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2262 | } |
| 2263 | return count; |
| 2264 | } |
| 2265 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2266 | /* "compl_match_array" points the currently displayed list of entries in the |
| 2267 | * popup menu. It is NULL when there is no popup menu. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2268 | static pumitem_T *compl_match_array = NULL; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2269 | static int compl_match_arraysize; |
| 2270 | |
| 2271 | /* |
| 2272 | * Update the screen and when there is any scrolling remove the popup menu. |
| 2273 | */ |
| 2274 | static void |
| 2275 | ins_compl_upd_pum() |
| 2276 | { |
| 2277 | int h; |
| 2278 | |
| 2279 | if (compl_match_array != NULL) |
| 2280 | { |
| 2281 | h = curwin->w_cline_height; |
| 2282 | update_screen(0); |
| 2283 | if (h != curwin->w_cline_height) |
| 2284 | ins_compl_del_pum(); |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | /* |
| 2289 | * Remove any popup menu. |
| 2290 | */ |
| 2291 | static void |
| 2292 | ins_compl_del_pum() |
| 2293 | { |
| 2294 | if (compl_match_array != NULL) |
| 2295 | { |
| 2296 | pum_undisplay(); |
| 2297 | vim_free(compl_match_array); |
| 2298 | compl_match_array = NULL; |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | /* |
| 2303 | * Return TRUE if the popup menu should be displayed. |
| 2304 | */ |
| 2305 | static int |
| 2306 | pum_wanted() |
| 2307 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2308 | /* 'completeopt' must contain "menu" */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2309 | if (vim_strchr(p_cot, 'm') == NULL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2310 | return FALSE; |
| 2311 | |
| 2312 | /* The display looks bad on a B&W display. */ |
| 2313 | if (t_colors < 8 |
| 2314 | #ifdef FEAT_GUI |
| 2315 | && !gui.in_use |
| 2316 | #endif |
| 2317 | ) |
| 2318 | return FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2319 | return TRUE; |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 2324 | */ |
| 2325 | static int |
| 2326 | pum_two_or_more() |
| 2327 | { |
| 2328 | compl_T *compl; |
| 2329 | int i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2330 | |
| 2331 | /* Don't display the popup menu if there are no matches or there is only |
| 2332 | * one (ignoring the original text). */ |
| 2333 | compl = compl_first_match; |
| 2334 | i = 0; |
| 2335 | do |
| 2336 | { |
| 2337 | if (compl == NULL |
| 2338 | || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) |
| 2339 | break; |
| 2340 | compl = compl->cp_next; |
| 2341 | } while (compl != compl_first_match); |
| 2342 | |
| 2343 | return (i >= 2); |
| 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * Show the popup menu for the list of matches. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2348 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2349 | */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2350 | void |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2351 | ins_compl_show_pum() |
| 2352 | { |
| 2353 | compl_T *compl; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2354 | compl_T *shown_compl = NULL; |
| 2355 | int did_find_shown_match = FALSE; |
| 2356 | int shown_match_ok = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2357 | int i; |
| 2358 | int cur = -1; |
| 2359 | colnr_T col; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2360 | int lead_len = 0; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2361 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2362 | if (!pum_wanted() || !pum_two_or_more()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2363 | return; |
| 2364 | |
| 2365 | /* Update the screen before drawing the popup menu over it. */ |
| 2366 | update_screen(0); |
| 2367 | |
| 2368 | if (compl_match_array == NULL) |
| 2369 | { |
| 2370 | /* Need to build the popup menu list. */ |
| 2371 | compl_match_arraysize = 0; |
| 2372 | compl = compl_first_match; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2373 | if (compl_leader != NULL) |
| 2374 | lead_len = STRLEN(compl_leader); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2375 | do |
| 2376 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2377 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2378 | && (compl_leader == NULL |
| 2379 | || STRNCMP(compl->cp_str, compl_leader, lead_len) == 0)) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2380 | ++compl_match_arraysize; |
| 2381 | compl = compl->cp_next; |
| 2382 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2383 | if (compl_match_arraysize == 0) |
| 2384 | return; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2385 | compl_match_array = (pumitem_T *)alloc_clear( |
| 2386 | (unsigned)(sizeof(pumitem_T) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2387 | * compl_match_arraysize)); |
| 2388 | if (compl_match_array != NULL) |
| 2389 | { |
| 2390 | i = 0; |
| 2391 | compl = compl_first_match; |
| 2392 | do |
| 2393 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2394 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2395 | && (compl_leader == NULL |
| 2396 | || STRNCMP(compl->cp_str, compl_leader, |
| 2397 | lead_len) == 0)) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2398 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2399 | if (!shown_match_ok) |
| 2400 | { |
| 2401 | if (compl == compl_shown_match || did_find_shown_match) |
| 2402 | { |
| 2403 | /* This item is the shown match or this is the |
| 2404 | * first displayed item after the shown match. */ |
| 2405 | compl_shown_match = compl; |
| 2406 | did_find_shown_match = TRUE; |
| 2407 | shown_match_ok = TRUE; |
| 2408 | } |
| 2409 | else |
| 2410 | /* Remember this displayed match for when the |
| 2411 | * shown match is just below it. */ |
| 2412 | shown_compl = compl; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2413 | cur = i; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2414 | } |
| 2415 | compl_match_array[i].pum_text = compl->cp_str; |
| 2416 | if (compl->cp_extra != NULL) |
| 2417 | compl_match_array[i++].pum_extra = compl->cp_extra; |
| 2418 | else |
| 2419 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 2420 | } |
| 2421 | |
| 2422 | if (compl == compl_shown_match) |
| 2423 | { |
| 2424 | did_find_shown_match = TRUE; |
| 2425 | if (!shown_match_ok && shown_compl != NULL) |
| 2426 | { |
| 2427 | /* The shown match isn't displayed, set it to the |
| 2428 | * previously displayed match. */ |
| 2429 | compl_shown_match = shown_compl; |
| 2430 | shown_match_ok = TRUE; |
| 2431 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2432 | } |
| 2433 | compl = compl->cp_next; |
| 2434 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2435 | |
| 2436 | if (!shown_match_ok) /* no displayed match at all */ |
| 2437 | cur = -1; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2438 | } |
| 2439 | } |
| 2440 | else |
| 2441 | { |
| 2442 | /* popup menu already exists, only need to find the current item.*/ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2443 | for (i = 0; i < compl_match_arraysize; ++i) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2444 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2445 | break; |
| 2446 | cur = i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | if (compl_match_array != NULL) |
| 2450 | { |
| 2451 | /* Compute the screen column of the start of the completed text. |
| 2452 | * Use the cursor to get all wrapping and other settings right. */ |
| 2453 | col = curwin->w_cursor.col; |
| 2454 | curwin->w_cursor.col = compl_col; |
| 2455 | validate_cursor_col(); |
| 2456 | pum_display(compl_match_array, compl_match_arraysize, cur, |
| 2457 | curwin->w_cline_row + W_WINROW(curwin), |
| 2458 | curwin->w_cline_height, |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2459 | curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2460 | curwin->w_cursor.col = col; |
| 2461 | } |
| 2462 | } |
| 2463 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 2465 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2466 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | /* |
| 2468 | * Add any identifiers that match the given pattern to the list of |
| 2469 | * completions. |
| 2470 | */ |
| 2471 | static void |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2472 | ins_compl_dictionaries(dict, pat, flags, thesaurus) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | char_u *dict; |
| 2474 | char_u *pat; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2475 | int flags; /* DICT_FIRST and/or DICT_EXACT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2476 | int thesaurus; |
| 2477 | { |
| 2478 | char_u *ptr; |
| 2479 | char_u *buf; |
| 2480 | FILE *fp; |
| 2481 | regmatch_T regmatch; |
| 2482 | int add_r; |
| 2483 | char_u **files; |
| 2484 | int count; |
| 2485 | int i; |
| 2486 | int save_p_scs; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2487 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2488 | |
| 2489 | buf = alloc(LSIZE); |
| 2490 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 2491 | save_p_scs = p_scs; |
| 2492 | if (curbuf->b_p_inf) |
| 2493 | p_scs = FALSE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2494 | |
| 2495 | /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 2496 | * to only match at the start of a line. Otherwise just match the |
| 2497 | * pattern. */ |
| 2498 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2499 | { |
| 2500 | i = STRLEN(pat) + 8; |
| 2501 | ptr = alloc(i); |
| 2502 | if (ptr == NULL) |
| 2503 | return; |
| 2504 | vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat); |
| 2505 | regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0); |
| 2506 | vim_free(ptr); |
| 2507 | } |
| 2508 | else |
| 2509 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 2510 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2511 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 2512 | regmatch.rm_ic = ignorecase(pat); |
| 2513 | while (buf != NULL && regmatch.regprog != NULL && *dict != NUL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2514 | && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2515 | { |
| 2516 | /* copy one dictionary file name into buf */ |
| 2517 | if (flags == DICT_EXACT) |
| 2518 | { |
| 2519 | count = 1; |
| 2520 | files = &dict; |
| 2521 | } |
| 2522 | else |
| 2523 | { |
| 2524 | /* Expand wildcards in the dictionary name, but do not allow |
| 2525 | * backticks (for security, the 'dict' option may have been set in |
| 2526 | * a modeline). */ |
| 2527 | copy_option_part(&dict, buf, LSIZE, ","); |
| 2528 | if (vim_strchr(buf, '`') != NULL |
| 2529 | || expand_wildcards(1, &buf, &count, &files, |
| 2530 | EW_FILE|EW_SILENT) != OK) |
| 2531 | count = 0; |
| 2532 | } |
| 2533 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2534 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | { |
| 2536 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 2537 | if (flags != DICT_EXACT) |
| 2538 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 2539 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2540 | _("Scanning dictionary: %s"), (char *)files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2541 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 2542 | } |
| 2543 | |
| 2544 | if (fp != NULL) |
| 2545 | { |
| 2546 | /* |
| 2547 | * Read dictionary file line by line. |
| 2548 | * Check each line for a match. |
| 2549 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2550 | while (!got_int && !compl_interrupted |
| 2551 | && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2552 | { |
| 2553 | ptr = buf; |
| 2554 | while (vim_regexec(®match, buf, (colnr_T)(ptr - buf))) |
| 2555 | { |
| 2556 | ptr = regmatch.startp[0]; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2557 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2558 | ptr = find_line_end(ptr); |
| 2559 | else |
| 2560 | ptr = find_word_end(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2561 | add_r = ins_compl_add_infercase(regmatch.startp[0], |
| 2562 | (int)(ptr - regmatch.startp[0]), |
| 2563 | files[i], dir, 0); |
| 2564 | if (thesaurus) |
| 2565 | { |
| 2566 | char_u *wstart; |
| 2567 | |
| 2568 | /* |
| 2569 | * Add the other matches on the line |
| 2570 | */ |
| 2571 | while (!got_int) |
| 2572 | { |
| 2573 | /* Find start of the next word. Skip white |
| 2574 | * space and punctuation. */ |
| 2575 | ptr = find_word_start(ptr); |
| 2576 | if (*ptr == NUL || *ptr == NL) |
| 2577 | break; |
| 2578 | wstart = ptr; |
| 2579 | |
| 2580 | /* Find end of the word and add it. */ |
| 2581 | #ifdef FEAT_MBYTE |
| 2582 | if (has_mbyte) |
| 2583 | /* Japanese words may have characters in |
| 2584 | * different classes, only separate words |
| 2585 | * with single-byte non-word characters. */ |
| 2586 | while (*ptr != NUL) |
| 2587 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2588 | int l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2589 | |
| 2590 | if (l < 2 && !vim_iswordc(*ptr)) |
| 2591 | break; |
| 2592 | ptr += l; |
| 2593 | } |
| 2594 | else |
| 2595 | #endif |
| 2596 | ptr = find_word_end(ptr); |
| 2597 | add_r = ins_compl_add_infercase(wstart, |
| 2598 | (int)(ptr - wstart), files[i], dir, 0); |
| 2599 | } |
| 2600 | } |
| 2601 | if (add_r == OK) |
| 2602 | /* if dir was BACKWARD then honor it just once */ |
| 2603 | dir = FORWARD; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2604 | else if (add_r == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2605 | break; |
| 2606 | /* avoid expensive call to vim_regexec() when at end |
| 2607 | * of line */ |
| 2608 | if (*ptr == '\n' || got_int) |
| 2609 | break; |
| 2610 | } |
| 2611 | line_breakcheck(); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2612 | ins_compl_check_keys(50); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | } |
| 2614 | fclose(fp); |
| 2615 | } |
| 2616 | } |
| 2617 | if (flags != DICT_EXACT) |
| 2618 | FreeWild(count, files); |
| 2619 | if (flags) |
| 2620 | break; |
| 2621 | } |
| 2622 | p_scs = save_p_scs; |
| 2623 | vim_free(regmatch.regprog); |
| 2624 | vim_free(buf); |
| 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * Find the start of the next word. |
| 2629 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 2630 | */ |
| 2631 | char_u * |
| 2632 | find_word_start(ptr) |
| 2633 | char_u *ptr; |
| 2634 | { |
| 2635 | #ifdef FEAT_MBYTE |
| 2636 | if (has_mbyte) |
| 2637 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2638 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2639 | else |
| 2640 | #endif |
| 2641 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 2642 | ++ptr; |
| 2643 | return ptr; |
| 2644 | } |
| 2645 | |
| 2646 | /* |
| 2647 | * Find the end of the word. Assumes it starts inside a word. |
| 2648 | * Returns a pointer to just after the word. |
| 2649 | */ |
| 2650 | char_u * |
| 2651 | find_word_end(ptr) |
| 2652 | char_u *ptr; |
| 2653 | { |
| 2654 | #ifdef FEAT_MBYTE |
| 2655 | int start_class; |
| 2656 | |
| 2657 | if (has_mbyte) |
| 2658 | { |
| 2659 | start_class = mb_get_class(ptr); |
| 2660 | if (start_class > 1) |
| 2661 | while (*ptr != NUL) |
| 2662 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2663 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | if (mb_get_class(ptr) != start_class) |
| 2665 | break; |
| 2666 | } |
| 2667 | } |
| 2668 | else |
| 2669 | #endif |
| 2670 | while (vim_iswordc(*ptr)) |
| 2671 | ++ptr; |
| 2672 | return ptr; |
| 2673 | } |
| 2674 | |
| 2675 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2676 | * Find the end of the line, omitting CR and NL at the end. |
| 2677 | * Returns a pointer to just after the line. |
| 2678 | */ |
| 2679 | static char_u * |
| 2680 | find_line_end(ptr) |
| 2681 | char_u *ptr; |
| 2682 | { |
| 2683 | char_u *s; |
| 2684 | |
| 2685 | s = ptr + STRLEN(ptr); |
| 2686 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 2687 | --s; |
| 2688 | return s; |
| 2689 | } |
| 2690 | |
| 2691 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2692 | * Free the list of completions |
| 2693 | */ |
| 2694 | static void |
| 2695 | ins_compl_free() |
| 2696 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2697 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2698 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2699 | vim_free(compl_pattern); |
| 2700 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2701 | vim_free(compl_leader); |
| 2702 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2703 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2704 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2705 | return; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2706 | |
| 2707 | ins_compl_del_pum(); |
| 2708 | pum_clear(); |
| 2709 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2710 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | do |
| 2712 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2713 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2714 | compl_curr_match = compl_curr_match->cp_next; |
| 2715 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2716 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2717 | if (match->cp_flags & FREE_FNAME) |
| 2718 | vim_free(match->cp_fname); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2719 | vim_free(match->cp_extra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2721 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 2722 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | static void |
| 2726 | ins_compl_clear() |
| 2727 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2728 | compl_cont_status = 0; |
| 2729 | compl_started = FALSE; |
| 2730 | compl_matches = 0; |
| 2731 | vim_free(compl_pattern); |
| 2732 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2733 | vim_free(compl_leader); |
| 2734 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | edit_submode_extra = NULL; |
| 2736 | } |
| 2737 | |
| 2738 | /* |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2739 | * Return TRUE when Insert completion is active. |
| 2740 | */ |
| 2741 | int |
| 2742 | ins_compl_active() |
| 2743 | { |
| 2744 | return compl_started; |
| 2745 | } |
| 2746 | |
| 2747 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2748 | * Delete one character before the cursor and show the subset of the matches |
| 2749 | * that match the word that is now before the cursor. |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2750 | * Returns TRUE if the work is done and another char to be got from the user. |
| 2751 | */ |
| 2752 | static int |
| 2753 | ins_compl_bs() |
| 2754 | { |
| 2755 | char_u *line; |
| 2756 | char_u *p; |
| 2757 | |
| 2758 | if (curwin->w_cursor.col <= compl_col + compl_length) |
| 2759 | { |
| 2760 | /* Deleted more than what was used to find matches, need to look for |
| 2761 | * matches all over again. */ |
| 2762 | ins_compl_free(); |
| 2763 | compl_started = FALSE; |
| 2764 | compl_matches = 0; |
| 2765 | } |
| 2766 | |
| 2767 | line = ml_get_curline(); |
| 2768 | p = line + curwin->w_cursor.col; |
| 2769 | mb_ptr_back(line, p); |
| 2770 | |
| 2771 | vim_free(compl_leader); |
| 2772 | compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col); |
| 2773 | if (compl_leader != NULL) |
| 2774 | { |
| 2775 | ins_compl_del_pum(); |
| 2776 | ins_compl_delete(); |
| 2777 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2778 | |
| 2779 | if (!compl_started) |
| 2780 | { |
| 2781 | /* Matches were cleared, need to search for them now. */ |
| 2782 | if (ins_complete(Ctrl_N) == FAIL) |
| 2783 | compl_cont_status = 0; |
| 2784 | else |
| 2785 | { |
| 2786 | /* Remove the completed word again. */ |
| 2787 | ins_compl_delete(); |
| 2788 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | /* Show the popup menu with a different set of matches. */ |
| 2793 | ins_compl_show_pum(); |
| 2794 | compl_used_match = FALSE; |
| 2795 | |
| 2796 | return TRUE; |
| 2797 | } |
| 2798 | return FALSE; |
| 2799 | } |
| 2800 | |
| 2801 | /* |
| 2802 | * Append one character to the match leader. May reduce the number of |
| 2803 | * matches. |
| 2804 | */ |
| 2805 | static void |
| 2806 | ins_compl_addleader(c) |
| 2807 | int c; |
| 2808 | { |
| 2809 | #ifdef FEAT_MBYTE |
| 2810 | int cc; |
| 2811 | |
| 2812 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 2813 | { |
| 2814 | char_u buf[MB_MAXBYTES + 1]; |
| 2815 | |
| 2816 | (*mb_char2bytes)(c, buf); |
| 2817 | buf[cc] = NUL; |
| 2818 | ins_char_bytes(buf, cc); |
| 2819 | } |
| 2820 | else |
| 2821 | #endif |
| 2822 | ins_char(c); |
| 2823 | |
| 2824 | vim_free(compl_leader); |
| 2825 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
| 2826 | curwin->w_cursor.col - compl_col); |
| 2827 | if (compl_leader != NULL) |
| 2828 | { |
| 2829 | /* Show the popup menu with a different set of matches. */ |
| 2830 | ins_compl_del_pum(); |
| 2831 | ins_compl_show_pum(); |
| 2832 | compl_used_match = FALSE; |
| 2833 | } |
| 2834 | } |
| 2835 | |
| 2836 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2837 | * Append one character to the match leader. May reduce the number of |
| 2838 | * matches. |
| 2839 | */ |
| 2840 | static void |
| 2841 | ins_compl_addfrommatch() |
| 2842 | { |
| 2843 | char_u *p; |
| 2844 | int len = curwin->w_cursor.col - compl_col; |
| 2845 | int c; |
| 2846 | |
| 2847 | p = compl_shown_match->cp_str; |
| 2848 | if (STRLEN(p) <= len) /* the match is too short */ |
| 2849 | return; |
| 2850 | p += len; |
| 2851 | #ifdef FEAT_MBYTE |
| 2852 | c = mb_ptr2char(p); |
| 2853 | #else |
| 2854 | c = *p; |
| 2855 | #endif |
| 2856 | ins_compl_addleader(c); |
| 2857 | } |
| 2858 | |
| 2859 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2861 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2862 | * Returns TRUE when the character is not to be inserted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2863 | */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2864 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2865 | ins_compl_prep(c) |
| 2866 | int c; |
| 2867 | { |
| 2868 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2869 | int temp; |
| 2870 | int want_cindent; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2871 | int retval = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2872 | |
| 2873 | /* Forget any previous 'special' messages if this is actually |
| 2874 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2875 | */ |
| 2876 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2877 | edit_submode_extra = NULL; |
| 2878 | |
| 2879 | /* Ignore end of Select mode mapping */ |
| 2880 | if (c == K_SELECT) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2881 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2882 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2883 | /* Set "compl_get_longest" when finding the first matches. */ |
| 2884 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 2885 | || (ctrl_x_mode == 0 && !compl_started)) |
| 2886 | { |
| 2887 | compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); |
| 2888 | compl_used_match = TRUE; |
| 2889 | } |
| 2890 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 2892 | { |
| 2893 | /* |
| 2894 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2895 | * it will be yet. Now we decide. |
| 2896 | */ |
| 2897 | switch (c) |
| 2898 | { |
| 2899 | case Ctrl_E: |
| 2900 | case Ctrl_Y: |
| 2901 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2902 | if (!(State & REPLACE_FLAG)) |
| 2903 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2904 | else |
| 2905 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2906 | edit_submode_pre = NULL; |
| 2907 | showmode(); |
| 2908 | break; |
| 2909 | case Ctrl_L: |
| 2910 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2911 | break; |
| 2912 | case Ctrl_F: |
| 2913 | ctrl_x_mode = CTRL_X_FILES; |
| 2914 | break; |
| 2915 | case Ctrl_K: |
| 2916 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2917 | break; |
| 2918 | case Ctrl_R: |
| 2919 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 2920 | break; |
| 2921 | case Ctrl_T: |
| 2922 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2923 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2924 | #ifdef FEAT_COMPL_FUNC |
| 2925 | case Ctrl_U: |
| 2926 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2927 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2928 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2929 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2930 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2931 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2932 | case 's': |
| 2933 | case Ctrl_S: |
| 2934 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 2935 | #ifdef FEAT_SYN_HL |
| 2936 | spell_back_to_badword(); |
| 2937 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2938 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2939 | case Ctrl_RSB: |
| 2940 | ctrl_x_mode = CTRL_X_TAGS; |
| 2941 | break; |
| 2942 | #ifdef FEAT_FIND_ID |
| 2943 | case Ctrl_I: |
| 2944 | case K_S_TAB: |
| 2945 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2946 | break; |
| 2947 | case Ctrl_D: |
| 2948 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2949 | break; |
| 2950 | #endif |
| 2951 | case Ctrl_V: |
| 2952 | case Ctrl_Q: |
| 2953 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2954 | break; |
| 2955 | case Ctrl_P: |
| 2956 | case Ctrl_N: |
| 2957 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2958 | * just started ^X mode, or there were enough ^X's to cancel |
| 2959 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2960 | * do normal expansion when interrupting a different mode (say |
| 2961 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 2962 | * nothing changes if interrupting mode 0, (eg, the flag |
| 2963 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2964 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2965 | compl_cont_status |= CONT_LOCAL; |
| 2966 | else if (compl_cont_mode != 0) |
| 2967 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | /* FALLTHROUGH */ |
| 2969 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2970 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 2971 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 2972 | * mode). |
| 2973 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 2974 | * value, in both cases ^X^X can be used to restart the same |
| 2975 | * mode (avoiding ADDING mode). |
| 2976 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 2977 | * 'complete' and local ^P expansions respectively. |
| 2978 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 2979 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2980 | if (c == Ctrl_X) |
| 2981 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2982 | if (compl_cont_mode != 0) |
| 2983 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2984 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2985 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2986 | } |
| 2987 | ctrl_x_mode = 0; |
| 2988 | edit_submode = NULL; |
| 2989 | showmode(); |
| 2990 | break; |
| 2991 | } |
| 2992 | } |
| 2993 | else if (ctrl_x_mode != 0) |
| 2994 | { |
| 2995 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 2996 | if (!vim_is_ctrl_x_key(c)) |
| 2997 | { |
| 2998 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 2999 | ctrl_x_mode = 0; |
| 3000 | else |
| 3001 | ctrl_x_mode = CTRL_X_FINISHED; |
| 3002 | edit_submode = NULL; |
| 3003 | } |
| 3004 | showmode(); |
| 3005 | } |
| 3006 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3007 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3008 | { |
| 3009 | /* Show error message from attempted keyword completion (probably |
| 3010 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3011 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | showmode(); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3013 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
| 3014 | && !ins_compl_pum_key(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3015 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 3016 | { |
| 3017 | /* Get here when we have finished typing a sequence of ^N and |
| 3018 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3019 | * memory that was used, and make sure we can redo the insert. */ |
| 3020 | if (compl_curr_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3022 | char_u *p; |
| 3023 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3024 | /* |
| 3025 | * If any of the original typed text has been changed, |
| 3026 | * eg when ignorecase is set, we must add back-spaces to |
| 3027 | * the redo buffer. We add as few as necessary to delete |
| 3028 | * just the part of the original text that has changed. |
| 3029 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3030 | ptr = compl_curr_match->cp_str; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3031 | p = compl_orig_text; |
| 3032 | while (*p && *p == *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3033 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3034 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3035 | ++ptr; |
| 3036 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3037 | for (temp = 0; p[temp]; ++temp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3038 | AppendCharToRedobuff(K_BS); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 3039 | AppendToRedobuffLit(ptr, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | #ifdef FEAT_CINDENT |
| 3043 | want_cindent = (can_cindent && cindent_on()); |
| 3044 | #endif |
| 3045 | /* |
| 3046 | * When completing whole lines: fix indent for 'cindent'. |
| 3047 | * Otherwise, break line if it's too long. |
| 3048 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3049 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3050 | { |
| 3051 | #ifdef FEAT_CINDENT |
| 3052 | /* re-indent the current line */ |
| 3053 | if (want_cindent) |
| 3054 | { |
| 3055 | do_c_expr_indent(); |
| 3056 | want_cindent = FALSE; /* don't do it again */ |
| 3057 | } |
| 3058 | #endif |
| 3059 | } |
| 3060 | else |
| 3061 | { |
| 3062 | /* put the cursor on the last char, for 'tw' formatting */ |
| 3063 | curwin->w_cursor.col--; |
| 3064 | if (stop_arrow() == OK) |
| 3065 | insertchar(NUL, 0, -1); |
| 3066 | curwin->w_cursor.col++; |
| 3067 | } |
| 3068 | |
| 3069 | auto_format(FALSE, TRUE); |
| 3070 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3071 | /* if the popup menu is displayed hitting Enter means accepting |
| 3072 | * the selection without inserting anything. */ |
| 3073 | if ((c == CAR || c == K_KENTER || c == NL) && pum_visible()) |
| 3074 | retval = TRUE; |
| 3075 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3076 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3077 | compl_started = FALSE; |
| 3078 | compl_matches = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3079 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 3080 | ctrl_x_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3081 | if (edit_submode != NULL) |
| 3082 | { |
| 3083 | edit_submode = NULL; |
| 3084 | showmode(); |
| 3085 | } |
| 3086 | |
| 3087 | #ifdef FEAT_CINDENT |
| 3088 | /* |
| 3089 | * Indent now if a key was typed that is in 'cinkeys'. |
| 3090 | */ |
| 3091 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 3092 | do_c_expr_indent(); |
| 3093 | #endif |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 3098 | * (re)set properly in ins_complete() */ |
| 3099 | if (!vim_is_ctrl_x_key(c)) |
| 3100 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3101 | compl_cont_status = 0; |
| 3102 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3103 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3104 | |
| 3105 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /* |
| 3109 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3110 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3111 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3112 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3113 | * |
| 3114 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3115 | */ |
| 3116 | static buf_T * |
| 3117 | ins_compl_next_buf(buf, flag) |
| 3118 | buf_T *buf; |
| 3119 | int flag; |
| 3120 | { |
| 3121 | #ifdef FEAT_WINDOWS |
| 3122 | static win_T *wp; |
| 3123 | #endif |
| 3124 | |
| 3125 | if (flag == 'w') /* just windows */ |
| 3126 | { |
| 3127 | #ifdef FEAT_WINDOWS |
| 3128 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 3129 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3130 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3131 | && wp->w_buffer->b_scanned) |
| 3132 | ; |
| 3133 | buf = wp->w_buffer; |
| 3134 | #else |
| 3135 | buf = curbuf; |
| 3136 | #endif |
| 3137 | } |
| 3138 | else |
| 3139 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3140 | * (unlisted buffers) |
| 3141 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3142 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3143 | && ((flag == 'U' |
| 3144 | ? buf->b_p_bl |
| 3145 | : (!buf->b_p_bl |
| 3146 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3147 | || buf->b_scanned)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3148 | ; |
| 3149 | return buf; |
| 3150 | } |
| 3151 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3152 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3153 | static void expand_by_function __ARGS((int type, char_u *base)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3154 | |
| 3155 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3156 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3157 | * get matches in "matches". |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3158 | * Return value is number of matches. |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3159 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3160 | static void |
| 3161 | expand_by_function(type, base) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3162 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3163 | char_u *base; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3164 | { |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3165 | list_T *matchlist; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3166 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3167 | listitem_T *li; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3168 | char_u *p; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3169 | char_u *funcname; |
| 3170 | pos_T pos; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3171 | int dir = compl_direction; |
| 3172 | char_u *x; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3173 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3174 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3175 | if (*funcname == NUL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3176 | return; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3177 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3178 | /* Call 'completefunc' to obtain the list of matches. */ |
| 3179 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3180 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3181 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3182 | pos = curwin->w_cursor; |
| 3183 | matchlist = call_func_retlist(funcname, 2, args, FALSE); |
| 3184 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3185 | if (matchlist == NULL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3186 | return; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3187 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3188 | /* Go through the List with matches and add each of them. */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3189 | for (li = matchlist->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3190 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3191 | if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL) |
| 3192 | { |
| 3193 | p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE); |
| 3194 | x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE); |
| 3195 | } |
| 3196 | else |
| 3197 | { |
| 3198 | p = get_tv_string_chk(&li->li_tv); |
| 3199 | x = NULL; |
| 3200 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3201 | if (p != NULL && *p != NUL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3202 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3203 | if (ins_compl_add(p, -1, NULL, x, dir, 0) == OK) |
| 3204 | /* if dir was BACKWARD then honor it just once */ |
| 3205 | dir = FORWARD; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3206 | } |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 3207 | else if (did_emsg) |
| 3208 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3209 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3210 | |
| 3211 | list_unref(matchlist); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3212 | } |
| 3213 | #endif /* FEAT_COMPL_FUNC */ |
| 3214 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3215 | /* |
| 3216 | * Get the next expansion(s), using "compl_pattern". |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3217 | * The search starts at position "ini" in curbuf and in the direction |
| 3218 | * compl_direction. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3219 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 3220 | * where we stopped searching before. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3221 | * This may return before finding all the matches. |
| 3222 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3223 | */ |
| 3224 | static int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3225 | ins_compl_get_exp(ini) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3226 | pos_T *ini; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3227 | { |
| 3228 | static pos_T first_match_pos; |
| 3229 | static pos_T last_match_pos; |
| 3230 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3231 | static int found_all = FALSE; /* Found all matches of a |
| 3232 | certain type. */ |
| 3233 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3234 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3235 | pos_T *pos; |
| 3236 | char_u **matches; |
| 3237 | int save_p_scs; |
| 3238 | int save_p_ws; |
| 3239 | int save_p_ic; |
| 3240 | int i; |
| 3241 | int num_matches; |
| 3242 | int len; |
| 3243 | int found_new_match; |
| 3244 | int type = ctrl_x_mode; |
| 3245 | char_u *ptr; |
| 3246 | char_u *dict = NULL; |
| 3247 | int dict_f = 0; |
| 3248 | compl_T *old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3249 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3250 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3251 | { |
| 3252 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 3253 | ins_buf->b_scanned = 0; |
| 3254 | found_all = FALSE; |
| 3255 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3256 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3257 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3258 | last_match_pos = first_match_pos = *ini; |
| 3259 | } |
| 3260 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3261 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3262 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 3264 | for (;;) |
| 3265 | { |
| 3266 | found_new_match = FAIL; |
| 3267 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3268 | /* 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] | 3269 | * or if found_all says this entry is done. For ^X^L only use the |
| 3270 | * entries from 'complete' that look in loaded buffers. */ |
| 3271 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3272 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | { |
| 3274 | found_all = FALSE; |
| 3275 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 3276 | e_cpt++; |
| 3277 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 3278 | { |
| 3279 | ins_buf = curbuf; |
| 3280 | first_match_pos = *ini; |
| 3281 | /* So that ^N can match word immediately after cursor */ |
| 3282 | if (ctrl_x_mode == 0) |
| 3283 | dec(&first_match_pos); |
| 3284 | last_match_pos = first_match_pos; |
| 3285 | type = 0; |
| 3286 | } |
| 3287 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 3288 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 3289 | { |
| 3290 | /* Scan a buffer, but not the current one. */ |
| 3291 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 3292 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3293 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3294 | first_match_pos.col = last_match_pos.col = 0; |
| 3295 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 3296 | last_match_pos.lnum = 0; |
| 3297 | type = 0; |
| 3298 | } |
| 3299 | else /* unloaded buffer, scan like dictionary */ |
| 3300 | { |
| 3301 | found_all = TRUE; |
| 3302 | if (ins_buf->b_fname == NULL) |
| 3303 | continue; |
| 3304 | type = CTRL_X_DICTIONARY; |
| 3305 | dict = ins_buf->b_fname; |
| 3306 | dict_f = DICT_EXACT; |
| 3307 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3308 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3309 | ins_buf->b_fname == NULL |
| 3310 | ? buf_spname(ins_buf) |
| 3311 | : ins_buf->b_sfname == NULL |
| 3312 | ? (char *)ins_buf->b_fname |
| 3313 | : (char *)ins_buf->b_sfname); |
| 3314 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 3315 | } |
| 3316 | else if (*e_cpt == NUL) |
| 3317 | break; |
| 3318 | else |
| 3319 | { |
| 3320 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3321 | type = -1; |
| 3322 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 3323 | { |
| 3324 | if (*e_cpt == 'k') |
| 3325 | type = CTRL_X_DICTIONARY; |
| 3326 | else |
| 3327 | type = CTRL_X_THESAURUS; |
| 3328 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 3329 | { |
| 3330 | dict = e_cpt; |
| 3331 | dict_f = DICT_FIRST; |
| 3332 | } |
| 3333 | } |
| 3334 | #ifdef FEAT_FIND_ID |
| 3335 | else if (*e_cpt == 'i') |
| 3336 | type = CTRL_X_PATH_PATTERNS; |
| 3337 | else if (*e_cpt == 'd') |
| 3338 | type = CTRL_X_PATH_DEFINES; |
| 3339 | #endif |
| 3340 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 3341 | { |
| 3342 | type = CTRL_X_TAGS; |
| 3343 | sprintf((char*)IObuff, _("Scanning tags.")); |
| 3344 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 3345 | } |
| 3346 | else |
| 3347 | type = -1; |
| 3348 | |
| 3349 | /* in any case e_cpt is advanced to the next entry */ |
| 3350 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 3351 | |
| 3352 | found_all = TRUE; |
| 3353 | if (type == -1) |
| 3354 | continue; |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | switch (type) |
| 3359 | { |
| 3360 | case -1: |
| 3361 | break; |
| 3362 | #ifdef FEAT_FIND_ID |
| 3363 | case CTRL_X_PATH_PATTERNS: |
| 3364 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3365 | find_pattern_in_path(compl_pattern, compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3366 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3367 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3368 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3369 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 3370 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 3371 | break; |
| 3372 | #endif |
| 3373 | |
| 3374 | case CTRL_X_DICTIONARY: |
| 3375 | case CTRL_X_THESAURUS: |
| 3376 | ins_compl_dictionaries( |
| 3377 | dict ? dict |
| 3378 | : (type == CTRL_X_THESAURUS |
| 3379 | ? (*curbuf->b_p_tsr == NUL |
| 3380 | ? p_tsr |
| 3381 | : curbuf->b_p_tsr) |
| 3382 | : (*curbuf->b_p_dict == NUL |
| 3383 | ? p_dict |
| 3384 | : curbuf->b_p_dict)), |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3385 | compl_pattern, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3386 | dict ? dict_f : 0, type == CTRL_X_THESAURUS); |
| 3387 | dict = NULL; |
| 3388 | break; |
| 3389 | |
| 3390 | case CTRL_X_TAGS: |
| 3391 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 3392 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3393 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3394 | |
| 3395 | /* Find up to TAG_MANY matches. Avoids that an enourmous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3396 | * of matches is found when compl_pattern is empty */ |
| 3397 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3398 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 3399 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 3400 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 3401 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3402 | ins_compl_add_matches(num_matches, matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3403 | } |
| 3404 | p_ic = save_p_ic; |
| 3405 | break; |
| 3406 | |
| 3407 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3408 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3409 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 3410 | { |
| 3411 | |
| 3412 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3413 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3414 | ins_compl_add_matches(num_matches, matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3415 | } |
| 3416 | break; |
| 3417 | |
| 3418 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3419 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 3420 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3421 | &num_matches, &matches) == EXPAND_OK) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3422 | ins_compl_add_matches(num_matches, matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3423 | break; |
| 3424 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3425 | #ifdef FEAT_COMPL_FUNC |
| 3426 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3427 | case CTRL_X_OMNI: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3428 | expand_by_function(type, compl_pattern); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3429 | break; |
| 3430 | #endif |
| 3431 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3432 | case CTRL_X_SPELL: |
| 3433 | #ifdef FEAT_SYN_HL |
| 3434 | num_matches = expand_spelling(first_match_pos.lnum, |
| 3435 | first_match_pos.col, compl_pattern, &matches); |
| 3436 | if (num_matches > 0) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3437 | ins_compl_add_matches(num_matches, matches); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3438 | #endif |
| 3439 | break; |
| 3440 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3441 | default: /* normal ^P/^N and ^X^L */ |
| 3442 | /* |
| 3443 | * If 'infercase' is set, don't use 'smartcase' here |
| 3444 | */ |
| 3445 | save_p_scs = p_scs; |
| 3446 | if (ins_buf->b_p_inf) |
| 3447 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3448 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | /* buffers other than curbuf are scanned from the beginning or the |
| 3450 | * end but never from the middle, thus setting nowrapscan in this |
| 3451 | * buffers is a good idea, on the other hand, we always set |
| 3452 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 3453 | save_p_ws = p_ws; |
| 3454 | if (ins_buf != curbuf) |
| 3455 | p_ws = FALSE; |
| 3456 | else if (*e_cpt == '.') |
| 3457 | p_ws = TRUE; |
| 3458 | for (;;) |
| 3459 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3460 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3462 | /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that |
| 3463 | * has added a word that was at the beginning of the line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3464 | if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3465 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3466 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3467 | compl_direction, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3468 | else |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3469 | found_new_match = searchit(NULL, ins_buf, pos, |
| 3470 | compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3471 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3472 | RE_LAST); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3473 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3474 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3475 | /* set "compl_started" even on fail */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3476 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3477 | first_match_pos = *pos; |
| 3478 | last_match_pos = *pos; |
| 3479 | } |
| 3480 | else if (first_match_pos.lnum == last_match_pos.lnum |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3481 | && first_match_pos.col == last_match_pos.col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3482 | found_new_match = FAIL; |
| 3483 | if (found_new_match == FAIL) |
| 3484 | { |
| 3485 | if (ins_buf == curbuf) |
| 3486 | found_all = TRUE; |
| 3487 | break; |
| 3488 | } |
| 3489 | |
| 3490 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3491 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3492 | && ini->lnum == pos->lnum |
| 3493 | && ini->col == pos->col) |
| 3494 | continue; |
| 3495 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 3496 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3497 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3498 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3499 | { |
| 3500 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 3501 | continue; |
| 3502 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3503 | if (!p_paste) |
| 3504 | ptr = skipwhite(ptr); |
| 3505 | } |
| 3506 | len = (int)STRLEN(ptr); |
| 3507 | } |
| 3508 | else |
| 3509 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3510 | char_u *tmp_ptr = ptr; |
| 3511 | |
| 3512 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3513 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3514 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3515 | /* Skip if already inside a word. */ |
| 3516 | if (vim_iswordp(tmp_ptr)) |
| 3517 | continue; |
| 3518 | /* Find start of next word. */ |
| 3519 | tmp_ptr = find_word_start(tmp_ptr); |
| 3520 | } |
| 3521 | /* Find end of this word. */ |
| 3522 | tmp_ptr = find_word_end(tmp_ptr); |
| 3523 | len = (int)(tmp_ptr - ptr); |
| 3524 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3525 | if ((compl_cont_status & CONT_ADDING) |
| 3526 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3527 | { |
| 3528 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 3529 | { |
| 3530 | /* Try next line, if any. the new word will be |
| 3531 | * "join" as if the normal command "J" was used. |
| 3532 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3533 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3534 | * works -- Acevedo */ |
| 3535 | STRNCPY(IObuff, ptr, len); |
| 3536 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3537 | tmp_ptr = ptr = skipwhite(ptr); |
| 3538 | /* Find start of next word. */ |
| 3539 | tmp_ptr = find_word_start(tmp_ptr); |
| 3540 | /* Find end of next word. */ |
| 3541 | tmp_ptr = find_word_end(tmp_ptr); |
| 3542 | if (tmp_ptr > ptr) |
| 3543 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3544 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3545 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3546 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3547 | IObuff[len++] = ' '; |
| 3548 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 3549 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3550 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3551 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 3552 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3553 | && (IObuff[len - 2] == '?' |
| 3554 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3555 | IObuff[len++] = ' '; |
| 3556 | } |
| 3557 | /* copy as much as posible of the new word */ |
| 3558 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 3559 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 3560 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 3561 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3562 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3563 | } |
| 3564 | IObuff[len] = NUL; |
| 3565 | ptr = IObuff; |
| 3566 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3567 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3568 | continue; |
| 3569 | } |
| 3570 | } |
| 3571 | if (ins_compl_add_infercase(ptr, len, |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3572 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3573 | 0, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3574 | { |
| 3575 | found_new_match = OK; |
| 3576 | break; |
| 3577 | } |
| 3578 | } |
| 3579 | p_scs = save_p_scs; |
| 3580 | p_ws = save_p_ws; |
| 3581 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3582 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3583 | /* check if compl_curr_match has changed, (e.g. other type of |
| 3584 | * expansion added somenthing) */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3585 | if (type != 0 && compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | found_new_match = OK; |
| 3587 | |
| 3588 | /* break the loop for specialized modes (use 'complete' just for the |
| 3589 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
| 3590 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3591 | || found_new_match != FAIL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3592 | { |
| 3593 | if (got_int) |
| 3594 | break; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3595 | /* Fill the popup menu as soon as possible. */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3596 | if (pum_wanted() && type != -1) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3597 | ins_compl_check_keys(0); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3598 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3599 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 3600 | || compl_interrupted) |
| 3601 | break; |
| 3602 | compl_started = TRUE; |
| 3603 | } |
| 3604 | else |
| 3605 | { |
| 3606 | /* Mark a buffer scanned when it has been scanned completely */ |
| 3607 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 3608 | ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3609 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3610 | compl_started = FALSE; |
| 3611 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3612 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3613 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3614 | |
| 3615 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3616 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 3617 | found_new_match = FAIL; |
| 3618 | |
| 3619 | i = -1; /* total of matches, unknown */ |
| 3620 | if (found_new_match == FAIL |
| 3621 | || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) |
| 3622 | i = ins_compl_make_cyclic(); |
| 3623 | |
| 3624 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3625 | * just been made cyclic then we have to move compl_curr_match to the next |
| 3626 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3627 | compl_curr_match = compl_direction == FORWARD ? old_match->cp_next : old_match->cp_prev; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3628 | if (compl_curr_match == NULL) |
| 3629 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3630 | return i; |
| 3631 | } |
| 3632 | |
| 3633 | /* Delete the old text being completed. */ |
| 3634 | static void |
| 3635 | ins_compl_delete() |
| 3636 | { |
| 3637 | int i; |
| 3638 | |
| 3639 | /* |
| 3640 | * In insert mode: Delete the typed part. |
| 3641 | * In replace mode: Put the old characters back, if any. |
| 3642 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3643 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | backspace_until_column(i); |
| 3645 | changed_cline_bef_curs(); |
| 3646 | } |
| 3647 | |
| 3648 | /* Insert the new text being completed. */ |
| 3649 | static void |
| 3650 | ins_compl_insert() |
| 3651 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3652 | ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3653 | compl_used_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /* |
| 3657 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3658 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 3659 | * get more completions. If it is FALSE, then we just do nothing when there |
| 3660 | * are no more completions in a given direction. The latter case is used when |
| 3661 | * we are still in the middle of finding completions, to allow browsing |
| 3662 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3663 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 3664 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3665 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 3666 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3667 | * |
| 3668 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3669 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 3670 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3671 | */ |
| 3672 | static int |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3673 | ins_compl_next(allow_get_expansion, count, insert_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3674 | int allow_get_expansion; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3675 | int count; /* repeat completion this many times; should |
| 3676 | be at least 1 */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3677 | int insert_match; /* Insert the newly selected match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3678 | { |
| 3679 | int num_matches = -1; |
| 3680 | int i; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3681 | int todo = count; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3682 | compl_T *found_compl = NULL; |
| 3683 | int found_end = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3684 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3685 | if (compl_leader != NULL |
| 3686 | && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3687 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3688 | /* Set "compl_shown_match" to the actually shown match, it may differ |
| 3689 | * when "compl_leader" is used to omit some of the matches. */ |
| 3690 | while (STRNCMP(compl_shown_match->cp_str, |
| 3691 | compl_leader, STRLEN(compl_leader)) != 0 |
| 3692 | && compl_shown_match->cp_next != NULL |
| 3693 | && compl_shown_match->cp_next != compl_first_match) |
| 3694 | compl_shown_match = compl_shown_match->cp_next; |
| 3695 | } |
| 3696 | |
| 3697 | if (allow_get_expansion && insert_match |
| 3698 | && (!compl_get_longest || compl_used_match)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3699 | /* Delete old text to be replaced */ |
| 3700 | ins_compl_delete(); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3701 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3702 | compl_pending = FALSE; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3703 | |
| 3704 | /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 3705 | * around. */ |
| 3706 | while (--todo >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3707 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3708 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3709 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3710 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3711 | found_end = (compl_first_match != NULL |
| 3712 | && (compl_shown_match->cp_next == compl_first_match |
| 3713 | || compl_shown_match == compl_first_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3714 | } |
| 3715 | else if (compl_shows_dir == BACKWARD |
| 3716 | && compl_shown_match->cp_prev != NULL) |
| 3717 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3718 | found_end = (compl_shown_match == compl_first_match); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3719 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3720 | found_end |= (compl_shown_match == compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3721 | } |
| 3722 | else |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3723 | { |
| 3724 | compl_pending = TRUE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3725 | if (!allow_get_expansion) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3726 | return -1; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3727 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3728 | num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3729 | if (compl_pending && compl_direction == compl_shows_dir) |
| 3730 | compl_shown_match = compl_curr_match; |
| 3731 | found_end = FALSE; |
| 3732 | } |
| 3733 | if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 |
| 3734 | && compl_leader != NULL |
| 3735 | && STRNCMP(compl_shown_match->cp_str, |
| 3736 | compl_leader, STRLEN(compl_leader)) != 0) |
| 3737 | ++todo; |
| 3738 | else |
| 3739 | /* Remember a matching item. */ |
| 3740 | found_compl = compl_shown_match; |
| 3741 | |
| 3742 | /* Stop at the end of the list when we found a usable match. */ |
| 3743 | if (found_end) |
| 3744 | { |
| 3745 | if (found_compl != NULL) |
| 3746 | { |
| 3747 | compl_shown_match = found_compl; |
| 3748 | break; |
| 3749 | } |
| 3750 | todo = 1; /* use first usable match after wrapping around */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3751 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3754 | /* Insert the text of the new completion, or the compl_leader. */ |
| 3755 | if (insert_match) |
| 3756 | { |
| 3757 | if (!compl_get_longest || compl_used_match) |
| 3758 | ins_compl_insert(); |
| 3759 | else |
| 3760 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 3761 | } |
| 3762 | else |
| 3763 | compl_used_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | |
| 3765 | if (!allow_get_expansion) |
| 3766 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3767 | /* may undisplay the popup menu first */ |
| 3768 | ins_compl_upd_pum(); |
| 3769 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3770 | /* redraw to show the user what was inserted */ |
| 3771 | update_screen(0); |
| 3772 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3773 | /* display the updated popup menu */ |
| 3774 | ins_compl_show_pum(); |
| 3775 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3776 | /* Delete old text to be replaced, since we're still searching and |
| 3777 | * don't want to match ourselves! */ |
| 3778 | ins_compl_delete(); |
| 3779 | } |
| 3780 | |
| 3781 | /* |
| 3782 | * Show the file name for the match (if any) |
| 3783 | * Truncate the file name to avoid a wait for return. |
| 3784 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3785 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3786 | { |
| 3787 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3788 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | if (i <= 0) |
| 3790 | i = 0; |
| 3791 | else |
| 3792 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3793 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3794 | msg(IObuff); |
| 3795 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 3796 | } |
| 3797 | |
| 3798 | return num_matches; |
| 3799 | } |
| 3800 | |
| 3801 | /* |
| 3802 | * Call this while finding completions, to check whether the user has hit a key |
| 3803 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3804 | * 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] | 3805 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3806 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | */ |
| 3808 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3809 | ins_compl_check_keys(frequency) |
| 3810 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3811 | { |
| 3812 | static int count = 0; |
| 3813 | |
| 3814 | int c; |
| 3815 | |
| 3816 | /* Don't check when reading keys from a script. That would break the test |
| 3817 | * scripts */ |
| 3818 | if (using_script()) |
| 3819 | return; |
| 3820 | |
| 3821 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3822 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3823 | return; |
| 3824 | count = 0; |
| 3825 | |
| 3826 | ++no_mapping; |
| 3827 | c = vpeekc_any(); |
| 3828 | --no_mapping; |
| 3829 | if (c != NUL) |
| 3830 | { |
| 3831 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 3832 | { |
| 3833 | c = safe_vgetc(); /* Eat the character */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3834 | compl_shows_dir = ins_compl_key2dir(c); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3835 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 3836 | c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3837 | } |
| 3838 | else if (c != Ctrl_R) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3839 | compl_interrupted = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3841 | if (compl_pending && !got_int) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3842 | (void)ins_compl_next(FALSE, 1, TRUE); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| 3845 | /* |
| 3846 | * Decide the direction of Insert mode complete from the key typed. |
| 3847 | * Returns BACKWARD or FORWARD. |
| 3848 | */ |
| 3849 | static int |
| 3850 | ins_compl_key2dir(c) |
| 3851 | int c; |
| 3852 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3853 | if (c == Ctrl_P || c == Ctrl_L |
| 3854 | || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP |
| 3855 | || c == K_S_UP || c == K_UP))) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3856 | return BACKWARD; |
| 3857 | return FORWARD; |
| 3858 | } |
| 3859 | |
| 3860 | /* |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3861 | * Return TRUE for keys that are used for completion only when the popup menu |
| 3862 | * is visible. |
| 3863 | */ |
| 3864 | static int |
| 3865 | ins_compl_pum_key(c) |
| 3866 | int c; |
| 3867 | { |
| 3868 | return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3869 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 3870 | || c == K_UP || c == K_DOWN); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | /* |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3874 | * Decide the number of completions to move forward. |
| 3875 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 3876 | */ |
| 3877 | static int |
| 3878 | ins_compl_key2count(c) |
| 3879 | int c; |
| 3880 | { |
| 3881 | int h; |
| 3882 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3883 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3884 | { |
| 3885 | h = pum_get_height(); |
| 3886 | if (h > 3) |
| 3887 | h -= 2; /* keep some context */ |
| 3888 | return h; |
| 3889 | } |
| 3890 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
| 3893 | /* |
| 3894 | * Do Insert mode completion. |
| 3895 | * Called when character "c" was typed, which has a meaning for completion. |
| 3896 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 3897 | */ |
| 3898 | static int |
| 3899 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3900 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3902 | char_u *line; |
| 3903 | int startcol = 0; /* column where searched text starts */ |
| 3904 | colnr_T curs_col; /* cursor column */ |
| 3905 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3906 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3907 | compl_direction = ins_compl_key2dir(c); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3908 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3909 | { |
| 3910 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 3911 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3912 | did_ai = FALSE; |
| 3913 | #ifdef FEAT_SMARTINDENT |
| 3914 | did_si = FALSE; |
| 3915 | can_si = FALSE; |
| 3916 | can_si_back = FALSE; |
| 3917 | #endif |
| 3918 | if (stop_arrow() == FAIL) |
| 3919 | return FAIL; |
| 3920 | |
| 3921 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3922 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3923 | |
| 3924 | /* 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] | 3925 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 3926 | * instead of expand the one before the cursor, in word-wise if |
| 3927 | * "compl_startpos" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3928 | * is not in the same line as the cursor then fix it (the line has |
| 3929 | * been split because it was longer than 'tw'). if SOL is set then |
| 3930 | * skip the previous pattern, a word at the beginning of the line has |
| 3931 | * been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3932 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 3933 | && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | { |
| 3935 | /* |
| 3936 | * it is a continued search |
| 3937 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3938 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3939 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 3940 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 3941 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3942 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3943 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3944 | /* line (probably) wrapped, set compl_startpos to the |
| 3945 | * first non_blank in the line, if it is not a wordchar |
| 3946 | * include it to get a better pattern, but then we don't |
| 3947 | * want the "\\<" prefix, check it bellow */ |
| 3948 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 3949 | compl_startpos.col = compl_col; |
| 3950 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 3951 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | } |
| 3953 | else |
| 3954 | { |
| 3955 | /* S_IPOS was set when we inserted a word that was at the |
| 3956 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3957 | * mode but first we need to redefine compl_startpos */ |
| 3958 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3959 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3960 | compl_cont_status |= CONT_SOL; |
| 3961 | compl_startpos.col = (colnr_T)(skipwhite( |
| 3962 | line + compl_length |
| 3963 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3965 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3966 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3967 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3968 | /* 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] | 3969 | * have enough space? just being paranoic */ |
| 3970 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3971 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3972 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3973 | compl_cont_status &= ~CONT_SOL; |
| 3974 | compl_length = (IOSIZE - MIN_SPACE); |
| 3975 | compl_col = curwin->w_cursor.col - compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3976 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3977 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 3978 | if (compl_length < 1) |
| 3979 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3980 | } |
| 3981 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3982 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3983 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3984 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3985 | } |
| 3986 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3987 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3988 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3989 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3990 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3991 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3992 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3993 | compl_cont_status = 0; |
| 3994 | compl_cont_status |= CONT_N_ADDS; |
| 3995 | compl_startpos = curwin->w_cursor; |
| 3996 | startcol = (int)curs_col; |
| 3997 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
| 4000 | /* Work out completion pattern and original text -- webb */ |
| 4001 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 4002 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4003 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4004 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4005 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4006 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4007 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4008 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4010 | compl_col += ++startcol; |
| 4011 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4012 | } |
| 4013 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4014 | compl_pattern = str_foldcase(line + compl_col, |
| 4015 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4016 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4017 | compl_pattern = vim_strnsave(line + compl_col, |
| 4018 | compl_length); |
| 4019 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | return FAIL; |
| 4021 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4022 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4023 | { |
| 4024 | char_u *prefix = (char_u *)"\\<"; |
| 4025 | |
| 4026 | /* we need 3 extra chars, 1 for the NUL and |
| 4027 | * 2 >= strlen(prefix) -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4028 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 4029 | compl_length) + 3); |
| 4030 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4031 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4032 | if (!vim_iswordp(line + compl_col) |
| 4033 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4034 | && ( |
| 4035 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4036 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4037 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4038 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4039 | #endif |
| 4040 | ))) |
| 4041 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4042 | STRCPY((char *)compl_pattern, prefix); |
| 4043 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 4044 | line + compl_col, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4045 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4046 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4047 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4048 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4049 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4050 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4051 | #endif |
| 4052 | ) |
| 4053 | { |
| 4054 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4055 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 4056 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4057 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4058 | compl_col += curs_col; |
| 4059 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4060 | } |
| 4061 | else |
| 4062 | { |
| 4063 | #ifdef FEAT_MBYTE |
| 4064 | /* Search the point of change class of multibyte character |
| 4065 | * or not a word single byte character backward. */ |
| 4066 | if (has_mbyte) |
| 4067 | { |
| 4068 | int base_class; |
| 4069 | int head_off; |
| 4070 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4071 | startcol -= (*mb_head_off)(line, line + startcol); |
| 4072 | base_class = mb_get_class(line + startcol); |
| 4073 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4074 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4075 | head_off = (*mb_head_off)(line, line + startcol); |
| 4076 | if (base_class != mb_get_class(line + startcol |
| 4077 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4078 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4079 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4080 | } |
| 4081 | } |
| 4082 | else |
| 4083 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4084 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4085 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4086 | compl_col += ++startcol; |
| 4087 | compl_length = (int)curs_col - startcol; |
| 4088 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4089 | { |
| 4090 | /* Only match word with at least two chars -- webb |
| 4091 | * there's no need to call quote_meta, |
| 4092 | * alloc(7) is enough -- Acevedo |
| 4093 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4094 | compl_pattern = alloc(7); |
| 4095 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4096 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4097 | STRCPY((char *)compl_pattern, "\\<"); |
| 4098 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 4099 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4100 | } |
| 4101 | else |
| 4102 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4103 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 4104 | compl_length) + 3); |
| 4105 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4106 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4107 | STRCPY((char *)compl_pattern, "\\<"); |
| 4108 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 4109 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4114 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4115 | compl_col = skipwhite(line) - line; |
| 4116 | compl_length = (int)curs_col - (int)compl_col; |
| 4117 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 4118 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4119 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4120 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 4121 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4122 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4123 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4124 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4125 | return FAIL; |
| 4126 | } |
| 4127 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 4128 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4129 | while (--startcol >= 0 && vim_isfilec(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4130 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4131 | compl_col += ++startcol; |
| 4132 | compl_length = (int)curs_col - startcol; |
| 4133 | compl_pattern = addstar(line + compl_col, compl_length, |
| 4134 | EXPAND_FILES); |
| 4135 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4136 | return FAIL; |
| 4137 | } |
| 4138 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 4139 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4140 | compl_pattern = vim_strnsave(line, curs_col); |
| 4141 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4142 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4143 | set_cmd_context(&compl_xp, compl_pattern, |
| 4144 | (int)STRLEN(compl_pattern), curs_col); |
| 4145 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 4146 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4147 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4148 | startcol = (int)(compl_xp.xp_pattern - compl_pattern); |
| 4149 | compl_col = startcol; |
| 4150 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4151 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4152 | 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] | 4153 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4154 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4155 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4156 | * Call user defined function 'completefunc' with "a:findstart" |
| 4157 | * 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] | 4158 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4159 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4160 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4161 | char_u *funcname; |
| 4162 | pos_T pos; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4163 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4164 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4165 | * string */ |
| 4166 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 4167 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 4168 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4169 | { |
| 4170 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 4171 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4172 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4173 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4174 | |
| 4175 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4176 | args[1] = NULL; |
| 4177 | pos = curwin->w_cursor; |
| 4178 | col = call_func_retnr(funcname, 2, args, FALSE); |
| 4179 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4180 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4181 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4182 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4183 | compl_col = col; |
| 4184 | if ((colnr_T)compl_col > curs_col) |
| 4185 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4186 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4187 | /* Setup variables for completion. Need to obtain "line" again, |
| 4188 | * it may have become invalid. */ |
| 4189 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4190 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4191 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4192 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4193 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4194 | return FAIL; |
| 4195 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4196 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 4197 | { |
| 4198 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 4199 | if (spell_bad_len > 0) |
| 4200 | compl_col = curs_col - spell_bad_len; |
| 4201 | else |
| 4202 | compl_col = spell_word_start(startcol); |
| 4203 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4204 | return FAIL; |
Bram Moolenaar | c54b8a7 | 2005-09-30 21:20:29 +0000 | [diff] [blame] | 4205 | spell_expand_check_cap(compl_col); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4206 | compl_length = (int)curs_col - compl_col; |
| 4207 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4208 | if (compl_pattern == NULL) |
| 4209 | #endif |
| 4210 | return FAIL; |
| 4211 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4212 | else |
| 4213 | { |
| 4214 | EMSG2(_(e_intern2), "ins_complete()"); |
| 4215 | return FAIL; |
| 4216 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4218 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4219 | { |
| 4220 | edit_submode_pre = (char_u *)_(" Adding"); |
| 4221 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4222 | { |
| 4223 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 4224 | #ifdef FEAT_COMMENTS |
| 4225 | char_u *old = curbuf->b_p_com; |
| 4226 | |
| 4227 | curbuf->b_p_com = (char_u *)""; |
| 4228 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4229 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4230 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4231 | ins_eol('\r'); |
| 4232 | #ifdef FEAT_COMMENTS |
| 4233 | curbuf->b_p_com = old; |
| 4234 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4235 | compl_length = 0; |
| 4236 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4237 | } |
| 4238 | } |
| 4239 | else |
| 4240 | { |
| 4241 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4242 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4243 | } |
| 4244 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4245 | if (compl_cont_status & CONT_LOCAL) |
| 4246 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4247 | else |
| 4248 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 4249 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4250 | /* Always add completion for the original text. Note that |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4251 | * "compl_orig_text" itself (not a copy) is added, it will be freed |
| 4252 | * when the list of matches is freed. */ |
| 4253 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 4254 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4255 | -1, NULL, NULL, 0, ORIGINAL_TEXT) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4257 | vim_free(compl_pattern); |
| 4258 | compl_pattern = NULL; |
| 4259 | vim_free(compl_orig_text); |
| 4260 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4261 | return FAIL; |
| 4262 | } |
| 4263 | |
| 4264 | /* showmode might reset the internal line pointers, so it must |
| 4265 | * be called before line = ml_get(), or when this address is no |
| 4266 | * longer needed. -- Acevedo. |
| 4267 | */ |
| 4268 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 4269 | edit_submode_highl = HLF_COUNT; |
| 4270 | showmode(); |
| 4271 | edit_submode_extra = NULL; |
| 4272 | out_flush(); |
| 4273 | } |
| 4274 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4275 | compl_shown_match = compl_curr_match; |
| 4276 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4277 | |
| 4278 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4279 | * Find next match (and following matches). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4280 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4281 | n = ins_compl_next(TRUE, ins_compl_key2count(c), c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4282 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4283 | /* may undisplay the popup menu */ |
| 4284 | ins_compl_upd_pum(); |
| 4285 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4286 | if (n > 1) /* all matches have been found */ |
| 4287 | compl_matches = n; |
| 4288 | compl_curr_match = compl_shown_match; |
| 4289 | compl_direction = compl_shows_dir; |
| 4290 | compl_interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4291 | |
| 4292 | /* eat the ESC to avoid leaving insert mode */ |
| 4293 | if (got_int && !global_busy) |
| 4294 | { |
| 4295 | (void)vgetc(); |
| 4296 | got_int = FALSE; |
| 4297 | } |
| 4298 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4299 | /* 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] | 4300 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4302 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 4303 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4304 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 4305 | edit_submode_highl = HLF_E; |
| 4306 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 4307 | * because we couldn't expand anything at first place, but if we used |
| 4308 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 4309 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4310 | if ( compl_length > 1 |
| 4311 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4312 | || (ctrl_x_mode != 0 |
| 4313 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 4314 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4315 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4318 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4319 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4320 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4321 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4322 | |
| 4323 | if (edit_submode_extra == NULL) |
| 4324 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4325 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4326 | { |
| 4327 | edit_submode_extra = (char_u *)_("Back at original"); |
| 4328 | edit_submode_highl = HLF_W; |
| 4329 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4330 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4331 | { |
| 4332 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 4333 | edit_submode_highl = HLF_COUNT; |
| 4334 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4335 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4336 | { |
| 4337 | edit_submode_extra = (char_u *)_("The only match"); |
| 4338 | edit_submode_highl = HLF_COUNT; |
| 4339 | } |
| 4340 | else |
| 4341 | { |
| 4342 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4343 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4344 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4345 | int number = 0; |
| 4346 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4347 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4348 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4349 | { |
| 4350 | /* search backwards for the first valid (!= -1) number. |
| 4351 | * This should normally succeed already at the first loop |
| 4352 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4353 | for (match = compl_curr_match->cp_prev; match != NULL |
| 4354 | && match != compl_first_match; |
| 4355 | match = match->cp_prev) |
| 4356 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4357 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4358 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4359 | break; |
| 4360 | } |
| 4361 | if (match != NULL) |
| 4362 | /* go up and assign all numbers which are not assigned |
| 4363 | * yet */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4364 | for (match = match->cp_next; |
| 4365 | match != NULL && match->cp_number == -1; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4366 | match = match->cp_next) |
| 4367 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4368 | } |
| 4369 | else /* BACKWARD */ |
| 4370 | { |
| 4371 | /* search forwards (upwards) for the first valid (!= -1) |
| 4372 | * number. This should normally succeed already at the |
| 4373 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4374 | for (match = compl_curr_match->cp_next; match != NULL |
| 4375 | && match != compl_first_match; |
| 4376 | match = match->cp_next) |
| 4377 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4378 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4379 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4380 | break; |
| 4381 | } |
| 4382 | if (match != NULL) |
| 4383 | /* go down and assign all numbers which are not |
| 4384 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4385 | for (match = match->cp_prev; match |
| 4386 | && match->cp_number == -1; |
| 4387 | match = match->cp_prev) |
| 4388 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | } |
| 4390 | } |
| 4391 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4392 | /* The match should always have a sequence number now, this is |
| 4393 | * just a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4394 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4395 | { |
| 4396 | /* Space for 10 text chars. + 2x10-digit no.s */ |
| 4397 | static char_u match_ref[31]; |
| 4398 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4399 | if (compl_matches > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4400 | sprintf((char *)IObuff, _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4401 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4402 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4403 | sprintf((char *)IObuff, _("match %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4404 | compl_curr_match->cp_number); |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4405 | vim_strncpy(match_ref, IObuff, 30); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4406 | edit_submode_extra = match_ref; |
| 4407 | edit_submode_highl = HLF_R; |
| 4408 | if (dollar_vcol) |
| 4409 | curs_columns(FALSE); |
| 4410 | } |
| 4411 | } |
| 4412 | } |
| 4413 | |
| 4414 | /* Show a message about what (completion) mode we're in. */ |
| 4415 | showmode(); |
| 4416 | if (edit_submode_extra != NULL) |
| 4417 | { |
| 4418 | if (!p_smd) |
| 4419 | msg_attr(edit_submode_extra, |
| 4420 | edit_submode_highl < HLF_COUNT |
| 4421 | ? hl_attr(edit_submode_highl) : 0); |
| 4422 | } |
| 4423 | else |
| 4424 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 4425 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4426 | ins_compl_show_pum(); |
| 4427 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4428 | return OK; |
| 4429 | } |
| 4430 | |
| 4431 | /* |
| 4432 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 4433 | * If dest is not NULL the chars. are copied there quoting (with |
| 4434 | * a backslash) the metachars, and dest would be NUL terminated. |
| 4435 | * Returns the length (needed) of dest |
| 4436 | */ |
| 4437 | static int |
| 4438 | quote_meta(dest, src, len) |
| 4439 | char_u *dest; |
| 4440 | char_u *src; |
| 4441 | int len; |
| 4442 | { |
| 4443 | int m; |
| 4444 | |
| 4445 | for (m = len; --len >= 0; src++) |
| 4446 | { |
| 4447 | switch (*src) |
| 4448 | { |
| 4449 | case '.': |
| 4450 | case '*': |
| 4451 | case '[': |
| 4452 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 4453 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 4454 | break; |
| 4455 | case '~': |
| 4456 | if (!p_magic) /* quote these only if magic is set */ |
| 4457 | break; |
| 4458 | case '\\': |
| 4459 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 4460 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 4461 | break; |
| 4462 | case '^': /* currently it's not needed. */ |
| 4463 | case '$': |
| 4464 | m++; |
| 4465 | if (dest != NULL) |
| 4466 | *dest++ = '\\'; |
| 4467 | break; |
| 4468 | } |
| 4469 | if (dest != NULL) |
| 4470 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4471 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4472 | /* Copy remaining bytes of a multibyte character. */ |
| 4473 | if (has_mbyte) |
| 4474 | { |
| 4475 | int i, mb_len; |
| 4476 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4477 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4478 | if (mb_len > 0 && len >= mb_len) |
| 4479 | for (i = 0; i < mb_len; ++i) |
| 4480 | { |
| 4481 | --len; |
| 4482 | ++src; |
| 4483 | if (dest != NULL) |
| 4484 | *dest++ = *src; |
| 4485 | } |
| 4486 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4487 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4488 | } |
| 4489 | if (dest != NULL) |
| 4490 | *dest = NUL; |
| 4491 | |
| 4492 | return m; |
| 4493 | } |
| 4494 | #endif /* FEAT_INS_EXPAND */ |
| 4495 | |
| 4496 | /* |
| 4497 | * Next character is interpreted literally. |
| 4498 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 4499 | * If one or two digits are entered, the next character is given to vungetc(). |
| 4500 | * For Unicode a character > 255 may be returned. |
| 4501 | */ |
| 4502 | int |
| 4503 | get_literal() |
| 4504 | { |
| 4505 | int cc; |
| 4506 | int nc; |
| 4507 | int i; |
| 4508 | int hex = FALSE; |
| 4509 | int octal = FALSE; |
| 4510 | #ifdef FEAT_MBYTE |
| 4511 | int unicode = 0; |
| 4512 | #endif |
| 4513 | |
| 4514 | if (got_int) |
| 4515 | return Ctrl_C; |
| 4516 | |
| 4517 | #ifdef FEAT_GUI |
| 4518 | /* |
| 4519 | * In GUI there is no point inserting the internal code for a special key. |
| 4520 | * It is more useful to insert the string "<KEY>" instead. This would |
| 4521 | * probably be useful in a text window too, but it would not be |
| 4522 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 4523 | */ |
| 4524 | if (gui.in_use) |
| 4525 | ++allow_keys; |
| 4526 | #endif |
| 4527 | #ifdef USE_ON_FLY_SCROLL |
| 4528 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 4529 | #endif |
| 4530 | ++no_mapping; /* don't map the next key hits */ |
| 4531 | cc = 0; |
| 4532 | i = 0; |
| 4533 | for (;;) |
| 4534 | { |
| 4535 | do |
| 4536 | nc = safe_vgetc(); |
| 4537 | while (nc == K_IGNORE || nc == K_VER_SCROLLBAR |
| 4538 | || nc == K_HOR_SCROLLBAR); |
| 4539 | #ifdef FEAT_CMDL_INFO |
| 4540 | if (!(State & CMDLINE) |
| 4541 | # ifdef FEAT_MBYTE |
| 4542 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 4543 | # endif |
| 4544 | ) |
| 4545 | add_to_showcmd(nc); |
| 4546 | #endif |
| 4547 | if (nc == 'x' || nc == 'X') |
| 4548 | hex = TRUE; |
| 4549 | else if (nc == 'o' || nc == 'O') |
| 4550 | octal = TRUE; |
| 4551 | #ifdef FEAT_MBYTE |
| 4552 | else if (nc == 'u' || nc == 'U') |
| 4553 | unicode = nc; |
| 4554 | #endif |
| 4555 | else |
| 4556 | { |
| 4557 | if (hex |
| 4558 | #ifdef FEAT_MBYTE |
| 4559 | || unicode != 0 |
| 4560 | #endif |
| 4561 | ) |
| 4562 | { |
| 4563 | if (!vim_isxdigit(nc)) |
| 4564 | break; |
| 4565 | cc = cc * 16 + hex2nr(nc); |
| 4566 | } |
| 4567 | else if (octal) |
| 4568 | { |
| 4569 | if (nc < '0' || nc > '7') |
| 4570 | break; |
| 4571 | cc = cc * 8 + nc - '0'; |
| 4572 | } |
| 4573 | else |
| 4574 | { |
| 4575 | if (!VIM_ISDIGIT(nc)) |
| 4576 | break; |
| 4577 | cc = cc * 10 + nc - '0'; |
| 4578 | } |
| 4579 | |
| 4580 | ++i; |
| 4581 | } |
| 4582 | |
| 4583 | if (cc > 255 |
| 4584 | #ifdef FEAT_MBYTE |
| 4585 | && unicode == 0 |
| 4586 | #endif |
| 4587 | ) |
| 4588 | cc = 255; /* limit range to 0-255 */ |
| 4589 | nc = 0; |
| 4590 | |
| 4591 | if (hex) /* hex: up to two chars */ |
| 4592 | { |
| 4593 | if (i >= 2) |
| 4594 | break; |
| 4595 | } |
| 4596 | #ifdef FEAT_MBYTE |
| 4597 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 4598 | { |
| 4599 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 4600 | break; |
| 4601 | } |
| 4602 | #endif |
| 4603 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 4604 | break; |
| 4605 | } |
| 4606 | if (i == 0) /* no number entered */ |
| 4607 | { |
| 4608 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 4609 | { |
| 4610 | cc = '\n'; |
| 4611 | nc = 0; |
| 4612 | } |
| 4613 | else |
| 4614 | { |
| 4615 | cc = nc; |
| 4616 | nc = 0; |
| 4617 | } |
| 4618 | } |
| 4619 | |
| 4620 | if (cc == 0) /* NUL is stored as NL */ |
| 4621 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 4622 | #ifdef FEAT_MBYTE |
| 4623 | if (enc_dbcs && (cc & 0xff) == 0) |
| 4624 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 4625 | second byte will cause trouble! */ |
| 4626 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4627 | |
| 4628 | --no_mapping; |
| 4629 | #ifdef FEAT_GUI |
| 4630 | if (gui.in_use) |
| 4631 | --allow_keys; |
| 4632 | #endif |
| 4633 | if (nc) |
| 4634 | vungetc(nc); |
| 4635 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 4636 | return cc; |
| 4637 | } |
| 4638 | |
| 4639 | /* |
| 4640 | * Insert character, taking care of special keys and mod_mask |
| 4641 | */ |
| 4642 | static void |
| 4643 | insert_special(c, allow_modmask, ctrlv) |
| 4644 | int c; |
| 4645 | int allow_modmask; |
| 4646 | int ctrlv; /* c was typed after CTRL-V */ |
| 4647 | { |
| 4648 | char_u *p; |
| 4649 | int len; |
| 4650 | |
| 4651 | /* |
| 4652 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 4653 | * inserted with ins_str(), so as not to replace characters in replace |
| 4654 | * mode. |
| 4655 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 4656 | * unless 'allow_modmask' is TRUE. |
| 4657 | */ |
| 4658 | #ifdef MACOS |
| 4659 | /* Command-key never produces a normal key */ |
| 4660 | if (mod_mask & MOD_MASK_CMD) |
| 4661 | allow_modmask = TRUE; |
| 4662 | #endif |
| 4663 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 4664 | { |
| 4665 | p = get_special_key_name(c, mod_mask); |
| 4666 | len = (int)STRLEN(p); |
| 4667 | c = p[len - 1]; |
| 4668 | if (len > 2) |
| 4669 | { |
| 4670 | if (stop_arrow() == FAIL) |
| 4671 | return; |
| 4672 | p[len - 1] = NUL; |
| 4673 | ins_str(p); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 4674 | AppendToRedobuffLit(p, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4675 | ctrlv = FALSE; |
| 4676 | } |
| 4677 | } |
| 4678 | if (stop_arrow() == OK) |
| 4679 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 4680 | } |
| 4681 | |
| 4682 | /* |
| 4683 | * Special characters in this context are those that need processing other |
| 4684 | * than the simple insertion that can be performed here. This includes ESC |
| 4685 | * which terminates the insert, and CR/NL which need special processing to |
| 4686 | * open up a new line. This routine tries to optimize insertions performed by |
| 4687 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 4688 | * stop and defer processing to the "normal" mechanism. |
| 4689 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 4690 | */ |
| 4691 | #ifdef EBCDIC |
| 4692 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 4693 | #else |
| 4694 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 4695 | #endif |
| 4696 | |
| 4697 | #ifdef FEAT_MBYTE |
| 4698 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 4699 | #else |
| 4700 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 4701 | #endif |
| 4702 | |
| 4703 | void |
| 4704 | insertchar(c, flags, second_indent) |
| 4705 | int c; /* character to insert or NUL */ |
| 4706 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 4707 | int second_indent; /* indent for second line if >= 0 */ |
| 4708 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4709 | int textwidth; |
| 4710 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4711 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4712 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4713 | int fo_ins_blank; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4714 | |
| 4715 | textwidth = comp_textwidth(flags & INSCHAR_FORMAT); |
| 4716 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4717 | |
| 4718 | /* |
| 4719 | * Try to break the line in two or more pieces when: |
| 4720 | * - Always do this if we have been called to do formatting only. |
| 4721 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 4722 | * ends in white space. |
| 4723 | * - Otherwise: |
| 4724 | * - Don't do this if inserting a blank |
| 4725 | * - Don't do this if an existing character is being replaced, unless |
| 4726 | * we're in VREPLACE mode. |
| 4727 | * - Do this if the cursor is not on the line where insert started |
| 4728 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 4729 | * before the insert. |
| 4730 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 4731 | * before 'textwidth' |
| 4732 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4733 | if (textwidth > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4734 | && ((flags & INSCHAR_FORMAT) |
| 4735 | || (!vim_iswhite(c) |
| 4736 | && !((State & REPLACE_FLAG) |
| 4737 | #ifdef FEAT_VREPLACE |
| 4738 | && !(State & VREPLACE_FLAG) |
| 4739 | #endif |
| 4740 | && *ml_get_cursor() != NUL) |
| 4741 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 4742 | || ((!has_format_option(FO_INS_LONG) |
| 4743 | || Insstart_textlen <= (colnr_T)textwidth) |
| 4744 | && (!fo_ins_blank |
| 4745 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 4746 | )))))) |
| 4747 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4748 | /* Format with 'formatexpr' when it's set. Use internal formatting |
| 4749 | * when 'formatexpr' isn't set or it returns non-zero. */ |
| 4750 | #if defined(FEAT_EVAL) |
| 4751 | if (*curbuf->b_p_fex == NUL |
| 4752 | || fex_format(curwin->w_cursor.lnum, 1L) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4753 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4754 | internal_format(textwidth, second_indent, flags, c == NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4755 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4756 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | if (c == NUL) /* only formatting was wanted */ |
| 4758 | return; |
| 4759 | |
| 4760 | #ifdef FEAT_COMMENTS |
| 4761 | /* Check whether this character should end a comment. */ |
| 4762 | if (did_ai && (int)c == end_comment_pending) |
| 4763 | { |
| 4764 | char_u *line; |
| 4765 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 4766 | int middle_len, end_len; |
| 4767 | int i; |
| 4768 | |
| 4769 | /* |
| 4770 | * Need to remove existing (middle) comment leader and insert end |
| 4771 | * comment leader. First, check what comment leader we can find. |
| 4772 | */ |
| 4773 | i = get_leader_len(line = ml_get_curline(), &p, FALSE); |
| 4774 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 4775 | { |
| 4776 | /* Skip middle-comment string */ |
| 4777 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 4778 | ++p; |
| 4779 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 4780 | /* Don't count trailing white space for middle_len */ |
| 4781 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 4782 | --middle_len; |
| 4783 | |
| 4784 | /* Find the end-comment string */ |
| 4785 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 4786 | ++p; |
| 4787 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 4788 | |
| 4789 | /* Skip white space before the cursor */ |
| 4790 | i = curwin->w_cursor.col; |
| 4791 | while (--i >= 0 && vim_iswhite(line[i])) |
| 4792 | ; |
| 4793 | i++; |
| 4794 | |
| 4795 | /* Skip to before the middle leader */ |
| 4796 | i -= middle_len; |
| 4797 | |
| 4798 | /* Check some expected things before we go on */ |
| 4799 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 4800 | { |
| 4801 | /* Backspace over all the stuff we want to replace */ |
| 4802 | backspace_until_column(i); |
| 4803 | |
| 4804 | /* |
| 4805 | * Insert the end-comment string, except for the last |
| 4806 | * character, which will get inserted as normal later. |
| 4807 | */ |
| 4808 | ins_bytes_len(lead_end, end_len - 1); |
| 4809 | } |
| 4810 | } |
| 4811 | } |
| 4812 | end_comment_pending = NUL; |
| 4813 | #endif |
| 4814 | |
| 4815 | did_ai = FALSE; |
| 4816 | #ifdef FEAT_SMARTINDENT |
| 4817 | did_si = FALSE; |
| 4818 | can_si = FALSE; |
| 4819 | can_si_back = FALSE; |
| 4820 | #endif |
| 4821 | |
| 4822 | /* |
| 4823 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 4824 | * This speeds up normal text input considerably. |
| 4825 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 4826 | * need to re-indent at a ':', or any other character (but not what |
| 4827 | * 'paste' is set).. |
| 4828 | */ |
| 4829 | #ifdef USE_ON_FLY_SCROLL |
| 4830 | dont_scroll = FALSE; /* allow scrolling here */ |
| 4831 | #endif |
| 4832 | |
| 4833 | if ( !ISSPECIAL(c) |
| 4834 | #ifdef FEAT_MBYTE |
| 4835 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 4836 | #endif |
| 4837 | && vpeekc() != NUL |
| 4838 | && !(State & REPLACE_FLAG) |
| 4839 | #ifdef FEAT_CINDENT |
| 4840 | && !cindent_on() |
| 4841 | #endif |
| 4842 | #ifdef FEAT_RIGHTLEFT |
| 4843 | && !p_ri |
| 4844 | #endif |
| 4845 | ) |
| 4846 | { |
| 4847 | #define INPUT_BUFLEN 100 |
| 4848 | char_u buf[INPUT_BUFLEN + 1]; |
| 4849 | int i; |
| 4850 | colnr_T virtcol = 0; |
| 4851 | |
| 4852 | buf[0] = c; |
| 4853 | i = 1; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4854 | if (textwidth > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4855 | virtcol = get_nolist_virtcol(); |
| 4856 | /* |
| 4857 | * Stop the string when: |
| 4858 | * - no more chars available |
| 4859 | * - finding a special character (command key) |
| 4860 | * - buffer is full |
| 4861 | * - running into the 'textwidth' boundary |
| 4862 | * - need to check for abbreviation: A non-word char after a word-char |
| 4863 | */ |
| 4864 | while ( (c = vpeekc()) != NUL |
| 4865 | && !ISSPECIAL(c) |
| 4866 | #ifdef FEAT_MBYTE |
| 4867 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 4868 | #endif |
| 4869 | && i < INPUT_BUFLEN |
| 4870 | && (textwidth == 0 |
| 4871 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 4872 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 4873 | { |
| 4874 | #ifdef FEAT_RIGHTLEFT |
| 4875 | c = vgetc(); |
| 4876 | if (p_hkmap && KeyTyped) |
| 4877 | c = hkmap(c); /* Hebrew mode mapping */ |
| 4878 | # ifdef FEAT_FKMAP |
| 4879 | if (p_fkmap && KeyTyped) |
| 4880 | c = fkmap(c); /* Farsi mode mapping */ |
| 4881 | # endif |
| 4882 | buf[i++] = c; |
| 4883 | #else |
| 4884 | buf[i++] = vgetc(); |
| 4885 | #endif |
| 4886 | } |
| 4887 | |
| 4888 | #ifdef FEAT_DIGRAPHS |
| 4889 | do_digraph(-1); /* clear digraphs */ |
| 4890 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 4891 | #endif |
| 4892 | buf[i] = NUL; |
| 4893 | ins_str(buf); |
| 4894 | if (flags & INSCHAR_CTRLV) |
| 4895 | { |
| 4896 | redo_literal(*buf); |
| 4897 | i = 1; |
| 4898 | } |
| 4899 | else |
| 4900 | i = 0; |
| 4901 | if (buf[i] != NUL) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 4902 | AppendToRedobuffLit(buf + i, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4903 | } |
| 4904 | else |
| 4905 | { |
| 4906 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4907 | int cc; |
| 4908 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4909 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 4910 | { |
| 4911 | char_u buf[MB_MAXBYTES + 1]; |
| 4912 | |
| 4913 | (*mb_char2bytes)(c, buf); |
| 4914 | buf[cc] = NUL; |
| 4915 | ins_char_bytes(buf, cc); |
| 4916 | AppendCharToRedobuff(c); |
| 4917 | } |
| 4918 | else |
| 4919 | #endif |
| 4920 | { |
| 4921 | ins_char(c); |
| 4922 | if (flags & INSCHAR_CTRLV) |
| 4923 | redo_literal(c); |
| 4924 | else |
| 4925 | AppendCharToRedobuff(c); |
| 4926 | } |
| 4927 | } |
| 4928 | } |
| 4929 | |
| 4930 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4931 | * Format text at the current insert position. |
| 4932 | */ |
| 4933 | static void |
| 4934 | internal_format(textwidth, second_indent, flags, format_only) |
| 4935 | int textwidth; |
| 4936 | int second_indent; |
| 4937 | int flags; |
| 4938 | int format_only; |
| 4939 | { |
| 4940 | int cc; |
| 4941 | int save_char = NUL; |
| 4942 | int haveto_redraw = FALSE; |
| 4943 | int fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 4944 | #ifdef FEAT_MBYTE |
| 4945 | int fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 4946 | #endif |
| 4947 | int fo_white_par = has_format_option(FO_WHITE_PAR); |
| 4948 | int first_line = TRUE; |
| 4949 | #ifdef FEAT_COMMENTS |
| 4950 | colnr_T leader_len; |
| 4951 | int no_leader = FALSE; |
| 4952 | int do_comments = (flags & INSCHAR_DO_COM); |
| 4953 | #endif |
| 4954 | |
| 4955 | /* |
| 4956 | * When 'ai' is off we don't want a space under the cursor to be |
| 4957 | * deleted. Replace it with an 'x' temporarily. |
| 4958 | */ |
| 4959 | if (!curbuf->b_p_ai) |
| 4960 | { |
| 4961 | cc = gchar_cursor(); |
| 4962 | if (vim_iswhite(cc)) |
| 4963 | { |
| 4964 | save_char = cc; |
| 4965 | pchar_cursor('x'); |
| 4966 | } |
| 4967 | } |
| 4968 | |
| 4969 | /* |
| 4970 | * Repeat breaking lines, until the current line is not too long. |
| 4971 | */ |
| 4972 | while (!got_int) |
| 4973 | { |
| 4974 | int startcol; /* Cursor column at entry */ |
| 4975 | int wantcol; /* column at textwidth border */ |
| 4976 | int foundcol; /* column for start of spaces */ |
| 4977 | int end_foundcol = 0; /* column for start of word */ |
| 4978 | colnr_T len; |
| 4979 | colnr_T virtcol; |
| 4980 | #ifdef FEAT_VREPLACE |
| 4981 | int orig_col = 0; |
| 4982 | char_u *saved_text = NULL; |
| 4983 | #endif |
| 4984 | colnr_T col; |
| 4985 | |
| 4986 | virtcol = get_nolist_virtcol(); |
| 4987 | if (virtcol < (colnr_T)textwidth) |
| 4988 | break; |
| 4989 | |
| 4990 | #ifdef FEAT_COMMENTS |
| 4991 | if (no_leader) |
| 4992 | do_comments = FALSE; |
| 4993 | else if (!(flags & INSCHAR_FORMAT) |
| 4994 | && has_format_option(FO_WRAP_COMS)) |
| 4995 | do_comments = TRUE; |
| 4996 | |
| 4997 | /* Don't break until after the comment leader */ |
| 4998 | if (do_comments) |
| 4999 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE); |
| 5000 | else |
| 5001 | leader_len = 0; |
| 5002 | |
| 5003 | /* If the line doesn't start with a comment leader, then don't |
| 5004 | * start one in a following broken line. Avoids that a %word |
| 5005 | * moved to the start of the next line causes all following lines |
| 5006 | * to start with %. */ |
| 5007 | if (leader_len == 0) |
| 5008 | no_leader = TRUE; |
| 5009 | #endif |
| 5010 | if (!(flags & INSCHAR_FORMAT) |
| 5011 | #ifdef FEAT_COMMENTS |
| 5012 | && leader_len == 0 |
| 5013 | #endif |
| 5014 | && !has_format_option(FO_WRAP)) |
| 5015 | |
| 5016 | { |
| 5017 | textwidth = 0; |
| 5018 | break; |
| 5019 | } |
| 5020 | if ((startcol = curwin->w_cursor.col) == 0) |
| 5021 | break; |
| 5022 | |
| 5023 | /* find column of textwidth border */ |
| 5024 | coladvance((colnr_T)textwidth); |
| 5025 | wantcol = curwin->w_cursor.col; |
| 5026 | |
| 5027 | curwin->w_cursor.col = startcol - 1; |
| 5028 | #ifdef FEAT_MBYTE |
| 5029 | /* Correct cursor for multi-byte character. */ |
| 5030 | if (has_mbyte) |
| 5031 | mb_adjust_cursor(); |
| 5032 | #endif |
| 5033 | foundcol = 0; |
| 5034 | |
| 5035 | /* |
| 5036 | * Find position to break at. |
| 5037 | * Stop at first entered white when 'formatoptions' has 'v' |
| 5038 | */ |
| 5039 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
| 5040 | || curwin->w_cursor.lnum != Insstart.lnum |
| 5041 | || curwin->w_cursor.col >= Insstart.col) |
| 5042 | { |
| 5043 | cc = gchar_cursor(); |
| 5044 | if (WHITECHAR(cc)) |
| 5045 | { |
| 5046 | /* remember position of blank just before text */ |
| 5047 | end_foundcol = curwin->w_cursor.col; |
| 5048 | |
| 5049 | /* find start of sequence of blanks */ |
| 5050 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 5051 | { |
| 5052 | dec_cursor(); |
| 5053 | cc = gchar_cursor(); |
| 5054 | } |
| 5055 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 5056 | break; /* only spaces in front of text */ |
| 5057 | #ifdef FEAT_COMMENTS |
| 5058 | /* Don't break until after the comment leader */ |
| 5059 | if (curwin->w_cursor.col < leader_len) |
| 5060 | break; |
| 5061 | #endif |
| 5062 | if (has_format_option(FO_ONE_LETTER)) |
| 5063 | { |
| 5064 | /* do not break after one-letter words */ |
| 5065 | if (curwin->w_cursor.col == 0) |
| 5066 | break; /* one-letter word at begin */ |
| 5067 | |
| 5068 | col = curwin->w_cursor.col; |
| 5069 | dec_cursor(); |
| 5070 | cc = gchar_cursor(); |
| 5071 | |
| 5072 | if (WHITECHAR(cc)) |
| 5073 | continue; /* one-letter, continue */ |
| 5074 | curwin->w_cursor.col = col; |
| 5075 | } |
| 5076 | #ifdef FEAT_MBYTE |
| 5077 | if (has_mbyte) |
| 5078 | foundcol = curwin->w_cursor.col |
| 5079 | + (*mb_ptr2len)(ml_get_cursor()); |
| 5080 | else |
| 5081 | #endif |
| 5082 | foundcol = curwin->w_cursor.col + 1; |
| 5083 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 5084 | break; |
| 5085 | } |
| 5086 | #ifdef FEAT_MBYTE |
| 5087 | else if (cc >= 0x100 && fo_multibyte |
| 5088 | && curwin->w_cursor.col <= (colnr_T)wantcol) |
| 5089 | { |
| 5090 | /* Break after or before a multi-byte character. */ |
| 5091 | foundcol = curwin->w_cursor.col; |
| 5092 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 5093 | foundcol += (*mb_char2len)(cc); |
| 5094 | end_foundcol = foundcol; |
| 5095 | break; |
| 5096 | } |
| 5097 | #endif |
| 5098 | if (curwin->w_cursor.col == 0) |
| 5099 | break; |
| 5100 | dec_cursor(); |
| 5101 | } |
| 5102 | |
| 5103 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 5104 | { |
| 5105 | curwin->w_cursor.col = startcol; |
| 5106 | break; |
| 5107 | } |
| 5108 | |
| 5109 | /* Going to break the line, remove any "$" now. */ |
| 5110 | undisplay_dollar(); |
| 5111 | |
| 5112 | /* |
| 5113 | * Offset between cursor position and line break is used by replace |
| 5114 | * stack functions. VREPLACE does not use this, and backspaces |
| 5115 | * over the text instead. |
| 5116 | */ |
| 5117 | #ifdef FEAT_VREPLACE |
| 5118 | if (State & VREPLACE_FLAG) |
| 5119 | orig_col = startcol; /* Will start backspacing from here */ |
| 5120 | else |
| 5121 | #endif |
| 5122 | replace_offset = startcol - end_foundcol - 1; |
| 5123 | |
| 5124 | /* |
| 5125 | * adjust startcol for spaces that will be deleted and |
| 5126 | * characters that will remain on top line |
| 5127 | */ |
| 5128 | curwin->w_cursor.col = foundcol; |
| 5129 | while (cc = gchar_cursor(), WHITECHAR(cc)) |
| 5130 | inc_cursor(); |
| 5131 | startcol -= curwin->w_cursor.col; |
| 5132 | if (startcol < 0) |
| 5133 | startcol = 0; |
| 5134 | |
| 5135 | #ifdef FEAT_VREPLACE |
| 5136 | if (State & VREPLACE_FLAG) |
| 5137 | { |
| 5138 | /* |
| 5139 | * In VREPLACE mode, we will backspace over the text to be |
| 5140 | * wrapped, so save a copy now to put on the next line. |
| 5141 | */ |
| 5142 | saved_text = vim_strsave(ml_get_cursor()); |
| 5143 | curwin->w_cursor.col = orig_col; |
| 5144 | if (saved_text == NULL) |
| 5145 | break; /* Can't do it, out of memory */ |
| 5146 | saved_text[startcol] = NUL; |
| 5147 | |
| 5148 | /* Backspace over characters that will move to the next line */ |
| 5149 | if (!fo_white_par) |
| 5150 | backspace_until_column(foundcol); |
| 5151 | } |
| 5152 | else |
| 5153 | #endif |
| 5154 | { |
| 5155 | /* put cursor after pos. to break line */ |
| 5156 | if (!fo_white_par) |
| 5157 | curwin->w_cursor.col = foundcol; |
| 5158 | } |
| 5159 | |
| 5160 | /* |
| 5161 | * Split the line just before the margin. |
| 5162 | * Only insert/delete lines, but don't really redraw the window. |
| 5163 | */ |
| 5164 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 5165 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 5166 | #ifdef FEAT_COMMENTS |
| 5167 | + (do_comments ? OPENLINE_DO_COM : 0) |
| 5168 | #endif |
| 5169 | , old_indent); |
| 5170 | old_indent = 0; |
| 5171 | |
| 5172 | replace_offset = 0; |
| 5173 | if (first_line) |
| 5174 | { |
| 5175 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
| 5176 | second_indent = get_number_indent(curwin->w_cursor.lnum -1); |
| 5177 | if (second_indent >= 0) |
| 5178 | { |
| 5179 | #ifdef FEAT_VREPLACE |
| 5180 | if (State & VREPLACE_FLAG) |
| 5181 | change_indent(INDENT_SET, second_indent, FALSE, NUL); |
| 5182 | else |
| 5183 | #endif |
| 5184 | (void)set_indent(second_indent, SIN_CHANGED); |
| 5185 | } |
| 5186 | first_line = FALSE; |
| 5187 | } |
| 5188 | |
| 5189 | #ifdef FEAT_VREPLACE |
| 5190 | if (State & VREPLACE_FLAG) |
| 5191 | { |
| 5192 | /* |
| 5193 | * In VREPLACE mode we have backspaced over the text to be |
| 5194 | * moved, now we re-insert it into the new line. |
| 5195 | */ |
| 5196 | ins_bytes(saved_text); |
| 5197 | vim_free(saved_text); |
| 5198 | } |
| 5199 | else |
| 5200 | #endif |
| 5201 | { |
| 5202 | /* |
| 5203 | * Check if cursor is not past the NUL off the line, cindent |
| 5204 | * may have added or removed indent. |
| 5205 | */ |
| 5206 | curwin->w_cursor.col += startcol; |
| 5207 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 5208 | if (curwin->w_cursor.col > len) |
| 5209 | curwin->w_cursor.col = len; |
| 5210 | } |
| 5211 | |
| 5212 | haveto_redraw = TRUE; |
| 5213 | #ifdef FEAT_CINDENT |
| 5214 | can_cindent = TRUE; |
| 5215 | #endif |
| 5216 | /* moved the cursor, don't autoindent or cindent now */ |
| 5217 | did_ai = FALSE; |
| 5218 | #ifdef FEAT_SMARTINDENT |
| 5219 | did_si = FALSE; |
| 5220 | can_si = FALSE; |
| 5221 | can_si_back = FALSE; |
| 5222 | #endif |
| 5223 | line_breakcheck(); |
| 5224 | } |
| 5225 | |
| 5226 | if (save_char != NUL) /* put back space after cursor */ |
| 5227 | pchar_cursor(save_char); |
| 5228 | |
| 5229 | if (!format_only && haveto_redraw) |
| 5230 | { |
| 5231 | update_topline(); |
| 5232 | redraw_curbuf_later(VALID); |
| 5233 | } |
| 5234 | } |
| 5235 | |
| 5236 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5237 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 5238 | * 'a' flag format from the current line until the end of the paragraph. |
| 5239 | * Keep the cursor at the same position relative to the text. |
| 5240 | * The caller must have saved the cursor line for undo, following ones will be |
| 5241 | * saved here. |
| 5242 | */ |
| 5243 | void |
| 5244 | auto_format(trailblank, prev_line) |
| 5245 | int trailblank; /* when TRUE also format with trailing blank */ |
| 5246 | int prev_line; /* may start in previous line */ |
| 5247 | { |
| 5248 | pos_T pos; |
| 5249 | colnr_T len; |
| 5250 | char_u *old; |
| 5251 | char_u *new, *pnew; |
| 5252 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5253 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5254 | |
| 5255 | if (!has_format_option(FO_AUTO)) |
| 5256 | return; |
| 5257 | |
| 5258 | pos = curwin->w_cursor; |
| 5259 | old = ml_get_curline(); |
| 5260 | |
| 5261 | /* may remove added space */ |
| 5262 | check_auto_format(FALSE); |
| 5263 | |
| 5264 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 5265 | * user might insert normal text next. Also skip formatting when "1" is |
| 5266 | * in 'formatoptions' and there is a single character before the cursor. |
| 5267 | * Otherwise the line would be broken and when typing another non-white |
| 5268 | * next they are not joined back together. */ |
| 5269 | wasatend = (pos.col == STRLEN(old)); |
| 5270 | if (*old != NUL && !trailblank && wasatend) |
| 5271 | { |
| 5272 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5273 | cc = gchar_cursor(); |
| 5274 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 5275 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5276 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5277 | cc = gchar_cursor(); |
| 5278 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5279 | { |
| 5280 | curwin->w_cursor = pos; |
| 5281 | return; |
| 5282 | } |
| 5283 | curwin->w_cursor = pos; |
| 5284 | } |
| 5285 | |
| 5286 | #ifdef FEAT_COMMENTS |
| 5287 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 5288 | * comments. */ |
| 5289 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
| 5290 | && get_leader_len(old, NULL, FALSE) == 0) |
| 5291 | return; |
| 5292 | #endif |
| 5293 | |
| 5294 | /* |
| 5295 | * May start formatting in a previous line, so that after "x" a word is |
| 5296 | * moved to the previous line if it fits there now. Only when this is not |
| 5297 | * the start of a paragraph. |
| 5298 | */ |
| 5299 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 5300 | { |
| 5301 | --curwin->w_cursor.lnum; |
| 5302 | if (u_save_cursor() == FAIL) |
| 5303 | return; |
| 5304 | } |
| 5305 | |
| 5306 | /* |
| 5307 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 5308 | * be adjusted for the text formatting. |
| 5309 | */ |
| 5310 | saved_cursor = pos; |
| 5311 | format_lines((linenr_T)-1); |
| 5312 | curwin->w_cursor = saved_cursor; |
| 5313 | saved_cursor.lnum = 0; |
| 5314 | |
| 5315 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 5316 | { |
| 5317 | /* "cannot happen" */ |
| 5318 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 5319 | coladvance((colnr_T)MAXCOL); |
| 5320 | } |
| 5321 | else |
| 5322 | check_cursor_col(); |
| 5323 | |
| 5324 | /* Insert mode: If the cursor is now after the end of the line while it |
| 5325 | * previously wasn't, the line was broken. Because of the rule above we |
| 5326 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 5327 | * formatted. */ |
| 5328 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 5329 | { |
| 5330 | new = ml_get_curline(); |
| 5331 | len = STRLEN(new); |
| 5332 | if (curwin->w_cursor.col == len) |
| 5333 | { |
| 5334 | pnew = vim_strnsave(new, len + 2); |
| 5335 | pnew[len] = ' '; |
| 5336 | pnew[len + 1] = NUL; |
| 5337 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 5338 | /* remove the space later */ |
| 5339 | did_add_space = TRUE; |
| 5340 | } |
| 5341 | else |
| 5342 | /* may remove added space */ |
| 5343 | check_auto_format(FALSE); |
| 5344 | } |
| 5345 | |
| 5346 | check_cursor(); |
| 5347 | } |
| 5348 | |
| 5349 | /* |
| 5350 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 5351 | * delete it now. The space must be under the cursor, just after the insert |
| 5352 | * position. |
| 5353 | */ |
| 5354 | static void |
| 5355 | check_auto_format(end_insert) |
| 5356 | int end_insert; /* TRUE when ending Insert mode */ |
| 5357 | { |
| 5358 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5359 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5360 | |
| 5361 | if (did_add_space) |
| 5362 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5363 | cc = gchar_cursor(); |
| 5364 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5365 | /* Somehow the space was removed already. */ |
| 5366 | did_add_space = FALSE; |
| 5367 | else |
| 5368 | { |
| 5369 | if (!end_insert) |
| 5370 | { |
| 5371 | inc_cursor(); |
| 5372 | c = gchar_cursor(); |
| 5373 | dec_cursor(); |
| 5374 | } |
| 5375 | if (c != NUL) |
| 5376 | { |
| 5377 | /* The space is no longer at the end of the line, delete it. */ |
| 5378 | del_char(FALSE); |
| 5379 | did_add_space = FALSE; |
| 5380 | } |
| 5381 | } |
| 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | /* |
| 5386 | * Find out textwidth to be used for formatting: |
| 5387 | * if 'textwidth' option is set, use it |
| 5388 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 5389 | * if invalid value, use 0. |
| 5390 | * Set default to window width (maximum 79) for "gq" operator. |
| 5391 | */ |
| 5392 | int |
| 5393 | comp_textwidth(ff) |
| 5394 | int ff; /* force formatting (for "Q" command) */ |
| 5395 | { |
| 5396 | int textwidth; |
| 5397 | |
| 5398 | textwidth = curbuf->b_p_tw; |
| 5399 | if (textwidth == 0 && curbuf->b_p_wm) |
| 5400 | { |
| 5401 | /* The width is the window width minus 'wrapmargin' minus all the |
| 5402 | * things that add to the margin. */ |
| 5403 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 5404 | #ifdef FEAT_CMDWIN |
| 5405 | if (cmdwin_type != 0) |
| 5406 | textwidth -= 1; |
| 5407 | #endif |
| 5408 | #ifdef FEAT_FOLDING |
| 5409 | textwidth -= curwin->w_p_fdc; |
| 5410 | #endif |
| 5411 | #ifdef FEAT_SIGNS |
| 5412 | if (curwin->w_buffer->b_signlist != NULL |
| 5413 | # ifdef FEAT_NETBEANS_INTG |
| 5414 | || usingNetbeans |
| 5415 | # endif |
| 5416 | ) |
| 5417 | textwidth -= 1; |
| 5418 | #endif |
| 5419 | if (curwin->w_p_nu) |
| 5420 | textwidth -= 8; |
| 5421 | } |
| 5422 | if (textwidth < 0) |
| 5423 | textwidth = 0; |
| 5424 | if (ff && textwidth == 0) |
| 5425 | { |
| 5426 | textwidth = W_WIDTH(curwin) - 1; |
| 5427 | if (textwidth > 79) |
| 5428 | textwidth = 79; |
| 5429 | } |
| 5430 | return textwidth; |
| 5431 | } |
| 5432 | |
| 5433 | /* |
| 5434 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 5435 | */ |
| 5436 | static void |
| 5437 | redo_literal(c) |
| 5438 | int c; |
| 5439 | { |
| 5440 | char_u buf[10]; |
| 5441 | |
| 5442 | /* Only digits need special treatment. Translate them into a string of |
| 5443 | * three digits. */ |
| 5444 | if (VIM_ISDIGIT(c)) |
| 5445 | { |
| 5446 | sprintf((char *)buf, "%03d", c); |
| 5447 | AppendToRedobuff(buf); |
| 5448 | } |
| 5449 | else |
| 5450 | AppendCharToRedobuff(c); |
| 5451 | } |
| 5452 | |
| 5453 | /* |
| 5454 | * 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] | 5455 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5456 | */ |
| 5457 | static void |
| 5458 | start_arrow(end_insert_pos) |
| 5459 | pos_T *end_insert_pos; |
| 5460 | { |
| 5461 | if (!arrow_used) /* something has been inserted */ |
| 5462 | { |
| 5463 | AppendToRedobuff(ESC_STR); |
| 5464 | stop_insert(end_insert_pos, FALSE); |
| 5465 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 5466 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5467 | #ifdef FEAT_SYN_HL |
| 5468 | check_spell_redraw(); |
| 5469 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5472 | #ifdef FEAT_SYN_HL |
| 5473 | /* |
| 5474 | * If we skipped highlighting word at cursor, do it now. |
| 5475 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 5476 | */ |
| 5477 | static void |
| 5478 | check_spell_redraw() |
| 5479 | { |
| 5480 | if (spell_redraw_lnum != 0) |
| 5481 | { |
| 5482 | linenr_T lnum = spell_redraw_lnum; |
| 5483 | |
| 5484 | spell_redraw_lnum = 0; |
| 5485 | redrawWinline(lnum, FALSE); |
| 5486 | } |
| 5487 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 5488 | |
| 5489 | /* |
| 5490 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 5491 | * spelled word, if there is one. |
| 5492 | */ |
| 5493 | static void |
| 5494 | spell_back_to_badword() |
| 5495 | { |
| 5496 | pos_T tpos = curwin->w_cursor; |
| 5497 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 5498 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 5499 | if (curwin->w_cursor.col != tpos.col) |
| 5500 | start_arrow(&tpos); |
| 5501 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5502 | #endif |
| 5503 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5504 | /* |
| 5505 | * stop_arrow() is called before a change is made in insert mode. |
| 5506 | * If an arrow key has been used, start a new insertion. |
| 5507 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 5508 | */ |
| 5509 | int |
| 5510 | stop_arrow() |
| 5511 | { |
| 5512 | if (arrow_used) |
| 5513 | { |
| 5514 | if (u_save_cursor() == OK) |
| 5515 | { |
| 5516 | arrow_used = FALSE; |
| 5517 | ins_need_undo = FALSE; |
| 5518 | } |
| 5519 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
| 5520 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 5521 | ai_col = 0; |
| 5522 | #ifdef FEAT_VREPLACE |
| 5523 | if (State & VREPLACE_FLAG) |
| 5524 | { |
| 5525 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 5526 | vr_lines_changed = 1; |
| 5527 | } |
| 5528 | #endif |
| 5529 | ResetRedobuff(); |
| 5530 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 5531 | new_insert_skip = 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5532 | } |
| 5533 | else if (ins_need_undo) |
| 5534 | { |
| 5535 | if (u_save_cursor() == OK) |
| 5536 | ins_need_undo = FALSE; |
| 5537 | } |
| 5538 | |
| 5539 | #ifdef FEAT_FOLDING |
| 5540 | /* Always open fold at the cursor line when inserting something. */ |
| 5541 | foldOpenCursor(); |
| 5542 | #endif |
| 5543 | |
| 5544 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 5545 | } |
| 5546 | |
| 5547 | /* |
| 5548 | * do a few things to stop inserting |
| 5549 | */ |
| 5550 | static void |
| 5551 | stop_insert(end_insert_pos, esc) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5552 | pos_T *end_insert_pos; /* where insert ended */ |
| 5553 | int esc; /* called by ins_esc() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5554 | { |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5555 | int cc; |
| 5556 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5557 | |
| 5558 | stop_redo_ins(); |
| 5559 | replace_flush(); /* abandon replace stack */ |
| 5560 | |
| 5561 | /* |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5562 | * Save the inserted text for later redo with ^@ and CTRL-A. |
| 5563 | * Don't do it when "restart_edit" was set and nothing was inserted, |
| 5564 | * otherwise CTRL-O w and then <Left> will clear "last_insert". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5565 | */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5566 | ptr = get_inserted(); |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 5567 | if (did_restart_edit == 0 || (ptr != NULL |
| 5568 | && (int)STRLEN(ptr) > new_insert_skip)) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5569 | { |
| 5570 | vim_free(last_insert); |
| 5571 | last_insert = ptr; |
| 5572 | last_insert_skip = new_insert_skip; |
| 5573 | } |
| 5574 | else |
| 5575 | vim_free(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5576 | |
| 5577 | if (!arrow_used) |
| 5578 | { |
| 5579 | /* Auto-format now. It may seem strange to do this when stopping an |
| 5580 | * insertion (or moving the cursor), but it's required when appending |
| 5581 | * a line and having it end in a space. But only do it when something |
| 5582 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5583 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5584 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5585 | pos_T tpos = curwin->w_cursor; |
| 5586 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5587 | /* When the cursor is at the end of the line after a space the |
| 5588 | * formatting will move it to the following word. Avoid that by |
| 5589 | * moving the cursor onto the space. */ |
| 5590 | cc = 'x'; |
| 5591 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 5592 | { |
| 5593 | dec_cursor(); |
| 5594 | cc = gchar_cursor(); |
| 5595 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5596 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | auto_format(TRUE, FALSE); |
| 5600 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5601 | if (vim_iswhite(cc)) |
| 5602 | { |
| 5603 | if (gchar_cursor() != NUL) |
| 5604 | inc_cursor(); |
| 5605 | #ifdef FEAT_VIRTUALEDIT |
| 5606 | /* If the cursor is still at the same character, also keep |
| 5607 | * the "coladd". */ |
| 5608 | if (gchar_cursor() == NUL |
| 5609 | && curwin->w_cursor.lnum == tpos.lnum |
| 5610 | && curwin->w_cursor.col == tpos.col) |
| 5611 | curwin->w_cursor.coladd = tpos.coladd; |
| 5612 | #endif |
| 5613 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5614 | } |
| 5615 | |
| 5616 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 5617 | check_auto_format(TRUE); |
| 5618 | |
| 5619 | /* 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] | 5620 | * of the line, and put the cursor back. |
| 5621 | * Do this when ESC was used or moving the cursor up/down. */ |
| 5622 | if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
| 5623 | && curwin->w_cursor.lnum != end_insert_pos->lnum))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5624 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5625 | pos_T tpos = curwin->w_cursor; |
| 5626 | |
| 5627 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5628 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 5629 | --curwin->w_cursor.col; |
| 5630 | while (cc = gchar_cursor(), vim_iswhite(cc)) |
| 5631 | (void)del_char(TRUE); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5632 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 5633 | curwin->w_cursor = tpos; |
| 5634 | else if (cc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5635 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 5636 | |
| 5637 | #ifdef FEAT_VISUAL |
| 5638 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 5639 | * deleted characters. */ |
| 5640 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 5641 | { |
| 5642 | cc = STRLEN(ml_get_curline()); |
| 5643 | if (VIsual.col > (colnr_T)cc) |
| 5644 | { |
| 5645 | VIsual.col = cc; |
| 5646 | # ifdef FEAT_VIRTUALEDIT |
| 5647 | VIsual.coladd = 0; |
| 5648 | # endif |
| 5649 | } |
| 5650 | } |
| 5651 | #endif |
| 5652 | } |
| 5653 | } |
| 5654 | did_ai = FALSE; |
| 5655 | #ifdef FEAT_SMARTINDENT |
| 5656 | did_si = FALSE; |
| 5657 | can_si = FALSE; |
| 5658 | can_si_back = FALSE; |
| 5659 | #endif |
| 5660 | |
| 5661 | /* set '[ and '] to the inserted text */ |
| 5662 | curbuf->b_op_start = Insstart; |
| 5663 | curbuf->b_op_end = *end_insert_pos; |
| 5664 | } |
| 5665 | |
| 5666 | /* |
| 5667 | * Set the last inserted text to a single character. |
| 5668 | * Used for the replace command. |
| 5669 | */ |
| 5670 | void |
| 5671 | set_last_insert(c) |
| 5672 | int c; |
| 5673 | { |
| 5674 | char_u *s; |
| 5675 | |
| 5676 | vim_free(last_insert); |
| 5677 | #ifdef FEAT_MBYTE |
| 5678 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
| 5679 | #else |
| 5680 | last_insert = alloc(6); |
| 5681 | #endif |
| 5682 | if (last_insert != NULL) |
| 5683 | { |
| 5684 | s = last_insert; |
| 5685 | /* Use the CTRL-V only when entering a special char */ |
| 5686 | if (c < ' ' || c == DEL) |
| 5687 | *s++ = Ctrl_V; |
| 5688 | s = add_char2buf(c, s); |
| 5689 | *s++ = ESC; |
| 5690 | *s++ = NUL; |
| 5691 | last_insert_skip = 0; |
| 5692 | } |
| 5693 | } |
| 5694 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 5695 | #if defined(EXITFREE) || defined(PROTO) |
| 5696 | void |
| 5697 | free_last_insert() |
| 5698 | { |
| 5699 | vim_free(last_insert); |
| 5700 | last_insert = NULL; |
| 5701 | } |
| 5702 | #endif |
| 5703 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5704 | /* |
| 5705 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 5706 | * and CSI. Handle multi-byte characters. |
| 5707 | * Returns a pointer to after the added bytes. |
| 5708 | */ |
| 5709 | char_u * |
| 5710 | add_char2buf(c, s) |
| 5711 | int c; |
| 5712 | char_u *s; |
| 5713 | { |
| 5714 | #ifdef FEAT_MBYTE |
| 5715 | char_u temp[MB_MAXBYTES]; |
| 5716 | int i; |
| 5717 | int len; |
| 5718 | |
| 5719 | len = (*mb_char2bytes)(c, temp); |
| 5720 | for (i = 0; i < len; ++i) |
| 5721 | { |
| 5722 | c = temp[i]; |
| 5723 | #endif |
| 5724 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 5725 | if (c == K_SPECIAL) |
| 5726 | { |
| 5727 | *s++ = K_SPECIAL; |
| 5728 | *s++ = KS_SPECIAL; |
| 5729 | *s++ = KE_FILLER; |
| 5730 | } |
| 5731 | #ifdef FEAT_GUI |
| 5732 | else if (c == CSI) |
| 5733 | { |
| 5734 | *s++ = CSI; |
| 5735 | *s++ = KS_EXTRA; |
| 5736 | *s++ = (int)KE_CSI; |
| 5737 | } |
| 5738 | #endif |
| 5739 | else |
| 5740 | *s++ = c; |
| 5741 | #ifdef FEAT_MBYTE |
| 5742 | } |
| 5743 | #endif |
| 5744 | return s; |
| 5745 | } |
| 5746 | |
| 5747 | /* |
| 5748 | * move cursor to start of line |
| 5749 | * if flags & BL_WHITE move to first non-white |
| 5750 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 5751 | * otherwise keep "curswant" column |
| 5752 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 5753 | */ |
| 5754 | void |
| 5755 | beginline(flags) |
| 5756 | int flags; |
| 5757 | { |
| 5758 | if ((flags & BL_SOL) && !p_sol) |
| 5759 | coladvance(curwin->w_curswant); |
| 5760 | else |
| 5761 | { |
| 5762 | curwin->w_cursor.col = 0; |
| 5763 | #ifdef FEAT_VIRTUALEDIT |
| 5764 | curwin->w_cursor.coladd = 0; |
| 5765 | #endif |
| 5766 | |
| 5767 | if (flags & (BL_WHITE | BL_SOL)) |
| 5768 | { |
| 5769 | char_u *ptr; |
| 5770 | |
| 5771 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 5772 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 5773 | ++curwin->w_cursor.col; |
| 5774 | } |
| 5775 | curwin->w_set_curswant = TRUE; |
| 5776 | } |
| 5777 | } |
| 5778 | |
| 5779 | /* |
| 5780 | * oneright oneleft cursor_down cursor_up |
| 5781 | * |
| 5782 | * Move one char {right,left,down,up}. |
| 5783 | * Doesn't move onto the NUL past the end of the line. |
| 5784 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 5785 | */ |
| 5786 | |
| 5787 | int |
| 5788 | oneright() |
| 5789 | { |
| 5790 | char_u *ptr; |
| 5791 | #ifdef FEAT_MBYTE |
| 5792 | int l; |
| 5793 | #endif |
| 5794 | |
| 5795 | #ifdef FEAT_VIRTUALEDIT |
| 5796 | if (virtual_active()) |
| 5797 | { |
| 5798 | pos_T prevpos = curwin->w_cursor; |
| 5799 | |
| 5800 | /* Adjust for multi-wide char (excluding TAB) */ |
| 5801 | ptr = ml_get_cursor(); |
| 5802 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
| 5803 | #ifdef FEAT_MBYTE |
| 5804 | (*mb_ptr2char)(ptr) |
| 5805 | #else |
| 5806 | *ptr |
| 5807 | #endif |
| 5808 | )) |
| 5809 | ? ptr2cells(ptr) : 1)); |
| 5810 | curwin->w_set_curswant = TRUE; |
| 5811 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 5812 | return (prevpos.col != curwin->w_cursor.col |
| 5813 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 5814 | } |
| 5815 | #endif |
| 5816 | |
| 5817 | ptr = ml_get_cursor(); |
| 5818 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5819 | if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5820 | { |
| 5821 | /* The character under the cursor is a multi-byte character, move |
| 5822 | * several bytes right, but don't end up on the NUL. */ |
| 5823 | if (ptr[l] == NUL) |
| 5824 | return FAIL; |
| 5825 | curwin->w_cursor.col += l; |
| 5826 | } |
| 5827 | else |
| 5828 | #endif |
| 5829 | { |
| 5830 | if (*ptr++ == NUL || *ptr == NUL) |
| 5831 | return FAIL; |
| 5832 | ++curwin->w_cursor.col; |
| 5833 | } |
| 5834 | |
| 5835 | curwin->w_set_curswant = TRUE; |
| 5836 | return OK; |
| 5837 | } |
| 5838 | |
| 5839 | int |
| 5840 | oneleft() |
| 5841 | { |
| 5842 | #ifdef FEAT_VIRTUALEDIT |
| 5843 | if (virtual_active()) |
| 5844 | { |
| 5845 | int width; |
| 5846 | int v = getviscol(); |
| 5847 | |
| 5848 | if (v == 0) |
| 5849 | return FAIL; |
| 5850 | |
| 5851 | # ifdef FEAT_LINEBREAK |
| 5852 | /* We might get stuck on 'showbreak', skip over it. */ |
| 5853 | width = 1; |
| 5854 | for (;;) |
| 5855 | { |
| 5856 | coladvance(v - width); |
| 5857 | /* getviscol() is slow, skip it when 'showbreak' is empty and |
| 5858 | * there are no multi-byte characters */ |
| 5859 | if ((*p_sbr == NUL |
| 5860 | # ifdef FEAT_MBYTE |
| 5861 | && !has_mbyte |
| 5862 | # endif |
| 5863 | ) || getviscol() < v) |
| 5864 | break; |
| 5865 | ++width; |
| 5866 | } |
| 5867 | # else |
| 5868 | coladvance(v - 1); |
| 5869 | # endif |
| 5870 | |
| 5871 | if (curwin->w_cursor.coladd == 1) |
| 5872 | { |
| 5873 | char_u *ptr; |
| 5874 | |
| 5875 | /* Adjust for multi-wide char (not a TAB) */ |
| 5876 | ptr = ml_get_cursor(); |
| 5877 | if (*ptr != TAB && vim_isprintc( |
| 5878 | # ifdef FEAT_MBYTE |
| 5879 | (*mb_ptr2char)(ptr) |
| 5880 | # else |
| 5881 | *ptr |
| 5882 | # endif |
| 5883 | ) && ptr2cells(ptr) > 1) |
| 5884 | curwin->w_cursor.coladd = 0; |
| 5885 | } |
| 5886 | |
| 5887 | curwin->w_set_curswant = TRUE; |
| 5888 | return OK; |
| 5889 | } |
| 5890 | #endif |
| 5891 | |
| 5892 | if (curwin->w_cursor.col == 0) |
| 5893 | return FAIL; |
| 5894 | |
| 5895 | curwin->w_set_curswant = TRUE; |
| 5896 | --curwin->w_cursor.col; |
| 5897 | |
| 5898 | #ifdef FEAT_MBYTE |
| 5899 | /* if the character on the left of the current cursor is a multi-byte |
| 5900 | * character, move to its first byte */ |
| 5901 | if (has_mbyte) |
| 5902 | mb_adjust_cursor(); |
| 5903 | #endif |
| 5904 | return OK; |
| 5905 | } |
| 5906 | |
| 5907 | int |
| 5908 | cursor_up(n, upd_topline) |
| 5909 | long n; |
| 5910 | int upd_topline; /* When TRUE: update topline */ |
| 5911 | { |
| 5912 | linenr_T lnum; |
| 5913 | |
| 5914 | if (n > 0) |
| 5915 | { |
| 5916 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 5917 | /* This fails if the cursor is already in the first line or the count |
| 5918 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 5919 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | return FAIL; |
| 5921 | if (n >= lnum) |
| 5922 | lnum = 1; |
| 5923 | else |
| 5924 | #ifdef FEAT_FOLDING |
| 5925 | if (hasAnyFolding(curwin)) |
| 5926 | { |
| 5927 | /* |
| 5928 | * Count each sequence of folded lines as one logical line. |
| 5929 | */ |
| 5930 | /* go to the the start of the current fold */ |
| 5931 | (void)hasFolding(lnum, &lnum, NULL); |
| 5932 | |
| 5933 | while (n--) |
| 5934 | { |
| 5935 | /* move up one line */ |
| 5936 | --lnum; |
| 5937 | if (lnum <= 1) |
| 5938 | break; |
| 5939 | /* If we entered a fold, move to the beginning, unless in |
| 5940 | * Insert mode or when 'foldopen' contains "all": it will open |
| 5941 | * in a moment. */ |
| 5942 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 5943 | (void)hasFolding(lnum, &lnum, NULL); |
| 5944 | } |
| 5945 | if (lnum < 1) |
| 5946 | lnum = 1; |
| 5947 | } |
| 5948 | else |
| 5949 | #endif |
| 5950 | lnum -= n; |
| 5951 | curwin->w_cursor.lnum = lnum; |
| 5952 | } |
| 5953 | |
| 5954 | /* try to advance to the column we want to be at */ |
| 5955 | coladvance(curwin->w_curswant); |
| 5956 | |
| 5957 | if (upd_topline) |
| 5958 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 5959 | |
| 5960 | return OK; |
| 5961 | } |
| 5962 | |
| 5963 | /* |
| 5964 | * Cursor down a number of logical lines. |
| 5965 | */ |
| 5966 | int |
| 5967 | cursor_down(n, upd_topline) |
| 5968 | long n; |
| 5969 | int upd_topline; /* When TRUE: update topline */ |
| 5970 | { |
| 5971 | linenr_T lnum; |
| 5972 | |
| 5973 | if (n > 0) |
| 5974 | { |
| 5975 | lnum = curwin->w_cursor.lnum; |
| 5976 | #ifdef FEAT_FOLDING |
| 5977 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 5978 | (void)hasFolding(lnum, NULL, &lnum); |
| 5979 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 5980 | /* This fails if the cursor is already in the last line or would move |
| 5981 | * beyound the last line and '-' is in 'cpoptions' */ |
| 5982 | if (lnum >= curbuf->b_ml.ml_line_count |
| 5983 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 5984 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5985 | return FAIL; |
| 5986 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 5987 | lnum = curbuf->b_ml.ml_line_count; |
| 5988 | else |
| 5989 | #ifdef FEAT_FOLDING |
| 5990 | if (hasAnyFolding(curwin)) |
| 5991 | { |
| 5992 | linenr_T last; |
| 5993 | |
| 5994 | /* count each sequence of folded lines as one logical line */ |
| 5995 | while (n--) |
| 5996 | { |
| 5997 | if (hasFolding(lnum, NULL, &last)) |
| 5998 | lnum = last + 1; |
| 5999 | else |
| 6000 | ++lnum; |
| 6001 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6002 | break; |
| 6003 | } |
| 6004 | if (lnum > curbuf->b_ml.ml_line_count) |
| 6005 | lnum = curbuf->b_ml.ml_line_count; |
| 6006 | } |
| 6007 | else |
| 6008 | #endif |
| 6009 | lnum += n; |
| 6010 | curwin->w_cursor.lnum = lnum; |
| 6011 | } |
| 6012 | |
| 6013 | /* try to advance to the column we want to be at */ |
| 6014 | coladvance(curwin->w_curswant); |
| 6015 | |
| 6016 | if (upd_topline) |
| 6017 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 6018 | |
| 6019 | return OK; |
| 6020 | } |
| 6021 | |
| 6022 | /* |
| 6023 | * Stuff the last inserted text in the read buffer. |
| 6024 | * Last_insert actually is a copy of the redo buffer, so we |
| 6025 | * first have to remove the command. |
| 6026 | */ |
| 6027 | int |
| 6028 | stuff_inserted(c, count, no_esc) |
| 6029 | int c; /* Command character to be inserted */ |
| 6030 | long count; /* Repeat this many times */ |
| 6031 | int no_esc; /* Don't add an ESC at the end */ |
| 6032 | { |
| 6033 | char_u *esc_ptr; |
| 6034 | char_u *ptr; |
| 6035 | char_u *last_ptr; |
| 6036 | char_u last = NUL; |
| 6037 | |
| 6038 | ptr = get_last_insert(); |
| 6039 | if (ptr == NULL) |
| 6040 | { |
| 6041 | EMSG(_(e_noinstext)); |
| 6042 | return FAIL; |
| 6043 | } |
| 6044 | |
| 6045 | /* may want to stuff the command character, to start Insert mode */ |
| 6046 | if (c != NUL) |
| 6047 | stuffcharReadbuff(c); |
| 6048 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 6049 | *esc_ptr = NUL; /* remove the ESC */ |
| 6050 | |
| 6051 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 6052 | * comes after it OR if it will inserted more than once and "ptr" |
| 6053 | * starts with ^D. -- Acevedo |
| 6054 | */ |
| 6055 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 6056 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 6057 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 6058 | { |
| 6059 | last = *last_ptr; |
| 6060 | *last_ptr = NUL; |
| 6061 | } |
| 6062 | |
| 6063 | do |
| 6064 | { |
| 6065 | stuffReadbuff(ptr); |
| 6066 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 6067 | if (last) |
| 6068 | stuffReadbuff((char_u *)(last == '0' |
| 6069 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 6070 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 6071 | } |
| 6072 | while (--count > 0); |
| 6073 | |
| 6074 | if (last) |
| 6075 | *last_ptr = last; |
| 6076 | |
| 6077 | if (esc_ptr != NULL) |
| 6078 | *esc_ptr = ESC; /* put the ESC back */ |
| 6079 | |
| 6080 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 6081 | if (!no_esc) |
| 6082 | stuffcharReadbuff(ESC); |
| 6083 | |
| 6084 | return OK; |
| 6085 | } |
| 6086 | |
| 6087 | char_u * |
| 6088 | get_last_insert() |
| 6089 | { |
| 6090 | if (last_insert == NULL) |
| 6091 | return NULL; |
| 6092 | return last_insert + last_insert_skip; |
| 6093 | } |
| 6094 | |
| 6095 | /* |
| 6096 | * Get last inserted string, and remove trailing <Esc>. |
| 6097 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 6098 | */ |
| 6099 | char_u * |
| 6100 | get_last_insert_save() |
| 6101 | { |
| 6102 | char_u *s; |
| 6103 | int len; |
| 6104 | |
| 6105 | if (last_insert == NULL) |
| 6106 | return NULL; |
| 6107 | s = vim_strsave(last_insert + last_insert_skip); |
| 6108 | if (s != NULL) |
| 6109 | { |
| 6110 | len = (int)STRLEN(s); |
| 6111 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 6112 | s[len - 1] = NUL; |
| 6113 | } |
| 6114 | return s; |
| 6115 | } |
| 6116 | |
| 6117 | /* |
| 6118 | * Check the word in front of the cursor for an abbreviation. |
| 6119 | * Called when the non-id character "c" has been entered. |
| 6120 | * When an abbreviation is recognized it is removed from the text and |
| 6121 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 6122 | */ |
| 6123 | static int |
| 6124 | echeck_abbr(c) |
| 6125 | int c; |
| 6126 | { |
| 6127 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 6128 | * after moving around with cursor keys. */ |
| 6129 | if (p_paste || no_abbr || arrow_used) |
| 6130 | return FALSE; |
| 6131 | |
| 6132 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 6133 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 6134 | } |
| 6135 | |
| 6136 | /* |
| 6137 | * replace-stack functions |
| 6138 | * |
| 6139 | * When replacing characters, the replaced characters are remembered for each |
| 6140 | * new character. This is used to re-insert the old text when backspacing. |
| 6141 | * |
| 6142 | * There is a NUL headed list of characters for each character that is |
| 6143 | * currently in the file after the insertion point. When BS is used, one NUL |
| 6144 | * headed list is put back for the deleted character. |
| 6145 | * |
| 6146 | * For a newline, there are two NUL headed lists. One contains the characters |
| 6147 | * that the NL replaced. The extra one stores the characters after the cursor |
| 6148 | * that were deleted (always white space). |
| 6149 | * |
| 6150 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 6151 | * character at the end of the stack. If replace_offset is not 0, that many |
| 6152 | * characters will be left on the stack above the newly inserted character. |
| 6153 | */ |
| 6154 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 6155 | static char_u *replace_stack = NULL; |
| 6156 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 6157 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6158 | |
| 6159 | void |
| 6160 | replace_push(c) |
| 6161 | int c; /* character that is replaced (NUL is none) */ |
| 6162 | { |
| 6163 | char_u *p; |
| 6164 | |
| 6165 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 6166 | return; |
| 6167 | if (replace_stack_len <= replace_stack_nr) |
| 6168 | { |
| 6169 | replace_stack_len += 50; |
| 6170 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 6171 | if (p == NULL) /* out of memory */ |
| 6172 | { |
| 6173 | replace_stack_len -= 50; |
| 6174 | return; |
| 6175 | } |
| 6176 | if (replace_stack != NULL) |
| 6177 | { |
| 6178 | mch_memmove(p, replace_stack, |
| 6179 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 6180 | vim_free(replace_stack); |
| 6181 | } |
| 6182 | replace_stack = p; |
| 6183 | } |
| 6184 | p = replace_stack + replace_stack_nr - replace_offset; |
| 6185 | if (replace_offset) |
| 6186 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 6187 | *p = c; |
| 6188 | ++replace_stack_nr; |
| 6189 | } |
| 6190 | |
| 6191 | /* |
| 6192 | * call replace_push(c) with replace_offset set to the first NUL. |
| 6193 | */ |
| 6194 | static void |
| 6195 | replace_push_off(c) |
| 6196 | int c; |
| 6197 | { |
| 6198 | char_u *p; |
| 6199 | |
| 6200 | p = replace_stack + replace_stack_nr; |
| 6201 | for (replace_offset = 1; replace_offset < replace_stack_nr; |
| 6202 | ++replace_offset) |
| 6203 | if (*--p == NUL) |
| 6204 | break; |
| 6205 | replace_push(c); |
| 6206 | replace_offset = 0; |
| 6207 | } |
| 6208 | |
| 6209 | /* |
| 6210 | * Pop one item from the replace stack. |
| 6211 | * return -1 if stack empty |
| 6212 | * return replaced character or NUL otherwise |
| 6213 | */ |
| 6214 | static int |
| 6215 | replace_pop() |
| 6216 | { |
| 6217 | if (replace_stack_nr == 0) |
| 6218 | return -1; |
| 6219 | return (int)replace_stack[--replace_stack_nr]; |
| 6220 | } |
| 6221 | |
| 6222 | /* |
| 6223 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 6224 | * encountered. |
| 6225 | */ |
| 6226 | static void |
| 6227 | replace_join(off) |
| 6228 | int off; /* offset for which NUL to remove */ |
| 6229 | { |
| 6230 | int i; |
| 6231 | |
| 6232 | for (i = replace_stack_nr; --i >= 0; ) |
| 6233 | if (replace_stack[i] == NUL && off-- <= 0) |
| 6234 | { |
| 6235 | --replace_stack_nr; |
| 6236 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 6237 | (size_t)(replace_stack_nr - i)); |
| 6238 | return; |
| 6239 | } |
| 6240 | } |
| 6241 | |
| 6242 | /* |
| 6243 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 6244 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 6245 | */ |
| 6246 | static void |
| 6247 | replace_pop_ins() |
| 6248 | { |
| 6249 | int cc; |
| 6250 | int oldState = State; |
| 6251 | |
| 6252 | State = NORMAL; /* don't want REPLACE here */ |
| 6253 | while ((cc = replace_pop()) > 0) |
| 6254 | { |
| 6255 | #ifdef FEAT_MBYTE |
| 6256 | mb_replace_pop_ins(cc); |
| 6257 | #else |
| 6258 | ins_char(cc); |
| 6259 | #endif |
| 6260 | dec_cursor(); |
| 6261 | } |
| 6262 | State = oldState; |
| 6263 | } |
| 6264 | |
| 6265 | #ifdef FEAT_MBYTE |
| 6266 | /* |
| 6267 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 6268 | * indicates a multi-byte char, pop the other bytes too. |
| 6269 | */ |
| 6270 | static void |
| 6271 | mb_replace_pop_ins(cc) |
| 6272 | int cc; |
| 6273 | { |
| 6274 | int n; |
| 6275 | char_u buf[MB_MAXBYTES]; |
| 6276 | int i; |
| 6277 | int c; |
| 6278 | |
| 6279 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 6280 | { |
| 6281 | buf[0] = cc; |
| 6282 | for (i = 1; i < n; ++i) |
| 6283 | buf[i] = replace_pop(); |
| 6284 | ins_bytes_len(buf, n); |
| 6285 | } |
| 6286 | else |
| 6287 | ins_char(cc); |
| 6288 | |
| 6289 | if (enc_utf8) |
| 6290 | /* Handle composing chars. */ |
| 6291 | for (;;) |
| 6292 | { |
| 6293 | c = replace_pop(); |
| 6294 | if (c == -1) /* stack empty */ |
| 6295 | break; |
| 6296 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 6297 | { |
| 6298 | /* Not a multi-byte char, put it back. */ |
| 6299 | replace_push(c); |
| 6300 | break; |
| 6301 | } |
| 6302 | else |
| 6303 | { |
| 6304 | buf[0] = c; |
| 6305 | for (i = 1; i < n; ++i) |
| 6306 | buf[i] = replace_pop(); |
| 6307 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 6308 | ins_bytes_len(buf, n); |
| 6309 | else |
| 6310 | { |
| 6311 | /* Not a composing char, put it back. */ |
| 6312 | for (i = n - 1; i >= 0; --i) |
| 6313 | replace_push(buf[i]); |
| 6314 | break; |
| 6315 | } |
| 6316 | } |
| 6317 | } |
| 6318 | } |
| 6319 | #endif |
| 6320 | |
| 6321 | /* |
| 6322 | * make the replace stack empty |
| 6323 | * (called when exiting replace mode) |
| 6324 | */ |
| 6325 | static void |
| 6326 | replace_flush() |
| 6327 | { |
| 6328 | vim_free(replace_stack); |
| 6329 | replace_stack = NULL; |
| 6330 | replace_stack_len = 0; |
| 6331 | replace_stack_nr = 0; |
| 6332 | } |
| 6333 | |
| 6334 | /* |
| 6335 | * Handle doing a BS for one character. |
| 6336 | * cc < 0: replace stack empty, just move cursor |
| 6337 | * cc == 0: character was inserted, delete it |
| 6338 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 6339 | * and check for more characters to be put back |
| 6340 | */ |
| 6341 | static void |
| 6342 | replace_do_bs() |
| 6343 | { |
| 6344 | int cc; |
| 6345 | #ifdef FEAT_VREPLACE |
| 6346 | int orig_len = 0; |
| 6347 | int ins_len; |
| 6348 | int orig_vcols = 0; |
| 6349 | colnr_T start_vcol; |
| 6350 | char_u *p; |
| 6351 | int i; |
| 6352 | int vcol; |
| 6353 | #endif |
| 6354 | |
| 6355 | cc = replace_pop(); |
| 6356 | if (cc > 0) |
| 6357 | { |
| 6358 | #ifdef FEAT_VREPLACE |
| 6359 | if (State & VREPLACE_FLAG) |
| 6360 | { |
| 6361 | /* Get the number of screen cells used by the character we are |
| 6362 | * going to delete. */ |
| 6363 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 6364 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 6365 | } |
| 6366 | #endif |
| 6367 | #ifdef FEAT_MBYTE |
| 6368 | if (has_mbyte) |
| 6369 | { |
| 6370 | del_char(FALSE); |
| 6371 | # ifdef FEAT_VREPLACE |
| 6372 | if (State & VREPLACE_FLAG) |
| 6373 | orig_len = STRLEN(ml_get_cursor()); |
| 6374 | # endif |
| 6375 | replace_push(cc); |
| 6376 | } |
| 6377 | else |
| 6378 | #endif |
| 6379 | { |
| 6380 | pchar_cursor(cc); |
| 6381 | #ifdef FEAT_VREPLACE |
| 6382 | if (State & VREPLACE_FLAG) |
| 6383 | orig_len = STRLEN(ml_get_cursor()) - 1; |
| 6384 | #endif |
| 6385 | } |
| 6386 | replace_pop_ins(); |
| 6387 | |
| 6388 | #ifdef FEAT_VREPLACE |
| 6389 | if (State & VREPLACE_FLAG) |
| 6390 | { |
| 6391 | /* Get the number of screen cells used by the inserted characters */ |
| 6392 | p = ml_get_cursor(); |
| 6393 | ins_len = STRLEN(p) - orig_len; |
| 6394 | vcol = start_vcol; |
| 6395 | for (i = 0; i < ins_len; ++i) |
| 6396 | { |
| 6397 | vcol += chartabsize(p + i, vcol); |
| 6398 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6399 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6400 | #endif |
| 6401 | } |
| 6402 | vcol -= start_vcol; |
| 6403 | |
| 6404 | /* Delete spaces that were inserted after the cursor to keep the |
| 6405 | * text aligned. */ |
| 6406 | curwin->w_cursor.col += ins_len; |
| 6407 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 6408 | { |
| 6409 | del_char(FALSE); |
| 6410 | ++orig_vcols; |
| 6411 | } |
| 6412 | curwin->w_cursor.col -= ins_len; |
| 6413 | } |
| 6414 | #endif |
| 6415 | |
| 6416 | /* mark the buffer as changed and prepare for displaying */ |
| 6417 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 6418 | } |
| 6419 | else if (cc == 0) |
| 6420 | (void)del_char(FALSE); |
| 6421 | } |
| 6422 | |
| 6423 | #ifdef FEAT_CINDENT |
| 6424 | /* |
| 6425 | * Return TRUE if C-indenting is on. |
| 6426 | */ |
| 6427 | static int |
| 6428 | cindent_on() |
| 6429 | { |
| 6430 | return (!p_paste && (curbuf->b_p_cin |
| 6431 | # ifdef FEAT_EVAL |
| 6432 | || *curbuf->b_p_inde != NUL |
| 6433 | # endif |
| 6434 | )); |
| 6435 | } |
| 6436 | #endif |
| 6437 | |
| 6438 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 6439 | /* |
| 6440 | * Re-indent the current line, based on the current contents of it and the |
| 6441 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 6442 | * confused what all the part that handles Control-T is doing that I'm not. |
| 6443 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 6444 | */ |
| 6445 | |
| 6446 | void |
| 6447 | fixthisline(get_the_indent) |
| 6448 | int (*get_the_indent) __ARGS((void)); |
| 6449 | { |
| 6450 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0); |
| 6451 | if (linewhite(curwin->w_cursor.lnum)) |
| 6452 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 6453 | } |
| 6454 | |
| 6455 | void |
| 6456 | fix_indent() |
| 6457 | { |
| 6458 | if (p_paste) |
| 6459 | return; |
| 6460 | # ifdef FEAT_LISP |
| 6461 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 6462 | fixthisline(get_lisp_indent); |
| 6463 | # endif |
| 6464 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 6465 | else |
| 6466 | # endif |
| 6467 | # ifdef FEAT_CINDENT |
| 6468 | if (cindent_on()) |
| 6469 | do_c_expr_indent(); |
| 6470 | # endif |
| 6471 | } |
| 6472 | |
| 6473 | #endif |
| 6474 | |
| 6475 | #ifdef FEAT_CINDENT |
| 6476 | /* |
| 6477 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 6478 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 6479 | * when == '!': Only if key is prededed with '!' (don't insert) |
| 6480 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 6481 | * |
| 6482 | * "keytyped" can have a few special values: |
| 6483 | * KEY_OPEN_FORW |
| 6484 | * KEY_OPEN_BACK |
| 6485 | * KEY_COMPLETE just finished completion. |
| 6486 | * |
| 6487 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 6488 | */ |
| 6489 | int |
| 6490 | in_cinkeys(keytyped, when, line_is_empty) |
| 6491 | int keytyped; |
| 6492 | int when; |
| 6493 | int line_is_empty; |
| 6494 | { |
| 6495 | char_u *look; |
| 6496 | int try_match; |
| 6497 | int try_match_word; |
| 6498 | char_u *p; |
| 6499 | char_u *line; |
| 6500 | int icase; |
| 6501 | int i; |
| 6502 | |
| 6503 | #ifdef FEAT_EVAL |
| 6504 | if (*curbuf->b_p_inde != NUL) |
| 6505 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 6506 | else |
| 6507 | #endif |
| 6508 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 6509 | while (*look) |
| 6510 | { |
| 6511 | /* |
| 6512 | * Find out if we want to try a match with this key, depending on |
| 6513 | * 'when' and a '*' or '!' before the key. |
| 6514 | */ |
| 6515 | switch (when) |
| 6516 | { |
| 6517 | case '*': try_match = (*look == '*'); break; |
| 6518 | case '!': try_match = (*look == '!'); break; |
| 6519 | default: try_match = (*look != '*'); break; |
| 6520 | } |
| 6521 | if (*look == '*' || *look == '!') |
| 6522 | ++look; |
| 6523 | |
| 6524 | /* |
| 6525 | * If there is a '0', only accept a match if the line is empty. |
| 6526 | * But may still match when typing last char of a word. |
| 6527 | */ |
| 6528 | if (*look == '0') |
| 6529 | { |
| 6530 | try_match_word = try_match; |
| 6531 | if (!line_is_empty) |
| 6532 | try_match = FALSE; |
| 6533 | ++look; |
| 6534 | } |
| 6535 | else |
| 6536 | try_match_word = FALSE; |
| 6537 | |
| 6538 | /* |
| 6539 | * does it look like a control character? |
| 6540 | */ |
| 6541 | if (*look == '^' |
| 6542 | #ifdef EBCDIC |
| 6543 | && (Ctrl_chr(look[1]) != 0) |
| 6544 | #else |
| 6545 | && look[1] >= '?' && look[1] <= '_' |
| 6546 | #endif |
| 6547 | ) |
| 6548 | { |
| 6549 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 6550 | return TRUE; |
| 6551 | look += 2; |
| 6552 | } |
| 6553 | /* |
| 6554 | * 'o' means "o" command, open forward. |
| 6555 | * 'O' means "O" command, open backward. |
| 6556 | */ |
| 6557 | else if (*look == 'o') |
| 6558 | { |
| 6559 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 6560 | return TRUE; |
| 6561 | ++look; |
| 6562 | } |
| 6563 | else if (*look == 'O') |
| 6564 | { |
| 6565 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 6566 | return TRUE; |
| 6567 | ++look; |
| 6568 | } |
| 6569 | |
| 6570 | /* |
| 6571 | * 'e' means to check for "else" at start of line and just before the |
| 6572 | * cursor. |
| 6573 | */ |
| 6574 | else if (*look == 'e') |
| 6575 | { |
| 6576 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 6577 | { |
| 6578 | p = ml_get_curline(); |
| 6579 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 6580 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 6581 | return TRUE; |
| 6582 | } |
| 6583 | ++look; |
| 6584 | } |
| 6585 | |
| 6586 | /* |
| 6587 | * ':' only causes an indent if it is at the end of a label or case |
| 6588 | * statement, or when it was before typing the ':' (to fix |
| 6589 | * class::method for C++). |
| 6590 | */ |
| 6591 | else if (*look == ':') |
| 6592 | { |
| 6593 | if (try_match && keytyped == ':') |
| 6594 | { |
| 6595 | p = ml_get_curline(); |
| 6596 | if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30)) |
| 6597 | return TRUE; |
| 6598 | if (curwin->w_cursor.col > 2 |
| 6599 | && p[curwin->w_cursor.col - 1] == ':' |
| 6600 | && p[curwin->w_cursor.col - 2] == ':') |
| 6601 | { |
| 6602 | p[curwin->w_cursor.col - 1] = ' '; |
| 6603 | i = (cin_iscase(p) || cin_isscopedecl(p) |
| 6604 | || cin_islabel(30)); |
| 6605 | p = ml_get_curline(); |
| 6606 | p[curwin->w_cursor.col - 1] = ':'; |
| 6607 | if (i) |
| 6608 | return TRUE; |
| 6609 | } |
| 6610 | } |
| 6611 | ++look; |
| 6612 | } |
| 6613 | |
| 6614 | |
| 6615 | /* |
| 6616 | * Is it a key in <>, maybe? |
| 6617 | */ |
| 6618 | else if (*look == '<') |
| 6619 | { |
| 6620 | if (try_match) |
| 6621 | { |
| 6622 | /* |
| 6623 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 6624 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 6625 | * >, *, : and ! keys if they really really want to. |
| 6626 | */ |
| 6627 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 6628 | && keytyped == look[1]) |
| 6629 | return TRUE; |
| 6630 | |
| 6631 | if (keytyped == get_special_key_code(look + 1)) |
| 6632 | return TRUE; |
| 6633 | } |
| 6634 | while (*look && *look != '>') |
| 6635 | look++; |
| 6636 | while (*look == '>') |
| 6637 | look++; |
| 6638 | } |
| 6639 | |
| 6640 | /* |
| 6641 | * Is it a word: "=word"? |
| 6642 | */ |
| 6643 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 6644 | { |
| 6645 | ++look; |
| 6646 | if (*look == '~') |
| 6647 | { |
| 6648 | icase = TRUE; |
| 6649 | ++look; |
| 6650 | } |
| 6651 | else |
| 6652 | icase = FALSE; |
| 6653 | p = vim_strchr(look, ','); |
| 6654 | if (p == NULL) |
| 6655 | p = look + STRLEN(look); |
| 6656 | if ((try_match || try_match_word) |
| 6657 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 6658 | { |
| 6659 | int match = FALSE; |
| 6660 | |
| 6661 | #ifdef FEAT_INS_EXPAND |
| 6662 | if (keytyped == KEY_COMPLETE) |
| 6663 | { |
| 6664 | char_u *s; |
| 6665 | |
| 6666 | /* Just completed a word, check if it starts with "look". |
| 6667 | * search back for the start of a word. */ |
| 6668 | line = ml_get_curline(); |
| 6669 | # ifdef FEAT_MBYTE |
| 6670 | if (has_mbyte) |
| 6671 | { |
| 6672 | char_u *n; |
| 6673 | |
| 6674 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 6675 | { |
| 6676 | n = mb_prevptr(line, s); |
| 6677 | if (!vim_iswordp(n)) |
| 6678 | break; |
| 6679 | } |
| 6680 | } |
| 6681 | else |
| 6682 | # endif |
| 6683 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 6684 | if (!vim_iswordc(s[-1])) |
| 6685 | break; |
| 6686 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 6687 | && (icase |
| 6688 | ? MB_STRNICMP(s, look, p - look) |
| 6689 | : STRNCMP(s, look, p - look)) == 0) |
| 6690 | match = TRUE; |
| 6691 | } |
| 6692 | else |
| 6693 | #endif |
| 6694 | /* TODO: multi-byte */ |
| 6695 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 6696 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 6697 | { |
| 6698 | line = ml_get_cursor(); |
| 6699 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 6700 | || !vim_iswordc(line[-(p - look) - 1])) |
| 6701 | && (icase |
| 6702 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 6703 | : STRNCMP(line - (p - look), look, p - look)) |
| 6704 | == 0) |
| 6705 | match = TRUE; |
| 6706 | } |
| 6707 | if (match && try_match_word && !try_match) |
| 6708 | { |
| 6709 | /* "0=word": Check if there are only blanks before the |
| 6710 | * word. */ |
| 6711 | line = ml_get_curline(); |
| 6712 | if ((int)(skipwhite(line) - line) != |
| 6713 | (int)(curwin->w_cursor.col - (p - look))) |
| 6714 | match = FALSE; |
| 6715 | } |
| 6716 | if (match) |
| 6717 | return TRUE; |
| 6718 | } |
| 6719 | look = p; |
| 6720 | } |
| 6721 | |
| 6722 | /* |
| 6723 | * ok, it's a boring generic character. |
| 6724 | */ |
| 6725 | else |
| 6726 | { |
| 6727 | if (try_match && *look == keytyped) |
| 6728 | return TRUE; |
| 6729 | ++look; |
| 6730 | } |
| 6731 | |
| 6732 | /* |
| 6733 | * Skip over ", ". |
| 6734 | */ |
| 6735 | look = skip_to_option_part(look); |
| 6736 | } |
| 6737 | return FALSE; |
| 6738 | } |
| 6739 | #endif /* FEAT_CINDENT */ |
| 6740 | |
| 6741 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 6742 | /* |
| 6743 | * Map Hebrew keyboard when in hkmap mode. |
| 6744 | */ |
| 6745 | int |
| 6746 | hkmap(c) |
| 6747 | int c; |
| 6748 | { |
| 6749 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 6750 | { |
| 6751 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 6752 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 6753 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 6754 | static char_u map[26] = |
| 6755 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 6756 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 6757 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 6758 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 6759 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 6760 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 6761 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 6762 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 6763 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 6764 | |
| 6765 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 6766 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 6767 | /* '-1'='sofit' */ |
| 6768 | else if (c == 'x') |
| 6769 | return 'X'; |
| 6770 | else if (c == 'q') |
| 6771 | return '\''; /* {geresh}={'} */ |
| 6772 | else if (c == 246) |
| 6773 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 6774 | else if (c == 228) |
| 6775 | return ' '; /* \"a --> ' ' -- / -- */ |
| 6776 | else if (c == 252) |
| 6777 | return ' '; /* \"u --> ' ' -- / -- */ |
| 6778 | #ifdef EBCDIC |
| 6779 | else if (islower(c)) |
| 6780 | #else |
| 6781 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 6782 | * do this the same was as 5.7 and previous, so it works correctly on |
| 6783 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 6784 | * munged and won't work if e.g. searching for Hebrew text. |
| 6785 | */ |
| 6786 | else if (c >= 'a' && c <= 'z') |
| 6787 | #endif |
| 6788 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 6789 | else |
| 6790 | return c; |
| 6791 | } |
| 6792 | else |
| 6793 | { |
| 6794 | switch (c) |
| 6795 | { |
| 6796 | case '`': return ';'; |
| 6797 | case '/': return '.'; |
| 6798 | case '\'': return ','; |
| 6799 | case 'q': return '/'; |
| 6800 | case 'w': return '\''; |
| 6801 | |
| 6802 | /* Hebrew letters - set offset from 'a' */ |
| 6803 | case ',': c = '{'; break; |
| 6804 | case '.': c = 'v'; break; |
| 6805 | case ';': c = 't'; break; |
| 6806 | default: { |
| 6807 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 6808 | |
| 6809 | #ifdef EBCDIC |
| 6810 | /* see note about islower() above */ |
| 6811 | if (!islower(c)) |
| 6812 | #else |
| 6813 | if (c < 'a' || c > 'z') |
| 6814 | #endif |
| 6815 | return c; |
| 6816 | c = str[CharOrdLow(c)]; |
| 6817 | break; |
| 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | return (int)(CharOrdLow(c) + p_aleph); |
| 6822 | } |
| 6823 | } |
| 6824 | #endif |
| 6825 | |
| 6826 | static void |
| 6827 | ins_reg() |
| 6828 | { |
| 6829 | int need_redraw = FALSE; |
| 6830 | int regname; |
| 6831 | int literally = 0; |
| 6832 | |
| 6833 | /* |
| 6834 | * If we are going to wait for a character, show a '"'. |
| 6835 | */ |
| 6836 | pc_status = PC_STATUS_UNSET; |
| 6837 | if (redrawing() && !char_avail()) |
| 6838 | { |
| 6839 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 6840 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6841 | |
| 6842 | edit_putchar('"', TRUE); |
| 6843 | #ifdef FEAT_CMDL_INFO |
| 6844 | add_to_showcmd_c(Ctrl_R); |
| 6845 | #endif |
| 6846 | } |
| 6847 | |
| 6848 | #ifdef USE_ON_FLY_SCROLL |
| 6849 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 6850 | #endif |
| 6851 | |
| 6852 | /* |
| 6853 | * Don't map the register name. This also prevents the mode message to be |
| 6854 | * deleted when ESC is hit. |
| 6855 | */ |
| 6856 | ++no_mapping; |
| 6857 | regname = safe_vgetc(); |
| 6858 | #ifdef FEAT_LANGMAP |
| 6859 | LANGMAP_ADJUST(regname, TRUE); |
| 6860 | #endif |
| 6861 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 6862 | { |
| 6863 | /* Get a third key for literal register insertion */ |
| 6864 | literally = regname; |
| 6865 | #ifdef FEAT_CMDL_INFO |
| 6866 | add_to_showcmd_c(literally); |
| 6867 | #endif |
| 6868 | regname = safe_vgetc(); |
| 6869 | #ifdef FEAT_LANGMAP |
| 6870 | LANGMAP_ADJUST(regname, TRUE); |
| 6871 | #endif |
| 6872 | } |
| 6873 | --no_mapping; |
| 6874 | |
| 6875 | #ifdef FEAT_EVAL |
| 6876 | /* |
| 6877 | * Don't call u_sync() while getting the expression, |
| 6878 | * evaluating it or giving an error message for it! |
| 6879 | */ |
| 6880 | ++no_u_sync; |
| 6881 | if (regname == '=') |
| 6882 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6883 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6884 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6885 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6886 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6887 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6888 | /* Restore the Input Method. */ |
| 6889 | if (im_on) |
| 6890 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6891 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6892 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 6893 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 6894 | { |
| 6895 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6896 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 6897 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | else |
| 6899 | { |
| 6900 | #endif |
| 6901 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 6902 | { |
| 6903 | /* Append the command to the redo buffer. */ |
| 6904 | AppendCharToRedobuff(Ctrl_R); |
| 6905 | AppendCharToRedobuff(literally); |
| 6906 | AppendCharToRedobuff(regname); |
| 6907 | |
| 6908 | do_put(regname, BACKWARD, 1L, |
| 6909 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 6910 | } |
| 6911 | else if (insert_reg(regname, literally) == FAIL) |
| 6912 | { |
| 6913 | vim_beep(); |
| 6914 | need_redraw = TRUE; /* remove the '"' */ |
| 6915 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6916 | else if (stop_insert_mode) |
| 6917 | /* When the '=' register was used and a function was invoked that |
| 6918 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 6919 | * insert anything, need to remove the '"' */ |
| 6920 | need_redraw = TRUE; |
| 6921 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6922 | #ifdef FEAT_EVAL |
| 6923 | } |
| 6924 | --no_u_sync; |
| 6925 | #endif |
| 6926 | #ifdef FEAT_CMDL_INFO |
| 6927 | clear_showcmd(); |
| 6928 | #endif |
| 6929 | |
| 6930 | /* If the inserted register is empty, we need to remove the '"' */ |
| 6931 | if (need_redraw || stuff_empty()) |
| 6932 | edit_unputchar(); |
| 6933 | } |
| 6934 | |
| 6935 | /* |
| 6936 | * CTRL-G commands in Insert mode. |
| 6937 | */ |
| 6938 | static void |
| 6939 | ins_ctrl_g() |
| 6940 | { |
| 6941 | int c; |
| 6942 | |
| 6943 | #ifdef FEAT_INS_EXPAND |
| 6944 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 6945 | setcursor(); |
| 6946 | #endif |
| 6947 | |
| 6948 | /* |
| 6949 | * Don't map the second key. This also prevents the mode message to be |
| 6950 | * deleted when ESC is hit. |
| 6951 | */ |
| 6952 | ++no_mapping; |
| 6953 | c = safe_vgetc(); |
| 6954 | --no_mapping; |
| 6955 | switch (c) |
| 6956 | { |
| 6957 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 6958 | case K_UP: |
| 6959 | case Ctrl_K: |
| 6960 | case 'k': ins_up(TRUE); |
| 6961 | break; |
| 6962 | |
| 6963 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 6964 | case K_DOWN: |
| 6965 | case Ctrl_J: |
| 6966 | case 'j': ins_down(TRUE); |
| 6967 | break; |
| 6968 | |
| 6969 | /* CTRL-G u: start new undoable edit */ |
| 6970 | case 'u': u_sync(); |
| 6971 | ins_need_undo = TRUE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 6972 | |
| 6973 | /* Need to reset Insstart, esp. because a BS that joins |
| 6974 | * aline to the previous one must save for undo. */ |
| 6975 | Insstart = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6976 | break; |
| 6977 | |
| 6978 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 6979 | default: vim_beep(); |
| 6980 | } |
| 6981 | } |
| 6982 | |
| 6983 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 6984 | * CTRL-^ in Insert mode. |
| 6985 | */ |
| 6986 | static void |
| 6987 | ins_ctrl_hat() |
| 6988 | { |
| 6989 | if (map_to_exists_mode((char_u *)"", LANGMAP)) |
| 6990 | { |
| 6991 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 6992 | if (State & LANGMAP) |
| 6993 | { |
| 6994 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 6995 | State &= ~LANGMAP; |
| 6996 | } |
| 6997 | else |
| 6998 | { |
| 6999 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 7000 | State |= LANGMAP; |
| 7001 | #ifdef USE_IM_CONTROL |
| 7002 | im_set_active(FALSE); |
| 7003 | #endif |
| 7004 | } |
| 7005 | } |
| 7006 | #ifdef USE_IM_CONTROL |
| 7007 | else |
| 7008 | { |
| 7009 | /* There are no ":lmap" mappings, toggle IM */ |
| 7010 | if (im_get_status()) |
| 7011 | { |
| 7012 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 7013 | im_set_active(FALSE); |
| 7014 | } |
| 7015 | else |
| 7016 | { |
| 7017 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 7018 | State &= ~LANGMAP; |
| 7019 | im_set_active(TRUE); |
| 7020 | } |
| 7021 | } |
| 7022 | #endif |
| 7023 | set_iminsert_global(); |
| 7024 | showmode(); |
| 7025 | #ifdef FEAT_GUI |
| 7026 | /* may show different cursor shape or color */ |
| 7027 | if (gui.in_use) |
| 7028 | gui_update_cursor(TRUE, FALSE); |
| 7029 | #endif |
| 7030 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 7031 | /* Show/unshow value of 'keymap' in status lines. */ |
| 7032 | status_redraw_curbuf(); |
| 7033 | #endif |
| 7034 | } |
| 7035 | |
| 7036 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7037 | * Handle ESC in insert mode. |
| 7038 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 7039 | * insert. |
| 7040 | */ |
| 7041 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7042 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7043 | long *count; |
| 7044 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7045 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7046 | { |
| 7047 | int temp; |
| 7048 | static int disabled_redraw = FALSE; |
| 7049 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7050 | #ifdef FEAT_SYN_HL |
| 7051 | check_spell_redraw(); |
| 7052 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7053 | #if defined(FEAT_HANGULIN) |
| 7054 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 7055 | hangul_input_state_set(0); |
| 7056 | # endif |
| 7057 | if (composing_hangul) |
| 7058 | { |
| 7059 | push_raw_key(composing_hangul_buffer, 2); |
| 7060 | composing_hangul = 0; |
| 7061 | } |
| 7062 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | |
| 7064 | temp = curwin->w_cursor.col; |
| 7065 | if (disabled_redraw) |
| 7066 | { |
| 7067 | --RedrawingDisabled; |
| 7068 | disabled_redraw = FALSE; |
| 7069 | } |
| 7070 | if (!arrow_used) |
| 7071 | { |
| 7072 | /* |
| 7073 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7074 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 7075 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7076 | */ |
| 7077 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7078 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7079 | |
| 7080 | /* |
| 7081 | * Repeating insert may take a long time. Check for |
| 7082 | * interrupt now and then. |
| 7083 | */ |
| 7084 | if (*count > 0) |
| 7085 | { |
| 7086 | line_breakcheck(); |
| 7087 | if (got_int) |
| 7088 | *count = 0; |
| 7089 | } |
| 7090 | |
| 7091 | if (--*count > 0) /* repeat what was typed */ |
| 7092 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 7093 | /* Vi repeats the insert without replacing characters. */ |
| 7094 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 7095 | State &= ~REPLACE_FLAG; |
| 7096 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7097 | (void)start_redo_ins(); |
| 7098 | if (cmdchar == 'r' || cmdchar == 'v') |
| 7099 | stuffReadbuff(ESC_STR); /* no ESC in redo buffer */ |
| 7100 | ++RedrawingDisabled; |
| 7101 | disabled_redraw = TRUE; |
| 7102 | return FALSE; /* repeat the insert */ |
| 7103 | } |
| 7104 | stop_insert(&curwin->w_cursor, TRUE); |
| 7105 | undisplay_dollar(); |
| 7106 | } |
| 7107 | |
| 7108 | /* When an autoindent was removed, curswant stays after the |
| 7109 | * indent */ |
| 7110 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 7111 | curwin->w_set_curswant = TRUE; |
| 7112 | |
| 7113 | /* Remember the last Insert position in the '^ mark. */ |
| 7114 | if (!cmdmod.keepjumps) |
| 7115 | curbuf->b_last_insert = curwin->w_cursor; |
| 7116 | |
| 7117 | /* |
| 7118 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7119 | * 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] | 7120 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7121 | if (!nomove |
| 7122 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7123 | #ifdef FEAT_VIRTUALEDIT |
| 7124 | || curwin->w_cursor.coladd > 0 |
| 7125 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7126 | ) |
| 7127 | && (restart_edit == NUL |
| 7128 | || (gchar_cursor() == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7129 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7130 | && !VIsual_active |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7131 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7132 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7133 | #ifdef FEAT_RIGHTLEFT |
| 7134 | && !revins_on |
| 7135 | #endif |
| 7136 | ) |
| 7137 | { |
| 7138 | #ifdef FEAT_VIRTUALEDIT |
| 7139 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 7140 | { |
| 7141 | oneleft(); |
| 7142 | if (restart_edit != NUL) |
| 7143 | ++curwin->w_cursor.coladd; |
| 7144 | } |
| 7145 | else |
| 7146 | #endif |
| 7147 | { |
| 7148 | --curwin->w_cursor.col; |
| 7149 | #ifdef FEAT_MBYTE |
| 7150 | /* Correct cursor for multi-byte character. */ |
| 7151 | if (has_mbyte) |
| 7152 | mb_adjust_cursor(); |
| 7153 | #endif |
| 7154 | } |
| 7155 | } |
| 7156 | |
| 7157 | #ifdef USE_IM_CONTROL |
| 7158 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 7159 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 7160 | * well). */ |
| 7161 | if (!(State & LANGMAP)) |
| 7162 | im_save_status(&curbuf->b_p_iminsert); |
| 7163 | im_set_active(FALSE); |
| 7164 | #endif |
| 7165 | |
| 7166 | State = NORMAL; |
| 7167 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 7168 | changed_cline_bef_curs(); |
| 7169 | |
| 7170 | #ifdef FEAT_MOUSE |
| 7171 | setmouse(); |
| 7172 | #endif |
| 7173 | #ifdef CURSOR_SHAPE |
| 7174 | ui_cursor_shape(); /* may show different cursor shape */ |
| 7175 | #endif |
| 7176 | |
| 7177 | /* |
| 7178 | * When recording or for CTRL-O, need to display the new mode. |
| 7179 | * Otherwise remove the mode message. |
| 7180 | */ |
| 7181 | if (Recording || restart_edit != NUL) |
| 7182 | showmode(); |
| 7183 | else if (p_smd) |
| 7184 | MSG(""); |
| 7185 | |
| 7186 | return TRUE; /* exit Insert mode */ |
| 7187 | } |
| 7188 | |
| 7189 | #ifdef FEAT_RIGHTLEFT |
| 7190 | /* |
| 7191 | * Toggle language: hkmap and revins_on. |
| 7192 | * Move to end of reverse inserted text. |
| 7193 | */ |
| 7194 | static void |
| 7195 | ins_ctrl_() |
| 7196 | { |
| 7197 | if (revins_on && revins_chars && revins_scol >= 0) |
| 7198 | { |
| 7199 | while (gchar_cursor() != NUL && revins_chars--) |
| 7200 | ++curwin->w_cursor.col; |
| 7201 | } |
| 7202 | p_ri = !p_ri; |
| 7203 | revins_on = (State == INSERT && p_ri); |
| 7204 | if (revins_on) |
| 7205 | { |
| 7206 | revins_scol = curwin->w_cursor.col; |
| 7207 | revins_legal++; |
| 7208 | revins_chars = 0; |
| 7209 | undisplay_dollar(); |
| 7210 | } |
| 7211 | else |
| 7212 | revins_scol = -1; |
| 7213 | #ifdef FEAT_FKMAP |
| 7214 | if (p_altkeymap) |
| 7215 | { |
| 7216 | /* |
| 7217 | * to be consistent also for redo command, using '.' |
| 7218 | * set arrow_used to true and stop it - causing to redo |
| 7219 | * characters entered in one mode (normal/reverse insert). |
| 7220 | */ |
| 7221 | arrow_used = TRUE; |
| 7222 | (void)stop_arrow(); |
| 7223 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 7224 | if (p_fkmap && p_ri) |
| 7225 | State = INSERT; |
| 7226 | } |
| 7227 | else |
| 7228 | #endif |
| 7229 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 7230 | showmode(); |
| 7231 | } |
| 7232 | #endif |
| 7233 | |
| 7234 | #ifdef FEAT_VISUAL |
| 7235 | /* |
| 7236 | * If 'keymodel' contains "startsel", may start selection. |
| 7237 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 7238 | */ |
| 7239 | static int |
| 7240 | ins_start_select(c) |
| 7241 | int c; |
| 7242 | { |
| 7243 | if (km_startsel) |
| 7244 | switch (c) |
| 7245 | { |
| 7246 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7247 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7248 | case K_PAGEUP: |
| 7249 | case K_KPAGEUP: |
| 7250 | case K_PAGEDOWN: |
| 7251 | case K_KPAGEDOWN: |
| 7252 | # ifdef MACOS |
| 7253 | case K_LEFT: |
| 7254 | case K_RIGHT: |
| 7255 | case K_UP: |
| 7256 | case K_DOWN: |
| 7257 | case K_END: |
| 7258 | case K_HOME: |
| 7259 | # endif |
| 7260 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 7261 | break; |
| 7262 | /* FALLTHROUGH */ |
| 7263 | case K_S_LEFT: |
| 7264 | case K_S_RIGHT: |
| 7265 | case K_S_UP: |
| 7266 | case K_S_DOWN: |
| 7267 | case K_S_END: |
| 7268 | case K_S_HOME: |
| 7269 | /* Start selection right away, the cursor can move with |
| 7270 | * CTRL-O when beyond the end of the line. */ |
| 7271 | start_selection(); |
| 7272 | |
| 7273 | /* Execute the key in (insert) Select mode. */ |
| 7274 | stuffcharReadbuff(Ctrl_O); |
| 7275 | if (mod_mask) |
| 7276 | { |
| 7277 | char_u buf[4]; |
| 7278 | |
| 7279 | buf[0] = K_SPECIAL; |
| 7280 | buf[1] = KS_MODIFIER; |
| 7281 | buf[2] = mod_mask; |
| 7282 | buf[3] = NUL; |
| 7283 | stuffReadbuff(buf); |
| 7284 | } |
| 7285 | stuffcharReadbuff(c); |
| 7286 | return TRUE; |
| 7287 | } |
| 7288 | return FALSE; |
| 7289 | } |
| 7290 | #endif |
| 7291 | |
| 7292 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7293 | * <Insert> key in Insert mode: toggle insert/remplace mode. |
| 7294 | */ |
| 7295 | static void |
| 7296 | ins_insert(replaceState) |
| 7297 | int replaceState; |
| 7298 | { |
| 7299 | #ifdef FEAT_FKMAP |
| 7300 | if (p_fkmap && p_ri) |
| 7301 | { |
| 7302 | beep_flush(); |
| 7303 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 7304 | return; |
| 7305 | } |
| 7306 | #endif |
| 7307 | |
| 7308 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 7309 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7310 | set_vim_var_string(VV_INSERTMODE, |
| 7311 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7312 | # ifdef FEAT_VREPLACE |
| 7313 | replaceState == VREPLACE ? "v" : |
| 7314 | # endif |
| 7315 | "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 7316 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7317 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 7318 | #endif |
| 7319 | if (State & REPLACE_FLAG) |
| 7320 | State = INSERT | (State & LANGMAP); |
| 7321 | else |
| 7322 | State = replaceState | (State & LANGMAP); |
| 7323 | AppendCharToRedobuff(K_INS); |
| 7324 | showmode(); |
| 7325 | #ifdef CURSOR_SHAPE |
| 7326 | ui_cursor_shape(); /* may show different cursor shape */ |
| 7327 | #endif |
| 7328 | } |
| 7329 | |
| 7330 | /* |
| 7331 | * Pressed CTRL-O in Insert mode. |
| 7332 | */ |
| 7333 | static void |
| 7334 | ins_ctrl_o() |
| 7335 | { |
| 7336 | #ifdef FEAT_VREPLACE |
| 7337 | if (State & VREPLACE_FLAG) |
| 7338 | restart_edit = 'V'; |
| 7339 | else |
| 7340 | #endif |
| 7341 | if (State & REPLACE_FLAG) |
| 7342 | restart_edit = 'R'; |
| 7343 | else |
| 7344 | restart_edit = 'I'; |
| 7345 | #ifdef FEAT_VIRTUALEDIT |
| 7346 | if (virtual_active()) |
| 7347 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 7348 | else |
| 7349 | #endif |
| 7350 | ins_at_eol = (gchar_cursor() == NUL); |
| 7351 | } |
| 7352 | |
| 7353 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7354 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 7355 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
| 7356 | * Always round the indent to 'shiftwith', this is compatible |
| 7357 | * with vi. But vi only supports ^T and ^D after an |
| 7358 | * autoindent, we support it everywhere. |
| 7359 | */ |
| 7360 | static void |
| 7361 | ins_shift(c, lastc) |
| 7362 | int c; |
| 7363 | int lastc; |
| 7364 | { |
| 7365 | if (stop_arrow() == FAIL) |
| 7366 | return; |
| 7367 | AppendCharToRedobuff(c); |
| 7368 | |
| 7369 | /* |
| 7370 | * 0^D and ^^D: remove all indent. |
| 7371 | */ |
| 7372 | if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col) |
| 7373 | { |
| 7374 | --curwin->w_cursor.col; |
| 7375 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 7376 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 7377 | if (State & REPLACE_FLAG) |
| 7378 | replace_pop_ins(); |
| 7379 | if (lastc == '^') |
| 7380 | old_indent = get_indent(); /* remember curr. indent */ |
| 7381 | change_indent(INDENT_SET, 0, TRUE, 0); |
| 7382 | } |
| 7383 | else |
| 7384 | change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0); |
| 7385 | |
| 7386 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 7387 | did_ai = FALSE; |
| 7388 | #ifdef FEAT_SMARTINDENT |
| 7389 | did_si = FALSE; |
| 7390 | can_si = FALSE; |
| 7391 | can_si_back = FALSE; |
| 7392 | #endif |
| 7393 | #ifdef FEAT_CINDENT |
| 7394 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 7395 | #endif |
| 7396 | } |
| 7397 | |
| 7398 | static void |
| 7399 | ins_del() |
| 7400 | { |
| 7401 | int temp; |
| 7402 | |
| 7403 | if (stop_arrow() == FAIL) |
| 7404 | return; |
| 7405 | if (gchar_cursor() == NUL) /* delete newline */ |
| 7406 | { |
| 7407 | temp = curwin->w_cursor.col; |
| 7408 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
| 7409 | || u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 7410 | (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL |
| 7411 | || do_join(FALSE) == FAIL) |
| 7412 | vim_beep(); |
| 7413 | else |
| 7414 | curwin->w_cursor.col = temp; |
| 7415 | } |
| 7416 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 7417 | vim_beep(); |
| 7418 | did_ai = FALSE; |
| 7419 | #ifdef FEAT_SMARTINDENT |
| 7420 | did_si = FALSE; |
| 7421 | can_si = FALSE; |
| 7422 | can_si_back = FALSE; |
| 7423 | #endif |
| 7424 | AppendCharToRedobuff(K_DEL); |
| 7425 | } |
| 7426 | |
| 7427 | /* |
| 7428 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 7429 | * Return TRUE when backspace was actually used. |
| 7430 | */ |
| 7431 | static int |
| 7432 | ins_bs(c, mode, inserted_space_p) |
| 7433 | int c; |
| 7434 | int mode; |
| 7435 | int *inserted_space_p; |
| 7436 | { |
| 7437 | linenr_T lnum; |
| 7438 | int cc; |
| 7439 | int temp = 0; /* init for GCC */ |
| 7440 | colnr_T mincol; |
| 7441 | int did_backspace = FALSE; |
| 7442 | int in_indent; |
| 7443 | int oldState; |
| 7444 | #ifdef FEAT_MBYTE |
| 7445 | int p1, p2; |
| 7446 | #endif |
| 7447 | |
| 7448 | /* |
| 7449 | * can't delete anything in an empty file |
| 7450 | * can't backup past first character in buffer |
| 7451 | * can't backup past starting point unless 'backspace' > 1 |
| 7452 | * can backup to a previous line if 'backspace' == 0 |
| 7453 | */ |
| 7454 | if ( bufempty() |
| 7455 | || ( |
| 7456 | #ifdef FEAT_RIGHTLEFT |
| 7457 | !revins_on && |
| 7458 | #endif |
| 7459 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 7460 | || (!can_bs(BS_START) |
| 7461 | && (arrow_used |
| 7462 | || (curwin->w_cursor.lnum == Insstart.lnum |
| 7463 | && curwin->w_cursor.col <= Insstart.col))) |
| 7464 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 7465 | && curwin->w_cursor.col <= ai_col) |
| 7466 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 7467 | { |
| 7468 | vim_beep(); |
| 7469 | return FALSE; |
| 7470 | } |
| 7471 | |
| 7472 | if (stop_arrow() == FAIL) |
| 7473 | return FALSE; |
| 7474 | in_indent = inindent(0); |
| 7475 | #ifdef FEAT_CINDENT |
| 7476 | if (in_indent) |
| 7477 | can_cindent = FALSE; |
| 7478 | #endif |
| 7479 | #ifdef FEAT_COMMENTS |
| 7480 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 7481 | #endif |
| 7482 | #ifdef FEAT_RIGHTLEFT |
| 7483 | if (revins_on) /* put cursor after last inserted char */ |
| 7484 | inc_cursor(); |
| 7485 | #endif |
| 7486 | |
| 7487 | #ifdef FEAT_VIRTUALEDIT |
| 7488 | /* Virtualedit: |
| 7489 | * BACKSPACE_CHAR eats a virtual space |
| 7490 | * BACKSPACE_WORD eats all coladd |
| 7491 | * BACKSPACE_LINE eats all coladd and keeps going |
| 7492 | */ |
| 7493 | if (curwin->w_cursor.coladd > 0) |
| 7494 | { |
| 7495 | if (mode == BACKSPACE_CHAR) |
| 7496 | { |
| 7497 | --curwin->w_cursor.coladd; |
| 7498 | return TRUE; |
| 7499 | } |
| 7500 | if (mode == BACKSPACE_WORD) |
| 7501 | { |
| 7502 | curwin->w_cursor.coladd = 0; |
| 7503 | return TRUE; |
| 7504 | } |
| 7505 | curwin->w_cursor.coladd = 0; |
| 7506 | } |
| 7507 | #endif |
| 7508 | |
| 7509 | /* |
| 7510 | * delete newline! |
| 7511 | */ |
| 7512 | if (curwin->w_cursor.col == 0) |
| 7513 | { |
| 7514 | lnum = Insstart.lnum; |
| 7515 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7516 | #ifdef FEAT_RIGHTLEFT |
| 7517 | || revins_on |
| 7518 | #endif |
| 7519 | ) |
| 7520 | { |
| 7521 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 7522 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 7523 | return FALSE; |
| 7524 | --Insstart.lnum; |
| 7525 | Insstart.col = MAXCOL; |
| 7526 | } |
| 7527 | /* |
| 7528 | * In replace mode: |
| 7529 | * cc < 0: NL was inserted, delete it |
| 7530 | * cc >= 0: NL was replaced, put original characters back |
| 7531 | */ |
| 7532 | cc = -1; |
| 7533 | if (State & REPLACE_FLAG) |
| 7534 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 7535 | /* |
| 7536 | * In replace mode, in the line we started replacing, we only move the |
| 7537 | * cursor. |
| 7538 | */ |
| 7539 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 7540 | { |
| 7541 | dec_cursor(); |
| 7542 | } |
| 7543 | else |
| 7544 | { |
| 7545 | #ifdef FEAT_VREPLACE |
| 7546 | if (!(State & VREPLACE_FLAG) |
| 7547 | || curwin->w_cursor.lnum > orig_line_count) |
| 7548 | #endif |
| 7549 | { |
| 7550 | temp = gchar_cursor(); /* remember current char */ |
| 7551 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 7552 | |
| 7553 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 7554 | * the end of the line, otherwise the line will be broken |
| 7555 | * again when auto-formatting. */ |
| 7556 | if (has_format_option(FO_AUTO) |
| 7557 | && has_format_option(FO_WHITE_PAR)) |
| 7558 | { |
| 7559 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 7560 | TRUE); |
| 7561 | int len; |
| 7562 | |
| 7563 | len = STRLEN(ptr); |
| 7564 | if (len > 0 && ptr[len - 1] == ' ') |
| 7565 | ptr[len - 1] = NUL; |
| 7566 | } |
| 7567 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7568 | (void)do_join(FALSE); |
| 7569 | if (temp == NUL && gchar_cursor() != NUL) |
| 7570 | inc_cursor(); |
| 7571 | } |
| 7572 | #ifdef FEAT_VREPLACE |
| 7573 | else |
| 7574 | dec_cursor(); |
| 7575 | #endif |
| 7576 | |
| 7577 | /* |
| 7578 | * In REPLACE mode we have to put back the text that was replaced |
| 7579 | * by the NL. On the replace stack is first a NUL-terminated |
| 7580 | * sequence of characters that were deleted and then the |
| 7581 | * characters that NL replaced. |
| 7582 | */ |
| 7583 | if (State & REPLACE_FLAG) |
| 7584 | { |
| 7585 | /* |
| 7586 | * Do the next ins_char() in NORMAL state, to |
| 7587 | * prevent ins_char() from replacing characters and |
| 7588 | * avoiding showmatch(). |
| 7589 | */ |
| 7590 | oldState = State; |
| 7591 | State = NORMAL; |
| 7592 | /* |
| 7593 | * restore characters (blanks) deleted after cursor |
| 7594 | */ |
| 7595 | while (cc > 0) |
| 7596 | { |
| 7597 | temp = curwin->w_cursor.col; |
| 7598 | #ifdef FEAT_MBYTE |
| 7599 | mb_replace_pop_ins(cc); |
| 7600 | #else |
| 7601 | ins_char(cc); |
| 7602 | #endif |
| 7603 | curwin->w_cursor.col = temp; |
| 7604 | cc = replace_pop(); |
| 7605 | } |
| 7606 | /* restore the characters that NL replaced */ |
| 7607 | replace_pop_ins(); |
| 7608 | State = oldState; |
| 7609 | } |
| 7610 | } |
| 7611 | did_ai = FALSE; |
| 7612 | } |
| 7613 | else |
| 7614 | { |
| 7615 | /* |
| 7616 | * Delete character(s) before the cursor. |
| 7617 | */ |
| 7618 | #ifdef FEAT_RIGHTLEFT |
| 7619 | if (revins_on) /* put cursor on last inserted char */ |
| 7620 | dec_cursor(); |
| 7621 | #endif |
| 7622 | mincol = 0; |
| 7623 | /* keep indent */ |
| 7624 | if (mode == BACKSPACE_LINE && curbuf->b_p_ai |
| 7625 | #ifdef FEAT_RIGHTLEFT |
| 7626 | && !revins_on |
| 7627 | #endif |
| 7628 | ) |
| 7629 | { |
| 7630 | temp = curwin->w_cursor.col; |
| 7631 | beginline(BL_WHITE); |
| 7632 | if (curwin->w_cursor.col < (colnr_T)temp) |
| 7633 | mincol = curwin->w_cursor.col; |
| 7634 | curwin->w_cursor.col = temp; |
| 7635 | } |
| 7636 | |
| 7637 | /* |
| 7638 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 7639 | */ |
| 7640 | if ( mode == BACKSPACE_CHAR |
| 7641 | && ((p_sta && in_indent) |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7642 | || (curbuf->b_p_sts != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7643 | && (*(ml_get_cursor() - 1) == TAB |
| 7644 | || (*(ml_get_cursor() - 1) == ' ' |
| 7645 | && (!*inserted_space_p |
| 7646 | || arrow_used)))))) |
| 7647 | { |
| 7648 | int ts; |
| 7649 | colnr_T vcol; |
| 7650 | colnr_T want_vcol; |
| 7651 | int extra = 0; |
| 7652 | |
| 7653 | *inserted_space_p = FALSE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7654 | if (p_sta && in_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7655 | ts = curbuf->b_p_sw; |
| 7656 | else |
| 7657 | ts = curbuf->b_p_sts; |
| 7658 | /* Compute the virtual column where we want to be. Since |
| 7659 | * 'showbreak' may get in the way, need to get the last column of |
| 7660 | * the previous character. */ |
| 7661 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 7662 | dec_cursor(); |
| 7663 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 7664 | inc_cursor(); |
| 7665 | want_vcol = (want_vcol / ts) * ts; |
| 7666 | |
| 7667 | /* delete characters until we are at or before want_vcol */ |
| 7668 | while (vcol > want_vcol |
| 7669 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
| 7670 | { |
| 7671 | dec_cursor(); |
| 7672 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 7673 | if (State & REPLACE_FLAG) |
| 7674 | { |
| 7675 | /* Don't delete characters before the insert point when in |
| 7676 | * Replace mode */ |
| 7677 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 7678 | || curwin->w_cursor.col >= Insstart.col) |
| 7679 | { |
| 7680 | #if 0 /* what was this for? It causes problems when sw != ts. */ |
| 7681 | if (State == REPLACE && (int)vcol < want_vcol) |
| 7682 | { |
| 7683 | (void)del_char(FALSE); |
| 7684 | extra = 2; /* don't pop too much */ |
| 7685 | } |
| 7686 | else |
| 7687 | #endif |
| 7688 | replace_do_bs(); |
| 7689 | } |
| 7690 | } |
| 7691 | else |
| 7692 | (void)del_char(FALSE); |
| 7693 | } |
| 7694 | |
| 7695 | /* insert extra spaces until we are at want_vcol */ |
| 7696 | while (vcol < want_vcol) |
| 7697 | { |
| 7698 | /* Remember the first char we inserted */ |
| 7699 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7700 | && curwin->w_cursor.col < Insstart.col) |
| 7701 | Insstart.col = curwin->w_cursor.col; |
| 7702 | |
| 7703 | #ifdef FEAT_VREPLACE |
| 7704 | if (State & VREPLACE_FLAG) |
| 7705 | ins_char(' '); |
| 7706 | else |
| 7707 | #endif |
| 7708 | { |
| 7709 | ins_str((char_u *)" "); |
| 7710 | if ((State & REPLACE_FLAG) && extra <= 1) |
| 7711 | { |
| 7712 | if (extra) |
| 7713 | replace_push_off(NUL); |
| 7714 | else |
| 7715 | replace_push(NUL); |
| 7716 | } |
| 7717 | if (extra == 2) |
| 7718 | extra = 1; |
| 7719 | } |
| 7720 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 7721 | } |
| 7722 | } |
| 7723 | |
| 7724 | /* |
| 7725 | * Delete upto starting point, start of line or previous word. |
| 7726 | */ |
| 7727 | else do |
| 7728 | { |
| 7729 | #ifdef FEAT_RIGHTLEFT |
| 7730 | if (!revins_on) /* put cursor on char to be deleted */ |
| 7731 | #endif |
| 7732 | dec_cursor(); |
| 7733 | |
| 7734 | /* start of word? */ |
| 7735 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 7736 | { |
| 7737 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 7738 | temp = vim_iswordc(gchar_cursor()); |
| 7739 | } |
| 7740 | /* end of word? */ |
| 7741 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 7742 | && (vim_isspace(cc = gchar_cursor()) |
| 7743 | || vim_iswordc(cc) != temp)) |
| 7744 | { |
| 7745 | #ifdef FEAT_RIGHTLEFT |
| 7746 | if (!revins_on) |
| 7747 | #endif |
| 7748 | inc_cursor(); |
| 7749 | #ifdef FEAT_RIGHTLEFT |
| 7750 | else if (State & REPLACE_FLAG) |
| 7751 | dec_cursor(); |
| 7752 | #endif |
| 7753 | break; |
| 7754 | } |
| 7755 | if (State & REPLACE_FLAG) |
| 7756 | replace_do_bs(); |
| 7757 | else |
| 7758 | { |
| 7759 | #ifdef FEAT_MBYTE |
| 7760 | if (enc_utf8 && p_deco) |
| 7761 | (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2); |
| 7762 | #endif |
| 7763 | (void)del_char(FALSE); |
| 7764 | #ifdef FEAT_MBYTE |
| 7765 | /* |
| 7766 | * If p1 or p2 is non-zero, there are combining characters we |
| 7767 | * need to take account of. Don't back up before the base |
| 7768 | * character. |
| 7769 | */ |
| 7770 | if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL)) |
| 7771 | inc_cursor(); |
| 7772 | #endif |
| 7773 | #ifdef FEAT_RIGHTLEFT |
| 7774 | if (revins_chars) |
| 7775 | { |
| 7776 | revins_chars--; |
| 7777 | revins_legal++; |
| 7778 | } |
| 7779 | if (revins_on && gchar_cursor() == NUL) |
| 7780 | break; |
| 7781 | #endif |
| 7782 | } |
| 7783 | /* Just a single backspace?: */ |
| 7784 | if (mode == BACKSPACE_CHAR) |
| 7785 | break; |
| 7786 | } while ( |
| 7787 | #ifdef FEAT_RIGHTLEFT |
| 7788 | revins_on || |
| 7789 | #endif |
| 7790 | (curwin->w_cursor.col > mincol |
| 7791 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 7792 | || curwin->w_cursor.col != Insstart.col))); |
| 7793 | did_backspace = TRUE; |
| 7794 | } |
| 7795 | #ifdef FEAT_SMARTINDENT |
| 7796 | did_si = FALSE; |
| 7797 | can_si = FALSE; |
| 7798 | can_si_back = FALSE; |
| 7799 | #endif |
| 7800 | if (curwin->w_cursor.col <= 1) |
| 7801 | did_ai = FALSE; |
| 7802 | /* |
| 7803 | * It's a little strange to put backspaces into the redo |
| 7804 | * buffer, but it makes auto-indent a lot easier to deal |
| 7805 | * with. |
| 7806 | */ |
| 7807 | AppendCharToRedobuff(c); |
| 7808 | |
| 7809 | /* If deleted before the insertion point, adjust it */ |
| 7810 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7811 | && curwin->w_cursor.col < Insstart.col) |
| 7812 | Insstart.col = curwin->w_cursor.col; |
| 7813 | |
| 7814 | /* vi behaviour: the cursor moves backward but the character that |
| 7815 | * was there remains visible |
| 7816 | * Vim behaviour: the cursor moves backward and the character that |
| 7817 | * was there is erased from the screen. |
| 7818 | * We can emulate the vi behaviour by pretending there is a dollar |
| 7819 | * displayed even when there isn't. |
| 7820 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
| 7821 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0) |
| 7822 | dollar_vcol = curwin->w_virtcol; |
| 7823 | |
| 7824 | return did_backspace; |
| 7825 | } |
| 7826 | |
| 7827 | #ifdef FEAT_MOUSE |
| 7828 | static void |
| 7829 | ins_mouse(c) |
| 7830 | int c; |
| 7831 | { |
| 7832 | pos_T tpos; |
| 7833 | |
| 7834 | # ifdef FEAT_GUI |
| 7835 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 7836 | if (!gui.in_use) |
| 7837 | # endif |
| 7838 | if (!mouse_has(MOUSE_INSERT)) |
| 7839 | return; |
| 7840 | |
| 7841 | undisplay_dollar(); |
| 7842 | tpos = curwin->w_cursor; |
| 7843 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 7844 | { |
| 7845 | start_arrow(&tpos); |
| 7846 | # ifdef FEAT_CINDENT |
| 7847 | can_cindent = TRUE; |
| 7848 | # endif |
| 7849 | } |
| 7850 | |
| 7851 | #ifdef FEAT_WINDOWS |
| 7852 | /* redraw status lines (in case another window became active) */ |
| 7853 | redraw_statuslines(); |
| 7854 | #endif |
| 7855 | } |
| 7856 | |
| 7857 | static void |
| 7858 | ins_mousescroll(up) |
| 7859 | int up; |
| 7860 | { |
| 7861 | pos_T tpos; |
| 7862 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7863 | win_T *old_curwin; |
| 7864 | # endif |
| 7865 | |
| 7866 | tpos = curwin->w_cursor; |
| 7867 | |
| 7868 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7869 | old_curwin = curwin; |
| 7870 | |
| 7871 | /* Currently the mouse coordinates are only known in the GUI. */ |
| 7872 | if (gui.in_use && mouse_row >= 0 && mouse_col >= 0) |
| 7873 | { |
| 7874 | int row, col; |
| 7875 | |
| 7876 | row = mouse_row; |
| 7877 | col = mouse_col; |
| 7878 | |
| 7879 | /* find the window at the pointer coordinates */ |
| 7880 | curwin = mouse_find_win(&row, &col); |
| 7881 | curbuf = curwin->w_buffer; |
| 7882 | } |
| 7883 | if (curwin == old_curwin) |
| 7884 | # endif |
| 7885 | undisplay_dollar(); |
| 7886 | |
| 7887 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 7888 | scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline)); |
| 7889 | else |
| 7890 | scroll_redraw(up, 3L); |
| 7891 | |
| 7892 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7893 | curwin->w_redr_status = TRUE; |
| 7894 | |
| 7895 | curwin = old_curwin; |
| 7896 | curbuf = curwin->w_buffer; |
| 7897 | # endif |
| 7898 | |
| 7899 | if (!equalpos(curwin->w_cursor, tpos)) |
| 7900 | { |
| 7901 | start_arrow(&tpos); |
| 7902 | # ifdef FEAT_CINDENT |
| 7903 | can_cindent = TRUE; |
| 7904 | # endif |
| 7905 | } |
| 7906 | } |
| 7907 | #endif |
| 7908 | |
| 7909 | #ifdef FEAT_GUI |
| 7910 | void |
| 7911 | ins_scroll() |
| 7912 | { |
| 7913 | pos_T tpos; |
| 7914 | |
| 7915 | undisplay_dollar(); |
| 7916 | tpos = curwin->w_cursor; |
| 7917 | if (gui_do_scroll()) |
| 7918 | { |
| 7919 | start_arrow(&tpos); |
| 7920 | # ifdef FEAT_CINDENT |
| 7921 | can_cindent = TRUE; |
| 7922 | # endif |
| 7923 | } |
| 7924 | } |
| 7925 | |
| 7926 | void |
| 7927 | ins_horscroll() |
| 7928 | { |
| 7929 | pos_T tpos; |
| 7930 | |
| 7931 | undisplay_dollar(); |
| 7932 | tpos = curwin->w_cursor; |
| 7933 | if (gui_do_horiz_scroll()) |
| 7934 | { |
| 7935 | start_arrow(&tpos); |
| 7936 | # ifdef FEAT_CINDENT |
| 7937 | can_cindent = TRUE; |
| 7938 | # endif |
| 7939 | } |
| 7940 | } |
| 7941 | #endif |
| 7942 | |
| 7943 | static void |
| 7944 | ins_left() |
| 7945 | { |
| 7946 | pos_T tpos; |
| 7947 | |
| 7948 | #ifdef FEAT_FOLDING |
| 7949 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7950 | foldOpenCursor(); |
| 7951 | #endif |
| 7952 | undisplay_dollar(); |
| 7953 | tpos = curwin->w_cursor; |
| 7954 | if (oneleft() == OK) |
| 7955 | { |
| 7956 | start_arrow(&tpos); |
| 7957 | #ifdef FEAT_RIGHTLEFT |
| 7958 | /* If exit reversed string, position is fixed */ |
| 7959 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 7960 | revins_legal++; |
| 7961 | revins_chars++; |
| 7962 | #endif |
| 7963 | } |
| 7964 | |
| 7965 | /* |
| 7966 | * if 'whichwrap' set for cursor in insert mode may go to |
| 7967 | * previous line |
| 7968 | */ |
| 7969 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 7970 | { |
| 7971 | start_arrow(&tpos); |
| 7972 | --(curwin->w_cursor.lnum); |
| 7973 | coladvance((colnr_T)MAXCOL); |
| 7974 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 7975 | } |
| 7976 | else |
| 7977 | vim_beep(); |
| 7978 | } |
| 7979 | |
| 7980 | static void |
| 7981 | ins_home(c) |
| 7982 | int c; |
| 7983 | { |
| 7984 | pos_T tpos; |
| 7985 | |
| 7986 | #ifdef FEAT_FOLDING |
| 7987 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 7988 | foldOpenCursor(); |
| 7989 | #endif |
| 7990 | undisplay_dollar(); |
| 7991 | tpos = curwin->w_cursor; |
| 7992 | if (c == K_C_HOME) |
| 7993 | curwin->w_cursor.lnum = 1; |
| 7994 | curwin->w_cursor.col = 0; |
| 7995 | #ifdef FEAT_VIRTUALEDIT |
| 7996 | curwin->w_cursor.coladd = 0; |
| 7997 | #endif |
| 7998 | curwin->w_curswant = 0; |
| 7999 | start_arrow(&tpos); |
| 8000 | } |
| 8001 | |
| 8002 | static void |
| 8003 | ins_end(c) |
| 8004 | int c; |
| 8005 | { |
| 8006 | pos_T tpos; |
| 8007 | |
| 8008 | #ifdef FEAT_FOLDING |
| 8009 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8010 | foldOpenCursor(); |
| 8011 | #endif |
| 8012 | undisplay_dollar(); |
| 8013 | tpos = curwin->w_cursor; |
| 8014 | if (c == K_C_END) |
| 8015 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 8016 | coladvance((colnr_T)MAXCOL); |
| 8017 | curwin->w_curswant = MAXCOL; |
| 8018 | |
| 8019 | start_arrow(&tpos); |
| 8020 | } |
| 8021 | |
| 8022 | static void |
| 8023 | ins_s_left() |
| 8024 | { |
| 8025 | #ifdef FEAT_FOLDING |
| 8026 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8027 | foldOpenCursor(); |
| 8028 | #endif |
| 8029 | undisplay_dollar(); |
| 8030 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 8031 | { |
| 8032 | start_arrow(&curwin->w_cursor); |
| 8033 | (void)bck_word(1L, FALSE, FALSE); |
| 8034 | curwin->w_set_curswant = TRUE; |
| 8035 | } |
| 8036 | else |
| 8037 | vim_beep(); |
| 8038 | } |
| 8039 | |
| 8040 | static void |
| 8041 | ins_right() |
| 8042 | { |
| 8043 | #ifdef FEAT_FOLDING |
| 8044 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8045 | foldOpenCursor(); |
| 8046 | #endif |
| 8047 | undisplay_dollar(); |
| 8048 | if (gchar_cursor() != NUL || virtual_active() |
| 8049 | ) |
| 8050 | { |
| 8051 | start_arrow(&curwin->w_cursor); |
| 8052 | curwin->w_set_curswant = TRUE; |
| 8053 | #ifdef FEAT_VIRTUALEDIT |
| 8054 | if (virtual_active()) |
| 8055 | oneright(); |
| 8056 | else |
| 8057 | #endif |
| 8058 | { |
| 8059 | #ifdef FEAT_MBYTE |
| 8060 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8061 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8062 | else |
| 8063 | #endif |
| 8064 | ++curwin->w_cursor.col; |
| 8065 | } |
| 8066 | |
| 8067 | #ifdef FEAT_RIGHTLEFT |
| 8068 | revins_legal++; |
| 8069 | if (revins_chars) |
| 8070 | revins_chars--; |
| 8071 | #endif |
| 8072 | } |
| 8073 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 8074 | * cursor to the next line */ |
| 8075 | else if (vim_strchr(p_ww, ']') != NULL |
| 8076 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 8077 | { |
| 8078 | start_arrow(&curwin->w_cursor); |
| 8079 | curwin->w_set_curswant = TRUE; |
| 8080 | ++curwin->w_cursor.lnum; |
| 8081 | curwin->w_cursor.col = 0; |
| 8082 | } |
| 8083 | else |
| 8084 | vim_beep(); |
| 8085 | } |
| 8086 | |
| 8087 | static void |
| 8088 | ins_s_right() |
| 8089 | { |
| 8090 | #ifdef FEAT_FOLDING |
| 8091 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8092 | foldOpenCursor(); |
| 8093 | #endif |
| 8094 | undisplay_dollar(); |
| 8095 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 8096 | || gchar_cursor() != NUL) |
| 8097 | { |
| 8098 | start_arrow(&curwin->w_cursor); |
| 8099 | (void)fwd_word(1L, FALSE, 0); |
| 8100 | curwin->w_set_curswant = TRUE; |
| 8101 | } |
| 8102 | else |
| 8103 | vim_beep(); |
| 8104 | } |
| 8105 | |
| 8106 | static void |
| 8107 | ins_up(startcol) |
| 8108 | int startcol; /* when TRUE move to Insstart.col */ |
| 8109 | { |
| 8110 | pos_T tpos; |
| 8111 | linenr_T old_topline = curwin->w_topline; |
| 8112 | #ifdef FEAT_DIFF |
| 8113 | int old_topfill = curwin->w_topfill; |
| 8114 | #endif |
| 8115 | |
| 8116 | undisplay_dollar(); |
| 8117 | tpos = curwin->w_cursor; |
| 8118 | if (cursor_up(1L, TRUE) == OK) |
| 8119 | { |
| 8120 | if (startcol) |
| 8121 | coladvance(getvcol_nolist(&Insstart)); |
| 8122 | if (old_topline != curwin->w_topline |
| 8123 | #ifdef FEAT_DIFF |
| 8124 | || old_topfill != curwin->w_topfill |
| 8125 | #endif |
| 8126 | ) |
| 8127 | redraw_later(VALID); |
| 8128 | start_arrow(&tpos); |
| 8129 | #ifdef FEAT_CINDENT |
| 8130 | can_cindent = TRUE; |
| 8131 | #endif |
| 8132 | } |
| 8133 | else |
| 8134 | vim_beep(); |
| 8135 | } |
| 8136 | |
| 8137 | static void |
| 8138 | ins_pageup() |
| 8139 | { |
| 8140 | pos_T tpos; |
| 8141 | |
| 8142 | undisplay_dollar(); |
| 8143 | tpos = curwin->w_cursor; |
| 8144 | if (onepage(BACKWARD, 1L) == OK) |
| 8145 | { |
| 8146 | start_arrow(&tpos); |
| 8147 | #ifdef FEAT_CINDENT |
| 8148 | can_cindent = TRUE; |
| 8149 | #endif |
| 8150 | } |
| 8151 | else |
| 8152 | vim_beep(); |
| 8153 | } |
| 8154 | |
| 8155 | static void |
| 8156 | ins_down(startcol) |
| 8157 | int startcol; /* when TRUE move to Insstart.col */ |
| 8158 | { |
| 8159 | pos_T tpos; |
| 8160 | linenr_T old_topline = curwin->w_topline; |
| 8161 | #ifdef FEAT_DIFF |
| 8162 | int old_topfill = curwin->w_topfill; |
| 8163 | #endif |
| 8164 | |
| 8165 | undisplay_dollar(); |
| 8166 | tpos = curwin->w_cursor; |
| 8167 | if (cursor_down(1L, TRUE) == OK) |
| 8168 | { |
| 8169 | if (startcol) |
| 8170 | coladvance(getvcol_nolist(&Insstart)); |
| 8171 | if (old_topline != curwin->w_topline |
| 8172 | #ifdef FEAT_DIFF |
| 8173 | || old_topfill != curwin->w_topfill |
| 8174 | #endif |
| 8175 | ) |
| 8176 | redraw_later(VALID); |
| 8177 | start_arrow(&tpos); |
| 8178 | #ifdef FEAT_CINDENT |
| 8179 | can_cindent = TRUE; |
| 8180 | #endif |
| 8181 | } |
| 8182 | else |
| 8183 | vim_beep(); |
| 8184 | } |
| 8185 | |
| 8186 | static void |
| 8187 | ins_pagedown() |
| 8188 | { |
| 8189 | pos_T tpos; |
| 8190 | |
| 8191 | undisplay_dollar(); |
| 8192 | tpos = curwin->w_cursor; |
| 8193 | if (onepage(FORWARD, 1L) == OK) |
| 8194 | { |
| 8195 | start_arrow(&tpos); |
| 8196 | #ifdef FEAT_CINDENT |
| 8197 | can_cindent = TRUE; |
| 8198 | #endif |
| 8199 | } |
| 8200 | else |
| 8201 | vim_beep(); |
| 8202 | } |
| 8203 | |
| 8204 | #ifdef FEAT_DND |
| 8205 | static void |
| 8206 | ins_drop() |
| 8207 | { |
| 8208 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 8209 | } |
| 8210 | #endif |
| 8211 | |
| 8212 | /* |
| 8213 | * Handle TAB in Insert or Replace mode. |
| 8214 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 8215 | */ |
| 8216 | static int |
| 8217 | ins_tab() |
| 8218 | { |
| 8219 | int ind; |
| 8220 | int i; |
| 8221 | int temp; |
| 8222 | |
| 8223 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 8224 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 8225 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 8226 | return FALSE; |
| 8227 | |
| 8228 | ind = inindent(0); |
| 8229 | #ifdef FEAT_CINDENT |
| 8230 | if (ind) |
| 8231 | can_cindent = FALSE; |
| 8232 | #endif |
| 8233 | |
| 8234 | /* |
| 8235 | * When nothing special, insert TAB like a normal character |
| 8236 | */ |
| 8237 | if (!curbuf->b_p_et |
| 8238 | && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw) |
| 8239 | && curbuf->b_p_sts == 0) |
| 8240 | return TRUE; |
| 8241 | |
| 8242 | if (stop_arrow() == FAIL) |
| 8243 | return TRUE; |
| 8244 | |
| 8245 | did_ai = FALSE; |
| 8246 | #ifdef FEAT_SMARTINDENT |
| 8247 | did_si = FALSE; |
| 8248 | can_si = FALSE; |
| 8249 | can_si_back = FALSE; |
| 8250 | #endif |
| 8251 | AppendToRedobuff((char_u *)"\t"); |
| 8252 | |
| 8253 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
| 8254 | temp = (int)curbuf->b_p_sw; |
| 8255 | else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */ |
| 8256 | temp = (int)curbuf->b_p_sts; |
| 8257 | else /* otherwise use 'tabstop' */ |
| 8258 | temp = (int)curbuf->b_p_ts; |
| 8259 | temp -= get_nolist_virtcol() % temp; |
| 8260 | |
| 8261 | /* |
| 8262 | * Insert the first space with ins_char(). It will delete one char in |
| 8263 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 8264 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 8265 | */ |
| 8266 | ins_char(' '); |
| 8267 | while (--temp > 0) |
| 8268 | { |
| 8269 | #ifdef FEAT_VREPLACE |
| 8270 | if (State & VREPLACE_FLAG) |
| 8271 | ins_char(' '); |
| 8272 | else |
| 8273 | #endif |
| 8274 | { |
| 8275 | ins_str((char_u *)" "); |
| 8276 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 8277 | replace_push(NUL); |
| 8278 | } |
| 8279 | } |
| 8280 | |
| 8281 | /* |
| 8282 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 8283 | */ |
| 8284 | if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind))) |
| 8285 | { |
| 8286 | char_u *ptr; |
| 8287 | #ifdef FEAT_VREPLACE |
| 8288 | char_u *saved_line = NULL; /* init for GCC */ |
| 8289 | pos_T pos; |
| 8290 | #endif |
| 8291 | pos_T fpos; |
| 8292 | pos_T *cursor; |
| 8293 | colnr_T want_vcol, vcol; |
| 8294 | int change_col = -1; |
| 8295 | int save_list = curwin->w_p_list; |
| 8296 | |
| 8297 | /* |
| 8298 | * Get the current line. For VREPLACE mode, don't make real changes |
| 8299 | * yet, just work on a copy of the line. |
| 8300 | */ |
| 8301 | #ifdef FEAT_VREPLACE |
| 8302 | if (State & VREPLACE_FLAG) |
| 8303 | { |
| 8304 | pos = curwin->w_cursor; |
| 8305 | cursor = &pos; |
| 8306 | saved_line = vim_strsave(ml_get_curline()); |
| 8307 | if (saved_line == NULL) |
| 8308 | return FALSE; |
| 8309 | ptr = saved_line + pos.col; |
| 8310 | } |
| 8311 | else |
| 8312 | #endif |
| 8313 | { |
| 8314 | ptr = ml_get_cursor(); |
| 8315 | cursor = &curwin->w_cursor; |
| 8316 | } |
| 8317 | |
| 8318 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 8319 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 8320 | curwin->w_p_list = FALSE; |
| 8321 | |
| 8322 | /* Find first white before the cursor */ |
| 8323 | fpos = curwin->w_cursor; |
| 8324 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 8325 | { |
| 8326 | --fpos.col; |
| 8327 | --ptr; |
| 8328 | } |
| 8329 | |
| 8330 | /* In Replace mode, don't change characters before the insert point. */ |
| 8331 | if ((State & REPLACE_FLAG) |
| 8332 | && fpos.lnum == Insstart.lnum |
| 8333 | && fpos.col < Insstart.col) |
| 8334 | { |
| 8335 | ptr += Insstart.col - fpos.col; |
| 8336 | fpos.col = Insstart.col; |
| 8337 | } |
| 8338 | |
| 8339 | /* compute virtual column numbers of first white and cursor */ |
| 8340 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 8341 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 8342 | |
| 8343 | /* Use as many TABs as possible. Beware of 'showbreak' and |
| 8344 | * 'linebreak' adding extra virtual columns. */ |
| 8345 | while (vim_iswhite(*ptr)) |
| 8346 | { |
| 8347 | i = lbr_chartabsize((char_u *)"\t", vcol); |
| 8348 | if (vcol + i > want_vcol) |
| 8349 | break; |
| 8350 | if (*ptr != TAB) |
| 8351 | { |
| 8352 | *ptr = TAB; |
| 8353 | if (change_col < 0) |
| 8354 | { |
| 8355 | change_col = fpos.col; /* Column of first change */ |
| 8356 | /* May have to adjust Insstart */ |
| 8357 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 8358 | Insstart.col = fpos.col; |
| 8359 | } |
| 8360 | } |
| 8361 | ++fpos.col; |
| 8362 | ++ptr; |
| 8363 | vcol += i; |
| 8364 | } |
| 8365 | |
| 8366 | if (change_col >= 0) |
| 8367 | { |
| 8368 | int repl_off = 0; |
| 8369 | |
| 8370 | /* Skip over the spaces we need. */ |
| 8371 | while (vcol < want_vcol && *ptr == ' ') |
| 8372 | { |
| 8373 | vcol += lbr_chartabsize(ptr, vcol); |
| 8374 | ++ptr; |
| 8375 | ++repl_off; |
| 8376 | } |
| 8377 | if (vcol > want_vcol) |
| 8378 | { |
| 8379 | /* Must have a char with 'showbreak' just before it. */ |
| 8380 | --ptr; |
| 8381 | --repl_off; |
| 8382 | } |
| 8383 | fpos.col += repl_off; |
| 8384 | |
| 8385 | /* Delete following spaces. */ |
| 8386 | i = cursor->col - fpos.col; |
| 8387 | if (i > 0) |
| 8388 | { |
| 8389 | mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1); |
| 8390 | /* correct replace stack. */ |
| 8391 | if ((State & REPLACE_FLAG) |
| 8392 | #ifdef FEAT_VREPLACE |
| 8393 | && !(State & VREPLACE_FLAG) |
| 8394 | #endif |
| 8395 | ) |
| 8396 | for (temp = i; --temp >= 0; ) |
| 8397 | replace_join(repl_off); |
| 8398 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 8399 | #ifdef FEAT_NETBEANS_INTG |
| 8400 | if (usingNetbeans) |
| 8401 | { |
| 8402 | netbeans_removed(curbuf, fpos.lnum, cursor->col, |
| 8403 | (long)(i + 1)); |
| 8404 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 8405 | (char_u *)"\t", 1); |
| 8406 | } |
| 8407 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8408 | cursor->col -= i; |
| 8409 | |
| 8410 | #ifdef FEAT_VREPLACE |
| 8411 | /* |
| 8412 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 8413 | * backspacing over the changed spacing and then inserting the new |
| 8414 | * spacing. |
| 8415 | */ |
| 8416 | if (State & VREPLACE_FLAG) |
| 8417 | { |
| 8418 | /* Backspace from real cursor to change_col */ |
| 8419 | backspace_until_column(change_col); |
| 8420 | |
| 8421 | /* Insert each char in saved_line from changed_col to |
| 8422 | * ptr-cursor */ |
| 8423 | ins_bytes_len(saved_line + change_col, |
| 8424 | cursor->col - change_col); |
| 8425 | } |
| 8426 | #endif |
| 8427 | } |
| 8428 | |
| 8429 | #ifdef FEAT_VREPLACE |
| 8430 | if (State & VREPLACE_FLAG) |
| 8431 | vim_free(saved_line); |
| 8432 | #endif |
| 8433 | curwin->w_p_list = save_list; |
| 8434 | } |
| 8435 | |
| 8436 | return FALSE; |
| 8437 | } |
| 8438 | |
| 8439 | /* |
| 8440 | * Handle CR or NL in insert mode. |
| 8441 | * Return TRUE when out of memory or can't undo. |
| 8442 | */ |
| 8443 | static int |
| 8444 | ins_eol(c) |
| 8445 | int c; |
| 8446 | { |
| 8447 | int i; |
| 8448 | |
| 8449 | if (echeck_abbr(c + ABBR_OFF)) |
| 8450 | return FALSE; |
| 8451 | if (stop_arrow() == FAIL) |
| 8452 | return TRUE; |
| 8453 | undisplay_dollar(); |
| 8454 | |
| 8455 | /* |
| 8456 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 8457 | * character under the cursor. Only push a NUL on the replace stack, |
| 8458 | * nothing to put back when the NL is deleted. |
| 8459 | */ |
| 8460 | if ((State & REPLACE_FLAG) |
| 8461 | #ifdef FEAT_VREPLACE |
| 8462 | && !(State & VREPLACE_FLAG) |
| 8463 | #endif |
| 8464 | ) |
| 8465 | replace_push(NUL); |
| 8466 | |
| 8467 | /* |
| 8468 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 8469 | * replacing the next line, so we push all of the characters left on the |
| 8470 | * line onto the replace stack. This is not done here though, it is done |
| 8471 | * in open_line(). |
| 8472 | */ |
| 8473 | |
| 8474 | #ifdef FEAT_RIGHTLEFT |
| 8475 | # ifdef FEAT_FKMAP |
| 8476 | if (p_altkeymap && p_fkmap) |
| 8477 | fkmap(NL); |
| 8478 | # endif |
| 8479 | /* NL in reverse insert will always start in the end of |
| 8480 | * current line. */ |
| 8481 | if (revins_on) |
| 8482 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 8483 | #endif |
| 8484 | |
| 8485 | AppendToRedobuff(NL_STR); |
| 8486 | i = open_line(FORWARD, |
| 8487 | #ifdef FEAT_COMMENTS |
| 8488 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 8489 | #endif |
| 8490 | 0, old_indent); |
| 8491 | old_indent = 0; |
| 8492 | #ifdef FEAT_CINDENT |
| 8493 | can_cindent = TRUE; |
| 8494 | #endif |
| 8495 | |
| 8496 | return (!i); |
| 8497 | } |
| 8498 | |
| 8499 | #ifdef FEAT_DIGRAPHS |
| 8500 | /* |
| 8501 | * Handle digraph in insert mode. |
| 8502 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 8503 | * done. |
| 8504 | */ |
| 8505 | static int |
| 8506 | ins_digraph() |
| 8507 | { |
| 8508 | int c; |
| 8509 | int cc; |
| 8510 | |
| 8511 | pc_status = PC_STATUS_UNSET; |
| 8512 | if (redrawing() && !char_avail()) |
| 8513 | { |
| 8514 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8515 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8516 | |
| 8517 | edit_putchar('?', TRUE); |
| 8518 | #ifdef FEAT_CMDL_INFO |
| 8519 | add_to_showcmd_c(Ctrl_K); |
| 8520 | #endif |
| 8521 | } |
| 8522 | |
| 8523 | #ifdef USE_ON_FLY_SCROLL |
| 8524 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 8525 | #endif |
| 8526 | |
| 8527 | /* don't map the digraph chars. This also prevents the |
| 8528 | * mode message to be deleted when ESC is hit */ |
| 8529 | ++no_mapping; |
| 8530 | ++allow_keys; |
| 8531 | c = safe_vgetc(); |
| 8532 | --no_mapping; |
| 8533 | --allow_keys; |
| 8534 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 8535 | { |
| 8536 | #ifdef FEAT_CMDL_INFO |
| 8537 | clear_showcmd(); |
| 8538 | #endif |
| 8539 | insert_special(c, TRUE, FALSE); |
| 8540 | return NUL; |
| 8541 | } |
| 8542 | if (c != ESC) |
| 8543 | { |
| 8544 | if (redrawing() && !char_avail()) |
| 8545 | { |
| 8546 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8547 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8548 | |
| 8549 | if (char2cells(c) == 1) |
| 8550 | { |
| 8551 | /* first remove the '?', otherwise it's restored when typing |
| 8552 | * an ESC next */ |
| 8553 | edit_unputchar(); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8554 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8555 | edit_putchar(c, TRUE); |
| 8556 | } |
| 8557 | #ifdef FEAT_CMDL_INFO |
| 8558 | add_to_showcmd_c(c); |
| 8559 | #endif |
| 8560 | } |
| 8561 | ++no_mapping; |
| 8562 | ++allow_keys; |
| 8563 | cc = safe_vgetc(); |
| 8564 | --no_mapping; |
| 8565 | --allow_keys; |
| 8566 | if (cc != ESC) |
| 8567 | { |
| 8568 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 8569 | c = getdigraph(c, cc, TRUE); |
| 8570 | #ifdef FEAT_CMDL_INFO |
| 8571 | clear_showcmd(); |
| 8572 | #endif |
| 8573 | return c; |
| 8574 | } |
| 8575 | } |
| 8576 | edit_unputchar(); |
| 8577 | #ifdef FEAT_CMDL_INFO |
| 8578 | clear_showcmd(); |
| 8579 | #endif |
| 8580 | return NUL; |
| 8581 | } |
| 8582 | #endif |
| 8583 | |
| 8584 | /* |
| 8585 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 8586 | * Returns the char to be inserted, or NUL if none found. |
| 8587 | */ |
| 8588 | static int |
| 8589 | ins_copychar(lnum) |
| 8590 | linenr_T lnum; |
| 8591 | { |
| 8592 | int c; |
| 8593 | int temp; |
| 8594 | char_u *ptr, *prev_ptr; |
| 8595 | |
| 8596 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 8597 | { |
| 8598 | vim_beep(); |
| 8599 | return NUL; |
| 8600 | } |
| 8601 | |
| 8602 | /* try to advance to the cursor column */ |
| 8603 | temp = 0; |
| 8604 | ptr = ml_get(lnum); |
| 8605 | prev_ptr = ptr; |
| 8606 | validate_virtcol(); |
| 8607 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 8608 | { |
| 8609 | prev_ptr = ptr; |
| 8610 | temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp); |
| 8611 | } |
| 8612 | if ((colnr_T)temp > curwin->w_virtcol) |
| 8613 | ptr = prev_ptr; |
| 8614 | |
| 8615 | #ifdef FEAT_MBYTE |
| 8616 | c = (*mb_ptr2char)(ptr); |
| 8617 | #else |
| 8618 | c = *ptr; |
| 8619 | #endif |
| 8620 | if (c == NUL) |
| 8621 | vim_beep(); |
| 8622 | return c; |
| 8623 | } |
| 8624 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8625 | /* |
| 8626 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 8627 | */ |
| 8628 | static int |
| 8629 | ins_ctrl_ey(tc) |
| 8630 | int tc; |
| 8631 | { |
| 8632 | int c = tc; |
| 8633 | |
| 8634 | #ifdef FEAT_INS_EXPAND |
| 8635 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 8636 | { |
| 8637 | if (c == Ctrl_Y) |
| 8638 | scrolldown_clamp(); |
| 8639 | else |
| 8640 | scrollup_clamp(); |
| 8641 | redraw_later(VALID); |
| 8642 | } |
| 8643 | else |
| 8644 | #endif |
| 8645 | { |
| 8646 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 8647 | if (c != NUL) |
| 8648 | { |
| 8649 | long tw_save; |
| 8650 | |
| 8651 | /* The character must be taken literally, insert like it |
| 8652 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 8653 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 8654 | * CTRL-V, don't use it for these. */ |
| 8655 | if (c < 256 && !isalnum(c)) |
| 8656 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 8657 | tw_save = curbuf->b_p_tw; |
| 8658 | curbuf->b_p_tw = -1; |
| 8659 | insert_special(c, TRUE, FALSE); |
| 8660 | curbuf->b_p_tw = tw_save; |
| 8661 | #ifdef FEAT_RIGHTLEFT |
| 8662 | revins_chars++; |
| 8663 | revins_legal++; |
| 8664 | #endif |
| 8665 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 8666 | auto_format(FALSE, TRUE); |
| 8667 | } |
| 8668 | } |
| 8669 | return c; |
| 8670 | } |
| 8671 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8672 | #ifdef FEAT_SMARTINDENT |
| 8673 | /* |
| 8674 | * Try to do some very smart auto-indenting. |
| 8675 | * Used when inserting a "normal" character. |
| 8676 | */ |
| 8677 | static void |
| 8678 | ins_try_si(c) |
| 8679 | int c; |
| 8680 | { |
| 8681 | pos_T *pos, old_pos; |
| 8682 | char_u *ptr; |
| 8683 | int i; |
| 8684 | int temp; |
| 8685 | |
| 8686 | /* |
| 8687 | * do some very smart indenting when entering '{' or '}' |
| 8688 | */ |
| 8689 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 8690 | { |
| 8691 | /* |
| 8692 | * for '}' set indent equal to indent of line containing matching '{' |
| 8693 | */ |
| 8694 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 8695 | { |
| 8696 | old_pos = curwin->w_cursor; |
| 8697 | /* |
| 8698 | * If the matching '{' has a ')' immediately before it (ignoring |
| 8699 | * white-space), then line up with the start of the line |
| 8700 | * containing the matching '(' if there is one. This handles the |
| 8701 | * case where an "if (..\n..) {" statement continues over multiple |
| 8702 | * lines -- webb |
| 8703 | */ |
| 8704 | ptr = ml_get(pos->lnum); |
| 8705 | i = pos->col; |
| 8706 | if (i > 0) /* skip blanks before '{' */ |
| 8707 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 8708 | ; |
| 8709 | curwin->w_cursor.lnum = pos->lnum; |
| 8710 | curwin->w_cursor.col = i; |
| 8711 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 8712 | curwin->w_cursor = *pos; |
| 8713 | i = get_indent(); |
| 8714 | curwin->w_cursor = old_pos; |
| 8715 | #ifdef FEAT_VREPLACE |
| 8716 | if (State & VREPLACE_FLAG) |
| 8717 | change_indent(INDENT_SET, i, FALSE, NUL); |
| 8718 | else |
| 8719 | #endif |
| 8720 | (void)set_indent(i, SIN_CHANGED); |
| 8721 | } |
| 8722 | else if (curwin->w_cursor.col > 0) |
| 8723 | { |
| 8724 | /* |
| 8725 | * when inserting '{' after "O" reduce indent, but not |
| 8726 | * more than indent of previous line |
| 8727 | */ |
| 8728 | temp = TRUE; |
| 8729 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 8730 | { |
| 8731 | old_pos = curwin->w_cursor; |
| 8732 | i = get_indent(); |
| 8733 | while (curwin->w_cursor.lnum > 1) |
| 8734 | { |
| 8735 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 8736 | |
| 8737 | /* ignore empty lines and lines starting with '#'. */ |
| 8738 | if (*ptr != '#' && *ptr != NUL) |
| 8739 | break; |
| 8740 | } |
| 8741 | if (get_indent() >= i) |
| 8742 | temp = FALSE; |
| 8743 | curwin->w_cursor = old_pos; |
| 8744 | } |
| 8745 | if (temp) |
| 8746 | shift_line(TRUE, FALSE, 1); |
| 8747 | } |
| 8748 | } |
| 8749 | |
| 8750 | /* |
| 8751 | * set indent of '#' always to 0 |
| 8752 | */ |
| 8753 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 8754 | { |
| 8755 | /* remember current indent for next line */ |
| 8756 | old_indent = get_indent(); |
| 8757 | (void)set_indent(0, SIN_CHANGED); |
| 8758 | } |
| 8759 | |
| 8760 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 8761 | if (ai_col > curwin->w_cursor.col) |
| 8762 | ai_col = curwin->w_cursor.col; |
| 8763 | } |
| 8764 | #endif |
| 8765 | |
| 8766 | /* |
| 8767 | * Get the value that w_virtcol would have when 'list' is off. |
| 8768 | * Unless 'cpo' contains the 'L' flag. |
| 8769 | */ |
| 8770 | static colnr_T |
| 8771 | get_nolist_virtcol() |
| 8772 | { |
| 8773 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 8774 | return getvcol_nolist(&curwin->w_cursor); |
| 8775 | validate_virtcol(); |
| 8776 | return curwin->w_virtcol; |
| 8777 | } |