Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ex_docmd.c: functions for executing an Ex command line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #ifdef HAVE_FCNTL_H |
| 17 | # include <fcntl.h> /* for chdir() */ |
| 18 | #endif |
| 19 | |
| 20 | static int quitmore = 0; |
| 21 | static int ex_pressedreturn = FALSE; |
| 22 | #ifndef FEAT_PRINTER |
| 23 | # define ex_hardcopy ex_ni |
| 24 | #endif |
| 25 | |
| 26 | #ifdef FEAT_USR_CMDS |
| 27 | typedef struct ucmd |
| 28 | { |
| 29 | char_u *uc_name; /* The command name */ |
| 30 | long_u uc_argt; /* The argument type */ |
| 31 | char_u *uc_rep; /* The command's replacement string */ |
| 32 | long uc_def; /* The default value for a range/count */ |
| 33 | scid_T uc_scriptID; /* SID where the command was defined */ |
| 34 | int uc_compl; /* completion type */ |
| 35 | # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 36 | char_u *uc_compl_arg; /* completion argument if any */ |
| 37 | # endif |
| 38 | } ucmd_T; |
| 39 | |
| 40 | #define UC_BUFFER 1 /* -buffer: local to current buffer */ |
| 41 | |
Bram Moolenaar | 2c29bee | 2005-06-01 21:46:07 +0000 | [diff] [blame] | 42 | static garray_T ucmds = {0, 0, sizeof(ucmd_T), 4, NULL}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 43 | |
| 44 | #define USER_CMD(i) (&((ucmd_T *)(ucmds.ga_data))[i]) |
| 45 | #define USER_CMD_GA(gap, i) (&((ucmd_T *)((gap)->ga_data))[i]) |
| 46 | |
| 47 | static void do_ucmd __ARGS((exarg_T *eap)); |
| 48 | static void ex_command __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | static void ex_delcommand __ARGS((exarg_T *eap)); |
| 50 | # ifdef FEAT_CMDL_COMPL |
| 51 | static char_u *get_user_command_name __ARGS((int idx)); |
| 52 | # endif |
| 53 | |
| 54 | #else |
| 55 | # define ex_command ex_ni |
| 56 | # define ex_comclear ex_ni |
| 57 | # define ex_delcommand ex_ni |
| 58 | #endif |
| 59 | |
| 60 | #ifdef FEAT_EVAL |
| 61 | static char_u *do_one_cmd __ARGS((char_u **, int, struct condstack *, char_u *(*getline)(int, void *, int), void *cookie)); |
| 62 | #else |
| 63 | static char_u *do_one_cmd __ARGS((char_u **, int, char_u *(*getline)(int, void *, int), void *cookie)); |
| 64 | static int if_level = 0; /* depth in :if */ |
| 65 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | static char_u *find_command __ARGS((exarg_T *eap, int *full)); |
| 67 | |
| 68 | static void ex_abbreviate __ARGS((exarg_T *eap)); |
| 69 | static void ex_map __ARGS((exarg_T *eap)); |
| 70 | static void ex_unmap __ARGS((exarg_T *eap)); |
| 71 | static void ex_mapclear __ARGS((exarg_T *eap)); |
| 72 | static void ex_abclear __ARGS((exarg_T *eap)); |
| 73 | #ifndef FEAT_MENU |
| 74 | # define ex_emenu ex_ni |
| 75 | # define ex_menu ex_ni |
| 76 | # define ex_menutranslate ex_ni |
| 77 | #endif |
| 78 | #ifdef FEAT_AUTOCMD |
| 79 | static void ex_autocmd __ARGS((exarg_T *eap)); |
| 80 | static void ex_doautocmd __ARGS((exarg_T *eap)); |
| 81 | #else |
| 82 | # define ex_autocmd ex_ni |
| 83 | # define ex_doautocmd ex_ni |
| 84 | # define ex_doautoall ex_ni |
| 85 | #endif |
| 86 | #ifdef FEAT_LISTCMDS |
| 87 | static void ex_bunload __ARGS((exarg_T *eap)); |
| 88 | static void ex_buffer __ARGS((exarg_T *eap)); |
| 89 | static void ex_bmodified __ARGS((exarg_T *eap)); |
| 90 | static void ex_bnext __ARGS((exarg_T *eap)); |
| 91 | static void ex_bprevious __ARGS((exarg_T *eap)); |
| 92 | static void ex_brewind __ARGS((exarg_T *eap)); |
| 93 | static void ex_blast __ARGS((exarg_T *eap)); |
| 94 | #else |
| 95 | # define ex_bunload ex_ni |
| 96 | # define ex_buffer ex_ni |
| 97 | # define ex_bmodified ex_ni |
| 98 | # define ex_bnext ex_ni |
| 99 | # define ex_bprevious ex_ni |
| 100 | # define ex_brewind ex_ni |
| 101 | # define ex_blast ex_ni |
| 102 | # define buflist_list ex_ni |
| 103 | # define ex_checktime ex_ni |
| 104 | #endif |
| 105 | #if !defined(FEAT_LISTCMDS) || !defined(FEAT_WINDOWS) |
| 106 | # define ex_buffer_all ex_ni |
| 107 | #endif |
| 108 | static char_u *getargcmd __ARGS((char_u **)); |
| 109 | static char_u *skip_cmd_arg __ARGS((char_u *p, int rembs)); |
| 110 | static int getargopt __ARGS((exarg_T *eap)); |
| 111 | #ifndef FEAT_QUICKFIX |
| 112 | # define ex_make ex_ni |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 113 | # define ex_cbuffer ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 114 | # define ex_cc ex_ni |
| 115 | # define ex_cnext ex_ni |
| 116 | # define ex_cfile ex_ni |
| 117 | # define qf_list ex_ni |
| 118 | # define qf_age ex_ni |
| 119 | # define ex_helpgrep ex_ni |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 120 | # define ex_vimgrep ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 121 | #endif |
| 122 | #if !defined(FEAT_QUICKFIX) || !defined(FEAT_WINDOWS) |
| 123 | # define ex_cclose ex_ni |
| 124 | # define ex_copen ex_ni |
| 125 | # define ex_cwindow ex_ni |
| 126 | #endif |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 127 | #if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL) |
| 128 | # define ex_cexpr ex_ni |
| 129 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 130 | |
| 131 | static int check_more __ARGS((int, int)); |
| 132 | static linenr_T get_address __ARGS((char_u **, int skip, int to_other_file)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 133 | static void get_flags __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 134 | #if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 135 | || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 136 | static void ex_script_ni __ARGS((exarg_T *eap)); |
| 137 | #endif |
| 138 | static char_u *invalid_range __ARGS((exarg_T *eap)); |
| 139 | static void correct_range __ARGS((exarg_T *eap)); |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 140 | #ifdef FEAT_QUICKFIX |
| 141 | static char_u *replace_makeprg __ARGS((exarg_T *eap, char_u *p, char_u **cmdlinep)); |
| 142 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 143 | static char_u *repl_cmdline __ARGS((exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep)); |
| 144 | static void ex_highlight __ARGS((exarg_T *eap)); |
| 145 | static void ex_colorscheme __ARGS((exarg_T *eap)); |
| 146 | static void ex_quit __ARGS((exarg_T *eap)); |
| 147 | static void ex_cquit __ARGS((exarg_T *eap)); |
| 148 | static void ex_quit_all __ARGS((exarg_T *eap)); |
| 149 | #ifdef FEAT_WINDOWS |
| 150 | static void ex_close __ARGS((exarg_T *eap)); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 151 | static void ex_win_close __ARGS((int forceit, win_T *win, tabpage_T *tp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 152 | static void ex_only __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 153 | static void ex_resize __ARGS((exarg_T *eap)); |
| 154 | static void ex_stag __ARGS((exarg_T *eap)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 155 | static void ex_tabclose __ARGS((exarg_T *eap)); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 156 | static void ex_tabonly __ARGS((exarg_T *eap)); |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 157 | static void ex_tabnext __ARGS((exarg_T *eap)); |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 158 | static void ex_tabmove __ARGS((exarg_T *eap)); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 159 | static void ex_tabs __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 160 | #else |
| 161 | # define ex_close ex_ni |
| 162 | # define ex_only ex_ni |
| 163 | # define ex_all ex_ni |
| 164 | # define ex_resize ex_ni |
| 165 | # define ex_splitview ex_ni |
| 166 | # define ex_stag ex_ni |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 167 | # define ex_tabnext ex_ni |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 168 | # define ex_tabmove ex_ni |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 169 | # define ex_tabs ex_ni |
| 170 | # define ex_tabclose ex_ni |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 171 | # define ex_tabonly ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 172 | #endif |
| 173 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 174 | static void ex_pclose __ARGS((exarg_T *eap)); |
| 175 | static void ex_ptag __ARGS((exarg_T *eap)); |
| 176 | static void ex_pedit __ARGS((exarg_T *eap)); |
| 177 | #else |
| 178 | # define ex_pclose ex_ni |
| 179 | # define ex_ptag ex_ni |
| 180 | # define ex_pedit ex_ni |
| 181 | #endif |
| 182 | static void ex_hide __ARGS((exarg_T *eap)); |
| 183 | static void ex_stop __ARGS((exarg_T *eap)); |
| 184 | static void ex_exit __ARGS((exarg_T *eap)); |
| 185 | static void ex_print __ARGS((exarg_T *eap)); |
| 186 | #ifdef FEAT_BYTEOFF |
| 187 | static void ex_goto __ARGS((exarg_T *eap)); |
| 188 | #else |
| 189 | # define ex_goto ex_ni |
| 190 | #endif |
| 191 | static void ex_shell __ARGS((exarg_T *eap)); |
| 192 | static void ex_preserve __ARGS((exarg_T *eap)); |
| 193 | static void ex_recover __ARGS((exarg_T *eap)); |
| 194 | #ifndef FEAT_LISTCMDS |
| 195 | # define ex_argedit ex_ni |
| 196 | # define ex_argadd ex_ni |
| 197 | # define ex_argdelete ex_ni |
| 198 | # define ex_listdo ex_ni |
| 199 | #endif |
| 200 | static void ex_mode __ARGS((exarg_T *eap)); |
| 201 | static void ex_wrongmodifier __ARGS((exarg_T *eap)); |
| 202 | static void ex_find __ARGS((exarg_T *eap)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 203 | static void ex_open __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 204 | static void ex_edit __ARGS((exarg_T *eap)); |
| 205 | #if !defined(FEAT_GUI) && !defined(FEAT_CLIENTSERVER) |
| 206 | # define ex_drop ex_ni |
| 207 | #endif |
| 208 | #ifndef FEAT_GUI |
| 209 | # define ex_gui ex_nogui |
| 210 | static void ex_nogui __ARGS((exarg_T *eap)); |
| 211 | #endif |
| 212 | #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) |
| 213 | static void ex_tearoff __ARGS((exarg_T *eap)); |
| 214 | #else |
| 215 | # define ex_tearoff ex_ni |
| 216 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 217 | #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 218 | static void ex_popup __ARGS((exarg_T *eap)); |
| 219 | #else |
| 220 | # define ex_popup ex_ni |
| 221 | #endif |
| 222 | #ifndef FEAT_GUI_MSWIN |
| 223 | # define ex_simalt ex_ni |
| 224 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 225 | #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] | 226 | # define gui_mch_find_dialog ex_ni |
| 227 | # define gui_mch_replace_dialog ex_ni |
| 228 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 229 | #if !defined(FEAT_GUI_GTK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | # define ex_helpfind ex_ni |
| 231 | #endif |
| 232 | #ifndef FEAT_CSCOPE |
| 233 | # define do_cscope ex_ni |
| 234 | # define do_scscope ex_ni |
| 235 | # define do_cstag ex_ni |
| 236 | #endif |
| 237 | #ifndef FEAT_SYN_HL |
| 238 | # define ex_syntax ex_ni |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 239 | #endif |
| 240 | #ifndef FEAT_SPELL |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 241 | # define ex_spell ex_ni |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 242 | # define ex_mkspell ex_ni |
Bram Moolenaar | f417f2b | 2005-06-23 22:29:21 +0000 | [diff] [blame] | 243 | # define ex_spelldump ex_ni |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 244 | # define ex_spellinfo ex_ni |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 245 | # define ex_spellrepall ex_ni |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 246 | #endif |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 247 | #ifndef FEAT_MZSCHEME |
| 248 | # define ex_mzscheme ex_script_ni |
| 249 | # define ex_mzfile ex_ni |
| 250 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 251 | #ifndef FEAT_PERL |
| 252 | # define ex_perl ex_script_ni |
| 253 | # define ex_perldo ex_ni |
| 254 | #endif |
| 255 | #ifndef FEAT_PYTHON |
| 256 | # define ex_python ex_script_ni |
| 257 | # define ex_pyfile ex_ni |
| 258 | #endif |
| 259 | #ifndef FEAT_TCL |
| 260 | # define ex_tcl ex_script_ni |
| 261 | # define ex_tcldo ex_ni |
| 262 | # define ex_tclfile ex_ni |
| 263 | #endif |
| 264 | #ifndef FEAT_RUBY |
| 265 | # define ex_ruby ex_script_ni |
| 266 | # define ex_rubydo ex_ni |
| 267 | # define ex_rubyfile ex_ni |
| 268 | #endif |
| 269 | #ifndef FEAT_SNIFF |
| 270 | # define ex_sniff ex_ni |
| 271 | #endif |
| 272 | #ifndef FEAT_KEYMAP |
| 273 | # define ex_loadkeymap ex_ni |
| 274 | #endif |
| 275 | static void ex_swapname __ARGS((exarg_T *eap)); |
| 276 | static void ex_syncbind __ARGS((exarg_T *eap)); |
| 277 | static void ex_read __ARGS((exarg_T *eap)); |
| 278 | static void ex_cd __ARGS((exarg_T *eap)); |
| 279 | static void ex_pwd __ARGS((exarg_T *eap)); |
| 280 | static void ex_equal __ARGS((exarg_T *eap)); |
| 281 | static void ex_sleep __ARGS((exarg_T *eap)); |
| 282 | static void do_exmap __ARGS((exarg_T *eap, int isabbrev)); |
| 283 | static void ex_winsize __ARGS((exarg_T *eap)); |
| 284 | #ifdef FEAT_WINDOWS |
| 285 | static void ex_wincmd __ARGS((exarg_T *eap)); |
| 286 | #else |
| 287 | # define ex_wincmd ex_ni |
| 288 | #endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 289 | #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 290 | static void ex_winpos __ARGS((exarg_T *eap)); |
| 291 | #else |
| 292 | # define ex_winpos ex_ni |
| 293 | #endif |
| 294 | static void ex_operators __ARGS((exarg_T *eap)); |
| 295 | static void ex_put __ARGS((exarg_T *eap)); |
| 296 | static void ex_copymove __ARGS((exarg_T *eap)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 297 | static void ex_may_print __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 298 | static void ex_submagic __ARGS((exarg_T *eap)); |
| 299 | static void ex_join __ARGS((exarg_T *eap)); |
| 300 | static void ex_at __ARGS((exarg_T *eap)); |
| 301 | static void ex_bang __ARGS((exarg_T *eap)); |
| 302 | static void ex_undo __ARGS((exarg_T *eap)); |
| 303 | static void ex_redo __ARGS((exarg_T *eap)); |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 304 | static void ex_later __ARGS((exarg_T *eap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 305 | static void ex_redir __ARGS((exarg_T *eap)); |
| 306 | static void ex_redraw __ARGS((exarg_T *eap)); |
| 307 | static void ex_redrawstatus __ARGS((exarg_T *eap)); |
| 308 | static void close_redir __ARGS((void)); |
| 309 | static void ex_mkrc __ARGS((exarg_T *eap)); |
| 310 | static void ex_mark __ARGS((exarg_T *eap)); |
| 311 | #ifdef FEAT_USR_CMDS |
| 312 | static char_u *uc_fun_cmd __ARGS((void)); |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 313 | static char_u *find_ucmd __ARGS((exarg_T *eap, char_u *p, int *full, expand_T *xp, int *compl)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | #endif |
| 315 | #ifdef FEAT_EX_EXTRA |
| 316 | static void ex_normal __ARGS((exarg_T *eap)); |
| 317 | static void ex_startinsert __ARGS((exarg_T *eap)); |
| 318 | static void ex_stopinsert __ARGS((exarg_T *eap)); |
| 319 | #else |
| 320 | # define ex_normal ex_ni |
| 321 | # define ex_align ex_ni |
| 322 | # define ex_retab ex_ni |
| 323 | # define ex_startinsert ex_ni |
| 324 | # define ex_stopinsert ex_ni |
| 325 | # define ex_helptags ex_ni |
Bram Moolenaar | f95dc3b | 2005-05-22 22:02:25 +0000 | [diff] [blame] | 326 | # define ex_sort ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 327 | #endif |
| 328 | #ifdef FEAT_FIND_ID |
| 329 | static void ex_checkpath __ARGS((exarg_T *eap)); |
| 330 | static void ex_findpat __ARGS((exarg_T *eap)); |
| 331 | #else |
| 332 | # define ex_findpat ex_ni |
| 333 | # define ex_checkpath ex_ni |
| 334 | #endif |
| 335 | #if defined(FEAT_FIND_ID) && defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 336 | static void ex_psearch __ARGS((exarg_T *eap)); |
| 337 | #else |
| 338 | # define ex_psearch ex_ni |
| 339 | #endif |
| 340 | static void ex_tag __ARGS((exarg_T *eap)); |
| 341 | static void ex_tag_cmd __ARGS((exarg_T *eap, char_u *name)); |
| 342 | #ifndef FEAT_EVAL |
| 343 | # define ex_scriptnames ex_ni |
| 344 | # define ex_finish ex_ni |
| 345 | # define ex_echo ex_ni |
| 346 | # define ex_echohl ex_ni |
| 347 | # define ex_execute ex_ni |
| 348 | # define ex_call ex_ni |
| 349 | # define ex_if ex_ni |
| 350 | # define ex_endif ex_ni |
| 351 | # define ex_else ex_ni |
| 352 | # define ex_while ex_ni |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 353 | # define ex_for ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 354 | # define ex_continue ex_ni |
| 355 | # define ex_break ex_ni |
| 356 | # define ex_endwhile ex_ni |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 357 | # define ex_endfor ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 358 | # define ex_throw ex_ni |
| 359 | # define ex_try ex_ni |
| 360 | # define ex_catch ex_ni |
| 361 | # define ex_finally ex_ni |
| 362 | # define ex_endtry ex_ni |
| 363 | # define ex_endfunction ex_ni |
| 364 | # define ex_let ex_ni |
| 365 | # define ex_unlet ex_ni |
Bram Moolenaar | 65c1b01 | 2005-01-31 19:02:28 +0000 | [diff] [blame] | 366 | # define ex_lockvar ex_ni |
| 367 | # define ex_unlockvar ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 368 | # define ex_function ex_ni |
| 369 | # define ex_delfunction ex_ni |
| 370 | # define ex_return ex_ni |
| 371 | #endif |
| 372 | static char_u *arg_all __ARGS((void)); |
| 373 | #ifdef FEAT_SESSION |
| 374 | static int makeopens __ARGS((FILE *fd, char_u *dirnow)); |
| 375 | static int put_view __ARGS((FILE *fd, win_T *wp, int add_edit, unsigned *flagp)); |
| 376 | static void ex_loadview __ARGS((exarg_T *eap)); |
| 377 | static char_u *get_view_file __ARGS((int c)); |
| 378 | #else |
| 379 | # define ex_loadview ex_ni |
| 380 | #endif |
| 381 | #ifndef FEAT_EVAL |
| 382 | # define ex_compiler ex_ni |
| 383 | #endif |
| 384 | #ifdef FEAT_VIMINFO |
| 385 | static void ex_viminfo __ARGS((exarg_T *eap)); |
| 386 | #else |
| 387 | # define ex_viminfo ex_ni |
| 388 | #endif |
| 389 | static void ex_behave __ARGS((exarg_T *eap)); |
| 390 | #ifdef FEAT_AUTOCMD |
| 391 | static void ex_filetype __ARGS((exarg_T *eap)); |
| 392 | static void ex_setfiletype __ARGS((exarg_T *eap)); |
| 393 | #else |
| 394 | # define ex_filetype ex_ni |
| 395 | # define ex_setfiletype ex_ni |
| 396 | #endif |
| 397 | #ifndef FEAT_DIFF |
Bram Moolenaar | 2df6dcc | 2004-07-12 15:53:54 +0000 | [diff] [blame] | 398 | # define ex_diffoff ex_ni |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 399 | # define ex_diffpatch ex_ni |
| 400 | # define ex_diffgetput ex_ni |
| 401 | # define ex_diffsplit ex_ni |
| 402 | # define ex_diffthis ex_ni |
| 403 | # define ex_diffupdate ex_ni |
| 404 | #endif |
| 405 | static void ex_digraphs __ARGS((exarg_T *eap)); |
| 406 | static void ex_set __ARGS((exarg_T *eap)); |
| 407 | #if !defined(FEAT_EVAL) || !defined(FEAT_AUTOCMD) |
| 408 | # define ex_options ex_ni |
| 409 | #endif |
| 410 | #ifdef FEAT_SEARCH_EXTRA |
| 411 | static void ex_nohlsearch __ARGS((exarg_T *eap)); |
| 412 | static void ex_match __ARGS((exarg_T *eap)); |
| 413 | #else |
| 414 | # define ex_nohlsearch ex_ni |
| 415 | # define ex_match ex_ni |
| 416 | #endif |
| 417 | #ifdef FEAT_CRYPT |
| 418 | static void ex_X __ARGS((exarg_T *eap)); |
| 419 | #else |
| 420 | # define ex_X ex_ni |
| 421 | #endif |
| 422 | #ifdef FEAT_FOLDING |
| 423 | static void ex_fold __ARGS((exarg_T *eap)); |
| 424 | static void ex_foldopen __ARGS((exarg_T *eap)); |
| 425 | static void ex_folddo __ARGS((exarg_T *eap)); |
| 426 | #else |
| 427 | # define ex_fold ex_ni |
| 428 | # define ex_foldopen ex_ni |
| 429 | # define ex_folddo ex_ni |
| 430 | #endif |
| 431 | #if !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 432 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))) |
| 433 | # define ex_language ex_ni |
| 434 | #endif |
| 435 | #ifndef FEAT_SIGNS |
| 436 | # define ex_sign ex_ni |
| 437 | #endif |
| 438 | #ifndef FEAT_SUN_WORKSHOP |
| 439 | # define ex_wsverb ex_ni |
| 440 | #endif |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 441 | #ifndef FEAT_NETBEANS_INTG |
| 442 | # define ex_nbkey ex_ni |
| 443 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 444 | |
| 445 | #ifndef FEAT_EVAL |
| 446 | # define ex_debug ex_ni |
| 447 | # define ex_breakadd ex_ni |
| 448 | # define ex_debuggreedy ex_ni |
| 449 | # define ex_breakdel ex_ni |
| 450 | # define ex_breaklist ex_ni |
| 451 | #endif |
| 452 | |
| 453 | #ifndef FEAT_CMDHIST |
| 454 | # define ex_history ex_ni |
| 455 | #endif |
| 456 | #ifndef FEAT_JUMPLIST |
| 457 | # define ex_jumps ex_ni |
| 458 | # define ex_changes ex_ni |
| 459 | #endif |
| 460 | |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 461 | #ifndef FEAT_PROFILE |
| 462 | # define ex_profile ex_ni |
| 463 | #endif |
| 464 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 465 | /* |
| 466 | * Declare cmdnames[]. |
| 467 | */ |
| 468 | #define DO_DECLARE_EXCMD |
| 469 | #include "ex_cmds.h" |
| 470 | |
| 471 | /* |
| 472 | * Table used to quickly search for a command, based on its first character. |
| 473 | */ |
Bram Moolenaar | 2c29bee | 2005-06-01 21:46:07 +0000 | [diff] [blame] | 474 | static cmdidx_T cmdidxs[27] = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 475 | { |
| 476 | CMD_append, |
| 477 | CMD_buffer, |
| 478 | CMD_change, |
| 479 | CMD_delete, |
| 480 | CMD_edit, |
| 481 | CMD_file, |
| 482 | CMD_global, |
| 483 | CMD_help, |
| 484 | CMD_insert, |
| 485 | CMD_join, |
| 486 | CMD_k, |
| 487 | CMD_list, |
| 488 | CMD_move, |
| 489 | CMD_next, |
| 490 | CMD_open, |
| 491 | CMD_print, |
| 492 | CMD_quit, |
| 493 | CMD_read, |
| 494 | CMD_substitute, |
| 495 | CMD_t, |
| 496 | CMD_undo, |
| 497 | CMD_vglobal, |
| 498 | CMD_write, |
| 499 | CMD_xit, |
| 500 | CMD_yank, |
| 501 | CMD_z, |
| 502 | CMD_bang |
| 503 | }; |
| 504 | |
| 505 | static char_u dollar_command[2] = {'$', 0}; |
| 506 | |
| 507 | |
| 508 | #ifdef FEAT_EVAL |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 509 | /* Struct for storing a line inside a while/for loop */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | typedef struct |
| 511 | { |
| 512 | char_u *line; /* command line */ |
| 513 | linenr_T lnum; /* sourcing_lnum of the line */ |
| 514 | } wcmd_T; |
| 515 | |
| 516 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 517 | * 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] | 518 | * This is required, because do_one_cmd() may invoke ex_function(), which |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 519 | * reads more lines that may come from the while/for loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 520 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 521 | struct loop_cookie |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 522 | { |
| 523 | garray_T *lines_gap; /* growarray with line info */ |
| 524 | int current_line; /* last read line from growarray */ |
| 525 | int repeating; /* TRUE when looping a second time */ |
| 526 | /* When "repeating" is FALSE use "getline" and "cookie" to get lines */ |
| 527 | char_u *(*getline) __ARGS((int, void *, int)); |
| 528 | void *cookie; |
| 529 | }; |
| 530 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 531 | static char_u *get_loop_line __ARGS((int c, void *cookie, int indent)); |
| 532 | static int store_loop_line __ARGS((garray_T *gap, char_u *line)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 533 | static void free_cmdlines __ARGS((garray_T *gap)); |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 534 | |
| 535 | /* Struct to save a few things while debugging. Used in do_cmdline() only. */ |
| 536 | struct dbg_stuff |
| 537 | { |
| 538 | int trylevel; |
| 539 | int force_abort; |
| 540 | except_T *caught_stack; |
| 541 | char_u *vv_exception; |
| 542 | char_u *vv_throwpoint; |
| 543 | int did_emsg; |
| 544 | int got_int; |
| 545 | int did_throw; |
| 546 | int need_rethrow; |
| 547 | int check_cstack; |
| 548 | except_T *current_exception; |
| 549 | }; |
| 550 | |
| 551 | static void save_dbg_stuff __ARGS((struct dbg_stuff *dsp)); |
| 552 | static void restore_dbg_stuff __ARGS((struct dbg_stuff *dsp)); |
| 553 | |
| 554 | static void |
| 555 | save_dbg_stuff(dsp) |
| 556 | struct dbg_stuff *dsp; |
| 557 | { |
| 558 | dsp->trylevel = trylevel; trylevel = 0; |
| 559 | dsp->force_abort = force_abort; force_abort = FALSE; |
| 560 | dsp->caught_stack = caught_stack; caught_stack = NULL; |
| 561 | dsp->vv_exception = v_exception(NULL); |
| 562 | dsp->vv_throwpoint = v_throwpoint(NULL); |
| 563 | |
| 564 | /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */ |
| 565 | dsp->did_emsg = did_emsg; did_emsg = FALSE; |
| 566 | dsp->got_int = got_int; got_int = FALSE; |
| 567 | dsp->did_throw = did_throw; did_throw = FALSE; |
| 568 | dsp->need_rethrow = need_rethrow; need_rethrow = FALSE; |
| 569 | dsp->check_cstack = check_cstack; check_cstack = FALSE; |
| 570 | dsp->current_exception = current_exception; current_exception = NULL; |
| 571 | } |
| 572 | |
| 573 | static void |
| 574 | restore_dbg_stuff(dsp) |
| 575 | struct dbg_stuff *dsp; |
| 576 | { |
| 577 | suppress_errthrow = FALSE; |
| 578 | trylevel = dsp->trylevel; |
| 579 | force_abort = dsp->force_abort; |
| 580 | caught_stack = dsp->caught_stack; |
| 581 | (void)v_exception(dsp->vv_exception); |
| 582 | (void)v_throwpoint(dsp->vv_throwpoint); |
| 583 | did_emsg = dsp->did_emsg; |
| 584 | got_int = dsp->got_int; |
| 585 | did_throw = dsp->did_throw; |
| 586 | need_rethrow = dsp->need_rethrow; |
| 587 | check_cstack = dsp->check_cstack; |
| 588 | current_exception = dsp->current_exception; |
| 589 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 590 | #endif |
| 591 | |
| 592 | |
| 593 | /* |
| 594 | * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi" |
| 595 | * command is given. |
| 596 | */ |
| 597 | void |
| 598 | do_exmode(improved) |
| 599 | int improved; /* TRUE for "improved Ex" mode */ |
| 600 | { |
| 601 | int save_msg_scroll; |
| 602 | int prev_msg_row; |
| 603 | linenr_T prev_line; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 604 | int changedtick; |
| 605 | |
| 606 | if (improved) |
| 607 | exmode_active = EXMODE_VIM; |
| 608 | else |
| 609 | exmode_active = EXMODE_NORMAL; |
| 610 | State = NORMAL; |
| 611 | |
| 612 | /* When using ":global /pat/ visual" and then "Q" we return to continue |
| 613 | * the :global command. */ |
| 614 | if (global_busy) |
| 615 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | |
| 617 | save_msg_scroll = msg_scroll; |
| 618 | ++RedrawingDisabled; /* don't redisplay the window */ |
| 619 | ++no_wait_return; /* don't wait for return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 620 | #ifdef FEAT_GUI |
| 621 | /* Ignore scrollbar and mouse events in Ex mode */ |
| 622 | ++hold_gui_events; |
| 623 | #endif |
| 624 | #ifdef FEAT_SNIFF |
| 625 | want_sniff_request = 0; /* No K_SNIFF wanted */ |
| 626 | #endif |
| 627 | |
| 628 | MSG(_("Entering Ex mode. Type \"visual\" to go to Normal mode.")); |
| 629 | while (exmode_active) |
| 630 | { |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 631 | #ifdef FEAT_EX_EXTRA |
| 632 | /* Check for a ":normal" command and no more characters left. */ |
| 633 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 634 | { |
| 635 | exmode_active = FALSE; |
| 636 | break; |
| 637 | } |
| 638 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 639 | msg_scroll = TRUE; |
| 640 | need_wait_return = FALSE; |
| 641 | ex_pressedreturn = FALSE; |
| 642 | ex_no_reprint = FALSE; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 643 | changedtick = curbuf->b_changedtick; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | prev_msg_row = msg_row; |
| 645 | prev_line = curwin->w_cursor.lnum; |
| 646 | #ifdef FEAT_SNIFF |
| 647 | ProcessSniffRequests(); |
| 648 | #endif |
| 649 | if (improved) |
| 650 | { |
| 651 | cmdline_row = msg_row; |
| 652 | do_cmdline(NULL, getexline, NULL, 0); |
| 653 | } |
| 654 | else |
| 655 | do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT); |
| 656 | lines_left = Rows - 1; |
| 657 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 658 | if ((prev_line != curwin->w_cursor.lnum |
| 659 | || changedtick != curbuf->b_changedtick) && !ex_no_reprint) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 660 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 661 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 662 | EMSG(_(e_emptybuf)); |
| 663 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 664 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 665 | if (ex_pressedreturn) |
| 666 | { |
| 667 | /* go up one line, to overwrite the ":<CR>" line, so the |
| 668 | * output doensn't contain empty lines. */ |
| 669 | msg_row = prev_msg_row; |
| 670 | if (prev_msg_row == Rows - 1) |
| 671 | msg_row--; |
| 672 | } |
| 673 | msg_col = 0; |
| 674 | print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE); |
| 675 | msg_clr_eos(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 676 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 678 | else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */ |
| 679 | { |
| 680 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 681 | EMSG(_(e_emptybuf)); |
| 682 | else |
| 683 | EMSG(_("E501: At end-of-file")); |
| 684 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | #ifdef FEAT_GUI |
| 688 | --hold_gui_events; |
| 689 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 690 | --RedrawingDisabled; |
| 691 | --no_wait_return; |
| 692 | update_screen(CLEAR); |
| 693 | need_wait_return = FALSE; |
| 694 | msg_scroll = save_msg_scroll; |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Execute a simple command line. Used for translated commands like "*". |
| 699 | */ |
| 700 | int |
| 701 | do_cmdline_cmd(cmd) |
| 702 | char_u *cmd; |
| 703 | { |
| 704 | return do_cmdline(cmd, NULL, NULL, |
| 705 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * do_cmdline(): execute one Ex command line |
| 710 | * |
| 711 | * 1. Execute "cmdline" when it is not NULL. |
| 712 | * If "cmdline" is NULL, or more lines are needed, getline() is used. |
| 713 | * 2. Split up in parts separated with '|'. |
| 714 | * |
| 715 | * This function can be called recursively! |
| 716 | * |
| 717 | * flags: |
| 718 | * DOCMD_VERBOSE - The command will be included in the error message. |
| 719 | * DOCMD_NOWAIT - Don't call wait_return() and friends. |
| 720 | * DOCMD_REPEAT - Repeat execution until getline() returns NULL. |
| 721 | * DOCMD_KEYTYPED - Don't reset KeyTyped. |
| 722 | * DOCMD_EXCRESET - Reset the exception environment (used for debugging). |
| 723 | * DOCMD_KEEPLINE - Store first typed line (for repeating with "."). |
| 724 | * |
| 725 | * return FAIL if cmdline could not be executed, OK otherwise |
| 726 | */ |
| 727 | int |
| 728 | do_cmdline(cmdline, getline, cookie, flags) |
| 729 | char_u *cmdline; |
| 730 | char_u *(*getline) __ARGS((int, void *, int)); |
| 731 | void *cookie; /* argument for getline() */ |
| 732 | int flags; |
| 733 | { |
| 734 | char_u *next_cmdline; /* next cmd to execute */ |
| 735 | char_u *cmdline_copy = NULL; /* copy of cmd line */ |
| 736 | int used_getline = FALSE; /* used "getline" to obtain command */ |
| 737 | static int recursive = 0; /* recursive depth */ |
| 738 | int msg_didout_before_start = 0; |
| 739 | int count = 0; /* line number count */ |
| 740 | int did_inc = FALSE; /* incremented RedrawingDisabled */ |
| 741 | int retval = OK; |
| 742 | #ifdef FEAT_EVAL |
| 743 | struct condstack cstack; /* conditional stack */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 744 | garray_T lines_ga; /* keep lines for ":while"/":for" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 745 | int current_line = 0; /* active line in lines_ga */ |
| 746 | char_u *fname = NULL; /* function or script name */ |
| 747 | linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */ |
| 748 | int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */ |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 749 | struct dbg_stuff debug_saved; /* saved things for debug mode */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | int initial_trylevel; |
| 751 | struct msglist **saved_msg_list = NULL; |
| 752 | struct msglist *private_msg_list; |
| 753 | |
| 754 | /* "getline" and "cookie" passed to do_one_cmd() */ |
| 755 | char_u *(*cmd_getline) __ARGS((int, void *, int)); |
| 756 | void *cmd_cookie; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 757 | struct loop_cookie cmd_loop_cookie; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 758 | void *real_cookie; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 759 | int getline_is_func; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 760 | #else |
| 761 | # define cmd_getline getline |
| 762 | # define cmd_cookie cookie |
| 763 | #endif |
| 764 | static int call_depth = 0; /* recursiveness */ |
| 765 | |
| 766 | #ifdef FEAT_EVAL |
| 767 | /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory |
| 768 | * location for storing error messages to be converted to an exception. |
Bram Moolenaar | cf3630f | 2005-01-08 16:04:29 +0000 | [diff] [blame] | 769 | * This ensures that the do_errthrow() call in do_one_cmd() does not |
| 770 | * combine the messages stored by an earlier invocation of do_one_cmd() |
| 771 | * with the command name of the later one. This would happen when |
| 772 | * BufWritePost autocommands are executed after a write error. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | saved_msg_list = msg_list; |
| 774 | msg_list = &private_msg_list; |
| 775 | private_msg_list = NULL; |
| 776 | #endif |
| 777 | |
| 778 | /* It's possible to create an endless loop with ":execute", catch that |
| 779 | * here. The value of 200 allows nested function calls, ":source", etc. */ |
| 780 | if (call_depth == 200) |
| 781 | { |
| 782 | EMSG(_("E169: Command too recursive")); |
| 783 | #ifdef FEAT_EVAL |
| 784 | /* When converting to an exception, we do not include the command name |
| 785 | * since this is not an error of the specific command. */ |
| 786 | do_errthrow((struct condstack *)NULL, (char_u *)NULL); |
| 787 | msg_list = saved_msg_list; |
| 788 | #endif |
| 789 | return FAIL; |
| 790 | } |
| 791 | ++call_depth; |
| 792 | |
| 793 | #ifdef FEAT_EVAL |
| 794 | cstack.cs_idx = -1; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 795 | cstack.cs_looplevel = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 796 | cstack.cs_trylevel = 0; |
| 797 | cstack.cs_emsg_silent_list = NULL; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 798 | cstack.cs_lflags = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 799 | ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10); |
| 800 | |
| 801 | real_cookie = getline_cookie(getline, cookie); |
| 802 | |
| 803 | /* Inside a function use a higher nesting level. */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 804 | getline_is_func = getline_equal(getline, cookie, get_func_line); |
| 805 | if (getline_is_func && ex_nesting_level == func_level(real_cookie)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 806 | ++ex_nesting_level; |
| 807 | |
| 808 | /* Get the function or script name and the address where the next breakpoint |
| 809 | * line and the debug tick for a function or script are stored. */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 810 | if (getline_is_func) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 811 | { |
| 812 | fname = func_name(real_cookie); |
| 813 | breakpoint = func_breakpoint(real_cookie); |
| 814 | dbg_tick = func_dbg_tick(real_cookie); |
| 815 | } |
| 816 | else if (getline_equal(getline, cookie, getsourceline)) |
| 817 | { |
| 818 | fname = sourcing_name; |
| 819 | breakpoint = source_breakpoint(real_cookie); |
| 820 | dbg_tick = source_dbg_tick(real_cookie); |
| 821 | } |
| 822 | |
| 823 | /* |
| 824 | * Initialize "force_abort" and "suppress_errthrow" at the top level. |
| 825 | */ |
| 826 | if (!recursive) |
| 827 | { |
| 828 | force_abort = FALSE; |
| 829 | suppress_errthrow = FALSE; |
| 830 | } |
| 831 | |
| 832 | /* |
| 833 | * If requested, store and reset the global values controlling the |
| 834 | * exception handling (used when debugging). |
| 835 | */ |
| 836 | else if (flags & DOCMD_EXCRESET) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 837 | save_dbg_stuff(&debug_saved); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 838 | |
| 839 | initial_trylevel = trylevel; |
| 840 | |
| 841 | /* |
| 842 | * "did_throw" will be set to TRUE when an exception is being thrown. |
| 843 | */ |
| 844 | did_throw = FALSE; |
| 845 | #endif |
| 846 | /* |
| 847 | * "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] | 848 | * cancel the whole command line, and any if/endif or loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 849 | * If force_abort is set, we cancel everything. |
| 850 | */ |
| 851 | did_emsg = FALSE; |
| 852 | |
| 853 | /* |
| 854 | * KeyTyped is only set when calling vgetc(). Reset it here when not |
| 855 | * calling vgetc() (sourced command lines). |
| 856 | */ |
| 857 | if (!(flags & DOCMD_KEYTYPED) && !getline_equal(getline, cookie, getexline)) |
| 858 | KeyTyped = FALSE; |
| 859 | |
| 860 | /* |
| 861 | * Continue executing command lines: |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 862 | * - when inside an ":if", ":while" or ":for" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 863 | * - for multiple commands on one line, separated with '|' |
| 864 | * - when repeating until there are no more lines (for ":source") |
| 865 | */ |
| 866 | next_cmdline = cmdline; |
| 867 | do |
| 868 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 869 | #ifdef FEAT_EVAL |
| 870 | getline_is_func = getline_equal(getline, cookie, get_func_line); |
| 871 | #endif |
| 872 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 873 | /* stop skipping cmds for an error msg after all endif/while/for */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 874 | if (next_cmdline == NULL |
| 875 | #ifdef FEAT_EVAL |
| 876 | && !force_abort |
| 877 | && cstack.cs_idx < 0 |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 878 | && !(getline_is_func && func_has_abort(real_cookie)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | #endif |
| 880 | ) |
| 881 | did_emsg = FALSE; |
| 882 | |
| 883 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 884 | * 1. If repeating a line in a loop, get a line from lines_ga. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 885 | * 2. If no line given: Get an allocated line with getline(). |
| 886 | * 3. If a line is given: Make a copy, so we can mess with it. |
| 887 | */ |
| 888 | |
| 889 | #ifdef FEAT_EVAL |
| 890 | /* 1. If repeating, get a previous line from lines_ga. */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 891 | if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | { |
| 893 | /* Each '|' separated command is stored separately in lines_ga, to |
| 894 | * be able to jump to it. Don't use next_cmdline now. */ |
| 895 | vim_free(cmdline_copy); |
| 896 | cmdline_copy = NULL; |
| 897 | |
| 898 | /* Check if a function has returned or, unless it has an unclosed |
| 899 | * try conditional, aborted. */ |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 900 | if (getline_is_func) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 901 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 902 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 903 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 904 | func_line_end(real_cookie); |
| 905 | # endif |
| 906 | if (func_has_ended(real_cookie)) |
| 907 | { |
| 908 | retval = FAIL; |
| 909 | break; |
| 910 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 912 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 913 | else if (do_profiling == PROF_YES |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 914 | && getline_equal(getline, cookie, getsourceline)) |
| 915 | script_line_end(); |
| 916 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 917 | |
| 918 | /* Check if a sourced file hit a ":finish" command. */ |
| 919 | if (source_finished(getline, cookie)) |
| 920 | { |
| 921 | retval = FAIL; |
| 922 | break; |
| 923 | } |
| 924 | |
| 925 | /* If breakpoints have been added/deleted need to check for it. */ |
| 926 | if (breakpoint != NULL && dbg_tick != NULL |
| 927 | && *dbg_tick != debug_tick) |
| 928 | { |
| 929 | *breakpoint = dbg_find_breakpoint( |
| 930 | getline_equal(getline, cookie, getsourceline), |
| 931 | fname, sourcing_lnum); |
| 932 | *dbg_tick = debug_tick; |
| 933 | } |
| 934 | |
| 935 | next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line; |
| 936 | sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum; |
| 937 | |
| 938 | /* Did we encounter a breakpoint? */ |
| 939 | if (breakpoint != NULL && *breakpoint != 0 |
| 940 | && *breakpoint <= sourcing_lnum) |
| 941 | { |
| 942 | dbg_breakpoint(fname, sourcing_lnum); |
| 943 | /* Find next breakpoint. */ |
| 944 | *breakpoint = dbg_find_breakpoint( |
| 945 | getline_equal(getline, cookie, getsourceline), |
| 946 | fname, sourcing_lnum); |
| 947 | *dbg_tick = debug_tick; |
| 948 | } |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 949 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 950 | if (do_profiling == PROF_YES) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 951 | { |
| 952 | if (getline_is_func) |
| 953 | func_line_start(real_cookie); |
| 954 | else if (getline_equal(getline, cookie, getsourceline)) |
| 955 | script_line_start(); |
| 956 | } |
| 957 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 960 | if (cstack.cs_looplevel > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 961 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 962 | /* Inside a while/for loop we need to store the lines and use them |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 963 | * again. Pass a different "getline" function to do_one_cmd() |
| 964 | * below, so that it stores lines in or reads them from |
| 965 | * "lines_ga". Makes it possible to define a function inside a |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 966 | * while/for loop. */ |
| 967 | cmd_getline = get_loop_line; |
| 968 | cmd_cookie = (void *)&cmd_loop_cookie; |
| 969 | cmd_loop_cookie.lines_gap = &lines_ga; |
| 970 | cmd_loop_cookie.current_line = current_line; |
| 971 | cmd_loop_cookie.getline = getline; |
| 972 | cmd_loop_cookie.cookie = cookie; |
| 973 | cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 974 | } |
| 975 | else |
| 976 | { |
| 977 | cmd_getline = getline; |
| 978 | cmd_cookie = cookie; |
| 979 | } |
| 980 | #endif |
| 981 | |
| 982 | /* 2. If no line given, get an allocated line with getline(). */ |
| 983 | if (next_cmdline == NULL) |
| 984 | { |
| 985 | /* |
| 986 | * Need to set msg_didout for the first line after an ":if", |
| 987 | * otherwise the ":if" will be overwritten. |
| 988 | */ |
| 989 | if (count == 1 && getline_equal(getline, cookie, getexline)) |
| 990 | msg_didout = TRUE; |
| 991 | if (getline == NULL || (next_cmdline = getline(':', cookie, |
| 992 | #ifdef FEAT_EVAL |
| 993 | cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2 |
| 994 | #else |
| 995 | 0 |
| 996 | #endif |
| 997 | )) == NULL) |
| 998 | { |
| 999 | /* Don't call wait_return for aborted command line. The NULL |
| 1000 | * returned for the end of a sourced file or executed function |
| 1001 | * doesn't do this. */ |
| 1002 | if (KeyTyped && !(flags & DOCMD_REPEAT)) |
| 1003 | need_wait_return = FALSE; |
| 1004 | retval = FAIL; |
| 1005 | break; |
| 1006 | } |
| 1007 | used_getline = TRUE; |
| 1008 | |
| 1009 | /* |
| 1010 | * Keep the first typed line. Clear it when more lines are typed. |
| 1011 | */ |
| 1012 | if (flags & DOCMD_KEEPLINE) |
| 1013 | { |
| 1014 | vim_free(repeat_cmdline); |
| 1015 | if (count == 0) |
| 1016 | repeat_cmdline = vim_strsave(next_cmdline); |
| 1017 | else |
| 1018 | repeat_cmdline = NULL; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /* 3. Make a copy of the command so we can mess with it. */ |
| 1023 | else if (cmdline_copy == NULL) |
| 1024 | { |
| 1025 | next_cmdline = vim_strsave(next_cmdline); |
| 1026 | if (next_cmdline == NULL) |
| 1027 | { |
| 1028 | EMSG(_(e_outofmem)); |
| 1029 | retval = FAIL; |
| 1030 | break; |
| 1031 | } |
| 1032 | } |
| 1033 | cmdline_copy = next_cmdline; |
| 1034 | |
| 1035 | #ifdef FEAT_EVAL |
| 1036 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1037 | * Save the current line when inside a ":while" or ":for", and when |
| 1038 | * the command looks like a ":while" or ":for", because we may need it |
| 1039 | * later. When there is a '|' and another command, it is stored |
| 1040 | * separately, because we need to be able to jump back to it from an |
| 1041 | * :endwhile/:endfor. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1042 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1043 | if (current_line == lines_ga.ga_len |
| 1044 | && (cstack.cs_looplevel || has_loop_cmd(next_cmdline))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1046 | if (store_loop_line(&lines_ga, next_cmdline) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1047 | { |
| 1048 | retval = FAIL; |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | did_endif = FALSE; |
| 1053 | #endif |
| 1054 | |
| 1055 | if (count++ == 0) |
| 1056 | { |
| 1057 | /* |
| 1058 | * All output from the commands is put below each other, without |
| 1059 | * waiting for a return. Don't do this when executing commands |
| 1060 | * from a script or when being called recursive (e.g. for ":e |
| 1061 | * +command file"). |
| 1062 | */ |
| 1063 | if (!(flags & DOCMD_NOWAIT) && !recursive) |
| 1064 | { |
| 1065 | msg_didout_before_start = msg_didout; |
| 1066 | msg_didany = FALSE; /* no output yet */ |
| 1067 | msg_start(); |
| 1068 | msg_scroll = TRUE; /* put messages below each other */ |
| 1069 | ++no_wait_return; /* dont wait for return until finished */ |
| 1070 | ++RedrawingDisabled; |
| 1071 | did_inc = TRUE; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | if (p_verbose >= 15 && sourcing_name != NULL) |
| 1076 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1077 | ++no_wait_return; |
Bram Moolenaar | 8b044b3 | 2005-05-31 22:05:58 +0000 | [diff] [blame] | 1078 | verbose_enter_scroll(); |
| 1079 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1080 | smsg((char_u *)_("line %ld: %s"), |
| 1081 | (long)sourcing_lnum, cmdline_copy); |
Bram Moolenaar | 8b044b3 | 2005-05-31 22:05:58 +0000 | [diff] [blame] | 1082 | if (msg_silent == 0) |
| 1083 | msg_puts((char_u *)"\n"); /* don't overwrite this */ |
| 1084 | |
| 1085 | verbose_leave_scroll(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1086 | --no_wait_return; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * 2. Execute one '|' separated command. |
| 1091 | * do_one_cmd() will return NULL if there is no trailing '|'. |
| 1092 | * "cmdline_copy" can change, e.g. for '%' and '#' expansion. |
| 1093 | */ |
| 1094 | ++recursive; |
| 1095 | next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE, |
| 1096 | #ifdef FEAT_EVAL |
| 1097 | &cstack, |
| 1098 | #endif |
| 1099 | cmd_getline, cmd_cookie); |
| 1100 | --recursive; |
| 1101 | |
| 1102 | #ifdef FEAT_EVAL |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1103 | if (cmd_cookie == (void *)&cmd_loop_cookie) |
| 1104 | /* Use "current_line" from "cmd_loop_cookie", it may have been |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | * incremented when defining a function. */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1106 | current_line = cmd_loop_cookie.current_line; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1107 | #endif |
| 1108 | |
| 1109 | if (next_cmdline == NULL) |
| 1110 | { |
| 1111 | vim_free(cmdline_copy); |
| 1112 | cmdline_copy = NULL; |
| 1113 | #ifdef FEAT_CMDHIST |
| 1114 | /* |
| 1115 | * If the command was typed, remember it for the ':' register. |
| 1116 | * Do this AFTER executing the command to make :@: work. |
| 1117 | */ |
| 1118 | if (getline_equal(getline, cookie, getexline) |
| 1119 | && new_last_cmdline != NULL) |
| 1120 | { |
| 1121 | vim_free(last_cmdline); |
| 1122 | last_cmdline = new_last_cmdline; |
| 1123 | new_last_cmdline = NULL; |
| 1124 | } |
| 1125 | #endif |
| 1126 | } |
| 1127 | else |
| 1128 | { |
| 1129 | /* need to copy the command after the '|' to cmdline_copy, for the |
| 1130 | * next do_one_cmd() */ |
| 1131 | mch_memmove(cmdline_copy, next_cmdline, STRLEN(next_cmdline) + 1); |
| 1132 | next_cmdline = cmdline_copy; |
| 1133 | } |
| 1134 | |
| 1135 | |
| 1136 | #ifdef FEAT_EVAL |
| 1137 | /* reset did_emsg for a function that is not aborted by an error */ |
| 1138 | if (did_emsg && !force_abort |
| 1139 | && getline_equal(getline, cookie, get_func_line) |
| 1140 | && !func_has_abort(real_cookie)) |
| 1141 | did_emsg = FALSE; |
| 1142 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1143 | if (cstack.cs_looplevel > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1144 | { |
| 1145 | ++current_line; |
| 1146 | |
| 1147 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1148 | * An ":endwhile", ":endfor" and ":continue" is handled here. |
| 1149 | * If we were executing commands, jump back to the ":while" or |
| 1150 | * ":for". |
| 1151 | * If we were not executing commands, decrement cs_looplevel. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1153 | if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1154 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1155 | cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1156 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1157 | /* Jump back to the matching ":while" or ":for". Be careful |
| 1158 | * not to use a cs_line[] from an entry that isn't a ":while" |
| 1159 | * or ":for": It would make "current_line" invalid and can |
| 1160 | * cause a crash. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1161 | if (!did_emsg && !got_int && !did_throw |
| 1162 | && cstack.cs_idx >= 0 |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1163 | && (cstack.cs_flags[cstack.cs_idx] |
| 1164 | & (CSF_WHILE | CSF_FOR)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1165 | && cstack.cs_line[cstack.cs_idx] >= 0 |
| 1166 | && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE)) |
| 1167 | { |
| 1168 | current_line = cstack.cs_line[cstack.cs_idx]; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1169 | /* remember we jumped there */ |
| 1170 | cstack.cs_lflags |= CSL_HAD_LOOP; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1171 | line_breakcheck(); /* check if CTRL-C typed */ |
| 1172 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1173 | /* Check for the next breakpoint at or after the ":while" |
| 1174 | * or ":for". */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1175 | if (breakpoint != NULL) |
| 1176 | { |
| 1177 | *breakpoint = dbg_find_breakpoint( |
| 1178 | getline_equal(getline, cookie, getsourceline), |
| 1179 | fname, |
| 1180 | ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1); |
| 1181 | *dbg_tick = debug_tick; |
| 1182 | } |
| 1183 | } |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1184 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1185 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1186 | /* can only get here with ":endwhile" or ":endfor" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1187 | if (cstack.cs_idx >= 0) |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1188 | rewind_conditionals(&cstack, cstack.cs_idx - 1, |
| 1189 | CSF_WHILE | CSF_FOR, &cstack.cs_looplevel); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1194 | * For a ":while" or ":for" we need to remember the line number. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1195 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1196 | else if (cstack.cs_lflags & CSL_HAD_LOOP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1197 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1198 | cstack.cs_lflags &= ~CSL_HAD_LOOP; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1199 | cstack.cs_line[cstack.cs_idx] = current_line - 1; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | /* |
| 1204 | * When not inside any ":while" loop, clear remembered lines. |
| 1205 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1206 | if (cstack.cs_looplevel == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1207 | { |
| 1208 | if (lines_ga.ga_len > 0) |
| 1209 | { |
| 1210 | sourcing_lnum = |
| 1211 | ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum; |
| 1212 | free_cmdlines(&lines_ga); |
| 1213 | } |
| 1214 | current_line = 0; |
| 1215 | } |
| 1216 | |
| 1217 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1218 | * A ":finally" makes did_emsg, got_int, and did_throw pending for |
| 1219 | * being restored at the ":endtry". Reset them here and set the |
| 1220 | * ACTIVE and FINALLY flags, so that the finally clause gets executed. |
| 1221 | * This includes the case where a missing ":endif", ":endwhile" or |
| 1222 | * ":endfor" was detected by the ":finally" itself. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | */ |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1224 | if (cstack.cs_lflags & CSL_HAD_FINA) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1225 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1226 | cstack.cs_lflags &= ~CSL_HAD_FINA; |
| 1227 | report_make_pending(cstack.cs_pending[cstack.cs_idx] |
| 1228 | & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | did_throw ? (void *)current_exception : NULL); |
| 1230 | did_emsg = got_int = did_throw = FALSE; |
| 1231 | cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY; |
| 1232 | } |
| 1233 | |
| 1234 | /* Update global "trylevel" for recursive calls to do_cmdline() from |
| 1235 | * within this loop. */ |
| 1236 | trylevel = initial_trylevel + cstack.cs_trylevel; |
| 1237 | |
| 1238 | /* |
| 1239 | * If the outermost try conditional (accross function calls and sourced |
| 1240 | * files) is aborted because of an error, an interrupt, or an uncaught |
| 1241 | * exception, cancel everything. If it is left normally, reset |
| 1242 | * force_abort to get the non-EH compatible abortion behavior for |
| 1243 | * the rest of the script. |
| 1244 | */ |
| 1245 | if (trylevel == 0 && !did_emsg && !got_int && !did_throw) |
| 1246 | force_abort = FALSE; |
| 1247 | |
| 1248 | /* Convert an interrupt to an exception if appropriate. */ |
| 1249 | (void)do_intthrow(&cstack); |
| 1250 | #endif /* FEAT_EVAL */ |
| 1251 | |
| 1252 | } |
| 1253 | /* |
| 1254 | * Continue executing command lines when: |
| 1255 | * - no CTRL-C typed, no aborting error, no exception thrown or try |
| 1256 | * conditionals need to be checked for executing finally clauses or |
| 1257 | * catching an interrupt exception |
| 1258 | * - didn't get an error message or lines are not typed |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1259 | * - 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] | 1260 | * looping for ":source" command or function call. |
| 1261 | */ |
| 1262 | while (!((got_int |
| 1263 | #ifdef FEAT_EVAL |
| 1264 | || (did_emsg && force_abort) || did_throw |
| 1265 | #endif |
| 1266 | ) |
| 1267 | #ifdef FEAT_EVAL |
| 1268 | && cstack.cs_trylevel == 0 |
| 1269 | #endif |
| 1270 | ) |
| 1271 | && !(did_emsg && used_getline |
| 1272 | && (getline_equal(getline, cookie, getexmodeline) |
| 1273 | || getline_equal(getline, cookie, getexline))) |
| 1274 | && (next_cmdline != NULL |
| 1275 | #ifdef FEAT_EVAL |
| 1276 | || cstack.cs_idx >= 0 |
| 1277 | #endif |
| 1278 | || (flags & DOCMD_REPEAT))); |
| 1279 | |
| 1280 | vim_free(cmdline_copy); |
| 1281 | #ifdef FEAT_EVAL |
| 1282 | free_cmdlines(&lines_ga); |
| 1283 | ga_clear(&lines_ga); |
| 1284 | |
| 1285 | if (cstack.cs_idx >= 0) |
| 1286 | { |
| 1287 | /* |
| 1288 | * If a sourced file or executed function ran to its end, report the |
| 1289 | * unclosed conditional. |
| 1290 | */ |
| 1291 | if (!got_int && !did_throw |
| 1292 | && ((getline_equal(getline, cookie, getsourceline) |
| 1293 | && !source_finished(getline, cookie)) |
| 1294 | || (getline_equal(getline, cookie, get_func_line) |
| 1295 | && !func_has_ended(real_cookie)))) |
| 1296 | { |
| 1297 | if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY) |
| 1298 | EMSG(_(e_endtry)); |
| 1299 | else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE) |
| 1300 | EMSG(_(e_endwhile)); |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1301 | else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR) |
| 1302 | EMSG(_(e_endfor)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | else |
| 1304 | EMSG(_(e_endif)); |
| 1305 | } |
| 1306 | |
| 1307 | /* |
| 1308 | * Reset "trylevel" in case of a ":finish" or ":return" or a missing |
| 1309 | * ":endtry" in a sourced file or executed function. If the try |
| 1310 | * conditional is in its finally clause, ignore anything pending. |
| 1311 | * If it is in a catch clause, finish the caught exception. |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1312 | * Also cleanup any "cs_forinfo" structures. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | */ |
| 1314 | do |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1315 | { |
| 1316 | int idx = cleanup_conditionals(&cstack, 0, TRUE); |
| 1317 | |
Bram Moolenaar | 89e5d68 | 2005-01-17 22:06:23 +0000 | [diff] [blame] | 1318 | if (idx >= 0) |
| 1319 | --idx; /* remove try block not in its finally clause */ |
Bram Moolenaar | bb761a7 | 2005-01-06 23:19:09 +0000 | [diff] [blame] | 1320 | rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR, |
| 1321 | &cstack.cs_looplevel); |
| 1322 | } |
| 1323 | while (cstack.cs_idx >= 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1324 | trylevel = initial_trylevel; |
| 1325 | } |
| 1326 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1327 | /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory |
| 1328 | * lack was reported above and the error message is to be converted to an |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1329 | * exception, do this now after rewinding the cstack. */ |
| 1330 | do_errthrow(&cstack, getline_equal(getline, cookie, get_func_line) |
| 1331 | ? (char_u *)"endfunction" : (char_u *)NULL); |
| 1332 | |
| 1333 | if (trylevel == 0) |
| 1334 | { |
| 1335 | /* |
| 1336 | * When an exception is being thrown out of the outermost try |
| 1337 | * conditional, discard the uncaught exception, disable the conversion |
| 1338 | * of interrupts or errors to exceptions, and ensure that no more |
| 1339 | * commands are executed. |
| 1340 | */ |
| 1341 | if (did_throw) |
| 1342 | { |
| 1343 | void *p = NULL; |
| 1344 | char_u *saved_sourcing_name; |
| 1345 | int saved_sourcing_lnum; |
| 1346 | struct msglist *messages = NULL, *next; |
| 1347 | |
| 1348 | /* |
| 1349 | * If the uncaught exception is a user exception, report it as an |
| 1350 | * error. If it is an error exception, display the saved error |
| 1351 | * message now. For an interrupt exception, do nothing; the |
| 1352 | * interrupt message is given elsewhere. |
| 1353 | */ |
| 1354 | switch (current_exception->type) |
| 1355 | { |
| 1356 | case ET_USER: |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 1357 | vim_snprintf((char *)IObuff, IOSIZE, |
| 1358 | _("E605: Exception not caught: %s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | current_exception->value); |
| 1360 | p = vim_strsave(IObuff); |
| 1361 | break; |
| 1362 | case ET_ERROR: |
| 1363 | messages = current_exception->messages; |
| 1364 | current_exception->messages = NULL; |
| 1365 | break; |
| 1366 | case ET_INTERRUPT: |
| 1367 | break; |
| 1368 | default: |
| 1369 | p = vim_strsave((char_u *)_(e_internal)); |
| 1370 | } |
| 1371 | |
| 1372 | saved_sourcing_name = sourcing_name; |
| 1373 | saved_sourcing_lnum = sourcing_lnum; |
| 1374 | sourcing_name = current_exception->throw_name; |
| 1375 | sourcing_lnum = current_exception->throw_lnum; |
| 1376 | current_exception->throw_name = NULL; |
| 1377 | |
| 1378 | discard_current_exception(); /* uses IObuff if 'verbose' */ |
| 1379 | suppress_errthrow = TRUE; |
| 1380 | force_abort = TRUE; |
| 1381 | |
| 1382 | if (messages != NULL) |
| 1383 | { |
| 1384 | do |
| 1385 | { |
| 1386 | next = messages->next; |
| 1387 | emsg(messages->msg); |
| 1388 | vim_free(messages->msg); |
| 1389 | vim_free(messages); |
| 1390 | messages = next; |
| 1391 | } |
| 1392 | while (messages != NULL); |
| 1393 | } |
| 1394 | else if (p != NULL) |
| 1395 | { |
| 1396 | emsg(p); |
| 1397 | vim_free(p); |
| 1398 | } |
| 1399 | vim_free(sourcing_name); |
| 1400 | sourcing_name = saved_sourcing_name; |
| 1401 | sourcing_lnum = saved_sourcing_lnum; |
| 1402 | } |
| 1403 | |
| 1404 | /* |
| 1405 | * On an interrupt or an aborting error not converted to an exception, |
| 1406 | * disable the conversion of errors to exceptions. (Interrupts are not |
| 1407 | * converted any more, here.) This enables also the interrupt message |
| 1408 | * when force_abort is set and did_emsg unset in case of an interrupt |
| 1409 | * from a finally clause after an error. |
| 1410 | */ |
| 1411 | else if (got_int || (did_emsg && force_abort)) |
| 1412 | suppress_errthrow = TRUE; |
| 1413 | } |
| 1414 | |
| 1415 | /* |
| 1416 | * The current cstack will be freed when do_cmdline() returns. An uncaught |
| 1417 | * exception will have to be rethrown in the previous cstack. If a function |
| 1418 | * has just returned or a script file was just finished and the previous |
| 1419 | * cstack belongs to the same function or, respectively, script file, it |
| 1420 | * will have to be checked for finally clauses to be executed due to the |
| 1421 | * ":return" or ":finish". This is done in do_one_cmd(). |
| 1422 | */ |
| 1423 | if (did_throw) |
| 1424 | need_rethrow = TRUE; |
| 1425 | if ((getline_equal(getline, cookie, getsourceline) |
| 1426 | && ex_nesting_level > source_level(real_cookie)) |
| 1427 | || (getline_equal(getline, cookie, get_func_line) |
| 1428 | && ex_nesting_level > func_level(real_cookie) + 1)) |
| 1429 | { |
| 1430 | if (!did_throw) |
| 1431 | check_cstack = TRUE; |
| 1432 | } |
| 1433 | else |
| 1434 | { |
| 1435 | /* When leaving a function, reduce nesting level. */ |
| 1436 | if (getline_equal(getline, cookie, get_func_line)) |
| 1437 | --ex_nesting_level; |
| 1438 | /* |
| 1439 | * Go to debug mode when returning from a function in which we are |
| 1440 | * single-stepping. |
| 1441 | */ |
| 1442 | if ((getline_equal(getline, cookie, getsourceline) |
| 1443 | || getline_equal(getline, cookie, get_func_line)) |
| 1444 | && ex_nesting_level + 1 <= debug_break_level) |
| 1445 | do_debug(getline_equal(getline, cookie, getsourceline) |
| 1446 | ? (char_u *)_("End of sourced file") |
| 1447 | : (char_u *)_("End of function")); |
| 1448 | } |
| 1449 | |
| 1450 | /* |
| 1451 | * Restore the exception environment (done after returning from the |
| 1452 | * debugger). |
| 1453 | */ |
| 1454 | if (flags & DOCMD_EXCRESET) |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1455 | restore_dbg_stuff(&debug_saved); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1456 | |
| 1457 | msg_list = saved_msg_list; |
| 1458 | #endif /* FEAT_EVAL */ |
| 1459 | |
| 1460 | /* |
| 1461 | * If there was too much output to fit on the command line, ask the user to |
| 1462 | * hit return before redrawing the screen. With the ":global" command we do |
| 1463 | * this only once after the command is finished. |
| 1464 | */ |
| 1465 | if (did_inc) |
| 1466 | { |
| 1467 | --RedrawingDisabled; |
| 1468 | --no_wait_return; |
| 1469 | msg_scroll = FALSE; |
| 1470 | |
| 1471 | /* |
| 1472 | * When just finished an ":if"-":else" which was typed, no need to |
| 1473 | * wait for hit-return. Also for an error situation. |
| 1474 | */ |
| 1475 | if (retval == FAIL |
| 1476 | #ifdef FEAT_EVAL |
| 1477 | || (did_endif && KeyTyped && !did_emsg) |
| 1478 | #endif |
| 1479 | ) |
| 1480 | { |
| 1481 | need_wait_return = FALSE; |
| 1482 | msg_didany = FALSE; /* don't wait when restarting edit */ |
| 1483 | } |
| 1484 | else if (need_wait_return) |
| 1485 | { |
| 1486 | /* |
| 1487 | * The msg_start() above clears msg_didout. The wait_return we do |
| 1488 | * here should not overwrite the command that may be shown before |
| 1489 | * doing that. |
| 1490 | */ |
| 1491 | msg_didout |= msg_didout_before_start; |
| 1492 | wait_return(FALSE); |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | #ifndef FEAT_EVAL |
| 1497 | /* |
| 1498 | * Reset if_level, in case a sourced script file contains more ":if" than |
| 1499 | * ":endif" (could be ":if x | foo | endif"). |
| 1500 | */ |
| 1501 | if_level = 0; |
| 1502 | #endif |
| 1503 | |
| 1504 | --call_depth; |
| 1505 | return retval; |
| 1506 | } |
| 1507 | |
| 1508 | #ifdef FEAT_EVAL |
| 1509 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1510 | * Obtain a line when inside a ":while" or ":for" loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1511 | */ |
| 1512 | static char_u * |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1513 | get_loop_line(c, cookie, indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1514 | int c; |
| 1515 | void *cookie; |
| 1516 | int indent; |
| 1517 | { |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1518 | struct loop_cookie *cp = (struct loop_cookie *)cookie; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1519 | wcmd_T *wp; |
| 1520 | char_u *line; |
| 1521 | |
| 1522 | if (cp->current_line + 1 >= cp->lines_gap->ga_len) |
| 1523 | { |
| 1524 | if (cp->repeating) |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1525 | return NULL; /* trying to read past ":endwhile"/":endfor" */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1526 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1527 | /* First time inside the ":while"/":for": get line normally. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1528 | if (cp->getline == NULL) |
| 1529 | line = getcmdline(c, 0L, indent); |
| 1530 | else |
| 1531 | line = cp->getline(c, cp->cookie, indent); |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1532 | if (store_loop_line(cp->lines_gap, line) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1533 | ++cp->current_line; |
| 1534 | |
| 1535 | return line; |
| 1536 | } |
| 1537 | |
| 1538 | KeyTyped = FALSE; |
| 1539 | ++cp->current_line; |
| 1540 | wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line; |
| 1541 | sourcing_lnum = wp->lnum; |
| 1542 | return vim_strsave(wp->line); |
| 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * Store a line in "gap" so that a ":while" loop can execute it again. |
| 1547 | */ |
| 1548 | static int |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1549 | store_loop_line(gap, line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1550 | garray_T *gap; |
| 1551 | char_u *line; |
| 1552 | { |
| 1553 | if (ga_grow(gap, 1) == FAIL) |
| 1554 | return FAIL; |
| 1555 | ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line); |
| 1556 | ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum; |
| 1557 | ++gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1558 | return OK; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1562 | * Free the lines stored for a ":while" or ":for" loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1563 | */ |
| 1564 | static void |
| 1565 | free_cmdlines(gap) |
| 1566 | garray_T *gap; |
| 1567 | { |
| 1568 | while (gap->ga_len > 0) |
| 1569 | { |
| 1570 | vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line); |
| 1571 | --gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | #endif |
| 1575 | |
| 1576 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1577 | * If "getline" is get_loop_line(), return TRUE if the getline it uses equals |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1578 | * "func". * Otherwise return TRUE when "getline" equals "func". |
| 1579 | */ |
| 1580 | /*ARGSUSED*/ |
| 1581 | int |
| 1582 | getline_equal(getline, cookie, func) |
| 1583 | char_u *(*getline) __ARGS((int, void *, int)); |
| 1584 | void *cookie; /* argument for getline() */ |
| 1585 | char_u *(*func) __ARGS((int, void *, int)); |
| 1586 | { |
| 1587 | #ifdef FEAT_EVAL |
| 1588 | char_u *(*gp) __ARGS((int, void *, int)); |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1589 | struct loop_cookie *cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1590 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1591 | /* When "getline" is "get_loop_line()" use the "cookie" to find the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1592 | * function that's orignally used to obtain the lines. This may be nested |
| 1593 | * several levels. */ |
| 1594 | gp = getline; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1595 | cp = (struct loop_cookie *)cookie; |
| 1596 | while (gp == get_loop_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1597 | { |
| 1598 | gp = cp->getline; |
| 1599 | cp = cp->cookie; |
| 1600 | } |
| 1601 | return gp == func; |
| 1602 | #else |
| 1603 | return getline == func; |
| 1604 | #endif |
| 1605 | } |
| 1606 | |
| 1607 | #if defined(FEAT_EVAL) || defined(FEAT_MBYTE) || defined(PROTO) |
| 1608 | /* |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1609 | * If "getline" is get_loop_line(), return the cookie used by the original |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1610 | * getline function. Otherwise return "cookie". |
| 1611 | */ |
| 1612 | /*ARGSUSED*/ |
| 1613 | void * |
| 1614 | getline_cookie(getline, cookie) |
| 1615 | char_u *(*getline) __ARGS((int, void *, int)); |
| 1616 | void *cookie; /* argument for getline() */ |
| 1617 | { |
| 1618 | # ifdef FEAT_EVAL |
| 1619 | char_u *(*gp) __ARGS((int, void *, int)); |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1620 | struct loop_cookie *cp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1621 | |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1622 | /* When "getline" is "get_loop_line()" use the "cookie" to find the |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1623 | * cookie that's orignally used to obtain the lines. This may be nested |
| 1624 | * several levels. */ |
| 1625 | gp = getline; |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 1626 | cp = (struct loop_cookie *)cookie; |
| 1627 | while (gp == get_loop_line) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1628 | { |
| 1629 | gp = cp->getline; |
| 1630 | cp = cp->cookie; |
| 1631 | } |
| 1632 | return cp; |
| 1633 | # else |
| 1634 | return cookie; |
| 1635 | # endif |
| 1636 | } |
| 1637 | #endif |
| 1638 | |
| 1639 | /* |
| 1640 | * Execute one Ex command. |
| 1641 | * |
| 1642 | * If 'sourcing' is TRUE, the command will be included in the error message. |
| 1643 | * |
| 1644 | * 1. skip comment lines and leading space |
| 1645 | * 2. handle command modifiers |
| 1646 | * 3. parse range |
| 1647 | * 4. parse command |
| 1648 | * 5. parse arguments |
| 1649 | * 6. switch on command name |
| 1650 | * |
| 1651 | * Note: "getline" can be NULL. |
| 1652 | * |
| 1653 | * This function may be called recursively! |
| 1654 | */ |
| 1655 | #if (_MSC_VER == 1200) |
| 1656 | /* |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1657 | * Avoid optimisation bug in VC++ version 6.0 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1658 | */ |
Bram Moolenaar | 281bdce | 2005-01-25 21:53:18 +0000 | [diff] [blame] | 1659 | #pragma optimize( "g", off ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1660 | #endif |
| 1661 | static char_u * |
| 1662 | do_one_cmd(cmdlinep, sourcing, |
| 1663 | #ifdef FEAT_EVAL |
| 1664 | cstack, |
| 1665 | #endif |
| 1666 | getline, cookie) |
| 1667 | char_u **cmdlinep; |
| 1668 | int sourcing; |
| 1669 | #ifdef FEAT_EVAL |
| 1670 | struct condstack *cstack; |
| 1671 | #endif |
| 1672 | char_u *(*getline) __ARGS((int, void *, int)); |
| 1673 | void *cookie; /* argument for getline() */ |
| 1674 | { |
| 1675 | char_u *p; |
| 1676 | linenr_T lnum; |
| 1677 | long n; |
| 1678 | char_u *errormsg = NULL; /* error message */ |
| 1679 | exarg_T ea; /* Ex command arguments */ |
| 1680 | long verbose_save = -1; |
| 1681 | int save_msg_scroll = 0; |
| 1682 | int did_silent = 0; |
| 1683 | int did_esilent = 0; |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 1684 | #ifdef HAVE_SANDBOX |
| 1685 | int did_sandbox = FALSE; |
| 1686 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1687 | cmdmod_T save_cmdmod; |
| 1688 | int ni; /* set when Not Implemented */ |
| 1689 | |
| 1690 | vim_memset(&ea, 0, sizeof(ea)); |
| 1691 | ea.line1 = 1; |
| 1692 | ea.line2 = 1; |
| 1693 | #ifdef FEAT_EVAL |
| 1694 | ++ex_nesting_level; |
| 1695 | #endif |
| 1696 | |
| 1697 | /* when not editing the last file :q has to be typed twice */ |
| 1698 | if (quitmore |
| 1699 | #ifdef FEAT_EVAL |
| 1700 | /* avoid that a function call in 'statusline' does this */ |
| 1701 | && !getline_equal(getline, cookie, get_func_line) |
| 1702 | #endif |
| 1703 | ) |
| 1704 | --quitmore; |
| 1705 | |
| 1706 | /* |
| 1707 | * Reset browse, confirm, etc.. They are restored when returning, for |
| 1708 | * recursive calls. |
| 1709 | */ |
| 1710 | save_cmdmod = cmdmod; |
| 1711 | vim_memset(&cmdmod, 0, sizeof(cmdmod)); |
| 1712 | |
| 1713 | /* |
| 1714 | * Repeat until no more command modifiers are found. |
| 1715 | */ |
| 1716 | ea.cmd = *cmdlinep; |
| 1717 | for (;;) |
| 1718 | { |
| 1719 | /* |
| 1720 | * 1. skip comment lines and leading white space and colons |
| 1721 | */ |
| 1722 | while (*ea.cmd == ' ' || *ea.cmd == '\t' || *ea.cmd == ':') |
| 1723 | ++ea.cmd; |
| 1724 | |
| 1725 | /* in ex mode, an empty line works like :+ */ |
| 1726 | if (*ea.cmd == NUL && exmode_active |
| 1727 | && (getline_equal(getline, cookie, getexmodeline) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1728 | || getline_equal(getline, cookie, getexline)) |
| 1729 | && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1730 | { |
| 1731 | ea.cmd = (char_u *)"+"; |
| 1732 | ex_pressedreturn = TRUE; |
| 1733 | } |
| 1734 | |
| 1735 | /* ignore comment and empty lines */ |
| 1736 | if (*ea.cmd == '"' || *ea.cmd == NUL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1737 | { |
| 1738 | ex_pressedreturn = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1739 | goto doend; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1740 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1741 | |
| 1742 | /* |
| 1743 | * 2. handle command modifiers. |
| 1744 | */ |
| 1745 | p = ea.cmd; |
| 1746 | if (VIM_ISDIGIT(*ea.cmd)) |
| 1747 | p = skipwhite(skipdigits(ea.cmd)); |
| 1748 | switch (*p) |
| 1749 | { |
| 1750 | /* When adding an entry, also modify cmd_exists(). */ |
| 1751 | case 'a': if (!checkforcmd(&ea.cmd, "aboveleft", 3)) |
| 1752 | break; |
| 1753 | #ifdef FEAT_WINDOWS |
| 1754 | cmdmod.split |= WSP_ABOVE; |
| 1755 | #endif |
| 1756 | continue; |
| 1757 | |
| 1758 | case 'b': if (checkforcmd(&ea.cmd, "belowright", 3)) |
| 1759 | { |
| 1760 | #ifdef FEAT_WINDOWS |
| 1761 | cmdmod.split |= WSP_BELOW; |
| 1762 | #endif |
| 1763 | continue; |
| 1764 | } |
| 1765 | if (checkforcmd(&ea.cmd, "browse", 3)) |
| 1766 | { |
| 1767 | #ifdef FEAT_BROWSE |
| 1768 | cmdmod.browse = TRUE; |
| 1769 | #endif |
| 1770 | continue; |
| 1771 | } |
| 1772 | if (!checkforcmd(&ea.cmd, "botright", 2)) |
| 1773 | break; |
| 1774 | #ifdef FEAT_WINDOWS |
| 1775 | cmdmod.split |= WSP_BOT; |
| 1776 | #endif |
| 1777 | continue; |
| 1778 | |
| 1779 | case 'c': if (!checkforcmd(&ea.cmd, "confirm", 4)) |
| 1780 | break; |
| 1781 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 1782 | cmdmod.confirm = TRUE; |
| 1783 | #endif |
| 1784 | continue; |
| 1785 | |
| 1786 | case 'k': if (checkforcmd(&ea.cmd, "keepmarks", 3)) |
| 1787 | { |
| 1788 | cmdmod.keepmarks = TRUE; |
| 1789 | continue; |
| 1790 | } |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 1791 | if (checkforcmd(&ea.cmd, "keepalt", 5)) |
| 1792 | { |
| 1793 | cmdmod.keepalt = TRUE; |
| 1794 | continue; |
| 1795 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1796 | if (!checkforcmd(&ea.cmd, "keepjumps", 5)) |
| 1797 | break; |
| 1798 | cmdmod.keepjumps = TRUE; |
| 1799 | continue; |
| 1800 | |
| 1801 | /* ":hide" and ":hide | cmd" are not modifiers */ |
| 1802 | case 'h': if (p != ea.cmd || !checkforcmd(&p, "hide", 3) |
| 1803 | || *p == NUL || ends_excmd(*p)) |
| 1804 | break; |
| 1805 | ea.cmd = p; |
| 1806 | cmdmod.hide = TRUE; |
| 1807 | continue; |
| 1808 | |
| 1809 | case 'l': if (checkforcmd(&ea.cmd, "lockmarks", 3)) |
| 1810 | { |
| 1811 | cmdmod.lockmarks = TRUE; |
| 1812 | continue; |
| 1813 | } |
| 1814 | |
| 1815 | if (!checkforcmd(&ea.cmd, "leftabove", 5)) |
| 1816 | break; |
| 1817 | #ifdef FEAT_WINDOWS |
| 1818 | cmdmod.split |= WSP_ABOVE; |
| 1819 | #endif |
| 1820 | continue; |
| 1821 | |
Bram Moolenaar | cdbac1e | 2005-12-11 21:27:22 +0000 | [diff] [blame] | 1822 | case 'n': if (!checkforcmd(&ea.cmd, "noautocmd", 3)) |
| 1823 | break; |
| 1824 | #ifdef FEAT_AUTOCMD |
| 1825 | if (cmdmod.save_ei == NULL) |
| 1826 | { |
| 1827 | /* Set 'eventignore' to "all". Don't free the |
| 1828 | * existing option value, we restore it later. */ |
| 1829 | cmdmod.save_ei = vim_strsave(p_ei); |
| 1830 | set_string_option_direct((char_u *)"ei", -1, |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 1831 | (char_u *)"all", OPT_FREE, SID_NONE); |
Bram Moolenaar | cdbac1e | 2005-12-11 21:27:22 +0000 | [diff] [blame] | 1832 | } |
| 1833 | #endif |
| 1834 | continue; |
| 1835 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1836 | case 'r': if (!checkforcmd(&ea.cmd, "rightbelow", 6)) |
| 1837 | break; |
| 1838 | #ifdef FEAT_WINDOWS |
| 1839 | cmdmod.split |= WSP_BELOW; |
| 1840 | #endif |
| 1841 | continue; |
| 1842 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 1843 | case 's': if (checkforcmd(&ea.cmd, "sandbox", 3)) |
| 1844 | { |
| 1845 | #ifdef HAVE_SANDBOX |
| 1846 | if (!did_sandbox) |
| 1847 | ++sandbox; |
| 1848 | did_sandbox = TRUE; |
| 1849 | #endif |
| 1850 | continue; |
| 1851 | } |
| 1852 | if (!checkforcmd(&ea.cmd, "silent", 3)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1853 | break; |
| 1854 | ++did_silent; |
| 1855 | ++msg_silent; |
| 1856 | save_msg_scroll = msg_scroll; |
| 1857 | if (*ea.cmd == '!' && !vim_iswhite(ea.cmd[-1])) |
| 1858 | { |
| 1859 | /* ":silent!", but not "silent !cmd" */ |
| 1860 | ea.cmd = skipwhite(ea.cmd + 1); |
| 1861 | ++emsg_silent; |
| 1862 | ++did_esilent; |
| 1863 | } |
| 1864 | continue; |
| 1865 | |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 1866 | case 't': if (checkforcmd(&p, "tab", 3)) |
| 1867 | { |
| 1868 | #ifdef FEAT_WINDOWS |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 1869 | if (vim_isdigit(*ea.cmd)) |
| 1870 | cmdmod.tab = atoi((char *)ea.cmd) + 1; |
| 1871 | else |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 1872 | cmdmod.tab = tabpage_index(curtab) + 1; |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 1873 | ea.cmd = p; |
| 1874 | #endif |
| 1875 | continue; |
| 1876 | } |
| 1877 | if (!checkforcmd(&ea.cmd, "topleft", 2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | #ifdef FEAT_WINDOWS |
| 1880 | cmdmod.split |= WSP_TOP; |
| 1881 | #endif |
| 1882 | continue; |
| 1883 | |
| 1884 | case 'v': if (checkforcmd(&ea.cmd, "vertical", 4)) |
| 1885 | { |
| 1886 | #ifdef FEAT_VERTSPLIT |
| 1887 | cmdmod.split |= WSP_VERT; |
| 1888 | #endif |
| 1889 | continue; |
| 1890 | } |
| 1891 | if (!checkforcmd(&p, "verbose", 4)) |
| 1892 | break; |
| 1893 | if (verbose_save < 0) |
| 1894 | verbose_save = p_verbose; |
Bram Moolenaar | ed20346 | 2004-06-16 11:19:22 +0000 | [diff] [blame] | 1895 | if (vim_isdigit(*ea.cmd)) |
| 1896 | p_verbose = atoi((char *)ea.cmd); |
| 1897 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1898 | p_verbose = 1; |
| 1899 | ea.cmd = p; |
| 1900 | continue; |
| 1901 | } |
| 1902 | break; |
| 1903 | } |
| 1904 | |
| 1905 | #ifdef FEAT_EVAL |
| 1906 | ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0 |
| 1907 | && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE)); |
| 1908 | #else |
| 1909 | ea.skip = (if_level > 0); |
| 1910 | #endif |
| 1911 | |
| 1912 | #ifdef FEAT_EVAL |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 1913 | # ifdef FEAT_PROFILE |
| 1914 | /* Count this line for profiling if ea.skip is FALSE. */ |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 1915 | if (do_profiling == PROF_YES && !ea.skip) |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 1916 | { |
| 1917 | if (getline_equal(getline, cookie, get_func_line)) |
| 1918 | func_line_exec(getline_cookie(getline, cookie)); |
| 1919 | else if (getline_equal(getline, cookie, getsourceline)) |
| 1920 | script_line_exec(); |
| 1921 | } |
| 1922 | #endif |
| 1923 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1924 | /* May go to debug mode. If this happens and the ">quit" debug command is |
| 1925 | * used, throw an interrupt exception and skip the next command. */ |
| 1926 | dbg_check_breakpoint(&ea); |
| 1927 | if (!ea.skip && got_int) |
| 1928 | { |
| 1929 | ea.skip = TRUE; |
| 1930 | (void)do_intthrow(cstack); |
| 1931 | } |
| 1932 | #endif |
| 1933 | |
| 1934 | /* |
| 1935 | * 3. parse a range specifier of the form: addr [,addr] [;addr] .. |
| 1936 | * |
| 1937 | * where 'addr' is: |
| 1938 | * |
| 1939 | * % (entire file) |
| 1940 | * $ [+-NUM] |
| 1941 | * 'x [+-NUM] (where x denotes a currently defined mark) |
| 1942 | * . [+-NUM] |
| 1943 | * [+-NUM].. |
| 1944 | * NUM |
| 1945 | * |
| 1946 | * The ea.cmd pointer is updated to point to the first character following the |
| 1947 | * range spec. If an initial address is found, but no second, the upper bound |
| 1948 | * is equal to the lower. |
| 1949 | */ |
| 1950 | |
| 1951 | /* repeat for all ',' or ';' separated addresses */ |
| 1952 | for (;;) |
| 1953 | { |
| 1954 | ea.line1 = ea.line2; |
| 1955 | ea.line2 = curwin->w_cursor.lnum; /* default is current line number */ |
| 1956 | ea.cmd = skipwhite(ea.cmd); |
| 1957 | lnum = get_address(&ea.cmd, ea.skip, ea.addr_count == 0); |
| 1958 | if (ea.cmd == NULL) /* error detected */ |
| 1959 | goto doend; |
| 1960 | if (lnum == MAXLNUM) |
| 1961 | { |
| 1962 | if (*ea.cmd == '%') /* '%' - all lines */ |
| 1963 | { |
| 1964 | ++ea.cmd; |
| 1965 | ea.line1 = 1; |
| 1966 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 1967 | ++ea.addr_count; |
| 1968 | } |
| 1969 | /* '*' - visual area */ |
| 1970 | else if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL) |
| 1971 | { |
| 1972 | pos_T *fp; |
| 1973 | |
| 1974 | ++ea.cmd; |
| 1975 | if (!ea.skip) |
| 1976 | { |
| 1977 | fp = getmark('<', FALSE); |
| 1978 | if (check_mark(fp) == FAIL) |
| 1979 | goto doend; |
| 1980 | ea.line1 = fp->lnum; |
| 1981 | fp = getmark('>', FALSE); |
| 1982 | if (check_mark(fp) == FAIL) |
| 1983 | goto doend; |
| 1984 | ea.line2 = fp->lnum; |
| 1985 | ++ea.addr_count; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | else |
| 1990 | ea.line2 = lnum; |
| 1991 | ea.addr_count++; |
| 1992 | |
| 1993 | if (*ea.cmd == ';') |
| 1994 | { |
| 1995 | if (!ea.skip) |
| 1996 | curwin->w_cursor.lnum = ea.line2; |
| 1997 | } |
| 1998 | else if (*ea.cmd != ',') |
| 1999 | break; |
| 2000 | ++ea.cmd; |
| 2001 | } |
| 2002 | |
| 2003 | /* One address given: set start and end lines */ |
| 2004 | if (ea.addr_count == 1) |
| 2005 | { |
| 2006 | ea.line1 = ea.line2; |
| 2007 | /* ... but only implicit: really no address given */ |
| 2008 | if (lnum == MAXLNUM) |
| 2009 | ea.addr_count = 0; |
| 2010 | } |
| 2011 | |
| 2012 | /* Don't leave the cursor on an illegal line (caused by ';') */ |
| 2013 | check_cursor_lnum(); |
| 2014 | |
| 2015 | /* |
| 2016 | * 4. parse command |
| 2017 | */ |
| 2018 | |
| 2019 | /* |
| 2020 | * Skip ':' and any white space |
| 2021 | */ |
| 2022 | ea.cmd = skipwhite(ea.cmd); |
| 2023 | while (*ea.cmd == ':') |
| 2024 | ea.cmd = skipwhite(ea.cmd + 1); |
| 2025 | |
| 2026 | /* |
| 2027 | * If we got a line, but no command, then go to the line. |
| 2028 | * If we find a '|' or '\n' we set ea.nextcmd. |
| 2029 | */ |
| 2030 | if (*ea.cmd == NUL || *ea.cmd == '"' || |
| 2031 | (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL) |
| 2032 | { |
| 2033 | /* |
| 2034 | * strange vi behaviour: |
| 2035 | * ":3" jumps to line 3 |
| 2036 | * ":3|..." prints line 3 |
| 2037 | * ":|" prints current line |
| 2038 | */ |
| 2039 | if (ea.skip) /* skip this if inside :if */ |
| 2040 | goto doend; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2041 | if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2042 | { |
| 2043 | ea.cmdidx = CMD_print; |
| 2044 | ea.argt = RANGE+COUNT+TRLBAR; |
| 2045 | if ((errormsg = invalid_range(&ea)) == NULL) |
| 2046 | { |
| 2047 | correct_range(&ea); |
| 2048 | ex_print(&ea); |
| 2049 | } |
| 2050 | } |
| 2051 | else if (ea.addr_count != 0) |
| 2052 | { |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 2053 | if (ea.line2 > curbuf->b_ml.ml_line_count) |
| 2054 | { |
| 2055 | /* With '-' in 'cpoptions' a line number past the file is an |
| 2056 | * error, otherwise put it at the end of the file. */ |
| 2057 | if (vim_strchr(p_cpo, CPO_MINUS) != NULL) |
| 2058 | ea.line2 = -1; |
| 2059 | else |
| 2060 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 2061 | } |
| 2062 | |
| 2063 | if (ea.line2 < 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2064 | errormsg = (char_u *)_(e_invrange); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | else |
| 2066 | { |
| 2067 | if (ea.line2 == 0) |
| 2068 | curwin->w_cursor.lnum = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2069 | else |
| 2070 | curwin->w_cursor.lnum = ea.line2; |
| 2071 | beginline(BL_SOL | BL_FIX); |
| 2072 | } |
| 2073 | } |
| 2074 | goto doend; |
| 2075 | } |
| 2076 | |
| 2077 | /* Find the command and let "p" point to after it. */ |
| 2078 | p = find_command(&ea, NULL); |
| 2079 | |
| 2080 | #ifdef FEAT_USR_CMDS |
| 2081 | if (p == NULL) |
| 2082 | { |
| 2083 | if (!ea.skip) |
| 2084 | errormsg = (char_u *)_("E464: Ambiguous use of user-defined command"); |
| 2085 | goto doend; |
| 2086 | } |
| 2087 | /* Check for wrong commands. */ |
| 2088 | if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78) |
| 2089 | { |
| 2090 | errormsg = uc_fun_cmd(); |
| 2091 | goto doend; |
| 2092 | } |
| 2093 | #endif |
| 2094 | if (ea.cmdidx == CMD_SIZE) |
| 2095 | { |
| 2096 | if (!ea.skip) |
| 2097 | { |
| 2098 | STRCPY(IObuff, _("E492: Not an editor command")); |
| 2099 | if (!sourcing) |
| 2100 | { |
| 2101 | STRCAT(IObuff, ": "); |
| 2102 | STRNCAT(IObuff, *cmdlinep, 40); |
| 2103 | } |
| 2104 | errormsg = IObuff; |
| 2105 | } |
| 2106 | goto doend; |
| 2107 | } |
| 2108 | |
| 2109 | ni = ( |
| 2110 | #ifdef FEAT_USR_CMDS |
| 2111 | !USER_CMDIDX(ea.cmdidx) && |
| 2112 | #endif |
| 2113 | cmdnames[ea.cmdidx].cmd_func == ex_ni); |
| 2114 | |
| 2115 | #ifndef FEAT_EVAL |
| 2116 | /* |
| 2117 | * When the expression evaluation is disabled, recognize the ":if" and |
| 2118 | * ":endif" commands and ignore everything in between it. |
| 2119 | */ |
| 2120 | if (ea.cmdidx == CMD_if) |
| 2121 | ++if_level; |
| 2122 | if (if_level) |
| 2123 | { |
| 2124 | if (ea.cmdidx == CMD_endif) |
| 2125 | --if_level; |
| 2126 | goto doend; |
| 2127 | } |
| 2128 | |
| 2129 | #endif |
| 2130 | |
| 2131 | if (*p == '!' && ea.cmdidx != CMD_substitute) /* forced commands */ |
| 2132 | { |
| 2133 | ++p; |
| 2134 | ea.forceit = TRUE; |
| 2135 | } |
| 2136 | else |
| 2137 | ea.forceit = FALSE; |
| 2138 | |
| 2139 | /* |
| 2140 | * 5. parse arguments |
| 2141 | */ |
| 2142 | #ifdef FEAT_USR_CMDS |
| 2143 | if (!USER_CMDIDX(ea.cmdidx)) |
| 2144 | #endif |
| 2145 | ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt; |
| 2146 | |
| 2147 | if (!ea.skip) |
| 2148 | { |
| 2149 | #ifdef HAVE_SANDBOX |
| 2150 | if (sandbox != 0 && !(ea.argt & SBOXOK)) |
| 2151 | { |
| 2152 | /* Command not allowed in sandbox. */ |
| 2153 | errormsg = (char_u *)_(e_sandbox); |
| 2154 | goto doend; |
| 2155 | } |
| 2156 | #endif |
| 2157 | if (!curbuf->b_p_ma && (ea.argt & MODIFY)) |
| 2158 | { |
| 2159 | /* Command not allowed in non-'modifiable' buffer */ |
| 2160 | errormsg = (char_u *)_(e_modifiable); |
| 2161 | goto doend; |
| 2162 | } |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 2163 | |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 2164 | if (text_locked() && !(ea.argt & CMDWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2165 | # ifdef FEAT_USR_CMDS |
| 2166 | && !USER_CMDIDX(ea.cmdidx) |
| 2167 | # endif |
| 2168 | ) |
| 2169 | { |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 2170 | /* Command not allowed when editing the command line. */ |
| 2171 | #ifdef FEAT_CMDWIN |
| 2172 | if (cmdwin_type != 0) |
| 2173 | errormsg = (char_u *)_(e_cmdwin); |
| 2174 | else |
| 2175 | #endif |
| 2176 | errormsg = (char_u *)_(e_secure); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2177 | goto doend; |
| 2178 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2179 | |
| 2180 | if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0) |
| 2181 | { |
| 2182 | /* no range allowed */ |
| 2183 | errormsg = (char_u *)_(e_norange); |
| 2184 | goto doend; |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */ |
| 2189 | { |
| 2190 | errormsg = (char_u *)_(e_nobang); |
| 2191 | goto doend; |
| 2192 | } |
| 2193 | |
| 2194 | /* |
| 2195 | * Don't complain about the range if it is not used |
| 2196 | * (could happen if line_count is accidentally set to 0). |
| 2197 | */ |
| 2198 | if (!ea.skip && !ni) |
| 2199 | { |
| 2200 | /* |
| 2201 | * If the range is backwards, ask for confirmation and, if given, swap |
| 2202 | * ea.line1 & ea.line2 so it's forwards again. |
| 2203 | * When global command is busy, don't ask, will fail below. |
| 2204 | */ |
| 2205 | if (!global_busy && ea.line1 > ea.line2) |
| 2206 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2207 | if (msg_silent == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2208 | { |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2209 | if (sourcing || exmode_active) |
| 2210 | { |
| 2211 | errormsg = (char_u *)_("E493: Backwards range given"); |
| 2212 | goto doend; |
| 2213 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2214 | if (ask_yesno((char_u *) |
| 2215 | _("Backwards range given, OK to swap"), FALSE) != 'y') |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 2216 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2217 | } |
| 2218 | lnum = ea.line1; |
| 2219 | ea.line1 = ea.line2; |
| 2220 | ea.line2 = lnum; |
| 2221 | } |
| 2222 | if ((errormsg = invalid_range(&ea)) != NULL) |
| 2223 | goto doend; |
| 2224 | } |
| 2225 | |
| 2226 | if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */ |
| 2227 | ea.line2 = 1; |
| 2228 | |
| 2229 | correct_range(&ea); |
| 2230 | |
| 2231 | #ifdef FEAT_FOLDING |
| 2232 | if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy) |
| 2233 | { |
| 2234 | /* Put the first line at the start of a closed fold, put the last line |
| 2235 | * at the end of a closed fold. */ |
| 2236 | (void)hasFolding(ea.line1, &ea.line1, NULL); |
| 2237 | (void)hasFolding(ea.line2, NULL, &ea.line2); |
| 2238 | } |
| 2239 | #endif |
| 2240 | |
| 2241 | #ifdef FEAT_QUICKFIX |
| 2242 | /* |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 2243 | * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | * option here, so things like % get expanded. |
| 2245 | */ |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 2246 | p = replace_makeprg(&ea, p, cmdlinep); |
| 2247 | if (p == NULL) |
| 2248 | goto doend; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2249 | #endif |
| 2250 | |
| 2251 | /* |
| 2252 | * Skip to start of argument. |
| 2253 | * Don't do this for the ":!" command, because ":!! -l" needs the space. |
| 2254 | */ |
| 2255 | if (ea.cmdidx == CMD_bang) |
| 2256 | ea.arg = p; |
| 2257 | else |
| 2258 | ea.arg = skipwhite(p); |
| 2259 | |
| 2260 | /* |
| 2261 | * Check for "++opt=val" argument. |
| 2262 | * Must be first, allow ":w ++enc=utf8 !cmd" |
| 2263 | */ |
| 2264 | if (ea.argt & ARGOPT) |
| 2265 | while (ea.arg[0] == '+' && ea.arg[1] == '+') |
| 2266 | if (getargopt(&ea) == FAIL && !ni) |
| 2267 | { |
| 2268 | errormsg = (char_u *)_(e_invarg); |
| 2269 | goto doend; |
| 2270 | } |
| 2271 | |
| 2272 | if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) |
| 2273 | { |
| 2274 | if (*ea.arg == '>') /* append */ |
| 2275 | { |
| 2276 | if (*++ea.arg != '>') /* typed wrong */ |
| 2277 | { |
| 2278 | errormsg = (char_u *)_("E494: Use w or w>>"); |
| 2279 | goto doend; |
| 2280 | } |
| 2281 | ea.arg = skipwhite(ea.arg + 1); |
| 2282 | ea.append = TRUE; |
| 2283 | } |
| 2284 | else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */ |
| 2285 | { |
| 2286 | ++ea.arg; |
| 2287 | ea.usefilter = TRUE; |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | if (ea.cmdidx == CMD_read) |
| 2292 | { |
| 2293 | if (ea.forceit) |
| 2294 | { |
| 2295 | ea.usefilter = TRUE; /* :r! filter if ea.forceit */ |
| 2296 | ea.forceit = FALSE; |
| 2297 | } |
| 2298 | else if (*ea.arg == '!') /* :r !filter */ |
| 2299 | { |
| 2300 | ++ea.arg; |
| 2301 | ea.usefilter = TRUE; |
| 2302 | } |
| 2303 | } |
| 2304 | |
| 2305 | if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) |
| 2306 | { |
| 2307 | ea.amount = 1; |
| 2308 | while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */ |
| 2309 | { |
| 2310 | ++ea.arg; |
| 2311 | ++ea.amount; |
| 2312 | } |
| 2313 | ea.arg = skipwhite(ea.arg); |
| 2314 | } |
| 2315 | |
| 2316 | /* |
| 2317 | * Check for "+command" argument, before checking for next command. |
| 2318 | * Don't do this for ":read !cmd" and ":write !cmd". |
| 2319 | */ |
| 2320 | if ((ea.argt & EDITCMD) && !ea.usefilter) |
| 2321 | ea.do_ecmd_cmd = getargcmd(&ea.arg); |
| 2322 | |
| 2323 | /* |
| 2324 | * Check for '|' to separate commands and '"' to start comments. |
| 2325 | * Don't do this for ":read !cmd" and ":write !cmd". |
| 2326 | */ |
| 2327 | if ((ea.argt & TRLBAR) && !ea.usefilter) |
| 2328 | separate_nextcmd(&ea); |
| 2329 | |
| 2330 | /* |
| 2331 | * Check for <newline> to end a shell command. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2332 | * Also do this for ":read !cmd", ":write !cmd" and ":global". |
| 2333 | * Any others? |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2334 | */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2335 | else if (ea.cmdidx == CMD_bang |
| 2336 | || ea.cmdidx == CMD_global |
| 2337 | || ea.cmdidx == CMD_vglobal |
| 2338 | || ea.usefilter) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | { |
| 2340 | for (p = ea.arg; *p; ++p) |
| 2341 | { |
| 2342 | /* Remove one backslash before a newline, so that it's possible to |
| 2343 | * pass a newline to the shell and also a newline that is preceded |
| 2344 | * with a backslash. This makes it impossible to end a shell |
| 2345 | * command in a backslash, but that doesn't appear useful. |
| 2346 | * Halving the number of backslashes is incompatible with previous |
| 2347 | * versions. */ |
| 2348 | if (*p == '\\' && p[1] == '\n') |
| 2349 | mch_memmove(p, p + 1, STRLEN(p)); |
| 2350 | else if (*p == '\n') |
| 2351 | { |
| 2352 | ea.nextcmd = p + 1; |
| 2353 | *p = NUL; |
| 2354 | break; |
| 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | if ((ea.argt & DFLALL) && ea.addr_count == 0) |
| 2360 | { |
| 2361 | ea.line1 = 1; |
| 2362 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 2363 | } |
| 2364 | |
| 2365 | /* accept numbered register only when no count allowed (:put) */ |
| 2366 | if ( (ea.argt & REGSTR) |
| 2367 | && *ea.arg != NUL |
| 2368 | #ifdef FEAT_USR_CMDS |
| 2369 | && valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put |
| 2370 | && USER_CMDIDX(ea.cmdidx))) |
| 2371 | /* Do not allow register = for user commands */ |
| 2372 | && (!USER_CMDIDX(ea.cmdidx) || *ea.arg != '=') |
| 2373 | #else |
| 2374 | && valid_yank_reg(*ea.arg, ea.cmdidx != CMD_put) |
| 2375 | #endif |
| 2376 | && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg))) |
| 2377 | { |
| 2378 | ea.regname = *ea.arg++; |
| 2379 | #ifdef FEAT_EVAL |
| 2380 | /* for '=' register: accept the rest of the line as an expression */ |
| 2381 | if (ea.arg[-1] == '=' && ea.arg[0] != NUL) |
| 2382 | { |
| 2383 | set_expr_line(vim_strsave(ea.arg)); |
| 2384 | ea.arg += STRLEN(ea.arg); |
| 2385 | } |
| 2386 | #endif |
| 2387 | ea.arg = skipwhite(ea.arg); |
| 2388 | } |
| 2389 | |
| 2390 | /* |
| 2391 | * Check for a count. When accepting a BUFNAME, don't use "123foo" as a |
| 2392 | * count, it's a buffer name. |
| 2393 | */ |
| 2394 | if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg) |
| 2395 | && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL |
| 2396 | || vim_iswhite(*p))) |
| 2397 | { |
| 2398 | n = getdigits(&ea.arg); |
| 2399 | ea.arg = skipwhite(ea.arg); |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 2400 | if (n <= 0 && !ni && (ea.argt & ZEROR) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2401 | { |
| 2402 | errormsg = (char_u *)_(e_zerocount); |
| 2403 | goto doend; |
| 2404 | } |
| 2405 | if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */ |
| 2406 | { |
| 2407 | ea.line2 = n; |
| 2408 | if (ea.addr_count == 0) |
| 2409 | ea.addr_count = 1; |
| 2410 | } |
| 2411 | else |
| 2412 | { |
| 2413 | ea.line1 = ea.line2; |
| 2414 | ea.line2 += n - 1; |
| 2415 | ++ea.addr_count; |
| 2416 | /* |
| 2417 | * Be vi compatible: no error message for out of range. |
| 2418 | */ |
| 2419 | if (ea.line2 > curbuf->b_ml.ml_line_count) |
| 2420 | ea.line2 = curbuf->b_ml.ml_line_count; |
| 2421 | } |
| 2422 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2423 | |
| 2424 | /* |
| 2425 | * Check for flags: 'l', 'p' and '#'. |
| 2426 | */ |
| 2427 | if (ea.argt & EXFLAGS) |
| 2428 | get_flags(&ea); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | /* no arguments allowed */ |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 2430 | if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL |
Bram Moolenaar | a203182 | 2006-03-07 22:29:51 +0000 | [diff] [blame] | 2431 | && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2432 | { |
| 2433 | errormsg = (char_u *)_(e_trailing); |
| 2434 | goto doend; |
| 2435 | } |
| 2436 | |
| 2437 | if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL) |
| 2438 | { |
| 2439 | errormsg = (char_u *)_(e_argreq); |
| 2440 | goto doend; |
| 2441 | } |
| 2442 | |
| 2443 | #ifdef FEAT_EVAL |
| 2444 | /* |
| 2445 | * Skip the command when it's not going to be executed. |
| 2446 | * The commands like :if, :endif, etc. always need to be executed. |
| 2447 | * Also make an exception for commands that handle a trailing command |
| 2448 | * themselves. |
| 2449 | */ |
| 2450 | if (ea.skip) |
| 2451 | { |
| 2452 | switch (ea.cmdidx) |
| 2453 | { |
| 2454 | /* commands that need evaluation */ |
| 2455 | case CMD_while: |
| 2456 | case CMD_endwhile: |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 2457 | case CMD_for: |
| 2458 | case CMD_endfor: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2459 | case CMD_if: |
| 2460 | case CMD_elseif: |
| 2461 | case CMD_else: |
| 2462 | case CMD_endif: |
| 2463 | case CMD_try: |
| 2464 | case CMD_catch: |
| 2465 | case CMD_finally: |
| 2466 | case CMD_endtry: |
| 2467 | case CMD_function: |
| 2468 | break; |
| 2469 | |
| 2470 | /* Commands that handle '|' themselves. Check: A command should |
| 2471 | * either have the TRLBAR flag, appear in this list or appear in |
| 2472 | * the list at ":help :bar". */ |
| 2473 | case CMD_aboveleft: |
| 2474 | case CMD_and: |
| 2475 | case CMD_belowright: |
| 2476 | case CMD_botright: |
| 2477 | case CMD_browse: |
| 2478 | case CMD_call: |
| 2479 | case CMD_confirm: |
| 2480 | case CMD_delfunction: |
| 2481 | case CMD_djump: |
| 2482 | case CMD_dlist: |
| 2483 | case CMD_dsearch: |
| 2484 | case CMD_dsplit: |
| 2485 | case CMD_echo: |
| 2486 | case CMD_echoerr: |
| 2487 | case CMD_echomsg: |
| 2488 | case CMD_echon: |
| 2489 | case CMD_execute: |
| 2490 | case CMD_help: |
| 2491 | case CMD_hide: |
| 2492 | case CMD_ijump: |
| 2493 | case CMD_ilist: |
| 2494 | case CMD_isearch: |
| 2495 | case CMD_isplit: |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2496 | case CMD_keepalt: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2497 | case CMD_keepjumps: |
| 2498 | case CMD_keepmarks: |
| 2499 | case CMD_leftabove: |
| 2500 | case CMD_let: |
| 2501 | case CMD_lockmarks: |
| 2502 | case CMD_match: |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 2503 | case CMD_mzscheme: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2504 | case CMD_perl: |
| 2505 | case CMD_psearch: |
| 2506 | case CMD_python: |
| 2507 | case CMD_return: |
| 2508 | case CMD_rightbelow: |
| 2509 | case CMD_ruby: |
| 2510 | case CMD_silent: |
| 2511 | case CMD_smagic: |
| 2512 | case CMD_snomagic: |
| 2513 | case CMD_substitute: |
| 2514 | case CMD_syntax: |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 2515 | case CMD_tab: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2516 | case CMD_tcl: |
| 2517 | case CMD_throw: |
| 2518 | case CMD_tilde: |
| 2519 | case CMD_topleft: |
| 2520 | case CMD_unlet: |
| 2521 | case CMD_verbose: |
| 2522 | case CMD_vertical: |
| 2523 | break; |
| 2524 | |
| 2525 | default: goto doend; |
| 2526 | } |
| 2527 | } |
| 2528 | #endif |
| 2529 | |
| 2530 | if (ea.argt & XFILE) |
| 2531 | { |
| 2532 | if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL) |
| 2533 | goto doend; |
| 2534 | } |
| 2535 | |
| 2536 | #ifdef FEAT_LISTCMDS |
| 2537 | /* |
| 2538 | * Accept buffer name. Cannot be used at the same time with a buffer |
| 2539 | * number. Don't do this for a user command. |
| 2540 | */ |
| 2541 | if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0 |
| 2542 | # ifdef FEAT_USR_CMDS |
| 2543 | && !USER_CMDIDX(ea.cmdidx) |
| 2544 | # endif |
| 2545 | ) |
| 2546 | { |
| 2547 | /* |
| 2548 | * :bdelete, :bwipeout and :bunload take several arguments, separated |
| 2549 | * by spaces: find next space (skipping over escaped characters). |
| 2550 | * The others take one argument: ignore trailing spaces. |
| 2551 | */ |
| 2552 | if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout |
| 2553 | || ea.cmdidx == CMD_bunload) |
| 2554 | p = skiptowhite_esc(ea.arg); |
| 2555 | else |
| 2556 | { |
| 2557 | p = ea.arg + STRLEN(ea.arg); |
| 2558 | while (p > ea.arg && vim_iswhite(p[-1])) |
| 2559 | --p; |
| 2560 | } |
| 2561 | ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0, FALSE); |
| 2562 | if (ea.line2 < 0) /* failed */ |
| 2563 | goto doend; |
| 2564 | ea.addr_count = 1; |
| 2565 | ea.arg = skipwhite(p); |
| 2566 | } |
| 2567 | #endif |
| 2568 | |
| 2569 | /* |
| 2570 | * 6. switch on command name |
| 2571 | * |
| 2572 | * The "ea" structure holds the arguments that can be used. |
| 2573 | */ |
| 2574 | ea.cmdlinep = cmdlinep; |
| 2575 | ea.getline = getline; |
| 2576 | ea.cookie = cookie; |
| 2577 | #ifdef FEAT_EVAL |
| 2578 | ea.cstack = cstack; |
| 2579 | #endif |
| 2580 | |
| 2581 | #ifdef FEAT_USR_CMDS |
| 2582 | if (USER_CMDIDX(ea.cmdidx)) |
| 2583 | { |
| 2584 | /* |
| 2585 | * Execute a user-defined command. |
| 2586 | */ |
| 2587 | do_ucmd(&ea); |
| 2588 | } |
| 2589 | else |
| 2590 | #endif |
| 2591 | { |
| 2592 | /* |
| 2593 | * Call the function to execute the command. |
| 2594 | */ |
| 2595 | ea.errmsg = NULL; |
| 2596 | (cmdnames[ea.cmdidx].cmd_func)(&ea); |
| 2597 | if (ea.errmsg != NULL) |
| 2598 | errormsg = (char_u *)_(ea.errmsg); |
| 2599 | } |
| 2600 | |
| 2601 | #ifdef FEAT_EVAL |
| 2602 | /* |
| 2603 | * If the command just executed called do_cmdline(), any throw or ":return" |
| 2604 | * or ":finish" encountered there must also check the cstack of the still |
| 2605 | * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught |
| 2606 | * exception, or reanimate a returned function or finished script file and |
| 2607 | * return or finish it again. |
| 2608 | */ |
| 2609 | if (need_rethrow) |
| 2610 | do_throw(cstack); |
| 2611 | else if (check_cstack) |
| 2612 | { |
| 2613 | if (source_finished(getline, cookie)) |
| 2614 | do_finish(&ea, TRUE); |
| 2615 | else if (getline_equal(getline, cookie, get_func_line) |
| 2616 | && current_func_returned()) |
| 2617 | do_return(&ea, TRUE, FALSE, NULL); |
| 2618 | } |
| 2619 | need_rethrow = check_cstack = FALSE; |
| 2620 | #endif |
| 2621 | |
| 2622 | doend: |
| 2623 | if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */ |
| 2624 | curwin->w_cursor.lnum = 1; |
| 2625 | |
| 2626 | if (errormsg != NULL && *errormsg != NUL && !did_emsg) |
| 2627 | { |
| 2628 | if (sourcing) |
| 2629 | { |
| 2630 | if (errormsg != IObuff) |
| 2631 | { |
| 2632 | STRCPY(IObuff, errormsg); |
| 2633 | errormsg = IObuff; |
| 2634 | } |
| 2635 | STRCAT(errormsg, ": "); |
| 2636 | STRNCAT(errormsg, *cmdlinep, IOSIZE - STRLEN(IObuff)); |
| 2637 | } |
| 2638 | emsg(errormsg); |
| 2639 | } |
| 2640 | #ifdef FEAT_EVAL |
| 2641 | do_errthrow(cstack, |
| 2642 | (ea.cmdidx != CMD_SIZE |
| 2643 | # ifdef FEAT_USR_CMDS |
| 2644 | && !USER_CMDIDX(ea.cmdidx) |
| 2645 | # endif |
| 2646 | ) ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL); |
| 2647 | #endif |
| 2648 | |
| 2649 | if (verbose_save >= 0) |
| 2650 | p_verbose = verbose_save; |
Bram Moolenaar | cdbac1e | 2005-12-11 21:27:22 +0000 | [diff] [blame] | 2651 | #ifdef FEAT_AUTOCMD |
| 2652 | if (cmdmod.save_ei != NULL) |
| 2653 | { |
| 2654 | /* Restore 'eventignore' to the value before ":noautocmd". */ |
Bram Moolenaar | 5e3cb7e | 2006-02-27 23:58:35 +0000 | [diff] [blame] | 2655 | set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei, |
| 2656 | OPT_FREE, SID_NONE); |
Bram Moolenaar | cdbac1e | 2005-12-11 21:27:22 +0000 | [diff] [blame] | 2657 | free_string_option(cmdmod.save_ei); |
| 2658 | } |
| 2659 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | |
| 2661 | cmdmod = save_cmdmod; |
| 2662 | |
| 2663 | if (did_silent > 0) |
| 2664 | { |
| 2665 | /* messages could be enabled for a serious error, need to check if the |
| 2666 | * counters don't become negative */ |
| 2667 | msg_silent -= did_silent; |
| 2668 | if (msg_silent < 0) |
| 2669 | msg_silent = 0; |
| 2670 | emsg_silent -= did_esilent; |
| 2671 | if (emsg_silent < 0) |
| 2672 | emsg_silent = 0; |
| 2673 | /* Restore msg_scroll, it's set by file I/O commands, even when no |
| 2674 | * message is actually displayed. */ |
| 2675 | msg_scroll = save_msg_scroll; |
| 2676 | } |
| 2677 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 2678 | #ifdef HAVE_SANDBOX |
| 2679 | if (did_sandbox) |
| 2680 | --sandbox; |
| 2681 | #endif |
| 2682 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2683 | if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */ |
| 2684 | ea.nextcmd = NULL; |
| 2685 | |
| 2686 | #ifdef FEAT_EVAL |
| 2687 | --ex_nesting_level; |
| 2688 | #endif |
| 2689 | |
| 2690 | return ea.nextcmd; |
| 2691 | } |
| 2692 | #if (_MSC_VER == 1200) |
Bram Moolenaar | 281bdce | 2005-01-25 21:53:18 +0000 | [diff] [blame] | 2693 | #pragma optimize( "", on ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2694 | #endif |
| 2695 | |
| 2696 | /* |
Bram Moolenaar | c5a1e80 | 2005-01-11 21:21:40 +0000 | [diff] [blame] | 2697 | * Check for an Ex command with optional tail. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2698 | * If there is a match advance "pp" to the argument and return TRUE. |
| 2699 | */ |
Bram Moolenaar | c5a1e80 | 2005-01-11 21:21:40 +0000 | [diff] [blame] | 2700 | int |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | checkforcmd(pp, cmd, len) |
Bram Moolenaar | c5a1e80 | 2005-01-11 21:21:40 +0000 | [diff] [blame] | 2702 | char_u **pp; /* start of command */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2703 | char *cmd; /* name of command */ |
| 2704 | int len; /* required length */ |
| 2705 | { |
| 2706 | int i; |
| 2707 | |
| 2708 | for (i = 0; cmd[i] != NUL; ++i) |
| 2709 | if (cmd[i] != (*pp)[i]) |
| 2710 | break; |
| 2711 | if (i >= len && !isalpha((*pp)[i])) |
| 2712 | { |
| 2713 | *pp = skipwhite(*pp + i); |
| 2714 | return TRUE; |
| 2715 | } |
| 2716 | return FALSE; |
| 2717 | } |
| 2718 | |
| 2719 | /* |
| 2720 | * Find an Ex command by its name, either built-in or user. |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2721 | * Start of the name can be found at eap->cmd. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2722 | * Returns pointer to char after the command name. |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2723 | * "full" is set to TRUE if the whole command name matched. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2724 | * Returns NULL for an ambiguous user command. |
| 2725 | */ |
| 2726 | /*ARGSUSED*/ |
| 2727 | static char_u * |
| 2728 | find_command(eap, full) |
| 2729 | exarg_T *eap; |
| 2730 | int *full; |
| 2731 | { |
| 2732 | int len; |
| 2733 | char_u *p; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2734 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | |
| 2736 | /* |
| 2737 | * Isolate the command and search for it in the command table. |
| 2738 | * Exeptions: |
| 2739 | * - the 'k' command can directly be followed by any character. |
| 2740 | * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' |
| 2741 | * but :sre[wind] is another command, as are :scrip[tnames], |
| 2742 | * :scs[cope], :sim[alt], :sig[ns] and :sil[ent]. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2743 | * - the "d" command can directly be followed by 'l' or 'p' flag. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2744 | */ |
| 2745 | p = eap->cmd; |
| 2746 | if (*p == 'k') |
| 2747 | { |
| 2748 | eap->cmdidx = CMD_k; |
| 2749 | ++p; |
| 2750 | } |
| 2751 | else if (p[0] == 's' |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2752 | && ((p[1] == 'c' && p[2] != 's' && p[2] != 'r' |
| 2753 | && p[3] != 'i' && p[4] != 'p') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | || p[1] == 'g' |
| 2755 | || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g') |
| 2756 | || p[1] == 'I' |
| 2757 | || (p[1] == 'r' && p[2] != 'e'))) |
| 2758 | { |
| 2759 | eap->cmdidx = CMD_substitute; |
| 2760 | ++p; |
| 2761 | } |
| 2762 | else |
| 2763 | { |
| 2764 | while (ASCII_ISALPHA(*p)) |
| 2765 | ++p; |
| 2766 | /* check for non-alpha command */ |
| 2767 | if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) |
| 2768 | ++p; |
| 2769 | len = (int)(p - eap->cmd); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2770 | if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p')) |
| 2771 | { |
| 2772 | /* Check for ":dl", ":dell", etc. to ":deletel": that's |
| 2773 | * :delete with the 'l' flag. Same for 'p'. */ |
| 2774 | for (i = 0; i < len; ++i) |
| 2775 | if (eap->cmd[i] != "delete"[i]) |
| 2776 | break; |
| 2777 | if (i == len - 1) |
| 2778 | { |
| 2779 | --len; |
| 2780 | if (p[-1] == 'l') |
| 2781 | eap->flags |= EXFLAG_LIST; |
| 2782 | else |
| 2783 | eap->flags |= EXFLAG_PRINT; |
| 2784 | } |
| 2785 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2786 | |
| 2787 | if (ASCII_ISLOWER(*eap->cmd)) |
| 2788 | eap->cmdidx = cmdidxs[CharOrdLow(*eap->cmd)]; |
| 2789 | else |
| 2790 | eap->cmdidx = cmdidxs[26]; |
| 2791 | |
| 2792 | for ( ; (int)eap->cmdidx < (int)CMD_SIZE; |
| 2793 | eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1)) |
| 2794 | if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd, |
| 2795 | (size_t)len) == 0) |
| 2796 | { |
| 2797 | #ifdef FEAT_EVAL |
| 2798 | if (full != NULL |
| 2799 | && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL) |
| 2800 | *full = TRUE; |
| 2801 | #endif |
| 2802 | break; |
| 2803 | } |
| 2804 | |
| 2805 | #ifdef FEAT_USR_CMDS |
| 2806 | /* Look for a user defined command as a last resort */ |
| 2807 | if (eap->cmdidx == CMD_SIZE && *eap->cmd >= 'A' && *eap->cmd <= 'Z') |
| 2808 | { |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2809 | /* User defined commands may contain digits. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2810 | while (ASCII_ISALNUM(*p)) |
| 2811 | ++p; |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2812 | p = find_ucmd(eap, p, full, NULL, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2813 | } |
| 2814 | #endif |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2815 | if (p == eap->cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2816 | eap->cmdidx = CMD_SIZE; |
| 2817 | } |
| 2818 | |
| 2819 | return p; |
| 2820 | } |
| 2821 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2822 | #ifdef FEAT_USR_CMDS |
| 2823 | /* |
| 2824 | * Search for a user command that matches "eap->cmd". |
| 2825 | * Return cmdidx in "eap->cmdidx", flags in "eap->argt", idx in "eap->useridx". |
| 2826 | * Return a pointer to just after the command. |
| 2827 | * Return NULL if there is no matching command. |
| 2828 | */ |
| 2829 | static char_u * |
| 2830 | find_ucmd(eap, p, full, xp, compl) |
| 2831 | exarg_T *eap; |
| 2832 | char_u *p; /* end of the command (possibly including count) */ |
| 2833 | int *full; /* set to TRUE for a full match */ |
| 2834 | expand_T *xp; /* used for completion, NULL otherwise */ |
| 2835 | int *compl; /* completion flags or NULL */ |
| 2836 | { |
| 2837 | int len = (int)(p - eap->cmd); |
| 2838 | int j, k, matchlen = 0; |
| 2839 | ucmd_T *uc; |
| 2840 | int found = FALSE; |
| 2841 | int possible = FALSE; |
| 2842 | char_u *cp, *np; /* Point into typed cmd and test name */ |
| 2843 | garray_T *gap; |
| 2844 | int amb_local = FALSE; /* Found ambiguous buffer-local command, |
| 2845 | only full match global is accepted. */ |
| 2846 | |
| 2847 | /* |
| 2848 | * Look for buffer-local user commands first, then global ones. |
| 2849 | */ |
| 2850 | gap = &curbuf->b_ucmds; |
| 2851 | for (;;) |
| 2852 | { |
| 2853 | for (j = 0; j < gap->ga_len; ++j) |
| 2854 | { |
| 2855 | uc = USER_CMD_GA(gap, j); |
| 2856 | cp = eap->cmd; |
| 2857 | np = uc->uc_name; |
| 2858 | k = 0; |
| 2859 | while (k < len && *np != NUL && *cp++ == *np++) |
| 2860 | k++; |
| 2861 | if (k == len || (*np == NUL && vim_isdigit(eap->cmd[k]))) |
| 2862 | { |
| 2863 | /* If finding a second match, the command is ambiguous. But |
| 2864 | * not if a buffer-local command wasn't a full match and a |
| 2865 | * global command is a full match. */ |
| 2866 | if (k == len && found && *np != NUL) |
| 2867 | { |
| 2868 | if (gap == &ucmds) |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2869 | return NULL; |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 2870 | amb_local = TRUE; |
| 2871 | } |
| 2872 | |
| 2873 | if (!found || (k == len && *np == NUL)) |
| 2874 | { |
| 2875 | /* If we matched up to a digit, then there could |
| 2876 | * be another command including the digit that we |
| 2877 | * should use instead. |
| 2878 | */ |
| 2879 | if (k == len) |
| 2880 | found = TRUE; |
| 2881 | else |
| 2882 | possible = TRUE; |
| 2883 | |
| 2884 | if (gap == &ucmds) |
| 2885 | eap->cmdidx = CMD_USER; |
| 2886 | else |
| 2887 | eap->cmdidx = CMD_USER_BUF; |
| 2888 | eap->argt = uc->uc_argt; |
| 2889 | eap->useridx = j; |
| 2890 | |
| 2891 | # ifdef FEAT_CMDL_COMPL |
| 2892 | if (compl != NULL) |
| 2893 | *compl = uc->uc_compl; |
| 2894 | # ifdef FEAT_EVAL |
| 2895 | if (xp != NULL) |
| 2896 | { |
| 2897 | xp->xp_arg = uc->uc_compl_arg; |
| 2898 | xp->xp_scriptID = uc->uc_scriptID; |
| 2899 | } |
| 2900 | # endif |
| 2901 | # endif |
| 2902 | /* Do not search for further abbreviations |
| 2903 | * if this is an exact match. */ |
| 2904 | matchlen = k; |
| 2905 | if (k == len && *np == NUL) |
| 2906 | { |
| 2907 | if (full != NULL) |
| 2908 | *full = TRUE; |
| 2909 | amb_local = FALSE; |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | } |
| 2914 | } |
| 2915 | |
| 2916 | /* Stop if we found a full match or searched all. */ |
| 2917 | if (j < gap->ga_len || gap == &ucmds) |
| 2918 | break; |
| 2919 | gap = &ucmds; |
| 2920 | } |
| 2921 | |
| 2922 | /* Only found ambiguous matches. */ |
| 2923 | if (amb_local) |
| 2924 | { |
| 2925 | if (xp != NULL) |
| 2926 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 2927 | return NULL; |
| 2928 | } |
| 2929 | |
| 2930 | /* The match we found may be followed immediately by a number. Move "p" |
| 2931 | * back to point to it. */ |
| 2932 | if (found || possible) |
| 2933 | return p + (matchlen - len); |
| 2934 | return p; |
| 2935 | } |
| 2936 | #endif |
| 2937 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2938 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2939 | /* |
| 2940 | * Return > 0 if an Ex command "name" exists. |
| 2941 | * Return 2 if there is an exact match. |
| 2942 | * Return 3 if there is an ambiguous match. |
| 2943 | */ |
| 2944 | int |
| 2945 | cmd_exists(name) |
| 2946 | char_u *name; |
| 2947 | { |
| 2948 | exarg_T ea; |
| 2949 | int full = FALSE; |
| 2950 | int i; |
| 2951 | int j; |
| 2952 | static struct cmdmod |
| 2953 | { |
| 2954 | char *name; |
| 2955 | int minlen; |
| 2956 | } cmdmods[] = { |
| 2957 | {"aboveleft", 3}, |
| 2958 | {"belowright", 3}, |
| 2959 | {"botright", 2}, |
| 2960 | {"browse", 3}, |
| 2961 | {"confirm", 4}, |
| 2962 | {"hide", 3}, |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 2963 | {"keepalt", 5}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2964 | {"keepjumps", 5}, |
| 2965 | {"keepmarks", 3}, |
| 2966 | {"leftabove", 5}, |
| 2967 | {"lockmarks", 3}, |
| 2968 | {"rightbelow", 6}, |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 2969 | {"sandbox", 3}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | {"silent", 3}, |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 2971 | {"tab", 3}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2972 | {"topleft", 2}, |
| 2973 | {"verbose", 4}, |
| 2974 | {"vertical", 4}, |
| 2975 | }; |
| 2976 | |
| 2977 | /* Check command modifiers. */ |
| 2978 | for (i = 0; i < sizeof(cmdmods) / sizeof(struct cmdmod); ++i) |
| 2979 | { |
| 2980 | for (j = 0; name[j] != NUL; ++j) |
| 2981 | if (name[j] != cmdmods[i].name[j]) |
| 2982 | break; |
| 2983 | if (name[j] == NUL && j >= cmdmods[i].minlen) |
| 2984 | return (cmdmods[i].name[j] == NUL ? 2 : 1); |
| 2985 | } |
| 2986 | |
| 2987 | /* Check built-in commands and user defined commands. */ |
| 2988 | ea.cmd = name; |
| 2989 | ea.cmdidx = (cmdidx_T)0; |
| 2990 | if (find_command(&ea, &full) == NULL) |
| 2991 | return 3; |
| 2992 | return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1)); |
| 2993 | } |
| 2994 | #endif |
| 2995 | |
| 2996 | /* |
| 2997 | * This is all pretty much copied from do_one_cmd(), with all the extra stuff |
| 2998 | * we don't need/want deleted. Maybe this could be done better if we didn't |
| 2999 | * repeat all this stuff. The only problem is that they may not stay |
| 3000 | * perfectly compatible with each other, but then the command line syntax |
| 3001 | * probably won't change that much -- webb. |
| 3002 | */ |
| 3003 | char_u * |
| 3004 | set_one_cmd_context(xp, buff) |
| 3005 | expand_T *xp; |
| 3006 | char_u *buff; /* buffer for command string */ |
| 3007 | { |
| 3008 | char_u *p; |
| 3009 | char_u *cmd, *arg; |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3010 | int len = 0; |
| 3011 | exarg_T ea; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3012 | #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
| 3013 | int compl = EXPAND_NOTHING; |
| 3014 | #endif |
| 3015 | #ifdef FEAT_CMDL_COMPL |
| 3016 | int delim; |
| 3017 | #endif |
| 3018 | int forceit = FALSE; |
| 3019 | int usefilter = FALSE; /* filter instead of file name */ |
| 3020 | |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 3021 | ExpandInit(xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3022 | xp->xp_pattern = buff; |
| 3023 | xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3024 | ea.argt = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3025 | |
| 3026 | /* |
| 3027 | * 2. skip comment lines and leading space, colons or bars |
| 3028 | */ |
| 3029 | for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++) |
| 3030 | ; |
| 3031 | xp->xp_pattern = cmd; |
| 3032 | |
| 3033 | if (*cmd == NUL) |
| 3034 | return NULL; |
| 3035 | if (*cmd == '"') /* ignore comment lines */ |
| 3036 | { |
| 3037 | xp->xp_context = EXPAND_NOTHING; |
| 3038 | return NULL; |
| 3039 | } |
| 3040 | |
| 3041 | /* |
| 3042 | * 3. parse a range specifier of the form: addr [,addr] [;addr] .. |
| 3043 | */ |
| 3044 | cmd = skip_range(cmd, &xp->xp_context); |
| 3045 | |
| 3046 | /* |
| 3047 | * 4. parse command |
| 3048 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3049 | xp->xp_pattern = cmd; |
| 3050 | if (*cmd == NUL) |
| 3051 | return NULL; |
| 3052 | if (*cmd == '"') |
| 3053 | { |
| 3054 | xp->xp_context = EXPAND_NOTHING; |
| 3055 | return NULL; |
| 3056 | } |
| 3057 | |
| 3058 | if (*cmd == '|' || *cmd == '\n') |
| 3059 | return cmd + 1; /* There's another command */ |
| 3060 | |
| 3061 | /* |
| 3062 | * Isolate the command and search for it in the command table. |
| 3063 | * Exceptions: |
| 3064 | * - the 'k' command can directly be followed by any character, but |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3065 | * do accept "keepmarks", "keepalt" and "keepjumps". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3066 | * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r' |
| 3067 | */ |
| 3068 | if (*cmd == 'k' && cmd[1] != 'e') |
| 3069 | { |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3070 | ea.cmdidx = CMD_k; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3071 | p = cmd + 1; |
| 3072 | } |
| 3073 | else |
| 3074 | { |
| 3075 | p = cmd; |
| 3076 | while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */ |
| 3077 | ++p; |
| 3078 | /* check for non-alpha command */ |
| 3079 | if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL) |
| 3080 | ++p; |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3081 | len = (int)(p - cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3082 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3083 | if (len == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3084 | { |
| 3085 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 3086 | return NULL; |
| 3087 | } |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3088 | for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE; |
| 3089 | ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1)) |
| 3090 | if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd, (size_t)len) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | |
| 3093 | #ifdef FEAT_USR_CMDS |
| 3094 | if (cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 3095 | { |
| 3096 | while (ASCII_ISALNUM(*p) || *p == '*') /* Allow * wild card */ |
| 3097 | ++p; |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3098 | len = (int)(p - cmd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3099 | } |
| 3100 | #endif |
| 3101 | } |
| 3102 | |
| 3103 | /* |
| 3104 | * If the cursor is touching the command, and it ends in an alpha-numeric |
| 3105 | * character, complete the command name. |
| 3106 | */ |
| 3107 | if (*p == NUL && ASCII_ISALNUM(p[-1])) |
| 3108 | return NULL; |
| 3109 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3110 | if (ea.cmdidx == CMD_SIZE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3111 | { |
| 3112 | if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL) |
| 3113 | { |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3114 | ea.cmdidx = CMD_substitute; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3115 | p = cmd + 1; |
| 3116 | } |
| 3117 | #ifdef FEAT_USR_CMDS |
| 3118 | else if (cmd[0] >= 'A' && cmd[0] <= 'Z') |
| 3119 | { |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3120 | ea.cmd = cmd; |
| 3121 | p = find_ucmd(&ea, p, NULL, xp, |
| 3122 | # if defined(FEAT_CMDL_COMPL) |
| 3123 | &compl |
| 3124 | # else |
| 3125 | NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3126 | # endif |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3127 | ); |
Bram Moolenaar | ebefac6 | 2005-12-28 22:39:57 +0000 | [diff] [blame] | 3128 | if (p == NULL) |
| 3129 | ea.cmdidx = CMD_SIZE; /* ambiguous user command */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3130 | } |
| 3131 | #endif |
| 3132 | } |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3133 | if (ea.cmdidx == CMD_SIZE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3134 | { |
| 3135 | /* Not still touching the command and it was an illegal one */ |
| 3136 | xp->xp_context = EXPAND_UNSUCCESSFUL; |
| 3137 | return NULL; |
| 3138 | } |
| 3139 | |
| 3140 | xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */ |
| 3141 | |
| 3142 | if (*p == '!') /* forced commands */ |
| 3143 | { |
| 3144 | forceit = TRUE; |
| 3145 | ++p; |
| 3146 | } |
| 3147 | |
| 3148 | /* |
| 3149 | * 5. parse arguments |
| 3150 | */ |
| 3151 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3152 | if (!USER_CMDIDX(ea.cmdidx)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3153 | #endif |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3154 | ea.argt = cmdnames[(int)ea.cmdidx].cmd_argt; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3155 | |
| 3156 | arg = skipwhite(p); |
| 3157 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3158 | if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3159 | { |
| 3160 | if (*arg == '>') /* append */ |
| 3161 | { |
| 3162 | if (*++arg == '>') |
| 3163 | ++arg; |
| 3164 | arg = skipwhite(arg); |
| 3165 | } |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3166 | else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3167 | { |
| 3168 | ++arg; |
| 3169 | usefilter = TRUE; |
| 3170 | } |
| 3171 | } |
| 3172 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3173 | if (ea.cmdidx == CMD_read) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3174 | { |
| 3175 | usefilter = forceit; /* :r! filter if forced */ |
| 3176 | if (*arg == '!') /* :r !filter */ |
| 3177 | { |
| 3178 | ++arg; |
| 3179 | usefilter = TRUE; |
| 3180 | } |
| 3181 | } |
| 3182 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3183 | if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3184 | { |
| 3185 | while (*arg == *cmd) /* allow any number of '>' or '<' */ |
| 3186 | ++arg; |
| 3187 | arg = skipwhite(arg); |
| 3188 | } |
| 3189 | |
| 3190 | /* Does command allow "+command"? */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3191 | if ((ea.argt & EDITCMD) && !usefilter && *arg == '+') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3192 | { |
| 3193 | /* Check if we're in the +command */ |
| 3194 | p = arg + 1; |
| 3195 | arg = skip_cmd_arg(arg, FALSE); |
| 3196 | |
| 3197 | /* Still touching the command after '+'? */ |
| 3198 | if (*arg == NUL) |
| 3199 | return p; |
| 3200 | |
| 3201 | /* Skip space(s) after +command to get to the real argument */ |
| 3202 | arg = skipwhite(arg); |
| 3203 | } |
| 3204 | |
| 3205 | /* |
| 3206 | * Check for '|' to separate commands and '"' to start comments. |
| 3207 | * Don't do this for ":read !cmd" and ":write !cmd". |
| 3208 | */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3209 | if ((ea.argt & TRLBAR) && !usefilter) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3210 | { |
| 3211 | p = arg; |
| 3212 | /* ":redir @" is not the start of a comment */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3213 | if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3214 | p += 2; |
| 3215 | while (*p) |
| 3216 | { |
| 3217 | if (*p == Ctrl_V) |
| 3218 | { |
| 3219 | if (p[1] != NUL) |
| 3220 | ++p; |
| 3221 | } |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3222 | else if ( (*p == '"' && !(ea.argt & NOTRLCOM)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3223 | || *p == '|' || *p == '\n') |
| 3224 | { |
| 3225 | if (*(p - 1) != '\\') |
| 3226 | { |
| 3227 | if (*p == '|' || *p == '\n') |
| 3228 | return p + 1; |
| 3229 | return NULL; /* It's a comment */ |
| 3230 | } |
| 3231 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3232 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3233 | } |
| 3234 | } |
| 3235 | |
| 3236 | /* no arguments allowed */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3237 | if (!(ea.argt & EXTRA) && *arg != NUL && |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3238 | vim_strchr((char_u *)"|\"", *arg) == NULL) |
| 3239 | return NULL; |
| 3240 | |
| 3241 | /* Find start of last argument (argument just before cursor): */ |
| 3242 | p = buff + STRLEN(buff); |
| 3243 | while (p != arg && *p != ' ' && *p != TAB) |
| 3244 | p--; |
| 3245 | if (*p == ' ' || *p == TAB) |
| 3246 | p++; |
| 3247 | xp->xp_pattern = p; |
| 3248 | |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3249 | if (ea.argt & XFILE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3250 | { |
| 3251 | int in_quote = FALSE; |
| 3252 | char_u *bow = NULL; /* Beginning of word */ |
| 3253 | |
| 3254 | /* |
| 3255 | * Allow spaces within back-quotes to count as part of the argument |
| 3256 | * being expanded. |
| 3257 | */ |
| 3258 | xp->xp_pattern = skipwhite(arg); |
| 3259 | for (p = xp->xp_pattern; *p; ) |
| 3260 | { |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 3261 | if (*p == '\\' && p[1] != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3262 | ++p; |
| 3263 | #ifdef SPACE_IN_FILENAME |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3264 | else if (vim_iswhite(*p) && (!(ea.argt & NOSPC) || usefilter)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3265 | #else |
| 3266 | else if (vim_iswhite(*p)) |
| 3267 | #endif |
| 3268 | { |
| 3269 | p = skipwhite(p); |
| 3270 | if (in_quote) |
| 3271 | bow = p; |
| 3272 | else |
| 3273 | xp->xp_pattern = p; |
| 3274 | --p; |
| 3275 | } |
| 3276 | else if (*p == '`') |
| 3277 | { |
| 3278 | if (!in_quote) |
| 3279 | { |
| 3280 | xp->xp_pattern = p; |
| 3281 | bow = p + 1; |
| 3282 | } |
| 3283 | in_quote = !in_quote; |
| 3284 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3285 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | /* |
| 3289 | * If we are still inside the quotes, and we passed a space, just |
| 3290 | * expand from there. |
| 3291 | */ |
| 3292 | if (bow != NULL && in_quote) |
| 3293 | xp->xp_pattern = bow; |
| 3294 | xp->xp_context = EXPAND_FILES; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3295 | |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 3296 | #ifndef BACKSLASH_IN_FILENAME |
| 3297 | /* For a shell command more chars need to be escaped. */ |
| 3298 | if (usefilter || ea.cmdidx == CMD_bang) |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3299 | { |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 3300 | xp->xp_shell = TRUE; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3301 | |
| 3302 | /* When still after the command name expand executables. */ |
| 3303 | if (xp->xp_pattern == skipwhite(arg)) |
| 3304 | xp->xp_context = EXPAND_SHELLCMD; |
| 3305 | } |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 3306 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3307 | |
| 3308 | /* Check for environment variable */ |
| 3309 | if (*xp->xp_pattern == '$' |
| 3310 | #if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
| 3311 | || *xp->xp_pattern == '%' |
| 3312 | #endif |
| 3313 | ) |
| 3314 | { |
| 3315 | for (p = xp->xp_pattern + 1; *p != NUL; ++p) |
| 3316 | if (!vim_isIDc(*p)) |
| 3317 | break; |
| 3318 | if (*p == NUL) |
| 3319 | { |
| 3320 | xp->xp_context = EXPAND_ENV_VARS; |
| 3321 | ++xp->xp_pattern; |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3322 | #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
| 3323 | /* Avoid that the assignment uses EXPAND_FILES again. */ |
Bram Moolenaar | a466c99 | 2005-07-09 21:03:22 +0000 | [diff] [blame] | 3324 | if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST) |
Bram Moolenaar | 21cf823 | 2004-07-16 20:18:37 +0000 | [diff] [blame] | 3325 | compl = EXPAND_ENV_VARS; |
| 3326 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | /* |
| 3332 | * 6. switch on command name |
| 3333 | */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3334 | switch (ea.cmdidx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3335 | { |
| 3336 | case CMD_cd: |
| 3337 | case CMD_chdir: |
| 3338 | case CMD_lcd: |
| 3339 | case CMD_lchdir: |
| 3340 | if (xp->xp_context == EXPAND_FILES) |
| 3341 | xp->xp_context = EXPAND_DIRECTORIES; |
| 3342 | break; |
| 3343 | case CMD_help: |
| 3344 | xp->xp_context = EXPAND_HELP; |
| 3345 | xp->xp_pattern = arg; |
| 3346 | break; |
| 3347 | |
Bram Moolenaar | df7b1ff | 2005-03-11 22:40:50 +0000 | [diff] [blame] | 3348 | /* Command modifiers: return the argument. |
| 3349 | * Also for commands with an argument that is a command. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3350 | case CMD_aboveleft: |
Bram Moolenaar | df7b1ff | 2005-03-11 22:40:50 +0000 | [diff] [blame] | 3351 | case CMD_argdo: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3352 | case CMD_belowright: |
| 3353 | case CMD_botright: |
| 3354 | case CMD_browse: |
Bram Moolenaar | df7b1ff | 2005-03-11 22:40:50 +0000 | [diff] [blame] | 3355 | case CMD_bufdo: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3356 | case CMD_confirm: |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3357 | case CMD_debug: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3358 | case CMD_folddoclosed: |
| 3359 | case CMD_folddoopen: |
| 3360 | case CMD_hide: |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 3361 | case CMD_keepalt: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3362 | case CMD_keepjumps: |
| 3363 | case CMD_keepmarks: |
| 3364 | case CMD_leftabove: |
| 3365 | case CMD_lockmarks: |
| 3366 | case CMD_rightbelow: |
Bram Moolenaar | df7b1ff | 2005-03-11 22:40:50 +0000 | [diff] [blame] | 3367 | case CMD_sandbox: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3368 | case CMD_silent: |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 3369 | case CMD_tab: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3370 | case CMD_topleft: |
| 3371 | case CMD_verbose: |
| 3372 | case CMD_vertical: |
Bram Moolenaar | df7b1ff | 2005-03-11 22:40:50 +0000 | [diff] [blame] | 3373 | case CMD_windo: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3374 | return arg; |
| 3375 | |
| 3376 | #ifdef FEAT_SEARCH_EXTRA |
| 3377 | case CMD_match: |
| 3378 | if (*arg == NUL || !ends_excmd(*arg)) |
| 3379 | { |
| 3380 | /* Dummy call to clear variables. */ |
| 3381 | set_context_in_highlight_cmd(xp, (char_u *)"link n"); |
| 3382 | xp->xp_context = EXPAND_HIGHLIGHT; |
| 3383 | xp->xp_pattern = arg; |
| 3384 | arg = skipwhite(skiptowhite(arg)); |
| 3385 | if (*arg != NUL) |
| 3386 | { |
| 3387 | xp->xp_context = EXPAND_NOTHING; |
| 3388 | arg = skip_regexp(arg + 1, *arg, p_magic, NULL); |
| 3389 | } |
| 3390 | } |
| 3391 | return find_nextcmd(arg); |
| 3392 | #endif |
| 3393 | |
| 3394 | #ifdef FEAT_CMDL_COMPL |
| 3395 | /* |
| 3396 | * All completion for the +cmdline_compl feature goes here. |
| 3397 | */ |
| 3398 | |
| 3399 | # ifdef FEAT_USR_CMDS |
| 3400 | case CMD_command: |
| 3401 | /* Check for attributes */ |
| 3402 | while (*arg == '-') |
| 3403 | { |
| 3404 | arg++; /* Skip "-" */ |
| 3405 | p = skiptowhite(arg); |
| 3406 | if (*p == NUL) |
| 3407 | { |
| 3408 | /* Cursor is still in the attribute */ |
| 3409 | p = vim_strchr(arg, '='); |
| 3410 | if (p == NULL) |
| 3411 | { |
| 3412 | /* No "=", so complete attribute names */ |
| 3413 | xp->xp_context = EXPAND_USER_CMD_FLAGS; |
| 3414 | xp->xp_pattern = arg; |
| 3415 | return NULL; |
| 3416 | } |
| 3417 | |
| 3418 | /* For the -complete and -nargs attributes, we complete |
| 3419 | * their arguments as well. |
| 3420 | */ |
| 3421 | if (STRNICMP(arg, "complete", p - arg) == 0) |
| 3422 | { |
| 3423 | xp->xp_context = EXPAND_USER_COMPLETE; |
| 3424 | xp->xp_pattern = p + 1; |
| 3425 | return NULL; |
| 3426 | } |
| 3427 | else if (STRNICMP(arg, "nargs", p - arg) == 0) |
| 3428 | { |
| 3429 | xp->xp_context = EXPAND_USER_NARGS; |
| 3430 | xp->xp_pattern = p + 1; |
| 3431 | return NULL; |
| 3432 | } |
| 3433 | return NULL; |
| 3434 | } |
| 3435 | arg = skipwhite(p); |
| 3436 | } |
| 3437 | |
| 3438 | /* After the attributes comes the new command name */ |
| 3439 | p = skiptowhite(arg); |
| 3440 | if (*p == NUL) |
| 3441 | { |
| 3442 | xp->xp_context = EXPAND_USER_COMMANDS; |
| 3443 | xp->xp_pattern = arg; |
| 3444 | break; |
| 3445 | } |
| 3446 | |
| 3447 | /* And finally comes a normal command */ |
| 3448 | return skipwhite(p); |
| 3449 | |
| 3450 | case CMD_delcommand: |
| 3451 | xp->xp_context = EXPAND_USER_COMMANDS; |
| 3452 | xp->xp_pattern = arg; |
| 3453 | break; |
| 3454 | # endif |
| 3455 | |
| 3456 | case CMD_global: |
| 3457 | case CMD_vglobal: |
| 3458 | delim = *arg; /* get the delimiter */ |
| 3459 | if (delim) |
| 3460 | ++arg; /* skip delimiter if there is one */ |
| 3461 | |
| 3462 | while (arg[0] != NUL && arg[0] != delim) |
| 3463 | { |
| 3464 | if (arg[0] == '\\' && arg[1] != NUL) |
| 3465 | ++arg; |
| 3466 | ++arg; |
| 3467 | } |
| 3468 | if (arg[0] != NUL) |
| 3469 | return arg + 1; |
| 3470 | break; |
| 3471 | case CMD_and: |
| 3472 | case CMD_substitute: |
| 3473 | delim = *arg; |
| 3474 | if (delim) |
| 3475 | { |
| 3476 | /* skip "from" part */ |
| 3477 | ++arg; |
| 3478 | arg = skip_regexp(arg, delim, p_magic, NULL); |
| 3479 | } |
| 3480 | /* skip "to" part */ |
| 3481 | while (arg[0] != NUL && arg[0] != delim) |
| 3482 | { |
| 3483 | if (arg[0] == '\\' && arg[1] != NUL) |
| 3484 | ++arg; |
| 3485 | ++arg; |
| 3486 | } |
| 3487 | if (arg[0] != NUL) /* skip delimiter */ |
| 3488 | ++arg; |
| 3489 | while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL) |
| 3490 | ++arg; |
| 3491 | if (arg[0] != NUL) |
| 3492 | return arg; |
| 3493 | break; |
| 3494 | case CMD_isearch: |
| 3495 | case CMD_dsearch: |
| 3496 | case CMD_ilist: |
| 3497 | case CMD_dlist: |
| 3498 | case CMD_ijump: |
| 3499 | case CMD_psearch: |
| 3500 | case CMD_djump: |
| 3501 | case CMD_isplit: |
| 3502 | case CMD_dsplit: |
| 3503 | arg = skipwhite(skipdigits(arg)); /* skip count */ |
| 3504 | if (*arg == '/') /* Match regexp, not just whole words */ |
| 3505 | { |
| 3506 | for (++arg; *arg && *arg != '/'; arg++) |
| 3507 | if (*arg == '\\' && arg[1] != NUL) |
| 3508 | arg++; |
| 3509 | if (*arg) |
| 3510 | { |
| 3511 | arg = skipwhite(arg + 1); |
| 3512 | |
| 3513 | /* Check for trailing illegal characters */ |
| 3514 | if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL) |
| 3515 | xp->xp_context = EXPAND_NOTHING; |
| 3516 | else |
| 3517 | return arg; |
| 3518 | } |
| 3519 | } |
| 3520 | break; |
| 3521 | #ifdef FEAT_AUTOCMD |
| 3522 | case CMD_autocmd: |
| 3523 | return set_context_in_autocmd(xp, arg, FALSE); |
| 3524 | |
| 3525 | case CMD_doautocmd: |
| 3526 | return set_context_in_autocmd(xp, arg, TRUE); |
| 3527 | #endif |
| 3528 | case CMD_set: |
| 3529 | set_context_in_set_cmd(xp, arg, 0); |
| 3530 | break; |
| 3531 | case CMD_setglobal: |
| 3532 | set_context_in_set_cmd(xp, arg, OPT_GLOBAL); |
| 3533 | break; |
| 3534 | case CMD_setlocal: |
| 3535 | set_context_in_set_cmd(xp, arg, OPT_LOCAL); |
| 3536 | break; |
| 3537 | case CMD_tag: |
| 3538 | case CMD_stag: |
| 3539 | case CMD_ptag: |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 3540 | case CMD_ltag: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3541 | case CMD_tselect: |
| 3542 | case CMD_stselect: |
| 3543 | case CMD_ptselect: |
| 3544 | case CMD_tjump: |
| 3545 | case CMD_stjump: |
| 3546 | case CMD_ptjump: |
Bram Moolenaar | b5bf5b8 | 2004-12-24 14:35:23 +0000 | [diff] [blame] | 3547 | if (*p_wop != NUL) |
| 3548 | xp->xp_context = EXPAND_TAGS_LISTFILES; |
| 3549 | else |
| 3550 | xp->xp_context = EXPAND_TAGS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3551 | xp->xp_pattern = arg; |
| 3552 | break; |
| 3553 | case CMD_augroup: |
| 3554 | xp->xp_context = EXPAND_AUGROUP; |
| 3555 | xp->xp_pattern = arg; |
| 3556 | break; |
| 3557 | #ifdef FEAT_SYN_HL |
| 3558 | case CMD_syntax: |
| 3559 | set_context_in_syntax_cmd(xp, arg); |
| 3560 | break; |
| 3561 | #endif |
| 3562 | #ifdef FEAT_EVAL |
| 3563 | case CMD_let: |
| 3564 | case CMD_if: |
| 3565 | case CMD_elseif: |
| 3566 | case CMD_while: |
Bram Moolenaar | b32ce2d | 2005-01-05 22:07:01 +0000 | [diff] [blame] | 3567 | case CMD_for: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3568 | case CMD_echo: |
| 3569 | case CMD_echon: |
| 3570 | case CMD_execute: |
| 3571 | case CMD_echomsg: |
| 3572 | case CMD_echoerr: |
| 3573 | case CMD_call: |
| 3574 | case CMD_return: |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3575 | set_context_for_expression(xp, arg, ea.cmdidx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3576 | break; |
| 3577 | |
| 3578 | case CMD_unlet: |
| 3579 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 3580 | arg = xp->xp_pattern + 1; |
| 3581 | xp->xp_context = EXPAND_USER_VARS; |
| 3582 | xp->xp_pattern = arg; |
| 3583 | break; |
| 3584 | |
| 3585 | case CMD_function: |
| 3586 | case CMD_delfunction: |
| 3587 | xp->xp_context = EXPAND_USER_FUNC; |
| 3588 | xp->xp_pattern = arg; |
| 3589 | break; |
| 3590 | |
| 3591 | case CMD_echohl: |
| 3592 | xp->xp_context = EXPAND_HIGHLIGHT; |
| 3593 | xp->xp_pattern = arg; |
| 3594 | break; |
| 3595 | #endif |
| 3596 | case CMD_highlight: |
| 3597 | set_context_in_highlight_cmd(xp, arg); |
| 3598 | break; |
| 3599 | #ifdef FEAT_LISTCMDS |
| 3600 | case CMD_bdelete: |
| 3601 | case CMD_bwipeout: |
| 3602 | case CMD_bunload: |
| 3603 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 3604 | arg = xp->xp_pattern + 1; |
| 3605 | /*FALLTHROUGH*/ |
| 3606 | case CMD_buffer: |
| 3607 | case CMD_sbuffer: |
| 3608 | case CMD_checktime: |
| 3609 | xp->xp_context = EXPAND_BUFFERS; |
| 3610 | xp->xp_pattern = arg; |
| 3611 | break; |
| 3612 | #endif |
| 3613 | #ifdef FEAT_USR_CMDS |
| 3614 | case CMD_USER: |
| 3615 | case CMD_USER_BUF: |
| 3616 | if (compl != EXPAND_NOTHING) |
| 3617 | { |
| 3618 | /* XFILE: file names are handled above */ |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3619 | if (!(ea.argt & XFILE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3620 | { |
| 3621 | # ifdef FEAT_MENU |
| 3622 | if (compl == EXPAND_MENUS) |
| 3623 | return set_context_in_menu_cmd(xp, cmd, arg, forceit); |
| 3624 | # endif |
| 3625 | if (compl == EXPAND_COMMANDS) |
| 3626 | return arg; |
| 3627 | if (compl == EXPAND_MAPPINGS) |
| 3628 | return set_context_in_map_cmd(xp, (char_u *)"map", |
| 3629 | arg, forceit, FALSE, FALSE, CMD_map); |
| 3630 | while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL) |
| 3631 | arg = xp->xp_pattern + 1; |
| 3632 | xp->xp_pattern = arg; |
| 3633 | } |
| 3634 | xp->xp_context = compl; |
| 3635 | } |
| 3636 | break; |
| 3637 | #endif |
| 3638 | case CMD_map: case CMD_noremap: |
| 3639 | case CMD_nmap: case CMD_nnoremap: |
| 3640 | case CMD_vmap: case CMD_vnoremap: |
| 3641 | case CMD_omap: case CMD_onoremap: |
| 3642 | case CMD_imap: case CMD_inoremap: |
| 3643 | case CMD_cmap: case CMD_cnoremap: |
| 3644 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3645 | FALSE, FALSE, ea.cmdidx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3646 | case CMD_unmap: |
| 3647 | case CMD_nunmap: |
| 3648 | case CMD_vunmap: |
| 3649 | case CMD_ounmap: |
| 3650 | case CMD_iunmap: |
| 3651 | case CMD_cunmap: |
| 3652 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3653 | FALSE, TRUE, ea.cmdidx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3654 | case CMD_abbreviate: case CMD_noreabbrev: |
| 3655 | case CMD_cabbrev: case CMD_cnoreabbrev: |
| 3656 | case CMD_iabbrev: case CMD_inoreabbrev: |
| 3657 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3658 | TRUE, FALSE, ea.cmdidx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3659 | case CMD_unabbreviate: |
| 3660 | case CMD_cunabbrev: |
| 3661 | case CMD_iunabbrev: |
| 3662 | return set_context_in_map_cmd(xp, cmd, arg, forceit, |
Bram Moolenaar | 52b4b55 | 2005-03-07 23:00:57 +0000 | [diff] [blame] | 3663 | TRUE, TRUE, ea.cmdidx); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3664 | #ifdef FEAT_MENU |
| 3665 | case CMD_menu: case CMD_noremenu: case CMD_unmenu: |
| 3666 | case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu: |
| 3667 | case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu: |
| 3668 | case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu: |
| 3669 | case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu: |
| 3670 | case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu: |
| 3671 | case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu: |
| 3672 | case CMD_tmenu: case CMD_tunmenu: |
| 3673 | case CMD_popup: case CMD_tearoff: case CMD_emenu: |
| 3674 | return set_context_in_menu_cmd(xp, cmd, arg, forceit); |
| 3675 | #endif |
| 3676 | |
| 3677 | case CMD_colorscheme: |
| 3678 | xp->xp_context = EXPAND_COLORS; |
| 3679 | xp->xp_pattern = arg; |
| 3680 | break; |
| 3681 | |
| 3682 | case CMD_compiler: |
| 3683 | xp->xp_context = EXPAND_COMPILER; |
| 3684 | xp->xp_pattern = arg; |
| 3685 | break; |
| 3686 | |
| 3687 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 3688 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
| 3689 | case CMD_language: |
| 3690 | if (*skiptowhite(arg) == NUL) |
| 3691 | { |
| 3692 | xp->xp_context = EXPAND_LANGUAGE; |
| 3693 | xp->xp_pattern = arg; |
| 3694 | } |
| 3695 | else |
| 3696 | xp->xp_context = EXPAND_NOTHING; |
| 3697 | break; |
| 3698 | #endif |
| 3699 | |
| 3700 | #endif /* FEAT_CMDL_COMPL */ |
| 3701 | |
| 3702 | default: |
| 3703 | break; |
| 3704 | } |
| 3705 | return NULL; |
| 3706 | } |
| 3707 | |
| 3708 | /* |
| 3709 | * skip a range specifier of the form: addr [,addr] [;addr] .. |
| 3710 | * |
| 3711 | * Backslashed delimiters after / or ? will be skipped, and commands will |
| 3712 | * not be expanded between /'s and ?'s or after "'". |
| 3713 | * |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3714 | * Also skip white space and ":" characters. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3715 | * Returns the "cmd" pointer advanced to beyond the range. |
| 3716 | */ |
| 3717 | char_u * |
| 3718 | skip_range(cmd, ctx) |
| 3719 | char_u *cmd; |
| 3720 | int *ctx; /* pointer to xp_context or NULL */ |
| 3721 | { |
| 3722 | int delim; |
| 3723 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3724 | while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;", *cmd) != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3725 | { |
| 3726 | if (*cmd == '\'') |
| 3727 | { |
| 3728 | if (*++cmd == NUL && ctx != NULL) |
| 3729 | *ctx = EXPAND_NOTHING; |
| 3730 | } |
| 3731 | else if (*cmd == '/' || *cmd == '?') |
| 3732 | { |
| 3733 | delim = *cmd++; |
| 3734 | while (*cmd != NUL && *cmd != delim) |
| 3735 | if (*cmd++ == '\\' && *cmd != NUL) |
| 3736 | ++cmd; |
| 3737 | if (*cmd == NUL && ctx != NULL) |
| 3738 | *ctx = EXPAND_NOTHING; |
| 3739 | } |
| 3740 | if (*cmd != NUL) |
| 3741 | ++cmd; |
| 3742 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3743 | |
| 3744 | /* Skip ":" and white space. */ |
| 3745 | while (*cmd == ':') |
| 3746 | cmd = skipwhite(cmd + 1); |
| 3747 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3748 | return cmd; |
| 3749 | } |
| 3750 | |
| 3751 | /* |
| 3752 | * get a single EX address |
| 3753 | * |
| 3754 | * Set ptr to the next character after the part that was interpreted. |
| 3755 | * Set ptr to NULL when an error is encountered. |
| 3756 | * |
| 3757 | * Return MAXLNUM when no Ex address was found. |
| 3758 | */ |
| 3759 | static linenr_T |
| 3760 | get_address(ptr, skip, to_other_file) |
| 3761 | char_u **ptr; |
| 3762 | int skip; /* only skip the address, don't use it */ |
| 3763 | int to_other_file; /* flag: may jump to other file */ |
| 3764 | { |
| 3765 | int c; |
| 3766 | int i; |
| 3767 | long n; |
| 3768 | char_u *cmd; |
| 3769 | pos_T pos; |
| 3770 | pos_T *fp; |
| 3771 | linenr_T lnum; |
| 3772 | |
| 3773 | cmd = skipwhite(*ptr); |
| 3774 | lnum = MAXLNUM; |
| 3775 | do |
| 3776 | { |
| 3777 | switch (*cmd) |
| 3778 | { |
| 3779 | case '.': /* '.' - Cursor position */ |
| 3780 | ++cmd; |
| 3781 | lnum = curwin->w_cursor.lnum; |
| 3782 | break; |
| 3783 | |
| 3784 | case '$': /* '$' - last line */ |
| 3785 | ++cmd; |
| 3786 | lnum = curbuf->b_ml.ml_line_count; |
| 3787 | break; |
| 3788 | |
| 3789 | case '\'': /* ''' - mark */ |
| 3790 | if (*++cmd == NUL) |
| 3791 | { |
| 3792 | cmd = NULL; |
| 3793 | goto error; |
| 3794 | } |
| 3795 | if (skip) |
| 3796 | ++cmd; |
| 3797 | else |
| 3798 | { |
| 3799 | /* Only accept a mark in another file when it is |
| 3800 | * used by itself: ":'M". */ |
| 3801 | fp = getmark(*cmd, to_other_file && cmd[1] == NUL); |
| 3802 | ++cmd; |
| 3803 | if (fp == (pos_T *)-1) |
| 3804 | /* Jumped to another file. */ |
| 3805 | lnum = curwin->w_cursor.lnum; |
| 3806 | else |
| 3807 | { |
| 3808 | if (check_mark(fp) == FAIL) |
| 3809 | { |
| 3810 | cmd = NULL; |
| 3811 | goto error; |
| 3812 | } |
| 3813 | lnum = fp->lnum; |
| 3814 | } |
| 3815 | } |
| 3816 | break; |
| 3817 | |
| 3818 | case '/': |
| 3819 | case '?': /* '/' or '?' - search */ |
| 3820 | c = *cmd++; |
| 3821 | if (skip) /* skip "/pat/" */ |
| 3822 | { |
| 3823 | cmd = skip_regexp(cmd, c, (int)p_magic, NULL); |
| 3824 | if (*cmd == c) |
| 3825 | ++cmd; |
| 3826 | } |
| 3827 | else |
| 3828 | { |
| 3829 | pos = curwin->w_cursor; /* save curwin->w_cursor */ |
| 3830 | /* |
| 3831 | * When '/' or '?' follows another address, start |
| 3832 | * from there. |
| 3833 | */ |
| 3834 | if (lnum != MAXLNUM) |
| 3835 | curwin->w_cursor.lnum = lnum; |
| 3836 | /* |
| 3837 | * Start a forward search at the end of the line. |
| 3838 | * Start a backward search at the start of the line. |
| 3839 | * This makes sure we never match in the current |
| 3840 | * line, and can match anywhere in the |
| 3841 | * next/previous line. |
| 3842 | */ |
| 3843 | if (c == '/') |
| 3844 | curwin->w_cursor.col = MAXCOL; |
| 3845 | else |
| 3846 | curwin->w_cursor.col = 0; |
| 3847 | searchcmdlen = 0; |
| 3848 | if (!do_search(NULL, c, cmd, 1L, |
| 3849 | SEARCH_HIS + SEARCH_MSG + SEARCH_START)) |
| 3850 | { |
| 3851 | curwin->w_cursor = pos; |
| 3852 | cmd = NULL; |
| 3853 | goto error; |
| 3854 | } |
| 3855 | lnum = curwin->w_cursor.lnum; |
| 3856 | curwin->w_cursor = pos; |
| 3857 | /* adjust command string pointer */ |
| 3858 | cmd += searchcmdlen; |
| 3859 | } |
| 3860 | break; |
| 3861 | |
| 3862 | case '\\': /* "\?", "\/" or "\&", repeat search */ |
| 3863 | ++cmd; |
| 3864 | if (*cmd == '&') |
| 3865 | i = RE_SUBST; |
| 3866 | else if (*cmd == '?' || *cmd == '/') |
| 3867 | i = RE_SEARCH; |
| 3868 | else |
| 3869 | { |
| 3870 | EMSG(_(e_backslash)); |
| 3871 | cmd = NULL; |
| 3872 | goto error; |
| 3873 | } |
| 3874 | |
| 3875 | if (!skip) |
| 3876 | { |
| 3877 | /* |
| 3878 | * When search follows another address, start from |
| 3879 | * there. |
| 3880 | */ |
| 3881 | if (lnum != MAXLNUM) |
| 3882 | pos.lnum = lnum; |
| 3883 | else |
| 3884 | pos.lnum = curwin->w_cursor.lnum; |
| 3885 | |
| 3886 | /* |
| 3887 | * Start the search just like for the above |
| 3888 | * do_search(). |
| 3889 | */ |
| 3890 | if (*cmd != '?') |
| 3891 | pos.col = MAXCOL; |
| 3892 | else |
| 3893 | pos.col = 0; |
| 3894 | if (searchit(curwin, curbuf, &pos, |
| 3895 | *cmd == '?' ? BACKWARD : FORWARD, |
| 3896 | (char_u *)"", 1L, |
Bram Moolenaar | a226a6d | 2006-02-26 23:59:20 +0000 | [diff] [blame] | 3897 | SEARCH_MSG + SEARCH_START, |
| 3898 | i, (linenr_T)0) != FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3899 | lnum = pos.lnum; |
| 3900 | else |
| 3901 | { |
| 3902 | cmd = NULL; |
| 3903 | goto error; |
| 3904 | } |
| 3905 | } |
| 3906 | ++cmd; |
| 3907 | break; |
| 3908 | |
| 3909 | default: |
| 3910 | if (VIM_ISDIGIT(*cmd)) /* absolute line number */ |
| 3911 | lnum = getdigits(&cmd); |
| 3912 | } |
| 3913 | |
| 3914 | for (;;) |
| 3915 | { |
| 3916 | cmd = skipwhite(cmd); |
| 3917 | if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd)) |
| 3918 | break; |
| 3919 | |
| 3920 | if (lnum == MAXLNUM) |
| 3921 | lnum = curwin->w_cursor.lnum; /* "+1" is same as ".+1" */ |
| 3922 | if (VIM_ISDIGIT(*cmd)) |
| 3923 | i = '+'; /* "number" is same as "+number" */ |
| 3924 | else |
| 3925 | i = *cmd++; |
| 3926 | if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */ |
| 3927 | n = 1; |
| 3928 | else |
| 3929 | n = getdigits(&cmd); |
| 3930 | if (i == '-') |
| 3931 | lnum -= n; |
| 3932 | else |
| 3933 | lnum += n; |
| 3934 | } |
| 3935 | } while (*cmd == '/' || *cmd == '?'); |
| 3936 | |
| 3937 | error: |
| 3938 | *ptr = cmd; |
| 3939 | return lnum; |
| 3940 | } |
| 3941 | |
| 3942 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3943 | * Get flags from an Ex command argument. |
| 3944 | */ |
| 3945 | static void |
| 3946 | get_flags(eap) |
| 3947 | exarg_T *eap; |
| 3948 | { |
| 3949 | while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL) |
| 3950 | { |
| 3951 | if (*eap->arg == 'l') |
| 3952 | eap->flags |= EXFLAG_LIST; |
| 3953 | else if (*eap->arg == 'p') |
| 3954 | eap->flags |= EXFLAG_PRINT; |
| 3955 | else |
| 3956 | eap->flags |= EXFLAG_NR; |
| 3957 | eap->arg = skipwhite(eap->arg + 1); |
| 3958 | } |
| 3959 | } |
| 3960 | |
| 3961 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3962 | * Function called for command which is Not Implemented. NI! |
| 3963 | */ |
| 3964 | void |
| 3965 | ex_ni(eap) |
| 3966 | exarg_T *eap; |
| 3967 | { |
| 3968 | if (!eap->skip) |
| 3969 | eap->errmsg = (char_u *)N_("E319: Sorry, the command is not available in this version"); |
| 3970 | } |
| 3971 | |
| 3972 | #if !defined(FEAT_PERL) || !defined(FEAT_PYTHON) || !defined(FEAT_TCL) \ |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 3973 | || !defined(FEAT_RUBY) || !defined(FEAT_MZSCHEME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3974 | /* |
| 3975 | * Function called for script command which is Not Implemented. NI! |
| 3976 | * Skips over ":perl <<EOF" constructs. |
| 3977 | */ |
| 3978 | static void |
| 3979 | ex_script_ni(eap) |
| 3980 | exarg_T *eap; |
| 3981 | { |
| 3982 | if (!eap->skip) |
| 3983 | ex_ni(eap); |
| 3984 | else |
| 3985 | vim_free(script_get(eap, eap->arg)); |
| 3986 | } |
| 3987 | #endif |
| 3988 | |
| 3989 | /* |
| 3990 | * Check range in Ex command for validity. |
| 3991 | * Return NULL when valid, error message when invalid. |
| 3992 | */ |
| 3993 | static char_u * |
| 3994 | invalid_range(eap) |
| 3995 | exarg_T *eap; |
| 3996 | { |
| 3997 | if ( eap->line1 < 0 |
| 3998 | || eap->line2 < 0 |
| 3999 | || eap->line1 > eap->line2 |
| 4000 | || ((eap->argt & RANGE) |
| 4001 | && !(eap->argt & NOTADR) |
| 4002 | && eap->line2 > curbuf->b_ml.ml_line_count |
| 4003 | #ifdef FEAT_DIFF |
| 4004 | + (eap->cmdidx == CMD_diffget) |
| 4005 | #endif |
| 4006 | )) |
| 4007 | return (char_u *)_(e_invrange); |
| 4008 | return NULL; |
| 4009 | } |
| 4010 | |
| 4011 | /* |
| 4012 | * Correct the range for zero line number, if required. |
| 4013 | */ |
| 4014 | static void |
| 4015 | correct_range(eap) |
| 4016 | exarg_T *eap; |
| 4017 | { |
| 4018 | if (!(eap->argt & ZEROR)) /* zero in range not allowed */ |
| 4019 | { |
| 4020 | if (eap->line1 == 0) |
| 4021 | eap->line1 = 1; |
| 4022 | if (eap->line2 == 0) |
| 4023 | eap->line2 = 1; |
| 4024 | } |
| 4025 | } |
| 4026 | |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4027 | #ifdef FEAT_QUICKFIX |
| 4028 | static char_u *skip_grep_pat __ARGS((exarg_T *eap)); |
| 4029 | |
| 4030 | /* |
| 4031 | * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the |
| 4032 | * pattern. Otherwise return eap->arg. |
| 4033 | */ |
| 4034 | static char_u * |
| 4035 | skip_grep_pat(eap) |
| 4036 | exarg_T *eap; |
| 4037 | { |
| 4038 | char_u *p = eap->arg; |
| 4039 | |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4040 | if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep |
| 4041 | || eap->cmdidx == CMD_vimgrepadd |
| 4042 | || eap->cmdidx == CMD_lvimgrepadd |
| 4043 | || grep_internal(eap->cmdidx))) |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4044 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 4045 | p = skip_vimgrep_pat(p, NULL, NULL); |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4046 | if (p == NULL) |
| 4047 | p = eap->arg; |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4048 | } |
| 4049 | return p; |
| 4050 | } |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4051 | |
| 4052 | /* |
| 4053 | * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option |
| 4054 | * in the command line, so that things like % get expanded. |
| 4055 | */ |
| 4056 | static char_u * |
| 4057 | replace_makeprg(eap, p, cmdlinep) |
| 4058 | exarg_T *eap; |
| 4059 | char_u *p; |
| 4060 | char_u **cmdlinep; |
| 4061 | { |
| 4062 | char_u *new_cmdline; |
| 4063 | char_u *program; |
| 4064 | char_u *pos; |
| 4065 | char_u *ptr; |
| 4066 | int len; |
| 4067 | int i; |
| 4068 | |
| 4069 | /* |
| 4070 | * Don't do it when ":vimgrep" is used for ":grep". |
| 4071 | */ |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4072 | if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake |
| 4073 | || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep |
| 4074 | || eap->cmdidx == CMD_grepadd |
| 4075 | || eap->cmdidx == CMD_lgrepadd) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4076 | && !grep_internal(eap->cmdidx)) |
| 4077 | { |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4078 | if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep |
| 4079 | || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd) |
Bram Moolenaar | d857f0e | 2005-06-21 22:37:39 +0000 | [diff] [blame] | 4080 | { |
| 4081 | if (*curbuf->b_p_gp == NUL) |
| 4082 | program = p_gp; |
| 4083 | else |
| 4084 | program = curbuf->b_p_gp; |
| 4085 | } |
| 4086 | else |
| 4087 | { |
| 4088 | if (*curbuf->b_p_mp == NUL) |
| 4089 | program = p_mp; |
| 4090 | else |
| 4091 | program = curbuf->b_p_mp; |
| 4092 | } |
| 4093 | |
| 4094 | p = skipwhite(p); |
| 4095 | |
| 4096 | if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) |
| 4097 | { |
| 4098 | /* replace $* by given arguments */ |
| 4099 | i = 1; |
| 4100 | while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL) |
| 4101 | ++i; |
| 4102 | len = (int)STRLEN(p); |
| 4103 | new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1)); |
| 4104 | if (new_cmdline == NULL) |
| 4105 | return NULL; /* out of memory */ |
| 4106 | ptr = new_cmdline; |
| 4107 | while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL) |
| 4108 | { |
| 4109 | i = (int)(pos - program); |
| 4110 | STRNCPY(ptr, program, i); |
| 4111 | STRCPY(ptr += i, p); |
| 4112 | ptr += len; |
| 4113 | program = pos + 2; |
| 4114 | } |
| 4115 | STRCPY(ptr, program); |
| 4116 | } |
| 4117 | else |
| 4118 | { |
| 4119 | new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2)); |
| 4120 | if (new_cmdline == NULL) |
| 4121 | return NULL; /* out of memory */ |
| 4122 | STRCPY(new_cmdline, program); |
| 4123 | STRCAT(new_cmdline, " "); |
| 4124 | STRCAT(new_cmdline, p); |
| 4125 | } |
| 4126 | msg_make(p); |
| 4127 | |
| 4128 | /* 'eap->cmd' is not set here, because it is not used at CMD_make */ |
| 4129 | vim_free(*cmdlinep); |
| 4130 | *cmdlinep = new_cmdline; |
| 4131 | p = new_cmdline; |
| 4132 | } |
| 4133 | return p; |
| 4134 | } |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4135 | #endif |
| 4136 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4137 | /* |
| 4138 | * Expand file name in Ex command argument. |
| 4139 | * Return FAIL for failure, OK otherwise. |
| 4140 | */ |
| 4141 | int |
| 4142 | expand_filename(eap, cmdlinep, errormsgp) |
| 4143 | exarg_T *eap; |
| 4144 | char_u **cmdlinep; |
| 4145 | char_u **errormsgp; |
| 4146 | { |
| 4147 | int has_wildcards; /* need to expand wildcards */ |
| 4148 | char_u *repl; |
| 4149 | int srclen; |
| 4150 | char_u *p; |
| 4151 | int n; |
| 4152 | |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4153 | #ifdef FEAT_QUICKFIX |
| 4154 | /* Skip a regexp pattern for ":vimgrep[add] pat file..." */ |
| 4155 | p = skip_grep_pat(eap); |
| 4156 | #else |
| 4157 | p = eap->arg; |
| 4158 | #endif |
| 4159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4160 | /* |
| 4161 | * Decide to expand wildcards *before* replacing '%', '#', etc. If |
| 4162 | * the file name contains a wildcard it should not cause expanding. |
| 4163 | * (it will be expanded anyway if there is a wildcard before replacing). |
| 4164 | */ |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4165 | has_wildcards = mch_has_wildcard(p); |
| 4166 | while (*p != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4167 | { |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4168 | #ifdef FEAT_EVAL |
| 4169 | /* Skip over `=expr`, wildcards in it are not expanded. */ |
| 4170 | if (p[0] == '`' && p[1] == '=') |
| 4171 | { |
| 4172 | p += 2; |
| 4173 | (void)skip_expr(&p); |
| 4174 | if (*p == '`') |
| 4175 | ++p; |
| 4176 | continue; |
| 4177 | } |
| 4178 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4179 | /* |
| 4180 | * Quick check if this cannot be the start of a special string. |
| 4181 | * Also removes backslash before '%', '#' and '<'. |
| 4182 | */ |
| 4183 | if (vim_strchr((char_u *)"%#<", *p) == NULL) |
| 4184 | { |
| 4185 | ++p; |
| 4186 | continue; |
| 4187 | } |
| 4188 | |
| 4189 | /* |
| 4190 | * Try to find a match at this position. |
| 4191 | */ |
| 4192 | repl = eval_vars(p, &srclen, &(eap->do_ecmd_lnum), errormsgp, eap->arg); |
| 4193 | if (*errormsgp != NULL) /* error detected */ |
| 4194 | return FAIL; |
| 4195 | if (repl == NULL) /* no match found */ |
| 4196 | { |
| 4197 | p += srclen; |
| 4198 | continue; |
| 4199 | } |
| 4200 | |
Bram Moolenaar | d8b0cf1 | 2004-12-12 11:33:30 +0000 | [diff] [blame] | 4201 | /* Wildcards won't be expanded below, the replacement is taken |
| 4202 | * literally. But do expand "~/file", "~user/file" and "$HOME/file". */ |
| 4203 | if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL) |
| 4204 | { |
| 4205 | char_u *l = repl; |
| 4206 | |
| 4207 | repl = expand_env_save(repl); |
| 4208 | vim_free(l); |
| 4209 | } |
| 4210 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | /* Need to escape white space et al. with a backslash. Don't do this |
| 4212 | * for shell commands (may have to use quotes instead). Don't do this |
| 4213 | * for non-unix systems when there is a single argument (spaces don't |
| 4214 | * separate arguments then). */ |
| 4215 | if (!eap->usefilter |
| 4216 | && eap->cmdidx != CMD_bang |
| 4217 | && eap->cmdidx != CMD_make |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4218 | && eap->cmdidx != CMD_lmake |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4219 | && eap->cmdidx != CMD_grep |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4220 | && eap->cmdidx != CMD_lgrep |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4221 | && eap->cmdidx != CMD_grepadd |
Bram Moolenaar | a37420f | 2006-02-04 22:37:47 +0000 | [diff] [blame] | 4222 | && eap->cmdidx != CMD_lgrepadd |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4223 | #ifndef UNIX |
| 4224 | && !(eap->argt & NOSPC) |
| 4225 | #endif |
| 4226 | ) |
| 4227 | { |
| 4228 | char_u *l; |
| 4229 | #ifdef BACKSLASH_IN_FILENAME |
| 4230 | /* Don't escape a backslash here, because rem_backslash() doesn't |
| 4231 | * remove it later. */ |
| 4232 | static char_u *nobslash = (char_u *)" \t\"|"; |
| 4233 | # define ESCAPE_CHARS nobslash |
| 4234 | #else |
| 4235 | # define ESCAPE_CHARS escape_chars |
| 4236 | #endif |
| 4237 | |
| 4238 | for (l = repl; *l; ++l) |
| 4239 | if (vim_strchr(ESCAPE_CHARS, *l) != NULL) |
| 4240 | { |
| 4241 | l = vim_strsave_escaped(repl, ESCAPE_CHARS); |
| 4242 | if (l != NULL) |
| 4243 | { |
| 4244 | vim_free(repl); |
| 4245 | repl = l; |
| 4246 | } |
| 4247 | break; |
| 4248 | } |
| 4249 | } |
| 4250 | |
| 4251 | /* For a shell command a '!' must be escaped. */ |
| 4252 | if ((eap->usefilter || eap->cmdidx == CMD_bang) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 4253 | && vim_strpbrk(repl, (char_u *)"!&;()<>") != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4254 | { |
| 4255 | char_u *l; |
| 4256 | |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 4257 | l = vim_strsave_escaped(repl, (char_u *)"!&;()<>"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4258 | if (l != NULL) |
| 4259 | { |
| 4260 | vim_free(repl); |
| 4261 | repl = l; |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 4262 | /* For a sh-like shell escape "!" another time. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4263 | if (strstr((char *)p_sh, "sh") != NULL) |
| 4264 | { |
| 4265 | l = vim_strsave_escaped(repl, (char_u *)"!"); |
| 4266 | if (l != NULL) |
| 4267 | { |
| 4268 | vim_free(repl); |
| 4269 | repl = l; |
| 4270 | } |
| 4271 | } |
| 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | p = repl_cmdline(eap, p, srclen, repl, cmdlinep); |
| 4276 | vim_free(repl); |
| 4277 | if (p == NULL) |
| 4278 | return FAIL; |
| 4279 | } |
| 4280 | |
| 4281 | /* |
| 4282 | * One file argument: Expand wildcards. |
| 4283 | * Don't do this with ":r !command" or ":w !command". |
| 4284 | */ |
| 4285 | if ((eap->argt & NOSPC) && !eap->usefilter) |
| 4286 | { |
| 4287 | /* |
| 4288 | * May do this twice: |
| 4289 | * 1. Replace environment variables. |
| 4290 | * 2. Replace any other wildcards, remove backslashes. |
| 4291 | */ |
| 4292 | for (n = 1; n <= 2; ++n) |
| 4293 | { |
| 4294 | if (n == 2) |
| 4295 | { |
| 4296 | #ifdef UNIX |
| 4297 | /* |
| 4298 | * Only for Unix we check for more than one file name. |
| 4299 | * For other systems spaces are considered to be part |
| 4300 | * of the file name. |
| 4301 | * Only check here if there is no wildcard, otherwise |
| 4302 | * ExpandOne() will check for errors. This allows |
| 4303 | * ":e `ls ve*.c`" on Unix. |
| 4304 | */ |
| 4305 | if (!has_wildcards) |
| 4306 | for (p = eap->arg; *p; ++p) |
| 4307 | { |
| 4308 | /* skip escaped characters */ |
| 4309 | if (p[1] && (*p == '\\' || *p == Ctrl_V)) |
| 4310 | ++p; |
| 4311 | else if (vim_iswhite(*p)) |
| 4312 | { |
| 4313 | *errormsgp = (char_u *)_("E172: Only one file name allowed"); |
| 4314 | return FAIL; |
| 4315 | } |
| 4316 | } |
| 4317 | #endif |
| 4318 | |
| 4319 | /* |
| 4320 | * Halve the number of backslashes (this is Vi compatible). |
| 4321 | * For Unix and OS/2, when wildcards are expanded, this is |
| 4322 | * done by ExpandOne() below. |
| 4323 | */ |
| 4324 | #if defined(UNIX) || defined(OS2) |
| 4325 | if (!has_wildcards) |
| 4326 | #endif |
| 4327 | backslash_halve(eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | if (has_wildcards) |
| 4331 | { |
| 4332 | if (n == 1) |
| 4333 | { |
| 4334 | /* |
| 4335 | * First loop: May expand environment variables. This |
| 4336 | * can be done much faster with expand_env() than with |
| 4337 | * something else (e.g., calling a shell). |
| 4338 | * After expanding environment variables, check again |
| 4339 | * if there are still wildcards present. |
| 4340 | */ |
| 4341 | if (vim_strchr(eap->arg, '$') != NULL |
| 4342 | || vim_strchr(eap->arg, '~') != NULL) |
| 4343 | { |
Bram Moolenaar | a1ba811 | 2005-06-28 23:23:32 +0000 | [diff] [blame] | 4344 | expand_env_esc(eap->arg, NameBuff, MAXPATHL, |
| 4345 | TRUE, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4346 | has_wildcards = mch_has_wildcard(NameBuff); |
| 4347 | p = NameBuff; |
| 4348 | } |
| 4349 | else |
| 4350 | p = NULL; |
| 4351 | } |
| 4352 | else /* n == 2 */ |
| 4353 | { |
| 4354 | expand_T xpc; |
| 4355 | |
| 4356 | ExpandInit(&xpc); |
| 4357 | xpc.xp_context = EXPAND_FILES; |
| 4358 | p = ExpandOne(&xpc, eap->arg, NULL, |
| 4359 | WILD_LIST_NOTFOUND|WILD_ADD_SLASH, |
| 4360 | WILD_EXPAND_FREE); |
| 4361 | ExpandCleanup(&xpc); |
| 4362 | if (p == NULL) |
| 4363 | return FAIL; |
| 4364 | } |
| 4365 | if (p != NULL) |
| 4366 | { |
| 4367 | (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg), |
| 4368 | p, cmdlinep); |
| 4369 | if (n == 2) /* p came from ExpandOne() */ |
| 4370 | vim_free(p); |
| 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | } |
| 4375 | return OK; |
| 4376 | } |
| 4377 | |
| 4378 | /* |
| 4379 | * Replace part of the command line, keeping eap->cmd, eap->arg and |
| 4380 | * eap->nextcmd correct. |
| 4381 | * "src" points to the part that is to be replaced, of length "srclen". |
| 4382 | * "repl" is the replacement string. |
| 4383 | * Returns a pointer to the character after the replaced string. |
| 4384 | * Returns NULL for failure. |
| 4385 | */ |
| 4386 | static char_u * |
| 4387 | repl_cmdline(eap, src, srclen, repl, cmdlinep) |
| 4388 | exarg_T *eap; |
| 4389 | char_u *src; |
| 4390 | int srclen; |
| 4391 | char_u *repl; |
| 4392 | char_u **cmdlinep; |
| 4393 | { |
| 4394 | int len; |
| 4395 | int i; |
| 4396 | char_u *new_cmdline; |
| 4397 | |
| 4398 | /* |
| 4399 | * The new command line is build in new_cmdline[]. |
| 4400 | * First allocate it. |
| 4401 | * Careful: a "+cmd" argument may have been NUL terminated. |
| 4402 | */ |
| 4403 | len = (int)STRLEN(repl); |
| 4404 | i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3; |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 4405 | if (eap->nextcmd != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4406 | i += (int)STRLEN(eap->nextcmd);/* add space for next command */ |
| 4407 | if ((new_cmdline = alloc((unsigned)i)) == NULL) |
| 4408 | return NULL; /* out of memory! */ |
| 4409 | |
| 4410 | /* |
| 4411 | * Copy the stuff before the expanded part. |
| 4412 | * Copy the expanded stuff. |
| 4413 | * Copy what came after the expanded part. |
| 4414 | * Copy the next commands, if there are any. |
| 4415 | */ |
| 4416 | i = (int)(src - *cmdlinep); /* length of part before match */ |
| 4417 | mch_memmove(new_cmdline, *cmdlinep, (size_t)i); |
Bram Moolenaar | a3ffd9c | 2005-07-21 21:03:15 +0000 | [diff] [blame] | 4418 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4419 | mch_memmove(new_cmdline + i, repl, (size_t)len); |
| 4420 | i += len; /* remember the end of the string */ |
| 4421 | STRCPY(new_cmdline + i, src + srclen); |
| 4422 | src = new_cmdline + i; /* remember where to continue */ |
| 4423 | |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 4424 | if (eap->nextcmd != NULL) /* append next command */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4425 | { |
| 4426 | i = (int)STRLEN(new_cmdline) + 1; |
| 4427 | STRCPY(new_cmdline + i, eap->nextcmd); |
| 4428 | eap->nextcmd = new_cmdline + i; |
| 4429 | } |
| 4430 | eap->cmd = new_cmdline + (eap->cmd - *cmdlinep); |
| 4431 | eap->arg = new_cmdline + (eap->arg - *cmdlinep); |
| 4432 | if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command) |
| 4433 | eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep); |
| 4434 | vim_free(*cmdlinep); |
| 4435 | *cmdlinep = new_cmdline; |
| 4436 | |
| 4437 | return src; |
| 4438 | } |
| 4439 | |
| 4440 | /* |
| 4441 | * Check for '|' to separate commands and '"' to start comments. |
| 4442 | */ |
| 4443 | void |
| 4444 | separate_nextcmd(eap) |
| 4445 | exarg_T *eap; |
| 4446 | { |
| 4447 | char_u *p; |
| 4448 | |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 4449 | #ifdef FEAT_QUICKFIX |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 4450 | p = skip_grep_pat(eap); |
| 4451 | #else |
| 4452 | p = eap->arg; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 4453 | #endif |
| 4454 | |
| 4455 | for ( ; *p; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4456 | { |
| 4457 | if (*p == Ctrl_V) |
| 4458 | { |
| 4459 | if (eap->argt & (USECTRLV | XFILE)) |
| 4460 | ++p; /* skip CTRL-V and next char */ |
| 4461 | else |
| 4462 | STRCPY(p, p + 1); /* remove CTRL-V and skip next char */ |
| 4463 | if (*p == NUL) /* stop at NUL after CTRL-V */ |
| 4464 | break; |
| 4465 | } |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4466 | |
| 4467 | #ifdef FEAT_EVAL |
| 4468 | /* Skip over `=expr` when wildcards are expanded. */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 4469 | else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE)) |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4470 | { |
| 4471 | p += 2; |
| 4472 | (void)skip_expr(&p); |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 4473 | } |
| 4474 | #endif |
| 4475 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4476 | /* Check for '"': start of comment or '|': next command */ |
| 4477 | /* :@" and :*" do not start a comment! |
| 4478 | * :redir @" doesn't either. */ |
| 4479 | else if ((*p == '"' && !(eap->argt & NOTRLCOM) |
| 4480 | && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star) |
| 4481 | || p != eap->arg) |
| 4482 | && (eap->cmdidx != CMD_redir |
| 4483 | || p != eap->arg + 1 || p[-1] != '@')) |
| 4484 | || *p == '|' || *p == '\n') |
| 4485 | { |
| 4486 | /* |
| 4487 | * We remove the '\' before the '|', unless USECTRLV is used |
| 4488 | * AND 'b' is present in 'cpoptions'. |
| 4489 | */ |
| 4490 | if ((vim_strchr(p_cpo, CPO_BAR) == NULL |
| 4491 | || !(eap->argt & USECTRLV)) && *(p - 1) == '\\') |
| 4492 | { |
| 4493 | mch_memmove(p - 1, p, STRLEN(p) + 1); /* remove the '\' */ |
| 4494 | --p; |
| 4495 | } |
| 4496 | else |
| 4497 | { |
| 4498 | eap->nextcmd = check_nextcmd(p); |
| 4499 | *p = NUL; |
| 4500 | break; |
| 4501 | } |
| 4502 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4503 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 4504 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4505 | if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */ |
| 4506 | del_trailing_spaces(eap->arg); |
| 4507 | } |
| 4508 | |
| 4509 | /* |
| 4510 | * get + command from ex argument |
| 4511 | */ |
| 4512 | static char_u * |
| 4513 | getargcmd(argp) |
| 4514 | char_u **argp; |
| 4515 | { |
| 4516 | char_u *arg = *argp; |
| 4517 | char_u *command = NULL; |
| 4518 | |
| 4519 | if (*arg == '+') /* +[command] */ |
| 4520 | { |
| 4521 | ++arg; |
| 4522 | if (vim_isspace(*arg)) |
| 4523 | command = dollar_command; |
| 4524 | else |
| 4525 | { |
| 4526 | command = arg; |
| 4527 | arg = skip_cmd_arg(command, TRUE); |
| 4528 | if (*arg != NUL) |
| 4529 | *arg++ = NUL; /* terminate command with NUL */ |
| 4530 | } |
| 4531 | |
| 4532 | arg = skipwhite(arg); /* skip over spaces */ |
| 4533 | *argp = arg; |
| 4534 | } |
| 4535 | return command; |
| 4536 | } |
| 4537 | |
| 4538 | /* |
| 4539 | * Find end of "+command" argument. Skip over "\ " and "\\". |
| 4540 | */ |
| 4541 | static char_u * |
| 4542 | skip_cmd_arg(p, rembs) |
| 4543 | char_u *p; |
| 4544 | int rembs; /* TRUE to halve the number of backslashes */ |
| 4545 | { |
| 4546 | while (*p && !vim_isspace(*p)) |
| 4547 | { |
| 4548 | if (*p == '\\' && p[1] != NUL) |
| 4549 | { |
| 4550 | if (rembs) |
| 4551 | mch_memmove(p, p + 1, STRLEN(p)); |
| 4552 | else |
| 4553 | ++p; |
| 4554 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4555 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4556 | } |
| 4557 | return p; |
| 4558 | } |
| 4559 | |
| 4560 | /* |
| 4561 | * Get "++opt=arg" argument. |
| 4562 | * Return FAIL or OK. |
| 4563 | */ |
| 4564 | static int |
| 4565 | getargopt(eap) |
| 4566 | exarg_T *eap; |
| 4567 | { |
| 4568 | char_u *arg = eap->arg + 2; |
| 4569 | int *pp = NULL; |
| 4570 | #ifdef FEAT_MBYTE |
| 4571 | char_u *p; |
| 4572 | #endif |
| 4573 | |
| 4574 | /* ":edit ++[no]bin[ary] file" */ |
| 4575 | if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0) |
| 4576 | { |
| 4577 | if (*arg == 'n') |
| 4578 | { |
| 4579 | arg += 2; |
| 4580 | eap->force_bin = FORCE_NOBIN; |
| 4581 | } |
| 4582 | else |
| 4583 | eap->force_bin = FORCE_BIN; |
| 4584 | if (!checkforcmd(&arg, "binary", 3)) |
| 4585 | return FAIL; |
| 4586 | eap->arg = skipwhite(arg); |
| 4587 | return OK; |
| 4588 | } |
| 4589 | |
| 4590 | if (STRNCMP(arg, "ff", 2) == 0) |
| 4591 | { |
| 4592 | arg += 2; |
| 4593 | pp = &eap->force_ff; |
| 4594 | } |
| 4595 | else if (STRNCMP(arg, "fileformat", 10) == 0) |
| 4596 | { |
| 4597 | arg += 10; |
| 4598 | pp = &eap->force_ff; |
| 4599 | } |
| 4600 | #ifdef FEAT_MBYTE |
| 4601 | else if (STRNCMP(arg, "enc", 3) == 0) |
| 4602 | { |
| 4603 | arg += 3; |
| 4604 | pp = &eap->force_enc; |
| 4605 | } |
| 4606 | else if (STRNCMP(arg, "encoding", 8) == 0) |
| 4607 | { |
| 4608 | arg += 8; |
| 4609 | pp = &eap->force_enc; |
| 4610 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4611 | else if (STRNCMP(arg, "bad", 3) == 0) |
| 4612 | { |
| 4613 | arg += 3; |
| 4614 | pp = &eap->bad_char; |
| 4615 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4616 | #endif |
| 4617 | |
| 4618 | if (pp == NULL || *arg != '=') |
| 4619 | return FAIL; |
| 4620 | |
| 4621 | ++arg; |
| 4622 | *pp = (int)(arg - eap->cmd); |
| 4623 | arg = skip_cmd_arg(arg, FALSE); |
| 4624 | eap->arg = skipwhite(arg); |
| 4625 | *arg = NUL; |
| 4626 | |
| 4627 | #ifdef FEAT_MBYTE |
| 4628 | if (pp == &eap->force_ff) |
| 4629 | { |
| 4630 | #endif |
| 4631 | if (check_ff_value(eap->cmd + eap->force_ff) == FAIL) |
| 4632 | return FAIL; |
| 4633 | #ifdef FEAT_MBYTE |
| 4634 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4635 | else if (pp == &eap->force_enc) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4636 | { |
| 4637 | /* Make 'fileencoding' lower case. */ |
| 4638 | for (p = eap->cmd + eap->force_enc; *p != NUL; ++p) |
| 4639 | *p = TOLOWER_ASC(*p); |
| 4640 | } |
Bram Moolenaar | b0bf858 | 2005-12-13 20:02:15 +0000 | [diff] [blame] | 4641 | else |
| 4642 | { |
| 4643 | /* Check ++bad= argument. Must be a single-byte character, "keep" or |
| 4644 | * "drop". */ |
| 4645 | p = eap->cmd + eap->bad_char; |
| 4646 | if (STRICMP(p, "keep") == 0) |
| 4647 | eap->bad_char = BAD_KEEP; |
| 4648 | else if (STRICMP(p, "drop") == 0) |
| 4649 | eap->bad_char = BAD_DROP; |
| 4650 | else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL) |
| 4651 | eap->bad_char = *p; |
| 4652 | else |
| 4653 | return FAIL; |
| 4654 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4655 | #endif |
| 4656 | |
| 4657 | return OK; |
| 4658 | } |
| 4659 | |
| 4660 | /* |
| 4661 | * ":abbreviate" and friends. |
| 4662 | */ |
| 4663 | static void |
| 4664 | ex_abbreviate(eap) |
| 4665 | exarg_T *eap; |
| 4666 | { |
| 4667 | do_exmap(eap, TRUE); /* almost the same as mapping */ |
| 4668 | } |
| 4669 | |
| 4670 | /* |
| 4671 | * ":map" and friends. |
| 4672 | */ |
| 4673 | static void |
| 4674 | ex_map(eap) |
| 4675 | exarg_T *eap; |
| 4676 | { |
| 4677 | /* |
| 4678 | * If we are sourcing .exrc or .vimrc in current directory we |
| 4679 | * print the mappings for security reasons. |
| 4680 | */ |
| 4681 | if (secure) |
| 4682 | { |
| 4683 | secure = 2; |
| 4684 | msg_outtrans(eap->cmd); |
| 4685 | msg_putchar('\n'); |
| 4686 | } |
| 4687 | do_exmap(eap, FALSE); |
| 4688 | } |
| 4689 | |
| 4690 | /* |
| 4691 | * ":unmap" and friends. |
| 4692 | */ |
| 4693 | static void |
| 4694 | ex_unmap(eap) |
| 4695 | exarg_T *eap; |
| 4696 | { |
| 4697 | do_exmap(eap, FALSE); |
| 4698 | } |
| 4699 | |
| 4700 | /* |
| 4701 | * ":mapclear" and friends. |
| 4702 | */ |
| 4703 | static void |
| 4704 | ex_mapclear(eap) |
| 4705 | exarg_T *eap; |
| 4706 | { |
| 4707 | map_clear(eap->cmd, eap->arg, eap->forceit, FALSE); |
| 4708 | } |
| 4709 | |
| 4710 | /* |
| 4711 | * ":abclear" and friends. |
| 4712 | */ |
| 4713 | static void |
| 4714 | ex_abclear(eap) |
| 4715 | exarg_T *eap; |
| 4716 | { |
| 4717 | map_clear(eap->cmd, eap->arg, TRUE, TRUE); |
| 4718 | } |
| 4719 | |
| 4720 | #ifdef FEAT_AUTOCMD |
| 4721 | static void |
| 4722 | ex_autocmd(eap) |
| 4723 | exarg_T *eap; |
| 4724 | { |
| 4725 | /* |
| 4726 | * Disallow auto commands from .exrc and .vimrc in current |
| 4727 | * directory for security reasons. |
| 4728 | */ |
| 4729 | if (secure) |
| 4730 | { |
| 4731 | secure = 2; |
| 4732 | eap->errmsg = e_curdir; |
| 4733 | } |
| 4734 | else if (eap->cmdidx == CMD_autocmd) |
| 4735 | do_autocmd(eap->arg, eap->forceit); |
| 4736 | else |
| 4737 | do_augroup(eap->arg, eap->forceit); |
| 4738 | } |
| 4739 | |
| 4740 | /* |
| 4741 | * ":doautocmd": Apply the automatic commands to the current buffer. |
| 4742 | */ |
| 4743 | static void |
| 4744 | ex_doautocmd(eap) |
| 4745 | exarg_T *eap; |
| 4746 | { |
| 4747 | (void)do_doautocmd(eap->arg, TRUE); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 4748 | do_modelines(0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4749 | } |
| 4750 | #endif |
| 4751 | |
| 4752 | #ifdef FEAT_LISTCMDS |
| 4753 | /* |
| 4754 | * :[N]bunload[!] [N] [bufname] unload buffer |
| 4755 | * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list |
| 4756 | * :[N]bwipeout[!] [N] [bufname] delete buffer really |
| 4757 | */ |
| 4758 | static void |
| 4759 | ex_bunload(eap) |
| 4760 | exarg_T *eap; |
| 4761 | { |
| 4762 | eap->errmsg = do_bufdel( |
| 4763 | eap->cmdidx == CMD_bdelete ? DOBUF_DEL |
| 4764 | : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE |
| 4765 | : DOBUF_UNLOAD, eap->arg, |
| 4766 | eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit); |
| 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * :[N]buffer [N] to buffer N |
| 4771 | * :[N]sbuffer [N] to buffer N |
| 4772 | */ |
| 4773 | static void |
| 4774 | ex_buffer(eap) |
| 4775 | exarg_T *eap; |
| 4776 | { |
| 4777 | if (*eap->arg) |
| 4778 | eap->errmsg = e_trailing; |
| 4779 | else |
| 4780 | { |
| 4781 | if (eap->addr_count == 0) /* default is current buffer */ |
| 4782 | goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0); |
| 4783 | else |
| 4784 | goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2); |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | /* |
| 4789 | * :[N]bmodified [N] to next mod. buffer |
| 4790 | * :[N]sbmodified [N] to next mod. buffer |
| 4791 | */ |
| 4792 | static void |
| 4793 | ex_bmodified(eap) |
| 4794 | exarg_T *eap; |
| 4795 | { |
| 4796 | goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2); |
| 4797 | } |
| 4798 | |
| 4799 | /* |
| 4800 | * :[N]bnext [N] to next buffer |
| 4801 | * :[N]sbnext [N] split and to next buffer |
| 4802 | */ |
| 4803 | static void |
| 4804 | ex_bnext(eap) |
| 4805 | exarg_T *eap; |
| 4806 | { |
| 4807 | goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2); |
| 4808 | } |
| 4809 | |
| 4810 | /* |
| 4811 | * :[N]bNext [N] to previous buffer |
| 4812 | * :[N]bprevious [N] to previous buffer |
| 4813 | * :[N]sbNext [N] split and to previous buffer |
| 4814 | * :[N]sbprevious [N] split and to previous buffer |
| 4815 | */ |
| 4816 | static void |
| 4817 | ex_bprevious(eap) |
| 4818 | exarg_T *eap; |
| 4819 | { |
| 4820 | goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2); |
| 4821 | } |
| 4822 | |
| 4823 | /* |
| 4824 | * :brewind to first buffer |
| 4825 | * :bfirst to first buffer |
| 4826 | * :sbrewind split and to first buffer |
| 4827 | * :sbfirst split and to first buffer |
| 4828 | */ |
| 4829 | static void |
| 4830 | ex_brewind(eap) |
| 4831 | exarg_T *eap; |
| 4832 | { |
| 4833 | goto_buffer(eap, DOBUF_FIRST, FORWARD, 0); |
| 4834 | } |
| 4835 | |
| 4836 | /* |
| 4837 | * :blast to last buffer |
| 4838 | * :sblast split and to last buffer |
| 4839 | */ |
| 4840 | static void |
| 4841 | ex_blast(eap) |
| 4842 | exarg_T *eap; |
| 4843 | { |
| 4844 | goto_buffer(eap, DOBUF_LAST, BACKWARD, 0); |
| 4845 | } |
| 4846 | #endif |
| 4847 | |
| 4848 | int |
| 4849 | ends_excmd(c) |
| 4850 | int c; |
| 4851 | { |
| 4852 | return (c == NUL || c == '|' || c == '"' || c == '\n'); |
| 4853 | } |
| 4854 | |
| 4855 | #if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \ |
| 4856 | || defined(PROTO) |
| 4857 | /* |
| 4858 | * Return the next command, after the first '|' or '\n'. |
| 4859 | * Return NULL if not found. |
| 4860 | */ |
| 4861 | char_u * |
| 4862 | find_nextcmd(p) |
| 4863 | char_u *p; |
| 4864 | { |
| 4865 | while (*p != '|' && *p != '\n') |
| 4866 | { |
| 4867 | if (*p == NUL) |
| 4868 | return NULL; |
| 4869 | ++p; |
| 4870 | } |
| 4871 | return (p + 1); |
| 4872 | } |
| 4873 | #endif |
| 4874 | |
| 4875 | /* |
| 4876 | * Check if *p is a separator between Ex commands. |
| 4877 | * Return NULL if it isn't, (p + 1) if it is. |
| 4878 | */ |
| 4879 | char_u * |
| 4880 | check_nextcmd(p) |
| 4881 | char_u *p; |
| 4882 | { |
| 4883 | p = skipwhite(p); |
| 4884 | if (*p == '|' || *p == '\n') |
| 4885 | return (p + 1); |
| 4886 | else |
| 4887 | return NULL; |
| 4888 | } |
| 4889 | |
| 4890 | /* |
| 4891 | * - if there are more files to edit |
| 4892 | * - and this is the last window |
| 4893 | * - and forceit not used |
| 4894 | * - and not repeated twice on a row |
| 4895 | * return FAIL and give error message if 'message' TRUE |
| 4896 | * return OK otherwise |
| 4897 | */ |
| 4898 | static int |
| 4899 | check_more(message, forceit) |
| 4900 | int message; /* when FALSE check only, no messages */ |
| 4901 | int forceit; |
| 4902 | { |
| 4903 | int n = ARGCOUNT - curwin->w_arg_idx - 1; |
| 4904 | |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 4905 | if (!forceit && only_one_window() |
| 4906 | && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4907 | { |
| 4908 | if (message) |
| 4909 | { |
| 4910 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
| 4911 | if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL) |
| 4912 | { |
| 4913 | char_u buff[IOSIZE]; |
| 4914 | |
| 4915 | if (n == 1) |
| 4916 | STRCPY(buff, _("1 more file to edit. Quit anyway?")); |
| 4917 | else |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 4918 | vim_snprintf((char *)buff, IOSIZE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | _("%d more files to edit. Quit anyway?"), n); |
| 4920 | if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES) |
| 4921 | return OK; |
| 4922 | return FAIL; |
| 4923 | } |
| 4924 | #endif |
| 4925 | if (n == 1) |
| 4926 | EMSG(_("E173: 1 more file to edit")); |
| 4927 | else |
| 4928 | EMSGN(_("E173: %ld more files to edit"), n); |
| 4929 | quitmore = 2; /* next try to quit is allowed */ |
| 4930 | } |
| 4931 | return FAIL; |
| 4932 | } |
| 4933 | return OK; |
| 4934 | } |
| 4935 | |
| 4936 | #ifdef FEAT_CMDL_COMPL |
| 4937 | /* |
| 4938 | * Function given to ExpandGeneric() to obtain the list of command names. |
| 4939 | */ |
| 4940 | /*ARGSUSED*/ |
| 4941 | char_u * |
| 4942 | get_command_name(xp, idx) |
| 4943 | expand_T *xp; |
| 4944 | int idx; |
| 4945 | { |
| 4946 | if (idx >= (int)CMD_SIZE) |
| 4947 | # ifdef FEAT_USR_CMDS |
| 4948 | return get_user_command_name(idx); |
| 4949 | # else |
| 4950 | return NULL; |
| 4951 | # endif |
| 4952 | return cmdnames[idx].cmd_name; |
| 4953 | } |
| 4954 | #endif |
| 4955 | |
| 4956 | #if defined(FEAT_USR_CMDS) || defined(PROTO) |
| 4957 | static int uc_add_command __ARGS((char_u *name, size_t name_len, char_u *rep, long argt, long def, int flags, int compl, char_u *compl_arg, int force)); |
| 4958 | static void uc_list __ARGS((char_u *name, size_t name_len)); |
| 4959 | static int uc_scan_attr __ARGS((char_u *attr, size_t len, long *argt, long *def, int *flags, int *compl, char_u **compl_arg)); |
| 4960 | static char_u *uc_split_args __ARGS((char_u *arg, size_t *lenp)); |
| 4961 | static size_t uc_check_code __ARGS((char_u *code, size_t len, char_u *buf, ucmd_T *cmd, exarg_T *eap, char_u **split_buf, size_t *split_len)); |
| 4962 | |
| 4963 | static int |
| 4964 | uc_add_command(name, name_len, rep, argt, def, flags, compl, compl_arg, force) |
| 4965 | char_u *name; |
| 4966 | size_t name_len; |
| 4967 | char_u *rep; |
| 4968 | long argt; |
| 4969 | long def; |
| 4970 | int flags; |
| 4971 | int compl; |
| 4972 | char_u *compl_arg; |
| 4973 | int force; |
| 4974 | { |
| 4975 | ucmd_T *cmd = NULL; |
| 4976 | char_u *p; |
| 4977 | int i; |
| 4978 | int cmp = 1; |
| 4979 | char_u *rep_buf = NULL; |
| 4980 | garray_T *gap; |
| 4981 | |
| 4982 | replace_termcodes(rep, &rep_buf, FALSE, FALSE); |
| 4983 | if (rep_buf == NULL) |
| 4984 | { |
| 4985 | /* Can't replace termcodes - try using the string as is */ |
| 4986 | rep_buf = vim_strsave(rep); |
| 4987 | |
| 4988 | /* Give up if out of memory */ |
| 4989 | if (rep_buf == NULL) |
| 4990 | return FAIL; |
| 4991 | } |
| 4992 | |
| 4993 | /* get address of growarray: global or in curbuf */ |
| 4994 | if (flags & UC_BUFFER) |
| 4995 | { |
| 4996 | gap = &curbuf->b_ucmds; |
| 4997 | if (gap->ga_itemsize == 0) |
| 4998 | ga_init2(gap, (int)sizeof(ucmd_T), 4); |
| 4999 | } |
| 5000 | else |
| 5001 | gap = &ucmds; |
| 5002 | |
| 5003 | /* Search for the command in the already defined commands. */ |
| 5004 | for (i = 0; i < gap->ga_len; ++i) |
| 5005 | { |
| 5006 | size_t len; |
| 5007 | |
| 5008 | cmd = USER_CMD_GA(gap, i); |
| 5009 | len = STRLEN(cmd->uc_name); |
| 5010 | cmp = STRNCMP(name, cmd->uc_name, name_len); |
| 5011 | if (cmp == 0) |
| 5012 | { |
| 5013 | if (name_len < len) |
| 5014 | cmp = -1; |
| 5015 | else if (name_len > len) |
| 5016 | cmp = 1; |
| 5017 | } |
| 5018 | |
| 5019 | if (cmp == 0) |
| 5020 | { |
| 5021 | if (!force) |
| 5022 | { |
| 5023 | EMSG(_("E174: Command already exists: add ! to replace it")); |
| 5024 | goto fail; |
| 5025 | } |
| 5026 | |
| 5027 | vim_free(cmd->uc_rep); |
| 5028 | cmd->uc_rep = 0; |
| 5029 | break; |
| 5030 | } |
| 5031 | |
| 5032 | /* Stop as soon as we pass the name to add */ |
| 5033 | if (cmp < 0) |
| 5034 | break; |
| 5035 | } |
| 5036 | |
| 5037 | /* Extend the array unless we're replacing an existing command */ |
| 5038 | if (cmp != 0) |
| 5039 | { |
| 5040 | if (ga_grow(gap, 1) != OK) |
| 5041 | goto fail; |
| 5042 | if ((p = vim_strnsave(name, (int)name_len)) == NULL) |
| 5043 | goto fail; |
| 5044 | |
| 5045 | cmd = USER_CMD_GA(gap, i); |
| 5046 | mch_memmove(cmd + 1, cmd, (gap->ga_len - i) * sizeof(ucmd_T)); |
| 5047 | |
| 5048 | ++gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5049 | |
| 5050 | cmd->uc_name = p; |
| 5051 | } |
| 5052 | |
| 5053 | cmd->uc_rep = rep_buf; |
| 5054 | cmd->uc_argt = argt; |
| 5055 | cmd->uc_def = def; |
| 5056 | cmd->uc_compl = compl; |
| 5057 | #ifdef FEAT_EVAL |
| 5058 | cmd->uc_scriptID = current_SID; |
| 5059 | # ifdef FEAT_CMDL_COMPL |
| 5060 | cmd->uc_compl_arg = compl_arg; |
| 5061 | # endif |
| 5062 | #endif |
| 5063 | |
| 5064 | return OK; |
| 5065 | |
| 5066 | fail: |
| 5067 | vim_free(rep_buf); |
| 5068 | #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 5069 | vim_free(compl_arg); |
| 5070 | #endif |
| 5071 | return FAIL; |
| 5072 | } |
| 5073 | |
| 5074 | /* |
| 5075 | * List of names for completion for ":command" with the EXPAND_ flag. |
| 5076 | * Must be alphabetical for completion. |
| 5077 | */ |
| 5078 | static struct |
| 5079 | { |
| 5080 | int expand; |
| 5081 | char *name; |
| 5082 | } command_complete[] = |
| 5083 | { |
| 5084 | {EXPAND_AUGROUP, "augroup"}, |
| 5085 | {EXPAND_BUFFERS, "buffer"}, |
| 5086 | {EXPAND_COMMANDS, "command"}, |
| 5087 | #if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 5088 | {EXPAND_USER_DEFINED, "custom"}, |
Bram Moolenaar | a466c99 | 2005-07-09 21:03:22 +0000 | [diff] [blame] | 5089 | {EXPAND_USER_LIST, "customlist"}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5090 | #endif |
| 5091 | {EXPAND_DIRECTORIES, "dir"}, |
| 5092 | {EXPAND_ENV_VARS, "environment"}, |
| 5093 | {EXPAND_EVENTS, "event"}, |
| 5094 | {EXPAND_EXPRESSION, "expression"}, |
| 5095 | {EXPAND_FILES, "file"}, |
| 5096 | {EXPAND_FUNCTIONS, "function"}, |
| 5097 | {EXPAND_HELP, "help"}, |
| 5098 | {EXPAND_HIGHLIGHT, "highlight"}, |
| 5099 | {EXPAND_MAPPINGS, "mapping"}, |
| 5100 | {EXPAND_MENUS, "menu"}, |
| 5101 | {EXPAND_SETTINGS, "option"}, |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 5102 | {EXPAND_SHELLCMD, "shellcmd"}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5103 | {EXPAND_TAGS, "tag"}, |
| 5104 | {EXPAND_TAGS_LISTFILES, "tag_listfiles"}, |
| 5105 | {EXPAND_USER_VARS, "var"}, |
| 5106 | {0, NULL} |
| 5107 | }; |
| 5108 | |
| 5109 | static void |
| 5110 | uc_list(name, name_len) |
| 5111 | char_u *name; |
| 5112 | size_t name_len; |
| 5113 | { |
| 5114 | int i, j; |
| 5115 | int found = FALSE; |
| 5116 | ucmd_T *cmd; |
| 5117 | int len; |
| 5118 | long a; |
| 5119 | garray_T *gap; |
| 5120 | |
| 5121 | gap = &curbuf->b_ucmds; |
| 5122 | for (;;) |
| 5123 | { |
| 5124 | for (i = 0; i < gap->ga_len; ++i) |
| 5125 | { |
| 5126 | cmd = USER_CMD_GA(gap, i); |
| 5127 | a = cmd->uc_argt; |
| 5128 | |
| 5129 | /* Skip commands which don't match the requested prefix */ |
| 5130 | if (STRNCMP(name, cmd->uc_name, name_len) != 0) |
| 5131 | continue; |
| 5132 | |
| 5133 | /* Put out the title first time */ |
| 5134 | if (!found) |
| 5135 | MSG_PUTS_TITLE(_("\n Name Args Range Complete Definition")); |
| 5136 | found = TRUE; |
| 5137 | msg_putchar('\n'); |
| 5138 | if (got_int) |
| 5139 | break; |
| 5140 | |
| 5141 | /* Special cases */ |
| 5142 | msg_putchar(a & BANG ? '!' : ' '); |
| 5143 | msg_putchar(a & REGSTR ? '"' : ' '); |
| 5144 | msg_putchar(gap != &ucmds ? 'b' : ' '); |
| 5145 | msg_putchar(' '); |
| 5146 | |
| 5147 | msg_outtrans_attr(cmd->uc_name, hl_attr(HLF_D)); |
| 5148 | len = (int)STRLEN(cmd->uc_name) + 4; |
| 5149 | |
| 5150 | do { |
| 5151 | msg_putchar(' '); |
| 5152 | ++len; |
| 5153 | } while (len < 16); |
| 5154 | |
| 5155 | len = 0; |
| 5156 | |
| 5157 | /* Arguments */ |
| 5158 | switch ((int)(a & (EXTRA|NOSPC|NEEDARG))) |
| 5159 | { |
| 5160 | case 0: IObuff[len++] = '0'; break; |
| 5161 | case (EXTRA): IObuff[len++] = '*'; break; |
| 5162 | case (EXTRA|NOSPC): IObuff[len++] = '?'; break; |
| 5163 | case (EXTRA|NEEDARG): IObuff[len++] = '+'; break; |
| 5164 | case (EXTRA|NOSPC|NEEDARG): IObuff[len++] = '1'; break; |
| 5165 | } |
| 5166 | |
| 5167 | do { |
| 5168 | IObuff[len++] = ' '; |
| 5169 | } while (len < 5); |
| 5170 | |
| 5171 | /* Range */ |
| 5172 | if (a & (RANGE|COUNT)) |
| 5173 | { |
| 5174 | if (a & COUNT) |
| 5175 | { |
| 5176 | /* -count=N */ |
| 5177 | sprintf((char *)IObuff + len, "%ldc", cmd->uc_def); |
| 5178 | len += (int)STRLEN(IObuff + len); |
| 5179 | } |
| 5180 | else if (a & DFLALL) |
| 5181 | IObuff[len++] = '%'; |
| 5182 | else if (cmd->uc_def >= 0) |
| 5183 | { |
| 5184 | /* -range=N */ |
| 5185 | sprintf((char *)IObuff + len, "%ld", cmd->uc_def); |
| 5186 | len += (int)STRLEN(IObuff + len); |
| 5187 | } |
| 5188 | else |
| 5189 | IObuff[len++] = '.'; |
| 5190 | } |
| 5191 | |
| 5192 | do { |
| 5193 | IObuff[len++] = ' '; |
| 5194 | } while (len < 11); |
| 5195 | |
| 5196 | /* Completion */ |
| 5197 | for (j = 0; command_complete[j].expand != 0; ++j) |
| 5198 | if (command_complete[j].expand == cmd->uc_compl) |
| 5199 | { |
| 5200 | STRCPY(IObuff + len, command_complete[j].name); |
| 5201 | len += (int)STRLEN(IObuff + len); |
| 5202 | break; |
| 5203 | } |
| 5204 | |
| 5205 | do { |
| 5206 | IObuff[len++] = ' '; |
| 5207 | } while (len < 21); |
| 5208 | |
| 5209 | IObuff[len] = '\0'; |
| 5210 | msg_outtrans(IObuff); |
| 5211 | |
| 5212 | msg_outtrans_special(cmd->uc_rep, FALSE); |
Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 5213 | #ifdef FEAT_EVAL |
| 5214 | if (p_verbose > 0) |
| 5215 | last_set_msg(cmd->uc_scriptID); |
| 5216 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5217 | out_flush(); |
| 5218 | ui_breakcheck(); |
| 5219 | if (got_int) |
| 5220 | break; |
| 5221 | } |
| 5222 | if (gap == &ucmds || i < gap->ga_len) |
| 5223 | break; |
| 5224 | gap = &ucmds; |
| 5225 | } |
| 5226 | |
| 5227 | if (!found) |
| 5228 | MSG(_("No user-defined commands found")); |
| 5229 | } |
| 5230 | |
| 5231 | static char_u * |
| 5232 | uc_fun_cmd() |
| 5233 | { |
| 5234 | static char_u fcmd[] = {0x84, 0xaf, 0x60, 0xb9, 0xaf, 0xb5, 0x60, 0xa4, |
| 5235 | 0xa5, 0xad, 0xa1, 0xae, 0xa4, 0x60, 0xa1, 0x60, |
| 5236 | 0xb3, 0xa8, 0xb2, 0xb5, 0xa2, 0xa2, 0xa5, 0xb2, |
| 5237 | 0xb9, 0x7f, 0}; |
| 5238 | int i; |
| 5239 | |
| 5240 | for (i = 0; fcmd[i]; ++i) |
| 5241 | IObuff[i] = fcmd[i] - 0x40; |
| 5242 | IObuff[i] = 0; |
| 5243 | return IObuff; |
| 5244 | } |
| 5245 | |
| 5246 | static int |
| 5247 | uc_scan_attr(attr, len, argt, def, flags, compl, compl_arg) |
| 5248 | char_u *attr; |
| 5249 | size_t len; |
| 5250 | long *argt; |
| 5251 | long *def; |
| 5252 | int *flags; |
| 5253 | int *compl; |
| 5254 | char_u **compl_arg; |
| 5255 | { |
| 5256 | char_u *p; |
| 5257 | |
| 5258 | if (len == 0) |
| 5259 | { |
| 5260 | EMSG(_("E175: No attribute specified")); |
| 5261 | return FAIL; |
| 5262 | } |
| 5263 | |
| 5264 | /* First, try the simple attributes (no arguments) */ |
| 5265 | if (STRNICMP(attr, "bang", len) == 0) |
| 5266 | *argt |= BANG; |
| 5267 | else if (STRNICMP(attr, "buffer", len) == 0) |
| 5268 | *flags |= UC_BUFFER; |
| 5269 | else if (STRNICMP(attr, "register", len) == 0) |
| 5270 | *argt |= REGSTR; |
| 5271 | else if (STRNICMP(attr, "bar", len) == 0) |
| 5272 | *argt |= TRLBAR; |
| 5273 | else |
| 5274 | { |
| 5275 | int i; |
| 5276 | char_u *val = NULL; |
| 5277 | size_t vallen = 0; |
| 5278 | size_t attrlen = len; |
| 5279 | |
| 5280 | /* Look for the attribute name - which is the part before any '=' */ |
| 5281 | for (i = 0; i < (int)len; ++i) |
| 5282 | { |
| 5283 | if (attr[i] == '=') |
| 5284 | { |
| 5285 | val = &attr[i + 1]; |
| 5286 | vallen = len - i - 1; |
| 5287 | attrlen = i; |
| 5288 | break; |
| 5289 | } |
| 5290 | } |
| 5291 | |
| 5292 | if (STRNICMP(attr, "nargs", attrlen) == 0) |
| 5293 | { |
| 5294 | if (vallen == 1) |
| 5295 | { |
| 5296 | if (*val == '0') |
| 5297 | /* Do nothing - this is the default */; |
| 5298 | else if (*val == '1') |
| 5299 | *argt |= (EXTRA | NOSPC | NEEDARG); |
| 5300 | else if (*val == '*') |
| 5301 | *argt |= EXTRA; |
| 5302 | else if (*val == '?') |
| 5303 | *argt |= (EXTRA | NOSPC); |
| 5304 | else if (*val == '+') |
| 5305 | *argt |= (EXTRA | NEEDARG); |
| 5306 | else |
| 5307 | goto wrong_nargs; |
| 5308 | } |
| 5309 | else |
| 5310 | { |
| 5311 | wrong_nargs: |
| 5312 | EMSG(_("E176: Invalid number of arguments")); |
| 5313 | return FAIL; |
| 5314 | } |
| 5315 | } |
| 5316 | else if (STRNICMP(attr, "range", attrlen) == 0) |
| 5317 | { |
| 5318 | *argt |= RANGE; |
| 5319 | if (vallen == 1 && *val == '%') |
| 5320 | *argt |= DFLALL; |
| 5321 | else if (val != NULL) |
| 5322 | { |
| 5323 | p = val; |
| 5324 | if (*def >= 0) |
| 5325 | { |
| 5326 | two_count: |
| 5327 | EMSG(_("E177: Count cannot be specified twice")); |
| 5328 | return FAIL; |
| 5329 | } |
| 5330 | |
| 5331 | *def = getdigits(&p); |
| 5332 | *argt |= (ZEROR | NOTADR); |
| 5333 | |
| 5334 | if (p != val + vallen || vallen == 0) |
| 5335 | { |
| 5336 | invalid_count: |
| 5337 | EMSG(_("E178: Invalid default value for count")); |
| 5338 | return FAIL; |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | else if (STRNICMP(attr, "count", attrlen) == 0) |
| 5343 | { |
Bram Moolenaar | 32e7b2d | 2005-02-27 22:36:47 +0000 | [diff] [blame] | 5344 | *argt |= (COUNT | ZEROR | RANGE | NOTADR); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5345 | |
| 5346 | if (val != NULL) |
| 5347 | { |
| 5348 | p = val; |
| 5349 | if (*def >= 0) |
| 5350 | goto two_count; |
| 5351 | |
| 5352 | *def = getdigits(&p); |
| 5353 | |
| 5354 | if (p != val + vallen) |
| 5355 | goto invalid_count; |
| 5356 | } |
| 5357 | |
| 5358 | if (*def < 0) |
| 5359 | *def = 0; |
| 5360 | } |
| 5361 | else if (STRNICMP(attr, "complete", attrlen) == 0) |
| 5362 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5363 | if (val == NULL) |
| 5364 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5365 | EMSG(_("E179: argument required for -complete")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5366 | return FAIL; |
| 5367 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5368 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5369 | if (parse_compl_arg(val, (int)vallen, compl, argt, compl_arg) |
| 5370 | == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5371 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5372 | } |
| 5373 | else |
| 5374 | { |
| 5375 | char_u ch = attr[len]; |
| 5376 | attr[len] = '\0'; |
| 5377 | EMSG2(_("E181: Invalid attribute: %s"), attr); |
| 5378 | attr[len] = ch; |
| 5379 | return FAIL; |
| 5380 | } |
| 5381 | } |
| 5382 | |
| 5383 | return OK; |
| 5384 | } |
| 5385 | |
| 5386 | static void |
| 5387 | ex_command(eap) |
| 5388 | exarg_T *eap; |
| 5389 | { |
| 5390 | char_u *name; |
| 5391 | char_u *end; |
| 5392 | char_u *p; |
| 5393 | long argt = 0; |
| 5394 | long def = -1; |
| 5395 | int flags = 0; |
| 5396 | int compl = EXPAND_NOTHING; |
| 5397 | char_u *compl_arg = NULL; |
| 5398 | int has_attr = (eap->arg[0] == '-'); |
| 5399 | |
| 5400 | p = eap->arg; |
| 5401 | |
| 5402 | /* Check for attributes */ |
| 5403 | while (*p == '-') |
| 5404 | { |
| 5405 | ++p; |
| 5406 | end = skiptowhite(p); |
| 5407 | if (uc_scan_attr(p, end - p, &argt, &def, &flags, &compl, &compl_arg) |
| 5408 | == FAIL) |
| 5409 | return; |
| 5410 | p = skipwhite(end); |
| 5411 | } |
| 5412 | |
| 5413 | /* Get the name (if any) and skip to the following argument */ |
| 5414 | name = p; |
| 5415 | if (ASCII_ISALPHA(*p)) |
| 5416 | while (ASCII_ISALNUM(*p)) |
| 5417 | ++p; |
| 5418 | if (!ends_excmd(*p) && !vim_iswhite(*p)) |
| 5419 | { |
| 5420 | EMSG(_("E182: Invalid command name")); |
| 5421 | return; |
| 5422 | } |
| 5423 | end = p; |
| 5424 | |
| 5425 | /* If there is nothing after the name, and no attributes were specified, |
| 5426 | * we are listing commands |
| 5427 | */ |
| 5428 | p = skipwhite(end); |
| 5429 | if (!has_attr && ends_excmd(*p)) |
| 5430 | { |
| 5431 | uc_list(name, end - name); |
| 5432 | } |
| 5433 | else if (!ASCII_ISUPPER(*name)) |
| 5434 | { |
| 5435 | EMSG(_("E183: User defined commands must start with an uppercase letter")); |
| 5436 | return; |
| 5437 | } |
| 5438 | else |
| 5439 | uc_add_command(name, end - name, p, argt, def, flags, compl, compl_arg, |
| 5440 | eap->forceit); |
| 5441 | } |
| 5442 | |
| 5443 | /* |
| 5444 | * ":comclear" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5445 | * Clear all user commands, global and for current buffer. |
| 5446 | */ |
Bram Moolenaar | 0a5fe21 | 2005-06-24 23:01:23 +0000 | [diff] [blame] | 5447 | /*ARGSUSED*/ |
| 5448 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5449 | ex_comclear(eap) |
| 5450 | exarg_T *eap; |
| 5451 | { |
| 5452 | uc_clear(&ucmds); |
| 5453 | uc_clear(&curbuf->b_ucmds); |
| 5454 | } |
| 5455 | |
| 5456 | /* |
| 5457 | * Clear all user commands for "gap". |
| 5458 | */ |
| 5459 | void |
| 5460 | uc_clear(gap) |
| 5461 | garray_T *gap; |
| 5462 | { |
| 5463 | int i; |
| 5464 | ucmd_T *cmd; |
| 5465 | |
| 5466 | for (i = 0; i < gap->ga_len; ++i) |
| 5467 | { |
| 5468 | cmd = USER_CMD_GA(gap, i); |
| 5469 | vim_free(cmd->uc_name); |
| 5470 | vim_free(cmd->uc_rep); |
| 5471 | # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 5472 | vim_free(cmd->uc_compl_arg); |
| 5473 | # endif |
| 5474 | } |
| 5475 | ga_clear(gap); |
| 5476 | } |
| 5477 | |
| 5478 | static void |
| 5479 | ex_delcommand(eap) |
| 5480 | exarg_T *eap; |
| 5481 | { |
| 5482 | int i = 0; |
| 5483 | ucmd_T *cmd = NULL; |
| 5484 | int cmp = -1; |
| 5485 | garray_T *gap; |
| 5486 | |
| 5487 | gap = &curbuf->b_ucmds; |
| 5488 | for (;;) |
| 5489 | { |
| 5490 | for (i = 0; i < gap->ga_len; ++i) |
| 5491 | { |
| 5492 | cmd = USER_CMD_GA(gap, i); |
| 5493 | cmp = STRCMP(eap->arg, cmd->uc_name); |
| 5494 | if (cmp <= 0) |
| 5495 | break; |
| 5496 | } |
| 5497 | if (gap == &ucmds || cmp == 0) |
| 5498 | break; |
| 5499 | gap = &ucmds; |
| 5500 | } |
| 5501 | |
| 5502 | if (cmp != 0) |
| 5503 | { |
| 5504 | EMSG2(_("E184: No such user-defined command: %s"), eap->arg); |
| 5505 | return; |
| 5506 | } |
| 5507 | |
| 5508 | vim_free(cmd->uc_name); |
| 5509 | vim_free(cmd->uc_rep); |
| 5510 | # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 5511 | vim_free(cmd->uc_compl_arg); |
| 5512 | # endif |
| 5513 | |
| 5514 | --gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5515 | |
| 5516 | if (i < gap->ga_len) |
| 5517 | mch_memmove(cmd, cmd + 1, (gap->ga_len - i) * sizeof(ucmd_T)); |
| 5518 | } |
| 5519 | |
| 5520 | static char_u * |
| 5521 | uc_split_args(arg, lenp) |
| 5522 | char_u *arg; |
| 5523 | size_t *lenp; |
| 5524 | { |
| 5525 | char_u *buf; |
| 5526 | char_u *p; |
| 5527 | char_u *q; |
| 5528 | int len; |
| 5529 | |
| 5530 | /* Precalculate length */ |
| 5531 | p = arg; |
| 5532 | len = 2; /* Initial and final quotes */ |
| 5533 | |
| 5534 | while (*p) |
| 5535 | { |
| 5536 | if (p[0] == '\\' && vim_iswhite(p[1])) |
| 5537 | { |
| 5538 | len += 1; |
| 5539 | p += 2; |
| 5540 | } |
| 5541 | else if (*p == '\\' || *p == '"') |
| 5542 | { |
| 5543 | len += 2; |
| 5544 | p += 1; |
| 5545 | } |
| 5546 | else if (vim_iswhite(*p)) |
| 5547 | { |
| 5548 | p = skipwhite(p); |
| 5549 | if (*p == NUL) |
| 5550 | break; |
| 5551 | len += 3; /* "," */ |
| 5552 | } |
| 5553 | else |
| 5554 | { |
| 5555 | ++len; |
| 5556 | ++p; |
| 5557 | } |
| 5558 | } |
| 5559 | |
| 5560 | buf = alloc(len + 1); |
| 5561 | if (buf == NULL) |
| 5562 | { |
| 5563 | *lenp = 0; |
| 5564 | return buf; |
| 5565 | } |
| 5566 | |
| 5567 | p = arg; |
| 5568 | q = buf; |
| 5569 | *q++ = '"'; |
| 5570 | while (*p) |
| 5571 | { |
| 5572 | if (p[0] == '\\' && vim_iswhite(p[1])) |
| 5573 | { |
| 5574 | *q++ = p[1]; |
| 5575 | p += 2; |
| 5576 | } |
| 5577 | else if (*p == '\\' || *p == '"') |
| 5578 | { |
| 5579 | *q++ = '\\'; |
| 5580 | *q++ = *p++; |
| 5581 | } |
| 5582 | else if (vim_iswhite(*p)) |
| 5583 | { |
| 5584 | p = skipwhite(p); |
| 5585 | if (*p == NUL) |
| 5586 | break; |
| 5587 | *q++ = '"'; |
| 5588 | *q++ = ','; |
| 5589 | *q++ = '"'; |
| 5590 | } |
| 5591 | else |
| 5592 | { |
| 5593 | *q++ = *p++; |
| 5594 | } |
| 5595 | } |
| 5596 | *q++ = '"'; |
| 5597 | *q = 0; |
| 5598 | |
| 5599 | *lenp = len; |
| 5600 | return buf; |
| 5601 | } |
| 5602 | |
| 5603 | /* |
| 5604 | * Check for a <> code in a user command. |
| 5605 | * "code" points to the '<'. "len" the length of the <> (inclusive). |
| 5606 | * "buf" is where the result is to be added. |
| 5607 | * "split_buf" points to a buffer used for splitting, caller should free it. |
| 5608 | * "split_len" is the length of what "split_buf" contains. |
| 5609 | * Returns the length of the replacement, which has been added to "buf". |
| 5610 | * Returns -1 if there was no match, and only the "<" has been copied. |
| 5611 | */ |
| 5612 | static size_t |
| 5613 | uc_check_code(code, len, buf, cmd, eap, split_buf, split_len) |
| 5614 | char_u *code; |
| 5615 | size_t len; |
| 5616 | char_u *buf; |
| 5617 | ucmd_T *cmd; /* the user command we're expanding */ |
| 5618 | exarg_T *eap; /* ex arguments */ |
| 5619 | char_u **split_buf; |
| 5620 | size_t *split_len; |
| 5621 | { |
| 5622 | size_t result = 0; |
| 5623 | char_u *p = code + 1; |
| 5624 | size_t l = len - 2; |
| 5625 | int quote = 0; |
| 5626 | enum { ct_ARGS, ct_BANG, ct_COUNT, ct_LINE1, ct_LINE2, ct_REGISTER, |
| 5627 | ct_LT, ct_NONE } type = ct_NONE; |
| 5628 | |
| 5629 | if ((vim_strchr((char_u *)"qQfF", *p) != NULL) && p[1] == '-') |
| 5630 | { |
| 5631 | quote = (*p == 'q' || *p == 'Q') ? 1 : 2; |
| 5632 | p += 2; |
| 5633 | l -= 2; |
| 5634 | } |
| 5635 | |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5636 | ++l; |
| 5637 | if (l <= 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5638 | type = ct_NONE; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5639 | else if (STRNICMP(p, "args>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5640 | type = ct_ARGS; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5641 | else if (STRNICMP(p, "bang>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5642 | type = ct_BANG; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5643 | else if (STRNICMP(p, "count>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5644 | type = ct_COUNT; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5645 | else if (STRNICMP(p, "line1>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5646 | type = ct_LINE1; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5647 | else if (STRNICMP(p, "line2>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5648 | type = ct_LINE2; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5649 | else if (STRNICMP(p, "lt>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5650 | type = ct_LT; |
Bram Moolenaar | 371d540 | 2006-03-20 21:47:49 +0000 | [diff] [blame] | 5651 | else if (STRNICMP(p, "reg>", l) == 0 || STRNICMP(p, "register>", l) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5652 | type = ct_REGISTER; |
| 5653 | |
| 5654 | switch (type) |
| 5655 | { |
| 5656 | case ct_ARGS: |
| 5657 | /* Simple case first */ |
| 5658 | if (*eap->arg == NUL) |
| 5659 | { |
| 5660 | if (quote == 1) |
| 5661 | { |
| 5662 | result = 2; |
| 5663 | if (buf != NULL) |
| 5664 | STRCPY(buf, "''"); |
| 5665 | } |
| 5666 | else |
| 5667 | result = 0; |
| 5668 | break; |
| 5669 | } |
| 5670 | |
| 5671 | /* When specified there is a single argument don't split it. |
| 5672 | * Works for ":Cmd %" when % is "a b c". */ |
| 5673 | if ((eap->argt & NOSPC) && quote == 2) |
| 5674 | quote = 1; |
| 5675 | |
| 5676 | switch (quote) |
| 5677 | { |
| 5678 | case 0: /* No quoting, no splitting */ |
| 5679 | result = STRLEN(eap->arg); |
| 5680 | if (buf != NULL) |
| 5681 | STRCPY(buf, eap->arg); |
| 5682 | break; |
| 5683 | case 1: /* Quote, but don't split */ |
| 5684 | result = STRLEN(eap->arg) + 2; |
| 5685 | for (p = eap->arg; *p; ++p) |
| 5686 | { |
| 5687 | if (*p == '\\' || *p == '"') |
| 5688 | ++result; |
| 5689 | } |
| 5690 | |
| 5691 | if (buf != NULL) |
| 5692 | { |
| 5693 | *buf++ = '"'; |
| 5694 | for (p = eap->arg; *p; ++p) |
| 5695 | { |
| 5696 | if (*p == '\\' || *p == '"') |
| 5697 | *buf++ = '\\'; |
| 5698 | *buf++ = *p; |
| 5699 | } |
| 5700 | *buf = '"'; |
| 5701 | } |
| 5702 | |
| 5703 | break; |
| 5704 | case 2: /* Quote and split */ |
| 5705 | /* This is hard, so only do it once, and cache the result */ |
| 5706 | if (*split_buf == NULL) |
| 5707 | *split_buf = uc_split_args(eap->arg, split_len); |
| 5708 | |
| 5709 | result = *split_len; |
| 5710 | if (buf != NULL && result != 0) |
| 5711 | STRCPY(buf, *split_buf); |
| 5712 | |
| 5713 | break; |
| 5714 | } |
| 5715 | break; |
| 5716 | |
| 5717 | case ct_BANG: |
| 5718 | result = eap->forceit ? 1 : 0; |
| 5719 | if (quote) |
| 5720 | result += 2; |
| 5721 | if (buf != NULL) |
| 5722 | { |
| 5723 | if (quote) |
| 5724 | *buf++ = '"'; |
| 5725 | if (eap->forceit) |
| 5726 | *buf++ = '!'; |
| 5727 | if (quote) |
| 5728 | *buf = '"'; |
| 5729 | } |
| 5730 | break; |
| 5731 | |
| 5732 | case ct_LINE1: |
| 5733 | case ct_LINE2: |
| 5734 | case ct_COUNT: |
| 5735 | { |
| 5736 | char num_buf[20]; |
| 5737 | long num = (type == ct_LINE1) ? eap->line1 : |
| 5738 | (type == ct_LINE2) ? eap->line2 : |
| 5739 | (eap->addr_count > 0) ? eap->line2 : cmd->uc_def; |
| 5740 | size_t num_len; |
| 5741 | |
| 5742 | sprintf(num_buf, "%ld", num); |
| 5743 | num_len = STRLEN(num_buf); |
| 5744 | result = num_len; |
| 5745 | |
| 5746 | if (quote) |
| 5747 | result += 2; |
| 5748 | |
| 5749 | if (buf != NULL) |
| 5750 | { |
| 5751 | if (quote) |
| 5752 | *buf++ = '"'; |
| 5753 | STRCPY(buf, num_buf); |
| 5754 | buf += num_len; |
| 5755 | if (quote) |
| 5756 | *buf = '"'; |
| 5757 | } |
| 5758 | |
| 5759 | break; |
| 5760 | } |
| 5761 | |
| 5762 | case ct_REGISTER: |
| 5763 | result = eap->regname ? 1 : 0; |
| 5764 | if (quote) |
| 5765 | result += 2; |
| 5766 | if (buf != NULL) |
| 5767 | { |
| 5768 | if (quote) |
| 5769 | *buf++ = '\''; |
| 5770 | if (eap->regname) |
| 5771 | *buf++ = eap->regname; |
| 5772 | if (quote) |
| 5773 | *buf = '\''; |
| 5774 | } |
| 5775 | break; |
| 5776 | |
| 5777 | case ct_LT: |
| 5778 | result = 1; |
| 5779 | if (buf != NULL) |
| 5780 | *buf = '<'; |
| 5781 | break; |
| 5782 | |
| 5783 | default: |
| 5784 | /* Not recognized: just copy the '<' and return -1. */ |
| 5785 | result = (size_t)-1; |
| 5786 | if (buf != NULL) |
| 5787 | *buf = '<'; |
| 5788 | break; |
| 5789 | } |
| 5790 | |
| 5791 | return result; |
| 5792 | } |
| 5793 | |
| 5794 | static void |
| 5795 | do_ucmd(eap) |
| 5796 | exarg_T *eap; |
| 5797 | { |
| 5798 | char_u *buf; |
| 5799 | char_u *p; |
| 5800 | char_u *q; |
| 5801 | |
| 5802 | char_u *start; |
| 5803 | char_u *end; |
| 5804 | size_t len, totlen; |
| 5805 | |
| 5806 | size_t split_len = 0; |
| 5807 | char_u *split_buf = NULL; |
| 5808 | ucmd_T *cmd; |
| 5809 | #ifdef FEAT_EVAL |
| 5810 | scid_T save_current_SID = current_SID; |
| 5811 | #endif |
| 5812 | |
| 5813 | if (eap->cmdidx == CMD_USER) |
| 5814 | cmd = USER_CMD(eap->useridx); |
| 5815 | else |
| 5816 | cmd = USER_CMD_GA(&curbuf->b_ucmds, eap->useridx); |
| 5817 | |
| 5818 | /* |
| 5819 | * Replace <> in the command by the arguments. |
| 5820 | */ |
| 5821 | buf = NULL; |
| 5822 | for (;;) |
| 5823 | { |
| 5824 | p = cmd->uc_rep; |
| 5825 | q = buf; |
| 5826 | totlen = 0; |
| 5827 | while ((start = vim_strchr(p, '<')) != NULL |
| 5828 | && (end = vim_strchr(start + 1, '>')) != NULL) |
| 5829 | { |
| 5830 | /* Include the '>' */ |
| 5831 | ++end; |
| 5832 | |
| 5833 | /* Take everything up to the '<' */ |
| 5834 | len = start - p; |
| 5835 | if (buf == NULL) |
| 5836 | totlen += len; |
| 5837 | else |
| 5838 | { |
| 5839 | mch_memmove(q, p, len); |
| 5840 | q += len; |
| 5841 | } |
| 5842 | |
| 5843 | len = uc_check_code(start, end - start, q, cmd, eap, |
| 5844 | &split_buf, &split_len); |
| 5845 | if (len == (size_t)-1) |
| 5846 | { |
| 5847 | /* no match, continue after '<' */ |
| 5848 | p = start + 1; |
| 5849 | len = 1; |
| 5850 | } |
| 5851 | else |
| 5852 | p = end; |
| 5853 | if (buf == NULL) |
| 5854 | totlen += len; |
| 5855 | else |
| 5856 | q += len; |
| 5857 | } |
| 5858 | if (buf != NULL) /* second time here, finished */ |
| 5859 | { |
| 5860 | STRCPY(q, p); |
| 5861 | break; |
| 5862 | } |
| 5863 | |
| 5864 | totlen += STRLEN(p); /* Add on the trailing characters */ |
| 5865 | buf = alloc((unsigned)(totlen + 1)); |
| 5866 | if (buf == NULL) |
| 5867 | { |
| 5868 | vim_free(split_buf); |
| 5869 | return; |
| 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | #ifdef FEAT_EVAL |
| 5874 | current_SID = cmd->uc_scriptID; |
| 5875 | #endif |
| 5876 | (void)do_cmdline(buf, eap->getline, eap->cookie, |
| 5877 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED); |
| 5878 | #ifdef FEAT_EVAL |
| 5879 | current_SID = save_current_SID; |
| 5880 | #endif |
| 5881 | vim_free(buf); |
| 5882 | vim_free(split_buf); |
| 5883 | } |
| 5884 | |
| 5885 | # if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5886 | static char_u * |
| 5887 | get_user_command_name(idx) |
| 5888 | int idx; |
| 5889 | { |
| 5890 | return get_user_commands(NULL, idx - (int)CMD_SIZE); |
| 5891 | } |
| 5892 | |
| 5893 | /* |
| 5894 | * Function given to ExpandGeneric() to obtain the list of user command names. |
| 5895 | */ |
| 5896 | /*ARGSUSED*/ |
| 5897 | char_u * |
| 5898 | get_user_commands(xp, idx) |
| 5899 | expand_T *xp; |
| 5900 | int idx; |
| 5901 | { |
| 5902 | if (idx < curbuf->b_ucmds.ga_len) |
| 5903 | return USER_CMD_GA(&curbuf->b_ucmds, idx)->uc_name; |
| 5904 | idx -= curbuf->b_ucmds.ga_len; |
| 5905 | if (idx < ucmds.ga_len) |
| 5906 | return USER_CMD(idx)->uc_name; |
| 5907 | return NULL; |
| 5908 | } |
| 5909 | |
| 5910 | /* |
| 5911 | * Function given to ExpandGeneric() to obtain the list of user command |
| 5912 | * attributes. |
| 5913 | */ |
| 5914 | /*ARGSUSED*/ |
| 5915 | char_u * |
| 5916 | get_user_cmd_flags(xp, idx) |
| 5917 | expand_T *xp; |
| 5918 | int idx; |
| 5919 | { |
| 5920 | static char *user_cmd_flags[] = |
| 5921 | {"bang", "bar", "buffer", "complete", "count", |
| 5922 | "nargs", "range", "register"}; |
| 5923 | |
| 5924 | if (idx >= sizeof(user_cmd_flags) / sizeof(user_cmd_flags[0])) |
| 5925 | return NULL; |
| 5926 | return (char_u *)user_cmd_flags[idx]; |
| 5927 | } |
| 5928 | |
| 5929 | /* |
| 5930 | * Function given to ExpandGeneric() to obtain the list of values for -nargs. |
| 5931 | */ |
| 5932 | /*ARGSUSED*/ |
| 5933 | char_u * |
| 5934 | get_user_cmd_nargs(xp, idx) |
| 5935 | expand_T *xp; |
| 5936 | int idx; |
| 5937 | { |
| 5938 | static char *user_cmd_nargs[] = {"0", "1", "*", "?", "+"}; |
| 5939 | |
| 5940 | if (idx >= sizeof(user_cmd_nargs) / sizeof(user_cmd_nargs[0])) |
| 5941 | return NULL; |
| 5942 | return (char_u *)user_cmd_nargs[idx]; |
| 5943 | } |
| 5944 | |
| 5945 | /* |
| 5946 | * Function given to ExpandGeneric() to obtain the list of values for -complete. |
| 5947 | */ |
| 5948 | /*ARGSUSED*/ |
| 5949 | char_u * |
| 5950 | get_user_cmd_complete(xp, idx) |
| 5951 | expand_T *xp; |
| 5952 | int idx; |
| 5953 | { |
| 5954 | return (char_u *)command_complete[idx].name; |
| 5955 | } |
| 5956 | # endif /* FEAT_CMDL_COMPL */ |
| 5957 | |
| 5958 | #endif /* FEAT_USR_CMDS */ |
| 5959 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5960 | #if defined(FEAT_USR_CMDS) || defined(FEAT_EVAL) || defined(PROTO) |
| 5961 | /* |
| 5962 | * Parse a completion argument "value[vallen]". |
| 5963 | * The detected completion goes in "*complp", argument type in "*argt". |
| 5964 | * When there is an argument, for function and user defined completion, it's |
| 5965 | * copied to allocated memory and stored in "*compl_arg". |
| 5966 | * Returns FAIL if something is wrong. |
| 5967 | */ |
| 5968 | int |
| 5969 | parse_compl_arg(value, vallen, complp, argt, compl_arg) |
| 5970 | char_u *value; |
| 5971 | int vallen; |
| 5972 | int *complp; |
| 5973 | long *argt; |
| 5974 | char_u **compl_arg; |
| 5975 | { |
| 5976 | char_u *arg = NULL; |
| 5977 | size_t arglen = 0; |
| 5978 | int i; |
| 5979 | int valend = vallen; |
| 5980 | |
| 5981 | /* Look for any argument part - which is the part after any ',' */ |
| 5982 | for (i = 0; i < vallen; ++i) |
| 5983 | { |
| 5984 | if (value[i] == ',') |
| 5985 | { |
| 5986 | arg = &value[i + 1]; |
| 5987 | arglen = vallen - i - 1; |
| 5988 | valend = i; |
| 5989 | break; |
| 5990 | } |
| 5991 | } |
| 5992 | |
| 5993 | for (i = 0; command_complete[i].expand != 0; ++i) |
| 5994 | { |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 5995 | if ((int)STRLEN(command_complete[i].name) == valend |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5996 | && STRNCMP(value, command_complete[i].name, valend) == 0) |
| 5997 | { |
| 5998 | *complp = command_complete[i].expand; |
| 5999 | if (command_complete[i].expand == EXPAND_BUFFERS) |
| 6000 | *argt |= BUFNAME; |
| 6001 | else if (command_complete[i].expand == EXPAND_DIRECTORIES |
| 6002 | || command_complete[i].expand == EXPAND_FILES) |
| 6003 | *argt |= XFILE; |
| 6004 | break; |
| 6005 | } |
| 6006 | } |
| 6007 | |
| 6008 | if (command_complete[i].expand == 0) |
| 6009 | { |
| 6010 | EMSG2(_("E180: Invalid complete value: %s"), value); |
| 6011 | return FAIL; |
| 6012 | } |
| 6013 | |
| 6014 | # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 6015 | if (*complp != EXPAND_USER_DEFINED && *complp != EXPAND_USER_LIST |
| 6016 | && arg != NULL) |
| 6017 | # else |
| 6018 | if (arg != NULL) |
| 6019 | # endif |
| 6020 | { |
| 6021 | EMSG(_("E468: Completion argument only allowed for custom completion")); |
| 6022 | return FAIL; |
| 6023 | } |
| 6024 | |
| 6025 | # if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 6026 | if ((*complp == EXPAND_USER_DEFINED || *complp == EXPAND_USER_LIST) |
| 6027 | && arg == NULL) |
| 6028 | { |
| 6029 | EMSG(_("E467: Custom completion requires a function argument")); |
| 6030 | return FAIL; |
| 6031 | } |
| 6032 | |
| 6033 | if (arg != NULL) |
| 6034 | *compl_arg = vim_strnsave(arg, (int)arglen); |
| 6035 | # endif |
| 6036 | return OK; |
| 6037 | } |
| 6038 | #endif |
| 6039 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6040 | static void |
| 6041 | ex_colorscheme(eap) |
| 6042 | exarg_T *eap; |
| 6043 | { |
| 6044 | if (load_colors(eap->arg) == FAIL) |
| 6045 | EMSG2(_("E185: Cannot find color scheme %s"), eap->arg); |
| 6046 | } |
| 6047 | |
| 6048 | static void |
| 6049 | ex_highlight(eap) |
| 6050 | exarg_T *eap; |
| 6051 | { |
| 6052 | if (*eap->arg == NUL && eap->cmd[2] == '!') |
| 6053 | MSG(_("Greetings, Vim user!")); |
| 6054 | do_highlight(eap->arg, eap->forceit, FALSE); |
| 6055 | } |
| 6056 | |
| 6057 | |
| 6058 | /* |
| 6059 | * Call this function if we thought we were going to exit, but we won't |
| 6060 | * (because of an error). May need to restore the terminal mode. |
| 6061 | */ |
| 6062 | void |
| 6063 | not_exiting() |
| 6064 | { |
| 6065 | exiting = FALSE; |
| 6066 | settmode(TMODE_RAW); |
| 6067 | } |
| 6068 | |
| 6069 | /* |
| 6070 | * ":quit": quit current window, quit Vim if closed the last window. |
| 6071 | */ |
| 6072 | static void |
| 6073 | ex_quit(eap) |
| 6074 | exarg_T *eap; |
| 6075 | { |
| 6076 | #ifdef FEAT_CMDWIN |
| 6077 | if (cmdwin_type != 0) |
| 6078 | { |
| 6079 | cmdwin_result = Ctrl_C; |
| 6080 | return; |
| 6081 | } |
| 6082 | #endif |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6083 | /* Don't quit while editing the command line. */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6084 | if (text_locked()) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6085 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6086 | text_locked_msg(); |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6087 | return; |
| 6088 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6089 | |
| 6090 | #ifdef FEAT_NETBEANS_INTG |
| 6091 | netbeansForcedQuit = eap->forceit; |
| 6092 | #endif |
| 6093 | |
| 6094 | /* |
| 6095 | * If there are more files or windows we won't exit. |
| 6096 | */ |
| 6097 | if (check_more(FALSE, eap->forceit) == OK && only_one_window()) |
| 6098 | exiting = TRUE; |
| 6099 | if ((!P_HID(curbuf) |
| 6100 | && check_changed(curbuf, p_awa, FALSE, eap->forceit, FALSE)) |
| 6101 | || check_more(TRUE, eap->forceit) == FAIL |
| 6102 | || (only_one_window() && check_changed_any(eap->forceit))) |
| 6103 | { |
| 6104 | not_exiting(); |
| 6105 | } |
| 6106 | else |
| 6107 | { |
| 6108 | #ifdef FEAT_WINDOWS |
| 6109 | if (only_one_window()) /* quit last window */ |
| 6110 | #endif |
| 6111 | getout(0); |
| 6112 | #ifdef FEAT_WINDOWS |
| 6113 | # ifdef FEAT_GUI |
| 6114 | need_mouse_correct = TRUE; |
| 6115 | # endif |
| 6116 | /* close window; may free buffer */ |
| 6117 | win_close(curwin, !P_HID(curwin->w_buffer) || eap->forceit); |
| 6118 | #endif |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | /* |
| 6123 | * ":cquit". |
| 6124 | */ |
| 6125 | /*ARGSUSED*/ |
| 6126 | static void |
| 6127 | ex_cquit(eap) |
| 6128 | exarg_T *eap; |
| 6129 | { |
| 6130 | getout(1); /* this does not always pass on the exit code to the Manx |
| 6131 | compiler. why? */ |
| 6132 | } |
| 6133 | |
| 6134 | /* |
| 6135 | * ":qall": try to quit all windows |
| 6136 | */ |
| 6137 | static void |
| 6138 | ex_quit_all(eap) |
| 6139 | exarg_T *eap; |
| 6140 | { |
| 6141 | # ifdef FEAT_CMDWIN |
| 6142 | if (cmdwin_type != 0) |
| 6143 | { |
| 6144 | if (eap->forceit) |
| 6145 | cmdwin_result = K_XF1; /* ex_window() takes care of this */ |
| 6146 | else |
| 6147 | cmdwin_result = K_XF2; |
| 6148 | return; |
| 6149 | } |
| 6150 | # endif |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6151 | |
| 6152 | /* Don't quit while editing the command line. */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6153 | if (text_locked()) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6154 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6155 | text_locked_msg(); |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6156 | return; |
| 6157 | } |
| 6158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6159 | exiting = TRUE; |
| 6160 | if (eap->forceit || !check_changed_any(FALSE)) |
| 6161 | getout(0); |
| 6162 | not_exiting(); |
| 6163 | } |
| 6164 | |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6165 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6166 | /* |
| 6167 | * ":close": close current window, unless it is the last one |
| 6168 | */ |
| 6169 | static void |
| 6170 | ex_close(eap) |
| 6171 | exarg_T *eap; |
| 6172 | { |
| 6173 | # ifdef FEAT_CMDWIN |
| 6174 | if (cmdwin_type != 0) |
| 6175 | cmdwin_result = K_IGNORE; |
| 6176 | else |
| 6177 | # endif |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6178 | if (!text_locked()) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6179 | ex_win_close(eap->forceit, curwin, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6180 | } |
| 6181 | |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6182 | # ifdef FEAT_QUICKFIX |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6183 | /* |
| 6184 | * ":pclose": Close any preview window. |
| 6185 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6186 | static void |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6187 | ex_pclose(eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6188 | exarg_T *eap; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6189 | { |
| 6190 | win_T *win; |
| 6191 | |
| 6192 | for (win = firstwin; win != NULL; win = win->w_next) |
| 6193 | if (win->w_p_pvw) |
| 6194 | { |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6195 | ex_win_close(eap->forceit, win, NULL); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6196 | break; |
| 6197 | } |
| 6198 | } |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6199 | # endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6200 | |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6201 | /* |
| 6202 | * Close window "win" and take care of handling closing the last window for a |
| 6203 | * modified buffer. |
| 6204 | */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6205 | static void |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6206 | ex_win_close(forceit, win, tp) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6207 | int forceit; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6208 | win_T *win; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6209 | tabpage_T *tp; /* NULL or the tab page "win" is in */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6210 | { |
| 6211 | int need_hide; |
| 6212 | buf_T *buf = win->w_buffer; |
| 6213 | |
| 6214 | need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6215 | if (need_hide && !P_HID(buf) && !forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6216 | { |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6217 | # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6218 | if ((p_confirm || cmdmod.confirm) && p_write) |
| 6219 | { |
| 6220 | dialog_changed(buf, FALSE); |
| 6221 | if (buf_valid(buf) && bufIsChanged(buf)) |
| 6222 | return; |
| 6223 | need_hide = FALSE; |
| 6224 | } |
| 6225 | else |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6226 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6227 | { |
| 6228 | EMSG(_(e_nowrtmsg)); |
| 6229 | return; |
| 6230 | } |
| 6231 | } |
| 6232 | |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6233 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6234 | need_mouse_correct = TRUE; |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6235 | # endif |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6236 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6237 | /* 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] | 6238 | if (tp == NULL) |
| 6239 | win_close(win, !need_hide && !P_HID(buf)); |
| 6240 | else |
| 6241 | win_close_othertab(win, !need_hide && !P_HID(buf), tp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6242 | } |
| 6243 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6244 | /* |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6245 | * ":tabclose": close current tab page, unless it is the last one. |
| 6246 | * ":tabclose N": close tab page N. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6247 | */ |
| 6248 | static void |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6249 | ex_tabclose(eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6250 | exarg_T *eap; |
| 6251 | { |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6252 | tabpage_T *tp; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6253 | int h = tabline_height(); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6254 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6255 | # ifdef FEAT_CMDWIN |
| 6256 | if (cmdwin_type != 0) |
| 6257 | cmdwin_result = K_IGNORE; |
| 6258 | else |
| 6259 | # endif |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6260 | if (first_tabpage->tp_next == NULL) |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 6261 | EMSG(_("E784: Cannot close last tab page")); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6262 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6263 | { |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6264 | if (eap->addr_count > 0) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6265 | { |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6266 | tp = find_tabpage((int)eap->line2); |
| 6267 | if (tp == NULL) |
| 6268 | { |
| 6269 | beep_flush(); |
| 6270 | return; |
| 6271 | } |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 6272 | if (tp != curtab) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6273 | { |
| 6274 | tabpage_close_other(tp, eap->forceit); |
| 6275 | return; |
| 6276 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6277 | } |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6278 | if (!text_locked()) |
| 6279 | tabpage_close(eap->forceit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6280 | } |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6281 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6282 | if (h != tabline_height()) |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6283 | shell_new_rows(); |
| 6284 | } |
| 6285 | |
| 6286 | /* |
| 6287 | * ":tabonly": close all tab pages except the current one |
| 6288 | */ |
| 6289 | static void |
| 6290 | ex_tabonly(eap) |
| 6291 | exarg_T *eap; |
| 6292 | { |
| 6293 | tabpage_T *tp; |
| 6294 | int done; |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6295 | int h = tabline_height(); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6296 | |
| 6297 | # ifdef FEAT_CMDWIN |
| 6298 | if (cmdwin_type != 0) |
| 6299 | cmdwin_result = K_IGNORE; |
| 6300 | else |
| 6301 | # endif |
| 6302 | if (first_tabpage->tp_next == NULL) |
| 6303 | MSG(_("Already only one tab page")); |
| 6304 | else |
| 6305 | { |
| 6306 | /* Repeat this up to a 1000 times, because autocommands may mess |
| 6307 | * up the lists. */ |
| 6308 | for (done = 0; done < 1000; ++done) |
| 6309 | { |
| 6310 | for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) |
| 6311 | if (tp->tp_topframe != topframe) |
| 6312 | { |
| 6313 | tabpage_close_other(tp, eap->forceit); |
| 6314 | /* if we failed to close it quit */ |
| 6315 | if (valid_tabpage(tp)) |
| 6316 | done = 1000; |
| 6317 | /* start over, "tp" is now invalid */ |
| 6318 | break; |
| 6319 | } |
| 6320 | if (first_tabpage->tp_next == NULL) |
| 6321 | break; |
| 6322 | } |
| 6323 | } |
| 6324 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 6325 | if (h != tabline_height()) |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6326 | shell_new_rows(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6327 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6328 | |
| 6329 | /* |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6330 | * Close the current tab page. |
| 6331 | */ |
| 6332 | void |
| 6333 | tabpage_close(forceit) |
| 6334 | int forceit; |
| 6335 | { |
| 6336 | /* First close all the windows but the current one. If that worked then |
| 6337 | * close the last window in this tab, that will close it. */ |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6338 | if (lastwin != firstwin) |
| 6339 | close_others(TRUE, forceit); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6340 | if (lastwin == firstwin) |
| 6341 | ex_win_close(forceit, curwin, NULL); |
| 6342 | # ifdef FEAT_GUI |
| 6343 | need_mouse_correct = TRUE; |
| 6344 | # endif |
| 6345 | } |
| 6346 | |
| 6347 | /* |
| 6348 | * Close tab page "tp", which is not the current tab page. |
| 6349 | * Note that autocommands may make "tp" invalid. |
| 6350 | */ |
| 6351 | void |
| 6352 | tabpage_close_other(tp, forceit) |
| 6353 | tabpage_T *tp; |
| 6354 | int forceit; |
| 6355 | { |
| 6356 | int done = 0; |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6357 | win_T *wp; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6358 | |
| 6359 | /* Limit to 1000 windows, autocommands may add a window while we close |
| 6360 | * one. OK, so I'm paranoid... */ |
| 6361 | while (++done < 1000) |
| 6362 | { |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6363 | wp = tp->tp_firstwin; |
| 6364 | ex_win_close(forceit, wp, tp); |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6365 | |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6366 | /* Autocommands may delete the tab page under our fingers and we may |
| 6367 | * fail to close a window with a modified buffer. */ |
| 6368 | if (!valid_tabpage(tp) || tp->tp_firstwin == wp) |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6369 | break; |
| 6370 | } |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 6371 | redraw_tabline = TRUE; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
| 6374 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6375 | * ":only". |
| 6376 | */ |
| 6377 | static void |
| 6378 | ex_only(eap) |
| 6379 | exarg_T *eap; |
| 6380 | { |
| 6381 | # ifdef FEAT_GUI |
| 6382 | need_mouse_correct = TRUE; |
| 6383 | # endif |
| 6384 | close_others(TRUE, eap->forceit); |
| 6385 | } |
| 6386 | |
| 6387 | /* |
| 6388 | * ":all" and ":sall". |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6389 | * Also used for ":tab drop file ..." after setting the argument list. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6390 | */ |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6391 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6392 | ex_all(eap) |
| 6393 | exarg_T *eap; |
| 6394 | { |
| 6395 | if (eap->addr_count == 0) |
| 6396 | eap->line2 = 9999; |
Bram Moolenaar | d996771 | 2006-03-11 21:18:15 +0000 | [diff] [blame] | 6397 | do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6398 | } |
| 6399 | #endif /* FEAT_WINDOWS */ |
| 6400 | |
| 6401 | static void |
| 6402 | ex_hide(eap) |
| 6403 | exarg_T *eap; |
| 6404 | { |
| 6405 | if (*eap->arg != NUL && check_nextcmd(eap->arg) == NULL) |
| 6406 | eap->errmsg = e_invarg; |
| 6407 | else |
| 6408 | { |
| 6409 | /* ":hide" or ":hide | cmd": hide current window */ |
| 6410 | eap->nextcmd = check_nextcmd(eap->arg); |
| 6411 | #ifdef FEAT_WINDOWS |
| 6412 | if (!eap->skip) |
| 6413 | { |
| 6414 | # ifdef FEAT_GUI |
| 6415 | need_mouse_correct = TRUE; |
| 6416 | # endif |
| 6417 | win_close(curwin, FALSE); /* don't free buffer */ |
| 6418 | } |
| 6419 | #endif |
| 6420 | } |
| 6421 | } |
| 6422 | |
| 6423 | /* |
| 6424 | * ":stop" and ":suspend": Suspend Vim. |
| 6425 | */ |
| 6426 | static void |
| 6427 | ex_stop(eap) |
| 6428 | exarg_T *eap; |
| 6429 | { |
| 6430 | /* |
| 6431 | * Disallow suspending for "rvim". |
| 6432 | */ |
| 6433 | if (!check_restricted() |
| 6434 | #ifdef WIN3264 |
| 6435 | /* |
| 6436 | * Check if external commands are allowed now. |
| 6437 | */ |
| 6438 | && can_end_termcap_mode(TRUE) |
| 6439 | #endif |
| 6440 | ) |
| 6441 | { |
| 6442 | if (!eap->forceit) |
| 6443 | autowrite_all(); |
| 6444 | windgoto((int)Rows - 1, 0); |
| 6445 | out_char('\n'); |
| 6446 | out_flush(); |
| 6447 | stoptermcap(); |
| 6448 | out_flush(); /* needed for SUN to restore xterm buffer */ |
| 6449 | #ifdef FEAT_TITLE |
| 6450 | mch_restore_title(3); /* restore window titles */ |
| 6451 | #endif |
| 6452 | ui_suspend(); /* call machine specific function */ |
| 6453 | #ifdef FEAT_TITLE |
| 6454 | maketitle(); |
| 6455 | resettitle(); /* force updating the title */ |
| 6456 | #endif |
| 6457 | starttermcap(); |
| 6458 | scroll_start(); /* scroll screen before redrawing */ |
| 6459 | redraw_later_clear(); |
| 6460 | shell_resized(); /* may have resized window */ |
| 6461 | } |
| 6462 | } |
| 6463 | |
| 6464 | /* |
| 6465 | * ":exit", ":xit" and ":wq": Write file and exit Vim. |
| 6466 | */ |
| 6467 | static void |
| 6468 | ex_exit(eap) |
| 6469 | exarg_T *eap; |
| 6470 | { |
| 6471 | #ifdef FEAT_CMDWIN |
| 6472 | if (cmdwin_type != 0) |
| 6473 | { |
| 6474 | cmdwin_result = Ctrl_C; |
| 6475 | return; |
| 6476 | } |
| 6477 | #endif |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6478 | /* Don't quit while editing the command line. */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6479 | if (text_locked()) |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6480 | { |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6481 | text_locked_msg(); |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6482 | return; |
| 6483 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6484 | |
| 6485 | /* |
| 6486 | * if more files or windows we won't exit |
| 6487 | */ |
| 6488 | if (check_more(FALSE, eap->forceit) == OK && only_one_window()) |
| 6489 | exiting = TRUE; |
| 6490 | if ( ((eap->cmdidx == CMD_wq |
| 6491 | || curbufIsChanged()) |
| 6492 | && do_write(eap) == FAIL) |
| 6493 | || check_more(TRUE, eap->forceit) == FAIL |
| 6494 | || (only_one_window() && check_changed_any(eap->forceit))) |
| 6495 | { |
| 6496 | not_exiting(); |
| 6497 | } |
| 6498 | else |
| 6499 | { |
| 6500 | #ifdef FEAT_WINDOWS |
| 6501 | if (only_one_window()) /* quit last window, exit Vim */ |
| 6502 | #endif |
| 6503 | getout(0); |
| 6504 | #ifdef FEAT_WINDOWS |
| 6505 | # ifdef FEAT_GUI |
| 6506 | need_mouse_correct = TRUE; |
| 6507 | # endif |
| 6508 | /* quit current window, may free buffer */ |
| 6509 | win_close(curwin, !P_HID(curwin->w_buffer)); |
| 6510 | #endif |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | /* |
| 6515 | * ":print", ":list", ":number". |
| 6516 | */ |
| 6517 | static void |
| 6518 | ex_print(eap) |
| 6519 | exarg_T *eap; |
| 6520 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6521 | if (curbuf->b_ml.ml_flags & ML_EMPTY) |
| 6522 | EMSG(_(e_emptybuf)); |
| 6523 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 6525 | for ( ;!got_int; ui_breakcheck()) |
| 6526 | { |
| 6527 | print_line(eap->line1, |
| 6528 | (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound |
| 6529 | || (eap->flags & EXFLAG_NR)), |
| 6530 | eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST)); |
| 6531 | if (++eap->line1 > eap->line2) |
| 6532 | break; |
| 6533 | out_flush(); /* show one line at a time */ |
| 6534 | } |
| 6535 | setpcmark(); |
| 6536 | /* put cursor at last line */ |
| 6537 | curwin->w_cursor.lnum = eap->line2; |
| 6538 | beginline(BL_SOL | BL_FIX); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6539 | } |
| 6540 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6541 | ex_no_reprint = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | #ifdef FEAT_BYTEOFF |
| 6545 | static void |
| 6546 | ex_goto(eap) |
| 6547 | exarg_T *eap; |
| 6548 | { |
| 6549 | goto_byte(eap->line2); |
| 6550 | } |
| 6551 | #endif |
| 6552 | |
| 6553 | /* |
| 6554 | * ":shell". |
| 6555 | */ |
| 6556 | /*ARGSUSED*/ |
| 6557 | static void |
| 6558 | ex_shell(eap) |
| 6559 | exarg_T *eap; |
| 6560 | { |
| 6561 | do_shell(NULL, 0); |
| 6562 | } |
| 6563 | |
| 6564 | #if (defined(FEAT_WINDOWS) && defined(HAVE_DROP_FILE)) \ |
| 6565 | || (defined(FEAT_GUI_GTK) && defined(FEAT_DND)) \ |
Bram Moolenaar | 371baa9 | 2005-12-29 22:43:53 +0000 | [diff] [blame] | 6566 | || defined(FEAT_GUI_MSWIN) \ |
| 6567 | || defined(FEAT_GUI_MAC) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6568 | || defined(PROTO) |
| 6569 | |
| 6570 | /* |
| 6571 | * Handle a file drop. The code is here because a drop is *nearly* like an |
| 6572 | * :args command, but not quite (we have a list of exact filenames, so we |
| 6573 | * don't want to (a) parse a command line, or (b) expand wildcards. So the |
| 6574 | * code is very similar to :args and hence needs access to a lot of the static |
| 6575 | * functions in this file. |
| 6576 | * |
| 6577 | * The list should be allocated using alloc(), as should each item in the |
| 6578 | * list. This function takes over responsibility for freeing the list. |
| 6579 | * |
| 6580 | * XXX The list is made into the arggument list. This is freed using |
| 6581 | * FreeWild(), which does a series of vim_free() calls, unless the two defines |
| 6582 | * __EMX__ and __ALWAYS_HAS_TRAILING_NUL_POINTER are set. In this case, a |
| 6583 | * routine _fnexplodefree() is used. This may cause problems, but as the drop |
| 6584 | * file functionality is (currently) not in EMX this is not presently a |
| 6585 | * problem. |
| 6586 | */ |
| 6587 | void |
| 6588 | handle_drop(filec, filev, split) |
| 6589 | int filec; /* the number of files dropped */ |
| 6590 | char_u **filev; /* the list of files dropped */ |
| 6591 | int split; /* force splitting the window */ |
| 6592 | { |
| 6593 | exarg_T ea; |
| 6594 | int save_msg_scroll = msg_scroll; |
| 6595 | |
Bram Moolenaar | 05a7bb3 | 2006-01-19 22:09:32 +0000 | [diff] [blame] | 6596 | /* Postpone this while editing the command line. */ |
Bram Moolenaar | 2d3f489 | 2006-01-20 23:02:51 +0000 | [diff] [blame] | 6597 | if (text_locked()) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6598 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6599 | |
| 6600 | /* Check whether the current buffer is changed. If so, we will need |
| 6601 | * to split the current window or data could be lost. |
| 6602 | * We don't need to check if the 'hidden' option is set, as in this |
| 6603 | * case the buffer won't be lost. |
| 6604 | */ |
| 6605 | if (!P_HID(curbuf) && !split) |
| 6606 | { |
| 6607 | ++emsg_off; |
| 6608 | split = check_changed(curbuf, TRUE, FALSE, FALSE, FALSE); |
| 6609 | --emsg_off; |
| 6610 | } |
| 6611 | if (split) |
| 6612 | { |
| 6613 | # ifdef FEAT_WINDOWS |
| 6614 | if (win_split(0, 0) == FAIL) |
| 6615 | return; |
| 6616 | # ifdef FEAT_SCROLLBIND |
| 6617 | curwin->w_p_scb = FALSE; |
| 6618 | # endif |
| 6619 | |
| 6620 | /* When splitting the window, create a new alist. Otherwise the |
| 6621 | * existing one is overwritten. */ |
| 6622 | alist_unlink(curwin->w_alist); |
| 6623 | alist_new(); |
| 6624 | # else |
| 6625 | return; /* can't split, always fail */ |
| 6626 | # endif |
| 6627 | } |
| 6628 | |
| 6629 | /* |
| 6630 | * Set up the new argument list. |
| 6631 | */ |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6632 | alist_set(ALIST(curwin), filec, filev, FALSE, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6633 | |
| 6634 | /* |
| 6635 | * Move to the first file. |
| 6636 | */ |
| 6637 | /* Fake up a minimal "next" command for do_argfile() */ |
| 6638 | vim_memset(&ea, 0, sizeof(ea)); |
| 6639 | ea.cmd = (char_u *)"next"; |
| 6640 | do_argfile(&ea, 0); |
| 6641 | |
| 6642 | /* do_ecmd() may set need_start_insertmode, but since we never left Insert |
| 6643 | * mode that is not needed here. */ |
| 6644 | need_start_insertmode = FALSE; |
| 6645 | |
| 6646 | /* Restore msg_scroll, otherwise a following command may cause scrolling |
| 6647 | * unexpectedly. The screen will be redrawn by the caller, thus |
| 6648 | * msg_scroll being set by displaying a message is irrelevant. */ |
| 6649 | msg_scroll = save_msg_scroll; |
| 6650 | } |
| 6651 | #endif |
| 6652 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6653 | /* |
| 6654 | * Clear an argument list: free all file names and reset it to zero entries. |
| 6655 | */ |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 6656 | void |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6657 | alist_clear(al) |
| 6658 | alist_T *al; |
| 6659 | { |
| 6660 | while (--al->al_ga.ga_len >= 0) |
| 6661 | vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname); |
| 6662 | ga_clear(&al->al_ga); |
| 6663 | } |
| 6664 | |
| 6665 | /* |
| 6666 | * Init an argument list. |
| 6667 | */ |
| 6668 | void |
| 6669 | alist_init(al) |
| 6670 | alist_T *al; |
| 6671 | { |
| 6672 | ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5); |
| 6673 | } |
| 6674 | |
| 6675 | #if defined(FEAT_WINDOWS) || defined(PROTO) |
| 6676 | |
| 6677 | /* |
| 6678 | * Remove a reference from an argument list. |
| 6679 | * Ignored when the argument list is the global one. |
| 6680 | * If the argument list is no longer used by any window, free it. |
| 6681 | */ |
| 6682 | void |
| 6683 | alist_unlink(al) |
| 6684 | alist_T *al; |
| 6685 | { |
| 6686 | if (al != &global_alist && --al->al_refcount <= 0) |
| 6687 | { |
| 6688 | alist_clear(al); |
| 6689 | vim_free(al); |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | # if defined(FEAT_LISTCMDS) || defined(HAVE_DROP_FILE) || defined(PROTO) |
| 6694 | /* |
| 6695 | * Create a new argument list and use it for the current window. |
| 6696 | */ |
| 6697 | void |
| 6698 | alist_new() |
| 6699 | { |
| 6700 | curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T)); |
| 6701 | if (curwin->w_alist == NULL) |
| 6702 | { |
| 6703 | curwin->w_alist = &global_alist; |
| 6704 | ++global_alist.al_refcount; |
| 6705 | } |
| 6706 | else |
| 6707 | { |
| 6708 | curwin->w_alist->al_refcount = 1; |
| 6709 | alist_init(curwin->w_alist); |
| 6710 | } |
| 6711 | } |
| 6712 | # endif |
| 6713 | #endif |
| 6714 | |
| 6715 | #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE) || defined(PROTO) |
| 6716 | /* |
| 6717 | * Expand the file names in the global argument list. |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6718 | * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer |
| 6719 | * numbers to be re-used. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6720 | */ |
| 6721 | void |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6722 | alist_expand(fnum_list, fnum_len) |
| 6723 | int *fnum_list; |
| 6724 | int fnum_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6725 | { |
| 6726 | char_u **old_arg_files; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6727 | int old_arg_count; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6728 | char_u **new_arg_files; |
| 6729 | int new_arg_file_count; |
| 6730 | char_u *save_p_su = p_su; |
| 6731 | int i; |
| 6732 | |
| 6733 | /* Don't use 'suffixes' here. This should work like the shell did the |
| 6734 | * expansion. Also, the vimrc file isn't read yet, thus the user |
| 6735 | * can't set the options. */ |
| 6736 | p_su = empty_option; |
| 6737 | old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT)); |
| 6738 | if (old_arg_files != NULL) |
| 6739 | { |
| 6740 | for (i = 0; i < GARGCOUNT; ++i) |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6741 | old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname); |
| 6742 | old_arg_count = GARGCOUNT; |
| 6743 | if (expand_wildcards(old_arg_count, old_arg_files, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6744 | &new_arg_file_count, &new_arg_files, |
| 6745 | EW_FILE|EW_NOTFOUND|EW_ADDSLASH) == OK |
| 6746 | && new_arg_file_count > 0) |
| 6747 | { |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6748 | alist_set(&global_alist, new_arg_file_count, new_arg_files, |
| 6749 | TRUE, fnum_list, fnum_len); |
| 6750 | FreeWild(old_arg_count, old_arg_files); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6751 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6752 | } |
| 6753 | p_su = save_p_su; |
| 6754 | } |
| 6755 | #endif |
| 6756 | |
| 6757 | /* |
| 6758 | * Set the argument list for the current window. |
| 6759 | * Takes over the allocated files[] and the allocated fnames in it. |
| 6760 | */ |
| 6761 | void |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6762 | alist_set(al, count, files, use_curbuf, fnum_list, fnum_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6763 | alist_T *al; |
| 6764 | int count; |
| 6765 | char_u **files; |
| 6766 | int use_curbuf; |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6767 | int *fnum_list; |
| 6768 | int fnum_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6769 | { |
| 6770 | int i; |
| 6771 | |
| 6772 | alist_clear(al); |
| 6773 | if (ga_grow(&al->al_ga, count) == OK) |
| 6774 | { |
| 6775 | for (i = 0; i < count; ++i) |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 6776 | { |
| 6777 | if (got_int) |
| 6778 | { |
| 6779 | /* When adding many buffers this can take a long time. Allow |
| 6780 | * interrupting here. */ |
| 6781 | while (i < count) |
| 6782 | vim_free(files[i++]); |
| 6783 | break; |
| 6784 | } |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 6785 | |
| 6786 | /* May set buffer name of a buffer previously used for the |
| 6787 | * argument list, so that it's re-used by alist_add. */ |
| 6788 | if (fnum_list != NULL && i < fnum_len) |
| 6789 | buf_set_name(fnum_list[i], files[i]); |
| 6790 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6791 | alist_add(al, files[i], use_curbuf ? 2 : 1); |
Bram Moolenaar | 15d0a8c | 2004-09-06 17:44:46 +0000 | [diff] [blame] | 6792 | ui_breakcheck(); |
| 6793 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6794 | vim_free(files); |
| 6795 | } |
| 6796 | else |
| 6797 | FreeWild(count, files); |
| 6798 | #ifdef FEAT_WINDOWS |
| 6799 | if (al == &global_alist) |
| 6800 | #endif |
| 6801 | arg_had_last = FALSE; |
| 6802 | } |
| 6803 | |
| 6804 | /* |
| 6805 | * Add file "fname" to argument list "al". |
| 6806 | * "fname" must have been allocated and "al" must have been checked for room. |
| 6807 | */ |
| 6808 | void |
| 6809 | alist_add(al, fname, set_fnum) |
| 6810 | alist_T *al; |
| 6811 | char_u *fname; |
| 6812 | int set_fnum; /* 1: set buffer number; 2: re-use curbuf */ |
| 6813 | { |
| 6814 | if (fname == NULL) /* don't add NULL file names */ |
| 6815 | return; |
| 6816 | #ifdef BACKSLASH_IN_FILENAME |
| 6817 | slash_adjust(fname); |
| 6818 | #endif |
| 6819 | AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname; |
| 6820 | if (set_fnum > 0) |
| 6821 | AARGLIST(al)[al->al_ga.ga_len].ae_fnum = |
| 6822 | buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0)); |
| 6823 | ++al->al_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 6827 | /* |
| 6828 | * Adjust slashes in file names. Called after 'shellslash' was set. |
| 6829 | */ |
| 6830 | void |
| 6831 | alist_slash_adjust() |
| 6832 | { |
| 6833 | int i; |
| 6834 | # ifdef FEAT_WINDOWS |
| 6835 | win_T *wp; |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6836 | tabpage_T *tp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6837 | # endif |
| 6838 | |
| 6839 | for (i = 0; i < GARGCOUNT; ++i) |
| 6840 | if (GARGLIST[i].ae_fname != NULL) |
| 6841 | slash_adjust(GARGLIST[i].ae_fname); |
| 6842 | # ifdef FEAT_WINDOWS |
Bram Moolenaar | f740b29 | 2006-02-16 22:11:02 +0000 | [diff] [blame] | 6843 | FOR_ALL_TAB_WINDOWS(tp, wp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6844 | if (wp->w_alist != &global_alist) |
| 6845 | for (i = 0; i < WARGCOUNT(wp); ++i) |
| 6846 | if (WARGLIST(wp)[i].ae_fname != NULL) |
| 6847 | slash_adjust(WARGLIST(wp)[i].ae_fname); |
| 6848 | # endif |
| 6849 | } |
| 6850 | #endif |
| 6851 | |
| 6852 | /* |
| 6853 | * ":preserve". |
| 6854 | */ |
| 6855 | /*ARGSUSED*/ |
| 6856 | static void |
| 6857 | ex_preserve(eap) |
| 6858 | exarg_T *eap; |
| 6859 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 6860 | curbuf->b_flags |= BF_PRESERVED; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6861 | ml_preserve(curbuf, TRUE); |
| 6862 | } |
| 6863 | |
| 6864 | /* |
| 6865 | * ":recover". |
| 6866 | */ |
| 6867 | static void |
| 6868 | ex_recover(eap) |
| 6869 | exarg_T *eap; |
| 6870 | { |
| 6871 | /* Set recoverymode right away to avoid the ATTENTION prompt. */ |
| 6872 | recoverymode = TRUE; |
| 6873 | if (!check_changed(curbuf, p_awa, TRUE, eap->forceit, FALSE) |
| 6874 | && (*eap->arg == NUL |
| 6875 | || setfname(curbuf, eap->arg, NULL, TRUE) == OK)) |
| 6876 | ml_recover(); |
| 6877 | recoverymode = FALSE; |
| 6878 | } |
| 6879 | |
| 6880 | /* |
| 6881 | * Command modifier used in a wrong way. |
| 6882 | */ |
| 6883 | static void |
| 6884 | ex_wrongmodifier(eap) |
| 6885 | exarg_T *eap; |
| 6886 | { |
| 6887 | eap->errmsg = e_invcmd; |
| 6888 | } |
| 6889 | |
| 6890 | #ifdef FEAT_WINDOWS |
| 6891 | /* |
| 6892 | * :sview [+command] file split window with new file, read-only |
| 6893 | * :split [[+command] file] split window with current or new file |
| 6894 | * :vsplit [[+command] file] split window vertically with current or new file |
| 6895 | * :new [[+command] file] split window with no or new file |
| 6896 | * :vnew [[+command] file] split vertically window with no or new file |
| 6897 | * :sfind [+command] file split window with file in 'path' |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6898 | * |
| 6899 | * :tabedit open new Tab page with empty window |
| 6900 | * :tabedit [+command] file open new Tab page and edit "file" |
| 6901 | * :tabnew [[+command] file] just like :tabedit |
| 6902 | * :tabfind [+command] file open new Tab page and find "file" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6903 | */ |
| 6904 | void |
| 6905 | ex_splitview(eap) |
| 6906 | exarg_T *eap; |
| 6907 | { |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6908 | win_T *old_curwin = curwin; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6909 | # if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6910 | char_u *fname = NULL; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6911 | # endif |
| 6912 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6913 | int browse_flag = cmdmod.browse; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6914 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6915 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6916 | # ifndef FEAT_VERTSPLIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6917 | if (eap->cmdidx == CMD_vsplit || eap->cmdidx == CMD_vnew) |
| 6918 | { |
| 6919 | ex_ni(eap); |
| 6920 | return; |
| 6921 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6922 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6923 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6924 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6925 | need_mouse_correct = TRUE; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6926 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6927 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6928 | # ifdef FEAT_QUICKFIX |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6929 | /* A ":split" in the quickfix window works like ":new". Don't want two |
| 6930 | * quickfix windows. */ |
| 6931 | if (bt_quickfix(curbuf)) |
| 6932 | { |
| 6933 | if (eap->cmdidx == CMD_split) |
| 6934 | eap->cmdidx = CMD_new; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6935 | # ifdef FEAT_VERTSPLIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6936 | if (eap->cmdidx == CMD_vsplit) |
| 6937 | eap->cmdidx = CMD_vnew; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6938 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6939 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6940 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6941 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6942 | # ifdef FEAT_SEARCHPATH |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6943 | if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6944 | { |
| 6945 | fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), |
| 6946 | FNAME_MESS, TRUE, curbuf->b_ffname); |
| 6947 | if (fname == NULL) |
| 6948 | goto theend; |
| 6949 | eap->arg = fname; |
| 6950 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6951 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6952 | else |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6953 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6954 | # endif |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6955 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6956 | if (cmdmod.browse |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6957 | # ifdef FEAT_VERTSPLIT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6958 | && eap->cmdidx != CMD_vnew |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6959 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6960 | && eap->cmdidx != CMD_new) |
| 6961 | { |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6962 | if ( |
| 6963 | # ifdef FEAT_GUI |
| 6964 | !gui.in_use && |
| 6965 | # endif |
| 6966 | au_has_group((char_u *)"FileExplorer")) |
| 6967 | { |
| 6968 | /* No browsing supported but we do have the file explorer: |
| 6969 | * Edit the directory. */ |
| 6970 | if (*eap->arg == NUL || !mch_isdir(eap->arg)) |
| 6971 | eap->arg = (char_u *)"."; |
| 6972 | } |
| 6973 | else |
| 6974 | { |
| 6975 | fname = do_browse(0, (char_u *)_("Edit File in new window"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6976 | eap->arg, NULL, NULL, NULL, curbuf); |
Bram Moolenaar | 1d94f9b | 2005-08-04 21:29:45 +0000 | [diff] [blame] | 6977 | if (fname == NULL) |
| 6978 | goto theend; |
| 6979 | eap->arg = fname; |
| 6980 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6981 | } |
| 6982 | cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */ |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 6983 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6984 | |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6985 | /* |
| 6986 | * Either open new tab page or split the window. |
| 6987 | */ |
| 6988 | if (eap->cmdidx == CMD_tabedit |
| 6989 | || eap->cmdidx == CMD_tabfind |
| 6990 | || eap->cmdidx == CMD_tabnew) |
| 6991 | { |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 6992 | if (win_new_tabpage(cmdmod.tab) != FAIL) |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 6993 | { |
| 6994 | do_exedit(eap, NULL); |
| 6995 | |
| 6996 | /* set the alternate buffer for the window we came from */ |
| 6997 | if (curwin != old_curwin |
| 6998 | && win_valid(old_curwin) |
| 6999 | && old_curwin->w_buffer != curbuf |
| 7000 | && !cmdmod.keepalt) |
| 7001 | old_curwin->w_alt_fnum = curbuf->b_fnum; |
| 7002 | } |
| 7003 | } |
| 7004 | else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7005 | *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL) |
| 7006 | { |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7007 | # ifdef FEAT_SCROLLBIND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7008 | /* Reset 'scrollbind' when editing another file, but keep it when |
| 7009 | * doing ":split" without arguments. */ |
| 7010 | if (*eap->arg != NUL |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7011 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7012 | || cmdmod.browse |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7013 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7014 | ) |
| 7015 | curwin->w_p_scb = FALSE; |
| 7016 | else |
| 7017 | do_check_scrollbind(FALSE); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7018 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7019 | do_exedit(eap, old_curwin); |
| 7020 | } |
| 7021 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7022 | # ifdef FEAT_BROWSE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7023 | cmdmod.browse = browse_flag; |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7024 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7025 | |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7026 | # if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7027 | theend: |
| 7028 | vim_free(fname); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7029 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7030 | } |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7031 | |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 7032 | #if defined(FEAT_MOUSE) || defined(PROTO) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7033 | /* |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 7034 | * Open a new tab page. |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7035 | */ |
| 7036 | void |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 7037 | tabpage_new() |
| 7038 | { |
| 7039 | exarg_T ea; |
| 7040 | |
| 7041 | vim_memset(&ea, 0, sizeof(ea)); |
| 7042 | ea.cmdidx = CMD_tabnew; |
| 7043 | ea.cmd = (char_u *)"tabn"; |
| 7044 | ea.arg = (char_u *)""; |
| 7045 | ex_splitview(&ea); |
| 7046 | } |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 7047 | #endif |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 7048 | |
| 7049 | /* |
| 7050 | * :tabnext command |
| 7051 | */ |
| 7052 | static void |
| 7053 | ex_tabnext(eap) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7054 | exarg_T *eap; |
| 7055 | { |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 7056 | switch (eap->cmdidx) |
| 7057 | { |
| 7058 | case CMD_tabfirst: |
| 7059 | case CMD_tabrewind: |
| 7060 | goto_tabpage(1); |
| 7061 | break; |
| 7062 | case CMD_tablast: |
| 7063 | goto_tabpage(9999); |
| 7064 | break; |
| 7065 | case CMD_tabprevious: |
| 7066 | case CMD_tabNext: |
| 7067 | goto_tabpage(eap->addr_count == 0 ? -1 : -(int)eap->line2); |
| 7068 | break; |
| 7069 | default: /* CMD_tabnext */ |
| 7070 | goto_tabpage(eap->addr_count == 0 ? 0 : (int)eap->line2); |
| 7071 | break; |
| 7072 | } |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 7073 | } |
| 7074 | |
| 7075 | /* |
| 7076 | * :tabmove command |
| 7077 | */ |
| 7078 | static void |
| 7079 | ex_tabmove(eap) |
| 7080 | exarg_T *eap; |
| 7081 | { |
| 7082 | tabpage_move(eap->addr_count == 0 ? 9999 : (int)eap->line2); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
| 7085 | /* |
| 7086 | * :tabs command: List tabs and their contents. |
| 7087 | */ |
| 7088 | /*ARGSUSED*/ |
| 7089 | static void |
| 7090 | ex_tabs(eap) |
| 7091 | exarg_T *eap; |
| 7092 | { |
| 7093 | tabpage_T *tp; |
| 7094 | win_T *wp; |
| 7095 | int tabcount = 1; |
| 7096 | |
| 7097 | msg_start(); |
| 7098 | msg_scroll = TRUE; |
| 7099 | for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next) |
| 7100 | { |
| 7101 | msg_putchar('\n'); |
| 7102 | vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++); |
| 7103 | msg_outtrans_attr(IObuff, hl_attr(HLF_T)); |
| 7104 | out_flush(); /* output one line at a time */ |
| 7105 | ui_breakcheck(); |
| 7106 | |
Bram Moolenaar | 030f0df | 2006-02-21 22:02:53 +0000 | [diff] [blame] | 7107 | if (tp == curtab) |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7108 | wp = firstwin; |
| 7109 | else |
| 7110 | wp = tp->tp_firstwin; |
| 7111 | for ( ; wp != NULL && !got_int; wp = wp->w_next) |
| 7112 | { |
Bram Moolenaar | 80a94a5 | 2006-02-23 21:26:58 +0000 | [diff] [blame] | 7113 | msg_putchar('\n'); |
| 7114 | msg_putchar(wp == curwin ? '>' : ' '); |
| 7115 | msg_putchar(' '); |
Bram Moolenaar | 49d7bf1 | 2006-02-17 21:45:41 +0000 | [diff] [blame] | 7116 | msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' '); |
| 7117 | msg_putchar(' '); |
Bram Moolenaar | 1d2ba7f | 2006-02-14 22:29:30 +0000 | [diff] [blame] | 7118 | if (buf_spname(wp->w_buffer) != NULL) |
| 7119 | STRCPY(IObuff, buf_spname(wp->w_buffer)); |
| 7120 | else |
| 7121 | home_replace(wp->w_buffer, wp->w_buffer->b_fname, |
| 7122 | IObuff, IOSIZE, TRUE); |
| 7123 | msg_outtrans(IObuff); |
| 7124 | out_flush(); /* output one line at a time */ |
| 7125 | ui_breakcheck(); |
| 7126 | } |
| 7127 | } |
| 7128 | } |
| 7129 | |
| 7130 | #endif /* FEAT_WINDOWS */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7131 | |
| 7132 | /* |
| 7133 | * ":mode": Set screen mode. |
| 7134 | * If no argument given, just get the screen size and redraw. |
| 7135 | */ |
| 7136 | static void |
| 7137 | ex_mode(eap) |
| 7138 | exarg_T *eap; |
| 7139 | { |
| 7140 | if (*eap->arg == NUL) |
| 7141 | shell_resized(); |
| 7142 | else |
| 7143 | mch_screenmode(eap->arg); |
| 7144 | } |
| 7145 | |
| 7146 | #ifdef FEAT_WINDOWS |
| 7147 | /* |
| 7148 | * ":resize". |
| 7149 | * set, increment or decrement current window height |
| 7150 | */ |
| 7151 | static void |
| 7152 | ex_resize(eap) |
| 7153 | exarg_T *eap; |
| 7154 | { |
| 7155 | int n; |
| 7156 | win_T *wp = curwin; |
| 7157 | |
| 7158 | if (eap->addr_count > 0) |
| 7159 | { |
| 7160 | n = eap->line2; |
| 7161 | for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next) |
| 7162 | ; |
| 7163 | } |
| 7164 | |
| 7165 | #ifdef FEAT_GUI |
| 7166 | need_mouse_correct = TRUE; |
| 7167 | #endif |
| 7168 | n = atol((char *)eap->arg); |
| 7169 | #ifdef FEAT_VERTSPLIT |
| 7170 | if (cmdmod.split & WSP_VERT) |
| 7171 | { |
| 7172 | if (*eap->arg == '-' || *eap->arg == '+') |
| 7173 | n += W_WIDTH(curwin); |
| 7174 | else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */ |
| 7175 | n = 9999; |
| 7176 | win_setwidth_win((int)n, wp); |
| 7177 | } |
| 7178 | else |
| 7179 | #endif |
| 7180 | { |
| 7181 | if (*eap->arg == '-' || *eap->arg == '+') |
| 7182 | n += curwin->w_height; |
| 7183 | else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */ |
| 7184 | n = 9999; |
| 7185 | win_setheight_win((int)n, wp); |
| 7186 | } |
| 7187 | } |
| 7188 | #endif |
| 7189 | |
| 7190 | /* |
| 7191 | * ":find [+command] <file>" command. |
| 7192 | */ |
| 7193 | static void |
| 7194 | ex_find(eap) |
| 7195 | exarg_T *eap; |
| 7196 | { |
| 7197 | #ifdef FEAT_SEARCHPATH |
| 7198 | char_u *fname; |
| 7199 | int count; |
| 7200 | |
| 7201 | fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS, |
| 7202 | TRUE, curbuf->b_ffname); |
| 7203 | if (eap->addr_count > 0) |
| 7204 | { |
| 7205 | /* Repeat finding the file "count" times. This matters when it |
| 7206 | * appears several times in the path. */ |
| 7207 | count = eap->line2; |
| 7208 | while (fname != NULL && --count > 0) |
| 7209 | { |
| 7210 | vim_free(fname); |
| 7211 | fname = find_file_in_path(NULL, 0, FNAME_MESS, |
| 7212 | FALSE, curbuf->b_ffname); |
| 7213 | } |
| 7214 | } |
| 7215 | |
| 7216 | if (fname != NULL) |
| 7217 | { |
| 7218 | eap->arg = fname; |
| 7219 | #endif |
| 7220 | do_exedit(eap, NULL); |
| 7221 | #ifdef FEAT_SEARCHPATH |
| 7222 | vim_free(fname); |
| 7223 | } |
| 7224 | #endif |
| 7225 | } |
| 7226 | |
| 7227 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7228 | * ":open" simulation: for now just work like ":visual". |
| 7229 | */ |
| 7230 | static void |
| 7231 | ex_open(eap) |
| 7232 | exarg_T *eap; |
| 7233 | { |
| 7234 | regmatch_T regmatch; |
| 7235 | char_u *p; |
| 7236 | |
| 7237 | curwin->w_cursor.lnum = eap->line2; |
| 7238 | beginline(BL_SOL | BL_FIX); |
| 7239 | if (*eap->arg == '/') |
| 7240 | { |
| 7241 | /* ":open /pattern/": put cursor in column found with pattern */ |
| 7242 | ++eap->arg; |
| 7243 | p = skip_regexp(eap->arg, '/', p_magic, NULL); |
| 7244 | *p = NUL; |
| 7245 | regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0); |
| 7246 | if (regmatch.regprog != NULL) |
| 7247 | { |
| 7248 | regmatch.rm_ic = p_ic; |
| 7249 | p = ml_get_curline(); |
| 7250 | if (vim_regexec(®match, p, (colnr_T)0)) |
| 7251 | curwin->w_cursor.col = regmatch.startp[0] - p; |
| 7252 | else |
| 7253 | EMSG(_(e_nomatch)); |
| 7254 | vim_free(regmatch.regprog); |
| 7255 | } |
| 7256 | /* Move to the NUL, ignore any other arguments. */ |
| 7257 | eap->arg += STRLEN(eap->arg); |
| 7258 | } |
| 7259 | check_cursor(); |
| 7260 | |
| 7261 | eap->cmdidx = CMD_visual; |
| 7262 | do_exedit(eap, NULL); |
| 7263 | } |
| 7264 | |
| 7265 | /* |
| 7266 | * ":edit", ":badd", ":visual". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7267 | */ |
| 7268 | static void |
| 7269 | ex_edit(eap) |
| 7270 | exarg_T *eap; |
| 7271 | { |
| 7272 | do_exedit(eap, NULL); |
| 7273 | } |
| 7274 | |
| 7275 | /* |
| 7276 | * ":edit <file>" command and alikes. |
| 7277 | */ |
| 7278 | /*ARGSUSED*/ |
| 7279 | void |
| 7280 | do_exedit(eap, old_curwin) |
| 7281 | exarg_T *eap; |
| 7282 | win_T *old_curwin; /* curwin before doing a split or NULL */ |
| 7283 | { |
| 7284 | int n; |
| 7285 | #ifdef FEAT_WINDOWS |
| 7286 | int need_hide; |
| 7287 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7288 | int exmode_was = exmode_active; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7289 | |
| 7290 | /* |
| 7291 | * ":vi" command ends Ex mode. |
| 7292 | */ |
| 7293 | if (exmode_active && (eap->cmdidx == CMD_visual |
| 7294 | || eap->cmdidx == CMD_view)) |
| 7295 | { |
| 7296 | exmode_active = FALSE; |
| 7297 | if (*eap->arg == NUL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7298 | { |
| 7299 | /* Special case: ":global/pat/visual\NLvi-commands" */ |
| 7300 | if (global_busy) |
| 7301 | { |
| 7302 | int rd = RedrawingDisabled; |
| 7303 | int nwr = no_wait_return; |
| 7304 | int ms = msg_scroll; |
| 7305 | #ifdef FEAT_GUI |
| 7306 | int he = hold_gui_events; |
| 7307 | #endif |
| 7308 | |
| 7309 | if (eap->nextcmd != NULL) |
| 7310 | { |
| 7311 | stuffReadbuff(eap->nextcmd); |
| 7312 | eap->nextcmd = NULL; |
| 7313 | } |
| 7314 | |
| 7315 | if (exmode_was != EXMODE_VIM) |
| 7316 | settmode(TMODE_RAW); |
| 7317 | RedrawingDisabled = 0; |
| 7318 | no_wait_return = 0; |
| 7319 | need_wait_return = FALSE; |
| 7320 | msg_scroll = 0; |
| 7321 | #ifdef FEAT_GUI |
| 7322 | hold_gui_events = 0; |
| 7323 | #endif |
| 7324 | must_redraw = CLEAR; |
| 7325 | |
| 7326 | main_loop(FALSE, TRUE); |
| 7327 | |
| 7328 | RedrawingDisabled = rd; |
| 7329 | no_wait_return = nwr; |
| 7330 | msg_scroll = ms; |
| 7331 | #ifdef FEAT_GUI |
| 7332 | hold_gui_events = he; |
| 7333 | #endif |
| 7334 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7335 | return; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7336 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7337 | } |
| 7338 | |
| 7339 | if ((eap->cmdidx == CMD_new |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 7340 | || eap->cmdidx == CMD_tabnew |
| 7341 | || eap->cmdidx == CMD_tabedit |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7342 | #ifdef FEAT_VERTSPLIT |
| 7343 | || eap->cmdidx == CMD_vnew |
| 7344 | #endif |
| 7345 | ) && *eap->arg == NUL) |
| 7346 | { |
Bram Moolenaar | 2a0449d | 2006-02-20 21:27:21 +0000 | [diff] [blame] | 7347 | /* ":new" or ":tabnew" without argument: edit an new empty buffer */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7348 | setpcmark(); |
| 7349 | (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE, |
| 7350 | ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0)); |
| 7351 | } |
| 7352 | else if ((eap->cmdidx != CMD_split |
| 7353 | #ifdef FEAT_VERTSPLIT |
| 7354 | && eap->cmdidx != CMD_vsplit |
| 7355 | #endif |
| 7356 | ) |
| 7357 | || *eap->arg != NUL |
| 7358 | #ifdef FEAT_BROWSE |
| 7359 | || cmdmod.browse |
| 7360 | #endif |
| 7361 | ) |
| 7362 | { |
| 7363 | n = readonlymode; |
| 7364 | if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview) |
| 7365 | readonlymode = TRUE; |
| 7366 | else if (eap->cmdidx == CMD_enew) |
| 7367 | readonlymode = FALSE; /* 'readonly' doesn't make sense in an |
| 7368 | empty buffer */ |
| 7369 | setpcmark(); |
| 7370 | if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg), |
| 7371 | NULL, eap, |
| 7372 | /* ":edit" goes to first line if Vi compatible */ |
| 7373 | (*eap->arg == NUL && eap->do_ecmd_lnum == 0 |
| 7374 | && vim_strchr(p_cpo, CPO_GOTO1) != NULL) |
| 7375 | ? ECMD_ONE : eap->do_ecmd_lnum, |
| 7376 | (P_HID(curbuf) ? ECMD_HIDE : 0) |
| 7377 | + (eap->forceit ? ECMD_FORCEIT : 0) |
| 7378 | #ifdef FEAT_LISTCMDS |
| 7379 | + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 ) |
| 7380 | #endif |
| 7381 | ) == FAIL) |
| 7382 | { |
| 7383 | /* Editing the file failed. If the window was split, close it. */ |
| 7384 | #ifdef FEAT_WINDOWS |
| 7385 | if (old_curwin != NULL) |
| 7386 | { |
| 7387 | need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1); |
| 7388 | if (!need_hide || P_HID(curbuf)) |
| 7389 | { |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 7390 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7391 | cleanup_T cs; |
| 7392 | |
| 7393 | /* Reset the error/interrupt/exception state here so that |
| 7394 | * aborting() returns FALSE when closing a window. */ |
| 7395 | enter_cleanup(&cs); |
| 7396 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7397 | # ifdef FEAT_GUI |
| 7398 | need_mouse_correct = TRUE; |
| 7399 | # endif |
| 7400 | win_close(curwin, !need_hide && !P_HID(curbuf)); |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 7401 | |
| 7402 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7403 | /* Restore the error/interrupt/exception state if not |
| 7404 | * discarded by a new aborting error, interrupt, or |
| 7405 | * uncaught exception. */ |
| 7406 | leave_cleanup(&cs); |
| 7407 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7408 | } |
| 7409 | } |
| 7410 | #endif |
| 7411 | } |
| 7412 | else if (readonlymode && curbuf->b_nwindows == 1) |
| 7413 | { |
| 7414 | /* When editing an already visited buffer, 'readonly' won't be set |
| 7415 | * but the previous value is kept. With ":view" and ":sview" we |
| 7416 | * want the file to be readonly, except when another window is |
| 7417 | * editing the same buffer. */ |
| 7418 | curbuf->b_p_ro = TRUE; |
| 7419 | } |
| 7420 | readonlymode = n; |
| 7421 | } |
| 7422 | else |
| 7423 | { |
| 7424 | if (eap->do_ecmd_cmd != NULL) |
| 7425 | do_cmdline_cmd(eap->do_ecmd_cmd); |
| 7426 | #ifdef FEAT_TITLE |
| 7427 | n = curwin->w_arg_idx_invalid; |
| 7428 | #endif |
| 7429 | check_arg_idx(curwin); |
| 7430 | #ifdef FEAT_TITLE |
| 7431 | if (n != curwin->w_arg_idx_invalid) |
| 7432 | maketitle(); |
| 7433 | #endif |
| 7434 | } |
| 7435 | |
| 7436 | #ifdef FEAT_WINDOWS |
| 7437 | /* |
| 7438 | * if ":split file" worked, set alternate file name in old window to new |
| 7439 | * file |
| 7440 | */ |
| 7441 | if (old_curwin != NULL |
| 7442 | && *eap->arg != NUL |
| 7443 | && curwin != old_curwin |
| 7444 | && win_valid(old_curwin) |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 7445 | && old_curwin->w_buffer != curbuf |
| 7446 | && !cmdmod.keepalt) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7447 | old_curwin->w_alt_fnum = curbuf->b_fnum; |
| 7448 | #endif |
| 7449 | |
| 7450 | ex_no_reprint = TRUE; |
| 7451 | } |
| 7452 | |
| 7453 | #ifndef FEAT_GUI |
| 7454 | /* |
| 7455 | * ":gui" and ":gvim" when there is no GUI. |
| 7456 | */ |
| 7457 | static void |
| 7458 | ex_nogui(eap) |
| 7459 | exarg_T *eap; |
| 7460 | { |
| 7461 | eap->errmsg = e_nogvim; |
| 7462 | } |
| 7463 | #endif |
| 7464 | |
| 7465 | #if defined(FEAT_GUI_W32) && defined(FEAT_MENU) && defined(FEAT_TEAROFF) |
| 7466 | static void |
| 7467 | ex_tearoff(eap) |
| 7468 | exarg_T *eap; |
| 7469 | { |
| 7470 | gui_make_tearoff(eap->arg); |
| 7471 | } |
| 7472 | #endif |
| 7473 | |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 7474 | #if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)) && defined(FEAT_MENU) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7475 | static void |
| 7476 | ex_popup(eap) |
| 7477 | exarg_T *eap; |
| 7478 | { |
Bram Moolenaar | 97409f1 | 2005-07-08 22:17:29 +0000 | [diff] [blame] | 7479 | gui_make_popup(eap->arg, eap->forceit); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7480 | } |
| 7481 | #endif |
| 7482 | |
| 7483 | /*ARGSUSED*/ |
| 7484 | static void |
| 7485 | ex_swapname(eap) |
| 7486 | exarg_T *eap; |
| 7487 | { |
| 7488 | if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL) |
| 7489 | MSG(_("No swap file")); |
| 7490 | else |
| 7491 | msg(curbuf->b_ml.ml_mfp->mf_fname); |
| 7492 | } |
| 7493 | |
| 7494 | /* |
| 7495 | * ":syncbind" forces all 'scrollbind' windows to have the same relative |
| 7496 | * offset. |
| 7497 | * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>) |
| 7498 | */ |
| 7499 | /*ARGSUSED*/ |
| 7500 | static void |
| 7501 | ex_syncbind(eap) |
| 7502 | exarg_T *eap; |
| 7503 | { |
| 7504 | #ifdef FEAT_SCROLLBIND |
| 7505 | win_T *wp; |
| 7506 | long topline; |
| 7507 | long y; |
| 7508 | linenr_T old_linenr = curwin->w_cursor.lnum; |
| 7509 | |
| 7510 | setpcmark(); |
| 7511 | |
| 7512 | /* |
| 7513 | * determine max topline |
| 7514 | */ |
| 7515 | if (curwin->w_p_scb) |
| 7516 | { |
| 7517 | topline = curwin->w_topline; |
| 7518 | for (wp = firstwin; wp; wp = wp->w_next) |
| 7519 | { |
| 7520 | if (wp->w_p_scb && wp->w_buffer) |
| 7521 | { |
| 7522 | y = wp->w_buffer->b_ml.ml_line_count - p_so; |
| 7523 | if (topline > y) |
| 7524 | topline = y; |
| 7525 | } |
| 7526 | } |
| 7527 | if (topline < 1) |
| 7528 | topline = 1; |
| 7529 | } |
| 7530 | else |
| 7531 | { |
| 7532 | topline = 1; |
| 7533 | } |
| 7534 | |
| 7535 | |
| 7536 | /* |
| 7537 | * set all scrollbind windows to the same topline |
| 7538 | */ |
| 7539 | wp = curwin; |
| 7540 | for (curwin = firstwin; curwin; curwin = curwin->w_next) |
| 7541 | { |
| 7542 | if (curwin->w_p_scb) |
| 7543 | { |
| 7544 | y = topline - curwin->w_topline; |
| 7545 | if (y > 0) |
| 7546 | scrollup(y, TRUE); |
| 7547 | else |
| 7548 | scrolldown(-y, TRUE); |
| 7549 | curwin->w_scbind_pos = topline; |
| 7550 | redraw_later(VALID); |
| 7551 | cursor_correct(); |
| 7552 | #ifdef FEAT_WINDOWS |
| 7553 | curwin->w_redr_status = TRUE; |
| 7554 | #endif |
| 7555 | } |
| 7556 | } |
| 7557 | curwin = wp; |
| 7558 | if (curwin->w_p_scb) |
| 7559 | { |
| 7560 | did_syncbind = TRUE; |
| 7561 | checkpcmark(); |
| 7562 | if (old_linenr != curwin->w_cursor.lnum) |
| 7563 | { |
| 7564 | char_u ctrl_o[2]; |
| 7565 | |
| 7566 | ctrl_o[0] = Ctrl_O; |
| 7567 | ctrl_o[1] = 0; |
| 7568 | ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE); |
| 7569 | } |
| 7570 | } |
| 7571 | #endif |
| 7572 | } |
| 7573 | |
| 7574 | |
| 7575 | static void |
| 7576 | ex_read(eap) |
| 7577 | exarg_T *eap; |
| 7578 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7579 | int i; |
| 7580 | int empty = (curbuf->b_ml.ml_flags & ML_EMPTY); |
| 7581 | linenr_T lnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7582 | |
| 7583 | if (eap->usefilter) /* :r!cmd */ |
| 7584 | do_bang(1, eap, FALSE, FALSE, TRUE); |
| 7585 | else |
| 7586 | { |
| 7587 | if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL) |
| 7588 | return; |
| 7589 | |
| 7590 | #ifdef FEAT_BROWSE |
| 7591 | if (cmdmod.browse) |
| 7592 | { |
| 7593 | char_u *browseFile; |
| 7594 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 7595 | browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7596 | NULL, NULL, NULL, curbuf); |
| 7597 | if (browseFile != NULL) |
| 7598 | { |
| 7599 | i = readfile(browseFile, NULL, |
| 7600 | eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); |
| 7601 | vim_free(browseFile); |
| 7602 | } |
| 7603 | else |
| 7604 | i = OK; |
| 7605 | } |
| 7606 | else |
| 7607 | #endif |
| 7608 | if (*eap->arg == NUL) |
| 7609 | { |
| 7610 | if (check_fname() == FAIL) /* check for no file name */ |
| 7611 | return; |
| 7612 | i = readfile(curbuf->b_ffname, curbuf->b_fname, |
| 7613 | eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); |
| 7614 | } |
| 7615 | else |
| 7616 | { |
| 7617 | if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL) |
| 7618 | (void)setaltfname(eap->arg, eap->arg, (linenr_T)1); |
| 7619 | i = readfile(eap->arg, NULL, |
| 7620 | eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0); |
| 7621 | |
| 7622 | } |
| 7623 | if (i == FAIL) |
| 7624 | { |
| 7625 | #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7626 | if (!aborting()) |
| 7627 | #endif |
| 7628 | EMSG2(_(e_notopen), eap->arg); |
| 7629 | } |
| 7630 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7631 | { |
| 7632 | if (empty && exmode_active) |
| 7633 | { |
| 7634 | /* Delete the empty line that remains. Historically ex does |
| 7635 | * this but vi doesn't. */ |
| 7636 | if (eap->line2 == 0) |
| 7637 | lnum = curbuf->b_ml.ml_line_count; |
| 7638 | else |
| 7639 | lnum = 1; |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 7640 | if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7641 | { |
| 7642 | ml_delete(lnum, FALSE); |
| 7643 | deleted_lines_mark(lnum, 1L); |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 7644 | if (curwin->w_cursor.lnum > 1 |
| 7645 | && curwin->w_cursor.lnum >= lnum) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7646 | --curwin->w_cursor.lnum; |
| 7647 | } |
| 7648 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7649 | redraw_curbuf_later(VALID); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7650 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7651 | } |
| 7652 | } |
| 7653 | |
Bram Moolenaar | ea40885 | 2005-06-25 22:49:46 +0000 | [diff] [blame] | 7654 | static char_u *prev_dir = NULL; |
| 7655 | |
| 7656 | #if defined(EXITFREE) || defined(PROTO) |
| 7657 | void |
| 7658 | free_cd_dir() |
| 7659 | { |
| 7660 | vim_free(prev_dir); |
| 7661 | } |
| 7662 | #endif |
| 7663 | |
| 7664 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7665 | /* |
| 7666 | * ":cd", ":lcd", ":chdir" and ":lchdir". |
| 7667 | */ |
| 7668 | static void |
| 7669 | ex_cd(eap) |
| 7670 | exarg_T *eap; |
| 7671 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7672 | char_u *new_dir; |
| 7673 | char_u *tofree; |
| 7674 | |
| 7675 | new_dir = eap->arg; |
| 7676 | #if !defined(UNIX) && !defined(VMS) |
| 7677 | /* for non-UNIX ":cd" means: print current directory */ |
| 7678 | if (*new_dir == NUL) |
| 7679 | ex_pwd(NULL); |
| 7680 | else |
| 7681 | #endif |
| 7682 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7683 | if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() |
| 7684 | && !eap->forceit) |
| 7685 | { |
| 7686 | EMSG(_("E747: Cannot change directory, buffer is modifed (add ! to override)")); |
| 7687 | return; |
| 7688 | } |
| 7689 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7690 | /* ":cd -": Change to previous directory */ |
| 7691 | if (STRCMP(new_dir, "-") == 0) |
| 7692 | { |
| 7693 | if (prev_dir == NULL) |
| 7694 | { |
| 7695 | EMSG(_("E186: No previous directory")); |
| 7696 | return; |
| 7697 | } |
| 7698 | new_dir = prev_dir; |
| 7699 | } |
| 7700 | |
| 7701 | /* Save current directory for next ":cd -" */ |
| 7702 | tofree = prev_dir; |
| 7703 | if (mch_dirname(NameBuff, MAXPATHL) == OK) |
| 7704 | prev_dir = vim_strsave(NameBuff); |
| 7705 | else |
| 7706 | prev_dir = NULL; |
| 7707 | |
| 7708 | #if defined(UNIX) || defined(VMS) |
| 7709 | /* for UNIX ":cd" means: go to home directory */ |
| 7710 | if (*new_dir == NUL) |
| 7711 | { |
| 7712 | /* use NameBuff for home directory name */ |
| 7713 | # ifdef VMS |
| 7714 | char_u *p; |
| 7715 | |
| 7716 | p = mch_getenv((char_u *)"SYS$LOGIN"); |
| 7717 | if (p == NULL || *p == NUL) /* empty is the same as not set */ |
| 7718 | NameBuff[0] = NUL; |
| 7719 | else |
Bram Moolenaar | b635633 | 2005-07-18 21:40:44 +0000 | [diff] [blame] | 7720 | vim_strncpy(NameBuff, p, MAXPATHL - 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7721 | # else |
| 7722 | expand_env((char_u *)"$HOME", NameBuff, MAXPATHL); |
| 7723 | # endif |
| 7724 | new_dir = NameBuff; |
| 7725 | } |
| 7726 | #endif |
| 7727 | if (new_dir == NULL || vim_chdir(new_dir)) |
| 7728 | EMSG(_(e_failed)); |
| 7729 | else |
| 7730 | { |
| 7731 | vim_free(curwin->w_localdir); |
| 7732 | if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir) |
| 7733 | { |
| 7734 | /* If still in global directory, need to remember current |
| 7735 | * directory as global directory. */ |
| 7736 | if (globaldir == NULL && prev_dir != NULL) |
| 7737 | globaldir = vim_strsave(prev_dir); |
| 7738 | /* Remember this local directory for the window. */ |
| 7739 | if (mch_dirname(NameBuff, MAXPATHL) == OK) |
| 7740 | curwin->w_localdir = vim_strsave(NameBuff); |
| 7741 | } |
| 7742 | else |
| 7743 | { |
| 7744 | /* We are now in the global directory, no need to remember its |
| 7745 | * name. */ |
| 7746 | vim_free(globaldir); |
| 7747 | globaldir = NULL; |
| 7748 | curwin->w_localdir = NULL; |
| 7749 | } |
| 7750 | |
| 7751 | shorten_fnames(TRUE); |
| 7752 | |
| 7753 | /* Echo the new current directory if the command was typed. */ |
| 7754 | if (KeyTyped) |
| 7755 | ex_pwd(eap); |
| 7756 | } |
| 7757 | vim_free(tofree); |
| 7758 | } |
| 7759 | } |
| 7760 | |
| 7761 | /* |
| 7762 | * ":pwd". |
| 7763 | */ |
| 7764 | /*ARGSUSED*/ |
| 7765 | static void |
| 7766 | ex_pwd(eap) |
| 7767 | exarg_T *eap; |
| 7768 | { |
| 7769 | if (mch_dirname(NameBuff, MAXPATHL) == OK) |
| 7770 | { |
| 7771 | #ifdef BACKSLASH_IN_FILENAME |
| 7772 | slash_adjust(NameBuff); |
| 7773 | #endif |
| 7774 | msg(NameBuff); |
| 7775 | } |
| 7776 | else |
| 7777 | EMSG(_("E187: Unknown")); |
| 7778 | } |
| 7779 | |
| 7780 | /* |
| 7781 | * ":=". |
| 7782 | */ |
| 7783 | static void |
| 7784 | ex_equal(eap) |
| 7785 | exarg_T *eap; |
| 7786 | { |
| 7787 | smsg((char_u *)"%ld", (long)eap->line2); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 7788 | ex_may_print(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7789 | } |
| 7790 | |
| 7791 | static void |
| 7792 | ex_sleep(eap) |
| 7793 | exarg_T *eap; |
| 7794 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7795 | int n; |
| 7796 | long len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7797 | |
| 7798 | if (cursor_valid()) |
| 7799 | { |
| 7800 | n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled; |
| 7801 | if (n >= 0) |
| 7802 | windgoto((int)n, curwin->w_wcol); |
| 7803 | } |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 7804 | |
| 7805 | len = eap->line2; |
| 7806 | switch (*eap->arg) |
| 7807 | { |
| 7808 | case 'm': break; |
| 7809 | case NUL: len *= 1000L; break; |
| 7810 | default: EMSG2(_(e_invarg2), eap->arg); return; |
| 7811 | } |
| 7812 | do_sleep(len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | /* |
| 7816 | * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second. |
| 7817 | */ |
| 7818 | void |
| 7819 | do_sleep(msec) |
| 7820 | long msec; |
| 7821 | { |
| 7822 | long done; |
| 7823 | |
| 7824 | cursor_on(); |
| 7825 | out_flush(); |
| 7826 | for (done = 0; !got_int && done < msec; done += 1000L) |
| 7827 | { |
| 7828 | ui_delay(msec - done > 1000L ? 1000L : msec - done, TRUE); |
| 7829 | ui_breakcheck(); |
| 7830 | } |
| 7831 | } |
| 7832 | |
| 7833 | static void |
| 7834 | do_exmap(eap, isabbrev) |
| 7835 | exarg_T *eap; |
| 7836 | int isabbrev; |
| 7837 | { |
| 7838 | int mode; |
| 7839 | char_u *cmdp; |
| 7840 | |
| 7841 | cmdp = eap->cmd; |
| 7842 | mode = get_map_mode(&cmdp, eap->forceit || isabbrev); |
| 7843 | |
| 7844 | switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'), |
| 7845 | eap->arg, mode, isabbrev)) |
| 7846 | { |
| 7847 | case 1: EMSG(_(e_invarg)); |
| 7848 | break; |
| 7849 | case 2: EMSG(isabbrev ? _(e_noabbr) : _(e_nomap)); |
| 7850 | break; |
| 7851 | } |
| 7852 | } |
| 7853 | |
| 7854 | /* |
| 7855 | * ":winsize" command (obsolete). |
| 7856 | */ |
| 7857 | static void |
| 7858 | ex_winsize(eap) |
| 7859 | exarg_T *eap; |
| 7860 | { |
| 7861 | int w, h; |
| 7862 | char_u *arg = eap->arg; |
| 7863 | char_u *p; |
| 7864 | |
| 7865 | w = getdigits(&arg); |
| 7866 | arg = skipwhite(arg); |
| 7867 | p = arg; |
| 7868 | h = getdigits(&arg); |
| 7869 | if (*p != NUL && *arg == NUL) |
| 7870 | set_shellsize(w, h, TRUE); |
| 7871 | else |
| 7872 | EMSG(_("E465: :winsize requires two number arguments")); |
| 7873 | } |
| 7874 | |
| 7875 | #ifdef FEAT_WINDOWS |
| 7876 | static void |
| 7877 | ex_wincmd(eap) |
| 7878 | exarg_T *eap; |
| 7879 | { |
| 7880 | int xchar = NUL; |
| 7881 | char_u *p; |
| 7882 | |
| 7883 | if (*eap->arg == 'g' || *eap->arg == Ctrl_G) |
| 7884 | { |
| 7885 | /* CTRL-W g and CTRL-W CTRL-G have an extra command character */ |
| 7886 | if (eap->arg[1] == NUL) |
| 7887 | { |
| 7888 | EMSG(_(e_invarg)); |
| 7889 | return; |
| 7890 | } |
| 7891 | xchar = eap->arg[1]; |
| 7892 | p = eap->arg + 2; |
| 7893 | } |
| 7894 | else |
| 7895 | p = eap->arg + 1; |
| 7896 | |
| 7897 | eap->nextcmd = check_nextcmd(p); |
| 7898 | p = skipwhite(p); |
| 7899 | if (*p != NUL && *p != '"' && eap->nextcmd == NULL) |
| 7900 | EMSG(_(e_invarg)); |
| 7901 | else |
| 7902 | { |
| 7903 | /* Pass flags on for ":vertical wincmd ]". */ |
| 7904 | postponed_split_flags = cmdmod.split; |
| 7905 | do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar); |
| 7906 | postponed_split_flags = 0; |
| 7907 | } |
| 7908 | } |
| 7909 | #endif |
| 7910 | |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7911 | #if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7912 | /* |
| 7913 | * ":winpos". |
| 7914 | */ |
| 7915 | static void |
| 7916 | ex_winpos(eap) |
| 7917 | exarg_T *eap; |
| 7918 | { |
| 7919 | int x, y; |
| 7920 | char_u *arg = eap->arg; |
| 7921 | char_u *p; |
| 7922 | |
| 7923 | if (*arg == NUL) |
| 7924 | { |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7925 | # if defined(FEAT_GUI) || defined(MSWIN) |
| 7926 | # ifdef FEAT_GUI |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7927 | if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL) |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7928 | # else |
| 7929 | if (mch_get_winpos(&x, &y) != FAIL) |
| 7930 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7931 | { |
| 7932 | sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y); |
| 7933 | msg(IObuff); |
| 7934 | } |
| 7935 | else |
| 7936 | # endif |
| 7937 | EMSG(_("E188: Obtaining window position not implemented for this platform")); |
| 7938 | } |
| 7939 | else |
| 7940 | { |
| 7941 | x = getdigits(&arg); |
| 7942 | arg = skipwhite(arg); |
| 7943 | p = arg; |
| 7944 | y = getdigits(&arg); |
| 7945 | if (*p == NUL || *arg != NUL) |
| 7946 | { |
| 7947 | EMSG(_("E466: :winpos requires two number arguments")); |
| 7948 | return; |
| 7949 | } |
| 7950 | # ifdef FEAT_GUI |
| 7951 | if (gui.in_use) |
| 7952 | gui_mch_set_winpos(x, y); |
| 7953 | else if (gui.starting) |
| 7954 | { |
| 7955 | /* Remember the coordinates for when the window is opened. */ |
| 7956 | gui_win_x = x; |
| 7957 | gui_win_y = y; |
| 7958 | } |
| 7959 | # ifdef HAVE_TGETENT |
| 7960 | else |
| 7961 | # endif |
Bram Moolenaar | 843ee41 | 2004-06-30 16:16:41 +0000 | [diff] [blame] | 7962 | # else |
| 7963 | # ifdef MSWIN |
| 7964 | mch_set_winpos(x, y); |
| 7965 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7966 | # endif |
| 7967 | # ifdef HAVE_TGETENT |
| 7968 | if (*T_CWP) |
| 7969 | term_set_winpos(x, y); |
| 7970 | # endif |
| 7971 | } |
| 7972 | } |
| 7973 | #endif |
| 7974 | |
| 7975 | /* |
| 7976 | * Handle command that work like operators: ":delete", ":yank", ":>" and ":<". |
| 7977 | */ |
| 7978 | static void |
| 7979 | ex_operators(eap) |
| 7980 | exarg_T *eap; |
| 7981 | { |
| 7982 | oparg_T oa; |
| 7983 | |
| 7984 | clear_oparg(&oa); |
| 7985 | oa.regname = eap->regname; |
| 7986 | oa.start.lnum = eap->line1; |
| 7987 | oa.end.lnum = eap->line2; |
| 7988 | oa.line_count = eap->line2 - eap->line1 + 1; |
| 7989 | oa.motion_type = MLINE; |
| 7990 | #ifdef FEAT_VIRTUALEDIT |
| 7991 | virtual_op = FALSE; |
| 7992 | #endif |
| 7993 | if (eap->cmdidx != CMD_yank) /* position cursor for undo */ |
| 7994 | { |
| 7995 | setpcmark(); |
| 7996 | curwin->w_cursor.lnum = eap->line1; |
| 7997 | beginline(BL_SOL | BL_FIX); |
| 7998 | } |
| 7999 | |
| 8000 | switch (eap->cmdidx) |
| 8001 | { |
| 8002 | case CMD_delete: |
| 8003 | oa.op_type = OP_DELETE; |
| 8004 | op_delete(&oa); |
| 8005 | break; |
| 8006 | |
| 8007 | case CMD_yank: |
| 8008 | oa.op_type = OP_YANK; |
| 8009 | (void)op_yank(&oa, FALSE, TRUE); |
| 8010 | break; |
| 8011 | |
| 8012 | default: /* CMD_rshift or CMD_lshift */ |
| 8013 | if ((eap->cmdidx == CMD_rshift) |
| 8014 | #ifdef FEAT_RIGHTLEFT |
| 8015 | ^ curwin->w_p_rl |
| 8016 | #endif |
| 8017 | ) |
| 8018 | oa.op_type = OP_RSHIFT; |
| 8019 | else |
| 8020 | oa.op_type = OP_LSHIFT; |
| 8021 | op_shift(&oa, FALSE, eap->amount); |
| 8022 | break; |
| 8023 | } |
| 8024 | #ifdef FEAT_VIRTUALEDIT |
| 8025 | virtual_op = MAYBE; |
| 8026 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8027 | ex_may_print(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8028 | } |
| 8029 | |
| 8030 | /* |
| 8031 | * ":put". |
| 8032 | */ |
| 8033 | static void |
| 8034 | ex_put(eap) |
| 8035 | exarg_T *eap; |
| 8036 | { |
| 8037 | /* ":0put" works like ":1put!". */ |
| 8038 | if (eap->line2 == 0) |
| 8039 | { |
| 8040 | eap->line2 = 1; |
| 8041 | eap->forceit = TRUE; |
| 8042 | } |
| 8043 | curwin->w_cursor.lnum = eap->line2; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8044 | do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L, |
| 8045 | PUT_LINE|PUT_CURSLINE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8046 | } |
| 8047 | |
| 8048 | /* |
| 8049 | * Handle ":copy" and ":move". |
| 8050 | */ |
| 8051 | static void |
| 8052 | ex_copymove(eap) |
| 8053 | exarg_T *eap; |
| 8054 | { |
| 8055 | long n; |
| 8056 | |
| 8057 | n = get_address(&eap->arg, FALSE, FALSE); |
| 8058 | if (eap->arg == NULL) /* error detected */ |
| 8059 | { |
| 8060 | eap->nextcmd = NULL; |
| 8061 | return; |
| 8062 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8063 | get_flags(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8064 | |
| 8065 | /* |
| 8066 | * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n' |
| 8067 | */ |
| 8068 | if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count) |
| 8069 | { |
| 8070 | EMSG(_(e_invaddr)); |
| 8071 | return; |
| 8072 | } |
| 8073 | |
| 8074 | if (eap->cmdidx == CMD_move) |
| 8075 | { |
| 8076 | if (do_move(eap->line1, eap->line2, n) == FAIL) |
| 8077 | return; |
| 8078 | } |
| 8079 | else |
| 8080 | ex_copy(eap->line1, eap->line2, n); |
| 8081 | u_clearline(); |
| 8082 | beginline(BL_SOL | BL_FIX); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8083 | ex_may_print(eap); |
| 8084 | } |
| 8085 | |
| 8086 | /* |
| 8087 | * Print the current line if flags were given to the Ex command. |
| 8088 | */ |
| 8089 | static void |
| 8090 | ex_may_print(eap) |
| 8091 | exarg_T *eap; |
| 8092 | { |
| 8093 | if (eap->flags != 0) |
| 8094 | { |
| 8095 | print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR), |
| 8096 | (eap->flags & EXFLAG_LIST)); |
| 8097 | ex_no_reprint = TRUE; |
| 8098 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8099 | } |
| 8100 | |
| 8101 | /* |
| 8102 | * ":smagic" and ":snomagic". |
| 8103 | */ |
| 8104 | static void |
| 8105 | ex_submagic(eap) |
| 8106 | exarg_T *eap; |
| 8107 | { |
| 8108 | int magic_save = p_magic; |
| 8109 | |
| 8110 | p_magic = (eap->cmdidx == CMD_smagic); |
| 8111 | do_sub(eap); |
| 8112 | p_magic = magic_save; |
| 8113 | } |
| 8114 | |
| 8115 | /* |
| 8116 | * ":join". |
| 8117 | */ |
| 8118 | static void |
| 8119 | ex_join(eap) |
| 8120 | exarg_T *eap; |
| 8121 | { |
| 8122 | curwin->w_cursor.lnum = eap->line1; |
| 8123 | if (eap->line1 == eap->line2) |
| 8124 | { |
| 8125 | if (eap->addr_count >= 2) /* :2,2join does nothing */ |
| 8126 | return; |
| 8127 | if (eap->line2 == curbuf->b_ml.ml_line_count) |
| 8128 | { |
| 8129 | beep_flush(); |
| 8130 | return; |
| 8131 | } |
| 8132 | ++eap->line2; |
| 8133 | } |
| 8134 | do_do_join(eap->line2 - eap->line1 + 1, !eap->forceit); |
| 8135 | beginline(BL_WHITE | BL_FIX); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8136 | ex_may_print(eap); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
| 8139 | /* |
| 8140 | * ":[addr]@r" or ":[addr]*r": execute register |
| 8141 | */ |
| 8142 | static void |
| 8143 | ex_at(eap) |
| 8144 | exarg_T *eap; |
| 8145 | { |
| 8146 | int c; |
| 8147 | |
| 8148 | curwin->w_cursor.lnum = eap->line2; |
| 8149 | |
| 8150 | #ifdef USE_ON_FLY_SCROLL |
| 8151 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 8152 | #endif |
| 8153 | |
| 8154 | /* get the register name. No name means to use the previous one */ |
| 8155 | c = *eap->arg; |
| 8156 | if (c == NUL || (c == '*' && *eap->cmd == '*')) |
| 8157 | c = '@'; |
| 8158 | /* put the register in mapbuf */ |
| 8159 | if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL) == FAIL) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8160 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8161 | beep_flush(); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8162 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8163 | else |
| 8164 | { |
| 8165 | int save_efr = exec_from_reg; |
| 8166 | |
| 8167 | exec_from_reg = TRUE; |
| 8168 | |
| 8169 | /* |
| 8170 | * Execute from the typeahead buffer. |
| 8171 | * Originally this didn't check for the typeahead buffer to be empty, |
| 8172 | * thus could read more Ex commands from stdin. It's not clear why, |
| 8173 | * it is certainly unexpected. |
| 8174 | */ |
| 8175 | while ((!stuff_empty() || typebuf.tb_len > 0) && vpeekc() == ':') |
| 8176 | (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE); |
| 8177 | |
| 8178 | exec_from_reg = save_efr; |
| 8179 | } |
| 8180 | } |
| 8181 | |
| 8182 | /* |
| 8183 | * ":!". |
| 8184 | */ |
| 8185 | static void |
| 8186 | ex_bang(eap) |
| 8187 | exarg_T *eap; |
| 8188 | { |
| 8189 | do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE); |
| 8190 | } |
| 8191 | |
| 8192 | /* |
| 8193 | * ":undo". |
| 8194 | */ |
| 8195 | /*ARGSUSED*/ |
| 8196 | static void |
| 8197 | ex_undo(eap) |
| 8198 | exarg_T *eap; |
| 8199 | { |
Bram Moolenaar | d3667a2 | 2006-03-16 21:35:52 +0000 | [diff] [blame] | 8200 | if (eap->addr_count == 1) /* :undo 123 */ |
| 8201 | undo_time(eap->line2, FALSE, TRUE); |
| 8202 | else |
| 8203 | u_undo(1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
| 8206 | /* |
| 8207 | * ":redo". |
| 8208 | */ |
| 8209 | /*ARGSUSED*/ |
| 8210 | static void |
| 8211 | ex_redo(eap) |
| 8212 | exarg_T *eap; |
| 8213 | { |
| 8214 | u_redo(1); |
| 8215 | } |
| 8216 | |
| 8217 | /* |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 8218 | * ":earlier" and ":later". |
| 8219 | */ |
| 8220 | /*ARGSUSED*/ |
| 8221 | static void |
| 8222 | ex_later(eap) |
| 8223 | exarg_T *eap; |
| 8224 | { |
| 8225 | long count = 0; |
| 8226 | int sec = FALSE; |
| 8227 | char_u *p = eap->arg; |
| 8228 | |
| 8229 | if (*p == NUL) |
| 8230 | count = 1; |
| 8231 | else if (isdigit(*p)) |
| 8232 | { |
| 8233 | count = getdigits(&p); |
| 8234 | switch (*p) |
| 8235 | { |
| 8236 | case 's': ++p; sec = TRUE; break; |
| 8237 | case 'm': ++p; sec = TRUE; count *= 60; break; |
| 8238 | case 'h': ++p; sec = TRUE; count *= 60 * 60; break; |
| 8239 | } |
| 8240 | } |
| 8241 | |
| 8242 | if (*p != NUL) |
| 8243 | EMSG2(_(e_invarg2), eap->arg); |
| 8244 | else |
Bram Moolenaar | d3667a2 | 2006-03-16 21:35:52 +0000 | [diff] [blame] | 8245 | undo_time(eap->cmdidx == CMD_earlier ? -count : count, sec, FALSE); |
Bram Moolenaar | c7d8935 | 2006-03-14 22:53:34 +0000 | [diff] [blame] | 8246 | } |
| 8247 | |
| 8248 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8249 | * ":redir": start/stop redirection. |
| 8250 | */ |
| 8251 | static void |
| 8252 | ex_redir(eap) |
| 8253 | exarg_T *eap; |
| 8254 | { |
| 8255 | char *mode; |
| 8256 | char_u *fname; |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8257 | char_u *arg = eap->arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8258 | |
| 8259 | if (STRICMP(eap->arg, "END") == 0) |
| 8260 | close_redir(); |
| 8261 | else |
| 8262 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8263 | if (*arg == '>') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8264 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8265 | ++arg; |
| 8266 | if (*arg == '>') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8267 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8268 | ++arg; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8269 | mode = "a"; |
| 8270 | } |
| 8271 | else |
| 8272 | mode = "w"; |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8273 | arg = skipwhite(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8274 | |
| 8275 | close_redir(); |
| 8276 | |
| 8277 | /* Expand environment variables and "~/". */ |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8278 | fname = expand_env_save(arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8279 | if (fname == NULL) |
| 8280 | return; |
| 8281 | #ifdef FEAT_BROWSE |
| 8282 | if (cmdmod.browse) |
| 8283 | { |
| 8284 | char_u *browseFile; |
| 8285 | |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 8286 | browseFile = do_browse(BROWSE_SAVE, |
| 8287 | (char_u *)_("Save Redirection"), |
| 8288 | fname, NULL, NULL, BROWSE_FILTER_ALL_FILES, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8289 | if (browseFile == NULL) |
| 8290 | return; /* operation cancelled */ |
| 8291 | vim_free(fname); |
| 8292 | fname = browseFile; |
| 8293 | eap->forceit = TRUE; /* since dialog already asked */ |
| 8294 | } |
| 8295 | #endif |
| 8296 | |
| 8297 | redir_fd = open_exfile(fname, eap->forceit, mode); |
| 8298 | vim_free(fname); |
| 8299 | } |
| 8300 | #ifdef FEAT_EVAL |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8301 | else if (*arg == '@') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8302 | { |
| 8303 | /* redirect to a register a-z (resp. A-Z for appending) */ |
| 8304 | close_redir(); |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8305 | ++arg; |
| 8306 | if (ASCII_ISALPHA(*arg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8307 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8308 | || *arg == '*' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8309 | # endif |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8310 | || *arg == '"') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8311 | { |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8312 | redir_reg = *arg++; |
Bram Moolenaar | d9d3058 | 2005-05-18 22:10:28 +0000 | [diff] [blame] | 8313 | if (*arg == '>' && arg[1] == '>') |
| 8314 | arg += 2; |
Bram Moolenaar | 2c29bee | 2005-06-01 21:46:07 +0000 | [diff] [blame] | 8315 | else if ((*arg == NUL || (*arg == '>' && arg[1] == NUL)) && |
| 8316 | (islower(redir_reg) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8317 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8318 | || redir_reg == '*' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8319 | # endif |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8320 | || redir_reg == '"')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8321 | { |
Bram Moolenaar | 2c29bee | 2005-06-01 21:46:07 +0000 | [diff] [blame] | 8322 | if (*arg == '>') |
| 8323 | arg++; |
| 8324 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8325 | /* make register empty */ |
| 8326 | write_reg_contents(redir_reg, (char_u *)"", -1, FALSE); |
| 8327 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8328 | } |
| 8329 | if (*arg != NUL) |
| 8330 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8331 | redir_reg = 0; |
Bram Moolenaar | d9d3058 | 2005-05-18 22:10:28 +0000 | [diff] [blame] | 8332 | EMSG2(_(e_invarg2), eap->arg); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8333 | } |
| 8334 | } |
| 8335 | else if (*arg == '=' && arg[1] == '>') |
| 8336 | { |
| 8337 | int append; |
| 8338 | |
| 8339 | /* redirect to a variable */ |
| 8340 | close_redir(); |
| 8341 | arg += 2; |
| 8342 | |
| 8343 | if (*arg == '>') |
| 8344 | { |
| 8345 | ++arg; |
| 8346 | append = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8347 | } |
| 8348 | else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8349 | append = FALSE; |
| 8350 | |
| 8351 | if (var_redir_start(skipwhite(arg), append) == OK) |
| 8352 | redir_vname = 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8353 | } |
| 8354 | #endif |
| 8355 | |
| 8356 | /* TODO: redirect to a buffer */ |
| 8357 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8358 | else |
Bram Moolenaar | ca47299 | 2005-01-21 11:46:23 +0000 | [diff] [blame] | 8359 | EMSG2(_(e_invarg2), eap->arg); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8360 | } |
| 8361 | } |
| 8362 | |
| 8363 | /* |
| 8364 | * ":redraw": force redraw |
| 8365 | */ |
| 8366 | static void |
| 8367 | ex_redraw(eap) |
| 8368 | exarg_T *eap; |
| 8369 | { |
| 8370 | int r = RedrawingDisabled; |
| 8371 | int p = p_lz; |
| 8372 | |
| 8373 | RedrawingDisabled = 0; |
| 8374 | p_lz = FALSE; |
| 8375 | update_topline(); |
| 8376 | update_screen(eap->forceit ? CLEAR : |
| 8377 | #ifdef FEAT_VISUAL |
| 8378 | VIsual_active ? INVERTED : |
| 8379 | #endif |
| 8380 | 0); |
| 8381 | #ifdef FEAT_TITLE |
| 8382 | if (need_maketitle) |
| 8383 | maketitle(); |
| 8384 | #endif |
| 8385 | RedrawingDisabled = r; |
| 8386 | p_lz = p; |
| 8387 | |
| 8388 | /* Reset msg_didout, so that a message that's there is overwritten. */ |
| 8389 | msg_didout = FALSE; |
| 8390 | msg_col = 0; |
| 8391 | |
| 8392 | out_flush(); |
| 8393 | } |
| 8394 | |
| 8395 | /* |
| 8396 | * ":redrawstatus": force redraw of status line(s) |
| 8397 | */ |
| 8398 | /*ARGSUSED*/ |
| 8399 | static void |
| 8400 | ex_redrawstatus(eap) |
| 8401 | exarg_T *eap; |
| 8402 | { |
| 8403 | #if defined(FEAT_WINDOWS) |
| 8404 | int r = RedrawingDisabled; |
| 8405 | int p = p_lz; |
| 8406 | |
| 8407 | RedrawingDisabled = 0; |
| 8408 | p_lz = FALSE; |
| 8409 | if (eap->forceit) |
| 8410 | status_redraw_all(); |
| 8411 | else |
| 8412 | status_redraw_curbuf(); |
| 8413 | update_screen( |
| 8414 | # ifdef FEAT_VISUAL |
| 8415 | VIsual_active ? INVERTED : |
| 8416 | # endif |
| 8417 | 0); |
| 8418 | RedrawingDisabled = r; |
| 8419 | p_lz = p; |
| 8420 | out_flush(); |
| 8421 | #endif |
| 8422 | } |
| 8423 | |
| 8424 | static void |
| 8425 | close_redir() |
| 8426 | { |
| 8427 | if (redir_fd != NULL) |
| 8428 | { |
| 8429 | fclose(redir_fd); |
| 8430 | redir_fd = NULL; |
| 8431 | } |
| 8432 | #ifdef FEAT_EVAL |
| 8433 | redir_reg = 0; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8434 | if (redir_vname) |
| 8435 | { |
| 8436 | var_redir_stop(); |
| 8437 | redir_vname = 0; |
| 8438 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8439 | #endif |
| 8440 | } |
| 8441 | |
| 8442 | #if defined(FEAT_SESSION) && defined(USE_CRNL) |
| 8443 | # define MKSESSION_NL |
| 8444 | static int mksession_nl = FALSE; /* use NL only in put_eol() */ |
| 8445 | #endif |
| 8446 | |
| 8447 | /* |
| 8448 | * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession". |
| 8449 | */ |
| 8450 | static void |
| 8451 | ex_mkrc(eap) |
| 8452 | exarg_T *eap; |
| 8453 | { |
| 8454 | FILE *fd; |
| 8455 | int failed = FALSE; |
| 8456 | char_u *fname; |
| 8457 | #ifdef FEAT_BROWSE |
| 8458 | char_u *browseFile = NULL; |
| 8459 | #endif |
| 8460 | #ifdef FEAT_SESSION |
| 8461 | int view_session = FALSE; |
| 8462 | int using_vdir = FALSE; /* using 'viewdir'? */ |
| 8463 | char_u *viewFile = NULL; |
| 8464 | unsigned *flagp; |
| 8465 | #endif |
| 8466 | |
| 8467 | if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview) |
| 8468 | { |
| 8469 | #ifdef FEAT_SESSION |
| 8470 | view_session = TRUE; |
| 8471 | #else |
| 8472 | ex_ni(eap); |
| 8473 | return; |
| 8474 | #endif |
| 8475 | } |
| 8476 | |
| 8477 | #ifdef FEAT_SESSION |
| 8478 | /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */ |
| 8479 | if (eap->cmdidx == CMD_mkview |
| 8480 | && (*eap->arg == NUL |
| 8481 | || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL))) |
| 8482 | { |
| 8483 | eap->forceit = TRUE; |
| 8484 | fname = get_view_file(*eap->arg); |
| 8485 | if (fname == NULL) |
| 8486 | return; |
| 8487 | viewFile = fname; |
| 8488 | using_vdir = TRUE; |
| 8489 | } |
| 8490 | else |
| 8491 | #endif |
| 8492 | if (*eap->arg != NUL) |
| 8493 | fname = eap->arg; |
| 8494 | else if (eap->cmdidx == CMD_mkvimrc) |
| 8495 | fname = (char_u *)VIMRC_FILE; |
| 8496 | #ifdef FEAT_SESSION |
| 8497 | else if (eap->cmdidx == CMD_mksession) |
| 8498 | fname = (char_u *)SESSION_FILE; |
| 8499 | #endif |
| 8500 | else |
| 8501 | fname = (char_u *)EXRC_FILE; |
| 8502 | |
| 8503 | #ifdef FEAT_BROWSE |
| 8504 | if (cmdmod.browse) |
| 8505 | { |
Bram Moolenaar | 7171abe | 2004-10-11 10:06:20 +0000 | [diff] [blame] | 8506 | browseFile = do_browse(BROWSE_SAVE, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8507 | # ifdef FEAT_SESSION |
| 8508 | eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") : |
| 8509 | eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") : |
| 8510 | # endif |
| 8511 | (char_u *)_("Save Setup"), |
| 8512 | fname, (char_u *)"vim", NULL, BROWSE_FILTER_MACROS, NULL); |
| 8513 | if (browseFile == NULL) |
| 8514 | goto theend; |
| 8515 | fname = browseFile; |
| 8516 | eap->forceit = TRUE; /* since dialog already asked */ |
| 8517 | } |
| 8518 | #endif |
| 8519 | |
| 8520 | #if defined(FEAT_SESSION) && defined(vim_mkdir) |
| 8521 | /* When using 'viewdir' may have to create the directory. */ |
| 8522 | if (using_vdir && !mch_isdir(p_vdir)) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8523 | vim_mkdir_emsg(p_vdir, 0755); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8524 | #endif |
| 8525 | |
| 8526 | fd = open_exfile(fname, eap->forceit, WRITEBIN); |
| 8527 | if (fd != NULL) |
| 8528 | { |
| 8529 | #ifdef FEAT_SESSION |
| 8530 | if (eap->cmdidx == CMD_mkview) |
| 8531 | flagp = &vop_flags; |
| 8532 | else |
| 8533 | flagp = &ssop_flags; |
| 8534 | #endif |
| 8535 | |
| 8536 | #ifdef MKSESSION_NL |
| 8537 | /* "unix" in 'sessionoptions': use NL line separator */ |
| 8538 | if (view_session && (*flagp & SSOP_UNIX)) |
| 8539 | mksession_nl = TRUE; |
| 8540 | #endif |
| 8541 | |
| 8542 | /* Write the version command for :mkvimrc */ |
| 8543 | if (eap->cmdidx == CMD_mkvimrc) |
| 8544 | (void)put_line(fd, "version 6.0"); |
| 8545 | |
| 8546 | #ifdef FEAT_SESSION |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 8547 | if (eap->cmdidx == CMD_mksession) |
| 8548 | { |
| 8549 | if (put_line(fd, "let SessionLoad = 1") == FAIL) |
| 8550 | failed = TRUE; |
| 8551 | } |
| 8552 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8553 | if (eap->cmdidx != CMD_mkview) |
| 8554 | #endif |
| 8555 | { |
| 8556 | /* Write setting 'compatible' first, because it has side effects. |
| 8557 | * For that same reason only do it when needed. */ |
| 8558 | if (p_cp) |
| 8559 | (void)put_line(fd, "if !&cp | set cp | endif"); |
| 8560 | else |
| 8561 | (void)put_line(fd, "if &cp | set nocp | endif"); |
| 8562 | } |
| 8563 | |
| 8564 | #ifdef FEAT_SESSION |
| 8565 | if (!view_session |
| 8566 | || (eap->cmdidx == CMD_mksession |
| 8567 | && (*flagp & SSOP_OPTIONS))) |
| 8568 | #endif |
| 8569 | failed |= (makemap(fd, NULL) == FAIL |
| 8570 | || makeset(fd, OPT_GLOBAL, FALSE) == FAIL); |
| 8571 | |
| 8572 | #ifdef FEAT_SESSION |
| 8573 | if (!failed && view_session) |
| 8574 | { |
| 8575 | if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL) |
| 8576 | failed = TRUE; |
| 8577 | if (eap->cmdidx == CMD_mksession) |
| 8578 | { |
| 8579 | char_u dirnow[MAXPATHL]; /* current directory */ |
| 8580 | |
| 8581 | /* |
| 8582 | * Change to session file's dir. |
| 8583 | */ |
| 8584 | if (mch_dirname(dirnow, MAXPATHL) == FAIL |
| 8585 | || mch_chdir((char *)dirnow) != 0) |
| 8586 | *dirnow = NUL; |
| 8587 | if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR)) |
| 8588 | { |
| 8589 | if (vim_chdirfile(fname) == OK) |
| 8590 | shorten_fnames(TRUE); |
| 8591 | } |
| 8592 | else if (*dirnow != NUL |
| 8593 | && (ssop_flags & SSOP_CURDIR) && globaldir != NULL) |
| 8594 | { |
| 8595 | (void)mch_chdir((char *)globaldir); |
| 8596 | shorten_fnames(TRUE); |
| 8597 | } |
| 8598 | |
| 8599 | failed |= (makeopens(fd, dirnow) == FAIL); |
| 8600 | |
| 8601 | /* restore original dir */ |
| 8602 | if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR) |
| 8603 | || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL))) |
| 8604 | { |
| 8605 | if (mch_chdir((char *)dirnow) != 0) |
| 8606 | EMSG(_(e_prev_dir)); |
| 8607 | shorten_fnames(TRUE); |
| 8608 | } |
| 8609 | } |
| 8610 | else |
| 8611 | { |
| 8612 | failed |= (put_view(fd, curwin, !using_vdir, flagp) == FAIL); |
| 8613 | } |
| 8614 | if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save") |
| 8615 | == FAIL) |
| 8616 | failed = TRUE; |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 8617 | if (put_line(fd, "doautoall SessionLoadPost") == FAIL) |
| 8618 | failed = TRUE; |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 8619 | if (eap->cmdidx == CMD_mksession) |
| 8620 | { |
| 8621 | if (put_line(fd, "unlet SessionLoad") == FAIL) |
| 8622 | failed = TRUE; |
| 8623 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8624 | } |
| 8625 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 8626 | if (put_line(fd, "\" vim: set ft=vim :") == FAIL) |
| 8627 | failed = TRUE; |
| 8628 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8629 | failed |= fclose(fd); |
| 8630 | |
| 8631 | if (failed) |
| 8632 | EMSG(_(e_write)); |
| 8633 | #if defined(FEAT_EVAL) && defined(FEAT_SESSION) |
| 8634 | else if (eap->cmdidx == CMD_mksession) |
| 8635 | { |
| 8636 | /* successful session write - set this_session var */ |
| 8637 | char_u tbuf[MAXPATHL]; |
| 8638 | |
| 8639 | if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK) |
| 8640 | set_vim_var_string(VV_THIS_SESSION, tbuf, -1); |
| 8641 | } |
| 8642 | #endif |
| 8643 | #ifdef MKSESSION_NL |
| 8644 | mksession_nl = FALSE; |
| 8645 | #endif |
| 8646 | } |
| 8647 | |
| 8648 | #ifdef FEAT_BROWSE |
| 8649 | theend: |
| 8650 | vim_free(browseFile); |
| 8651 | #endif |
| 8652 | #ifdef FEAT_SESSION |
| 8653 | vim_free(viewFile); |
| 8654 | #endif |
| 8655 | } |
| 8656 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8657 | #if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \ |
| 8658 | || defined(PROTO) |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 8659 | /*ARGSUSED*/ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 8660 | int |
| 8661 | vim_mkdir_emsg(name, prot) |
| 8662 | char_u *name; |
| 8663 | int prot; |
| 8664 | { |
| 8665 | if (vim_mkdir(name, prot) != 0) |
| 8666 | { |
| 8667 | EMSG2(_("E739: Cannot create directory: %s"), name); |
| 8668 | return FAIL; |
| 8669 | } |
| 8670 | return OK; |
| 8671 | } |
| 8672 | #endif |
| 8673 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8674 | /* |
| 8675 | * Open a file for writing for an Ex command, with some checks. |
| 8676 | * Return file descriptor, or NULL on failure. |
| 8677 | */ |
| 8678 | FILE * |
| 8679 | open_exfile(fname, forceit, mode) |
| 8680 | char_u *fname; |
| 8681 | int forceit; |
| 8682 | char *mode; /* "w" for create new file or "a" for append */ |
| 8683 | { |
| 8684 | FILE *fd; |
| 8685 | |
| 8686 | #ifdef UNIX |
| 8687 | /* with Unix it is possible to open a directory */ |
| 8688 | if (mch_isdir(fname)) |
| 8689 | { |
| 8690 | EMSG2(_(e_isadir2), fname); |
| 8691 | return NULL; |
| 8692 | } |
| 8693 | #endif |
| 8694 | if (!forceit && *mode != 'a' && vim_fexists(fname)) |
| 8695 | { |
| 8696 | EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname); |
| 8697 | return NULL; |
| 8698 | } |
| 8699 | |
| 8700 | if ((fd = mch_fopen((char *)fname, mode)) == NULL) |
| 8701 | EMSG2(_("E190: Cannot open \"%s\" for writing"), fname); |
| 8702 | |
| 8703 | return fd; |
| 8704 | } |
| 8705 | |
| 8706 | /* |
| 8707 | * ":mark" and ":k". |
| 8708 | */ |
| 8709 | static void |
| 8710 | ex_mark(eap) |
| 8711 | exarg_T *eap; |
| 8712 | { |
| 8713 | pos_T pos; |
| 8714 | |
| 8715 | if (*eap->arg == NUL) /* No argument? */ |
| 8716 | EMSG(_(e_argreq)); |
| 8717 | else if (eap->arg[1] != NUL) /* more than one character? */ |
| 8718 | EMSG(_(e_trailing)); |
| 8719 | else |
| 8720 | { |
| 8721 | pos = curwin->w_cursor; /* save curwin->w_cursor */ |
| 8722 | curwin->w_cursor.lnum = eap->line2; |
| 8723 | beginline(BL_WHITE | BL_FIX); |
| 8724 | if (setmark(*eap->arg) == FAIL) /* set mark */ |
| 8725 | EMSG(_("E191: Argument must be a letter or forward/backward quote")); |
| 8726 | curwin->w_cursor = pos; /* restore curwin->w_cursor */ |
| 8727 | } |
| 8728 | } |
| 8729 | |
| 8730 | /* |
| 8731 | * Update w_topline, w_leftcol and the cursor position. |
| 8732 | */ |
| 8733 | void |
| 8734 | update_topline_cursor() |
| 8735 | { |
| 8736 | check_cursor(); /* put cursor on valid line */ |
| 8737 | update_topline(); |
| 8738 | if (!curwin->w_p_wrap) |
| 8739 | validate_cursor(); |
| 8740 | update_curswant(); |
| 8741 | } |
| 8742 | |
| 8743 | #ifdef FEAT_EX_EXTRA |
| 8744 | /* |
| 8745 | * ":normal[!] {commands}": Execute normal mode commands. |
| 8746 | */ |
| 8747 | static void |
| 8748 | ex_normal(eap) |
| 8749 | exarg_T *eap; |
| 8750 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8751 | int save_msg_scroll = msg_scroll; |
| 8752 | int save_restart_edit = restart_edit; |
| 8753 | int save_msg_didout = msg_didout; |
| 8754 | int save_State = State; |
| 8755 | tasave_T tabuf; |
| 8756 | int save_insertmode = p_im; |
| 8757 | int save_finish_op = finish_op; |
| 8758 | #ifdef FEAT_MBYTE |
| 8759 | char_u *arg = NULL; |
| 8760 | int l; |
| 8761 | char_u *p; |
| 8762 | #endif |
| 8763 | |
| 8764 | if (ex_normal_busy >= p_mmd) |
| 8765 | { |
| 8766 | EMSG(_("E192: Recursive use of :normal too deep")); |
| 8767 | return; |
| 8768 | } |
| 8769 | ++ex_normal_busy; |
| 8770 | |
| 8771 | msg_scroll = FALSE; /* no msg scrolling in Normal mode */ |
| 8772 | restart_edit = 0; /* don't go to Insert mode */ |
| 8773 | p_im = FALSE; /* don't use 'insertmode' */ |
| 8774 | |
| 8775 | #ifdef FEAT_MBYTE |
| 8776 | /* |
| 8777 | * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do |
| 8778 | * this for the K_SPECIAL leading byte, otherwise special keys will not |
| 8779 | * work. |
| 8780 | */ |
| 8781 | if (has_mbyte) |
| 8782 | { |
| 8783 | int len = 0; |
| 8784 | |
| 8785 | /* Count the number of characters to be escaped. */ |
| 8786 | for (p = eap->arg; *p != NUL; ++p) |
| 8787 | { |
| 8788 | # ifdef FEAT_GUI |
| 8789 | if (*p == CSI) /* leadbyte CSI */ |
| 8790 | len += 2; |
| 8791 | # endif |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8792 | for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8793 | if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */ |
| 8794 | # ifdef FEAT_GUI |
| 8795 | || *p == CSI |
| 8796 | # endif |
| 8797 | ) |
| 8798 | len += 2; |
| 8799 | } |
| 8800 | if (len > 0) |
| 8801 | { |
| 8802 | arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1)); |
| 8803 | if (arg != NULL) |
| 8804 | { |
| 8805 | len = 0; |
| 8806 | for (p = eap->arg; *p != NUL; ++p) |
| 8807 | { |
| 8808 | arg[len++] = *p; |
| 8809 | # ifdef FEAT_GUI |
| 8810 | if (*p == CSI) |
| 8811 | { |
| 8812 | arg[len++] = KS_EXTRA; |
| 8813 | arg[len++] = (int)KE_CSI; |
| 8814 | } |
| 8815 | # endif |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 8816 | for (l = (*mb_ptr2len)(p) - 1; l > 0; --l) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8817 | { |
| 8818 | arg[len++] = *++p; |
| 8819 | if (*p == K_SPECIAL) |
| 8820 | { |
| 8821 | arg[len++] = KS_SPECIAL; |
| 8822 | arg[len++] = KE_FILLER; |
| 8823 | } |
| 8824 | # ifdef FEAT_GUI |
| 8825 | else if (*p == CSI) |
| 8826 | { |
| 8827 | arg[len++] = KS_EXTRA; |
| 8828 | arg[len++] = (int)KE_CSI; |
| 8829 | } |
| 8830 | # endif |
| 8831 | } |
| 8832 | arg[len] = NUL; |
| 8833 | } |
| 8834 | } |
| 8835 | } |
| 8836 | } |
| 8837 | #endif |
| 8838 | |
| 8839 | /* |
| 8840 | * Save the current typeahead. This is required to allow using ":normal" |
| 8841 | * from an event handler and makes sure we don't hang when the argument |
| 8842 | * ends with half a command. |
| 8843 | */ |
| 8844 | save_typeahead(&tabuf); |
| 8845 | if (tabuf.typebuf_valid) |
| 8846 | { |
| 8847 | /* |
| 8848 | * Repeat the :normal command for each line in the range. When no |
| 8849 | * range given, execute it just once, without positioning the cursor |
| 8850 | * first. |
| 8851 | */ |
| 8852 | do |
| 8853 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8854 | if (eap->addr_count != 0) |
| 8855 | { |
| 8856 | curwin->w_cursor.lnum = eap->line1++; |
| 8857 | curwin->w_cursor.col = 0; |
| 8858 | } |
| 8859 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8860 | exec_normal_cmd( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8861 | #ifdef FEAT_MBYTE |
| 8862 | arg != NULL ? arg : |
| 8863 | #endif |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8864 | eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8865 | } |
| 8866 | while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int); |
| 8867 | } |
| 8868 | |
| 8869 | /* Might not return to the main loop when in an event handler. */ |
| 8870 | update_topline_cursor(); |
| 8871 | |
| 8872 | /* Restore the previous typeahead. */ |
| 8873 | restore_typeahead(&tabuf); |
| 8874 | |
| 8875 | --ex_normal_busy; |
| 8876 | msg_scroll = save_msg_scroll; |
| 8877 | restart_edit = save_restart_edit; |
| 8878 | p_im = save_insertmode; |
| 8879 | finish_op = save_finish_op; |
| 8880 | msg_didout |= save_msg_didout; /* don't reset msg_didout now */ |
| 8881 | |
| 8882 | /* Restore the state (needed when called from a function executed for |
| 8883 | * 'indentexpr'). */ |
| 8884 | State = save_State; |
| 8885 | #ifdef FEAT_MBYTE |
| 8886 | vim_free(arg); |
| 8887 | #endif |
| 8888 | } |
| 8889 | |
| 8890 | /* |
Bram Moolenaar | 6496966 | 2005-12-14 21:59:55 +0000 | [diff] [blame] | 8891 | * ":startinsert", ":startreplace" and ":startgreplace" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8892 | */ |
| 8893 | static void |
| 8894 | ex_startinsert(eap) |
| 8895 | exarg_T *eap; |
| 8896 | { |
Bram Moolenaar | fd37168 | 2005-01-14 21:42:54 +0000 | [diff] [blame] | 8897 | if (eap->forceit) |
| 8898 | { |
| 8899 | coladvance((colnr_T)MAXCOL); |
| 8900 | curwin->w_curswant = MAXCOL; |
| 8901 | curwin->w_set_curswant = FALSE; |
| 8902 | } |
| 8903 | |
Bram Moolenaar | a40c500 | 2005-01-09 21:16:21 +0000 | [diff] [blame] | 8904 | /* Ignore the command when already in Insert mode. Inserting an |
| 8905 | * expression register that invokes a function can do this. */ |
| 8906 | if (State & INSERT) |
| 8907 | return; |
| 8908 | |
Bram Moolenaar | 6496966 | 2005-12-14 21:59:55 +0000 | [diff] [blame] | 8909 | if (eap->cmdidx == CMD_startinsert) |
| 8910 | restart_edit = 'a'; |
| 8911 | else if (eap->cmdidx == CMD_startreplace) |
| 8912 | restart_edit = 'R'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8913 | else |
Bram Moolenaar | 6496966 | 2005-12-14 21:59:55 +0000 | [diff] [blame] | 8914 | restart_edit = 'V'; |
| 8915 | |
| 8916 | if (!eap->forceit) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8917 | { |
Bram Moolenaar | 325b7a2 | 2004-07-05 15:58:32 +0000 | [diff] [blame] | 8918 | if (eap->cmdidx == CMD_startinsert) |
| 8919 | restart_edit = 'i'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8920 | curwin->w_curswant = 0; /* avoid MAXCOL */ |
| 8921 | } |
| 8922 | } |
| 8923 | |
| 8924 | /* |
| 8925 | * ":stopinsert" |
| 8926 | */ |
| 8927 | /*ARGSUSED*/ |
| 8928 | static void |
| 8929 | ex_stopinsert(eap) |
| 8930 | exarg_T *eap; |
| 8931 | { |
| 8932 | restart_edit = 0; |
| 8933 | stop_insert_mode = TRUE; |
| 8934 | } |
| 8935 | #endif |
| 8936 | |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 8937 | #if defined(FEAT_EX_EXTRA) || defined(FEAT_MENU) || defined(PROTO) |
| 8938 | /* |
| 8939 | * Execute normal mode command "cmd". |
| 8940 | * "remap" can be REMAP_NONE or REMAP_YES. |
| 8941 | */ |
| 8942 | void |
| 8943 | exec_normal_cmd(cmd, remap, silent) |
| 8944 | char_u *cmd; |
| 8945 | int remap; |
| 8946 | int silent; |
| 8947 | { |
| 8948 | oparg_T oa; |
| 8949 | |
| 8950 | /* |
| 8951 | * Stuff the argument into the typeahead buffer. |
| 8952 | * Execute normal_cmd() until there is no typeahead left. |
| 8953 | */ |
| 8954 | clear_oparg(&oa); |
| 8955 | finish_op = FALSE; |
| 8956 | ins_typebuf(cmd, remap, 0, TRUE, silent); |
| 8957 | while ((!stuff_empty() || (!typebuf_typed() && typebuf.tb_len > 0)) |
| 8958 | && !got_int) |
| 8959 | { |
| 8960 | update_topline_cursor(); |
| 8961 | normal_cmd(&oa, FALSE); /* execute a Normal mode cmd */ |
| 8962 | } |
| 8963 | } |
| 8964 | #endif |
| 8965 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 8966 | #ifdef FEAT_FIND_ID |
| 8967 | static void |
| 8968 | ex_checkpath(eap) |
| 8969 | exarg_T *eap; |
| 8970 | { |
| 8971 | find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L, |
| 8972 | eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW, |
| 8973 | (linenr_T)1, (linenr_T)MAXLNUM); |
| 8974 | } |
| 8975 | |
| 8976 | #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX) |
| 8977 | /* |
| 8978 | * ":psearch" |
| 8979 | */ |
| 8980 | static void |
| 8981 | ex_psearch(eap) |
| 8982 | exarg_T *eap; |
| 8983 | { |
| 8984 | g_do_tagpreview = p_pvh; |
| 8985 | ex_findpat(eap); |
| 8986 | g_do_tagpreview = 0; |
| 8987 | } |
| 8988 | #endif |
| 8989 | |
| 8990 | static void |
| 8991 | ex_findpat(eap) |
| 8992 | exarg_T *eap; |
| 8993 | { |
| 8994 | int whole = TRUE; |
| 8995 | long n; |
| 8996 | char_u *p; |
| 8997 | int action; |
| 8998 | |
| 8999 | switch (cmdnames[eap->cmdidx].cmd_name[2]) |
| 9000 | { |
| 9001 | case 'e': /* ":psearch", ":isearch" and ":dsearch" */ |
| 9002 | if (cmdnames[eap->cmdidx].cmd_name[0] == 'p') |
| 9003 | action = ACTION_GOTO; |
| 9004 | else |
| 9005 | action = ACTION_SHOW; |
| 9006 | break; |
| 9007 | case 'i': /* ":ilist" and ":dlist" */ |
| 9008 | action = ACTION_SHOW_ALL; |
| 9009 | break; |
| 9010 | case 'u': /* ":ijump" and ":djump" */ |
| 9011 | action = ACTION_GOTO; |
| 9012 | break; |
| 9013 | default: /* ":isplit" and ":dsplit" */ |
| 9014 | action = ACTION_SPLIT; |
| 9015 | break; |
| 9016 | } |
| 9017 | |
| 9018 | n = 1; |
| 9019 | if (vim_isdigit(*eap->arg)) /* get count */ |
| 9020 | { |
| 9021 | n = getdigits(&eap->arg); |
| 9022 | eap->arg = skipwhite(eap->arg); |
| 9023 | } |
| 9024 | if (*eap->arg == '/') /* Match regexp, not just whole words */ |
| 9025 | { |
| 9026 | whole = FALSE; |
| 9027 | ++eap->arg; |
| 9028 | p = skip_regexp(eap->arg, '/', p_magic, NULL); |
| 9029 | if (*p) |
| 9030 | { |
| 9031 | *p++ = NUL; |
| 9032 | p = skipwhite(p); |
| 9033 | |
| 9034 | /* Check for trailing illegal characters */ |
| 9035 | if (!ends_excmd(*p)) |
| 9036 | eap->errmsg = e_trailing; |
| 9037 | else |
| 9038 | eap->nextcmd = check_nextcmd(p); |
| 9039 | } |
| 9040 | } |
| 9041 | if (!eap->skip) |
| 9042 | find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg), |
| 9043 | whole, !eap->forceit, |
| 9044 | *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY, |
| 9045 | n, action, eap->line1, eap->line2); |
| 9046 | } |
| 9047 | #endif |
| 9048 | |
| 9049 | #ifdef FEAT_WINDOWS |
| 9050 | |
| 9051 | # ifdef FEAT_QUICKFIX |
| 9052 | /* |
| 9053 | * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc. |
| 9054 | */ |
| 9055 | static void |
| 9056 | ex_ptag(eap) |
| 9057 | exarg_T *eap; |
| 9058 | { |
| 9059 | g_do_tagpreview = p_pvh; |
| 9060 | ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); |
| 9061 | } |
| 9062 | |
| 9063 | /* |
| 9064 | * ":pedit" |
| 9065 | */ |
| 9066 | static void |
| 9067 | ex_pedit(eap) |
| 9068 | exarg_T *eap; |
| 9069 | { |
| 9070 | win_T *curwin_save = curwin; |
| 9071 | |
| 9072 | g_do_tagpreview = p_pvh; |
Bram Moolenaar | 607a95ed | 2006-03-28 20:57:42 +0000 | [diff] [blame] | 9073 | prepare_tagpreview(TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9074 | keep_help_flag = curwin_save->w_buffer->b_help; |
| 9075 | do_exedit(eap, NULL); |
| 9076 | keep_help_flag = FALSE; |
| 9077 | if (curwin != curwin_save && win_valid(curwin_save)) |
| 9078 | { |
| 9079 | /* Return cursor to where we were */ |
| 9080 | validate_cursor(); |
| 9081 | redraw_later(VALID); |
| 9082 | win_enter(curwin_save, TRUE); |
| 9083 | } |
| 9084 | g_do_tagpreview = 0; |
| 9085 | } |
| 9086 | # endif |
| 9087 | |
| 9088 | /* |
| 9089 | * ":stag", ":stselect" and ":stjump". |
| 9090 | */ |
| 9091 | static void |
| 9092 | ex_stag(eap) |
| 9093 | exarg_T *eap; |
| 9094 | { |
| 9095 | postponed_split = -1; |
| 9096 | postponed_split_flags = cmdmod.split; |
| 9097 | ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1); |
| 9098 | postponed_split_flags = 0; |
| 9099 | } |
| 9100 | #endif |
| 9101 | |
| 9102 | /* |
| 9103 | * ":tag", ":tselect", ":tjump", ":tnext", etc. |
| 9104 | */ |
| 9105 | static void |
| 9106 | ex_tag(eap) |
| 9107 | exarg_T *eap; |
| 9108 | { |
| 9109 | ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name); |
| 9110 | } |
| 9111 | |
| 9112 | static void |
| 9113 | ex_tag_cmd(eap, name) |
| 9114 | exarg_T *eap; |
| 9115 | char_u *name; |
| 9116 | { |
| 9117 | int cmd; |
| 9118 | |
| 9119 | switch (name[1]) |
| 9120 | { |
| 9121 | case 'j': cmd = DT_JUMP; /* ":tjump" */ |
| 9122 | break; |
| 9123 | case 's': cmd = DT_SELECT; /* ":tselect" */ |
| 9124 | break; |
| 9125 | case 'p': cmd = DT_PREV; /* ":tprevious" */ |
| 9126 | break; |
| 9127 | case 'N': cmd = DT_PREV; /* ":tNext" */ |
| 9128 | break; |
| 9129 | case 'n': cmd = DT_NEXT; /* ":tnext" */ |
| 9130 | break; |
| 9131 | case 'o': cmd = DT_POP; /* ":pop" */ |
| 9132 | break; |
| 9133 | case 'f': /* ":tfirst" */ |
| 9134 | case 'r': cmd = DT_FIRST; /* ":trewind" */ |
| 9135 | break; |
| 9136 | case 'l': cmd = DT_LAST; /* ":tlast" */ |
| 9137 | break; |
| 9138 | default: /* ":tag" */ |
| 9139 | #ifdef FEAT_CSCOPE |
| 9140 | if (p_cst) |
| 9141 | { |
| 9142 | do_cstag(eap); |
| 9143 | return; |
| 9144 | } |
| 9145 | #endif |
| 9146 | cmd = DT_TAG; |
| 9147 | break; |
| 9148 | } |
| 9149 | |
Bram Moolenaar | b8a7b56 | 2006-02-01 21:47:16 +0000 | [diff] [blame] | 9150 | if (name[0] == 'l') |
| 9151 | { |
| 9152 | #ifndef FEAT_QUICKFIX |
| 9153 | ex_ni(eap); |
| 9154 | return; |
| 9155 | #else |
| 9156 | cmd = DT_LTAG; |
| 9157 | #endif |
| 9158 | } |
| 9159 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9160 | do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1, |
| 9161 | eap->forceit, TRUE); |
| 9162 | } |
| 9163 | |
| 9164 | /* |
| 9165 | * Evaluate cmdline variables. |
| 9166 | * |
| 9167 | * change '%' to curbuf->b_ffname |
| 9168 | * '#' to curwin->w_altfile |
| 9169 | * '<cword>' to word under the cursor |
| 9170 | * '<cWORD>' to WORD under the cursor |
| 9171 | * '<cfile>' to path name under the cursor |
| 9172 | * '<sfile>' to sourced file name |
| 9173 | * '<afile>' to file name for autocommand |
| 9174 | * '<abuf>' to buffer number for autocommand |
| 9175 | * '<amatch>' to matching name for autocommand |
| 9176 | * |
| 9177 | * When an error is detected, "errormsg" is set to a non-NULL pointer (may be |
| 9178 | * "" for error without a message) and NULL is returned. |
| 9179 | * Returns an allocated string if a valid match was found. |
| 9180 | * Returns NULL if no match was found. "usedlen" then still contains the |
| 9181 | * number of characters to skip. |
| 9182 | */ |
| 9183 | char_u * |
| 9184 | eval_vars(src, usedlen, lnump, errormsg, srcstart) |
| 9185 | char_u *src; /* pointer into commandline */ |
| 9186 | int *usedlen; /* characters after src that are used */ |
| 9187 | linenr_T *lnump; /* line number for :e command, or NULL */ |
| 9188 | char_u **errormsg; /* pointer to error message */ |
| 9189 | char_u *srcstart; /* beginning of valid memory for src */ |
| 9190 | { |
| 9191 | int i; |
| 9192 | char_u *s; |
| 9193 | char_u *result; |
| 9194 | char_u *resultbuf = NULL; |
| 9195 | int resultlen; |
| 9196 | buf_T *buf; |
| 9197 | int valid = VALID_HEAD + VALID_PATH; /* assume valid result */ |
| 9198 | int spec_idx; |
| 9199 | #ifdef FEAT_MODIFY_FNAME |
| 9200 | int skip_mod = FALSE; |
| 9201 | #endif |
| 9202 | static char *(spec_str[]) = |
| 9203 | { |
| 9204 | "%", |
| 9205 | #define SPEC_PERC 0 |
| 9206 | "#", |
| 9207 | #define SPEC_HASH 1 |
| 9208 | "<cword>", /* cursor word */ |
| 9209 | #define SPEC_CWORD 2 |
| 9210 | "<cWORD>", /* cursor WORD */ |
| 9211 | #define SPEC_CCWORD 3 |
| 9212 | "<cfile>", /* cursor path name */ |
| 9213 | #define SPEC_CFILE 4 |
| 9214 | "<sfile>", /* ":so" file name */ |
| 9215 | #define SPEC_SFILE 5 |
| 9216 | #ifdef FEAT_AUTOCMD |
| 9217 | "<afile>", /* autocommand file name */ |
| 9218 | # define SPEC_AFILE 6 |
| 9219 | "<abuf>", /* autocommand buffer number */ |
| 9220 | # define SPEC_ABUF 7 |
| 9221 | "<amatch>", /* autocommand match name */ |
| 9222 | # define SPEC_AMATCH 8 |
| 9223 | #endif |
| 9224 | #ifdef FEAT_CLIENTSERVER |
| 9225 | "<client>" |
| 9226 | # define SPEC_CLIENT 9 |
| 9227 | #endif |
| 9228 | }; |
| 9229 | #define SPEC_COUNT (sizeof(spec_str) / sizeof(char *)) |
| 9230 | |
| 9231 | #if defined(FEAT_AUTOCMD) || defined(FEAT_CLIENTSERVER) |
| 9232 | char_u strbuf[30]; |
| 9233 | #endif |
| 9234 | |
| 9235 | *errormsg = NULL; |
| 9236 | |
| 9237 | /* |
| 9238 | * Check if there is something to do. |
| 9239 | */ |
| 9240 | for (spec_idx = 0; spec_idx < SPEC_COUNT; ++spec_idx) |
| 9241 | { |
| 9242 | *usedlen = (int)STRLEN(spec_str[spec_idx]); |
| 9243 | if (STRNCMP(src, spec_str[spec_idx], *usedlen) == 0) |
| 9244 | break; |
| 9245 | } |
| 9246 | if (spec_idx == SPEC_COUNT) /* no match */ |
| 9247 | { |
| 9248 | *usedlen = 1; |
| 9249 | return NULL; |
| 9250 | } |
| 9251 | |
| 9252 | /* |
| 9253 | * Skip when preceded with a backslash "\%" and "\#". |
| 9254 | * Note: In "\\%" the % is also not recognized! |
| 9255 | */ |
| 9256 | if (src > srcstart && src[-1] == '\\') |
| 9257 | { |
| 9258 | *usedlen = 0; |
| 9259 | STRCPY(src - 1, src); /* remove backslash */ |
| 9260 | return NULL; |
| 9261 | } |
| 9262 | |
| 9263 | /* |
| 9264 | * word or WORD under cursor |
| 9265 | */ |
| 9266 | if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD) |
| 9267 | { |
| 9268 | resultlen = find_ident_under_cursor(&result, spec_idx == SPEC_CWORD ? |
| 9269 | (FIND_IDENT|FIND_STRING) : FIND_STRING); |
| 9270 | if (resultlen == 0) |
| 9271 | { |
| 9272 | *errormsg = (char_u *)""; |
| 9273 | return NULL; |
| 9274 | } |
| 9275 | } |
| 9276 | |
| 9277 | /* |
| 9278 | * '#': Alternate file name |
| 9279 | * '%': Current file name |
| 9280 | * File name under the cursor |
| 9281 | * File name for autocommand |
| 9282 | * and following modifiers |
| 9283 | */ |
| 9284 | else |
| 9285 | { |
| 9286 | switch (spec_idx) |
| 9287 | { |
| 9288 | case SPEC_PERC: /* '%': current file */ |
| 9289 | if (curbuf->b_fname == NULL) |
| 9290 | { |
| 9291 | result = (char_u *)""; |
| 9292 | valid = 0; /* Must have ":p:h" to be valid */ |
| 9293 | } |
| 9294 | else |
| 9295 | #ifdef RISCOS |
| 9296 | /* Always use the full path for RISC OS if possible. */ |
| 9297 | result = curbuf->b_ffname; |
| 9298 | if (result == NULL) |
| 9299 | result = curbuf->b_fname; |
| 9300 | #else |
| 9301 | result = curbuf->b_fname; |
| 9302 | #endif |
| 9303 | break; |
| 9304 | |
| 9305 | case SPEC_HASH: /* '#' or "#99": alternate file */ |
| 9306 | if (src[1] == '#') /* "##": the argument list */ |
| 9307 | { |
| 9308 | result = arg_all(); |
| 9309 | resultbuf = result; |
| 9310 | *usedlen = 2; |
| 9311 | #ifdef FEAT_MODIFY_FNAME |
| 9312 | skip_mod = TRUE; |
| 9313 | #endif |
| 9314 | break; |
| 9315 | } |
| 9316 | s = src + 1; |
| 9317 | i = (int)getdigits(&s); |
| 9318 | *usedlen = (int)(s - src); /* length of what we expand */ |
| 9319 | |
| 9320 | buf = buflist_findnr(i); |
| 9321 | if (buf == NULL) |
| 9322 | { |
| 9323 | *errormsg = (char_u *)_("E194: No alternate file name to substitute for '#'"); |
| 9324 | return NULL; |
| 9325 | } |
| 9326 | if (lnump != NULL) |
| 9327 | *lnump = ECMD_LAST; |
| 9328 | if (buf->b_fname == NULL) |
| 9329 | { |
| 9330 | result = (char_u *)""; |
| 9331 | valid = 0; /* Must have ":p:h" to be valid */ |
| 9332 | } |
| 9333 | else |
| 9334 | result = buf->b_fname; |
| 9335 | break; |
| 9336 | |
| 9337 | #ifdef FEAT_SEARCHPATH |
| 9338 | case SPEC_CFILE: /* file name under cursor */ |
Bram Moolenaar | d1f56e6 | 2006-02-22 21:25:37 +0000 | [diff] [blame] | 9339 | result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9340 | if (result == NULL) |
| 9341 | { |
| 9342 | *errormsg = (char_u *)""; |
| 9343 | return NULL; |
| 9344 | } |
| 9345 | resultbuf = result; /* remember allocated string */ |
| 9346 | break; |
| 9347 | #endif |
| 9348 | |
| 9349 | #ifdef FEAT_AUTOCMD |
| 9350 | case SPEC_AFILE: /* file name for autocommand */ |
| 9351 | result = autocmd_fname; |
| 9352 | if (result == NULL) |
| 9353 | { |
| 9354 | *errormsg = (char_u *)_("E495: no autocommand file name to substitute for \"<afile>\""); |
| 9355 | return NULL; |
| 9356 | } |
| 9357 | break; |
| 9358 | |
| 9359 | case SPEC_ABUF: /* buffer number for autocommand */ |
| 9360 | if (autocmd_bufnr <= 0) |
| 9361 | { |
| 9362 | *errormsg = (char_u *)_("E496: no autocommand buffer number to substitute for \"<abuf>\""); |
| 9363 | return NULL; |
| 9364 | } |
| 9365 | sprintf((char *)strbuf, "%d", autocmd_bufnr); |
| 9366 | result = strbuf; |
| 9367 | break; |
| 9368 | |
| 9369 | case SPEC_AMATCH: /* match name for autocommand */ |
| 9370 | result = autocmd_match; |
| 9371 | if (result == NULL) |
| 9372 | { |
| 9373 | *errormsg = (char_u *)_("E497: no autocommand match name to substitute for \"<amatch>\""); |
| 9374 | return NULL; |
| 9375 | } |
| 9376 | break; |
| 9377 | |
| 9378 | #endif |
| 9379 | case SPEC_SFILE: /* file name for ":so" command */ |
| 9380 | result = sourcing_name; |
| 9381 | if (result == NULL) |
| 9382 | { |
| 9383 | *errormsg = (char_u *)_("E498: no :source file name to substitute for \"<sfile>\""); |
| 9384 | return NULL; |
| 9385 | } |
| 9386 | break; |
| 9387 | #if defined(FEAT_CLIENTSERVER) |
| 9388 | case SPEC_CLIENT: /* Source of last submitted input */ |
| 9389 | sprintf((char *)strbuf, "0x%x", (unsigned int)clientWindow); |
| 9390 | result = strbuf; |
| 9391 | break; |
| 9392 | #endif |
| 9393 | } |
| 9394 | |
| 9395 | resultlen = (int)STRLEN(result); /* length of new string */ |
| 9396 | if (src[*usedlen] == '<') /* remove the file name extension */ |
| 9397 | { |
| 9398 | ++*usedlen; |
| 9399 | #ifdef RISCOS |
| 9400 | if ((s = vim_strrchr(result, '/')) != NULL && s >= gettail(result)) |
| 9401 | #else |
| 9402 | if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result)) |
| 9403 | #endif |
| 9404 | resultlen = (int)(s - result); |
| 9405 | } |
| 9406 | #ifdef FEAT_MODIFY_FNAME |
| 9407 | else if (!skip_mod) |
| 9408 | { |
| 9409 | valid |= modify_fname(src, usedlen, &result, &resultbuf, |
| 9410 | &resultlen); |
| 9411 | if (result == NULL) |
| 9412 | { |
| 9413 | *errormsg = (char_u *)""; |
| 9414 | return NULL; |
| 9415 | } |
| 9416 | } |
| 9417 | #endif |
| 9418 | } |
| 9419 | |
| 9420 | if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH) |
| 9421 | { |
| 9422 | if (valid != VALID_HEAD + VALID_PATH) |
| 9423 | /* xgettext:no-c-format */ |
| 9424 | *errormsg = (char_u *)_("E499: Empty file name for '%' or '#', only works with \":p:h\""); |
| 9425 | else |
| 9426 | *errormsg = (char_u *)_("E500: Evaluates to an empty string"); |
| 9427 | result = NULL; |
| 9428 | } |
| 9429 | else |
| 9430 | result = vim_strnsave(result, resultlen); |
| 9431 | vim_free(resultbuf); |
| 9432 | return result; |
| 9433 | } |
| 9434 | |
| 9435 | /* |
| 9436 | * Concatenate all files in the argument list, separated by spaces, and return |
| 9437 | * it in one allocated string. |
| 9438 | * Spaces and backslashes in the file names are escaped with a backslash. |
| 9439 | * Returns NULL when out of memory. |
| 9440 | */ |
| 9441 | static char_u * |
| 9442 | arg_all() |
| 9443 | { |
| 9444 | int len; |
| 9445 | int idx; |
| 9446 | char_u *retval = NULL; |
| 9447 | char_u *p; |
| 9448 | |
| 9449 | /* |
| 9450 | * Do this loop two times: |
| 9451 | * first time: compute the total length |
| 9452 | * second time: concatenate the names |
| 9453 | */ |
| 9454 | for (;;) |
| 9455 | { |
| 9456 | len = 0; |
| 9457 | for (idx = 0; idx < ARGCOUNT; ++idx) |
| 9458 | { |
| 9459 | p = alist_name(&ARGLIST[idx]); |
| 9460 | if (p != NULL) |
| 9461 | { |
| 9462 | if (len > 0) |
| 9463 | { |
| 9464 | /* insert a space in between names */ |
| 9465 | if (retval != NULL) |
| 9466 | retval[len] = ' '; |
| 9467 | ++len; |
| 9468 | } |
| 9469 | for ( ; *p != NUL; ++p) |
| 9470 | { |
| 9471 | if (*p == ' ' || *p == '\\') |
| 9472 | { |
| 9473 | /* insert a backslash */ |
| 9474 | if (retval != NULL) |
| 9475 | retval[len] = '\\'; |
| 9476 | ++len; |
| 9477 | } |
| 9478 | if (retval != NULL) |
| 9479 | retval[len] = *p; |
| 9480 | ++len; |
| 9481 | } |
| 9482 | } |
| 9483 | } |
| 9484 | |
| 9485 | /* second time: break here */ |
| 9486 | if (retval != NULL) |
| 9487 | { |
| 9488 | retval[len] = NUL; |
| 9489 | break; |
| 9490 | } |
| 9491 | |
| 9492 | /* allocate memory */ |
| 9493 | retval = alloc(len + 1); |
| 9494 | if (retval == NULL) |
| 9495 | break; |
| 9496 | } |
| 9497 | |
| 9498 | return retval; |
| 9499 | } |
| 9500 | |
| 9501 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 9502 | /* |
| 9503 | * Expand the <sfile> string in "arg". |
| 9504 | * |
| 9505 | * Returns an allocated string, or NULL for any error. |
| 9506 | */ |
| 9507 | char_u * |
| 9508 | expand_sfile(arg) |
| 9509 | char_u *arg; |
| 9510 | { |
| 9511 | char_u *errormsg; |
| 9512 | int len; |
| 9513 | char_u *result; |
| 9514 | char_u *newres; |
| 9515 | char_u *repl; |
| 9516 | int srclen; |
| 9517 | char_u *p; |
| 9518 | |
| 9519 | result = vim_strsave(arg); |
| 9520 | if (result == NULL) |
| 9521 | return NULL; |
| 9522 | |
| 9523 | for (p = result; *p; ) |
| 9524 | { |
| 9525 | if (STRNCMP(p, "<sfile>", 7) != 0) |
| 9526 | ++p; |
| 9527 | else |
| 9528 | { |
| 9529 | /* replace "<sfile>" with the sourced file name, and do ":" stuff */ |
| 9530 | repl = eval_vars(p, &srclen, NULL, &errormsg, result); |
| 9531 | if (errormsg != NULL) |
| 9532 | { |
| 9533 | if (*errormsg) |
| 9534 | emsg(errormsg); |
| 9535 | vim_free(result); |
| 9536 | return NULL; |
| 9537 | } |
| 9538 | if (repl == NULL) /* no match (cannot happen) */ |
| 9539 | { |
| 9540 | p += srclen; |
| 9541 | continue; |
| 9542 | } |
| 9543 | len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1; |
| 9544 | newres = alloc(len); |
| 9545 | if (newres == NULL) |
| 9546 | { |
| 9547 | vim_free(repl); |
| 9548 | vim_free(result); |
| 9549 | return NULL; |
| 9550 | } |
| 9551 | mch_memmove(newres, result, (size_t)(p - result)); |
| 9552 | STRCPY(newres + (p - result), repl); |
| 9553 | len = (int)STRLEN(newres); |
| 9554 | STRCAT(newres, p + srclen); |
| 9555 | vim_free(repl); |
| 9556 | vim_free(result); |
| 9557 | result = newres; |
| 9558 | p = newres + len; /* continue after the match */ |
| 9559 | } |
| 9560 | } |
| 9561 | |
| 9562 | return result; |
| 9563 | } |
| 9564 | #endif |
| 9565 | |
| 9566 | #ifdef FEAT_SESSION |
| 9567 | static int ses_winsizes __ARGS((FILE *fd, int restore_size)); |
| 9568 | static int ses_win_rec __ARGS((FILE *fd, frame_T *fr)); |
| 9569 | static frame_T *ses_skipframe __ARGS((frame_T *fr)); |
| 9570 | static int ses_do_frame __ARGS((frame_T *fr)); |
| 9571 | static int ses_do_win __ARGS((win_T *wp)); |
| 9572 | static int ses_arglist __ARGS((FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp)); |
| 9573 | static int ses_put_fname __ARGS((FILE *fd, char_u *name, unsigned *flagp)); |
| 9574 | static int ses_fname __ARGS((FILE *fd, buf_T *buf, unsigned *flagp)); |
| 9575 | |
| 9576 | /* |
| 9577 | * Write openfile commands for the current buffers to an .exrc file. |
| 9578 | * Return FAIL on error, OK otherwise. |
| 9579 | */ |
| 9580 | static int |
| 9581 | makeopens(fd, dirnow) |
| 9582 | FILE *fd; |
| 9583 | char_u *dirnow; /* Current directory name */ |
| 9584 | { |
| 9585 | buf_T *buf; |
| 9586 | int only_save_windows = TRUE; |
| 9587 | int nr; |
| 9588 | int cnr = 1; |
| 9589 | int restore_size = TRUE; |
| 9590 | win_T *wp; |
| 9591 | char_u *sname; |
| 9592 | win_T *edited_win = NULL; |
| 9593 | |
| 9594 | if (ssop_flags & SSOP_BUFFERS) |
| 9595 | only_save_windows = FALSE; /* Save ALL buffers */ |
| 9596 | |
| 9597 | /* |
| 9598 | * Begin by setting the this_session variable, and then other |
| 9599 | * sessionable variables. |
| 9600 | */ |
| 9601 | #ifdef FEAT_EVAL |
| 9602 | if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL) |
| 9603 | return FAIL; |
| 9604 | if (ssop_flags & SSOP_GLOBALS) |
| 9605 | if (store_session_globals(fd) == FAIL) |
| 9606 | return FAIL; |
| 9607 | #endif |
| 9608 | |
| 9609 | /* |
| 9610 | * Close all windows but one. |
| 9611 | */ |
| 9612 | if (put_line(fd, "silent only") == FAIL) |
| 9613 | return FAIL; |
| 9614 | |
| 9615 | /* |
| 9616 | * Now a :cd command to the session directory or the current directory |
| 9617 | */ |
| 9618 | if (ssop_flags & SSOP_SESDIR) |
| 9619 | { |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 9620 | if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')") |
| 9621 | == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9622 | return FAIL; |
| 9623 | } |
| 9624 | else if (ssop_flags & SSOP_CURDIR) |
| 9625 | { |
| 9626 | sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow); |
| 9627 | if (sname == NULL |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 9628 | || fputs("cd ", fd) < 0 |
| 9629 | || ses_put_fname(fd, sname, &ssop_flags) == FAIL |
| 9630 | || put_eol(fd) == FAIL) |
| 9631 | { |
| 9632 | vim_free(sname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9633 | return FAIL; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 9634 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9635 | vim_free(sname); |
| 9636 | } |
| 9637 | |
| 9638 | /* |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 9639 | * If there is an empty, unnamed buffer we will wipe it out later. |
| 9640 | * Remember the buffer number. |
| 9641 | */ |
| 9642 | if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL) |
| 9643 | return FAIL; |
| 9644 | if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL) |
| 9645 | return FAIL; |
| 9646 | if (put_line(fd, "endif") == FAIL) |
| 9647 | return FAIL; |
| 9648 | |
| 9649 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9650 | * Now save the current files, current buffer first. |
| 9651 | */ |
| 9652 | if (put_line(fd, "set shortmess=aoO") == FAIL) |
| 9653 | return FAIL; |
| 9654 | |
| 9655 | /* Now put the other buffers into the buffer list */ |
| 9656 | for (buf = firstbuf; buf != NULL; buf = buf->b_next) |
| 9657 | { |
| 9658 | if (!(only_save_windows && buf->b_nwindows == 0) |
| 9659 | && !(buf->b_help && !(ssop_flags & SSOP_HELP)) |
| 9660 | && buf->b_fname != NULL |
| 9661 | && buf->b_p_bl) |
| 9662 | { |
| 9663 | if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L |
| 9664 | : buf->b_wininfo->wi_fpos.lnum) < 0 |
| 9665 | || ses_fname(fd, buf, &ssop_flags) == FAIL) |
| 9666 | return FAIL; |
| 9667 | } |
| 9668 | } |
| 9669 | |
| 9670 | /* the global argument list */ |
| 9671 | if (ses_arglist(fd, "args", &global_alist.al_ga, |
| 9672 | !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL) |
| 9673 | return FAIL; |
| 9674 | |
| 9675 | if (ssop_flags & SSOP_RESIZE) |
| 9676 | { |
| 9677 | /* Note: after the restore we still check it worked!*/ |
| 9678 | if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0 |
| 9679 | || put_eol(fd) == FAIL) |
| 9680 | return FAIL; |
| 9681 | } |
| 9682 | |
| 9683 | #ifdef FEAT_GUI |
| 9684 | if (gui.in_use && (ssop_flags & SSOP_WINPOS)) |
| 9685 | { |
| 9686 | int x, y; |
| 9687 | |
| 9688 | if (gui_mch_get_winpos(&x, &y) == OK) |
| 9689 | { |
| 9690 | /* Note: after the restore we still check it worked!*/ |
| 9691 | if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL) |
| 9692 | return FAIL; |
| 9693 | } |
| 9694 | } |
| 9695 | #endif |
| 9696 | |
| 9697 | /* |
| 9698 | * Before creating the window layout, try loading one file. If this is |
| 9699 | * aborted we don't end up with a number of useless windows. |
| 9700 | * This may have side effects! (e.g., compressed or network file). |
| 9701 | */ |
| 9702 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 9703 | { |
| 9704 | if (ses_do_win(wp) |
| 9705 | && wp->w_buffer->b_ffname != NULL |
| 9706 | && !wp->w_buffer->b_help |
| 9707 | #ifdef FEAT_QUICKFIX |
| 9708 | && !bt_nofile(wp->w_buffer) |
| 9709 | #endif |
| 9710 | ) |
| 9711 | { |
| 9712 | if (fputs("edit ", fd) < 0 |
| 9713 | || ses_fname(fd, wp->w_buffer, &ssop_flags) == FAIL) |
| 9714 | return FAIL; |
| 9715 | if (!wp->w_arg_idx_invalid) |
| 9716 | edited_win = wp; |
| 9717 | break; |
| 9718 | } |
| 9719 | } |
| 9720 | |
| 9721 | /* |
| 9722 | * Save current window layout. |
| 9723 | */ |
| 9724 | if (put_line(fd, "set splitbelow splitright") == FAIL) |
| 9725 | return FAIL; |
| 9726 | if (ses_win_rec(fd, topframe) == FAIL) |
| 9727 | return FAIL; |
| 9728 | if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL) |
| 9729 | return FAIL; |
| 9730 | if (!p_spr && put_line(fd, "set nosplitright") == FAIL) |
| 9731 | return FAIL; |
| 9732 | |
| 9733 | /* |
| 9734 | * Check if window sizes can be restored (no windows omitted). |
| 9735 | * Remember the window number of the current window after restoring. |
| 9736 | */ |
| 9737 | nr = 0; |
| 9738 | for (wp = firstwin; wp != NULL; wp = W_NEXT(wp)) |
| 9739 | { |
| 9740 | if (ses_do_win(wp)) |
| 9741 | ++nr; |
| 9742 | else |
| 9743 | restore_size = FALSE; |
| 9744 | if (curwin == wp) |
| 9745 | cnr = nr; |
| 9746 | } |
| 9747 | |
| 9748 | /* Go to the first window. */ |
| 9749 | if (put_line(fd, "wincmd t") == FAIL) |
| 9750 | return FAIL; |
| 9751 | |
| 9752 | /* |
| 9753 | * If more than one window, see if sizes can be restored. |
| 9754 | * First set 'winheight' and 'winwidth' to 1 to avoid the windows being |
| 9755 | * resized when moving between windows. |
| 9756 | * Do this before restoring the view, so that the topline and the cursor |
| 9757 | * can be set. This is done again below. |
| 9758 | */ |
| 9759 | if (put_line(fd, "set winheight=1 winwidth=1") == FAIL) |
| 9760 | return FAIL; |
| 9761 | if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL) |
| 9762 | return FAIL; |
| 9763 | |
| 9764 | /* |
| 9765 | * Restore the view of the window (options, file, cursor, etc.). |
| 9766 | */ |
| 9767 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 9768 | { |
| 9769 | if (!ses_do_win(wp)) |
| 9770 | continue; |
| 9771 | if (put_view(fd, wp, wp != edited_win, &ssop_flags) == FAIL) |
| 9772 | return FAIL; |
| 9773 | if (nr > 1 && put_line(fd, "wincmd w") == FAIL) |
| 9774 | return FAIL; |
| 9775 | } |
| 9776 | |
| 9777 | /* |
| 9778 | * Restore cursor to the current window if it's not the first one. |
| 9779 | */ |
| 9780 | if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0 || put_eol(fd) == FAIL)) |
| 9781 | return FAIL; |
| 9782 | |
| 9783 | /* |
Bram Moolenaar | 293ee4d | 2004-12-09 21:34:53 +0000 | [diff] [blame] | 9784 | * Wipe out an empty unnamed buffer we started in. |
| 9785 | */ |
| 9786 | if (put_line(fd, "if exists('s:wipebuf')") == FAIL) |
| 9787 | return FAIL; |
| 9788 | if (put_line(fd, " exe 'bwipe ' . s:wipebuf") == FAIL) |
| 9789 | return FAIL; |
| 9790 | if (put_line(fd, "endif") == FAIL) |
| 9791 | return FAIL; |
| 9792 | if (put_line(fd, "unlet! s:wipebuf") == FAIL) |
| 9793 | return FAIL; |
| 9794 | |
| 9795 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9796 | * Restore window sizes again after jumping around in windows, because the |
| 9797 | * current window has a minimum size while others may not. |
| 9798 | */ |
| 9799 | if (nr > 1 && ses_winsizes(fd, restore_size) == FAIL) |
| 9800 | return FAIL; |
| 9801 | |
| 9802 | /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */ |
| 9803 | if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s", |
| 9804 | p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL) |
| 9805 | return FAIL; |
| 9806 | |
| 9807 | /* |
| 9808 | * Lastly, execute the x.vim file if it exists. |
| 9809 | */ |
| 9810 | if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL |
| 9811 | || put_line(fd, "if file_readable(s:sx)") == FAIL |
| 9812 | || put_line(fd, " exe \"source \" . s:sx") == FAIL |
| 9813 | || put_line(fd, "endif") == FAIL) |
| 9814 | return FAIL; |
| 9815 | |
| 9816 | return OK; |
| 9817 | } |
| 9818 | |
| 9819 | static int |
| 9820 | ses_winsizes(fd, restore_size) |
| 9821 | FILE *fd; |
| 9822 | int restore_size; |
| 9823 | { |
| 9824 | int n = 0; |
| 9825 | win_T *wp; |
| 9826 | |
| 9827 | if (restore_size && (ssop_flags & SSOP_WINSIZE)) |
| 9828 | { |
| 9829 | for (wp = firstwin; wp != NULL; wp = wp->w_next) |
| 9830 | { |
| 9831 | if (!ses_do_win(wp)) |
| 9832 | continue; |
| 9833 | ++n; |
| 9834 | |
| 9835 | /* restore height when not full height */ |
| 9836 | if (wp->w_height + wp->w_status_height < topframe->fr_height |
| 9837 | && (fprintf(fd, |
| 9838 | "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)", |
| 9839 | n, (long)wp->w_height, Rows / 2, Rows) < 0 |
| 9840 | || put_eol(fd) == FAIL)) |
| 9841 | return FAIL; |
| 9842 | |
| 9843 | /* restore width when not full width */ |
| 9844 | if (wp->w_width < Columns && (fprintf(fd, |
| 9845 | "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)", |
| 9846 | n, (long)wp->w_width, Columns / 2, Columns) < 0 |
| 9847 | || put_eol(fd) == FAIL)) |
| 9848 | return FAIL; |
| 9849 | } |
| 9850 | } |
| 9851 | else |
| 9852 | { |
| 9853 | /* Just equalise window sizes */ |
| 9854 | if (put_line(fd, "wincmd =") == FAIL) |
| 9855 | return FAIL; |
| 9856 | } |
| 9857 | return OK; |
| 9858 | } |
| 9859 | |
| 9860 | /* |
| 9861 | * Write commands to "fd" to recursively create windows for frame "fr", |
| 9862 | * horizontally and vertically split. |
| 9863 | * After the commands the last window in the frame is the current window. |
| 9864 | * Returns FAIL when writing the commands to "fd" fails. |
| 9865 | */ |
| 9866 | static int |
| 9867 | ses_win_rec(fd, fr) |
| 9868 | FILE *fd; |
| 9869 | frame_T *fr; |
| 9870 | { |
| 9871 | frame_T *frc; |
| 9872 | int count = 0; |
| 9873 | |
| 9874 | if (fr->fr_layout != FR_LEAF) |
| 9875 | { |
| 9876 | /* Find first frame that's not skipped and then create a window for |
| 9877 | * each following one (first frame is already there). */ |
| 9878 | frc = ses_skipframe(fr->fr_child); |
| 9879 | if (frc != NULL) |
| 9880 | while ((frc = ses_skipframe(frc->fr_next)) != NULL) |
| 9881 | { |
| 9882 | /* Make window as big as possible so that we have lots of room |
| 9883 | * to split. */ |
| 9884 | if (put_line(fd, "wincmd _ | wincmd |") == FAIL |
| 9885 | || put_line(fd, fr->fr_layout == FR_COL |
| 9886 | ? "split" : "vsplit") == FAIL) |
| 9887 | return FAIL; |
| 9888 | ++count; |
| 9889 | } |
| 9890 | |
| 9891 | /* Go back to the first window. */ |
| 9892 | if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL |
| 9893 | ? "%dwincmd k" : "%dwincmd h", count) < 0 |
| 9894 | || put_eol(fd) == FAIL)) |
| 9895 | return FAIL; |
| 9896 | |
| 9897 | /* Recursively create frames/windows in each window of this column or |
| 9898 | * row. */ |
| 9899 | frc = ses_skipframe(fr->fr_child); |
| 9900 | while (frc != NULL) |
| 9901 | { |
| 9902 | ses_win_rec(fd, frc); |
| 9903 | frc = ses_skipframe(frc->fr_next); |
| 9904 | /* Go to next window. */ |
| 9905 | if (frc != NULL && put_line(fd, "wincmd w") == FAIL) |
| 9906 | return FAIL; |
| 9907 | } |
| 9908 | } |
| 9909 | return OK; |
| 9910 | } |
| 9911 | |
| 9912 | /* |
| 9913 | * Skip frames that don't contain windows we want to save in the Session. |
| 9914 | * Returns NULL when there none. |
| 9915 | */ |
| 9916 | static frame_T * |
| 9917 | ses_skipframe(fr) |
| 9918 | frame_T *fr; |
| 9919 | { |
| 9920 | frame_T *frc; |
| 9921 | |
| 9922 | for (frc = fr; frc != NULL; frc = frc->fr_next) |
| 9923 | if (ses_do_frame(frc)) |
| 9924 | break; |
| 9925 | return frc; |
| 9926 | } |
| 9927 | |
| 9928 | /* |
| 9929 | * Return TRUE if frame "fr" has a window somewhere that we want to save in |
| 9930 | * the Session. |
| 9931 | */ |
| 9932 | static int |
| 9933 | ses_do_frame(fr) |
| 9934 | frame_T *fr; |
| 9935 | { |
| 9936 | frame_T *frc; |
| 9937 | |
| 9938 | if (fr->fr_layout == FR_LEAF) |
| 9939 | return ses_do_win(fr->fr_win); |
| 9940 | for (frc = fr->fr_child; frc != NULL; frc = frc->fr_next) |
| 9941 | if (ses_do_frame(frc)) |
| 9942 | return TRUE; |
| 9943 | return FALSE; |
| 9944 | } |
| 9945 | |
| 9946 | /* |
| 9947 | * Return non-zero if window "wp" is to be stored in the Session. |
| 9948 | */ |
| 9949 | static int |
| 9950 | ses_do_win(wp) |
| 9951 | win_T *wp; |
| 9952 | { |
| 9953 | if (wp->w_buffer->b_fname == NULL |
| 9954 | #ifdef FEAT_QUICKFIX |
| 9955 | /* When 'buftype' is "nofile" can't restore the window contents. */ |
| 9956 | || bt_nofile(wp->w_buffer) |
| 9957 | #endif |
| 9958 | ) |
| 9959 | return (ssop_flags & SSOP_BLANK); |
| 9960 | if (wp->w_buffer->b_help) |
| 9961 | return (ssop_flags & SSOP_HELP); |
| 9962 | return TRUE; |
| 9963 | } |
| 9964 | |
| 9965 | /* |
| 9966 | * Write commands to "fd" to restore the view of a window. |
| 9967 | * Caller must make sure 'scrolloff' is zero. |
| 9968 | */ |
| 9969 | static int |
| 9970 | put_view(fd, wp, add_edit, flagp) |
| 9971 | FILE *fd; |
| 9972 | win_T *wp; |
| 9973 | int add_edit; /* add ":edit" command to view */ |
| 9974 | unsigned *flagp; /* vop_flags or ssop_flags */ |
| 9975 | { |
| 9976 | win_T *save_curwin; |
| 9977 | int f; |
| 9978 | int do_cursor; |
Bram Moolenaar | f95dc3b | 2005-05-22 22:02:25 +0000 | [diff] [blame] | 9979 | int did_next = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 9980 | |
| 9981 | /* Always restore cursor position for ":mksession". For ":mkview" only |
| 9982 | * when 'viewoptions' contains "cursor". */ |
| 9983 | do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR); |
| 9984 | |
| 9985 | /* |
| 9986 | * Local argument list. |
| 9987 | */ |
| 9988 | if (wp->w_alist == &global_alist) |
| 9989 | { |
| 9990 | if (put_line(fd, "argglobal") == FAIL) |
| 9991 | return FAIL; |
| 9992 | } |
| 9993 | else |
| 9994 | { |
| 9995 | if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga, |
| 9996 | flagp == &vop_flags |
| 9997 | || !(*flagp & SSOP_CURDIR) |
| 9998 | || wp->w_localdir != NULL, flagp) == FAIL) |
| 9999 | return FAIL; |
| 10000 | } |
| 10001 | |
Bram Moolenaar | f95dc3b | 2005-05-22 22:02:25 +0000 | [diff] [blame] | 10002 | /* Only when part of a session: restore the argument index. Some |
| 10003 | * arguments may have been deleted, check if the index is valid. */ |
| 10004 | if (wp->w_arg_idx != 0 && wp->w_arg_idx <= WARGCOUNT(wp) |
| 10005 | && flagp == &ssop_flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10006 | { |
| 10007 | if (fprintf(fd, "%ldnext", (long)wp->w_arg_idx) < 0 |
| 10008 | || put_eol(fd) == FAIL) |
| 10009 | return FAIL; |
Bram Moolenaar | f95dc3b | 2005-05-22 22:02:25 +0000 | [diff] [blame] | 10010 | did_next = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10011 | } |
| 10012 | |
| 10013 | /* Edit the file. Skip this when ":next" already did it. */ |
Bram Moolenaar | f95dc3b | 2005-05-22 22:02:25 +0000 | [diff] [blame] | 10014 | if (add_edit && (!did_next || wp->w_arg_idx_invalid)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10015 | { |
| 10016 | /* |
| 10017 | * Load the file. |
| 10018 | */ |
| 10019 | if (wp->w_buffer->b_ffname != NULL |
| 10020 | #ifdef FEAT_QUICKFIX |
| 10021 | && !bt_nofile(wp->w_buffer) |
| 10022 | #endif |
| 10023 | ) |
| 10024 | { |
| 10025 | /* |
| 10026 | * Editing a file in this buffer: use ":edit file". |
| 10027 | * This may have side effects! (e.g., compressed or network file). |
| 10028 | */ |
| 10029 | if (fputs("edit ", fd) < 0 |
| 10030 | || ses_fname(fd, wp->w_buffer, flagp) == FAIL) |
| 10031 | return FAIL; |
| 10032 | } |
| 10033 | else |
| 10034 | { |
| 10035 | /* No file in this buffer, just make it empty. */ |
| 10036 | if (put_line(fd, "enew") == FAIL) |
| 10037 | return FAIL; |
| 10038 | #ifdef FEAT_QUICKFIX |
| 10039 | if (wp->w_buffer->b_ffname != NULL) |
| 10040 | { |
| 10041 | /* The buffer does have a name, but it's not a file name. */ |
| 10042 | if (fputs("file ", fd) < 0 |
| 10043 | || ses_fname(fd, wp->w_buffer, flagp) == FAIL) |
| 10044 | return FAIL; |
| 10045 | } |
| 10046 | #endif |
| 10047 | do_cursor = FALSE; |
| 10048 | } |
| 10049 | } |
| 10050 | |
| 10051 | /* |
| 10052 | * Local mappings and abbreviations. |
| 10053 | */ |
| 10054 | if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) |
| 10055 | && makemap(fd, wp->w_buffer) == FAIL) |
| 10056 | return FAIL; |
| 10057 | |
| 10058 | /* |
| 10059 | * Local options. Need to go to the window temporarily. |
| 10060 | * Store only local values when using ":mkview" and when ":mksession" is |
| 10061 | * used and 'sessionoptions' doesn't include "options". |
| 10062 | * Some folding options are always stored when "folds" is included, |
| 10063 | * otherwise the folds would not be restored correctly. |
| 10064 | */ |
| 10065 | save_curwin = curwin; |
| 10066 | curwin = wp; |
| 10067 | curbuf = curwin->w_buffer; |
| 10068 | if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS)) |
| 10069 | f = makeset(fd, OPT_LOCAL, |
| 10070 | flagp == &vop_flags || !(*flagp & SSOP_OPTIONS)); |
| 10071 | #ifdef FEAT_FOLDING |
| 10072 | else if (*flagp & SSOP_FOLDS) |
| 10073 | f = makefoldset(fd); |
| 10074 | #endif |
| 10075 | else |
| 10076 | f = OK; |
| 10077 | curwin = save_curwin; |
| 10078 | curbuf = curwin->w_buffer; |
| 10079 | if (f == FAIL) |
| 10080 | return FAIL; |
| 10081 | |
| 10082 | #ifdef FEAT_FOLDING |
| 10083 | /* |
| 10084 | * Save Folds when 'buftype' is empty and for help files. |
| 10085 | */ |
| 10086 | if ((*flagp & SSOP_FOLDS) |
| 10087 | && wp->w_buffer->b_ffname != NULL |
Bram Moolenaar | b1b715d | 2006-01-21 22:09:43 +0000 | [diff] [blame] | 10088 | # ifdef FEAT_QUICKFIX |
| 10089 | && (*wp->w_buffer->b_p_bt == NUL || wp->w_buffer->b_help) |
| 10090 | # endif |
| 10091 | ) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10092 | { |
| 10093 | if (put_folds(fd, wp) == FAIL) |
| 10094 | return FAIL; |
| 10095 | } |
| 10096 | #endif |
| 10097 | |
| 10098 | /* |
| 10099 | * Set the cursor after creating folds, since that moves the cursor. |
| 10100 | */ |
| 10101 | if (do_cursor) |
| 10102 | { |
| 10103 | |
| 10104 | /* Restore the cursor line in the file and relatively in the |
| 10105 | * window. Don't use "G", it changes the jumplist. */ |
| 10106 | if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)", |
| 10107 | (long)wp->w_cursor.lnum, |
| 10108 | (long)(wp->w_cursor.lnum - wp->w_topline), |
| 10109 | (long)wp->w_height / 2, (long)wp->w_height) < 0 |
| 10110 | || put_eol(fd) == FAIL |
| 10111 | || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL |
| 10112 | || put_line(fd, "exe s:l") == FAIL |
| 10113 | || put_line(fd, "normal! zt") == FAIL |
| 10114 | || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0 |
| 10115 | || put_eol(fd) == FAIL) |
| 10116 | return FAIL; |
| 10117 | /* Restore the cursor column and left offset when not wrapping. */ |
| 10118 | if (wp->w_cursor.col == 0) |
| 10119 | { |
| 10120 | if (put_line(fd, "normal! 0") == FAIL) |
| 10121 | return FAIL; |
| 10122 | } |
| 10123 | else |
| 10124 | { |
| 10125 | if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0) |
| 10126 | { |
| 10127 | if (fprintf(fd, |
| 10128 | "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)", |
| 10129 | (long)wp->w_cursor.col, |
| 10130 | (long)(wp->w_cursor.col - wp->w_leftcol), |
| 10131 | (long)wp->w_width / 2, (long)wp->w_width) < 0 |
| 10132 | || put_eol(fd) == FAIL |
| 10133 | || put_line(fd, "if s:c > 0") == FAIL |
| 10134 | || fprintf(fd, |
| 10135 | " exe 'normal! 0' . s:c . 'lzs' . (%ld - s:c) . 'l'", |
| 10136 | (long)wp->w_cursor.col) < 0 |
| 10137 | || put_eol(fd) == FAIL |
| 10138 | || put_line(fd, "else") == FAIL |
| 10139 | || fprintf(fd, " normal! 0%dl", wp->w_cursor.col) < 0 |
| 10140 | || put_eol(fd) == FAIL |
| 10141 | || put_line(fd, "endif") == FAIL) |
| 10142 | return FAIL; |
| 10143 | } |
| 10144 | else |
| 10145 | { |
| 10146 | if (fprintf(fd, "normal! 0%dl", wp->w_cursor.col) < 0 |
| 10147 | || put_eol(fd) == FAIL) |
| 10148 | return FAIL; |
| 10149 | } |
| 10150 | } |
| 10151 | } |
| 10152 | |
| 10153 | /* |
| 10154 | * Local directory. |
| 10155 | */ |
| 10156 | if (wp->w_localdir != NULL) |
| 10157 | { |
| 10158 | if (fputs("lcd ", fd) < 0 |
| 10159 | || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL |
| 10160 | || put_eol(fd) == FAIL) |
| 10161 | return FAIL; |
| 10162 | } |
| 10163 | |
| 10164 | return OK; |
| 10165 | } |
| 10166 | |
| 10167 | /* |
| 10168 | * Write an argument list to the session file. |
| 10169 | * Returns FAIL if writing fails. |
| 10170 | */ |
| 10171 | static int |
| 10172 | ses_arglist(fd, cmd, gap, fullname, flagp) |
| 10173 | FILE *fd; |
| 10174 | char *cmd; |
| 10175 | garray_T *gap; |
| 10176 | int fullname; /* TRUE: use full path name */ |
| 10177 | unsigned *flagp; |
| 10178 | { |
| 10179 | int i; |
| 10180 | char_u buf[MAXPATHL]; |
| 10181 | char_u *s; |
| 10182 | |
| 10183 | if (gap->ga_len == 0) |
| 10184 | return put_line(fd, "silent! argdel *"); |
| 10185 | if (fputs(cmd, fd) < 0) |
| 10186 | return FAIL; |
| 10187 | for (i = 0; i < gap->ga_len; ++i) |
| 10188 | { |
| 10189 | /* NULL file names are skipped (only happens when out of memory). */ |
| 10190 | s = alist_name(&((aentry_T *)gap->ga_data)[i]); |
| 10191 | if (s != NULL) |
| 10192 | { |
| 10193 | if (fullname) |
| 10194 | { |
| 10195 | (void)vim_FullName(s, buf, MAXPATHL, FALSE); |
| 10196 | s = buf; |
| 10197 | } |
| 10198 | if (fputs(" ", fd) < 0 || ses_put_fname(fd, s, flagp) == FAIL) |
| 10199 | return FAIL; |
| 10200 | } |
| 10201 | } |
| 10202 | return put_eol(fd); |
| 10203 | } |
| 10204 | |
| 10205 | /* |
| 10206 | * Write a buffer name to the session file. |
| 10207 | * Also ends the line. |
| 10208 | * Returns FAIL if writing fails. |
| 10209 | */ |
| 10210 | static int |
| 10211 | ses_fname(fd, buf, flagp) |
| 10212 | FILE *fd; |
| 10213 | buf_T *buf; |
| 10214 | unsigned *flagp; |
| 10215 | { |
| 10216 | char_u *name; |
| 10217 | |
| 10218 | /* Use the short file name if the current directory is known at the time |
| 10219 | * the session file will be sourced. Don't do this for ":mkview", we |
| 10220 | * don't know the current directory. */ |
| 10221 | if (buf->b_sfname != NULL |
| 10222 | && flagp == &ssop_flags |
| 10223 | && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))) |
| 10224 | name = buf->b_sfname; |
| 10225 | else |
| 10226 | name = buf->b_ffname; |
| 10227 | if (ses_put_fname(fd, name, flagp) == FAIL || put_eol(fd) == FAIL) |
| 10228 | return FAIL; |
| 10229 | return OK; |
| 10230 | } |
| 10231 | |
| 10232 | /* |
| 10233 | * Write a file name to the session file. |
| 10234 | * Takes care of the "slash" option in 'sessionoptions' and escapes special |
| 10235 | * characters. |
| 10236 | * Returns FAIL if writing fails. |
| 10237 | */ |
| 10238 | static int |
| 10239 | ses_put_fname(fd, name, flagp) |
| 10240 | FILE *fd; |
| 10241 | char_u *name; |
| 10242 | unsigned *flagp; |
| 10243 | { |
| 10244 | char_u *sname; |
| 10245 | int retval = OK; |
| 10246 | int c; |
| 10247 | |
| 10248 | sname = home_replace_save(NULL, name); |
| 10249 | if (sname != NULL) |
| 10250 | name = sname; |
| 10251 | while (*name != NUL) |
| 10252 | { |
| 10253 | #ifdef FEAT_MBYTE |
| 10254 | { |
| 10255 | int l; |
| 10256 | |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 10257 | if (has_mbyte && (l = (*mb_ptr2len)(name)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10258 | { |
| 10259 | /* copy a multibyte char */ |
| 10260 | while (--l >= 0) |
| 10261 | { |
| 10262 | if (putc(*name, fd) != *name) |
| 10263 | retval = FAIL; |
| 10264 | ++name; |
| 10265 | } |
| 10266 | continue; |
| 10267 | } |
| 10268 | } |
| 10269 | #endif |
| 10270 | c = *name++; |
| 10271 | if (c == '\\' && (*flagp & SSOP_SLASH)) |
| 10272 | /* change a backslash to a forward slash */ |
| 10273 | c = '/'; |
| 10274 | else if ((vim_strchr(escape_chars, c) != NULL |
| 10275 | #ifdef BACKSLASH_IN_FILENAME |
| 10276 | && c != '\\' |
| 10277 | #endif |
| 10278 | ) || c == '#' || c == '%') |
| 10279 | { |
| 10280 | /* escape a special character with a backslash */ |
| 10281 | if (putc('\\', fd) != '\\') |
| 10282 | retval = FAIL; |
| 10283 | } |
| 10284 | if (putc(c, fd) != c) |
| 10285 | retval = FAIL; |
| 10286 | } |
| 10287 | vim_free(sname); |
| 10288 | return retval; |
| 10289 | } |
| 10290 | |
| 10291 | /* |
| 10292 | * ":loadview [nr]" |
| 10293 | */ |
| 10294 | static void |
| 10295 | ex_loadview(eap) |
| 10296 | exarg_T *eap; |
| 10297 | { |
| 10298 | char_u *fname; |
| 10299 | |
| 10300 | fname = get_view_file(*eap->arg); |
| 10301 | if (fname != NULL) |
| 10302 | { |
| 10303 | do_source(fname, FALSE, FALSE); |
| 10304 | vim_free(fname); |
| 10305 | } |
| 10306 | } |
| 10307 | |
| 10308 | /* |
| 10309 | * Get the name of the view file for the current buffer. |
| 10310 | */ |
| 10311 | static char_u * |
| 10312 | get_view_file(c) |
| 10313 | int c; |
| 10314 | { |
| 10315 | int len = 0; |
| 10316 | char_u *p, *s; |
| 10317 | char_u *retval; |
| 10318 | char_u *sname; |
| 10319 | |
| 10320 | if (curbuf->b_ffname == NULL) |
| 10321 | { |
| 10322 | EMSG(_(e_noname)); |
| 10323 | return NULL; |
| 10324 | } |
| 10325 | sname = home_replace_save(NULL, curbuf->b_ffname); |
| 10326 | if (sname == NULL) |
| 10327 | return NULL; |
| 10328 | |
| 10329 | /* |
| 10330 | * We want a file name without separators, because we're not going to make |
| 10331 | * a directory. |
| 10332 | * "normal" path separator -> "=+" |
| 10333 | * "=" -> "==" |
| 10334 | * ":" path separator -> "=-" |
| 10335 | */ |
| 10336 | for (p = sname; *p; ++p) |
| 10337 | if (*p == '=' || vim_ispathsep(*p)) |
| 10338 | ++len; |
| 10339 | retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9)); |
| 10340 | if (retval != NULL) |
| 10341 | { |
| 10342 | STRCPY(retval, p_vdir); |
| 10343 | add_pathsep(retval); |
| 10344 | s = retval + STRLEN(retval); |
| 10345 | for (p = sname; *p; ++p) |
| 10346 | { |
| 10347 | if (*p == '=') |
| 10348 | { |
| 10349 | *s++ = '='; |
| 10350 | *s++ = '='; |
| 10351 | } |
| 10352 | else if (vim_ispathsep(*p)) |
| 10353 | { |
| 10354 | *s++ = '='; |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 10355 | #if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(RISCOS) \ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10356 | || defined(VMS) |
| 10357 | if (*p == ':') |
| 10358 | *s++ = '-'; |
| 10359 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10360 | #endif |
Bram Moolenaar | cef9dcc | 2005-12-06 19:50:41 +0000 | [diff] [blame] | 10361 | *s++ = '+'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10362 | } |
| 10363 | else |
| 10364 | *s++ = *p; |
| 10365 | } |
| 10366 | *s++ = '='; |
| 10367 | *s++ = c; |
| 10368 | STRCPY(s, ".vim"); |
| 10369 | } |
| 10370 | |
| 10371 | vim_free(sname); |
| 10372 | return retval; |
| 10373 | } |
| 10374 | |
| 10375 | #endif /* FEAT_SESSION */ |
| 10376 | |
| 10377 | /* |
| 10378 | * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession". |
| 10379 | * Return FAIL for a write error. |
| 10380 | */ |
| 10381 | int |
| 10382 | put_eol(fd) |
| 10383 | FILE *fd; |
| 10384 | { |
| 10385 | if ( |
| 10386 | #ifdef USE_CRNL |
| 10387 | ( |
| 10388 | # ifdef MKSESSION_NL |
| 10389 | !mksession_nl && |
| 10390 | # endif |
| 10391 | (putc('\r', fd) < 0)) || |
| 10392 | #endif |
| 10393 | (putc('\n', fd) < 0)) |
| 10394 | return FAIL; |
| 10395 | return OK; |
| 10396 | } |
| 10397 | |
| 10398 | /* |
| 10399 | * Write a line to "fd". |
| 10400 | * Return FAIL for a write error. |
| 10401 | */ |
| 10402 | int |
| 10403 | put_line(fd, s) |
| 10404 | FILE *fd; |
| 10405 | char *s; |
| 10406 | { |
| 10407 | if (fputs(s, fd) < 0 || put_eol(fd) == FAIL) |
| 10408 | return FAIL; |
| 10409 | return OK; |
| 10410 | } |
| 10411 | |
| 10412 | #ifdef FEAT_VIMINFO |
| 10413 | /* |
| 10414 | * ":rviminfo" and ":wviminfo". |
| 10415 | */ |
| 10416 | static void |
| 10417 | ex_viminfo(eap) |
| 10418 | exarg_T *eap; |
| 10419 | { |
| 10420 | char_u *save_viminfo; |
| 10421 | |
| 10422 | save_viminfo = p_viminfo; |
| 10423 | if (*p_viminfo == NUL) |
| 10424 | p_viminfo = (char_u *)"'100"; |
| 10425 | if (eap->cmdidx == CMD_rviminfo) |
| 10426 | { |
| 10427 | if (read_viminfo(eap->arg, TRUE, TRUE, eap->forceit) == FAIL) |
| 10428 | EMSG(_("E195: Cannot open viminfo file for reading")); |
| 10429 | } |
| 10430 | else |
| 10431 | write_viminfo(eap->arg, eap->forceit); |
| 10432 | p_viminfo = save_viminfo; |
| 10433 | } |
| 10434 | #endif |
| 10435 | |
| 10436 | #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO) |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 10437 | /* |
| 10438 | * Make a dialog message in "buff[IOSIZE]". |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 10439 | * "format" must contain "%s". |
Bram Moolenaar | 9c13b35 | 2005-05-19 20:53:52 +0000 | [diff] [blame] | 10440 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10441 | void |
| 10442 | dialog_msg(buff, format, fname) |
| 10443 | char_u *buff; |
| 10444 | char *format; |
| 10445 | char_u *fname; |
| 10446 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10447 | if (fname == NULL) |
| 10448 | fname = (char_u *)_("Untitled"); |
Bram Moolenaar | b765d63 | 2005-06-07 21:00:02 +0000 | [diff] [blame] | 10449 | vim_snprintf((char *)buff, IOSIZE, format, fname); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10450 | } |
| 10451 | #endif |
| 10452 | |
| 10453 | /* |
| 10454 | * ":behave {mswin,xterm}" |
| 10455 | */ |
| 10456 | static void |
| 10457 | ex_behave(eap) |
| 10458 | exarg_T *eap; |
| 10459 | { |
| 10460 | if (STRCMP(eap->arg, "mswin") == 0) |
| 10461 | { |
| 10462 | set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0); |
| 10463 | set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0); |
| 10464 | set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0); |
| 10465 | set_option_value((char_u *)"keymodel", 0L, |
| 10466 | (char_u *)"startsel,stopsel", 0); |
| 10467 | } |
| 10468 | else if (STRCMP(eap->arg, "xterm") == 0) |
| 10469 | { |
| 10470 | set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0); |
| 10471 | set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0); |
| 10472 | set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0); |
| 10473 | set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0); |
| 10474 | } |
| 10475 | else |
| 10476 | EMSG2(_(e_invarg2), eap->arg); |
| 10477 | } |
| 10478 | |
| 10479 | #ifdef FEAT_AUTOCMD |
| 10480 | static int filetype_detect = FALSE; |
| 10481 | static int filetype_plugin = FALSE; |
| 10482 | static int filetype_indent = FALSE; |
| 10483 | |
| 10484 | /* |
| 10485 | * ":filetype [plugin] [indent] {on,off,detect}" |
| 10486 | * on: Load the filetype.vim file to install autocommands for file types. |
| 10487 | * off: Load the ftoff.vim file to remove all autocommands for file types. |
| 10488 | * plugin on: load filetype.vim and ftplugin.vim |
| 10489 | * plugin off: load ftplugof.vim |
| 10490 | * indent on: load filetype.vim and indent.vim |
| 10491 | * indent off: load indoff.vim |
| 10492 | */ |
| 10493 | static void |
| 10494 | ex_filetype(eap) |
| 10495 | exarg_T *eap; |
| 10496 | { |
| 10497 | char_u *arg = eap->arg; |
| 10498 | int plugin = FALSE; |
| 10499 | int indent = FALSE; |
| 10500 | |
| 10501 | if (*eap->arg == NUL) |
| 10502 | { |
| 10503 | /* Print current status. */ |
| 10504 | smsg((char_u *)"filetype detection:%s plugin:%s indent:%s", |
| 10505 | filetype_detect ? "ON" : "OFF", |
| 10506 | filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF", |
| 10507 | filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF"); |
| 10508 | return; |
| 10509 | } |
| 10510 | |
| 10511 | /* Accept "plugin" and "indent" in any order. */ |
| 10512 | for (;;) |
| 10513 | { |
| 10514 | if (STRNCMP(arg, "plugin", 6) == 0) |
| 10515 | { |
| 10516 | plugin = TRUE; |
| 10517 | arg = skipwhite(arg + 6); |
| 10518 | continue; |
| 10519 | } |
| 10520 | if (STRNCMP(arg, "indent", 6) == 0) |
| 10521 | { |
| 10522 | indent = TRUE; |
| 10523 | arg = skipwhite(arg + 6); |
| 10524 | continue; |
| 10525 | } |
| 10526 | break; |
| 10527 | } |
| 10528 | if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0) |
| 10529 | { |
| 10530 | if (*arg == 'o' || !filetype_detect) |
| 10531 | { |
Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 10532 | source_runtime((char_u *)FILETYPE_FILE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10533 | filetype_detect = TRUE; |
| 10534 | if (plugin) |
| 10535 | { |
Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 10536 | source_runtime((char_u *)FTPLUGIN_FILE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10537 | filetype_plugin = TRUE; |
| 10538 | } |
| 10539 | if (indent) |
| 10540 | { |
Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 10541 | source_runtime((char_u *)INDENT_FILE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10542 | filetype_indent = TRUE; |
| 10543 | } |
| 10544 | } |
| 10545 | if (*arg == 'd') |
| 10546 | { |
| 10547 | (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE); |
Bram Moolenaar | a3227e2 | 2006-03-08 21:32:40 +0000 | [diff] [blame] | 10548 | do_modelines(0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10549 | } |
| 10550 | } |
| 10551 | else if (STRCMP(arg, "off") == 0) |
| 10552 | { |
| 10553 | if (plugin || indent) |
| 10554 | { |
| 10555 | if (plugin) |
| 10556 | { |
Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 10557 | source_runtime((char_u *)FTPLUGOF_FILE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10558 | filetype_plugin = FALSE; |
| 10559 | } |
| 10560 | if (indent) |
| 10561 | { |
Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 10562 | source_runtime((char_u *)INDOFF_FILE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10563 | filetype_indent = FALSE; |
| 10564 | } |
| 10565 | } |
| 10566 | else |
| 10567 | { |
Bram Moolenaar | e5b8e3d | 2005-08-12 19:48:49 +0000 | [diff] [blame] | 10568 | source_runtime((char_u *)FTOFF_FILE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10569 | filetype_detect = FALSE; |
| 10570 | } |
| 10571 | } |
| 10572 | else |
| 10573 | EMSG2(_(e_invarg2), arg); |
| 10574 | } |
| 10575 | |
| 10576 | /* |
| 10577 | * ":setfiletype {name}" |
| 10578 | */ |
| 10579 | static void |
| 10580 | ex_setfiletype(eap) |
| 10581 | exarg_T *eap; |
| 10582 | { |
| 10583 | if (!did_filetype) |
| 10584 | set_option_value((char_u *)"filetype", 0L, eap->arg, OPT_LOCAL); |
| 10585 | } |
| 10586 | #endif |
| 10587 | |
| 10588 | /*ARGSUSED*/ |
| 10589 | static void |
| 10590 | ex_digraphs(eap) |
| 10591 | exarg_T *eap; |
| 10592 | { |
| 10593 | #ifdef FEAT_DIGRAPHS |
| 10594 | if (*eap->arg != NUL) |
| 10595 | putdigraph(eap->arg); |
| 10596 | else |
| 10597 | listdigraphs(); |
| 10598 | #else |
| 10599 | EMSG(_("E196: No digraphs in this version")); |
| 10600 | #endif |
| 10601 | } |
| 10602 | |
| 10603 | static void |
| 10604 | ex_set(eap) |
| 10605 | exarg_T *eap; |
| 10606 | { |
| 10607 | int flags = 0; |
| 10608 | |
| 10609 | if (eap->cmdidx == CMD_setlocal) |
| 10610 | flags = OPT_LOCAL; |
| 10611 | else if (eap->cmdidx == CMD_setglobal) |
| 10612 | flags = OPT_GLOBAL; |
| 10613 | #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) && defined(FEAT_BROWSE) |
| 10614 | if (cmdmod.browse && flags == 0) |
| 10615 | ex_options(eap); |
| 10616 | else |
| 10617 | #endif |
| 10618 | (void)do_set(eap->arg, flags); |
| 10619 | } |
| 10620 | |
| 10621 | #ifdef FEAT_SEARCH_EXTRA |
| 10622 | /* |
| 10623 | * ":nohlsearch" |
| 10624 | */ |
| 10625 | /*ARGSUSED*/ |
| 10626 | static void |
| 10627 | ex_nohlsearch(eap) |
| 10628 | exarg_T *eap; |
| 10629 | { |
| 10630 | no_hlsearch = TRUE; |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 10631 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10632 | } |
| 10633 | |
| 10634 | /* |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 10635 | * ":[N]match {group} {pattern}" |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10636 | * Sets nextcmd to the start of the next command, if any. Also called when |
| 10637 | * skipping commands to find the next command. |
| 10638 | */ |
| 10639 | static void |
| 10640 | ex_match(eap) |
| 10641 | exarg_T *eap; |
| 10642 | { |
| 10643 | char_u *p; |
| 10644 | char_u *end; |
| 10645 | int c; |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 10646 | int mi; |
| 10647 | |
| 10648 | if (eap->line2 <= 3) |
| 10649 | mi = eap->line2 - 1; |
| 10650 | else |
| 10651 | { |
| 10652 | EMSG(e_invcmd); |
| 10653 | return; |
| 10654 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10655 | |
| 10656 | /* First clear any old pattern. */ |
| 10657 | if (!eap->skip) |
| 10658 | { |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 10659 | vim_free(curwin->w_match[mi].regprog); |
| 10660 | curwin->w_match[mi].regprog = NULL; |
Bram Moolenaar | f71a3db | 2006-03-12 21:50:18 +0000 | [diff] [blame] | 10661 | redraw_later(SOME_VALID); /* always need a redraw */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10662 | } |
| 10663 | |
| 10664 | if (ends_excmd(*eap->arg)) |
| 10665 | end = eap->arg; |
| 10666 | else if ((STRNICMP(eap->arg, "none", 4) == 0 |
| 10667 | && (vim_iswhite(eap->arg[4]) || ends_excmd(eap->arg[4])))) |
| 10668 | end = eap->arg + 4; |
| 10669 | else |
| 10670 | { |
| 10671 | p = skiptowhite(eap->arg); |
| 10672 | if (!eap->skip) |
| 10673 | { |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 10674 | curwin->w_match_id[mi] = syn_namen2id(eap->arg, |
| 10675 | (int)(p - eap->arg)); |
| 10676 | if (curwin->w_match_id[mi] == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10677 | { |
| 10678 | EMSG2(_(e_nogroup), eap->arg); |
| 10679 | return; |
| 10680 | } |
| 10681 | } |
| 10682 | p = skipwhite(p); |
| 10683 | if (*p == NUL) |
| 10684 | { |
| 10685 | /* There must be two arguments. */ |
| 10686 | EMSG2(_(e_invarg2), eap->arg); |
| 10687 | return; |
| 10688 | } |
| 10689 | end = skip_regexp(p + 1, *p, TRUE, NULL); |
| 10690 | if (!eap->skip) |
| 10691 | { |
| 10692 | if (*end != NUL && !ends_excmd(*skipwhite(end + 1))) |
| 10693 | { |
| 10694 | eap->errmsg = e_trailing; |
| 10695 | return; |
| 10696 | } |
Bram Moolenaar | 7e8fd63 | 2006-02-18 22:14:51 +0000 | [diff] [blame] | 10697 | if (*end != *p) |
| 10698 | { |
| 10699 | EMSG2(_(e_invarg2), p); |
| 10700 | return; |
| 10701 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10702 | |
| 10703 | c = *end; |
| 10704 | *end = NUL; |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 10705 | curwin->w_match[mi].regprog = vim_regcomp(p + 1, RE_MAGIC); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10706 | *end = c; |
Bram Moolenaar | e1438bb | 2006-03-01 22:01:55 +0000 | [diff] [blame] | 10707 | if (curwin->w_match[mi].regprog == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 10708 | { |
| 10709 | EMSG2(_(e_invarg2), p); |
| 10710 | return; |
| 10711 | } |
| 10712 | } |
| 10713 | } |
| 10714 | eap->nextcmd = find_nextcmd(end); |
| 10715 | } |
| 10716 | #endif |
| 10717 | |
| 10718 | #ifdef FEAT_CRYPT |
| 10719 | /* |
| 10720 | * ":X": Get crypt key |
| 10721 | */ |
| 10722 | /*ARGSUSED*/ |
| 10723 | static void |
| 10724 | ex_X(eap) |
| 10725 | exarg_T *eap; |
| 10726 | { |
| 10727 | (void)get_crypt_key(TRUE, TRUE); |
| 10728 | } |
| 10729 | #endif |
| 10730 | |
| 10731 | #ifdef FEAT_FOLDING |
| 10732 | static void |
| 10733 | ex_fold(eap) |
| 10734 | exarg_T *eap; |
| 10735 | { |
| 10736 | if (foldManualAllowed(TRUE)) |
| 10737 | foldCreate(eap->line1, eap->line2); |
| 10738 | } |
| 10739 | |
| 10740 | static void |
| 10741 | ex_foldopen(eap) |
| 10742 | exarg_T *eap; |
| 10743 | { |
| 10744 | opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen, |
| 10745 | eap->forceit, FALSE); |
| 10746 | } |
| 10747 | |
| 10748 | static void |
| 10749 | ex_folddo(eap) |
| 10750 | exarg_T *eap; |
| 10751 | { |
| 10752 | linenr_T lnum; |
| 10753 | |
| 10754 | /* First set the marks for all lines closed/open. */ |
| 10755 | for (lnum = eap->line1; lnum <= eap->line2; ++lnum) |
| 10756 | if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed)) |
| 10757 | ml_setmarked(lnum); |
| 10758 | |
| 10759 | /* Execute the command on the marked lines. */ |
| 10760 | global_exe(eap->arg); |
| 10761 | ml_clearmarked(); /* clear rest of the marks */ |
| 10762 | } |
| 10763 | #endif |