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 | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 43 | N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^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 | 910f66f | 2006-04-05 20:41:53 +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 | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 72 | char_u *(cp_text[CPT_COUNT]); /* text for the menu */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 73 | char_u *cp_fname; /* file containing the match, allocated when |
| 74 | * cp_flags has FREE_FNAME */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 75 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 76 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 79 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 80 | #define FREE_FNAME (2) |
| 81 | |
| 82 | /* |
| 83 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 84 | * "compl_first_match" points to the start of the list. |
| 85 | * "compl_curr_match" points to the currently selected entry. |
| 86 | * "compl_shown_match" is different from compl_curr_match during |
| 87 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 89 | static compl_T *compl_first_match = NULL; |
| 90 | static compl_T *compl_curr_match = NULL; |
| 91 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 92 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 93 | /* After using a cursor key <Enter> selects a match in the popup menu, |
| 94 | * otherwise it inserts a line break. */ |
| 95 | static int compl_enter_selects = FALSE; |
| 96 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 97 | /* When "compl_leader" is not NULL only matches that start with this string |
| 98 | * are used. */ |
| 99 | static char_u *compl_leader = NULL; |
| 100 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 101 | static int compl_get_longest = FALSE; /* put longest common string |
| 102 | in compl_leader */ |
| 103 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 104 | static int compl_used_match; /* Selected one of the matches. When |
| 105 | FALSE the match was edited or using |
| 106 | the longest common string. */ |
| 107 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 108 | static int compl_was_interrupted = FALSE; /* didn't finish finding |
| 109 | completions. */ |
| 110 | |
| 111 | static int compl_restarting = FALSE; /* don't insert match */ |
| 112 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 113 | /* When the first completion is done "compl_started" is set. When it's |
| 114 | * FALSE the word to be completed must be located. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 115 | static int compl_started = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 116 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 117 | static int compl_matches = 0; |
| 118 | static char_u *compl_pattern = NULL; |
| 119 | static int compl_direction = FORWARD; |
| 120 | static int compl_shows_dir = FORWARD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 121 | static int compl_pending = 0; /* > 1 for postponed CTRL-N */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 122 | static pos_T compl_startpos; |
| 123 | static colnr_T compl_col = 0; /* column where the text starts |
| 124 | * that is being completed */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 125 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 126 | * completion started */ |
| 127 | static int compl_cont_mode = 0; |
| 128 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 129 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 130 | static void ins_ctrl_x __ARGS((void)); |
| 131 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 132 | static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 133 | 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] | 134 | static void ins_compl_longest_match __ARGS((compl_T *match)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 135 | 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] | 136 | static int ins_compl_make_cyclic __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 137 | static void ins_compl_upd_pum __ARGS((void)); |
| 138 | static void ins_compl_del_pum __ARGS((void)); |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 139 | static int pum_wanted __ARGS((void)); |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 140 | static int pum_enough_matches __ARGS((void)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 141 | static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus)); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 142 | static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 143 | static char_u *find_line_end __ARGS((char_u *ptr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | static void ins_compl_free __ARGS((void)); |
| 145 | static void ins_compl_clear __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 146 | static int ins_compl_bs __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 147 | static void ins_compl_new_leader __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 148 | static void ins_compl_addleader __ARGS((int c)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 149 | static void ins_compl_restart __ARGS((void)); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 150 | static void ins_compl_set_original_text __ARGS((char_u *str)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 151 | static void ins_compl_addfrommatch __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 152 | static int ins_compl_prep __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag)); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 154 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 155 | static void ins_compl_add_list __ARGS((list_T *list)); |
| 156 | #endif |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 157 | static int ins_compl_get_exp __ARGS((pos_T *ini)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | static void ins_compl_delete __ARGS((void)); |
| 159 | static void ins_compl_insert __ARGS((void)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 160 | 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] | 161 | static int ins_compl_key2dir __ARGS((int c)); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 162 | static int ins_compl_pum_key __ARGS((int c)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 163 | static int ins_compl_key2count __ARGS((int c)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 164 | static int ins_compl_use_match __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 165 | static int ins_complete __ARGS((int c)); |
| 166 | static int quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
| 167 | #endif /* FEAT_INS_EXPAND */ |
| 168 | |
| 169 | #define BACKSPACE_CHAR 1 |
| 170 | #define BACKSPACE_WORD 2 |
| 171 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 172 | #define BACKSPACE_LINE 4 |
| 173 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 174 | static void ins_redraw __ARGS((int ready)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 175 | static void ins_ctrl_v __ARGS((void)); |
| 176 | static void undisplay_dollar __ARGS((void)); |
| 177 | static void insert_special __ARGS((int, int, int)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 178 | 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] | 179 | static void check_auto_format __ARGS((int)); |
| 180 | static void redo_literal __ARGS((int c)); |
| 181 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 182 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 183 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 184 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 185 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 186 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc)); |
| 188 | static int echeck_abbr __ARGS((int)); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 189 | #if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | static void replace_push_off __ARGS((int c)); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 191 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 192 | static int replace_pop __ARGS((void)); |
| 193 | static void replace_join __ARGS((int off)); |
| 194 | static void replace_pop_ins __ARGS((void)); |
| 195 | #ifdef FEAT_MBYTE |
| 196 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 197 | #endif |
| 198 | static void replace_flush __ARGS((void)); |
| 199 | static void replace_do_bs __ARGS((void)); |
| 200 | #ifdef FEAT_CINDENT |
| 201 | static int cindent_on __ARGS((void)); |
| 202 | #endif |
| 203 | static void ins_reg __ARGS((void)); |
| 204 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 205 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 206 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | #ifdef FEAT_RIGHTLEFT |
| 208 | static void ins_ctrl_ __ARGS((void)); |
| 209 | #endif |
| 210 | #ifdef FEAT_VISUAL |
| 211 | static int ins_start_select __ARGS((int c)); |
| 212 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 213 | static void ins_insert __ARGS((int replaceState)); |
| 214 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 215 | static void ins_shift __ARGS((int c, int lastc)); |
| 216 | static void ins_del __ARGS((void)); |
| 217 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 218 | #ifdef FEAT_MOUSE |
| 219 | static void ins_mouse __ARGS((int c)); |
| 220 | static void ins_mousescroll __ARGS((int up)); |
| 221 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 222 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 223 | static void ins_tabline __ARGS((int c)); |
| 224 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | static void ins_left __ARGS((void)); |
| 226 | static void ins_home __ARGS((int c)); |
| 227 | static void ins_end __ARGS((int c)); |
| 228 | static void ins_s_left __ARGS((void)); |
| 229 | static void ins_right __ARGS((void)); |
| 230 | static void ins_s_right __ARGS((void)); |
| 231 | static void ins_up __ARGS((int startcol)); |
| 232 | static void ins_pageup __ARGS((void)); |
| 233 | static void ins_down __ARGS((int startcol)); |
| 234 | static void ins_pagedown __ARGS((void)); |
| 235 | #ifdef FEAT_DND |
| 236 | static void ins_drop __ARGS((void)); |
| 237 | #endif |
| 238 | static int ins_tab __ARGS((void)); |
| 239 | static int ins_eol __ARGS((int c)); |
| 240 | #ifdef FEAT_DIGRAPHS |
| 241 | static int ins_digraph __ARGS((void)); |
| 242 | #endif |
| 243 | static int ins_copychar __ARGS((linenr_T lnum)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 244 | static int ins_ctrl_ey __ARGS((int tc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | #ifdef FEAT_SMARTINDENT |
| 246 | static void ins_try_si __ARGS((int c)); |
| 247 | #endif |
| 248 | static colnr_T get_nolist_virtcol __ARGS((void)); |
| 249 | |
| 250 | static colnr_T Insstart_textlen; /* length of line when insert started */ |
| 251 | static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ |
| 252 | |
| 253 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 254 | K_SPECIAL and CSI are escaped */ |
| 255 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 256 | 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] | 257 | static int did_restart_edit; /* "restart_edit" when calling edit() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 258 | |
| 259 | #ifdef FEAT_CINDENT |
| 260 | static int can_cindent; /* may do cindenting on this line */ |
| 261 | #endif |
| 262 | |
| 263 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 264 | |
| 265 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 266 | static int revins_on; /* reverse insert mode on */ |
| 267 | static int revins_chars; /* how much to skip after edit */ |
| 268 | static int revins_legal; /* was the last char 'legal'? */ |
| 269 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 270 | #endif |
| 271 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 272 | static int ins_need_undo; /* call u_save() before inserting a |
| 273 | char. Set when edit() is called. |
| 274 | after that arrow_used is used. */ |
| 275 | |
| 276 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 277 | under the cursor */ |
| 278 | |
| 279 | /* |
| 280 | * edit(): Start inserting text. |
| 281 | * |
| 282 | * "cmdchar" can be: |
| 283 | * 'i' normal insert command |
| 284 | * 'a' normal append command |
| 285 | * 'R' replace command |
| 286 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 287 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 288 | * 'g' "gI" command. |
| 289 | * 'V' "gR" command for Virtual Replace mode. |
| 290 | * 'v' "gr" command for single character Virtual Replace mode. |
| 291 | * |
| 292 | * This function is not called recursively. For CTRL-O commands, it returns |
| 293 | * and lets the caller handle the Normal-mode command. |
| 294 | * |
| 295 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 296 | */ |
| 297 | int |
| 298 | edit(cmdchar, startln, count) |
| 299 | int cmdchar; |
| 300 | int startln; /* if set, insert at start of line */ |
| 301 | long count; |
| 302 | { |
| 303 | int c = 0; |
| 304 | char_u *ptr; |
| 305 | int lastc; |
| 306 | colnr_T mincol; |
| 307 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 308 | int i; |
| 309 | int did_backspace = TRUE; /* previous char was backspace */ |
| 310 | #ifdef FEAT_CINDENT |
| 311 | int line_is_white = FALSE; /* line is empty before insert */ |
| 312 | #endif |
| 313 | linenr_T old_topline = 0; /* topline before insertion */ |
| 314 | #ifdef FEAT_DIFF |
| 315 | int old_topfill = -1; |
| 316 | #endif |
| 317 | int inserted_space = FALSE; /* just inserted a space */ |
| 318 | int replaceState = REPLACE; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 319 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 320 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 321 | /* Remember whether editing was restarted after CTRL-O. */ |
| 322 | did_restart_edit = restart_edit; |
| 323 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 325 | * error message */ |
| 326 | check_for_delay(TRUE); |
| 327 | |
| 328 | #ifdef HAVE_SANDBOX |
| 329 | /* Don't allow inserting in the sandbox. */ |
| 330 | if (sandbox != 0) |
| 331 | { |
| 332 | EMSG(_(e_sandbox)); |
| 333 | return FALSE; |
| 334 | } |
| 335 | #endif |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 336 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 337 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 338 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 339 | { |
| 340 | EMSG(_(e_secure)); |
| 341 | return FALSE; |
| 342 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 343 | |
| 344 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 345 | /* Don't allow recursive insert mode when busy with completion. */ |
| 346 | if (compl_started || pum_visible()) |
| 347 | { |
| 348 | EMSG(_(e_secure)); |
| 349 | return FALSE; |
| 350 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 351 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 352 | #endif |
| 353 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 354 | #ifdef FEAT_AUTOCMD |
| 355 | /* |
| 356 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 357 | */ |
| 358 | if (cmdchar != 'r' && cmdchar != 'v') |
| 359 | { |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 360 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 361 | if (cmdchar == 'R') |
| 362 | ptr = (char_u *)"r"; |
| 363 | else if (cmdchar == 'V') |
| 364 | ptr = (char_u *)"v"; |
| 365 | else |
| 366 | ptr = (char_u *)"i"; |
| 367 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 368 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 369 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
| 370 | } |
| 371 | #endif |
| 372 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 373 | #ifdef FEAT_MOUSE |
| 374 | /* |
| 375 | * When doing a paste with the middle mouse button, Insstart is set to |
| 376 | * where the paste started. |
| 377 | */ |
| 378 | if (where_paste_started.lnum != 0) |
| 379 | Insstart = where_paste_started; |
| 380 | else |
| 381 | #endif |
| 382 | { |
| 383 | Insstart = curwin->w_cursor; |
| 384 | if (startln) |
| 385 | Insstart.col = 0; |
| 386 | } |
| 387 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 388 | Insstart_blank_vcol = MAXCOL; |
| 389 | if (!did_ai) |
| 390 | ai_col = 0; |
| 391 | |
| 392 | if (cmdchar != NUL && restart_edit == 0) |
| 393 | { |
| 394 | ResetRedobuff(); |
| 395 | AppendNumberToRedobuff(count); |
| 396 | #ifdef FEAT_VREPLACE |
| 397 | if (cmdchar == 'V' || cmdchar == 'v') |
| 398 | { |
| 399 | /* "gR" or "gr" command */ |
| 400 | AppendCharToRedobuff('g'); |
| 401 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 402 | } |
| 403 | else |
| 404 | #endif |
| 405 | { |
| 406 | AppendCharToRedobuff(cmdchar); |
| 407 | if (cmdchar == 'g') /* "gI" command */ |
| 408 | AppendCharToRedobuff('I'); |
| 409 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 410 | count = 1; /* insert only one <CR> */ |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | if (cmdchar == 'R') |
| 415 | { |
| 416 | #ifdef FEAT_FKMAP |
| 417 | if (p_fkmap && p_ri) |
| 418 | { |
| 419 | beep_flush(); |
| 420 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 421 | State = INSERT; |
| 422 | } |
| 423 | else |
| 424 | #endif |
| 425 | State = REPLACE; |
| 426 | } |
| 427 | #ifdef FEAT_VREPLACE |
| 428 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 429 | { |
| 430 | State = VREPLACE; |
| 431 | replaceState = VREPLACE; |
| 432 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 433 | vr_lines_changed = 1; |
| 434 | } |
| 435 | #endif |
| 436 | else |
| 437 | State = INSERT; |
| 438 | |
| 439 | stop_insert_mode = FALSE; |
| 440 | |
| 441 | /* |
| 442 | * Need to recompute the cursor position, it might move when the cursor is |
| 443 | * on a TAB or special character. |
| 444 | */ |
| 445 | curs_columns(TRUE); |
| 446 | |
| 447 | /* |
| 448 | * Enable langmap or IME, indicated by 'iminsert'. |
| 449 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 450 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 451 | * when hitting <Esc>. |
| 452 | */ |
| 453 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 454 | State |= LANGMAP; |
| 455 | #ifdef USE_IM_CONTROL |
| 456 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 457 | #endif |
| 458 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 459 | #ifdef FEAT_MOUSE |
| 460 | setmouse(); |
| 461 | #endif |
| 462 | #ifdef FEAT_CMDL_INFO |
| 463 | clear_showcmd(); |
| 464 | #endif |
| 465 | #ifdef FEAT_RIGHTLEFT |
| 466 | /* there is no reverse replace mode */ |
| 467 | revins_on = (State == INSERT && p_ri); |
| 468 | if (revins_on) |
| 469 | undisplay_dollar(); |
| 470 | revins_chars = 0; |
| 471 | revins_legal = 0; |
| 472 | revins_scol = -1; |
| 473 | #endif |
| 474 | |
| 475 | /* |
| 476 | * Handle restarting Insert mode. |
| 477 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 478 | * restart_edit non-zero, and something in the stuff buffer. |
| 479 | */ |
| 480 | if (restart_edit != 0 && stuff_empty()) |
| 481 | { |
| 482 | #ifdef FEAT_MOUSE |
| 483 | /* |
| 484 | * After a paste we consider text typed to be part of the insert for |
| 485 | * the pasted text. You can backspace over the pasted text too. |
| 486 | */ |
| 487 | if (where_paste_started.lnum) |
| 488 | arrow_used = FALSE; |
| 489 | else |
| 490 | #endif |
| 491 | arrow_used = TRUE; |
| 492 | restart_edit = 0; |
| 493 | |
| 494 | /* |
| 495 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 496 | * now at the end-of-line, put it after the end-of-line (this is not |
| 497 | * correct in very rare cases). |
| 498 | * Also do this if curswant is greater than the current virtual |
| 499 | * column. Eg after "^O$" or "^O80|". |
| 500 | */ |
| 501 | validate_virtcol(); |
| 502 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 503 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 504 | || curwin->w_curswant > curwin->w_virtcol) |
| 505 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 506 | { |
| 507 | if (ptr[1] == NUL) |
| 508 | ++curwin->w_cursor.col; |
| 509 | #ifdef FEAT_MBYTE |
| 510 | else if (has_mbyte) |
| 511 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 512 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 513 | if (ptr[i] == NUL) |
| 514 | curwin->w_cursor.col += i; |
| 515 | } |
| 516 | #endif |
| 517 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 518 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | arrow_used = FALSE; |
| 522 | |
| 523 | /* we are in insert mode now, don't need to start it anymore */ |
| 524 | need_start_insertmode = FALSE; |
| 525 | |
| 526 | /* Need to save the line for undo before inserting the first char. */ |
| 527 | ins_need_undo = TRUE; |
| 528 | |
| 529 | #ifdef FEAT_MOUSE |
| 530 | where_paste_started.lnum = 0; |
| 531 | #endif |
| 532 | #ifdef FEAT_CINDENT |
| 533 | can_cindent = TRUE; |
| 534 | #endif |
| 535 | #ifdef FEAT_FOLDING |
| 536 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 537 | * restarting. */ |
| 538 | if (!p_im && did_restart_edit == 0) |
| 539 | foldOpenCursor(); |
| 540 | #endif |
| 541 | |
| 542 | /* |
| 543 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 544 | * A warning message for changing a readonly file is given here, before |
| 545 | * actually changing anything. It's put after the mode, if any. |
| 546 | */ |
| 547 | i = 0; |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 548 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 549 | i = showmode(); |
| 550 | |
| 551 | if (!p_im && did_restart_edit == 0) |
| 552 | change_warning(i + 1); |
| 553 | |
| 554 | #ifdef CURSOR_SHAPE |
| 555 | ui_cursor_shape(); /* may show different cursor shape */ |
| 556 | #endif |
| 557 | #ifdef FEAT_DIGRAPHS |
| 558 | do_digraph(-1); /* clear digraphs */ |
| 559 | #endif |
| 560 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 561 | /* |
| 562 | * Get the current length of the redo buffer, those characters have to be |
| 563 | * skipped if we want to get to the inserted characters. |
| 564 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | ptr = get_inserted(); |
| 566 | if (ptr == NULL) |
| 567 | new_insert_skip = 0; |
| 568 | else |
| 569 | { |
| 570 | new_insert_skip = (int)STRLEN(ptr); |
| 571 | vim_free(ptr); |
| 572 | } |
| 573 | |
| 574 | old_indent = 0; |
| 575 | |
| 576 | /* |
| 577 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 578 | */ |
| 579 | for (;;) |
| 580 | { |
| 581 | #ifdef FEAT_RIGHTLEFT |
| 582 | if (!revins_legal) |
| 583 | revins_scol = -1; /* reset on illegal motions */ |
| 584 | else |
| 585 | revins_legal = 0; |
| 586 | #endif |
| 587 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 588 | count = 0; |
| 589 | |
| 590 | if (stop_insert_mode) |
| 591 | { |
| 592 | /* ":stopinsert" used or 'insertmode' reset */ |
| 593 | count = 0; |
| 594 | goto doESCkey; |
| 595 | } |
| 596 | |
| 597 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 598 | if (!arrow_used) |
| 599 | curwin->w_set_curswant = TRUE; |
| 600 | |
| 601 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 602 | * menu invoked a shell command). */ |
| 603 | if (stuff_empty()) |
| 604 | { |
| 605 | did_check_timestamps = FALSE; |
| 606 | if (need_check_timestamps) |
| 607 | check_timestamps(FALSE); |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * When emsg() was called msg_scroll will have been set. |
| 612 | */ |
| 613 | msg_scroll = FALSE; |
| 614 | |
| 615 | #ifdef FEAT_GUI |
| 616 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 617 | * another window. "need_mouse_correct" may then be set because of an |
| 618 | * autocommand. */ |
| 619 | if (need_mouse_correct) |
| 620 | gui_mouse_correct(); |
| 621 | #endif |
| 622 | |
| 623 | #ifdef FEAT_FOLDING |
| 624 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 625 | if (fdo_flags & FDO_INSERT) |
| 626 | foldOpenCursor(); |
| 627 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 628 | if (!char_avail()) |
| 629 | foldCheckClose(); |
| 630 | #endif |
| 631 | |
| 632 | /* |
| 633 | * If we inserted a character at the last position of the last line in |
| 634 | * the window, scroll the window one line up. This avoids an extra |
| 635 | * redraw. |
| 636 | * This is detected when the cursor column is smaller after inserting |
| 637 | * something. |
| 638 | * Don't do this when the topline changed already, it has |
| 639 | * already been adjusted (by insertchar() calling open_line())). |
| 640 | */ |
| 641 | if (curbuf->b_mod_set |
| 642 | && curwin->w_p_wrap |
| 643 | && !did_backspace |
| 644 | && curwin->w_topline == old_topline |
| 645 | #ifdef FEAT_DIFF |
| 646 | && curwin->w_topfill == old_topfill |
| 647 | #endif |
| 648 | ) |
| 649 | { |
| 650 | mincol = curwin->w_wcol; |
| 651 | validate_cursor_col(); |
| 652 | |
| 653 | if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts |
| 654 | && curwin->w_wrow == W_WINROW(curwin) |
| 655 | + curwin->w_height - 1 - p_so |
| 656 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 657 | #ifdef FEAT_DIFF |
| 658 | || curwin->w_topfill > 0 |
| 659 | #endif |
| 660 | )) |
| 661 | { |
| 662 | #ifdef FEAT_DIFF |
| 663 | if (curwin->w_topfill > 0) |
| 664 | --curwin->w_topfill; |
| 665 | else |
| 666 | #endif |
| 667 | #ifdef FEAT_FOLDING |
| 668 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 669 | set_topline(curwin, old_topline + 1); |
| 670 | else |
| 671 | #endif |
| 672 | set_topline(curwin, curwin->w_topline + 1); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /* May need to adjust w_topline to show the cursor. */ |
| 677 | update_topline(); |
| 678 | |
| 679 | did_backspace = FALSE; |
| 680 | |
| 681 | validate_cursor(); /* may set must_redraw */ |
| 682 | |
| 683 | /* |
| 684 | * Redraw the display when no characters are waiting. |
| 685 | * Also shows mode, ruler and positions cursor. |
| 686 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 687 | ins_redraw(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | |
| 689 | #ifdef FEAT_SCROLLBIND |
| 690 | if (curwin->w_p_scb) |
| 691 | do_check_scrollbind(TRUE); |
| 692 | #endif |
| 693 | |
| 694 | update_curswant(); |
| 695 | old_topline = curwin->w_topline; |
| 696 | #ifdef FEAT_DIFF |
| 697 | old_topfill = curwin->w_topfill; |
| 698 | #endif |
| 699 | |
| 700 | #ifdef USE_ON_FLY_SCROLL |
| 701 | dont_scroll = FALSE; /* allow scrolling here */ |
| 702 | #endif |
| 703 | |
| 704 | /* |
| 705 | * Get a character for Insert mode. |
| 706 | */ |
| 707 | lastc = c; /* remember previous char for CTRL-D */ |
| 708 | c = safe_vgetc(); |
| 709 | |
| 710 | #ifdef FEAT_RIGHTLEFT |
| 711 | if (p_hkmap && KeyTyped) |
| 712 | c = hkmap(c); /* Hebrew mode mapping */ |
| 713 | #endif |
| 714 | #ifdef FEAT_FKMAP |
| 715 | if (p_fkmap && KeyTyped) |
| 716 | c = fkmap(c); /* Farsi mode mapping */ |
| 717 | #endif |
| 718 | |
| 719 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 720 | /* |
| 721 | * Special handling of keys while the popup menu is visible or wanted |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 722 | * and the cursor is still in the completed word. Only when there is |
| 723 | * a match, skip this when no matches were found. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 724 | */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 725 | if (compl_started |
| 726 | && pum_wanted() |
| 727 | && curwin->w_cursor.col >= compl_col |
| 728 | && (compl_shown_match == NULL |
| 729 | || compl_shown_match != compl_shown_match->cp_next)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 730 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 731 | /* BS: Delete one character from "compl_leader". */ |
| 732 | if ((c == K_BS || c == Ctrl_H) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 733 | && curwin->w_cursor.col > compl_col |
| 734 | && (c = ins_compl_bs()) == NUL) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 735 | continue; |
| 736 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 737 | /* When no match was selected or it was edited. */ |
| 738 | if (!compl_used_match) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 739 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 740 | /* CTRL-L: Add one character from the current match to |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 741 | * "compl_leader". Except when at the original match and |
| 742 | * there is nothing to add, CTRL-L works like CTRL-P then. */ |
| 743 | if (c == Ctrl_L |
| 744 | && (ctrl_x_mode != CTRL_X_WHOLE_LINE |
| 745 | || STRLEN(compl_shown_match->cp_str) |
| 746 | > curwin->w_cursor.col - compl_col)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 747 | { |
| 748 | ins_compl_addfrommatch(); |
| 749 | continue; |
| 750 | } |
| 751 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 752 | /* A printable, non-white character: Add to "compl_leader". */ |
| 753 | if (vim_isprintc(c) && !vim_iswhite(c)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 754 | { |
| 755 | ins_compl_addleader(c); |
| 756 | continue; |
| 757 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 758 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 759 | /* Pressing CTRL-Y selects the current match. When |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 760 | * compl_enter_selects is set the Enter key does the same. */ |
| 761 | if (c == Ctrl_Y || (compl_enter_selects |
| 762 | && (c == CAR || c == K_KENTER || c == NL))) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 763 | { |
| 764 | ins_compl_delete(); |
| 765 | ins_compl_insert(); |
| 766 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 771 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 772 | compl_get_longest = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 773 | if (c != K_IGNORE && ins_compl_prep(c)) |
| 774 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 775 | #endif |
| 776 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 777 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 778 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 779 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 780 | if (c == Ctrl_BSL) |
| 781 | { |
| 782 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 783 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 784 | ++no_mapping; |
| 785 | ++allow_keys; |
| 786 | c = safe_vgetc(); |
| 787 | --no_mapping; |
| 788 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 789 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 790 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 791 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 792 | vungetc(c); |
| 793 | c = Ctrl_BSL; |
| 794 | } |
| 795 | else if (c == Ctrl_G && p_im) |
| 796 | continue; |
| 797 | else |
| 798 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 799 | if (c == Ctrl_O) |
| 800 | { |
| 801 | ins_ctrl_o(); |
| 802 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 803 | nomove = TRUE; |
| 804 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 805 | count = 0; |
| 806 | goto doESCkey; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | #ifdef FEAT_DIGRAPHS |
| 811 | c = do_digraph(c); |
| 812 | #endif |
| 813 | |
| 814 | #ifdef FEAT_INS_EXPAND |
| 815 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 816 | goto docomplete; |
| 817 | #endif |
| 818 | if (c == Ctrl_V || c == Ctrl_Q) |
| 819 | { |
| 820 | ins_ctrl_v(); |
| 821 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 822 | continue; |
| 823 | } |
| 824 | |
| 825 | #ifdef FEAT_CINDENT |
| 826 | if (cindent_on() |
| 827 | # ifdef FEAT_INS_EXPAND |
| 828 | && ctrl_x_mode == 0 |
| 829 | # endif |
| 830 | ) |
| 831 | { |
| 832 | /* A key name preceded by a bang means this key is not to be |
| 833 | * inserted. Skip ahead to the re-indenting below. |
| 834 | * A key name preceded by a star means that indenting has to be |
| 835 | * done before inserting the key. */ |
| 836 | line_is_white = inindent(0); |
| 837 | if (in_cinkeys(c, '!', line_is_white)) |
| 838 | goto force_cindent; |
| 839 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 840 | && stop_arrow() == OK) |
| 841 | do_c_expr_indent(); |
| 842 | } |
| 843 | #endif |
| 844 | |
| 845 | #ifdef FEAT_RIGHTLEFT |
| 846 | if (curwin->w_p_rl) |
| 847 | switch (c) |
| 848 | { |
| 849 | case K_LEFT: c = K_RIGHT; break; |
| 850 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 851 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 852 | case K_RIGHT: c = K_LEFT; break; |
| 853 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 854 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 855 | } |
| 856 | #endif |
| 857 | |
| 858 | #ifdef FEAT_VISUAL |
| 859 | /* |
| 860 | * If 'keymodel' contains "startsel", may start selection. If it |
| 861 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 862 | * characters. |
| 863 | */ |
| 864 | if (ins_start_select(c)) |
| 865 | continue; |
| 866 | #endif |
| 867 | |
| 868 | /* |
| 869 | * The big switch to handle a character in insert mode. |
| 870 | */ |
| 871 | switch (c) |
| 872 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 873 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 874 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 875 | break; |
| 876 | /*FALLTHROUGH*/ |
| 877 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 878 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | #ifdef FEAT_CMDWIN |
| 880 | if (c == Ctrl_C && cmdwin_type != 0) |
| 881 | { |
| 882 | /* Close the cmdline window. */ |
| 883 | cmdwin_result = K_IGNORE; |
| 884 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 885 | nomove = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 886 | goto doESCkey; |
| 887 | } |
| 888 | #endif |
| 889 | |
| 890 | #ifdef UNIX |
| 891 | do_intr: |
| 892 | #endif |
| 893 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 894 | * Insert mode */ |
| 895 | if (goto_im()) |
| 896 | { |
| 897 | if (got_int) |
| 898 | { |
| 899 | (void)vgetc(); /* flush all buffers */ |
| 900 | got_int = FALSE; |
| 901 | } |
| 902 | else |
| 903 | vim_beep(); |
| 904 | break; |
| 905 | } |
| 906 | doESCkey: |
| 907 | /* |
| 908 | * This is the ONLY return from edit()! |
| 909 | */ |
| 910 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 911 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 912 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 913 | o_lnum = curwin->w_cursor.lnum; |
| 914 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 915 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 916 | { |
| 917 | #ifdef FEAT_AUTOCMD |
| 918 | if (cmdchar != 'r' && cmdchar != 'v') |
| 919 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 920 | FALSE, curbuf); |
| 921 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 922 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 923 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 924 | continue; |
| 925 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 926 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 927 | if (!p_im) |
| 928 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 929 | stuffReadbuff((char_u *)":st\r"); |
| 930 | c = Ctrl_O; |
| 931 | /*FALLTHROUGH*/ |
| 932 | |
| 933 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 934 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 935 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 936 | goto docomplete; |
| 937 | #endif |
| 938 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 939 | break; |
| 940 | ins_ctrl_o(); |
Bram Moolenaar | 06a89a5 | 2006-04-29 22:01:03 +0000 | [diff] [blame] | 941 | |
| 942 | #ifdef FEAT_VIRTUALEDIT |
| 943 | /* don't move the cursor left when 'virtualedit' has "onemore". */ |
| 944 | if (ve_flags & VE_ONEMORE) |
| 945 | { |
| 946 | ins_at_eol = FALSE; |
| 947 | nomove = TRUE; |
| 948 | } |
| 949 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 950 | count = 0; |
| 951 | goto doESCkey; |
| 952 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 953 | case K_INS: /* toggle insert/replace mode */ |
| 954 | case K_KINS: |
| 955 | ins_insert(replaceState); |
| 956 | break; |
| 957 | |
| 958 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 959 | break; |
| 960 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 961 | #ifdef FEAT_SNIFF |
| 962 | case K_SNIFF: /* Sniff command received */ |
| 963 | stuffcharReadbuff(K_SNIFF); |
| 964 | goto doESCkey; |
| 965 | #endif |
| 966 | |
| 967 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 968 | case K_F1: |
| 969 | case K_XF1: |
| 970 | stuffcharReadbuff(K_HELP); |
| 971 | if (p_im) |
| 972 | need_start_insertmode = TRUE; |
| 973 | goto doESCkey; |
| 974 | |
| 975 | #ifdef FEAT_NETBEANS_INTG |
| 976 | case K_F21: /* NetBeans command */ |
| 977 | ++no_mapping; /* don't map the next key hits */ |
| 978 | i = safe_vgetc(); |
| 979 | --no_mapping; |
| 980 | netbeans_keycommand(i); |
| 981 | break; |
| 982 | #endif |
| 983 | |
| 984 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 985 | case NUL: |
| 986 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 987 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 988 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 989 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 990 | && c != Ctrl_A && !p_im) |
| 991 | goto doESCkey; /* quit insert mode */ |
| 992 | inserted_space = FALSE; |
| 993 | break; |
| 994 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 995 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 996 | ins_reg(); |
| 997 | auto_format(FALSE, TRUE); |
| 998 | inserted_space = FALSE; |
| 999 | break; |
| 1000 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1001 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1002 | ins_ctrl_g(); |
| 1003 | break; |
| 1004 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1005 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 1006 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | |
| 1009 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1010 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1011 | if (!p_ari) |
| 1012 | goto normalchar; |
| 1013 | ins_ctrl_(); |
| 1014 | break; |
| 1015 | #endif |
| 1016 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1017 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1018 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1019 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 1020 | goto docomplete; |
| 1021 | #endif |
| 1022 | /* FALLTHROUGH */ |
| 1023 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1024 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1025 | # ifdef FEAT_INS_EXPAND |
| 1026 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 1027 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1028 | if (has_compl_option(FALSE)) |
| 1029 | goto docomplete; |
| 1030 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1031 | } |
| 1032 | # endif |
| 1033 | ins_shift(c, lastc); |
| 1034 | auto_format(FALSE, TRUE); |
| 1035 | inserted_space = FALSE; |
| 1036 | break; |
| 1037 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1038 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1039 | case K_KDEL: |
| 1040 | ins_del(); |
| 1041 | auto_format(FALSE, TRUE); |
| 1042 | break; |
| 1043 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1044 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | case Ctrl_H: |
| 1046 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 1047 | auto_format(FALSE, TRUE); |
| 1048 | break; |
| 1049 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1050 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 1052 | auto_format(FALSE, TRUE); |
| 1053 | break; |
| 1054 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1055 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1056 | # ifdef FEAT_COMPL_FUNC |
| 1057 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1058 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1059 | goto docomplete; |
| 1060 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1061 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 1062 | auto_format(FALSE, TRUE); |
| 1063 | inserted_space = FALSE; |
| 1064 | break; |
| 1065 | |
| 1066 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1067 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1068 | case K_LEFTMOUSE_NM: |
| 1069 | case K_LEFTDRAG: |
| 1070 | case K_LEFTRELEASE: |
| 1071 | case K_LEFTRELEASE_NM: |
| 1072 | case K_MIDDLEMOUSE: |
| 1073 | case K_MIDDLEDRAG: |
| 1074 | case K_MIDDLERELEASE: |
| 1075 | case K_RIGHTMOUSE: |
| 1076 | case K_RIGHTDRAG: |
| 1077 | case K_RIGHTRELEASE: |
| 1078 | case K_X1MOUSE: |
| 1079 | case K_X1DRAG: |
| 1080 | case K_X1RELEASE: |
| 1081 | case K_X2MOUSE: |
| 1082 | case K_X2DRAG: |
| 1083 | case K_X2RELEASE: |
| 1084 | ins_mouse(c); |
| 1085 | break; |
| 1086 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1087 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | ins_mousescroll(FALSE); |
| 1089 | break; |
| 1090 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1091 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1092 | ins_mousescroll(TRUE); |
| 1093 | break; |
| 1094 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1095 | #ifdef FEAT_GUI_TABLINE |
| 1096 | case K_TABLINE: |
| 1097 | case K_TABMENU: |
| 1098 | ins_tabline(c); |
| 1099 | break; |
| 1100 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1101 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1102 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1105 | #ifdef FEAT_AUTOCMD |
| 1106 | case K_CURSORHOLD: /* Didn't type something for a while. */ |
| 1107 | apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); |
| 1108 | did_cursorhold = TRUE; |
| 1109 | break; |
| 1110 | #endif |
| 1111 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1112 | #ifdef FEAT_GUI_W32 |
| 1113 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1114 | * cancelled. */ |
| 1115 | case K_F4: |
| 1116 | if (mod_mask != MOD_MASK_ALT) |
| 1117 | goto normalchar; |
| 1118 | break; |
| 1119 | #endif |
| 1120 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1121 | #ifdef FEAT_GUI |
| 1122 | case K_VER_SCROLLBAR: |
| 1123 | ins_scroll(); |
| 1124 | break; |
| 1125 | |
| 1126 | case K_HOR_SCROLLBAR: |
| 1127 | ins_horscroll(); |
| 1128 | break; |
| 1129 | #endif |
| 1130 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1131 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1133 | case K_S_HOME: |
| 1134 | case K_C_HOME: |
| 1135 | ins_home(c); |
| 1136 | break; |
| 1137 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1138 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1139 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | case K_S_END: |
| 1141 | case K_C_END: |
| 1142 | ins_end(c); |
| 1143 | break; |
| 1144 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1145 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1146 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1147 | ins_s_left(); |
| 1148 | else |
| 1149 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1152 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | case K_C_LEFT: |
| 1154 | ins_s_left(); |
| 1155 | break; |
| 1156 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1157 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1158 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1159 | ins_s_right(); |
| 1160 | else |
| 1161 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1162 | break; |
| 1163 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1164 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1165 | case K_C_RIGHT: |
| 1166 | ins_s_right(); |
| 1167 | break; |
| 1168 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1169 | case K_UP: /* <Up> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1170 | #ifdef FEAT_INS_EXPAND |
| 1171 | if (pum_visible()) |
| 1172 | goto docomplete; |
| 1173 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1174 | if (mod_mask & MOD_MASK_SHIFT) |
| 1175 | ins_pageup(); |
| 1176 | else |
| 1177 | ins_up(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1180 | case K_S_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1181 | case K_PAGEUP: |
| 1182 | case K_KPAGEUP: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1183 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1184 | if (pum_visible()) |
| 1185 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1186 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | ins_pageup(); |
| 1188 | break; |
| 1189 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1190 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1191 | #ifdef FEAT_INS_EXPAND |
| 1192 | if (pum_visible()) |
| 1193 | goto docomplete; |
| 1194 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1195 | if (mod_mask & MOD_MASK_SHIFT) |
| 1196 | ins_pagedown(); |
| 1197 | else |
| 1198 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1201 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1202 | case K_PAGEDOWN: |
| 1203 | case K_KPAGEDOWN: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1204 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1205 | if (pum_visible()) |
| 1206 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1207 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1208 | ins_pagedown(); |
| 1209 | break; |
| 1210 | |
| 1211 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1212 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1213 | ins_drop(); |
| 1214 | break; |
| 1215 | #endif |
| 1216 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1217 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | c = TAB; |
| 1219 | /* FALLTHROUGH */ |
| 1220 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1221 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1222 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1223 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1224 | goto docomplete; |
| 1225 | #endif |
| 1226 | inserted_space = FALSE; |
| 1227 | if (ins_tab()) |
| 1228 | goto normalchar; /* insert TAB as a normal char */ |
| 1229 | auto_format(FALSE, TRUE); |
| 1230 | break; |
| 1231 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1232 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1233 | c = CAR; |
| 1234 | /* FALLTHROUGH */ |
| 1235 | case CAR: |
| 1236 | case NL: |
| 1237 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1238 | /* In a quickfix window a <CR> jumps to the error under the |
| 1239 | * cursor. */ |
| 1240 | if (bt_quickfix(curbuf) && c == CAR) |
| 1241 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1242 | if (curwin->w_llist_ref == NULL) /* quickfix window */ |
| 1243 | do_cmdline_cmd((char_u *)".cc"); |
| 1244 | else /* location list window */ |
| 1245 | do_cmdline_cmd((char_u *)".ll"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | #endif |
| 1249 | #ifdef FEAT_CMDWIN |
| 1250 | if (cmdwin_type != 0) |
| 1251 | { |
| 1252 | /* Execute the command in the cmdline window. */ |
| 1253 | cmdwin_result = CAR; |
| 1254 | goto doESCkey; |
| 1255 | } |
| 1256 | #endif |
| 1257 | if (ins_eol(c) && !p_im) |
| 1258 | goto doESCkey; /* out of memory */ |
| 1259 | auto_format(FALSE, FALSE); |
| 1260 | inserted_space = FALSE; |
| 1261 | break; |
| 1262 | |
| 1263 | #if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1264 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1265 | # ifdef FEAT_INS_EXPAND |
| 1266 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1267 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1268 | if (has_compl_option(TRUE)) |
| 1269 | goto docomplete; |
| 1270 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1271 | } |
| 1272 | # endif |
| 1273 | # ifdef FEAT_DIGRAPHS |
| 1274 | c = ins_digraph(); |
| 1275 | if (c == NUL) |
| 1276 | break; |
| 1277 | # endif |
| 1278 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1279 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1280 | |
| 1281 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1282 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1283 | ins_ctrl_x(); |
| 1284 | break; |
| 1285 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1286 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1287 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1288 | goto normalchar; |
| 1289 | goto docomplete; |
| 1290 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1291 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1292 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1293 | goto normalchar; |
| 1294 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1295 | |
| 1296 | case 's': /* Spelling completion after ^X */ |
| 1297 | case Ctrl_S: |
| 1298 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1299 | goto normalchar; |
| 1300 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | #endif |
| 1302 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1303 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1304 | #ifdef FEAT_INS_EXPAND |
| 1305 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1306 | #endif |
| 1307 | { |
| 1308 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1309 | if (p_im) |
| 1310 | { |
| 1311 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1312 | break; |
| 1313 | goto doESCkey; |
| 1314 | } |
| 1315 | goto normalchar; |
| 1316 | } |
| 1317 | #ifdef FEAT_INS_EXPAND |
| 1318 | /* FALLTHROUGH */ |
| 1319 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1320 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | case Ctrl_N: |
| 1322 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1323 | * but it is under other ^X modes */ |
| 1324 | if (*curbuf->b_p_cpt == NUL |
| 1325 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1326 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1327 | goto normalchar; |
| 1328 | |
| 1329 | docomplete: |
| 1330 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1331 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1332 | break; |
| 1333 | #endif /* FEAT_INS_EXPAND */ |
| 1334 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1335 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1336 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1337 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | |
| 1340 | default: |
| 1341 | #ifdef UNIX |
| 1342 | if (c == intr_char) /* special interrupt char */ |
| 1343 | goto do_intr; |
| 1344 | #endif |
| 1345 | |
| 1346 | /* |
| 1347 | * Insert a nomal character. |
| 1348 | */ |
| 1349 | normalchar: |
| 1350 | #ifdef FEAT_SMARTINDENT |
| 1351 | /* Try to perform smart-indenting. */ |
| 1352 | ins_try_si(c); |
| 1353 | #endif |
| 1354 | |
| 1355 | if (c == ' ') |
| 1356 | { |
| 1357 | inserted_space = TRUE; |
| 1358 | #ifdef FEAT_CINDENT |
| 1359 | if (inindent(0)) |
| 1360 | can_cindent = FALSE; |
| 1361 | #endif |
| 1362 | if (Insstart_blank_vcol == MAXCOL |
| 1363 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1364 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1365 | } |
| 1366 | |
| 1367 | if (vim_iswordc(c) || !echeck_abbr( |
| 1368 | #ifdef FEAT_MBYTE |
| 1369 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1370 | * what check_abbr() expects. */ |
| 1371 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1372 | #endif |
| 1373 | c)) |
| 1374 | { |
| 1375 | insert_special(c, FALSE, FALSE); |
| 1376 | #ifdef FEAT_RIGHTLEFT |
| 1377 | revins_legal++; |
| 1378 | revins_chars++; |
| 1379 | #endif |
| 1380 | } |
| 1381 | |
| 1382 | auto_format(FALSE, TRUE); |
| 1383 | |
| 1384 | #ifdef FEAT_FOLDING |
| 1385 | /* When inserting a character the cursor line must never be in a |
| 1386 | * closed fold. */ |
| 1387 | foldOpenCursor(); |
| 1388 | #endif |
| 1389 | break; |
| 1390 | } /* end of switch (c) */ |
| 1391 | |
| 1392 | /* If the cursor was moved we didn't just insert a space */ |
| 1393 | if (arrow_used) |
| 1394 | inserted_space = FALSE; |
| 1395 | |
| 1396 | #ifdef FEAT_CINDENT |
| 1397 | if (can_cindent && cindent_on() |
| 1398 | # ifdef FEAT_INS_EXPAND |
| 1399 | && ctrl_x_mode == 0 |
| 1400 | # endif |
| 1401 | ) |
| 1402 | { |
| 1403 | force_cindent: |
| 1404 | /* |
| 1405 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1406 | */ |
| 1407 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1408 | { |
| 1409 | if (stop_arrow() == OK) |
| 1410 | /* re-indent the current line */ |
| 1411 | do_c_expr_indent(); |
| 1412 | } |
| 1413 | } |
| 1414 | #endif /* FEAT_CINDENT */ |
| 1415 | |
| 1416 | } /* for (;;) */ |
| 1417 | /* NOTREACHED */ |
| 1418 | } |
| 1419 | |
| 1420 | /* |
| 1421 | * Redraw for Insert mode. |
| 1422 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1423 | * option work correctly. |
| 1424 | * Only redraw when there are no characters available. This speeds up |
| 1425 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1426 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1427 | /*ARGSUSED*/ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1428 | static void |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1429 | ins_redraw(ready) |
| 1430 | int ready; /* not busy with something */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1431 | { |
| 1432 | if (!char_avail()) |
| 1433 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1434 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1435 | /* Trigger CursorMoved if the cursor moved. Not when the popup menu is |
| 1436 | * visible, the command might delete it. */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1437 | if (ready && has_cursormovedI() |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1438 | && !equalpos(last_cursormoved, curwin->w_cursor) |
| 1439 | # ifdef FEAT_INS_EXPAND |
| 1440 | && !pum_visible() |
| 1441 | # endif |
| 1442 | ) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1443 | { |
| 1444 | apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); |
| 1445 | last_cursormoved = curwin->w_cursor; |
| 1446 | } |
| 1447 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | if (must_redraw) |
| 1449 | update_screen(0); |
| 1450 | else if (clear_cmdline || redraw_cmdline) |
| 1451 | showmode(); /* clear cmdline and show mode */ |
| 1452 | showruler(FALSE); |
| 1453 | setcursor(); |
| 1454 | emsg_on_display = FALSE; /* may remove error message now */ |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1460 | */ |
| 1461 | static void |
| 1462 | ins_ctrl_v() |
| 1463 | { |
| 1464 | int c; |
| 1465 | |
| 1466 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1467 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1468 | |
| 1469 | if (redrawing() && !char_avail()) |
| 1470 | edit_putchar('^', TRUE); |
| 1471 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1472 | |
| 1473 | #ifdef FEAT_CMDL_INFO |
| 1474 | add_to_showcmd_c(Ctrl_V); |
| 1475 | #endif |
| 1476 | |
| 1477 | c = get_literal(); |
| 1478 | #ifdef FEAT_CMDL_INFO |
| 1479 | clear_showcmd(); |
| 1480 | #endif |
| 1481 | insert_special(c, FALSE, TRUE); |
| 1482 | #ifdef FEAT_RIGHTLEFT |
| 1483 | revins_chars++; |
| 1484 | revins_legal++; |
| 1485 | #endif |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1490 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1491 | */ |
| 1492 | static int pc_status; |
| 1493 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1494 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1495 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1496 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
| 1497 | #ifdef FEAT_MBYTE |
| 1498 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
| 1499 | #else |
| 1500 | static char_u pc_bytes[2]; /* saved bytes */ |
| 1501 | #endif |
| 1502 | static int pc_attr; |
| 1503 | static int pc_row; |
| 1504 | static int pc_col; |
| 1505 | |
| 1506 | void |
| 1507 | edit_putchar(c, highlight) |
| 1508 | int c; |
| 1509 | int highlight; |
| 1510 | { |
| 1511 | int attr; |
| 1512 | |
| 1513 | if (ScreenLines != NULL) |
| 1514 | { |
| 1515 | update_topline(); /* just in case w_topline isn't valid */ |
| 1516 | validate_cursor(); |
| 1517 | if (highlight) |
| 1518 | attr = hl_attr(HLF_8); |
| 1519 | else |
| 1520 | attr = 0; |
| 1521 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1522 | pc_col = W_WINCOL(curwin); |
| 1523 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1524 | pc_status = PC_STATUS_UNSET; |
| 1525 | #endif |
| 1526 | #ifdef FEAT_RIGHTLEFT |
| 1527 | if (curwin->w_p_rl) |
| 1528 | { |
| 1529 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1530 | # ifdef FEAT_MBYTE |
| 1531 | if (has_mbyte) |
| 1532 | { |
| 1533 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1534 | |
| 1535 | if (fix_col != pc_col) |
| 1536 | { |
| 1537 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1538 | --curwin->w_wcol; |
| 1539 | pc_status = PC_STATUS_RIGHT; |
| 1540 | } |
| 1541 | } |
| 1542 | # endif |
| 1543 | } |
| 1544 | else |
| 1545 | #endif |
| 1546 | { |
| 1547 | pc_col += curwin->w_wcol; |
| 1548 | #ifdef FEAT_MBYTE |
| 1549 | if (mb_lefthalve(pc_row, pc_col)) |
| 1550 | pc_status = PC_STATUS_LEFT; |
| 1551 | #endif |
| 1552 | } |
| 1553 | |
| 1554 | /* save the character to be able to put it back */ |
| 1555 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1556 | if (pc_status == PC_STATUS_UNSET) |
| 1557 | #endif |
| 1558 | { |
| 1559 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1560 | pc_status = PC_STATUS_SET; |
| 1561 | } |
| 1562 | screen_putchar(c, pc_row, pc_col, attr); |
| 1563 | } |
| 1564 | } |
| 1565 | |
| 1566 | /* |
| 1567 | * Undo the previous edit_putchar(). |
| 1568 | */ |
| 1569 | void |
| 1570 | edit_unputchar() |
| 1571 | { |
| 1572 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1573 | { |
| 1574 | #if defined(FEAT_MBYTE) |
| 1575 | if (pc_status == PC_STATUS_RIGHT) |
| 1576 | ++curwin->w_wcol; |
| 1577 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1578 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1579 | else |
| 1580 | #endif |
| 1581 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1582 | } |
| 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1587 | * Only works when cursor is in the line that changes. |
| 1588 | */ |
| 1589 | void |
| 1590 | display_dollar(col) |
| 1591 | colnr_T col; |
| 1592 | { |
| 1593 | colnr_T save_col; |
| 1594 | |
| 1595 | if (!redrawing()) |
| 1596 | return; |
| 1597 | |
| 1598 | cursor_off(); |
| 1599 | save_col = curwin->w_cursor.col; |
| 1600 | curwin->w_cursor.col = col; |
| 1601 | #ifdef FEAT_MBYTE |
| 1602 | if (has_mbyte) |
| 1603 | { |
| 1604 | char_u *p; |
| 1605 | |
| 1606 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1607 | p = ml_get_curline(); |
| 1608 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1609 | } |
| 1610 | #endif |
| 1611 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1612 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1613 | { |
| 1614 | edit_putchar('$', FALSE); |
| 1615 | dollar_vcol = curwin->w_virtcol; |
| 1616 | } |
| 1617 | curwin->w_cursor.col = save_col; |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * Call this function before moving the cursor from the normal insert position |
| 1622 | * in insert mode. |
| 1623 | */ |
| 1624 | static void |
| 1625 | undisplay_dollar() |
| 1626 | { |
| 1627 | if (dollar_vcol) |
| 1628 | { |
| 1629 | dollar_vcol = 0; |
| 1630 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1636 | * Keep the cursor on the same character. |
| 1637 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1638 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1639 | * type == INDENT_SET set indent to "amount" |
| 1640 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1641 | */ |
| 1642 | void |
| 1643 | change_indent(type, amount, round, replaced) |
| 1644 | int type; |
| 1645 | int amount; |
| 1646 | int round; |
| 1647 | int replaced; /* replaced character, put on replace stack */ |
| 1648 | { |
| 1649 | int vcol; |
| 1650 | int last_vcol; |
| 1651 | int insstart_less; /* reduction for Insstart.col */ |
| 1652 | int new_cursor_col; |
| 1653 | int i; |
| 1654 | char_u *ptr; |
| 1655 | int save_p_list; |
| 1656 | int start_col; |
| 1657 | colnr_T vc; |
| 1658 | #ifdef FEAT_VREPLACE |
| 1659 | colnr_T orig_col = 0; /* init for GCC */ |
| 1660 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1661 | |
| 1662 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1663 | if (State & VREPLACE_FLAG) |
| 1664 | { |
| 1665 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1666 | orig_col = curwin->w_cursor.col; |
| 1667 | } |
| 1668 | #endif |
| 1669 | |
| 1670 | /* for the following tricks we don't want list mode */ |
| 1671 | save_p_list = curwin->w_p_list; |
| 1672 | curwin->w_p_list = FALSE; |
| 1673 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1674 | vcol = vc; |
| 1675 | |
| 1676 | /* |
| 1677 | * For Replace mode we need to fix the replace stack later, which is only |
| 1678 | * possible when the cursor is in the indent. Remember the number of |
| 1679 | * characters before the cursor if it's possible. |
| 1680 | */ |
| 1681 | start_col = curwin->w_cursor.col; |
| 1682 | |
| 1683 | /* determine offset from first non-blank */ |
| 1684 | new_cursor_col = curwin->w_cursor.col; |
| 1685 | beginline(BL_WHITE); |
| 1686 | new_cursor_col -= curwin->w_cursor.col; |
| 1687 | |
| 1688 | insstart_less = curwin->w_cursor.col; |
| 1689 | |
| 1690 | /* |
| 1691 | * If the cursor is in the indent, compute how many screen columns the |
| 1692 | * cursor is to the left of the first non-blank. |
| 1693 | */ |
| 1694 | if (new_cursor_col < 0) |
| 1695 | vcol = get_indent() - vcol; |
| 1696 | |
| 1697 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1698 | start_col = -1; |
| 1699 | |
| 1700 | /* |
| 1701 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1702 | */ |
| 1703 | if (type == INDENT_SET) |
| 1704 | (void)set_indent(amount, SIN_CHANGED); |
| 1705 | else |
| 1706 | { |
| 1707 | #ifdef FEAT_VREPLACE |
| 1708 | int save_State = State; |
| 1709 | |
| 1710 | /* Avoid being called recursively. */ |
| 1711 | if (State & VREPLACE_FLAG) |
| 1712 | State = INSERT; |
| 1713 | #endif |
| 1714 | shift_line(type == INDENT_DEC, round, 1); |
| 1715 | #ifdef FEAT_VREPLACE |
| 1716 | State = save_State; |
| 1717 | #endif |
| 1718 | } |
| 1719 | insstart_less -= curwin->w_cursor.col; |
| 1720 | |
| 1721 | /* |
| 1722 | * Try to put cursor on same character. |
| 1723 | * If the cursor is at or after the first non-blank in the line, |
| 1724 | * compute the cursor column relative to the column of the first |
| 1725 | * non-blank character. |
| 1726 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1727 | * If the cursor is before the first non-blank, position it relative |
| 1728 | * to the first non-blank, counted in screen columns. |
| 1729 | */ |
| 1730 | if (new_cursor_col >= 0) |
| 1731 | { |
| 1732 | /* |
| 1733 | * When changing the indent while the cursor is touching it, reset |
| 1734 | * Insstart_col to 0. |
| 1735 | */ |
| 1736 | if (new_cursor_col == 0) |
| 1737 | insstart_less = MAXCOL; |
| 1738 | new_cursor_col += curwin->w_cursor.col; |
| 1739 | } |
| 1740 | else if (!(State & INSERT)) |
| 1741 | new_cursor_col = curwin->w_cursor.col; |
| 1742 | else |
| 1743 | { |
| 1744 | /* |
| 1745 | * Compute the screen column where the cursor should be. |
| 1746 | */ |
| 1747 | vcol = get_indent() - vcol; |
| 1748 | curwin->w_virtcol = (vcol < 0) ? 0 : vcol; |
| 1749 | |
| 1750 | /* |
| 1751 | * Advance the cursor until we reach the right screen column. |
| 1752 | */ |
| 1753 | vcol = last_vcol = 0; |
| 1754 | new_cursor_col = -1; |
| 1755 | ptr = ml_get_curline(); |
| 1756 | while (vcol <= (int)curwin->w_virtcol) |
| 1757 | { |
| 1758 | last_vcol = vcol; |
| 1759 | #ifdef FEAT_MBYTE |
| 1760 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1761 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1762 | else |
| 1763 | #endif |
| 1764 | ++new_cursor_col; |
| 1765 | vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol); |
| 1766 | } |
| 1767 | vcol = last_vcol; |
| 1768 | |
| 1769 | /* |
| 1770 | * May need to insert spaces to be able to position the cursor on |
| 1771 | * the right screen column. |
| 1772 | */ |
| 1773 | if (vcol != (int)curwin->w_virtcol) |
| 1774 | { |
| 1775 | curwin->w_cursor.col = new_cursor_col; |
| 1776 | i = (int)curwin->w_virtcol - vcol; |
| 1777 | ptr = alloc(i + 1); |
| 1778 | if (ptr != NULL) |
| 1779 | { |
| 1780 | new_cursor_col += i; |
| 1781 | ptr[i] = NUL; |
| 1782 | while (--i >= 0) |
| 1783 | ptr[i] = ' '; |
| 1784 | ins_str(ptr); |
| 1785 | vim_free(ptr); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * When changing the indent while the cursor is in it, reset |
| 1791 | * Insstart_col to 0. |
| 1792 | */ |
| 1793 | insstart_less = MAXCOL; |
| 1794 | } |
| 1795 | |
| 1796 | curwin->w_p_list = save_p_list; |
| 1797 | |
| 1798 | if (new_cursor_col <= 0) |
| 1799 | curwin->w_cursor.col = 0; |
| 1800 | else |
| 1801 | curwin->w_cursor.col = new_cursor_col; |
| 1802 | curwin->w_set_curswant = TRUE; |
| 1803 | changed_cline_bef_curs(); |
| 1804 | |
| 1805 | /* |
| 1806 | * May have to adjust the start of the insert. |
| 1807 | */ |
| 1808 | if (State & INSERT) |
| 1809 | { |
| 1810 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1811 | { |
| 1812 | if ((int)Insstart.col <= insstart_less) |
| 1813 | Insstart.col = 0; |
| 1814 | else |
| 1815 | Insstart.col -= insstart_less; |
| 1816 | } |
| 1817 | if ((int)ai_col <= insstart_less) |
| 1818 | ai_col = 0; |
| 1819 | else |
| 1820 | ai_col -= insstart_less; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 1825 | * If the number of characters before the cursor decreased, need to pop a |
| 1826 | * few characters from the replace stack. |
| 1827 | * If the number of characters before the cursor increased, need to push a |
| 1828 | * few NULs onto the replace stack. |
| 1829 | */ |
| 1830 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1831 | { |
| 1832 | while (start_col > (int)curwin->w_cursor.col) |
| 1833 | { |
| 1834 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1835 | --start_col; |
| 1836 | } |
| 1837 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1838 | { |
| 1839 | replace_push(NUL); |
| 1840 | if (replaced) |
| 1841 | { |
| 1842 | replace_push(replaced); |
| 1843 | replaced = NUL; |
| 1844 | } |
| 1845 | ++start_col; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | #ifdef FEAT_VREPLACE |
| 1850 | /* |
| 1851 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 1852 | * it is always possible because we backspace over the whole line and then |
| 1853 | * put it back again the way we wanted it. |
| 1854 | */ |
| 1855 | if (State & VREPLACE_FLAG) |
| 1856 | { |
| 1857 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 1858 | * even if you can't backspace. */ |
| 1859 | if (orig_line == NULL) |
| 1860 | return; |
| 1861 | |
| 1862 | /* Save new line */ |
| 1863 | new_line = vim_strsave(ml_get_curline()); |
| 1864 | if (new_line == NULL) |
| 1865 | return; |
| 1866 | |
| 1867 | /* We only put back the new line up to the cursor */ |
| 1868 | new_line[curwin->w_cursor.col] = NUL; |
| 1869 | |
| 1870 | /* Put back original line */ |
| 1871 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 1872 | curwin->w_cursor.col = orig_col; |
| 1873 | |
| 1874 | /* Backspace from cursor to start of line */ |
| 1875 | backspace_until_column(0); |
| 1876 | |
| 1877 | /* Insert new stuff into line again */ |
| 1878 | ins_bytes(new_line); |
| 1879 | |
| 1880 | vim_free(new_line); |
| 1881 | } |
| 1882 | #endif |
| 1883 | } |
| 1884 | |
| 1885 | /* |
| 1886 | * Truncate the space at the end of a line. This is to be used only in an |
| 1887 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 1888 | * modes. |
| 1889 | */ |
| 1890 | void |
| 1891 | truncate_spaces(line) |
| 1892 | char_u *line; |
| 1893 | { |
| 1894 | int i; |
| 1895 | |
| 1896 | /* find start of trailing white space */ |
| 1897 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 1898 | { |
| 1899 | if (State & REPLACE_FLAG) |
| 1900 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1901 | } |
| 1902 | line[i + 1] = NUL; |
| 1903 | } |
| 1904 | |
| 1905 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 1906 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 1907 | /* |
| 1908 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 1909 | * modes correctly. May also be used when not in insert mode at all. |
| 1910 | */ |
| 1911 | void |
| 1912 | backspace_until_column(col) |
| 1913 | int col; |
| 1914 | { |
| 1915 | while ((int)curwin->w_cursor.col > col) |
| 1916 | { |
| 1917 | curwin->w_cursor.col--; |
| 1918 | if (State & REPLACE_FLAG) |
| 1919 | replace_do_bs(); |
| 1920 | else |
| 1921 | (void)del_char(FALSE); |
| 1922 | } |
| 1923 | } |
| 1924 | #endif |
| 1925 | |
| 1926 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 1927 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1928 | * CTRL-X pressed in Insert mode. |
| 1929 | */ |
| 1930 | static void |
| 1931 | ins_ctrl_x() |
| 1932 | { |
| 1933 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 1934 | * CTRL-V works like CTRL-N */ |
| 1935 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 1936 | { |
| 1937 | /* if the next ^X<> won't ADD nothing, then reset |
| 1938 | * compl_cont_status */ |
| 1939 | if (compl_cont_status & CONT_N_ADDS) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1940 | compl_cont_status |= CONT_INTRPT; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1941 | else |
| 1942 | compl_cont_status = 0; |
| 1943 | /* We're not sure which CTRL-X mode it will be yet */ |
| 1944 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 1945 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 1946 | edit_submode_pre = NULL; |
| 1947 | showmode(); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | /* |
| 1952 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 1953 | */ |
| 1954 | static int |
| 1955 | has_compl_option(dict_opt) |
| 1956 | int dict_opt; |
| 1957 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 1958 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 1959 | # ifdef FEAT_SPELL |
| 1960 | && !curwin->w_p_spell |
| 1961 | # endif |
| 1962 | ) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1963 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 1964 | { |
| 1965 | ctrl_x_mode = 0; |
| 1966 | edit_submode = NULL; |
| 1967 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 1968 | : (char_u *)_("'thesaurus' option is empty"), |
| 1969 | hl_attr(HLF_E)); |
| 1970 | if (emsg_silent == 0) |
| 1971 | { |
| 1972 | vim_beep(); |
| 1973 | setcursor(); |
| 1974 | out_flush(); |
| 1975 | ui_delay(2000L, FALSE); |
| 1976 | } |
| 1977 | return FALSE; |
| 1978 | } |
| 1979 | return TRUE; |
| 1980 | } |
| 1981 | |
| 1982 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1983 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 1984 | * This depends on the current mode. |
| 1985 | */ |
| 1986 | int |
| 1987 | vim_is_ctrl_x_key(c) |
| 1988 | int c; |
| 1989 | { |
| 1990 | /* Always allow ^R - let it's results then be checked */ |
| 1991 | if (c == Ctrl_R) |
| 1992 | return TRUE; |
| 1993 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1994 | /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1995 | if (ins_compl_pum_key(c)) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1996 | return TRUE; |
| 1997 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1998 | switch (ctrl_x_mode) |
| 1999 | { |
| 2000 | case 0: /* Not in any CTRL-X mode */ |
| 2001 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 2002 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2003 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2004 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 2005 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 2006 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2007 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
| 2008 | || c == Ctrl_S || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2009 | case CTRL_X_SCROLL: |
| 2010 | return (c == Ctrl_Y || c == Ctrl_E); |
| 2011 | case CTRL_X_WHOLE_LINE: |
| 2012 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 2013 | case CTRL_X_FILES: |
| 2014 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 2015 | case CTRL_X_DICTIONARY: |
| 2016 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 2017 | case CTRL_X_THESAURUS: |
| 2018 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 2019 | case CTRL_X_TAGS: |
| 2020 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 2021 | #ifdef FEAT_FIND_ID |
| 2022 | case CTRL_X_PATH_PATTERNS: |
| 2023 | return (c == Ctrl_P || c == Ctrl_N); |
| 2024 | case CTRL_X_PATH_DEFINES: |
| 2025 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 2026 | #endif |
| 2027 | case CTRL_X_CMDLINE: |
| 2028 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 2029 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2030 | #ifdef FEAT_COMPL_FUNC |
| 2031 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2032 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2033 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2034 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2035 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2036 | case CTRL_X_SPELL: |
| 2037 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2038 | } |
| 2039 | EMSG(_(e_internal)); |
| 2040 | return FALSE; |
| 2041 | } |
| 2042 | |
| 2043 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2044 | * 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] | 2045 | * case of the originally typed text is used, and the case of the completed |
| 2046 | * text is infered, ie this tries to work out what case you probably wanted |
| 2047 | * the rest of the word to be in -- webb |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2048 | * TODO: make this work for multi-byte characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | */ |
| 2050 | int |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2051 | ins_compl_add_infercase(str, len, icase, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2052 | char_u *str; |
| 2053 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2054 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2055 | char_u *fname; |
| 2056 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2057 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | { |
| 2059 | int has_lower = FALSE; |
| 2060 | int was_letter = FALSE; |
| 2061 | int idx; |
| 2062 | |
| 2063 | if (p_ic && curbuf->b_p_inf && len < IOSIZE) |
| 2064 | { |
| 2065 | /* Infer case of completed part -- webb */ |
| 2066 | /* Use IObuff, str would change text in buffer! */ |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 2067 | vim_strncpy(IObuff, str, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | |
| 2069 | /* Rule 1: Were any chars converted to lower? */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2070 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2071 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2072 | if (islower(compl_orig_text[idx])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2073 | { |
| 2074 | has_lower = TRUE; |
| 2075 | if (isupper(IObuff[idx])) |
| 2076 | { |
| 2077 | /* Rule 1 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2078 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2079 | IObuff[idx] = TOLOWER_LOC(IObuff[idx]); |
| 2080 | break; |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | /* |
| 2086 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 2087 | * upper case. |
| 2088 | */ |
| 2089 | if (!has_lower) |
| 2090 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2091 | for (idx = 0; idx < compl_length; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2092 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2093 | if (was_letter && isupper(compl_orig_text[idx]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2094 | && islower(IObuff[idx])) |
| 2095 | { |
| 2096 | /* Rule 2 is satisfied */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2097 | for (idx = compl_length; idx < len; ++idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | IObuff[idx] = TOUPPER_LOC(IObuff[idx]); |
| 2099 | break; |
| 2100 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2101 | was_letter = isalpha(compl_orig_text[idx]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | /* Copy the original case of the part we typed */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2106 | STRNCPY(IObuff, compl_orig_text, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2107 | |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2108 | return ins_compl_add(IObuff, len, icase, fname, NULL, dir, |
| 2109 | flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2110 | } |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2111 | return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /* |
| 2115 | * Add a match to the list of matches. |
| 2116 | * 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] | 2117 | * 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] | 2118 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2119 | */ |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2120 | static int |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2121 | ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2122 | char_u *str; |
| 2123 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2124 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2125 | char_u *fname; |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2126 | char_u **cptext; /* extra text for popup menu or NULL */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2127 | int cdir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2128 | int flags; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2129 | int adup; /* accept duplicate match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2130 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2131 | compl_T *match; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2132 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2133 | |
| 2134 | ui_breakcheck(); |
| 2135 | if (got_int) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2136 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2137 | if (len < 0) |
| 2138 | len = (int)STRLEN(str); |
| 2139 | |
| 2140 | /* |
| 2141 | * If the same match is already present, don't add it. |
| 2142 | */ |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2143 | if (compl_first_match != NULL && !adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2144 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2145 | match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2146 | do |
| 2147 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2148 | if ( !(match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2149 | && ins_compl_equal(match, str, len) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2150 | && match->cp_str[len] == NUL) |
| 2151 | return NOTDONE; |
| 2152 | match = match->cp_next; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2153 | } while (match != NULL && match != compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2156 | /* Remove any popup menu before changing the list of matches. */ |
| 2157 | ins_compl_del_pum(); |
| 2158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2159 | /* |
| 2160 | * Allocate a new match structure. |
| 2161 | * Copy the values to the new match structure. |
| 2162 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2163 | match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2164 | if (match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2165 | return FAIL; |
| 2166 | match->cp_number = -1; |
| 2167 | if (flags & ORIGINAL_TEXT) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2168 | match->cp_number = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2169 | if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2170 | { |
| 2171 | vim_free(match); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2172 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2173 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2174 | match->cp_icase = icase; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2175 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2176 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2177 | * - 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] | 2178 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 2179 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2180 | if (fname != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2181 | && compl_curr_match != NULL |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2182 | && compl_curr_match->cp_fname != NULL |
| 2183 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2184 | match->cp_fname = compl_curr_match->cp_fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2185 | else if (fname != NULL) |
| 2186 | { |
| 2187 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2188 | flags |= FREE_FNAME; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2189 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2190 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2191 | match->cp_fname = NULL; |
| 2192 | match->cp_flags = flags; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2193 | |
| 2194 | if (cptext != NULL) |
| 2195 | { |
| 2196 | int i; |
| 2197 | |
| 2198 | for (i = 0; i < CPT_COUNT; ++i) |
| 2199 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 2200 | match->cp_text[i] = vim_strsave(cptext[i]); |
| 2201 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* |
| 2204 | * Link the new match structure in the list of matches. |
| 2205 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2206 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2207 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2208 | else if (dir == FORWARD) |
| 2209 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2210 | match->cp_next = compl_curr_match->cp_next; |
| 2211 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2212 | } |
| 2213 | else /* BACKWARD */ |
| 2214 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2215 | match->cp_next = compl_curr_match; |
| 2216 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2218 | if (match->cp_next) |
| 2219 | match->cp_next->cp_prev = match; |
| 2220 | if (match->cp_prev) |
| 2221 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2222 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2223 | compl_first_match = match; |
| 2224 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2225 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2226 | /* |
| 2227 | * Find the longest common string if still doing that. |
| 2228 | */ |
| 2229 | if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) |
| 2230 | ins_compl_longest_match(match); |
| 2231 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2232 | return OK; |
| 2233 | } |
| 2234 | |
| 2235 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2236 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
| 2237 | * match->cp_icase. |
| 2238 | */ |
| 2239 | static int |
| 2240 | ins_compl_equal(match, str, len) |
| 2241 | compl_T *match; |
| 2242 | char_u *str; |
| 2243 | int len; |
| 2244 | { |
| 2245 | if (match->cp_icase) |
| 2246 | return STRNICMP(match->cp_str, str, (size_t)len) == 0; |
| 2247 | return STRNCMP(match->cp_str, str, (size_t)len) == 0; |
| 2248 | } |
| 2249 | |
| 2250 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2251 | * Reduce the longest common string for match "match". |
| 2252 | */ |
| 2253 | static void |
| 2254 | ins_compl_longest_match(match) |
| 2255 | compl_T *match; |
| 2256 | { |
| 2257 | char_u *p, *s; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2258 | int c1, c2; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2259 | int had_match; |
| 2260 | |
| 2261 | if (compl_leader == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2262 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2263 | /* First match, use it as a whole. */ |
| 2264 | compl_leader = vim_strsave(match->cp_str); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2265 | if (compl_leader != NULL) |
| 2266 | { |
| 2267 | had_match = (curwin->w_cursor.col > compl_col); |
| 2268 | ins_compl_delete(); |
| 2269 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2270 | ins_redraw(FALSE); |
| 2271 | |
| 2272 | /* When the match isn't there (to avoid matching itself) remove it |
| 2273 | * again after redrawing. */ |
| 2274 | if (!had_match) |
| 2275 | ins_compl_delete(); |
| 2276 | compl_used_match = FALSE; |
| 2277 | } |
| 2278 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2279 | else |
| 2280 | { |
| 2281 | /* Reduce the text if this match differs from compl_leader. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2282 | p = compl_leader; |
| 2283 | s = match->cp_str; |
| 2284 | while (*p != NUL) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2285 | { |
| 2286 | #ifdef FEAT_MBYTE |
| 2287 | if (has_mbyte) |
| 2288 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2289 | c1 = mb_ptr2char(p); |
| 2290 | c2 = mb_ptr2char(s); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2291 | } |
| 2292 | else |
| 2293 | #endif |
| 2294 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2295 | c1 = *p; |
| 2296 | c2 = *s; |
| 2297 | } |
| 2298 | if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) |
| 2299 | : (c1 != c2)) |
| 2300 | break; |
| 2301 | #ifdef FEAT_MBYTE |
| 2302 | if (has_mbyte) |
| 2303 | { |
| 2304 | mb_ptr_adv(p); |
| 2305 | mb_ptr_adv(s); |
| 2306 | } |
| 2307 | else |
| 2308 | #endif |
| 2309 | { |
| 2310 | ++p; |
| 2311 | ++s; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | if (*p != NUL) |
| 2316 | { |
| 2317 | /* Leader was shortened, need to change the inserted text. */ |
| 2318 | *p = NUL; |
| 2319 | had_match = (curwin->w_cursor.col > compl_col); |
| 2320 | ins_compl_delete(); |
| 2321 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 2322 | ins_redraw(FALSE); |
| 2323 | |
| 2324 | /* When the match isn't there (to avoid matching itself) remove it |
| 2325 | * again after redrawing. */ |
| 2326 | if (!had_match) |
| 2327 | ins_compl_delete(); |
| 2328 | } |
| 2329 | |
| 2330 | compl_used_match = FALSE; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | * Add an array of matches to the list of matches. |
| 2336 | * Frees matches[]. |
| 2337 | */ |
| 2338 | static void |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2339 | ins_compl_add_matches(num_matches, matches, icase) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2340 | int num_matches; |
| 2341 | char_u **matches; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2342 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2343 | { |
| 2344 | int i; |
| 2345 | int add_r = OK; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2346 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2347 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2348 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2349 | if ((add_r = ins_compl_add(matches[i], -1, icase, |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2350 | NULL, NULL, dir, 0, FALSE)) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | /* if dir was BACKWARD then honor it just once */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2352 | dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2353 | FreeWild(num_matches, matches); |
| 2354 | } |
| 2355 | |
| 2356 | /* Make the completion list cyclic. |
| 2357 | * Return the number of matches (excluding the original). |
| 2358 | */ |
| 2359 | static int |
| 2360 | ins_compl_make_cyclic() |
| 2361 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2362 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2363 | int count = 0; |
| 2364 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2365 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2366 | { |
| 2367 | /* |
| 2368 | * Find the end of the list. |
| 2369 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2370 | match = compl_first_match; |
| 2371 | /* 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] | 2372 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2373 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2374 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2375 | ++count; |
| 2376 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2377 | match->cp_next = compl_first_match; |
| 2378 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | } |
| 2380 | return count; |
| 2381 | } |
| 2382 | |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2383 | /* |
| 2384 | * Start completion for the complete() function. |
| 2385 | * "startcol" is where the matched text starts (1 is first column). |
| 2386 | * "list" is the list of matches. |
| 2387 | */ |
| 2388 | void |
| 2389 | set_completion(startcol, list) |
| 2390 | int startcol; |
| 2391 | list_T *list; |
| 2392 | { |
| 2393 | /* If already doing completions stop it. */ |
| 2394 | if (ctrl_x_mode != 0) |
| 2395 | ins_compl_prep(' '); |
| 2396 | ins_compl_clear(); |
| 2397 | |
| 2398 | if (stop_arrow() == FAIL) |
| 2399 | return; |
| 2400 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2401 | if (startcol > (int)curwin->w_cursor.col) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2402 | startcol = curwin->w_cursor.col; |
| 2403 | compl_col = startcol; |
| 2404 | compl_length = curwin->w_cursor.col - startcol; |
| 2405 | /* compl_pattern doesn't need to be set */ |
| 2406 | compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); |
| 2407 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 2408 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2409 | return; |
| 2410 | |
| 2411 | /* Handle like dictionary completion. */ |
| 2412 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2413 | |
| 2414 | ins_compl_add_list(list); |
| 2415 | compl_matches = ins_compl_make_cyclic(); |
| 2416 | compl_started = TRUE; |
| 2417 | compl_used_match = TRUE; |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 2418 | compl_cont_status = 0; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2419 | |
| 2420 | compl_curr_match = compl_first_match; |
| 2421 | ins_complete(Ctrl_N); |
| 2422 | out_flush(); |
| 2423 | } |
| 2424 | |
| 2425 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2426 | /* "compl_match_array" points the currently displayed list of entries in the |
| 2427 | * popup menu. It is NULL when there is no popup menu. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2428 | static pumitem_T *compl_match_array = NULL; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2429 | static int compl_match_arraysize; |
| 2430 | |
| 2431 | /* |
| 2432 | * Update the screen and when there is any scrolling remove the popup menu. |
| 2433 | */ |
| 2434 | static void |
| 2435 | ins_compl_upd_pum() |
| 2436 | { |
| 2437 | int h; |
| 2438 | |
| 2439 | if (compl_match_array != NULL) |
| 2440 | { |
| 2441 | h = curwin->w_cline_height; |
| 2442 | update_screen(0); |
| 2443 | if (h != curwin->w_cline_height) |
| 2444 | ins_compl_del_pum(); |
| 2445 | } |
| 2446 | } |
| 2447 | |
| 2448 | /* |
| 2449 | * Remove any popup menu. |
| 2450 | */ |
| 2451 | static void |
| 2452 | ins_compl_del_pum() |
| 2453 | { |
| 2454 | if (compl_match_array != NULL) |
| 2455 | { |
| 2456 | pum_undisplay(); |
| 2457 | vim_free(compl_match_array); |
| 2458 | compl_match_array = NULL; |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | /* |
| 2463 | * Return TRUE if the popup menu should be displayed. |
| 2464 | */ |
| 2465 | static int |
| 2466 | pum_wanted() |
| 2467 | { |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2468 | /* 'completeopt' must contain "menu" or "menuone" */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2469 | if (vim_strchr(p_cot, 'm') == NULL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2470 | return FALSE; |
| 2471 | |
| 2472 | /* The display looks bad on a B&W display. */ |
| 2473 | if (t_colors < 8 |
| 2474 | #ifdef FEAT_GUI |
| 2475 | && !gui.in_use |
| 2476 | #endif |
| 2477 | ) |
| 2478 | return FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2479 | return TRUE; |
| 2480 | } |
| 2481 | |
| 2482 | /* |
| 2483 | * Return TRUE if there are two or more matches to be shown in the popup menu. |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2484 | * One if 'completopt' contains "menuone". |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2485 | */ |
| 2486 | static int |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2487 | pum_enough_matches() |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2488 | { |
| 2489 | compl_T *compl; |
| 2490 | int i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2491 | |
| 2492 | /* Don't display the popup menu if there are no matches or there is only |
| 2493 | * one (ignoring the original text). */ |
| 2494 | compl = compl_first_match; |
| 2495 | i = 0; |
| 2496 | do |
| 2497 | { |
| 2498 | if (compl == NULL |
| 2499 | || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) |
| 2500 | break; |
| 2501 | compl = compl->cp_next; |
| 2502 | } while (compl != compl_first_match); |
| 2503 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2504 | if (strstr((char *)p_cot, "menuone") != NULL) |
| 2505 | return (i >= 1); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2506 | return (i >= 2); |
| 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * Show the popup menu for the list of matches. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2511 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2512 | */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2513 | void |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2514 | ins_compl_show_pum() |
| 2515 | { |
| 2516 | compl_T *compl; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2517 | compl_T *shown_compl = NULL; |
| 2518 | int did_find_shown_match = FALSE; |
| 2519 | int shown_match_ok = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2520 | int i; |
| 2521 | int cur = -1; |
| 2522 | colnr_T col; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2523 | int lead_len = 0; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2524 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2525 | if (!pum_wanted() || !pum_enough_matches()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2526 | return; |
| 2527 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2528 | #if defined(FEAT_EVAL) |
| 2529 | /* Dirty hard-coded hack: remove any matchparen highlighting. */ |
| 2530 | do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif"); |
| 2531 | #endif |
| 2532 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2533 | /* Update the screen before drawing the popup menu over it. */ |
| 2534 | update_screen(0); |
| 2535 | |
| 2536 | if (compl_match_array == NULL) |
| 2537 | { |
| 2538 | /* Need to build the popup menu list. */ |
| 2539 | compl_match_arraysize = 0; |
| 2540 | compl = compl_first_match; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2541 | if (compl_leader != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2542 | lead_len = (int)STRLEN(compl_leader); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2543 | do |
| 2544 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2545 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2546 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2547 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2548 | ++compl_match_arraysize; |
| 2549 | compl = compl->cp_next; |
| 2550 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2551 | if (compl_match_arraysize == 0) |
| 2552 | return; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2553 | compl_match_array = (pumitem_T *)alloc_clear( |
| 2554 | (unsigned)(sizeof(pumitem_T) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2555 | * compl_match_arraysize)); |
| 2556 | if (compl_match_array != NULL) |
| 2557 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2558 | /* If the current match is the original text don't find the first |
| 2559 | * match after it, don't highlight anything. */ |
| 2560 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 2561 | shown_match_ok = TRUE; |
| 2562 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2563 | i = 0; |
| 2564 | compl = compl_first_match; |
| 2565 | do |
| 2566 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2567 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2568 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2569 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2570 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2571 | if (!shown_match_ok) |
| 2572 | { |
| 2573 | if (compl == compl_shown_match || did_find_shown_match) |
| 2574 | { |
| 2575 | /* This item is the shown match or this is the |
| 2576 | * first displayed item after the shown match. */ |
| 2577 | compl_shown_match = compl; |
| 2578 | did_find_shown_match = TRUE; |
| 2579 | shown_match_ok = TRUE; |
| 2580 | } |
| 2581 | else |
| 2582 | /* Remember this displayed match for when the |
| 2583 | * shown match is just below it. */ |
| 2584 | shown_compl = compl; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2585 | cur = i; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2586 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2587 | |
| 2588 | if (compl->cp_text[CPT_ABBR] != NULL) |
| 2589 | compl_match_array[i].pum_text = |
| 2590 | compl->cp_text[CPT_ABBR]; |
| 2591 | else |
| 2592 | compl_match_array[i].pum_text = compl->cp_str; |
| 2593 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 2594 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 2595 | if (compl->cp_text[CPT_MENU] != NULL) |
| 2596 | compl_match_array[i++].pum_extra = |
| 2597 | compl->cp_text[CPT_MENU]; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2598 | else |
| 2599 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 2600 | } |
| 2601 | |
| 2602 | if (compl == compl_shown_match) |
| 2603 | { |
| 2604 | did_find_shown_match = TRUE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 2605 | |
| 2606 | /* When the original text is the shown match don't set |
| 2607 | * compl_shown_match. */ |
| 2608 | if (compl->cp_flags & ORIGINAL_TEXT) |
| 2609 | shown_match_ok = TRUE; |
| 2610 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2611 | if (!shown_match_ok && shown_compl != NULL) |
| 2612 | { |
| 2613 | /* The shown match isn't displayed, set it to the |
| 2614 | * previously displayed match. */ |
| 2615 | compl_shown_match = shown_compl; |
| 2616 | shown_match_ok = TRUE; |
| 2617 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2618 | } |
| 2619 | compl = compl->cp_next; |
| 2620 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2621 | |
| 2622 | if (!shown_match_ok) /* no displayed match at all */ |
| 2623 | cur = -1; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | else |
| 2627 | { |
| 2628 | /* popup menu already exists, only need to find the current item.*/ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2629 | for (i = 0; i < compl_match_arraysize; ++i) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2630 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str |
| 2631 | || compl_match_array[i].pum_text |
| 2632 | == compl_shown_match->cp_text[CPT_ABBR]) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2633 | { |
| 2634 | cur = i; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2635 | break; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2636 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | if (compl_match_array != NULL) |
| 2640 | { |
| 2641 | /* Compute the screen column of the start of the completed text. |
| 2642 | * Use the cursor to get all wrapping and other settings right. */ |
| 2643 | col = curwin->w_cursor.col; |
| 2644 | curwin->w_cursor.col = compl_col; |
Bram Moolenaar | d289f13 | 2006-03-11 21:30:53 +0000 | [diff] [blame] | 2645 | pum_display(compl_match_array, compl_match_arraysize, cur); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2646 | curwin->w_cursor.col = col; |
| 2647 | } |
| 2648 | } |
| 2649 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2650 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 2651 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2652 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | /* |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2654 | * Add any identifiers that match the given pattern in the list of dictionary |
| 2655 | * files "dict_start" to the list of completions. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | */ |
| 2657 | static void |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2658 | ins_compl_dictionaries(dict_start, pat, flags, thesaurus) |
| 2659 | char_u *dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | char_u *pat; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2661 | int flags; /* DICT_FIRST and/or DICT_EXACT */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2662 | int thesaurus; /* Thesaurus completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2663 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2664 | char_u *dict = dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2665 | char_u *ptr; |
| 2666 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2667 | regmatch_T regmatch; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2668 | char_u **files; |
| 2669 | int count; |
| 2670 | int i; |
| 2671 | int save_p_scs; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2672 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2673 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2674 | if (*dict == NUL) |
| 2675 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2676 | #ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2677 | /* When 'dictionary' is empty and spell checking is enabled use |
| 2678 | * "spell". */ |
| 2679 | if (!thesaurus && curwin->w_p_spell) |
| 2680 | dict = (char_u *)"spell"; |
| 2681 | else |
| 2682 | #endif |
| 2683 | return; |
| 2684 | } |
| 2685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2686 | buf = alloc(LSIZE); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2687 | if (buf == NULL) |
| 2688 | return; |
| 2689 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2690 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 2691 | save_p_scs = p_scs; |
| 2692 | if (curbuf->b_p_inf) |
| 2693 | p_scs = FALSE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2694 | |
| 2695 | /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 2696 | * to only match at the start of a line. Otherwise just match the |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2697 | * pattern. Also need to double backslashes. */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2698 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2699 | { |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2700 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
| 2701 | |
| 2702 | if (pat_esc == NULL) |
| 2703 | return ; |
| 2704 | i = (int)STRLEN(pat_esc) + 10; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2705 | ptr = alloc(i); |
| 2706 | if (ptr == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2707 | { |
| 2708 | vim_free(pat_esc); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2709 | return; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2710 | } |
| 2711 | vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc); |
| 2712 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 2713 | vim_free(pat_esc); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2714 | vim_free(ptr); |
| 2715 | } |
| 2716 | else |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2717 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2718 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2719 | if (regmatch.regprog == NULL) |
| 2720 | goto theend; |
| 2721 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2722 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 2724 | regmatch.rm_ic = ignorecase(pat); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2725 | while (*dict != NUL && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2726 | { |
| 2727 | /* copy one dictionary file name into buf */ |
| 2728 | if (flags == DICT_EXACT) |
| 2729 | { |
| 2730 | count = 1; |
| 2731 | files = &dict; |
| 2732 | } |
| 2733 | else |
| 2734 | { |
| 2735 | /* Expand wildcards in the dictionary name, but do not allow |
| 2736 | * backticks (for security, the 'dict' option may have been set in |
| 2737 | * a modeline). */ |
| 2738 | copy_option_part(&dict, buf, LSIZE, ","); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2739 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2740 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 2741 | count = -1; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2742 | else |
| 2743 | # endif |
| 2744 | if (vim_strchr(buf, '`') != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2745 | || expand_wildcards(1, &buf, &count, &files, |
| 2746 | EW_FILE|EW_SILENT) != OK) |
| 2747 | count = 0; |
| 2748 | } |
| 2749 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2750 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2751 | if (count == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2752 | { |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 2753 | /* Complete from active spelling. Skip "\<" in the pattern, we |
| 2754 | * don't use it as a RE. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2755 | if (pat[0] == '\\' && pat[1] == '<') |
| 2756 | ptr = pat + 2; |
| 2757 | else |
| 2758 | ptr = pat; |
| 2759 | spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2760 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2761 | else |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2762 | # endif |
Bram Moolenaar | d7fd0c4 | 2006-08-22 17:55:55 +0000 | [diff] [blame] | 2763 | if (count > 0) /* avoid warning for using "files" uninit */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2764 | { |
| 2765 | ins_compl_files(count, files, thesaurus, flags, |
| 2766 | ®match, buf, &dir); |
| 2767 | if (flags != DICT_EXACT) |
| 2768 | FreeWild(count, files); |
| 2769 | } |
| 2770 | if (flags != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2771 | break; |
| 2772 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2773 | |
| 2774 | theend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2775 | p_scs = save_p_scs; |
| 2776 | vim_free(regmatch.regprog); |
| 2777 | vim_free(buf); |
| 2778 | } |
| 2779 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2780 | static void |
| 2781 | ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir) |
| 2782 | int count; |
| 2783 | char_u **files; |
| 2784 | int thesaurus; |
| 2785 | int flags; |
| 2786 | regmatch_T *regmatch; |
| 2787 | char_u *buf; |
| 2788 | int *dir; |
| 2789 | { |
| 2790 | char_u *ptr; |
| 2791 | int i; |
| 2792 | FILE *fp; |
| 2793 | int add_r; |
| 2794 | |
| 2795 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 2796 | { |
| 2797 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 2798 | if (flags != DICT_EXACT) |
| 2799 | { |
| 2800 | vim_snprintf((char *)IObuff, IOSIZE, |
| 2801 | _("Scanning dictionary: %s"), (char *)files[i]); |
| 2802 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 2803 | } |
| 2804 | |
| 2805 | if (fp != NULL) |
| 2806 | { |
| 2807 | /* |
| 2808 | * Read dictionary file line by line. |
| 2809 | * Check each line for a match. |
| 2810 | */ |
| 2811 | while (!got_int && !compl_interrupted |
| 2812 | && !vim_fgets(buf, LSIZE, fp)) |
| 2813 | { |
| 2814 | ptr = buf; |
| 2815 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
| 2816 | { |
| 2817 | ptr = regmatch->startp[0]; |
| 2818 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 2819 | ptr = find_line_end(ptr); |
| 2820 | else |
| 2821 | ptr = find_word_end(ptr); |
| 2822 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 2823 | (int)(ptr - regmatch->startp[0]), |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 2824 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2825 | if (thesaurus) |
| 2826 | { |
| 2827 | char_u *wstart; |
| 2828 | |
| 2829 | /* |
| 2830 | * Add the other matches on the line |
| 2831 | */ |
| 2832 | while (!got_int) |
| 2833 | { |
| 2834 | /* Find start of the next word. Skip white |
| 2835 | * space and punctuation. */ |
| 2836 | ptr = find_word_start(ptr); |
| 2837 | if (*ptr == NUL || *ptr == NL) |
| 2838 | break; |
| 2839 | wstart = ptr; |
| 2840 | |
| 2841 | /* Find end of the word and add it. */ |
| 2842 | #ifdef FEAT_MBYTE |
| 2843 | if (has_mbyte) |
| 2844 | /* Japanese words may have characters in |
| 2845 | * different classes, only separate words |
| 2846 | * with single-byte non-word characters. */ |
| 2847 | while (*ptr != NUL) |
| 2848 | { |
| 2849 | int l = (*mb_ptr2len)(ptr); |
| 2850 | |
| 2851 | if (l < 2 && !vim_iswordc(*ptr)) |
| 2852 | break; |
| 2853 | ptr += l; |
| 2854 | } |
| 2855 | else |
| 2856 | #endif |
| 2857 | ptr = find_word_end(ptr); |
| 2858 | add_r = ins_compl_add_infercase(wstart, |
| 2859 | (int)(ptr - wstart), |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 2860 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2861 | } |
| 2862 | } |
| 2863 | if (add_r == OK) |
| 2864 | /* if dir was BACKWARD then honor it just once */ |
| 2865 | *dir = FORWARD; |
| 2866 | else if (add_r == FAIL) |
| 2867 | break; |
| 2868 | /* avoid expensive call to vim_regexec() when at end |
| 2869 | * of line */ |
| 2870 | if (*ptr == '\n' || got_int) |
| 2871 | break; |
| 2872 | } |
| 2873 | line_breakcheck(); |
| 2874 | ins_compl_check_keys(50); |
| 2875 | } |
| 2876 | fclose(fp); |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2881 | /* |
| 2882 | * Find the start of the next word. |
| 2883 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 2884 | */ |
| 2885 | char_u * |
| 2886 | find_word_start(ptr) |
| 2887 | char_u *ptr; |
| 2888 | { |
| 2889 | #ifdef FEAT_MBYTE |
| 2890 | if (has_mbyte) |
| 2891 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2892 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2893 | else |
| 2894 | #endif |
| 2895 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 2896 | ++ptr; |
| 2897 | return ptr; |
| 2898 | } |
| 2899 | |
| 2900 | /* |
| 2901 | * Find the end of the word. Assumes it starts inside a word. |
| 2902 | * Returns a pointer to just after the word. |
| 2903 | */ |
| 2904 | char_u * |
| 2905 | find_word_end(ptr) |
| 2906 | char_u *ptr; |
| 2907 | { |
| 2908 | #ifdef FEAT_MBYTE |
| 2909 | int start_class; |
| 2910 | |
| 2911 | if (has_mbyte) |
| 2912 | { |
| 2913 | start_class = mb_get_class(ptr); |
| 2914 | if (start_class > 1) |
| 2915 | while (*ptr != NUL) |
| 2916 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2917 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | if (mb_get_class(ptr) != start_class) |
| 2919 | break; |
| 2920 | } |
| 2921 | } |
| 2922 | else |
| 2923 | #endif |
| 2924 | while (vim_iswordc(*ptr)) |
| 2925 | ++ptr; |
| 2926 | return ptr; |
| 2927 | } |
| 2928 | |
| 2929 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2930 | * Find the end of the line, omitting CR and NL at the end. |
| 2931 | * Returns a pointer to just after the line. |
| 2932 | */ |
| 2933 | static char_u * |
| 2934 | find_line_end(ptr) |
| 2935 | char_u *ptr; |
| 2936 | { |
| 2937 | char_u *s; |
| 2938 | |
| 2939 | s = ptr + STRLEN(ptr); |
| 2940 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 2941 | --s; |
| 2942 | return s; |
| 2943 | } |
| 2944 | |
| 2945 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2946 | * Free the list of completions |
| 2947 | */ |
| 2948 | static void |
| 2949 | ins_compl_free() |
| 2950 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2951 | compl_T *match; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2952 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2953 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2954 | vim_free(compl_pattern); |
| 2955 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2956 | vim_free(compl_leader); |
| 2957 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2958 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2959 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2960 | return; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2961 | |
| 2962 | ins_compl_del_pum(); |
| 2963 | pum_clear(); |
| 2964 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2965 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2966 | do |
| 2967 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2968 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2969 | compl_curr_match = compl_curr_match->cp_next; |
| 2970 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2971 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2972 | if (match->cp_flags & FREE_FNAME) |
| 2973 | vim_free(match->cp_fname); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2974 | for (i = 0; i < CPT_COUNT; ++i) |
| 2975 | vim_free(match->cp_text[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2977 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 2978 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | static void |
| 2982 | ins_compl_clear() |
| 2983 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2984 | compl_cont_status = 0; |
| 2985 | compl_started = FALSE; |
| 2986 | compl_matches = 0; |
| 2987 | vim_free(compl_pattern); |
| 2988 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2989 | vim_free(compl_leader); |
| 2990 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2991 | edit_submode_extra = NULL; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2992 | vim_free(compl_orig_text); |
| 2993 | compl_orig_text = NULL; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 2994 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
| 2997 | /* |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 2998 | * Return TRUE when Insert completion is active. |
| 2999 | */ |
| 3000 | int |
| 3001 | ins_compl_active() |
| 3002 | { |
| 3003 | return compl_started; |
| 3004 | } |
| 3005 | |
| 3006 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3007 | * Delete one character before the cursor and show the subset of the matches |
| 3008 | * that match the word that is now before the cursor. |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3009 | * Returns the character to be used, NUL if the work is done and another char |
| 3010 | * to be got from the user. |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3011 | */ |
| 3012 | static int |
| 3013 | ins_compl_bs() |
| 3014 | { |
| 3015 | char_u *line; |
| 3016 | char_u *p; |
| 3017 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3018 | line = ml_get_curline(); |
| 3019 | p = line + curwin->w_cursor.col; |
| 3020 | mb_ptr_back(line, p); |
| 3021 | |
| 3022 | /* Stop completion when the whole word was deleted. */ |
| 3023 | if ((int)(p - line) - (int)compl_col <= 0) |
| 3024 | return K_BS; |
| 3025 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3026 | /* Deleted more than what was used to find matches or didn't finish |
| 3027 | * finding all matches: need to look for matches all over again. */ |
| 3028 | if (curwin->w_cursor.col <= compl_col + compl_length |
| 3029 | || compl_was_interrupted) |
| 3030 | ins_compl_restart(); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3031 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3032 | vim_free(compl_leader); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3033 | compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3034 | if (compl_leader != NULL) |
| 3035 | { |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3036 | ins_compl_new_leader(); |
| 3037 | return NUL; |
| 3038 | } |
| 3039 | return K_BS; |
| 3040 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3041 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3042 | /* |
| 3043 | * Called after changing "compl_leader". |
| 3044 | * Show the popup menu with a different set of matches. |
| 3045 | * May also search for matches again if the previous search was interrupted. |
| 3046 | */ |
| 3047 | static void |
| 3048 | ins_compl_new_leader() |
| 3049 | { |
| 3050 | ins_compl_del_pum(); |
| 3051 | ins_compl_delete(); |
| 3052 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 3053 | compl_used_match = FALSE; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3054 | |
| 3055 | if (compl_started) |
| 3056 | ins_compl_set_original_text(compl_leader); |
| 3057 | else |
| 3058 | { |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3059 | #ifdef FEAT_SPELL |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3060 | spell_bad_len = 0; /* need to redetect bad word */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3061 | #endif |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3062 | /* |
| 3063 | * Matches were cleared, need to search for them now. First display |
| 3064 | * the changed text before the cursor. Set "compl_restarting" to |
| 3065 | * avoid that the first match is inserted. |
| 3066 | */ |
| 3067 | update_screen(0); |
| 3068 | #ifdef FEAT_GUI |
| 3069 | if (gui.in_use) |
| 3070 | { |
| 3071 | /* Show the cursor after the match, not after the redrawn text. */ |
| 3072 | setcursor(); |
| 3073 | out_flush(); |
| 3074 | gui_update_cursor(FALSE, FALSE); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3075 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3076 | #endif |
| 3077 | compl_restarting = TRUE; |
| 3078 | if (ins_complete(Ctrl_N) == FAIL) |
| 3079 | compl_cont_status = 0; |
| 3080 | compl_restarting = FALSE; |
| 3081 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3082 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3083 | #if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */ |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3084 | if (!compl_used_match) |
| 3085 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 3086 | /* Go to the original text, since none of the matches is inserted. */ |
| 3087 | if (compl_first_match->cp_prev != NULL |
| 3088 | && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT)) |
| 3089 | compl_shown_match = compl_first_match->cp_prev; |
| 3090 | else |
| 3091 | compl_shown_match = compl_first_match; |
| 3092 | compl_curr_match = compl_shown_match; |
| 3093 | compl_shows_dir = compl_direction; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3094 | } |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3095 | #endif |
| 3096 | compl_enter_selects = !compl_used_match; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3097 | |
| 3098 | /* Show the popup menu with a different set of matches. */ |
| 3099 | ins_compl_show_pum(); |
Bram Moolenaar | 7073cc8 | 2006-08-29 16:33:06 +0000 | [diff] [blame] | 3100 | |
| 3101 | /* Don't let Enter select the original text when there is no popup menu. */ |
| 3102 | if (compl_match_array == NULL) |
| 3103 | compl_enter_selects = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3104 | } |
| 3105 | |
| 3106 | /* |
| 3107 | * Append one character to the match leader. May reduce the number of |
| 3108 | * matches. |
| 3109 | */ |
| 3110 | static void |
| 3111 | ins_compl_addleader(c) |
| 3112 | int c; |
| 3113 | { |
| 3114 | #ifdef FEAT_MBYTE |
| 3115 | int cc; |
| 3116 | |
| 3117 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 3118 | { |
| 3119 | char_u buf[MB_MAXBYTES + 1]; |
| 3120 | |
| 3121 | (*mb_char2bytes)(c, buf); |
| 3122 | buf[cc] = NUL; |
| 3123 | ins_char_bytes(buf, cc); |
| 3124 | } |
| 3125 | else |
| 3126 | #endif |
| 3127 | ins_char(c); |
| 3128 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3129 | /* If we didn't complete finding matches we must search again. */ |
| 3130 | if (compl_was_interrupted) |
| 3131 | ins_compl_restart(); |
| 3132 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3133 | vim_free(compl_leader); |
| 3134 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
| 3135 | curwin->w_cursor.col - compl_col); |
| 3136 | if (compl_leader != NULL) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3137 | ins_compl_new_leader(); |
| 3138 | } |
| 3139 | |
| 3140 | /* |
| 3141 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 3142 | * BS or a key was typed while still searching for matches. |
| 3143 | */ |
| 3144 | static void |
| 3145 | ins_compl_restart() |
| 3146 | { |
| 3147 | ins_compl_free(); |
| 3148 | compl_started = FALSE; |
| 3149 | compl_matches = 0; |
| 3150 | compl_cont_status = 0; |
| 3151 | compl_cont_mode = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | /* |
| 3155 | * Set the first match, the original text. |
| 3156 | */ |
| 3157 | static void |
| 3158 | ins_compl_set_original_text(str) |
| 3159 | char_u *str; |
| 3160 | { |
| 3161 | char_u *p; |
| 3162 | |
| 3163 | /* Replace the original text entry. */ |
| 3164 | if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */ |
| 3165 | { |
| 3166 | p = vim_strsave(str); |
| 3167 | if (p != NULL) |
| 3168 | { |
| 3169 | vim_free(compl_first_match->cp_str); |
| 3170 | compl_first_match->cp_str = p; |
| 3171 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | |
| 3175 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3176 | * Append one character to the match leader. May reduce the number of |
| 3177 | * matches. |
| 3178 | */ |
| 3179 | static void |
| 3180 | ins_compl_addfrommatch() |
| 3181 | { |
| 3182 | char_u *p; |
| 3183 | int len = curwin->w_cursor.col - compl_col; |
| 3184 | int c; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3185 | compl_T *cp; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3186 | |
| 3187 | p = compl_shown_match->cp_str; |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 3188 | if ((int)STRLEN(p) <= len) /* the match is too short */ |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3189 | { |
| 3190 | /* When still at the original match use the first entry that matches |
| 3191 | * the leader. */ |
| 3192 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 3193 | { |
| 3194 | p = NULL; |
| 3195 | for (cp = compl_shown_match->cp_next; cp != NULL |
| 3196 | && cp != compl_first_match; cp = cp->cp_next) |
| 3197 | { |
| 3198 | if (ins_compl_equal(cp, compl_leader, |
| 3199 | (int)STRLEN(compl_leader))) |
| 3200 | { |
| 3201 | p = cp->cp_str; |
| 3202 | break; |
| 3203 | } |
| 3204 | } |
| 3205 | if (p == NULL || (int)STRLEN(p) <= len) |
| 3206 | return; |
| 3207 | } |
| 3208 | else |
| 3209 | return; |
| 3210 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3211 | p += len; |
| 3212 | #ifdef FEAT_MBYTE |
| 3213 | c = mb_ptr2char(p); |
| 3214 | #else |
| 3215 | c = *p; |
| 3216 | #endif |
| 3217 | ins_compl_addleader(c); |
| 3218 | } |
| 3219 | |
| 3220 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3221 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3222 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3223 | * Returns TRUE when the character is not to be inserted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3224 | */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3225 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3226 | ins_compl_prep(c) |
| 3227 | int c; |
| 3228 | { |
| 3229 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3230 | int want_cindent; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3231 | int retval = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3232 | |
| 3233 | /* Forget any previous 'special' messages if this is actually |
| 3234 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 3235 | */ |
| 3236 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 3237 | edit_submode_extra = NULL; |
| 3238 | |
| 3239 | /* Ignore end of Select mode mapping */ |
| 3240 | if (c == K_SELECT) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3241 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3242 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3243 | /* Set "compl_get_longest" when finding the first matches. */ |
| 3244 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 3245 | || (ctrl_x_mode == 0 && !compl_started)) |
| 3246 | { |
| 3247 | compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); |
| 3248 | compl_used_match = TRUE; |
| 3249 | } |
| 3250 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3251 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 3252 | { |
| 3253 | /* |
| 3254 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 3255 | * it will be yet. Now we decide. |
| 3256 | */ |
| 3257 | switch (c) |
| 3258 | { |
| 3259 | case Ctrl_E: |
| 3260 | case Ctrl_Y: |
| 3261 | ctrl_x_mode = CTRL_X_SCROLL; |
| 3262 | if (!(State & REPLACE_FLAG)) |
| 3263 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 3264 | else |
| 3265 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 3266 | edit_submode_pre = NULL; |
| 3267 | showmode(); |
| 3268 | break; |
| 3269 | case Ctrl_L: |
| 3270 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 3271 | break; |
| 3272 | case Ctrl_F: |
| 3273 | ctrl_x_mode = CTRL_X_FILES; |
| 3274 | break; |
| 3275 | case Ctrl_K: |
| 3276 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 3277 | break; |
| 3278 | case Ctrl_R: |
| 3279 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 3280 | break; |
| 3281 | case Ctrl_T: |
| 3282 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 3283 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3284 | #ifdef FEAT_COMPL_FUNC |
| 3285 | case Ctrl_U: |
| 3286 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 3287 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3288 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3289 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3290 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3291 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3292 | case 's': |
| 3293 | case Ctrl_S: |
| 3294 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3295 | #ifdef FEAT_SPELL |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3296 | ++emsg_off; /* Avoid getting the E756 error twice. */ |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3297 | spell_back_to_badword(); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3298 | --emsg_off; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3299 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3300 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3301 | case Ctrl_RSB: |
| 3302 | ctrl_x_mode = CTRL_X_TAGS; |
| 3303 | break; |
| 3304 | #ifdef FEAT_FIND_ID |
| 3305 | case Ctrl_I: |
| 3306 | case K_S_TAB: |
| 3307 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 3308 | break; |
| 3309 | case Ctrl_D: |
| 3310 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 3311 | break; |
| 3312 | #endif |
| 3313 | case Ctrl_V: |
| 3314 | case Ctrl_Q: |
| 3315 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 3316 | break; |
| 3317 | case Ctrl_P: |
| 3318 | case Ctrl_N: |
| 3319 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 3320 | * just started ^X mode, or there were enough ^X's to cancel |
| 3321 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 3322 | * do normal expansion when interrupting a different mode (say |
| 3323 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 3324 | * nothing changes if interrupting mode 0, (eg, the flag |
| 3325 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3326 | if (!(compl_cont_status & CONT_INTRPT)) |
| 3327 | compl_cont_status |= CONT_LOCAL; |
| 3328 | else if (compl_cont_mode != 0) |
| 3329 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3330 | /* FALLTHROUGH */ |
| 3331 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3332 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 3333 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 3334 | * mode). |
| 3335 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 3336 | * value, in both cases ^X^X can be used to restart the same |
| 3337 | * mode (avoiding ADDING mode). |
| 3338 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 3339 | * 'complete' and local ^P expansions respectively. |
| 3340 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 3341 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3342 | if (c == Ctrl_X) |
| 3343 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3344 | if (compl_cont_mode != 0) |
| 3345 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3346 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3347 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3348 | } |
| 3349 | ctrl_x_mode = 0; |
| 3350 | edit_submode = NULL; |
| 3351 | showmode(); |
| 3352 | break; |
| 3353 | } |
| 3354 | } |
| 3355 | else if (ctrl_x_mode != 0) |
| 3356 | { |
| 3357 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 3358 | if (!vim_is_ctrl_x_key(c)) |
| 3359 | { |
| 3360 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 3361 | ctrl_x_mode = 0; |
| 3362 | else |
| 3363 | ctrl_x_mode = CTRL_X_FINISHED; |
| 3364 | edit_submode = NULL; |
| 3365 | } |
| 3366 | showmode(); |
| 3367 | } |
| 3368 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3369 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3370 | { |
| 3371 | /* Show error message from attempted keyword completion (probably |
| 3372 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3373 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | showmode(); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3375 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
| 3376 | && !ins_compl_pum_key(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3377 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 3378 | { |
| 3379 | /* Get here when we have finished typing a sequence of ^N and |
| 3380 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3381 | * memory that was used, and make sure we can redo the insert. */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3382 | if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3384 | char_u *p; |
Bram Moolenaar | d7fd0c4 | 2006-08-22 17:55:55 +0000 | [diff] [blame] | 3385 | int temp = 0; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3386 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3387 | /* |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3388 | * If any of the original typed text has been changed, eg when |
| 3389 | * ignorecase is set, we must add back-spaces to the redo |
| 3390 | * buffer. We add as few as necessary to delete just the part |
| 3391 | * of the original text that has changed. |
| 3392 | * When using the longest match, edited the match or used |
| 3393 | * CTRL-E then don't use the current match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3394 | */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3395 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
| 3396 | ptr = compl_curr_match->cp_str; |
| 3397 | else if (compl_leader != NULL) |
| 3398 | ptr = compl_leader; |
| 3399 | else |
| 3400 | ptr = compl_orig_text; |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 3401 | if (compl_orig_text != NULL) |
| 3402 | { |
| 3403 | p = compl_orig_text; |
| 3404 | for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp]; |
| 3405 | ++temp) |
| 3406 | ; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3407 | #ifdef FEAT_MBYTE |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 3408 | if (temp > 0) |
| 3409 | temp -= (*mb_head_off)(compl_orig_text, p + temp); |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3410 | #endif |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 3411 | for (p += temp; *p != NUL; mb_ptr_adv(p)) |
| 3412 | AppendCharToRedobuff(K_BS); |
| 3413 | } |
| 3414 | if (ptr != NULL) |
| 3415 | AppendToRedobuffLit(ptr + temp, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | #ifdef FEAT_CINDENT |
| 3419 | want_cindent = (can_cindent && cindent_on()); |
| 3420 | #endif |
| 3421 | /* |
| 3422 | * When completing whole lines: fix indent for 'cindent'. |
| 3423 | * Otherwise, break line if it's too long. |
| 3424 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3425 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | { |
| 3427 | #ifdef FEAT_CINDENT |
| 3428 | /* re-indent the current line */ |
| 3429 | if (want_cindent) |
| 3430 | { |
| 3431 | do_c_expr_indent(); |
| 3432 | want_cindent = FALSE; /* don't do it again */ |
| 3433 | } |
| 3434 | #endif |
| 3435 | } |
| 3436 | else |
| 3437 | { |
| 3438 | /* put the cursor on the last char, for 'tw' formatting */ |
| 3439 | curwin->w_cursor.col--; |
| 3440 | if (stop_arrow() == OK) |
| 3441 | insertchar(NUL, 0, -1); |
| 3442 | curwin->w_cursor.col++; |
| 3443 | } |
| 3444 | |
| 3445 | auto_format(FALSE, TRUE); |
| 3446 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3447 | /* If the popup menu is displayed pressing CTRL-Y means accepting |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3448 | * the selection without inserting anything. When |
| 3449 | * compl_enter_selects is set the Enter key does the same. */ |
| 3450 | if ((c == Ctrl_Y || (compl_enter_selects |
| 3451 | && (c == CAR || c == K_KENTER || c == NL))) |
| 3452 | && pum_visible()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3453 | retval = TRUE; |
| 3454 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3455 | /* CTRL-E means completion is Ended, go back to the typed text. */ |
| 3456 | if (c == Ctrl_E) |
| 3457 | { |
| 3458 | ins_compl_delete(); |
| 3459 | if (compl_leader != NULL) |
| 3460 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 3461 | else if (compl_first_match != NULL) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3462 | ins_bytes(compl_orig_text |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3463 | + curwin->w_cursor.col - compl_col); |
| 3464 | retval = TRUE; |
| 3465 | } |
| 3466 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3467 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3468 | compl_started = FALSE; |
| 3469 | compl_matches = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3470 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 3471 | ctrl_x_mode = 0; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3472 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3473 | if (edit_submode != NULL) |
| 3474 | { |
| 3475 | edit_submode = NULL; |
| 3476 | showmode(); |
| 3477 | } |
| 3478 | |
| 3479 | #ifdef FEAT_CINDENT |
| 3480 | /* |
| 3481 | * Indent now if a key was typed that is in 'cinkeys'. |
| 3482 | */ |
| 3483 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 3484 | do_c_expr_indent(); |
| 3485 | #endif |
| 3486 | } |
| 3487 | } |
| 3488 | |
| 3489 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 3490 | * (re)set properly in ins_complete() */ |
| 3491 | if (!vim_is_ctrl_x_key(c)) |
| 3492 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3493 | compl_cont_status = 0; |
| 3494 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3495 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3496 | |
| 3497 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | /* |
| 3501 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3502 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3503 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3504 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3505 | * |
| 3506 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3507 | */ |
| 3508 | static buf_T * |
| 3509 | ins_compl_next_buf(buf, flag) |
| 3510 | buf_T *buf; |
| 3511 | int flag; |
| 3512 | { |
| 3513 | #ifdef FEAT_WINDOWS |
| 3514 | static win_T *wp; |
| 3515 | #endif |
| 3516 | |
| 3517 | if (flag == 'w') /* just windows */ |
| 3518 | { |
| 3519 | #ifdef FEAT_WINDOWS |
| 3520 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 3521 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3522 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | && wp->w_buffer->b_scanned) |
| 3524 | ; |
| 3525 | buf = wp->w_buffer; |
| 3526 | #else |
| 3527 | buf = curbuf; |
| 3528 | #endif |
| 3529 | } |
| 3530 | else |
| 3531 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3532 | * (unlisted buffers) |
| 3533 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3534 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3535 | && ((flag == 'U' |
| 3536 | ? buf->b_p_bl |
| 3537 | : (!buf->b_p_bl |
| 3538 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3539 | || buf->b_scanned)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3540 | ; |
| 3541 | return buf; |
| 3542 | } |
| 3543 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3544 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3545 | static void expand_by_function __ARGS((int type, char_u *base)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3546 | |
| 3547 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3548 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3549 | * get matches in "matches". |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3550 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3551 | static void |
| 3552 | expand_by_function(type, base) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3553 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3554 | char_u *base; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3555 | { |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3556 | list_T *matchlist; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3557 | char_u *args[2]; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3558 | char_u *funcname; |
| 3559 | pos_T pos; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3560 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3561 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3562 | if (*funcname == NUL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3563 | return; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3564 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3565 | /* Call 'completefunc' to obtain the list of matches. */ |
| 3566 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3567 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3568 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3569 | pos = curwin->w_cursor; |
| 3570 | matchlist = call_func_retlist(funcname, 2, args, FALSE); |
| 3571 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3572 | if (matchlist == NULL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3573 | return; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3574 | |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3575 | ins_compl_add_list(matchlist); |
| 3576 | list_unref(matchlist); |
| 3577 | } |
| 3578 | #endif /* FEAT_COMPL_FUNC */ |
| 3579 | |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3580 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3581 | /* |
| 3582 | * Add completions from a list. |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3583 | */ |
| 3584 | static void |
| 3585 | ins_compl_add_list(list) |
| 3586 | list_T *list; |
| 3587 | { |
| 3588 | listitem_T *li; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3589 | int dir = compl_direction; |
| 3590 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3591 | /* Go through the List with matches and add each of them. */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3592 | for (li = list->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3593 | { |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3594 | if (ins_compl_add_tv(&li->li_tv, dir) == OK) |
| 3595 | /* if dir was BACKWARD then honor it just once */ |
| 3596 | dir = FORWARD; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 3597 | else if (did_emsg) |
| 3598 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3599 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3600 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3601 | |
| 3602 | /* |
| 3603 | * Add a match to the list of matches from a typeval_T. |
| 3604 | * If the given string is already in the list of completions, then return |
| 3605 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 3606 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 3607 | */ |
| 3608 | int |
| 3609 | ins_compl_add_tv(tv, dir) |
| 3610 | typval_T *tv; |
| 3611 | int dir; |
| 3612 | { |
| 3613 | char_u *word; |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 3614 | int icase = FALSE; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3615 | int adup = FALSE; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3616 | char_u *(cptext[CPT_COUNT]); |
| 3617 | |
| 3618 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 3619 | { |
| 3620 | word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE); |
| 3621 | cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict, |
| 3622 | (char_u *)"abbr", FALSE); |
| 3623 | cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict, |
| 3624 | (char_u *)"menu", FALSE); |
| 3625 | cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict, |
| 3626 | (char_u *)"kind", FALSE); |
| 3627 | cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict, |
| 3628 | (char_u *)"info", FALSE); |
| 3629 | if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL) |
| 3630 | icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase"); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 3631 | if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL) |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3632 | adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup"); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3633 | } |
| 3634 | else |
| 3635 | { |
| 3636 | word = get_tv_string_chk(tv); |
| 3637 | vim_memset(cptext, 0, sizeof(cptext)); |
| 3638 | } |
| 3639 | if (word == NULL || *word == NUL) |
| 3640 | return FAIL; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 3641 | return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3642 | } |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3643 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3644 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3645 | /* |
| 3646 | * Get the next expansion(s), using "compl_pattern". |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3647 | * The search starts at position "ini" in curbuf and in the direction |
| 3648 | * compl_direction. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3649 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 3650 | * where we stopped searching before. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3651 | * This may return before finding all the matches. |
| 3652 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3653 | */ |
| 3654 | static int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3655 | ins_compl_get_exp(ini) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3656 | pos_T *ini; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | { |
| 3658 | static pos_T first_match_pos; |
| 3659 | static pos_T last_match_pos; |
| 3660 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3661 | static int found_all = FALSE; /* Found all matches of a |
| 3662 | certain type. */ |
| 3663 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3664 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3665 | pos_T *pos; |
| 3666 | char_u **matches; |
| 3667 | int save_p_scs; |
| 3668 | int save_p_ws; |
| 3669 | int save_p_ic; |
| 3670 | int i; |
| 3671 | int num_matches; |
| 3672 | int len; |
| 3673 | int found_new_match; |
| 3674 | int type = ctrl_x_mode; |
| 3675 | char_u *ptr; |
| 3676 | char_u *dict = NULL; |
| 3677 | int dict_f = 0; |
| 3678 | compl_T *old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3680 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3681 | { |
| 3682 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 3683 | ins_buf->b_scanned = 0; |
| 3684 | found_all = FALSE; |
| 3685 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3686 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3687 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3688 | last_match_pos = first_match_pos = *ini; |
| 3689 | } |
| 3690 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3691 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3692 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3693 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 3694 | for (;;) |
| 3695 | { |
| 3696 | found_new_match = FAIL; |
| 3697 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3698 | /* 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] | 3699 | * or if found_all says this entry is done. For ^X^L only use the |
| 3700 | * entries from 'complete' that look in loaded buffers. */ |
| 3701 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3702 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3703 | { |
| 3704 | found_all = FALSE; |
| 3705 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 3706 | e_cpt++; |
| 3707 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 3708 | { |
| 3709 | ins_buf = curbuf; |
| 3710 | first_match_pos = *ini; |
| 3711 | /* So that ^N can match word immediately after cursor */ |
| 3712 | if (ctrl_x_mode == 0) |
| 3713 | dec(&first_match_pos); |
| 3714 | last_match_pos = first_match_pos; |
| 3715 | type = 0; |
| 3716 | } |
| 3717 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 3718 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 3719 | { |
| 3720 | /* Scan a buffer, but not the current one. */ |
| 3721 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 3722 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3723 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3724 | first_match_pos.col = last_match_pos.col = 0; |
| 3725 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 3726 | last_match_pos.lnum = 0; |
| 3727 | type = 0; |
| 3728 | } |
| 3729 | else /* unloaded buffer, scan like dictionary */ |
| 3730 | { |
| 3731 | found_all = TRUE; |
| 3732 | if (ins_buf->b_fname == NULL) |
| 3733 | continue; |
| 3734 | type = CTRL_X_DICTIONARY; |
| 3735 | dict = ins_buf->b_fname; |
| 3736 | dict_f = DICT_EXACT; |
| 3737 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 3738 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3739 | ins_buf->b_fname == NULL |
| 3740 | ? buf_spname(ins_buf) |
| 3741 | : ins_buf->b_sfname == NULL |
| 3742 | ? (char *)ins_buf->b_fname |
| 3743 | : (char *)ins_buf->b_sfname); |
| 3744 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 3745 | } |
| 3746 | else if (*e_cpt == NUL) |
| 3747 | break; |
| 3748 | else |
| 3749 | { |
| 3750 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3751 | type = -1; |
| 3752 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 3753 | { |
| 3754 | if (*e_cpt == 'k') |
| 3755 | type = CTRL_X_DICTIONARY; |
| 3756 | else |
| 3757 | type = CTRL_X_THESAURUS; |
| 3758 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 3759 | { |
| 3760 | dict = e_cpt; |
| 3761 | dict_f = DICT_FIRST; |
| 3762 | } |
| 3763 | } |
| 3764 | #ifdef FEAT_FIND_ID |
| 3765 | else if (*e_cpt == 'i') |
| 3766 | type = CTRL_X_PATH_PATTERNS; |
| 3767 | else if (*e_cpt == 'd') |
| 3768 | type = CTRL_X_PATH_DEFINES; |
| 3769 | #endif |
| 3770 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 3771 | { |
| 3772 | type = CTRL_X_TAGS; |
| 3773 | sprintf((char*)IObuff, _("Scanning tags.")); |
| 3774 | msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
| 3775 | } |
| 3776 | else |
| 3777 | type = -1; |
| 3778 | |
| 3779 | /* in any case e_cpt is advanced to the next entry */ |
| 3780 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 3781 | |
| 3782 | found_all = TRUE; |
| 3783 | if (type == -1) |
| 3784 | continue; |
| 3785 | } |
| 3786 | } |
| 3787 | |
| 3788 | switch (type) |
| 3789 | { |
| 3790 | case -1: |
| 3791 | break; |
| 3792 | #ifdef FEAT_FIND_ID |
| 3793 | case CTRL_X_PATH_PATTERNS: |
| 3794 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3795 | find_pattern_in_path(compl_pattern, compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3796 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3797 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3798 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3799 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 3800 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 3801 | break; |
| 3802 | #endif |
| 3803 | |
| 3804 | case CTRL_X_DICTIONARY: |
| 3805 | case CTRL_X_THESAURUS: |
| 3806 | ins_compl_dictionaries( |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3807 | dict != NULL ? dict |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3808 | : (type == CTRL_X_THESAURUS |
| 3809 | ? (*curbuf->b_p_tsr == NUL |
| 3810 | ? p_tsr |
| 3811 | : curbuf->b_p_tsr) |
| 3812 | : (*curbuf->b_p_dict == NUL |
| 3813 | ? p_dict |
| 3814 | : curbuf->b_p_dict)), |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3815 | compl_pattern, |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3816 | dict != NULL ? dict_f |
| 3817 | : 0, type == CTRL_X_THESAURUS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3818 | dict = NULL; |
| 3819 | break; |
| 3820 | |
| 3821 | case CTRL_X_TAGS: |
| 3822 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 3823 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3824 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3825 | |
| 3826 | /* Find up to TAG_MANY matches. Avoids that an enourmous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3827 | * of matches is found when compl_pattern is empty */ |
| 3828 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3829 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 3830 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 3831 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 3832 | { |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3833 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3834 | } |
| 3835 | p_ic = save_p_ic; |
| 3836 | break; |
| 3837 | |
| 3838 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3839 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3840 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 3841 | { |
| 3842 | |
| 3843 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3844 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3845 | ins_compl_add_matches(num_matches, matches, |
| 3846 | #ifdef CASE_INSENSITIVE_FILENAME |
| 3847 | TRUE |
| 3848 | #else |
| 3849 | FALSE |
| 3850 | #endif |
| 3851 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3852 | } |
| 3853 | break; |
| 3854 | |
| 3855 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3856 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 3857 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3858 | &num_matches, &matches) == EXPAND_OK) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 3859 | ins_compl_add_matches(num_matches, matches, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3860 | break; |
| 3861 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3862 | #ifdef FEAT_COMPL_FUNC |
| 3863 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3864 | case CTRL_X_OMNI: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3865 | expand_by_function(type, compl_pattern); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3866 | break; |
| 3867 | #endif |
| 3868 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3869 | case CTRL_X_SPELL: |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3870 | #ifdef FEAT_SPELL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3871 | num_matches = expand_spelling(first_match_pos.lnum, |
| 3872 | first_match_pos.col, compl_pattern, &matches); |
| 3873 | if (num_matches > 0) |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3874 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3875 | #endif |
| 3876 | break; |
| 3877 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3878 | default: /* normal ^P/^N and ^X^L */ |
| 3879 | /* |
| 3880 | * If 'infercase' is set, don't use 'smartcase' here |
| 3881 | */ |
| 3882 | save_p_scs = p_scs; |
| 3883 | if (ins_buf->b_p_inf) |
| 3884 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3885 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3886 | /* buffers other than curbuf are scanned from the beginning or the |
| 3887 | * end but never from the middle, thus setting nowrapscan in this |
| 3888 | * buffers is a good idea, on the other hand, we always set |
| 3889 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 3890 | save_p_ws = p_ws; |
| 3891 | if (ins_buf != curbuf) |
| 3892 | p_ws = FALSE; |
| 3893 | else if (*e_cpt == '.') |
| 3894 | p_ws = TRUE; |
| 3895 | for (;;) |
| 3896 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3897 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3898 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3899 | /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that |
| 3900 | * has added a word that was at the beginning of the line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3902 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3903 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3904 | compl_direction, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3905 | else |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3906 | found_new_match = searchit(NULL, ins_buf, pos, |
| 3907 | compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3908 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 3909 | RE_LAST, (linenr_T)0); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3910 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3911 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3912 | /* set "compl_started" even on fail */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3913 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3914 | first_match_pos = *pos; |
| 3915 | last_match_pos = *pos; |
| 3916 | } |
| 3917 | else if (first_match_pos.lnum == last_match_pos.lnum |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3918 | && first_match_pos.col == last_match_pos.col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3919 | found_new_match = FAIL; |
| 3920 | if (found_new_match == FAIL) |
| 3921 | { |
| 3922 | if (ins_buf == curbuf) |
| 3923 | found_all = TRUE; |
| 3924 | break; |
| 3925 | } |
| 3926 | |
| 3927 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3928 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3929 | && ini->lnum == pos->lnum |
| 3930 | && ini->col == pos->col) |
| 3931 | continue; |
| 3932 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 3933 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3934 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3935 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3936 | { |
| 3937 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 3938 | continue; |
| 3939 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3940 | if (!p_paste) |
| 3941 | ptr = skipwhite(ptr); |
| 3942 | } |
| 3943 | len = (int)STRLEN(ptr); |
| 3944 | } |
| 3945 | else |
| 3946 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3947 | char_u *tmp_ptr = ptr; |
| 3948 | |
| 3949 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3950 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3951 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | /* Skip if already inside a word. */ |
| 3953 | if (vim_iswordp(tmp_ptr)) |
| 3954 | continue; |
| 3955 | /* Find start of next word. */ |
| 3956 | tmp_ptr = find_word_start(tmp_ptr); |
| 3957 | } |
| 3958 | /* Find end of this word. */ |
| 3959 | tmp_ptr = find_word_end(tmp_ptr); |
| 3960 | len = (int)(tmp_ptr - ptr); |
| 3961 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3962 | if ((compl_cont_status & CONT_ADDING) |
| 3963 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | { |
| 3965 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 3966 | { |
| 3967 | /* Try next line, if any. the new word will be |
| 3968 | * "join" as if the normal command "J" was used. |
| 3969 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3970 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3971 | * works -- Acevedo */ |
| 3972 | STRNCPY(IObuff, ptr, len); |
| 3973 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 3974 | tmp_ptr = ptr = skipwhite(ptr); |
| 3975 | /* Find start of next word. */ |
| 3976 | tmp_ptr = find_word_start(tmp_ptr); |
| 3977 | /* Find end of next word. */ |
| 3978 | tmp_ptr = find_word_end(tmp_ptr); |
| 3979 | if (tmp_ptr > ptr) |
| 3980 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3981 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3982 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3983 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3984 | IObuff[len++] = ' '; |
| 3985 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 3986 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3987 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3988 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 3989 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3990 | && (IObuff[len - 2] == '?' |
| 3991 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3992 | IObuff[len++] = ' '; |
| 3993 | } |
| 3994 | /* copy as much as posible of the new word */ |
| 3995 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 3996 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 3997 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 3998 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3999 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4000 | } |
| 4001 | IObuff[len] = NUL; |
| 4002 | ptr = IObuff; |
| 4003 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4004 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4005 | continue; |
| 4006 | } |
| 4007 | } |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4008 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4009 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4010 | 0, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4011 | { |
| 4012 | found_new_match = OK; |
| 4013 | break; |
| 4014 | } |
| 4015 | } |
| 4016 | p_scs = save_p_scs; |
| 4017 | p_ws = save_p_ws; |
| 4018 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4019 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4020 | /* check if compl_curr_match has changed, (e.g. other type of |
| 4021 | * expansion added somenthing) */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4022 | if (type != 0 && compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4023 | found_new_match = OK; |
| 4024 | |
| 4025 | /* break the loop for specialized modes (use 'complete' just for the |
| 4026 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
| 4027 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4028 | || found_new_match != FAIL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4029 | { |
| 4030 | if (got_int) |
| 4031 | break; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4032 | /* Fill the popup menu as soon as possible. */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4033 | if (pum_wanted() && type != -1) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4034 | ins_compl_check_keys(0); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4035 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4036 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 4037 | || compl_interrupted) |
| 4038 | break; |
| 4039 | compl_started = TRUE; |
| 4040 | } |
| 4041 | else |
| 4042 | { |
| 4043 | /* Mark a buffer scanned when it has been scanned completely */ |
| 4044 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 4045 | ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4046 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4047 | compl_started = FALSE; |
| 4048 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4049 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4050 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4051 | |
| 4052 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4053 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 4054 | found_new_match = FAIL; |
| 4055 | |
| 4056 | i = -1; /* total of matches, unknown */ |
| 4057 | if (found_new_match == FAIL |
| 4058 | || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) |
| 4059 | i = ins_compl_make_cyclic(); |
| 4060 | |
| 4061 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4062 | * just been made cyclic then we have to move compl_curr_match to the next |
| 4063 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4064 | compl_curr_match = compl_direction == FORWARD ? old_match->cp_next |
| 4065 | : old_match->cp_prev; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4066 | if (compl_curr_match == NULL) |
| 4067 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4068 | return i; |
| 4069 | } |
| 4070 | |
| 4071 | /* Delete the old text being completed. */ |
| 4072 | static void |
| 4073 | ins_compl_delete() |
| 4074 | { |
| 4075 | int i; |
| 4076 | |
| 4077 | /* |
| 4078 | * In insert mode: Delete the typed part. |
| 4079 | * In replace mode: Put the old characters back, if any. |
| 4080 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4081 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4082 | backspace_until_column(i); |
| 4083 | changed_cline_bef_curs(); |
| 4084 | } |
| 4085 | |
| 4086 | /* Insert the new text being completed. */ |
| 4087 | static void |
| 4088 | ins_compl_insert() |
| 4089 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4090 | ins_bytes(compl_shown_match->cp_str + curwin->w_cursor.col - compl_col); |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 4091 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 4092 | compl_used_match = FALSE; |
| 4093 | else |
| 4094 | compl_used_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /* |
| 4098 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4099 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 4100 | * get more completions. If it is FALSE, then we just do nothing when there |
| 4101 | * are no more completions in a given direction. The latter case is used when |
| 4102 | * we are still in the middle of finding completions, to allow browsing |
| 4103 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4104 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 4105 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4106 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 4107 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4108 | * |
| 4109 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4110 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 4111 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4112 | */ |
| 4113 | static int |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4114 | ins_compl_next(allow_get_expansion, count, insert_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4115 | int allow_get_expansion; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4116 | int count; /* repeat completion this many times; should |
| 4117 | be at least 1 */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4118 | int insert_match; /* Insert the newly selected match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4119 | { |
| 4120 | int num_matches = -1; |
| 4121 | int i; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4122 | int todo = count; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4123 | compl_T *found_compl = NULL; |
| 4124 | int found_end = FALSE; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4125 | int advance; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4126 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4127 | if (compl_leader != NULL |
| 4128 | && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4129 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4130 | /* Set "compl_shown_match" to the actually shown match, it may differ |
| 4131 | * when "compl_leader" is used to omit some of the matches. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4132 | while (!ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4133 | compl_leader, (int)STRLEN(compl_leader)) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4134 | && compl_shown_match->cp_next != NULL |
| 4135 | && compl_shown_match->cp_next != compl_first_match) |
| 4136 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 4137 | |
| 4138 | /* If we didn't find it searching forward, and compl_shows_dir is |
| 4139 | * backward, find the last match. */ |
| 4140 | if (compl_shows_dir == BACKWARD |
| 4141 | && !ins_compl_equal(compl_shown_match, |
| 4142 | compl_leader, (int)STRLEN(compl_leader)) |
| 4143 | && (compl_shown_match->cp_next == NULL |
| 4144 | || compl_shown_match->cp_next == compl_first_match)) |
| 4145 | { |
| 4146 | while (!ins_compl_equal(compl_shown_match, |
| 4147 | compl_leader, (int)STRLEN(compl_leader)) |
| 4148 | && compl_shown_match->cp_prev != NULL |
| 4149 | && compl_shown_match->cp_prev != compl_first_match) |
| 4150 | compl_shown_match = compl_shown_match->cp_prev; |
| 4151 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | if (allow_get_expansion && insert_match |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4155 | && (!(compl_get_longest || compl_restarting) || compl_used_match)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4156 | /* Delete old text to be replaced */ |
| 4157 | ins_compl_delete(); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4158 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4159 | /* When finding the longest common text we stick at the original text, |
| 4160 | * don't let CTRL-N or CTRL-P move to the first match. */ |
| 4161 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 4162 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4163 | /* When restarting the search don't insert the first match either. */ |
| 4164 | if (compl_restarting) |
| 4165 | { |
| 4166 | advance = FALSE; |
| 4167 | compl_restarting = FALSE; |
| 4168 | } |
| 4169 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4170 | /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 4171 | * around. */ |
| 4172 | while (--todo >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4173 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4174 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4175 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4176 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4177 | found_end = (compl_first_match != NULL |
| 4178 | && (compl_shown_match->cp_next == compl_first_match |
| 4179 | || compl_shown_match == compl_first_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4180 | } |
| 4181 | else if (compl_shows_dir == BACKWARD |
| 4182 | && compl_shown_match->cp_prev != NULL) |
| 4183 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4184 | found_end = (compl_shown_match == compl_first_match); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4185 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4186 | found_end |= (compl_shown_match == compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4187 | } |
| 4188 | else |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4189 | { |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4190 | if (!allow_get_expansion) |
| 4191 | { |
| 4192 | if (advance) |
| 4193 | { |
| 4194 | if (compl_shows_dir == BACKWARD) |
| 4195 | compl_pending -= todo + 1; |
| 4196 | else |
| 4197 | compl_pending += todo + 1; |
| 4198 | } |
| 4199 | return -1; |
| 4200 | } |
| 4201 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4202 | if (advance) |
| 4203 | { |
| 4204 | if (compl_shows_dir == BACKWARD) |
| 4205 | --compl_pending; |
| 4206 | else |
| 4207 | ++compl_pending; |
| 4208 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4209 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4210 | /* Find matches. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4211 | num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4212 | |
| 4213 | /* handle any pending completions */ |
| 4214 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4215 | && advance) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4216 | { |
| 4217 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 4218 | { |
| 4219 | compl_shown_match = compl_shown_match->cp_next; |
| 4220 | --compl_pending; |
| 4221 | } |
| 4222 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 4223 | { |
| 4224 | compl_shown_match = compl_shown_match->cp_prev; |
| 4225 | ++compl_pending; |
| 4226 | } |
| 4227 | else |
| 4228 | break; |
| 4229 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4230 | found_end = FALSE; |
| 4231 | } |
| 4232 | if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 |
| 4233 | && compl_leader != NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4234 | && !ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4235 | compl_leader, (int)STRLEN(compl_leader))) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4236 | ++todo; |
| 4237 | else |
| 4238 | /* Remember a matching item. */ |
| 4239 | found_compl = compl_shown_match; |
| 4240 | |
| 4241 | /* Stop at the end of the list when we found a usable match. */ |
| 4242 | if (found_end) |
| 4243 | { |
| 4244 | if (found_compl != NULL) |
| 4245 | { |
| 4246 | compl_shown_match = found_compl; |
| 4247 | break; |
| 4248 | } |
| 4249 | todo = 1; /* use first usable match after wrapping around */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4250 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4251 | } |
| 4252 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4253 | /* Insert the text of the new completion, or the compl_leader. */ |
| 4254 | if (insert_match) |
| 4255 | { |
| 4256 | if (!compl_get_longest || compl_used_match) |
| 4257 | ins_compl_insert(); |
| 4258 | else |
| 4259 | ins_bytes(compl_leader + curwin->w_cursor.col - compl_col); |
| 4260 | } |
| 4261 | else |
| 4262 | compl_used_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4263 | |
| 4264 | if (!allow_get_expansion) |
| 4265 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4266 | /* may undisplay the popup menu first */ |
| 4267 | ins_compl_upd_pum(); |
| 4268 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4269 | /* redraw to show the user what was inserted */ |
| 4270 | update_screen(0); |
| 4271 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4272 | /* display the updated popup menu */ |
| 4273 | ins_compl_show_pum(); |
Bram Moolenaar | 1471681 | 2006-05-04 21:54:08 +0000 | [diff] [blame] | 4274 | #ifdef FEAT_GUI |
| 4275 | if (gui.in_use) |
| 4276 | { |
| 4277 | /* Show the cursor after the match, not after the redrawn text. */ |
| 4278 | setcursor(); |
| 4279 | out_flush(); |
| 4280 | gui_update_cursor(FALSE, FALSE); |
| 4281 | } |
| 4282 | #endif |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4283 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4284 | /* Delete old text to be replaced, since we're still searching and |
| 4285 | * don't want to match ourselves! */ |
| 4286 | ins_compl_delete(); |
| 4287 | } |
| 4288 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4289 | /* Enter will select a match when the match wasn't inserted and the popup |
| 4290 | * menu is visislbe. */ |
| 4291 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 4292 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * Show the file name for the match (if any) |
| 4295 | * Truncate the file name to avoid a wait for return. |
| 4296 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4297 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4298 | { |
| 4299 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4300 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | if (i <= 0) |
| 4302 | i = 0; |
| 4303 | else |
| 4304 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4305 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4306 | msg(IObuff); |
| 4307 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 4308 | } |
| 4309 | |
| 4310 | return num_matches; |
| 4311 | } |
| 4312 | |
| 4313 | /* |
| 4314 | * Call this while finding completions, to check whether the user has hit a key |
| 4315 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4316 | * mode. Also, when compl_pending is not zero, show a completion as soon as |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4317 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4318 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | */ |
| 4320 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4321 | ins_compl_check_keys(frequency) |
| 4322 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4323 | { |
| 4324 | static int count = 0; |
| 4325 | |
| 4326 | int c; |
| 4327 | |
| 4328 | /* Don't check when reading keys from a script. That would break the test |
| 4329 | * scripts */ |
| 4330 | if (using_script()) |
| 4331 | return; |
| 4332 | |
| 4333 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4334 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4335 | return; |
| 4336 | count = 0; |
| 4337 | |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4338 | /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 4339 | * can't do its work correctly. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4340 | c = vpeekc_any(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4341 | if (c != NUL) |
| 4342 | { |
| 4343 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 4344 | { |
| 4345 | c = safe_vgetc(); /* Eat the character */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4346 | compl_shows_dir = ins_compl_key2dir(c); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4347 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 4348 | c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4349 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4350 | else |
| 4351 | { |
| 4352 | /* Need to get the character to have KeyTyped set. We'll put it |
| 4353 | * back with vungetc() below. */ |
| 4354 | c = safe_vgetc(); |
| 4355 | |
| 4356 | /* Don't interrupt completion when the character wasn't typed, |
| 4357 | * e.g., when doing @q to replay keys. */ |
| 4358 | if (c != Ctrl_R && KeyTyped) |
| 4359 | compl_interrupted = TRUE; |
| 4360 | |
| 4361 | vungetc(c); |
| 4362 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4363 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4364 | if (compl_pending != 0 && !got_int) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4365 | { |
| 4366 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 4367 | |
| 4368 | compl_pending = 0; |
| 4369 | (void)ins_compl_next(FALSE, todo, TRUE); |
| 4370 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | /* |
| 4374 | * Decide the direction of Insert mode complete from the key typed. |
| 4375 | * Returns BACKWARD or FORWARD. |
| 4376 | */ |
| 4377 | static int |
| 4378 | ins_compl_key2dir(c) |
| 4379 | int c; |
| 4380 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4381 | if (c == Ctrl_P || c == Ctrl_L |
| 4382 | || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP |
| 4383 | || c == K_S_UP || c == K_UP))) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4384 | return BACKWARD; |
| 4385 | return FORWARD; |
| 4386 | } |
| 4387 | |
| 4388 | /* |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4389 | * Return TRUE for keys that are used for completion only when the popup menu |
| 4390 | * is visible. |
| 4391 | */ |
| 4392 | static int |
| 4393 | ins_compl_pum_key(c) |
| 4394 | int c; |
| 4395 | { |
| 4396 | 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] | 4397 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 4398 | || c == K_UP || c == K_DOWN); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| 4401 | /* |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4402 | * Decide the number of completions to move forward. |
| 4403 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 4404 | */ |
| 4405 | static int |
| 4406 | ins_compl_key2count(c) |
| 4407 | int c; |
| 4408 | { |
| 4409 | int h; |
| 4410 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4411 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4412 | { |
| 4413 | h = pum_get_height(); |
| 4414 | if (h > 3) |
| 4415 | h -= 2; /* keep some context */ |
| 4416 | return h; |
| 4417 | } |
| 4418 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
| 4421 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4422 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 4423 | * to change the currently selected completion. |
| 4424 | */ |
| 4425 | static int |
| 4426 | ins_compl_use_match(c) |
| 4427 | int c; |
| 4428 | { |
| 4429 | switch (c) |
| 4430 | { |
| 4431 | case K_UP: |
| 4432 | case K_DOWN: |
| 4433 | case K_PAGEDOWN: |
| 4434 | case K_KPAGEDOWN: |
| 4435 | case K_S_DOWN: |
| 4436 | case K_PAGEUP: |
| 4437 | case K_KPAGEUP: |
| 4438 | case K_S_UP: |
| 4439 | return FALSE; |
| 4440 | } |
| 4441 | return TRUE; |
| 4442 | } |
| 4443 | |
| 4444 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4445 | * Do Insert mode completion. |
| 4446 | * Called when character "c" was typed, which has a meaning for completion. |
| 4447 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 4448 | */ |
| 4449 | static int |
| 4450 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4451 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4452 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4453 | char_u *line; |
| 4454 | int startcol = 0; /* column where searched text starts */ |
| 4455 | colnr_T curs_col; /* cursor column */ |
| 4456 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4458 | compl_direction = ins_compl_key2dir(c); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4459 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | { |
| 4461 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 4462 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4463 | did_ai = FALSE; |
| 4464 | #ifdef FEAT_SMARTINDENT |
| 4465 | did_si = FALSE; |
| 4466 | can_si = FALSE; |
| 4467 | can_si_back = FALSE; |
| 4468 | #endif |
| 4469 | if (stop_arrow() == FAIL) |
| 4470 | return FAIL; |
| 4471 | |
| 4472 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4473 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4474 | compl_pending = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4475 | |
| 4476 | /* 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] | 4477 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 4478 | * instead of expand the one before the cursor, in word-wise if |
| 4479 | * "compl_startpos" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4480 | * is not in the same line as the cursor then fix it (the line has |
| 4481 | * been split because it was longer than 'tw'). if SOL is set then |
| 4482 | * skip the previous pattern, a word at the beginning of the line has |
| 4483 | * been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4484 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 4485 | && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4486 | { |
| 4487 | /* |
| 4488 | * it is a continued search |
| 4489 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4490 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4491 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 4492 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4493 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4494 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4495 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4496 | /* line (probably) wrapped, set compl_startpos to the |
| 4497 | * first non_blank in the line, if it is not a wordchar |
| 4498 | * include it to get a better pattern, but then we don't |
| 4499 | * want the "\\<" prefix, check it bellow */ |
| 4500 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 4501 | compl_startpos.col = compl_col; |
| 4502 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4503 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4504 | } |
| 4505 | else |
| 4506 | { |
| 4507 | /* S_IPOS was set when we inserted a word that was at the |
| 4508 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4509 | * mode but first we need to redefine compl_startpos */ |
| 4510 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4511 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4512 | compl_cont_status |= CONT_SOL; |
| 4513 | compl_startpos.col = (colnr_T)(skipwhite( |
| 4514 | line + compl_length |
| 4515 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4516 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4517 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4518 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4519 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4520 | /* 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] | 4521 | * have enough space? just being paranoic */ |
| 4522 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4523 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4524 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4525 | compl_cont_status &= ~CONT_SOL; |
| 4526 | compl_length = (IOSIZE - MIN_SPACE); |
| 4527 | compl_col = curwin->w_cursor.col - compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4529 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 4530 | if (compl_length < 1) |
| 4531 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4532 | } |
| 4533 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4534 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4535 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4536 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4537 | } |
| 4538 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4539 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4540 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4541 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4542 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4543 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4544 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4545 | compl_cont_status = 0; |
| 4546 | compl_cont_status |= CONT_N_ADDS; |
| 4547 | compl_startpos = curwin->w_cursor; |
| 4548 | startcol = (int)curs_col; |
| 4549 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4550 | } |
| 4551 | |
| 4552 | /* Work out completion pattern and original text -- webb */ |
| 4553 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 4554 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4555 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4556 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4557 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4558 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4559 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4560 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4561 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4562 | compl_col += ++startcol; |
| 4563 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4564 | } |
| 4565 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4566 | compl_pattern = str_foldcase(line + compl_col, |
| 4567 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4569 | compl_pattern = vim_strnsave(line + compl_col, |
| 4570 | compl_length); |
| 4571 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4572 | return FAIL; |
| 4573 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4574 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4575 | { |
| 4576 | char_u *prefix = (char_u *)"\\<"; |
| 4577 | |
| 4578 | /* we need 3 extra chars, 1 for the NUL and |
| 4579 | * 2 >= strlen(prefix) -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4580 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 4581 | compl_length) + 3); |
| 4582 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4583 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4584 | if (!vim_iswordp(line + compl_col) |
| 4585 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4586 | && ( |
| 4587 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4588 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4589 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4590 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4591 | #endif |
| 4592 | ))) |
| 4593 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4594 | STRCPY((char *)compl_pattern, prefix); |
| 4595 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 4596 | line + compl_col, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4597 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4598 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4599 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4600 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4601 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4602 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4603 | #endif |
| 4604 | ) |
| 4605 | { |
| 4606 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4607 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 4608 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4610 | compl_col += curs_col; |
| 4611 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4612 | } |
| 4613 | else |
| 4614 | { |
| 4615 | #ifdef FEAT_MBYTE |
| 4616 | /* Search the point of change class of multibyte character |
| 4617 | * or not a word single byte character backward. */ |
| 4618 | if (has_mbyte) |
| 4619 | { |
| 4620 | int base_class; |
| 4621 | int head_off; |
| 4622 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4623 | startcol -= (*mb_head_off)(line, line + startcol); |
| 4624 | base_class = mb_get_class(line + startcol); |
| 4625 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4626 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4627 | head_off = (*mb_head_off)(line, line + startcol); |
| 4628 | if (base_class != mb_get_class(line + startcol |
| 4629 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4630 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4631 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4632 | } |
| 4633 | } |
| 4634 | else |
| 4635 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4636 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4637 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4638 | compl_col += ++startcol; |
| 4639 | compl_length = (int)curs_col - startcol; |
| 4640 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4641 | { |
| 4642 | /* Only match word with at least two chars -- webb |
| 4643 | * there's no need to call quote_meta, |
| 4644 | * alloc(7) is enough -- Acevedo |
| 4645 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4646 | compl_pattern = alloc(7); |
| 4647 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4648 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4649 | STRCPY((char *)compl_pattern, "\\<"); |
| 4650 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 4651 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4652 | } |
| 4653 | else |
| 4654 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4655 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
| 4656 | compl_length) + 3); |
| 4657 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4658 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4659 | STRCPY((char *)compl_pattern, "\\<"); |
| 4660 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 4661 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4662 | } |
| 4663 | } |
| 4664 | } |
| 4665 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4666 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4667 | compl_col = (colnr_T)(skipwhite(line) - line); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4668 | compl_length = (int)curs_col - (int)compl_col; |
| 4669 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 4670 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4671 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4672 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 4673 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4674 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4675 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4676 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4677 | return FAIL; |
| 4678 | } |
| 4679 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 4680 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4681 | while (--startcol >= 0 && vim_isfilec(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4682 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4683 | compl_col += ++startcol; |
| 4684 | compl_length = (int)curs_col - startcol; |
| 4685 | compl_pattern = addstar(line + compl_col, compl_length, |
| 4686 | EXPAND_FILES); |
| 4687 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4688 | return FAIL; |
| 4689 | } |
| 4690 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 4691 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4692 | compl_pattern = vim_strnsave(line, curs_col); |
| 4693 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4694 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4695 | set_cmd_context(&compl_xp, compl_pattern, |
| 4696 | (int)STRLEN(compl_pattern), curs_col); |
| 4697 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 4698 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 4699 | /* No completion possible, use an empty pattern to get a |
| 4700 | * "pattern not found" message. */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 4701 | compl_col = curs_col; |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 4702 | else |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 4703 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern); |
| 4704 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4705 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4706 | 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] | 4707 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4708 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4709 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4710 | * Call user defined function 'completefunc' with "a:findstart" |
| 4711 | * 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] | 4712 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4713 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4714 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4715 | char_u *funcname; |
| 4716 | pos_T pos; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4717 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4718 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4719 | * string */ |
| 4720 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 4721 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 4722 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4723 | { |
| 4724 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 4725 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4726 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4727 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4728 | |
| 4729 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4730 | args[1] = NULL; |
| 4731 | pos = curwin->w_cursor; |
| 4732 | col = call_func_retnr(funcname, 2, args, FALSE); |
| 4733 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4734 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4735 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4736 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4737 | compl_col = col; |
| 4738 | if ((colnr_T)compl_col > curs_col) |
| 4739 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4740 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4741 | /* Setup variables for completion. Need to obtain "line" again, |
| 4742 | * it may have become invalid. */ |
| 4743 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4744 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4745 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4746 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4747 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4748 | return FAIL; |
| 4749 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4750 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 4751 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 4752 | #ifdef FEAT_SPELL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 4753 | if (spell_bad_len > 0) |
| 4754 | compl_col = curs_col - spell_bad_len; |
| 4755 | else |
| 4756 | compl_col = spell_word_start(startcol); |
| 4757 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 4758 | { |
| 4759 | compl_length = 0; |
| 4760 | compl_col = curs_col; |
| 4761 | } |
| 4762 | else |
| 4763 | { |
| 4764 | spell_expand_check_cap(compl_col); |
| 4765 | compl_length = (int)curs_col - compl_col; |
| 4766 | } |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 4767 | /* Need to obtain "line" again, it may have become invalid. */ |
| 4768 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4769 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 4770 | if (compl_pattern == NULL) |
| 4771 | #endif |
| 4772 | return FAIL; |
| 4773 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4774 | else |
| 4775 | { |
| 4776 | EMSG2(_(e_intern2), "ins_complete()"); |
| 4777 | return FAIL; |
| 4778 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4779 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4780 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4781 | { |
| 4782 | edit_submode_pre = (char_u *)_(" Adding"); |
| 4783 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4784 | { |
| 4785 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 4786 | #ifdef FEAT_COMMENTS |
| 4787 | char_u *old = curbuf->b_p_com; |
| 4788 | |
| 4789 | curbuf->b_p_com = (char_u *)""; |
| 4790 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4791 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4792 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4793 | ins_eol('\r'); |
| 4794 | #ifdef FEAT_COMMENTS |
| 4795 | curbuf->b_p_com = old; |
| 4796 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4797 | compl_length = 0; |
| 4798 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4799 | } |
| 4800 | } |
| 4801 | else |
| 4802 | { |
| 4803 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4804 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4807 | if (compl_cont_status & CONT_LOCAL) |
| 4808 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4809 | else |
| 4810 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 4811 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 4812 | /* Always add completion for the original text. */ |
| 4813 | vim_free(compl_orig_text); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4814 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 4815 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4816 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4817 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4818 | vim_free(compl_pattern); |
| 4819 | compl_pattern = NULL; |
| 4820 | vim_free(compl_orig_text); |
| 4821 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | return FAIL; |
| 4823 | } |
| 4824 | |
| 4825 | /* showmode might reset the internal line pointers, so it must |
| 4826 | * be called before line = ml_get(), or when this address is no |
| 4827 | * longer needed. -- Acevedo. |
| 4828 | */ |
| 4829 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 4830 | edit_submode_highl = HLF_COUNT; |
| 4831 | showmode(); |
| 4832 | edit_submode_extra = NULL; |
| 4833 | out_flush(); |
| 4834 | } |
| 4835 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4836 | compl_shown_match = compl_curr_match; |
| 4837 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4838 | |
| 4839 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4840 | * Find next match (and following matches). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4841 | */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4842 | 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] | 4843 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4844 | /* may undisplay the popup menu */ |
| 4845 | ins_compl_upd_pum(); |
| 4846 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4847 | if (n > 1) /* all matches have been found */ |
| 4848 | compl_matches = n; |
| 4849 | compl_curr_match = compl_shown_match; |
| 4850 | compl_direction = compl_shows_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4851 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4852 | /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 4853 | * mode. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4854 | if (got_int && !global_busy) |
| 4855 | { |
| 4856 | (void)vgetc(); |
| 4857 | got_int = FALSE; |
| 4858 | } |
| 4859 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4860 | /* 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] | 4861 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4862 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4863 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 4864 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4865 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 4866 | edit_submode_highl = HLF_E; |
| 4867 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 4868 | * because we couldn't expand anything at first place, but if we used |
| 4869 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 4870 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4871 | if ( compl_length > 1 |
| 4872 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4873 | || (ctrl_x_mode != 0 |
| 4874 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 4875 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4876 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4879 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4880 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4881 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4882 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4883 | |
| 4884 | if (edit_submode_extra == NULL) |
| 4885 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4886 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4887 | { |
| 4888 | edit_submode_extra = (char_u *)_("Back at original"); |
| 4889 | edit_submode_highl = HLF_W; |
| 4890 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4891 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4892 | { |
| 4893 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 4894 | edit_submode_highl = HLF_COUNT; |
| 4895 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4896 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4897 | { |
| 4898 | edit_submode_extra = (char_u *)_("The only match"); |
| 4899 | edit_submode_highl = HLF_COUNT; |
| 4900 | } |
| 4901 | else |
| 4902 | { |
| 4903 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4904 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4906 | int number = 0; |
| 4907 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4908 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4909 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4910 | { |
| 4911 | /* search backwards for the first valid (!= -1) number. |
| 4912 | * This should normally succeed already at the first loop |
| 4913 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4914 | for (match = compl_curr_match->cp_prev; match != NULL |
| 4915 | && match != compl_first_match; |
| 4916 | match = match->cp_prev) |
| 4917 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4918 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4919 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4920 | break; |
| 4921 | } |
| 4922 | if (match != NULL) |
| 4923 | /* go up and assign all numbers which are not assigned |
| 4924 | * yet */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4925 | for (match = match->cp_next; |
| 4926 | match != NULL && match->cp_number == -1; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4927 | match = match->cp_next) |
| 4928 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4929 | } |
| 4930 | else /* BACKWARD */ |
| 4931 | { |
| 4932 | /* search forwards (upwards) for the first valid (!= -1) |
| 4933 | * number. This should normally succeed already at the |
| 4934 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4935 | for (match = compl_curr_match->cp_next; match != NULL |
| 4936 | && match != compl_first_match; |
| 4937 | match = match->cp_next) |
| 4938 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4939 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4940 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4941 | break; |
| 4942 | } |
| 4943 | if (match != NULL) |
| 4944 | /* go down and assign all numbers which are not |
| 4945 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4946 | for (match = match->cp_prev; match |
| 4947 | && match->cp_number == -1; |
| 4948 | match = match->cp_prev) |
| 4949 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | } |
| 4951 | } |
| 4952 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4953 | /* The match should always have a sequence number now, this is |
| 4954 | * just a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4955 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4956 | { |
| 4957 | /* Space for 10 text chars. + 2x10-digit no.s */ |
| 4958 | static char_u match_ref[31]; |
| 4959 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4960 | if (compl_matches > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4961 | sprintf((char *)IObuff, _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4962 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4963 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4964 | sprintf((char *)IObuff, _("match %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4965 | compl_curr_match->cp_number); |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4966 | vim_strncpy(match_ref, IObuff, 30); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4967 | edit_submode_extra = match_ref; |
| 4968 | edit_submode_highl = HLF_R; |
| 4969 | if (dollar_vcol) |
| 4970 | curs_columns(FALSE); |
| 4971 | } |
| 4972 | } |
| 4973 | } |
| 4974 | |
| 4975 | /* Show a message about what (completion) mode we're in. */ |
| 4976 | showmode(); |
| 4977 | if (edit_submode_extra != NULL) |
| 4978 | { |
| 4979 | if (!p_smd) |
| 4980 | msg_attr(edit_submode_extra, |
| 4981 | edit_submode_highl < HLF_COUNT |
| 4982 | ? hl_attr(edit_submode_highl) : 0); |
| 4983 | } |
| 4984 | else |
| 4985 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 4986 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4987 | /* Show the popup menu, unless we got interrupted. */ |
| 4988 | if (!compl_interrupted) |
| 4989 | { |
| 4990 | /* RedrawingDisabled may be set when invoked through complete(). */ |
| 4991 | n = RedrawingDisabled; |
| 4992 | RedrawingDisabled = 0; |
| 4993 | ins_compl_show_pum(); |
| 4994 | setcursor(); |
| 4995 | RedrawingDisabled = n; |
| 4996 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4997 | compl_was_interrupted = compl_interrupted; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 4998 | compl_interrupted = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4999 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | return OK; |
| 5001 | } |
| 5002 | |
| 5003 | /* |
| 5004 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 5005 | * If dest is not NULL the chars. are copied there quoting (with |
| 5006 | * a backslash) the metachars, and dest would be NUL terminated. |
| 5007 | * Returns the length (needed) of dest |
| 5008 | */ |
| 5009 | static int |
| 5010 | quote_meta(dest, src, len) |
| 5011 | char_u *dest; |
| 5012 | char_u *src; |
| 5013 | int len; |
| 5014 | { |
| 5015 | int m; |
| 5016 | |
| 5017 | for (m = len; --len >= 0; src++) |
| 5018 | { |
| 5019 | switch (*src) |
| 5020 | { |
| 5021 | case '.': |
| 5022 | case '*': |
| 5023 | case '[': |
| 5024 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5025 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5026 | break; |
| 5027 | case '~': |
| 5028 | if (!p_magic) /* quote these only if magic is set */ |
| 5029 | break; |
| 5030 | case '\\': |
| 5031 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5032 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5033 | break; |
| 5034 | case '^': /* currently it's not needed. */ |
| 5035 | case '$': |
| 5036 | m++; |
| 5037 | if (dest != NULL) |
| 5038 | *dest++ = '\\'; |
| 5039 | break; |
| 5040 | } |
| 5041 | if (dest != NULL) |
| 5042 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5043 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | /* Copy remaining bytes of a multibyte character. */ |
| 5045 | if (has_mbyte) |
| 5046 | { |
| 5047 | int i, mb_len; |
| 5048 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5049 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5050 | if (mb_len > 0 && len >= mb_len) |
| 5051 | for (i = 0; i < mb_len; ++i) |
| 5052 | { |
| 5053 | --len; |
| 5054 | ++src; |
| 5055 | if (dest != NULL) |
| 5056 | *dest++ = *src; |
| 5057 | } |
| 5058 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5059 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5060 | } |
| 5061 | if (dest != NULL) |
| 5062 | *dest = NUL; |
| 5063 | |
| 5064 | return m; |
| 5065 | } |
| 5066 | #endif /* FEAT_INS_EXPAND */ |
| 5067 | |
| 5068 | /* |
| 5069 | * Next character is interpreted literally. |
| 5070 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 5071 | * If one or two digits are entered, the next character is given to vungetc(). |
| 5072 | * For Unicode a character > 255 may be returned. |
| 5073 | */ |
| 5074 | int |
| 5075 | get_literal() |
| 5076 | { |
| 5077 | int cc; |
| 5078 | int nc; |
| 5079 | int i; |
| 5080 | int hex = FALSE; |
| 5081 | int octal = FALSE; |
| 5082 | #ifdef FEAT_MBYTE |
| 5083 | int unicode = 0; |
| 5084 | #endif |
| 5085 | |
| 5086 | if (got_int) |
| 5087 | return Ctrl_C; |
| 5088 | |
| 5089 | #ifdef FEAT_GUI |
| 5090 | /* |
| 5091 | * In GUI there is no point inserting the internal code for a special key. |
| 5092 | * It is more useful to insert the string "<KEY>" instead. This would |
| 5093 | * probably be useful in a text window too, but it would not be |
| 5094 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 5095 | */ |
| 5096 | if (gui.in_use) |
| 5097 | ++allow_keys; |
| 5098 | #endif |
| 5099 | #ifdef USE_ON_FLY_SCROLL |
| 5100 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 5101 | #endif |
| 5102 | ++no_mapping; /* don't map the next key hits */ |
| 5103 | cc = 0; |
| 5104 | i = 0; |
| 5105 | for (;;) |
| 5106 | { |
| 5107 | do |
| 5108 | nc = safe_vgetc(); |
| 5109 | while (nc == K_IGNORE || nc == K_VER_SCROLLBAR |
| 5110 | || nc == K_HOR_SCROLLBAR); |
| 5111 | #ifdef FEAT_CMDL_INFO |
| 5112 | if (!(State & CMDLINE) |
| 5113 | # ifdef FEAT_MBYTE |
| 5114 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 5115 | # endif |
| 5116 | ) |
| 5117 | add_to_showcmd(nc); |
| 5118 | #endif |
| 5119 | if (nc == 'x' || nc == 'X') |
| 5120 | hex = TRUE; |
| 5121 | else if (nc == 'o' || nc == 'O') |
| 5122 | octal = TRUE; |
| 5123 | #ifdef FEAT_MBYTE |
| 5124 | else if (nc == 'u' || nc == 'U') |
| 5125 | unicode = nc; |
| 5126 | #endif |
| 5127 | else |
| 5128 | { |
| 5129 | if (hex |
| 5130 | #ifdef FEAT_MBYTE |
| 5131 | || unicode != 0 |
| 5132 | #endif |
| 5133 | ) |
| 5134 | { |
| 5135 | if (!vim_isxdigit(nc)) |
| 5136 | break; |
| 5137 | cc = cc * 16 + hex2nr(nc); |
| 5138 | } |
| 5139 | else if (octal) |
| 5140 | { |
| 5141 | if (nc < '0' || nc > '7') |
| 5142 | break; |
| 5143 | cc = cc * 8 + nc - '0'; |
| 5144 | } |
| 5145 | else |
| 5146 | { |
| 5147 | if (!VIM_ISDIGIT(nc)) |
| 5148 | break; |
| 5149 | cc = cc * 10 + nc - '0'; |
| 5150 | } |
| 5151 | |
| 5152 | ++i; |
| 5153 | } |
| 5154 | |
| 5155 | if (cc > 255 |
| 5156 | #ifdef FEAT_MBYTE |
| 5157 | && unicode == 0 |
| 5158 | #endif |
| 5159 | ) |
| 5160 | cc = 255; /* limit range to 0-255 */ |
| 5161 | nc = 0; |
| 5162 | |
| 5163 | if (hex) /* hex: up to two chars */ |
| 5164 | { |
| 5165 | if (i >= 2) |
| 5166 | break; |
| 5167 | } |
| 5168 | #ifdef FEAT_MBYTE |
| 5169 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 5170 | { |
| 5171 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 5172 | break; |
| 5173 | } |
| 5174 | #endif |
| 5175 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 5176 | break; |
| 5177 | } |
| 5178 | if (i == 0) /* no number entered */ |
| 5179 | { |
| 5180 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 5181 | { |
| 5182 | cc = '\n'; |
| 5183 | nc = 0; |
| 5184 | } |
| 5185 | else |
| 5186 | { |
| 5187 | cc = nc; |
| 5188 | nc = 0; |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | if (cc == 0) /* NUL is stored as NL */ |
| 5193 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5194 | #ifdef FEAT_MBYTE |
| 5195 | if (enc_dbcs && (cc & 0xff) == 0) |
| 5196 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 5197 | second byte will cause trouble! */ |
| 5198 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5199 | |
| 5200 | --no_mapping; |
| 5201 | #ifdef FEAT_GUI |
| 5202 | if (gui.in_use) |
| 5203 | --allow_keys; |
| 5204 | #endif |
| 5205 | if (nc) |
| 5206 | vungetc(nc); |
| 5207 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 5208 | return cc; |
| 5209 | } |
| 5210 | |
| 5211 | /* |
| 5212 | * Insert character, taking care of special keys and mod_mask |
| 5213 | */ |
| 5214 | static void |
| 5215 | insert_special(c, allow_modmask, ctrlv) |
| 5216 | int c; |
| 5217 | int allow_modmask; |
| 5218 | int ctrlv; /* c was typed after CTRL-V */ |
| 5219 | { |
| 5220 | char_u *p; |
| 5221 | int len; |
| 5222 | |
| 5223 | /* |
| 5224 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 5225 | * inserted with ins_str(), so as not to replace characters in replace |
| 5226 | * mode. |
| 5227 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 5228 | * unless 'allow_modmask' is TRUE. |
| 5229 | */ |
| 5230 | #ifdef MACOS |
| 5231 | /* Command-key never produces a normal key */ |
| 5232 | if (mod_mask & MOD_MASK_CMD) |
| 5233 | allow_modmask = TRUE; |
| 5234 | #endif |
| 5235 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 5236 | { |
| 5237 | p = get_special_key_name(c, mod_mask); |
| 5238 | len = (int)STRLEN(p); |
| 5239 | c = p[len - 1]; |
| 5240 | if (len > 2) |
| 5241 | { |
| 5242 | if (stop_arrow() == FAIL) |
| 5243 | return; |
| 5244 | p[len - 1] = NUL; |
| 5245 | ins_str(p); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5246 | AppendToRedobuffLit(p, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5247 | ctrlv = FALSE; |
| 5248 | } |
| 5249 | } |
| 5250 | if (stop_arrow() == OK) |
| 5251 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 5252 | } |
| 5253 | |
| 5254 | /* |
| 5255 | * Special characters in this context are those that need processing other |
| 5256 | * than the simple insertion that can be performed here. This includes ESC |
| 5257 | * which terminates the insert, and CR/NL which need special processing to |
| 5258 | * open up a new line. This routine tries to optimize insertions performed by |
| 5259 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 5260 | * stop and defer processing to the "normal" mechanism. |
| 5261 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 5262 | */ |
| 5263 | #ifdef EBCDIC |
| 5264 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 5265 | #else |
| 5266 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 5267 | #endif |
| 5268 | |
| 5269 | #ifdef FEAT_MBYTE |
| 5270 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 5271 | #else |
| 5272 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 5273 | #endif |
| 5274 | |
| 5275 | void |
| 5276 | insertchar(c, flags, second_indent) |
| 5277 | int c; /* character to insert or NUL */ |
| 5278 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 5279 | int second_indent; /* indent for second line if >= 0 */ |
| 5280 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5281 | int textwidth; |
| 5282 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5283 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5285 | int fo_ins_blank; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5286 | |
| 5287 | textwidth = comp_textwidth(flags & INSCHAR_FORMAT); |
| 5288 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | |
| 5290 | /* |
| 5291 | * Try to break the line in two or more pieces when: |
| 5292 | * - Always do this if we have been called to do formatting only. |
| 5293 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 5294 | * ends in white space. |
| 5295 | * - Otherwise: |
| 5296 | * - Don't do this if inserting a blank |
| 5297 | * - Don't do this if an existing character is being replaced, unless |
| 5298 | * we're in VREPLACE mode. |
| 5299 | * - Do this if the cursor is not on the line where insert started |
| 5300 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 5301 | * before the insert. |
| 5302 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 5303 | * before 'textwidth' |
| 5304 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5305 | if (textwidth > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5306 | && ((flags & INSCHAR_FORMAT) |
| 5307 | || (!vim_iswhite(c) |
| 5308 | && !((State & REPLACE_FLAG) |
| 5309 | #ifdef FEAT_VREPLACE |
| 5310 | && !(State & VREPLACE_FLAG) |
| 5311 | #endif |
| 5312 | && *ml_get_cursor() != NUL) |
| 5313 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 5314 | || ((!has_format_option(FO_INS_LONG) |
| 5315 | || Insstart_textlen <= (colnr_T)textwidth) |
| 5316 | && (!fo_ins_blank |
| 5317 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 5318 | )))))) |
| 5319 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5320 | /* Format with 'formatexpr' when it's set. Use internal formatting |
| 5321 | * when 'formatexpr' isn't set or it returns non-zero. */ |
| 5322 | #if defined(FEAT_EVAL) |
| 5323 | if (*curbuf->b_p_fex == NUL |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 5324 | || fex_format(curwin->w_cursor.lnum, 1L, c) != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5326 | internal_format(textwidth, second_indent, flags, c == NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5327 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5328 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | if (c == NUL) /* only formatting was wanted */ |
| 5330 | return; |
| 5331 | |
| 5332 | #ifdef FEAT_COMMENTS |
| 5333 | /* Check whether this character should end a comment. */ |
| 5334 | if (did_ai && (int)c == end_comment_pending) |
| 5335 | { |
| 5336 | char_u *line; |
| 5337 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 5338 | int middle_len, end_len; |
| 5339 | int i; |
| 5340 | |
| 5341 | /* |
| 5342 | * Need to remove existing (middle) comment leader and insert end |
| 5343 | * comment leader. First, check what comment leader we can find. |
| 5344 | */ |
| 5345 | i = get_leader_len(line = ml_get_curline(), &p, FALSE); |
| 5346 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 5347 | { |
| 5348 | /* Skip middle-comment string */ |
| 5349 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 5350 | ++p; |
| 5351 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5352 | /* Don't count trailing white space for middle_len */ |
| 5353 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 5354 | --middle_len; |
| 5355 | |
| 5356 | /* Find the end-comment string */ |
| 5357 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 5358 | ++p; |
| 5359 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5360 | |
| 5361 | /* Skip white space before the cursor */ |
| 5362 | i = curwin->w_cursor.col; |
| 5363 | while (--i >= 0 && vim_iswhite(line[i])) |
| 5364 | ; |
| 5365 | i++; |
| 5366 | |
| 5367 | /* Skip to before the middle leader */ |
| 5368 | i -= middle_len; |
| 5369 | |
| 5370 | /* Check some expected things before we go on */ |
| 5371 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 5372 | { |
| 5373 | /* Backspace over all the stuff we want to replace */ |
| 5374 | backspace_until_column(i); |
| 5375 | |
| 5376 | /* |
| 5377 | * Insert the end-comment string, except for the last |
| 5378 | * character, which will get inserted as normal later. |
| 5379 | */ |
| 5380 | ins_bytes_len(lead_end, end_len - 1); |
| 5381 | } |
| 5382 | } |
| 5383 | } |
| 5384 | end_comment_pending = NUL; |
| 5385 | #endif |
| 5386 | |
| 5387 | did_ai = FALSE; |
| 5388 | #ifdef FEAT_SMARTINDENT |
| 5389 | did_si = FALSE; |
| 5390 | can_si = FALSE; |
| 5391 | can_si_back = FALSE; |
| 5392 | #endif |
| 5393 | |
| 5394 | /* |
| 5395 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 5396 | * This speeds up normal text input considerably. |
| 5397 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 5398 | * need to re-indent at a ':', or any other character (but not what |
| 5399 | * 'paste' is set).. |
| 5400 | */ |
| 5401 | #ifdef USE_ON_FLY_SCROLL |
| 5402 | dont_scroll = FALSE; /* allow scrolling here */ |
| 5403 | #endif |
| 5404 | |
| 5405 | if ( !ISSPECIAL(c) |
| 5406 | #ifdef FEAT_MBYTE |
| 5407 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 5408 | #endif |
| 5409 | && vpeekc() != NUL |
| 5410 | && !(State & REPLACE_FLAG) |
| 5411 | #ifdef FEAT_CINDENT |
| 5412 | && !cindent_on() |
| 5413 | #endif |
| 5414 | #ifdef FEAT_RIGHTLEFT |
| 5415 | && !p_ri |
| 5416 | #endif |
| 5417 | ) |
| 5418 | { |
| 5419 | #define INPUT_BUFLEN 100 |
| 5420 | char_u buf[INPUT_BUFLEN + 1]; |
| 5421 | int i; |
| 5422 | colnr_T virtcol = 0; |
| 5423 | |
| 5424 | buf[0] = c; |
| 5425 | i = 1; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5426 | if (textwidth > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5427 | virtcol = get_nolist_virtcol(); |
| 5428 | /* |
| 5429 | * Stop the string when: |
| 5430 | * - no more chars available |
| 5431 | * - finding a special character (command key) |
| 5432 | * - buffer is full |
| 5433 | * - running into the 'textwidth' boundary |
| 5434 | * - need to check for abbreviation: A non-word char after a word-char |
| 5435 | */ |
| 5436 | while ( (c = vpeekc()) != NUL |
| 5437 | && !ISSPECIAL(c) |
| 5438 | #ifdef FEAT_MBYTE |
| 5439 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 5440 | #endif |
| 5441 | && i < INPUT_BUFLEN |
| 5442 | && (textwidth == 0 |
| 5443 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 5444 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 5445 | { |
| 5446 | #ifdef FEAT_RIGHTLEFT |
| 5447 | c = vgetc(); |
| 5448 | if (p_hkmap && KeyTyped) |
| 5449 | c = hkmap(c); /* Hebrew mode mapping */ |
| 5450 | # ifdef FEAT_FKMAP |
| 5451 | if (p_fkmap && KeyTyped) |
| 5452 | c = fkmap(c); /* Farsi mode mapping */ |
| 5453 | # endif |
| 5454 | buf[i++] = c; |
| 5455 | #else |
| 5456 | buf[i++] = vgetc(); |
| 5457 | #endif |
| 5458 | } |
| 5459 | |
| 5460 | #ifdef FEAT_DIGRAPHS |
| 5461 | do_digraph(-1); /* clear digraphs */ |
| 5462 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 5463 | #endif |
| 5464 | buf[i] = NUL; |
| 5465 | ins_str(buf); |
| 5466 | if (flags & INSCHAR_CTRLV) |
| 5467 | { |
| 5468 | redo_literal(*buf); |
| 5469 | i = 1; |
| 5470 | } |
| 5471 | else |
| 5472 | i = 0; |
| 5473 | if (buf[i] != NUL) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5474 | AppendToRedobuffLit(buf + i, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5475 | } |
| 5476 | else |
| 5477 | { |
| 5478 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5479 | int cc; |
| 5480 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5481 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 5482 | { |
| 5483 | char_u buf[MB_MAXBYTES + 1]; |
| 5484 | |
| 5485 | (*mb_char2bytes)(c, buf); |
| 5486 | buf[cc] = NUL; |
| 5487 | ins_char_bytes(buf, cc); |
| 5488 | AppendCharToRedobuff(c); |
| 5489 | } |
| 5490 | else |
| 5491 | #endif |
| 5492 | { |
| 5493 | ins_char(c); |
| 5494 | if (flags & INSCHAR_CTRLV) |
| 5495 | redo_literal(c); |
| 5496 | else |
| 5497 | AppendCharToRedobuff(c); |
| 5498 | } |
| 5499 | } |
| 5500 | } |
| 5501 | |
| 5502 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5503 | * Format text at the current insert position. |
| 5504 | */ |
| 5505 | static void |
| 5506 | internal_format(textwidth, second_indent, flags, format_only) |
| 5507 | int textwidth; |
| 5508 | int second_indent; |
| 5509 | int flags; |
| 5510 | int format_only; |
| 5511 | { |
| 5512 | int cc; |
| 5513 | int save_char = NUL; |
| 5514 | int haveto_redraw = FALSE; |
| 5515 | int fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 5516 | #ifdef FEAT_MBYTE |
| 5517 | int fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 5518 | #endif |
| 5519 | int fo_white_par = has_format_option(FO_WHITE_PAR); |
| 5520 | int first_line = TRUE; |
| 5521 | #ifdef FEAT_COMMENTS |
| 5522 | colnr_T leader_len; |
| 5523 | int no_leader = FALSE; |
| 5524 | int do_comments = (flags & INSCHAR_DO_COM); |
| 5525 | #endif |
| 5526 | |
| 5527 | /* |
| 5528 | * When 'ai' is off we don't want a space under the cursor to be |
| 5529 | * deleted. Replace it with an 'x' temporarily. |
| 5530 | */ |
| 5531 | if (!curbuf->b_p_ai) |
| 5532 | { |
| 5533 | cc = gchar_cursor(); |
| 5534 | if (vim_iswhite(cc)) |
| 5535 | { |
| 5536 | save_char = cc; |
| 5537 | pchar_cursor('x'); |
| 5538 | } |
| 5539 | } |
| 5540 | |
| 5541 | /* |
| 5542 | * Repeat breaking lines, until the current line is not too long. |
| 5543 | */ |
| 5544 | while (!got_int) |
| 5545 | { |
| 5546 | int startcol; /* Cursor column at entry */ |
| 5547 | int wantcol; /* column at textwidth border */ |
| 5548 | int foundcol; /* column for start of spaces */ |
| 5549 | int end_foundcol = 0; /* column for start of word */ |
| 5550 | colnr_T len; |
| 5551 | colnr_T virtcol; |
| 5552 | #ifdef FEAT_VREPLACE |
| 5553 | int orig_col = 0; |
| 5554 | char_u *saved_text = NULL; |
| 5555 | #endif |
| 5556 | colnr_T col; |
| 5557 | |
| 5558 | virtcol = get_nolist_virtcol(); |
| 5559 | if (virtcol < (colnr_T)textwidth) |
| 5560 | break; |
| 5561 | |
| 5562 | #ifdef FEAT_COMMENTS |
| 5563 | if (no_leader) |
| 5564 | do_comments = FALSE; |
| 5565 | else if (!(flags & INSCHAR_FORMAT) |
| 5566 | && has_format_option(FO_WRAP_COMS)) |
| 5567 | do_comments = TRUE; |
| 5568 | |
| 5569 | /* Don't break until after the comment leader */ |
| 5570 | if (do_comments) |
| 5571 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE); |
| 5572 | else |
| 5573 | leader_len = 0; |
| 5574 | |
| 5575 | /* If the line doesn't start with a comment leader, then don't |
| 5576 | * start one in a following broken line. Avoids that a %word |
| 5577 | * moved to the start of the next line causes all following lines |
| 5578 | * to start with %. */ |
| 5579 | if (leader_len == 0) |
| 5580 | no_leader = TRUE; |
| 5581 | #endif |
| 5582 | if (!(flags & INSCHAR_FORMAT) |
| 5583 | #ifdef FEAT_COMMENTS |
| 5584 | && leader_len == 0 |
| 5585 | #endif |
| 5586 | && !has_format_option(FO_WRAP)) |
| 5587 | |
| 5588 | { |
| 5589 | textwidth = 0; |
| 5590 | break; |
| 5591 | } |
| 5592 | if ((startcol = curwin->w_cursor.col) == 0) |
| 5593 | break; |
| 5594 | |
| 5595 | /* find column of textwidth border */ |
| 5596 | coladvance((colnr_T)textwidth); |
| 5597 | wantcol = curwin->w_cursor.col; |
| 5598 | |
| 5599 | curwin->w_cursor.col = startcol - 1; |
| 5600 | #ifdef FEAT_MBYTE |
| 5601 | /* Correct cursor for multi-byte character. */ |
| 5602 | if (has_mbyte) |
| 5603 | mb_adjust_cursor(); |
| 5604 | #endif |
| 5605 | foundcol = 0; |
| 5606 | |
| 5607 | /* |
| 5608 | * Find position to break at. |
| 5609 | * Stop at first entered white when 'formatoptions' has 'v' |
| 5610 | */ |
| 5611 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
| 5612 | || curwin->w_cursor.lnum != Insstart.lnum |
| 5613 | || curwin->w_cursor.col >= Insstart.col) |
| 5614 | { |
| 5615 | cc = gchar_cursor(); |
| 5616 | if (WHITECHAR(cc)) |
| 5617 | { |
| 5618 | /* remember position of blank just before text */ |
| 5619 | end_foundcol = curwin->w_cursor.col; |
| 5620 | |
| 5621 | /* find start of sequence of blanks */ |
| 5622 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 5623 | { |
| 5624 | dec_cursor(); |
| 5625 | cc = gchar_cursor(); |
| 5626 | } |
| 5627 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 5628 | break; /* only spaces in front of text */ |
| 5629 | #ifdef FEAT_COMMENTS |
| 5630 | /* Don't break until after the comment leader */ |
| 5631 | if (curwin->w_cursor.col < leader_len) |
| 5632 | break; |
| 5633 | #endif |
| 5634 | if (has_format_option(FO_ONE_LETTER)) |
| 5635 | { |
| 5636 | /* do not break after one-letter words */ |
| 5637 | if (curwin->w_cursor.col == 0) |
| 5638 | break; /* one-letter word at begin */ |
| 5639 | |
| 5640 | col = curwin->w_cursor.col; |
| 5641 | dec_cursor(); |
| 5642 | cc = gchar_cursor(); |
| 5643 | |
| 5644 | if (WHITECHAR(cc)) |
| 5645 | continue; /* one-letter, continue */ |
| 5646 | curwin->w_cursor.col = col; |
| 5647 | } |
| 5648 | #ifdef FEAT_MBYTE |
| 5649 | if (has_mbyte) |
| 5650 | foundcol = curwin->w_cursor.col |
| 5651 | + (*mb_ptr2len)(ml_get_cursor()); |
| 5652 | else |
| 5653 | #endif |
| 5654 | foundcol = curwin->w_cursor.col + 1; |
| 5655 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 5656 | break; |
| 5657 | } |
| 5658 | #ifdef FEAT_MBYTE |
| 5659 | else if (cc >= 0x100 && fo_multibyte |
| 5660 | && curwin->w_cursor.col <= (colnr_T)wantcol) |
| 5661 | { |
| 5662 | /* Break after or before a multi-byte character. */ |
| 5663 | foundcol = curwin->w_cursor.col; |
| 5664 | if (curwin->w_cursor.col < (colnr_T)wantcol) |
| 5665 | foundcol += (*mb_char2len)(cc); |
| 5666 | end_foundcol = foundcol; |
| 5667 | break; |
| 5668 | } |
| 5669 | #endif |
| 5670 | if (curwin->w_cursor.col == 0) |
| 5671 | break; |
| 5672 | dec_cursor(); |
| 5673 | } |
| 5674 | |
| 5675 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 5676 | { |
| 5677 | curwin->w_cursor.col = startcol; |
| 5678 | break; |
| 5679 | } |
| 5680 | |
| 5681 | /* Going to break the line, remove any "$" now. */ |
| 5682 | undisplay_dollar(); |
| 5683 | |
| 5684 | /* |
| 5685 | * Offset between cursor position and line break is used by replace |
| 5686 | * stack functions. VREPLACE does not use this, and backspaces |
| 5687 | * over the text instead. |
| 5688 | */ |
| 5689 | #ifdef FEAT_VREPLACE |
| 5690 | if (State & VREPLACE_FLAG) |
| 5691 | orig_col = startcol; /* Will start backspacing from here */ |
| 5692 | else |
| 5693 | #endif |
| 5694 | replace_offset = startcol - end_foundcol - 1; |
| 5695 | |
| 5696 | /* |
| 5697 | * adjust startcol for spaces that will be deleted and |
| 5698 | * characters that will remain on top line |
| 5699 | */ |
| 5700 | curwin->w_cursor.col = foundcol; |
| 5701 | while (cc = gchar_cursor(), WHITECHAR(cc)) |
| 5702 | inc_cursor(); |
| 5703 | startcol -= curwin->w_cursor.col; |
| 5704 | if (startcol < 0) |
| 5705 | startcol = 0; |
| 5706 | |
| 5707 | #ifdef FEAT_VREPLACE |
| 5708 | if (State & VREPLACE_FLAG) |
| 5709 | { |
| 5710 | /* |
| 5711 | * In VREPLACE mode, we will backspace over the text to be |
| 5712 | * wrapped, so save a copy now to put on the next line. |
| 5713 | */ |
| 5714 | saved_text = vim_strsave(ml_get_cursor()); |
| 5715 | curwin->w_cursor.col = orig_col; |
| 5716 | if (saved_text == NULL) |
| 5717 | break; /* Can't do it, out of memory */ |
| 5718 | saved_text[startcol] = NUL; |
| 5719 | |
| 5720 | /* Backspace over characters that will move to the next line */ |
| 5721 | if (!fo_white_par) |
| 5722 | backspace_until_column(foundcol); |
| 5723 | } |
| 5724 | else |
| 5725 | #endif |
| 5726 | { |
| 5727 | /* put cursor after pos. to break line */ |
| 5728 | if (!fo_white_par) |
| 5729 | curwin->w_cursor.col = foundcol; |
| 5730 | } |
| 5731 | |
| 5732 | /* |
| 5733 | * Split the line just before the margin. |
| 5734 | * Only insert/delete lines, but don't really redraw the window. |
| 5735 | */ |
| 5736 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 5737 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 5738 | #ifdef FEAT_COMMENTS |
| 5739 | + (do_comments ? OPENLINE_DO_COM : 0) |
| 5740 | #endif |
| 5741 | , old_indent); |
| 5742 | old_indent = 0; |
| 5743 | |
| 5744 | replace_offset = 0; |
| 5745 | if (first_line) |
| 5746 | { |
| 5747 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
| 5748 | second_indent = get_number_indent(curwin->w_cursor.lnum -1); |
| 5749 | if (second_indent >= 0) |
| 5750 | { |
| 5751 | #ifdef FEAT_VREPLACE |
| 5752 | if (State & VREPLACE_FLAG) |
| 5753 | change_indent(INDENT_SET, second_indent, FALSE, NUL); |
| 5754 | else |
| 5755 | #endif |
| 5756 | (void)set_indent(second_indent, SIN_CHANGED); |
| 5757 | } |
| 5758 | first_line = FALSE; |
| 5759 | } |
| 5760 | |
| 5761 | #ifdef FEAT_VREPLACE |
| 5762 | if (State & VREPLACE_FLAG) |
| 5763 | { |
| 5764 | /* |
| 5765 | * In VREPLACE mode we have backspaced over the text to be |
| 5766 | * moved, now we re-insert it into the new line. |
| 5767 | */ |
| 5768 | ins_bytes(saved_text); |
| 5769 | vim_free(saved_text); |
| 5770 | } |
| 5771 | else |
| 5772 | #endif |
| 5773 | { |
| 5774 | /* |
| 5775 | * Check if cursor is not past the NUL off the line, cindent |
| 5776 | * may have added or removed indent. |
| 5777 | */ |
| 5778 | curwin->w_cursor.col += startcol; |
| 5779 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 5780 | if (curwin->w_cursor.col > len) |
| 5781 | curwin->w_cursor.col = len; |
| 5782 | } |
| 5783 | |
| 5784 | haveto_redraw = TRUE; |
| 5785 | #ifdef FEAT_CINDENT |
| 5786 | can_cindent = TRUE; |
| 5787 | #endif |
| 5788 | /* moved the cursor, don't autoindent or cindent now */ |
| 5789 | did_ai = FALSE; |
| 5790 | #ifdef FEAT_SMARTINDENT |
| 5791 | did_si = FALSE; |
| 5792 | can_si = FALSE; |
| 5793 | can_si_back = FALSE; |
| 5794 | #endif |
| 5795 | line_breakcheck(); |
| 5796 | } |
| 5797 | |
| 5798 | if (save_char != NUL) /* put back space after cursor */ |
| 5799 | pchar_cursor(save_char); |
| 5800 | |
| 5801 | if (!format_only && haveto_redraw) |
| 5802 | { |
| 5803 | update_topline(); |
| 5804 | redraw_curbuf_later(VALID); |
| 5805 | } |
| 5806 | } |
| 5807 | |
| 5808 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5809 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 5810 | * 'a' flag format from the current line until the end of the paragraph. |
| 5811 | * Keep the cursor at the same position relative to the text. |
| 5812 | * The caller must have saved the cursor line for undo, following ones will be |
| 5813 | * saved here. |
| 5814 | */ |
| 5815 | void |
| 5816 | auto_format(trailblank, prev_line) |
| 5817 | int trailblank; /* when TRUE also format with trailing blank */ |
| 5818 | int prev_line; /* may start in previous line */ |
| 5819 | { |
| 5820 | pos_T pos; |
| 5821 | colnr_T len; |
| 5822 | char_u *old; |
| 5823 | char_u *new, *pnew; |
| 5824 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5825 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5826 | |
| 5827 | if (!has_format_option(FO_AUTO)) |
| 5828 | return; |
| 5829 | |
| 5830 | pos = curwin->w_cursor; |
| 5831 | old = ml_get_curline(); |
| 5832 | |
| 5833 | /* may remove added space */ |
| 5834 | check_auto_format(FALSE); |
| 5835 | |
| 5836 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 5837 | * user might insert normal text next. Also skip formatting when "1" is |
| 5838 | * in 'formatoptions' and there is a single character before the cursor. |
| 5839 | * Otherwise the line would be broken and when typing another non-white |
| 5840 | * next they are not joined back together. */ |
| 5841 | wasatend = (pos.col == STRLEN(old)); |
| 5842 | if (*old != NUL && !trailblank && wasatend) |
| 5843 | { |
| 5844 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5845 | cc = gchar_cursor(); |
| 5846 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 5847 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5848 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5849 | cc = gchar_cursor(); |
| 5850 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5851 | { |
| 5852 | curwin->w_cursor = pos; |
| 5853 | return; |
| 5854 | } |
| 5855 | curwin->w_cursor = pos; |
| 5856 | } |
| 5857 | |
| 5858 | #ifdef FEAT_COMMENTS |
| 5859 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 5860 | * comments. */ |
| 5861 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
| 5862 | && get_leader_len(old, NULL, FALSE) == 0) |
| 5863 | return; |
| 5864 | #endif |
| 5865 | |
| 5866 | /* |
| 5867 | * May start formatting in a previous line, so that after "x" a word is |
| 5868 | * moved to the previous line if it fits there now. Only when this is not |
| 5869 | * the start of a paragraph. |
| 5870 | */ |
| 5871 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 5872 | { |
| 5873 | --curwin->w_cursor.lnum; |
| 5874 | if (u_save_cursor() == FAIL) |
| 5875 | return; |
| 5876 | } |
| 5877 | |
| 5878 | /* |
| 5879 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 5880 | * be adjusted for the text formatting. |
| 5881 | */ |
| 5882 | saved_cursor = pos; |
| 5883 | format_lines((linenr_T)-1); |
| 5884 | curwin->w_cursor = saved_cursor; |
| 5885 | saved_cursor.lnum = 0; |
| 5886 | |
| 5887 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 5888 | { |
| 5889 | /* "cannot happen" */ |
| 5890 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 5891 | coladvance((colnr_T)MAXCOL); |
| 5892 | } |
| 5893 | else |
| 5894 | check_cursor_col(); |
| 5895 | |
| 5896 | /* Insert mode: If the cursor is now after the end of the line while it |
| 5897 | * previously wasn't, the line was broken. Because of the rule above we |
| 5898 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 5899 | * formatted. */ |
| 5900 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 5901 | { |
| 5902 | new = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5903 | len = (colnr_T)STRLEN(new); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | if (curwin->w_cursor.col == len) |
| 5905 | { |
| 5906 | pnew = vim_strnsave(new, len + 2); |
| 5907 | pnew[len] = ' '; |
| 5908 | pnew[len + 1] = NUL; |
| 5909 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 5910 | /* remove the space later */ |
| 5911 | did_add_space = TRUE; |
| 5912 | } |
| 5913 | else |
| 5914 | /* may remove added space */ |
| 5915 | check_auto_format(FALSE); |
| 5916 | } |
| 5917 | |
| 5918 | check_cursor(); |
| 5919 | } |
| 5920 | |
| 5921 | /* |
| 5922 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 5923 | * delete it now. The space must be under the cursor, just after the insert |
| 5924 | * position. |
| 5925 | */ |
| 5926 | static void |
| 5927 | check_auto_format(end_insert) |
| 5928 | int end_insert; /* TRUE when ending Insert mode */ |
| 5929 | { |
| 5930 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5931 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5932 | |
| 5933 | if (did_add_space) |
| 5934 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 5935 | cc = gchar_cursor(); |
| 5936 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5937 | /* Somehow the space was removed already. */ |
| 5938 | did_add_space = FALSE; |
| 5939 | else |
| 5940 | { |
| 5941 | if (!end_insert) |
| 5942 | { |
| 5943 | inc_cursor(); |
| 5944 | c = gchar_cursor(); |
| 5945 | dec_cursor(); |
| 5946 | } |
| 5947 | if (c != NUL) |
| 5948 | { |
| 5949 | /* The space is no longer at the end of the line, delete it. */ |
| 5950 | del_char(FALSE); |
| 5951 | did_add_space = FALSE; |
| 5952 | } |
| 5953 | } |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | /* |
| 5958 | * Find out textwidth to be used for formatting: |
| 5959 | * if 'textwidth' option is set, use it |
| 5960 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 5961 | * if invalid value, use 0. |
| 5962 | * Set default to window width (maximum 79) for "gq" operator. |
| 5963 | */ |
| 5964 | int |
| 5965 | comp_textwidth(ff) |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 5966 | int ff; /* force formatting (for "gq" command) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5967 | { |
| 5968 | int textwidth; |
| 5969 | |
| 5970 | textwidth = curbuf->b_p_tw; |
| 5971 | if (textwidth == 0 && curbuf->b_p_wm) |
| 5972 | { |
| 5973 | /* The width is the window width minus 'wrapmargin' minus all the |
| 5974 | * things that add to the margin. */ |
| 5975 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 5976 | #ifdef FEAT_CMDWIN |
| 5977 | if (cmdwin_type != 0) |
| 5978 | textwidth -= 1; |
| 5979 | #endif |
| 5980 | #ifdef FEAT_FOLDING |
| 5981 | textwidth -= curwin->w_p_fdc; |
| 5982 | #endif |
| 5983 | #ifdef FEAT_SIGNS |
| 5984 | if (curwin->w_buffer->b_signlist != NULL |
| 5985 | # ifdef FEAT_NETBEANS_INTG |
| 5986 | || usingNetbeans |
| 5987 | # endif |
| 5988 | ) |
| 5989 | textwidth -= 1; |
| 5990 | #endif |
| 5991 | if (curwin->w_p_nu) |
| 5992 | textwidth -= 8; |
| 5993 | } |
| 5994 | if (textwidth < 0) |
| 5995 | textwidth = 0; |
| 5996 | if (ff && textwidth == 0) |
| 5997 | { |
| 5998 | textwidth = W_WIDTH(curwin) - 1; |
| 5999 | if (textwidth > 79) |
| 6000 | textwidth = 79; |
| 6001 | } |
| 6002 | return textwidth; |
| 6003 | } |
| 6004 | |
| 6005 | /* |
| 6006 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 6007 | */ |
| 6008 | static void |
| 6009 | redo_literal(c) |
| 6010 | int c; |
| 6011 | { |
| 6012 | char_u buf[10]; |
| 6013 | |
| 6014 | /* Only digits need special treatment. Translate them into a string of |
| 6015 | * three digits. */ |
| 6016 | if (VIM_ISDIGIT(c)) |
| 6017 | { |
| 6018 | sprintf((char *)buf, "%03d", c); |
| 6019 | AppendToRedobuff(buf); |
| 6020 | } |
| 6021 | else |
| 6022 | AppendCharToRedobuff(c); |
| 6023 | } |
| 6024 | |
| 6025 | /* |
| 6026 | * 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] | 6027 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6028 | */ |
| 6029 | static void |
| 6030 | start_arrow(end_insert_pos) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6031 | pos_T *end_insert_pos; /* can be NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6032 | { |
| 6033 | if (!arrow_used) /* something has been inserted */ |
| 6034 | { |
| 6035 | AppendToRedobuff(ESC_STR); |
| 6036 | stop_insert(end_insert_pos, FALSE); |
| 6037 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 6038 | } |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6039 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6040 | check_spell_redraw(); |
| 6041 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6042 | } |
| 6043 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6044 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6045 | /* |
| 6046 | * If we skipped highlighting word at cursor, do it now. |
| 6047 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 6048 | */ |
| 6049 | static void |
| 6050 | check_spell_redraw() |
| 6051 | { |
| 6052 | if (spell_redraw_lnum != 0) |
| 6053 | { |
| 6054 | linenr_T lnum = spell_redraw_lnum; |
| 6055 | |
| 6056 | spell_redraw_lnum = 0; |
| 6057 | redrawWinline(lnum, FALSE); |
| 6058 | } |
| 6059 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6060 | |
| 6061 | /* |
| 6062 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6063 | * spelled word, if there is one. |
| 6064 | */ |
| 6065 | static void |
| 6066 | spell_back_to_badword() |
| 6067 | { |
| 6068 | pos_T tpos = curwin->w_cursor; |
| 6069 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 6070 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6071 | if (curwin->w_cursor.col != tpos.col) |
| 6072 | start_arrow(&tpos); |
| 6073 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6074 | #endif |
| 6075 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | /* |
| 6077 | * stop_arrow() is called before a change is made in insert mode. |
| 6078 | * If an arrow key has been used, start a new insertion. |
| 6079 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 6080 | */ |
| 6081 | int |
| 6082 | stop_arrow() |
| 6083 | { |
| 6084 | if (arrow_used) |
| 6085 | { |
| 6086 | if (u_save_cursor() == OK) |
| 6087 | { |
| 6088 | arrow_used = FALSE; |
| 6089 | ins_need_undo = FALSE; |
| 6090 | } |
| 6091 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
| 6092 | Insstart_textlen = linetabsize(ml_get_curline()); |
| 6093 | ai_col = 0; |
| 6094 | #ifdef FEAT_VREPLACE |
| 6095 | if (State & VREPLACE_FLAG) |
| 6096 | { |
| 6097 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 6098 | vr_lines_changed = 1; |
| 6099 | } |
| 6100 | #endif |
| 6101 | ResetRedobuff(); |
| 6102 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 6103 | new_insert_skip = 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6104 | } |
| 6105 | else if (ins_need_undo) |
| 6106 | { |
| 6107 | if (u_save_cursor() == OK) |
| 6108 | ins_need_undo = FALSE; |
| 6109 | } |
| 6110 | |
| 6111 | #ifdef FEAT_FOLDING |
| 6112 | /* Always open fold at the cursor line when inserting something. */ |
| 6113 | foldOpenCursor(); |
| 6114 | #endif |
| 6115 | |
| 6116 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 6117 | } |
| 6118 | |
| 6119 | /* |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6120 | * Do a few things to stop inserting. |
| 6121 | * "end_insert_pos" is where insert ended. It is NULL when we already jumped |
| 6122 | * to another window/buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6123 | */ |
| 6124 | static void |
| 6125 | stop_insert(end_insert_pos, esc) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6126 | pos_T *end_insert_pos; |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6127 | int esc; /* called by ins_esc() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6128 | { |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6129 | int cc; |
| 6130 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6131 | |
| 6132 | stop_redo_ins(); |
| 6133 | replace_flush(); /* abandon replace stack */ |
| 6134 | |
| 6135 | /* |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6136 | * Save the inserted text for later redo with ^@ and CTRL-A. |
| 6137 | * Don't do it when "restart_edit" was set and nothing was inserted, |
| 6138 | * otherwise CTRL-O w and then <Left> will clear "last_insert". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6139 | */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6140 | ptr = get_inserted(); |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 6141 | if (did_restart_edit == 0 || (ptr != NULL |
| 6142 | && (int)STRLEN(ptr) > new_insert_skip)) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6143 | { |
| 6144 | vim_free(last_insert); |
| 6145 | last_insert = ptr; |
| 6146 | last_insert_skip = new_insert_skip; |
| 6147 | } |
| 6148 | else |
| 6149 | vim_free(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6150 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6151 | if (!arrow_used && end_insert_pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6152 | { |
| 6153 | /* Auto-format now. It may seem strange to do this when stopping an |
| 6154 | * insertion (or moving the cursor), but it's required when appending |
| 6155 | * a line and having it end in a space. But only do it when something |
| 6156 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6157 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6158 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6159 | pos_T tpos = curwin->w_cursor; |
| 6160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6161 | /* When the cursor is at the end of the line after a space the |
| 6162 | * formatting will move it to the following word. Avoid that by |
| 6163 | * moving the cursor onto the space. */ |
| 6164 | cc = 'x'; |
| 6165 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 6166 | { |
| 6167 | dec_cursor(); |
| 6168 | cc = gchar_cursor(); |
| 6169 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6170 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6171 | } |
| 6172 | |
| 6173 | auto_format(TRUE, FALSE); |
| 6174 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6175 | if (vim_iswhite(cc)) |
| 6176 | { |
| 6177 | if (gchar_cursor() != NUL) |
| 6178 | inc_cursor(); |
| 6179 | #ifdef FEAT_VIRTUALEDIT |
| 6180 | /* If the cursor is still at the same character, also keep |
| 6181 | * the "coladd". */ |
| 6182 | if (gchar_cursor() == NUL |
| 6183 | && curwin->w_cursor.lnum == tpos.lnum |
| 6184 | && curwin->w_cursor.col == tpos.col) |
| 6185 | curwin->w_cursor.coladd = tpos.coladd; |
| 6186 | #endif |
| 6187 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
| 6190 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 6191 | check_auto_format(TRUE); |
| 6192 | |
| 6193 | /* 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] | 6194 | * of the line, and put the cursor back. |
| 6195 | * Do this when ESC was used or moving the cursor up/down. */ |
| 6196 | if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6197 | && curwin->w_cursor.lnum != end_insert_pos->lnum))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6198 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6199 | pos_T tpos = curwin->w_cursor; |
| 6200 | |
| 6201 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6202 | for (;;) |
| 6203 | { |
| 6204 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 6205 | --curwin->w_cursor.col; |
| 6206 | cc = gchar_cursor(); |
| 6207 | if (!vim_iswhite(cc)) |
| 6208 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6209 | (void)del_char(TRUE); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6210 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6211 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 6212 | curwin->w_cursor = tpos; |
| 6213 | else if (cc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6214 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 6215 | |
| 6216 | #ifdef FEAT_VISUAL |
| 6217 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 6218 | * deleted characters. */ |
| 6219 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 6220 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6221 | cc = (int)STRLEN(ml_get_curline()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6222 | if (VIsual.col > (colnr_T)cc) |
| 6223 | { |
| 6224 | VIsual.col = cc; |
| 6225 | # ifdef FEAT_VIRTUALEDIT |
| 6226 | VIsual.coladd = 0; |
| 6227 | # endif |
| 6228 | } |
| 6229 | } |
| 6230 | #endif |
| 6231 | } |
| 6232 | } |
| 6233 | did_ai = FALSE; |
| 6234 | #ifdef FEAT_SMARTINDENT |
| 6235 | did_si = FALSE; |
| 6236 | can_si = FALSE; |
| 6237 | can_si_back = FALSE; |
| 6238 | #endif |
| 6239 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6240 | /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are |
| 6241 | * now in a different buffer. */ |
| 6242 | if (end_insert_pos != NULL) |
| 6243 | { |
| 6244 | curbuf->b_op_start = Insstart; |
| 6245 | curbuf->b_op_end = *end_insert_pos; |
| 6246 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | /* |
| 6250 | * Set the last inserted text to a single character. |
| 6251 | * Used for the replace command. |
| 6252 | */ |
| 6253 | void |
| 6254 | set_last_insert(c) |
| 6255 | int c; |
| 6256 | { |
| 6257 | char_u *s; |
| 6258 | |
| 6259 | vim_free(last_insert); |
| 6260 | #ifdef FEAT_MBYTE |
| 6261 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
| 6262 | #else |
| 6263 | last_insert = alloc(6); |
| 6264 | #endif |
| 6265 | if (last_insert != NULL) |
| 6266 | { |
| 6267 | s = last_insert; |
| 6268 | /* Use the CTRL-V only when entering a special char */ |
| 6269 | if (c < ' ' || c == DEL) |
| 6270 | *s++ = Ctrl_V; |
| 6271 | s = add_char2buf(c, s); |
| 6272 | *s++ = ESC; |
| 6273 | *s++ = NUL; |
| 6274 | last_insert_skip = 0; |
| 6275 | } |
| 6276 | } |
| 6277 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6278 | #if defined(EXITFREE) || defined(PROTO) |
| 6279 | void |
| 6280 | free_last_insert() |
| 6281 | { |
| 6282 | vim_free(last_insert); |
| 6283 | last_insert = NULL; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6284 | vim_free(compl_orig_text); |
| 6285 | compl_orig_text = NULL; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6286 | } |
| 6287 | #endif |
| 6288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6289 | /* |
| 6290 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 6291 | * and CSI. Handle multi-byte characters. |
| 6292 | * Returns a pointer to after the added bytes. |
| 6293 | */ |
| 6294 | char_u * |
| 6295 | add_char2buf(c, s) |
| 6296 | int c; |
| 6297 | char_u *s; |
| 6298 | { |
| 6299 | #ifdef FEAT_MBYTE |
| 6300 | char_u temp[MB_MAXBYTES]; |
| 6301 | int i; |
| 6302 | int len; |
| 6303 | |
| 6304 | len = (*mb_char2bytes)(c, temp); |
| 6305 | for (i = 0; i < len; ++i) |
| 6306 | { |
| 6307 | c = temp[i]; |
| 6308 | #endif |
| 6309 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 6310 | if (c == K_SPECIAL) |
| 6311 | { |
| 6312 | *s++ = K_SPECIAL; |
| 6313 | *s++ = KS_SPECIAL; |
| 6314 | *s++ = KE_FILLER; |
| 6315 | } |
| 6316 | #ifdef FEAT_GUI |
| 6317 | else if (c == CSI) |
| 6318 | { |
| 6319 | *s++ = CSI; |
| 6320 | *s++ = KS_EXTRA; |
| 6321 | *s++ = (int)KE_CSI; |
| 6322 | } |
| 6323 | #endif |
| 6324 | else |
| 6325 | *s++ = c; |
| 6326 | #ifdef FEAT_MBYTE |
| 6327 | } |
| 6328 | #endif |
| 6329 | return s; |
| 6330 | } |
| 6331 | |
| 6332 | /* |
| 6333 | * move cursor to start of line |
| 6334 | * if flags & BL_WHITE move to first non-white |
| 6335 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 6336 | * otherwise keep "curswant" column |
| 6337 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 6338 | */ |
| 6339 | void |
| 6340 | beginline(flags) |
| 6341 | int flags; |
| 6342 | { |
| 6343 | if ((flags & BL_SOL) && !p_sol) |
| 6344 | coladvance(curwin->w_curswant); |
| 6345 | else |
| 6346 | { |
| 6347 | curwin->w_cursor.col = 0; |
| 6348 | #ifdef FEAT_VIRTUALEDIT |
| 6349 | curwin->w_cursor.coladd = 0; |
| 6350 | #endif |
| 6351 | |
| 6352 | if (flags & (BL_WHITE | BL_SOL)) |
| 6353 | { |
| 6354 | char_u *ptr; |
| 6355 | |
| 6356 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 6357 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 6358 | ++curwin->w_cursor.col; |
| 6359 | } |
| 6360 | curwin->w_set_curswant = TRUE; |
| 6361 | } |
| 6362 | } |
| 6363 | |
| 6364 | /* |
| 6365 | * oneright oneleft cursor_down cursor_up |
| 6366 | * |
| 6367 | * Move one char {right,left,down,up}. |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6368 | * Doesn't move onto the NUL past the end of the line, unless it is allowed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6369 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 6370 | */ |
| 6371 | |
| 6372 | int |
| 6373 | oneright() |
| 6374 | { |
| 6375 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6376 | int l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6377 | |
| 6378 | #ifdef FEAT_VIRTUALEDIT |
| 6379 | if (virtual_active()) |
| 6380 | { |
| 6381 | pos_T prevpos = curwin->w_cursor; |
| 6382 | |
| 6383 | /* Adjust for multi-wide char (excluding TAB) */ |
| 6384 | ptr = ml_get_cursor(); |
| 6385 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6386 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6387 | (*mb_ptr2char)(ptr) |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6388 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6389 | *ptr |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6390 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6391 | )) |
| 6392 | ? ptr2cells(ptr) : 1)); |
| 6393 | curwin->w_set_curswant = TRUE; |
| 6394 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 6395 | return (prevpos.col != curwin->w_cursor.col |
| 6396 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 6397 | } |
| 6398 | #endif |
| 6399 | |
| 6400 | ptr = ml_get_cursor(); |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6401 | if (*ptr == NUL) |
| 6402 | return FAIL; /* already at the very end */ |
| 6403 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6405 | if (has_mbyte) |
| 6406 | l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6407 | else |
| 6408 | #endif |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6409 | l = 1; |
| 6410 | |
| 6411 | /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' |
| 6412 | * contains "onemore". */ |
| 6413 | if (ptr[l] == NUL |
| 6414 | #ifdef FEAT_VIRTUALEDIT |
| 6415 | && (ve_flags & VE_ONEMORE) == 0 |
| 6416 | #endif |
| 6417 | ) |
| 6418 | return FAIL; |
| 6419 | curwin->w_cursor.col += l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6420 | |
| 6421 | curwin->w_set_curswant = TRUE; |
| 6422 | return OK; |
| 6423 | } |
| 6424 | |
| 6425 | int |
| 6426 | oneleft() |
| 6427 | { |
| 6428 | #ifdef FEAT_VIRTUALEDIT |
| 6429 | if (virtual_active()) |
| 6430 | { |
| 6431 | int width; |
| 6432 | int v = getviscol(); |
| 6433 | |
| 6434 | if (v == 0) |
| 6435 | return FAIL; |
| 6436 | |
| 6437 | # ifdef FEAT_LINEBREAK |
| 6438 | /* We might get stuck on 'showbreak', skip over it. */ |
| 6439 | width = 1; |
| 6440 | for (;;) |
| 6441 | { |
| 6442 | coladvance(v - width); |
| 6443 | /* getviscol() is slow, skip it when 'showbreak' is empty and |
| 6444 | * there are no multi-byte characters */ |
| 6445 | if ((*p_sbr == NUL |
| 6446 | # ifdef FEAT_MBYTE |
| 6447 | && !has_mbyte |
| 6448 | # endif |
| 6449 | ) || getviscol() < v) |
| 6450 | break; |
| 6451 | ++width; |
| 6452 | } |
| 6453 | # else |
| 6454 | coladvance(v - 1); |
| 6455 | # endif |
| 6456 | |
| 6457 | if (curwin->w_cursor.coladd == 1) |
| 6458 | { |
| 6459 | char_u *ptr; |
| 6460 | |
| 6461 | /* Adjust for multi-wide char (not a TAB) */ |
| 6462 | ptr = ml_get_cursor(); |
| 6463 | if (*ptr != TAB && vim_isprintc( |
| 6464 | # ifdef FEAT_MBYTE |
| 6465 | (*mb_ptr2char)(ptr) |
| 6466 | # else |
| 6467 | *ptr |
| 6468 | # endif |
| 6469 | ) && ptr2cells(ptr) > 1) |
| 6470 | curwin->w_cursor.coladd = 0; |
| 6471 | } |
| 6472 | |
| 6473 | curwin->w_set_curswant = TRUE; |
| 6474 | return OK; |
| 6475 | } |
| 6476 | #endif |
| 6477 | |
| 6478 | if (curwin->w_cursor.col == 0) |
| 6479 | return FAIL; |
| 6480 | |
| 6481 | curwin->w_set_curswant = TRUE; |
| 6482 | --curwin->w_cursor.col; |
| 6483 | |
| 6484 | #ifdef FEAT_MBYTE |
| 6485 | /* if the character on the left of the current cursor is a multi-byte |
| 6486 | * character, move to its first byte */ |
| 6487 | if (has_mbyte) |
| 6488 | mb_adjust_cursor(); |
| 6489 | #endif |
| 6490 | return OK; |
| 6491 | } |
| 6492 | |
| 6493 | int |
| 6494 | cursor_up(n, upd_topline) |
| 6495 | long n; |
| 6496 | int upd_topline; /* When TRUE: update topline */ |
| 6497 | { |
| 6498 | linenr_T lnum; |
| 6499 | |
| 6500 | if (n > 0) |
| 6501 | { |
| 6502 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6503 | /* This fails if the cursor is already in the first line or the count |
| 6504 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 6505 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6506 | return FAIL; |
| 6507 | if (n >= lnum) |
| 6508 | lnum = 1; |
| 6509 | else |
| 6510 | #ifdef FEAT_FOLDING |
| 6511 | if (hasAnyFolding(curwin)) |
| 6512 | { |
| 6513 | /* |
| 6514 | * Count each sequence of folded lines as one logical line. |
| 6515 | */ |
| 6516 | /* go to the the start of the current fold */ |
| 6517 | (void)hasFolding(lnum, &lnum, NULL); |
| 6518 | |
| 6519 | while (n--) |
| 6520 | { |
| 6521 | /* move up one line */ |
| 6522 | --lnum; |
| 6523 | if (lnum <= 1) |
| 6524 | break; |
| 6525 | /* If we entered a fold, move to the beginning, unless in |
| 6526 | * Insert mode or when 'foldopen' contains "all": it will open |
| 6527 | * in a moment. */ |
| 6528 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 6529 | (void)hasFolding(lnum, &lnum, NULL); |
| 6530 | } |
| 6531 | if (lnum < 1) |
| 6532 | lnum = 1; |
| 6533 | } |
| 6534 | else |
| 6535 | #endif |
| 6536 | lnum -= n; |
| 6537 | curwin->w_cursor.lnum = lnum; |
| 6538 | } |
| 6539 | |
| 6540 | /* try to advance to the column we want to be at */ |
| 6541 | coladvance(curwin->w_curswant); |
| 6542 | |
| 6543 | if (upd_topline) |
| 6544 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 6545 | |
| 6546 | return OK; |
| 6547 | } |
| 6548 | |
| 6549 | /* |
| 6550 | * Cursor down a number of logical lines. |
| 6551 | */ |
| 6552 | int |
| 6553 | cursor_down(n, upd_topline) |
| 6554 | long n; |
| 6555 | int upd_topline; /* When TRUE: update topline */ |
| 6556 | { |
| 6557 | linenr_T lnum; |
| 6558 | |
| 6559 | if (n > 0) |
| 6560 | { |
| 6561 | lnum = curwin->w_cursor.lnum; |
| 6562 | #ifdef FEAT_FOLDING |
| 6563 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 6564 | (void)hasFolding(lnum, NULL, &lnum); |
| 6565 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 6566 | /* This fails if the cursor is already in the last line or would move |
| 6567 | * beyound the last line and '-' is in 'cpoptions' */ |
| 6568 | if (lnum >= curbuf->b_ml.ml_line_count |
| 6569 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 6570 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | return FAIL; |
| 6572 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 6573 | lnum = curbuf->b_ml.ml_line_count; |
| 6574 | else |
| 6575 | #ifdef FEAT_FOLDING |
| 6576 | if (hasAnyFolding(curwin)) |
| 6577 | { |
| 6578 | linenr_T last; |
| 6579 | |
| 6580 | /* count each sequence of folded lines as one logical line */ |
| 6581 | while (n--) |
| 6582 | { |
| 6583 | if (hasFolding(lnum, NULL, &last)) |
| 6584 | lnum = last + 1; |
| 6585 | else |
| 6586 | ++lnum; |
| 6587 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 6588 | break; |
| 6589 | } |
| 6590 | if (lnum > curbuf->b_ml.ml_line_count) |
| 6591 | lnum = curbuf->b_ml.ml_line_count; |
| 6592 | } |
| 6593 | else |
| 6594 | #endif |
| 6595 | lnum += n; |
| 6596 | curwin->w_cursor.lnum = lnum; |
| 6597 | } |
| 6598 | |
| 6599 | /* try to advance to the column we want to be at */ |
| 6600 | coladvance(curwin->w_curswant); |
| 6601 | |
| 6602 | if (upd_topline) |
| 6603 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 6604 | |
| 6605 | return OK; |
| 6606 | } |
| 6607 | |
| 6608 | /* |
| 6609 | * Stuff the last inserted text in the read buffer. |
| 6610 | * Last_insert actually is a copy of the redo buffer, so we |
| 6611 | * first have to remove the command. |
| 6612 | */ |
| 6613 | int |
| 6614 | stuff_inserted(c, count, no_esc) |
| 6615 | int c; /* Command character to be inserted */ |
| 6616 | long count; /* Repeat this many times */ |
| 6617 | int no_esc; /* Don't add an ESC at the end */ |
| 6618 | { |
| 6619 | char_u *esc_ptr; |
| 6620 | char_u *ptr; |
| 6621 | char_u *last_ptr; |
| 6622 | char_u last = NUL; |
| 6623 | |
| 6624 | ptr = get_last_insert(); |
| 6625 | if (ptr == NULL) |
| 6626 | { |
| 6627 | EMSG(_(e_noinstext)); |
| 6628 | return FAIL; |
| 6629 | } |
| 6630 | |
| 6631 | /* may want to stuff the command character, to start Insert mode */ |
| 6632 | if (c != NUL) |
| 6633 | stuffcharReadbuff(c); |
| 6634 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 6635 | *esc_ptr = NUL; /* remove the ESC */ |
| 6636 | |
| 6637 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 6638 | * comes after it OR if it will inserted more than once and "ptr" |
| 6639 | * starts with ^D. -- Acevedo |
| 6640 | */ |
| 6641 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 6642 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 6643 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 6644 | { |
| 6645 | last = *last_ptr; |
| 6646 | *last_ptr = NUL; |
| 6647 | } |
| 6648 | |
| 6649 | do |
| 6650 | { |
| 6651 | stuffReadbuff(ptr); |
| 6652 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 6653 | if (last) |
| 6654 | stuffReadbuff((char_u *)(last == '0' |
| 6655 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 6656 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 6657 | } |
| 6658 | while (--count > 0); |
| 6659 | |
| 6660 | if (last) |
| 6661 | *last_ptr = last; |
| 6662 | |
| 6663 | if (esc_ptr != NULL) |
| 6664 | *esc_ptr = ESC; /* put the ESC back */ |
| 6665 | |
| 6666 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 6667 | if (!no_esc) |
| 6668 | stuffcharReadbuff(ESC); |
| 6669 | |
| 6670 | return OK; |
| 6671 | } |
| 6672 | |
| 6673 | char_u * |
| 6674 | get_last_insert() |
| 6675 | { |
| 6676 | if (last_insert == NULL) |
| 6677 | return NULL; |
| 6678 | return last_insert + last_insert_skip; |
| 6679 | } |
| 6680 | |
| 6681 | /* |
| 6682 | * Get last inserted string, and remove trailing <Esc>. |
| 6683 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 6684 | */ |
| 6685 | char_u * |
| 6686 | get_last_insert_save() |
| 6687 | { |
| 6688 | char_u *s; |
| 6689 | int len; |
| 6690 | |
| 6691 | if (last_insert == NULL) |
| 6692 | return NULL; |
| 6693 | s = vim_strsave(last_insert + last_insert_skip); |
| 6694 | if (s != NULL) |
| 6695 | { |
| 6696 | len = (int)STRLEN(s); |
| 6697 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 6698 | s[len - 1] = NUL; |
| 6699 | } |
| 6700 | return s; |
| 6701 | } |
| 6702 | |
| 6703 | /* |
| 6704 | * Check the word in front of the cursor for an abbreviation. |
| 6705 | * Called when the non-id character "c" has been entered. |
| 6706 | * When an abbreviation is recognized it is removed from the text and |
| 6707 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 6708 | */ |
| 6709 | static int |
| 6710 | echeck_abbr(c) |
| 6711 | int c; |
| 6712 | { |
| 6713 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 6714 | * after moving around with cursor keys. */ |
| 6715 | if (p_paste || no_abbr || arrow_used) |
| 6716 | return FALSE; |
| 6717 | |
| 6718 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 6719 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 6720 | } |
| 6721 | |
| 6722 | /* |
| 6723 | * replace-stack functions |
| 6724 | * |
| 6725 | * When replacing characters, the replaced characters are remembered for each |
| 6726 | * new character. This is used to re-insert the old text when backspacing. |
| 6727 | * |
| 6728 | * There is a NUL headed list of characters for each character that is |
| 6729 | * currently in the file after the insertion point. When BS is used, one NUL |
| 6730 | * headed list is put back for the deleted character. |
| 6731 | * |
| 6732 | * For a newline, there are two NUL headed lists. One contains the characters |
| 6733 | * that the NL replaced. The extra one stores the characters after the cursor |
| 6734 | * that were deleted (always white space). |
| 6735 | * |
| 6736 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 6737 | * character at the end of the stack. If replace_offset is not 0, that many |
| 6738 | * characters will be left on the stack above the newly inserted character. |
| 6739 | */ |
| 6740 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 6741 | static char_u *replace_stack = NULL; |
| 6742 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 6743 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6744 | |
| 6745 | void |
| 6746 | replace_push(c) |
| 6747 | int c; /* character that is replaced (NUL is none) */ |
| 6748 | { |
| 6749 | char_u *p; |
| 6750 | |
| 6751 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 6752 | return; |
| 6753 | if (replace_stack_len <= replace_stack_nr) |
| 6754 | { |
| 6755 | replace_stack_len += 50; |
| 6756 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 6757 | if (p == NULL) /* out of memory */ |
| 6758 | { |
| 6759 | replace_stack_len -= 50; |
| 6760 | return; |
| 6761 | } |
| 6762 | if (replace_stack != NULL) |
| 6763 | { |
| 6764 | mch_memmove(p, replace_stack, |
| 6765 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 6766 | vim_free(replace_stack); |
| 6767 | } |
| 6768 | replace_stack = p; |
| 6769 | } |
| 6770 | p = replace_stack + replace_stack_nr - replace_offset; |
| 6771 | if (replace_offset) |
| 6772 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 6773 | *p = c; |
| 6774 | ++replace_stack_nr; |
| 6775 | } |
| 6776 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6777 | #if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6778 | /* |
| 6779 | * call replace_push(c) with replace_offset set to the first NUL. |
| 6780 | */ |
| 6781 | static void |
| 6782 | replace_push_off(c) |
| 6783 | int c; |
| 6784 | { |
| 6785 | char_u *p; |
| 6786 | |
| 6787 | p = replace_stack + replace_stack_nr; |
| 6788 | for (replace_offset = 1; replace_offset < replace_stack_nr; |
| 6789 | ++replace_offset) |
| 6790 | if (*--p == NUL) |
| 6791 | break; |
| 6792 | replace_push(c); |
| 6793 | replace_offset = 0; |
| 6794 | } |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6795 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6796 | |
| 6797 | /* |
| 6798 | * Pop one item from the replace stack. |
| 6799 | * return -1 if stack empty |
| 6800 | * return replaced character or NUL otherwise |
| 6801 | */ |
| 6802 | static int |
| 6803 | replace_pop() |
| 6804 | { |
| 6805 | if (replace_stack_nr == 0) |
| 6806 | return -1; |
| 6807 | return (int)replace_stack[--replace_stack_nr]; |
| 6808 | } |
| 6809 | |
| 6810 | /* |
| 6811 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 6812 | * encountered. |
| 6813 | */ |
| 6814 | static void |
| 6815 | replace_join(off) |
| 6816 | int off; /* offset for which NUL to remove */ |
| 6817 | { |
| 6818 | int i; |
| 6819 | |
| 6820 | for (i = replace_stack_nr; --i >= 0; ) |
| 6821 | if (replace_stack[i] == NUL && off-- <= 0) |
| 6822 | { |
| 6823 | --replace_stack_nr; |
| 6824 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 6825 | (size_t)(replace_stack_nr - i)); |
| 6826 | return; |
| 6827 | } |
| 6828 | } |
| 6829 | |
| 6830 | /* |
| 6831 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 6832 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 6833 | */ |
| 6834 | static void |
| 6835 | replace_pop_ins() |
| 6836 | { |
| 6837 | int cc; |
| 6838 | int oldState = State; |
| 6839 | |
| 6840 | State = NORMAL; /* don't want REPLACE here */ |
| 6841 | while ((cc = replace_pop()) > 0) |
| 6842 | { |
| 6843 | #ifdef FEAT_MBYTE |
| 6844 | mb_replace_pop_ins(cc); |
| 6845 | #else |
| 6846 | ins_char(cc); |
| 6847 | #endif |
| 6848 | dec_cursor(); |
| 6849 | } |
| 6850 | State = oldState; |
| 6851 | } |
| 6852 | |
| 6853 | #ifdef FEAT_MBYTE |
| 6854 | /* |
| 6855 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 6856 | * indicates a multi-byte char, pop the other bytes too. |
| 6857 | */ |
| 6858 | static void |
| 6859 | mb_replace_pop_ins(cc) |
| 6860 | int cc; |
| 6861 | { |
| 6862 | int n; |
| 6863 | char_u buf[MB_MAXBYTES]; |
| 6864 | int i; |
| 6865 | int c; |
| 6866 | |
| 6867 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 6868 | { |
| 6869 | buf[0] = cc; |
| 6870 | for (i = 1; i < n; ++i) |
| 6871 | buf[i] = replace_pop(); |
| 6872 | ins_bytes_len(buf, n); |
| 6873 | } |
| 6874 | else |
| 6875 | ins_char(cc); |
| 6876 | |
| 6877 | if (enc_utf8) |
| 6878 | /* Handle composing chars. */ |
| 6879 | for (;;) |
| 6880 | { |
| 6881 | c = replace_pop(); |
| 6882 | if (c == -1) /* stack empty */ |
| 6883 | break; |
| 6884 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 6885 | { |
| 6886 | /* Not a multi-byte char, put it back. */ |
| 6887 | replace_push(c); |
| 6888 | break; |
| 6889 | } |
| 6890 | else |
| 6891 | { |
| 6892 | buf[0] = c; |
| 6893 | for (i = 1; i < n; ++i) |
| 6894 | buf[i] = replace_pop(); |
| 6895 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 6896 | ins_bytes_len(buf, n); |
| 6897 | else |
| 6898 | { |
| 6899 | /* Not a composing char, put it back. */ |
| 6900 | for (i = n - 1; i >= 0; --i) |
| 6901 | replace_push(buf[i]); |
| 6902 | break; |
| 6903 | } |
| 6904 | } |
| 6905 | } |
| 6906 | } |
| 6907 | #endif |
| 6908 | |
| 6909 | /* |
| 6910 | * make the replace stack empty |
| 6911 | * (called when exiting replace mode) |
| 6912 | */ |
| 6913 | static void |
| 6914 | replace_flush() |
| 6915 | { |
| 6916 | vim_free(replace_stack); |
| 6917 | replace_stack = NULL; |
| 6918 | replace_stack_len = 0; |
| 6919 | replace_stack_nr = 0; |
| 6920 | } |
| 6921 | |
| 6922 | /* |
| 6923 | * Handle doing a BS for one character. |
| 6924 | * cc < 0: replace stack empty, just move cursor |
| 6925 | * cc == 0: character was inserted, delete it |
| 6926 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 6927 | * and check for more characters to be put back |
| 6928 | */ |
| 6929 | static void |
| 6930 | replace_do_bs() |
| 6931 | { |
| 6932 | int cc; |
| 6933 | #ifdef FEAT_VREPLACE |
| 6934 | int orig_len = 0; |
| 6935 | int ins_len; |
| 6936 | int orig_vcols = 0; |
| 6937 | colnr_T start_vcol; |
| 6938 | char_u *p; |
| 6939 | int i; |
| 6940 | int vcol; |
| 6941 | #endif |
| 6942 | |
| 6943 | cc = replace_pop(); |
| 6944 | if (cc > 0) |
| 6945 | { |
| 6946 | #ifdef FEAT_VREPLACE |
| 6947 | if (State & VREPLACE_FLAG) |
| 6948 | { |
| 6949 | /* Get the number of screen cells used by the character we are |
| 6950 | * going to delete. */ |
| 6951 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 6952 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 6953 | } |
| 6954 | #endif |
| 6955 | #ifdef FEAT_MBYTE |
| 6956 | if (has_mbyte) |
| 6957 | { |
| 6958 | del_char(FALSE); |
| 6959 | # ifdef FEAT_VREPLACE |
| 6960 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6961 | orig_len = (int)STRLEN(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6962 | # endif |
| 6963 | replace_push(cc); |
| 6964 | } |
| 6965 | else |
| 6966 | #endif |
| 6967 | { |
| 6968 | pchar_cursor(cc); |
| 6969 | #ifdef FEAT_VREPLACE |
| 6970 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6971 | orig_len = (int)STRLEN(ml_get_cursor()) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6972 | #endif |
| 6973 | } |
| 6974 | replace_pop_ins(); |
| 6975 | |
| 6976 | #ifdef FEAT_VREPLACE |
| 6977 | if (State & VREPLACE_FLAG) |
| 6978 | { |
| 6979 | /* Get the number of screen cells used by the inserted characters */ |
| 6980 | p = ml_get_cursor(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6981 | ins_len = (int)STRLEN(p) - orig_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6982 | vcol = start_vcol; |
| 6983 | for (i = 0; i < ins_len; ++i) |
| 6984 | { |
| 6985 | vcol += chartabsize(p + i, vcol); |
| 6986 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6987 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6988 | #endif |
| 6989 | } |
| 6990 | vcol -= start_vcol; |
| 6991 | |
| 6992 | /* Delete spaces that were inserted after the cursor to keep the |
| 6993 | * text aligned. */ |
| 6994 | curwin->w_cursor.col += ins_len; |
| 6995 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 6996 | { |
| 6997 | del_char(FALSE); |
| 6998 | ++orig_vcols; |
| 6999 | } |
| 7000 | curwin->w_cursor.col -= ins_len; |
| 7001 | } |
| 7002 | #endif |
| 7003 | |
| 7004 | /* mark the buffer as changed and prepare for displaying */ |
| 7005 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 7006 | } |
| 7007 | else if (cc == 0) |
| 7008 | (void)del_char(FALSE); |
| 7009 | } |
| 7010 | |
| 7011 | #ifdef FEAT_CINDENT |
| 7012 | /* |
| 7013 | * Return TRUE if C-indenting is on. |
| 7014 | */ |
| 7015 | static int |
| 7016 | cindent_on() |
| 7017 | { |
| 7018 | return (!p_paste && (curbuf->b_p_cin |
| 7019 | # ifdef FEAT_EVAL |
| 7020 | || *curbuf->b_p_inde != NUL |
| 7021 | # endif |
| 7022 | )); |
| 7023 | } |
| 7024 | #endif |
| 7025 | |
| 7026 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 7027 | /* |
| 7028 | * Re-indent the current line, based on the current contents of it and the |
| 7029 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 7030 | * confused what all the part that handles Control-T is doing that I'm not. |
| 7031 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 7032 | */ |
| 7033 | |
| 7034 | void |
| 7035 | fixthisline(get_the_indent) |
| 7036 | int (*get_the_indent) __ARGS((void)); |
| 7037 | { |
| 7038 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0); |
| 7039 | if (linewhite(curwin->w_cursor.lnum)) |
| 7040 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 7041 | } |
| 7042 | |
| 7043 | void |
| 7044 | fix_indent() |
| 7045 | { |
| 7046 | if (p_paste) |
| 7047 | return; |
| 7048 | # ifdef FEAT_LISP |
| 7049 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 7050 | fixthisline(get_lisp_indent); |
| 7051 | # endif |
| 7052 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 7053 | else |
| 7054 | # endif |
| 7055 | # ifdef FEAT_CINDENT |
| 7056 | if (cindent_on()) |
| 7057 | do_c_expr_indent(); |
| 7058 | # endif |
| 7059 | } |
| 7060 | |
| 7061 | #endif |
| 7062 | |
| 7063 | #ifdef FEAT_CINDENT |
| 7064 | /* |
| 7065 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 7066 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 7067 | * when == '!': Only if key is prededed with '!' (don't insert) |
| 7068 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 7069 | * |
| 7070 | * "keytyped" can have a few special values: |
| 7071 | * KEY_OPEN_FORW |
| 7072 | * KEY_OPEN_BACK |
| 7073 | * KEY_COMPLETE just finished completion. |
| 7074 | * |
| 7075 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 7076 | */ |
| 7077 | int |
| 7078 | in_cinkeys(keytyped, when, line_is_empty) |
| 7079 | int keytyped; |
| 7080 | int when; |
| 7081 | int line_is_empty; |
| 7082 | { |
| 7083 | char_u *look; |
| 7084 | int try_match; |
| 7085 | int try_match_word; |
| 7086 | char_u *p; |
| 7087 | char_u *line; |
| 7088 | int icase; |
| 7089 | int i; |
| 7090 | |
| 7091 | #ifdef FEAT_EVAL |
| 7092 | if (*curbuf->b_p_inde != NUL) |
| 7093 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 7094 | else |
| 7095 | #endif |
| 7096 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 7097 | while (*look) |
| 7098 | { |
| 7099 | /* |
| 7100 | * Find out if we want to try a match with this key, depending on |
| 7101 | * 'when' and a '*' or '!' before the key. |
| 7102 | */ |
| 7103 | switch (when) |
| 7104 | { |
| 7105 | case '*': try_match = (*look == '*'); break; |
| 7106 | case '!': try_match = (*look == '!'); break; |
| 7107 | default: try_match = (*look != '*'); break; |
| 7108 | } |
| 7109 | if (*look == '*' || *look == '!') |
| 7110 | ++look; |
| 7111 | |
| 7112 | /* |
| 7113 | * If there is a '0', only accept a match if the line is empty. |
| 7114 | * But may still match when typing last char of a word. |
| 7115 | */ |
| 7116 | if (*look == '0') |
| 7117 | { |
| 7118 | try_match_word = try_match; |
| 7119 | if (!line_is_empty) |
| 7120 | try_match = FALSE; |
| 7121 | ++look; |
| 7122 | } |
| 7123 | else |
| 7124 | try_match_word = FALSE; |
| 7125 | |
| 7126 | /* |
| 7127 | * does it look like a control character? |
| 7128 | */ |
| 7129 | if (*look == '^' |
| 7130 | #ifdef EBCDIC |
| 7131 | && (Ctrl_chr(look[1]) != 0) |
| 7132 | #else |
| 7133 | && look[1] >= '?' && look[1] <= '_' |
| 7134 | #endif |
| 7135 | ) |
| 7136 | { |
| 7137 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 7138 | return TRUE; |
| 7139 | look += 2; |
| 7140 | } |
| 7141 | /* |
| 7142 | * 'o' means "o" command, open forward. |
| 7143 | * 'O' means "O" command, open backward. |
| 7144 | */ |
| 7145 | else if (*look == 'o') |
| 7146 | { |
| 7147 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 7148 | return TRUE; |
| 7149 | ++look; |
| 7150 | } |
| 7151 | else if (*look == 'O') |
| 7152 | { |
| 7153 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 7154 | return TRUE; |
| 7155 | ++look; |
| 7156 | } |
| 7157 | |
| 7158 | /* |
| 7159 | * 'e' means to check for "else" at start of line and just before the |
| 7160 | * cursor. |
| 7161 | */ |
| 7162 | else if (*look == 'e') |
| 7163 | { |
| 7164 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 7165 | { |
| 7166 | p = ml_get_curline(); |
| 7167 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 7168 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 7169 | return TRUE; |
| 7170 | } |
| 7171 | ++look; |
| 7172 | } |
| 7173 | |
| 7174 | /* |
| 7175 | * ':' only causes an indent if it is at the end of a label or case |
| 7176 | * statement, or when it was before typing the ':' (to fix |
| 7177 | * class::method for C++). |
| 7178 | */ |
| 7179 | else if (*look == ':') |
| 7180 | { |
| 7181 | if (try_match && keytyped == ':') |
| 7182 | { |
| 7183 | p = ml_get_curline(); |
| 7184 | if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30)) |
| 7185 | return TRUE; |
| 7186 | if (curwin->w_cursor.col > 2 |
| 7187 | && p[curwin->w_cursor.col - 1] == ':' |
| 7188 | && p[curwin->w_cursor.col - 2] == ':') |
| 7189 | { |
| 7190 | p[curwin->w_cursor.col - 1] = ' '; |
| 7191 | i = (cin_iscase(p) || cin_isscopedecl(p) |
| 7192 | || cin_islabel(30)); |
| 7193 | p = ml_get_curline(); |
| 7194 | p[curwin->w_cursor.col - 1] = ':'; |
| 7195 | if (i) |
| 7196 | return TRUE; |
| 7197 | } |
| 7198 | } |
| 7199 | ++look; |
| 7200 | } |
| 7201 | |
| 7202 | |
| 7203 | /* |
| 7204 | * Is it a key in <>, maybe? |
| 7205 | */ |
| 7206 | else if (*look == '<') |
| 7207 | { |
| 7208 | if (try_match) |
| 7209 | { |
| 7210 | /* |
| 7211 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 7212 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 7213 | * >, *, : and ! keys if they really really want to. |
| 7214 | */ |
| 7215 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 7216 | && keytyped == look[1]) |
| 7217 | return TRUE; |
| 7218 | |
| 7219 | if (keytyped == get_special_key_code(look + 1)) |
| 7220 | return TRUE; |
| 7221 | } |
| 7222 | while (*look && *look != '>') |
| 7223 | look++; |
| 7224 | while (*look == '>') |
| 7225 | look++; |
| 7226 | } |
| 7227 | |
| 7228 | /* |
| 7229 | * Is it a word: "=word"? |
| 7230 | */ |
| 7231 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 7232 | { |
| 7233 | ++look; |
| 7234 | if (*look == '~') |
| 7235 | { |
| 7236 | icase = TRUE; |
| 7237 | ++look; |
| 7238 | } |
| 7239 | else |
| 7240 | icase = FALSE; |
| 7241 | p = vim_strchr(look, ','); |
| 7242 | if (p == NULL) |
| 7243 | p = look + STRLEN(look); |
| 7244 | if ((try_match || try_match_word) |
| 7245 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 7246 | { |
| 7247 | int match = FALSE; |
| 7248 | |
| 7249 | #ifdef FEAT_INS_EXPAND |
| 7250 | if (keytyped == KEY_COMPLETE) |
| 7251 | { |
| 7252 | char_u *s; |
| 7253 | |
| 7254 | /* Just completed a word, check if it starts with "look". |
| 7255 | * search back for the start of a word. */ |
| 7256 | line = ml_get_curline(); |
| 7257 | # ifdef FEAT_MBYTE |
| 7258 | if (has_mbyte) |
| 7259 | { |
| 7260 | char_u *n; |
| 7261 | |
| 7262 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 7263 | { |
| 7264 | n = mb_prevptr(line, s); |
| 7265 | if (!vim_iswordp(n)) |
| 7266 | break; |
| 7267 | } |
| 7268 | } |
| 7269 | else |
| 7270 | # endif |
| 7271 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 7272 | if (!vim_iswordc(s[-1])) |
| 7273 | break; |
| 7274 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 7275 | && (icase |
| 7276 | ? MB_STRNICMP(s, look, p - look) |
| 7277 | : STRNCMP(s, look, p - look)) == 0) |
| 7278 | match = TRUE; |
| 7279 | } |
| 7280 | else |
| 7281 | #endif |
| 7282 | /* TODO: multi-byte */ |
| 7283 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 7284 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 7285 | { |
| 7286 | line = ml_get_cursor(); |
| 7287 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 7288 | || !vim_iswordc(line[-(p - look) - 1])) |
| 7289 | && (icase |
| 7290 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 7291 | : STRNCMP(line - (p - look), look, p - look)) |
| 7292 | == 0) |
| 7293 | match = TRUE; |
| 7294 | } |
| 7295 | if (match && try_match_word && !try_match) |
| 7296 | { |
| 7297 | /* "0=word": Check if there are only blanks before the |
| 7298 | * word. */ |
| 7299 | line = ml_get_curline(); |
| 7300 | if ((int)(skipwhite(line) - line) != |
| 7301 | (int)(curwin->w_cursor.col - (p - look))) |
| 7302 | match = FALSE; |
| 7303 | } |
| 7304 | if (match) |
| 7305 | return TRUE; |
| 7306 | } |
| 7307 | look = p; |
| 7308 | } |
| 7309 | |
| 7310 | /* |
| 7311 | * ok, it's a boring generic character. |
| 7312 | */ |
| 7313 | else |
| 7314 | { |
| 7315 | if (try_match && *look == keytyped) |
| 7316 | return TRUE; |
| 7317 | ++look; |
| 7318 | } |
| 7319 | |
| 7320 | /* |
| 7321 | * Skip over ", ". |
| 7322 | */ |
| 7323 | look = skip_to_option_part(look); |
| 7324 | } |
| 7325 | return FALSE; |
| 7326 | } |
| 7327 | #endif /* FEAT_CINDENT */ |
| 7328 | |
| 7329 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 7330 | /* |
| 7331 | * Map Hebrew keyboard when in hkmap mode. |
| 7332 | */ |
| 7333 | int |
| 7334 | hkmap(c) |
| 7335 | int c; |
| 7336 | { |
| 7337 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 7338 | { |
| 7339 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 7340 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 7341 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 7342 | static char_u map[26] = |
| 7343 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 7344 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 7345 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 7346 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 7347 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 7348 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 7349 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 7350 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 7351 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 7352 | |
| 7353 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 7354 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 7355 | /* '-1'='sofit' */ |
| 7356 | else if (c == 'x') |
| 7357 | return 'X'; |
| 7358 | else if (c == 'q') |
| 7359 | return '\''; /* {geresh}={'} */ |
| 7360 | else if (c == 246) |
| 7361 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 7362 | else if (c == 228) |
| 7363 | return ' '; /* \"a --> ' ' -- / -- */ |
| 7364 | else if (c == 252) |
| 7365 | return ' '; /* \"u --> ' ' -- / -- */ |
| 7366 | #ifdef EBCDIC |
| 7367 | else if (islower(c)) |
| 7368 | #else |
| 7369 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 7370 | * do this the same was as 5.7 and previous, so it works correctly on |
| 7371 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 7372 | * munged and won't work if e.g. searching for Hebrew text. |
| 7373 | */ |
| 7374 | else if (c >= 'a' && c <= 'z') |
| 7375 | #endif |
| 7376 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 7377 | else |
| 7378 | return c; |
| 7379 | } |
| 7380 | else |
| 7381 | { |
| 7382 | switch (c) |
| 7383 | { |
| 7384 | case '`': return ';'; |
| 7385 | case '/': return '.'; |
| 7386 | case '\'': return ','; |
| 7387 | case 'q': return '/'; |
| 7388 | case 'w': return '\''; |
| 7389 | |
| 7390 | /* Hebrew letters - set offset from 'a' */ |
| 7391 | case ',': c = '{'; break; |
| 7392 | case '.': c = 'v'; break; |
| 7393 | case ';': c = 't'; break; |
| 7394 | default: { |
| 7395 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 7396 | |
| 7397 | #ifdef EBCDIC |
| 7398 | /* see note about islower() above */ |
| 7399 | if (!islower(c)) |
| 7400 | #else |
| 7401 | if (c < 'a' || c > 'z') |
| 7402 | #endif |
| 7403 | return c; |
| 7404 | c = str[CharOrdLow(c)]; |
| 7405 | break; |
| 7406 | } |
| 7407 | } |
| 7408 | |
| 7409 | return (int)(CharOrdLow(c) + p_aleph); |
| 7410 | } |
| 7411 | } |
| 7412 | #endif |
| 7413 | |
| 7414 | static void |
| 7415 | ins_reg() |
| 7416 | { |
| 7417 | int need_redraw = FALSE; |
| 7418 | int regname; |
| 7419 | int literally = 0; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 7420 | #ifdef FEAT_VISUAL |
| 7421 | int vis_active = VIsual_active; |
| 7422 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7423 | |
| 7424 | /* |
| 7425 | * If we are going to wait for a character, show a '"'. |
| 7426 | */ |
| 7427 | pc_status = PC_STATUS_UNSET; |
| 7428 | if (redrawing() && !char_avail()) |
| 7429 | { |
| 7430 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7431 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7432 | |
| 7433 | edit_putchar('"', TRUE); |
| 7434 | #ifdef FEAT_CMDL_INFO |
| 7435 | add_to_showcmd_c(Ctrl_R); |
| 7436 | #endif |
| 7437 | } |
| 7438 | |
| 7439 | #ifdef USE_ON_FLY_SCROLL |
| 7440 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 7441 | #endif |
| 7442 | |
| 7443 | /* |
| 7444 | * Don't map the register name. This also prevents the mode message to be |
| 7445 | * deleted when ESC is hit. |
| 7446 | */ |
| 7447 | ++no_mapping; |
| 7448 | regname = safe_vgetc(); |
| 7449 | #ifdef FEAT_LANGMAP |
| 7450 | LANGMAP_ADJUST(regname, TRUE); |
| 7451 | #endif |
| 7452 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 7453 | { |
| 7454 | /* Get a third key for literal register insertion */ |
| 7455 | literally = regname; |
| 7456 | #ifdef FEAT_CMDL_INFO |
| 7457 | add_to_showcmd_c(literally); |
| 7458 | #endif |
| 7459 | regname = safe_vgetc(); |
| 7460 | #ifdef FEAT_LANGMAP |
| 7461 | LANGMAP_ADJUST(regname, TRUE); |
| 7462 | #endif |
| 7463 | } |
| 7464 | --no_mapping; |
| 7465 | |
| 7466 | #ifdef FEAT_EVAL |
| 7467 | /* |
| 7468 | * Don't call u_sync() while getting the expression, |
| 7469 | * evaluating it or giving an error message for it! |
| 7470 | */ |
| 7471 | ++no_u_sync; |
| 7472 | if (regname == '=') |
| 7473 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 7474 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7475 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 7476 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7477 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 7478 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7479 | /* Restore the Input Method. */ |
| 7480 | if (im_on) |
| 7481 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 7482 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7483 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 7484 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 7485 | { |
| 7486 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7487 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 7488 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7489 | else |
| 7490 | { |
| 7491 | #endif |
| 7492 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 7493 | { |
| 7494 | /* Append the command to the redo buffer. */ |
| 7495 | AppendCharToRedobuff(Ctrl_R); |
| 7496 | AppendCharToRedobuff(literally); |
| 7497 | AppendCharToRedobuff(regname); |
| 7498 | |
| 7499 | do_put(regname, BACKWARD, 1L, |
| 7500 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 7501 | } |
| 7502 | else if (insert_reg(regname, literally) == FAIL) |
| 7503 | { |
| 7504 | vim_beep(); |
| 7505 | need_redraw = TRUE; /* remove the '"' */ |
| 7506 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 7507 | else if (stop_insert_mode) |
| 7508 | /* When the '=' register was used and a function was invoked that |
| 7509 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 7510 | * insert anything, need to remove the '"' */ |
| 7511 | need_redraw = TRUE; |
| 7512 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7513 | #ifdef FEAT_EVAL |
| 7514 | } |
| 7515 | --no_u_sync; |
| 7516 | #endif |
| 7517 | #ifdef FEAT_CMDL_INFO |
| 7518 | clear_showcmd(); |
| 7519 | #endif |
| 7520 | |
| 7521 | /* If the inserted register is empty, we need to remove the '"' */ |
| 7522 | if (need_redraw || stuff_empty()) |
| 7523 | edit_unputchar(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 7524 | |
| 7525 | #ifdef FEAT_VISUAL |
| 7526 | /* Disallow starting Visual mode here, would get a weird mode. */ |
| 7527 | if (!vis_active && VIsual_active) |
| 7528 | end_visual_mode(); |
| 7529 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7530 | } |
| 7531 | |
| 7532 | /* |
| 7533 | * CTRL-G commands in Insert mode. |
| 7534 | */ |
| 7535 | static void |
| 7536 | ins_ctrl_g() |
| 7537 | { |
| 7538 | int c; |
| 7539 | |
| 7540 | #ifdef FEAT_INS_EXPAND |
| 7541 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 7542 | setcursor(); |
| 7543 | #endif |
| 7544 | |
| 7545 | /* |
| 7546 | * Don't map the second key. This also prevents the mode message to be |
| 7547 | * deleted when ESC is hit. |
| 7548 | */ |
| 7549 | ++no_mapping; |
| 7550 | c = safe_vgetc(); |
| 7551 | --no_mapping; |
| 7552 | switch (c) |
| 7553 | { |
| 7554 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 7555 | case K_UP: |
| 7556 | case Ctrl_K: |
| 7557 | case 'k': ins_up(TRUE); |
| 7558 | break; |
| 7559 | |
| 7560 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 7561 | case K_DOWN: |
| 7562 | case Ctrl_J: |
| 7563 | case 'j': ins_down(TRUE); |
| 7564 | break; |
| 7565 | |
| 7566 | /* CTRL-G u: start new undoable edit */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 7567 | case 'u': u_sync(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7568 | ins_need_undo = TRUE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 7569 | |
| 7570 | /* Need to reset Insstart, esp. because a BS that joins |
| 7571 | * aline to the previous one must save for undo. */ |
| 7572 | Insstart = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7573 | break; |
| 7574 | |
| 7575 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 7576 | default: vim_beep(); |
| 7577 | } |
| 7578 | } |
| 7579 | |
| 7580 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7581 | * CTRL-^ in Insert mode. |
| 7582 | */ |
| 7583 | static void |
| 7584 | ins_ctrl_hat() |
| 7585 | { |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 7586 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7587 | { |
| 7588 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 7589 | if (State & LANGMAP) |
| 7590 | { |
| 7591 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 7592 | State &= ~LANGMAP; |
| 7593 | } |
| 7594 | else |
| 7595 | { |
| 7596 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 7597 | State |= LANGMAP; |
| 7598 | #ifdef USE_IM_CONTROL |
| 7599 | im_set_active(FALSE); |
| 7600 | #endif |
| 7601 | } |
| 7602 | } |
| 7603 | #ifdef USE_IM_CONTROL |
| 7604 | else |
| 7605 | { |
| 7606 | /* There are no ":lmap" mappings, toggle IM */ |
| 7607 | if (im_get_status()) |
| 7608 | { |
| 7609 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 7610 | im_set_active(FALSE); |
| 7611 | } |
| 7612 | else |
| 7613 | { |
| 7614 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 7615 | State &= ~LANGMAP; |
| 7616 | im_set_active(TRUE); |
| 7617 | } |
| 7618 | } |
| 7619 | #endif |
| 7620 | set_iminsert_global(); |
| 7621 | showmode(); |
| 7622 | #ifdef FEAT_GUI |
| 7623 | /* may show different cursor shape or color */ |
| 7624 | if (gui.in_use) |
| 7625 | gui_update_cursor(TRUE, FALSE); |
| 7626 | #endif |
| 7627 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 7628 | /* Show/unshow value of 'keymap' in status lines. */ |
| 7629 | status_redraw_curbuf(); |
| 7630 | #endif |
| 7631 | } |
| 7632 | |
| 7633 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7634 | * Handle ESC in insert mode. |
| 7635 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 7636 | * insert. |
| 7637 | */ |
| 7638 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7639 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7640 | long *count; |
| 7641 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7642 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7643 | { |
| 7644 | int temp; |
| 7645 | static int disabled_redraw = FALSE; |
| 7646 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 7647 | #ifdef FEAT_SPELL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7648 | check_spell_redraw(); |
| 7649 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7650 | #if defined(FEAT_HANGULIN) |
| 7651 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 7652 | hangul_input_state_set(0); |
| 7653 | # endif |
| 7654 | if (composing_hangul) |
| 7655 | { |
| 7656 | push_raw_key(composing_hangul_buffer, 2); |
| 7657 | composing_hangul = 0; |
| 7658 | } |
| 7659 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7660 | |
| 7661 | temp = curwin->w_cursor.col; |
| 7662 | if (disabled_redraw) |
| 7663 | { |
| 7664 | --RedrawingDisabled; |
| 7665 | disabled_redraw = FALSE; |
| 7666 | } |
| 7667 | if (!arrow_used) |
| 7668 | { |
| 7669 | /* |
| 7670 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7671 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 7672 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7673 | */ |
| 7674 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7675 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7676 | |
| 7677 | /* |
| 7678 | * Repeating insert may take a long time. Check for |
| 7679 | * interrupt now and then. |
| 7680 | */ |
| 7681 | if (*count > 0) |
| 7682 | { |
| 7683 | line_breakcheck(); |
| 7684 | if (got_int) |
| 7685 | *count = 0; |
| 7686 | } |
| 7687 | |
| 7688 | if (--*count > 0) /* repeat what was typed */ |
| 7689 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 7690 | /* Vi repeats the insert without replacing characters. */ |
| 7691 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 7692 | State &= ~REPLACE_FLAG; |
| 7693 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7694 | (void)start_redo_ins(); |
| 7695 | if (cmdchar == 'r' || cmdchar == 'v') |
| 7696 | stuffReadbuff(ESC_STR); /* no ESC in redo buffer */ |
| 7697 | ++RedrawingDisabled; |
| 7698 | disabled_redraw = TRUE; |
| 7699 | return FALSE; /* repeat the insert */ |
| 7700 | } |
| 7701 | stop_insert(&curwin->w_cursor, TRUE); |
| 7702 | undisplay_dollar(); |
| 7703 | } |
| 7704 | |
| 7705 | /* When an autoindent was removed, curswant stays after the |
| 7706 | * indent */ |
| 7707 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 7708 | curwin->w_set_curswant = TRUE; |
| 7709 | |
| 7710 | /* Remember the last Insert position in the '^ mark. */ |
| 7711 | if (!cmdmod.keepjumps) |
| 7712 | curbuf->b_last_insert = curwin->w_cursor; |
| 7713 | |
| 7714 | /* |
| 7715 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7716 | * 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] | 7717 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7718 | if (!nomove |
| 7719 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7720 | #ifdef FEAT_VIRTUALEDIT |
| 7721 | || curwin->w_cursor.coladd > 0 |
| 7722 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7723 | ) |
| 7724 | && (restart_edit == NUL |
| 7725 | || (gchar_cursor() == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7726 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7727 | && !VIsual_active |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7728 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 7729 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7730 | #ifdef FEAT_RIGHTLEFT |
| 7731 | && !revins_on |
| 7732 | #endif |
| 7733 | ) |
| 7734 | { |
| 7735 | #ifdef FEAT_VIRTUALEDIT |
| 7736 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 7737 | { |
| 7738 | oneleft(); |
| 7739 | if (restart_edit != NUL) |
| 7740 | ++curwin->w_cursor.coladd; |
| 7741 | } |
| 7742 | else |
| 7743 | #endif |
| 7744 | { |
| 7745 | --curwin->w_cursor.col; |
| 7746 | #ifdef FEAT_MBYTE |
| 7747 | /* Correct cursor for multi-byte character. */ |
| 7748 | if (has_mbyte) |
| 7749 | mb_adjust_cursor(); |
| 7750 | #endif |
| 7751 | } |
| 7752 | } |
| 7753 | |
| 7754 | #ifdef USE_IM_CONTROL |
| 7755 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 7756 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 7757 | * well). */ |
| 7758 | if (!(State & LANGMAP)) |
| 7759 | im_save_status(&curbuf->b_p_iminsert); |
| 7760 | im_set_active(FALSE); |
| 7761 | #endif |
| 7762 | |
| 7763 | State = NORMAL; |
| 7764 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 7765 | changed_cline_bef_curs(); |
| 7766 | |
| 7767 | #ifdef FEAT_MOUSE |
| 7768 | setmouse(); |
| 7769 | #endif |
| 7770 | #ifdef CURSOR_SHAPE |
| 7771 | ui_cursor_shape(); /* may show different cursor shape */ |
| 7772 | #endif |
| 7773 | |
| 7774 | /* |
| 7775 | * When recording or for CTRL-O, need to display the new mode. |
| 7776 | * Otherwise remove the mode message. |
| 7777 | */ |
| 7778 | if (Recording || restart_edit != NUL) |
| 7779 | showmode(); |
| 7780 | else if (p_smd) |
| 7781 | MSG(""); |
| 7782 | |
| 7783 | return TRUE; /* exit Insert mode */ |
| 7784 | } |
| 7785 | |
| 7786 | #ifdef FEAT_RIGHTLEFT |
| 7787 | /* |
| 7788 | * Toggle language: hkmap and revins_on. |
| 7789 | * Move to end of reverse inserted text. |
| 7790 | */ |
| 7791 | static void |
| 7792 | ins_ctrl_() |
| 7793 | { |
| 7794 | if (revins_on && revins_chars && revins_scol >= 0) |
| 7795 | { |
| 7796 | while (gchar_cursor() != NUL && revins_chars--) |
| 7797 | ++curwin->w_cursor.col; |
| 7798 | } |
| 7799 | p_ri = !p_ri; |
| 7800 | revins_on = (State == INSERT && p_ri); |
| 7801 | if (revins_on) |
| 7802 | { |
| 7803 | revins_scol = curwin->w_cursor.col; |
| 7804 | revins_legal++; |
| 7805 | revins_chars = 0; |
| 7806 | undisplay_dollar(); |
| 7807 | } |
| 7808 | else |
| 7809 | revins_scol = -1; |
| 7810 | #ifdef FEAT_FKMAP |
| 7811 | if (p_altkeymap) |
| 7812 | { |
| 7813 | /* |
| 7814 | * to be consistent also for redo command, using '.' |
| 7815 | * set arrow_used to true and stop it - causing to redo |
| 7816 | * characters entered in one mode (normal/reverse insert). |
| 7817 | */ |
| 7818 | arrow_used = TRUE; |
| 7819 | (void)stop_arrow(); |
| 7820 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 7821 | if (p_fkmap && p_ri) |
| 7822 | State = INSERT; |
| 7823 | } |
| 7824 | else |
| 7825 | #endif |
| 7826 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 7827 | showmode(); |
| 7828 | } |
| 7829 | #endif |
| 7830 | |
| 7831 | #ifdef FEAT_VISUAL |
| 7832 | /* |
| 7833 | * If 'keymodel' contains "startsel", may start selection. |
| 7834 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 7835 | */ |
| 7836 | static int |
| 7837 | ins_start_select(c) |
| 7838 | int c; |
| 7839 | { |
| 7840 | if (km_startsel) |
| 7841 | switch (c) |
| 7842 | { |
| 7843 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7844 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7845 | case K_PAGEUP: |
| 7846 | case K_KPAGEUP: |
| 7847 | case K_PAGEDOWN: |
| 7848 | case K_KPAGEDOWN: |
| 7849 | # ifdef MACOS |
| 7850 | case K_LEFT: |
| 7851 | case K_RIGHT: |
| 7852 | case K_UP: |
| 7853 | case K_DOWN: |
| 7854 | case K_END: |
| 7855 | case K_HOME: |
| 7856 | # endif |
| 7857 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 7858 | break; |
| 7859 | /* FALLTHROUGH */ |
| 7860 | case K_S_LEFT: |
| 7861 | case K_S_RIGHT: |
| 7862 | case K_S_UP: |
| 7863 | case K_S_DOWN: |
| 7864 | case K_S_END: |
| 7865 | case K_S_HOME: |
| 7866 | /* Start selection right away, the cursor can move with |
| 7867 | * CTRL-O when beyond the end of the line. */ |
| 7868 | start_selection(); |
| 7869 | |
| 7870 | /* Execute the key in (insert) Select mode. */ |
| 7871 | stuffcharReadbuff(Ctrl_O); |
| 7872 | if (mod_mask) |
| 7873 | { |
| 7874 | char_u buf[4]; |
| 7875 | |
| 7876 | buf[0] = K_SPECIAL; |
| 7877 | buf[1] = KS_MODIFIER; |
| 7878 | buf[2] = mod_mask; |
| 7879 | buf[3] = NUL; |
| 7880 | stuffReadbuff(buf); |
| 7881 | } |
| 7882 | stuffcharReadbuff(c); |
| 7883 | return TRUE; |
| 7884 | } |
| 7885 | return FALSE; |
| 7886 | } |
| 7887 | #endif |
| 7888 | |
| 7889 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7890 | * <Insert> key in Insert mode: toggle insert/remplace mode. |
| 7891 | */ |
| 7892 | static void |
| 7893 | ins_insert(replaceState) |
| 7894 | int replaceState; |
| 7895 | { |
| 7896 | #ifdef FEAT_FKMAP |
| 7897 | if (p_fkmap && p_ri) |
| 7898 | { |
| 7899 | beep_flush(); |
| 7900 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 7901 | return; |
| 7902 | } |
| 7903 | #endif |
| 7904 | |
| 7905 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 7906 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7907 | set_vim_var_string(VV_INSERTMODE, |
| 7908 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7909 | # ifdef FEAT_VREPLACE |
| 7910 | replaceState == VREPLACE ? "v" : |
| 7911 | # endif |
| 7912 | "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 7913 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 7914 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 7915 | #endif |
| 7916 | if (State & REPLACE_FLAG) |
| 7917 | State = INSERT | (State & LANGMAP); |
| 7918 | else |
| 7919 | State = replaceState | (State & LANGMAP); |
| 7920 | AppendCharToRedobuff(K_INS); |
| 7921 | showmode(); |
| 7922 | #ifdef CURSOR_SHAPE |
| 7923 | ui_cursor_shape(); /* may show different cursor shape */ |
| 7924 | #endif |
| 7925 | } |
| 7926 | |
| 7927 | /* |
| 7928 | * Pressed CTRL-O in Insert mode. |
| 7929 | */ |
| 7930 | static void |
| 7931 | ins_ctrl_o() |
| 7932 | { |
| 7933 | #ifdef FEAT_VREPLACE |
| 7934 | if (State & VREPLACE_FLAG) |
| 7935 | restart_edit = 'V'; |
| 7936 | else |
| 7937 | #endif |
| 7938 | if (State & REPLACE_FLAG) |
| 7939 | restart_edit = 'R'; |
| 7940 | else |
| 7941 | restart_edit = 'I'; |
| 7942 | #ifdef FEAT_VIRTUALEDIT |
| 7943 | if (virtual_active()) |
| 7944 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 7945 | else |
| 7946 | #endif |
| 7947 | ins_at_eol = (gchar_cursor() == NUL); |
| 7948 | } |
| 7949 | |
| 7950 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7951 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 7952 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
| 7953 | * Always round the indent to 'shiftwith', this is compatible |
| 7954 | * with vi. But vi only supports ^T and ^D after an |
| 7955 | * autoindent, we support it everywhere. |
| 7956 | */ |
| 7957 | static void |
| 7958 | ins_shift(c, lastc) |
| 7959 | int c; |
| 7960 | int lastc; |
| 7961 | { |
| 7962 | if (stop_arrow() == FAIL) |
| 7963 | return; |
| 7964 | AppendCharToRedobuff(c); |
| 7965 | |
| 7966 | /* |
| 7967 | * 0^D and ^^D: remove all indent. |
| 7968 | */ |
| 7969 | if ((lastc == '0' || lastc == '^') && curwin->w_cursor.col) |
| 7970 | { |
| 7971 | --curwin->w_cursor.col; |
| 7972 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 7973 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 7974 | if (State & REPLACE_FLAG) |
| 7975 | replace_pop_ins(); |
| 7976 | if (lastc == '^') |
| 7977 | old_indent = get_indent(); /* remember curr. indent */ |
| 7978 | change_indent(INDENT_SET, 0, TRUE, 0); |
| 7979 | } |
| 7980 | else |
| 7981 | change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0); |
| 7982 | |
| 7983 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 7984 | did_ai = FALSE; |
| 7985 | #ifdef FEAT_SMARTINDENT |
| 7986 | did_si = FALSE; |
| 7987 | can_si = FALSE; |
| 7988 | can_si_back = FALSE; |
| 7989 | #endif |
| 7990 | #ifdef FEAT_CINDENT |
| 7991 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 7992 | #endif |
| 7993 | } |
| 7994 | |
| 7995 | static void |
| 7996 | ins_del() |
| 7997 | { |
| 7998 | int temp; |
| 7999 | |
| 8000 | if (stop_arrow() == FAIL) |
| 8001 | return; |
| 8002 | if (gchar_cursor() == NUL) /* delete newline */ |
| 8003 | { |
| 8004 | temp = curwin->w_cursor.col; |
| 8005 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
| 8006 | || u_save((linenr_T)(curwin->w_cursor.lnum - 1), |
| 8007 | (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL |
| 8008 | || do_join(FALSE) == FAIL) |
| 8009 | vim_beep(); |
| 8010 | else |
| 8011 | curwin->w_cursor.col = temp; |
| 8012 | } |
| 8013 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 8014 | vim_beep(); |
| 8015 | did_ai = FALSE; |
| 8016 | #ifdef FEAT_SMARTINDENT |
| 8017 | did_si = FALSE; |
| 8018 | can_si = FALSE; |
| 8019 | can_si_back = FALSE; |
| 8020 | #endif |
| 8021 | AppendCharToRedobuff(K_DEL); |
| 8022 | } |
| 8023 | |
| 8024 | /* |
| 8025 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 8026 | * Return TRUE when backspace was actually used. |
| 8027 | */ |
| 8028 | static int |
| 8029 | ins_bs(c, mode, inserted_space_p) |
| 8030 | int c; |
| 8031 | int mode; |
| 8032 | int *inserted_space_p; |
| 8033 | { |
| 8034 | linenr_T lnum; |
| 8035 | int cc; |
| 8036 | int temp = 0; /* init for GCC */ |
| 8037 | colnr_T mincol; |
| 8038 | int did_backspace = FALSE; |
| 8039 | int in_indent; |
| 8040 | int oldState; |
| 8041 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8042 | int cpc[MAX_MCO]; /* composing characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8043 | #endif |
| 8044 | |
| 8045 | /* |
| 8046 | * can't delete anything in an empty file |
| 8047 | * can't backup past first character in buffer |
| 8048 | * can't backup past starting point unless 'backspace' > 1 |
| 8049 | * can backup to a previous line if 'backspace' == 0 |
| 8050 | */ |
| 8051 | if ( bufempty() |
| 8052 | || ( |
| 8053 | #ifdef FEAT_RIGHTLEFT |
| 8054 | !revins_on && |
| 8055 | #endif |
| 8056 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 8057 | || (!can_bs(BS_START) |
| 8058 | && (arrow_used |
| 8059 | || (curwin->w_cursor.lnum == Insstart.lnum |
| 8060 | && curwin->w_cursor.col <= Insstart.col))) |
| 8061 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 8062 | && curwin->w_cursor.col <= ai_col) |
| 8063 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 8064 | { |
| 8065 | vim_beep(); |
| 8066 | return FALSE; |
| 8067 | } |
| 8068 | |
| 8069 | if (stop_arrow() == FAIL) |
| 8070 | return FALSE; |
| 8071 | in_indent = inindent(0); |
| 8072 | #ifdef FEAT_CINDENT |
| 8073 | if (in_indent) |
| 8074 | can_cindent = FALSE; |
| 8075 | #endif |
| 8076 | #ifdef FEAT_COMMENTS |
| 8077 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 8078 | #endif |
| 8079 | #ifdef FEAT_RIGHTLEFT |
| 8080 | if (revins_on) /* put cursor after last inserted char */ |
| 8081 | inc_cursor(); |
| 8082 | #endif |
| 8083 | |
| 8084 | #ifdef FEAT_VIRTUALEDIT |
| 8085 | /* Virtualedit: |
| 8086 | * BACKSPACE_CHAR eats a virtual space |
| 8087 | * BACKSPACE_WORD eats all coladd |
| 8088 | * BACKSPACE_LINE eats all coladd and keeps going |
| 8089 | */ |
| 8090 | if (curwin->w_cursor.coladd > 0) |
| 8091 | { |
| 8092 | if (mode == BACKSPACE_CHAR) |
| 8093 | { |
| 8094 | --curwin->w_cursor.coladd; |
| 8095 | return TRUE; |
| 8096 | } |
| 8097 | if (mode == BACKSPACE_WORD) |
| 8098 | { |
| 8099 | curwin->w_cursor.coladd = 0; |
| 8100 | return TRUE; |
| 8101 | } |
| 8102 | curwin->w_cursor.coladd = 0; |
| 8103 | } |
| 8104 | #endif |
| 8105 | |
| 8106 | /* |
| 8107 | * delete newline! |
| 8108 | */ |
| 8109 | if (curwin->w_cursor.col == 0) |
| 8110 | { |
| 8111 | lnum = Insstart.lnum; |
| 8112 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8113 | #ifdef FEAT_RIGHTLEFT |
| 8114 | || revins_on |
| 8115 | #endif |
| 8116 | ) |
| 8117 | { |
| 8118 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 8119 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 8120 | return FALSE; |
| 8121 | --Insstart.lnum; |
| 8122 | Insstart.col = MAXCOL; |
| 8123 | } |
| 8124 | /* |
| 8125 | * In replace mode: |
| 8126 | * cc < 0: NL was inserted, delete it |
| 8127 | * cc >= 0: NL was replaced, put original characters back |
| 8128 | */ |
| 8129 | cc = -1; |
| 8130 | if (State & REPLACE_FLAG) |
| 8131 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 8132 | /* |
| 8133 | * In replace mode, in the line we started replacing, we only move the |
| 8134 | * cursor. |
| 8135 | */ |
| 8136 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 8137 | { |
| 8138 | dec_cursor(); |
| 8139 | } |
| 8140 | else |
| 8141 | { |
| 8142 | #ifdef FEAT_VREPLACE |
| 8143 | if (!(State & VREPLACE_FLAG) |
| 8144 | || curwin->w_cursor.lnum > orig_line_count) |
| 8145 | #endif |
| 8146 | { |
| 8147 | temp = gchar_cursor(); /* remember current char */ |
| 8148 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8149 | |
| 8150 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 8151 | * the end of the line, otherwise the line will be broken |
| 8152 | * again when auto-formatting. */ |
| 8153 | if (has_format_option(FO_AUTO) |
| 8154 | && has_format_option(FO_WHITE_PAR)) |
| 8155 | { |
| 8156 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 8157 | TRUE); |
| 8158 | int len; |
| 8159 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8160 | len = (int)STRLEN(ptr); |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8161 | if (len > 0 && ptr[len - 1] == ' ') |
| 8162 | ptr[len - 1] = NUL; |
| 8163 | } |
| 8164 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8165 | (void)do_join(FALSE); |
| 8166 | if (temp == NUL && gchar_cursor() != NUL) |
| 8167 | inc_cursor(); |
| 8168 | } |
| 8169 | #ifdef FEAT_VREPLACE |
| 8170 | else |
| 8171 | dec_cursor(); |
| 8172 | #endif |
| 8173 | |
| 8174 | /* |
| 8175 | * In REPLACE mode we have to put back the text that was replaced |
| 8176 | * by the NL. On the replace stack is first a NUL-terminated |
| 8177 | * sequence of characters that were deleted and then the |
| 8178 | * characters that NL replaced. |
| 8179 | */ |
| 8180 | if (State & REPLACE_FLAG) |
| 8181 | { |
| 8182 | /* |
| 8183 | * Do the next ins_char() in NORMAL state, to |
| 8184 | * prevent ins_char() from replacing characters and |
| 8185 | * avoiding showmatch(). |
| 8186 | */ |
| 8187 | oldState = State; |
| 8188 | State = NORMAL; |
| 8189 | /* |
| 8190 | * restore characters (blanks) deleted after cursor |
| 8191 | */ |
| 8192 | while (cc > 0) |
| 8193 | { |
| 8194 | temp = curwin->w_cursor.col; |
| 8195 | #ifdef FEAT_MBYTE |
| 8196 | mb_replace_pop_ins(cc); |
| 8197 | #else |
| 8198 | ins_char(cc); |
| 8199 | #endif |
| 8200 | curwin->w_cursor.col = temp; |
| 8201 | cc = replace_pop(); |
| 8202 | } |
| 8203 | /* restore the characters that NL replaced */ |
| 8204 | replace_pop_ins(); |
| 8205 | State = oldState; |
| 8206 | } |
| 8207 | } |
| 8208 | did_ai = FALSE; |
| 8209 | } |
| 8210 | else |
| 8211 | { |
| 8212 | /* |
| 8213 | * Delete character(s) before the cursor. |
| 8214 | */ |
| 8215 | #ifdef FEAT_RIGHTLEFT |
| 8216 | if (revins_on) /* put cursor on last inserted char */ |
| 8217 | dec_cursor(); |
| 8218 | #endif |
| 8219 | mincol = 0; |
| 8220 | /* keep indent */ |
| 8221 | if (mode == BACKSPACE_LINE && curbuf->b_p_ai |
| 8222 | #ifdef FEAT_RIGHTLEFT |
| 8223 | && !revins_on |
| 8224 | #endif |
| 8225 | ) |
| 8226 | { |
| 8227 | temp = curwin->w_cursor.col; |
| 8228 | beginline(BL_WHITE); |
| 8229 | if (curwin->w_cursor.col < (colnr_T)temp) |
| 8230 | mincol = curwin->w_cursor.col; |
| 8231 | curwin->w_cursor.col = temp; |
| 8232 | } |
| 8233 | |
| 8234 | /* |
| 8235 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 8236 | */ |
| 8237 | if ( mode == BACKSPACE_CHAR |
| 8238 | && ((p_sta && in_indent) |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 8239 | || (curbuf->b_p_sts != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8240 | && (*(ml_get_cursor() - 1) == TAB |
| 8241 | || (*(ml_get_cursor() - 1) == ' ' |
| 8242 | && (!*inserted_space_p |
| 8243 | || arrow_used)))))) |
| 8244 | { |
| 8245 | int ts; |
| 8246 | colnr_T vcol; |
| 8247 | colnr_T want_vcol; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8248 | #if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8249 | int extra = 0; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8250 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8251 | |
| 8252 | *inserted_space_p = FALSE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 8253 | if (p_sta && in_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8254 | ts = curbuf->b_p_sw; |
| 8255 | else |
| 8256 | ts = curbuf->b_p_sts; |
| 8257 | /* Compute the virtual column where we want to be. Since |
| 8258 | * 'showbreak' may get in the way, need to get the last column of |
| 8259 | * the previous character. */ |
| 8260 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 8261 | dec_cursor(); |
| 8262 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 8263 | inc_cursor(); |
| 8264 | want_vcol = (want_vcol / ts) * ts; |
| 8265 | |
| 8266 | /* delete characters until we are at or before want_vcol */ |
| 8267 | while (vcol > want_vcol |
| 8268 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
| 8269 | { |
| 8270 | dec_cursor(); |
| 8271 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 8272 | if (State & REPLACE_FLAG) |
| 8273 | { |
| 8274 | /* Don't delete characters before the insert point when in |
| 8275 | * Replace mode */ |
| 8276 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 8277 | || curwin->w_cursor.col >= Insstart.col) |
| 8278 | { |
| 8279 | #if 0 /* what was this for? It causes problems when sw != ts. */ |
| 8280 | if (State == REPLACE && (int)vcol < want_vcol) |
| 8281 | { |
| 8282 | (void)del_char(FALSE); |
| 8283 | extra = 2; /* don't pop too much */ |
| 8284 | } |
| 8285 | else |
| 8286 | #endif |
| 8287 | replace_do_bs(); |
| 8288 | } |
| 8289 | } |
| 8290 | else |
| 8291 | (void)del_char(FALSE); |
| 8292 | } |
| 8293 | |
| 8294 | /* insert extra spaces until we are at want_vcol */ |
| 8295 | while (vcol < want_vcol) |
| 8296 | { |
| 8297 | /* Remember the first char we inserted */ |
| 8298 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8299 | && curwin->w_cursor.col < Insstart.col) |
| 8300 | Insstart.col = curwin->w_cursor.col; |
| 8301 | |
| 8302 | #ifdef FEAT_VREPLACE |
| 8303 | if (State & VREPLACE_FLAG) |
| 8304 | ins_char(' '); |
| 8305 | else |
| 8306 | #endif |
| 8307 | { |
| 8308 | ins_str((char_u *)" "); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8309 | if ((State & REPLACE_FLAG) /* && extra <= 1 */) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8310 | { |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8311 | #if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8312 | if (extra) |
| 8313 | replace_push_off(NUL); |
| 8314 | else |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8315 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8316 | replace_push(NUL); |
| 8317 | } |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8318 | #if 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8319 | if (extra == 2) |
| 8320 | extra = 1; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8321 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8322 | } |
| 8323 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 8324 | } |
| 8325 | } |
| 8326 | |
| 8327 | /* |
| 8328 | * Delete upto starting point, start of line or previous word. |
| 8329 | */ |
| 8330 | else do |
| 8331 | { |
| 8332 | #ifdef FEAT_RIGHTLEFT |
| 8333 | if (!revins_on) /* put cursor on char to be deleted */ |
| 8334 | #endif |
| 8335 | dec_cursor(); |
| 8336 | |
| 8337 | /* start of word? */ |
| 8338 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 8339 | { |
| 8340 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 8341 | temp = vim_iswordc(gchar_cursor()); |
| 8342 | } |
| 8343 | /* end of word? */ |
| 8344 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 8345 | && (vim_isspace(cc = gchar_cursor()) |
| 8346 | || vim_iswordc(cc) != temp)) |
| 8347 | { |
| 8348 | #ifdef FEAT_RIGHTLEFT |
| 8349 | if (!revins_on) |
| 8350 | #endif |
| 8351 | inc_cursor(); |
| 8352 | #ifdef FEAT_RIGHTLEFT |
| 8353 | else if (State & REPLACE_FLAG) |
| 8354 | dec_cursor(); |
| 8355 | #endif |
| 8356 | break; |
| 8357 | } |
| 8358 | if (State & REPLACE_FLAG) |
| 8359 | replace_do_bs(); |
| 8360 | else |
| 8361 | { |
| 8362 | #ifdef FEAT_MBYTE |
| 8363 | if (enc_utf8 && p_deco) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8364 | (void)utfc_ptr2char(ml_get_cursor(), cpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8365 | #endif |
| 8366 | (void)del_char(FALSE); |
| 8367 | #ifdef FEAT_MBYTE |
| 8368 | /* |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8369 | * If there are combining characters and 'delcombine' is set |
| 8370 | * move the cursor back. Don't back up before the base |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8371 | * character. |
| 8372 | */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8373 | if (enc_utf8 && p_deco && cpc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8374 | inc_cursor(); |
| 8375 | #endif |
| 8376 | #ifdef FEAT_RIGHTLEFT |
| 8377 | if (revins_chars) |
| 8378 | { |
| 8379 | revins_chars--; |
| 8380 | revins_legal++; |
| 8381 | } |
| 8382 | if (revins_on && gchar_cursor() == NUL) |
| 8383 | break; |
| 8384 | #endif |
| 8385 | } |
| 8386 | /* Just a single backspace?: */ |
| 8387 | if (mode == BACKSPACE_CHAR) |
| 8388 | break; |
| 8389 | } while ( |
| 8390 | #ifdef FEAT_RIGHTLEFT |
| 8391 | revins_on || |
| 8392 | #endif |
| 8393 | (curwin->w_cursor.col > mincol |
| 8394 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 8395 | || curwin->w_cursor.col != Insstart.col))); |
| 8396 | did_backspace = TRUE; |
| 8397 | } |
| 8398 | #ifdef FEAT_SMARTINDENT |
| 8399 | did_si = FALSE; |
| 8400 | can_si = FALSE; |
| 8401 | can_si_back = FALSE; |
| 8402 | #endif |
| 8403 | if (curwin->w_cursor.col <= 1) |
| 8404 | did_ai = FALSE; |
| 8405 | /* |
| 8406 | * It's a little strange to put backspaces into the redo |
| 8407 | * buffer, but it makes auto-indent a lot easier to deal |
| 8408 | * with. |
| 8409 | */ |
| 8410 | AppendCharToRedobuff(c); |
| 8411 | |
| 8412 | /* If deleted before the insertion point, adjust it */ |
| 8413 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8414 | && curwin->w_cursor.col < Insstart.col) |
| 8415 | Insstart.col = curwin->w_cursor.col; |
| 8416 | |
| 8417 | /* vi behaviour: the cursor moves backward but the character that |
| 8418 | * was there remains visible |
| 8419 | * Vim behaviour: the cursor moves backward and the character that |
| 8420 | * was there is erased from the screen. |
| 8421 | * We can emulate the vi behaviour by pretending there is a dollar |
| 8422 | * displayed even when there isn't. |
| 8423 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
| 8424 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0) |
| 8425 | dollar_vcol = curwin->w_virtcol; |
| 8426 | |
| 8427 | return did_backspace; |
| 8428 | } |
| 8429 | |
| 8430 | #ifdef FEAT_MOUSE |
| 8431 | static void |
| 8432 | ins_mouse(c) |
| 8433 | int c; |
| 8434 | { |
| 8435 | pos_T tpos; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8436 | win_T *old_curwin = curwin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8437 | |
| 8438 | # ifdef FEAT_GUI |
| 8439 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 8440 | if (!gui.in_use) |
| 8441 | # endif |
| 8442 | if (!mouse_has(MOUSE_INSERT)) |
| 8443 | return; |
| 8444 | |
| 8445 | undisplay_dollar(); |
| 8446 | tpos = curwin->w_cursor; |
| 8447 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 8448 | { |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8449 | #ifdef FEAT_WINDOWS |
| 8450 | win_T *new_curwin = curwin; |
| 8451 | |
| 8452 | if (curwin != old_curwin && win_valid(old_curwin)) |
| 8453 | { |
| 8454 | /* Mouse took us to another window. We need to go back to the |
| 8455 | * previous one to stop insert there properly. */ |
| 8456 | curwin = old_curwin; |
| 8457 | curbuf = curwin->w_buffer; |
| 8458 | } |
| 8459 | #endif |
| 8460 | start_arrow(curwin == old_curwin ? &tpos : NULL); |
| 8461 | #ifdef FEAT_WINDOWS |
| 8462 | if (curwin != new_curwin && win_valid(new_curwin)) |
| 8463 | { |
| 8464 | curwin = new_curwin; |
| 8465 | curbuf = curwin->w_buffer; |
| 8466 | } |
| 8467 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8468 | # ifdef FEAT_CINDENT |
| 8469 | can_cindent = TRUE; |
| 8470 | # endif |
| 8471 | } |
| 8472 | |
| 8473 | #ifdef FEAT_WINDOWS |
| 8474 | /* redraw status lines (in case another window became active) */ |
| 8475 | redraw_statuslines(); |
| 8476 | #endif |
| 8477 | } |
| 8478 | |
| 8479 | static void |
| 8480 | ins_mousescroll(up) |
| 8481 | int up; |
| 8482 | { |
| 8483 | pos_T tpos; |
| 8484 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 8485 | win_T *old_curwin; |
| 8486 | # endif |
| 8487 | |
| 8488 | tpos = curwin->w_cursor; |
| 8489 | |
| 8490 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 8491 | old_curwin = curwin; |
| 8492 | |
| 8493 | /* Currently the mouse coordinates are only known in the GUI. */ |
| 8494 | if (gui.in_use && mouse_row >= 0 && mouse_col >= 0) |
| 8495 | { |
| 8496 | int row, col; |
| 8497 | |
| 8498 | row = mouse_row; |
| 8499 | col = mouse_col; |
| 8500 | |
| 8501 | /* find the window at the pointer coordinates */ |
| 8502 | curwin = mouse_find_win(&row, &col); |
| 8503 | curbuf = curwin->w_buffer; |
| 8504 | } |
| 8505 | if (curwin == old_curwin) |
| 8506 | # endif |
| 8507 | undisplay_dollar(); |
| 8508 | |
| 8509 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 8510 | scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline)); |
| 8511 | else |
| 8512 | scroll_redraw(up, 3L); |
| 8513 | |
| 8514 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 8515 | curwin->w_redr_status = TRUE; |
| 8516 | |
| 8517 | curwin = old_curwin; |
| 8518 | curbuf = curwin->w_buffer; |
| 8519 | # endif |
| 8520 | |
| 8521 | if (!equalpos(curwin->w_cursor, tpos)) |
| 8522 | { |
| 8523 | start_arrow(&tpos); |
| 8524 | # ifdef FEAT_CINDENT |
| 8525 | can_cindent = TRUE; |
| 8526 | # endif |
| 8527 | } |
| 8528 | } |
| 8529 | #endif |
| 8530 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 8531 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 8532 | static void |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 8533 | ins_tabline(c) |
| 8534 | int c; |
| 8535 | { |
| 8536 | /* We will be leaving the current window, unless closing another tab. */ |
| 8537 | if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE |
| 8538 | || (current_tab != 0 && current_tab != tabpage_index(curtab))) |
| 8539 | { |
| 8540 | undisplay_dollar(); |
| 8541 | start_arrow(&curwin->w_cursor); |
| 8542 | # ifdef FEAT_CINDENT |
| 8543 | can_cindent = TRUE; |
| 8544 | # endif |
| 8545 | } |
| 8546 | |
| 8547 | if (c == K_TABLINE) |
| 8548 | goto_tabpage(current_tab); |
| 8549 | else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 8550 | { |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 8551 | handle_tabmenu(); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 8552 | redraw_statuslines(); /* will redraw the tabline when needed */ |
| 8553 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 8554 | } |
| 8555 | #endif |
| 8556 | |
| 8557 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8558 | void |
| 8559 | ins_scroll() |
| 8560 | { |
| 8561 | pos_T tpos; |
| 8562 | |
| 8563 | undisplay_dollar(); |
| 8564 | tpos = curwin->w_cursor; |
| 8565 | if (gui_do_scroll()) |
| 8566 | { |
| 8567 | start_arrow(&tpos); |
| 8568 | # ifdef FEAT_CINDENT |
| 8569 | can_cindent = TRUE; |
| 8570 | # endif |
| 8571 | } |
| 8572 | } |
| 8573 | |
| 8574 | void |
| 8575 | ins_horscroll() |
| 8576 | { |
| 8577 | pos_T tpos; |
| 8578 | |
| 8579 | undisplay_dollar(); |
| 8580 | tpos = curwin->w_cursor; |
| 8581 | if (gui_do_horiz_scroll()) |
| 8582 | { |
| 8583 | start_arrow(&tpos); |
| 8584 | # ifdef FEAT_CINDENT |
| 8585 | can_cindent = TRUE; |
| 8586 | # endif |
| 8587 | } |
| 8588 | } |
| 8589 | #endif |
| 8590 | |
| 8591 | static void |
| 8592 | ins_left() |
| 8593 | { |
| 8594 | pos_T tpos; |
| 8595 | |
| 8596 | #ifdef FEAT_FOLDING |
| 8597 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8598 | foldOpenCursor(); |
| 8599 | #endif |
| 8600 | undisplay_dollar(); |
| 8601 | tpos = curwin->w_cursor; |
| 8602 | if (oneleft() == OK) |
| 8603 | { |
Bram Moolenaar | 39fecab | 2006-08-29 14:07:36 +0000 | [diff] [blame] | 8604 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 8605 | /* Only call start_arrow() when not busy with preediting, it will |
| 8606 | * break undo. K_LEFT is inserted in im_correct_cursor(). */ |
| 8607 | if (!im_is_preediting()) |
| 8608 | #endif |
| 8609 | start_arrow(&tpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8610 | #ifdef FEAT_RIGHTLEFT |
| 8611 | /* If exit reversed string, position is fixed */ |
| 8612 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 8613 | revins_legal++; |
| 8614 | revins_chars++; |
| 8615 | #endif |
| 8616 | } |
| 8617 | |
| 8618 | /* |
| 8619 | * if 'whichwrap' set for cursor in insert mode may go to |
| 8620 | * previous line |
| 8621 | */ |
| 8622 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 8623 | { |
| 8624 | start_arrow(&tpos); |
| 8625 | --(curwin->w_cursor.lnum); |
| 8626 | coladvance((colnr_T)MAXCOL); |
| 8627 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 8628 | } |
| 8629 | else |
| 8630 | vim_beep(); |
| 8631 | } |
| 8632 | |
| 8633 | static void |
| 8634 | ins_home(c) |
| 8635 | int c; |
| 8636 | { |
| 8637 | pos_T tpos; |
| 8638 | |
| 8639 | #ifdef FEAT_FOLDING |
| 8640 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8641 | foldOpenCursor(); |
| 8642 | #endif |
| 8643 | undisplay_dollar(); |
| 8644 | tpos = curwin->w_cursor; |
| 8645 | if (c == K_C_HOME) |
| 8646 | curwin->w_cursor.lnum = 1; |
| 8647 | curwin->w_cursor.col = 0; |
| 8648 | #ifdef FEAT_VIRTUALEDIT |
| 8649 | curwin->w_cursor.coladd = 0; |
| 8650 | #endif |
| 8651 | curwin->w_curswant = 0; |
| 8652 | start_arrow(&tpos); |
| 8653 | } |
| 8654 | |
| 8655 | static void |
| 8656 | ins_end(c) |
| 8657 | int c; |
| 8658 | { |
| 8659 | pos_T tpos; |
| 8660 | |
| 8661 | #ifdef FEAT_FOLDING |
| 8662 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8663 | foldOpenCursor(); |
| 8664 | #endif |
| 8665 | undisplay_dollar(); |
| 8666 | tpos = curwin->w_cursor; |
| 8667 | if (c == K_C_END) |
| 8668 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 8669 | coladvance((colnr_T)MAXCOL); |
| 8670 | curwin->w_curswant = MAXCOL; |
| 8671 | |
| 8672 | start_arrow(&tpos); |
| 8673 | } |
| 8674 | |
| 8675 | static void |
| 8676 | ins_s_left() |
| 8677 | { |
| 8678 | #ifdef FEAT_FOLDING |
| 8679 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8680 | foldOpenCursor(); |
| 8681 | #endif |
| 8682 | undisplay_dollar(); |
| 8683 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 8684 | { |
| 8685 | start_arrow(&curwin->w_cursor); |
| 8686 | (void)bck_word(1L, FALSE, FALSE); |
| 8687 | curwin->w_set_curswant = TRUE; |
| 8688 | } |
| 8689 | else |
| 8690 | vim_beep(); |
| 8691 | } |
| 8692 | |
| 8693 | static void |
| 8694 | ins_right() |
| 8695 | { |
| 8696 | #ifdef FEAT_FOLDING |
| 8697 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8698 | foldOpenCursor(); |
| 8699 | #endif |
| 8700 | undisplay_dollar(); |
| 8701 | if (gchar_cursor() != NUL || virtual_active() |
| 8702 | ) |
| 8703 | { |
| 8704 | start_arrow(&curwin->w_cursor); |
| 8705 | curwin->w_set_curswant = TRUE; |
| 8706 | #ifdef FEAT_VIRTUALEDIT |
| 8707 | if (virtual_active()) |
| 8708 | oneright(); |
| 8709 | else |
| 8710 | #endif |
| 8711 | { |
| 8712 | #ifdef FEAT_MBYTE |
| 8713 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8714 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8715 | else |
| 8716 | #endif |
| 8717 | ++curwin->w_cursor.col; |
| 8718 | } |
| 8719 | |
| 8720 | #ifdef FEAT_RIGHTLEFT |
| 8721 | revins_legal++; |
| 8722 | if (revins_chars) |
| 8723 | revins_chars--; |
| 8724 | #endif |
| 8725 | } |
| 8726 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 8727 | * cursor to the next line */ |
| 8728 | else if (vim_strchr(p_ww, ']') != NULL |
| 8729 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 8730 | { |
| 8731 | start_arrow(&curwin->w_cursor); |
| 8732 | curwin->w_set_curswant = TRUE; |
| 8733 | ++curwin->w_cursor.lnum; |
| 8734 | curwin->w_cursor.col = 0; |
| 8735 | } |
| 8736 | else |
| 8737 | vim_beep(); |
| 8738 | } |
| 8739 | |
| 8740 | static void |
| 8741 | ins_s_right() |
| 8742 | { |
| 8743 | #ifdef FEAT_FOLDING |
| 8744 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 8745 | foldOpenCursor(); |
| 8746 | #endif |
| 8747 | undisplay_dollar(); |
| 8748 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 8749 | || gchar_cursor() != NUL) |
| 8750 | { |
| 8751 | start_arrow(&curwin->w_cursor); |
| 8752 | (void)fwd_word(1L, FALSE, 0); |
| 8753 | curwin->w_set_curswant = TRUE; |
| 8754 | } |
| 8755 | else |
| 8756 | vim_beep(); |
| 8757 | } |
| 8758 | |
| 8759 | static void |
| 8760 | ins_up(startcol) |
| 8761 | int startcol; /* when TRUE move to Insstart.col */ |
| 8762 | { |
| 8763 | pos_T tpos; |
| 8764 | linenr_T old_topline = curwin->w_topline; |
| 8765 | #ifdef FEAT_DIFF |
| 8766 | int old_topfill = curwin->w_topfill; |
| 8767 | #endif |
| 8768 | |
| 8769 | undisplay_dollar(); |
| 8770 | tpos = curwin->w_cursor; |
| 8771 | if (cursor_up(1L, TRUE) == OK) |
| 8772 | { |
| 8773 | if (startcol) |
| 8774 | coladvance(getvcol_nolist(&Insstart)); |
| 8775 | if (old_topline != curwin->w_topline |
| 8776 | #ifdef FEAT_DIFF |
| 8777 | || old_topfill != curwin->w_topfill |
| 8778 | #endif |
| 8779 | ) |
| 8780 | redraw_later(VALID); |
| 8781 | start_arrow(&tpos); |
| 8782 | #ifdef FEAT_CINDENT |
| 8783 | can_cindent = TRUE; |
| 8784 | #endif |
| 8785 | } |
| 8786 | else |
| 8787 | vim_beep(); |
| 8788 | } |
| 8789 | |
| 8790 | static void |
| 8791 | ins_pageup() |
| 8792 | { |
| 8793 | pos_T tpos; |
| 8794 | |
| 8795 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8796 | |
| 8797 | #ifdef FEAT_WINDOWS |
| 8798 | if (mod_mask & MOD_MASK_CTRL) |
| 8799 | { |
| 8800 | /* <C-PageUp>: tab page back */ |
| 8801 | goto_tabpage(-1); |
| 8802 | return; |
| 8803 | } |
| 8804 | #endif |
| 8805 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8806 | tpos = curwin->w_cursor; |
| 8807 | if (onepage(BACKWARD, 1L) == OK) |
| 8808 | { |
| 8809 | start_arrow(&tpos); |
| 8810 | #ifdef FEAT_CINDENT |
| 8811 | can_cindent = TRUE; |
| 8812 | #endif |
| 8813 | } |
| 8814 | else |
| 8815 | vim_beep(); |
| 8816 | } |
| 8817 | |
| 8818 | static void |
| 8819 | ins_down(startcol) |
| 8820 | int startcol; /* when TRUE move to Insstart.col */ |
| 8821 | { |
| 8822 | pos_T tpos; |
| 8823 | linenr_T old_topline = curwin->w_topline; |
| 8824 | #ifdef FEAT_DIFF |
| 8825 | int old_topfill = curwin->w_topfill; |
| 8826 | #endif |
| 8827 | |
| 8828 | undisplay_dollar(); |
| 8829 | tpos = curwin->w_cursor; |
| 8830 | if (cursor_down(1L, TRUE) == OK) |
| 8831 | { |
| 8832 | if (startcol) |
| 8833 | coladvance(getvcol_nolist(&Insstart)); |
| 8834 | if (old_topline != curwin->w_topline |
| 8835 | #ifdef FEAT_DIFF |
| 8836 | || old_topfill != curwin->w_topfill |
| 8837 | #endif |
| 8838 | ) |
| 8839 | redraw_later(VALID); |
| 8840 | start_arrow(&tpos); |
| 8841 | #ifdef FEAT_CINDENT |
| 8842 | can_cindent = TRUE; |
| 8843 | #endif |
| 8844 | } |
| 8845 | else |
| 8846 | vim_beep(); |
| 8847 | } |
| 8848 | |
| 8849 | static void |
| 8850 | ins_pagedown() |
| 8851 | { |
| 8852 | pos_T tpos; |
| 8853 | |
| 8854 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 8855 | |
| 8856 | #ifdef FEAT_WINDOWS |
| 8857 | if (mod_mask & MOD_MASK_CTRL) |
| 8858 | { |
| 8859 | /* <C-PageDown>: tab page forward */ |
| 8860 | goto_tabpage(0); |
| 8861 | return; |
| 8862 | } |
| 8863 | #endif |
| 8864 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8865 | tpos = curwin->w_cursor; |
| 8866 | if (onepage(FORWARD, 1L) == OK) |
| 8867 | { |
| 8868 | start_arrow(&tpos); |
| 8869 | #ifdef FEAT_CINDENT |
| 8870 | can_cindent = TRUE; |
| 8871 | #endif |
| 8872 | } |
| 8873 | else |
| 8874 | vim_beep(); |
| 8875 | } |
| 8876 | |
| 8877 | #ifdef FEAT_DND |
| 8878 | static void |
| 8879 | ins_drop() |
| 8880 | { |
| 8881 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 8882 | } |
| 8883 | #endif |
| 8884 | |
| 8885 | /* |
| 8886 | * Handle TAB in Insert or Replace mode. |
| 8887 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 8888 | */ |
| 8889 | static int |
| 8890 | ins_tab() |
| 8891 | { |
| 8892 | int ind; |
| 8893 | int i; |
| 8894 | int temp; |
| 8895 | |
| 8896 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 8897 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 8898 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 8899 | return FALSE; |
| 8900 | |
| 8901 | ind = inindent(0); |
| 8902 | #ifdef FEAT_CINDENT |
| 8903 | if (ind) |
| 8904 | can_cindent = FALSE; |
| 8905 | #endif |
| 8906 | |
| 8907 | /* |
| 8908 | * When nothing special, insert TAB like a normal character |
| 8909 | */ |
| 8910 | if (!curbuf->b_p_et |
| 8911 | && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw) |
| 8912 | && curbuf->b_p_sts == 0) |
| 8913 | return TRUE; |
| 8914 | |
| 8915 | if (stop_arrow() == FAIL) |
| 8916 | return TRUE; |
| 8917 | |
| 8918 | did_ai = FALSE; |
| 8919 | #ifdef FEAT_SMARTINDENT |
| 8920 | did_si = FALSE; |
| 8921 | can_si = FALSE; |
| 8922 | can_si_back = FALSE; |
| 8923 | #endif |
| 8924 | AppendToRedobuff((char_u *)"\t"); |
| 8925 | |
| 8926 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
| 8927 | temp = (int)curbuf->b_p_sw; |
| 8928 | else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */ |
| 8929 | temp = (int)curbuf->b_p_sts; |
| 8930 | else /* otherwise use 'tabstop' */ |
| 8931 | temp = (int)curbuf->b_p_ts; |
| 8932 | temp -= get_nolist_virtcol() % temp; |
| 8933 | |
| 8934 | /* |
| 8935 | * Insert the first space with ins_char(). It will delete one char in |
| 8936 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 8937 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 8938 | */ |
| 8939 | ins_char(' '); |
| 8940 | while (--temp > 0) |
| 8941 | { |
| 8942 | #ifdef FEAT_VREPLACE |
| 8943 | if (State & VREPLACE_FLAG) |
| 8944 | ins_char(' '); |
| 8945 | else |
| 8946 | #endif |
| 8947 | { |
| 8948 | ins_str((char_u *)" "); |
| 8949 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 8950 | replace_push(NUL); |
| 8951 | } |
| 8952 | } |
| 8953 | |
| 8954 | /* |
| 8955 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 8956 | */ |
| 8957 | if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind))) |
| 8958 | { |
| 8959 | char_u *ptr; |
| 8960 | #ifdef FEAT_VREPLACE |
| 8961 | char_u *saved_line = NULL; /* init for GCC */ |
| 8962 | pos_T pos; |
| 8963 | #endif |
| 8964 | pos_T fpos; |
| 8965 | pos_T *cursor; |
| 8966 | colnr_T want_vcol, vcol; |
| 8967 | int change_col = -1; |
| 8968 | int save_list = curwin->w_p_list; |
| 8969 | |
| 8970 | /* |
| 8971 | * Get the current line. For VREPLACE mode, don't make real changes |
| 8972 | * yet, just work on a copy of the line. |
| 8973 | */ |
| 8974 | #ifdef FEAT_VREPLACE |
| 8975 | if (State & VREPLACE_FLAG) |
| 8976 | { |
| 8977 | pos = curwin->w_cursor; |
| 8978 | cursor = &pos; |
| 8979 | saved_line = vim_strsave(ml_get_curline()); |
| 8980 | if (saved_line == NULL) |
| 8981 | return FALSE; |
| 8982 | ptr = saved_line + pos.col; |
| 8983 | } |
| 8984 | else |
| 8985 | #endif |
| 8986 | { |
| 8987 | ptr = ml_get_cursor(); |
| 8988 | cursor = &curwin->w_cursor; |
| 8989 | } |
| 8990 | |
| 8991 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 8992 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 8993 | curwin->w_p_list = FALSE; |
| 8994 | |
| 8995 | /* Find first white before the cursor */ |
| 8996 | fpos = curwin->w_cursor; |
| 8997 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 8998 | { |
| 8999 | --fpos.col; |
| 9000 | --ptr; |
| 9001 | } |
| 9002 | |
| 9003 | /* In Replace mode, don't change characters before the insert point. */ |
| 9004 | if ((State & REPLACE_FLAG) |
| 9005 | && fpos.lnum == Insstart.lnum |
| 9006 | && fpos.col < Insstart.col) |
| 9007 | { |
| 9008 | ptr += Insstart.col - fpos.col; |
| 9009 | fpos.col = Insstart.col; |
| 9010 | } |
| 9011 | |
| 9012 | /* compute virtual column numbers of first white and cursor */ |
| 9013 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 9014 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 9015 | |
| 9016 | /* Use as many TABs as possible. Beware of 'showbreak' and |
| 9017 | * 'linebreak' adding extra virtual columns. */ |
| 9018 | while (vim_iswhite(*ptr)) |
| 9019 | { |
| 9020 | i = lbr_chartabsize((char_u *)"\t", vcol); |
| 9021 | if (vcol + i > want_vcol) |
| 9022 | break; |
| 9023 | if (*ptr != TAB) |
| 9024 | { |
| 9025 | *ptr = TAB; |
| 9026 | if (change_col < 0) |
| 9027 | { |
| 9028 | change_col = fpos.col; /* Column of first change */ |
| 9029 | /* May have to adjust Insstart */ |
| 9030 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 9031 | Insstart.col = fpos.col; |
| 9032 | } |
| 9033 | } |
| 9034 | ++fpos.col; |
| 9035 | ++ptr; |
| 9036 | vcol += i; |
| 9037 | } |
| 9038 | |
| 9039 | if (change_col >= 0) |
| 9040 | { |
| 9041 | int repl_off = 0; |
| 9042 | |
| 9043 | /* Skip over the spaces we need. */ |
| 9044 | while (vcol < want_vcol && *ptr == ' ') |
| 9045 | { |
| 9046 | vcol += lbr_chartabsize(ptr, vcol); |
| 9047 | ++ptr; |
| 9048 | ++repl_off; |
| 9049 | } |
| 9050 | if (vcol > want_vcol) |
| 9051 | { |
| 9052 | /* Must have a char with 'showbreak' just before it. */ |
| 9053 | --ptr; |
| 9054 | --repl_off; |
| 9055 | } |
| 9056 | fpos.col += repl_off; |
| 9057 | |
| 9058 | /* Delete following spaces. */ |
| 9059 | i = cursor->col - fpos.col; |
| 9060 | if (i > 0) |
| 9061 | { |
| 9062 | mch_memmove(ptr, ptr + i, STRLEN(ptr + i) + 1); |
| 9063 | /* correct replace stack. */ |
| 9064 | if ((State & REPLACE_FLAG) |
| 9065 | #ifdef FEAT_VREPLACE |
| 9066 | && !(State & VREPLACE_FLAG) |
| 9067 | #endif |
| 9068 | ) |
| 9069 | for (temp = i; --temp >= 0; ) |
| 9070 | replace_join(repl_off); |
| 9071 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9072 | #ifdef FEAT_NETBEANS_INTG |
| 9073 | if (usingNetbeans) |
| 9074 | { |
| 9075 | netbeans_removed(curbuf, fpos.lnum, cursor->col, |
| 9076 | (long)(i + 1)); |
| 9077 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 9078 | (char_u *)"\t", 1); |
| 9079 | } |
| 9080 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9081 | cursor->col -= i; |
| 9082 | |
| 9083 | #ifdef FEAT_VREPLACE |
| 9084 | /* |
| 9085 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 9086 | * backspacing over the changed spacing and then inserting the new |
| 9087 | * spacing. |
| 9088 | */ |
| 9089 | if (State & VREPLACE_FLAG) |
| 9090 | { |
| 9091 | /* Backspace from real cursor to change_col */ |
| 9092 | backspace_until_column(change_col); |
| 9093 | |
| 9094 | /* Insert each char in saved_line from changed_col to |
| 9095 | * ptr-cursor */ |
| 9096 | ins_bytes_len(saved_line + change_col, |
| 9097 | cursor->col - change_col); |
| 9098 | } |
| 9099 | #endif |
| 9100 | } |
| 9101 | |
| 9102 | #ifdef FEAT_VREPLACE |
| 9103 | if (State & VREPLACE_FLAG) |
| 9104 | vim_free(saved_line); |
| 9105 | #endif |
| 9106 | curwin->w_p_list = save_list; |
| 9107 | } |
| 9108 | |
| 9109 | return FALSE; |
| 9110 | } |
| 9111 | |
| 9112 | /* |
| 9113 | * Handle CR or NL in insert mode. |
| 9114 | * Return TRUE when out of memory or can't undo. |
| 9115 | */ |
| 9116 | static int |
| 9117 | ins_eol(c) |
| 9118 | int c; |
| 9119 | { |
| 9120 | int i; |
| 9121 | |
| 9122 | if (echeck_abbr(c + ABBR_OFF)) |
| 9123 | return FALSE; |
| 9124 | if (stop_arrow() == FAIL) |
| 9125 | return TRUE; |
| 9126 | undisplay_dollar(); |
| 9127 | |
| 9128 | /* |
| 9129 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 9130 | * character under the cursor. Only push a NUL on the replace stack, |
| 9131 | * nothing to put back when the NL is deleted. |
| 9132 | */ |
| 9133 | if ((State & REPLACE_FLAG) |
| 9134 | #ifdef FEAT_VREPLACE |
| 9135 | && !(State & VREPLACE_FLAG) |
| 9136 | #endif |
| 9137 | ) |
| 9138 | replace_push(NUL); |
| 9139 | |
| 9140 | /* |
| 9141 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 9142 | * replacing the next line, so we push all of the characters left on the |
| 9143 | * line onto the replace stack. This is not done here though, it is done |
| 9144 | * in open_line(). |
| 9145 | */ |
| 9146 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 9147 | #ifdef FEAT_VIRTUALEDIT |
| 9148 | /* Put cursor on NUL if on the last char and coladd is 1 (happens after |
| 9149 | * CTRL-O). */ |
| 9150 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 9151 | coladvance(getviscol()); |
| 9152 | #endif |
| 9153 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9154 | #ifdef FEAT_RIGHTLEFT |
| 9155 | # ifdef FEAT_FKMAP |
| 9156 | if (p_altkeymap && p_fkmap) |
| 9157 | fkmap(NL); |
| 9158 | # endif |
| 9159 | /* NL in reverse insert will always start in the end of |
| 9160 | * current line. */ |
| 9161 | if (revins_on) |
| 9162 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 9163 | #endif |
| 9164 | |
| 9165 | AppendToRedobuff(NL_STR); |
| 9166 | i = open_line(FORWARD, |
| 9167 | #ifdef FEAT_COMMENTS |
| 9168 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 9169 | #endif |
| 9170 | 0, old_indent); |
| 9171 | old_indent = 0; |
| 9172 | #ifdef FEAT_CINDENT |
| 9173 | can_cindent = TRUE; |
| 9174 | #endif |
| 9175 | |
| 9176 | return (!i); |
| 9177 | } |
| 9178 | |
| 9179 | #ifdef FEAT_DIGRAPHS |
| 9180 | /* |
| 9181 | * Handle digraph in insert mode. |
| 9182 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 9183 | * done. |
| 9184 | */ |
| 9185 | static int |
| 9186 | ins_digraph() |
| 9187 | { |
| 9188 | int c; |
| 9189 | int cc; |
| 9190 | |
| 9191 | pc_status = PC_STATUS_UNSET; |
| 9192 | if (redrawing() && !char_avail()) |
| 9193 | { |
| 9194 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9195 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9196 | |
| 9197 | edit_putchar('?', TRUE); |
| 9198 | #ifdef FEAT_CMDL_INFO |
| 9199 | add_to_showcmd_c(Ctrl_K); |
| 9200 | #endif |
| 9201 | } |
| 9202 | |
| 9203 | #ifdef USE_ON_FLY_SCROLL |
| 9204 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 9205 | #endif |
| 9206 | |
| 9207 | /* don't map the digraph chars. This also prevents the |
| 9208 | * mode message to be deleted when ESC is hit */ |
| 9209 | ++no_mapping; |
| 9210 | ++allow_keys; |
| 9211 | c = safe_vgetc(); |
| 9212 | --no_mapping; |
| 9213 | --allow_keys; |
| 9214 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 9215 | { |
| 9216 | #ifdef FEAT_CMDL_INFO |
| 9217 | clear_showcmd(); |
| 9218 | #endif |
| 9219 | insert_special(c, TRUE, FALSE); |
| 9220 | return NUL; |
| 9221 | } |
| 9222 | if (c != ESC) |
| 9223 | { |
| 9224 | if (redrawing() && !char_avail()) |
| 9225 | { |
| 9226 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9227 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9228 | |
| 9229 | if (char2cells(c) == 1) |
| 9230 | { |
| 9231 | /* first remove the '?', otherwise it's restored when typing |
| 9232 | * an ESC next */ |
| 9233 | edit_unputchar(); |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9234 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9235 | edit_putchar(c, TRUE); |
| 9236 | } |
| 9237 | #ifdef FEAT_CMDL_INFO |
| 9238 | add_to_showcmd_c(c); |
| 9239 | #endif |
| 9240 | } |
| 9241 | ++no_mapping; |
| 9242 | ++allow_keys; |
| 9243 | cc = safe_vgetc(); |
| 9244 | --no_mapping; |
| 9245 | --allow_keys; |
| 9246 | if (cc != ESC) |
| 9247 | { |
| 9248 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 9249 | c = getdigraph(c, cc, TRUE); |
| 9250 | #ifdef FEAT_CMDL_INFO |
| 9251 | clear_showcmd(); |
| 9252 | #endif |
| 9253 | return c; |
| 9254 | } |
| 9255 | } |
| 9256 | edit_unputchar(); |
| 9257 | #ifdef FEAT_CMDL_INFO |
| 9258 | clear_showcmd(); |
| 9259 | #endif |
| 9260 | return NUL; |
| 9261 | } |
| 9262 | #endif |
| 9263 | |
| 9264 | /* |
| 9265 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 9266 | * Returns the char to be inserted, or NUL if none found. |
| 9267 | */ |
| 9268 | static int |
| 9269 | ins_copychar(lnum) |
| 9270 | linenr_T lnum; |
| 9271 | { |
| 9272 | int c; |
| 9273 | int temp; |
| 9274 | char_u *ptr, *prev_ptr; |
| 9275 | |
| 9276 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 9277 | { |
| 9278 | vim_beep(); |
| 9279 | return NUL; |
| 9280 | } |
| 9281 | |
| 9282 | /* try to advance to the cursor column */ |
| 9283 | temp = 0; |
| 9284 | ptr = ml_get(lnum); |
| 9285 | prev_ptr = ptr; |
| 9286 | validate_virtcol(); |
| 9287 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 9288 | { |
| 9289 | prev_ptr = ptr; |
| 9290 | temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp); |
| 9291 | } |
| 9292 | if ((colnr_T)temp > curwin->w_virtcol) |
| 9293 | ptr = prev_ptr; |
| 9294 | |
| 9295 | #ifdef FEAT_MBYTE |
| 9296 | c = (*mb_ptr2char)(ptr); |
| 9297 | #else |
| 9298 | c = *ptr; |
| 9299 | #endif |
| 9300 | if (c == NUL) |
| 9301 | vim_beep(); |
| 9302 | return c; |
| 9303 | } |
| 9304 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 9305 | /* |
| 9306 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 9307 | */ |
| 9308 | static int |
| 9309 | ins_ctrl_ey(tc) |
| 9310 | int tc; |
| 9311 | { |
| 9312 | int c = tc; |
| 9313 | |
| 9314 | #ifdef FEAT_INS_EXPAND |
| 9315 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 9316 | { |
| 9317 | if (c == Ctrl_Y) |
| 9318 | scrolldown_clamp(); |
| 9319 | else |
| 9320 | scrollup_clamp(); |
| 9321 | redraw_later(VALID); |
| 9322 | } |
| 9323 | else |
| 9324 | #endif |
| 9325 | { |
| 9326 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 9327 | if (c != NUL) |
| 9328 | { |
| 9329 | long tw_save; |
| 9330 | |
| 9331 | /* The character must be taken literally, insert like it |
| 9332 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 9333 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 9334 | * CTRL-V, don't use it for these. */ |
| 9335 | if (c < 256 && !isalnum(c)) |
| 9336 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 9337 | tw_save = curbuf->b_p_tw; |
| 9338 | curbuf->b_p_tw = -1; |
| 9339 | insert_special(c, TRUE, FALSE); |
| 9340 | curbuf->b_p_tw = tw_save; |
| 9341 | #ifdef FEAT_RIGHTLEFT |
| 9342 | revins_chars++; |
| 9343 | revins_legal++; |
| 9344 | #endif |
| 9345 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 9346 | auto_format(FALSE, TRUE); |
| 9347 | } |
| 9348 | } |
| 9349 | return c; |
| 9350 | } |
| 9351 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9352 | #ifdef FEAT_SMARTINDENT |
| 9353 | /* |
| 9354 | * Try to do some very smart auto-indenting. |
| 9355 | * Used when inserting a "normal" character. |
| 9356 | */ |
| 9357 | static void |
| 9358 | ins_try_si(c) |
| 9359 | int c; |
| 9360 | { |
| 9361 | pos_T *pos, old_pos; |
| 9362 | char_u *ptr; |
| 9363 | int i; |
| 9364 | int temp; |
| 9365 | |
| 9366 | /* |
| 9367 | * do some very smart indenting when entering '{' or '}' |
| 9368 | */ |
| 9369 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 9370 | { |
| 9371 | /* |
| 9372 | * for '}' set indent equal to indent of line containing matching '{' |
| 9373 | */ |
| 9374 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 9375 | { |
| 9376 | old_pos = curwin->w_cursor; |
| 9377 | /* |
| 9378 | * If the matching '{' has a ')' immediately before it (ignoring |
| 9379 | * white-space), then line up with the start of the line |
| 9380 | * containing the matching '(' if there is one. This handles the |
| 9381 | * case where an "if (..\n..) {" statement continues over multiple |
| 9382 | * lines -- webb |
| 9383 | */ |
| 9384 | ptr = ml_get(pos->lnum); |
| 9385 | i = pos->col; |
| 9386 | if (i > 0) /* skip blanks before '{' */ |
| 9387 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 9388 | ; |
| 9389 | curwin->w_cursor.lnum = pos->lnum; |
| 9390 | curwin->w_cursor.col = i; |
| 9391 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 9392 | curwin->w_cursor = *pos; |
| 9393 | i = get_indent(); |
| 9394 | curwin->w_cursor = old_pos; |
| 9395 | #ifdef FEAT_VREPLACE |
| 9396 | if (State & VREPLACE_FLAG) |
| 9397 | change_indent(INDENT_SET, i, FALSE, NUL); |
| 9398 | else |
| 9399 | #endif |
| 9400 | (void)set_indent(i, SIN_CHANGED); |
| 9401 | } |
| 9402 | else if (curwin->w_cursor.col > 0) |
| 9403 | { |
| 9404 | /* |
| 9405 | * when inserting '{' after "O" reduce indent, but not |
| 9406 | * more than indent of previous line |
| 9407 | */ |
| 9408 | temp = TRUE; |
| 9409 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 9410 | { |
| 9411 | old_pos = curwin->w_cursor; |
| 9412 | i = get_indent(); |
| 9413 | while (curwin->w_cursor.lnum > 1) |
| 9414 | { |
| 9415 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 9416 | |
| 9417 | /* ignore empty lines and lines starting with '#'. */ |
| 9418 | if (*ptr != '#' && *ptr != NUL) |
| 9419 | break; |
| 9420 | } |
| 9421 | if (get_indent() >= i) |
| 9422 | temp = FALSE; |
| 9423 | curwin->w_cursor = old_pos; |
| 9424 | } |
| 9425 | if (temp) |
| 9426 | shift_line(TRUE, FALSE, 1); |
| 9427 | } |
| 9428 | } |
| 9429 | |
| 9430 | /* |
| 9431 | * set indent of '#' always to 0 |
| 9432 | */ |
| 9433 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 9434 | { |
| 9435 | /* remember current indent for next line */ |
| 9436 | old_indent = get_indent(); |
| 9437 | (void)set_indent(0, SIN_CHANGED); |
| 9438 | } |
| 9439 | |
| 9440 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 9441 | if (ai_col > curwin->w_cursor.col) |
| 9442 | ai_col = curwin->w_cursor.col; |
| 9443 | } |
| 9444 | #endif |
| 9445 | |
| 9446 | /* |
| 9447 | * Get the value that w_virtcol would have when 'list' is off. |
| 9448 | * Unless 'cpo' contains the 'L' flag. |
| 9449 | */ |
| 9450 | static colnr_T |
| 9451 | get_nolist_virtcol() |
| 9452 | { |
| 9453 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 9454 | return getvcol_nolist(&curwin->w_cursor); |
| 9455 | validate_virtcol(); |
| 9456 | return curwin->w_virtcol; |
| 9457 | } |