Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | * ex_docmd.c: functions for executing an Ex command line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 16 | static int quitmore = 0; |
| 17 | static int ex_pressedreturn = FALSE; |
| 18 | #ifndef FEAT_PRINTER |
| 19 | # define ex_hardcopy ex_ni |
| 20 | #endif |
| 21 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | #ifdef FEAT_EVAL |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 23 | static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 24 | #else |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 25 | static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 26 | static int if_level = 0; // depth in :if |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 27 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 28 | static void append_command(char_u *cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 30 | #ifndef FEAT_MENU |
| 31 | # define ex_emenu ex_ni |
| 32 | # define ex_menu ex_ni |
| 33 | # define ex_menutranslate ex_ni |
| 34 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 35 | static void ex_autocmd(exarg_T *eap); |
| 36 | static void ex_doautocmd(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 37 | static void ex_bunload(exarg_T *eap); |
| 38 | static void ex_buffer(exarg_T *eap); |
| 39 | static void ex_bmodified(exarg_T *eap); |
| 40 | static void ex_bnext(exarg_T *eap); |
| 41 | static void ex_bprevious(exarg_T *eap); |
| 42 | static void ex_brewind(exarg_T *eap); |
| 43 | static void ex_blast(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 44 | static char_u *getargcmd(char_u **); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 45 | static int getargopt(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 46 | #ifndef FEAT_QUICKFIX |
| 47 | # define ex_make ex_ni |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 48 | # define ex_cbuffer ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | # define ex_cc ex_ni |
| 50 | # define ex_cnext ex_ni |
Bram Moolenaar | 3ff3311 | 2019-05-03 21:56:35 +0200 | [diff] [blame] | 51 | # define ex_cbelow ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 52 | # define ex_cfile ex_ni |
| 53 | # define qf_list ex_ni |
| 54 | # define qf_age ex_ni |
Bram Moolenaar | f6acffb | 2016-07-16 16:54:24 +0200 | [diff] [blame] | 55 | # define qf_history ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 56 | # define ex_helpgrep ex_ni |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 57 | # define ex_vimgrep ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 58 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 59 | #if !defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | # define ex_cclose ex_ni |
| 61 | # define ex_copen ex_ni |
| 62 | # define ex_cwindow ex_ni |
Bram Moolenaar | dcb1700 | 2016-07-07 18:58:59 +0200 | [diff] [blame] | 63 | # define ex_cbottom ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | #endif |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 65 | #if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL) |
| 66 | # define ex_cexpr ex_ni |
| 67 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 68 | |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 69 | static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 70 | static void get_flags(exarg_T *eap); |
Bram Moolenaar | 85363ab | 2010-07-18 13:58:26 +0200 | [diff] [blame] | 71 | #if !defined(FEAT_PERL) \ |
| 72 | || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \ |
| 73 | || !defined(FEAT_TCL) \ |
| 74 | || !defined(FEAT_RUBY) \ |
| 75 | || !defined(FEAT_LUA) \ |
| 76 | || !defined(FEAT_MZSCHEME) |
Bram Moolenaar | 7bb7555 | 2007-07-16 18:39:49 +0000 | [diff] [blame] | 77 | # define HAVE_EX_SCRIPT_NI |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 78 | static void ex_script_ni(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | #endif |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 80 | static char *invalid_range(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 81 | static void correct_range(exarg_T *eap); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 82 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 83 | static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 84 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 85 | static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep); |
| 86 | static void ex_highlight(exarg_T *eap); |
| 87 | static void ex_colorscheme(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 88 | static void ex_cquit(exarg_T *eap); |
| 89 | static void ex_quit_all(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 90 | static void ex_close(exarg_T *eap); |
| 91 | static void ex_win_close(int forceit, win_T *win, tabpage_T *tp); |
| 92 | static void ex_only(exarg_T *eap); |
| 93 | static void ex_resize(exarg_T *eap); |
| 94 | static void ex_stag(exarg_T *eap); |
| 95 | static void ex_tabclose(exarg_T *eap); |
| 96 | static void ex_tabonly(exarg_T *eap); |
| 97 | static void ex_tabnext(exarg_T *eap); |
| 98 | static void ex_tabmove(exarg_T *eap); |
| 99 | static void ex_tabs(exarg_T *eap); |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 100 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 101 | static void ex_pclose(exarg_T *eap); |
| 102 | static void ex_ptag(exarg_T *eap); |
| 103 | static void ex_pedit(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | #else |
| 105 | # define ex_pclose ex_ni |
| 106 | # define ex_ptag ex_ni |
| 107 | # define ex_pedit ex_ni |
| 108 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 109 | static void ex_hide(exarg_T *eap); |
| 110 | static void ex_stop(exarg_T *eap); |
| 111 | static void ex_exit(exarg_T *eap); |
| 112 | static void ex_print(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | #ifdef FEAT_BYTEOFF |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 114 | static void ex_goto(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 115 | #else |
| 116 | # define ex_goto ex_ni |
| 117 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 118 | static void ex_shell(exarg_T *eap); |
| 119 | static void ex_preserve(exarg_T *eap); |
| 120 | static void ex_recover(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 121 | static void ex_mode(exarg_T *eap); |
| 122 | static void ex_wrongmodifier(exarg_T *eap); |
| 123 | static void ex_find(exarg_T *eap); |
| 124 | static void ex_open(exarg_T *eap); |
| 125 | static void ex_edit(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 126 | #ifndef FEAT_GUI |
| 127 | # define ex_gui ex_nogui |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 128 | static void ex_nogui(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 129 | #endif |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 130 | #if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 131 | static void ex_tearoff(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | #else |
| 133 | # define ex_tearoff ex_ni |
| 134 | #endif |
Bram Moolenaar | 29a2c08 | 2018-03-05 21:06:23 +0100 | [diff] [blame] | 135 | #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \ |
| 136 | || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU) |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 137 | static void ex_popup(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 138 | #else |
| 139 | # define ex_popup ex_ni |
| 140 | #endif |
| 141 | #ifndef FEAT_GUI_MSWIN |
| 142 | # define ex_simalt ex_ni |
| 143 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 144 | #if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 145 | # define gui_mch_find_dialog ex_ni |
| 146 | # define gui_mch_replace_dialog ex_ni |
| 147 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 148 | #if !defined(FEAT_GUI_GTK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 149 | # define ex_helpfind ex_ni |
| 150 | #endif |
| 151 | #ifndef FEAT_CSCOPE |
Bram Moolenaar | d4db771 | 2016-11-12 19:16:46 +0100 | [diff] [blame] | 152 | # define ex_cscope ex_ni |
| 153 | # define ex_scscope ex_ni |
| 154 | # define ex_cstag ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 155 | #endif |
| 156 | #ifndef FEAT_SYN_HL |
| 157 | # define ex_syntax ex_ni |
Bram Moolenaar | 860cae1 | 2010-06-05 23:22:07 +0200 | [diff] [blame] | 158 | # define ex_ownsyntax ex_ni |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 159 | #endif |
Bram Moolenaar | f751255 | 2013-06-06 14:55:19 +0200 | [diff] [blame] | 160 | #if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE) |
Bram Moolenaar | 8a7f5a2 | 2013-06-06 14:01:46 +0200 | [diff] [blame] | 161 | # define ex_syntime ex_ni |
| 162 | #endif |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 163 | #ifndef FEAT_SPELL |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 164 | # define ex_spell ex_ni |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 165 | # define ex_mkspell ex_ni |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 166 | # define ex_spelldump ex_ni |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 167 | # define ex_spellinfo ex_ni |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 168 | # define ex_spellrepall ex_ni |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 169 | #endif |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 170 | #ifndef FEAT_PERSISTENT_UNDO |
| 171 | # define ex_rundo ex_ni |
| 172 | # define ex_wundo ex_ni |
| 173 | #endif |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 174 | #ifndef FEAT_LUA |
| 175 | # define ex_lua ex_script_ni |
| 176 | # define ex_luado ex_ni |
| 177 | # define ex_luafile ex_ni |
| 178 | #endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 179 | #ifndef FEAT_MZSCHEME |
| 180 | # define ex_mzscheme ex_script_ni |
| 181 | # define ex_mzfile ex_ni |
| 182 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 183 | #ifndef FEAT_PERL |
| 184 | # define ex_perl ex_script_ni |
| 185 | # define ex_perldo ex_ni |
| 186 | #endif |
| 187 | #ifndef FEAT_PYTHON |
| 188 | # define ex_python ex_script_ni |
Bram Moolenaar | d620aa9 | 2013-05-17 16:40:06 +0200 | [diff] [blame] | 189 | # define ex_pydo ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | # define ex_pyfile ex_ni |
| 191 | #endif |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 192 | #ifndef FEAT_PYTHON3 |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 193 | # define ex_py3 ex_script_ni |
Bram Moolenaar | 3dab280 | 2013-05-15 18:28:13 +0200 | [diff] [blame] | 194 | # define ex_py3do ex_ni |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 195 | # define ex_py3file ex_ni |
| 196 | #endif |
Bram Moolenaar | f42dd3c | 2017-01-28 16:06:38 +0100 | [diff] [blame] | 197 | #if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3) |
| 198 | # define ex_pyx ex_script_ni |
| 199 | # define ex_pyxdo ex_ni |
| 200 | # define ex_pyxfile ex_ni |
| 201 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 202 | #ifndef FEAT_TCL |
| 203 | # define ex_tcl ex_script_ni |
| 204 | # define ex_tcldo ex_ni |
| 205 | # define ex_tclfile ex_ni |
| 206 | #endif |
| 207 | #ifndef FEAT_RUBY |
| 208 | # define ex_ruby ex_script_ni |
| 209 | # define ex_rubydo ex_ni |
| 210 | # define ex_rubyfile ex_ni |
| 211 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 212 | #ifndef FEAT_KEYMAP |
| 213 | # define ex_loadkeymap ex_ni |
| 214 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 215 | static void ex_swapname(exarg_T *eap); |
| 216 | static void ex_syncbind(exarg_T *eap); |
| 217 | static void ex_read(exarg_T *eap); |
| 218 | static void ex_pwd(exarg_T *eap); |
| 219 | static void ex_equal(exarg_T *eap); |
| 220 | static void ex_sleep(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 221 | static void ex_winsize(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 222 | static void ex_wincmd(exarg_T *eap); |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 223 | #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN) |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 224 | static void ex_winpos(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 225 | #else |
| 226 | # define ex_winpos ex_ni |
| 227 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 228 | static void ex_operators(exarg_T *eap); |
| 229 | static void ex_put(exarg_T *eap); |
| 230 | static void ex_copymove(exarg_T *eap); |
| 231 | static void ex_submagic(exarg_T *eap); |
| 232 | static void ex_join(exarg_T *eap); |
| 233 | static void ex_at(exarg_T *eap); |
| 234 | static void ex_bang(exarg_T *eap); |
| 235 | static void ex_undo(exarg_T *eap); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 236 | #ifdef FEAT_PERSISTENT_UNDO |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 237 | static void ex_wundo(exarg_T *eap); |
| 238 | static void ex_rundo(exarg_T *eap); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 239 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 240 | static void ex_redo(exarg_T *eap); |
| 241 | static void ex_later(exarg_T *eap); |
| 242 | static void ex_redir(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 243 | static void ex_redrawstatus(exarg_T *eap); |
Bram Moolenaar | e12bab3 | 2019-01-08 22:02:56 +0100 | [diff] [blame] | 244 | static void ex_redrawtabline(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 245 | static void close_redir(void); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 246 | static void ex_mark(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 247 | static void ex_startinsert(exarg_T *eap); |
| 248 | static void ex_stopinsert(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 249 | #ifdef FEAT_FIND_ID |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 250 | static void ex_checkpath(exarg_T *eap); |
| 251 | static void ex_findpat(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 252 | #else |
| 253 | # define ex_findpat ex_ni |
| 254 | # define ex_checkpath ex_ni |
| 255 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 256 | #if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX) |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 257 | static void ex_psearch(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 258 | #else |
| 259 | # define ex_psearch ex_ni |
| 260 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 261 | static void ex_tag(exarg_T *eap); |
| 262 | static void ex_tag_cmd(exarg_T *eap, char_u *name); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 263 | #ifndef FEAT_EVAL |
Bram Moolenaar | e4ce825 | 2019-08-04 15:30:16 +0200 | [diff] [blame] | 264 | # define ex_break ex_ni |
| 265 | # define ex_breakadd ex_ni |
| 266 | # define ex_breakdel ex_ni |
| 267 | # define ex_breaklist ex_ni |
| 268 | # define ex_call ex_ni |
| 269 | # define ex_catch ex_ni |
| 270 | # define ex_compiler ex_ni |
Bram Moolenaar | e4ce825 | 2019-08-04 15:30:16 +0200 | [diff] [blame] | 271 | # define ex_continue ex_ni |
| 272 | # define ex_debug ex_ni |
| 273 | # define ex_debuggreedy ex_ni |
Bram Moolenaar | 822ba24 | 2020-05-24 23:00:18 +0200 | [diff] [blame] | 274 | # define ex_def ex_ni |
| 275 | # define ex_defcompile ex_ni |
Bram Moolenaar | e4ce825 | 2019-08-04 15:30:16 +0200 | [diff] [blame] | 276 | # define ex_delfunction ex_ni |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 277 | # define ex_disassemble ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 278 | # define ex_echo ex_ni |
| 279 | # define ex_echohl ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 280 | # define ex_else ex_ni |
Bram Moolenaar | e4ce825 | 2019-08-04 15:30:16 +0200 | [diff] [blame] | 281 | # define ex_endfunction ex_ni |
| 282 | # define ex_endif ex_ni |
| 283 | # define ex_endtry ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 284 | # define ex_endwhile ex_ni |
Bram Moolenaar | e4ce825 | 2019-08-04 15:30:16 +0200 | [diff] [blame] | 285 | # define ex_eval ex_ni |
| 286 | # define ex_execute ex_ni |
| 287 | # define ex_finally ex_ni |
| 288 | # define ex_finish ex_ni |
| 289 | # define ex_function ex_ni |
| 290 | # define ex_if ex_ni |
| 291 | # define ex_let ex_ni |
| 292 | # define ex_lockvar ex_ni |
| 293 | # define ex_oldfiles ex_ni |
| 294 | # define ex_options ex_ni |
| 295 | # define ex_packadd ex_ni |
| 296 | # define ex_packloadall ex_ni |
| 297 | # define ex_return ex_ni |
| 298 | # define ex_scriptnames ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 299 | # define ex_throw ex_ni |
| 300 | # define ex_try ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 301 | # define ex_unlet ex_ni |
Bram Moolenaar | 65c1b01 | 2005-01-31 19:02:28 +0000 | [diff] [blame] | 302 | # define ex_unlockvar ex_ni |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 303 | # define ex_vim9script ex_ni |
Bram Moolenaar | e4ce825 | 2019-08-04 15:30:16 +0200 | [diff] [blame] | 304 | # define ex_while ex_ni |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 305 | # define ex_import ex_ni |
| 306 | # define ex_export ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 307 | #endif |
Bram Moolenaar | 8453807 | 2019-07-28 14:15:42 +0200 | [diff] [blame] | 308 | #ifndef FEAT_SESSION |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | # define ex_loadview ex_ni |
| 310 | #endif |
Bram Moolenaar | defa067 | 2019-07-21 19:25:37 +0200 | [diff] [blame] | 311 | #ifndef FEAT_VIMINFO |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 312 | # define ex_viminfo ex_ni |
| 313 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 314 | static void ex_behave(exarg_T *eap); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 315 | static void ex_filetype(exarg_T *eap); |
| 316 | static void ex_setfiletype(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 317 | #ifndef FEAT_DIFF |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 318 | # define ex_diffoff ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | # define ex_diffpatch ex_ni |
| 320 | # define ex_diffgetput ex_ni |
| 321 | # define ex_diffsplit ex_ni |
| 322 | # define ex_diffthis ex_ni |
| 323 | # define ex_diffupdate ex_ni |
| 324 | #endif |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 325 | static void ex_digraphs(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 326 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 327 | static void ex_nohlsearch(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 328 | #else |
| 329 | # define ex_nohlsearch ex_ni |
| 330 | # define ex_match ex_ni |
| 331 | #endif |
| 332 | #ifdef FEAT_CRYPT |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 333 | static void ex_X(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 334 | #else |
| 335 | # define ex_X ex_ni |
| 336 | #endif |
| 337 | #ifdef FEAT_FOLDING |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 338 | static void ex_fold(exarg_T *eap); |
| 339 | static void ex_foldopen(exarg_T *eap); |
| 340 | static void ex_folddo(exarg_T *eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 341 | #else |
| 342 | # define ex_fold ex_ni |
| 343 | # define ex_foldopen ex_ni |
| 344 | # define ex_folddo ex_ni |
| 345 | #endif |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 346 | #if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 347 | # define ex_language ex_ni |
| 348 | #endif |
| 349 | #ifndef FEAT_SIGNS |
| 350 | # define ex_sign ex_ni |
| 351 | #endif |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 352 | #ifndef FEAT_NETBEANS_INTG |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 353 | # define ex_nbclose ex_ni |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 354 | # define ex_nbkey ex_ni |
Bram Moolenaar | b26e632 | 2010-05-22 21:34:09 +0200 | [diff] [blame] | 355 | # define ex_nbstart ex_ni |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 356 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 357 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | #ifndef FEAT_JUMPLIST |
| 359 | # define ex_jumps ex_ni |
Bram Moolenaar | 2d35899 | 2016-06-12 21:20:54 +0200 | [diff] [blame] | 360 | # define ex_clearjumps ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 361 | # define ex_changes ex_ni |
| 362 | #endif |
| 363 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 364 | #ifndef FEAT_PROFILE |
| 365 | # define ex_profile ex_ni |
| 366 | #endif |
Bram Moolenaar | e4f25e4 | 2017-07-07 11:54:15 +0200 | [diff] [blame] | 367 | #ifndef FEAT_TERMINAL |
| 368 | # define ex_terminal ex_ni |
| 369 | #endif |
Bram Moolenaar | d4aa83a | 2019-05-09 18:59:31 +0200 | [diff] [blame] | 370 | #if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD) |
| 371 | # define ex_xrestore ex_ni |
| 372 | #endif |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 373 | #if !defined(FEAT_PROP_POPUP) |
Bram Moolenaar | 682725c | 2019-05-25 20:10:37 +0200 | [diff] [blame] | 374 | # define ex_popupclear ex_ni |
| 375 | #endif |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 376 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 377 | /* |
| 378 | * Declare cmdnames[]. |
| 379 | */ |
| 380 | #define DO_DECLARE_EXCMD |
| 381 | #include "ex_cmds.h" |
Bram Moolenaar | 6de5e12 | 2017-04-20 21:55:44 +0200 | [diff] [blame] | 382 | #include "ex_cmdidxs.h" |
Bram Moolenaar | e5e0fbc | 2017-03-25 14:51:01 +0100 | [diff] [blame] | 383 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 384 | static char_u dollar_command[2] = {'$', 0}; |
| 385 | |
| 386 | |
| 387 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 388 | // Struct for storing a line inside a while/for loop |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 389 | typedef struct |
| 390 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 391 | char_u *line; // command line |
| 392 | linenr_T lnum; // sourcing_lnum of the line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 393 | } wcmd_T; |
| 394 | |
| 395 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 396 | * Structure used to store info for line position in a while or for loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 397 | * This is required, because do_one_cmd() may invoke ex_function(), which |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 398 | * reads more lines that may come from the while/for loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 399 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 400 | struct loop_cookie |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 401 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 402 | garray_T *lines_gap; // growarray with line info |
| 403 | int current_line; // last read line from growarray |
| 404 | int repeating; // TRUE when looping a second time |
| 405 | // When "repeating" is FALSE use "getline" and "cookie" to get lines |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 406 | char_u *(*getline)(int, void *, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | void *cookie; |
| 408 | }; |
| 409 | |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 410 | static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat); |
Bram Moolenaar | f28dbce | 2016-01-29 22:03:47 +0100 | [diff] [blame] | 411 | static int store_loop_line(garray_T *gap, char_u *line); |
| 412 | static void free_cmdlines(garray_T *gap); |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 413 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 414 | // Struct to save a few things while debugging. Used in do_cmdline() only. |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 415 | struct dbg_stuff |
| 416 | { |
| 417 | int trylevel; |
| 418 | int force_abort; |
| 419 | except_T *caught_stack; |
| 420 | char_u *vv_exception; |
| 421 | char_u *vv_throwpoint; |
| 422 | int did_emsg; |
| 423 | int got_int; |
| 424 | int did_throw; |
| 425 | int need_rethrow; |
| 426 | int check_cstack; |
| 427 | except_T *current_exception; |
| 428 | }; |
| 429 | |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 430 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 431 | save_dbg_stuff(struct dbg_stuff *dsp) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 432 | { |
| 433 | dsp->trylevel = trylevel; trylevel = 0; |
| 434 | dsp->force_abort = force_abort; force_abort = FALSE; |
| 435 | dsp->caught_stack = caught_stack; caught_stack = NULL; |
| 436 | dsp->vv_exception = v_exception(NULL); |
| 437 | dsp->vv_throwpoint = v_throwpoint(NULL); |
| 438 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 439 | // Necessary for debugging an inactive ":catch", ":finally", ":endtry" |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 440 | dsp->did_emsg = did_emsg; did_emsg = FALSE; |
| 441 | dsp->got_int = got_int; got_int = FALSE; |
| 442 | dsp->did_throw = did_throw; did_throw = FALSE; |
| 443 | dsp->need_rethrow = need_rethrow; need_rethrow = FALSE; |
| 444 | dsp->check_cstack = check_cstack; check_cstack = FALSE; |
| 445 | dsp->current_exception = current_exception; current_exception = NULL; |
| 446 | } |
| 447 | |
| 448 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 449 | restore_dbg_stuff(struct dbg_stuff *dsp) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 450 | { |
| 451 | suppress_errthrow = FALSE; |
| 452 | trylevel = dsp->trylevel; |
| 453 | force_abort = dsp->force_abort; |
| 454 | caught_stack = dsp->caught_stack; |
| 455 | (void)v_exception(dsp->vv_exception); |
| 456 | (void)v_throwpoint(dsp->vv_throwpoint); |
| 457 | did_emsg = dsp->did_emsg; |
| 458 | got_int = dsp->got_int; |
| 459 | did_throw = dsp->did_throw; |
| 460 | need_rethrow = dsp->need_rethrow; |
| 461 | check_cstack = dsp->check_cstack; |
| 462 | current_exception = dsp->current_exception; |
| 463 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 464 | #endif |
| 465 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 466 | /* |
| 467 | * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi" |
| 468 | * command is given. |
| 469 | */ |
| 470 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 471 | do_exmode( |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 472 | int improved) // TRUE for "improved Ex" mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 473 | { |
| 474 | int save_msg_scroll; |
| 475 | int prev_msg_row; |
| 476 | linenr_T prev_line; |
Bram Moolenaar | 79518e2 | 2017-02-17 16:31:35 +0100 | [diff] [blame] | 477 | varnumber_T changedtick; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 478 | |
| 479 | if (improved) |
| 480 | exmode_active = EXMODE_VIM; |
| 481 | else |
| 482 | exmode_active = EXMODE_NORMAL; |
| 483 | State = NORMAL; |
| 484 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 485 | // When using ":global /pat/ visual" and then "Q" we return to continue |
| 486 | // the :global command. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 487 | if (global_busy) |
| 488 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 489 | |
| 490 | save_msg_scroll = msg_scroll; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 491 | ++RedrawingDisabled; // don't redisplay the window |
| 492 | ++no_wait_return; // don't wait for return |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 493 | #ifdef FEAT_GUI |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 494 | // Ignore scrollbar and mouse events in Ex mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 495 | ++hold_gui_events; |
| 496 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 497 | |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 498 | msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode.")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | while (exmode_active) |
| 500 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 501 | // Check for a ":normal" command and no more characters left. |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 502 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 503 | { |
| 504 | exmode_active = FALSE; |
| 505 | break; |
| 506 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | msg_scroll = TRUE; |
| 508 | need_wait_return = FALSE; |
| 509 | ex_pressedreturn = FALSE; |
| 510 | ex_no_reprint = FALSE; |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 511 | changedtick = CHANGEDTICK(curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | prev_msg_row = msg_row; |
| 513 | prev_line = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 514 | if (improved) |
| 515 | { |
| 516 | cmdline_row = msg_row; |
| 517 | do_cmdline(NULL, getexline, NULL, 0); |
| 518 | } |
| 519 | else |
| 520 | do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT); |
| 521 | lines_left = Rows - 1; |
| 522 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 523 | if ((prev_line != curwin->w_cursor.lnum |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 524 | || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 525 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 526 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 527 | emsg(_(e_emptybuf)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 528 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 529 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 530 | if (ex_pressedreturn) |
| 531 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 532 | // go up one line, to overwrite the ":<CR>" line, so the |
| 533 | // output doesn't contain empty lines. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 534 | msg_row = prev_msg_row; |
| 535 | if (prev_msg_row == Rows - 1) |
| 536 | msg_row--; |
| 537 | } |
| 538 | msg_col = 0; |
| 539 | print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE); |
| 540 | msg_clr_eos(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 541 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 542 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 543 | else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 544 | { |
| 545 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 546 | emsg(_(e_emptybuf)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 547 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 548 | emsg(_("E501: At end-of-file")); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 549 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | #ifdef FEAT_GUI |
| 553 | --hold_gui_events; |
| 554 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 555 | --RedrawingDisabled; |
| 556 | --no_wait_return; |
| 557 | update_screen(CLEAR); |
| 558 | need_wait_return = FALSE; |
| 559 | msg_scroll = save_msg_scroll; |
| 560 | } |
| 561 | |
| 562 | /* |
Bram Moolenaar | 4facea3 | 2019-10-12 20:17:40 +0200 | [diff] [blame] | 563 | * Print the executed command for when 'verbose' is set. |
| 564 | * When "lnum" is 0 only print the command. |
| 565 | */ |
| 566 | static void |
| 567 | msg_verbose_cmd(linenr_T lnum, char_u *cmd) |
| 568 | { |
| 569 | ++no_wait_return; |
| 570 | verbose_enter_scroll(); |
| 571 | |
| 572 | if (lnum == 0) |
| 573 | smsg(_("Executing: %s"), cmd); |
| 574 | else |
| 575 | smsg(_("line %ld: %s"), (long)lnum, cmd); |
| 576 | if (msg_silent == 0) |
| 577 | msg_puts("\n"); // don't overwrite this |
| 578 | |
| 579 | verbose_leave_scroll(); |
| 580 | --no_wait_return; |
| 581 | } |
| 582 | |
| 583 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 584 | * Execute a simple command line. Used for translated commands like "*". |
| 585 | */ |
| 586 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 587 | do_cmdline_cmd(char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 588 | { |
| 589 | return do_cmdline(cmd, NULL, NULL, |
| 590 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * do_cmdline(): execute one Ex command line |
| 595 | * |
| 596 | * 1. Execute "cmdline" when it is not NULL. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 597 | * If "cmdline" is NULL, or more lines are needed, fgetline() is used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 598 | * 2. Split up in parts separated with '|'. |
| 599 | * |
| 600 | * This function can be called recursively! |
| 601 | * |
| 602 | * flags: |
| 603 | * DOCMD_VERBOSE - The command will be included in the error message. |
| 604 | * DOCMD_NOWAIT - Don't call wait_return() and friends. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 605 | * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 606 | * DOCMD_KEYTYPED - Don't reset KeyTyped. |
| 607 | * DOCMD_EXCRESET - Reset the exception environment (used for debugging). |
| 608 | * DOCMD_KEEPLINE - Store first typed line (for repeating with "."). |
| 609 | * |
| 610 | * return FAIL if cmdline could not be executed, OK otherwise |
| 611 | */ |
| 612 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 613 | do_cmdline( |
| 614 | char_u *cmdline, |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 615 | char_u *(*fgetline)(int, void *, int, int), |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 616 | void *cookie, // argument for fgetline() |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 617 | int flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 618 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 619 | char_u *next_cmdline; // next cmd to execute |
| 620 | char_u *cmdline_copy = NULL; // copy of cmd line |
| 621 | int used_getline = FALSE; // used "fgetline" to obtain command |
| 622 | static int recursive = 0; // recursive depth |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 623 | int msg_didout_before_start = 0; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 624 | int count = 0; // line number count |
| 625 | int did_inc = FALSE; // incremented RedrawingDisabled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 626 | int retval = OK; |
| 627 | #ifdef FEAT_EVAL |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 628 | cstack_T cstack; // conditional stack |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 629 | garray_T lines_ga; // keep lines for ":while"/":for" |
| 630 | int current_line = 0; // active line in lines_ga |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 631 | int current_line_before = 0; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 632 | char_u *fname = NULL; // function or script name |
| 633 | linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie |
| 634 | int *dbg_tick = NULL; // ptr to dbg_tick field in cookie |
| 635 | struct dbg_stuff debug_saved; // saved things for debug mode |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 636 | int initial_trylevel; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 637 | msglist_T **saved_msg_list = NULL; |
| 638 | msglist_T *private_msg_list; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 639 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 640 | // "fgetline" and "cookie" passed to do_one_cmd() |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 641 | char_u *(*cmd_getline)(int, void *, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 642 | void *cmd_cookie; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 643 | struct loop_cookie cmd_loop_cookie; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | void *real_cookie; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 645 | int getline_is_func; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 646 | #else |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 647 | # define cmd_getline fgetline |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 648 | # define cmd_cookie cookie |
| 649 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 650 | static int call_depth = 0; // recursiveness |
Bram Moolenaar | 2196bce | 2020-04-12 20:01:11 +0200 | [diff] [blame] | 651 | #ifdef FEAT_EVAL |
Bram Moolenaar | e31ee86 | 2020-01-07 20:59:34 +0100 | [diff] [blame] | 652 | ESTACK_CHECK_DECLARATION |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 653 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 654 | // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory |
| 655 | // location for storing error messages to be converted to an exception. |
| 656 | // This ensures that the do_errthrow() call in do_one_cmd() does not |
| 657 | // combine the messages stored by an earlier invocation of do_one_cmd() |
| 658 | // with the command name of the later one. This would happen when |
| 659 | // BufWritePost autocommands are executed after a write error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | saved_msg_list = msg_list; |
| 661 | msg_list = &private_msg_list; |
| 662 | private_msg_list = NULL; |
| 663 | #endif |
| 664 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 665 | // It's possible to create an endless loop with ":execute", catch that |
| 666 | // here. The value of 200 allows nested function calls, ":source", etc. |
| 667 | // Allow 200 or 'maxfuncdepth', whatever is larger. |
Bram Moolenaar | b094ff4 | 2017-01-02 16:16:39 +0100 | [diff] [blame] | 668 | if (call_depth >= 200 |
| 669 | #ifdef FEAT_EVAL |
| 670 | && call_depth >= p_mfd |
| 671 | #endif |
| 672 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 674 | emsg(_("E169: Command too recursive")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 675 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 676 | // When converting to an exception, we do not include the command name |
| 677 | // since this is not an error of the specific command. |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 678 | do_errthrow((cstack_T *)NULL, (char_u *)NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 679 | msg_list = saved_msg_list; |
| 680 | #endif |
| 681 | return FAIL; |
| 682 | } |
| 683 | ++call_depth; |
| 684 | |
| 685 | #ifdef FEAT_EVAL |
| 686 | cstack.cs_idx = -1; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 687 | cstack.cs_looplevel = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | cstack.cs_trylevel = 0; |
| 689 | cstack.cs_emsg_silent_list = NULL; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 690 | cstack.cs_lflags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 691 | ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10); |
| 692 | |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 693 | real_cookie = getline_cookie(fgetline, cookie); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 695 | // Inside a function use a higher nesting level. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 696 | getline_is_func = getline_equal(fgetline, cookie, get_func_line); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 697 | if (getline_is_func && ex_nesting_level == func_level(real_cookie)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | ++ex_nesting_level; |
| 699 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 700 | // Get the function or script name and the address where the next breakpoint |
| 701 | // line and the debug tick for a function or script are stored. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 702 | if (getline_is_func) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 703 | { |
| 704 | fname = func_name(real_cookie); |
| 705 | breakpoint = func_breakpoint(real_cookie); |
| 706 | dbg_tick = func_dbg_tick(real_cookie); |
| 707 | } |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 708 | else if (getline_equal(fgetline, cookie, getsourceline)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | { |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 710 | fname = SOURCING_NAME; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 711 | breakpoint = source_breakpoint(real_cookie); |
| 712 | dbg_tick = source_dbg_tick(real_cookie); |
| 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Initialize "force_abort" and "suppress_errthrow" at the top level. |
| 717 | */ |
| 718 | if (!recursive) |
| 719 | { |
| 720 | force_abort = FALSE; |
| 721 | suppress_errthrow = FALSE; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * If requested, store and reset the global values controlling the |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 726 | * exception handling (used when debugging). Otherwise clear it to avoid |
| 727 | * a bogus compiler warning when the optimizer uses inline functions... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | */ |
Bram Moolenaar | b487294 | 2006-05-13 10:32:52 +0000 | [diff] [blame] | 729 | if (flags & DOCMD_EXCRESET) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 730 | save_dbg_stuff(&debug_saved); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 731 | else |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 732 | CLEAR_FIELD(debug_saved); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 733 | |
| 734 | initial_trylevel = trylevel; |
| 735 | |
| 736 | /* |
| 737 | * "did_throw" will be set to TRUE when an exception is being thrown. |
| 738 | */ |
| 739 | did_throw = FALSE; |
| 740 | #endif |
| 741 | /* |
| 742 | * "did_emsg" will be set to TRUE when emsg() is used, in which case we |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 743 | * cancel the whole command line, and any if/endif or loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 744 | * If force_abort is set, we cancel everything. |
| 745 | */ |
| 746 | did_emsg = FALSE; |
| 747 | |
| 748 | /* |
| 749 | * KeyTyped is only set when calling vgetc(). Reset it here when not |
| 750 | * calling vgetc() (sourced command lines). |
| 751 | */ |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 752 | if (!(flags & DOCMD_KEYTYPED) |
| 753 | && !getline_equal(fgetline, cookie, getexline)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 754 | KeyTyped = FALSE; |
| 755 | |
| 756 | /* |
| 757 | * Continue executing command lines: |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 758 | * - when inside an ":if", ":while" or ":for" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 759 | * - for multiple commands on one line, separated with '|' |
| 760 | * - when repeating until there are no more lines (for ":source") |
| 761 | */ |
| 762 | next_cmdline = cmdline; |
| 763 | do |
| 764 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 765 | #ifdef FEAT_EVAL |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 766 | getline_is_func = getline_equal(fgetline, cookie, get_func_line); |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 767 | #endif |
| 768 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 769 | // stop skipping cmds for an error msg after all endif/while/for |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 770 | if (next_cmdline == NULL |
| 771 | #ifdef FEAT_EVAL |
| 772 | && !force_abort |
| 773 | && cstack.cs_idx < 0 |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 774 | && !(getline_is_func && func_has_abort(real_cookie)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 775 | #endif |
| 776 | ) |
| 777 | did_emsg = FALSE; |
| 778 | |
| 779 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 780 | * 1. If repeating a line in a loop, get a line from lines_ga. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 781 | * 2. If no line given: Get an allocated line with fgetline(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | * 3. If a line is given: Make a copy, so we can mess with it. |
| 783 | */ |
| 784 | |
| 785 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 786 | // 1. If repeating, get a previous line from lines_ga. |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 787 | if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 788 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 789 | // Each '|' separated command is stored separately in lines_ga, to |
| 790 | // be able to jump to it. Don't use next_cmdline now. |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 791 | VIM_CLEAR(cmdline_copy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 792 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 793 | // Check if a function has returned or, unless it has an unclosed |
| 794 | // try conditional, aborted. |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 795 | if (getline_is_func) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 796 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 797 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 798 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 799 | func_line_end(real_cookie); |
| 800 | # endif |
| 801 | if (func_has_ended(real_cookie)) |
| 802 | { |
| 803 | retval = FAIL; |
| 804 | break; |
| 805 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 806 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 807 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 808 | else if (do_profiling == PROF_YES |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 809 | && getline_equal(fgetline, cookie, getsourceline)) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 810 | script_line_end(); |
| 811 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 812 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 813 | // Check if a sourced file hit a ":finish" command. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 814 | if (source_finished(fgetline, cookie)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 815 | { |
| 816 | retval = FAIL; |
| 817 | break; |
| 818 | } |
| 819 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 820 | // If breakpoints have been added/deleted need to check for it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 821 | if (breakpoint != NULL && dbg_tick != NULL |
| 822 | && *dbg_tick != debug_tick) |
| 823 | { |
| 824 | *breakpoint = dbg_find_breakpoint( |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 825 | getline_equal(fgetline, cookie, getsourceline), |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 826 | fname, SOURCING_LNUM); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 827 | *dbg_tick = debug_tick; |
| 828 | } |
| 829 | |
| 830 | next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 831 | SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 832 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 833 | // Did we encounter a breakpoint? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 834 | if (breakpoint != NULL && *breakpoint != 0 |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 835 | && *breakpoint <= SOURCING_LNUM) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 836 | { |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 837 | dbg_breakpoint(fname, SOURCING_LNUM); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 838 | // Find next breakpoint. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 839 | *breakpoint = dbg_find_breakpoint( |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 840 | getline_equal(fgetline, cookie, getsourceline), |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 841 | fname, SOURCING_LNUM); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 842 | *dbg_tick = debug_tick; |
| 843 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 844 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 845 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 846 | { |
| 847 | if (getline_is_func) |
| 848 | func_line_start(real_cookie); |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 849 | else if (getline_equal(fgetline, cookie, getsourceline)) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 850 | script_line_start(); |
| 851 | } |
| 852 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 853 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | #endif |
| 855 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 856 | // 2. If no line given, get an allocated line with fgetline(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 857 | if (next_cmdline == NULL) |
| 858 | { |
| 859 | /* |
| 860 | * Need to set msg_didout for the first line after an ":if", |
| 861 | * otherwise the ":if" will be overwritten. |
| 862 | */ |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 863 | if (count == 1 && getline_equal(fgetline, cookie, getexline)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | msg_didout = TRUE; |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 865 | if (fgetline == NULL || (next_cmdline = fgetline(':', cookie, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 866 | #ifdef FEAT_EVAL |
| 867 | cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2 |
| 868 | #else |
| 869 | 0 |
| 870 | #endif |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 871 | , TRUE)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 873 | // Don't call wait_return for aborted command line. The NULL |
| 874 | // returned for the end of a sourced file or executed function |
| 875 | // doesn't do this. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 876 | if (KeyTyped && !(flags & DOCMD_REPEAT)) |
| 877 | need_wait_return = FALSE; |
| 878 | retval = FAIL; |
| 879 | break; |
| 880 | } |
| 881 | used_getline = TRUE; |
| 882 | |
| 883 | /* |
| 884 | * Keep the first typed line. Clear it when more lines are typed. |
| 885 | */ |
| 886 | if (flags & DOCMD_KEEPLINE) |
| 887 | { |
| 888 | vim_free(repeat_cmdline); |
| 889 | if (count == 0) |
| 890 | repeat_cmdline = vim_strsave(next_cmdline); |
| 891 | else |
| 892 | repeat_cmdline = NULL; |
| 893 | } |
| 894 | } |
| 895 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 896 | // 3. Make a copy of the command so we can mess with it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 897 | else if (cmdline_copy == NULL) |
| 898 | { |
| 899 | next_cmdline = vim_strsave(next_cmdline); |
| 900 | if (next_cmdline == NULL) |
| 901 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 902 | emsg(_(e_outofmem)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 903 | retval = FAIL; |
| 904 | break; |
| 905 | } |
| 906 | } |
| 907 | cmdline_copy = next_cmdline; |
| 908 | |
| 909 | #ifdef FEAT_EVAL |
| 910 | /* |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 911 | * Inside a while/for loop, and when the command looks like a ":while" |
| 912 | * or ":for", the line is stored, because we may need it later when |
| 913 | * looping. |
| 914 | * |
| 915 | * When there is a '|' and another command, it is stored separately, |
| 916 | * because we need to be able to jump back to it from an |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 917 | * :endwhile/:endfor. |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 918 | * |
| 919 | * Pass a different "fgetline" function to do_one_cmd() below, |
| 920 | * that it stores lines in or reads them from "lines_ga". Makes it |
| 921 | * possible to define a function inside a while/for loop and handles |
| 922 | * line continuation. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 923 | */ |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 924 | if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 925 | { |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 926 | cmd_getline = get_loop_line; |
| 927 | cmd_cookie = (void *)&cmd_loop_cookie; |
| 928 | cmd_loop_cookie.lines_gap = &lines_ga; |
| 929 | cmd_loop_cookie.current_line = current_line; |
| 930 | cmd_loop_cookie.getline = fgetline; |
| 931 | cmd_loop_cookie.cookie = cookie; |
| 932 | cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len); |
| 933 | |
| 934 | // Save the current line when encountering it the first time. |
| 935 | if (current_line == lines_ga.ga_len |
| 936 | && store_loop_line(&lines_ga, next_cmdline) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 937 | { |
| 938 | retval = FAIL; |
| 939 | break; |
| 940 | } |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 941 | current_line_before = current_line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 942 | } |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 943 | else |
| 944 | { |
| 945 | cmd_getline = fgetline; |
| 946 | cmd_cookie = cookie; |
| 947 | } |
| 948 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 949 | did_endif = FALSE; |
| 950 | #endif |
| 951 | |
| 952 | if (count++ == 0) |
| 953 | { |
| 954 | /* |
| 955 | * All output from the commands is put below each other, without |
| 956 | * waiting for a return. Don't do this when executing commands |
| 957 | * from a script or when being called recursive (e.g. for ":e |
| 958 | * +command file"). |
| 959 | */ |
| 960 | if (!(flags & DOCMD_NOWAIT) && !recursive) |
| 961 | { |
| 962 | msg_didout_before_start = msg_didout; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 963 | msg_didany = FALSE; // no output yet |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 964 | msg_start(); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 965 | msg_scroll = TRUE; // put messages below each other |
| 966 | ++no_wait_return; // don't wait for return until finished |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | ++RedrawingDisabled; |
| 968 | did_inc = TRUE; |
| 969 | } |
| 970 | } |
| 971 | |
Bram Moolenaar | 823654b | 2020-05-29 23:03:09 +0200 | [diff] [blame] | 972 | if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16) |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 973 | msg_verbose_cmd(SOURCING_LNUM, cmdline_copy); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * 2. Execute one '|' separated command. |
| 977 | * do_one_cmd() will return NULL if there is no trailing '|'. |
| 978 | * "cmdline_copy" can change, e.g. for '%' and '#' expansion. |
| 979 | */ |
| 980 | ++recursive; |
| 981 | next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE, |
| 982 | #ifdef FEAT_EVAL |
| 983 | &cstack, |
| 984 | #endif |
| 985 | cmd_getline, cmd_cookie); |
| 986 | --recursive; |
| 987 | |
| 988 | #ifdef FEAT_EVAL |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 989 | if (cmd_cookie == (void *)&cmd_loop_cookie) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 990 | // Use "current_line" from "cmd_loop_cookie", it may have been |
| 991 | // incremented when defining a function. |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 992 | current_line = cmd_loop_cookie.current_line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 993 | #endif |
| 994 | |
| 995 | if (next_cmdline == NULL) |
| 996 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 997 | VIM_CLEAR(cmdline_copy); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 998 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 999 | /* |
| 1000 | * If the command was typed, remember it for the ':' register. |
| 1001 | * Do this AFTER executing the command to make :@: work. |
| 1002 | */ |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1003 | if (getline_equal(fgetline, cookie, getexline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1004 | && new_last_cmdline != NULL) |
| 1005 | { |
| 1006 | vim_free(last_cmdline); |
| 1007 | last_cmdline = new_last_cmdline; |
| 1008 | new_last_cmdline = NULL; |
| 1009 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1010 | } |
| 1011 | else |
| 1012 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1013 | // need to copy the command after the '|' to cmdline_copy, for the |
| 1014 | // next do_one_cmd() |
Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 1015 | STRMOVE(cmdline_copy, next_cmdline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | next_cmdline = cmdline_copy; |
| 1017 | } |
| 1018 | |
| 1019 | |
| 1020 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1021 | // reset did_emsg for a function that is not aborted by an error |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1022 | if (did_emsg && !force_abort |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1023 | && getline_equal(fgetline, cookie, get_func_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1024 | && !func_has_abort(real_cookie)) |
| 1025 | did_emsg = FALSE; |
| 1026 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1027 | if (cstack.cs_looplevel > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1028 | { |
| 1029 | ++current_line; |
| 1030 | |
| 1031 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1032 | * An ":endwhile", ":endfor" and ":continue" is handled here. |
| 1033 | * If we were executing commands, jump back to the ":while" or |
| 1034 | * ":for". |
| 1035 | * If we were not executing commands, decrement cs_looplevel. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1036 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1037 | if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1038 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1039 | cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1040 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1041 | // Jump back to the matching ":while" or ":for". Be careful |
| 1042 | // not to use a cs_line[] from an entry that isn't a ":while" |
| 1043 | // or ":for": It would make "current_line" invalid and can |
| 1044 | // cause a crash. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | if (!did_emsg && !got_int && !did_throw |
| 1046 | && cstack.cs_idx >= 0 |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1047 | && (cstack.cs_flags[cstack.cs_idx] |
| 1048 | & (CSF_WHILE | CSF_FOR)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1049 | && cstack.cs_line[cstack.cs_idx] >= 0 |
| 1050 | && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE)) |
| 1051 | { |
| 1052 | current_line = cstack.cs_line[cstack.cs_idx]; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1053 | // remember we jumped there |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1054 | cstack.cs_lflags |= CSL_HAD_LOOP; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1055 | line_breakcheck(); // check if CTRL-C typed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1057 | // Check for the next breakpoint at or after the ":while" |
| 1058 | // or ":for". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1059 | if (breakpoint != NULL) |
| 1060 | { |
| 1061 | *breakpoint = dbg_find_breakpoint( |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1062 | getline_equal(fgetline, cookie, getsourceline), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1063 | fname, |
| 1064 | ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1); |
| 1065 | *dbg_tick = debug_tick; |
| 1066 | } |
| 1067 | } |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1068 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1069 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1070 | // can only get here with ":endwhile" or ":endfor" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1071 | if (cstack.cs_idx >= 0) |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1072 | rewind_conditionals(&cstack, cstack.cs_idx - 1, |
| 1073 | CSF_WHILE | CSF_FOR, &cstack.cs_looplevel); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1074 | } |
| 1075 | } |
| 1076 | |
| 1077 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1078 | * For a ":while" or ":for" we need to remember the line number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1079 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1080 | else if (cstack.cs_lflags & CSL_HAD_LOOP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1081 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1082 | cstack.cs_lflags &= ~CSL_HAD_LOOP; |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 1083 | cstack.cs_line[cstack.cs_idx] = current_line_before; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1084 | } |
| 1085 | } |
| 1086 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1087 | // Check for the next breakpoint after a watchexpression |
Bram Moolenaar | c6f9f73 | 2018-02-11 19:06:26 +0100 | [diff] [blame] | 1088 | if (breakpoint != NULL && has_watchexpr()) |
| 1089 | { |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1090 | *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM); |
Bram Moolenaar | c6f9f73 | 2018-02-11 19:06:26 +0100 | [diff] [blame] | 1091 | *dbg_tick = debug_tick; |
| 1092 | } |
| 1093 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1094 | /* |
| 1095 | * When not inside any ":while" loop, clear remembered lines. |
| 1096 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1097 | if (cstack.cs_looplevel == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1098 | { |
| 1099 | if (lines_ga.ga_len > 0) |
| 1100 | { |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1101 | SOURCING_LNUM = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1102 | ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum; |
| 1103 | free_cmdlines(&lines_ga); |
| 1104 | } |
| 1105 | current_line = 0; |
| 1106 | } |
| 1107 | |
| 1108 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1109 | * A ":finally" makes did_emsg, got_int, and did_throw pending for |
| 1110 | * being restored at the ":endtry". Reset them here and set the |
| 1111 | * ACTIVE and FINALLY flags, so that the finally clause gets executed. |
| 1112 | * This includes the case where a missing ":endif", ":endwhile" or |
| 1113 | * ":endfor" was detected by the ":finally" itself. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1114 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1115 | if (cstack.cs_lflags & CSL_HAD_FINA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1116 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1117 | cstack.cs_lflags &= ~CSL_HAD_FINA; |
| 1118 | report_make_pending(cstack.cs_pending[cstack.cs_idx] |
| 1119 | & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1120 | did_throw ? (void *)current_exception : NULL); |
| 1121 | did_emsg = got_int = did_throw = FALSE; |
| 1122 | cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY; |
| 1123 | } |
| 1124 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1125 | // Update global "trylevel" for recursive calls to do_cmdline() from |
| 1126 | // within this loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1127 | trylevel = initial_trylevel + cstack.cs_trylevel; |
| 1128 | |
| 1129 | /* |
Bram Moolenaar | c1762cc | 2007-05-10 16:56:30 +0000 | [diff] [blame] | 1130 | * If the outermost try conditional (across function calls and sourced |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1131 | * files) is aborted because of an error, an interrupt, or an uncaught |
| 1132 | * exception, cancel everything. If it is left normally, reset |
| 1133 | * force_abort to get the non-EH compatible abortion behavior for |
| 1134 | * the rest of the script. |
| 1135 | */ |
| 1136 | if (trylevel == 0 && !did_emsg && !got_int && !did_throw) |
| 1137 | force_abort = FALSE; |
| 1138 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1139 | // Convert an interrupt to an exception if appropriate. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1140 | (void)do_intthrow(&cstack); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1141 | #endif // FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1142 | |
| 1143 | } |
| 1144 | /* |
| 1145 | * Continue executing command lines when: |
| 1146 | * - no CTRL-C typed, no aborting error, no exception thrown or try |
| 1147 | * conditionals need to be checked for executing finally clauses or |
| 1148 | * catching an interrupt exception |
| 1149 | * - didn't get an error message or lines are not typed |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1150 | * - there is a command after '|', inside a :if, :while, :for or :try, or |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | * looping for ":source" command or function call. |
| 1152 | */ |
| 1153 | while (!((got_int |
| 1154 | #ifdef FEAT_EVAL |
| 1155 | || (did_emsg && force_abort) || did_throw |
| 1156 | #endif |
| 1157 | ) |
| 1158 | #ifdef FEAT_EVAL |
| 1159 | && cstack.cs_trylevel == 0 |
| 1160 | #endif |
| 1161 | ) |
Bram Moolenaar | 00b8ae0 | 2012-08-23 18:43:10 +0200 | [diff] [blame] | 1162 | && !(did_emsg |
| 1163 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1164 | // Keep going when inside try/catch, so that the error can be |
| 1165 | // deal with, except when it is a syntax error, it may cause |
| 1166 | // the :endtry to be missed. |
Bram Moolenaar | 00b8ae0 | 2012-08-23 18:43:10 +0200 | [diff] [blame] | 1167 | && (cstack.cs_trylevel == 0 || did_emsg_syntax) |
| 1168 | #endif |
| 1169 | && used_getline |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1170 | && (getline_equal(fgetline, cookie, getexmodeline) |
| 1171 | || getline_equal(fgetline, cookie, getexline))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1172 | && (next_cmdline != NULL |
| 1173 | #ifdef FEAT_EVAL |
| 1174 | || cstack.cs_idx >= 0 |
| 1175 | #endif |
| 1176 | || (flags & DOCMD_REPEAT))); |
| 1177 | |
| 1178 | vim_free(cmdline_copy); |
Bram Moolenaar | 00b8ae0 | 2012-08-23 18:43:10 +0200 | [diff] [blame] | 1179 | did_emsg_syntax = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1180 | #ifdef FEAT_EVAL |
| 1181 | free_cmdlines(&lines_ga); |
| 1182 | ga_clear(&lines_ga); |
| 1183 | |
| 1184 | if (cstack.cs_idx >= 0) |
| 1185 | { |
| 1186 | /* |
| 1187 | * If a sourced file or executed function ran to its end, report the |
| 1188 | * unclosed conditional. |
| 1189 | */ |
| 1190 | if (!got_int && !did_throw |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1191 | && ((getline_equal(fgetline, cookie, getsourceline) |
| 1192 | && !source_finished(fgetline, cookie)) |
| 1193 | || (getline_equal(fgetline, cookie, get_func_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1194 | && !func_has_ended(real_cookie)))) |
| 1195 | { |
| 1196 | if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1197 | emsg(_(e_endtry)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1198 | else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1199 | emsg(_(e_endwhile)); |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1200 | else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1201 | emsg(_(e_endfor)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1202 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1203 | emsg(_(e_endif)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | /* |
| 1207 | * Reset "trylevel" in case of a ":finish" or ":return" or a missing |
| 1208 | * ":endtry" in a sourced file or executed function. If the try |
| 1209 | * conditional is in its finally clause, ignore anything pending. |
| 1210 | * If it is in a catch clause, finish the caught exception. |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1211 | * Also cleanup any "cs_forinfo" structures. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1212 | */ |
| 1213 | do |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1214 | { |
| 1215 | int idx = cleanup_conditionals(&cstack, 0, TRUE); |
| 1216 | |
Bram Moolenaar | 89e5d68 | 2005-01-17 22:06:23 +0000 | [diff] [blame] | 1217 | if (idx >= 0) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1218 | --idx; // remove try block not in its finally clause |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1219 | rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR, |
| 1220 | &cstack.cs_looplevel); |
| 1221 | } |
| 1222 | while (cstack.cs_idx >= 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | trylevel = initial_trylevel; |
| 1224 | } |
| 1225 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1226 | // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory |
| 1227 | // lack was reported above and the error message is to be converted to an |
| 1228 | // exception, do this now after rewinding the cstack. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1229 | do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1230 | ? (char_u *)"endfunction" : (char_u *)NULL); |
| 1231 | |
| 1232 | if (trylevel == 0) |
| 1233 | { |
| 1234 | /* |
| 1235 | * When an exception is being thrown out of the outermost try |
| 1236 | * conditional, discard the uncaught exception, disable the conversion |
| 1237 | * of interrupts or errors to exceptions, and ensure that no more |
| 1238 | * commands are executed. |
| 1239 | */ |
| 1240 | if (did_throw) |
| 1241 | { |
| 1242 | void *p = NULL; |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 1243 | msglist_T *messages = NULL, *next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1244 | |
| 1245 | /* |
| 1246 | * If the uncaught exception is a user exception, report it as an |
| 1247 | * error. If it is an error exception, display the saved error |
| 1248 | * message now. For an interrupt exception, do nothing; the |
| 1249 | * interrupt message is given elsewhere. |
| 1250 | */ |
| 1251 | switch (current_exception->type) |
| 1252 | { |
| 1253 | case ET_USER: |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1254 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1255 | _("E605: Exception not caught: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1256 | current_exception->value); |
| 1257 | p = vim_strsave(IObuff); |
| 1258 | break; |
| 1259 | case ET_ERROR: |
| 1260 | messages = current_exception->messages; |
| 1261 | current_exception->messages = NULL; |
| 1262 | break; |
| 1263 | case ET_INTERRUPT: |
| 1264 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1267 | estack_push(ETYPE_EXCEPT, current_exception->throw_name, |
| 1268 | current_exception->throw_lnum); |
Bram Moolenaar | e31ee86 | 2020-01-07 20:59:34 +0100 | [diff] [blame] | 1269 | ESTACK_CHECK_SETUP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1270 | current_exception->throw_name = NULL; |
| 1271 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1272 | discard_current_exception(); // uses IObuff if 'verbose' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | suppress_errthrow = TRUE; |
| 1274 | force_abort = TRUE; |
| 1275 | |
| 1276 | if (messages != NULL) |
| 1277 | { |
| 1278 | do |
| 1279 | { |
| 1280 | next = messages->next; |
| 1281 | emsg(messages->msg); |
| 1282 | vim_free(messages->msg); |
Bram Moolenaar | 5fbf3bc | 2020-06-01 21:13:11 +0200 | [diff] [blame] | 1283 | vim_free(messages->sfile); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1284 | vim_free(messages); |
| 1285 | messages = next; |
| 1286 | } |
| 1287 | while (messages != NULL); |
| 1288 | } |
| 1289 | else if (p != NULL) |
| 1290 | { |
Bram Moolenaar | b5443cc | 2019-01-15 20:19:40 +0100 | [diff] [blame] | 1291 | emsg(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1292 | vim_free(p); |
| 1293 | } |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1294 | vim_free(SOURCING_NAME); |
Bram Moolenaar | e31ee86 | 2020-01-07 20:59:34 +0100 | [diff] [blame] | 1295 | ESTACK_CHECK_NOW |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1296 | estack_pop(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
| 1299 | /* |
| 1300 | * On an interrupt or an aborting error not converted to an exception, |
| 1301 | * disable the conversion of errors to exceptions. (Interrupts are not |
Bram Moolenaar | 2196bce | 2020-04-12 20:01:11 +0200 | [diff] [blame] | 1302 | * converted anymore, here.) This enables also the interrupt message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | * when force_abort is set and did_emsg unset in case of an interrupt |
| 1304 | * from a finally clause after an error. |
| 1305 | */ |
| 1306 | else if (got_int || (did_emsg && force_abort)) |
| 1307 | suppress_errthrow = TRUE; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * The current cstack will be freed when do_cmdline() returns. An uncaught |
| 1312 | * exception will have to be rethrown in the previous cstack. If a function |
| 1313 | * has just returned or a script file was just finished and the previous |
| 1314 | * cstack belongs to the same function or, respectively, script file, it |
| 1315 | * will have to be checked for finally clauses to be executed due to the |
| 1316 | * ":return" or ":finish". This is done in do_one_cmd(). |
| 1317 | */ |
| 1318 | if (did_throw) |
| 1319 | need_rethrow = TRUE; |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1320 | if ((getline_equal(fgetline, cookie, getsourceline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | && ex_nesting_level > source_level(real_cookie)) |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1322 | || (getline_equal(fgetline, cookie, get_func_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1323 | && ex_nesting_level > func_level(real_cookie) + 1)) |
| 1324 | { |
| 1325 | if (!did_throw) |
| 1326 | check_cstack = TRUE; |
| 1327 | } |
| 1328 | else |
| 1329 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1330 | // When leaving a function, reduce nesting level. |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1331 | if (getline_equal(fgetline, cookie, get_func_line)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1332 | --ex_nesting_level; |
| 1333 | /* |
| 1334 | * Go to debug mode when returning from a function in which we are |
| 1335 | * single-stepping. |
| 1336 | */ |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1337 | if ((getline_equal(fgetline, cookie, getsourceline) |
| 1338 | || getline_equal(fgetline, cookie, get_func_line)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1339 | && ex_nesting_level + 1 <= debug_break_level) |
Bram Moolenaar | bf55e14 | 2010-11-16 11:32:01 +0100 | [diff] [blame] | 1340 | do_debug(getline_equal(fgetline, cookie, getsourceline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1341 | ? (char_u *)_("End of sourced file") |
| 1342 | : (char_u *)_("End of function")); |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * Restore the exception environment (done after returning from the |
| 1347 | * debugger). |
| 1348 | */ |
| 1349 | if (flags & DOCMD_EXCRESET) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1350 | restore_dbg_stuff(&debug_saved); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1351 | |
| 1352 | msg_list = saved_msg_list; |
Bram Moolenaar | 292b90d | 2020-03-18 15:23:16 +0100 | [diff] [blame] | 1353 | |
| 1354 | // Cleanup if "cs_emsg_silent_list" remains. |
| 1355 | if (cstack.cs_emsg_silent_list != NULL) |
| 1356 | { |
| 1357 | eslist_T *elem, *temp; |
| 1358 | |
| 1359 | for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp) |
| 1360 | { |
| 1361 | temp = elem->next; |
| 1362 | vim_free(elem); |
| 1363 | } |
| 1364 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1365 | #endif // FEAT_EVAL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1366 | |
| 1367 | /* |
| 1368 | * If there was too much output to fit on the command line, ask the user to |
| 1369 | * hit return before redrawing the screen. With the ":global" command we do |
| 1370 | * this only once after the command is finished. |
| 1371 | */ |
| 1372 | if (did_inc) |
| 1373 | { |
| 1374 | --RedrawingDisabled; |
| 1375 | --no_wait_return; |
| 1376 | msg_scroll = FALSE; |
| 1377 | |
| 1378 | /* |
| 1379 | * When just finished an ":if"-":else" which was typed, no need to |
| 1380 | * wait for hit-return. Also for an error situation. |
| 1381 | */ |
| 1382 | if (retval == FAIL |
| 1383 | #ifdef FEAT_EVAL |
| 1384 | || (did_endif && KeyTyped && !did_emsg) |
| 1385 | #endif |
| 1386 | ) |
| 1387 | { |
| 1388 | need_wait_return = FALSE; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1389 | msg_didany = FALSE; // don't wait when restarting edit |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1390 | } |
| 1391 | else if (need_wait_return) |
| 1392 | { |
| 1393 | /* |
| 1394 | * The msg_start() above clears msg_didout. The wait_return we do |
| 1395 | * here should not overwrite the command that may be shown before |
| 1396 | * doing that. |
| 1397 | */ |
| 1398 | msg_didout |= msg_didout_before_start; |
| 1399 | wait_return(FALSE); |
| 1400 | } |
| 1401 | } |
| 1402 | |
Bram Moolenaar | 2a94225 | 2012-11-28 23:03:07 +0100 | [diff] [blame] | 1403 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1404 | did_endif = FALSE; // in case do_cmdline used recursively |
Bram Moolenaar | 2a94225 | 2012-11-28 23:03:07 +0100 | [diff] [blame] | 1405 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1406 | /* |
| 1407 | * Reset if_level, in case a sourced script file contains more ":if" than |
| 1408 | * ":endif" (could be ":if x | foo | endif"). |
| 1409 | */ |
| 1410 | if_level = 0; |
Bram Moolenaar | 2e18a12 | 2012-11-28 19:10:54 +0100 | [diff] [blame] | 1411 | #endif |
Bram Moolenaar | d4ad0d4 | 2012-11-28 17:34:48 +0100 | [diff] [blame] | 1412 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | --call_depth; |
| 1414 | return retval; |
| 1415 | } |
| 1416 | |
| 1417 | #ifdef FEAT_EVAL |
| 1418 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1419 | * Obtain a line when inside a ":while" or ":for" loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1420 | */ |
| 1421 | static char_u * |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1422 | get_loop_line(int c, void *cookie, int indent, int do_concat) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1423 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1424 | struct loop_cookie *cp = (struct loop_cookie *)cookie; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | wcmd_T *wp; |
| 1426 | char_u *line; |
| 1427 | |
| 1428 | if (cp->current_line + 1 >= cp->lines_gap->ga_len) |
| 1429 | { |
| 1430 | if (cp->repeating) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1431 | return NULL; // trying to read past ":endwhile"/":endfor" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1433 | // First time inside the ":while"/":for": get line normally. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | if (cp->getline == NULL) |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1435 | line = getcmdline(c, 0L, indent, do_concat); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1436 | else |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1437 | line = cp->getline(c, cp->cookie, indent, do_concat); |
Bram Moolenaar | d68071d | 2006-05-02 22:08:30 +0000 | [diff] [blame] | 1438 | if (line != NULL && store_loop_line(cp->lines_gap, line) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1439 | ++cp->current_line; |
| 1440 | |
| 1441 | return line; |
| 1442 | } |
| 1443 | |
| 1444 | KeyTyped = FALSE; |
| 1445 | ++cp->current_line; |
| 1446 | wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1447 | SOURCING_LNUM = wp->lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | return vim_strsave(wp->line); |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Store a line in "gap" so that a ":while" loop can execute it again. |
| 1453 | */ |
| 1454 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1455 | store_loop_line(garray_T *gap, char_u *line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1456 | { |
| 1457 | if (ga_grow(gap, 1) == FAIL) |
| 1458 | return FAIL; |
| 1459 | ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line); |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1460 | ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1461 | ++gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | return OK; |
| 1463 | } |
| 1464 | |
| 1465 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1466 | * Free the lines stored for a ":while" or ":for" loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1467 | */ |
| 1468 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1469 | free_cmdlines(garray_T *gap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1470 | { |
| 1471 | while (gap->ga_len > 0) |
| 1472 | { |
| 1473 | vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line); |
| 1474 | --gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
| 1477 | #endif |
| 1478 | |
| 1479 | /* |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1480 | * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals |
| 1481 | * "func". * Otherwise return TRUE when "fgetline" equals "func". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1482 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1483 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1484 | getline_equal( |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1485 | char_u *(*fgetline)(int, void *, int, int), |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1486 | void *cookie UNUSED, // argument for fgetline() |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1487 | char_u *(*func)(int, void *, int, int)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1488 | { |
| 1489 | #ifdef FEAT_EVAL |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1490 | char_u *(*gp)(int, void *, int, int); |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1491 | struct loop_cookie *cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1492 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1493 | // When "fgetline" is "get_loop_line()" use the "cookie" to find the |
| 1494 | // function that's originally used to obtain the lines. This may be |
| 1495 | // nested several levels. |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1496 | gp = fgetline; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1497 | cp = (struct loop_cookie *)cookie; |
| 1498 | while (gp == get_loop_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1499 | { |
| 1500 | gp = cp->getline; |
| 1501 | cp = cp->cookie; |
| 1502 | } |
| 1503 | return gp == func; |
| 1504 | #else |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1505 | return fgetline == func; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1506 | #endif |
| 1507 | } |
| 1508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1509 | /* |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1510 | * If "fgetline" is get_loop_line(), return the cookie used by the original |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1511 | * getline function. Otherwise return "cookie". |
| 1512 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1513 | void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1514 | getline_cookie( |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1515 | char_u *(*fgetline)(int, void *, int, int) UNUSED, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1516 | void *cookie) // argument for fgetline() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1517 | { |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1518 | #ifdef FEAT_EVAL |
Bram Moolenaar | e96a249 | 2019-06-25 04:12:16 +0200 | [diff] [blame] | 1519 | char_u *(*gp)(int, void *, int, int); |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 1520 | struct loop_cookie *cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1521 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1522 | // When "fgetline" is "get_loop_line()" use the "cookie" to find the |
| 1523 | // cookie that's originally used to obtain the lines. This may be nested |
| 1524 | // several levels. |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1525 | gp = fgetline; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1526 | cp = (struct loop_cookie *)cookie; |
| 1527 | while (gp == get_loop_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1528 | { |
| 1529 | gp = cp->getline; |
| 1530 | cp = cp->cookie; |
| 1531 | } |
| 1532 | return cp; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1533 | #else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1534 | return cookie; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1535 | #endif |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 1536 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1537 | |
Bram Moolenaar | d5053d0 | 2020-06-28 15:51:16 +0200 | [diff] [blame] | 1538 | #if defined(FEAT_EVAL) || defined(PROT) |
| 1539 | /* |
| 1540 | * Get the next line source line without advancing. |
| 1541 | */ |
| 1542 | char_u * |
| 1543 | getline_peek( |
| 1544 | char_u *(*fgetline)(int, void *, int, int) UNUSED, |
| 1545 | void *cookie) // argument for fgetline() |
| 1546 | { |
| 1547 | char_u *(*gp)(int, void *, int, int); |
| 1548 | struct loop_cookie *cp; |
| 1549 | wcmd_T *wp; |
| 1550 | |
| 1551 | // When "fgetline" is "get_loop_line()" use the "cookie" to find the |
| 1552 | // cookie that's originally used to obtain the lines. This may be nested |
| 1553 | // several levels. |
| 1554 | gp = fgetline; |
| 1555 | cp = (struct loop_cookie *)cookie; |
| 1556 | while (gp == get_loop_line) |
| 1557 | { |
| 1558 | if (cp->current_line + 1 < cp->lines_gap->ga_len) |
| 1559 | { |
| 1560 | // executing lines a second time, use the stored copy |
| 1561 | wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1; |
| 1562 | return wp->line; |
| 1563 | } |
| 1564 | gp = cp->getline; |
| 1565 | cp = cp->cookie; |
| 1566 | } |
| 1567 | if (gp == getsourceline) |
| 1568 | return source_nextline(cp); |
| 1569 | return NULL; |
| 1570 | } |
| 1571 | #endif |
| 1572 | |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* |
| 1575 | * Helper function to apply an offset for buffer commands, i.e. ":bdelete", |
| 1576 | * ":bwipeout", etc. |
| 1577 | * Returns the buffer number. |
| 1578 | */ |
| 1579 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1580 | compute_buffer_local_count(int addr_type, int lnum, int offset) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1581 | { |
| 1582 | buf_T *buf; |
Bram Moolenaar | 4d84d93 | 2014-11-30 14:50:16 +0100 | [diff] [blame] | 1583 | buf_T *nextbuf; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1584 | int count = offset; |
| 1585 | |
| 1586 | buf = firstbuf; |
| 1587 | while (buf->b_next != NULL && buf->b_fnum < lnum) |
| 1588 | buf = buf->b_next; |
| 1589 | while (count != 0) |
| 1590 | { |
Bram Moolenaar | 4d84d93 | 2014-11-30 14:50:16 +0100 | [diff] [blame] | 1591 | count += (offset < 0) ? 1 : -1; |
| 1592 | nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; |
| 1593 | if (nextbuf == NULL) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1594 | break; |
Bram Moolenaar | 4d84d93 | 2014-11-30 14:50:16 +0100 | [diff] [blame] | 1595 | buf = nextbuf; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1596 | if (addr_type == ADDR_LOADED_BUFFERS) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1597 | // skip over unloaded buffers |
Bram Moolenaar | 4d84d93 | 2014-11-30 14:50:16 +0100 | [diff] [blame] | 1598 | while (buf->b_ml.ml_mfp == NULL) |
| 1599 | { |
| 1600 | nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; |
| 1601 | if (nextbuf == NULL) |
| 1602 | break; |
| 1603 | buf = nextbuf; |
| 1604 | } |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1605 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1606 | // we might have gone too far, last buffer is not loadedd |
Bram Moolenaar | 4d84d93 | 2014-11-30 14:50:16 +0100 | [diff] [blame] | 1607 | if (addr_type == ADDR_LOADED_BUFFERS) |
| 1608 | while (buf->b_ml.ml_mfp == NULL) |
| 1609 | { |
| 1610 | nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next; |
| 1611 | if (nextbuf == NULL) |
| 1612 | break; |
| 1613 | buf = nextbuf; |
| 1614 | } |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1615 | return buf->b_fnum; |
| 1616 | } |
| 1617 | |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 1618 | /* |
| 1619 | * Return the window number of "win". |
| 1620 | * When "win" is NULL return the number of windows. |
| 1621 | */ |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 1622 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1623 | current_win_nr(win_T *win) |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 1624 | { |
| 1625 | win_T *wp; |
| 1626 | int nr = 0; |
| 1627 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1628 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 1629 | { |
| 1630 | ++nr; |
| 1631 | if (wp == win) |
| 1632 | break; |
| 1633 | } |
| 1634 | return nr; |
| 1635 | } |
| 1636 | |
| 1637 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1638 | current_tab_nr(tabpage_T *tab) |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 1639 | { |
| 1640 | tabpage_T *tp; |
| 1641 | int nr = 0; |
| 1642 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 1643 | FOR_ALL_TABPAGES(tp) |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 1644 | { |
| 1645 | ++nr; |
| 1646 | if (tp == tab) |
| 1647 | break; |
| 1648 | } |
| 1649 | return nr; |
| 1650 | } |
| 1651 | |
Bram Moolenaar | 6e36b1c | 2020-07-17 20:47:51 +0200 | [diff] [blame] | 1652 | static int |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1653 | comment_start(char_u *p, int starts_with_colon UNUSED) |
| 1654 | { |
| 1655 | #ifdef FEAT_EVAL |
| 1656 | if (in_vim9script()) |
| 1657 | return p[0] == '#' && p[1] != '{' && !starts_with_colon; |
| 1658 | #endif |
| 1659 | return *p == '"'; |
| 1660 | } |
| 1661 | |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 1662 | # define CURRENT_WIN_NR current_win_nr(curwin) |
| 1663 | # define LAST_WIN_NR current_win_nr(NULL) |
| 1664 | # define CURRENT_TAB_NR current_tab_nr(curtab) |
| 1665 | # define LAST_TAB_NR current_tab_nr(NULL) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1666 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1667 | /* |
| 1668 | * Execute one Ex command. |
| 1669 | * |
| 1670 | * If 'sourcing' is TRUE, the command will be included in the error message. |
| 1671 | * |
| 1672 | * 1. skip comment lines and leading space |
| 1673 | * 2. handle command modifiers |
Bram Moolenaar | 1c40a66 | 2014-11-27 16:38:11 +0100 | [diff] [blame] | 1674 | * 3. find the command |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1675 | * 4. parse range |
Bram Moolenaar | 1c40a66 | 2014-11-27 16:38:11 +0100 | [diff] [blame] | 1676 | * 5. Parse the command. |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1677 | * 6. parse arguments |
| 1678 | * 7. switch on command name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1679 | * |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1680 | * Note: "fgetline" can be NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1681 | * |
| 1682 | * This function may be called recursively! |
| 1683 | */ |
| 1684 | #if (_MSC_VER == 1200) |
| 1685 | /* |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1686 | * Avoid optimisation bug in VC++ version 6.0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | */ |
Bram Moolenaar | 281bdce | 2005-01-25 21:53:18 +0000 | [diff] [blame] | 1688 | #pragma optimize( "g", off ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1689 | #endif |
| 1690 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 1691 | do_one_cmd( |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 1692 | char_u **cmdlinep, |
| 1693 | int sourcing, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1694 | #ifdef FEAT_EVAL |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 1695 | cstack_T *cstack, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1696 | #endif |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 1697 | char_u *(*fgetline)(int, void *, int, int), |
| 1698 | void *cookie) // argument for fgetline() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1699 | { |
Bram Moolenaar | ddef129 | 2019-12-16 17:10:33 +0100 | [diff] [blame] | 1700 | char_u *p; |
| 1701 | linenr_T lnum; |
| 1702 | long n; |
| 1703 | char *errormsg = NULL; // error message |
| 1704 | char_u *after_modifier = NULL; |
| 1705 | exarg_T ea; // Ex command arguments |
| 1706 | int save_msg_scroll = msg_scroll; |
| 1707 | cmdmod_T save_cmdmod; |
| 1708 | int save_reg_executing = reg_executing; |
| 1709 | int ni; // set when Not Implemented |
| 1710 | char_u *cmd; |
Bram Moolenaar | 6e36b1c | 2020-07-17 20:47:51 +0200 | [diff] [blame] | 1711 | int starts_with_colon = FALSE; |
Bram Moolenaar | 83f37b9 | 2020-02-23 14:35:01 +0100 | [diff] [blame] | 1712 | #ifdef FEAT_EVAL |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 1713 | int starts_with_quote; |
| 1714 | int vim9script = in_vim9script(); |
Bram Moolenaar | 83f37b9 | 2020-02-23 14:35:01 +0100 | [diff] [blame] | 1715 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1716 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 1717 | CLEAR_FIELD(ea); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1718 | ea.line1 = 1; |
| 1719 | ea.line2 = 1; |
| 1720 | #ifdef FEAT_EVAL |
| 1721 | ++ex_nesting_level; |
| 1722 | #endif |
| 1723 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1724 | // When the last file has not been edited :q has to be typed twice. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1725 | if (quitmore |
| 1726 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1727 | // avoid that a function call in 'statusline' does this |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 1728 | && !getline_equal(fgetline, cookie, get_func_line) |
Bram Moolenaar | b2c5a5a | 2013-02-14 22:11:39 +0100 | [diff] [blame] | 1729 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1730 | // avoid that an autocommand, e.g. QuitPre, does this |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1731 | && !getline_equal(fgetline, cookie, getnextac)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1732 | --quitmore; |
| 1733 | |
| 1734 | /* |
| 1735 | * Reset browse, confirm, etc.. They are restored when returning, for |
| 1736 | * recursive calls. |
| 1737 | */ |
| 1738 | save_cmdmod = cmdmod; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1739 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1740 | // "#!anything" is handled like a comment. |
Bram Moolenaar | cbb37ad | 2006-08-16 15:04:21 +0000 | [diff] [blame] | 1741 | if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!') |
| 1742 | goto doend; |
| 1743 | |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 1744 | /* |
| 1745 | * 1. Skip comment lines and leading white space and colons. |
| 1746 | * 2. Handle command modifiers. |
| 1747 | */ |
| 1748 | // The "ea" structure holds the arguments that can be used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1749 | ea.cmd = *cmdlinep; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 1750 | ea.cmdlinep = cmdlinep; |
| 1751 | ea.getline = fgetline; |
| 1752 | ea.cookie = cookie; |
| 1753 | #ifdef FEAT_EVAL |
| 1754 | ea.cstack = cstack; |
Bram Moolenaar | 83f37b9 | 2020-02-23 14:35:01 +0100 | [diff] [blame] | 1755 | starts_with_colon = *skipwhite(ea.cmd) == ':'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1756 | #endif |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 1757 | if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL) |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 1758 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1759 | |
Bram Moolenaar | 7b668e8 | 2016-08-23 23:51:21 +0200 | [diff] [blame] | 1760 | after_modifier = ea.cmd; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1761 | |
| 1762 | #ifdef FEAT_EVAL |
| 1763 | ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0 |
| 1764 | && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)); |
| 1765 | #else |
| 1766 | ea.skip = (if_level > 0); |
| 1767 | #endif |
| 1768 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1769 | /* |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1770 | * 3. Skip over the range to find the command. Let "p" point to after it. |
| 1771 | * |
| 1772 | * We need the command to know what kind of range it uses. |
| 1773 | */ |
| 1774 | cmd = ea.cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 1775 | #ifdef FEAT_EVAL |
Bram Moolenaar | 13e12b8 | 2020-07-24 18:47:22 +0200 | [diff] [blame] | 1776 | starts_with_quote = vim9script && !starts_with_colon && *ea.cmd == '\''; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 1777 | if (!starts_with_quote) |
| 1778 | #endif |
| 1779 | ea.cmd = skip_range(ea.cmd, NULL); |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1780 | if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL) |
| 1781 | ea.cmd = skipwhite(ea.cmd + 1); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1782 | |
| 1783 | #ifdef FEAT_EVAL |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 1784 | if (vim9script && !starts_with_colon) |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 1785 | { |
| 1786 | if (ea.cmd > cmd) |
| 1787 | { |
| 1788 | emsg(_(e_colon_required)); |
| 1789 | goto doend; |
| 1790 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1791 | p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL); |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 1792 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1793 | else |
| 1794 | #endif |
| 1795 | p = find_ex_command(&ea, NULL, NULL, NULL); |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1796 | |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 1797 | #ifdef FEAT_EVAL |
| 1798 | # ifdef FEAT_PROFILE |
| 1799 | // Count this line for profiling if skip is TRUE. |
| 1800 | if (do_profiling == PROF_YES |
| 1801 | && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0 |
| 1802 | && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE)))) |
| 1803 | { |
| 1804 | int skip = did_emsg || got_int || did_throw; |
| 1805 | |
| 1806 | if (ea.cmdidx == CMD_catch) |
| 1807 | skip = !skip && !(cstack->cs_idx >= 0 |
| 1808 | && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN) |
| 1809 | && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT)); |
| 1810 | else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif) |
| 1811 | skip = skip || !(cstack->cs_idx >= 0 |
| 1812 | && !(cstack->cs_flags[cstack->cs_idx] |
| 1813 | & (CSF_ACTIVE | CSF_TRUE))); |
| 1814 | else if (ea.cmdidx == CMD_finally) |
| 1815 | skip = FALSE; |
| 1816 | else if (ea.cmdidx != CMD_endif |
| 1817 | && ea.cmdidx != CMD_endfor |
| 1818 | && ea.cmdidx != CMD_endtry |
| 1819 | && ea.cmdidx != CMD_endwhile) |
| 1820 | skip = ea.skip; |
| 1821 | |
| 1822 | if (!skip) |
| 1823 | { |
| 1824 | if (getline_equal(fgetline, cookie, get_func_line)) |
| 1825 | func_line_exec(getline_cookie(fgetline, cookie)); |
| 1826 | else if (getline_equal(fgetline, cookie, getsourceline)) |
| 1827 | script_line_exec(); |
| 1828 | } |
| 1829 | } |
| 1830 | # endif |
| 1831 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1832 | // May go to debug mode. If this happens and the ">quit" debug command is |
| 1833 | // used, throw an interrupt exception and skip the next command. |
Bram Moolenaar | 7feb35e | 2018-08-21 17:49:54 +0200 | [diff] [blame] | 1834 | dbg_check_breakpoint(&ea); |
| 1835 | if (!ea.skip && got_int) |
| 1836 | { |
| 1837 | ea.skip = TRUE; |
| 1838 | (void)do_intthrow(cstack); |
| 1839 | } |
| 1840 | #endif |
| 1841 | |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1842 | /* |
| 1843 | * 4. parse a range specifier of the form: addr [,addr] [;addr] .. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1844 | * |
| 1845 | * where 'addr' is: |
| 1846 | * |
| 1847 | * % (entire file) |
| 1848 | * $ [+-NUM] |
| 1849 | * 'x [+-NUM] (where x denotes a currently defined mark) |
| 1850 | * . [+-NUM] |
| 1851 | * [+-NUM].. |
| 1852 | * NUM |
| 1853 | * |
| 1854 | * The ea.cmd pointer is updated to point to the first character following the |
| 1855 | * range spec. If an initial address is found, but no second, the upper bound |
| 1856 | * is equal to the lower. |
| 1857 | */ |
| 1858 | |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 1859 | // ea.addr_type for user commands is set by find_ucmd |
Bram Moolenaar | 84c8e5a | 2015-01-14 15:47:36 +0100 | [diff] [blame] | 1860 | if (!IS_USER_CMDIDX(ea.cmdidx)) |
| 1861 | { |
| 1862 | if (ea.cmdidx != CMD_SIZE) |
| 1863 | ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type; |
| 1864 | else |
| 1865 | ea.addr_type = ADDR_LINES; |
| 1866 | |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 1867 | // :wincmd range depends on the argument. |
Bram Moolenaar | 164f326 | 2015-01-14 21:22:01 +0100 | [diff] [blame] | 1868 | if (ea.cmdidx == CMD_wincmd && p != NULL) |
| 1869 | get_wincmd_addr_type(skipwhite(p), &ea); |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 1870 | #ifdef FEAT_QUICKFIX |
| 1871 | // :.cc in quickfix window uses line number |
| 1872 | if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf)) |
| 1873 | ea.addr_type = ADDR_OTHER; |
| 1874 | #endif |
Bram Moolenaar | 84c8e5a | 2015-01-14 15:47:36 +0100 | [diff] [blame] | 1875 | } |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1876 | |
Bram Moolenaar | 84c8e5a | 2015-01-14 15:47:36 +0100 | [diff] [blame] | 1877 | ea.cmd = cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 1878 | #ifdef FEAT_EVAL |
| 1879 | if (!starts_with_quote) |
| 1880 | #endif |
| 1881 | if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL) |
| 1882 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1883 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1884 | /* |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 1885 | * 5. Parse the command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1886 | */ |
| 1887 | |
| 1888 | /* |
| 1889 | * Skip ':' and any white space |
| 1890 | */ |
| 1891 | ea.cmd = skipwhite(ea.cmd); |
| 1892 | while (*ea.cmd == ':') |
| 1893 | ea.cmd = skipwhite(ea.cmd + 1); |
| 1894 | |
| 1895 | /* |
| 1896 | * If we got a line, but no command, then go to the line. |
| 1897 | * If we find a '|' or '\n' we set ea.nextcmd. |
| 1898 | */ |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 1899 | if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon) |
| 1900 | || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1901 | { |
| 1902 | /* |
| 1903 | * strange vi behaviour: |
| 1904 | * ":3" jumps to line 3 |
| 1905 | * ":3|..." prints line 3 |
| 1906 | * ":|" prints current line |
| 1907 | */ |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1908 | if (ea.skip) // skip this if inside :if |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1909 | goto doend; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1910 | if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1911 | { |
| 1912 | ea.cmdidx = CMD_print; |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 1913 | ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1914 | if ((errormsg = invalid_range(&ea)) == NULL) |
| 1915 | { |
| 1916 | correct_range(&ea); |
| 1917 | ex_print(&ea); |
| 1918 | } |
| 1919 | } |
| 1920 | else if (ea.addr_count != 0) |
| 1921 | { |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 1922 | if (ea.line2 > curbuf->b_ml.ml_line_count) |
| 1923 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1924 | // With '-' in 'cpoptions' a line number past the file is an |
| 1925 | // error, otherwise put it at the end of the file. |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 1926 | if (vim_strchr(p_cpo, CPO_MINUS) != NULL) |
| 1927 | ea.line2 = -1; |
| 1928 | else |
| 1929 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 1930 | } |
| 1931 | |
| 1932 | if (ea.line2 < 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1933 | errormsg = _(e_invrange); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1934 | else |
| 1935 | { |
| 1936 | if (ea.line2 == 0) |
| 1937 | curwin->w_cursor.lnum = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1938 | else |
| 1939 | curwin->w_cursor.lnum = ea.line2; |
| 1940 | beginline(BL_SOL | BL_FIX); |
| 1941 | } |
| 1942 | } |
| 1943 | goto doend; |
| 1944 | } |
| 1945 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1946 | // If this looks like an undefined user command and there are CmdUndefined |
| 1947 | // autocommands defined, trigger the matching autocommands. |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 1948 | if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip |
| 1949 | && ASCII_ISUPPER(*ea.cmd) |
| 1950 | && has_cmdundefined()) |
| 1951 | { |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 1952 | int ret; |
| 1953 | |
Bram Moolenaar | 5a31b46 | 2014-08-23 14:16:20 +0200 | [diff] [blame] | 1954 | p = ea.cmd; |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 1955 | while (ASCII_ISALNUM(*p)) |
| 1956 | ++p; |
Bram Moolenaar | 158ea17 | 2020-06-18 17:28:39 +0200 | [diff] [blame] | 1957 | p = vim_strnsave(ea.cmd, p - ea.cmd); |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 1958 | ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL); |
| 1959 | vim_free(p); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1960 | // If the autocommands did something and didn't cause an error, try |
| 1961 | // finding the command again. |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1962 | p = (ret |
| 1963 | #ifdef FEAT_EVAL |
| 1964 | && !aborting() |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 1965 | #endif |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1966 | ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1967 | } |
Bram Moolenaar | d500516 | 2014-08-22 23:05:54 +0200 | [diff] [blame] | 1968 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1969 | if (p == NULL) |
| 1970 | { |
| 1971 | if (!ea.skip) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1972 | errormsg = _("E464: Ambiguous use of user-defined command"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1973 | goto doend; |
| 1974 | } |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1975 | // Check for wrong commands. |
Bram Moolenaar | 6f9a476 | 2017-06-22 20:39:17 +0200 | [diff] [blame] | 1976 | if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78 |
| 1977 | && !IS_USER_CMDIDX(ea.cmdidx)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1978 | { |
| 1979 | errormsg = uc_fun_cmd(); |
| 1980 | goto doend; |
| 1981 | } |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 1982 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1983 | if (ea.cmdidx == CMD_SIZE) |
| 1984 | { |
| 1985 | if (!ea.skip) |
| 1986 | { |
| 1987 | STRCPY(IObuff, _("E492: Not an editor command")); |
| 1988 | if (!sourcing) |
Bram Moolenaar | 7b668e8 | 2016-08-23 23:51:21 +0200 | [diff] [blame] | 1989 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 1990 | // If the modifier was parsed OK the error must be in the |
| 1991 | // following command |
Bram Moolenaar | 7b668e8 | 2016-08-23 23:51:21 +0200 | [diff] [blame] | 1992 | if (after_modifier != NULL) |
| 1993 | append_command(after_modifier); |
| 1994 | else |
| 1995 | append_command(*cmdlinep); |
| 1996 | } |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1997 | errormsg = (char *)IObuff; |
Bram Moolenaar | 00b8ae0 | 2012-08-23 18:43:10 +0200 | [diff] [blame] | 1998 | did_emsg_syntax = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1999 | } |
| 2000 | goto doend; |
| 2001 | } |
| 2002 | |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2003 | ni = (!IS_USER_CMDIDX(ea.cmdidx) |
| 2004 | && (cmdnames[ea.cmdidx].cmd_func == ex_ni |
Bram Moolenaar | 7bb7555 | 2007-07-16 18:39:49 +0000 | [diff] [blame] | 2005 | #ifdef HAVE_EX_SCRIPT_NI |
| 2006 | || cmdnames[ea.cmdidx].cmd_func == ex_script_ni |
| 2007 | #endif |
| 2008 | )); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2009 | |
| 2010 | #ifndef FEAT_EVAL |
| 2011 | /* |
| 2012 | * When the expression evaluation is disabled, recognize the ":if" and |
| 2013 | * ":endif" commands and ignore everything in between it. |
| 2014 | */ |
| 2015 | if (ea.cmdidx == CMD_if) |
| 2016 | ++if_level; |
| 2017 | if (if_level) |
| 2018 | { |
| 2019 | if (ea.cmdidx == CMD_endif) |
| 2020 | --if_level; |
| 2021 | goto doend; |
| 2022 | } |
| 2023 | |
| 2024 | #endif |
| 2025 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2026 | // forced commands |
Bram Moolenaar | 196b3b0 | 2008-06-20 16:51:41 +0000 | [diff] [blame] | 2027 | if (*p == '!' && ea.cmdidx != CMD_substitute |
| 2028 | && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2029 | { |
| 2030 | ++p; |
| 2031 | ea.forceit = TRUE; |
| 2032 | } |
| 2033 | else |
| 2034 | ea.forceit = FALSE; |
| 2035 | |
| 2036 | /* |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2037 | * 6. Parse arguments. Then check for errors. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2038 | */ |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2039 | if (!IS_USER_CMDIDX(ea.cmdidx)) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 2040 | ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2041 | |
| 2042 | if (!ea.skip) |
| 2043 | { |
| 2044 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2045 | if (sandbox != 0 && !(ea.argt & EX_SBOXOK)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2046 | { |
Bram Moolenaar | 8c62a08 | 2019-02-08 14:34:10 +0100 | [diff] [blame] | 2047 | // Command not allowed in sandbox. |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2048 | errormsg = _(e_sandbox); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | goto doend; |
| 2050 | } |
| 2051 | #endif |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2052 | if (restricted != 0 && (ea.argt & EX_RESTRICT)) |
Bram Moolenaar | 8c62a08 | 2019-02-08 14:34:10 +0100 | [diff] [blame] | 2053 | { |
| 2054 | errormsg = _("E981: Command not allowed in rvim"); |
| 2055 | goto doend; |
| 2056 | } |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2057 | if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2058 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2059 | // Command not allowed in non-'modifiable' buffer |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2060 | errormsg = _(e_modifiable); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2061 | goto doend; |
| 2062 | } |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 2063 | |
Bram Moolenaar | 37394ff | 2020-07-25 19:38:18 +0200 | [diff] [blame] | 2064 | if (!IS_USER_CMDIDX(ea.cmdidx)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | { |
Bram Moolenaar | 37394ff | 2020-07-25 19:38:18 +0200 | [diff] [blame] | 2066 | #ifdef FEAT_CMDWIN |
| 2067 | if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN)) |
| 2068 | { |
| 2069 | // Command not allowed in the command line window |
| 2070 | errormsg = _(e_cmdwin); |
| 2071 | goto doend; |
| 2072 | } |
| 2073 | #endif |
| 2074 | if (text_locked() && !(ea.argt & EX_LOCK_OK)) |
| 2075 | { |
| 2076 | // Command not allowed when text is locked |
| 2077 | errormsg = _(get_text_locked_msg()); |
| 2078 | goto doend; |
| 2079 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2080 | } |
Bram Moolenaar | 379fb76 | 2018-08-30 15:58:28 +0200 | [diff] [blame] | 2081 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2082 | // Disallow editing another buffer when "curbuf_lock" is set. |
| 2083 | // Do allow ":checktime" (it is postponed). |
| 2084 | // Do allow ":edit" (check for an argument later). |
| 2085 | // Do allow ":file" with no arguments (check for an argument later). |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2086 | if (!(ea.argt & EX_CMDWIN) |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 2087 | && ea.cmdidx != CMD_checktime |
Bram Moolenaar | 379fb76 | 2018-08-30 15:58:28 +0200 | [diff] [blame] | 2088 | && ea.cmdidx != CMD_edit |
| 2089 | && ea.cmdidx != CMD_file |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2090 | && !IS_USER_CMDIDX(ea.cmdidx) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2091 | && curbuf_locked()) |
| 2092 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2093 | |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2094 | if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2095 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2096 | // no range allowed |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2097 | errormsg = _(e_norange); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | goto doend; |
| 2099 | } |
| 2100 | } |
| 2101 | |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2102 | if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2103 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2104 | errormsg = _(e_nobang); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2105 | goto doend; |
| 2106 | } |
| 2107 | |
| 2108 | /* |
| 2109 | * Don't complain about the range if it is not used |
| 2110 | * (could happen if line_count is accidentally set to 0). |
| 2111 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2112 | if (!ea.skip && !ni && (ea.argt & EX_RANGE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2113 | { |
| 2114 | /* |
| 2115 | * If the range is backwards, ask for confirmation and, if given, swap |
| 2116 | * ea.line1 & ea.line2 so it's forwards again. |
| 2117 | * When global command is busy, don't ask, will fail below. |
| 2118 | */ |
| 2119 | if (!global_busy && ea.line1 > ea.line2) |
| 2120 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2121 | if (msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2122 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2123 | if (sourcing || exmode_active) |
| 2124 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2125 | errormsg = _("E493: Backwards range given"); |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2126 | goto doend; |
| 2127 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | if (ask_yesno((char_u *) |
| 2129 | _("Backwards range given, OK to swap"), FALSE) != 'y') |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2130 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2131 | } |
| 2132 | lnum = ea.line1; |
| 2133 | ea.line1 = ea.line2; |
| 2134 | ea.line2 = lnum; |
| 2135 | } |
| 2136 | if ((errormsg = invalid_range(&ea)) != NULL) |
| 2137 | goto doend; |
| 2138 | } |
| 2139 | |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2140 | if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0) |
| 2141 | // default is 1, not cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2142 | ea.line2 = 1; |
| 2143 | |
| 2144 | correct_range(&ea); |
| 2145 | |
| 2146 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2147 | if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy |
Bram Moolenaar | a330695 | 2016-01-02 21:41:06 +0100 | [diff] [blame] | 2148 | && ea.addr_type == ADDR_LINES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2149 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2150 | // Put the first line at the start of a closed fold, put the last line |
| 2151 | // at the end of a closed fold. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2152 | (void)hasFolding(ea.line1, &ea.line1, NULL); |
| 2153 | (void)hasFolding(ea.line2, NULL, &ea.line2); |
| 2154 | } |
| 2155 | #endif |
| 2156 | |
| 2157 | #ifdef FEAT_QUICKFIX |
| 2158 | /* |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2159 | * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2160 | * option here, so things like % get expanded. |
| 2161 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2162 | p = replace_makeprg(&ea, p, cmdlinep); |
| 2163 | if (p == NULL) |
| 2164 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | #endif |
| 2166 | |
| 2167 | /* |
| 2168 | * Skip to start of argument. |
| 2169 | * Don't do this for the ":!" command, because ":!! -l" needs the space. |
| 2170 | */ |
| 2171 | if (ea.cmdidx == CMD_bang) |
| 2172 | ea.arg = p; |
| 2173 | else |
| 2174 | ea.arg = skipwhite(p); |
| 2175 | |
Bram Moolenaar | 379fb76 | 2018-08-30 15:58:28 +0200 | [diff] [blame] | 2176 | // ":file" cannot be run with an argument when "curbuf_lock" is set |
| 2177 | if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked()) |
| 2178 | goto doend; |
| 2179 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2180 | /* |
| 2181 | * Check for "++opt=val" argument. |
| 2182 | * Must be first, allow ":w ++enc=utf8 !cmd" |
| 2183 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2184 | if (ea.argt & EX_ARGOPT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2185 | while (ea.arg[0] == '+' && ea.arg[1] == '+') |
| 2186 | if (getargopt(&ea) == FAIL && !ni) |
| 2187 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2188 | errormsg = _(e_invarg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2189 | goto doend; |
| 2190 | } |
| 2191 | |
| 2192 | if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) |
| 2193 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2194 | if (*ea.arg == '>') // append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2195 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2196 | if (*++ea.arg != '>') // typed wrong |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2197 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2198 | errormsg = _("E494: Use w or w>>"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2199 | goto doend; |
| 2200 | } |
| 2201 | ea.arg = skipwhite(ea.arg + 1); |
| 2202 | ea.append = TRUE; |
| 2203 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2204 | else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2205 | { |
| 2206 | ++ea.arg; |
| 2207 | ea.usefilter = TRUE; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | if (ea.cmdidx == CMD_read) |
| 2212 | { |
| 2213 | if (ea.forceit) |
| 2214 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2215 | ea.usefilter = TRUE; // :r! filter if ea.forceit |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2216 | ea.forceit = FALSE; |
| 2217 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2218 | else if (*ea.arg == '!') // :r !filter |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2219 | { |
| 2220 | ++ea.arg; |
| 2221 | ea.usefilter = TRUE; |
| 2222 | } |
| 2223 | } |
| 2224 | |
| 2225 | if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) |
| 2226 | { |
| 2227 | ea.amount = 1; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2228 | while (*ea.arg == *ea.cmd) // count number of '>' or '<' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2229 | { |
| 2230 | ++ea.arg; |
| 2231 | ++ea.amount; |
| 2232 | } |
| 2233 | ea.arg = skipwhite(ea.arg); |
| 2234 | } |
| 2235 | |
| 2236 | /* |
| 2237 | * Check for "+command" argument, before checking for next command. |
| 2238 | * Don't do this for ":read !cmd" and ":write !cmd". |
| 2239 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2240 | if ((ea.argt & EX_CMDARG) && !ea.usefilter) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | ea.do_ecmd_cmd = getargcmd(&ea.arg); |
| 2242 | |
| 2243 | /* |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2244 | * Check for '|' to separate commands and '"' or '#' to start comments. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2245 | * Don't do this for ":read !cmd" and ":write !cmd". |
| 2246 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2247 | if ((ea.argt & EX_TRLBAR) && !ea.usefilter) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2248 | separate_nextcmd(&ea); |
| 2249 | |
| 2250 | /* |
| 2251 | * Check for <newline> to end a shell command. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2252 | * Also do this for ":read !cmd", ":write !cmd" and ":global". |
| 2253 | * Any others? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2254 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2255 | else if (ea.cmdidx == CMD_bang |
Bram Moolenaar | 67883b4 | 2017-07-27 22:57:00 +0200 | [diff] [blame] | 2256 | || ea.cmdidx == CMD_terminal |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2257 | || ea.cmdidx == CMD_global |
| 2258 | || ea.cmdidx == CMD_vglobal |
| 2259 | || ea.usefilter) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2260 | { |
| 2261 | for (p = ea.arg; *p; ++p) |
| 2262 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2263 | // Remove one backslash before a newline, so that it's possible to |
| 2264 | // pass a newline to the shell and also a newline that is preceded |
| 2265 | // with a backslash. This makes it impossible to end a shell |
| 2266 | // command in a backslash, but that doesn't appear useful. |
| 2267 | // Halving the number of backslashes is incompatible with previous |
| 2268 | // versions. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2269 | if (*p == '\\' && p[1] == '\n') |
Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 2270 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2271 | else if (*p == '\n') |
| 2272 | { |
| 2273 | ea.nextcmd = p + 1; |
| 2274 | *p = NUL; |
| 2275 | break; |
| 2276 | } |
| 2277 | } |
| 2278 | } |
| 2279 | |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2280 | if ((ea.argt & EX_DFLALL) && ea.addr_count == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2281 | { |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 2282 | buf_T *buf; |
| 2283 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2284 | ea.line1 = 1; |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 2285 | switch (ea.addr_type) |
| 2286 | { |
| 2287 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2288 | case ADDR_OTHER: |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 2289 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 2290 | break; |
| 2291 | case ADDR_LOADED_BUFFERS: |
| 2292 | buf = firstbuf; |
| 2293 | while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL) |
| 2294 | buf = buf->b_next; |
| 2295 | ea.line1 = buf->b_fnum; |
| 2296 | buf = lastbuf; |
| 2297 | while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL) |
| 2298 | buf = buf->b_prev; |
| 2299 | ea.line2 = buf->b_fnum; |
| 2300 | break; |
| 2301 | case ADDR_BUFFERS: |
| 2302 | ea.line1 = firstbuf->b_fnum; |
| 2303 | ea.line2 = lastbuf->b_fnum; |
| 2304 | break; |
| 2305 | case ADDR_WINDOWS: |
| 2306 | ea.line2 = LAST_WIN_NR; |
| 2307 | break; |
| 2308 | case ADDR_TABS: |
| 2309 | ea.line2 = LAST_TAB_NR; |
| 2310 | break; |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 2311 | case ADDR_TABS_RELATIVE: |
| 2312 | ea.line2 = 1; |
| 2313 | break; |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 2314 | case ADDR_ARGUMENTS: |
| 2315 | if (ARGCOUNT == 0) |
| 2316 | ea.line1 = ea.line2 = 0; |
| 2317 | else |
| 2318 | ea.line2 = ARGCOUNT; |
| 2319 | break; |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 2320 | case ADDR_QUICKFIX_VALID: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 2321 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 2322 | ea.line2 = qf_get_valid_size(&ea); |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 2323 | if (ea.line2 == 0) |
| 2324 | ea.line2 = 1; |
Bram Moolenaar | e906c50 | 2015-09-09 21:10:39 +0200 | [diff] [blame] | 2325 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 2326 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2327 | case ADDR_NONE: |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 2328 | case ADDR_UNSIGNED: |
| 2329 | case ADDR_QUICKFIX: |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2330 | iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX")); |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2331 | break; |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 2332 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2335 | // accept numbered register only when no count allowed (:put) |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2336 | if ( (ea.argt & EX_REGSTR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | && *ea.arg != NUL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2338 | // Do not allow register = for user commands |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2339 | && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=') |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2340 | && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2341 | { |
Bram Moolenaar | 85de206 | 2011-05-05 14:26:41 +0200 | [diff] [blame] | 2342 | #ifndef FEAT_CLIPBOARD |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2343 | // check these explicitly for a more specific error message |
Bram Moolenaar | 85de206 | 2011-05-05 14:26:41 +0200 | [diff] [blame] | 2344 | if (*ea.arg == '*' || *ea.arg == '+') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2345 | { |
Bram Moolenaar | 99b1272 | 2019-01-14 20:16:40 +0100 | [diff] [blame] | 2346 | errormsg = _(e_invalidreg); |
Bram Moolenaar | 85de206 | 2011-05-05 14:26:41 +0200 | [diff] [blame] | 2347 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2348 | } |
| 2349 | #endif |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2350 | if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put |
| 2351 | && !IS_USER_CMDIDX(ea.cmdidx)))) |
Bram Moolenaar | 85de206 | 2011-05-05 14:26:41 +0200 | [diff] [blame] | 2352 | { |
| 2353 | ea.regname = *ea.arg++; |
| 2354 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2355 | // for '=' register: accept the rest of the line as an expression |
Bram Moolenaar | 85de206 | 2011-05-05 14:26:41 +0200 | [diff] [blame] | 2356 | if (ea.arg[-1] == '=' && ea.arg[0] != NUL) |
| 2357 | { |
| 2358 | set_expr_line(vim_strsave(ea.arg)); |
| 2359 | ea.arg += STRLEN(ea.arg); |
| 2360 | } |
| 2361 | #endif |
| 2362 | ea.arg = skipwhite(ea.arg); |
| 2363 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | /* |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2367 | * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2368 | * count, it's a buffer name. |
| 2369 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2370 | if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg) |
| 2371 | && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2372 | || VIM_ISWHITE(*p))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2373 | { |
| 2374 | n = getdigits(&ea.arg); |
| 2375 | ea.arg = skipwhite(ea.arg); |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2376 | if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2377 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2378 | errormsg = _(e_zerocount); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | goto doend; |
| 2380 | } |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2381 | if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2382 | { |
| 2383 | ea.line2 = n; |
| 2384 | if (ea.addr_count == 0) |
| 2385 | ea.addr_count = 1; |
| 2386 | } |
| 2387 | else |
| 2388 | { |
| 2389 | ea.line1 = ea.line2; |
| 2390 | ea.line2 += n - 1; |
| 2391 | ++ea.addr_count; |
| 2392 | /* |
| 2393 | * Be vi compatible: no error message for out of range. |
| 2394 | */ |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 2395 | if (ea.line2 > curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 2397 | } |
| 2398 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2399 | |
| 2400 | /* |
| 2401 | * Check for flags: 'l', 'p' and '#'. |
| 2402 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2403 | if (ea.argt & EX_FLAGS) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2404 | get_flags(&ea); |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2405 | if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL |
| 2406 | && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2407 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2408 | // no arguments allowed but there is something |
Bram Moolenaar | 2d06bfd | 2020-07-23 17:16:18 +0200 | [diff] [blame] | 2409 | errormsg = ex_errmsg(e_trailing_arg, ea.arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2410 | goto doend; |
| 2411 | } |
| 2412 | |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2413 | if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2414 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2415 | errormsg = _(e_argreq); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2416 | goto doend; |
| 2417 | } |
| 2418 | |
| 2419 | #ifdef FEAT_EVAL |
| 2420 | /* |
| 2421 | * Skip the command when it's not going to be executed. |
| 2422 | * The commands like :if, :endif, etc. always need to be executed. |
| 2423 | * Also make an exception for commands that handle a trailing command |
| 2424 | * themselves. |
| 2425 | */ |
| 2426 | if (ea.skip) |
| 2427 | { |
| 2428 | switch (ea.cmdidx) |
| 2429 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2430 | // commands that need evaluation |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | case CMD_while: |
| 2432 | case CMD_endwhile: |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 2433 | case CMD_for: |
| 2434 | case CMD_endfor: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2435 | case CMD_if: |
| 2436 | case CMD_elseif: |
| 2437 | case CMD_else: |
| 2438 | case CMD_endif: |
| 2439 | case CMD_try: |
| 2440 | case CMD_catch: |
| 2441 | case CMD_finally: |
| 2442 | case CMD_endtry: |
| 2443 | case CMD_function: |
Bram Moolenaar | ab55c68 | 2020-03-01 19:41:43 +0100 | [diff] [blame] | 2444 | case CMD_def: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | break; |
| 2446 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2447 | // Commands that handle '|' themselves. Check: A command should |
| 2448 | // either have the EX_TRLBAR flag, appear in this list or appear in |
| 2449 | // the list at ":help :bar". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | case CMD_aboveleft: |
| 2451 | case CMD_and: |
| 2452 | case CMD_belowright: |
| 2453 | case CMD_botright: |
| 2454 | case CMD_browse: |
| 2455 | case CMD_call: |
| 2456 | case CMD_confirm: |
Bram Moolenaar | 8f76e6b | 2019-11-26 16:50:30 +0100 | [diff] [blame] | 2457 | case CMD_const: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2458 | case CMD_delfunction: |
| 2459 | case CMD_djump: |
| 2460 | case CMD_dlist: |
| 2461 | case CMD_dsearch: |
| 2462 | case CMD_dsplit: |
| 2463 | case CMD_echo: |
| 2464 | case CMD_echoerr: |
| 2465 | case CMD_echomsg: |
| 2466 | case CMD_echon: |
Bram Moolenaar | a76b315 | 2020-02-16 16:20:21 +0100 | [diff] [blame] | 2467 | case CMD_eval: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | case CMD_execute: |
Bram Moolenaar | 7b668e8 | 2016-08-23 23:51:21 +0200 | [diff] [blame] | 2469 | case CMD_filter: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | case CMD_help: |
| 2471 | case CMD_hide: |
| 2472 | case CMD_ijump: |
| 2473 | case CMD_ilist: |
| 2474 | case CMD_isearch: |
| 2475 | case CMD_isplit: |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2476 | case CMD_keepalt: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | case CMD_keepjumps: |
| 2478 | case CMD_keepmarks: |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 2479 | case CMD_keeppatterns: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2480 | case CMD_leftabove: |
| 2481 | case CMD_let: |
| 2482 | case CMD_lockmarks: |
Bram Moolenaar | cc4423a | 2019-11-26 17:05:00 +0100 | [diff] [blame] | 2483 | case CMD_lockvar: |
Bram Moolenaar | 0ba0429 | 2010-07-14 23:23:17 +0200 | [diff] [blame] | 2484 | case CMD_lua: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2485 | case CMD_match: |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2486 | case CMD_mzscheme: |
Bram Moolenaar | 5803ae6 | 2014-03-23 16:04:02 +0100 | [diff] [blame] | 2487 | case CMD_noautocmd: |
| 2488 | case CMD_noswapfile: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2489 | case CMD_perl: |
| 2490 | case CMD_psearch: |
| 2491 | case CMD_python: |
Bram Moolenaar | 368373e | 2010-07-19 20:46:22 +0200 | [diff] [blame] | 2492 | case CMD_py3: |
Bram Moolenaar | b659052 | 2010-07-21 16:00:43 +0200 | [diff] [blame] | 2493 | case CMD_python3: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2494 | case CMD_return: |
| 2495 | case CMD_rightbelow: |
| 2496 | case CMD_ruby: |
| 2497 | case CMD_silent: |
| 2498 | case CMD_smagic: |
| 2499 | case CMD_snomagic: |
| 2500 | case CMD_substitute: |
| 2501 | case CMD_syntax: |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 2502 | case CMD_tab: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | case CMD_tcl: |
| 2504 | case CMD_throw: |
| 2505 | case CMD_tilde: |
| 2506 | case CMD_topleft: |
| 2507 | case CMD_unlet: |
Bram Moolenaar | cc4423a | 2019-11-26 17:05:00 +0100 | [diff] [blame] | 2508 | case CMD_unlockvar: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2509 | case CMD_verbose: |
| 2510 | case CMD_vertical: |
Bram Moolenaar | 12bde49 | 2011-06-13 01:19:56 +0200 | [diff] [blame] | 2511 | case CMD_wincmd: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2512 | break; |
| 2513 | |
| 2514 | default: goto doend; |
| 2515 | } |
| 2516 | } |
| 2517 | #endif |
| 2518 | |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2519 | if (ea.argt & EX_XFILE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | { |
| 2521 | if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL) |
| 2522 | goto doend; |
| 2523 | } |
| 2524 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2525 | /* |
| 2526 | * Accept buffer name. Cannot be used at the same time with a buffer |
| 2527 | * number. Don't do this for a user command. |
| 2528 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2529 | if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0 |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2530 | && !IS_USER_CMDIDX(ea.cmdidx)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2531 | { |
| 2532 | /* |
| 2533 | * :bdelete, :bwipeout and :bunload take several arguments, separated |
| 2534 | * by spaces: find next space (skipping over escaped characters). |
| 2535 | * The others take one argument: ignore trailing spaces. |
| 2536 | */ |
| 2537 | if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout |
| 2538 | || ea.cmdidx == CMD_bunload) |
| 2539 | p = skiptowhite_esc(ea.arg); |
| 2540 | else |
| 2541 | { |
| 2542 | p = ea.arg + STRLEN(ea.arg); |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 2543 | while (p > ea.arg && VIM_ISWHITE(p[-1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2544 | --p; |
| 2545 | } |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 2546 | ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0, |
Bram Moolenaar | 0c279bb | 2013-03-19 14:25:54 +0100 | [diff] [blame] | 2547 | FALSE, FALSE); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2548 | if (ea.line2 < 0) // failed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2549 | goto doend; |
| 2550 | ea.addr_count = 1; |
| 2551 | ea.arg = skipwhite(p); |
| 2552 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2553 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2554 | // The :try command saves the emsg_silent flag, reset it here when |
| 2555 | // ":silent! try" was used, it should only apply to :try itself. |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2556 | if (ea.cmdidx == CMD_try && ea.did_esilent > 0) |
Bram Moolenaar | 2be5733 | 2018-02-13 18:05:18 +0100 | [diff] [blame] | 2557 | { |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2558 | emsg_silent -= ea.did_esilent; |
Bram Moolenaar | 2be5733 | 2018-02-13 18:05:18 +0100 | [diff] [blame] | 2559 | if (emsg_silent < 0) |
| 2560 | emsg_silent = 0; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2561 | ea.did_esilent = 0; |
Bram Moolenaar | 2be5733 | 2018-02-13 18:05:18 +0100 | [diff] [blame] | 2562 | } |
| 2563 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2564 | /* |
Bram Moolenaar | 2be5733 | 2018-02-13 18:05:18 +0100 | [diff] [blame] | 2565 | * 7. Execute the command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2566 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2567 | |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2568 | if (IS_USER_CMDIDX(ea.cmdidx)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2569 | { |
| 2570 | /* |
| 2571 | * Execute a user-defined command. |
| 2572 | */ |
| 2573 | do_ucmd(&ea); |
| 2574 | } |
| 2575 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | { |
| 2577 | /* |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 2578 | * Call the function to execute the builtin command. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2579 | */ |
| 2580 | ea.errmsg = NULL; |
| 2581 | (cmdnames[ea.cmdidx].cmd_func)(&ea); |
| 2582 | if (ea.errmsg != NULL) |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 2583 | errormsg = ea.errmsg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | #ifdef FEAT_EVAL |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2587 | // Set flag that any command was executed, used by ex_vim9script(). |
Bram Moolenaar | 1653155 | 2020-02-08 16:00:46 +0100 | [diff] [blame] | 2588 | if (getline_equal(ea.getline, ea.cookie, getsourceline) |
| 2589 | && current_sctx.sc_sid > 0) |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2590 | SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2591 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2592 | /* |
| 2593 | * If the command just executed called do_cmdline(), any throw or ":return" |
| 2594 | * or ":finish" encountered there must also check the cstack of the still |
| 2595 | * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught |
| 2596 | * exception, or reanimate a returned function or finished script file and |
| 2597 | * return or finish it again. |
| 2598 | */ |
| 2599 | if (need_rethrow) |
| 2600 | do_throw(cstack); |
| 2601 | else if (check_cstack) |
| 2602 | { |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2603 | if (source_finished(fgetline, cookie)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2604 | do_finish(&ea, TRUE); |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 2605 | else if (getline_equal(fgetline, cookie, get_func_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2606 | && current_func_returned()) |
| 2607 | do_return(&ea, TRUE, FALSE, NULL); |
| 2608 | } |
| 2609 | need_rethrow = check_cstack = FALSE; |
| 2610 | #endif |
| 2611 | |
| 2612 | doend: |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2613 | if (curwin->w_cursor.lnum == 0) // can happen with zero line number |
Bram Moolenaar | 6de5e12 | 2017-04-20 21:55:44 +0200 | [diff] [blame] | 2614 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | curwin->w_cursor.lnum = 1; |
Bram Moolenaar | 6de5e12 | 2017-04-20 21:55:44 +0200 | [diff] [blame] | 2616 | curwin->w_cursor.col = 0; |
| 2617 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2618 | |
| 2619 | if (errormsg != NULL && *errormsg != NUL && !did_emsg) |
| 2620 | { |
| 2621 | if (sourcing) |
| 2622 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2623 | if (errormsg != (char *)IObuff) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2624 | { |
| 2625 | STRCPY(IObuff, errormsg); |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2626 | errormsg = (char *)IObuff; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2627 | } |
Bram Moolenaar | a6f4d61 | 2011-09-21 19:10:46 +0200 | [diff] [blame] | 2628 | append_command(*cmdlinep); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2629 | } |
| 2630 | emsg(errormsg); |
| 2631 | } |
| 2632 | #ifdef FEAT_EVAL |
| 2633 | do_errthrow(cstack, |
Bram Moolenaar | 958636c | 2014-10-21 20:01:58 +0200 | [diff] [blame] | 2634 | (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx)) |
| 2635 | ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2636 | #endif |
| 2637 | |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 2638 | undo_cmdmod(&ea, save_msg_scroll); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2639 | cmdmod = save_cmdmod; |
Bram Moolenaar | 9a2c091 | 2019-03-30 14:26:18 +0100 | [diff] [blame] | 2640 | reg_executing = save_reg_executing; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2641 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 2642 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2643 | if (ea.did_sandbox) |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 2644 | --sandbox; |
| 2645 | #endif |
| 2646 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2647 | if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2648 | ea.nextcmd = NULL; |
| 2649 | |
| 2650 | #ifdef FEAT_EVAL |
| 2651 | --ex_nesting_level; |
Bram Moolenaar | b171fb1 | 2020-06-24 20:34:03 +0200 | [diff] [blame] | 2652 | vim_free(ea.cmdline_tofree); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | #endif |
| 2654 | |
| 2655 | return ea.nextcmd; |
| 2656 | } |
| 2657 | #if (_MSC_VER == 1200) |
Bram Moolenaar | 281bdce | 2005-01-25 21:53:18 +0000 | [diff] [blame] | 2658 | #pragma optimize( "", on ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2659 | #endif |
| 2660 | |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 2661 | static char ex_error_buf[MSG_BUF_LEN]; |
| 2662 | |
| 2663 | /* |
| 2664 | * Return an error message with argument included. |
| 2665 | * Uses a static buffer, only the last error will be kept. |
| 2666 | * "msg" will be translated, caller should use N_(). |
| 2667 | */ |
| 2668 | char * |
| 2669 | ex_errmsg(char *msg, char_u *arg) |
| 2670 | { |
| 2671 | vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg); |
| 2672 | return ex_error_buf; |
| 2673 | } |
| 2674 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2675 | /* |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2676 | * Parse and skip over command modifiers: |
| 2677 | * - update eap->cmd |
| 2678 | * - store flags in "cmdmod". |
| 2679 | * - Set ex_pressedreturn for an empty command line. |
| 2680 | * - set msg_silent for ":silent" |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2681 | * - set 'eventignore' to "all" for ":noautocmd" |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2682 | * - set p_verbose for ":verbose" |
| 2683 | * - Increment "sandbox" for ":sandbox" |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2684 | * When "skip_only" is TRUE the global variables are not changed, except for |
| 2685 | * "cmdmod". |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2686 | * Return FAIL when the command is not to be executed. |
| 2687 | * May set "errormsg" to an error message. |
| 2688 | */ |
| 2689 | int |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2690 | parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only) |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2691 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2692 | char_u *p; |
| 2693 | int starts_with_colon = FALSE; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2694 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 2695 | CLEAR_FIELD(cmdmod); |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2696 | eap->verbose_save = -1; |
| 2697 | eap->save_msg_silent = -1; |
| 2698 | |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2699 | // Repeat until no more command modifiers are found. |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2700 | for (;;) |
| 2701 | { |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2702 | while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':') |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2703 | { |
| 2704 | if (*eap->cmd == ':') |
| 2705 | starts_with_colon = TRUE; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2706 | ++eap->cmd; |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2707 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2708 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2709 | // in ex mode, an empty line works like :+ |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2710 | if (*eap->cmd == NUL && exmode_active |
| 2711 | && (getline_equal(eap->getline, eap->cookie, getexmodeline) |
| 2712 | || getline_equal(eap->getline, eap->cookie, getexline)) |
| 2713 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
| 2714 | { |
| 2715 | eap->cmd = (char_u *)"+"; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2716 | if (!skip_only) |
| 2717 | ex_pressedreturn = TRUE; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2718 | } |
| 2719 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2720 | // ignore comment and empty lines |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 2721 | if (comment_start(eap->cmd, starts_with_colon)) |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2722 | return FAIL; |
| 2723 | if (*eap->cmd == NUL) |
| 2724 | { |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2725 | if (!skip_only) |
| 2726 | ex_pressedreturn = TRUE; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2727 | return FAIL; |
| 2728 | } |
| 2729 | |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2730 | p = skip_range(eap->cmd, NULL); |
| 2731 | switch (*p) |
| 2732 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2733 | // When adding an entry, also modify cmd_exists(). |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2734 | case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3)) |
| 2735 | break; |
| 2736 | cmdmod.split |= WSP_ABOVE; |
| 2737 | continue; |
| 2738 | |
| 2739 | case 'b': if (checkforcmd(&eap->cmd, "belowright", 3)) |
| 2740 | { |
| 2741 | cmdmod.split |= WSP_BELOW; |
| 2742 | continue; |
| 2743 | } |
| 2744 | if (checkforcmd(&eap->cmd, "browse", 3)) |
| 2745 | { |
| 2746 | #ifdef FEAT_BROWSE_CMD |
| 2747 | cmdmod.browse = TRUE; |
| 2748 | #endif |
| 2749 | continue; |
| 2750 | } |
| 2751 | if (!checkforcmd(&eap->cmd, "botright", 2)) |
| 2752 | break; |
| 2753 | cmdmod.split |= WSP_BOT; |
| 2754 | continue; |
| 2755 | |
| 2756 | case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4)) |
| 2757 | break; |
| 2758 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 2759 | cmdmod.confirm = TRUE; |
| 2760 | #endif |
| 2761 | continue; |
| 2762 | |
| 2763 | case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3)) |
| 2764 | { |
| 2765 | cmdmod.keepmarks = TRUE; |
| 2766 | continue; |
| 2767 | } |
| 2768 | if (checkforcmd(&eap->cmd, "keepalt", 5)) |
| 2769 | { |
| 2770 | cmdmod.keepalt = TRUE; |
| 2771 | continue; |
| 2772 | } |
| 2773 | if (checkforcmd(&eap->cmd, "keeppatterns", 5)) |
| 2774 | { |
| 2775 | cmdmod.keeppatterns = TRUE; |
| 2776 | continue; |
| 2777 | } |
| 2778 | if (!checkforcmd(&eap->cmd, "keepjumps", 5)) |
| 2779 | break; |
| 2780 | cmdmod.keepjumps = TRUE; |
| 2781 | continue; |
| 2782 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2783 | case 'f': // only accept ":filter {pat} cmd" |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2784 | { |
| 2785 | char_u *reg_pat; |
| 2786 | |
| 2787 | if (!checkforcmd(&p, "filter", 4) |
| 2788 | || *p == NUL || ends_excmd(*p)) |
| 2789 | break; |
| 2790 | if (*p == '!') |
| 2791 | { |
| 2792 | cmdmod.filter_force = TRUE; |
| 2793 | p = skipwhite(p + 1); |
| 2794 | if (*p == NUL || ends_excmd(*p)) |
| 2795 | break; |
| 2796 | } |
Bram Moolenaar | 1e624c9 | 2020-07-11 14:08:04 +0200 | [diff] [blame] | 2797 | #ifdef FEAT_EVAL |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 2798 | // Avoid that "filter(arg)" is recognized. |
| 2799 | if (in_vim9script() && !VIM_ISWHITE(*p)) |
| 2800 | break; |
Bram Moolenaar | 1e624c9 | 2020-07-11 14:08:04 +0200 | [diff] [blame] | 2801 | #endif |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2802 | if (skip_only) |
| 2803 | p = skip_vimgrep_pat(p, NULL, NULL); |
| 2804 | else |
| 2805 | // NOTE: This puts a NUL after the pattern. |
| 2806 | p = skip_vimgrep_pat(p, ®_pat, NULL); |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2807 | if (p == NULL || *p == NUL) |
| 2808 | break; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2809 | if (!skip_only) |
| 2810 | { |
| 2811 | cmdmod.filter_regmatch.regprog = |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2812 | vim_regcomp(reg_pat, RE_MAGIC); |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2813 | if (cmdmod.filter_regmatch.regprog == NULL) |
| 2814 | break; |
| 2815 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2816 | eap->cmd = p; |
| 2817 | continue; |
| 2818 | } |
| 2819 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2820 | // ":hide" and ":hide | cmd" are not modifiers |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2821 | case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3) |
| 2822 | || *p == NUL || ends_excmd(*p)) |
| 2823 | break; |
| 2824 | eap->cmd = p; |
| 2825 | cmdmod.hide = TRUE; |
| 2826 | continue; |
| 2827 | |
| 2828 | case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3)) |
| 2829 | { |
| 2830 | cmdmod.lockmarks = TRUE; |
| 2831 | continue; |
| 2832 | } |
| 2833 | |
| 2834 | if (!checkforcmd(&eap->cmd, "leftabove", 5)) |
| 2835 | break; |
| 2836 | cmdmod.split |= WSP_ABOVE; |
| 2837 | continue; |
| 2838 | |
| 2839 | case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3)) |
| 2840 | { |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2841 | if (cmdmod.save_ei == NULL && !skip_only) |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2842 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2843 | // Set 'eventignore' to "all". Restore the |
| 2844 | // existing option value later. |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2845 | cmdmod.save_ei = vim_strsave(p_ei); |
| 2846 | set_string_option_direct((char_u *)"ei", -1, |
| 2847 | (char_u *)"all", OPT_FREE, SID_NONE); |
| 2848 | } |
| 2849 | continue; |
| 2850 | } |
| 2851 | if (!checkforcmd(&eap->cmd, "noswapfile", 3)) |
| 2852 | break; |
| 2853 | cmdmod.noswapfile = TRUE; |
| 2854 | continue; |
| 2855 | |
| 2856 | case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6)) |
| 2857 | break; |
| 2858 | cmdmod.split |= WSP_BELOW; |
| 2859 | continue; |
| 2860 | |
| 2861 | case 's': if (checkforcmd(&eap->cmd, "sandbox", 3)) |
| 2862 | { |
| 2863 | #ifdef HAVE_SANDBOX |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2864 | if (!skip_only) |
| 2865 | { |
| 2866 | if (!eap->did_sandbox) |
| 2867 | ++sandbox; |
| 2868 | eap->did_sandbox = TRUE; |
| 2869 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2870 | #endif |
| 2871 | continue; |
| 2872 | } |
| 2873 | if (!checkforcmd(&eap->cmd, "silent", 3)) |
| 2874 | break; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2875 | if (!skip_only) |
| 2876 | { |
| 2877 | if (eap->save_msg_silent == -1) |
| 2878 | eap->save_msg_silent = msg_silent; |
| 2879 | ++msg_silent; |
| 2880 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2881 | if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1])) |
| 2882 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2883 | // ":silent!", but not "silent !cmd" |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2884 | eap->cmd = skipwhite(eap->cmd + 1); |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2885 | if (!skip_only) |
| 2886 | { |
| 2887 | ++emsg_silent; |
| 2888 | ++eap->did_esilent; |
| 2889 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2890 | } |
| 2891 | continue; |
| 2892 | |
| 2893 | case 't': if (checkforcmd(&p, "tab", 3)) |
| 2894 | { |
Bram Moolenaar | 548e598 | 2018-12-26 21:45:00 +0100 | [diff] [blame] | 2895 | if (!skip_only) |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2896 | { |
Bram Moolenaar | 548e598 | 2018-12-26 21:45:00 +0100 | [diff] [blame] | 2897 | long tabnr = get_address(eap, &eap->cmd, |
| 2898 | ADDR_TABS, eap->skip, |
| 2899 | skip_only, FALSE, 1); |
| 2900 | if (tabnr == MAXLNUM) |
| 2901 | cmdmod.tab = tabpage_index(curtab) + 1; |
| 2902 | else |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2903 | { |
Bram Moolenaar | 548e598 | 2018-12-26 21:45:00 +0100 | [diff] [blame] | 2904 | if (tabnr < 0 || tabnr > LAST_TAB_NR) |
| 2905 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 2906 | *errormsg = _(e_invrange); |
Bram Moolenaar | 548e598 | 2018-12-26 21:45:00 +0100 | [diff] [blame] | 2907 | return FAIL; |
| 2908 | } |
| 2909 | cmdmod.tab = tabnr + 1; |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2910 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2911 | } |
| 2912 | eap->cmd = p; |
| 2913 | continue; |
| 2914 | } |
| 2915 | if (!checkforcmd(&eap->cmd, "topleft", 2)) |
| 2916 | break; |
| 2917 | cmdmod.split |= WSP_TOP; |
| 2918 | continue; |
| 2919 | |
| 2920 | case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3)) |
| 2921 | break; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2922 | if (!skip_only) |
| 2923 | { |
| 2924 | if (eap->save_msg_silent == -1) |
| 2925 | eap->save_msg_silent = msg_silent; |
| 2926 | msg_silent = 0; |
| 2927 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2928 | continue; |
| 2929 | |
| 2930 | case 'v': if (checkforcmd(&eap->cmd, "vertical", 4)) |
| 2931 | { |
| 2932 | cmdmod.split |= WSP_VERT; |
| 2933 | continue; |
| 2934 | } |
| 2935 | if (!checkforcmd(&p, "verbose", 4)) |
| 2936 | break; |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2937 | if (!skip_only) |
| 2938 | { |
| 2939 | if (eap->verbose_save < 0) |
| 2940 | eap->verbose_save = p_verbose; |
| 2941 | if (vim_isdigit(*eap->cmd)) |
| 2942 | p_verbose = atoi((char *)eap->cmd); |
| 2943 | else |
| 2944 | p_verbose = 1; |
| 2945 | } |
Bram Moolenaar | effed93 | 2018-08-14 13:38:17 +0200 | [diff] [blame] | 2946 | eap->cmd = p; |
| 2947 | continue; |
| 2948 | } |
| 2949 | break; |
| 2950 | } |
| 2951 | |
| 2952 | return OK; |
| 2953 | } |
| 2954 | |
| 2955 | /* |
Bram Moolenaar | b074e8b | 2020-07-11 13:40:45 +0200 | [diff] [blame] | 2956 | * Undo and free contents of "cmdmod". |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2957 | */ |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 2958 | void |
| 2959 | undo_cmdmod(exarg_T *eap, int save_msg_scroll) |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2960 | { |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 2961 | if (eap->verbose_save >= 0) |
| 2962 | p_verbose = eap->verbose_save; |
| 2963 | |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2964 | if (cmdmod.save_ei != NULL) |
| 2965 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 2966 | // Restore 'eventignore' to the value before ":noautocmd". |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2967 | set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, |
| 2968 | OPT_FREE, SID_NONE); |
| 2969 | free_string_option(cmdmod.save_ei); |
| 2970 | } |
| 2971 | |
| 2972 | if (cmdmod.filter_regmatch.regprog != NULL) |
| 2973 | vim_regfree(cmdmod.filter_regmatch.regprog); |
Bram Moolenaar | 47e7d70 | 2020-07-05 18:18:42 +0200 | [diff] [blame] | 2974 | |
| 2975 | if (eap->save_msg_silent != -1) |
| 2976 | { |
| 2977 | // messages could be enabled for a serious error, need to check if the |
| 2978 | // counters don't become negative |
| 2979 | if (!did_emsg || msg_silent > eap->save_msg_silent) |
| 2980 | msg_silent = eap->save_msg_silent; |
| 2981 | emsg_silent -= eap->did_esilent; |
| 2982 | if (emsg_silent < 0) |
| 2983 | emsg_silent = 0; |
| 2984 | // Restore msg_scroll, it's set by file I/O commands, even when no |
| 2985 | // message is actually displayed. |
| 2986 | msg_scroll = save_msg_scroll; |
| 2987 | |
| 2988 | // "silent reg" or "silent echo x" inside "redir" leaves msg_col |
| 2989 | // somewhere in the line. Put it back in the first column. |
| 2990 | if (redirecting()) |
| 2991 | msg_col = 0; |
| 2992 | } |
Bram Moolenaar | 33c4dbb | 2018-08-14 16:06:16 +0200 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /* |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 2996 | * Parse the address range, if any, in "eap". |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 2997 | * May set the last search pattern, unless "silent" is TRUE. |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 2998 | * Return FAIL and set "errormsg" or return OK. |
| 2999 | */ |
| 3000 | int |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3001 | parse_cmd_address(exarg_T *eap, char **errormsg, int silent) |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3002 | { |
| 3003 | int address_count = 1; |
| 3004 | linenr_T lnum; |
| 3005 | |
| 3006 | // Repeat for all ',' or ';' separated addresses. |
| 3007 | for (;;) |
| 3008 | { |
| 3009 | eap->line1 = eap->line2; |
| 3010 | switch (eap->addr_type) |
| 3011 | { |
| 3012 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3013 | case ADDR_OTHER: |
Bram Moolenaar | b5383b1 | 2020-05-18 19:46:48 +0200 | [diff] [blame] | 3014 | // Default is the cursor line number. Avoid using an invalid |
| 3015 | // line number though. |
| 3016 | if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) |
| 3017 | eap->line2 = curbuf->b_ml.ml_line_count; |
| 3018 | else |
| 3019 | eap->line2 = curwin->w_cursor.lnum; |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3020 | break; |
| 3021 | case ADDR_WINDOWS: |
| 3022 | eap->line2 = CURRENT_WIN_NR; |
| 3023 | break; |
| 3024 | case ADDR_ARGUMENTS: |
| 3025 | eap->line2 = curwin->w_arg_idx + 1; |
| 3026 | if (eap->line2 > ARGCOUNT) |
| 3027 | eap->line2 = ARGCOUNT; |
| 3028 | break; |
| 3029 | case ADDR_LOADED_BUFFERS: |
| 3030 | case ADDR_BUFFERS: |
| 3031 | eap->line2 = curbuf->b_fnum; |
| 3032 | break; |
| 3033 | case ADDR_TABS: |
| 3034 | eap->line2 = CURRENT_TAB_NR; |
| 3035 | break; |
| 3036 | case ADDR_TABS_RELATIVE: |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3037 | case ADDR_UNSIGNED: |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3038 | eap->line2 = 1; |
| 3039 | break; |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3040 | case ADDR_QUICKFIX: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3041 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3042 | eap->line2 = qf_get_cur_idx(eap); |
| 3043 | #endif |
| 3044 | break; |
| 3045 | case ADDR_QUICKFIX_VALID: |
| 3046 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3047 | eap->line2 = qf_get_cur_valid_idx(eap); |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3048 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3049 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3050 | case ADDR_NONE: |
| 3051 | // Will give an error later if a range is found. |
| 3052 | break; |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3053 | } |
| 3054 | eap->cmd = skipwhite(eap->cmd); |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 3055 | lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent, |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3056 | eap->addr_count == 0, address_count++); |
| 3057 | if (eap->cmd == NULL) // error detected |
| 3058 | return FAIL; |
| 3059 | if (lnum == MAXLNUM) |
| 3060 | { |
| 3061 | if (*eap->cmd == '%') // '%' - all lines |
| 3062 | { |
| 3063 | ++eap->cmd; |
| 3064 | switch (eap->addr_type) |
| 3065 | { |
| 3066 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3067 | case ADDR_OTHER: |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3068 | eap->line1 = 1; |
| 3069 | eap->line2 = curbuf->b_ml.ml_line_count; |
| 3070 | break; |
| 3071 | case ADDR_LOADED_BUFFERS: |
| 3072 | { |
| 3073 | buf_T *buf = firstbuf; |
| 3074 | |
| 3075 | while (buf->b_next != NULL |
| 3076 | && buf->b_ml.ml_mfp == NULL) |
| 3077 | buf = buf->b_next; |
| 3078 | eap->line1 = buf->b_fnum; |
| 3079 | buf = lastbuf; |
| 3080 | while (buf->b_prev != NULL |
| 3081 | && buf->b_ml.ml_mfp == NULL) |
| 3082 | buf = buf->b_prev; |
| 3083 | eap->line2 = buf->b_fnum; |
| 3084 | break; |
| 3085 | } |
| 3086 | case ADDR_BUFFERS: |
| 3087 | eap->line1 = firstbuf->b_fnum; |
| 3088 | eap->line2 = lastbuf->b_fnum; |
| 3089 | break; |
| 3090 | case ADDR_WINDOWS: |
| 3091 | case ADDR_TABS: |
| 3092 | if (IS_USER_CMDIDX(eap->cmdidx)) |
| 3093 | { |
| 3094 | eap->line1 = 1; |
| 3095 | eap->line2 = eap->addr_type == ADDR_WINDOWS |
| 3096 | ? LAST_WIN_NR : LAST_TAB_NR; |
| 3097 | } |
| 3098 | else |
| 3099 | { |
| 3100 | // there is no Vim command which uses '%' and |
| 3101 | // ADDR_WINDOWS or ADDR_TABS |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3102 | *errormsg = _(e_invrange); |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3103 | return FAIL; |
| 3104 | } |
| 3105 | break; |
| 3106 | case ADDR_TABS_RELATIVE: |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3107 | case ADDR_UNSIGNED: |
| 3108 | case ADDR_QUICKFIX: |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3109 | *errormsg = _(e_invrange); |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3110 | return FAIL; |
| 3111 | case ADDR_ARGUMENTS: |
| 3112 | if (ARGCOUNT == 0) |
| 3113 | eap->line1 = eap->line2 = 0; |
| 3114 | else |
| 3115 | { |
| 3116 | eap->line1 = 1; |
| 3117 | eap->line2 = ARGCOUNT; |
| 3118 | } |
| 3119 | break; |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3120 | case ADDR_QUICKFIX_VALID: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3121 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3122 | eap->line1 = 1; |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3123 | eap->line2 = qf_get_valid_size(eap); |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3124 | if (eap->line2 == 0) |
| 3125 | eap->line2 = 1; |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3126 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3127 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3128 | case ADDR_NONE: |
| 3129 | // Will give an error later if a range is found. |
| 3130 | break; |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3131 | } |
| 3132 | ++eap->addr_count; |
| 3133 | } |
| 3134 | else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL) |
| 3135 | { |
| 3136 | pos_T *fp; |
| 3137 | |
| 3138 | // '*' - visual area |
| 3139 | if (eap->addr_type != ADDR_LINES) |
| 3140 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3141 | *errormsg = _(e_invrange); |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3142 | return FAIL; |
| 3143 | } |
| 3144 | |
| 3145 | ++eap->cmd; |
| 3146 | if (!eap->skip) |
| 3147 | { |
| 3148 | fp = getmark('<', FALSE); |
| 3149 | if (check_mark(fp) == FAIL) |
| 3150 | return FAIL; |
| 3151 | eap->line1 = fp->lnum; |
| 3152 | fp = getmark('>', FALSE); |
| 3153 | if (check_mark(fp) == FAIL) |
| 3154 | return FAIL; |
| 3155 | eap->line2 = fp->lnum; |
| 3156 | ++eap->addr_count; |
| 3157 | } |
| 3158 | } |
| 3159 | } |
| 3160 | else |
| 3161 | eap->line2 = lnum; |
| 3162 | eap->addr_count++; |
| 3163 | |
| 3164 | if (*eap->cmd == ';') |
| 3165 | { |
| 3166 | if (!eap->skip) |
| 3167 | { |
| 3168 | curwin->w_cursor.lnum = eap->line2; |
Bram Moolenaar | 0e71704 | 2020-04-27 19:29:01 +0200 | [diff] [blame] | 3169 | // Don't leave the cursor on an illegal line or column, but do |
| 3170 | // accept zero as address, so 0;/PATTERN/ works correctly. |
| 3171 | if (eap->line2 > 0) |
| 3172 | check_cursor(); |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3173 | } |
| 3174 | } |
| 3175 | else if (*eap->cmd != ',') |
| 3176 | break; |
| 3177 | ++eap->cmd; |
| 3178 | } |
| 3179 | |
| 3180 | // One address given: set start and end lines. |
| 3181 | if (eap->addr_count == 1) |
| 3182 | { |
| 3183 | eap->line1 = eap->line2; |
| 3184 | // ... but only implicit: really no address given |
| 3185 | if (lnum == MAXLNUM) |
| 3186 | eap->addr_count = 0; |
| 3187 | } |
| 3188 | return OK; |
| 3189 | } |
| 3190 | |
| 3191 | /* |
Bram Moolenaar | c5a1e80 | 2005-01-11 21:21:40 +0000 | [diff] [blame] | 3192 | * Check for an Ex command with optional tail. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3193 | * If there is a match advance "pp" to the argument and return TRUE. |
| 3194 | */ |
Bram Moolenaar | c5a1e80 | 2005-01-11 21:21:40 +0000 | [diff] [blame] | 3195 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3196 | checkforcmd( |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3197 | char_u **pp, // start of command |
| 3198 | char *cmd, // name of command |
| 3199 | int len) // required length |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3200 | { |
| 3201 | int i; |
| 3202 | |
| 3203 | for (i = 0; cmd[i] != NUL; ++i) |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 3204 | if (((char_u *)cmd)[i] != (*pp)[i]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3205 | break; |
| 3206 | if (i >= len && !isalpha((*pp)[i])) |
| 3207 | { |
| 3208 | *pp = skipwhite(*pp + i); |
| 3209 | return TRUE; |
| 3210 | } |
| 3211 | return FALSE; |
| 3212 | } |
| 3213 | |
| 3214 | /* |
Bram Moolenaar | a6f4d61 | 2011-09-21 19:10:46 +0200 | [diff] [blame] | 3215 | * Append "cmd" to the error message in IObuff. |
| 3216 | * Takes care of limiting the length and handling 0xa0, which would be |
| 3217 | * invisible otherwise. |
| 3218 | */ |
| 3219 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3220 | append_command(char_u *cmd) |
Bram Moolenaar | a6f4d61 | 2011-09-21 19:10:46 +0200 | [diff] [blame] | 3221 | { |
| 3222 | char_u *s = cmd; |
| 3223 | char_u *d; |
| 3224 | |
| 3225 | STRCAT(IObuff, ": "); |
| 3226 | d = IObuff + STRLEN(IObuff); |
| 3227 | while (*s != NUL && d - IObuff < IOSIZE - 7) |
| 3228 | { |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 3229 | if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0) |
Bram Moolenaar | a6f4d61 | 2011-09-21 19:10:46 +0200 | [diff] [blame] | 3230 | { |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 3231 | s += enc_utf8 ? 2 : 1; |
Bram Moolenaar | a6f4d61 | 2011-09-21 19:10:46 +0200 | [diff] [blame] | 3232 | STRCPY(d, "<a0>"); |
| 3233 | d += 4; |
| 3234 | } |
| 3235 | else |
| 3236 | MB_COPY_CHAR(s, d); |
| 3237 | } |
| 3238 | *d = NUL; |
| 3239 | } |
| 3240 | |
| 3241 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3242 | * Find an Ex command by its name, either built-in or user. |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3243 | * Start of the name can be found at eap->cmd. |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3244 | * Sets eap->cmdidx and returns a pointer to char after the command name. |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3245 | * "full" is set to TRUE if the whole command name matched. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3246 | * |
| 3247 | * If "lookup" is not NULL recognize expression without "eval" or "call" and |
| 3248 | * assignment without "let". Sets eap->cmdidx to the command while returning |
| 3249 | * "eap->cmd". |
| 3250 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3251 | * Returns NULL for an ambiguous user command. |
| 3252 | */ |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3253 | char_u * |
| 3254 | find_ex_command( |
| 3255 | exarg_T *eap, |
Bram Moolenaar | a494f56 | 2020-04-18 17:45:38 +0200 | [diff] [blame] | 3256 | int *full UNUSED, |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 3257 | void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED, |
Bram Moolenaar | a494f56 | 2020-04-18 17:45:38 +0200 | [diff] [blame] | 3258 | cctx_T *cctx UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3259 | { |
| 3260 | int len; |
| 3261 | char_u *p; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3262 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3263 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3264 | #ifdef FEAT_EVAL |
| 3265 | /* |
| 3266 | * Recognize a Vim9 script function/method call and assignment: |
| 3267 | * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()" |
| 3268 | */ |
Bram Moolenaar | 0c6ceaf | 2020-02-22 18:36:32 +0100 | [diff] [blame] | 3269 | p = eap->cmd; |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 3270 | if (lookup != NULL && (vim_strchr((char_u *)"{('[", *p) != NULL |
| 3271 | || ((p = to_name_const_end(eap->cmd)) > eap->cmd |
| 3272 | && *p != NUL))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3273 | { |
| 3274 | int oplen; |
| 3275 | int heredoc; |
| 3276 | |
Bram Moolenaar | 3d48e25 | 2020-07-15 14:15:52 +0200 | [diff] [blame] | 3277 | if ( |
| 3278 | // "(..." is an expression. |
| 3279 | // "funcname(" is always a function call. |
| 3280 | *p == '(' |
| 3281 | || (p == eap->cmd |
| 3282 | ? ( |
| 3283 | // "{..." is an dict expression. |
| 3284 | *eap->cmd == '{' |
| 3285 | // "'string'->func()" is an expression. |
| 3286 | || *eap->cmd == '\'' |
| 3287 | // "g:varname" is an expression. |
| 3288 | || eap->cmd[1] == ':' |
| 3289 | ) |
| 3290 | : ( |
| 3291 | // "varname[]" is an expression. |
| 3292 | *p == '[' |
| 3293 | // "varname->func()" is an expression. |
| 3294 | || (*p == '-' && p[1] == '>') |
| 3295 | // "varname.expr" is an expression. |
| 3296 | || (*p == '.' && ASCII_ISALPHA(p[1])) |
| 3297 | ))) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3298 | { |
| 3299 | eap->cmdidx = CMD_eval; |
| 3300 | return eap->cmd; |
| 3301 | } |
| 3302 | |
Bram Moolenaar | d2ef6b3 | 2020-07-02 21:11:34 +0200 | [diff] [blame] | 3303 | // "[...]->Method()" is a list expression, but "[a, b] = Func()" is |
| 3304 | // an assignment. |
| 3305 | // If there is no line break inside the "[...]" then "p" is advanced to |
| 3306 | // after the "]" by to_name_const_end(): check if a "=" follows. |
| 3307 | // If "[...]" has a line break "p" still points at the "[" and it can't |
| 3308 | // be an assignment. |
Bram Moolenaar | b31be3f | 2020-07-20 22:37:44 +0200 | [diff] [blame] | 3309 | if (*eap->cmd == '[') |
Bram Moolenaar | d2ef6b3 | 2020-07-02 21:11:34 +0200 | [diff] [blame] | 3310 | { |
Bram Moolenaar | b31be3f | 2020-07-20 22:37:44 +0200 | [diff] [blame] | 3311 | p = to_name_const_end(eap->cmd); |
| 3312 | if (p == eap->cmd || *skipwhite(p) != '=') |
| 3313 | { |
| 3314 | eap->cmdidx = CMD_eval; |
| 3315 | return eap->cmd; |
| 3316 | } |
| 3317 | if (p > eap->cmd && *skipwhite(p) == '=') |
| 3318 | { |
| 3319 | eap->cmdidx = CMD_let; |
| 3320 | return eap->cmd; |
| 3321 | } |
Bram Moolenaar | d2ef6b3 | 2020-07-02 21:11:34 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
Bram Moolenaar | 5f19593 | 2020-07-01 20:07:14 +0200 | [diff] [blame] | 3324 | // Recognize an assignment if we recognize the variable name: |
| 3325 | // "g:var = expr" |
| 3326 | // "var = expr" where "var" is a local var name. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3327 | oplen = assignment_len(skipwhite(p), &heredoc); |
| 3328 | if (oplen > 0) |
| 3329 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3330 | if (((p - eap->cmd) > 2 && eap->cmd[1] == ':') |
Bram Moolenaar | b84a381 | 2020-05-01 15:44:29 +0200 | [diff] [blame] | 3331 | || lookup(eap->cmd, p - eap->cmd, cctx) != NULL) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3332 | { |
| 3333 | eap->cmdidx = CMD_let; |
| 3334 | return eap->cmd; |
| 3335 | } |
| 3336 | } |
Bram Moolenaar | 43e969d | 2020-07-23 21:14:43 +0200 | [diff] [blame] | 3337 | |
| 3338 | // Recognize using a type for a w:, b:, t: or g: variable: |
| 3339 | // "w:varname: number = 123". |
| 3340 | if (eap->cmd[1] == ':' && *p == ':') |
| 3341 | { |
| 3342 | eap->cmdidx = CMD_eval; |
| 3343 | return eap->cmd; |
| 3344 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3345 | } |
| 3346 | #endif |
| 3347 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3348 | /* |
| 3349 | * Isolate the command and search for it in the command table. |
Bram Moolenaar | 8187089 | 2007-11-11 18:17:28 +0000 | [diff] [blame] | 3350 | * Exceptions: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3351 | * - the 'k' command can directly be followed by any character. |
| 3352 | * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 3353 | * but :sre[wind] is another command, as are :scr[iptnames], |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3354 | * :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3355 | * - the "d" command can directly be followed by 'l' or 'p' flag. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3356 | */ |
| 3357 | p = eap->cmd; |
| 3358 | if (*p == 'k') |
| 3359 | { |
| 3360 | eap->cmdidx = CMD_k; |
| 3361 | ++p; |
| 3362 | } |
| 3363 | else if (p[0] == 's' |
Bram Moolenaar | 204b93f | 2015-08-04 22:02:51 +0200 | [diff] [blame] | 3364 | && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r' |
| 3365 | && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p'))))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3366 | || p[1] == 'g' |
| 3367 | || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g') |
| 3368 | || p[1] == 'I' |
| 3369 | || (p[1] == 'r' && p[2] != 'e'))) |
| 3370 | { |
| 3371 | eap->cmdidx = CMD_substitute; |
| 3372 | ++p; |
| 3373 | } |
| 3374 | else |
| 3375 | { |
| 3376 | while (ASCII_ISALPHA(*p)) |
| 3377 | ++p; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3378 | // for python 3.x support ":py3", ":python3", ":py3file", etc. |
Bram Moolenaar | 55d5c03 | 2010-07-17 23:52:29 +0200 | [diff] [blame] | 3379 | if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y') |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3380 | { |
Bram Moolenaar | b659052 | 2010-07-21 16:00:43 +0200 | [diff] [blame] | 3381 | while (ASCII_ISALNUM(*p)) |
| 3382 | ++p; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3383 | } |
| 3384 | else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0) |
| 3385 | { |
| 3386 | // include "9" for "vim9script" |
| 3387 | ++p; |
| 3388 | while (ASCII_ISALPHA(*p)) |
| 3389 | ++p; |
| 3390 | } |
Bram Moolenaar | bd5e15f | 2010-07-17 21:19:38 +0200 | [diff] [blame] | 3391 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3392 | // check for non-alpha command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3393 | if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) |
| 3394 | ++p; |
| 3395 | len = (int)(p - eap->cmd); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3396 | if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p')) |
| 3397 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3398 | // Check for ":dl", ":dell", etc. to ":deletel": that's |
| 3399 | // :delete with the 'l' flag. Same for 'p'. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3400 | for (i = 0; i < len; ++i) |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 3401 | if (eap->cmd[i] != ((char_u *)"delete")[i]) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3402 | break; |
| 3403 | if (i == len - 1) |
| 3404 | { |
| 3405 | --len; |
| 3406 | if (p[-1] == 'l') |
| 3407 | eap->flags |= EXFLAG_LIST; |
| 3408 | else |
| 3409 | eap->flags |= EXFLAG_PRINT; |
| 3410 | } |
| 3411 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3412 | |
Bram Moolenaar | e5e0fbc | 2017-03-25 14:51:01 +0100 | [diff] [blame] | 3413 | if (ASCII_ISLOWER(eap->cmd[0])) |
| 3414 | { |
Bram Moolenaar | 6c0c1e8 | 2017-03-25 15:07:43 +0100 | [diff] [blame] | 3415 | int c1 = eap->cmd[0]; |
Bram Moolenaar | 94f82cb | 2019-07-24 22:30:27 +0200 | [diff] [blame] | 3416 | int c2 = len == 1 ? NUL : eap->cmd[1]; |
Bram Moolenaar | 6c0c1e8 | 2017-03-25 15:07:43 +0100 | [diff] [blame] | 3417 | |
Bram Moolenaar | e5e0fbc | 2017-03-25 14:51:01 +0100 | [diff] [blame] | 3418 | if (command_count != (int)CMD_SIZE) |
| 3419 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3420 | iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'")); |
Bram Moolenaar | e5e0fbc | 2017-03-25 14:51:01 +0100 | [diff] [blame] | 3421 | getout(1); |
| 3422 | } |
| 3423 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3424 | // Use a precomputed index for fast look-up in cmdnames[] |
| 3425 | // taking into account the first 2 letters of eap->cmd. |
Bram Moolenaar | e5e0fbc | 2017-03-25 14:51:01 +0100 | [diff] [blame] | 3426 | eap->cmdidx = cmdidxs1[CharOrdLow(c1)]; |
| 3427 | if (ASCII_ISLOWER(c2)) |
| 3428 | eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)]; |
| 3429 | } |
Bram Moolenaar | a494f56 | 2020-04-18 17:45:38 +0200 | [diff] [blame] | 3430 | else if (ASCII_ISUPPER(eap->cmd[0])) |
| 3431 | eap->cmdidx = CMD_Next; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | else |
Bram Moolenaar | e5e0fbc | 2017-03-25 14:51:01 +0100 | [diff] [blame] | 3433 | eap->cmdidx = CMD_bang; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3434 | |
| 3435 | for ( ; (int)eap->cmdidx < (int)CMD_SIZE; |
| 3436 | eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1)) |
| 3437 | if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd, |
| 3438 | (size_t)len) == 0) |
| 3439 | { |
| 3440 | #ifdef FEAT_EVAL |
| 3441 | if (full != NULL |
| 3442 | && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL) |
| 3443 | *full = TRUE; |
| 3444 | #endif |
| 3445 | break; |
| 3446 | } |
| 3447 | |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 3448 | // Look for a user defined command as a last resort. Let ":Print" be |
| 3449 | // overruled by a user defined command. |
Bram Moolenaar | a3e7b1f | 2010-11-10 19:00:01 +0100 | [diff] [blame] | 3450 | if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print) |
| 3451 | && *eap->cmd >= 'A' && *eap->cmd <= 'Z') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3452 | { |
Bram Moolenaar | ac9fb18 | 2019-04-27 13:04:13 +0200 | [diff] [blame] | 3453 | // User defined commands may contain digits. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3454 | while (ASCII_ISALNUM(*p)) |
| 3455 | ++p; |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3456 | p = find_ucmd(eap, p, full, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3457 | } |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3458 | if (p == eap->cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3459 | eap->cmdidx = CMD_SIZE; |
| 3460 | } |
| 3461 | |
| 3462 | return p; |
| 3463 | } |
| 3464 | |
| 3465 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3466 | static struct cmdmod |
| 3467 | { |
| 3468 | char *name; |
| 3469 | int minlen; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3470 | int has_count; // :123verbose :3tab |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3471 | } cmdmods[] = { |
| 3472 | {"aboveleft", 3, FALSE}, |
| 3473 | {"belowright", 3, FALSE}, |
| 3474 | {"botright", 2, FALSE}, |
| 3475 | {"browse", 3, FALSE}, |
| 3476 | {"confirm", 4, FALSE}, |
Bram Moolenaar | 7b668e8 | 2016-08-23 23:51:21 +0200 | [diff] [blame] | 3477 | {"filter", 4, FALSE}, |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3478 | {"hide", 3, FALSE}, |
| 3479 | {"keepalt", 5, FALSE}, |
| 3480 | {"keepjumps", 5, FALSE}, |
| 3481 | {"keepmarks", 3, FALSE}, |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 3482 | {"keeppatterns", 5, FALSE}, |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3483 | {"leftabove", 5, FALSE}, |
| 3484 | {"lockmarks", 3, FALSE}, |
Bram Moolenaar | ca9f958 | 2008-09-18 10:44:28 +0000 | [diff] [blame] | 3485 | {"noautocmd", 3, FALSE}, |
Bram Moolenaar | 5803ae6 | 2014-03-23 16:04:02 +0100 | [diff] [blame] | 3486 | {"noswapfile", 3, FALSE}, |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3487 | {"rightbelow", 6, FALSE}, |
| 3488 | {"sandbox", 3, FALSE}, |
| 3489 | {"silent", 3, FALSE}, |
| 3490 | {"tab", 3, TRUE}, |
| 3491 | {"topleft", 2, FALSE}, |
Bram Moolenaar | 8e258a4 | 2009-07-09 13:55:43 +0000 | [diff] [blame] | 3492 | {"unsilent", 3, FALSE}, |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3493 | {"verbose", 4, TRUE}, |
| 3494 | {"vertical", 4, FALSE}, |
| 3495 | }; |
| 3496 | |
| 3497 | /* |
| 3498 | * Return length of a command modifier (including optional count). |
| 3499 | * Return zero when it's not a modifier. |
| 3500 | */ |
| 3501 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3502 | modifier_len(char_u *cmd) |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3503 | { |
| 3504 | int i, j; |
| 3505 | char_u *p = cmd; |
| 3506 | |
| 3507 | if (VIM_ISDIGIT(*cmd)) |
| 3508 | p = skipwhite(skipdigits(cmd)); |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 3509 | for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i) |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3510 | { |
| 3511 | for (j = 0; p[j] != NUL; ++j) |
| 3512 | if (p[j] != cmdmods[i].name[j]) |
| 3513 | break; |
Bram Moolenaar | 2d473ab | 2013-06-12 17:12:24 +0200 | [diff] [blame] | 3514 | if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3515 | && (p == cmd || cmdmods[i].has_count)) |
Bram Moolenaar | cb4cef2 | 2008-03-16 15:04:34 +0000 | [diff] [blame] | 3516 | return j + (int)(p - cmd); |
Bram Moolenaar | ed53fb9 | 2007-11-24 20:50:24 +0000 | [diff] [blame] | 3517 | } |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3521 | /* |
| 3522 | * Return > 0 if an Ex command "name" exists. |
| 3523 | * Return 2 if there is an exact match. |
| 3524 | * Return 3 if there is an ambiguous match. |
| 3525 | */ |
| 3526 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3527 | cmd_exists(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3528 | { |
| 3529 | exarg_T ea; |
| 3530 | int full = FALSE; |
| 3531 | int i; |
| 3532 | int j; |
Bram Moolenaar | f3a6788 | 2006-05-05 21:09:41 +0000 | [diff] [blame] | 3533 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3534 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3535 | // Check command modifiers. |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 3536 | for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3537 | { |
| 3538 | for (j = 0; name[j] != NUL; ++j) |
| 3539 | if (name[j] != cmdmods[i].name[j]) |
| 3540 | break; |
| 3541 | if (name[j] == NUL && j >= cmdmods[i].minlen) |
| 3542 | return (cmdmods[i].name[j] == NUL ? 2 : 1); |
| 3543 | } |
| 3544 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3545 | // Check built-in commands and user defined commands. |
| 3546 | // For ":2match" and ":3match" we need to skip the number. |
Bram Moolenaar | a958761 | 2006-05-04 21:47:50 +0000 | [diff] [blame] | 3547 | ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3548 | ea.cmdidx = (cmdidx_T)0; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 3549 | p = find_ex_command(&ea, &full, NULL, NULL); |
Bram Moolenaar | f3a6788 | 2006-05-05 21:09:41 +0000 | [diff] [blame] | 3550 | if (p == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3551 | return 3; |
Bram Moolenaar | a958761 | 2006-05-04 21:47:50 +0000 | [diff] [blame] | 3552 | if (vim_isdigit(*name) && ea.cmdidx != CMD_match) |
| 3553 | return 0; |
Bram Moolenaar | f3a6788 | 2006-05-05 21:09:41 +0000 | [diff] [blame] | 3554 | if (*skipwhite(p) != NUL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3555 | return 0; // trailing garbage |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3556 | return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1)); |
| 3557 | } |
| 3558 | #endif |
| 3559 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 3560 | cmdidx_T |
| 3561 | excmd_get_cmdidx(char_u *cmd, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3562 | { |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 3563 | cmdidx_T idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3564 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 3565 | for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE; |
| 3566 | idx = (cmdidx_T)((int)idx + 1)) |
| 3567 | if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3568 | break; |
| 3569 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 3570 | return idx; |
| 3571 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3572 | |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 3573 | long |
| 3574 | excmd_get_argt(cmdidx_T idx) |
| 3575 | { |
| 3576 | return (long)cmdnames[(int)idx].cmd_argt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | /* |
Bram Moolenaar | ee8415b | 2018-08-10 23:13:12 +0200 | [diff] [blame] | 3580 | * Skip a range specifier of the form: addr [,addr] [;addr] .. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3581 | * |
| 3582 | * Backslashed delimiters after / or ? will be skipped, and commands will |
| 3583 | * not be expanded between /'s and ?'s or after "'". |
| 3584 | * |
Bram Moolenaar | df069ee | 2020-06-22 23:02:51 +0200 | [diff] [blame] | 3585 | * Also skip white space and ":" characters after the range. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | * Returns the "cmd" pointer advanced to beyond the range. |
| 3587 | */ |
| 3588 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3589 | skip_range( |
| 3590 | char_u *cmd, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3591 | int *ctx) // pointer to xp_context or NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3592 | { |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 3593 | unsigned delim; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3594 | |
Bram Moolenaar | cbf20fb | 2017-02-03 21:19:04 +0100 | [diff] [blame] | 3595 | while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3596 | { |
Bram Moolenaar | cbf20fb | 2017-02-03 21:19:04 +0100 | [diff] [blame] | 3597 | if (*cmd == '\\') |
| 3598 | { |
| 3599 | if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&') |
| 3600 | ++cmd; |
| 3601 | else |
| 3602 | break; |
| 3603 | } |
| 3604 | else if (*cmd == '\'') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | { |
| 3606 | if (*++cmd == NUL && ctx != NULL) |
| 3607 | *ctx = EXPAND_NOTHING; |
| 3608 | } |
| 3609 | else if (*cmd == '/' || *cmd == '?') |
| 3610 | { |
| 3611 | delim = *cmd++; |
| 3612 | while (*cmd != NUL && *cmd != delim) |
| 3613 | if (*cmd++ == '\\' && *cmd != NUL) |
| 3614 | ++cmd; |
| 3615 | if (*cmd == NUL && ctx != NULL) |
| 3616 | *ctx = EXPAND_NOTHING; |
| 3617 | } |
| 3618 | if (*cmd != NUL) |
| 3619 | ++cmd; |
| 3620 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3621 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3622 | // Skip ":" and white space. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3623 | while (*cmd == ':') |
| 3624 | cmd = skipwhite(cmd + 1); |
| 3625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3626 | return cmd; |
| 3627 | } |
| 3628 | |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3629 | static void |
| 3630 | addr_error(cmd_addr_T addr_type) |
| 3631 | { |
| 3632 | if (addr_type == ADDR_NONE) |
| 3633 | emsg(_(e_norange)); |
| 3634 | else |
| 3635 | emsg(_(e_invrange)); |
| 3636 | } |
| 3637 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3638 | /* |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 3639 | * Get a single EX address. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3640 | * |
| 3641 | * Set ptr to the next character after the part that was interpreted. |
| 3642 | * Set ptr to NULL when an error is encountered. |
Bram Moolenaar | 198cb66 | 2018-09-06 21:44:17 +0200 | [diff] [blame] | 3643 | * This may set the last used search pattern. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | * |
| 3645 | * Return MAXLNUM when no Ex address was found. |
| 3646 | */ |
| 3647 | static linenr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3648 | get_address( |
| 3649 | exarg_T *eap UNUSED, |
| 3650 | char_u **ptr, |
Bram Moolenaar | 1b03a19 | 2019-12-08 17:08:29 +0100 | [diff] [blame] | 3651 | cmd_addr_T addr_type, |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 3652 | int skip, // only skip the address, don't use it |
| 3653 | int silent, // no errors or side effects |
| 3654 | int to_other_file, // flag: may jump to other file |
| 3655 | int address_count UNUSED) // 1 for first address, >1 after comma |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3656 | { |
| 3657 | int c; |
| 3658 | int i; |
| 3659 | long n; |
| 3660 | char_u *cmd; |
| 3661 | pos_T pos; |
| 3662 | pos_T *fp; |
| 3663 | linenr_T lnum; |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 3664 | buf_T *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3665 | |
| 3666 | cmd = skipwhite(*ptr); |
| 3667 | lnum = MAXLNUM; |
| 3668 | do |
| 3669 | { |
| 3670 | switch (*cmd) |
| 3671 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3672 | case '.': // '.' - Cursor position |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3673 | ++cmd; |
| 3674 | switch (addr_type) |
| 3675 | { |
| 3676 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3677 | case ADDR_OTHER: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3678 | lnum = curwin->w_cursor.lnum; |
| 3679 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3680 | case ADDR_WINDOWS: |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3681 | lnum = CURRENT_WIN_NR; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3682 | break; |
| 3683 | case ADDR_ARGUMENTS: |
| 3684 | lnum = curwin->w_arg_idx + 1; |
| 3685 | break; |
| 3686 | case ADDR_LOADED_BUFFERS: |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 3687 | case ADDR_BUFFERS: |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3688 | lnum = curbuf->b_fnum; |
| 3689 | break; |
| 3690 | case ADDR_TABS: |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3691 | lnum = CURRENT_TAB_NR; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3692 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3693 | case ADDR_NONE: |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3694 | case ADDR_TABS_RELATIVE: |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3695 | case ADDR_UNSIGNED: |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3696 | addr_error(addr_type); |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 3697 | cmd = NULL; |
| 3698 | goto error; |
| 3699 | break; |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 3700 | case ADDR_QUICKFIX: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3701 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3702 | lnum = qf_get_cur_idx(eap); |
| 3703 | #endif |
| 3704 | break; |
| 3705 | case ADDR_QUICKFIX_VALID: |
| 3706 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 3707 | lnum = qf_get_cur_valid_idx(eap); |
Bram Moolenaar | e906c50 | 2015-09-09 21:10:39 +0200 | [diff] [blame] | 3708 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3709 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3710 | } |
| 3711 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3712 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3713 | case '$': // '$' - last line |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3714 | ++cmd; |
| 3715 | switch (addr_type) |
| 3716 | { |
| 3717 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3718 | case ADDR_OTHER: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3719 | lnum = curbuf->b_ml.ml_line_count; |
| 3720 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3721 | case ADDR_WINDOWS: |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3722 | lnum = LAST_WIN_NR; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3723 | break; |
| 3724 | case ADDR_ARGUMENTS: |
| 3725 | lnum = ARGCOUNT; |
| 3726 | break; |
| 3727 | case ADDR_LOADED_BUFFERS: |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 3728 | buf = lastbuf; |
| 3729 | while (buf->b_ml.ml_mfp == NULL) |
| 3730 | { |
| 3731 | if (buf->b_prev == NULL) |
| 3732 | break; |
| 3733 | buf = buf->b_prev; |
| 3734 | } |
| 3735 | lnum = buf->b_fnum; |
| 3736 | break; |
| 3737 | case ADDR_BUFFERS: |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3738 | lnum = lastbuf->b_fnum; |
| 3739 | break; |
| 3740 | case ADDR_TABS: |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3741 | lnum = LAST_TAB_NR; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3742 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3743 | case ADDR_NONE: |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3744 | case ADDR_TABS_RELATIVE: |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3745 | case ADDR_UNSIGNED: |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3746 | addr_error(addr_type); |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 3747 | cmd = NULL; |
| 3748 | goto error; |
| 3749 | break; |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 3750 | case ADDR_QUICKFIX: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3751 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 3752 | lnum = qf_get_size(eap); |
| 3753 | if (lnum == 0) |
| 3754 | lnum = 1; |
Bram Moolenaar | e906c50 | 2015-09-09 21:10:39 +0200 | [diff] [blame] | 3755 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3756 | break; |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3757 | case ADDR_QUICKFIX_VALID: |
| 3758 | #ifdef FEAT_QUICKFIX |
| 3759 | lnum = qf_get_valid_size(eap); |
| 3760 | if (lnum == 0) |
| 3761 | lnum = 1; |
| 3762 | #endif |
| 3763 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3764 | } |
| 3765 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3766 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3767 | case '\'': // ''' - mark |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3768 | if (*++cmd == NUL) |
| 3769 | { |
| 3770 | cmd = NULL; |
| 3771 | goto error; |
| 3772 | } |
| 3773 | if (addr_type != ADDR_LINES) |
| 3774 | { |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3775 | addr_error(addr_type); |
Bram Moolenaar | c0a37b9 | 2015-02-03 19:10:53 +0100 | [diff] [blame] | 3776 | cmd = NULL; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3777 | goto error; |
| 3778 | } |
| 3779 | if (skip) |
| 3780 | ++cmd; |
| 3781 | else |
| 3782 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3783 | // Only accept a mark in another file when it is |
| 3784 | // used by itself: ":'M". |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3785 | fp = getmark(*cmd, to_other_file && cmd[1] == NUL); |
| 3786 | ++cmd; |
| 3787 | if (fp == (pos_T *)-1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3788 | // Jumped to another file. |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3789 | lnum = curwin->w_cursor.lnum; |
| 3790 | else |
| 3791 | { |
| 3792 | if (check_mark(fp) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3793 | { |
| 3794 | cmd = NULL; |
| 3795 | goto error; |
| 3796 | } |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3797 | lnum = fp->lnum; |
| 3798 | } |
| 3799 | } |
| 3800 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3801 | |
| 3802 | case '/': |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3803 | case '?': // '/' or '?' - search |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3804 | c = *cmd++; |
| 3805 | if (addr_type != ADDR_LINES) |
| 3806 | { |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3807 | addr_error(addr_type); |
Bram Moolenaar | c0a37b9 | 2015-02-03 19:10:53 +0100 | [diff] [blame] | 3808 | cmd = NULL; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3809 | goto error; |
| 3810 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3811 | if (skip) // skip "/pat/" |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3812 | { |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 3813 | cmd = skip_regexp(cmd, c, (int)p_magic); |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3814 | if (*cmd == c) |
| 3815 | ++cmd; |
| 3816 | } |
| 3817 | else |
| 3818 | { |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 3819 | int flags; |
| 3820 | |
| 3821 | pos = curwin->w_cursor; // save curwin->w_cursor |
| 3822 | |
| 3823 | // When '/' or '?' follows another address, start from |
| 3824 | // there. |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3825 | if (lnum != MAXLNUM) |
| 3826 | curwin->w_cursor.lnum = lnum; |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 3827 | |
| 3828 | // Start a forward search at the end of the line (unless |
| 3829 | // before the first line). |
| 3830 | // Start a backward search at the start of the line. |
| 3831 | // This makes sure we never match in the current |
| 3832 | // line, and can match anywhere in the |
| 3833 | // next/previous line. |
Bram Moolenaar | 8ada6aa | 2017-12-19 21:23:21 +0100 | [diff] [blame] | 3834 | if (c == '/' && curwin->w_cursor.lnum > 0) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3835 | curwin->w_cursor.col = MAXCOL; |
| 3836 | else |
| 3837 | curwin->w_cursor.col = 0; |
| 3838 | searchcmdlen = 0; |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 3839 | flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG; |
Bram Moolenaar | c036e87 | 2020-02-21 21:30:52 +0100 | [diff] [blame] | 3840 | if (!do_search(NULL, c, c, cmd, 1L, flags, NULL)) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3841 | { |
| 3842 | curwin->w_cursor = pos; |
| 3843 | cmd = NULL; |
| 3844 | goto error; |
| 3845 | } |
| 3846 | lnum = curwin->w_cursor.lnum; |
| 3847 | curwin->w_cursor = pos; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3848 | // adjust command string pointer |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3849 | cmd += searchcmdlen; |
| 3850 | } |
| 3851 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3852 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3853 | case '\\': // "\?", "\/" or "\&", repeat search |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3854 | ++cmd; |
| 3855 | if (addr_type != ADDR_LINES) |
| 3856 | { |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 3857 | addr_error(addr_type); |
Bram Moolenaar | c0a37b9 | 2015-02-03 19:10:53 +0100 | [diff] [blame] | 3858 | cmd = NULL; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3859 | goto error; |
| 3860 | } |
| 3861 | if (*cmd == '&') |
| 3862 | i = RE_SUBST; |
| 3863 | else if (*cmd == '?' || *cmd == '/') |
| 3864 | i = RE_SEARCH; |
| 3865 | else |
| 3866 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3867 | emsg(_(e_backslash)); |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3868 | cmd = NULL; |
| 3869 | goto error; |
| 3870 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3871 | |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3872 | if (!skip) |
| 3873 | { |
| 3874 | /* |
| 3875 | * When search follows another address, start from |
| 3876 | * there. |
| 3877 | */ |
| 3878 | if (lnum != MAXLNUM) |
| 3879 | pos.lnum = lnum; |
| 3880 | else |
| 3881 | pos.lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3882 | |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3883 | /* |
| 3884 | * Start the search just like for the above |
| 3885 | * do_search(). |
| 3886 | */ |
| 3887 | if (*cmd != '?') |
| 3888 | pos.col = MAXCOL; |
| 3889 | else |
| 3890 | pos.col = 0; |
Bram Moolenaar | bd8539a | 2015-08-11 18:53:03 +0200 | [diff] [blame] | 3891 | pos.coladd = 0; |
Bram Moolenaar | 5d24a22 | 2018-12-23 19:10:09 +0100 | [diff] [blame] | 3892 | if (searchit(curwin, curbuf, &pos, NULL, |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3893 | *cmd == '?' ? BACKWARD : FORWARD, |
Bram Moolenaar | 92ea26b | 2019-10-18 20:53:34 +0200 | [diff] [blame] | 3894 | (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3895 | lnum = pos.lnum; |
| 3896 | else |
| 3897 | { |
| 3898 | cmd = NULL; |
| 3899 | goto error; |
| 3900 | } |
| 3901 | } |
| 3902 | ++cmd; |
| 3903 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3904 | |
| 3905 | default: |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3906 | if (VIM_ISDIGIT(*cmd)) // absolute line number |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3907 | lnum = getdigits(&cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | for (;;) |
| 3911 | { |
| 3912 | cmd = skipwhite(cmd); |
| 3913 | if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd)) |
| 3914 | break; |
| 3915 | |
| 3916 | if (lnum == MAXLNUM) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3917 | { |
| 3918 | switch (addr_type) |
| 3919 | { |
| 3920 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3921 | case ADDR_OTHER: |
| 3922 | // "+1" is same as ".+1" |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3923 | lnum = curwin->w_cursor.lnum; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3924 | break; |
| 3925 | case ADDR_WINDOWS: |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3926 | lnum = CURRENT_WIN_NR; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3927 | break; |
| 3928 | case ADDR_ARGUMENTS: |
| 3929 | lnum = curwin->w_arg_idx + 1; |
| 3930 | break; |
| 3931 | case ADDR_LOADED_BUFFERS: |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 3932 | case ADDR_BUFFERS: |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3933 | lnum = curbuf->b_fnum; |
| 3934 | break; |
| 3935 | case ADDR_TABS: |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 3936 | lnum = CURRENT_TAB_NR; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3937 | break; |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 3938 | case ADDR_TABS_RELATIVE: |
| 3939 | lnum = 1; |
| 3940 | break; |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 3941 | case ADDR_QUICKFIX: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3942 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3943 | lnum = qf_get_cur_idx(eap); |
| 3944 | #endif |
| 3945 | break; |
| 3946 | case ADDR_QUICKFIX_VALID: |
| 3947 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 3948 | lnum = qf_get_cur_valid_idx(eap); |
Bram Moolenaar | e906c50 | 2015-09-09 21:10:39 +0200 | [diff] [blame] | 3949 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 3950 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3951 | case ADDR_NONE: |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 3952 | case ADDR_UNSIGNED: |
| 3953 | lnum = 0; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 3954 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3958 | if (VIM_ISDIGIT(*cmd)) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3959 | i = '+'; // "number" is same as "+number" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3960 | else |
| 3961 | i = *cmd++; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3962 | if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3963 | n = 1; |
| 3964 | else |
| 3965 | n = getdigits(&cmd); |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 3966 | |
| 3967 | if (addr_type == ADDR_TABS_RELATIVE) |
| 3968 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 3969 | emsg(_(e_invrange)); |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 3970 | cmd = NULL; |
| 3971 | goto error; |
| 3972 | } |
| 3973 | else if (addr_type == ADDR_LOADED_BUFFERS |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 3974 | || addr_type == ADDR_BUFFERS) |
Bram Moolenaar | c0a37b9 | 2015-02-03 19:10:53 +0100 | [diff] [blame] | 3975 | lnum = compute_buffer_local_count( |
| 3976 | addr_type, lnum, (i == '-') ? -1 * n : n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3977 | else |
Bram Moolenaar | ded2782 | 2017-01-02 14:27:34 +0100 | [diff] [blame] | 3978 | { |
| 3979 | #ifdef FEAT_FOLDING |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 3980 | // Relative line addressing, need to adjust for folded lines |
| 3981 | // now, but only do it after the first address. |
Bram Moolenaar | ded2782 | 2017-01-02 14:27:34 +0100 | [diff] [blame] | 3982 | if (addr_type == ADDR_LINES && (i == '-' || i == '+') |
| 3983 | && address_count >= 2) |
| 3984 | (void)hasFolding(lnum, NULL, &lnum); |
| 3985 | #endif |
| 3986 | if (i == '-') |
| 3987 | lnum -= n; |
| 3988 | else |
| 3989 | lnum += n; |
| 3990 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3991 | } |
| 3992 | } while (*cmd == '/' || *cmd == '?'); |
| 3993 | |
| 3994 | error: |
| 3995 | *ptr = cmd; |
| 3996 | return lnum; |
| 3997 | } |
| 3998 | |
| 3999 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4000 | * Get flags from an Ex command argument. |
| 4001 | */ |
| 4002 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4003 | get_flags(exarg_T *eap) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4004 | { |
| 4005 | while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL) |
| 4006 | { |
| 4007 | if (*eap->arg == 'l') |
| 4008 | eap->flags |= EXFLAG_LIST; |
| 4009 | else if (*eap->arg == 'p') |
| 4010 | eap->flags |= EXFLAG_PRINT; |
| 4011 | else |
| 4012 | eap->flags |= EXFLAG_NR; |
| 4013 | eap->arg = skipwhite(eap->arg + 1); |
| 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4018 | * Function called for command which is Not Implemented. NI! |
| 4019 | */ |
| 4020 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4021 | ex_ni(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4022 | { |
| 4023 | if (!eap->skip) |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 4024 | eap->errmsg = |
| 4025 | _("E319: Sorry, the command is not available in this version"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Bram Moolenaar | 7bb7555 | 2007-07-16 18:39:49 +0000 | [diff] [blame] | 4028 | #ifdef HAVE_EX_SCRIPT_NI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4029 | /* |
| 4030 | * Function called for script command which is Not Implemented. NI! |
| 4031 | * Skips over ":perl <<EOF" constructs. |
| 4032 | */ |
| 4033 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4034 | ex_script_ni(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4035 | { |
| 4036 | if (!eap->skip) |
| 4037 | ex_ni(eap); |
| 4038 | else |
| 4039 | vim_free(script_get(eap, eap->arg)); |
| 4040 | } |
| 4041 | #endif |
| 4042 | |
| 4043 | /* |
| 4044 | * Check range in Ex command for validity. |
| 4045 | * Return NULL when valid, error message when invalid. |
| 4046 | */ |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4047 | static char * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4048 | invalid_range(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4049 | { |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4050 | buf_T *buf; |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 4051 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4052 | if ( eap->line1 < 0 |
| 4053 | || eap->line2 < 0 |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4054 | || eap->line1 > eap->line2) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4055 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4056 | |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4057 | if (eap->argt & EX_RANGE) |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4058 | { |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 4059 | switch (eap->addr_type) |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4060 | { |
| 4061 | case ADDR_LINES: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 4062 | if (eap->line2 > curbuf->b_ml.ml_line_count |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4063 | #ifdef FEAT_DIFF |
| 4064 | + (eap->cmdidx == CMD_diffget) |
| 4065 | #endif |
| 4066 | ) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4067 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4068 | break; |
| 4069 | case ADDR_ARGUMENTS: |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4070 | // add 1 if ARGCOUNT is 0 |
Bram Moolenaar | c0a37b9 | 2015-02-03 19:10:53 +0100 | [diff] [blame] | 4071 | if (eap->line2 > ARGCOUNT + (!ARGCOUNT)) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4072 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4073 | break; |
| 4074 | case ADDR_BUFFERS: |
Bram Moolenaar | 00b0d6d | 2019-08-21 22:25:30 +0200 | [diff] [blame] | 4075 | // Only a boundary check, not whether the buffers actually |
| 4076 | // exist. |
| 4077 | if (eap->line1 < 1 || eap->line2 > get_highest_fnum()) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4078 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4079 | break; |
| 4080 | case ADDR_LOADED_BUFFERS: |
| 4081 | buf = firstbuf; |
| 4082 | while (buf->b_ml.ml_mfp == NULL) |
| 4083 | { |
| 4084 | if (buf->b_next == NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4085 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4086 | buf = buf->b_next; |
| 4087 | } |
| 4088 | if (eap->line1 < buf->b_fnum) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4089 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4090 | buf = lastbuf; |
| 4091 | while (buf->b_ml.ml_mfp == NULL) |
| 4092 | { |
| 4093 | if (buf->b_prev == NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4094 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4095 | buf = buf->b_prev; |
| 4096 | } |
| 4097 | if (eap->line2 > buf->b_fnum) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4098 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4099 | break; |
| 4100 | case ADDR_WINDOWS: |
Bram Moolenaar | 8be6388 | 2015-01-14 11:25:05 +0100 | [diff] [blame] | 4101 | if (eap->line2 > LAST_WIN_NR) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4102 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4103 | break; |
| 4104 | case ADDR_TABS: |
| 4105 | if (eap->line2 > LAST_TAB_NR) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4106 | return _(e_invrange); |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4107 | break; |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 4108 | case ADDR_TABS_RELATIVE: |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 4109 | case ADDR_OTHER: |
| 4110 | // Any range is OK. |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 4111 | break; |
Bram Moolenaar | aa23b37 | 2015-09-08 18:46:31 +0200 | [diff] [blame] | 4112 | case ADDR_QUICKFIX: |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 4113 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 4114 | // No error for value that is too big, will use the last entry. |
| 4115 | if (eap->line2 <= 0) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4116 | return _(e_invrange); |
Bram Moolenaar | e906c50 | 2015-09-09 21:10:39 +0200 | [diff] [blame] | 4117 | #endif |
Bram Moolenaar | 26f0cb1 | 2019-05-01 21:43:42 +0200 | [diff] [blame] | 4118 | break; |
Bram Moolenaar | 25190db | 2019-05-04 15:05:28 +0200 | [diff] [blame] | 4119 | case ADDR_QUICKFIX_VALID: |
| 4120 | #ifdef FEAT_QUICKFIX |
| 4121 | if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap)) |
| 4122 | || eap->line2 < 0) |
| 4123 | return _(e_invrange); |
| 4124 | #endif |
| 4125 | break; |
| 4126 | case ADDR_UNSIGNED: |
| 4127 | if (eap->line2 < 0) |
| 4128 | return _(e_invrange); |
| 4129 | break; |
Bram Moolenaar | b731689 | 2019-05-01 18:08:42 +0200 | [diff] [blame] | 4130 | case ADDR_NONE: |
| 4131 | // Will give an error elsewhere. |
| 4132 | break; |
Bram Moolenaar | 3ffc79a | 2015-01-07 15:57:17 +0100 | [diff] [blame] | 4133 | } |
| 4134 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4135 | return NULL; |
| 4136 | } |
| 4137 | |
| 4138 | /* |
| 4139 | * Correct the range for zero line number, if required. |
| 4140 | */ |
| 4141 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4142 | correct_range(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4143 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4144 | if (!(eap->argt & EX_ZEROR)) // zero in range not allowed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4145 | { |
| 4146 | if (eap->line1 == 0) |
| 4147 | eap->line1 = 1; |
| 4148 | if (eap->line2 == 0) |
| 4149 | eap->line2 = 1; |
| 4150 | } |
| 4151 | } |
| 4152 | |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4153 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4154 | /* |
| 4155 | * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the |
| 4156 | * pattern. Otherwise return eap->arg. |
| 4157 | */ |
| 4158 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4159 | skip_grep_pat(exarg_T *eap) |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4160 | { |
| 4161 | char_u *p = eap->arg; |
| 4162 | |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4163 | if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep |
| 4164 | || eap->cmdidx == CMD_vimgrepadd |
| 4165 | || eap->cmdidx == CMD_lvimgrepadd |
| 4166 | || grep_internal(eap->cmdidx))) |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4167 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4168 | p = skip_vimgrep_pat(p, NULL, NULL); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4169 | if (p == NULL) |
| 4170 | p = eap->arg; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4171 | } |
| 4172 | return p; |
| 4173 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4174 | |
| 4175 | /* |
| 4176 | * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option |
| 4177 | * in the command line, so that things like % get expanded. |
| 4178 | */ |
| 4179 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4180 | replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4181 | { |
| 4182 | char_u *new_cmdline; |
| 4183 | char_u *program; |
| 4184 | char_u *pos; |
| 4185 | char_u *ptr; |
| 4186 | int len; |
| 4187 | int i; |
| 4188 | |
| 4189 | /* |
| 4190 | * Don't do it when ":vimgrep" is used for ":grep". |
| 4191 | */ |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4192 | if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake |
| 4193 | || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep |
| 4194 | || eap->cmdidx == CMD_grepadd |
| 4195 | || eap->cmdidx == CMD_lgrepadd) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4196 | && !grep_internal(eap->cmdidx)) |
| 4197 | { |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4198 | if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep |
| 4199 | || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4200 | { |
| 4201 | if (*curbuf->b_p_gp == NUL) |
| 4202 | program = p_gp; |
| 4203 | else |
| 4204 | program = curbuf->b_p_gp; |
| 4205 | } |
| 4206 | else |
| 4207 | { |
| 4208 | if (*curbuf->b_p_mp == NUL) |
| 4209 | program = p_mp; |
| 4210 | else |
| 4211 | program = curbuf->b_p_mp; |
| 4212 | } |
| 4213 | |
| 4214 | p = skipwhite(p); |
| 4215 | |
| 4216 | if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) |
| 4217 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4218 | // replace $* by given arguments |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4219 | i = 1; |
| 4220 | while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL) |
| 4221 | ++i; |
| 4222 | len = (int)STRLEN(p); |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 4223 | new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4224 | if (new_cmdline == NULL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4225 | return NULL; // out of memory |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4226 | ptr = new_cmdline; |
| 4227 | while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) |
| 4228 | { |
| 4229 | i = (int)(pos - program); |
| 4230 | STRNCPY(ptr, program, i); |
| 4231 | STRCPY(ptr += i, p); |
| 4232 | ptr += len; |
| 4233 | program = pos + 2; |
| 4234 | } |
| 4235 | STRCPY(ptr, program); |
| 4236 | } |
| 4237 | else |
| 4238 | { |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 4239 | new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4240 | if (new_cmdline == NULL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4241 | return NULL; // out of memory |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4242 | STRCPY(new_cmdline, program); |
| 4243 | STRCAT(new_cmdline, " "); |
| 4244 | STRCAT(new_cmdline, p); |
| 4245 | } |
| 4246 | msg_make(p); |
| 4247 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4248 | // 'eap->cmd' is not set here, because it is not used at CMD_make |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4249 | vim_free(*cmdlinep); |
| 4250 | *cmdlinep = new_cmdline; |
| 4251 | p = new_cmdline; |
| 4252 | } |
| 4253 | return p; |
| 4254 | } |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4255 | #endif |
| 4256 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | /* |
| 4258 | * Expand file name in Ex command argument. |
Bram Moolenaar | 0abb427 | 2019-06-15 16:06:00 +0200 | [diff] [blame] | 4259 | * When an error is detected, "errormsgp" is set to a non-NULL pointer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4260 | * Return FAIL for failure, OK otherwise. |
| 4261 | */ |
| 4262 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4263 | expand_filename( |
| 4264 | exarg_T *eap, |
| 4265 | char_u **cmdlinep, |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 4266 | char **errormsgp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4267 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4268 | int has_wildcards; // need to expand wildcards |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4269 | char_u *repl; |
| 4270 | int srclen; |
| 4271 | char_u *p; |
| 4272 | int n; |
Bram Moolenaar | 63b9254 | 2007-03-27 14:57:09 +0000 | [diff] [blame] | 4273 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4274 | |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4275 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4276 | // Skip a regexp pattern for ":vimgrep[add] pat file..." |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4277 | p = skip_grep_pat(eap); |
| 4278 | #else |
| 4279 | p = eap->arg; |
| 4280 | #endif |
| 4281 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4282 | /* |
| 4283 | * Decide to expand wildcards *before* replacing '%', '#', etc. If |
| 4284 | * the file name contains a wildcard it should not cause expanding. |
| 4285 | * (it will be expanded anyway if there is a wildcard before replacing). |
| 4286 | */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4287 | has_wildcards = mch_has_wildcard(p); |
| 4288 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4289 | { |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4290 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4291 | // Skip over `=expr`, wildcards in it are not expanded. |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4292 | if (p[0] == '`' && p[1] == '=') |
| 4293 | { |
| 4294 | p += 2; |
| 4295 | (void)skip_expr(&p); |
| 4296 | if (*p == '`') |
| 4297 | ++p; |
| 4298 | continue; |
| 4299 | } |
| 4300 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4301 | /* |
| 4302 | * Quick check if this cannot be the start of a special string. |
| 4303 | * Also removes backslash before '%', '#' and '<'. |
| 4304 | */ |
| 4305 | if (vim_strchr((char_u *)"%#<", *p) == NULL) |
| 4306 | { |
| 4307 | ++p; |
| 4308 | continue; |
| 4309 | } |
| 4310 | |
| 4311 | /* |
| 4312 | * Try to find a match at this position. |
| 4313 | */ |
Bram Moolenaar | 63b9254 | 2007-03-27 14:57:09 +0000 | [diff] [blame] | 4314 | repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum), |
| 4315 | errormsgp, &escaped); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4316 | if (*errormsgp != NULL) // error detected |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4317 | return FAIL; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4318 | if (repl == NULL) // no match found |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | { |
| 4320 | p += srclen; |
| 4321 | continue; |
| 4322 | } |
| 4323 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4324 | // Wildcards won't be expanded below, the replacement is taken |
| 4325 | // literally. But do expand "~/file", "~user/file" and "$HOME/file". |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 4326 | if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL) |
| 4327 | { |
| 4328 | char_u *l = repl; |
| 4329 | |
| 4330 | repl = expand_env_save(repl); |
| 4331 | vim_free(l); |
| 4332 | } |
| 4333 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4334 | // Need to escape white space et al. with a backslash. |
| 4335 | // Don't do this for: |
| 4336 | // - replacement that already has been escaped: "##" |
| 4337 | // - shell commands (may have to use quotes instead). |
| 4338 | // - non-unix systems when there is a single argument (spaces don't |
| 4339 | // separate arguments then). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4340 | if (!eap->usefilter |
Bram Moolenaar | 63b9254 | 2007-03-27 14:57:09 +0000 | [diff] [blame] | 4341 | && !escaped |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4342 | && eap->cmdidx != CMD_bang |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4343 | && eap->cmdidx != CMD_grep |
| 4344 | && eap->cmdidx != CMD_grepadd |
Bram Moolenaar | bf15b8d | 2017-06-04 20:43:48 +0200 | [diff] [blame] | 4345 | && eap->cmdidx != CMD_hardcopy |
Bram Moolenaar | 67883b4 | 2017-07-27 22:57:00 +0200 | [diff] [blame] | 4346 | && eap->cmdidx != CMD_lgrep |
| 4347 | && eap->cmdidx != CMD_lgrepadd |
| 4348 | && eap->cmdidx != CMD_lmake |
| 4349 | && eap->cmdidx != CMD_make |
| 4350 | && eap->cmdidx != CMD_terminal |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4351 | #ifndef UNIX |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4352 | && !(eap->argt & EX_NOSPC) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4353 | #endif |
| 4354 | ) |
| 4355 | { |
| 4356 | char_u *l; |
| 4357 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4358 | // Don't escape a backslash here, because rem_backslash() doesn't |
| 4359 | // remove it later. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4360 | static char_u *nobslash = (char_u *)" \t\"|"; |
| 4361 | # define ESCAPE_CHARS nobslash |
| 4362 | #else |
| 4363 | # define ESCAPE_CHARS escape_chars |
| 4364 | #endif |
| 4365 | |
| 4366 | for (l = repl; *l; ++l) |
| 4367 | if (vim_strchr(ESCAPE_CHARS, *l) != NULL) |
| 4368 | { |
| 4369 | l = vim_strsave_escaped(repl, ESCAPE_CHARS); |
| 4370 | if (l != NULL) |
| 4371 | { |
| 4372 | vim_free(repl); |
| 4373 | repl = l; |
| 4374 | } |
| 4375 | break; |
| 4376 | } |
| 4377 | } |
| 4378 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4379 | // For a shell command a '!' must be escaped. |
Bram Moolenaar | 67883b4 | 2017-07-27 22:57:00 +0200 | [diff] [blame] | 4380 | if ((eap->usefilter || eap->cmdidx == CMD_bang |
| 4381 | || eap->cmdidx == CMD_terminal) |
Bram Moolenaar | 31b7d38 | 2014-04-01 18:54:48 +0200 | [diff] [blame] | 4382 | && vim_strpbrk(repl, (char_u *)"!") != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4383 | { |
| 4384 | char_u *l; |
| 4385 | |
Bram Moolenaar | 31b7d38 | 2014-04-01 18:54:48 +0200 | [diff] [blame] | 4386 | l = vim_strsave_escaped(repl, (char_u *)"!"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4387 | if (l != NULL) |
| 4388 | { |
| 4389 | vim_free(repl); |
| 4390 | repl = l; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | p = repl_cmdline(eap, p, srclen, repl, cmdlinep); |
| 4395 | vim_free(repl); |
| 4396 | if (p == NULL) |
| 4397 | return FAIL; |
| 4398 | } |
| 4399 | |
| 4400 | /* |
| 4401 | * One file argument: Expand wildcards. |
| 4402 | * Don't do this with ":r !command" or ":w !command". |
| 4403 | */ |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4404 | if ((eap->argt & EX_NOSPC) && !eap->usefilter) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4405 | { |
| 4406 | /* |
| 4407 | * May do this twice: |
| 4408 | * 1. Replace environment variables. |
| 4409 | * 2. Replace any other wildcards, remove backslashes. |
| 4410 | */ |
| 4411 | for (n = 1; n <= 2; ++n) |
| 4412 | { |
| 4413 | if (n == 2) |
| 4414 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4415 | /* |
| 4416 | * Halve the number of backslashes (this is Vi compatible). |
| 4417 | * For Unix and OS/2, when wildcards are expanded, this is |
| 4418 | * done by ExpandOne() below. |
| 4419 | */ |
Bram Moolenaar | e7fedb6 | 2015-12-31 19:07:19 +0100 | [diff] [blame] | 4420 | #if defined(UNIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4421 | if (!has_wildcards) |
| 4422 | #endif |
| 4423 | backslash_halve(eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | if (has_wildcards) |
| 4427 | { |
| 4428 | if (n == 1) |
| 4429 | { |
| 4430 | /* |
| 4431 | * First loop: May expand environment variables. This |
| 4432 | * can be done much faster with expand_env() than with |
| 4433 | * something else (e.g., calling a shell). |
| 4434 | * After expanding environment variables, check again |
| 4435 | * if there are still wildcards present. |
| 4436 | */ |
| 4437 | if (vim_strchr(eap->arg, '$') != NULL |
| 4438 | || vim_strchr(eap->arg, '~') != NULL) |
| 4439 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 4440 | expand_env_esc(eap->arg, NameBuff, MAXPATHL, |
Bram Moolenaar | 9f0545d | 2007-09-26 20:36:32 +0000 | [diff] [blame] | 4441 | TRUE, TRUE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4442 | has_wildcards = mch_has_wildcard(NameBuff); |
| 4443 | p = NameBuff; |
| 4444 | } |
| 4445 | else |
| 4446 | p = NULL; |
| 4447 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4448 | else // n == 2 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4449 | { |
| 4450 | expand_T xpc; |
Bram Moolenaar | b40c257 | 2019-10-19 21:01:05 +0200 | [diff] [blame] | 4451 | int options = WILD_LIST_NOTFOUND |
| 4452 | | WILD_NOERROR | WILD_ADD_SLASH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | |
| 4454 | ExpandInit(&xpc); |
| 4455 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4456 | if (p_wic) |
| 4457 | options += WILD_ICASE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4458 | p = ExpandOne(&xpc, eap->arg, NULL, |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4459 | options, WILD_EXPAND_FREE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | if (p == NULL) |
| 4461 | return FAIL; |
| 4462 | } |
| 4463 | if (p != NULL) |
| 4464 | { |
| 4465 | (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg), |
| 4466 | p, cmdlinep); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4467 | if (n == 2) // p came from ExpandOne() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4468 | vim_free(p); |
| 4469 | } |
| 4470 | } |
| 4471 | } |
| 4472 | } |
| 4473 | return OK; |
| 4474 | } |
| 4475 | |
| 4476 | /* |
| 4477 | * Replace part of the command line, keeping eap->cmd, eap->arg and |
| 4478 | * eap->nextcmd correct. |
| 4479 | * "src" points to the part that is to be replaced, of length "srclen". |
| 4480 | * "repl" is the replacement string. |
| 4481 | * Returns a pointer to the character after the replaced string. |
| 4482 | * Returns NULL for failure. |
| 4483 | */ |
| 4484 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4485 | repl_cmdline( |
| 4486 | exarg_T *eap, |
| 4487 | char_u *src, |
| 4488 | int srclen, |
| 4489 | char_u *repl, |
| 4490 | char_u **cmdlinep) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4491 | { |
| 4492 | int len; |
| 4493 | int i; |
| 4494 | char_u *new_cmdline; |
| 4495 | |
| 4496 | /* |
| 4497 | * The new command line is build in new_cmdline[]. |
| 4498 | * First allocate it. |
| 4499 | * Careful: a "+cmd" argument may have been NUL terminated. |
| 4500 | */ |
| 4501 | len = (int)STRLEN(repl); |
| 4502 | i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3; |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 4503 | if (eap->nextcmd != NULL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4504 | i += (int)STRLEN(eap->nextcmd);// add space for next command |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 4505 | if ((new_cmdline = alloc(i)) == NULL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4506 | return NULL; // out of memory! |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4507 | |
| 4508 | /* |
| 4509 | * Copy the stuff before the expanded part. |
| 4510 | * Copy the expanded stuff. |
| 4511 | * Copy what came after the expanded part. |
| 4512 | * Copy the next commands, if there are any. |
| 4513 | */ |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4514 | i = (int)(src - *cmdlinep); // length of part before match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4515 | mch_memmove(new_cmdline, *cmdlinep, (size_t)i); |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 4516 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4517 | mch_memmove(new_cmdline + i, repl, (size_t)len); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4518 | i += len; // remember the end of the string |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4519 | STRCPY(new_cmdline + i, src + srclen); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4520 | src = new_cmdline + i; // remember where to continue |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4521 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4522 | if (eap->nextcmd != NULL) // append next command |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4523 | { |
| 4524 | i = (int)STRLEN(new_cmdline) + 1; |
| 4525 | STRCPY(new_cmdline + i, eap->nextcmd); |
| 4526 | eap->nextcmd = new_cmdline + i; |
| 4527 | } |
| 4528 | eap->cmd = new_cmdline + (eap->cmd - *cmdlinep); |
| 4529 | eap->arg = new_cmdline + (eap->arg - *cmdlinep); |
| 4530 | if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command) |
| 4531 | eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep); |
| 4532 | vim_free(*cmdlinep); |
| 4533 | *cmdlinep = new_cmdline; |
| 4534 | |
| 4535 | return src; |
| 4536 | } |
| 4537 | |
| 4538 | /* |
| 4539 | * Check for '|' to separate commands and '"' to start comments. |
| 4540 | */ |
| 4541 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4542 | separate_nextcmd(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4543 | { |
| 4544 | char_u *p; |
| 4545 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 4546 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4547 | p = skip_grep_pat(eap); |
| 4548 | #else |
| 4549 | p = eap->arg; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 4550 | #endif |
| 4551 | |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4552 | for ( ; *p; MB_PTR_ADV(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4553 | { |
| 4554 | if (*p == Ctrl_V) |
| 4555 | { |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4556 | if (eap->argt & (EX_CTRLV | EX_XFILE)) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4557 | ++p; // skip CTRL-V and next char |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4558 | else |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4559 | // remove CTRL-V and skip next char |
Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 4560 | STRMOVE(p, p + 1); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4561 | if (*p == NUL) // stop at NUL after CTRL-V |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4562 | break; |
| 4563 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4564 | |
| 4565 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4566 | // Skip over `=expr` when wildcards are expanded. |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4567 | else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE)) |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4568 | { |
| 4569 | p += 2; |
| 4570 | (void)skip_expr(&p); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4571 | } |
| 4572 | #endif |
| 4573 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4574 | // Check for '"': start of comment or '|': next command |
| 4575 | // :@" and :*" do not start a comment! |
| 4576 | // :redir @" doesn't either. |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4577 | else if ((*p == '"' |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 4578 | #ifdef FEAT_EVAL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4579 | && !in_vim9script() |
| 4580 | #endif |
| 4581 | && !(eap->argt & EX_NOTRLCOM) |
| 4582 | && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star) |
| 4583 | || p != eap->arg) |
| 4584 | && (eap->cmdidx != CMD_redir |
| 4585 | || p != eap->arg + 1 || p[-1] != '@')) |
| 4586 | #ifdef FEAT_EVAL |
| 4587 | || (*p == '#' |
| 4588 | && in_vim9script() |
| 4589 | && p[1] != '{' |
| 4590 | && p > eap->cmd && VIM_ISWHITE(p[-1])) |
Bram Moolenaar | a26b970 | 2020-04-18 19:53:28 +0200 | [diff] [blame] | 4591 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4592 | || *p == '|' || *p == '\n') |
| 4593 | { |
| 4594 | /* |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4595 | * We remove the '\' before the '|', unless EX_CTRLV is used |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4596 | * AND 'b' is present in 'cpoptions'. |
| 4597 | */ |
| 4598 | if ((vim_strchr(p_cpo, CPO_BAR) == NULL |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 4599 | || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4600 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4601 | STRMOVE(p - 1, p); // remove the '\' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4602 | --p; |
| 4603 | } |
| 4604 | else |
| 4605 | { |
| 4606 | eap->nextcmd = check_nextcmd(p); |
| 4607 | *p = NUL; |
| 4608 | break; |
| 4609 | } |
| 4610 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4611 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 4612 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4613 | if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4614 | del_trailing_spaces(eap->arg); |
| 4615 | } |
| 4616 | |
| 4617 | /* |
| 4618 | * get + command from ex argument |
| 4619 | */ |
| 4620 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4621 | getargcmd(char_u **argp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4622 | { |
| 4623 | char_u *arg = *argp; |
| 4624 | char_u *command = NULL; |
| 4625 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4626 | if (*arg == '+') // +[command] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4627 | { |
| 4628 | ++arg; |
Bram Moolenaar | 3e45159 | 2014-04-02 14:22:05 +0200 | [diff] [blame] | 4629 | if (vim_isspace(*arg) || *arg == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4630 | command = dollar_command; |
| 4631 | else |
| 4632 | { |
| 4633 | command = arg; |
| 4634 | arg = skip_cmd_arg(command, TRUE); |
| 4635 | if (*arg != NUL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4636 | *arg++ = NUL; // terminate command with NUL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4639 | arg = skipwhite(arg); // skip over spaces |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | *argp = arg; |
| 4641 | } |
| 4642 | return command; |
| 4643 | } |
| 4644 | |
| 4645 | /* |
| 4646 | * Find end of "+command" argument. Skip over "\ " and "\\". |
| 4647 | */ |
Bram Moolenaar | d019039 | 2019-08-23 21:17:35 +0200 | [diff] [blame] | 4648 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4649 | skip_cmd_arg( |
| 4650 | char_u *p, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4651 | int rembs) // TRUE to halve the number of backslashes |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4652 | { |
| 4653 | while (*p && !vim_isspace(*p)) |
| 4654 | { |
| 4655 | if (*p == '\\' && p[1] != NUL) |
| 4656 | { |
| 4657 | if (rembs) |
Bram Moolenaar | a7241f5 | 2008-06-24 20:39:31 +0000 | [diff] [blame] | 4658 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4659 | else |
| 4660 | ++p; |
| 4661 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4662 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4663 | } |
| 4664 | return p; |
| 4665 | } |
| 4666 | |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 4667 | int |
| 4668 | get_bad_opt(char_u *p, exarg_T *eap) |
| 4669 | { |
| 4670 | if (STRICMP(p, "keep") == 0) |
| 4671 | eap->bad_char = BAD_KEEP; |
| 4672 | else if (STRICMP(p, "drop") == 0) |
| 4673 | eap->bad_char = BAD_DROP; |
| 4674 | else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL) |
| 4675 | eap->bad_char = *p; |
Bram Moolenaar | 7580849 | 2018-06-12 12:39:41 +0200 | [diff] [blame] | 4676 | else |
| 4677 | return FAIL; |
| 4678 | return OK; |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 4679 | } |
| 4680 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4681 | /* |
| 4682 | * Get "++opt=arg" argument. |
| 4683 | * Return FAIL or OK. |
| 4684 | */ |
| 4685 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4686 | getargopt(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4687 | { |
| 4688 | char_u *arg = eap->arg + 2; |
| 4689 | int *pp = NULL; |
Bram Moolenaar | 34b4daf | 2010-05-16 13:26:25 +0200 | [diff] [blame] | 4690 | int bad_char_idx; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4691 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4692 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4693 | // ":edit ++[no]bin[ary] file" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4694 | if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0) |
| 4695 | { |
| 4696 | if (*arg == 'n') |
| 4697 | { |
| 4698 | arg += 2; |
| 4699 | eap->force_bin = FORCE_NOBIN; |
| 4700 | } |
| 4701 | else |
| 4702 | eap->force_bin = FORCE_BIN; |
| 4703 | if (!checkforcmd(&arg, "binary", 3)) |
| 4704 | return FAIL; |
| 4705 | eap->arg = skipwhite(arg); |
| 4706 | return OK; |
| 4707 | } |
| 4708 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4709 | // ":read ++edit file" |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 4710 | if (STRNCMP(arg, "edit", 4) == 0) |
| 4711 | { |
| 4712 | eap->read_edit = TRUE; |
| 4713 | eap->arg = skipwhite(arg + 4); |
| 4714 | return OK; |
| 4715 | } |
| 4716 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4717 | if (STRNCMP(arg, "ff", 2) == 0) |
| 4718 | { |
| 4719 | arg += 2; |
| 4720 | pp = &eap->force_ff; |
| 4721 | } |
| 4722 | else if (STRNCMP(arg, "fileformat", 10) == 0) |
| 4723 | { |
| 4724 | arg += 10; |
| 4725 | pp = &eap->force_ff; |
| 4726 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4727 | else if (STRNCMP(arg, "enc", 3) == 0) |
| 4728 | { |
Bram Moolenaar | b38e9ab | 2011-12-14 14:49:45 +0100 | [diff] [blame] | 4729 | if (STRNCMP(arg, "encoding", 8) == 0) |
| 4730 | arg += 8; |
| 4731 | else |
| 4732 | arg += 3; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4733 | pp = &eap->force_enc; |
| 4734 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4735 | else if (STRNCMP(arg, "bad", 3) == 0) |
| 4736 | { |
| 4737 | arg += 3; |
Bram Moolenaar | 34b4daf | 2010-05-16 13:26:25 +0200 | [diff] [blame] | 4738 | pp = &bad_char_idx; |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4739 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4740 | |
| 4741 | if (pp == NULL || *arg != '=') |
| 4742 | return FAIL; |
| 4743 | |
| 4744 | ++arg; |
| 4745 | *pp = (int)(arg - eap->cmd); |
| 4746 | arg = skip_cmd_arg(arg, FALSE); |
| 4747 | eap->arg = skipwhite(arg); |
| 4748 | *arg = NUL; |
| 4749 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | if (pp == &eap->force_ff) |
| 4751 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4752 | if (check_ff_value(eap->cmd + eap->force_ff) == FAIL) |
| 4753 | return FAIL; |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 4754 | eap->force_ff = eap->cmd[eap->force_ff]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4755 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4756 | else if (pp == &eap->force_enc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4757 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4758 | // Make 'fileencoding' lower case. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4759 | for (p = eap->cmd + eap->force_enc; *p != NUL; ++p) |
| 4760 | *p = TOLOWER_ASC(*p); |
| 4761 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4762 | else |
| 4763 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4764 | // Check ++bad= argument. Must be a single-byte character, "keep" or |
| 4765 | // "drop". |
Bram Moolenaar | 333b80a | 2018-04-04 22:57:29 +0200 | [diff] [blame] | 4766 | if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL) |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4767 | return FAIL; |
| 4768 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4769 | |
| 4770 | return OK; |
| 4771 | } |
| 4772 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4773 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4774 | ex_autocmd(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4775 | { |
| 4776 | /* |
Bram Moolenaar | 26d4b89 | 2018-07-04 22:26:28 +0200 | [diff] [blame] | 4777 | * Disallow autocommands from .exrc and .vimrc in current |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4778 | * directory for security reasons. |
| 4779 | */ |
| 4780 | if (secure) |
| 4781 | { |
| 4782 | secure = 2; |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 4783 | eap->errmsg = _(e_curdir); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4784 | } |
| 4785 | else if (eap->cmdidx == CMD_autocmd) |
| 4786 | do_autocmd(eap->arg, eap->forceit); |
| 4787 | else |
| 4788 | do_augroup(eap->arg, eap->forceit); |
| 4789 | } |
| 4790 | |
| 4791 | /* |
| 4792 | * ":doautocmd": Apply the automatic commands to the current buffer. |
| 4793 | */ |
| 4794 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4795 | ex_doautocmd(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4796 | { |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 4797 | char_u *arg = eap->arg; |
| 4798 | int call_do_modelines = check_nomodeline(&arg); |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 4799 | int did_aucmd; |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 4800 | |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 4801 | (void)do_doautocmd(arg, TRUE, &did_aucmd); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4802 | // Only when there is no <nomodeline>. |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 4803 | if (call_do_modelines && did_aucmd) |
Bram Moolenaar | 60542ac | 2012-02-12 20:14:01 +0100 | [diff] [blame] | 4804 | do_modelines(0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4806 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4807 | /* |
| 4808 | * :[N]bunload[!] [N] [bufname] unload buffer |
| 4809 | * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list |
| 4810 | * :[N]bwipeout[!] [N] [bufname] delete buffer really |
| 4811 | */ |
| 4812 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4813 | ex_bunload(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4814 | { |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 4815 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 4816 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4817 | eap->errmsg = do_bufdel( |
| 4818 | eap->cmdidx == CMD_bdelete ? DOBUF_DEL |
| 4819 | : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE |
| 4820 | : DOBUF_UNLOAD, eap->arg, |
| 4821 | eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit); |
| 4822 | } |
| 4823 | |
| 4824 | /* |
| 4825 | * :[N]buffer [N] to buffer N |
| 4826 | * :[N]sbuffer [N] to buffer N |
| 4827 | */ |
| 4828 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4829 | ex_buffer(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4830 | { |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 4831 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 4832 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4833 | if (*eap->arg) |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 4834 | eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4835 | else |
| 4836 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 4837 | if (eap->addr_count == 0) // default is current buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4838 | goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0); |
| 4839 | else |
| 4840 | goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2); |
Bram Moolenaar | 9c8d9e1 | 2014-09-19 20:07:26 +0200 | [diff] [blame] | 4841 | if (eap->do_ecmd_cmd != NULL) |
| 4842 | do_cmdline_cmd(eap->do_ecmd_cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4843 | } |
| 4844 | } |
| 4845 | |
| 4846 | /* |
| 4847 | * :[N]bmodified [N] to next mod. buffer |
| 4848 | * :[N]sbmodified [N] to next mod. buffer |
| 4849 | */ |
| 4850 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4851 | ex_bmodified(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4852 | { |
| 4853 | goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2); |
Bram Moolenaar | 9c8d9e1 | 2014-09-19 20:07:26 +0200 | [diff] [blame] | 4854 | if (eap->do_ecmd_cmd != NULL) |
| 4855 | do_cmdline_cmd(eap->do_ecmd_cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | /* |
| 4859 | * :[N]bnext [N] to next buffer |
| 4860 | * :[N]sbnext [N] split and to next buffer |
| 4861 | */ |
| 4862 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4863 | ex_bnext(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4864 | { |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 4865 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 3029bcc | 2020-01-18 15:06:19 +0100 | [diff] [blame] | 4866 | return; |
| 4867 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4868 | goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2); |
Bram Moolenaar | 9c8d9e1 | 2014-09-19 20:07:26 +0200 | [diff] [blame] | 4869 | if (eap->do_ecmd_cmd != NULL) |
| 4870 | do_cmdline_cmd(eap->do_ecmd_cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | /* |
| 4874 | * :[N]bNext [N] to previous buffer |
| 4875 | * :[N]bprevious [N] to previous buffer |
| 4876 | * :[N]sbNext [N] split and to previous buffer |
| 4877 | * :[N]sbprevious [N] split and to previous buffer |
| 4878 | */ |
| 4879 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4880 | ex_bprevious(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4881 | { |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 4882 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 3029bcc | 2020-01-18 15:06:19 +0100 | [diff] [blame] | 4883 | return; |
| 4884 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4885 | goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2); |
Bram Moolenaar | 9c8d9e1 | 2014-09-19 20:07:26 +0200 | [diff] [blame] | 4886 | if (eap->do_ecmd_cmd != NULL) |
| 4887 | do_cmdline_cmd(eap->do_ecmd_cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | /* |
| 4891 | * :brewind to first buffer |
| 4892 | * :bfirst to first buffer |
| 4893 | * :sbrewind split and to first buffer |
| 4894 | * :sbfirst split and to first buffer |
| 4895 | */ |
| 4896 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4897 | ex_brewind(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | { |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 4899 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 3029bcc | 2020-01-18 15:06:19 +0100 | [diff] [blame] | 4900 | return; |
| 4901 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4902 | goto_buffer(eap, DOBUF_FIRST, FORWARD, 0); |
Bram Moolenaar | 9c8d9e1 | 2014-09-19 20:07:26 +0200 | [diff] [blame] | 4903 | if (eap->do_ecmd_cmd != NULL) |
| 4904 | do_cmdline_cmd(eap->do_ecmd_cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | /* |
| 4908 | * :blast to last buffer |
| 4909 | * :sblast split and to last buffer |
| 4910 | */ |
| 4911 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4912 | ex_blast(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4913 | { |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 4914 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 3029bcc | 2020-01-18 15:06:19 +0100 | [diff] [blame] | 4915 | return; |
| 4916 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4917 | goto_buffer(eap, DOBUF_LAST, BACKWARD, 0); |
Bram Moolenaar | 9c8d9e1 | 2014-09-19 20:07:26 +0200 | [diff] [blame] | 4918 | if (eap->do_ecmd_cmd != NULL) |
| 4919 | do_cmdline_cmd(eap->do_ecmd_cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4920 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4921 | |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 4922 | /* |
| 4923 | * Check if "c" ends an Ex command. |
Bram Moolenaar | a494f56 | 2020-04-18 17:45:38 +0200 | [diff] [blame] | 4924 | * In Vim9 script does not check for white space before # or #{. |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 4925 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4927 | ends_excmd(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4928 | { |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4929 | int comment_char = '"'; |
| 4930 | |
Bram Moolenaar | fbda69b | 2020-04-13 15:06:53 +0200 | [diff] [blame] | 4931 | #ifdef FEAT_EVAL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4932 | if (in_vim9script()) |
| 4933 | comment_char = '#'; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 4934 | #endif |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4935 | return (c == NUL || c == '|' || c == comment_char || c == '\n'); |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 4936 | } |
| 4937 | |
| 4938 | /* |
| 4939 | * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal |
| 4940 | * to "cmd_start" or has a white space character before it. |
| 4941 | */ |
| 4942 | int |
Bram Moolenaar | 21cfe50 | 2020-04-16 23:01:50 +0200 | [diff] [blame] | 4943 | ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd) |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 4944 | { |
| 4945 | int c = *cmd; |
| 4946 | |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4947 | if (c == NUL || c == '|' || c == '\n') |
| 4948 | return TRUE; |
Bram Moolenaar | 7a09224 | 2020-04-16 22:10:49 +0200 | [diff] [blame] | 4949 | #ifdef FEAT_EVAL |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4950 | if (in_vim9script()) |
| 4951 | return c == '#' && cmd[1] != '{' |
| 4952 | && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])); |
Bram Moolenaar | fbda69b | 2020-04-13 15:06:53 +0200 | [diff] [blame] | 4953 | #endif |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 4954 | return c == '"'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
| 4957 | #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \ |
| 4958 | || defined(PROTO) |
| 4959 | /* |
| 4960 | * Return the next command, after the first '|' or '\n'. |
| 4961 | * Return NULL if not found. |
| 4962 | */ |
| 4963 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4964 | find_nextcmd(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4965 | { |
| 4966 | while (*p != '|' && *p != '\n') |
| 4967 | { |
| 4968 | if (*p == NUL) |
| 4969 | return NULL; |
| 4970 | ++p; |
| 4971 | } |
| 4972 | return (p + 1); |
| 4973 | } |
| 4974 | #endif |
| 4975 | |
| 4976 | /* |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 4977 | * Check if *p is a separator between Ex commands, skipping over white space. |
| 4978 | * Return NULL if it isn't, the following character if it is. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | */ |
| 4980 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4981 | check_nextcmd(char_u *p) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | { |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 4983 | char_u *s = skipwhite(p); |
| 4984 | |
| 4985 | if (*s == '|' || *s == '\n') |
| 4986 | return (s + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4987 | else |
| 4988 | return NULL; |
| 4989 | } |
| 4990 | |
| 4991 | /* |
| 4992 | * - if there are more files to edit |
| 4993 | * - and this is the last window |
| 4994 | * - and forceit not used |
| 4995 | * - and not repeated twice on a row |
| 4996 | * return FAIL and give error message if 'message' TRUE |
| 4997 | * return OK otherwise |
| 4998 | */ |
| 4999 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5000 | check_more( |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5001 | int message, // when FALSE check only, no messages |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5002 | int forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | { |
| 5004 | int n = ARGCOUNT - curwin->w_arg_idx - 1; |
| 5005 | |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5006 | if (!forceit && only_one_window() |
| 5007 | && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5008 | { |
| 5009 | if (message) |
| 5010 | { |
| 5011 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 5012 | if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL) |
| 5013 | { |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 5014 | char_u buff[DIALOG_MSG_SIZE]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5015 | |
Bram Moolenaar | da6e891 | 2018-08-21 15:12:14 +0200 | [diff] [blame] | 5016 | vim_snprintf((char *)buff, DIALOG_MSG_SIZE, |
| 5017 | NGETTEXT("%d more file to edit. Quit anyway?", |
| 5018 | "%d more files to edit. Quit anyway?", n), n); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES) |
| 5020 | return OK; |
| 5021 | return FAIL; |
| 5022 | } |
| 5023 | #endif |
Bram Moolenaar | b5443cc | 2019-01-15 20:19:40 +0100 | [diff] [blame] | 5024 | semsg(NGETTEXT("E173: %d more file to edit", |
| 5025 | "E173: %d more files to edit", n), n); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5026 | quitmore = 2; // next try to quit is allowed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5027 | } |
| 5028 | return FAIL; |
| 5029 | } |
| 5030 | return OK; |
| 5031 | } |
| 5032 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5033 | /* |
| 5034 | * Function given to ExpandGeneric() to obtain the list of command names. |
| 5035 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5036 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5037 | get_command_name(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5038 | { |
| 5039 | if (idx >= (int)CMD_SIZE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5040 | return get_user_command_name(idx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5041 | return cmdnames[idx].cmd_name; |
| 5042 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5043 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5045 | ex_colorscheme(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5046 | { |
Bram Moolenaar | e685079 | 2010-05-14 15:28:44 +0200 | [diff] [blame] | 5047 | if (*eap->arg == NUL) |
| 5048 | { |
| 5049 | #ifdef FEAT_EVAL |
| 5050 | char_u *expr = vim_strsave((char_u *)"g:colors_name"); |
| 5051 | char_u *p = NULL; |
| 5052 | |
| 5053 | if (expr != NULL) |
| 5054 | { |
| 5055 | ++emsg_off; |
Bram Moolenaar | b171fb1 | 2020-06-24 20:34:03 +0200 | [diff] [blame] | 5056 | p = eval_to_string(expr, FALSE); |
Bram Moolenaar | e685079 | 2010-05-14 15:28:44 +0200 | [diff] [blame] | 5057 | --emsg_off; |
| 5058 | vim_free(expr); |
| 5059 | } |
| 5060 | if (p != NULL) |
| 5061 | { |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5062 | msg((char *)p); |
Bram Moolenaar | e685079 | 2010-05-14 15:28:44 +0200 | [diff] [blame] | 5063 | vim_free(p); |
| 5064 | } |
| 5065 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5066 | msg("default"); |
Bram Moolenaar | e685079 | 2010-05-14 15:28:44 +0200 | [diff] [blame] | 5067 | #else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5068 | msg(_("unknown")); |
Bram Moolenaar | e685079 | 2010-05-14 15:28:44 +0200 | [diff] [blame] | 5069 | #endif |
| 5070 | } |
| 5071 | else if (load_colors(eap->arg) == FAIL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5072 | semsg(_("E185: Cannot find color scheme '%s'"), eap->arg); |
Bram Moolenaar | f58d81a | 2019-01-28 20:19:05 +0100 | [diff] [blame] | 5073 | |
| 5074 | #ifdef FEAT_VTP |
| 5075 | else if (has_vtp_working()) |
| 5076 | { |
| 5077 | // background color change requires clear + redraw |
| 5078 | update_screen(CLEAR); |
| 5079 | redrawcmd(); |
| 5080 | } |
| 5081 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
| 5084 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5085 | ex_highlight(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5086 | { |
| 5087 | if (*eap->arg == NUL && eap->cmd[2] == '!') |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5088 | msg(_("Greetings, Vim user!")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5089 | do_highlight(eap->arg, eap->forceit, FALSE); |
| 5090 | } |
| 5091 | |
| 5092 | |
| 5093 | /* |
| 5094 | * Call this function if we thought we were going to exit, but we won't |
| 5095 | * (because of an error). May need to restore the terminal mode. |
| 5096 | */ |
| 5097 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5098 | not_exiting(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5099 | { |
| 5100 | exiting = FALSE; |
| 5101 | settmode(TMODE_RAW); |
| 5102 | } |
| 5103 | |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5104 | static int |
| 5105 | before_quit_autocmds(win_T *wp, int quit_all, int forceit) |
| 5106 | { |
| 5107 | apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer); |
| 5108 | |
Bram Moolenaar | 34ba06b | 2019-10-20 22:27:10 +0200 | [diff] [blame] | 5109 | // Bail out when autocommands closed the window. |
| 5110 | // Refuse to quit when the buffer in the last window is being closed (can |
| 5111 | // only happen in autocommands). |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5112 | if (!win_valid(wp) |
| 5113 | || curbuf_locked() |
| 5114 | || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0)) |
| 5115 | return TRUE; |
| 5116 | |
| 5117 | if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window())) |
| 5118 | { |
| 5119 | apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 34ba06b | 2019-10-20 22:27:10 +0200 | [diff] [blame] | 5120 | // Refuse to quit when locked or when the window was closed or the |
| 5121 | // buffer in the last window is being closed (can only happen in |
| 5122 | // autocommands). |
| 5123 | if (!win_valid(wp) || curbuf_locked() |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5124 | || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0)) |
| 5125 | return TRUE; |
| 5126 | } |
| 5127 | |
| 5128 | return FALSE; |
| 5129 | } |
| 5130 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5131 | /* |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 5132 | * ":quit": quit current window, quit Vim if the last window is closed. |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5133 | * ":{nr}quit": quit window {nr} |
Bram Moolenaar | 4d14bac | 2019-10-20 21:15:15 +0200 | [diff] [blame] | 5134 | * Also used when closing a terminal window that's the last one. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5135 | */ |
Bram Moolenaar | 4d14bac | 2019-10-20 21:15:15 +0200 | [diff] [blame] | 5136 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5137 | ex_quit(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5138 | { |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5139 | win_T *wp; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5140 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5141 | #ifdef FEAT_CMDWIN |
| 5142 | if (cmdwin_type != 0) |
| 5143 | { |
| 5144 | cmdwin_result = Ctrl_C; |
| 5145 | return; |
| 5146 | } |
| 5147 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5148 | // Don't quit while editing the command line. |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5149 | if (text_locked()) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5150 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5151 | text_locked_msg(); |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5152 | return; |
| 5153 | } |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5154 | if (eap->addr_count > 0) |
| 5155 | { |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 5156 | int wnr = eap->line2; |
| 5157 | |
| 5158 | for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next) |
| 5159 | if (--wnr <= 0) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5160 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5161 | } |
| 5162 | else |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5163 | wp = curwin; |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 5164 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5165 | // Refuse to quit when locked. |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 5166 | if (curbuf_locked()) |
| 5167 | return; |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5168 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5169 | // Trigger QuitPre and maybe ExitPre |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5170 | if (before_quit_autocmds(wp, FALSE, eap->forceit)) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5171 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5172 | |
| 5173 | #ifdef FEAT_NETBEANS_INTG |
| 5174 | netbeansForcedQuit = eap->forceit; |
| 5175 | #endif |
| 5176 | |
| 5177 | /* |
| 5178 | * If there are more files or windows we won't exit. |
| 5179 | */ |
| 5180 | if (check_more(FALSE, eap->forceit) == OK && only_one_window()) |
| 5181 | exiting = TRUE; |
Bram Moolenaar | ff930ca | 2017-10-19 17:12:10 +0200 | [diff] [blame] | 5182 | if ((!buf_hide(wp->w_buffer) |
| 5183 | && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0) |
Bram Moolenaar | 45d3b14 | 2013-11-09 03:31:51 +0100 | [diff] [blame] | 5184 | | (eap->forceit ? CCGD_FORCEIT : 0) |
| 5185 | | CCGD_EXCMD)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5186 | || check_more(TRUE, eap->forceit) == FAIL |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 5187 | || (only_one_window() && check_changed_any(eap->forceit, TRUE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5188 | { |
| 5189 | not_exiting(); |
| 5190 | } |
| 5191 | else |
| 5192 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5193 | // quit last window |
| 5194 | // Note: only_one_window() returns true, even so a help window is |
| 5195 | // still open. In that case only quit, if no address has been |
| 5196 | // specified. Example: |
| 5197 | // :h|wincmd w|1q - don't quit |
| 5198 | // :h|wincmd w|q - quit |
Bram Moolenaar | a1f4cb9 | 2016-11-06 15:25:42 +0100 | [diff] [blame] | 5199 | if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5200 | getout(0); |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 5201 | not_exiting(); |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5202 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5203 | need_mouse_correct = TRUE; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5204 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5205 | // close window; may free buffer |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 5206 | win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5207 | } |
| 5208 | } |
| 5209 | |
| 5210 | /* |
| 5211 | * ":cquit". |
| 5212 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5213 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5214 | ex_cquit(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5215 | { |
Bram Moolenaar | 1860bde | 2020-01-06 21:47:21 +0100 | [diff] [blame] | 5216 | // this does not always pass on the exit code to the Manx compiler. why? |
| 5217 | getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | /* |
| 5221 | * ":qall": try to quit all windows |
| 5222 | */ |
| 5223 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5224 | ex_quit_all(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5225 | { |
| 5226 | # ifdef FEAT_CMDWIN |
| 5227 | if (cmdwin_type != 0) |
| 5228 | { |
| 5229 | if (eap->forceit) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5230 | cmdwin_result = K_XF1; // ex_window() takes care of this |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5231 | else |
| 5232 | cmdwin_result = K_XF2; |
| 5233 | return; |
| 5234 | } |
| 5235 | # endif |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5236 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5237 | // Don't quit while editing the command line. |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5238 | if (text_locked()) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5239 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5240 | text_locked_msg(); |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5241 | return; |
| 5242 | } |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5243 | |
| 5244 | if (before_quit_autocmds(curwin, TRUE, eap->forceit)) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5245 | return; |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5246 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5247 | exiting = TRUE; |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 5248 | if (eap->forceit || !check_changed_any(FALSE, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5249 | getout(0); |
| 5250 | not_exiting(); |
| 5251 | } |
| 5252 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5253 | /* |
| 5254 | * ":close": close current window, unless it is the last one |
| 5255 | */ |
| 5256 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5257 | ex_close(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5258 | { |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5259 | win_T *win; |
| 5260 | int winnr = 0; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5261 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5262 | if (cmdwin_type != 0) |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 5263 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5264 | else |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5265 | #endif |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 5266 | if (!text_locked() && !curbuf_locked()) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5267 | { |
| 5268 | if (eap->addr_count == 0) |
| 5269 | ex_win_close(eap->forceit, curwin, NULL); |
Bram Moolenaar | 6d41c78 | 2018-06-06 09:11:12 +0200 | [diff] [blame] | 5270 | else |
| 5271 | { |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 5272 | FOR_ALL_WINDOWS(win) |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5273 | { |
| 5274 | winnr++; |
| 5275 | if (winnr == eap->line2) |
| 5276 | break; |
| 5277 | } |
| 5278 | if (win == NULL) |
| 5279 | win = lastwin; |
| 5280 | ex_win_close(eap->forceit, win, NULL); |
| 5281 | } |
| 5282 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5285 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5286 | /* |
| 5287 | * ":pclose": Close any preview window. |
| 5288 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5290 | ex_pclose(exarg_T *eap) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5291 | { |
| 5292 | win_T *win; |
| 5293 | |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 5294 | // First close any normal window. |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 5295 | FOR_ALL_WINDOWS(win) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5296 | if (win->w_p_pvw) |
| 5297 | { |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5298 | ex_win_close(eap->forceit, win, NULL); |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 5299 | return; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5300 | } |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 5301 | # ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 5302 | // Also when 'previewpopup' is empty, it might have been cleared. |
Bram Moolenaar | c7c5f10 | 2019-08-21 18:31:03 +0200 | [diff] [blame] | 5303 | popup_close_preview(); |
Bram Moolenaar | 7964873 | 2019-07-18 21:43:07 +0200 | [diff] [blame] | 5304 | # endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5305 | } |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5306 | #endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5307 | |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5308 | /* |
| 5309 | * Close window "win" and take care of handling closing the last window for a |
| 5310 | * modified buffer. |
| 5311 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5312 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5313 | ex_win_close( |
| 5314 | int forceit, |
| 5315 | win_T *win, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5316 | tabpage_T *tp) // NULL or the tab page "win" is in |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5317 | { |
| 5318 | int need_hide; |
| 5319 | buf_T *buf = win->w_buffer; |
| 5320 | |
Bram Moolenaar | cf84417 | 2020-06-26 19:44:06 +0200 | [diff] [blame] | 5321 | // Never close the autocommand window. |
| 5322 | if (win == aucmd_win) |
| 5323 | { |
| 5324 | emsg(_(e_autocmd_close)); |
| 5325 | return; |
| 5326 | } |
| 5327 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5328 | need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 5329 | if (need_hide && !buf_hide(buf) && !forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5330 | { |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5331 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5332 | if ((p_confirm || cmdmod.confirm) && p_write) |
| 5333 | { |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 5334 | bufref_T bufref; |
| 5335 | |
| 5336 | set_bufref(&bufref, buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5337 | dialog_changed(buf, FALSE); |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 5338 | if (bufref_valid(&bufref) && bufIsChanged(buf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5339 | return; |
| 5340 | need_hide = FALSE; |
| 5341 | } |
| 5342 | else |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5343 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5344 | { |
Bram Moolenaar | f5be7cd | 2017-08-17 16:55:13 +0200 | [diff] [blame] | 5345 | no_write_message(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5346 | return; |
| 5347 | } |
| 5348 | } |
| 5349 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5350 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | need_mouse_correct = TRUE; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5352 | #endif |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5353 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5354 | // free buffer when not hiding it or when it's a scratch buffer |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5355 | if (tp == NULL) |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 5356 | win_close(win, !need_hide && !buf_hide(buf)); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5357 | else |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 5358 | win_close_othertab(win, !need_hide && !buf_hide(buf), tp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5359 | } |
| 5360 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5361 | /* |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5362 | * Handle the argument for a tabpage related ex command. |
| 5363 | * Returns a tabpage number. |
| 5364 | * When an error is encountered then eap->errmsg is set. |
| 5365 | */ |
| 5366 | static int |
| 5367 | get_tabpage_arg(exarg_T *eap) |
| 5368 | { |
| 5369 | int tab_number; |
| 5370 | int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1; |
| 5371 | |
| 5372 | if (eap->arg && *eap->arg != NUL) |
| 5373 | { |
| 5374 | char_u *p = eap->arg; |
| 5375 | char_u *p_save; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5376 | int relative = 0; // argument +N/-N means: go to N places to the |
| 5377 | // right/left relative to the current position. |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5378 | |
| 5379 | if (*p == '-') |
| 5380 | { |
| 5381 | relative = -1; |
| 5382 | p++; |
| 5383 | } |
| 5384 | else if (*p == '+') |
| 5385 | { |
| 5386 | relative = 1; |
| 5387 | p++; |
| 5388 | } |
| 5389 | |
| 5390 | p_save = p; |
| 5391 | tab_number = getdigits(&p); |
| 5392 | |
| 5393 | if (relative == 0) |
| 5394 | { |
| 5395 | if (STRCMP(p, "$") == 0) |
| 5396 | tab_number = LAST_TAB_NR; |
| 5397 | else if (p == p_save || *p_save == '-' || *p != NUL |
| 5398 | || tab_number > LAST_TAB_NR) |
| 5399 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5400 | // No numbers as argument. |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 5401 | eap->errmsg = ex_errmsg(e_invarg2, eap->arg); |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5402 | goto theend; |
| 5403 | } |
| 5404 | } |
| 5405 | else |
| 5406 | { |
| 5407 | if (*p_save == NUL) |
| 5408 | tab_number = 1; |
| 5409 | else if (p == p_save || *p_save == '-' || *p != NUL |
| 5410 | || tab_number == 0) |
| 5411 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5412 | // No numbers as argument. |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 5413 | eap->errmsg = ex_errmsg(e_invarg2, eap->arg); |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5414 | goto theend; |
| 5415 | } |
| 5416 | tab_number = tab_number * relative + tabpage_index(curtab); |
| 5417 | if (!unaccept_arg0 && relative == -1) |
| 5418 | --tab_number; |
| 5419 | } |
| 5420 | if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR) |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 5421 | eap->errmsg = ex_errmsg(e_invarg2, eap->arg); |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5422 | } |
| 5423 | else if (eap->addr_count > 0) |
| 5424 | { |
| 5425 | if (unaccept_arg0 && eap->line2 == 0) |
Bram Moolenaar | c625155 | 2017-01-29 21:42:20 +0100 | [diff] [blame] | 5426 | { |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 5427 | eap->errmsg = _(e_invrange); |
Bram Moolenaar | c625155 | 2017-01-29 21:42:20 +0100 | [diff] [blame] | 5428 | tab_number = 0; |
| 5429 | } |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5430 | else |
| 5431 | { |
| 5432 | tab_number = eap->line2; |
Bram Moolenaar | 82a1246 | 2019-01-22 22:41:42 +0100 | [diff] [blame] | 5433 | if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-') |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5434 | { |
| 5435 | --tab_number; |
| 5436 | if (tab_number < unaccept_arg0) |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 5437 | eap->errmsg = _(e_invrange); |
Bram Moolenaar | dea2570 | 2017-01-29 15:18:10 +0100 | [diff] [blame] | 5438 | } |
| 5439 | } |
| 5440 | } |
| 5441 | else |
| 5442 | { |
| 5443 | switch (eap->cmdidx) |
| 5444 | { |
| 5445 | case CMD_tabnext: |
| 5446 | tab_number = tabpage_index(curtab) + 1; |
| 5447 | if (tab_number > LAST_TAB_NR) |
| 5448 | tab_number = 1; |
| 5449 | break; |
| 5450 | case CMD_tabmove: |
| 5451 | tab_number = LAST_TAB_NR; |
| 5452 | break; |
| 5453 | default: |
| 5454 | tab_number = tabpage_index(curtab); |
| 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | theend: |
| 5459 | return tab_number; |
| 5460 | } |
| 5461 | |
| 5462 | /* |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5463 | * ":tabclose": close current tab page, unless it is the last one. |
| 5464 | * ":tabclose N": close tab page N. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | */ |
| 5466 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5467 | ex_tabclose(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5468 | { |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5469 | tabpage_T *tp; |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5470 | int tab_number; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5471 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5472 | # ifdef FEAT_CMDWIN |
| 5473 | if (cmdwin_type != 0) |
| 5474 | cmdwin_result = K_IGNORE; |
| 5475 | else |
| 5476 | # endif |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5477 | if (first_tabpage->tp_next == NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5478 | emsg(_("E784: Cannot close last tab page")); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5479 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5480 | { |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5481 | tab_number = get_tabpage_arg(eap); |
| 5482 | if (eap->errmsg == NULL) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 5483 | { |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5484 | tp = find_tabpage(tab_number); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5485 | if (tp == NULL) |
| 5486 | { |
| 5487 | beep_flush(); |
| 5488 | return; |
| 5489 | } |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 5490 | if (tp != curtab) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5491 | { |
| 5492 | tabpage_close_other(tp, eap->forceit); |
| 5493 | return; |
| 5494 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 5495 | else if (!text_locked() && !curbuf_locked()) |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5496 | tabpage_close(eap->forceit); |
| 5497 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5498 | } |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5499 | } |
| 5500 | |
| 5501 | /* |
| 5502 | * ":tabonly": close all tab pages except the current one |
| 5503 | */ |
| 5504 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5505 | ex_tabonly(exarg_T *eap) |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5506 | { |
| 5507 | tabpage_T *tp; |
| 5508 | int done; |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5509 | int tab_number; |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5510 | |
| 5511 | # ifdef FEAT_CMDWIN |
| 5512 | if (cmdwin_type != 0) |
| 5513 | cmdwin_result = K_IGNORE; |
| 5514 | else |
| 5515 | # endif |
| 5516 | if (first_tabpage->tp_next == NULL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 5517 | msg(_("Already only one tab page")); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5518 | else |
| 5519 | { |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5520 | tab_number = get_tabpage_arg(eap); |
| 5521 | if (eap->errmsg == NULL) |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5522 | { |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5523 | goto_tabpage(tab_number); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5524 | // Repeat this up to a 1000 times, because autocommands may |
| 5525 | // mess up the lists. |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5526 | for (done = 0; done < 1000; ++done) |
| 5527 | { |
| 5528 | FOR_ALL_TABPAGES(tp) |
| 5529 | if (tp->tp_topframe != topframe) |
| 5530 | { |
| 5531 | tabpage_close_other(tp, eap->forceit); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5532 | // if we failed to close it quit |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5533 | if (valid_tabpage(tp)) |
| 5534 | done = 1000; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5535 | // start over, "tp" is now invalid |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5536 | break; |
| 5537 | } |
| 5538 | if (first_tabpage->tp_next == NULL) |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5539 | break; |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 5540 | } |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5541 | } |
| 5542 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5543 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5544 | |
| 5545 | /* |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5546 | * Close the current tab page. |
| 5547 | */ |
| 5548 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5549 | tabpage_close(int forceit) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5550 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5551 | // First close all the windows but the current one. If that worked then |
| 5552 | // close the last window in this tab, that will close it. |
Bram Moolenaar | 459ca56 | 2016-11-10 18:16:33 +0100 | [diff] [blame] | 5553 | if (!ONE_WINDOW) |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 5554 | close_others(TRUE, forceit); |
Bram Moolenaar | 459ca56 | 2016-11-10 18:16:33 +0100 | [diff] [blame] | 5555 | if (ONE_WINDOW) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5556 | ex_win_close(forceit, curwin, NULL); |
| 5557 | # ifdef FEAT_GUI |
| 5558 | need_mouse_correct = TRUE; |
| 5559 | # endif |
| 5560 | } |
| 5561 | |
| 5562 | /* |
| 5563 | * Close tab page "tp", which is not the current tab page. |
| 5564 | * Note that autocommands may make "tp" invalid. |
Bram Moolenaar | 7875acc | 2006-09-10 13:51:17 +0000 | [diff] [blame] | 5565 | * Also takes care of the tab pages line disappearing when closing the |
| 5566 | * last-but-one tab page. |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5567 | */ |
| 5568 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5569 | tabpage_close_other(tabpage_T *tp, int forceit) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5570 | { |
| 5571 | int done = 0; |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5572 | win_T *wp; |
Bram Moolenaar | 7875acc | 2006-09-10 13:51:17 +0000 | [diff] [blame] | 5573 | int h = tabline_height(); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5574 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5575 | // Limit to 1000 windows, autocommands may add a window while we close |
| 5576 | // one. OK, so I'm paranoid... |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5577 | while (++done < 1000) |
| 5578 | { |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5579 | wp = tp->tp_firstwin; |
| 5580 | ex_win_close(forceit, wp, tp); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5581 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5582 | // Autocommands may delete the tab page under our fingers and we may |
| 5583 | // fail to close a window with a modified buffer. |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5584 | if (!valid_tabpage(tp) || tp->tp_firstwin == wp) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5585 | break; |
| 5586 | } |
Bram Moolenaar | 7875acc | 2006-09-10 13:51:17 +0000 | [diff] [blame] | 5587 | |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 5588 | apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf); |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 5589 | |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 5590 | redraw_tabline = TRUE; |
Bram Moolenaar | 7875acc | 2006-09-10 13:51:17 +0000 | [diff] [blame] | 5591 | if (h != tabline_height()) |
| 5592 | shell_new_rows(); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5596 | * ":only". |
| 5597 | */ |
| 5598 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5599 | ex_only(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5600 | { |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5601 | win_T *wp; |
| 5602 | int wnr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5603 | # ifdef FEAT_GUI |
| 5604 | need_mouse_correct = TRUE; |
| 5605 | # endif |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5606 | if (eap->addr_count > 0) |
| 5607 | { |
| 5608 | wnr = eap->line2; |
| 5609 | for (wp = firstwin; --wnr > 0; ) |
| 5610 | { |
| 5611 | if (wp->w_next == NULL) |
| 5612 | break; |
| 5613 | else |
| 5614 | wp = wp->w_next; |
| 5615 | } |
| 5616 | win_goto(wp); |
| 5617 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5618 | close_others(TRUE, eap->forceit); |
| 5619 | } |
| 5620 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5621 | static void |
Bram Moolenaar | 5e1e6d2 | 2017-01-02 17:26:00 +0100 | [diff] [blame] | 5622 | ex_hide(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5623 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5624 | // ":hide" or ":hide | cmd": hide current window |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 5625 | if (!eap->skip) |
| 5626 | { |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5627 | #ifdef FEAT_GUI |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 5628 | need_mouse_correct = TRUE; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5629 | #endif |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 5630 | if (eap->addr_count == 0) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5631 | win_close(curwin, FALSE); // don't free buffer |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 5632 | else |
| 5633 | { |
| 5634 | int winnr = 0; |
| 5635 | win_T *win; |
Bram Moolenaar | f240e18 | 2014-11-27 18:33:02 +0100 | [diff] [blame] | 5636 | |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 5637 | FOR_ALL_WINDOWS(win) |
| 5638 | { |
| 5639 | winnr++; |
| 5640 | if (winnr == eap->line2) |
| 5641 | break; |
Bram Moolenaar | b96a7f3 | 2014-11-27 16:22:48 +0100 | [diff] [blame] | 5642 | } |
Bram Moolenaar | 2256c99 | 2016-11-15 21:17:07 +0100 | [diff] [blame] | 5643 | if (win == NULL) |
| 5644 | win = lastwin; |
| 5645 | win_close(win, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5646 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5647 | } |
| 5648 | } |
| 5649 | |
| 5650 | /* |
| 5651 | * ":stop" and ":suspend": Suspend Vim. |
| 5652 | */ |
| 5653 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5654 | ex_stop(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5655 | { |
| 5656 | /* |
| 5657 | * Disallow suspending for "rvim". |
| 5658 | */ |
Bram Moolenaar | cea912a | 2016-10-12 14:20:24 +0200 | [diff] [blame] | 5659 | if (!check_restricted()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5660 | { |
| 5661 | if (!eap->forceit) |
| 5662 | autowrite_all(); |
| 5663 | windgoto((int)Rows - 1, 0); |
| 5664 | out_char('\n'); |
| 5665 | out_flush(); |
| 5666 | stoptermcap(); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5667 | out_flush(); // needed for SUN to restore xterm buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5668 | #ifdef FEAT_TITLE |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5669 | mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5670 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5671 | ui_suspend(); // call machine specific function |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5672 | #ifdef FEAT_TITLE |
| 5673 | maketitle(); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5674 | resettitle(); // force updating the title |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5675 | #endif |
| 5676 | starttermcap(); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5677 | scroll_start(); // scroll screen before redrawing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5678 | redraw_later_clear(); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5679 | shell_resized(); // may have resized window |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | /* |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5684 | * ":exit", ":xit" and ":wq": Write file and quite the current window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5685 | */ |
| 5686 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5687 | ex_exit(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5688 | { |
| 5689 | #ifdef FEAT_CMDWIN |
| 5690 | if (cmdwin_type != 0) |
| 5691 | { |
| 5692 | cmdwin_result = Ctrl_C; |
| 5693 | return; |
| 5694 | } |
| 5695 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5696 | // Don't quit while editing the command line. |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5697 | if (text_locked()) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5698 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 5699 | text_locked_msg(); |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 5700 | return; |
| 5701 | } |
Bram Moolenaar | 12a96de | 2018-03-11 14:44:18 +0100 | [diff] [blame] | 5702 | |
| 5703 | if (before_quit_autocmds(curwin, FALSE, eap->forceit)) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 5704 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5705 | |
| 5706 | /* |
| 5707 | * if more files or windows we won't exit |
| 5708 | */ |
| 5709 | if (check_more(FALSE, eap->forceit) == OK && only_one_window()) |
| 5710 | exiting = TRUE; |
| 5711 | if ( ((eap->cmdidx == CMD_wq |
| 5712 | || curbufIsChanged()) |
| 5713 | && do_write(eap) == FAIL) |
| 5714 | || check_more(TRUE, eap->forceit) == FAIL |
Bram Moolenaar | 027387f | 2016-01-02 22:25:52 +0100 | [diff] [blame] | 5715 | || (only_one_window() && check_changed_any(eap->forceit, FALSE))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5716 | { |
| 5717 | not_exiting(); |
| 5718 | } |
| 5719 | else |
| 5720 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5721 | if (only_one_window()) // quit last window, exit Vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5722 | getout(0); |
Bram Moolenaar | 2c33d7b | 2017-10-14 16:06:20 +0200 | [diff] [blame] | 5723 | not_exiting(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5724 | # ifdef FEAT_GUI |
| 5725 | need_mouse_correct = TRUE; |
| 5726 | # endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5727 | // Quit current window, may free the buffer. |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 5728 | win_close(curwin, !buf_hide(curwin->w_buffer)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5729 | } |
| 5730 | } |
| 5731 | |
| 5732 | /* |
| 5733 | * ":print", ":list", ":number". |
| 5734 | */ |
| 5735 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5736 | ex_print(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5737 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5738 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 5739 | emsg(_(e_emptybuf)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5740 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5741 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5742 | for ( ;!got_int; ui_breakcheck()) |
| 5743 | { |
| 5744 | print_line(eap->line1, |
| 5745 | (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound |
| 5746 | || (eap->flags & EXFLAG_NR)), |
| 5747 | eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST)); |
| 5748 | if (++eap->line1 > eap->line2) |
| 5749 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5750 | out_flush(); // show one line at a time |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5751 | } |
| 5752 | setpcmark(); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5753 | // put cursor at last line |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 5754 | curwin->w_cursor.lnum = eap->line2; |
| 5755 | beginline(BL_SOL | BL_FIX); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5756 | } |
| 5757 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5758 | ex_no_reprint = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5759 | } |
| 5760 | |
| 5761 | #ifdef FEAT_BYTEOFF |
| 5762 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5763 | ex_goto(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5764 | { |
| 5765 | goto_byte(eap->line2); |
| 5766 | } |
| 5767 | #endif |
| 5768 | |
| 5769 | /* |
| 5770 | * ":shell". |
| 5771 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5772 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5773 | ex_shell(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5774 | { |
| 5775 | do_shell(NULL, 0); |
| 5776 | } |
| 5777 | |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5778 | #if defined(HAVE_DROP_FILE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5779 | |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5780 | static int drop_busy = FALSE; |
| 5781 | static int drop_filec; |
| 5782 | static char_u **drop_filev = NULL; |
| 5783 | static int drop_split; |
| 5784 | static void (*drop_callback)(void *); |
| 5785 | static void *drop_cookie; |
| 5786 | |
| 5787 | static void |
| 5788 | handle_drop_internal(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5789 | { |
| 5790 | exarg_T ea; |
| 5791 | int save_msg_scroll = msg_scroll; |
| 5792 | |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5793 | // Setting the argument list may cause screen updates and being called |
| 5794 | // recursively. Avoid that by setting drop_busy. |
| 5795 | drop_busy = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5796 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5797 | // Check whether the current buffer is changed. If so, we will need |
| 5798 | // to split the current window or data could be lost. |
| 5799 | // We don't need to check if the 'hidden' option is set, as in this |
| 5800 | // case the buffer won't be lost. |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5801 | if (!buf_hide(curbuf) && !drop_split) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5802 | { |
| 5803 | ++emsg_off; |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5804 | drop_split = check_changed(curbuf, CCGD_AW); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5805 | --emsg_off; |
| 5806 | } |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5807 | if (drop_split) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5808 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5809 | if (win_split(0, 0) == FAIL) |
| 5810 | return; |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 5811 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5813 | // When splitting the window, create a new alist. Otherwise the |
| 5814 | // existing one is overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5815 | alist_unlink(curwin->w_alist); |
| 5816 | alist_new(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
| 5819 | /* |
| 5820 | * Set up the new argument list. |
| 5821 | */ |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5822 | alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5823 | |
| 5824 | /* |
| 5825 | * Move to the first file. |
| 5826 | */ |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5827 | // Fake up a minimal "next" command for do_argfile() |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 5828 | CLEAR_FIELD(ea); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5829 | ea.cmd = (char_u *)"next"; |
| 5830 | do_argfile(&ea, 0); |
| 5831 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5832 | // do_ecmd() may set need_start_insertmode, but since we never left Insert |
| 5833 | // mode that is not needed here. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5834 | need_start_insertmode = FALSE; |
| 5835 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5836 | // Restore msg_scroll, otherwise a following command may cause scrolling |
| 5837 | // unexpectedly. The screen will be redrawn by the caller, thus |
| 5838 | // msg_scroll being set by displaying a message is irrelevant. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5839 | msg_scroll = save_msg_scroll; |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5840 | |
| 5841 | if (drop_callback != NULL) |
| 5842 | drop_callback(drop_cookie); |
| 5843 | |
| 5844 | drop_filev = NULL; |
| 5845 | drop_busy = FALSE; |
| 5846 | } |
| 5847 | |
| 5848 | /* |
| 5849 | * Handle a file drop. The code is here because a drop is *nearly* like an |
| 5850 | * :args command, but not quite (we have a list of exact filenames, so we |
Bram Moolenaar | b5443cc | 2019-01-15 20:19:40 +0100 | [diff] [blame] | 5851 | * don't want to (a) parse a command line, or (b) expand wildcards). So the |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5852 | * code is very similar to :args and hence needs access to a lot of the static |
| 5853 | * functions in this file. |
| 5854 | * |
| 5855 | * The "filev" list must have been allocated using alloc(), as should each item |
| 5856 | * in the list. This function takes over responsibility for freeing the "filev" |
| 5857 | * list. |
| 5858 | */ |
| 5859 | void |
| 5860 | handle_drop( |
| 5861 | int filec, // the number of files dropped |
| 5862 | char_u **filev, // the list of files dropped |
| 5863 | int split, // force splitting the window |
| 5864 | void (*callback)(void *), // to be called after setting the argument |
| 5865 | // list |
| 5866 | void *cookie) // argument for "callback" (allocated) |
| 5867 | { |
| 5868 | // Cannot handle recursive drops, finish the pending one. |
| 5869 | if (drop_busy) |
| 5870 | { |
| 5871 | FreeWild(filec, filev); |
| 5872 | vim_free(cookie); |
| 5873 | return; |
| 5874 | } |
| 5875 | |
| 5876 | // When calling handle_drop() more than once in a row we only use the last |
| 5877 | // one. |
| 5878 | if (drop_filev != NULL) |
| 5879 | { |
| 5880 | FreeWild(drop_filec, drop_filev); |
| 5881 | vim_free(drop_cookie); |
| 5882 | } |
| 5883 | |
| 5884 | drop_filec = filec; |
| 5885 | drop_filev = filev; |
| 5886 | drop_split = split; |
| 5887 | drop_callback = callback; |
| 5888 | drop_cookie = cookie; |
| 5889 | |
| 5890 | // Postpone this when: |
| 5891 | // - editing the command line |
| 5892 | // - not possible to change the current buffer |
| 5893 | // - updating the screen |
| 5894 | // As it may change buffers and window structures that are in use and cause |
| 5895 | // freed memory to be used. |
| 5896 | if (text_locked() || curbuf_locked() || updating_screen) |
| 5897 | return; |
| 5898 | |
| 5899 | handle_drop_internal(); |
| 5900 | } |
| 5901 | |
| 5902 | /* |
| 5903 | * To be called when text is unlocked, curbuf is unlocked or updating_screen is |
| 5904 | * reset: Handle a postponed drop. |
| 5905 | */ |
| 5906 | void |
| 5907 | handle_any_postponed_drop(void) |
| 5908 | { |
| 5909 | if (!drop_busy && drop_filev != NULL |
Bram Moolenaar | 6adb9ea | 2020-04-30 22:31:18 +0200 | [diff] [blame] | 5910 | && !text_locked() && !curbuf_locked() && !updating_screen) |
Bram Moolenaar | 92d147b | 2018-07-29 17:35:23 +0200 | [diff] [blame] | 5911 | handle_drop_internal(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5912 | } |
| 5913 | #endif |
| 5914 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5915 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5916 | * ":preserve". |
| 5917 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5918 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5919 | ex_preserve(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5920 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 5921 | curbuf->b_flags |= BF_PRESERVED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5922 | ml_preserve(curbuf, TRUE); |
| 5923 | } |
| 5924 | |
| 5925 | /* |
| 5926 | * ":recover". |
| 5927 | */ |
| 5928 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5929 | ex_recover(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5930 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5931 | // Set recoverymode right away to avoid the ATTENTION prompt. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5932 | recoverymode = TRUE; |
Bram Moolenaar | 45d3b14 | 2013-11-09 03:31:51 +0100 | [diff] [blame] | 5933 | if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0) |
| 5934 | | CCGD_MULTWIN |
| 5935 | | (eap->forceit ? CCGD_FORCEIT : 0) |
| 5936 | | CCGD_EXCMD) |
| 5937 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5938 | && (*eap->arg == NUL |
| 5939 | || setfname(curbuf, eap->arg, NULL, TRUE) == OK)) |
Bram Moolenaar | 99499b1 | 2019-05-23 21:35:48 +0200 | [diff] [blame] | 5940 | ml_recover(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5941 | recoverymode = FALSE; |
| 5942 | } |
| 5943 | |
| 5944 | /* |
| 5945 | * Command modifier used in a wrong way. |
| 5946 | */ |
| 5947 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5948 | ex_wrongmodifier(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5949 | { |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 5950 | eap->errmsg = _(e_invcmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5951 | } |
| 5952 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5953 | /* |
| 5954 | * :sview [+command] file split window with new file, read-only |
| 5955 | * :split [[+command] file] split window with current or new file |
| 5956 | * :vsplit [[+command] file] split window vertically with current or new file |
| 5957 | * :new [[+command] file] split window with no or new file |
| 5958 | * :vnew [[+command] file] split vertically window with no or new file |
| 5959 | * :sfind [+command] file split window with file in 'path' |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 5960 | * |
| 5961 | * :tabedit open new Tab page with empty window |
| 5962 | * :tabedit [+command] file open new Tab page and edit "file" |
| 5963 | * :tabnew [[+command] file] just like :tabedit |
| 5964 | * :tabfind [+command] file open new Tab page and find "file" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5965 | */ |
| 5966 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5967 | ex_splitview(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5968 | { |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 5969 | win_T *old_curwin = curwin; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5970 | #if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5971 | char_u *fname = NULL; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5972 | #endif |
| 5973 | #ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5974 | int browse_flag = cmdmod.browse; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5975 | #endif |
Bram Moolenaar | 39902a0 | 2018-06-21 22:10:08 +0200 | [diff] [blame] | 5976 | int use_tab = eap->cmdidx == CMD_tabedit |
| 5977 | || eap->cmdidx == CMD_tabfind |
| 5978 | || eap->cmdidx == CMD_tabnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5979 | |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 5980 | if (ERROR_IF_ANY_POPUP_WINDOW) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 5981 | return; |
| 5982 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5983 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5984 | need_mouse_correct = TRUE; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5985 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5986 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5987 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 5988 | // A ":split" in the quickfix window works like ":new". Don't want two |
| 5989 | // quickfix windows. But it's OK when doing ":tab split". |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 5990 | if (bt_quickfix(curbuf) && cmdmod.tab == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5991 | { |
| 5992 | if (eap->cmdidx == CMD_split) |
| 5993 | eap->cmdidx = CMD_new; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5994 | if (eap->cmdidx == CMD_vsplit) |
| 5995 | eap->cmdidx = CMD_vnew; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5996 | } |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5997 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5998 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 5999 | #ifdef FEAT_SEARCHPATH |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6000 | if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6001 | { |
| 6002 | fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), |
| 6003 | FNAME_MESS, TRUE, curbuf->b_ffname); |
| 6004 | if (fname == NULL) |
| 6005 | goto theend; |
| 6006 | eap->arg = fname; |
| 6007 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6008 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6009 | else |
| 6010 | # endif |
| 6011 | #endif |
| 6012 | #ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6013 | if (cmdmod.browse |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6014 | && eap->cmdidx != CMD_vnew |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6015 | && eap->cmdidx != CMD_new) |
| 6016 | { |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6017 | if ( |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6018 | # ifdef FEAT_GUI |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6019 | !gui.in_use && |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6020 | # endif |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6021 | au_has_group((char_u *)"FileExplorer")) |
| 6022 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6023 | // No browsing supported but we do have the file explorer: |
| 6024 | // Edit the directory. |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6025 | if (*eap->arg == NUL || !mch_isdir(eap->arg)) |
| 6026 | eap->arg = (char_u *)"."; |
| 6027 | } |
| 6028 | else |
| 6029 | { |
Bram Moolenaar | 39902a0 | 2018-06-21 22:10:08 +0200 | [diff] [blame] | 6030 | fname = do_browse(0, (char_u *)(use_tab |
| 6031 | ? _("Edit File in new tab page") |
| 6032 | : _("Edit File in new window")), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6033 | eap->arg, NULL, NULL, NULL, curbuf); |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6034 | if (fname == NULL) |
| 6035 | goto theend; |
| 6036 | eap->arg = fname; |
| 6037 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6038 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6039 | cmdmod.browse = FALSE; // Don't browse again in do_ecmd(). |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6040 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6041 | |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6042 | /* |
| 6043 | * Either open new tab page or split the window. |
| 6044 | */ |
Bram Moolenaar | 39902a0 | 2018-06-21 22:10:08 +0200 | [diff] [blame] | 6045 | if (use_tab) |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6046 | { |
Bram Moolenaar | 8dff818 | 2006-04-06 20:18:50 +0000 | [diff] [blame] | 6047 | if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab |
| 6048 | : eap->addr_count == 0 ? 0 |
| 6049 | : (int)eap->line2 + 1) != FAIL) |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6050 | { |
Bram Moolenaar | d2b6601 | 2008-01-09 19:30:36 +0000 | [diff] [blame] | 6051 | do_exedit(eap, old_curwin); |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6052 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6053 | // set the alternate buffer for the window we came from |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6054 | if (curwin != old_curwin |
| 6055 | && win_valid(old_curwin) |
| 6056 | && old_curwin->w_buffer != curbuf |
| 6057 | && !cmdmod.keepalt) |
| 6058 | old_curwin->w_alt_fnum = curbuf->b_fnum; |
| 6059 | } |
| 6060 | } |
| 6061 | else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6062 | *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL) |
| 6063 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6064 | // Reset 'scrollbind' when editing another file, but keep it when |
| 6065 | // doing ":split" without arguments. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6066 | if (*eap->arg != NUL |
Bram Moolenaar | 8a3bb56 | 2018-03-04 20:14:14 +0100 | [diff] [blame] | 6067 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6068 | || cmdmod.browse |
Bram Moolenaar | 8a3bb56 | 2018-03-04 20:14:14 +0100 | [diff] [blame] | 6069 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | ) |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6071 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6072 | else |
| 6073 | do_check_scrollbind(FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6074 | do_exedit(eap, old_curwin); |
| 6075 | } |
| 6076 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6077 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6078 | cmdmod.browse = browse_flag; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6079 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6080 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6081 | # if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6082 | theend: |
| 6083 | vim_free(fname); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6084 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6085 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6086 | |
| 6087 | /* |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6088 | * Open a new tab page. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6089 | */ |
| 6090 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6091 | tabpage_new(void) |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6092 | { |
| 6093 | exarg_T ea; |
| 6094 | |
Bram Moolenaar | a80faa8 | 2020-04-12 19:37:17 +0200 | [diff] [blame] | 6095 | CLEAR_FIELD(ea); |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6096 | ea.cmdidx = CMD_tabnew; |
| 6097 | ea.cmd = (char_u *)"tabn"; |
| 6098 | ea.arg = (char_u *)""; |
| 6099 | ex_splitview(&ea); |
| 6100 | } |
| 6101 | |
| 6102 | /* |
| 6103 | * :tabnext command |
| 6104 | */ |
| 6105 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6106 | ex_tabnext(exarg_T *eap) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6107 | { |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 6108 | int tab_number; |
| 6109 | |
Bram Moolenaar | 8cdbd5b | 2019-06-16 15:50:45 +0200 | [diff] [blame] | 6110 | if (ERROR_IF_POPUP_WINDOW) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 6111 | return; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6112 | switch (eap->cmdidx) |
| 6113 | { |
| 6114 | case CMD_tabfirst: |
| 6115 | case CMD_tabrewind: |
| 6116 | goto_tabpage(1); |
| 6117 | break; |
| 6118 | case CMD_tablast: |
| 6119 | goto_tabpage(9999); |
| 6120 | break; |
| 6121 | case CMD_tabprevious: |
| 6122 | case CMD_tabNext: |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 6123 | if (eap->arg && *eap->arg != NUL) |
| 6124 | { |
| 6125 | char_u *p = eap->arg; |
| 6126 | char_u *p_save = p; |
| 6127 | |
| 6128 | tab_number = getdigits(&p); |
| 6129 | if (p == p_save || *p_save == '-' || *p != NUL |
| 6130 | || tab_number == 0) |
| 6131 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6132 | // No numbers as argument. |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 6133 | eap->errmsg = ex_errmsg(e_invarg2, eap->arg); |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 6134 | return; |
| 6135 | } |
| 6136 | } |
| 6137 | else |
| 6138 | { |
| 6139 | if (eap->addr_count == 0) |
| 6140 | tab_number = 1; |
| 6141 | else |
| 6142 | { |
| 6143 | tab_number = eap->line2; |
| 6144 | if (tab_number < 1) |
| 6145 | { |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 6146 | eap->errmsg = _(e_invrange); |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 6147 | return; |
| 6148 | } |
| 6149 | } |
| 6150 | } |
| 6151 | goto_tabpage(-tab_number); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6152 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6153 | default: // CMD_tabnext |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 6154 | tab_number = get_tabpage_arg(eap); |
| 6155 | if (eap->errmsg == NULL) |
| 6156 | goto_tabpage(tab_number); |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6157 | break; |
| 6158 | } |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6159 | } |
| 6160 | |
| 6161 | /* |
| 6162 | * :tabmove command |
| 6163 | */ |
| 6164 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6165 | ex_tabmove(exarg_T *eap) |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6166 | { |
Bram Moolenaar | 40ce3a4 | 2015-04-21 18:08:39 +0200 | [diff] [blame] | 6167 | int tab_number; |
Bram Moolenaar | 8cb8dca | 2012-07-06 18:27:39 +0200 | [diff] [blame] | 6168 | |
Bram Moolenaar | 2f72c70 | 2017-01-29 14:48:10 +0100 | [diff] [blame] | 6169 | tab_number = get_tabpage_arg(eap); |
| 6170 | if (eap->errmsg == NULL) |
| 6171 | tabpage_move(tab_number); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | /* |
| 6175 | * :tabs command: List tabs and their contents. |
| 6176 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6177 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6178 | ex_tabs(exarg_T *eap UNUSED) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6179 | { |
| 6180 | tabpage_T *tp; |
| 6181 | win_T *wp; |
| 6182 | int tabcount = 1; |
| 6183 | |
| 6184 | msg_start(); |
| 6185 | msg_scroll = TRUE; |
| 6186 | for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next) |
| 6187 | { |
| 6188 | msg_putchar('\n'); |
| 6189 | vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 6190 | msg_outtrans_attr(IObuff, HL_ATTR(HLF_T)); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6191 | out_flush(); // output one line at a time |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6192 | ui_breakcheck(); |
| 6193 | |
Bram Moolenaar | 030f0df | 2006-02-21 22:02:53 +0000 | [diff] [blame] | 6194 | if (tp == curtab) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6195 | wp = firstwin; |
| 6196 | else |
| 6197 | wp = tp->tp_firstwin; |
| 6198 | for ( ; wp != NULL && !got_int; wp = wp->w_next) |
| 6199 | { |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6200 | msg_putchar('\n'); |
| 6201 | msg_putchar(wp == curwin ? '>' : ' '); |
| 6202 | msg_putchar(' '); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6203 | msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' '); |
| 6204 | msg_putchar(' '); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6205 | if (buf_spname(wp->w_buffer) != NULL) |
Bram Moolenaar | e1704ba | 2012-10-03 18:25:00 +0200 | [diff] [blame] | 6206 | vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6207 | else |
| 6208 | home_replace(wp->w_buffer, wp->w_buffer->b_fname, |
| 6209 | IObuff, IOSIZE, TRUE); |
| 6210 | msg_outtrans(IObuff); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6211 | out_flush(); // output one line at a time |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6212 | ui_breakcheck(); |
| 6213 | } |
| 6214 | } |
| 6215 | } |
| 6216 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6217 | /* |
| 6218 | * ":mode": Set screen mode. |
| 6219 | * If no argument given, just get the screen size and redraw. |
| 6220 | */ |
| 6221 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6222 | ex_mode(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6223 | { |
| 6224 | if (*eap->arg == NUL) |
| 6225 | shell_resized(); |
| 6226 | else |
Bram Moolenaar | 3c71aec | 2020-01-17 19:32:20 +0100 | [diff] [blame] | 6227 | emsg(_(e_screenmode)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6228 | } |
| 6229 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6230 | /* |
| 6231 | * ":resize". |
| 6232 | * set, increment or decrement current window height |
| 6233 | */ |
| 6234 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6235 | ex_resize(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6236 | { |
| 6237 | int n; |
| 6238 | win_T *wp = curwin; |
| 6239 | |
| 6240 | if (eap->addr_count > 0) |
| 6241 | { |
| 6242 | n = eap->line2; |
| 6243 | for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next) |
| 6244 | ; |
| 6245 | } |
| 6246 | |
Bram Moolenaar | 44a2f92 | 2016-03-19 22:11:51 +0100 | [diff] [blame] | 6247 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6248 | need_mouse_correct = TRUE; |
Bram Moolenaar | 44a2f92 | 2016-03-19 22:11:51 +0100 | [diff] [blame] | 6249 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6250 | n = atol((char *)eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6251 | if (cmdmod.split & WSP_VERT) |
| 6252 | { |
| 6253 | if (*eap->arg == '-' || *eap->arg == '+') |
Bram Moolenaar | 0263146 | 2017-09-22 15:20:32 +0200 | [diff] [blame] | 6254 | n += curwin->w_width; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6255 | else if (n == 0 && eap->arg[0] == NUL) // default is very wide |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6256 | n = 9999; |
| 6257 | win_setwidth_win((int)n, wp); |
| 6258 | } |
| 6259 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6260 | { |
| 6261 | if (*eap->arg == '-' || *eap->arg == '+') |
| 6262 | n += curwin->w_height; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6263 | else if (n == 0 && eap->arg[0] == NUL) // default is very high |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6264 | n = 9999; |
| 6265 | win_setheight_win((int)n, wp); |
| 6266 | } |
| 6267 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6268 | |
| 6269 | /* |
| 6270 | * ":find [+command] <file>" command. |
| 6271 | */ |
| 6272 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6273 | ex_find(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6274 | { |
| 6275 | #ifdef FEAT_SEARCHPATH |
| 6276 | char_u *fname; |
| 6277 | int count; |
| 6278 | |
| 6279 | fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS, |
| 6280 | TRUE, curbuf->b_ffname); |
| 6281 | if (eap->addr_count > 0) |
| 6282 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6283 | // Repeat finding the file "count" times. This matters when it |
| 6284 | // appears several times in the path. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6285 | count = eap->line2; |
| 6286 | while (fname != NULL && --count > 0) |
| 6287 | { |
| 6288 | vim_free(fname); |
| 6289 | fname = find_file_in_path(NULL, 0, FNAME_MESS, |
| 6290 | FALSE, curbuf->b_ffname); |
| 6291 | } |
| 6292 | } |
| 6293 | |
| 6294 | if (fname != NULL) |
| 6295 | { |
| 6296 | eap->arg = fname; |
| 6297 | #endif |
| 6298 | do_exedit(eap, NULL); |
| 6299 | #ifdef FEAT_SEARCHPATH |
| 6300 | vim_free(fname); |
| 6301 | } |
| 6302 | #endif |
| 6303 | } |
| 6304 | |
| 6305 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6306 | * ":open" simulation: for now just work like ":visual". |
| 6307 | */ |
| 6308 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6309 | ex_open(exarg_T *eap) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6310 | { |
| 6311 | regmatch_T regmatch; |
| 6312 | char_u *p; |
| 6313 | |
| 6314 | curwin->w_cursor.lnum = eap->line2; |
| 6315 | beginline(BL_SOL | BL_FIX); |
| 6316 | if (*eap->arg == '/') |
| 6317 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6318 | // ":open /pattern/": put cursor in column found with pattern |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6319 | ++eap->arg; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 6320 | p = skip_regexp(eap->arg, '/', p_magic); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6321 | *p = NUL; |
| 6322 | regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0); |
| 6323 | if (regmatch.regprog != NULL) |
| 6324 | { |
| 6325 | regmatch.rm_ic = p_ic; |
| 6326 | p = ml_get_curline(); |
| 6327 | if (vim_regexec(®match, p, (colnr_T)0)) |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 6328 | curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6329 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6330 | emsg(_(e_nomatch)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6331 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6332 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6333 | // Move to the NUL, ignore any other arguments. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6334 | eap->arg += STRLEN(eap->arg); |
| 6335 | } |
| 6336 | check_cursor(); |
| 6337 | |
| 6338 | eap->cmdidx = CMD_visual; |
| 6339 | do_exedit(eap, NULL); |
| 6340 | } |
| 6341 | |
| 6342 | /* |
| 6343 | * ":edit", ":badd", ":visual". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6344 | */ |
| 6345 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6346 | ex_edit(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | { |
| 6348 | do_exedit(eap, NULL); |
| 6349 | } |
| 6350 | |
| 6351 | /* |
Bram Moolenaar | 4b96df5 | 2020-01-26 22:00:26 +0100 | [diff] [blame] | 6352 | * ":edit <file>" command and alike. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6353 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6354 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6355 | do_exedit( |
| 6356 | exarg_T *eap, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6357 | win_T *old_curwin) // curwin before doing a split or NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6358 | { |
| 6359 | int n; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6360 | int need_hide; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6361 | int exmode_was = exmode_active; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6362 | |
Bram Moolenaar | 3c01c4a | 2020-02-01 23:04:24 +0100 | [diff] [blame] | 6363 | if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW) |
| 6364 | || ERROR_IF_TERM_POPUP_WINDOW) |
Bram Moolenaar | 815b76b | 2019-06-01 14:15:52 +0200 | [diff] [blame] | 6365 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6366 | /* |
| 6367 | * ":vi" command ends Ex mode. |
| 6368 | */ |
| 6369 | if (exmode_active && (eap->cmdidx == CMD_visual |
| 6370 | || eap->cmdidx == CMD_view)) |
| 6371 | { |
| 6372 | exmode_active = FALSE; |
Bram Moolenaar | 158ea17 | 2020-06-18 17:28:39 +0200 | [diff] [blame] | 6373 | ex_pressedreturn = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6374 | if (*eap->arg == NUL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6375 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6376 | // Special case: ":global/pat/visual\NLvi-commands" |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6377 | if (global_busy) |
| 6378 | { |
| 6379 | int rd = RedrawingDisabled; |
| 6380 | int nwr = no_wait_return; |
| 6381 | int ms = msg_scroll; |
| 6382 | #ifdef FEAT_GUI |
| 6383 | int he = hold_gui_events; |
| 6384 | #endif |
| 6385 | |
| 6386 | if (eap->nextcmd != NULL) |
| 6387 | { |
| 6388 | stuffReadbuff(eap->nextcmd); |
| 6389 | eap->nextcmd = NULL; |
| 6390 | } |
| 6391 | |
| 6392 | if (exmode_was != EXMODE_VIM) |
| 6393 | settmode(TMODE_RAW); |
| 6394 | RedrawingDisabled = 0; |
| 6395 | no_wait_return = 0; |
| 6396 | need_wait_return = FALSE; |
| 6397 | msg_scroll = 0; |
| 6398 | #ifdef FEAT_GUI |
| 6399 | hold_gui_events = 0; |
| 6400 | #endif |
| 6401 | must_redraw = CLEAR; |
Bram Moolenaar | 9e2bcb5 | 2020-02-18 21:33:00 +0100 | [diff] [blame] | 6402 | pending_exmode_active = TRUE; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6403 | |
| 6404 | main_loop(FALSE, TRUE); |
| 6405 | |
Bram Moolenaar | 9e2bcb5 | 2020-02-18 21:33:00 +0100 | [diff] [blame] | 6406 | pending_exmode_active = FALSE; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6407 | RedrawingDisabled = rd; |
| 6408 | no_wait_return = nwr; |
| 6409 | msg_scroll = ms; |
| 6410 | #ifdef FEAT_GUI |
| 6411 | hold_gui_events = he; |
| 6412 | #endif |
| 6413 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6414 | return; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6415 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
| 6418 | if ((eap->cmdidx == CMD_new |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6419 | || eap->cmdidx == CMD_tabnew |
| 6420 | || eap->cmdidx == CMD_tabedit |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6421 | || eap->cmdidx == CMD_vnew) && *eap->arg == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6422 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6423 | // ":new" or ":tabnew" without argument: edit an new empty buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6424 | setpcmark(); |
| 6425 | (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE, |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6426 | ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0), |
| 6427 | old_curwin == NULL ? curwin : NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6428 | } |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6429 | else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6430 | || *eap->arg != NUL |
| 6431 | #ifdef FEAT_BROWSE |
| 6432 | || cmdmod.browse |
| 6433 | #endif |
| 6434 | ) |
| 6435 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6436 | // Can't edit another file when "curbuf_lock" is set. Only ":edit" |
| 6437 | // can bring us here, others are stopped earlier. |
Bram Moolenaar | 5555acc | 2006-04-07 21:33:12 +0000 | [diff] [blame] | 6438 | if (*eap->arg != NUL && curbuf_locked()) |
| 6439 | return; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6440 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6441 | n = readonlymode; |
| 6442 | if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview) |
| 6443 | readonlymode = TRUE; |
| 6444 | else if (eap->cmdidx == CMD_enew) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6445 | readonlymode = FALSE; // 'readonly' doesn't make sense in an |
| 6446 | // empty buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6447 | setpcmark(); |
| 6448 | if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg), |
| 6449 | NULL, eap, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6450 | // ":edit" goes to first line if Vi compatible |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6451 | (*eap->arg == NUL && eap->do_ecmd_lnum == 0 |
| 6452 | && vim_strchr(p_cpo, CPO_GOTO1) != NULL) |
| 6453 | ? ECMD_ONE : eap->do_ecmd_lnum, |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 6454 | (buf_hide(curbuf) ? ECMD_HIDE : 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6455 | + (eap->forceit ? ECMD_FORCEIT : 0) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6456 | // after a split we can use an existing buffer |
Bram Moolenaar | 7b44934 | 2014-03-25 13:03:48 +0100 | [diff] [blame] | 6457 | + (old_curwin != NULL ? ECMD_OLDBUF : 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6458 | + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 ) |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6459 | , old_curwin == NULL ? curwin : NULL) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6460 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6461 | // Editing the file failed. If the window was split, close it. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6462 | if (old_curwin != NULL) |
| 6463 | { |
| 6464 | need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1); |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 6465 | if (!need_hide || buf_hide(curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6466 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6467 | #if defined(FEAT_EVAL) |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6468 | cleanup_T cs; |
| 6469 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6470 | // Reset the error/interrupt/exception state here so that |
| 6471 | // aborting() returns FALSE when closing a window. |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6472 | enter_cleanup(&cs); |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6473 | #endif |
| 6474 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6475 | need_mouse_correct = TRUE; |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6476 | #endif |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 6477 | win_close(curwin, !need_hide && !buf_hide(curbuf)); |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6478 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6479 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6480 | // Restore the error/interrupt/exception state if not |
| 6481 | // discarded by a new aborting error, interrupt, or |
| 6482 | // uncaught exception. |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 6483 | leave_cleanup(&cs); |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 6484 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6485 | } |
| 6486 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6487 | } |
| 6488 | else if (readonlymode && curbuf->b_nwindows == 1) |
| 6489 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6490 | // When editing an already visited buffer, 'readonly' won't be set |
| 6491 | // but the previous value is kept. With ":view" and ":sview" we |
| 6492 | // want the file to be readonly, except when another window is |
| 6493 | // editing the same buffer. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6494 | curbuf->b_p_ro = TRUE; |
| 6495 | } |
| 6496 | readonlymode = n; |
| 6497 | } |
| 6498 | else |
| 6499 | { |
| 6500 | if (eap->do_ecmd_cmd != NULL) |
| 6501 | do_cmdline_cmd(eap->do_ecmd_cmd); |
| 6502 | #ifdef FEAT_TITLE |
| 6503 | n = curwin->w_arg_idx_invalid; |
| 6504 | #endif |
| 6505 | check_arg_idx(curwin); |
| 6506 | #ifdef FEAT_TITLE |
| 6507 | if (n != curwin->w_arg_idx_invalid) |
| 6508 | maketitle(); |
| 6509 | #endif |
| 6510 | } |
| 6511 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6512 | /* |
| 6513 | * if ":split file" worked, set alternate file name in old window to new |
| 6514 | * file |
| 6515 | */ |
| 6516 | if (old_curwin != NULL |
| 6517 | && *eap->arg != NUL |
| 6518 | && curwin != old_curwin |
| 6519 | && win_valid(old_curwin) |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 6520 | && old_curwin->w_buffer != curbuf |
| 6521 | && !cmdmod.keepalt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6522 | old_curwin->w_alt_fnum = curbuf->b_fnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6523 | |
| 6524 | ex_no_reprint = TRUE; |
| 6525 | } |
| 6526 | |
| 6527 | #ifndef FEAT_GUI |
| 6528 | /* |
| 6529 | * ":gui" and ":gvim" when there is no GUI. |
| 6530 | */ |
| 6531 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6532 | ex_nogui(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6533 | { |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 6534 | eap->errmsg = _(e_nogvim); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6535 | } |
| 6536 | #endif |
| 6537 | |
Bram Moolenaar | 4f97475 | 2019-02-17 17:44:42 +0100 | [diff] [blame] | 6538 | #if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6539 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6540 | ex_tearoff(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6541 | { |
| 6542 | gui_make_tearoff(eap->arg); |
| 6543 | } |
| 6544 | #endif |
| 6545 | |
Bram Moolenaar | 29a2c08 | 2018-03-05 21:06:23 +0100 | [diff] [blame] | 6546 | #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \ |
| 6547 | || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6548 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6549 | ex_popup(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6550 | { |
Bram Moolenaar | 29a2c08 | 2018-03-05 21:06:23 +0100 | [diff] [blame] | 6551 | # if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) |
| 6552 | if (gui.in_use) |
| 6553 | gui_make_popup(eap->arg, eap->forceit); |
| 6554 | # ifdef FEAT_TERM_POPUP_MENU |
| 6555 | else |
| 6556 | # endif |
| 6557 | # endif |
| 6558 | # ifdef FEAT_TERM_POPUP_MENU |
| 6559 | pum_make_popup(eap->arg, eap->forceit); |
| 6560 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6561 | } |
| 6562 | #endif |
| 6563 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6564 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6565 | ex_swapname(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6566 | { |
| 6567 | if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL) |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 6568 | msg(_("No swap file")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6569 | else |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 6570 | msg((char *)curbuf->b_ml.ml_mfp->mf_fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6571 | } |
| 6572 | |
| 6573 | /* |
| 6574 | * ":syncbind" forces all 'scrollbind' windows to have the same relative |
| 6575 | * offset. |
| 6576 | * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>) |
| 6577 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6578 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6579 | ex_syncbind(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6580 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6581 | win_T *wp; |
Bram Moolenaar | dedd1b0 | 2013-12-14 13:06:17 +0100 | [diff] [blame] | 6582 | win_T *save_curwin = curwin; |
| 6583 | buf_T *save_curbuf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6584 | long topline; |
| 6585 | long y; |
| 6586 | linenr_T old_linenr = curwin->w_cursor.lnum; |
| 6587 | |
| 6588 | setpcmark(); |
| 6589 | |
| 6590 | /* |
| 6591 | * determine max topline |
| 6592 | */ |
| 6593 | if (curwin->w_p_scb) |
| 6594 | { |
| 6595 | topline = curwin->w_topline; |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 6596 | FOR_ALL_WINDOWS(wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6597 | { |
| 6598 | if (wp->w_p_scb && wp->w_buffer) |
| 6599 | { |
Bram Moolenaar | 375e339 | 2019-01-31 18:26:10 +0100 | [diff] [blame] | 6600 | y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6601 | if (topline > y) |
| 6602 | topline = y; |
| 6603 | } |
| 6604 | } |
| 6605 | if (topline < 1) |
| 6606 | topline = 1; |
| 6607 | } |
| 6608 | else |
| 6609 | { |
| 6610 | topline = 1; |
| 6611 | } |
| 6612 | |
| 6613 | |
| 6614 | /* |
Bram Moolenaar | dedd1b0 | 2013-12-14 13:06:17 +0100 | [diff] [blame] | 6615 | * Set all scrollbind windows to the same topline. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6616 | */ |
Bram Moolenaar | 2932359 | 2016-07-24 22:04:11 +0200 | [diff] [blame] | 6617 | FOR_ALL_WINDOWS(curwin) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6618 | { |
| 6619 | if (curwin->w_p_scb) |
| 6620 | { |
Bram Moolenaar | dedd1b0 | 2013-12-14 13:06:17 +0100 | [diff] [blame] | 6621 | curbuf = curwin->w_buffer; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6622 | y = topline - curwin->w_topline; |
| 6623 | if (y > 0) |
| 6624 | scrollup(y, TRUE); |
| 6625 | else |
| 6626 | scrolldown(-y, TRUE); |
| 6627 | curwin->w_scbind_pos = topline; |
| 6628 | redraw_later(VALID); |
| 6629 | cursor_correct(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6630 | curwin->w_redr_status = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6631 | } |
| 6632 | } |
Bram Moolenaar | dedd1b0 | 2013-12-14 13:06:17 +0100 | [diff] [blame] | 6633 | curwin = save_curwin; |
| 6634 | curbuf = save_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6635 | if (curwin->w_p_scb) |
| 6636 | { |
| 6637 | did_syncbind = TRUE; |
| 6638 | checkpcmark(); |
| 6639 | if (old_linenr != curwin->w_cursor.lnum) |
| 6640 | { |
| 6641 | char_u ctrl_o[2]; |
| 6642 | |
| 6643 | ctrl_o[0] = Ctrl_O; |
| 6644 | ctrl_o[1] = 0; |
| 6645 | ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE); |
| 6646 | } |
| 6647 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | |
| 6651 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6652 | ex_read(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6653 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6654 | int i; |
| 6655 | int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
| 6656 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6657 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6658 | if (eap->usefilter) // :r!cmd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6659 | do_bang(1, eap, FALSE, FALSE, TRUE); |
| 6660 | else |
| 6661 | { |
| 6662 | if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL) |
| 6663 | return; |
| 6664 | |
| 6665 | #ifdef FEAT_BROWSE |
| 6666 | if (cmdmod.browse) |
| 6667 | { |
| 6668 | char_u *browseFile; |
| 6669 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 6670 | browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6671 | NULL, NULL, NULL, curbuf); |
| 6672 | if (browseFile != NULL) |
| 6673 | { |
| 6674 | i = readfile(browseFile, NULL, |
| 6675 | eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); |
| 6676 | vim_free(browseFile); |
| 6677 | } |
| 6678 | else |
| 6679 | i = OK; |
| 6680 | } |
| 6681 | else |
| 6682 | #endif |
| 6683 | if (*eap->arg == NUL) |
| 6684 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6685 | if (check_fname() == FAIL) // check for no file name |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6686 | return; |
| 6687 | i = readfile(curbuf->b_ffname, curbuf->b_fname, |
| 6688 | eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); |
| 6689 | } |
| 6690 | else |
| 6691 | { |
| 6692 | if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL) |
| 6693 | (void)setaltfname(eap->arg, eap->arg, (linenr_T)1); |
| 6694 | i = readfile(eap->arg, NULL, |
| 6695 | eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); |
| 6696 | |
| 6697 | } |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 6698 | if (i != OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6699 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6700 | #if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6701 | if (!aborting()) |
| 6702 | #endif |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6703 | semsg(_(e_notopen), eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6704 | } |
| 6705 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6706 | { |
| 6707 | if (empty && exmode_active) |
| 6708 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6709 | // Delete the empty line that remains. Historically ex does |
| 6710 | // this but vi doesn't. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6711 | if (eap->line2 == 0) |
| 6712 | lnum = curbuf->b_ml.ml_line_count; |
| 6713 | else |
| 6714 | lnum = 1; |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 6715 | if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6716 | { |
Bram Moolenaar | ca70c07 | 2020-05-30 20:30:46 +0200 | [diff] [blame] | 6717 | ml_delete(lnum); |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 6718 | if (curwin->w_cursor.lnum > 1 |
| 6719 | && curwin->w_cursor.lnum >= lnum) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6720 | --curwin->w_cursor.lnum; |
Bram Moolenaar | cdcaa58 | 2009-07-09 18:06:49 +0000 | [diff] [blame] | 6721 | deleted_lines_mark(lnum, 1L); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6722 | } |
| 6723 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6724 | redraw_curbuf_later(VALID); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6725 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6726 | } |
| 6727 | } |
| 6728 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6729 | static char_u *prev_dir = NULL; |
| 6730 | |
| 6731 | #if defined(EXITFREE) || defined(PROTO) |
| 6732 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6733 | free_cd_dir(void) |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6734 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6735 | VIM_CLEAR(prev_dir); |
| 6736 | VIM_CLEAR(globaldir); |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6737 | } |
| 6738 | #endif |
| 6739 | |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6740 | /* |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6741 | * Get the previous directory for the given chdir scope. |
| 6742 | */ |
| 6743 | static char_u * |
| 6744 | get_prevdir(cdscope_T scope) |
| 6745 | { |
| 6746 | if (scope == CDSCOPE_WINDOW) |
| 6747 | return curwin->w_prevdir; |
| 6748 | else if (scope == CDSCOPE_TABPAGE) |
| 6749 | return curtab->tp_prevdir; |
| 6750 | return prev_dir; |
| 6751 | } |
| 6752 | |
| 6753 | /* |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6754 | * Deal with the side effects of changing the current directory. |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6755 | * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command. |
| 6756 | * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command. |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6757 | */ |
| 6758 | void |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6759 | post_chdir(cdscope_T scope) |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6760 | { |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6761 | if (scope != CDSCOPE_WINDOW) |
Bram Moolenaar | 00aa069 | 2019-04-27 20:37:57 +0200 | [diff] [blame] | 6762 | // Clear tab local directory for both :cd and :tcd |
| 6763 | VIM_CLEAR(curtab->tp_localdir); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6764 | VIM_CLEAR(curwin->w_localdir); |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6765 | if (scope != CDSCOPE_GLOBAL) |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6766 | { |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6767 | char_u *pdir = get_prevdir(scope); |
| 6768 | |
| 6769 | // If still in the global directory, need to remember current |
| 6770 | // directory as the global directory. |
| 6771 | if (globaldir == NULL && pdir != NULL) |
| 6772 | globaldir = vim_strsave(pdir); |
| 6773 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 6774 | // Remember this local directory for the window. |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6775 | if (mch_dirname(NameBuff, MAXPATHL) == OK) |
Bram Moolenaar | 00aa069 | 2019-04-27 20:37:57 +0200 | [diff] [blame] | 6776 | { |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6777 | if (scope == CDSCOPE_TABPAGE) |
Bram Moolenaar | 00aa069 | 2019-04-27 20:37:57 +0200 | [diff] [blame] | 6778 | curtab->tp_localdir = vim_strsave(NameBuff); |
| 6779 | else |
| 6780 | curwin->w_localdir = vim_strsave(NameBuff); |
| 6781 | } |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6782 | } |
| 6783 | else |
| 6784 | { |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6785 | // We are now in the global directory, no need to remember its name. |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6786 | VIM_CLEAR(globaldir); |
Bram Moolenaar | f425830 | 2013-06-02 18:20:17 +0200 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | shorten_fnames(TRUE); |
| 6790 | } |
| 6791 | |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6792 | /* |
| 6793 | * Change directory function used by :cd/:tcd/:lcd Ex commands and the |
Bram Moolenaar | 9505872 | 2020-06-01 16:26:19 +0200 | [diff] [blame] | 6794 | * chdir() function. |
| 6795 | * scope == CDSCOPE_WINDOW: changes the window-local directory |
| 6796 | * scope == CDSCOPE_TABPAGE: changes the tab-local directory |
| 6797 | * Otherwise: changes the global directory |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6798 | * Returns TRUE if the directory is successfully changed. |
| 6799 | */ |
| 6800 | int |
| 6801 | changedir_func( |
| 6802 | char_u *new_dir, |
| 6803 | int forceit, |
| 6804 | cdscope_T scope) |
| 6805 | { |
| 6806 | char_u *tofree; |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6807 | char_u *pdir = NULL; |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6808 | int dir_differs; |
| 6809 | int retval = FALSE; |
| 6810 | |
Bram Moolenaar | 7cc9692 | 2020-01-31 22:41:38 +0100 | [diff] [blame] | 6811 | if (new_dir == NULL || allbuf_locked()) |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6812 | return FALSE; |
| 6813 | |
| 6814 | if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit) |
| 6815 | { |
| 6816 | emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)")); |
| 6817 | return FALSE; |
| 6818 | } |
| 6819 | |
| 6820 | // ":cd -": Change to previous directory |
| 6821 | if (STRCMP(new_dir, "-") == 0) |
| 6822 | { |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6823 | pdir = get_prevdir(scope); |
| 6824 | if (pdir == NULL) |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6825 | { |
| 6826 | emsg(_("E186: No previous directory")); |
| 6827 | return FALSE; |
| 6828 | } |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6829 | new_dir = pdir; |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6830 | } |
| 6831 | |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6832 | // Free the previous directory |
| 6833 | tofree = get_prevdir(scope); |
| 6834 | |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6835 | // Save current directory for next ":cd -" |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6836 | if (mch_dirname(NameBuff, MAXPATHL) == OK) |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6837 | pdir = vim_strsave(NameBuff); |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6838 | else |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6839 | pdir = NULL; |
| 6840 | if (scope == CDSCOPE_WINDOW) |
| 6841 | curwin->w_prevdir = pdir; |
| 6842 | else if (scope == CDSCOPE_TABPAGE) |
| 6843 | curtab->tp_prevdir = pdir; |
| 6844 | else |
| 6845 | prev_dir = pdir; |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6846 | |
| 6847 | #if defined(UNIX) || defined(VMS) |
| 6848 | // for UNIX ":cd" means: go to home directory |
| 6849 | if (*new_dir == NUL) |
| 6850 | { |
| 6851 | // use NameBuff for home directory name |
| 6852 | # ifdef VMS |
| 6853 | char_u *p; |
| 6854 | |
| 6855 | p = mch_getenv((char_u *)"SYS$LOGIN"); |
| 6856 | if (p == NULL || *p == NUL) // empty is the same as not set |
| 6857 | NameBuff[0] = NUL; |
| 6858 | else |
| 6859 | vim_strncpy(NameBuff, p, MAXPATHL - 1); |
| 6860 | # else |
| 6861 | expand_env((char_u *)"$HOME", NameBuff, MAXPATHL); |
| 6862 | # endif |
| 6863 | new_dir = NameBuff; |
| 6864 | } |
| 6865 | #endif |
Bram Moolenaar | 002bc79 | 2020-06-05 22:33:42 +0200 | [diff] [blame] | 6866 | dir_differs = new_dir == NULL || pdir == NULL |
| 6867 | || pathcmp((char *)pdir, (char *)new_dir, -1) != 0; |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6868 | if (new_dir == NULL || (dir_differs && vim_chdir(new_dir))) |
| 6869 | emsg(_(e_failed)); |
| 6870 | else |
| 6871 | { |
| 6872 | char_u *acmd_fname; |
| 6873 | |
| 6874 | post_chdir(scope); |
| 6875 | |
| 6876 | if (dir_differs) |
| 6877 | { |
| 6878 | if (scope == CDSCOPE_WINDOW) |
| 6879 | acmd_fname = (char_u *)"window"; |
| 6880 | else if (scope == CDSCOPE_TABPAGE) |
| 6881 | acmd_fname = (char_u *)"tabpage"; |
| 6882 | else |
| 6883 | acmd_fname = (char_u *)"global"; |
| 6884 | apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE, |
| 6885 | curbuf); |
| 6886 | } |
| 6887 | retval = TRUE; |
| 6888 | } |
| 6889 | vim_free(tofree); |
| 6890 | |
| 6891 | return retval; |
| 6892 | } |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 6893 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6894 | /* |
Bram Moolenaar | 00aa069 | 2019-04-27 20:37:57 +0200 | [diff] [blame] | 6895 | * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6896 | */ |
Bram Moolenaar | d089d9b | 2007-09-30 12:02:55 +0000 | [diff] [blame] | 6897 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6898 | ex_cd(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6899 | { |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 6900 | char_u *new_dir; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6901 | |
| 6902 | new_dir = eap->arg; |
| 6903 | #if !defined(UNIX) && !defined(VMS) |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6904 | // for non-UNIX ":cd" means: print current directory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6905 | if (*new_dir == NUL) |
| 6906 | ex_pwd(NULL); |
| 6907 | else |
| 6908 | #endif |
| 6909 | { |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6910 | cdscope_T scope = CDSCOPE_GLOBAL; |
| 6911 | |
| 6912 | if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir) |
| 6913 | scope = CDSCOPE_WINDOW; |
| 6914 | else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir) |
| 6915 | scope = CDSCOPE_TABPAGE; |
| 6916 | |
| 6917 | if (changedir_func(new_dir, eap->forceit, scope)) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6918 | { |
Bram Moolenaar | 1063f3d | 2019-05-07 22:06:52 +0200 | [diff] [blame] | 6919 | // Echo the new current directory if the command was typed. |
Bram Moolenaar | fcfbc67 | 2009-07-09 18:13:49 +0000 | [diff] [blame] | 6920 | if (KeyTyped || p_verbose >= 5) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | ex_pwd(eap); |
| 6922 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6923 | } |
| 6924 | } |
| 6925 | |
| 6926 | /* |
| 6927 | * ":pwd". |
| 6928 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6929 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6930 | ex_pwd(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6931 | { |
| 6932 | if (mch_dirname(NameBuff, MAXPATHL) == OK) |
| 6933 | { |
| 6934 | #ifdef BACKSLASH_IN_FILENAME |
| 6935 | slash_adjust(NameBuff); |
| 6936 | #endif |
Bram Moolenaar | 9505872 | 2020-06-01 16:26:19 +0200 | [diff] [blame] | 6937 | if (p_verbose > 0) |
| 6938 | { |
| 6939 | char *context = "global"; |
| 6940 | |
| 6941 | if (curwin->w_localdir != NULL) |
| 6942 | context = "window"; |
| 6943 | else if (curtab->tp_localdir != NULL) |
| 6944 | context = "tabpage"; |
| 6945 | smsg("[%s] %s", context, (char *)NameBuff); |
| 6946 | } |
| 6947 | else |
| 6948 | msg((char *)NameBuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6949 | } |
| 6950 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6951 | emsg(_("E187: Unknown")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6952 | } |
| 6953 | |
| 6954 | /* |
| 6955 | * ":=". |
| 6956 | */ |
| 6957 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6958 | ex_equal(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6959 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6960 | smsg("%ld", (long)eap->line2); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6961 | ex_may_print(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6965 | ex_sleep(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6966 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6967 | int n; |
| 6968 | long len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6969 | |
| 6970 | if (cursor_valid()) |
| 6971 | { |
| 6972 | n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled; |
| 6973 | if (n >= 0) |
Bram Moolenaar | 53f8174 | 2017-09-22 14:35:51 +0200 | [diff] [blame] | 6974 | windgoto((int)n, curwin->w_wincol + curwin->w_wcol); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6975 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6976 | |
| 6977 | len = eap->line2; |
| 6978 | switch (*eap->arg) |
| 6979 | { |
| 6980 | case 'm': break; |
| 6981 | case NUL: len *= 1000L; break; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 6982 | default: semsg(_(e_invarg2), eap->arg); return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 6983 | } |
| 6984 | do_sleep(len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | /* |
| 6988 | * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second. |
| 6989 | */ |
| 6990 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6991 | do_sleep(long msec) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6992 | { |
Bram Moolenaar | 5295319 | 2019-08-16 10:27:13 +0200 | [diff] [blame] | 6993 | long done = 0; |
Bram Moolenaar | 975b527 | 2016-03-15 23:10:59 +0100 | [diff] [blame] | 6994 | long wait_now; |
Bram Moolenaar | 5295319 | 2019-08-16 10:27:13 +0200 | [diff] [blame] | 6995 | # ifdef ELAPSED_FUNC |
| 6996 | elapsed_T start_tv; |
| 6997 | |
| 6998 | // Remember at what time we started, so that we know how much longer we |
| 6999 | // should wait after waiting for a bit. |
| 7000 | ELAPSED_INIT(start_tv); |
| 7001 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7002 | |
| 7003 | cursor_on(); |
Bram Moolenaar | f45d747 | 2018-09-30 18:22:26 +0200 | [diff] [blame] | 7004 | out_flush_cursor(FALSE, FALSE); |
Bram Moolenaar | 5295319 | 2019-08-16 10:27:13 +0200 | [diff] [blame] | 7005 | while (!got_int && done < msec) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7006 | { |
Bram Moolenaar | 975b527 | 2016-03-15 23:10:59 +0100 | [diff] [blame] | 7007 | wait_now = msec - done > 1000L ? 1000L : msec - done; |
| 7008 | #ifdef FEAT_TIMERS |
| 7009 | { |
| 7010 | long due_time = check_due_timer(); |
| 7011 | |
| 7012 | if (due_time > 0 && due_time < wait_now) |
| 7013 | wait_now = due_time; |
| 7014 | } |
| 7015 | #endif |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 7016 | #ifdef FEAT_JOB_CHANNEL |
Bram Moolenaar | 28e67e0 | 2019-08-15 23:05:49 +0200 | [diff] [blame] | 7017 | if (has_any_channel() && wait_now > 20L) |
| 7018 | wait_now = 20L; |
| 7019 | #endif |
| 7020 | #ifdef FEAT_SOUND |
| 7021 | if (has_any_sound_callback() && wait_now > 20L) |
| 7022 | wait_now = 20L; |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 7023 | #endif |
Bram Moolenaar | 975b527 | 2016-03-15 23:10:59 +0100 | [diff] [blame] | 7024 | ui_delay(wait_now, TRUE); |
Bram Moolenaar | 5295319 | 2019-08-16 10:27:13 +0200 | [diff] [blame] | 7025 | |
Bram Moolenaar | b9c31e7 | 2016-09-29 15:18:57 +0200 | [diff] [blame] | 7026 | #ifdef FEAT_JOB_CHANNEL |
| 7027 | if (has_any_channel()) |
| 7028 | ui_breakcheck_force(TRUE); |
| 7029 | else |
| 7030 | #endif |
| 7031 | ui_breakcheck(); |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7032 | #ifdef MESSAGE_QUEUE |
Bram Moolenaar | 5295319 | 2019-08-16 10:27:13 +0200 | [diff] [blame] | 7033 | // Process the netbeans and clientserver messages that may have been |
| 7034 | // received in the call to ui_breakcheck() when the GUI is in use. This |
| 7035 | // may occur when running a test case. |
Bram Moolenaar | 93c88e0 | 2015-09-15 14:12:05 +0200 | [diff] [blame] | 7036 | parse_queued_messages(); |
Bram Moolenaar | e3cc6d4 | 2011-10-20 21:58:34 +0200 | [diff] [blame] | 7037 | #endif |
Bram Moolenaar | 5295319 | 2019-08-16 10:27:13 +0200 | [diff] [blame] | 7038 | |
| 7039 | # ifdef ELAPSED_FUNC |
| 7040 | // actual time passed |
| 7041 | done = ELAPSED_FUNC(start_tv); |
| 7042 | # else |
| 7043 | // guestimate time passed (will actually be more) |
| 7044 | done += wait_now; |
| 7045 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7046 | } |
Bram Moolenaar | 1ebff3d | 2018-07-07 16:18:13 +0200 | [diff] [blame] | 7047 | |
| 7048 | // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the |
| 7049 | // input buffer, otherwise a following call to input() fails. |
| 7050 | if (got_int) |
| 7051 | (void)vpeekc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7052 | } |
| 7053 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7054 | /* |
| 7055 | * ":winsize" command (obsolete). |
| 7056 | */ |
| 7057 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7058 | ex_winsize(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7059 | { |
| 7060 | int w, h; |
| 7061 | char_u *arg = eap->arg; |
| 7062 | char_u *p; |
| 7063 | |
| 7064 | w = getdigits(&arg); |
| 7065 | arg = skipwhite(arg); |
| 7066 | p = arg; |
| 7067 | h = getdigits(&arg); |
| 7068 | if (*p != NUL && *arg == NUL) |
| 7069 | set_shellsize(w, h, TRUE); |
| 7070 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7071 | emsg(_("E465: :winsize requires two number arguments")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7072 | } |
| 7073 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7074 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7075 | ex_wincmd(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7076 | { |
| 7077 | int xchar = NUL; |
| 7078 | char_u *p; |
| 7079 | |
| 7080 | if (*eap->arg == 'g' || *eap->arg == Ctrl_G) |
| 7081 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7082 | // CTRL-W g and CTRL-W CTRL-G have an extra command character |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7083 | if (eap->arg[1] == NUL) |
| 7084 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7085 | emsg(_(e_invarg)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7086 | return; |
| 7087 | } |
| 7088 | xchar = eap->arg[1]; |
| 7089 | p = eap->arg + 2; |
| 7090 | } |
| 7091 | else |
| 7092 | p = eap->arg + 1; |
| 7093 | |
| 7094 | eap->nextcmd = check_nextcmd(p); |
| 7095 | p = skipwhite(p); |
Bram Moolenaar | f5be8cd | 2020-07-17 20:36:00 +0200 | [diff] [blame] | 7096 | if (*p != NUL && *p != ( |
| 7097 | #ifdef FEAT_EVAL |
| 7098 | in_vim9script() ? '#' : |
| 7099 | #endif |
| 7100 | '"') |
| 7101 | && eap->nextcmd == NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7102 | emsg(_(e_invarg)); |
Bram Moolenaar | 12bde49 | 2011-06-13 01:19:56 +0200 | [diff] [blame] | 7103 | else if (!eap->skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7104 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7105 | // Pass flags on for ":vertical wincmd ]". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7106 | postponed_split_flags = cmdmod.split; |
Bram Moolenaar | d326ce8 | 2007-03-11 14:48:29 +0000 | [diff] [blame] | 7107 | postponed_split_tab = cmdmod.tab; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7108 | do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar); |
| 7109 | postponed_split_flags = 0; |
Bram Moolenaar | d326ce8 | 2007-03-11 14:48:29 +0000 | [diff] [blame] | 7110 | postponed_split_tab = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7111 | } |
| 7112 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7113 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7114 | #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | /* |
| 7116 | * ":winpos". |
| 7117 | */ |
| 7118 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7119 | ex_winpos(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7120 | { |
| 7121 | int x, y; |
| 7122 | char_u *arg = eap->arg; |
| 7123 | char_u *p; |
| 7124 | |
| 7125 | if (*arg == NUL) |
| 7126 | { |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7127 | # if defined(FEAT_GUI) || defined(MSWIN) |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 7128 | # ifdef VIMDLL |
| 7129 | if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL : |
| 7130 | mch_get_winpos(&x, &y) != FAIL) |
| 7131 | # elif defined(FEAT_GUI) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7132 | if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7133 | # else |
| 7134 | if (mch_get_winpos(&x, &y) != FAIL) |
| 7135 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7136 | { |
| 7137 | sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y); |
Bram Moolenaar | 32526b3 | 2019-01-19 17:43:09 +0100 | [diff] [blame] | 7138 | msg((char *)IObuff); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7139 | } |
| 7140 | else |
| 7141 | # endif |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7142 | emsg(_("E188: Obtaining window position not implemented for this platform")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7143 | } |
| 7144 | else |
| 7145 | { |
| 7146 | x = getdigits(&arg); |
| 7147 | arg = skipwhite(arg); |
| 7148 | p = arg; |
| 7149 | y = getdigits(&arg); |
| 7150 | if (*p == NUL || *arg != NUL) |
| 7151 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7152 | emsg(_("E466: :winpos requires two number arguments")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7153 | return; |
| 7154 | } |
| 7155 | # ifdef FEAT_GUI |
| 7156 | if (gui.in_use) |
| 7157 | gui_mch_set_winpos(x, y); |
| 7158 | else if (gui.starting) |
| 7159 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7160 | // Remember the coordinates for when the window is opened. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7161 | gui_win_x = x; |
| 7162 | gui_win_y = y; |
| 7163 | } |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 7164 | # if defined(HAVE_TGETENT) || defined(VIMDLL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7165 | else |
| 7166 | # endif |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 7167 | # endif |
| 7168 | # if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL)) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7169 | mch_set_winpos(x, y); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7170 | # endif |
| 7171 | # ifdef HAVE_TGETENT |
| 7172 | if (*T_CWP) |
| 7173 | term_set_winpos(x, y); |
| 7174 | # endif |
| 7175 | } |
| 7176 | } |
| 7177 | #endif |
| 7178 | |
| 7179 | /* |
| 7180 | * Handle command that work like operators: ":delete", ":yank", ":>" and ":<". |
| 7181 | */ |
| 7182 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7183 | ex_operators(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7184 | { |
| 7185 | oparg_T oa; |
| 7186 | |
| 7187 | clear_oparg(&oa); |
| 7188 | oa.regname = eap->regname; |
| 7189 | oa.start.lnum = eap->line1; |
| 7190 | oa.end.lnum = eap->line2; |
| 7191 | oa.line_count = eap->line2 - eap->line1 + 1; |
| 7192 | oa.motion_type = MLINE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7193 | virtual_op = FALSE; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7194 | if (eap->cmdidx != CMD_yank) // position cursor for undo |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7195 | { |
| 7196 | setpcmark(); |
| 7197 | curwin->w_cursor.lnum = eap->line1; |
| 7198 | beginline(BL_SOL | BL_FIX); |
| 7199 | } |
| 7200 | |
Bram Moolenaar | d07c6e1 | 2013-11-21 14:21:40 +0100 | [diff] [blame] | 7201 | if (VIsual_active) |
| 7202 | end_visual_mode(); |
Bram Moolenaar | d07c6e1 | 2013-11-21 14:21:40 +0100 | [diff] [blame] | 7203 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7204 | switch (eap->cmdidx) |
| 7205 | { |
| 7206 | case CMD_delete: |
| 7207 | oa.op_type = OP_DELETE; |
| 7208 | op_delete(&oa); |
| 7209 | break; |
| 7210 | |
| 7211 | case CMD_yank: |
| 7212 | oa.op_type = OP_YANK; |
| 7213 | (void)op_yank(&oa, FALSE, TRUE); |
| 7214 | break; |
| 7215 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7216 | default: // CMD_rshift or CMD_lshift |
Bram Moolenaar | 6e70736 | 2013-06-14 19:15:58 +0200 | [diff] [blame] | 7217 | if ( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7218 | #ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 6e70736 | 2013-06-14 19:15:58 +0200 | [diff] [blame] | 7219 | (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl |
| 7220 | #else |
| 7221 | eap->cmdidx == CMD_rshift |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7222 | #endif |
Bram Moolenaar | 6e70736 | 2013-06-14 19:15:58 +0200 | [diff] [blame] | 7223 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7224 | oa.op_type = OP_RSHIFT; |
| 7225 | else |
| 7226 | oa.op_type = OP_LSHIFT; |
| 7227 | op_shift(&oa, FALSE, eap->amount); |
| 7228 | break; |
| 7229 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7230 | virtual_op = MAYBE; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7231 | ex_may_print(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7232 | } |
| 7233 | |
| 7234 | /* |
| 7235 | * ":put". |
| 7236 | */ |
| 7237 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7238 | ex_put(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7239 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7240 | // ":0put" works like ":1put!". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7241 | if (eap->line2 == 0) |
| 7242 | { |
| 7243 | eap->line2 = 1; |
| 7244 | eap->forceit = TRUE; |
| 7245 | } |
| 7246 | curwin->w_cursor.lnum = eap->line2; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7247 | do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L, |
| 7248 | PUT_LINE|PUT_CURSLINE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7249 | } |
| 7250 | |
| 7251 | /* |
| 7252 | * Handle ":copy" and ":move". |
| 7253 | */ |
| 7254 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7255 | ex_copymove(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7256 | { |
| 7257 | long n; |
| 7258 | |
Bram Moolenaar | 50eb16c | 2018-09-15 15:42:40 +0200 | [diff] [blame] | 7259 | n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7260 | if (eap->arg == NULL) // error detected |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7261 | { |
| 7262 | eap->nextcmd = NULL; |
| 7263 | return; |
| 7264 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7265 | get_flags(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7266 | |
| 7267 | /* |
| 7268 | * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n' |
| 7269 | */ |
| 7270 | if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count) |
| 7271 | { |
Bram Moolenaar | 0acae7a | 2019-08-06 21:29:29 +0200 | [diff] [blame] | 7272 | emsg(_(e_invrange)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7273 | return; |
| 7274 | } |
| 7275 | |
| 7276 | if (eap->cmdidx == CMD_move) |
| 7277 | { |
| 7278 | if (do_move(eap->line1, eap->line2, n) == FAIL) |
| 7279 | return; |
| 7280 | } |
| 7281 | else |
| 7282 | ex_copy(eap->line1, eap->line2, n); |
| 7283 | u_clearline(); |
| 7284 | beginline(BL_SOL | BL_FIX); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7285 | ex_may_print(eap); |
| 7286 | } |
| 7287 | |
| 7288 | /* |
| 7289 | * Print the current line if flags were given to the Ex command. |
| 7290 | */ |
Bram Moolenaar | fd3fe98 | 2014-04-01 17:49:44 +0200 | [diff] [blame] | 7291 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7292 | ex_may_print(exarg_T *eap) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7293 | { |
| 7294 | if (eap->flags != 0) |
| 7295 | { |
| 7296 | print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR), |
| 7297 | (eap->flags & EXFLAG_LIST)); |
| 7298 | ex_no_reprint = TRUE; |
| 7299 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7300 | } |
| 7301 | |
| 7302 | /* |
| 7303 | * ":smagic" and ":snomagic". |
| 7304 | */ |
| 7305 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7306 | ex_submagic(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7307 | { |
| 7308 | int magic_save = p_magic; |
| 7309 | |
| 7310 | p_magic = (eap->cmdidx == CMD_smagic); |
| 7311 | do_sub(eap); |
| 7312 | p_magic = magic_save; |
| 7313 | } |
| 7314 | |
| 7315 | /* |
| 7316 | * ":join". |
| 7317 | */ |
| 7318 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7319 | ex_join(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7320 | { |
| 7321 | curwin->w_cursor.lnum = eap->line1; |
| 7322 | if (eap->line1 == eap->line2) |
| 7323 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7324 | if (eap->addr_count >= 2) // :2,2join does nothing |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7325 | return; |
| 7326 | if (eap->line2 == curbuf->b_ml.ml_line_count) |
| 7327 | { |
| 7328 | beep_flush(); |
| 7329 | return; |
| 7330 | } |
| 7331 | ++eap->line2; |
| 7332 | } |
Bram Moolenaar | d69bd9a | 2014-04-29 12:15:40 +0200 | [diff] [blame] | 7333 | (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7334 | beginline(BL_WHITE | BL_FIX); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7335 | ex_may_print(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7336 | } |
| 7337 | |
| 7338 | /* |
| 7339 | * ":[addr]@r" or ":[addr]*r": execute register |
| 7340 | */ |
| 7341 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7342 | ex_at(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7343 | { |
| 7344 | int c; |
Bram Moolenaar | 6046287 | 2009-11-03 11:40:19 +0000 | [diff] [blame] | 7345 | int prev_len = typebuf.tb_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7346 | |
| 7347 | curwin->w_cursor.lnum = eap->line2; |
Bram Moolenaar | 4930a76 | 2016-09-11 14:39:53 +0200 | [diff] [blame] | 7348 | check_cursor_col(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7349 | |
| 7350 | #ifdef USE_ON_FLY_SCROLL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7351 | dont_scroll = TRUE; // disallow scrolling here |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7352 | #endif |
| 7353 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7354 | // get the register name. No name means to use the previous one |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7355 | c = *eap->arg; |
| 7356 | if (c == NUL || (c == '*' && *eap->cmd == '*')) |
| 7357 | c = '@'; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7358 | // Put the register in the typeahead buffer with the "silent" flag. |
Bram Moolenaar | d333d1e | 2006-11-07 17:43:47 +0000 | [diff] [blame] | 7359 | if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE) |
| 7360 | == FAIL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7361 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7362 | beep_flush(); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7363 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7364 | else |
| 7365 | { |
| 7366 | int save_efr = exec_from_reg; |
| 7367 | |
| 7368 | exec_from_reg = TRUE; |
| 7369 | |
| 7370 | /* |
| 7371 | * Execute from the typeahead buffer. |
Bram Moolenaar | 6046287 | 2009-11-03 11:40:19 +0000 | [diff] [blame] | 7372 | * Continue until the stuff buffer is empty and all added characters |
| 7373 | * have been consumed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7374 | */ |
Bram Moolenaar | 6046287 | 2009-11-03 11:40:19 +0000 | [diff] [blame] | 7375 | while (!stuff_empty() || typebuf.tb_len > prev_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7376 | (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE); |
| 7377 | |
| 7378 | exec_from_reg = save_efr; |
| 7379 | } |
| 7380 | } |
| 7381 | |
| 7382 | /* |
| 7383 | * ":!". |
| 7384 | */ |
| 7385 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7386 | ex_bang(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7387 | { |
| 7388 | do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE); |
| 7389 | } |
| 7390 | |
| 7391 | /* |
| 7392 | * ":undo". |
| 7393 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7394 | static void |
Bram Moolenaar | f1d2501 | 2016-03-03 12:22:53 +0100 | [diff] [blame] | 7395 | ex_undo(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7396 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7397 | if (eap->addr_count == 1) // :undo 123 |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 7398 | undo_time(eap->line2, FALSE, FALSE, TRUE); |
Bram Moolenaar | d3667a2 | 2006-03-16 21:35:52 +0000 | [diff] [blame] | 7399 | else |
| 7400 | u_undo(1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7401 | } |
| 7402 | |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 7403 | #ifdef FEAT_PERSISTENT_UNDO |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 7404 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7405 | ex_wundo(exarg_T *eap) |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 7406 | { |
| 7407 | char_u hash[UNDO_HASH_SIZE]; |
| 7408 | |
| 7409 | u_compute_hash(hash); |
| 7410 | u_write_undo(eap->arg, eap->forceit, curbuf, hash); |
| 7411 | } |
| 7412 | |
Bram Moolenaar | 6df6f47 | 2010-07-18 18:04:50 +0200 | [diff] [blame] | 7413 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7414 | ex_rundo(exarg_T *eap) |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 7415 | { |
| 7416 | char_u hash[UNDO_HASH_SIZE]; |
| 7417 | |
| 7418 | u_compute_hash(hash); |
Bram Moolenaar | 6ed8ed8 | 2010-05-30 20:40:11 +0200 | [diff] [blame] | 7419 | u_read_undo(eap->arg, hash, NULL); |
Bram Moolenaar | 55debbe | 2010-05-23 23:34:36 +0200 | [diff] [blame] | 7420 | } |
| 7421 | #endif |
| 7422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7423 | /* |
| 7424 | * ":redo". |
| 7425 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7426 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7427 | ex_redo(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7428 | { |
| 7429 | u_redo(1); |
| 7430 | } |
| 7431 | |
| 7432 | /* |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7433 | * ":earlier" and ":later". |
| 7434 | */ |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7435 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7436 | ex_later(exarg_T *eap) |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7437 | { |
| 7438 | long count = 0; |
| 7439 | int sec = FALSE; |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 7440 | int file = FALSE; |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7441 | char_u *p = eap->arg; |
| 7442 | |
| 7443 | if (*p == NUL) |
| 7444 | count = 1; |
| 7445 | else if (isdigit(*p)) |
| 7446 | { |
| 7447 | count = getdigits(&p); |
| 7448 | switch (*p) |
| 7449 | { |
| 7450 | case 's': ++p; sec = TRUE; break; |
| 7451 | case 'm': ++p; sec = TRUE; count *= 60; break; |
| 7452 | case 'h': ++p; sec = TRUE; count *= 60 * 60; break; |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 7453 | case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break; |
| 7454 | case 'f': ++p; file = TRUE; break; |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7455 | } |
| 7456 | } |
| 7457 | |
| 7458 | if (*p != NUL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7459 | semsg(_(e_invarg2), eap->arg); |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7460 | else |
Bram Moolenaar | 730cde9 | 2010-06-27 05:18:54 +0200 | [diff] [blame] | 7461 | undo_time(eap->cmdidx == CMD_earlier ? -count : count, |
| 7462 | sec, file, FALSE); |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 7463 | } |
| 7464 | |
| 7465 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7466 | * ":redir": start/stop redirection. |
| 7467 | */ |
| 7468 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7469 | ex_redir(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7470 | { |
| 7471 | char *mode; |
| 7472 | char_u *fname; |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7473 | char_u *arg = eap->arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7474 | |
Bram Moolenaar | ba76849 | 2016-07-08 15:32:54 +0200 | [diff] [blame] | 7475 | #ifdef FEAT_EVAL |
Bram Moolenaar | 79815f1 | 2016-07-09 17:07:29 +0200 | [diff] [blame] | 7476 | if (redir_execute) |
Bram Moolenaar | 245a7cb | 2016-07-08 10:53:12 +0200 | [diff] [blame] | 7477 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7478 | emsg(_("E930: Cannot use :redir inside execute()")); |
Bram Moolenaar | 245a7cb | 2016-07-08 10:53:12 +0200 | [diff] [blame] | 7479 | return; |
| 7480 | } |
Bram Moolenaar | ba76849 | 2016-07-08 15:32:54 +0200 | [diff] [blame] | 7481 | #endif |
Bram Moolenaar | 245a7cb | 2016-07-08 10:53:12 +0200 | [diff] [blame] | 7482 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7483 | if (STRICMP(eap->arg, "END") == 0) |
| 7484 | close_redir(); |
| 7485 | else |
| 7486 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7487 | if (*arg == '>') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7488 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7489 | ++arg; |
| 7490 | if (*arg == '>') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7491 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7492 | ++arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7493 | mode = "a"; |
| 7494 | } |
| 7495 | else |
| 7496 | mode = "w"; |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7497 | arg = skipwhite(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7498 | |
| 7499 | close_redir(); |
| 7500 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7501 | // Expand environment variables and "~/". |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7502 | fname = expand_env_save(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7503 | if (fname == NULL) |
| 7504 | return; |
| 7505 | #ifdef FEAT_BROWSE |
| 7506 | if (cmdmod.browse) |
| 7507 | { |
| 7508 | char_u *browseFile; |
| 7509 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 7510 | browseFile = do_browse(BROWSE_SAVE, |
| 7511 | (char_u *)_("Save Redirection"), |
Bram Moolenaar | c36651b | 2018-04-29 12:22:56 +0200 | [diff] [blame] | 7512 | fname, NULL, NULL, |
| 7513 | (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7514 | if (browseFile == NULL) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7515 | return; // operation cancelled |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7516 | vim_free(fname); |
| 7517 | fname = browseFile; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7518 | eap->forceit = TRUE; // since dialog already asked |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7519 | } |
| 7520 | #endif |
| 7521 | |
| 7522 | redir_fd = open_exfile(fname, eap->forceit, mode); |
| 7523 | vim_free(fname); |
| 7524 | } |
| 7525 | #ifdef FEAT_EVAL |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7526 | else if (*arg == '@') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7527 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7528 | // redirect to a register a-z (resp. A-Z for appending) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7529 | close_redir(); |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7530 | ++arg; |
| 7531 | if (ASCII_ISALPHA(*arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7532 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7533 | || *arg == '*' |
Bram Moolenaar | 9a51c6e | 2006-11-14 19:25:02 +0000 | [diff] [blame] | 7534 | || *arg == '+' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7535 | # endif |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7536 | || *arg == '"') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7537 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 7538 | redir_reg = *arg++; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7539 | if (*arg == '>' && arg[1] == '>') // append |
Bram Moolenaar | d9d3058 | 2005-05-18 22:10:28 +0000 | [diff] [blame] | 7540 | arg += 2; |
Bram Moolenaar | c188b88 | 2007-10-19 14:20:54 +0000 | [diff] [blame] | 7541 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7542 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7543 | // Can use both "@a" and "@a>". |
Bram Moolenaar | 2c29bee | 2005-06-01 21:46:07 +0000 | [diff] [blame] | 7544 | if (*arg == '>') |
| 7545 | arg++; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7546 | // Make register empty when not using @A-@Z and the |
| 7547 | // command is valid. |
Bram Moolenaar | c188b88 | 2007-10-19 14:20:54 +0000 | [diff] [blame] | 7548 | if (*arg == NUL && !isupper(redir_reg)) |
| 7549 | write_reg_contents(redir_reg, (char_u *)"", -1, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7550 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7551 | } |
| 7552 | if (*arg != NUL) |
| 7553 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7554 | redir_reg = 0; |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7555 | semsg(_(e_invarg2), eap->arg); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7556 | } |
| 7557 | } |
| 7558 | else if (*arg == '=' && arg[1] == '>') |
| 7559 | { |
| 7560 | int append; |
| 7561 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7562 | // redirect to a variable |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7563 | close_redir(); |
| 7564 | arg += 2; |
| 7565 | |
| 7566 | if (*arg == '>') |
| 7567 | { |
| 7568 | ++arg; |
| 7569 | append = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7570 | } |
| 7571 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7572 | append = FALSE; |
| 7573 | |
| 7574 | if (var_redir_start(skipwhite(arg), append) == OK) |
| 7575 | redir_vname = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7576 | } |
| 7577 | #endif |
| 7578 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7579 | // TODO: redirect to a buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7580 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7581 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7582 | semsg(_(e_invarg2), eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7583 | } |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 7584 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7585 | // Make sure redirection is not off. Can happen for cmdline completion |
| 7586 | // that indirectly invokes a command to catch its output. |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 7587 | if (redir_fd != NULL |
| 7588 | #ifdef FEAT_EVAL |
| 7589 | || redir_reg || redir_vname |
| 7590 | #endif |
| 7591 | ) |
| 7592 | redir_off = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7593 | } |
| 7594 | |
| 7595 | /* |
| 7596 | * ":redraw": force redraw |
| 7597 | */ |
Bram Moolenaar | fb1f626 | 2016-01-31 20:24:32 +0100 | [diff] [blame] | 7598 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7599 | ex_redraw(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7600 | { |
| 7601 | int r = RedrawingDisabled; |
| 7602 | int p = p_lz; |
| 7603 | |
| 7604 | RedrawingDisabled = 0; |
| 7605 | p_lz = FALSE; |
Bram Moolenaar | abc39ab | 2017-03-01 18:04:05 +0100 | [diff] [blame] | 7606 | validate_cursor(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7607 | update_topline(); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7608 | update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7609 | #ifdef FEAT_TITLE |
| 7610 | if (need_maketitle) |
| 7611 | maketitle(); |
| 7612 | #endif |
Bram Moolenaar | afde13b | 2019-04-28 19:46:49 +0200 | [diff] [blame] | 7613 | #if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL)) |
| 7614 | # ifdef VIMDLL |
| 7615 | if (!gui.in_use) |
| 7616 | # endif |
| 7617 | resize_console_buf(); |
Bram Moolenaar | 78d21da | 2019-02-17 15:00:52 +0100 | [diff] [blame] | 7618 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7619 | RedrawingDisabled = r; |
| 7620 | p_lz = p; |
| 7621 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7622 | // Reset msg_didout, so that a message that's there is overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7623 | msg_didout = FALSE; |
| 7624 | msg_col = 0; |
| 7625 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7626 | // No need to wait after an intentional redraw. |
Bram Moolenaar | 56667a5 | 2013-07-17 11:54:28 +0200 | [diff] [blame] | 7627 | need_wait_return = FALSE; |
| 7628 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7629 | out_flush(); |
| 7630 | } |
| 7631 | |
| 7632 | /* |
| 7633 | * ":redrawstatus": force redraw of status line(s) |
| 7634 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7635 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7636 | ex_redrawstatus(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7637 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7638 | int r = RedrawingDisabled; |
| 7639 | int p = p_lz; |
| 7640 | |
| 7641 | RedrawingDisabled = 0; |
| 7642 | p_lz = FALSE; |
| 7643 | if (eap->forceit) |
| 7644 | status_redraw_all(); |
| 7645 | else |
| 7646 | status_redraw_curbuf(); |
Bram Moolenaar | f7ff6e8 | 2014-03-23 15:13:05 +0100 | [diff] [blame] | 7647 | update_screen(VIsual_active ? INVERTED : 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7648 | RedrawingDisabled = r; |
| 7649 | p_lz = p; |
| 7650 | out_flush(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7651 | } |
| 7652 | |
Bram Moolenaar | e12bab3 | 2019-01-08 22:02:56 +0100 | [diff] [blame] | 7653 | /* |
| 7654 | * ":redrawtabline": force redraw of the tabline |
| 7655 | */ |
| 7656 | static void |
| 7657 | ex_redrawtabline(exarg_T *eap UNUSED) |
| 7658 | { |
| 7659 | int r = RedrawingDisabled; |
| 7660 | int p = p_lz; |
| 7661 | |
| 7662 | RedrawingDisabled = 0; |
| 7663 | p_lz = FALSE; |
| 7664 | |
| 7665 | draw_tabline(); |
| 7666 | |
| 7667 | RedrawingDisabled = r; |
| 7668 | p_lz = p; |
| 7669 | out_flush(); |
| 7670 | } |
| 7671 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7672 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7673 | close_redir(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7674 | { |
| 7675 | if (redir_fd != NULL) |
| 7676 | { |
| 7677 | fclose(redir_fd); |
| 7678 | redir_fd = NULL; |
| 7679 | } |
| 7680 | #ifdef FEAT_EVAL |
| 7681 | redir_reg = 0; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7682 | if (redir_vname) |
| 7683 | { |
| 7684 | var_redir_stop(); |
| 7685 | redir_vname = 0; |
| 7686 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7687 | #endif |
| 7688 | } |
| 7689 | |
Bram Moolenaar | f96ae0b | 2019-07-28 15:21:55 +0200 | [diff] [blame] | 7690 | #if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7691 | int |
Bram Moolenaar | bd67aac | 2019-09-21 23:09:04 +0200 | [diff] [blame] | 7692 | vim_mkdir_emsg(char_u *name, int prot UNUSED) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7693 | { |
| 7694 | if (vim_mkdir(name, prot) != 0) |
| 7695 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7696 | semsg(_("E739: Cannot create directory: %s"), name); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7697 | return FAIL; |
| 7698 | } |
| 7699 | return OK; |
| 7700 | } |
| 7701 | #endif |
| 7702 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7703 | /* |
| 7704 | * Open a file for writing for an Ex command, with some checks. |
| 7705 | * Return file descriptor, or NULL on failure. |
| 7706 | */ |
| 7707 | FILE * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7708 | open_exfile( |
| 7709 | char_u *fname, |
| 7710 | int forceit, |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7711 | char *mode) // "w" for create new file or "a" for append |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7712 | { |
| 7713 | FILE *fd; |
| 7714 | |
| 7715 | #ifdef UNIX |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7716 | // with Unix it is possible to open a directory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7717 | if (mch_isdir(fname)) |
| 7718 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7719 | semsg(_(e_isadir2), fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7720 | return NULL; |
| 7721 | } |
| 7722 | #endif |
| 7723 | if (!forceit && *mode != 'a' && vim_fexists(fname)) |
| 7724 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7725 | semsg(_("E189: \"%s\" exists (add ! to override)"), fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7726 | return NULL; |
| 7727 | } |
| 7728 | |
| 7729 | if ((fd = mch_fopen((char *)fname, mode)) == NULL) |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7730 | semsg(_("E190: Cannot open \"%s\" for writing"), fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7731 | |
| 7732 | return fd; |
| 7733 | } |
| 7734 | |
| 7735 | /* |
| 7736 | * ":mark" and ":k". |
| 7737 | */ |
| 7738 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7739 | ex_mark(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7740 | { |
| 7741 | pos_T pos; |
| 7742 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7743 | if (*eap->arg == NUL) // No argument? |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7744 | emsg(_(e_argreq)); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7745 | else if (eap->arg[1] != NUL) // more than one character? |
Bram Moolenaar | 2d06bfd | 2020-07-23 17:16:18 +0200 | [diff] [blame] | 7746 | semsg(_(e_trailing_arg), eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7747 | else |
| 7748 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7749 | pos = curwin->w_cursor; // save curwin->w_cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7750 | curwin->w_cursor.lnum = eap->line2; |
| 7751 | beginline(BL_WHITE | BL_FIX); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7752 | if (setmark(*eap->arg) == FAIL) // set mark |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7753 | emsg(_("E191: Argument must be a letter or forward/backward quote")); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7754 | curwin->w_cursor = pos; // restore curwin->w_cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7755 | } |
| 7756 | } |
| 7757 | |
| 7758 | /* |
| 7759 | * Update w_topline, w_leftcol and the cursor position. |
| 7760 | */ |
| 7761 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7762 | update_topline_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7763 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7764 | check_cursor(); // put cursor on valid line |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7765 | update_topline(); |
| 7766 | if (!curwin->w_p_wrap) |
| 7767 | validate_cursor(); |
| 7768 | update_curswant(); |
| 7769 | } |
| 7770 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7771 | /* |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7772 | * Save the current State and go to Normal mode. |
| 7773 | * Return TRUE if the typeahead could be saved. |
| 7774 | */ |
| 7775 | int |
| 7776 | save_current_state(save_state_T *sst) |
| 7777 | { |
| 7778 | sst->save_msg_scroll = msg_scroll; |
| 7779 | sst->save_restart_edit = restart_edit; |
| 7780 | sst->save_msg_didout = msg_didout; |
| 7781 | sst->save_State = State; |
| 7782 | sst->save_insertmode = p_im; |
| 7783 | sst->save_finish_op = finish_op; |
| 7784 | sst->save_opcount = opcount; |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 7785 | sst->save_reg_executing = reg_executing; |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7786 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7787 | msg_scroll = FALSE; // no msg scrolling in Normal mode |
| 7788 | restart_edit = 0; // don't go to Insert mode |
| 7789 | p_im = FALSE; // don't use 'insertmode' |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7790 | |
| 7791 | /* |
| 7792 | * Save the current typeahead. This is required to allow using ":normal" |
| 7793 | * from an event handler and makes sure we don't hang when the argument |
| 7794 | * ends with half a command. |
| 7795 | */ |
| 7796 | save_typeahead(&sst->tabuf); |
| 7797 | return sst->tabuf.typebuf_valid; |
| 7798 | } |
| 7799 | |
| 7800 | void |
| 7801 | restore_current_state(save_state_T *sst) |
| 7802 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7803 | // Restore the previous typeahead. |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7804 | restore_typeahead(&sst->tabuf); |
| 7805 | |
| 7806 | msg_scroll = sst->save_msg_scroll; |
| 7807 | restart_edit = sst->save_restart_edit; |
| 7808 | p_im = sst->save_insertmode; |
| 7809 | finish_op = sst->save_finish_op; |
| 7810 | opcount = sst->save_opcount; |
Bram Moolenaar | cce713d | 2019-03-04 11:40:12 +0100 | [diff] [blame] | 7811 | reg_executing = sst->save_reg_executing; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7812 | msg_didout |= sst->save_msg_didout; // don't reset msg_didout now |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7813 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7814 | // Restore the state (needed when called from a function executed for |
| 7815 | // 'indentexpr'). Update the mouse and cursor, they may have changed. |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7816 | State = sst->save_State; |
| 7817 | #ifdef CURSOR_SHAPE |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7818 | ui_cursor_shape(); // may show different cursor shape |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7819 | #endif |
| 7820 | } |
| 7821 | |
| 7822 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7823 | * ":normal[!] {commands}": Execute normal mode commands. |
| 7824 | */ |
Bram Moolenaar | 20fb9f3 | 2016-01-30 23:20:33 +0100 | [diff] [blame] | 7825 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7826 | ex_normal(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7827 | { |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7828 | save_state_T save_state; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7829 | char_u *arg = NULL; |
| 7830 | int l; |
| 7831 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7832 | |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 7833 | if (ex_normal_lock > 0) |
| 7834 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7835 | emsg(_(e_secure)); |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 7836 | return; |
| 7837 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7838 | if (ex_normal_busy >= p_mmd) |
| 7839 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 7840 | emsg(_("E192: Recursive use of :normal too deep")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7841 | return; |
| 7842 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7843 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7844 | /* |
| 7845 | * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do |
| 7846 | * this for the K_SPECIAL leading byte, otherwise special keys will not |
| 7847 | * work. |
| 7848 | */ |
| 7849 | if (has_mbyte) |
| 7850 | { |
| 7851 | int len = 0; |
| 7852 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7853 | // Count the number of characters to be escaped. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7854 | for (p = eap->arg; *p != NUL; ++p) |
| 7855 | { |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7856 | #ifdef FEAT_GUI |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7857 | if (*p == CSI) // leadbyte CSI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7858 | len += 2; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7859 | #endif |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7860 | for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7861 | if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7862 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7863 | || *p == CSI |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7864 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7865 | ) |
| 7866 | len += 2; |
| 7867 | } |
| 7868 | if (len > 0) |
| 7869 | { |
Bram Moolenaar | 964b374 | 2019-05-24 18:54:09 +0200 | [diff] [blame] | 7870 | arg = alloc(STRLEN(eap->arg) + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7871 | if (arg != NULL) |
| 7872 | { |
| 7873 | len = 0; |
| 7874 | for (p = eap->arg; *p != NUL; ++p) |
| 7875 | { |
| 7876 | arg[len++] = *p; |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7877 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7878 | if (*p == CSI) |
| 7879 | { |
| 7880 | arg[len++] = KS_EXTRA; |
| 7881 | arg[len++] = (int)KE_CSI; |
| 7882 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7883 | #endif |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7884 | for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7885 | { |
| 7886 | arg[len++] = *++p; |
| 7887 | if (*p == K_SPECIAL) |
| 7888 | { |
| 7889 | arg[len++] = KS_SPECIAL; |
| 7890 | arg[len++] = KE_FILLER; |
| 7891 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7892 | #ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7893 | else if (*p == CSI) |
| 7894 | { |
| 7895 | arg[len++] = KS_EXTRA; |
| 7896 | arg[len++] = (int)KE_CSI; |
| 7897 | } |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7898 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7899 | } |
| 7900 | arg[len] = NUL; |
| 7901 | } |
| 7902 | } |
| 7903 | } |
| 7904 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7905 | |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7906 | ++ex_normal_busy; |
| 7907 | if (save_current_state(&save_state)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7908 | { |
| 7909 | /* |
| 7910 | * Repeat the :normal command for each line in the range. When no |
| 7911 | * range given, execute it just once, without positioning the cursor |
| 7912 | * first. |
| 7913 | */ |
| 7914 | do |
| 7915 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7916 | if (eap->addr_count != 0) |
| 7917 | { |
| 7918 | curwin->w_cursor.lnum = eap->line1++; |
| 7919 | curwin->w_cursor.col = 0; |
Bram Moolenaar | d5d3753 | 2017-03-27 23:02:07 +0200 | [diff] [blame] | 7920 | check_cursor_moved(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7921 | } |
| 7922 | |
Bram Moolenaar | 1350597 | 2019-01-24 15:04:48 +0100 | [diff] [blame] | 7923 | exec_normal_cmd(arg != NULL |
| 7924 | ? arg |
| 7925 | : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7926 | } |
| 7927 | while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int); |
| 7928 | } |
| 7929 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7930 | // Might not return to the main loop when in an event handler. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7931 | update_topline_cursor(); |
| 7932 | |
Bram Moolenaar | a21a6a9 | 2017-09-23 16:33:50 +0200 | [diff] [blame] | 7933 | restore_current_state(&save_state); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7934 | --ex_normal_busy; |
Bram Moolenaar | eda7360 | 2014-11-05 09:53:23 +0100 | [diff] [blame] | 7935 | setmouse(); |
Bram Moolenaar | eda7360 | 2014-11-05 09:53:23 +0100 | [diff] [blame] | 7936 | #ifdef CURSOR_SHAPE |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 7937 | ui_cursor_shape(); // may show different cursor shape |
Bram Moolenaar | eda7360 | 2014-11-05 09:53:23 +0100 | [diff] [blame] | 7938 | #endif |
| 7939 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7940 | vim_free(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7941 | } |
| 7942 | |
| 7943 | /* |
Bram Moolenaar | 6496966 | 2005-12-14 21:59:55 +0000 | [diff] [blame] | 7944 | * ":startinsert", ":startreplace" and ":startgreplace" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7945 | */ |
| 7946 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7947 | ex_startinsert(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7948 | { |
Bram Moolenaar | fd37168 | 2005-01-14 21:42:54 +0000 | [diff] [blame] | 7949 | if (eap->forceit) |
| 7950 | { |
Bram Moolenaar | 8d3b510 | 2019-09-05 21:29:01 +0200 | [diff] [blame] | 7951 | // cursor line can be zero on startup |
Bram Moolenaar | 09ca932 | 2017-09-26 17:40:45 +0200 | [diff] [blame] | 7952 | if (!curwin->w_cursor.lnum) |
| 7953 | curwin->w_cursor.lnum = 1; |
Bram Moolenaar | 8d3b510 | 2019-09-05 21:29:01 +0200 | [diff] [blame] | 7954 | set_cursor_for_append_to_line(); |
Bram Moolenaar | fd37168 | 2005-01-14 21:42:54 +0000 | [diff] [blame] | 7955 | } |
Bram Moolenaar | eb58a24 | 2020-04-19 18:13:19 +0200 | [diff] [blame] | 7956 | #ifdef FEAT_TERMINAL |
| 7957 | // Ignore this when running in an active terminal. |
| 7958 | if (term_job_running(curbuf->b_term)) |
| 7959 | return; |
| 7960 | #endif |
Bram Moolenaar | fd37168 | 2005-01-14 21:42:54 +0000 | [diff] [blame] | 7961 | |
Bram Moolenaar | 8d3b510 | 2019-09-05 21:29:01 +0200 | [diff] [blame] | 7962 | // Ignore the command when already in Insert mode. Inserting an |
| 7963 | // expression register that invokes a function can do this. |
Bram Moolenaar | a40c500 | 2005-01-09 21:16:21 +0000 | [diff] [blame] | 7964 | if (State & INSERT) |
| 7965 | return; |
| 7966 | |
Bram Moolenaar | 6496966 | 2005-12-14 21:59:55 +0000 | [diff] [blame] | 7967 | if (eap->cmdidx == CMD_startinsert) |
| 7968 | restart_edit = 'a'; |
| 7969 | else if (eap->cmdidx == CMD_startreplace) |
| 7970 | restart_edit = 'R'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7971 | else |
Bram Moolenaar | 6496966 | 2005-12-14 21:59:55 +0000 | [diff] [blame] | 7972 | restart_edit = 'V'; |
| 7973 | |
| 7974 | if (!eap->forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7975 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 7976 | if (eap->cmdidx == CMD_startinsert) |
| 7977 | restart_edit = 'i'; |
Bram Moolenaar | 8d3b510 | 2019-09-05 21:29:01 +0200 | [diff] [blame] | 7978 | curwin->w_curswant = 0; // avoid MAXCOL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7979 | } |
| 7980 | } |
| 7981 | |
| 7982 | /* |
| 7983 | * ":stopinsert" |
| 7984 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7985 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7986 | ex_stopinsert(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7987 | { |
| 7988 | restart_edit = 0; |
| 7989 | stop_insert_mode = TRUE; |
Bram Moolenaar | fd773e9 | 2016-04-02 19:39:16 +0200 | [diff] [blame] | 7990 | clearmode(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7991 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7992 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 7993 | /* |
| 7994 | * Execute normal mode command "cmd". |
| 7995 | * "remap" can be REMAP_NONE or REMAP_YES. |
| 7996 | */ |
| 7997 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7998 | exec_normal_cmd(char_u *cmd, int remap, int silent) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 7999 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8000 | // Stuff the argument into the typeahead buffer. |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 8001 | ins_typebuf(cmd, remap, 0, TRUE, silent); |
Bram Moolenaar | 586c70c | 2018-10-02 16:23:58 +0200 | [diff] [blame] | 8002 | exec_normal(FALSE, FALSE, FALSE); |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 8003 | } |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 8004 | |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 8005 | /* |
| 8006 | * Execute normal_cmd() until there is no typeahead left. |
Bram Moolenaar | 586c70c | 2018-10-02 16:23:58 +0200 | [diff] [blame] | 8007 | * When "use_vpeekc" is TRUE use vpeekc() to check for available chars. |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 8008 | */ |
| 8009 | void |
Bram Moolenaar | 586c70c | 2018-10-02 16:23:58 +0200 | [diff] [blame] | 8010 | exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED) |
Bram Moolenaar | 2528163 | 2016-01-21 23:32:32 +0100 | [diff] [blame] | 8011 | { |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8012 | oparg_T oa; |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 8013 | int c; |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8014 | |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 8015 | // When calling vpeekc() from feedkeys() it will return Ctrl_C when there |
| 8016 | // is nothing to get, so also check for Ctrl_C. |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8017 | clear_oparg(&oa); |
| 8018 | finish_op = FALSE; |
Bram Moolenaar | 586c70c | 2018-10-02 16:23:58 +0200 | [diff] [blame] | 8019 | while ((!stuff_empty() |
| 8020 | || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0) |
Bram Moolenaar | 905dd90 | 2019-04-07 14:21:47 +0200 | [diff] [blame] | 8021 | || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C)) |
Bram Moolenaar | 586c70c | 2018-10-02 16:23:58 +0200 | [diff] [blame] | 8022 | && !got_int) |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8023 | { |
| 8024 | update_topline_cursor(); |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 8025 | #ifdef FEAT_TERMINAL |
Bram Moolenaar | 655a82a | 2018-05-08 22:01:07 +0200 | [diff] [blame] | 8026 | if (may_use_terminal_loop && term_use_loop() |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 8027 | && oa.op_type == OP_NOP && oa.regname == NUL |
| 8028 | && !VIsual_active) |
| 8029 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8030 | // If terminal_loop() returns OK we got a key that is handled |
| 8031 | // in Normal model. With FAIL we first need to position the |
| 8032 | // cursor and the screen needs to be redrawn. |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 8033 | if (terminal_loop(TRUE) == OK) |
| 8034 | normal_cmd(&oa, TRUE); |
| 8035 | } |
| 8036 | else |
| 8037 | #endif |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8038 | // execute a Normal mode cmd |
Bram Moolenaar | b2ac14c | 2018-05-01 18:47:59 +0200 | [diff] [blame] | 8039 | normal_cmd(&oa, TRUE); |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8040 | } |
| 8041 | } |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8042 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8043 | #ifdef FEAT_FIND_ID |
| 8044 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8045 | ex_checkpath(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8046 | { |
| 8047 | find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L, |
| 8048 | eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW, |
| 8049 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 8050 | } |
| 8051 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 8052 | #if defined(FEAT_QUICKFIX) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8053 | /* |
| 8054 | * ":psearch" |
| 8055 | */ |
| 8056 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8057 | ex_psearch(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8058 | { |
| 8059 | g_do_tagpreview = p_pvh; |
| 8060 | ex_findpat(eap); |
| 8061 | g_do_tagpreview = 0; |
| 8062 | } |
| 8063 | #endif |
| 8064 | |
| 8065 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8066 | ex_findpat(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8067 | { |
| 8068 | int whole = TRUE; |
| 8069 | long n; |
| 8070 | char_u *p; |
| 8071 | int action; |
| 8072 | |
| 8073 | switch (cmdnames[eap->cmdidx].cmd_name[2]) |
| 8074 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8075 | case 'e': // ":psearch", ":isearch" and ":dsearch" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8076 | if (cmdnames[eap->cmdidx].cmd_name[0] == 'p') |
| 8077 | action = ACTION_GOTO; |
| 8078 | else |
| 8079 | action = ACTION_SHOW; |
| 8080 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8081 | case 'i': // ":ilist" and ":dlist" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8082 | action = ACTION_SHOW_ALL; |
| 8083 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8084 | case 'u': // ":ijump" and ":djump" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8085 | action = ACTION_GOTO; |
| 8086 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8087 | default: // ":isplit" and ":dsplit" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8088 | action = ACTION_SPLIT; |
| 8089 | break; |
| 8090 | } |
| 8091 | |
| 8092 | n = 1; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8093 | if (vim_isdigit(*eap->arg)) // get count |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8094 | { |
| 8095 | n = getdigits(&eap->arg); |
| 8096 | eap->arg = skipwhite(eap->arg); |
| 8097 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8098 | if (*eap->arg == '/') // Match regexp, not just whole words |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8099 | { |
| 8100 | whole = FALSE; |
| 8101 | ++eap->arg; |
Bram Moolenaar | e8c4abb | 2020-04-02 21:13:25 +0200 | [diff] [blame] | 8102 | p = skip_regexp(eap->arg, '/', p_magic); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8103 | if (*p) |
| 8104 | { |
| 8105 | *p++ = NUL; |
| 8106 | p = skipwhite(p); |
| 8107 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8108 | // Check for trailing illegal characters |
Bram Moolenaar | faac410 | 2020-04-20 17:46:14 +0200 | [diff] [blame] | 8109 | if (!ends_excmd2(eap->arg, p)) |
Bram Moolenaar | 8930caa | 2020-07-23 16:37:03 +0200 | [diff] [blame] | 8110 | eap->errmsg = ex_errmsg(e_trailing_arg, p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8111 | else |
| 8112 | eap->nextcmd = check_nextcmd(p); |
| 8113 | } |
| 8114 | } |
| 8115 | if (!eap->skip) |
| 8116 | find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg), |
| 8117 | whole, !eap->forceit, |
| 8118 | *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY, |
| 8119 | n, action, eap->line1, eap->line2); |
| 8120 | } |
| 8121 | #endif |
| 8122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8123 | |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 8124 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8125 | /* |
| 8126 | * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc. |
| 8127 | */ |
| 8128 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8129 | ex_ptag(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8130 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8131 | g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd() |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8132 | ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); |
| 8133 | } |
| 8134 | |
| 8135 | /* |
| 8136 | * ":pedit" |
| 8137 | */ |
| 8138 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8139 | ex_pedit(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8140 | { |
| 8141 | win_T *curwin_save = curwin; |
| 8142 | |
Bram Moolenaar | 3a2505c | 2020-03-09 16:40:41 +0100 | [diff] [blame] | 8143 | if (ERROR_IF_ANY_POPUP_WINDOW) |
| 8144 | return; |
| 8145 | |
Bram Moolenaar | 026587b | 2019-08-17 15:08:00 +0200 | [diff] [blame] | 8146 | // Open the preview window or popup and make it the current window. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8147 | g_do_tagpreview = p_pvh; |
Bram Moolenaar | 576a4a6 | 2019-08-18 15:25:17 +0200 | [diff] [blame] | 8148 | prepare_tagpreview(TRUE, TRUE, FALSE); |
Bram Moolenaar | 1b6d9c4 | 2019-08-05 21:52:04 +0200 | [diff] [blame] | 8149 | |
Bram Moolenaar | 026587b | 2019-08-17 15:08:00 +0200 | [diff] [blame] | 8150 | // Edit the file. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8151 | do_exedit(eap, NULL); |
Bram Moolenaar | 1b6d9c4 | 2019-08-05 21:52:04 +0200 | [diff] [blame] | 8152 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8153 | if (curwin != curwin_save && win_valid(curwin_save)) |
| 8154 | { |
Bram Moolenaar | 026587b | 2019-08-17 15:08:00 +0200 | [diff] [blame] | 8155 | // Return cursor to where we were |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8156 | validate_cursor(); |
| 8157 | redraw_later(VALID); |
| 8158 | win_enter(curwin_save, TRUE); |
| 8159 | } |
Bram Moolenaar | 05ad5ff | 2019-11-30 22:48:27 +0100 | [diff] [blame] | 8160 | # ifdef FEAT_PROP_POPUP |
Bram Moolenaar | 1b6d9c4 | 2019-08-05 21:52:04 +0200 | [diff] [blame] | 8161 | else if (WIN_IS_POPUP(curwin)) |
| 8162 | { |
| 8163 | // can't keep focus in popup window |
| 8164 | win_enter(firstwin, TRUE); |
| 8165 | } |
| 8166 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8167 | g_do_tagpreview = 0; |
| 8168 | } |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 8169 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8170 | |
| 8171 | /* |
| 8172 | * ":stag", ":stselect" and ":stjump". |
| 8173 | */ |
| 8174 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8175 | ex_stag(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8176 | { |
| 8177 | postponed_split = -1; |
| 8178 | postponed_split_flags = cmdmod.split; |
Bram Moolenaar | d326ce8 | 2007-03-11 14:48:29 +0000 | [diff] [blame] | 8179 | postponed_split_tab = cmdmod.tab; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8180 | ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); |
| 8181 | postponed_split_flags = 0; |
Bram Moolenaar | d326ce8 | 2007-03-11 14:48:29 +0000 | [diff] [blame] | 8182 | postponed_split_tab = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8183 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8184 | |
| 8185 | /* |
| 8186 | * ":tag", ":tselect", ":tjump", ":tnext", etc. |
| 8187 | */ |
| 8188 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8189 | ex_tag(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8190 | { |
| 8191 | ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name); |
| 8192 | } |
| 8193 | |
| 8194 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8195 | ex_tag_cmd(exarg_T *eap, char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8196 | { |
| 8197 | int cmd; |
| 8198 | |
| 8199 | switch (name[1]) |
| 8200 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8201 | case 'j': cmd = DT_JUMP; // ":tjump" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8202 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8203 | case 's': cmd = DT_SELECT; // ":tselect" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8204 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8205 | case 'p': cmd = DT_PREV; // ":tprevious" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8206 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8207 | case 'N': cmd = DT_PREV; // ":tNext" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8208 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8209 | case 'n': cmd = DT_NEXT; // ":tnext" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8210 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8211 | case 'o': cmd = DT_POP; // ":pop" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8212 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8213 | case 'f': // ":tfirst" |
| 8214 | case 'r': cmd = DT_FIRST; // ":trewind" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8215 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8216 | case 'l': cmd = DT_LAST; // ":tlast" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8217 | break; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8218 | default: // ":tag" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8219 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 7c94c26 | 2008-06-20 09:11:34 +0000 | [diff] [blame] | 8220 | if (p_cst && *eap->arg != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8221 | { |
Bram Moolenaar | d4db771 | 2016-11-12 19:16:46 +0100 | [diff] [blame] | 8222 | ex_cstag(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8223 | return; |
| 8224 | } |
| 8225 | #endif |
| 8226 | cmd = DT_TAG; |
| 8227 | break; |
| 8228 | } |
| 8229 | |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 8230 | if (name[0] == 'l') |
| 8231 | { |
| 8232 | #ifndef FEAT_QUICKFIX |
| 8233 | ex_ni(eap); |
| 8234 | return; |
| 8235 | #else |
| 8236 | cmd = DT_LTAG; |
| 8237 | #endif |
| 8238 | } |
| 8239 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8240 | do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1, |
| 8241 | eap->forceit, TRUE); |
| 8242 | } |
| 8243 | |
| 8244 | /* |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8245 | * Check "str" for starting with a special cmdline variable. |
| 8246 | * If found return one of the SPEC_ values and set "*usedlen" to the length of |
| 8247 | * the variable. Otherwise return -1 and "*usedlen" is unchanged. |
| 8248 | */ |
| 8249 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8250 | find_cmdline_var(char_u *src, int *usedlen) |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8251 | { |
| 8252 | int len; |
| 8253 | int i; |
Bram Moolenaar | 8f0b2d4 | 2009-05-16 14:41:10 +0000 | [diff] [blame] | 8254 | static char *(spec_str[]) = { |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8255 | "%", |
| 8256 | #define SPEC_PERC 0 |
| 8257 | "#", |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8258 | #define SPEC_HASH (SPEC_PERC + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8259 | "<cword>", // cursor word |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8260 | #define SPEC_CWORD (SPEC_HASH + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8261 | "<cWORD>", // cursor WORD |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8262 | #define SPEC_CCWORD (SPEC_CWORD + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8263 | "<cexpr>", // expr under cursor |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8264 | #define SPEC_CEXPR (SPEC_CCWORD + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8265 | "<cfile>", // cursor path name |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8266 | #define SPEC_CFILE (SPEC_CEXPR + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8267 | "<sfile>", // ":so" file name |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8268 | #define SPEC_SFILE (SPEC_CFILE + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8269 | "<slnum>", // ":so" file line number |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8270 | #define SPEC_SLNUM (SPEC_SFILE + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8271 | "<afile>", // autocommand file name |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8272 | #define SPEC_AFILE (SPEC_SLNUM + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8273 | "<abuf>", // autocommand buffer number |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8274 | #define SPEC_ABUF (SPEC_AFILE + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8275 | "<amatch>", // autocommand match name |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 8276 | #define SPEC_AMATCH (SPEC_ABUF + 1) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8277 | "<sflnum>", // script file line number |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8278 | #define SPEC_SFLNUM (SPEC_AMATCH + 1) |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8279 | #ifdef FEAT_CLIENTSERVER |
| 8280 | "<client>" |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8281 | # define SPEC_CLIENT (SPEC_SFLNUM + 1) |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8282 | #endif |
| 8283 | }; |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8284 | |
Bram Moolenaar | 5fd0ca7 | 2009-05-13 16:56:33 +0000 | [diff] [blame] | 8285 | for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i) |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8286 | { |
| 8287 | len = (int)STRLEN(spec_str[i]); |
| 8288 | if (STRNCMP(src, spec_str[i], len) == 0) |
| 8289 | { |
| 8290 | *usedlen = len; |
| 8291 | return i; |
| 8292 | } |
| 8293 | } |
| 8294 | return -1; |
| 8295 | } |
| 8296 | |
| 8297 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8298 | * Evaluate cmdline variables. |
| 8299 | * |
| 8300 | * change '%' to curbuf->b_ffname |
| 8301 | * '#' to curwin->w_altfile |
| 8302 | * '<cword>' to word under the cursor |
| 8303 | * '<cWORD>' to WORD under the cursor |
Bram Moolenaar | 8071cb2 | 2019-07-12 17:58:01 +0200 | [diff] [blame] | 8304 | * '<cexpr>' to C-expression under the cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8305 | * '<cfile>' to path name under the cursor |
| 8306 | * '<sfile>' to sourced file name |
Bram Moolenaar | 4dbbff5 | 2010-11-24 15:50:59 +0100 | [diff] [blame] | 8307 | * '<slnum>' to sourced file line number |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8308 | * '<afile>' to file name for autocommand |
| 8309 | * '<abuf>' to buffer number for autocommand |
| 8310 | * '<amatch>' to matching name for autocommand |
| 8311 | * |
| 8312 | * When an error is detected, "errormsg" is set to a non-NULL pointer (may be |
| 8313 | * "" for error without a message) and NULL is returned. |
| 8314 | * Returns an allocated string if a valid match was found. |
| 8315 | * Returns NULL if no match was found. "usedlen" then still contains the |
| 8316 | * number of characters to skip. |
| 8317 | */ |
| 8318 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8319 | eval_vars( |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8320 | char_u *src, // pointer into commandline |
| 8321 | char_u *srcstart, // beginning of valid memory for src |
| 8322 | int *usedlen, // characters after src that are used |
| 8323 | linenr_T *lnump, // line number for :e command, or NULL |
| 8324 | char **errormsg, // pointer to error message |
| 8325 | int *escaped) // return value has escaped white space (can |
| 8326 | // be NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8327 | { |
| 8328 | int i; |
| 8329 | char_u *s; |
| 8330 | char_u *result; |
| 8331 | char_u *resultbuf = NULL; |
| 8332 | int resultlen; |
| 8333 | buf_T *buf; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8334 | int valid = VALID_HEAD + VALID_PATH; // assume valid result |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8335 | int spec_idx; |
Bram Moolenaar | fd24946 | 2018-07-28 16:14:30 +0200 | [diff] [blame] | 8336 | int tilde_file = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8337 | int skip_mod = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8338 | char_u strbuf[30]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8339 | |
| 8340 | *errormsg = NULL; |
Bram Moolenaar | 63b9254 | 2007-03-27 14:57:09 +0000 | [diff] [blame] | 8341 | if (escaped != NULL) |
| 8342 | *escaped = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8343 | |
| 8344 | /* |
| 8345 | * Check if there is something to do. |
| 8346 | */ |
Bram Moolenaar | 05bb953 | 2008-07-04 09:44:11 +0000 | [diff] [blame] | 8347 | spec_idx = find_cmdline_var(src, usedlen); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8348 | if (spec_idx < 0) // no match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8349 | { |
| 8350 | *usedlen = 1; |
| 8351 | return NULL; |
| 8352 | } |
| 8353 | |
| 8354 | /* |
| 8355 | * Skip when preceded with a backslash "\%" and "\#". |
| 8356 | * Note: In "\\%" the % is also not recognized! |
| 8357 | */ |
| 8358 | if (src > srcstart && src[-1] == '\\') |
| 8359 | { |
| 8360 | *usedlen = 0; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8361 | STRMOVE(src - 1, src); // remove backslash |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8362 | return NULL; |
| 8363 | } |
| 8364 | |
| 8365 | /* |
| 8366 | * word or WORD under cursor |
| 8367 | */ |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8368 | if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD |
| 8369 | || spec_idx == SPEC_CEXPR) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8370 | { |
Bram Moolenaar | 65f0847 | 2017-09-10 18:16:20 +0200 | [diff] [blame] | 8371 | resultlen = find_ident_under_cursor(&result, |
| 8372 | spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING) |
| 8373 | : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL) |
| 8374 | : FIND_STRING); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8375 | if (resultlen == 0) |
| 8376 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8377 | *errormsg = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8378 | return NULL; |
| 8379 | } |
| 8380 | } |
| 8381 | |
| 8382 | /* |
| 8383 | * '#': Alternate file name |
| 8384 | * '%': Current file name |
| 8385 | * File name under the cursor |
| 8386 | * File name for autocommand |
| 8387 | * and following modifiers |
| 8388 | */ |
| 8389 | else |
| 8390 | { |
| 8391 | switch (spec_idx) |
| 8392 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8393 | case SPEC_PERC: // '%': current file |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8394 | if (curbuf->b_fname == NULL) |
| 8395 | { |
| 8396 | result = (char_u *)""; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8397 | valid = 0; // Must have ":p:h" to be valid |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8398 | } |
| 8399 | else |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 8400 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8401 | result = curbuf->b_fname; |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 8402 | tilde_file = STRCMP(result, "~") == 0; |
| 8403 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8404 | break; |
| 8405 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8406 | case SPEC_HASH: // '#' or "#99": alternate file |
| 8407 | if (src[1] == '#') // "##": the argument list |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8408 | { |
| 8409 | result = arg_all(); |
| 8410 | resultbuf = result; |
| 8411 | *usedlen = 2; |
Bram Moolenaar | 63b9254 | 2007-03-27 14:57:09 +0000 | [diff] [blame] | 8412 | if (escaped != NULL) |
| 8413 | *escaped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8414 | skip_mod = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8415 | break; |
| 8416 | } |
| 8417 | s = src + 1; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8418 | if (*s == '<') // "#<99" uses v:oldfiles |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8419 | ++s; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8420 | i = (int)getdigits(&s); |
Bram Moolenaar | c312b8b | 2017-10-28 17:53:04 +0200 | [diff] [blame] | 8421 | if (s == src + 2 && src[1] == '-') |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8422 | // just a minus sign, don't skip over it |
Bram Moolenaar | c312b8b | 2017-10-28 17:53:04 +0200 | [diff] [blame] | 8423 | s--; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8424 | *usedlen = (int)(s - src); // length of what we expand |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8425 | |
Bram Moolenaar | c312b8b | 2017-10-28 17:53:04 +0200 | [diff] [blame] | 8426 | if (src[1] == '<' && i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8427 | { |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8428 | if (*usedlen < 2) |
| 8429 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8430 | // Should we give an error message for #<text? |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8431 | *usedlen = 1; |
| 8432 | return NULL; |
| 8433 | } |
| 8434 | #ifdef FEAT_EVAL |
| 8435 | result = list_find_str(get_vim_var_list(VV_OLDFILES), |
| 8436 | (long)i); |
| 8437 | if (result == NULL) |
| 8438 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8439 | *errormsg = ""; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8440 | return NULL; |
| 8441 | } |
| 8442 | #else |
Bram Moolenaar | 8e481e8 | 2019-01-15 20:07:48 +0100 | [diff] [blame] | 8443 | *errormsg = _("E809: #< is not available without the +eval feature"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8444 | return NULL; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8445 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8446 | } |
| 8447 | else |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8448 | { |
Bram Moolenaar | c312b8b | 2017-10-28 17:53:04 +0200 | [diff] [blame] | 8449 | if (i == 0 && src[1] == '<' && *usedlen > 1) |
| 8450 | *usedlen = 1; |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8451 | buf = buflist_findnr(i); |
| 8452 | if (buf == NULL) |
| 8453 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8454 | *errormsg = _("E194: No alternate file name to substitute for '#'"); |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8455 | return NULL; |
| 8456 | } |
| 8457 | if (lnump != NULL) |
| 8458 | *lnump = ECMD_LAST; |
| 8459 | if (buf->b_fname == NULL) |
| 8460 | { |
| 8461 | result = (char_u *)""; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8462 | valid = 0; // Must have ":p:h" to be valid |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8463 | } |
| 8464 | else |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 8465 | { |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8466 | result = buf->b_fname; |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 8467 | tilde_file = STRCMP(result, "~") == 0; |
| 8468 | } |
Bram Moolenaar | d812df6 | 2008-11-09 12:46:09 +0000 | [diff] [blame] | 8469 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8470 | break; |
| 8471 | |
| 8472 | #ifdef FEAT_SEARCHPATH |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8473 | case SPEC_CFILE: // file name under cursor |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 8474 | result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8475 | if (result == NULL) |
| 8476 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8477 | *errormsg = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8478 | return NULL; |
| 8479 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8480 | resultbuf = result; // remember allocated string |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8481 | break; |
| 8482 | #endif |
| 8483 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8484 | case SPEC_AFILE: // file name for autocommand |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8485 | result = autocmd_fname; |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 8486 | if (result != NULL && !autocmd_fname_full) |
| 8487 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8488 | // Still need to turn the fname into a full path. It is |
| 8489 | // postponed to avoid a delay when <afile> is not used. |
Bram Moolenaar | f6dad43 | 2008-09-18 19:29:58 +0000 | [diff] [blame] | 8490 | autocmd_fname_full = TRUE; |
| 8491 | result = FullName_save(autocmd_fname, FALSE); |
| 8492 | vim_free(autocmd_fname); |
| 8493 | autocmd_fname = result; |
| 8494 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8495 | if (result == NULL) |
| 8496 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8497 | *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\""); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8498 | return NULL; |
| 8499 | } |
Bram Moolenaar | a0174af | 2008-01-02 20:08:25 +0000 | [diff] [blame] | 8500 | result = shorten_fname1(result); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8501 | break; |
| 8502 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8503 | case SPEC_ABUF: // buffer number for autocommand |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8504 | if (autocmd_bufnr <= 0) |
| 8505 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8506 | *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\""); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8507 | return NULL; |
| 8508 | } |
| 8509 | sprintf((char *)strbuf, "%d", autocmd_bufnr); |
| 8510 | result = strbuf; |
| 8511 | break; |
| 8512 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8513 | case SPEC_AMATCH: // match name for autocommand |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8514 | result = autocmd_match; |
| 8515 | if (result == NULL) |
| 8516 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8517 | *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\""); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8518 | return NULL; |
| 8519 | } |
| 8520 | break; |
| 8521 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8522 | case SPEC_SFILE: // file name for ":so" command |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 8523 | result = estack_sfile(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8524 | if (result == NULL) |
| 8525 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8526 | *errormsg = _("E498: no :source file name to substitute for \"<sfile>\""); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8527 | return NULL; |
| 8528 | } |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 8529 | resultbuf = result; // remember allocated string |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8530 | break; |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8531 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8532 | case SPEC_SLNUM: // line in file for ":so" command |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 8533 | if (SOURCING_NAME == NULL || SOURCING_LNUM == 0) |
Bram Moolenaar | 4dbbff5 | 2010-11-24 15:50:59 +0100 | [diff] [blame] | 8534 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8535 | *errormsg = _("E842: no line number to use for \"<slnum>\""); |
Bram Moolenaar | 4dbbff5 | 2010-11-24 15:50:59 +0100 | [diff] [blame] | 8536 | return NULL; |
| 8537 | } |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 8538 | sprintf((char *)strbuf, "%ld", SOURCING_LNUM); |
Bram Moolenaar | 4dbbff5 | 2010-11-24 15:50:59 +0100 | [diff] [blame] | 8539 | result = strbuf; |
| 8540 | break; |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8541 | |
| 8542 | #ifdef FEAT_EVAL |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8543 | case SPEC_SFLNUM: // line in script file |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 8544 | if (current_sctx.sc_lnum + SOURCING_LNUM == 0) |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8545 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8546 | *errormsg = _("E961: no line number to use for \"<sflnum>\""); |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8547 | return NULL; |
| 8548 | } |
| 8549 | sprintf((char *)strbuf, "%ld", |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 8550 | (long)(current_sctx.sc_lnum + SOURCING_LNUM)); |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8551 | result = strbuf; |
| 8552 | break; |
| 8553 | #endif |
| 8554 | |
| 8555 | #ifdef FEAT_CLIENTSERVER |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8556 | case SPEC_CLIENT: // Source of last submitted input |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 8557 | sprintf((char *)strbuf, PRINTF_HEX_LONG_U, |
| 8558 | (long_u)clientWindow); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8559 | result = strbuf; |
| 8560 | break; |
| 8561 | #endif |
Bram Moolenaar | f29c1c6 | 2018-09-10 21:05:02 +0200 | [diff] [blame] | 8562 | |
Bram Moolenaar | 9e0f6ec | 2017-05-16 09:36:54 +0200 | [diff] [blame] | 8563 | default: |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8564 | result = (char_u *)""; // avoid gcc warning |
Bram Moolenaar | 9e0f6ec | 2017-05-16 09:36:54 +0200 | [diff] [blame] | 8565 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8566 | } |
| 8567 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8568 | resultlen = (int)STRLEN(result); // length of new string |
| 8569 | if (src[*usedlen] == '<') // remove the file name extension |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8570 | { |
| 8571 | ++*usedlen; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8572 | if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8573 | resultlen = (int)(s - result); |
| 8574 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8575 | else if (!skip_mod) |
| 8576 | { |
Bram Moolenaar | 00136dc | 2018-07-25 21:19:13 +0200 | [diff] [blame] | 8577 | valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8578 | &resultlen); |
| 8579 | if (result == NULL) |
| 8580 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8581 | *errormsg = ""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8582 | return NULL; |
| 8583 | } |
| 8584 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8585 | } |
| 8586 | |
| 8587 | if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH) |
| 8588 | { |
| 8589 | if (valid != VALID_HEAD + VALID_PATH) |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8590 | // xgettext:no-c-format |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8591 | *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\""); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8592 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8593 | *errormsg = _("E500: Evaluates to an empty string"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8594 | result = NULL; |
| 8595 | } |
| 8596 | else |
| 8597 | result = vim_strnsave(result, resultlen); |
| 8598 | vim_free(resultbuf); |
| 8599 | return result; |
| 8600 | } |
| 8601 | |
| 8602 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8603 | * Expand the <sfile> string in "arg". |
| 8604 | * |
| 8605 | * Returns an allocated string, or NULL for any error. |
| 8606 | */ |
| 8607 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8608 | expand_sfile(char_u *arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8609 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8610 | char *errormsg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8611 | int len; |
| 8612 | char_u *result; |
| 8613 | char_u *newres; |
| 8614 | char_u *repl; |
| 8615 | int srclen; |
| 8616 | char_u *p; |
| 8617 | |
| 8618 | result = vim_strsave(arg); |
| 8619 | if (result == NULL) |
| 8620 | return NULL; |
| 8621 | |
| 8622 | for (p = result; *p; ) |
| 8623 | { |
| 8624 | if (STRNCMP(p, "<sfile>", 7) != 0) |
| 8625 | ++p; |
| 8626 | else |
| 8627 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8628 | // replace "<sfile>" with the sourced file name, and do ":" stuff |
Bram Moolenaar | 63b9254 | 2007-03-27 14:57:09 +0000 | [diff] [blame] | 8629 | repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8630 | if (errormsg != NULL) |
| 8631 | { |
| 8632 | if (*errormsg) |
| 8633 | emsg(errormsg); |
| 8634 | vim_free(result); |
| 8635 | return NULL; |
| 8636 | } |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8637 | if (repl == NULL) // no match (cannot happen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8638 | { |
| 8639 | p += srclen; |
| 8640 | continue; |
| 8641 | } |
| 8642 | len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1; |
| 8643 | newres = alloc(len); |
| 8644 | if (newres == NULL) |
| 8645 | { |
| 8646 | vim_free(repl); |
| 8647 | vim_free(result); |
| 8648 | return NULL; |
| 8649 | } |
| 8650 | mch_memmove(newres, result, (size_t)(p - result)); |
| 8651 | STRCPY(newres + (p - result), repl); |
| 8652 | len = (int)STRLEN(newres); |
| 8653 | STRCAT(newres, p + srclen); |
| 8654 | vim_free(repl); |
| 8655 | vim_free(result); |
| 8656 | result = newres; |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8657 | p = newres + len; // continue after the match |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8658 | } |
| 8659 | } |
| 8660 | |
| 8661 | return result; |
| 8662 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8663 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8664 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO) |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 8665 | /* |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 8666 | * Make a dialog message in "buff[DIALOG_MSG_SIZE]". |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 8667 | * "format" must contain "%s". |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 8668 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8669 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8670 | dialog_msg(char_u *buff, char *format, char_u *fname) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8671 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8672 | if (fname == NULL) |
| 8673 | fname = (char_u *)_("Untitled"); |
Bram Moolenaar | d9462e3 | 2011-04-11 21:35:11 +0200 | [diff] [blame] | 8674 | vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8675 | } |
| 8676 | #endif |
| 8677 | |
| 8678 | /* |
| 8679 | * ":behave {mswin,xterm}" |
| 8680 | */ |
| 8681 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8682 | ex_behave(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8683 | { |
| 8684 | if (STRCMP(eap->arg, "mswin") == 0) |
| 8685 | { |
| 8686 | set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0); |
| 8687 | set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0); |
| 8688 | set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0); |
| 8689 | set_option_value((char_u *)"keymodel", 0L, |
| 8690 | (char_u *)"startsel,stopsel", 0); |
| 8691 | } |
| 8692 | else if (STRCMP(eap->arg, "xterm") == 0) |
| 8693 | { |
| 8694 | set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0); |
| 8695 | set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0); |
| 8696 | set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0); |
| 8697 | set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0); |
| 8698 | } |
| 8699 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8700 | semsg(_(e_invarg2), eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8701 | } |
| 8702 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8703 | static int filetype_detect = FALSE; |
| 8704 | static int filetype_plugin = FALSE; |
| 8705 | static int filetype_indent = FALSE; |
| 8706 | |
| 8707 | /* |
| 8708 | * ":filetype [plugin] [indent] {on,off,detect}" |
| 8709 | * on: Load the filetype.vim file to install autocommands for file types. |
| 8710 | * off: Load the ftoff.vim file to remove all autocommands for file types. |
| 8711 | * plugin on: load filetype.vim and ftplugin.vim |
| 8712 | * plugin off: load ftplugof.vim |
| 8713 | * indent on: load filetype.vim and indent.vim |
| 8714 | * indent off: load indoff.vim |
| 8715 | */ |
| 8716 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8717 | ex_filetype(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8718 | { |
| 8719 | char_u *arg = eap->arg; |
| 8720 | int plugin = FALSE; |
| 8721 | int indent = FALSE; |
| 8722 | |
| 8723 | if (*eap->arg == NUL) |
| 8724 | { |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8725 | // Print current status. |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8726 | smsg("filetype detection:%s plugin:%s indent:%s", |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8727 | filetype_detect ? "ON" : "OFF", |
| 8728 | filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF", |
| 8729 | filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF"); |
| 8730 | return; |
| 8731 | } |
| 8732 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8733 | // Accept "plugin" and "indent" in any order. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8734 | for (;;) |
| 8735 | { |
| 8736 | if (STRNCMP(arg, "plugin", 6) == 0) |
| 8737 | { |
| 8738 | plugin = TRUE; |
| 8739 | arg = skipwhite(arg + 6); |
| 8740 | continue; |
| 8741 | } |
| 8742 | if (STRNCMP(arg, "indent", 6) == 0) |
| 8743 | { |
| 8744 | indent = TRUE; |
| 8745 | arg = skipwhite(arg + 6); |
| 8746 | continue; |
| 8747 | } |
| 8748 | break; |
| 8749 | } |
| 8750 | if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0) |
| 8751 | { |
| 8752 | if (*arg == 'o' || !filetype_detect) |
| 8753 | { |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 8754 | source_runtime((char_u *)FILETYPE_FILE, DIP_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8755 | filetype_detect = TRUE; |
| 8756 | if (plugin) |
| 8757 | { |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 8758 | source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8759 | filetype_plugin = TRUE; |
| 8760 | } |
| 8761 | if (indent) |
| 8762 | { |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 8763 | source_runtime((char_u *)INDENT_FILE, DIP_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8764 | filetype_indent = TRUE; |
| 8765 | } |
| 8766 | } |
| 8767 | if (*arg == 'd') |
| 8768 | { |
Bram Moolenaar | 1610d05 | 2016-06-09 22:53:01 +0200 | [diff] [blame] | 8769 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 8770 | do_modelines(0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8771 | } |
| 8772 | } |
| 8773 | else if (STRCMP(arg, "off") == 0) |
| 8774 | { |
| 8775 | if (plugin || indent) |
| 8776 | { |
| 8777 | if (plugin) |
| 8778 | { |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 8779 | source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8780 | filetype_plugin = FALSE; |
| 8781 | } |
| 8782 | if (indent) |
| 8783 | { |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 8784 | source_runtime((char_u *)INDOFF_FILE, DIP_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8785 | filetype_indent = FALSE; |
| 8786 | } |
| 8787 | } |
| 8788 | else |
| 8789 | { |
Bram Moolenaar | 7f8989d | 2016-03-12 22:11:39 +0100 | [diff] [blame] | 8790 | source_runtime((char_u *)FTOFF_FILE, DIP_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8791 | filetype_detect = FALSE; |
| 8792 | } |
| 8793 | } |
| 8794 | else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8795 | semsg(_(e_invarg2), arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8796 | } |
| 8797 | |
| 8798 | /* |
Bram Moolenaar | 3e54569 | 2017-06-04 19:00:32 +0200 | [diff] [blame] | 8799 | * ":setfiletype [FALLBACK] {name}" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8800 | */ |
| 8801 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8802 | ex_setfiletype(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8803 | { |
| 8804 | if (!did_filetype) |
Bram Moolenaar | 3e54569 | 2017-06-04 19:00:32 +0200 | [diff] [blame] | 8805 | { |
| 8806 | char_u *arg = eap->arg; |
| 8807 | |
| 8808 | if (STRNCMP(arg, "FALLBACK ", 9) == 0) |
| 8809 | arg += 9; |
| 8810 | |
| 8811 | set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL); |
| 8812 | if (arg != eap->arg) |
| 8813 | did_filetype = FALSE; |
| 8814 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8815 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8816 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8817 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8818 | ex_digraphs(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8819 | { |
| 8820 | #ifdef FEAT_DIGRAPHS |
| 8821 | if (*eap->arg != NUL) |
| 8822 | putdigraph(eap->arg); |
| 8823 | else |
Bram Moolenaar | eae8ae1 | 2018-12-14 18:53:02 +0100 | [diff] [blame] | 8824 | listdigraphs(eap->forceit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8825 | #else |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 8826 | emsg(_("E196: No digraphs in this version")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8827 | #endif |
| 8828 | } |
| 8829 | |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 8830 | #if defined(FEAT_SEARCH_EXTRA) || defined(PROTO) |
| 8831 | void |
| 8832 | set_no_hlsearch(int flag) |
| 8833 | { |
| 8834 | no_hlsearch = flag; |
| 8835 | # ifdef FEAT_EVAL |
| 8836 | set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls); |
| 8837 | # endif |
| 8838 | } |
| 8839 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8840 | /* |
| 8841 | * ":nohlsearch" |
| 8842 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8843 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8844 | ex_nohlsearch(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8845 | { |
Bram Moolenaar | 451fc7b | 2018-04-27 22:53:07 +0200 | [diff] [blame] | 8846 | set_no_hlsearch(TRUE); |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 8847 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8848 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8849 | #endif |
| 8850 | |
| 8851 | #ifdef FEAT_CRYPT |
| 8852 | /* |
| 8853 | * ":X": Get crypt key |
| 8854 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8855 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8856 | ex_X(exarg_T *eap UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8857 | { |
Bram Moolenaar | 3a0c908 | 2014-11-12 15:15:42 +0100 | [diff] [blame] | 8858 | crypt_check_current_method(); |
Bram Moolenaar | 8f4ac01 | 2014-08-10 13:38:34 +0200 | [diff] [blame] | 8859 | (void)crypt_get_key(TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8860 | } |
| 8861 | #endif |
| 8862 | |
| 8863 | #ifdef FEAT_FOLDING |
| 8864 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8865 | ex_fold(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8866 | { |
| 8867 | if (foldManualAllowed(TRUE)) |
| 8868 | foldCreate(eap->line1, eap->line2); |
| 8869 | } |
| 8870 | |
| 8871 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8872 | ex_foldopen(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8873 | { |
| 8874 | opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen, |
| 8875 | eap->forceit, FALSE); |
| 8876 | } |
| 8877 | |
| 8878 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 8879 | ex_folddo(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8880 | { |
| 8881 | linenr_T lnum; |
| 8882 | |
Bram Moolenaar | 4facea3 | 2019-10-12 20:17:40 +0200 | [diff] [blame] | 8883 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 8884 | start_global_changes(); |
Bram Moolenaar | 4facea3 | 2019-10-12 20:17:40 +0200 | [diff] [blame] | 8885 | # endif |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 8886 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8887 | // First set the marks for all lines closed/open. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8888 | for (lnum = eap->line1; lnum <= eap->line2; ++lnum) |
| 8889 | if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed)) |
| 8890 | ml_setmarked(lnum); |
| 8891 | |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8892 | // Execute the command on the marked lines. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8893 | global_exe(eap->arg); |
Bram Moolenaar | 217e1b8 | 2019-12-01 21:41:28 +0100 | [diff] [blame] | 8894 | ml_clearmarked(); // clear rest of the marks |
Bram Moolenaar | 4facea3 | 2019-10-12 20:17:40 +0200 | [diff] [blame] | 8895 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 6b1ee34 | 2014-08-06 18:17:11 +0200 | [diff] [blame] | 8896 | end_global_changes(); |
Bram Moolenaar | 4facea3 | 2019-10-12 20:17:40 +0200 | [diff] [blame] | 8897 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8898 | } |
| 8899 | #endif |
Bram Moolenaar | f5291f3 | 2017-09-14 22:55:37 +0200 | [diff] [blame] | 8900 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 8901 | #if defined(FEAT_QUICKFIX) || defined(PROTO) |
Bram Moolenaar | 3966595 | 2018-08-15 20:59:48 +0200 | [diff] [blame] | 8902 | /* |
| 8903 | * Returns TRUE if the supplied Ex cmdidx is for a location list command |
| 8904 | * instead of a quickfix command. |
| 8905 | */ |
| 8906 | int |
| 8907 | is_loclist_cmd(int cmdidx) |
| 8908 | { |
Bram Moolenaar | 74c8be2 | 2018-08-23 22:51:40 +0200 | [diff] [blame] | 8909 | if (cmdidx < 0 || cmdidx >= CMD_SIZE) |
Bram Moolenaar | 3966595 | 2018-08-15 20:59:48 +0200 | [diff] [blame] | 8910 | return FALSE; |
| 8911 | return cmdnames[cmdidx].cmd_name[0] == 'l'; |
| 8912 | } |
| 8913 | #endif |
| 8914 | |
Bram Moolenaar | 4facea3 | 2019-10-12 20:17:40 +0200 | [diff] [blame] | 8915 | #if defined(FEAT_TIMERS) || defined(PROTO) |
Bram Moolenaar | f5291f3 | 2017-09-14 22:55:37 +0200 | [diff] [blame] | 8916 | int |
| 8917 | get_pressedreturn(void) |
| 8918 | { |
| 8919 | return ex_pressedreturn; |
| 8920 | } |
| 8921 | |
| 8922 | void |
| 8923 | set_pressedreturn(int val) |
| 8924 | { |
| 8925 | ex_pressedreturn = val; |
| 8926 | } |
| 8927 | #endif |