Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * edit.c: functions for Insert mode |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #ifdef FEAT_INS_EXPAND |
| 17 | /* |
| 18 | * definitions used for CTRL-X submode |
| 19 | */ |
| 20 | #define CTRL_X_WANT_IDENT 0x100 |
| 21 | |
| 22 | #define CTRL_X_NOT_DEFINED_YET 1 |
| 23 | #define CTRL_X_SCROLL 2 |
| 24 | #define CTRL_X_WHOLE_LINE 3 |
| 25 | #define CTRL_X_FILES 4 |
| 26 | #define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT) |
| 27 | #define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT) |
| 28 | #define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT) |
| 29 | #define CTRL_X_FINISHED 8 |
| 30 | #define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT) |
| 31 | #define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT) |
| 32 | #define CTRL_X_CMDLINE 11 |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 33 | #define CTRL_X_FUNCTION 12 |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 34 | #define CTRL_X_OMNI 13 |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 35 | #define CTRL_X_SPELL 14 |
| 36 | #define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 37 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | #define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT] |
| 39 | |
| 40 | static char *ctrl_x_msgs[] = |
| 41 | { |
| 42 | N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */ |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 43 | N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"), |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 44 | NULL, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 45 | N_(" Whole line completion (^L^N^P)"), |
| 46 | N_(" File name completion (^F^N^P)"), |
| 47 | N_(" Tag completion (^]^N^P)"), |
| 48 | N_(" Path pattern completion (^N^P)"), |
| 49 | N_(" Definition completion (^D^N^P)"), |
| 50 | NULL, |
| 51 | N_(" Dictionary completion (^K^N^P)"), |
| 52 | N_(" Thesaurus completion (^T^N^P)"), |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 53 | N_(" Command-line completion (^V^N^P)"), |
| 54 | N_(" User defined completion (^U^N^P)"), |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 55 | N_(" Omni completion (^O^N^P)"), |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 56 | N_(" Spelling suggestion (s^N^P)"), |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 57 | N_(" Keyword Local completion (^N^P)"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 60 | static char e_hitend[] = N_("Hit end of paragraph"); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 61 | #ifdef FEAT_COMPL_FUNC |
| 62 | static char e_complwin[] = N_("E839: Completion function changed window"); |
| 63 | static char e_compldel[] = N_("E840: Completion function deleted text"); |
| 64 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Structure used to store one match for insert completion. |
| 68 | */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 69 | typedef struct compl_S compl_T; |
| 70 | struct compl_S |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 72 | compl_T *cp_next; |
| 73 | compl_T *cp_prev; |
| 74 | char_u *cp_str; /* matched text */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 75 | char cp_icase; /* TRUE or FALSE: ignore case */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 76 | char_u *(cp_text[CPT_COUNT]); /* text for the menu */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 77 | char_u *cp_fname; /* file containing the match, allocated when |
| 78 | * cp_flags has FREE_FNAME */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 79 | int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */ |
| 80 | int cp_number; /* sequence number */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 83 | #define ORIGINAL_TEXT (1) /* the original text when the expansion begun */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 84 | #define FREE_FNAME (2) |
| 85 | |
| 86 | /* |
| 87 | * All the current matches are stored in a list. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 88 | * "compl_first_match" points to the start of the list. |
| 89 | * "compl_curr_match" points to the currently selected entry. |
| 90 | * "compl_shown_match" is different from compl_curr_match during |
| 91 | * ins_compl_get_exp(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 92 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 93 | static compl_T *compl_first_match = NULL; |
| 94 | static compl_T *compl_curr_match = NULL; |
| 95 | static compl_T *compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 97 | /* After using a cursor key <Enter> selects a match in the popup menu, |
| 98 | * otherwise it inserts a line break. */ |
| 99 | static int compl_enter_selects = FALSE; |
| 100 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 101 | /* When "compl_leader" is not NULL only matches that start with this string |
| 102 | * are used. */ |
| 103 | static char_u *compl_leader = NULL; |
| 104 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 105 | static int compl_get_longest = FALSE; /* put longest common string |
| 106 | in compl_leader */ |
| 107 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 108 | static int compl_used_match; /* Selected one of the matches. When |
| 109 | FALSE the match was edited or using |
| 110 | the longest common string. */ |
| 111 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 112 | static int compl_was_interrupted = FALSE; /* didn't finish finding |
| 113 | completions. */ |
| 114 | |
| 115 | static int compl_restarting = FALSE; /* don't insert match */ |
| 116 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 117 | /* When the first completion is done "compl_started" is set. When it's |
| 118 | * FALSE the word to be completed must be located. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 119 | static int compl_started = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 120 | |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 121 | /* Set when doing something for completion that may call edit() recursively, |
| 122 | * which is not allowed. */ |
| 123 | static int compl_busy = FALSE; |
| 124 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 125 | static int compl_matches = 0; |
| 126 | static char_u *compl_pattern = NULL; |
| 127 | static int compl_direction = FORWARD; |
| 128 | static int compl_shows_dir = FORWARD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 129 | static int compl_pending = 0; /* > 1 for postponed CTRL-N */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 130 | static pos_T compl_startpos; |
| 131 | static colnr_T compl_col = 0; /* column where the text starts |
| 132 | * that is being completed */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 133 | static char_u *compl_orig_text = NULL; /* text as it was before |
| 134 | * completion started */ |
| 135 | static int compl_cont_mode = 0; |
| 136 | static expand_T compl_xp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 137 | |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 138 | static int compl_opt_refresh_always = FALSE; |
| 139 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 140 | static void ins_ctrl_x __ARGS((void)); |
| 141 | static int has_compl_option __ARGS((int dict_opt)); |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 142 | static int ins_compl_accept_char __ARGS((int c)); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 143 | static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 144 | static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 145 | static void ins_compl_longest_match __ARGS((compl_T *match)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 146 | static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 147 | static int ins_compl_make_cyclic __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 148 | static void ins_compl_upd_pum __ARGS((void)); |
| 149 | static void ins_compl_del_pum __ARGS((void)); |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 150 | static int pum_wanted __ARGS((void)); |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 151 | static int pum_enough_matches __ARGS((void)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 152 | static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus)); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 153 | static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 154 | static char_u *find_line_end __ARGS((char_u *ptr)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | static void ins_compl_free __ARGS((void)); |
| 156 | static void ins_compl_clear __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 157 | static int ins_compl_bs __ARGS((void)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 158 | static int ins_compl_need_restart __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 159 | static void ins_compl_new_leader __ARGS((void)); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 160 | static void ins_compl_addleader __ARGS((int c)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 161 | static int ins_compl_len __ARGS((void)); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 162 | static void ins_compl_restart __ARGS((void)); |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 163 | static void ins_compl_set_original_text __ARGS((char_u *str)); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 164 | static void ins_compl_addfrommatch __ARGS((void)); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 165 | static int ins_compl_prep __ARGS((int c)); |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 166 | static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 167 | static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag)); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 168 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) |
| 169 | static void ins_compl_add_list __ARGS((list_T *list)); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 170 | static void ins_compl_add_dict __ARGS((dict_T *dict)); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 171 | #endif |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 172 | static int ins_compl_get_exp __ARGS((pos_T *ini)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 173 | static void ins_compl_delete __ARGS((void)); |
| 174 | static void ins_compl_insert __ARGS((void)); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 175 | static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 176 | static int ins_compl_key2dir __ARGS((int c)); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 177 | static int ins_compl_pum_key __ARGS((int c)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 178 | static int ins_compl_key2count __ARGS((int c)); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 179 | static int ins_compl_use_match __ARGS((int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | static int ins_complete __ARGS((int c)); |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 181 | static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | #endif /* FEAT_INS_EXPAND */ |
| 183 | |
| 184 | #define BACKSPACE_CHAR 1 |
| 185 | #define BACKSPACE_WORD 2 |
| 186 | #define BACKSPACE_WORD_NOT_SPACE 3 |
| 187 | #define BACKSPACE_LINE 4 |
| 188 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 189 | static void ins_redraw __ARGS((int ready)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | static void ins_ctrl_v __ARGS((void)); |
| 191 | static void undisplay_dollar __ARGS((void)); |
| 192 | static void insert_special __ARGS((int, int, int)); |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 193 | static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | static void check_auto_format __ARGS((int)); |
| 195 | static void redo_literal __ARGS((int c)); |
| 196 | static void start_arrow __ARGS((pos_T *end_insert_pos)); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 197 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 198 | static void check_spell_redraw __ARGS((void)); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 199 | static void spell_back_to_badword __ARGS((void)); |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 200 | static int spell_bad_len = 0; /* length of located bad word */ |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 201 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | static void stop_insert __ARGS((pos_T *end_insert_pos, int esc)); |
| 203 | static int echeck_abbr __ARGS((int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | static int replace_pop __ARGS((void)); |
| 205 | static void replace_join __ARGS((int off)); |
| 206 | static void replace_pop_ins __ARGS((void)); |
| 207 | #ifdef FEAT_MBYTE |
| 208 | static void mb_replace_pop_ins __ARGS((int cc)); |
| 209 | #endif |
| 210 | static void replace_flush __ARGS((void)); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 211 | static void replace_do_bs __ARGS((int limit_col)); |
| 212 | static int del_char_after_col __ARGS((int limit_col)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | #ifdef FEAT_CINDENT |
| 214 | static int cindent_on __ARGS((void)); |
| 215 | #endif |
| 216 | static void ins_reg __ARGS((void)); |
| 217 | static void ins_ctrl_g __ARGS((void)); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 218 | static void ins_ctrl_hat __ARGS((void)); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 219 | static int ins_esc __ARGS((long *count, int cmdchar, int nomove)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 220 | #ifdef FEAT_RIGHTLEFT |
| 221 | static void ins_ctrl_ __ARGS((void)); |
| 222 | #endif |
| 223 | #ifdef FEAT_VISUAL |
| 224 | static int ins_start_select __ARGS((int c)); |
| 225 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 226 | static void ins_insert __ARGS((int replaceState)); |
| 227 | static void ins_ctrl_o __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | static void ins_shift __ARGS((int c, int lastc)); |
| 229 | static void ins_del __ARGS((void)); |
| 230 | static int ins_bs __ARGS((int c, int mode, int *inserted_space_p)); |
| 231 | #ifdef FEAT_MOUSE |
| 232 | static void ins_mouse __ARGS((int c)); |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 233 | static void ins_mousescroll __ARGS((int dir)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 234 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 235 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
| 236 | static void ins_tabline __ARGS((int c)); |
| 237 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 238 | static void ins_left __ARGS((void)); |
| 239 | static void ins_home __ARGS((int c)); |
| 240 | static void ins_end __ARGS((int c)); |
| 241 | static void ins_s_left __ARGS((void)); |
| 242 | static void ins_right __ARGS((void)); |
| 243 | static void ins_s_right __ARGS((void)); |
| 244 | static void ins_up __ARGS((int startcol)); |
| 245 | static void ins_pageup __ARGS((void)); |
| 246 | static void ins_down __ARGS((int startcol)); |
| 247 | static void ins_pagedown __ARGS((void)); |
| 248 | #ifdef FEAT_DND |
| 249 | static void ins_drop __ARGS((void)); |
| 250 | #endif |
| 251 | static int ins_tab __ARGS((void)); |
| 252 | static int ins_eol __ARGS((int c)); |
| 253 | #ifdef FEAT_DIGRAPHS |
| 254 | static int ins_digraph __ARGS((void)); |
| 255 | #endif |
| 256 | static int ins_copychar __ARGS((linenr_T lnum)); |
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 */ |
| 268 | |
| 269 | static char_u *last_insert = NULL; /* the text of the previous insert, |
| 270 | K_SPECIAL and CSI are escaped */ |
| 271 | static int last_insert_skip; /* nr of chars in front of previous insert */ |
| 272 | 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] | 273 | static int did_restart_edit; /* "restart_edit" when calling edit() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | |
| 275 | #ifdef FEAT_CINDENT |
| 276 | static int can_cindent; /* may do cindenting on this line */ |
| 277 | #endif |
| 278 | |
| 279 | static int old_indent = 0; /* for ^^D command in insert mode */ |
| 280 | |
| 281 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 282 | static int revins_on; /* reverse insert mode on */ |
| 283 | static int revins_chars; /* how much to skip after edit */ |
| 284 | static int revins_legal; /* was the last char 'legal'? */ |
| 285 | static int revins_scol; /* start column of revins session */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 286 | #endif |
| 287 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 288 | static int ins_need_undo; /* call u_save() before inserting a |
| 289 | char. Set when edit() is called. |
| 290 | after that arrow_used is used. */ |
| 291 | |
| 292 | static int did_add_space = FALSE; /* auto_format() added an extra space |
| 293 | under the cursor */ |
| 294 | |
| 295 | /* |
| 296 | * edit(): Start inserting text. |
| 297 | * |
| 298 | * "cmdchar" can be: |
| 299 | * 'i' normal insert command |
| 300 | * 'a' normal append command |
| 301 | * 'R' replace command |
| 302 | * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo, |
| 303 | * but still only one <CR> is inserted. The <Esc> is not used for redo. |
| 304 | * 'g' "gI" command. |
| 305 | * 'V' "gR" command for Virtual Replace mode. |
| 306 | * 'v' "gr" command for single character Virtual Replace mode. |
| 307 | * |
| 308 | * This function is not called recursively. For CTRL-O commands, it returns |
| 309 | * and lets the caller handle the Normal-mode command. |
| 310 | * |
| 311 | * Return TRUE if a CTRL-O command caused the return (insert mode pending). |
| 312 | */ |
| 313 | int |
| 314 | edit(cmdchar, startln, count) |
| 315 | int cmdchar; |
| 316 | int startln; /* if set, insert at start of line */ |
| 317 | long count; |
| 318 | { |
| 319 | int c = 0; |
| 320 | char_u *ptr; |
| 321 | int lastc; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 322 | int mincol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 323 | static linenr_T o_lnum = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | int i; |
| 325 | int did_backspace = TRUE; /* previous char was backspace */ |
| 326 | #ifdef FEAT_CINDENT |
| 327 | int line_is_white = FALSE; /* line is empty before insert */ |
| 328 | #endif |
| 329 | linenr_T old_topline = 0; /* topline before insertion */ |
| 330 | #ifdef FEAT_DIFF |
| 331 | int old_topfill = -1; |
| 332 | #endif |
| 333 | int inserted_space = FALSE; /* just inserted a space */ |
| 334 | int replaceState = REPLACE; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 335 | int nomove = FALSE; /* don't move cursor on return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 336 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 337 | /* Remember whether editing was restarted after CTRL-O. */ |
| 338 | did_restart_edit = restart_edit; |
| 339 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 340 | /* sleep before redrawing, needed for "CTRL-O :" that results in an |
| 341 | * error message */ |
| 342 | check_for_delay(TRUE); |
| 343 | |
| 344 | #ifdef HAVE_SANDBOX |
| 345 | /* Don't allow inserting in the sandbox. */ |
| 346 | if (sandbox != 0) |
| 347 | { |
| 348 | EMSG(_(e_sandbox)); |
| 349 | return FALSE; |
| 350 | } |
| 351 | #endif |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 352 | /* Don't allow changes in the buffer while editing the cmdline. The |
| 353 | * caller of getcmdline() may get confused. */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 354 | if (textlock != 0) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 355 | { |
| 356 | EMSG(_(e_secure)); |
| 357 | return FALSE; |
| 358 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | |
| 360 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 361 | /* Don't allow recursive insert mode when busy with completion. */ |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 362 | if (compl_started || compl_busy || pum_visible()) |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 363 | { |
| 364 | EMSG(_(e_secure)); |
| 365 | return FALSE; |
| 366 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | ins_compl_clear(); /* clear stuff for CTRL-X mode */ |
| 368 | #endif |
| 369 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 370 | #ifdef FEAT_AUTOCMD |
| 371 | /* |
| 372 | * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx". |
| 373 | */ |
| 374 | if (cmdchar != 'r' && cmdchar != 'v') |
| 375 | { |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 376 | # ifdef FEAT_EVAL |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 377 | if (cmdchar == 'R') |
| 378 | ptr = (char_u *)"r"; |
| 379 | else if (cmdchar == 'V') |
| 380 | ptr = (char_u *)"v"; |
| 381 | else |
| 382 | ptr = (char_u *)"i"; |
| 383 | set_vim_var_string(VV_INSERTMODE, ptr, 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 384 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 385 | apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf); |
| 386 | } |
| 387 | #endif |
| 388 | |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 389 | #ifdef FEAT_CONCEAL |
| 390 | /* Check if the cursor line needs redrawing before changing State. If |
| 391 | * 'concealcursor' is "n" it needs to be redrawn without concealing. */ |
Bram Moolenaar | 8e46927 | 2010-07-28 19:38:16 +0200 | [diff] [blame] | 392 | conceal_check_cursur_line(); |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 393 | #endif |
| 394 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 395 | #ifdef FEAT_MOUSE |
| 396 | /* |
| 397 | * When doing a paste with the middle mouse button, Insstart is set to |
| 398 | * where the paste started. |
| 399 | */ |
| 400 | if (where_paste_started.lnum != 0) |
| 401 | Insstart = where_paste_started; |
| 402 | else |
| 403 | #endif |
| 404 | { |
| 405 | Insstart = curwin->w_cursor; |
| 406 | if (startln) |
| 407 | Insstart.col = 0; |
| 408 | } |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 409 | Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 410 | Insstart_blank_vcol = MAXCOL; |
| 411 | if (!did_ai) |
| 412 | ai_col = 0; |
| 413 | |
| 414 | if (cmdchar != NUL && restart_edit == 0) |
| 415 | { |
| 416 | ResetRedobuff(); |
| 417 | AppendNumberToRedobuff(count); |
| 418 | #ifdef FEAT_VREPLACE |
| 419 | if (cmdchar == 'V' || cmdchar == 'v') |
| 420 | { |
| 421 | /* "gR" or "gr" command */ |
| 422 | AppendCharToRedobuff('g'); |
| 423 | AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R'); |
| 424 | } |
| 425 | else |
| 426 | #endif |
| 427 | { |
| 428 | AppendCharToRedobuff(cmdchar); |
| 429 | if (cmdchar == 'g') /* "gI" command */ |
| 430 | AppendCharToRedobuff('I'); |
| 431 | else if (cmdchar == 'r') /* "r<CR>" command */ |
| 432 | count = 1; /* insert only one <CR> */ |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | if (cmdchar == 'R') |
| 437 | { |
| 438 | #ifdef FEAT_FKMAP |
| 439 | if (p_fkmap && p_ri) |
| 440 | { |
| 441 | beep_flush(); |
| 442 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 443 | State = INSERT; |
| 444 | } |
| 445 | else |
| 446 | #endif |
| 447 | State = REPLACE; |
| 448 | } |
| 449 | #ifdef FEAT_VREPLACE |
| 450 | else if (cmdchar == 'V' || cmdchar == 'v') |
| 451 | { |
| 452 | State = VREPLACE; |
| 453 | replaceState = VREPLACE; |
| 454 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 455 | vr_lines_changed = 1; |
| 456 | } |
| 457 | #endif |
| 458 | else |
| 459 | State = INSERT; |
| 460 | |
| 461 | stop_insert_mode = FALSE; |
| 462 | |
| 463 | /* |
| 464 | * Need to recompute the cursor position, it might move when the cursor is |
| 465 | * on a TAB or special character. |
| 466 | */ |
| 467 | curs_columns(TRUE); |
| 468 | |
| 469 | /* |
| 470 | * Enable langmap or IME, indicated by 'iminsert'. |
| 471 | * Note that IME may enabled/disabled without us noticing here, thus the |
| 472 | * 'iminsert' value may not reflect what is actually used. It is updated |
| 473 | * when hitting <Esc>. |
| 474 | */ |
| 475 | if (curbuf->b_p_iminsert == B_IMODE_LMAP) |
| 476 | State |= LANGMAP; |
| 477 | #ifdef USE_IM_CONTROL |
| 478 | im_set_active(curbuf->b_p_iminsert == B_IMODE_IM); |
| 479 | #endif |
| 480 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 481 | #ifdef FEAT_MOUSE |
| 482 | setmouse(); |
| 483 | #endif |
| 484 | #ifdef FEAT_CMDL_INFO |
| 485 | clear_showcmd(); |
| 486 | #endif |
| 487 | #ifdef FEAT_RIGHTLEFT |
| 488 | /* there is no reverse replace mode */ |
| 489 | revins_on = (State == INSERT && p_ri); |
| 490 | if (revins_on) |
| 491 | undisplay_dollar(); |
| 492 | revins_chars = 0; |
| 493 | revins_legal = 0; |
| 494 | revins_scol = -1; |
| 495 | #endif |
| 496 | |
| 497 | /* |
| 498 | * Handle restarting Insert mode. |
| 499 | * Don't do this for "CTRL-O ." (repeat an insert): we get here with |
| 500 | * restart_edit non-zero, and something in the stuff buffer. |
| 501 | */ |
| 502 | if (restart_edit != 0 && stuff_empty()) |
| 503 | { |
| 504 | #ifdef FEAT_MOUSE |
| 505 | /* |
| 506 | * After a paste we consider text typed to be part of the insert for |
| 507 | * the pasted text. You can backspace over the pasted text too. |
| 508 | */ |
| 509 | if (where_paste_started.lnum) |
| 510 | arrow_used = FALSE; |
| 511 | else |
| 512 | #endif |
| 513 | arrow_used = TRUE; |
| 514 | restart_edit = 0; |
| 515 | |
| 516 | /* |
| 517 | * If the cursor was after the end-of-line before the CTRL-O and it is |
| 518 | * now at the end-of-line, put it after the end-of-line (this is not |
| 519 | * correct in very rare cases). |
| 520 | * Also do this if curswant is greater than the current virtual |
| 521 | * column. Eg after "^O$" or "^O80|". |
| 522 | */ |
| 523 | validate_virtcol(); |
| 524 | update_curswant(); |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 525 | if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 526 | || curwin->w_curswant > curwin->w_virtcol) |
| 527 | && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL) |
| 528 | { |
| 529 | if (ptr[1] == NUL) |
| 530 | ++curwin->w_cursor.col; |
| 531 | #ifdef FEAT_MBYTE |
| 532 | else if (has_mbyte) |
| 533 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 534 | i = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 535 | if (ptr[i] == NUL) |
| 536 | curwin->w_cursor.col += i; |
| 537 | } |
| 538 | #endif |
| 539 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 540 | ins_at_eol = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 541 | } |
| 542 | else |
| 543 | arrow_used = FALSE; |
| 544 | |
| 545 | /* we are in insert mode now, don't need to start it anymore */ |
| 546 | need_start_insertmode = FALSE; |
| 547 | |
| 548 | /* Need to save the line for undo before inserting the first char. */ |
| 549 | ins_need_undo = TRUE; |
| 550 | |
| 551 | #ifdef FEAT_MOUSE |
| 552 | where_paste_started.lnum = 0; |
| 553 | #endif |
| 554 | #ifdef FEAT_CINDENT |
| 555 | can_cindent = TRUE; |
| 556 | #endif |
| 557 | #ifdef FEAT_FOLDING |
| 558 | /* The cursor line is not in a closed fold, unless 'insertmode' is set or |
| 559 | * restarting. */ |
| 560 | if (!p_im && did_restart_edit == 0) |
| 561 | foldOpenCursor(); |
| 562 | #endif |
| 563 | |
| 564 | /* |
| 565 | * If 'showmode' is set, show the current (insert/replace/..) mode. |
| 566 | * A warning message for changing a readonly file is given here, before |
| 567 | * actually changing anything. It's put after the mode, if any. |
| 568 | */ |
| 569 | i = 0; |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 570 | if (p_smd && msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 571 | i = showmode(); |
| 572 | |
| 573 | if (!p_im && did_restart_edit == 0) |
Bram Moolenaar | 21af89e | 2008-01-02 21:09:33 +0000 | [diff] [blame] | 574 | change_warning(i == 0 ? 0 : i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 575 | |
| 576 | #ifdef CURSOR_SHAPE |
| 577 | ui_cursor_shape(); /* may show different cursor shape */ |
| 578 | #endif |
| 579 | #ifdef FEAT_DIGRAPHS |
| 580 | do_digraph(-1); /* clear digraphs */ |
| 581 | #endif |
| 582 | |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 583 | /* |
| 584 | * Get the current length of the redo buffer, those characters have to be |
| 585 | * skipped if we want to get to the inserted characters. |
| 586 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 587 | ptr = get_inserted(); |
| 588 | if (ptr == NULL) |
| 589 | new_insert_skip = 0; |
| 590 | else |
| 591 | { |
| 592 | new_insert_skip = (int)STRLEN(ptr); |
| 593 | vim_free(ptr); |
| 594 | } |
| 595 | |
| 596 | old_indent = 0; |
| 597 | |
| 598 | /* |
| 599 | * Main loop in Insert mode: repeat until Insert mode is left. |
| 600 | */ |
| 601 | for (;;) |
| 602 | { |
| 603 | #ifdef FEAT_RIGHTLEFT |
| 604 | if (!revins_legal) |
| 605 | revins_scol = -1; /* reset on illegal motions */ |
| 606 | else |
| 607 | revins_legal = 0; |
| 608 | #endif |
| 609 | if (arrow_used) /* don't repeat insert when arrow key used */ |
| 610 | count = 0; |
| 611 | |
| 612 | if (stop_insert_mode) |
| 613 | { |
| 614 | /* ":stopinsert" used or 'insertmode' reset */ |
| 615 | count = 0; |
| 616 | goto doESCkey; |
| 617 | } |
| 618 | |
| 619 | /* set curwin->w_curswant for next K_DOWN or K_UP */ |
| 620 | if (!arrow_used) |
| 621 | curwin->w_set_curswant = TRUE; |
| 622 | |
| 623 | /* If there is no typeahead may check for timestamps (e.g., for when a |
| 624 | * menu invoked a shell command). */ |
| 625 | if (stuff_empty()) |
| 626 | { |
| 627 | did_check_timestamps = FALSE; |
| 628 | if (need_check_timestamps) |
| 629 | check_timestamps(FALSE); |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * When emsg() was called msg_scroll will have been set. |
| 634 | */ |
| 635 | msg_scroll = FALSE; |
| 636 | |
| 637 | #ifdef FEAT_GUI |
| 638 | /* When 'mousefocus' is set a mouse movement may have taken us to |
| 639 | * another window. "need_mouse_correct" may then be set because of an |
| 640 | * autocommand. */ |
| 641 | if (need_mouse_correct) |
| 642 | gui_mouse_correct(); |
| 643 | #endif |
| 644 | |
| 645 | #ifdef FEAT_FOLDING |
| 646 | /* Open fold at the cursor line, according to 'foldopen'. */ |
| 647 | if (fdo_flags & FDO_INSERT) |
| 648 | foldOpenCursor(); |
| 649 | /* Close folds where the cursor isn't, according to 'foldclose' */ |
| 650 | if (!char_avail()) |
| 651 | foldCheckClose(); |
| 652 | #endif |
| 653 | |
| 654 | /* |
| 655 | * If we inserted a character at the last position of the last line in |
| 656 | * the window, scroll the window one line up. This avoids an extra |
| 657 | * redraw. |
| 658 | * This is detected when the cursor column is smaller after inserting |
| 659 | * something. |
| 660 | * Don't do this when the topline changed already, it has |
| 661 | * already been adjusted (by insertchar() calling open_line())). |
| 662 | */ |
| 663 | if (curbuf->b_mod_set |
| 664 | && curwin->w_p_wrap |
| 665 | && !did_backspace |
| 666 | && curwin->w_topline == old_topline |
| 667 | #ifdef FEAT_DIFF |
| 668 | && curwin->w_topfill == old_topfill |
| 669 | #endif |
| 670 | ) |
| 671 | { |
| 672 | mincol = curwin->w_wcol; |
| 673 | validate_cursor_col(); |
| 674 | |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 675 | if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 676 | && curwin->w_wrow == W_WINROW(curwin) |
| 677 | + curwin->w_height - 1 - p_so |
| 678 | && (curwin->w_cursor.lnum != curwin->w_topline |
| 679 | #ifdef FEAT_DIFF |
| 680 | || curwin->w_topfill > 0 |
| 681 | #endif |
| 682 | )) |
| 683 | { |
| 684 | #ifdef FEAT_DIFF |
| 685 | if (curwin->w_topfill > 0) |
| 686 | --curwin->w_topfill; |
| 687 | else |
| 688 | #endif |
| 689 | #ifdef FEAT_FOLDING |
| 690 | if (hasFolding(curwin->w_topline, NULL, &old_topline)) |
| 691 | set_topline(curwin, old_topline + 1); |
| 692 | else |
| 693 | #endif |
| 694 | set_topline(curwin, curwin->w_topline + 1); |
| 695 | } |
| 696 | } |
| 697 | |
| 698 | /* May need to adjust w_topline to show the cursor. */ |
| 699 | update_topline(); |
| 700 | |
| 701 | did_backspace = FALSE; |
| 702 | |
| 703 | validate_cursor(); /* may set must_redraw */ |
| 704 | |
| 705 | /* |
| 706 | * Redraw the display when no characters are waiting. |
| 707 | * Also shows mode, ruler and positions cursor. |
| 708 | */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 709 | ins_redraw(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 710 | |
| 711 | #ifdef FEAT_SCROLLBIND |
| 712 | if (curwin->w_p_scb) |
| 713 | do_check_scrollbind(TRUE); |
| 714 | #endif |
| 715 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 716 | #ifdef FEAT_CURSORBIND |
| 717 | if (curwin->w_p_crb) |
| 718 | do_check_cursorbind(); |
| 719 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 720 | update_curswant(); |
| 721 | old_topline = curwin->w_topline; |
| 722 | #ifdef FEAT_DIFF |
| 723 | old_topfill = curwin->w_topfill; |
| 724 | #endif |
| 725 | |
| 726 | #ifdef USE_ON_FLY_SCROLL |
| 727 | dont_scroll = FALSE; /* allow scrolling here */ |
| 728 | #endif |
| 729 | |
| 730 | /* |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 731 | * Get a character for Insert mode. Ignore K_IGNORE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | */ |
| 733 | lastc = c; /* remember previous char for CTRL-D */ |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 734 | do |
| 735 | { |
| 736 | c = safe_vgetc(); |
| 737 | } while (c == K_IGNORE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 738 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 739 | #ifdef FEAT_AUTOCMD |
| 740 | /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */ |
| 741 | did_cursorhold = TRUE; |
| 742 | #endif |
| 743 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 744 | #ifdef FEAT_RIGHTLEFT |
| 745 | if (p_hkmap && KeyTyped) |
| 746 | c = hkmap(c); /* Hebrew mode mapping */ |
| 747 | #endif |
| 748 | #ifdef FEAT_FKMAP |
| 749 | if (p_fkmap && KeyTyped) |
| 750 | c = fkmap(c); /* Farsi mode mapping */ |
| 751 | #endif |
| 752 | |
| 753 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 754 | /* |
| 755 | * Special handling of keys while the popup menu is visible or wanted |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 756 | * and the cursor is still in the completed word. Only when there is |
| 757 | * a match, skip this when no matches were found. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 758 | */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 759 | if (compl_started |
| 760 | && pum_wanted() |
| 761 | && curwin->w_cursor.col >= compl_col |
| 762 | && (compl_shown_match == NULL |
| 763 | || compl_shown_match != compl_shown_match->cp_next)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 764 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 765 | /* BS: Delete one character from "compl_leader". */ |
| 766 | if ((c == K_BS || c == Ctrl_H) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 767 | && curwin->w_cursor.col > compl_col |
| 768 | && (c = ins_compl_bs()) == NUL) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 769 | continue; |
| 770 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 771 | /* When no match was selected or it was edited. */ |
| 772 | if (!compl_used_match) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 773 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 774 | /* CTRL-L: Add one character from the current match to |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 775 | * "compl_leader". Except when at the original match and |
| 776 | * there is nothing to add, CTRL-L works like CTRL-P then. */ |
| 777 | if (c == Ctrl_L |
| 778 | && (ctrl_x_mode != CTRL_X_WHOLE_LINE |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 779 | || (int)STRLEN(compl_shown_match->cp_str) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 780 | > curwin->w_cursor.col - compl_col)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 781 | { |
| 782 | ins_compl_addfrommatch(); |
| 783 | continue; |
| 784 | } |
| 785 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 786 | /* A non-white character that fits in with the current |
| 787 | * completion: Add to "compl_leader". */ |
| 788 | if (ins_compl_accept_char(c)) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 789 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 790 | #ifdef FEAT_AUTOCMD |
| 791 | /* Trigger InsertCharPre. */ |
| 792 | char_u *str = do_insert_char_pre(c); |
| 793 | char_u *p; |
| 794 | |
| 795 | if (str != NULL) |
| 796 | { |
| 797 | for (p = str; *p != NUL; mb_ptr_adv(p)) |
| 798 | ins_compl_addleader(PTR2CHAR(p)); |
| 799 | vim_free(str); |
| 800 | } |
| 801 | else |
| 802 | #endif |
| 803 | ins_compl_addleader(c); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 804 | continue; |
| 805 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 806 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 807 | /* Pressing CTRL-Y selects the current match. When |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 808 | * compl_enter_selects is set the Enter key does the same. */ |
| 809 | if (c == Ctrl_Y || (compl_enter_selects |
| 810 | && (c == CAR || c == K_KENTER || c == NL))) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 811 | { |
| 812 | ins_compl_delete(); |
| 813 | ins_compl_insert(); |
| 814 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 815 | } |
| 816 | } |
| 817 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 818 | /* Prepare for or stop CTRL-X mode. This doesn't do completion, but |
| 819 | * it does fix up the text when finishing completion. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 820 | compl_get_longest = FALSE; |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 821 | if (ins_compl_prep(c)) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 822 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 823 | #endif |
| 824 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 825 | /* CTRL-\ CTRL-N goes to Normal mode, |
| 826 | * CTRL-\ CTRL-G goes to mode selected with 'insertmode', |
| 827 | * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 828 | if (c == Ctrl_BSL) |
| 829 | { |
| 830 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 831 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 832 | ++no_mapping; |
| 833 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 834 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 835 | --no_mapping; |
| 836 | --allow_keys; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 837 | if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 838 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 839 | /* it's something else */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | vungetc(c); |
| 841 | c = Ctrl_BSL; |
| 842 | } |
| 843 | else if (c == Ctrl_G && p_im) |
| 844 | continue; |
| 845 | else |
| 846 | { |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 847 | if (c == Ctrl_O) |
| 848 | { |
| 849 | ins_ctrl_o(); |
| 850 | ins_at_eol = FALSE; /* cursor keeps its column */ |
| 851 | nomove = TRUE; |
| 852 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | count = 0; |
| 854 | goto doESCkey; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | #ifdef FEAT_DIGRAPHS |
| 859 | c = do_digraph(c); |
| 860 | #endif |
| 861 | |
| 862 | #ifdef FEAT_INS_EXPAND |
| 863 | if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE) |
| 864 | goto docomplete; |
| 865 | #endif |
| 866 | if (c == Ctrl_V || c == Ctrl_Q) |
| 867 | { |
| 868 | ins_ctrl_v(); |
| 869 | c = Ctrl_V; /* pretend CTRL-V is last typed character */ |
| 870 | continue; |
| 871 | } |
| 872 | |
| 873 | #ifdef FEAT_CINDENT |
| 874 | if (cindent_on() |
| 875 | # ifdef FEAT_INS_EXPAND |
| 876 | && ctrl_x_mode == 0 |
| 877 | # endif |
| 878 | ) |
| 879 | { |
| 880 | /* A key name preceded by a bang means this key is not to be |
| 881 | * inserted. Skip ahead to the re-indenting below. |
| 882 | * A key name preceded by a star means that indenting has to be |
| 883 | * done before inserting the key. */ |
| 884 | line_is_white = inindent(0); |
| 885 | if (in_cinkeys(c, '!', line_is_white)) |
| 886 | goto force_cindent; |
| 887 | if (can_cindent && in_cinkeys(c, '*', line_is_white) |
| 888 | && stop_arrow() == OK) |
| 889 | do_c_expr_indent(); |
| 890 | } |
| 891 | #endif |
| 892 | |
| 893 | #ifdef FEAT_RIGHTLEFT |
| 894 | if (curwin->w_p_rl) |
| 895 | switch (c) |
| 896 | { |
| 897 | case K_LEFT: c = K_RIGHT; break; |
| 898 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 899 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 900 | case K_RIGHT: c = K_LEFT; break; |
| 901 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 902 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 903 | } |
| 904 | #endif |
| 905 | |
| 906 | #ifdef FEAT_VISUAL |
| 907 | /* |
| 908 | * If 'keymodel' contains "startsel", may start selection. If it |
| 909 | * does, a CTRL-O and c will be stuffed, we need to get these |
| 910 | * characters. |
| 911 | */ |
| 912 | if (ins_start_select(c)) |
| 913 | continue; |
| 914 | #endif |
| 915 | |
| 916 | /* |
| 917 | * The big switch to handle a character in insert mode. |
| 918 | */ |
| 919 | switch (c) |
| 920 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 921 | case ESC: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 922 | if (echeck_abbr(ESC + ABBR_OFF)) |
| 923 | break; |
| 924 | /*FALLTHROUGH*/ |
| 925 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 926 | case Ctrl_C: /* End input mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 927 | #ifdef FEAT_CMDWIN |
| 928 | if (c == Ctrl_C && cmdwin_type != 0) |
| 929 | { |
| 930 | /* Close the cmdline window. */ |
| 931 | cmdwin_result = K_IGNORE; |
| 932 | got_int = FALSE; /* don't stop executing autocommands et al. */ |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 933 | nomove = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 934 | goto doESCkey; |
| 935 | } |
| 936 | #endif |
| 937 | |
| 938 | #ifdef UNIX |
| 939 | do_intr: |
| 940 | #endif |
| 941 | /* when 'insertmode' set, and not halfway a mapping, don't leave |
| 942 | * Insert mode */ |
| 943 | if (goto_im()) |
| 944 | { |
| 945 | if (got_int) |
| 946 | { |
| 947 | (void)vgetc(); /* flush all buffers */ |
| 948 | got_int = FALSE; |
| 949 | } |
| 950 | else |
| 951 | vim_beep(); |
| 952 | break; |
| 953 | } |
| 954 | doESCkey: |
| 955 | /* |
| 956 | * This is the ONLY return from edit()! |
| 957 | */ |
| 958 | /* Always update o_lnum, so that a "CTRL-O ." that adds a line |
| 959 | * still puts the cursor back after the inserted text. */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 960 | if (ins_at_eol && gchar_cursor() == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 961 | o_lnum = curwin->w_cursor.lnum; |
| 962 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 963 | if (ins_esc(&count, cmdchar, nomove)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 964 | { |
| 965 | #ifdef FEAT_AUTOCMD |
| 966 | if (cmdchar != 'r' && cmdchar != 'v') |
| 967 | apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL, |
| 968 | FALSE, curbuf); |
Bram Moolenaar | c0a0ab5 | 2006-10-06 18:39:58 +0000 | [diff] [blame] | 969 | did_cursorhold = FALSE; |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 970 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | return (c == Ctrl_O); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 972 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 973 | continue; |
| 974 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 975 | case Ctrl_Z: /* suspend when 'insertmode' set */ |
| 976 | if (!p_im) |
| 977 | goto normalchar; /* insert CTRL-Z as normal char */ |
| 978 | stuffReadbuff((char_u *)":st\r"); |
| 979 | c = Ctrl_O; |
| 980 | /*FALLTHROUGH*/ |
| 981 | |
| 982 | case Ctrl_O: /* execute one command */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 983 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 984 | if (ctrl_x_mode == CTRL_X_OMNI) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 985 | goto docomplete; |
| 986 | #endif |
| 987 | if (echeck_abbr(Ctrl_O + ABBR_OFF)) |
| 988 | break; |
| 989 | ins_ctrl_o(); |
Bram Moolenaar | 06a89a5 | 2006-04-29 22:01:03 +0000 | [diff] [blame] | 990 | |
| 991 | #ifdef FEAT_VIRTUALEDIT |
| 992 | /* don't move the cursor left when 'virtualedit' has "onemore". */ |
| 993 | if (ve_flags & VE_ONEMORE) |
| 994 | { |
| 995 | ins_at_eol = FALSE; |
| 996 | nomove = TRUE; |
| 997 | } |
| 998 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 999 | count = 0; |
| 1000 | goto doESCkey; |
| 1001 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1002 | case K_INS: /* toggle insert/replace mode */ |
| 1003 | case K_KINS: |
| 1004 | ins_insert(replaceState); |
| 1005 | break; |
| 1006 | |
| 1007 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1008 | break; |
| 1009 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1010 | #ifdef FEAT_SNIFF |
| 1011 | case K_SNIFF: /* Sniff command received */ |
| 1012 | stuffcharReadbuff(K_SNIFF); |
| 1013 | goto doESCkey; |
| 1014 | #endif |
| 1015 | |
| 1016 | case K_HELP: /* Help key works like <ESC> <Help> */ |
| 1017 | case K_F1: |
| 1018 | case K_XF1: |
| 1019 | stuffcharReadbuff(K_HELP); |
| 1020 | if (p_im) |
| 1021 | need_start_insertmode = TRUE; |
| 1022 | goto doESCkey; |
| 1023 | |
| 1024 | #ifdef FEAT_NETBEANS_INTG |
| 1025 | case K_F21: /* NetBeans command */ |
| 1026 | ++no_mapping; /* don't map the next key hits */ |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1027 | i = plain_vgetc(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1028 | --no_mapping; |
| 1029 | netbeans_keycommand(i); |
| 1030 | break; |
| 1031 | #endif |
| 1032 | |
| 1033 | case K_ZERO: /* Insert the previously inserted text. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1034 | case NUL: |
| 1035 | case Ctrl_A: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1036 | /* For ^@ the trailing ESC will end the insert, unless there is an |
| 1037 | * error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1038 | if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL |
| 1039 | && c != Ctrl_A && !p_im) |
| 1040 | goto doESCkey; /* quit insert mode */ |
| 1041 | inserted_space = FALSE; |
| 1042 | break; |
| 1043 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1044 | case Ctrl_R: /* insert the contents of a register */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | ins_reg(); |
| 1046 | auto_format(FALSE, TRUE); |
| 1047 | inserted_space = FALSE; |
| 1048 | break; |
| 1049 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1050 | case Ctrl_G: /* commands starting with CTRL-G */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1051 | ins_ctrl_g(); |
| 1052 | break; |
| 1053 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1054 | case Ctrl_HAT: /* switch input mode and/or langmap */ |
| 1055 | ins_ctrl_hat(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | |
| 1058 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1059 | case Ctrl__: /* switch between languages */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1060 | if (!p_ari) |
| 1061 | goto normalchar; |
| 1062 | ins_ctrl_(); |
| 1063 | break; |
| 1064 | #endif |
| 1065 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1066 | case Ctrl_D: /* Make indent one shiftwidth smaller. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1068 | if (ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 1069 | goto docomplete; |
| 1070 | #endif |
| 1071 | /* FALLTHROUGH */ |
| 1072 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1073 | case Ctrl_T: /* Make indent one shiftwidth greater. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | # ifdef FEAT_INS_EXPAND |
| 1075 | if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS) |
| 1076 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1077 | if (has_compl_option(FALSE)) |
| 1078 | goto docomplete; |
| 1079 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1080 | } |
| 1081 | # endif |
| 1082 | ins_shift(c, lastc); |
| 1083 | auto_format(FALSE, TRUE); |
| 1084 | inserted_space = FALSE; |
| 1085 | break; |
| 1086 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1087 | case K_DEL: /* delete character under the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1088 | case K_KDEL: |
| 1089 | ins_del(); |
| 1090 | auto_format(FALSE, TRUE); |
| 1091 | break; |
| 1092 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1093 | case K_BS: /* delete character before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1094 | case Ctrl_H: |
| 1095 | did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space); |
| 1096 | auto_format(FALSE, TRUE); |
| 1097 | break; |
| 1098 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1099 | case Ctrl_W: /* delete word before the cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1100 | did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space); |
| 1101 | auto_format(FALSE, TRUE); |
| 1102 | break; |
| 1103 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1104 | case Ctrl_U: /* delete all inserted text in current line */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1105 | # ifdef FEAT_COMPL_FUNC |
| 1106 | /* CTRL-X CTRL-U completes with 'completefunc'. */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1107 | if (ctrl_x_mode == CTRL_X_FUNCTION) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 1108 | goto docomplete; |
| 1109 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1110 | did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space); |
| 1111 | auto_format(FALSE, TRUE); |
| 1112 | inserted_space = FALSE; |
| 1113 | break; |
| 1114 | |
| 1115 | #ifdef FEAT_MOUSE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1116 | case K_LEFTMOUSE: /* mouse keys */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1117 | case K_LEFTMOUSE_NM: |
| 1118 | case K_LEFTDRAG: |
| 1119 | case K_LEFTRELEASE: |
| 1120 | case K_LEFTRELEASE_NM: |
| 1121 | case K_MIDDLEMOUSE: |
| 1122 | case K_MIDDLEDRAG: |
| 1123 | case K_MIDDLERELEASE: |
| 1124 | case K_RIGHTMOUSE: |
| 1125 | case K_RIGHTDRAG: |
| 1126 | case K_RIGHTRELEASE: |
| 1127 | case K_X1MOUSE: |
| 1128 | case K_X1DRAG: |
| 1129 | case K_X1RELEASE: |
| 1130 | case K_X2MOUSE: |
| 1131 | case K_X2DRAG: |
| 1132 | case K_X2RELEASE: |
| 1133 | ins_mouse(c); |
| 1134 | break; |
| 1135 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1136 | case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1137 | ins_mousescroll(MSCR_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1138 | break; |
| 1139 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1140 | case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1141 | ins_mousescroll(MSCR_UP); |
| 1142 | break; |
| 1143 | |
| 1144 | case K_MOUSELEFT: /* Scroll wheel left */ |
| 1145 | ins_mousescroll(MSCR_LEFT); |
| 1146 | break; |
| 1147 | |
| 1148 | case K_MOUSERIGHT: /* Scroll wheel right */ |
| 1149 | ins_mousescroll(MSCR_RIGHT); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1150 | break; |
| 1151 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1152 | #ifdef FEAT_GUI_TABLINE |
| 1153 | case K_TABLINE: |
| 1154 | case K_TABMENU: |
| 1155 | ins_tabline(c); |
| 1156 | break; |
| 1157 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1158 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1159 | case K_IGNORE: /* Something mapped to nothing */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1160 | break; |
| 1161 | |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1162 | #ifdef FEAT_AUTOCMD |
| 1163 | case K_CURSORHOLD: /* Didn't type something for a while. */ |
| 1164 | apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf); |
| 1165 | did_cursorhold = TRUE; |
| 1166 | break; |
| 1167 | #endif |
| 1168 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1169 | #ifdef FEAT_GUI_W32 |
| 1170 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1171 | * cancelled. */ |
| 1172 | case K_F4: |
| 1173 | if (mod_mask != MOD_MASK_ALT) |
| 1174 | goto normalchar; |
| 1175 | break; |
| 1176 | #endif |
| 1177 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1178 | #ifdef FEAT_GUI |
| 1179 | case K_VER_SCROLLBAR: |
| 1180 | ins_scroll(); |
| 1181 | break; |
| 1182 | |
| 1183 | case K_HOR_SCROLLBAR: |
| 1184 | ins_horscroll(); |
| 1185 | break; |
| 1186 | #endif |
| 1187 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1188 | case K_HOME: /* <Home> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1189 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1190 | case K_S_HOME: |
| 1191 | case K_C_HOME: |
| 1192 | ins_home(c); |
| 1193 | break; |
| 1194 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1195 | case K_END: /* <End> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1196 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1197 | case K_S_END: |
| 1198 | case K_C_END: |
| 1199 | ins_end(c); |
| 1200 | break; |
| 1201 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1202 | case K_LEFT: /* <Left> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1203 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1204 | ins_s_left(); |
| 1205 | else |
| 1206 | ins_left(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1209 | case K_S_LEFT: /* <S-Left> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1210 | case K_C_LEFT: |
| 1211 | ins_s_left(); |
| 1212 | break; |
| 1213 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1214 | case K_RIGHT: /* <Right> */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1215 | if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)) |
| 1216 | ins_s_right(); |
| 1217 | else |
| 1218 | ins_right(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1221 | case K_S_RIGHT: /* <S-Right> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1222 | case K_C_RIGHT: |
| 1223 | ins_s_right(); |
| 1224 | break; |
| 1225 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1226 | case K_UP: /* <Up> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1227 | #ifdef FEAT_INS_EXPAND |
| 1228 | if (pum_visible()) |
| 1229 | goto docomplete; |
| 1230 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1231 | if (mod_mask & MOD_MASK_SHIFT) |
| 1232 | ins_pageup(); |
| 1233 | else |
| 1234 | ins_up(FALSE); |
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_UP: /* <S-Up> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1238 | case K_PAGEUP: |
| 1239 | case K_KPAGEUP: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1240 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1241 | if (pum_visible()) |
| 1242 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1243 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1244 | ins_pageup(); |
| 1245 | break; |
| 1246 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1247 | case K_DOWN: /* <Down> */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 1248 | #ifdef FEAT_INS_EXPAND |
| 1249 | if (pum_visible()) |
| 1250 | goto docomplete; |
| 1251 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1252 | if (mod_mask & MOD_MASK_SHIFT) |
| 1253 | ins_pagedown(); |
| 1254 | else |
| 1255 | ins_down(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1258 | case K_S_DOWN: /* <S-Down> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1259 | case K_PAGEDOWN: |
| 1260 | case K_KPAGEDOWN: |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1261 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 1262 | if (pum_visible()) |
| 1263 | goto docomplete; |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1264 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1265 | ins_pagedown(); |
| 1266 | break; |
| 1267 | |
| 1268 | #ifdef FEAT_DND |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1269 | case K_DROP: /* drag-n-drop event */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1270 | ins_drop(); |
| 1271 | break; |
| 1272 | #endif |
| 1273 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1274 | case K_S_TAB: /* When not mapped, use like a normal TAB */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | c = TAB; |
| 1276 | /* FALLTHROUGH */ |
| 1277 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1278 | case TAB: /* TAB or Complete patterns along path */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1279 | #if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID) |
| 1280 | if (ctrl_x_mode == CTRL_X_PATH_PATTERNS) |
| 1281 | goto docomplete; |
| 1282 | #endif |
| 1283 | inserted_space = FALSE; |
| 1284 | if (ins_tab()) |
| 1285 | goto normalchar; /* insert TAB as a normal char */ |
| 1286 | auto_format(FALSE, TRUE); |
| 1287 | break; |
| 1288 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1289 | case K_KENTER: /* <Enter> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1290 | c = CAR; |
| 1291 | /* FALLTHROUGH */ |
| 1292 | case CAR: |
| 1293 | case NL: |
| 1294 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 1295 | /* In a quickfix window a <CR> jumps to the error under the |
| 1296 | * cursor. */ |
| 1297 | if (bt_quickfix(curbuf) && c == CAR) |
| 1298 | { |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 1299 | if (curwin->w_llist_ref == NULL) /* quickfix window */ |
| 1300 | do_cmdline_cmd((char_u *)".cc"); |
| 1301 | else /* location list window */ |
| 1302 | do_cmdline_cmd((char_u *)".ll"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | } |
| 1305 | #endif |
| 1306 | #ifdef FEAT_CMDWIN |
| 1307 | if (cmdwin_type != 0) |
| 1308 | { |
| 1309 | /* Execute the command in the cmdline window. */ |
| 1310 | cmdwin_result = CAR; |
| 1311 | goto doESCkey; |
| 1312 | } |
| 1313 | #endif |
| 1314 | if (ins_eol(c) && !p_im) |
| 1315 | goto doESCkey; /* out of memory */ |
| 1316 | auto_format(FALSE, FALSE); |
| 1317 | inserted_space = FALSE; |
| 1318 | break; |
| 1319 | |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 1320 | #if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1321 | case Ctrl_K: /* digraph or keyword completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | # ifdef FEAT_INS_EXPAND |
| 1323 | if (ctrl_x_mode == CTRL_X_DICTIONARY) |
| 1324 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1325 | if (has_compl_option(TRUE)) |
| 1326 | goto docomplete; |
| 1327 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1328 | } |
| 1329 | # endif |
| 1330 | # ifdef FEAT_DIGRAPHS |
| 1331 | c = ins_digraph(); |
| 1332 | if (c == NUL) |
| 1333 | break; |
| 1334 | # endif |
| 1335 | goto normalchar; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1336 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1337 | |
| 1338 | #ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 1339 | case Ctrl_X: /* Enter CTRL-X mode */ |
| 1340 | ins_ctrl_x(); |
| 1341 | break; |
| 1342 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1343 | case Ctrl_RSB: /* Tag name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1344 | if (ctrl_x_mode != CTRL_X_TAGS) |
| 1345 | goto normalchar; |
| 1346 | goto docomplete; |
| 1347 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1348 | case Ctrl_F: /* File name completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1349 | if (ctrl_x_mode != CTRL_X_FILES) |
| 1350 | goto normalchar; |
| 1351 | goto docomplete; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 1352 | |
| 1353 | case 's': /* Spelling completion after ^X */ |
| 1354 | case Ctrl_S: |
| 1355 | if (ctrl_x_mode != CTRL_X_SPELL) |
| 1356 | goto normalchar; |
| 1357 | goto docomplete; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1358 | #endif |
| 1359 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1360 | case Ctrl_L: /* Whole line completion after ^X */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | #ifdef FEAT_INS_EXPAND |
| 1362 | if (ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 1363 | #endif |
| 1364 | { |
| 1365 | /* CTRL-L with 'insertmode' set: Leave Insert mode */ |
| 1366 | if (p_im) |
| 1367 | { |
| 1368 | if (echeck_abbr(Ctrl_L + ABBR_OFF)) |
| 1369 | break; |
| 1370 | goto doESCkey; |
| 1371 | } |
| 1372 | goto normalchar; |
| 1373 | } |
| 1374 | #ifdef FEAT_INS_EXPAND |
| 1375 | /* FALLTHROUGH */ |
| 1376 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1377 | case Ctrl_P: /* Do previous/next pattern completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1378 | case Ctrl_N: |
| 1379 | /* if 'complete' is empty then plain ^P is no longer special, |
| 1380 | * but it is under other ^X modes */ |
| 1381 | if (*curbuf->b_p_cpt == NUL |
| 1382 | && ctrl_x_mode != 0 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1383 | && !(compl_cont_status & CONT_LOCAL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | goto normalchar; |
| 1385 | |
| 1386 | docomplete: |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 1387 | compl_busy = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1388 | if (ins_complete(c) == FAIL) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1389 | compl_cont_status = 0; |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 1390 | compl_busy = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1391 | break; |
| 1392 | #endif /* FEAT_INS_EXPAND */ |
| 1393 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 1394 | case Ctrl_Y: /* copy from previous line or scroll down */ |
| 1395 | case Ctrl_E: /* copy from next line or scroll up */ |
| 1396 | c = ins_ctrl_ey(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1397 | break; |
| 1398 | |
| 1399 | default: |
| 1400 | #ifdef UNIX |
| 1401 | if (c == intr_char) /* special interrupt char */ |
| 1402 | goto do_intr; |
| 1403 | #endif |
| 1404 | |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1405 | normalchar: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1406 | /* |
| 1407 | * Insert a nomal character. |
| 1408 | */ |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1409 | #ifdef FEAT_AUTOCMD |
| 1410 | if (!p_paste) |
| 1411 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1412 | /* Trigger InsertCharPre. */ |
| 1413 | char_u *str = do_insert_char_pre(c); |
| 1414 | char_u *p; |
| 1415 | |
| 1416 | if (str != NULL) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1417 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1418 | if (*str != NUL && stop_arrow() != FAIL) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1419 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1420 | /* Insert the new value of v:char literally. */ |
| 1421 | for (p = str; *p != NUL; mb_ptr_adv(p)) |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1422 | { |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1423 | c = PTR2CHAR(p); |
| 1424 | if (c == CAR || c == K_KENTER || c == NL) |
| 1425 | ins_eol(c); |
| 1426 | else |
| 1427 | ins_char(c); |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1428 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1429 | AppendToRedobuffLit(str, -1); |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1430 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1431 | vim_free(str); |
| 1432 | c = NUL; |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 1435 | /* If the new value is already inserted or an empty string |
| 1436 | * then don't insert any character. */ |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 1437 | if (c == NUL) |
| 1438 | break; |
| 1439 | } |
| 1440 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1441 | #ifdef FEAT_SMARTINDENT |
| 1442 | /* Try to perform smart-indenting. */ |
| 1443 | ins_try_si(c); |
| 1444 | #endif |
| 1445 | |
| 1446 | if (c == ' ') |
| 1447 | { |
| 1448 | inserted_space = TRUE; |
| 1449 | #ifdef FEAT_CINDENT |
| 1450 | if (inindent(0)) |
| 1451 | can_cindent = FALSE; |
| 1452 | #endif |
| 1453 | if (Insstart_blank_vcol == MAXCOL |
| 1454 | && curwin->w_cursor.lnum == Insstart.lnum) |
| 1455 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 1456 | } |
| 1457 | |
Bram Moolenaar | e0ebfd7 | 2012-04-05 16:07:06 +0200 | [diff] [blame] | 1458 | /* Insert a normal character and check for abbreviations on a |
| 1459 | * special character. Let CTRL-] expand abbreviations without |
| 1460 | * inserting it. */ |
| 1461 | if (vim_iswordc(c) || (!echeck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | #ifdef FEAT_MBYTE |
| 1463 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1464 | * what check_abbr() expects. */ |
| 1465 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1466 | #endif |
Bram Moolenaar | e0ebfd7 | 2012-04-05 16:07:06 +0200 | [diff] [blame] | 1467 | c) && c != Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1468 | { |
| 1469 | insert_special(c, FALSE, FALSE); |
| 1470 | #ifdef FEAT_RIGHTLEFT |
| 1471 | revins_legal++; |
| 1472 | revins_chars++; |
| 1473 | #endif |
| 1474 | } |
| 1475 | |
| 1476 | auto_format(FALSE, TRUE); |
| 1477 | |
| 1478 | #ifdef FEAT_FOLDING |
| 1479 | /* When inserting a character the cursor line must never be in a |
| 1480 | * closed fold. */ |
| 1481 | foldOpenCursor(); |
| 1482 | #endif |
| 1483 | break; |
| 1484 | } /* end of switch (c) */ |
| 1485 | |
Bram Moolenaar | d29a9ee | 2006-09-14 09:07:34 +0000 | [diff] [blame] | 1486 | #ifdef FEAT_AUTOCMD |
| 1487 | /* If typed something may trigger CursorHoldI again. */ |
| 1488 | if (c != K_CURSORHOLD) |
| 1489 | did_cursorhold = FALSE; |
| 1490 | #endif |
| 1491 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1492 | /* If the cursor was moved we didn't just insert a space */ |
| 1493 | if (arrow_used) |
| 1494 | inserted_space = FALSE; |
| 1495 | |
| 1496 | #ifdef FEAT_CINDENT |
| 1497 | if (can_cindent && cindent_on() |
| 1498 | # ifdef FEAT_INS_EXPAND |
| 1499 | && ctrl_x_mode == 0 |
| 1500 | # endif |
| 1501 | ) |
| 1502 | { |
| 1503 | force_cindent: |
| 1504 | /* |
| 1505 | * Indent now if a key was typed that is in 'cinkeys'. |
| 1506 | */ |
| 1507 | if (in_cinkeys(c, ' ', line_is_white)) |
| 1508 | { |
| 1509 | if (stop_arrow() == OK) |
| 1510 | /* re-indent the current line */ |
| 1511 | do_c_expr_indent(); |
| 1512 | } |
| 1513 | } |
| 1514 | #endif /* FEAT_CINDENT */ |
| 1515 | |
| 1516 | } /* for (;;) */ |
| 1517 | /* NOTREACHED */ |
| 1518 | } |
| 1519 | |
| 1520 | /* |
| 1521 | * Redraw for Insert mode. |
| 1522 | * This is postponed until getting the next character to make '$' in the 'cpo' |
| 1523 | * option work correctly. |
| 1524 | * Only redraw when there are no characters available. This speeds up |
| 1525 | * inserting sequences of characters (e.g., for CTRL-R). |
| 1526 | */ |
| 1527 | static void |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1528 | ins_redraw(ready) |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 1529 | int ready UNUSED; /* not busy with something */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1530 | { |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1531 | #ifdef FEAT_CONCEAL |
| 1532 | linenr_T conceal_old_cursor_line = 0; |
| 1533 | linenr_T conceal_new_cursor_line = 0; |
| 1534 | int conceal_update_lines = FALSE; |
| 1535 | #endif |
| 1536 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | if (!char_avail()) |
| 1538 | { |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1539 | #if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL) |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1540 | /* Trigger CursorMoved if the cursor moved. Not when the popup menu is |
| 1541 | * visible, the command might delete it. */ |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1542 | if (ready && ( |
| 1543 | # ifdef FEAT_AUTOCMD |
| 1544 | has_cursormovedI() |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 1545 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1546 | # if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL) |
| 1547 | || |
| 1548 | # endif |
| 1549 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1550 | curwin->w_p_cole > 0 |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1551 | # endif |
| 1552 | ) |
| 1553 | && !equalpos(last_cursormoved, curwin->w_cursor) |
| 1554 | # ifdef FEAT_INS_EXPAND |
| 1555 | && !pum_visible() |
| 1556 | # endif |
| 1557 | ) |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1558 | { |
Bram Moolenaar | e77c760 | 2008-01-12 17:13:50 +0000 | [diff] [blame] | 1559 | # ifdef FEAT_SYN_HL |
| 1560 | /* Need to update the screen first, to make sure syntax |
| 1561 | * highlighting is correct after making a change (e.g., inserting |
| 1562 | * a "(". The autocommand may also require a redraw, so it's done |
| 1563 | * again below, unfortunately. */ |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 1564 | if (syntax_present(curwin) && must_redraw) |
Bram Moolenaar | e77c760 | 2008-01-12 17:13:50 +0000 | [diff] [blame] | 1565 | update_screen(0); |
| 1566 | # endif |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1567 | # ifdef FEAT_AUTOCMD |
| 1568 | if (has_cursormovedI()) |
| 1569 | apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf); |
| 1570 | # endif |
| 1571 | # ifdef FEAT_CONCEAL |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1572 | if (curwin->w_p_cole > 0) |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1573 | { |
| 1574 | conceal_old_cursor_line = last_cursormoved.lnum; |
| 1575 | conceal_new_cursor_line = curwin->w_cursor.lnum; |
| 1576 | conceal_update_lines = TRUE; |
| 1577 | } |
| 1578 | # endif |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1579 | last_cursormoved = curwin->w_cursor; |
| 1580 | } |
| 1581 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1582 | if (must_redraw) |
| 1583 | update_screen(0); |
| 1584 | else if (clear_cmdline || redraw_cmdline) |
| 1585 | showmode(); /* clear cmdline and show mode */ |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1586 | # if defined(FEAT_CONCEAL) |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1587 | if ((conceal_update_lines |
| 1588 | && (conceal_old_cursor_line != conceal_new_cursor_line |
| 1589 | || conceal_cursor_line(curwin))) |
| 1590 | || need_cursor_line_redraw) |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1591 | { |
Bram Moolenaar | f5963f7 | 2010-07-23 22:10:27 +0200 | [diff] [blame] | 1592 | if (conceal_old_cursor_line != conceal_new_cursor_line) |
| 1593 | update_single_line(curwin, conceal_old_cursor_line); |
| 1594 | update_single_line(curwin, conceal_new_cursor_line == 0 |
| 1595 | ? curwin->w_cursor.lnum : conceal_new_cursor_line); |
Bram Moolenaar | b2c0350 | 2010-07-02 20:20:09 +0200 | [diff] [blame] | 1596 | curwin->w_valid &= ~VALID_CROW; |
| 1597 | } |
| 1598 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1599 | showruler(FALSE); |
| 1600 | setcursor(); |
| 1601 | emsg_on_display = FALSE; /* may remove error message now */ |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * Handle a CTRL-V or CTRL-Q typed in Insert mode. |
| 1607 | */ |
| 1608 | static void |
| 1609 | ins_ctrl_v() |
| 1610 | { |
| 1611 | int c; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1612 | int did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1613 | |
| 1614 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 1615 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1616 | |
| 1617 | if (redrawing() && !char_avail()) |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1618 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1619 | edit_putchar('^', TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1620 | did_putchar = TRUE; |
| 1621 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1622 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 1623 | |
| 1624 | #ifdef FEAT_CMDL_INFO |
| 1625 | add_to_showcmd_c(Ctrl_V); |
| 1626 | #endif |
| 1627 | |
| 1628 | c = get_literal(); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 1629 | if (did_putchar) |
| 1630 | /* when the line fits in 'columns' the '^' is at the start of the next |
| 1631 | * line and will not removed by the redraw */ |
| 1632 | edit_unputchar(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1633 | #ifdef FEAT_CMDL_INFO |
| 1634 | clear_showcmd(); |
| 1635 | #endif |
| 1636 | insert_special(c, FALSE, TRUE); |
| 1637 | #ifdef FEAT_RIGHTLEFT |
| 1638 | revins_chars++; |
| 1639 | revins_legal++; |
| 1640 | #endif |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * Put a character directly onto the screen. It's not stored in a buffer. |
| 1645 | * Used while handling CTRL-K, CTRL-V, etc. in Insert mode. |
| 1646 | */ |
| 1647 | static int pc_status; |
| 1648 | #define PC_STATUS_UNSET 0 /* pc_bytes was not set */ |
| 1649 | #define PC_STATUS_RIGHT 1 /* right halve of double-wide char */ |
| 1650 | #define PC_STATUS_LEFT 2 /* left halve of double-wide char */ |
| 1651 | #define PC_STATUS_SET 3 /* pc_bytes was filled */ |
| 1652 | #ifdef FEAT_MBYTE |
| 1653 | static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */ |
| 1654 | #else |
| 1655 | static char_u pc_bytes[2]; /* saved bytes */ |
| 1656 | #endif |
| 1657 | static int pc_attr; |
| 1658 | static int pc_row; |
| 1659 | static int pc_col; |
| 1660 | |
| 1661 | void |
| 1662 | edit_putchar(c, highlight) |
| 1663 | int c; |
| 1664 | int highlight; |
| 1665 | { |
| 1666 | int attr; |
| 1667 | |
| 1668 | if (ScreenLines != NULL) |
| 1669 | { |
| 1670 | update_topline(); /* just in case w_topline isn't valid */ |
| 1671 | validate_cursor(); |
| 1672 | if (highlight) |
| 1673 | attr = hl_attr(HLF_8); |
| 1674 | else |
| 1675 | attr = 0; |
| 1676 | pc_row = W_WINROW(curwin) + curwin->w_wrow; |
| 1677 | pc_col = W_WINCOL(curwin); |
| 1678 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1679 | pc_status = PC_STATUS_UNSET; |
| 1680 | #endif |
| 1681 | #ifdef FEAT_RIGHTLEFT |
| 1682 | if (curwin->w_p_rl) |
| 1683 | { |
| 1684 | pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol; |
| 1685 | # ifdef FEAT_MBYTE |
| 1686 | if (has_mbyte) |
| 1687 | { |
| 1688 | int fix_col = mb_fix_col(pc_col, pc_row); |
| 1689 | |
| 1690 | if (fix_col != pc_col) |
| 1691 | { |
| 1692 | screen_putchar(' ', pc_row, fix_col, attr); |
| 1693 | --curwin->w_wcol; |
| 1694 | pc_status = PC_STATUS_RIGHT; |
| 1695 | } |
| 1696 | } |
| 1697 | # endif |
| 1698 | } |
| 1699 | else |
| 1700 | #endif |
| 1701 | { |
| 1702 | pc_col += curwin->w_wcol; |
| 1703 | #ifdef FEAT_MBYTE |
| 1704 | if (mb_lefthalve(pc_row, pc_col)) |
| 1705 | pc_status = PC_STATUS_LEFT; |
| 1706 | #endif |
| 1707 | } |
| 1708 | |
| 1709 | /* save the character to be able to put it back */ |
| 1710 | #if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE) |
| 1711 | if (pc_status == PC_STATUS_UNSET) |
| 1712 | #endif |
| 1713 | { |
| 1714 | screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr); |
| 1715 | pc_status = PC_STATUS_SET; |
| 1716 | } |
| 1717 | screen_putchar(c, pc_row, pc_col, attr); |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | /* |
| 1722 | * Undo the previous edit_putchar(). |
| 1723 | */ |
| 1724 | void |
| 1725 | edit_unputchar() |
| 1726 | { |
| 1727 | if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled) |
| 1728 | { |
| 1729 | #if defined(FEAT_MBYTE) |
| 1730 | if (pc_status == PC_STATUS_RIGHT) |
| 1731 | ++curwin->w_wcol; |
| 1732 | if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT) |
| 1733 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1734 | else |
| 1735 | #endif |
| 1736 | screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | /* |
| 1741 | * Called when p_dollar is set: display a '$' at the end of the changed text |
| 1742 | * Only works when cursor is in the line that changes. |
| 1743 | */ |
| 1744 | void |
| 1745 | display_dollar(col) |
| 1746 | colnr_T col; |
| 1747 | { |
| 1748 | colnr_T save_col; |
| 1749 | |
| 1750 | if (!redrawing()) |
| 1751 | return; |
| 1752 | |
| 1753 | cursor_off(); |
| 1754 | save_col = curwin->w_cursor.col; |
| 1755 | curwin->w_cursor.col = col; |
| 1756 | #ifdef FEAT_MBYTE |
| 1757 | if (has_mbyte) |
| 1758 | { |
| 1759 | char_u *p; |
| 1760 | |
| 1761 | /* If on the last byte of a multi-byte move to the first byte. */ |
| 1762 | p = ml_get_curline(); |
| 1763 | curwin->w_cursor.col -= (*mb_head_off)(p, p + col); |
| 1764 | } |
| 1765 | #endif |
| 1766 | curs_columns(FALSE); /* recompute w_wrow and w_wcol */ |
| 1767 | if (curwin->w_wcol < W_WIDTH(curwin)) |
| 1768 | { |
| 1769 | edit_putchar('$', FALSE); |
| 1770 | dollar_vcol = curwin->w_virtcol; |
| 1771 | } |
| 1772 | curwin->w_cursor.col = save_col; |
| 1773 | } |
| 1774 | |
| 1775 | /* |
| 1776 | * Call this function before moving the cursor from the normal insert position |
| 1777 | * in insert mode. |
| 1778 | */ |
| 1779 | static void |
| 1780 | undisplay_dollar() |
| 1781 | { |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 1782 | if (dollar_vcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1783 | { |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 1784 | dollar_vcol = -1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | redrawWinline(curwin->w_cursor.lnum, FALSE); |
| 1786 | } |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D). |
| 1791 | * Keep the cursor on the same character. |
| 1792 | * type == INDENT_INC increase indent (for CTRL-T or <Tab>) |
| 1793 | * type == INDENT_DEC decrease indent (for CTRL-D) |
| 1794 | * type == INDENT_SET set indent to "amount" |
| 1795 | * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec). |
| 1796 | */ |
| 1797 | void |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1798 | change_indent(type, amount, round, replaced, call_changed_bytes) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1799 | int type; |
| 1800 | int amount; |
| 1801 | int round; |
| 1802 | int replaced; /* replaced character, put on replace stack */ |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1803 | int call_changed_bytes; /* call changed_bytes() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1804 | { |
| 1805 | int vcol; |
| 1806 | int last_vcol; |
| 1807 | int insstart_less; /* reduction for Insstart.col */ |
| 1808 | int new_cursor_col; |
| 1809 | int i; |
| 1810 | char_u *ptr; |
| 1811 | int save_p_list; |
| 1812 | int start_col; |
| 1813 | colnr_T vc; |
| 1814 | #ifdef FEAT_VREPLACE |
| 1815 | colnr_T orig_col = 0; /* init for GCC */ |
| 1816 | char_u *new_line, *orig_line = NULL; /* init for GCC */ |
| 1817 | |
| 1818 | /* VREPLACE mode needs to know what the line was like before changing */ |
| 1819 | if (State & VREPLACE_FLAG) |
| 1820 | { |
| 1821 | orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */ |
| 1822 | orig_col = curwin->w_cursor.col; |
| 1823 | } |
| 1824 | #endif |
| 1825 | |
| 1826 | /* for the following tricks we don't want list mode */ |
| 1827 | save_p_list = curwin->w_p_list; |
| 1828 | curwin->w_p_list = FALSE; |
| 1829 | vc = getvcol_nolist(&curwin->w_cursor); |
| 1830 | vcol = vc; |
| 1831 | |
| 1832 | /* |
| 1833 | * For Replace mode we need to fix the replace stack later, which is only |
| 1834 | * possible when the cursor is in the indent. Remember the number of |
| 1835 | * characters before the cursor if it's possible. |
| 1836 | */ |
| 1837 | start_col = curwin->w_cursor.col; |
| 1838 | |
| 1839 | /* determine offset from first non-blank */ |
| 1840 | new_cursor_col = curwin->w_cursor.col; |
| 1841 | beginline(BL_WHITE); |
| 1842 | new_cursor_col -= curwin->w_cursor.col; |
| 1843 | |
| 1844 | insstart_less = curwin->w_cursor.col; |
| 1845 | |
| 1846 | /* |
| 1847 | * If the cursor is in the indent, compute how many screen columns the |
| 1848 | * cursor is to the left of the first non-blank. |
| 1849 | */ |
| 1850 | if (new_cursor_col < 0) |
| 1851 | vcol = get_indent() - vcol; |
| 1852 | |
| 1853 | if (new_cursor_col > 0) /* can't fix replace stack */ |
| 1854 | start_col = -1; |
| 1855 | |
| 1856 | /* |
| 1857 | * Set the new indent. The cursor will be put on the first non-blank. |
| 1858 | */ |
| 1859 | if (type == INDENT_SET) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1860 | (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1861 | else |
| 1862 | { |
| 1863 | #ifdef FEAT_VREPLACE |
| 1864 | int save_State = State; |
| 1865 | |
| 1866 | /* Avoid being called recursively. */ |
| 1867 | if (State & VREPLACE_FLAG) |
| 1868 | State = INSERT; |
| 1869 | #endif |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 1870 | shift_line(type == INDENT_DEC, round, 1, call_changed_bytes); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1871 | #ifdef FEAT_VREPLACE |
| 1872 | State = save_State; |
| 1873 | #endif |
| 1874 | } |
| 1875 | insstart_less -= curwin->w_cursor.col; |
| 1876 | |
| 1877 | /* |
| 1878 | * Try to put cursor on same character. |
| 1879 | * If the cursor is at or after the first non-blank in the line, |
| 1880 | * compute the cursor column relative to the column of the first |
| 1881 | * non-blank character. |
| 1882 | * If we are not in insert mode, leave the cursor on the first non-blank. |
| 1883 | * If the cursor is before the first non-blank, position it relative |
| 1884 | * to the first non-blank, counted in screen columns. |
| 1885 | */ |
| 1886 | if (new_cursor_col >= 0) |
| 1887 | { |
| 1888 | /* |
| 1889 | * When changing the indent while the cursor is touching it, reset |
| 1890 | * Insstart_col to 0. |
| 1891 | */ |
| 1892 | if (new_cursor_col == 0) |
| 1893 | insstart_less = MAXCOL; |
| 1894 | new_cursor_col += curwin->w_cursor.col; |
| 1895 | } |
| 1896 | else if (!(State & INSERT)) |
| 1897 | new_cursor_col = curwin->w_cursor.col; |
| 1898 | else |
| 1899 | { |
| 1900 | /* |
| 1901 | * Compute the screen column where the cursor should be. |
| 1902 | */ |
| 1903 | vcol = get_indent() - vcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1904 | curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | |
| 1906 | /* |
| 1907 | * Advance the cursor until we reach the right screen column. |
| 1908 | */ |
| 1909 | vcol = last_vcol = 0; |
| 1910 | new_cursor_col = -1; |
| 1911 | ptr = ml_get_curline(); |
| 1912 | while (vcol <= (int)curwin->w_virtcol) |
| 1913 | { |
| 1914 | last_vcol = vcol; |
| 1915 | #ifdef FEAT_MBYTE |
| 1916 | if (has_mbyte && new_cursor_col >= 0) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1917 | new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1918 | else |
| 1919 | #endif |
| 1920 | ++new_cursor_col; |
| 1921 | vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol); |
| 1922 | } |
| 1923 | vcol = last_vcol; |
| 1924 | |
| 1925 | /* |
| 1926 | * May need to insert spaces to be able to position the cursor on |
| 1927 | * the right screen column. |
| 1928 | */ |
| 1929 | if (vcol != (int)curwin->w_virtcol) |
| 1930 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1931 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | i = (int)curwin->w_virtcol - vcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1933 | ptr = alloc((unsigned)(i + 1)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1934 | if (ptr != NULL) |
| 1935 | { |
| 1936 | new_cursor_col += i; |
| 1937 | ptr[i] = NUL; |
| 1938 | while (--i >= 0) |
| 1939 | ptr[i] = ' '; |
| 1940 | ins_str(ptr); |
| 1941 | vim_free(ptr); |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | /* |
| 1946 | * When changing the indent while the cursor is in it, reset |
| 1947 | * Insstart_col to 0. |
| 1948 | */ |
| 1949 | insstart_less = MAXCOL; |
| 1950 | } |
| 1951 | |
| 1952 | curwin->w_p_list = save_p_list; |
| 1953 | |
| 1954 | if (new_cursor_col <= 0) |
| 1955 | curwin->w_cursor.col = 0; |
| 1956 | else |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 1957 | curwin->w_cursor.col = (colnr_T)new_cursor_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1958 | curwin->w_set_curswant = TRUE; |
| 1959 | changed_cline_bef_curs(); |
| 1960 | |
| 1961 | /* |
| 1962 | * May have to adjust the start of the insert. |
| 1963 | */ |
| 1964 | if (State & INSERT) |
| 1965 | { |
| 1966 | if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0) |
| 1967 | { |
| 1968 | if ((int)Insstart.col <= insstart_less) |
| 1969 | Insstart.col = 0; |
| 1970 | else |
| 1971 | Insstart.col -= insstart_less; |
| 1972 | } |
| 1973 | if ((int)ai_col <= insstart_less) |
| 1974 | ai_col = 0; |
| 1975 | else |
| 1976 | ai_col -= insstart_less; |
| 1977 | } |
| 1978 | |
| 1979 | /* |
| 1980 | * For REPLACE mode, may have to fix the replace stack, if it's possible. |
| 1981 | * If the number of characters before the cursor decreased, need to pop a |
| 1982 | * few characters from the replace stack. |
| 1983 | * If the number of characters before the cursor increased, need to push a |
| 1984 | * few NULs onto the replace stack. |
| 1985 | */ |
| 1986 | if (REPLACE_NORMAL(State) && start_col >= 0) |
| 1987 | { |
| 1988 | while (start_col > (int)curwin->w_cursor.col) |
| 1989 | { |
| 1990 | replace_join(0); /* remove a NUL from the replace stack */ |
| 1991 | --start_col; |
| 1992 | } |
| 1993 | while (start_col < (int)curwin->w_cursor.col || replaced) |
| 1994 | { |
| 1995 | replace_push(NUL); |
| 1996 | if (replaced) |
| 1997 | { |
| 1998 | replace_push(replaced); |
| 1999 | replaced = NUL; |
| 2000 | } |
| 2001 | ++start_col; |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | #ifdef FEAT_VREPLACE |
| 2006 | /* |
| 2007 | * For VREPLACE mode, we also have to fix the replace stack. In this case |
| 2008 | * it is always possible because we backspace over the whole line and then |
| 2009 | * put it back again the way we wanted it. |
| 2010 | */ |
| 2011 | if (State & VREPLACE_FLAG) |
| 2012 | { |
| 2013 | /* If orig_line didn't allocate, just return. At least we did the job, |
| 2014 | * even if you can't backspace. */ |
| 2015 | if (orig_line == NULL) |
| 2016 | return; |
| 2017 | |
| 2018 | /* Save new line */ |
| 2019 | new_line = vim_strsave(ml_get_curline()); |
| 2020 | if (new_line == NULL) |
| 2021 | return; |
| 2022 | |
| 2023 | /* We only put back the new line up to the cursor */ |
| 2024 | new_line[curwin->w_cursor.col] = NUL; |
| 2025 | |
| 2026 | /* Put back original line */ |
| 2027 | ml_replace(curwin->w_cursor.lnum, orig_line, FALSE); |
| 2028 | curwin->w_cursor.col = orig_col; |
| 2029 | |
| 2030 | /* Backspace from cursor to start of line */ |
| 2031 | backspace_until_column(0); |
| 2032 | |
| 2033 | /* Insert new stuff into line again */ |
| 2034 | ins_bytes(new_line); |
| 2035 | |
| 2036 | vim_free(new_line); |
| 2037 | } |
| 2038 | #endif |
| 2039 | } |
| 2040 | |
| 2041 | /* |
| 2042 | * Truncate the space at the end of a line. This is to be used only in an |
| 2043 | * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE |
| 2044 | * modes. |
| 2045 | */ |
| 2046 | void |
| 2047 | truncate_spaces(line) |
| 2048 | char_u *line; |
| 2049 | { |
| 2050 | int i; |
| 2051 | |
| 2052 | /* find start of trailing white space */ |
| 2053 | for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--) |
| 2054 | { |
| 2055 | if (State & REPLACE_FLAG) |
| 2056 | replace_join(0); /* remove a NUL from the replace stack */ |
| 2057 | } |
| 2058 | line[i + 1] = NUL; |
| 2059 | } |
| 2060 | |
| 2061 | #if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \ |
| 2062 | || defined(FEAT_COMMENTS) || defined(PROTO) |
| 2063 | /* |
| 2064 | * Backspace the cursor until the given column. Handles REPLACE and VREPLACE |
| 2065 | * 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] | 2066 | * Will attempt not to go before "col" even when there is a composing |
| 2067 | * character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2068 | */ |
| 2069 | void |
| 2070 | backspace_until_column(col) |
| 2071 | int col; |
| 2072 | { |
| 2073 | while ((int)curwin->w_cursor.col > col) |
| 2074 | { |
| 2075 | curwin->w_cursor.col--; |
| 2076 | if (State & REPLACE_FLAG) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2077 | replace_do_bs(col); |
| 2078 | else if (!del_char_after_col(col)) |
| 2079 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | #endif |
| 2083 | |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2084 | /* |
| 2085 | * Like del_char(), but make sure not to go before column "limit_col". |
| 2086 | * Only matters when there are composing characters. |
| 2087 | * Return TRUE when something was deleted. |
| 2088 | */ |
| 2089 | static int |
| 2090 | del_char_after_col(limit_col) |
Bram Moolenaar | 0c094b9 | 2009-05-14 20:20:33 +0000 | [diff] [blame] | 2091 | int limit_col UNUSED; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2092 | { |
| 2093 | #ifdef FEAT_MBYTE |
| 2094 | if (enc_utf8 && limit_col >= 0) |
| 2095 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2096 | colnr_T ecol = curwin->w_cursor.col + 1; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2097 | |
| 2098 | /* Make sure the cursor is at the start of a character, but |
| 2099 | * skip forward again when going too far back because of a |
| 2100 | * composing character. */ |
| 2101 | mb_adjust_cursor(); |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 2102 | while (curwin->w_cursor.col < (colnr_T)limit_col) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2103 | { |
| 2104 | int l = utf_ptr2len(ml_get_cursor()); |
| 2105 | |
| 2106 | if (l == 0) /* end of line */ |
| 2107 | break; |
| 2108 | curwin->w_cursor.col += l; |
| 2109 | } |
| 2110 | if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol) |
| 2111 | return FALSE; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2112 | del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2113 | } |
| 2114 | else |
| 2115 | #endif |
| 2116 | (void)del_char(FALSE); |
| 2117 | return TRUE; |
| 2118 | } |
| 2119 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2120 | #if defined(FEAT_INS_EXPAND) || defined(PROTO) |
| 2121 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2122 | * CTRL-X pressed in Insert mode. |
| 2123 | */ |
| 2124 | static void |
| 2125 | ins_ctrl_x() |
| 2126 | { |
| 2127 | /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X |
| 2128 | * CTRL-V works like CTRL-N */ |
| 2129 | if (ctrl_x_mode != CTRL_X_CMDLINE) |
| 2130 | { |
| 2131 | /* if the next ^X<> won't ADD nothing, then reset |
| 2132 | * compl_cont_status */ |
| 2133 | if (compl_cont_status & CONT_N_ADDS) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2134 | compl_cont_status |= CONT_INTRPT; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2135 | else |
| 2136 | compl_cont_status = 0; |
| 2137 | /* We're not sure which CTRL-X mode it will be yet */ |
| 2138 | ctrl_x_mode = CTRL_X_NOT_DEFINED_YET; |
| 2139 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 2140 | edit_submode_pre = NULL; |
| 2141 | showmode(); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * Return TRUE if the 'dict' or 'tsr' option can be used. |
| 2147 | */ |
| 2148 | static int |
| 2149 | has_compl_option(dict_opt) |
| 2150 | int dict_opt; |
| 2151 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2152 | if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 2153 | # ifdef FEAT_SPELL |
| 2154 | && !curwin->w_p_spell |
| 2155 | # endif |
| 2156 | ) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2157 | : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL)) |
| 2158 | { |
| 2159 | ctrl_x_mode = 0; |
| 2160 | edit_submode = NULL; |
| 2161 | msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty") |
| 2162 | : (char_u *)_("'thesaurus' option is empty"), |
| 2163 | hl_attr(HLF_E)); |
| 2164 | if (emsg_silent == 0) |
| 2165 | { |
| 2166 | vim_beep(); |
| 2167 | setcursor(); |
| 2168 | out_flush(); |
| 2169 | ui_delay(2000L, FALSE); |
| 2170 | } |
| 2171 | return FALSE; |
| 2172 | } |
| 2173 | return TRUE; |
| 2174 | } |
| 2175 | |
| 2176 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2177 | * Is the character 'c' a valid key to go to or keep us in CTRL-X mode? |
| 2178 | * This depends on the current mode. |
| 2179 | */ |
| 2180 | int |
| 2181 | vim_is_ctrl_x_key(c) |
| 2182 | int c; |
| 2183 | { |
| 2184 | /* Always allow ^R - let it's results then be checked */ |
| 2185 | if (c == Ctrl_R) |
| 2186 | return TRUE; |
| 2187 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2188 | /* Accept <PageUp> and <PageDown> if the popup menu is visible. */ |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 2189 | if (ins_compl_pum_key(c)) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 2190 | return TRUE; |
| 2191 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2192 | switch (ctrl_x_mode) |
| 2193 | { |
| 2194 | case 0: /* Not in any CTRL-X mode */ |
| 2195 | return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X); |
| 2196 | case CTRL_X_NOT_DEFINED_YET: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2197 | return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2198 | || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB |
| 2199 | || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P |
| 2200 | || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2201 | || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O |
Bram Moolenaar | bbd9fd7 | 2011-12-23 13:15:03 +0100 | [diff] [blame] | 2202 | || c == Ctrl_S || c == Ctrl_K || c == 's'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2203 | case CTRL_X_SCROLL: |
| 2204 | return (c == Ctrl_Y || c == Ctrl_E); |
| 2205 | case CTRL_X_WHOLE_LINE: |
| 2206 | return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N); |
| 2207 | case CTRL_X_FILES: |
| 2208 | return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N); |
| 2209 | case CTRL_X_DICTIONARY: |
| 2210 | return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N); |
| 2211 | case CTRL_X_THESAURUS: |
| 2212 | return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N); |
| 2213 | case CTRL_X_TAGS: |
| 2214 | return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N); |
| 2215 | #ifdef FEAT_FIND_ID |
| 2216 | case CTRL_X_PATH_PATTERNS: |
| 2217 | return (c == Ctrl_P || c == Ctrl_N); |
| 2218 | case CTRL_X_PATH_DEFINES: |
| 2219 | return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N); |
| 2220 | #endif |
| 2221 | case CTRL_X_CMDLINE: |
| 2222 | return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N |
| 2223 | || c == Ctrl_X); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 2224 | #ifdef FEAT_COMPL_FUNC |
| 2225 | case CTRL_X_FUNCTION: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2226 | return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 2227 | case CTRL_X_OMNI: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2228 | return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2229 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 2230 | case CTRL_X_SPELL: |
| 2231 | return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2232 | } |
| 2233 | EMSG(_(e_internal)); |
| 2234 | return FALSE; |
| 2235 | } |
| 2236 | |
| 2237 | /* |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 2238 | * Return TRUE when character "c" is part of the item currently being |
| 2239 | * completed. Used to decide whether to abandon complete mode when the menu |
| 2240 | * is visible. |
| 2241 | */ |
| 2242 | static int |
| 2243 | ins_compl_accept_char(c) |
| 2244 | int c; |
| 2245 | { |
| 2246 | if (ctrl_x_mode & CTRL_X_WANT_IDENT) |
| 2247 | /* When expanding an identifier only accept identifier chars. */ |
| 2248 | return vim_isIDc(c); |
| 2249 | |
| 2250 | switch (ctrl_x_mode) |
| 2251 | { |
| 2252 | case CTRL_X_FILES: |
| 2253 | /* When expanding file name only accept file name chars. But not |
| 2254 | * path separators, so that "proto/<Tab>" expands files in |
| 2255 | * "proto", not "proto/" as a whole */ |
| 2256 | return vim_isfilec(c) && !vim_ispathsep(c); |
| 2257 | |
| 2258 | case CTRL_X_CMDLINE: |
| 2259 | case CTRL_X_OMNI: |
| 2260 | /* Command line and Omni completion can work with just about any |
| 2261 | * printable character, but do stop at white space. */ |
| 2262 | return vim_isprintc(c) && !vim_iswhite(c); |
| 2263 | |
| 2264 | case CTRL_X_WHOLE_LINE: |
| 2265 | /* For while line completion a space can be part of the line. */ |
| 2266 | return vim_isprintc(c); |
| 2267 | } |
| 2268 | return vim_iswordc(c); |
| 2269 | } |
| 2270 | |
| 2271 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2272 | * 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] | 2273 | * 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] | 2274 | * 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] | 2275 | * the rest of the word to be in -- webb |
| 2276 | */ |
| 2277 | int |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2278 | ins_compl_add_infercase(str, len, icase, fname, dir, flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2279 | char_u *str; |
| 2280 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2281 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | char_u *fname; |
| 2283 | int dir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2284 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2285 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2286 | char_u *p; |
| 2287 | int i, c; |
| 2288 | int actual_len; /* Take multi-byte characters */ |
| 2289 | int actual_compl_length; /* into account. */ |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2290 | int min_len; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 2291 | int *wca; /* Wide character array. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2292 | int has_lower = FALSE; |
| 2293 | int was_letter = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2294 | |
Bram Moolenaar | e40e57c | 2007-11-08 12:04:26 +0000 | [diff] [blame] | 2295 | if (p_ic && curbuf->b_p_inf && len > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2297 | /* Infer case of completed part. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2298 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2299 | /* Find actual length of completion. */ |
| 2300 | #ifdef FEAT_MBYTE |
| 2301 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2303 | p = str; |
| 2304 | actual_len = 0; |
| 2305 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2307 | mb_ptr_adv(p); |
| 2308 | ++actual_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2309 | } |
| 2310 | } |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2311 | else |
| 2312 | #endif |
| 2313 | actual_len = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2314 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2315 | /* Find actual length of original text. */ |
| 2316 | #ifdef FEAT_MBYTE |
| 2317 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2318 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2319 | p = compl_orig_text; |
| 2320 | actual_compl_length = 0; |
| 2321 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2322 | { |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2323 | mb_ptr_adv(p); |
| 2324 | ++actual_compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2325 | } |
| 2326 | } |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2327 | else |
| 2328 | #endif |
| 2329 | actual_compl_length = compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2331 | /* "actual_len" may be smaller than "actual_compl_length" when using |
| 2332 | * thesaurus, only use the minimum when comparing. */ |
| 2333 | min_len = actual_len < actual_compl_length |
| 2334 | ? actual_len : actual_compl_length; |
| 2335 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2336 | /* Allocate wide character array for the completion and fill it. */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2337 | wca = (int *)alloc((unsigned)(actual_len * sizeof(int))); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2338 | if (wca != NULL) |
| 2339 | { |
| 2340 | p = str; |
| 2341 | for (i = 0; i < actual_len; ++i) |
| 2342 | #ifdef FEAT_MBYTE |
| 2343 | if (has_mbyte) |
| 2344 | wca[i] = mb_ptr2char_adv(&p); |
| 2345 | else |
| 2346 | #endif |
| 2347 | wca[i] = *(p++); |
| 2348 | |
| 2349 | /* Rule 1: Were any chars converted to lower? */ |
| 2350 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2351 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2352 | { |
| 2353 | #ifdef FEAT_MBYTE |
| 2354 | if (has_mbyte) |
| 2355 | c = mb_ptr2char_adv(&p); |
| 2356 | else |
| 2357 | #endif |
| 2358 | c = *(p++); |
| 2359 | if (MB_ISLOWER(c)) |
| 2360 | { |
| 2361 | has_lower = TRUE; |
| 2362 | if (MB_ISUPPER(wca[i])) |
| 2363 | { |
| 2364 | /* Rule 1 is satisfied. */ |
| 2365 | for (i = actual_compl_length; i < actual_len; ++i) |
| 2366 | wca[i] = MB_TOLOWER(wca[i]); |
| 2367 | break; |
| 2368 | } |
| 2369 | } |
| 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * Rule 2: No lower case, 2nd consecutive letter converted to |
| 2374 | * upper case. |
| 2375 | */ |
| 2376 | if (!has_lower) |
| 2377 | { |
| 2378 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2379 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2380 | { |
| 2381 | #ifdef FEAT_MBYTE |
| 2382 | if (has_mbyte) |
| 2383 | c = mb_ptr2char_adv(&p); |
| 2384 | else |
| 2385 | #endif |
| 2386 | c = *(p++); |
| 2387 | if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i])) |
| 2388 | { |
| 2389 | /* Rule 2 is satisfied. */ |
| 2390 | for (i = actual_compl_length; i < actual_len; ++i) |
| 2391 | wca[i] = MB_TOUPPER(wca[i]); |
| 2392 | break; |
| 2393 | } |
| 2394 | was_letter = MB_ISLOWER(c) || MB_ISUPPER(c); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | /* Copy the original case of the part we typed. */ |
| 2399 | p = compl_orig_text; |
Bram Moolenaar | 04fa542 | 2010-05-28 21:31:58 +0200 | [diff] [blame] | 2400 | for (i = 0; i < min_len; ++i) |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2401 | { |
| 2402 | #ifdef FEAT_MBYTE |
| 2403 | if (has_mbyte) |
| 2404 | c = mb_ptr2char_adv(&p); |
| 2405 | else |
| 2406 | #endif |
| 2407 | c = *(p++); |
| 2408 | if (MB_ISLOWER(c)) |
| 2409 | wca[i] = MB_TOLOWER(wca[i]); |
| 2410 | else if (MB_ISUPPER(c)) |
| 2411 | wca[i] = MB_TOUPPER(wca[i]); |
| 2412 | } |
| 2413 | |
Bram Moolenaar | e40e57c | 2007-11-08 12:04:26 +0000 | [diff] [blame] | 2414 | /* |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2415 | * Generate encoding specific output from wide character array. |
| 2416 | * Multi-byte characters can occupy up to five bytes more than |
| 2417 | * ASCII characters, and we also need one byte for NUL, so stay |
| 2418 | * six bytes away from the edge of IObuff. |
| 2419 | */ |
| 2420 | p = IObuff; |
| 2421 | i = 0; |
| 2422 | while (i < actual_len && (p - IObuff + 6) < IOSIZE) |
| 2423 | #ifdef FEAT_MBYTE |
| 2424 | if (has_mbyte) |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 2425 | p += (*mb_char2bytes)(wca[i++], p); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 2426 | else |
| 2427 | #endif |
| 2428 | *(p++) = wca[i++]; |
| 2429 | *p = NUL; |
| 2430 | |
| 2431 | vim_free(wca); |
| 2432 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2433 | |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2434 | return ins_compl_add(IObuff, len, icase, fname, NULL, dir, |
| 2435 | flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2436 | } |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2437 | return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
| 2440 | /* |
| 2441 | * Add a match to the list of matches. |
| 2442 | * 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] | 2443 | * 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] | 2444 | * maybe because alloc() returns NULL, then FAIL is returned. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | */ |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2446 | static int |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2447 | ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2448 | char_u *str; |
| 2449 | int len; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2450 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | char_u *fname; |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2452 | char_u **cptext; /* extra text for popup menu or NULL */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2453 | int cdir; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2454 | int flags; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2455 | int adup; /* accept duplicate match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2456 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2457 | compl_T *match; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2458 | int dir = (cdir == 0 ? compl_direction : cdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2459 | |
| 2460 | ui_breakcheck(); |
| 2461 | if (got_int) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2462 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2463 | if (len < 0) |
| 2464 | len = (int)STRLEN(str); |
| 2465 | |
| 2466 | /* |
| 2467 | * If the same match is already present, don't add it. |
| 2468 | */ |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2469 | if (compl_first_match != NULL && !adup) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2471 | match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2472 | do |
| 2473 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2474 | if ( !(match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 5948a57 | 2006-10-03 13:49:29 +0000 | [diff] [blame] | 2475 | && STRNCMP(match->cp_str, str, len) == 0 |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2476 | && match->cp_str[len] == NUL) |
| 2477 | return NOTDONE; |
| 2478 | match = match->cp_next; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2479 | } while (match != NULL && match != compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2482 | /* Remove any popup menu before changing the list of matches. */ |
| 2483 | ins_compl_del_pum(); |
| 2484 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | /* |
| 2486 | * Allocate a new match structure. |
| 2487 | * Copy the values to the new match structure. |
| 2488 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2489 | match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2490 | if (match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2491 | return FAIL; |
| 2492 | match->cp_number = -1; |
| 2493 | if (flags & ORIGINAL_TEXT) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2494 | match->cp_number = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2495 | if ((match->cp_str = vim_strnsave(str, len)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2496 | { |
| 2497 | vim_free(match); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2498 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | } |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2500 | match->cp_icase = icase; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2501 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2502 | /* match-fname is: |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2503 | * - 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] | 2504 | * - a copy of fname, FREE_FNAME is set to free later THE allocated mem. |
| 2505 | * - NULL otherwise. --Acevedo */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2506 | if (fname != NULL |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2507 | && compl_curr_match != NULL |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2508 | && compl_curr_match->cp_fname != NULL |
| 2509 | && STRCMP(fname, compl_curr_match->cp_fname) == 0) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2510 | match->cp_fname = compl_curr_match->cp_fname; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2511 | else if (fname != NULL) |
| 2512 | { |
| 2513 | match->cp_fname = vim_strsave(fname); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2514 | flags |= FREE_FNAME; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2515 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2516 | else |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2517 | match->cp_fname = NULL; |
| 2518 | match->cp_flags = flags; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2519 | |
| 2520 | if (cptext != NULL) |
| 2521 | { |
| 2522 | int i; |
| 2523 | |
| 2524 | for (i = 0; i < CPT_COUNT; ++i) |
| 2525 | if (cptext[i] != NULL && *cptext[i] != NUL) |
| 2526 | match->cp_text[i] = vim_strsave(cptext[i]); |
| 2527 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | |
| 2529 | /* |
| 2530 | * Link the new match structure in the list of matches. |
| 2531 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2532 | if (compl_first_match == NULL) |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2533 | match->cp_next = match->cp_prev = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2534 | else if (dir == FORWARD) |
| 2535 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2536 | match->cp_next = compl_curr_match->cp_next; |
| 2537 | match->cp_prev = compl_curr_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2538 | } |
| 2539 | else /* BACKWARD */ |
| 2540 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2541 | match->cp_next = compl_curr_match; |
| 2542 | match->cp_prev = compl_curr_match->cp_prev; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2544 | if (match->cp_next) |
| 2545 | match->cp_next->cp_prev = match; |
| 2546 | if (match->cp_prev) |
| 2547 | match->cp_prev->cp_next = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2548 | else /* if there's nothing before, it is the first match */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2549 | compl_first_match = match; |
| 2550 | compl_curr_match = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2551 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2552 | /* |
| 2553 | * Find the longest common string if still doing that. |
| 2554 | */ |
| 2555 | if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0) |
| 2556 | ins_compl_longest_match(match); |
| 2557 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2558 | return OK; |
| 2559 | } |
| 2560 | |
| 2561 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2562 | * Return TRUE if "str[len]" matches with match->cp_str, considering |
| 2563 | * match->cp_icase. |
| 2564 | */ |
| 2565 | static int |
| 2566 | ins_compl_equal(match, str, len) |
| 2567 | compl_T *match; |
| 2568 | char_u *str; |
| 2569 | int len; |
| 2570 | { |
| 2571 | if (match->cp_icase) |
| 2572 | return STRNICMP(match->cp_str, str, (size_t)len) == 0; |
| 2573 | return STRNCMP(match->cp_str, str, (size_t)len) == 0; |
| 2574 | } |
| 2575 | |
| 2576 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2577 | * Reduce the longest common string for match "match". |
| 2578 | */ |
| 2579 | static void |
| 2580 | ins_compl_longest_match(match) |
| 2581 | compl_T *match; |
| 2582 | { |
| 2583 | char_u *p, *s; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2584 | int c1, c2; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2585 | int had_match; |
| 2586 | |
| 2587 | if (compl_leader == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2588 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2589 | /* First match, use it as a whole. */ |
| 2590 | compl_leader = vim_strsave(match->cp_str); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2591 | if (compl_leader != NULL) |
| 2592 | { |
| 2593 | had_match = (curwin->w_cursor.col > compl_col); |
| 2594 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2595 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 2596 | ins_redraw(FALSE); |
| 2597 | |
| 2598 | /* When the match isn't there (to avoid matching itself) remove it |
| 2599 | * again after redrawing. */ |
| 2600 | if (!had_match) |
| 2601 | ins_compl_delete(); |
| 2602 | compl_used_match = FALSE; |
| 2603 | } |
| 2604 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2605 | else |
| 2606 | { |
| 2607 | /* Reduce the text if this match differs from compl_leader. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2608 | p = compl_leader; |
| 2609 | s = match->cp_str; |
| 2610 | while (*p != NUL) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2611 | { |
| 2612 | #ifdef FEAT_MBYTE |
| 2613 | if (has_mbyte) |
| 2614 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2615 | c1 = mb_ptr2char(p); |
| 2616 | c2 = mb_ptr2char(s); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2617 | } |
| 2618 | else |
| 2619 | #endif |
| 2620 | { |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2621 | c1 = *p; |
| 2622 | c2 = *s; |
| 2623 | } |
| 2624 | if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2)) |
| 2625 | : (c1 != c2)) |
| 2626 | break; |
| 2627 | #ifdef FEAT_MBYTE |
| 2628 | if (has_mbyte) |
| 2629 | { |
| 2630 | mb_ptr_adv(p); |
| 2631 | mb_ptr_adv(s); |
| 2632 | } |
| 2633 | else |
| 2634 | #endif |
| 2635 | { |
| 2636 | ++p; |
| 2637 | ++s; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | if (*p != NUL) |
| 2642 | { |
| 2643 | /* Leader was shortened, need to change the inserted text. */ |
| 2644 | *p = NUL; |
| 2645 | had_match = (curwin->w_cursor.col > compl_col); |
| 2646 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 2647 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2648 | ins_redraw(FALSE); |
| 2649 | |
| 2650 | /* When the match isn't there (to avoid matching itself) remove it |
| 2651 | * again after redrawing. */ |
| 2652 | if (!had_match) |
| 2653 | ins_compl_delete(); |
| 2654 | } |
| 2655 | |
| 2656 | compl_used_match = FALSE; |
| 2657 | } |
| 2658 | } |
| 2659 | |
| 2660 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2661 | * Add an array of matches to the list of matches. |
| 2662 | * Frees matches[]. |
| 2663 | */ |
| 2664 | static void |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2665 | ins_compl_add_matches(num_matches, matches, icase) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | int num_matches; |
| 2667 | char_u **matches; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2668 | int icase; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2669 | { |
| 2670 | int i; |
| 2671 | int add_r = OK; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2672 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2673 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2674 | for (i = 0; i < num_matches && add_r != FAIL; i++) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2675 | if ((add_r = ins_compl_add(matches[i], -1, icase, |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 2676 | NULL, NULL, dir, 0, FALSE)) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2677 | /* if dir was BACKWARD then honor it just once */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2678 | dir = FORWARD; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2679 | FreeWild(num_matches, matches); |
| 2680 | } |
| 2681 | |
| 2682 | /* Make the completion list cyclic. |
| 2683 | * Return the number of matches (excluding the original). |
| 2684 | */ |
| 2685 | static int |
| 2686 | ins_compl_make_cyclic() |
| 2687 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2688 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2689 | int count = 0; |
| 2690 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2691 | if (compl_first_match != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2692 | { |
| 2693 | /* |
| 2694 | * Find the end of the list. |
| 2695 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 2696 | match = compl_first_match; |
| 2697 | /* 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] | 2698 | while (match->cp_next != NULL && match->cp_next != compl_first_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2699 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2700 | match = match->cp_next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | ++count; |
| 2702 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 2703 | match->cp_next = compl_first_match; |
| 2704 | compl_first_match->cp_prev = match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2705 | } |
| 2706 | return count; |
| 2707 | } |
| 2708 | |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2709 | /* |
| 2710 | * Start completion for the complete() function. |
| 2711 | * "startcol" is where the matched text starts (1 is first column). |
| 2712 | * "list" is the list of matches. |
| 2713 | */ |
| 2714 | void |
| 2715 | set_completion(startcol, list) |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2716 | colnr_T startcol; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2717 | list_T *list; |
| 2718 | { |
| 2719 | /* If already doing completions stop it. */ |
| 2720 | if (ctrl_x_mode != 0) |
| 2721 | ins_compl_prep(' '); |
| 2722 | ins_compl_clear(); |
| 2723 | |
| 2724 | if (stop_arrow() == FAIL) |
| 2725 | return; |
| 2726 | |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 2727 | compl_direction = FORWARD; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2728 | if (startcol > curwin->w_cursor.col) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2729 | startcol = curwin->w_cursor.col; |
| 2730 | compl_col = startcol; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 2731 | compl_length = (int)curwin->w_cursor.col - (int)startcol; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2732 | /* compl_pattern doesn't need to be set */ |
| 2733 | compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length); |
| 2734 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 2735 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2736 | return; |
| 2737 | |
| 2738 | /* Handle like dictionary completion. */ |
| 2739 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 2740 | |
| 2741 | ins_compl_add_list(list); |
| 2742 | compl_matches = ins_compl_make_cyclic(); |
| 2743 | compl_started = TRUE; |
| 2744 | compl_used_match = TRUE; |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 2745 | compl_cont_status = 0; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 2746 | |
| 2747 | compl_curr_match = compl_first_match; |
| 2748 | ins_complete(Ctrl_N); |
| 2749 | out_flush(); |
| 2750 | } |
| 2751 | |
| 2752 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2753 | /* "compl_match_array" points the currently displayed list of entries in the |
| 2754 | * popup menu. It is NULL when there is no popup menu. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2755 | static pumitem_T *compl_match_array = NULL; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2756 | static int compl_match_arraysize; |
| 2757 | |
| 2758 | /* |
| 2759 | * Update the screen and when there is any scrolling remove the popup menu. |
| 2760 | */ |
| 2761 | static void |
| 2762 | ins_compl_upd_pum() |
| 2763 | { |
| 2764 | int h; |
| 2765 | |
| 2766 | if (compl_match_array != NULL) |
| 2767 | { |
| 2768 | h = curwin->w_cline_height; |
| 2769 | update_screen(0); |
| 2770 | if (h != curwin->w_cline_height) |
| 2771 | ins_compl_del_pum(); |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | /* |
| 2776 | * Remove any popup menu. |
| 2777 | */ |
| 2778 | static void |
| 2779 | ins_compl_del_pum() |
| 2780 | { |
| 2781 | if (compl_match_array != NULL) |
| 2782 | { |
| 2783 | pum_undisplay(); |
| 2784 | vim_free(compl_match_array); |
| 2785 | compl_match_array = NULL; |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | /* |
| 2790 | * Return TRUE if the popup menu should be displayed. |
| 2791 | */ |
| 2792 | static int |
| 2793 | pum_wanted() |
| 2794 | { |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2795 | /* 'completeopt' must contain "menu" or "menuone" */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 2796 | if (vim_strchr(p_cot, 'm') == NULL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2797 | return FALSE; |
| 2798 | |
| 2799 | /* The display looks bad on a B&W display. */ |
| 2800 | if (t_colors < 8 |
| 2801 | #ifdef FEAT_GUI |
| 2802 | && !gui.in_use |
| 2803 | #endif |
| 2804 | ) |
| 2805 | return FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2806 | return TRUE; |
| 2807 | } |
| 2808 | |
| 2809 | /* |
| 2810 | * 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] | 2811 | * One if 'completopt' contains "menuone". |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2812 | */ |
| 2813 | static int |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2814 | pum_enough_matches() |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2815 | { |
| 2816 | compl_T *compl; |
| 2817 | int i; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2818 | |
| 2819 | /* Don't display the popup menu if there are no matches or there is only |
| 2820 | * one (ignoring the original text). */ |
| 2821 | compl = compl_first_match; |
| 2822 | i = 0; |
| 2823 | do |
| 2824 | { |
| 2825 | if (compl == NULL |
| 2826 | || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2)) |
| 2827 | break; |
| 2828 | compl = compl->cp_next; |
| 2829 | } while (compl != compl_first_match); |
| 2830 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2831 | if (strstr((char *)p_cot, "menuone") != NULL) |
| 2832 | return (i >= 1); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2833 | return (i >= 2); |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * Show the popup menu for the list of matches. |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2838 | * Also adjusts "compl_shown_match" to an entry that is actually displayed. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2839 | */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2840 | void |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2841 | ins_compl_show_pum() |
| 2842 | { |
| 2843 | compl_T *compl; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2844 | compl_T *shown_compl = NULL; |
| 2845 | int did_find_shown_match = FALSE; |
| 2846 | int shown_match_ok = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2847 | int i; |
| 2848 | int cur = -1; |
| 2849 | colnr_T col; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2850 | int lead_len = 0; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2851 | |
Bram Moolenaar | 65c923a | 2006-03-03 22:56:30 +0000 | [diff] [blame] | 2852 | if (!pum_wanted() || !pum_enough_matches()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2853 | return; |
| 2854 | |
Bram Moolenaar | 433f7c8 | 2006-03-21 21:29:36 +0000 | [diff] [blame] | 2855 | #if defined(FEAT_EVAL) |
| 2856 | /* Dirty hard-coded hack: remove any matchparen highlighting. */ |
| 2857 | do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif"); |
| 2858 | #endif |
| 2859 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2860 | /* Update the screen before drawing the popup menu over it. */ |
| 2861 | update_screen(0); |
| 2862 | |
| 2863 | if (compl_match_array == NULL) |
| 2864 | { |
| 2865 | /* Need to build the popup menu list. */ |
| 2866 | compl_match_arraysize = 0; |
| 2867 | compl = compl_first_match; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2868 | if (compl_leader != NULL) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2869 | lead_len = (int)STRLEN(compl_leader); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2870 | do |
| 2871 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2872 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2873 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2874 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2875 | ++compl_match_arraysize; |
| 2876 | compl = compl->cp_next; |
| 2877 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2878 | if (compl_match_arraysize == 0) |
| 2879 | return; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2880 | compl_match_array = (pumitem_T *)alloc_clear( |
| 2881 | (unsigned)(sizeof(pumitem_T) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2882 | * compl_match_arraysize)); |
| 2883 | if (compl_match_array != NULL) |
| 2884 | { |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2885 | /* If the current match is the original text don't find the first |
| 2886 | * match after it, don't highlight anything. */ |
| 2887 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 2888 | shown_match_ok = TRUE; |
| 2889 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2890 | i = 0; |
| 2891 | compl = compl_first_match; |
| 2892 | do |
| 2893 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2894 | if ((compl->cp_flags & ORIGINAL_TEXT) == 0 |
| 2895 | && (compl_leader == NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 2896 | || ins_compl_equal(compl, compl_leader, lead_len))) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2897 | { |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2898 | if (!shown_match_ok) |
| 2899 | { |
| 2900 | if (compl == compl_shown_match || did_find_shown_match) |
| 2901 | { |
| 2902 | /* This item is the shown match or this is the |
| 2903 | * first displayed item after the shown match. */ |
| 2904 | compl_shown_match = compl; |
| 2905 | did_find_shown_match = TRUE; |
| 2906 | shown_match_ok = TRUE; |
| 2907 | } |
| 2908 | else |
| 2909 | /* Remember this displayed match for when the |
| 2910 | * shown match is just below it. */ |
| 2911 | shown_compl = compl; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2912 | cur = i; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2913 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2914 | |
| 2915 | if (compl->cp_text[CPT_ABBR] != NULL) |
| 2916 | compl_match_array[i].pum_text = |
| 2917 | compl->cp_text[CPT_ABBR]; |
| 2918 | else |
| 2919 | compl_match_array[i].pum_text = compl->cp_str; |
| 2920 | compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND]; |
| 2921 | compl_match_array[i].pum_info = compl->cp_text[CPT_INFO]; |
| 2922 | if (compl->cp_text[CPT_MENU] != NULL) |
| 2923 | compl_match_array[i++].pum_extra = |
| 2924 | compl->cp_text[CPT_MENU]; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2925 | else |
| 2926 | compl_match_array[i++].pum_extra = compl->cp_fname; |
| 2927 | } |
| 2928 | |
| 2929 | if (compl == compl_shown_match) |
| 2930 | { |
| 2931 | did_find_shown_match = TRUE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 2932 | |
| 2933 | /* When the original text is the shown match don't set |
| 2934 | * compl_shown_match. */ |
| 2935 | if (compl->cp_flags & ORIGINAL_TEXT) |
| 2936 | shown_match_ok = TRUE; |
| 2937 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2938 | if (!shown_match_ok && shown_compl != NULL) |
| 2939 | { |
| 2940 | /* The shown match isn't displayed, set it to the |
| 2941 | * previously displayed match. */ |
| 2942 | compl_shown_match = shown_compl; |
| 2943 | shown_match_ok = TRUE; |
| 2944 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2945 | } |
| 2946 | compl = compl->cp_next; |
| 2947 | } while (compl != NULL && compl != compl_first_match); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2948 | |
| 2949 | if (!shown_match_ok) /* no displayed match at all */ |
| 2950 | cur = -1; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2951 | } |
| 2952 | } |
| 2953 | else |
| 2954 | { |
| 2955 | /* popup menu already exists, only need to find the current item.*/ |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2956 | for (i = 0; i < compl_match_arraysize; ++i) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 2957 | if (compl_match_array[i].pum_text == compl_shown_match->cp_str |
| 2958 | || compl_match_array[i].pum_text |
| 2959 | == compl_shown_match->cp_text[CPT_ABBR]) |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2960 | { |
| 2961 | cur = i; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 2962 | break; |
Bram Moolenaar | 9e54a0e | 2006-04-14 20:42:25 +0000 | [diff] [blame] | 2963 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | if (compl_match_array != NULL) |
| 2967 | { |
| 2968 | /* Compute the screen column of the start of the completed text. |
| 2969 | * Use the cursor to get all wrapping and other settings right. */ |
| 2970 | col = curwin->w_cursor.col; |
| 2971 | curwin->w_cursor.col = compl_col; |
Bram Moolenaar | d289f13 | 2006-03-11 21:30:53 +0000 | [diff] [blame] | 2972 | pum_display(compl_match_array, compl_match_arraysize, cur); |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 2973 | curwin->w_cursor.col = col; |
| 2974 | } |
| 2975 | } |
| 2976 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2977 | #define DICT_FIRST (1) /* use just first element in "dict" */ |
| 2978 | #define DICT_EXACT (2) /* "dict" is the exact name of a file */ |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 2979 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2980 | /* |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2981 | * Add any identifiers that match the given pattern in the list of dictionary |
| 2982 | * files "dict_start" to the list of completions. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2983 | */ |
| 2984 | static void |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2985 | ins_compl_dictionaries(dict_start, pat, flags, thesaurus) |
| 2986 | char_u *dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2987 | char_u *pat; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 2988 | int flags; /* DICT_FIRST and/or DICT_EXACT */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2989 | int thesaurus; /* Thesaurus completion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | { |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 2991 | char_u *dict = dict_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | char_u *ptr; |
| 2993 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2994 | regmatch_T regmatch; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2995 | char_u **files; |
| 2996 | int count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2997 | int save_p_scs; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 2998 | int dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2999 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3000 | if (*dict == NUL) |
| 3001 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3002 | #ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3003 | /* When 'dictionary' is empty and spell checking is enabled use |
| 3004 | * "spell". */ |
| 3005 | if (!thesaurus && curwin->w_p_spell) |
| 3006 | dict = (char_u *)"spell"; |
| 3007 | else |
| 3008 | #endif |
| 3009 | return; |
| 3010 | } |
| 3011 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | buf = alloc(LSIZE); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3013 | if (buf == NULL) |
| 3014 | return; |
Bram Moolenaar | fa3491a | 2007-02-20 02:49:19 +0000 | [diff] [blame] | 3015 | regmatch.regprog = NULL; /* so that we can goto theend */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3016 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3017 | /* If 'infercase' is set, don't use 'smartcase' here */ |
| 3018 | save_p_scs = p_scs; |
| 3019 | if (curbuf->b_p_inf) |
| 3020 | p_scs = FALSE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3021 | |
| 3022 | /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern |
| 3023 | * 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] | 3024 | * pattern. Also need to double backslashes. */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3025 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3026 | { |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3027 | char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\"); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3028 | size_t len; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3029 | |
| 3030 | if (pat_esc == NULL) |
Bram Moolenaar | 6519ac8 | 2007-05-06 13:45:52 +0000 | [diff] [blame] | 3031 | goto theend; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3032 | len = STRLEN(pat_esc) + 10; |
| 3033 | ptr = alloc((unsigned)len); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3034 | if (ptr == NULL) |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3035 | { |
| 3036 | vim_free(pat_esc); |
Bram Moolenaar | fa3491a | 2007-02-20 02:49:19 +0000 | [diff] [blame] | 3037 | goto theend; |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3038 | } |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3039 | vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc); |
Bram Moolenaar | f9393ef | 2006-04-24 19:47:27 +0000 | [diff] [blame] | 3040 | regmatch.regprog = vim_regcomp(ptr, RE_MAGIC); |
| 3041 | vim_free(pat_esc); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3042 | vim_free(ptr); |
| 3043 | } |
| 3044 | else |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3045 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3046 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3047 | if (regmatch.regprog == NULL) |
| 3048 | goto theend; |
| 3049 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3050 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3051 | /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */ |
| 3052 | regmatch.rm_ic = ignorecase(pat); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3053 | while (*dict != NUL && !got_int && !compl_interrupted) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3054 | { |
| 3055 | /* copy one dictionary file name into buf */ |
| 3056 | if (flags == DICT_EXACT) |
| 3057 | { |
| 3058 | count = 1; |
| 3059 | files = &dict; |
| 3060 | } |
| 3061 | else |
| 3062 | { |
| 3063 | /* Expand wildcards in the dictionary name, but do not allow |
| 3064 | * backticks (for security, the 'dict' option may have been set in |
| 3065 | * a modeline). */ |
| 3066 | copy_option_part(&dict, buf, LSIZE, ","); |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3067 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3068 | if (!thesaurus && STRCMP(buf, "spell") == 0) |
| 3069 | count = -1; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3070 | else |
| 3071 | # endif |
| 3072 | if (vim_strchr(buf, '`') != NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3073 | || expand_wildcards(1, &buf, &count, &files, |
| 3074 | EW_FILE|EW_SILENT) != OK) |
| 3075 | count = 0; |
| 3076 | } |
| 3077 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3078 | # ifdef FEAT_SPELL |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3079 | if (count == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3080 | { |
Bram Moolenaar | 87b5ca5 | 2006-03-04 21:55:31 +0000 | [diff] [blame] | 3081 | /* Complete from active spelling. Skip "\<" in the pattern, we |
| 3082 | * don't use it as a RE. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3083 | if (pat[0] == '\\' && pat[1] == '<') |
| 3084 | ptr = pat + 2; |
| 3085 | else |
| 3086 | ptr = pat; |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 3087 | spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3088 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3089 | else |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3090 | # endif |
Bram Moolenaar | d7fd0c4 | 2006-08-22 17:55:55 +0000 | [diff] [blame] | 3091 | if (count > 0) /* avoid warning for using "files" uninit */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3092 | { |
| 3093 | ins_compl_files(count, files, thesaurus, flags, |
| 3094 | ®match, buf, &dir); |
| 3095 | if (flags != DICT_EXACT) |
| 3096 | FreeWild(count, files); |
| 3097 | } |
| 3098 | if (flags != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3099 | break; |
| 3100 | } |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3101 | |
| 3102 | theend: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3103 | p_scs = save_p_scs; |
| 3104 | vim_free(regmatch.regprog); |
| 3105 | vim_free(buf); |
| 3106 | } |
| 3107 | |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3108 | static void |
| 3109 | ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir) |
| 3110 | int count; |
| 3111 | char_u **files; |
| 3112 | int thesaurus; |
| 3113 | int flags; |
| 3114 | regmatch_T *regmatch; |
| 3115 | char_u *buf; |
| 3116 | int *dir; |
| 3117 | { |
| 3118 | char_u *ptr; |
| 3119 | int i; |
| 3120 | FILE *fp; |
| 3121 | int add_r; |
| 3122 | |
| 3123 | for (i = 0; i < count && !got_int && !compl_interrupted; i++) |
| 3124 | { |
| 3125 | fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */ |
| 3126 | if (flags != DICT_EXACT) |
| 3127 | { |
| 3128 | vim_snprintf((char *)IObuff, IOSIZE, |
| 3129 | _("Scanning dictionary: %s"), (char *)files[i]); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3130 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | if (fp != NULL) |
| 3134 | { |
| 3135 | /* |
| 3136 | * Read dictionary file line by line. |
| 3137 | * Check each line for a match. |
| 3138 | */ |
| 3139 | while (!got_int && !compl_interrupted |
| 3140 | && !vim_fgets(buf, LSIZE, fp)) |
| 3141 | { |
| 3142 | ptr = buf; |
| 3143 | while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf))) |
| 3144 | { |
| 3145 | ptr = regmatch->startp[0]; |
| 3146 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 3147 | ptr = find_line_end(ptr); |
| 3148 | else |
| 3149 | ptr = find_word_end(ptr); |
| 3150 | add_r = ins_compl_add_infercase(regmatch->startp[0], |
| 3151 | (int)(ptr - regmatch->startp[0]), |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 3152 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3153 | if (thesaurus) |
| 3154 | { |
| 3155 | char_u *wstart; |
| 3156 | |
| 3157 | /* |
| 3158 | * Add the other matches on the line |
| 3159 | */ |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3160 | ptr = buf; |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3161 | while (!got_int) |
| 3162 | { |
| 3163 | /* Find start of the next word. Skip white |
| 3164 | * space and punctuation. */ |
| 3165 | ptr = find_word_start(ptr); |
| 3166 | if (*ptr == NUL || *ptr == NL) |
| 3167 | break; |
| 3168 | wstart = ptr; |
| 3169 | |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3170 | /* Find end of the word. */ |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3171 | #ifdef FEAT_MBYTE |
| 3172 | if (has_mbyte) |
| 3173 | /* Japanese words may have characters in |
| 3174 | * different classes, only separate words |
| 3175 | * with single-byte non-word characters. */ |
| 3176 | while (*ptr != NUL) |
| 3177 | { |
| 3178 | int l = (*mb_ptr2len)(ptr); |
| 3179 | |
| 3180 | if (l < 2 && !vim_iswordc(*ptr)) |
| 3181 | break; |
| 3182 | ptr += l; |
| 3183 | } |
| 3184 | else |
| 3185 | #endif |
| 3186 | ptr = find_word_end(ptr); |
Bram Moolenaar | a2993e1 | 2007-08-12 14:38:46 +0000 | [diff] [blame] | 3187 | |
| 3188 | /* Add the word. Skip the regexp match. */ |
| 3189 | if (wstart != regmatch->startp[0]) |
| 3190 | add_r = ins_compl_add_infercase(wstart, |
| 3191 | (int)(ptr - wstart), |
| 3192 | p_ic, files[i], *dir, 0); |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | if (add_r == OK) |
| 3196 | /* if dir was BACKWARD then honor it just once */ |
| 3197 | *dir = FORWARD; |
| 3198 | else if (add_r == FAIL) |
| 3199 | break; |
| 3200 | /* avoid expensive call to vim_regexec() when at end |
| 3201 | * of line */ |
| 3202 | if (*ptr == '\n' || got_int) |
| 3203 | break; |
| 3204 | } |
| 3205 | line_breakcheck(); |
| 3206 | ins_compl_check_keys(50); |
| 3207 | } |
| 3208 | fclose(fp); |
| 3209 | } |
| 3210 | } |
| 3211 | } |
| 3212 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3213 | /* |
| 3214 | * Find the start of the next word. |
| 3215 | * Returns a pointer to the first char of the word. Also stops at a NUL. |
| 3216 | */ |
| 3217 | char_u * |
| 3218 | find_word_start(ptr) |
| 3219 | char_u *ptr; |
| 3220 | { |
| 3221 | #ifdef FEAT_MBYTE |
| 3222 | if (has_mbyte) |
| 3223 | while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3224 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3225 | else |
| 3226 | #endif |
| 3227 | while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr)) |
| 3228 | ++ptr; |
| 3229 | return ptr; |
| 3230 | } |
| 3231 | |
| 3232 | /* |
| 3233 | * Find the end of the word. Assumes it starts inside a word. |
| 3234 | * Returns a pointer to just after the word. |
| 3235 | */ |
| 3236 | char_u * |
| 3237 | find_word_end(ptr) |
| 3238 | char_u *ptr; |
| 3239 | { |
| 3240 | #ifdef FEAT_MBYTE |
| 3241 | int start_class; |
| 3242 | |
| 3243 | if (has_mbyte) |
| 3244 | { |
| 3245 | start_class = mb_get_class(ptr); |
| 3246 | if (start_class > 1) |
| 3247 | while (*ptr != NUL) |
| 3248 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3249 | ptr += (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3250 | if (mb_get_class(ptr) != start_class) |
| 3251 | break; |
| 3252 | } |
| 3253 | } |
| 3254 | else |
| 3255 | #endif |
| 3256 | while (vim_iswordc(*ptr)) |
| 3257 | ++ptr; |
| 3258 | return ptr; |
| 3259 | } |
| 3260 | |
| 3261 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3262 | * Find the end of the line, omitting CR and NL at the end. |
| 3263 | * Returns a pointer to just after the line. |
| 3264 | */ |
| 3265 | static char_u * |
| 3266 | find_line_end(ptr) |
| 3267 | char_u *ptr; |
| 3268 | { |
| 3269 | char_u *s; |
| 3270 | |
| 3271 | s = ptr + STRLEN(ptr); |
| 3272 | while (s > ptr && (s[-1] == CAR || s[-1] == NL)) |
| 3273 | --s; |
| 3274 | return s; |
| 3275 | } |
| 3276 | |
| 3277 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3278 | * Free the list of completions |
| 3279 | */ |
| 3280 | static void |
| 3281 | ins_compl_free() |
| 3282 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3283 | compl_T *match; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3284 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3285 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3286 | vim_free(compl_pattern); |
| 3287 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3288 | vim_free(compl_leader); |
| 3289 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3290 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3291 | if (compl_first_match == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3292 | return; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3293 | |
| 3294 | ins_compl_del_pum(); |
| 3295 | pum_clear(); |
| 3296 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3297 | compl_curr_match = compl_first_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3298 | do |
| 3299 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3300 | match = compl_curr_match; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3301 | compl_curr_match = compl_curr_match->cp_next; |
| 3302 | vim_free(match->cp_str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3303 | /* several entries may use the same fname, free it just once. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3304 | if (match->cp_flags & FREE_FNAME) |
| 3305 | vim_free(match->cp_fname); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3306 | for (i = 0; i < CPT_COUNT; ++i) |
| 3307 | vim_free(match->cp_text[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3308 | vim_free(match); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3309 | } while (compl_curr_match != NULL && compl_curr_match != compl_first_match); |
| 3310 | compl_first_match = compl_curr_match = NULL; |
Bram Moolenaar | 031e0dd | 2009-07-09 16:15:16 +0000 | [diff] [blame] | 3311 | compl_shown_match = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | static void |
| 3315 | ins_compl_clear() |
| 3316 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3317 | compl_cont_status = 0; |
| 3318 | compl_started = FALSE; |
| 3319 | compl_matches = 0; |
| 3320 | vim_free(compl_pattern); |
| 3321 | compl_pattern = NULL; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3322 | vim_free(compl_leader); |
| 3323 | compl_leader = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3324 | edit_submode_extra = NULL; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3325 | vim_free(compl_orig_text); |
| 3326 | compl_orig_text = NULL; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3327 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /* |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 3331 | * Return TRUE when Insert completion is active. |
| 3332 | */ |
| 3333 | int |
| 3334 | ins_compl_active() |
| 3335 | { |
| 3336 | return compl_started; |
| 3337 | } |
| 3338 | |
| 3339 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3340 | * Delete one character before the cursor and show the subset of the matches |
| 3341 | * that match the word that is now before the cursor. |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3342 | * Returns the character to be used, NUL if the work is done and another char |
| 3343 | * to be got from the user. |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3344 | */ |
| 3345 | static int |
| 3346 | ins_compl_bs() |
| 3347 | { |
| 3348 | char_u *line; |
| 3349 | char_u *p; |
| 3350 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3351 | line = ml_get_curline(); |
| 3352 | p = line + curwin->w_cursor.col; |
| 3353 | mb_ptr_back(line, p); |
| 3354 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 3355 | /* Stop completion when the whole word was deleted. For Omni completion |
| 3356 | * allow the word to be deleted, we won't match everything. */ |
| 3357 | if ((int)(p - line) - (int)compl_col < 0 |
| 3358 | || ((int)(p - line) - (int)compl_col == 0 |
| 3359 | && (ctrl_x_mode & CTRL_X_OMNI) == 0)) |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3360 | return K_BS; |
| 3361 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3362 | /* Deleted more than what was used to find matches or didn't finish |
| 3363 | * finding all matches: need to look for matches all over again. */ |
| 3364 | if (curwin->w_cursor.col <= compl_col + compl_length |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3365 | || ins_compl_need_restart()) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3366 | ins_compl_restart(); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3367 | |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3368 | vim_free(compl_leader); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 3369 | compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3370 | if (compl_leader != NULL) |
| 3371 | { |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3372 | ins_compl_new_leader(); |
| 3373 | return NUL; |
| 3374 | } |
| 3375 | return K_BS; |
| 3376 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3377 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3378 | /* |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3379 | * Return TRUE when we need to find matches again, ins_compl_restart() is to |
| 3380 | * be called. |
| 3381 | */ |
| 3382 | static int |
| 3383 | ins_compl_need_restart() |
| 3384 | { |
| 3385 | /* Return TRUE if we didn't complete finding matches or when the |
| 3386 | * 'completefunc' returned "always" in the "refresh" dictionary item. */ |
| 3387 | return compl_was_interrupted |
| 3388 | || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI) |
| 3389 | && compl_opt_refresh_always); |
| 3390 | } |
| 3391 | |
| 3392 | /* |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3393 | * Called after changing "compl_leader". |
| 3394 | * Show the popup menu with a different set of matches. |
| 3395 | * May also search for matches again if the previous search was interrupted. |
| 3396 | */ |
| 3397 | static void |
| 3398 | ins_compl_new_leader() |
| 3399 | { |
| 3400 | ins_compl_del_pum(); |
| 3401 | ins_compl_delete(); |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3402 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3403 | compl_used_match = FALSE; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3404 | |
| 3405 | if (compl_started) |
| 3406 | ins_compl_set_original_text(compl_leader); |
| 3407 | else |
| 3408 | { |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3409 | #ifdef FEAT_SPELL |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3410 | spell_bad_len = 0; /* need to redetect bad word */ |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 3411 | #endif |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3412 | /* |
| 3413 | * Matches were cleared, need to search for them now. First display |
| 3414 | * the changed text before the cursor. Set "compl_restarting" to |
| 3415 | * avoid that the first match is inserted. |
| 3416 | */ |
| 3417 | update_screen(0); |
| 3418 | #ifdef FEAT_GUI |
| 3419 | if (gui.in_use) |
| 3420 | { |
| 3421 | /* Show the cursor after the match, not after the redrawn text. */ |
| 3422 | setcursor(); |
| 3423 | out_flush(); |
| 3424 | gui_update_cursor(FALSE, FALSE); |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3425 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3426 | #endif |
| 3427 | compl_restarting = TRUE; |
| 3428 | if (ins_complete(Ctrl_N) == FAIL) |
| 3429 | compl_cont_status = 0; |
| 3430 | compl_restarting = FALSE; |
| 3431 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3432 | |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3433 | compl_enter_selects = !compl_used_match; |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3434 | |
| 3435 | /* Show the popup menu with a different set of matches. */ |
| 3436 | ins_compl_show_pum(); |
Bram Moolenaar | 7073cc8 | 2006-08-29 16:33:06 +0000 | [diff] [blame] | 3437 | |
| 3438 | /* Don't let Enter select the original text when there is no popup menu. */ |
| 3439 | if (compl_match_array == NULL) |
| 3440 | compl_enter_selects = FALSE; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3441 | } |
| 3442 | |
| 3443 | /* |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3444 | * Return the length of the completion, from the completion start column to |
| 3445 | * the cursor column. Making sure it never goes below zero. |
| 3446 | */ |
| 3447 | static int |
| 3448 | ins_compl_len() |
| 3449 | { |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3450 | int off = (int)curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3451 | |
| 3452 | if (off < 0) |
| 3453 | return 0; |
| 3454 | return off; |
| 3455 | } |
| 3456 | |
| 3457 | /* |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3458 | * Append one character to the match leader. May reduce the number of |
| 3459 | * matches. |
| 3460 | */ |
| 3461 | static void |
| 3462 | ins_compl_addleader(c) |
| 3463 | int c; |
| 3464 | { |
| 3465 | #ifdef FEAT_MBYTE |
| 3466 | int cc; |
| 3467 | |
| 3468 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 3469 | { |
| 3470 | char_u buf[MB_MAXBYTES + 1]; |
| 3471 | |
| 3472 | (*mb_char2bytes)(c, buf); |
| 3473 | buf[cc] = NUL; |
| 3474 | ins_char_bytes(buf, cc); |
| 3475 | } |
| 3476 | else |
| 3477 | #endif |
| 3478 | ins_char(c); |
| 3479 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3480 | /* If we didn't complete finding matches we must search again. */ |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3481 | if (ins_compl_need_restart()) |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3482 | ins_compl_restart(); |
| 3483 | |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3484 | /* When 'always' is set, don't reset compl_leader. While completing, |
| 3485 | * cursor don't point original position, changing compl_leader would |
| 3486 | * break redo. */ |
| 3487 | if (!compl_opt_refresh_always) |
| 3488 | { |
| 3489 | vim_free(compl_leader); |
| 3490 | compl_leader = vim_strnsave(ml_get_curline() + compl_col, |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3491 | (int)(curwin->w_cursor.col - compl_col)); |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 3492 | if (compl_leader != NULL) |
| 3493 | ins_compl_new_leader(); |
| 3494 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | /* |
| 3498 | * Setup for finding completions again without leaving CTRL-X mode. Used when |
| 3499 | * BS or a key was typed while still searching for matches. |
| 3500 | */ |
| 3501 | static void |
| 3502 | ins_compl_restart() |
| 3503 | { |
| 3504 | ins_compl_free(); |
| 3505 | compl_started = FALSE; |
| 3506 | compl_matches = 0; |
| 3507 | compl_cont_status = 0; |
| 3508 | compl_cont_mode = 0; |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | /* |
| 3512 | * Set the first match, the original text. |
| 3513 | */ |
| 3514 | static void |
| 3515 | ins_compl_set_original_text(str) |
| 3516 | char_u *str; |
| 3517 | { |
| 3518 | char_u *p; |
| 3519 | |
| 3520 | /* Replace the original text entry. */ |
| 3521 | if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */ |
| 3522 | { |
| 3523 | p = vim_strsave(str); |
| 3524 | if (p != NULL) |
| 3525 | { |
| 3526 | vim_free(compl_first_match->cp_str); |
| 3527 | compl_first_match->cp_str = p; |
| 3528 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | /* |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3533 | * Append one character to the match leader. May reduce the number of |
| 3534 | * matches. |
| 3535 | */ |
| 3536 | static void |
| 3537 | ins_compl_addfrommatch() |
| 3538 | { |
| 3539 | char_u *p; |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 3540 | int len = (int)curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3541 | int c; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3542 | compl_T *cp; |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3543 | |
| 3544 | p = compl_shown_match->cp_str; |
Bram Moolenaar | faa959a | 2006-02-20 21:37:40 +0000 | [diff] [blame] | 3545 | if ((int)STRLEN(p) <= len) /* the match is too short */ |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3546 | { |
| 3547 | /* When still at the original match use the first entry that matches |
| 3548 | * the leader. */ |
| 3549 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 3550 | { |
| 3551 | p = NULL; |
| 3552 | for (cp = compl_shown_match->cp_next; cp != NULL |
| 3553 | && cp != compl_first_match; cp = cp->cp_next) |
| 3554 | { |
Bram Moolenaar | 132283f | 2006-10-03 13:22:23 +0000 | [diff] [blame] | 3555 | if (compl_leader == NULL |
| 3556 | || ins_compl_equal(cp, compl_leader, |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 3557 | (int)STRLEN(compl_leader))) |
| 3558 | { |
| 3559 | p = cp->cp_str; |
| 3560 | break; |
| 3561 | } |
| 3562 | } |
| 3563 | if (p == NULL || (int)STRLEN(p) <= len) |
| 3564 | return; |
| 3565 | } |
| 3566 | else |
| 3567 | return; |
| 3568 | } |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3569 | p += len; |
Bram Moolenaar | e659c95 | 2011-05-19 17:25:41 +0200 | [diff] [blame] | 3570 | c = PTR2CHAR(p); |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3571 | ins_compl_addleader(c); |
| 3572 | } |
| 3573 | |
| 3574 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3575 | * Prepare for Insert mode completion, or stop it. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 3576 | * Called just after typing a character in Insert mode. |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3577 | * Returns TRUE when the character is not to be inserted; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3578 | */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3579 | static int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3580 | ins_compl_prep(c) |
| 3581 | int c; |
| 3582 | { |
| 3583 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3584 | int want_cindent; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3585 | int retval = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | |
| 3587 | /* Forget any previous 'special' messages if this is actually |
| 3588 | * a ^X mode key - bar ^R, in which case we wait to see what it gives us. |
| 3589 | */ |
| 3590 | if (c != Ctrl_R && vim_is_ctrl_x_key(c)) |
| 3591 | edit_submode_extra = NULL; |
| 3592 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 3593 | /* Ignore end of Select mode mapping and mouse scroll buttons. */ |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 3594 | if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP |
| 3595 | || c == K_MOUSELEFT || c == K_MOUSERIGHT) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3596 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3597 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 3598 | /* Set "compl_get_longest" when finding the first matches. */ |
| 3599 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET |
| 3600 | || (ctrl_x_mode == 0 && !compl_started)) |
| 3601 | { |
| 3602 | compl_get_longest = (vim_strchr(p_cot, 'l') != NULL); |
| 3603 | compl_used_match = TRUE; |
| 3604 | } |
| 3605 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3606 | if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET) |
| 3607 | { |
| 3608 | /* |
| 3609 | * We have just typed CTRL-X and aren't quite sure which CTRL-X mode |
| 3610 | * it will be yet. Now we decide. |
| 3611 | */ |
| 3612 | switch (c) |
| 3613 | { |
| 3614 | case Ctrl_E: |
| 3615 | case Ctrl_Y: |
| 3616 | ctrl_x_mode = CTRL_X_SCROLL; |
| 3617 | if (!(State & REPLACE_FLAG)) |
| 3618 | edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)"); |
| 3619 | else |
| 3620 | edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)"); |
| 3621 | edit_submode_pre = NULL; |
| 3622 | showmode(); |
| 3623 | break; |
| 3624 | case Ctrl_L: |
| 3625 | ctrl_x_mode = CTRL_X_WHOLE_LINE; |
| 3626 | break; |
| 3627 | case Ctrl_F: |
| 3628 | ctrl_x_mode = CTRL_X_FILES; |
| 3629 | break; |
| 3630 | case Ctrl_K: |
| 3631 | ctrl_x_mode = CTRL_X_DICTIONARY; |
| 3632 | break; |
| 3633 | case Ctrl_R: |
| 3634 | /* Simply allow ^R to happen without affecting ^X mode */ |
| 3635 | break; |
| 3636 | case Ctrl_T: |
| 3637 | ctrl_x_mode = CTRL_X_THESAURUS; |
| 3638 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3639 | #ifdef FEAT_COMPL_FUNC |
| 3640 | case Ctrl_U: |
| 3641 | ctrl_x_mode = CTRL_X_FUNCTION; |
| 3642 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3643 | case Ctrl_O: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3644 | ctrl_x_mode = CTRL_X_OMNI; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3645 | break; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3646 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3647 | case 's': |
| 3648 | case Ctrl_S: |
| 3649 | ctrl_x_mode = CTRL_X_SPELL; |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 3650 | #ifdef FEAT_SPELL |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3651 | ++emsg_off; /* Avoid getting the E756 error twice. */ |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3652 | spell_back_to_badword(); |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 3653 | --emsg_off; |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 3654 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 3655 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3656 | case Ctrl_RSB: |
| 3657 | ctrl_x_mode = CTRL_X_TAGS; |
| 3658 | break; |
| 3659 | #ifdef FEAT_FIND_ID |
| 3660 | case Ctrl_I: |
| 3661 | case K_S_TAB: |
| 3662 | ctrl_x_mode = CTRL_X_PATH_PATTERNS; |
| 3663 | break; |
| 3664 | case Ctrl_D: |
| 3665 | ctrl_x_mode = CTRL_X_PATH_DEFINES; |
| 3666 | break; |
| 3667 | #endif |
| 3668 | case Ctrl_V: |
| 3669 | case Ctrl_Q: |
| 3670 | ctrl_x_mode = CTRL_X_CMDLINE; |
| 3671 | break; |
| 3672 | case Ctrl_P: |
| 3673 | case Ctrl_N: |
| 3674 | /* ^X^P means LOCAL expansion if nothing interrupted (eg we |
| 3675 | * just started ^X mode, or there were enough ^X's to cancel |
| 3676 | * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below) |
| 3677 | * do normal expansion when interrupting a different mode (say |
| 3678 | * ^X^F^X^P or ^P^X^X^P, see below) |
| 3679 | * nothing changes if interrupting mode 0, (eg, the flag |
| 3680 | * doesn't change when going to ADDING mode -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3681 | if (!(compl_cont_status & CONT_INTRPT)) |
| 3682 | compl_cont_status |= CONT_LOCAL; |
| 3683 | else if (compl_cont_mode != 0) |
| 3684 | compl_cont_status &= ~CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3685 | /* FALLTHROUGH */ |
| 3686 | default: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3687 | /* If we have typed at least 2 ^X's... for modes != 0, we set |
| 3688 | * compl_cont_status = 0 (eg, as if we had just started ^X |
| 3689 | * mode). |
| 3690 | * For mode 0, we set "compl_cont_mode" to an impossible |
| 3691 | * value, in both cases ^X^X can be used to restart the same |
| 3692 | * mode (avoiding ADDING mode). |
| 3693 | * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start |
| 3694 | * 'complete' and local ^P expansions respectively. |
| 3695 | * In mode 0 an extra ^X is needed since ^X^P goes to ADDING |
| 3696 | * mode -- Acevedo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | if (c == Ctrl_X) |
| 3698 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3699 | if (compl_cont_mode != 0) |
| 3700 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3701 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3702 | compl_cont_mode = CTRL_X_NOT_DEFINED_YET; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3703 | } |
| 3704 | ctrl_x_mode = 0; |
| 3705 | edit_submode = NULL; |
| 3706 | showmode(); |
| 3707 | break; |
| 3708 | } |
| 3709 | } |
| 3710 | else if (ctrl_x_mode != 0) |
| 3711 | { |
| 3712 | /* We're already in CTRL-X mode, do we stay in it? */ |
| 3713 | if (!vim_is_ctrl_x_key(c)) |
| 3714 | { |
| 3715 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 3716 | ctrl_x_mode = 0; |
| 3717 | else |
| 3718 | ctrl_x_mode = CTRL_X_FINISHED; |
| 3719 | edit_submode = NULL; |
| 3720 | } |
| 3721 | showmode(); |
| 3722 | } |
| 3723 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3724 | if (compl_started || ctrl_x_mode == CTRL_X_FINISHED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | { |
| 3726 | /* Show error message from attempted keyword completion (probably |
| 3727 | * 'Pattern not found') until another key is hit, then go back to |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3728 | * showing what mode we are in. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3729 | showmode(); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 3730 | if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R |
| 3731 | && !ins_compl_pum_key(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3732 | || ctrl_x_mode == CTRL_X_FINISHED) |
| 3733 | { |
| 3734 | /* Get here when we have finished typing a sequence of ^N and |
| 3735 | * ^P or other completion characters in CTRL-X mode. Free up |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3736 | * memory that was used, and make sure we can redo the insert. */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3737 | if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3738 | { |
| 3739 | /* |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3740 | * If any of the original typed text has been changed, eg when |
| 3741 | * ignorecase is set, we must add back-spaces to the redo |
| 3742 | * buffer. We add as few as necessary to delete just the part |
| 3743 | * of the original text that has changed. |
| 3744 | * When using the longest match, edited the match or used |
| 3745 | * CTRL-E then don't use the current match. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3746 | */ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3747 | if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E) |
| 3748 | ptr = compl_curr_match->cp_str; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3749 | else |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 3750 | ptr = NULL; |
| 3751 | ins_compl_fixRedoBufForLeader(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | #ifdef FEAT_CINDENT |
| 3755 | want_cindent = (can_cindent && cindent_on()); |
| 3756 | #endif |
| 3757 | /* |
| 3758 | * When completing whole lines: fix indent for 'cindent'. |
| 3759 | * Otherwise, break line if it's too long. |
| 3760 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3761 | if (compl_cont_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3762 | { |
| 3763 | #ifdef FEAT_CINDENT |
| 3764 | /* re-indent the current line */ |
| 3765 | if (want_cindent) |
| 3766 | { |
| 3767 | do_c_expr_indent(); |
| 3768 | want_cindent = FALSE; /* don't do it again */ |
| 3769 | } |
| 3770 | #endif |
| 3771 | } |
| 3772 | else |
| 3773 | { |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3774 | int prev_col = curwin->w_cursor.col; |
| 3775 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3776 | /* put the cursor on the last char, for 'tw' formatting */ |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3777 | if (prev_col > 0) |
| 3778 | dec_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3779 | if (stop_arrow() == OK) |
| 3780 | insertchar(NUL, 0, -1); |
Bram Moolenaar | 09a16b5 | 2007-02-20 02:31:20 +0000 | [diff] [blame] | 3781 | if (prev_col > 0 |
| 3782 | && ml_get_curline()[curwin->w_cursor.col] != NUL) |
| 3783 | inc_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3786 | /* If the popup menu is displayed pressing CTRL-Y means accepting |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3787 | * the selection without inserting anything. When |
| 3788 | * compl_enter_selects is set the Enter key does the same. */ |
| 3789 | if ((c == Ctrl_Y || (compl_enter_selects |
| 3790 | && (c == CAR || c == K_KENTER || c == NL))) |
| 3791 | && pum_visible()) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3792 | retval = TRUE; |
| 3793 | |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3794 | /* CTRL-E means completion is Ended, go back to the typed text. */ |
| 3795 | if (c == Ctrl_E) |
| 3796 | { |
| 3797 | ins_compl_delete(); |
| 3798 | if (compl_leader != NULL) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3799 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3800 | else if (compl_first_match != NULL) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 3801 | ins_bytes(compl_orig_text + ins_compl_len()); |
Bram Moolenaar | d2cec5b | 2006-03-28 21:08:56 +0000 | [diff] [blame] | 3802 | retval = TRUE; |
| 3803 | } |
| 3804 | |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 3805 | auto_format(FALSE, TRUE); |
| 3806 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3807 | ins_compl_free(); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3808 | compl_started = FALSE; |
| 3809 | compl_matches = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3810 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 3811 | ctrl_x_mode = 0; |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 3812 | compl_enter_selects = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | if (edit_submode != NULL) |
| 3814 | { |
| 3815 | edit_submode = NULL; |
| 3816 | showmode(); |
| 3817 | } |
| 3818 | |
| 3819 | #ifdef FEAT_CINDENT |
| 3820 | /* |
| 3821 | * Indent now if a key was typed that is in 'cinkeys'. |
| 3822 | */ |
| 3823 | if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0))) |
| 3824 | do_c_expr_indent(); |
| 3825 | #endif |
| 3826 | } |
| 3827 | } |
| 3828 | |
| 3829 | /* reset continue_* if we left expansion-mode, if we stay they'll be |
| 3830 | * (re)set properly in ins_complete() */ |
| 3831 | if (!vim_is_ctrl_x_key(c)) |
| 3832 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 3833 | compl_cont_status = 0; |
| 3834 | compl_cont_mode = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3836 | |
| 3837 | return retval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | /* |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 3841 | * Fix the redo buffer for the completion leader replacing some of the typed |
| 3842 | * text. This inserts backspaces and appends the changed text. |
| 3843 | * "ptr" is the known leader text or NUL. |
| 3844 | */ |
| 3845 | static void |
| 3846 | ins_compl_fixRedoBufForLeader(ptr_arg) |
| 3847 | char_u *ptr_arg; |
| 3848 | { |
| 3849 | int len; |
| 3850 | char_u *p; |
| 3851 | char_u *ptr = ptr_arg; |
| 3852 | |
| 3853 | if (ptr == NULL) |
| 3854 | { |
| 3855 | if (compl_leader != NULL) |
| 3856 | ptr = compl_leader; |
| 3857 | else |
| 3858 | return; /* nothing to do */ |
| 3859 | } |
| 3860 | if (compl_orig_text != NULL) |
| 3861 | { |
| 3862 | p = compl_orig_text; |
| 3863 | for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len) |
| 3864 | ; |
| 3865 | #ifdef FEAT_MBYTE |
| 3866 | if (len > 0) |
| 3867 | len -= (*mb_head_off)(p, p + len); |
| 3868 | #endif |
| 3869 | for (p += len; *p != NUL; mb_ptr_adv(p)) |
| 3870 | AppendCharToRedobuff(K_BS); |
| 3871 | } |
| 3872 | else |
| 3873 | len = 0; |
| 3874 | if (ptr != NULL) |
| 3875 | AppendToRedobuffLit(ptr + len, -1); |
| 3876 | } |
| 3877 | |
| 3878 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3879 | * Loops through the list of windows, loaded-buffers or non-loaded-buffers |
| 3880 | * (depending on flag) starting from buf and looking for a non-scanned |
| 3881 | * buffer (other than curbuf). curbuf is special, if it is called with |
| 3882 | * buf=curbuf then it has to be the first call for a given flag/expansion. |
| 3883 | * |
| 3884 | * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo |
| 3885 | */ |
| 3886 | static buf_T * |
| 3887 | ins_compl_next_buf(buf, flag) |
| 3888 | buf_T *buf; |
| 3889 | int flag; |
| 3890 | { |
| 3891 | #ifdef FEAT_WINDOWS |
| 3892 | static win_T *wp; |
| 3893 | #endif |
| 3894 | |
| 3895 | if (flag == 'w') /* just windows */ |
| 3896 | { |
| 3897 | #ifdef FEAT_WINDOWS |
| 3898 | if (buf == curbuf) /* first call for this flag/expansion */ |
| 3899 | wp = curwin; |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3900 | while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3901 | && wp->w_buffer->b_scanned) |
| 3902 | ; |
| 3903 | buf = wp->w_buffer; |
| 3904 | #else |
| 3905 | buf = curbuf; |
| 3906 | #endif |
| 3907 | } |
| 3908 | else |
| 3909 | /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U' |
| 3910 | * (unlisted buffers) |
| 3911 | * When completing whole lines skip unloaded buffers. */ |
Bram Moolenaar | 1f8a5f0 | 2005-07-01 22:41:52 +0000 | [diff] [blame] | 3912 | while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3913 | && ((flag == 'U' |
| 3914 | ? buf->b_p_bl |
| 3915 | : (!buf->b_p_bl |
| 3916 | || (buf->b_ml.ml_mfp == NULL) != (flag == 'u'))) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 3917 | || buf->b_scanned)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3918 | ; |
| 3919 | return buf; |
| 3920 | } |
| 3921 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3922 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3923 | static void expand_by_function __ARGS((int type, char_u *base)); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3924 | |
| 3925 | /* |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3926 | * Execute user defined complete function 'completefunc' or 'omnifunc', and |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3927 | * get matches in "matches". |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3928 | */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3929 | static void |
| 3930 | expand_by_function(type, base) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 3931 | int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */ |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3932 | char_u *base; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3933 | { |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3934 | list_T *matchlist = NULL; |
| 3935 | dict_T *matchdict = NULL; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3936 | char_u *args[2]; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3937 | char_u *funcname; |
| 3938 | pos_T pos; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3939 | win_T *curwin_save; |
| 3940 | buf_T *curbuf_save; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3941 | typval_T rettv; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3942 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3943 | funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 3944 | if (*funcname == NUL) |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 3945 | return; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 3946 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3947 | /* Call 'completefunc' to obtain the list of matches. */ |
| 3948 | args[0] = (char_u *)"0"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3949 | args[1] = base; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3950 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3951 | pos = curwin->w_cursor; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3952 | curwin_save = curwin; |
| 3953 | curbuf_save = curbuf; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3954 | |
| 3955 | /* Call a function, which returns a list or dict. */ |
| 3956 | if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK) |
| 3957 | { |
| 3958 | switch (rettv.v_type) |
| 3959 | { |
| 3960 | case VAR_LIST: |
| 3961 | matchlist = rettv.vval.v_list; |
| 3962 | break; |
| 3963 | case VAR_DICT: |
| 3964 | matchdict = rettv.vval.v_dict; |
| 3965 | break; |
| 3966 | default: |
| 3967 | /* TODO: Give error message? */ |
| 3968 | clear_tv(&rettv); |
| 3969 | break; |
| 3970 | } |
| 3971 | } |
| 3972 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3973 | if (curwin_save != curwin || curbuf_save != curbuf) |
| 3974 | { |
| 3975 | EMSG(_(e_complwin)); |
| 3976 | goto theend; |
| 3977 | } |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 3978 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3979 | check_cursor(); |
| 3980 | if (!equalpos(curwin->w_cursor, pos)) |
| 3981 | { |
| 3982 | EMSG(_(e_compldel)); |
| 3983 | goto theend; |
| 3984 | } |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3985 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3986 | if (matchlist != NULL) |
| 3987 | ins_compl_add_list(matchlist); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3988 | else if (matchdict != NULL) |
| 3989 | ins_compl_add_dict(matchdict); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 3990 | |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3991 | theend: |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 3992 | if (matchdict != NULL) |
| 3993 | dict_unref(matchdict); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 3994 | if (matchlist != NULL) |
| 3995 | list_unref(matchlist); |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 3996 | } |
| 3997 | #endif /* FEAT_COMPL_FUNC */ |
| 3998 | |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 3999 | #if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4000 | /* |
| 4001 | * Add completions from a list. |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4002 | */ |
| 4003 | static void |
| 4004 | ins_compl_add_list(list) |
| 4005 | list_T *list; |
| 4006 | { |
| 4007 | listitem_T *li; |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4008 | int dir = compl_direction; |
| 4009 | |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4010 | /* Go through the List with matches and add each of them. */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4011 | for (li = list->lv_first; li != NULL; li = li->li_next) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4012 | { |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4013 | if (ins_compl_add_tv(&li->li_tv, dir) == OK) |
| 4014 | /* if dir was BACKWARD then honor it just once */ |
| 4015 | dir = FORWARD; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 4016 | else if (did_emsg) |
| 4017 | break; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4018 | } |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4019 | } |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4020 | |
| 4021 | /* |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4022 | * Add completions from a dict. |
| 4023 | */ |
| 4024 | static void |
| 4025 | ins_compl_add_dict(dict) |
| 4026 | dict_T *dict; |
| 4027 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4028 | dictitem_T *di_refresh; |
| 4029 | dictitem_T *di_words; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4030 | |
| 4031 | /* Check for optional "refresh" item. */ |
| 4032 | compl_opt_refresh_always = FALSE; |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4033 | di_refresh = dict_find(dict, (char_u *)"refresh", 7); |
| 4034 | if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING) |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4035 | { |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4036 | char_u *v = di_refresh->di_tv.vval.v_string; |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4037 | |
| 4038 | if (v != NULL && STRCMP(v, (char_u *)"always") == 0) |
| 4039 | compl_opt_refresh_always = TRUE; |
| 4040 | } |
| 4041 | |
| 4042 | /* Add completions from a "words" list. */ |
Bram Moolenaar | 70b2a56 | 2012-01-10 22:26:17 +0100 | [diff] [blame] | 4043 | di_words = dict_find(dict, (char_u *)"words", 5); |
| 4044 | if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST) |
| 4045 | ins_compl_add_list(di_words->di_tv.vval.v_list); |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | /* |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4049 | * Add a match to the list of matches from a typeval_T. |
| 4050 | * If the given string is already in the list of completions, then return |
| 4051 | * NOTDONE, otherwise add it to the list and return OK. If there is an error, |
| 4052 | * maybe because alloc() returns NULL, then FAIL is returned. |
| 4053 | */ |
| 4054 | int |
| 4055 | ins_compl_add_tv(tv, dir) |
| 4056 | typval_T *tv; |
| 4057 | int dir; |
| 4058 | { |
| 4059 | char_u *word; |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 4060 | int icase = FALSE; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4061 | int adup = FALSE; |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4062 | int aempty = FALSE; |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4063 | char_u *(cptext[CPT_COUNT]); |
| 4064 | |
| 4065 | if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL) |
| 4066 | { |
| 4067 | word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE); |
| 4068 | cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict, |
| 4069 | (char_u *)"abbr", FALSE); |
| 4070 | cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict, |
| 4071 | (char_u *)"menu", FALSE); |
| 4072 | cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict, |
| 4073 | (char_u *)"kind", FALSE); |
| 4074 | cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict, |
| 4075 | (char_u *)"info", FALSE); |
| 4076 | if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL) |
| 4077 | icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase"); |
Bram Moolenaar | 4a85b41 | 2006-04-23 22:40:29 +0000 | [diff] [blame] | 4078 | 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] | 4079 | adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup"); |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4080 | if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL) |
| 4081 | aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty"); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4082 | } |
| 4083 | else |
| 4084 | { |
| 4085 | word = get_tv_string_chk(tv); |
| 4086 | vim_memset(cptext, 0, sizeof(cptext)); |
| 4087 | } |
Bram Moolenaar | 2a8caa4 | 2010-11-10 17:11:33 +0100 | [diff] [blame] | 4088 | if (word == NULL || (!aempty && *word == NUL)) |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4089 | return FAIL; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4090 | return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup); |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 4091 | } |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4092 | #endif |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4093 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4094 | /* |
| 4095 | * Get the next expansion(s), using "compl_pattern". |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4096 | * The search starts at position "ini" in curbuf and in the direction |
| 4097 | * compl_direction. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4098 | * When "compl_started" is FALSE start at that position, otherwise continue |
| 4099 | * where we stopped searching before. |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4100 | * This may return before finding all the matches. |
| 4101 | * Return the total number of matches or -1 if still unknown -- Acevedo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4102 | */ |
| 4103 | static int |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4104 | ins_compl_get_exp(ini) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4105 | pos_T *ini; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4106 | { |
| 4107 | static pos_T first_match_pos; |
| 4108 | static pos_T last_match_pos; |
| 4109 | static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4110 | static int found_all = FALSE; /* Found all matches of a |
| 4111 | certain type. */ |
| 4112 | static buf_T *ins_buf = NULL; /* buffer being scanned */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4113 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4114 | pos_T *pos; |
| 4115 | char_u **matches; |
| 4116 | int save_p_scs; |
| 4117 | int save_p_ws; |
| 4118 | int save_p_ic; |
| 4119 | int i; |
| 4120 | int num_matches; |
| 4121 | int len; |
| 4122 | int found_new_match; |
| 4123 | int type = ctrl_x_mode; |
| 4124 | char_u *ptr; |
| 4125 | char_u *dict = NULL; |
| 4126 | int dict_f = 0; |
| 4127 | compl_T *old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4128 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4129 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4130 | { |
| 4131 | for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next) |
| 4132 | ins_buf->b_scanned = 0; |
| 4133 | found_all = FALSE; |
| 4134 | ins_buf = curbuf; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4135 | e_cpt = (compl_cont_status & CONT_LOCAL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4136 | ? (char_u *)"." : curbuf->b_p_cpt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4137 | last_match_pos = first_match_pos = *ini; |
| 4138 | } |
| 4139 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4140 | old_match = compl_curr_match; /* remember the last current match */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4141 | pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4142 | /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */ |
| 4143 | for (;;) |
| 4144 | { |
| 4145 | found_new_match = FAIL; |
| 4146 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4147 | /* 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] | 4148 | * or if found_all says this entry is done. For ^X^L only use the |
| 4149 | * entries from 'complete' that look in loaded buffers. */ |
| 4150 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4151 | && (!compl_started || found_all)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4152 | { |
| 4153 | found_all = FALSE; |
| 4154 | while (*e_cpt == ',' || *e_cpt == ' ') |
| 4155 | e_cpt++; |
| 4156 | if (*e_cpt == '.' && !curbuf->b_scanned) |
| 4157 | { |
| 4158 | ins_buf = curbuf; |
| 4159 | first_match_pos = *ini; |
| 4160 | /* So that ^N can match word immediately after cursor */ |
| 4161 | if (ctrl_x_mode == 0) |
| 4162 | dec(&first_match_pos); |
| 4163 | last_match_pos = first_match_pos; |
| 4164 | type = 0; |
| 4165 | } |
| 4166 | else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL |
| 4167 | && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf) |
| 4168 | { |
| 4169 | /* Scan a buffer, but not the current one. */ |
| 4170 | if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */ |
| 4171 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4172 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4173 | first_match_pos.col = last_match_pos.col = 0; |
| 4174 | first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1; |
| 4175 | last_match_pos.lnum = 0; |
| 4176 | type = 0; |
| 4177 | } |
| 4178 | else /* unloaded buffer, scan like dictionary */ |
| 4179 | { |
| 4180 | found_all = TRUE; |
| 4181 | if (ins_buf->b_fname == NULL) |
| 4182 | continue; |
| 4183 | type = CTRL_X_DICTIONARY; |
| 4184 | dict = ins_buf->b_fname; |
| 4185 | dict_f = DICT_EXACT; |
| 4186 | } |
Bram Moolenaar | 555b280 | 2005-05-19 21:08:39 +0000 | [diff] [blame] | 4187 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4188 | ins_buf->b_fname == NULL |
| 4189 | ? buf_spname(ins_buf) |
| 4190 | : ins_buf->b_sfname == NULL |
| 4191 | ? (char *)ins_buf->b_fname |
| 4192 | : (char *)ins_buf->b_sfname); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 4193 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4194 | } |
| 4195 | else if (*e_cpt == NUL) |
| 4196 | break; |
| 4197 | else |
| 4198 | { |
| 4199 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4200 | type = -1; |
| 4201 | else if (*e_cpt == 'k' || *e_cpt == 's') |
| 4202 | { |
| 4203 | if (*e_cpt == 'k') |
| 4204 | type = CTRL_X_DICTIONARY; |
| 4205 | else |
| 4206 | type = CTRL_X_THESAURUS; |
| 4207 | if (*++e_cpt != ',' && *e_cpt != NUL) |
| 4208 | { |
| 4209 | dict = e_cpt; |
| 4210 | dict_f = DICT_FIRST; |
| 4211 | } |
| 4212 | } |
| 4213 | #ifdef FEAT_FIND_ID |
| 4214 | else if (*e_cpt == 'i') |
| 4215 | type = CTRL_X_PATH_PATTERNS; |
| 4216 | else if (*e_cpt == 'd') |
| 4217 | type = CTRL_X_PATH_DEFINES; |
| 4218 | #endif |
| 4219 | else if (*e_cpt == ']' || *e_cpt == 't') |
| 4220 | { |
| 4221 | type = CTRL_X_TAGS; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4222 | vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags.")); |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 4223 | (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4224 | } |
| 4225 | else |
| 4226 | type = -1; |
| 4227 | |
| 4228 | /* in any case e_cpt is advanced to the next entry */ |
| 4229 | (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ","); |
| 4230 | |
| 4231 | found_all = TRUE; |
| 4232 | if (type == -1) |
| 4233 | continue; |
| 4234 | } |
| 4235 | } |
| 4236 | |
| 4237 | switch (type) |
| 4238 | { |
| 4239 | case -1: |
| 4240 | break; |
| 4241 | #ifdef FEAT_FIND_ID |
| 4242 | case CTRL_X_PATH_PATTERNS: |
| 4243 | case CTRL_X_PATH_DEFINES: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4244 | find_pattern_in_path(compl_pattern, compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4245 | (int)STRLEN(compl_pattern), FALSE, FALSE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4246 | (type == CTRL_X_PATH_DEFINES |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4247 | && !(compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4248 | ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND, |
| 4249 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 4250 | break; |
| 4251 | #endif |
| 4252 | |
| 4253 | case CTRL_X_DICTIONARY: |
| 4254 | case CTRL_X_THESAURUS: |
| 4255 | ins_compl_dictionaries( |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 4256 | dict != NULL ? dict |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | : (type == CTRL_X_THESAURUS |
| 4258 | ? (*curbuf->b_p_tsr == NUL |
| 4259 | ? p_tsr |
| 4260 | : curbuf->b_p_tsr) |
| 4261 | : (*curbuf->b_p_dict == NUL |
| 4262 | ? p_dict |
| 4263 | : curbuf->b_p_dict)), |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4264 | compl_pattern, |
Bram Moolenaar | 0b23879 | 2006-03-02 22:49:12 +0000 | [diff] [blame] | 4265 | dict != NULL ? dict_f |
| 4266 | : 0, type == CTRL_X_THESAURUS); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4267 | dict = NULL; |
| 4268 | break; |
| 4269 | |
| 4270 | case CTRL_X_TAGS: |
| 4271 | /* set p_ic according to p_ic, p_scs and pat for find_tags(). */ |
| 4272 | save_p_ic = p_ic; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4273 | p_ic = ignorecase(compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4274 | |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4275 | /* Find up to TAG_MANY matches. Avoids that an enormous number |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4276 | * of matches is found when compl_pattern is empty */ |
| 4277 | if (find_tags(compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4278 | TAG_REGEXP | TAG_NAMES | TAG_NOIC | |
| 4279 | TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0), |
| 4280 | TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0) |
| 4281 | { |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4282 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4283 | } |
| 4284 | p_ic = save_p_ic; |
| 4285 | break; |
| 4286 | |
| 4287 | case CTRL_X_FILES: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4288 | if (expand_wildcards(1, &compl_pattern, &num_matches, &matches, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4289 | EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK) |
| 4290 | { |
| 4291 | |
| 4292 | /* May change home directory back to "~". */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4293 | tilde_replace(compl_pattern, num_matches, matches); |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4294 | ins_compl_add_matches(num_matches, matches, |
| 4295 | #ifdef CASE_INSENSITIVE_FILENAME |
| 4296 | TRUE |
| 4297 | #else |
| 4298 | FALSE |
| 4299 | #endif |
| 4300 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | } |
| 4302 | break; |
| 4303 | |
| 4304 | case CTRL_X_CMDLINE: |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4305 | if (expand_cmdline(&compl_xp, compl_pattern, |
| 4306 | (int)STRLEN(compl_pattern), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4307 | &num_matches, &matches) == EXPAND_OK) |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4308 | ins_compl_add_matches(num_matches, matches, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | break; |
| 4310 | |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4311 | #ifdef FEAT_COMPL_FUNC |
| 4312 | case CTRL_X_FUNCTION: |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 4313 | case CTRL_X_OMNI: |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4314 | expand_by_function(type, compl_pattern); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 4315 | break; |
| 4316 | #endif |
| 4317 | |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4318 | case CTRL_X_SPELL: |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 4319 | #ifdef FEAT_SPELL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4320 | num_matches = expand_spelling(first_match_pos.lnum, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 4321 | compl_pattern, &matches); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4322 | if (num_matches > 0) |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4323 | ins_compl_add_matches(num_matches, matches, p_ic); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 4324 | #endif |
| 4325 | break; |
| 4326 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4327 | default: /* normal ^P/^N and ^X^L */ |
| 4328 | /* |
| 4329 | * If 'infercase' is set, don't use 'smartcase' here |
| 4330 | */ |
| 4331 | save_p_scs = p_scs; |
| 4332 | if (ins_buf->b_p_inf) |
| 4333 | p_scs = FALSE; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4335 | /* buffers other than curbuf are scanned from the beginning or the |
| 4336 | * end but never from the middle, thus setting nowrapscan in this |
| 4337 | * buffers is a good idea, on the other hand, we always set |
| 4338 | * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */ |
| 4339 | save_p_ws = p_ws; |
| 4340 | if (ins_buf != curbuf) |
| 4341 | p_ws = FALSE; |
| 4342 | else if (*e_cpt == '.') |
| 4343 | p_ws = TRUE; |
| 4344 | for (;;) |
| 4345 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4346 | int flags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4347 | |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 4348 | ++msg_silent; /* Don't want messages for wrapscan. */ |
| 4349 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4350 | /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that |
| 4351 | * has added a word that was at the beginning of the line */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4352 | if ( ctrl_x_mode == CTRL_X_WHOLE_LINE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4353 | || (compl_cont_status & CONT_SOL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4354 | found_new_match = search_for_exact_line(ins_buf, pos, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4355 | compl_direction, compl_pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4356 | else |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4357 | found_new_match = searchit(NULL, ins_buf, pos, |
| 4358 | compl_direction, |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4359 | compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG, |
Bram Moolenaar | 7692929 | 2008-01-06 19:07:36 +0000 | [diff] [blame] | 4360 | RE_LAST, (linenr_T)0, NULL); |
Bram Moolenaar | df40adf | 2006-10-14 12:32:39 +0000 | [diff] [blame] | 4361 | --msg_silent; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4362 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4363 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 4364 | /* set "compl_started" even on fail */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4365 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4366 | first_match_pos = *pos; |
| 4367 | last_match_pos = *pos; |
| 4368 | } |
| 4369 | else if (first_match_pos.lnum == last_match_pos.lnum |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4370 | && first_match_pos.col == last_match_pos.col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4371 | found_new_match = FAIL; |
| 4372 | if (found_new_match == FAIL) |
| 4373 | { |
| 4374 | if (ins_buf == curbuf) |
| 4375 | found_all = TRUE; |
| 4376 | break; |
| 4377 | } |
| 4378 | |
| 4379 | /* when ADDING, the text before the cursor matches, skip it */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4380 | if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4381 | && ini->lnum == pos->lnum |
| 4382 | && ini->col == pos->col) |
| 4383 | continue; |
| 4384 | ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col; |
| 4385 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4386 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4387 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4388 | { |
| 4389 | if (pos->lnum >= ins_buf->b_ml.ml_line_count) |
| 4390 | continue; |
| 4391 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 4392 | if (!p_paste) |
| 4393 | ptr = skipwhite(ptr); |
| 4394 | } |
| 4395 | len = (int)STRLEN(ptr); |
| 4396 | } |
| 4397 | else |
| 4398 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4399 | char_u *tmp_ptr = ptr; |
| 4400 | |
| 4401 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4402 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4403 | tmp_ptr += compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4404 | /* Skip if already inside a word. */ |
| 4405 | if (vim_iswordp(tmp_ptr)) |
| 4406 | continue; |
| 4407 | /* Find start of next word. */ |
| 4408 | tmp_ptr = find_word_start(tmp_ptr); |
| 4409 | } |
| 4410 | /* Find end of this word. */ |
| 4411 | tmp_ptr = find_word_end(tmp_ptr); |
| 4412 | len = (int)(tmp_ptr - ptr); |
| 4413 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4414 | if ((compl_cont_status & CONT_ADDING) |
| 4415 | && len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4416 | { |
| 4417 | if (pos->lnum < ins_buf->b_ml.ml_line_count) |
| 4418 | { |
| 4419 | /* Try next line, if any. the new word will be |
| 4420 | * "join" as if the normal command "J" was used. |
| 4421 | * IOSIZE is always greater than |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4422 | * compl_length, so the next STRNCPY always |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4423 | * works -- Acevedo */ |
| 4424 | STRNCPY(IObuff, ptr, len); |
| 4425 | ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE); |
| 4426 | tmp_ptr = ptr = skipwhite(ptr); |
| 4427 | /* Find start of next word. */ |
| 4428 | tmp_ptr = find_word_start(tmp_ptr); |
| 4429 | /* Find end of next word. */ |
| 4430 | tmp_ptr = find_word_end(tmp_ptr); |
| 4431 | if (tmp_ptr > ptr) |
| 4432 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4433 | if (*ptr != ')' && IObuff[len - 1] != TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4434 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4435 | if (IObuff[len - 1] != ' ') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4436 | IObuff[len++] = ' '; |
| 4437 | /* IObuf =~ "\k.* ", thus len >= 2 */ |
| 4438 | if (p_js |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4439 | && (IObuff[len - 2] == '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4440 | || (vim_strchr(p_cpo, CPO_JOINSP) |
| 4441 | == NULL |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4442 | && (IObuff[len - 2] == '?' |
| 4443 | || IObuff[len - 2] == '!')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4444 | IObuff[len++] = ' '; |
| 4445 | } |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 4446 | /* copy as much as possible of the new word */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4447 | if (tmp_ptr - ptr >= IOSIZE - len) |
| 4448 | tmp_ptr = ptr + IOSIZE - len - 1; |
| 4449 | STRNCPY(IObuff + len, ptr, tmp_ptr - ptr); |
| 4450 | len += (int)(tmp_ptr - ptr); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4451 | flags |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4452 | } |
| 4453 | IObuff[len] = NUL; |
| 4454 | ptr = IObuff; |
| 4455 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4456 | if (len == compl_length) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | continue; |
| 4458 | } |
| 4459 | } |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 4460 | if (ins_compl_add_infercase(ptr, len, p_ic, |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4461 | ins_buf == curbuf ? NULL : ins_buf->b_sfname, |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4462 | 0, flags) != NOTDONE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4463 | { |
| 4464 | found_new_match = OK; |
| 4465 | break; |
| 4466 | } |
| 4467 | } |
| 4468 | p_scs = save_p_scs; |
| 4469 | p_ws = save_p_ws; |
| 4470 | } |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4471 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4472 | /* check if compl_curr_match has changed, (e.g. other type of |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 4473 | * expansion added something) */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4474 | if (type != 0 && compl_curr_match != old_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4475 | found_new_match = OK; |
| 4476 | |
| 4477 | /* break the loop for specialized modes (use 'complete' just for the |
| 4478 | * generic ctrl_x_mode == 0) or when we've found a new match */ |
| 4479 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4480 | || found_new_match != FAIL) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4481 | { |
| 4482 | if (got_int) |
| 4483 | break; |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4484 | /* Fill the popup menu as soon as possible. */ |
Bram Moolenaar | 5948a57 | 2006-10-03 13:49:29 +0000 | [diff] [blame] | 4485 | if (type != -1) |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4486 | ins_compl_check_keys(0); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4487 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4488 | if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE) |
| 4489 | || compl_interrupted) |
| 4490 | break; |
| 4491 | compl_started = TRUE; |
| 4492 | } |
| 4493 | else |
| 4494 | { |
| 4495 | /* Mark a buffer scanned when it has been scanned completely */ |
| 4496 | if (type == 0 || type == CTRL_X_PATH_PATTERNS) |
| 4497 | ins_buf->b_scanned = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4498 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4499 | compl_started = FALSE; |
| 4500 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4501 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4502 | compl_started = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4503 | |
| 4504 | if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 4505 | && *e_cpt == NUL) /* Got to end of 'complete' */ |
| 4506 | found_new_match = FAIL; |
| 4507 | |
| 4508 | i = -1; /* total of matches, unknown */ |
| 4509 | if (found_new_match == FAIL |
| 4510 | || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)) |
| 4511 | i = ins_compl_make_cyclic(); |
| 4512 | |
| 4513 | /* If several matches were added (FORWARD) or the search failed and has |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4514 | * just been made cyclic then we have to move compl_curr_match to the next |
| 4515 | * or previous entry (if any) -- Acevedo */ |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 4516 | compl_curr_match = compl_direction == FORWARD ? old_match->cp_next |
| 4517 | : old_match->cp_prev; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4518 | if (compl_curr_match == NULL) |
| 4519 | compl_curr_match = old_match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4520 | return i; |
| 4521 | } |
| 4522 | |
| 4523 | /* Delete the old text being completed. */ |
| 4524 | static void |
| 4525 | ins_compl_delete() |
| 4526 | { |
| 4527 | int i; |
| 4528 | |
| 4529 | /* |
| 4530 | * In insert mode: Delete the typed part. |
| 4531 | * In replace mode: Put the old characters back, if any. |
| 4532 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4533 | i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4534 | backspace_until_column(i); |
| 4535 | changed_cline_bef_curs(); |
| 4536 | } |
| 4537 | |
| 4538 | /* Insert the new text being completed. */ |
| 4539 | static void |
| 4540 | ins_compl_insert() |
| 4541 | { |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 4542 | ins_bytes(compl_shown_match->cp_str + ins_compl_len()); |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 4543 | if (compl_shown_match->cp_flags & ORIGINAL_TEXT) |
| 4544 | compl_used_match = FALSE; |
| 4545 | else |
| 4546 | compl_used_match = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
| 4549 | /* |
| 4550 | * Fill in the next completion in the current direction. |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4551 | * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to |
| 4552 | * get more completions. If it is FALSE, then we just do nothing when there |
| 4553 | * are no more completions in a given direction. The latter case is used when |
| 4554 | * we are still in the middle of finding completions, to allow browsing |
| 4555 | * through the ones found so far. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4556 | * Return the total number of matches, or -1 if still unknown -- webb. |
| 4557 | * |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4558 | * compl_curr_match is currently being used by ins_compl_get_exp(), so we use |
| 4559 | * compl_shown_match here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4560 | * |
| 4561 | * Note that this function may be called recursively once only. First with |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4562 | * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn |
| 4563 | * calls this function with "allow_get_expansion" FALSE. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4564 | */ |
| 4565 | static int |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4566 | ins_compl_next(allow_get_expansion, count, insert_match) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4567 | int allow_get_expansion; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4568 | int count; /* repeat completion this many times; should |
| 4569 | be at least 1 */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4570 | int insert_match; /* Insert the newly selected match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4571 | { |
| 4572 | int num_matches = -1; |
| 4573 | int i; |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4574 | int todo = count; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4575 | compl_T *found_compl = NULL; |
| 4576 | int found_end = FALSE; |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4577 | int advance; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4578 | |
Bram Moolenaar | 6d6cec8 | 2012-01-20 14:32:27 +0100 | [diff] [blame] | 4579 | /* When user complete function return -1 for findstart which is next |
| 4580 | * time of 'always', compl_shown_match become NULL. */ |
| 4581 | if (compl_shown_match == NULL) |
| 4582 | return -1; |
| 4583 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4584 | if (compl_leader != NULL |
| 4585 | && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4586 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4587 | /* Set "compl_shown_match" to the actually shown match, it may differ |
| 4588 | * when "compl_leader" is used to omit some of the matches. */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4589 | while (!ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4590 | compl_leader, (int)STRLEN(compl_leader)) |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4591 | && compl_shown_match->cp_next != NULL |
| 4592 | && compl_shown_match->cp_next != compl_first_match) |
| 4593 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | 0440ca3 | 2006-05-13 13:24:33 +0000 | [diff] [blame] | 4594 | |
| 4595 | /* If we didn't find it searching forward, and compl_shows_dir is |
| 4596 | * backward, find the last match. */ |
| 4597 | if (compl_shows_dir == BACKWARD |
| 4598 | && !ins_compl_equal(compl_shown_match, |
| 4599 | compl_leader, (int)STRLEN(compl_leader)) |
| 4600 | && (compl_shown_match->cp_next == NULL |
| 4601 | || compl_shown_match->cp_next == compl_first_match)) |
| 4602 | { |
| 4603 | while (!ins_compl_equal(compl_shown_match, |
| 4604 | compl_leader, (int)STRLEN(compl_leader)) |
| 4605 | && compl_shown_match->cp_prev != NULL |
| 4606 | && compl_shown_match->cp_prev != compl_first_match) |
| 4607 | compl_shown_match = compl_shown_match->cp_prev; |
| 4608 | } |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
| 4611 | if (allow_get_expansion && insert_match |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4612 | && (!(compl_get_longest || compl_restarting) || compl_used_match)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4613 | /* Delete old text to be replaced */ |
| 4614 | ins_compl_delete(); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4615 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4616 | /* When finding the longest common text we stick at the original text, |
| 4617 | * don't let CTRL-N or CTRL-P move to the first match. */ |
| 4618 | advance = count != 1 || !allow_get_expansion || !compl_get_longest; |
| 4619 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4620 | /* When restarting the search don't insert the first match either. */ |
| 4621 | if (compl_restarting) |
| 4622 | { |
| 4623 | advance = FALSE; |
| 4624 | compl_restarting = FALSE; |
| 4625 | } |
| 4626 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4627 | /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap |
| 4628 | * around. */ |
| 4629 | while (--todo >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4630 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4631 | if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4632 | { |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4633 | compl_shown_match = compl_shown_match->cp_next; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4634 | found_end = (compl_first_match != NULL |
| 4635 | && (compl_shown_match->cp_next == compl_first_match |
| 4636 | || compl_shown_match == compl_first_match)); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4637 | } |
| 4638 | else if (compl_shows_dir == BACKWARD |
| 4639 | && compl_shown_match->cp_prev != NULL) |
| 4640 | { |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4641 | found_end = (compl_shown_match == compl_first_match); |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4642 | compl_shown_match = compl_shown_match->cp_prev; |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4643 | found_end |= (compl_shown_match == compl_first_match); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4644 | } |
| 4645 | else |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4646 | { |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4647 | if (!allow_get_expansion) |
| 4648 | { |
| 4649 | if (advance) |
| 4650 | { |
| 4651 | if (compl_shows_dir == BACKWARD) |
| 4652 | compl_pending -= todo + 1; |
| 4653 | else |
| 4654 | compl_pending += todo + 1; |
| 4655 | } |
| 4656 | return -1; |
| 4657 | } |
| 4658 | |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4659 | if (advance) |
| 4660 | { |
| 4661 | if (compl_shows_dir == BACKWARD) |
| 4662 | --compl_pending; |
| 4663 | else |
| 4664 | ++compl_pending; |
| 4665 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4666 | |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 4667 | /* Find matches. */ |
Bram Moolenaar | 8b6144b | 2006-02-08 09:20:24 +0000 | [diff] [blame] | 4668 | num_matches = ins_compl_get_exp(&compl_startpos); |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4669 | |
| 4670 | /* handle any pending completions */ |
| 4671 | while (compl_pending != 0 && compl_direction == compl_shows_dir |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4672 | && advance) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4673 | { |
| 4674 | if (compl_pending > 0 && compl_shown_match->cp_next != NULL) |
| 4675 | { |
| 4676 | compl_shown_match = compl_shown_match->cp_next; |
| 4677 | --compl_pending; |
| 4678 | } |
| 4679 | if (compl_pending < 0 && compl_shown_match->cp_prev != NULL) |
| 4680 | { |
| 4681 | compl_shown_match = compl_shown_match->cp_prev; |
| 4682 | ++compl_pending; |
| 4683 | } |
| 4684 | else |
| 4685 | break; |
| 4686 | } |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4687 | found_end = FALSE; |
| 4688 | } |
| 4689 | if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0 |
| 4690 | && compl_leader != NULL |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4691 | && !ins_compl_equal(compl_shown_match, |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 4692 | compl_leader, (int)STRLEN(compl_leader))) |
Bram Moolenaar | a655760 | 2006-02-04 22:43:20 +0000 | [diff] [blame] | 4693 | ++todo; |
| 4694 | else |
| 4695 | /* Remember a matching item. */ |
| 4696 | found_compl = compl_shown_match; |
| 4697 | |
| 4698 | /* Stop at the end of the list when we found a usable match. */ |
| 4699 | if (found_end) |
| 4700 | { |
| 4701 | if (found_compl != NULL) |
| 4702 | { |
| 4703 | compl_shown_match = found_compl; |
| 4704 | break; |
| 4705 | } |
| 4706 | todo = 1; /* use first usable match after wrapping around */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4707 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4710 | /* Insert the text of the new completion, or the compl_leader. */ |
| 4711 | if (insert_match) |
| 4712 | { |
| 4713 | if (!compl_get_longest || compl_used_match) |
| 4714 | ins_compl_insert(); |
| 4715 | else |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 4716 | ins_bytes(compl_leader + ins_compl_len()); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4717 | } |
| 4718 | else |
| 4719 | compl_used_match = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4720 | |
| 4721 | if (!allow_get_expansion) |
| 4722 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4723 | /* may undisplay the popup menu first */ |
| 4724 | ins_compl_upd_pum(); |
| 4725 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4726 | /* redraw to show the user what was inserted */ |
| 4727 | update_screen(0); |
| 4728 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4729 | /* display the updated popup menu */ |
| 4730 | ins_compl_show_pum(); |
Bram Moolenaar | 1471681 | 2006-05-04 21:54:08 +0000 | [diff] [blame] | 4731 | #ifdef FEAT_GUI |
| 4732 | if (gui.in_use) |
| 4733 | { |
| 4734 | /* Show the cursor after the match, not after the redrawn text. */ |
| 4735 | setcursor(); |
| 4736 | out_flush(); |
| 4737 | gui_update_cursor(FALSE, FALSE); |
| 4738 | } |
| 4739 | #endif |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 4740 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4741 | /* Delete old text to be replaced, since we're still searching and |
| 4742 | * don't want to match ourselves! */ |
| 4743 | ins_compl_delete(); |
| 4744 | } |
| 4745 | |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4746 | /* 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] | 4747 | * menu is visible. */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 4748 | compl_enter_selects = !insert_match && compl_match_array != NULL; |
| 4749 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | /* |
| 4751 | * Show the file name for the match (if any) |
| 4752 | * Truncate the file name to avoid a wait for return. |
| 4753 | */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4754 | if (compl_shown_match->cp_fname != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4755 | { |
| 4756 | STRCPY(IObuff, "match in file "); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4757 | i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4758 | if (i <= 0) |
| 4759 | i = 0; |
| 4760 | else |
| 4761 | STRCAT(IObuff, "<"); |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4762 | STRCAT(IObuff, compl_shown_match->cp_fname + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4763 | msg(IObuff); |
| 4764 | redraw_cmdline = FALSE; /* don't overwrite! */ |
| 4765 | } |
| 4766 | |
| 4767 | return num_matches; |
| 4768 | } |
| 4769 | |
| 4770 | /* |
| 4771 | * Call this while finding completions, to check whether the user has hit a key |
| 4772 | * that should change the currently displayed completion, or exit completion |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4773 | * 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] | 4774 | * possible. -- webb |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4775 | * "frequency" specifies out of how many calls we actually check. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | */ |
| 4777 | void |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4778 | ins_compl_check_keys(frequency) |
| 4779 | int frequency; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4780 | { |
| 4781 | static int count = 0; |
| 4782 | |
| 4783 | int c; |
| 4784 | |
| 4785 | /* Don't check when reading keys from a script. That would break the test |
| 4786 | * scripts */ |
| 4787 | if (using_script()) |
| 4788 | return; |
| 4789 | |
| 4790 | /* Only do this at regular intervals */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 4791 | if (++count < frequency) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4792 | return; |
| 4793 | count = 0; |
| 4794 | |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4795 | /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key() |
| 4796 | * can't do its work correctly. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4797 | c = vpeekc_any(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4798 | if (c != NUL) |
| 4799 | { |
| 4800 | if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R) |
| 4801 | { |
| 4802 | c = safe_vgetc(); /* Eat the character */ |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4803 | compl_shows_dir = ins_compl_key2dir(c); |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4804 | (void)ins_compl_next(FALSE, ins_compl_key2count(c), |
| 4805 | c != K_UP && c != K_DOWN); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4806 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4807 | else |
| 4808 | { |
| 4809 | /* 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] | 4810 | * back with vungetc() below. But skip K_IGNORE. */ |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4811 | c = safe_vgetc(); |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4812 | if (c != K_IGNORE) |
| 4813 | { |
| 4814 | /* Don't interrupt completion when the character wasn't typed, |
| 4815 | * e.g., when doing @q to replay keys. */ |
| 4816 | if (c != Ctrl_R && KeyTyped) |
| 4817 | compl_interrupted = TRUE; |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4818 | |
Bram Moolenaar | d4e20a7 | 2008-01-22 16:50:03 +0000 | [diff] [blame] | 4819 | vungetc(c); |
| 4820 | } |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4821 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4822 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4823 | if (compl_pending != 0 && !got_int) |
Bram Moolenaar | a260a97 | 2006-06-23 19:36:29 +0000 | [diff] [blame] | 4824 | { |
| 4825 | int todo = compl_pending > 0 ? compl_pending : -compl_pending; |
| 4826 | |
| 4827 | compl_pending = 0; |
| 4828 | (void)ins_compl_next(FALSE, todo, TRUE); |
| 4829 | } |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /* |
| 4833 | * Decide the direction of Insert mode complete from the key typed. |
| 4834 | * Returns BACKWARD or FORWARD. |
| 4835 | */ |
| 4836 | static int |
| 4837 | ins_compl_key2dir(c) |
| 4838 | int c; |
| 4839 | { |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4840 | if (c == Ctrl_P || c == Ctrl_L |
| 4841 | || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP |
| 4842 | || c == K_S_UP || c == K_UP))) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4843 | return BACKWARD; |
| 4844 | return FORWARD; |
| 4845 | } |
| 4846 | |
| 4847 | /* |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4848 | * Return TRUE for keys that are used for completion only when the popup menu |
| 4849 | * is visible. |
| 4850 | */ |
| 4851 | static int |
| 4852 | ins_compl_pum_key(c) |
| 4853 | int c; |
| 4854 | { |
| 4855 | 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] | 4856 | || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN |
| 4857 | || c == K_UP || c == K_DOWN); |
Bram Moolenaar | d12f5c1 | 2006-01-25 22:10:52 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
| 4860 | /* |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4861 | * Decide the number of completions to move forward. |
| 4862 | * Returns 1 for most keys, height of the popup menu for page-up/down keys. |
| 4863 | */ |
| 4864 | static int |
| 4865 | ins_compl_key2count(c) |
| 4866 | int c; |
| 4867 | { |
| 4868 | int h; |
| 4869 | |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4870 | if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN) |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4871 | { |
| 4872 | h = pum_get_height(); |
| 4873 | if (h > 3) |
| 4874 | h -= 2; /* keep some context */ |
| 4875 | return h; |
| 4876 | } |
| 4877 | return 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | } |
| 4879 | |
| 4880 | /* |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 4881 | * Return TRUE if completion with "c" should insert the match, FALSE if only |
| 4882 | * to change the currently selected completion. |
| 4883 | */ |
| 4884 | static int |
| 4885 | ins_compl_use_match(c) |
| 4886 | int c; |
| 4887 | { |
| 4888 | switch (c) |
| 4889 | { |
| 4890 | case K_UP: |
| 4891 | case K_DOWN: |
| 4892 | case K_PAGEDOWN: |
| 4893 | case K_KPAGEDOWN: |
| 4894 | case K_S_DOWN: |
| 4895 | case K_PAGEUP: |
| 4896 | case K_KPAGEUP: |
| 4897 | case K_S_UP: |
| 4898 | return FALSE; |
| 4899 | } |
| 4900 | return TRUE; |
| 4901 | } |
| 4902 | |
| 4903 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4904 | * Do Insert mode completion. |
| 4905 | * Called when character "c" was typed, which has a meaning for completion. |
| 4906 | * Returns OK if completion was done, FAIL if something failed (out of mem). |
| 4907 | */ |
| 4908 | static int |
| 4909 | ins_complete(c) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4910 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4911 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4912 | char_u *line; |
| 4913 | int startcol = 0; /* column where searched text starts */ |
| 4914 | colnr_T curs_col; /* cursor column */ |
| 4915 | int n; |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 4916 | int save_w_wrow; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4917 | |
Bram Moolenaar | e3226be | 2005-12-18 22:10:00 +0000 | [diff] [blame] | 4918 | compl_direction = ins_compl_key2dir(c); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4919 | if (!compl_started) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4920 | { |
| 4921 | /* First time we hit ^N or ^P (in a row, I mean) */ |
| 4922 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4923 | did_ai = FALSE; |
| 4924 | #ifdef FEAT_SMARTINDENT |
| 4925 | did_si = FALSE; |
| 4926 | can_si = FALSE; |
| 4927 | can_si_back = FALSE; |
| 4928 | #endif |
| 4929 | if (stop_arrow() == FAIL) |
| 4930 | return FAIL; |
| 4931 | |
| 4932 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4933 | curs_col = curwin->w_cursor.col; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4934 | compl_pending = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4935 | |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 4936 | /* 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] | 4937 | * "compl_startpos" to the cursor as a pattern to add a new word |
| 4938 | * instead of expand the one before the cursor, in word-wise if |
Bram Moolenaar | 711d5b5 | 2007-10-19 18:40:51 +0000 | [diff] [blame] | 4939 | * "compl_startpos" is not in the same line as the cursor then fix it |
| 4940 | * (the line has been split because it was longer than 'tw'). if SOL |
| 4941 | * is set then skip the previous pattern, a word at the beginning of |
| 4942 | * the line has been inserted, we'll look for that -- Acevedo. */ |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 4943 | if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT |
| 4944 | && compl_cont_mode == ctrl_x_mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4945 | { |
| 4946 | /* |
| 4947 | * it is a continued search |
| 4948 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4949 | compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS |
| 4951 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 4952 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4953 | if (compl_startpos.lnum != curwin->w_cursor.lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4955 | /* line (probably) wrapped, set compl_startpos to the |
| 4956 | * first non_blank in the line, if it is not a wordchar |
| 4957 | * include it to get a better pattern, but then we don't |
| 4958 | * want the "\\<" prefix, check it bellow */ |
| 4959 | compl_col = (colnr_T)(skipwhite(line) - line); |
| 4960 | compl_startpos.col = compl_col; |
| 4961 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 4962 | compl_cont_status &= ~CONT_SOL; /* clear SOL if present */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4963 | } |
| 4964 | else |
| 4965 | { |
| 4966 | /* S_IPOS was set when we inserted a word that was at the |
| 4967 | * beginning of the line, which means that we'll go to SOL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4968 | * mode but first we need to redefine compl_startpos */ |
| 4969 | if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4970 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4971 | compl_cont_status |= CONT_SOL; |
| 4972 | compl_startpos.col = (colnr_T)(skipwhite( |
| 4973 | line + compl_length |
| 4974 | + compl_startpos.col) - line); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4975 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4976 | compl_col = compl_startpos.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4977 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4978 | compl_length = curwin->w_cursor.col - (int)compl_col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 4979 | /* 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] | 4980 | * have enough space? just being paranoid */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4981 | #define MIN_SPACE 75 |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4982 | if (compl_length > (IOSIZE - MIN_SPACE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4983 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4984 | compl_cont_status &= ~CONT_SOL; |
| 4985 | compl_length = (IOSIZE - MIN_SPACE); |
| 4986 | compl_col = curwin->w_cursor.col - compl_length; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4987 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4988 | compl_cont_status |= CONT_ADDING | CONT_N_ADDS; |
| 4989 | if (compl_length < 1) |
| 4990 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4991 | } |
| 4992 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4993 | compl_cont_status = CONT_ADDING | CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4994 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4995 | compl_cont_status = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4996 | } |
| 4997 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 4998 | compl_cont_status &= CONT_LOCAL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5000 | if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5001 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5002 | compl_cont_mode = ctrl_x_mode; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5004 | compl_cont_status = 0; |
| 5005 | compl_cont_status |= CONT_N_ADDS; |
| 5006 | compl_startpos = curwin->w_cursor; |
| 5007 | startcol = (int)curs_col; |
| 5008 | compl_col = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5009 | } |
| 5010 | |
| 5011 | /* Work out completion pattern and original text -- webb */ |
| 5012 | if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT)) |
| 5013 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5014 | if ((compl_cont_status & CONT_SOL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5015 | || ctrl_x_mode == CTRL_X_PATH_DEFINES) |
| 5016 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5017 | if (!(compl_cont_status & CONT_ADDING)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5018 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5019 | while (--startcol >= 0 && vim_isIDc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5020 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5021 | compl_col += ++startcol; |
| 5022 | compl_length = curs_col - startcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5023 | } |
| 5024 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5025 | compl_pattern = str_foldcase(line + compl_col, |
| 5026 | compl_length, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5027 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5028 | compl_pattern = vim_strnsave(line + compl_col, |
| 5029 | compl_length); |
| 5030 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5031 | return FAIL; |
| 5032 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5033 | else if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5034 | { |
| 5035 | char_u *prefix = (char_u *)"\\<"; |
| 5036 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5037 | /* we need up to 2 extra chars for the prefix */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5038 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5039 | compl_length) + 2); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5040 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5041 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5042 | if (!vim_iswordp(line + compl_col) |
| 5043 | || (compl_col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | && ( |
| 5045 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5046 | vim_iswordp(mb_prevptr(line, line + compl_col)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5047 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5048 | vim_iswordc(line[compl_col - 1]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5049 | #endif |
| 5050 | ))) |
| 5051 | prefix = (char_u *)""; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5052 | STRCPY((char *)compl_pattern, prefix); |
| 5053 | (void)quote_meta(compl_pattern + STRLEN(prefix), |
| 5054 | line + compl_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 | else if (--startcol < 0 || |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5057 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5058 | !vim_iswordp(mb_prevptr(line, line + startcol + 1)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5059 | #else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5060 | !vim_iswordc(line[startcol]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5061 | #endif |
| 5062 | ) |
| 5063 | { |
| 5064 | /* Match any word of at least two chars */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5065 | compl_pattern = vim_strsave((char_u *)"\\<\\k\\k"); |
| 5066 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5067 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5068 | compl_col += curs_col; |
| 5069 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5070 | } |
| 5071 | else |
| 5072 | { |
| 5073 | #ifdef FEAT_MBYTE |
| 5074 | /* Search the point of change class of multibyte character |
| 5075 | * or not a word single byte character backward. */ |
| 5076 | if (has_mbyte) |
| 5077 | { |
| 5078 | int base_class; |
| 5079 | int head_off; |
| 5080 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5081 | startcol -= (*mb_head_off)(line, line + startcol); |
| 5082 | base_class = mb_get_class(line + startcol); |
| 5083 | while (--startcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5084 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5085 | head_off = (*mb_head_off)(line, line + startcol); |
| 5086 | if (base_class != mb_get_class(line + startcol |
| 5087 | - head_off)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5088 | break; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5089 | startcol -= head_off; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5090 | } |
| 5091 | } |
| 5092 | else |
| 5093 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5094 | while (--startcol >= 0 && vim_iswordc(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5095 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5096 | compl_col += ++startcol; |
| 5097 | compl_length = (int)curs_col - startcol; |
| 5098 | if (compl_length == 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5099 | { |
| 5100 | /* Only match word with at least two chars -- webb |
| 5101 | * there's no need to call quote_meta, |
| 5102 | * alloc(7) is enough -- Acevedo |
| 5103 | */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5104 | compl_pattern = alloc(7); |
| 5105 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5106 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5107 | STRCPY((char *)compl_pattern, "\\<"); |
| 5108 | (void)quote_meta(compl_pattern + 2, line + compl_col, 1); |
| 5109 | STRCAT((char *)compl_pattern, "\\k"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5110 | } |
| 5111 | else |
| 5112 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5113 | compl_pattern = alloc(quote_meta(NULL, line + compl_col, |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5114 | compl_length) + 2); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5115 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5116 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5117 | STRCPY((char *)compl_pattern, "\\<"); |
| 5118 | (void)quote_meta(compl_pattern + 2, line + compl_col, |
| 5119 | compl_length); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | else if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 5124 | { |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5125 | compl_col = (colnr_T)(skipwhite(line) - line); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5126 | compl_length = (int)curs_col - (int)compl_col; |
| 5127 | if (compl_length < 0) /* cursor in indent: empty pattern */ |
| 5128 | compl_length = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5129 | if (p_ic) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5130 | compl_pattern = str_foldcase(line + compl_col, compl_length, |
| 5131 | NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5132 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5133 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5134 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5135 | return FAIL; |
| 5136 | } |
| 5137 | else if (ctrl_x_mode == CTRL_X_FILES) |
| 5138 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5139 | while (--startcol >= 0 && vim_isfilec(line[startcol])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5140 | ; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5141 | compl_col += ++startcol; |
| 5142 | compl_length = (int)curs_col - startcol; |
| 5143 | compl_pattern = addstar(line + compl_col, compl_length, |
| 5144 | EXPAND_FILES); |
| 5145 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5146 | return FAIL; |
| 5147 | } |
| 5148 | else if (ctrl_x_mode == CTRL_X_CMDLINE) |
| 5149 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5150 | compl_pattern = vim_strnsave(line, curs_col); |
| 5151 | if (compl_pattern == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5152 | return FAIL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5153 | set_cmd_context(&compl_xp, compl_pattern, |
| 5154 | (int)STRLEN(compl_pattern), curs_col); |
| 5155 | if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL |
| 5156 | || compl_xp.xp_context == EXPAND_NOTHING) |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 5157 | /* No completion possible, use an empty pattern to get a |
| 5158 | * "pattern not found" message. */ |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5159 | compl_col = curs_col; |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5160 | else |
Bram Moolenaar | f83c5c0 | 2006-08-16 19:24:22 +0000 | [diff] [blame] | 5161 | compl_col = (int)(compl_xp.xp_pattern - compl_pattern); |
| 5162 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5163 | } |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5164 | 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] | 5165 | { |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5166 | #ifdef FEAT_COMPL_FUNC |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5167 | /* |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5168 | * Call user defined function 'completefunc' with "a:findstart" |
| 5169 | * 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] | 5170 | */ |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5171 | char_u *args[2]; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5172 | int col; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5173 | char_u *funcname; |
| 5174 | pos_T pos; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5175 | win_T *curwin_save; |
| 5176 | buf_T *curbuf_save; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5177 | |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5178 | /* Call 'completefunc' or 'omnifunc' and get pattern length as a |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5179 | * string */ |
| 5180 | funcname = ctrl_x_mode == CTRL_X_FUNCTION |
| 5181 | ? curbuf->b_p_cfu : curbuf->b_p_ofu; |
| 5182 | if (*funcname == NUL) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5183 | { |
| 5184 | EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION |
| 5185 | ? "completefunc" : "omnifunc"); |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5186 | return FAIL; |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5187 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5188 | |
| 5189 | args[0] = (char_u *)"1"; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5190 | args[1] = NULL; |
| 5191 | pos = curwin->w_cursor; |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5192 | curwin_save = curwin; |
| 5193 | curbuf_save = curbuf; |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5194 | col = call_func_retnr(funcname, 2, args, FALSE); |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5195 | if (curwin_save != curwin || curbuf_save != curbuf) |
| 5196 | { |
| 5197 | EMSG(_(e_complwin)); |
| 5198 | return FAIL; |
| 5199 | } |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 5200 | curwin->w_cursor = pos; /* restore the cursor position */ |
Bram Moolenaar | 37dd018 | 2010-11-10 16:54:20 +0100 | [diff] [blame] | 5201 | check_cursor(); |
| 5202 | if (!equalpos(curwin->w_cursor, pos)) |
| 5203 | { |
| 5204 | EMSG(_(e_compldel)); |
| 5205 | return FAIL; |
| 5206 | } |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5207 | |
Bram Moolenaar | 6110a00 | 2012-01-26 18:58:38 +0100 | [diff] [blame] | 5208 | /* Return value -2 means the user complete function wants to |
| 5209 | * cancel the complete without an error. */ |
| 5210 | if (col == -2) |
| 5211 | return FAIL; |
| 5212 | |
Bram Moolenaar | 8213908 | 2011-09-14 16:52:09 +0200 | [diff] [blame] | 5213 | /* |
| 5214 | * Reset extended parameters of completion, when start new |
| 5215 | * completion. |
| 5216 | */ |
| 5217 | compl_opt_refresh_always = FALSE; |
| 5218 | |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5219 | if (col < 0) |
Bram Moolenaar | f75a963 | 2005-09-13 21:20:47 +0000 | [diff] [blame] | 5220 | col = curs_col; |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5221 | compl_col = col; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5222 | if (compl_col > curs_col) |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5223 | compl_col = curs_col; |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5224 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5225 | /* Setup variables for completion. Need to obtain "line" again, |
| 5226 | * it may have become invalid. */ |
| 5227 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 5a8684e | 2005-07-30 22:43:24 +0000 | [diff] [blame] | 5228 | compl_length = curs_col - compl_col; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5229 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5230 | if (compl_pattern == NULL) |
Bram Moolenaar | cfbc5ee | 2004-07-02 15:38:35 +0000 | [diff] [blame] | 5231 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5232 | return FAIL; |
| 5233 | } |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 5234 | else if (ctrl_x_mode == CTRL_X_SPELL) |
| 5235 | { |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 5236 | #ifdef FEAT_SPELL |
Bram Moolenaar | 6e7c7f3 | 2005-08-24 22:16:11 +0000 | [diff] [blame] | 5237 | if (spell_bad_len > 0) |
| 5238 | compl_col = curs_col - spell_bad_len; |
| 5239 | else |
| 5240 | compl_col = spell_word_start(startcol); |
| 5241 | if (compl_col >= (colnr_T)startcol) |
Bram Moolenaar | be46a1e | 2006-06-22 15:13:21 +0000 | [diff] [blame] | 5242 | { |
| 5243 | compl_length = 0; |
| 5244 | compl_col = curs_col; |
| 5245 | } |
| 5246 | else |
| 5247 | { |
| 5248 | spell_expand_check_cap(compl_col); |
| 5249 | compl_length = (int)curs_col - compl_col; |
| 5250 | } |
Bram Moolenaar | e2f98b9 | 2006-03-29 21:18:24 +0000 | [diff] [blame] | 5251 | /* Need to obtain "line" again, it may have become invalid. */ |
| 5252 | line = ml_get(curwin->w_cursor.lnum); |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 5253 | compl_pattern = vim_strnsave(line + compl_col, compl_length); |
| 5254 | if (compl_pattern == NULL) |
| 5255 | #endif |
| 5256 | return FAIL; |
| 5257 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5258 | else |
| 5259 | { |
| 5260 | EMSG2(_(e_intern2), "ins_complete()"); |
| 5261 | return FAIL; |
| 5262 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5263 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5264 | if (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5265 | { |
| 5266 | edit_submode_pre = (char_u *)_(" Adding"); |
| 5267 | if (ctrl_x_mode == CTRL_X_WHOLE_LINE) |
| 5268 | { |
| 5269 | /* Insert a new line, keep indentation but ignore 'comments' */ |
| 5270 | #ifdef FEAT_COMMENTS |
| 5271 | char_u *old = curbuf->b_p_com; |
| 5272 | |
| 5273 | curbuf->b_p_com = (char_u *)""; |
| 5274 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5275 | compl_startpos.lnum = curwin->w_cursor.lnum; |
| 5276 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5277 | ins_eol('\r'); |
| 5278 | #ifdef FEAT_COMMENTS |
| 5279 | curbuf->b_p_com = old; |
| 5280 | #endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5281 | compl_length = 0; |
| 5282 | compl_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | else |
| 5286 | { |
| 5287 | edit_submode_pre = NULL; |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5288 | compl_startpos.col = compl_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | } |
| 5290 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5291 | if (compl_cont_status & CONT_LOCAL) |
| 5292 | edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5293 | else |
| 5294 | edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode)); |
| 5295 | |
Bram Moolenaar | 62951b1 | 2011-09-21 18:23:05 +0200 | [diff] [blame] | 5296 | /* If any of the original typed text has been changed we need to fix |
| 5297 | * the redo buffer. */ |
| 5298 | ins_compl_fixRedoBufForLeader(NULL); |
| 5299 | |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 5300 | /* Always add completion for the original text. */ |
| 5301 | vim_free(compl_orig_text); |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5302 | compl_orig_text = vim_strnsave(line + compl_col, compl_length); |
| 5303 | if (compl_orig_text == NULL || ins_compl_add(compl_orig_text, |
Bram Moolenaar | e8c3a14 | 2006-08-29 14:30:35 +0000 | [diff] [blame] | 5304 | -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5305 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5306 | vim_free(compl_pattern); |
| 5307 | compl_pattern = NULL; |
| 5308 | vim_free(compl_orig_text); |
| 5309 | compl_orig_text = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5310 | return FAIL; |
| 5311 | } |
| 5312 | |
| 5313 | /* showmode might reset the internal line pointers, so it must |
| 5314 | * be called before line = ml_get(), or when this address is no |
| 5315 | * longer needed. -- Acevedo. |
| 5316 | */ |
| 5317 | edit_submode_extra = (char_u *)_("-- Searching..."); |
| 5318 | edit_submode_highl = HLF_COUNT; |
| 5319 | showmode(); |
| 5320 | edit_submode_extra = NULL; |
| 5321 | out_flush(); |
| 5322 | } |
| 5323 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5324 | compl_shown_match = compl_curr_match; |
| 5325 | compl_shows_dir = compl_direction; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5326 | |
| 5327 | /* |
Bram Moolenaar | c7453f5 | 2006-02-10 23:20:28 +0000 | [diff] [blame] | 5328 | * Find next match (and following matches). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | */ |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 5330 | save_w_wrow = curwin->w_wrow; |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 5331 | 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] | 5332 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5333 | /* may undisplay the popup menu */ |
| 5334 | ins_compl_upd_pum(); |
| 5335 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5336 | if (n > 1) /* all matches have been found */ |
| 5337 | compl_matches = n; |
| 5338 | compl_curr_match = compl_shown_match; |
| 5339 | compl_direction = compl_shows_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5340 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5341 | /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert |
| 5342 | * mode. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5343 | if (got_int && !global_busy) |
| 5344 | { |
| 5345 | (void)vgetc(); |
| 5346 | got_int = FALSE; |
| 5347 | } |
| 5348 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5349 | /* 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] | 5350 | if (compl_first_match == compl_first_match->cp_next) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | { |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5352 | edit_submode_extra = (compl_cont_status & CONT_ADDING) |
| 5353 | && compl_length > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5354 | ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf); |
| 5355 | edit_submode_highl = HLF_E; |
| 5356 | /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode, |
| 5357 | * because we couldn't expand anything at first place, but if we used |
| 5358 | * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word |
| 5359 | * (such as M in M'exico) if not tried already. -- Acevedo */ |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5360 | if ( compl_length > 1 |
| 5361 | || (compl_cont_status & CONT_ADDING) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5362 | || (ctrl_x_mode != 0 |
| 5363 | && ctrl_x_mode != CTRL_X_PATH_PATTERNS |
| 5364 | && ctrl_x_mode != CTRL_X_PATH_DEFINES)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5365 | compl_cont_status &= ~CONT_N_ADDS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5366 | } |
| 5367 | |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5368 | if (compl_curr_match->cp_flags & CONT_S_IPOS) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5369 | compl_cont_status |= CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5370 | else |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5371 | compl_cont_status &= ~CONT_S_IPOS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5372 | |
| 5373 | if (edit_submode_extra == NULL) |
| 5374 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5375 | if (compl_curr_match->cp_flags & ORIGINAL_TEXT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5376 | { |
| 5377 | edit_submode_extra = (char_u *)_("Back at original"); |
| 5378 | edit_submode_highl = HLF_W; |
| 5379 | } |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5380 | else if (compl_cont_status & CONT_S_IPOS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5381 | { |
| 5382 | edit_submode_extra = (char_u *)_("Word from other line"); |
| 5383 | edit_submode_highl = HLF_COUNT; |
| 5384 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5385 | else if (compl_curr_match->cp_next == compl_curr_match->cp_prev) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5386 | { |
| 5387 | edit_submode_extra = (char_u *)_("The only match"); |
| 5388 | edit_submode_highl = HLF_COUNT; |
| 5389 | } |
| 5390 | else |
| 5391 | { |
| 5392 | /* Update completion sequence number when needed. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5393 | if (compl_curr_match->cp_number == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5394 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5395 | int number = 0; |
| 5396 | compl_T *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5397 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5398 | if (compl_direction == FORWARD) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5399 | { |
| 5400 | /* search backwards for the first valid (!= -1) number. |
| 5401 | * This should normally succeed already at the first loop |
| 5402 | * cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5403 | for (match = compl_curr_match->cp_prev; match != NULL |
| 5404 | && match != compl_first_match; |
| 5405 | match = match->cp_prev) |
| 5406 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5407 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5408 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5409 | break; |
| 5410 | } |
| 5411 | if (match != NULL) |
| 5412 | /* go up and assign all numbers which are not assigned |
| 5413 | * yet */ |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5414 | for (match = match->cp_next; |
| 5415 | match != NULL && match->cp_number == -1; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5416 | match = match->cp_next) |
| 5417 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5418 | } |
| 5419 | else /* BACKWARD */ |
| 5420 | { |
| 5421 | /* search forwards (upwards) for the first valid (!= -1) |
| 5422 | * number. This should normally succeed already at the |
| 5423 | * first loop cycle, so it's fast! */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5424 | for (match = compl_curr_match->cp_next; match != NULL |
| 5425 | && match != compl_first_match; |
| 5426 | match = match->cp_next) |
| 5427 | if (match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5428 | { |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5429 | number = match->cp_number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5430 | break; |
| 5431 | } |
| 5432 | if (match != NULL) |
| 5433 | /* go down and assign all numbers which are not |
| 5434 | * assigned yet */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5435 | for (match = match->cp_prev; match |
| 5436 | && match->cp_number == -1; |
| 5437 | match = match->cp_prev) |
| 5438 | match->cp_number = ++number; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5439 | } |
| 5440 | } |
| 5441 | |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5442 | /* The match should always have a sequence number now, this is |
| 5443 | * just a safety check. */ |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5444 | if (compl_curr_match->cp_number != -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5445 | { |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5446 | /* Space for 10 text chars. + 2x10-digit no.s = 31. |
| 5447 | * Translations may need more than twice that. */ |
| 5448 | static char_u match_ref[81]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5449 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 5450 | if (compl_matches > 0) |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5451 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 5452 | _("match %d of %d"), |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5453 | compl_curr_match->cp_number, compl_matches); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5454 | else |
Bram Moolenaar | 0739a1e | 2007-02-04 01:37:39 +0000 | [diff] [blame] | 5455 | vim_snprintf((char *)match_ref, sizeof(match_ref), |
| 5456 | _("match %d"), |
| 5457 | compl_curr_match->cp_number); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5458 | edit_submode_extra = match_ref; |
| 5459 | edit_submode_highl = HLF_R; |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 5460 | if (dollar_vcol >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5461 | curs_columns(FALSE); |
| 5462 | } |
| 5463 | } |
| 5464 | } |
| 5465 | |
| 5466 | /* Show a message about what (completion) mode we're in. */ |
| 5467 | showmode(); |
| 5468 | if (edit_submode_extra != NULL) |
| 5469 | { |
| 5470 | if (!p_smd) |
| 5471 | msg_attr(edit_submode_extra, |
| 5472 | edit_submode_highl < HLF_COUNT |
| 5473 | ? hl_attr(edit_submode_highl) : 0); |
| 5474 | } |
| 5475 | else |
| 5476 | msg_clr_cmdline(); /* necessary for "noshowmode" */ |
| 5477 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5478 | /* Show the popup menu, unless we got interrupted. */ |
| 5479 | if (!compl_interrupted) |
| 5480 | { |
| 5481 | /* RedrawingDisabled may be set when invoked through complete(). */ |
| 5482 | n = RedrawingDisabled; |
| 5483 | RedrawingDisabled = 0; |
Bram Moolenaar | be678f8 | 2010-03-10 14:15:54 +0100 | [diff] [blame] | 5484 | |
| 5485 | /* If the cursor moved we need to remove the pum first. */ |
| 5486 | setcursor(); |
| 5487 | if (save_w_wrow != curwin->w_wrow) |
| 5488 | ins_compl_del_pum(); |
| 5489 | |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5490 | ins_compl_show_pum(); |
| 5491 | setcursor(); |
| 5492 | RedrawingDisabled = n; |
| 5493 | } |
Bram Moolenaar | 1423b9d | 2006-05-07 15:16:06 +0000 | [diff] [blame] | 5494 | compl_was_interrupted = compl_interrupted; |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 5495 | compl_interrupted = FALSE; |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 5496 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5497 | return OK; |
| 5498 | } |
| 5499 | |
| 5500 | /* |
| 5501 | * Looks in the first "len" chars. of "src" for search-metachars. |
| 5502 | * If dest is not NULL the chars. are copied there quoting (with |
| 5503 | * a backslash) the metachars, and dest would be NUL terminated. |
| 5504 | * Returns the length (needed) of dest |
| 5505 | */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5506 | static unsigned |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5507 | quote_meta(dest, src, len) |
| 5508 | char_u *dest; |
| 5509 | char_u *src; |
| 5510 | int len; |
| 5511 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5512 | unsigned m = (unsigned)len + 1; /* one extra for the NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5513 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 5514 | for ( ; --len >= 0; src++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5515 | { |
| 5516 | switch (*src) |
| 5517 | { |
| 5518 | case '.': |
| 5519 | case '*': |
| 5520 | case '[': |
| 5521 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5522 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5523 | break; |
| 5524 | case '~': |
| 5525 | if (!p_magic) /* quote these only if magic is set */ |
| 5526 | break; |
| 5527 | case '\\': |
| 5528 | if (ctrl_x_mode == CTRL_X_DICTIONARY |
| 5529 | || ctrl_x_mode == CTRL_X_THESAURUS) |
| 5530 | break; |
| 5531 | case '^': /* currently it's not needed. */ |
| 5532 | case '$': |
| 5533 | m++; |
| 5534 | if (dest != NULL) |
| 5535 | *dest++ = '\\'; |
| 5536 | break; |
| 5537 | } |
| 5538 | if (dest != NULL) |
| 5539 | *dest++ = *src; |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5540 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5541 | /* Copy remaining bytes of a multibyte character. */ |
| 5542 | if (has_mbyte) |
| 5543 | { |
| 5544 | int i, mb_len; |
| 5545 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5546 | mb_len = (*mb_ptr2len)(src) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5547 | if (mb_len > 0 && len >= mb_len) |
| 5548 | for (i = 0; i < mb_len; ++i) |
| 5549 | { |
| 5550 | --len; |
| 5551 | ++src; |
| 5552 | if (dest != NULL) |
| 5553 | *dest++ = *src; |
| 5554 | } |
| 5555 | } |
Bram Moolenaar | 572cb56 | 2005-08-05 21:35:02 +0000 | [diff] [blame] | 5556 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5557 | } |
| 5558 | if (dest != NULL) |
| 5559 | *dest = NUL; |
| 5560 | |
| 5561 | return m; |
| 5562 | } |
| 5563 | #endif /* FEAT_INS_EXPAND */ |
| 5564 | |
| 5565 | /* |
| 5566 | * Next character is interpreted literally. |
| 5567 | * A one, two or three digit decimal number is interpreted as its byte value. |
| 5568 | * If one or two digits are entered, the next character is given to vungetc(). |
| 5569 | * For Unicode a character > 255 may be returned. |
| 5570 | */ |
| 5571 | int |
| 5572 | get_literal() |
| 5573 | { |
| 5574 | int cc; |
| 5575 | int nc; |
| 5576 | int i; |
| 5577 | int hex = FALSE; |
| 5578 | int octal = FALSE; |
| 5579 | #ifdef FEAT_MBYTE |
| 5580 | int unicode = 0; |
| 5581 | #endif |
| 5582 | |
| 5583 | if (got_int) |
| 5584 | return Ctrl_C; |
| 5585 | |
| 5586 | #ifdef FEAT_GUI |
| 5587 | /* |
| 5588 | * In GUI there is no point inserting the internal code for a special key. |
| 5589 | * It is more useful to insert the string "<KEY>" instead. This would |
| 5590 | * probably be useful in a text window too, but it would not be |
| 5591 | * vi-compatible (maybe there should be an option for it?) -- webb |
| 5592 | */ |
| 5593 | if (gui.in_use) |
| 5594 | ++allow_keys; |
| 5595 | #endif |
| 5596 | #ifdef USE_ON_FLY_SCROLL |
| 5597 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 5598 | #endif |
| 5599 | ++no_mapping; /* don't map the next key hits */ |
| 5600 | cc = 0; |
| 5601 | i = 0; |
| 5602 | for (;;) |
| 5603 | { |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 5604 | nc = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5605 | #ifdef FEAT_CMDL_INFO |
| 5606 | if (!(State & CMDLINE) |
| 5607 | # ifdef FEAT_MBYTE |
| 5608 | && MB_BYTE2LEN_CHECK(nc) == 1 |
| 5609 | # endif |
| 5610 | ) |
| 5611 | add_to_showcmd(nc); |
| 5612 | #endif |
| 5613 | if (nc == 'x' || nc == 'X') |
| 5614 | hex = TRUE; |
| 5615 | else if (nc == 'o' || nc == 'O') |
| 5616 | octal = TRUE; |
| 5617 | #ifdef FEAT_MBYTE |
| 5618 | else if (nc == 'u' || nc == 'U') |
| 5619 | unicode = nc; |
| 5620 | #endif |
| 5621 | else |
| 5622 | { |
| 5623 | if (hex |
| 5624 | #ifdef FEAT_MBYTE |
| 5625 | || unicode != 0 |
| 5626 | #endif |
| 5627 | ) |
| 5628 | { |
| 5629 | if (!vim_isxdigit(nc)) |
| 5630 | break; |
| 5631 | cc = cc * 16 + hex2nr(nc); |
| 5632 | } |
| 5633 | else if (octal) |
| 5634 | { |
| 5635 | if (nc < '0' || nc > '7') |
| 5636 | break; |
| 5637 | cc = cc * 8 + nc - '0'; |
| 5638 | } |
| 5639 | else |
| 5640 | { |
| 5641 | if (!VIM_ISDIGIT(nc)) |
| 5642 | break; |
| 5643 | cc = cc * 10 + nc - '0'; |
| 5644 | } |
| 5645 | |
| 5646 | ++i; |
| 5647 | } |
| 5648 | |
| 5649 | if (cc > 255 |
| 5650 | #ifdef FEAT_MBYTE |
| 5651 | && unicode == 0 |
| 5652 | #endif |
| 5653 | ) |
| 5654 | cc = 255; /* limit range to 0-255 */ |
| 5655 | nc = 0; |
| 5656 | |
| 5657 | if (hex) /* hex: up to two chars */ |
| 5658 | { |
| 5659 | if (i >= 2) |
| 5660 | break; |
| 5661 | } |
| 5662 | #ifdef FEAT_MBYTE |
| 5663 | else if (unicode) /* Unicode: up to four or eight chars */ |
| 5664 | { |
| 5665 | if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8)) |
| 5666 | break; |
| 5667 | } |
| 5668 | #endif |
| 5669 | else if (i >= 3) /* decimal or octal: up to three chars */ |
| 5670 | break; |
| 5671 | } |
| 5672 | if (i == 0) /* no number entered */ |
| 5673 | { |
| 5674 | if (nc == K_ZERO) /* NUL is stored as NL */ |
| 5675 | { |
| 5676 | cc = '\n'; |
| 5677 | nc = 0; |
| 5678 | } |
| 5679 | else |
| 5680 | { |
| 5681 | cc = nc; |
| 5682 | nc = 0; |
| 5683 | } |
| 5684 | } |
| 5685 | |
| 5686 | if (cc == 0) /* NUL is stored as NL */ |
| 5687 | cc = '\n'; |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 5688 | #ifdef FEAT_MBYTE |
| 5689 | if (enc_dbcs && (cc & 0xff) == 0) |
| 5690 | cc = '?'; /* don't accept an illegal DBCS char, the NUL in the |
| 5691 | second byte will cause trouble! */ |
| 5692 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5693 | |
| 5694 | --no_mapping; |
| 5695 | #ifdef FEAT_GUI |
| 5696 | if (gui.in_use) |
| 5697 | --allow_keys; |
| 5698 | #endif |
| 5699 | if (nc) |
| 5700 | vungetc(nc); |
| 5701 | got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */ |
| 5702 | return cc; |
| 5703 | } |
| 5704 | |
| 5705 | /* |
| 5706 | * Insert character, taking care of special keys and mod_mask |
| 5707 | */ |
| 5708 | static void |
| 5709 | insert_special(c, allow_modmask, ctrlv) |
| 5710 | int c; |
| 5711 | int allow_modmask; |
| 5712 | int ctrlv; /* c was typed after CTRL-V */ |
| 5713 | { |
| 5714 | char_u *p; |
| 5715 | int len; |
| 5716 | |
| 5717 | /* |
| 5718 | * Special function key, translate into "<Key>". Up to the last '>' is |
| 5719 | * inserted with ins_str(), so as not to replace characters in replace |
| 5720 | * mode. |
| 5721 | * Only use mod_mask for special keys, to avoid things like <S-Space>, |
| 5722 | * unless 'allow_modmask' is TRUE. |
| 5723 | */ |
| 5724 | #ifdef MACOS |
| 5725 | /* Command-key never produces a normal key */ |
| 5726 | if (mod_mask & MOD_MASK_CMD) |
| 5727 | allow_modmask = TRUE; |
| 5728 | #endif |
| 5729 | if (IS_SPECIAL(c) || (mod_mask && allow_modmask)) |
| 5730 | { |
| 5731 | p = get_special_key_name(c, mod_mask); |
| 5732 | len = (int)STRLEN(p); |
| 5733 | c = p[len - 1]; |
| 5734 | if (len > 2) |
| 5735 | { |
| 5736 | if (stop_arrow() == FAIL) |
| 5737 | return; |
| 5738 | p[len - 1] = NUL; |
| 5739 | ins_str(p); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5740 | AppendToRedobuffLit(p, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5741 | ctrlv = FALSE; |
| 5742 | } |
| 5743 | } |
| 5744 | if (stop_arrow() == OK) |
| 5745 | insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1); |
| 5746 | } |
| 5747 | |
| 5748 | /* |
| 5749 | * Special characters in this context are those that need processing other |
| 5750 | * than the simple insertion that can be performed here. This includes ESC |
| 5751 | * which terminates the insert, and CR/NL which need special processing to |
| 5752 | * open up a new line. This routine tries to optimize insertions performed by |
| 5753 | * the "redo", "undo" or "put" commands, so it needs to know when it should |
| 5754 | * stop and defer processing to the "normal" mechanism. |
| 5755 | * '0' and '^' are special, because they can be followed by CTRL-D. |
| 5756 | */ |
| 5757 | #ifdef EBCDIC |
| 5758 | # define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^') |
| 5759 | #else |
| 5760 | # define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^') |
| 5761 | #endif |
| 5762 | |
| 5763 | #ifdef FEAT_MBYTE |
| 5764 | # define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1)))) |
| 5765 | #else |
| 5766 | # define WHITECHAR(cc) vim_iswhite(cc) |
| 5767 | #endif |
| 5768 | |
| 5769 | void |
| 5770 | insertchar(c, flags, second_indent) |
| 5771 | int c; /* character to insert or NUL */ |
| 5772 | int flags; /* INSCHAR_FORMAT, etc. */ |
| 5773 | int second_indent; /* indent for second line if >= 0 */ |
| 5774 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5775 | int textwidth; |
| 5776 | #ifdef FEAT_COMMENTS |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5777 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5778 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5779 | int fo_ins_blank; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5780 | |
| 5781 | textwidth = comp_textwidth(flags & INSCHAR_FORMAT); |
| 5782 | fo_ins_blank = has_format_option(FO_INS_BLANK); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5783 | |
| 5784 | /* |
| 5785 | * Try to break the line in two or more pieces when: |
| 5786 | * - Always do this if we have been called to do formatting only. |
| 5787 | * - Always do this when 'formatoptions' has the 'a' flag and the line |
| 5788 | * ends in white space. |
| 5789 | * - Otherwise: |
| 5790 | * - Don't do this if inserting a blank |
| 5791 | * - Don't do this if an existing character is being replaced, unless |
| 5792 | * we're in VREPLACE mode. |
| 5793 | * - Do this if the cursor is not on the line where insert started |
| 5794 | * or - 'formatoptions' doesn't have 'l' or the line was not too long |
| 5795 | * before the insert. |
| 5796 | * - 'formatoptions' doesn't have 'b' or a blank was inserted at or |
| 5797 | * before 'textwidth' |
| 5798 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5799 | if (textwidth > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5800 | && ((flags & INSCHAR_FORMAT) |
| 5801 | || (!vim_iswhite(c) |
| 5802 | && !((State & REPLACE_FLAG) |
| 5803 | #ifdef FEAT_VREPLACE |
| 5804 | && !(State & VREPLACE_FLAG) |
| 5805 | #endif |
| 5806 | && *ml_get_cursor() != NUL) |
| 5807 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 5808 | || ((!has_format_option(FO_INS_LONG) |
| 5809 | || Insstart_textlen <= (colnr_T)textwidth) |
| 5810 | && (!fo_ins_blank |
| 5811 | || Insstart_blank_vcol <= (colnr_T)textwidth |
| 5812 | )))))) |
| 5813 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5814 | /* Format with 'formatexpr' when it's set. Use internal formatting |
| 5815 | * when 'formatexpr' isn't set or it returns non-zero. */ |
| 5816 | #if defined(FEAT_EVAL) |
Bram Moolenaar | f3442e7 | 2006-10-10 13:49:10 +0000 | [diff] [blame] | 5817 | int do_internal = TRUE; |
| 5818 | |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 5819 | if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0) |
Bram Moolenaar | f3442e7 | 2006-10-10 13:49:10 +0000 | [diff] [blame] | 5820 | { |
| 5821 | do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0); |
| 5822 | /* It may be required to save for undo again, e.g. when setline() |
| 5823 | * was called. */ |
| 5824 | ins_need_undo = TRUE; |
| 5825 | } |
| 5826 | if (do_internal) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5827 | #endif |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 5828 | internal_format(textwidth, second_indent, flags, c == NUL, c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5829 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5830 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5831 | if (c == NUL) /* only formatting was wanted */ |
| 5832 | return; |
| 5833 | |
| 5834 | #ifdef FEAT_COMMENTS |
| 5835 | /* Check whether this character should end a comment. */ |
| 5836 | if (did_ai && (int)c == end_comment_pending) |
| 5837 | { |
| 5838 | char_u *line; |
| 5839 | char_u lead_end[COM_MAX_LEN]; /* end-comment string */ |
| 5840 | int middle_len, end_len; |
| 5841 | int i; |
| 5842 | |
| 5843 | /* |
| 5844 | * Need to remove existing (middle) comment leader and insert end |
| 5845 | * comment leader. First, check what comment leader we can find. |
| 5846 | */ |
| 5847 | i = get_leader_len(line = ml_get_curline(), &p, FALSE); |
| 5848 | if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */ |
| 5849 | { |
| 5850 | /* Skip middle-comment string */ |
| 5851 | while (*p && p[-1] != ':') /* find end of middle flags */ |
| 5852 | ++p; |
| 5853 | middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5854 | /* Don't count trailing white space for middle_len */ |
| 5855 | while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1])) |
| 5856 | --middle_len; |
| 5857 | |
| 5858 | /* Find the end-comment string */ |
| 5859 | while (*p && p[-1] != ':') /* find end of end flags */ |
| 5860 | ++p; |
| 5861 | end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ","); |
| 5862 | |
| 5863 | /* Skip white space before the cursor */ |
| 5864 | i = curwin->w_cursor.col; |
| 5865 | while (--i >= 0 && vim_iswhite(line[i])) |
| 5866 | ; |
| 5867 | i++; |
| 5868 | |
| 5869 | /* Skip to before the middle leader */ |
| 5870 | i -= middle_len; |
| 5871 | |
| 5872 | /* Check some expected things before we go on */ |
| 5873 | if (i >= 0 && lead_end[end_len - 1] == end_comment_pending) |
| 5874 | { |
| 5875 | /* Backspace over all the stuff we want to replace */ |
| 5876 | backspace_until_column(i); |
| 5877 | |
| 5878 | /* |
| 5879 | * Insert the end-comment string, except for the last |
| 5880 | * character, which will get inserted as normal later. |
| 5881 | */ |
| 5882 | ins_bytes_len(lead_end, end_len - 1); |
| 5883 | } |
| 5884 | } |
| 5885 | } |
| 5886 | end_comment_pending = NUL; |
| 5887 | #endif |
| 5888 | |
| 5889 | did_ai = FALSE; |
| 5890 | #ifdef FEAT_SMARTINDENT |
| 5891 | did_si = FALSE; |
| 5892 | can_si = FALSE; |
| 5893 | can_si_back = FALSE; |
| 5894 | #endif |
| 5895 | |
| 5896 | /* |
| 5897 | * If there's any pending input, grab up to INPUT_BUFLEN at once. |
| 5898 | * This speeds up normal text input considerably. |
| 5899 | * Don't do this when 'cindent' or 'indentexpr' is set, because we might |
| 5900 | * need to re-indent at a ':', or any other character (but not what |
| 5901 | * 'paste' is set).. |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 5902 | * Don't do this when there an InsertCharPre autocommand is defined, |
| 5903 | * because we need to fire the event for every character. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | */ |
| 5905 | #ifdef USE_ON_FLY_SCROLL |
| 5906 | dont_scroll = FALSE; /* allow scrolling here */ |
| 5907 | #endif |
| 5908 | |
| 5909 | if ( !ISSPECIAL(c) |
| 5910 | #ifdef FEAT_MBYTE |
| 5911 | && (!has_mbyte || (*mb_char2len)(c) == 1) |
| 5912 | #endif |
| 5913 | && vpeekc() != NUL |
| 5914 | && !(State & REPLACE_FLAG) |
| 5915 | #ifdef FEAT_CINDENT |
| 5916 | && !cindent_on() |
| 5917 | #endif |
| 5918 | #ifdef FEAT_RIGHTLEFT |
| 5919 | && !p_ri |
| 5920 | #endif |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 5921 | #ifdef FEAT_AUTOCMD |
| 5922 | && !has_insertcharpre() |
| 5923 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5924 | ) |
| 5925 | { |
| 5926 | #define INPUT_BUFLEN 100 |
| 5927 | char_u buf[INPUT_BUFLEN + 1]; |
| 5928 | int i; |
| 5929 | colnr_T virtcol = 0; |
| 5930 | |
| 5931 | buf[0] = c; |
| 5932 | i = 1; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5933 | if (textwidth > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5934 | virtcol = get_nolist_virtcol(); |
| 5935 | /* |
| 5936 | * Stop the string when: |
| 5937 | * - no more chars available |
| 5938 | * - finding a special character (command key) |
| 5939 | * - buffer is full |
| 5940 | * - running into the 'textwidth' boundary |
| 5941 | * - need to check for abbreviation: A non-word char after a word-char |
| 5942 | */ |
| 5943 | while ( (c = vpeekc()) != NUL |
| 5944 | && !ISSPECIAL(c) |
| 5945 | #ifdef FEAT_MBYTE |
| 5946 | && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1) |
| 5947 | #endif |
| 5948 | && i < INPUT_BUFLEN |
| 5949 | && (textwidth == 0 |
| 5950 | || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth) |
| 5951 | && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1]))) |
| 5952 | { |
| 5953 | #ifdef FEAT_RIGHTLEFT |
| 5954 | c = vgetc(); |
| 5955 | if (p_hkmap && KeyTyped) |
| 5956 | c = hkmap(c); /* Hebrew mode mapping */ |
| 5957 | # ifdef FEAT_FKMAP |
| 5958 | if (p_fkmap && KeyTyped) |
| 5959 | c = fkmap(c); /* Farsi mode mapping */ |
| 5960 | # endif |
| 5961 | buf[i++] = c; |
| 5962 | #else |
| 5963 | buf[i++] = vgetc(); |
| 5964 | #endif |
| 5965 | } |
| 5966 | |
| 5967 | #ifdef FEAT_DIGRAPHS |
| 5968 | do_digraph(-1); /* clear digraphs */ |
| 5969 | do_digraph(buf[i-1]); /* may be the start of a digraph */ |
| 5970 | #endif |
| 5971 | buf[i] = NUL; |
| 5972 | ins_str(buf); |
| 5973 | if (flags & INSCHAR_CTRLV) |
| 5974 | { |
| 5975 | redo_literal(*buf); |
| 5976 | i = 1; |
| 5977 | } |
| 5978 | else |
| 5979 | i = 0; |
| 5980 | if (buf[i] != NUL) |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 5981 | AppendToRedobuffLit(buf + i, -1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5982 | } |
| 5983 | else |
| 5984 | { |
| 5985 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5986 | int cc; |
| 5987 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5988 | if (has_mbyte && (cc = (*mb_char2len)(c)) > 1) |
| 5989 | { |
| 5990 | char_u buf[MB_MAXBYTES + 1]; |
| 5991 | |
| 5992 | (*mb_char2bytes)(c, buf); |
| 5993 | buf[cc] = NUL; |
| 5994 | ins_char_bytes(buf, cc); |
| 5995 | AppendCharToRedobuff(c); |
| 5996 | } |
| 5997 | else |
| 5998 | #endif |
| 5999 | { |
| 6000 | ins_char(c); |
| 6001 | if (flags & INSCHAR_CTRLV) |
| 6002 | redo_literal(c); |
| 6003 | else |
| 6004 | AppendCharToRedobuff(c); |
| 6005 | } |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | /* |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6010 | * Format text at the current insert position. |
| 6011 | */ |
| 6012 | static void |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6013 | internal_format(textwidth, second_indent, flags, format_only, c) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6014 | int textwidth; |
| 6015 | int second_indent; |
| 6016 | int flags; |
| 6017 | int format_only; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6018 | int c; /* character to be inserted (can be NUL) */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6019 | { |
| 6020 | int cc; |
| 6021 | int save_char = NUL; |
| 6022 | int haveto_redraw = FALSE; |
| 6023 | int fo_ins_blank = has_format_option(FO_INS_BLANK); |
| 6024 | #ifdef FEAT_MBYTE |
| 6025 | int fo_multibyte = has_format_option(FO_MBYTE_BREAK); |
| 6026 | #endif |
| 6027 | int fo_white_par = has_format_option(FO_WHITE_PAR); |
| 6028 | int first_line = TRUE; |
| 6029 | #ifdef FEAT_COMMENTS |
| 6030 | colnr_T leader_len; |
| 6031 | int no_leader = FALSE; |
| 6032 | int do_comments = (flags & INSCHAR_DO_COM); |
| 6033 | #endif |
| 6034 | |
| 6035 | /* |
| 6036 | * When 'ai' is off we don't want a space under the cursor to be |
| 6037 | * deleted. Replace it with an 'x' temporarily. |
| 6038 | */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6039 | if (!curbuf->b_p_ai |
| 6040 | #ifdef FEAT_VREPLACE |
| 6041 | && !(State & VREPLACE_FLAG) |
| 6042 | #endif |
| 6043 | ) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6044 | { |
| 6045 | cc = gchar_cursor(); |
| 6046 | if (vim_iswhite(cc)) |
| 6047 | { |
| 6048 | save_char = cc; |
| 6049 | pchar_cursor('x'); |
| 6050 | } |
| 6051 | } |
| 6052 | |
| 6053 | /* |
| 6054 | * Repeat breaking lines, until the current line is not too long. |
| 6055 | */ |
| 6056 | while (!got_int) |
| 6057 | { |
| 6058 | int startcol; /* Cursor column at entry */ |
| 6059 | int wantcol; /* column at textwidth border */ |
| 6060 | int foundcol; /* column for start of spaces */ |
| 6061 | int end_foundcol = 0; /* column for start of word */ |
| 6062 | colnr_T len; |
| 6063 | colnr_T virtcol; |
| 6064 | #ifdef FEAT_VREPLACE |
| 6065 | int orig_col = 0; |
| 6066 | char_u *saved_text = NULL; |
| 6067 | #endif |
| 6068 | colnr_T col; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6069 | colnr_T end_col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6070 | |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6071 | virtcol = get_nolist_virtcol() |
| 6072 | + char2cells(c != NUL ? c : gchar_cursor()); |
| 6073 | if (virtcol <= (colnr_T)textwidth) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6074 | break; |
| 6075 | |
| 6076 | #ifdef FEAT_COMMENTS |
| 6077 | if (no_leader) |
| 6078 | do_comments = FALSE; |
| 6079 | else if (!(flags & INSCHAR_FORMAT) |
| 6080 | && has_format_option(FO_WRAP_COMS)) |
| 6081 | do_comments = TRUE; |
| 6082 | |
| 6083 | /* Don't break until after the comment leader */ |
| 6084 | if (do_comments) |
| 6085 | leader_len = get_leader_len(ml_get_curline(), NULL, FALSE); |
| 6086 | else |
| 6087 | leader_len = 0; |
| 6088 | |
| 6089 | /* If the line doesn't start with a comment leader, then don't |
| 6090 | * start one in a following broken line. Avoids that a %word |
| 6091 | * moved to the start of the next line causes all following lines |
| 6092 | * to start with %. */ |
| 6093 | if (leader_len == 0) |
| 6094 | no_leader = TRUE; |
| 6095 | #endif |
| 6096 | if (!(flags & INSCHAR_FORMAT) |
| 6097 | #ifdef FEAT_COMMENTS |
| 6098 | && leader_len == 0 |
| 6099 | #endif |
| 6100 | && !has_format_option(FO_WRAP)) |
| 6101 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6102 | break; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6103 | if ((startcol = curwin->w_cursor.col) == 0) |
| 6104 | break; |
| 6105 | |
| 6106 | /* find column of textwidth border */ |
| 6107 | coladvance((colnr_T)textwidth); |
| 6108 | wantcol = curwin->w_cursor.col; |
| 6109 | |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6110 | curwin->w_cursor.col = startcol; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6111 | foundcol = 0; |
| 6112 | |
| 6113 | /* |
| 6114 | * Find position to break at. |
| 6115 | * Stop at first entered white when 'formatoptions' has 'v' |
| 6116 | */ |
| 6117 | while ((!fo_ins_blank && !has_format_option(FO_INS_VI)) |
Bram Moolenaar | a27ad5a | 2011-10-26 17:04:29 +0200 | [diff] [blame] | 6118 | || (flags & INSCHAR_FORMAT) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6119 | || curwin->w_cursor.lnum != Insstart.lnum |
| 6120 | || curwin->w_cursor.col >= Insstart.col) |
| 6121 | { |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6122 | if (curwin->w_cursor.col == startcol && c != NUL) |
| 6123 | cc = c; |
| 6124 | else |
| 6125 | cc = gchar_cursor(); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6126 | if (WHITECHAR(cc)) |
| 6127 | { |
| 6128 | /* remember position of blank just before text */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6129 | end_col = curwin->w_cursor.col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6130 | |
| 6131 | /* find start of sequence of blanks */ |
| 6132 | while (curwin->w_cursor.col > 0 && WHITECHAR(cc)) |
| 6133 | { |
| 6134 | dec_cursor(); |
| 6135 | cc = gchar_cursor(); |
| 6136 | } |
| 6137 | if (curwin->w_cursor.col == 0 && WHITECHAR(cc)) |
| 6138 | break; /* only spaces in front of text */ |
| 6139 | #ifdef FEAT_COMMENTS |
| 6140 | /* Don't break until after the comment leader */ |
| 6141 | if (curwin->w_cursor.col < leader_len) |
| 6142 | break; |
| 6143 | #endif |
| 6144 | if (has_format_option(FO_ONE_LETTER)) |
| 6145 | { |
| 6146 | /* do not break after one-letter words */ |
| 6147 | if (curwin->w_cursor.col == 0) |
| 6148 | break; /* one-letter word at begin */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6149 | #ifdef FEAT_COMMENTS |
| 6150 | /* do not break "#a b" when 'tw' is 2 */ |
| 6151 | if (curwin->w_cursor.col <= leader_len) |
| 6152 | break; |
| 6153 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6154 | col = curwin->w_cursor.col; |
| 6155 | dec_cursor(); |
| 6156 | cc = gchar_cursor(); |
| 6157 | |
| 6158 | if (WHITECHAR(cc)) |
| 6159 | continue; /* one-letter, continue */ |
| 6160 | curwin->w_cursor.col = col; |
| 6161 | } |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6162 | |
| 6163 | inc_cursor(); |
| 6164 | |
| 6165 | end_foundcol = end_col + 1; |
| 6166 | foundcol = curwin->w_cursor.col; |
| 6167 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6168 | break; |
| 6169 | } |
| 6170 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6171 | else if (cc >= 0x100 && fo_multibyte) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6172 | { |
| 6173 | /* Break after or before a multi-byte character. */ |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6174 | if (curwin->w_cursor.col != startcol) |
| 6175 | { |
| 6176 | #ifdef FEAT_COMMENTS |
| 6177 | /* Don't break until after the comment leader */ |
| 6178 | if (curwin->w_cursor.col < leader_len) |
| 6179 | break; |
| 6180 | #endif |
| 6181 | col = curwin->w_cursor.col; |
| 6182 | inc_cursor(); |
| 6183 | /* Don't change end_foundcol if already set. */ |
| 6184 | if (foundcol != curwin->w_cursor.col) |
| 6185 | { |
| 6186 | foundcol = curwin->w_cursor.col; |
| 6187 | end_foundcol = foundcol; |
| 6188 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
| 6189 | break; |
| 6190 | } |
| 6191 | curwin->w_cursor.col = col; |
| 6192 | } |
| 6193 | |
| 6194 | if (curwin->w_cursor.col == 0) |
| 6195 | break; |
| 6196 | |
| 6197 | col = curwin->w_cursor.col; |
| 6198 | |
| 6199 | dec_cursor(); |
| 6200 | cc = gchar_cursor(); |
| 6201 | |
| 6202 | if (WHITECHAR(cc)) |
| 6203 | continue; /* break with space */ |
| 6204 | #ifdef FEAT_COMMENTS |
| 6205 | /* Don't break until after the comment leader */ |
| 6206 | if (curwin->w_cursor.col < leader_len) |
| 6207 | break; |
| 6208 | #endif |
| 6209 | |
| 6210 | curwin->w_cursor.col = col; |
| 6211 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6212 | foundcol = curwin->w_cursor.col; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6213 | end_foundcol = foundcol; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6214 | if (curwin->w_cursor.col <= (colnr_T)wantcol) |
| 6215 | break; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6216 | } |
| 6217 | #endif |
| 6218 | if (curwin->w_cursor.col == 0) |
| 6219 | break; |
| 6220 | dec_cursor(); |
| 6221 | } |
| 6222 | |
| 6223 | if (foundcol == 0) /* no spaces, cannot break line */ |
| 6224 | { |
| 6225 | curwin->w_cursor.col = startcol; |
| 6226 | break; |
| 6227 | } |
| 6228 | |
| 6229 | /* Going to break the line, remove any "$" now. */ |
| 6230 | undisplay_dollar(); |
| 6231 | |
| 6232 | /* |
| 6233 | * Offset between cursor position and line break is used by replace |
| 6234 | * stack functions. VREPLACE does not use this, and backspaces |
| 6235 | * over the text instead. |
| 6236 | */ |
| 6237 | #ifdef FEAT_VREPLACE |
| 6238 | if (State & VREPLACE_FLAG) |
| 6239 | orig_col = startcol; /* Will start backspacing from here */ |
| 6240 | else |
| 6241 | #endif |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6242 | replace_offset = startcol - end_foundcol; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6243 | |
| 6244 | /* |
| 6245 | * adjust startcol for spaces that will be deleted and |
| 6246 | * characters that will remain on top line |
| 6247 | */ |
| 6248 | curwin->w_cursor.col = foundcol; |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 6249 | while ((cc = gchar_cursor(), WHITECHAR(cc)) |
| 6250 | && (!fo_white_par || curwin->w_cursor.col < startcol)) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6251 | inc_cursor(); |
| 6252 | startcol -= curwin->w_cursor.col; |
| 6253 | if (startcol < 0) |
| 6254 | startcol = 0; |
| 6255 | |
| 6256 | #ifdef FEAT_VREPLACE |
| 6257 | if (State & VREPLACE_FLAG) |
| 6258 | { |
| 6259 | /* |
| 6260 | * In VREPLACE mode, we will backspace over the text to be |
| 6261 | * wrapped, so save a copy now to put on the next line. |
| 6262 | */ |
| 6263 | saved_text = vim_strsave(ml_get_cursor()); |
| 6264 | curwin->w_cursor.col = orig_col; |
| 6265 | if (saved_text == NULL) |
| 6266 | break; /* Can't do it, out of memory */ |
| 6267 | saved_text[startcol] = NUL; |
| 6268 | |
| 6269 | /* Backspace over characters that will move to the next line */ |
| 6270 | if (!fo_white_par) |
| 6271 | backspace_until_column(foundcol); |
| 6272 | } |
| 6273 | else |
| 6274 | #endif |
| 6275 | { |
| 6276 | /* put cursor after pos. to break line */ |
| 6277 | if (!fo_white_par) |
| 6278 | curwin->w_cursor.col = foundcol; |
| 6279 | } |
| 6280 | |
| 6281 | /* |
| 6282 | * Split the line just before the margin. |
| 6283 | * Only insert/delete lines, but don't really redraw the window. |
| 6284 | */ |
| 6285 | open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX |
| 6286 | + (fo_white_par ? OPENLINE_KEEPTRAIL : 0) |
| 6287 | #ifdef FEAT_COMMENTS |
| 6288 | + (do_comments ? OPENLINE_DO_COM : 0) |
| 6289 | #endif |
| 6290 | , old_indent); |
| 6291 | old_indent = 0; |
| 6292 | |
| 6293 | replace_offset = 0; |
| 6294 | if (first_line) |
| 6295 | { |
| 6296 | if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) |
| 6297 | second_indent = get_number_indent(curwin->w_cursor.lnum -1); |
| 6298 | if (second_indent >= 0) |
| 6299 | { |
| 6300 | #ifdef FEAT_VREPLACE |
| 6301 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 6302 | change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6303 | else |
| 6304 | #endif |
| 6305 | (void)set_indent(second_indent, SIN_CHANGED); |
| 6306 | } |
| 6307 | first_line = FALSE; |
| 6308 | } |
| 6309 | |
| 6310 | #ifdef FEAT_VREPLACE |
| 6311 | if (State & VREPLACE_FLAG) |
| 6312 | { |
| 6313 | /* |
| 6314 | * In VREPLACE mode we have backspaced over the text to be |
| 6315 | * moved, now we re-insert it into the new line. |
| 6316 | */ |
| 6317 | ins_bytes(saved_text); |
| 6318 | vim_free(saved_text); |
| 6319 | } |
| 6320 | else |
| 6321 | #endif |
| 6322 | { |
| 6323 | /* |
| 6324 | * Check if cursor is not past the NUL off the line, cindent |
| 6325 | * may have added or removed indent. |
| 6326 | */ |
| 6327 | curwin->w_cursor.col += startcol; |
| 6328 | len = (colnr_T)STRLEN(ml_get_curline()); |
| 6329 | if (curwin->w_cursor.col > len) |
| 6330 | curwin->w_cursor.col = len; |
| 6331 | } |
| 6332 | |
| 6333 | haveto_redraw = TRUE; |
| 6334 | #ifdef FEAT_CINDENT |
| 6335 | can_cindent = TRUE; |
| 6336 | #endif |
| 6337 | /* moved the cursor, don't autoindent or cindent now */ |
| 6338 | did_ai = FALSE; |
| 6339 | #ifdef FEAT_SMARTINDENT |
| 6340 | did_si = FALSE; |
| 6341 | can_si = FALSE; |
| 6342 | can_si_back = FALSE; |
| 6343 | #endif |
| 6344 | line_breakcheck(); |
| 6345 | } |
| 6346 | |
| 6347 | if (save_char != NUL) /* put back space after cursor */ |
| 6348 | pchar_cursor(save_char); |
| 6349 | |
| 6350 | if (!format_only && haveto_redraw) |
| 6351 | { |
| 6352 | update_topline(); |
| 6353 | redraw_curbuf_later(VALID); |
| 6354 | } |
| 6355 | } |
| 6356 | |
| 6357 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6358 | * Called after inserting or deleting text: When 'formatoptions' includes the |
| 6359 | * 'a' flag format from the current line until the end of the paragraph. |
| 6360 | * Keep the cursor at the same position relative to the text. |
| 6361 | * The caller must have saved the cursor line for undo, following ones will be |
| 6362 | * saved here. |
| 6363 | */ |
| 6364 | void |
| 6365 | auto_format(trailblank, prev_line) |
| 6366 | int trailblank; /* when TRUE also format with trailing blank */ |
| 6367 | int prev_line; /* may start in previous line */ |
| 6368 | { |
| 6369 | pos_T pos; |
| 6370 | colnr_T len; |
| 6371 | char_u *old; |
| 6372 | char_u *new, *pnew; |
| 6373 | int wasatend; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6374 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6375 | |
| 6376 | if (!has_format_option(FO_AUTO)) |
| 6377 | return; |
| 6378 | |
| 6379 | pos = curwin->w_cursor; |
| 6380 | old = ml_get_curline(); |
| 6381 | |
| 6382 | /* may remove added space */ |
| 6383 | check_auto_format(FALSE); |
| 6384 | |
| 6385 | /* Don't format in Insert mode when the cursor is on a trailing blank, the |
| 6386 | * user might insert normal text next. Also skip formatting when "1" is |
| 6387 | * in 'formatoptions' and there is a single character before the cursor. |
| 6388 | * Otherwise the line would be broken and when typing another non-white |
| 6389 | * next they are not joined back together. */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6390 | wasatend = (pos.col == (colnr_T)STRLEN(old)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6391 | if (*old != NUL && !trailblank && wasatend) |
| 6392 | { |
| 6393 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6394 | cc = gchar_cursor(); |
| 6395 | if (!WHITECHAR(cc) && curwin->w_cursor.col > 0 |
| 6396 | && has_format_option(FO_ONE_LETTER)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6397 | dec_cursor(); |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6398 | cc = gchar_cursor(); |
| 6399 | if (WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6400 | { |
| 6401 | curwin->w_cursor = pos; |
| 6402 | return; |
| 6403 | } |
| 6404 | curwin->w_cursor = pos; |
| 6405 | } |
| 6406 | |
| 6407 | #ifdef FEAT_COMMENTS |
| 6408 | /* With the 'c' flag in 'formatoptions' and 't' missing: only format |
| 6409 | * comments. */ |
| 6410 | if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP) |
| 6411 | && get_leader_len(old, NULL, FALSE) == 0) |
| 6412 | return; |
| 6413 | #endif |
| 6414 | |
| 6415 | /* |
| 6416 | * May start formatting in a previous line, so that after "x" a word is |
| 6417 | * moved to the previous line if it fits there now. Only when this is not |
| 6418 | * the start of a paragraph. |
| 6419 | */ |
| 6420 | if (prev_line && !paragraph_start(curwin->w_cursor.lnum)) |
| 6421 | { |
| 6422 | --curwin->w_cursor.lnum; |
| 6423 | if (u_save_cursor() == FAIL) |
| 6424 | return; |
| 6425 | } |
| 6426 | |
| 6427 | /* |
| 6428 | * Do the formatting and restore the cursor position. "saved_cursor" will |
| 6429 | * be adjusted for the text formatting. |
| 6430 | */ |
| 6431 | saved_cursor = pos; |
Bram Moolenaar | 81a8209 | 2008-03-12 16:27:00 +0000 | [diff] [blame] | 6432 | format_lines((linenr_T)-1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6433 | curwin->w_cursor = saved_cursor; |
| 6434 | saved_cursor.lnum = 0; |
| 6435 | |
| 6436 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 6437 | { |
| 6438 | /* "cannot happen" */ |
| 6439 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6440 | coladvance((colnr_T)MAXCOL); |
| 6441 | } |
| 6442 | else |
| 6443 | check_cursor_col(); |
| 6444 | |
| 6445 | /* Insert mode: If the cursor is now after the end of the line while it |
| 6446 | * previously wasn't, the line was broken. Because of the rule above we |
| 6447 | * need to add a space when 'w' is in 'formatoptions' to keep a paragraph |
| 6448 | * formatted. */ |
| 6449 | if (!wasatend && has_format_option(FO_WHITE_PAR)) |
| 6450 | { |
| 6451 | new = ml_get_curline(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6452 | len = (colnr_T)STRLEN(new); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6453 | if (curwin->w_cursor.col == len) |
| 6454 | { |
| 6455 | pnew = vim_strnsave(new, len + 2); |
| 6456 | pnew[len] = ' '; |
| 6457 | pnew[len + 1] = NUL; |
| 6458 | ml_replace(curwin->w_cursor.lnum, pnew, FALSE); |
| 6459 | /* remove the space later */ |
| 6460 | did_add_space = TRUE; |
| 6461 | } |
| 6462 | else |
| 6463 | /* may remove added space */ |
| 6464 | check_auto_format(FALSE); |
| 6465 | } |
| 6466 | |
| 6467 | check_cursor(); |
| 6468 | } |
| 6469 | |
| 6470 | /* |
| 6471 | * When an extra space was added to continue a paragraph for auto-formatting, |
| 6472 | * delete it now. The space must be under the cursor, just after the insert |
| 6473 | * position. |
| 6474 | */ |
| 6475 | static void |
| 6476 | check_auto_format(end_insert) |
| 6477 | int end_insert; /* TRUE when ending Insert mode */ |
| 6478 | { |
| 6479 | int c = ' '; |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6480 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6481 | |
| 6482 | if (did_add_space) |
| 6483 | { |
Bram Moolenaar | 75c50c4 | 2005-06-04 22:06:24 +0000 | [diff] [blame] | 6484 | cc = gchar_cursor(); |
| 6485 | if (!WHITECHAR(cc)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6486 | /* Somehow the space was removed already. */ |
| 6487 | did_add_space = FALSE; |
| 6488 | else |
| 6489 | { |
| 6490 | if (!end_insert) |
| 6491 | { |
| 6492 | inc_cursor(); |
| 6493 | c = gchar_cursor(); |
| 6494 | dec_cursor(); |
| 6495 | } |
| 6496 | if (c != NUL) |
| 6497 | { |
| 6498 | /* The space is no longer at the end of the line, delete it. */ |
| 6499 | del_char(FALSE); |
| 6500 | did_add_space = FALSE; |
| 6501 | } |
| 6502 | } |
| 6503 | } |
| 6504 | } |
| 6505 | |
| 6506 | /* |
| 6507 | * Find out textwidth to be used for formatting: |
| 6508 | * if 'textwidth' option is set, use it |
| 6509 | * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin' |
| 6510 | * if invalid value, use 0. |
| 6511 | * Set default to window width (maximum 79) for "gq" operator. |
| 6512 | */ |
| 6513 | int |
| 6514 | comp_textwidth(ff) |
Bram Moolenaar | 91170f8 | 2006-05-05 21:15:17 +0000 | [diff] [blame] | 6515 | int ff; /* force formatting (for "gq" command) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6516 | { |
| 6517 | int textwidth; |
| 6518 | |
| 6519 | textwidth = curbuf->b_p_tw; |
| 6520 | if (textwidth == 0 && curbuf->b_p_wm) |
| 6521 | { |
| 6522 | /* The width is the window width minus 'wrapmargin' minus all the |
| 6523 | * things that add to the margin. */ |
| 6524 | textwidth = W_WIDTH(curwin) - curbuf->b_p_wm; |
| 6525 | #ifdef FEAT_CMDWIN |
| 6526 | if (cmdwin_type != 0) |
| 6527 | textwidth -= 1; |
| 6528 | #endif |
| 6529 | #ifdef FEAT_FOLDING |
| 6530 | textwidth -= curwin->w_p_fdc; |
| 6531 | #endif |
| 6532 | #ifdef FEAT_SIGNS |
| 6533 | if (curwin->w_buffer->b_signlist != NULL |
| 6534 | # ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 6535 | || netbeans_active() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6536 | # endif |
| 6537 | ) |
| 6538 | textwidth -= 1; |
| 6539 | #endif |
Bram Moolenaar | 6448667 | 2010-05-16 15:46:46 +0200 | [diff] [blame] | 6540 | if (curwin->w_p_nu || curwin->w_p_rnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6541 | textwidth -= 8; |
| 6542 | } |
| 6543 | if (textwidth < 0) |
| 6544 | textwidth = 0; |
| 6545 | if (ff && textwidth == 0) |
| 6546 | { |
| 6547 | textwidth = W_WIDTH(curwin) - 1; |
| 6548 | if (textwidth > 79) |
| 6549 | textwidth = 79; |
| 6550 | } |
| 6551 | return textwidth; |
| 6552 | } |
| 6553 | |
| 6554 | /* |
| 6555 | * Put a character in the redo buffer, for when just after a CTRL-V. |
| 6556 | */ |
| 6557 | static void |
| 6558 | redo_literal(c) |
| 6559 | int c; |
| 6560 | { |
| 6561 | char_u buf[10]; |
| 6562 | |
| 6563 | /* Only digits need special treatment. Translate them into a string of |
| 6564 | * three digits. */ |
| 6565 | if (VIM_ISDIGIT(c)) |
| 6566 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6567 | vim_snprintf((char *)buf, sizeof(buf), "%03d", c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | AppendToRedobuff(buf); |
| 6569 | } |
| 6570 | else |
| 6571 | AppendCharToRedobuff(c); |
| 6572 | } |
| 6573 | |
| 6574 | /* |
| 6575 | * 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] | 6576 | * For undo/redo it resembles hitting the <ESC> key. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6577 | */ |
| 6578 | static void |
| 6579 | start_arrow(end_insert_pos) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6580 | pos_T *end_insert_pos; /* can be NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6581 | { |
| 6582 | if (!arrow_used) /* something has been inserted */ |
| 6583 | { |
| 6584 | AppendToRedobuff(ESC_STR); |
| 6585 | stop_insert(end_insert_pos, FALSE); |
| 6586 | arrow_used = TRUE; /* this means we stopped the current insert */ |
| 6587 | } |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6588 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6589 | check_spell_redraw(); |
| 6590 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6591 | } |
| 6592 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 6593 | #ifdef FEAT_SPELL |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6594 | /* |
| 6595 | * If we skipped highlighting word at cursor, do it now. |
| 6596 | * It may be skipped again, thus reset spell_redraw_lnum first. |
| 6597 | */ |
| 6598 | static void |
| 6599 | check_spell_redraw() |
| 6600 | { |
| 6601 | if (spell_redraw_lnum != 0) |
| 6602 | { |
| 6603 | linenr_T lnum = spell_redraw_lnum; |
| 6604 | |
| 6605 | spell_redraw_lnum = 0; |
| 6606 | redrawWinline(lnum, FALSE); |
| 6607 | } |
| 6608 | } |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6609 | |
| 6610 | /* |
| 6611 | * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly |
| 6612 | * spelled word, if there is one. |
| 6613 | */ |
| 6614 | static void |
| 6615 | spell_back_to_badword() |
| 6616 | { |
| 6617 | pos_T tpos = curwin->w_cursor; |
| 6618 | |
Bram Moolenaar | 81f1ecb | 2005-08-25 21:27:31 +0000 | [diff] [blame] | 6619 | spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL); |
Bram Moolenaar | 8aff23a | 2005-08-19 20:40:30 +0000 | [diff] [blame] | 6620 | if (curwin->w_cursor.col != tpos.col) |
| 6621 | start_arrow(&tpos); |
| 6622 | } |
Bram Moolenaar | 217ad92 | 2005-03-20 22:37:15 +0000 | [diff] [blame] | 6623 | #endif |
| 6624 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6625 | /* |
| 6626 | * stop_arrow() is called before a change is made in insert mode. |
| 6627 | * If an arrow key has been used, start a new insertion. |
| 6628 | * Returns FAIL if undo is impossible, shouldn't insert then. |
| 6629 | */ |
| 6630 | int |
| 6631 | stop_arrow() |
| 6632 | { |
| 6633 | if (arrow_used) |
| 6634 | { |
| 6635 | if (u_save_cursor() == OK) |
| 6636 | { |
| 6637 | arrow_used = FALSE; |
| 6638 | ins_need_undo = FALSE; |
| 6639 | } |
| 6640 | Insstart = curwin->w_cursor; /* new insertion starts here */ |
Bram Moolenaar | 0ab2a88 | 2009-05-13 10:51:08 +0000 | [diff] [blame] | 6641 | Insstart_textlen = (colnr_T)linetabsize(ml_get_curline()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6642 | ai_col = 0; |
| 6643 | #ifdef FEAT_VREPLACE |
| 6644 | if (State & VREPLACE_FLAG) |
| 6645 | { |
| 6646 | orig_line_count = curbuf->b_ml.ml_line_count; |
| 6647 | vr_lines_changed = 1; |
| 6648 | } |
| 6649 | #endif |
| 6650 | ResetRedobuff(); |
| 6651 | AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */ |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 6652 | new_insert_skip = 2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6653 | } |
| 6654 | else if (ins_need_undo) |
| 6655 | { |
| 6656 | if (u_save_cursor() == OK) |
| 6657 | ins_need_undo = FALSE; |
| 6658 | } |
| 6659 | |
| 6660 | #ifdef FEAT_FOLDING |
| 6661 | /* Always open fold at the cursor line when inserting something. */ |
| 6662 | foldOpenCursor(); |
| 6663 | #endif |
| 6664 | |
| 6665 | return (arrow_used || ins_need_undo ? FAIL : OK); |
| 6666 | } |
| 6667 | |
| 6668 | /* |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6669 | * Do a few things to stop inserting. |
| 6670 | * "end_insert_pos" is where insert ended. It is NULL when we already jumped |
| 6671 | * to another window/buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6672 | */ |
| 6673 | static void |
| 6674 | stop_insert(end_insert_pos, esc) |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6675 | pos_T *end_insert_pos; |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6676 | int esc; /* called by ins_esc() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6677 | { |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6678 | int cc; |
| 6679 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6680 | |
| 6681 | stop_redo_ins(); |
| 6682 | replace_flush(); /* abandon replace stack */ |
| 6683 | |
| 6684 | /* |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6685 | * Save the inserted text for later redo with ^@ and CTRL-A. |
| 6686 | * Don't do it when "restart_edit" was set and nothing was inserted, |
| 6687 | * otherwise CTRL-O w and then <Left> will clear "last_insert". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6688 | */ |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6689 | ptr = get_inserted(); |
Bram Moolenaar | f4cd3e8 | 2005-12-22 22:47:02 +0000 | [diff] [blame] | 6690 | if (did_restart_edit == 0 || (ptr != NULL |
| 6691 | && (int)STRLEN(ptr) > new_insert_skip)) |
Bram Moolenaar | 83c465c | 2005-12-16 21:53:56 +0000 | [diff] [blame] | 6692 | { |
| 6693 | vim_free(last_insert); |
| 6694 | last_insert = ptr; |
| 6695 | last_insert_skip = new_insert_skip; |
| 6696 | } |
| 6697 | else |
| 6698 | vim_free(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6699 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6700 | if (!arrow_used && end_insert_pos != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6701 | { |
| 6702 | /* Auto-format now. It may seem strange to do this when stopping an |
| 6703 | * insertion (or moving the cursor), but it's required when appending |
| 6704 | * a line and having it end in a space. But only do it when something |
| 6705 | * was actually inserted, otherwise undo won't work. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6706 | if (!ins_need_undo && has_format_option(FO_AUTO)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6707 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6708 | pos_T tpos = curwin->w_cursor; |
| 6709 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6710 | /* When the cursor is at the end of the line after a space the |
| 6711 | * formatting will move it to the following word. Avoid that by |
| 6712 | * moving the cursor onto the space. */ |
| 6713 | cc = 'x'; |
| 6714 | if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL) |
| 6715 | { |
| 6716 | dec_cursor(); |
| 6717 | cc = gchar_cursor(); |
| 6718 | if (!vim_iswhite(cc)) |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6719 | curwin->w_cursor = tpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6720 | } |
| 6721 | |
| 6722 | auto_format(TRUE, FALSE); |
| 6723 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6724 | if (vim_iswhite(cc)) |
| 6725 | { |
| 6726 | if (gchar_cursor() != NUL) |
| 6727 | inc_cursor(); |
| 6728 | #ifdef FEAT_VIRTUALEDIT |
| 6729 | /* If the cursor is still at the same character, also keep |
| 6730 | * the "coladd". */ |
| 6731 | if (gchar_cursor() == NUL |
| 6732 | && curwin->w_cursor.lnum == tpos.lnum |
| 6733 | && curwin->w_cursor.col == tpos.col) |
| 6734 | curwin->w_cursor.coladd = tpos.coladd; |
| 6735 | #endif |
| 6736 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6737 | } |
| 6738 | |
| 6739 | /* If a space was inserted for auto-formatting, remove it now. */ |
| 6740 | check_auto_format(TRUE); |
| 6741 | |
| 6742 | /* 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] | 6743 | * of the line, and put the cursor back. |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6744 | * Do this when ESC was used or moving the cursor up/down. |
| 6745 | * Check for the old position still being valid, just in case the text |
| 6746 | * got changed unexpectedly. */ |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6747 | if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6748 | && curwin->w_cursor.lnum != end_insert_pos->lnum)) |
| 6749 | && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6750 | { |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6751 | pos_T tpos = curwin->w_cursor; |
| 6752 | |
| 6753 | curwin->w_cursor = *end_insert_pos; |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6754 | check_cursor_col(); /* make sure it is not past the line */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6755 | for (;;) |
| 6756 | { |
| 6757 | if (gchar_cursor() == NUL && curwin->w_cursor.col > 0) |
| 6758 | --curwin->w_cursor.col; |
| 6759 | cc = gchar_cursor(); |
| 6760 | if (!vim_iswhite(cc)) |
| 6761 | break; |
Bram Moolenaar | 4be5068 | 2009-05-26 09:02:10 +0000 | [diff] [blame] | 6762 | if (del_char(TRUE) == FAIL) |
| 6763 | break; /* should not happen */ |
Bram Moolenaar | 39f0563 | 2006-03-19 22:15:26 +0000 | [diff] [blame] | 6764 | } |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6765 | if (curwin->w_cursor.lnum != tpos.lnum) |
| 6766 | curwin->w_cursor = tpos; |
| 6767 | else if (cc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6768 | ++curwin->w_cursor.col; /* put cursor back on the NUL */ |
| 6769 | |
| 6770 | #ifdef FEAT_VISUAL |
| 6771 | /* <C-S-Right> may have started Visual mode, adjust the position for |
| 6772 | * deleted characters. */ |
| 6773 | if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum) |
| 6774 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6775 | int len = (int)STRLEN(ml_get_curline()); |
| 6776 | |
| 6777 | if (VIsual.col > len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6778 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 6779 | VIsual.col = len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6780 | # ifdef FEAT_VIRTUALEDIT |
| 6781 | VIsual.coladd = 0; |
| 6782 | # endif |
| 6783 | } |
| 6784 | } |
| 6785 | #endif |
| 6786 | } |
| 6787 | } |
| 6788 | did_ai = FALSE; |
| 6789 | #ifdef FEAT_SMARTINDENT |
| 6790 | did_si = FALSE; |
| 6791 | can_si = FALSE; |
| 6792 | can_si_back = FALSE; |
| 6793 | #endif |
| 6794 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 6795 | /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are |
| 6796 | * now in a different buffer. */ |
| 6797 | if (end_insert_pos != NULL) |
| 6798 | { |
| 6799 | curbuf->b_op_start = Insstart; |
| 6800 | curbuf->b_op_end = *end_insert_pos; |
| 6801 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6802 | } |
| 6803 | |
| 6804 | /* |
| 6805 | * Set the last inserted text to a single character. |
| 6806 | * Used for the replace command. |
| 6807 | */ |
| 6808 | void |
| 6809 | set_last_insert(c) |
| 6810 | int c; |
| 6811 | { |
| 6812 | char_u *s; |
| 6813 | |
| 6814 | vim_free(last_insert); |
| 6815 | #ifdef FEAT_MBYTE |
| 6816 | last_insert = alloc(MB_MAXBYTES * 3 + 5); |
| 6817 | #else |
| 6818 | last_insert = alloc(6); |
| 6819 | #endif |
| 6820 | if (last_insert != NULL) |
| 6821 | { |
| 6822 | s = last_insert; |
| 6823 | /* Use the CTRL-V only when entering a special char */ |
| 6824 | if (c < ' ' || c == DEL) |
| 6825 | *s++ = Ctrl_V; |
| 6826 | s = add_char2buf(c, s); |
| 6827 | *s++ = ESC; |
| 6828 | *s++ = NUL; |
| 6829 | last_insert_skip = 0; |
| 6830 | } |
| 6831 | } |
| 6832 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6833 | #if defined(EXITFREE) || defined(PROTO) |
| 6834 | void |
| 6835 | free_last_insert() |
| 6836 | { |
| 6837 | vim_free(last_insert); |
| 6838 | last_insert = NULL; |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 6839 | # ifdef FEAT_INS_EXPAND |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 6840 | vim_free(compl_orig_text); |
| 6841 | compl_orig_text = NULL; |
Bram Moolenaar | e0ca7b2 | 2007-11-24 20:28:24 +0000 | [diff] [blame] | 6842 | # endif |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 6843 | } |
| 6844 | #endif |
| 6845 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6846 | /* |
| 6847 | * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL |
| 6848 | * and CSI. Handle multi-byte characters. |
| 6849 | * Returns a pointer to after the added bytes. |
| 6850 | */ |
| 6851 | char_u * |
| 6852 | add_char2buf(c, s) |
| 6853 | int c; |
| 6854 | char_u *s; |
| 6855 | { |
| 6856 | #ifdef FEAT_MBYTE |
| 6857 | char_u temp[MB_MAXBYTES]; |
| 6858 | int i; |
| 6859 | int len; |
| 6860 | |
| 6861 | len = (*mb_char2bytes)(c, temp); |
| 6862 | for (i = 0; i < len; ++i) |
| 6863 | { |
| 6864 | c = temp[i]; |
| 6865 | #endif |
| 6866 | /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */ |
| 6867 | if (c == K_SPECIAL) |
| 6868 | { |
| 6869 | *s++ = K_SPECIAL; |
| 6870 | *s++ = KS_SPECIAL; |
| 6871 | *s++ = KE_FILLER; |
| 6872 | } |
| 6873 | #ifdef FEAT_GUI |
| 6874 | else if (c == CSI) |
| 6875 | { |
| 6876 | *s++ = CSI; |
| 6877 | *s++ = KS_EXTRA; |
| 6878 | *s++ = (int)KE_CSI; |
| 6879 | } |
| 6880 | #endif |
| 6881 | else |
| 6882 | *s++ = c; |
| 6883 | #ifdef FEAT_MBYTE |
| 6884 | } |
| 6885 | #endif |
| 6886 | return s; |
| 6887 | } |
| 6888 | |
| 6889 | /* |
| 6890 | * move cursor to start of line |
| 6891 | * if flags & BL_WHITE move to first non-white |
| 6892 | * if flags & BL_SOL move to first non-white if startofline is set, |
| 6893 | * otherwise keep "curswant" column |
| 6894 | * if flags & BL_FIX don't leave the cursor on a NUL. |
| 6895 | */ |
| 6896 | void |
| 6897 | beginline(flags) |
| 6898 | int flags; |
| 6899 | { |
| 6900 | if ((flags & BL_SOL) && !p_sol) |
| 6901 | coladvance(curwin->w_curswant); |
| 6902 | else |
| 6903 | { |
| 6904 | curwin->w_cursor.col = 0; |
| 6905 | #ifdef FEAT_VIRTUALEDIT |
| 6906 | curwin->w_cursor.coladd = 0; |
| 6907 | #endif |
| 6908 | |
| 6909 | if (flags & (BL_WHITE | BL_SOL)) |
| 6910 | { |
| 6911 | char_u *ptr; |
| 6912 | |
| 6913 | for (ptr = ml_get_curline(); vim_iswhite(*ptr) |
| 6914 | && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr) |
| 6915 | ++curwin->w_cursor.col; |
| 6916 | } |
| 6917 | curwin->w_set_curswant = TRUE; |
| 6918 | } |
| 6919 | } |
| 6920 | |
| 6921 | /* |
| 6922 | * oneright oneleft cursor_down cursor_up |
| 6923 | * |
| 6924 | * Move one char {right,left,down,up}. |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6925 | * 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] | 6926 | * Return OK when successful, FAIL when we hit a line of file boundary. |
| 6927 | */ |
| 6928 | |
| 6929 | int |
| 6930 | oneright() |
| 6931 | { |
| 6932 | char_u *ptr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6933 | int l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6934 | |
| 6935 | #ifdef FEAT_VIRTUALEDIT |
| 6936 | if (virtual_active()) |
| 6937 | { |
| 6938 | pos_T prevpos = curwin->w_cursor; |
| 6939 | |
| 6940 | /* Adjust for multi-wide char (excluding TAB) */ |
| 6941 | ptr = ml_get_cursor(); |
| 6942 | coladvance(getviscol() + ((*ptr != TAB && vim_isprintc( |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6943 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6944 | (*mb_ptr2char)(ptr) |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6945 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6946 | *ptr |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6947 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6948 | )) |
| 6949 | ? ptr2cells(ptr) : 1)); |
| 6950 | curwin->w_set_curswant = TRUE; |
| 6951 | /* Return OK if the cursor moved, FAIL otherwise (at window edge). */ |
| 6952 | return (prevpos.col != curwin->w_cursor.col |
| 6953 | || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL; |
| 6954 | } |
| 6955 | #endif |
| 6956 | |
| 6957 | ptr = ml_get_cursor(); |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6958 | if (*ptr == NUL) |
| 6959 | return FAIL; /* already at the very end */ |
| 6960 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6961 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6962 | if (has_mbyte) |
| 6963 | l = (*mb_ptr2len)(ptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6964 | else |
| 6965 | #endif |
Bram Moolenaar | 2eb25da | 2006-03-16 21:43:34 +0000 | [diff] [blame] | 6966 | l = 1; |
| 6967 | |
| 6968 | /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit' |
| 6969 | * contains "onemore". */ |
| 6970 | if (ptr[l] == NUL |
| 6971 | #ifdef FEAT_VIRTUALEDIT |
| 6972 | && (ve_flags & VE_ONEMORE) == 0 |
| 6973 | #endif |
| 6974 | ) |
| 6975 | return FAIL; |
| 6976 | curwin->w_cursor.col += l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6977 | |
| 6978 | curwin->w_set_curswant = TRUE; |
| 6979 | return OK; |
| 6980 | } |
| 6981 | |
| 6982 | int |
| 6983 | oneleft() |
| 6984 | { |
| 6985 | #ifdef FEAT_VIRTUALEDIT |
| 6986 | if (virtual_active()) |
| 6987 | { |
| 6988 | int width; |
| 6989 | int v = getviscol(); |
| 6990 | |
| 6991 | if (v == 0) |
| 6992 | return FAIL; |
| 6993 | |
| 6994 | # ifdef FEAT_LINEBREAK |
| 6995 | /* We might get stuck on 'showbreak', skip over it. */ |
| 6996 | width = 1; |
| 6997 | for (;;) |
| 6998 | { |
| 6999 | coladvance(v - width); |
| 7000 | /* getviscol() is slow, skip it when 'showbreak' is empty and |
| 7001 | * there are no multi-byte characters */ |
| 7002 | if ((*p_sbr == NUL |
| 7003 | # ifdef FEAT_MBYTE |
| 7004 | && !has_mbyte |
| 7005 | # endif |
| 7006 | ) || getviscol() < v) |
| 7007 | break; |
| 7008 | ++width; |
| 7009 | } |
| 7010 | # else |
| 7011 | coladvance(v - 1); |
| 7012 | # endif |
| 7013 | |
| 7014 | if (curwin->w_cursor.coladd == 1) |
| 7015 | { |
| 7016 | char_u *ptr; |
| 7017 | |
| 7018 | /* Adjust for multi-wide char (not a TAB) */ |
| 7019 | ptr = ml_get_cursor(); |
| 7020 | if (*ptr != TAB && vim_isprintc( |
| 7021 | # ifdef FEAT_MBYTE |
| 7022 | (*mb_ptr2char)(ptr) |
| 7023 | # else |
| 7024 | *ptr |
| 7025 | # endif |
| 7026 | ) && ptr2cells(ptr) > 1) |
| 7027 | curwin->w_cursor.coladd = 0; |
| 7028 | } |
| 7029 | |
| 7030 | curwin->w_set_curswant = TRUE; |
| 7031 | return OK; |
| 7032 | } |
| 7033 | #endif |
| 7034 | |
| 7035 | if (curwin->w_cursor.col == 0) |
| 7036 | return FAIL; |
| 7037 | |
| 7038 | curwin->w_set_curswant = TRUE; |
| 7039 | --curwin->w_cursor.col; |
| 7040 | |
| 7041 | #ifdef FEAT_MBYTE |
| 7042 | /* if the character on the left of the current cursor is a multi-byte |
| 7043 | * character, move to its first byte */ |
| 7044 | if (has_mbyte) |
| 7045 | mb_adjust_cursor(); |
| 7046 | #endif |
| 7047 | return OK; |
| 7048 | } |
| 7049 | |
| 7050 | int |
| 7051 | cursor_up(n, upd_topline) |
| 7052 | long n; |
| 7053 | int upd_topline; /* When TRUE: update topline */ |
| 7054 | { |
| 7055 | linenr_T lnum; |
| 7056 | |
| 7057 | if (n > 0) |
| 7058 | { |
| 7059 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7060 | /* This fails if the cursor is already in the first line or the count |
| 7061 | * is larger than the line number and '-' is in 'cpoptions' */ |
| 7062 | if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7063 | return FAIL; |
| 7064 | if (n >= lnum) |
| 7065 | lnum = 1; |
| 7066 | else |
| 7067 | #ifdef FEAT_FOLDING |
| 7068 | if (hasAnyFolding(curwin)) |
| 7069 | { |
| 7070 | /* |
| 7071 | * Count each sequence of folded lines as one logical line. |
| 7072 | */ |
| 7073 | /* go to the the start of the current fold */ |
| 7074 | (void)hasFolding(lnum, &lnum, NULL); |
| 7075 | |
| 7076 | while (n--) |
| 7077 | { |
| 7078 | /* move up one line */ |
| 7079 | --lnum; |
| 7080 | if (lnum <= 1) |
| 7081 | break; |
| 7082 | /* If we entered a fold, move to the beginning, unless in |
| 7083 | * Insert mode or when 'foldopen' contains "all": it will open |
| 7084 | * in a moment. */ |
| 7085 | if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL))) |
| 7086 | (void)hasFolding(lnum, &lnum, NULL); |
| 7087 | } |
| 7088 | if (lnum < 1) |
| 7089 | lnum = 1; |
| 7090 | } |
| 7091 | else |
| 7092 | #endif |
| 7093 | lnum -= n; |
| 7094 | curwin->w_cursor.lnum = lnum; |
| 7095 | } |
| 7096 | |
| 7097 | /* try to advance to the column we want to be at */ |
| 7098 | coladvance(curwin->w_curswant); |
| 7099 | |
| 7100 | if (upd_topline) |
| 7101 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 7102 | |
| 7103 | return OK; |
| 7104 | } |
| 7105 | |
| 7106 | /* |
| 7107 | * Cursor down a number of logical lines. |
| 7108 | */ |
| 7109 | int |
| 7110 | cursor_down(n, upd_topline) |
| 7111 | long n; |
| 7112 | int upd_topline; /* When TRUE: update topline */ |
| 7113 | { |
| 7114 | linenr_T lnum; |
| 7115 | |
| 7116 | if (n > 0) |
| 7117 | { |
| 7118 | lnum = curwin->w_cursor.lnum; |
| 7119 | #ifdef FEAT_FOLDING |
| 7120 | /* Move to last line of fold, will fail if it's the end-of-file. */ |
| 7121 | (void)hasFolding(lnum, NULL, &lnum); |
| 7122 | #endif |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 7123 | /* This fails if the cursor is already in the last line or would move |
| 7124 | * beyound the last line and '-' is in 'cpoptions' */ |
| 7125 | if (lnum >= curbuf->b_ml.ml_line_count |
| 7126 | || (lnum + n > curbuf->b_ml.ml_line_count |
| 7127 | && vim_strchr(p_cpo, CPO_MINUS) != NULL)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7128 | return FAIL; |
| 7129 | if (lnum + n >= curbuf->b_ml.ml_line_count) |
| 7130 | lnum = curbuf->b_ml.ml_line_count; |
| 7131 | else |
| 7132 | #ifdef FEAT_FOLDING |
| 7133 | if (hasAnyFolding(curwin)) |
| 7134 | { |
| 7135 | linenr_T last; |
| 7136 | |
| 7137 | /* count each sequence of folded lines as one logical line */ |
| 7138 | while (n--) |
| 7139 | { |
| 7140 | if (hasFolding(lnum, NULL, &last)) |
| 7141 | lnum = last + 1; |
| 7142 | else |
| 7143 | ++lnum; |
| 7144 | if (lnum >= curbuf->b_ml.ml_line_count) |
| 7145 | break; |
| 7146 | } |
| 7147 | if (lnum > curbuf->b_ml.ml_line_count) |
| 7148 | lnum = curbuf->b_ml.ml_line_count; |
| 7149 | } |
| 7150 | else |
| 7151 | #endif |
| 7152 | lnum += n; |
| 7153 | curwin->w_cursor.lnum = lnum; |
| 7154 | } |
| 7155 | |
| 7156 | /* try to advance to the column we want to be at */ |
| 7157 | coladvance(curwin->w_curswant); |
| 7158 | |
| 7159 | if (upd_topline) |
| 7160 | update_topline(); /* make sure curwin->w_topline is valid */ |
| 7161 | |
| 7162 | return OK; |
| 7163 | } |
| 7164 | |
| 7165 | /* |
| 7166 | * Stuff the last inserted text in the read buffer. |
| 7167 | * Last_insert actually is a copy of the redo buffer, so we |
| 7168 | * first have to remove the command. |
| 7169 | */ |
| 7170 | int |
| 7171 | stuff_inserted(c, count, no_esc) |
| 7172 | int c; /* Command character to be inserted */ |
| 7173 | long count; /* Repeat this many times */ |
| 7174 | int no_esc; /* Don't add an ESC at the end */ |
| 7175 | { |
| 7176 | char_u *esc_ptr; |
| 7177 | char_u *ptr; |
| 7178 | char_u *last_ptr; |
| 7179 | char_u last = NUL; |
| 7180 | |
| 7181 | ptr = get_last_insert(); |
| 7182 | if (ptr == NULL) |
| 7183 | { |
| 7184 | EMSG(_(e_noinstext)); |
| 7185 | return FAIL; |
| 7186 | } |
| 7187 | |
| 7188 | /* may want to stuff the command character, to start Insert mode */ |
| 7189 | if (c != NUL) |
| 7190 | stuffcharReadbuff(c); |
| 7191 | if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL) |
| 7192 | *esc_ptr = NUL; /* remove the ESC */ |
| 7193 | |
| 7194 | /* when the last char is either "0" or "^" it will be quoted if no ESC |
| 7195 | * comes after it OR if it will inserted more than once and "ptr" |
| 7196 | * starts with ^D. -- Acevedo |
| 7197 | */ |
| 7198 | last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1; |
| 7199 | if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^') |
| 7200 | && (no_esc || (*ptr == Ctrl_D && count > 1))) |
| 7201 | { |
| 7202 | last = *last_ptr; |
| 7203 | *last_ptr = NUL; |
| 7204 | } |
| 7205 | |
| 7206 | do |
| 7207 | { |
| 7208 | stuffReadbuff(ptr); |
| 7209 | /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */ |
| 7210 | if (last) |
| 7211 | stuffReadbuff((char_u *)(last == '0' |
| 7212 | ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0") |
| 7213 | : IF_EB("\026^", CTRL_V_STR "^"))); |
| 7214 | } |
| 7215 | while (--count > 0); |
| 7216 | |
| 7217 | if (last) |
| 7218 | *last_ptr = last; |
| 7219 | |
| 7220 | if (esc_ptr != NULL) |
| 7221 | *esc_ptr = ESC; /* put the ESC back */ |
| 7222 | |
| 7223 | /* may want to stuff a trailing ESC, to get out of Insert mode */ |
| 7224 | if (!no_esc) |
| 7225 | stuffcharReadbuff(ESC); |
| 7226 | |
| 7227 | return OK; |
| 7228 | } |
| 7229 | |
| 7230 | char_u * |
| 7231 | get_last_insert() |
| 7232 | { |
| 7233 | if (last_insert == NULL) |
| 7234 | return NULL; |
| 7235 | return last_insert + last_insert_skip; |
| 7236 | } |
| 7237 | |
| 7238 | /* |
| 7239 | * Get last inserted string, and remove trailing <Esc>. |
| 7240 | * Returns pointer to allocated memory (must be freed) or NULL. |
| 7241 | */ |
| 7242 | char_u * |
| 7243 | get_last_insert_save() |
| 7244 | { |
| 7245 | char_u *s; |
| 7246 | int len; |
| 7247 | |
| 7248 | if (last_insert == NULL) |
| 7249 | return NULL; |
| 7250 | s = vim_strsave(last_insert + last_insert_skip); |
| 7251 | if (s != NULL) |
| 7252 | { |
| 7253 | len = (int)STRLEN(s); |
| 7254 | if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */ |
| 7255 | s[len - 1] = NUL; |
| 7256 | } |
| 7257 | return s; |
| 7258 | } |
| 7259 | |
| 7260 | /* |
| 7261 | * Check the word in front of the cursor for an abbreviation. |
| 7262 | * Called when the non-id character "c" has been entered. |
| 7263 | * When an abbreviation is recognized it is removed from the text and |
| 7264 | * the replacement string is inserted in typebuf.tb_buf[], followed by "c". |
| 7265 | */ |
| 7266 | static int |
| 7267 | echeck_abbr(c) |
| 7268 | int c; |
| 7269 | { |
| 7270 | /* Don't check for abbreviation in paste mode, when disabled and just |
| 7271 | * after moving around with cursor keys. */ |
| 7272 | if (p_paste || no_abbr || arrow_used) |
| 7273 | return FALSE; |
| 7274 | |
| 7275 | return check_abbr(c, ml_get_curline(), curwin->w_cursor.col, |
| 7276 | curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0); |
| 7277 | } |
| 7278 | |
| 7279 | /* |
| 7280 | * replace-stack functions |
| 7281 | * |
| 7282 | * When replacing characters, the replaced characters are remembered for each |
| 7283 | * new character. This is used to re-insert the old text when backspacing. |
| 7284 | * |
| 7285 | * There is a NUL headed list of characters for each character that is |
| 7286 | * currently in the file after the insertion point. When BS is used, one NUL |
| 7287 | * headed list is put back for the deleted character. |
| 7288 | * |
| 7289 | * For a newline, there are two NUL headed lists. One contains the characters |
| 7290 | * that the NL replaced. The extra one stores the characters after the cursor |
| 7291 | * that were deleted (always white space). |
| 7292 | * |
| 7293 | * Replace_offset is normally 0, in which case replace_push will add a new |
| 7294 | * character at the end of the stack. If replace_offset is not 0, that many |
| 7295 | * characters will be left on the stack above the newly inserted character. |
| 7296 | */ |
| 7297 | |
Bram Moolenaar | 6c0b44b | 2005-06-01 21:56:33 +0000 | [diff] [blame] | 7298 | static char_u *replace_stack = NULL; |
| 7299 | static long replace_stack_nr = 0; /* next entry in replace stack */ |
| 7300 | static long replace_stack_len = 0; /* max. number of entries */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7301 | |
| 7302 | void |
| 7303 | replace_push(c) |
| 7304 | int c; /* character that is replaced (NUL is none) */ |
| 7305 | { |
| 7306 | char_u *p; |
| 7307 | |
| 7308 | if (replace_stack_nr < replace_offset) /* nothing to do */ |
| 7309 | return; |
| 7310 | if (replace_stack_len <= replace_stack_nr) |
| 7311 | { |
| 7312 | replace_stack_len += 50; |
| 7313 | p = lalloc(sizeof(char_u) * replace_stack_len, TRUE); |
| 7314 | if (p == NULL) /* out of memory */ |
| 7315 | { |
| 7316 | replace_stack_len -= 50; |
| 7317 | return; |
| 7318 | } |
| 7319 | if (replace_stack != NULL) |
| 7320 | { |
| 7321 | mch_memmove(p, replace_stack, |
| 7322 | (size_t)(replace_stack_nr * sizeof(char_u))); |
| 7323 | vim_free(replace_stack); |
| 7324 | } |
| 7325 | replace_stack = p; |
| 7326 | } |
| 7327 | p = replace_stack + replace_stack_nr - replace_offset; |
| 7328 | if (replace_offset) |
| 7329 | mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u))); |
| 7330 | *p = c; |
| 7331 | ++replace_stack_nr; |
| 7332 | } |
| 7333 | |
Bram Moolenaar | 2c994e8 | 2008-01-02 16:49:36 +0000 | [diff] [blame] | 7334 | #if defined(FEAT_MBYTE) || defined(PROTO) |
| 7335 | /* |
| 7336 | * Push a character onto the replace stack. Handles a multi-byte character in |
| 7337 | * reverse byte order, so that the first byte is popped off first. |
| 7338 | * Return the number of bytes done (includes composing characters). |
| 7339 | */ |
| 7340 | int |
| 7341 | replace_push_mb(p) |
| 7342 | char_u *p; |
| 7343 | { |
| 7344 | int l = (*mb_ptr2len)(p); |
| 7345 | int j; |
| 7346 | |
| 7347 | for (j = l - 1; j >= 0; --j) |
| 7348 | replace_push(p[j]); |
| 7349 | return l; |
| 7350 | } |
| 7351 | #endif |
| 7352 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7353 | /* |
| 7354 | * Pop one item from the replace stack. |
| 7355 | * return -1 if stack empty |
| 7356 | * return replaced character or NUL otherwise |
| 7357 | */ |
| 7358 | static int |
| 7359 | replace_pop() |
| 7360 | { |
| 7361 | if (replace_stack_nr == 0) |
| 7362 | return -1; |
| 7363 | return (int)replace_stack[--replace_stack_nr]; |
| 7364 | } |
| 7365 | |
| 7366 | /* |
| 7367 | * Join the top two items on the replace stack. This removes to "off"'th NUL |
| 7368 | * encountered. |
| 7369 | */ |
| 7370 | static void |
| 7371 | replace_join(off) |
| 7372 | int off; /* offset for which NUL to remove */ |
| 7373 | { |
| 7374 | int i; |
| 7375 | |
| 7376 | for (i = replace_stack_nr; --i >= 0; ) |
| 7377 | if (replace_stack[i] == NUL && off-- <= 0) |
| 7378 | { |
| 7379 | --replace_stack_nr; |
| 7380 | mch_memmove(replace_stack + i, replace_stack + i + 1, |
| 7381 | (size_t)(replace_stack_nr - i)); |
| 7382 | return; |
| 7383 | } |
| 7384 | } |
| 7385 | |
| 7386 | /* |
| 7387 | * Pop bytes from the replace stack until a NUL is found, and insert them |
| 7388 | * before the cursor. Can only be used in REPLACE or VREPLACE mode. |
| 7389 | */ |
| 7390 | static void |
| 7391 | replace_pop_ins() |
| 7392 | { |
| 7393 | int cc; |
| 7394 | int oldState = State; |
| 7395 | |
| 7396 | State = NORMAL; /* don't want REPLACE here */ |
| 7397 | while ((cc = replace_pop()) > 0) |
| 7398 | { |
| 7399 | #ifdef FEAT_MBYTE |
| 7400 | mb_replace_pop_ins(cc); |
| 7401 | #else |
| 7402 | ins_char(cc); |
| 7403 | #endif |
| 7404 | dec_cursor(); |
| 7405 | } |
| 7406 | State = oldState; |
| 7407 | } |
| 7408 | |
| 7409 | #ifdef FEAT_MBYTE |
| 7410 | /* |
| 7411 | * Insert bytes popped from the replace stack. "cc" is the first byte. If it |
| 7412 | * indicates a multi-byte char, pop the other bytes too. |
| 7413 | */ |
| 7414 | static void |
| 7415 | mb_replace_pop_ins(cc) |
| 7416 | int cc; |
| 7417 | { |
| 7418 | int n; |
| 7419 | char_u buf[MB_MAXBYTES]; |
| 7420 | int i; |
| 7421 | int c; |
| 7422 | |
| 7423 | if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1) |
| 7424 | { |
| 7425 | buf[0] = cc; |
| 7426 | for (i = 1; i < n; ++i) |
| 7427 | buf[i] = replace_pop(); |
| 7428 | ins_bytes_len(buf, n); |
| 7429 | } |
| 7430 | else |
| 7431 | ins_char(cc); |
| 7432 | |
| 7433 | if (enc_utf8) |
| 7434 | /* Handle composing chars. */ |
| 7435 | for (;;) |
| 7436 | { |
| 7437 | c = replace_pop(); |
| 7438 | if (c == -1) /* stack empty */ |
| 7439 | break; |
| 7440 | if ((n = MB_BYTE2LEN(c)) == 1) |
| 7441 | { |
| 7442 | /* Not a multi-byte char, put it back. */ |
| 7443 | replace_push(c); |
| 7444 | break; |
| 7445 | } |
| 7446 | else |
| 7447 | { |
| 7448 | buf[0] = c; |
| 7449 | for (i = 1; i < n; ++i) |
| 7450 | buf[i] = replace_pop(); |
| 7451 | if (utf_iscomposing(utf_ptr2char(buf))) |
| 7452 | ins_bytes_len(buf, n); |
| 7453 | else |
| 7454 | { |
| 7455 | /* Not a composing char, put it back. */ |
| 7456 | for (i = n - 1; i >= 0; --i) |
| 7457 | replace_push(buf[i]); |
| 7458 | break; |
| 7459 | } |
| 7460 | } |
| 7461 | } |
| 7462 | } |
| 7463 | #endif |
| 7464 | |
| 7465 | /* |
| 7466 | * make the replace stack empty |
| 7467 | * (called when exiting replace mode) |
| 7468 | */ |
| 7469 | static void |
| 7470 | replace_flush() |
| 7471 | { |
| 7472 | vim_free(replace_stack); |
| 7473 | replace_stack = NULL; |
| 7474 | replace_stack_len = 0; |
| 7475 | replace_stack_nr = 0; |
| 7476 | } |
| 7477 | |
| 7478 | /* |
| 7479 | * Handle doing a BS for one character. |
| 7480 | * cc < 0: replace stack empty, just move cursor |
| 7481 | * cc == 0: character was inserted, delete it |
| 7482 | * cc > 0: character was replaced, put cc (first byte of original char) back |
| 7483 | * and check for more characters to be put back |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7484 | * When "limit_col" is >= 0, don't delete before this column. Matters when |
| 7485 | * using composing characters, use del_char_after_col() instead of del_char(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7486 | */ |
| 7487 | static void |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7488 | replace_do_bs(limit_col) |
| 7489 | int limit_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7490 | { |
| 7491 | int cc; |
| 7492 | #ifdef FEAT_VREPLACE |
| 7493 | int orig_len = 0; |
| 7494 | int ins_len; |
| 7495 | int orig_vcols = 0; |
| 7496 | colnr_T start_vcol; |
| 7497 | char_u *p; |
| 7498 | int i; |
| 7499 | int vcol; |
| 7500 | #endif |
| 7501 | |
| 7502 | cc = replace_pop(); |
| 7503 | if (cc > 0) |
| 7504 | { |
| 7505 | #ifdef FEAT_VREPLACE |
| 7506 | if (State & VREPLACE_FLAG) |
| 7507 | { |
| 7508 | /* Get the number of screen cells used by the character we are |
| 7509 | * going to delete. */ |
| 7510 | getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL); |
| 7511 | orig_vcols = chartabsize(ml_get_cursor(), start_vcol); |
| 7512 | } |
| 7513 | #endif |
| 7514 | #ifdef FEAT_MBYTE |
| 7515 | if (has_mbyte) |
| 7516 | { |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7517 | (void)del_char_after_col(limit_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | # ifdef FEAT_VREPLACE |
| 7519 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7520 | orig_len = (int)STRLEN(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7521 | # endif |
| 7522 | replace_push(cc); |
| 7523 | } |
| 7524 | else |
| 7525 | #endif |
| 7526 | { |
| 7527 | pchar_cursor(cc); |
| 7528 | #ifdef FEAT_VREPLACE |
| 7529 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7530 | orig_len = (int)STRLEN(ml_get_cursor()) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7531 | #endif |
| 7532 | } |
| 7533 | replace_pop_ins(); |
| 7534 | |
| 7535 | #ifdef FEAT_VREPLACE |
| 7536 | if (State & VREPLACE_FLAG) |
| 7537 | { |
| 7538 | /* Get the number of screen cells used by the inserted characters */ |
| 7539 | p = ml_get_cursor(); |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 7540 | ins_len = (int)STRLEN(p) - orig_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7541 | vcol = start_vcol; |
| 7542 | for (i = 0; i < ins_len; ++i) |
| 7543 | { |
| 7544 | vcol += chartabsize(p + i, vcol); |
| 7545 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7546 | i += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7547 | #endif |
| 7548 | } |
| 7549 | vcol -= start_vcol; |
| 7550 | |
| 7551 | /* Delete spaces that were inserted after the cursor to keep the |
| 7552 | * text aligned. */ |
| 7553 | curwin->w_cursor.col += ins_len; |
| 7554 | while (vcol > orig_vcols && gchar_cursor() == ' ') |
| 7555 | { |
| 7556 | del_char(FALSE); |
| 7557 | ++orig_vcols; |
| 7558 | } |
| 7559 | curwin->w_cursor.col -= ins_len; |
| 7560 | } |
| 7561 | #endif |
| 7562 | |
| 7563 | /* mark the buffer as changed and prepare for displaying */ |
| 7564 | changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col); |
| 7565 | } |
| 7566 | else if (cc == 0) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 7567 | (void)del_char_after_col(limit_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7568 | } |
| 7569 | |
| 7570 | #ifdef FEAT_CINDENT |
| 7571 | /* |
| 7572 | * Return TRUE if C-indenting is on. |
| 7573 | */ |
| 7574 | static int |
| 7575 | cindent_on() |
| 7576 | { |
| 7577 | return (!p_paste && (curbuf->b_p_cin |
| 7578 | # ifdef FEAT_EVAL |
| 7579 | || *curbuf->b_p_inde != NUL |
| 7580 | # endif |
| 7581 | )); |
| 7582 | } |
| 7583 | #endif |
| 7584 | |
| 7585 | #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO) |
| 7586 | /* |
| 7587 | * Re-indent the current line, based on the current contents of it and the |
| 7588 | * surrounding lines. Fixing the cursor position seems really easy -- I'm very |
| 7589 | * confused what all the part that handles Control-T is doing that I'm not. |
| 7590 | * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent. |
| 7591 | */ |
| 7592 | |
| 7593 | void |
| 7594 | fixthisline(get_the_indent) |
| 7595 | int (*get_the_indent) __ARGS((void)); |
| 7596 | { |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 7597 | change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7598 | if (linewhite(curwin->w_cursor.lnum)) |
| 7599 | did_ai = TRUE; /* delete the indent if the line stays empty */ |
| 7600 | } |
| 7601 | |
| 7602 | void |
| 7603 | fix_indent() |
| 7604 | { |
| 7605 | if (p_paste) |
| 7606 | return; |
| 7607 | # ifdef FEAT_LISP |
| 7608 | if (curbuf->b_p_lisp && curbuf->b_p_ai) |
| 7609 | fixthisline(get_lisp_indent); |
| 7610 | # endif |
| 7611 | # if defined(FEAT_LISP) && defined(FEAT_CINDENT) |
| 7612 | else |
| 7613 | # endif |
| 7614 | # ifdef FEAT_CINDENT |
| 7615 | if (cindent_on()) |
| 7616 | do_c_expr_indent(); |
| 7617 | # endif |
| 7618 | } |
| 7619 | |
| 7620 | #endif |
| 7621 | |
| 7622 | #ifdef FEAT_CINDENT |
| 7623 | /* |
| 7624 | * return TRUE if 'cinkeys' contains the key "keytyped", |
| 7625 | * when == '*': Only if key is preceded with '*' (indent before insert) |
| 7626 | * when == '!': Only if key is prededed with '!' (don't insert) |
| 7627 | * when == ' ': Only if key is not preceded with '*'(indent afterwards) |
| 7628 | * |
| 7629 | * "keytyped" can have a few special values: |
| 7630 | * KEY_OPEN_FORW |
| 7631 | * KEY_OPEN_BACK |
| 7632 | * KEY_COMPLETE just finished completion. |
| 7633 | * |
| 7634 | * If line_is_empty is TRUE accept keys with '0' before them. |
| 7635 | */ |
| 7636 | int |
| 7637 | in_cinkeys(keytyped, when, line_is_empty) |
| 7638 | int keytyped; |
| 7639 | int when; |
| 7640 | int line_is_empty; |
| 7641 | { |
| 7642 | char_u *look; |
| 7643 | int try_match; |
| 7644 | int try_match_word; |
| 7645 | char_u *p; |
| 7646 | char_u *line; |
| 7647 | int icase; |
| 7648 | int i; |
| 7649 | |
Bram Moolenaar | 3084894 | 2009-12-24 14:46:12 +0000 | [diff] [blame] | 7650 | if (keytyped == NUL) |
| 7651 | /* Can happen with CTRL-Y and CTRL-E on a short line. */ |
| 7652 | return FALSE; |
| 7653 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7654 | #ifdef FEAT_EVAL |
| 7655 | if (*curbuf->b_p_inde != NUL) |
| 7656 | look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */ |
| 7657 | else |
| 7658 | #endif |
| 7659 | look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */ |
| 7660 | while (*look) |
| 7661 | { |
| 7662 | /* |
| 7663 | * Find out if we want to try a match with this key, depending on |
| 7664 | * 'when' and a '*' or '!' before the key. |
| 7665 | */ |
| 7666 | switch (when) |
| 7667 | { |
| 7668 | case '*': try_match = (*look == '*'); break; |
| 7669 | case '!': try_match = (*look == '!'); break; |
| 7670 | default: try_match = (*look != '*'); break; |
| 7671 | } |
| 7672 | if (*look == '*' || *look == '!') |
| 7673 | ++look; |
| 7674 | |
| 7675 | /* |
| 7676 | * If there is a '0', only accept a match if the line is empty. |
| 7677 | * But may still match when typing last char of a word. |
| 7678 | */ |
| 7679 | if (*look == '0') |
| 7680 | { |
| 7681 | try_match_word = try_match; |
| 7682 | if (!line_is_empty) |
| 7683 | try_match = FALSE; |
| 7684 | ++look; |
| 7685 | } |
| 7686 | else |
| 7687 | try_match_word = FALSE; |
| 7688 | |
| 7689 | /* |
| 7690 | * does it look like a control character? |
| 7691 | */ |
| 7692 | if (*look == '^' |
| 7693 | #ifdef EBCDIC |
| 7694 | && (Ctrl_chr(look[1]) != 0) |
| 7695 | #else |
| 7696 | && look[1] >= '?' && look[1] <= '_' |
| 7697 | #endif |
| 7698 | ) |
| 7699 | { |
| 7700 | if (try_match && keytyped == Ctrl_chr(look[1])) |
| 7701 | return TRUE; |
| 7702 | look += 2; |
| 7703 | } |
| 7704 | /* |
| 7705 | * 'o' means "o" command, open forward. |
| 7706 | * 'O' means "O" command, open backward. |
| 7707 | */ |
| 7708 | else if (*look == 'o') |
| 7709 | { |
| 7710 | if (try_match && keytyped == KEY_OPEN_FORW) |
| 7711 | return TRUE; |
| 7712 | ++look; |
| 7713 | } |
| 7714 | else if (*look == 'O') |
| 7715 | { |
| 7716 | if (try_match && keytyped == KEY_OPEN_BACK) |
| 7717 | return TRUE; |
| 7718 | ++look; |
| 7719 | } |
| 7720 | |
| 7721 | /* |
| 7722 | * 'e' means to check for "else" at start of line and just before the |
| 7723 | * cursor. |
| 7724 | */ |
| 7725 | else if (*look == 'e') |
| 7726 | { |
| 7727 | if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4) |
| 7728 | { |
| 7729 | p = ml_get_curline(); |
| 7730 | if (skipwhite(p) == p + curwin->w_cursor.col - 4 && |
| 7731 | STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0) |
| 7732 | return TRUE; |
| 7733 | } |
| 7734 | ++look; |
| 7735 | } |
| 7736 | |
| 7737 | /* |
| 7738 | * ':' only causes an indent if it is at the end of a label or case |
| 7739 | * statement, or when it was before typing the ':' (to fix |
| 7740 | * class::method for C++). |
| 7741 | */ |
| 7742 | else if (*look == ':') |
| 7743 | { |
| 7744 | if (try_match && keytyped == ':') |
| 7745 | { |
| 7746 | p = ml_get_curline(); |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7747 | if (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
| 7748 | || cin_islabel(30)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7749 | return TRUE; |
Bram Moolenaar | 3011815 | 2007-06-28 10:49:22 +0000 | [diff] [blame] | 7750 | /* Need to get the line again after cin_islabel(). */ |
| 7751 | p = ml_get_curline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7752 | if (curwin->w_cursor.col > 2 |
| 7753 | && p[curwin->w_cursor.col - 1] == ':' |
| 7754 | && p[curwin->w_cursor.col - 2] == ':') |
| 7755 | { |
| 7756 | p[curwin->w_cursor.col - 1] = ' '; |
Bram Moolenaar | 3acfc30 | 2010-07-11 17:23:02 +0200 | [diff] [blame] | 7757 | i = (cin_iscase(p, FALSE) || cin_isscopedecl(p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7758 | || cin_islabel(30)); |
| 7759 | p = ml_get_curline(); |
| 7760 | p[curwin->w_cursor.col - 1] = ':'; |
| 7761 | if (i) |
| 7762 | return TRUE; |
| 7763 | } |
| 7764 | } |
| 7765 | ++look; |
| 7766 | } |
| 7767 | |
| 7768 | |
| 7769 | /* |
| 7770 | * Is it a key in <>, maybe? |
| 7771 | */ |
| 7772 | else if (*look == '<') |
| 7773 | { |
| 7774 | if (try_match) |
| 7775 | { |
| 7776 | /* |
| 7777 | * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>, |
| 7778 | * <:> and <!> so that people can re-indent on o, O, e, 0, <, |
| 7779 | * >, *, : and ! keys if they really really want to. |
| 7780 | */ |
| 7781 | if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL |
| 7782 | && keytyped == look[1]) |
| 7783 | return TRUE; |
| 7784 | |
| 7785 | if (keytyped == get_special_key_code(look + 1)) |
| 7786 | return TRUE; |
| 7787 | } |
| 7788 | while (*look && *look != '>') |
| 7789 | look++; |
| 7790 | while (*look == '>') |
| 7791 | look++; |
| 7792 | } |
| 7793 | |
| 7794 | /* |
| 7795 | * Is it a word: "=word"? |
| 7796 | */ |
| 7797 | else if (*look == '=' && look[1] != ',' && look[1] != NUL) |
| 7798 | { |
| 7799 | ++look; |
| 7800 | if (*look == '~') |
| 7801 | { |
| 7802 | icase = TRUE; |
| 7803 | ++look; |
| 7804 | } |
| 7805 | else |
| 7806 | icase = FALSE; |
| 7807 | p = vim_strchr(look, ','); |
| 7808 | if (p == NULL) |
| 7809 | p = look + STRLEN(look); |
| 7810 | if ((try_match || try_match_word) |
| 7811 | && curwin->w_cursor.col >= (colnr_T)(p - look)) |
| 7812 | { |
| 7813 | int match = FALSE; |
| 7814 | |
| 7815 | #ifdef FEAT_INS_EXPAND |
| 7816 | if (keytyped == KEY_COMPLETE) |
| 7817 | { |
| 7818 | char_u *s; |
| 7819 | |
| 7820 | /* Just completed a word, check if it starts with "look". |
| 7821 | * search back for the start of a word. */ |
| 7822 | line = ml_get_curline(); |
| 7823 | # ifdef FEAT_MBYTE |
| 7824 | if (has_mbyte) |
| 7825 | { |
| 7826 | char_u *n; |
| 7827 | |
| 7828 | for (s = line + curwin->w_cursor.col; s > line; s = n) |
| 7829 | { |
| 7830 | n = mb_prevptr(line, s); |
| 7831 | if (!vim_iswordp(n)) |
| 7832 | break; |
| 7833 | } |
| 7834 | } |
| 7835 | else |
| 7836 | # endif |
| 7837 | for (s = line + curwin->w_cursor.col; s > line; --s) |
| 7838 | if (!vim_iswordc(s[-1])) |
| 7839 | break; |
| 7840 | if (s + (p - look) <= line + curwin->w_cursor.col |
| 7841 | && (icase |
| 7842 | ? MB_STRNICMP(s, look, p - look) |
| 7843 | : STRNCMP(s, look, p - look)) == 0) |
| 7844 | match = TRUE; |
| 7845 | } |
| 7846 | else |
| 7847 | #endif |
| 7848 | /* TODO: multi-byte */ |
| 7849 | if (keytyped == (int)p[-1] || (icase && keytyped < 256 |
| 7850 | && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1]))) |
| 7851 | { |
| 7852 | line = ml_get_cursor(); |
| 7853 | if ((curwin->w_cursor.col == (colnr_T)(p - look) |
| 7854 | || !vim_iswordc(line[-(p - look) - 1])) |
| 7855 | && (icase |
| 7856 | ? MB_STRNICMP(line - (p - look), look, p - look) |
| 7857 | : STRNCMP(line - (p - look), look, p - look)) |
| 7858 | == 0) |
| 7859 | match = TRUE; |
| 7860 | } |
| 7861 | if (match && try_match_word && !try_match) |
| 7862 | { |
| 7863 | /* "0=word": Check if there are only blanks before the |
| 7864 | * word. */ |
| 7865 | line = ml_get_curline(); |
| 7866 | if ((int)(skipwhite(line) - line) != |
| 7867 | (int)(curwin->w_cursor.col - (p - look))) |
| 7868 | match = FALSE; |
| 7869 | } |
| 7870 | if (match) |
| 7871 | return TRUE; |
| 7872 | } |
| 7873 | look = p; |
| 7874 | } |
| 7875 | |
| 7876 | /* |
| 7877 | * ok, it's a boring generic character. |
| 7878 | */ |
| 7879 | else |
| 7880 | { |
| 7881 | if (try_match && *look == keytyped) |
| 7882 | return TRUE; |
| 7883 | ++look; |
| 7884 | } |
| 7885 | |
| 7886 | /* |
| 7887 | * Skip over ", ". |
| 7888 | */ |
| 7889 | look = skip_to_option_part(look); |
| 7890 | } |
| 7891 | return FALSE; |
| 7892 | } |
| 7893 | #endif /* FEAT_CINDENT */ |
| 7894 | |
| 7895 | #if defined(FEAT_RIGHTLEFT) || defined(PROTO) |
| 7896 | /* |
| 7897 | * Map Hebrew keyboard when in hkmap mode. |
| 7898 | */ |
| 7899 | int |
| 7900 | hkmap(c) |
| 7901 | int c; |
| 7902 | { |
| 7903 | if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */ |
| 7904 | { |
| 7905 | enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD, |
| 7906 | KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN, |
| 7907 | PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV}; |
| 7908 | static char_u map[26] = |
| 7909 | {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/, |
| 7910 | (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/, |
| 7911 | (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/, |
| 7912 | (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/, |
| 7913 | (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/, |
| 7914 | (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/, |
| 7915 | (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/, |
| 7916 | (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/, |
| 7917 | (char_u)AIN /*y*/, (char_u)ZADI /*z*/}; |
| 7918 | |
| 7919 | if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z') |
| 7920 | return (int)(map[CharOrd(c)] - 1 + p_aleph); |
| 7921 | /* '-1'='sofit' */ |
| 7922 | else if (c == 'x') |
| 7923 | return 'X'; |
| 7924 | else if (c == 'q') |
| 7925 | return '\''; /* {geresh}={'} */ |
| 7926 | else if (c == 246) |
| 7927 | return ' '; /* \"o --> ' ' for a german keyboard */ |
| 7928 | else if (c == 228) |
| 7929 | return ' '; /* \"a --> ' ' -- / -- */ |
| 7930 | else if (c == 252) |
| 7931 | return ' '; /* \"u --> ' ' -- / -- */ |
| 7932 | #ifdef EBCDIC |
| 7933 | else if (islower(c)) |
| 7934 | #else |
| 7935 | /* NOTE: islower() does not do the right thing for us on Linux so we |
| 7936 | * do this the same was as 5.7 and previous, so it works correctly on |
| 7937 | * all systems. Specifically, the e.g. Delete and Arrow keys are |
| 7938 | * munged and won't work if e.g. searching for Hebrew text. |
| 7939 | */ |
| 7940 | else if (c >= 'a' && c <= 'z') |
| 7941 | #endif |
| 7942 | return (int)(map[CharOrdLow(c)] + p_aleph); |
| 7943 | else |
| 7944 | return c; |
| 7945 | } |
| 7946 | else |
| 7947 | { |
| 7948 | switch (c) |
| 7949 | { |
| 7950 | case '`': return ';'; |
| 7951 | case '/': return '.'; |
| 7952 | case '\'': return ','; |
| 7953 | case 'q': return '/'; |
| 7954 | case 'w': return '\''; |
| 7955 | |
| 7956 | /* Hebrew letters - set offset from 'a' */ |
| 7957 | case ',': c = '{'; break; |
| 7958 | case '.': c = 'v'; break; |
| 7959 | case ';': c = 't'; break; |
| 7960 | default: { |
| 7961 | static char str[] = "zqbcxlsjphmkwonu ydafe rig"; |
| 7962 | |
| 7963 | #ifdef EBCDIC |
| 7964 | /* see note about islower() above */ |
| 7965 | if (!islower(c)) |
| 7966 | #else |
| 7967 | if (c < 'a' || c > 'z') |
| 7968 | #endif |
| 7969 | return c; |
| 7970 | c = str[CharOrdLow(c)]; |
| 7971 | break; |
| 7972 | } |
| 7973 | } |
| 7974 | |
| 7975 | return (int)(CharOrdLow(c) + p_aleph); |
| 7976 | } |
| 7977 | } |
| 7978 | #endif |
| 7979 | |
| 7980 | static void |
| 7981 | ins_reg() |
| 7982 | { |
| 7983 | int need_redraw = FALSE; |
| 7984 | int regname; |
| 7985 | int literally = 0; |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 7986 | #ifdef FEAT_VISUAL |
| 7987 | int vis_active = VIsual_active; |
| 7988 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7989 | |
| 7990 | /* |
| 7991 | * If we are going to wait for a character, show a '"'. |
| 7992 | */ |
| 7993 | pc_status = PC_STATUS_UNSET; |
| 7994 | if (redrawing() && !char_avail()) |
| 7995 | { |
| 7996 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 7997 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7998 | |
| 7999 | edit_putchar('"', TRUE); |
| 8000 | #ifdef FEAT_CMDL_INFO |
| 8001 | add_to_showcmd_c(Ctrl_R); |
| 8002 | #endif |
| 8003 | } |
| 8004 | |
| 8005 | #ifdef USE_ON_FLY_SCROLL |
| 8006 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 8007 | #endif |
| 8008 | |
| 8009 | /* |
| 8010 | * Don't map the register name. This also prevents the mode message to be |
| 8011 | * deleted when ESC is hit. |
| 8012 | */ |
| 8013 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8014 | regname = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8015 | LANGMAP_ADJUST(regname, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8016 | if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P) |
| 8017 | { |
| 8018 | /* Get a third key for literal register insertion */ |
| 8019 | literally = regname; |
| 8020 | #ifdef FEAT_CMDL_INFO |
| 8021 | add_to_showcmd_c(literally); |
| 8022 | #endif |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8023 | regname = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8024 | LANGMAP_ADJUST(regname, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8025 | } |
| 8026 | --no_mapping; |
| 8027 | |
| 8028 | #ifdef FEAT_EVAL |
| 8029 | /* |
| 8030 | * Don't call u_sync() while getting the expression, |
| 8031 | * evaluating it or giving an error message for it! |
| 8032 | */ |
| 8033 | ++no_u_sync; |
| 8034 | if (regname == '=') |
| 8035 | { |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8036 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8037 | int im_on = im_get_status(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8038 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8039 | regname = get_expr_register(); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8040 | # ifdef USE_IM_CONTROL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8041 | /* Restore the Input Method. */ |
| 8042 | if (im_on) |
| 8043 | im_set_active(TRUE); |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8044 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8045 | } |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 8046 | if (regname == NUL || !valid_yank_reg(regname, FALSE)) |
| 8047 | { |
| 8048 | vim_beep(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8049 | need_redraw = TRUE; /* remove the '"' */ |
Bram Moolenaar | 677ee68 | 2005-01-27 14:41:15 +0000 | [diff] [blame] | 8050 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8051 | else |
| 8052 | { |
| 8053 | #endif |
| 8054 | if (literally == Ctrl_O || literally == Ctrl_P) |
| 8055 | { |
| 8056 | /* Append the command to the redo buffer. */ |
| 8057 | AppendCharToRedobuff(Ctrl_R); |
| 8058 | AppendCharToRedobuff(literally); |
| 8059 | AppendCharToRedobuff(regname); |
| 8060 | |
| 8061 | do_put(regname, BACKWARD, 1L, |
| 8062 | (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND); |
| 8063 | } |
| 8064 | else if (insert_reg(regname, literally) == FAIL) |
| 8065 | { |
| 8066 | vim_beep(); |
| 8067 | need_redraw = TRUE; /* remove the '"' */ |
| 8068 | } |
Bram Moolenaar | 8f999f1 | 2005-01-25 22:12:55 +0000 | [diff] [blame] | 8069 | else if (stop_insert_mode) |
| 8070 | /* When the '=' register was used and a function was invoked that |
| 8071 | * did ":stopinsert" then stuff_empty() returns FALSE but we won't |
| 8072 | * insert anything, need to remove the '"' */ |
| 8073 | need_redraw = TRUE; |
| 8074 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8075 | #ifdef FEAT_EVAL |
| 8076 | } |
| 8077 | --no_u_sync; |
| 8078 | #endif |
| 8079 | #ifdef FEAT_CMDL_INFO |
| 8080 | clear_showcmd(); |
| 8081 | #endif |
| 8082 | |
| 8083 | /* If the inserted register is empty, we need to remove the '"' */ |
| 8084 | if (need_redraw || stuff_empty()) |
| 8085 | edit_unputchar(); |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 8086 | |
| 8087 | #ifdef FEAT_VISUAL |
| 8088 | /* Disallow starting Visual mode here, would get a weird mode. */ |
| 8089 | if (!vis_active && VIsual_active) |
| 8090 | end_visual_mode(); |
| 8091 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8092 | } |
| 8093 | |
| 8094 | /* |
| 8095 | * CTRL-G commands in Insert mode. |
| 8096 | */ |
| 8097 | static void |
| 8098 | ins_ctrl_g() |
| 8099 | { |
| 8100 | int c; |
| 8101 | |
| 8102 | #ifdef FEAT_INS_EXPAND |
| 8103 | /* Right after CTRL-X the cursor will be after the ruler. */ |
| 8104 | setcursor(); |
| 8105 | #endif |
| 8106 | |
| 8107 | /* |
| 8108 | * Don't map the second key. This also prevents the mode message to be |
| 8109 | * deleted when ESC is hit. |
| 8110 | */ |
| 8111 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 8112 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8113 | --no_mapping; |
| 8114 | switch (c) |
| 8115 | { |
| 8116 | /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */ |
| 8117 | case K_UP: |
| 8118 | case Ctrl_K: |
| 8119 | case 'k': ins_up(TRUE); |
| 8120 | break; |
| 8121 | |
| 8122 | /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */ |
| 8123 | case K_DOWN: |
| 8124 | case Ctrl_J: |
| 8125 | case 'j': ins_down(TRUE); |
| 8126 | break; |
| 8127 | |
| 8128 | /* CTRL-G u: start new undoable edit */ |
Bram Moolenaar | 779b74b | 2006-04-10 14:55:34 +0000 | [diff] [blame] | 8129 | case 'u': u_sync(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8130 | ins_need_undo = TRUE; |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 8131 | |
| 8132 | /* Need to reset Insstart, esp. because a BS that joins |
Bram Moolenaar | 34e0bfa | 2007-05-10 18:44:18 +0000 | [diff] [blame] | 8133 | * a line to the previous one must save for undo. */ |
Bram Moolenaar | a40ceaf | 2006-01-13 22:35:40 +0000 | [diff] [blame] | 8134 | Insstart = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8135 | break; |
| 8136 | |
| 8137 | /* Unknown CTRL-G command, reserved for future expansion. */ |
| 8138 | default: vim_beep(); |
| 8139 | } |
| 8140 | } |
| 8141 | |
| 8142 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8143 | * CTRL-^ in Insert mode. |
| 8144 | */ |
| 8145 | static void |
| 8146 | ins_ctrl_hat() |
| 8147 | { |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 8148 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8149 | { |
| 8150 | /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */ |
| 8151 | if (State & LANGMAP) |
| 8152 | { |
| 8153 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 8154 | State &= ~LANGMAP; |
| 8155 | } |
| 8156 | else |
| 8157 | { |
| 8158 | curbuf->b_p_iminsert = B_IMODE_LMAP; |
| 8159 | State |= LANGMAP; |
| 8160 | #ifdef USE_IM_CONTROL |
| 8161 | im_set_active(FALSE); |
| 8162 | #endif |
| 8163 | } |
| 8164 | } |
| 8165 | #ifdef USE_IM_CONTROL |
| 8166 | else |
| 8167 | { |
| 8168 | /* There are no ":lmap" mappings, toggle IM */ |
| 8169 | if (im_get_status()) |
| 8170 | { |
| 8171 | curbuf->b_p_iminsert = B_IMODE_NONE; |
| 8172 | im_set_active(FALSE); |
| 8173 | } |
| 8174 | else |
| 8175 | { |
| 8176 | curbuf->b_p_iminsert = B_IMODE_IM; |
| 8177 | State &= ~LANGMAP; |
| 8178 | im_set_active(TRUE); |
| 8179 | } |
| 8180 | } |
| 8181 | #endif |
| 8182 | set_iminsert_global(); |
| 8183 | showmode(); |
| 8184 | #ifdef FEAT_GUI |
| 8185 | /* may show different cursor shape or color */ |
| 8186 | if (gui.in_use) |
| 8187 | gui_update_cursor(TRUE, FALSE); |
| 8188 | #endif |
| 8189 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 8190 | /* Show/unshow value of 'keymap' in status lines. */ |
| 8191 | status_redraw_curbuf(); |
| 8192 | #endif |
| 8193 | } |
| 8194 | |
| 8195 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8196 | * Handle ESC in insert mode. |
| 8197 | * Returns TRUE when leaving insert mode, FALSE when going to repeat the |
| 8198 | * insert. |
| 8199 | */ |
| 8200 | static int |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8201 | ins_esc(count, cmdchar, nomove) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8202 | long *count; |
| 8203 | int cmdchar; |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8204 | int nomove; /* don't move cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8205 | { |
| 8206 | int temp; |
| 8207 | static int disabled_redraw = FALSE; |
| 8208 | |
Bram Moolenaar | b9a02fc | 2006-03-12 22:08:12 +0000 | [diff] [blame] | 8209 | #ifdef FEAT_SPELL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8210 | check_spell_redraw(); |
| 8211 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8212 | #if defined(FEAT_HANGULIN) |
| 8213 | # if defined(ESC_CHG_TO_ENG_MODE) |
| 8214 | hangul_input_state_set(0); |
| 8215 | # endif |
| 8216 | if (composing_hangul) |
| 8217 | { |
| 8218 | push_raw_key(composing_hangul_buffer, 2); |
| 8219 | composing_hangul = 0; |
| 8220 | } |
| 8221 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8222 | |
| 8223 | temp = curwin->w_cursor.col; |
| 8224 | if (disabled_redraw) |
| 8225 | { |
| 8226 | --RedrawingDisabled; |
| 8227 | disabled_redraw = FALSE; |
| 8228 | } |
| 8229 | if (!arrow_used) |
| 8230 | { |
| 8231 | /* |
| 8232 | * Don't append the ESC for "r<CR>" and "grx". |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 8233 | * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for |
| 8234 | * when "count" is non-zero. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8235 | */ |
| 8236 | if (cmdchar != 'r' && cmdchar != 'v') |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 8237 | AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8238 | |
| 8239 | /* |
| 8240 | * Repeating insert may take a long time. Check for |
| 8241 | * interrupt now and then. |
| 8242 | */ |
| 8243 | if (*count > 0) |
| 8244 | { |
| 8245 | line_breakcheck(); |
| 8246 | if (got_int) |
| 8247 | *count = 0; |
| 8248 | } |
| 8249 | |
| 8250 | if (--*count > 0) /* repeat what was typed */ |
| 8251 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 8252 | /* Vi repeats the insert without replacing characters. */ |
| 8253 | if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL) |
| 8254 | State &= ~REPLACE_FLAG; |
| 8255 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8256 | (void)start_redo_ins(); |
| 8257 | if (cmdchar == 'r' || cmdchar == 'v') |
| 8258 | stuffReadbuff(ESC_STR); /* no ESC in redo buffer */ |
| 8259 | ++RedrawingDisabled; |
| 8260 | disabled_redraw = TRUE; |
| 8261 | return FALSE; /* repeat the insert */ |
| 8262 | } |
| 8263 | stop_insert(&curwin->w_cursor, TRUE); |
| 8264 | undisplay_dollar(); |
| 8265 | } |
| 8266 | |
| 8267 | /* When an autoindent was removed, curswant stays after the |
| 8268 | * indent */ |
| 8269 | if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col) |
| 8270 | curwin->w_set_curswant = TRUE; |
| 8271 | |
| 8272 | /* Remember the last Insert position in the '^ mark. */ |
| 8273 | if (!cmdmod.keepjumps) |
| 8274 | curbuf->b_last_insert = curwin->w_cursor; |
| 8275 | |
| 8276 | /* |
| 8277 | * The cursor should end up on the last inserted character. |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8278 | * 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] | 8279 | */ |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8280 | if (!nomove |
| 8281 | && (curwin->w_cursor.col != 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8282 | #ifdef FEAT_VIRTUALEDIT |
| 8283 | || curwin->w_cursor.coladd > 0 |
| 8284 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8285 | ) |
| 8286 | && (restart_edit == NUL |
| 8287 | || (gchar_cursor() == NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8288 | #ifdef FEAT_VISUAL |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8289 | && !VIsual_active |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8290 | #endif |
Bram Moolenaar | 488c651 | 2005-08-11 20:09:58 +0000 | [diff] [blame] | 8291 | )) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8292 | #ifdef FEAT_RIGHTLEFT |
| 8293 | && !revins_on |
| 8294 | #endif |
| 8295 | ) |
| 8296 | { |
| 8297 | #ifdef FEAT_VIRTUALEDIT |
| 8298 | if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL) |
| 8299 | { |
| 8300 | oneleft(); |
| 8301 | if (restart_edit != NUL) |
| 8302 | ++curwin->w_cursor.coladd; |
| 8303 | } |
| 8304 | else |
| 8305 | #endif |
| 8306 | { |
| 8307 | --curwin->w_cursor.col; |
| 8308 | #ifdef FEAT_MBYTE |
| 8309 | /* Correct cursor for multi-byte character. */ |
| 8310 | if (has_mbyte) |
| 8311 | mb_adjust_cursor(); |
| 8312 | #endif |
| 8313 | } |
| 8314 | } |
| 8315 | |
| 8316 | #ifdef USE_IM_CONTROL |
| 8317 | /* Disable IM to allow typing English directly for Normal mode commands. |
| 8318 | * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as |
| 8319 | * well). */ |
| 8320 | if (!(State & LANGMAP)) |
| 8321 | im_save_status(&curbuf->b_p_iminsert); |
| 8322 | im_set_active(FALSE); |
| 8323 | #endif |
| 8324 | |
| 8325 | State = NORMAL; |
| 8326 | /* need to position cursor again (e.g. when on a TAB ) */ |
| 8327 | changed_cline_bef_curs(); |
| 8328 | |
| 8329 | #ifdef FEAT_MOUSE |
| 8330 | setmouse(); |
| 8331 | #endif |
| 8332 | #ifdef CURSOR_SHAPE |
| 8333 | ui_cursor_shape(); /* may show different cursor shape */ |
| 8334 | #endif |
| 8335 | |
| 8336 | /* |
| 8337 | * When recording or for CTRL-O, need to display the new mode. |
| 8338 | * Otherwise remove the mode message. |
| 8339 | */ |
| 8340 | if (Recording || restart_edit != NUL) |
| 8341 | showmode(); |
| 8342 | else if (p_smd) |
| 8343 | MSG(""); |
| 8344 | |
| 8345 | return TRUE; /* exit Insert mode */ |
| 8346 | } |
| 8347 | |
| 8348 | #ifdef FEAT_RIGHTLEFT |
| 8349 | /* |
| 8350 | * Toggle language: hkmap and revins_on. |
| 8351 | * Move to end of reverse inserted text. |
| 8352 | */ |
| 8353 | static void |
| 8354 | ins_ctrl_() |
| 8355 | { |
| 8356 | if (revins_on && revins_chars && revins_scol >= 0) |
| 8357 | { |
| 8358 | while (gchar_cursor() != NUL && revins_chars--) |
| 8359 | ++curwin->w_cursor.col; |
| 8360 | } |
| 8361 | p_ri = !p_ri; |
| 8362 | revins_on = (State == INSERT && p_ri); |
| 8363 | if (revins_on) |
| 8364 | { |
| 8365 | revins_scol = curwin->w_cursor.col; |
| 8366 | revins_legal++; |
| 8367 | revins_chars = 0; |
| 8368 | undisplay_dollar(); |
| 8369 | } |
| 8370 | else |
| 8371 | revins_scol = -1; |
| 8372 | #ifdef FEAT_FKMAP |
| 8373 | if (p_altkeymap) |
| 8374 | { |
| 8375 | /* |
| 8376 | * to be consistent also for redo command, using '.' |
| 8377 | * set arrow_used to true and stop it - causing to redo |
| 8378 | * characters entered in one mode (normal/reverse insert). |
| 8379 | */ |
| 8380 | arrow_used = TRUE; |
| 8381 | (void)stop_arrow(); |
| 8382 | p_fkmap = curwin->w_p_rl ^ p_ri; |
| 8383 | if (p_fkmap && p_ri) |
| 8384 | State = INSERT; |
| 8385 | } |
| 8386 | else |
| 8387 | #endif |
| 8388 | p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */ |
| 8389 | showmode(); |
| 8390 | } |
| 8391 | #endif |
| 8392 | |
| 8393 | #ifdef FEAT_VISUAL |
| 8394 | /* |
| 8395 | * If 'keymodel' contains "startsel", may start selection. |
| 8396 | * Returns TRUE when a CTRL-O and other keys stuffed. |
| 8397 | */ |
| 8398 | static int |
| 8399 | ins_start_select(c) |
| 8400 | int c; |
| 8401 | { |
| 8402 | if (km_startsel) |
| 8403 | switch (c) |
| 8404 | { |
| 8405 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8406 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8407 | case K_PAGEUP: |
| 8408 | case K_KPAGEUP: |
| 8409 | case K_PAGEDOWN: |
| 8410 | case K_KPAGEDOWN: |
| 8411 | # ifdef MACOS |
| 8412 | case K_LEFT: |
| 8413 | case K_RIGHT: |
| 8414 | case K_UP: |
| 8415 | case K_DOWN: |
| 8416 | case K_END: |
| 8417 | case K_HOME: |
| 8418 | # endif |
| 8419 | if (!(mod_mask & MOD_MASK_SHIFT)) |
| 8420 | break; |
| 8421 | /* FALLTHROUGH */ |
| 8422 | case K_S_LEFT: |
| 8423 | case K_S_RIGHT: |
| 8424 | case K_S_UP: |
| 8425 | case K_S_DOWN: |
| 8426 | case K_S_END: |
| 8427 | case K_S_HOME: |
| 8428 | /* Start selection right away, the cursor can move with |
| 8429 | * CTRL-O when beyond the end of the line. */ |
| 8430 | start_selection(); |
| 8431 | |
| 8432 | /* Execute the key in (insert) Select mode. */ |
| 8433 | stuffcharReadbuff(Ctrl_O); |
| 8434 | if (mod_mask) |
| 8435 | { |
| 8436 | char_u buf[4]; |
| 8437 | |
| 8438 | buf[0] = K_SPECIAL; |
| 8439 | buf[1] = KS_MODIFIER; |
| 8440 | buf[2] = mod_mask; |
| 8441 | buf[3] = NUL; |
| 8442 | stuffReadbuff(buf); |
| 8443 | } |
| 8444 | stuffcharReadbuff(c); |
| 8445 | return TRUE; |
| 8446 | } |
| 8447 | return FALSE; |
| 8448 | } |
| 8449 | #endif |
| 8450 | |
| 8451 | /* |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8452 | * <Insert> key in Insert mode: toggle insert/remplace mode. |
| 8453 | */ |
| 8454 | static void |
| 8455 | ins_insert(replaceState) |
| 8456 | int replaceState; |
| 8457 | { |
| 8458 | #ifdef FEAT_FKMAP |
| 8459 | if (p_fkmap && p_ri) |
| 8460 | { |
| 8461 | beep_flush(); |
| 8462 | EMSG(farsi_text_3); /* encoded in Farsi */ |
| 8463 | return; |
| 8464 | } |
| 8465 | #endif |
| 8466 | |
| 8467 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 8468 | # ifdef FEAT_EVAL |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8469 | set_vim_var_string(VV_INSERTMODE, |
| 8470 | (char_u *)((State & REPLACE_FLAG) ? "i" : |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 8471 | # ifdef FEAT_VREPLACE |
| 8472 | replaceState == VREPLACE ? "v" : |
| 8473 | # endif |
| 8474 | "r"), 1); |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 8475 | # endif |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 8476 | apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf); |
| 8477 | #endif |
| 8478 | if (State & REPLACE_FLAG) |
| 8479 | State = INSERT | (State & LANGMAP); |
| 8480 | else |
| 8481 | State = replaceState | (State & LANGMAP); |
| 8482 | AppendCharToRedobuff(K_INS); |
| 8483 | showmode(); |
| 8484 | #ifdef CURSOR_SHAPE |
| 8485 | ui_cursor_shape(); /* may show different cursor shape */ |
| 8486 | #endif |
| 8487 | } |
| 8488 | |
| 8489 | /* |
| 8490 | * Pressed CTRL-O in Insert mode. |
| 8491 | */ |
| 8492 | static void |
| 8493 | ins_ctrl_o() |
| 8494 | { |
| 8495 | #ifdef FEAT_VREPLACE |
| 8496 | if (State & VREPLACE_FLAG) |
| 8497 | restart_edit = 'V'; |
| 8498 | else |
| 8499 | #endif |
| 8500 | if (State & REPLACE_FLAG) |
| 8501 | restart_edit = 'R'; |
| 8502 | else |
| 8503 | restart_edit = 'I'; |
| 8504 | #ifdef FEAT_VIRTUALEDIT |
| 8505 | if (virtual_active()) |
| 8506 | ins_at_eol = FALSE; /* cursor always keeps its column */ |
| 8507 | else |
| 8508 | #endif |
| 8509 | ins_at_eol = (gchar_cursor() == NUL); |
| 8510 | } |
| 8511 | |
| 8512 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8513 | * If the cursor is on an indent, ^T/^D insert/delete one |
| 8514 | * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>". |
Bram Moolenaar | 5b3e460 | 2009-02-04 10:20:58 +0000 | [diff] [blame] | 8515 | * Always round the indent to 'shiftwidth', this is compatible |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8516 | * with vi. But vi only supports ^T and ^D after an |
| 8517 | * autoindent, we support it everywhere. |
| 8518 | */ |
| 8519 | static void |
| 8520 | ins_shift(c, lastc) |
| 8521 | int c; |
| 8522 | int lastc; |
| 8523 | { |
| 8524 | if (stop_arrow() == FAIL) |
| 8525 | return; |
| 8526 | AppendCharToRedobuff(c); |
| 8527 | |
| 8528 | /* |
| 8529 | * 0^D and ^^D: remove all indent. |
| 8530 | */ |
Bram Moolenaar | 0cbac5b | 2007-07-29 13:03:35 +0000 | [diff] [blame] | 8531 | if (c == Ctrl_D && (lastc == '0' || lastc == '^') |
| 8532 | && curwin->w_cursor.col > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8533 | { |
| 8534 | --curwin->w_cursor.col; |
| 8535 | (void)del_char(FALSE); /* delete the '^' or '0' */ |
| 8536 | /* In Replace mode, restore the characters that '^' or '0' replaced. */ |
| 8537 | if (State & REPLACE_FLAG) |
| 8538 | replace_pop_ins(); |
| 8539 | if (lastc == '^') |
| 8540 | old_indent = get_indent(); /* remember curr. indent */ |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 8541 | change_indent(INDENT_SET, 0, TRUE, 0, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8542 | } |
| 8543 | else |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 8544 | 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] | 8545 | |
| 8546 | if (did_ai && *skipwhite(ml_get_curline()) != NUL) |
| 8547 | did_ai = FALSE; |
| 8548 | #ifdef FEAT_SMARTINDENT |
| 8549 | did_si = FALSE; |
| 8550 | can_si = FALSE; |
| 8551 | can_si_back = FALSE; |
| 8552 | #endif |
| 8553 | #ifdef FEAT_CINDENT |
| 8554 | can_cindent = FALSE; /* no cindenting after ^D or ^T */ |
| 8555 | #endif |
| 8556 | } |
| 8557 | |
| 8558 | static void |
| 8559 | ins_del() |
| 8560 | { |
| 8561 | int temp; |
| 8562 | |
| 8563 | if (stop_arrow() == FAIL) |
| 8564 | return; |
| 8565 | if (gchar_cursor() == NUL) /* delete newline */ |
| 8566 | { |
| 8567 | temp = curwin->w_cursor.col; |
| 8568 | if (!can_bs(BS_EOL) /* only if "eol" included */ |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 8569 | || do_join(2, FALSE, TRUE) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8570 | vim_beep(); |
| 8571 | else |
| 8572 | curwin->w_cursor.col = temp; |
| 8573 | } |
| 8574 | else if (del_char(FALSE) == FAIL) /* delete char under cursor */ |
| 8575 | vim_beep(); |
| 8576 | did_ai = FALSE; |
| 8577 | #ifdef FEAT_SMARTINDENT |
| 8578 | did_si = FALSE; |
| 8579 | can_si = FALSE; |
| 8580 | can_si_back = FALSE; |
| 8581 | #endif |
| 8582 | AppendCharToRedobuff(K_DEL); |
| 8583 | } |
| 8584 | |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8585 | static void ins_bs_one __ARGS((colnr_T *vcolp)); |
| 8586 | |
| 8587 | /* |
| 8588 | * Delete one character for ins_bs(). |
| 8589 | */ |
| 8590 | static void |
| 8591 | ins_bs_one(vcolp) |
| 8592 | colnr_T *vcolp; |
| 8593 | { |
| 8594 | dec_cursor(); |
| 8595 | getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL); |
| 8596 | if (State & REPLACE_FLAG) |
| 8597 | { |
| 8598 | /* Don't delete characters before the insert point when in |
| 8599 | * Replace mode */ |
| 8600 | if (curwin->w_cursor.lnum != Insstart.lnum |
| 8601 | || curwin->w_cursor.col >= Insstart.col) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 8602 | replace_do_bs(-1); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8603 | } |
| 8604 | else |
| 8605 | (void)del_char(FALSE); |
| 8606 | } |
| 8607 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8608 | /* |
| 8609 | * Handle Backspace, delete-word and delete-line in Insert mode. |
| 8610 | * Return TRUE when backspace was actually used. |
| 8611 | */ |
| 8612 | static int |
| 8613 | ins_bs(c, mode, inserted_space_p) |
| 8614 | int c; |
| 8615 | int mode; |
| 8616 | int *inserted_space_p; |
| 8617 | { |
| 8618 | linenr_T lnum; |
| 8619 | int cc; |
| 8620 | int temp = 0; /* init for GCC */ |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8621 | colnr_T save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8622 | colnr_T mincol; |
| 8623 | int did_backspace = FALSE; |
| 8624 | int in_indent; |
| 8625 | int oldState; |
| 8626 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8627 | int cpc[MAX_MCO]; /* composing characters */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8628 | #endif |
| 8629 | |
| 8630 | /* |
| 8631 | * can't delete anything in an empty file |
| 8632 | * can't backup past first character in buffer |
| 8633 | * can't backup past starting point unless 'backspace' > 1 |
| 8634 | * can backup to a previous line if 'backspace' == 0 |
| 8635 | */ |
| 8636 | if ( bufempty() |
| 8637 | || ( |
| 8638 | #ifdef FEAT_RIGHTLEFT |
| 8639 | !revins_on && |
| 8640 | #endif |
| 8641 | ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0) |
| 8642 | || (!can_bs(BS_START) |
| 8643 | && (arrow_used |
| 8644 | || (curwin->w_cursor.lnum == Insstart.lnum |
| 8645 | && curwin->w_cursor.col <= Insstart.col))) |
| 8646 | || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0 |
| 8647 | && curwin->w_cursor.col <= ai_col) |
| 8648 | || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0)))) |
| 8649 | { |
| 8650 | vim_beep(); |
| 8651 | return FALSE; |
| 8652 | } |
| 8653 | |
| 8654 | if (stop_arrow() == FAIL) |
| 8655 | return FALSE; |
| 8656 | in_indent = inindent(0); |
| 8657 | #ifdef FEAT_CINDENT |
| 8658 | if (in_indent) |
| 8659 | can_cindent = FALSE; |
| 8660 | #endif |
| 8661 | #ifdef FEAT_COMMENTS |
| 8662 | end_comment_pending = NUL; /* After BS, don't auto-end comment */ |
| 8663 | #endif |
| 8664 | #ifdef FEAT_RIGHTLEFT |
| 8665 | if (revins_on) /* put cursor after last inserted char */ |
| 8666 | inc_cursor(); |
| 8667 | #endif |
| 8668 | |
| 8669 | #ifdef FEAT_VIRTUALEDIT |
| 8670 | /* Virtualedit: |
| 8671 | * BACKSPACE_CHAR eats a virtual space |
| 8672 | * BACKSPACE_WORD eats all coladd |
| 8673 | * BACKSPACE_LINE eats all coladd and keeps going |
| 8674 | */ |
| 8675 | if (curwin->w_cursor.coladd > 0) |
| 8676 | { |
| 8677 | if (mode == BACKSPACE_CHAR) |
| 8678 | { |
| 8679 | --curwin->w_cursor.coladd; |
| 8680 | return TRUE; |
| 8681 | } |
| 8682 | if (mode == BACKSPACE_WORD) |
| 8683 | { |
| 8684 | curwin->w_cursor.coladd = 0; |
| 8685 | return TRUE; |
| 8686 | } |
| 8687 | curwin->w_cursor.coladd = 0; |
| 8688 | } |
| 8689 | #endif |
| 8690 | |
| 8691 | /* |
| 8692 | * delete newline! |
| 8693 | */ |
| 8694 | if (curwin->w_cursor.col == 0) |
| 8695 | { |
| 8696 | lnum = Insstart.lnum; |
| 8697 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8698 | #ifdef FEAT_RIGHTLEFT |
| 8699 | || revins_on |
| 8700 | #endif |
| 8701 | ) |
| 8702 | { |
| 8703 | if (u_save((linenr_T)(curwin->w_cursor.lnum - 2), |
| 8704 | (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL) |
| 8705 | return FALSE; |
| 8706 | --Insstart.lnum; |
| 8707 | Insstart.col = MAXCOL; |
| 8708 | } |
| 8709 | /* |
| 8710 | * In replace mode: |
| 8711 | * cc < 0: NL was inserted, delete it |
| 8712 | * cc >= 0: NL was replaced, put original characters back |
| 8713 | */ |
| 8714 | cc = -1; |
| 8715 | if (State & REPLACE_FLAG) |
| 8716 | cc = replace_pop(); /* returns -1 if NL was inserted */ |
| 8717 | /* |
| 8718 | * In replace mode, in the line we started replacing, we only move the |
| 8719 | * cursor. |
| 8720 | */ |
| 8721 | if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum) |
| 8722 | { |
| 8723 | dec_cursor(); |
| 8724 | } |
| 8725 | else |
| 8726 | { |
| 8727 | #ifdef FEAT_VREPLACE |
| 8728 | if (!(State & VREPLACE_FLAG) |
| 8729 | || curwin->w_cursor.lnum > orig_line_count) |
| 8730 | #endif |
| 8731 | { |
| 8732 | temp = gchar_cursor(); /* remember current char */ |
| 8733 | --curwin->w_cursor.lnum; |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8734 | |
| 8735 | /* When "aw" is in 'formatoptions' we must delete the space at |
| 8736 | * the end of the line, otherwise the line will be broken |
| 8737 | * again when auto-formatting. */ |
| 8738 | if (has_format_option(FO_AUTO) |
| 8739 | && has_format_option(FO_WHITE_PAR)) |
| 8740 | { |
| 8741 | char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum, |
| 8742 | TRUE); |
| 8743 | int len; |
| 8744 | |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 8745 | len = (int)STRLEN(ptr); |
Bram Moolenaar | c930a3c | 2005-05-20 21:27:20 +0000 | [diff] [blame] | 8746 | if (len > 0 && ptr[len - 1] == ' ') |
| 8747 | ptr[len - 1] = NUL; |
| 8748 | } |
| 8749 | |
Bram Moolenaar | 893eaab | 2010-07-10 17:51:46 +0200 | [diff] [blame] | 8750 | (void)do_join(2, FALSE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8751 | if (temp == NUL && gchar_cursor() != NUL) |
| 8752 | inc_cursor(); |
| 8753 | } |
| 8754 | #ifdef FEAT_VREPLACE |
| 8755 | else |
| 8756 | dec_cursor(); |
| 8757 | #endif |
| 8758 | |
| 8759 | /* |
| 8760 | * In REPLACE mode we have to put back the text that was replaced |
| 8761 | * by the NL. On the replace stack is first a NUL-terminated |
| 8762 | * sequence of characters that were deleted and then the |
| 8763 | * characters that NL replaced. |
| 8764 | */ |
| 8765 | if (State & REPLACE_FLAG) |
| 8766 | { |
| 8767 | /* |
| 8768 | * Do the next ins_char() in NORMAL state, to |
| 8769 | * prevent ins_char() from replacing characters and |
| 8770 | * avoiding showmatch(). |
| 8771 | */ |
| 8772 | oldState = State; |
| 8773 | State = NORMAL; |
| 8774 | /* |
| 8775 | * restore characters (blanks) deleted after cursor |
| 8776 | */ |
| 8777 | while (cc > 0) |
| 8778 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8779 | save_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8780 | #ifdef FEAT_MBYTE |
| 8781 | mb_replace_pop_ins(cc); |
| 8782 | #else |
| 8783 | ins_char(cc); |
| 8784 | #endif |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8785 | curwin->w_cursor.col = save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8786 | cc = replace_pop(); |
| 8787 | } |
| 8788 | /* restore the characters that NL replaced */ |
| 8789 | replace_pop_ins(); |
| 8790 | State = oldState; |
| 8791 | } |
| 8792 | } |
| 8793 | did_ai = FALSE; |
| 8794 | } |
| 8795 | else |
| 8796 | { |
| 8797 | /* |
| 8798 | * Delete character(s) before the cursor. |
| 8799 | */ |
| 8800 | #ifdef FEAT_RIGHTLEFT |
| 8801 | if (revins_on) /* put cursor on last inserted char */ |
| 8802 | dec_cursor(); |
| 8803 | #endif |
| 8804 | mincol = 0; |
| 8805 | /* keep indent */ |
Bram Moolenaar | 9248e6e | 2007-03-08 12:10:13 +0000 | [diff] [blame] | 8806 | if (mode == BACKSPACE_LINE |
| 8807 | && (curbuf->b_p_ai |
| 8808 | #ifdef FEAT_CINDENT |
Bram Moolenaar | 97b9810 | 2009-11-17 16:41:01 +0000 | [diff] [blame] | 8809 | || cindent_on() |
Bram Moolenaar | 9248e6e | 2007-03-08 12:10:13 +0000 | [diff] [blame] | 8810 | #endif |
| 8811 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8812 | #ifdef FEAT_RIGHTLEFT |
| 8813 | && !revins_on |
| 8814 | #endif |
| 8815 | ) |
| 8816 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8817 | save_col = curwin->w_cursor.col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8818 | beginline(BL_WHITE); |
Bram Moolenaar | 7667556 | 2009-11-11 12:22:32 +0000 | [diff] [blame] | 8819 | if (curwin->w_cursor.col < save_col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8820 | mincol = curwin->w_cursor.col; |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8821 | curwin->w_cursor.col = save_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8822 | } |
| 8823 | |
| 8824 | /* |
| 8825 | * Handle deleting one 'shiftwidth' or 'softtabstop'. |
| 8826 | */ |
| 8827 | if ( mode == BACKSPACE_CHAR |
| 8828 | && ((p_sta && in_indent) |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 8829 | || (curbuf->b_p_sts != 0 |
Bram Moolenaar | 7b88a0e | 2008-01-09 09:14:13 +0000 | [diff] [blame] | 8830 | && curwin->w_cursor.col > 0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8831 | && (*(ml_get_cursor() - 1) == TAB |
| 8832 | || (*(ml_get_cursor() - 1) == ' ' |
| 8833 | && (!*inserted_space_p |
| 8834 | || arrow_used)))))) |
| 8835 | { |
| 8836 | int ts; |
| 8837 | colnr_T vcol; |
| 8838 | colnr_T want_vcol; |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8839 | colnr_T start_vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8840 | |
| 8841 | *inserted_space_p = FALSE; |
Bram Moolenaar | 280f126 | 2006-01-30 00:14:18 +0000 | [diff] [blame] | 8842 | if (p_sta && in_indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8843 | ts = curbuf->b_p_sw; |
| 8844 | else |
| 8845 | ts = curbuf->b_p_sts; |
| 8846 | /* Compute the virtual column where we want to be. Since |
| 8847 | * 'showbreak' may get in the way, need to get the last column of |
| 8848 | * the previous character. */ |
| 8849 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8850 | start_vcol = vcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8851 | dec_cursor(); |
| 8852 | getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol); |
| 8853 | inc_cursor(); |
| 8854 | want_vcol = (want_vcol / ts) * ts; |
| 8855 | |
| 8856 | /* delete characters until we are at or before want_vcol */ |
| 8857 | while (vcol > want_vcol |
| 8858 | && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc))) |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8859 | ins_bs_one(&vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8860 | |
| 8861 | /* insert extra spaces until we are at want_vcol */ |
| 8862 | while (vcol < want_vcol) |
| 8863 | { |
| 8864 | /* Remember the first char we inserted */ |
| 8865 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8866 | && curwin->w_cursor.col < Insstart.col) |
| 8867 | Insstart.col = curwin->w_cursor.col; |
| 8868 | |
| 8869 | #ifdef FEAT_VREPLACE |
| 8870 | if (State & VREPLACE_FLAG) |
| 8871 | ins_char(' '); |
| 8872 | else |
| 8873 | #endif |
| 8874 | { |
| 8875 | ins_str((char_u *)" "); |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8876 | if ((State & REPLACE_FLAG)) |
| 8877 | replace_push(NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8878 | } |
| 8879 | getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL); |
| 8880 | } |
Bram Moolenaar | f5dcf7c | 2007-12-09 19:26:44 +0000 | [diff] [blame] | 8881 | |
| 8882 | /* If we are now back where we started delete one character. Can |
| 8883 | * happen when using 'sts' and 'linebreak'. */ |
| 8884 | if (vcol >= start_vcol) |
| 8885 | ins_bs_one(&vcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8886 | } |
| 8887 | |
| 8888 | /* |
| 8889 | * Delete upto starting point, start of line or previous word. |
| 8890 | */ |
| 8891 | else do |
| 8892 | { |
| 8893 | #ifdef FEAT_RIGHTLEFT |
| 8894 | if (!revins_on) /* put cursor on char to be deleted */ |
| 8895 | #endif |
| 8896 | dec_cursor(); |
| 8897 | |
| 8898 | /* start of word? */ |
| 8899 | if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor())) |
| 8900 | { |
| 8901 | mode = BACKSPACE_WORD_NOT_SPACE; |
| 8902 | temp = vim_iswordc(gchar_cursor()); |
| 8903 | } |
| 8904 | /* end of word? */ |
| 8905 | else if (mode == BACKSPACE_WORD_NOT_SPACE |
| 8906 | && (vim_isspace(cc = gchar_cursor()) |
| 8907 | || vim_iswordc(cc) != temp)) |
| 8908 | { |
| 8909 | #ifdef FEAT_RIGHTLEFT |
| 8910 | if (!revins_on) |
| 8911 | #endif |
| 8912 | inc_cursor(); |
| 8913 | #ifdef FEAT_RIGHTLEFT |
| 8914 | else if (State & REPLACE_FLAG) |
| 8915 | dec_cursor(); |
| 8916 | #endif |
| 8917 | break; |
| 8918 | } |
| 8919 | if (State & REPLACE_FLAG) |
Bram Moolenaar | 0f6c948 | 2009-01-13 11:29:48 +0000 | [diff] [blame] | 8920 | replace_do_bs(-1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8921 | else |
| 8922 | { |
| 8923 | #ifdef FEAT_MBYTE |
| 8924 | if (enc_utf8 && p_deco) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8925 | (void)utfc_ptr2char(ml_get_cursor(), cpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8926 | #endif |
| 8927 | (void)del_char(FALSE); |
| 8928 | #ifdef FEAT_MBYTE |
| 8929 | /* |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8930 | * If there are combining characters and 'delcombine' is set |
| 8931 | * move the cursor back. Don't back up before the base |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8932 | * character. |
| 8933 | */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 8934 | if (enc_utf8 && p_deco && cpc[0] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8935 | inc_cursor(); |
| 8936 | #endif |
| 8937 | #ifdef FEAT_RIGHTLEFT |
| 8938 | if (revins_chars) |
| 8939 | { |
| 8940 | revins_chars--; |
| 8941 | revins_legal++; |
| 8942 | } |
| 8943 | if (revins_on && gchar_cursor() == NUL) |
| 8944 | break; |
| 8945 | #endif |
| 8946 | } |
| 8947 | /* Just a single backspace?: */ |
| 8948 | if (mode == BACKSPACE_CHAR) |
| 8949 | break; |
| 8950 | } while ( |
| 8951 | #ifdef FEAT_RIGHTLEFT |
| 8952 | revins_on || |
| 8953 | #endif |
| 8954 | (curwin->w_cursor.col > mincol |
| 8955 | && (curwin->w_cursor.lnum != Insstart.lnum |
| 8956 | || curwin->w_cursor.col != Insstart.col))); |
| 8957 | did_backspace = TRUE; |
| 8958 | } |
| 8959 | #ifdef FEAT_SMARTINDENT |
| 8960 | did_si = FALSE; |
| 8961 | can_si = FALSE; |
| 8962 | can_si_back = FALSE; |
| 8963 | #endif |
| 8964 | if (curwin->w_cursor.col <= 1) |
| 8965 | did_ai = FALSE; |
| 8966 | /* |
| 8967 | * It's a little strange to put backspaces into the redo |
| 8968 | * buffer, but it makes auto-indent a lot easier to deal |
| 8969 | * with. |
| 8970 | */ |
| 8971 | AppendCharToRedobuff(c); |
| 8972 | |
| 8973 | /* If deleted before the insertion point, adjust it */ |
| 8974 | if (curwin->w_cursor.lnum == Insstart.lnum |
| 8975 | && curwin->w_cursor.col < Insstart.col) |
| 8976 | Insstart.col = curwin->w_cursor.col; |
| 8977 | |
| 8978 | /* vi behaviour: the cursor moves backward but the character that |
| 8979 | * was there remains visible |
| 8980 | * Vim behaviour: the cursor moves backward and the character that |
| 8981 | * was there is erased from the screen. |
| 8982 | * We can emulate the vi behaviour by pretending there is a dollar |
| 8983 | * displayed even when there isn't. |
| 8984 | * --pkv Sun Jan 19 01:56:40 EST 2003 */ |
Bram Moolenaar | 76b9b36 | 2012-02-04 23:35:00 +0100 | [diff] [blame] | 8985 | if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8986 | dollar_vcol = curwin->w_virtcol; |
| 8987 | |
Bram Moolenaar | ce3be47 | 2008-01-14 19:12:28 +0000 | [diff] [blame] | 8988 | #ifdef FEAT_FOLDING |
| 8989 | /* When deleting a char the cursor line must never be in a closed fold. |
| 8990 | * E.g., when 'foldmethod' is indent and deleting the first non-white |
| 8991 | * char before a Tab. */ |
| 8992 | if (did_backspace) |
| 8993 | foldOpenCursor(); |
| 8994 | #endif |
| 8995 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8996 | return did_backspace; |
| 8997 | } |
| 8998 | |
| 8999 | #ifdef FEAT_MOUSE |
| 9000 | static void |
| 9001 | ins_mouse(c) |
| 9002 | int c; |
| 9003 | { |
| 9004 | pos_T tpos; |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 9005 | win_T *old_curwin = curwin; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9006 | |
| 9007 | # ifdef FEAT_GUI |
| 9008 | /* When GUI is active, also move/paste when 'mouse' is empty */ |
| 9009 | if (!gui.in_use) |
| 9010 | # endif |
| 9011 | if (!mouse_has(MOUSE_INSERT)) |
| 9012 | return; |
| 9013 | |
| 9014 | undisplay_dollar(); |
| 9015 | tpos = curwin->w_cursor; |
| 9016 | if (do_mouse(NULL, c, BACKWARD, 1L, 0)) |
| 9017 | { |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 9018 | #ifdef FEAT_WINDOWS |
| 9019 | win_T *new_curwin = curwin; |
| 9020 | |
| 9021 | if (curwin != old_curwin && win_valid(old_curwin)) |
| 9022 | { |
| 9023 | /* Mouse took us to another window. We need to go back to the |
| 9024 | * previous one to stop insert there properly. */ |
| 9025 | curwin = old_curwin; |
| 9026 | curbuf = curwin->w_buffer; |
| 9027 | } |
| 9028 | #endif |
| 9029 | start_arrow(curwin == old_curwin ? &tpos : NULL); |
| 9030 | #ifdef FEAT_WINDOWS |
| 9031 | if (curwin != new_curwin && win_valid(new_curwin)) |
| 9032 | { |
| 9033 | curwin = new_curwin; |
| 9034 | curbuf = curwin->w_buffer; |
| 9035 | } |
| 9036 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9037 | # ifdef FEAT_CINDENT |
| 9038 | can_cindent = TRUE; |
| 9039 | # endif |
| 9040 | } |
| 9041 | |
| 9042 | #ifdef FEAT_WINDOWS |
| 9043 | /* redraw status lines (in case another window became active) */ |
| 9044 | redraw_statuslines(); |
| 9045 | #endif |
| 9046 | } |
| 9047 | |
| 9048 | static void |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9049 | ins_mousescroll(dir) |
| 9050 | int dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9051 | { |
| 9052 | pos_T tpos; |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9053 | # if defined(FEAT_WINDOWS) |
| 9054 | win_T *old_curwin = curwin; |
| 9055 | # endif |
| 9056 | # ifdef FEAT_INS_EXPAND |
| 9057 | int did_scroll = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9058 | # endif |
| 9059 | |
| 9060 | tpos = curwin->w_cursor; |
| 9061 | |
| 9062 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9063 | /* Currently the mouse coordinates are only known in the GUI. */ |
| 9064 | if (gui.in_use && mouse_row >= 0 && mouse_col >= 0) |
| 9065 | { |
| 9066 | int row, col; |
| 9067 | |
| 9068 | row = mouse_row; |
| 9069 | col = mouse_col; |
| 9070 | |
| 9071 | /* find the window at the pointer coordinates */ |
| 9072 | curwin = mouse_find_win(&row, &col); |
| 9073 | curbuf = curwin->w_buffer; |
| 9074 | } |
| 9075 | if (curwin == old_curwin) |
| 9076 | # endif |
| 9077 | undisplay_dollar(); |
| 9078 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9079 | # ifdef FEAT_INS_EXPAND |
| 9080 | /* Don't scroll the window in which completion is being done. */ |
| 9081 | if (!pum_visible() |
| 9082 | # if defined(FEAT_WINDOWS) |
| 9083 | || curwin != old_curwin |
| 9084 | # endif |
| 9085 | ) |
| 9086 | # endif |
| 9087 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9088 | if (dir == MSCR_DOWN || dir == MSCR_UP) |
| 9089 | { |
| 9090 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 9091 | scroll_redraw(dir, |
| 9092 | (long)(curwin->w_botline - curwin->w_topline)); |
| 9093 | else |
| 9094 | scroll_redraw(dir, 3L); |
| 9095 | } |
| 9096 | #ifdef FEAT_GUI |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9097 | else |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9098 | { |
| 9099 | int val, step = 6; |
| 9100 | |
| 9101 | if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL)) |
| 9102 | step = W_WIDTH(curwin); |
| 9103 | val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step); |
| 9104 | if (val < 0) |
| 9105 | val = 0; |
| 9106 | gui_do_horiz_scroll(val, TRUE); |
| 9107 | } |
| 9108 | #endif |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9109 | # ifdef FEAT_INS_EXPAND |
| 9110 | did_scroll = TRUE; |
| 9111 | # endif |
| 9112 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9113 | |
| 9114 | # if defined(FEAT_GUI) && defined(FEAT_WINDOWS) |
| 9115 | curwin->w_redr_status = TRUE; |
| 9116 | |
| 9117 | curwin = old_curwin; |
| 9118 | curbuf = curwin->w_buffer; |
| 9119 | # endif |
| 9120 | |
Bram Moolenaar | 9b25ffb | 2007-11-06 21:27:31 +0000 | [diff] [blame] | 9121 | # ifdef FEAT_INS_EXPAND |
| 9122 | /* The popup menu may overlay the window, need to redraw it. |
| 9123 | * TODO: Would be more efficient to only redraw the windows that are |
| 9124 | * overlapped by the popup menu. */ |
| 9125 | if (pum_visible() && did_scroll) |
| 9126 | { |
| 9127 | redraw_all_later(NOT_VALID); |
| 9128 | ins_compl_show_pum(); |
| 9129 | } |
| 9130 | # endif |
| 9131 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9132 | if (!equalpos(curwin->w_cursor, tpos)) |
| 9133 | { |
| 9134 | start_arrow(&tpos); |
| 9135 | # ifdef FEAT_CINDENT |
| 9136 | can_cindent = TRUE; |
| 9137 | # endif |
| 9138 | } |
| 9139 | } |
| 9140 | #endif |
| 9141 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9142 | #if defined(FEAT_GUI_TABLINE) || defined(PROTO) |
Bram Moolenaar | a94bc43 | 2006-03-10 21:42:59 +0000 | [diff] [blame] | 9143 | static void |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9144 | ins_tabline(c) |
| 9145 | int c; |
| 9146 | { |
| 9147 | /* We will be leaving the current window, unless closing another tab. */ |
| 9148 | if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE |
| 9149 | || (current_tab != 0 && current_tab != tabpage_index(curtab))) |
| 9150 | { |
| 9151 | undisplay_dollar(); |
| 9152 | start_arrow(&curwin->w_cursor); |
| 9153 | # ifdef FEAT_CINDENT |
| 9154 | can_cindent = TRUE; |
| 9155 | # endif |
| 9156 | } |
| 9157 | |
| 9158 | if (c == K_TABLINE) |
| 9159 | goto_tabpage(current_tab); |
| 9160 | else |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 9161 | { |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9162 | handle_tabmenu(); |
Bram Moolenaar | 437df8f | 2006-04-27 21:47:44 +0000 | [diff] [blame] | 9163 | redraw_statuslines(); /* will redraw the tabline when needed */ |
| 9164 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 9165 | } |
| 9166 | #endif |
| 9167 | |
| 9168 | #if defined(FEAT_GUI) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9169 | void |
| 9170 | ins_scroll() |
| 9171 | { |
| 9172 | pos_T tpos; |
| 9173 | |
| 9174 | undisplay_dollar(); |
| 9175 | tpos = curwin->w_cursor; |
| 9176 | if (gui_do_scroll()) |
| 9177 | { |
| 9178 | start_arrow(&tpos); |
| 9179 | # ifdef FEAT_CINDENT |
| 9180 | can_cindent = TRUE; |
| 9181 | # endif |
| 9182 | } |
| 9183 | } |
| 9184 | |
| 9185 | void |
| 9186 | ins_horscroll() |
| 9187 | { |
| 9188 | pos_T tpos; |
| 9189 | |
| 9190 | undisplay_dollar(); |
| 9191 | tpos = curwin->w_cursor; |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 9192 | if (gui_do_horiz_scroll(scrollbar_value, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9193 | { |
| 9194 | start_arrow(&tpos); |
| 9195 | # ifdef FEAT_CINDENT |
| 9196 | can_cindent = TRUE; |
| 9197 | # endif |
| 9198 | } |
| 9199 | } |
| 9200 | #endif |
| 9201 | |
| 9202 | static void |
| 9203 | ins_left() |
| 9204 | { |
| 9205 | pos_T tpos; |
| 9206 | |
| 9207 | #ifdef FEAT_FOLDING |
| 9208 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9209 | foldOpenCursor(); |
| 9210 | #endif |
| 9211 | undisplay_dollar(); |
| 9212 | tpos = curwin->w_cursor; |
| 9213 | if (oneleft() == OK) |
| 9214 | { |
Bram Moolenaar | 39fecab | 2006-08-29 14:07:36 +0000 | [diff] [blame] | 9215 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 9216 | /* Only call start_arrow() when not busy with preediting, it will |
| 9217 | * break undo. K_LEFT is inserted in im_correct_cursor(). */ |
| 9218 | if (!im_is_preediting()) |
| 9219 | #endif |
| 9220 | start_arrow(&tpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9221 | #ifdef FEAT_RIGHTLEFT |
| 9222 | /* If exit reversed string, position is fixed */ |
| 9223 | if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol) |
| 9224 | revins_legal++; |
| 9225 | revins_chars++; |
| 9226 | #endif |
| 9227 | } |
| 9228 | |
| 9229 | /* |
| 9230 | * if 'whichwrap' set for cursor in insert mode may go to |
| 9231 | * previous line |
| 9232 | */ |
| 9233 | else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1) |
| 9234 | { |
| 9235 | start_arrow(&tpos); |
| 9236 | --(curwin->w_cursor.lnum); |
| 9237 | coladvance((colnr_T)MAXCOL); |
| 9238 | curwin->w_set_curswant = TRUE; /* so we stay at the end */ |
| 9239 | } |
| 9240 | else |
| 9241 | vim_beep(); |
| 9242 | } |
| 9243 | |
| 9244 | static void |
| 9245 | ins_home(c) |
| 9246 | int c; |
| 9247 | { |
| 9248 | pos_T tpos; |
| 9249 | |
| 9250 | #ifdef FEAT_FOLDING |
| 9251 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9252 | foldOpenCursor(); |
| 9253 | #endif |
| 9254 | undisplay_dollar(); |
| 9255 | tpos = curwin->w_cursor; |
| 9256 | if (c == K_C_HOME) |
| 9257 | curwin->w_cursor.lnum = 1; |
| 9258 | curwin->w_cursor.col = 0; |
| 9259 | #ifdef FEAT_VIRTUALEDIT |
| 9260 | curwin->w_cursor.coladd = 0; |
| 9261 | #endif |
| 9262 | curwin->w_curswant = 0; |
| 9263 | start_arrow(&tpos); |
| 9264 | } |
| 9265 | |
| 9266 | static void |
| 9267 | ins_end(c) |
| 9268 | int c; |
| 9269 | { |
| 9270 | pos_T tpos; |
| 9271 | |
| 9272 | #ifdef FEAT_FOLDING |
| 9273 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9274 | foldOpenCursor(); |
| 9275 | #endif |
| 9276 | undisplay_dollar(); |
| 9277 | tpos = curwin->w_cursor; |
| 9278 | if (c == K_C_END) |
| 9279 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 9280 | coladvance((colnr_T)MAXCOL); |
| 9281 | curwin->w_curswant = MAXCOL; |
| 9282 | |
| 9283 | start_arrow(&tpos); |
| 9284 | } |
| 9285 | |
| 9286 | static void |
| 9287 | ins_s_left() |
| 9288 | { |
| 9289 | #ifdef FEAT_FOLDING |
| 9290 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9291 | foldOpenCursor(); |
| 9292 | #endif |
| 9293 | undisplay_dollar(); |
| 9294 | if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0) |
| 9295 | { |
| 9296 | start_arrow(&curwin->w_cursor); |
| 9297 | (void)bck_word(1L, FALSE, FALSE); |
| 9298 | curwin->w_set_curswant = TRUE; |
| 9299 | } |
| 9300 | else |
| 9301 | vim_beep(); |
| 9302 | } |
| 9303 | |
| 9304 | static void |
| 9305 | ins_right() |
| 9306 | { |
| 9307 | #ifdef FEAT_FOLDING |
| 9308 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9309 | foldOpenCursor(); |
| 9310 | #endif |
| 9311 | undisplay_dollar(); |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 9312 | if (gchar_cursor() != NUL |
| 9313 | #ifdef FEAT_VIRTUALEDIT |
| 9314 | || virtual_active() |
| 9315 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9316 | ) |
| 9317 | { |
| 9318 | start_arrow(&curwin->w_cursor); |
| 9319 | curwin->w_set_curswant = TRUE; |
| 9320 | #ifdef FEAT_VIRTUALEDIT |
| 9321 | if (virtual_active()) |
| 9322 | oneright(); |
| 9323 | else |
| 9324 | #endif |
| 9325 | { |
| 9326 | #ifdef FEAT_MBYTE |
| 9327 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 9328 | curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor()); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9329 | else |
| 9330 | #endif |
| 9331 | ++curwin->w_cursor.col; |
| 9332 | } |
| 9333 | |
| 9334 | #ifdef FEAT_RIGHTLEFT |
| 9335 | revins_legal++; |
| 9336 | if (revins_chars) |
| 9337 | revins_chars--; |
| 9338 | #endif |
| 9339 | } |
| 9340 | /* if 'whichwrap' set for cursor in insert mode, may move the |
| 9341 | * cursor to the next line */ |
| 9342 | else if (vim_strchr(p_ww, ']') != NULL |
| 9343 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 9344 | { |
| 9345 | start_arrow(&curwin->w_cursor); |
| 9346 | curwin->w_set_curswant = TRUE; |
| 9347 | ++curwin->w_cursor.lnum; |
| 9348 | curwin->w_cursor.col = 0; |
| 9349 | } |
| 9350 | else |
| 9351 | vim_beep(); |
| 9352 | } |
| 9353 | |
| 9354 | static void |
| 9355 | ins_s_right() |
| 9356 | { |
| 9357 | #ifdef FEAT_FOLDING |
| 9358 | if ((fdo_flags & FDO_HOR) && KeyTyped) |
| 9359 | foldOpenCursor(); |
| 9360 | #endif |
| 9361 | undisplay_dollar(); |
| 9362 | if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count |
| 9363 | || gchar_cursor() != NUL) |
| 9364 | { |
| 9365 | start_arrow(&curwin->w_cursor); |
| 9366 | (void)fwd_word(1L, FALSE, 0); |
| 9367 | curwin->w_set_curswant = TRUE; |
| 9368 | } |
| 9369 | else |
| 9370 | vim_beep(); |
| 9371 | } |
| 9372 | |
| 9373 | static void |
| 9374 | ins_up(startcol) |
| 9375 | int startcol; /* when TRUE move to Insstart.col */ |
| 9376 | { |
| 9377 | pos_T tpos; |
| 9378 | linenr_T old_topline = curwin->w_topline; |
| 9379 | #ifdef FEAT_DIFF |
| 9380 | int old_topfill = curwin->w_topfill; |
| 9381 | #endif |
| 9382 | |
| 9383 | undisplay_dollar(); |
| 9384 | tpos = curwin->w_cursor; |
| 9385 | if (cursor_up(1L, TRUE) == OK) |
| 9386 | { |
| 9387 | if (startcol) |
| 9388 | coladvance(getvcol_nolist(&Insstart)); |
| 9389 | if (old_topline != curwin->w_topline |
| 9390 | #ifdef FEAT_DIFF |
| 9391 | || old_topfill != curwin->w_topfill |
| 9392 | #endif |
| 9393 | ) |
| 9394 | redraw_later(VALID); |
| 9395 | start_arrow(&tpos); |
| 9396 | #ifdef FEAT_CINDENT |
| 9397 | can_cindent = TRUE; |
| 9398 | #endif |
| 9399 | } |
| 9400 | else |
| 9401 | vim_beep(); |
| 9402 | } |
| 9403 | |
| 9404 | static void |
| 9405 | ins_pageup() |
| 9406 | { |
| 9407 | pos_T tpos; |
| 9408 | |
| 9409 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9410 | |
| 9411 | #ifdef FEAT_WINDOWS |
| 9412 | if (mod_mask & MOD_MASK_CTRL) |
| 9413 | { |
| 9414 | /* <C-PageUp>: tab page back */ |
Bram Moolenaar | bc44482 | 2006-10-17 11:37:50 +0000 | [diff] [blame] | 9415 | if (first_tabpage->tp_next != NULL) |
| 9416 | { |
| 9417 | start_arrow(&curwin->w_cursor); |
| 9418 | goto_tabpage(-1); |
| 9419 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9420 | return; |
| 9421 | } |
| 9422 | #endif |
| 9423 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9424 | tpos = curwin->w_cursor; |
| 9425 | if (onepage(BACKWARD, 1L) == OK) |
| 9426 | { |
| 9427 | start_arrow(&tpos); |
| 9428 | #ifdef FEAT_CINDENT |
| 9429 | can_cindent = TRUE; |
| 9430 | #endif |
| 9431 | } |
| 9432 | else |
| 9433 | vim_beep(); |
| 9434 | } |
| 9435 | |
| 9436 | static void |
| 9437 | ins_down(startcol) |
| 9438 | int startcol; /* when TRUE move to Insstart.col */ |
| 9439 | { |
| 9440 | pos_T tpos; |
| 9441 | linenr_T old_topline = curwin->w_topline; |
| 9442 | #ifdef FEAT_DIFF |
| 9443 | int old_topfill = curwin->w_topfill; |
| 9444 | #endif |
| 9445 | |
| 9446 | undisplay_dollar(); |
| 9447 | tpos = curwin->w_cursor; |
| 9448 | if (cursor_down(1L, TRUE) == OK) |
| 9449 | { |
| 9450 | if (startcol) |
| 9451 | coladvance(getvcol_nolist(&Insstart)); |
| 9452 | if (old_topline != curwin->w_topline |
| 9453 | #ifdef FEAT_DIFF |
| 9454 | || old_topfill != curwin->w_topfill |
| 9455 | #endif |
| 9456 | ) |
| 9457 | redraw_later(VALID); |
| 9458 | start_arrow(&tpos); |
| 9459 | #ifdef FEAT_CINDENT |
| 9460 | can_cindent = TRUE; |
| 9461 | #endif |
| 9462 | } |
| 9463 | else |
| 9464 | vim_beep(); |
| 9465 | } |
| 9466 | |
| 9467 | static void |
| 9468 | ins_pagedown() |
| 9469 | { |
| 9470 | pos_T tpos; |
| 9471 | |
| 9472 | undisplay_dollar(); |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9473 | |
| 9474 | #ifdef FEAT_WINDOWS |
| 9475 | if (mod_mask & MOD_MASK_CTRL) |
| 9476 | { |
| 9477 | /* <C-PageDown>: tab page forward */ |
Bram Moolenaar | bc44482 | 2006-10-17 11:37:50 +0000 | [diff] [blame] | 9478 | if (first_tabpage->tp_next != NULL) |
| 9479 | { |
| 9480 | start_arrow(&curwin->w_cursor); |
| 9481 | goto_tabpage(0); |
| 9482 | } |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 9483 | return; |
| 9484 | } |
| 9485 | #endif |
| 9486 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9487 | tpos = curwin->w_cursor; |
| 9488 | if (onepage(FORWARD, 1L) == OK) |
| 9489 | { |
| 9490 | start_arrow(&tpos); |
| 9491 | #ifdef FEAT_CINDENT |
| 9492 | can_cindent = TRUE; |
| 9493 | #endif |
| 9494 | } |
| 9495 | else |
| 9496 | vim_beep(); |
| 9497 | } |
| 9498 | |
| 9499 | #ifdef FEAT_DND |
| 9500 | static void |
| 9501 | ins_drop() |
| 9502 | { |
| 9503 | do_put('~', BACKWARD, 1L, PUT_CURSEND); |
| 9504 | } |
| 9505 | #endif |
| 9506 | |
| 9507 | /* |
| 9508 | * Handle TAB in Insert or Replace mode. |
| 9509 | * Return TRUE when the TAB needs to be inserted like a normal character. |
| 9510 | */ |
| 9511 | static int |
| 9512 | ins_tab() |
| 9513 | { |
| 9514 | int ind; |
| 9515 | int i; |
| 9516 | int temp; |
| 9517 | |
| 9518 | if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum) |
| 9519 | Insstart_blank_vcol = get_nolist_virtcol(); |
| 9520 | if (echeck_abbr(TAB + ABBR_OFF)) |
| 9521 | return FALSE; |
| 9522 | |
| 9523 | ind = inindent(0); |
| 9524 | #ifdef FEAT_CINDENT |
| 9525 | if (ind) |
| 9526 | can_cindent = FALSE; |
| 9527 | #endif |
| 9528 | |
| 9529 | /* |
| 9530 | * When nothing special, insert TAB like a normal character |
| 9531 | */ |
| 9532 | if (!curbuf->b_p_et |
| 9533 | && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw) |
| 9534 | && curbuf->b_p_sts == 0) |
| 9535 | return TRUE; |
| 9536 | |
| 9537 | if (stop_arrow() == FAIL) |
| 9538 | return TRUE; |
| 9539 | |
| 9540 | did_ai = FALSE; |
| 9541 | #ifdef FEAT_SMARTINDENT |
| 9542 | did_si = FALSE; |
| 9543 | can_si = FALSE; |
| 9544 | can_si_back = FALSE; |
| 9545 | #endif |
| 9546 | AppendToRedobuff((char_u *)"\t"); |
| 9547 | |
| 9548 | if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */ |
| 9549 | temp = (int)curbuf->b_p_sw; |
| 9550 | else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */ |
| 9551 | temp = (int)curbuf->b_p_sts; |
| 9552 | else /* otherwise use 'tabstop' */ |
| 9553 | temp = (int)curbuf->b_p_ts; |
| 9554 | temp -= get_nolist_virtcol() % temp; |
| 9555 | |
| 9556 | /* |
| 9557 | * Insert the first space with ins_char(). It will delete one char in |
| 9558 | * replace mode. Insert the rest with ins_str(); it will not delete any |
| 9559 | * chars. For VREPLACE mode, we use ins_char() for all characters. |
| 9560 | */ |
| 9561 | ins_char(' '); |
| 9562 | while (--temp > 0) |
| 9563 | { |
| 9564 | #ifdef FEAT_VREPLACE |
| 9565 | if (State & VREPLACE_FLAG) |
| 9566 | ins_char(' '); |
| 9567 | else |
| 9568 | #endif |
| 9569 | { |
| 9570 | ins_str((char_u *)" "); |
| 9571 | if (State & REPLACE_FLAG) /* no char replaced */ |
| 9572 | replace_push(NUL); |
| 9573 | } |
| 9574 | } |
| 9575 | |
| 9576 | /* |
| 9577 | * When 'expandtab' not set: Replace spaces by TABs where possible. |
| 9578 | */ |
| 9579 | if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind))) |
| 9580 | { |
| 9581 | char_u *ptr; |
| 9582 | #ifdef FEAT_VREPLACE |
| 9583 | char_u *saved_line = NULL; /* init for GCC */ |
| 9584 | pos_T pos; |
| 9585 | #endif |
| 9586 | pos_T fpos; |
| 9587 | pos_T *cursor; |
| 9588 | colnr_T want_vcol, vcol; |
| 9589 | int change_col = -1; |
| 9590 | int save_list = curwin->w_p_list; |
| 9591 | |
| 9592 | /* |
| 9593 | * Get the current line. For VREPLACE mode, don't make real changes |
| 9594 | * yet, just work on a copy of the line. |
| 9595 | */ |
| 9596 | #ifdef FEAT_VREPLACE |
| 9597 | if (State & VREPLACE_FLAG) |
| 9598 | { |
| 9599 | pos = curwin->w_cursor; |
| 9600 | cursor = &pos; |
| 9601 | saved_line = vim_strsave(ml_get_curline()); |
| 9602 | if (saved_line == NULL) |
| 9603 | return FALSE; |
| 9604 | ptr = saved_line + pos.col; |
| 9605 | } |
| 9606 | else |
| 9607 | #endif |
| 9608 | { |
| 9609 | ptr = ml_get_cursor(); |
| 9610 | cursor = &curwin->w_cursor; |
| 9611 | } |
| 9612 | |
| 9613 | /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */ |
| 9614 | if (vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 9615 | curwin->w_p_list = FALSE; |
| 9616 | |
| 9617 | /* Find first white before the cursor */ |
| 9618 | fpos = curwin->w_cursor; |
| 9619 | while (fpos.col > 0 && vim_iswhite(ptr[-1])) |
| 9620 | { |
| 9621 | --fpos.col; |
| 9622 | --ptr; |
| 9623 | } |
| 9624 | |
| 9625 | /* In Replace mode, don't change characters before the insert point. */ |
| 9626 | if ((State & REPLACE_FLAG) |
| 9627 | && fpos.lnum == Insstart.lnum |
| 9628 | && fpos.col < Insstart.col) |
| 9629 | { |
| 9630 | ptr += Insstart.col - fpos.col; |
| 9631 | fpos.col = Insstart.col; |
| 9632 | } |
| 9633 | |
| 9634 | /* compute virtual column numbers of first white and cursor */ |
| 9635 | getvcol(curwin, &fpos, &vcol, NULL, NULL); |
| 9636 | getvcol(curwin, cursor, &want_vcol, NULL, NULL); |
| 9637 | |
| 9638 | /* Use as many TABs as possible. Beware of 'showbreak' and |
| 9639 | * 'linebreak' adding extra virtual columns. */ |
| 9640 | while (vim_iswhite(*ptr)) |
| 9641 | { |
| 9642 | i = lbr_chartabsize((char_u *)"\t", vcol); |
| 9643 | if (vcol + i > want_vcol) |
| 9644 | break; |
| 9645 | if (*ptr != TAB) |
| 9646 | { |
| 9647 | *ptr = TAB; |
| 9648 | if (change_col < 0) |
| 9649 | { |
| 9650 | change_col = fpos.col; /* Column of first change */ |
| 9651 | /* May have to adjust Insstart */ |
| 9652 | if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col) |
| 9653 | Insstart.col = fpos.col; |
| 9654 | } |
| 9655 | } |
| 9656 | ++fpos.col; |
| 9657 | ++ptr; |
| 9658 | vcol += i; |
| 9659 | } |
| 9660 | |
| 9661 | if (change_col >= 0) |
| 9662 | { |
| 9663 | int repl_off = 0; |
| 9664 | |
| 9665 | /* Skip over the spaces we need. */ |
| 9666 | while (vcol < want_vcol && *ptr == ' ') |
| 9667 | { |
| 9668 | vcol += lbr_chartabsize(ptr, vcol); |
| 9669 | ++ptr; |
| 9670 | ++repl_off; |
| 9671 | } |
| 9672 | if (vcol > want_vcol) |
| 9673 | { |
| 9674 | /* Must have a char with 'showbreak' just before it. */ |
| 9675 | --ptr; |
| 9676 | --repl_off; |
| 9677 | } |
| 9678 | fpos.col += repl_off; |
| 9679 | |
| 9680 | /* Delete following spaces. */ |
| 9681 | i = cursor->col - fpos.col; |
| 9682 | if (i > 0) |
| 9683 | { |
Bram Moolenaar | c1a11ed | 2008-06-24 22:09:24 +0000 | [diff] [blame] | 9684 | STRMOVE(ptr, ptr + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9685 | /* correct replace stack. */ |
| 9686 | if ((State & REPLACE_FLAG) |
| 9687 | #ifdef FEAT_VREPLACE |
| 9688 | && !(State & VREPLACE_FLAG) |
| 9689 | #endif |
| 9690 | ) |
| 9691 | for (temp = i; --temp >= 0; ) |
| 9692 | replace_join(repl_off); |
| 9693 | } |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9694 | #ifdef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 9695 | if (netbeans_active()) |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9696 | { |
Bram Moolenaar | 67c5384 | 2010-05-22 18:28:27 +0200 | [diff] [blame] | 9697 | netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1)); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 9698 | netbeans_inserted(curbuf, fpos.lnum, cursor->col, |
| 9699 | (char_u *)"\t", 1); |
| 9700 | } |
| 9701 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9702 | cursor->col -= i; |
| 9703 | |
| 9704 | #ifdef FEAT_VREPLACE |
| 9705 | /* |
| 9706 | * In VREPLACE mode, we haven't changed anything yet. Do it now by |
| 9707 | * backspacing over the changed spacing and then inserting the new |
| 9708 | * spacing. |
| 9709 | */ |
| 9710 | if (State & VREPLACE_FLAG) |
| 9711 | { |
| 9712 | /* Backspace from real cursor to change_col */ |
| 9713 | backspace_until_column(change_col); |
| 9714 | |
| 9715 | /* Insert each char in saved_line from changed_col to |
| 9716 | * ptr-cursor */ |
| 9717 | ins_bytes_len(saved_line + change_col, |
| 9718 | cursor->col - change_col); |
| 9719 | } |
| 9720 | #endif |
| 9721 | } |
| 9722 | |
| 9723 | #ifdef FEAT_VREPLACE |
| 9724 | if (State & VREPLACE_FLAG) |
| 9725 | vim_free(saved_line); |
| 9726 | #endif |
| 9727 | curwin->w_p_list = save_list; |
| 9728 | } |
| 9729 | |
| 9730 | return FALSE; |
| 9731 | } |
| 9732 | |
| 9733 | /* |
| 9734 | * Handle CR or NL in insert mode. |
| 9735 | * Return TRUE when out of memory or can't undo. |
| 9736 | */ |
| 9737 | static int |
| 9738 | ins_eol(c) |
| 9739 | int c; |
| 9740 | { |
| 9741 | int i; |
| 9742 | |
| 9743 | if (echeck_abbr(c + ABBR_OFF)) |
| 9744 | return FALSE; |
| 9745 | if (stop_arrow() == FAIL) |
| 9746 | return TRUE; |
| 9747 | undisplay_dollar(); |
| 9748 | |
| 9749 | /* |
| 9750 | * Strange Vi behaviour: In Replace mode, typing a NL will not delete the |
| 9751 | * character under the cursor. Only push a NUL on the replace stack, |
| 9752 | * nothing to put back when the NL is deleted. |
| 9753 | */ |
| 9754 | if ((State & REPLACE_FLAG) |
| 9755 | #ifdef FEAT_VREPLACE |
| 9756 | && !(State & VREPLACE_FLAG) |
| 9757 | #endif |
| 9758 | ) |
| 9759 | replace_push(NUL); |
| 9760 | |
| 9761 | /* |
| 9762 | * In VREPLACE mode, a NL replaces the rest of the line, and starts |
| 9763 | * replacing the next line, so we push all of the characters left on the |
| 9764 | * line onto the replace stack. This is not done here though, it is done |
| 9765 | * in open_line(). |
| 9766 | */ |
| 9767 | |
Bram Moolenaar | f193fff | 2006-04-27 00:02:13 +0000 | [diff] [blame] | 9768 | #ifdef FEAT_VIRTUALEDIT |
| 9769 | /* Put cursor on NUL if on the last char and coladd is 1 (happens after |
| 9770 | * CTRL-O). */ |
| 9771 | if (virtual_active() && curwin->w_cursor.coladd > 0) |
| 9772 | coladvance(getviscol()); |
| 9773 | #endif |
| 9774 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9775 | #ifdef FEAT_RIGHTLEFT |
| 9776 | # ifdef FEAT_FKMAP |
| 9777 | if (p_altkeymap && p_fkmap) |
| 9778 | fkmap(NL); |
| 9779 | # endif |
| 9780 | /* NL in reverse insert will always start in the end of |
| 9781 | * current line. */ |
| 9782 | if (revins_on) |
| 9783 | curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor()); |
| 9784 | #endif |
| 9785 | |
| 9786 | AppendToRedobuff(NL_STR); |
| 9787 | i = open_line(FORWARD, |
| 9788 | #ifdef FEAT_COMMENTS |
| 9789 | has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM : |
| 9790 | #endif |
| 9791 | 0, old_indent); |
| 9792 | old_indent = 0; |
| 9793 | #ifdef FEAT_CINDENT |
| 9794 | can_cindent = TRUE; |
| 9795 | #endif |
Bram Moolenaar | 6ae133b | 2006-11-01 20:25:45 +0000 | [diff] [blame] | 9796 | #ifdef FEAT_FOLDING |
| 9797 | /* When inserting a line the cursor line must never be in a closed fold. */ |
| 9798 | foldOpenCursor(); |
| 9799 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9800 | |
| 9801 | return (!i); |
| 9802 | } |
| 9803 | |
| 9804 | #ifdef FEAT_DIGRAPHS |
| 9805 | /* |
| 9806 | * Handle digraph in insert mode. |
| 9807 | * Returns character still to be inserted, or NUL when nothing remaining to be |
| 9808 | * done. |
| 9809 | */ |
| 9810 | static int |
| 9811 | ins_digraph() |
| 9812 | { |
| 9813 | int c; |
| 9814 | int cc; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9815 | int did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9816 | |
| 9817 | pc_status = PC_STATUS_UNSET; |
| 9818 | if (redrawing() && !char_avail()) |
| 9819 | { |
| 9820 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9821 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9822 | |
| 9823 | edit_putchar('?', TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9824 | did_putchar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9825 | #ifdef FEAT_CMDL_INFO |
| 9826 | add_to_showcmd_c(Ctrl_K); |
| 9827 | #endif |
| 9828 | } |
| 9829 | |
| 9830 | #ifdef USE_ON_FLY_SCROLL |
| 9831 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 9832 | #endif |
| 9833 | |
| 9834 | /* don't map the digraph chars. This also prevents the |
| 9835 | * mode message to be deleted when ESC is hit */ |
| 9836 | ++no_mapping; |
| 9837 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 9838 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9839 | --no_mapping; |
| 9840 | --allow_keys; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9841 | if (did_putchar) |
| 9842 | /* when the line fits in 'columns' the '?' is at the start of the next |
| 9843 | * line and will not be removed by the redraw */ |
| 9844 | edit_unputchar(); |
Bram Moolenaar | 26dcc7e | 2010-07-14 22:35:55 +0200 | [diff] [blame] | 9845 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9846 | if (IS_SPECIAL(c) || mod_mask) /* special key */ |
| 9847 | { |
| 9848 | #ifdef FEAT_CMDL_INFO |
| 9849 | clear_showcmd(); |
| 9850 | #endif |
| 9851 | insert_special(c, TRUE, FALSE); |
| 9852 | return NUL; |
| 9853 | } |
| 9854 | if (c != ESC) |
| 9855 | { |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9856 | did_putchar = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9857 | if (redrawing() && !char_avail()) |
| 9858 | { |
| 9859 | /* may need to redraw when no more chars available now */ |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9860 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9861 | |
| 9862 | if (char2cells(c) == 1) |
| 9863 | { |
Bram Moolenaar | 754b560 | 2006-02-09 23:53:20 +0000 | [diff] [blame] | 9864 | ins_redraw(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9865 | edit_putchar(c, TRUE); |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9866 | did_putchar = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9867 | } |
| 9868 | #ifdef FEAT_CMDL_INFO |
| 9869 | add_to_showcmd_c(c); |
| 9870 | #endif |
| 9871 | } |
| 9872 | ++no_mapping; |
| 9873 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 9874 | cc = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9875 | --no_mapping; |
| 9876 | --allow_keys; |
Bram Moolenaar | 9c520cb | 2011-05-10 14:22:16 +0200 | [diff] [blame] | 9877 | if (did_putchar) |
| 9878 | /* when the line fits in 'columns' the '?' is at the start of the |
| 9879 | * next line and will not be removed by a redraw */ |
| 9880 | edit_unputchar(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9881 | if (cc != ESC) |
| 9882 | { |
| 9883 | AppendToRedobuff((char_u *)CTRL_V_STR); |
| 9884 | c = getdigraph(c, cc, TRUE); |
| 9885 | #ifdef FEAT_CMDL_INFO |
| 9886 | clear_showcmd(); |
| 9887 | #endif |
| 9888 | return c; |
| 9889 | } |
| 9890 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9891 | #ifdef FEAT_CMDL_INFO |
| 9892 | clear_showcmd(); |
| 9893 | #endif |
| 9894 | return NUL; |
| 9895 | } |
| 9896 | #endif |
| 9897 | |
| 9898 | /* |
| 9899 | * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line. |
| 9900 | * Returns the char to be inserted, or NUL if none found. |
| 9901 | */ |
| 9902 | static int |
| 9903 | ins_copychar(lnum) |
| 9904 | linenr_T lnum; |
| 9905 | { |
| 9906 | int c; |
| 9907 | int temp; |
| 9908 | char_u *ptr, *prev_ptr; |
| 9909 | |
| 9910 | if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count) |
| 9911 | { |
| 9912 | vim_beep(); |
| 9913 | return NUL; |
| 9914 | } |
| 9915 | |
| 9916 | /* try to advance to the cursor column */ |
| 9917 | temp = 0; |
| 9918 | ptr = ml_get(lnum); |
| 9919 | prev_ptr = ptr; |
| 9920 | validate_virtcol(); |
| 9921 | while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL) |
| 9922 | { |
| 9923 | prev_ptr = ptr; |
| 9924 | temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp); |
| 9925 | } |
| 9926 | if ((colnr_T)temp > curwin->w_virtcol) |
| 9927 | ptr = prev_ptr; |
| 9928 | |
| 9929 | #ifdef FEAT_MBYTE |
| 9930 | c = (*mb_ptr2char)(ptr); |
| 9931 | #else |
| 9932 | c = *ptr; |
| 9933 | #endif |
| 9934 | if (c == NUL) |
| 9935 | vim_beep(); |
| 9936 | return c; |
| 9937 | } |
| 9938 | |
Bram Moolenaar | 4be06f9 | 2005-07-29 22:36:03 +0000 | [diff] [blame] | 9939 | /* |
| 9940 | * CTRL-Y or CTRL-E typed in Insert mode. |
| 9941 | */ |
| 9942 | static int |
| 9943 | ins_ctrl_ey(tc) |
| 9944 | int tc; |
| 9945 | { |
| 9946 | int c = tc; |
| 9947 | |
| 9948 | #ifdef FEAT_INS_EXPAND |
| 9949 | if (ctrl_x_mode == CTRL_X_SCROLL) |
| 9950 | { |
| 9951 | if (c == Ctrl_Y) |
| 9952 | scrolldown_clamp(); |
| 9953 | else |
| 9954 | scrollup_clamp(); |
| 9955 | redraw_later(VALID); |
| 9956 | } |
| 9957 | else |
| 9958 | #endif |
| 9959 | { |
| 9960 | c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1)); |
| 9961 | if (c != NUL) |
| 9962 | { |
| 9963 | long tw_save; |
| 9964 | |
| 9965 | /* The character must be taken literally, insert like it |
| 9966 | * was typed after a CTRL-V, and pretend 'textwidth' |
| 9967 | * wasn't set. Digits, 'o' and 'x' are special after a |
| 9968 | * CTRL-V, don't use it for these. */ |
| 9969 | if (c < 256 && !isalnum(c)) |
| 9970 | AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */ |
| 9971 | tw_save = curbuf->b_p_tw; |
| 9972 | curbuf->b_p_tw = -1; |
| 9973 | insert_special(c, TRUE, FALSE); |
| 9974 | curbuf->b_p_tw = tw_save; |
| 9975 | #ifdef FEAT_RIGHTLEFT |
| 9976 | revins_chars++; |
| 9977 | revins_legal++; |
| 9978 | #endif |
| 9979 | c = Ctrl_V; /* pretend CTRL-V is last character */ |
| 9980 | auto_format(FALSE, TRUE); |
| 9981 | } |
| 9982 | } |
| 9983 | return c; |
| 9984 | } |
| 9985 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9986 | #ifdef FEAT_SMARTINDENT |
| 9987 | /* |
| 9988 | * Try to do some very smart auto-indenting. |
| 9989 | * Used when inserting a "normal" character. |
| 9990 | */ |
| 9991 | static void |
| 9992 | ins_try_si(c) |
| 9993 | int c; |
| 9994 | { |
| 9995 | pos_T *pos, old_pos; |
| 9996 | char_u *ptr; |
| 9997 | int i; |
| 9998 | int temp; |
| 9999 | |
| 10000 | /* |
| 10001 | * do some very smart indenting when entering '{' or '}' |
| 10002 | */ |
| 10003 | if (((did_si || can_si_back) && c == '{') || (can_si && c == '}')) |
| 10004 | { |
| 10005 | /* |
| 10006 | * for '}' set indent equal to indent of line containing matching '{' |
| 10007 | */ |
| 10008 | if (c == '}' && (pos = findmatch(NULL, '{')) != NULL) |
| 10009 | { |
| 10010 | old_pos = curwin->w_cursor; |
| 10011 | /* |
| 10012 | * If the matching '{' has a ')' immediately before it (ignoring |
| 10013 | * white-space), then line up with the start of the line |
| 10014 | * containing the matching '(' if there is one. This handles the |
| 10015 | * case where an "if (..\n..) {" statement continues over multiple |
| 10016 | * lines -- webb |
| 10017 | */ |
| 10018 | ptr = ml_get(pos->lnum); |
| 10019 | i = pos->col; |
| 10020 | if (i > 0) /* skip blanks before '{' */ |
| 10021 | while (--i > 0 && vim_iswhite(ptr[i])) |
| 10022 | ; |
| 10023 | curwin->w_cursor.lnum = pos->lnum; |
| 10024 | curwin->w_cursor.col = i; |
| 10025 | if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL) |
| 10026 | curwin->w_cursor = *pos; |
| 10027 | i = get_indent(); |
| 10028 | curwin->w_cursor = old_pos; |
| 10029 | #ifdef FEAT_VREPLACE |
| 10030 | if (State & VREPLACE_FLAG) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 10031 | change_indent(INDENT_SET, i, FALSE, NUL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10032 | else |
| 10033 | #endif |
| 10034 | (void)set_indent(i, SIN_CHANGED); |
| 10035 | } |
| 10036 | else if (curwin->w_cursor.col > 0) |
| 10037 | { |
| 10038 | /* |
| 10039 | * when inserting '{' after "O" reduce indent, but not |
| 10040 | * more than indent of previous line |
| 10041 | */ |
| 10042 | temp = TRUE; |
| 10043 | if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1) |
| 10044 | { |
| 10045 | old_pos = curwin->w_cursor; |
| 10046 | i = get_indent(); |
| 10047 | while (curwin->w_cursor.lnum > 1) |
| 10048 | { |
| 10049 | ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum))); |
| 10050 | |
| 10051 | /* ignore empty lines and lines starting with '#'. */ |
| 10052 | if (*ptr != '#' && *ptr != NUL) |
| 10053 | break; |
| 10054 | } |
| 10055 | if (get_indent() >= i) |
| 10056 | temp = FALSE; |
| 10057 | curwin->w_cursor = old_pos; |
| 10058 | } |
| 10059 | if (temp) |
Bram Moolenaar | 21b17e7 | 2008-01-16 19:03:13 +0000 | [diff] [blame] | 10060 | shift_line(TRUE, FALSE, 1, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10061 | } |
| 10062 | } |
| 10063 | |
| 10064 | /* |
| 10065 | * set indent of '#' always to 0 |
| 10066 | */ |
| 10067 | if (curwin->w_cursor.col > 0 && can_si && c == '#') |
| 10068 | { |
| 10069 | /* remember current indent for next line */ |
| 10070 | old_indent = get_indent(); |
| 10071 | (void)set_indent(0, SIN_CHANGED); |
| 10072 | } |
| 10073 | |
| 10074 | /* Adjust ai_col, the char at this position can be deleted. */ |
| 10075 | if (ai_col > curwin->w_cursor.col) |
| 10076 | ai_col = curwin->w_cursor.col; |
| 10077 | } |
| 10078 | #endif |
| 10079 | |
| 10080 | /* |
| 10081 | * Get the value that w_virtcol would have when 'list' is off. |
| 10082 | * Unless 'cpo' contains the 'L' flag. |
| 10083 | */ |
| 10084 | static colnr_T |
| 10085 | get_nolist_virtcol() |
| 10086 | { |
| 10087 | if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL) |
| 10088 | return getvcol_nolist(&curwin->w_cursor); |
| 10089 | validate_virtcol(); |
| 10090 | return curwin->w_virtcol; |
| 10091 | } |
Bram Moolenaar | f5876f1 | 2012-02-29 18:22:08 +0100 | [diff] [blame] | 10092 | |
| 10093 | #ifdef FEAT_AUTOCMD |
| 10094 | /* |
| 10095 | * Handle the InsertCharPre autocommand. |
| 10096 | * "c" is the character that was typed. |
| 10097 | * Return a pointer to allocated memory with the replacement string. |
| 10098 | * Return NULL to continue inserting "c". |
| 10099 | */ |
| 10100 | static char_u * |
| 10101 | do_insert_char_pre(c) |
| 10102 | int c; |
| 10103 | { |
| 10104 | char_u *res; |
| 10105 | |
| 10106 | /* Return quickly when there is nothing to do. */ |
| 10107 | if (!has_insertcharpre()) |
| 10108 | return NULL; |
| 10109 | |
| 10110 | /* Lock the text to avoid weird things from happening. */ |
| 10111 | ++textlock; |
| 10112 | set_vim_var_char(c); /* set v:char */ |
| 10113 | |
| 10114 | if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf)) |
| 10115 | /* Get the new value of v:char. It may be empty or more than one |
| 10116 | * character. */ |
| 10117 | res = vim_strsave(get_vim_var_str(VV_CHAR)); |
| 10118 | else |
| 10119 | res = NULL; |
| 10120 | |
| 10121 | set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */ |
| 10122 | --textlock; |
| 10123 | |
| 10124 | return res; |
| 10125 | } |
| 10126 | #endif |