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 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 60 | static char e_hitend[] = N_("Hit end of paragraph"); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 61 | #ifdef FEAT_COMPL_FUNC |
| 62 | static char e_complwin[] = N_("E839: Completion function changed window"); |
| 63 | static char e_compldel[] = N_("E840: Completion function deleted text"); |
| 64 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Structure used to store one match for insert completion. |
| 68 | */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 69 | typedef struct compl_S compl_T; |
| 70 | struct compl_S |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 72 | compl_T *cp_next; |
| 73 | compl_T *cp_prev; |
| 74 | char_u *cp_str; /* matched text */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 75 | char cp_icase; /* TRUE or FALSE: ignore case */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 76 | char_u *(cp_text[CPT_COUNT]); /* text for the menu */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 77 | char_u *cp_fname; /* file containing the match, allocated when |
| 78 | * cp_flags has FREE_FNAME */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 79 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 80 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 83 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | #define FREE_FNAME (2) |
| 85 | |
| 86 | /* |
| 87 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 88 | * "compl_first_match" points to the start of the list. |
| 89 | * "compl_curr_match" points to the currently selected entry. |
| 90 | * "compl_shown_match" is different from compl_curr_match during |
| 91 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 92 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 93 | static compl_T *compl_first_match = NULL; |
| 94 | static compl_T *compl_curr_match = NULL; |
| 95 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 97 | /* After using a cursor key <Enter> selects a match in the popup menu, |
| 98 | * otherwise it inserts a line break. */ |
| 99 | static int compl_enter_selects = FALSE; |
| 100 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 101 | /* When "compl_leader" is not NULL only matches that start with this string |
| 102 | * are used. */ |
| 103 | static char_u *compl_leader = NULL; |
| 104 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 105 | static int compl_get_longest = FALSE; /* put longest common string |
| 106 | in compl_leader */ |
| 107 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 108 | static int compl_used_match; /* Selected one of the matches. When |
| 109 | FALSE the match was edited or using |
| 110 | the longest common string. */ |
| 111 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 112 | static int compl_was_interrupted = FALSE; /* didn't finish finding |
| 113 | completions. */ |
| 114 | |
| 115 | static int compl_restarting = FALSE; /* don't insert match */ |
| 116 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 117 | /* When the first completion is done "compl_started" is set. When it's |
| 118 | * FALSE the word to be completed must be located. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 119 | static int compl_started = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 120 | |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 121 | /* Set when doing something for completion that may call edit() recursively, |
| 122 | * which is not allowed. */ |
| 123 | static int compl_busy = FALSE; |
| 124 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 125 | static int compl_matches = 0; |
| 126 | static char_u *compl_pattern = NULL; |
| 127 | static int compl_direction = FORWARD; |
| 128 | static int compl_shows_dir = FORWARD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 129 | static int compl_pending = 0; /* > 1 for postponed CTRL-N */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 130 | static pos_T compl_startpos; |
| 131 | static colnr_T compl_col = 0; /* column where the text starts |
| 132 | * that is being completed */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 133 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 134 | * completion started */ |
| 135 | static int compl_cont_mode = 0; |
| 136 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 138 | static int compl_opt_refresh_always = FALSE; |
| 139 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 140 | static void ins_ctrl_x __ARGS((void)); |
| 141 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 142 | static int ins_compl_accept_char __ARGS((int c)); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 143 | 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] | 144 | 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] | 145 | static void ins_compl_longest_match __ARGS((compl_T *match)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 146 | 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] | 147 | static int ins_compl_make_cyclic __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 148 | static void ins_compl_upd_pum __ARGS((void)); |
| 149 | static void ins_compl_del_pum __ARGS((void)); |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 150 | static int pum_wanted __ARGS((void)); |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 151 | static int pum_enough_matches __ARGS((void)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 152 | 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] | 153 | 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] | 154 | static char_u *find_line_end __ARGS((char_u *ptr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | static void ins_compl_free __ARGS((void)); |
| 156 | static void ins_compl_clear __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 157 | static int ins_compl_bs __ARGS((void)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 158 | static int ins_compl_need_restart __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 159 | static void ins_compl_new_leader __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 160 | static void ins_compl_addleader __ARGS((int c)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 161 | static int ins_compl_len __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 162 | static void ins_compl_restart __ARGS((void)); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 163 | static void ins_compl_set_original_text __ARGS((char_u *str)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 164 | static void ins_compl_addfrommatch __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 165 | static int ins_compl_prep __ARGS((int c)); |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 166 | static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | 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] | 168 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 169 | static void ins_compl_add_list __ARGS((list_T *list)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 170 | static void ins_compl_add_dict __ARGS((dict_T *dict)); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 171 | #endif |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 172 | static int ins_compl_get_exp __ARGS((pos_T *ini)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | static void ins_compl_delete __ARGS((void)); |
| 174 | static void ins_compl_insert __ARGS((void)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 175 | 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] | 176 | static int ins_compl_key2dir __ARGS((int c)); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 177 | static int ins_compl_pum_key __ARGS((int c)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 178 | static int ins_compl_key2count __ARGS((int c)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 179 | static int ins_compl_use_match __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | static int ins_complete __ARGS((int c)); |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 181 | static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | #endif /* FEAT_INS_EXPAND */ |
| 183 | |
| 184 | #define BACKSPACE_CHAR 1 |
| 185 | #define BACKSPACE_WORD 2 |
| 186 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 187 | #define BACKSPACE_LINE 4 |
| 188 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 189 | static void ins_redraw __ARGS((int ready)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | static void ins_ctrl_v __ARGS((void)); |
| 191 | static void undisplay_dollar __ARGS((void)); |
| 192 | static void insert_special __ARGS((int, int, int)); |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 193 | static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | static void check_auto_format __ARGS((int)); |
| 195 | static void redo_literal __ARGS((int c)); |
| 196 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 197 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 198 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 199 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 200 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 201 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc)); |
| 203 | static int echeck_abbr __ARGS((int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | static int replace_pop __ARGS((void)); |
| 205 | static void replace_join __ARGS((int off)); |
| 206 | static void replace_pop_ins __ARGS((void)); |
| 207 | #ifdef FEAT_MBYTE |
| 208 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 209 | #endif |
| 210 | static void replace_flush __ARGS((void)); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 211 | static void replace_do_bs __ARGS((int limit_col)); |
| 212 | static int del_char_after_col __ARGS((int limit_col)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | #ifdef FEAT_CINDENT |
| 214 | static int cindent_on __ARGS((void)); |
| 215 | #endif |
| 216 | static void ins_reg __ARGS((void)); |
| 217 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 218 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 219 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | #ifdef FEAT_RIGHTLEFT |
| 221 | static void ins_ctrl_ __ARGS((void)); |
| 222 | #endif |
| 223 | #ifdef FEAT_VISUAL |
| 224 | static int ins_start_select __ARGS((int c)); |
| 225 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 226 | static void ins_insert __ARGS((int replaceState)); |
| 227 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | static void ins_shift __ARGS((int c, int lastc)); |
| 229 | static void ins_del __ARGS((void)); |
| 230 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 231 | #ifdef FEAT_MOUSE |
| 232 | static void ins_mouse __ARGS((int c)); |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 233 | static void ins_mousescroll __ARGS((int dir)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 235 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 236 | static void ins_tabline __ARGS((int c)); |
| 237 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | static void ins_left __ARGS((void)); |
| 239 | static void ins_home __ARGS((int c)); |
| 240 | static void ins_end __ARGS((int c)); |
| 241 | static void ins_s_left __ARGS((void)); |
| 242 | static void ins_right __ARGS((void)); |
| 243 | static void ins_s_right __ARGS((void)); |
| 244 | static void ins_up __ARGS((int startcol)); |
| 245 | static void ins_pageup __ARGS((void)); |
| 246 | static void ins_down __ARGS((int startcol)); |
| 247 | static void ins_pagedown __ARGS((void)); |
| 248 | #ifdef FEAT_DND |
| 249 | static void ins_drop __ARGS((void)); |
| 250 | #endif |
| 251 | static int ins_tab __ARGS((void)); |
| 252 | static int ins_eol __ARGS((int c)); |
| 253 | #ifdef FEAT_DIGRAPHS |
| 254 | static int ins_digraph __ARGS((void)); |
| 255 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 256 | static int ins_ctrl_ey __ARGS((int tc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 257 | #ifdef FEAT_SMARTINDENT |
| 258 | static void ins_try_si __ARGS((int c)); |
| 259 | #endif |
| 260 | static colnr_T get_nolist_virtcol __ARGS((void)); |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 261 | #ifdef FEAT_AUTOCMD |
| 262 | static char_u *do_insert_char_pre __ARGS((int c)); |
| 263 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 264 | |
| 265 | static colnr_T Insstart_textlen; /* length of line when insert started */ |
| 266 | static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ |
| 267 | |
| 268 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 269 | K_SPECIAL and CSI are escaped */ |
| 270 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 271 | 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] | 272 | static int did_restart_edit; /* "restart_edit" when calling edit() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 273 | |
| 274 | #ifdef FEAT_CINDENT |
| 275 | static int can_cindent; /* may do cindenting on this line */ |
| 276 | #endif |
| 277 | |
| 278 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 279 | |
| 280 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 281 | static int revins_on; /* reverse insert mode on */ |
| 282 | static int revins_chars; /* how much to skip after edit */ |
| 283 | static int revins_legal; /* was the last char 'legal'? */ |
| 284 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 285 | #endif |
| 286 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 287 | static int ins_need_undo; /* call u_save() before inserting a |
| 288 | char. Set when edit() is called. |
| 289 | after that arrow_used is used. */ |
| 290 | |
| 291 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 292 | under the cursor */ |
| 293 | |
| 294 | /* |
| 295 | * edit(): Start inserting text. |
| 296 | * |
| 297 | * "cmdchar" can be: |
| 298 | * 'i' normal insert command |
| 299 | * 'a' normal append command |
| 300 | * 'R' replace command |
| 301 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 302 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 303 | * 'g' "gI" command. |
| 304 | * 'V' "gR" command for Virtual Replace mode. |
| 305 | * 'v' "gr" command for single character Virtual Replace mode. |
| 306 | * |
| 307 | * This function is not called recursively. For CTRL-O commands, it returns |
| 308 | * and lets the caller handle the Normal-mode command. |
| 309 | * |
| 310 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 311 | */ |
| 312 | int |
| 313 | edit(cmdchar, startln, count) |
| 314 | int cmdchar; |
| 315 | int startln; /* if set, insert at start of line */ |
| 316 | long count; |
| 317 | { |
| 318 | int c = 0; |
| 319 | char_u *ptr; |
| 320 | int lastc; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 321 | int mincol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 322 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | int i; |
| 324 | int did_backspace = TRUE; /* previous char was backspace */ |
| 325 | #ifdef FEAT_CINDENT |
| 326 | int line_is_white = FALSE; /* line is empty before insert */ |
| 327 | #endif |
| 328 | linenr_T old_topline = 0; /* topline before insertion */ |
| 329 | #ifdef FEAT_DIFF |
| 330 | int old_topfill = -1; |
| 331 | #endif |
| 332 | int inserted_space = FALSE; /* just inserted a space */ |
| 333 | int replaceState = REPLACE; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 334 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 335 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 336 | /* Remember whether editing was restarted after CTRL-O. */ |
| 337 | did_restart_edit = restart_edit; |
| 338 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 339 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 340 | * error message */ |
| 341 | check_for_delay(TRUE); |
| 342 | |
| 343 | #ifdef HAVE_SANDBOX |
| 344 | /* Don't allow inserting in the sandbox. */ |
| 345 | if (sandbox != 0) |
| 346 | { |
| 347 | EMSG(_(e_sandbox)); |
| 348 | return FALSE; |
| 349 | } |
| 350 | #endif |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 351 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 352 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 353 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 354 | { |
| 355 | EMSG(_(e_secure)); |
| 356 | return FALSE; |
| 357 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | |
| 359 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 360 | /* Don't allow recursive insert mode when busy with completion. */ |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 361 | if (compl_started || compl_busy || pum_visible()) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 362 | { |
| 363 | EMSG(_(e_secure)); |
| 364 | return FALSE; |
| 365 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 366 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 367 | #endif |
| 368 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 369 | #ifdef FEAT_AUTOCMD |
| 370 | /* |
| 371 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 372 | */ |
| 373 | if (cmdchar != 'r' && cmdchar != 'v') |
| 374 | { |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 375 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 376 | if (cmdchar == 'R') |
| 377 | ptr = (char_u *)"r"; |
| 378 | else if (cmdchar == 'V') |
| 379 | ptr = (char_u *)"v"; |
| 380 | else |
| 381 | ptr = (char_u *)"i"; |
| 382 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 383 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 384 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
| 385 | } |
| 386 | #endif |
| 387 | |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 388 | #ifdef FEAT_CONCEAL |
| 389 | /* Check if the cursor line needs redrawing before changing State. If |
| 390 | * 'concealcursor' is "n" it needs to be redrawn without concealing. */ |
Bram Moolenaar | 8e46927 | 2010-07-28 19:38:16 +0200 | [diff] [blame] | 391 | conceal_check_cursur_line(); |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 392 | #endif |
| 393 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 394 | #ifdef FEAT_MOUSE |
| 395 | /* |
| 396 | * When doing a paste with the middle mouse button, Insstart is set to |
| 397 | * where the paste started. |
| 398 | */ |
| 399 | if (where_paste_started.lnum != 0) |
| 400 | Insstart = where_paste_started; |
| 401 | else |
| 402 | #endif |
| 403 | { |
| 404 | Insstart = curwin->w_cursor; |
| 405 | if (startln) |
| 406 | Insstart.col = 0; |
| 407 | } |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 408 | Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | Insstart_blank_vcol = MAXCOL; |
| 410 | if (!did_ai) |
| 411 | ai_col = 0; |
| 412 | |
| 413 | if (cmdchar != NUL && restart_edit == 0) |
| 414 | { |
| 415 | ResetRedobuff(); |
| 416 | AppendNumberToRedobuff(count); |
| 417 | #ifdef FEAT_VREPLACE |
| 418 | if (cmdchar == 'V' || cmdchar == 'v') |
| 419 | { |
| 420 | /* "gR" or "gr" command */ |
| 421 | AppendCharToRedobuff('g'); |
| 422 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 423 | } |
| 424 | else |
| 425 | #endif |
| 426 | { |
| 427 | AppendCharToRedobuff(cmdchar); |
| 428 | if (cmdchar == 'g') /* "gI" command */ |
| 429 | AppendCharToRedobuff('I'); |
| 430 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 431 | count = 1; /* insert only one <CR> */ |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (cmdchar == 'R') |
| 436 | { |
| 437 | #ifdef FEAT_FKMAP |
| 438 | if (p_fkmap && p_ri) |
| 439 | { |
| 440 | beep_flush(); |
| 441 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 442 | State = INSERT; |
| 443 | } |
| 444 | else |
| 445 | #endif |
| 446 | State = REPLACE; |
| 447 | } |
| 448 | #ifdef FEAT_VREPLACE |
| 449 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 450 | { |
| 451 | State = VREPLACE; |
| 452 | replaceState = VREPLACE; |
| 453 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 454 | vr_lines_changed = 1; |
| 455 | } |
| 456 | #endif |
| 457 | else |
| 458 | State = INSERT; |
| 459 | |
| 460 | stop_insert_mode = FALSE; |
| 461 | |
| 462 | /* |
| 463 | * Need to recompute the cursor position, it might move when the cursor is |
| 464 | * on a TAB or special character. |
| 465 | */ |
| 466 | curs_columns(TRUE); |
| 467 | |
| 468 | /* |
| 469 | * Enable langmap or IME, indicated by 'iminsert'. |
| 470 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 471 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 472 | * when hitting <Esc>. |
| 473 | */ |
| 474 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 475 | State |= LANGMAP; |
| 476 | #ifdef USE_IM_CONTROL |
| 477 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 478 | #endif |
| 479 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | #ifdef FEAT_MOUSE |
| 481 | setmouse(); |
| 482 | #endif |
| 483 | #ifdef FEAT_CMDL_INFO |
| 484 | clear_showcmd(); |
| 485 | #endif |
| 486 | #ifdef FEAT_RIGHTLEFT |
| 487 | /* there is no reverse replace mode */ |
| 488 | revins_on = (State == INSERT && p_ri); |
| 489 | if (revins_on) |
| 490 | undisplay_dollar(); |
| 491 | revins_chars = 0; |
| 492 | revins_legal = 0; |
| 493 | revins_scol = -1; |
| 494 | #endif |
| 495 | |
| 496 | /* |
| 497 | * Handle restarting Insert mode. |
| 498 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 499 | * restart_edit non-zero, and something in the stuff buffer. |
| 500 | */ |
| 501 | if (restart_edit != 0 && stuff_empty()) |
| 502 | { |
| 503 | #ifdef FEAT_MOUSE |
| 504 | /* |
| 505 | * After a paste we consider text typed to be part of the insert for |
| 506 | * the pasted text. You can backspace over the pasted text too. |
| 507 | */ |
| 508 | if (where_paste_started.lnum) |
| 509 | arrow_used = FALSE; |
| 510 | else |
| 511 | #endif |
| 512 | arrow_used = TRUE; |
| 513 | restart_edit = 0; |
| 514 | |
| 515 | /* |
| 516 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 517 | * now at the end-of-line, put it after the end-of-line (this is not |
| 518 | * correct in very rare cases). |
| 519 | * Also do this if curswant is greater than the current virtual |
| 520 | * column. Eg after "^O$" or "^O80|". |
| 521 | */ |
| 522 | validate_virtcol(); |
| 523 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 524 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 525 | || curwin->w_curswant > curwin->w_virtcol) |
| 526 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 527 | { |
| 528 | if (ptr[1] == NUL) |
| 529 | ++curwin->w_cursor.col; |
| 530 | #ifdef FEAT_MBYTE |
| 531 | else if (has_mbyte) |
| 532 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 533 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 534 | if (ptr[i] == NUL) |
| 535 | curwin->w_cursor.col += i; |
| 536 | } |
| 537 | #endif |
| 538 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 539 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 540 | } |
| 541 | else |
| 542 | arrow_used = FALSE; |
| 543 | |
| 544 | /* we are in insert mode now, don't need to start it anymore */ |
| 545 | need_start_insertmode = FALSE; |
| 546 | |
| 547 | /* Need to save the line for undo before inserting the first char. */ |
| 548 | ins_need_undo = TRUE; |
| 549 | |
| 550 | #ifdef FEAT_MOUSE |
| 551 | where_paste_started.lnum = 0; |
| 552 | #endif |
| 553 | #ifdef FEAT_CINDENT |
| 554 | can_cindent = TRUE; |
| 555 | #endif |
| 556 | #ifdef FEAT_FOLDING |
| 557 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 558 | * restarting. */ |
| 559 | if (!p_im && did_restart_edit == 0) |
| 560 | foldOpenCursor(); |
| 561 | #endif |
| 562 | |
| 563 | /* |
| 564 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 565 | * A warning message for changing a readonly file is given here, before |
| 566 | * actually changing anything. It's put after the mode, if any. |
| 567 | */ |
| 568 | i = 0; |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 569 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 570 | i = showmode(); |
| 571 | |
| 572 | if (!p_im && did_restart_edit == 0) |
Bram Moolenaar | 21af89e | 2008-01-02 21:09:33 +0000 | [diff] [blame] | 573 | change_warning(i == 0 ? 0 : i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 574 | |
| 575 | #ifdef CURSOR_SHAPE |
| 576 | ui_cursor_shape(); /* may show different cursor shape */ |
| 577 | #endif |
| 578 | #ifdef FEAT_DIGRAPHS |
| 579 | do_digraph(-1); /* clear digraphs */ |
| 580 | #endif |
| 581 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 582 | /* |
| 583 | * Get the current length of the redo buffer, those characters have to be |
| 584 | * skipped if we want to get to the inserted characters. |
| 585 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 586 | ptr = get_inserted(); |
| 587 | if (ptr == NULL) |
| 588 | new_insert_skip = 0; |
| 589 | else |
| 590 | { |
| 591 | new_insert_skip = (int)STRLEN(ptr); |
| 592 | vim_free(ptr); |
| 593 | } |
| 594 | |
| 595 | old_indent = 0; |
| 596 | |
| 597 | /* |
| 598 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 599 | */ |
| 600 | for (;;) |
| 601 | { |
| 602 | #ifdef FEAT_RIGHTLEFT |
| 603 | if (!revins_legal) |
| 604 | revins_scol = -1; /* reset on illegal motions */ |
| 605 | else |
| 606 | revins_legal = 0; |
| 607 | #endif |
| 608 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 609 | count = 0; |
| 610 | |
| 611 | if (stop_insert_mode) |
| 612 | { |
| 613 | /* ":stopinsert" used or 'insertmode' reset */ |
| 614 | count = 0; |
| 615 | goto doESCkey; |
| 616 | } |
| 617 | |
| 618 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 619 | if (!arrow_used) |
| 620 | curwin->w_set_curswant = TRUE; |
| 621 | |
| 622 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 623 | * menu invoked a shell command). */ |
| 624 | if (stuff_empty()) |
| 625 | { |
| 626 | did_check_timestamps = FALSE; |
| 627 | if (need_check_timestamps) |
| 628 | check_timestamps(FALSE); |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * When emsg() was called msg_scroll will have been set. |
| 633 | */ |
| 634 | msg_scroll = FALSE; |
| 635 | |
| 636 | #ifdef FEAT_GUI |
| 637 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 638 | * another window. "need_mouse_correct" may then be set because of an |
| 639 | * autocommand. */ |
| 640 | if (need_mouse_correct) |
| 641 | gui_mouse_correct(); |
| 642 | #endif |
| 643 | |
| 644 | #ifdef FEAT_FOLDING |
| 645 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 646 | if (fdo_flags & FDO_INSERT) |
| 647 | foldOpenCursor(); |
| 648 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 649 | if (!char_avail()) |
| 650 | foldCheckClose(); |
| 651 | #endif |
| 652 | |
| 653 | /* |
| 654 | * If we inserted a character at the last position of the last line in |
| 655 | * the window, scroll the window one line up. This avoids an extra |
| 656 | * redraw. |
| 657 | * This is detected when the cursor column is smaller after inserting |
| 658 | * something. |
| 659 | * Don't do this when the topline changed already, it has |
| 660 | * already been adjusted (by insertchar() calling open_line())). |
| 661 | */ |
| 662 | if (curbuf->b_mod_set |
| 663 | && curwin->w_p_wrap |
| 664 | && !did_backspace |
| 665 | && curwin->w_topline == old_topline |
| 666 | #ifdef FEAT_DIFF |
| 667 | && curwin->w_topfill == old_topfill |
| 668 | #endif |
| 669 | ) |
| 670 | { |
| 671 | mincol = curwin->w_wcol; |
| 672 | validate_cursor_col(); |
| 673 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 674 | if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 675 | && curwin->w_wrow == W_WINROW(curwin) |
| 676 | + curwin->w_height - 1 - p_so |
| 677 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 678 | #ifdef FEAT_DIFF |
| 679 | || curwin->w_topfill > 0 |
| 680 | #endif |
| 681 | )) |
| 682 | { |
| 683 | #ifdef FEAT_DIFF |
| 684 | if (curwin->w_topfill > 0) |
| 685 | --curwin->w_topfill; |
| 686 | else |
| 687 | #endif |
| 688 | #ifdef FEAT_FOLDING |
| 689 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 690 | set_topline(curwin, old_topline + 1); |
| 691 | else |
| 692 | #endif |
| 693 | set_topline(curwin, curwin->w_topline + 1); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | /* May need to adjust w_topline to show the cursor. */ |
| 698 | update_topline(); |
| 699 | |
| 700 | did_backspace = FALSE; |
| 701 | |
| 702 | validate_cursor(); /* may set must_redraw */ |
| 703 | |
| 704 | /* |
| 705 | * Redraw the display when no characters are waiting. |
| 706 | * Also shows mode, ruler and positions cursor. |
| 707 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 708 | ins_redraw(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | |
| 710 | #ifdef FEAT_SCROLLBIND |
| 711 | if (curwin->w_p_scb) |
| 712 | do_check_scrollbind(TRUE); |
| 713 | #endif |
| 714 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 715 | #ifdef FEAT_CURSORBIND |
| 716 | if (curwin->w_p_crb) |
| 717 | do_check_cursorbind(); |
| 718 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 719 | update_curswant(); |
| 720 | old_topline = curwin->w_topline; |
| 721 | #ifdef FEAT_DIFF |
| 722 | old_topfill = curwin->w_topfill; |
| 723 | #endif |
| 724 | |
| 725 | #ifdef USE_ON_FLY_SCROLL |
| 726 | dont_scroll = FALSE; /* allow scrolling here */ |
| 727 | #endif |
| 728 | |
| 729 | /* |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 730 | * Get a character for Insert mode. Ignore K_IGNORE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 731 | */ |
| 732 | lastc = c; /* remember previous char for CTRL-D */ |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 733 | do |
| 734 | { |
| 735 | c = safe_vgetc(); |
| 736 | } while (c == K_IGNORE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 738 | #ifdef FEAT_AUTOCMD |
| 739 | /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */ |
| 740 | did_cursorhold = TRUE; |
| 741 | #endif |
| 742 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | #ifdef FEAT_RIGHTLEFT |
| 744 | if (p_hkmap && KeyTyped) |
| 745 | c = hkmap(c); /* Hebrew mode mapping */ |
| 746 | #endif |
| 747 | #ifdef FEAT_FKMAP |
| 748 | if (p_fkmap && KeyTyped) |
| 749 | c = fkmap(c); /* Farsi mode mapping */ |
| 750 | #endif |
| 751 | |
| 752 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 753 | /* |
| 754 | * Special handling of keys while the popup menu is visible or wanted |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 755 | * and the cursor is still in the completed word. Only when there is |
| 756 | * a match, skip this when no matches were found. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 757 | */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 758 | if (compl_started |
| 759 | && pum_wanted() |
| 760 | && curwin->w_cursor.col >= compl_col |
| 761 | && (compl_shown_match == NULL |
| 762 | || compl_shown_match != compl_shown_match->cp_next)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 763 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 764 | /* BS: Delete one character from "compl_leader". */ |
| 765 | if ((c == K_BS || c == Ctrl_H) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 766 | && curwin->w_cursor.col > compl_col |
| 767 | && (c = ins_compl_bs()) == NUL) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 768 | continue; |
| 769 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 770 | /* When no match was selected or it was edited. */ |
| 771 | if (!compl_used_match) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 772 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 773 | /* CTRL-L: Add one character from the current match to |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 774 | * "compl_leader". Except when at the original match and |
| 775 | * there is nothing to add, CTRL-L works like CTRL-P then. */ |
| 776 | if (c == Ctrl_L |
| 777 | && (ctrl_x_mode != CTRL_X_WHOLE_LINE |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 778 | || (int)STRLEN(compl_shown_match->cp_str) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 779 | > curwin->w_cursor.col - compl_col)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 780 | { |
| 781 | ins_compl_addfrommatch(); |
| 782 | continue; |
| 783 | } |
| 784 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 785 | /* A non-white character that fits in with the current |
| 786 | * completion: Add to "compl_leader". */ |
| 787 | if (ins_compl_accept_char(c)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 788 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 789 | #ifdef FEAT_AUTOCMD |
| 790 | /* Trigger InsertCharPre. */ |
| 791 | char_u *str = do_insert_char_pre(c); |
| 792 | char_u *p; |
| 793 | |
| 794 | if (str != NULL) |
| 795 | { |
| 796 | for (p = str; *p != NUL; mb_ptr_adv(p)) |
| 797 | ins_compl_addleader(PTR2CHAR(p)); |
| 798 | vim_free(str); |
| 799 | } |
| 800 | else |
| 801 | #endif |
| 802 | ins_compl_addleader(c); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 803 | continue; |
| 804 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 805 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 806 | /* Pressing CTRL-Y selects the current match. When |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 807 | * compl_enter_selects is set the Enter key does the same. */ |
| 808 | if (c == Ctrl_Y || (compl_enter_selects |
| 809 | && (c == CAR || c == K_KENTER || c == NL))) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 810 | { |
| 811 | ins_compl_delete(); |
| 812 | ins_compl_insert(); |
| 813 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 817 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 818 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 819 | compl_get_longest = FALSE; |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 820 | if (ins_compl_prep(c)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 821 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 822 | #endif |
| 823 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 824 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 825 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 826 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | if (c == Ctrl_BSL) |
| 828 | { |
| 829 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 830 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 831 | ++no_mapping; |
| 832 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 833 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 834 | --no_mapping; |
| 835 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 836 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 838 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 839 | vungetc(c); |
| 840 | c = Ctrl_BSL; |
| 841 | } |
| 842 | else if (c == Ctrl_G && p_im) |
| 843 | continue; |
| 844 | else |
| 845 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 846 | if (c == Ctrl_O) |
| 847 | { |
| 848 | ins_ctrl_o(); |
| 849 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 850 | nomove = TRUE; |
| 851 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | count = 0; |
| 853 | goto doESCkey; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | #ifdef FEAT_DIGRAPHS |
| 858 | c = do_digraph(c); |
| 859 | #endif |
| 860 | |
| 861 | #ifdef FEAT_INS_EXPAND |
| 862 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 863 | goto docomplete; |
| 864 | #endif |
| 865 | if (c == Ctrl_V || c == Ctrl_Q) |
| 866 | { |
| 867 | ins_ctrl_v(); |
| 868 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 869 | continue; |
| 870 | } |
| 871 | |
| 872 | #ifdef FEAT_CINDENT |
| 873 | if (cindent_on() |
| 874 | # ifdef FEAT_INS_EXPAND |
| 875 | && ctrl_x_mode == 0 |
| 876 | # endif |
| 877 | ) |
| 878 | { |
| 879 | /* A key name preceded by a bang means this key is not to be |
| 880 | * inserted. Skip ahead to the re-indenting below. |
| 881 | * A key name preceded by a star means that indenting has to be |
| 882 | * done before inserting the key. */ |
| 883 | line_is_white = inindent(0); |
| 884 | if (in_cinkeys(c, '!', line_is_white)) |
| 885 | goto force_cindent; |
| 886 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 887 | && stop_arrow() == OK) |
| 888 | do_c_expr_indent(); |
| 889 | } |
| 890 | #endif |
| 891 | |
| 892 | #ifdef FEAT_RIGHTLEFT |
| 893 | if (curwin->w_p_rl) |
| 894 | switch (c) |
| 895 | { |
| 896 | case K_LEFT: c = K_RIGHT; break; |
| 897 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 898 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 899 | case K_RIGHT: c = K_LEFT; break; |
| 900 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 901 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 902 | } |
| 903 | #endif |
| 904 | |
| 905 | #ifdef FEAT_VISUAL |
| 906 | /* |
| 907 | * If 'keymodel' contains "startsel", may start selection. If it |
| 908 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 909 | * characters. |
| 910 | */ |
| 911 | if (ins_start_select(c)) |
| 912 | continue; |
| 913 | #endif |
| 914 | |
| 915 | /* |
| 916 | * The big switch to handle a character in insert mode. |
| 917 | */ |
| 918 | switch (c) |
| 919 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 920 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 921 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 922 | break; |
| 923 | /*FALLTHROUGH*/ |
| 924 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 925 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 926 | #ifdef FEAT_CMDWIN |
| 927 | if (c == Ctrl_C && cmdwin_type != 0) |
| 928 | { |
| 929 | /* Close the cmdline window. */ |
| 930 | cmdwin_result = K_IGNORE; |
| 931 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 932 | nomove = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 933 | goto doESCkey; |
| 934 | } |
| 935 | #endif |
| 936 | |
| 937 | #ifdef UNIX |
| 938 | do_intr: |
| 939 | #endif |
| 940 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 941 | * Insert mode */ |
| 942 | if (goto_im()) |
| 943 | { |
| 944 | if (got_int) |
| 945 | { |
| 946 | (void)vgetc(); /* flush all buffers */ |
| 947 | got_int = FALSE; |
| 948 | } |
| 949 | else |
| 950 | vim_beep(); |
| 951 | break; |
| 952 | } |
| 953 | doESCkey: |
| 954 | /* |
| 955 | * This is the ONLY return from edit()! |
| 956 | */ |
| 957 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 958 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 959 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 960 | o_lnum = curwin->w_cursor.lnum; |
| 961 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 962 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 963 | { |
| 964 | #ifdef FEAT_AUTOCMD |
| 965 | if (cmdchar != 'r' && cmdchar != 'v') |
| 966 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 967 | FALSE, curbuf); |
Bram Moolenaar | c0a0ab5 | 2006-10-06 18:39:58 +0000 | [diff] [blame] | 968 | did_cursorhold = FALSE; |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 969 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 970 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 971 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 972 | continue; |
| 973 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 974 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 975 | if (!p_im) |
| 976 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 977 | stuffReadbuff((char_u *)":st\r"); |
| 978 | c = Ctrl_O; |
| 979 | /*FALLTHROUGH*/ |
| 980 | |
| 981 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 982 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 983 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 984 | goto docomplete; |
| 985 | #endif |
| 986 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 987 | break; |
| 988 | ins_ctrl_o(); |
Bram Moolenaar | 06a89a5 | 2006-04-29 22:01:03 +0000 | [diff] [blame] | 989 | |
| 990 | #ifdef FEAT_VIRTUALEDIT |
| 991 | /* don't move the cursor left when 'virtualedit' has "onemore". */ |
| 992 | if (ve_flags & VE_ONEMORE) |
| 993 | { |
| 994 | ins_at_eol = FALSE; |
| 995 | nomove = TRUE; |
| 996 | } |
| 997 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 998 | count = 0; |
| 999 | goto doESCkey; |
| 1000 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1001 | case K_INS: /* toggle insert/replace mode */ |
| 1002 | case K_KINS: |
| 1003 | ins_insert(replaceState); |
| 1004 | break; |
| 1005 | |
| 1006 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1007 | break; |
| 1008 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1009 | #ifdef FEAT_SNIFF |
| 1010 | case K_SNIFF: /* Sniff command received */ |
| 1011 | stuffcharReadbuff(K_SNIFF); |
| 1012 | goto doESCkey; |
| 1013 | #endif |
| 1014 | |
| 1015 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 1016 | case K_F1: |
| 1017 | case K_XF1: |
| 1018 | stuffcharReadbuff(K_HELP); |
| 1019 | if (p_im) |
| 1020 | need_start_insertmode = TRUE; |
| 1021 | goto doESCkey; |
| 1022 | |
| 1023 | #ifdef FEAT_NETBEANS_INTG |
| 1024 | case K_F21: /* NetBeans command */ |
| 1025 | ++no_mapping; /* don't map the next key hits */ |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1026 | i = plain_vgetc(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1027 | --no_mapping; |
| 1028 | netbeans_keycommand(i); |
| 1029 | break; |
| 1030 | #endif |
| 1031 | |
| 1032 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1033 | case NUL: |
| 1034 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1035 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 1036 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1037 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 1038 | && c != Ctrl_A && !p_im) |
| 1039 | goto doESCkey; /* quit insert mode */ |
| 1040 | inserted_space = FALSE; |
| 1041 | break; |
| 1042 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1043 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1044 | ins_reg(); |
| 1045 | auto_format(FALSE, TRUE); |
| 1046 | inserted_space = FALSE; |
| 1047 | break; |
| 1048 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1049 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1050 | ins_ctrl_g(); |
| 1051 | break; |
| 1052 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1053 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 1054 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | |
| 1057 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1058 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | if (!p_ari) |
| 1060 | goto normalchar; |
| 1061 | ins_ctrl_(); |
| 1062 | break; |
| 1063 | #endif |
| 1064 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1065 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1066 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1067 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 1068 | goto docomplete; |
| 1069 | #endif |
| 1070 | /* FALLTHROUGH */ |
| 1071 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1072 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1073 | # ifdef FEAT_INS_EXPAND |
| 1074 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 1075 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1076 | if (has_compl_option(FALSE)) |
| 1077 | goto docomplete; |
| 1078 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | } |
| 1080 | # endif |
| 1081 | ins_shift(c, lastc); |
| 1082 | auto_format(FALSE, TRUE); |
| 1083 | inserted_space = FALSE; |
| 1084 | break; |
| 1085 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1086 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1087 | case K_KDEL: |
| 1088 | ins_del(); |
| 1089 | auto_format(FALSE, TRUE); |
| 1090 | break; |
| 1091 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1092 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1093 | case Ctrl_H: |
| 1094 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 1095 | auto_format(FALSE, TRUE); |
| 1096 | break; |
| 1097 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1098 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1099 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 1100 | auto_format(FALSE, TRUE); |
| 1101 | break; |
| 1102 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1103 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1104 | # ifdef FEAT_COMPL_FUNC |
| 1105 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1106 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1107 | goto docomplete; |
| 1108 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1109 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 1110 | auto_format(FALSE, TRUE); |
| 1111 | inserted_space = FALSE; |
| 1112 | break; |
| 1113 | |
| 1114 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1115 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1116 | case K_LEFTMOUSE_NM: |
| 1117 | case K_LEFTDRAG: |
| 1118 | case K_LEFTRELEASE: |
| 1119 | case K_LEFTRELEASE_NM: |
| 1120 | case K_MIDDLEMOUSE: |
| 1121 | case K_MIDDLEDRAG: |
| 1122 | case K_MIDDLERELEASE: |
| 1123 | case K_RIGHTMOUSE: |
| 1124 | case K_RIGHTDRAG: |
| 1125 | case K_RIGHTRELEASE: |
| 1126 | case K_X1MOUSE: |
| 1127 | case K_X1DRAG: |
| 1128 | case K_X1RELEASE: |
| 1129 | case K_X2MOUSE: |
| 1130 | case K_X2DRAG: |
| 1131 | case K_X2RELEASE: |
| 1132 | ins_mouse(c); |
| 1133 | break; |
| 1134 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1135 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1136 | ins_mousescroll(MSCR_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1139 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1140 | ins_mousescroll(MSCR_UP); |
| 1141 | break; |
| 1142 | |
| 1143 | case K_MOUSELEFT: /* Scroll wheel left */ |
| 1144 | ins_mousescroll(MSCR_LEFT); |
| 1145 | break; |
| 1146 | |
| 1147 | case K_MOUSERIGHT: /* Scroll wheel right */ |
| 1148 | ins_mousescroll(MSCR_RIGHT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1149 | break; |
| 1150 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1151 | #ifdef FEAT_GUI_TABLINE |
| 1152 | case K_TABLINE: |
| 1153 | case K_TABMENU: |
| 1154 | ins_tabline(c); |
| 1155 | break; |
| 1156 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1157 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1158 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1161 | #ifdef FEAT_AUTOCMD |
| 1162 | case K_CURSORHOLD: /* Didn't type something for a while. */ |
| 1163 | apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); |
| 1164 | did_cursorhold = TRUE; |
| 1165 | break; |
| 1166 | #endif |
| 1167 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1168 | #ifdef FEAT_GUI_W32 |
| 1169 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1170 | * cancelled. */ |
| 1171 | case K_F4: |
| 1172 | if (mod_mask != MOD_MASK_ALT) |
| 1173 | goto normalchar; |
| 1174 | break; |
| 1175 | #endif |
| 1176 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1177 | #ifdef FEAT_GUI |
| 1178 | case K_VER_SCROLLBAR: |
| 1179 | ins_scroll(); |
| 1180 | break; |
| 1181 | |
| 1182 | case K_HOR_SCROLLBAR: |
| 1183 | ins_horscroll(); |
| 1184 | break; |
| 1185 | #endif |
| 1186 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1187 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1188 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | case K_S_HOME: |
| 1190 | case K_C_HOME: |
| 1191 | ins_home(c); |
| 1192 | break; |
| 1193 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1194 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1195 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1196 | case K_S_END: |
| 1197 | case K_C_END: |
| 1198 | ins_end(c); |
| 1199 | break; |
| 1200 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1201 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1202 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1203 | ins_s_left(); |
| 1204 | else |
| 1205 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | break; |
| 1207 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1208 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | case K_C_LEFT: |
| 1210 | ins_s_left(); |
| 1211 | break; |
| 1212 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1213 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1214 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1215 | ins_s_right(); |
| 1216 | else |
| 1217 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | break; |
| 1219 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1220 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1221 | case K_C_RIGHT: |
| 1222 | ins_s_right(); |
| 1223 | break; |
| 1224 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1225 | case K_UP: /* <Up> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1226 | #ifdef FEAT_INS_EXPAND |
| 1227 | if (pum_visible()) |
| 1228 | goto docomplete; |
| 1229 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1230 | if (mod_mask & MOD_MASK_SHIFT) |
| 1231 | ins_pageup(); |
| 1232 | else |
| 1233 | ins_up(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1234 | break; |
| 1235 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1236 | case K_S_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | case K_PAGEUP: |
| 1238 | case K_KPAGEUP: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1239 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1240 | if (pum_visible()) |
| 1241 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1242 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1243 | ins_pageup(); |
| 1244 | break; |
| 1245 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1246 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1247 | #ifdef FEAT_INS_EXPAND |
| 1248 | if (pum_visible()) |
| 1249 | goto docomplete; |
| 1250 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1251 | if (mod_mask & MOD_MASK_SHIFT) |
| 1252 | ins_pagedown(); |
| 1253 | else |
| 1254 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1255 | break; |
| 1256 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1257 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1258 | case K_PAGEDOWN: |
| 1259 | case K_KPAGEDOWN: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1260 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1261 | if (pum_visible()) |
| 1262 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1263 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | ins_pagedown(); |
| 1265 | break; |
| 1266 | |
| 1267 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1268 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1269 | ins_drop(); |
| 1270 | break; |
| 1271 | #endif |
| 1272 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1273 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1274 | c = TAB; |
| 1275 | /* FALLTHROUGH */ |
| 1276 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1277 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1279 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1280 | goto docomplete; |
| 1281 | #endif |
| 1282 | inserted_space = FALSE; |
| 1283 | if (ins_tab()) |
| 1284 | goto normalchar; /* insert TAB as a normal char */ |
| 1285 | auto_format(FALSE, TRUE); |
| 1286 | break; |
| 1287 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1288 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | c = CAR; |
| 1290 | /* FALLTHROUGH */ |
| 1291 | case CAR: |
| 1292 | case NL: |
| 1293 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1294 | /* In a quickfix window a <CR> jumps to the error under the |
| 1295 | * cursor. */ |
| 1296 | if (bt_quickfix(curbuf) && c == CAR) |
| 1297 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1298 | if (curwin->w_llist_ref == NULL) /* quickfix window */ |
| 1299 | do_cmdline_cmd((char_u *)".cc"); |
| 1300 | else /* location list window */ |
| 1301 | do_cmdline_cmd((char_u *)".ll"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1302 | break; |
| 1303 | } |
| 1304 | #endif |
| 1305 | #ifdef FEAT_CMDWIN |
| 1306 | if (cmdwin_type != 0) |
| 1307 | { |
| 1308 | /* Execute the command in the cmdline window. */ |
| 1309 | cmdwin_result = CAR; |
| 1310 | goto doESCkey; |
| 1311 | } |
| 1312 | #endif |
| 1313 | if (ins_eol(c) && !p_im) |
| 1314 | goto doESCkey; /* out of memory */ |
| 1315 | auto_format(FALSE, FALSE); |
| 1316 | inserted_space = FALSE; |
| 1317 | break; |
| 1318 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 1319 | #if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1320 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | # ifdef FEAT_INS_EXPAND |
| 1322 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1323 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1324 | if (has_compl_option(TRUE)) |
| 1325 | goto docomplete; |
| 1326 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1327 | } |
| 1328 | # endif |
| 1329 | # ifdef FEAT_DIGRAPHS |
| 1330 | c = ins_digraph(); |
| 1331 | if (c == NUL) |
| 1332 | break; |
| 1333 | # endif |
| 1334 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1335 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1336 | |
| 1337 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1338 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1339 | ins_ctrl_x(); |
| 1340 | break; |
| 1341 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1342 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1343 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1344 | goto normalchar; |
| 1345 | goto docomplete; |
| 1346 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1347 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1348 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1349 | goto normalchar; |
| 1350 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1351 | |
| 1352 | case 's': /* Spelling completion after ^X */ |
| 1353 | case Ctrl_S: |
| 1354 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1355 | goto normalchar; |
| 1356 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1357 | #endif |
| 1358 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1359 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1360 | #ifdef FEAT_INS_EXPAND |
| 1361 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1362 | #endif |
| 1363 | { |
| 1364 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1365 | if (p_im) |
| 1366 | { |
| 1367 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1368 | break; |
| 1369 | goto doESCkey; |
| 1370 | } |
| 1371 | goto normalchar; |
| 1372 | } |
| 1373 | #ifdef FEAT_INS_EXPAND |
| 1374 | /* FALLTHROUGH */ |
| 1375 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1376 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | case Ctrl_N: |
| 1378 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1379 | * but it is under other ^X modes */ |
| 1380 | if (*curbuf->b_p_cpt == NUL |
| 1381 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1382 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1383 | goto normalchar; |
| 1384 | |
| 1385 | docomplete: |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 1386 | compl_busy = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1388 | compl_cont_status = 0; |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 1389 | compl_busy = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | #endif /* FEAT_INS_EXPAND */ |
| 1392 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1393 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1394 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1395 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | |
| 1398 | default: |
| 1399 | #ifdef UNIX |
| 1400 | if (c == intr_char) /* special interrupt char */ |
| 1401 | goto do_intr; |
| 1402 | #endif |
| 1403 | |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1404 | normalchar: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1405 | /* |
| 1406 | * Insert a nomal character. |
| 1407 | */ |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1408 | #ifdef FEAT_AUTOCMD |
| 1409 | if (!p_paste) |
| 1410 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1411 | /* Trigger InsertCharPre. */ |
| 1412 | char_u *str = do_insert_char_pre(c); |
| 1413 | char_u *p; |
| 1414 | |
| 1415 | if (str != NULL) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1416 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1417 | if (*str != NUL && stop_arrow() != FAIL) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1418 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1419 | /* Insert the new value of v:char literally. */ |
| 1420 | for (p = str; *p != NUL; mb_ptr_adv(p)) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1421 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1422 | c = PTR2CHAR(p); |
| 1423 | if (c == CAR || c == K_KENTER || c == NL) |
| 1424 | ins_eol(c); |
| 1425 | else |
| 1426 | ins_char(c); |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1427 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1428 | AppendToRedobuffLit(str, -1); |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1429 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1430 | vim_free(str); |
| 1431 | c = NUL; |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1434 | /* If the new value is already inserted or an empty string |
| 1435 | * then don't insert any character. */ |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1436 | if (c == NUL) |
| 1437 | break; |
| 1438 | } |
| 1439 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1440 | #ifdef FEAT_SMARTINDENT |
| 1441 | /* Try to perform smart-indenting. */ |
| 1442 | ins_try_si(c); |
| 1443 | #endif |
| 1444 | |
| 1445 | if (c == ' ') |
| 1446 | { |
| 1447 | inserted_space = TRUE; |
| 1448 | #ifdef FEAT_CINDENT |
| 1449 | if (inindent(0)) |
| 1450 | can_cindent = FALSE; |
| 1451 | #endif |
| 1452 | if (Insstart_blank_vcol == MAXCOL |
| 1453 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1454 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1455 | } |
| 1456 | |
Bram Moolenaar | e0ebfd7 | 2012-04-05 16:07:06 +0200 | [diff] [blame] | 1457 | /* Insert a normal character and check for abbreviations on a |
| 1458 | * special character. Let CTRL-] expand abbreviations without |
| 1459 | * inserting it. */ |
| 1460 | if (vim_iswordc(c) || (!echeck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1461 | #ifdef FEAT_MBYTE |
| 1462 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1463 | * what check_abbr() expects. */ |
| 1464 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1465 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1466 | c) && c != Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1467 | { |
| 1468 | insert_special(c, FALSE, FALSE); |
| 1469 | #ifdef FEAT_RIGHTLEFT |
| 1470 | revins_legal++; |
| 1471 | revins_chars++; |
| 1472 | #endif |
| 1473 | } |
| 1474 | |
| 1475 | auto_format(FALSE, TRUE); |
| 1476 | |
| 1477 | #ifdef FEAT_FOLDING |
| 1478 | /* When inserting a character the cursor line must never be in a |
| 1479 | * closed fold. */ |
| 1480 | foldOpenCursor(); |
| 1481 | #endif |
| 1482 | break; |
| 1483 | } /* end of switch (c) */ |
| 1484 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 1485 | #ifdef FEAT_AUTOCMD |
| 1486 | /* If typed something may trigger CursorHoldI again. */ |
| 1487 | if (c != K_CURSORHOLD) |
| 1488 | did_cursorhold = FALSE; |
| 1489 | #endif |
| 1490 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | /* If the cursor was moved we didn't just insert a space */ |
| 1492 | if (arrow_used) |
| 1493 | inserted_space = FALSE; |
| 1494 | |
| 1495 | #ifdef FEAT_CINDENT |
| 1496 | if (can_cindent && cindent_on() |
| 1497 | # ifdef FEAT_INS_EXPAND |
| 1498 | && ctrl_x_mode == 0 |
| 1499 | # endif |
| 1500 | ) |
| 1501 | { |
| 1502 | force_cindent: |
| 1503 | /* |
| 1504 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1505 | */ |
| 1506 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1507 | { |
| 1508 | if (stop_arrow() == OK) |
| 1509 | /* re-indent the current line */ |
| 1510 | do_c_expr_indent(); |
| 1511 | } |
| 1512 | } |
| 1513 | #endif /* FEAT_CINDENT */ |
| 1514 | |
| 1515 | } /* for (;;) */ |
| 1516 | /* NOTREACHED */ |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * Redraw for Insert mode. |
| 1521 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1522 | * option work correctly. |
| 1523 | * Only redraw when there are no characters available. This speeds up |
| 1524 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1525 | */ |
| 1526 | static void |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1527 | ins_redraw(ready) |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 1528 | int ready UNUSED; /* not busy with something */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1529 | { |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1530 | #ifdef FEAT_CONCEAL |
| 1531 | linenr_T conceal_old_cursor_line = 0; |
| 1532 | linenr_T conceal_new_cursor_line = 0; |
| 1533 | int conceal_update_lines = FALSE; |
| 1534 | #endif |
| 1535 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1536 | if (!char_avail()) |
| 1537 | { |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1538 | #if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL) |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1539 | /* Trigger CursorMoved if the cursor moved. Not when the popup menu is |
| 1540 | * visible, the command might delete it. */ |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1541 | if (ready && ( |
| 1542 | # ifdef FEAT_AUTOCMD |
| 1543 | has_cursormovedI() |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1544 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1545 | # if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL) |
| 1546 | || |
| 1547 | # endif |
| 1548 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1549 | curwin->w_p_cole > 0 |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1550 | # endif |
| 1551 | ) |
| 1552 | && !equalpos(last_cursormoved, curwin->w_cursor) |
| 1553 | # ifdef FEAT_INS_EXPAND |
| 1554 | && !pum_visible() |
| 1555 | # endif |
| 1556 | ) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1557 | { |
Bram Moolenaar | e77c760 | 2008-01-12 17:13:50 +0000 | [diff] [blame] | 1558 | # ifdef FEAT_SYN_HL |
| 1559 | /* Need to update the screen first, to make sure syntax |
| 1560 | * highlighting is correct after making a change (e.g., inserting |
| 1561 | * a "(". The autocommand may also require a redraw, so it's done |
| 1562 | * again below, unfortunately. */ |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 1563 | if (syntax_present(curwin) && must_redraw) |
Bram Moolenaar | e77c760 | 2008-01-12 17:13:50 +0000 | [diff] [blame] | 1564 | update_screen(0); |
| 1565 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1566 | # ifdef FEAT_AUTOCMD |
| 1567 | if (has_cursormovedI()) |
| 1568 | apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); |
| 1569 | # endif |
| 1570 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1571 | if (curwin->w_p_cole > 0) |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1572 | { |
| 1573 | conceal_old_cursor_line = last_cursormoved.lnum; |
| 1574 | conceal_new_cursor_line = curwin->w_cursor.lnum; |
| 1575 | conceal_update_lines = TRUE; |
| 1576 | } |
| 1577 | # endif |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1578 | last_cursormoved = curwin->w_cursor; |
| 1579 | } |
| 1580 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1581 | if (must_redraw) |
| 1582 | update_screen(0); |
| 1583 | else if (clear_cmdline || redraw_cmdline) |
| 1584 | showmode(); /* clear cmdline and show mode */ |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1585 | # if defined(FEAT_CONCEAL) |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1586 | if ((conceal_update_lines |
| 1587 | && (conceal_old_cursor_line != conceal_new_cursor_line |
| 1588 | || conceal_cursor_line(curwin))) |
| 1589 | || need_cursor_line_redraw) |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1590 | { |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1591 | if (conceal_old_cursor_line != conceal_new_cursor_line) |
| 1592 | update_single_line(curwin, conceal_old_cursor_line); |
| 1593 | update_single_line(curwin, conceal_new_cursor_line == 0 |
| 1594 | ? curwin->w_cursor.lnum : conceal_new_cursor_line); |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1595 | curwin->w_valid &= ~VALID_CROW; |
| 1596 | } |
| 1597 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1598 | showruler(FALSE); |
| 1599 | setcursor(); |
| 1600 | emsg_on_display = FALSE; /* may remove error message now */ |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | /* |
| 1605 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1606 | */ |
| 1607 | static void |
| 1608 | ins_ctrl_v() |
| 1609 | { |
| 1610 | int c; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1611 | int did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1612 | |
| 1613 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1614 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (redrawing() && !char_avail()) |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1617 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1618 | edit_putchar('^', TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1619 | did_putchar = TRUE; |
| 1620 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1621 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1622 | |
| 1623 | #ifdef FEAT_CMDL_INFO |
| 1624 | add_to_showcmd_c(Ctrl_V); |
| 1625 | #endif |
| 1626 | |
| 1627 | c = get_literal(); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1628 | if (did_putchar) |
| 1629 | /* when the line fits in 'columns' the '^' is at the start of the next |
| 1630 | * line and will not removed by the redraw */ |
| 1631 | edit_unputchar(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1632 | #ifdef FEAT_CMDL_INFO |
| 1633 | clear_showcmd(); |
| 1634 | #endif |
| 1635 | insert_special(c, FALSE, TRUE); |
| 1636 | #ifdef FEAT_RIGHTLEFT |
| 1637 | revins_chars++; |
| 1638 | revins_legal++; |
| 1639 | #endif |
| 1640 | } |
| 1641 | |
| 1642 | /* |
| 1643 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1644 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1645 | */ |
| 1646 | static int pc_status; |
| 1647 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1648 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1649 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1650 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1651 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1652 | static int pc_attr; |
| 1653 | static int pc_row; |
| 1654 | static int pc_col; |
| 1655 | |
| 1656 | void |
| 1657 | edit_putchar(c, highlight) |
| 1658 | int c; |
| 1659 | int highlight; |
| 1660 | { |
| 1661 | int attr; |
| 1662 | |
| 1663 | if (ScreenLines != NULL) |
| 1664 | { |
| 1665 | update_topline(); /* just in case w_topline isn't valid */ |
| 1666 | validate_cursor(); |
| 1667 | if (highlight) |
| 1668 | attr = hl_attr(HLF_8); |
| 1669 | else |
| 1670 | attr = 0; |
| 1671 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1672 | pc_col = W_WINCOL(curwin); |
| 1673 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1674 | pc_status = PC_STATUS_UNSET; |
| 1675 | #endif |
| 1676 | #ifdef FEAT_RIGHTLEFT |
| 1677 | if (curwin->w_p_rl) |
| 1678 | { |
| 1679 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1680 | # ifdef FEAT_MBYTE |
| 1681 | if (has_mbyte) |
| 1682 | { |
| 1683 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1684 | |
| 1685 | if (fix_col != pc_col) |
| 1686 | { |
| 1687 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1688 | --curwin->w_wcol; |
| 1689 | pc_status = PC_STATUS_RIGHT; |
| 1690 | } |
| 1691 | } |
| 1692 | # endif |
| 1693 | } |
| 1694 | else |
| 1695 | #endif |
| 1696 | { |
| 1697 | pc_col += curwin->w_wcol; |
| 1698 | #ifdef FEAT_MBYTE |
| 1699 | if (mb_lefthalve(pc_row, pc_col)) |
| 1700 | pc_status = PC_STATUS_LEFT; |
| 1701 | #endif |
| 1702 | } |
| 1703 | |
| 1704 | /* save the character to be able to put it back */ |
| 1705 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1706 | if (pc_status == PC_STATUS_UNSET) |
| 1707 | #endif |
| 1708 | { |
| 1709 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1710 | pc_status = PC_STATUS_SET; |
| 1711 | } |
| 1712 | screen_putchar(c, pc_row, pc_col, attr); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | /* |
| 1717 | * Undo the previous edit_putchar(). |
| 1718 | */ |
| 1719 | void |
| 1720 | edit_unputchar() |
| 1721 | { |
| 1722 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1723 | { |
| 1724 | #if defined(FEAT_MBYTE) |
| 1725 | if (pc_status == PC_STATUS_RIGHT) |
| 1726 | ++curwin->w_wcol; |
| 1727 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1728 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1729 | else |
| 1730 | #endif |
| 1731 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1732 | } |
| 1733 | } |
| 1734 | |
| 1735 | /* |
| 1736 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1737 | * Only works when cursor is in the line that changes. |
| 1738 | */ |
| 1739 | void |
| 1740 | display_dollar(col) |
| 1741 | colnr_T col; |
| 1742 | { |
| 1743 | colnr_T save_col; |
| 1744 | |
| 1745 | if (!redrawing()) |
| 1746 | return; |
| 1747 | |
| 1748 | cursor_off(); |
| 1749 | save_col = curwin->w_cursor.col; |
| 1750 | curwin->w_cursor.col = col; |
| 1751 | #ifdef FEAT_MBYTE |
| 1752 | if (has_mbyte) |
| 1753 | { |
| 1754 | char_u *p; |
| 1755 | |
| 1756 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1757 | p = ml_get_curline(); |
| 1758 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1759 | } |
| 1760 | #endif |
| 1761 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1762 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1763 | { |
| 1764 | edit_putchar('$', FALSE); |
| 1765 | dollar_vcol = curwin->w_virtcol; |
| 1766 | } |
| 1767 | curwin->w_cursor.col = save_col; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | * Call this function before moving the cursor from the normal insert position |
| 1772 | * in insert mode. |
| 1773 | */ |
| 1774 | static void |
| 1775 | undisplay_dollar() |
| 1776 | { |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 1777 | if (dollar_vcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1778 | { |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 1779 | dollar_vcol = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1780 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1786 | * Keep the cursor on the same character. |
| 1787 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1788 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1789 | * type == INDENT_SET set indent to "amount" |
| 1790 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1791 | */ |
| 1792 | void |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1793 | change_indent(type, amount, round, replaced, call_changed_bytes) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1794 | int type; |
| 1795 | int amount; |
| 1796 | int round; |
| 1797 | int replaced; /* replaced character, put on replace stack */ |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1798 | int call_changed_bytes; /* call changed_bytes() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1799 | { |
| 1800 | int vcol; |
| 1801 | int last_vcol; |
| 1802 | int insstart_less; /* reduction for Insstart.col */ |
| 1803 | int new_cursor_col; |
| 1804 | int i; |
| 1805 | char_u *ptr; |
| 1806 | int save_p_list; |
| 1807 | int start_col; |
| 1808 | colnr_T vc; |
| 1809 | #ifdef FEAT_VREPLACE |
| 1810 | colnr_T orig_col = 0; /* init for GCC */ |
| 1811 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1812 | |
| 1813 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1814 | if (State & VREPLACE_FLAG) |
| 1815 | { |
| 1816 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1817 | orig_col = curwin->w_cursor.col; |
| 1818 | } |
| 1819 | #endif |
| 1820 | |
| 1821 | /* for the following tricks we don't want list mode */ |
| 1822 | save_p_list = curwin->w_p_list; |
| 1823 | curwin->w_p_list = FALSE; |
| 1824 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1825 | vcol = vc; |
| 1826 | |
| 1827 | /* |
| 1828 | * For Replace mode we need to fix the replace stack later, which is only |
| 1829 | * possible when the cursor is in the indent. Remember the number of |
| 1830 | * characters before the cursor if it's possible. |
| 1831 | */ |
| 1832 | start_col = curwin->w_cursor.col; |
| 1833 | |
| 1834 | /* determine offset from first non-blank */ |
| 1835 | new_cursor_col = curwin->w_cursor.col; |
| 1836 | beginline(BL_WHITE); |
| 1837 | new_cursor_col -= curwin->w_cursor.col; |
| 1838 | |
| 1839 | insstart_less = curwin->w_cursor.col; |
| 1840 | |
| 1841 | /* |
| 1842 | * If the cursor is in the indent, compute how many screen columns the |
| 1843 | * cursor is to the left of the first non-blank. |
| 1844 | */ |
| 1845 | if (new_cursor_col < 0) |
| 1846 | vcol = get_indent() - vcol; |
| 1847 | |
| 1848 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1849 | start_col = -1; |
| 1850 | |
| 1851 | /* |
| 1852 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1853 | */ |
| 1854 | if (type == INDENT_SET) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1855 | (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | else |
| 1857 | { |
| 1858 | #ifdef FEAT_VREPLACE |
| 1859 | int save_State = State; |
| 1860 | |
| 1861 | /* Avoid being called recursively. */ |
| 1862 | if (State & VREPLACE_FLAG) |
| 1863 | State = INSERT; |
| 1864 | #endif |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1865 | shift_line(type == INDENT_DEC, round, 1, call_changed_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1866 | #ifdef FEAT_VREPLACE |
| 1867 | State = save_State; |
| 1868 | #endif |
| 1869 | } |
| 1870 | insstart_less -= curwin->w_cursor.col; |
| 1871 | |
| 1872 | /* |
| 1873 | * Try to put cursor on same character. |
| 1874 | * If the cursor is at or after the first non-blank in the line, |
| 1875 | * compute the cursor column relative to the column of the first |
| 1876 | * non-blank character. |
| 1877 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1878 | * If the cursor is before the first non-blank, position it relative |
| 1879 | * to the first non-blank, counted in screen columns. |
| 1880 | */ |
| 1881 | if (new_cursor_col >= 0) |
| 1882 | { |
| 1883 | /* |
| 1884 | * When changing the indent while the cursor is touching it, reset |
| 1885 | * Insstart_col to 0. |
| 1886 | */ |
| 1887 | if (new_cursor_col == 0) |
| 1888 | insstart_less = MAXCOL; |
| 1889 | new_cursor_col += curwin->w_cursor.col; |
| 1890 | } |
| 1891 | else if (!(State & INSERT)) |
| 1892 | new_cursor_col = curwin->w_cursor.col; |
| 1893 | else |
| 1894 | { |
| 1895 | /* |
| 1896 | * Compute the screen column where the cursor should be. |
| 1897 | */ |
| 1898 | vcol = get_indent() - vcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1899 | curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1900 | |
| 1901 | /* |
| 1902 | * Advance the cursor until we reach the right screen column. |
| 1903 | */ |
| 1904 | vcol = last_vcol = 0; |
| 1905 | new_cursor_col = -1; |
| 1906 | ptr = ml_get_curline(); |
| 1907 | while (vcol <= (int)curwin->w_virtcol) |
| 1908 | { |
| 1909 | last_vcol = vcol; |
| 1910 | #ifdef FEAT_MBYTE |
| 1911 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1912 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1913 | else |
| 1914 | #endif |
| 1915 | ++new_cursor_col; |
| 1916 | vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol); |
| 1917 | } |
| 1918 | vcol = last_vcol; |
| 1919 | |
| 1920 | /* |
| 1921 | * May need to insert spaces to be able to position the cursor on |
| 1922 | * the right screen column. |
| 1923 | */ |
| 1924 | if (vcol != (int)curwin->w_virtcol) |
| 1925 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1926 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1927 | i = (int)curwin->w_virtcol - vcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1928 | ptr = alloc((unsigned)(i + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1929 | if (ptr != NULL) |
| 1930 | { |
| 1931 | new_cursor_col += i; |
| 1932 | ptr[i] = NUL; |
| 1933 | while (--i >= 0) |
| 1934 | ptr[i] = ' '; |
| 1935 | ins_str(ptr); |
| 1936 | vim_free(ptr); |
| 1937 | } |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * When changing the indent while the cursor is in it, reset |
| 1942 | * Insstart_col to 0. |
| 1943 | */ |
| 1944 | insstart_less = MAXCOL; |
| 1945 | } |
| 1946 | |
| 1947 | curwin->w_p_list = save_p_list; |
| 1948 | |
| 1949 | if (new_cursor_col <= 0) |
| 1950 | curwin->w_cursor.col = 0; |
| 1951 | else |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1952 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1953 | curwin->w_set_curswant = TRUE; |
| 1954 | changed_cline_bef_curs(); |
| 1955 | |
| 1956 | /* |
| 1957 | * May have to adjust the start of the insert. |
| 1958 | */ |
| 1959 | if (State & INSERT) |
| 1960 | { |
| 1961 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1962 | { |
| 1963 | if ((int)Insstart.col <= insstart_less) |
| 1964 | Insstart.col = 0; |
| 1965 | else |
| 1966 | Insstart.col -= insstart_less; |
| 1967 | } |
| 1968 | if ((int)ai_col <= insstart_less) |
| 1969 | ai_col = 0; |
| 1970 | else |
| 1971 | ai_col -= insstart_less; |
| 1972 | } |
| 1973 | |
| 1974 | /* |
| 1975 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 1976 | * If the number of characters before the cursor decreased, need to pop a |
| 1977 | * few characters from the replace stack. |
| 1978 | * If the number of characters before the cursor increased, need to push a |
| 1979 | * few NULs onto the replace stack. |
| 1980 | */ |
| 1981 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1982 | { |
| 1983 | while (start_col > (int)curwin->w_cursor.col) |
| 1984 | { |
| 1985 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1986 | --start_col; |
| 1987 | } |
| 1988 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1989 | { |
| 1990 | replace_push(NUL); |
| 1991 | if (replaced) |
| 1992 | { |
| 1993 | replace_push(replaced); |
| 1994 | replaced = NUL; |
| 1995 | } |
| 1996 | ++start_col; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | #ifdef FEAT_VREPLACE |
| 2001 | /* |
| 2002 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 2003 | * it is always possible because we backspace over the whole line and then |
| 2004 | * put it back again the way we wanted it. |
| 2005 | */ |
| 2006 | if (State & VREPLACE_FLAG) |
| 2007 | { |
| 2008 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 2009 | * even if you can't backspace. */ |
| 2010 | if (orig_line == NULL) |
| 2011 | return; |
| 2012 | |
| 2013 | /* Save new line */ |
| 2014 | new_line = vim_strsave(ml_get_curline()); |
| 2015 | if (new_line == NULL) |
| 2016 | return; |
| 2017 | |
| 2018 | /* We only put back the new line up to the cursor */ |
| 2019 | new_line[curwin->w_cursor.col] = NUL; |
| 2020 | |
| 2021 | /* Put back original line */ |
| 2022 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 2023 | curwin->w_cursor.col = orig_col; |
| 2024 | |
| 2025 | /* Backspace from cursor to start of line */ |
| 2026 | backspace_until_column(0); |
| 2027 | |
| 2028 | /* Insert new stuff into line again */ |
| 2029 | ins_bytes(new_line); |
| 2030 | |
| 2031 | vim_free(new_line); |
| 2032 | } |
| 2033 | #endif |
| 2034 | } |
| 2035 | |
| 2036 | /* |
| 2037 | * Truncate the space at the end of a line. This is to be used only in an |
| 2038 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 2039 | * modes. |
| 2040 | */ |
| 2041 | void |
| 2042 | truncate_spaces(line) |
| 2043 | char_u *line; |
| 2044 | { |
| 2045 | int i; |
| 2046 | |
| 2047 | /* find start of trailing white space */ |
| 2048 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 2049 | { |
| 2050 | if (State & REPLACE_FLAG) |
| 2051 | replace_join(0); /* remove a NUL from the replace stack */ |
| 2052 | } |
| 2053 | line[i + 1] = NUL; |
| 2054 | } |
| 2055 | |
| 2056 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 2057 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 2058 | /* |
| 2059 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 2060 | * modes correctly. May also be used when not in insert mode at all. |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2061 | * Will attempt not to go before "col" even when there is a composing |
| 2062 | * character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | */ |
| 2064 | void |
| 2065 | backspace_until_column(col) |
| 2066 | int col; |
| 2067 | { |
| 2068 | while ((int)curwin->w_cursor.col > col) |
| 2069 | { |
| 2070 | curwin->w_cursor.col--; |
| 2071 | if (State & REPLACE_FLAG) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2072 | replace_do_bs(col); |
| 2073 | else if (!del_char_after_col(col)) |
| 2074 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2075 | } |
| 2076 | } |
| 2077 | #endif |
| 2078 | |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2079 | /* |
| 2080 | * Like del_char(), but make sure not to go before column "limit_col". |
| 2081 | * Only matters when there are composing characters. |
| 2082 | * Return TRUE when something was deleted. |
| 2083 | */ |
| 2084 | static int |
| 2085 | del_char_after_col(limit_col) |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 2086 | int limit_col UNUSED; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2087 | { |
| 2088 | #ifdef FEAT_MBYTE |
| 2089 | if (enc_utf8 && limit_col >= 0) |
| 2090 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2091 | colnr_T ecol = curwin->w_cursor.col + 1; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2092 | |
| 2093 | /* Make sure the cursor is at the start of a character, but |
| 2094 | * skip forward again when going too far back because of a |
| 2095 | * composing character. */ |
| 2096 | mb_adjust_cursor(); |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 2097 | while (curwin->w_cursor.col < (colnr_T)limit_col) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2098 | { |
| 2099 | int l = utf_ptr2len(ml_get_cursor()); |
| 2100 | |
| 2101 | if (l == 0) /* end of line */ |
| 2102 | break; |
| 2103 | curwin->w_cursor.col += l; |
| 2104 | } |
| 2105 | if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol) |
| 2106 | return FALSE; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2107 | del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2108 | } |
| 2109 | else |
| 2110 | #endif |
| 2111 | (void)del_char(FALSE); |
| 2112 | return TRUE; |
| 2113 | } |
| 2114 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 2116 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2117 | * CTRL-X pressed in Insert mode. |
| 2118 | */ |
| 2119 | static void |
| 2120 | ins_ctrl_x() |
| 2121 | { |
| 2122 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 2123 | * CTRL-V works like CTRL-N */ |
| 2124 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 2125 | { |
| 2126 | /* if the next ^X<> won't ADD nothing, then reset |
| 2127 | * compl_cont_status */ |
| 2128 | if (compl_cont_status & CONT_N_ADDS) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2129 | compl_cont_status |= CONT_INTRPT; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2130 | else |
| 2131 | compl_cont_status = 0; |
| 2132 | /* We're not sure which CTRL-X mode it will be yet */ |
| 2133 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2134 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 2135 | edit_submode_pre = NULL; |
| 2136 | showmode(); |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | /* |
| 2141 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 2142 | */ |
| 2143 | static int |
| 2144 | has_compl_option(dict_opt) |
| 2145 | int dict_opt; |
| 2146 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2147 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2148 | # ifdef FEAT_SPELL |
| 2149 | && !curwin->w_p_spell |
| 2150 | # endif |
| 2151 | ) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2152 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 2153 | { |
| 2154 | ctrl_x_mode = 0; |
| 2155 | edit_submode = NULL; |
| 2156 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 2157 | : (char_u *)_("'thesaurus' option is empty"), |
| 2158 | hl_attr(HLF_E)); |
| 2159 | if (emsg_silent == 0) |
| 2160 | { |
| 2161 | vim_beep(); |
| 2162 | setcursor(); |
| 2163 | out_flush(); |
| 2164 | ui_delay(2000L, FALSE); |
| 2165 | } |
| 2166 | return FALSE; |
| 2167 | } |
| 2168 | return TRUE; |
| 2169 | } |
| 2170 | |
| 2171 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2172 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 2173 | * This depends on the current mode. |
| 2174 | */ |
| 2175 | int |
| 2176 | vim_is_ctrl_x_key(c) |
| 2177 | int c; |
| 2178 | { |
| 2179 | /* Always allow ^R - let it's results then be checked */ |
| 2180 | if (c == Ctrl_R) |
| 2181 | return TRUE; |
| 2182 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2183 | /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 2184 | if (ins_compl_pum_key(c)) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2185 | return TRUE; |
| 2186 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2187 | switch (ctrl_x_mode) |
| 2188 | { |
| 2189 | case 0: /* Not in any CTRL-X mode */ |
| 2190 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 2191 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2192 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2193 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 2194 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 2195 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2196 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
Bram Moolenaar | bbd9fd7 | 2011-12-23 13:15:03 +0100 | [diff] [blame] | 2197 | || c == Ctrl_S || c == Ctrl_K || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2198 | case CTRL_X_SCROLL: |
| 2199 | return (c == Ctrl_Y || c == Ctrl_E); |
| 2200 | case CTRL_X_WHOLE_LINE: |
| 2201 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 2202 | case CTRL_X_FILES: |
| 2203 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 2204 | case CTRL_X_DICTIONARY: |
| 2205 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 2206 | case CTRL_X_THESAURUS: |
| 2207 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 2208 | case CTRL_X_TAGS: |
| 2209 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 2210 | #ifdef FEAT_FIND_ID |
| 2211 | case CTRL_X_PATH_PATTERNS: |
| 2212 | return (c == Ctrl_P || c == Ctrl_N); |
| 2213 | case CTRL_X_PATH_DEFINES: |
| 2214 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 2215 | #endif |
| 2216 | case CTRL_X_CMDLINE: |
| 2217 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 2218 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2219 | #ifdef FEAT_COMPL_FUNC |
| 2220 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2221 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2222 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2223 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2224 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2225 | case CTRL_X_SPELL: |
| 2226 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2227 | } |
| 2228 | EMSG(_(e_internal)); |
| 2229 | return FALSE; |
| 2230 | } |
| 2231 | |
| 2232 | /* |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 2233 | * Return TRUE when character "c" is part of the item currently being |
| 2234 | * completed. Used to decide whether to abandon complete mode when the menu |
| 2235 | * is visible. |
| 2236 | */ |
| 2237 | static int |
| 2238 | ins_compl_accept_char(c) |
| 2239 | int c; |
| 2240 | { |
| 2241 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 2242 | /* When expanding an identifier only accept identifier chars. */ |
| 2243 | return vim_isIDc(c); |
| 2244 | |
| 2245 | switch (ctrl_x_mode) |
| 2246 | { |
| 2247 | case CTRL_X_FILES: |
| 2248 | /* When expanding file name only accept file name chars. But not |
| 2249 | * path separators, so that "proto/<Tab>" expands files in |
| 2250 | * "proto", not "proto/" as a whole */ |
| 2251 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 2252 | |
| 2253 | case CTRL_X_CMDLINE: |
| 2254 | case CTRL_X_OMNI: |
| 2255 | /* Command line and Omni completion can work with just about any |
| 2256 | * printable character, but do stop at white space. */ |
| 2257 | return vim_isprintc(c) && !vim_iswhite(c); |
| 2258 | |
| 2259 | case CTRL_X_WHOLE_LINE: |
| 2260 | /* For while line completion a space can be part of the line. */ |
| 2261 | return vim_isprintc(c); |
| 2262 | } |
| 2263 | return vim_iswordc(c); |
| 2264 | } |
| 2265 | |
| 2266 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2267 | * 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] | 2268 | * case of the originally typed text is used, and the case of the completed |
Bram Moolenaar | 34e0bfa | 2007-05-10 18:44:18 +0000 | [diff] [blame] | 2269 | * text is inferred, ie this tries to work out what case you probably wanted |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | * the rest of the word to be in -- webb |
| 2271 | */ |
| 2272 | int |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2273 | ins_compl_add_infercase(str, len, icase, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2274 | char_u *str; |
| 2275 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2276 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | char_u *fname; |
| 2278 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2279 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2280 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2281 | char_u *p; |
| 2282 | int i, c; |
| 2283 | int actual_len; /* Take multi-byte characters */ |
| 2284 | int actual_compl_length; /* into account. */ |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2285 | int min_len; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 2286 | int *wca; /* Wide character array. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2287 | int has_lower = FALSE; |
| 2288 | int was_letter = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2289 | |
Bram Moolenaar | e40e57c | 2007-11-08 12:04:26 +0000 | [diff] [blame] | 2290 | if (p_ic && curbuf->b_p_inf && len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2291 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2292 | /* Infer case of completed part. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2293 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2294 | /* Find actual length of completion. */ |
| 2295 | #ifdef FEAT_MBYTE |
| 2296 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2297 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2298 | p = str; |
| 2299 | actual_len = 0; |
| 2300 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2301 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2302 | mb_ptr_adv(p); |
| 2303 | ++actual_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2304 | } |
| 2305 | } |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2306 | else |
| 2307 | #endif |
| 2308 | actual_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2309 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2310 | /* Find actual length of original text. */ |
| 2311 | #ifdef FEAT_MBYTE |
| 2312 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2313 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2314 | p = compl_orig_text; |
| 2315 | actual_compl_length = 0; |
| 2316 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2318 | mb_ptr_adv(p); |
| 2319 | ++actual_compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2320 | } |
| 2321 | } |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2322 | else |
| 2323 | #endif |
| 2324 | actual_compl_length = compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2325 | |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2326 | /* "actual_len" may be smaller than "actual_compl_length" when using |
| 2327 | * thesaurus, only use the minimum when comparing. */ |
| 2328 | min_len = actual_len < actual_compl_length |
| 2329 | ? actual_len : actual_compl_length; |
| 2330 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2331 | /* Allocate wide character array for the completion and fill it. */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2332 | wca = (int *)alloc((unsigned)(actual_len * sizeof(int))); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2333 | if (wca != NULL) |
| 2334 | { |
| 2335 | p = str; |
| 2336 | for (i = 0; i < actual_len; ++i) |
| 2337 | #ifdef FEAT_MBYTE |
| 2338 | if (has_mbyte) |
| 2339 | wca[i] = mb_ptr2char_adv(&p); |
| 2340 | else |
| 2341 | #endif |
| 2342 | wca[i] = *(p++); |
| 2343 | |
| 2344 | /* Rule 1: Were any chars converted to lower? */ |
| 2345 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2346 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2347 | { |
| 2348 | #ifdef FEAT_MBYTE |
| 2349 | if (has_mbyte) |
| 2350 | c = mb_ptr2char_adv(&p); |
| 2351 | else |
| 2352 | #endif |
| 2353 | c = *(p++); |
| 2354 | if (MB_ISLOWER(c)) |
| 2355 | { |
| 2356 | has_lower = TRUE; |
| 2357 | if (MB_ISUPPER(wca[i])) |
| 2358 | { |
| 2359 | /* Rule 1 is satisfied. */ |
| 2360 | for (i = actual_compl_length; i < actual_len; ++i) |
| 2361 | wca[i] = MB_TOLOWER(wca[i]); |
| 2362 | break; |
| 2363 | } |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | /* |
| 2368 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 2369 | * upper case. |
| 2370 | */ |
| 2371 | if (!has_lower) |
| 2372 | { |
| 2373 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2374 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2375 | { |
| 2376 | #ifdef FEAT_MBYTE |
| 2377 | if (has_mbyte) |
| 2378 | c = mb_ptr2char_adv(&p); |
| 2379 | else |
| 2380 | #endif |
| 2381 | c = *(p++); |
| 2382 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 2383 | { |
| 2384 | /* Rule 2 is satisfied. */ |
| 2385 | for (i = actual_compl_length; i < actual_len; ++i) |
| 2386 | wca[i] = MB_TOUPPER(wca[i]); |
| 2387 | break; |
| 2388 | } |
| 2389 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | /* Copy the original case of the part we typed. */ |
| 2394 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2395 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2396 | { |
| 2397 | #ifdef FEAT_MBYTE |
| 2398 | if (has_mbyte) |
| 2399 | c = mb_ptr2char_adv(&p); |
| 2400 | else |
| 2401 | #endif |
| 2402 | c = *(p++); |
| 2403 | if (MB_ISLOWER(c)) |
| 2404 | wca[i] = MB_TOLOWER(wca[i]); |
| 2405 | else if (MB_ISUPPER(c)) |
| 2406 | wca[i] = MB_TOUPPER(wca[i]); |
| 2407 | } |
| 2408 | |
Bram Moolenaar | e40e57c | 2007-11-08 12:04:26 +0000 | [diff] [blame] | 2409 | /* |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2410 | * Generate encoding specific output from wide character array. |
| 2411 | * Multi-byte characters can occupy up to five bytes more than |
| 2412 | * ASCII characters, and we also need one byte for NUL, so stay |
| 2413 | * six bytes away from the edge of IObuff. |
| 2414 | */ |
| 2415 | p = IObuff; |
| 2416 | i = 0; |
| 2417 | while (i < actual_len && (p - IObuff + 6) < IOSIZE) |
| 2418 | #ifdef FEAT_MBYTE |
| 2419 | if (has_mbyte) |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 2420 | p += (*mb_char2bytes)(wca[i++], p); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2421 | else |
| 2422 | #endif |
| 2423 | *(p++) = wca[i++]; |
| 2424 | *p = NUL; |
| 2425 | |
| 2426 | vim_free(wca); |
| 2427 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2428 | |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2429 | return ins_compl_add(IObuff, len, icase, fname, NULL, dir, |
| 2430 | flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | } |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2432 | return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | /* |
| 2436 | * Add a match to the list of matches. |
| 2437 | * 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] | 2438 | * 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] | 2439 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | */ |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2441 | static int |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2442 | ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2443 | char_u *str; |
| 2444 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2445 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2446 | char_u *fname; |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2447 | char_u **cptext; /* extra text for popup menu or NULL */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2448 | int cdir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2449 | int flags; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2450 | int adup; /* accept duplicate match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2452 | compl_T *match; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2453 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2454 | |
| 2455 | ui_breakcheck(); |
| 2456 | if (got_int) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2457 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2458 | if (len < 0) |
| 2459 | len = (int)STRLEN(str); |
| 2460 | |
| 2461 | /* |
| 2462 | * If the same match is already present, don't add it. |
| 2463 | */ |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2464 | if (compl_first_match != NULL && !adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2465 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2466 | match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | do |
| 2468 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2469 | if ( !(match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 5948a57 | 2006-10-03 13:49:29 +0000 | [diff] [blame] | 2470 | && STRNCMP(match->cp_str, str, len) == 0 |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2471 | && match->cp_str[len] == NUL) |
| 2472 | return NOTDONE; |
| 2473 | match = match->cp_next; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2474 | } while (match != NULL && match != compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2477 | /* Remove any popup menu before changing the list of matches. */ |
| 2478 | ins_compl_del_pum(); |
| 2479 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | /* |
| 2481 | * Allocate a new match structure. |
| 2482 | * Copy the values to the new match structure. |
| 2483 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2484 | match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | if (match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2486 | return FAIL; |
| 2487 | match->cp_number = -1; |
| 2488 | if (flags & ORIGINAL_TEXT) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2489 | match->cp_number = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2490 | if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | { |
| 2492 | vim_free(match); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2493 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2495 | match->cp_icase = icase; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2496 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2497 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2498 | * - 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] | 2499 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 2500 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2501 | if (fname != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2502 | && compl_curr_match != NULL |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2503 | && compl_curr_match->cp_fname != NULL |
| 2504 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2505 | match->cp_fname = compl_curr_match->cp_fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2506 | else if (fname != NULL) |
| 2507 | { |
| 2508 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2509 | flags |= FREE_FNAME; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2510 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2511 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2512 | match->cp_fname = NULL; |
| 2513 | match->cp_flags = flags; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2514 | |
| 2515 | if (cptext != NULL) |
| 2516 | { |
| 2517 | int i; |
| 2518 | |
| 2519 | for (i = 0; i < CPT_COUNT; ++i) |
| 2520 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 2521 | match->cp_text[i] = vim_strsave(cptext[i]); |
| 2522 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2523 | |
| 2524 | /* |
| 2525 | * Link the new match structure in the list of matches. |
| 2526 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2527 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2528 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2529 | else if (dir == FORWARD) |
| 2530 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2531 | match->cp_next = compl_curr_match->cp_next; |
| 2532 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2533 | } |
| 2534 | else /* BACKWARD */ |
| 2535 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2536 | match->cp_next = compl_curr_match; |
| 2537 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2538 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2539 | if (match->cp_next) |
| 2540 | match->cp_next->cp_prev = match; |
| 2541 | if (match->cp_prev) |
| 2542 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2544 | compl_first_match = match; |
| 2545 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2547 | /* |
| 2548 | * Find the longest common string if still doing that. |
| 2549 | */ |
| 2550 | if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) |
| 2551 | ins_compl_longest_match(match); |
| 2552 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2553 | return OK; |
| 2554 | } |
| 2555 | |
| 2556 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2557 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
| 2558 | * match->cp_icase. |
| 2559 | */ |
| 2560 | static int |
| 2561 | ins_compl_equal(match, str, len) |
| 2562 | compl_T *match; |
| 2563 | char_u *str; |
| 2564 | int len; |
| 2565 | { |
| 2566 | if (match->cp_icase) |
| 2567 | return STRNICMP(match->cp_str, str, (size_t)len) == 0; |
| 2568 | return STRNCMP(match->cp_str, str, (size_t)len) == 0; |
| 2569 | } |
| 2570 | |
| 2571 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2572 | * Reduce the longest common string for match "match". |
| 2573 | */ |
| 2574 | static void |
| 2575 | ins_compl_longest_match(match) |
| 2576 | compl_T *match; |
| 2577 | { |
| 2578 | char_u *p, *s; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2579 | int c1, c2; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2580 | int had_match; |
| 2581 | |
| 2582 | if (compl_leader == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2583 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2584 | /* First match, use it as a whole. */ |
| 2585 | compl_leader = vim_strsave(match->cp_str); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2586 | if (compl_leader != NULL) |
| 2587 | { |
| 2588 | had_match = (curwin->w_cursor.col > compl_col); |
| 2589 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2590 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2591 | ins_redraw(FALSE); |
| 2592 | |
| 2593 | /* When the match isn't there (to avoid matching itself) remove it |
| 2594 | * again after redrawing. */ |
| 2595 | if (!had_match) |
| 2596 | ins_compl_delete(); |
| 2597 | compl_used_match = FALSE; |
| 2598 | } |
| 2599 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2600 | else |
| 2601 | { |
| 2602 | /* Reduce the text if this match differs from compl_leader. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2603 | p = compl_leader; |
| 2604 | s = match->cp_str; |
| 2605 | while (*p != NUL) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2606 | { |
| 2607 | #ifdef FEAT_MBYTE |
| 2608 | if (has_mbyte) |
| 2609 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2610 | c1 = mb_ptr2char(p); |
| 2611 | c2 = mb_ptr2char(s); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2612 | } |
| 2613 | else |
| 2614 | #endif |
| 2615 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2616 | c1 = *p; |
| 2617 | c2 = *s; |
| 2618 | } |
| 2619 | if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) |
| 2620 | : (c1 != c2)) |
| 2621 | break; |
| 2622 | #ifdef FEAT_MBYTE |
| 2623 | if (has_mbyte) |
| 2624 | { |
| 2625 | mb_ptr_adv(p); |
| 2626 | mb_ptr_adv(s); |
| 2627 | } |
| 2628 | else |
| 2629 | #endif |
| 2630 | { |
| 2631 | ++p; |
| 2632 | ++s; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2633 | } |
| 2634 | } |
| 2635 | |
| 2636 | if (*p != NUL) |
| 2637 | { |
| 2638 | /* Leader was shortened, need to change the inserted text. */ |
| 2639 | *p = NUL; |
| 2640 | had_match = (curwin->w_cursor.col > compl_col); |
| 2641 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2642 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2643 | ins_redraw(FALSE); |
| 2644 | |
| 2645 | /* When the match isn't there (to avoid matching itself) remove it |
| 2646 | * again after redrawing. */ |
| 2647 | if (!had_match) |
| 2648 | ins_compl_delete(); |
| 2649 | } |
| 2650 | |
| 2651 | compl_used_match = FALSE; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | * Add an array of matches to the list of matches. |
| 2657 | * Frees matches[]. |
| 2658 | */ |
| 2659 | static void |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2660 | ins_compl_add_matches(num_matches, matches, icase) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2661 | int num_matches; |
| 2662 | char_u **matches; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2663 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | { |
| 2665 | int i; |
| 2666 | int add_r = OK; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2667 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2668 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2669 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2670 | if ((add_r = ins_compl_add(matches[i], -1, icase, |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2671 | NULL, NULL, dir, 0, FALSE)) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | /* if dir was BACKWARD then honor it just once */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2673 | dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2674 | FreeWild(num_matches, matches); |
| 2675 | } |
| 2676 | |
| 2677 | /* Make the completion list cyclic. |
| 2678 | * Return the number of matches (excluding the original). |
| 2679 | */ |
| 2680 | static int |
| 2681 | ins_compl_make_cyclic() |
| 2682 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2683 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2684 | int count = 0; |
| 2685 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2686 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2687 | { |
| 2688 | /* |
| 2689 | * Find the end of the list. |
| 2690 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2691 | match = compl_first_match; |
| 2692 | /* 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] | 2693 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2694 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2695 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | ++count; |
| 2697 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2698 | match->cp_next = compl_first_match; |
| 2699 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | } |
| 2701 | return count; |
| 2702 | } |
| 2703 | |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2704 | /* |
| 2705 | * Start completion for the complete() function. |
| 2706 | * "startcol" is where the matched text starts (1 is first column). |
| 2707 | * "list" is the list of matches. |
| 2708 | */ |
| 2709 | void |
| 2710 | set_completion(startcol, list) |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2711 | colnr_T startcol; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2712 | list_T *list; |
| 2713 | { |
| 2714 | /* If already doing completions stop it. */ |
| 2715 | if (ctrl_x_mode != 0) |
| 2716 | ins_compl_prep(' '); |
| 2717 | ins_compl_clear(); |
| 2718 | |
| 2719 | if (stop_arrow() == FAIL) |
| 2720 | return; |
| 2721 | |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 2722 | compl_direction = FORWARD; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2723 | if (startcol > curwin->w_cursor.col) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2724 | startcol = curwin->w_cursor.col; |
| 2725 | compl_col = startcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2726 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2727 | /* compl_pattern doesn't need to be set */ |
| 2728 | compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); |
| 2729 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 2730 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2731 | return; |
| 2732 | |
| 2733 | /* Handle like dictionary completion. */ |
| 2734 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2735 | |
| 2736 | ins_compl_add_list(list); |
| 2737 | compl_matches = ins_compl_make_cyclic(); |
| 2738 | compl_started = TRUE; |
| 2739 | compl_used_match = TRUE; |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 2740 | compl_cont_status = 0; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2741 | |
| 2742 | compl_curr_match = compl_first_match; |
| 2743 | ins_complete(Ctrl_N); |
| 2744 | out_flush(); |
| 2745 | } |
| 2746 | |
| 2747 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2748 | /* "compl_match_array" points the currently displayed list of entries in the |
| 2749 | * popup menu. It is NULL when there is no popup menu. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2750 | static pumitem_T *compl_match_array = NULL; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2751 | static int compl_match_arraysize; |
| 2752 | |
| 2753 | /* |
| 2754 | * Update the screen and when there is any scrolling remove the popup menu. |
| 2755 | */ |
| 2756 | static void |
| 2757 | ins_compl_upd_pum() |
| 2758 | { |
| 2759 | int h; |
| 2760 | |
| 2761 | if (compl_match_array != NULL) |
| 2762 | { |
| 2763 | h = curwin->w_cline_height; |
| 2764 | update_screen(0); |
| 2765 | if (h != curwin->w_cline_height) |
| 2766 | ins_compl_del_pum(); |
| 2767 | } |
| 2768 | } |
| 2769 | |
| 2770 | /* |
| 2771 | * Remove any popup menu. |
| 2772 | */ |
| 2773 | static void |
| 2774 | ins_compl_del_pum() |
| 2775 | { |
| 2776 | if (compl_match_array != NULL) |
| 2777 | { |
| 2778 | pum_undisplay(); |
| 2779 | vim_free(compl_match_array); |
| 2780 | compl_match_array = NULL; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | /* |
| 2785 | * Return TRUE if the popup menu should be displayed. |
| 2786 | */ |
| 2787 | static int |
| 2788 | pum_wanted() |
| 2789 | { |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2790 | /* 'completeopt' must contain "menu" or "menuone" */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2791 | if (vim_strchr(p_cot, 'm') == NULL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2792 | return FALSE; |
| 2793 | |
| 2794 | /* The display looks bad on a B&W display. */ |
| 2795 | if (t_colors < 8 |
| 2796 | #ifdef FEAT_GUI |
| 2797 | && !gui.in_use |
| 2798 | #endif |
| 2799 | ) |
| 2800 | return FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2801 | return TRUE; |
| 2802 | } |
| 2803 | |
| 2804 | /* |
| 2805 | * 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] | 2806 | * One if 'completopt' contains "menuone". |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2807 | */ |
| 2808 | static int |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2809 | pum_enough_matches() |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2810 | { |
| 2811 | compl_T *compl; |
| 2812 | int i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2813 | |
| 2814 | /* Don't display the popup menu if there are no matches or there is only |
| 2815 | * one (ignoring the original text). */ |
| 2816 | compl = compl_first_match; |
| 2817 | i = 0; |
| 2818 | do |
| 2819 | { |
| 2820 | if (compl == NULL |
| 2821 | || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) |
| 2822 | break; |
| 2823 | compl = compl->cp_next; |
| 2824 | } while (compl != compl_first_match); |
| 2825 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2826 | if (strstr((char *)p_cot, "menuone") != NULL) |
| 2827 | return (i >= 1); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2828 | return (i >= 2); |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * Show the popup menu for the list of matches. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2833 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2834 | */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2835 | void |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2836 | ins_compl_show_pum() |
| 2837 | { |
| 2838 | compl_T *compl; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2839 | compl_T *shown_compl = NULL; |
| 2840 | int did_find_shown_match = FALSE; |
| 2841 | int shown_match_ok = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2842 | int i; |
| 2843 | int cur = -1; |
| 2844 | colnr_T col; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2845 | int lead_len = 0; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2846 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2847 | if (!pum_wanted() || !pum_enough_matches()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2848 | return; |
| 2849 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2850 | #if defined(FEAT_EVAL) |
| 2851 | /* Dirty hard-coded hack: remove any matchparen highlighting. */ |
| 2852 | do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif"); |
| 2853 | #endif |
| 2854 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2855 | /* Update the screen before drawing the popup menu over it. */ |
| 2856 | update_screen(0); |
| 2857 | |
| 2858 | if (compl_match_array == NULL) |
| 2859 | { |
| 2860 | /* Need to build the popup menu list. */ |
| 2861 | compl_match_arraysize = 0; |
| 2862 | compl = compl_first_match; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2863 | if (compl_leader != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2864 | lead_len = (int)STRLEN(compl_leader); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2865 | do |
| 2866 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2867 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2868 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2869 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2870 | ++compl_match_arraysize; |
| 2871 | compl = compl->cp_next; |
| 2872 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2873 | if (compl_match_arraysize == 0) |
| 2874 | return; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2875 | compl_match_array = (pumitem_T *)alloc_clear( |
| 2876 | (unsigned)(sizeof(pumitem_T) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2877 | * compl_match_arraysize)); |
| 2878 | if (compl_match_array != NULL) |
| 2879 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2880 | /* If the current match is the original text don't find the first |
| 2881 | * match after it, don't highlight anything. */ |
| 2882 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 2883 | shown_match_ok = TRUE; |
| 2884 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2885 | i = 0; |
| 2886 | compl = compl_first_match; |
| 2887 | do |
| 2888 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2889 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2890 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2891 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2892 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2893 | if (!shown_match_ok) |
| 2894 | { |
| 2895 | if (compl == compl_shown_match || did_find_shown_match) |
| 2896 | { |
| 2897 | /* This item is the shown match or this is the |
| 2898 | * first displayed item after the shown match. */ |
| 2899 | compl_shown_match = compl; |
| 2900 | did_find_shown_match = TRUE; |
| 2901 | shown_match_ok = TRUE; |
| 2902 | } |
| 2903 | else |
| 2904 | /* Remember this displayed match for when the |
| 2905 | * shown match is just below it. */ |
| 2906 | shown_compl = compl; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2907 | cur = i; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2908 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2909 | |
| 2910 | if (compl->cp_text[CPT_ABBR] != NULL) |
| 2911 | compl_match_array[i].pum_text = |
| 2912 | compl->cp_text[CPT_ABBR]; |
| 2913 | else |
| 2914 | compl_match_array[i].pum_text = compl->cp_str; |
| 2915 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 2916 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 2917 | if (compl->cp_text[CPT_MENU] != NULL) |
| 2918 | compl_match_array[i++].pum_extra = |
| 2919 | compl->cp_text[CPT_MENU]; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2920 | else |
| 2921 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 2922 | } |
| 2923 | |
| 2924 | if (compl == compl_shown_match) |
| 2925 | { |
| 2926 | did_find_shown_match = TRUE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 2927 | |
| 2928 | /* When the original text is the shown match don't set |
| 2929 | * compl_shown_match. */ |
| 2930 | if (compl->cp_flags & ORIGINAL_TEXT) |
| 2931 | shown_match_ok = TRUE; |
| 2932 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2933 | if (!shown_match_ok && shown_compl != NULL) |
| 2934 | { |
| 2935 | /* The shown match isn't displayed, set it to the |
| 2936 | * previously displayed match. */ |
| 2937 | compl_shown_match = shown_compl; |
| 2938 | shown_match_ok = TRUE; |
| 2939 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2940 | } |
| 2941 | compl = compl->cp_next; |
| 2942 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2943 | |
| 2944 | if (!shown_match_ok) /* no displayed match at all */ |
| 2945 | cur = -1; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2946 | } |
| 2947 | } |
| 2948 | else |
| 2949 | { |
| 2950 | /* popup menu already exists, only need to find the current item.*/ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2951 | for (i = 0; i < compl_match_arraysize; ++i) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2952 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str |
| 2953 | || compl_match_array[i].pum_text |
| 2954 | == compl_shown_match->cp_text[CPT_ABBR]) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2955 | { |
| 2956 | cur = i; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2957 | break; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2958 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
| 2961 | if (compl_match_array != NULL) |
| 2962 | { |
| 2963 | /* Compute the screen column of the start of the completed text. |
| 2964 | * Use the cursor to get all wrapping and other settings right. */ |
| 2965 | col = curwin->w_cursor.col; |
| 2966 | curwin->w_cursor.col = compl_col; |
Bram Moolenaar | d289f13 | 2006-03-11 21:30:53 +0000 | [diff] [blame] | 2967 | pum_display(compl_match_array, compl_match_arraysize, cur); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2968 | curwin->w_cursor.col = col; |
| 2969 | } |
| 2970 | } |
| 2971 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2972 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 2973 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2974 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2975 | /* |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2976 | * Add any identifiers that match the given pattern in the list of dictionary |
| 2977 | * files "dict_start" to the list of completions. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2978 | */ |
| 2979 | static void |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2980 | ins_compl_dictionaries(dict_start, pat, flags, thesaurus) |
| 2981 | char_u *dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2982 | char_u *pat; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2983 | int flags; /* DICT_FIRST and/or DICT_EXACT */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2984 | int thesaurus; /* Thesaurus completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2985 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2986 | char_u *dict = dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2987 | char_u *ptr; |
| 2988 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2989 | regmatch_T regmatch; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | char_u **files; |
| 2991 | int count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | int save_p_scs; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2993 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2995 | if (*dict == NUL) |
| 2996 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2997 | #ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2998 | /* When 'dictionary' is empty and spell checking is enabled use |
| 2999 | * "spell". */ |
| 3000 | if (!thesaurus && curwin->w_p_spell) |
| 3001 | dict = (char_u *)"spell"; |
| 3002 | else |
| 3003 | #endif |
| 3004 | return; |
| 3005 | } |
| 3006 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3007 | buf = alloc(LSIZE); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3008 | if (buf == NULL) |
| 3009 | return; |
Bram Moolenaar | fa3491a | 2007-02-20 02:49:19 +0000 | [diff] [blame] | 3010 | regmatch.regprog = NULL; /* so that we can goto theend */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3011 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 3013 | save_p_scs = p_scs; |
| 3014 | if (curbuf->b_p_inf) |
| 3015 | p_scs = FALSE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3016 | |
| 3017 | /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 3018 | * 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] | 3019 | * pattern. Also need to double backslashes. */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3020 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3021 | { |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3022 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3023 | size_t len; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3024 | |
| 3025 | if (pat_esc == NULL) |
Bram Moolenaar | 6519ac8 | 2007-05-06 13:45:52 +0000 | [diff] [blame] | 3026 | goto theend; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3027 | len = STRLEN(pat_esc) + 10; |
| 3028 | ptr = alloc((unsigned)len); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3029 | if (ptr == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3030 | { |
| 3031 | vim_free(pat_esc); |
Bram Moolenaar | fa3491a | 2007-02-20 02:49:19 +0000 | [diff] [blame] | 3032 | goto theend; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3033 | } |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3034 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3035 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 3036 | vim_free(pat_esc); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3037 | vim_free(ptr); |
| 3038 | } |
| 3039 | else |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3040 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3041 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3042 | if (regmatch.regprog == NULL) |
| 3043 | goto theend; |
| 3044 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3045 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3046 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 3047 | regmatch.rm_ic = ignorecase(pat); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3048 | while (*dict != NUL && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3049 | { |
| 3050 | /* copy one dictionary file name into buf */ |
| 3051 | if (flags == DICT_EXACT) |
| 3052 | { |
| 3053 | count = 1; |
| 3054 | files = &dict; |
| 3055 | } |
| 3056 | else |
| 3057 | { |
| 3058 | /* Expand wildcards in the dictionary name, but do not allow |
| 3059 | * backticks (for security, the 'dict' option may have been set in |
| 3060 | * a modeline). */ |
| 3061 | copy_option_part(&dict, buf, LSIZE, ","); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3062 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3063 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 3064 | count = -1; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3065 | else |
| 3066 | # endif |
| 3067 | if (vim_strchr(buf, '`') != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3068 | || expand_wildcards(1, &buf, &count, &files, |
| 3069 | EW_FILE|EW_SILENT) != OK) |
| 3070 | count = 0; |
| 3071 | } |
| 3072 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3073 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3074 | if (count == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3075 | { |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3076 | /* Complete from active spelling. Skip "\<" in the pattern, we |
| 3077 | * don't use it as a RE. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3078 | if (pat[0] == '\\' && pat[1] == '<') |
| 3079 | ptr = pat + 2; |
| 3080 | else |
| 3081 | ptr = pat; |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3082 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3083 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3084 | else |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3085 | # endif |
Bram Moolenaar | d7fd0c4 | 2006-08-22 17:55:55 +0000 | [diff] [blame] | 3086 | if (count > 0) /* avoid warning for using "files" uninit */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3087 | { |
| 3088 | ins_compl_files(count, files, thesaurus, flags, |
| 3089 | ®match, buf, &dir); |
| 3090 | if (flags != DICT_EXACT) |
| 3091 | FreeWild(count, files); |
| 3092 | } |
| 3093 | if (flags != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3094 | break; |
| 3095 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3096 | |
| 3097 | theend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3098 | p_scs = save_p_scs; |
| 3099 | vim_free(regmatch.regprog); |
| 3100 | vim_free(buf); |
| 3101 | } |
| 3102 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3103 | static void |
| 3104 | ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir) |
| 3105 | int count; |
| 3106 | char_u **files; |
| 3107 | int thesaurus; |
| 3108 | int flags; |
| 3109 | regmatch_T *regmatch; |
| 3110 | char_u *buf; |
| 3111 | int *dir; |
| 3112 | { |
| 3113 | char_u *ptr; |
| 3114 | int i; |
| 3115 | FILE *fp; |
| 3116 | int add_r; |
| 3117 | |
| 3118 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 3119 | { |
| 3120 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 3121 | if (flags != DICT_EXACT) |
| 3122 | { |
| 3123 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3124 | _("Scanning dictionary: %s"), (char *)files[i]); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3125 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
| 3128 | if (fp != NULL) |
| 3129 | { |
| 3130 | /* |
| 3131 | * Read dictionary file line by line. |
| 3132 | * Check each line for a match. |
| 3133 | */ |
| 3134 | while (!got_int && !compl_interrupted |
| 3135 | && !vim_fgets(buf, LSIZE, fp)) |
| 3136 | { |
| 3137 | ptr = buf; |
| 3138 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
| 3139 | { |
| 3140 | ptr = regmatch->startp[0]; |
| 3141 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3142 | ptr = find_line_end(ptr); |
| 3143 | else |
| 3144 | ptr = find_word_end(ptr); |
| 3145 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 3146 | (int)(ptr - regmatch->startp[0]), |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3147 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3148 | if (thesaurus) |
| 3149 | { |
| 3150 | char_u *wstart; |
| 3151 | |
| 3152 | /* |
| 3153 | * Add the other matches on the line |
| 3154 | */ |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3155 | ptr = buf; |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3156 | while (!got_int) |
| 3157 | { |
| 3158 | /* Find start of the next word. Skip white |
| 3159 | * space and punctuation. */ |
| 3160 | ptr = find_word_start(ptr); |
| 3161 | if (*ptr == NUL || *ptr == NL) |
| 3162 | break; |
| 3163 | wstart = ptr; |
| 3164 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3165 | /* Find end of the word. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3166 | #ifdef FEAT_MBYTE |
| 3167 | if (has_mbyte) |
| 3168 | /* Japanese words may have characters in |
| 3169 | * different classes, only separate words |
| 3170 | * with single-byte non-word characters. */ |
| 3171 | while (*ptr != NUL) |
| 3172 | { |
| 3173 | int l = (*mb_ptr2len)(ptr); |
| 3174 | |
| 3175 | if (l < 2 && !vim_iswordc(*ptr)) |
| 3176 | break; |
| 3177 | ptr += l; |
| 3178 | } |
| 3179 | else |
| 3180 | #endif |
| 3181 | ptr = find_word_end(ptr); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3182 | |
| 3183 | /* Add the word. Skip the regexp match. */ |
| 3184 | if (wstart != regmatch->startp[0]) |
| 3185 | add_r = ins_compl_add_infercase(wstart, |
| 3186 | (int)(ptr - wstart), |
| 3187 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3188 | } |
| 3189 | } |
| 3190 | if (add_r == OK) |
| 3191 | /* if dir was BACKWARD then honor it just once */ |
| 3192 | *dir = FORWARD; |
| 3193 | else if (add_r == FAIL) |
| 3194 | break; |
| 3195 | /* avoid expensive call to vim_regexec() when at end |
| 3196 | * of line */ |
| 3197 | if (*ptr == '\n' || got_int) |
| 3198 | break; |
| 3199 | } |
| 3200 | line_breakcheck(); |
| 3201 | ins_compl_check_keys(50); |
| 3202 | } |
| 3203 | fclose(fp); |
| 3204 | } |
| 3205 | } |
| 3206 | } |
| 3207 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3208 | /* |
| 3209 | * Find the start of the next word. |
| 3210 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 3211 | */ |
| 3212 | char_u * |
| 3213 | find_word_start(ptr) |
| 3214 | char_u *ptr; |
| 3215 | { |
| 3216 | #ifdef FEAT_MBYTE |
| 3217 | if (has_mbyte) |
| 3218 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3219 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3220 | else |
| 3221 | #endif |
| 3222 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 3223 | ++ptr; |
| 3224 | return ptr; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | * Find the end of the word. Assumes it starts inside a word. |
| 3229 | * Returns a pointer to just after the word. |
| 3230 | */ |
| 3231 | char_u * |
| 3232 | find_word_end(ptr) |
| 3233 | char_u *ptr; |
| 3234 | { |
| 3235 | #ifdef FEAT_MBYTE |
| 3236 | int start_class; |
| 3237 | |
| 3238 | if (has_mbyte) |
| 3239 | { |
| 3240 | start_class = mb_get_class(ptr); |
| 3241 | if (start_class > 1) |
| 3242 | while (*ptr != NUL) |
| 3243 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3244 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3245 | if (mb_get_class(ptr) != start_class) |
| 3246 | break; |
| 3247 | } |
| 3248 | } |
| 3249 | else |
| 3250 | #endif |
| 3251 | while (vim_iswordc(*ptr)) |
| 3252 | ++ptr; |
| 3253 | return ptr; |
| 3254 | } |
| 3255 | |
| 3256 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3257 | * Find the end of the line, omitting CR and NL at the end. |
| 3258 | * Returns a pointer to just after the line. |
| 3259 | */ |
| 3260 | static char_u * |
| 3261 | find_line_end(ptr) |
| 3262 | char_u *ptr; |
| 3263 | { |
| 3264 | char_u *s; |
| 3265 | |
| 3266 | s = ptr + STRLEN(ptr); |
| 3267 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 3268 | --s; |
| 3269 | return s; |
| 3270 | } |
| 3271 | |
| 3272 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3273 | * Free the list of completions |
| 3274 | */ |
| 3275 | static void |
| 3276 | ins_compl_free() |
| 3277 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3278 | compl_T *match; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3279 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3280 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3281 | vim_free(compl_pattern); |
| 3282 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3283 | vim_free(compl_leader); |
| 3284 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3285 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3286 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3287 | return; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3288 | |
| 3289 | ins_compl_del_pum(); |
| 3290 | pum_clear(); |
| 3291 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3292 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3293 | do |
| 3294 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3295 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3296 | compl_curr_match = compl_curr_match->cp_next; |
| 3297 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3298 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3299 | if (match->cp_flags & FREE_FNAME) |
| 3300 | vim_free(match->cp_fname); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3301 | for (i = 0; i < CPT_COUNT; ++i) |
| 3302 | vim_free(match->cp_text[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3303 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3304 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 3305 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 3306 | compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | static void |
| 3310 | ins_compl_clear() |
| 3311 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3312 | compl_cont_status = 0; |
| 3313 | compl_started = FALSE; |
| 3314 | compl_matches = 0; |
| 3315 | vim_free(compl_pattern); |
| 3316 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3317 | vim_free(compl_leader); |
| 3318 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3319 | edit_submode_extra = NULL; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3320 | vim_free(compl_orig_text); |
| 3321 | compl_orig_text = NULL; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3322 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | /* |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 3326 | * Return TRUE when Insert completion is active. |
| 3327 | */ |
| 3328 | int |
| 3329 | ins_compl_active() |
| 3330 | { |
| 3331 | return compl_started; |
| 3332 | } |
| 3333 | |
| 3334 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3335 | * Delete one character before the cursor and show the subset of the matches |
| 3336 | * that match the word that is now before the cursor. |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3337 | * Returns the character to be used, NUL if the work is done and another char |
| 3338 | * to be got from the user. |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3339 | */ |
| 3340 | static int |
| 3341 | ins_compl_bs() |
| 3342 | { |
| 3343 | char_u *line; |
| 3344 | char_u *p; |
| 3345 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3346 | line = ml_get_curline(); |
| 3347 | p = line + curwin->w_cursor.col; |
| 3348 | mb_ptr_back(line, p); |
| 3349 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 3350 | /* Stop completion when the whole word was deleted. For Omni completion |
| 3351 | * allow the word to be deleted, we won't match everything. */ |
| 3352 | if ((int)(p - line) - (int)compl_col < 0 |
| 3353 | || ((int)(p - line) - (int)compl_col == 0 |
| 3354 | && (ctrl_x_mode & CTRL_X_OMNI) == 0)) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3355 | return K_BS; |
| 3356 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3357 | /* Deleted more than what was used to find matches or didn't finish |
| 3358 | * finding all matches: need to look for matches all over again. */ |
| 3359 | if (curwin->w_cursor.col <= compl_col + compl_length |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3360 | || ins_compl_need_restart()) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3361 | ins_compl_restart(); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3362 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3363 | vim_free(compl_leader); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3364 | compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3365 | if (compl_leader != NULL) |
| 3366 | { |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3367 | ins_compl_new_leader(); |
| 3368 | return NUL; |
| 3369 | } |
| 3370 | return K_BS; |
| 3371 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3372 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3373 | /* |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3374 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 3375 | * be called. |
| 3376 | */ |
| 3377 | static int |
| 3378 | ins_compl_need_restart() |
| 3379 | { |
| 3380 | /* Return TRUE if we didn't complete finding matches or when the |
| 3381 | * 'completefunc' returned "always" in the "refresh" dictionary item. */ |
| 3382 | return compl_was_interrupted |
| 3383 | || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
| 3384 | && compl_opt_refresh_always); |
| 3385 | } |
| 3386 | |
| 3387 | /* |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3388 | * Called after changing "compl_leader". |
| 3389 | * Show the popup menu with a different set of matches. |
| 3390 | * May also search for matches again if the previous search was interrupted. |
| 3391 | */ |
| 3392 | static void |
| 3393 | ins_compl_new_leader() |
| 3394 | { |
| 3395 | ins_compl_del_pum(); |
| 3396 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3397 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3398 | compl_used_match = FALSE; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3399 | |
| 3400 | if (compl_started) |
| 3401 | ins_compl_set_original_text(compl_leader); |
| 3402 | else |
| 3403 | { |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3404 | #ifdef FEAT_SPELL |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3405 | spell_bad_len = 0; /* need to redetect bad word */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3406 | #endif |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3407 | /* |
| 3408 | * Matches were cleared, need to search for them now. First display |
| 3409 | * the changed text before the cursor. Set "compl_restarting" to |
| 3410 | * avoid that the first match is inserted. |
| 3411 | */ |
| 3412 | update_screen(0); |
| 3413 | #ifdef FEAT_GUI |
| 3414 | if (gui.in_use) |
| 3415 | { |
| 3416 | /* Show the cursor after the match, not after the redrawn text. */ |
| 3417 | setcursor(); |
| 3418 | out_flush(); |
| 3419 | gui_update_cursor(FALSE, FALSE); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3420 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3421 | #endif |
| 3422 | compl_restarting = TRUE; |
| 3423 | if (ins_complete(Ctrl_N) == FAIL) |
| 3424 | compl_cont_status = 0; |
| 3425 | compl_restarting = FALSE; |
| 3426 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3427 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3428 | compl_enter_selects = !compl_used_match; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3429 | |
| 3430 | /* Show the popup menu with a different set of matches. */ |
| 3431 | ins_compl_show_pum(); |
Bram Moolenaar | 7073cc8 | 2006-08-29 16:33:06 +0000 | [diff] [blame] | 3432 | |
| 3433 | /* Don't let Enter select the original text when there is no popup menu. */ |
| 3434 | if (compl_match_array == NULL) |
| 3435 | compl_enter_selects = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | /* |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3439 | * Return the length of the completion, from the completion start column to |
| 3440 | * the cursor column. Making sure it never goes below zero. |
| 3441 | */ |
| 3442 | static int |
| 3443 | ins_compl_len() |
| 3444 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3445 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3446 | |
| 3447 | if (off < 0) |
| 3448 | return 0; |
| 3449 | return off; |
| 3450 | } |
| 3451 | |
| 3452 | /* |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3453 | * Append one character to the match leader. May reduce the number of |
| 3454 | * matches. |
| 3455 | */ |
| 3456 | static void |
| 3457 | ins_compl_addleader(c) |
| 3458 | int c; |
| 3459 | { |
| 3460 | #ifdef FEAT_MBYTE |
| 3461 | int cc; |
| 3462 | |
| 3463 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 3464 | { |
| 3465 | char_u buf[MB_MAXBYTES + 1]; |
| 3466 | |
| 3467 | (*mb_char2bytes)(c, buf); |
| 3468 | buf[cc] = NUL; |
| 3469 | ins_char_bytes(buf, cc); |
Bram Moolenaar | 5e1a0a9 | 2012-06-20 14:26:35 +0200 | [diff] [blame] | 3470 | AppendToRedobuff(buf); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3471 | } |
| 3472 | else |
| 3473 | #endif |
Bram Moolenaar | 5e1a0a9 | 2012-06-20 14:26:35 +0200 | [diff] [blame] | 3474 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3475 | ins_char(c); |
Bram Moolenaar | 5e1a0a9 | 2012-06-20 14:26:35 +0200 | [diff] [blame] | 3476 | AppendCharToRedobuff(c); |
| 3477 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3478 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3479 | /* If we didn't complete finding matches we must search again. */ |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3480 | if (ins_compl_need_restart()) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3481 | ins_compl_restart(); |
| 3482 | |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3483 | /* When 'always' is set, don't reset compl_leader. While completing, |
| 3484 | * cursor don't point original position, changing compl_leader would |
| 3485 | * break redo. */ |
| 3486 | if (!compl_opt_refresh_always) |
| 3487 | { |
| 3488 | vim_free(compl_leader); |
| 3489 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3490 | (int)(curwin->w_cursor.col - compl_col)); |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3491 | if (compl_leader != NULL) |
| 3492 | ins_compl_new_leader(); |
| 3493 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3494 | } |
| 3495 | |
| 3496 | /* |
| 3497 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 3498 | * BS or a key was typed while still searching for matches. |
| 3499 | */ |
| 3500 | static void |
| 3501 | ins_compl_restart() |
| 3502 | { |
| 3503 | ins_compl_free(); |
| 3504 | compl_started = FALSE; |
| 3505 | compl_matches = 0; |
| 3506 | compl_cont_status = 0; |
| 3507 | compl_cont_mode = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* |
| 3511 | * Set the first match, the original text. |
| 3512 | */ |
| 3513 | static void |
| 3514 | ins_compl_set_original_text(str) |
| 3515 | char_u *str; |
| 3516 | { |
| 3517 | char_u *p; |
| 3518 | |
| 3519 | /* Replace the original text entry. */ |
| 3520 | if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */ |
| 3521 | { |
| 3522 | p = vim_strsave(str); |
| 3523 | if (p != NULL) |
| 3524 | { |
| 3525 | vim_free(compl_first_match->cp_str); |
| 3526 | compl_first_match->cp_str = p; |
| 3527 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3532 | * Append one character to the match leader. May reduce the number of |
| 3533 | * matches. |
| 3534 | */ |
| 3535 | static void |
| 3536 | ins_compl_addfrommatch() |
| 3537 | { |
| 3538 | char_u *p; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3539 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3540 | int c; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3541 | compl_T *cp; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3542 | |
| 3543 | p = compl_shown_match->cp_str; |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 3544 | if ((int)STRLEN(p) <= len) /* the match is too short */ |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3545 | { |
| 3546 | /* When still at the original match use the first entry that matches |
| 3547 | * the leader. */ |
| 3548 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 3549 | { |
| 3550 | p = NULL; |
| 3551 | for (cp = compl_shown_match->cp_next; cp != NULL |
| 3552 | && cp != compl_first_match; cp = cp->cp_next) |
| 3553 | { |
Bram Moolenaar | 132283f | 2006-10-03 13:22:23 +0000 | [diff] [blame] | 3554 | if (compl_leader == NULL |
| 3555 | || ins_compl_equal(cp, compl_leader, |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3556 | (int)STRLEN(compl_leader))) |
| 3557 | { |
| 3558 | p = cp->cp_str; |
| 3559 | break; |
| 3560 | } |
| 3561 | } |
| 3562 | if (p == NULL || (int)STRLEN(p) <= len) |
| 3563 | return; |
| 3564 | } |
| 3565 | else |
| 3566 | return; |
| 3567 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3568 | p += len; |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 3569 | c = PTR2CHAR(p); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3570 | ins_compl_addleader(c); |
| 3571 | } |
| 3572 | |
| 3573 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3574 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3575 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3576 | * Returns TRUE when the character is not to be inserted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3577 | */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3578 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3579 | ins_compl_prep(c) |
| 3580 | int c; |
| 3581 | { |
| 3582 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3583 | int want_cindent; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3584 | int retval = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3585 | |
| 3586 | /* Forget any previous 'special' messages if this is actually |
| 3587 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 3588 | */ |
| 3589 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 3590 | edit_submode_extra = NULL; |
| 3591 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 3592 | /* Ignore end of Select mode mapping and mouse scroll buttons. */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 3593 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
| 3594 | || c == K_MOUSELEFT || c == K_MOUSERIGHT) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3595 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3596 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3597 | /* Set "compl_get_longest" when finding the first matches. */ |
| 3598 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 3599 | || (ctrl_x_mode == 0 && !compl_started)) |
| 3600 | { |
| 3601 | compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); |
| 3602 | compl_used_match = TRUE; |
| 3603 | } |
| 3604 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 3606 | { |
| 3607 | /* |
| 3608 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 3609 | * it will be yet. Now we decide. |
| 3610 | */ |
| 3611 | switch (c) |
| 3612 | { |
| 3613 | case Ctrl_E: |
| 3614 | case Ctrl_Y: |
| 3615 | ctrl_x_mode = CTRL_X_SCROLL; |
| 3616 | if (!(State & REPLACE_FLAG)) |
| 3617 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 3618 | else |
| 3619 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 3620 | edit_submode_pre = NULL; |
| 3621 | showmode(); |
| 3622 | break; |
| 3623 | case Ctrl_L: |
| 3624 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 3625 | break; |
| 3626 | case Ctrl_F: |
| 3627 | ctrl_x_mode = CTRL_X_FILES; |
| 3628 | break; |
| 3629 | case Ctrl_K: |
| 3630 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 3631 | break; |
| 3632 | case Ctrl_R: |
| 3633 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 3634 | break; |
| 3635 | case Ctrl_T: |
| 3636 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 3637 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3638 | #ifdef FEAT_COMPL_FUNC |
| 3639 | case Ctrl_U: |
| 3640 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 3641 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3642 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3643 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3644 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3645 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3646 | case 's': |
| 3647 | case Ctrl_S: |
| 3648 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3649 | #ifdef FEAT_SPELL |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3650 | ++emsg_off; /* Avoid getting the E756 error twice. */ |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3651 | spell_back_to_badword(); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3652 | --emsg_off; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3653 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3654 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3655 | case Ctrl_RSB: |
| 3656 | ctrl_x_mode = CTRL_X_TAGS; |
| 3657 | break; |
| 3658 | #ifdef FEAT_FIND_ID |
| 3659 | case Ctrl_I: |
| 3660 | case K_S_TAB: |
| 3661 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 3662 | break; |
| 3663 | case Ctrl_D: |
| 3664 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 3665 | break; |
| 3666 | #endif |
| 3667 | case Ctrl_V: |
| 3668 | case Ctrl_Q: |
| 3669 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 3670 | break; |
| 3671 | case Ctrl_P: |
| 3672 | case Ctrl_N: |
| 3673 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 3674 | * just started ^X mode, or there were enough ^X's to cancel |
| 3675 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 3676 | * do normal expansion when interrupting a different mode (say |
| 3677 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 3678 | * nothing changes if interrupting mode 0, (eg, the flag |
| 3679 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3680 | if (!(compl_cont_status & CONT_INTRPT)) |
| 3681 | compl_cont_status |= CONT_LOCAL; |
| 3682 | else if (compl_cont_mode != 0) |
| 3683 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3684 | /* FALLTHROUGH */ |
| 3685 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3686 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 3687 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 3688 | * mode). |
| 3689 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 3690 | * value, in both cases ^X^X can be used to restart the same |
| 3691 | * mode (avoiding ADDING mode). |
| 3692 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 3693 | * 'complete' and local ^P expansions respectively. |
| 3694 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 3695 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3696 | if (c == Ctrl_X) |
| 3697 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3698 | if (compl_cont_mode != 0) |
| 3699 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3700 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3701 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3702 | } |
| 3703 | ctrl_x_mode = 0; |
| 3704 | edit_submode = NULL; |
| 3705 | showmode(); |
| 3706 | break; |
| 3707 | } |
| 3708 | } |
| 3709 | else if (ctrl_x_mode != 0) |
| 3710 | { |
| 3711 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 3712 | if (!vim_is_ctrl_x_key(c)) |
| 3713 | { |
| 3714 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 3715 | ctrl_x_mode = 0; |
| 3716 | else |
| 3717 | ctrl_x_mode = CTRL_X_FINISHED; |
| 3718 | edit_submode = NULL; |
| 3719 | } |
| 3720 | showmode(); |
| 3721 | } |
| 3722 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3723 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3724 | { |
| 3725 | /* Show error message from attempted keyword completion (probably |
| 3726 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3727 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3728 | showmode(); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3729 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
| 3730 | && !ins_compl_pum_key(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3731 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 3732 | { |
| 3733 | /* Get here when we have finished typing a sequence of ^N and |
| 3734 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3735 | * memory that was used, and make sure we can redo the insert. */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3736 | if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3737 | { |
| 3738 | /* |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3739 | * If any of the original typed text has been changed, eg when |
| 3740 | * ignorecase is set, we must add back-spaces to the redo |
| 3741 | * buffer. We add as few as necessary to delete just the part |
| 3742 | * of the original text that has changed. |
| 3743 | * When using the longest match, edited the match or used |
| 3744 | * CTRL-E then don't use the current match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3745 | */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3746 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
| 3747 | ptr = compl_curr_match->cp_str; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3748 | else |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 3749 | ptr = NULL; |
| 3750 | ins_compl_fixRedoBufForLeader(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | #ifdef FEAT_CINDENT |
| 3754 | want_cindent = (can_cindent && cindent_on()); |
| 3755 | #endif |
| 3756 | /* |
| 3757 | * When completing whole lines: fix indent for 'cindent'. |
| 3758 | * Otherwise, break line if it's too long. |
| 3759 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3760 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3761 | { |
| 3762 | #ifdef FEAT_CINDENT |
| 3763 | /* re-indent the current line */ |
| 3764 | if (want_cindent) |
| 3765 | { |
| 3766 | do_c_expr_indent(); |
| 3767 | want_cindent = FALSE; /* don't do it again */ |
| 3768 | } |
| 3769 | #endif |
| 3770 | } |
| 3771 | else |
| 3772 | { |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3773 | int prev_col = curwin->w_cursor.col; |
| 3774 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3775 | /* put the cursor on the last char, for 'tw' formatting */ |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3776 | if (prev_col > 0) |
| 3777 | dec_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3778 | if (stop_arrow() == OK) |
| 3779 | insertchar(NUL, 0, -1); |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3780 | if (prev_col > 0 |
| 3781 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 3782 | inc_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3785 | /* If the popup menu is displayed pressing CTRL-Y means accepting |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3786 | * the selection without inserting anything. When |
| 3787 | * compl_enter_selects is set the Enter key does the same. */ |
| 3788 | if ((c == Ctrl_Y || (compl_enter_selects |
| 3789 | && (c == CAR || c == K_KENTER || c == NL))) |
| 3790 | && pum_visible()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3791 | retval = TRUE; |
| 3792 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3793 | /* CTRL-E means completion is Ended, go back to the typed text. */ |
| 3794 | if (c == Ctrl_E) |
| 3795 | { |
| 3796 | ins_compl_delete(); |
| 3797 | if (compl_leader != NULL) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3798 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3799 | else if (compl_first_match != NULL) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3800 | ins_bytes(compl_orig_text + ins_compl_len()); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3801 | retval = TRUE; |
| 3802 | } |
| 3803 | |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 3804 | auto_format(FALSE, TRUE); |
| 3805 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3806 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3807 | compl_started = FALSE; |
| 3808 | compl_matches = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3809 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 3810 | ctrl_x_mode = 0; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3811 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3812 | if (edit_submode != NULL) |
| 3813 | { |
| 3814 | edit_submode = NULL; |
| 3815 | showmode(); |
| 3816 | } |
| 3817 | |
| 3818 | #ifdef FEAT_CINDENT |
| 3819 | /* |
| 3820 | * Indent now if a key was typed that is in 'cinkeys'. |
| 3821 | */ |
| 3822 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 3823 | do_c_expr_indent(); |
| 3824 | #endif |
| 3825 | } |
| 3826 | } |
| 3827 | |
| 3828 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 3829 | * (re)set properly in ins_complete() */ |
| 3830 | if (!vim_is_ctrl_x_key(c)) |
| 3831 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3832 | compl_cont_status = 0; |
| 3833 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3834 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3835 | |
| 3836 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | /* |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 3840 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 3841 | * text. This inserts backspaces and appends the changed text. |
| 3842 | * "ptr" is the known leader text or NUL. |
| 3843 | */ |
| 3844 | static void |
| 3845 | ins_compl_fixRedoBufForLeader(ptr_arg) |
| 3846 | char_u *ptr_arg; |
| 3847 | { |
| 3848 | int len; |
| 3849 | char_u *p; |
| 3850 | char_u *ptr = ptr_arg; |
| 3851 | |
| 3852 | if (ptr == NULL) |
| 3853 | { |
| 3854 | if (compl_leader != NULL) |
| 3855 | ptr = compl_leader; |
| 3856 | else |
| 3857 | return; /* nothing to do */ |
| 3858 | } |
| 3859 | if (compl_orig_text != NULL) |
| 3860 | { |
| 3861 | p = compl_orig_text; |
| 3862 | for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len) |
| 3863 | ; |
| 3864 | #ifdef FEAT_MBYTE |
| 3865 | if (len > 0) |
| 3866 | len -= (*mb_head_off)(p, p + len); |
| 3867 | #endif |
| 3868 | for (p += len; *p != NUL; mb_ptr_adv(p)) |
| 3869 | AppendCharToRedobuff(K_BS); |
| 3870 | } |
| 3871 | else |
| 3872 | len = 0; |
| 3873 | if (ptr != NULL) |
| 3874 | AppendToRedobuffLit(ptr + len, -1); |
| 3875 | } |
| 3876 | |
| 3877 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3878 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3879 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3880 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3881 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3882 | * |
| 3883 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3884 | */ |
| 3885 | static buf_T * |
| 3886 | ins_compl_next_buf(buf, flag) |
| 3887 | buf_T *buf; |
| 3888 | int flag; |
| 3889 | { |
| 3890 | #ifdef FEAT_WINDOWS |
| 3891 | static win_T *wp; |
| 3892 | #endif |
| 3893 | |
| 3894 | if (flag == 'w') /* just windows */ |
| 3895 | { |
| 3896 | #ifdef FEAT_WINDOWS |
| 3897 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 3898 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3899 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3900 | && wp->w_buffer->b_scanned) |
| 3901 | ; |
| 3902 | buf = wp->w_buffer; |
| 3903 | #else |
| 3904 | buf = curbuf; |
| 3905 | #endif |
| 3906 | } |
| 3907 | else |
| 3908 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3909 | * (unlisted buffers) |
| 3910 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3911 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3912 | && ((flag == 'U' |
| 3913 | ? buf->b_p_bl |
| 3914 | : (!buf->b_p_bl |
| 3915 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3916 | || buf->b_scanned)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3917 | ; |
| 3918 | return buf; |
| 3919 | } |
| 3920 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3921 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3922 | static void expand_by_function __ARGS((int type, char_u *base)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3923 | |
| 3924 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3925 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3926 | * get matches in "matches". |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3927 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3928 | static void |
| 3929 | expand_by_function(type, base) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3930 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3931 | char_u *base; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3932 | { |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3933 | list_T *matchlist = NULL; |
| 3934 | dict_T *matchdict = NULL; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3935 | char_u *args[2]; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3936 | char_u *funcname; |
| 3937 | pos_T pos; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3938 | win_T *curwin_save; |
| 3939 | buf_T *curbuf_save; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3940 | typval_T rettv; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3941 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3942 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3943 | if (*funcname == NUL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3944 | return; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3945 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3946 | /* Call 'completefunc' to obtain the list of matches. */ |
| 3947 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3948 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3949 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3950 | pos = curwin->w_cursor; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3951 | curwin_save = curwin; |
| 3952 | curbuf_save = curbuf; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3953 | |
| 3954 | /* Call a function, which returns a list or dict. */ |
| 3955 | if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK) |
| 3956 | { |
| 3957 | switch (rettv.v_type) |
| 3958 | { |
| 3959 | case VAR_LIST: |
| 3960 | matchlist = rettv.vval.v_list; |
| 3961 | break; |
| 3962 | case VAR_DICT: |
| 3963 | matchdict = rettv.vval.v_dict; |
| 3964 | break; |
| 3965 | default: |
| 3966 | /* TODO: Give error message? */ |
| 3967 | clear_tv(&rettv); |
| 3968 | break; |
| 3969 | } |
| 3970 | } |
| 3971 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3972 | if (curwin_save != curwin || curbuf_save != curbuf) |
| 3973 | { |
| 3974 | EMSG(_(e_complwin)); |
| 3975 | goto theend; |
| 3976 | } |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3977 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3978 | check_cursor(); |
| 3979 | if (!equalpos(curwin->w_cursor, pos)) |
| 3980 | { |
| 3981 | EMSG(_(e_compldel)); |
| 3982 | goto theend; |
| 3983 | } |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3984 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3985 | if (matchlist != NULL) |
| 3986 | ins_compl_add_list(matchlist); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3987 | else if (matchdict != NULL) |
| 3988 | ins_compl_add_dict(matchdict); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3989 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3990 | theend: |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3991 | if (matchdict != NULL) |
| 3992 | dict_unref(matchdict); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3993 | if (matchlist != NULL) |
| 3994 | list_unref(matchlist); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3995 | } |
| 3996 | #endif /* FEAT_COMPL_FUNC */ |
| 3997 | |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3998 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3999 | /* |
| 4000 | * Add completions from a list. |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4001 | */ |
| 4002 | static void |
| 4003 | ins_compl_add_list(list) |
| 4004 | list_T *list; |
| 4005 | { |
| 4006 | listitem_T *li; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4007 | int dir = compl_direction; |
| 4008 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4009 | /* Go through the List with matches and add each of them. */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4010 | for (li = list->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4011 | { |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4012 | if (ins_compl_add_tv(&li->li_tv, dir) == OK) |
| 4013 | /* if dir was BACKWARD then honor it just once */ |
| 4014 | dir = FORWARD; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 4015 | else if (did_emsg) |
| 4016 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4017 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4018 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4019 | |
| 4020 | /* |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4021 | * Add completions from a dict. |
| 4022 | */ |
| 4023 | static void |
| 4024 | ins_compl_add_dict(dict) |
| 4025 | dict_T *dict; |
| 4026 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4027 | dictitem_T *di_refresh; |
| 4028 | dictitem_T *di_words; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4029 | |
| 4030 | /* Check for optional "refresh" item. */ |
| 4031 | compl_opt_refresh_always = FALSE; |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4032 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 4033 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4034 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4035 | char_u *v = di_refresh->di_tv.vval.v_string; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4036 | |
| 4037 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 4038 | compl_opt_refresh_always = TRUE; |
| 4039 | } |
| 4040 | |
| 4041 | /* Add completions from a "words" list. */ |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4042 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 4043 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 4044 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /* |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4048 | * Add a match to the list of matches from a typeval_T. |
| 4049 | * If the given string is already in the list of completions, then return |
| 4050 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 4051 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 4052 | */ |
| 4053 | int |
| 4054 | ins_compl_add_tv(tv, dir) |
| 4055 | typval_T *tv; |
| 4056 | int dir; |
| 4057 | { |
| 4058 | char_u *word; |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 4059 | int icase = FALSE; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4060 | int adup = FALSE; |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4061 | int aempty = FALSE; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4062 | char_u *(cptext[CPT_COUNT]); |
| 4063 | |
| 4064 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 4065 | { |
| 4066 | word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE); |
| 4067 | cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict, |
| 4068 | (char_u *)"abbr", FALSE); |
| 4069 | cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict, |
| 4070 | (char_u *)"menu", FALSE); |
| 4071 | cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict, |
| 4072 | (char_u *)"kind", FALSE); |
| 4073 | cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict, |
| 4074 | (char_u *)"info", FALSE); |
| 4075 | if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL) |
| 4076 | icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase"); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 4077 | 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] | 4078 | adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup"); |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4079 | if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL) |
| 4080 | aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty"); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4081 | } |
| 4082 | else |
| 4083 | { |
| 4084 | word = get_tv_string_chk(tv); |
| 4085 | vim_memset(cptext, 0, sizeof(cptext)); |
| 4086 | } |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4087 | if (word == NULL || (!aempty && *word == NUL)) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4088 | return FAIL; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4089 | return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4090 | } |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4091 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4092 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4093 | /* |
| 4094 | * Get the next expansion(s), using "compl_pattern". |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4095 | * The search starts at position "ini" in curbuf and in the direction |
| 4096 | * compl_direction. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4097 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4098 | * where we stopped searching before. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4099 | * This may return before finding all the matches. |
| 4100 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4101 | */ |
| 4102 | static int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4103 | ins_compl_get_exp(ini) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4104 | pos_T *ini; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4105 | { |
| 4106 | static pos_T first_match_pos; |
| 4107 | static pos_T last_match_pos; |
| 4108 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4109 | static int found_all = FALSE; /* Found all matches of a |
| 4110 | certain type. */ |
| 4111 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4112 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4113 | pos_T *pos; |
| 4114 | char_u **matches; |
| 4115 | int save_p_scs; |
| 4116 | int save_p_ws; |
| 4117 | int save_p_ic; |
| 4118 | int i; |
| 4119 | int num_matches; |
| 4120 | int len; |
| 4121 | int found_new_match; |
| 4122 | int type = ctrl_x_mode; |
| 4123 | char_u *ptr; |
| 4124 | char_u *dict = NULL; |
| 4125 | int dict_f = 0; |
| 4126 | compl_T *old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4127 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4128 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4129 | { |
| 4130 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 4131 | ins_buf->b_scanned = 0; |
| 4132 | found_all = FALSE; |
| 4133 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4134 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4135 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4136 | last_match_pos = first_match_pos = *ini; |
| 4137 | } |
| 4138 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4139 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4140 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4141 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 4142 | for (;;) |
| 4143 | { |
| 4144 | found_new_match = FAIL; |
| 4145 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4146 | /* 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] | 4147 | * or if found_all says this entry is done. For ^X^L only use the |
| 4148 | * entries from 'complete' that look in loaded buffers. */ |
| 4149 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4150 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4151 | { |
| 4152 | found_all = FALSE; |
| 4153 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 4154 | e_cpt++; |
| 4155 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 4156 | { |
| 4157 | ins_buf = curbuf; |
| 4158 | first_match_pos = *ini; |
| 4159 | /* So that ^N can match word immediately after cursor */ |
| 4160 | if (ctrl_x_mode == 0) |
| 4161 | dec(&first_match_pos); |
| 4162 | last_match_pos = first_match_pos; |
| 4163 | type = 0; |
| 4164 | } |
| 4165 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 4166 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 4167 | { |
| 4168 | /* Scan a buffer, but not the current one. */ |
| 4169 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 4170 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4171 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4172 | first_match_pos.col = last_match_pos.col = 0; |
| 4173 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 4174 | last_match_pos.lnum = 0; |
| 4175 | type = 0; |
| 4176 | } |
| 4177 | else /* unloaded buffer, scan like dictionary */ |
| 4178 | { |
| 4179 | found_all = TRUE; |
| 4180 | if (ins_buf->b_fname == NULL) |
| 4181 | continue; |
| 4182 | type = CTRL_X_DICTIONARY; |
| 4183 | dict = ins_buf->b_fname; |
| 4184 | dict_f = DICT_EXACT; |
| 4185 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4186 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4187 | ins_buf->b_fname == NULL |
| 4188 | ? buf_spname(ins_buf) |
| 4189 | : ins_buf->b_sfname == NULL |
| 4190 | ? (char *)ins_buf->b_fname |
| 4191 | : (char *)ins_buf->b_sfname); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 4192 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4193 | } |
| 4194 | else if (*e_cpt == NUL) |
| 4195 | break; |
| 4196 | else |
| 4197 | { |
| 4198 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4199 | type = -1; |
| 4200 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 4201 | { |
| 4202 | if (*e_cpt == 'k') |
| 4203 | type = CTRL_X_DICTIONARY; |
| 4204 | else |
| 4205 | type = CTRL_X_THESAURUS; |
| 4206 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 4207 | { |
| 4208 | dict = e_cpt; |
| 4209 | dict_f = DICT_FIRST; |
| 4210 | } |
| 4211 | } |
| 4212 | #ifdef FEAT_FIND_ID |
| 4213 | else if (*e_cpt == 'i') |
| 4214 | type = CTRL_X_PATH_PATTERNS; |
| 4215 | else if (*e_cpt == 'd') |
| 4216 | type = CTRL_X_PATH_DEFINES; |
| 4217 | #endif |
| 4218 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 4219 | { |
| 4220 | type = CTRL_X_TAGS; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4221 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 4222 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4223 | } |
| 4224 | else |
| 4225 | type = -1; |
| 4226 | |
| 4227 | /* in any case e_cpt is advanced to the next entry */ |
| 4228 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 4229 | |
| 4230 | found_all = TRUE; |
| 4231 | if (type == -1) |
| 4232 | continue; |
| 4233 | } |
| 4234 | } |
| 4235 | |
| 4236 | switch (type) |
| 4237 | { |
| 4238 | case -1: |
| 4239 | break; |
| 4240 | #ifdef FEAT_FIND_ID |
| 4241 | case CTRL_X_PATH_PATTERNS: |
| 4242 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4243 | find_pattern_in_path(compl_pattern, compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4244 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4245 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4246 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4247 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 4248 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 4249 | break; |
| 4250 | #endif |
| 4251 | |
| 4252 | case CTRL_X_DICTIONARY: |
| 4253 | case CTRL_X_THESAURUS: |
| 4254 | ins_compl_dictionaries( |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 4255 | dict != NULL ? dict |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4256 | : (type == CTRL_X_THESAURUS |
| 4257 | ? (*curbuf->b_p_tsr == NUL |
| 4258 | ? p_tsr |
| 4259 | : curbuf->b_p_tsr) |
| 4260 | : (*curbuf->b_p_dict == NUL |
| 4261 | ? p_dict |
| 4262 | : curbuf->b_p_dict)), |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4263 | compl_pattern, |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 4264 | dict != NULL ? dict_f |
| 4265 | : 0, type == CTRL_X_THESAURUS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4266 | dict = NULL; |
| 4267 | break; |
| 4268 | |
| 4269 | case CTRL_X_TAGS: |
| 4270 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 4271 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4272 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4273 | |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4274 | /* Find up to TAG_MANY matches. Avoids that an enormous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4275 | * of matches is found when compl_pattern is empty */ |
| 4276 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4277 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 4278 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 4279 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 4280 | { |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4281 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4282 | } |
| 4283 | p_ic = save_p_ic; |
| 4284 | break; |
| 4285 | |
| 4286 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4287 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 4289 | { |
| 4290 | |
| 4291 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4292 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4293 | ins_compl_add_matches(num_matches, matches, |
| 4294 | #ifdef CASE_INSENSITIVE_FILENAME |
| 4295 | TRUE |
| 4296 | #else |
| 4297 | FALSE |
| 4298 | #endif |
| 4299 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4300 | } |
| 4301 | break; |
| 4302 | |
| 4303 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4304 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 4305 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4306 | &num_matches, &matches) == EXPAND_OK) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4307 | ins_compl_add_matches(num_matches, matches, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4308 | break; |
| 4309 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4310 | #ifdef FEAT_COMPL_FUNC |
| 4311 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4312 | case CTRL_X_OMNI: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4313 | expand_by_function(type, compl_pattern); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4314 | break; |
| 4315 | #endif |
| 4316 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4317 | case CTRL_X_SPELL: |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 4318 | #ifdef FEAT_SPELL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4319 | num_matches = expand_spelling(first_match_pos.lnum, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4320 | compl_pattern, &matches); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4321 | if (num_matches > 0) |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4322 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4323 | #endif |
| 4324 | break; |
| 4325 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4326 | default: /* normal ^P/^N and ^X^L */ |
| 4327 | /* |
| 4328 | * If 'infercase' is set, don't use 'smartcase' here |
| 4329 | */ |
| 4330 | save_p_scs = p_scs; |
| 4331 | if (ins_buf->b_p_inf) |
| 4332 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4333 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4334 | /* buffers other than curbuf are scanned from the beginning or the |
| 4335 | * end but never from the middle, thus setting nowrapscan in this |
| 4336 | * buffers is a good idea, on the other hand, we always set |
| 4337 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 4338 | save_p_ws = p_ws; |
| 4339 | if (ins_buf != curbuf) |
| 4340 | p_ws = FALSE; |
| 4341 | else if (*e_cpt == '.') |
| 4342 | p_ws = TRUE; |
| 4343 | for (;;) |
| 4344 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4345 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4346 | |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 4347 | ++msg_silent; /* Don't want messages for wrapscan. */ |
| 4348 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4349 | /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that |
| 4350 | * has added a word that was at the beginning of the line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4351 | if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4352 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4353 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4354 | compl_direction, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4355 | else |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4356 | found_new_match = searchit(NULL, ins_buf, pos, |
| 4357 | compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4358 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 4359 | RE_LAST, (linenr_T)0, NULL); |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 4360 | --msg_silent; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4361 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4362 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4363 | /* set "compl_started" even on fail */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4364 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4365 | first_match_pos = *pos; |
| 4366 | last_match_pos = *pos; |
| 4367 | } |
| 4368 | else if (first_match_pos.lnum == last_match_pos.lnum |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4369 | && first_match_pos.col == last_match_pos.col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4370 | found_new_match = FAIL; |
| 4371 | if (found_new_match == FAIL) |
| 4372 | { |
| 4373 | if (ins_buf == curbuf) |
| 4374 | found_all = TRUE; |
| 4375 | break; |
| 4376 | } |
| 4377 | |
| 4378 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4379 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4380 | && ini->lnum == pos->lnum |
| 4381 | && ini->col == pos->col) |
| 4382 | continue; |
| 4383 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 4384 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4385 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4386 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4387 | { |
| 4388 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 4389 | continue; |
| 4390 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 4391 | if (!p_paste) |
| 4392 | ptr = skipwhite(ptr); |
| 4393 | } |
| 4394 | len = (int)STRLEN(ptr); |
| 4395 | } |
| 4396 | else |
| 4397 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4398 | char_u *tmp_ptr = ptr; |
| 4399 | |
| 4400 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4401 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4402 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4403 | /* Skip if already inside a word. */ |
| 4404 | if (vim_iswordp(tmp_ptr)) |
| 4405 | continue; |
| 4406 | /* Find start of next word. */ |
| 4407 | tmp_ptr = find_word_start(tmp_ptr); |
| 4408 | } |
| 4409 | /* Find end of this word. */ |
| 4410 | tmp_ptr = find_word_end(tmp_ptr); |
| 4411 | len = (int)(tmp_ptr - ptr); |
| 4412 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4413 | if ((compl_cont_status & CONT_ADDING) |
| 4414 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4415 | { |
| 4416 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 4417 | { |
| 4418 | /* Try next line, if any. the new word will be |
| 4419 | * "join" as if the normal command "J" was used. |
| 4420 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4421 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4422 | * works -- Acevedo */ |
| 4423 | STRNCPY(IObuff, ptr, len); |
| 4424 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 4425 | tmp_ptr = ptr = skipwhite(ptr); |
| 4426 | /* Find start of next word. */ |
| 4427 | tmp_ptr = find_word_start(tmp_ptr); |
| 4428 | /* Find end of next word. */ |
| 4429 | tmp_ptr = find_word_end(tmp_ptr); |
| 4430 | if (tmp_ptr > ptr) |
| 4431 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4432 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4434 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4435 | IObuff[len++] = ' '; |
| 4436 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 4437 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4438 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4439 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 4440 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4441 | && (IObuff[len - 2] == '?' |
| 4442 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4443 | IObuff[len++] = ' '; |
| 4444 | } |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4445 | /* copy as much as possible of the new word */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4446 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 4447 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 4448 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 4449 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4450 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | } |
| 4452 | IObuff[len] = NUL; |
| 4453 | ptr = IObuff; |
| 4454 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4455 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4456 | continue; |
| 4457 | } |
| 4458 | } |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4459 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4460 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4461 | 0, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4462 | { |
| 4463 | found_new_match = OK; |
| 4464 | break; |
| 4465 | } |
| 4466 | } |
| 4467 | p_scs = save_p_scs; |
| 4468 | p_ws = save_p_ws; |
| 4469 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4470 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4471 | /* check if compl_curr_match has changed, (e.g. other type of |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 4472 | * expansion added something) */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4473 | if (type != 0 && compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4474 | found_new_match = OK; |
| 4475 | |
| 4476 | /* break the loop for specialized modes (use 'complete' just for the |
| 4477 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
| 4478 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4479 | || found_new_match != FAIL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4480 | { |
| 4481 | if (got_int) |
| 4482 | break; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4483 | /* Fill the popup menu as soon as possible. */ |
Bram Moolenaar | 5948a57 | 2006-10-03 13:49:29 +0000 | [diff] [blame] | 4484 | if (type != -1) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4485 | ins_compl_check_keys(0); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4486 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4487 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 4488 | || compl_interrupted) |
| 4489 | break; |
| 4490 | compl_started = TRUE; |
| 4491 | } |
| 4492 | else |
| 4493 | { |
| 4494 | /* Mark a buffer scanned when it has been scanned completely */ |
| 4495 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 4496 | ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4497 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4498 | compl_started = FALSE; |
| 4499 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4500 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4501 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4502 | |
| 4503 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4504 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 4505 | found_new_match = FAIL; |
| 4506 | |
| 4507 | i = -1; /* total of matches, unknown */ |
| 4508 | if (found_new_match == FAIL |
| 4509 | || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) |
| 4510 | i = ins_compl_make_cyclic(); |
| 4511 | |
| 4512 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4513 | * just been made cyclic then we have to move compl_curr_match to the next |
| 4514 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4515 | compl_curr_match = compl_direction == FORWARD ? old_match->cp_next |
| 4516 | : old_match->cp_prev; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4517 | if (compl_curr_match == NULL) |
| 4518 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4519 | return i; |
| 4520 | } |
| 4521 | |
| 4522 | /* Delete the old text being completed. */ |
| 4523 | static void |
| 4524 | ins_compl_delete() |
| 4525 | { |
| 4526 | int i; |
| 4527 | |
| 4528 | /* |
| 4529 | * In insert mode: Delete the typed part. |
| 4530 | * In replace mode: Put the old characters back, if any. |
| 4531 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4532 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4533 | backspace_until_column(i); |
| 4534 | changed_cline_bef_curs(); |
| 4535 | } |
| 4536 | |
| 4537 | /* Insert the new text being completed. */ |
| 4538 | static void |
| 4539 | ins_compl_insert() |
| 4540 | { |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 4541 | ins_bytes(compl_shown_match->cp_str + ins_compl_len()); |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 4542 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 4543 | compl_used_match = FALSE; |
| 4544 | else |
| 4545 | compl_used_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | /* |
| 4549 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4550 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 4551 | * get more completions. If it is FALSE, then we just do nothing when there |
| 4552 | * are no more completions in a given direction. The latter case is used when |
| 4553 | * we are still in the middle of finding completions, to allow browsing |
| 4554 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4555 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 4556 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4557 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 4558 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4559 | * |
| 4560 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4561 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 4562 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4563 | */ |
| 4564 | static int |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4565 | ins_compl_next(allow_get_expansion, count, insert_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4566 | int allow_get_expansion; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4567 | int count; /* repeat completion this many times; should |
| 4568 | be at least 1 */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4569 | int insert_match; /* Insert the newly selected match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4570 | { |
| 4571 | int num_matches = -1; |
| 4572 | int i; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4573 | int todo = count; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4574 | compl_T *found_compl = NULL; |
| 4575 | int found_end = FALSE; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4576 | int advance; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4577 | |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 4578 | /* When user complete function return -1 for findstart which is next |
| 4579 | * time of 'always', compl_shown_match become NULL. */ |
| 4580 | if (compl_shown_match == NULL) |
| 4581 | return -1; |
| 4582 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4583 | if (compl_leader != NULL |
| 4584 | && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4585 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4586 | /* Set "compl_shown_match" to the actually shown match, it may differ |
| 4587 | * when "compl_leader" is used to omit some of the matches. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4588 | while (!ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4589 | compl_leader, (int)STRLEN(compl_leader)) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4590 | && compl_shown_match->cp_next != NULL |
| 4591 | && compl_shown_match->cp_next != compl_first_match) |
| 4592 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 4593 | |
| 4594 | /* If we didn't find it searching forward, and compl_shows_dir is |
| 4595 | * backward, find the last match. */ |
| 4596 | if (compl_shows_dir == BACKWARD |
| 4597 | && !ins_compl_equal(compl_shown_match, |
| 4598 | compl_leader, (int)STRLEN(compl_leader)) |
| 4599 | && (compl_shown_match->cp_next == NULL |
| 4600 | || compl_shown_match->cp_next == compl_first_match)) |
| 4601 | { |
| 4602 | while (!ins_compl_equal(compl_shown_match, |
| 4603 | compl_leader, (int)STRLEN(compl_leader)) |
| 4604 | && compl_shown_match->cp_prev != NULL |
| 4605 | && compl_shown_match->cp_prev != compl_first_match) |
| 4606 | compl_shown_match = compl_shown_match->cp_prev; |
| 4607 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | if (allow_get_expansion && insert_match |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4611 | && (!(compl_get_longest || compl_restarting) || compl_used_match)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4612 | /* Delete old text to be replaced */ |
| 4613 | ins_compl_delete(); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4614 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4615 | /* When finding the longest common text we stick at the original text, |
| 4616 | * don't let CTRL-N or CTRL-P move to the first match. */ |
| 4617 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 4618 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4619 | /* When restarting the search don't insert the first match either. */ |
| 4620 | if (compl_restarting) |
| 4621 | { |
| 4622 | advance = FALSE; |
| 4623 | compl_restarting = FALSE; |
| 4624 | } |
| 4625 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4626 | /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 4627 | * around. */ |
| 4628 | while (--todo >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4629 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4630 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4631 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4632 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4633 | found_end = (compl_first_match != NULL |
| 4634 | && (compl_shown_match->cp_next == compl_first_match |
| 4635 | || compl_shown_match == compl_first_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4636 | } |
| 4637 | else if (compl_shows_dir == BACKWARD |
| 4638 | && compl_shown_match->cp_prev != NULL) |
| 4639 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4640 | found_end = (compl_shown_match == compl_first_match); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4641 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4642 | found_end |= (compl_shown_match == compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4643 | } |
| 4644 | else |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4645 | { |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4646 | if (!allow_get_expansion) |
| 4647 | { |
| 4648 | if (advance) |
| 4649 | { |
| 4650 | if (compl_shows_dir == BACKWARD) |
| 4651 | compl_pending -= todo + 1; |
| 4652 | else |
| 4653 | compl_pending += todo + 1; |
| 4654 | } |
| 4655 | return -1; |
| 4656 | } |
| 4657 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4658 | if (advance) |
| 4659 | { |
| 4660 | if (compl_shows_dir == BACKWARD) |
| 4661 | --compl_pending; |
| 4662 | else |
| 4663 | ++compl_pending; |
| 4664 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4665 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4666 | /* Find matches. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4667 | num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4668 | |
| 4669 | /* handle any pending completions */ |
| 4670 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4671 | && advance) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4672 | { |
| 4673 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 4674 | { |
| 4675 | compl_shown_match = compl_shown_match->cp_next; |
| 4676 | --compl_pending; |
| 4677 | } |
| 4678 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 4679 | { |
| 4680 | compl_shown_match = compl_shown_match->cp_prev; |
| 4681 | ++compl_pending; |
| 4682 | } |
| 4683 | else |
| 4684 | break; |
| 4685 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4686 | found_end = FALSE; |
| 4687 | } |
| 4688 | if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 |
| 4689 | && compl_leader != NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4690 | && !ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4691 | compl_leader, (int)STRLEN(compl_leader))) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4692 | ++todo; |
| 4693 | else |
| 4694 | /* Remember a matching item. */ |
| 4695 | found_compl = compl_shown_match; |
| 4696 | |
| 4697 | /* Stop at the end of the list when we found a usable match. */ |
| 4698 | if (found_end) |
| 4699 | { |
| 4700 | if (found_compl != NULL) |
| 4701 | { |
| 4702 | compl_shown_match = found_compl; |
| 4703 | break; |
| 4704 | } |
| 4705 | todo = 1; /* use first usable match after wrapping around */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4706 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4709 | /* Insert the text of the new completion, or the compl_leader. */ |
| 4710 | if (insert_match) |
| 4711 | { |
| 4712 | if (!compl_get_longest || compl_used_match) |
| 4713 | ins_compl_insert(); |
| 4714 | else |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 4715 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4716 | } |
| 4717 | else |
| 4718 | compl_used_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4719 | |
| 4720 | if (!allow_get_expansion) |
| 4721 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4722 | /* may undisplay the popup menu first */ |
| 4723 | ins_compl_upd_pum(); |
| 4724 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4725 | /* redraw to show the user what was inserted */ |
| 4726 | update_screen(0); |
| 4727 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4728 | /* display the updated popup menu */ |
| 4729 | ins_compl_show_pum(); |
Bram Moolenaar | 1471681 | 2006-05-04 21:54:08 +0000 | [diff] [blame] | 4730 | #ifdef FEAT_GUI |
| 4731 | if (gui.in_use) |
| 4732 | { |
| 4733 | /* Show the cursor after the match, not after the redrawn text. */ |
| 4734 | setcursor(); |
| 4735 | out_flush(); |
| 4736 | gui_update_cursor(FALSE, FALSE); |
| 4737 | } |
| 4738 | #endif |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4739 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4740 | /* Delete old text to be replaced, since we're still searching and |
| 4741 | * don't want to match ourselves! */ |
| 4742 | ins_compl_delete(); |
| 4743 | } |
| 4744 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4745 | /* Enter will select a match when the match wasn't inserted and the popup |
Bram Moolenaar | 34e0bfa | 2007-05-10 18:44:18 +0000 | [diff] [blame] | 4746 | * menu is visible. */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4747 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 4748 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4749 | /* |
| 4750 | * Show the file name for the match (if any) |
| 4751 | * Truncate the file name to avoid a wait for return. |
| 4752 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4753 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | { |
| 4755 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4756 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | if (i <= 0) |
| 4758 | i = 0; |
| 4759 | else |
| 4760 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4761 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4762 | msg(IObuff); |
| 4763 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 4764 | } |
| 4765 | |
| 4766 | return num_matches; |
| 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * Call this while finding completions, to check whether the user has hit a key |
| 4771 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4772 | * 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] | 4773 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4774 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4775 | */ |
| 4776 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4777 | ins_compl_check_keys(frequency) |
| 4778 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4779 | { |
| 4780 | static int count = 0; |
| 4781 | |
| 4782 | int c; |
| 4783 | |
| 4784 | /* Don't check when reading keys from a script. That would break the test |
| 4785 | * scripts */ |
| 4786 | if (using_script()) |
| 4787 | return; |
| 4788 | |
| 4789 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4790 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4791 | return; |
| 4792 | count = 0; |
| 4793 | |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4794 | /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 4795 | * can't do its work correctly. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4796 | c = vpeekc_any(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4797 | if (c != NUL) |
| 4798 | { |
| 4799 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 4800 | { |
| 4801 | c = safe_vgetc(); /* Eat the character */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4802 | compl_shows_dir = ins_compl_key2dir(c); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4803 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 4804 | c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4806 | else |
| 4807 | { |
| 4808 | /* Need to get the character to have KeyTyped set. We'll put it |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4809 | * back with vungetc() below. But skip K_IGNORE. */ |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4810 | c = safe_vgetc(); |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4811 | if (c != K_IGNORE) |
| 4812 | { |
| 4813 | /* Don't interrupt completion when the character wasn't typed, |
| 4814 | * e.g., when doing @q to replay keys. */ |
| 4815 | if (c != Ctrl_R && KeyTyped) |
| 4816 | compl_interrupted = TRUE; |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4817 | |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4818 | vungetc(c); |
| 4819 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4820 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4821 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4822 | if (compl_pending != 0 && !got_int) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4823 | { |
| 4824 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 4825 | |
| 4826 | compl_pending = 0; |
| 4827 | (void)ins_compl_next(FALSE, todo, TRUE); |
| 4828 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | /* |
| 4832 | * Decide the direction of Insert mode complete from the key typed. |
| 4833 | * Returns BACKWARD or FORWARD. |
| 4834 | */ |
| 4835 | static int |
| 4836 | ins_compl_key2dir(c) |
| 4837 | int c; |
| 4838 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4839 | if (c == Ctrl_P || c == Ctrl_L |
| 4840 | || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP |
| 4841 | || c == K_S_UP || c == K_UP))) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4842 | return BACKWARD; |
| 4843 | return FORWARD; |
| 4844 | } |
| 4845 | |
| 4846 | /* |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4847 | * Return TRUE for keys that are used for completion only when the popup menu |
| 4848 | * is visible. |
| 4849 | */ |
| 4850 | static int |
| 4851 | ins_compl_pum_key(c) |
| 4852 | int c; |
| 4853 | { |
| 4854 | 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] | 4855 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 4856 | || c == K_UP || c == K_DOWN); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | /* |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4860 | * Decide the number of completions to move forward. |
| 4861 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 4862 | */ |
| 4863 | static int |
| 4864 | ins_compl_key2count(c) |
| 4865 | int c; |
| 4866 | { |
| 4867 | int h; |
| 4868 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4869 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4870 | { |
| 4871 | h = pum_get_height(); |
| 4872 | if (h > 3) |
| 4873 | h -= 2; /* keep some context */ |
| 4874 | return h; |
| 4875 | } |
| 4876 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4880 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 4881 | * to change the currently selected completion. |
| 4882 | */ |
| 4883 | static int |
| 4884 | ins_compl_use_match(c) |
| 4885 | int c; |
| 4886 | { |
| 4887 | switch (c) |
| 4888 | { |
| 4889 | case K_UP: |
| 4890 | case K_DOWN: |
| 4891 | case K_PAGEDOWN: |
| 4892 | case K_KPAGEDOWN: |
| 4893 | case K_S_DOWN: |
| 4894 | case K_PAGEUP: |
| 4895 | case K_KPAGEUP: |
| 4896 | case K_S_UP: |
| 4897 | return FALSE; |
| 4898 | } |
| 4899 | return TRUE; |
| 4900 | } |
| 4901 | |
| 4902 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4903 | * Do Insert mode completion. |
| 4904 | * Called when character "c" was typed, which has a meaning for completion. |
| 4905 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 4906 | */ |
| 4907 | static int |
| 4908 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4909 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4910 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4911 | char_u *line; |
| 4912 | int startcol = 0; /* column where searched text starts */ |
| 4913 | colnr_T curs_col; /* cursor column */ |
| 4914 | int n; |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 4915 | int save_w_wrow; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4916 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4917 | compl_direction = ins_compl_key2dir(c); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4918 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | { |
| 4920 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 4921 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4922 | did_ai = FALSE; |
| 4923 | #ifdef FEAT_SMARTINDENT |
| 4924 | did_si = FALSE; |
| 4925 | can_si = FALSE; |
| 4926 | can_si_back = FALSE; |
| 4927 | #endif |
| 4928 | if (stop_arrow() == FAIL) |
| 4929 | return FAIL; |
| 4930 | |
| 4931 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4932 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4933 | compl_pending = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4934 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 4935 | /* 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] | 4936 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 4937 | * instead of expand the one before the cursor, in word-wise if |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 4938 | * "compl_startpos" is not in the same line as the cursor then fix it |
| 4939 | * (the line has been split because it was longer than 'tw'). if SOL |
| 4940 | * is set then skip the previous pattern, a word at the beginning of |
| 4941 | * the line has been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4942 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 4943 | && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4944 | { |
| 4945 | /* |
| 4946 | * it is a continued search |
| 4947 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4948 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4949 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 4950 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4951 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4952 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4953 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4954 | /* line (probably) wrapped, set compl_startpos to the |
| 4955 | * first non_blank in the line, if it is not a wordchar |
| 4956 | * include it to get a better pattern, but then we don't |
| 4957 | * want the "\\<" prefix, check it bellow */ |
| 4958 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 4959 | compl_startpos.col = compl_col; |
| 4960 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4961 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4962 | } |
| 4963 | else |
| 4964 | { |
| 4965 | /* S_IPOS was set when we inserted a word that was at the |
| 4966 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4967 | * mode but first we need to redefine compl_startpos */ |
| 4968 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4969 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4970 | compl_cont_status |= CONT_SOL; |
| 4971 | compl_startpos.col = (colnr_T)(skipwhite( |
| 4972 | line + compl_length |
| 4973 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4974 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4975 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4976 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4977 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4978 | /* IObuff is used to add a "word from the next line" would we |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 4979 | * have enough space? just being paranoid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4980 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4981 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4983 | compl_cont_status &= ~CONT_SOL; |
| 4984 | compl_length = (IOSIZE - MIN_SPACE); |
| 4985 | compl_col = curwin->w_cursor.col - compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4986 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4987 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 4988 | if (compl_length < 1) |
| 4989 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4990 | } |
| 4991 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4992 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4993 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4994 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4995 | } |
| 4996 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4997 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4998 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4999 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5001 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5002 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5003 | compl_cont_status = 0; |
| 5004 | compl_cont_status |= CONT_N_ADDS; |
| 5005 | compl_startpos = curwin->w_cursor; |
| 5006 | startcol = (int)curs_col; |
| 5007 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | /* Work out completion pattern and original text -- webb */ |
| 5011 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 5012 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5013 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5014 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 5015 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5016 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5017 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5018 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5020 | compl_col += ++startcol; |
| 5021 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5022 | } |
| 5023 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5024 | compl_pattern = str_foldcase(line + compl_col, |
| 5025 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5026 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5027 | compl_pattern = vim_strnsave(line + compl_col, |
| 5028 | compl_length); |
| 5029 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5030 | return FAIL; |
| 5031 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5032 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5033 | { |
| 5034 | char_u *prefix = (char_u *)"\\<"; |
| 5035 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5036 | /* we need up to 2 extra chars for the prefix */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5037 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5038 | compl_length) + 2); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5039 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5040 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5041 | if (!vim_iswordp(line + compl_col) |
| 5042 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5043 | && ( |
| 5044 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5045 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5046 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5047 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5048 | #endif |
| 5049 | ))) |
| 5050 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5051 | STRCPY((char *)compl_pattern, prefix); |
| 5052 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 5053 | line + compl_col, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5054 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5055 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5056 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5057 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5059 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5060 | #endif |
| 5061 | ) |
| 5062 | { |
| 5063 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5064 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 5065 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5066 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5067 | compl_col += curs_col; |
| 5068 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | } |
| 5070 | else |
| 5071 | { |
| 5072 | #ifdef FEAT_MBYTE |
| 5073 | /* Search the point of change class of multibyte character |
| 5074 | * or not a word single byte character backward. */ |
| 5075 | if (has_mbyte) |
| 5076 | { |
| 5077 | int base_class; |
| 5078 | int head_off; |
| 5079 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5080 | startcol -= (*mb_head_off)(line, line + startcol); |
| 5081 | base_class = mb_get_class(line + startcol); |
| 5082 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5083 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5084 | head_off = (*mb_head_off)(line, line + startcol); |
| 5085 | if (base_class != mb_get_class(line + startcol |
| 5086 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5087 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5088 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5089 | } |
| 5090 | } |
| 5091 | else |
| 5092 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5093 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5094 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5095 | compl_col += ++startcol; |
| 5096 | compl_length = (int)curs_col - startcol; |
| 5097 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5098 | { |
| 5099 | /* Only match word with at least two chars -- webb |
| 5100 | * there's no need to call quote_meta, |
| 5101 | * alloc(7) is enough -- Acevedo |
| 5102 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5103 | compl_pattern = alloc(7); |
| 5104 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5105 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5106 | STRCPY((char *)compl_pattern, "\\<"); |
| 5107 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 5108 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | } |
| 5110 | else |
| 5111 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5112 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5113 | compl_length) + 2); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5114 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5115 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5116 | STRCPY((char *)compl_pattern, "\\<"); |
| 5117 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 5118 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 5123 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5124 | compl_col = (colnr_T)(skipwhite(line) - line); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5125 | compl_length = (int)curs_col - (int)compl_col; |
| 5126 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 5127 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5128 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5129 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 5130 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5131 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5132 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5133 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5134 | return FAIL; |
| 5135 | } |
| 5136 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 5137 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5138 | while (--startcol >= 0 && vim_isfilec(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5139 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5140 | compl_col += ++startcol; |
| 5141 | compl_length = (int)curs_col - startcol; |
| 5142 | compl_pattern = addstar(line + compl_col, compl_length, |
| 5143 | EXPAND_FILES); |
| 5144 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5145 | return FAIL; |
| 5146 | } |
| 5147 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 5148 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5149 | compl_pattern = vim_strnsave(line, curs_col); |
| 5150 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5151 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5152 | set_cmd_context(&compl_xp, compl_pattern, |
| 5153 | (int)STRLEN(compl_pattern), curs_col); |
| 5154 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 5155 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 5156 | /* No completion possible, use an empty pattern to get a |
| 5157 | * "pattern not found" message. */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5158 | compl_col = curs_col; |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5159 | else |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 5160 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern); |
| 5161 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5162 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5163 | 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] | 5164 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5165 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5166 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5167 | * Call user defined function 'completefunc' with "a:findstart" |
| 5168 | * 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] | 5169 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5170 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5171 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5172 | char_u *funcname; |
| 5173 | pos_T pos; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5174 | win_T *curwin_save; |
| 5175 | buf_T *curbuf_save; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5176 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5177 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5178 | * string */ |
| 5179 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 5180 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 5181 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5182 | { |
| 5183 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 5184 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5185 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5186 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5187 | |
| 5188 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5189 | args[1] = NULL; |
| 5190 | pos = curwin->w_cursor; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5191 | curwin_save = curwin; |
| 5192 | curbuf_save = curbuf; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5193 | col = call_func_retnr(funcname, 2, args, FALSE); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5194 | if (curwin_save != curwin || curbuf_save != curbuf) |
| 5195 | { |
| 5196 | EMSG(_(e_complwin)); |
| 5197 | return FAIL; |
| 5198 | } |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5199 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5200 | check_cursor(); |
| 5201 | if (!equalpos(curwin->w_cursor, pos)) |
| 5202 | { |
| 5203 | EMSG(_(e_compldel)); |
| 5204 | return FAIL; |
| 5205 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5206 | |
Bram Moolenaar | 6110a00 | 2012-01-26 18:58:38 +0100 | [diff] [blame] | 5207 | /* Return value -2 means the user complete function wants to |
Bram Moolenaar | 8a4c136 | 2012-05-18 16:35:21 +0200 | [diff] [blame] | 5208 | * cancel the complete without an error. |
| 5209 | * Return value -3 does the same as -2 and leaves CTRL-X mode.*/ |
Bram Moolenaar | 6110a00 | 2012-01-26 18:58:38 +0100 | [diff] [blame] | 5210 | if (col == -2) |
| 5211 | return FAIL; |
Bram Moolenaar | 8a4c136 | 2012-05-18 16:35:21 +0200 | [diff] [blame] | 5212 | if (col == -3) |
| 5213 | { |
| 5214 | ctrl_x_mode = 0; |
| 5215 | edit_submode = NULL; |
| 5216 | msg_clr_cmdline(); |
| 5217 | return FAIL; |
| 5218 | } |
Bram Moolenaar | 6110a00 | 2012-01-26 18:58:38 +0100 | [diff] [blame] | 5219 | |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 5220 | /* |
| 5221 | * Reset extended parameters of completion, when start new |
| 5222 | * completion. |
| 5223 | */ |
| 5224 | compl_opt_refresh_always = FALSE; |
| 5225 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5226 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5227 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5228 | compl_col = col; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5229 | if (compl_col > curs_col) |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5230 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5231 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5232 | /* Setup variables for completion. Need to obtain "line" again, |
| 5233 | * it may have become invalid. */ |
| 5234 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5235 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5236 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5237 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5238 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5239 | return FAIL; |
| 5240 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 5241 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 5242 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 5243 | #ifdef FEAT_SPELL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 5244 | if (spell_bad_len > 0) |
| 5245 | compl_col = curs_col - spell_bad_len; |
| 5246 | else |
| 5247 | compl_col = spell_word_start(startcol); |
| 5248 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5249 | { |
| 5250 | compl_length = 0; |
| 5251 | compl_col = curs_col; |
| 5252 | } |
| 5253 | else |
| 5254 | { |
| 5255 | spell_expand_check_cap(compl_col); |
| 5256 | compl_length = (int)curs_col - compl_col; |
| 5257 | } |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 5258 | /* Need to obtain "line" again, it may have become invalid. */ |
| 5259 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 5260 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5261 | if (compl_pattern == NULL) |
| 5262 | #endif |
| 5263 | return FAIL; |
| 5264 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5265 | else |
| 5266 | { |
| 5267 | EMSG2(_(e_intern2), "ins_complete()"); |
| 5268 | return FAIL; |
| 5269 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5270 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5271 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | { |
| 5273 | edit_submode_pre = (char_u *)_(" Adding"); |
| 5274 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 5275 | { |
| 5276 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 5277 | #ifdef FEAT_COMMENTS |
| 5278 | char_u *old = curbuf->b_p_com; |
| 5279 | |
| 5280 | curbuf->b_p_com = (char_u *)""; |
| 5281 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5282 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5283 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5284 | ins_eol('\r'); |
| 5285 | #ifdef FEAT_COMMENTS |
| 5286 | curbuf->b_p_com = old; |
| 5287 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5288 | compl_length = 0; |
| 5289 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5290 | } |
| 5291 | } |
| 5292 | else |
| 5293 | { |
| 5294 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5295 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5298 | if (compl_cont_status & CONT_LOCAL) |
| 5299 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5300 | else |
| 5301 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 5302 | |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 5303 | /* If any of the original typed text has been changed we need to fix |
| 5304 | * the redo buffer. */ |
| 5305 | ins_compl_fixRedoBufForLeader(NULL); |
| 5306 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5307 | /* Always add completion for the original text. */ |
| 5308 | vim_free(compl_orig_text); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5309 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 5310 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 5311 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5312 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5313 | vim_free(compl_pattern); |
| 5314 | compl_pattern = NULL; |
| 5315 | vim_free(compl_orig_text); |
| 5316 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5317 | return FAIL; |
| 5318 | } |
| 5319 | |
| 5320 | /* showmode might reset the internal line pointers, so it must |
| 5321 | * be called before line = ml_get(), or when this address is no |
| 5322 | * longer needed. -- Acevedo. |
| 5323 | */ |
| 5324 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 5325 | edit_submode_highl = HLF_COUNT; |
| 5326 | showmode(); |
| 5327 | edit_submode_extra = NULL; |
| 5328 | out_flush(); |
| 5329 | } |
| 5330 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5331 | compl_shown_match = compl_curr_match; |
| 5332 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5333 | |
| 5334 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 5335 | * Find next match (and following matches). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5336 | */ |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 5337 | save_w_wrow = curwin->w_wrow; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 5338 | 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] | 5339 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5340 | /* may undisplay the popup menu */ |
| 5341 | ins_compl_upd_pum(); |
| 5342 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5343 | if (n > 1) /* all matches have been found */ |
| 5344 | compl_matches = n; |
| 5345 | compl_curr_match = compl_shown_match; |
| 5346 | compl_direction = compl_shows_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5347 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5348 | /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 5349 | * mode. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5350 | if (got_int && !global_busy) |
| 5351 | { |
| 5352 | (void)vgetc(); |
| 5353 | got_int = FALSE; |
| 5354 | } |
| 5355 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5356 | /* 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] | 5357 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5358 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5359 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 5360 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5361 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 5362 | edit_submode_highl = HLF_E; |
| 5363 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 5364 | * because we couldn't expand anything at first place, but if we used |
| 5365 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 5366 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5367 | if ( compl_length > 1 |
| 5368 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5369 | || (ctrl_x_mode != 0 |
| 5370 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 5371 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5372 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5373 | } |
| 5374 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5375 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5376 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5377 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5378 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5379 | |
| 5380 | if (edit_submode_extra == NULL) |
| 5381 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5382 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5383 | { |
| 5384 | edit_submode_extra = (char_u *)_("Back at original"); |
| 5385 | edit_submode_highl = HLF_W; |
| 5386 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5387 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5388 | { |
| 5389 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 5390 | edit_submode_highl = HLF_COUNT; |
| 5391 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5392 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5393 | { |
| 5394 | edit_submode_extra = (char_u *)_("The only match"); |
| 5395 | edit_submode_highl = HLF_COUNT; |
| 5396 | } |
| 5397 | else |
| 5398 | { |
| 5399 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5400 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5401 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5402 | int number = 0; |
| 5403 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5404 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5405 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5406 | { |
| 5407 | /* search backwards for the first valid (!= -1) number. |
| 5408 | * This should normally succeed already at the first loop |
| 5409 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5410 | for (match = compl_curr_match->cp_prev; match != NULL |
| 5411 | && match != compl_first_match; |
| 5412 | match = match->cp_prev) |
| 5413 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5414 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5415 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5416 | break; |
| 5417 | } |
| 5418 | if (match != NULL) |
| 5419 | /* go up and assign all numbers which are not assigned |
| 5420 | * yet */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5421 | for (match = match->cp_next; |
| 5422 | match != NULL && match->cp_number == -1; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5423 | match = match->cp_next) |
| 5424 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5425 | } |
| 5426 | else /* BACKWARD */ |
| 5427 | { |
| 5428 | /* search forwards (upwards) for the first valid (!= -1) |
| 5429 | * number. This should normally succeed already at the |
| 5430 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5431 | for (match = compl_curr_match->cp_next; match != NULL |
| 5432 | && match != compl_first_match; |
| 5433 | match = match->cp_next) |
| 5434 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5436 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5437 | break; |
| 5438 | } |
| 5439 | if (match != NULL) |
| 5440 | /* go down and assign all numbers which are not |
| 5441 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5442 | for (match = match->cp_prev; match |
| 5443 | && match->cp_number == -1; |
| 5444 | match = match->cp_prev) |
| 5445 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5446 | } |
| 5447 | } |
| 5448 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5449 | /* The match should always have a sequence number now, this is |
| 5450 | * just a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5451 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | { |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5453 | /* Space for 10 text chars. + 2x10-digit no.s = 31. |
| 5454 | * Translations may need more than twice that. */ |
| 5455 | static char_u match_ref[81]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5456 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5457 | if (compl_matches > 0) |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5458 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 5459 | _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5460 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5461 | else |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5462 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 5463 | _("match %d"), |
| 5464 | compl_curr_match->cp_number); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | edit_submode_extra = match_ref; |
| 5466 | edit_submode_highl = HLF_R; |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 5467 | if (dollar_vcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5468 | curs_columns(FALSE); |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
| 5473 | /* Show a message about what (completion) mode we're in. */ |
| 5474 | showmode(); |
| 5475 | if (edit_submode_extra != NULL) |
| 5476 | { |
| 5477 | if (!p_smd) |
| 5478 | msg_attr(edit_submode_extra, |
| 5479 | edit_submode_highl < HLF_COUNT |
| 5480 | ? hl_attr(edit_submode_highl) : 0); |
| 5481 | } |
| 5482 | else |
| 5483 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 5484 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5485 | /* Show the popup menu, unless we got interrupted. */ |
| 5486 | if (!compl_interrupted) |
| 5487 | { |
| 5488 | /* RedrawingDisabled may be set when invoked through complete(). */ |
| 5489 | n = RedrawingDisabled; |
| 5490 | RedrawingDisabled = 0; |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 5491 | |
| 5492 | /* If the cursor moved we need to remove the pum first. */ |
| 5493 | setcursor(); |
| 5494 | if (save_w_wrow != curwin->w_wrow) |
| 5495 | ins_compl_del_pum(); |
| 5496 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5497 | ins_compl_show_pum(); |
| 5498 | setcursor(); |
| 5499 | RedrawingDisabled = n; |
| 5500 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 5501 | compl_was_interrupted = compl_interrupted; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5502 | compl_interrupted = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5503 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5504 | return OK; |
| 5505 | } |
| 5506 | |
| 5507 | /* |
| 5508 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 5509 | * If dest is not NULL the chars. are copied there quoting (with |
| 5510 | * a backslash) the metachars, and dest would be NUL terminated. |
| 5511 | * Returns the length (needed) of dest |
| 5512 | */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5513 | static unsigned |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5514 | quote_meta(dest, src, len) |
| 5515 | char_u *dest; |
| 5516 | char_u *src; |
| 5517 | int len; |
| 5518 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5519 | unsigned m = (unsigned)len + 1; /* one extra for the NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5520 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5521 | for ( ; --len >= 0; src++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5522 | { |
| 5523 | switch (*src) |
| 5524 | { |
| 5525 | case '.': |
| 5526 | case '*': |
| 5527 | case '[': |
| 5528 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5529 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5530 | break; |
| 5531 | case '~': |
| 5532 | if (!p_magic) /* quote these only if magic is set */ |
| 5533 | break; |
| 5534 | case '\\': |
| 5535 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5536 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5537 | break; |
| 5538 | case '^': /* currently it's not needed. */ |
| 5539 | case '$': |
| 5540 | m++; |
| 5541 | if (dest != NULL) |
| 5542 | *dest++ = '\\'; |
| 5543 | break; |
| 5544 | } |
| 5545 | if (dest != NULL) |
| 5546 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5547 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5548 | /* Copy remaining bytes of a multibyte character. */ |
| 5549 | if (has_mbyte) |
| 5550 | { |
| 5551 | int i, mb_len; |
| 5552 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5553 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5554 | if (mb_len > 0 && len >= mb_len) |
| 5555 | for (i = 0; i < mb_len; ++i) |
| 5556 | { |
| 5557 | --len; |
| 5558 | ++src; |
| 5559 | if (dest != NULL) |
| 5560 | *dest++ = *src; |
| 5561 | } |
| 5562 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5563 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5564 | } |
| 5565 | if (dest != NULL) |
| 5566 | *dest = NUL; |
| 5567 | |
| 5568 | return m; |
| 5569 | } |
| 5570 | #endif /* FEAT_INS_EXPAND */ |
| 5571 | |
| 5572 | /* |
| 5573 | * Next character is interpreted literally. |
| 5574 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 5575 | * If one or two digits are entered, the next character is given to vungetc(). |
| 5576 | * For Unicode a character > 255 may be returned. |
| 5577 | */ |
| 5578 | int |
| 5579 | get_literal() |
| 5580 | { |
| 5581 | int cc; |
| 5582 | int nc; |
| 5583 | int i; |
| 5584 | int hex = FALSE; |
| 5585 | int octal = FALSE; |
| 5586 | #ifdef FEAT_MBYTE |
| 5587 | int unicode = 0; |
| 5588 | #endif |
| 5589 | |
| 5590 | if (got_int) |
| 5591 | return Ctrl_C; |
| 5592 | |
| 5593 | #ifdef FEAT_GUI |
| 5594 | /* |
| 5595 | * In GUI there is no point inserting the internal code for a special key. |
| 5596 | * It is more useful to insert the string "<KEY>" instead. This would |
| 5597 | * probably be useful in a text window too, but it would not be |
| 5598 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 5599 | */ |
| 5600 | if (gui.in_use) |
| 5601 | ++allow_keys; |
| 5602 | #endif |
| 5603 | #ifdef USE_ON_FLY_SCROLL |
| 5604 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 5605 | #endif |
| 5606 | ++no_mapping; /* don't map the next key hits */ |
| 5607 | cc = 0; |
| 5608 | i = 0; |
| 5609 | for (;;) |
| 5610 | { |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 5611 | nc = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5612 | #ifdef FEAT_CMDL_INFO |
| 5613 | if (!(State & CMDLINE) |
| 5614 | # ifdef FEAT_MBYTE |
| 5615 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 5616 | # endif |
| 5617 | ) |
| 5618 | add_to_showcmd(nc); |
| 5619 | #endif |
| 5620 | if (nc == 'x' || nc == 'X') |
| 5621 | hex = TRUE; |
| 5622 | else if (nc == 'o' || nc == 'O') |
| 5623 | octal = TRUE; |
| 5624 | #ifdef FEAT_MBYTE |
| 5625 | else if (nc == 'u' || nc == 'U') |
| 5626 | unicode = nc; |
| 5627 | #endif |
| 5628 | else |
| 5629 | { |
| 5630 | if (hex |
| 5631 | #ifdef FEAT_MBYTE |
| 5632 | || unicode != 0 |
| 5633 | #endif |
| 5634 | ) |
| 5635 | { |
| 5636 | if (!vim_isxdigit(nc)) |
| 5637 | break; |
| 5638 | cc = cc * 16 + hex2nr(nc); |
| 5639 | } |
| 5640 | else if (octal) |
| 5641 | { |
| 5642 | if (nc < '0' || nc > '7') |
| 5643 | break; |
| 5644 | cc = cc * 8 + nc - '0'; |
| 5645 | } |
| 5646 | else |
| 5647 | { |
| 5648 | if (!VIM_ISDIGIT(nc)) |
| 5649 | break; |
| 5650 | cc = cc * 10 + nc - '0'; |
| 5651 | } |
| 5652 | |
| 5653 | ++i; |
| 5654 | } |
| 5655 | |
| 5656 | if (cc > 255 |
| 5657 | #ifdef FEAT_MBYTE |
| 5658 | && unicode == 0 |
| 5659 | #endif |
| 5660 | ) |
| 5661 | cc = 255; /* limit range to 0-255 */ |
| 5662 | nc = 0; |
| 5663 | |
| 5664 | if (hex) /* hex: up to two chars */ |
| 5665 | { |
| 5666 | if (i >= 2) |
| 5667 | break; |
| 5668 | } |
| 5669 | #ifdef FEAT_MBYTE |
| 5670 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 5671 | { |
| 5672 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 5673 | break; |
| 5674 | } |
| 5675 | #endif |
| 5676 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 5677 | break; |
| 5678 | } |
| 5679 | if (i == 0) /* no number entered */ |
| 5680 | { |
| 5681 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 5682 | { |
| 5683 | cc = '\n'; |
| 5684 | nc = 0; |
| 5685 | } |
| 5686 | else |
| 5687 | { |
| 5688 | cc = nc; |
| 5689 | nc = 0; |
| 5690 | } |
| 5691 | } |
| 5692 | |
| 5693 | if (cc == 0) /* NUL is stored as NL */ |
| 5694 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5695 | #ifdef FEAT_MBYTE |
| 5696 | if (enc_dbcs && (cc & 0xff) == 0) |
| 5697 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 5698 | second byte will cause trouble! */ |
| 5699 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5700 | |
| 5701 | --no_mapping; |
| 5702 | #ifdef FEAT_GUI |
| 5703 | if (gui.in_use) |
| 5704 | --allow_keys; |
| 5705 | #endif |
| 5706 | if (nc) |
| 5707 | vungetc(nc); |
| 5708 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 5709 | return cc; |
| 5710 | } |
| 5711 | |
| 5712 | /* |
| 5713 | * Insert character, taking care of special keys and mod_mask |
| 5714 | */ |
| 5715 | static void |
| 5716 | insert_special(c, allow_modmask, ctrlv) |
| 5717 | int c; |
| 5718 | int allow_modmask; |
| 5719 | int ctrlv; /* c was typed after CTRL-V */ |
| 5720 | { |
| 5721 | char_u *p; |
| 5722 | int len; |
| 5723 | |
| 5724 | /* |
| 5725 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 5726 | * inserted with ins_str(), so as not to replace characters in replace |
| 5727 | * mode. |
| 5728 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 5729 | * unless 'allow_modmask' is TRUE. |
| 5730 | */ |
| 5731 | #ifdef MACOS |
| 5732 | /* Command-key never produces a normal key */ |
| 5733 | if (mod_mask & MOD_MASK_CMD) |
| 5734 | allow_modmask = TRUE; |
| 5735 | #endif |
| 5736 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 5737 | { |
| 5738 | p = get_special_key_name(c, mod_mask); |
| 5739 | len = (int)STRLEN(p); |
| 5740 | c = p[len - 1]; |
| 5741 | if (len > 2) |
| 5742 | { |
| 5743 | if (stop_arrow() == FAIL) |
| 5744 | return; |
| 5745 | p[len - 1] = NUL; |
| 5746 | ins_str(p); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5747 | AppendToRedobuffLit(p, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5748 | ctrlv = FALSE; |
| 5749 | } |
| 5750 | } |
| 5751 | if (stop_arrow() == OK) |
| 5752 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 5753 | } |
| 5754 | |
| 5755 | /* |
| 5756 | * Special characters in this context are those that need processing other |
| 5757 | * than the simple insertion that can be performed here. This includes ESC |
| 5758 | * which terminates the insert, and CR/NL which need special processing to |
| 5759 | * open up a new line. This routine tries to optimize insertions performed by |
| 5760 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 5761 | * stop and defer processing to the "normal" mechanism. |
| 5762 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 5763 | */ |
| 5764 | #ifdef EBCDIC |
| 5765 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 5766 | #else |
| 5767 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 5768 | #endif |
| 5769 | |
| 5770 | #ifdef FEAT_MBYTE |
| 5771 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 5772 | #else |
| 5773 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 5774 | #endif |
| 5775 | |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5776 | /* |
| 5777 | * "flags": INSCHAR_FORMAT - force formatting |
| 5778 | * INSCHAR_CTRLV - char typed just after CTRL-V |
| 5779 | * INSCHAR_NO_FEX - don't use 'formatexpr' |
| 5780 | * |
| 5781 | * NOTE: passes the flags value straight through to internal_format() which, |
| 5782 | * beside INSCHAR_FORMAT (above), is also looking for these: |
| 5783 | * INSCHAR_DO_COM - format comments |
| 5784 | * INSCHAR_COM_LIST - format comments with num list or 2nd line indent |
| 5785 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5786 | void |
| 5787 | insertchar(c, flags, second_indent) |
| 5788 | int c; /* character to insert or NUL */ |
| 5789 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 5790 | int second_indent; /* indent for second line if >= 0 */ |
| 5791 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5792 | int textwidth; |
| 5793 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5794 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5795 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5796 | int fo_ins_blank; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5797 | |
| 5798 | textwidth = comp_textwidth(flags & INSCHAR_FORMAT); |
| 5799 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5800 | |
| 5801 | /* |
| 5802 | * Try to break the line in two or more pieces when: |
| 5803 | * - Always do this if we have been called to do formatting only. |
| 5804 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 5805 | * ends in white space. |
| 5806 | * - Otherwise: |
| 5807 | * - Don't do this if inserting a blank |
| 5808 | * - Don't do this if an existing character is being replaced, unless |
| 5809 | * we're in VREPLACE mode. |
| 5810 | * - Do this if the cursor is not on the line where insert started |
| 5811 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 5812 | * before the insert. |
| 5813 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 5814 | * before 'textwidth' |
| 5815 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5816 | if (textwidth > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5817 | && ((flags & INSCHAR_FORMAT) |
| 5818 | || (!vim_iswhite(c) |
| 5819 | && !((State & REPLACE_FLAG) |
| 5820 | #ifdef FEAT_VREPLACE |
| 5821 | && !(State & VREPLACE_FLAG) |
| 5822 | #endif |
| 5823 | && *ml_get_cursor() != NUL) |
| 5824 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 5825 | || ((!has_format_option(FO_INS_LONG) |
| 5826 | || Insstart_textlen <= (colnr_T)textwidth) |
| 5827 | && (!fo_ins_blank |
| 5828 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 5829 | )))))) |
| 5830 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5831 | /* Format with 'formatexpr' when it's set. Use internal formatting |
| 5832 | * when 'formatexpr' isn't set or it returns non-zero. */ |
| 5833 | #if defined(FEAT_EVAL) |
Bram Moolenaar | f3442e7 | 2006-10-10 13:49:10 +0000 | [diff] [blame] | 5834 | int do_internal = TRUE; |
| 5835 | |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 5836 | if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0) |
Bram Moolenaar | f3442e7 | 2006-10-10 13:49:10 +0000 | [diff] [blame] | 5837 | { |
| 5838 | do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0); |
| 5839 | /* It may be required to save for undo again, e.g. when setline() |
| 5840 | * was called. */ |
| 5841 | ins_need_undo = TRUE; |
| 5842 | } |
| 5843 | if (do_internal) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5844 | #endif |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 5845 | internal_format(textwidth, second_indent, flags, c == NUL, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5846 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5847 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5848 | if (c == NUL) /* only formatting was wanted */ |
| 5849 | return; |
| 5850 | |
| 5851 | #ifdef FEAT_COMMENTS |
| 5852 | /* Check whether this character should end a comment. */ |
| 5853 | if (did_ai && (int)c == end_comment_pending) |
| 5854 | { |
| 5855 | char_u *line; |
| 5856 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 5857 | int middle_len, end_len; |
| 5858 | int i; |
| 5859 | |
| 5860 | /* |
| 5861 | * Need to remove existing (middle) comment leader and insert end |
| 5862 | * comment leader. First, check what comment leader we can find. |
| 5863 | */ |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 5864 | i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5865 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 5866 | { |
| 5867 | /* Skip middle-comment string */ |
| 5868 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 5869 | ++p; |
| 5870 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5871 | /* Don't count trailing white space for middle_len */ |
| 5872 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 5873 | --middle_len; |
| 5874 | |
| 5875 | /* Find the end-comment string */ |
| 5876 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 5877 | ++p; |
| 5878 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5879 | |
| 5880 | /* Skip white space before the cursor */ |
| 5881 | i = curwin->w_cursor.col; |
| 5882 | while (--i >= 0 && vim_iswhite(line[i])) |
| 5883 | ; |
| 5884 | i++; |
| 5885 | |
| 5886 | /* Skip to before the middle leader */ |
| 5887 | i -= middle_len; |
| 5888 | |
| 5889 | /* Check some expected things before we go on */ |
| 5890 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 5891 | { |
| 5892 | /* Backspace over all the stuff we want to replace */ |
| 5893 | backspace_until_column(i); |
| 5894 | |
| 5895 | /* |
| 5896 | * Insert the end-comment string, except for the last |
| 5897 | * character, which will get inserted as normal later. |
| 5898 | */ |
| 5899 | ins_bytes_len(lead_end, end_len - 1); |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | end_comment_pending = NUL; |
| 5904 | #endif |
| 5905 | |
| 5906 | did_ai = FALSE; |
| 5907 | #ifdef FEAT_SMARTINDENT |
| 5908 | did_si = FALSE; |
| 5909 | can_si = FALSE; |
| 5910 | can_si_back = FALSE; |
| 5911 | #endif |
| 5912 | |
| 5913 | /* |
| 5914 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 5915 | * This speeds up normal text input considerably. |
| 5916 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 5917 | * need to re-indent at a ':', or any other character (but not what |
| 5918 | * 'paste' is set).. |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 5919 | * Don't do this when there an InsertCharPre autocommand is defined, |
| 5920 | * because we need to fire the event for every character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5921 | */ |
| 5922 | #ifdef USE_ON_FLY_SCROLL |
| 5923 | dont_scroll = FALSE; /* allow scrolling here */ |
| 5924 | #endif |
| 5925 | |
| 5926 | if ( !ISSPECIAL(c) |
| 5927 | #ifdef FEAT_MBYTE |
| 5928 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 5929 | #endif |
| 5930 | && vpeekc() != NUL |
| 5931 | && !(State & REPLACE_FLAG) |
| 5932 | #ifdef FEAT_CINDENT |
| 5933 | && !cindent_on() |
| 5934 | #endif |
| 5935 | #ifdef FEAT_RIGHTLEFT |
| 5936 | && !p_ri |
| 5937 | #endif |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 5938 | #ifdef FEAT_AUTOCMD |
| 5939 | && !has_insertcharpre() |
| 5940 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5941 | ) |
| 5942 | { |
| 5943 | #define INPUT_BUFLEN 100 |
| 5944 | char_u buf[INPUT_BUFLEN + 1]; |
| 5945 | int i; |
| 5946 | colnr_T virtcol = 0; |
| 5947 | |
| 5948 | buf[0] = c; |
| 5949 | i = 1; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5950 | if (textwidth > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5951 | virtcol = get_nolist_virtcol(); |
| 5952 | /* |
| 5953 | * Stop the string when: |
| 5954 | * - no more chars available |
| 5955 | * - finding a special character (command key) |
| 5956 | * - buffer is full |
| 5957 | * - running into the 'textwidth' boundary |
| 5958 | * - need to check for abbreviation: A non-word char after a word-char |
| 5959 | */ |
| 5960 | while ( (c = vpeekc()) != NUL |
| 5961 | && !ISSPECIAL(c) |
| 5962 | #ifdef FEAT_MBYTE |
| 5963 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 5964 | #endif |
| 5965 | && i < INPUT_BUFLEN |
| 5966 | && (textwidth == 0 |
| 5967 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 5968 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 5969 | { |
| 5970 | #ifdef FEAT_RIGHTLEFT |
| 5971 | c = vgetc(); |
| 5972 | if (p_hkmap && KeyTyped) |
| 5973 | c = hkmap(c); /* Hebrew mode mapping */ |
| 5974 | # ifdef FEAT_FKMAP |
| 5975 | if (p_fkmap && KeyTyped) |
| 5976 | c = fkmap(c); /* Farsi mode mapping */ |
| 5977 | # endif |
| 5978 | buf[i++] = c; |
| 5979 | #else |
| 5980 | buf[i++] = vgetc(); |
| 5981 | #endif |
| 5982 | } |
| 5983 | |
| 5984 | #ifdef FEAT_DIGRAPHS |
| 5985 | do_digraph(-1); /* clear digraphs */ |
| 5986 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 5987 | #endif |
| 5988 | buf[i] = NUL; |
| 5989 | ins_str(buf); |
| 5990 | if (flags & INSCHAR_CTRLV) |
| 5991 | { |
| 5992 | redo_literal(*buf); |
| 5993 | i = 1; |
| 5994 | } |
| 5995 | else |
| 5996 | i = 0; |
| 5997 | if (buf[i] != NUL) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5998 | AppendToRedobuffLit(buf + i, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5999 | } |
| 6000 | else |
| 6001 | { |
| 6002 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6003 | int cc; |
| 6004 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6005 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 6006 | { |
| 6007 | char_u buf[MB_MAXBYTES + 1]; |
| 6008 | |
| 6009 | (*mb_char2bytes)(c, buf); |
| 6010 | buf[cc] = NUL; |
| 6011 | ins_char_bytes(buf, cc); |
| 6012 | AppendCharToRedobuff(c); |
| 6013 | } |
| 6014 | else |
| 6015 | #endif |
| 6016 | { |
| 6017 | ins_char(c); |
| 6018 | if (flags & INSCHAR_CTRLV) |
| 6019 | redo_literal(c); |
| 6020 | else |
| 6021 | AppendCharToRedobuff(c); |
| 6022 | } |
| 6023 | } |
| 6024 | } |
| 6025 | |
| 6026 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6027 | * Format text at the current insert position. |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6028 | * |
| 6029 | * If the INSCHAR_COM_LIST flag is present, then the value of second_indent |
| 6030 | * will be the comment leader length sent to open_line(). |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6031 | */ |
| 6032 | static void |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6033 | internal_format(textwidth, second_indent, flags, format_only, c) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6034 | int textwidth; |
| 6035 | int second_indent; |
| 6036 | int flags; |
| 6037 | int format_only; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6038 | int c; /* character to be inserted (can be NUL) */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6039 | { |
| 6040 | int cc; |
| 6041 | int save_char = NUL; |
| 6042 | int haveto_redraw = FALSE; |
| 6043 | int fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 6044 | #ifdef FEAT_MBYTE |
| 6045 | int fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 6046 | #endif |
| 6047 | int fo_white_par = has_format_option(FO_WHITE_PAR); |
| 6048 | int first_line = TRUE; |
| 6049 | #ifdef FEAT_COMMENTS |
| 6050 | colnr_T leader_len; |
| 6051 | int no_leader = FALSE; |
| 6052 | int do_comments = (flags & INSCHAR_DO_COM); |
| 6053 | #endif |
| 6054 | |
| 6055 | /* |
| 6056 | * When 'ai' is off we don't want a space under the cursor to be |
| 6057 | * deleted. Replace it with an 'x' temporarily. |
| 6058 | */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6059 | if (!curbuf->b_p_ai |
| 6060 | #ifdef FEAT_VREPLACE |
| 6061 | && !(State & VREPLACE_FLAG) |
| 6062 | #endif |
| 6063 | ) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6064 | { |
| 6065 | cc = gchar_cursor(); |
| 6066 | if (vim_iswhite(cc)) |
| 6067 | { |
| 6068 | save_char = cc; |
| 6069 | pchar_cursor('x'); |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | /* |
| 6074 | * Repeat breaking lines, until the current line is not too long. |
| 6075 | */ |
| 6076 | while (!got_int) |
| 6077 | { |
| 6078 | int startcol; /* Cursor column at entry */ |
| 6079 | int wantcol; /* column at textwidth border */ |
| 6080 | int foundcol; /* column for start of spaces */ |
| 6081 | int end_foundcol = 0; /* column for start of word */ |
| 6082 | colnr_T len; |
| 6083 | colnr_T virtcol; |
| 6084 | #ifdef FEAT_VREPLACE |
| 6085 | int orig_col = 0; |
| 6086 | char_u *saved_text = NULL; |
| 6087 | #endif |
| 6088 | colnr_T col; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6089 | colnr_T end_col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6090 | |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6091 | virtcol = get_nolist_virtcol() |
| 6092 | + char2cells(c != NUL ? c : gchar_cursor()); |
| 6093 | if (virtcol <= (colnr_T)textwidth) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6094 | break; |
| 6095 | |
| 6096 | #ifdef FEAT_COMMENTS |
| 6097 | if (no_leader) |
| 6098 | do_comments = FALSE; |
| 6099 | else if (!(flags & INSCHAR_FORMAT) |
| 6100 | && has_format_option(FO_WRAP_COMS)) |
| 6101 | do_comments = TRUE; |
| 6102 | |
| 6103 | /* Don't break until after the comment leader */ |
| 6104 | if (do_comments) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 6105 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6106 | else |
| 6107 | leader_len = 0; |
| 6108 | |
| 6109 | /* If the line doesn't start with a comment leader, then don't |
| 6110 | * start one in a following broken line. Avoids that a %word |
| 6111 | * moved to the start of the next line causes all following lines |
| 6112 | * to start with %. */ |
| 6113 | if (leader_len == 0) |
| 6114 | no_leader = TRUE; |
| 6115 | #endif |
| 6116 | if (!(flags & INSCHAR_FORMAT) |
| 6117 | #ifdef FEAT_COMMENTS |
| 6118 | && leader_len == 0 |
| 6119 | #endif |
| 6120 | && !has_format_option(FO_WRAP)) |
| 6121 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6122 | break; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6123 | if ((startcol = curwin->w_cursor.col) == 0) |
| 6124 | break; |
| 6125 | |
| 6126 | /* find column of textwidth border */ |
| 6127 | coladvance((colnr_T)textwidth); |
| 6128 | wantcol = curwin->w_cursor.col; |
| 6129 | |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6130 | curwin->w_cursor.col = startcol; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6131 | foundcol = 0; |
| 6132 | |
| 6133 | /* |
| 6134 | * Find position to break at. |
| 6135 | * Stop at first entered white when 'formatoptions' has 'v' |
| 6136 | */ |
| 6137 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
Bram Moolenaar | a27ad5a | 2011-10-26 17:04:29 +0200 | [diff] [blame] | 6138 | || (flags & INSCHAR_FORMAT) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6139 | || curwin->w_cursor.lnum != Insstart.lnum |
| 6140 | || curwin->w_cursor.col >= Insstart.col) |
| 6141 | { |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6142 | if (curwin->w_cursor.col == startcol && c != NUL) |
| 6143 | cc = c; |
| 6144 | else |
| 6145 | cc = gchar_cursor(); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6146 | if (WHITECHAR(cc)) |
| 6147 | { |
| 6148 | /* remember position of blank just before text */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6149 | end_col = curwin->w_cursor.col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6150 | |
| 6151 | /* find start of sequence of blanks */ |
| 6152 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 6153 | { |
| 6154 | dec_cursor(); |
| 6155 | cc = gchar_cursor(); |
| 6156 | } |
| 6157 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 6158 | break; /* only spaces in front of text */ |
| 6159 | #ifdef FEAT_COMMENTS |
| 6160 | /* Don't break until after the comment leader */ |
| 6161 | if (curwin->w_cursor.col < leader_len) |
| 6162 | break; |
| 6163 | #endif |
| 6164 | if (has_format_option(FO_ONE_LETTER)) |
| 6165 | { |
| 6166 | /* do not break after one-letter words */ |
| 6167 | if (curwin->w_cursor.col == 0) |
| 6168 | break; /* one-letter word at begin */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6169 | #ifdef FEAT_COMMENTS |
| 6170 | /* do not break "#a b" when 'tw' is 2 */ |
| 6171 | if (curwin->w_cursor.col <= leader_len) |
| 6172 | break; |
| 6173 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6174 | col = curwin->w_cursor.col; |
| 6175 | dec_cursor(); |
| 6176 | cc = gchar_cursor(); |
| 6177 | |
| 6178 | if (WHITECHAR(cc)) |
| 6179 | continue; /* one-letter, continue */ |
| 6180 | curwin->w_cursor.col = col; |
| 6181 | } |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6182 | |
| 6183 | inc_cursor(); |
| 6184 | |
| 6185 | end_foundcol = end_col + 1; |
| 6186 | foundcol = curwin->w_cursor.col; |
| 6187 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6188 | break; |
| 6189 | } |
| 6190 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6191 | else if (cc >= 0x100 && fo_multibyte) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6192 | { |
| 6193 | /* Break after or before a multi-byte character. */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6194 | if (curwin->w_cursor.col != startcol) |
| 6195 | { |
| 6196 | #ifdef FEAT_COMMENTS |
| 6197 | /* Don't break until after the comment leader */ |
| 6198 | if (curwin->w_cursor.col < leader_len) |
| 6199 | break; |
| 6200 | #endif |
| 6201 | col = curwin->w_cursor.col; |
| 6202 | inc_cursor(); |
| 6203 | /* Don't change end_foundcol if already set. */ |
| 6204 | if (foundcol != curwin->w_cursor.col) |
| 6205 | { |
| 6206 | foundcol = curwin->w_cursor.col; |
| 6207 | end_foundcol = foundcol; |
| 6208 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
| 6209 | break; |
| 6210 | } |
| 6211 | curwin->w_cursor.col = col; |
| 6212 | } |
| 6213 | |
| 6214 | if (curwin->w_cursor.col == 0) |
| 6215 | break; |
| 6216 | |
| 6217 | col = curwin->w_cursor.col; |
| 6218 | |
| 6219 | dec_cursor(); |
| 6220 | cc = gchar_cursor(); |
| 6221 | |
| 6222 | if (WHITECHAR(cc)) |
| 6223 | continue; /* break with space */ |
| 6224 | #ifdef FEAT_COMMENTS |
| 6225 | /* Don't break until after the comment leader */ |
| 6226 | if (curwin->w_cursor.col < leader_len) |
| 6227 | break; |
| 6228 | #endif |
| 6229 | |
| 6230 | curwin->w_cursor.col = col; |
| 6231 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6232 | foundcol = curwin->w_cursor.col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6233 | end_foundcol = foundcol; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6234 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
| 6235 | break; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6236 | } |
| 6237 | #endif |
| 6238 | if (curwin->w_cursor.col == 0) |
| 6239 | break; |
| 6240 | dec_cursor(); |
| 6241 | } |
| 6242 | |
| 6243 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 6244 | { |
| 6245 | curwin->w_cursor.col = startcol; |
| 6246 | break; |
| 6247 | } |
| 6248 | |
| 6249 | /* Going to break the line, remove any "$" now. */ |
| 6250 | undisplay_dollar(); |
| 6251 | |
| 6252 | /* |
| 6253 | * Offset between cursor position and line break is used by replace |
| 6254 | * stack functions. VREPLACE does not use this, and backspaces |
| 6255 | * over the text instead. |
| 6256 | */ |
| 6257 | #ifdef FEAT_VREPLACE |
| 6258 | if (State & VREPLACE_FLAG) |
| 6259 | orig_col = startcol; /* Will start backspacing from here */ |
| 6260 | else |
| 6261 | #endif |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6262 | replace_offset = startcol - end_foundcol; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6263 | |
| 6264 | /* |
| 6265 | * adjust startcol for spaces that will be deleted and |
| 6266 | * characters that will remain on top line |
| 6267 | */ |
| 6268 | curwin->w_cursor.col = foundcol; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6269 | while ((cc = gchar_cursor(), WHITECHAR(cc)) |
| 6270 | && (!fo_white_par || curwin->w_cursor.col < startcol)) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6271 | inc_cursor(); |
| 6272 | startcol -= curwin->w_cursor.col; |
| 6273 | if (startcol < 0) |
| 6274 | startcol = 0; |
| 6275 | |
| 6276 | #ifdef FEAT_VREPLACE |
| 6277 | if (State & VREPLACE_FLAG) |
| 6278 | { |
| 6279 | /* |
| 6280 | * In VREPLACE mode, we will backspace over the text to be |
| 6281 | * wrapped, so save a copy now to put on the next line. |
| 6282 | */ |
| 6283 | saved_text = vim_strsave(ml_get_cursor()); |
| 6284 | curwin->w_cursor.col = orig_col; |
| 6285 | if (saved_text == NULL) |
| 6286 | break; /* Can't do it, out of memory */ |
| 6287 | saved_text[startcol] = NUL; |
| 6288 | |
| 6289 | /* Backspace over characters that will move to the next line */ |
| 6290 | if (!fo_white_par) |
| 6291 | backspace_until_column(foundcol); |
| 6292 | } |
| 6293 | else |
| 6294 | #endif |
| 6295 | { |
| 6296 | /* put cursor after pos. to break line */ |
| 6297 | if (!fo_white_par) |
| 6298 | curwin->w_cursor.col = foundcol; |
| 6299 | } |
| 6300 | |
| 6301 | /* |
| 6302 | * Split the line just before the margin. |
| 6303 | * Only insert/delete lines, but don't really redraw the window. |
| 6304 | */ |
| 6305 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 6306 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 6307 | #ifdef FEAT_COMMENTS |
| 6308 | + (do_comments ? OPENLINE_DO_COM : 0) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6309 | + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6310 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6311 | , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent)); |
| 6312 | if (!(flags & INSCHAR_COM_LIST)) |
| 6313 | old_indent = 0; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6314 | |
| 6315 | replace_offset = 0; |
| 6316 | if (first_line) |
| 6317 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6318 | if (!(flags & INSCHAR_COM_LIST)) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6319 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6320 | /* |
| 6321 | * This section is for numeric lists w/o comments. If comment |
| 6322 | * indents are needed with numeric lists (formatoptions=nq), |
| 6323 | * then the INSCHAR_COM_LIST flag will cause the corresponding |
| 6324 | * OPENLINE_COM_LIST flag to be passed through to open_line() |
| 6325 | * (as seen above)... |
| 6326 | */ |
| 6327 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
| 6328 | second_indent = get_number_indent(curwin->w_cursor.lnum -1); |
| 6329 | if (second_indent >= 0) |
| 6330 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6331 | #ifdef FEAT_VREPLACE |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6332 | if (State & VREPLACE_FLAG) |
| 6333 | change_indent(INDENT_SET, second_indent, |
| 6334 | FALSE, NUL, TRUE); |
| 6335 | else |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6336 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6337 | (void)set_indent(second_indent, SIN_CHANGED); |
| 6338 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6339 | } |
| 6340 | first_line = FALSE; |
| 6341 | } |
| 6342 | |
| 6343 | #ifdef FEAT_VREPLACE |
| 6344 | if (State & VREPLACE_FLAG) |
| 6345 | { |
| 6346 | /* |
| 6347 | * In VREPLACE mode we have backspaced over the text to be |
| 6348 | * moved, now we re-insert it into the new line. |
| 6349 | */ |
| 6350 | ins_bytes(saved_text); |
| 6351 | vim_free(saved_text); |
| 6352 | } |
| 6353 | else |
| 6354 | #endif |
| 6355 | { |
| 6356 | /* |
| 6357 | * Check if cursor is not past the NUL off the line, cindent |
| 6358 | * may have added or removed indent. |
| 6359 | */ |
| 6360 | curwin->w_cursor.col += startcol; |
| 6361 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 6362 | if (curwin->w_cursor.col > len) |
| 6363 | curwin->w_cursor.col = len; |
| 6364 | } |
| 6365 | |
| 6366 | haveto_redraw = TRUE; |
| 6367 | #ifdef FEAT_CINDENT |
| 6368 | can_cindent = TRUE; |
| 6369 | #endif |
| 6370 | /* moved the cursor, don't autoindent or cindent now */ |
| 6371 | did_ai = FALSE; |
| 6372 | #ifdef FEAT_SMARTINDENT |
| 6373 | did_si = FALSE; |
| 6374 | can_si = FALSE; |
| 6375 | can_si_back = FALSE; |
| 6376 | #endif |
| 6377 | line_breakcheck(); |
| 6378 | } |
| 6379 | |
| 6380 | if (save_char != NUL) /* put back space after cursor */ |
| 6381 | pchar_cursor(save_char); |
| 6382 | |
| 6383 | if (!format_only && haveto_redraw) |
| 6384 | { |
| 6385 | update_topline(); |
| 6386 | redraw_curbuf_later(VALID); |
| 6387 | } |
| 6388 | } |
| 6389 | |
| 6390 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6391 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 6392 | * 'a' flag format from the current line until the end of the paragraph. |
| 6393 | * Keep the cursor at the same position relative to the text. |
| 6394 | * The caller must have saved the cursor line for undo, following ones will be |
| 6395 | * saved here. |
| 6396 | */ |
| 6397 | void |
| 6398 | auto_format(trailblank, prev_line) |
| 6399 | int trailblank; /* when TRUE also format with trailing blank */ |
| 6400 | int prev_line; /* may start in previous line */ |
| 6401 | { |
| 6402 | pos_T pos; |
| 6403 | colnr_T len; |
| 6404 | char_u *old; |
| 6405 | char_u *new, *pnew; |
| 6406 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6407 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6408 | |
| 6409 | if (!has_format_option(FO_AUTO)) |
| 6410 | return; |
| 6411 | |
| 6412 | pos = curwin->w_cursor; |
| 6413 | old = ml_get_curline(); |
| 6414 | |
| 6415 | /* may remove added space */ |
| 6416 | check_auto_format(FALSE); |
| 6417 | |
| 6418 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 6419 | * user might insert normal text next. Also skip formatting when "1" is |
| 6420 | * in 'formatoptions' and there is a single character before the cursor. |
| 6421 | * Otherwise the line would be broken and when typing another non-white |
| 6422 | * next they are not joined back together. */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6423 | wasatend = (pos.col == (colnr_T)STRLEN(old)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6424 | if (*old != NUL && !trailblank && wasatend) |
| 6425 | { |
| 6426 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6427 | cc = gchar_cursor(); |
| 6428 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 6429 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6430 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6431 | cc = gchar_cursor(); |
| 6432 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6433 | { |
| 6434 | curwin->w_cursor = pos; |
| 6435 | return; |
| 6436 | } |
| 6437 | curwin->w_cursor = pos; |
| 6438 | } |
| 6439 | |
| 6440 | #ifdef FEAT_COMMENTS |
| 6441 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 6442 | * comments. */ |
| 6443 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 6444 | && get_leader_len(old, NULL, FALSE, TRUE) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6445 | return; |
| 6446 | #endif |
| 6447 | |
| 6448 | /* |
| 6449 | * May start formatting in a previous line, so that after "x" a word is |
| 6450 | * moved to the previous line if it fits there now. Only when this is not |
| 6451 | * the start of a paragraph. |
| 6452 | */ |
| 6453 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 6454 | { |
| 6455 | --curwin->w_cursor.lnum; |
| 6456 | if (u_save_cursor() == FAIL) |
| 6457 | return; |
| 6458 | } |
| 6459 | |
| 6460 | /* |
| 6461 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 6462 | * be adjusted for the text formatting. |
| 6463 | */ |
| 6464 | saved_cursor = pos; |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 6465 | format_lines((linenr_T)-1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6466 | curwin->w_cursor = saved_cursor; |
| 6467 | saved_cursor.lnum = 0; |
| 6468 | |
| 6469 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 6470 | { |
| 6471 | /* "cannot happen" */ |
| 6472 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6473 | coladvance((colnr_T)MAXCOL); |
| 6474 | } |
| 6475 | else |
| 6476 | check_cursor_col(); |
| 6477 | |
| 6478 | /* Insert mode: If the cursor is now after the end of the line while it |
| 6479 | * previously wasn't, the line was broken. Because of the rule above we |
| 6480 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 6481 | * formatted. */ |
| 6482 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 6483 | { |
| 6484 | new = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6485 | len = (colnr_T)STRLEN(new); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6486 | if (curwin->w_cursor.col == len) |
| 6487 | { |
| 6488 | pnew = vim_strnsave(new, len + 2); |
| 6489 | pnew[len] = ' '; |
| 6490 | pnew[len + 1] = NUL; |
| 6491 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 6492 | /* remove the space later */ |
| 6493 | did_add_space = TRUE; |
| 6494 | } |
| 6495 | else |
| 6496 | /* may remove added space */ |
| 6497 | check_auto_format(FALSE); |
| 6498 | } |
| 6499 | |
| 6500 | check_cursor(); |
| 6501 | } |
| 6502 | |
| 6503 | /* |
| 6504 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 6505 | * delete it now. The space must be under the cursor, just after the insert |
| 6506 | * position. |
| 6507 | */ |
| 6508 | static void |
| 6509 | check_auto_format(end_insert) |
| 6510 | int end_insert; /* TRUE when ending Insert mode */ |
| 6511 | { |
| 6512 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6513 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6514 | |
| 6515 | if (did_add_space) |
| 6516 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6517 | cc = gchar_cursor(); |
| 6518 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6519 | /* Somehow the space was removed already. */ |
| 6520 | did_add_space = FALSE; |
| 6521 | else |
| 6522 | { |
| 6523 | if (!end_insert) |
| 6524 | { |
| 6525 | inc_cursor(); |
| 6526 | c = gchar_cursor(); |
| 6527 | dec_cursor(); |
| 6528 | } |
| 6529 | if (c != NUL) |
| 6530 | { |
| 6531 | /* The space is no longer at the end of the line, delete it. */ |
| 6532 | del_char(FALSE); |
| 6533 | did_add_space = FALSE; |
| 6534 | } |
| 6535 | } |
| 6536 | } |
| 6537 | } |
| 6538 | |
| 6539 | /* |
| 6540 | * Find out textwidth to be used for formatting: |
| 6541 | * if 'textwidth' option is set, use it |
| 6542 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 6543 | * if invalid value, use 0. |
| 6544 | * Set default to window width (maximum 79) for "gq" operator. |
| 6545 | */ |
| 6546 | int |
| 6547 | comp_textwidth(ff) |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 6548 | int ff; /* force formatting (for "gq" command) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6549 | { |
| 6550 | int textwidth; |
| 6551 | |
| 6552 | textwidth = curbuf->b_p_tw; |
| 6553 | if (textwidth == 0 && curbuf->b_p_wm) |
| 6554 | { |
| 6555 | /* The width is the window width minus 'wrapmargin' minus all the |
| 6556 | * things that add to the margin. */ |
| 6557 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 6558 | #ifdef FEAT_CMDWIN |
| 6559 | if (cmdwin_type != 0) |
| 6560 | textwidth -= 1; |
| 6561 | #endif |
| 6562 | #ifdef FEAT_FOLDING |
| 6563 | textwidth -= curwin->w_p_fdc; |
| 6564 | #endif |
| 6565 | #ifdef FEAT_SIGNS |
| 6566 | if (curwin->w_buffer->b_signlist != NULL |
| 6567 | # ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 6568 | || netbeans_active() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6569 | # endif |
| 6570 | ) |
| 6571 | textwidth -= 1; |
| 6572 | #endif |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 6573 | if (curwin->w_p_nu || curwin->w_p_rnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6574 | textwidth -= 8; |
| 6575 | } |
| 6576 | if (textwidth < 0) |
| 6577 | textwidth = 0; |
| 6578 | if (ff && textwidth == 0) |
| 6579 | { |
| 6580 | textwidth = W_WIDTH(curwin) - 1; |
| 6581 | if (textwidth > 79) |
| 6582 | textwidth = 79; |
| 6583 | } |
| 6584 | return textwidth; |
| 6585 | } |
| 6586 | |
| 6587 | /* |
| 6588 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 6589 | */ |
| 6590 | static void |
| 6591 | redo_literal(c) |
| 6592 | int c; |
| 6593 | { |
| 6594 | char_u buf[10]; |
| 6595 | |
| 6596 | /* Only digits need special treatment. Translate them into a string of |
| 6597 | * three digits. */ |
| 6598 | if (VIM_ISDIGIT(c)) |
| 6599 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6600 | vim_snprintf((char *)buf, sizeof(buf), "%03d", c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6601 | AppendToRedobuff(buf); |
| 6602 | } |
| 6603 | else |
| 6604 | AppendCharToRedobuff(c); |
| 6605 | } |
| 6606 | |
| 6607 | /* |
| 6608 | * 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] | 6609 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6610 | */ |
| 6611 | static void |
| 6612 | start_arrow(end_insert_pos) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6613 | pos_T *end_insert_pos; /* can be NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6614 | { |
| 6615 | if (!arrow_used) /* something has been inserted */ |
| 6616 | { |
| 6617 | AppendToRedobuff(ESC_STR); |
| 6618 | stop_insert(end_insert_pos, FALSE); |
| 6619 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 6620 | } |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6621 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6622 | check_spell_redraw(); |
| 6623 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6624 | } |
| 6625 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6626 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6627 | /* |
| 6628 | * If we skipped highlighting word at cursor, do it now. |
| 6629 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 6630 | */ |
| 6631 | static void |
| 6632 | check_spell_redraw() |
| 6633 | { |
| 6634 | if (spell_redraw_lnum != 0) |
| 6635 | { |
| 6636 | linenr_T lnum = spell_redraw_lnum; |
| 6637 | |
| 6638 | spell_redraw_lnum = 0; |
| 6639 | redrawWinline(lnum, FALSE); |
| 6640 | } |
| 6641 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6642 | |
| 6643 | /* |
| 6644 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6645 | * spelled word, if there is one. |
| 6646 | */ |
| 6647 | static void |
| 6648 | spell_back_to_badword() |
| 6649 | { |
| 6650 | pos_T tpos = curwin->w_cursor; |
| 6651 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 6652 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6653 | if (curwin->w_cursor.col != tpos.col) |
| 6654 | start_arrow(&tpos); |
| 6655 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6656 | #endif |
| 6657 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6658 | /* |
| 6659 | * stop_arrow() is called before a change is made in insert mode. |
| 6660 | * If an arrow key has been used, start a new insertion. |
| 6661 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 6662 | */ |
| 6663 | int |
| 6664 | stop_arrow() |
| 6665 | { |
| 6666 | if (arrow_used) |
| 6667 | { |
| 6668 | if (u_save_cursor() == OK) |
| 6669 | { |
| 6670 | arrow_used = FALSE; |
| 6671 | ins_need_undo = FALSE; |
| 6672 | } |
| 6673 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 6674 | Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6675 | ai_col = 0; |
| 6676 | #ifdef FEAT_VREPLACE |
| 6677 | if (State & VREPLACE_FLAG) |
| 6678 | { |
| 6679 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 6680 | vr_lines_changed = 1; |
| 6681 | } |
| 6682 | #endif |
| 6683 | ResetRedobuff(); |
| 6684 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 6685 | new_insert_skip = 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6686 | } |
| 6687 | else if (ins_need_undo) |
| 6688 | { |
| 6689 | if (u_save_cursor() == OK) |
| 6690 | ins_need_undo = FALSE; |
| 6691 | } |
| 6692 | |
| 6693 | #ifdef FEAT_FOLDING |
| 6694 | /* Always open fold at the cursor line when inserting something. */ |
| 6695 | foldOpenCursor(); |
| 6696 | #endif |
| 6697 | |
| 6698 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 6699 | } |
| 6700 | |
| 6701 | /* |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6702 | * Do a few things to stop inserting. |
| 6703 | * "end_insert_pos" is where insert ended. It is NULL when we already jumped |
| 6704 | * to another window/buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6705 | */ |
| 6706 | static void |
| 6707 | stop_insert(end_insert_pos, esc) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6708 | pos_T *end_insert_pos; |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6709 | int esc; /* called by ins_esc() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6710 | { |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6711 | int cc; |
| 6712 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6713 | |
| 6714 | stop_redo_ins(); |
| 6715 | replace_flush(); /* abandon replace stack */ |
| 6716 | |
| 6717 | /* |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6718 | * Save the inserted text for later redo with ^@ and CTRL-A. |
| 6719 | * Don't do it when "restart_edit" was set and nothing was inserted, |
| 6720 | * otherwise CTRL-O w and then <Left> will clear "last_insert". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6721 | */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6722 | ptr = get_inserted(); |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 6723 | if (did_restart_edit == 0 || (ptr != NULL |
| 6724 | && (int)STRLEN(ptr) > new_insert_skip)) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6725 | { |
| 6726 | vim_free(last_insert); |
| 6727 | last_insert = ptr; |
| 6728 | last_insert_skip = new_insert_skip; |
| 6729 | } |
| 6730 | else |
| 6731 | vim_free(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6732 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6733 | if (!arrow_used && end_insert_pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6734 | { |
| 6735 | /* Auto-format now. It may seem strange to do this when stopping an |
| 6736 | * insertion (or moving the cursor), but it's required when appending |
| 6737 | * a line and having it end in a space. But only do it when something |
| 6738 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6739 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6741 | pos_T tpos = curwin->w_cursor; |
| 6742 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6743 | /* When the cursor is at the end of the line after a space the |
| 6744 | * formatting will move it to the following word. Avoid that by |
| 6745 | * moving the cursor onto the space. */ |
| 6746 | cc = 'x'; |
| 6747 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 6748 | { |
| 6749 | dec_cursor(); |
| 6750 | cc = gchar_cursor(); |
| 6751 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6752 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | auto_format(TRUE, FALSE); |
| 6756 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6757 | if (vim_iswhite(cc)) |
| 6758 | { |
| 6759 | if (gchar_cursor() != NUL) |
| 6760 | inc_cursor(); |
| 6761 | #ifdef FEAT_VIRTUALEDIT |
| 6762 | /* If the cursor is still at the same character, also keep |
| 6763 | * the "coladd". */ |
| 6764 | if (gchar_cursor() == NUL |
| 6765 | && curwin->w_cursor.lnum == tpos.lnum |
| 6766 | && curwin->w_cursor.col == tpos.col) |
| 6767 | curwin->w_cursor.coladd = tpos.coladd; |
| 6768 | #endif |
| 6769 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6770 | } |
| 6771 | |
| 6772 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 6773 | check_auto_format(TRUE); |
| 6774 | |
| 6775 | /* 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] | 6776 | * of the line, and put the cursor back. |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6777 | * Do this when ESC was used or moving the cursor up/down. |
| 6778 | * Check for the old position still being valid, just in case the text |
| 6779 | * got changed unexpectedly. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6780 | if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6781 | && curwin->w_cursor.lnum != end_insert_pos->lnum)) |
| 6782 | && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6783 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6784 | pos_T tpos = curwin->w_cursor; |
| 6785 | |
| 6786 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6787 | check_cursor_col(); /* make sure it is not past the line */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6788 | for (;;) |
| 6789 | { |
| 6790 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 6791 | --curwin->w_cursor.col; |
| 6792 | cc = gchar_cursor(); |
| 6793 | if (!vim_iswhite(cc)) |
| 6794 | break; |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6795 | if (del_char(TRUE) == FAIL) |
| 6796 | break; /* should not happen */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6797 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6798 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 6799 | curwin->w_cursor = tpos; |
| 6800 | else if (cc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6801 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 6802 | |
| 6803 | #ifdef FEAT_VISUAL |
| 6804 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 6805 | * deleted characters. */ |
| 6806 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 6807 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6808 | int len = (int)STRLEN(ml_get_curline()); |
| 6809 | |
| 6810 | if (VIsual.col > len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6811 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6812 | VIsual.col = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6813 | # ifdef FEAT_VIRTUALEDIT |
| 6814 | VIsual.coladd = 0; |
| 6815 | # endif |
| 6816 | } |
| 6817 | } |
| 6818 | #endif |
| 6819 | } |
| 6820 | } |
| 6821 | did_ai = FALSE; |
| 6822 | #ifdef FEAT_SMARTINDENT |
| 6823 | did_si = FALSE; |
| 6824 | can_si = FALSE; |
| 6825 | can_si_back = FALSE; |
| 6826 | #endif |
| 6827 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6828 | /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are |
| 6829 | * now in a different buffer. */ |
| 6830 | if (end_insert_pos != NULL) |
| 6831 | { |
| 6832 | curbuf->b_op_start = Insstart; |
| 6833 | curbuf->b_op_end = *end_insert_pos; |
| 6834 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | /* |
| 6838 | * Set the last inserted text to a single character. |
| 6839 | * Used for the replace command. |
| 6840 | */ |
| 6841 | void |
| 6842 | set_last_insert(c) |
| 6843 | int c; |
| 6844 | { |
| 6845 | char_u *s; |
| 6846 | |
| 6847 | vim_free(last_insert); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6848 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6849 | if (last_insert != NULL) |
| 6850 | { |
| 6851 | s = last_insert; |
| 6852 | /* Use the CTRL-V only when entering a special char */ |
| 6853 | if (c < ' ' || c == DEL) |
| 6854 | *s++ = Ctrl_V; |
| 6855 | s = add_char2buf(c, s); |
| 6856 | *s++ = ESC; |
| 6857 | *s++ = NUL; |
| 6858 | last_insert_skip = 0; |
| 6859 | } |
| 6860 | } |
| 6861 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6862 | #if defined(EXITFREE) || defined(PROTO) |
| 6863 | void |
| 6864 | free_last_insert() |
| 6865 | { |
| 6866 | vim_free(last_insert); |
| 6867 | last_insert = NULL; |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 6868 | # ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6869 | vim_free(compl_orig_text); |
| 6870 | compl_orig_text = NULL; |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 6871 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6872 | } |
| 6873 | #endif |
| 6874 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6875 | /* |
| 6876 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 6877 | * and CSI. Handle multi-byte characters. |
| 6878 | * Returns a pointer to after the added bytes. |
| 6879 | */ |
| 6880 | char_u * |
| 6881 | add_char2buf(c, s) |
| 6882 | int c; |
| 6883 | char_u *s; |
| 6884 | { |
| 6885 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 6886 | char_u temp[MB_MAXBYTES + 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6887 | int i; |
| 6888 | int len; |
| 6889 | |
| 6890 | len = (*mb_char2bytes)(c, temp); |
| 6891 | for (i = 0; i < len; ++i) |
| 6892 | { |
| 6893 | c = temp[i]; |
| 6894 | #endif |
| 6895 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 6896 | if (c == K_SPECIAL) |
| 6897 | { |
| 6898 | *s++ = K_SPECIAL; |
| 6899 | *s++ = KS_SPECIAL; |
| 6900 | *s++ = KE_FILLER; |
| 6901 | } |
| 6902 | #ifdef FEAT_GUI |
| 6903 | else if (c == CSI) |
| 6904 | { |
| 6905 | *s++ = CSI; |
| 6906 | *s++ = KS_EXTRA; |
| 6907 | *s++ = (int)KE_CSI; |
| 6908 | } |
| 6909 | #endif |
| 6910 | else |
| 6911 | *s++ = c; |
| 6912 | #ifdef FEAT_MBYTE |
| 6913 | } |
| 6914 | #endif |
| 6915 | return s; |
| 6916 | } |
| 6917 | |
| 6918 | /* |
| 6919 | * move cursor to start of line |
| 6920 | * if flags & BL_WHITE move to first non-white |
| 6921 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 6922 | * otherwise keep "curswant" column |
| 6923 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 6924 | */ |
| 6925 | void |
| 6926 | beginline(flags) |
| 6927 | int flags; |
| 6928 | { |
| 6929 | if ((flags & BL_SOL) && !p_sol) |
| 6930 | coladvance(curwin->w_curswant); |
| 6931 | else |
| 6932 | { |
| 6933 | curwin->w_cursor.col = 0; |
| 6934 | #ifdef FEAT_VIRTUALEDIT |
| 6935 | curwin->w_cursor.coladd = 0; |
| 6936 | #endif |
| 6937 | |
| 6938 | if (flags & (BL_WHITE | BL_SOL)) |
| 6939 | { |
| 6940 | char_u *ptr; |
| 6941 | |
| 6942 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 6943 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 6944 | ++curwin->w_cursor.col; |
| 6945 | } |
| 6946 | curwin->w_set_curswant = TRUE; |
| 6947 | } |
| 6948 | } |
| 6949 | |
| 6950 | /* |
| 6951 | * oneright oneleft cursor_down cursor_up |
| 6952 | * |
| 6953 | * Move one char {right,left,down,up}. |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6954 | * 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] | 6955 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 6956 | */ |
| 6957 | |
| 6958 | int |
| 6959 | oneright() |
| 6960 | { |
| 6961 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6962 | int l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6963 | |
| 6964 | #ifdef FEAT_VIRTUALEDIT |
| 6965 | if (virtual_active()) |
| 6966 | { |
| 6967 | pos_T prevpos = curwin->w_cursor; |
| 6968 | |
| 6969 | /* Adjust for multi-wide char (excluding TAB) */ |
| 6970 | ptr = ml_get_cursor(); |
| 6971 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6972 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6973 | (*mb_ptr2char)(ptr) |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6974 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6975 | *ptr |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6976 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6977 | )) |
| 6978 | ? ptr2cells(ptr) : 1)); |
| 6979 | curwin->w_set_curswant = TRUE; |
| 6980 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 6981 | return (prevpos.col != curwin->w_cursor.col |
| 6982 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 6983 | } |
| 6984 | #endif |
| 6985 | |
| 6986 | ptr = ml_get_cursor(); |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6987 | if (*ptr == NUL) |
| 6988 | return FAIL; /* already at the very end */ |
| 6989 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6990 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6991 | if (has_mbyte) |
| 6992 | l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6993 | else |
| 6994 | #endif |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6995 | l = 1; |
| 6996 | |
| 6997 | /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' |
| 6998 | * contains "onemore". */ |
| 6999 | if (ptr[l] == NUL |
| 7000 | #ifdef FEAT_VIRTUALEDIT |
| 7001 | && (ve_flags & VE_ONEMORE) == 0 |
| 7002 | #endif |
| 7003 | ) |
| 7004 | return FAIL; |
| 7005 | curwin->w_cursor.col += l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7006 | |
| 7007 | curwin->w_set_curswant = TRUE; |
| 7008 | return OK; |
| 7009 | } |
| 7010 | |
| 7011 | int |
| 7012 | oneleft() |
| 7013 | { |
| 7014 | #ifdef FEAT_VIRTUALEDIT |
| 7015 | if (virtual_active()) |
| 7016 | { |
| 7017 | int width; |
| 7018 | int v = getviscol(); |
| 7019 | |
| 7020 | if (v == 0) |
| 7021 | return FAIL; |
| 7022 | |
| 7023 | # ifdef FEAT_LINEBREAK |
| 7024 | /* We might get stuck on 'showbreak', skip over it. */ |
| 7025 | width = 1; |
| 7026 | for (;;) |
| 7027 | { |
| 7028 | coladvance(v - width); |
| 7029 | /* getviscol() is slow, skip it when 'showbreak' is empty and |
| 7030 | * there are no multi-byte characters */ |
| 7031 | if ((*p_sbr == NUL |
| 7032 | # ifdef FEAT_MBYTE |
| 7033 | && !has_mbyte |
| 7034 | # endif |
| 7035 | ) || getviscol() < v) |
| 7036 | break; |
| 7037 | ++width; |
| 7038 | } |
| 7039 | # else |
| 7040 | coladvance(v - 1); |
| 7041 | # endif |
| 7042 | |
| 7043 | if (curwin->w_cursor.coladd == 1) |
| 7044 | { |
| 7045 | char_u *ptr; |
| 7046 | |
| 7047 | /* Adjust for multi-wide char (not a TAB) */ |
| 7048 | ptr = ml_get_cursor(); |
| 7049 | if (*ptr != TAB && vim_isprintc( |
| 7050 | # ifdef FEAT_MBYTE |
| 7051 | (*mb_ptr2char)(ptr) |
| 7052 | # else |
| 7053 | *ptr |
| 7054 | # endif |
| 7055 | ) && ptr2cells(ptr) > 1) |
| 7056 | curwin->w_cursor.coladd = 0; |
| 7057 | } |
| 7058 | |
| 7059 | curwin->w_set_curswant = TRUE; |
| 7060 | return OK; |
| 7061 | } |
| 7062 | #endif |
| 7063 | |
| 7064 | if (curwin->w_cursor.col == 0) |
| 7065 | return FAIL; |
| 7066 | |
| 7067 | curwin->w_set_curswant = TRUE; |
| 7068 | --curwin->w_cursor.col; |
| 7069 | |
| 7070 | #ifdef FEAT_MBYTE |
| 7071 | /* if the character on the left of the current cursor is a multi-byte |
| 7072 | * character, move to its first byte */ |
| 7073 | if (has_mbyte) |
| 7074 | mb_adjust_cursor(); |
| 7075 | #endif |
| 7076 | return OK; |
| 7077 | } |
| 7078 | |
| 7079 | int |
| 7080 | cursor_up(n, upd_topline) |
| 7081 | long n; |
| 7082 | int upd_topline; /* When TRUE: update topline */ |
| 7083 | { |
| 7084 | linenr_T lnum; |
| 7085 | |
| 7086 | if (n > 0) |
| 7087 | { |
| 7088 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7089 | /* This fails if the cursor is already in the first line or the count |
| 7090 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 7091 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7092 | return FAIL; |
| 7093 | if (n >= lnum) |
| 7094 | lnum = 1; |
| 7095 | else |
| 7096 | #ifdef FEAT_FOLDING |
| 7097 | if (hasAnyFolding(curwin)) |
| 7098 | { |
| 7099 | /* |
| 7100 | * Count each sequence of folded lines as one logical line. |
| 7101 | */ |
| 7102 | /* go to the the start of the current fold */ |
| 7103 | (void)hasFolding(lnum, &lnum, NULL); |
| 7104 | |
| 7105 | while (n--) |
| 7106 | { |
| 7107 | /* move up one line */ |
| 7108 | --lnum; |
| 7109 | if (lnum <= 1) |
| 7110 | break; |
| 7111 | /* If we entered a fold, move to the beginning, unless in |
| 7112 | * Insert mode or when 'foldopen' contains "all": it will open |
| 7113 | * in a moment. */ |
| 7114 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 7115 | (void)hasFolding(lnum, &lnum, NULL); |
| 7116 | } |
| 7117 | if (lnum < 1) |
| 7118 | lnum = 1; |
| 7119 | } |
| 7120 | else |
| 7121 | #endif |
| 7122 | lnum -= n; |
| 7123 | curwin->w_cursor.lnum = lnum; |
| 7124 | } |
| 7125 | |
| 7126 | /* try to advance to the column we want to be at */ |
| 7127 | coladvance(curwin->w_curswant); |
| 7128 | |
| 7129 | if (upd_topline) |
| 7130 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 7131 | |
| 7132 | return OK; |
| 7133 | } |
| 7134 | |
| 7135 | /* |
| 7136 | * Cursor down a number of logical lines. |
| 7137 | */ |
| 7138 | int |
| 7139 | cursor_down(n, upd_topline) |
| 7140 | long n; |
| 7141 | int upd_topline; /* When TRUE: update topline */ |
| 7142 | { |
| 7143 | linenr_T lnum; |
| 7144 | |
| 7145 | if (n > 0) |
| 7146 | { |
| 7147 | lnum = curwin->w_cursor.lnum; |
| 7148 | #ifdef FEAT_FOLDING |
| 7149 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 7150 | (void)hasFolding(lnum, NULL, &lnum); |
| 7151 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7152 | /* This fails if the cursor is already in the last line or would move |
| 7153 | * beyound the last line and '-' is in 'cpoptions' */ |
| 7154 | if (lnum >= curbuf->b_ml.ml_line_count |
| 7155 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 7156 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7157 | return FAIL; |
| 7158 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 7159 | lnum = curbuf->b_ml.ml_line_count; |
| 7160 | else |
| 7161 | #ifdef FEAT_FOLDING |
| 7162 | if (hasAnyFolding(curwin)) |
| 7163 | { |
| 7164 | linenr_T last; |
| 7165 | |
| 7166 | /* count each sequence of folded lines as one logical line */ |
| 7167 | while (n--) |
| 7168 | { |
| 7169 | if (hasFolding(lnum, NULL, &last)) |
| 7170 | lnum = last + 1; |
| 7171 | else |
| 7172 | ++lnum; |
| 7173 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 7174 | break; |
| 7175 | } |
| 7176 | if (lnum > curbuf->b_ml.ml_line_count) |
| 7177 | lnum = curbuf->b_ml.ml_line_count; |
| 7178 | } |
| 7179 | else |
| 7180 | #endif |
| 7181 | lnum += n; |
| 7182 | curwin->w_cursor.lnum = lnum; |
| 7183 | } |
| 7184 | |
| 7185 | /* try to advance to the column we want to be at */ |
| 7186 | coladvance(curwin->w_curswant); |
| 7187 | |
| 7188 | if (upd_topline) |
| 7189 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 7190 | |
| 7191 | return OK; |
| 7192 | } |
| 7193 | |
| 7194 | /* |
| 7195 | * Stuff the last inserted text in the read buffer. |
| 7196 | * Last_insert actually is a copy of the redo buffer, so we |
| 7197 | * first have to remove the command. |
| 7198 | */ |
| 7199 | int |
| 7200 | stuff_inserted(c, count, no_esc) |
| 7201 | int c; /* Command character to be inserted */ |
| 7202 | long count; /* Repeat this many times */ |
| 7203 | int no_esc; /* Don't add an ESC at the end */ |
| 7204 | { |
| 7205 | char_u *esc_ptr; |
| 7206 | char_u *ptr; |
| 7207 | char_u *last_ptr; |
| 7208 | char_u last = NUL; |
| 7209 | |
| 7210 | ptr = get_last_insert(); |
| 7211 | if (ptr == NULL) |
| 7212 | { |
| 7213 | EMSG(_(e_noinstext)); |
| 7214 | return FAIL; |
| 7215 | } |
| 7216 | |
| 7217 | /* may want to stuff the command character, to start Insert mode */ |
| 7218 | if (c != NUL) |
| 7219 | stuffcharReadbuff(c); |
| 7220 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 7221 | *esc_ptr = NUL; /* remove the ESC */ |
| 7222 | |
| 7223 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 7224 | * comes after it OR if it will inserted more than once and "ptr" |
| 7225 | * starts with ^D. -- Acevedo |
| 7226 | */ |
| 7227 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 7228 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 7229 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 7230 | { |
| 7231 | last = *last_ptr; |
| 7232 | *last_ptr = NUL; |
| 7233 | } |
| 7234 | |
| 7235 | do |
| 7236 | { |
| 7237 | stuffReadbuff(ptr); |
| 7238 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 7239 | if (last) |
| 7240 | stuffReadbuff((char_u *)(last == '0' |
| 7241 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 7242 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 7243 | } |
| 7244 | while (--count > 0); |
| 7245 | |
| 7246 | if (last) |
| 7247 | *last_ptr = last; |
| 7248 | |
| 7249 | if (esc_ptr != NULL) |
| 7250 | *esc_ptr = ESC; /* put the ESC back */ |
| 7251 | |
| 7252 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 7253 | if (!no_esc) |
| 7254 | stuffcharReadbuff(ESC); |
| 7255 | |
| 7256 | return OK; |
| 7257 | } |
| 7258 | |
| 7259 | char_u * |
| 7260 | get_last_insert() |
| 7261 | { |
| 7262 | if (last_insert == NULL) |
| 7263 | return NULL; |
| 7264 | return last_insert + last_insert_skip; |
| 7265 | } |
| 7266 | |
| 7267 | /* |
| 7268 | * Get last inserted string, and remove trailing <Esc>. |
| 7269 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 7270 | */ |
| 7271 | char_u * |
| 7272 | get_last_insert_save() |
| 7273 | { |
| 7274 | char_u *s; |
| 7275 | int len; |
| 7276 | |
| 7277 | if (last_insert == NULL) |
| 7278 | return NULL; |
| 7279 | s = vim_strsave(last_insert + last_insert_skip); |
| 7280 | if (s != NULL) |
| 7281 | { |
| 7282 | len = (int)STRLEN(s); |
| 7283 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 7284 | s[len - 1] = NUL; |
| 7285 | } |
| 7286 | return s; |
| 7287 | } |
| 7288 | |
| 7289 | /* |
| 7290 | * Check the word in front of the cursor for an abbreviation. |
| 7291 | * Called when the non-id character "c" has been entered. |
| 7292 | * When an abbreviation is recognized it is removed from the text and |
| 7293 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 7294 | */ |
| 7295 | static int |
| 7296 | echeck_abbr(c) |
| 7297 | int c; |
| 7298 | { |
| 7299 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 7300 | * after moving around with cursor keys. */ |
| 7301 | if (p_paste || no_abbr || arrow_used) |
| 7302 | return FALSE; |
| 7303 | |
| 7304 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 7305 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 7306 | } |
| 7307 | |
| 7308 | /* |
| 7309 | * replace-stack functions |
| 7310 | * |
| 7311 | * When replacing characters, the replaced characters are remembered for each |
| 7312 | * new character. This is used to re-insert the old text when backspacing. |
| 7313 | * |
| 7314 | * There is a NUL headed list of characters for each character that is |
| 7315 | * currently in the file after the insertion point. When BS is used, one NUL |
| 7316 | * headed list is put back for the deleted character. |
| 7317 | * |
| 7318 | * For a newline, there are two NUL headed lists. One contains the characters |
| 7319 | * that the NL replaced. The extra one stores the characters after the cursor |
| 7320 | * that were deleted (always white space). |
| 7321 | * |
| 7322 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 7323 | * character at the end of the stack. If replace_offset is not 0, that many |
| 7324 | * characters will be left on the stack above the newly inserted character. |
| 7325 | */ |
| 7326 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 7327 | static char_u *replace_stack = NULL; |
| 7328 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 7329 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7330 | |
| 7331 | void |
| 7332 | replace_push(c) |
| 7333 | int c; /* character that is replaced (NUL is none) */ |
| 7334 | { |
| 7335 | char_u *p; |
| 7336 | |
| 7337 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 7338 | return; |
| 7339 | if (replace_stack_len <= replace_stack_nr) |
| 7340 | { |
| 7341 | replace_stack_len += 50; |
| 7342 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 7343 | if (p == NULL) /* out of memory */ |
| 7344 | { |
| 7345 | replace_stack_len -= 50; |
| 7346 | return; |
| 7347 | } |
| 7348 | if (replace_stack != NULL) |
| 7349 | { |
| 7350 | mch_memmove(p, replace_stack, |
| 7351 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 7352 | vim_free(replace_stack); |
| 7353 | } |
| 7354 | replace_stack = p; |
| 7355 | } |
| 7356 | p = replace_stack + replace_stack_nr - replace_offset; |
| 7357 | if (replace_offset) |
| 7358 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 7359 | *p = c; |
| 7360 | ++replace_stack_nr; |
| 7361 | } |
| 7362 | |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 7363 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 7364 | /* |
| 7365 | * Push a character onto the replace stack. Handles a multi-byte character in |
| 7366 | * reverse byte order, so that the first byte is popped off first. |
| 7367 | * Return the number of bytes done (includes composing characters). |
| 7368 | */ |
| 7369 | int |
| 7370 | replace_push_mb(p) |
| 7371 | char_u *p; |
| 7372 | { |
| 7373 | int l = (*mb_ptr2len)(p); |
| 7374 | int j; |
| 7375 | |
| 7376 | for (j = l - 1; j >= 0; --j) |
| 7377 | replace_push(p[j]); |
| 7378 | return l; |
| 7379 | } |
| 7380 | #endif |
| 7381 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7382 | /* |
| 7383 | * Pop one item from the replace stack. |
| 7384 | * return -1 if stack empty |
| 7385 | * return replaced character or NUL otherwise |
| 7386 | */ |
| 7387 | static int |
| 7388 | replace_pop() |
| 7389 | { |
| 7390 | if (replace_stack_nr == 0) |
| 7391 | return -1; |
| 7392 | return (int)replace_stack[--replace_stack_nr]; |
| 7393 | } |
| 7394 | |
| 7395 | /* |
| 7396 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 7397 | * encountered. |
| 7398 | */ |
| 7399 | static void |
| 7400 | replace_join(off) |
| 7401 | int off; /* offset for which NUL to remove */ |
| 7402 | { |
| 7403 | int i; |
| 7404 | |
| 7405 | for (i = replace_stack_nr; --i >= 0; ) |
| 7406 | if (replace_stack[i] == NUL && off-- <= 0) |
| 7407 | { |
| 7408 | --replace_stack_nr; |
| 7409 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 7410 | (size_t)(replace_stack_nr - i)); |
| 7411 | return; |
| 7412 | } |
| 7413 | } |
| 7414 | |
| 7415 | /* |
| 7416 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 7417 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 7418 | */ |
| 7419 | static void |
| 7420 | replace_pop_ins() |
| 7421 | { |
| 7422 | int cc; |
| 7423 | int oldState = State; |
| 7424 | |
| 7425 | State = NORMAL; /* don't want REPLACE here */ |
| 7426 | while ((cc = replace_pop()) > 0) |
| 7427 | { |
| 7428 | #ifdef FEAT_MBYTE |
| 7429 | mb_replace_pop_ins(cc); |
| 7430 | #else |
| 7431 | ins_char(cc); |
| 7432 | #endif |
| 7433 | dec_cursor(); |
| 7434 | } |
| 7435 | State = oldState; |
| 7436 | } |
| 7437 | |
| 7438 | #ifdef FEAT_MBYTE |
| 7439 | /* |
| 7440 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 7441 | * indicates a multi-byte char, pop the other bytes too. |
| 7442 | */ |
| 7443 | static void |
| 7444 | mb_replace_pop_ins(cc) |
| 7445 | int cc; |
| 7446 | { |
| 7447 | int n; |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 7448 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7449 | int i; |
| 7450 | int c; |
| 7451 | |
| 7452 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 7453 | { |
| 7454 | buf[0] = cc; |
| 7455 | for (i = 1; i < n; ++i) |
| 7456 | buf[i] = replace_pop(); |
| 7457 | ins_bytes_len(buf, n); |
| 7458 | } |
| 7459 | else |
| 7460 | ins_char(cc); |
| 7461 | |
| 7462 | if (enc_utf8) |
| 7463 | /* Handle composing chars. */ |
| 7464 | for (;;) |
| 7465 | { |
| 7466 | c = replace_pop(); |
| 7467 | if (c == -1) /* stack empty */ |
| 7468 | break; |
| 7469 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 7470 | { |
| 7471 | /* Not a multi-byte char, put it back. */ |
| 7472 | replace_push(c); |
| 7473 | break; |
| 7474 | } |
| 7475 | else |
| 7476 | { |
| 7477 | buf[0] = c; |
| 7478 | for (i = 1; i < n; ++i) |
| 7479 | buf[i] = replace_pop(); |
| 7480 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 7481 | ins_bytes_len(buf, n); |
| 7482 | else |
| 7483 | { |
| 7484 | /* Not a composing char, put it back. */ |
| 7485 | for (i = n - 1; i >= 0; --i) |
| 7486 | replace_push(buf[i]); |
| 7487 | break; |
| 7488 | } |
| 7489 | } |
| 7490 | } |
| 7491 | } |
| 7492 | #endif |
| 7493 | |
| 7494 | /* |
| 7495 | * make the replace stack empty |
| 7496 | * (called when exiting replace mode) |
| 7497 | */ |
| 7498 | static void |
| 7499 | replace_flush() |
| 7500 | { |
| 7501 | vim_free(replace_stack); |
| 7502 | replace_stack = NULL; |
| 7503 | replace_stack_len = 0; |
| 7504 | replace_stack_nr = 0; |
| 7505 | } |
| 7506 | |
| 7507 | /* |
| 7508 | * Handle doing a BS for one character. |
| 7509 | * cc < 0: replace stack empty, just move cursor |
| 7510 | * cc == 0: character was inserted, delete it |
| 7511 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 7512 | * and check for more characters to be put back |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7513 | * When "limit_col" is >= 0, don't delete before this column. Matters when |
| 7514 | * using composing characters, use del_char_after_col() instead of del_char(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7515 | */ |
| 7516 | static void |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7517 | replace_do_bs(limit_col) |
| 7518 | int limit_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7519 | { |
| 7520 | int cc; |
| 7521 | #ifdef FEAT_VREPLACE |
| 7522 | int orig_len = 0; |
| 7523 | int ins_len; |
| 7524 | int orig_vcols = 0; |
| 7525 | colnr_T start_vcol; |
| 7526 | char_u *p; |
| 7527 | int i; |
| 7528 | int vcol; |
| 7529 | #endif |
| 7530 | |
| 7531 | cc = replace_pop(); |
| 7532 | if (cc > 0) |
| 7533 | { |
| 7534 | #ifdef FEAT_VREPLACE |
| 7535 | if (State & VREPLACE_FLAG) |
| 7536 | { |
| 7537 | /* Get the number of screen cells used by the character we are |
| 7538 | * going to delete. */ |
| 7539 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 7540 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 7541 | } |
| 7542 | #endif |
| 7543 | #ifdef FEAT_MBYTE |
| 7544 | if (has_mbyte) |
| 7545 | { |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7546 | (void)del_char_after_col(limit_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7547 | # ifdef FEAT_VREPLACE |
| 7548 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7549 | orig_len = (int)STRLEN(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7550 | # endif |
| 7551 | replace_push(cc); |
| 7552 | } |
| 7553 | else |
| 7554 | #endif |
| 7555 | { |
| 7556 | pchar_cursor(cc); |
| 7557 | #ifdef FEAT_VREPLACE |
| 7558 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7559 | orig_len = (int)STRLEN(ml_get_cursor()) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7560 | #endif |
| 7561 | } |
| 7562 | replace_pop_ins(); |
| 7563 | |
| 7564 | #ifdef FEAT_VREPLACE |
| 7565 | if (State & VREPLACE_FLAG) |
| 7566 | { |
| 7567 | /* Get the number of screen cells used by the inserted characters */ |
| 7568 | p = ml_get_cursor(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7569 | ins_len = (int)STRLEN(p) - orig_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7570 | vcol = start_vcol; |
| 7571 | for (i = 0; i < ins_len; ++i) |
| 7572 | { |
| 7573 | vcol += chartabsize(p + i, vcol); |
| 7574 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7575 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7576 | #endif |
| 7577 | } |
| 7578 | vcol -= start_vcol; |
| 7579 | |
| 7580 | /* Delete spaces that were inserted after the cursor to keep the |
| 7581 | * text aligned. */ |
| 7582 | curwin->w_cursor.col += ins_len; |
| 7583 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 7584 | { |
| 7585 | del_char(FALSE); |
| 7586 | ++orig_vcols; |
| 7587 | } |
| 7588 | curwin->w_cursor.col -= ins_len; |
| 7589 | } |
| 7590 | #endif |
| 7591 | |
| 7592 | /* mark the buffer as changed and prepare for displaying */ |
| 7593 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 7594 | } |
| 7595 | else if (cc == 0) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7596 | (void)del_char_after_col(limit_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7597 | } |
| 7598 | |
| 7599 | #ifdef FEAT_CINDENT |
| 7600 | /* |
| 7601 | * Return TRUE if C-indenting is on. |
| 7602 | */ |
| 7603 | static int |
| 7604 | cindent_on() |
| 7605 | { |
| 7606 | return (!p_paste && (curbuf->b_p_cin |
| 7607 | # ifdef FEAT_EVAL |
| 7608 | || *curbuf->b_p_inde != NUL |
| 7609 | # endif |
| 7610 | )); |
| 7611 | } |
| 7612 | #endif |
| 7613 | |
| 7614 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 7615 | /* |
| 7616 | * Re-indent the current line, based on the current contents of it and the |
| 7617 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 7618 | * confused what all the part that handles Control-T is doing that I'm not. |
| 7619 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 7620 | */ |
| 7621 | |
| 7622 | void |
| 7623 | fixthisline(get_the_indent) |
| 7624 | int (*get_the_indent) __ARGS((void)); |
| 7625 | { |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 7626 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7627 | if (linewhite(curwin->w_cursor.lnum)) |
| 7628 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 7629 | } |
| 7630 | |
| 7631 | void |
| 7632 | fix_indent() |
| 7633 | { |
| 7634 | if (p_paste) |
| 7635 | return; |
| 7636 | # ifdef FEAT_LISP |
| 7637 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 7638 | fixthisline(get_lisp_indent); |
| 7639 | # endif |
| 7640 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 7641 | else |
| 7642 | # endif |
| 7643 | # ifdef FEAT_CINDENT |
| 7644 | if (cindent_on()) |
| 7645 | do_c_expr_indent(); |
| 7646 | # endif |
| 7647 | } |
| 7648 | |
| 7649 | #endif |
| 7650 | |
| 7651 | #ifdef FEAT_CINDENT |
| 7652 | /* |
| 7653 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 7654 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 7655 | * when == '!': Only if key is prededed with '!' (don't insert) |
| 7656 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 7657 | * |
| 7658 | * "keytyped" can have a few special values: |
| 7659 | * KEY_OPEN_FORW |
| 7660 | * KEY_OPEN_BACK |
| 7661 | * KEY_COMPLETE just finished completion. |
| 7662 | * |
| 7663 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 7664 | */ |
| 7665 | int |
| 7666 | in_cinkeys(keytyped, when, line_is_empty) |
| 7667 | int keytyped; |
| 7668 | int when; |
| 7669 | int line_is_empty; |
| 7670 | { |
| 7671 | char_u *look; |
| 7672 | int try_match; |
| 7673 | int try_match_word; |
| 7674 | char_u *p; |
| 7675 | char_u *line; |
| 7676 | int icase; |
| 7677 | int i; |
| 7678 | |
Bram Moolenaar | 3084894 | 2009-12-24 14:46:12 +0000 | [diff] [blame] | 7679 | if (keytyped == NUL) |
| 7680 | /* Can happen with CTRL-Y and CTRL-E on a short line. */ |
| 7681 | return FALSE; |
| 7682 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7683 | #ifdef FEAT_EVAL |
| 7684 | if (*curbuf->b_p_inde != NUL) |
| 7685 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 7686 | else |
| 7687 | #endif |
| 7688 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 7689 | while (*look) |
| 7690 | { |
| 7691 | /* |
| 7692 | * Find out if we want to try a match with this key, depending on |
| 7693 | * 'when' and a '*' or '!' before the key. |
| 7694 | */ |
| 7695 | switch (when) |
| 7696 | { |
| 7697 | case '*': try_match = (*look == '*'); break; |
| 7698 | case '!': try_match = (*look == '!'); break; |
| 7699 | default: try_match = (*look != '*'); break; |
| 7700 | } |
| 7701 | if (*look == '*' || *look == '!') |
| 7702 | ++look; |
| 7703 | |
| 7704 | /* |
| 7705 | * If there is a '0', only accept a match if the line is empty. |
| 7706 | * But may still match when typing last char of a word. |
| 7707 | */ |
| 7708 | if (*look == '0') |
| 7709 | { |
| 7710 | try_match_word = try_match; |
| 7711 | if (!line_is_empty) |
| 7712 | try_match = FALSE; |
| 7713 | ++look; |
| 7714 | } |
| 7715 | else |
| 7716 | try_match_word = FALSE; |
| 7717 | |
| 7718 | /* |
| 7719 | * does it look like a control character? |
| 7720 | */ |
| 7721 | if (*look == '^' |
| 7722 | #ifdef EBCDIC |
| 7723 | && (Ctrl_chr(look[1]) != 0) |
| 7724 | #else |
| 7725 | && look[1] >= '?' && look[1] <= '_' |
| 7726 | #endif |
| 7727 | ) |
| 7728 | { |
| 7729 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 7730 | return TRUE; |
| 7731 | look += 2; |
| 7732 | } |
| 7733 | /* |
| 7734 | * 'o' means "o" command, open forward. |
| 7735 | * 'O' means "O" command, open backward. |
| 7736 | */ |
| 7737 | else if (*look == 'o') |
| 7738 | { |
| 7739 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 7740 | return TRUE; |
| 7741 | ++look; |
| 7742 | } |
| 7743 | else if (*look == 'O') |
| 7744 | { |
| 7745 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 7746 | return TRUE; |
| 7747 | ++look; |
| 7748 | } |
| 7749 | |
| 7750 | /* |
| 7751 | * 'e' means to check for "else" at start of line and just before the |
| 7752 | * cursor. |
| 7753 | */ |
| 7754 | else if (*look == 'e') |
| 7755 | { |
| 7756 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 7757 | { |
| 7758 | p = ml_get_curline(); |
| 7759 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 7760 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 7761 | return TRUE; |
| 7762 | } |
| 7763 | ++look; |
| 7764 | } |
| 7765 | |
| 7766 | /* |
| 7767 | * ':' only causes an indent if it is at the end of a label or case |
| 7768 | * statement, or when it was before typing the ':' (to fix |
| 7769 | * class::method for C++). |
| 7770 | */ |
| 7771 | else if (*look == ':') |
| 7772 | { |
| 7773 | if (try_match && keytyped == ':') |
| 7774 | { |
| 7775 | p = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7776 | if (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
| 7777 | || cin_islabel(30)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7778 | return TRUE; |
Bram Moolenaar | 3011815 | 2007-06-28 10:49:22 +0000 | [diff] [blame] | 7779 | /* Need to get the line again after cin_islabel(). */ |
| 7780 | p = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7781 | if (curwin->w_cursor.col > 2 |
| 7782 | && p[curwin->w_cursor.col - 1] == ':' |
| 7783 | && p[curwin->w_cursor.col - 2] == ':') |
| 7784 | { |
| 7785 | p[curwin->w_cursor.col - 1] = ' '; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7786 | i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7787 | || cin_islabel(30)); |
| 7788 | p = ml_get_curline(); |
| 7789 | p[curwin->w_cursor.col - 1] = ':'; |
| 7790 | if (i) |
| 7791 | return TRUE; |
| 7792 | } |
| 7793 | } |
| 7794 | ++look; |
| 7795 | } |
| 7796 | |
| 7797 | |
| 7798 | /* |
| 7799 | * Is it a key in <>, maybe? |
| 7800 | */ |
| 7801 | else if (*look == '<') |
| 7802 | { |
| 7803 | if (try_match) |
| 7804 | { |
| 7805 | /* |
| 7806 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 7807 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 7808 | * >, *, : and ! keys if they really really want to. |
| 7809 | */ |
| 7810 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 7811 | && keytyped == look[1]) |
| 7812 | return TRUE; |
| 7813 | |
| 7814 | if (keytyped == get_special_key_code(look + 1)) |
| 7815 | return TRUE; |
| 7816 | } |
| 7817 | while (*look && *look != '>') |
| 7818 | look++; |
| 7819 | while (*look == '>') |
| 7820 | look++; |
| 7821 | } |
| 7822 | |
| 7823 | /* |
| 7824 | * Is it a word: "=word"? |
| 7825 | */ |
| 7826 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 7827 | { |
| 7828 | ++look; |
| 7829 | if (*look == '~') |
| 7830 | { |
| 7831 | icase = TRUE; |
| 7832 | ++look; |
| 7833 | } |
| 7834 | else |
| 7835 | icase = FALSE; |
| 7836 | p = vim_strchr(look, ','); |
| 7837 | if (p == NULL) |
| 7838 | p = look + STRLEN(look); |
| 7839 | if ((try_match || try_match_word) |
| 7840 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 7841 | { |
| 7842 | int match = FALSE; |
| 7843 | |
| 7844 | #ifdef FEAT_INS_EXPAND |
| 7845 | if (keytyped == KEY_COMPLETE) |
| 7846 | { |
| 7847 | char_u *s; |
| 7848 | |
| 7849 | /* Just completed a word, check if it starts with "look". |
| 7850 | * search back for the start of a word. */ |
| 7851 | line = ml_get_curline(); |
| 7852 | # ifdef FEAT_MBYTE |
| 7853 | if (has_mbyte) |
| 7854 | { |
| 7855 | char_u *n; |
| 7856 | |
| 7857 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 7858 | { |
| 7859 | n = mb_prevptr(line, s); |
| 7860 | if (!vim_iswordp(n)) |
| 7861 | break; |
| 7862 | } |
| 7863 | } |
| 7864 | else |
| 7865 | # endif |
| 7866 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 7867 | if (!vim_iswordc(s[-1])) |
| 7868 | break; |
| 7869 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 7870 | && (icase |
| 7871 | ? MB_STRNICMP(s, look, p - look) |
| 7872 | : STRNCMP(s, look, p - look)) == 0) |
| 7873 | match = TRUE; |
| 7874 | } |
| 7875 | else |
| 7876 | #endif |
| 7877 | /* TODO: multi-byte */ |
| 7878 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 7879 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 7880 | { |
| 7881 | line = ml_get_cursor(); |
| 7882 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 7883 | || !vim_iswordc(line[-(p - look) - 1])) |
| 7884 | && (icase |
| 7885 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 7886 | : STRNCMP(line - (p - look), look, p - look)) |
| 7887 | == 0) |
| 7888 | match = TRUE; |
| 7889 | } |
| 7890 | if (match && try_match_word && !try_match) |
| 7891 | { |
| 7892 | /* "0=word": Check if there are only blanks before the |
| 7893 | * word. */ |
| 7894 | line = ml_get_curline(); |
| 7895 | if ((int)(skipwhite(line) - line) != |
| 7896 | (int)(curwin->w_cursor.col - (p - look))) |
| 7897 | match = FALSE; |
| 7898 | } |
| 7899 | if (match) |
| 7900 | return TRUE; |
| 7901 | } |
| 7902 | look = p; |
| 7903 | } |
| 7904 | |
| 7905 | /* |
| 7906 | * ok, it's a boring generic character. |
| 7907 | */ |
| 7908 | else |
| 7909 | { |
| 7910 | if (try_match && *look == keytyped) |
| 7911 | return TRUE; |
| 7912 | ++look; |
| 7913 | } |
| 7914 | |
| 7915 | /* |
| 7916 | * Skip over ", ". |
| 7917 | */ |
| 7918 | look = skip_to_option_part(look); |
| 7919 | } |
| 7920 | return FALSE; |
| 7921 | } |
| 7922 | #endif /* FEAT_CINDENT */ |
| 7923 | |
| 7924 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 7925 | /* |
| 7926 | * Map Hebrew keyboard when in hkmap mode. |
| 7927 | */ |
| 7928 | int |
| 7929 | hkmap(c) |
| 7930 | int c; |
| 7931 | { |
| 7932 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 7933 | { |
| 7934 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 7935 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 7936 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 7937 | static char_u map[26] = |
| 7938 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 7939 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 7940 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 7941 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 7942 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 7943 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 7944 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 7945 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 7946 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 7947 | |
| 7948 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 7949 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 7950 | /* '-1'='sofit' */ |
| 7951 | else if (c == 'x') |
| 7952 | return 'X'; |
| 7953 | else if (c == 'q') |
| 7954 | return '\''; /* {geresh}={'} */ |
| 7955 | else if (c == 246) |
| 7956 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 7957 | else if (c == 228) |
| 7958 | return ' '; /* \"a --> ' ' -- / -- */ |
| 7959 | else if (c == 252) |
| 7960 | return ' '; /* \"u --> ' ' -- / -- */ |
| 7961 | #ifdef EBCDIC |
| 7962 | else if (islower(c)) |
| 7963 | #else |
| 7964 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 7965 | * do this the same was as 5.7 and previous, so it works correctly on |
| 7966 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 7967 | * munged and won't work if e.g. searching for Hebrew text. |
| 7968 | */ |
| 7969 | else if (c >= 'a' && c <= 'z') |
| 7970 | #endif |
| 7971 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 7972 | else |
| 7973 | return c; |
| 7974 | } |
| 7975 | else |
| 7976 | { |
| 7977 | switch (c) |
| 7978 | { |
| 7979 | case '`': return ';'; |
| 7980 | case '/': return '.'; |
| 7981 | case '\'': return ','; |
| 7982 | case 'q': return '/'; |
| 7983 | case 'w': return '\''; |
| 7984 | |
| 7985 | /* Hebrew letters - set offset from 'a' */ |
| 7986 | case ',': c = '{'; break; |
| 7987 | case '.': c = 'v'; break; |
| 7988 | case ';': c = 't'; break; |
| 7989 | default: { |
| 7990 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 7991 | |
| 7992 | #ifdef EBCDIC |
| 7993 | /* see note about islower() above */ |
| 7994 | if (!islower(c)) |
| 7995 | #else |
| 7996 | if (c < 'a' || c > 'z') |
| 7997 | #endif |
| 7998 | return c; |
| 7999 | c = str[CharOrdLow(c)]; |
| 8000 | break; |
| 8001 | } |
| 8002 | } |
| 8003 | |
| 8004 | return (int)(CharOrdLow(c) + p_aleph); |
| 8005 | } |
| 8006 | } |
| 8007 | #endif |
| 8008 | |
| 8009 | static void |
| 8010 | ins_reg() |
| 8011 | { |
| 8012 | int need_redraw = FALSE; |
| 8013 | int regname; |
| 8014 | int literally = 0; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8015 | #ifdef FEAT_VISUAL |
| 8016 | int vis_active = VIsual_active; |
| 8017 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8018 | |
| 8019 | /* |
| 8020 | * If we are going to wait for a character, show a '"'. |
| 8021 | */ |
| 8022 | pc_status = PC_STATUS_UNSET; |
| 8023 | if (redrawing() && !char_avail()) |
| 8024 | { |
| 8025 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8026 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8027 | |
| 8028 | edit_putchar('"', TRUE); |
| 8029 | #ifdef FEAT_CMDL_INFO |
| 8030 | add_to_showcmd_c(Ctrl_R); |
| 8031 | #endif |
| 8032 | } |
| 8033 | |
| 8034 | #ifdef USE_ON_FLY_SCROLL |
| 8035 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 8036 | #endif |
| 8037 | |
| 8038 | /* |
| 8039 | * Don't map the register name. This also prevents the mode message to be |
| 8040 | * deleted when ESC is hit. |
| 8041 | */ |
| 8042 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8043 | regname = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8044 | LANGMAP_ADJUST(regname, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8045 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 8046 | { |
| 8047 | /* Get a third key for literal register insertion */ |
| 8048 | literally = regname; |
| 8049 | #ifdef FEAT_CMDL_INFO |
| 8050 | add_to_showcmd_c(literally); |
| 8051 | #endif |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8052 | regname = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8053 | LANGMAP_ADJUST(regname, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8054 | } |
| 8055 | --no_mapping; |
| 8056 | |
| 8057 | #ifdef FEAT_EVAL |
| 8058 | /* |
| 8059 | * Don't call u_sync() while getting the expression, |
| 8060 | * evaluating it or giving an error message for it! |
| 8061 | */ |
| 8062 | ++no_u_sync; |
| 8063 | if (regname == '=') |
| 8064 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8065 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8066 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8067 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8068 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8069 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8070 | /* Restore the Input Method. */ |
| 8071 | if (im_on) |
| 8072 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8073 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8074 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 8075 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 8076 | { |
| 8077 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8078 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 8079 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8080 | else |
| 8081 | { |
| 8082 | #endif |
| 8083 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 8084 | { |
| 8085 | /* Append the command to the redo buffer. */ |
| 8086 | AppendCharToRedobuff(Ctrl_R); |
| 8087 | AppendCharToRedobuff(literally); |
| 8088 | AppendCharToRedobuff(regname); |
| 8089 | |
| 8090 | do_put(regname, BACKWARD, 1L, |
| 8091 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 8092 | } |
| 8093 | else if (insert_reg(regname, literally) == FAIL) |
| 8094 | { |
| 8095 | vim_beep(); |
| 8096 | need_redraw = TRUE; /* remove the '"' */ |
| 8097 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8098 | else if (stop_insert_mode) |
| 8099 | /* When the '=' register was used and a function was invoked that |
| 8100 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 8101 | * insert anything, need to remove the '"' */ |
| 8102 | need_redraw = TRUE; |
| 8103 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8104 | #ifdef FEAT_EVAL |
| 8105 | } |
| 8106 | --no_u_sync; |
| 8107 | #endif |
| 8108 | #ifdef FEAT_CMDL_INFO |
| 8109 | clear_showcmd(); |
| 8110 | #endif |
| 8111 | |
| 8112 | /* If the inserted register is empty, we need to remove the '"' */ |
| 8113 | if (need_redraw || stuff_empty()) |
| 8114 | edit_unputchar(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8115 | |
| 8116 | #ifdef FEAT_VISUAL |
| 8117 | /* Disallow starting Visual mode here, would get a weird mode. */ |
| 8118 | if (!vis_active && VIsual_active) |
| 8119 | end_visual_mode(); |
| 8120 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8121 | } |
| 8122 | |
| 8123 | /* |
| 8124 | * CTRL-G commands in Insert mode. |
| 8125 | */ |
| 8126 | static void |
| 8127 | ins_ctrl_g() |
| 8128 | { |
| 8129 | int c; |
| 8130 | |
| 8131 | #ifdef FEAT_INS_EXPAND |
| 8132 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 8133 | setcursor(); |
| 8134 | #endif |
| 8135 | |
| 8136 | /* |
| 8137 | * Don't map the second key. This also prevents the mode message to be |
| 8138 | * deleted when ESC is hit. |
| 8139 | */ |
| 8140 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8141 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8142 | --no_mapping; |
| 8143 | switch (c) |
| 8144 | { |
| 8145 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 8146 | case K_UP: |
| 8147 | case Ctrl_K: |
| 8148 | case 'k': ins_up(TRUE); |
| 8149 | break; |
| 8150 | |
| 8151 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 8152 | case K_DOWN: |
| 8153 | case Ctrl_J: |
| 8154 | case 'j': ins_down(TRUE); |
| 8155 | break; |
| 8156 | |
| 8157 | /* CTRL-G u: start new undoable edit */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 8158 | case 'u': u_sync(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8159 | ins_need_undo = TRUE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 8160 | |
| 8161 | /* Need to reset Insstart, esp. because a BS that joins |
Bram Moolenaar | 34e0bfa | 2007-05-10 18:44:18 +0000 | [diff] [blame] | 8162 | * a line to the previous one must save for undo. */ |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 8163 | Insstart = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8164 | break; |
| 8165 | |
| 8166 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 8167 | default: vim_beep(); |
| 8168 | } |
| 8169 | } |
| 8170 | |
| 8171 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8172 | * CTRL-^ in Insert mode. |
| 8173 | */ |
| 8174 | static void |
| 8175 | ins_ctrl_hat() |
| 8176 | { |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 8177 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8178 | { |
| 8179 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 8180 | if (State & LANGMAP) |
| 8181 | { |
| 8182 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 8183 | State &= ~LANGMAP; |
| 8184 | } |
| 8185 | else |
| 8186 | { |
| 8187 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 8188 | State |= LANGMAP; |
| 8189 | #ifdef USE_IM_CONTROL |
| 8190 | im_set_active(FALSE); |
| 8191 | #endif |
| 8192 | } |
| 8193 | } |
| 8194 | #ifdef USE_IM_CONTROL |
| 8195 | else |
| 8196 | { |
| 8197 | /* There are no ":lmap" mappings, toggle IM */ |
| 8198 | if (im_get_status()) |
| 8199 | { |
| 8200 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 8201 | im_set_active(FALSE); |
| 8202 | } |
| 8203 | else |
| 8204 | { |
| 8205 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 8206 | State &= ~LANGMAP; |
| 8207 | im_set_active(TRUE); |
| 8208 | } |
| 8209 | } |
| 8210 | #endif |
| 8211 | set_iminsert_global(); |
| 8212 | showmode(); |
| 8213 | #ifdef FEAT_GUI |
| 8214 | /* may show different cursor shape or color */ |
| 8215 | if (gui.in_use) |
| 8216 | gui_update_cursor(TRUE, FALSE); |
| 8217 | #endif |
| 8218 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 8219 | /* Show/unshow value of 'keymap' in status lines. */ |
| 8220 | status_redraw_curbuf(); |
| 8221 | #endif |
| 8222 | } |
| 8223 | |
| 8224 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8225 | * Handle ESC in insert mode. |
| 8226 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 8227 | * insert. |
| 8228 | */ |
| 8229 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8230 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8231 | long *count; |
| 8232 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8233 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8234 | { |
| 8235 | int temp; |
| 8236 | static int disabled_redraw = FALSE; |
| 8237 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 8238 | #ifdef FEAT_SPELL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8239 | check_spell_redraw(); |
| 8240 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8241 | #if defined(FEAT_HANGULIN) |
| 8242 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 8243 | hangul_input_state_set(0); |
| 8244 | # endif |
| 8245 | if (composing_hangul) |
| 8246 | { |
| 8247 | push_raw_key(composing_hangul_buffer, 2); |
| 8248 | composing_hangul = 0; |
| 8249 | } |
| 8250 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8251 | |
| 8252 | temp = curwin->w_cursor.col; |
| 8253 | if (disabled_redraw) |
| 8254 | { |
| 8255 | --RedrawingDisabled; |
| 8256 | disabled_redraw = FALSE; |
| 8257 | } |
| 8258 | if (!arrow_used) |
| 8259 | { |
| 8260 | /* |
| 8261 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 8262 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 8263 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8264 | */ |
| 8265 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 8266 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8267 | |
| 8268 | /* |
| 8269 | * Repeating insert may take a long time. Check for |
| 8270 | * interrupt now and then. |
| 8271 | */ |
| 8272 | if (*count > 0) |
| 8273 | { |
| 8274 | line_breakcheck(); |
| 8275 | if (got_int) |
| 8276 | *count = 0; |
| 8277 | } |
| 8278 | |
| 8279 | if (--*count > 0) /* repeat what was typed */ |
| 8280 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 8281 | /* Vi repeats the insert without replacing characters. */ |
| 8282 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 8283 | State &= ~REPLACE_FLAG; |
| 8284 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8285 | (void)start_redo_ins(); |
| 8286 | if (cmdchar == 'r' || cmdchar == 'v') |
| 8287 | stuffReadbuff(ESC_STR); /* no ESC in redo buffer */ |
| 8288 | ++RedrawingDisabled; |
| 8289 | disabled_redraw = TRUE; |
| 8290 | return FALSE; /* repeat the insert */ |
| 8291 | } |
| 8292 | stop_insert(&curwin->w_cursor, TRUE); |
| 8293 | undisplay_dollar(); |
| 8294 | } |
| 8295 | |
| 8296 | /* When an autoindent was removed, curswant stays after the |
| 8297 | * indent */ |
| 8298 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 8299 | curwin->w_set_curswant = TRUE; |
| 8300 | |
| 8301 | /* Remember the last Insert position in the '^ mark. */ |
| 8302 | if (!cmdmod.keepjumps) |
| 8303 | curbuf->b_last_insert = curwin->w_cursor; |
| 8304 | |
| 8305 | /* |
| 8306 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8307 | * 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] | 8308 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8309 | if (!nomove |
| 8310 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8311 | #ifdef FEAT_VIRTUALEDIT |
| 8312 | || curwin->w_cursor.coladd > 0 |
| 8313 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8314 | ) |
| 8315 | && (restart_edit == NUL |
| 8316 | || (gchar_cursor() == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8317 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8318 | && !VIsual_active |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8319 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8320 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8321 | #ifdef FEAT_RIGHTLEFT |
| 8322 | && !revins_on |
| 8323 | #endif |
| 8324 | ) |
| 8325 | { |
| 8326 | #ifdef FEAT_VIRTUALEDIT |
| 8327 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 8328 | { |
| 8329 | oneleft(); |
| 8330 | if (restart_edit != NUL) |
| 8331 | ++curwin->w_cursor.coladd; |
| 8332 | } |
| 8333 | else |
| 8334 | #endif |
| 8335 | { |
| 8336 | --curwin->w_cursor.col; |
| 8337 | #ifdef FEAT_MBYTE |
| 8338 | /* Correct cursor for multi-byte character. */ |
| 8339 | if (has_mbyte) |
| 8340 | mb_adjust_cursor(); |
| 8341 | #endif |
| 8342 | } |
| 8343 | } |
| 8344 | |
| 8345 | #ifdef USE_IM_CONTROL |
| 8346 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 8347 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 8348 | * well). */ |
| 8349 | if (!(State & LANGMAP)) |
| 8350 | im_save_status(&curbuf->b_p_iminsert); |
| 8351 | im_set_active(FALSE); |
| 8352 | #endif |
| 8353 | |
| 8354 | State = NORMAL; |
| 8355 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 8356 | changed_cline_bef_curs(); |
| 8357 | |
| 8358 | #ifdef FEAT_MOUSE |
| 8359 | setmouse(); |
| 8360 | #endif |
| 8361 | #ifdef CURSOR_SHAPE |
| 8362 | ui_cursor_shape(); /* may show different cursor shape */ |
| 8363 | #endif |
| 8364 | |
| 8365 | /* |
| 8366 | * When recording or for CTRL-O, need to display the new mode. |
| 8367 | * Otherwise remove the mode message. |
| 8368 | */ |
| 8369 | if (Recording || restart_edit != NUL) |
| 8370 | showmode(); |
| 8371 | else if (p_smd) |
| 8372 | MSG(""); |
| 8373 | |
| 8374 | return TRUE; /* exit Insert mode */ |
| 8375 | } |
| 8376 | |
| 8377 | #ifdef FEAT_RIGHTLEFT |
| 8378 | /* |
| 8379 | * Toggle language: hkmap and revins_on. |
| 8380 | * Move to end of reverse inserted text. |
| 8381 | */ |
| 8382 | static void |
| 8383 | ins_ctrl_() |
| 8384 | { |
| 8385 | if (revins_on && revins_chars && revins_scol >= 0) |
| 8386 | { |
| 8387 | while (gchar_cursor() != NUL && revins_chars--) |
| 8388 | ++curwin->w_cursor.col; |
| 8389 | } |
| 8390 | p_ri = !p_ri; |
| 8391 | revins_on = (State == INSERT && p_ri); |
| 8392 | if (revins_on) |
| 8393 | { |
| 8394 | revins_scol = curwin->w_cursor.col; |
| 8395 | revins_legal++; |
| 8396 | revins_chars = 0; |
| 8397 | undisplay_dollar(); |
| 8398 | } |
| 8399 | else |
| 8400 | revins_scol = -1; |
| 8401 | #ifdef FEAT_FKMAP |
| 8402 | if (p_altkeymap) |
| 8403 | { |
| 8404 | /* |
| 8405 | * to be consistent also for redo command, using '.' |
| 8406 | * set arrow_used to true and stop it - causing to redo |
| 8407 | * characters entered in one mode (normal/reverse insert). |
| 8408 | */ |
| 8409 | arrow_used = TRUE; |
| 8410 | (void)stop_arrow(); |
| 8411 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 8412 | if (p_fkmap && p_ri) |
| 8413 | State = INSERT; |
| 8414 | } |
| 8415 | else |
| 8416 | #endif |
| 8417 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 8418 | showmode(); |
| 8419 | } |
| 8420 | #endif |
| 8421 | |
| 8422 | #ifdef FEAT_VISUAL |
| 8423 | /* |
| 8424 | * If 'keymodel' contains "startsel", may start selection. |
| 8425 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 8426 | */ |
| 8427 | static int |
| 8428 | ins_start_select(c) |
| 8429 | int c; |
| 8430 | { |
| 8431 | if (km_startsel) |
| 8432 | switch (c) |
| 8433 | { |
| 8434 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8435 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8436 | case K_PAGEUP: |
| 8437 | case K_KPAGEUP: |
| 8438 | case K_PAGEDOWN: |
| 8439 | case K_KPAGEDOWN: |
| 8440 | # ifdef MACOS |
| 8441 | case K_LEFT: |
| 8442 | case K_RIGHT: |
| 8443 | case K_UP: |
| 8444 | case K_DOWN: |
| 8445 | case K_END: |
| 8446 | case K_HOME: |
| 8447 | # endif |
| 8448 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 8449 | break; |
| 8450 | /* FALLTHROUGH */ |
| 8451 | case K_S_LEFT: |
| 8452 | case K_S_RIGHT: |
| 8453 | case K_S_UP: |
| 8454 | case K_S_DOWN: |
| 8455 | case K_S_END: |
| 8456 | case K_S_HOME: |
| 8457 | /* Start selection right away, the cursor can move with |
| 8458 | * CTRL-O when beyond the end of the line. */ |
| 8459 | start_selection(); |
| 8460 | |
| 8461 | /* Execute the key in (insert) Select mode. */ |
| 8462 | stuffcharReadbuff(Ctrl_O); |
| 8463 | if (mod_mask) |
| 8464 | { |
| 8465 | char_u buf[4]; |
| 8466 | |
| 8467 | buf[0] = K_SPECIAL; |
| 8468 | buf[1] = KS_MODIFIER; |
| 8469 | buf[2] = mod_mask; |
| 8470 | buf[3] = NUL; |
| 8471 | stuffReadbuff(buf); |
| 8472 | } |
| 8473 | stuffcharReadbuff(c); |
| 8474 | return TRUE; |
| 8475 | } |
| 8476 | return FALSE; |
| 8477 | } |
| 8478 | #endif |
| 8479 | |
| 8480 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8481 | * <Insert> key in Insert mode: toggle insert/remplace mode. |
| 8482 | */ |
| 8483 | static void |
| 8484 | ins_insert(replaceState) |
| 8485 | int replaceState; |
| 8486 | { |
| 8487 | #ifdef FEAT_FKMAP |
| 8488 | if (p_fkmap && p_ri) |
| 8489 | { |
| 8490 | beep_flush(); |
| 8491 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 8492 | return; |
| 8493 | } |
| 8494 | #endif |
| 8495 | |
| 8496 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 8497 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8498 | set_vim_var_string(VV_INSERTMODE, |
| 8499 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 8500 | # ifdef FEAT_VREPLACE |
| 8501 | replaceState == VREPLACE ? "v" : |
| 8502 | # endif |
| 8503 | "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 8504 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8505 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 8506 | #endif |
| 8507 | if (State & REPLACE_FLAG) |
| 8508 | State = INSERT | (State & LANGMAP); |
| 8509 | else |
| 8510 | State = replaceState | (State & LANGMAP); |
| 8511 | AppendCharToRedobuff(K_INS); |
| 8512 | showmode(); |
| 8513 | #ifdef CURSOR_SHAPE |
| 8514 | ui_cursor_shape(); /* may show different cursor shape */ |
| 8515 | #endif |
| 8516 | } |
| 8517 | |
| 8518 | /* |
| 8519 | * Pressed CTRL-O in Insert mode. |
| 8520 | */ |
| 8521 | static void |
| 8522 | ins_ctrl_o() |
| 8523 | { |
| 8524 | #ifdef FEAT_VREPLACE |
| 8525 | if (State & VREPLACE_FLAG) |
| 8526 | restart_edit = 'V'; |
| 8527 | else |
| 8528 | #endif |
| 8529 | if (State & REPLACE_FLAG) |
| 8530 | restart_edit = 'R'; |
| 8531 | else |
| 8532 | restart_edit = 'I'; |
| 8533 | #ifdef FEAT_VIRTUALEDIT |
| 8534 | if (virtual_active()) |
| 8535 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 8536 | else |
| 8537 | #endif |
| 8538 | ins_at_eol = (gchar_cursor() == NUL); |
| 8539 | } |
| 8540 | |
| 8541 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8542 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 8543 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 8544 | * Always round the indent to 'shiftwidth', this is compatible |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8545 | * with vi. But vi only supports ^T and ^D after an |
| 8546 | * autoindent, we support it everywhere. |
| 8547 | */ |
| 8548 | static void |
| 8549 | ins_shift(c, lastc) |
| 8550 | int c; |
| 8551 | int lastc; |
| 8552 | { |
| 8553 | if (stop_arrow() == FAIL) |
| 8554 | return; |
| 8555 | AppendCharToRedobuff(c); |
| 8556 | |
| 8557 | /* |
| 8558 | * 0^D and ^^D: remove all indent. |
| 8559 | */ |
Bram Moolenaar | 0cbac5b | 2007-07-29 13:03:35 +0000 | [diff] [blame] | 8560 | if (c == Ctrl_D && (lastc == '0' || lastc == '^') |
| 8561 | && curwin->w_cursor.col > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8562 | { |
| 8563 | --curwin->w_cursor.col; |
| 8564 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 8565 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 8566 | if (State & REPLACE_FLAG) |
| 8567 | replace_pop_ins(); |
| 8568 | if (lastc == '^') |
| 8569 | old_indent = get_indent(); /* remember curr. indent */ |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 8570 | change_indent(INDENT_SET, 0, TRUE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8571 | } |
| 8572 | else |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 8573 | change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8574 | |
| 8575 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 8576 | did_ai = FALSE; |
| 8577 | #ifdef FEAT_SMARTINDENT |
| 8578 | did_si = FALSE; |
| 8579 | can_si = FALSE; |
| 8580 | can_si_back = FALSE; |
| 8581 | #endif |
| 8582 | #ifdef FEAT_CINDENT |
| 8583 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 8584 | #endif |
| 8585 | } |
| 8586 | |
| 8587 | static void |
| 8588 | ins_del() |
| 8589 | { |
| 8590 | int temp; |
| 8591 | |
| 8592 | if (stop_arrow() == FAIL) |
| 8593 | return; |
| 8594 | if (gchar_cursor() == NUL) /* delete newline */ |
| 8595 | { |
| 8596 | temp = curwin->w_cursor.col; |
| 8597 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 8598 | || do_join(2, FALSE, TRUE, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8599 | vim_beep(); |
| 8600 | else |
| 8601 | curwin->w_cursor.col = temp; |
| 8602 | } |
| 8603 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 8604 | vim_beep(); |
| 8605 | did_ai = FALSE; |
| 8606 | #ifdef FEAT_SMARTINDENT |
| 8607 | did_si = FALSE; |
| 8608 | can_si = FALSE; |
| 8609 | can_si_back = FALSE; |
| 8610 | #endif |
| 8611 | AppendCharToRedobuff(K_DEL); |
| 8612 | } |
| 8613 | |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8614 | static void ins_bs_one __ARGS((colnr_T *vcolp)); |
| 8615 | |
| 8616 | /* |
| 8617 | * Delete one character for ins_bs(). |
| 8618 | */ |
| 8619 | static void |
| 8620 | ins_bs_one(vcolp) |
| 8621 | colnr_T *vcolp; |
| 8622 | { |
| 8623 | dec_cursor(); |
| 8624 | getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL); |
| 8625 | if (State & REPLACE_FLAG) |
| 8626 | { |
| 8627 | /* Don't delete characters before the insert point when in |
| 8628 | * Replace mode */ |
| 8629 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 8630 | || curwin->w_cursor.col >= Insstart.col) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 8631 | replace_do_bs(-1); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8632 | } |
| 8633 | else |
| 8634 | (void)del_char(FALSE); |
| 8635 | } |
| 8636 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8637 | /* |
| 8638 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 8639 | * Return TRUE when backspace was actually used. |
| 8640 | */ |
| 8641 | static int |
| 8642 | ins_bs(c, mode, inserted_space_p) |
| 8643 | int c; |
| 8644 | int mode; |
| 8645 | int *inserted_space_p; |
| 8646 | { |
| 8647 | linenr_T lnum; |
| 8648 | int cc; |
| 8649 | int temp = 0; /* init for GCC */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8650 | colnr_T save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8651 | colnr_T mincol; |
| 8652 | int did_backspace = FALSE; |
| 8653 | int in_indent; |
| 8654 | int oldState; |
| 8655 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8656 | int cpc[MAX_MCO]; /* composing characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8657 | #endif |
| 8658 | |
| 8659 | /* |
| 8660 | * can't delete anything in an empty file |
| 8661 | * can't backup past first character in buffer |
| 8662 | * can't backup past starting point unless 'backspace' > 1 |
| 8663 | * can backup to a previous line if 'backspace' == 0 |
| 8664 | */ |
| 8665 | if ( bufempty() |
| 8666 | || ( |
| 8667 | #ifdef FEAT_RIGHTLEFT |
| 8668 | !revins_on && |
| 8669 | #endif |
| 8670 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 8671 | || (!can_bs(BS_START) |
| 8672 | && (arrow_used |
| 8673 | || (curwin->w_cursor.lnum == Insstart.lnum |
| 8674 | && curwin->w_cursor.col <= Insstart.col))) |
| 8675 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 8676 | && curwin->w_cursor.col <= ai_col) |
| 8677 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 8678 | { |
| 8679 | vim_beep(); |
| 8680 | return FALSE; |
| 8681 | } |
| 8682 | |
| 8683 | if (stop_arrow() == FAIL) |
| 8684 | return FALSE; |
| 8685 | in_indent = inindent(0); |
| 8686 | #ifdef FEAT_CINDENT |
| 8687 | if (in_indent) |
| 8688 | can_cindent = FALSE; |
| 8689 | #endif |
| 8690 | #ifdef FEAT_COMMENTS |
| 8691 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 8692 | #endif |
| 8693 | #ifdef FEAT_RIGHTLEFT |
| 8694 | if (revins_on) /* put cursor after last inserted char */ |
| 8695 | inc_cursor(); |
| 8696 | #endif |
| 8697 | |
| 8698 | #ifdef FEAT_VIRTUALEDIT |
| 8699 | /* Virtualedit: |
| 8700 | * BACKSPACE_CHAR eats a virtual space |
| 8701 | * BACKSPACE_WORD eats all coladd |
| 8702 | * BACKSPACE_LINE eats all coladd and keeps going |
| 8703 | */ |
| 8704 | if (curwin->w_cursor.coladd > 0) |
| 8705 | { |
| 8706 | if (mode == BACKSPACE_CHAR) |
| 8707 | { |
| 8708 | --curwin->w_cursor.coladd; |
| 8709 | return TRUE; |
| 8710 | } |
| 8711 | if (mode == BACKSPACE_WORD) |
| 8712 | { |
| 8713 | curwin->w_cursor.coladd = 0; |
| 8714 | return TRUE; |
| 8715 | } |
| 8716 | curwin->w_cursor.coladd = 0; |
| 8717 | } |
| 8718 | #endif |
| 8719 | |
| 8720 | /* |
| 8721 | * delete newline! |
| 8722 | */ |
| 8723 | if (curwin->w_cursor.col == 0) |
| 8724 | { |
| 8725 | lnum = Insstart.lnum; |
| 8726 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8727 | #ifdef FEAT_RIGHTLEFT |
| 8728 | || revins_on |
| 8729 | #endif |
| 8730 | ) |
| 8731 | { |
| 8732 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 8733 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 8734 | return FALSE; |
| 8735 | --Insstart.lnum; |
| 8736 | Insstart.col = MAXCOL; |
| 8737 | } |
| 8738 | /* |
| 8739 | * In replace mode: |
| 8740 | * cc < 0: NL was inserted, delete it |
| 8741 | * cc >= 0: NL was replaced, put original characters back |
| 8742 | */ |
| 8743 | cc = -1; |
| 8744 | if (State & REPLACE_FLAG) |
| 8745 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 8746 | /* |
| 8747 | * In replace mode, in the line we started replacing, we only move the |
| 8748 | * cursor. |
| 8749 | */ |
| 8750 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 8751 | { |
| 8752 | dec_cursor(); |
| 8753 | } |
| 8754 | else |
| 8755 | { |
| 8756 | #ifdef FEAT_VREPLACE |
| 8757 | if (!(State & VREPLACE_FLAG) |
| 8758 | || curwin->w_cursor.lnum > orig_line_count) |
| 8759 | #endif |
| 8760 | { |
| 8761 | temp = gchar_cursor(); /* remember current char */ |
| 8762 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8763 | |
| 8764 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 8765 | * the end of the line, otherwise the line will be broken |
| 8766 | * again when auto-formatting. */ |
| 8767 | if (has_format_option(FO_AUTO) |
| 8768 | && has_format_option(FO_WHITE_PAR)) |
| 8769 | { |
| 8770 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 8771 | TRUE); |
| 8772 | int len; |
| 8773 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8774 | len = (int)STRLEN(ptr); |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8775 | if (len > 0 && ptr[len - 1] == ' ') |
| 8776 | ptr[len - 1] = NUL; |
| 8777 | } |
| 8778 | |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 8779 | (void)do_join(2, FALSE, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8780 | if (temp == NUL && gchar_cursor() != NUL) |
| 8781 | inc_cursor(); |
| 8782 | } |
| 8783 | #ifdef FEAT_VREPLACE |
| 8784 | else |
| 8785 | dec_cursor(); |
| 8786 | #endif |
| 8787 | |
| 8788 | /* |
| 8789 | * In REPLACE mode we have to put back the text that was replaced |
| 8790 | * by the NL. On the replace stack is first a NUL-terminated |
| 8791 | * sequence of characters that were deleted and then the |
| 8792 | * characters that NL replaced. |
| 8793 | */ |
| 8794 | if (State & REPLACE_FLAG) |
| 8795 | { |
| 8796 | /* |
| 8797 | * Do the next ins_char() in NORMAL state, to |
| 8798 | * prevent ins_char() from replacing characters and |
| 8799 | * avoiding showmatch(). |
| 8800 | */ |
| 8801 | oldState = State; |
| 8802 | State = NORMAL; |
| 8803 | /* |
| 8804 | * restore characters (blanks) deleted after cursor |
| 8805 | */ |
| 8806 | while (cc > 0) |
| 8807 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8808 | save_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8809 | #ifdef FEAT_MBYTE |
| 8810 | mb_replace_pop_ins(cc); |
| 8811 | #else |
| 8812 | ins_char(cc); |
| 8813 | #endif |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8814 | curwin->w_cursor.col = save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8815 | cc = replace_pop(); |
| 8816 | } |
| 8817 | /* restore the characters that NL replaced */ |
| 8818 | replace_pop_ins(); |
| 8819 | State = oldState; |
| 8820 | } |
| 8821 | } |
| 8822 | did_ai = FALSE; |
| 8823 | } |
| 8824 | else |
| 8825 | { |
| 8826 | /* |
| 8827 | * Delete character(s) before the cursor. |
| 8828 | */ |
| 8829 | #ifdef FEAT_RIGHTLEFT |
| 8830 | if (revins_on) /* put cursor on last inserted char */ |
| 8831 | dec_cursor(); |
| 8832 | #endif |
| 8833 | mincol = 0; |
| 8834 | /* keep indent */ |
Bram Moolenaar | 9248e6e | 2007-03-08 12:10:13 +0000 | [diff] [blame] | 8835 | if (mode == BACKSPACE_LINE |
| 8836 | && (curbuf->b_p_ai |
| 8837 | #ifdef FEAT_CINDENT |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 8838 | || cindent_on() |
Bram Moolenaar | 9248e6e | 2007-03-08 12:10:13 +0000 | [diff] [blame] | 8839 | #endif |
| 8840 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8841 | #ifdef FEAT_RIGHTLEFT |
| 8842 | && !revins_on |
| 8843 | #endif |
| 8844 | ) |
| 8845 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8846 | save_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8847 | beginline(BL_WHITE); |
Bram Moolenaar | 7667556 | 2009-11-11 12:22:32 +0000 | [diff] [blame] | 8848 | if (curwin->w_cursor.col < save_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8849 | mincol = curwin->w_cursor.col; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8850 | curwin->w_cursor.col = save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
| 8853 | /* |
| 8854 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 8855 | */ |
| 8856 | if ( mode == BACKSPACE_CHAR |
| 8857 | && ((p_sta && in_indent) |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 8858 | || (curbuf->b_p_sts != 0 |
Bram Moolenaar | 7b88a0e | 2008-01-09 09:14:13 +0000 | [diff] [blame] | 8859 | && curwin->w_cursor.col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8860 | && (*(ml_get_cursor() - 1) == TAB |
| 8861 | || (*(ml_get_cursor() - 1) == ' ' |
| 8862 | && (!*inserted_space_p |
| 8863 | || arrow_used)))))) |
| 8864 | { |
| 8865 | int ts; |
| 8866 | colnr_T vcol; |
| 8867 | colnr_T want_vcol; |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8868 | colnr_T start_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8869 | |
| 8870 | *inserted_space_p = FALSE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 8871 | if (p_sta && in_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8872 | ts = curbuf->b_p_sw; |
| 8873 | else |
| 8874 | ts = curbuf->b_p_sts; |
| 8875 | /* Compute the virtual column where we want to be. Since |
| 8876 | * 'showbreak' may get in the way, need to get the last column of |
| 8877 | * the previous character. */ |
| 8878 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8879 | start_vcol = vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8880 | dec_cursor(); |
| 8881 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 8882 | inc_cursor(); |
| 8883 | want_vcol = (want_vcol / ts) * ts; |
| 8884 | |
| 8885 | /* delete characters until we are at or before want_vcol */ |
| 8886 | while (vcol > want_vcol |
| 8887 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8888 | ins_bs_one(&vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8889 | |
| 8890 | /* insert extra spaces until we are at want_vcol */ |
| 8891 | while (vcol < want_vcol) |
| 8892 | { |
| 8893 | /* Remember the first char we inserted */ |
| 8894 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8895 | && curwin->w_cursor.col < Insstart.col) |
| 8896 | Insstart.col = curwin->w_cursor.col; |
| 8897 | |
| 8898 | #ifdef FEAT_VREPLACE |
| 8899 | if (State & VREPLACE_FLAG) |
| 8900 | ins_char(' '); |
| 8901 | else |
| 8902 | #endif |
| 8903 | { |
| 8904 | ins_str((char_u *)" "); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8905 | if ((State & REPLACE_FLAG)) |
| 8906 | replace_push(NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8907 | } |
| 8908 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 8909 | } |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8910 | |
| 8911 | /* If we are now back where we started delete one character. Can |
| 8912 | * happen when using 'sts' and 'linebreak'. */ |
| 8913 | if (vcol >= start_vcol) |
| 8914 | ins_bs_one(&vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8915 | } |
| 8916 | |
| 8917 | /* |
| 8918 | * Delete upto starting point, start of line or previous word. |
| 8919 | */ |
| 8920 | else do |
| 8921 | { |
| 8922 | #ifdef FEAT_RIGHTLEFT |
| 8923 | if (!revins_on) /* put cursor on char to be deleted */ |
| 8924 | #endif |
| 8925 | dec_cursor(); |
| 8926 | |
| 8927 | /* start of word? */ |
| 8928 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 8929 | { |
| 8930 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 8931 | temp = vim_iswordc(gchar_cursor()); |
| 8932 | } |
| 8933 | /* end of word? */ |
| 8934 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 8935 | && (vim_isspace(cc = gchar_cursor()) |
| 8936 | || vim_iswordc(cc) != temp)) |
| 8937 | { |
| 8938 | #ifdef FEAT_RIGHTLEFT |
| 8939 | if (!revins_on) |
| 8940 | #endif |
| 8941 | inc_cursor(); |
| 8942 | #ifdef FEAT_RIGHTLEFT |
| 8943 | else if (State & REPLACE_FLAG) |
| 8944 | dec_cursor(); |
| 8945 | #endif |
| 8946 | break; |
| 8947 | } |
| 8948 | if (State & REPLACE_FLAG) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 8949 | replace_do_bs(-1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8950 | else |
| 8951 | { |
| 8952 | #ifdef FEAT_MBYTE |
| 8953 | if (enc_utf8 && p_deco) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8954 | (void)utfc_ptr2char(ml_get_cursor(), cpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8955 | #endif |
| 8956 | (void)del_char(FALSE); |
| 8957 | #ifdef FEAT_MBYTE |
| 8958 | /* |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8959 | * If there are combining characters and 'delcombine' is set |
| 8960 | * move the cursor back. Don't back up before the base |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8961 | * character. |
| 8962 | */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8963 | if (enc_utf8 && p_deco && cpc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8964 | inc_cursor(); |
| 8965 | #endif |
| 8966 | #ifdef FEAT_RIGHTLEFT |
| 8967 | if (revins_chars) |
| 8968 | { |
| 8969 | revins_chars--; |
| 8970 | revins_legal++; |
| 8971 | } |
| 8972 | if (revins_on && gchar_cursor() == NUL) |
| 8973 | break; |
| 8974 | #endif |
| 8975 | } |
| 8976 | /* Just a single backspace?: */ |
| 8977 | if (mode == BACKSPACE_CHAR) |
| 8978 | break; |
| 8979 | } while ( |
| 8980 | #ifdef FEAT_RIGHTLEFT |
| 8981 | revins_on || |
| 8982 | #endif |
| 8983 | (curwin->w_cursor.col > mincol |
| 8984 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 8985 | || curwin->w_cursor.col != Insstart.col))); |
| 8986 | did_backspace = TRUE; |
| 8987 | } |
| 8988 | #ifdef FEAT_SMARTINDENT |
| 8989 | did_si = FALSE; |
| 8990 | can_si = FALSE; |
| 8991 | can_si_back = FALSE; |
| 8992 | #endif |
| 8993 | if (curwin->w_cursor.col <= 1) |
| 8994 | did_ai = FALSE; |
| 8995 | /* |
| 8996 | * It's a little strange to put backspaces into the redo |
| 8997 | * buffer, but it makes auto-indent a lot easier to deal |
| 8998 | * with. |
| 8999 | */ |
| 9000 | AppendCharToRedobuff(c); |
| 9001 | |
| 9002 | /* If deleted before the insertion point, adjust it */ |
| 9003 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 9004 | && curwin->w_cursor.col < Insstart.col) |
| 9005 | Insstart.col = curwin->w_cursor.col; |
| 9006 | |
| 9007 | /* vi behaviour: the cursor moves backward but the character that |
| 9008 | * was there remains visible |
| 9009 | * Vim behaviour: the cursor moves backward and the character that |
| 9010 | * was there is erased from the screen. |
| 9011 | * We can emulate the vi behaviour by pretending there is a dollar |
| 9012 | * displayed even when there isn't. |
| 9013 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 9014 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9015 | dollar_vcol = curwin->w_virtcol; |
| 9016 | |
Bram Moolenaar | ce3be47 | 2008-01-14 19:12:28 +0000 | [diff] [blame] | 9017 | #ifdef FEAT_FOLDING |
| 9018 | /* When deleting a char the cursor line must never be in a closed fold. |
| 9019 | * E.g., when 'foldmethod' is indent and deleting the first non-white |
| 9020 | * char before a Tab. */ |
| 9021 | if (did_backspace) |
| 9022 | foldOpenCursor(); |
| 9023 | #endif |
| 9024 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9025 | return did_backspace; |
| 9026 | } |
| 9027 | |
| 9028 | #ifdef FEAT_MOUSE |
| 9029 | static void |
| 9030 | ins_mouse(c) |
| 9031 | int c; |
| 9032 | { |
| 9033 | pos_T tpos; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 9034 | win_T *old_curwin = curwin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9035 | |
| 9036 | # ifdef FEAT_GUI |
| 9037 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 9038 | if (!gui.in_use) |
| 9039 | # endif |
| 9040 | if (!mouse_has(MOUSE_INSERT)) |
| 9041 | return; |
| 9042 | |
| 9043 | undisplay_dollar(); |
| 9044 | tpos = curwin->w_cursor; |
| 9045 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 9046 | { |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 9047 | #ifdef FEAT_WINDOWS |
| 9048 | win_T *new_curwin = curwin; |
| 9049 | |
| 9050 | if (curwin != old_curwin && win_valid(old_curwin)) |
| 9051 | { |
| 9052 | /* Mouse took us to another window. We need to go back to the |
| 9053 | * previous one to stop insert there properly. */ |
| 9054 | curwin = old_curwin; |
| 9055 | curbuf = curwin->w_buffer; |
| 9056 | } |
| 9057 | #endif |
| 9058 | start_arrow(curwin == old_curwin ? &tpos : NULL); |
| 9059 | #ifdef FEAT_WINDOWS |
| 9060 | if (curwin != new_curwin && win_valid(new_curwin)) |
| 9061 | { |
| 9062 | curwin = new_curwin; |
| 9063 | curbuf = curwin->w_buffer; |
| 9064 | } |
| 9065 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9066 | # ifdef FEAT_CINDENT |
| 9067 | can_cindent = TRUE; |
| 9068 | # endif |
| 9069 | } |
| 9070 | |
| 9071 | #ifdef FEAT_WINDOWS |
| 9072 | /* redraw status lines (in case another window became active) */ |
| 9073 | redraw_statuslines(); |
| 9074 | #endif |
| 9075 | } |
| 9076 | |
| 9077 | static void |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9078 | ins_mousescroll(dir) |
| 9079 | int dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9080 | { |
| 9081 | pos_T tpos; |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9082 | # if defined(FEAT_WINDOWS) |
| 9083 | win_T *old_curwin = curwin; |
| 9084 | # endif |
| 9085 | # ifdef FEAT_INS_EXPAND |
| 9086 | int did_scroll = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9087 | # endif |
| 9088 | |
| 9089 | tpos = curwin->w_cursor; |
| 9090 | |
| 9091 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9092 | /* Currently the mouse coordinates are only known in the GUI. */ |
| 9093 | if (gui.in_use && mouse_row >= 0 && mouse_col >= 0) |
| 9094 | { |
| 9095 | int row, col; |
| 9096 | |
| 9097 | row = mouse_row; |
| 9098 | col = mouse_col; |
| 9099 | |
| 9100 | /* find the window at the pointer coordinates */ |
| 9101 | curwin = mouse_find_win(&row, &col); |
| 9102 | curbuf = curwin->w_buffer; |
| 9103 | } |
| 9104 | if (curwin == old_curwin) |
| 9105 | # endif |
| 9106 | undisplay_dollar(); |
| 9107 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9108 | # ifdef FEAT_INS_EXPAND |
| 9109 | /* Don't scroll the window in which completion is being done. */ |
| 9110 | if (!pum_visible() |
| 9111 | # if defined(FEAT_WINDOWS) |
| 9112 | || curwin != old_curwin |
| 9113 | # endif |
| 9114 | ) |
| 9115 | # endif |
| 9116 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9117 | if (dir == MSCR_DOWN || dir == MSCR_UP) |
| 9118 | { |
| 9119 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 9120 | scroll_redraw(dir, |
| 9121 | (long)(curwin->w_botline - curwin->w_topline)); |
| 9122 | else |
| 9123 | scroll_redraw(dir, 3L); |
| 9124 | } |
| 9125 | #ifdef FEAT_GUI |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9126 | else |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9127 | { |
| 9128 | int val, step = 6; |
| 9129 | |
| 9130 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 9131 | step = W_WIDTH(curwin); |
| 9132 | val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step); |
| 9133 | if (val < 0) |
| 9134 | val = 0; |
| 9135 | gui_do_horiz_scroll(val, TRUE); |
| 9136 | } |
| 9137 | #endif |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9138 | # ifdef FEAT_INS_EXPAND |
| 9139 | did_scroll = TRUE; |
| 9140 | # endif |
| 9141 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9142 | |
| 9143 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 9144 | curwin->w_redr_status = TRUE; |
| 9145 | |
| 9146 | curwin = old_curwin; |
| 9147 | curbuf = curwin->w_buffer; |
| 9148 | # endif |
| 9149 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9150 | # ifdef FEAT_INS_EXPAND |
| 9151 | /* The popup menu may overlay the window, need to redraw it. |
| 9152 | * TODO: Would be more efficient to only redraw the windows that are |
| 9153 | * overlapped by the popup menu. */ |
| 9154 | if (pum_visible() && did_scroll) |
| 9155 | { |
| 9156 | redraw_all_later(NOT_VALID); |
| 9157 | ins_compl_show_pum(); |
| 9158 | } |
| 9159 | # endif |
| 9160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9161 | if (!equalpos(curwin->w_cursor, tpos)) |
| 9162 | { |
| 9163 | start_arrow(&tpos); |
| 9164 | # ifdef FEAT_CINDENT |
| 9165 | can_cindent = TRUE; |
| 9166 | # endif |
| 9167 | } |
| 9168 | } |
| 9169 | #endif |
| 9170 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9171 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 9172 | static void |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9173 | ins_tabline(c) |
| 9174 | int c; |
| 9175 | { |
| 9176 | /* We will be leaving the current window, unless closing another tab. */ |
| 9177 | if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE |
| 9178 | || (current_tab != 0 && current_tab != tabpage_index(curtab))) |
| 9179 | { |
| 9180 | undisplay_dollar(); |
| 9181 | start_arrow(&curwin->w_cursor); |
| 9182 | # ifdef FEAT_CINDENT |
| 9183 | can_cindent = TRUE; |
| 9184 | # endif |
| 9185 | } |
| 9186 | |
| 9187 | if (c == K_TABLINE) |
| 9188 | goto_tabpage(current_tab); |
| 9189 | else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 9190 | { |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9191 | handle_tabmenu(); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 9192 | redraw_statuslines(); /* will redraw the tabline when needed */ |
| 9193 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9194 | } |
| 9195 | #endif |
| 9196 | |
| 9197 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9198 | void |
| 9199 | ins_scroll() |
| 9200 | { |
| 9201 | pos_T tpos; |
| 9202 | |
| 9203 | undisplay_dollar(); |
| 9204 | tpos = curwin->w_cursor; |
| 9205 | if (gui_do_scroll()) |
| 9206 | { |
| 9207 | start_arrow(&tpos); |
| 9208 | # ifdef FEAT_CINDENT |
| 9209 | can_cindent = TRUE; |
| 9210 | # endif |
| 9211 | } |
| 9212 | } |
| 9213 | |
| 9214 | void |
| 9215 | ins_horscroll() |
| 9216 | { |
| 9217 | pos_T tpos; |
| 9218 | |
| 9219 | undisplay_dollar(); |
| 9220 | tpos = curwin->w_cursor; |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9221 | if (gui_do_horiz_scroll(scrollbar_value, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9222 | { |
| 9223 | start_arrow(&tpos); |
| 9224 | # ifdef FEAT_CINDENT |
| 9225 | can_cindent = TRUE; |
| 9226 | # endif |
| 9227 | } |
| 9228 | } |
| 9229 | #endif |
| 9230 | |
| 9231 | static void |
| 9232 | ins_left() |
| 9233 | { |
| 9234 | pos_T tpos; |
| 9235 | |
| 9236 | #ifdef FEAT_FOLDING |
| 9237 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9238 | foldOpenCursor(); |
| 9239 | #endif |
| 9240 | undisplay_dollar(); |
| 9241 | tpos = curwin->w_cursor; |
| 9242 | if (oneleft() == OK) |
| 9243 | { |
Bram Moolenaar | 39fecab | 2006-08-29 14:07:36 +0000 | [diff] [blame] | 9244 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 9245 | /* Only call start_arrow() when not busy with preediting, it will |
| 9246 | * break undo. K_LEFT is inserted in im_correct_cursor(). */ |
| 9247 | if (!im_is_preediting()) |
| 9248 | #endif |
| 9249 | start_arrow(&tpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9250 | #ifdef FEAT_RIGHTLEFT |
| 9251 | /* If exit reversed string, position is fixed */ |
| 9252 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 9253 | revins_legal++; |
| 9254 | revins_chars++; |
| 9255 | #endif |
| 9256 | } |
| 9257 | |
| 9258 | /* |
| 9259 | * if 'whichwrap' set for cursor in insert mode may go to |
| 9260 | * previous line |
| 9261 | */ |
| 9262 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 9263 | { |
| 9264 | start_arrow(&tpos); |
| 9265 | --(curwin->w_cursor.lnum); |
| 9266 | coladvance((colnr_T)MAXCOL); |
| 9267 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 9268 | } |
| 9269 | else |
| 9270 | vim_beep(); |
| 9271 | } |
| 9272 | |
| 9273 | static void |
| 9274 | ins_home(c) |
| 9275 | int c; |
| 9276 | { |
| 9277 | pos_T tpos; |
| 9278 | |
| 9279 | #ifdef FEAT_FOLDING |
| 9280 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9281 | foldOpenCursor(); |
| 9282 | #endif |
| 9283 | undisplay_dollar(); |
| 9284 | tpos = curwin->w_cursor; |
| 9285 | if (c == K_C_HOME) |
| 9286 | curwin->w_cursor.lnum = 1; |
| 9287 | curwin->w_cursor.col = 0; |
| 9288 | #ifdef FEAT_VIRTUALEDIT |
| 9289 | curwin->w_cursor.coladd = 0; |
| 9290 | #endif |
| 9291 | curwin->w_curswant = 0; |
| 9292 | start_arrow(&tpos); |
| 9293 | } |
| 9294 | |
| 9295 | static void |
| 9296 | ins_end(c) |
| 9297 | int c; |
| 9298 | { |
| 9299 | pos_T tpos; |
| 9300 | |
| 9301 | #ifdef FEAT_FOLDING |
| 9302 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9303 | foldOpenCursor(); |
| 9304 | #endif |
| 9305 | undisplay_dollar(); |
| 9306 | tpos = curwin->w_cursor; |
| 9307 | if (c == K_C_END) |
| 9308 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 9309 | coladvance((colnr_T)MAXCOL); |
| 9310 | curwin->w_curswant = MAXCOL; |
| 9311 | |
| 9312 | start_arrow(&tpos); |
| 9313 | } |
| 9314 | |
| 9315 | static void |
| 9316 | ins_s_left() |
| 9317 | { |
| 9318 | #ifdef FEAT_FOLDING |
| 9319 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9320 | foldOpenCursor(); |
| 9321 | #endif |
| 9322 | undisplay_dollar(); |
| 9323 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 9324 | { |
| 9325 | start_arrow(&curwin->w_cursor); |
| 9326 | (void)bck_word(1L, FALSE, FALSE); |
| 9327 | curwin->w_set_curswant = TRUE; |
| 9328 | } |
| 9329 | else |
| 9330 | vim_beep(); |
| 9331 | } |
| 9332 | |
| 9333 | static void |
| 9334 | ins_right() |
| 9335 | { |
| 9336 | #ifdef FEAT_FOLDING |
| 9337 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9338 | foldOpenCursor(); |
| 9339 | #endif |
| 9340 | undisplay_dollar(); |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 9341 | if (gchar_cursor() != NUL |
| 9342 | #ifdef FEAT_VIRTUALEDIT |
| 9343 | || virtual_active() |
| 9344 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9345 | ) |
| 9346 | { |
| 9347 | start_arrow(&curwin->w_cursor); |
| 9348 | curwin->w_set_curswant = TRUE; |
| 9349 | #ifdef FEAT_VIRTUALEDIT |
| 9350 | if (virtual_active()) |
| 9351 | oneright(); |
| 9352 | else |
| 9353 | #endif |
| 9354 | { |
| 9355 | #ifdef FEAT_MBYTE |
| 9356 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9357 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9358 | else |
| 9359 | #endif |
| 9360 | ++curwin->w_cursor.col; |
| 9361 | } |
| 9362 | |
| 9363 | #ifdef FEAT_RIGHTLEFT |
| 9364 | revins_legal++; |
| 9365 | if (revins_chars) |
| 9366 | revins_chars--; |
| 9367 | #endif |
| 9368 | } |
| 9369 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 9370 | * cursor to the next line */ |
| 9371 | else if (vim_strchr(p_ww, ']') != NULL |
| 9372 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 9373 | { |
| 9374 | start_arrow(&curwin->w_cursor); |
| 9375 | curwin->w_set_curswant = TRUE; |
| 9376 | ++curwin->w_cursor.lnum; |
| 9377 | curwin->w_cursor.col = 0; |
| 9378 | } |
| 9379 | else |
| 9380 | vim_beep(); |
| 9381 | } |
| 9382 | |
| 9383 | static void |
| 9384 | ins_s_right() |
| 9385 | { |
| 9386 | #ifdef FEAT_FOLDING |
| 9387 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9388 | foldOpenCursor(); |
| 9389 | #endif |
| 9390 | undisplay_dollar(); |
| 9391 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 9392 | || gchar_cursor() != NUL) |
| 9393 | { |
| 9394 | start_arrow(&curwin->w_cursor); |
| 9395 | (void)fwd_word(1L, FALSE, 0); |
| 9396 | curwin->w_set_curswant = TRUE; |
| 9397 | } |
| 9398 | else |
| 9399 | vim_beep(); |
| 9400 | } |
| 9401 | |
| 9402 | static void |
| 9403 | ins_up(startcol) |
| 9404 | int startcol; /* when TRUE move to Insstart.col */ |
| 9405 | { |
| 9406 | pos_T tpos; |
| 9407 | linenr_T old_topline = curwin->w_topline; |
| 9408 | #ifdef FEAT_DIFF |
| 9409 | int old_topfill = curwin->w_topfill; |
| 9410 | #endif |
| 9411 | |
| 9412 | undisplay_dollar(); |
| 9413 | tpos = curwin->w_cursor; |
| 9414 | if (cursor_up(1L, TRUE) == OK) |
| 9415 | { |
| 9416 | if (startcol) |
| 9417 | coladvance(getvcol_nolist(&Insstart)); |
| 9418 | if (old_topline != curwin->w_topline |
| 9419 | #ifdef FEAT_DIFF |
| 9420 | || old_topfill != curwin->w_topfill |
| 9421 | #endif |
| 9422 | ) |
| 9423 | redraw_later(VALID); |
| 9424 | start_arrow(&tpos); |
| 9425 | #ifdef FEAT_CINDENT |
| 9426 | can_cindent = TRUE; |
| 9427 | #endif |
| 9428 | } |
| 9429 | else |
| 9430 | vim_beep(); |
| 9431 | } |
| 9432 | |
| 9433 | static void |
| 9434 | ins_pageup() |
| 9435 | { |
| 9436 | pos_T tpos; |
| 9437 | |
| 9438 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9439 | |
| 9440 | #ifdef FEAT_WINDOWS |
| 9441 | if (mod_mask & MOD_MASK_CTRL) |
| 9442 | { |
| 9443 | /* <C-PageUp>: tab page back */ |
Bram Moolenaar | bc44482 | 2006-10-17 11:37:50 +0000 | [diff] [blame] | 9444 | if (first_tabpage->tp_next != NULL) |
| 9445 | { |
| 9446 | start_arrow(&curwin->w_cursor); |
| 9447 | goto_tabpage(-1); |
| 9448 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9449 | return; |
| 9450 | } |
| 9451 | #endif |
| 9452 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9453 | tpos = curwin->w_cursor; |
| 9454 | if (onepage(BACKWARD, 1L) == OK) |
| 9455 | { |
| 9456 | start_arrow(&tpos); |
| 9457 | #ifdef FEAT_CINDENT |
| 9458 | can_cindent = TRUE; |
| 9459 | #endif |
| 9460 | } |
| 9461 | else |
| 9462 | vim_beep(); |
| 9463 | } |
| 9464 | |
| 9465 | static void |
| 9466 | ins_down(startcol) |
| 9467 | int startcol; /* when TRUE move to Insstart.col */ |
| 9468 | { |
| 9469 | pos_T tpos; |
| 9470 | linenr_T old_topline = curwin->w_topline; |
| 9471 | #ifdef FEAT_DIFF |
| 9472 | int old_topfill = curwin->w_topfill; |
| 9473 | #endif |
| 9474 | |
| 9475 | undisplay_dollar(); |
| 9476 | tpos = curwin->w_cursor; |
| 9477 | if (cursor_down(1L, TRUE) == OK) |
| 9478 | { |
| 9479 | if (startcol) |
| 9480 | coladvance(getvcol_nolist(&Insstart)); |
| 9481 | if (old_topline != curwin->w_topline |
| 9482 | #ifdef FEAT_DIFF |
| 9483 | || old_topfill != curwin->w_topfill |
| 9484 | #endif |
| 9485 | ) |
| 9486 | redraw_later(VALID); |
| 9487 | start_arrow(&tpos); |
| 9488 | #ifdef FEAT_CINDENT |
| 9489 | can_cindent = TRUE; |
| 9490 | #endif |
| 9491 | } |
| 9492 | else |
| 9493 | vim_beep(); |
| 9494 | } |
| 9495 | |
| 9496 | static void |
| 9497 | ins_pagedown() |
| 9498 | { |
| 9499 | pos_T tpos; |
| 9500 | |
| 9501 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9502 | |
| 9503 | #ifdef FEAT_WINDOWS |
| 9504 | if (mod_mask & MOD_MASK_CTRL) |
| 9505 | { |
| 9506 | /* <C-PageDown>: tab page forward */ |
Bram Moolenaar | bc44482 | 2006-10-17 11:37:50 +0000 | [diff] [blame] | 9507 | if (first_tabpage->tp_next != NULL) |
| 9508 | { |
| 9509 | start_arrow(&curwin->w_cursor); |
| 9510 | goto_tabpage(0); |
| 9511 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9512 | return; |
| 9513 | } |
| 9514 | #endif |
| 9515 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9516 | tpos = curwin->w_cursor; |
| 9517 | if (onepage(FORWARD, 1L) == OK) |
| 9518 | { |
| 9519 | start_arrow(&tpos); |
| 9520 | #ifdef FEAT_CINDENT |
| 9521 | can_cindent = TRUE; |
| 9522 | #endif |
| 9523 | } |
| 9524 | else |
| 9525 | vim_beep(); |
| 9526 | } |
| 9527 | |
| 9528 | #ifdef FEAT_DND |
| 9529 | static void |
| 9530 | ins_drop() |
| 9531 | { |
| 9532 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 9533 | } |
| 9534 | #endif |
| 9535 | |
| 9536 | /* |
| 9537 | * Handle TAB in Insert or Replace mode. |
| 9538 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 9539 | */ |
| 9540 | static int |
| 9541 | ins_tab() |
| 9542 | { |
| 9543 | int ind; |
| 9544 | int i; |
| 9545 | int temp; |
| 9546 | |
| 9547 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 9548 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 9549 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 9550 | return FALSE; |
| 9551 | |
| 9552 | ind = inindent(0); |
| 9553 | #ifdef FEAT_CINDENT |
| 9554 | if (ind) |
| 9555 | can_cindent = FALSE; |
| 9556 | #endif |
| 9557 | |
| 9558 | /* |
| 9559 | * When nothing special, insert TAB like a normal character |
| 9560 | */ |
| 9561 | if (!curbuf->b_p_et |
| 9562 | && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw) |
| 9563 | && curbuf->b_p_sts == 0) |
| 9564 | return TRUE; |
| 9565 | |
| 9566 | if (stop_arrow() == FAIL) |
| 9567 | return TRUE; |
| 9568 | |
| 9569 | did_ai = FALSE; |
| 9570 | #ifdef FEAT_SMARTINDENT |
| 9571 | did_si = FALSE; |
| 9572 | can_si = FALSE; |
| 9573 | can_si_back = FALSE; |
| 9574 | #endif |
| 9575 | AppendToRedobuff((char_u *)"\t"); |
| 9576 | |
| 9577 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
| 9578 | temp = (int)curbuf->b_p_sw; |
| 9579 | else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */ |
| 9580 | temp = (int)curbuf->b_p_sts; |
| 9581 | else /* otherwise use 'tabstop' */ |
| 9582 | temp = (int)curbuf->b_p_ts; |
| 9583 | temp -= get_nolist_virtcol() % temp; |
| 9584 | |
| 9585 | /* |
| 9586 | * Insert the first space with ins_char(). It will delete one char in |
| 9587 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 9588 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 9589 | */ |
| 9590 | ins_char(' '); |
| 9591 | while (--temp > 0) |
| 9592 | { |
| 9593 | #ifdef FEAT_VREPLACE |
| 9594 | if (State & VREPLACE_FLAG) |
| 9595 | ins_char(' '); |
| 9596 | else |
| 9597 | #endif |
| 9598 | { |
| 9599 | ins_str((char_u *)" "); |
| 9600 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 9601 | replace_push(NUL); |
| 9602 | } |
| 9603 | } |
| 9604 | |
| 9605 | /* |
| 9606 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 9607 | */ |
| 9608 | if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind))) |
| 9609 | { |
| 9610 | char_u *ptr; |
| 9611 | #ifdef FEAT_VREPLACE |
| 9612 | char_u *saved_line = NULL; /* init for GCC */ |
| 9613 | pos_T pos; |
| 9614 | #endif |
| 9615 | pos_T fpos; |
| 9616 | pos_T *cursor; |
| 9617 | colnr_T want_vcol, vcol; |
| 9618 | int change_col = -1; |
| 9619 | int save_list = curwin->w_p_list; |
| 9620 | |
| 9621 | /* |
| 9622 | * Get the current line. For VREPLACE mode, don't make real changes |
| 9623 | * yet, just work on a copy of the line. |
| 9624 | */ |
| 9625 | #ifdef FEAT_VREPLACE |
| 9626 | if (State & VREPLACE_FLAG) |
| 9627 | { |
| 9628 | pos = curwin->w_cursor; |
| 9629 | cursor = &pos; |
| 9630 | saved_line = vim_strsave(ml_get_curline()); |
| 9631 | if (saved_line == NULL) |
| 9632 | return FALSE; |
| 9633 | ptr = saved_line + pos.col; |
| 9634 | } |
| 9635 | else |
| 9636 | #endif |
| 9637 | { |
| 9638 | ptr = ml_get_cursor(); |
| 9639 | cursor = &curwin->w_cursor; |
| 9640 | } |
| 9641 | |
| 9642 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 9643 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 9644 | curwin->w_p_list = FALSE; |
| 9645 | |
| 9646 | /* Find first white before the cursor */ |
| 9647 | fpos = curwin->w_cursor; |
| 9648 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 9649 | { |
| 9650 | --fpos.col; |
| 9651 | --ptr; |
| 9652 | } |
| 9653 | |
| 9654 | /* In Replace mode, don't change characters before the insert point. */ |
| 9655 | if ((State & REPLACE_FLAG) |
| 9656 | && fpos.lnum == Insstart.lnum |
| 9657 | && fpos.col < Insstart.col) |
| 9658 | { |
| 9659 | ptr += Insstart.col - fpos.col; |
| 9660 | fpos.col = Insstart.col; |
| 9661 | } |
| 9662 | |
| 9663 | /* compute virtual column numbers of first white and cursor */ |
| 9664 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 9665 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 9666 | |
| 9667 | /* Use as many TABs as possible. Beware of 'showbreak' and |
| 9668 | * 'linebreak' adding extra virtual columns. */ |
| 9669 | while (vim_iswhite(*ptr)) |
| 9670 | { |
| 9671 | i = lbr_chartabsize((char_u *)"\t", vcol); |
| 9672 | if (vcol + i > want_vcol) |
| 9673 | break; |
| 9674 | if (*ptr != TAB) |
| 9675 | { |
| 9676 | *ptr = TAB; |
| 9677 | if (change_col < 0) |
| 9678 | { |
| 9679 | change_col = fpos.col; /* Column of first change */ |
| 9680 | /* May have to adjust Insstart */ |
| 9681 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 9682 | Insstart.col = fpos.col; |
| 9683 | } |
| 9684 | } |
| 9685 | ++fpos.col; |
| 9686 | ++ptr; |
| 9687 | vcol += i; |
| 9688 | } |
| 9689 | |
| 9690 | if (change_col >= 0) |
| 9691 | { |
| 9692 | int repl_off = 0; |
| 9693 | |
| 9694 | /* Skip over the spaces we need. */ |
| 9695 | while (vcol < want_vcol && *ptr == ' ') |
| 9696 | { |
| 9697 | vcol += lbr_chartabsize(ptr, vcol); |
| 9698 | ++ptr; |
| 9699 | ++repl_off; |
| 9700 | } |
| 9701 | if (vcol > want_vcol) |
| 9702 | { |
| 9703 | /* Must have a char with 'showbreak' just before it. */ |
| 9704 | --ptr; |
| 9705 | --repl_off; |
| 9706 | } |
| 9707 | fpos.col += repl_off; |
| 9708 | |
| 9709 | /* Delete following spaces. */ |
| 9710 | i = cursor->col - fpos.col; |
| 9711 | if (i > 0) |
| 9712 | { |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 9713 | STRMOVE(ptr, ptr + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9714 | /* correct replace stack. */ |
| 9715 | if ((State & REPLACE_FLAG) |
| 9716 | #ifdef FEAT_VREPLACE |
| 9717 | && !(State & VREPLACE_FLAG) |
| 9718 | #endif |
| 9719 | ) |
| 9720 | for (temp = i; --temp >= 0; ) |
| 9721 | replace_join(repl_off); |
| 9722 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9723 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 9724 | if (netbeans_active()) |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9725 | { |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 9726 | netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1)); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9727 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 9728 | (char_u *)"\t", 1); |
| 9729 | } |
| 9730 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9731 | cursor->col -= i; |
| 9732 | |
| 9733 | #ifdef FEAT_VREPLACE |
| 9734 | /* |
| 9735 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 9736 | * backspacing over the changed spacing and then inserting the new |
| 9737 | * spacing. |
| 9738 | */ |
| 9739 | if (State & VREPLACE_FLAG) |
| 9740 | { |
| 9741 | /* Backspace from real cursor to change_col */ |
| 9742 | backspace_until_column(change_col); |
| 9743 | |
| 9744 | /* Insert each char in saved_line from changed_col to |
| 9745 | * ptr-cursor */ |
| 9746 | ins_bytes_len(saved_line + change_col, |
| 9747 | cursor->col - change_col); |
| 9748 | } |
| 9749 | #endif |
| 9750 | } |
| 9751 | |
| 9752 | #ifdef FEAT_VREPLACE |
| 9753 | if (State & VREPLACE_FLAG) |
| 9754 | vim_free(saved_line); |
| 9755 | #endif |
| 9756 | curwin->w_p_list = save_list; |
| 9757 | } |
| 9758 | |
| 9759 | return FALSE; |
| 9760 | } |
| 9761 | |
| 9762 | /* |
| 9763 | * Handle CR or NL in insert mode. |
| 9764 | * Return TRUE when out of memory or can't undo. |
| 9765 | */ |
| 9766 | static int |
| 9767 | ins_eol(c) |
| 9768 | int c; |
| 9769 | { |
| 9770 | int i; |
| 9771 | |
| 9772 | if (echeck_abbr(c + ABBR_OFF)) |
| 9773 | return FALSE; |
| 9774 | if (stop_arrow() == FAIL) |
| 9775 | return TRUE; |
| 9776 | undisplay_dollar(); |
| 9777 | |
| 9778 | /* |
| 9779 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 9780 | * character under the cursor. Only push a NUL on the replace stack, |
| 9781 | * nothing to put back when the NL is deleted. |
| 9782 | */ |
| 9783 | if ((State & REPLACE_FLAG) |
| 9784 | #ifdef FEAT_VREPLACE |
| 9785 | && !(State & VREPLACE_FLAG) |
| 9786 | #endif |
| 9787 | ) |
| 9788 | replace_push(NUL); |
| 9789 | |
| 9790 | /* |
| 9791 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 9792 | * replacing the next line, so we push all of the characters left on the |
| 9793 | * line onto the replace stack. This is not done here though, it is done |
| 9794 | * in open_line(). |
| 9795 | */ |
| 9796 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 9797 | #ifdef FEAT_VIRTUALEDIT |
| 9798 | /* Put cursor on NUL if on the last char and coladd is 1 (happens after |
| 9799 | * CTRL-O). */ |
| 9800 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 9801 | coladvance(getviscol()); |
| 9802 | #endif |
| 9803 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9804 | #ifdef FEAT_RIGHTLEFT |
| 9805 | # ifdef FEAT_FKMAP |
| 9806 | if (p_altkeymap && p_fkmap) |
| 9807 | fkmap(NL); |
| 9808 | # endif |
| 9809 | /* NL in reverse insert will always start in the end of |
| 9810 | * current line. */ |
| 9811 | if (revins_on) |
| 9812 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 9813 | #endif |
| 9814 | |
| 9815 | AppendToRedobuff(NL_STR); |
| 9816 | i = open_line(FORWARD, |
| 9817 | #ifdef FEAT_COMMENTS |
| 9818 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 9819 | #endif |
| 9820 | 0, old_indent); |
| 9821 | old_indent = 0; |
| 9822 | #ifdef FEAT_CINDENT |
| 9823 | can_cindent = TRUE; |
| 9824 | #endif |
Bram Moolenaar | 6ae133b | 2006-11-01 20:25:45 +0000 | [diff] [blame] | 9825 | #ifdef FEAT_FOLDING |
| 9826 | /* When inserting a line the cursor line must never be in a closed fold. */ |
| 9827 | foldOpenCursor(); |
| 9828 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9829 | |
| 9830 | return (!i); |
| 9831 | } |
| 9832 | |
| 9833 | #ifdef FEAT_DIGRAPHS |
| 9834 | /* |
| 9835 | * Handle digraph in insert mode. |
| 9836 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 9837 | * done. |
| 9838 | */ |
| 9839 | static int |
| 9840 | ins_digraph() |
| 9841 | { |
| 9842 | int c; |
| 9843 | int cc; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9844 | int did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9845 | |
| 9846 | pc_status = PC_STATUS_UNSET; |
| 9847 | if (redrawing() && !char_avail()) |
| 9848 | { |
| 9849 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9850 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9851 | |
| 9852 | edit_putchar('?', TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9853 | did_putchar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9854 | #ifdef FEAT_CMDL_INFO |
| 9855 | add_to_showcmd_c(Ctrl_K); |
| 9856 | #endif |
| 9857 | } |
| 9858 | |
| 9859 | #ifdef USE_ON_FLY_SCROLL |
| 9860 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 9861 | #endif |
| 9862 | |
| 9863 | /* don't map the digraph chars. This also prevents the |
| 9864 | * mode message to be deleted when ESC is hit */ |
| 9865 | ++no_mapping; |
| 9866 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 9867 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9868 | --no_mapping; |
| 9869 | --allow_keys; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9870 | if (did_putchar) |
| 9871 | /* when the line fits in 'columns' the '?' is at the start of the next |
| 9872 | * line and will not be removed by the redraw */ |
| 9873 | edit_unputchar(); |
Bram Moolenaar | 26dcc7e | 2010-07-14 22:35:55 +0200 | [diff] [blame] | 9874 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9875 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 9876 | { |
| 9877 | #ifdef FEAT_CMDL_INFO |
| 9878 | clear_showcmd(); |
| 9879 | #endif |
| 9880 | insert_special(c, TRUE, FALSE); |
| 9881 | return NUL; |
| 9882 | } |
| 9883 | if (c != ESC) |
| 9884 | { |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9885 | did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9886 | if (redrawing() && !char_avail()) |
| 9887 | { |
| 9888 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9889 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9890 | |
| 9891 | if (char2cells(c) == 1) |
| 9892 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9893 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9894 | edit_putchar(c, TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9895 | did_putchar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9896 | } |
| 9897 | #ifdef FEAT_CMDL_INFO |
| 9898 | add_to_showcmd_c(c); |
| 9899 | #endif |
| 9900 | } |
| 9901 | ++no_mapping; |
| 9902 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 9903 | cc = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9904 | --no_mapping; |
| 9905 | --allow_keys; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9906 | if (did_putchar) |
| 9907 | /* when the line fits in 'columns' the '?' is at the start of the |
| 9908 | * next line and will not be removed by a redraw */ |
| 9909 | edit_unputchar(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9910 | if (cc != ESC) |
| 9911 | { |
| 9912 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 9913 | c = getdigraph(c, cc, TRUE); |
| 9914 | #ifdef FEAT_CMDL_INFO |
| 9915 | clear_showcmd(); |
| 9916 | #endif |
| 9917 | return c; |
| 9918 | } |
| 9919 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9920 | #ifdef FEAT_CMDL_INFO |
| 9921 | clear_showcmd(); |
| 9922 | #endif |
| 9923 | return NUL; |
| 9924 | } |
| 9925 | #endif |
| 9926 | |
| 9927 | /* |
| 9928 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 9929 | * Returns the char to be inserted, or NUL if none found. |
| 9930 | */ |
Bram Moolenaar | 8320da4 | 2012-04-30 18:18:47 +0200 | [diff] [blame] | 9931 | int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9932 | ins_copychar(lnum) |
| 9933 | linenr_T lnum; |
| 9934 | { |
| 9935 | int c; |
| 9936 | int temp; |
| 9937 | char_u *ptr, *prev_ptr; |
| 9938 | |
| 9939 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 9940 | { |
| 9941 | vim_beep(); |
| 9942 | return NUL; |
| 9943 | } |
| 9944 | |
| 9945 | /* try to advance to the cursor column */ |
| 9946 | temp = 0; |
| 9947 | ptr = ml_get(lnum); |
| 9948 | prev_ptr = ptr; |
| 9949 | validate_virtcol(); |
| 9950 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 9951 | { |
| 9952 | prev_ptr = ptr; |
| 9953 | temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp); |
| 9954 | } |
| 9955 | if ((colnr_T)temp > curwin->w_virtcol) |
| 9956 | ptr = prev_ptr; |
| 9957 | |
| 9958 | #ifdef FEAT_MBYTE |
| 9959 | c = (*mb_ptr2char)(ptr); |
| 9960 | #else |
| 9961 | c = *ptr; |
| 9962 | #endif |
| 9963 | if (c == NUL) |
| 9964 | vim_beep(); |
| 9965 | return c; |
| 9966 | } |
| 9967 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 9968 | /* |
| 9969 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 9970 | */ |
| 9971 | static int |
| 9972 | ins_ctrl_ey(tc) |
| 9973 | int tc; |
| 9974 | { |
| 9975 | int c = tc; |
| 9976 | |
| 9977 | #ifdef FEAT_INS_EXPAND |
| 9978 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 9979 | { |
| 9980 | if (c == Ctrl_Y) |
| 9981 | scrolldown_clamp(); |
| 9982 | else |
| 9983 | scrollup_clamp(); |
| 9984 | redraw_later(VALID); |
| 9985 | } |
| 9986 | else |
| 9987 | #endif |
| 9988 | { |
| 9989 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 9990 | if (c != NUL) |
| 9991 | { |
| 9992 | long tw_save; |
| 9993 | |
| 9994 | /* The character must be taken literally, insert like it |
| 9995 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 9996 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 9997 | * CTRL-V, don't use it for these. */ |
| 9998 | if (c < 256 && !isalnum(c)) |
| 9999 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 10000 | tw_save = curbuf->b_p_tw; |
| 10001 | curbuf->b_p_tw = -1; |
| 10002 | insert_special(c, TRUE, FALSE); |
| 10003 | curbuf->b_p_tw = tw_save; |
| 10004 | #ifdef FEAT_RIGHTLEFT |
| 10005 | revins_chars++; |
| 10006 | revins_legal++; |
| 10007 | #endif |
| 10008 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 10009 | auto_format(FALSE, TRUE); |
| 10010 | } |
| 10011 | } |
| 10012 | return c; |
| 10013 | } |
| 10014 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10015 | #ifdef FEAT_SMARTINDENT |
| 10016 | /* |
| 10017 | * Try to do some very smart auto-indenting. |
| 10018 | * Used when inserting a "normal" character. |
| 10019 | */ |
| 10020 | static void |
| 10021 | ins_try_si(c) |
| 10022 | int c; |
| 10023 | { |
| 10024 | pos_T *pos, old_pos; |
| 10025 | char_u *ptr; |
| 10026 | int i; |
| 10027 | int temp; |
| 10028 | |
| 10029 | /* |
| 10030 | * do some very smart indenting when entering '{' or '}' |
| 10031 | */ |
| 10032 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 10033 | { |
| 10034 | /* |
| 10035 | * for '}' set indent equal to indent of line containing matching '{' |
| 10036 | */ |
| 10037 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 10038 | { |
| 10039 | old_pos = curwin->w_cursor; |
| 10040 | /* |
| 10041 | * If the matching '{' has a ')' immediately before it (ignoring |
| 10042 | * white-space), then line up with the start of the line |
| 10043 | * containing the matching '(' if there is one. This handles the |
| 10044 | * case where an "if (..\n..) {" statement continues over multiple |
| 10045 | * lines -- webb |
| 10046 | */ |
| 10047 | ptr = ml_get(pos->lnum); |
| 10048 | i = pos->col; |
| 10049 | if (i > 0) /* skip blanks before '{' */ |
| 10050 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 10051 | ; |
| 10052 | curwin->w_cursor.lnum = pos->lnum; |
| 10053 | curwin->w_cursor.col = i; |
| 10054 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 10055 | curwin->w_cursor = *pos; |
| 10056 | i = get_indent(); |
| 10057 | curwin->w_cursor = old_pos; |
| 10058 | #ifdef FEAT_VREPLACE |
| 10059 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 10060 | change_indent(INDENT_SET, i, FALSE, NUL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10061 | else |
| 10062 | #endif |
| 10063 | (void)set_indent(i, SIN_CHANGED); |
| 10064 | } |
| 10065 | else if (curwin->w_cursor.col > 0) |
| 10066 | { |
| 10067 | /* |
| 10068 | * when inserting '{' after "O" reduce indent, but not |
| 10069 | * more than indent of previous line |
| 10070 | */ |
| 10071 | temp = TRUE; |
| 10072 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 10073 | { |
| 10074 | old_pos = curwin->w_cursor; |
| 10075 | i = get_indent(); |
| 10076 | while (curwin->w_cursor.lnum > 1) |
| 10077 | { |
| 10078 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 10079 | |
| 10080 | /* ignore empty lines and lines starting with '#'. */ |
| 10081 | if (*ptr != '#' && *ptr != NUL) |
| 10082 | break; |
| 10083 | } |
| 10084 | if (get_indent() >= i) |
| 10085 | temp = FALSE; |
| 10086 | curwin->w_cursor = old_pos; |
| 10087 | } |
| 10088 | if (temp) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 10089 | shift_line(TRUE, FALSE, 1, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10090 | } |
| 10091 | } |
| 10092 | |
| 10093 | /* |
| 10094 | * set indent of '#' always to 0 |
| 10095 | */ |
| 10096 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 10097 | { |
| 10098 | /* remember current indent for next line */ |
| 10099 | old_indent = get_indent(); |
| 10100 | (void)set_indent(0, SIN_CHANGED); |
| 10101 | } |
| 10102 | |
| 10103 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 10104 | if (ai_col > curwin->w_cursor.col) |
| 10105 | ai_col = curwin->w_cursor.col; |
| 10106 | } |
| 10107 | #endif |
| 10108 | |
| 10109 | /* |
| 10110 | * Get the value that w_virtcol would have when 'list' is off. |
| 10111 | * Unless 'cpo' contains the 'L' flag. |
| 10112 | */ |
| 10113 | static colnr_T |
| 10114 | get_nolist_virtcol() |
| 10115 | { |
| 10116 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 10117 | return getvcol_nolist(&curwin->w_cursor); |
| 10118 | validate_virtcol(); |
| 10119 | return curwin->w_virtcol; |
| 10120 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10121 | |
| 10122 | #ifdef FEAT_AUTOCMD |
| 10123 | /* |
| 10124 | * Handle the InsertCharPre autocommand. |
| 10125 | * "c" is the character that was typed. |
| 10126 | * Return a pointer to allocated memory with the replacement string. |
| 10127 | * Return NULL to continue inserting "c". |
| 10128 | */ |
| 10129 | static char_u * |
| 10130 | do_insert_char_pre(c) |
| 10131 | int c; |
| 10132 | { |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10133 | char_u *res; |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10134 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10135 | |
| 10136 | /* Return quickly when there is nothing to do. */ |
| 10137 | if (!has_insertcharpre()) |
| 10138 | return NULL; |
| 10139 | |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10140 | #ifdef FEAT_MBYTE |
| 10141 | if (has_mbyte) |
| 10142 | buf[(*mb_char2bytes)(c, buf)] = NUL; |
| 10143 | else |
| 10144 | #endif |
| 10145 | { |
| 10146 | buf[0] = c; |
| 10147 | buf[1] = NUL; |
| 10148 | } |
| 10149 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10150 | /* Lock the text to avoid weird things from happening. */ |
| 10151 | ++textlock; |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10152 | set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */ |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10153 | |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10154 | res = NULL; |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10155 | if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf)) |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10156 | { |
| 10157 | /* Get the value of v:char. It may be empty or more than one |
| 10158 | * character. Only use it when changed, otherwise continue with the |
| 10159 | * original character to avoid breaking autoindent. */ |
| 10160 | if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0) |
| 10161 | res = vim_strsave(get_vim_var_str(VV_CHAR)); |
| 10162 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10163 | |
| 10164 | set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */ |
| 10165 | --textlock; |
| 10166 | |
| 10167 | return res; |
| 10168 | } |
| 10169 | #endif |