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 | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 65 | typedef struct compl_S compl_T; |
| 66 | struct compl_S |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 71 | char cp_icase; /* TRUE or FALSE: ignore case */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 72 | char_u *cp_extra; /* extra menu text (allocated, can be NULL) */ |
| 73 | char_u *cp_info; /* verbose info (can be NULL) */ |
| 74 | char_u cp_kind; /* kind of match, single letter, or NUL */ |
| 75 | char_u *cp_fname; /* file containing the match, allocated when |
| 76 | * cp_flags has FREE_FNAME */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 77 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 78 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 81 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 82 | #define FREE_FNAME (2) |
| 83 | |
| 84 | /* |
| 85 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 86 | * "compl_first_match" points to the start of the list. |
| 87 | * "compl_curr_match" points to the currently selected entry. |
| 88 | * "compl_shown_match" is different from compl_curr_match during |
| 89 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 90 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 91 | static compl_T *compl_first_match = NULL; |
| 92 | static compl_T *compl_curr_match = NULL; |
| 93 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 94 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 95 | /* When "compl_leader" is not NULL only matches that start with this string |
| 96 | * are used. */ |
| 97 | static char_u *compl_leader = NULL; |
| 98 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 99 | static int compl_get_longest = FALSE; /* put longest common string |
| 100 | in compl_leader */ |
| 101 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 102 | static int compl_used_match; /* Selected one of the matches. When |
| 103 | FALSE the match was edited or using |
| 104 | the longest common string. */ |
| 105 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 106 | /* When the first completion is done "compl_started" is set. When it's |
| 107 | * FALSE the word to be completed must be located. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 108 | static int compl_started = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 109 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 110 | static int compl_matches = 0; |
| 111 | static char_u *compl_pattern = NULL; |
| 112 | static int compl_direction = FORWARD; |
| 113 | static int compl_shows_dir = FORWARD; |
| 114 | static int compl_pending = FALSE; |
| 115 | static pos_T compl_startpos; |
| 116 | static colnr_T compl_col = 0; /* column where the text starts |
| 117 | * that is being completed */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 118 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 119 | * completion started */ |
| 120 | static int compl_cont_mode = 0; |
| 121 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 123 | static void ins_ctrl_x __ARGS((void)); |
| 124 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 125 | static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 126 | static void ins_compl_longest_match __ARGS((compl_T *match)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 127 | static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | static int ins_compl_make_cyclic __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 129 | static void ins_compl_upd_pum __ARGS((void)); |
| 130 | static void ins_compl_del_pum __ARGS((void)); |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 131 | static int pum_wanted __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 132 | static int pum_two_or_more __ARGS((void)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 133 | 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] | 134 | static char_u *find_line_end __ARGS((char_u *ptr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 135 | static void ins_compl_free __ARGS((void)); |
| 136 | static void ins_compl_clear __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 137 | static int ins_compl_bs __ARGS((void)); |
| 138 | static void ins_compl_addleader __ARGS((int c)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 139 | static void ins_compl_addfrommatch __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 140 | static int ins_compl_prep __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 141 | 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] | 142 | static int ins_compl_get_exp __ARGS((pos_T *ini)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | static void ins_compl_delete __ARGS((void)); |
| 144 | static void ins_compl_insert __ARGS((void)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 145 | 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] | 146 | static int ins_compl_key2dir __ARGS((int c)); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 147 | static int ins_compl_pum_key __ARGS((int c)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 148 | static int ins_compl_key2count __ARGS((int c)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 149 | static int ins_compl_use_match __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 150 | static int ins_complete __ARGS((int c)); |
| 151 | static int quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
| 152 | #endif /* FEAT_INS_EXPAND */ |
| 153 | |
| 154 | #define BACKSPACE_CHAR 1 |
| 155 | #define BACKSPACE_WORD 2 |
| 156 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 157 | #define BACKSPACE_LINE 4 |
| 158 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 159 | static void ins_redraw __ARGS((int ready)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | static void ins_ctrl_v __ARGS((void)); |
| 161 | static void undisplay_dollar __ARGS((void)); |
| 162 | static void insert_special __ARGS((int, int, int)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 163 | 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] | 164 | static void check_auto_format __ARGS((int)); |
| 165 | static void redo_literal __ARGS((int c)); |
| 166 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 167 | #ifdef FEAT_SYN_HL |
| 168 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 169 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 170 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 171 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc)); |
| 173 | static int echeck_abbr __ARGS((int)); |
| 174 | static void replace_push_off __ARGS((int c)); |
| 175 | static int replace_pop __ARGS((void)); |
| 176 | static void replace_join __ARGS((int off)); |
| 177 | static void replace_pop_ins __ARGS((void)); |
| 178 | #ifdef FEAT_MBYTE |
| 179 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 180 | #endif |
| 181 | static void replace_flush __ARGS((void)); |
| 182 | static void replace_do_bs __ARGS((void)); |
| 183 | #ifdef FEAT_CINDENT |
| 184 | static int cindent_on __ARGS((void)); |
| 185 | #endif |
| 186 | static void ins_reg __ARGS((void)); |
| 187 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 188 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 189 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | #ifdef FEAT_RIGHTLEFT |
| 191 | static void ins_ctrl_ __ARGS((void)); |
| 192 | #endif |
| 193 | #ifdef FEAT_VISUAL |
| 194 | static int ins_start_select __ARGS((int c)); |
| 195 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 196 | static void ins_insert __ARGS((int replaceState)); |
| 197 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 198 | static void ins_shift __ARGS((int c, int lastc)); |
| 199 | static void ins_del __ARGS((void)); |
| 200 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 201 | #ifdef FEAT_MOUSE |
| 202 | static void ins_mouse __ARGS((int c)); |
| 203 | static void ins_mousescroll __ARGS((int up)); |
| 204 | #endif |
| 205 | static void ins_left __ARGS((void)); |
| 206 | static void ins_home __ARGS((int c)); |
| 207 | static void ins_end __ARGS((int c)); |
| 208 | static void ins_s_left __ARGS((void)); |
| 209 | static void ins_right __ARGS((void)); |
| 210 | static void ins_s_right __ARGS((void)); |
| 211 | static void ins_up __ARGS((int startcol)); |
| 212 | static void ins_pageup __ARGS((void)); |
| 213 | static void ins_down __ARGS((int startcol)); |
| 214 | static void ins_pagedown __ARGS((void)); |
| 215 | #ifdef FEAT_DND |
| 216 | static void ins_drop __ARGS((void)); |
| 217 | #endif |
| 218 | static int ins_tab __ARGS((void)); |
| 219 | static int ins_eol __ARGS((int c)); |
| 220 | #ifdef FEAT_DIGRAPHS |
| 221 | static int ins_digraph __ARGS((void)); |
| 222 | #endif |
| 223 | static int ins_copychar __ARGS((linenr_T lnum)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 224 | static int ins_ctrl_ey __ARGS((int tc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | #ifdef FEAT_SMARTINDENT |
| 226 | static void ins_try_si __ARGS((int c)); |
| 227 | #endif |
| 228 | static colnr_T get_nolist_virtcol __ARGS((void)); |
| 229 | |
| 230 | static colnr_T Insstart_textlen; /* length of line when insert started */ |
| 231 | static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ |
| 232 | |
| 233 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 234 | K_SPECIAL and CSI are escaped */ |
| 235 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 236 | 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] | 237 | static int did_restart_edit; /* "restart_edit" when calling edit() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | |
| 239 | #ifdef FEAT_CINDENT |
| 240 | static int can_cindent; /* may do cindenting on this line */ |
| 241 | #endif |
| 242 | |
| 243 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 244 | |
| 245 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 246 | static int revins_on; /* reverse insert mode on */ |
| 247 | static int revins_chars; /* how much to skip after edit */ |
| 248 | static int revins_legal; /* was the last char 'legal'? */ |
| 249 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 250 | #endif |
| 251 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 252 | static int ins_need_undo; /* call u_save() before inserting a |
| 253 | char. Set when edit() is called. |
| 254 | after that arrow_used is used. */ |
| 255 | |
| 256 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 257 | under the cursor */ |
| 258 | |
| 259 | /* |
| 260 | * edit(): Start inserting text. |
| 261 | * |
| 262 | * "cmdchar" can be: |
| 263 | * 'i' normal insert command |
| 264 | * 'a' normal append command |
| 265 | * 'R' replace command |
| 266 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 267 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 268 | * 'g' "gI" command. |
| 269 | * 'V' "gR" command for Virtual Replace mode. |
| 270 | * 'v' "gr" command for single character Virtual Replace mode. |
| 271 | * |
| 272 | * This function is not called recursively. For CTRL-O commands, it returns |
| 273 | * and lets the caller handle the Normal-mode command. |
| 274 | * |
| 275 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 276 | */ |
| 277 | int |
| 278 | edit(cmdchar, startln, count) |
| 279 | int cmdchar; |
| 280 | int startln; /* if set, insert at start of line */ |
| 281 | long count; |
| 282 | { |
| 283 | int c = 0; |
| 284 | char_u *ptr; |
| 285 | int lastc; |
| 286 | colnr_T mincol; |
| 287 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 288 | int i; |
| 289 | int did_backspace = TRUE; /* previous char was backspace */ |
| 290 | #ifdef FEAT_CINDENT |
| 291 | int line_is_white = FALSE; /* line is empty before insert */ |
| 292 | #endif |
| 293 | linenr_T old_topline = 0; /* topline before insertion */ |
| 294 | #ifdef FEAT_DIFF |
| 295 | int old_topfill = -1; |
| 296 | #endif |
| 297 | int inserted_space = FALSE; /* just inserted a space */ |
| 298 | int replaceState = REPLACE; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 299 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 300 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 301 | /* Remember whether editing was restarted after CTRL-O. */ |
| 302 | did_restart_edit = restart_edit; |
| 303 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 304 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 305 | * error message */ |
| 306 | check_for_delay(TRUE); |
| 307 | |
| 308 | #ifdef HAVE_SANDBOX |
| 309 | /* Don't allow inserting in the sandbox. */ |
| 310 | if (sandbox != 0) |
| 311 | { |
| 312 | EMSG(_(e_sandbox)); |
| 313 | return FALSE; |
| 314 | } |
| 315 | #endif |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 316 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 317 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 318 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 319 | { |
| 320 | EMSG(_(e_secure)); |
| 321 | return FALSE; |
| 322 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | |
| 324 | #ifdef FEAT_INS_EXPAND |
| 325 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 326 | #endif |
| 327 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 328 | #ifdef FEAT_AUTOCMD |
| 329 | /* |
| 330 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 331 | */ |
| 332 | if (cmdchar != 'r' && cmdchar != 'v') |
| 333 | { |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 334 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 335 | if (cmdchar == 'R') |
| 336 | ptr = (char_u *)"r"; |
| 337 | else if (cmdchar == 'V') |
| 338 | ptr = (char_u *)"v"; |
| 339 | else |
| 340 | ptr = (char_u *)"i"; |
| 341 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 342 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 343 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
| 344 | } |
| 345 | #endif |
| 346 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 347 | #ifdef FEAT_MOUSE |
| 348 | /* |
| 349 | * When doing a paste with the middle mouse button, Insstart is set to |
| 350 | * where the paste started. |
| 351 | */ |
| 352 | if (where_paste_started.lnum != 0) |
| 353 | Insstart = where_paste_started; |
| 354 | else |
| 355 | #endif |
| 356 | { |
| 357 | Insstart = curwin->w_cursor; |
| 358 | if (startln) |
| 359 | Insstart.col = 0; |
| 360 | } |
| 361 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 362 | Insstart_blank_vcol = MAXCOL; |
| 363 | if (!did_ai) |
| 364 | ai_col = 0; |
| 365 | |
| 366 | if (cmdchar != NUL && restart_edit == 0) |
| 367 | { |
| 368 | ResetRedobuff(); |
| 369 | AppendNumberToRedobuff(count); |
| 370 | #ifdef FEAT_VREPLACE |
| 371 | if (cmdchar == 'V' || cmdchar == 'v') |
| 372 | { |
| 373 | /* "gR" or "gr" command */ |
| 374 | AppendCharToRedobuff('g'); |
| 375 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 376 | } |
| 377 | else |
| 378 | #endif |
| 379 | { |
| 380 | AppendCharToRedobuff(cmdchar); |
| 381 | if (cmdchar == 'g') /* "gI" command */ |
| 382 | AppendCharToRedobuff('I'); |
| 383 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 384 | count = 1; /* insert only one <CR> */ |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | if (cmdchar == 'R') |
| 389 | { |
| 390 | #ifdef FEAT_FKMAP |
| 391 | if (p_fkmap && p_ri) |
| 392 | { |
| 393 | beep_flush(); |
| 394 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 395 | State = INSERT; |
| 396 | } |
| 397 | else |
| 398 | #endif |
| 399 | State = REPLACE; |
| 400 | } |
| 401 | #ifdef FEAT_VREPLACE |
| 402 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 403 | { |
| 404 | State = VREPLACE; |
| 405 | replaceState = VREPLACE; |
| 406 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 407 | vr_lines_changed = 1; |
| 408 | } |
| 409 | #endif |
| 410 | else |
| 411 | State = INSERT; |
| 412 | |
| 413 | stop_insert_mode = FALSE; |
| 414 | |
| 415 | /* |
| 416 | * Need to recompute the cursor position, it might move when the cursor is |
| 417 | * on a TAB or special character. |
| 418 | */ |
| 419 | curs_columns(TRUE); |
| 420 | |
| 421 | /* |
| 422 | * Enable langmap or IME, indicated by 'iminsert'. |
| 423 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 424 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 425 | * when hitting <Esc>. |
| 426 | */ |
| 427 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 428 | State |= LANGMAP; |
| 429 | #ifdef USE_IM_CONTROL |
| 430 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 431 | #endif |
| 432 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 433 | #ifdef FEAT_MOUSE |
| 434 | setmouse(); |
| 435 | #endif |
| 436 | #ifdef FEAT_CMDL_INFO |
| 437 | clear_showcmd(); |
| 438 | #endif |
| 439 | #ifdef FEAT_RIGHTLEFT |
| 440 | /* there is no reverse replace mode */ |
| 441 | revins_on = (State == INSERT && p_ri); |
| 442 | if (revins_on) |
| 443 | undisplay_dollar(); |
| 444 | revins_chars = 0; |
| 445 | revins_legal = 0; |
| 446 | revins_scol = -1; |
| 447 | #endif |
| 448 | |
| 449 | /* |
| 450 | * Handle restarting Insert mode. |
| 451 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 452 | * restart_edit non-zero, and something in the stuff buffer. |
| 453 | */ |
| 454 | if (restart_edit != 0 && stuff_empty()) |
| 455 | { |
| 456 | #ifdef FEAT_MOUSE |
| 457 | /* |
| 458 | * After a paste we consider text typed to be part of the insert for |
| 459 | * the pasted text. You can backspace over the pasted text too. |
| 460 | */ |
| 461 | if (where_paste_started.lnum) |
| 462 | arrow_used = FALSE; |
| 463 | else |
| 464 | #endif |
| 465 | arrow_used = TRUE; |
| 466 | restart_edit = 0; |
| 467 | |
| 468 | /* |
| 469 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 470 | * now at the end-of-line, put it after the end-of-line (this is not |
| 471 | * correct in very rare cases). |
| 472 | * Also do this if curswant is greater than the current virtual |
| 473 | * column. Eg after "^O$" or "^O80|". |
| 474 | */ |
| 475 | validate_virtcol(); |
| 476 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 477 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 478 | || curwin->w_curswant > curwin->w_virtcol) |
| 479 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 480 | { |
| 481 | if (ptr[1] == NUL) |
| 482 | ++curwin->w_cursor.col; |
| 483 | #ifdef FEAT_MBYTE |
| 484 | else if (has_mbyte) |
| 485 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 486 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 487 | if (ptr[i] == NUL) |
| 488 | curwin->w_cursor.col += i; |
| 489 | } |
| 490 | #endif |
| 491 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 492 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | } |
| 494 | else |
| 495 | arrow_used = FALSE; |
| 496 | |
| 497 | /* we are in insert mode now, don't need to start it anymore */ |
| 498 | need_start_insertmode = FALSE; |
| 499 | |
| 500 | /* Need to save the line for undo before inserting the first char. */ |
| 501 | ins_need_undo = TRUE; |
| 502 | |
| 503 | #ifdef FEAT_MOUSE |
| 504 | where_paste_started.lnum = 0; |
| 505 | #endif |
| 506 | #ifdef FEAT_CINDENT |
| 507 | can_cindent = TRUE; |
| 508 | #endif |
| 509 | #ifdef FEAT_FOLDING |
| 510 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 511 | * restarting. */ |
| 512 | if (!p_im && did_restart_edit == 0) |
| 513 | foldOpenCursor(); |
| 514 | #endif |
| 515 | |
| 516 | /* |
| 517 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 518 | * A warning message for changing a readonly file is given here, before |
| 519 | * actually changing anything. It's put after the mode, if any. |
| 520 | */ |
| 521 | i = 0; |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 522 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 523 | i = showmode(); |
| 524 | |
| 525 | if (!p_im && did_restart_edit == 0) |
| 526 | change_warning(i + 1); |
| 527 | |
| 528 | #ifdef CURSOR_SHAPE |
| 529 | ui_cursor_shape(); /* may show different cursor shape */ |
| 530 | #endif |
| 531 | #ifdef FEAT_DIGRAPHS |
| 532 | do_digraph(-1); /* clear digraphs */ |
| 533 | #endif |
| 534 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 535 | /* |
| 536 | * Get the current length of the redo buffer, those characters have to be |
| 537 | * skipped if we want to get to the inserted characters. |
| 538 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 539 | ptr = get_inserted(); |
| 540 | if (ptr == NULL) |
| 541 | new_insert_skip = 0; |
| 542 | else |
| 543 | { |
| 544 | new_insert_skip = (int)STRLEN(ptr); |
| 545 | vim_free(ptr); |
| 546 | } |
| 547 | |
| 548 | old_indent = 0; |
| 549 | |
| 550 | /* |
| 551 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 552 | */ |
| 553 | for (;;) |
| 554 | { |
| 555 | #ifdef FEAT_RIGHTLEFT |
| 556 | if (!revins_legal) |
| 557 | revins_scol = -1; /* reset on illegal motions */ |
| 558 | else |
| 559 | revins_legal = 0; |
| 560 | #endif |
| 561 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 562 | count = 0; |
| 563 | |
| 564 | if (stop_insert_mode) |
| 565 | { |
| 566 | /* ":stopinsert" used or 'insertmode' reset */ |
| 567 | count = 0; |
| 568 | goto doESCkey; |
| 569 | } |
| 570 | |
| 571 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 572 | if (!arrow_used) |
| 573 | curwin->w_set_curswant = TRUE; |
| 574 | |
| 575 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 576 | * menu invoked a shell command). */ |
| 577 | if (stuff_empty()) |
| 578 | { |
| 579 | did_check_timestamps = FALSE; |
| 580 | if (need_check_timestamps) |
| 581 | check_timestamps(FALSE); |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * When emsg() was called msg_scroll will have been set. |
| 586 | */ |
| 587 | msg_scroll = FALSE; |
| 588 | |
| 589 | #ifdef FEAT_GUI |
| 590 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 591 | * another window. "need_mouse_correct" may then be set because of an |
| 592 | * autocommand. */ |
| 593 | if (need_mouse_correct) |
| 594 | gui_mouse_correct(); |
| 595 | #endif |
| 596 | |
| 597 | #ifdef FEAT_FOLDING |
| 598 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 599 | if (fdo_flags & FDO_INSERT) |
| 600 | foldOpenCursor(); |
| 601 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 602 | if (!char_avail()) |
| 603 | foldCheckClose(); |
| 604 | #endif |
| 605 | |
| 606 | /* |
| 607 | * If we inserted a character at the last position of the last line in |
| 608 | * the window, scroll the window one line up. This avoids an extra |
| 609 | * redraw. |
| 610 | * This is detected when the cursor column is smaller after inserting |
| 611 | * something. |
| 612 | * Don't do this when the topline changed already, it has |
| 613 | * already been adjusted (by insertchar() calling open_line())). |
| 614 | */ |
| 615 | if (curbuf->b_mod_set |
| 616 | && curwin->w_p_wrap |
| 617 | && !did_backspace |
| 618 | && curwin->w_topline == old_topline |
| 619 | #ifdef FEAT_DIFF |
| 620 | && curwin->w_topfill == old_topfill |
| 621 | #endif |
| 622 | ) |
| 623 | { |
| 624 | mincol = curwin->w_wcol; |
| 625 | validate_cursor_col(); |
| 626 | |
| 627 | if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts |
| 628 | && curwin->w_wrow == W_WINROW(curwin) |
| 629 | + curwin->w_height - 1 - p_so |
| 630 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 631 | #ifdef FEAT_DIFF |
| 632 | || curwin->w_topfill > 0 |
| 633 | #endif |
| 634 | )) |
| 635 | { |
| 636 | #ifdef FEAT_DIFF |
| 637 | if (curwin->w_topfill > 0) |
| 638 | --curwin->w_topfill; |
| 639 | else |
| 640 | #endif |
| 641 | #ifdef FEAT_FOLDING |
| 642 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 643 | set_topline(curwin, old_topline + 1); |
| 644 | else |
| 645 | #endif |
| 646 | set_topline(curwin, curwin->w_topline + 1); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | /* May need to adjust w_topline to show the cursor. */ |
| 651 | update_topline(); |
| 652 | |
| 653 | did_backspace = FALSE; |
| 654 | |
| 655 | validate_cursor(); /* may set must_redraw */ |
| 656 | |
| 657 | /* |
| 658 | * Redraw the display when no characters are waiting. |
| 659 | * Also shows mode, ruler and positions cursor. |
| 660 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 661 | ins_redraw(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 662 | |
| 663 | #ifdef FEAT_SCROLLBIND |
| 664 | if (curwin->w_p_scb) |
| 665 | do_check_scrollbind(TRUE); |
| 666 | #endif |
| 667 | |
| 668 | update_curswant(); |
| 669 | old_topline = curwin->w_topline; |
| 670 | #ifdef FEAT_DIFF |
| 671 | old_topfill = curwin->w_topfill; |
| 672 | #endif |
| 673 | |
| 674 | #ifdef USE_ON_FLY_SCROLL |
| 675 | dont_scroll = FALSE; /* allow scrolling here */ |
| 676 | #endif |
| 677 | |
| 678 | /* |
| 679 | * Get a character for Insert mode. |
| 680 | */ |
| 681 | lastc = c; /* remember previous char for CTRL-D */ |
| 682 | c = safe_vgetc(); |
| 683 | |
| 684 | #ifdef FEAT_RIGHTLEFT |
| 685 | if (p_hkmap && KeyTyped) |
| 686 | c = hkmap(c); /* Hebrew mode mapping */ |
| 687 | #endif |
| 688 | #ifdef FEAT_FKMAP |
| 689 | if (p_fkmap && KeyTyped) |
| 690 | c = fkmap(c); /* Farsi mode mapping */ |
| 691 | #endif |
| 692 | |
| 693 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 694 | /* |
| 695 | * Special handling of keys while the popup menu is visible or wanted |
| 696 | * and the cursor is still in the completed word. |
| 697 | */ |
| 698 | if (compl_started && pum_wanted() && curwin->w_cursor.col >= compl_col) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 699 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 700 | /* BS: Delete one character from "compl_leader". */ |
| 701 | if ((c == K_BS || c == Ctrl_H) |
| 702 | && curwin->w_cursor.col > compl_col && ins_compl_bs()) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 703 | continue; |
| 704 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 705 | /* When no match was selected or it was edited. */ |
| 706 | if (!compl_used_match) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 707 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 708 | /* CTRL-L: Add one character from the current match to |
| 709 | * "compl_leader". */ |
| 710 | if (c == Ctrl_L) |
| 711 | { |
| 712 | ins_compl_addfrommatch(); |
| 713 | continue; |
| 714 | } |
| 715 | |
| 716 | /* A printable character: Add it to "compl_leader". */ |
| 717 | if (vim_isprintc(c)) |
| 718 | { |
| 719 | ins_compl_addleader(c); |
| 720 | continue; |
| 721 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 722 | |
| 723 | /* Pressing Enter selects the current match. */ |
| 724 | if (c == CAR || c == K_KENTER || c == NL) |
| 725 | { |
| 726 | ins_compl_delete(); |
| 727 | ins_compl_insert(); |
| 728 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 733 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 734 | compl_get_longest = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 735 | if (c != K_IGNORE && ins_compl_prep(c)) |
| 736 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | #endif |
| 738 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 739 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 740 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 741 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | if (c == Ctrl_BSL) |
| 743 | { |
| 744 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 745 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | ++no_mapping; |
| 747 | ++allow_keys; |
| 748 | c = safe_vgetc(); |
| 749 | --no_mapping; |
| 750 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 751 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 753 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | vungetc(c); |
| 755 | c = Ctrl_BSL; |
| 756 | } |
| 757 | else if (c == Ctrl_G && p_im) |
| 758 | continue; |
| 759 | else |
| 760 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 761 | if (c == Ctrl_O) |
| 762 | { |
| 763 | ins_ctrl_o(); |
| 764 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 765 | nomove = TRUE; |
| 766 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 767 | count = 0; |
| 768 | goto doESCkey; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | #ifdef FEAT_DIGRAPHS |
| 773 | c = do_digraph(c); |
| 774 | #endif |
| 775 | |
| 776 | #ifdef FEAT_INS_EXPAND |
| 777 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 778 | goto docomplete; |
| 779 | #endif |
| 780 | if (c == Ctrl_V || c == Ctrl_Q) |
| 781 | { |
| 782 | ins_ctrl_v(); |
| 783 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 784 | continue; |
| 785 | } |
| 786 | |
| 787 | #ifdef FEAT_CINDENT |
| 788 | if (cindent_on() |
| 789 | # ifdef FEAT_INS_EXPAND |
| 790 | && ctrl_x_mode == 0 |
| 791 | # endif |
| 792 | ) |
| 793 | { |
| 794 | /* A key name preceded by a bang means this key is not to be |
| 795 | * inserted. Skip ahead to the re-indenting below. |
| 796 | * A key name preceded by a star means that indenting has to be |
| 797 | * done before inserting the key. */ |
| 798 | line_is_white = inindent(0); |
| 799 | if (in_cinkeys(c, '!', line_is_white)) |
| 800 | goto force_cindent; |
| 801 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 802 | && stop_arrow() == OK) |
| 803 | do_c_expr_indent(); |
| 804 | } |
| 805 | #endif |
| 806 | |
| 807 | #ifdef FEAT_RIGHTLEFT |
| 808 | if (curwin->w_p_rl) |
| 809 | switch (c) |
| 810 | { |
| 811 | case K_LEFT: c = K_RIGHT; break; |
| 812 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 813 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 814 | case K_RIGHT: c = K_LEFT; break; |
| 815 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 816 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 817 | } |
| 818 | #endif |
| 819 | |
| 820 | #ifdef FEAT_VISUAL |
| 821 | /* |
| 822 | * If 'keymodel' contains "startsel", may start selection. If it |
| 823 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 824 | * characters. |
| 825 | */ |
| 826 | if (ins_start_select(c)) |
| 827 | continue; |
| 828 | #endif |
| 829 | |
| 830 | /* |
| 831 | * The big switch to handle a character in insert mode. |
| 832 | */ |
| 833 | switch (c) |
| 834 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 835 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 836 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 837 | break; |
| 838 | /*FALLTHROUGH*/ |
| 839 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 840 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 841 | #ifdef FEAT_CMDWIN |
| 842 | if (c == Ctrl_C && cmdwin_type != 0) |
| 843 | { |
| 844 | /* Close the cmdline window. */ |
| 845 | cmdwin_result = K_IGNORE; |
| 846 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
| 847 | goto doESCkey; |
| 848 | } |
| 849 | #endif |
| 850 | |
| 851 | #ifdef UNIX |
| 852 | do_intr: |
| 853 | #endif |
| 854 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 855 | * Insert mode */ |
| 856 | if (goto_im()) |
| 857 | { |
| 858 | if (got_int) |
| 859 | { |
| 860 | (void)vgetc(); /* flush all buffers */ |
| 861 | got_int = FALSE; |
| 862 | } |
| 863 | else |
| 864 | vim_beep(); |
| 865 | break; |
| 866 | } |
| 867 | doESCkey: |
| 868 | /* |
| 869 | * This is the ONLY return from edit()! |
| 870 | */ |
| 871 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 872 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 873 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 874 | o_lnum = curwin->w_cursor.lnum; |
| 875 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 876 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 877 | { |
| 878 | #ifdef FEAT_AUTOCMD |
| 879 | if (cmdchar != 'r' && cmdchar != 'v') |
| 880 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 881 | FALSE, curbuf); |
| 882 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 884 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | continue; |
| 886 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 887 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 888 | if (!p_im) |
| 889 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 890 | stuffReadbuff((char_u *)":st\r"); |
| 891 | c = Ctrl_O; |
| 892 | /*FALLTHROUGH*/ |
| 893 | |
| 894 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 895 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 896 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 897 | goto docomplete; |
| 898 | #endif |
| 899 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 900 | break; |
| 901 | ins_ctrl_o(); |
| 902 | count = 0; |
| 903 | goto doESCkey; |
| 904 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 905 | case K_INS: /* toggle insert/replace mode */ |
| 906 | case K_KINS: |
| 907 | ins_insert(replaceState); |
| 908 | break; |
| 909 | |
| 910 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 911 | break; |
| 912 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 913 | #ifdef FEAT_SNIFF |
| 914 | case K_SNIFF: /* Sniff command received */ |
| 915 | stuffcharReadbuff(K_SNIFF); |
| 916 | goto doESCkey; |
| 917 | #endif |
| 918 | |
| 919 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 920 | case K_F1: |
| 921 | case K_XF1: |
| 922 | stuffcharReadbuff(K_HELP); |
| 923 | if (p_im) |
| 924 | need_start_insertmode = TRUE; |
| 925 | goto doESCkey; |
| 926 | |
| 927 | #ifdef FEAT_NETBEANS_INTG |
| 928 | case K_F21: /* NetBeans command */ |
| 929 | ++no_mapping; /* don't map the next key hits */ |
| 930 | i = safe_vgetc(); |
| 931 | --no_mapping; |
| 932 | netbeans_keycommand(i); |
| 933 | break; |
| 934 | #endif |
| 935 | |
| 936 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | case NUL: |
| 938 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 939 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 940 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 941 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 942 | && c != Ctrl_A && !p_im) |
| 943 | goto doESCkey; /* quit insert mode */ |
| 944 | inserted_space = FALSE; |
| 945 | break; |
| 946 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 947 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 948 | ins_reg(); |
| 949 | auto_format(FALSE, TRUE); |
| 950 | inserted_space = FALSE; |
| 951 | break; |
| 952 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 953 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 954 | ins_ctrl_g(); |
| 955 | break; |
| 956 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 957 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 958 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 959 | break; |
| 960 | |
| 961 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 962 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 963 | if (!p_ari) |
| 964 | goto normalchar; |
| 965 | ins_ctrl_(); |
| 966 | break; |
| 967 | #endif |
| 968 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 969 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 970 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 971 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 972 | goto docomplete; |
| 973 | #endif |
| 974 | /* FALLTHROUGH */ |
| 975 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 976 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 977 | # ifdef FEAT_INS_EXPAND |
| 978 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 979 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 980 | if (has_compl_option(FALSE)) |
| 981 | goto docomplete; |
| 982 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 983 | } |
| 984 | # endif |
| 985 | ins_shift(c, lastc); |
| 986 | auto_format(FALSE, TRUE); |
| 987 | inserted_space = FALSE; |
| 988 | break; |
| 989 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 990 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 991 | case K_KDEL: |
| 992 | ins_del(); |
| 993 | auto_format(FALSE, TRUE); |
| 994 | break; |
| 995 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 996 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 997 | case Ctrl_H: |
| 998 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 999 | auto_format(FALSE, TRUE); |
| 1000 | break; |
| 1001 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1002 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1003 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 1004 | auto_format(FALSE, TRUE); |
| 1005 | break; |
| 1006 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1007 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1008 | # ifdef FEAT_COMPL_FUNC |
| 1009 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1010 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1011 | goto docomplete; |
| 1012 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1013 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 1014 | auto_format(FALSE, TRUE); |
| 1015 | inserted_space = FALSE; |
| 1016 | break; |
| 1017 | |
| 1018 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1019 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1020 | case K_LEFTMOUSE_NM: |
| 1021 | case K_LEFTDRAG: |
| 1022 | case K_LEFTRELEASE: |
| 1023 | case K_LEFTRELEASE_NM: |
| 1024 | case K_MIDDLEMOUSE: |
| 1025 | case K_MIDDLEDRAG: |
| 1026 | case K_MIDDLERELEASE: |
| 1027 | case K_RIGHTMOUSE: |
| 1028 | case K_RIGHTDRAG: |
| 1029 | case K_RIGHTRELEASE: |
| 1030 | case K_X1MOUSE: |
| 1031 | case K_X1DRAG: |
| 1032 | case K_X1RELEASE: |
| 1033 | case K_X2MOUSE: |
| 1034 | case K_X2DRAG: |
| 1035 | case K_X2RELEASE: |
| 1036 | ins_mouse(c); |
| 1037 | break; |
| 1038 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1039 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | ins_mousescroll(FALSE); |
| 1041 | break; |
| 1042 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1043 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1044 | ins_mousescroll(TRUE); |
| 1045 | break; |
| 1046 | #endif |
| 1047 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1048 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1049 | break; |
| 1050 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1051 | #ifdef FEAT_AUTOCMD |
| 1052 | case K_CURSORHOLD: /* Didn't type something for a while. */ |
| 1053 | apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); |
| 1054 | did_cursorhold = TRUE; |
| 1055 | break; |
| 1056 | #endif |
| 1057 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1058 | #ifdef FEAT_GUI_W32 |
| 1059 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1060 | * cancelled. */ |
| 1061 | case K_F4: |
| 1062 | if (mod_mask != MOD_MASK_ALT) |
| 1063 | goto normalchar; |
| 1064 | break; |
| 1065 | #endif |
| 1066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | #ifdef FEAT_GUI |
| 1068 | case K_VER_SCROLLBAR: |
| 1069 | ins_scroll(); |
| 1070 | break; |
| 1071 | |
| 1072 | case K_HOR_SCROLLBAR: |
| 1073 | ins_horscroll(); |
| 1074 | break; |
| 1075 | #endif |
| 1076 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1077 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | case K_S_HOME: |
| 1080 | case K_C_HOME: |
| 1081 | ins_home(c); |
| 1082 | break; |
| 1083 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1084 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1085 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1086 | case K_S_END: |
| 1087 | case K_C_END: |
| 1088 | ins_end(c); |
| 1089 | break; |
| 1090 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1091 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1092 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1093 | ins_s_left(); |
| 1094 | else |
| 1095 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1096 | break; |
| 1097 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1098 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1099 | case K_C_LEFT: |
| 1100 | ins_s_left(); |
| 1101 | break; |
| 1102 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1103 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1104 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1105 | ins_s_right(); |
| 1106 | else |
| 1107 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | break; |
| 1109 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1110 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1111 | case K_C_RIGHT: |
| 1112 | ins_s_right(); |
| 1113 | break; |
| 1114 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1115 | case K_UP: /* <Up> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1116 | #ifdef FEAT_INS_EXPAND |
| 1117 | if (pum_visible()) |
| 1118 | goto docomplete; |
| 1119 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1120 | if (mod_mask & MOD_MASK_SHIFT) |
| 1121 | ins_pageup(); |
| 1122 | else |
| 1123 | ins_up(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1124 | break; |
| 1125 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1126 | case K_S_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1127 | case K_PAGEUP: |
| 1128 | case K_KPAGEUP: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1129 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1130 | if (pum_visible()) |
| 1131 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1132 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1133 | ins_pageup(); |
| 1134 | break; |
| 1135 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1136 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1137 | #ifdef FEAT_INS_EXPAND |
| 1138 | if (pum_visible()) |
| 1139 | goto docomplete; |
| 1140 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1141 | if (mod_mask & MOD_MASK_SHIFT) |
| 1142 | ins_pagedown(); |
| 1143 | else |
| 1144 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1145 | break; |
| 1146 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1147 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1148 | case K_PAGEDOWN: |
| 1149 | case K_KPAGEDOWN: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1150 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1151 | if (pum_visible()) |
| 1152 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1153 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1154 | ins_pagedown(); |
| 1155 | break; |
| 1156 | |
| 1157 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1158 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1159 | ins_drop(); |
| 1160 | break; |
| 1161 | #endif |
| 1162 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1163 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1164 | c = TAB; |
| 1165 | /* FALLTHROUGH */ |
| 1166 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1167 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1168 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1169 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1170 | goto docomplete; |
| 1171 | #endif |
| 1172 | inserted_space = FALSE; |
| 1173 | if (ins_tab()) |
| 1174 | goto normalchar; /* insert TAB as a normal char */ |
| 1175 | auto_format(FALSE, TRUE); |
| 1176 | break; |
| 1177 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1178 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | c = CAR; |
| 1180 | /* FALLTHROUGH */ |
| 1181 | case CAR: |
| 1182 | case NL: |
| 1183 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1184 | /* In a quickfix window a <CR> jumps to the error under the |
| 1185 | * cursor. */ |
| 1186 | if (bt_quickfix(curbuf) && c == CAR) |
| 1187 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1188 | if (curwin->w_llist_ref == NULL) /* quickfix window */ |
| 1189 | do_cmdline_cmd((char_u *)".cc"); |
| 1190 | else /* location list window */ |
| 1191 | do_cmdline_cmd((char_u *)".ll"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1192 | break; |
| 1193 | } |
| 1194 | #endif |
| 1195 | #ifdef FEAT_CMDWIN |
| 1196 | if (cmdwin_type != 0) |
| 1197 | { |
| 1198 | /* Execute the command in the cmdline window. */ |
| 1199 | cmdwin_result = CAR; |
| 1200 | goto doESCkey; |
| 1201 | } |
| 1202 | #endif |
| 1203 | if (ins_eol(c) && !p_im) |
| 1204 | goto doESCkey; /* out of memory */ |
| 1205 | auto_format(FALSE, FALSE); |
| 1206 | inserted_space = FALSE; |
| 1207 | break; |
| 1208 | |
| 1209 | #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1210 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1211 | # ifdef FEAT_INS_EXPAND |
| 1212 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1213 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1214 | if (has_compl_option(TRUE)) |
| 1215 | goto docomplete; |
| 1216 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | } |
| 1218 | # endif |
| 1219 | # ifdef FEAT_DIGRAPHS |
| 1220 | c = ins_digraph(); |
| 1221 | if (c == NUL) |
| 1222 | break; |
| 1223 | # endif |
| 1224 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1225 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1226 | |
| 1227 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1228 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1229 | ins_ctrl_x(); |
| 1230 | break; |
| 1231 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1232 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1234 | goto normalchar; |
| 1235 | goto docomplete; |
| 1236 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1237 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1238 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1239 | goto normalchar; |
| 1240 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1241 | |
| 1242 | case 's': /* Spelling completion after ^X */ |
| 1243 | case Ctrl_S: |
| 1244 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1245 | goto normalchar; |
| 1246 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | #endif |
| 1248 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1249 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | #ifdef FEAT_INS_EXPAND |
| 1251 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1252 | #endif |
| 1253 | { |
| 1254 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1255 | if (p_im) |
| 1256 | { |
| 1257 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1258 | break; |
| 1259 | goto doESCkey; |
| 1260 | } |
| 1261 | goto normalchar; |
| 1262 | } |
| 1263 | #ifdef FEAT_INS_EXPAND |
| 1264 | /* FALLTHROUGH */ |
| 1265 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1266 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1267 | case Ctrl_N: |
| 1268 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1269 | * but it is under other ^X modes */ |
| 1270 | if (*curbuf->b_p_cpt == NUL |
| 1271 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1272 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | goto normalchar; |
| 1274 | |
| 1275 | docomplete: |
| 1276 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1277 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | break; |
| 1279 | #endif /* FEAT_INS_EXPAND */ |
| 1280 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1281 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1282 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1283 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1284 | break; |
| 1285 | |
| 1286 | default: |
| 1287 | #ifdef UNIX |
| 1288 | if (c == intr_char) /* special interrupt char */ |
| 1289 | goto do_intr; |
| 1290 | #endif |
| 1291 | |
| 1292 | /* |
| 1293 | * Insert a nomal character. |
| 1294 | */ |
| 1295 | normalchar: |
| 1296 | #ifdef FEAT_SMARTINDENT |
| 1297 | /* Try to perform smart-indenting. */ |
| 1298 | ins_try_si(c); |
| 1299 | #endif |
| 1300 | |
| 1301 | if (c == ' ') |
| 1302 | { |
| 1303 | inserted_space = TRUE; |
| 1304 | #ifdef FEAT_CINDENT |
| 1305 | if (inindent(0)) |
| 1306 | can_cindent = FALSE; |
| 1307 | #endif |
| 1308 | if (Insstart_blank_vcol == MAXCOL |
| 1309 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1310 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1311 | } |
| 1312 | |
| 1313 | if (vim_iswordc(c) || !echeck_abbr( |
| 1314 | #ifdef FEAT_MBYTE |
| 1315 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1316 | * what check_abbr() expects. */ |
| 1317 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1318 | #endif |
| 1319 | c)) |
| 1320 | { |
| 1321 | insert_special(c, FALSE, FALSE); |
| 1322 | #ifdef FEAT_RIGHTLEFT |
| 1323 | revins_legal++; |
| 1324 | revins_chars++; |
| 1325 | #endif |
| 1326 | } |
| 1327 | |
| 1328 | auto_format(FALSE, TRUE); |
| 1329 | |
| 1330 | #ifdef FEAT_FOLDING |
| 1331 | /* When inserting a character the cursor line must never be in a |
| 1332 | * closed fold. */ |
| 1333 | foldOpenCursor(); |
| 1334 | #endif |
| 1335 | break; |
| 1336 | } /* end of switch (c) */ |
| 1337 | |
| 1338 | /* If the cursor was moved we didn't just insert a space */ |
| 1339 | if (arrow_used) |
| 1340 | inserted_space = FALSE; |
| 1341 | |
| 1342 | #ifdef FEAT_CINDENT |
| 1343 | if (can_cindent && cindent_on() |
| 1344 | # ifdef FEAT_INS_EXPAND |
| 1345 | && ctrl_x_mode == 0 |
| 1346 | # endif |
| 1347 | ) |
| 1348 | { |
| 1349 | force_cindent: |
| 1350 | /* |
| 1351 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1352 | */ |
| 1353 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1354 | { |
| 1355 | if (stop_arrow() == OK) |
| 1356 | /* re-indent the current line */ |
| 1357 | do_c_expr_indent(); |
| 1358 | } |
| 1359 | } |
| 1360 | #endif /* FEAT_CINDENT */ |
| 1361 | |
| 1362 | } /* for (;;) */ |
| 1363 | /* NOTREACHED */ |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | * Redraw for Insert mode. |
| 1368 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1369 | * option work correctly. |
| 1370 | * Only redraw when there are no characters available. This speeds up |
| 1371 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1372 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1373 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1374 | static void |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1375 | ins_redraw(ready) |
| 1376 | int ready; /* not busy with something */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | { |
| 1378 | if (!char_avail()) |
| 1379 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1380 | #ifdef FEAT_AUTOCMD |
| 1381 | /* Trigger CursorMoved if the cursor moved. */ |
| 1382 | if (ready && has_cursormovedI() |
| 1383 | && !equalpos(last_cursormoved, curwin->w_cursor)) |
| 1384 | { |
| 1385 | apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); |
| 1386 | last_cursormoved = curwin->w_cursor; |
| 1387 | } |
| 1388 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | if (must_redraw) |
| 1390 | update_screen(0); |
| 1391 | else if (clear_cmdline || redraw_cmdline) |
| 1392 | showmode(); /* clear cmdline and show mode */ |
| 1393 | showruler(FALSE); |
| 1394 | setcursor(); |
| 1395 | emsg_on_display = FALSE; /* may remove error message now */ |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1401 | */ |
| 1402 | static void |
| 1403 | ins_ctrl_v() |
| 1404 | { |
| 1405 | int c; |
| 1406 | |
| 1407 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1408 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1409 | |
| 1410 | if (redrawing() && !char_avail()) |
| 1411 | edit_putchar('^', TRUE); |
| 1412 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1413 | |
| 1414 | #ifdef FEAT_CMDL_INFO |
| 1415 | add_to_showcmd_c(Ctrl_V); |
| 1416 | #endif |
| 1417 | |
| 1418 | c = get_literal(); |
| 1419 | #ifdef FEAT_CMDL_INFO |
| 1420 | clear_showcmd(); |
| 1421 | #endif |
| 1422 | insert_special(c, FALSE, TRUE); |
| 1423 | #ifdef FEAT_RIGHTLEFT |
| 1424 | revins_chars++; |
| 1425 | revins_legal++; |
| 1426 | #endif |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1431 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1432 | */ |
| 1433 | static int pc_status; |
| 1434 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1435 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1436 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1437 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
| 1438 | #ifdef FEAT_MBYTE |
| 1439 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
| 1440 | #else |
| 1441 | static char_u pc_bytes[2]; /* saved bytes */ |
| 1442 | #endif |
| 1443 | static int pc_attr; |
| 1444 | static int pc_row; |
| 1445 | static int pc_col; |
| 1446 | |
| 1447 | void |
| 1448 | edit_putchar(c, highlight) |
| 1449 | int c; |
| 1450 | int highlight; |
| 1451 | { |
| 1452 | int attr; |
| 1453 | |
| 1454 | if (ScreenLines != NULL) |
| 1455 | { |
| 1456 | update_topline(); /* just in case w_topline isn't valid */ |
| 1457 | validate_cursor(); |
| 1458 | if (highlight) |
| 1459 | attr = hl_attr(HLF_8); |
| 1460 | else |
| 1461 | attr = 0; |
| 1462 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1463 | pc_col = W_WINCOL(curwin); |
| 1464 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1465 | pc_status = PC_STATUS_UNSET; |
| 1466 | #endif |
| 1467 | #ifdef FEAT_RIGHTLEFT |
| 1468 | if (curwin->w_p_rl) |
| 1469 | { |
| 1470 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1471 | # ifdef FEAT_MBYTE |
| 1472 | if (has_mbyte) |
| 1473 | { |
| 1474 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1475 | |
| 1476 | if (fix_col != pc_col) |
| 1477 | { |
| 1478 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1479 | --curwin->w_wcol; |
| 1480 | pc_status = PC_STATUS_RIGHT; |
| 1481 | } |
| 1482 | } |
| 1483 | # endif |
| 1484 | } |
| 1485 | else |
| 1486 | #endif |
| 1487 | { |
| 1488 | pc_col += curwin->w_wcol; |
| 1489 | #ifdef FEAT_MBYTE |
| 1490 | if (mb_lefthalve(pc_row, pc_col)) |
| 1491 | pc_status = PC_STATUS_LEFT; |
| 1492 | #endif |
| 1493 | } |
| 1494 | |
| 1495 | /* save the character to be able to put it back */ |
| 1496 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1497 | if (pc_status == PC_STATUS_UNSET) |
| 1498 | #endif |
| 1499 | { |
| 1500 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1501 | pc_status = PC_STATUS_SET; |
| 1502 | } |
| 1503 | screen_putchar(c, pc_row, pc_col, attr); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * Undo the previous edit_putchar(). |
| 1509 | */ |
| 1510 | void |
| 1511 | edit_unputchar() |
| 1512 | { |
| 1513 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1514 | { |
| 1515 | #if defined(FEAT_MBYTE) |
| 1516 | if (pc_status == PC_STATUS_RIGHT) |
| 1517 | ++curwin->w_wcol; |
| 1518 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1519 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1520 | else |
| 1521 | #endif |
| 1522 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1528 | * Only works when cursor is in the line that changes. |
| 1529 | */ |
| 1530 | void |
| 1531 | display_dollar(col) |
| 1532 | colnr_T col; |
| 1533 | { |
| 1534 | colnr_T save_col; |
| 1535 | |
| 1536 | if (!redrawing()) |
| 1537 | return; |
| 1538 | |
| 1539 | cursor_off(); |
| 1540 | save_col = curwin->w_cursor.col; |
| 1541 | curwin->w_cursor.col = col; |
| 1542 | #ifdef FEAT_MBYTE |
| 1543 | if (has_mbyte) |
| 1544 | { |
| 1545 | char_u *p; |
| 1546 | |
| 1547 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1548 | p = ml_get_curline(); |
| 1549 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1550 | } |
| 1551 | #endif |
| 1552 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1553 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1554 | { |
| 1555 | edit_putchar('$', FALSE); |
| 1556 | dollar_vcol = curwin->w_virtcol; |
| 1557 | } |
| 1558 | curwin->w_cursor.col = save_col; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * Call this function before moving the cursor from the normal insert position |
| 1563 | * in insert mode. |
| 1564 | */ |
| 1565 | static void |
| 1566 | undisplay_dollar() |
| 1567 | { |
| 1568 | if (dollar_vcol) |
| 1569 | { |
| 1570 | dollar_vcol = 0; |
| 1571 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1577 | * Keep the cursor on the same character. |
| 1578 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1579 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1580 | * type == INDENT_SET set indent to "amount" |
| 1581 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1582 | */ |
| 1583 | void |
| 1584 | change_indent(type, amount, round, replaced) |
| 1585 | int type; |
| 1586 | int amount; |
| 1587 | int round; |
| 1588 | int replaced; /* replaced character, put on replace stack */ |
| 1589 | { |
| 1590 | int vcol; |
| 1591 | int last_vcol; |
| 1592 | int insstart_less; /* reduction for Insstart.col */ |
| 1593 | int new_cursor_col; |
| 1594 | int i; |
| 1595 | char_u *ptr; |
| 1596 | int save_p_list; |
| 1597 | int start_col; |
| 1598 | colnr_T vc; |
| 1599 | #ifdef FEAT_VREPLACE |
| 1600 | colnr_T orig_col = 0; /* init for GCC */ |
| 1601 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1602 | |
| 1603 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1604 | if (State & VREPLACE_FLAG) |
| 1605 | { |
| 1606 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1607 | orig_col = curwin->w_cursor.col; |
| 1608 | } |
| 1609 | #endif |
| 1610 | |
| 1611 | /* for the following tricks we don't want list mode */ |
| 1612 | save_p_list = curwin->w_p_list; |
| 1613 | curwin->w_p_list = FALSE; |
| 1614 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1615 | vcol = vc; |
| 1616 | |
| 1617 | /* |
| 1618 | * For Replace mode we need to fix the replace stack later, which is only |
| 1619 | * possible when the cursor is in the indent. Remember the number of |
| 1620 | * characters before the cursor if it's possible. |
| 1621 | */ |
| 1622 | start_col = curwin->w_cursor.col; |
| 1623 | |
| 1624 | /* determine offset from first non-blank */ |
| 1625 | new_cursor_col = curwin->w_cursor.col; |
| 1626 | beginline(BL_WHITE); |
| 1627 | new_cursor_col -= curwin->w_cursor.col; |
| 1628 | |
| 1629 | insstart_less = curwin->w_cursor.col; |
| 1630 | |
| 1631 | /* |
| 1632 | * If the cursor is in the indent, compute how many screen columns the |
| 1633 | * cursor is to the left of the first non-blank. |
| 1634 | */ |
| 1635 | if (new_cursor_col < 0) |
| 1636 | vcol = get_indent() - vcol; |
| 1637 | |
| 1638 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1639 | start_col = -1; |
| 1640 | |
| 1641 | /* |
| 1642 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1643 | */ |
| 1644 | if (type == INDENT_SET) |
| 1645 | (void)set_indent(amount, SIN_CHANGED); |
| 1646 | else |
| 1647 | { |
| 1648 | #ifdef FEAT_VREPLACE |
| 1649 | int save_State = State; |
| 1650 | |
| 1651 | /* Avoid being called recursively. */ |
| 1652 | if (State & VREPLACE_FLAG) |
| 1653 | State = INSERT; |
| 1654 | #endif |
| 1655 | shift_line(type == INDENT_DEC, round, 1); |
| 1656 | #ifdef FEAT_VREPLACE |
| 1657 | State = save_State; |
| 1658 | #endif |
| 1659 | } |
| 1660 | insstart_less -= curwin->w_cursor.col; |
| 1661 | |
| 1662 | /* |
| 1663 | * Try to put cursor on same character. |
| 1664 | * If the cursor is at or after the first non-blank in the line, |
| 1665 | * compute the cursor column relative to the column of the first |
| 1666 | * non-blank character. |
| 1667 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1668 | * If the cursor is before the first non-blank, position it relative |
| 1669 | * to the first non-blank, counted in screen columns. |
| 1670 | */ |
| 1671 | if (new_cursor_col >= 0) |
| 1672 | { |
| 1673 | /* |
| 1674 | * When changing the indent while the cursor is touching it, reset |
| 1675 | * Insstart_col to 0. |
| 1676 | */ |
| 1677 | if (new_cursor_col == 0) |
| 1678 | insstart_less = MAXCOL; |
| 1679 | new_cursor_col += curwin->w_cursor.col; |
| 1680 | } |
| 1681 | else if (!(State & INSERT)) |
| 1682 | new_cursor_col = curwin->w_cursor.col; |
| 1683 | else |
| 1684 | { |
| 1685 | /* |
| 1686 | * Compute the screen column where the cursor should be. |
| 1687 | */ |
| 1688 | vcol = get_indent() - vcol; |
| 1689 | curwin->w_virtcol = (vcol < 0) ? 0 : vcol; |
| 1690 | |
| 1691 | /* |
| 1692 | * Advance the cursor until we reach the right screen column. |
| 1693 | */ |
| 1694 | vcol = last_vcol = 0; |
| 1695 | new_cursor_col = -1; |
| 1696 | ptr = ml_get_curline(); |
| 1697 | while (vcol <= (int)curwin->w_virtcol) |
| 1698 | { |
| 1699 | last_vcol = vcol; |
| 1700 | #ifdef FEAT_MBYTE |
| 1701 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1702 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1703 | else |
| 1704 | #endif |
| 1705 | ++new_cursor_col; |
| 1706 | vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol); |
| 1707 | } |
| 1708 | vcol = last_vcol; |
| 1709 | |
| 1710 | /* |
| 1711 | * May need to insert spaces to be able to position the cursor on |
| 1712 | * the right screen column. |
| 1713 | */ |
| 1714 | if (vcol != (int)curwin->w_virtcol) |
| 1715 | { |
| 1716 | curwin->w_cursor.col = new_cursor_col; |
| 1717 | i = (int)curwin->w_virtcol - vcol; |
| 1718 | ptr = alloc(i + 1); |
| 1719 | if (ptr != NULL) |
| 1720 | { |
| 1721 | new_cursor_col += i; |
| 1722 | ptr[i] = NUL; |
| 1723 | while (--i >= 0) |
| 1724 | ptr[i] = ' '; |
| 1725 | ins_str(ptr); |
| 1726 | vim_free(ptr); |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | /* |
| 1731 | * When changing the indent while the cursor is in it, reset |
| 1732 | * Insstart_col to 0. |
| 1733 | */ |
| 1734 | insstart_less = MAXCOL; |
| 1735 | } |
| 1736 | |
| 1737 | curwin->w_p_list = save_p_list; |
| 1738 | |
| 1739 | if (new_cursor_col <= 0) |
| 1740 | curwin->w_cursor.col = 0; |
| 1741 | else |
| 1742 | curwin->w_cursor.col = new_cursor_col; |
| 1743 | curwin->w_set_curswant = TRUE; |
| 1744 | changed_cline_bef_curs(); |
| 1745 | |
| 1746 | /* |
| 1747 | * May have to adjust the start of the insert. |
| 1748 | */ |
| 1749 | if (State & INSERT) |
| 1750 | { |
| 1751 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1752 | { |
| 1753 | if ((int)Insstart.col <= insstart_less) |
| 1754 | Insstart.col = 0; |
| 1755 | else |
| 1756 | Insstart.col -= insstart_less; |
| 1757 | } |
| 1758 | if ((int)ai_col <= insstart_less) |
| 1759 | ai_col = 0; |
| 1760 | else |
| 1761 | ai_col -= insstart_less; |
| 1762 | } |
| 1763 | |
| 1764 | /* |
| 1765 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 1766 | * If the number of characters before the cursor decreased, need to pop a |
| 1767 | * few characters from the replace stack. |
| 1768 | * If the number of characters before the cursor increased, need to push a |
| 1769 | * few NULs onto the replace stack. |
| 1770 | */ |
| 1771 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1772 | { |
| 1773 | while (start_col > (int)curwin->w_cursor.col) |
| 1774 | { |
| 1775 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1776 | --start_col; |
| 1777 | } |
| 1778 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1779 | { |
| 1780 | replace_push(NUL); |
| 1781 | if (replaced) |
| 1782 | { |
| 1783 | replace_push(replaced); |
| 1784 | replaced = NUL; |
| 1785 | } |
| 1786 | ++start_col; |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | #ifdef FEAT_VREPLACE |
| 1791 | /* |
| 1792 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 1793 | * it is always possible because we backspace over the whole line and then |
| 1794 | * put it back again the way we wanted it. |
| 1795 | */ |
| 1796 | if (State & VREPLACE_FLAG) |
| 1797 | { |
| 1798 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 1799 | * even if you can't backspace. */ |
| 1800 | if (orig_line == NULL) |
| 1801 | return; |
| 1802 | |
| 1803 | /* Save new line */ |
| 1804 | new_line = vim_strsave(ml_get_curline()); |
| 1805 | if (new_line == NULL) |
| 1806 | return; |
| 1807 | |
| 1808 | /* We only put back the new line up to the cursor */ |
| 1809 | new_line[curwin->w_cursor.col] = NUL; |
| 1810 | |
| 1811 | /* Put back original line */ |
| 1812 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 1813 | curwin->w_cursor.col = orig_col; |
| 1814 | |
| 1815 | /* Backspace from cursor to start of line */ |
| 1816 | backspace_until_column(0); |
| 1817 | |
| 1818 | /* Insert new stuff into line again */ |
| 1819 | ins_bytes(new_line); |
| 1820 | |
| 1821 | vim_free(new_line); |
| 1822 | } |
| 1823 | #endif |
| 1824 | } |
| 1825 | |
| 1826 | /* |
| 1827 | * Truncate the space at the end of a line. This is to be used only in an |
| 1828 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 1829 | * modes. |
| 1830 | */ |
| 1831 | void |
| 1832 | truncate_spaces(line) |
| 1833 | char_u *line; |
| 1834 | { |
| 1835 | int i; |
| 1836 | |
| 1837 | /* find start of trailing white space */ |
| 1838 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 1839 | { |
| 1840 | if (State & REPLACE_FLAG) |
| 1841 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1842 | } |
| 1843 | line[i + 1] = NUL; |
| 1844 | } |
| 1845 | |
| 1846 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 1847 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 1848 | /* |
| 1849 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 1850 | * modes correctly. May also be used when not in insert mode at all. |
| 1851 | */ |
| 1852 | void |
| 1853 | backspace_until_column(col) |
| 1854 | int col; |
| 1855 | { |
| 1856 | while ((int)curwin->w_cursor.col > col) |
| 1857 | { |
| 1858 | curwin->w_cursor.col--; |
| 1859 | if (State & REPLACE_FLAG) |
| 1860 | replace_do_bs(); |
| 1861 | else |
| 1862 | (void)del_char(FALSE); |
| 1863 | } |
| 1864 | } |
| 1865 | #endif |
| 1866 | |
| 1867 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 1868 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1869 | * CTRL-X pressed in Insert mode. |
| 1870 | */ |
| 1871 | static void |
| 1872 | ins_ctrl_x() |
| 1873 | { |
| 1874 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 1875 | * CTRL-V works like CTRL-N */ |
| 1876 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 1877 | { |
| 1878 | /* if the next ^X<> won't ADD nothing, then reset |
| 1879 | * compl_cont_status */ |
| 1880 | if (compl_cont_status & CONT_N_ADDS) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1881 | compl_cont_status |= CONT_INTRPT; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1882 | else |
| 1883 | compl_cont_status = 0; |
| 1884 | /* We're not sure which CTRL-X mode it will be yet */ |
| 1885 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 1886 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 1887 | edit_submode_pre = NULL; |
| 1888 | showmode(); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | /* |
| 1893 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 1894 | */ |
| 1895 | static int |
| 1896 | has_compl_option(dict_opt) |
| 1897 | int dict_opt; |
| 1898 | { |
| 1899 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL) |
| 1900 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 1901 | { |
| 1902 | ctrl_x_mode = 0; |
| 1903 | edit_submode = NULL; |
| 1904 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 1905 | : (char_u *)_("'thesaurus' option is empty"), |
| 1906 | hl_attr(HLF_E)); |
| 1907 | if (emsg_silent == 0) |
| 1908 | { |
| 1909 | vim_beep(); |
| 1910 | setcursor(); |
| 1911 | out_flush(); |
| 1912 | ui_delay(2000L, FALSE); |
| 1913 | } |
| 1914 | return FALSE; |
| 1915 | } |
| 1916 | return TRUE; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1920 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 1921 | * This depends on the current mode. |
| 1922 | */ |
| 1923 | int |
| 1924 | vim_is_ctrl_x_key(c) |
| 1925 | int c; |
| 1926 | { |
| 1927 | /* Always allow ^R - let it's results then be checked */ |
| 1928 | if (c == Ctrl_R) |
| 1929 | return TRUE; |
| 1930 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1931 | /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1932 | if (ins_compl_pum_key(c)) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1933 | return TRUE; |
| 1934 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1935 | switch (ctrl_x_mode) |
| 1936 | { |
| 1937 | case 0: /* Not in any CTRL-X mode */ |
| 1938 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 1939 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1940 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1941 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 1942 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 1943 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1944 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
| 1945 | || c == Ctrl_S || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1946 | case CTRL_X_SCROLL: |
| 1947 | return (c == Ctrl_Y || c == Ctrl_E); |
| 1948 | case CTRL_X_WHOLE_LINE: |
| 1949 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 1950 | case CTRL_X_FILES: |
| 1951 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 1952 | case CTRL_X_DICTIONARY: |
| 1953 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 1954 | case CTRL_X_THESAURUS: |
| 1955 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 1956 | case CTRL_X_TAGS: |
| 1957 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 1958 | #ifdef FEAT_FIND_ID |
| 1959 | case CTRL_X_PATH_PATTERNS: |
| 1960 | return (c == Ctrl_P || c == Ctrl_N); |
| 1961 | case CTRL_X_PATH_DEFINES: |
| 1962 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 1963 | #endif |
| 1964 | case CTRL_X_CMDLINE: |
| 1965 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 1966 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1967 | #ifdef FEAT_COMPL_FUNC |
| 1968 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1969 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 1970 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1971 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1972 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1973 | case CTRL_X_SPELL: |
| 1974 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1975 | } |
| 1976 | EMSG(_(e_internal)); |
| 1977 | return FALSE; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 1981 | * 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] | 1982 | * case of the originally typed text is used, and the case of the completed |
| 1983 | * text is infered, ie this tries to work out what case you probably wanted |
| 1984 | * the rest of the word to be in -- webb |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1985 | * TODO: make this work for multi-byte characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1986 | */ |
| 1987 | int |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 1988 | ins_compl_add_infercase(str, len, icase, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1989 | char_u *str; |
| 1990 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 1991 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1992 | char_u *fname; |
| 1993 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1994 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1995 | { |
| 1996 | int has_lower = FALSE; |
| 1997 | int was_letter = FALSE; |
| 1998 | int idx; |
| 1999 | |
| 2000 | if (p_ic && curbuf->b_p_inf && len < IOSIZE) |
| 2001 | { |
| 2002 | /* Infer case of completed part -- webb */ |
| 2003 | /* Use IObuff, str would change text in buffer! */ |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2004 | vim_strncpy(IObuff, str, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2005 | |
| 2006 | /* Rule 1: Were any chars converted to lower? */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2007 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2008 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2009 | if (islower(compl_orig_text[idx])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2010 | { |
| 2011 | has_lower = TRUE; |
| 2012 | if (isupper(IObuff[idx])) |
| 2013 | { |
| 2014 | /* Rule 1 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2015 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2016 | IObuff[idx] = TOLOWER_LOC(IObuff[idx]); |
| 2017 | break; |
| 2018 | } |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | /* |
| 2023 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 2024 | * upper case. |
| 2025 | */ |
| 2026 | if (!has_lower) |
| 2027 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2028 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2029 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2030 | if (was_letter && isupper(compl_orig_text[idx]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2031 | && islower(IObuff[idx])) |
| 2032 | { |
| 2033 | /* Rule 2 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2034 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2035 | IObuff[idx] = TOUPPER_LOC(IObuff[idx]); |
| 2036 | break; |
| 2037 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2038 | was_letter = isalpha(compl_orig_text[idx]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2039 | } |
| 2040 | } |
| 2041 | |
| 2042 | /* Copy the original case of the part we typed */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2043 | STRNCPY(IObuff, compl_orig_text, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2044 | |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2045 | return ins_compl_add(IObuff, len, icase, fname, NULL, dir, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2047 | return ins_compl_add(str, len, icase, fname, NULL, dir, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | /* |
| 2051 | * Add a match to the list of matches. |
| 2052 | * If the given string is already in the list of completions, then return |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2053 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2054 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2055 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2056 | int |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2057 | ins_compl_add(str, len, icase, fname, extra, cdir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | char_u *str; |
| 2059 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2060 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2084 | && ins_compl_equal(match, str, len) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 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 | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2112 | match->cp_icase = icase; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2113 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2114 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2115 | * - 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] | 2116 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 2117 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2118 | if (fname != NULL |
| 2119 | && compl_curr_match |
| 2120 | && compl_curr_match->cp_fname != NULL |
| 2121 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2122 | match->cp_fname = compl_curr_match->cp_fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2123 | else if (fname != NULL) |
| 2124 | { |
| 2125 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2126 | flags |= FREE_FNAME; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2127 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2129 | match->cp_fname = NULL; |
| 2130 | match->cp_flags = flags; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2131 | if (extra != NULL) |
| 2132 | match->cp_extra = vim_strsave(extra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2133 | |
| 2134 | /* |
| 2135 | * Link the new match structure in the list of matches. |
| 2136 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2137 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2138 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2139 | else if (dir == FORWARD) |
| 2140 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2141 | match->cp_next = compl_curr_match->cp_next; |
| 2142 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2143 | } |
| 2144 | else /* BACKWARD */ |
| 2145 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2146 | match->cp_next = compl_curr_match; |
| 2147 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2149 | if (match->cp_next) |
| 2150 | match->cp_next->cp_prev = match; |
| 2151 | if (match->cp_prev) |
| 2152 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2153 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2154 | compl_first_match = match; |
| 2155 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2156 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2157 | /* |
| 2158 | * Find the longest common string if still doing that. |
| 2159 | */ |
| 2160 | if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) |
| 2161 | ins_compl_longest_match(match); |
| 2162 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2163 | return OK; |
| 2164 | } |
| 2165 | |
| 2166 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2167 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
| 2168 | * match->cp_icase. |
| 2169 | */ |
| 2170 | static int |
| 2171 | ins_compl_equal(match, str, len) |
| 2172 | compl_T *match; |
| 2173 | char_u *str; |
| 2174 | int len; |
| 2175 | { |
| 2176 | if (match->cp_icase) |
| 2177 | return STRNICMP(match->cp_str, str, (size_t)len) == 0; |
| 2178 | return STRNCMP(match->cp_str, str, (size_t)len) == 0; |
| 2179 | } |
| 2180 | |
| 2181 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2182 | * Reduce the longest common string for match "match". |
| 2183 | */ |
| 2184 | static void |
| 2185 | ins_compl_longest_match(match) |
| 2186 | compl_T *match; |
| 2187 | { |
| 2188 | char_u *p, *s; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2189 | int c1, c2; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2190 | int had_match; |
| 2191 | |
| 2192 | if (compl_leader == NULL) |
| 2193 | /* First match, use it as a whole. */ |
| 2194 | compl_leader = vim_strsave(match->cp_str); |
| 2195 | else |
| 2196 | { |
| 2197 | /* Reduce the text if this match differs from compl_leader. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2198 | p = compl_leader; |
| 2199 | s = match->cp_str; |
| 2200 | while (*p != NUL) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2201 | { |
| 2202 | #ifdef FEAT_MBYTE |
| 2203 | if (has_mbyte) |
| 2204 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2205 | c1 = mb_ptr2char(p); |
| 2206 | c2 = mb_ptr2char(s); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2207 | } |
| 2208 | else |
| 2209 | #endif |
| 2210 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2211 | c1 = *p; |
| 2212 | c2 = *s; |
| 2213 | } |
| 2214 | if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) |
| 2215 | : (c1 != c2)) |
| 2216 | break; |
| 2217 | #ifdef FEAT_MBYTE |
| 2218 | if (has_mbyte) |
| 2219 | { |
| 2220 | mb_ptr_adv(p); |
| 2221 | mb_ptr_adv(s); |
| 2222 | } |
| 2223 | else |
| 2224 | #endif |
| 2225 | { |
| 2226 | ++p; |
| 2227 | ++s; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | if (*p != NUL) |
| 2232 | { |
| 2233 | /* Leader was shortened, need to change the inserted text. */ |
| 2234 | *p = NUL; |
| 2235 | had_match = (curwin->w_cursor.col > compl_col); |
| 2236 | ins_compl_delete(); |
| 2237 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2238 | ins_redraw(FALSE); |
| 2239 | |
| 2240 | /* When the match isn't there (to avoid matching itself) remove it |
| 2241 | * again after redrawing. */ |
| 2242 | if (!had_match) |
| 2243 | ins_compl_delete(); |
| 2244 | } |
| 2245 | |
| 2246 | compl_used_match = FALSE; |
| 2247 | } |
| 2248 | } |
| 2249 | |
| 2250 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2251 | * Add an array of matches to the list of matches. |
| 2252 | * Frees matches[]. |
| 2253 | */ |
| 2254 | static void |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2255 | ins_compl_add_matches(num_matches, matches, icase) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | int num_matches; |
| 2257 | char_u **matches; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2258 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2259 | { |
| 2260 | int i; |
| 2261 | int add_r = OK; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2262 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2264 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2265 | if ((add_r = ins_compl_add(matches[i], -1, icase, |
| 2266 | NULL, NULL, dir, 0)) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2267 | /* if dir was BACKWARD then honor it just once */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2268 | dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2269 | FreeWild(num_matches, matches); |
| 2270 | } |
| 2271 | |
| 2272 | /* Make the completion list cyclic. |
| 2273 | * Return the number of matches (excluding the original). |
| 2274 | */ |
| 2275 | static int |
| 2276 | ins_compl_make_cyclic() |
| 2277 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2278 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | int count = 0; |
| 2280 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2281 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | { |
| 2283 | /* |
| 2284 | * Find the end of the list. |
| 2285 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2286 | match = compl_first_match; |
| 2287 | /* 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] | 2288 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2290 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2291 | ++count; |
| 2292 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2293 | match->cp_next = compl_first_match; |
| 2294 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2295 | } |
| 2296 | return count; |
| 2297 | } |
| 2298 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2299 | /* "compl_match_array" points the currently displayed list of entries in the |
| 2300 | * popup menu. It is NULL when there is no popup menu. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2301 | static pumitem_T *compl_match_array = NULL; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2302 | static int compl_match_arraysize; |
| 2303 | |
| 2304 | /* |
| 2305 | * Update the screen and when there is any scrolling remove the popup menu. |
| 2306 | */ |
| 2307 | static void |
| 2308 | ins_compl_upd_pum() |
| 2309 | { |
| 2310 | int h; |
| 2311 | |
| 2312 | if (compl_match_array != NULL) |
| 2313 | { |
| 2314 | h = curwin->w_cline_height; |
| 2315 | update_screen(0); |
| 2316 | if (h != curwin->w_cline_height) |
| 2317 | ins_compl_del_pum(); |
| 2318 | } |
| 2319 | } |
| 2320 | |
| 2321 | /* |
| 2322 | * Remove any popup menu. |
| 2323 | */ |
| 2324 | static void |
| 2325 | ins_compl_del_pum() |
| 2326 | { |
| 2327 | if (compl_match_array != NULL) |
| 2328 | { |
| 2329 | pum_undisplay(); |
| 2330 | vim_free(compl_match_array); |
| 2331 | compl_match_array = NULL; |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Return TRUE if the popup menu should be displayed. |
| 2337 | */ |
| 2338 | static int |
| 2339 | pum_wanted() |
| 2340 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2341 | /* 'completeopt' must contain "menu" */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2342 | if (vim_strchr(p_cot, 'm') == NULL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2343 | return FALSE; |
| 2344 | |
| 2345 | /* The display looks bad on a B&W display. */ |
| 2346 | if (t_colors < 8 |
| 2347 | #ifdef FEAT_GUI |
| 2348 | && !gui.in_use |
| 2349 | #endif |
| 2350 | ) |
| 2351 | return FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2352 | return TRUE; |
| 2353 | } |
| 2354 | |
| 2355 | /* |
| 2356 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
| 2357 | */ |
| 2358 | static int |
| 2359 | pum_two_or_more() |
| 2360 | { |
| 2361 | compl_T *compl; |
| 2362 | int i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2363 | |
| 2364 | /* Don't display the popup menu if there are no matches or there is only |
| 2365 | * one (ignoring the original text). */ |
| 2366 | compl = compl_first_match; |
| 2367 | i = 0; |
| 2368 | do |
| 2369 | { |
| 2370 | if (compl == NULL |
| 2371 | || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) |
| 2372 | break; |
| 2373 | compl = compl->cp_next; |
| 2374 | } while (compl != compl_first_match); |
| 2375 | |
| 2376 | return (i >= 2); |
| 2377 | } |
| 2378 | |
| 2379 | /* |
| 2380 | * Show the popup menu for the list of matches. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2381 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2382 | */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2383 | void |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2384 | ins_compl_show_pum() |
| 2385 | { |
| 2386 | compl_T *compl; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2387 | compl_T *shown_compl = NULL; |
| 2388 | int did_find_shown_match = FALSE; |
| 2389 | int shown_match_ok = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2390 | int i; |
| 2391 | int cur = -1; |
| 2392 | colnr_T col; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2393 | int lead_len = 0; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2394 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2395 | if (!pum_wanted() || !pum_two_or_more()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2396 | return; |
| 2397 | |
| 2398 | /* Update the screen before drawing the popup menu over it. */ |
| 2399 | update_screen(0); |
| 2400 | |
| 2401 | if (compl_match_array == NULL) |
| 2402 | { |
| 2403 | /* Need to build the popup menu list. */ |
| 2404 | compl_match_arraysize = 0; |
| 2405 | compl = compl_first_match; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2406 | if (compl_leader != NULL) |
| 2407 | lead_len = STRLEN(compl_leader); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2408 | do |
| 2409 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2410 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2411 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2412 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2413 | ++compl_match_arraysize; |
| 2414 | compl = compl->cp_next; |
| 2415 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2416 | if (compl_match_arraysize == 0) |
| 2417 | return; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2418 | compl_match_array = (pumitem_T *)alloc_clear( |
| 2419 | (unsigned)(sizeof(pumitem_T) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2420 | * compl_match_arraysize)); |
| 2421 | if (compl_match_array != NULL) |
| 2422 | { |
| 2423 | i = 0; |
| 2424 | compl = compl_first_match; |
| 2425 | do |
| 2426 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2427 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2428 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2429 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2430 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2431 | if (!shown_match_ok) |
| 2432 | { |
| 2433 | if (compl == compl_shown_match || did_find_shown_match) |
| 2434 | { |
| 2435 | /* This item is the shown match or this is the |
| 2436 | * first displayed item after the shown match. */ |
| 2437 | compl_shown_match = compl; |
| 2438 | did_find_shown_match = TRUE; |
| 2439 | shown_match_ok = TRUE; |
| 2440 | } |
| 2441 | else |
| 2442 | /* Remember this displayed match for when the |
| 2443 | * shown match is just below it. */ |
| 2444 | shown_compl = compl; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2445 | cur = i; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2446 | } |
| 2447 | compl_match_array[i].pum_text = compl->cp_str; |
| 2448 | if (compl->cp_extra != NULL) |
| 2449 | compl_match_array[i++].pum_extra = compl->cp_extra; |
| 2450 | else |
| 2451 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 2452 | } |
| 2453 | |
| 2454 | if (compl == compl_shown_match) |
| 2455 | { |
| 2456 | did_find_shown_match = TRUE; |
| 2457 | if (!shown_match_ok && shown_compl != NULL) |
| 2458 | { |
| 2459 | /* The shown match isn't displayed, set it to the |
| 2460 | * previously displayed match. */ |
| 2461 | compl_shown_match = shown_compl; |
| 2462 | shown_match_ok = TRUE; |
| 2463 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2464 | } |
| 2465 | compl = compl->cp_next; |
| 2466 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2467 | |
| 2468 | if (!shown_match_ok) /* no displayed match at all */ |
| 2469 | cur = -1; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2470 | } |
| 2471 | } |
| 2472 | else |
| 2473 | { |
| 2474 | /* popup menu already exists, only need to find the current item.*/ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2475 | for (i = 0; i < compl_match_arraysize; ++i) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2476 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2477 | break; |
| 2478 | cur = i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
| 2481 | if (compl_match_array != NULL) |
| 2482 | { |
| 2483 | /* Compute the screen column of the start of the completed text. |
| 2484 | * Use the cursor to get all wrapping and other settings right. */ |
| 2485 | col = curwin->w_cursor.col; |
| 2486 | curwin->w_cursor.col = compl_col; |
| 2487 | validate_cursor_col(); |
| 2488 | pum_display(compl_match_array, compl_match_arraysize, cur, |
| 2489 | curwin->w_cline_row + W_WINROW(curwin), |
| 2490 | curwin->w_cline_height, |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2491 | curwin->w_wcol + W_WINCOL(curwin) - curwin->w_leftcol); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2492 | curwin->w_cursor.col = col; |
| 2493 | } |
| 2494 | } |
| 2495 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2496 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 2497 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2498 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | /* |
| 2500 | * Add any identifiers that match the given pattern to the list of |
| 2501 | * completions. |
| 2502 | */ |
| 2503 | static void |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2504 | ins_compl_dictionaries(dict, pat, flags, thesaurus) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2505 | char_u *dict; |
| 2506 | char_u *pat; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2507 | int flags; /* DICT_FIRST and/or DICT_EXACT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2508 | int thesaurus; |
| 2509 | { |
| 2510 | char_u *ptr; |
| 2511 | char_u *buf; |
| 2512 | FILE *fp; |
| 2513 | regmatch_T regmatch; |
| 2514 | int add_r; |
| 2515 | char_u **files; |
| 2516 | int count; |
| 2517 | int i; |
| 2518 | int save_p_scs; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2519 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | |
| 2521 | buf = alloc(LSIZE); |
| 2522 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 2523 | save_p_scs = p_scs; |
| 2524 | if (curbuf->b_p_inf) |
| 2525 | p_scs = FALSE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2526 | |
| 2527 | /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 2528 | * to only match at the start of a line. Otherwise just match the |
| 2529 | * pattern. */ |
| 2530 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2531 | { |
| 2532 | i = STRLEN(pat) + 8; |
| 2533 | ptr = alloc(i); |
| 2534 | if (ptr == NULL) |
| 2535 | return; |
| 2536 | vim_snprintf((char *)ptr, i, "^\\s*\\zs%s", pat); |
| 2537 | regmatch.regprog = vim_regcomp(ptr, p_magic ? RE_MAGIC : 0); |
| 2538 | vim_free(ptr); |
| 2539 | } |
| 2540 | else |
| 2541 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 2542 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 2544 | regmatch.rm_ic = ignorecase(pat); |
| 2545 | while (buf != NULL && regmatch.regprog != NULL && *dict != NUL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2546 | && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2547 | { |
| 2548 | /* copy one dictionary file name into buf */ |
| 2549 | if (flags == DICT_EXACT) |
| 2550 | { |
| 2551 | count = 1; |
| 2552 | files = &dict; |
| 2553 | } |
| 2554 | else |
| 2555 | { |
| 2556 | /* Expand wildcards in the dictionary name, but do not allow |
| 2557 | * backticks (for security, the 'dict' option may have been set in |
| 2558 | * a modeline). */ |
| 2559 | copy_option_part(&dict, buf, LSIZE, ","); |
| 2560 | if (vim_strchr(buf, '`') != NULL |
| 2561 | || expand_wildcards(1, &buf, &count, &files, |
| 2562 | EW_FILE|EW_SILENT) != OK) |
| 2563 | count = 0; |
| 2564 | } |
| 2565 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2566 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2567 | { |
| 2568 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 2569 | if (flags != DICT_EXACT) |
| 2570 | { |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 2571 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2572 | _("Scanning dictionary: %s"), (char *)files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2573 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 2574 | } |
| 2575 | |
| 2576 | if (fp != NULL) |
| 2577 | { |
| 2578 | /* |
| 2579 | * Read dictionary file line by line. |
| 2580 | * Check each line for a match. |
| 2581 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2582 | while (!got_int && !compl_interrupted |
| 2583 | && !vim_fgets(buf, LSIZE, fp)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2584 | { |
| 2585 | ptr = buf; |
| 2586 | while (vim_regexec(®match, buf, (colnr_T)(ptr - buf))) |
| 2587 | { |
| 2588 | ptr = regmatch.startp[0]; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2589 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2590 | ptr = find_line_end(ptr); |
| 2591 | else |
| 2592 | ptr = find_word_end(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2593 | add_r = ins_compl_add_infercase(regmatch.startp[0], |
| 2594 | (int)(ptr - regmatch.startp[0]), |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2595 | p_ic, files[i], dir, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2596 | if (thesaurus) |
| 2597 | { |
| 2598 | char_u *wstart; |
| 2599 | |
| 2600 | /* |
| 2601 | * Add the other matches on the line |
| 2602 | */ |
| 2603 | while (!got_int) |
| 2604 | { |
| 2605 | /* Find start of the next word. Skip white |
| 2606 | * space and punctuation. */ |
| 2607 | ptr = find_word_start(ptr); |
| 2608 | if (*ptr == NUL || *ptr == NL) |
| 2609 | break; |
| 2610 | wstart = ptr; |
| 2611 | |
| 2612 | /* Find end of the word and add it. */ |
| 2613 | #ifdef FEAT_MBYTE |
| 2614 | if (has_mbyte) |
| 2615 | /* Japanese words may have characters in |
| 2616 | * different classes, only separate words |
| 2617 | * with single-byte non-word characters. */ |
| 2618 | while (*ptr != NUL) |
| 2619 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2620 | int l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2621 | |
| 2622 | if (l < 2 && !vim_iswordc(*ptr)) |
| 2623 | break; |
| 2624 | ptr += l; |
| 2625 | } |
| 2626 | else |
| 2627 | #endif |
| 2628 | ptr = find_word_end(ptr); |
| 2629 | add_r = ins_compl_add_infercase(wstart, |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2630 | (int)(ptr - wstart), |
| 2631 | p_ic, files[i], dir, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2632 | } |
| 2633 | } |
| 2634 | if (add_r == OK) |
| 2635 | /* if dir was BACKWARD then honor it just once */ |
| 2636 | dir = FORWARD; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2637 | else if (add_r == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2638 | break; |
| 2639 | /* avoid expensive call to vim_regexec() when at end |
| 2640 | * of line */ |
| 2641 | if (*ptr == '\n' || got_int) |
| 2642 | break; |
| 2643 | } |
| 2644 | line_breakcheck(); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2645 | ins_compl_check_keys(50); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2646 | } |
| 2647 | fclose(fp); |
| 2648 | } |
| 2649 | } |
| 2650 | if (flags != DICT_EXACT) |
| 2651 | FreeWild(count, files); |
| 2652 | if (flags) |
| 2653 | break; |
| 2654 | } |
| 2655 | p_scs = save_p_scs; |
| 2656 | vim_free(regmatch.regprog); |
| 2657 | vim_free(buf); |
| 2658 | } |
| 2659 | |
| 2660 | /* |
| 2661 | * Find the start of the next word. |
| 2662 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 2663 | */ |
| 2664 | char_u * |
| 2665 | find_word_start(ptr) |
| 2666 | char_u *ptr; |
| 2667 | { |
| 2668 | #ifdef FEAT_MBYTE |
| 2669 | if (has_mbyte) |
| 2670 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2671 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | else |
| 2673 | #endif |
| 2674 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 2675 | ++ptr; |
| 2676 | return ptr; |
| 2677 | } |
| 2678 | |
| 2679 | /* |
| 2680 | * Find the end of the word. Assumes it starts inside a word. |
| 2681 | * Returns a pointer to just after the word. |
| 2682 | */ |
| 2683 | char_u * |
| 2684 | find_word_end(ptr) |
| 2685 | char_u *ptr; |
| 2686 | { |
| 2687 | #ifdef FEAT_MBYTE |
| 2688 | int start_class; |
| 2689 | |
| 2690 | if (has_mbyte) |
| 2691 | { |
| 2692 | start_class = mb_get_class(ptr); |
| 2693 | if (start_class > 1) |
| 2694 | while (*ptr != NUL) |
| 2695 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2696 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | if (mb_get_class(ptr) != start_class) |
| 2698 | break; |
| 2699 | } |
| 2700 | } |
| 2701 | else |
| 2702 | #endif |
| 2703 | while (vim_iswordc(*ptr)) |
| 2704 | ++ptr; |
| 2705 | return ptr; |
| 2706 | } |
| 2707 | |
| 2708 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2709 | * Find the end of the line, omitting CR and NL at the end. |
| 2710 | * Returns a pointer to just after the line. |
| 2711 | */ |
| 2712 | static char_u * |
| 2713 | find_line_end(ptr) |
| 2714 | char_u *ptr; |
| 2715 | { |
| 2716 | char_u *s; |
| 2717 | |
| 2718 | s = ptr + STRLEN(ptr); |
| 2719 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 2720 | --s; |
| 2721 | return s; |
| 2722 | } |
| 2723 | |
| 2724 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2725 | * Free the list of completions |
| 2726 | */ |
| 2727 | static void |
| 2728 | ins_compl_free() |
| 2729 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2730 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2731 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2732 | vim_free(compl_pattern); |
| 2733 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2734 | vim_free(compl_leader); |
| 2735 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2736 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2737 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2738 | return; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2739 | |
| 2740 | ins_compl_del_pum(); |
| 2741 | pum_clear(); |
| 2742 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2743 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2744 | do |
| 2745 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2746 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2747 | compl_curr_match = compl_curr_match->cp_next; |
| 2748 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2749 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2750 | if (match->cp_flags & FREE_FNAME) |
| 2751 | vim_free(match->cp_fname); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2752 | vim_free(match->cp_extra); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2753 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2754 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 2755 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | static void |
| 2759 | ins_compl_clear() |
| 2760 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2761 | compl_cont_status = 0; |
| 2762 | compl_started = FALSE; |
| 2763 | compl_matches = 0; |
| 2764 | vim_free(compl_pattern); |
| 2765 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2766 | vim_free(compl_leader); |
| 2767 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2768 | edit_submode_extra = NULL; |
| 2769 | } |
| 2770 | |
| 2771 | /* |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2772 | * Return TRUE when Insert completion is active. |
| 2773 | */ |
| 2774 | int |
| 2775 | ins_compl_active() |
| 2776 | { |
| 2777 | return compl_started; |
| 2778 | } |
| 2779 | |
| 2780 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2781 | * Delete one character before the cursor and show the subset of the matches |
| 2782 | * that match the word that is now before the cursor. |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2783 | * Returns TRUE if the work is done and another char to be got from the user. |
| 2784 | */ |
| 2785 | static int |
| 2786 | ins_compl_bs() |
| 2787 | { |
| 2788 | char_u *line; |
| 2789 | char_u *p; |
| 2790 | |
| 2791 | if (curwin->w_cursor.col <= compl_col + compl_length) |
| 2792 | { |
| 2793 | /* Deleted more than what was used to find matches, need to look for |
| 2794 | * matches all over again. */ |
| 2795 | ins_compl_free(); |
| 2796 | compl_started = FALSE; |
| 2797 | compl_matches = 0; |
| 2798 | } |
| 2799 | |
| 2800 | line = ml_get_curline(); |
| 2801 | p = line + curwin->w_cursor.col; |
| 2802 | mb_ptr_back(line, p); |
| 2803 | |
| 2804 | vim_free(compl_leader); |
| 2805 | compl_leader = vim_strnsave(line + compl_col, (p - line) - compl_col); |
| 2806 | if (compl_leader != NULL) |
| 2807 | { |
| 2808 | ins_compl_del_pum(); |
| 2809 | ins_compl_delete(); |
| 2810 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2811 | |
| 2812 | if (!compl_started) |
| 2813 | { |
| 2814 | /* Matches were cleared, need to search for them now. */ |
| 2815 | if (ins_complete(Ctrl_N) == FAIL) |
| 2816 | compl_cont_status = 0; |
| 2817 | else |
| 2818 | { |
| 2819 | /* Remove the completed word again. */ |
| 2820 | ins_compl_delete(); |
| 2821 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | /* Show the popup menu with a different set of matches. */ |
| 2826 | ins_compl_show_pum(); |
| 2827 | compl_used_match = FALSE; |
| 2828 | |
| 2829 | return TRUE; |
| 2830 | } |
| 2831 | return FALSE; |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * Append one character to the match leader. May reduce the number of |
| 2836 | * matches. |
| 2837 | */ |
| 2838 | static void |
| 2839 | ins_compl_addleader(c) |
| 2840 | int c; |
| 2841 | { |
| 2842 | #ifdef FEAT_MBYTE |
| 2843 | int cc; |
| 2844 | |
| 2845 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 2846 | { |
| 2847 | char_u buf[MB_MAXBYTES + 1]; |
| 2848 | |
| 2849 | (*mb_char2bytes)(c, buf); |
| 2850 | buf[cc] = NUL; |
| 2851 | ins_char_bytes(buf, cc); |
| 2852 | } |
| 2853 | else |
| 2854 | #endif |
| 2855 | ins_char(c); |
| 2856 | |
| 2857 | vim_free(compl_leader); |
| 2858 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
| 2859 | curwin->w_cursor.col - compl_col); |
| 2860 | if (compl_leader != NULL) |
| 2861 | { |
| 2862 | /* Show the popup menu with a different set of matches. */ |
| 2863 | ins_compl_del_pum(); |
| 2864 | ins_compl_show_pum(); |
| 2865 | compl_used_match = FALSE; |
| 2866 | } |
| 2867 | } |
| 2868 | |
| 2869 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2870 | * Append one character to the match leader. May reduce the number of |
| 2871 | * matches. |
| 2872 | */ |
| 2873 | static void |
| 2874 | ins_compl_addfrommatch() |
| 2875 | { |
| 2876 | char_u *p; |
| 2877 | int len = curwin->w_cursor.col - compl_col; |
| 2878 | int c; |
| 2879 | |
| 2880 | p = compl_shown_match->cp_str; |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 2881 | if ((int)STRLEN(p) <= len) /* the match is too short */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2882 | return; |
| 2883 | p += len; |
| 2884 | #ifdef FEAT_MBYTE |
| 2885 | c = mb_ptr2char(p); |
| 2886 | #else |
| 2887 | c = *p; |
| 2888 | #endif |
| 2889 | ins_compl_addleader(c); |
| 2890 | } |
| 2891 | |
| 2892 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2893 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2894 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2895 | * Returns TRUE when the character is not to be inserted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2896 | */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2897 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2898 | ins_compl_prep(c) |
| 2899 | int c; |
| 2900 | { |
| 2901 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | int temp; |
| 2903 | int want_cindent; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2904 | int retval = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2905 | |
| 2906 | /* Forget any previous 'special' messages if this is actually |
| 2907 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 2908 | */ |
| 2909 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 2910 | edit_submode_extra = NULL; |
| 2911 | |
| 2912 | /* Ignore end of Select mode mapping */ |
| 2913 | if (c == K_SELECT) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2914 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2915 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2916 | /* Set "compl_get_longest" when finding the first matches. */ |
| 2917 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 2918 | || (ctrl_x_mode == 0 && !compl_started)) |
| 2919 | { |
| 2920 | compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); |
| 2921 | compl_used_match = TRUE; |
| 2922 | } |
| 2923 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 2925 | { |
| 2926 | /* |
| 2927 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 2928 | * it will be yet. Now we decide. |
| 2929 | */ |
| 2930 | switch (c) |
| 2931 | { |
| 2932 | case Ctrl_E: |
| 2933 | case Ctrl_Y: |
| 2934 | ctrl_x_mode = CTRL_X_SCROLL; |
| 2935 | if (!(State & REPLACE_FLAG)) |
| 2936 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 2937 | else |
| 2938 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 2939 | edit_submode_pre = NULL; |
| 2940 | showmode(); |
| 2941 | break; |
| 2942 | case Ctrl_L: |
| 2943 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2944 | break; |
| 2945 | case Ctrl_F: |
| 2946 | ctrl_x_mode = CTRL_X_FILES; |
| 2947 | break; |
| 2948 | case Ctrl_K: |
| 2949 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 2950 | break; |
| 2951 | case Ctrl_R: |
| 2952 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 2953 | break; |
| 2954 | case Ctrl_T: |
| 2955 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 2956 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2957 | #ifdef FEAT_COMPL_FUNC |
| 2958 | case Ctrl_U: |
| 2959 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 2960 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2961 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2962 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2963 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2964 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2965 | case 's': |
| 2966 | case Ctrl_S: |
| 2967 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 2968 | #ifdef FEAT_SYN_HL |
| 2969 | spell_back_to_badword(); |
| 2970 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2971 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2972 | case Ctrl_RSB: |
| 2973 | ctrl_x_mode = CTRL_X_TAGS; |
| 2974 | break; |
| 2975 | #ifdef FEAT_FIND_ID |
| 2976 | case Ctrl_I: |
| 2977 | case K_S_TAB: |
| 2978 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 2979 | break; |
| 2980 | case Ctrl_D: |
| 2981 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 2982 | break; |
| 2983 | #endif |
| 2984 | case Ctrl_V: |
| 2985 | case Ctrl_Q: |
| 2986 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 2987 | break; |
| 2988 | case Ctrl_P: |
| 2989 | case Ctrl_N: |
| 2990 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 2991 | * just started ^X mode, or there were enough ^X's to cancel |
| 2992 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 2993 | * do normal expansion when interrupting a different mode (say |
| 2994 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 2995 | * nothing changes if interrupting mode 0, (eg, the flag |
| 2996 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2997 | if (!(compl_cont_status & CONT_INTRPT)) |
| 2998 | compl_cont_status |= CONT_LOCAL; |
| 2999 | else if (compl_cont_mode != 0) |
| 3000 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3001 | /* FALLTHROUGH */ |
| 3002 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3003 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 3004 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 3005 | * mode). |
| 3006 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 3007 | * value, in both cases ^X^X can be used to restart the same |
| 3008 | * mode (avoiding ADDING mode). |
| 3009 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 3010 | * 'complete' and local ^P expansions respectively. |
| 3011 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 3012 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3013 | if (c == Ctrl_X) |
| 3014 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3015 | if (compl_cont_mode != 0) |
| 3016 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3017 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3018 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3019 | } |
| 3020 | ctrl_x_mode = 0; |
| 3021 | edit_submode = NULL; |
| 3022 | showmode(); |
| 3023 | break; |
| 3024 | } |
| 3025 | } |
| 3026 | else if (ctrl_x_mode != 0) |
| 3027 | { |
| 3028 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 3029 | if (!vim_is_ctrl_x_key(c)) |
| 3030 | { |
| 3031 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 3032 | ctrl_x_mode = 0; |
| 3033 | else |
| 3034 | ctrl_x_mode = CTRL_X_FINISHED; |
| 3035 | edit_submode = NULL; |
| 3036 | } |
| 3037 | showmode(); |
| 3038 | } |
| 3039 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3040 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3041 | { |
| 3042 | /* Show error message from attempted keyword completion (probably |
| 3043 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3044 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3045 | showmode(); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3046 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
| 3047 | && !ins_compl_pum_key(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 3049 | { |
| 3050 | /* Get here when we have finished typing a sequence of ^N and |
| 3051 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3052 | * memory that was used, and make sure we can redo the insert. */ |
| 3053 | if (compl_curr_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3054 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3055 | char_u *p; |
| 3056 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3057 | /* |
| 3058 | * If any of the original typed text has been changed, |
| 3059 | * eg when ignorecase is set, we must add back-spaces to |
| 3060 | * the redo buffer. We add as few as necessary to delete |
| 3061 | * just the part of the original text that has changed. |
| 3062 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3063 | ptr = compl_curr_match->cp_str; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3064 | p = compl_orig_text; |
| 3065 | while (*p && *p == *ptr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3066 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3067 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | ++ptr; |
| 3069 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3070 | for (temp = 0; p[temp]; ++temp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3071 | AppendCharToRedobuff(K_BS); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 3072 | AppendToRedobuffLit(ptr, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | #ifdef FEAT_CINDENT |
| 3076 | want_cindent = (can_cindent && cindent_on()); |
| 3077 | #endif |
| 3078 | /* |
| 3079 | * When completing whole lines: fix indent for 'cindent'. |
| 3080 | * Otherwise, break line if it's too long. |
| 3081 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3082 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3083 | { |
| 3084 | #ifdef FEAT_CINDENT |
| 3085 | /* re-indent the current line */ |
| 3086 | if (want_cindent) |
| 3087 | { |
| 3088 | do_c_expr_indent(); |
| 3089 | want_cindent = FALSE; /* don't do it again */ |
| 3090 | } |
| 3091 | #endif |
| 3092 | } |
| 3093 | else |
| 3094 | { |
| 3095 | /* put the cursor on the last char, for 'tw' formatting */ |
| 3096 | curwin->w_cursor.col--; |
| 3097 | if (stop_arrow() == OK) |
| 3098 | insertchar(NUL, 0, -1); |
| 3099 | curwin->w_cursor.col++; |
| 3100 | } |
| 3101 | |
| 3102 | auto_format(FALSE, TRUE); |
| 3103 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3104 | /* if the popup menu is displayed hitting Enter means accepting |
| 3105 | * the selection without inserting anything. */ |
| 3106 | if ((c == CAR || c == K_KENTER || c == NL) && pum_visible()) |
| 3107 | retval = TRUE; |
| 3108 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3109 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3110 | compl_started = FALSE; |
| 3111 | compl_matches = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3112 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 3113 | ctrl_x_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3114 | if (edit_submode != NULL) |
| 3115 | { |
| 3116 | edit_submode = NULL; |
| 3117 | showmode(); |
| 3118 | } |
| 3119 | |
| 3120 | #ifdef FEAT_CINDENT |
| 3121 | /* |
| 3122 | * Indent now if a key was typed that is in 'cinkeys'. |
| 3123 | */ |
| 3124 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 3125 | do_c_expr_indent(); |
| 3126 | #endif |
| 3127 | } |
| 3128 | } |
| 3129 | |
| 3130 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 3131 | * (re)set properly in ins_complete() */ |
| 3132 | if (!vim_is_ctrl_x_key(c)) |
| 3133 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3134 | compl_cont_status = 0; |
| 3135 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3136 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3137 | |
| 3138 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | /* |
| 3142 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3143 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3144 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3145 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3146 | * |
| 3147 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3148 | */ |
| 3149 | static buf_T * |
| 3150 | ins_compl_next_buf(buf, flag) |
| 3151 | buf_T *buf; |
| 3152 | int flag; |
| 3153 | { |
| 3154 | #ifdef FEAT_WINDOWS |
| 3155 | static win_T *wp; |
| 3156 | #endif |
| 3157 | |
| 3158 | if (flag == 'w') /* just windows */ |
| 3159 | { |
| 3160 | #ifdef FEAT_WINDOWS |
| 3161 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 3162 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3163 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3164 | && wp->w_buffer->b_scanned) |
| 3165 | ; |
| 3166 | buf = wp->w_buffer; |
| 3167 | #else |
| 3168 | buf = curbuf; |
| 3169 | #endif |
| 3170 | } |
| 3171 | else |
| 3172 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3173 | * (unlisted buffers) |
| 3174 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3175 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3176 | && ((flag == 'U' |
| 3177 | ? buf->b_p_bl |
| 3178 | : (!buf->b_p_bl |
| 3179 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3180 | || buf->b_scanned)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3181 | ; |
| 3182 | return buf; |
| 3183 | } |
| 3184 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3185 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3186 | static void expand_by_function __ARGS((int type, char_u *base)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3187 | |
| 3188 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3189 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3190 | * get matches in "matches". |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3191 | * Return value is number of matches. |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3192 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3193 | static void |
| 3194 | expand_by_function(type, base) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3195 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3196 | char_u *base; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3197 | { |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3198 | list_T *matchlist; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3199 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3200 | listitem_T *li; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3201 | char_u *p; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3202 | char_u *funcname; |
| 3203 | pos_T pos; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3204 | int dir = compl_direction; |
| 3205 | char_u *x; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3206 | int icase; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3207 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3208 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3209 | if (*funcname == NUL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3210 | return; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3211 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3212 | /* Call 'completefunc' to obtain the list of matches. */ |
| 3213 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3214 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3215 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3216 | pos = curwin->w_cursor; |
| 3217 | matchlist = call_func_retlist(funcname, 2, args, FALSE); |
| 3218 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3219 | if (matchlist == NULL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3220 | return; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3221 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3222 | /* Go through the List with matches and add each of them. */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3223 | for (li = matchlist->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3224 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3225 | if (li->li_tv.v_type == VAR_DICT && li->li_tv.vval.v_dict != NULL) |
| 3226 | { |
| 3227 | p = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"word", FALSE); |
| 3228 | x = get_dict_string(li->li_tv.vval.v_dict, (char_u *)"menu", FALSE); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3229 | if (get_dict_string(li->li_tv.vval.v_dict, (char_u *)"icase", |
| 3230 | FALSE) == NULL) |
| 3231 | icase = p_ic; |
| 3232 | else |
| 3233 | icase = get_dict_number(li->li_tv.vval.v_dict, |
| 3234 | (char_u *)"icase"); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3235 | } |
| 3236 | else |
| 3237 | { |
| 3238 | p = get_tv_string_chk(&li->li_tv); |
| 3239 | x = NULL; |
| 3240 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3241 | if (p != NULL && *p != NUL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3242 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3243 | if (ins_compl_add(p, -1, icase, NULL, x, dir, 0) == OK) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3244 | /* if dir was BACKWARD then honor it just once */ |
| 3245 | dir = FORWARD; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3246 | } |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 3247 | else if (did_emsg) |
| 3248 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3249 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3250 | |
| 3251 | list_unref(matchlist); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3252 | } |
| 3253 | #endif /* FEAT_COMPL_FUNC */ |
| 3254 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3255 | /* |
| 3256 | * Get the next expansion(s), using "compl_pattern". |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3257 | * The search starts at position "ini" in curbuf and in the direction |
| 3258 | * compl_direction. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3259 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 3260 | * where we stopped searching before. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3261 | * This may return before finding all the matches. |
| 3262 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | */ |
| 3264 | static int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3265 | ins_compl_get_exp(ini) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3266 | pos_T *ini; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3267 | { |
| 3268 | static pos_T first_match_pos; |
| 3269 | static pos_T last_match_pos; |
| 3270 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3271 | static int found_all = FALSE; /* Found all matches of a |
| 3272 | certain type. */ |
| 3273 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3274 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3275 | pos_T *pos; |
| 3276 | char_u **matches; |
| 3277 | int save_p_scs; |
| 3278 | int save_p_ws; |
| 3279 | int save_p_ic; |
| 3280 | int i; |
| 3281 | int num_matches; |
| 3282 | int len; |
| 3283 | int found_new_match; |
| 3284 | int type = ctrl_x_mode; |
| 3285 | char_u *ptr; |
| 3286 | char_u *dict = NULL; |
| 3287 | int dict_f = 0; |
| 3288 | compl_T *old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3289 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3290 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3291 | { |
| 3292 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 3293 | ins_buf->b_scanned = 0; |
| 3294 | found_all = FALSE; |
| 3295 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3296 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3297 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3298 | last_match_pos = first_match_pos = *ini; |
| 3299 | } |
| 3300 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3301 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3302 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3303 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 3304 | for (;;) |
| 3305 | { |
| 3306 | found_new_match = FAIL; |
| 3307 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3308 | /* 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] | 3309 | * or if found_all says this entry is done. For ^X^L only use the |
| 3310 | * entries from 'complete' that look in loaded buffers. */ |
| 3311 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3312 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3313 | { |
| 3314 | found_all = FALSE; |
| 3315 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 3316 | e_cpt++; |
| 3317 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 3318 | { |
| 3319 | ins_buf = curbuf; |
| 3320 | first_match_pos = *ini; |
| 3321 | /* So that ^N can match word immediately after cursor */ |
| 3322 | if (ctrl_x_mode == 0) |
| 3323 | dec(&first_match_pos); |
| 3324 | last_match_pos = first_match_pos; |
| 3325 | type = 0; |
| 3326 | } |
| 3327 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 3328 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 3329 | { |
| 3330 | /* Scan a buffer, but not the current one. */ |
| 3331 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 3332 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3333 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | first_match_pos.col = last_match_pos.col = 0; |
| 3335 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 3336 | last_match_pos.lnum = 0; |
| 3337 | type = 0; |
| 3338 | } |
| 3339 | else /* unloaded buffer, scan like dictionary */ |
| 3340 | { |
| 3341 | found_all = TRUE; |
| 3342 | if (ins_buf->b_fname == NULL) |
| 3343 | continue; |
| 3344 | type = CTRL_X_DICTIONARY; |
| 3345 | dict = ins_buf->b_fname; |
| 3346 | dict_f = DICT_EXACT; |
| 3347 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3348 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3349 | ins_buf->b_fname == NULL |
| 3350 | ? buf_spname(ins_buf) |
| 3351 | : ins_buf->b_sfname == NULL |
| 3352 | ? (char *)ins_buf->b_fname |
| 3353 | : (char *)ins_buf->b_sfname); |
| 3354 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 3355 | } |
| 3356 | else if (*e_cpt == NUL) |
| 3357 | break; |
| 3358 | else |
| 3359 | { |
| 3360 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3361 | type = -1; |
| 3362 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 3363 | { |
| 3364 | if (*e_cpt == 'k') |
| 3365 | type = CTRL_X_DICTIONARY; |
| 3366 | else |
| 3367 | type = CTRL_X_THESAURUS; |
| 3368 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 3369 | { |
| 3370 | dict = e_cpt; |
| 3371 | dict_f = DICT_FIRST; |
| 3372 | } |
| 3373 | } |
| 3374 | #ifdef FEAT_FIND_ID |
| 3375 | else if (*e_cpt == 'i') |
| 3376 | type = CTRL_X_PATH_PATTERNS; |
| 3377 | else if (*e_cpt == 'd') |
| 3378 | type = CTRL_X_PATH_DEFINES; |
| 3379 | #endif |
| 3380 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 3381 | { |
| 3382 | type = CTRL_X_TAGS; |
| 3383 | sprintf((char*)IObuff, _("Scanning tags.")); |
| 3384 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 3385 | } |
| 3386 | else |
| 3387 | type = -1; |
| 3388 | |
| 3389 | /* in any case e_cpt is advanced to the next entry */ |
| 3390 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 3391 | |
| 3392 | found_all = TRUE; |
| 3393 | if (type == -1) |
| 3394 | continue; |
| 3395 | } |
| 3396 | } |
| 3397 | |
| 3398 | switch (type) |
| 3399 | { |
| 3400 | case -1: |
| 3401 | break; |
| 3402 | #ifdef FEAT_FIND_ID |
| 3403 | case CTRL_X_PATH_PATTERNS: |
| 3404 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3405 | find_pattern_in_path(compl_pattern, compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3406 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3407 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3408 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3409 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 3410 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 3411 | break; |
| 3412 | #endif |
| 3413 | |
| 3414 | case CTRL_X_DICTIONARY: |
| 3415 | case CTRL_X_THESAURUS: |
| 3416 | ins_compl_dictionaries( |
| 3417 | dict ? dict |
| 3418 | : (type == CTRL_X_THESAURUS |
| 3419 | ? (*curbuf->b_p_tsr == NUL |
| 3420 | ? p_tsr |
| 3421 | : curbuf->b_p_tsr) |
| 3422 | : (*curbuf->b_p_dict == NUL |
| 3423 | ? p_dict |
| 3424 | : curbuf->b_p_dict)), |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3425 | compl_pattern, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | dict ? dict_f : 0, type == CTRL_X_THESAURUS); |
| 3427 | dict = NULL; |
| 3428 | break; |
| 3429 | |
| 3430 | case CTRL_X_TAGS: |
| 3431 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 3432 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3433 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3434 | |
| 3435 | /* Find up to TAG_MANY matches. Avoids that an enourmous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3436 | * of matches is found when compl_pattern is empty */ |
| 3437 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3438 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 3439 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 3440 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 3441 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3442 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3443 | } |
| 3444 | p_ic = save_p_ic; |
| 3445 | break; |
| 3446 | |
| 3447 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3448 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 3450 | { |
| 3451 | |
| 3452 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3453 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3454 | ins_compl_add_matches(num_matches, matches, |
| 3455 | #ifdef CASE_INSENSITIVE_FILENAME |
| 3456 | TRUE |
| 3457 | #else |
| 3458 | FALSE |
| 3459 | #endif |
| 3460 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | } |
| 3462 | break; |
| 3463 | |
| 3464 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3465 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 3466 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3467 | &num_matches, &matches) == EXPAND_OK) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3468 | ins_compl_add_matches(num_matches, matches, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3469 | break; |
| 3470 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3471 | #ifdef FEAT_COMPL_FUNC |
| 3472 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3473 | case CTRL_X_OMNI: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3474 | expand_by_function(type, compl_pattern); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3475 | break; |
| 3476 | #endif |
| 3477 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3478 | case CTRL_X_SPELL: |
| 3479 | #ifdef FEAT_SYN_HL |
| 3480 | num_matches = expand_spelling(first_match_pos.lnum, |
| 3481 | first_match_pos.col, compl_pattern, &matches); |
| 3482 | if (num_matches > 0) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3483 | ins_compl_add_matches(num_matches, matches, FALSE); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3484 | #endif |
| 3485 | break; |
| 3486 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3487 | default: /* normal ^P/^N and ^X^L */ |
| 3488 | /* |
| 3489 | * If 'infercase' is set, don't use 'smartcase' here |
| 3490 | */ |
| 3491 | save_p_scs = p_scs; |
| 3492 | if (ins_buf->b_p_inf) |
| 3493 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3494 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3495 | /* buffers other than curbuf are scanned from the beginning or the |
| 3496 | * end but never from the middle, thus setting nowrapscan in this |
| 3497 | * buffers is a good idea, on the other hand, we always set |
| 3498 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 3499 | save_p_ws = p_ws; |
| 3500 | if (ins_buf != curbuf) |
| 3501 | p_ws = FALSE; |
| 3502 | else if (*e_cpt == '.') |
| 3503 | p_ws = TRUE; |
| 3504 | for (;;) |
| 3505 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3506 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3507 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3508 | /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that |
| 3509 | * has added a word that was at the beginning of the line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3510 | if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3511 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3512 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3513 | compl_direction, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | else |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3515 | found_new_match = searchit(NULL, ins_buf, pos, |
| 3516 | compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3517 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3518 | RE_LAST); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3519 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3520 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3521 | /* set "compl_started" even on fail */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3522 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | first_match_pos = *pos; |
| 3524 | last_match_pos = *pos; |
| 3525 | } |
| 3526 | else if (first_match_pos.lnum == last_match_pos.lnum |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3527 | && first_match_pos.col == last_match_pos.col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3528 | found_new_match = FAIL; |
| 3529 | if (found_new_match == FAIL) |
| 3530 | { |
| 3531 | if (ins_buf == curbuf) |
| 3532 | found_all = TRUE; |
| 3533 | break; |
| 3534 | } |
| 3535 | |
| 3536 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3537 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3538 | && ini->lnum == pos->lnum |
| 3539 | && ini->col == pos->col) |
| 3540 | continue; |
| 3541 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 3542 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3543 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3544 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3545 | { |
| 3546 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 3547 | continue; |
| 3548 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3549 | if (!p_paste) |
| 3550 | ptr = skipwhite(ptr); |
| 3551 | } |
| 3552 | len = (int)STRLEN(ptr); |
| 3553 | } |
| 3554 | else |
| 3555 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3556 | char_u *tmp_ptr = ptr; |
| 3557 | |
| 3558 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3559 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3560 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3561 | /* Skip if already inside a word. */ |
| 3562 | if (vim_iswordp(tmp_ptr)) |
| 3563 | continue; |
| 3564 | /* Find start of next word. */ |
| 3565 | tmp_ptr = find_word_start(tmp_ptr); |
| 3566 | } |
| 3567 | /* Find end of this word. */ |
| 3568 | tmp_ptr = find_word_end(tmp_ptr); |
| 3569 | len = (int)(tmp_ptr - ptr); |
| 3570 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3571 | if ((compl_cont_status & CONT_ADDING) |
| 3572 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3573 | { |
| 3574 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 3575 | { |
| 3576 | /* Try next line, if any. the new word will be |
| 3577 | * "join" as if the normal command "J" was used. |
| 3578 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3579 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3580 | * works -- Acevedo */ |
| 3581 | STRNCPY(IObuff, ptr, len); |
| 3582 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3583 | tmp_ptr = ptr = skipwhite(ptr); |
| 3584 | /* Find start of next word. */ |
| 3585 | tmp_ptr = find_word_start(tmp_ptr); |
| 3586 | /* Find end of next word. */ |
| 3587 | tmp_ptr = find_word_end(tmp_ptr); |
| 3588 | if (tmp_ptr > ptr) |
| 3589 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3590 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3591 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3592 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3593 | IObuff[len++] = ' '; |
| 3594 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 3595 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3596 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3597 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 3598 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3599 | && (IObuff[len - 2] == '?' |
| 3600 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | IObuff[len++] = ' '; |
| 3602 | } |
| 3603 | /* copy as much as posible of the new word */ |
| 3604 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 3605 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 3606 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 3607 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3608 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3609 | } |
| 3610 | IObuff[len] = NUL; |
| 3611 | ptr = IObuff; |
| 3612 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3613 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3614 | continue; |
| 3615 | } |
| 3616 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3617 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3618 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3619 | 0, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3620 | { |
| 3621 | found_new_match = OK; |
| 3622 | break; |
| 3623 | } |
| 3624 | } |
| 3625 | p_scs = save_p_scs; |
| 3626 | p_ws = save_p_ws; |
| 3627 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3628 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3629 | /* check if compl_curr_match has changed, (e.g. other type of |
| 3630 | * expansion added somenthing) */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3631 | if (type != 0 && compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3632 | found_new_match = OK; |
| 3633 | |
| 3634 | /* break the loop for specialized modes (use 'complete' just for the |
| 3635 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
| 3636 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3637 | || found_new_match != FAIL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3638 | { |
| 3639 | if (got_int) |
| 3640 | break; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3641 | /* Fill the popup menu as soon as possible. */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3642 | if (pum_wanted() && type != -1) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3643 | ins_compl_check_keys(0); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3644 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3645 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 3646 | || compl_interrupted) |
| 3647 | break; |
| 3648 | compl_started = TRUE; |
| 3649 | } |
| 3650 | else |
| 3651 | { |
| 3652 | /* Mark a buffer scanned when it has been scanned completely */ |
| 3653 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 3654 | ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3656 | compl_started = FALSE; |
| 3657 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3658 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3659 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3660 | |
| 3661 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3662 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 3663 | found_new_match = FAIL; |
| 3664 | |
| 3665 | i = -1; /* total of matches, unknown */ |
| 3666 | if (found_new_match == FAIL |
| 3667 | || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) |
| 3668 | i = ins_compl_make_cyclic(); |
| 3669 | |
| 3670 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3671 | * just been made cyclic then we have to move compl_curr_match to the next |
| 3672 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3673 | 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] | 3674 | if (compl_curr_match == NULL) |
| 3675 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3676 | return i; |
| 3677 | } |
| 3678 | |
| 3679 | /* Delete the old text being completed. */ |
| 3680 | static void |
| 3681 | ins_compl_delete() |
| 3682 | { |
| 3683 | int i; |
| 3684 | |
| 3685 | /* |
| 3686 | * In insert mode: Delete the typed part. |
| 3687 | * In replace mode: Put the old characters back, if any. |
| 3688 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3689 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | backspace_until_column(i); |
| 3691 | changed_cline_bef_curs(); |
| 3692 | } |
| 3693 | |
| 3694 | /* Insert the new text being completed. */ |
| 3695 | static void |
| 3696 | ins_compl_insert() |
| 3697 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3698 | 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] | 3699 | compl_used_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | /* |
| 3703 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3704 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 3705 | * get more completions. If it is FALSE, then we just do nothing when there |
| 3706 | * are no more completions in a given direction. The latter case is used when |
| 3707 | * we are still in the middle of finding completions, to allow browsing |
| 3708 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3709 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 3710 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3711 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 3712 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | * |
| 3714 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3715 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 3716 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3717 | */ |
| 3718 | static int |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3719 | ins_compl_next(allow_get_expansion, count, insert_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3720 | int allow_get_expansion; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3721 | int count; /* repeat completion this many times; should |
| 3722 | be at least 1 */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3723 | int insert_match; /* Insert the newly selected match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3724 | { |
| 3725 | int num_matches = -1; |
| 3726 | int i; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3727 | int todo = count; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3728 | compl_T *found_compl = NULL; |
| 3729 | int found_end = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3730 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3731 | if (compl_leader != NULL |
| 3732 | && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3733 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3734 | /* Set "compl_shown_match" to the actually shown match, it may differ |
| 3735 | * when "compl_leader" is used to omit some of the matches. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3736 | while (!ins_compl_equal(compl_shown_match, |
| 3737 | compl_leader, STRLEN(compl_leader)) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3738 | && compl_shown_match->cp_next != NULL |
| 3739 | && compl_shown_match->cp_next != compl_first_match) |
| 3740 | compl_shown_match = compl_shown_match->cp_next; |
| 3741 | } |
| 3742 | |
| 3743 | if (allow_get_expansion && insert_match |
| 3744 | && (!compl_get_longest || compl_used_match)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3745 | /* Delete old text to be replaced */ |
| 3746 | ins_compl_delete(); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3747 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3748 | compl_pending = FALSE; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3749 | |
| 3750 | /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 3751 | * around. */ |
| 3752 | while (--todo >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3753 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3754 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3755 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3756 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3757 | found_end = (compl_first_match != NULL |
| 3758 | && (compl_shown_match->cp_next == compl_first_match |
| 3759 | || compl_shown_match == compl_first_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3760 | } |
| 3761 | else if (compl_shows_dir == BACKWARD |
| 3762 | && compl_shown_match->cp_prev != NULL) |
| 3763 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3764 | found_end = (compl_shown_match == compl_first_match); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3765 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3766 | found_end |= (compl_shown_match == compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | } |
| 3768 | else |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3769 | { |
| 3770 | compl_pending = TRUE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3771 | if (!allow_get_expansion) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3772 | return -1; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3773 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3774 | num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3775 | if (compl_pending && compl_direction == compl_shows_dir) |
| 3776 | compl_shown_match = compl_curr_match; |
| 3777 | found_end = FALSE; |
| 3778 | } |
| 3779 | if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 |
| 3780 | && compl_leader != NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3781 | && !ins_compl_equal(compl_shown_match, |
| 3782 | compl_leader, STRLEN(compl_leader))) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3783 | ++todo; |
| 3784 | else |
| 3785 | /* Remember a matching item. */ |
| 3786 | found_compl = compl_shown_match; |
| 3787 | |
| 3788 | /* Stop at the end of the list when we found a usable match. */ |
| 3789 | if (found_end) |
| 3790 | { |
| 3791 | if (found_compl != NULL) |
| 3792 | { |
| 3793 | compl_shown_match = found_compl; |
| 3794 | break; |
| 3795 | } |
| 3796 | todo = 1; /* use first usable match after wrapping around */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3797 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3800 | /* Insert the text of the new completion, or the compl_leader. */ |
| 3801 | if (insert_match) |
| 3802 | { |
| 3803 | if (!compl_get_longest || compl_used_match) |
| 3804 | ins_compl_insert(); |
| 3805 | else |
| 3806 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 3807 | } |
| 3808 | else |
| 3809 | compl_used_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3810 | |
| 3811 | if (!allow_get_expansion) |
| 3812 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3813 | /* may undisplay the popup menu first */ |
| 3814 | ins_compl_upd_pum(); |
| 3815 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3816 | /* redraw to show the user what was inserted */ |
| 3817 | update_screen(0); |
| 3818 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3819 | /* display the updated popup menu */ |
| 3820 | ins_compl_show_pum(); |
| 3821 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3822 | /* Delete old text to be replaced, since we're still searching and |
| 3823 | * don't want to match ourselves! */ |
| 3824 | ins_compl_delete(); |
| 3825 | } |
| 3826 | |
| 3827 | /* |
| 3828 | * Show the file name for the match (if any) |
| 3829 | * Truncate the file name to avoid a wait for return. |
| 3830 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3831 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | { |
| 3833 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3834 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | if (i <= 0) |
| 3836 | i = 0; |
| 3837 | else |
| 3838 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3839 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | msg(IObuff); |
| 3841 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 3842 | } |
| 3843 | |
| 3844 | return num_matches; |
| 3845 | } |
| 3846 | |
| 3847 | /* |
| 3848 | * Call this while finding completions, to check whether the user has hit a key |
| 3849 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3850 | * 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] | 3851 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3852 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3853 | */ |
| 3854 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3855 | ins_compl_check_keys(frequency) |
| 3856 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3857 | { |
| 3858 | static int count = 0; |
| 3859 | |
| 3860 | int c; |
| 3861 | |
| 3862 | /* Don't check when reading keys from a script. That would break the test |
| 3863 | * scripts */ |
| 3864 | if (using_script()) |
| 3865 | return; |
| 3866 | |
| 3867 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3868 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3869 | return; |
| 3870 | count = 0; |
| 3871 | |
| 3872 | ++no_mapping; |
| 3873 | c = vpeekc_any(); |
| 3874 | --no_mapping; |
| 3875 | if (c != NUL) |
| 3876 | { |
| 3877 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 3878 | { |
| 3879 | c = safe_vgetc(); /* Eat the character */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3880 | compl_shows_dir = ins_compl_key2dir(c); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3881 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 3882 | c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3883 | } |
| 3884 | else if (c != Ctrl_R) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3885 | compl_interrupted = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3886 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3887 | if (compl_pending && !got_int) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3888 | (void)ins_compl_next(FALSE, 1, TRUE); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | /* |
| 3892 | * Decide the direction of Insert mode complete from the key typed. |
| 3893 | * Returns BACKWARD or FORWARD. |
| 3894 | */ |
| 3895 | static int |
| 3896 | ins_compl_key2dir(c) |
| 3897 | int c; |
| 3898 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3899 | if (c == Ctrl_P || c == Ctrl_L |
| 3900 | || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP |
| 3901 | || c == K_S_UP || c == K_UP))) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3902 | return BACKWARD; |
| 3903 | return FORWARD; |
| 3904 | } |
| 3905 | |
| 3906 | /* |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3907 | * Return TRUE for keys that are used for completion only when the popup menu |
| 3908 | * is visible. |
| 3909 | */ |
| 3910 | static int |
| 3911 | ins_compl_pum_key(c) |
| 3912 | int c; |
| 3913 | { |
| 3914 | 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] | 3915 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 3916 | || c == K_UP || c == K_DOWN); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
| 3919 | /* |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3920 | * Decide the number of completions to move forward. |
| 3921 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 3922 | */ |
| 3923 | static int |
| 3924 | ins_compl_key2count(c) |
| 3925 | int c; |
| 3926 | { |
| 3927 | int h; |
| 3928 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3929 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3930 | { |
| 3931 | h = pum_get_height(); |
| 3932 | if (h > 3) |
| 3933 | h -= 2; /* keep some context */ |
| 3934 | return h; |
| 3935 | } |
| 3936 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3940 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 3941 | * to change the currently selected completion. |
| 3942 | */ |
| 3943 | static int |
| 3944 | ins_compl_use_match(c) |
| 3945 | int c; |
| 3946 | { |
| 3947 | switch (c) |
| 3948 | { |
| 3949 | case K_UP: |
| 3950 | case K_DOWN: |
| 3951 | case K_PAGEDOWN: |
| 3952 | case K_KPAGEDOWN: |
| 3953 | case K_S_DOWN: |
| 3954 | case K_PAGEUP: |
| 3955 | case K_KPAGEUP: |
| 3956 | case K_S_UP: |
| 3957 | return FALSE; |
| 3958 | } |
| 3959 | return TRUE; |
| 3960 | } |
| 3961 | |
| 3962 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3963 | * Do Insert mode completion. |
| 3964 | * Called when character "c" was typed, which has a meaning for completion. |
| 3965 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 3966 | */ |
| 3967 | static int |
| 3968 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3969 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3970 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3971 | char_u *line; |
| 3972 | int startcol = 0; /* column where searched text starts */ |
| 3973 | colnr_T curs_col; /* cursor column */ |
| 3974 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3975 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 3976 | compl_direction = ins_compl_key2dir(c); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3977 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3978 | { |
| 3979 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 3980 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3981 | did_ai = FALSE; |
| 3982 | #ifdef FEAT_SMARTINDENT |
| 3983 | did_si = FALSE; |
| 3984 | can_si = FALSE; |
| 3985 | can_si_back = FALSE; |
| 3986 | #endif |
| 3987 | if (stop_arrow() == FAIL) |
| 3988 | return FAIL; |
| 3989 | |
| 3990 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3991 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3992 | |
| 3993 | /* 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] | 3994 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 3995 | * instead of expand the one before the cursor, in word-wise if |
| 3996 | * "compl_startpos" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3997 | * is not in the same line as the cursor then fix it (the line has |
| 3998 | * been split because it was longer than 'tw'). if SOL is set then |
| 3999 | * skip the previous pattern, a word at the beginning of the line has |
| 4000 | * been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4001 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 4002 | && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4003 | { |
| 4004 | /* |
| 4005 | * it is a continued search |
| 4006 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4007 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4008 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 4009 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4010 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4011 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4012 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4013 | /* line (probably) wrapped, set compl_startpos to the |
| 4014 | * first non_blank in the line, if it is not a wordchar |
| 4015 | * include it to get a better pattern, but then we don't |
| 4016 | * want the "\\<" prefix, check it bellow */ |
| 4017 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 4018 | compl_startpos.col = compl_col; |
| 4019 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4020 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | } |
| 4022 | else |
| 4023 | { |
| 4024 | /* S_IPOS was set when we inserted a word that was at the |
| 4025 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4026 | * mode but first we need to redefine compl_startpos */ |
| 4027 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4028 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4029 | compl_cont_status |= CONT_SOL; |
| 4030 | compl_startpos.col = (colnr_T)(skipwhite( |
| 4031 | line + compl_length |
| 4032 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4033 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4034 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4035 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4036 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4037 | /* 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] | 4038 | * have enough space? just being paranoic */ |
| 4039 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4040 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4041 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4042 | compl_cont_status &= ~CONT_SOL; |
| 4043 | compl_length = (IOSIZE - MIN_SPACE); |
| 4044 | compl_col = curwin->w_cursor.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 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 4047 | if (compl_length < 1) |
| 4048 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4049 | } |
| 4050 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4051 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4052 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4053 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4054 | } |
| 4055 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4056 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4057 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4058 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4059 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4060 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4061 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4062 | compl_cont_status = 0; |
| 4063 | compl_cont_status |= CONT_N_ADDS; |
| 4064 | compl_startpos = curwin->w_cursor; |
| 4065 | startcol = (int)curs_col; |
| 4066 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | /* Work out completion pattern and original text -- webb */ |
| 4070 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 4071 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4072 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4073 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4074 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4075 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4076 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4077 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4078 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4079 | compl_col += ++startcol; |
| 4080 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4081 | } |
| 4082 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4083 | compl_pattern = str_foldcase(line + compl_col, |
| 4084 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4085 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4086 | compl_pattern = vim_strnsave(line + compl_col, |
| 4087 | compl_length); |
| 4088 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4089 | return FAIL; |
| 4090 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4091 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4092 | { |
| 4093 | char_u *prefix = (char_u *)"\\<"; |
| 4094 | |
| 4095 | /* we need 3 extra chars, 1 for the NUL and |
| 4096 | * 2 >= strlen(prefix) -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4097 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 4098 | compl_length) + 3); |
| 4099 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4100 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4101 | if (!vim_iswordp(line + compl_col) |
| 4102 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4103 | && ( |
| 4104 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4105 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4106 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4107 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4108 | #endif |
| 4109 | ))) |
| 4110 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4111 | STRCPY((char *)compl_pattern, prefix); |
| 4112 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 4113 | line + compl_col, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4114 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4115 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4116 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4117 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4118 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4119 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4120 | #endif |
| 4121 | ) |
| 4122 | { |
| 4123 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4124 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 4125 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4126 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4127 | compl_col += curs_col; |
| 4128 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4129 | } |
| 4130 | else |
| 4131 | { |
| 4132 | #ifdef FEAT_MBYTE |
| 4133 | /* Search the point of change class of multibyte character |
| 4134 | * or not a word single byte character backward. */ |
| 4135 | if (has_mbyte) |
| 4136 | { |
| 4137 | int base_class; |
| 4138 | int head_off; |
| 4139 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4140 | startcol -= (*mb_head_off)(line, line + startcol); |
| 4141 | base_class = mb_get_class(line + startcol); |
| 4142 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4143 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4144 | head_off = (*mb_head_off)(line, line + startcol); |
| 4145 | if (base_class != mb_get_class(line + startcol |
| 4146 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4147 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4148 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4149 | } |
| 4150 | } |
| 4151 | else |
| 4152 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4153 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4154 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4155 | compl_col += ++startcol; |
| 4156 | compl_length = (int)curs_col - startcol; |
| 4157 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4158 | { |
| 4159 | /* Only match word with at least two chars -- webb |
| 4160 | * there's no need to call quote_meta, |
| 4161 | * alloc(7) is enough -- Acevedo |
| 4162 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4163 | compl_pattern = alloc(7); |
| 4164 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4166 | STRCPY((char *)compl_pattern, "\\<"); |
| 4167 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 4168 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4169 | } |
| 4170 | else |
| 4171 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4172 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 4173 | compl_length) + 3); |
| 4174 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4175 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4176 | STRCPY((char *)compl_pattern, "\\<"); |
| 4177 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 4178 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | } |
| 4182 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4183 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4184 | compl_col = skipwhite(line) - line; |
| 4185 | compl_length = (int)curs_col - (int)compl_col; |
| 4186 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 4187 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4188 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4189 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 4190 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4191 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4192 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4193 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4194 | return FAIL; |
| 4195 | } |
| 4196 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 4197 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4198 | while (--startcol >= 0 && vim_isfilec(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4199 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4200 | compl_col += ++startcol; |
| 4201 | compl_length = (int)curs_col - startcol; |
| 4202 | compl_pattern = addstar(line + compl_col, compl_length, |
| 4203 | EXPAND_FILES); |
| 4204 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4205 | return FAIL; |
| 4206 | } |
| 4207 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 4208 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4209 | compl_pattern = vim_strnsave(line, curs_col); |
| 4210 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4212 | set_cmd_context(&compl_xp, compl_pattern, |
| 4213 | (int)STRLEN(compl_pattern), curs_col); |
| 4214 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 4215 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4216 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4217 | startcol = (int)(compl_xp.xp_pattern - compl_pattern); |
| 4218 | compl_col = startcol; |
| 4219 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4220 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4221 | 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] | 4222 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4223 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4224 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4225 | * Call user defined function 'completefunc' with "a:findstart" |
| 4226 | * 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] | 4227 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4228 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4229 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4230 | char_u *funcname; |
| 4231 | pos_T pos; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4232 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4233 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4234 | * string */ |
| 4235 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 4236 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 4237 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4238 | { |
| 4239 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 4240 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4241 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4242 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4243 | |
| 4244 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4245 | args[1] = NULL; |
| 4246 | pos = curwin->w_cursor; |
| 4247 | col = call_func_retnr(funcname, 2, args, FALSE); |
| 4248 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4249 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4250 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4251 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4252 | compl_col = col; |
| 4253 | if ((colnr_T)compl_col > curs_col) |
| 4254 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4255 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4256 | /* Setup variables for completion. Need to obtain "line" again, |
| 4257 | * it may have become invalid. */ |
| 4258 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4259 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4260 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4261 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4262 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4263 | return FAIL; |
| 4264 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4265 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 4266 | { |
| 4267 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 4268 | if (spell_bad_len > 0) |
| 4269 | compl_col = curs_col - spell_bad_len; |
| 4270 | else |
| 4271 | compl_col = spell_word_start(startcol); |
| 4272 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4273 | return FAIL; |
Bram Moolenaar | c54b8a7 | 2005-09-30 21:20:29 +0000 | [diff] [blame] | 4274 | spell_expand_check_cap(compl_col); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4275 | compl_length = (int)curs_col - compl_col; |
| 4276 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4277 | if (compl_pattern == NULL) |
| 4278 | #endif |
| 4279 | return FAIL; |
| 4280 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4281 | else |
| 4282 | { |
| 4283 | EMSG2(_(e_intern2), "ins_complete()"); |
| 4284 | return FAIL; |
| 4285 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4286 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4287 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | { |
| 4289 | edit_submode_pre = (char_u *)_(" Adding"); |
| 4290 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4291 | { |
| 4292 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 4293 | #ifdef FEAT_COMMENTS |
| 4294 | char_u *old = curbuf->b_p_com; |
| 4295 | |
| 4296 | curbuf->b_p_com = (char_u *)""; |
| 4297 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4298 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4299 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4300 | ins_eol('\r'); |
| 4301 | #ifdef FEAT_COMMENTS |
| 4302 | curbuf->b_p_com = old; |
| 4303 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4304 | compl_length = 0; |
| 4305 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4306 | } |
| 4307 | } |
| 4308 | else |
| 4309 | { |
| 4310 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4311 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4312 | } |
| 4313 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4314 | if (compl_cont_status & CONT_LOCAL) |
| 4315 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4316 | else |
| 4317 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 4318 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | /* Always add completion for the original text. Note that |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4320 | * "compl_orig_text" itself (not a copy) is added, it will be freed |
| 4321 | * when the list of matches is freed. */ |
| 4322 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 4323 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4324 | -1, FALSE, NULL, NULL, 0, ORIGINAL_TEXT) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4325 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4326 | vim_free(compl_pattern); |
| 4327 | compl_pattern = NULL; |
| 4328 | vim_free(compl_orig_text); |
| 4329 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4330 | return FAIL; |
| 4331 | } |
| 4332 | |
| 4333 | /* showmode might reset the internal line pointers, so it must |
| 4334 | * be called before line = ml_get(), or when this address is no |
| 4335 | * longer needed. -- Acevedo. |
| 4336 | */ |
| 4337 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 4338 | edit_submode_highl = HLF_COUNT; |
| 4339 | showmode(); |
| 4340 | edit_submode_extra = NULL; |
| 4341 | out_flush(); |
| 4342 | } |
| 4343 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4344 | compl_shown_match = compl_curr_match; |
| 4345 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4346 | |
| 4347 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4348 | * Find next match (and following matches). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4349 | */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4350 | n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4351 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4352 | /* may undisplay the popup menu */ |
| 4353 | ins_compl_upd_pum(); |
| 4354 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4355 | if (n > 1) /* all matches have been found */ |
| 4356 | compl_matches = n; |
| 4357 | compl_curr_match = compl_shown_match; |
| 4358 | compl_direction = compl_shows_dir; |
| 4359 | compl_interrupted = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4360 | |
| 4361 | /* eat the ESC to avoid leaving insert mode */ |
| 4362 | if (got_int && !global_busy) |
| 4363 | { |
| 4364 | (void)vgetc(); |
| 4365 | got_int = FALSE; |
| 4366 | } |
| 4367 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4368 | /* 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] | 4369 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4370 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4371 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 4372 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4373 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 4374 | edit_submode_highl = HLF_E; |
| 4375 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 4376 | * because we couldn't expand anything at first place, but if we used |
| 4377 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 4378 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4379 | if ( compl_length > 1 |
| 4380 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4381 | || (ctrl_x_mode != 0 |
| 4382 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 4383 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4384 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4387 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4388 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4390 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4391 | |
| 4392 | if (edit_submode_extra == NULL) |
| 4393 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4394 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4395 | { |
| 4396 | edit_submode_extra = (char_u *)_("Back at original"); |
| 4397 | edit_submode_highl = HLF_W; |
| 4398 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4399 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4400 | { |
| 4401 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 4402 | edit_submode_highl = HLF_COUNT; |
| 4403 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4404 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4405 | { |
| 4406 | edit_submode_extra = (char_u *)_("The only match"); |
| 4407 | edit_submode_highl = HLF_COUNT; |
| 4408 | } |
| 4409 | else |
| 4410 | { |
| 4411 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4412 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4413 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4414 | int number = 0; |
| 4415 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4416 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4417 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4418 | { |
| 4419 | /* search backwards for the first valid (!= -1) number. |
| 4420 | * This should normally succeed already at the first loop |
| 4421 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4422 | for (match = compl_curr_match->cp_prev; match != NULL |
| 4423 | && match != compl_first_match; |
| 4424 | match = match->cp_prev) |
| 4425 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4426 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4427 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4428 | break; |
| 4429 | } |
| 4430 | if (match != NULL) |
| 4431 | /* go up and assign all numbers which are not assigned |
| 4432 | * yet */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4433 | for (match = match->cp_next; |
| 4434 | match != NULL && match->cp_number == -1; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4435 | match = match->cp_next) |
| 4436 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4437 | } |
| 4438 | else /* BACKWARD */ |
| 4439 | { |
| 4440 | /* search forwards (upwards) for the first valid (!= -1) |
| 4441 | * number. This should normally succeed already at the |
| 4442 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4443 | for (match = compl_curr_match->cp_next; match != NULL |
| 4444 | && match != compl_first_match; |
| 4445 | match = match->cp_next) |
| 4446 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4447 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4448 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4449 | break; |
| 4450 | } |
| 4451 | if (match != NULL) |
| 4452 | /* go down and assign all numbers which are not |
| 4453 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4454 | for (match = match->cp_prev; match |
| 4455 | && match->cp_number == -1; |
| 4456 | match = match->cp_prev) |
| 4457 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4458 | } |
| 4459 | } |
| 4460 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4461 | /* The match should always have a sequence number now, this is |
| 4462 | * just a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4463 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4464 | { |
| 4465 | /* Space for 10 text chars. + 2x10-digit no.s */ |
| 4466 | static char_u match_ref[31]; |
| 4467 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4468 | if (compl_matches > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4469 | sprintf((char *)IObuff, _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4470 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4471 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4472 | sprintf((char *)IObuff, _("match %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4473 | compl_curr_match->cp_number); |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4474 | vim_strncpy(match_ref, IObuff, 30); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4475 | edit_submode_extra = match_ref; |
| 4476 | edit_submode_highl = HLF_R; |
| 4477 | if (dollar_vcol) |
| 4478 | curs_columns(FALSE); |
| 4479 | } |
| 4480 | } |
| 4481 | } |
| 4482 | |
| 4483 | /* Show a message about what (completion) mode we're in. */ |
| 4484 | showmode(); |
| 4485 | if (edit_submode_extra != NULL) |
| 4486 | { |
| 4487 | if (!p_smd) |
| 4488 | msg_attr(edit_submode_extra, |
| 4489 | edit_submode_highl < HLF_COUNT |
| 4490 | ? hl_attr(edit_submode_highl) : 0); |
| 4491 | } |
| 4492 | else |
| 4493 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 4494 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4495 | ins_compl_show_pum(); |
| 4496 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4497 | return OK; |
| 4498 | } |
| 4499 | |
| 4500 | /* |
| 4501 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 4502 | * If dest is not NULL the chars. are copied there quoting (with |
| 4503 | * a backslash) the metachars, and dest would be NUL terminated. |
| 4504 | * Returns the length (needed) of dest |
| 4505 | */ |
| 4506 | static int |
| 4507 | quote_meta(dest, src, len) |
| 4508 | char_u *dest; |
| 4509 | char_u *src; |
| 4510 | int len; |
| 4511 | { |
| 4512 | int m; |
| 4513 | |
| 4514 | for (m = len; --len >= 0; src++) |
| 4515 | { |
| 4516 | switch (*src) |
| 4517 | { |
| 4518 | case '.': |
| 4519 | case '*': |
| 4520 | case '[': |
| 4521 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 4522 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 4523 | break; |
| 4524 | case '~': |
| 4525 | if (!p_magic) /* quote these only if magic is set */ |
| 4526 | break; |
| 4527 | case '\\': |
| 4528 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 4529 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 4530 | break; |
| 4531 | case '^': /* currently it's not needed. */ |
| 4532 | case '$': |
| 4533 | m++; |
| 4534 | if (dest != NULL) |
| 4535 | *dest++ = '\\'; |
| 4536 | break; |
| 4537 | } |
| 4538 | if (dest != NULL) |
| 4539 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4540 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4541 | /* Copy remaining bytes of a multibyte character. */ |
| 4542 | if (has_mbyte) |
| 4543 | { |
| 4544 | int i, mb_len; |
| 4545 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4546 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4547 | if (mb_len > 0 && len >= mb_len) |
| 4548 | for (i = 0; i < mb_len; ++i) |
| 4549 | { |
| 4550 | --len; |
| 4551 | ++src; |
| 4552 | if (dest != NULL) |
| 4553 | *dest++ = *src; |
| 4554 | } |
| 4555 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4556 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4557 | } |
| 4558 | if (dest != NULL) |
| 4559 | *dest = NUL; |
| 4560 | |
| 4561 | return m; |
| 4562 | } |
| 4563 | #endif /* FEAT_INS_EXPAND */ |
| 4564 | |
| 4565 | /* |
| 4566 | * Next character is interpreted literally. |
| 4567 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 4568 | * If one or two digits are entered, the next character is given to vungetc(). |
| 4569 | * For Unicode a character > 255 may be returned. |
| 4570 | */ |
| 4571 | int |
| 4572 | get_literal() |
| 4573 | { |
| 4574 | int cc; |
| 4575 | int nc; |
| 4576 | int i; |
| 4577 | int hex = FALSE; |
| 4578 | int octal = FALSE; |
| 4579 | #ifdef FEAT_MBYTE |
| 4580 | int unicode = 0; |
| 4581 | #endif |
| 4582 | |
| 4583 | if (got_int) |
| 4584 | return Ctrl_C; |
| 4585 | |
| 4586 | #ifdef FEAT_GUI |
| 4587 | /* |
| 4588 | * In GUI there is no point inserting the internal code for a special key. |
| 4589 | * It is more useful to insert the string "<KEY>" instead. This would |
| 4590 | * probably be useful in a text window too, but it would not be |
| 4591 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 4592 | */ |
| 4593 | if (gui.in_use) |
| 4594 | ++allow_keys; |
| 4595 | #endif |
| 4596 | #ifdef USE_ON_FLY_SCROLL |
| 4597 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 4598 | #endif |
| 4599 | ++no_mapping; /* don't map the next key hits */ |
| 4600 | cc = 0; |
| 4601 | i = 0; |
| 4602 | for (;;) |
| 4603 | { |
| 4604 | do |
| 4605 | nc = safe_vgetc(); |
| 4606 | while (nc == K_IGNORE || nc == K_VER_SCROLLBAR |
| 4607 | || nc == K_HOR_SCROLLBAR); |
| 4608 | #ifdef FEAT_CMDL_INFO |
| 4609 | if (!(State & CMDLINE) |
| 4610 | # ifdef FEAT_MBYTE |
| 4611 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 4612 | # endif |
| 4613 | ) |
| 4614 | add_to_showcmd(nc); |
| 4615 | #endif |
| 4616 | if (nc == 'x' || nc == 'X') |
| 4617 | hex = TRUE; |
| 4618 | else if (nc == 'o' || nc == 'O') |
| 4619 | octal = TRUE; |
| 4620 | #ifdef FEAT_MBYTE |
| 4621 | else if (nc == 'u' || nc == 'U') |
| 4622 | unicode = nc; |
| 4623 | #endif |
| 4624 | else |
| 4625 | { |
| 4626 | if (hex |
| 4627 | #ifdef FEAT_MBYTE |
| 4628 | || unicode != 0 |
| 4629 | #endif |
| 4630 | ) |
| 4631 | { |
| 4632 | if (!vim_isxdigit(nc)) |
| 4633 | break; |
| 4634 | cc = cc * 16 + hex2nr(nc); |
| 4635 | } |
| 4636 | else if (octal) |
| 4637 | { |
| 4638 | if (nc < '0' || nc > '7') |
| 4639 | break; |
| 4640 | cc = cc * 8 + nc - '0'; |
| 4641 | } |
| 4642 | else |
| 4643 | { |
| 4644 | if (!VIM_ISDIGIT(nc)) |
| 4645 | break; |
| 4646 | cc = cc * 10 + nc - '0'; |
| 4647 | } |
| 4648 | |
| 4649 | ++i; |
| 4650 | } |
| 4651 | |
| 4652 | if (cc > 255 |
| 4653 | #ifdef FEAT_MBYTE |
| 4654 | && unicode == 0 |
| 4655 | #endif |
| 4656 | ) |
| 4657 | cc = 255; /* limit range to 0-255 */ |
| 4658 | nc = 0; |
| 4659 | |
| 4660 | if (hex) /* hex: up to two chars */ |
| 4661 | { |
| 4662 | if (i >= 2) |
| 4663 | break; |
| 4664 | } |
| 4665 | #ifdef FEAT_MBYTE |
| 4666 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 4667 | { |
| 4668 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 4669 | break; |
| 4670 | } |
| 4671 | #endif |
| 4672 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 4673 | break; |
| 4674 | } |
| 4675 | if (i == 0) /* no number entered */ |
| 4676 | { |
| 4677 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 4678 | { |
| 4679 | cc = '\n'; |
| 4680 | nc = 0; |
| 4681 | } |
| 4682 | else |
| 4683 | { |
| 4684 | cc = nc; |
| 4685 | nc = 0; |
| 4686 | } |
| 4687 | } |
| 4688 | |
| 4689 | if (cc == 0) /* NUL is stored as NL */ |
| 4690 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 4691 | #ifdef FEAT_MBYTE |
| 4692 | if (enc_dbcs && (cc & 0xff) == 0) |
| 4693 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 4694 | second byte will cause trouble! */ |
| 4695 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4696 | |
| 4697 | --no_mapping; |
| 4698 | #ifdef FEAT_GUI |
| 4699 | if (gui.in_use) |
| 4700 | --allow_keys; |
| 4701 | #endif |
| 4702 | if (nc) |
| 4703 | vungetc(nc); |
| 4704 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 4705 | return cc; |
| 4706 | } |
| 4707 | |
| 4708 | /* |
| 4709 | * Insert character, taking care of special keys and mod_mask |
| 4710 | */ |
| 4711 | static void |
| 4712 | insert_special(c, allow_modmask, ctrlv) |
| 4713 | int c; |
| 4714 | int allow_modmask; |
| 4715 | int ctrlv; /* c was typed after CTRL-V */ |
| 4716 | { |
| 4717 | char_u *p; |
| 4718 | int len; |
| 4719 | |
| 4720 | /* |
| 4721 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 4722 | * inserted with ins_str(), so as not to replace characters in replace |
| 4723 | * mode. |
| 4724 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 4725 | * unless 'allow_modmask' is TRUE. |
| 4726 | */ |
| 4727 | #ifdef MACOS |
| 4728 | /* Command-key never produces a normal key */ |
| 4729 | if (mod_mask & MOD_MASK_CMD) |
| 4730 | allow_modmask = TRUE; |
| 4731 | #endif |
| 4732 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 4733 | { |
| 4734 | p = get_special_key_name(c, mod_mask); |
| 4735 | len = (int)STRLEN(p); |
| 4736 | c = p[len - 1]; |
| 4737 | if (len > 2) |
| 4738 | { |
| 4739 | if (stop_arrow() == FAIL) |
| 4740 | return; |
| 4741 | p[len - 1] = NUL; |
| 4742 | ins_str(p); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 4743 | AppendToRedobuffLit(p, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4744 | ctrlv = FALSE; |
| 4745 | } |
| 4746 | } |
| 4747 | if (stop_arrow() == OK) |
| 4748 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 4749 | } |
| 4750 | |
| 4751 | /* |
| 4752 | * Special characters in this context are those that need processing other |
| 4753 | * than the simple insertion that can be performed here. This includes ESC |
| 4754 | * which terminates the insert, and CR/NL which need special processing to |
| 4755 | * open up a new line. This routine tries to optimize insertions performed by |
| 4756 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 4757 | * stop and defer processing to the "normal" mechanism. |
| 4758 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 4759 | */ |
| 4760 | #ifdef EBCDIC |
| 4761 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 4762 | #else |
| 4763 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 4764 | #endif |
| 4765 | |
| 4766 | #ifdef FEAT_MBYTE |
| 4767 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 4768 | #else |
| 4769 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 4770 | #endif |
| 4771 | |
| 4772 | void |
| 4773 | insertchar(c, flags, second_indent) |
| 4774 | int c; /* character to insert or NUL */ |
| 4775 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 4776 | int second_indent; /* indent for second line if >= 0 */ |
| 4777 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4778 | int textwidth; |
| 4779 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4780 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4781 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4782 | int fo_ins_blank; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4783 | |
| 4784 | textwidth = comp_textwidth(flags & INSCHAR_FORMAT); |
| 4785 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4786 | |
| 4787 | /* |
| 4788 | * Try to break the line in two or more pieces when: |
| 4789 | * - Always do this if we have been called to do formatting only. |
| 4790 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 4791 | * ends in white space. |
| 4792 | * - Otherwise: |
| 4793 | * - Don't do this if inserting a blank |
| 4794 | * - Don't do this if an existing character is being replaced, unless |
| 4795 | * we're in VREPLACE mode. |
| 4796 | * - Do this if the cursor is not on the line where insert started |
| 4797 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 4798 | * before the insert. |
| 4799 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 4800 | * before 'textwidth' |
| 4801 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4802 | if (textwidth > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4803 | && ((flags & INSCHAR_FORMAT) |
| 4804 | || (!vim_iswhite(c) |
| 4805 | && !((State & REPLACE_FLAG) |
| 4806 | #ifdef FEAT_VREPLACE |
| 4807 | && !(State & VREPLACE_FLAG) |
| 4808 | #endif |
| 4809 | && *ml_get_cursor() != NUL) |
| 4810 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 4811 | || ((!has_format_option(FO_INS_LONG) |
| 4812 | || Insstart_textlen <= (colnr_T)textwidth) |
| 4813 | && (!fo_ins_blank |
| 4814 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 4815 | )))))) |
| 4816 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4817 | /* Format with 'formatexpr' when it's set. Use internal formatting |
| 4818 | * when 'formatexpr' isn't set or it returns non-zero. */ |
| 4819 | #if defined(FEAT_EVAL) |
| 4820 | if (*curbuf->b_p_fex == NUL |
| 4821 | || fex_format(curwin->w_cursor.lnum, 1L) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4823 | internal_format(textwidth, second_indent, flags, c == NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4824 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4825 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | if (c == NUL) /* only formatting was wanted */ |
| 4827 | return; |
| 4828 | |
| 4829 | #ifdef FEAT_COMMENTS |
| 4830 | /* Check whether this character should end a comment. */ |
| 4831 | if (did_ai && (int)c == end_comment_pending) |
| 4832 | { |
| 4833 | char_u *line; |
| 4834 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 4835 | int middle_len, end_len; |
| 4836 | int i; |
| 4837 | |
| 4838 | /* |
| 4839 | * Need to remove existing (middle) comment leader and insert end |
| 4840 | * comment leader. First, check what comment leader we can find. |
| 4841 | */ |
| 4842 | i = get_leader_len(line = ml_get_curline(), &p, FALSE); |
| 4843 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 4844 | { |
| 4845 | /* Skip middle-comment string */ |
| 4846 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 4847 | ++p; |
| 4848 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 4849 | /* Don't count trailing white space for middle_len */ |
| 4850 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 4851 | --middle_len; |
| 4852 | |
| 4853 | /* Find the end-comment string */ |
| 4854 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 4855 | ++p; |
| 4856 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 4857 | |
| 4858 | /* Skip white space before the cursor */ |
| 4859 | i = curwin->w_cursor.col; |
| 4860 | while (--i >= 0 && vim_iswhite(line[i])) |
| 4861 | ; |
| 4862 | i++; |
| 4863 | |
| 4864 | /* Skip to before the middle leader */ |
| 4865 | i -= middle_len; |
| 4866 | |
| 4867 | /* Check some expected things before we go on */ |
| 4868 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 4869 | { |
| 4870 | /* Backspace over all the stuff we want to replace */ |
| 4871 | backspace_until_column(i); |
| 4872 | |
| 4873 | /* |
| 4874 | * Insert the end-comment string, except for the last |
| 4875 | * character, which will get inserted as normal later. |
| 4876 | */ |
| 4877 | ins_bytes_len(lead_end, end_len - 1); |
| 4878 | } |
| 4879 | } |
| 4880 | } |
| 4881 | end_comment_pending = NUL; |
| 4882 | #endif |
| 4883 | |
| 4884 | did_ai = FALSE; |
| 4885 | #ifdef FEAT_SMARTINDENT |
| 4886 | did_si = FALSE; |
| 4887 | can_si = FALSE; |
| 4888 | can_si_back = FALSE; |
| 4889 | #endif |
| 4890 | |
| 4891 | /* |
| 4892 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 4893 | * This speeds up normal text input considerably. |
| 4894 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 4895 | * need to re-indent at a ':', or any other character (but not what |
| 4896 | * 'paste' is set).. |
| 4897 | */ |
| 4898 | #ifdef USE_ON_FLY_SCROLL |
| 4899 | dont_scroll = FALSE; /* allow scrolling here */ |
| 4900 | #endif |
| 4901 | |
| 4902 | if ( !ISSPECIAL(c) |
| 4903 | #ifdef FEAT_MBYTE |
| 4904 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 4905 | #endif |
| 4906 | && vpeekc() != NUL |
| 4907 | && !(State & REPLACE_FLAG) |
| 4908 | #ifdef FEAT_CINDENT |
| 4909 | && !cindent_on() |
| 4910 | #endif |
| 4911 | #ifdef FEAT_RIGHTLEFT |
| 4912 | && !p_ri |
| 4913 | #endif |
| 4914 | ) |
| 4915 | { |
| 4916 | #define INPUT_BUFLEN 100 |
| 4917 | char_u buf[INPUT_BUFLEN + 1]; |
| 4918 | int i; |
| 4919 | colnr_T virtcol = 0; |
| 4920 | |
| 4921 | buf[0] = c; |
| 4922 | i = 1; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4923 | if (textwidth > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4924 | virtcol = get_nolist_virtcol(); |
| 4925 | /* |
| 4926 | * Stop the string when: |
| 4927 | * - no more chars available |
| 4928 | * - finding a special character (command key) |
| 4929 | * - buffer is full |
| 4930 | * - running into the 'textwidth' boundary |
| 4931 | * - need to check for abbreviation: A non-word char after a word-char |
| 4932 | */ |
| 4933 | while ( (c = vpeekc()) != NUL |
| 4934 | && !ISSPECIAL(c) |
| 4935 | #ifdef FEAT_MBYTE |
| 4936 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 4937 | #endif |
| 4938 | && i < INPUT_BUFLEN |
| 4939 | && (textwidth == 0 |
| 4940 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 4941 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 4942 | { |
| 4943 | #ifdef FEAT_RIGHTLEFT |
| 4944 | c = vgetc(); |
| 4945 | if (p_hkmap && KeyTyped) |
| 4946 | c = hkmap(c); /* Hebrew mode mapping */ |
| 4947 | # ifdef FEAT_FKMAP |
| 4948 | if (p_fkmap && KeyTyped) |
| 4949 | c = fkmap(c); /* Farsi mode mapping */ |
| 4950 | # endif |
| 4951 | buf[i++] = c; |
| 4952 | #else |
| 4953 | buf[i++] = vgetc(); |
| 4954 | #endif |
| 4955 | } |
| 4956 | |
| 4957 | #ifdef FEAT_DIGRAPHS |
| 4958 | do_digraph(-1); /* clear digraphs */ |
| 4959 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 4960 | #endif |
| 4961 | buf[i] = NUL; |
| 4962 | ins_str(buf); |
| 4963 | if (flags & INSCHAR_CTRLV) |
| 4964 | { |
| 4965 | redo_literal(*buf); |
| 4966 | i = 1; |
| 4967 | } |
| 4968 | else |
| 4969 | i = 0; |
| 4970 | if (buf[i] != NUL) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 4971 | AppendToRedobuffLit(buf + i, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | } |
| 4973 | else |
| 4974 | { |
| 4975 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4976 | int cc; |
| 4977 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4978 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 4979 | { |
| 4980 | char_u buf[MB_MAXBYTES + 1]; |
| 4981 | |
| 4982 | (*mb_char2bytes)(c, buf); |
| 4983 | buf[cc] = NUL; |
| 4984 | ins_char_bytes(buf, cc); |
| 4985 | AppendCharToRedobuff(c); |
| 4986 | } |
| 4987 | else |
| 4988 | #endif |
| 4989 | { |
| 4990 | ins_char(c); |
| 4991 | if (flags & INSCHAR_CTRLV) |
| 4992 | redo_literal(c); |
| 4993 | else |
| 4994 | AppendCharToRedobuff(c); |
| 4995 | } |
| 4996 | } |
| 4997 | } |
| 4998 | |
| 4999 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5000 | * Format text at the current insert position. |
| 5001 | */ |
| 5002 | static void |
| 5003 | internal_format(textwidth, second_indent, flags, format_only) |
| 5004 | int textwidth; |
| 5005 | int second_indent; |
| 5006 | int flags; |
| 5007 | int format_only; |
| 5008 | { |
| 5009 | int cc; |
| 5010 | int save_char = NUL; |
| 5011 | int haveto_redraw = FALSE; |
| 5012 | int fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 5013 | #ifdef FEAT_MBYTE |
| 5014 | int fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 5015 | #endif |
| 5016 | int fo_white_par = has_format_option(FO_WHITE_PAR); |
| 5017 | int first_line = TRUE; |
| 5018 | #ifdef FEAT_COMMENTS |
| 5019 | colnr_T leader_len; |
| 5020 | int no_leader = FALSE; |
| 5021 | int do_comments = (flags & INSCHAR_DO_COM); |
| 5022 | #endif |
| 5023 | |
| 5024 | /* |
| 5025 | * When 'ai' is off we don't want a space under the cursor to be |
| 5026 | * deleted. Replace it with an 'x' temporarily. |
| 5027 | */ |
| 5028 | if (!curbuf->b_p_ai) |
| 5029 | { |
| 5030 | cc = gchar_cursor(); |
| 5031 | if (vim_iswhite(cc)) |
| 5032 | { |
| 5033 | save_char = cc; |
| 5034 | pchar_cursor('x'); |
| 5035 | } |
| 5036 | } |
| 5037 | |
| 5038 | /* |
| 5039 | * Repeat breaking lines, until the current line is not too long. |
| 5040 | */ |
| 5041 | while (!got_int) |
| 5042 | { |
| 5043 | int startcol; /* Cursor column at entry */ |
| 5044 | int wantcol; /* column at textwidth border */ |
| 5045 | int foundcol; /* column for start of spaces */ |
| 5046 | int end_foundcol = 0; /* column for start of word */ |
| 5047 | colnr_T len; |
| 5048 | colnr_T virtcol; |
| 5049 | #ifdef FEAT_VREPLACE |
| 5050 | int orig_col = 0; |
| 5051 | char_u *saved_text = NULL; |
| 5052 | #endif |
| 5053 | colnr_T col; |
| 5054 | |
| 5055 | virtcol = get_nolist_virtcol(); |
| 5056 | if (virtcol < (colnr_T)textwidth) |
| 5057 | break; |
| 5058 | |
| 5059 | #ifdef FEAT_COMMENTS |
| 5060 | if (no_leader) |
| 5061 | do_comments = FALSE; |
| 5062 | else if (!(flags & INSCHAR_FORMAT) |
| 5063 | && has_format_option(FO_WRAP_COMS)) |
| 5064 | do_comments = TRUE; |
| 5065 | |
| 5066 | /* Don't break until after the comment leader */ |
| 5067 | if (do_comments) |
| 5068 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE); |
| 5069 | else |
| 5070 | leader_len = 0; |
| 5071 | |
| 5072 | /* If the line doesn't start with a comment leader, then don't |
| 5073 | * start one in a following broken line. Avoids that a %word |
| 5074 | * moved to the start of the next line causes all following lines |
| 5075 | * to start with %. */ |
| 5076 | if (leader_len == 0) |
| 5077 | no_leader = TRUE; |
| 5078 | #endif |
| 5079 | if (!(flags & INSCHAR_FORMAT) |
| 5080 | #ifdef FEAT_COMMENTS |
| 5081 | && leader_len == 0 |
| 5082 | #endif |
| 5083 | && !has_format_option(FO_WRAP)) |
| 5084 | |
| 5085 | { |
| 5086 | textwidth = 0; |
| 5087 | break; |
| 5088 | } |
| 5089 | if ((startcol = curwin->w_cursor.col) == 0) |
| 5090 | break; |
| 5091 | |
| 5092 | /* find column of textwidth border */ |
| 5093 | coladvance((colnr_T)textwidth); |
| 5094 | wantcol = curwin->w_cursor.col; |
| 5095 | |
| 5096 | curwin->w_cursor.col = startcol - 1; |
| 5097 | #ifdef FEAT_MBYTE |
| 5098 | /* Correct cursor for multi-byte character. */ |
| 5099 | if (has_mbyte) |
| 5100 | mb_adjust_cursor(); |
| 5101 | #endif |
| 5102 | foundcol = 0; |
| 5103 | |
| 5104 | /* |
| 5105 | * Find position to break at. |
| 5106 | * Stop at first entered white when 'formatoptions' has 'v' |
| 5107 | */ |
| 5108 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
| 5109 | || curwin->w_cursor.lnum != Insstart.lnum |
| 5110 | || curwin->w_cursor.col >= Insstart.col) |
| 5111 | { |
| 5112 | cc = gchar_cursor(); |
| 5113 | if (WHITECHAR(cc)) |
| 5114 | { |
| 5115 | /* remember position of blank just before text */ |
| 5116 | end_foundcol = curwin->w_cursor.col; |
| 5117 | |
| 5118 | /* find start of sequence of blanks */ |
| 5119 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 5120 | { |
| 5121 | dec_cursor(); |
| 5122 | cc = gchar_cursor(); |
| 5123 | } |
| 5124 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 5125 | break; /* only spaces in front of text */ |
| 5126 | #ifdef FEAT_COMMENTS |
| 5127 | /* Don't break until after the comment leader */ |
| 5128 | if (curwin->w_cursor.col < leader_len) |
| 5129 | break; |
| 5130 | #endif |
| 5131 | if (has_format_option(FO_ONE_LETTER)) |
| 5132 | { |
| 5133 | /* do not break after one-letter words */ |
| 5134 | if (curwin->w_cursor.col == 0) |
| 5135 | break; /* one-letter word at begin */ |
| 5136 | |
| 5137 | col = curwin->w_cursor.col; |
| 5138 | dec_cursor(); |
| 5139 | cc = gchar_cursor(); |
| 5140 | |
| 5141 | if (WHITECHAR(cc)) |
| 5142 | continue; /* one-letter, continue */ |
| 5143 | curwin->w_cursor.col = col; |
| 5144 | } |
| 5145 | #ifdef FEAT_MBYTE |
| 5146 | if (has_mbyte) |
| 5147 | foundcol = curwin->w_cursor.col |
| 5148 | + (*mb_ptr2len)(ml_get_cursor()); |
| 5149 | else |
| 5150 | #endif |
| 5151 | foundcol = curwin->w_cursor.col + 1; |
| 5152 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 5153 | break; |
| 5154 | } |
| 5155 | #ifdef FEAT_MBYTE |
| 5156 | else if (cc >= 0x100 && fo_multibyte |
| 5157 | && curwin->w_cursor.col <= (colnr_T)wantcol) |
| 5158 | { |
| 5159 | /* Break after or before a multi-byte character. */ |
| 5160 | foundcol = curwin->w_cursor.col; |
| 5161 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 5162 | foundcol += (*mb_char2len)(cc); |
| 5163 | end_foundcol = foundcol; |
| 5164 | break; |
| 5165 | } |
| 5166 | #endif |
| 5167 | if (curwin->w_cursor.col == 0) |
| 5168 | break; |
| 5169 | dec_cursor(); |
| 5170 | } |
| 5171 | |
| 5172 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 5173 | { |
| 5174 | curwin->w_cursor.col = startcol; |
| 5175 | break; |
| 5176 | } |
| 5177 | |
| 5178 | /* Going to break the line, remove any "$" now. */ |
| 5179 | undisplay_dollar(); |
| 5180 | |
| 5181 | /* |
| 5182 | * Offset between cursor position and line break is used by replace |
| 5183 | * stack functions. VREPLACE does not use this, and backspaces |
| 5184 | * over the text instead. |
| 5185 | */ |
| 5186 | #ifdef FEAT_VREPLACE |
| 5187 | if (State & VREPLACE_FLAG) |
| 5188 | orig_col = startcol; /* Will start backspacing from here */ |
| 5189 | else |
| 5190 | #endif |
| 5191 | replace_offset = startcol - end_foundcol - 1; |
| 5192 | |
| 5193 | /* |
| 5194 | * adjust startcol for spaces that will be deleted and |
| 5195 | * characters that will remain on top line |
| 5196 | */ |
| 5197 | curwin->w_cursor.col = foundcol; |
| 5198 | while (cc = gchar_cursor(), WHITECHAR(cc)) |
| 5199 | inc_cursor(); |
| 5200 | startcol -= curwin->w_cursor.col; |
| 5201 | if (startcol < 0) |
| 5202 | startcol = 0; |
| 5203 | |
| 5204 | #ifdef FEAT_VREPLACE |
| 5205 | if (State & VREPLACE_FLAG) |
| 5206 | { |
| 5207 | /* |
| 5208 | * In VREPLACE mode, we will backspace over the text to be |
| 5209 | * wrapped, so save a copy now to put on the next line. |
| 5210 | */ |
| 5211 | saved_text = vim_strsave(ml_get_cursor()); |
| 5212 | curwin->w_cursor.col = orig_col; |
| 5213 | if (saved_text == NULL) |
| 5214 | break; /* Can't do it, out of memory */ |
| 5215 | saved_text[startcol] = NUL; |
| 5216 | |
| 5217 | /* Backspace over characters that will move to the next line */ |
| 5218 | if (!fo_white_par) |
| 5219 | backspace_until_column(foundcol); |
| 5220 | } |
| 5221 | else |
| 5222 | #endif |
| 5223 | { |
| 5224 | /* put cursor after pos. to break line */ |
| 5225 | if (!fo_white_par) |
| 5226 | curwin->w_cursor.col = foundcol; |
| 5227 | } |
| 5228 | |
| 5229 | /* |
| 5230 | * Split the line just before the margin. |
| 5231 | * Only insert/delete lines, but don't really redraw the window. |
| 5232 | */ |
| 5233 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 5234 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 5235 | #ifdef FEAT_COMMENTS |
| 5236 | + (do_comments ? OPENLINE_DO_COM : 0) |
| 5237 | #endif |
| 5238 | , old_indent); |
| 5239 | old_indent = 0; |
| 5240 | |
| 5241 | replace_offset = 0; |
| 5242 | if (first_line) |
| 5243 | { |
| 5244 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
| 5245 | second_indent = get_number_indent(curwin->w_cursor.lnum -1); |
| 5246 | if (second_indent >= 0) |
| 5247 | { |
| 5248 | #ifdef FEAT_VREPLACE |
| 5249 | if (State & VREPLACE_FLAG) |
| 5250 | change_indent(INDENT_SET, second_indent, FALSE, NUL); |
| 5251 | else |
| 5252 | #endif |
| 5253 | (void)set_indent(second_indent, SIN_CHANGED); |
| 5254 | } |
| 5255 | first_line = FALSE; |
| 5256 | } |
| 5257 | |
| 5258 | #ifdef FEAT_VREPLACE |
| 5259 | if (State & VREPLACE_FLAG) |
| 5260 | { |
| 5261 | /* |
| 5262 | * In VREPLACE mode we have backspaced over the text to be |
| 5263 | * moved, now we re-insert it into the new line. |
| 5264 | */ |
| 5265 | ins_bytes(saved_text); |
| 5266 | vim_free(saved_text); |
| 5267 | } |
| 5268 | else |
| 5269 | #endif |
| 5270 | { |
| 5271 | /* |
| 5272 | * Check if cursor is not past the NUL off the line, cindent |
| 5273 | * may have added or removed indent. |
| 5274 | */ |
| 5275 | curwin->w_cursor.col += startcol; |
| 5276 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 5277 | if (curwin->w_cursor.col > len) |
| 5278 | curwin->w_cursor.col = len; |
| 5279 | } |
| 5280 | |
| 5281 | haveto_redraw = TRUE; |
| 5282 | #ifdef FEAT_CINDENT |
| 5283 | can_cindent = TRUE; |
| 5284 | #endif |
| 5285 | /* moved the cursor, don't autoindent or cindent now */ |
| 5286 | did_ai = FALSE; |
| 5287 | #ifdef FEAT_SMARTINDENT |
| 5288 | did_si = FALSE; |
| 5289 | can_si = FALSE; |
| 5290 | can_si_back = FALSE; |
| 5291 | #endif |
| 5292 | line_breakcheck(); |
| 5293 | } |
| 5294 | |
| 5295 | if (save_char != NUL) /* put back space after cursor */ |
| 5296 | pchar_cursor(save_char); |
| 5297 | |
| 5298 | if (!format_only && haveto_redraw) |
| 5299 | { |
| 5300 | update_topline(); |
| 5301 | redraw_curbuf_later(VALID); |
| 5302 | } |
| 5303 | } |
| 5304 | |
| 5305 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5306 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 5307 | * 'a' flag format from the current line until the end of the paragraph. |
| 5308 | * Keep the cursor at the same position relative to the text. |
| 5309 | * The caller must have saved the cursor line for undo, following ones will be |
| 5310 | * saved here. |
| 5311 | */ |
| 5312 | void |
| 5313 | auto_format(trailblank, prev_line) |
| 5314 | int trailblank; /* when TRUE also format with trailing blank */ |
| 5315 | int prev_line; /* may start in previous line */ |
| 5316 | { |
| 5317 | pos_T pos; |
| 5318 | colnr_T len; |
| 5319 | char_u *old; |
| 5320 | char_u *new, *pnew; |
| 5321 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5322 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5323 | |
| 5324 | if (!has_format_option(FO_AUTO)) |
| 5325 | return; |
| 5326 | |
| 5327 | pos = curwin->w_cursor; |
| 5328 | old = ml_get_curline(); |
| 5329 | |
| 5330 | /* may remove added space */ |
| 5331 | check_auto_format(FALSE); |
| 5332 | |
| 5333 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 5334 | * user might insert normal text next. Also skip formatting when "1" is |
| 5335 | * in 'formatoptions' and there is a single character before the cursor. |
| 5336 | * Otherwise the line would be broken and when typing another non-white |
| 5337 | * next they are not joined back together. */ |
| 5338 | wasatend = (pos.col == STRLEN(old)); |
| 5339 | if (*old != NUL && !trailblank && wasatend) |
| 5340 | { |
| 5341 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5342 | cc = gchar_cursor(); |
| 5343 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 5344 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5345 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5346 | cc = gchar_cursor(); |
| 5347 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5348 | { |
| 5349 | curwin->w_cursor = pos; |
| 5350 | return; |
| 5351 | } |
| 5352 | curwin->w_cursor = pos; |
| 5353 | } |
| 5354 | |
| 5355 | #ifdef FEAT_COMMENTS |
| 5356 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 5357 | * comments. */ |
| 5358 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
| 5359 | && get_leader_len(old, NULL, FALSE) == 0) |
| 5360 | return; |
| 5361 | #endif |
| 5362 | |
| 5363 | /* |
| 5364 | * May start formatting in a previous line, so that after "x" a word is |
| 5365 | * moved to the previous line if it fits there now. Only when this is not |
| 5366 | * the start of a paragraph. |
| 5367 | */ |
| 5368 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 5369 | { |
| 5370 | --curwin->w_cursor.lnum; |
| 5371 | if (u_save_cursor() == FAIL) |
| 5372 | return; |
| 5373 | } |
| 5374 | |
| 5375 | /* |
| 5376 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 5377 | * be adjusted for the text formatting. |
| 5378 | */ |
| 5379 | saved_cursor = pos; |
| 5380 | format_lines((linenr_T)-1); |
| 5381 | curwin->w_cursor = saved_cursor; |
| 5382 | saved_cursor.lnum = 0; |
| 5383 | |
| 5384 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 5385 | { |
| 5386 | /* "cannot happen" */ |
| 5387 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 5388 | coladvance((colnr_T)MAXCOL); |
| 5389 | } |
| 5390 | else |
| 5391 | check_cursor_col(); |
| 5392 | |
| 5393 | /* Insert mode: If the cursor is now after the end of the line while it |
| 5394 | * previously wasn't, the line was broken. Because of the rule above we |
| 5395 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 5396 | * formatted. */ |
| 5397 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 5398 | { |
| 5399 | new = ml_get_curline(); |
| 5400 | len = STRLEN(new); |
| 5401 | if (curwin->w_cursor.col == len) |
| 5402 | { |
| 5403 | pnew = vim_strnsave(new, len + 2); |
| 5404 | pnew[len] = ' '; |
| 5405 | pnew[len + 1] = NUL; |
| 5406 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 5407 | /* remove the space later */ |
| 5408 | did_add_space = TRUE; |
| 5409 | } |
| 5410 | else |
| 5411 | /* may remove added space */ |
| 5412 | check_auto_format(FALSE); |
| 5413 | } |
| 5414 | |
| 5415 | check_cursor(); |
| 5416 | } |
| 5417 | |
| 5418 | /* |
| 5419 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 5420 | * delete it now. The space must be under the cursor, just after the insert |
| 5421 | * position. |
| 5422 | */ |
| 5423 | static void |
| 5424 | check_auto_format(end_insert) |
| 5425 | int end_insert; /* TRUE when ending Insert mode */ |
| 5426 | { |
| 5427 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5428 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5429 | |
| 5430 | if (did_add_space) |
| 5431 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5432 | cc = gchar_cursor(); |
| 5433 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5434 | /* Somehow the space was removed already. */ |
| 5435 | did_add_space = FALSE; |
| 5436 | else |
| 5437 | { |
| 5438 | if (!end_insert) |
| 5439 | { |
| 5440 | inc_cursor(); |
| 5441 | c = gchar_cursor(); |
| 5442 | dec_cursor(); |
| 5443 | } |
| 5444 | if (c != NUL) |
| 5445 | { |
| 5446 | /* The space is no longer at the end of the line, delete it. */ |
| 5447 | del_char(FALSE); |
| 5448 | did_add_space = FALSE; |
| 5449 | } |
| 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | |
| 5454 | /* |
| 5455 | * Find out textwidth to be used for formatting: |
| 5456 | * if 'textwidth' option is set, use it |
| 5457 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 5458 | * if invalid value, use 0. |
| 5459 | * Set default to window width (maximum 79) for "gq" operator. |
| 5460 | */ |
| 5461 | int |
| 5462 | comp_textwidth(ff) |
| 5463 | int ff; /* force formatting (for "Q" command) */ |
| 5464 | { |
| 5465 | int textwidth; |
| 5466 | |
| 5467 | textwidth = curbuf->b_p_tw; |
| 5468 | if (textwidth == 0 && curbuf->b_p_wm) |
| 5469 | { |
| 5470 | /* The width is the window width minus 'wrapmargin' minus all the |
| 5471 | * things that add to the margin. */ |
| 5472 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 5473 | #ifdef FEAT_CMDWIN |
| 5474 | if (cmdwin_type != 0) |
| 5475 | textwidth -= 1; |
| 5476 | #endif |
| 5477 | #ifdef FEAT_FOLDING |
| 5478 | textwidth -= curwin->w_p_fdc; |
| 5479 | #endif |
| 5480 | #ifdef FEAT_SIGNS |
| 5481 | if (curwin->w_buffer->b_signlist != NULL |
| 5482 | # ifdef FEAT_NETBEANS_INTG |
| 5483 | || usingNetbeans |
| 5484 | # endif |
| 5485 | ) |
| 5486 | textwidth -= 1; |
| 5487 | #endif |
| 5488 | if (curwin->w_p_nu) |
| 5489 | textwidth -= 8; |
| 5490 | } |
| 5491 | if (textwidth < 0) |
| 5492 | textwidth = 0; |
| 5493 | if (ff && textwidth == 0) |
| 5494 | { |
| 5495 | textwidth = W_WIDTH(curwin) - 1; |
| 5496 | if (textwidth > 79) |
| 5497 | textwidth = 79; |
| 5498 | } |
| 5499 | return textwidth; |
| 5500 | } |
| 5501 | |
| 5502 | /* |
| 5503 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 5504 | */ |
| 5505 | static void |
| 5506 | redo_literal(c) |
| 5507 | int c; |
| 5508 | { |
| 5509 | char_u buf[10]; |
| 5510 | |
| 5511 | /* Only digits need special treatment. Translate them into a string of |
| 5512 | * three digits. */ |
| 5513 | if (VIM_ISDIGIT(c)) |
| 5514 | { |
| 5515 | sprintf((char *)buf, "%03d", c); |
| 5516 | AppendToRedobuff(buf); |
| 5517 | } |
| 5518 | else |
| 5519 | AppendCharToRedobuff(c); |
| 5520 | } |
| 5521 | |
| 5522 | /* |
| 5523 | * 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] | 5524 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | */ |
| 5526 | static void |
| 5527 | start_arrow(end_insert_pos) |
| 5528 | pos_T *end_insert_pos; |
| 5529 | { |
| 5530 | if (!arrow_used) /* something has been inserted */ |
| 5531 | { |
| 5532 | AppendToRedobuff(ESC_STR); |
| 5533 | stop_insert(end_insert_pos, FALSE); |
| 5534 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 5535 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5536 | #ifdef FEAT_SYN_HL |
| 5537 | check_spell_redraw(); |
| 5538 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5541 | #ifdef FEAT_SYN_HL |
| 5542 | /* |
| 5543 | * If we skipped highlighting word at cursor, do it now. |
| 5544 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 5545 | */ |
| 5546 | static void |
| 5547 | check_spell_redraw() |
| 5548 | { |
| 5549 | if (spell_redraw_lnum != 0) |
| 5550 | { |
| 5551 | linenr_T lnum = spell_redraw_lnum; |
| 5552 | |
| 5553 | spell_redraw_lnum = 0; |
| 5554 | redrawWinline(lnum, FALSE); |
| 5555 | } |
| 5556 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 5557 | |
| 5558 | /* |
| 5559 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 5560 | * spelled word, if there is one. |
| 5561 | */ |
| 5562 | static void |
| 5563 | spell_back_to_badword() |
| 5564 | { |
| 5565 | pos_T tpos = curwin->w_cursor; |
| 5566 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 5567 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 5568 | if (curwin->w_cursor.col != tpos.col) |
| 5569 | start_arrow(&tpos); |
| 5570 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5571 | #endif |
| 5572 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5573 | /* |
| 5574 | * stop_arrow() is called before a change is made in insert mode. |
| 5575 | * If an arrow key has been used, start a new insertion. |
| 5576 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 5577 | */ |
| 5578 | int |
| 5579 | stop_arrow() |
| 5580 | { |
| 5581 | if (arrow_used) |
| 5582 | { |
| 5583 | if (u_save_cursor() == OK) |
| 5584 | { |
| 5585 | arrow_used = FALSE; |
| 5586 | ins_need_undo = FALSE; |
| 5587 | } |
| 5588 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
| 5589 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 5590 | ai_col = 0; |
| 5591 | #ifdef FEAT_VREPLACE |
| 5592 | if (State & VREPLACE_FLAG) |
| 5593 | { |
| 5594 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 5595 | vr_lines_changed = 1; |
| 5596 | } |
| 5597 | #endif |
| 5598 | ResetRedobuff(); |
| 5599 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 5600 | new_insert_skip = 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5601 | } |
| 5602 | else if (ins_need_undo) |
| 5603 | { |
| 5604 | if (u_save_cursor() == OK) |
| 5605 | ins_need_undo = FALSE; |
| 5606 | } |
| 5607 | |
| 5608 | #ifdef FEAT_FOLDING |
| 5609 | /* Always open fold at the cursor line when inserting something. */ |
| 5610 | foldOpenCursor(); |
| 5611 | #endif |
| 5612 | |
| 5613 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 5614 | } |
| 5615 | |
| 5616 | /* |
| 5617 | * do a few things to stop inserting |
| 5618 | */ |
| 5619 | static void |
| 5620 | stop_insert(end_insert_pos, esc) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5621 | pos_T *end_insert_pos; /* where insert ended */ |
| 5622 | int esc; /* called by ins_esc() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5623 | { |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5624 | int cc; |
| 5625 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5626 | |
| 5627 | stop_redo_ins(); |
| 5628 | replace_flush(); /* abandon replace stack */ |
| 5629 | |
| 5630 | /* |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5631 | * Save the inserted text for later redo with ^@ and CTRL-A. |
| 5632 | * Don't do it when "restart_edit" was set and nothing was inserted, |
| 5633 | * otherwise CTRL-O w and then <Left> will clear "last_insert". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5635 | ptr = get_inserted(); |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 5636 | if (did_restart_edit == 0 || (ptr != NULL |
| 5637 | && (int)STRLEN(ptr) > new_insert_skip)) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 5638 | { |
| 5639 | vim_free(last_insert); |
| 5640 | last_insert = ptr; |
| 5641 | last_insert_skip = new_insert_skip; |
| 5642 | } |
| 5643 | else |
| 5644 | vim_free(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5645 | |
| 5646 | if (!arrow_used) |
| 5647 | { |
| 5648 | /* Auto-format now. It may seem strange to do this when stopping an |
| 5649 | * insertion (or moving the cursor), but it's required when appending |
| 5650 | * a line and having it end in a space. But only do it when something |
| 5651 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5652 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5653 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5654 | pos_T tpos = curwin->w_cursor; |
| 5655 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5656 | /* When the cursor is at the end of the line after a space the |
| 5657 | * formatting will move it to the following word. Avoid that by |
| 5658 | * moving the cursor onto the space. */ |
| 5659 | cc = 'x'; |
| 5660 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 5661 | { |
| 5662 | dec_cursor(); |
| 5663 | cc = gchar_cursor(); |
| 5664 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5665 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5666 | } |
| 5667 | |
| 5668 | auto_format(TRUE, FALSE); |
| 5669 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5670 | if (vim_iswhite(cc)) |
| 5671 | { |
| 5672 | if (gchar_cursor() != NUL) |
| 5673 | inc_cursor(); |
| 5674 | #ifdef FEAT_VIRTUALEDIT |
| 5675 | /* If the cursor is still at the same character, also keep |
| 5676 | * the "coladd". */ |
| 5677 | if (gchar_cursor() == NUL |
| 5678 | && curwin->w_cursor.lnum == tpos.lnum |
| 5679 | && curwin->w_cursor.col == tpos.col) |
| 5680 | curwin->w_cursor.coladd = tpos.coladd; |
| 5681 | #endif |
| 5682 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
| 5685 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 5686 | check_auto_format(TRUE); |
| 5687 | |
| 5688 | /* 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] | 5689 | * of the line, and put the cursor back. |
| 5690 | * Do this when ESC was used or moving the cursor up/down. */ |
| 5691 | if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
| 5692 | && curwin->w_cursor.lnum != end_insert_pos->lnum))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5693 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5694 | pos_T tpos = curwin->w_cursor; |
| 5695 | |
| 5696 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5697 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 5698 | --curwin->w_cursor.col; |
| 5699 | while (cc = gchar_cursor(), vim_iswhite(cc)) |
| 5700 | (void)del_char(TRUE); |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 5701 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 5702 | curwin->w_cursor = tpos; |
| 5703 | else if (cc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5704 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 5705 | |
| 5706 | #ifdef FEAT_VISUAL |
| 5707 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 5708 | * deleted characters. */ |
| 5709 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 5710 | { |
| 5711 | cc = STRLEN(ml_get_curline()); |
| 5712 | if (VIsual.col > (colnr_T)cc) |
| 5713 | { |
| 5714 | VIsual.col = cc; |
| 5715 | # ifdef FEAT_VIRTUALEDIT |
| 5716 | VIsual.coladd = 0; |
| 5717 | # endif |
| 5718 | } |
| 5719 | } |
| 5720 | #endif |
| 5721 | } |
| 5722 | } |
| 5723 | did_ai = FALSE; |
| 5724 | #ifdef FEAT_SMARTINDENT |
| 5725 | did_si = FALSE; |
| 5726 | can_si = FALSE; |
| 5727 | can_si_back = FALSE; |
| 5728 | #endif |
| 5729 | |
| 5730 | /* set '[ and '] to the inserted text */ |
| 5731 | curbuf->b_op_start = Insstart; |
| 5732 | curbuf->b_op_end = *end_insert_pos; |
| 5733 | } |
| 5734 | |
| 5735 | /* |
| 5736 | * Set the last inserted text to a single character. |
| 5737 | * Used for the replace command. |
| 5738 | */ |
| 5739 | void |
| 5740 | set_last_insert(c) |
| 5741 | int c; |
| 5742 | { |
| 5743 | char_u *s; |
| 5744 | |
| 5745 | vim_free(last_insert); |
| 5746 | #ifdef FEAT_MBYTE |
| 5747 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
| 5748 | #else |
| 5749 | last_insert = alloc(6); |
| 5750 | #endif |
| 5751 | if (last_insert != NULL) |
| 5752 | { |
| 5753 | s = last_insert; |
| 5754 | /* Use the CTRL-V only when entering a special char */ |
| 5755 | if (c < ' ' || c == DEL) |
| 5756 | *s++ = Ctrl_V; |
| 5757 | s = add_char2buf(c, s); |
| 5758 | *s++ = ESC; |
| 5759 | *s++ = NUL; |
| 5760 | last_insert_skip = 0; |
| 5761 | } |
| 5762 | } |
| 5763 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 5764 | #if defined(EXITFREE) || defined(PROTO) |
| 5765 | void |
| 5766 | free_last_insert() |
| 5767 | { |
| 5768 | vim_free(last_insert); |
| 5769 | last_insert = NULL; |
| 5770 | } |
| 5771 | #endif |
| 5772 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5773 | /* |
| 5774 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 5775 | * and CSI. Handle multi-byte characters. |
| 5776 | * Returns a pointer to after the added bytes. |
| 5777 | */ |
| 5778 | char_u * |
| 5779 | add_char2buf(c, s) |
| 5780 | int c; |
| 5781 | char_u *s; |
| 5782 | { |
| 5783 | #ifdef FEAT_MBYTE |
| 5784 | char_u temp[MB_MAXBYTES]; |
| 5785 | int i; |
| 5786 | int len; |
| 5787 | |
| 5788 | len = (*mb_char2bytes)(c, temp); |
| 5789 | for (i = 0; i < len; ++i) |
| 5790 | { |
| 5791 | c = temp[i]; |
| 5792 | #endif |
| 5793 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 5794 | if (c == K_SPECIAL) |
| 5795 | { |
| 5796 | *s++ = K_SPECIAL; |
| 5797 | *s++ = KS_SPECIAL; |
| 5798 | *s++ = KE_FILLER; |
| 5799 | } |
| 5800 | #ifdef FEAT_GUI |
| 5801 | else if (c == CSI) |
| 5802 | { |
| 5803 | *s++ = CSI; |
| 5804 | *s++ = KS_EXTRA; |
| 5805 | *s++ = (int)KE_CSI; |
| 5806 | } |
| 5807 | #endif |
| 5808 | else |
| 5809 | *s++ = c; |
| 5810 | #ifdef FEAT_MBYTE |
| 5811 | } |
| 5812 | #endif |
| 5813 | return s; |
| 5814 | } |
| 5815 | |
| 5816 | /* |
| 5817 | * move cursor to start of line |
| 5818 | * if flags & BL_WHITE move to first non-white |
| 5819 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 5820 | * otherwise keep "curswant" column |
| 5821 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 5822 | */ |
| 5823 | void |
| 5824 | beginline(flags) |
| 5825 | int flags; |
| 5826 | { |
| 5827 | if ((flags & BL_SOL) && !p_sol) |
| 5828 | coladvance(curwin->w_curswant); |
| 5829 | else |
| 5830 | { |
| 5831 | curwin->w_cursor.col = 0; |
| 5832 | #ifdef FEAT_VIRTUALEDIT |
| 5833 | curwin->w_cursor.coladd = 0; |
| 5834 | #endif |
| 5835 | |
| 5836 | if (flags & (BL_WHITE | BL_SOL)) |
| 5837 | { |
| 5838 | char_u *ptr; |
| 5839 | |
| 5840 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 5841 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 5842 | ++curwin->w_cursor.col; |
| 5843 | } |
| 5844 | curwin->w_set_curswant = TRUE; |
| 5845 | } |
| 5846 | } |
| 5847 | |
| 5848 | /* |
| 5849 | * oneright oneleft cursor_down cursor_up |
| 5850 | * |
| 5851 | * Move one char {right,left,down,up}. |
| 5852 | * Doesn't move onto the NUL past the end of the line. |
| 5853 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 5854 | */ |
| 5855 | |
| 5856 | int |
| 5857 | oneright() |
| 5858 | { |
| 5859 | char_u *ptr; |
| 5860 | #ifdef FEAT_MBYTE |
| 5861 | int l; |
| 5862 | #endif |
| 5863 | |
| 5864 | #ifdef FEAT_VIRTUALEDIT |
| 5865 | if (virtual_active()) |
| 5866 | { |
| 5867 | pos_T prevpos = curwin->w_cursor; |
| 5868 | |
| 5869 | /* Adjust for multi-wide char (excluding TAB) */ |
| 5870 | ptr = ml_get_cursor(); |
| 5871 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
| 5872 | #ifdef FEAT_MBYTE |
| 5873 | (*mb_ptr2char)(ptr) |
| 5874 | #else |
| 5875 | *ptr |
| 5876 | #endif |
| 5877 | )) |
| 5878 | ? ptr2cells(ptr) : 1)); |
| 5879 | curwin->w_set_curswant = TRUE; |
| 5880 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 5881 | return (prevpos.col != curwin->w_cursor.col |
| 5882 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 5883 | } |
| 5884 | #endif |
| 5885 | |
| 5886 | ptr = ml_get_cursor(); |
| 5887 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5888 | if (has_mbyte && (l = (*mb_ptr2len)(ptr)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5889 | { |
| 5890 | /* The character under the cursor is a multi-byte character, move |
| 5891 | * several bytes right, but don't end up on the NUL. */ |
| 5892 | if (ptr[l] == NUL) |
| 5893 | return FAIL; |
| 5894 | curwin->w_cursor.col += l; |
| 5895 | } |
| 5896 | else |
| 5897 | #endif |
| 5898 | { |
| 5899 | if (*ptr++ == NUL || *ptr == NUL) |
| 5900 | return FAIL; |
| 5901 | ++curwin->w_cursor.col; |
| 5902 | } |
| 5903 | |
| 5904 | curwin->w_set_curswant = TRUE; |
| 5905 | return OK; |
| 5906 | } |
| 5907 | |
| 5908 | int |
| 5909 | oneleft() |
| 5910 | { |
| 5911 | #ifdef FEAT_VIRTUALEDIT |
| 5912 | if (virtual_active()) |
| 5913 | { |
| 5914 | int width; |
| 5915 | int v = getviscol(); |
| 5916 | |
| 5917 | if (v == 0) |
| 5918 | return FAIL; |
| 5919 | |
| 5920 | # ifdef FEAT_LINEBREAK |
| 5921 | /* We might get stuck on 'showbreak', skip over it. */ |
| 5922 | width = 1; |
| 5923 | for (;;) |
| 5924 | { |
| 5925 | coladvance(v - width); |
| 5926 | /* getviscol() is slow, skip it when 'showbreak' is empty and |
| 5927 | * there are no multi-byte characters */ |
| 5928 | if ((*p_sbr == NUL |
| 5929 | # ifdef FEAT_MBYTE |
| 5930 | && !has_mbyte |
| 5931 | # endif |
| 5932 | ) || getviscol() < v) |
| 5933 | break; |
| 5934 | ++width; |
| 5935 | } |
| 5936 | # else |
| 5937 | coladvance(v - 1); |
| 5938 | # endif |
| 5939 | |
| 5940 | if (curwin->w_cursor.coladd == 1) |
| 5941 | { |
| 5942 | char_u *ptr; |
| 5943 | |
| 5944 | /* Adjust for multi-wide char (not a TAB) */ |
| 5945 | ptr = ml_get_cursor(); |
| 5946 | if (*ptr != TAB && vim_isprintc( |
| 5947 | # ifdef FEAT_MBYTE |
| 5948 | (*mb_ptr2char)(ptr) |
| 5949 | # else |
| 5950 | *ptr |
| 5951 | # endif |
| 5952 | ) && ptr2cells(ptr) > 1) |
| 5953 | curwin->w_cursor.coladd = 0; |
| 5954 | } |
| 5955 | |
| 5956 | curwin->w_set_curswant = TRUE; |
| 5957 | return OK; |
| 5958 | } |
| 5959 | #endif |
| 5960 | |
| 5961 | if (curwin->w_cursor.col == 0) |
| 5962 | return FAIL; |
| 5963 | |
| 5964 | curwin->w_set_curswant = TRUE; |
| 5965 | --curwin->w_cursor.col; |
| 5966 | |
| 5967 | #ifdef FEAT_MBYTE |
| 5968 | /* if the character on the left of the current cursor is a multi-byte |
| 5969 | * character, move to its first byte */ |
| 5970 | if (has_mbyte) |
| 5971 | mb_adjust_cursor(); |
| 5972 | #endif |
| 5973 | return OK; |
| 5974 | } |
| 5975 | |
| 5976 | int |
| 5977 | cursor_up(n, upd_topline) |
| 5978 | long n; |
| 5979 | int upd_topline; /* When TRUE: update topline */ |
| 5980 | { |
| 5981 | linenr_T lnum; |
| 5982 | |
| 5983 | if (n > 0) |
| 5984 | { |
| 5985 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 5986 | /* This fails if the cursor is already in the first line or the count |
| 5987 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 5988 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5989 | return FAIL; |
| 5990 | if (n >= lnum) |
| 5991 | lnum = 1; |
| 5992 | else |
| 5993 | #ifdef FEAT_FOLDING |
| 5994 | if (hasAnyFolding(curwin)) |
| 5995 | { |
| 5996 | /* |
| 5997 | * Count each sequence of folded lines as one logical line. |
| 5998 | */ |
| 5999 | /* go to the the start of the current fold */ |
| 6000 | (void)hasFolding(lnum, &lnum, NULL); |
| 6001 | |
| 6002 | while (n--) |
| 6003 | { |
| 6004 | /* move up one line */ |
| 6005 | --lnum; |
| 6006 | if (lnum <= 1) |
| 6007 | break; |
| 6008 | /* If we entered a fold, move to the beginning, unless in |
| 6009 | * Insert mode or when 'foldopen' contains "all": it will open |
| 6010 | * in a moment. */ |
| 6011 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 6012 | (void)hasFolding(lnum, &lnum, NULL); |
| 6013 | } |
| 6014 | if (lnum < 1) |
| 6015 | lnum = 1; |
| 6016 | } |
| 6017 | else |
| 6018 | #endif |
| 6019 | lnum -= n; |
| 6020 | curwin->w_cursor.lnum = lnum; |
| 6021 | } |
| 6022 | |
| 6023 | /* try to advance to the column we want to be at */ |
| 6024 | coladvance(curwin->w_curswant); |
| 6025 | |
| 6026 | if (upd_topline) |
| 6027 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 6028 | |
| 6029 | return OK; |
| 6030 | } |
| 6031 | |
| 6032 | /* |
| 6033 | * Cursor down a number of logical lines. |
| 6034 | */ |
| 6035 | int |
| 6036 | cursor_down(n, upd_topline) |
| 6037 | long n; |
| 6038 | int upd_topline; /* When TRUE: update topline */ |
| 6039 | { |
| 6040 | linenr_T lnum; |
| 6041 | |
| 6042 | if (n > 0) |
| 6043 | { |
| 6044 | lnum = curwin->w_cursor.lnum; |
| 6045 | #ifdef FEAT_FOLDING |
| 6046 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 6047 | (void)hasFolding(lnum, NULL, &lnum); |
| 6048 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6049 | /* This fails if the cursor is already in the last line or would move |
| 6050 | * beyound the last line and '-' is in 'cpoptions' */ |
| 6051 | if (lnum >= curbuf->b_ml.ml_line_count |
| 6052 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 6053 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6054 | return FAIL; |
| 6055 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 6056 | lnum = curbuf->b_ml.ml_line_count; |
| 6057 | else |
| 6058 | #ifdef FEAT_FOLDING |
| 6059 | if (hasAnyFolding(curwin)) |
| 6060 | { |
| 6061 | linenr_T last; |
| 6062 | |
| 6063 | /* count each sequence of folded lines as one logical line */ |
| 6064 | while (n--) |
| 6065 | { |
| 6066 | if (hasFolding(lnum, NULL, &last)) |
| 6067 | lnum = last + 1; |
| 6068 | else |
| 6069 | ++lnum; |
| 6070 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6071 | break; |
| 6072 | } |
| 6073 | if (lnum > curbuf->b_ml.ml_line_count) |
| 6074 | lnum = curbuf->b_ml.ml_line_count; |
| 6075 | } |
| 6076 | else |
| 6077 | #endif |
| 6078 | lnum += n; |
| 6079 | curwin->w_cursor.lnum = lnum; |
| 6080 | } |
| 6081 | |
| 6082 | /* try to advance to the column we want to be at */ |
| 6083 | coladvance(curwin->w_curswant); |
| 6084 | |
| 6085 | if (upd_topline) |
| 6086 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 6087 | |
| 6088 | return OK; |
| 6089 | } |
| 6090 | |
| 6091 | /* |
| 6092 | * Stuff the last inserted text in the read buffer. |
| 6093 | * Last_insert actually is a copy of the redo buffer, so we |
| 6094 | * first have to remove the command. |
| 6095 | */ |
| 6096 | int |
| 6097 | stuff_inserted(c, count, no_esc) |
| 6098 | int c; /* Command character to be inserted */ |
| 6099 | long count; /* Repeat this many times */ |
| 6100 | int no_esc; /* Don't add an ESC at the end */ |
| 6101 | { |
| 6102 | char_u *esc_ptr; |
| 6103 | char_u *ptr; |
| 6104 | char_u *last_ptr; |
| 6105 | char_u last = NUL; |
| 6106 | |
| 6107 | ptr = get_last_insert(); |
| 6108 | if (ptr == NULL) |
| 6109 | { |
| 6110 | EMSG(_(e_noinstext)); |
| 6111 | return FAIL; |
| 6112 | } |
| 6113 | |
| 6114 | /* may want to stuff the command character, to start Insert mode */ |
| 6115 | if (c != NUL) |
| 6116 | stuffcharReadbuff(c); |
| 6117 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 6118 | *esc_ptr = NUL; /* remove the ESC */ |
| 6119 | |
| 6120 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 6121 | * comes after it OR if it will inserted more than once and "ptr" |
| 6122 | * starts with ^D. -- Acevedo |
| 6123 | */ |
| 6124 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 6125 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 6126 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 6127 | { |
| 6128 | last = *last_ptr; |
| 6129 | *last_ptr = NUL; |
| 6130 | } |
| 6131 | |
| 6132 | do |
| 6133 | { |
| 6134 | stuffReadbuff(ptr); |
| 6135 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 6136 | if (last) |
| 6137 | stuffReadbuff((char_u *)(last == '0' |
| 6138 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 6139 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 6140 | } |
| 6141 | while (--count > 0); |
| 6142 | |
| 6143 | if (last) |
| 6144 | *last_ptr = last; |
| 6145 | |
| 6146 | if (esc_ptr != NULL) |
| 6147 | *esc_ptr = ESC; /* put the ESC back */ |
| 6148 | |
| 6149 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 6150 | if (!no_esc) |
| 6151 | stuffcharReadbuff(ESC); |
| 6152 | |
| 6153 | return OK; |
| 6154 | } |
| 6155 | |
| 6156 | char_u * |
| 6157 | get_last_insert() |
| 6158 | { |
| 6159 | if (last_insert == NULL) |
| 6160 | return NULL; |
| 6161 | return last_insert + last_insert_skip; |
| 6162 | } |
| 6163 | |
| 6164 | /* |
| 6165 | * Get last inserted string, and remove trailing <Esc>. |
| 6166 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 6167 | */ |
| 6168 | char_u * |
| 6169 | get_last_insert_save() |
| 6170 | { |
| 6171 | char_u *s; |
| 6172 | int len; |
| 6173 | |
| 6174 | if (last_insert == NULL) |
| 6175 | return NULL; |
| 6176 | s = vim_strsave(last_insert + last_insert_skip); |
| 6177 | if (s != NULL) |
| 6178 | { |
| 6179 | len = (int)STRLEN(s); |
| 6180 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 6181 | s[len - 1] = NUL; |
| 6182 | } |
| 6183 | return s; |
| 6184 | } |
| 6185 | |
| 6186 | /* |
| 6187 | * Check the word in front of the cursor for an abbreviation. |
| 6188 | * Called when the non-id character "c" has been entered. |
| 6189 | * When an abbreviation is recognized it is removed from the text and |
| 6190 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 6191 | */ |
| 6192 | static int |
| 6193 | echeck_abbr(c) |
| 6194 | int c; |
| 6195 | { |
| 6196 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 6197 | * after moving around with cursor keys. */ |
| 6198 | if (p_paste || no_abbr || arrow_used) |
| 6199 | return FALSE; |
| 6200 | |
| 6201 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 6202 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 6203 | } |
| 6204 | |
| 6205 | /* |
| 6206 | * replace-stack functions |
| 6207 | * |
| 6208 | * When replacing characters, the replaced characters are remembered for each |
| 6209 | * new character. This is used to re-insert the old text when backspacing. |
| 6210 | * |
| 6211 | * There is a NUL headed list of characters for each character that is |
| 6212 | * currently in the file after the insertion point. When BS is used, one NUL |
| 6213 | * headed list is put back for the deleted character. |
| 6214 | * |
| 6215 | * For a newline, there are two NUL headed lists. One contains the characters |
| 6216 | * that the NL replaced. The extra one stores the characters after the cursor |
| 6217 | * that were deleted (always white space). |
| 6218 | * |
| 6219 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 6220 | * character at the end of the stack. If replace_offset is not 0, that many |
| 6221 | * characters will be left on the stack above the newly inserted character. |
| 6222 | */ |
| 6223 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 6224 | static char_u *replace_stack = NULL; |
| 6225 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 6226 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6227 | |
| 6228 | void |
| 6229 | replace_push(c) |
| 6230 | int c; /* character that is replaced (NUL is none) */ |
| 6231 | { |
| 6232 | char_u *p; |
| 6233 | |
| 6234 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 6235 | return; |
| 6236 | if (replace_stack_len <= replace_stack_nr) |
| 6237 | { |
| 6238 | replace_stack_len += 50; |
| 6239 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 6240 | if (p == NULL) /* out of memory */ |
| 6241 | { |
| 6242 | replace_stack_len -= 50; |
| 6243 | return; |
| 6244 | } |
| 6245 | if (replace_stack != NULL) |
| 6246 | { |
| 6247 | mch_memmove(p, replace_stack, |
| 6248 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 6249 | vim_free(replace_stack); |
| 6250 | } |
| 6251 | replace_stack = p; |
| 6252 | } |
| 6253 | p = replace_stack + replace_stack_nr - replace_offset; |
| 6254 | if (replace_offset) |
| 6255 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 6256 | *p = c; |
| 6257 | ++replace_stack_nr; |
| 6258 | } |
| 6259 | |
| 6260 | /* |
| 6261 | * call replace_push(c) with replace_offset set to the first NUL. |
| 6262 | */ |
| 6263 | static void |
| 6264 | replace_push_off(c) |
| 6265 | int c; |
| 6266 | { |
| 6267 | char_u *p; |
| 6268 | |
| 6269 | p = replace_stack + replace_stack_nr; |
| 6270 | for (replace_offset = 1; replace_offset < replace_stack_nr; |
| 6271 | ++replace_offset) |
| 6272 | if (*--p == NUL) |
| 6273 | break; |
| 6274 | replace_push(c); |
| 6275 | replace_offset = 0; |
| 6276 | } |
| 6277 | |
| 6278 | /* |
| 6279 | * Pop one item from the replace stack. |
| 6280 | * return -1 if stack empty |
| 6281 | * return replaced character or NUL otherwise |
| 6282 | */ |
| 6283 | static int |
| 6284 | replace_pop() |
| 6285 | { |
| 6286 | if (replace_stack_nr == 0) |
| 6287 | return -1; |
| 6288 | return (int)replace_stack[--replace_stack_nr]; |
| 6289 | } |
| 6290 | |
| 6291 | /* |
| 6292 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 6293 | * encountered. |
| 6294 | */ |
| 6295 | static void |
| 6296 | replace_join(off) |
| 6297 | int off; /* offset for which NUL to remove */ |
| 6298 | { |
| 6299 | int i; |
| 6300 | |
| 6301 | for (i = replace_stack_nr; --i >= 0; ) |
| 6302 | if (replace_stack[i] == NUL && off-- <= 0) |
| 6303 | { |
| 6304 | --replace_stack_nr; |
| 6305 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 6306 | (size_t)(replace_stack_nr - i)); |
| 6307 | return; |
| 6308 | } |
| 6309 | } |
| 6310 | |
| 6311 | /* |
| 6312 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 6313 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 6314 | */ |
| 6315 | static void |
| 6316 | replace_pop_ins() |
| 6317 | { |
| 6318 | int cc; |
| 6319 | int oldState = State; |
| 6320 | |
| 6321 | State = NORMAL; /* don't want REPLACE here */ |
| 6322 | while ((cc = replace_pop()) > 0) |
| 6323 | { |
| 6324 | #ifdef FEAT_MBYTE |
| 6325 | mb_replace_pop_ins(cc); |
| 6326 | #else |
| 6327 | ins_char(cc); |
| 6328 | #endif |
| 6329 | dec_cursor(); |
| 6330 | } |
| 6331 | State = oldState; |
| 6332 | } |
| 6333 | |
| 6334 | #ifdef FEAT_MBYTE |
| 6335 | /* |
| 6336 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 6337 | * indicates a multi-byte char, pop the other bytes too. |
| 6338 | */ |
| 6339 | static void |
| 6340 | mb_replace_pop_ins(cc) |
| 6341 | int cc; |
| 6342 | { |
| 6343 | int n; |
| 6344 | char_u buf[MB_MAXBYTES]; |
| 6345 | int i; |
| 6346 | int c; |
| 6347 | |
| 6348 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 6349 | { |
| 6350 | buf[0] = cc; |
| 6351 | for (i = 1; i < n; ++i) |
| 6352 | buf[i] = replace_pop(); |
| 6353 | ins_bytes_len(buf, n); |
| 6354 | } |
| 6355 | else |
| 6356 | ins_char(cc); |
| 6357 | |
| 6358 | if (enc_utf8) |
| 6359 | /* Handle composing chars. */ |
| 6360 | for (;;) |
| 6361 | { |
| 6362 | c = replace_pop(); |
| 6363 | if (c == -1) /* stack empty */ |
| 6364 | break; |
| 6365 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 6366 | { |
| 6367 | /* Not a multi-byte char, put it back. */ |
| 6368 | replace_push(c); |
| 6369 | break; |
| 6370 | } |
| 6371 | else |
| 6372 | { |
| 6373 | buf[0] = c; |
| 6374 | for (i = 1; i < n; ++i) |
| 6375 | buf[i] = replace_pop(); |
| 6376 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 6377 | ins_bytes_len(buf, n); |
| 6378 | else |
| 6379 | { |
| 6380 | /* Not a composing char, put it back. */ |
| 6381 | for (i = n - 1; i >= 0; --i) |
| 6382 | replace_push(buf[i]); |
| 6383 | break; |
| 6384 | } |
| 6385 | } |
| 6386 | } |
| 6387 | } |
| 6388 | #endif |
| 6389 | |
| 6390 | /* |
| 6391 | * make the replace stack empty |
| 6392 | * (called when exiting replace mode) |
| 6393 | */ |
| 6394 | static void |
| 6395 | replace_flush() |
| 6396 | { |
| 6397 | vim_free(replace_stack); |
| 6398 | replace_stack = NULL; |
| 6399 | replace_stack_len = 0; |
| 6400 | replace_stack_nr = 0; |
| 6401 | } |
| 6402 | |
| 6403 | /* |
| 6404 | * Handle doing a BS for one character. |
| 6405 | * cc < 0: replace stack empty, just move cursor |
| 6406 | * cc == 0: character was inserted, delete it |
| 6407 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 6408 | * and check for more characters to be put back |
| 6409 | */ |
| 6410 | static void |
| 6411 | replace_do_bs() |
| 6412 | { |
| 6413 | int cc; |
| 6414 | #ifdef FEAT_VREPLACE |
| 6415 | int orig_len = 0; |
| 6416 | int ins_len; |
| 6417 | int orig_vcols = 0; |
| 6418 | colnr_T start_vcol; |
| 6419 | char_u *p; |
| 6420 | int i; |
| 6421 | int vcol; |
| 6422 | #endif |
| 6423 | |
| 6424 | cc = replace_pop(); |
| 6425 | if (cc > 0) |
| 6426 | { |
| 6427 | #ifdef FEAT_VREPLACE |
| 6428 | if (State & VREPLACE_FLAG) |
| 6429 | { |
| 6430 | /* Get the number of screen cells used by the character we are |
| 6431 | * going to delete. */ |
| 6432 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 6433 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 6434 | } |
| 6435 | #endif |
| 6436 | #ifdef FEAT_MBYTE |
| 6437 | if (has_mbyte) |
| 6438 | { |
| 6439 | del_char(FALSE); |
| 6440 | # ifdef FEAT_VREPLACE |
| 6441 | if (State & VREPLACE_FLAG) |
| 6442 | orig_len = STRLEN(ml_get_cursor()); |
| 6443 | # endif |
| 6444 | replace_push(cc); |
| 6445 | } |
| 6446 | else |
| 6447 | #endif |
| 6448 | { |
| 6449 | pchar_cursor(cc); |
| 6450 | #ifdef FEAT_VREPLACE |
| 6451 | if (State & VREPLACE_FLAG) |
| 6452 | orig_len = STRLEN(ml_get_cursor()) - 1; |
| 6453 | #endif |
| 6454 | } |
| 6455 | replace_pop_ins(); |
| 6456 | |
| 6457 | #ifdef FEAT_VREPLACE |
| 6458 | if (State & VREPLACE_FLAG) |
| 6459 | { |
| 6460 | /* Get the number of screen cells used by the inserted characters */ |
| 6461 | p = ml_get_cursor(); |
| 6462 | ins_len = STRLEN(p) - orig_len; |
| 6463 | vcol = start_vcol; |
| 6464 | for (i = 0; i < ins_len; ++i) |
| 6465 | { |
| 6466 | vcol += chartabsize(p + i, vcol); |
| 6467 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6468 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6469 | #endif |
| 6470 | } |
| 6471 | vcol -= start_vcol; |
| 6472 | |
| 6473 | /* Delete spaces that were inserted after the cursor to keep the |
| 6474 | * text aligned. */ |
| 6475 | curwin->w_cursor.col += ins_len; |
| 6476 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 6477 | { |
| 6478 | del_char(FALSE); |
| 6479 | ++orig_vcols; |
| 6480 | } |
| 6481 | curwin->w_cursor.col -= ins_len; |
| 6482 | } |
| 6483 | #endif |
| 6484 | |
| 6485 | /* mark the buffer as changed and prepare for displaying */ |
| 6486 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 6487 | } |
| 6488 | else if (cc == 0) |
| 6489 | (void)del_char(FALSE); |
| 6490 | } |
| 6491 | |
| 6492 | #ifdef FEAT_CINDENT |
| 6493 | /* |
| 6494 | * Return TRUE if C-indenting is on. |
| 6495 | */ |
| 6496 | static int |
| 6497 | cindent_on() |
| 6498 | { |
| 6499 | return (!p_paste && (curbuf->b_p_cin |
| 6500 | # ifdef FEAT_EVAL |
| 6501 | || *curbuf->b_p_inde != NUL |
| 6502 | # endif |
| 6503 | )); |
| 6504 | } |
| 6505 | #endif |
| 6506 | |
| 6507 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 6508 | /* |
| 6509 | * Re-indent the current line, based on the current contents of it and the |
| 6510 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 6511 | * confused what all the part that handles Control-T is doing that I'm not. |
| 6512 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 6513 | */ |
| 6514 | |
| 6515 | void |
| 6516 | fixthisline(get_the_indent) |
| 6517 | int (*get_the_indent) __ARGS((void)); |
| 6518 | { |
| 6519 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0); |
| 6520 | if (linewhite(curwin->w_cursor.lnum)) |
| 6521 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 6522 | } |
| 6523 | |
| 6524 | void |
| 6525 | fix_indent() |
| 6526 | { |
| 6527 | if (p_paste) |
| 6528 | return; |
| 6529 | # ifdef FEAT_LISP |
| 6530 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 6531 | fixthisline(get_lisp_indent); |
| 6532 | # endif |
| 6533 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 6534 | else |
| 6535 | # endif |
| 6536 | # ifdef FEAT_CINDENT |
| 6537 | if (cindent_on()) |
| 6538 | do_c_expr_indent(); |
| 6539 | # endif |
| 6540 | } |
| 6541 | |
| 6542 | #endif |
| 6543 | |
| 6544 | #ifdef FEAT_CINDENT |
| 6545 | /* |
| 6546 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 6547 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 6548 | * when == '!': Only if key is prededed with '!' (don't insert) |
| 6549 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 6550 | * |
| 6551 | * "keytyped" can have a few special values: |
| 6552 | * KEY_OPEN_FORW |
| 6553 | * KEY_OPEN_BACK |
| 6554 | * KEY_COMPLETE just finished completion. |
| 6555 | * |
| 6556 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 6557 | */ |
| 6558 | int |
| 6559 | in_cinkeys(keytyped, when, line_is_empty) |
| 6560 | int keytyped; |
| 6561 | int when; |
| 6562 | int line_is_empty; |
| 6563 | { |
| 6564 | char_u *look; |
| 6565 | int try_match; |
| 6566 | int try_match_word; |
| 6567 | char_u *p; |
| 6568 | char_u *line; |
| 6569 | int icase; |
| 6570 | int i; |
| 6571 | |
| 6572 | #ifdef FEAT_EVAL |
| 6573 | if (*curbuf->b_p_inde != NUL) |
| 6574 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 6575 | else |
| 6576 | #endif |
| 6577 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 6578 | while (*look) |
| 6579 | { |
| 6580 | /* |
| 6581 | * Find out if we want to try a match with this key, depending on |
| 6582 | * 'when' and a '*' or '!' before the key. |
| 6583 | */ |
| 6584 | switch (when) |
| 6585 | { |
| 6586 | case '*': try_match = (*look == '*'); break; |
| 6587 | case '!': try_match = (*look == '!'); break; |
| 6588 | default: try_match = (*look != '*'); break; |
| 6589 | } |
| 6590 | if (*look == '*' || *look == '!') |
| 6591 | ++look; |
| 6592 | |
| 6593 | /* |
| 6594 | * If there is a '0', only accept a match if the line is empty. |
| 6595 | * But may still match when typing last char of a word. |
| 6596 | */ |
| 6597 | if (*look == '0') |
| 6598 | { |
| 6599 | try_match_word = try_match; |
| 6600 | if (!line_is_empty) |
| 6601 | try_match = FALSE; |
| 6602 | ++look; |
| 6603 | } |
| 6604 | else |
| 6605 | try_match_word = FALSE; |
| 6606 | |
| 6607 | /* |
| 6608 | * does it look like a control character? |
| 6609 | */ |
| 6610 | if (*look == '^' |
| 6611 | #ifdef EBCDIC |
| 6612 | && (Ctrl_chr(look[1]) != 0) |
| 6613 | #else |
| 6614 | && look[1] >= '?' && look[1] <= '_' |
| 6615 | #endif |
| 6616 | ) |
| 6617 | { |
| 6618 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 6619 | return TRUE; |
| 6620 | look += 2; |
| 6621 | } |
| 6622 | /* |
| 6623 | * 'o' means "o" command, open forward. |
| 6624 | * 'O' means "O" command, open backward. |
| 6625 | */ |
| 6626 | else if (*look == 'o') |
| 6627 | { |
| 6628 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 6629 | return TRUE; |
| 6630 | ++look; |
| 6631 | } |
| 6632 | else if (*look == 'O') |
| 6633 | { |
| 6634 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 6635 | return TRUE; |
| 6636 | ++look; |
| 6637 | } |
| 6638 | |
| 6639 | /* |
| 6640 | * 'e' means to check for "else" at start of line and just before the |
| 6641 | * cursor. |
| 6642 | */ |
| 6643 | else if (*look == 'e') |
| 6644 | { |
| 6645 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 6646 | { |
| 6647 | p = ml_get_curline(); |
| 6648 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 6649 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 6650 | return TRUE; |
| 6651 | } |
| 6652 | ++look; |
| 6653 | } |
| 6654 | |
| 6655 | /* |
| 6656 | * ':' only causes an indent if it is at the end of a label or case |
| 6657 | * statement, or when it was before typing the ':' (to fix |
| 6658 | * class::method for C++). |
| 6659 | */ |
| 6660 | else if (*look == ':') |
| 6661 | { |
| 6662 | if (try_match && keytyped == ':') |
| 6663 | { |
| 6664 | p = ml_get_curline(); |
| 6665 | if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30)) |
| 6666 | return TRUE; |
| 6667 | if (curwin->w_cursor.col > 2 |
| 6668 | && p[curwin->w_cursor.col - 1] == ':' |
| 6669 | && p[curwin->w_cursor.col - 2] == ':') |
| 6670 | { |
| 6671 | p[curwin->w_cursor.col - 1] = ' '; |
| 6672 | i = (cin_iscase(p) || cin_isscopedecl(p) |
| 6673 | || cin_islabel(30)); |
| 6674 | p = ml_get_curline(); |
| 6675 | p[curwin->w_cursor.col - 1] = ':'; |
| 6676 | if (i) |
| 6677 | return TRUE; |
| 6678 | } |
| 6679 | } |
| 6680 | ++look; |
| 6681 | } |
| 6682 | |
| 6683 | |
| 6684 | /* |
| 6685 | * Is it a key in <>, maybe? |
| 6686 | */ |
| 6687 | else if (*look == '<') |
| 6688 | { |
| 6689 | if (try_match) |
| 6690 | { |
| 6691 | /* |
| 6692 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 6693 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 6694 | * >, *, : and ! keys if they really really want to. |
| 6695 | */ |
| 6696 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 6697 | && keytyped == look[1]) |
| 6698 | return TRUE; |
| 6699 | |
| 6700 | if (keytyped == get_special_key_code(look + 1)) |
| 6701 | return TRUE; |
| 6702 | } |
| 6703 | while (*look && *look != '>') |
| 6704 | look++; |
| 6705 | while (*look == '>') |
| 6706 | look++; |
| 6707 | } |
| 6708 | |
| 6709 | /* |
| 6710 | * Is it a word: "=word"? |
| 6711 | */ |
| 6712 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 6713 | { |
| 6714 | ++look; |
| 6715 | if (*look == '~') |
| 6716 | { |
| 6717 | icase = TRUE; |
| 6718 | ++look; |
| 6719 | } |
| 6720 | else |
| 6721 | icase = FALSE; |
| 6722 | p = vim_strchr(look, ','); |
| 6723 | if (p == NULL) |
| 6724 | p = look + STRLEN(look); |
| 6725 | if ((try_match || try_match_word) |
| 6726 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 6727 | { |
| 6728 | int match = FALSE; |
| 6729 | |
| 6730 | #ifdef FEAT_INS_EXPAND |
| 6731 | if (keytyped == KEY_COMPLETE) |
| 6732 | { |
| 6733 | char_u *s; |
| 6734 | |
| 6735 | /* Just completed a word, check if it starts with "look". |
| 6736 | * search back for the start of a word. */ |
| 6737 | line = ml_get_curline(); |
| 6738 | # ifdef FEAT_MBYTE |
| 6739 | if (has_mbyte) |
| 6740 | { |
| 6741 | char_u *n; |
| 6742 | |
| 6743 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 6744 | { |
| 6745 | n = mb_prevptr(line, s); |
| 6746 | if (!vim_iswordp(n)) |
| 6747 | break; |
| 6748 | } |
| 6749 | } |
| 6750 | else |
| 6751 | # endif |
| 6752 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 6753 | if (!vim_iswordc(s[-1])) |
| 6754 | break; |
| 6755 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 6756 | && (icase |
| 6757 | ? MB_STRNICMP(s, look, p - look) |
| 6758 | : STRNCMP(s, look, p - look)) == 0) |
| 6759 | match = TRUE; |
| 6760 | } |
| 6761 | else |
| 6762 | #endif |
| 6763 | /* TODO: multi-byte */ |
| 6764 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 6765 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 6766 | { |
| 6767 | line = ml_get_cursor(); |
| 6768 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 6769 | || !vim_iswordc(line[-(p - look) - 1])) |
| 6770 | && (icase |
| 6771 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 6772 | : STRNCMP(line - (p - look), look, p - look)) |
| 6773 | == 0) |
| 6774 | match = TRUE; |
| 6775 | } |
| 6776 | if (match && try_match_word && !try_match) |
| 6777 | { |
| 6778 | /* "0=word": Check if there are only blanks before the |
| 6779 | * word. */ |
| 6780 | line = ml_get_curline(); |
| 6781 | if ((int)(skipwhite(line) - line) != |
| 6782 | (int)(curwin->w_cursor.col - (p - look))) |
| 6783 | match = FALSE; |
| 6784 | } |
| 6785 | if (match) |
| 6786 | return TRUE; |
| 6787 | } |
| 6788 | look = p; |
| 6789 | } |
| 6790 | |
| 6791 | /* |
| 6792 | * ok, it's a boring generic character. |
| 6793 | */ |
| 6794 | else |
| 6795 | { |
| 6796 | if (try_match && *look == keytyped) |
| 6797 | return TRUE; |
| 6798 | ++look; |
| 6799 | } |
| 6800 | |
| 6801 | /* |
| 6802 | * Skip over ", ". |
| 6803 | */ |
| 6804 | look = skip_to_option_part(look); |
| 6805 | } |
| 6806 | return FALSE; |
| 6807 | } |
| 6808 | #endif /* FEAT_CINDENT */ |
| 6809 | |
| 6810 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 6811 | /* |
| 6812 | * Map Hebrew keyboard when in hkmap mode. |
| 6813 | */ |
| 6814 | int |
| 6815 | hkmap(c) |
| 6816 | int c; |
| 6817 | { |
| 6818 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 6819 | { |
| 6820 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 6821 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 6822 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 6823 | static char_u map[26] = |
| 6824 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 6825 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 6826 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 6827 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 6828 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 6829 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 6830 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 6831 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 6832 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 6833 | |
| 6834 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 6835 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 6836 | /* '-1'='sofit' */ |
| 6837 | else if (c == 'x') |
| 6838 | return 'X'; |
| 6839 | else if (c == 'q') |
| 6840 | return '\''; /* {geresh}={'} */ |
| 6841 | else if (c == 246) |
| 6842 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 6843 | else if (c == 228) |
| 6844 | return ' '; /* \"a --> ' ' -- / -- */ |
| 6845 | else if (c == 252) |
| 6846 | return ' '; /* \"u --> ' ' -- / -- */ |
| 6847 | #ifdef EBCDIC |
| 6848 | else if (islower(c)) |
| 6849 | #else |
| 6850 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 6851 | * do this the same was as 5.7 and previous, so it works correctly on |
| 6852 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 6853 | * munged and won't work if e.g. searching for Hebrew text. |
| 6854 | */ |
| 6855 | else if (c >= 'a' && c <= 'z') |
| 6856 | #endif |
| 6857 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 6858 | else |
| 6859 | return c; |
| 6860 | } |
| 6861 | else |
| 6862 | { |
| 6863 | switch (c) |
| 6864 | { |
| 6865 | case '`': return ';'; |
| 6866 | case '/': return '.'; |
| 6867 | case '\'': return ','; |
| 6868 | case 'q': return '/'; |
| 6869 | case 'w': return '\''; |
| 6870 | |
| 6871 | /* Hebrew letters - set offset from 'a' */ |
| 6872 | case ',': c = '{'; break; |
| 6873 | case '.': c = 'v'; break; |
| 6874 | case ';': c = 't'; break; |
| 6875 | default: { |
| 6876 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 6877 | |
| 6878 | #ifdef EBCDIC |
| 6879 | /* see note about islower() above */ |
| 6880 | if (!islower(c)) |
| 6881 | #else |
| 6882 | if (c < 'a' || c > 'z') |
| 6883 | #endif |
| 6884 | return c; |
| 6885 | c = str[CharOrdLow(c)]; |
| 6886 | break; |
| 6887 | } |
| 6888 | } |
| 6889 | |
| 6890 | return (int)(CharOrdLow(c) + p_aleph); |
| 6891 | } |
| 6892 | } |
| 6893 | #endif |
| 6894 | |
| 6895 | static void |
| 6896 | ins_reg() |
| 6897 | { |
| 6898 | int need_redraw = FALSE; |
| 6899 | int regname; |
| 6900 | int literally = 0; |
| 6901 | |
| 6902 | /* |
| 6903 | * If we are going to wait for a character, show a '"'. |
| 6904 | */ |
| 6905 | pc_status = PC_STATUS_UNSET; |
| 6906 | if (redrawing() && !char_avail()) |
| 6907 | { |
| 6908 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 6909 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6910 | |
| 6911 | edit_putchar('"', TRUE); |
| 6912 | #ifdef FEAT_CMDL_INFO |
| 6913 | add_to_showcmd_c(Ctrl_R); |
| 6914 | #endif |
| 6915 | } |
| 6916 | |
| 6917 | #ifdef USE_ON_FLY_SCROLL |
| 6918 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 6919 | #endif |
| 6920 | |
| 6921 | /* |
| 6922 | * Don't map the register name. This also prevents the mode message to be |
| 6923 | * deleted when ESC is hit. |
| 6924 | */ |
| 6925 | ++no_mapping; |
| 6926 | regname = safe_vgetc(); |
| 6927 | #ifdef FEAT_LANGMAP |
| 6928 | LANGMAP_ADJUST(regname, TRUE); |
| 6929 | #endif |
| 6930 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 6931 | { |
| 6932 | /* Get a third key for literal register insertion */ |
| 6933 | literally = regname; |
| 6934 | #ifdef FEAT_CMDL_INFO |
| 6935 | add_to_showcmd_c(literally); |
| 6936 | #endif |
| 6937 | regname = safe_vgetc(); |
| 6938 | #ifdef FEAT_LANGMAP |
| 6939 | LANGMAP_ADJUST(regname, TRUE); |
| 6940 | #endif |
| 6941 | } |
| 6942 | --no_mapping; |
| 6943 | |
| 6944 | #ifdef FEAT_EVAL |
| 6945 | /* |
| 6946 | * Don't call u_sync() while getting the expression, |
| 6947 | * evaluating it or giving an error message for it! |
| 6948 | */ |
| 6949 | ++no_u_sync; |
| 6950 | if (regname == '=') |
| 6951 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6952 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6953 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6954 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6955 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6956 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6957 | /* Restore the Input Method. */ |
| 6958 | if (im_on) |
| 6959 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6960 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6961 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 6962 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 6963 | { |
| 6964 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6965 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 6966 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6967 | else |
| 6968 | { |
| 6969 | #endif |
| 6970 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 6971 | { |
| 6972 | /* Append the command to the redo buffer. */ |
| 6973 | AppendCharToRedobuff(Ctrl_R); |
| 6974 | AppendCharToRedobuff(literally); |
| 6975 | AppendCharToRedobuff(regname); |
| 6976 | |
| 6977 | do_put(regname, BACKWARD, 1L, |
| 6978 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 6979 | } |
| 6980 | else if (insert_reg(regname, literally) == FAIL) |
| 6981 | { |
| 6982 | vim_beep(); |
| 6983 | need_redraw = TRUE; /* remove the '"' */ |
| 6984 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 6985 | else if (stop_insert_mode) |
| 6986 | /* When the '=' register was used and a function was invoked that |
| 6987 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 6988 | * insert anything, need to remove the '"' */ |
| 6989 | need_redraw = TRUE; |
| 6990 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6991 | #ifdef FEAT_EVAL |
| 6992 | } |
| 6993 | --no_u_sync; |
| 6994 | #endif |
| 6995 | #ifdef FEAT_CMDL_INFO |
| 6996 | clear_showcmd(); |
| 6997 | #endif |
| 6998 | |
| 6999 | /* If the inserted register is empty, we need to remove the '"' */ |
| 7000 | if (need_redraw || stuff_empty()) |
| 7001 | edit_unputchar(); |
| 7002 | } |
| 7003 | |
| 7004 | /* |
| 7005 | * CTRL-G commands in Insert mode. |
| 7006 | */ |
| 7007 | static void |
| 7008 | ins_ctrl_g() |
| 7009 | { |
| 7010 | int c; |
| 7011 | |
| 7012 | #ifdef FEAT_INS_EXPAND |
| 7013 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 7014 | setcursor(); |
| 7015 | #endif |
| 7016 | |
| 7017 | /* |
| 7018 | * Don't map the second key. This also prevents the mode message to be |
| 7019 | * deleted when ESC is hit. |
| 7020 | */ |
| 7021 | ++no_mapping; |
| 7022 | c = safe_vgetc(); |
| 7023 | --no_mapping; |
| 7024 | switch (c) |
| 7025 | { |
| 7026 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 7027 | case K_UP: |
| 7028 | case Ctrl_K: |
| 7029 | case 'k': ins_up(TRUE); |
| 7030 | break; |
| 7031 | |
| 7032 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 7033 | case K_DOWN: |
| 7034 | case Ctrl_J: |
| 7035 | case 'j': ins_down(TRUE); |
| 7036 | break; |
| 7037 | |
| 7038 | /* CTRL-G u: start new undoable edit */ |
| 7039 | case 'u': u_sync(); |
| 7040 | ins_need_undo = TRUE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 7041 | |
| 7042 | /* Need to reset Insstart, esp. because a BS that joins |
| 7043 | * aline to the previous one must save for undo. */ |
| 7044 | Insstart = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7045 | break; |
| 7046 | |
| 7047 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 7048 | default: vim_beep(); |
| 7049 | } |
| 7050 | } |
| 7051 | |
| 7052 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7053 | * CTRL-^ in Insert mode. |
| 7054 | */ |
| 7055 | static void |
| 7056 | ins_ctrl_hat() |
| 7057 | { |
| 7058 | if (map_to_exists_mode((char_u *)"", LANGMAP)) |
| 7059 | { |
| 7060 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 7061 | if (State & LANGMAP) |
| 7062 | { |
| 7063 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 7064 | State &= ~LANGMAP; |
| 7065 | } |
| 7066 | else |
| 7067 | { |
| 7068 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 7069 | State |= LANGMAP; |
| 7070 | #ifdef USE_IM_CONTROL |
| 7071 | im_set_active(FALSE); |
| 7072 | #endif |
| 7073 | } |
| 7074 | } |
| 7075 | #ifdef USE_IM_CONTROL |
| 7076 | else |
| 7077 | { |
| 7078 | /* There are no ":lmap" mappings, toggle IM */ |
| 7079 | if (im_get_status()) |
| 7080 | { |
| 7081 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 7082 | im_set_active(FALSE); |
| 7083 | } |
| 7084 | else |
| 7085 | { |
| 7086 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 7087 | State &= ~LANGMAP; |
| 7088 | im_set_active(TRUE); |
| 7089 | } |
| 7090 | } |
| 7091 | #endif |
| 7092 | set_iminsert_global(); |
| 7093 | showmode(); |
| 7094 | #ifdef FEAT_GUI |
| 7095 | /* may show different cursor shape or color */ |
| 7096 | if (gui.in_use) |
| 7097 | gui_update_cursor(TRUE, FALSE); |
| 7098 | #endif |
| 7099 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 7100 | /* Show/unshow value of 'keymap' in status lines. */ |
| 7101 | status_redraw_curbuf(); |
| 7102 | #endif |
| 7103 | } |
| 7104 | |
| 7105 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7106 | * Handle ESC in insert mode. |
| 7107 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 7108 | * insert. |
| 7109 | */ |
| 7110 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7111 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7112 | long *count; |
| 7113 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7114 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | { |
| 7116 | int temp; |
| 7117 | static int disabled_redraw = FALSE; |
| 7118 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7119 | #ifdef FEAT_SYN_HL |
| 7120 | check_spell_redraw(); |
| 7121 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7122 | #if defined(FEAT_HANGULIN) |
| 7123 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 7124 | hangul_input_state_set(0); |
| 7125 | # endif |
| 7126 | if (composing_hangul) |
| 7127 | { |
| 7128 | push_raw_key(composing_hangul_buffer, 2); |
| 7129 | composing_hangul = 0; |
| 7130 | } |
| 7131 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7132 | |
| 7133 | temp = curwin->w_cursor.col; |
| 7134 | if (disabled_redraw) |
| 7135 | { |
| 7136 | --RedrawingDisabled; |
| 7137 | disabled_redraw = FALSE; |
| 7138 | } |
| 7139 | if (!arrow_used) |
| 7140 | { |
| 7141 | /* |
| 7142 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7143 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 7144 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7145 | */ |
| 7146 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7147 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7148 | |
| 7149 | /* |
| 7150 | * Repeating insert may take a long time. Check for |
| 7151 | * interrupt now and then. |
| 7152 | */ |
| 7153 | if (*count > 0) |
| 7154 | { |
| 7155 | line_breakcheck(); |
| 7156 | if (got_int) |
| 7157 | *count = 0; |
| 7158 | } |
| 7159 | |
| 7160 | if (--*count > 0) /* repeat what was typed */ |
| 7161 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 7162 | /* Vi repeats the insert without replacing characters. */ |
| 7163 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 7164 | State &= ~REPLACE_FLAG; |
| 7165 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7166 | (void)start_redo_ins(); |
| 7167 | if (cmdchar == 'r' || cmdchar == 'v') |
| 7168 | stuffReadbuff(ESC_STR); /* no ESC in redo buffer */ |
| 7169 | ++RedrawingDisabled; |
| 7170 | disabled_redraw = TRUE; |
| 7171 | return FALSE; /* repeat the insert */ |
| 7172 | } |
| 7173 | stop_insert(&curwin->w_cursor, TRUE); |
| 7174 | undisplay_dollar(); |
| 7175 | } |
| 7176 | |
| 7177 | /* When an autoindent was removed, curswant stays after the |
| 7178 | * indent */ |
| 7179 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 7180 | curwin->w_set_curswant = TRUE; |
| 7181 | |
| 7182 | /* Remember the last Insert position in the '^ mark. */ |
| 7183 | if (!cmdmod.keepjumps) |
| 7184 | curbuf->b_last_insert = curwin->w_cursor; |
| 7185 | |
| 7186 | /* |
| 7187 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7188 | * 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] | 7189 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7190 | if (!nomove |
| 7191 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7192 | #ifdef FEAT_VIRTUALEDIT |
| 7193 | || curwin->w_cursor.coladd > 0 |
| 7194 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7195 | ) |
| 7196 | && (restart_edit == NUL |
| 7197 | || (gchar_cursor() == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7198 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7199 | && !VIsual_active |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7200 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7201 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7202 | #ifdef FEAT_RIGHTLEFT |
| 7203 | && !revins_on |
| 7204 | #endif |
| 7205 | ) |
| 7206 | { |
| 7207 | #ifdef FEAT_VIRTUALEDIT |
| 7208 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 7209 | { |
| 7210 | oneleft(); |
| 7211 | if (restart_edit != NUL) |
| 7212 | ++curwin->w_cursor.coladd; |
| 7213 | } |
| 7214 | else |
| 7215 | #endif |
| 7216 | { |
| 7217 | --curwin->w_cursor.col; |
| 7218 | #ifdef FEAT_MBYTE |
| 7219 | /* Correct cursor for multi-byte character. */ |
| 7220 | if (has_mbyte) |
| 7221 | mb_adjust_cursor(); |
| 7222 | #endif |
| 7223 | } |
| 7224 | } |
| 7225 | |
| 7226 | #ifdef USE_IM_CONTROL |
| 7227 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 7228 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 7229 | * well). */ |
| 7230 | if (!(State & LANGMAP)) |
| 7231 | im_save_status(&curbuf->b_p_iminsert); |
| 7232 | im_set_active(FALSE); |
| 7233 | #endif |
| 7234 | |
| 7235 | State = NORMAL; |
| 7236 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 7237 | changed_cline_bef_curs(); |
| 7238 | |
| 7239 | #ifdef FEAT_MOUSE |
| 7240 | setmouse(); |
| 7241 | #endif |
| 7242 | #ifdef CURSOR_SHAPE |
| 7243 | ui_cursor_shape(); /* may show different cursor shape */ |
| 7244 | #endif |
| 7245 | |
| 7246 | /* |
| 7247 | * When recording or for CTRL-O, need to display the new mode. |
| 7248 | * Otherwise remove the mode message. |
| 7249 | */ |
| 7250 | if (Recording || restart_edit != NUL) |
| 7251 | showmode(); |
| 7252 | else if (p_smd) |
| 7253 | MSG(""); |
| 7254 | |
| 7255 | return TRUE; /* exit Insert mode */ |
| 7256 | } |
| 7257 | |
| 7258 | #ifdef FEAT_RIGHTLEFT |
| 7259 | /* |
| 7260 | * Toggle language: hkmap and revins_on. |
| 7261 | * Move to end of reverse inserted text. |
| 7262 | */ |
| 7263 | static void |
| 7264 | ins_ctrl_() |
| 7265 | { |
| 7266 | if (revins_on && revins_chars && revins_scol >= 0) |
| 7267 | { |
| 7268 | while (gchar_cursor() != NUL && revins_chars--) |
| 7269 | ++curwin->w_cursor.col; |
| 7270 | } |
| 7271 | p_ri = !p_ri; |
| 7272 | revins_on = (State == INSERT && p_ri); |
| 7273 | if (revins_on) |
| 7274 | { |
| 7275 | revins_scol = curwin->w_cursor.col; |
| 7276 | revins_legal++; |
| 7277 | revins_chars = 0; |
| 7278 | undisplay_dollar(); |
| 7279 | } |
| 7280 | else |
| 7281 | revins_scol = -1; |
| 7282 | #ifdef FEAT_FKMAP |
| 7283 | if (p_altkeymap) |
| 7284 | { |
| 7285 | /* |
| 7286 | * to be consistent also for redo command, using '.' |
| 7287 | * set arrow_used to true and stop it - causing to redo |
| 7288 | * characters entered in one mode (normal/reverse insert). |
| 7289 | */ |
| 7290 | arrow_used = TRUE; |
| 7291 | (void)stop_arrow(); |
| 7292 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 7293 | if (p_fkmap && p_ri) |
| 7294 | State = INSERT; |
| 7295 | } |
| 7296 | else |
| 7297 | #endif |
| 7298 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 7299 | showmode(); |
| 7300 | } |
| 7301 | #endif |
| 7302 | |
| 7303 | #ifdef FEAT_VISUAL |
| 7304 | /* |
| 7305 | * If 'keymodel' contains "startsel", may start selection. |
| 7306 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 7307 | */ |
| 7308 | static int |
| 7309 | ins_start_select(c) |
| 7310 | int c; |
| 7311 | { |
| 7312 | if (km_startsel) |
| 7313 | switch (c) |
| 7314 | { |
| 7315 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7316 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7317 | case K_PAGEUP: |
| 7318 | case K_KPAGEUP: |
| 7319 | case K_PAGEDOWN: |
| 7320 | case K_KPAGEDOWN: |
| 7321 | # ifdef MACOS |
| 7322 | case K_LEFT: |
| 7323 | case K_RIGHT: |
| 7324 | case K_UP: |
| 7325 | case K_DOWN: |
| 7326 | case K_END: |
| 7327 | case K_HOME: |
| 7328 | # endif |
| 7329 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 7330 | break; |
| 7331 | /* FALLTHROUGH */ |
| 7332 | case K_S_LEFT: |
| 7333 | case K_S_RIGHT: |
| 7334 | case K_S_UP: |
| 7335 | case K_S_DOWN: |
| 7336 | case K_S_END: |
| 7337 | case K_S_HOME: |
| 7338 | /* Start selection right away, the cursor can move with |
| 7339 | * CTRL-O when beyond the end of the line. */ |
| 7340 | start_selection(); |
| 7341 | |
| 7342 | /* Execute the key in (insert) Select mode. */ |
| 7343 | stuffcharReadbuff(Ctrl_O); |
| 7344 | if (mod_mask) |
| 7345 | { |
| 7346 | char_u buf[4]; |
| 7347 | |
| 7348 | buf[0] = K_SPECIAL; |
| 7349 | buf[1] = KS_MODIFIER; |
| 7350 | buf[2] = mod_mask; |
| 7351 | buf[3] = NUL; |
| 7352 | stuffReadbuff(buf); |
| 7353 | } |
| 7354 | stuffcharReadbuff(c); |
| 7355 | return TRUE; |
| 7356 | } |
| 7357 | return FALSE; |
| 7358 | } |
| 7359 | #endif |
| 7360 | |
| 7361 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7362 | * <Insert> key in Insert mode: toggle insert/remplace mode. |
| 7363 | */ |
| 7364 | static void |
| 7365 | ins_insert(replaceState) |
| 7366 | int replaceState; |
| 7367 | { |
| 7368 | #ifdef FEAT_FKMAP |
| 7369 | if (p_fkmap && p_ri) |
| 7370 | { |
| 7371 | beep_flush(); |
| 7372 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 7373 | return; |
| 7374 | } |
| 7375 | #endif |
| 7376 | |
| 7377 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 7378 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7379 | set_vim_var_string(VV_INSERTMODE, |
| 7380 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7381 | # ifdef FEAT_VREPLACE |
| 7382 | replaceState == VREPLACE ? "v" : |
| 7383 | # endif |
| 7384 | "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 7385 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7386 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 7387 | #endif |
| 7388 | if (State & REPLACE_FLAG) |
| 7389 | State = INSERT | (State & LANGMAP); |
| 7390 | else |
| 7391 | State = replaceState | (State & LANGMAP); |
| 7392 | AppendCharToRedobuff(K_INS); |
| 7393 | showmode(); |
| 7394 | #ifdef CURSOR_SHAPE |
| 7395 | ui_cursor_shape(); /* may show different cursor shape */ |
| 7396 | #endif |
| 7397 | } |
| 7398 | |
| 7399 | /* |
| 7400 | * Pressed CTRL-O in Insert mode. |
| 7401 | */ |
| 7402 | static void |
| 7403 | ins_ctrl_o() |
| 7404 | { |
| 7405 | #ifdef FEAT_VREPLACE |
| 7406 | if (State & VREPLACE_FLAG) |
| 7407 | restart_edit = 'V'; |
| 7408 | else |
| 7409 | #endif |
| 7410 | if (State & REPLACE_FLAG) |
| 7411 | restart_edit = 'R'; |
| 7412 | else |
| 7413 | restart_edit = 'I'; |
| 7414 | #ifdef FEAT_VIRTUALEDIT |
| 7415 | if (virtual_active()) |
| 7416 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 7417 | else |
| 7418 | #endif |
| 7419 | ins_at_eol = (gchar_cursor() == NUL); |
| 7420 | } |
| 7421 | |
| 7422 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7423 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 7424 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
| 7425 | * Always round the indent to 'shiftwith', this is compatible |
| 7426 | * with vi. But vi only supports ^T and ^D after an |
| 7427 | * autoindent, we support it everywhere. |
| 7428 | */ |
| 7429 | static void |
| 7430 | ins_shift(c, lastc) |
| 7431 | int c; |
| 7432 | int lastc; |
| 7433 | { |
| 7434 | if (stop_arrow() == FAIL) |
| 7435 | return; |
| 7436 | AppendCharToRedobuff(c); |
| 7437 | |
| 7438 | /* |
| 7439 | * 0^D and ^^D: remove all indent. |
| 7440 | */ |
| 7441 | if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col) |
| 7442 | { |
| 7443 | --curwin->w_cursor.col; |
| 7444 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 7445 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 7446 | if (State & REPLACE_FLAG) |
| 7447 | replace_pop_ins(); |
| 7448 | if (lastc == '^') |
| 7449 | old_indent = get_indent(); /* remember curr. indent */ |
| 7450 | change_indent(INDENT_SET, 0, TRUE, 0); |
| 7451 | } |
| 7452 | else |
| 7453 | change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0); |
| 7454 | |
| 7455 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 7456 | did_ai = FALSE; |
| 7457 | #ifdef FEAT_SMARTINDENT |
| 7458 | did_si = FALSE; |
| 7459 | can_si = FALSE; |
| 7460 | can_si_back = FALSE; |
| 7461 | #endif |
| 7462 | #ifdef FEAT_CINDENT |
| 7463 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 7464 | #endif |
| 7465 | } |
| 7466 | |
| 7467 | static void |
| 7468 | ins_del() |
| 7469 | { |
| 7470 | int temp; |
| 7471 | |
| 7472 | if (stop_arrow() == FAIL) |
| 7473 | return; |
| 7474 | if (gchar_cursor() == NUL) /* delete newline */ |
| 7475 | { |
| 7476 | temp = curwin->w_cursor.col; |
| 7477 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
| 7478 | || u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 7479 | (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL |
| 7480 | || do_join(FALSE) == FAIL) |
| 7481 | vim_beep(); |
| 7482 | else |
| 7483 | curwin->w_cursor.col = temp; |
| 7484 | } |
| 7485 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 7486 | vim_beep(); |
| 7487 | did_ai = FALSE; |
| 7488 | #ifdef FEAT_SMARTINDENT |
| 7489 | did_si = FALSE; |
| 7490 | can_si = FALSE; |
| 7491 | can_si_back = FALSE; |
| 7492 | #endif |
| 7493 | AppendCharToRedobuff(K_DEL); |
| 7494 | } |
| 7495 | |
| 7496 | /* |
| 7497 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 7498 | * Return TRUE when backspace was actually used. |
| 7499 | */ |
| 7500 | static int |
| 7501 | ins_bs(c, mode, inserted_space_p) |
| 7502 | int c; |
| 7503 | int mode; |
| 7504 | int *inserted_space_p; |
| 7505 | { |
| 7506 | linenr_T lnum; |
| 7507 | int cc; |
| 7508 | int temp = 0; /* init for GCC */ |
| 7509 | colnr_T mincol; |
| 7510 | int did_backspace = FALSE; |
| 7511 | int in_indent; |
| 7512 | int oldState; |
| 7513 | #ifdef FEAT_MBYTE |
| 7514 | int p1, p2; |
| 7515 | #endif |
| 7516 | |
| 7517 | /* |
| 7518 | * can't delete anything in an empty file |
| 7519 | * can't backup past first character in buffer |
| 7520 | * can't backup past starting point unless 'backspace' > 1 |
| 7521 | * can backup to a previous line if 'backspace' == 0 |
| 7522 | */ |
| 7523 | if ( bufempty() |
| 7524 | || ( |
| 7525 | #ifdef FEAT_RIGHTLEFT |
| 7526 | !revins_on && |
| 7527 | #endif |
| 7528 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 7529 | || (!can_bs(BS_START) |
| 7530 | && (arrow_used |
| 7531 | || (curwin->w_cursor.lnum == Insstart.lnum |
| 7532 | && curwin->w_cursor.col <= Insstart.col))) |
| 7533 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 7534 | && curwin->w_cursor.col <= ai_col) |
| 7535 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 7536 | { |
| 7537 | vim_beep(); |
| 7538 | return FALSE; |
| 7539 | } |
| 7540 | |
| 7541 | if (stop_arrow() == FAIL) |
| 7542 | return FALSE; |
| 7543 | in_indent = inindent(0); |
| 7544 | #ifdef FEAT_CINDENT |
| 7545 | if (in_indent) |
| 7546 | can_cindent = FALSE; |
| 7547 | #endif |
| 7548 | #ifdef FEAT_COMMENTS |
| 7549 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 7550 | #endif |
| 7551 | #ifdef FEAT_RIGHTLEFT |
| 7552 | if (revins_on) /* put cursor after last inserted char */ |
| 7553 | inc_cursor(); |
| 7554 | #endif |
| 7555 | |
| 7556 | #ifdef FEAT_VIRTUALEDIT |
| 7557 | /* Virtualedit: |
| 7558 | * BACKSPACE_CHAR eats a virtual space |
| 7559 | * BACKSPACE_WORD eats all coladd |
| 7560 | * BACKSPACE_LINE eats all coladd and keeps going |
| 7561 | */ |
| 7562 | if (curwin->w_cursor.coladd > 0) |
| 7563 | { |
| 7564 | if (mode == BACKSPACE_CHAR) |
| 7565 | { |
| 7566 | --curwin->w_cursor.coladd; |
| 7567 | return TRUE; |
| 7568 | } |
| 7569 | if (mode == BACKSPACE_WORD) |
| 7570 | { |
| 7571 | curwin->w_cursor.coladd = 0; |
| 7572 | return TRUE; |
| 7573 | } |
| 7574 | curwin->w_cursor.coladd = 0; |
| 7575 | } |
| 7576 | #endif |
| 7577 | |
| 7578 | /* |
| 7579 | * delete newline! |
| 7580 | */ |
| 7581 | if (curwin->w_cursor.col == 0) |
| 7582 | { |
| 7583 | lnum = Insstart.lnum; |
| 7584 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7585 | #ifdef FEAT_RIGHTLEFT |
| 7586 | || revins_on |
| 7587 | #endif |
| 7588 | ) |
| 7589 | { |
| 7590 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 7591 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 7592 | return FALSE; |
| 7593 | --Insstart.lnum; |
| 7594 | Insstart.col = MAXCOL; |
| 7595 | } |
| 7596 | /* |
| 7597 | * In replace mode: |
| 7598 | * cc < 0: NL was inserted, delete it |
| 7599 | * cc >= 0: NL was replaced, put original characters back |
| 7600 | */ |
| 7601 | cc = -1; |
| 7602 | if (State & REPLACE_FLAG) |
| 7603 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 7604 | /* |
| 7605 | * In replace mode, in the line we started replacing, we only move the |
| 7606 | * cursor. |
| 7607 | */ |
| 7608 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 7609 | { |
| 7610 | dec_cursor(); |
| 7611 | } |
| 7612 | else |
| 7613 | { |
| 7614 | #ifdef FEAT_VREPLACE |
| 7615 | if (!(State & VREPLACE_FLAG) |
| 7616 | || curwin->w_cursor.lnum > orig_line_count) |
| 7617 | #endif |
| 7618 | { |
| 7619 | temp = gchar_cursor(); /* remember current char */ |
| 7620 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 7621 | |
| 7622 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 7623 | * the end of the line, otherwise the line will be broken |
| 7624 | * again when auto-formatting. */ |
| 7625 | if (has_format_option(FO_AUTO) |
| 7626 | && has_format_option(FO_WHITE_PAR)) |
| 7627 | { |
| 7628 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 7629 | TRUE); |
| 7630 | int len; |
| 7631 | |
| 7632 | len = STRLEN(ptr); |
| 7633 | if (len > 0 && ptr[len - 1] == ' ') |
| 7634 | ptr[len - 1] = NUL; |
| 7635 | } |
| 7636 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7637 | (void)do_join(FALSE); |
| 7638 | if (temp == NUL && gchar_cursor() != NUL) |
| 7639 | inc_cursor(); |
| 7640 | } |
| 7641 | #ifdef FEAT_VREPLACE |
| 7642 | else |
| 7643 | dec_cursor(); |
| 7644 | #endif |
| 7645 | |
| 7646 | /* |
| 7647 | * In REPLACE mode we have to put back the text that was replaced |
| 7648 | * by the NL. On the replace stack is first a NUL-terminated |
| 7649 | * sequence of characters that were deleted and then the |
| 7650 | * characters that NL replaced. |
| 7651 | */ |
| 7652 | if (State & REPLACE_FLAG) |
| 7653 | { |
| 7654 | /* |
| 7655 | * Do the next ins_char() in NORMAL state, to |
| 7656 | * prevent ins_char() from replacing characters and |
| 7657 | * avoiding showmatch(). |
| 7658 | */ |
| 7659 | oldState = State; |
| 7660 | State = NORMAL; |
| 7661 | /* |
| 7662 | * restore characters (blanks) deleted after cursor |
| 7663 | */ |
| 7664 | while (cc > 0) |
| 7665 | { |
| 7666 | temp = curwin->w_cursor.col; |
| 7667 | #ifdef FEAT_MBYTE |
| 7668 | mb_replace_pop_ins(cc); |
| 7669 | #else |
| 7670 | ins_char(cc); |
| 7671 | #endif |
| 7672 | curwin->w_cursor.col = temp; |
| 7673 | cc = replace_pop(); |
| 7674 | } |
| 7675 | /* restore the characters that NL replaced */ |
| 7676 | replace_pop_ins(); |
| 7677 | State = oldState; |
| 7678 | } |
| 7679 | } |
| 7680 | did_ai = FALSE; |
| 7681 | } |
| 7682 | else |
| 7683 | { |
| 7684 | /* |
| 7685 | * Delete character(s) before the cursor. |
| 7686 | */ |
| 7687 | #ifdef FEAT_RIGHTLEFT |
| 7688 | if (revins_on) /* put cursor on last inserted char */ |
| 7689 | dec_cursor(); |
| 7690 | #endif |
| 7691 | mincol = 0; |
| 7692 | /* keep indent */ |
| 7693 | if (mode == BACKSPACE_LINE && curbuf->b_p_ai |
| 7694 | #ifdef FEAT_RIGHTLEFT |
| 7695 | && !revins_on |
| 7696 | #endif |
| 7697 | ) |
| 7698 | { |
| 7699 | temp = curwin->w_cursor.col; |
| 7700 | beginline(BL_WHITE); |
| 7701 | if (curwin->w_cursor.col < (colnr_T)temp) |
| 7702 | mincol = curwin->w_cursor.col; |
| 7703 | curwin->w_cursor.col = temp; |
| 7704 | } |
| 7705 | |
| 7706 | /* |
| 7707 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 7708 | */ |
| 7709 | if ( mode == BACKSPACE_CHAR |
| 7710 | && ((p_sta && in_indent) |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7711 | || (curbuf->b_p_sts != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7712 | && (*(ml_get_cursor() - 1) == TAB |
| 7713 | || (*(ml_get_cursor() - 1) == ' ' |
| 7714 | && (!*inserted_space_p |
| 7715 | || arrow_used)))))) |
| 7716 | { |
| 7717 | int ts; |
| 7718 | colnr_T vcol; |
| 7719 | colnr_T want_vcol; |
| 7720 | int extra = 0; |
| 7721 | |
| 7722 | *inserted_space_p = FALSE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 7723 | if (p_sta && in_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7724 | ts = curbuf->b_p_sw; |
| 7725 | else |
| 7726 | ts = curbuf->b_p_sts; |
| 7727 | /* Compute the virtual column where we want to be. Since |
| 7728 | * 'showbreak' may get in the way, need to get the last column of |
| 7729 | * the previous character. */ |
| 7730 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 7731 | dec_cursor(); |
| 7732 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 7733 | inc_cursor(); |
| 7734 | want_vcol = (want_vcol / ts) * ts; |
| 7735 | |
| 7736 | /* delete characters until we are at or before want_vcol */ |
| 7737 | while (vcol > want_vcol |
| 7738 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
| 7739 | { |
| 7740 | dec_cursor(); |
| 7741 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 7742 | if (State & REPLACE_FLAG) |
| 7743 | { |
| 7744 | /* Don't delete characters before the insert point when in |
| 7745 | * Replace mode */ |
| 7746 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 7747 | || curwin->w_cursor.col >= Insstart.col) |
| 7748 | { |
| 7749 | #if 0 /* what was this for? It causes problems when sw != ts. */ |
| 7750 | if (State == REPLACE && (int)vcol < want_vcol) |
| 7751 | { |
| 7752 | (void)del_char(FALSE); |
| 7753 | extra = 2; /* don't pop too much */ |
| 7754 | } |
| 7755 | else |
| 7756 | #endif |
| 7757 | replace_do_bs(); |
| 7758 | } |
| 7759 | } |
| 7760 | else |
| 7761 | (void)del_char(FALSE); |
| 7762 | } |
| 7763 | |
| 7764 | /* insert extra spaces until we are at want_vcol */ |
| 7765 | while (vcol < want_vcol) |
| 7766 | { |
| 7767 | /* Remember the first char we inserted */ |
| 7768 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7769 | && curwin->w_cursor.col < Insstart.col) |
| 7770 | Insstart.col = curwin->w_cursor.col; |
| 7771 | |
| 7772 | #ifdef FEAT_VREPLACE |
| 7773 | if (State & VREPLACE_FLAG) |
| 7774 | ins_char(' '); |
| 7775 | else |
| 7776 | #endif |
| 7777 | { |
| 7778 | ins_str((char_u *)" "); |
| 7779 | if ((State & REPLACE_FLAG) && extra <= 1) |
| 7780 | { |
| 7781 | if (extra) |
| 7782 | replace_push_off(NUL); |
| 7783 | else |
| 7784 | replace_push(NUL); |
| 7785 | } |
| 7786 | if (extra == 2) |
| 7787 | extra = 1; |
| 7788 | } |
| 7789 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 7790 | } |
| 7791 | } |
| 7792 | |
| 7793 | /* |
| 7794 | * Delete upto starting point, start of line or previous word. |
| 7795 | */ |
| 7796 | else do |
| 7797 | { |
| 7798 | #ifdef FEAT_RIGHTLEFT |
| 7799 | if (!revins_on) /* put cursor on char to be deleted */ |
| 7800 | #endif |
| 7801 | dec_cursor(); |
| 7802 | |
| 7803 | /* start of word? */ |
| 7804 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 7805 | { |
| 7806 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 7807 | temp = vim_iswordc(gchar_cursor()); |
| 7808 | } |
| 7809 | /* end of word? */ |
| 7810 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 7811 | && (vim_isspace(cc = gchar_cursor()) |
| 7812 | || vim_iswordc(cc) != temp)) |
| 7813 | { |
| 7814 | #ifdef FEAT_RIGHTLEFT |
| 7815 | if (!revins_on) |
| 7816 | #endif |
| 7817 | inc_cursor(); |
| 7818 | #ifdef FEAT_RIGHTLEFT |
| 7819 | else if (State & REPLACE_FLAG) |
| 7820 | dec_cursor(); |
| 7821 | #endif |
| 7822 | break; |
| 7823 | } |
| 7824 | if (State & REPLACE_FLAG) |
| 7825 | replace_do_bs(); |
| 7826 | else |
| 7827 | { |
| 7828 | #ifdef FEAT_MBYTE |
| 7829 | if (enc_utf8 && p_deco) |
| 7830 | (void)utfc_ptr2char(ml_get_cursor(), &p1, &p2); |
| 7831 | #endif |
| 7832 | (void)del_char(FALSE); |
| 7833 | #ifdef FEAT_MBYTE |
| 7834 | /* |
| 7835 | * If p1 or p2 is non-zero, there are combining characters we |
| 7836 | * need to take account of. Don't back up before the base |
| 7837 | * character. |
| 7838 | */ |
| 7839 | if (enc_utf8 && p_deco && (p1 != NUL || p2 != NUL)) |
| 7840 | inc_cursor(); |
| 7841 | #endif |
| 7842 | #ifdef FEAT_RIGHTLEFT |
| 7843 | if (revins_chars) |
| 7844 | { |
| 7845 | revins_chars--; |
| 7846 | revins_legal++; |
| 7847 | } |
| 7848 | if (revins_on && gchar_cursor() == NUL) |
| 7849 | break; |
| 7850 | #endif |
| 7851 | } |
| 7852 | /* Just a single backspace?: */ |
| 7853 | if (mode == BACKSPACE_CHAR) |
| 7854 | break; |
| 7855 | } while ( |
| 7856 | #ifdef FEAT_RIGHTLEFT |
| 7857 | revins_on || |
| 7858 | #endif |
| 7859 | (curwin->w_cursor.col > mincol |
| 7860 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 7861 | || curwin->w_cursor.col != Insstart.col))); |
| 7862 | did_backspace = TRUE; |
| 7863 | } |
| 7864 | #ifdef FEAT_SMARTINDENT |
| 7865 | did_si = FALSE; |
| 7866 | can_si = FALSE; |
| 7867 | can_si_back = FALSE; |
| 7868 | #endif |
| 7869 | if (curwin->w_cursor.col <= 1) |
| 7870 | did_ai = FALSE; |
| 7871 | /* |
| 7872 | * It's a little strange to put backspaces into the redo |
| 7873 | * buffer, but it makes auto-indent a lot easier to deal |
| 7874 | * with. |
| 7875 | */ |
| 7876 | AppendCharToRedobuff(c); |
| 7877 | |
| 7878 | /* If deleted before the insertion point, adjust it */ |
| 7879 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 7880 | && curwin->w_cursor.col < Insstart.col) |
| 7881 | Insstart.col = curwin->w_cursor.col; |
| 7882 | |
| 7883 | /* vi behaviour: the cursor moves backward but the character that |
| 7884 | * was there remains visible |
| 7885 | * Vim behaviour: the cursor moves backward and the character that |
| 7886 | * was there is erased from the screen. |
| 7887 | * We can emulate the vi behaviour by pretending there is a dollar |
| 7888 | * displayed even when there isn't. |
| 7889 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
| 7890 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0) |
| 7891 | dollar_vcol = curwin->w_virtcol; |
| 7892 | |
| 7893 | return did_backspace; |
| 7894 | } |
| 7895 | |
| 7896 | #ifdef FEAT_MOUSE |
| 7897 | static void |
| 7898 | ins_mouse(c) |
| 7899 | int c; |
| 7900 | { |
| 7901 | pos_T tpos; |
| 7902 | |
| 7903 | # ifdef FEAT_GUI |
| 7904 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 7905 | if (!gui.in_use) |
| 7906 | # endif |
| 7907 | if (!mouse_has(MOUSE_INSERT)) |
| 7908 | return; |
| 7909 | |
| 7910 | undisplay_dollar(); |
| 7911 | tpos = curwin->w_cursor; |
| 7912 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 7913 | { |
| 7914 | start_arrow(&tpos); |
| 7915 | # ifdef FEAT_CINDENT |
| 7916 | can_cindent = TRUE; |
| 7917 | # endif |
| 7918 | } |
| 7919 | |
| 7920 | #ifdef FEAT_WINDOWS |
| 7921 | /* redraw status lines (in case another window became active) */ |
| 7922 | redraw_statuslines(); |
| 7923 | #endif |
| 7924 | } |
| 7925 | |
| 7926 | static void |
| 7927 | ins_mousescroll(up) |
| 7928 | int up; |
| 7929 | { |
| 7930 | pos_T tpos; |
| 7931 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7932 | win_T *old_curwin; |
| 7933 | # endif |
| 7934 | |
| 7935 | tpos = curwin->w_cursor; |
| 7936 | |
| 7937 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7938 | old_curwin = curwin; |
| 7939 | |
| 7940 | /* Currently the mouse coordinates are only known in the GUI. */ |
| 7941 | if (gui.in_use && mouse_row >= 0 && mouse_col >= 0) |
| 7942 | { |
| 7943 | int row, col; |
| 7944 | |
| 7945 | row = mouse_row; |
| 7946 | col = mouse_col; |
| 7947 | |
| 7948 | /* find the window at the pointer coordinates */ |
| 7949 | curwin = mouse_find_win(&row, &col); |
| 7950 | curbuf = curwin->w_buffer; |
| 7951 | } |
| 7952 | if (curwin == old_curwin) |
| 7953 | # endif |
| 7954 | undisplay_dollar(); |
| 7955 | |
| 7956 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 7957 | scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline)); |
| 7958 | else |
| 7959 | scroll_redraw(up, 3L); |
| 7960 | |
| 7961 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 7962 | curwin->w_redr_status = TRUE; |
| 7963 | |
| 7964 | curwin = old_curwin; |
| 7965 | curbuf = curwin->w_buffer; |
| 7966 | # endif |
| 7967 | |
| 7968 | if (!equalpos(curwin->w_cursor, tpos)) |
| 7969 | { |
| 7970 | start_arrow(&tpos); |
| 7971 | # ifdef FEAT_CINDENT |
| 7972 | can_cindent = TRUE; |
| 7973 | # endif |
| 7974 | } |
| 7975 | } |
| 7976 | #endif |
| 7977 | |
| 7978 | #ifdef FEAT_GUI |
| 7979 | void |
| 7980 | ins_scroll() |
| 7981 | { |
| 7982 | pos_T tpos; |
| 7983 | |
| 7984 | undisplay_dollar(); |
| 7985 | tpos = curwin->w_cursor; |
| 7986 | if (gui_do_scroll()) |
| 7987 | { |
| 7988 | start_arrow(&tpos); |
| 7989 | # ifdef FEAT_CINDENT |
| 7990 | can_cindent = TRUE; |
| 7991 | # endif |
| 7992 | } |
| 7993 | } |
| 7994 | |
| 7995 | void |
| 7996 | ins_horscroll() |
| 7997 | { |
| 7998 | pos_T tpos; |
| 7999 | |
| 8000 | undisplay_dollar(); |
| 8001 | tpos = curwin->w_cursor; |
| 8002 | if (gui_do_horiz_scroll()) |
| 8003 | { |
| 8004 | start_arrow(&tpos); |
| 8005 | # ifdef FEAT_CINDENT |
| 8006 | can_cindent = TRUE; |
| 8007 | # endif |
| 8008 | } |
| 8009 | } |
| 8010 | #endif |
| 8011 | |
| 8012 | static void |
| 8013 | ins_left() |
| 8014 | { |
| 8015 | pos_T tpos; |
| 8016 | |
| 8017 | #ifdef FEAT_FOLDING |
| 8018 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8019 | foldOpenCursor(); |
| 8020 | #endif |
| 8021 | undisplay_dollar(); |
| 8022 | tpos = curwin->w_cursor; |
| 8023 | if (oneleft() == OK) |
| 8024 | { |
| 8025 | start_arrow(&tpos); |
| 8026 | #ifdef FEAT_RIGHTLEFT |
| 8027 | /* If exit reversed string, position is fixed */ |
| 8028 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 8029 | revins_legal++; |
| 8030 | revins_chars++; |
| 8031 | #endif |
| 8032 | } |
| 8033 | |
| 8034 | /* |
| 8035 | * if 'whichwrap' set for cursor in insert mode may go to |
| 8036 | * previous line |
| 8037 | */ |
| 8038 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 8039 | { |
| 8040 | start_arrow(&tpos); |
| 8041 | --(curwin->w_cursor.lnum); |
| 8042 | coladvance((colnr_T)MAXCOL); |
| 8043 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 8044 | } |
| 8045 | else |
| 8046 | vim_beep(); |
| 8047 | } |
| 8048 | |
| 8049 | static void |
| 8050 | ins_home(c) |
| 8051 | int c; |
| 8052 | { |
| 8053 | pos_T tpos; |
| 8054 | |
| 8055 | #ifdef FEAT_FOLDING |
| 8056 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8057 | foldOpenCursor(); |
| 8058 | #endif |
| 8059 | undisplay_dollar(); |
| 8060 | tpos = curwin->w_cursor; |
| 8061 | if (c == K_C_HOME) |
| 8062 | curwin->w_cursor.lnum = 1; |
| 8063 | curwin->w_cursor.col = 0; |
| 8064 | #ifdef FEAT_VIRTUALEDIT |
| 8065 | curwin->w_cursor.coladd = 0; |
| 8066 | #endif |
| 8067 | curwin->w_curswant = 0; |
| 8068 | start_arrow(&tpos); |
| 8069 | } |
| 8070 | |
| 8071 | static void |
| 8072 | ins_end(c) |
| 8073 | int c; |
| 8074 | { |
| 8075 | pos_T tpos; |
| 8076 | |
| 8077 | #ifdef FEAT_FOLDING |
| 8078 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8079 | foldOpenCursor(); |
| 8080 | #endif |
| 8081 | undisplay_dollar(); |
| 8082 | tpos = curwin->w_cursor; |
| 8083 | if (c == K_C_END) |
| 8084 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 8085 | coladvance((colnr_T)MAXCOL); |
| 8086 | curwin->w_curswant = MAXCOL; |
| 8087 | |
| 8088 | start_arrow(&tpos); |
| 8089 | } |
| 8090 | |
| 8091 | static void |
| 8092 | ins_s_left() |
| 8093 | { |
| 8094 | #ifdef FEAT_FOLDING |
| 8095 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8096 | foldOpenCursor(); |
| 8097 | #endif |
| 8098 | undisplay_dollar(); |
| 8099 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 8100 | { |
| 8101 | start_arrow(&curwin->w_cursor); |
| 8102 | (void)bck_word(1L, FALSE, FALSE); |
| 8103 | curwin->w_set_curswant = TRUE; |
| 8104 | } |
| 8105 | else |
| 8106 | vim_beep(); |
| 8107 | } |
| 8108 | |
| 8109 | static void |
| 8110 | ins_right() |
| 8111 | { |
| 8112 | #ifdef FEAT_FOLDING |
| 8113 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8114 | foldOpenCursor(); |
| 8115 | #endif |
| 8116 | undisplay_dollar(); |
| 8117 | if (gchar_cursor() != NUL || virtual_active() |
| 8118 | ) |
| 8119 | { |
| 8120 | start_arrow(&curwin->w_cursor); |
| 8121 | curwin->w_set_curswant = TRUE; |
| 8122 | #ifdef FEAT_VIRTUALEDIT |
| 8123 | if (virtual_active()) |
| 8124 | oneright(); |
| 8125 | else |
| 8126 | #endif |
| 8127 | { |
| 8128 | #ifdef FEAT_MBYTE |
| 8129 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8130 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8131 | else |
| 8132 | #endif |
| 8133 | ++curwin->w_cursor.col; |
| 8134 | } |
| 8135 | |
| 8136 | #ifdef FEAT_RIGHTLEFT |
| 8137 | revins_legal++; |
| 8138 | if (revins_chars) |
| 8139 | revins_chars--; |
| 8140 | #endif |
| 8141 | } |
| 8142 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 8143 | * cursor to the next line */ |
| 8144 | else if (vim_strchr(p_ww, ']') != NULL |
| 8145 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 8146 | { |
| 8147 | start_arrow(&curwin->w_cursor); |
| 8148 | curwin->w_set_curswant = TRUE; |
| 8149 | ++curwin->w_cursor.lnum; |
| 8150 | curwin->w_cursor.col = 0; |
| 8151 | } |
| 8152 | else |
| 8153 | vim_beep(); |
| 8154 | } |
| 8155 | |
| 8156 | static void |
| 8157 | ins_s_right() |
| 8158 | { |
| 8159 | #ifdef FEAT_FOLDING |
| 8160 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8161 | foldOpenCursor(); |
| 8162 | #endif |
| 8163 | undisplay_dollar(); |
| 8164 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 8165 | || gchar_cursor() != NUL) |
| 8166 | { |
| 8167 | start_arrow(&curwin->w_cursor); |
| 8168 | (void)fwd_word(1L, FALSE, 0); |
| 8169 | curwin->w_set_curswant = TRUE; |
| 8170 | } |
| 8171 | else |
| 8172 | vim_beep(); |
| 8173 | } |
| 8174 | |
| 8175 | static void |
| 8176 | ins_up(startcol) |
| 8177 | int startcol; /* when TRUE move to Insstart.col */ |
| 8178 | { |
| 8179 | pos_T tpos; |
| 8180 | linenr_T old_topline = curwin->w_topline; |
| 8181 | #ifdef FEAT_DIFF |
| 8182 | int old_topfill = curwin->w_topfill; |
| 8183 | #endif |
| 8184 | |
| 8185 | undisplay_dollar(); |
| 8186 | tpos = curwin->w_cursor; |
| 8187 | if (cursor_up(1L, TRUE) == OK) |
| 8188 | { |
| 8189 | if (startcol) |
| 8190 | coladvance(getvcol_nolist(&Insstart)); |
| 8191 | if (old_topline != curwin->w_topline |
| 8192 | #ifdef FEAT_DIFF |
| 8193 | || old_topfill != curwin->w_topfill |
| 8194 | #endif |
| 8195 | ) |
| 8196 | redraw_later(VALID); |
| 8197 | start_arrow(&tpos); |
| 8198 | #ifdef FEAT_CINDENT |
| 8199 | can_cindent = TRUE; |
| 8200 | #endif |
| 8201 | } |
| 8202 | else |
| 8203 | vim_beep(); |
| 8204 | } |
| 8205 | |
| 8206 | static void |
| 8207 | ins_pageup() |
| 8208 | { |
| 8209 | pos_T tpos; |
| 8210 | |
| 8211 | undisplay_dollar(); |
| 8212 | tpos = curwin->w_cursor; |
| 8213 | if (onepage(BACKWARD, 1L) == OK) |
| 8214 | { |
| 8215 | start_arrow(&tpos); |
| 8216 | #ifdef FEAT_CINDENT |
| 8217 | can_cindent = TRUE; |
| 8218 | #endif |
| 8219 | } |
| 8220 | else |
| 8221 | vim_beep(); |
| 8222 | } |
| 8223 | |
| 8224 | static void |
| 8225 | ins_down(startcol) |
| 8226 | int startcol; /* when TRUE move to Insstart.col */ |
| 8227 | { |
| 8228 | pos_T tpos; |
| 8229 | linenr_T old_topline = curwin->w_topline; |
| 8230 | #ifdef FEAT_DIFF |
| 8231 | int old_topfill = curwin->w_topfill; |
| 8232 | #endif |
| 8233 | |
| 8234 | undisplay_dollar(); |
| 8235 | tpos = curwin->w_cursor; |
| 8236 | if (cursor_down(1L, TRUE) == OK) |
| 8237 | { |
| 8238 | if (startcol) |
| 8239 | coladvance(getvcol_nolist(&Insstart)); |
| 8240 | if (old_topline != curwin->w_topline |
| 8241 | #ifdef FEAT_DIFF |
| 8242 | || old_topfill != curwin->w_topfill |
| 8243 | #endif |
| 8244 | ) |
| 8245 | redraw_later(VALID); |
| 8246 | start_arrow(&tpos); |
| 8247 | #ifdef FEAT_CINDENT |
| 8248 | can_cindent = TRUE; |
| 8249 | #endif |
| 8250 | } |
| 8251 | else |
| 8252 | vim_beep(); |
| 8253 | } |
| 8254 | |
| 8255 | static void |
| 8256 | ins_pagedown() |
| 8257 | { |
| 8258 | pos_T tpos; |
| 8259 | |
| 8260 | undisplay_dollar(); |
| 8261 | tpos = curwin->w_cursor; |
| 8262 | if (onepage(FORWARD, 1L) == OK) |
| 8263 | { |
| 8264 | start_arrow(&tpos); |
| 8265 | #ifdef FEAT_CINDENT |
| 8266 | can_cindent = TRUE; |
| 8267 | #endif |
| 8268 | } |
| 8269 | else |
| 8270 | vim_beep(); |
| 8271 | } |
| 8272 | |
| 8273 | #ifdef FEAT_DND |
| 8274 | static void |
| 8275 | ins_drop() |
| 8276 | { |
| 8277 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 8278 | } |
| 8279 | #endif |
| 8280 | |
| 8281 | /* |
| 8282 | * Handle TAB in Insert or Replace mode. |
| 8283 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 8284 | */ |
| 8285 | static int |
| 8286 | ins_tab() |
| 8287 | { |
| 8288 | int ind; |
| 8289 | int i; |
| 8290 | int temp; |
| 8291 | |
| 8292 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 8293 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 8294 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 8295 | return FALSE; |
| 8296 | |
| 8297 | ind = inindent(0); |
| 8298 | #ifdef FEAT_CINDENT |
| 8299 | if (ind) |
| 8300 | can_cindent = FALSE; |
| 8301 | #endif |
| 8302 | |
| 8303 | /* |
| 8304 | * When nothing special, insert TAB like a normal character |
| 8305 | */ |
| 8306 | if (!curbuf->b_p_et |
| 8307 | && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw) |
| 8308 | && curbuf->b_p_sts == 0) |
| 8309 | return TRUE; |
| 8310 | |
| 8311 | if (stop_arrow() == FAIL) |
| 8312 | return TRUE; |
| 8313 | |
| 8314 | did_ai = FALSE; |
| 8315 | #ifdef FEAT_SMARTINDENT |
| 8316 | did_si = FALSE; |
| 8317 | can_si = FALSE; |
| 8318 | can_si_back = FALSE; |
| 8319 | #endif |
| 8320 | AppendToRedobuff((char_u *)"\t"); |
| 8321 | |
| 8322 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
| 8323 | temp = (int)curbuf->b_p_sw; |
| 8324 | else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */ |
| 8325 | temp = (int)curbuf->b_p_sts; |
| 8326 | else /* otherwise use 'tabstop' */ |
| 8327 | temp = (int)curbuf->b_p_ts; |
| 8328 | temp -= get_nolist_virtcol() % temp; |
| 8329 | |
| 8330 | /* |
| 8331 | * Insert the first space with ins_char(). It will delete one char in |
| 8332 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 8333 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 8334 | */ |
| 8335 | ins_char(' '); |
| 8336 | while (--temp > 0) |
| 8337 | { |
| 8338 | #ifdef FEAT_VREPLACE |
| 8339 | if (State & VREPLACE_FLAG) |
| 8340 | ins_char(' '); |
| 8341 | else |
| 8342 | #endif |
| 8343 | { |
| 8344 | ins_str((char_u *)" "); |
| 8345 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 8346 | replace_push(NUL); |
| 8347 | } |
| 8348 | } |
| 8349 | |
| 8350 | /* |
| 8351 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 8352 | */ |
| 8353 | if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind))) |
| 8354 | { |
| 8355 | char_u *ptr; |
| 8356 | #ifdef FEAT_VREPLACE |
| 8357 | char_u *saved_line = NULL; /* init for GCC */ |
| 8358 | pos_T pos; |
| 8359 | #endif |
| 8360 | pos_T fpos; |
| 8361 | pos_T *cursor; |
| 8362 | colnr_T want_vcol, vcol; |
| 8363 | int change_col = -1; |
| 8364 | int save_list = curwin->w_p_list; |
| 8365 | |
| 8366 | /* |
| 8367 | * Get the current line. For VREPLACE mode, don't make real changes |
| 8368 | * yet, just work on a copy of the line. |
| 8369 | */ |
| 8370 | #ifdef FEAT_VREPLACE |
| 8371 | if (State & VREPLACE_FLAG) |
| 8372 | { |
| 8373 | pos = curwin->w_cursor; |
| 8374 | cursor = &pos; |
| 8375 | saved_line = vim_strsave(ml_get_curline()); |
| 8376 | if (saved_line == NULL) |
| 8377 | return FALSE; |
| 8378 | ptr = saved_line + pos.col; |
| 8379 | } |
| 8380 | else |
| 8381 | #endif |
| 8382 | { |
| 8383 | ptr = ml_get_cursor(); |
| 8384 | cursor = &curwin->w_cursor; |
| 8385 | } |
| 8386 | |
| 8387 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 8388 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 8389 | curwin->w_p_list = FALSE; |
| 8390 | |
| 8391 | /* Find first white before the cursor */ |
| 8392 | fpos = curwin->w_cursor; |
| 8393 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 8394 | { |
| 8395 | --fpos.col; |
| 8396 | --ptr; |
| 8397 | } |
| 8398 | |
| 8399 | /* In Replace mode, don't change characters before the insert point. */ |
| 8400 | if ((State & REPLACE_FLAG) |
| 8401 | && fpos.lnum == Insstart.lnum |
| 8402 | && fpos.col < Insstart.col) |
| 8403 | { |
| 8404 | ptr += Insstart.col - fpos.col; |
| 8405 | fpos.col = Insstart.col; |
| 8406 | } |
| 8407 | |
| 8408 | /* compute virtual column numbers of first white and cursor */ |
| 8409 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 8410 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 8411 | |
| 8412 | /* Use as many TABs as possible. Beware of 'showbreak' and |
| 8413 | * 'linebreak' adding extra virtual columns. */ |
| 8414 | while (vim_iswhite(*ptr)) |
| 8415 | { |
| 8416 | i = lbr_chartabsize((char_u *)"\t", vcol); |
| 8417 | if (vcol + i > want_vcol) |
| 8418 | break; |
| 8419 | if (*ptr != TAB) |
| 8420 | { |
| 8421 | *ptr = TAB; |
| 8422 | if (change_col < 0) |
| 8423 | { |
| 8424 | change_col = fpos.col; /* Column of first change */ |
| 8425 | /* May have to adjust Insstart */ |
| 8426 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 8427 | Insstart.col = fpos.col; |
| 8428 | } |
| 8429 | } |
| 8430 | ++fpos.col; |
| 8431 | ++ptr; |
| 8432 | vcol += i; |
| 8433 | } |
| 8434 | |
| 8435 | if (change_col >= 0) |
| 8436 | { |
| 8437 | int repl_off = 0; |
| 8438 | |
| 8439 | /* Skip over the spaces we need. */ |
| 8440 | while (vcol < want_vcol && *ptr == ' ') |
| 8441 | { |
| 8442 | vcol += lbr_chartabsize(ptr, vcol); |
| 8443 | ++ptr; |
| 8444 | ++repl_off; |
| 8445 | } |
| 8446 | if (vcol > want_vcol) |
| 8447 | { |
| 8448 | /* Must have a char with 'showbreak' just before it. */ |
| 8449 | --ptr; |
| 8450 | --repl_off; |
| 8451 | } |
| 8452 | fpos.col += repl_off; |
| 8453 | |
| 8454 | /* Delete following spaces. */ |
| 8455 | i = cursor->col - fpos.col; |
| 8456 | if (i > 0) |
| 8457 | { |
| 8458 | mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1); |
| 8459 | /* correct replace stack. */ |
| 8460 | if ((State & REPLACE_FLAG) |
| 8461 | #ifdef FEAT_VREPLACE |
| 8462 | && !(State & VREPLACE_FLAG) |
| 8463 | #endif |
| 8464 | ) |
| 8465 | for (temp = i; --temp >= 0; ) |
| 8466 | replace_join(repl_off); |
| 8467 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 8468 | #ifdef FEAT_NETBEANS_INTG |
| 8469 | if (usingNetbeans) |
| 8470 | { |
| 8471 | netbeans_removed(curbuf, fpos.lnum, cursor->col, |
| 8472 | (long)(i + 1)); |
| 8473 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 8474 | (char_u *)"\t", 1); |
| 8475 | } |
| 8476 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8477 | cursor->col -= i; |
| 8478 | |
| 8479 | #ifdef FEAT_VREPLACE |
| 8480 | /* |
| 8481 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 8482 | * backspacing over the changed spacing and then inserting the new |
| 8483 | * spacing. |
| 8484 | */ |
| 8485 | if (State & VREPLACE_FLAG) |
| 8486 | { |
| 8487 | /* Backspace from real cursor to change_col */ |
| 8488 | backspace_until_column(change_col); |
| 8489 | |
| 8490 | /* Insert each char in saved_line from changed_col to |
| 8491 | * ptr-cursor */ |
| 8492 | ins_bytes_len(saved_line + change_col, |
| 8493 | cursor->col - change_col); |
| 8494 | } |
| 8495 | #endif |
| 8496 | } |
| 8497 | |
| 8498 | #ifdef FEAT_VREPLACE |
| 8499 | if (State & VREPLACE_FLAG) |
| 8500 | vim_free(saved_line); |
| 8501 | #endif |
| 8502 | curwin->w_p_list = save_list; |
| 8503 | } |
| 8504 | |
| 8505 | return FALSE; |
| 8506 | } |
| 8507 | |
| 8508 | /* |
| 8509 | * Handle CR or NL in insert mode. |
| 8510 | * Return TRUE when out of memory or can't undo. |
| 8511 | */ |
| 8512 | static int |
| 8513 | ins_eol(c) |
| 8514 | int c; |
| 8515 | { |
| 8516 | int i; |
| 8517 | |
| 8518 | if (echeck_abbr(c + ABBR_OFF)) |
| 8519 | return FALSE; |
| 8520 | if (stop_arrow() == FAIL) |
| 8521 | return TRUE; |
| 8522 | undisplay_dollar(); |
| 8523 | |
| 8524 | /* |
| 8525 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 8526 | * character under the cursor. Only push a NUL on the replace stack, |
| 8527 | * nothing to put back when the NL is deleted. |
| 8528 | */ |
| 8529 | if ((State & REPLACE_FLAG) |
| 8530 | #ifdef FEAT_VREPLACE |
| 8531 | && !(State & VREPLACE_FLAG) |
| 8532 | #endif |
| 8533 | ) |
| 8534 | replace_push(NUL); |
| 8535 | |
| 8536 | /* |
| 8537 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 8538 | * replacing the next line, so we push all of the characters left on the |
| 8539 | * line onto the replace stack. This is not done here though, it is done |
| 8540 | * in open_line(). |
| 8541 | */ |
| 8542 | |
| 8543 | #ifdef FEAT_RIGHTLEFT |
| 8544 | # ifdef FEAT_FKMAP |
| 8545 | if (p_altkeymap && p_fkmap) |
| 8546 | fkmap(NL); |
| 8547 | # endif |
| 8548 | /* NL in reverse insert will always start in the end of |
| 8549 | * current line. */ |
| 8550 | if (revins_on) |
| 8551 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 8552 | #endif |
| 8553 | |
| 8554 | AppendToRedobuff(NL_STR); |
| 8555 | i = open_line(FORWARD, |
| 8556 | #ifdef FEAT_COMMENTS |
| 8557 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 8558 | #endif |
| 8559 | 0, old_indent); |
| 8560 | old_indent = 0; |
| 8561 | #ifdef FEAT_CINDENT |
| 8562 | can_cindent = TRUE; |
| 8563 | #endif |
| 8564 | |
| 8565 | return (!i); |
| 8566 | } |
| 8567 | |
| 8568 | #ifdef FEAT_DIGRAPHS |
| 8569 | /* |
| 8570 | * Handle digraph in insert mode. |
| 8571 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 8572 | * done. |
| 8573 | */ |
| 8574 | static int |
| 8575 | ins_digraph() |
| 8576 | { |
| 8577 | int c; |
| 8578 | int cc; |
| 8579 | |
| 8580 | pc_status = PC_STATUS_UNSET; |
| 8581 | if (redrawing() && !char_avail()) |
| 8582 | { |
| 8583 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8584 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8585 | |
| 8586 | edit_putchar('?', TRUE); |
| 8587 | #ifdef FEAT_CMDL_INFO |
| 8588 | add_to_showcmd_c(Ctrl_K); |
| 8589 | #endif |
| 8590 | } |
| 8591 | |
| 8592 | #ifdef USE_ON_FLY_SCROLL |
| 8593 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 8594 | #endif |
| 8595 | |
| 8596 | /* don't map the digraph chars. This also prevents the |
| 8597 | * mode message to be deleted when ESC is hit */ |
| 8598 | ++no_mapping; |
| 8599 | ++allow_keys; |
| 8600 | c = safe_vgetc(); |
| 8601 | --no_mapping; |
| 8602 | --allow_keys; |
| 8603 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 8604 | { |
| 8605 | #ifdef FEAT_CMDL_INFO |
| 8606 | clear_showcmd(); |
| 8607 | #endif |
| 8608 | insert_special(c, TRUE, FALSE); |
| 8609 | return NUL; |
| 8610 | } |
| 8611 | if (c != ESC) |
| 8612 | { |
| 8613 | if (redrawing() && !char_avail()) |
| 8614 | { |
| 8615 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8616 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8617 | |
| 8618 | if (char2cells(c) == 1) |
| 8619 | { |
| 8620 | /* first remove the '?', otherwise it's restored when typing |
| 8621 | * an ESC next */ |
| 8622 | edit_unputchar(); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8623 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8624 | edit_putchar(c, TRUE); |
| 8625 | } |
| 8626 | #ifdef FEAT_CMDL_INFO |
| 8627 | add_to_showcmd_c(c); |
| 8628 | #endif |
| 8629 | } |
| 8630 | ++no_mapping; |
| 8631 | ++allow_keys; |
| 8632 | cc = safe_vgetc(); |
| 8633 | --no_mapping; |
| 8634 | --allow_keys; |
| 8635 | if (cc != ESC) |
| 8636 | { |
| 8637 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 8638 | c = getdigraph(c, cc, TRUE); |
| 8639 | #ifdef FEAT_CMDL_INFO |
| 8640 | clear_showcmd(); |
| 8641 | #endif |
| 8642 | return c; |
| 8643 | } |
| 8644 | } |
| 8645 | edit_unputchar(); |
| 8646 | #ifdef FEAT_CMDL_INFO |
| 8647 | clear_showcmd(); |
| 8648 | #endif |
| 8649 | return NUL; |
| 8650 | } |
| 8651 | #endif |
| 8652 | |
| 8653 | /* |
| 8654 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 8655 | * Returns the char to be inserted, or NUL if none found. |
| 8656 | */ |
| 8657 | static int |
| 8658 | ins_copychar(lnum) |
| 8659 | linenr_T lnum; |
| 8660 | { |
| 8661 | int c; |
| 8662 | int temp; |
| 8663 | char_u *ptr, *prev_ptr; |
| 8664 | |
| 8665 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 8666 | { |
| 8667 | vim_beep(); |
| 8668 | return NUL; |
| 8669 | } |
| 8670 | |
| 8671 | /* try to advance to the cursor column */ |
| 8672 | temp = 0; |
| 8673 | ptr = ml_get(lnum); |
| 8674 | prev_ptr = ptr; |
| 8675 | validate_virtcol(); |
| 8676 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 8677 | { |
| 8678 | prev_ptr = ptr; |
| 8679 | temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp); |
| 8680 | } |
| 8681 | if ((colnr_T)temp > curwin->w_virtcol) |
| 8682 | ptr = prev_ptr; |
| 8683 | |
| 8684 | #ifdef FEAT_MBYTE |
| 8685 | c = (*mb_ptr2char)(ptr); |
| 8686 | #else |
| 8687 | c = *ptr; |
| 8688 | #endif |
| 8689 | if (c == NUL) |
| 8690 | vim_beep(); |
| 8691 | return c; |
| 8692 | } |
| 8693 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8694 | /* |
| 8695 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 8696 | */ |
| 8697 | static int |
| 8698 | ins_ctrl_ey(tc) |
| 8699 | int tc; |
| 8700 | { |
| 8701 | int c = tc; |
| 8702 | |
| 8703 | #ifdef FEAT_INS_EXPAND |
| 8704 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 8705 | { |
| 8706 | if (c == Ctrl_Y) |
| 8707 | scrolldown_clamp(); |
| 8708 | else |
| 8709 | scrollup_clamp(); |
| 8710 | redraw_later(VALID); |
| 8711 | } |
| 8712 | else |
| 8713 | #endif |
| 8714 | { |
| 8715 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 8716 | if (c != NUL) |
| 8717 | { |
| 8718 | long tw_save; |
| 8719 | |
| 8720 | /* The character must be taken literally, insert like it |
| 8721 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 8722 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 8723 | * CTRL-V, don't use it for these. */ |
| 8724 | if (c < 256 && !isalnum(c)) |
| 8725 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 8726 | tw_save = curbuf->b_p_tw; |
| 8727 | curbuf->b_p_tw = -1; |
| 8728 | insert_special(c, TRUE, FALSE); |
| 8729 | curbuf->b_p_tw = tw_save; |
| 8730 | #ifdef FEAT_RIGHTLEFT |
| 8731 | revins_chars++; |
| 8732 | revins_legal++; |
| 8733 | #endif |
| 8734 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 8735 | auto_format(FALSE, TRUE); |
| 8736 | } |
| 8737 | } |
| 8738 | return c; |
| 8739 | } |
| 8740 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8741 | #ifdef FEAT_SMARTINDENT |
| 8742 | /* |
| 8743 | * Try to do some very smart auto-indenting. |
| 8744 | * Used when inserting a "normal" character. |
| 8745 | */ |
| 8746 | static void |
| 8747 | ins_try_si(c) |
| 8748 | int c; |
| 8749 | { |
| 8750 | pos_T *pos, old_pos; |
| 8751 | char_u *ptr; |
| 8752 | int i; |
| 8753 | int temp; |
| 8754 | |
| 8755 | /* |
| 8756 | * do some very smart indenting when entering '{' or '}' |
| 8757 | */ |
| 8758 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 8759 | { |
| 8760 | /* |
| 8761 | * for '}' set indent equal to indent of line containing matching '{' |
| 8762 | */ |
| 8763 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 8764 | { |
| 8765 | old_pos = curwin->w_cursor; |
| 8766 | /* |
| 8767 | * If the matching '{' has a ')' immediately before it (ignoring |
| 8768 | * white-space), then line up with the start of the line |
| 8769 | * containing the matching '(' if there is one. This handles the |
| 8770 | * case where an "if (..\n..) {" statement continues over multiple |
| 8771 | * lines -- webb |
| 8772 | */ |
| 8773 | ptr = ml_get(pos->lnum); |
| 8774 | i = pos->col; |
| 8775 | if (i > 0) /* skip blanks before '{' */ |
| 8776 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 8777 | ; |
| 8778 | curwin->w_cursor.lnum = pos->lnum; |
| 8779 | curwin->w_cursor.col = i; |
| 8780 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 8781 | curwin->w_cursor = *pos; |
| 8782 | i = get_indent(); |
| 8783 | curwin->w_cursor = old_pos; |
| 8784 | #ifdef FEAT_VREPLACE |
| 8785 | if (State & VREPLACE_FLAG) |
| 8786 | change_indent(INDENT_SET, i, FALSE, NUL); |
| 8787 | else |
| 8788 | #endif |
| 8789 | (void)set_indent(i, SIN_CHANGED); |
| 8790 | } |
| 8791 | else if (curwin->w_cursor.col > 0) |
| 8792 | { |
| 8793 | /* |
| 8794 | * when inserting '{' after "O" reduce indent, but not |
| 8795 | * more than indent of previous line |
| 8796 | */ |
| 8797 | temp = TRUE; |
| 8798 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 8799 | { |
| 8800 | old_pos = curwin->w_cursor; |
| 8801 | i = get_indent(); |
| 8802 | while (curwin->w_cursor.lnum > 1) |
| 8803 | { |
| 8804 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 8805 | |
| 8806 | /* ignore empty lines and lines starting with '#'. */ |
| 8807 | if (*ptr != '#' && *ptr != NUL) |
| 8808 | break; |
| 8809 | } |
| 8810 | if (get_indent() >= i) |
| 8811 | temp = FALSE; |
| 8812 | curwin->w_cursor = old_pos; |
| 8813 | } |
| 8814 | if (temp) |
| 8815 | shift_line(TRUE, FALSE, 1); |
| 8816 | } |
| 8817 | } |
| 8818 | |
| 8819 | /* |
| 8820 | * set indent of '#' always to 0 |
| 8821 | */ |
| 8822 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 8823 | { |
| 8824 | /* remember current indent for next line */ |
| 8825 | old_indent = get_indent(); |
| 8826 | (void)set_indent(0, SIN_CHANGED); |
| 8827 | } |
| 8828 | |
| 8829 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 8830 | if (ai_col > curwin->w_cursor.col) |
| 8831 | ai_col = curwin->w_cursor.col; |
| 8832 | } |
| 8833 | #endif |
| 8834 | |
| 8835 | /* |
| 8836 | * Get the value that w_virtcol would have when 'list' is off. |
| 8837 | * Unless 'cpo' contains the 'L' flag. |
| 8838 | */ |
| 8839 | static colnr_T |
| 8840 | get_nolist_virtcol() |
| 8841 | { |
| 8842 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 8843 | return getvcol_nolist(&curwin->w_cursor); |
| 8844 | validate_virtcol(); |
| 8845 | return curwin->w_virtcol; |
| 8846 | } |