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 | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 37 | #define CTRL_X_EVAL 16 /* for builtin function complete() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 39 | #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 40 | #define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | |
| 42 | static char *ctrl_x_msgs[] = |
| 43 | { |
| 44 | N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 45 | 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] | 46 | NULL, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 47 | N_(" Whole line completion (^L^N^P)"), |
| 48 | N_(" File name completion (^F^N^P)"), |
| 49 | N_(" Tag completion (^]^N^P)"), |
| 50 | N_(" Path pattern completion (^N^P)"), |
| 51 | N_(" Definition completion (^D^N^P)"), |
| 52 | NULL, |
| 53 | N_(" Dictionary completion (^K^N^P)"), |
| 54 | N_(" Thesaurus completion (^T^N^P)"), |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 55 | N_(" Command-line completion (^V^N^P)"), |
| 56 | N_(" User defined completion (^U^N^P)"), |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 57 | N_(" Omni completion (^O^N^P)"), |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 58 | N_(" Spelling suggestion (s^N^P)"), |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 59 | N_(" Keyword Local completion (^N^P)"), |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 60 | NULL, /* CTRL_X_EVAL doesn't use msg. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 63 | static char e_hitend[] = N_("Hit end of paragraph"); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 64 | #ifdef FEAT_COMPL_FUNC |
| 65 | static char e_complwin[] = N_("E839: Completion function changed window"); |
| 66 | static char e_compldel[] = N_("E840: Completion function deleted text"); |
| 67 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * Structure used to store one match for insert completion. |
| 71 | */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 72 | typedef struct compl_S compl_T; |
| 73 | struct compl_S |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 74 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 75 | compl_T *cp_next; |
| 76 | compl_T *cp_prev; |
| 77 | char_u *cp_str; /* matched text */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 78 | char cp_icase; /* TRUE or FALSE: ignore case */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 79 | char_u *(cp_text[CPT_COUNT]); /* text for the menu */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 80 | char_u *cp_fname; /* file containing the match, allocated when |
| 81 | * cp_flags has FREE_FNAME */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 82 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 83 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 86 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 87 | #define FREE_FNAME (2) |
| 88 | |
| 89 | /* |
| 90 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 91 | * "compl_first_match" points to the start of the list. |
| 92 | * "compl_curr_match" points to the currently selected entry. |
| 93 | * "compl_shown_match" is different from compl_curr_match during |
| 94 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 96 | static compl_T *compl_first_match = NULL; |
| 97 | static compl_T *compl_curr_match = NULL; |
| 98 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 100 | /* After using a cursor key <Enter> selects a match in the popup menu, |
| 101 | * otherwise it inserts a line break. */ |
| 102 | static int compl_enter_selects = FALSE; |
| 103 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 104 | /* When "compl_leader" is not NULL only matches that start with this string |
| 105 | * are used. */ |
| 106 | static char_u *compl_leader = NULL; |
| 107 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 108 | static int compl_get_longest = FALSE; /* put longest common string |
| 109 | in compl_leader */ |
| 110 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 111 | static int compl_used_match; /* Selected one of the matches. When |
| 112 | FALSE the match was edited or using |
| 113 | the longest common string. */ |
| 114 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 115 | static int compl_was_interrupted = FALSE; /* didn't finish finding |
| 116 | completions. */ |
| 117 | |
| 118 | static int compl_restarting = FALSE; /* don't insert match */ |
| 119 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 120 | /* When the first completion is done "compl_started" is set. When it's |
| 121 | * FALSE the word to be completed must be located. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 122 | static int compl_started = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 123 | |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 124 | /* Set when doing something for completion that may call edit() recursively, |
| 125 | * which is not allowed. */ |
| 126 | static int compl_busy = FALSE; |
| 127 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 128 | static int compl_matches = 0; |
| 129 | static char_u *compl_pattern = NULL; |
| 130 | static int compl_direction = FORWARD; |
| 131 | static int compl_shows_dir = FORWARD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 132 | static int compl_pending = 0; /* > 1 for postponed CTRL-N */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 133 | static pos_T compl_startpos; |
| 134 | static colnr_T compl_col = 0; /* column where the text starts |
| 135 | * that is being completed */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 136 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 137 | * completion started */ |
| 138 | static int compl_cont_mode = 0; |
| 139 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 140 | |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 141 | static int compl_opt_refresh_always = FALSE; |
| 142 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 143 | static void ins_ctrl_x __ARGS((void)); |
| 144 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 145 | static int ins_compl_accept_char __ARGS((int c)); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 146 | 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] | 147 | 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] | 148 | static void ins_compl_longest_match __ARGS((compl_T *match)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 149 | 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] | 150 | static int ins_compl_make_cyclic __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 151 | static void ins_compl_upd_pum __ARGS((void)); |
| 152 | static void ins_compl_del_pum __ARGS((void)); |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 153 | static int pum_wanted __ARGS((void)); |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 154 | static int pum_enough_matches __ARGS((void)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 155 | 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] | 156 | 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] | 157 | static char_u *find_line_end __ARGS((char_u *ptr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 158 | static void ins_compl_free __ARGS((void)); |
| 159 | static void ins_compl_clear __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 160 | static int ins_compl_bs __ARGS((void)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 161 | static int ins_compl_need_restart __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 162 | static void ins_compl_new_leader __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 163 | static void ins_compl_addleader __ARGS((int c)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 164 | static int ins_compl_len __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 165 | static void ins_compl_restart __ARGS((void)); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 166 | static void ins_compl_set_original_text __ARGS((char_u *str)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 167 | static void ins_compl_addfrommatch __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 168 | static int ins_compl_prep __ARGS((int c)); |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 169 | static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | 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] | 171 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 172 | static void ins_compl_add_list __ARGS((list_T *list)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 173 | static void ins_compl_add_dict __ARGS((dict_T *dict)); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 174 | #endif |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 175 | static int ins_compl_get_exp __ARGS((pos_T *ini)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 176 | static void ins_compl_delete __ARGS((void)); |
| 177 | static void ins_compl_insert __ARGS((void)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 178 | 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] | 179 | static int ins_compl_key2dir __ARGS((int c)); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 180 | static int ins_compl_pum_key __ARGS((int c)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 181 | static int ins_compl_key2count __ARGS((int c)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 182 | static int ins_compl_use_match __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | static int ins_complete __ARGS((int c)); |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 184 | 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] | 185 | #endif /* FEAT_INS_EXPAND */ |
| 186 | |
| 187 | #define BACKSPACE_CHAR 1 |
| 188 | #define BACKSPACE_WORD 2 |
| 189 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 190 | #define BACKSPACE_LINE 4 |
| 191 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 192 | static void ins_redraw __ARGS((int ready)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | static void ins_ctrl_v __ARGS((void)); |
| 194 | static void undisplay_dollar __ARGS((void)); |
| 195 | static void insert_special __ARGS((int, int, int)); |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 196 | 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] | 197 | static void check_auto_format __ARGS((int)); |
| 198 | static void redo_literal __ARGS((int c)); |
| 199 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 200 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 201 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 202 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 203 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 204 | #endif |
Bram Moolenaar | f332a65 | 2013-11-04 04:20:33 +0100 | [diff] [blame] | 205 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 206 | static int echeck_abbr __ARGS((int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | static int replace_pop __ARGS((void)); |
| 208 | static void replace_join __ARGS((int off)); |
| 209 | static void replace_pop_ins __ARGS((void)); |
| 210 | #ifdef FEAT_MBYTE |
| 211 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 212 | #endif |
| 213 | static void replace_flush __ARGS((void)); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 214 | static void replace_do_bs __ARGS((int limit_col)); |
| 215 | static int del_char_after_col __ARGS((int limit_col)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | #ifdef FEAT_CINDENT |
| 217 | static int cindent_on __ARGS((void)); |
| 218 | #endif |
| 219 | static void ins_reg __ARGS((void)); |
| 220 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 221 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 222 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 223 | #ifdef FEAT_RIGHTLEFT |
| 224 | static void ins_ctrl_ __ARGS((void)); |
| 225 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 226 | static int ins_start_select __ARGS((int c)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 227 | static void ins_insert __ARGS((int replaceState)); |
| 228 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 229 | static void ins_shift __ARGS((int c, int lastc)); |
| 230 | static void ins_del __ARGS((void)); |
| 231 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 232 | #ifdef FEAT_MOUSE |
| 233 | static void ins_mouse __ARGS((int c)); |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 234 | static void ins_mousescroll __ARGS((int dir)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 236 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 237 | static void ins_tabline __ARGS((int c)); |
| 238 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | static void ins_left __ARGS((void)); |
| 240 | static void ins_home __ARGS((int c)); |
| 241 | static void ins_end __ARGS((int c)); |
| 242 | static void ins_s_left __ARGS((void)); |
| 243 | static void ins_right __ARGS((void)); |
| 244 | static void ins_s_right __ARGS((void)); |
| 245 | static void ins_up __ARGS((int startcol)); |
| 246 | static void ins_pageup __ARGS((void)); |
| 247 | static void ins_down __ARGS((int startcol)); |
| 248 | static void ins_pagedown __ARGS((void)); |
| 249 | #ifdef FEAT_DND |
| 250 | static void ins_drop __ARGS((void)); |
| 251 | #endif |
| 252 | static int ins_tab __ARGS((void)); |
| 253 | static int ins_eol __ARGS((int c)); |
| 254 | #ifdef FEAT_DIGRAPHS |
| 255 | static int ins_digraph __ARGS((void)); |
| 256 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 257 | static int ins_ctrl_ey __ARGS((int tc)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 258 | #ifdef FEAT_SMARTINDENT |
| 259 | static void ins_try_si __ARGS((int c)); |
| 260 | #endif |
| 261 | static colnr_T get_nolist_virtcol __ARGS((void)); |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 262 | #ifdef FEAT_AUTOCMD |
| 263 | static char_u *do_insert_char_pre __ARGS((int c)); |
| 264 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | |
| 266 | static colnr_T Insstart_textlen; /* length of line when insert started */ |
| 267 | static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */ |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 268 | static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | |
| 270 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 271 | K_SPECIAL and CSI are escaped */ |
| 272 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 273 | 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] | 274 | static int did_restart_edit; /* "restart_edit" when calling edit() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 275 | |
| 276 | #ifdef FEAT_CINDENT |
| 277 | static int can_cindent; /* may do cindenting on this line */ |
| 278 | #endif |
| 279 | |
| 280 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 281 | |
| 282 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 283 | static int revins_on; /* reverse insert mode on */ |
| 284 | static int revins_chars; /* how much to skip after edit */ |
| 285 | static int revins_legal; /* was the last char 'legal'? */ |
| 286 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 287 | #endif |
| 288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 289 | static int ins_need_undo; /* call u_save() before inserting a |
| 290 | char. Set when edit() is called. |
| 291 | after that arrow_used is used. */ |
| 292 | |
| 293 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 294 | under the cursor */ |
| 295 | |
| 296 | /* |
| 297 | * edit(): Start inserting text. |
| 298 | * |
| 299 | * "cmdchar" can be: |
| 300 | * 'i' normal insert command |
| 301 | * 'a' normal append command |
| 302 | * 'R' replace command |
| 303 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 304 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 305 | * 'g' "gI" command. |
| 306 | * 'V' "gR" command for Virtual Replace mode. |
| 307 | * 'v' "gr" command for single character Virtual Replace mode. |
| 308 | * |
| 309 | * This function is not called recursively. For CTRL-O commands, it returns |
| 310 | * and lets the caller handle the Normal-mode command. |
| 311 | * |
| 312 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 313 | */ |
| 314 | int |
| 315 | edit(cmdchar, startln, count) |
| 316 | int cmdchar; |
| 317 | int startln; /* if set, insert at start of line */ |
| 318 | long count; |
| 319 | { |
| 320 | int c = 0; |
| 321 | char_u *ptr; |
| 322 | int lastc; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 323 | int mincol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 325 | int i; |
| 326 | int did_backspace = TRUE; /* previous char was backspace */ |
| 327 | #ifdef FEAT_CINDENT |
| 328 | int line_is_white = FALSE; /* line is empty before insert */ |
| 329 | #endif |
| 330 | linenr_T old_topline = 0; /* topline before insertion */ |
| 331 | #ifdef FEAT_DIFF |
| 332 | int old_topfill = -1; |
| 333 | #endif |
| 334 | int inserted_space = FALSE; /* just inserted a space */ |
| 335 | int replaceState = REPLACE; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 336 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 337 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 338 | /* Remember whether editing was restarted after CTRL-O. */ |
| 339 | did_restart_edit = restart_edit; |
| 340 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 341 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 342 | * error message */ |
| 343 | check_for_delay(TRUE); |
| 344 | |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 345 | /* set Insstart_orig to Insstart */ |
| 346 | update_Insstart_orig = TRUE; |
| 347 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 348 | #ifdef HAVE_SANDBOX |
| 349 | /* Don't allow inserting in the sandbox. */ |
| 350 | if (sandbox != 0) |
| 351 | { |
| 352 | EMSG(_(e_sandbox)); |
| 353 | return FALSE; |
| 354 | } |
| 355 | #endif |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 356 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 357 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 358 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 359 | { |
| 360 | EMSG(_(e_secure)); |
| 361 | return FALSE; |
| 362 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 363 | |
| 364 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 365 | /* Don't allow recursive insert mode when busy with completion. */ |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 366 | if (compl_started || compl_busy || pum_visible()) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 367 | { |
| 368 | EMSG(_(e_secure)); |
| 369 | return FALSE; |
| 370 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 371 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 372 | #endif |
| 373 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 374 | #ifdef FEAT_AUTOCMD |
| 375 | /* |
| 376 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 377 | */ |
| 378 | if (cmdchar != 'r' && cmdchar != 'v') |
| 379 | { |
Bram Moolenaar | 3e37fd0 | 2013-01-17 15:37:01 +0100 | [diff] [blame] | 380 | pos_T save_cursor = curwin->w_cursor; |
| 381 | |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 382 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 383 | if (cmdchar == 'R') |
| 384 | ptr = (char_u *)"r"; |
| 385 | else if (cmdchar == 'V') |
| 386 | ptr = (char_u *)"v"; |
| 387 | else |
| 388 | ptr = (char_u *)"i"; |
| 389 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 097c992 | 2013-05-19 21:15:15 +0200 | [diff] [blame] | 390 | set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */ |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 391 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 392 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 3e37fd0 | 2013-01-17 15:37:01 +0100 | [diff] [blame] | 393 | |
Bram Moolenaar | 097c992 | 2013-05-19 21:15:15 +0200 | [diff] [blame] | 394 | /* Make sure the cursor didn't move. Do call check_cursor_col() in |
| 395 | * case the text was modified. Since Insert mode was not started yet |
| 396 | * a call to check_cursor_col() may move the cursor, especially with |
| 397 | * the "A" command, thus set State to avoid that. Also check that the |
| 398 | * line number is still valid (lines may have been deleted). |
| 399 | * Do not restore if v:char was set to a non-empty string. */ |
| 400 | if (!equalpos(curwin->w_cursor, save_cursor) |
| 401 | # ifdef FEAT_EVAL |
| 402 | && *get_vim_var_str(VV_CHAR) == NUL |
| 403 | # endif |
| 404 | && save_cursor.lnum <= curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 3e37fd0 | 2013-01-17 15:37:01 +0100 | [diff] [blame] | 405 | { |
| 406 | int save_state = State; |
| 407 | |
| 408 | curwin->w_cursor = save_cursor; |
| 409 | State = INSERT; |
| 410 | check_cursor_col(); |
| 411 | State = save_state; |
| 412 | } |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 413 | } |
| 414 | #endif |
| 415 | |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 416 | #ifdef FEAT_CONCEAL |
| 417 | /* Check if the cursor line needs redrawing before changing State. If |
| 418 | * 'concealcursor' is "n" it needs to be redrawn without concealing. */ |
Bram Moolenaar | 8e46927 | 2010-07-28 19:38:16 +0200 | [diff] [blame] | 419 | conceal_check_cursur_line(); |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 420 | #endif |
| 421 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | #ifdef FEAT_MOUSE |
| 423 | /* |
| 424 | * When doing a paste with the middle mouse button, Insstart is set to |
| 425 | * where the paste started. |
| 426 | */ |
| 427 | if (where_paste_started.lnum != 0) |
| 428 | Insstart = where_paste_started; |
| 429 | else |
| 430 | #endif |
| 431 | { |
| 432 | Insstart = curwin->w_cursor; |
| 433 | if (startln) |
| 434 | Insstart.col = 0; |
| 435 | } |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 436 | Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 437 | Insstart_blank_vcol = MAXCOL; |
| 438 | if (!did_ai) |
| 439 | ai_col = 0; |
| 440 | |
| 441 | if (cmdchar != NUL && restart_edit == 0) |
| 442 | { |
| 443 | ResetRedobuff(); |
| 444 | AppendNumberToRedobuff(count); |
| 445 | #ifdef FEAT_VREPLACE |
| 446 | if (cmdchar == 'V' || cmdchar == 'v') |
| 447 | { |
| 448 | /* "gR" or "gr" command */ |
| 449 | AppendCharToRedobuff('g'); |
| 450 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 451 | } |
| 452 | else |
| 453 | #endif |
| 454 | { |
| 455 | AppendCharToRedobuff(cmdchar); |
| 456 | if (cmdchar == 'g') /* "gI" command */ |
| 457 | AppendCharToRedobuff('I'); |
| 458 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 459 | count = 1; /* insert only one <CR> */ |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (cmdchar == 'R') |
| 464 | { |
| 465 | #ifdef FEAT_FKMAP |
| 466 | if (p_fkmap && p_ri) |
| 467 | { |
| 468 | beep_flush(); |
| 469 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 470 | State = INSERT; |
| 471 | } |
| 472 | else |
| 473 | #endif |
| 474 | State = REPLACE; |
| 475 | } |
| 476 | #ifdef FEAT_VREPLACE |
| 477 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 478 | { |
| 479 | State = VREPLACE; |
| 480 | replaceState = VREPLACE; |
| 481 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 482 | vr_lines_changed = 1; |
| 483 | } |
| 484 | #endif |
| 485 | else |
| 486 | State = INSERT; |
| 487 | |
| 488 | stop_insert_mode = FALSE; |
| 489 | |
| 490 | /* |
| 491 | * Need to recompute the cursor position, it might move when the cursor is |
| 492 | * on a TAB or special character. |
| 493 | */ |
| 494 | curs_columns(TRUE); |
| 495 | |
| 496 | /* |
| 497 | * Enable langmap or IME, indicated by 'iminsert'. |
| 498 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 499 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 500 | * when hitting <Esc>. |
| 501 | */ |
| 502 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 503 | State |= LANGMAP; |
| 504 | #ifdef USE_IM_CONTROL |
| 505 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 506 | #endif |
| 507 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | #ifdef FEAT_MOUSE |
| 509 | setmouse(); |
| 510 | #endif |
| 511 | #ifdef FEAT_CMDL_INFO |
| 512 | clear_showcmd(); |
| 513 | #endif |
| 514 | #ifdef FEAT_RIGHTLEFT |
| 515 | /* there is no reverse replace mode */ |
| 516 | revins_on = (State == INSERT && p_ri); |
| 517 | if (revins_on) |
| 518 | undisplay_dollar(); |
| 519 | revins_chars = 0; |
| 520 | revins_legal = 0; |
| 521 | revins_scol = -1; |
| 522 | #endif |
| 523 | |
| 524 | /* |
| 525 | * Handle restarting Insert mode. |
| 526 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 527 | * restart_edit non-zero, and something in the stuff buffer. |
| 528 | */ |
| 529 | if (restart_edit != 0 && stuff_empty()) |
| 530 | { |
| 531 | #ifdef FEAT_MOUSE |
| 532 | /* |
| 533 | * After a paste we consider text typed to be part of the insert for |
| 534 | * the pasted text. You can backspace over the pasted text too. |
| 535 | */ |
| 536 | if (where_paste_started.lnum) |
| 537 | arrow_used = FALSE; |
| 538 | else |
| 539 | #endif |
| 540 | arrow_used = TRUE; |
| 541 | restart_edit = 0; |
| 542 | |
| 543 | /* |
| 544 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 545 | * now at the end-of-line, put it after the end-of-line (this is not |
| 546 | * correct in very rare cases). |
| 547 | * Also do this if curswant is greater than the current virtual |
| 548 | * column. Eg after "^O$" or "^O80|". |
| 549 | */ |
| 550 | validate_virtcol(); |
| 551 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 552 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 553 | || curwin->w_curswant > curwin->w_virtcol) |
| 554 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 555 | { |
| 556 | if (ptr[1] == NUL) |
| 557 | ++curwin->w_cursor.col; |
| 558 | #ifdef FEAT_MBYTE |
| 559 | else if (has_mbyte) |
| 560 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 561 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 562 | if (ptr[i] == NUL) |
| 563 | curwin->w_cursor.col += i; |
| 564 | } |
| 565 | #endif |
| 566 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 567 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 568 | } |
| 569 | else |
| 570 | arrow_used = FALSE; |
| 571 | |
| 572 | /* we are in insert mode now, don't need to start it anymore */ |
| 573 | need_start_insertmode = FALSE; |
| 574 | |
| 575 | /* Need to save the line for undo before inserting the first char. */ |
| 576 | ins_need_undo = TRUE; |
| 577 | |
| 578 | #ifdef FEAT_MOUSE |
| 579 | where_paste_started.lnum = 0; |
| 580 | #endif |
| 581 | #ifdef FEAT_CINDENT |
| 582 | can_cindent = TRUE; |
| 583 | #endif |
| 584 | #ifdef FEAT_FOLDING |
| 585 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 586 | * restarting. */ |
| 587 | if (!p_im && did_restart_edit == 0) |
| 588 | foldOpenCursor(); |
| 589 | #endif |
| 590 | |
| 591 | /* |
| 592 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 593 | * A warning message for changing a readonly file is given here, before |
| 594 | * actually changing anything. It's put after the mode, if any. |
| 595 | */ |
| 596 | i = 0; |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 597 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | i = showmode(); |
| 599 | |
| 600 | if (!p_im && did_restart_edit == 0) |
Bram Moolenaar | 21af89e | 2008-01-02 21:09:33 +0000 | [diff] [blame] | 601 | change_warning(i == 0 ? 0 : i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 602 | |
| 603 | #ifdef CURSOR_SHAPE |
| 604 | ui_cursor_shape(); /* may show different cursor shape */ |
| 605 | #endif |
| 606 | #ifdef FEAT_DIGRAPHS |
| 607 | do_digraph(-1); /* clear digraphs */ |
| 608 | #endif |
| 609 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 610 | /* |
| 611 | * Get the current length of the redo buffer, those characters have to be |
| 612 | * skipped if we want to get to the inserted characters. |
| 613 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 614 | ptr = get_inserted(); |
| 615 | if (ptr == NULL) |
| 616 | new_insert_skip = 0; |
| 617 | else |
| 618 | { |
| 619 | new_insert_skip = (int)STRLEN(ptr); |
| 620 | vim_free(ptr); |
| 621 | } |
| 622 | |
| 623 | old_indent = 0; |
| 624 | |
| 625 | /* |
| 626 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 627 | */ |
| 628 | for (;;) |
| 629 | { |
| 630 | #ifdef FEAT_RIGHTLEFT |
| 631 | if (!revins_legal) |
| 632 | revins_scol = -1; /* reset on illegal motions */ |
| 633 | else |
| 634 | revins_legal = 0; |
| 635 | #endif |
| 636 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 637 | count = 0; |
| 638 | |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 639 | if (update_Insstart_orig) |
| 640 | Insstart_orig = Insstart; |
| 641 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 642 | if (stop_insert_mode) |
| 643 | { |
| 644 | /* ":stopinsert" used or 'insertmode' reset */ |
| 645 | count = 0; |
| 646 | goto doESCkey; |
| 647 | } |
| 648 | |
| 649 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 650 | if (!arrow_used) |
| 651 | curwin->w_set_curswant = TRUE; |
| 652 | |
| 653 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 654 | * menu invoked a shell command). */ |
| 655 | if (stuff_empty()) |
| 656 | { |
| 657 | did_check_timestamps = FALSE; |
| 658 | if (need_check_timestamps) |
| 659 | check_timestamps(FALSE); |
| 660 | } |
| 661 | |
| 662 | /* |
| 663 | * When emsg() was called msg_scroll will have been set. |
| 664 | */ |
| 665 | msg_scroll = FALSE; |
| 666 | |
| 667 | #ifdef FEAT_GUI |
| 668 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 669 | * another window. "need_mouse_correct" may then be set because of an |
| 670 | * autocommand. */ |
| 671 | if (need_mouse_correct) |
| 672 | gui_mouse_correct(); |
| 673 | #endif |
| 674 | |
| 675 | #ifdef FEAT_FOLDING |
| 676 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 677 | if (fdo_flags & FDO_INSERT) |
| 678 | foldOpenCursor(); |
| 679 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 680 | if (!char_avail()) |
| 681 | foldCheckClose(); |
| 682 | #endif |
| 683 | |
| 684 | /* |
| 685 | * If we inserted a character at the last position of the last line in |
| 686 | * the window, scroll the window one line up. This avoids an extra |
| 687 | * redraw. |
| 688 | * This is detected when the cursor column is smaller after inserting |
| 689 | * something. |
| 690 | * Don't do this when the topline changed already, it has |
| 691 | * already been adjusted (by insertchar() calling open_line())). |
| 692 | */ |
| 693 | if (curbuf->b_mod_set |
| 694 | && curwin->w_p_wrap |
| 695 | && !did_backspace |
| 696 | && curwin->w_topline == old_topline |
| 697 | #ifdef FEAT_DIFF |
| 698 | && curwin->w_topfill == old_topfill |
| 699 | #endif |
| 700 | ) |
| 701 | { |
| 702 | mincol = curwin->w_wcol; |
| 703 | validate_cursor_col(); |
| 704 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 705 | if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 706 | && curwin->w_wrow == W_WINROW(curwin) |
| 707 | + curwin->w_height - 1 - p_so |
| 708 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 709 | #ifdef FEAT_DIFF |
| 710 | || curwin->w_topfill > 0 |
| 711 | #endif |
| 712 | )) |
| 713 | { |
| 714 | #ifdef FEAT_DIFF |
| 715 | if (curwin->w_topfill > 0) |
| 716 | --curwin->w_topfill; |
| 717 | else |
| 718 | #endif |
| 719 | #ifdef FEAT_FOLDING |
| 720 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 721 | set_topline(curwin, old_topline + 1); |
| 722 | else |
| 723 | #endif |
| 724 | set_topline(curwin, curwin->w_topline + 1); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | /* May need to adjust w_topline to show the cursor. */ |
| 729 | update_topline(); |
| 730 | |
| 731 | did_backspace = FALSE; |
| 732 | |
| 733 | validate_cursor(); /* may set must_redraw */ |
| 734 | |
| 735 | /* |
| 736 | * Redraw the display when no characters are waiting. |
| 737 | * Also shows mode, ruler and positions cursor. |
| 738 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 739 | ins_redraw(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 740 | |
| 741 | #ifdef FEAT_SCROLLBIND |
| 742 | if (curwin->w_p_scb) |
| 743 | do_check_scrollbind(TRUE); |
| 744 | #endif |
| 745 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 746 | #ifdef FEAT_CURSORBIND |
| 747 | if (curwin->w_p_crb) |
| 748 | do_check_cursorbind(); |
| 749 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | update_curswant(); |
| 751 | old_topline = curwin->w_topline; |
| 752 | #ifdef FEAT_DIFF |
| 753 | old_topfill = curwin->w_topfill; |
| 754 | #endif |
| 755 | |
| 756 | #ifdef USE_ON_FLY_SCROLL |
| 757 | dont_scroll = FALSE; /* allow scrolling here */ |
| 758 | #endif |
| 759 | |
| 760 | /* |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 761 | * Get a character for Insert mode. Ignore K_IGNORE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 762 | */ |
| 763 | lastc = c; /* remember previous char for CTRL-D */ |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 764 | do |
| 765 | { |
| 766 | c = safe_vgetc(); |
| 767 | } while (c == K_IGNORE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 768 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 769 | #ifdef FEAT_AUTOCMD |
| 770 | /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */ |
| 771 | did_cursorhold = TRUE; |
| 772 | #endif |
| 773 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 774 | #ifdef FEAT_RIGHTLEFT |
| 775 | if (p_hkmap && KeyTyped) |
| 776 | c = hkmap(c); /* Hebrew mode mapping */ |
| 777 | #endif |
| 778 | #ifdef FEAT_FKMAP |
| 779 | if (p_fkmap && KeyTyped) |
| 780 | c = fkmap(c); /* Farsi mode mapping */ |
| 781 | #endif |
| 782 | |
| 783 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 784 | /* |
| 785 | * Special handling of keys while the popup menu is visible or wanted |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 786 | * and the cursor is still in the completed word. Only when there is |
| 787 | * a match, skip this when no matches were found. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 788 | */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 789 | if (compl_started |
| 790 | && pum_wanted() |
| 791 | && curwin->w_cursor.col >= compl_col |
| 792 | && (compl_shown_match == NULL |
| 793 | || compl_shown_match != compl_shown_match->cp_next)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 794 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 795 | /* BS: Delete one character from "compl_leader". */ |
| 796 | if ((c == K_BS || c == Ctrl_H) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 797 | && curwin->w_cursor.col > compl_col |
| 798 | && (c = ins_compl_bs()) == NUL) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 799 | continue; |
| 800 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 801 | /* When no match was selected or it was edited. */ |
| 802 | if (!compl_used_match) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 803 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 804 | /* CTRL-L: Add one character from the current match to |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 805 | * "compl_leader". Except when at the original match and |
| 806 | * there is nothing to add, CTRL-L works like CTRL-P then. */ |
| 807 | if (c == Ctrl_L |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 808 | && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode) |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 809 | || (int)STRLEN(compl_shown_match->cp_str) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 810 | > curwin->w_cursor.col - compl_col)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 811 | { |
| 812 | ins_compl_addfrommatch(); |
| 813 | continue; |
| 814 | } |
| 815 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 816 | /* A non-white character that fits in with the current |
| 817 | * completion: Add to "compl_leader". */ |
| 818 | if (ins_compl_accept_char(c)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 819 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 820 | #ifdef FEAT_AUTOCMD |
| 821 | /* Trigger InsertCharPre. */ |
| 822 | char_u *str = do_insert_char_pre(c); |
| 823 | char_u *p; |
| 824 | |
| 825 | if (str != NULL) |
| 826 | { |
| 827 | for (p = str; *p != NUL; mb_ptr_adv(p)) |
| 828 | ins_compl_addleader(PTR2CHAR(p)); |
| 829 | vim_free(str); |
| 830 | } |
| 831 | else |
| 832 | #endif |
| 833 | ins_compl_addleader(c); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 834 | continue; |
| 835 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 836 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 837 | /* Pressing CTRL-Y selects the current match. When |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 838 | * compl_enter_selects is set the Enter key does the same. */ |
| 839 | if (c == Ctrl_Y || (compl_enter_selects |
| 840 | && (c == CAR || c == K_KENTER || c == NL))) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 841 | { |
| 842 | ins_compl_delete(); |
| 843 | ins_compl_insert(); |
| 844 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 848 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 849 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 850 | compl_get_longest = FALSE; |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 851 | if (ins_compl_prep(c)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 852 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | #endif |
| 854 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 855 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 856 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 857 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 858 | if (c == Ctrl_BSL) |
| 859 | { |
| 860 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 861 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 862 | ++no_mapping; |
| 863 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 864 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 865 | --no_mapping; |
| 866 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 867 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 868 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 869 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 870 | vungetc(c); |
| 871 | c = Ctrl_BSL; |
| 872 | } |
| 873 | else if (c == Ctrl_G && p_im) |
| 874 | continue; |
| 875 | else |
| 876 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 877 | if (c == Ctrl_O) |
| 878 | { |
| 879 | ins_ctrl_o(); |
| 880 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 881 | nomove = TRUE; |
| 882 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 883 | count = 0; |
| 884 | goto doESCkey; |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | #ifdef FEAT_DIGRAPHS |
| 889 | c = do_digraph(c); |
| 890 | #endif |
| 891 | |
| 892 | #ifdef FEAT_INS_EXPAND |
| 893 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 894 | goto docomplete; |
| 895 | #endif |
| 896 | if (c == Ctrl_V || c == Ctrl_Q) |
| 897 | { |
| 898 | ins_ctrl_v(); |
| 899 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 900 | continue; |
| 901 | } |
| 902 | |
| 903 | #ifdef FEAT_CINDENT |
| 904 | if (cindent_on() |
| 905 | # ifdef FEAT_INS_EXPAND |
| 906 | && ctrl_x_mode == 0 |
| 907 | # endif |
| 908 | ) |
| 909 | { |
| 910 | /* A key name preceded by a bang means this key is not to be |
| 911 | * inserted. Skip ahead to the re-indenting below. |
| 912 | * A key name preceded by a star means that indenting has to be |
| 913 | * done before inserting the key. */ |
| 914 | line_is_white = inindent(0); |
| 915 | if (in_cinkeys(c, '!', line_is_white)) |
| 916 | goto force_cindent; |
| 917 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 918 | && stop_arrow() == OK) |
| 919 | do_c_expr_indent(); |
| 920 | } |
| 921 | #endif |
| 922 | |
| 923 | #ifdef FEAT_RIGHTLEFT |
| 924 | if (curwin->w_p_rl) |
| 925 | switch (c) |
| 926 | { |
| 927 | case K_LEFT: c = K_RIGHT; break; |
| 928 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 929 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 930 | case K_RIGHT: c = K_LEFT; break; |
| 931 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 932 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 933 | } |
| 934 | #endif |
| 935 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 936 | /* |
| 937 | * If 'keymodel' contains "startsel", may start selection. If it |
| 938 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 939 | * characters. |
| 940 | */ |
| 941 | if (ins_start_select(c)) |
| 942 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 943 | |
| 944 | /* |
| 945 | * The big switch to handle a character in insert mode. |
| 946 | */ |
| 947 | switch (c) |
| 948 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 949 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 951 | break; |
| 952 | /*FALLTHROUGH*/ |
| 953 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 954 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 955 | #ifdef FEAT_CMDWIN |
| 956 | if (c == Ctrl_C && cmdwin_type != 0) |
| 957 | { |
| 958 | /* Close the cmdline window. */ |
| 959 | cmdwin_result = K_IGNORE; |
| 960 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 961 | nomove = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 962 | goto doESCkey; |
| 963 | } |
| 964 | #endif |
| 965 | |
| 966 | #ifdef UNIX |
| 967 | do_intr: |
| 968 | #endif |
| 969 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 970 | * Insert mode */ |
| 971 | if (goto_im()) |
| 972 | { |
| 973 | if (got_int) |
| 974 | { |
| 975 | (void)vgetc(); /* flush all buffers */ |
| 976 | got_int = FALSE; |
| 977 | } |
| 978 | else |
| 979 | vim_beep(); |
| 980 | break; |
| 981 | } |
| 982 | doESCkey: |
| 983 | /* |
| 984 | * This is the ONLY return from edit()! |
| 985 | */ |
| 986 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 987 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 988 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 989 | o_lnum = curwin->w_cursor.lnum; |
| 990 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 991 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 992 | { |
| 993 | #ifdef FEAT_AUTOCMD |
| 994 | if (cmdchar != 'r' && cmdchar != 'v') |
| 995 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 996 | FALSE, curbuf); |
Bram Moolenaar | c0a0ab5 | 2006-10-06 18:39:58 +0000 | [diff] [blame] | 997 | did_cursorhold = FALSE; |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 998 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 1000 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1001 | continue; |
| 1002 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1003 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 1004 | if (!p_im) |
| 1005 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 1006 | stuffReadbuff((char_u *)":st\r"); |
| 1007 | c = Ctrl_O; |
| 1008 | /*FALLTHROUGH*/ |
| 1009 | |
| 1010 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 1011 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 1012 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1013 | goto docomplete; |
| 1014 | #endif |
| 1015 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 1016 | break; |
| 1017 | ins_ctrl_o(); |
Bram Moolenaar | 06a89a5 | 2006-04-29 22:01:03 +0000 | [diff] [blame] | 1018 | |
| 1019 | #ifdef FEAT_VIRTUALEDIT |
| 1020 | /* don't move the cursor left when 'virtualedit' has "onemore". */ |
| 1021 | if (ve_flags & VE_ONEMORE) |
| 1022 | { |
| 1023 | ins_at_eol = FALSE; |
| 1024 | nomove = TRUE; |
| 1025 | } |
| 1026 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1027 | count = 0; |
| 1028 | goto doESCkey; |
| 1029 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1030 | case K_INS: /* toggle insert/replace mode */ |
| 1031 | case K_KINS: |
| 1032 | ins_insert(replaceState); |
| 1033 | break; |
| 1034 | |
| 1035 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1036 | break; |
| 1037 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1038 | #ifdef FEAT_SNIFF |
| 1039 | case K_SNIFF: /* Sniff command received */ |
| 1040 | stuffcharReadbuff(K_SNIFF); |
| 1041 | goto doESCkey; |
| 1042 | #endif |
| 1043 | |
| 1044 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 1045 | case K_F1: |
| 1046 | case K_XF1: |
| 1047 | stuffcharReadbuff(K_HELP); |
| 1048 | if (p_im) |
| 1049 | need_start_insertmode = TRUE; |
| 1050 | goto doESCkey; |
| 1051 | |
| 1052 | #ifdef FEAT_NETBEANS_INTG |
| 1053 | case K_F21: /* NetBeans command */ |
| 1054 | ++no_mapping; /* don't map the next key hits */ |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1055 | i = plain_vgetc(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1056 | --no_mapping; |
| 1057 | netbeans_keycommand(i); |
| 1058 | break; |
| 1059 | #endif |
| 1060 | |
| 1061 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1062 | case NUL: |
| 1063 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1064 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 1065 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1066 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 1067 | && c != Ctrl_A && !p_im) |
| 1068 | goto doESCkey; /* quit insert mode */ |
| 1069 | inserted_space = FALSE; |
| 1070 | break; |
| 1071 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1072 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1073 | ins_reg(); |
| 1074 | auto_format(FALSE, TRUE); |
| 1075 | inserted_space = FALSE; |
| 1076 | break; |
| 1077 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1078 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | ins_ctrl_g(); |
| 1080 | break; |
| 1081 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1082 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 1083 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | break; |
| 1085 | |
| 1086 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1087 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | if (!p_ari) |
| 1089 | goto normalchar; |
| 1090 | ins_ctrl_(); |
| 1091 | break; |
| 1092 | #endif |
| 1093 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1094 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1095 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1096 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 1097 | goto docomplete; |
| 1098 | #endif |
| 1099 | /* FALLTHROUGH */ |
| 1100 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1101 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | # ifdef FEAT_INS_EXPAND |
| 1103 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 1104 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1105 | if (has_compl_option(FALSE)) |
| 1106 | goto docomplete; |
| 1107 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1108 | } |
| 1109 | # endif |
| 1110 | ins_shift(c, lastc); |
| 1111 | auto_format(FALSE, TRUE); |
| 1112 | inserted_space = FALSE; |
| 1113 | break; |
| 1114 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1115 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1116 | case K_KDEL: |
| 1117 | ins_del(); |
| 1118 | auto_format(FALSE, TRUE); |
| 1119 | break; |
| 1120 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1121 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1122 | case Ctrl_H: |
| 1123 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 1124 | auto_format(FALSE, TRUE); |
| 1125 | break; |
| 1126 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1127 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1128 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 1129 | auto_format(FALSE, TRUE); |
| 1130 | break; |
| 1131 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1132 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1133 | # ifdef FEAT_COMPL_FUNC |
| 1134 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1135 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1136 | goto docomplete; |
| 1137 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1138 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 1139 | auto_format(FALSE, TRUE); |
| 1140 | inserted_space = FALSE; |
| 1141 | break; |
| 1142 | |
| 1143 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1144 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1145 | case K_LEFTMOUSE_NM: |
| 1146 | case K_LEFTDRAG: |
| 1147 | case K_LEFTRELEASE: |
| 1148 | case K_LEFTRELEASE_NM: |
| 1149 | case K_MIDDLEMOUSE: |
| 1150 | case K_MIDDLEDRAG: |
| 1151 | case K_MIDDLERELEASE: |
| 1152 | case K_RIGHTMOUSE: |
| 1153 | case K_RIGHTDRAG: |
| 1154 | case K_RIGHTRELEASE: |
| 1155 | case K_X1MOUSE: |
| 1156 | case K_X1DRAG: |
| 1157 | case K_X1RELEASE: |
| 1158 | case K_X2MOUSE: |
| 1159 | case K_X2DRAG: |
| 1160 | case K_X2RELEASE: |
| 1161 | ins_mouse(c); |
| 1162 | break; |
| 1163 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1164 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1165 | ins_mousescroll(MSCR_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1166 | break; |
| 1167 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1168 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1169 | ins_mousescroll(MSCR_UP); |
| 1170 | break; |
| 1171 | |
| 1172 | case K_MOUSELEFT: /* Scroll wheel left */ |
| 1173 | ins_mousescroll(MSCR_LEFT); |
| 1174 | break; |
| 1175 | |
| 1176 | case K_MOUSERIGHT: /* Scroll wheel right */ |
| 1177 | ins_mousescroll(MSCR_RIGHT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1178 | break; |
| 1179 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1180 | #ifdef FEAT_GUI_TABLINE |
| 1181 | case K_TABLINE: |
| 1182 | case K_TABMENU: |
| 1183 | ins_tabline(c); |
| 1184 | break; |
| 1185 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1186 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1187 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1188 | break; |
| 1189 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1190 | #ifdef FEAT_AUTOCMD |
| 1191 | case K_CURSORHOLD: /* Didn't type something for a while. */ |
| 1192 | apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); |
| 1193 | did_cursorhold = TRUE; |
| 1194 | break; |
| 1195 | #endif |
| 1196 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1197 | #ifdef FEAT_GUI_W32 |
| 1198 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1199 | * cancelled. */ |
| 1200 | case K_F4: |
| 1201 | if (mod_mask != MOD_MASK_ALT) |
| 1202 | goto normalchar; |
| 1203 | break; |
| 1204 | #endif |
| 1205 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1206 | #ifdef FEAT_GUI |
| 1207 | case K_VER_SCROLLBAR: |
| 1208 | ins_scroll(); |
| 1209 | break; |
| 1210 | |
| 1211 | case K_HOR_SCROLLBAR: |
| 1212 | ins_horscroll(); |
| 1213 | break; |
| 1214 | #endif |
| 1215 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1216 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1217 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | case K_S_HOME: |
| 1219 | case K_C_HOME: |
| 1220 | ins_home(c); |
| 1221 | break; |
| 1222 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1223 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1224 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1225 | case K_S_END: |
| 1226 | case K_C_END: |
| 1227 | ins_end(c); |
| 1228 | break; |
| 1229 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1230 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1231 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1232 | ins_s_left(); |
| 1233 | else |
| 1234 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1237 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1238 | case K_C_LEFT: |
| 1239 | ins_s_left(); |
| 1240 | break; |
| 1241 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1242 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1243 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1244 | ins_s_right(); |
| 1245 | else |
| 1246 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | break; |
| 1248 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1249 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | case K_C_RIGHT: |
| 1251 | ins_s_right(); |
| 1252 | break; |
| 1253 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1254 | case K_UP: /* <Up> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1255 | #ifdef FEAT_INS_EXPAND |
| 1256 | if (pum_visible()) |
| 1257 | goto docomplete; |
| 1258 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1259 | if (mod_mask & MOD_MASK_SHIFT) |
| 1260 | ins_pageup(); |
| 1261 | else |
| 1262 | ins_up(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1265 | case K_S_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1266 | case K_PAGEUP: |
| 1267 | case K_KPAGEUP: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1268 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1269 | if (pum_visible()) |
| 1270 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1271 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1272 | ins_pageup(); |
| 1273 | break; |
| 1274 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1275 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1276 | #ifdef FEAT_INS_EXPAND |
| 1277 | if (pum_visible()) |
| 1278 | goto docomplete; |
| 1279 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1280 | if (mod_mask & MOD_MASK_SHIFT) |
| 1281 | ins_pagedown(); |
| 1282 | else |
| 1283 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1284 | break; |
| 1285 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1286 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1287 | case K_PAGEDOWN: |
| 1288 | case K_KPAGEDOWN: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1289 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1290 | if (pum_visible()) |
| 1291 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1292 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1293 | ins_pagedown(); |
| 1294 | break; |
| 1295 | |
| 1296 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1297 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1298 | ins_drop(); |
| 1299 | break; |
| 1300 | #endif |
| 1301 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1302 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | c = TAB; |
| 1304 | /* FALLTHROUGH */ |
| 1305 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1306 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1307 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1308 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1309 | goto docomplete; |
| 1310 | #endif |
| 1311 | inserted_space = FALSE; |
| 1312 | if (ins_tab()) |
| 1313 | goto normalchar; /* insert TAB as a normal char */ |
| 1314 | auto_format(FALSE, TRUE); |
| 1315 | break; |
| 1316 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1317 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1318 | c = CAR; |
| 1319 | /* FALLTHROUGH */ |
| 1320 | case CAR: |
| 1321 | case NL: |
| 1322 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1323 | /* In a quickfix window a <CR> jumps to the error under the |
| 1324 | * cursor. */ |
| 1325 | if (bt_quickfix(curbuf) && c == CAR) |
| 1326 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1327 | if (curwin->w_llist_ref == NULL) /* quickfix window */ |
| 1328 | do_cmdline_cmd((char_u *)".cc"); |
| 1329 | else /* location list window */ |
| 1330 | do_cmdline_cmd((char_u *)".ll"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | break; |
| 1332 | } |
| 1333 | #endif |
| 1334 | #ifdef FEAT_CMDWIN |
| 1335 | if (cmdwin_type != 0) |
| 1336 | { |
| 1337 | /* Execute the command in the cmdline window. */ |
| 1338 | cmdwin_result = CAR; |
| 1339 | goto doESCkey; |
| 1340 | } |
| 1341 | #endif |
| 1342 | if (ins_eol(c) && !p_im) |
| 1343 | goto doESCkey; /* out of memory */ |
| 1344 | auto_format(FALSE, FALSE); |
| 1345 | inserted_space = FALSE; |
| 1346 | break; |
| 1347 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 1348 | #if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1349 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1350 | # ifdef FEAT_INS_EXPAND |
| 1351 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1352 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1353 | if (has_compl_option(TRUE)) |
| 1354 | goto docomplete; |
| 1355 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1356 | } |
| 1357 | # endif |
| 1358 | # ifdef FEAT_DIGRAPHS |
| 1359 | c = ins_digraph(); |
| 1360 | if (c == NUL) |
| 1361 | break; |
| 1362 | # endif |
| 1363 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1364 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1365 | |
| 1366 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1367 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1368 | ins_ctrl_x(); |
| 1369 | break; |
| 1370 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1371 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1372 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1373 | goto normalchar; |
| 1374 | goto docomplete; |
| 1375 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1376 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1377 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1378 | goto normalchar; |
| 1379 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1380 | |
| 1381 | case 's': /* Spelling completion after ^X */ |
| 1382 | case Ctrl_S: |
| 1383 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1384 | goto normalchar; |
| 1385 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | #endif |
| 1387 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1388 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | #ifdef FEAT_INS_EXPAND |
| 1390 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1391 | #endif |
| 1392 | { |
| 1393 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1394 | if (p_im) |
| 1395 | { |
| 1396 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1397 | break; |
| 1398 | goto doESCkey; |
| 1399 | } |
| 1400 | goto normalchar; |
| 1401 | } |
| 1402 | #ifdef FEAT_INS_EXPAND |
| 1403 | /* FALLTHROUGH */ |
| 1404 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1405 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1406 | case Ctrl_N: |
| 1407 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1408 | * but it is under other ^X modes */ |
| 1409 | if (*curbuf->b_p_cpt == NUL |
| 1410 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1411 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1412 | goto normalchar; |
| 1413 | |
| 1414 | docomplete: |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 1415 | compl_busy = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1416 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1417 | compl_cont_status = 0; |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 1418 | compl_busy = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1419 | break; |
| 1420 | #endif /* FEAT_INS_EXPAND */ |
| 1421 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1422 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1423 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1424 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | |
| 1427 | default: |
| 1428 | #ifdef UNIX |
| 1429 | if (c == intr_char) /* special interrupt char */ |
| 1430 | goto do_intr; |
| 1431 | #endif |
| 1432 | |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1433 | normalchar: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | /* |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 1435 | * Insert a normal character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1436 | */ |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1437 | #ifdef FEAT_AUTOCMD |
| 1438 | if (!p_paste) |
| 1439 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1440 | /* Trigger InsertCharPre. */ |
| 1441 | char_u *str = do_insert_char_pre(c); |
| 1442 | char_u *p; |
| 1443 | |
| 1444 | if (str != NULL) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1445 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1446 | if (*str != NUL && stop_arrow() != FAIL) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1447 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1448 | /* Insert the new value of v:char literally. */ |
| 1449 | for (p = str; *p != NUL; mb_ptr_adv(p)) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1450 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1451 | c = PTR2CHAR(p); |
| 1452 | if (c == CAR || c == K_KENTER || c == NL) |
| 1453 | ins_eol(c); |
| 1454 | else |
| 1455 | ins_char(c); |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1456 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1457 | AppendToRedobuffLit(str, -1); |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1458 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1459 | vim_free(str); |
| 1460 | c = NUL; |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1461 | } |
| 1462 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1463 | /* If the new value is already inserted or an empty string |
| 1464 | * then don't insert any character. */ |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1465 | if (c == NUL) |
| 1466 | break; |
| 1467 | } |
| 1468 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1469 | #ifdef FEAT_SMARTINDENT |
| 1470 | /* Try to perform smart-indenting. */ |
| 1471 | ins_try_si(c); |
| 1472 | #endif |
| 1473 | |
| 1474 | if (c == ' ') |
| 1475 | { |
| 1476 | inserted_space = TRUE; |
| 1477 | #ifdef FEAT_CINDENT |
| 1478 | if (inindent(0)) |
| 1479 | can_cindent = FALSE; |
| 1480 | #endif |
| 1481 | if (Insstart_blank_vcol == MAXCOL |
| 1482 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1483 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1484 | } |
| 1485 | |
Bram Moolenaar | e0ebfd7 | 2012-04-05 16:07:06 +0200 | [diff] [blame] | 1486 | /* Insert a normal character and check for abbreviations on a |
| 1487 | * special character. Let CTRL-] expand abbreviations without |
| 1488 | * inserting it. */ |
| 1489 | if (vim_iswordc(c) || (!echeck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1490 | #ifdef FEAT_MBYTE |
| 1491 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1492 | * what check_abbr() expects. */ |
| 1493 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1494 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 1495 | c) && c != Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1496 | { |
| 1497 | insert_special(c, FALSE, FALSE); |
| 1498 | #ifdef FEAT_RIGHTLEFT |
| 1499 | revins_legal++; |
| 1500 | revins_chars++; |
| 1501 | #endif |
| 1502 | } |
| 1503 | |
| 1504 | auto_format(FALSE, TRUE); |
| 1505 | |
| 1506 | #ifdef FEAT_FOLDING |
| 1507 | /* When inserting a character the cursor line must never be in a |
| 1508 | * closed fold. */ |
| 1509 | foldOpenCursor(); |
| 1510 | #endif |
| 1511 | break; |
| 1512 | } /* end of switch (c) */ |
| 1513 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 1514 | #ifdef FEAT_AUTOCMD |
| 1515 | /* If typed something may trigger CursorHoldI again. */ |
| 1516 | if (c != K_CURSORHOLD) |
| 1517 | did_cursorhold = FALSE; |
| 1518 | #endif |
| 1519 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1520 | /* If the cursor was moved we didn't just insert a space */ |
| 1521 | if (arrow_used) |
| 1522 | inserted_space = FALSE; |
| 1523 | |
| 1524 | #ifdef FEAT_CINDENT |
| 1525 | if (can_cindent && cindent_on() |
| 1526 | # ifdef FEAT_INS_EXPAND |
| 1527 | && ctrl_x_mode == 0 |
| 1528 | # endif |
| 1529 | ) |
| 1530 | { |
| 1531 | force_cindent: |
| 1532 | /* |
| 1533 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1534 | */ |
| 1535 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1536 | { |
| 1537 | if (stop_arrow() == OK) |
| 1538 | /* re-indent the current line */ |
| 1539 | do_c_expr_indent(); |
| 1540 | } |
| 1541 | } |
| 1542 | #endif /* FEAT_CINDENT */ |
| 1543 | |
| 1544 | } /* for (;;) */ |
| 1545 | /* NOTREACHED */ |
| 1546 | } |
| 1547 | |
| 1548 | /* |
| 1549 | * Redraw for Insert mode. |
| 1550 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1551 | * option work correctly. |
| 1552 | * Only redraw when there are no characters available. This speeds up |
| 1553 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1554 | */ |
| 1555 | static void |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1556 | ins_redraw(ready) |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 1557 | int ready UNUSED; /* not busy with something */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1558 | { |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1559 | #ifdef FEAT_CONCEAL |
| 1560 | linenr_T conceal_old_cursor_line = 0; |
| 1561 | linenr_T conceal_new_cursor_line = 0; |
| 1562 | int conceal_update_lines = FALSE; |
| 1563 | #endif |
| 1564 | |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1565 | if (char_avail()) |
| 1566 | return; |
| 1567 | |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1568 | #if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL) |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1569 | /* Trigger CursorMoved if the cursor moved. Not when the popup menu is |
| 1570 | * visible, the command might delete it. */ |
| 1571 | if (ready && ( |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1572 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1573 | has_cursormovedI() |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1574 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1575 | # if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL) |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1576 | || |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1577 | # endif |
| 1578 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1579 | curwin->w_p_cole > 0 |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1580 | # endif |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1581 | ) |
| 1582 | && !equalpos(last_cursormoved, curwin->w_cursor) |
| 1583 | # ifdef FEAT_INS_EXPAND |
| 1584 | && !pum_visible() |
| 1585 | # endif |
| 1586 | ) |
| 1587 | { |
| 1588 | # ifdef FEAT_SYN_HL |
| 1589 | /* Need to update the screen first, to make sure syntax |
| 1590 | * highlighting is correct after making a change (e.g., inserting |
| 1591 | * a "(". The autocommand may also require a redraw, so it's done |
| 1592 | * again below, unfortunately. */ |
| 1593 | if (syntax_present(curwin) && must_redraw) |
| 1594 | update_screen(0); |
| 1595 | # endif |
| 1596 | # ifdef FEAT_AUTOCMD |
| 1597 | if (has_cursormovedI()) |
| 1598 | apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); |
| 1599 | # endif |
| 1600 | # ifdef FEAT_CONCEAL |
| 1601 | if (curwin->w_p_cole > 0) |
| 1602 | { |
| 1603 | conceal_old_cursor_line = last_cursormoved.lnum; |
| 1604 | conceal_new_cursor_line = curwin->w_cursor.lnum; |
| 1605 | conceal_update_lines = TRUE; |
| 1606 | } |
| 1607 | # endif |
| 1608 | last_cursormoved = curwin->w_cursor; |
| 1609 | } |
| 1610 | #endif |
| 1611 | |
| 1612 | #ifdef FEAT_AUTOCMD |
| 1613 | /* Trigger TextChangedI if b_changedtick differs. */ |
| 1614 | if (ready && has_textchangedI() |
| 1615 | && last_changedtick != curbuf->b_changedtick |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1616 | # ifdef FEAT_INS_EXPAND |
| 1617 | && !pum_visible() |
| 1618 | # endif |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1619 | ) |
| 1620 | { |
| 1621 | if (last_changedtick_buf == curbuf) |
| 1622 | apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf); |
| 1623 | last_changedtick_buf = curbuf; |
| 1624 | last_changedtick = curbuf->b_changedtick; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1625 | } |
Bram Moolenaar | e21b6b2 | 2014-01-14 12:17:02 +0100 | [diff] [blame] | 1626 | #endif |
| 1627 | |
| 1628 | if (must_redraw) |
| 1629 | update_screen(0); |
| 1630 | else if (clear_cmdline || redraw_cmdline) |
| 1631 | showmode(); /* clear cmdline and show mode */ |
| 1632 | # if defined(FEAT_CONCEAL) |
| 1633 | if ((conceal_update_lines |
| 1634 | && (conceal_old_cursor_line != conceal_new_cursor_line |
| 1635 | || conceal_cursor_line(curwin))) |
| 1636 | || need_cursor_line_redraw) |
| 1637 | { |
| 1638 | if (conceal_old_cursor_line != conceal_new_cursor_line) |
| 1639 | update_single_line(curwin, conceal_old_cursor_line); |
| 1640 | update_single_line(curwin, conceal_new_cursor_line == 0 |
| 1641 | ? curwin->w_cursor.lnum : conceal_new_cursor_line); |
| 1642 | curwin->w_valid &= ~VALID_CROW; |
| 1643 | } |
| 1644 | # endif |
| 1645 | showruler(FALSE); |
| 1646 | setcursor(); |
| 1647 | emsg_on_display = FALSE; /* may remove error message now */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | /* |
| 1651 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1652 | */ |
| 1653 | static void |
| 1654 | ins_ctrl_v() |
| 1655 | { |
| 1656 | int c; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1657 | int did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1658 | |
| 1659 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1660 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1661 | |
| 1662 | if (redrawing() && !char_avail()) |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1663 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1664 | edit_putchar('^', TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1665 | did_putchar = TRUE; |
| 1666 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1667 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1668 | |
| 1669 | #ifdef FEAT_CMDL_INFO |
| 1670 | add_to_showcmd_c(Ctrl_V); |
| 1671 | #endif |
| 1672 | |
| 1673 | c = get_literal(); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1674 | if (did_putchar) |
| 1675 | /* when the line fits in 'columns' the '^' is at the start of the next |
| 1676 | * line and will not removed by the redraw */ |
| 1677 | edit_unputchar(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1678 | #ifdef FEAT_CMDL_INFO |
| 1679 | clear_showcmd(); |
| 1680 | #endif |
| 1681 | insert_special(c, FALSE, TRUE); |
| 1682 | #ifdef FEAT_RIGHTLEFT |
| 1683 | revins_chars++; |
| 1684 | revins_legal++; |
| 1685 | #endif |
| 1686 | } |
| 1687 | |
| 1688 | /* |
| 1689 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1690 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1691 | */ |
| 1692 | static int pc_status; |
| 1693 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1694 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1695 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1696 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1697 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1698 | static int pc_attr; |
| 1699 | static int pc_row; |
| 1700 | static int pc_col; |
| 1701 | |
| 1702 | void |
| 1703 | edit_putchar(c, highlight) |
| 1704 | int c; |
| 1705 | int highlight; |
| 1706 | { |
| 1707 | int attr; |
| 1708 | |
| 1709 | if (ScreenLines != NULL) |
| 1710 | { |
| 1711 | update_topline(); /* just in case w_topline isn't valid */ |
| 1712 | validate_cursor(); |
| 1713 | if (highlight) |
| 1714 | attr = hl_attr(HLF_8); |
| 1715 | else |
| 1716 | attr = 0; |
| 1717 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1718 | pc_col = W_WINCOL(curwin); |
| 1719 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1720 | pc_status = PC_STATUS_UNSET; |
| 1721 | #endif |
| 1722 | #ifdef FEAT_RIGHTLEFT |
| 1723 | if (curwin->w_p_rl) |
| 1724 | { |
| 1725 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1726 | # ifdef FEAT_MBYTE |
| 1727 | if (has_mbyte) |
| 1728 | { |
| 1729 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1730 | |
| 1731 | if (fix_col != pc_col) |
| 1732 | { |
| 1733 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1734 | --curwin->w_wcol; |
| 1735 | pc_status = PC_STATUS_RIGHT; |
| 1736 | } |
| 1737 | } |
| 1738 | # endif |
| 1739 | } |
| 1740 | else |
| 1741 | #endif |
| 1742 | { |
| 1743 | pc_col += curwin->w_wcol; |
| 1744 | #ifdef FEAT_MBYTE |
| 1745 | if (mb_lefthalve(pc_row, pc_col)) |
| 1746 | pc_status = PC_STATUS_LEFT; |
| 1747 | #endif |
| 1748 | } |
| 1749 | |
| 1750 | /* save the character to be able to put it back */ |
| 1751 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1752 | if (pc_status == PC_STATUS_UNSET) |
| 1753 | #endif |
| 1754 | { |
| 1755 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1756 | pc_status = PC_STATUS_SET; |
| 1757 | } |
| 1758 | screen_putchar(c, pc_row, pc_col, attr); |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | /* |
| 1763 | * Undo the previous edit_putchar(). |
| 1764 | */ |
| 1765 | void |
| 1766 | edit_unputchar() |
| 1767 | { |
| 1768 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1769 | { |
| 1770 | #if defined(FEAT_MBYTE) |
| 1771 | if (pc_status == PC_STATUS_RIGHT) |
| 1772 | ++curwin->w_wcol; |
| 1773 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1774 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1775 | else |
| 1776 | #endif |
| 1777 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1783 | * Only works when cursor is in the line that changes. |
| 1784 | */ |
| 1785 | void |
| 1786 | display_dollar(col) |
| 1787 | colnr_T col; |
| 1788 | { |
| 1789 | colnr_T save_col; |
| 1790 | |
| 1791 | if (!redrawing()) |
| 1792 | return; |
| 1793 | |
| 1794 | cursor_off(); |
| 1795 | save_col = curwin->w_cursor.col; |
| 1796 | curwin->w_cursor.col = col; |
| 1797 | #ifdef FEAT_MBYTE |
| 1798 | if (has_mbyte) |
| 1799 | { |
| 1800 | char_u *p; |
| 1801 | |
| 1802 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1803 | p = ml_get_curline(); |
| 1804 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1805 | } |
| 1806 | #endif |
| 1807 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1808 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1809 | { |
| 1810 | edit_putchar('$', FALSE); |
| 1811 | dollar_vcol = curwin->w_virtcol; |
| 1812 | } |
| 1813 | curwin->w_cursor.col = save_col; |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * Call this function before moving the cursor from the normal insert position |
| 1818 | * in insert mode. |
| 1819 | */ |
| 1820 | static void |
| 1821 | undisplay_dollar() |
| 1822 | { |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 1823 | if (dollar_vcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | { |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 1825 | dollar_vcol = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1826 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1832 | * Keep the cursor on the same character. |
| 1833 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1834 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1835 | * type == INDENT_SET set indent to "amount" |
| 1836 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1837 | */ |
| 1838 | void |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1839 | change_indent(type, amount, round, replaced, call_changed_bytes) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1840 | int type; |
| 1841 | int amount; |
| 1842 | int round; |
| 1843 | int replaced; /* replaced character, put on replace stack */ |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1844 | int call_changed_bytes; /* call changed_bytes() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1845 | { |
| 1846 | int vcol; |
| 1847 | int last_vcol; |
| 1848 | int insstart_less; /* reduction for Insstart.col */ |
| 1849 | int new_cursor_col; |
| 1850 | int i; |
| 1851 | char_u *ptr; |
| 1852 | int save_p_list; |
| 1853 | int start_col; |
| 1854 | colnr_T vc; |
| 1855 | #ifdef FEAT_VREPLACE |
| 1856 | colnr_T orig_col = 0; /* init for GCC */ |
| 1857 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1858 | |
| 1859 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1860 | if (State & VREPLACE_FLAG) |
| 1861 | { |
| 1862 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1863 | orig_col = curwin->w_cursor.col; |
| 1864 | } |
| 1865 | #endif |
| 1866 | |
| 1867 | /* for the following tricks we don't want list mode */ |
| 1868 | save_p_list = curwin->w_p_list; |
| 1869 | curwin->w_p_list = FALSE; |
| 1870 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1871 | vcol = vc; |
| 1872 | |
| 1873 | /* |
| 1874 | * For Replace mode we need to fix the replace stack later, which is only |
| 1875 | * possible when the cursor is in the indent. Remember the number of |
| 1876 | * characters before the cursor if it's possible. |
| 1877 | */ |
| 1878 | start_col = curwin->w_cursor.col; |
| 1879 | |
| 1880 | /* determine offset from first non-blank */ |
| 1881 | new_cursor_col = curwin->w_cursor.col; |
| 1882 | beginline(BL_WHITE); |
| 1883 | new_cursor_col -= curwin->w_cursor.col; |
| 1884 | |
| 1885 | insstart_less = curwin->w_cursor.col; |
| 1886 | |
| 1887 | /* |
| 1888 | * If the cursor is in the indent, compute how many screen columns the |
| 1889 | * cursor is to the left of the first non-blank. |
| 1890 | */ |
| 1891 | if (new_cursor_col < 0) |
| 1892 | vcol = get_indent() - vcol; |
| 1893 | |
| 1894 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1895 | start_col = -1; |
| 1896 | |
| 1897 | /* |
| 1898 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1899 | */ |
| 1900 | if (type == INDENT_SET) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1901 | (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1902 | else |
| 1903 | { |
| 1904 | #ifdef FEAT_VREPLACE |
| 1905 | int save_State = State; |
| 1906 | |
| 1907 | /* Avoid being called recursively. */ |
| 1908 | if (State & VREPLACE_FLAG) |
| 1909 | State = INSERT; |
| 1910 | #endif |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1911 | shift_line(type == INDENT_DEC, round, 1, call_changed_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1912 | #ifdef FEAT_VREPLACE |
| 1913 | State = save_State; |
| 1914 | #endif |
| 1915 | } |
| 1916 | insstart_less -= curwin->w_cursor.col; |
| 1917 | |
| 1918 | /* |
| 1919 | * Try to put cursor on same character. |
| 1920 | * If the cursor is at or after the first non-blank in the line, |
| 1921 | * compute the cursor column relative to the column of the first |
| 1922 | * non-blank character. |
| 1923 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1924 | * If the cursor is before the first non-blank, position it relative |
| 1925 | * to the first non-blank, counted in screen columns. |
| 1926 | */ |
| 1927 | if (new_cursor_col >= 0) |
| 1928 | { |
| 1929 | /* |
| 1930 | * When changing the indent while the cursor is touching it, reset |
| 1931 | * Insstart_col to 0. |
| 1932 | */ |
| 1933 | if (new_cursor_col == 0) |
| 1934 | insstart_less = MAXCOL; |
| 1935 | new_cursor_col += curwin->w_cursor.col; |
| 1936 | } |
| 1937 | else if (!(State & INSERT)) |
| 1938 | new_cursor_col = curwin->w_cursor.col; |
| 1939 | else |
| 1940 | { |
| 1941 | /* |
| 1942 | * Compute the screen column where the cursor should be. |
| 1943 | */ |
| 1944 | vcol = get_indent() - vcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1945 | curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1946 | |
| 1947 | /* |
| 1948 | * Advance the cursor until we reach the right screen column. |
| 1949 | */ |
| 1950 | vcol = last_vcol = 0; |
| 1951 | new_cursor_col = -1; |
| 1952 | ptr = ml_get_curline(); |
| 1953 | while (vcol <= (int)curwin->w_virtcol) |
| 1954 | { |
| 1955 | last_vcol = vcol; |
| 1956 | #ifdef FEAT_MBYTE |
| 1957 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1958 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | else |
| 1960 | #endif |
| 1961 | ++new_cursor_col; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 1962 | vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1963 | } |
| 1964 | vcol = last_vcol; |
| 1965 | |
| 1966 | /* |
| 1967 | * May need to insert spaces to be able to position the cursor on |
| 1968 | * the right screen column. |
| 1969 | */ |
| 1970 | if (vcol != (int)curwin->w_virtcol) |
| 1971 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1972 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1973 | i = (int)curwin->w_virtcol - vcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1974 | ptr = alloc((unsigned)(i + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1975 | if (ptr != NULL) |
| 1976 | { |
| 1977 | new_cursor_col += i; |
| 1978 | ptr[i] = NUL; |
| 1979 | while (--i >= 0) |
| 1980 | ptr[i] = ' '; |
| 1981 | ins_str(ptr); |
| 1982 | vim_free(ptr); |
| 1983 | } |
| 1984 | } |
| 1985 | |
| 1986 | /* |
| 1987 | * When changing the indent while the cursor is in it, reset |
| 1988 | * Insstart_col to 0. |
| 1989 | */ |
| 1990 | insstart_less = MAXCOL; |
| 1991 | } |
| 1992 | |
| 1993 | curwin->w_p_list = save_p_list; |
| 1994 | |
| 1995 | if (new_cursor_col <= 0) |
| 1996 | curwin->w_cursor.col = 0; |
| 1997 | else |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1998 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1999 | curwin->w_set_curswant = TRUE; |
| 2000 | changed_cline_bef_curs(); |
| 2001 | |
| 2002 | /* |
| 2003 | * May have to adjust the start of the insert. |
| 2004 | */ |
| 2005 | if (State & INSERT) |
| 2006 | { |
| 2007 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 2008 | { |
| 2009 | if ((int)Insstart.col <= insstart_less) |
| 2010 | Insstart.col = 0; |
| 2011 | else |
| 2012 | Insstart.col -= insstart_less; |
| 2013 | } |
| 2014 | if ((int)ai_col <= insstart_less) |
| 2015 | ai_col = 0; |
| 2016 | else |
| 2017 | ai_col -= insstart_less; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 2022 | * If the number of characters before the cursor decreased, need to pop a |
| 2023 | * few characters from the replace stack. |
| 2024 | * If the number of characters before the cursor increased, need to push a |
| 2025 | * few NULs onto the replace stack. |
| 2026 | */ |
| 2027 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 2028 | { |
| 2029 | while (start_col > (int)curwin->w_cursor.col) |
| 2030 | { |
| 2031 | replace_join(0); /* remove a NUL from the replace stack */ |
| 2032 | --start_col; |
| 2033 | } |
| 2034 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 2035 | { |
| 2036 | replace_push(NUL); |
| 2037 | if (replaced) |
| 2038 | { |
| 2039 | replace_push(replaced); |
| 2040 | replaced = NUL; |
| 2041 | } |
| 2042 | ++start_col; |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | #ifdef FEAT_VREPLACE |
| 2047 | /* |
| 2048 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 2049 | * it is always possible because we backspace over the whole line and then |
| 2050 | * put it back again the way we wanted it. |
| 2051 | */ |
| 2052 | if (State & VREPLACE_FLAG) |
| 2053 | { |
| 2054 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 2055 | * even if you can't backspace. */ |
| 2056 | if (orig_line == NULL) |
| 2057 | return; |
| 2058 | |
| 2059 | /* Save new line */ |
| 2060 | new_line = vim_strsave(ml_get_curline()); |
| 2061 | if (new_line == NULL) |
| 2062 | return; |
| 2063 | |
| 2064 | /* We only put back the new line up to the cursor */ |
| 2065 | new_line[curwin->w_cursor.col] = NUL; |
| 2066 | |
| 2067 | /* Put back original line */ |
| 2068 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 2069 | curwin->w_cursor.col = orig_col; |
| 2070 | |
| 2071 | /* Backspace from cursor to start of line */ |
| 2072 | backspace_until_column(0); |
| 2073 | |
| 2074 | /* Insert new stuff into line again */ |
| 2075 | ins_bytes(new_line); |
| 2076 | |
| 2077 | vim_free(new_line); |
| 2078 | } |
| 2079 | #endif |
| 2080 | } |
| 2081 | |
| 2082 | /* |
| 2083 | * Truncate the space at the end of a line. This is to be used only in an |
| 2084 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 2085 | * modes. |
| 2086 | */ |
| 2087 | void |
| 2088 | truncate_spaces(line) |
| 2089 | char_u *line; |
| 2090 | { |
| 2091 | int i; |
| 2092 | |
| 2093 | /* find start of trailing white space */ |
| 2094 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 2095 | { |
| 2096 | if (State & REPLACE_FLAG) |
| 2097 | replace_join(0); /* remove a NUL from the replace stack */ |
| 2098 | } |
| 2099 | line[i + 1] = NUL; |
| 2100 | } |
| 2101 | |
| 2102 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 2103 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 2104 | /* |
| 2105 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 2106 | * 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] | 2107 | * Will attempt not to go before "col" even when there is a composing |
| 2108 | * character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2109 | */ |
| 2110 | void |
| 2111 | backspace_until_column(col) |
| 2112 | int col; |
| 2113 | { |
| 2114 | while ((int)curwin->w_cursor.col > col) |
| 2115 | { |
| 2116 | curwin->w_cursor.col--; |
| 2117 | if (State & REPLACE_FLAG) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2118 | replace_do_bs(col); |
| 2119 | else if (!del_char_after_col(col)) |
| 2120 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | #endif |
| 2124 | |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2125 | /* |
| 2126 | * Like del_char(), but make sure not to go before column "limit_col". |
| 2127 | * Only matters when there are composing characters. |
| 2128 | * Return TRUE when something was deleted. |
| 2129 | */ |
| 2130 | static int |
| 2131 | del_char_after_col(limit_col) |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 2132 | int limit_col UNUSED; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2133 | { |
| 2134 | #ifdef FEAT_MBYTE |
| 2135 | if (enc_utf8 && limit_col >= 0) |
| 2136 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2137 | colnr_T ecol = curwin->w_cursor.col + 1; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2138 | |
| 2139 | /* Make sure the cursor is at the start of a character, but |
| 2140 | * skip forward again when going too far back because of a |
| 2141 | * composing character. */ |
| 2142 | mb_adjust_cursor(); |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 2143 | while (curwin->w_cursor.col < (colnr_T)limit_col) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2144 | { |
| 2145 | int l = utf_ptr2len(ml_get_cursor()); |
| 2146 | |
| 2147 | if (l == 0) /* end of line */ |
| 2148 | break; |
| 2149 | curwin->w_cursor.col += l; |
| 2150 | } |
| 2151 | if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol) |
| 2152 | return FALSE; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2153 | del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2154 | } |
| 2155 | else |
| 2156 | #endif |
| 2157 | (void)del_char(FALSE); |
| 2158 | return TRUE; |
| 2159 | } |
| 2160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2161 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 2162 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2163 | * CTRL-X pressed in Insert mode. |
| 2164 | */ |
| 2165 | static void |
| 2166 | ins_ctrl_x() |
| 2167 | { |
| 2168 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 2169 | * CTRL-V works like CTRL-N */ |
| 2170 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 2171 | { |
| 2172 | /* if the next ^X<> won't ADD nothing, then reset |
| 2173 | * compl_cont_status */ |
| 2174 | if (compl_cont_status & CONT_N_ADDS) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2175 | compl_cont_status |= CONT_INTRPT; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2176 | else |
| 2177 | compl_cont_status = 0; |
| 2178 | /* We're not sure which CTRL-X mode it will be yet */ |
| 2179 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2180 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 2181 | edit_submode_pre = NULL; |
| 2182 | showmode(); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | /* |
| 2187 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 2188 | */ |
| 2189 | static int |
| 2190 | has_compl_option(dict_opt) |
| 2191 | int dict_opt; |
| 2192 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2193 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2194 | # ifdef FEAT_SPELL |
| 2195 | && !curwin->w_p_spell |
| 2196 | # endif |
| 2197 | ) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2198 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 2199 | { |
| 2200 | ctrl_x_mode = 0; |
| 2201 | edit_submode = NULL; |
| 2202 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 2203 | : (char_u *)_("'thesaurus' option is empty"), |
| 2204 | hl_attr(HLF_E)); |
| 2205 | if (emsg_silent == 0) |
| 2206 | { |
| 2207 | vim_beep(); |
| 2208 | setcursor(); |
| 2209 | out_flush(); |
| 2210 | ui_delay(2000L, FALSE); |
| 2211 | } |
| 2212 | return FALSE; |
| 2213 | } |
| 2214 | return TRUE; |
| 2215 | } |
| 2216 | |
| 2217 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2218 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 2219 | * This depends on the current mode. |
| 2220 | */ |
| 2221 | int |
| 2222 | vim_is_ctrl_x_key(c) |
| 2223 | int c; |
| 2224 | { |
| 2225 | /* Always allow ^R - let it's results then be checked */ |
| 2226 | if (c == Ctrl_R) |
| 2227 | return TRUE; |
| 2228 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2229 | /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 2230 | if (ins_compl_pum_key(c)) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2231 | return TRUE; |
| 2232 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2233 | switch (ctrl_x_mode) |
| 2234 | { |
| 2235 | case 0: /* Not in any CTRL-X mode */ |
| 2236 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 2237 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2238 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2239 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 2240 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 2241 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2242 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
Bram Moolenaar | bbd9fd7 | 2011-12-23 13:15:03 +0100 | [diff] [blame] | 2243 | || c == Ctrl_S || c == Ctrl_K || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | case CTRL_X_SCROLL: |
| 2245 | return (c == Ctrl_Y || c == Ctrl_E); |
| 2246 | case CTRL_X_WHOLE_LINE: |
| 2247 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 2248 | case CTRL_X_FILES: |
| 2249 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 2250 | case CTRL_X_DICTIONARY: |
| 2251 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 2252 | case CTRL_X_THESAURUS: |
| 2253 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 2254 | case CTRL_X_TAGS: |
| 2255 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 2256 | #ifdef FEAT_FIND_ID |
| 2257 | case CTRL_X_PATH_PATTERNS: |
| 2258 | return (c == Ctrl_P || c == Ctrl_N); |
| 2259 | case CTRL_X_PATH_DEFINES: |
| 2260 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 2261 | #endif |
| 2262 | case CTRL_X_CMDLINE: |
| 2263 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 2264 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2265 | #ifdef FEAT_COMPL_FUNC |
| 2266 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2267 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2268 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2269 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2270 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2271 | case CTRL_X_SPELL: |
| 2272 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 2273 | case CTRL_X_EVAL: |
| 2274 | return (c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2275 | } |
| 2276 | EMSG(_(e_internal)); |
| 2277 | return FALSE; |
| 2278 | } |
| 2279 | |
| 2280 | /* |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 2281 | * Return TRUE when character "c" is part of the item currently being |
| 2282 | * completed. Used to decide whether to abandon complete mode when the menu |
| 2283 | * is visible. |
| 2284 | */ |
| 2285 | static int |
| 2286 | ins_compl_accept_char(c) |
| 2287 | int c; |
| 2288 | { |
| 2289 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 2290 | /* When expanding an identifier only accept identifier chars. */ |
| 2291 | return vim_isIDc(c); |
| 2292 | |
| 2293 | switch (ctrl_x_mode) |
| 2294 | { |
| 2295 | case CTRL_X_FILES: |
| 2296 | /* When expanding file name only accept file name chars. But not |
| 2297 | * path separators, so that "proto/<Tab>" expands files in |
| 2298 | * "proto", not "proto/" as a whole */ |
| 2299 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 2300 | |
| 2301 | case CTRL_X_CMDLINE: |
| 2302 | case CTRL_X_OMNI: |
| 2303 | /* Command line and Omni completion can work with just about any |
| 2304 | * printable character, but do stop at white space. */ |
| 2305 | return vim_isprintc(c) && !vim_iswhite(c); |
| 2306 | |
| 2307 | case CTRL_X_WHOLE_LINE: |
| 2308 | /* For while line completion a space can be part of the line. */ |
| 2309 | return vim_isprintc(c); |
| 2310 | } |
| 2311 | return vim_iswordc(c); |
| 2312 | } |
| 2313 | |
| 2314 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2315 | * 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] | 2316 | * 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] | 2317 | * 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] | 2318 | * the rest of the word to be in -- webb |
| 2319 | */ |
| 2320 | int |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2321 | ins_compl_add_infercase(str, len, icase, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2322 | char_u *str; |
| 2323 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2324 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2325 | char_u *fname; |
| 2326 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2327 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2328 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2329 | char_u *p; |
| 2330 | int i, c; |
| 2331 | int actual_len; /* Take multi-byte characters */ |
| 2332 | int actual_compl_length; /* into account. */ |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2333 | int min_len; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 2334 | int *wca; /* Wide character array. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | int has_lower = FALSE; |
| 2336 | int was_letter = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | |
Bram Moolenaar | e40e57c | 2007-11-08 12:04:26 +0000 | [diff] [blame] | 2338 | if (p_ic && curbuf->b_p_inf && len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2340 | /* Infer case of completed part. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2341 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2342 | /* Find actual length of completion. */ |
| 2343 | #ifdef FEAT_MBYTE |
| 2344 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2345 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2346 | p = str; |
| 2347 | actual_len = 0; |
| 2348 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2350 | mb_ptr_adv(p); |
| 2351 | ++actual_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2352 | } |
| 2353 | } |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2354 | else |
| 2355 | #endif |
| 2356 | actual_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2358 | /* Find actual length of original text. */ |
| 2359 | #ifdef FEAT_MBYTE |
| 2360 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2361 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2362 | p = compl_orig_text; |
| 2363 | actual_compl_length = 0; |
| 2364 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2366 | mb_ptr_adv(p); |
| 2367 | ++actual_compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2368 | } |
| 2369 | } |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2370 | else |
| 2371 | #endif |
| 2372 | actual_compl_length = compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2373 | |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2374 | /* "actual_len" may be smaller than "actual_compl_length" when using |
| 2375 | * thesaurus, only use the minimum when comparing. */ |
| 2376 | min_len = actual_len < actual_compl_length |
| 2377 | ? actual_len : actual_compl_length; |
| 2378 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2379 | /* Allocate wide character array for the completion and fill it. */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2380 | wca = (int *)alloc((unsigned)(actual_len * sizeof(int))); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2381 | if (wca != NULL) |
| 2382 | { |
| 2383 | p = str; |
| 2384 | for (i = 0; i < actual_len; ++i) |
| 2385 | #ifdef FEAT_MBYTE |
| 2386 | if (has_mbyte) |
| 2387 | wca[i] = mb_ptr2char_adv(&p); |
| 2388 | else |
| 2389 | #endif |
| 2390 | wca[i] = *(p++); |
| 2391 | |
| 2392 | /* Rule 1: Were any chars converted to lower? */ |
| 2393 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2394 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2395 | { |
| 2396 | #ifdef FEAT_MBYTE |
| 2397 | if (has_mbyte) |
| 2398 | c = mb_ptr2char_adv(&p); |
| 2399 | else |
| 2400 | #endif |
| 2401 | c = *(p++); |
| 2402 | if (MB_ISLOWER(c)) |
| 2403 | { |
| 2404 | has_lower = TRUE; |
| 2405 | if (MB_ISUPPER(wca[i])) |
| 2406 | { |
| 2407 | /* Rule 1 is satisfied. */ |
| 2408 | for (i = actual_compl_length; i < actual_len; ++i) |
| 2409 | wca[i] = MB_TOLOWER(wca[i]); |
| 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | /* |
| 2416 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 2417 | * upper case. |
| 2418 | */ |
| 2419 | if (!has_lower) |
| 2420 | { |
| 2421 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2422 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2423 | { |
| 2424 | #ifdef FEAT_MBYTE |
| 2425 | if (has_mbyte) |
| 2426 | c = mb_ptr2char_adv(&p); |
| 2427 | else |
| 2428 | #endif |
| 2429 | c = *(p++); |
| 2430 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 2431 | { |
| 2432 | /* Rule 2 is satisfied. */ |
| 2433 | for (i = actual_compl_length; i < actual_len; ++i) |
| 2434 | wca[i] = MB_TOUPPER(wca[i]); |
| 2435 | break; |
| 2436 | } |
| 2437 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | /* Copy the original case of the part we typed. */ |
| 2442 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2443 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2444 | { |
| 2445 | #ifdef FEAT_MBYTE |
| 2446 | if (has_mbyte) |
| 2447 | c = mb_ptr2char_adv(&p); |
| 2448 | else |
| 2449 | #endif |
| 2450 | c = *(p++); |
| 2451 | if (MB_ISLOWER(c)) |
| 2452 | wca[i] = MB_TOLOWER(wca[i]); |
| 2453 | else if (MB_ISUPPER(c)) |
| 2454 | wca[i] = MB_TOUPPER(wca[i]); |
| 2455 | } |
| 2456 | |
Bram Moolenaar | e40e57c | 2007-11-08 12:04:26 +0000 | [diff] [blame] | 2457 | /* |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2458 | * Generate encoding specific output from wide character array. |
| 2459 | * Multi-byte characters can occupy up to five bytes more than |
| 2460 | * ASCII characters, and we also need one byte for NUL, so stay |
| 2461 | * six bytes away from the edge of IObuff. |
| 2462 | */ |
| 2463 | p = IObuff; |
| 2464 | i = 0; |
| 2465 | while (i < actual_len && (p - IObuff + 6) < IOSIZE) |
| 2466 | #ifdef FEAT_MBYTE |
| 2467 | if (has_mbyte) |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 2468 | p += (*mb_char2bytes)(wca[i++], p); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2469 | else |
| 2470 | #endif |
| 2471 | *(p++) = wca[i++]; |
| 2472 | *p = NUL; |
| 2473 | |
| 2474 | vim_free(wca); |
| 2475 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2476 | |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2477 | return ins_compl_add(IObuff, len, icase, fname, NULL, dir, |
| 2478 | flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2479 | } |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2480 | return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /* |
| 2484 | * Add a match to the list of matches. |
| 2485 | * 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] | 2486 | * 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] | 2487 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2488 | */ |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2489 | static int |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2490 | ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | char_u *str; |
| 2492 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2493 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | char_u *fname; |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2495 | char_u **cptext; /* extra text for popup menu or NULL */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2496 | int cdir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2497 | int flags; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2498 | int adup; /* accept duplicate match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2500 | compl_T *match; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2501 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | |
| 2503 | ui_breakcheck(); |
| 2504 | if (got_int) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2505 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2506 | if (len < 0) |
| 2507 | len = (int)STRLEN(str); |
| 2508 | |
| 2509 | /* |
| 2510 | * If the same match is already present, don't add it. |
| 2511 | */ |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2512 | if (compl_first_match != NULL && !adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2513 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2514 | match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2515 | do |
| 2516 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2517 | if ( !(match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 5948a57 | 2006-10-03 13:49:29 +0000 | [diff] [blame] | 2518 | && STRNCMP(match->cp_str, str, len) == 0 |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2519 | && match->cp_str[len] == NUL) |
| 2520 | return NOTDONE; |
| 2521 | match = match->cp_next; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2522 | } while (match != NULL && match != compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2525 | /* Remove any popup menu before changing the list of matches. */ |
| 2526 | ins_compl_del_pum(); |
| 2527 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | /* |
| 2529 | * Allocate a new match structure. |
| 2530 | * Copy the values to the new match structure. |
| 2531 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2532 | match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2533 | if (match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2534 | return FAIL; |
| 2535 | match->cp_number = -1; |
| 2536 | if (flags & ORIGINAL_TEXT) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2537 | match->cp_number = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2538 | if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | { |
| 2540 | vim_free(match); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2541 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2542 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2543 | match->cp_icase = icase; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2544 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2545 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2546 | * - 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] | 2547 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 2548 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2549 | if (fname != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2550 | && compl_curr_match != NULL |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2551 | && compl_curr_match->cp_fname != NULL |
| 2552 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2553 | match->cp_fname = compl_curr_match->cp_fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2554 | else if (fname != NULL) |
| 2555 | { |
| 2556 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2557 | flags |= FREE_FNAME; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2558 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2559 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2560 | match->cp_fname = NULL; |
| 2561 | match->cp_flags = flags; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2562 | |
| 2563 | if (cptext != NULL) |
| 2564 | { |
| 2565 | int i; |
| 2566 | |
| 2567 | for (i = 0; i < CPT_COUNT; ++i) |
| 2568 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 2569 | match->cp_text[i] = vim_strsave(cptext[i]); |
| 2570 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2571 | |
| 2572 | /* |
| 2573 | * Link the new match structure in the list of matches. |
| 2574 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2575 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2576 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2577 | else if (dir == FORWARD) |
| 2578 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2579 | match->cp_next = compl_curr_match->cp_next; |
| 2580 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2581 | } |
| 2582 | else /* BACKWARD */ |
| 2583 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2584 | match->cp_next = compl_curr_match; |
| 2585 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2586 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2587 | if (match->cp_next) |
| 2588 | match->cp_next->cp_prev = match; |
| 2589 | if (match->cp_prev) |
| 2590 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2591 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2592 | compl_first_match = match; |
| 2593 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2595 | /* |
| 2596 | * Find the longest common string if still doing that. |
| 2597 | */ |
| 2598 | if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) |
| 2599 | ins_compl_longest_match(match); |
| 2600 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2601 | return OK; |
| 2602 | } |
| 2603 | |
| 2604 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2605 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
| 2606 | * match->cp_icase. |
| 2607 | */ |
| 2608 | static int |
| 2609 | ins_compl_equal(match, str, len) |
| 2610 | compl_T *match; |
| 2611 | char_u *str; |
| 2612 | int len; |
| 2613 | { |
| 2614 | if (match->cp_icase) |
| 2615 | return STRNICMP(match->cp_str, str, (size_t)len) == 0; |
| 2616 | return STRNCMP(match->cp_str, str, (size_t)len) == 0; |
| 2617 | } |
| 2618 | |
| 2619 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2620 | * Reduce the longest common string for match "match". |
| 2621 | */ |
| 2622 | static void |
| 2623 | ins_compl_longest_match(match) |
| 2624 | compl_T *match; |
| 2625 | { |
| 2626 | char_u *p, *s; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2627 | int c1, c2; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2628 | int had_match; |
| 2629 | |
| 2630 | if (compl_leader == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2631 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2632 | /* First match, use it as a whole. */ |
| 2633 | compl_leader = vim_strsave(match->cp_str); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2634 | if (compl_leader != NULL) |
| 2635 | { |
| 2636 | had_match = (curwin->w_cursor.col > compl_col); |
| 2637 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2638 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2639 | ins_redraw(FALSE); |
| 2640 | |
| 2641 | /* When the match isn't there (to avoid matching itself) remove it |
| 2642 | * again after redrawing. */ |
| 2643 | if (!had_match) |
| 2644 | ins_compl_delete(); |
| 2645 | compl_used_match = FALSE; |
| 2646 | } |
| 2647 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2648 | else |
| 2649 | { |
| 2650 | /* Reduce the text if this match differs from compl_leader. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2651 | p = compl_leader; |
| 2652 | s = match->cp_str; |
| 2653 | while (*p != NUL) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2654 | { |
| 2655 | #ifdef FEAT_MBYTE |
| 2656 | if (has_mbyte) |
| 2657 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2658 | c1 = mb_ptr2char(p); |
| 2659 | c2 = mb_ptr2char(s); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2660 | } |
| 2661 | else |
| 2662 | #endif |
| 2663 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2664 | c1 = *p; |
| 2665 | c2 = *s; |
| 2666 | } |
| 2667 | if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) |
| 2668 | : (c1 != c2)) |
| 2669 | break; |
| 2670 | #ifdef FEAT_MBYTE |
| 2671 | if (has_mbyte) |
| 2672 | { |
| 2673 | mb_ptr_adv(p); |
| 2674 | mb_ptr_adv(s); |
| 2675 | } |
| 2676 | else |
| 2677 | #endif |
| 2678 | { |
| 2679 | ++p; |
| 2680 | ++s; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | if (*p != NUL) |
| 2685 | { |
| 2686 | /* Leader was shortened, need to change the inserted text. */ |
| 2687 | *p = NUL; |
| 2688 | had_match = (curwin->w_cursor.col > compl_col); |
| 2689 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2690 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2691 | ins_redraw(FALSE); |
| 2692 | |
| 2693 | /* When the match isn't there (to avoid matching itself) remove it |
| 2694 | * again after redrawing. */ |
| 2695 | if (!had_match) |
| 2696 | ins_compl_delete(); |
| 2697 | } |
| 2698 | |
| 2699 | compl_used_match = FALSE; |
| 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2704 | * Add an array of matches to the list of matches. |
| 2705 | * Frees matches[]. |
| 2706 | */ |
| 2707 | static void |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2708 | ins_compl_add_matches(num_matches, matches, icase) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | int num_matches; |
| 2710 | char_u **matches; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2711 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2712 | { |
| 2713 | int i; |
| 2714 | int add_r = OK; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2715 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2716 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2717 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2718 | if ((add_r = ins_compl_add(matches[i], -1, icase, |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2719 | NULL, NULL, dir, 0, FALSE)) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2720 | /* if dir was BACKWARD then honor it just once */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2721 | dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2722 | FreeWild(num_matches, matches); |
| 2723 | } |
| 2724 | |
| 2725 | /* Make the completion list cyclic. |
| 2726 | * Return the number of matches (excluding the original). |
| 2727 | */ |
| 2728 | static int |
| 2729 | ins_compl_make_cyclic() |
| 2730 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2731 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2732 | int count = 0; |
| 2733 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2734 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | { |
| 2736 | /* |
| 2737 | * Find the end of the list. |
| 2738 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2739 | match = compl_first_match; |
| 2740 | /* 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] | 2741 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2743 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2744 | ++count; |
| 2745 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2746 | match->cp_next = compl_first_match; |
| 2747 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | } |
| 2749 | return count; |
| 2750 | } |
| 2751 | |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2752 | /* |
| 2753 | * Start completion for the complete() function. |
| 2754 | * "startcol" is where the matched text starts (1 is first column). |
| 2755 | * "list" is the list of matches. |
| 2756 | */ |
| 2757 | void |
| 2758 | set_completion(startcol, list) |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2759 | colnr_T startcol; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2760 | list_T *list; |
| 2761 | { |
| 2762 | /* If already doing completions stop it. */ |
| 2763 | if (ctrl_x_mode != 0) |
| 2764 | ins_compl_prep(' '); |
| 2765 | ins_compl_clear(); |
| 2766 | |
| 2767 | if (stop_arrow() == FAIL) |
| 2768 | return; |
| 2769 | |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 2770 | compl_direction = FORWARD; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2771 | if (startcol > curwin->w_cursor.col) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2772 | startcol = curwin->w_cursor.col; |
| 2773 | compl_col = startcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2774 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2775 | /* compl_pattern doesn't need to be set */ |
| 2776 | compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); |
| 2777 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 2778 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2779 | return; |
| 2780 | |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 2781 | ctrl_x_mode = CTRL_X_EVAL; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2782 | |
| 2783 | ins_compl_add_list(list); |
| 2784 | compl_matches = ins_compl_make_cyclic(); |
| 2785 | compl_started = TRUE; |
| 2786 | compl_used_match = TRUE; |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 2787 | compl_cont_status = 0; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2788 | |
| 2789 | compl_curr_match = compl_first_match; |
| 2790 | ins_complete(Ctrl_N); |
| 2791 | out_flush(); |
| 2792 | } |
| 2793 | |
| 2794 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2795 | /* "compl_match_array" points the currently displayed list of entries in the |
| 2796 | * popup menu. It is NULL when there is no popup menu. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2797 | static pumitem_T *compl_match_array = NULL; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2798 | static int compl_match_arraysize; |
| 2799 | |
| 2800 | /* |
| 2801 | * Update the screen and when there is any scrolling remove the popup menu. |
| 2802 | */ |
| 2803 | static void |
| 2804 | ins_compl_upd_pum() |
| 2805 | { |
| 2806 | int h; |
| 2807 | |
| 2808 | if (compl_match_array != NULL) |
| 2809 | { |
| 2810 | h = curwin->w_cline_height; |
| 2811 | update_screen(0); |
| 2812 | if (h != curwin->w_cline_height) |
| 2813 | ins_compl_del_pum(); |
| 2814 | } |
| 2815 | } |
| 2816 | |
| 2817 | /* |
| 2818 | * Remove any popup menu. |
| 2819 | */ |
| 2820 | static void |
| 2821 | ins_compl_del_pum() |
| 2822 | { |
| 2823 | if (compl_match_array != NULL) |
| 2824 | { |
| 2825 | pum_undisplay(); |
| 2826 | vim_free(compl_match_array); |
| 2827 | compl_match_array = NULL; |
| 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * Return TRUE if the popup menu should be displayed. |
| 2833 | */ |
| 2834 | static int |
| 2835 | pum_wanted() |
| 2836 | { |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2837 | /* 'completeopt' must contain "menu" or "menuone" */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2838 | if (vim_strchr(p_cot, 'm') == NULL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2839 | return FALSE; |
| 2840 | |
| 2841 | /* The display looks bad on a B&W display. */ |
| 2842 | if (t_colors < 8 |
| 2843 | #ifdef FEAT_GUI |
| 2844 | && !gui.in_use |
| 2845 | #endif |
| 2846 | ) |
| 2847 | return FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2848 | return TRUE; |
| 2849 | } |
| 2850 | |
| 2851 | /* |
| 2852 | * 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] | 2853 | * One if 'completopt' contains "menuone". |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2854 | */ |
| 2855 | static int |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2856 | pum_enough_matches() |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2857 | { |
| 2858 | compl_T *compl; |
| 2859 | int i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2860 | |
| 2861 | /* Don't display the popup menu if there are no matches or there is only |
| 2862 | * one (ignoring the original text). */ |
| 2863 | compl = compl_first_match; |
| 2864 | i = 0; |
| 2865 | do |
| 2866 | { |
| 2867 | if (compl == NULL |
| 2868 | || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) |
| 2869 | break; |
| 2870 | compl = compl->cp_next; |
| 2871 | } while (compl != compl_first_match); |
| 2872 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2873 | if (strstr((char *)p_cot, "menuone") != NULL) |
| 2874 | return (i >= 1); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2875 | return (i >= 2); |
| 2876 | } |
| 2877 | |
| 2878 | /* |
| 2879 | * Show the popup menu for the list of matches. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2880 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2881 | */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2882 | void |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2883 | ins_compl_show_pum() |
| 2884 | { |
| 2885 | compl_T *compl; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2886 | compl_T *shown_compl = NULL; |
| 2887 | int did_find_shown_match = FALSE; |
| 2888 | int shown_match_ok = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2889 | int i; |
| 2890 | int cur = -1; |
| 2891 | colnr_T col; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2892 | int lead_len = 0; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2893 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2894 | if (!pum_wanted() || !pum_enough_matches()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2897 | #if defined(FEAT_EVAL) |
| 2898 | /* Dirty hard-coded hack: remove any matchparen highlighting. */ |
| 2899 | do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif"); |
| 2900 | #endif |
| 2901 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2902 | /* Update the screen before drawing the popup menu over it. */ |
| 2903 | update_screen(0); |
| 2904 | |
| 2905 | if (compl_match_array == NULL) |
| 2906 | { |
| 2907 | /* Need to build the popup menu list. */ |
| 2908 | compl_match_arraysize = 0; |
| 2909 | compl = compl_first_match; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2910 | if (compl_leader != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2911 | lead_len = (int)STRLEN(compl_leader); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2912 | do |
| 2913 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2914 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2915 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2916 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2917 | ++compl_match_arraysize; |
| 2918 | compl = compl->cp_next; |
| 2919 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2920 | if (compl_match_arraysize == 0) |
| 2921 | return; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2922 | compl_match_array = (pumitem_T *)alloc_clear( |
| 2923 | (unsigned)(sizeof(pumitem_T) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2924 | * compl_match_arraysize)); |
| 2925 | if (compl_match_array != NULL) |
| 2926 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2927 | /* If the current match is the original text don't find the first |
| 2928 | * match after it, don't highlight anything. */ |
| 2929 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 2930 | shown_match_ok = TRUE; |
| 2931 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2932 | i = 0; |
| 2933 | compl = compl_first_match; |
| 2934 | do |
| 2935 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2936 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2937 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2938 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2939 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2940 | if (!shown_match_ok) |
| 2941 | { |
| 2942 | if (compl == compl_shown_match || did_find_shown_match) |
| 2943 | { |
| 2944 | /* This item is the shown match or this is the |
| 2945 | * first displayed item after the shown match. */ |
| 2946 | compl_shown_match = compl; |
| 2947 | did_find_shown_match = TRUE; |
| 2948 | shown_match_ok = TRUE; |
| 2949 | } |
| 2950 | else |
| 2951 | /* Remember this displayed match for when the |
| 2952 | * shown match is just below it. */ |
| 2953 | shown_compl = compl; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2954 | cur = i; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2955 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2956 | |
| 2957 | if (compl->cp_text[CPT_ABBR] != NULL) |
| 2958 | compl_match_array[i].pum_text = |
| 2959 | compl->cp_text[CPT_ABBR]; |
| 2960 | else |
| 2961 | compl_match_array[i].pum_text = compl->cp_str; |
| 2962 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 2963 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 2964 | if (compl->cp_text[CPT_MENU] != NULL) |
| 2965 | compl_match_array[i++].pum_extra = |
| 2966 | compl->cp_text[CPT_MENU]; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2967 | else |
| 2968 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 2969 | } |
| 2970 | |
| 2971 | if (compl == compl_shown_match) |
| 2972 | { |
| 2973 | did_find_shown_match = TRUE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 2974 | |
| 2975 | /* When the original text is the shown match don't set |
| 2976 | * compl_shown_match. */ |
| 2977 | if (compl->cp_flags & ORIGINAL_TEXT) |
| 2978 | shown_match_ok = TRUE; |
| 2979 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2980 | if (!shown_match_ok && shown_compl != NULL) |
| 2981 | { |
| 2982 | /* The shown match isn't displayed, set it to the |
| 2983 | * previously displayed match. */ |
| 2984 | compl_shown_match = shown_compl; |
| 2985 | shown_match_ok = TRUE; |
| 2986 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2987 | } |
| 2988 | compl = compl->cp_next; |
| 2989 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2990 | |
| 2991 | if (!shown_match_ok) /* no displayed match at all */ |
| 2992 | cur = -1; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2993 | } |
| 2994 | } |
| 2995 | else |
| 2996 | { |
| 2997 | /* popup menu already exists, only need to find the current item.*/ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2998 | for (i = 0; i < compl_match_arraysize; ++i) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2999 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str |
| 3000 | || compl_match_array[i].pum_text |
| 3001 | == compl_shown_match->cp_text[CPT_ABBR]) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 3002 | { |
| 3003 | cur = i; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3004 | break; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 3005 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | if (compl_match_array != NULL) |
| 3009 | { |
| 3010 | /* Compute the screen column of the start of the completed text. |
| 3011 | * Use the cursor to get all wrapping and other settings right. */ |
| 3012 | col = curwin->w_cursor.col; |
| 3013 | curwin->w_cursor.col = compl_col; |
Bram Moolenaar | d289f13 | 2006-03-11 21:30:53 +0000 | [diff] [blame] | 3014 | pum_display(compl_match_array, compl_match_arraysize, cur); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3015 | curwin->w_cursor.col = col; |
| 3016 | } |
| 3017 | } |
| 3018 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3019 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 3020 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 3021 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3022 | /* |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3023 | * Add any identifiers that match the given pattern in the list of dictionary |
| 3024 | * files "dict_start" to the list of completions. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3025 | */ |
| 3026 | static void |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3027 | ins_compl_dictionaries(dict_start, pat, flags, thesaurus) |
| 3028 | char_u *dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3029 | char_u *pat; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3030 | int flags; /* DICT_FIRST and/or DICT_EXACT */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3031 | int thesaurus; /* Thesaurus completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3032 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3033 | char_u *dict = dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3034 | char_u *ptr; |
| 3035 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3036 | regmatch_T regmatch; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3037 | char_u **files; |
| 3038 | int count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3039 | int save_p_scs; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3040 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3041 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3042 | if (*dict == NUL) |
| 3043 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3044 | #ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3045 | /* When 'dictionary' is empty and spell checking is enabled use |
| 3046 | * "spell". */ |
| 3047 | if (!thesaurus && curwin->w_p_spell) |
| 3048 | dict = (char_u *)"spell"; |
| 3049 | else |
| 3050 | #endif |
| 3051 | return; |
| 3052 | } |
| 3053 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3054 | buf = alloc(LSIZE); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3055 | if (buf == NULL) |
| 3056 | return; |
Bram Moolenaar | fa3491a | 2007-02-20 02:49:19 +0000 | [diff] [blame] | 3057 | regmatch.regprog = NULL; /* so that we can goto theend */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3058 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3059 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 3060 | save_p_scs = p_scs; |
| 3061 | if (curbuf->b_p_inf) |
| 3062 | p_scs = FALSE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3063 | |
| 3064 | /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 3065 | * 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] | 3066 | * pattern. Also need to double backslashes. */ |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 3067 | if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3068 | { |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3069 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3070 | size_t len; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3071 | |
| 3072 | if (pat_esc == NULL) |
Bram Moolenaar | 6519ac8 | 2007-05-06 13:45:52 +0000 | [diff] [blame] | 3073 | goto theend; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3074 | len = STRLEN(pat_esc) + 10; |
| 3075 | ptr = alloc((unsigned)len); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3076 | if (ptr == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3077 | { |
| 3078 | vim_free(pat_esc); |
Bram Moolenaar | fa3491a | 2007-02-20 02:49:19 +0000 | [diff] [blame] | 3079 | goto theend; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3080 | } |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3081 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3082 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 3083 | vim_free(pat_esc); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3084 | vim_free(ptr); |
| 3085 | } |
| 3086 | else |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3087 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3088 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3089 | if (regmatch.regprog == NULL) |
| 3090 | goto theend; |
| 3091 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3092 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3093 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 3094 | regmatch.rm_ic = ignorecase(pat); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3095 | while (*dict != NUL && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3096 | { |
| 3097 | /* copy one dictionary file name into buf */ |
| 3098 | if (flags == DICT_EXACT) |
| 3099 | { |
| 3100 | count = 1; |
| 3101 | files = &dict; |
| 3102 | } |
| 3103 | else |
| 3104 | { |
| 3105 | /* Expand wildcards in the dictionary name, but do not allow |
| 3106 | * backticks (for security, the 'dict' option may have been set in |
| 3107 | * a modeline). */ |
| 3108 | copy_option_part(&dict, buf, LSIZE, ","); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3109 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3110 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 3111 | count = -1; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3112 | else |
| 3113 | # endif |
| 3114 | if (vim_strchr(buf, '`') != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3115 | || expand_wildcards(1, &buf, &count, &files, |
| 3116 | EW_FILE|EW_SILENT) != OK) |
| 3117 | count = 0; |
| 3118 | } |
| 3119 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3120 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3121 | if (count == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3122 | { |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3123 | /* Complete from active spelling. Skip "\<" in the pattern, we |
| 3124 | * don't use it as a RE. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3125 | if (pat[0] == '\\' && pat[1] == '<') |
| 3126 | ptr = pat + 2; |
| 3127 | else |
| 3128 | ptr = pat; |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3129 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3130 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3131 | else |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3132 | # endif |
Bram Moolenaar | d7fd0c4 | 2006-08-22 17:55:55 +0000 | [diff] [blame] | 3133 | if (count > 0) /* avoid warning for using "files" uninit */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3134 | { |
| 3135 | ins_compl_files(count, files, thesaurus, flags, |
| 3136 | ®match, buf, &dir); |
| 3137 | if (flags != DICT_EXACT) |
| 3138 | FreeWild(count, files); |
| 3139 | } |
| 3140 | if (flags != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3141 | break; |
| 3142 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3143 | |
| 3144 | theend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3145 | p_scs = save_p_scs; |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 3146 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3147 | vim_free(buf); |
| 3148 | } |
| 3149 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3150 | static void |
| 3151 | ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir) |
| 3152 | int count; |
| 3153 | char_u **files; |
| 3154 | int thesaurus; |
| 3155 | int flags; |
| 3156 | regmatch_T *regmatch; |
| 3157 | char_u *buf; |
| 3158 | int *dir; |
| 3159 | { |
| 3160 | char_u *ptr; |
| 3161 | int i; |
| 3162 | FILE *fp; |
| 3163 | int add_r; |
| 3164 | |
| 3165 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 3166 | { |
| 3167 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 3168 | if (flags != DICT_EXACT) |
| 3169 | { |
| 3170 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3171 | _("Scanning dictionary: %s"), (char *)files[i]); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3172 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | if (fp != NULL) |
| 3176 | { |
| 3177 | /* |
| 3178 | * Read dictionary file line by line. |
| 3179 | * Check each line for a match. |
| 3180 | */ |
| 3181 | while (!got_int && !compl_interrupted |
| 3182 | && !vim_fgets(buf, LSIZE, fp)) |
| 3183 | { |
| 3184 | ptr = buf; |
| 3185 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
| 3186 | { |
| 3187 | ptr = regmatch->startp[0]; |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 3188 | if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3189 | ptr = find_line_end(ptr); |
| 3190 | else |
| 3191 | ptr = find_word_end(ptr); |
| 3192 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 3193 | (int)(ptr - regmatch->startp[0]), |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3194 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3195 | if (thesaurus) |
| 3196 | { |
| 3197 | char_u *wstart; |
| 3198 | |
| 3199 | /* |
| 3200 | * Add the other matches on the line |
| 3201 | */ |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3202 | ptr = buf; |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3203 | while (!got_int) |
| 3204 | { |
| 3205 | /* Find start of the next word. Skip white |
| 3206 | * space and punctuation. */ |
| 3207 | ptr = find_word_start(ptr); |
| 3208 | if (*ptr == NUL || *ptr == NL) |
| 3209 | break; |
| 3210 | wstart = ptr; |
| 3211 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3212 | /* Find end of the word. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3213 | #ifdef FEAT_MBYTE |
| 3214 | if (has_mbyte) |
| 3215 | /* Japanese words may have characters in |
| 3216 | * different classes, only separate words |
| 3217 | * with single-byte non-word characters. */ |
| 3218 | while (*ptr != NUL) |
| 3219 | { |
| 3220 | int l = (*mb_ptr2len)(ptr); |
| 3221 | |
| 3222 | if (l < 2 && !vim_iswordc(*ptr)) |
| 3223 | break; |
| 3224 | ptr += l; |
| 3225 | } |
| 3226 | else |
| 3227 | #endif |
| 3228 | ptr = find_word_end(ptr); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3229 | |
| 3230 | /* Add the word. Skip the regexp match. */ |
| 3231 | if (wstart != regmatch->startp[0]) |
| 3232 | add_r = ins_compl_add_infercase(wstart, |
| 3233 | (int)(ptr - wstart), |
| 3234 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3235 | } |
| 3236 | } |
| 3237 | if (add_r == OK) |
| 3238 | /* if dir was BACKWARD then honor it just once */ |
| 3239 | *dir = FORWARD; |
| 3240 | else if (add_r == FAIL) |
| 3241 | break; |
| 3242 | /* avoid expensive call to vim_regexec() when at end |
| 3243 | * of line */ |
| 3244 | if (*ptr == '\n' || got_int) |
| 3245 | break; |
| 3246 | } |
| 3247 | line_breakcheck(); |
| 3248 | ins_compl_check_keys(50); |
| 3249 | } |
| 3250 | fclose(fp); |
| 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3255 | /* |
| 3256 | * Find the start of the next word. |
| 3257 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 3258 | */ |
| 3259 | char_u * |
| 3260 | find_word_start(ptr) |
| 3261 | char_u *ptr; |
| 3262 | { |
| 3263 | #ifdef FEAT_MBYTE |
| 3264 | if (has_mbyte) |
| 3265 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3266 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3267 | else |
| 3268 | #endif |
| 3269 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 3270 | ++ptr; |
| 3271 | return ptr; |
| 3272 | } |
| 3273 | |
| 3274 | /* |
| 3275 | * Find the end of the word. Assumes it starts inside a word. |
| 3276 | * Returns a pointer to just after the word. |
| 3277 | */ |
| 3278 | char_u * |
| 3279 | find_word_end(ptr) |
| 3280 | char_u *ptr; |
| 3281 | { |
| 3282 | #ifdef FEAT_MBYTE |
| 3283 | int start_class; |
| 3284 | |
| 3285 | if (has_mbyte) |
| 3286 | { |
| 3287 | start_class = mb_get_class(ptr); |
| 3288 | if (start_class > 1) |
| 3289 | while (*ptr != NUL) |
| 3290 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3291 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3292 | if (mb_get_class(ptr) != start_class) |
| 3293 | break; |
| 3294 | } |
| 3295 | } |
| 3296 | else |
| 3297 | #endif |
| 3298 | while (vim_iswordc(*ptr)) |
| 3299 | ++ptr; |
| 3300 | return ptr; |
| 3301 | } |
| 3302 | |
| 3303 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3304 | * Find the end of the line, omitting CR and NL at the end. |
| 3305 | * Returns a pointer to just after the line. |
| 3306 | */ |
| 3307 | static char_u * |
| 3308 | find_line_end(ptr) |
| 3309 | char_u *ptr; |
| 3310 | { |
| 3311 | char_u *s; |
| 3312 | |
| 3313 | s = ptr + STRLEN(ptr); |
| 3314 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 3315 | --s; |
| 3316 | return s; |
| 3317 | } |
| 3318 | |
| 3319 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3320 | * Free the list of completions |
| 3321 | */ |
| 3322 | static void |
| 3323 | ins_compl_free() |
| 3324 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3325 | compl_T *match; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3326 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3328 | vim_free(compl_pattern); |
| 3329 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3330 | vim_free(compl_leader); |
| 3331 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3332 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3333 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | return; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3335 | |
| 3336 | ins_compl_del_pum(); |
| 3337 | pum_clear(); |
| 3338 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3339 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3340 | do |
| 3341 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3342 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3343 | compl_curr_match = compl_curr_match->cp_next; |
| 3344 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3345 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3346 | if (match->cp_flags & FREE_FNAME) |
| 3347 | vim_free(match->cp_fname); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3348 | for (i = 0; i < CPT_COUNT; ++i) |
| 3349 | vim_free(match->cp_text[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3350 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3351 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 3352 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 3353 | compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
| 3356 | static void |
| 3357 | ins_compl_clear() |
| 3358 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3359 | compl_cont_status = 0; |
| 3360 | compl_started = FALSE; |
| 3361 | compl_matches = 0; |
| 3362 | vim_free(compl_pattern); |
| 3363 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3364 | vim_free(compl_leader); |
| 3365 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3366 | edit_submode_extra = NULL; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3367 | vim_free(compl_orig_text); |
| 3368 | compl_orig_text = NULL; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3369 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | /* |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 3373 | * Return TRUE when Insert completion is active. |
| 3374 | */ |
| 3375 | int |
| 3376 | ins_compl_active() |
| 3377 | { |
| 3378 | return compl_started; |
| 3379 | } |
| 3380 | |
| 3381 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3382 | * Delete one character before the cursor and show the subset of the matches |
| 3383 | * that match the word that is now before the cursor. |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3384 | * Returns the character to be used, NUL if the work is done and another char |
| 3385 | * to be got from the user. |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3386 | */ |
| 3387 | static int |
| 3388 | ins_compl_bs() |
| 3389 | { |
| 3390 | char_u *line; |
| 3391 | char_u *p; |
| 3392 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3393 | line = ml_get_curline(); |
| 3394 | p = line + curwin->w_cursor.col; |
| 3395 | mb_ptr_back(line, p); |
| 3396 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 3397 | /* Stop completion when the whole word was deleted. For Omni completion |
| 3398 | * allow the word to be deleted, we won't match everything. */ |
| 3399 | if ((int)(p - line) - (int)compl_col < 0 |
| 3400 | || ((int)(p - line) - (int)compl_col == 0 |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 3401 | && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3402 | return K_BS; |
| 3403 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3404 | /* Deleted more than what was used to find matches or didn't finish |
| 3405 | * finding all matches: need to look for matches all over again. */ |
| 3406 | if (curwin->w_cursor.col <= compl_col + compl_length |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3407 | || ins_compl_need_restart()) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3408 | ins_compl_restart(); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3409 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3410 | vim_free(compl_leader); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3411 | compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3412 | if (compl_leader != NULL) |
| 3413 | { |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3414 | ins_compl_new_leader(); |
Bram Moolenaar | 3978e08 | 2013-03-07 19:38:54 +0100 | [diff] [blame] | 3415 | if (compl_shown_match != NULL) |
| 3416 | /* Make sure current match is not a hidden item. */ |
| 3417 | compl_curr_match = compl_shown_match; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3418 | return NUL; |
| 3419 | } |
| 3420 | return K_BS; |
| 3421 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3422 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3423 | /* |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3424 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 3425 | * be called. |
| 3426 | */ |
| 3427 | static int |
| 3428 | ins_compl_need_restart() |
| 3429 | { |
| 3430 | /* Return TRUE if we didn't complete finding matches or when the |
| 3431 | * 'completefunc' returned "always" in the "refresh" dictionary item. */ |
| 3432 | return compl_was_interrupted |
| 3433 | || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
| 3434 | && compl_opt_refresh_always); |
| 3435 | } |
| 3436 | |
| 3437 | /* |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3438 | * Called after changing "compl_leader". |
| 3439 | * Show the popup menu with a different set of matches. |
| 3440 | * May also search for matches again if the previous search was interrupted. |
| 3441 | */ |
| 3442 | static void |
| 3443 | ins_compl_new_leader() |
| 3444 | { |
| 3445 | ins_compl_del_pum(); |
| 3446 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3447 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3448 | compl_used_match = FALSE; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3449 | |
| 3450 | if (compl_started) |
| 3451 | ins_compl_set_original_text(compl_leader); |
| 3452 | else |
| 3453 | { |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3454 | #ifdef FEAT_SPELL |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3455 | spell_bad_len = 0; /* need to redetect bad word */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3456 | #endif |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3457 | /* |
| 3458 | * Matches were cleared, need to search for them now. First display |
| 3459 | * the changed text before the cursor. Set "compl_restarting" to |
| 3460 | * avoid that the first match is inserted. |
| 3461 | */ |
| 3462 | update_screen(0); |
| 3463 | #ifdef FEAT_GUI |
| 3464 | if (gui.in_use) |
| 3465 | { |
| 3466 | /* Show the cursor after the match, not after the redrawn text. */ |
| 3467 | setcursor(); |
| 3468 | out_flush(); |
| 3469 | gui_update_cursor(FALSE, FALSE); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3470 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3471 | #endif |
| 3472 | compl_restarting = TRUE; |
| 3473 | if (ins_complete(Ctrl_N) == FAIL) |
| 3474 | compl_cont_status = 0; |
| 3475 | compl_restarting = FALSE; |
| 3476 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3477 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3478 | compl_enter_selects = !compl_used_match; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3479 | |
| 3480 | /* Show the popup menu with a different set of matches. */ |
| 3481 | ins_compl_show_pum(); |
Bram Moolenaar | 7073cc8 | 2006-08-29 16:33:06 +0000 | [diff] [blame] | 3482 | |
| 3483 | /* Don't let Enter select the original text when there is no popup menu. */ |
| 3484 | if (compl_match_array == NULL) |
| 3485 | compl_enter_selects = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3489 | * Return the length of the completion, from the completion start column to |
| 3490 | * the cursor column. Making sure it never goes below zero. |
| 3491 | */ |
| 3492 | static int |
| 3493 | ins_compl_len() |
| 3494 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3495 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3496 | |
| 3497 | if (off < 0) |
| 3498 | return 0; |
| 3499 | return off; |
| 3500 | } |
| 3501 | |
| 3502 | /* |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3503 | * Append one character to the match leader. May reduce the number of |
| 3504 | * matches. |
| 3505 | */ |
| 3506 | static void |
| 3507 | ins_compl_addleader(c) |
| 3508 | int c; |
| 3509 | { |
| 3510 | #ifdef FEAT_MBYTE |
| 3511 | int cc; |
| 3512 | |
| 3513 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 3514 | { |
| 3515 | char_u buf[MB_MAXBYTES + 1]; |
| 3516 | |
| 3517 | (*mb_char2bytes)(c, buf); |
| 3518 | buf[cc] = NUL; |
| 3519 | ins_char_bytes(buf, cc); |
Bram Moolenaar | 22189a4 | 2012-06-20 22:56:02 +0200 | [diff] [blame] | 3520 | if (compl_opt_refresh_always) |
| 3521 | AppendToRedobuff(buf); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3522 | } |
| 3523 | else |
| 3524 | #endif |
Bram Moolenaar | 5e1a0a9 | 2012-06-20 14:26:35 +0200 | [diff] [blame] | 3525 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3526 | ins_char(c); |
Bram Moolenaar | 22189a4 | 2012-06-20 22:56:02 +0200 | [diff] [blame] | 3527 | if (compl_opt_refresh_always) |
| 3528 | AppendCharToRedobuff(c); |
Bram Moolenaar | 5e1a0a9 | 2012-06-20 14:26:35 +0200 | [diff] [blame] | 3529 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3530 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3531 | /* If we didn't complete finding matches we must search again. */ |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3532 | if (ins_compl_need_restart()) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3533 | ins_compl_restart(); |
| 3534 | |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3535 | /* When 'always' is set, don't reset compl_leader. While completing, |
Bram Moolenaar | 22189a4 | 2012-06-20 22:56:02 +0200 | [diff] [blame] | 3536 | * cursor doesn't point original position, changing compl_leader would |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3537 | * break redo. */ |
| 3538 | if (!compl_opt_refresh_always) |
| 3539 | { |
| 3540 | vim_free(compl_leader); |
| 3541 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3542 | (int)(curwin->w_cursor.col - compl_col)); |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3543 | if (compl_leader != NULL) |
| 3544 | ins_compl_new_leader(); |
| 3545 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | /* |
| 3549 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 3550 | * BS or a key was typed while still searching for matches. |
| 3551 | */ |
| 3552 | static void |
| 3553 | ins_compl_restart() |
| 3554 | { |
| 3555 | ins_compl_free(); |
| 3556 | compl_started = FALSE; |
| 3557 | compl_matches = 0; |
| 3558 | compl_cont_status = 0; |
| 3559 | compl_cont_mode = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | /* |
| 3563 | * Set the first match, the original text. |
| 3564 | */ |
| 3565 | static void |
| 3566 | ins_compl_set_original_text(str) |
| 3567 | char_u *str; |
| 3568 | { |
| 3569 | char_u *p; |
| 3570 | |
| 3571 | /* Replace the original text entry. */ |
| 3572 | if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */ |
| 3573 | { |
| 3574 | p = vim_strsave(str); |
| 3575 | if (p != NULL) |
| 3576 | { |
| 3577 | vim_free(compl_first_match->cp_str); |
| 3578 | compl_first_match->cp_str = p; |
| 3579 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3580 | } |
| 3581 | } |
| 3582 | |
| 3583 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3584 | * Append one character to the match leader. May reduce the number of |
| 3585 | * matches. |
| 3586 | */ |
| 3587 | static void |
| 3588 | ins_compl_addfrommatch() |
| 3589 | { |
| 3590 | char_u *p; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3591 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3592 | int c; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3593 | compl_T *cp; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3594 | |
| 3595 | p = compl_shown_match->cp_str; |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 3596 | if ((int)STRLEN(p) <= len) /* the match is too short */ |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3597 | { |
| 3598 | /* When still at the original match use the first entry that matches |
| 3599 | * the leader. */ |
| 3600 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 3601 | { |
| 3602 | p = NULL; |
| 3603 | for (cp = compl_shown_match->cp_next; cp != NULL |
| 3604 | && cp != compl_first_match; cp = cp->cp_next) |
| 3605 | { |
Bram Moolenaar | 132283f | 2006-10-03 13:22:23 +0000 | [diff] [blame] | 3606 | if (compl_leader == NULL |
| 3607 | || ins_compl_equal(cp, compl_leader, |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3608 | (int)STRLEN(compl_leader))) |
| 3609 | { |
| 3610 | p = cp->cp_str; |
| 3611 | break; |
| 3612 | } |
| 3613 | } |
| 3614 | if (p == NULL || (int)STRLEN(p) <= len) |
| 3615 | return; |
| 3616 | } |
| 3617 | else |
| 3618 | return; |
| 3619 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3620 | p += len; |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 3621 | c = PTR2CHAR(p); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3622 | ins_compl_addleader(c); |
| 3623 | } |
| 3624 | |
| 3625 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3626 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3627 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3628 | * Returns TRUE when the character is not to be inserted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3629 | */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3630 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3631 | ins_compl_prep(c) |
| 3632 | int c; |
| 3633 | { |
| 3634 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3635 | int want_cindent; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3636 | int retval = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3637 | |
| 3638 | /* Forget any previous 'special' messages if this is actually |
| 3639 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 3640 | */ |
| 3641 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 3642 | edit_submode_extra = NULL; |
| 3643 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 3644 | /* Ignore end of Select mode mapping and mouse scroll buttons. */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 3645 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
| 3646 | || c == K_MOUSELEFT || c == K_MOUSERIGHT) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3647 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3648 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3649 | /* Set "compl_get_longest" when finding the first matches. */ |
| 3650 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 3651 | || (ctrl_x_mode == 0 && !compl_started)) |
| 3652 | { |
| 3653 | compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); |
| 3654 | compl_used_match = TRUE; |
| 3655 | } |
| 3656 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 3658 | { |
| 3659 | /* |
| 3660 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 3661 | * it will be yet. Now we decide. |
| 3662 | */ |
| 3663 | switch (c) |
| 3664 | { |
| 3665 | case Ctrl_E: |
| 3666 | case Ctrl_Y: |
| 3667 | ctrl_x_mode = CTRL_X_SCROLL; |
| 3668 | if (!(State & REPLACE_FLAG)) |
| 3669 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 3670 | else |
| 3671 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 3672 | edit_submode_pre = NULL; |
| 3673 | showmode(); |
| 3674 | break; |
| 3675 | case Ctrl_L: |
| 3676 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 3677 | break; |
| 3678 | case Ctrl_F: |
| 3679 | ctrl_x_mode = CTRL_X_FILES; |
| 3680 | break; |
| 3681 | case Ctrl_K: |
| 3682 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 3683 | break; |
| 3684 | case Ctrl_R: |
| 3685 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 3686 | break; |
| 3687 | case Ctrl_T: |
| 3688 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 3689 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3690 | #ifdef FEAT_COMPL_FUNC |
| 3691 | case Ctrl_U: |
| 3692 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 3693 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3694 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3695 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3696 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3697 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3698 | case 's': |
| 3699 | case Ctrl_S: |
| 3700 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3701 | #ifdef FEAT_SPELL |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3702 | ++emsg_off; /* Avoid getting the E756 error twice. */ |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3703 | spell_back_to_badword(); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3704 | --emsg_off; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3705 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3706 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3707 | case Ctrl_RSB: |
| 3708 | ctrl_x_mode = CTRL_X_TAGS; |
| 3709 | break; |
| 3710 | #ifdef FEAT_FIND_ID |
| 3711 | case Ctrl_I: |
| 3712 | case K_S_TAB: |
| 3713 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 3714 | break; |
| 3715 | case Ctrl_D: |
| 3716 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 3717 | break; |
| 3718 | #endif |
| 3719 | case Ctrl_V: |
| 3720 | case Ctrl_Q: |
| 3721 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 3722 | break; |
| 3723 | case Ctrl_P: |
| 3724 | case Ctrl_N: |
| 3725 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 3726 | * just started ^X mode, or there were enough ^X's to cancel |
| 3727 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 3728 | * do normal expansion when interrupting a different mode (say |
| 3729 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 3730 | * nothing changes if interrupting mode 0, (eg, the flag |
| 3731 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3732 | if (!(compl_cont_status & CONT_INTRPT)) |
| 3733 | compl_cont_status |= CONT_LOCAL; |
| 3734 | else if (compl_cont_mode != 0) |
| 3735 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3736 | /* FALLTHROUGH */ |
| 3737 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3738 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 3739 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 3740 | * mode). |
| 3741 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 3742 | * value, in both cases ^X^X can be used to restart the same |
| 3743 | * mode (avoiding ADDING mode). |
| 3744 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 3745 | * 'complete' and local ^P expansions respectively. |
| 3746 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 3747 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3748 | if (c == Ctrl_X) |
| 3749 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3750 | if (compl_cont_mode != 0) |
| 3751 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3752 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3753 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3754 | } |
| 3755 | ctrl_x_mode = 0; |
| 3756 | edit_submode = NULL; |
| 3757 | showmode(); |
| 3758 | break; |
| 3759 | } |
| 3760 | } |
| 3761 | else if (ctrl_x_mode != 0) |
| 3762 | { |
| 3763 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 3764 | if (!vim_is_ctrl_x_key(c)) |
| 3765 | { |
| 3766 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 3767 | ctrl_x_mode = 0; |
| 3768 | else |
| 3769 | ctrl_x_mode = CTRL_X_FINISHED; |
| 3770 | edit_submode = NULL; |
| 3771 | } |
| 3772 | showmode(); |
| 3773 | } |
| 3774 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3775 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3776 | { |
| 3777 | /* Show error message from attempted keyword completion (probably |
| 3778 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3779 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3780 | showmode(); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3781 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
| 3782 | && !ins_compl_pum_key(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3783 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 3784 | { |
| 3785 | /* Get here when we have finished typing a sequence of ^N and |
| 3786 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3787 | * memory that was used, and make sure we can redo the insert. */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3788 | if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3789 | { |
| 3790 | /* |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3791 | * If any of the original typed text has been changed, eg when |
| 3792 | * ignorecase is set, we must add back-spaces to the redo |
| 3793 | * buffer. We add as few as necessary to delete just the part |
| 3794 | * of the original text that has changed. |
| 3795 | * When using the longest match, edited the match or used |
| 3796 | * CTRL-E then don't use the current match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3797 | */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3798 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
| 3799 | ptr = compl_curr_match->cp_str; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3800 | else |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 3801 | ptr = NULL; |
| 3802 | ins_compl_fixRedoBufForLeader(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | #ifdef FEAT_CINDENT |
| 3806 | want_cindent = (can_cindent && cindent_on()); |
| 3807 | #endif |
| 3808 | /* |
| 3809 | * When completing whole lines: fix indent for 'cindent'. |
| 3810 | * Otherwise, break line if it's too long. |
| 3811 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3812 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | { |
| 3814 | #ifdef FEAT_CINDENT |
| 3815 | /* re-indent the current line */ |
| 3816 | if (want_cindent) |
| 3817 | { |
| 3818 | do_c_expr_indent(); |
| 3819 | want_cindent = FALSE; /* don't do it again */ |
| 3820 | } |
| 3821 | #endif |
| 3822 | } |
| 3823 | else |
| 3824 | { |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3825 | int prev_col = curwin->w_cursor.col; |
| 3826 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3827 | /* put the cursor on the last char, for 'tw' formatting */ |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3828 | if (prev_col > 0) |
| 3829 | dec_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3830 | if (stop_arrow() == OK) |
| 3831 | insertchar(NUL, 0, -1); |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3832 | if (prev_col > 0 |
| 3833 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 3834 | inc_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | } |
| 3836 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3837 | /* If the popup menu is displayed pressing CTRL-Y means accepting |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3838 | * the selection without inserting anything. When |
| 3839 | * compl_enter_selects is set the Enter key does the same. */ |
| 3840 | if ((c == Ctrl_Y || (compl_enter_selects |
| 3841 | && (c == CAR || c == K_KENTER || c == NL))) |
| 3842 | && pum_visible()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3843 | retval = TRUE; |
| 3844 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3845 | /* CTRL-E means completion is Ended, go back to the typed text. */ |
| 3846 | if (c == Ctrl_E) |
| 3847 | { |
| 3848 | ins_compl_delete(); |
| 3849 | if (compl_leader != NULL) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3850 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3851 | else if (compl_first_match != NULL) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3852 | ins_bytes(compl_orig_text + ins_compl_len()); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3853 | retval = TRUE; |
| 3854 | } |
| 3855 | |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 3856 | auto_format(FALSE, TRUE); |
| 3857 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3858 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3859 | compl_started = FALSE; |
| 3860 | compl_matches = 0; |
Bram Moolenaar | f1924a9 | 2014-07-16 14:42:46 +0200 | [diff] [blame] | 3861 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 3862 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3863 | ctrl_x_mode = 0; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3864 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3865 | if (edit_submode != NULL) |
| 3866 | { |
| 3867 | edit_submode = NULL; |
| 3868 | showmode(); |
| 3869 | } |
| 3870 | |
| 3871 | #ifdef FEAT_CINDENT |
| 3872 | /* |
| 3873 | * Indent now if a key was typed that is in 'cinkeys'. |
| 3874 | */ |
| 3875 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 3876 | do_c_expr_indent(); |
| 3877 | #endif |
Bram Moolenaar | cfa3cae | 2012-07-10 17:14:56 +0200 | [diff] [blame] | 3878 | #ifdef FEAT_AUTOCMD |
| 3879 | /* Trigger the CompleteDone event to give scripts a chance to act |
| 3880 | * upon the completion. */ |
| 3881 | apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf); |
| 3882 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3883 | } |
| 3884 | } |
Bram Moolenaar | a391432 | 2013-02-13 16:30:21 +0100 | [diff] [blame] | 3885 | #ifdef FEAT_AUTOCMD |
| 3886 | else if (ctrl_x_mode == CTRL_X_LOCAL_MSG) |
| 3887 | /* Trigger the CompleteDone event to give scripts a chance to act |
| 3888 | * upon the (possibly failed) completion. */ |
| 3889 | apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf); |
| 3890 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3891 | |
| 3892 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 3893 | * (re)set properly in ins_complete() */ |
| 3894 | if (!vim_is_ctrl_x_key(c)) |
| 3895 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3896 | compl_cont_status = 0; |
| 3897 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3898 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3899 | |
| 3900 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | /* |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 3904 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 3905 | * text. This inserts backspaces and appends the changed text. |
| 3906 | * "ptr" is the known leader text or NUL. |
| 3907 | */ |
| 3908 | static void |
| 3909 | ins_compl_fixRedoBufForLeader(ptr_arg) |
| 3910 | char_u *ptr_arg; |
| 3911 | { |
| 3912 | int len; |
| 3913 | char_u *p; |
| 3914 | char_u *ptr = ptr_arg; |
| 3915 | |
| 3916 | if (ptr == NULL) |
| 3917 | { |
| 3918 | if (compl_leader != NULL) |
| 3919 | ptr = compl_leader; |
| 3920 | else |
| 3921 | return; /* nothing to do */ |
| 3922 | } |
| 3923 | if (compl_orig_text != NULL) |
| 3924 | { |
| 3925 | p = compl_orig_text; |
| 3926 | for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len) |
| 3927 | ; |
| 3928 | #ifdef FEAT_MBYTE |
| 3929 | if (len > 0) |
| 3930 | len -= (*mb_head_off)(p, p + len); |
| 3931 | #endif |
| 3932 | for (p += len; *p != NUL; mb_ptr_adv(p)) |
| 3933 | AppendCharToRedobuff(K_BS); |
| 3934 | } |
| 3935 | else |
| 3936 | len = 0; |
| 3937 | if (ptr != NULL) |
| 3938 | AppendToRedobuffLit(ptr + len, -1); |
| 3939 | } |
| 3940 | |
| 3941 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3942 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3943 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3944 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3945 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3946 | * |
| 3947 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3948 | */ |
| 3949 | static buf_T * |
| 3950 | ins_compl_next_buf(buf, flag) |
| 3951 | buf_T *buf; |
| 3952 | int flag; |
| 3953 | { |
| 3954 | #ifdef FEAT_WINDOWS |
| 3955 | static win_T *wp; |
| 3956 | #endif |
| 3957 | |
| 3958 | if (flag == 'w') /* just windows */ |
| 3959 | { |
| 3960 | #ifdef FEAT_WINDOWS |
| 3961 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 3962 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3963 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | && wp->w_buffer->b_scanned) |
| 3965 | ; |
| 3966 | buf = wp->w_buffer; |
| 3967 | #else |
| 3968 | buf = curbuf; |
| 3969 | #endif |
| 3970 | } |
| 3971 | else |
| 3972 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3973 | * (unlisted buffers) |
| 3974 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3975 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3976 | && ((flag == 'U' |
| 3977 | ? buf->b_p_bl |
| 3978 | : (!buf->b_p_bl |
| 3979 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3980 | || buf->b_scanned)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3981 | ; |
| 3982 | return buf; |
| 3983 | } |
| 3984 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3985 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3986 | static void expand_by_function __ARGS((int type, char_u *base)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3987 | |
| 3988 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3989 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3990 | * get matches in "matches". |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3991 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3992 | static void |
| 3993 | expand_by_function(type, base) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3994 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3995 | char_u *base; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3996 | { |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3997 | list_T *matchlist = NULL; |
| 3998 | dict_T *matchdict = NULL; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3999 | char_u *args[2]; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4000 | char_u *funcname; |
| 4001 | pos_T pos; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4002 | win_T *curwin_save; |
| 4003 | buf_T *curbuf_save; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4004 | typval_T rettv; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4005 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4006 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 4007 | if (*funcname == NUL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4008 | return; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4009 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4010 | /* Call 'completefunc' to obtain the list of matches. */ |
| 4011 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4012 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4013 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4014 | pos = curwin->w_cursor; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4015 | curwin_save = curwin; |
| 4016 | curbuf_save = curbuf; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4017 | |
| 4018 | /* Call a function, which returns a list or dict. */ |
Bram Moolenaar | 0cbba94 | 2012-07-25 16:47:03 +0200 | [diff] [blame] | 4019 | if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK) |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4020 | { |
| 4021 | switch (rettv.v_type) |
| 4022 | { |
| 4023 | case VAR_LIST: |
| 4024 | matchlist = rettv.vval.v_list; |
| 4025 | break; |
| 4026 | case VAR_DICT: |
| 4027 | matchdict = rettv.vval.v_dict; |
| 4028 | break; |
| 4029 | default: |
| 4030 | /* TODO: Give error message? */ |
| 4031 | clear_tv(&rettv); |
| 4032 | break; |
| 4033 | } |
| 4034 | } |
| 4035 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4036 | if (curwin_save != curwin || curbuf_save != curbuf) |
| 4037 | { |
| 4038 | EMSG(_(e_complwin)); |
| 4039 | goto theend; |
| 4040 | } |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4041 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 834def3 | 2014-09-09 18:29:33 +0200 | [diff] [blame] | 4042 | validate_cursor(); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4043 | if (!equalpos(curwin->w_cursor, pos)) |
| 4044 | { |
| 4045 | EMSG(_(e_compldel)); |
| 4046 | goto theend; |
| 4047 | } |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4048 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4049 | if (matchlist != NULL) |
| 4050 | ins_compl_add_list(matchlist); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4051 | else if (matchdict != NULL) |
| 4052 | ins_compl_add_dict(matchdict); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 4053 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4054 | theend: |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4055 | if (matchdict != NULL) |
| 4056 | dict_unref(matchdict); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 4057 | if (matchlist != NULL) |
| 4058 | list_unref(matchlist); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4059 | } |
| 4060 | #endif /* FEAT_COMPL_FUNC */ |
| 4061 | |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4062 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4063 | /* |
| 4064 | * Add completions from a list. |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4065 | */ |
| 4066 | static void |
| 4067 | ins_compl_add_list(list) |
| 4068 | list_T *list; |
| 4069 | { |
| 4070 | listitem_T *li; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4071 | int dir = compl_direction; |
| 4072 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4073 | /* Go through the List with matches and add each of them. */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4074 | for (li = list->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4075 | { |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4076 | if (ins_compl_add_tv(&li->li_tv, dir) == OK) |
| 4077 | /* if dir was BACKWARD then honor it just once */ |
| 4078 | dir = FORWARD; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 4079 | else if (did_emsg) |
| 4080 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4081 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4082 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4083 | |
| 4084 | /* |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4085 | * Add completions from a dict. |
| 4086 | */ |
| 4087 | static void |
| 4088 | ins_compl_add_dict(dict) |
| 4089 | dict_T *dict; |
| 4090 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4091 | dictitem_T *di_refresh; |
| 4092 | dictitem_T *di_words; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4093 | |
| 4094 | /* Check for optional "refresh" item. */ |
| 4095 | compl_opt_refresh_always = FALSE; |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4096 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 4097 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4098 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4099 | char_u *v = di_refresh->di_tv.vval.v_string; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4100 | |
| 4101 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 4102 | compl_opt_refresh_always = TRUE; |
| 4103 | } |
| 4104 | |
| 4105 | /* Add completions from a "words" list. */ |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4106 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 4107 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 4108 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4109 | } |
| 4110 | |
| 4111 | /* |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4112 | * Add a match to the list of matches from a typeval_T. |
| 4113 | * If the given string is already in the list of completions, then return |
| 4114 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 4115 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 4116 | */ |
| 4117 | int |
| 4118 | ins_compl_add_tv(tv, dir) |
| 4119 | typval_T *tv; |
| 4120 | int dir; |
| 4121 | { |
| 4122 | char_u *word; |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 4123 | int icase = FALSE; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4124 | int adup = FALSE; |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4125 | int aempty = FALSE; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4126 | char_u *(cptext[CPT_COUNT]); |
| 4127 | |
| 4128 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 4129 | { |
| 4130 | word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE); |
| 4131 | cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict, |
| 4132 | (char_u *)"abbr", FALSE); |
| 4133 | cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict, |
| 4134 | (char_u *)"menu", FALSE); |
| 4135 | cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict, |
| 4136 | (char_u *)"kind", FALSE); |
| 4137 | cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict, |
| 4138 | (char_u *)"info", FALSE); |
| 4139 | if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL) |
| 4140 | icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase"); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 4141 | 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] | 4142 | adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup"); |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4143 | if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL) |
| 4144 | aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty"); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4145 | } |
| 4146 | else |
| 4147 | { |
| 4148 | word = get_tv_string_chk(tv); |
| 4149 | vim_memset(cptext, 0, sizeof(cptext)); |
| 4150 | } |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4151 | if (word == NULL || (!aempty && *word == NUL)) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4152 | return FAIL; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4153 | return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4154 | } |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4155 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4156 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4157 | /* |
| 4158 | * Get the next expansion(s), using "compl_pattern". |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4159 | * The search starts at position "ini" in curbuf and in the direction |
| 4160 | * compl_direction. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4161 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4162 | * where we stopped searching before. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4163 | * This may return before finding all the matches. |
| 4164 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | */ |
| 4166 | static int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4167 | ins_compl_get_exp(ini) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4168 | pos_T *ini; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4169 | { |
| 4170 | static pos_T first_match_pos; |
| 4171 | static pos_T last_match_pos; |
| 4172 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4173 | static int found_all = FALSE; /* Found all matches of a |
| 4174 | certain type. */ |
| 4175 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4176 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4177 | pos_T *pos; |
| 4178 | char_u **matches; |
| 4179 | int save_p_scs; |
| 4180 | int save_p_ws; |
| 4181 | int save_p_ic; |
| 4182 | int i; |
| 4183 | int num_matches; |
| 4184 | int len; |
| 4185 | int found_new_match; |
| 4186 | int type = ctrl_x_mode; |
| 4187 | char_u *ptr; |
| 4188 | char_u *dict = NULL; |
| 4189 | int dict_f = 0; |
| 4190 | compl_T *old_match; |
Bram Moolenaar | 361aa50 | 2014-01-23 22:45:58 +0100 | [diff] [blame] | 4191 | int set_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4192 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4193 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4194 | { |
| 4195 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 4196 | ins_buf->b_scanned = 0; |
| 4197 | found_all = FALSE; |
| 4198 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4199 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4200 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | last_match_pos = first_match_pos = *ini; |
| 4202 | } |
| 4203 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4204 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4205 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4206 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 4207 | for (;;) |
| 4208 | { |
| 4209 | found_new_match = FAIL; |
Bram Moolenaar | 361aa50 | 2014-01-23 22:45:58 +0100 | [diff] [blame] | 4210 | set_match_pos = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4212 | /* 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] | 4213 | * or if found_all says this entry is done. For ^X^L only use the |
| 4214 | * entries from 'complete' that look in loaded buffers. */ |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4215 | if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4216 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4217 | { |
| 4218 | found_all = FALSE; |
| 4219 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 4220 | e_cpt++; |
| 4221 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 4222 | { |
| 4223 | ins_buf = curbuf; |
| 4224 | first_match_pos = *ini; |
| 4225 | /* So that ^N can match word immediately after cursor */ |
| 4226 | if (ctrl_x_mode == 0) |
| 4227 | dec(&first_match_pos); |
| 4228 | last_match_pos = first_match_pos; |
| 4229 | type = 0; |
Bram Moolenaar | 361aa50 | 2014-01-23 22:45:58 +0100 | [diff] [blame] | 4230 | |
| 4231 | /* Remember the first match so that the loop stops when we |
| 4232 | * wrap and come back there a second time. */ |
| 4233 | set_match_pos = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4234 | } |
| 4235 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 4236 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 4237 | { |
| 4238 | /* Scan a buffer, but not the current one. */ |
| 4239 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 4240 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4241 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4242 | first_match_pos.col = last_match_pos.col = 0; |
| 4243 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 4244 | last_match_pos.lnum = 0; |
| 4245 | type = 0; |
| 4246 | } |
| 4247 | else /* unloaded buffer, scan like dictionary */ |
| 4248 | { |
| 4249 | found_all = TRUE; |
| 4250 | if (ins_buf->b_fname == NULL) |
| 4251 | continue; |
| 4252 | type = CTRL_X_DICTIONARY; |
| 4253 | dict = ins_buf->b_fname; |
| 4254 | dict_f = DICT_EXACT; |
| 4255 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4256 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | ins_buf->b_fname == NULL |
| 4258 | ? buf_spname(ins_buf) |
| 4259 | : ins_buf->b_sfname == NULL |
Bram Moolenaar | 4ccb265 | 2012-10-04 22:38:37 +0200 | [diff] [blame] | 4260 | ? ins_buf->b_fname |
| 4261 | : ins_buf->b_sfname); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 4262 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4263 | } |
| 4264 | else if (*e_cpt == NUL) |
| 4265 | break; |
| 4266 | else |
| 4267 | { |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4268 | if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4269 | type = -1; |
| 4270 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 4271 | { |
| 4272 | if (*e_cpt == 'k') |
| 4273 | type = CTRL_X_DICTIONARY; |
| 4274 | else |
| 4275 | type = CTRL_X_THESAURUS; |
| 4276 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 4277 | { |
| 4278 | dict = e_cpt; |
| 4279 | dict_f = DICT_FIRST; |
| 4280 | } |
| 4281 | } |
| 4282 | #ifdef FEAT_FIND_ID |
| 4283 | else if (*e_cpt == 'i') |
| 4284 | type = CTRL_X_PATH_PATTERNS; |
| 4285 | else if (*e_cpt == 'd') |
| 4286 | type = CTRL_X_PATH_DEFINES; |
| 4287 | #endif |
| 4288 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 4289 | { |
| 4290 | type = CTRL_X_TAGS; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4291 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 4292 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4293 | } |
| 4294 | else |
| 4295 | type = -1; |
| 4296 | |
| 4297 | /* in any case e_cpt is advanced to the next entry */ |
| 4298 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 4299 | |
| 4300 | found_all = TRUE; |
| 4301 | if (type == -1) |
| 4302 | continue; |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | switch (type) |
| 4307 | { |
| 4308 | case -1: |
| 4309 | break; |
| 4310 | #ifdef FEAT_FIND_ID |
| 4311 | case CTRL_X_PATH_PATTERNS: |
| 4312 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4313 | find_pattern_in_path(compl_pattern, compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4314 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4315 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4316 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4317 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 4318 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 4319 | break; |
| 4320 | #endif |
| 4321 | |
| 4322 | case CTRL_X_DICTIONARY: |
| 4323 | case CTRL_X_THESAURUS: |
| 4324 | ins_compl_dictionaries( |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 4325 | dict != NULL ? dict |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4326 | : (type == CTRL_X_THESAURUS |
| 4327 | ? (*curbuf->b_p_tsr == NUL |
| 4328 | ? p_tsr |
| 4329 | : curbuf->b_p_tsr) |
| 4330 | : (*curbuf->b_p_dict == NUL |
| 4331 | ? p_dict |
| 4332 | : curbuf->b_p_dict)), |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4333 | compl_pattern, |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 4334 | dict != NULL ? dict_f |
| 4335 | : 0, type == CTRL_X_THESAURUS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4336 | dict = NULL; |
| 4337 | break; |
| 4338 | |
| 4339 | case CTRL_X_TAGS: |
| 4340 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 4341 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4342 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4343 | |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4344 | /* Find up to TAG_MANY matches. Avoids that an enormous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4345 | * of matches is found when compl_pattern is empty */ |
| 4346 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4347 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 4348 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 4349 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 4350 | { |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4351 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4352 | } |
| 4353 | p_ic = save_p_ic; |
| 4354 | break; |
| 4355 | |
| 4356 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4357 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4358 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 4359 | { |
| 4360 | |
| 4361 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4362 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 4363 | ins_compl_add_matches(num_matches, matches, p_fic || p_wic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4364 | } |
| 4365 | break; |
| 4366 | |
| 4367 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4368 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 4369 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4370 | &num_matches, &matches) == EXPAND_OK) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4371 | ins_compl_add_matches(num_matches, matches, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4372 | break; |
| 4373 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4374 | #ifdef FEAT_COMPL_FUNC |
| 4375 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4376 | case CTRL_X_OMNI: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4377 | expand_by_function(type, compl_pattern); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4378 | break; |
| 4379 | #endif |
| 4380 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4381 | case CTRL_X_SPELL: |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 4382 | #ifdef FEAT_SPELL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4383 | num_matches = expand_spelling(first_match_pos.lnum, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4384 | compl_pattern, &matches); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4385 | if (num_matches > 0) |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4386 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4387 | #endif |
| 4388 | break; |
| 4389 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4390 | default: /* normal ^P/^N and ^X^L */ |
| 4391 | /* |
| 4392 | * If 'infercase' is set, don't use 'smartcase' here |
| 4393 | */ |
| 4394 | save_p_scs = p_scs; |
| 4395 | if (ins_buf->b_p_inf) |
| 4396 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4397 | |
Bram Moolenaar | 361aa50 | 2014-01-23 22:45:58 +0100 | [diff] [blame] | 4398 | /* Buffers other than curbuf are scanned from the beginning or the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4399 | * end but never from the middle, thus setting nowrapscan in this |
| 4400 | * buffers is a good idea, on the other hand, we always set |
| 4401 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 4402 | save_p_ws = p_ws; |
| 4403 | if (ins_buf != curbuf) |
| 4404 | p_ws = FALSE; |
| 4405 | else if (*e_cpt == '.') |
| 4406 | p_ws = TRUE; |
| 4407 | for (;;) |
| 4408 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4409 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4410 | |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 4411 | ++msg_silent; /* Don't want messages for wrapscan. */ |
| 4412 | |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4413 | /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode) |
| 4414 | * || word-wise search that |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4415 | * has added a word that was at the beginning of the line */ |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4416 | if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4417 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4418 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4419 | compl_direction, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4420 | else |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4421 | found_new_match = searchit(NULL, ins_buf, pos, |
| 4422 | compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4423 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 4424 | RE_LAST, (linenr_T)0, NULL); |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 4425 | --msg_silent; |
Bram Moolenaar | 361aa50 | 2014-01-23 22:45:58 +0100 | [diff] [blame] | 4426 | if (!compl_started || set_match_pos) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4427 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4428 | /* set "compl_started" even on fail */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4429 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4430 | first_match_pos = *pos; |
| 4431 | last_match_pos = *pos; |
Bram Moolenaar | 361aa50 | 2014-01-23 22:45:58 +0100 | [diff] [blame] | 4432 | set_match_pos = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | } |
| 4434 | else if (first_match_pos.lnum == last_match_pos.lnum |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4435 | && first_match_pos.col == last_match_pos.col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4436 | found_new_match = FAIL; |
| 4437 | if (found_new_match == FAIL) |
| 4438 | { |
| 4439 | if (ins_buf == curbuf) |
| 4440 | found_all = TRUE; |
| 4441 | break; |
| 4442 | } |
| 4443 | |
| 4444 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4445 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4446 | && ini->lnum == pos->lnum |
| 4447 | && ini->col == pos->col) |
| 4448 | continue; |
| 4449 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4450 | if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4452 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | { |
| 4454 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 4455 | continue; |
| 4456 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 4457 | if (!p_paste) |
| 4458 | ptr = skipwhite(ptr); |
| 4459 | } |
| 4460 | len = (int)STRLEN(ptr); |
| 4461 | } |
| 4462 | else |
| 4463 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4464 | char_u *tmp_ptr = ptr; |
| 4465 | |
| 4466 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4467 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4468 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4469 | /* Skip if already inside a word. */ |
| 4470 | if (vim_iswordp(tmp_ptr)) |
| 4471 | continue; |
| 4472 | /* Find start of next word. */ |
| 4473 | tmp_ptr = find_word_start(tmp_ptr); |
| 4474 | } |
| 4475 | /* Find end of this word. */ |
| 4476 | tmp_ptr = find_word_end(tmp_ptr); |
| 4477 | len = (int)(tmp_ptr - ptr); |
| 4478 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4479 | if ((compl_cont_status & CONT_ADDING) |
| 4480 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4481 | { |
| 4482 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 4483 | { |
| 4484 | /* Try next line, if any. the new word will be |
| 4485 | * "join" as if the normal command "J" was used. |
| 4486 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4487 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4488 | * works -- Acevedo */ |
| 4489 | STRNCPY(IObuff, ptr, len); |
| 4490 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 4491 | tmp_ptr = ptr = skipwhite(ptr); |
| 4492 | /* Find start of next word. */ |
| 4493 | tmp_ptr = find_word_start(tmp_ptr); |
| 4494 | /* Find end of next word. */ |
| 4495 | tmp_ptr = find_word_end(tmp_ptr); |
| 4496 | if (tmp_ptr > ptr) |
| 4497 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4498 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4499 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4500 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4501 | IObuff[len++] = ' '; |
| 4502 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 4503 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4504 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4505 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 4506 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4507 | && (IObuff[len - 2] == '?' |
| 4508 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4509 | IObuff[len++] = ' '; |
| 4510 | } |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4511 | /* copy as much as possible of the new word */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4512 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 4513 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 4514 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 4515 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4516 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4517 | } |
| 4518 | IObuff[len] = NUL; |
| 4519 | ptr = IObuff; |
| 4520 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4521 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | continue; |
| 4523 | } |
| 4524 | } |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4525 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4526 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4527 | 0, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | { |
| 4529 | found_new_match = OK; |
| 4530 | break; |
| 4531 | } |
| 4532 | } |
| 4533 | p_scs = save_p_scs; |
| 4534 | p_ws = save_p_ws; |
| 4535 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4536 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4537 | /* check if compl_curr_match has changed, (e.g. other type of |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 4538 | * expansion added something) */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4539 | if (type != 0 && compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4540 | found_new_match = OK; |
| 4541 | |
| 4542 | /* break the loop for specialized modes (use 'complete' just for the |
| 4543 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4544 | if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4545 | || found_new_match != FAIL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4546 | { |
| 4547 | if (got_int) |
| 4548 | break; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4549 | /* Fill the popup menu as soon as possible. */ |
Bram Moolenaar | 5948a57 | 2006-10-03 13:49:29 +0000 | [diff] [blame] | 4550 | if (type != -1) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4551 | ins_compl_check_keys(0); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4552 | |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4553 | if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4554 | || compl_interrupted) |
| 4555 | break; |
| 4556 | compl_started = TRUE; |
| 4557 | } |
| 4558 | else |
| 4559 | { |
| 4560 | /* Mark a buffer scanned when it has been scanned completely */ |
| 4561 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 4562 | ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4563 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4564 | compl_started = FALSE; |
| 4565 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4566 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4567 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4569 | if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4570 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 4571 | found_new_match = FAIL; |
| 4572 | |
| 4573 | i = -1; /* total of matches, unknown */ |
| 4574 | if (found_new_match == FAIL |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 4575 | || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4576 | i = ins_compl_make_cyclic(); |
| 4577 | |
| 4578 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4579 | * just been made cyclic then we have to move compl_curr_match to the next |
| 4580 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4581 | compl_curr_match = compl_direction == FORWARD ? old_match->cp_next |
| 4582 | : old_match->cp_prev; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4583 | if (compl_curr_match == NULL) |
| 4584 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4585 | return i; |
| 4586 | } |
| 4587 | |
| 4588 | /* Delete the old text being completed. */ |
| 4589 | static void |
| 4590 | ins_compl_delete() |
| 4591 | { |
| 4592 | int i; |
| 4593 | |
| 4594 | /* |
| 4595 | * In insert mode: Delete the typed part. |
| 4596 | * In replace mode: Put the old characters back, if any. |
| 4597 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4598 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4599 | backspace_until_column(i); |
Bram Moolenaar | f1924a9 | 2014-07-16 14:42:46 +0200 | [diff] [blame] | 4600 | |
Bram Moolenaar | 674fffe | 2014-07-23 13:50:46 +0200 | [diff] [blame] | 4601 | /* TODO: is this sufficient for redrawing? Redrawing everything causes |
| 4602 | * flicker, thus we can't do that. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4603 | changed_cline_bef_curs(); |
| 4604 | } |
| 4605 | |
| 4606 | /* Insert the new text being completed. */ |
| 4607 | static void |
| 4608 | ins_compl_insert() |
| 4609 | { |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 4610 | ins_bytes(compl_shown_match->cp_str + ins_compl_len()); |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 4611 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 4612 | compl_used_match = FALSE; |
| 4613 | else |
| 4614 | compl_used_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | /* |
| 4618 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4619 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 4620 | * get more completions. If it is FALSE, then we just do nothing when there |
| 4621 | * are no more completions in a given direction. The latter case is used when |
| 4622 | * we are still in the middle of finding completions, to allow browsing |
| 4623 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4624 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 4625 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4626 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 4627 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4628 | * |
| 4629 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4630 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 4631 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4632 | */ |
| 4633 | static int |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4634 | ins_compl_next(allow_get_expansion, count, insert_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4635 | int allow_get_expansion; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4636 | int count; /* repeat completion this many times; should |
| 4637 | be at least 1 */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4638 | int insert_match; /* Insert the newly selected match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4639 | { |
| 4640 | int num_matches = -1; |
| 4641 | int i; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4642 | int todo = count; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4643 | compl_T *found_compl = NULL; |
| 4644 | int found_end = FALSE; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4645 | int advance; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4646 | |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 4647 | /* When user complete function return -1 for findstart which is next |
| 4648 | * time of 'always', compl_shown_match become NULL. */ |
| 4649 | if (compl_shown_match == NULL) |
| 4650 | return -1; |
| 4651 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4652 | if (compl_leader != NULL |
| 4653 | && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4654 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4655 | /* Set "compl_shown_match" to the actually shown match, it may differ |
| 4656 | * when "compl_leader" is used to omit some of the matches. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4657 | while (!ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4658 | compl_leader, (int)STRLEN(compl_leader)) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4659 | && compl_shown_match->cp_next != NULL |
| 4660 | && compl_shown_match->cp_next != compl_first_match) |
| 4661 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 4662 | |
| 4663 | /* If we didn't find it searching forward, and compl_shows_dir is |
| 4664 | * backward, find the last match. */ |
| 4665 | if (compl_shows_dir == BACKWARD |
| 4666 | && !ins_compl_equal(compl_shown_match, |
| 4667 | compl_leader, (int)STRLEN(compl_leader)) |
| 4668 | && (compl_shown_match->cp_next == NULL |
| 4669 | || compl_shown_match->cp_next == compl_first_match)) |
| 4670 | { |
| 4671 | while (!ins_compl_equal(compl_shown_match, |
| 4672 | compl_leader, (int)STRLEN(compl_leader)) |
| 4673 | && compl_shown_match->cp_prev != NULL |
| 4674 | && compl_shown_match->cp_prev != compl_first_match) |
| 4675 | compl_shown_match = compl_shown_match->cp_prev; |
| 4676 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | if (allow_get_expansion && insert_match |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4680 | && (!(compl_get_longest || compl_restarting) || compl_used_match)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4681 | /* Delete old text to be replaced */ |
| 4682 | ins_compl_delete(); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4683 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4684 | /* When finding the longest common text we stick at the original text, |
| 4685 | * don't let CTRL-N or CTRL-P move to the first match. */ |
| 4686 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 4687 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4688 | /* When restarting the search don't insert the first match either. */ |
| 4689 | if (compl_restarting) |
| 4690 | { |
| 4691 | advance = FALSE; |
| 4692 | compl_restarting = FALSE; |
| 4693 | } |
| 4694 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4695 | /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 4696 | * around. */ |
| 4697 | while (--todo >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4698 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4699 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4700 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4701 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4702 | found_end = (compl_first_match != NULL |
| 4703 | && (compl_shown_match->cp_next == compl_first_match |
| 4704 | || compl_shown_match == compl_first_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4705 | } |
| 4706 | else if (compl_shows_dir == BACKWARD |
| 4707 | && compl_shown_match->cp_prev != NULL) |
| 4708 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4709 | found_end = (compl_shown_match == compl_first_match); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4710 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4711 | found_end |= (compl_shown_match == compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4712 | } |
| 4713 | else |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4714 | { |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4715 | if (!allow_get_expansion) |
| 4716 | { |
| 4717 | if (advance) |
| 4718 | { |
| 4719 | if (compl_shows_dir == BACKWARD) |
| 4720 | compl_pending -= todo + 1; |
| 4721 | else |
| 4722 | compl_pending += todo + 1; |
| 4723 | } |
| 4724 | return -1; |
| 4725 | } |
| 4726 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4727 | if (advance) |
| 4728 | { |
| 4729 | if (compl_shows_dir == BACKWARD) |
| 4730 | --compl_pending; |
| 4731 | else |
| 4732 | ++compl_pending; |
| 4733 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4734 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4735 | /* Find matches. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4736 | num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4737 | |
| 4738 | /* handle any pending completions */ |
| 4739 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4740 | && advance) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4741 | { |
| 4742 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 4743 | { |
| 4744 | compl_shown_match = compl_shown_match->cp_next; |
| 4745 | --compl_pending; |
| 4746 | } |
| 4747 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 4748 | { |
| 4749 | compl_shown_match = compl_shown_match->cp_prev; |
| 4750 | ++compl_pending; |
| 4751 | } |
| 4752 | else |
| 4753 | break; |
| 4754 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4755 | found_end = FALSE; |
| 4756 | } |
| 4757 | if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 |
| 4758 | && compl_leader != NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4759 | && !ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4760 | compl_leader, (int)STRLEN(compl_leader))) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4761 | ++todo; |
| 4762 | else |
| 4763 | /* Remember a matching item. */ |
| 4764 | found_compl = compl_shown_match; |
| 4765 | |
| 4766 | /* Stop at the end of the list when we found a usable match. */ |
| 4767 | if (found_end) |
| 4768 | { |
| 4769 | if (found_compl != NULL) |
| 4770 | { |
| 4771 | compl_shown_match = found_compl; |
| 4772 | break; |
| 4773 | } |
| 4774 | todo = 1; /* use first usable match after wrapping around */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4775 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4778 | /* Insert the text of the new completion, or the compl_leader. */ |
| 4779 | if (insert_match) |
| 4780 | { |
| 4781 | if (!compl_get_longest || compl_used_match) |
| 4782 | ins_compl_insert(); |
| 4783 | else |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 4784 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4785 | } |
| 4786 | else |
| 4787 | compl_used_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4788 | |
| 4789 | if (!allow_get_expansion) |
| 4790 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4791 | /* may undisplay the popup menu first */ |
| 4792 | ins_compl_upd_pum(); |
| 4793 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4794 | /* redraw to show the user what was inserted */ |
| 4795 | update_screen(0); |
| 4796 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4797 | /* display the updated popup menu */ |
| 4798 | ins_compl_show_pum(); |
Bram Moolenaar | 1471681 | 2006-05-04 21:54:08 +0000 | [diff] [blame] | 4799 | #ifdef FEAT_GUI |
| 4800 | if (gui.in_use) |
| 4801 | { |
| 4802 | /* Show the cursor after the match, not after the redrawn text. */ |
| 4803 | setcursor(); |
| 4804 | out_flush(); |
| 4805 | gui_update_cursor(FALSE, FALSE); |
| 4806 | } |
| 4807 | #endif |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4808 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4809 | /* Delete old text to be replaced, since we're still searching and |
| 4810 | * don't want to match ourselves! */ |
| 4811 | ins_compl_delete(); |
| 4812 | } |
| 4813 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4814 | /* 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] | 4815 | * menu is visible. */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4816 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 4817 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4818 | /* |
| 4819 | * Show the file name for the match (if any) |
| 4820 | * Truncate the file name to avoid a wait for return. |
| 4821 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4822 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4823 | { |
| 4824 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4825 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | if (i <= 0) |
| 4827 | i = 0; |
| 4828 | else |
| 4829 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4830 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | msg(IObuff); |
| 4832 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 4833 | } |
| 4834 | |
| 4835 | return num_matches; |
| 4836 | } |
| 4837 | |
| 4838 | /* |
| 4839 | * Call this while finding completions, to check whether the user has hit a key |
| 4840 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4841 | * 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] | 4842 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4843 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4844 | */ |
| 4845 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4846 | ins_compl_check_keys(frequency) |
| 4847 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4848 | { |
| 4849 | static int count = 0; |
| 4850 | |
| 4851 | int c; |
| 4852 | |
| 4853 | /* Don't check when reading keys from a script. That would break the test |
| 4854 | * scripts */ |
| 4855 | if (using_script()) |
| 4856 | return; |
| 4857 | |
| 4858 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4859 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4860 | return; |
| 4861 | count = 0; |
| 4862 | |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4863 | /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 4864 | * can't do its work correctly. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4865 | c = vpeekc_any(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4866 | if (c != NUL) |
| 4867 | { |
| 4868 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 4869 | { |
| 4870 | c = safe_vgetc(); /* Eat the character */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4871 | compl_shows_dir = ins_compl_key2dir(c); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4872 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 4873 | c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4874 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4875 | else |
| 4876 | { |
| 4877 | /* 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] | 4878 | * back with vungetc() below. But skip K_IGNORE. */ |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4879 | c = safe_vgetc(); |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4880 | if (c != K_IGNORE) |
| 4881 | { |
| 4882 | /* Don't interrupt completion when the character wasn't typed, |
| 4883 | * e.g., when doing @q to replay keys. */ |
| 4884 | if (c != Ctrl_R && KeyTyped) |
| 4885 | compl_interrupted = TRUE; |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4886 | |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4887 | vungetc(c); |
| 4888 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4889 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4890 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4891 | if (compl_pending != 0 && !got_int) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4892 | { |
| 4893 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 4894 | |
| 4895 | compl_pending = 0; |
| 4896 | (void)ins_compl_next(FALSE, todo, TRUE); |
| 4897 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | /* |
| 4901 | * Decide the direction of Insert mode complete from the key typed. |
| 4902 | * Returns BACKWARD or FORWARD. |
| 4903 | */ |
| 4904 | static int |
| 4905 | ins_compl_key2dir(c) |
| 4906 | int c; |
| 4907 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4908 | if (c == Ctrl_P || c == Ctrl_L |
| 4909 | || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP |
| 4910 | || c == K_S_UP || c == K_UP))) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4911 | return BACKWARD; |
| 4912 | return FORWARD; |
| 4913 | } |
| 4914 | |
| 4915 | /* |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4916 | * Return TRUE for keys that are used for completion only when the popup menu |
| 4917 | * is visible. |
| 4918 | */ |
| 4919 | static int |
| 4920 | ins_compl_pum_key(c) |
| 4921 | int c; |
| 4922 | { |
| 4923 | 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] | 4924 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 4925 | || c == K_UP || c == K_DOWN); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | /* |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4929 | * Decide the number of completions to move forward. |
| 4930 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 4931 | */ |
| 4932 | static int |
| 4933 | ins_compl_key2count(c) |
| 4934 | int c; |
| 4935 | { |
| 4936 | int h; |
| 4937 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4938 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4939 | { |
| 4940 | h = pum_get_height(); |
| 4941 | if (h > 3) |
| 4942 | h -= 2; /* keep some context */ |
| 4943 | return h; |
| 4944 | } |
| 4945 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4949 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 4950 | * to change the currently selected completion. |
| 4951 | */ |
| 4952 | static int |
| 4953 | ins_compl_use_match(c) |
| 4954 | int c; |
| 4955 | { |
| 4956 | switch (c) |
| 4957 | { |
| 4958 | case K_UP: |
| 4959 | case K_DOWN: |
| 4960 | case K_PAGEDOWN: |
| 4961 | case K_KPAGEDOWN: |
| 4962 | case K_S_DOWN: |
| 4963 | case K_PAGEUP: |
| 4964 | case K_KPAGEUP: |
| 4965 | case K_S_UP: |
| 4966 | return FALSE; |
| 4967 | } |
| 4968 | return TRUE; |
| 4969 | } |
| 4970 | |
| 4971 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | * Do Insert mode completion. |
| 4973 | * Called when character "c" was typed, which has a meaning for completion. |
| 4974 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 4975 | */ |
| 4976 | static int |
| 4977 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4978 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4980 | char_u *line; |
| 4981 | int startcol = 0; /* column where searched text starts */ |
| 4982 | colnr_T curs_col; /* cursor column */ |
| 4983 | int n; |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 4984 | int save_w_wrow; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4985 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4986 | compl_direction = ins_compl_key2dir(c); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4987 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4988 | { |
| 4989 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 4990 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | did_ai = FALSE; |
| 4992 | #ifdef FEAT_SMARTINDENT |
| 4993 | did_si = FALSE; |
| 4994 | can_si = FALSE; |
| 4995 | can_si_back = FALSE; |
| 4996 | #endif |
| 4997 | if (stop_arrow() == FAIL) |
| 4998 | return FAIL; |
| 4999 | |
| 5000 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5001 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5002 | compl_pending = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 5004 | /* 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] | 5005 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 5006 | * instead of expand the one before the cursor, in word-wise if |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 5007 | * "compl_startpos" is not in the same line as the cursor then fix it |
| 5008 | * (the line has been split because it was longer than 'tw'). if SOL |
| 5009 | * is set then skip the previous pattern, a word at the beginning of |
| 5010 | * the line has been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 5011 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 5012 | && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5013 | { |
| 5014 | /* |
| 5015 | * it is a continued search |
| 5016 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5017 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5018 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 5019 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 5020 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5021 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5022 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5023 | /* line (probably) wrapped, set compl_startpos to the |
| 5024 | * first non_blank in the line, if it is not a wordchar |
| 5025 | * include it to get a better pattern, but then we don't |
| 5026 | * want the "\\<" prefix, check it bellow */ |
| 5027 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 5028 | compl_startpos.col = compl_col; |
| 5029 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5030 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5031 | } |
| 5032 | else |
| 5033 | { |
| 5034 | /* S_IPOS was set when we inserted a word that was at the |
| 5035 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5036 | * mode but first we need to redefine compl_startpos */ |
| 5037 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5038 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5039 | compl_cont_status |= CONT_SOL; |
| 5040 | compl_startpos.col = (colnr_T)(skipwhite( |
| 5041 | line + compl_length |
| 5042 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5043 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5044 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5045 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5046 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5047 | /* 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] | 5048 | * have enough space? just being paranoid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5049 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5050 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5051 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5052 | compl_cont_status &= ~CONT_SOL; |
| 5053 | compl_length = (IOSIZE - MIN_SPACE); |
| 5054 | compl_col = curwin->w_cursor.col - compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5055 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5056 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 5057 | if (compl_length < 1) |
| 5058 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5059 | } |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 5060 | else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5061 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5062 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5063 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5064 | } |
| 5065 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5066 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5067 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5068 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5069 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5070 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5071 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5072 | compl_cont_status = 0; |
| 5073 | compl_cont_status |= CONT_N_ADDS; |
| 5074 | compl_startpos = curwin->w_cursor; |
| 5075 | startcol = (int)curs_col; |
| 5076 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | /* Work out completion pattern and original text -- webb */ |
| 5080 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 5081 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5082 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5083 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 5084 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5085 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5086 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5087 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5088 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5089 | compl_col += ++startcol; |
| 5090 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5091 | } |
| 5092 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5093 | compl_pattern = str_foldcase(line + compl_col, |
| 5094 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5095 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5096 | compl_pattern = vim_strnsave(line + compl_col, |
| 5097 | compl_length); |
| 5098 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5099 | return FAIL; |
| 5100 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5101 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5102 | { |
| 5103 | char_u *prefix = (char_u *)"\\<"; |
| 5104 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5105 | /* we need up to 2 extra chars for the prefix */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5106 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5107 | compl_length) + 2); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5108 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5110 | if (!vim_iswordp(line + compl_col) |
| 5111 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5112 | && ( |
| 5113 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5114 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5115 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5116 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5117 | #endif |
| 5118 | ))) |
| 5119 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5120 | STRCPY((char *)compl_pattern, prefix); |
| 5121 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 5122 | line + compl_col, compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5123 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5124 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5125 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5126 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5127 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5128 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5129 | #endif |
| 5130 | ) |
| 5131 | { |
| 5132 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5133 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 5134 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5135 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5136 | compl_col += curs_col; |
| 5137 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5138 | } |
| 5139 | else |
| 5140 | { |
| 5141 | #ifdef FEAT_MBYTE |
| 5142 | /* Search the point of change class of multibyte character |
| 5143 | * or not a word single byte character backward. */ |
| 5144 | if (has_mbyte) |
| 5145 | { |
| 5146 | int base_class; |
| 5147 | int head_off; |
| 5148 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5149 | startcol -= (*mb_head_off)(line, line + startcol); |
| 5150 | base_class = mb_get_class(line + startcol); |
| 5151 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5152 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5153 | head_off = (*mb_head_off)(line, line + startcol); |
| 5154 | if (base_class != mb_get_class(line + startcol |
| 5155 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5156 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5157 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5158 | } |
| 5159 | } |
| 5160 | else |
| 5161 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5162 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5163 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5164 | compl_col += ++startcol; |
| 5165 | compl_length = (int)curs_col - startcol; |
| 5166 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5167 | { |
| 5168 | /* Only match word with at least two chars -- webb |
| 5169 | * there's no need to call quote_meta, |
| 5170 | * alloc(7) is enough -- Acevedo |
| 5171 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5172 | compl_pattern = alloc(7); |
| 5173 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5174 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5175 | STRCPY((char *)compl_pattern, "\\<"); |
| 5176 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 5177 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5178 | } |
| 5179 | else |
| 5180 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5181 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5182 | compl_length) + 2); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5183 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5184 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5185 | STRCPY((char *)compl_pattern, "\\<"); |
| 5186 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 5187 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5188 | } |
| 5189 | } |
| 5190 | } |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 5191 | else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5192 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5193 | compl_col = (colnr_T)(skipwhite(line) - line); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5194 | compl_length = (int)curs_col - (int)compl_col; |
| 5195 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 5196 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5197 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5198 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 5199 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5200 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5201 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5202 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | return FAIL; |
| 5204 | } |
| 5205 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 5206 | { |
Bram Moolenaar | 00b764a | 2013-09-05 13:50:53 +0200 | [diff] [blame] | 5207 | /* Go back to just before the first filename character. */ |
Bram Moolenaar | dd40734 | 2013-09-08 20:00:48 +0200 | [diff] [blame] | 5208 | if (startcol > 0) |
| 5209 | { |
| 5210 | char_u *p = line + startcol; |
| 5211 | |
Bram Moolenaar | 00b764a | 2013-09-05 13:50:53 +0200 | [diff] [blame] | 5212 | mb_ptr_back(line, p); |
Bram Moolenaar | dd40734 | 2013-09-08 20:00:48 +0200 | [diff] [blame] | 5213 | while (p > line && vim_isfilec(PTR2CHAR(p))) |
| 5214 | mb_ptr_back(line, p); |
| 5215 | if (p == line && vim_isfilec(PTR2CHAR(p))) |
| 5216 | startcol = 0; |
| 5217 | else |
| 5218 | startcol = (int)(p - line) + 1; |
| 5219 | } |
Bram Moolenaar | 00b764a | 2013-09-05 13:50:53 +0200 | [diff] [blame] | 5220 | |
Bram Moolenaar | 0300e46 | 2013-09-08 16:03:45 +0200 | [diff] [blame] | 5221 | compl_col += startcol; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5222 | compl_length = (int)curs_col - startcol; |
| 5223 | compl_pattern = addstar(line + compl_col, compl_length, |
| 5224 | EXPAND_FILES); |
| 5225 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5226 | return FAIL; |
| 5227 | } |
| 5228 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 5229 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5230 | compl_pattern = vim_strnsave(line, curs_col); |
| 5231 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5232 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5233 | set_cmd_context(&compl_xp, compl_pattern, |
| 5234 | (int)STRLEN(compl_pattern), curs_col); |
| 5235 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 5236 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 5237 | /* No completion possible, use an empty pattern to get a |
| 5238 | * "pattern not found" message. */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5239 | compl_col = curs_col; |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5240 | else |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 5241 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern); |
| 5242 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5243 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5244 | 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] | 5245 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5246 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5247 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5248 | * Call user defined function 'completefunc' with "a:findstart" |
| 5249 | * 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] | 5250 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5251 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5252 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5253 | char_u *funcname; |
| 5254 | pos_T pos; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5255 | win_T *curwin_save; |
| 5256 | buf_T *curbuf_save; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5257 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5258 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5259 | * string */ |
| 5260 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 5261 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 5262 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5263 | { |
| 5264 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 5265 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5266 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5267 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5268 | |
| 5269 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5270 | args[1] = NULL; |
| 5271 | pos = curwin->w_cursor; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5272 | curwin_save = curwin; |
| 5273 | curbuf_save = curbuf; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5274 | col = call_func_retnr(funcname, 2, args, FALSE); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5275 | if (curwin_save != curwin || curbuf_save != curbuf) |
| 5276 | { |
| 5277 | EMSG(_(e_complwin)); |
| 5278 | return FAIL; |
| 5279 | } |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5280 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 834def3 | 2014-09-09 18:29:33 +0200 | [diff] [blame] | 5281 | validate_cursor(); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5282 | if (!equalpos(curwin->w_cursor, pos)) |
| 5283 | { |
| 5284 | EMSG(_(e_compldel)); |
| 5285 | return FAIL; |
| 5286 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5287 | |
Bram Moolenaar | 6110a00 | 2012-01-26 18:58:38 +0100 | [diff] [blame] | 5288 | /* Return value -2 means the user complete function wants to |
Bram Moolenaar | 8a4c136 | 2012-05-18 16:35:21 +0200 | [diff] [blame] | 5289 | * cancel the complete without an error. |
| 5290 | * 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] | 5291 | if (col == -2) |
| 5292 | return FAIL; |
Bram Moolenaar | 8a4c136 | 2012-05-18 16:35:21 +0200 | [diff] [blame] | 5293 | if (col == -3) |
| 5294 | { |
| 5295 | ctrl_x_mode = 0; |
| 5296 | edit_submode = NULL; |
Bram Moolenaar | ea389e9 | 2014-05-28 21:40:52 +0200 | [diff] [blame] | 5297 | if (!shortmess(SHM_COMPLETIONMENU)) |
| 5298 | msg_clr_cmdline(); |
Bram Moolenaar | 8a4c136 | 2012-05-18 16:35:21 +0200 | [diff] [blame] | 5299 | return FAIL; |
| 5300 | } |
Bram Moolenaar | 6110a00 | 2012-01-26 18:58:38 +0100 | [diff] [blame] | 5301 | |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 5302 | /* |
| 5303 | * Reset extended parameters of completion, when start new |
| 5304 | * completion. |
| 5305 | */ |
| 5306 | compl_opt_refresh_always = FALSE; |
| 5307 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5308 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5309 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5310 | compl_col = col; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5311 | if (compl_col > curs_col) |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5312 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5313 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5314 | /* Setup variables for completion. Need to obtain "line" again, |
| 5315 | * it may have become invalid. */ |
| 5316 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5317 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5318 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5319 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5320 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5321 | return FAIL; |
| 5322 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 5323 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 5324 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 5325 | #ifdef FEAT_SPELL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 5326 | if (spell_bad_len > 0) |
| 5327 | compl_col = curs_col - spell_bad_len; |
| 5328 | else |
| 5329 | compl_col = spell_word_start(startcol); |
| 5330 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5331 | { |
| 5332 | compl_length = 0; |
| 5333 | compl_col = curs_col; |
| 5334 | } |
| 5335 | else |
| 5336 | { |
| 5337 | spell_expand_check_cap(compl_col); |
| 5338 | compl_length = (int)curs_col - compl_col; |
| 5339 | } |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 5340 | /* Need to obtain "line" again, it may have become invalid. */ |
| 5341 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 5342 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5343 | if (compl_pattern == NULL) |
| 5344 | #endif |
| 5345 | return FAIL; |
| 5346 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5347 | else |
| 5348 | { |
| 5349 | EMSG2(_(e_intern2), "ins_complete()"); |
| 5350 | return FAIL; |
| 5351 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5352 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5353 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5354 | { |
| 5355 | edit_submode_pre = (char_u *)_(" Adding"); |
Bram Moolenaar | e421450 | 2015-03-05 18:08:43 +0100 | [diff] [blame] | 5356 | if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5357 | { |
| 5358 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 5359 | #ifdef FEAT_COMMENTS |
| 5360 | char_u *old = curbuf->b_p_com; |
| 5361 | |
| 5362 | curbuf->b_p_com = (char_u *)""; |
| 5363 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5364 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5365 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5366 | ins_eol('\r'); |
| 5367 | #ifdef FEAT_COMMENTS |
| 5368 | curbuf->b_p_com = old; |
| 5369 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5370 | compl_length = 0; |
| 5371 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5372 | } |
| 5373 | } |
| 5374 | else |
| 5375 | { |
| 5376 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5377 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5378 | } |
| 5379 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5380 | if (compl_cont_status & CONT_LOCAL) |
| 5381 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5382 | else |
| 5383 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 5384 | |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 5385 | /* If any of the original typed text has been changed we need to fix |
| 5386 | * the redo buffer. */ |
| 5387 | ins_compl_fixRedoBufForLeader(NULL); |
| 5388 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5389 | /* Always add completion for the original text. */ |
| 5390 | vim_free(compl_orig_text); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5391 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 5392 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 5393 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5394 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5395 | vim_free(compl_pattern); |
| 5396 | compl_pattern = NULL; |
| 5397 | vim_free(compl_orig_text); |
| 5398 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5399 | return FAIL; |
| 5400 | } |
| 5401 | |
| 5402 | /* showmode might reset the internal line pointers, so it must |
| 5403 | * be called before line = ml_get(), or when this address is no |
| 5404 | * longer needed. -- Acevedo. |
| 5405 | */ |
| 5406 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 5407 | edit_submode_highl = HLF_COUNT; |
| 5408 | showmode(); |
| 5409 | edit_submode_extra = NULL; |
| 5410 | out_flush(); |
| 5411 | } |
| 5412 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5413 | compl_shown_match = compl_curr_match; |
| 5414 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5415 | |
| 5416 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 5417 | * Find next match (and following matches). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5418 | */ |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 5419 | save_w_wrow = curwin->w_wrow; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 5420 | 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] | 5421 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5422 | /* may undisplay the popup menu */ |
| 5423 | ins_compl_upd_pum(); |
| 5424 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5425 | if (n > 1) /* all matches have been found */ |
| 5426 | compl_matches = n; |
| 5427 | compl_curr_match = compl_shown_match; |
| 5428 | compl_direction = compl_shows_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5429 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5430 | /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 5431 | * mode. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5432 | if (got_int && !global_busy) |
| 5433 | { |
| 5434 | (void)vgetc(); |
| 5435 | got_int = FALSE; |
| 5436 | } |
| 5437 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5438 | /* 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] | 5439 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5440 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5441 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 5442 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5443 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 5444 | edit_submode_highl = HLF_E; |
| 5445 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 5446 | * because we couldn't expand anything at first place, but if we used |
| 5447 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 5448 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5449 | if ( compl_length > 1 |
| 5450 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5451 | || (ctrl_x_mode != 0 |
| 5452 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 5453 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5454 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5457 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5458 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5459 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5460 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5461 | |
| 5462 | if (edit_submode_extra == NULL) |
| 5463 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5464 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | { |
| 5466 | edit_submode_extra = (char_u *)_("Back at original"); |
| 5467 | edit_submode_highl = HLF_W; |
| 5468 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5469 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5470 | { |
| 5471 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 5472 | edit_submode_highl = HLF_COUNT; |
| 5473 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5474 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5475 | { |
| 5476 | edit_submode_extra = (char_u *)_("The only match"); |
| 5477 | edit_submode_highl = HLF_COUNT; |
| 5478 | } |
| 5479 | else |
| 5480 | { |
| 5481 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5482 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5483 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5484 | int number = 0; |
| 5485 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5486 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5487 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5488 | { |
| 5489 | /* search backwards for the first valid (!= -1) number. |
| 5490 | * This should normally succeed already at the first loop |
| 5491 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5492 | for (match = compl_curr_match->cp_prev; match != NULL |
| 5493 | && match != compl_first_match; |
| 5494 | match = match->cp_prev) |
| 5495 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5496 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5497 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5498 | break; |
| 5499 | } |
| 5500 | if (match != NULL) |
| 5501 | /* go up and assign all numbers which are not assigned |
| 5502 | * yet */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5503 | for (match = match->cp_next; |
| 5504 | match != NULL && match->cp_number == -1; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5505 | match = match->cp_next) |
| 5506 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5507 | } |
| 5508 | else /* BACKWARD */ |
| 5509 | { |
| 5510 | /* search forwards (upwards) for the first valid (!= -1) |
| 5511 | * number. This should normally succeed already at the |
| 5512 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5513 | for (match = compl_curr_match->cp_next; match != NULL |
| 5514 | && match != compl_first_match; |
| 5515 | match = match->cp_next) |
| 5516 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5517 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5518 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5519 | break; |
| 5520 | } |
| 5521 | if (match != NULL) |
| 5522 | /* go down and assign all numbers which are not |
| 5523 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5524 | for (match = match->cp_prev; match |
| 5525 | && match->cp_number == -1; |
| 5526 | match = match->cp_prev) |
| 5527 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5528 | } |
| 5529 | } |
| 5530 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5531 | /* The match should always have a sequence number now, this is |
| 5532 | * just a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5533 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5534 | { |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5535 | /* Space for 10 text chars. + 2x10-digit no.s = 31. |
| 5536 | * Translations may need more than twice that. */ |
| 5537 | static char_u match_ref[81]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5538 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5539 | if (compl_matches > 0) |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5540 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 5541 | _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5542 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5543 | else |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5544 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 5545 | _("match %d"), |
| 5546 | compl_curr_match->cp_number); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5547 | edit_submode_extra = match_ref; |
| 5548 | edit_submode_highl = HLF_R; |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 5549 | if (dollar_vcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5550 | curs_columns(FALSE); |
| 5551 | } |
| 5552 | } |
| 5553 | } |
| 5554 | |
| 5555 | /* Show a message about what (completion) mode we're in. */ |
| 5556 | showmode(); |
Bram Moolenaar | ea389e9 | 2014-05-28 21:40:52 +0200 | [diff] [blame] | 5557 | if (!shortmess(SHM_COMPLETIONMENU)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5558 | { |
Bram Moolenaar | ea389e9 | 2014-05-28 21:40:52 +0200 | [diff] [blame] | 5559 | if (edit_submode_extra != NULL) |
| 5560 | { |
| 5561 | if (!p_smd) |
| 5562 | msg_attr(edit_submode_extra, |
| 5563 | edit_submode_highl < HLF_COUNT |
| 5564 | ? hl_attr(edit_submode_highl) : 0); |
| 5565 | } |
| 5566 | else |
| 5567 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5568 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5569 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5570 | /* Show the popup menu, unless we got interrupted. */ |
| 5571 | if (!compl_interrupted) |
| 5572 | { |
| 5573 | /* RedrawingDisabled may be set when invoked through complete(). */ |
| 5574 | n = RedrawingDisabled; |
| 5575 | RedrawingDisabled = 0; |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 5576 | |
| 5577 | /* If the cursor moved we need to remove the pum first. */ |
| 5578 | setcursor(); |
| 5579 | if (save_w_wrow != curwin->w_wrow) |
| 5580 | ins_compl_del_pum(); |
| 5581 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5582 | ins_compl_show_pum(); |
| 5583 | setcursor(); |
| 5584 | RedrawingDisabled = n; |
| 5585 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 5586 | compl_was_interrupted = compl_interrupted; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5587 | compl_interrupted = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5588 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5589 | return OK; |
| 5590 | } |
| 5591 | |
| 5592 | /* |
| 5593 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 5594 | * If dest is not NULL the chars. are copied there quoting (with |
| 5595 | * a backslash) the metachars, and dest would be NUL terminated. |
| 5596 | * Returns the length (needed) of dest |
| 5597 | */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5598 | static unsigned |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5599 | quote_meta(dest, src, len) |
| 5600 | char_u *dest; |
| 5601 | char_u *src; |
| 5602 | int len; |
| 5603 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5604 | unsigned m = (unsigned)len + 1; /* one extra for the NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5605 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5606 | for ( ; --len >= 0; src++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5607 | { |
| 5608 | switch (*src) |
| 5609 | { |
| 5610 | case '.': |
| 5611 | case '*': |
| 5612 | case '[': |
| 5613 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5614 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5615 | break; |
| 5616 | case '~': |
| 5617 | if (!p_magic) /* quote these only if magic is set */ |
| 5618 | break; |
| 5619 | case '\\': |
| 5620 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5621 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5622 | break; |
| 5623 | case '^': /* currently it's not needed. */ |
| 5624 | case '$': |
| 5625 | m++; |
| 5626 | if (dest != NULL) |
| 5627 | *dest++ = '\\'; |
| 5628 | break; |
| 5629 | } |
| 5630 | if (dest != NULL) |
| 5631 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5632 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5633 | /* Copy remaining bytes of a multibyte character. */ |
| 5634 | if (has_mbyte) |
| 5635 | { |
| 5636 | int i, mb_len; |
| 5637 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5638 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5639 | if (mb_len > 0 && len >= mb_len) |
| 5640 | for (i = 0; i < mb_len; ++i) |
| 5641 | { |
| 5642 | --len; |
| 5643 | ++src; |
| 5644 | if (dest != NULL) |
| 5645 | *dest++ = *src; |
| 5646 | } |
| 5647 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5648 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5649 | } |
| 5650 | if (dest != NULL) |
| 5651 | *dest = NUL; |
| 5652 | |
| 5653 | return m; |
| 5654 | } |
| 5655 | #endif /* FEAT_INS_EXPAND */ |
| 5656 | |
| 5657 | /* |
| 5658 | * Next character is interpreted literally. |
| 5659 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 5660 | * If one or two digits are entered, the next character is given to vungetc(). |
| 5661 | * For Unicode a character > 255 may be returned. |
| 5662 | */ |
| 5663 | int |
| 5664 | get_literal() |
| 5665 | { |
| 5666 | int cc; |
| 5667 | int nc; |
| 5668 | int i; |
| 5669 | int hex = FALSE; |
| 5670 | int octal = FALSE; |
| 5671 | #ifdef FEAT_MBYTE |
| 5672 | int unicode = 0; |
| 5673 | #endif |
| 5674 | |
| 5675 | if (got_int) |
| 5676 | return Ctrl_C; |
| 5677 | |
| 5678 | #ifdef FEAT_GUI |
| 5679 | /* |
| 5680 | * In GUI there is no point inserting the internal code for a special key. |
| 5681 | * It is more useful to insert the string "<KEY>" instead. This would |
| 5682 | * probably be useful in a text window too, but it would not be |
| 5683 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 5684 | */ |
| 5685 | if (gui.in_use) |
| 5686 | ++allow_keys; |
| 5687 | #endif |
| 5688 | #ifdef USE_ON_FLY_SCROLL |
| 5689 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 5690 | #endif |
| 5691 | ++no_mapping; /* don't map the next key hits */ |
| 5692 | cc = 0; |
| 5693 | i = 0; |
| 5694 | for (;;) |
| 5695 | { |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 5696 | nc = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5697 | #ifdef FEAT_CMDL_INFO |
| 5698 | if (!(State & CMDLINE) |
| 5699 | # ifdef FEAT_MBYTE |
| 5700 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 5701 | # endif |
| 5702 | ) |
| 5703 | add_to_showcmd(nc); |
| 5704 | #endif |
| 5705 | if (nc == 'x' || nc == 'X') |
| 5706 | hex = TRUE; |
| 5707 | else if (nc == 'o' || nc == 'O') |
| 5708 | octal = TRUE; |
| 5709 | #ifdef FEAT_MBYTE |
| 5710 | else if (nc == 'u' || nc == 'U') |
| 5711 | unicode = nc; |
| 5712 | #endif |
| 5713 | else |
| 5714 | { |
| 5715 | if (hex |
| 5716 | #ifdef FEAT_MBYTE |
| 5717 | || unicode != 0 |
| 5718 | #endif |
| 5719 | ) |
| 5720 | { |
| 5721 | if (!vim_isxdigit(nc)) |
| 5722 | break; |
| 5723 | cc = cc * 16 + hex2nr(nc); |
| 5724 | } |
| 5725 | else if (octal) |
| 5726 | { |
| 5727 | if (nc < '0' || nc > '7') |
| 5728 | break; |
| 5729 | cc = cc * 8 + nc - '0'; |
| 5730 | } |
| 5731 | else |
| 5732 | { |
| 5733 | if (!VIM_ISDIGIT(nc)) |
| 5734 | break; |
| 5735 | cc = cc * 10 + nc - '0'; |
| 5736 | } |
| 5737 | |
| 5738 | ++i; |
| 5739 | } |
| 5740 | |
| 5741 | if (cc > 255 |
| 5742 | #ifdef FEAT_MBYTE |
| 5743 | && unicode == 0 |
| 5744 | #endif |
| 5745 | ) |
| 5746 | cc = 255; /* limit range to 0-255 */ |
| 5747 | nc = 0; |
| 5748 | |
| 5749 | if (hex) /* hex: up to two chars */ |
| 5750 | { |
| 5751 | if (i >= 2) |
| 5752 | break; |
| 5753 | } |
| 5754 | #ifdef FEAT_MBYTE |
| 5755 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 5756 | { |
| 5757 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 5758 | break; |
| 5759 | } |
| 5760 | #endif |
| 5761 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 5762 | break; |
| 5763 | } |
| 5764 | if (i == 0) /* no number entered */ |
| 5765 | { |
| 5766 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 5767 | { |
| 5768 | cc = '\n'; |
| 5769 | nc = 0; |
| 5770 | } |
| 5771 | else |
| 5772 | { |
| 5773 | cc = nc; |
| 5774 | nc = 0; |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | if (cc == 0) /* NUL is stored as NL */ |
| 5779 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5780 | #ifdef FEAT_MBYTE |
| 5781 | if (enc_dbcs && (cc & 0xff) == 0) |
| 5782 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 5783 | second byte will cause trouble! */ |
| 5784 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5785 | |
| 5786 | --no_mapping; |
| 5787 | #ifdef FEAT_GUI |
| 5788 | if (gui.in_use) |
| 5789 | --allow_keys; |
| 5790 | #endif |
| 5791 | if (nc) |
| 5792 | vungetc(nc); |
| 5793 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 5794 | return cc; |
| 5795 | } |
| 5796 | |
| 5797 | /* |
| 5798 | * Insert character, taking care of special keys and mod_mask |
| 5799 | */ |
| 5800 | static void |
| 5801 | insert_special(c, allow_modmask, ctrlv) |
| 5802 | int c; |
| 5803 | int allow_modmask; |
| 5804 | int ctrlv; /* c was typed after CTRL-V */ |
| 5805 | { |
| 5806 | char_u *p; |
| 5807 | int len; |
| 5808 | |
| 5809 | /* |
| 5810 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 5811 | * inserted with ins_str(), so as not to replace characters in replace |
| 5812 | * mode. |
| 5813 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 5814 | * unless 'allow_modmask' is TRUE. |
| 5815 | */ |
| 5816 | #ifdef MACOS |
| 5817 | /* Command-key never produces a normal key */ |
| 5818 | if (mod_mask & MOD_MASK_CMD) |
| 5819 | allow_modmask = TRUE; |
| 5820 | #endif |
| 5821 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 5822 | { |
| 5823 | p = get_special_key_name(c, mod_mask); |
| 5824 | len = (int)STRLEN(p); |
| 5825 | c = p[len - 1]; |
| 5826 | if (len > 2) |
| 5827 | { |
| 5828 | if (stop_arrow() == FAIL) |
| 5829 | return; |
| 5830 | p[len - 1] = NUL; |
| 5831 | ins_str(p); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5832 | AppendToRedobuffLit(p, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5833 | ctrlv = FALSE; |
| 5834 | } |
| 5835 | } |
| 5836 | if (stop_arrow() == OK) |
| 5837 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 5838 | } |
| 5839 | |
| 5840 | /* |
| 5841 | * Special characters in this context are those that need processing other |
| 5842 | * than the simple insertion that can be performed here. This includes ESC |
| 5843 | * which terminates the insert, and CR/NL which need special processing to |
| 5844 | * open up a new line. This routine tries to optimize insertions performed by |
| 5845 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 5846 | * stop and defer processing to the "normal" mechanism. |
| 5847 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 5848 | */ |
| 5849 | #ifdef EBCDIC |
| 5850 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 5851 | #else |
| 5852 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 5853 | #endif |
| 5854 | |
| 5855 | #ifdef FEAT_MBYTE |
| 5856 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 5857 | #else |
| 5858 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 5859 | #endif |
| 5860 | |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 5861 | /* |
| 5862 | * "flags": INSCHAR_FORMAT - force formatting |
| 5863 | * INSCHAR_CTRLV - char typed just after CTRL-V |
| 5864 | * INSCHAR_NO_FEX - don't use 'formatexpr' |
| 5865 | * |
| 5866 | * NOTE: passes the flags value straight through to internal_format() which, |
| 5867 | * beside INSCHAR_FORMAT (above), is also looking for these: |
| 5868 | * INSCHAR_DO_COM - format comments |
| 5869 | * INSCHAR_COM_LIST - format comments with num list or 2nd line indent |
| 5870 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5871 | void |
| 5872 | insertchar(c, flags, second_indent) |
| 5873 | int c; /* character to insert or NUL */ |
| 5874 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 5875 | int second_indent; /* indent for second line if >= 0 */ |
| 5876 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5877 | int textwidth; |
| 5878 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5879 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | int fo_ins_blank; |
Bram Moolenaar | 0f8dd84 | 2015-03-08 14:48:49 +0100 | [diff] [blame] | 5882 | int force_format = flags & INSCHAR_FORMAT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5883 | |
Bram Moolenaar | 0f8dd84 | 2015-03-08 14:48:49 +0100 | [diff] [blame] | 5884 | textwidth = comp_textwidth(force_format); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5885 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5886 | |
| 5887 | /* |
| 5888 | * Try to break the line in two or more pieces when: |
| 5889 | * - Always do this if we have been called to do formatting only. |
| 5890 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 5891 | * ends in white space. |
| 5892 | * - Otherwise: |
| 5893 | * - Don't do this if inserting a blank |
| 5894 | * - Don't do this if an existing character is being replaced, unless |
| 5895 | * we're in VREPLACE mode. |
| 5896 | * - Do this if the cursor is not on the line where insert started |
| 5897 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 5898 | * before the insert. |
| 5899 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 5900 | * before 'textwidth' |
| 5901 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5902 | if (textwidth > 0 |
Bram Moolenaar | 0f8dd84 | 2015-03-08 14:48:49 +0100 | [diff] [blame] | 5903 | && (force_format |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | || (!vim_iswhite(c) |
| 5905 | && !((State & REPLACE_FLAG) |
| 5906 | #ifdef FEAT_VREPLACE |
| 5907 | && !(State & VREPLACE_FLAG) |
| 5908 | #endif |
| 5909 | && *ml_get_cursor() != NUL) |
| 5910 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 5911 | || ((!has_format_option(FO_INS_LONG) |
| 5912 | || Insstart_textlen <= (colnr_T)textwidth) |
| 5913 | && (!fo_ins_blank |
| 5914 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 5915 | )))))) |
| 5916 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5917 | /* Format with 'formatexpr' when it's set. Use internal formatting |
| 5918 | * when 'formatexpr' isn't set or it returns non-zero. */ |
| 5919 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 0f8dd84 | 2015-03-08 14:48:49 +0100 | [diff] [blame] | 5920 | int do_internal = TRUE; |
| 5921 | colnr_T virtcol = get_nolist_virtcol() |
| 5922 | + char2cells(c != NUL ? c : gchar_cursor()); |
Bram Moolenaar | f3442e7 | 2006-10-10 13:49:10 +0000 | [diff] [blame] | 5923 | |
Bram Moolenaar | 0f8dd84 | 2015-03-08 14:48:49 +0100 | [diff] [blame] | 5924 | if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0 |
| 5925 | && (force_format || virtcol > (colnr_T)textwidth)) |
Bram Moolenaar | f3442e7 | 2006-10-10 13:49:10 +0000 | [diff] [blame] | 5926 | { |
| 5927 | do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0); |
| 5928 | /* It may be required to save for undo again, e.g. when setline() |
| 5929 | * was called. */ |
| 5930 | ins_need_undo = TRUE; |
| 5931 | } |
| 5932 | if (do_internal) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5933 | #endif |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 5934 | internal_format(textwidth, second_indent, flags, c == NUL, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5935 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5936 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5937 | if (c == NUL) /* only formatting was wanted */ |
| 5938 | return; |
| 5939 | |
| 5940 | #ifdef FEAT_COMMENTS |
| 5941 | /* Check whether this character should end a comment. */ |
| 5942 | if (did_ai && (int)c == end_comment_pending) |
| 5943 | { |
| 5944 | char_u *line; |
| 5945 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 5946 | int middle_len, end_len; |
| 5947 | int i; |
| 5948 | |
| 5949 | /* |
| 5950 | * Need to remove existing (middle) comment leader and insert end |
| 5951 | * comment leader. First, check what comment leader we can find. |
| 5952 | */ |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 5953 | i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5954 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 5955 | { |
| 5956 | /* Skip middle-comment string */ |
| 5957 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 5958 | ++p; |
| 5959 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5960 | /* Don't count trailing white space for middle_len */ |
| 5961 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 5962 | --middle_len; |
| 5963 | |
| 5964 | /* Find the end-comment string */ |
| 5965 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 5966 | ++p; |
| 5967 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5968 | |
| 5969 | /* Skip white space before the cursor */ |
| 5970 | i = curwin->w_cursor.col; |
| 5971 | while (--i >= 0 && vim_iswhite(line[i])) |
| 5972 | ; |
| 5973 | i++; |
| 5974 | |
| 5975 | /* Skip to before the middle leader */ |
| 5976 | i -= middle_len; |
| 5977 | |
| 5978 | /* Check some expected things before we go on */ |
| 5979 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 5980 | { |
| 5981 | /* Backspace over all the stuff we want to replace */ |
| 5982 | backspace_until_column(i); |
| 5983 | |
| 5984 | /* |
| 5985 | * Insert the end-comment string, except for the last |
| 5986 | * character, which will get inserted as normal later. |
| 5987 | */ |
| 5988 | ins_bytes_len(lead_end, end_len - 1); |
| 5989 | } |
| 5990 | } |
| 5991 | } |
| 5992 | end_comment_pending = NUL; |
| 5993 | #endif |
| 5994 | |
| 5995 | did_ai = FALSE; |
| 5996 | #ifdef FEAT_SMARTINDENT |
| 5997 | did_si = FALSE; |
| 5998 | can_si = FALSE; |
| 5999 | can_si_back = FALSE; |
| 6000 | #endif |
| 6001 | |
| 6002 | /* |
| 6003 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 6004 | * This speeds up normal text input considerably. |
| 6005 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 6006 | * need to re-indent at a ':', or any other character (but not what |
| 6007 | * 'paste' is set).. |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 6008 | * Don't do this when there an InsertCharPre autocommand is defined, |
| 6009 | * because we need to fire the event for every character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6010 | */ |
| 6011 | #ifdef USE_ON_FLY_SCROLL |
| 6012 | dont_scroll = FALSE; /* allow scrolling here */ |
| 6013 | #endif |
| 6014 | |
| 6015 | if ( !ISSPECIAL(c) |
| 6016 | #ifdef FEAT_MBYTE |
| 6017 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 6018 | #endif |
| 6019 | && vpeekc() != NUL |
| 6020 | && !(State & REPLACE_FLAG) |
| 6021 | #ifdef FEAT_CINDENT |
| 6022 | && !cindent_on() |
| 6023 | #endif |
| 6024 | #ifdef FEAT_RIGHTLEFT |
| 6025 | && !p_ri |
| 6026 | #endif |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 6027 | #ifdef FEAT_AUTOCMD |
| 6028 | && !has_insertcharpre() |
| 6029 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6030 | ) |
| 6031 | { |
| 6032 | #define INPUT_BUFLEN 100 |
| 6033 | char_u buf[INPUT_BUFLEN + 1]; |
| 6034 | int i; |
| 6035 | colnr_T virtcol = 0; |
| 6036 | |
| 6037 | buf[0] = c; |
| 6038 | i = 1; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6039 | if (textwidth > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6040 | virtcol = get_nolist_virtcol(); |
| 6041 | /* |
| 6042 | * Stop the string when: |
| 6043 | * - no more chars available |
| 6044 | * - finding a special character (command key) |
| 6045 | * - buffer is full |
| 6046 | * - running into the 'textwidth' boundary |
| 6047 | * - need to check for abbreviation: A non-word char after a word-char |
| 6048 | */ |
| 6049 | while ( (c = vpeekc()) != NUL |
| 6050 | && !ISSPECIAL(c) |
| 6051 | #ifdef FEAT_MBYTE |
| 6052 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 6053 | #endif |
| 6054 | && i < INPUT_BUFLEN |
| 6055 | && (textwidth == 0 |
| 6056 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 6057 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 6058 | { |
| 6059 | #ifdef FEAT_RIGHTLEFT |
| 6060 | c = vgetc(); |
| 6061 | if (p_hkmap && KeyTyped) |
| 6062 | c = hkmap(c); /* Hebrew mode mapping */ |
| 6063 | # ifdef FEAT_FKMAP |
| 6064 | if (p_fkmap && KeyTyped) |
| 6065 | c = fkmap(c); /* Farsi mode mapping */ |
| 6066 | # endif |
| 6067 | buf[i++] = c; |
| 6068 | #else |
| 6069 | buf[i++] = vgetc(); |
| 6070 | #endif |
| 6071 | } |
| 6072 | |
| 6073 | #ifdef FEAT_DIGRAPHS |
| 6074 | do_digraph(-1); /* clear digraphs */ |
| 6075 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 6076 | #endif |
| 6077 | buf[i] = NUL; |
| 6078 | ins_str(buf); |
| 6079 | if (flags & INSCHAR_CTRLV) |
| 6080 | { |
| 6081 | redo_literal(*buf); |
| 6082 | i = 1; |
| 6083 | } |
| 6084 | else |
| 6085 | i = 0; |
| 6086 | if (buf[i] != NUL) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 6087 | AppendToRedobuffLit(buf + i, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6088 | } |
| 6089 | else |
| 6090 | { |
| 6091 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6092 | int cc; |
| 6093 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6094 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 6095 | { |
| 6096 | char_u buf[MB_MAXBYTES + 1]; |
| 6097 | |
| 6098 | (*mb_char2bytes)(c, buf); |
| 6099 | buf[cc] = NUL; |
| 6100 | ins_char_bytes(buf, cc); |
| 6101 | AppendCharToRedobuff(c); |
| 6102 | } |
| 6103 | else |
| 6104 | #endif |
| 6105 | { |
| 6106 | ins_char(c); |
| 6107 | if (flags & INSCHAR_CTRLV) |
| 6108 | redo_literal(c); |
| 6109 | else |
| 6110 | AppendCharToRedobuff(c); |
| 6111 | } |
| 6112 | } |
| 6113 | } |
| 6114 | |
| 6115 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6116 | * Format text at the current insert position. |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6117 | * |
| 6118 | * If the INSCHAR_COM_LIST flag is present, then the value of second_indent |
| 6119 | * will be the comment leader length sent to open_line(). |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6120 | */ |
| 6121 | static void |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6122 | internal_format(textwidth, second_indent, flags, format_only, c) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6123 | int textwidth; |
| 6124 | int second_indent; |
| 6125 | int flags; |
| 6126 | int format_only; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6127 | int c; /* character to be inserted (can be NUL) */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6128 | { |
| 6129 | int cc; |
| 6130 | int save_char = NUL; |
| 6131 | int haveto_redraw = FALSE; |
| 6132 | int fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 6133 | #ifdef FEAT_MBYTE |
| 6134 | int fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 6135 | #endif |
| 6136 | int fo_white_par = has_format_option(FO_WHITE_PAR); |
| 6137 | int first_line = TRUE; |
| 6138 | #ifdef FEAT_COMMENTS |
| 6139 | colnr_T leader_len; |
| 6140 | int no_leader = FALSE; |
| 6141 | int do_comments = (flags & INSCHAR_DO_COM); |
| 6142 | #endif |
Bram Moolenaar | 0026d47 | 2014-09-09 16:32:39 +0200 | [diff] [blame] | 6143 | #ifdef FEAT_LINEBREAK |
| 6144 | int has_lbr = curwin->w_p_lbr; |
| 6145 | |
| 6146 | /* make sure win_lbr_chartabsize() counts correctly */ |
| 6147 | curwin->w_p_lbr = FALSE; |
| 6148 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6149 | |
| 6150 | /* |
| 6151 | * When 'ai' is off we don't want a space under the cursor to be |
| 6152 | * deleted. Replace it with an 'x' temporarily. |
| 6153 | */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6154 | if (!curbuf->b_p_ai |
| 6155 | #ifdef FEAT_VREPLACE |
| 6156 | && !(State & VREPLACE_FLAG) |
| 6157 | #endif |
| 6158 | ) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6159 | { |
| 6160 | cc = gchar_cursor(); |
| 6161 | if (vim_iswhite(cc)) |
| 6162 | { |
| 6163 | save_char = cc; |
| 6164 | pchar_cursor('x'); |
| 6165 | } |
| 6166 | } |
| 6167 | |
| 6168 | /* |
| 6169 | * Repeat breaking lines, until the current line is not too long. |
| 6170 | */ |
| 6171 | while (!got_int) |
| 6172 | { |
| 6173 | int startcol; /* Cursor column at entry */ |
| 6174 | int wantcol; /* column at textwidth border */ |
| 6175 | int foundcol; /* column for start of spaces */ |
| 6176 | int end_foundcol = 0; /* column for start of word */ |
| 6177 | colnr_T len; |
| 6178 | colnr_T virtcol; |
| 6179 | #ifdef FEAT_VREPLACE |
| 6180 | int orig_col = 0; |
| 6181 | char_u *saved_text = NULL; |
| 6182 | #endif |
| 6183 | colnr_T col; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6184 | colnr_T end_col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6185 | |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6186 | virtcol = get_nolist_virtcol() |
| 6187 | + char2cells(c != NUL ? c : gchar_cursor()); |
| 6188 | if (virtcol <= (colnr_T)textwidth) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6189 | break; |
| 6190 | |
| 6191 | #ifdef FEAT_COMMENTS |
| 6192 | if (no_leader) |
| 6193 | do_comments = FALSE; |
| 6194 | else if (!(flags & INSCHAR_FORMAT) |
| 6195 | && has_format_option(FO_WRAP_COMS)) |
| 6196 | do_comments = TRUE; |
| 6197 | |
| 6198 | /* Don't break until after the comment leader */ |
| 6199 | if (do_comments) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 6200 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6201 | else |
| 6202 | leader_len = 0; |
| 6203 | |
| 6204 | /* If the line doesn't start with a comment leader, then don't |
| 6205 | * start one in a following broken line. Avoids that a %word |
| 6206 | * moved to the start of the next line causes all following lines |
| 6207 | * to start with %. */ |
| 6208 | if (leader_len == 0) |
| 6209 | no_leader = TRUE; |
| 6210 | #endif |
| 6211 | if (!(flags & INSCHAR_FORMAT) |
| 6212 | #ifdef FEAT_COMMENTS |
| 6213 | && leader_len == 0 |
| 6214 | #endif |
| 6215 | && !has_format_option(FO_WRAP)) |
| 6216 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6217 | break; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6218 | if ((startcol = curwin->w_cursor.col) == 0) |
| 6219 | break; |
| 6220 | |
| 6221 | /* find column of textwidth border */ |
| 6222 | coladvance((colnr_T)textwidth); |
| 6223 | wantcol = curwin->w_cursor.col; |
| 6224 | |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6225 | curwin->w_cursor.col = startcol; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6226 | foundcol = 0; |
| 6227 | |
| 6228 | /* |
| 6229 | * Find position to break at. |
| 6230 | * Stop at first entered white when 'formatoptions' has 'v' |
| 6231 | */ |
| 6232 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
Bram Moolenaar | a27ad5a | 2011-10-26 17:04:29 +0200 | [diff] [blame] | 6233 | || (flags & INSCHAR_FORMAT) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6234 | || curwin->w_cursor.lnum != Insstart.lnum |
| 6235 | || curwin->w_cursor.col >= Insstart.col) |
| 6236 | { |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6237 | if (curwin->w_cursor.col == startcol && c != NUL) |
| 6238 | cc = c; |
| 6239 | else |
| 6240 | cc = gchar_cursor(); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6241 | if (WHITECHAR(cc)) |
| 6242 | { |
| 6243 | /* remember position of blank just before text */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6244 | end_col = curwin->w_cursor.col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6245 | |
| 6246 | /* find start of sequence of blanks */ |
| 6247 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 6248 | { |
| 6249 | dec_cursor(); |
| 6250 | cc = gchar_cursor(); |
| 6251 | } |
| 6252 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 6253 | break; /* only spaces in front of text */ |
| 6254 | #ifdef FEAT_COMMENTS |
| 6255 | /* Don't break until after the comment leader */ |
| 6256 | if (curwin->w_cursor.col < leader_len) |
| 6257 | break; |
| 6258 | #endif |
| 6259 | if (has_format_option(FO_ONE_LETTER)) |
| 6260 | { |
| 6261 | /* do not break after one-letter words */ |
| 6262 | if (curwin->w_cursor.col == 0) |
| 6263 | break; /* one-letter word at begin */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6264 | #ifdef FEAT_COMMENTS |
| 6265 | /* do not break "#a b" when 'tw' is 2 */ |
| 6266 | if (curwin->w_cursor.col <= leader_len) |
| 6267 | break; |
| 6268 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6269 | col = curwin->w_cursor.col; |
| 6270 | dec_cursor(); |
| 6271 | cc = gchar_cursor(); |
| 6272 | |
| 6273 | if (WHITECHAR(cc)) |
| 6274 | continue; /* one-letter, continue */ |
| 6275 | curwin->w_cursor.col = col; |
| 6276 | } |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6277 | |
| 6278 | inc_cursor(); |
| 6279 | |
| 6280 | end_foundcol = end_col + 1; |
| 6281 | foundcol = curwin->w_cursor.col; |
| 6282 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6283 | break; |
| 6284 | } |
| 6285 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6286 | else if (cc >= 0x100 && fo_multibyte) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6287 | { |
| 6288 | /* Break after or before a multi-byte character. */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6289 | if (curwin->w_cursor.col != startcol) |
| 6290 | { |
| 6291 | #ifdef FEAT_COMMENTS |
| 6292 | /* Don't break until after the comment leader */ |
| 6293 | if (curwin->w_cursor.col < leader_len) |
| 6294 | break; |
| 6295 | #endif |
| 6296 | col = curwin->w_cursor.col; |
| 6297 | inc_cursor(); |
| 6298 | /* Don't change end_foundcol if already set. */ |
| 6299 | if (foundcol != curwin->w_cursor.col) |
| 6300 | { |
| 6301 | foundcol = curwin->w_cursor.col; |
| 6302 | end_foundcol = foundcol; |
| 6303 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
| 6304 | break; |
| 6305 | } |
| 6306 | curwin->w_cursor.col = col; |
| 6307 | } |
| 6308 | |
| 6309 | if (curwin->w_cursor.col == 0) |
| 6310 | break; |
| 6311 | |
| 6312 | col = curwin->w_cursor.col; |
| 6313 | |
| 6314 | dec_cursor(); |
| 6315 | cc = gchar_cursor(); |
| 6316 | |
| 6317 | if (WHITECHAR(cc)) |
| 6318 | continue; /* break with space */ |
| 6319 | #ifdef FEAT_COMMENTS |
| 6320 | /* Don't break until after the comment leader */ |
| 6321 | if (curwin->w_cursor.col < leader_len) |
| 6322 | break; |
| 6323 | #endif |
| 6324 | |
| 6325 | curwin->w_cursor.col = col; |
| 6326 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6327 | foundcol = curwin->w_cursor.col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6328 | end_foundcol = foundcol; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6329 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
| 6330 | break; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6331 | } |
| 6332 | #endif |
| 6333 | if (curwin->w_cursor.col == 0) |
| 6334 | break; |
| 6335 | dec_cursor(); |
| 6336 | } |
| 6337 | |
| 6338 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 6339 | { |
| 6340 | curwin->w_cursor.col = startcol; |
| 6341 | break; |
| 6342 | } |
| 6343 | |
| 6344 | /* Going to break the line, remove any "$" now. */ |
| 6345 | undisplay_dollar(); |
| 6346 | |
| 6347 | /* |
| 6348 | * Offset between cursor position and line break is used by replace |
| 6349 | * stack functions. VREPLACE does not use this, and backspaces |
| 6350 | * over the text instead. |
| 6351 | */ |
| 6352 | #ifdef FEAT_VREPLACE |
| 6353 | if (State & VREPLACE_FLAG) |
| 6354 | orig_col = startcol; /* Will start backspacing from here */ |
| 6355 | else |
| 6356 | #endif |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6357 | replace_offset = startcol - end_foundcol; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6358 | |
| 6359 | /* |
| 6360 | * adjust startcol for spaces that will be deleted and |
| 6361 | * characters that will remain on top line |
| 6362 | */ |
| 6363 | curwin->w_cursor.col = foundcol; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6364 | while ((cc = gchar_cursor(), WHITECHAR(cc)) |
| 6365 | && (!fo_white_par || curwin->w_cursor.col < startcol)) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6366 | inc_cursor(); |
| 6367 | startcol -= curwin->w_cursor.col; |
| 6368 | if (startcol < 0) |
| 6369 | startcol = 0; |
| 6370 | |
| 6371 | #ifdef FEAT_VREPLACE |
| 6372 | if (State & VREPLACE_FLAG) |
| 6373 | { |
| 6374 | /* |
| 6375 | * In VREPLACE mode, we will backspace over the text to be |
| 6376 | * wrapped, so save a copy now to put on the next line. |
| 6377 | */ |
| 6378 | saved_text = vim_strsave(ml_get_cursor()); |
| 6379 | curwin->w_cursor.col = orig_col; |
| 6380 | if (saved_text == NULL) |
| 6381 | break; /* Can't do it, out of memory */ |
| 6382 | saved_text[startcol] = NUL; |
| 6383 | |
| 6384 | /* Backspace over characters that will move to the next line */ |
| 6385 | if (!fo_white_par) |
| 6386 | backspace_until_column(foundcol); |
| 6387 | } |
| 6388 | else |
| 6389 | #endif |
| 6390 | { |
| 6391 | /* put cursor after pos. to break line */ |
| 6392 | if (!fo_white_par) |
| 6393 | curwin->w_cursor.col = foundcol; |
| 6394 | } |
| 6395 | |
| 6396 | /* |
| 6397 | * Split the line just before the margin. |
| 6398 | * Only insert/delete lines, but don't really redraw the window. |
| 6399 | */ |
| 6400 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 6401 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 6402 | #ifdef FEAT_COMMENTS |
| 6403 | + (do_comments ? OPENLINE_DO_COM : 0) |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6404 | + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6405 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6406 | , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent)); |
| 6407 | if (!(flags & INSCHAR_COM_LIST)) |
| 6408 | old_indent = 0; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6409 | |
| 6410 | replace_offset = 0; |
| 6411 | if (first_line) |
| 6412 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6413 | if (!(flags & INSCHAR_COM_LIST)) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6414 | { |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6415 | /* |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 6416 | * This section is for auto-wrap of numeric lists. When not |
| 6417 | * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST |
| 6418 | * flag will be set and open_line() will handle it (as seen |
| 6419 | * above). The code here (and in get_number_indent()) will |
| 6420 | * recognize comments if needed... |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6421 | */ |
| 6422 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 6423 | second_indent = |
| 6424 | get_number_indent(curwin->w_cursor.lnum - 1); |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6425 | if (second_indent >= 0) |
| 6426 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6427 | #ifdef FEAT_VREPLACE |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6428 | if (State & VREPLACE_FLAG) |
| 6429 | change_indent(INDENT_SET, second_indent, |
| 6430 | FALSE, NUL, TRUE); |
| 6431 | else |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6432 | #endif |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 6433 | #ifdef FEAT_COMMENTS |
| 6434 | if (leader_len > 0 && second_indent - leader_len > 0) |
| 6435 | { |
| 6436 | int i; |
| 6437 | int padding = second_indent - leader_len; |
| 6438 | |
| 6439 | /* We started at the first_line of a numbered list |
| 6440 | * that has a comment. the open_line() function has |
| 6441 | * inserted the proper comment leader and positioned |
| 6442 | * the cursor at the end of the split line. Now we |
| 6443 | * add the additional whitespace needed after the |
| 6444 | * comment leader for the numbered list. */ |
| 6445 | for (i = 0; i < padding; i++) |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 6446 | ins_str((char_u *)" "); |
Bram Moolenaar | 5967abb | 2012-07-06 13:36:48 +0200 | [diff] [blame] | 6447 | changed_bytes(curwin->w_cursor.lnum, leader_len); |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 6448 | } |
| 6449 | else |
| 6450 | { |
| 6451 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6452 | (void)set_indent(second_indent, SIN_CHANGED); |
Bram Moolenaar | 96b7ca5 | 2012-06-29 15:04:49 +0200 | [diff] [blame] | 6453 | #ifdef FEAT_COMMENTS |
| 6454 | } |
| 6455 | #endif |
Bram Moolenaar | bfe3bf8 | 2012-06-13 17:28:55 +0200 | [diff] [blame] | 6456 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6457 | } |
| 6458 | first_line = FALSE; |
| 6459 | } |
| 6460 | |
| 6461 | #ifdef FEAT_VREPLACE |
| 6462 | if (State & VREPLACE_FLAG) |
| 6463 | { |
| 6464 | /* |
| 6465 | * In VREPLACE mode we have backspaced over the text to be |
| 6466 | * moved, now we re-insert it into the new line. |
| 6467 | */ |
| 6468 | ins_bytes(saved_text); |
| 6469 | vim_free(saved_text); |
| 6470 | } |
| 6471 | else |
| 6472 | #endif |
| 6473 | { |
| 6474 | /* |
| 6475 | * Check if cursor is not past the NUL off the line, cindent |
| 6476 | * may have added or removed indent. |
| 6477 | */ |
| 6478 | curwin->w_cursor.col += startcol; |
| 6479 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 6480 | if (curwin->w_cursor.col > len) |
| 6481 | curwin->w_cursor.col = len; |
| 6482 | } |
| 6483 | |
| 6484 | haveto_redraw = TRUE; |
| 6485 | #ifdef FEAT_CINDENT |
| 6486 | can_cindent = TRUE; |
| 6487 | #endif |
| 6488 | /* moved the cursor, don't autoindent or cindent now */ |
| 6489 | did_ai = FALSE; |
| 6490 | #ifdef FEAT_SMARTINDENT |
| 6491 | did_si = FALSE; |
| 6492 | can_si = FALSE; |
| 6493 | can_si_back = FALSE; |
| 6494 | #endif |
| 6495 | line_breakcheck(); |
| 6496 | } |
| 6497 | |
| 6498 | if (save_char != NUL) /* put back space after cursor */ |
| 6499 | pchar_cursor(save_char); |
| 6500 | |
Bram Moolenaar | 0026d47 | 2014-09-09 16:32:39 +0200 | [diff] [blame] | 6501 | #ifdef FEAT_LINEBREAK |
| 6502 | curwin->w_p_lbr = has_lbr; |
| 6503 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6504 | if (!format_only && haveto_redraw) |
| 6505 | { |
| 6506 | update_topline(); |
| 6507 | redraw_curbuf_later(VALID); |
| 6508 | } |
| 6509 | } |
| 6510 | |
| 6511 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6512 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 6513 | * 'a' flag format from the current line until the end of the paragraph. |
| 6514 | * Keep the cursor at the same position relative to the text. |
| 6515 | * The caller must have saved the cursor line for undo, following ones will be |
| 6516 | * saved here. |
| 6517 | */ |
| 6518 | void |
| 6519 | auto_format(trailblank, prev_line) |
| 6520 | int trailblank; /* when TRUE also format with trailing blank */ |
| 6521 | int prev_line; /* may start in previous line */ |
| 6522 | { |
| 6523 | pos_T pos; |
| 6524 | colnr_T len; |
| 6525 | char_u *old; |
| 6526 | char_u *new, *pnew; |
| 6527 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6528 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6529 | |
| 6530 | if (!has_format_option(FO_AUTO)) |
| 6531 | return; |
| 6532 | |
| 6533 | pos = curwin->w_cursor; |
| 6534 | old = ml_get_curline(); |
| 6535 | |
| 6536 | /* may remove added space */ |
| 6537 | check_auto_format(FALSE); |
| 6538 | |
| 6539 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 6540 | * user might insert normal text next. Also skip formatting when "1" is |
| 6541 | * in 'formatoptions' and there is a single character before the cursor. |
| 6542 | * Otherwise the line would be broken and when typing another non-white |
| 6543 | * next they are not joined back together. */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6544 | wasatend = (pos.col == (colnr_T)STRLEN(old)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6545 | if (*old != NUL && !trailblank && wasatend) |
| 6546 | { |
| 6547 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6548 | cc = gchar_cursor(); |
| 6549 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 6550 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6551 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6552 | cc = gchar_cursor(); |
| 6553 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6554 | { |
| 6555 | curwin->w_cursor = pos; |
| 6556 | return; |
| 6557 | } |
| 6558 | curwin->w_cursor = pos; |
| 6559 | } |
| 6560 | |
| 6561 | #ifdef FEAT_COMMENTS |
| 6562 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 6563 | * comments. */ |
| 6564 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
Bram Moolenaar | 8134039 | 2012-06-06 16:12:59 +0200 | [diff] [blame] | 6565 | && get_leader_len(old, NULL, FALSE, TRUE) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | return; |
| 6567 | #endif |
| 6568 | |
| 6569 | /* |
| 6570 | * May start formatting in a previous line, so that after "x" a word is |
| 6571 | * moved to the previous line if it fits there now. Only when this is not |
| 6572 | * the start of a paragraph. |
| 6573 | */ |
| 6574 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 6575 | { |
| 6576 | --curwin->w_cursor.lnum; |
| 6577 | if (u_save_cursor() == FAIL) |
| 6578 | return; |
| 6579 | } |
| 6580 | |
| 6581 | /* |
| 6582 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 6583 | * be adjusted for the text formatting. |
| 6584 | */ |
| 6585 | saved_cursor = pos; |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 6586 | format_lines((linenr_T)-1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6587 | curwin->w_cursor = saved_cursor; |
| 6588 | saved_cursor.lnum = 0; |
| 6589 | |
| 6590 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 6591 | { |
| 6592 | /* "cannot happen" */ |
| 6593 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6594 | coladvance((colnr_T)MAXCOL); |
| 6595 | } |
| 6596 | else |
| 6597 | check_cursor_col(); |
| 6598 | |
| 6599 | /* Insert mode: If the cursor is now after the end of the line while it |
| 6600 | * previously wasn't, the line was broken. Because of the rule above we |
| 6601 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 6602 | * formatted. */ |
| 6603 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 6604 | { |
| 6605 | new = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6606 | len = (colnr_T)STRLEN(new); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6607 | if (curwin->w_cursor.col == len) |
| 6608 | { |
| 6609 | pnew = vim_strnsave(new, len + 2); |
| 6610 | pnew[len] = ' '; |
| 6611 | pnew[len + 1] = NUL; |
| 6612 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 6613 | /* remove the space later */ |
| 6614 | did_add_space = TRUE; |
| 6615 | } |
| 6616 | else |
| 6617 | /* may remove added space */ |
| 6618 | check_auto_format(FALSE); |
| 6619 | } |
| 6620 | |
| 6621 | check_cursor(); |
| 6622 | } |
| 6623 | |
| 6624 | /* |
| 6625 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 6626 | * delete it now. The space must be under the cursor, just after the insert |
| 6627 | * position. |
| 6628 | */ |
| 6629 | static void |
| 6630 | check_auto_format(end_insert) |
| 6631 | int end_insert; /* TRUE when ending Insert mode */ |
| 6632 | { |
| 6633 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6634 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6635 | |
| 6636 | if (did_add_space) |
| 6637 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6638 | cc = gchar_cursor(); |
| 6639 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6640 | /* Somehow the space was removed already. */ |
| 6641 | did_add_space = FALSE; |
| 6642 | else |
| 6643 | { |
| 6644 | if (!end_insert) |
| 6645 | { |
| 6646 | inc_cursor(); |
| 6647 | c = gchar_cursor(); |
| 6648 | dec_cursor(); |
| 6649 | } |
| 6650 | if (c != NUL) |
| 6651 | { |
| 6652 | /* The space is no longer at the end of the line, delete it. */ |
| 6653 | del_char(FALSE); |
| 6654 | did_add_space = FALSE; |
| 6655 | } |
| 6656 | } |
| 6657 | } |
| 6658 | } |
| 6659 | |
| 6660 | /* |
| 6661 | * Find out textwidth to be used for formatting: |
| 6662 | * if 'textwidth' option is set, use it |
| 6663 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 6664 | * if invalid value, use 0. |
| 6665 | * Set default to window width (maximum 79) for "gq" operator. |
| 6666 | */ |
| 6667 | int |
| 6668 | comp_textwidth(ff) |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 6669 | int ff; /* force formatting (for "gq" command) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6670 | { |
| 6671 | int textwidth; |
| 6672 | |
| 6673 | textwidth = curbuf->b_p_tw; |
| 6674 | if (textwidth == 0 && curbuf->b_p_wm) |
| 6675 | { |
| 6676 | /* The width is the window width minus 'wrapmargin' minus all the |
| 6677 | * things that add to the margin. */ |
| 6678 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 6679 | #ifdef FEAT_CMDWIN |
| 6680 | if (cmdwin_type != 0) |
| 6681 | textwidth -= 1; |
| 6682 | #endif |
| 6683 | #ifdef FEAT_FOLDING |
| 6684 | textwidth -= curwin->w_p_fdc; |
| 6685 | #endif |
| 6686 | #ifdef FEAT_SIGNS |
| 6687 | if (curwin->w_buffer->b_signlist != NULL |
| 6688 | # ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 6689 | || netbeans_active() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6690 | # endif |
| 6691 | ) |
| 6692 | textwidth -= 1; |
| 6693 | #endif |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 6694 | if (curwin->w_p_nu || curwin->w_p_rnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6695 | textwidth -= 8; |
| 6696 | } |
| 6697 | if (textwidth < 0) |
| 6698 | textwidth = 0; |
| 6699 | if (ff && textwidth == 0) |
| 6700 | { |
| 6701 | textwidth = W_WIDTH(curwin) - 1; |
| 6702 | if (textwidth > 79) |
| 6703 | textwidth = 79; |
| 6704 | } |
| 6705 | return textwidth; |
| 6706 | } |
| 6707 | |
| 6708 | /* |
| 6709 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 6710 | */ |
| 6711 | static void |
| 6712 | redo_literal(c) |
| 6713 | int c; |
| 6714 | { |
| 6715 | char_u buf[10]; |
| 6716 | |
| 6717 | /* Only digits need special treatment. Translate them into a string of |
| 6718 | * three digits. */ |
| 6719 | if (VIM_ISDIGIT(c)) |
| 6720 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6721 | vim_snprintf((char *)buf, sizeof(buf), "%03d", c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6722 | AppendToRedobuff(buf); |
| 6723 | } |
| 6724 | else |
| 6725 | AppendCharToRedobuff(c); |
| 6726 | } |
| 6727 | |
| 6728 | /* |
| 6729 | * 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] | 6730 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6731 | */ |
| 6732 | static void |
| 6733 | start_arrow(end_insert_pos) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6734 | pos_T *end_insert_pos; /* can be NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6735 | { |
| 6736 | if (!arrow_used) /* something has been inserted */ |
| 6737 | { |
| 6738 | AppendToRedobuff(ESC_STR); |
Bram Moolenaar | f332a65 | 2013-11-04 04:20:33 +0100 | [diff] [blame] | 6739 | stop_insert(end_insert_pos, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 6741 | } |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6742 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6743 | check_spell_redraw(); |
| 6744 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6745 | } |
| 6746 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6747 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6748 | /* |
| 6749 | * If we skipped highlighting word at cursor, do it now. |
| 6750 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 6751 | */ |
| 6752 | static void |
| 6753 | check_spell_redraw() |
| 6754 | { |
| 6755 | if (spell_redraw_lnum != 0) |
| 6756 | { |
| 6757 | linenr_T lnum = spell_redraw_lnum; |
| 6758 | |
| 6759 | spell_redraw_lnum = 0; |
| 6760 | redrawWinline(lnum, FALSE); |
| 6761 | } |
| 6762 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6763 | |
| 6764 | /* |
| 6765 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6766 | * spelled word, if there is one. |
| 6767 | */ |
| 6768 | static void |
| 6769 | spell_back_to_badword() |
| 6770 | { |
| 6771 | pos_T tpos = curwin->w_cursor; |
| 6772 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 6773 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6774 | if (curwin->w_cursor.col != tpos.col) |
| 6775 | start_arrow(&tpos); |
| 6776 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6777 | #endif |
| 6778 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6779 | /* |
| 6780 | * stop_arrow() is called before a change is made in insert mode. |
| 6781 | * If an arrow key has been used, start a new insertion. |
| 6782 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 6783 | */ |
| 6784 | int |
| 6785 | stop_arrow() |
| 6786 | { |
| 6787 | if (arrow_used) |
| 6788 | { |
Bram Moolenaar | 1fc7e97 | 2014-08-16 18:13:03 +0200 | [diff] [blame] | 6789 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
| 6790 | if (Insstart.col > Insstart_orig.col && !ins_need_undo) |
| 6791 | /* Don't update the original insert position when moved to the |
| 6792 | * right, except when nothing was inserted yet. */ |
| 6793 | update_Insstart_orig = FALSE; |
| 6794 | Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
| 6795 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6796 | if (u_save_cursor() == OK) |
| 6797 | { |
| 6798 | arrow_used = FALSE; |
| 6799 | ins_need_undo = FALSE; |
| 6800 | } |
Bram Moolenaar | 1fc7e97 | 2014-08-16 18:13:03 +0200 | [diff] [blame] | 6801 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6802 | ai_col = 0; |
| 6803 | #ifdef FEAT_VREPLACE |
| 6804 | if (State & VREPLACE_FLAG) |
| 6805 | { |
| 6806 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 6807 | vr_lines_changed = 1; |
| 6808 | } |
| 6809 | #endif |
| 6810 | ResetRedobuff(); |
| 6811 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 6812 | new_insert_skip = 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6813 | } |
| 6814 | else if (ins_need_undo) |
| 6815 | { |
| 6816 | if (u_save_cursor() == OK) |
| 6817 | ins_need_undo = FALSE; |
| 6818 | } |
| 6819 | |
| 6820 | #ifdef FEAT_FOLDING |
| 6821 | /* Always open fold at the cursor line when inserting something. */ |
| 6822 | foldOpenCursor(); |
| 6823 | #endif |
| 6824 | |
| 6825 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 6826 | } |
| 6827 | |
| 6828 | /* |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6829 | * Do a few things to stop inserting. |
| 6830 | * "end_insert_pos" is where insert ended. It is NULL when we already jumped |
| 6831 | * to another window/buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6832 | */ |
| 6833 | static void |
Bram Moolenaar | f332a65 | 2013-11-04 04:20:33 +0100 | [diff] [blame] | 6834 | stop_insert(end_insert_pos, esc, nomove) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6835 | pos_T *end_insert_pos; |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6836 | int esc; /* called by ins_esc() */ |
Bram Moolenaar | f332a65 | 2013-11-04 04:20:33 +0100 | [diff] [blame] | 6837 | int nomove; /* <c-\><c-o>, don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6838 | { |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6839 | int cc; |
| 6840 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6841 | |
| 6842 | stop_redo_ins(); |
| 6843 | replace_flush(); /* abandon replace stack */ |
| 6844 | |
| 6845 | /* |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6846 | * Save the inserted text for later redo with ^@ and CTRL-A. |
| 6847 | * Don't do it when "restart_edit" was set and nothing was inserted, |
| 6848 | * otherwise CTRL-O w and then <Left> will clear "last_insert". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6849 | */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6850 | ptr = get_inserted(); |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 6851 | if (did_restart_edit == 0 || (ptr != NULL |
| 6852 | && (int)STRLEN(ptr) > new_insert_skip)) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6853 | { |
| 6854 | vim_free(last_insert); |
| 6855 | last_insert = ptr; |
| 6856 | last_insert_skip = new_insert_skip; |
| 6857 | } |
| 6858 | else |
| 6859 | vim_free(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6860 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6861 | if (!arrow_used && end_insert_pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6862 | { |
| 6863 | /* Auto-format now. It may seem strange to do this when stopping an |
| 6864 | * insertion (or moving the cursor), but it's required when appending |
| 6865 | * a line and having it end in a space. But only do it when something |
| 6866 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6867 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6868 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6869 | pos_T tpos = curwin->w_cursor; |
| 6870 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6871 | /* When the cursor is at the end of the line after a space the |
| 6872 | * formatting will move it to the following word. Avoid that by |
| 6873 | * moving the cursor onto the space. */ |
| 6874 | cc = 'x'; |
| 6875 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 6876 | { |
| 6877 | dec_cursor(); |
| 6878 | cc = gchar_cursor(); |
| 6879 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6880 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6881 | } |
| 6882 | |
| 6883 | auto_format(TRUE, FALSE); |
| 6884 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6885 | if (vim_iswhite(cc)) |
| 6886 | { |
| 6887 | if (gchar_cursor() != NUL) |
| 6888 | inc_cursor(); |
| 6889 | #ifdef FEAT_VIRTUALEDIT |
| 6890 | /* If the cursor is still at the same character, also keep |
| 6891 | * the "coladd". */ |
| 6892 | if (gchar_cursor() == NUL |
| 6893 | && curwin->w_cursor.lnum == tpos.lnum |
| 6894 | && curwin->w_cursor.col == tpos.col) |
| 6895 | curwin->w_cursor.coladd = tpos.coladd; |
| 6896 | #endif |
| 6897 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | } |
| 6899 | |
| 6900 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 6901 | check_auto_format(TRUE); |
| 6902 | |
| 6903 | /* 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] | 6904 | * of the line, and put the cursor back. |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6905 | * Do this when ESC was used or moving the cursor up/down. |
| 6906 | * Check for the old position still being valid, just in case the text |
| 6907 | * got changed unexpectedly. */ |
Bram Moolenaar | f332a65 | 2013-11-04 04:20:33 +0100 | [diff] [blame] | 6908 | if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6909 | && curwin->w_cursor.lnum != end_insert_pos->lnum)) |
| 6910 | && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6911 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6912 | pos_T tpos = curwin->w_cursor; |
| 6913 | |
| 6914 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6915 | check_cursor_col(); /* make sure it is not past the line */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6916 | for (;;) |
| 6917 | { |
| 6918 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 6919 | --curwin->w_cursor.col; |
| 6920 | cc = gchar_cursor(); |
| 6921 | if (!vim_iswhite(cc)) |
| 6922 | break; |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6923 | if (del_char(TRUE) == FAIL) |
| 6924 | break; /* should not happen */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6925 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6926 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 6927 | curwin->w_cursor = tpos; |
Bram Moolenaar | 2f31e39 | 2014-10-31 19:20:36 +0100 | [diff] [blame] | 6928 | else |
| 6929 | { |
Bram Moolenaar | ef6875b | 2014-11-12 18:59:25 +0100 | [diff] [blame] | 6930 | /* reset tpos, could have been invalidated in the loop above */ |
| 6931 | tpos = curwin->w_cursor; |
Bram Moolenaar | 2f31e39 | 2014-10-31 19:20:36 +0100 | [diff] [blame] | 6932 | tpos.col++; |
| 6933 | if (cc != NUL && gchar_pos(&tpos) == NUL) |
| 6934 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 6935 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6936 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 6938 | * deleted characters. */ |
| 6939 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 6940 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6941 | int len = (int)STRLEN(ml_get_curline()); |
| 6942 | |
| 6943 | if (VIsual.col > len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6944 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6945 | VIsual.col = len; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 6946 | #ifdef FEAT_VIRTUALEDIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6947 | VIsual.coladd = 0; |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 6948 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6949 | } |
| 6950 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6951 | } |
| 6952 | } |
| 6953 | did_ai = FALSE; |
| 6954 | #ifdef FEAT_SMARTINDENT |
| 6955 | did_si = FALSE; |
| 6956 | can_si = FALSE; |
| 6957 | can_si_back = FALSE; |
| 6958 | #endif |
| 6959 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6960 | /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are |
| 6961 | * now in a different buffer. */ |
| 6962 | if (end_insert_pos != NULL) |
| 6963 | { |
| 6964 | curbuf->b_op_start = Insstart; |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 6965 | curbuf->b_op_start_orig = Insstart_orig; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6966 | curbuf->b_op_end = *end_insert_pos; |
| 6967 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6968 | } |
| 6969 | |
| 6970 | /* |
| 6971 | * Set the last inserted text to a single character. |
| 6972 | * Used for the replace command. |
| 6973 | */ |
| 6974 | void |
| 6975 | set_last_insert(c) |
| 6976 | int c; |
| 6977 | { |
| 6978 | char_u *s; |
| 6979 | |
| 6980 | vim_free(last_insert); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6981 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6982 | if (last_insert != NULL) |
| 6983 | { |
| 6984 | s = last_insert; |
| 6985 | /* Use the CTRL-V only when entering a special char */ |
| 6986 | if (c < ' ' || c == DEL) |
| 6987 | *s++ = Ctrl_V; |
| 6988 | s = add_char2buf(c, s); |
| 6989 | *s++ = ESC; |
| 6990 | *s++ = NUL; |
| 6991 | last_insert_skip = 0; |
| 6992 | } |
| 6993 | } |
| 6994 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6995 | #if defined(EXITFREE) || defined(PROTO) |
| 6996 | void |
| 6997 | free_last_insert() |
| 6998 | { |
| 6999 | vim_free(last_insert); |
| 7000 | last_insert = NULL; |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 7001 | # ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 7002 | vim_free(compl_orig_text); |
| 7003 | compl_orig_text = NULL; |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 7004 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 7005 | } |
| 7006 | #endif |
| 7007 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7008 | /* |
| 7009 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 7010 | * and CSI. Handle multi-byte characters. |
| 7011 | * Returns a pointer to after the added bytes. |
| 7012 | */ |
| 7013 | char_u * |
| 7014 | add_char2buf(c, s) |
| 7015 | int c; |
| 7016 | char_u *s; |
| 7017 | { |
| 7018 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 7019 | char_u temp[MB_MAXBYTES + 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7020 | int i; |
| 7021 | int len; |
| 7022 | |
| 7023 | len = (*mb_char2bytes)(c, temp); |
| 7024 | for (i = 0; i < len; ++i) |
| 7025 | { |
| 7026 | c = temp[i]; |
| 7027 | #endif |
| 7028 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 7029 | if (c == K_SPECIAL) |
| 7030 | { |
| 7031 | *s++ = K_SPECIAL; |
| 7032 | *s++ = KS_SPECIAL; |
| 7033 | *s++ = KE_FILLER; |
| 7034 | } |
| 7035 | #ifdef FEAT_GUI |
| 7036 | else if (c == CSI) |
| 7037 | { |
| 7038 | *s++ = CSI; |
| 7039 | *s++ = KS_EXTRA; |
| 7040 | *s++ = (int)KE_CSI; |
| 7041 | } |
| 7042 | #endif |
| 7043 | else |
| 7044 | *s++ = c; |
| 7045 | #ifdef FEAT_MBYTE |
| 7046 | } |
| 7047 | #endif |
| 7048 | return s; |
| 7049 | } |
| 7050 | |
| 7051 | /* |
| 7052 | * move cursor to start of line |
| 7053 | * if flags & BL_WHITE move to first non-white |
| 7054 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 7055 | * otherwise keep "curswant" column |
| 7056 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 7057 | */ |
| 7058 | void |
| 7059 | beginline(flags) |
| 7060 | int flags; |
| 7061 | { |
| 7062 | if ((flags & BL_SOL) && !p_sol) |
| 7063 | coladvance(curwin->w_curswant); |
| 7064 | else |
| 7065 | { |
| 7066 | curwin->w_cursor.col = 0; |
| 7067 | #ifdef FEAT_VIRTUALEDIT |
| 7068 | curwin->w_cursor.coladd = 0; |
| 7069 | #endif |
| 7070 | |
| 7071 | if (flags & (BL_WHITE | BL_SOL)) |
| 7072 | { |
| 7073 | char_u *ptr; |
| 7074 | |
| 7075 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 7076 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 7077 | ++curwin->w_cursor.col; |
| 7078 | } |
| 7079 | curwin->w_set_curswant = TRUE; |
| 7080 | } |
| 7081 | } |
| 7082 | |
| 7083 | /* |
| 7084 | * oneright oneleft cursor_down cursor_up |
| 7085 | * |
| 7086 | * Move one char {right,left,down,up}. |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7087 | * 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] | 7088 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 7089 | */ |
| 7090 | |
| 7091 | int |
| 7092 | oneright() |
| 7093 | { |
| 7094 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7095 | int l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7096 | |
| 7097 | #ifdef FEAT_VIRTUALEDIT |
| 7098 | if (virtual_active()) |
| 7099 | { |
| 7100 | pos_T prevpos = curwin->w_cursor; |
| 7101 | |
| 7102 | /* Adjust for multi-wide char (excluding TAB) */ |
| 7103 | ptr = ml_get_cursor(); |
| 7104 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7105 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7106 | (*mb_ptr2char)(ptr) |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7107 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7108 | *ptr |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7109 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7110 | )) |
| 7111 | ? ptr2cells(ptr) : 1)); |
| 7112 | curwin->w_set_curswant = TRUE; |
| 7113 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 7114 | return (prevpos.col != curwin->w_cursor.col |
| 7115 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 7116 | } |
| 7117 | #endif |
| 7118 | |
| 7119 | ptr = ml_get_cursor(); |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7120 | if (*ptr == NUL) |
| 7121 | return FAIL; /* already at the very end */ |
| 7122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7123 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7124 | if (has_mbyte) |
| 7125 | l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7126 | else |
| 7127 | #endif |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 7128 | l = 1; |
| 7129 | |
| 7130 | /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' |
| 7131 | * contains "onemore". */ |
| 7132 | if (ptr[l] == NUL |
| 7133 | #ifdef FEAT_VIRTUALEDIT |
| 7134 | && (ve_flags & VE_ONEMORE) == 0 |
| 7135 | #endif |
| 7136 | ) |
| 7137 | return FAIL; |
| 7138 | curwin->w_cursor.col += l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7139 | |
| 7140 | curwin->w_set_curswant = TRUE; |
| 7141 | return OK; |
| 7142 | } |
| 7143 | |
| 7144 | int |
| 7145 | oneleft() |
| 7146 | { |
| 7147 | #ifdef FEAT_VIRTUALEDIT |
| 7148 | if (virtual_active()) |
| 7149 | { |
| 7150 | int width; |
| 7151 | int v = getviscol(); |
| 7152 | |
| 7153 | if (v == 0) |
| 7154 | return FAIL; |
| 7155 | |
| 7156 | # ifdef FEAT_LINEBREAK |
| 7157 | /* We might get stuck on 'showbreak', skip over it. */ |
| 7158 | width = 1; |
| 7159 | for (;;) |
| 7160 | { |
| 7161 | coladvance(v - width); |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 7162 | /* getviscol() is slow, skip it when 'showbreak' is empty, |
| 7163 | * 'breakindent' is not set and there are no multi-byte |
| 7164 | * characters */ |
| 7165 | if ((*p_sbr == NUL && !curwin->w_p_bri |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7166 | # ifdef FEAT_MBYTE |
| 7167 | && !has_mbyte |
| 7168 | # endif |
| 7169 | ) || getviscol() < v) |
| 7170 | break; |
| 7171 | ++width; |
| 7172 | } |
| 7173 | # else |
| 7174 | coladvance(v - 1); |
| 7175 | # endif |
| 7176 | |
| 7177 | if (curwin->w_cursor.coladd == 1) |
| 7178 | { |
| 7179 | char_u *ptr; |
| 7180 | |
| 7181 | /* Adjust for multi-wide char (not a TAB) */ |
| 7182 | ptr = ml_get_cursor(); |
| 7183 | if (*ptr != TAB && vim_isprintc( |
| 7184 | # ifdef FEAT_MBYTE |
| 7185 | (*mb_ptr2char)(ptr) |
| 7186 | # else |
| 7187 | *ptr |
| 7188 | # endif |
| 7189 | ) && ptr2cells(ptr) > 1) |
| 7190 | curwin->w_cursor.coladd = 0; |
| 7191 | } |
| 7192 | |
| 7193 | curwin->w_set_curswant = TRUE; |
| 7194 | return OK; |
| 7195 | } |
| 7196 | #endif |
| 7197 | |
| 7198 | if (curwin->w_cursor.col == 0) |
| 7199 | return FAIL; |
| 7200 | |
| 7201 | curwin->w_set_curswant = TRUE; |
| 7202 | --curwin->w_cursor.col; |
| 7203 | |
| 7204 | #ifdef FEAT_MBYTE |
| 7205 | /* if the character on the left of the current cursor is a multi-byte |
| 7206 | * character, move to its first byte */ |
| 7207 | if (has_mbyte) |
| 7208 | mb_adjust_cursor(); |
| 7209 | #endif |
| 7210 | return OK; |
| 7211 | } |
| 7212 | |
| 7213 | int |
| 7214 | cursor_up(n, upd_topline) |
| 7215 | long n; |
| 7216 | int upd_topline; /* When TRUE: update topline */ |
| 7217 | { |
| 7218 | linenr_T lnum; |
| 7219 | |
| 7220 | if (n > 0) |
| 7221 | { |
| 7222 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7223 | /* This fails if the cursor is already in the first line or the count |
| 7224 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 7225 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7226 | return FAIL; |
| 7227 | if (n >= lnum) |
| 7228 | lnum = 1; |
| 7229 | else |
| 7230 | #ifdef FEAT_FOLDING |
| 7231 | if (hasAnyFolding(curwin)) |
| 7232 | { |
| 7233 | /* |
| 7234 | * Count each sequence of folded lines as one logical line. |
| 7235 | */ |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 7236 | /* go to the start of the current fold */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7237 | (void)hasFolding(lnum, &lnum, NULL); |
| 7238 | |
| 7239 | while (n--) |
| 7240 | { |
| 7241 | /* move up one line */ |
| 7242 | --lnum; |
| 7243 | if (lnum <= 1) |
| 7244 | break; |
| 7245 | /* If we entered a fold, move to the beginning, unless in |
| 7246 | * Insert mode or when 'foldopen' contains "all": it will open |
| 7247 | * in a moment. */ |
| 7248 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 7249 | (void)hasFolding(lnum, &lnum, NULL); |
| 7250 | } |
| 7251 | if (lnum < 1) |
| 7252 | lnum = 1; |
| 7253 | } |
| 7254 | else |
| 7255 | #endif |
| 7256 | lnum -= n; |
| 7257 | curwin->w_cursor.lnum = lnum; |
| 7258 | } |
| 7259 | |
| 7260 | /* try to advance to the column we want to be at */ |
| 7261 | coladvance(curwin->w_curswant); |
| 7262 | |
| 7263 | if (upd_topline) |
| 7264 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 7265 | |
| 7266 | return OK; |
| 7267 | } |
| 7268 | |
| 7269 | /* |
| 7270 | * Cursor down a number of logical lines. |
| 7271 | */ |
| 7272 | int |
| 7273 | cursor_down(n, upd_topline) |
| 7274 | long n; |
| 7275 | int upd_topline; /* When TRUE: update topline */ |
| 7276 | { |
| 7277 | linenr_T lnum; |
| 7278 | |
| 7279 | if (n > 0) |
| 7280 | { |
| 7281 | lnum = curwin->w_cursor.lnum; |
| 7282 | #ifdef FEAT_FOLDING |
| 7283 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 7284 | (void)hasFolding(lnum, NULL, &lnum); |
| 7285 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7286 | /* This fails if the cursor is already in the last line or would move |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 7287 | * beyond the last line and '-' is in 'cpoptions' */ |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7288 | if (lnum >= curbuf->b_ml.ml_line_count |
| 7289 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 7290 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7291 | return FAIL; |
| 7292 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 7293 | lnum = curbuf->b_ml.ml_line_count; |
| 7294 | else |
| 7295 | #ifdef FEAT_FOLDING |
| 7296 | if (hasAnyFolding(curwin)) |
| 7297 | { |
| 7298 | linenr_T last; |
| 7299 | |
| 7300 | /* count each sequence of folded lines as one logical line */ |
| 7301 | while (n--) |
| 7302 | { |
| 7303 | if (hasFolding(lnum, NULL, &last)) |
| 7304 | lnum = last + 1; |
| 7305 | else |
| 7306 | ++lnum; |
| 7307 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 7308 | break; |
| 7309 | } |
| 7310 | if (lnum > curbuf->b_ml.ml_line_count) |
| 7311 | lnum = curbuf->b_ml.ml_line_count; |
| 7312 | } |
| 7313 | else |
| 7314 | #endif |
| 7315 | lnum += n; |
| 7316 | curwin->w_cursor.lnum = lnum; |
| 7317 | } |
| 7318 | |
| 7319 | /* try to advance to the column we want to be at */ |
| 7320 | coladvance(curwin->w_curswant); |
| 7321 | |
| 7322 | if (upd_topline) |
| 7323 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 7324 | |
| 7325 | return OK; |
| 7326 | } |
| 7327 | |
| 7328 | /* |
| 7329 | * Stuff the last inserted text in the read buffer. |
| 7330 | * Last_insert actually is a copy of the redo buffer, so we |
| 7331 | * first have to remove the command. |
| 7332 | */ |
| 7333 | int |
| 7334 | stuff_inserted(c, count, no_esc) |
| 7335 | int c; /* Command character to be inserted */ |
| 7336 | long count; /* Repeat this many times */ |
| 7337 | int no_esc; /* Don't add an ESC at the end */ |
| 7338 | { |
| 7339 | char_u *esc_ptr; |
| 7340 | char_u *ptr; |
| 7341 | char_u *last_ptr; |
| 7342 | char_u last = NUL; |
| 7343 | |
| 7344 | ptr = get_last_insert(); |
| 7345 | if (ptr == NULL) |
| 7346 | { |
| 7347 | EMSG(_(e_noinstext)); |
| 7348 | return FAIL; |
| 7349 | } |
| 7350 | |
| 7351 | /* may want to stuff the command character, to start Insert mode */ |
| 7352 | if (c != NUL) |
| 7353 | stuffcharReadbuff(c); |
| 7354 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 7355 | *esc_ptr = NUL; /* remove the ESC */ |
| 7356 | |
| 7357 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 7358 | * comes after it OR if it will inserted more than once and "ptr" |
| 7359 | * starts with ^D. -- Acevedo |
| 7360 | */ |
| 7361 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 7362 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 7363 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 7364 | { |
| 7365 | last = *last_ptr; |
| 7366 | *last_ptr = NUL; |
| 7367 | } |
| 7368 | |
| 7369 | do |
| 7370 | { |
| 7371 | stuffReadbuff(ptr); |
| 7372 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 7373 | if (last) |
| 7374 | stuffReadbuff((char_u *)(last == '0' |
| 7375 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 7376 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 7377 | } |
| 7378 | while (--count > 0); |
| 7379 | |
| 7380 | if (last) |
| 7381 | *last_ptr = last; |
| 7382 | |
| 7383 | if (esc_ptr != NULL) |
| 7384 | *esc_ptr = ESC; /* put the ESC back */ |
| 7385 | |
| 7386 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 7387 | if (!no_esc) |
| 7388 | stuffcharReadbuff(ESC); |
| 7389 | |
| 7390 | return OK; |
| 7391 | } |
| 7392 | |
| 7393 | char_u * |
| 7394 | get_last_insert() |
| 7395 | { |
| 7396 | if (last_insert == NULL) |
| 7397 | return NULL; |
| 7398 | return last_insert + last_insert_skip; |
| 7399 | } |
| 7400 | |
| 7401 | /* |
| 7402 | * Get last inserted string, and remove trailing <Esc>. |
| 7403 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 7404 | */ |
| 7405 | char_u * |
| 7406 | get_last_insert_save() |
| 7407 | { |
| 7408 | char_u *s; |
| 7409 | int len; |
| 7410 | |
| 7411 | if (last_insert == NULL) |
| 7412 | return NULL; |
| 7413 | s = vim_strsave(last_insert + last_insert_skip); |
| 7414 | if (s != NULL) |
| 7415 | { |
| 7416 | len = (int)STRLEN(s); |
| 7417 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 7418 | s[len - 1] = NUL; |
| 7419 | } |
| 7420 | return s; |
| 7421 | } |
| 7422 | |
| 7423 | /* |
| 7424 | * Check the word in front of the cursor for an abbreviation. |
| 7425 | * Called when the non-id character "c" has been entered. |
| 7426 | * When an abbreviation is recognized it is removed from the text and |
| 7427 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 7428 | */ |
| 7429 | static int |
| 7430 | echeck_abbr(c) |
| 7431 | int c; |
| 7432 | { |
| 7433 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 7434 | * after moving around with cursor keys. */ |
| 7435 | if (p_paste || no_abbr || arrow_used) |
| 7436 | return FALSE; |
| 7437 | |
| 7438 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 7439 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 7440 | } |
| 7441 | |
| 7442 | /* |
| 7443 | * replace-stack functions |
| 7444 | * |
| 7445 | * When replacing characters, the replaced characters are remembered for each |
| 7446 | * new character. This is used to re-insert the old text when backspacing. |
| 7447 | * |
| 7448 | * There is a NUL headed list of characters for each character that is |
| 7449 | * currently in the file after the insertion point. When BS is used, one NUL |
| 7450 | * headed list is put back for the deleted character. |
| 7451 | * |
| 7452 | * For a newline, there are two NUL headed lists. One contains the characters |
| 7453 | * that the NL replaced. The extra one stores the characters after the cursor |
| 7454 | * that were deleted (always white space). |
| 7455 | * |
| 7456 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 7457 | * character at the end of the stack. If replace_offset is not 0, that many |
| 7458 | * characters will be left on the stack above the newly inserted character. |
| 7459 | */ |
| 7460 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 7461 | static char_u *replace_stack = NULL; |
| 7462 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 7463 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7464 | |
| 7465 | void |
| 7466 | replace_push(c) |
| 7467 | int c; /* character that is replaced (NUL is none) */ |
| 7468 | { |
| 7469 | char_u *p; |
| 7470 | |
| 7471 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 7472 | return; |
| 7473 | if (replace_stack_len <= replace_stack_nr) |
| 7474 | { |
| 7475 | replace_stack_len += 50; |
| 7476 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 7477 | if (p == NULL) /* out of memory */ |
| 7478 | { |
| 7479 | replace_stack_len -= 50; |
| 7480 | return; |
| 7481 | } |
| 7482 | if (replace_stack != NULL) |
| 7483 | { |
| 7484 | mch_memmove(p, replace_stack, |
| 7485 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 7486 | vim_free(replace_stack); |
| 7487 | } |
| 7488 | replace_stack = p; |
| 7489 | } |
| 7490 | p = replace_stack + replace_stack_nr - replace_offset; |
| 7491 | if (replace_offset) |
| 7492 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 7493 | *p = c; |
| 7494 | ++replace_stack_nr; |
| 7495 | } |
| 7496 | |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 7497 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 7498 | /* |
| 7499 | * Push a character onto the replace stack. Handles a multi-byte character in |
| 7500 | * reverse byte order, so that the first byte is popped off first. |
| 7501 | * Return the number of bytes done (includes composing characters). |
| 7502 | */ |
| 7503 | int |
| 7504 | replace_push_mb(p) |
| 7505 | char_u *p; |
| 7506 | { |
| 7507 | int l = (*mb_ptr2len)(p); |
| 7508 | int j; |
| 7509 | |
| 7510 | for (j = l - 1; j >= 0; --j) |
| 7511 | replace_push(p[j]); |
| 7512 | return l; |
| 7513 | } |
| 7514 | #endif |
| 7515 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7516 | /* |
| 7517 | * Pop one item from the replace stack. |
| 7518 | * return -1 if stack empty |
| 7519 | * return replaced character or NUL otherwise |
| 7520 | */ |
| 7521 | static int |
| 7522 | replace_pop() |
| 7523 | { |
| 7524 | if (replace_stack_nr == 0) |
| 7525 | return -1; |
| 7526 | return (int)replace_stack[--replace_stack_nr]; |
| 7527 | } |
| 7528 | |
| 7529 | /* |
| 7530 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 7531 | * encountered. |
| 7532 | */ |
| 7533 | static void |
| 7534 | replace_join(off) |
| 7535 | int off; /* offset for which NUL to remove */ |
| 7536 | { |
| 7537 | int i; |
| 7538 | |
| 7539 | for (i = replace_stack_nr; --i >= 0; ) |
| 7540 | if (replace_stack[i] == NUL && off-- <= 0) |
| 7541 | { |
| 7542 | --replace_stack_nr; |
| 7543 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 7544 | (size_t)(replace_stack_nr - i)); |
| 7545 | return; |
| 7546 | } |
| 7547 | } |
| 7548 | |
| 7549 | /* |
| 7550 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 7551 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 7552 | */ |
| 7553 | static void |
| 7554 | replace_pop_ins() |
| 7555 | { |
| 7556 | int cc; |
| 7557 | int oldState = State; |
| 7558 | |
| 7559 | State = NORMAL; /* don't want REPLACE here */ |
| 7560 | while ((cc = replace_pop()) > 0) |
| 7561 | { |
| 7562 | #ifdef FEAT_MBYTE |
| 7563 | mb_replace_pop_ins(cc); |
| 7564 | #else |
| 7565 | ins_char(cc); |
| 7566 | #endif |
| 7567 | dec_cursor(); |
| 7568 | } |
| 7569 | State = oldState; |
| 7570 | } |
| 7571 | |
| 7572 | #ifdef FEAT_MBYTE |
| 7573 | /* |
| 7574 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 7575 | * indicates a multi-byte char, pop the other bytes too. |
| 7576 | */ |
| 7577 | static void |
| 7578 | mb_replace_pop_ins(cc) |
| 7579 | int cc; |
| 7580 | { |
| 7581 | int n; |
Bram Moolenaar | 9a920d8 | 2012-06-01 15:21:02 +0200 | [diff] [blame] | 7582 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7583 | int i; |
| 7584 | int c; |
| 7585 | |
| 7586 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 7587 | { |
| 7588 | buf[0] = cc; |
| 7589 | for (i = 1; i < n; ++i) |
| 7590 | buf[i] = replace_pop(); |
| 7591 | ins_bytes_len(buf, n); |
| 7592 | } |
| 7593 | else |
| 7594 | ins_char(cc); |
| 7595 | |
| 7596 | if (enc_utf8) |
| 7597 | /* Handle composing chars. */ |
| 7598 | for (;;) |
| 7599 | { |
| 7600 | c = replace_pop(); |
| 7601 | if (c == -1) /* stack empty */ |
| 7602 | break; |
| 7603 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 7604 | { |
| 7605 | /* Not a multi-byte char, put it back. */ |
| 7606 | replace_push(c); |
| 7607 | break; |
| 7608 | } |
| 7609 | else |
| 7610 | { |
| 7611 | buf[0] = c; |
| 7612 | for (i = 1; i < n; ++i) |
| 7613 | buf[i] = replace_pop(); |
| 7614 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 7615 | ins_bytes_len(buf, n); |
| 7616 | else |
| 7617 | { |
| 7618 | /* Not a composing char, put it back. */ |
| 7619 | for (i = n - 1; i >= 0; --i) |
| 7620 | replace_push(buf[i]); |
| 7621 | break; |
| 7622 | } |
| 7623 | } |
| 7624 | } |
| 7625 | } |
| 7626 | #endif |
| 7627 | |
| 7628 | /* |
| 7629 | * make the replace stack empty |
| 7630 | * (called when exiting replace mode) |
| 7631 | */ |
| 7632 | static void |
| 7633 | replace_flush() |
| 7634 | { |
| 7635 | vim_free(replace_stack); |
| 7636 | replace_stack = NULL; |
| 7637 | replace_stack_len = 0; |
| 7638 | replace_stack_nr = 0; |
| 7639 | } |
| 7640 | |
| 7641 | /* |
| 7642 | * Handle doing a BS for one character. |
| 7643 | * cc < 0: replace stack empty, just move cursor |
| 7644 | * cc == 0: character was inserted, delete it |
| 7645 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 7646 | * and check for more characters to be put back |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7647 | * When "limit_col" is >= 0, don't delete before this column. Matters when |
| 7648 | * using composing characters, use del_char_after_col() instead of del_char(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7649 | */ |
| 7650 | static void |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7651 | replace_do_bs(limit_col) |
| 7652 | int limit_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7653 | { |
| 7654 | int cc; |
| 7655 | #ifdef FEAT_VREPLACE |
| 7656 | int orig_len = 0; |
| 7657 | int ins_len; |
| 7658 | int orig_vcols = 0; |
| 7659 | colnr_T start_vcol; |
| 7660 | char_u *p; |
| 7661 | int i; |
| 7662 | int vcol; |
| 7663 | #endif |
| 7664 | |
| 7665 | cc = replace_pop(); |
| 7666 | if (cc > 0) |
| 7667 | { |
| 7668 | #ifdef FEAT_VREPLACE |
| 7669 | if (State & VREPLACE_FLAG) |
| 7670 | { |
| 7671 | /* Get the number of screen cells used by the character we are |
| 7672 | * going to delete. */ |
| 7673 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 7674 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 7675 | } |
| 7676 | #endif |
| 7677 | #ifdef FEAT_MBYTE |
| 7678 | if (has_mbyte) |
| 7679 | { |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7680 | (void)del_char_after_col(limit_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7681 | # ifdef FEAT_VREPLACE |
| 7682 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7683 | orig_len = (int)STRLEN(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7684 | # endif |
| 7685 | replace_push(cc); |
| 7686 | } |
| 7687 | else |
| 7688 | #endif |
| 7689 | { |
| 7690 | pchar_cursor(cc); |
| 7691 | #ifdef FEAT_VREPLACE |
| 7692 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7693 | orig_len = (int)STRLEN(ml_get_cursor()) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7694 | #endif |
| 7695 | } |
| 7696 | replace_pop_ins(); |
| 7697 | |
| 7698 | #ifdef FEAT_VREPLACE |
| 7699 | if (State & VREPLACE_FLAG) |
| 7700 | { |
| 7701 | /* Get the number of screen cells used by the inserted characters */ |
| 7702 | p = ml_get_cursor(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7703 | ins_len = (int)STRLEN(p) - orig_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7704 | vcol = start_vcol; |
| 7705 | for (i = 0; i < ins_len; ++i) |
| 7706 | { |
| 7707 | vcol += chartabsize(p + i, vcol); |
| 7708 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7709 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7710 | #endif |
| 7711 | } |
| 7712 | vcol -= start_vcol; |
| 7713 | |
| 7714 | /* Delete spaces that were inserted after the cursor to keep the |
| 7715 | * text aligned. */ |
| 7716 | curwin->w_cursor.col += ins_len; |
| 7717 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 7718 | { |
| 7719 | del_char(FALSE); |
| 7720 | ++orig_vcols; |
| 7721 | } |
| 7722 | curwin->w_cursor.col -= ins_len; |
| 7723 | } |
| 7724 | #endif |
| 7725 | |
| 7726 | /* mark the buffer as changed and prepare for displaying */ |
| 7727 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 7728 | } |
| 7729 | else if (cc == 0) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7730 | (void)del_char_after_col(limit_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7731 | } |
| 7732 | |
| 7733 | #ifdef FEAT_CINDENT |
| 7734 | /* |
| 7735 | * Return TRUE if C-indenting is on. |
| 7736 | */ |
| 7737 | static int |
| 7738 | cindent_on() |
| 7739 | { |
| 7740 | return (!p_paste && (curbuf->b_p_cin |
| 7741 | # ifdef FEAT_EVAL |
| 7742 | || *curbuf->b_p_inde != NUL |
| 7743 | # endif |
| 7744 | )); |
| 7745 | } |
| 7746 | #endif |
| 7747 | |
| 7748 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 7749 | /* |
| 7750 | * Re-indent the current line, based on the current contents of it and the |
| 7751 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 7752 | * confused what all the part that handles Control-T is doing that I'm not. |
| 7753 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 7754 | */ |
| 7755 | |
| 7756 | void |
| 7757 | fixthisline(get_the_indent) |
| 7758 | int (*get_the_indent) __ARGS((void)); |
| 7759 | { |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 7760 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7761 | if (linewhite(curwin->w_cursor.lnum)) |
| 7762 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 7763 | } |
| 7764 | |
| 7765 | void |
| 7766 | fix_indent() |
| 7767 | { |
| 7768 | if (p_paste) |
| 7769 | return; |
| 7770 | # ifdef FEAT_LISP |
| 7771 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 7772 | fixthisline(get_lisp_indent); |
| 7773 | # endif |
| 7774 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 7775 | else |
| 7776 | # endif |
| 7777 | # ifdef FEAT_CINDENT |
| 7778 | if (cindent_on()) |
| 7779 | do_c_expr_indent(); |
| 7780 | # endif |
| 7781 | } |
| 7782 | |
| 7783 | #endif |
| 7784 | |
| 7785 | #ifdef FEAT_CINDENT |
| 7786 | /* |
| 7787 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 7788 | * when == '*': Only if key is preceded with '*' (indent before insert) |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 7789 | * when == '!': Only if key is preceded with '!' (don't insert) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7790 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 7791 | * |
| 7792 | * "keytyped" can have a few special values: |
| 7793 | * KEY_OPEN_FORW |
| 7794 | * KEY_OPEN_BACK |
| 7795 | * KEY_COMPLETE just finished completion. |
| 7796 | * |
| 7797 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 7798 | */ |
| 7799 | int |
| 7800 | in_cinkeys(keytyped, when, line_is_empty) |
| 7801 | int keytyped; |
| 7802 | int when; |
| 7803 | int line_is_empty; |
| 7804 | { |
| 7805 | char_u *look; |
| 7806 | int try_match; |
| 7807 | int try_match_word; |
| 7808 | char_u *p; |
| 7809 | char_u *line; |
| 7810 | int icase; |
| 7811 | int i; |
| 7812 | |
Bram Moolenaar | 3084894 | 2009-12-24 14:46:12 +0000 | [diff] [blame] | 7813 | if (keytyped == NUL) |
| 7814 | /* Can happen with CTRL-Y and CTRL-E on a short line. */ |
| 7815 | return FALSE; |
| 7816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7817 | #ifdef FEAT_EVAL |
| 7818 | if (*curbuf->b_p_inde != NUL) |
| 7819 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 7820 | else |
| 7821 | #endif |
| 7822 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 7823 | while (*look) |
| 7824 | { |
| 7825 | /* |
| 7826 | * Find out if we want to try a match with this key, depending on |
| 7827 | * 'when' and a '*' or '!' before the key. |
| 7828 | */ |
| 7829 | switch (when) |
| 7830 | { |
| 7831 | case '*': try_match = (*look == '*'); break; |
| 7832 | case '!': try_match = (*look == '!'); break; |
| 7833 | default: try_match = (*look != '*'); break; |
| 7834 | } |
| 7835 | if (*look == '*' || *look == '!') |
| 7836 | ++look; |
| 7837 | |
| 7838 | /* |
| 7839 | * If there is a '0', only accept a match if the line is empty. |
| 7840 | * But may still match when typing last char of a word. |
| 7841 | */ |
| 7842 | if (*look == '0') |
| 7843 | { |
| 7844 | try_match_word = try_match; |
| 7845 | if (!line_is_empty) |
| 7846 | try_match = FALSE; |
| 7847 | ++look; |
| 7848 | } |
| 7849 | else |
| 7850 | try_match_word = FALSE; |
| 7851 | |
| 7852 | /* |
| 7853 | * does it look like a control character? |
| 7854 | */ |
| 7855 | if (*look == '^' |
| 7856 | #ifdef EBCDIC |
| 7857 | && (Ctrl_chr(look[1]) != 0) |
| 7858 | #else |
| 7859 | && look[1] >= '?' && look[1] <= '_' |
| 7860 | #endif |
| 7861 | ) |
| 7862 | { |
| 7863 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 7864 | return TRUE; |
| 7865 | look += 2; |
| 7866 | } |
| 7867 | /* |
| 7868 | * 'o' means "o" command, open forward. |
| 7869 | * 'O' means "O" command, open backward. |
| 7870 | */ |
| 7871 | else if (*look == 'o') |
| 7872 | { |
| 7873 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 7874 | return TRUE; |
| 7875 | ++look; |
| 7876 | } |
| 7877 | else if (*look == 'O') |
| 7878 | { |
| 7879 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 7880 | return TRUE; |
| 7881 | ++look; |
| 7882 | } |
| 7883 | |
| 7884 | /* |
| 7885 | * 'e' means to check for "else" at start of line and just before the |
| 7886 | * cursor. |
| 7887 | */ |
| 7888 | else if (*look == 'e') |
| 7889 | { |
| 7890 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 7891 | { |
| 7892 | p = ml_get_curline(); |
| 7893 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 7894 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 7895 | return TRUE; |
| 7896 | } |
| 7897 | ++look; |
| 7898 | } |
| 7899 | |
| 7900 | /* |
| 7901 | * ':' only causes an indent if it is at the end of a label or case |
| 7902 | * statement, or when it was before typing the ':' (to fix |
| 7903 | * class::method for C++). |
| 7904 | */ |
| 7905 | else if (*look == ':') |
| 7906 | { |
| 7907 | if (try_match && keytyped == ':') |
| 7908 | { |
| 7909 | p = ml_get_curline(); |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7910 | if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7911 | return TRUE; |
Bram Moolenaar | 3011815 | 2007-06-28 10:49:22 +0000 | [diff] [blame] | 7912 | /* Need to get the line again after cin_islabel(). */ |
| 7913 | p = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7914 | if (curwin->w_cursor.col > 2 |
| 7915 | && p[curwin->w_cursor.col - 1] == ':' |
| 7916 | && p[curwin->w_cursor.col - 2] == ':') |
| 7917 | { |
| 7918 | p[curwin->w_cursor.col - 1] = ' '; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7919 | i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
Bram Moolenaar | 84dbb62 | 2013-11-06 04:01:36 +0100 | [diff] [blame] | 7920 | || cin_islabel()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7921 | p = ml_get_curline(); |
| 7922 | p[curwin->w_cursor.col - 1] = ':'; |
| 7923 | if (i) |
| 7924 | return TRUE; |
| 7925 | } |
| 7926 | } |
| 7927 | ++look; |
| 7928 | } |
| 7929 | |
| 7930 | |
| 7931 | /* |
| 7932 | * Is it a key in <>, maybe? |
| 7933 | */ |
| 7934 | else if (*look == '<') |
| 7935 | { |
| 7936 | if (try_match) |
| 7937 | { |
| 7938 | /* |
| 7939 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 7940 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 7941 | * >, *, : and ! keys if they really really want to. |
| 7942 | */ |
| 7943 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 7944 | && keytyped == look[1]) |
| 7945 | return TRUE; |
| 7946 | |
| 7947 | if (keytyped == get_special_key_code(look + 1)) |
| 7948 | return TRUE; |
| 7949 | } |
| 7950 | while (*look && *look != '>') |
| 7951 | look++; |
| 7952 | while (*look == '>') |
| 7953 | look++; |
| 7954 | } |
| 7955 | |
| 7956 | /* |
| 7957 | * Is it a word: "=word"? |
| 7958 | */ |
| 7959 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 7960 | { |
| 7961 | ++look; |
| 7962 | if (*look == '~') |
| 7963 | { |
| 7964 | icase = TRUE; |
| 7965 | ++look; |
| 7966 | } |
| 7967 | else |
| 7968 | icase = FALSE; |
| 7969 | p = vim_strchr(look, ','); |
| 7970 | if (p == NULL) |
| 7971 | p = look + STRLEN(look); |
| 7972 | if ((try_match || try_match_word) |
| 7973 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 7974 | { |
| 7975 | int match = FALSE; |
| 7976 | |
| 7977 | #ifdef FEAT_INS_EXPAND |
| 7978 | if (keytyped == KEY_COMPLETE) |
| 7979 | { |
| 7980 | char_u *s; |
| 7981 | |
| 7982 | /* Just completed a word, check if it starts with "look". |
| 7983 | * search back for the start of a word. */ |
| 7984 | line = ml_get_curline(); |
| 7985 | # ifdef FEAT_MBYTE |
| 7986 | if (has_mbyte) |
| 7987 | { |
| 7988 | char_u *n; |
| 7989 | |
| 7990 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 7991 | { |
| 7992 | n = mb_prevptr(line, s); |
| 7993 | if (!vim_iswordp(n)) |
| 7994 | break; |
| 7995 | } |
| 7996 | } |
| 7997 | else |
| 7998 | # endif |
| 7999 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 8000 | if (!vim_iswordc(s[-1])) |
| 8001 | break; |
| 8002 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 8003 | && (icase |
| 8004 | ? MB_STRNICMP(s, look, p - look) |
| 8005 | : STRNCMP(s, look, p - look)) == 0) |
| 8006 | match = TRUE; |
| 8007 | } |
| 8008 | else |
| 8009 | #endif |
| 8010 | /* TODO: multi-byte */ |
| 8011 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 8012 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 8013 | { |
| 8014 | line = ml_get_cursor(); |
| 8015 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 8016 | || !vim_iswordc(line[-(p - look) - 1])) |
| 8017 | && (icase |
| 8018 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 8019 | : STRNCMP(line - (p - look), look, p - look)) |
| 8020 | == 0) |
| 8021 | match = TRUE; |
| 8022 | } |
| 8023 | if (match && try_match_word && !try_match) |
| 8024 | { |
| 8025 | /* "0=word": Check if there are only blanks before the |
| 8026 | * word. */ |
| 8027 | line = ml_get_curline(); |
| 8028 | if ((int)(skipwhite(line) - line) != |
| 8029 | (int)(curwin->w_cursor.col - (p - look))) |
| 8030 | match = FALSE; |
| 8031 | } |
| 8032 | if (match) |
| 8033 | return TRUE; |
| 8034 | } |
| 8035 | look = p; |
| 8036 | } |
| 8037 | |
| 8038 | /* |
| 8039 | * ok, it's a boring generic character. |
| 8040 | */ |
| 8041 | else |
| 8042 | { |
| 8043 | if (try_match && *look == keytyped) |
| 8044 | return TRUE; |
| 8045 | ++look; |
| 8046 | } |
| 8047 | |
| 8048 | /* |
| 8049 | * Skip over ", ". |
| 8050 | */ |
| 8051 | look = skip_to_option_part(look); |
| 8052 | } |
| 8053 | return FALSE; |
| 8054 | } |
| 8055 | #endif /* FEAT_CINDENT */ |
| 8056 | |
| 8057 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 8058 | /* |
| 8059 | * Map Hebrew keyboard when in hkmap mode. |
| 8060 | */ |
| 8061 | int |
| 8062 | hkmap(c) |
| 8063 | int c; |
| 8064 | { |
| 8065 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 8066 | { |
| 8067 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 8068 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 8069 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 8070 | static char_u map[26] = |
| 8071 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 8072 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 8073 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 8074 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 8075 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 8076 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 8077 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 8078 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 8079 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 8080 | |
| 8081 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 8082 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 8083 | /* '-1'='sofit' */ |
| 8084 | else if (c == 'x') |
| 8085 | return 'X'; |
| 8086 | else if (c == 'q') |
| 8087 | return '\''; /* {geresh}={'} */ |
| 8088 | else if (c == 246) |
| 8089 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 8090 | else if (c == 228) |
| 8091 | return ' '; /* \"a --> ' ' -- / -- */ |
| 8092 | else if (c == 252) |
| 8093 | return ' '; /* \"u --> ' ' -- / -- */ |
| 8094 | #ifdef EBCDIC |
| 8095 | else if (islower(c)) |
| 8096 | #else |
| 8097 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 8098 | * do this the same was as 5.7 and previous, so it works correctly on |
| 8099 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 8100 | * munged and won't work if e.g. searching for Hebrew text. |
| 8101 | */ |
| 8102 | else if (c >= 'a' && c <= 'z') |
| 8103 | #endif |
| 8104 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 8105 | else |
| 8106 | return c; |
| 8107 | } |
| 8108 | else |
| 8109 | { |
| 8110 | switch (c) |
| 8111 | { |
| 8112 | case '`': return ';'; |
| 8113 | case '/': return '.'; |
| 8114 | case '\'': return ','; |
| 8115 | case 'q': return '/'; |
| 8116 | case 'w': return '\''; |
| 8117 | |
| 8118 | /* Hebrew letters - set offset from 'a' */ |
| 8119 | case ',': c = '{'; break; |
| 8120 | case '.': c = 'v'; break; |
| 8121 | case ';': c = 't'; break; |
| 8122 | default: { |
| 8123 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 8124 | |
| 8125 | #ifdef EBCDIC |
| 8126 | /* see note about islower() above */ |
| 8127 | if (!islower(c)) |
| 8128 | #else |
| 8129 | if (c < 'a' || c > 'z') |
| 8130 | #endif |
| 8131 | return c; |
| 8132 | c = str[CharOrdLow(c)]; |
| 8133 | break; |
| 8134 | } |
| 8135 | } |
| 8136 | |
| 8137 | return (int)(CharOrdLow(c) + p_aleph); |
| 8138 | } |
| 8139 | } |
| 8140 | #endif |
| 8141 | |
| 8142 | static void |
| 8143 | ins_reg() |
| 8144 | { |
| 8145 | int need_redraw = FALSE; |
| 8146 | int regname; |
| 8147 | int literally = 0; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8148 | int vis_active = VIsual_active; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8149 | |
| 8150 | /* |
| 8151 | * If we are going to wait for a character, show a '"'. |
| 8152 | */ |
| 8153 | pc_status = PC_STATUS_UNSET; |
| 8154 | if (redrawing() && !char_avail()) |
| 8155 | { |
| 8156 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 8157 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8158 | |
| 8159 | edit_putchar('"', TRUE); |
| 8160 | #ifdef FEAT_CMDL_INFO |
| 8161 | add_to_showcmd_c(Ctrl_R); |
| 8162 | #endif |
| 8163 | } |
| 8164 | |
| 8165 | #ifdef USE_ON_FLY_SCROLL |
| 8166 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 8167 | #endif |
| 8168 | |
| 8169 | /* |
| 8170 | * Don't map the register name. This also prevents the mode message to be |
| 8171 | * deleted when ESC is hit. |
| 8172 | */ |
| 8173 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8174 | regname = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8175 | LANGMAP_ADJUST(regname, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8176 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 8177 | { |
| 8178 | /* Get a third key for literal register insertion */ |
| 8179 | literally = regname; |
| 8180 | #ifdef FEAT_CMDL_INFO |
| 8181 | add_to_showcmd_c(literally); |
| 8182 | #endif |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8183 | regname = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8184 | LANGMAP_ADJUST(regname, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8185 | } |
| 8186 | --no_mapping; |
| 8187 | |
| 8188 | #ifdef FEAT_EVAL |
Bram Moolenaar | df9259a | 2013-06-15 17:54:43 +0200 | [diff] [blame] | 8189 | /* Don't call u_sync() while typing the expression or giving an error |
| 8190 | * message for it. Only call it explicitly. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8191 | ++no_u_sync; |
| 8192 | if (regname == '=') |
| 8193 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8194 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8195 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8196 | # endif |
Bram Moolenaar | 3c1e9c2 | 2013-07-04 20:25:41 +0200 | [diff] [blame] | 8197 | /* Sync undo when evaluating the expression calls setline() or |
| 8198 | * append(), so that it can be undone separately. */ |
| 8199 | u_sync_once = 2; |
Bram Moolenaar | 5737ca2 | 2013-06-27 22:21:24 +0200 | [diff] [blame] | 8200 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8201 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8202 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8203 | /* Restore the Input Method. */ |
| 8204 | if (im_on) |
| 8205 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8206 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8207 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 8208 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 8209 | { |
| 8210 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8211 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 8212 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8213 | else |
| 8214 | { |
| 8215 | #endif |
| 8216 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 8217 | { |
| 8218 | /* Append the command to the redo buffer. */ |
| 8219 | AppendCharToRedobuff(Ctrl_R); |
| 8220 | AppendCharToRedobuff(literally); |
| 8221 | AppendCharToRedobuff(regname); |
| 8222 | |
| 8223 | do_put(regname, BACKWARD, 1L, |
| 8224 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 8225 | } |
| 8226 | else if (insert_reg(regname, literally) == FAIL) |
| 8227 | { |
| 8228 | vim_beep(); |
| 8229 | need_redraw = TRUE; /* remove the '"' */ |
| 8230 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8231 | else if (stop_insert_mode) |
| 8232 | /* When the '=' register was used and a function was invoked that |
| 8233 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 8234 | * insert anything, need to remove the '"' */ |
| 8235 | need_redraw = TRUE; |
| 8236 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8237 | #ifdef FEAT_EVAL |
| 8238 | } |
| 8239 | --no_u_sync; |
Bram Moolenaar | 3c1e9c2 | 2013-07-04 20:25:41 +0200 | [diff] [blame] | 8240 | if (u_sync_once == 1) |
| 8241 | ins_need_undo = TRUE; |
| 8242 | u_sync_once = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8243 | #endif |
| 8244 | #ifdef FEAT_CMDL_INFO |
| 8245 | clear_showcmd(); |
| 8246 | #endif |
| 8247 | |
| 8248 | /* If the inserted register is empty, we need to remove the '"' */ |
| 8249 | if (need_redraw || stuff_empty()) |
| 8250 | edit_unputchar(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8251 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8252 | /* Disallow starting Visual mode here, would get a weird mode. */ |
| 8253 | if (!vis_active && VIsual_active) |
| 8254 | end_visual_mode(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8255 | } |
| 8256 | |
| 8257 | /* |
| 8258 | * CTRL-G commands in Insert mode. |
| 8259 | */ |
| 8260 | static void |
| 8261 | ins_ctrl_g() |
| 8262 | { |
| 8263 | int c; |
| 8264 | |
| 8265 | #ifdef FEAT_INS_EXPAND |
| 8266 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 8267 | setcursor(); |
| 8268 | #endif |
| 8269 | |
| 8270 | /* |
| 8271 | * Don't map the second key. This also prevents the mode message to be |
| 8272 | * deleted when ESC is hit. |
| 8273 | */ |
| 8274 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8275 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8276 | --no_mapping; |
| 8277 | switch (c) |
| 8278 | { |
| 8279 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 8280 | case K_UP: |
| 8281 | case Ctrl_K: |
| 8282 | case 'k': ins_up(TRUE); |
| 8283 | break; |
| 8284 | |
| 8285 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 8286 | case K_DOWN: |
| 8287 | case Ctrl_J: |
| 8288 | case 'j': ins_down(TRUE); |
| 8289 | break; |
| 8290 | |
| 8291 | /* CTRL-G u: start new undoable edit */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 8292 | case 'u': u_sync(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8293 | ins_need_undo = TRUE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 8294 | |
| 8295 | /* Need to reset Insstart, esp. because a BS that joins |
Bram Moolenaar | 34e0bfa | 2007-05-10 18:44:18 +0000 | [diff] [blame] | 8296 | * a line to the previous one must save for undo. */ |
Bram Moolenaar | b1d90a3 | 2014-02-22 23:03:55 +0100 | [diff] [blame] | 8297 | update_Insstart_orig = FALSE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 8298 | Insstart = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8299 | break; |
| 8300 | |
| 8301 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 8302 | default: vim_beep(); |
| 8303 | } |
| 8304 | } |
| 8305 | |
| 8306 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8307 | * CTRL-^ in Insert mode. |
| 8308 | */ |
| 8309 | static void |
| 8310 | ins_ctrl_hat() |
| 8311 | { |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 8312 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8313 | { |
| 8314 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 8315 | if (State & LANGMAP) |
| 8316 | { |
| 8317 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 8318 | State &= ~LANGMAP; |
| 8319 | } |
| 8320 | else |
| 8321 | { |
| 8322 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 8323 | State |= LANGMAP; |
| 8324 | #ifdef USE_IM_CONTROL |
| 8325 | im_set_active(FALSE); |
| 8326 | #endif |
| 8327 | } |
| 8328 | } |
| 8329 | #ifdef USE_IM_CONTROL |
| 8330 | else |
| 8331 | { |
| 8332 | /* There are no ":lmap" mappings, toggle IM */ |
| 8333 | if (im_get_status()) |
| 8334 | { |
| 8335 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 8336 | im_set_active(FALSE); |
| 8337 | } |
| 8338 | else |
| 8339 | { |
| 8340 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 8341 | State &= ~LANGMAP; |
| 8342 | im_set_active(TRUE); |
| 8343 | } |
| 8344 | } |
| 8345 | #endif |
| 8346 | set_iminsert_global(); |
| 8347 | showmode(); |
| 8348 | #ifdef FEAT_GUI |
| 8349 | /* may show different cursor shape or color */ |
| 8350 | if (gui.in_use) |
| 8351 | gui_update_cursor(TRUE, FALSE); |
| 8352 | #endif |
| 8353 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 8354 | /* Show/unshow value of 'keymap' in status lines. */ |
| 8355 | status_redraw_curbuf(); |
| 8356 | #endif |
| 8357 | } |
| 8358 | |
| 8359 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8360 | * Handle ESC in insert mode. |
| 8361 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 8362 | * insert. |
| 8363 | */ |
| 8364 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8365 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8366 | long *count; |
| 8367 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8368 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8369 | { |
| 8370 | int temp; |
| 8371 | static int disabled_redraw = FALSE; |
| 8372 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 8373 | #ifdef FEAT_SPELL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8374 | check_spell_redraw(); |
| 8375 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8376 | #if defined(FEAT_HANGULIN) |
| 8377 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 8378 | hangul_input_state_set(0); |
| 8379 | # endif |
| 8380 | if (composing_hangul) |
| 8381 | { |
| 8382 | push_raw_key(composing_hangul_buffer, 2); |
| 8383 | composing_hangul = 0; |
| 8384 | } |
| 8385 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8386 | |
| 8387 | temp = curwin->w_cursor.col; |
| 8388 | if (disabled_redraw) |
| 8389 | { |
| 8390 | --RedrawingDisabled; |
| 8391 | disabled_redraw = FALSE; |
| 8392 | } |
| 8393 | if (!arrow_used) |
| 8394 | { |
| 8395 | /* |
| 8396 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 8397 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 8398 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8399 | */ |
| 8400 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 8401 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8402 | |
| 8403 | /* |
| 8404 | * Repeating insert may take a long time. Check for |
| 8405 | * interrupt now and then. |
| 8406 | */ |
| 8407 | if (*count > 0) |
| 8408 | { |
| 8409 | line_breakcheck(); |
| 8410 | if (got_int) |
| 8411 | *count = 0; |
| 8412 | } |
| 8413 | |
| 8414 | if (--*count > 0) /* repeat what was typed */ |
| 8415 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 8416 | /* Vi repeats the insert without replacing characters. */ |
| 8417 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 8418 | State &= ~REPLACE_FLAG; |
| 8419 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8420 | (void)start_redo_ins(); |
| 8421 | if (cmdchar == 'r' || cmdchar == 'v') |
Bram Moolenaar | 4f5ce33 | 2014-07-30 16:00:58 +0200 | [diff] [blame] | 8422 | stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8423 | ++RedrawingDisabled; |
| 8424 | disabled_redraw = TRUE; |
| 8425 | return FALSE; /* repeat the insert */ |
| 8426 | } |
Bram Moolenaar | f332a65 | 2013-11-04 04:20:33 +0100 | [diff] [blame] | 8427 | stop_insert(&curwin->w_cursor, TRUE, nomove); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8428 | undisplay_dollar(); |
| 8429 | } |
| 8430 | |
| 8431 | /* When an autoindent was removed, curswant stays after the |
| 8432 | * indent */ |
| 8433 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 8434 | curwin->w_set_curswant = TRUE; |
| 8435 | |
| 8436 | /* Remember the last Insert position in the '^ mark. */ |
| 8437 | if (!cmdmod.keepjumps) |
| 8438 | curbuf->b_last_insert = curwin->w_cursor; |
| 8439 | |
| 8440 | /* |
| 8441 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8442 | * 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] | 8443 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8444 | if (!nomove |
| 8445 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8446 | #ifdef FEAT_VIRTUALEDIT |
| 8447 | || curwin->w_cursor.coladd > 0 |
| 8448 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8449 | ) |
| 8450 | && (restart_edit == NUL |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 8451 | || (gchar_cursor() == NUL && !VIsual_active)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8452 | #ifdef FEAT_RIGHTLEFT |
| 8453 | && !revins_on |
| 8454 | #endif |
| 8455 | ) |
| 8456 | { |
| 8457 | #ifdef FEAT_VIRTUALEDIT |
| 8458 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 8459 | { |
| 8460 | oneleft(); |
| 8461 | if (restart_edit != NUL) |
| 8462 | ++curwin->w_cursor.coladd; |
| 8463 | } |
| 8464 | else |
| 8465 | #endif |
| 8466 | { |
| 8467 | --curwin->w_cursor.col; |
| 8468 | #ifdef FEAT_MBYTE |
| 8469 | /* Correct cursor for multi-byte character. */ |
| 8470 | if (has_mbyte) |
| 8471 | mb_adjust_cursor(); |
| 8472 | #endif |
| 8473 | } |
| 8474 | } |
| 8475 | |
| 8476 | #ifdef USE_IM_CONTROL |
| 8477 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 8478 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 8479 | * well). */ |
| 8480 | if (!(State & LANGMAP)) |
| 8481 | im_save_status(&curbuf->b_p_iminsert); |
| 8482 | im_set_active(FALSE); |
| 8483 | #endif |
| 8484 | |
| 8485 | State = NORMAL; |
| 8486 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 8487 | changed_cline_bef_curs(); |
| 8488 | |
| 8489 | #ifdef FEAT_MOUSE |
| 8490 | setmouse(); |
| 8491 | #endif |
| 8492 | #ifdef CURSOR_SHAPE |
| 8493 | ui_cursor_shape(); /* may show different cursor shape */ |
| 8494 | #endif |
| 8495 | |
| 8496 | /* |
| 8497 | * When recording or for CTRL-O, need to display the new mode. |
| 8498 | * Otherwise remove the mode message. |
| 8499 | */ |
| 8500 | if (Recording || restart_edit != NUL) |
| 8501 | showmode(); |
| 8502 | else if (p_smd) |
| 8503 | MSG(""); |
| 8504 | |
| 8505 | return TRUE; /* exit Insert mode */ |
| 8506 | } |
| 8507 | |
| 8508 | #ifdef FEAT_RIGHTLEFT |
| 8509 | /* |
| 8510 | * Toggle language: hkmap and revins_on. |
| 8511 | * Move to end of reverse inserted text. |
| 8512 | */ |
| 8513 | static void |
| 8514 | ins_ctrl_() |
| 8515 | { |
| 8516 | if (revins_on && revins_chars && revins_scol >= 0) |
| 8517 | { |
| 8518 | while (gchar_cursor() != NUL && revins_chars--) |
| 8519 | ++curwin->w_cursor.col; |
| 8520 | } |
| 8521 | p_ri = !p_ri; |
| 8522 | revins_on = (State == INSERT && p_ri); |
| 8523 | if (revins_on) |
| 8524 | { |
| 8525 | revins_scol = curwin->w_cursor.col; |
| 8526 | revins_legal++; |
| 8527 | revins_chars = 0; |
| 8528 | undisplay_dollar(); |
| 8529 | } |
| 8530 | else |
| 8531 | revins_scol = -1; |
| 8532 | #ifdef FEAT_FKMAP |
| 8533 | if (p_altkeymap) |
| 8534 | { |
| 8535 | /* |
| 8536 | * to be consistent also for redo command, using '.' |
| 8537 | * set arrow_used to true and stop it - causing to redo |
| 8538 | * characters entered in one mode (normal/reverse insert). |
| 8539 | */ |
| 8540 | arrow_used = TRUE; |
| 8541 | (void)stop_arrow(); |
| 8542 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 8543 | if (p_fkmap && p_ri) |
| 8544 | State = INSERT; |
| 8545 | } |
| 8546 | else |
| 8547 | #endif |
| 8548 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 8549 | showmode(); |
| 8550 | } |
| 8551 | #endif |
| 8552 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8553 | /* |
| 8554 | * If 'keymodel' contains "startsel", may start selection. |
| 8555 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 8556 | */ |
| 8557 | static int |
| 8558 | ins_start_select(c) |
| 8559 | int c; |
| 8560 | { |
| 8561 | if (km_startsel) |
| 8562 | switch (c) |
| 8563 | { |
| 8564 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8565 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8566 | case K_PAGEUP: |
| 8567 | case K_KPAGEUP: |
| 8568 | case K_PAGEDOWN: |
| 8569 | case K_KPAGEDOWN: |
| 8570 | # ifdef MACOS |
| 8571 | case K_LEFT: |
| 8572 | case K_RIGHT: |
| 8573 | case K_UP: |
| 8574 | case K_DOWN: |
| 8575 | case K_END: |
| 8576 | case K_HOME: |
| 8577 | # endif |
| 8578 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 8579 | break; |
| 8580 | /* FALLTHROUGH */ |
| 8581 | case K_S_LEFT: |
| 8582 | case K_S_RIGHT: |
| 8583 | case K_S_UP: |
| 8584 | case K_S_DOWN: |
| 8585 | case K_S_END: |
| 8586 | case K_S_HOME: |
| 8587 | /* Start selection right away, the cursor can move with |
| 8588 | * CTRL-O when beyond the end of the line. */ |
| 8589 | start_selection(); |
| 8590 | |
| 8591 | /* Execute the key in (insert) Select mode. */ |
| 8592 | stuffcharReadbuff(Ctrl_O); |
| 8593 | if (mod_mask) |
| 8594 | { |
| 8595 | char_u buf[4]; |
| 8596 | |
| 8597 | buf[0] = K_SPECIAL; |
| 8598 | buf[1] = KS_MODIFIER; |
| 8599 | buf[2] = mod_mask; |
| 8600 | buf[3] = NUL; |
| 8601 | stuffReadbuff(buf); |
| 8602 | } |
| 8603 | stuffcharReadbuff(c); |
| 8604 | return TRUE; |
| 8605 | } |
| 8606 | return FALSE; |
| 8607 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8608 | |
| 8609 | /* |
Bram Moolenaar | 84a05ac | 2013-05-06 04:24:17 +0200 | [diff] [blame] | 8610 | * <Insert> key in Insert mode: toggle insert/replace mode. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8611 | */ |
| 8612 | static void |
| 8613 | ins_insert(replaceState) |
| 8614 | int replaceState; |
| 8615 | { |
| 8616 | #ifdef FEAT_FKMAP |
| 8617 | if (p_fkmap && p_ri) |
| 8618 | { |
| 8619 | beep_flush(); |
| 8620 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 8621 | return; |
| 8622 | } |
| 8623 | #endif |
| 8624 | |
| 8625 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 8626 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8627 | set_vim_var_string(VV_INSERTMODE, |
| 8628 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 8629 | # ifdef FEAT_VREPLACE |
| 8630 | replaceState == VREPLACE ? "v" : |
| 8631 | # endif |
| 8632 | "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 8633 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8634 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 8635 | #endif |
| 8636 | if (State & REPLACE_FLAG) |
| 8637 | State = INSERT | (State & LANGMAP); |
| 8638 | else |
| 8639 | State = replaceState | (State & LANGMAP); |
| 8640 | AppendCharToRedobuff(K_INS); |
| 8641 | showmode(); |
| 8642 | #ifdef CURSOR_SHAPE |
| 8643 | ui_cursor_shape(); /* may show different cursor shape */ |
| 8644 | #endif |
| 8645 | } |
| 8646 | |
| 8647 | /* |
| 8648 | * Pressed CTRL-O in Insert mode. |
| 8649 | */ |
| 8650 | static void |
| 8651 | ins_ctrl_o() |
| 8652 | { |
| 8653 | #ifdef FEAT_VREPLACE |
| 8654 | if (State & VREPLACE_FLAG) |
| 8655 | restart_edit = 'V'; |
| 8656 | else |
| 8657 | #endif |
| 8658 | if (State & REPLACE_FLAG) |
| 8659 | restart_edit = 'R'; |
| 8660 | else |
| 8661 | restart_edit = 'I'; |
| 8662 | #ifdef FEAT_VIRTUALEDIT |
| 8663 | if (virtual_active()) |
| 8664 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 8665 | else |
| 8666 | #endif |
| 8667 | ins_at_eol = (gchar_cursor() == NUL); |
| 8668 | } |
| 8669 | |
| 8670 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8671 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 8672 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 8673 | * Always round the indent to 'shiftwidth', this is compatible |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8674 | * with vi. But vi only supports ^T and ^D after an |
| 8675 | * autoindent, we support it everywhere. |
| 8676 | */ |
| 8677 | static void |
| 8678 | ins_shift(c, lastc) |
| 8679 | int c; |
| 8680 | int lastc; |
| 8681 | { |
| 8682 | if (stop_arrow() == FAIL) |
| 8683 | return; |
| 8684 | AppendCharToRedobuff(c); |
| 8685 | |
| 8686 | /* |
| 8687 | * 0^D and ^^D: remove all indent. |
| 8688 | */ |
Bram Moolenaar | 0cbac5b | 2007-07-29 13:03:35 +0000 | [diff] [blame] | 8689 | if (c == Ctrl_D && (lastc == '0' || lastc == '^') |
| 8690 | && curwin->w_cursor.col > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8691 | { |
| 8692 | --curwin->w_cursor.col; |
| 8693 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 8694 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 8695 | if (State & REPLACE_FLAG) |
| 8696 | replace_pop_ins(); |
| 8697 | if (lastc == '^') |
| 8698 | old_indent = get_indent(); /* remember curr. indent */ |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 8699 | change_indent(INDENT_SET, 0, TRUE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8700 | } |
| 8701 | else |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 8702 | 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] | 8703 | |
| 8704 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 8705 | did_ai = FALSE; |
| 8706 | #ifdef FEAT_SMARTINDENT |
| 8707 | did_si = FALSE; |
| 8708 | can_si = FALSE; |
| 8709 | can_si_back = FALSE; |
| 8710 | #endif |
| 8711 | #ifdef FEAT_CINDENT |
| 8712 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 8713 | #endif |
| 8714 | } |
| 8715 | |
| 8716 | static void |
| 8717 | ins_del() |
| 8718 | { |
| 8719 | int temp; |
| 8720 | |
| 8721 | if (stop_arrow() == FAIL) |
| 8722 | return; |
| 8723 | if (gchar_cursor() == NUL) /* delete newline */ |
| 8724 | { |
| 8725 | temp = curwin->w_cursor.col; |
| 8726 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 8727 | || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8728 | vim_beep(); |
| 8729 | else |
| 8730 | curwin->w_cursor.col = temp; |
| 8731 | } |
| 8732 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 8733 | vim_beep(); |
| 8734 | did_ai = FALSE; |
| 8735 | #ifdef FEAT_SMARTINDENT |
| 8736 | did_si = FALSE; |
| 8737 | can_si = FALSE; |
| 8738 | can_si_back = FALSE; |
| 8739 | #endif |
| 8740 | AppendCharToRedobuff(K_DEL); |
| 8741 | } |
| 8742 | |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8743 | static void ins_bs_one __ARGS((colnr_T *vcolp)); |
| 8744 | |
| 8745 | /* |
| 8746 | * Delete one character for ins_bs(). |
| 8747 | */ |
| 8748 | static void |
| 8749 | ins_bs_one(vcolp) |
| 8750 | colnr_T *vcolp; |
| 8751 | { |
| 8752 | dec_cursor(); |
| 8753 | getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL); |
| 8754 | if (State & REPLACE_FLAG) |
| 8755 | { |
| 8756 | /* Don't delete characters before the insert point when in |
| 8757 | * Replace mode */ |
| 8758 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 8759 | || curwin->w_cursor.col >= Insstart.col) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 8760 | replace_do_bs(-1); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8761 | } |
| 8762 | else |
| 8763 | (void)del_char(FALSE); |
| 8764 | } |
| 8765 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8766 | /* |
| 8767 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 8768 | * Return TRUE when backspace was actually used. |
| 8769 | */ |
| 8770 | static int |
| 8771 | ins_bs(c, mode, inserted_space_p) |
| 8772 | int c; |
| 8773 | int mode; |
| 8774 | int *inserted_space_p; |
| 8775 | { |
| 8776 | linenr_T lnum; |
| 8777 | int cc; |
| 8778 | int temp = 0; /* init for GCC */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8779 | colnr_T save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8780 | colnr_T mincol; |
| 8781 | int did_backspace = FALSE; |
| 8782 | int in_indent; |
| 8783 | int oldState; |
| 8784 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8785 | int cpc[MAX_MCO]; /* composing characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8786 | #endif |
| 8787 | |
| 8788 | /* |
| 8789 | * can't delete anything in an empty file |
| 8790 | * can't backup past first character in buffer |
| 8791 | * can't backup past starting point unless 'backspace' > 1 |
| 8792 | * can backup to a previous line if 'backspace' == 0 |
| 8793 | */ |
| 8794 | if ( bufempty() |
| 8795 | || ( |
| 8796 | #ifdef FEAT_RIGHTLEFT |
| 8797 | !revins_on && |
| 8798 | #endif |
| 8799 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 8800 | || (!can_bs(BS_START) |
| 8801 | && (arrow_used |
Bram Moolenaar | 3d1956b | 2014-04-29 14:44:35 +0200 | [diff] [blame] | 8802 | || (curwin->w_cursor.lnum == Insstart_orig.lnum |
| 8803 | && curwin->w_cursor.col <= Insstart_orig.col))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8804 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 8805 | && curwin->w_cursor.col <= ai_col) |
| 8806 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 8807 | { |
| 8808 | vim_beep(); |
| 8809 | return FALSE; |
| 8810 | } |
| 8811 | |
| 8812 | if (stop_arrow() == FAIL) |
| 8813 | return FALSE; |
| 8814 | in_indent = inindent(0); |
| 8815 | #ifdef FEAT_CINDENT |
| 8816 | if (in_indent) |
| 8817 | can_cindent = FALSE; |
| 8818 | #endif |
| 8819 | #ifdef FEAT_COMMENTS |
| 8820 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 8821 | #endif |
| 8822 | #ifdef FEAT_RIGHTLEFT |
| 8823 | if (revins_on) /* put cursor after last inserted char */ |
| 8824 | inc_cursor(); |
| 8825 | #endif |
| 8826 | |
| 8827 | #ifdef FEAT_VIRTUALEDIT |
| 8828 | /* Virtualedit: |
| 8829 | * BACKSPACE_CHAR eats a virtual space |
| 8830 | * BACKSPACE_WORD eats all coladd |
| 8831 | * BACKSPACE_LINE eats all coladd and keeps going |
| 8832 | */ |
| 8833 | if (curwin->w_cursor.coladd > 0) |
| 8834 | { |
| 8835 | if (mode == BACKSPACE_CHAR) |
| 8836 | { |
| 8837 | --curwin->w_cursor.coladd; |
| 8838 | return TRUE; |
| 8839 | } |
| 8840 | if (mode == BACKSPACE_WORD) |
| 8841 | { |
| 8842 | curwin->w_cursor.coladd = 0; |
| 8843 | return TRUE; |
| 8844 | } |
| 8845 | curwin->w_cursor.coladd = 0; |
| 8846 | } |
| 8847 | #endif |
| 8848 | |
| 8849 | /* |
| 8850 | * delete newline! |
| 8851 | */ |
| 8852 | if (curwin->w_cursor.col == 0) |
| 8853 | { |
Bram Moolenaar | c3bbad0 | 2015-02-17 17:50:26 +0100 | [diff] [blame] | 8854 | lnum = Insstart.lnum; |
Bram Moolenaar | 3d1956b | 2014-04-29 14:44:35 +0200 | [diff] [blame] | 8855 | if (curwin->w_cursor.lnum == lnum |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8856 | #ifdef FEAT_RIGHTLEFT |
| 8857 | || revins_on |
| 8858 | #endif |
| 8859 | ) |
| 8860 | { |
| 8861 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 8862 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 8863 | return FALSE; |
Bram Moolenaar | c3bbad0 | 2015-02-17 17:50:26 +0100 | [diff] [blame] | 8864 | --Insstart.lnum; |
| 8865 | Insstart.col = MAXCOL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8866 | } |
| 8867 | /* |
| 8868 | * In replace mode: |
| 8869 | * cc < 0: NL was inserted, delete it |
| 8870 | * cc >= 0: NL was replaced, put original characters back |
| 8871 | */ |
| 8872 | cc = -1; |
| 8873 | if (State & REPLACE_FLAG) |
| 8874 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 8875 | /* |
| 8876 | * In replace mode, in the line we started replacing, we only move the |
| 8877 | * cursor. |
| 8878 | */ |
| 8879 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 8880 | { |
| 8881 | dec_cursor(); |
| 8882 | } |
| 8883 | else |
| 8884 | { |
| 8885 | #ifdef FEAT_VREPLACE |
| 8886 | if (!(State & VREPLACE_FLAG) |
| 8887 | || curwin->w_cursor.lnum > orig_line_count) |
| 8888 | #endif |
| 8889 | { |
| 8890 | temp = gchar_cursor(); /* remember current char */ |
| 8891 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8892 | |
| 8893 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 8894 | * the end of the line, otherwise the line will be broken |
| 8895 | * again when auto-formatting. */ |
| 8896 | if (has_format_option(FO_AUTO) |
| 8897 | && has_format_option(FO_WHITE_PAR)) |
| 8898 | { |
| 8899 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 8900 | TRUE); |
| 8901 | int len; |
| 8902 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8903 | len = (int)STRLEN(ptr); |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8904 | if (len > 0 && ptr[len - 1] == ' ') |
| 8905 | ptr[len - 1] = NUL; |
| 8906 | } |
| 8907 | |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 8908 | (void)do_join(2, FALSE, FALSE, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8909 | if (temp == NUL && gchar_cursor() != NUL) |
| 8910 | inc_cursor(); |
| 8911 | } |
| 8912 | #ifdef FEAT_VREPLACE |
| 8913 | else |
| 8914 | dec_cursor(); |
| 8915 | #endif |
| 8916 | |
| 8917 | /* |
| 8918 | * In REPLACE mode we have to put back the text that was replaced |
| 8919 | * by the NL. On the replace stack is first a NUL-terminated |
| 8920 | * sequence of characters that were deleted and then the |
| 8921 | * characters that NL replaced. |
| 8922 | */ |
| 8923 | if (State & REPLACE_FLAG) |
| 8924 | { |
| 8925 | /* |
| 8926 | * Do the next ins_char() in NORMAL state, to |
| 8927 | * prevent ins_char() from replacing characters and |
| 8928 | * avoiding showmatch(). |
| 8929 | */ |
| 8930 | oldState = State; |
| 8931 | State = NORMAL; |
| 8932 | /* |
| 8933 | * restore characters (blanks) deleted after cursor |
| 8934 | */ |
| 8935 | while (cc > 0) |
| 8936 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8937 | save_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8938 | #ifdef FEAT_MBYTE |
| 8939 | mb_replace_pop_ins(cc); |
| 8940 | #else |
| 8941 | ins_char(cc); |
| 8942 | #endif |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8943 | curwin->w_cursor.col = save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8944 | cc = replace_pop(); |
| 8945 | } |
| 8946 | /* restore the characters that NL replaced */ |
| 8947 | replace_pop_ins(); |
| 8948 | State = oldState; |
| 8949 | } |
| 8950 | } |
| 8951 | did_ai = FALSE; |
| 8952 | } |
| 8953 | else |
| 8954 | { |
| 8955 | /* |
| 8956 | * Delete character(s) before the cursor. |
| 8957 | */ |
| 8958 | #ifdef FEAT_RIGHTLEFT |
| 8959 | if (revins_on) /* put cursor on last inserted char */ |
| 8960 | dec_cursor(); |
| 8961 | #endif |
| 8962 | mincol = 0; |
| 8963 | /* keep indent */ |
Bram Moolenaar | 9248e6e | 2007-03-08 12:10:13 +0000 | [diff] [blame] | 8964 | if (mode == BACKSPACE_LINE |
| 8965 | && (curbuf->b_p_ai |
| 8966 | #ifdef FEAT_CINDENT |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 8967 | || cindent_on() |
Bram Moolenaar | 9248e6e | 2007-03-08 12:10:13 +0000 | [diff] [blame] | 8968 | #endif |
| 8969 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8970 | #ifdef FEAT_RIGHTLEFT |
| 8971 | && !revins_on |
| 8972 | #endif |
| 8973 | ) |
| 8974 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8975 | save_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8976 | beginline(BL_WHITE); |
Bram Moolenaar | 7667556 | 2009-11-11 12:22:32 +0000 | [diff] [blame] | 8977 | if (curwin->w_cursor.col < save_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8978 | mincol = curwin->w_cursor.col; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8979 | curwin->w_cursor.col = save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8980 | } |
| 8981 | |
| 8982 | /* |
| 8983 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 8984 | */ |
| 8985 | if ( mode == BACKSPACE_CHAR |
| 8986 | && ((p_sta && in_indent) |
Bram Moolenaar | 9f340fa | 2012-10-21 00:10:39 +0200 | [diff] [blame] | 8987 | || (get_sts_value() != 0 |
Bram Moolenaar | 7b88a0e | 2008-01-09 09:14:13 +0000 | [diff] [blame] | 8988 | && curwin->w_cursor.col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8989 | && (*(ml_get_cursor() - 1) == TAB |
| 8990 | || (*(ml_get_cursor() - 1) == ' ' |
| 8991 | && (!*inserted_space_p |
| 8992 | || arrow_used)))))) |
| 8993 | { |
| 8994 | int ts; |
| 8995 | colnr_T vcol; |
| 8996 | colnr_T want_vcol; |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8997 | colnr_T start_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8998 | |
| 8999 | *inserted_space_p = FALSE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 9000 | if (p_sta && in_indent) |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9001 | ts = (int)get_sw_value(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9002 | else |
Bram Moolenaar | 9f340fa | 2012-10-21 00:10:39 +0200 | [diff] [blame] | 9003 | ts = (int)get_sts_value(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9004 | /* Compute the virtual column where we want to be. Since |
| 9005 | * 'showbreak' may get in the way, need to get the last column of |
| 9006 | * the previous character. */ |
| 9007 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 9008 | start_vcol = vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9009 | dec_cursor(); |
| 9010 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 9011 | inc_cursor(); |
| 9012 | want_vcol = (want_vcol / ts) * ts; |
| 9013 | |
| 9014 | /* delete characters until we are at or before want_vcol */ |
| 9015 | while (vcol > want_vcol |
| 9016 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 9017 | ins_bs_one(&vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9018 | |
| 9019 | /* insert extra spaces until we are at want_vcol */ |
| 9020 | while (vcol < want_vcol) |
| 9021 | { |
| 9022 | /* Remember the first char we inserted */ |
Bram Moolenaar | 3d1956b | 2014-04-29 14:44:35 +0200 | [diff] [blame] | 9023 | if (curwin->w_cursor.lnum == Insstart_orig.lnum |
| 9024 | && curwin->w_cursor.col < Insstart_orig.col) |
| 9025 | Insstart_orig.col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9026 | |
| 9027 | #ifdef FEAT_VREPLACE |
| 9028 | if (State & VREPLACE_FLAG) |
| 9029 | ins_char(' '); |
| 9030 | else |
| 9031 | #endif |
| 9032 | { |
| 9033 | ins_str((char_u *)" "); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 9034 | if ((State & REPLACE_FLAG)) |
| 9035 | replace_push(NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9036 | } |
| 9037 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 9038 | } |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 9039 | |
| 9040 | /* If we are now back where we started delete one character. Can |
| 9041 | * happen when using 'sts' and 'linebreak'. */ |
| 9042 | if (vcol >= start_vcol) |
| 9043 | ins_bs_one(&vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9044 | } |
| 9045 | |
| 9046 | /* |
| 9047 | * Delete upto starting point, start of line or previous word. |
| 9048 | */ |
| 9049 | else do |
| 9050 | { |
| 9051 | #ifdef FEAT_RIGHTLEFT |
| 9052 | if (!revins_on) /* put cursor on char to be deleted */ |
| 9053 | #endif |
| 9054 | dec_cursor(); |
| 9055 | |
| 9056 | /* start of word? */ |
| 9057 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 9058 | { |
| 9059 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 9060 | temp = vim_iswordc(gchar_cursor()); |
| 9061 | } |
| 9062 | /* end of word? */ |
| 9063 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 9064 | && (vim_isspace(cc = gchar_cursor()) |
| 9065 | || vim_iswordc(cc) != temp)) |
| 9066 | { |
| 9067 | #ifdef FEAT_RIGHTLEFT |
| 9068 | if (!revins_on) |
| 9069 | #endif |
| 9070 | inc_cursor(); |
| 9071 | #ifdef FEAT_RIGHTLEFT |
| 9072 | else if (State & REPLACE_FLAG) |
| 9073 | dec_cursor(); |
| 9074 | #endif |
| 9075 | break; |
| 9076 | } |
| 9077 | if (State & REPLACE_FLAG) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 9078 | replace_do_bs(-1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9079 | else |
| 9080 | { |
| 9081 | #ifdef FEAT_MBYTE |
| 9082 | if (enc_utf8 && p_deco) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 9083 | (void)utfc_ptr2char(ml_get_cursor(), cpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9084 | #endif |
| 9085 | (void)del_char(FALSE); |
| 9086 | #ifdef FEAT_MBYTE |
| 9087 | /* |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 9088 | * If there are combining characters and 'delcombine' is set |
| 9089 | * move the cursor back. Don't back up before the base |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9090 | * character. |
| 9091 | */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 9092 | if (enc_utf8 && p_deco && cpc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9093 | inc_cursor(); |
| 9094 | #endif |
| 9095 | #ifdef FEAT_RIGHTLEFT |
| 9096 | if (revins_chars) |
| 9097 | { |
| 9098 | revins_chars--; |
| 9099 | revins_legal++; |
| 9100 | } |
| 9101 | if (revins_on && gchar_cursor() == NUL) |
| 9102 | break; |
| 9103 | #endif |
| 9104 | } |
| 9105 | /* Just a single backspace?: */ |
| 9106 | if (mode == BACKSPACE_CHAR) |
| 9107 | break; |
| 9108 | } while ( |
| 9109 | #ifdef FEAT_RIGHTLEFT |
| 9110 | revins_on || |
| 9111 | #endif |
| 9112 | (curwin->w_cursor.col > mincol |
Bram Moolenaar | 3d1956b | 2014-04-29 14:44:35 +0200 | [diff] [blame] | 9113 | && (curwin->w_cursor.lnum != Insstart_orig.lnum |
| 9114 | || curwin->w_cursor.col != Insstart_orig.col))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9115 | did_backspace = TRUE; |
| 9116 | } |
| 9117 | #ifdef FEAT_SMARTINDENT |
| 9118 | did_si = FALSE; |
| 9119 | can_si = FALSE; |
| 9120 | can_si_back = FALSE; |
| 9121 | #endif |
| 9122 | if (curwin->w_cursor.col <= 1) |
| 9123 | did_ai = FALSE; |
| 9124 | /* |
| 9125 | * It's a little strange to put backspaces into the redo |
| 9126 | * buffer, but it makes auto-indent a lot easier to deal |
| 9127 | * with. |
| 9128 | */ |
| 9129 | AppendCharToRedobuff(c); |
| 9130 | |
| 9131 | /* If deleted before the insertion point, adjust it */ |
Bram Moolenaar | 3d1956b | 2014-04-29 14:44:35 +0200 | [diff] [blame] | 9132 | if (curwin->w_cursor.lnum == Insstart_orig.lnum |
| 9133 | && curwin->w_cursor.col < Insstart_orig.col) |
| 9134 | Insstart_orig.col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9135 | |
| 9136 | /* vi behaviour: the cursor moves backward but the character that |
| 9137 | * was there remains visible |
| 9138 | * Vim behaviour: the cursor moves backward and the character that |
| 9139 | * was there is erased from the screen. |
| 9140 | * We can emulate the vi behaviour by pretending there is a dollar |
| 9141 | * displayed even when there isn't. |
| 9142 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 9143 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9144 | dollar_vcol = curwin->w_virtcol; |
| 9145 | |
Bram Moolenaar | ce3be47 | 2008-01-14 19:12:28 +0000 | [diff] [blame] | 9146 | #ifdef FEAT_FOLDING |
| 9147 | /* When deleting a char the cursor line must never be in a closed fold. |
| 9148 | * E.g., when 'foldmethod' is indent and deleting the first non-white |
| 9149 | * char before a Tab. */ |
| 9150 | if (did_backspace) |
| 9151 | foldOpenCursor(); |
| 9152 | #endif |
| 9153 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9154 | return did_backspace; |
| 9155 | } |
| 9156 | |
| 9157 | #ifdef FEAT_MOUSE |
| 9158 | static void |
| 9159 | ins_mouse(c) |
| 9160 | int c; |
| 9161 | { |
| 9162 | pos_T tpos; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 9163 | win_T *old_curwin = curwin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9164 | |
| 9165 | # ifdef FEAT_GUI |
| 9166 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 9167 | if (!gui.in_use) |
| 9168 | # endif |
| 9169 | if (!mouse_has(MOUSE_INSERT)) |
| 9170 | return; |
| 9171 | |
| 9172 | undisplay_dollar(); |
| 9173 | tpos = curwin->w_cursor; |
| 9174 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 9175 | { |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 9176 | #ifdef FEAT_WINDOWS |
| 9177 | win_T *new_curwin = curwin; |
| 9178 | |
| 9179 | if (curwin != old_curwin && win_valid(old_curwin)) |
| 9180 | { |
| 9181 | /* Mouse took us to another window. We need to go back to the |
| 9182 | * previous one to stop insert there properly. */ |
| 9183 | curwin = old_curwin; |
| 9184 | curbuf = curwin->w_buffer; |
| 9185 | } |
| 9186 | #endif |
| 9187 | start_arrow(curwin == old_curwin ? &tpos : NULL); |
| 9188 | #ifdef FEAT_WINDOWS |
| 9189 | if (curwin != new_curwin && win_valid(new_curwin)) |
| 9190 | { |
| 9191 | curwin = new_curwin; |
| 9192 | curbuf = curwin->w_buffer; |
| 9193 | } |
| 9194 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9195 | # ifdef FEAT_CINDENT |
| 9196 | can_cindent = TRUE; |
| 9197 | # endif |
| 9198 | } |
| 9199 | |
| 9200 | #ifdef FEAT_WINDOWS |
| 9201 | /* redraw status lines (in case another window became active) */ |
| 9202 | redraw_statuslines(); |
| 9203 | #endif |
| 9204 | } |
| 9205 | |
| 9206 | static void |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9207 | ins_mousescroll(dir) |
| 9208 | int dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9209 | { |
| 9210 | pos_T tpos; |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9211 | # if defined(FEAT_WINDOWS) |
| 9212 | win_T *old_curwin = curwin; |
| 9213 | # endif |
| 9214 | # ifdef FEAT_INS_EXPAND |
| 9215 | int did_scroll = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9216 | # endif |
| 9217 | |
| 9218 | tpos = curwin->w_cursor; |
| 9219 | |
Bram Moolenaar | 40cf4b4 | 2013-02-26 13:30:32 +0100 | [diff] [blame] | 9220 | # ifdef FEAT_WINDOWS |
| 9221 | if (mouse_row >= 0 && mouse_col >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9222 | { |
| 9223 | int row, col; |
| 9224 | |
| 9225 | row = mouse_row; |
| 9226 | col = mouse_col; |
| 9227 | |
| 9228 | /* find the window at the pointer coordinates */ |
| 9229 | curwin = mouse_find_win(&row, &col); |
| 9230 | curbuf = curwin->w_buffer; |
| 9231 | } |
| 9232 | if (curwin == old_curwin) |
| 9233 | # endif |
| 9234 | undisplay_dollar(); |
| 9235 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9236 | # ifdef FEAT_INS_EXPAND |
| 9237 | /* Don't scroll the window in which completion is being done. */ |
| 9238 | if (!pum_visible() |
| 9239 | # if defined(FEAT_WINDOWS) |
| 9240 | || curwin != old_curwin |
| 9241 | # endif |
| 9242 | ) |
| 9243 | # endif |
| 9244 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9245 | if (dir == MSCR_DOWN || dir == MSCR_UP) |
| 9246 | { |
| 9247 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 9248 | scroll_redraw(dir, |
| 9249 | (long)(curwin->w_botline - curwin->w_topline)); |
| 9250 | else |
| 9251 | scroll_redraw(dir, 3L); |
| 9252 | } |
| 9253 | #ifdef FEAT_GUI |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9254 | else |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9255 | { |
| 9256 | int val, step = 6; |
| 9257 | |
| 9258 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 9259 | step = W_WIDTH(curwin); |
| 9260 | val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step); |
| 9261 | if (val < 0) |
| 9262 | val = 0; |
| 9263 | gui_do_horiz_scroll(val, TRUE); |
| 9264 | } |
| 9265 | #endif |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9266 | # ifdef FEAT_INS_EXPAND |
| 9267 | did_scroll = TRUE; |
| 9268 | # endif |
| 9269 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9270 | |
Bram Moolenaar | 40cf4b4 | 2013-02-26 13:30:32 +0100 | [diff] [blame] | 9271 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9272 | curwin->w_redr_status = TRUE; |
| 9273 | |
| 9274 | curwin = old_curwin; |
| 9275 | curbuf = curwin->w_buffer; |
| 9276 | # endif |
| 9277 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9278 | # ifdef FEAT_INS_EXPAND |
| 9279 | /* The popup menu may overlay the window, need to redraw it. |
| 9280 | * TODO: Would be more efficient to only redraw the windows that are |
| 9281 | * overlapped by the popup menu. */ |
| 9282 | if (pum_visible() && did_scroll) |
| 9283 | { |
| 9284 | redraw_all_later(NOT_VALID); |
| 9285 | ins_compl_show_pum(); |
| 9286 | } |
| 9287 | # endif |
| 9288 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9289 | if (!equalpos(curwin->w_cursor, tpos)) |
| 9290 | { |
| 9291 | start_arrow(&tpos); |
| 9292 | # ifdef FEAT_CINDENT |
| 9293 | can_cindent = TRUE; |
| 9294 | # endif |
| 9295 | } |
| 9296 | } |
| 9297 | #endif |
| 9298 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9299 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 9300 | static void |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9301 | ins_tabline(c) |
| 9302 | int c; |
| 9303 | { |
| 9304 | /* We will be leaving the current window, unless closing another tab. */ |
| 9305 | if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE |
| 9306 | || (current_tab != 0 && current_tab != tabpage_index(curtab))) |
| 9307 | { |
| 9308 | undisplay_dollar(); |
| 9309 | start_arrow(&curwin->w_cursor); |
| 9310 | # ifdef FEAT_CINDENT |
| 9311 | can_cindent = TRUE; |
| 9312 | # endif |
| 9313 | } |
| 9314 | |
| 9315 | if (c == K_TABLINE) |
| 9316 | goto_tabpage(current_tab); |
| 9317 | else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 9318 | { |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9319 | handle_tabmenu(); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 9320 | redraw_statuslines(); /* will redraw the tabline when needed */ |
| 9321 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9322 | } |
| 9323 | #endif |
| 9324 | |
| 9325 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9326 | void |
| 9327 | ins_scroll() |
| 9328 | { |
| 9329 | pos_T tpos; |
| 9330 | |
| 9331 | undisplay_dollar(); |
| 9332 | tpos = curwin->w_cursor; |
| 9333 | if (gui_do_scroll()) |
| 9334 | { |
| 9335 | start_arrow(&tpos); |
| 9336 | # ifdef FEAT_CINDENT |
| 9337 | can_cindent = TRUE; |
| 9338 | # endif |
| 9339 | } |
| 9340 | } |
| 9341 | |
| 9342 | void |
| 9343 | ins_horscroll() |
| 9344 | { |
| 9345 | pos_T tpos; |
| 9346 | |
| 9347 | undisplay_dollar(); |
| 9348 | tpos = curwin->w_cursor; |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9349 | if (gui_do_horiz_scroll(scrollbar_value, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9350 | { |
| 9351 | start_arrow(&tpos); |
| 9352 | # ifdef FEAT_CINDENT |
| 9353 | can_cindent = TRUE; |
| 9354 | # endif |
| 9355 | } |
| 9356 | } |
| 9357 | #endif |
| 9358 | |
| 9359 | static void |
| 9360 | ins_left() |
| 9361 | { |
| 9362 | pos_T tpos; |
| 9363 | |
| 9364 | #ifdef FEAT_FOLDING |
| 9365 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9366 | foldOpenCursor(); |
| 9367 | #endif |
| 9368 | undisplay_dollar(); |
| 9369 | tpos = curwin->w_cursor; |
| 9370 | if (oneleft() == OK) |
| 9371 | { |
Bram Moolenaar | 39fecab | 2006-08-29 14:07:36 +0000 | [diff] [blame] | 9372 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 9373 | /* Only call start_arrow() when not busy with preediting, it will |
| 9374 | * break undo. K_LEFT is inserted in im_correct_cursor(). */ |
| 9375 | if (!im_is_preediting()) |
| 9376 | #endif |
| 9377 | start_arrow(&tpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9378 | #ifdef FEAT_RIGHTLEFT |
| 9379 | /* If exit reversed string, position is fixed */ |
| 9380 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 9381 | revins_legal++; |
| 9382 | revins_chars++; |
| 9383 | #endif |
| 9384 | } |
| 9385 | |
| 9386 | /* |
| 9387 | * if 'whichwrap' set for cursor in insert mode may go to |
| 9388 | * previous line |
| 9389 | */ |
| 9390 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 9391 | { |
| 9392 | start_arrow(&tpos); |
| 9393 | --(curwin->w_cursor.lnum); |
| 9394 | coladvance((colnr_T)MAXCOL); |
| 9395 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 9396 | } |
| 9397 | else |
| 9398 | vim_beep(); |
| 9399 | } |
| 9400 | |
| 9401 | static void |
| 9402 | ins_home(c) |
| 9403 | int c; |
| 9404 | { |
| 9405 | pos_T tpos; |
| 9406 | |
| 9407 | #ifdef FEAT_FOLDING |
| 9408 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9409 | foldOpenCursor(); |
| 9410 | #endif |
| 9411 | undisplay_dollar(); |
| 9412 | tpos = curwin->w_cursor; |
| 9413 | if (c == K_C_HOME) |
| 9414 | curwin->w_cursor.lnum = 1; |
| 9415 | curwin->w_cursor.col = 0; |
| 9416 | #ifdef FEAT_VIRTUALEDIT |
| 9417 | curwin->w_cursor.coladd = 0; |
| 9418 | #endif |
| 9419 | curwin->w_curswant = 0; |
| 9420 | start_arrow(&tpos); |
| 9421 | } |
| 9422 | |
| 9423 | static void |
| 9424 | ins_end(c) |
| 9425 | int c; |
| 9426 | { |
| 9427 | pos_T tpos; |
| 9428 | |
| 9429 | #ifdef FEAT_FOLDING |
| 9430 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9431 | foldOpenCursor(); |
| 9432 | #endif |
| 9433 | undisplay_dollar(); |
| 9434 | tpos = curwin->w_cursor; |
| 9435 | if (c == K_C_END) |
| 9436 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 9437 | coladvance((colnr_T)MAXCOL); |
| 9438 | curwin->w_curswant = MAXCOL; |
| 9439 | |
| 9440 | start_arrow(&tpos); |
| 9441 | } |
| 9442 | |
| 9443 | static void |
| 9444 | ins_s_left() |
| 9445 | { |
| 9446 | #ifdef FEAT_FOLDING |
| 9447 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9448 | foldOpenCursor(); |
| 9449 | #endif |
| 9450 | undisplay_dollar(); |
| 9451 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 9452 | { |
| 9453 | start_arrow(&curwin->w_cursor); |
| 9454 | (void)bck_word(1L, FALSE, FALSE); |
| 9455 | curwin->w_set_curswant = TRUE; |
| 9456 | } |
| 9457 | else |
| 9458 | vim_beep(); |
| 9459 | } |
| 9460 | |
| 9461 | static void |
| 9462 | ins_right() |
| 9463 | { |
| 9464 | #ifdef FEAT_FOLDING |
| 9465 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9466 | foldOpenCursor(); |
| 9467 | #endif |
| 9468 | undisplay_dollar(); |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 9469 | if (gchar_cursor() != NUL |
| 9470 | #ifdef FEAT_VIRTUALEDIT |
| 9471 | || virtual_active() |
| 9472 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9473 | ) |
| 9474 | { |
| 9475 | start_arrow(&curwin->w_cursor); |
| 9476 | curwin->w_set_curswant = TRUE; |
| 9477 | #ifdef FEAT_VIRTUALEDIT |
| 9478 | if (virtual_active()) |
| 9479 | oneright(); |
| 9480 | else |
| 9481 | #endif |
| 9482 | { |
| 9483 | #ifdef FEAT_MBYTE |
| 9484 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9485 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9486 | else |
| 9487 | #endif |
| 9488 | ++curwin->w_cursor.col; |
| 9489 | } |
| 9490 | |
| 9491 | #ifdef FEAT_RIGHTLEFT |
| 9492 | revins_legal++; |
| 9493 | if (revins_chars) |
| 9494 | revins_chars--; |
| 9495 | #endif |
| 9496 | } |
| 9497 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 9498 | * cursor to the next line */ |
| 9499 | else if (vim_strchr(p_ww, ']') != NULL |
| 9500 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 9501 | { |
| 9502 | start_arrow(&curwin->w_cursor); |
| 9503 | curwin->w_set_curswant = TRUE; |
| 9504 | ++curwin->w_cursor.lnum; |
| 9505 | curwin->w_cursor.col = 0; |
| 9506 | } |
| 9507 | else |
| 9508 | vim_beep(); |
| 9509 | } |
| 9510 | |
| 9511 | static void |
| 9512 | ins_s_right() |
| 9513 | { |
| 9514 | #ifdef FEAT_FOLDING |
| 9515 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9516 | foldOpenCursor(); |
| 9517 | #endif |
| 9518 | undisplay_dollar(); |
| 9519 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 9520 | || gchar_cursor() != NUL) |
| 9521 | { |
| 9522 | start_arrow(&curwin->w_cursor); |
| 9523 | (void)fwd_word(1L, FALSE, 0); |
| 9524 | curwin->w_set_curswant = TRUE; |
| 9525 | } |
| 9526 | else |
| 9527 | vim_beep(); |
| 9528 | } |
| 9529 | |
| 9530 | static void |
| 9531 | ins_up(startcol) |
| 9532 | int startcol; /* when TRUE move to Insstart.col */ |
| 9533 | { |
| 9534 | pos_T tpos; |
| 9535 | linenr_T old_topline = curwin->w_topline; |
| 9536 | #ifdef FEAT_DIFF |
| 9537 | int old_topfill = curwin->w_topfill; |
| 9538 | #endif |
| 9539 | |
| 9540 | undisplay_dollar(); |
| 9541 | tpos = curwin->w_cursor; |
| 9542 | if (cursor_up(1L, TRUE) == OK) |
| 9543 | { |
| 9544 | if (startcol) |
| 9545 | coladvance(getvcol_nolist(&Insstart)); |
| 9546 | if (old_topline != curwin->w_topline |
| 9547 | #ifdef FEAT_DIFF |
| 9548 | || old_topfill != curwin->w_topfill |
| 9549 | #endif |
| 9550 | ) |
| 9551 | redraw_later(VALID); |
| 9552 | start_arrow(&tpos); |
| 9553 | #ifdef FEAT_CINDENT |
| 9554 | can_cindent = TRUE; |
| 9555 | #endif |
| 9556 | } |
| 9557 | else |
| 9558 | vim_beep(); |
| 9559 | } |
| 9560 | |
| 9561 | static void |
| 9562 | ins_pageup() |
| 9563 | { |
| 9564 | pos_T tpos; |
| 9565 | |
| 9566 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9567 | |
| 9568 | #ifdef FEAT_WINDOWS |
| 9569 | if (mod_mask & MOD_MASK_CTRL) |
| 9570 | { |
| 9571 | /* <C-PageUp>: tab page back */ |
Bram Moolenaar | bc44482 | 2006-10-17 11:37:50 +0000 | [diff] [blame] | 9572 | if (first_tabpage->tp_next != NULL) |
| 9573 | { |
| 9574 | start_arrow(&curwin->w_cursor); |
| 9575 | goto_tabpage(-1); |
| 9576 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9577 | return; |
| 9578 | } |
| 9579 | #endif |
| 9580 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9581 | tpos = curwin->w_cursor; |
| 9582 | if (onepage(BACKWARD, 1L) == OK) |
| 9583 | { |
| 9584 | start_arrow(&tpos); |
| 9585 | #ifdef FEAT_CINDENT |
| 9586 | can_cindent = TRUE; |
| 9587 | #endif |
| 9588 | } |
| 9589 | else |
| 9590 | vim_beep(); |
| 9591 | } |
| 9592 | |
| 9593 | static void |
| 9594 | ins_down(startcol) |
| 9595 | int startcol; /* when TRUE move to Insstart.col */ |
| 9596 | { |
| 9597 | pos_T tpos; |
| 9598 | linenr_T old_topline = curwin->w_topline; |
| 9599 | #ifdef FEAT_DIFF |
| 9600 | int old_topfill = curwin->w_topfill; |
| 9601 | #endif |
| 9602 | |
| 9603 | undisplay_dollar(); |
| 9604 | tpos = curwin->w_cursor; |
| 9605 | if (cursor_down(1L, TRUE) == OK) |
| 9606 | { |
| 9607 | if (startcol) |
| 9608 | coladvance(getvcol_nolist(&Insstart)); |
| 9609 | if (old_topline != curwin->w_topline |
| 9610 | #ifdef FEAT_DIFF |
| 9611 | || old_topfill != curwin->w_topfill |
| 9612 | #endif |
| 9613 | ) |
| 9614 | redraw_later(VALID); |
| 9615 | start_arrow(&tpos); |
| 9616 | #ifdef FEAT_CINDENT |
| 9617 | can_cindent = TRUE; |
| 9618 | #endif |
| 9619 | } |
| 9620 | else |
| 9621 | vim_beep(); |
| 9622 | } |
| 9623 | |
| 9624 | static void |
| 9625 | ins_pagedown() |
| 9626 | { |
| 9627 | pos_T tpos; |
| 9628 | |
| 9629 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9630 | |
| 9631 | #ifdef FEAT_WINDOWS |
| 9632 | if (mod_mask & MOD_MASK_CTRL) |
| 9633 | { |
| 9634 | /* <C-PageDown>: tab page forward */ |
Bram Moolenaar | bc44482 | 2006-10-17 11:37:50 +0000 | [diff] [blame] | 9635 | if (first_tabpage->tp_next != NULL) |
| 9636 | { |
| 9637 | start_arrow(&curwin->w_cursor); |
| 9638 | goto_tabpage(0); |
| 9639 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9640 | return; |
| 9641 | } |
| 9642 | #endif |
| 9643 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9644 | tpos = curwin->w_cursor; |
| 9645 | if (onepage(FORWARD, 1L) == OK) |
| 9646 | { |
| 9647 | start_arrow(&tpos); |
| 9648 | #ifdef FEAT_CINDENT |
| 9649 | can_cindent = TRUE; |
| 9650 | #endif |
| 9651 | } |
| 9652 | else |
| 9653 | vim_beep(); |
| 9654 | } |
| 9655 | |
| 9656 | #ifdef FEAT_DND |
| 9657 | static void |
| 9658 | ins_drop() |
| 9659 | { |
| 9660 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 9661 | } |
| 9662 | #endif |
| 9663 | |
| 9664 | /* |
| 9665 | * Handle TAB in Insert or Replace mode. |
| 9666 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 9667 | */ |
| 9668 | static int |
| 9669 | ins_tab() |
| 9670 | { |
| 9671 | int ind; |
| 9672 | int i; |
| 9673 | int temp; |
| 9674 | |
| 9675 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 9676 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 9677 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 9678 | return FALSE; |
| 9679 | |
| 9680 | ind = inindent(0); |
| 9681 | #ifdef FEAT_CINDENT |
| 9682 | if (ind) |
| 9683 | can_cindent = FALSE; |
| 9684 | #endif |
| 9685 | |
| 9686 | /* |
| 9687 | * When nothing special, insert TAB like a normal character |
| 9688 | */ |
| 9689 | if (!curbuf->b_p_et |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9690 | && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf)) |
Bram Moolenaar | 9f340fa | 2012-10-21 00:10:39 +0200 | [diff] [blame] | 9691 | && get_sts_value() == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9692 | return TRUE; |
| 9693 | |
| 9694 | if (stop_arrow() == FAIL) |
| 9695 | return TRUE; |
| 9696 | |
| 9697 | did_ai = FALSE; |
| 9698 | #ifdef FEAT_SMARTINDENT |
| 9699 | did_si = FALSE; |
| 9700 | can_si = FALSE; |
| 9701 | can_si_back = FALSE; |
| 9702 | #endif |
| 9703 | AppendToRedobuff((char_u *)"\t"); |
| 9704 | |
| 9705 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 9706 | temp = (int)get_sw_value(curbuf); |
Bram Moolenaar | 9f340fa | 2012-10-21 00:10:39 +0200 | [diff] [blame] | 9707 | else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */ |
| 9708 | temp = (int)get_sts_value(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9709 | else /* otherwise use 'tabstop' */ |
| 9710 | temp = (int)curbuf->b_p_ts; |
| 9711 | temp -= get_nolist_virtcol() % temp; |
| 9712 | |
| 9713 | /* |
| 9714 | * Insert the first space with ins_char(). It will delete one char in |
| 9715 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 9716 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 9717 | */ |
| 9718 | ins_char(' '); |
| 9719 | while (--temp > 0) |
| 9720 | { |
| 9721 | #ifdef FEAT_VREPLACE |
| 9722 | if (State & VREPLACE_FLAG) |
| 9723 | ins_char(' '); |
| 9724 | else |
| 9725 | #endif |
| 9726 | { |
| 9727 | ins_str((char_u *)" "); |
| 9728 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 9729 | replace_push(NUL); |
| 9730 | } |
| 9731 | } |
| 9732 | |
| 9733 | /* |
| 9734 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 9735 | */ |
Bram Moolenaar | 9f340fa | 2012-10-21 00:10:39 +0200 | [diff] [blame] | 9736 | if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9737 | { |
| 9738 | char_u *ptr; |
| 9739 | #ifdef FEAT_VREPLACE |
| 9740 | char_u *saved_line = NULL; /* init for GCC */ |
| 9741 | pos_T pos; |
| 9742 | #endif |
| 9743 | pos_T fpos; |
| 9744 | pos_T *cursor; |
| 9745 | colnr_T want_vcol, vcol; |
| 9746 | int change_col = -1; |
| 9747 | int save_list = curwin->w_p_list; |
| 9748 | |
| 9749 | /* |
| 9750 | * Get the current line. For VREPLACE mode, don't make real changes |
| 9751 | * yet, just work on a copy of the line. |
| 9752 | */ |
| 9753 | #ifdef FEAT_VREPLACE |
| 9754 | if (State & VREPLACE_FLAG) |
| 9755 | { |
| 9756 | pos = curwin->w_cursor; |
| 9757 | cursor = &pos; |
| 9758 | saved_line = vim_strsave(ml_get_curline()); |
| 9759 | if (saved_line == NULL) |
| 9760 | return FALSE; |
| 9761 | ptr = saved_line + pos.col; |
| 9762 | } |
| 9763 | else |
| 9764 | #endif |
| 9765 | { |
| 9766 | ptr = ml_get_cursor(); |
| 9767 | cursor = &curwin->w_cursor; |
| 9768 | } |
| 9769 | |
| 9770 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 9771 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 9772 | curwin->w_p_list = FALSE; |
| 9773 | |
| 9774 | /* Find first white before the cursor */ |
| 9775 | fpos = curwin->w_cursor; |
| 9776 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 9777 | { |
| 9778 | --fpos.col; |
| 9779 | --ptr; |
| 9780 | } |
| 9781 | |
| 9782 | /* In Replace mode, don't change characters before the insert point. */ |
| 9783 | if ((State & REPLACE_FLAG) |
| 9784 | && fpos.lnum == Insstart.lnum |
| 9785 | && fpos.col < Insstart.col) |
| 9786 | { |
| 9787 | ptr += Insstart.col - fpos.col; |
| 9788 | fpos.col = Insstart.col; |
| 9789 | } |
| 9790 | |
| 9791 | /* compute virtual column numbers of first white and cursor */ |
| 9792 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 9793 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 9794 | |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9795 | /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak' |
| 9796 | * and 'linebreak' adding extra virtual columns. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9797 | while (vim_iswhite(*ptr)) |
| 9798 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9799 | i = lbr_chartabsize(NULL, (char_u *)"\t", vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9800 | if (vcol + i > want_vcol) |
| 9801 | break; |
| 9802 | if (*ptr != TAB) |
| 9803 | { |
| 9804 | *ptr = TAB; |
| 9805 | if (change_col < 0) |
| 9806 | { |
| 9807 | change_col = fpos.col; /* Column of first change */ |
| 9808 | /* May have to adjust Insstart */ |
| 9809 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 9810 | Insstart.col = fpos.col; |
| 9811 | } |
| 9812 | } |
| 9813 | ++fpos.col; |
| 9814 | ++ptr; |
| 9815 | vcol += i; |
| 9816 | } |
| 9817 | |
| 9818 | if (change_col >= 0) |
| 9819 | { |
| 9820 | int repl_off = 0; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9821 | char_u *line = ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9822 | |
| 9823 | /* Skip over the spaces we need. */ |
| 9824 | while (vcol < want_vcol && *ptr == ' ') |
| 9825 | { |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 9826 | vcol += lbr_chartabsize(line, ptr, vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9827 | ++ptr; |
| 9828 | ++repl_off; |
| 9829 | } |
| 9830 | if (vcol > want_vcol) |
| 9831 | { |
| 9832 | /* Must have a char with 'showbreak' just before it. */ |
| 9833 | --ptr; |
| 9834 | --repl_off; |
| 9835 | } |
| 9836 | fpos.col += repl_off; |
| 9837 | |
| 9838 | /* Delete following spaces. */ |
| 9839 | i = cursor->col - fpos.col; |
| 9840 | if (i > 0) |
| 9841 | { |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 9842 | STRMOVE(ptr, ptr + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9843 | /* correct replace stack. */ |
| 9844 | if ((State & REPLACE_FLAG) |
| 9845 | #ifdef FEAT_VREPLACE |
| 9846 | && !(State & VREPLACE_FLAG) |
| 9847 | #endif |
| 9848 | ) |
| 9849 | for (temp = i; --temp >= 0; ) |
| 9850 | replace_join(repl_off); |
| 9851 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9852 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 9853 | if (netbeans_active()) |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9854 | { |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 9855 | netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1)); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9856 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 9857 | (char_u *)"\t", 1); |
| 9858 | } |
| 9859 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9860 | cursor->col -= i; |
| 9861 | |
| 9862 | #ifdef FEAT_VREPLACE |
| 9863 | /* |
| 9864 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 9865 | * backspacing over the changed spacing and then inserting the new |
| 9866 | * spacing. |
| 9867 | */ |
| 9868 | if (State & VREPLACE_FLAG) |
| 9869 | { |
| 9870 | /* Backspace from real cursor to change_col */ |
| 9871 | backspace_until_column(change_col); |
| 9872 | |
| 9873 | /* Insert each char in saved_line from changed_col to |
| 9874 | * ptr-cursor */ |
| 9875 | ins_bytes_len(saved_line + change_col, |
| 9876 | cursor->col - change_col); |
| 9877 | } |
| 9878 | #endif |
| 9879 | } |
| 9880 | |
| 9881 | #ifdef FEAT_VREPLACE |
| 9882 | if (State & VREPLACE_FLAG) |
| 9883 | vim_free(saved_line); |
| 9884 | #endif |
| 9885 | curwin->w_p_list = save_list; |
| 9886 | } |
| 9887 | |
| 9888 | return FALSE; |
| 9889 | } |
| 9890 | |
| 9891 | /* |
| 9892 | * Handle CR or NL in insert mode. |
| 9893 | * Return TRUE when out of memory or can't undo. |
| 9894 | */ |
| 9895 | static int |
| 9896 | ins_eol(c) |
| 9897 | int c; |
| 9898 | { |
| 9899 | int i; |
| 9900 | |
| 9901 | if (echeck_abbr(c + ABBR_OFF)) |
| 9902 | return FALSE; |
| 9903 | if (stop_arrow() == FAIL) |
| 9904 | return TRUE; |
| 9905 | undisplay_dollar(); |
| 9906 | |
| 9907 | /* |
| 9908 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 9909 | * character under the cursor. Only push a NUL on the replace stack, |
| 9910 | * nothing to put back when the NL is deleted. |
| 9911 | */ |
| 9912 | if ((State & REPLACE_FLAG) |
| 9913 | #ifdef FEAT_VREPLACE |
| 9914 | && !(State & VREPLACE_FLAG) |
| 9915 | #endif |
| 9916 | ) |
| 9917 | replace_push(NUL); |
| 9918 | |
| 9919 | /* |
| 9920 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 9921 | * replacing the next line, so we push all of the characters left on the |
| 9922 | * line onto the replace stack. This is not done here though, it is done |
| 9923 | * in open_line(). |
| 9924 | */ |
| 9925 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 9926 | #ifdef FEAT_VIRTUALEDIT |
| 9927 | /* Put cursor on NUL if on the last char and coladd is 1 (happens after |
| 9928 | * CTRL-O). */ |
| 9929 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 9930 | coladvance(getviscol()); |
| 9931 | #endif |
| 9932 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9933 | #ifdef FEAT_RIGHTLEFT |
| 9934 | # ifdef FEAT_FKMAP |
| 9935 | if (p_altkeymap && p_fkmap) |
| 9936 | fkmap(NL); |
| 9937 | # endif |
| 9938 | /* NL in reverse insert will always start in the end of |
| 9939 | * current line. */ |
| 9940 | if (revins_on) |
| 9941 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 9942 | #endif |
| 9943 | |
| 9944 | AppendToRedobuff(NL_STR); |
| 9945 | i = open_line(FORWARD, |
| 9946 | #ifdef FEAT_COMMENTS |
| 9947 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 9948 | #endif |
| 9949 | 0, old_indent); |
| 9950 | old_indent = 0; |
| 9951 | #ifdef FEAT_CINDENT |
| 9952 | can_cindent = TRUE; |
| 9953 | #endif |
Bram Moolenaar | 6ae133b | 2006-11-01 20:25:45 +0000 | [diff] [blame] | 9954 | #ifdef FEAT_FOLDING |
| 9955 | /* When inserting a line the cursor line must never be in a closed fold. */ |
| 9956 | foldOpenCursor(); |
| 9957 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9958 | |
| 9959 | return (!i); |
| 9960 | } |
| 9961 | |
| 9962 | #ifdef FEAT_DIGRAPHS |
| 9963 | /* |
| 9964 | * Handle digraph in insert mode. |
| 9965 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 9966 | * done. |
| 9967 | */ |
| 9968 | static int |
| 9969 | ins_digraph() |
| 9970 | { |
| 9971 | int c; |
| 9972 | int cc; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9973 | int did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9974 | |
| 9975 | pc_status = PC_STATUS_UNSET; |
| 9976 | if (redrawing() && !char_avail()) |
| 9977 | { |
| 9978 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9979 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9980 | |
| 9981 | edit_putchar('?', TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9982 | did_putchar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9983 | #ifdef FEAT_CMDL_INFO |
| 9984 | add_to_showcmd_c(Ctrl_K); |
| 9985 | #endif |
| 9986 | } |
| 9987 | |
| 9988 | #ifdef USE_ON_FLY_SCROLL |
| 9989 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 9990 | #endif |
| 9991 | |
| 9992 | /* don't map the digraph chars. This also prevents the |
| 9993 | * mode message to be deleted when ESC is hit */ |
| 9994 | ++no_mapping; |
| 9995 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 9996 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9997 | --no_mapping; |
| 9998 | --allow_keys; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9999 | if (did_putchar) |
| 10000 | /* when the line fits in 'columns' the '?' is at the start of the next |
| 10001 | * line and will not be removed by the redraw */ |
| 10002 | edit_unputchar(); |
Bram Moolenaar | 26dcc7e | 2010-07-14 22:35:55 +0200 | [diff] [blame] | 10003 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10004 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 10005 | { |
| 10006 | #ifdef FEAT_CMDL_INFO |
| 10007 | clear_showcmd(); |
| 10008 | #endif |
| 10009 | insert_special(c, TRUE, FALSE); |
| 10010 | return NUL; |
| 10011 | } |
| 10012 | if (c != ESC) |
| 10013 | { |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 10014 | did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10015 | if (redrawing() && !char_avail()) |
| 10016 | { |
| 10017 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 10018 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10019 | |
| 10020 | if (char2cells(c) == 1) |
| 10021 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 10022 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10023 | edit_putchar(c, TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 10024 | did_putchar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10025 | } |
| 10026 | #ifdef FEAT_CMDL_INFO |
| 10027 | add_to_showcmd_c(c); |
| 10028 | #endif |
| 10029 | } |
| 10030 | ++no_mapping; |
| 10031 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 10032 | cc = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10033 | --no_mapping; |
| 10034 | --allow_keys; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 10035 | if (did_putchar) |
| 10036 | /* when the line fits in 'columns' the '?' is at the start of the |
| 10037 | * next line and will not be removed by a redraw */ |
| 10038 | edit_unputchar(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10039 | if (cc != ESC) |
| 10040 | { |
| 10041 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 10042 | c = getdigraph(c, cc, TRUE); |
| 10043 | #ifdef FEAT_CMDL_INFO |
| 10044 | clear_showcmd(); |
| 10045 | #endif |
| 10046 | return c; |
| 10047 | } |
| 10048 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10049 | #ifdef FEAT_CMDL_INFO |
| 10050 | clear_showcmd(); |
| 10051 | #endif |
| 10052 | return NUL; |
| 10053 | } |
| 10054 | #endif |
| 10055 | |
| 10056 | /* |
| 10057 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 10058 | * Returns the char to be inserted, or NUL if none found. |
| 10059 | */ |
Bram Moolenaar | 8320da4 | 2012-04-30 18:18:47 +0200 | [diff] [blame] | 10060 | int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10061 | ins_copychar(lnum) |
| 10062 | linenr_T lnum; |
| 10063 | { |
| 10064 | int c; |
| 10065 | int temp; |
| 10066 | char_u *ptr, *prev_ptr; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 10067 | char_u *line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10068 | |
| 10069 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 10070 | { |
| 10071 | vim_beep(); |
| 10072 | return NUL; |
| 10073 | } |
| 10074 | |
| 10075 | /* try to advance to the cursor column */ |
| 10076 | temp = 0; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 10077 | line = ptr = ml_get(lnum); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10078 | prev_ptr = ptr; |
| 10079 | validate_virtcol(); |
| 10080 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 10081 | { |
| 10082 | prev_ptr = ptr; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 10083 | temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10084 | } |
| 10085 | if ((colnr_T)temp > curwin->w_virtcol) |
| 10086 | ptr = prev_ptr; |
| 10087 | |
| 10088 | #ifdef FEAT_MBYTE |
| 10089 | c = (*mb_ptr2char)(ptr); |
| 10090 | #else |
| 10091 | c = *ptr; |
| 10092 | #endif |
| 10093 | if (c == NUL) |
| 10094 | vim_beep(); |
| 10095 | return c; |
| 10096 | } |
| 10097 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 10098 | /* |
| 10099 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 10100 | */ |
| 10101 | static int |
| 10102 | ins_ctrl_ey(tc) |
| 10103 | int tc; |
| 10104 | { |
| 10105 | int c = tc; |
| 10106 | |
| 10107 | #ifdef FEAT_INS_EXPAND |
| 10108 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 10109 | { |
| 10110 | if (c == Ctrl_Y) |
| 10111 | scrolldown_clamp(); |
| 10112 | else |
| 10113 | scrollup_clamp(); |
| 10114 | redraw_later(VALID); |
| 10115 | } |
| 10116 | else |
| 10117 | #endif |
| 10118 | { |
| 10119 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 10120 | if (c != NUL) |
| 10121 | { |
| 10122 | long tw_save; |
| 10123 | |
| 10124 | /* The character must be taken literally, insert like it |
| 10125 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 10126 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 10127 | * CTRL-V, don't use it for these. */ |
| 10128 | if (c < 256 && !isalnum(c)) |
| 10129 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 10130 | tw_save = curbuf->b_p_tw; |
| 10131 | curbuf->b_p_tw = -1; |
| 10132 | insert_special(c, TRUE, FALSE); |
| 10133 | curbuf->b_p_tw = tw_save; |
| 10134 | #ifdef FEAT_RIGHTLEFT |
| 10135 | revins_chars++; |
| 10136 | revins_legal++; |
| 10137 | #endif |
| 10138 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 10139 | auto_format(FALSE, TRUE); |
| 10140 | } |
| 10141 | } |
| 10142 | return c; |
| 10143 | } |
| 10144 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10145 | #ifdef FEAT_SMARTINDENT |
| 10146 | /* |
| 10147 | * Try to do some very smart auto-indenting. |
| 10148 | * Used when inserting a "normal" character. |
| 10149 | */ |
| 10150 | static void |
| 10151 | ins_try_si(c) |
| 10152 | int c; |
| 10153 | { |
| 10154 | pos_T *pos, old_pos; |
| 10155 | char_u *ptr; |
| 10156 | int i; |
| 10157 | int temp; |
| 10158 | |
| 10159 | /* |
| 10160 | * do some very smart indenting when entering '{' or '}' |
| 10161 | */ |
| 10162 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 10163 | { |
| 10164 | /* |
| 10165 | * for '}' set indent equal to indent of line containing matching '{' |
| 10166 | */ |
| 10167 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 10168 | { |
| 10169 | old_pos = curwin->w_cursor; |
| 10170 | /* |
| 10171 | * If the matching '{' has a ')' immediately before it (ignoring |
| 10172 | * white-space), then line up with the start of the line |
| 10173 | * containing the matching '(' if there is one. This handles the |
| 10174 | * case where an "if (..\n..) {" statement continues over multiple |
| 10175 | * lines -- webb |
| 10176 | */ |
| 10177 | ptr = ml_get(pos->lnum); |
| 10178 | i = pos->col; |
| 10179 | if (i > 0) /* skip blanks before '{' */ |
| 10180 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 10181 | ; |
| 10182 | curwin->w_cursor.lnum = pos->lnum; |
| 10183 | curwin->w_cursor.col = i; |
| 10184 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 10185 | curwin->w_cursor = *pos; |
| 10186 | i = get_indent(); |
| 10187 | curwin->w_cursor = old_pos; |
| 10188 | #ifdef FEAT_VREPLACE |
| 10189 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 10190 | change_indent(INDENT_SET, i, FALSE, NUL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10191 | else |
| 10192 | #endif |
| 10193 | (void)set_indent(i, SIN_CHANGED); |
| 10194 | } |
| 10195 | else if (curwin->w_cursor.col > 0) |
| 10196 | { |
| 10197 | /* |
| 10198 | * when inserting '{' after "O" reduce indent, but not |
| 10199 | * more than indent of previous line |
| 10200 | */ |
| 10201 | temp = TRUE; |
| 10202 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 10203 | { |
| 10204 | old_pos = curwin->w_cursor; |
| 10205 | i = get_indent(); |
| 10206 | while (curwin->w_cursor.lnum > 1) |
| 10207 | { |
| 10208 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 10209 | |
| 10210 | /* ignore empty lines and lines starting with '#'. */ |
| 10211 | if (*ptr != '#' && *ptr != NUL) |
| 10212 | break; |
| 10213 | } |
| 10214 | if (get_indent() >= i) |
| 10215 | temp = FALSE; |
| 10216 | curwin->w_cursor = old_pos; |
| 10217 | } |
| 10218 | if (temp) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 10219 | shift_line(TRUE, FALSE, 1, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10220 | } |
| 10221 | } |
| 10222 | |
| 10223 | /* |
| 10224 | * set indent of '#' always to 0 |
| 10225 | */ |
| 10226 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 10227 | { |
| 10228 | /* remember current indent for next line */ |
| 10229 | old_indent = get_indent(); |
| 10230 | (void)set_indent(0, SIN_CHANGED); |
| 10231 | } |
| 10232 | |
| 10233 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 10234 | if (ai_col > curwin->w_cursor.col) |
| 10235 | ai_col = curwin->w_cursor.col; |
| 10236 | } |
| 10237 | #endif |
| 10238 | |
| 10239 | /* |
| 10240 | * Get the value that w_virtcol would have when 'list' is off. |
| 10241 | * Unless 'cpo' contains the 'L' flag. |
| 10242 | */ |
| 10243 | static colnr_T |
| 10244 | get_nolist_virtcol() |
| 10245 | { |
| 10246 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 10247 | return getvcol_nolist(&curwin->w_cursor); |
| 10248 | validate_virtcol(); |
| 10249 | return curwin->w_virtcol; |
| 10250 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10251 | |
| 10252 | #ifdef FEAT_AUTOCMD |
| 10253 | /* |
| 10254 | * Handle the InsertCharPre autocommand. |
| 10255 | * "c" is the character that was typed. |
| 10256 | * Return a pointer to allocated memory with the replacement string. |
| 10257 | * Return NULL to continue inserting "c". |
| 10258 | */ |
| 10259 | static char_u * |
| 10260 | do_insert_char_pre(c) |
| 10261 | int c; |
| 10262 | { |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10263 | char_u *res; |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10264 | char_u buf[MB_MAXBYTES + 1]; |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10265 | |
| 10266 | /* Return quickly when there is nothing to do. */ |
| 10267 | if (!has_insertcharpre()) |
| 10268 | return NULL; |
| 10269 | |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10270 | #ifdef FEAT_MBYTE |
| 10271 | if (has_mbyte) |
| 10272 | buf[(*mb_char2bytes)(c, buf)] = NUL; |
| 10273 | else |
| 10274 | #endif |
| 10275 | { |
| 10276 | buf[0] = c; |
| 10277 | buf[1] = NUL; |
| 10278 | } |
| 10279 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10280 | /* Lock the text to avoid weird things from happening. */ |
| 10281 | ++textlock; |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10282 | set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */ |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10283 | |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10284 | res = NULL; |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10285 | if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf)) |
Bram Moolenaar | 704984a | 2012-06-01 14:57:51 +0200 | [diff] [blame] | 10286 | { |
| 10287 | /* Get the value of v:char. It may be empty or more than one |
| 10288 | * character. Only use it when changed, otherwise continue with the |
| 10289 | * original character to avoid breaking autoindent. */ |
| 10290 | if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0) |
| 10291 | res = vim_strsave(get_vim_var_str(VV_CHAR)); |
| 10292 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10293 | |
| 10294 | set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */ |
| 10295 | --textlock; |
| 10296 | |
| 10297 | return res; |
| 10298 | } |
| 10299 | #endif |