Commit | Line | Data |
---|---|---|
52fa07ba | 1 | ;;; viper-ex.el --- functions implementing the Ex commands for Viper |
be010748 | 2 | |
9b70a748 | 3 | ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc. |
6c2e12f4 KH |
4 | |
5 | ;; This file is part of GNU Emacs. | |
6 | ||
7 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 | ;; it under the terms of the GNU General Public License as published by | |
9 | ;; the Free Software Foundation; either version 2, or (at your option) | |
10 | ;; any later version. | |
11 | ||
12 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ;; GNU General Public License for more details. | |
16 | ||
17 | ;; You should have received a copy of the GNU General Public License | |
52fa07ba MK |
18 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
19 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | ;; Boston, MA 02111-1307, USA. | |
6c2e12f4 | 21 | |
03fc1246 MK |
22 | ;; Code |
23 | ||
9b70a748 | 24 | (provide 'viper-ex) |
6c2e12f4 | 25 | |
03fc1246 | 26 | ;; Compiler pacifier |
52fa07ba | 27 | (defvar read-file-name-map) |
8626cfa2 MK |
28 | (defvar viper-use-register) |
29 | (defvar viper-s-string) | |
30 | (defvar viper-shift-width) | |
31 | (defvar viper-ex-history) | |
32 | (defvar viper-related-files-and-buffers-ring) | |
33 | (defvar viper-local-search-start-marker) | |
1e70790f | 34 | (defvar viper-expert-level) |
8626cfa2 MK |
35 | (defvar viper-custom-file-name) |
36 | (defvar viper-case-fold-search) | |
e36a387d | 37 | (defvar explicit-shell-file-name) |
9b70a748 | 38 | |
726e270f MK |
39 | ;; loading happens only in non-interactive compilation |
40 | ;; in order to spare non-viperized emacs from being viperized | |
41 | (if noninteractive | |
42 | (eval-when-compile | |
43 | (let ((load-path (cons (expand-file-name ".") load-path))) | |
44 | (or (featurep 'viper-util) | |
45 | (load "viper-util.el" nil nil 'nosuffix)) | |
46 | (or (featurep 'viper-keym) | |
47 | (load "viper-keym.el" nil nil 'nosuffix)) | |
48 | (or (featurep 'viper-cmd) | |
49 | (load "viper-cmd.el" nil nil 'nosuffix)) | |
50 | ))) | |
9b70a748 MK |
51 | ;; end pacifier |
52 | ||
53 | (require 'viper-util) | |
54 | ||
1e70790f MK |
55 | (defgroup viper-ex nil |
56 | "Viper support for Ex commands" | |
57 | :prefix "ex-" | |
58 | :group 'viper) | |
59 | ||
60 | ||
03fc1246 | 61 | |
6c2e12f4 | 62 | ;;; Variables |
bbe6126c | 63 | |
8626cfa2 MK |
64 | (defconst viper-ex-work-buf-name " *ex-working-space*") |
65 | (defconst viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) | |
66 | (defconst viper-ex-tmp-buf-name " *ex-tmp*") | |
52fa07ba MK |
67 | |
68 | ||
69 | ;;; Variable completion in :set command | |
70 | ||
71 | ;; The list of Ex commands. Used for completing command names. | |
72 | (defconst ex-token-alist | |
73 | '(("!") ("=") (">") ("&") ("~") | |
74 | ("yank") ("xit") ("WWrite") ("Write") ("write") ("wq") ("visual") | |
75 | ("version") ("vglobal") ("unmap") ("undo") ("tag") ("transfer") ("suspend") | |
76 | ("substitute") ("submitReport") ("stop") ("sr") ("source") ("shell") | |
77 | ("set") ("rewind") ("recover") ("read") ("quit") ("pwd") | |
78 | ("put") ("preserve") ("PreviousRelatedFile") ("RelatedFile") | |
79 | ("next") ("Next") ("move") ("mark") ("map") ("kmark") ("join") | |
80 | ("help") ("goto") ("global") ("file") ("edit") ("delete") ("copy") | |
81 | ("chdir") ("cd") ("Buffer") ("buffer") ("args")) ) | |
82 | ||
83 | ;; A-list of Ex variables that can be set using the :set command. | |
84 | (defconst ex-variable-alist | |
85 | '(("wrapscan") ("ws") ("wrapmargin") ("wm") | |
e36a387d | 86 | ("tabstop-global") ("ts-g") ("tabstop") ("ts") |
52fa07ba MK |
87 | ("showmatch") ("sm") ("shiftwidth") ("sw") ("shell") ("sh") |
88 | ("readonly") ("ro") | |
89 | ("nowrapscan") ("nows") ("noshowmatch") ("nosm") | |
90 | ("noreadonly") ("noro") ("nomagic") ("noma") | |
91 | ("noignorecase") ("noic") | |
e36a387d | 92 | ("noautoindent-global") ("noai-g") ("noautoindent") ("noai") |
52fa07ba | 93 | ("magic") ("ma") ("ignorecase") ("ic") |
e36a387d MK |
94 | ("autoindent-global") ("ai-g") ("autoindent") ("ai") |
95 | ("all") | |
52fa07ba | 96 | )) |
bbe6126c | 97 | |
bbe6126c | 98 | |
bbe6126c | 99 | |
52fa07ba MK |
100 | ;; Token recognized during parsing of Ex commands (e.g., "read", "comma") |
101 | (defvar ex-token nil) | |
102 | ||
103 | ;; Type of token. | |
104 | ;; If non-nil, gives type of address; if nil, it is a command. | |
105 | (defvar ex-token-type nil) | |
106 | ||
107 | ;; List of addresses passed to Ex command | |
108 | (defvar ex-addresses nil) | |
109 | ||
110 | ;; It seems that this flag is used only for `#', `print', and `list', which | |
111 | ;; aren't implemented. Check later. | |
112 | (defvar ex-flag nil) | |
113 | ||
114 | ;; "buffer" where Ex commands keep deleted data. | |
115 | ;; In Emacs terms, this is a register. | |
116 | (defvar ex-buffer nil) | |
117 | ||
118 | ;; Value of ex count. | |
119 | (defvar ex-count nil) | |
120 | ||
1e70790f | 121 | ;; Flag indicating that :global Ex command is being executed. |
52fa07ba | 122 | (defvar ex-g-flag nil) |
1e70790f | 123 | ;; Flag indicating that :vglobal Ex command is being executed. |
52fa07ba MK |
124 | (defvar ex-g-variant nil) |
125 | ||
126 | ;; Save reg-exp used in substitute. | |
127 | (defvar ex-reg-exp nil) | |
128 | ||
129 | ||
130 | ;; Replace pattern for substitute. | |
131 | (defvar ex-repl nil) | |
132 | ||
133 | ;; Pattern for global command. | |
134 | (defvar ex-g-pat nil) | |
135 | ||
1e70790f | 136 | (defcustom ex-unix-type-shell |
52fa07ba MK |
137 | (let ((case-fold-search t)) |
138 | (and (stringp shell-file-name) | |
139 | (string-match | |
140 | (concat | |
141 | "\\(" | |
142 | "csh$\\|csh.exe$" | |
143 | "\\|" | |
144 | "ksh$\\|ksh.exe$" | |
145 | "\\|" | |
146 | "^sh$\\|sh.exe$" | |
147 | "\\|" | |
148 | "[^a-z]sh$\\|[^a-z]sh.exe$" | |
149 | "\\|" | |
150 | "bash$\\|bash.exe$" | |
151 | "\\)") | |
152 | shell-file-name))) | |
1e70790f MK |
153 | "Is the user using a unix-type shell under a non-OS?" |
154 | :type 'string | |
155 | :group 'viper-ex) | |
52fa07ba | 156 | |
1e70790f | 157 | (defcustom ex-unix-type-shell-options |
52fa07ba MK |
158 | (let ((case-fold-search t)) |
159 | (if ex-unix-type-shell | |
160 | (cond ((string-match "\\(csh$\\|csh.exe$\\)" shell-file-name) | |
161 | "-f") ; csh: do it fast | |
162 | ((string-match "\\(bash$\\|bash.exe$\\)" shell-file-name) | |
163 | "-noprofile") ; bash: ignore .profile | |
164 | ))) | |
165 | "Options to pass to the Unix-style shell. | |
1e70790f MK |
166 | Don't put `-c' here, as it is added automatically." |
167 | :type 'string | |
168 | :group 'viper-ex) | |
52fa07ba MK |
169 | |
170 | (defvar ex-nontrivial-find-file-function | |
8626cfa2 MK |
171 | (cond (ex-unix-type-shell 'viper-ex-nontrivial-find-file-unix) |
172 | ((eq system-type 'emx) 'viper-ex-nontrivial-find-file-ms) ; OS/2 | |
173 | (viper-ms-style-os-p 'viper-ex-nontrivial-find-file-ms) ; Microsoft OS | |
174 | (viper-vms-os-p 'viper-ex-nontrivial-find-file-unix) ; VMS | |
175 | (t 'viper-ex-nontrivial-find-file-unix) ; presumably UNIX | |
52fa07ba | 176 | )) |
bbe6126c | 177 | |
52fa07ba MK |
178 | ;; Remembers the previous Ex tag. |
179 | (defvar ex-tag nil) | |
bbe6126c | 180 | |
52fa07ba MK |
181 | ;; file used by Ex commands like :r, :w, :n |
182 | (defvar ex-file nil) | |
bbe6126c | 183 | |
52fa07ba MK |
184 | ;; If t, tells Ex that this is a variant-command, i.e., w>>, r!, etc. |
185 | (defvar ex-variant nil) | |
bbe6126c | 186 | |
52fa07ba MK |
187 | ;; Specified the offset of an Ex command, such as :read. |
188 | (defvar ex-offset nil) | |
bbe6126c | 189 | |
52fa07ba MK |
190 | ;; Tells Ex that this is a w>> command. |
191 | (defvar ex-append nil) | |
bbe6126c | 192 | |
52fa07ba MK |
193 | ;; File containing the shell command to be executed at Ex prompt, |
194 | ;; e.g., :r !date | |
195 | (defvar ex-cmdfile nil) | |
bbe6126c | 196 | |
8626cfa2 | 197 | ;; flag used in viper-ex-read-file-name to indicate that we may be reading |
52fa07ba | 198 | ;; multiple file names. Used for :edit and :next |
8626cfa2 | 199 | (defvar viper-keep-reading-filename nil) |
bbe6126c | 200 | |
1e70790f | 201 | (defcustom ex-cycle-other-window t |
52fa07ba MK |
202 | "*If t, :n and :b cycles through files and buffers in other window. |
203 | Then :N and :B cycles in the current window. If nil, this behavior is | |
1e70790f MK |
204 | reversed." |
205 | :type 'boolean | |
206 | :group 'viper-ex) | |
bbe6126c | 207 | |
1e70790f MK |
208 | (defcustom ex-cycle-through-non-files nil |
209 | "*Cycle through *scratch* and other buffers that don't visit any file." | |
210 | :type 'boolean | |
211 | :group 'viper-ex) | |
bbe6126c | 212 | |
52fa07ba | 213 | ;; Last shell command executed with :! command. |
8626cfa2 | 214 | (defvar viper-ex-last-shell-com nil) |
bbe6126c | 215 | |
52fa07ba | 216 | ;; Indicates if Minibuffer was exited temporarily in Ex-command. |
8626cfa2 | 217 | (defvar viper-incomplete-ex-cmd nil) |
bbe6126c | 218 | |
52fa07ba | 219 | ;; Remembers the last ex-command prompt. |
8626cfa2 | 220 | (defvar viper-last-ex-prompt "") |
bbe6126c | 221 | |
bbe6126c | 222 | |
52fa07ba MK |
223 | ;;; Code |
224 | ||
225 | ;; Check if ex-token is an initial segment of STR | |
8626cfa2 | 226 | (defun viper-check-sub (str) |
52fa07ba MK |
227 | (let ((length (length ex-token))) |
228 | (if (and (<= length (length str)) | |
229 | (string= ex-token (substring str 0 length))) | |
230 | (setq ex-token str) | |
231 | (setq ex-token-type 'non-command)))) | |
bbe6126c | 232 | |
52fa07ba | 233 | ;; Get a complete ex command |
8626cfa2 | 234 | (defun viper-get-ex-com-subr () |
52fa07ba MK |
235 | (let (case-fold-search) |
236 | (set-mark (point)) | |
237 | (re-search-forward "[a-zA-Z][a-zA-Z]*") | |
238 | (setq ex-token-type 'command) | |
239 | (setq ex-token (buffer-substring (point) (mark t))) | |
240 | (exchange-point-and-mark) | |
241 | (cond ((looking-at "a") | |
8626cfa2 MK |
242 | (cond ((looking-at "ab") (viper-check-sub "abbreviate")) |
243 | ((looking-at "ar") (viper-check-sub "args")) | |
244 | (t (viper-check-sub "append")))) | |
245 | ((looking-at "h") (viper-check-sub "help")) | |
52fa07ba | 246 | ((looking-at "c") |
8626cfa2 MK |
247 | (cond ((looking-at "cd") (viper-check-sub "cd")) |
248 | ((looking-at "ch") (viper-check-sub "chdir")) | |
249 | ((looking-at "co") (viper-check-sub "copy")) | |
250 | (t (viper-check-sub "change")))) | |
251 | ((looking-at "d") (viper-check-sub "delete")) | |
252 | ((looking-at "b") (viper-check-sub "buffer")) | |
253 | ((looking-at "B") (viper-check-sub "Buffer")) | |
52fa07ba | 254 | ((looking-at "e") |
8626cfa2 MK |
255 | (if (looking-at "ex") (viper-check-sub "ex") |
256 | (viper-check-sub "edit"))) | |
257 | ((looking-at "f") (viper-check-sub "file")) | |
258 | ((looking-at "g") (viper-check-sub "global")) | |
259 | ((looking-at "i") (viper-check-sub "insert")) | |
260 | ((looking-at "j") (viper-check-sub "join")) | |
261 | ((looking-at "l") (viper-check-sub "list")) | |
52fa07ba | 262 | ((looking-at "m") |
8626cfa2 MK |
263 | (cond ((looking-at "map") (viper-check-sub "map")) |
264 | ((looking-at "mar") (viper-check-sub "mark")) | |
265 | (t (viper-check-sub "move")))) | |
52fa07ba MK |
266 | ((looking-at "k[a-z][^a-z]") |
267 | (setq ex-token "kmark") | |
268 | (forward-char 1) | |
269 | (exchange-point-and-mark)) ; this is canceled out by another | |
270 | ; exchange-point-and-mark at the end | |
8626cfa2 | 271 | ((looking-at "k") (viper-check-sub "kmark")) |
52fa07ba | 272 | ((looking-at "n") (if (looking-at "nu") |
8626cfa2 MK |
273 | (viper-check-sub "number") |
274 | (viper-check-sub "next"))) | |
275 | ((looking-at "N") (viper-check-sub "Next")) | |
276 | ((looking-at "o") (viper-check-sub "open")) | |
52fa07ba | 277 | ((looking-at "p") |
8626cfa2 MK |
278 | (cond ((looking-at "pre") (viper-check-sub "preserve")) |
279 | ((looking-at "pu") (viper-check-sub "put")) | |
280 | ((looking-at "pw") (viper-check-sub "pwd")) | |
281 | (t (viper-check-sub "print")))) | |
282 | ((looking-at "P") (viper-check-sub "PreviousRelatedFile")) | |
283 | ((looking-at "R") (viper-check-sub "RelatedFile")) | |
284 | ((looking-at "q") (viper-check-sub "quit")) | |
52fa07ba | 285 | ((looking-at "r") |
8626cfa2 MK |
286 | (cond ((looking-at "rec") (viper-check-sub "recover")) |
287 | ((looking-at "rew") (viper-check-sub "rewind")) | |
288 | (t (viper-check-sub "read")))) | |
52fa07ba | 289 | ((looking-at "s") |
8626cfa2 MK |
290 | (cond ((looking-at "se") (viper-check-sub "set")) |
291 | ((looking-at "sh") (viper-check-sub "shell")) | |
292 | ((looking-at "so") (viper-check-sub "source")) | |
293 | ((looking-at "sr") (viper-check-sub "sr")) | |
294 | ((looking-at "st") (viper-check-sub "stop")) | |
295 | ((looking-at "sus") (viper-check-sub "suspend")) | |
296 | ((looking-at "subm") (viper-check-sub "submitReport")) | |
297 | (t (viper-check-sub "substitute")))) | |
52fa07ba | 298 | ((looking-at "t") |
8626cfa2 MK |
299 | (if (looking-at "ta") (viper-check-sub "tag") |
300 | (viper-check-sub "transfer"))) | |
52fa07ba | 301 | ((looking-at "u") |
8626cfa2 MK |
302 | (cond ((looking-at "una") (viper-check-sub "unabbreviate")) |
303 | ((looking-at "unm") (viper-check-sub "unmap")) | |
304 | (t (viper-check-sub "undo")))) | |
52fa07ba | 305 | ((looking-at "v") |
8626cfa2 MK |
306 | (cond ((looking-at "ve") (viper-check-sub "version")) |
307 | ((looking-at "vi") (viper-check-sub "visual")) | |
308 | (t (viper-check-sub "vglobal")))) | |
52fa07ba | 309 | ((looking-at "w") |
8626cfa2 MK |
310 | (if (looking-at "wq") (viper-check-sub "wq") |
311 | (viper-check-sub "write"))) | |
52fa07ba MK |
312 | ((looking-at "W") |
313 | (if (looking-at "WW") | |
8626cfa2 MK |
314 | (viper-check-sub "WWrite") |
315 | (viper-check-sub "Write"))) | |
316 | ((looking-at "x") (viper-check-sub "xit")) | |
317 | ((looking-at "y") (viper-check-sub "yank")) | |
318 | ((looking-at "z") (viper-check-sub "z"))) | |
52fa07ba MK |
319 | (exchange-point-and-mark) |
320 | )) | |
bbe6126c | 321 | |
52fa07ba MK |
322 | ;; Get an ex-token which is either an address or a command. |
323 | ;; A token has a type, \(command, address, end-mark\), and a value | |
8626cfa2 | 324 | (defun viper-get-ex-token () |
52fa07ba | 325 | (save-window-excursion |
8626cfa2 MK |
326 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
327 | (set-buffer viper-ex-work-buf) | |
52fa07ba | 328 | (skip-chars-forward " \t|") |
96dffd25 MK |
329 | (let ((case-fold-search t)) |
330 | (cond ((looking-at "#") | |
331 | (setq ex-token-type 'command) | |
332 | (setq ex-token (char-to-string (following-char))) | |
333 | (forward-char 1)) | |
334 | ((looking-at "[a-z]") (viper-get-ex-com-subr)) | |
335 | ((looking-at "\\.") | |
336 | (forward-char 1) | |
337 | (setq ex-token-type 'dot)) | |
338 | ((looking-at "[0-9]") | |
339 | (set-mark (point)) | |
340 | (re-search-forward "[0-9]*") | |
341 | (setq ex-token-type | |
342 | (cond ((eq ex-token-type 'plus) 'add-number) | |
343 | ((eq ex-token-type 'minus) 'sub-number) | |
344 | (t 'abs-number))) | |
345 | (setq ex-token | |
346 | (string-to-int (buffer-substring (point) (mark t))))) | |
347 | ((looking-at "\\$") | |
348 | (forward-char 1) | |
349 | (setq ex-token-type 'end)) | |
350 | ((looking-at "%") | |
351 | (forward-char 1) | |
352 | (setq ex-token-type 'whole)) | |
353 | ((looking-at "+") | |
354 | (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]")) | |
355 | (forward-char 1) | |
356 | (insert "1") | |
357 | (backward-char 1) | |
52fa07ba | 358 | (setq ex-token-type 'plus)) |
96dffd25 MK |
359 | ((looking-at "+[0-9]") |
360 | (forward-char 1) | |
361 | (setq ex-token-type 'plus)) | |
362 | (t | |
363 | (error viper-BadAddress)))) | |
364 | ((looking-at "-") | |
365 | (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]")) | |
366 | (forward-char 1) | |
367 | (insert "1") | |
368 | (backward-char 1) | |
369 | (setq ex-token-type 'minus)) | |
370 | ((looking-at "-[0-9]") | |
371 | (forward-char 1) | |
372 | (setq ex-token-type 'minus)) | |
373 | (t | |
374 | (error viper-BadAddress)))) | |
375 | ((looking-at "/") | |
376 | (forward-char 1) | |
377 | (set-mark (point)) | |
378 | (let ((cont t)) | |
379 | (while (and (not (eolp)) cont) | |
380 | ;;(re-search-forward "[^/]*/") | |
381 | (re-search-forward "[^/]*\\(/\\|\n\\)") | |
382 | (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/")) | |
383 | (setq cont nil)))) | |
384 | (backward-char 1) | |
385 | (setq ex-token (buffer-substring (point) (mark t))) | |
386 | (if (looking-at "/") (forward-char 1)) | |
387 | (setq ex-token-type 'search-forward)) | |
388 | ((looking-at "\\?") | |
389 | (forward-char 1) | |
390 | (set-mark (point)) | |
391 | (let ((cont t)) | |
392 | (while (and (not (eolp)) cont) | |
393 | ;;(re-search-forward "[^\\?]*\\?") | |
394 | (re-search-forward "[^\\?]*\\(\\?\\|\n\\)") | |
395 | (if (not (viper-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?")) | |
396 | (setq cont nil)) | |
397 | (backward-char 1) | |
398 | (if (not (looking-at "\n")) (forward-char 1)))) | |
399 | (setq ex-token-type 'search-backward) | |
400 | (setq ex-token (buffer-substring (1- (point)) (mark t)))) | |
401 | ((looking-at ",") | |
402 | (forward-char 1) | |
403 | (setq ex-token-type 'comma)) | |
404 | ((looking-at ";") | |
405 | (forward-char 1) | |
406 | (setq ex-token-type 'semi-colon)) | |
407 | ((looking-at "[!=><&~]") | |
408 | (setq ex-token-type 'command) | |
409 | (setq ex-token (char-to-string (following-char))) | |
410 | (forward-char 1)) | |
411 | ((looking-at "'") | |
412 | (setq ex-token-type 'goto-mark) | |
413 | (forward-char 1) | |
414 | (cond ((looking-at "'") (setq ex-token nil)) | |
415 | ((looking-at "[a-z]") (setq ex-token (following-char))) | |
416 | (t (error "Marks are ' and a-z"))) | |
417 | (forward-char 1)) | |
418 | ((looking-at "\n") | |
419 | (setq ex-token-type 'end-mark) | |
420 | (setq ex-token "goto")) | |
421 | (t | |
422 | (error viper-BadExCommand)))))) | |
bbe6126c | 423 | |
52fa07ba MK |
424 | ;; Reads Ex command. Tries to determine if it has to exit because command |
425 | ;; is complete or invalid. If not, keeps reading command. | |
426 | (defun ex-cmd-read-exit () | |
427 | (interactive) | |
8626cfa2 | 428 | (setq viper-incomplete-ex-cmd t) |
52fa07ba MK |
429 | (let ((quit-regex1 (concat |
430 | "\\(" "set[ \t]*" | |
431 | "\\|" "edit[ \t]*" | |
432 | "\\|" "[nN]ext[ \t]*" | |
433 | "\\|" "unm[ \t]*" | |
434 | "\\|" "^[ \t]*rep" | |
435 | "\\)")) | |
436 | (quit-regex2 (concat | |
437 | "[a-zA-Z][ \t]*" | |
438 | "\\(" "!" "\\|" ">>" | |
439 | "\\|" "\\+[0-9]+" | |
440 | "\\)" | |
441 | "*[ \t]*$")) | |
442 | (stay-regex (concat | |
443 | "\\(" "^[ \t]*$" | |
1e70790f | 444 | "\\|" "[?/].*" |
52fa07ba MK |
445 | "\\|" "[ktgjmsz][ \t]*$" |
446 | "\\|" "^[ \t]*ab.*" | |
447 | "\\|" "tr[ansfer \t]*" | |
448 | "\\|" "sr[ \t]*" | |
449 | "\\|" "mo.*" | |
450 | "\\|" "^[ \t]*k?ma[^p]*" | |
451 | "\\|" "^[ \t]*fi.*" | |
452 | "\\|" "v?gl.*" | |
453 | "\\|" "[vg][ \t]*$" | |
454 | "\\|" "jo.*" | |
455 | "\\|" "^[ \t]*ta.*" | |
456 | "\\|" "^[ \t]*una.*" | |
457 | "\\|" "^[ \t]*su.*" | |
458 | "\\|['`][a-z][ \t]*" | |
459 | "\\|" "![ \t]*[a-zA-Z].*" | |
460 | "\\)" | |
461 | "!*"))) | |
462 | ||
463 | (save-window-excursion ;; put cursor at the end of the Ex working buffer | |
8626cfa2 MK |
464 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
465 | (set-buffer viper-ex-work-buf) | |
52fa07ba | 466 | (goto-char (point-max))) |
8626cfa2 MK |
467 | (cond ((viper-looking-back quit-regex1) (exit-minibuffer)) |
468 | ((viper-looking-back stay-regex) (insert " ")) | |
469 | ((viper-looking-back quit-regex2) (exit-minibuffer)) | |
52fa07ba MK |
470 | (t (insert " "))))) |
471 | ||
472 | ;; complete Ex command | |
473 | (defun ex-cmd-complete () | |
474 | (interactive) | |
475 | (let (save-pos dist compl-list string-to-complete completion-result) | |
476 | ||
477 | (save-excursion | |
478 | (setq dist (skip-chars-backward "[a-zA-Z!=>&~]") | |
479 | save-pos (point))) | |
480 | ||
481 | (if (or (= dist 0) | |
8626cfa2 MK |
482 | (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)") |
483 | (viper-looking-back | |
52fa07ba MK |
484 | "^[ \t]*[a-zA-Z!=>&~][ \t]*[/?]*+[ \t]+[a-zA-Z!=>&~]+")) |
485 | ;; Preceding characters are not the ones allowed in an Ex command | |
486 | ;; or we have typed past command name. | |
487 | ;; Note: we didn't do parsing, so there may be surprises. | |
8626cfa2 MK |
488 | (if (or (viper-looking-back "[a-zA-Z!=>&~][ \t]*[/?]*[ \t]*") |
489 | (viper-looking-back "\\([ \t]*['`][ \t]*[a-z]*\\)") | |
52fa07ba MK |
490 | (looking-at "[^ \t\n\C-m]")) |
491 | nil | |
492 | (with-output-to-temp-buffer "*Completions*" | |
493 | (display-completion-list | |
8626cfa2 | 494 | (viper-alist-to-list ex-token-alist)))) |
52fa07ba MK |
495 | ;; Preceding chars may be part of a command name |
496 | (setq string-to-complete (buffer-substring save-pos (point))) | |
497 | (setq completion-result | |
498 | (try-completion string-to-complete ex-token-alist)) | |
499 | ||
500 | (cond ((eq completion-result t) ; exact match--do nothing | |
8626cfa2 | 501 | (viper-tmp-insert-at-eob " (Sole completion)")) |
52fa07ba | 502 | ((eq completion-result nil) |
8626cfa2 | 503 | (viper-tmp-insert-at-eob " (No match)")) |
52fa07ba MK |
504 | (t ;; partial completion |
505 | (goto-char save-pos) | |
506 | (delete-region (point) (point-max)) | |
507 | (insert completion-result) | |
508 | (let (case-fold-search) | |
509 | (setq compl-list | |
8626cfa2 | 510 | (viper-filter-alist (concat "^" completion-result) |
52fa07ba MK |
511 | ex-token-alist))) |
512 | (if (> (length compl-list) 1) | |
513 | (with-output-to-temp-buffer "*Completions*" | |
514 | (display-completion-list | |
8626cfa2 | 515 | (viper-alist-to-list (reverse compl-list))))))) |
52fa07ba MK |
516 | ))) |
517 | ||
bbe6126c | 518 | |
52fa07ba | 519 | ;; Read Ex commands |
8626cfa2 | 520 | (defun viper-ex (&optional string) |
52fa07ba MK |
521 | (interactive) |
522 | (or string | |
523 | (setq ex-g-flag nil | |
524 | ex-g-variant nil)) | |
525 | (let* ((map (copy-keymap minibuffer-local-map)) | |
526 | (address nil) | |
527 | (cont t) | |
528 | (dot (point)) | |
529 | prev-token-type com-str) | |
530 | ||
8626cfa2 | 531 | (viper-add-keymap viper-ex-cmd-map map) |
52fa07ba | 532 | |
8626cfa2 | 533 | (setq com-str (or string (viper-read-string-with-history |
52fa07ba MK |
534 | ":" |
535 | nil | |
8626cfa2 MK |
536 | 'viper-ex-history |
537 | (car viper-ex-history) | |
52fa07ba MK |
538 | map))) |
539 | (save-window-excursion | |
540 | ;; just a precaution | |
8626cfa2 MK |
541 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
542 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
543 | (delete-region (point-min) (point-max)) |
544 | (insert com-str "\n") | |
545 | (goto-char (point-min))) | |
546 | (setq ex-token-type nil | |
547 | ex-addresses nil) | |
548 | (while cont | |
8626cfa2 | 549 | (viper-get-ex-token) |
52fa07ba MK |
550 | (cond ((memq ex-token-type '(command end-mark)) |
551 | (if address (setq ex-addresses (cons address ex-addresses))) | |
552 | (cond ((string= ex-token "global") | |
553 | (ex-global nil) | |
554 | (setq cont nil)) | |
555 | ((string= ex-token "vglobal") | |
556 | (ex-global t) | |
557 | (setq cont nil)) | |
558 | (t | |
8626cfa2 | 559 | (viper-execute-ex-command) |
52fa07ba | 560 | (save-window-excursion |
8626cfa2 MK |
561 | (setq viper-ex-work-buf |
562 | (get-buffer-create viper-ex-work-buf-name)) | |
563 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
564 | (skip-chars-forward " \t") |
565 | (cond ((looking-at "|") | |
566 | (forward-char 1)) | |
567 | ((looking-at "\n") | |
568 | (setq cont nil)) | |
8626cfa2 | 569 | (t (error "`%s': %s" ex-token viper-SpuriousText))) |
52fa07ba MK |
570 | )) |
571 | )) | |
572 | ((eq ex-token-type 'non-command) | |
8626cfa2 | 573 | (error "`%s': %s" ex-token viper-BadExCommand)) |
52fa07ba MK |
574 | ((eq ex-token-type 'whole) |
575 | (setq address nil) | |
576 | (setq ex-addresses | |
577 | (if ex-addresses | |
578 | (cons (point-max) ex-addresses) | |
579 | (cons (point-max) (cons (point-min) ex-addresses))))) | |
580 | ((eq ex-token-type 'comma) | |
581 | (if (eq prev-token-type 'whole) | |
582 | (setq address (point-min))) | |
583 | (setq ex-addresses | |
584 | (cons (if (null address) (point) address) ex-addresses))) | |
585 | ((eq ex-token-type 'semi-colon) | |
586 | (if (eq prev-token-type 'whole) | |
587 | (setq address (point-min))) | |
588 | (if address (setq dot address)) | |
589 | (setq ex-addresses | |
590 | (cons (if (null address) (point) address) ex-addresses))) | |
8626cfa2 | 591 | (t (let ((ans (viper-get-ex-address-subr address dot))) |
52fa07ba MK |
592 | (if ans (setq address ans))))) |
593 | (setq prev-token-type ex-token-type)))) | |
594 | ||
bbe6126c | 595 | |
52fa07ba | 596 | ;; Get a regular expression and set `ex-variant', if found |
8626cfa2 | 597 | (defun viper-get-ex-pat () |
52fa07ba | 598 | (save-window-excursion |
8626cfa2 MK |
599 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
600 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
601 | (skip-chars-forward " \t") |
602 | (if (looking-at "!") | |
603 | (progn | |
604 | (setq ex-g-variant (not ex-g-variant) | |
605 | ex-g-flag (not ex-g-flag)) | |
606 | (forward-char 1) | |
607 | (skip-chars-forward " \t"))) | |
608 | (let ((c (following-char))) | |
609 | (if (string-match "[0-9A-Za-z]" (format "%c" c)) | |
610 | (error | |
611 | "Global regexp must be inside matching non-alphanumeric chars")) | |
612 | (if (looking-at "[^\\\\\n]") | |
613 | (progn | |
614 | (forward-char 1) | |
615 | (set-mark (point)) | |
616 | (let ((cont t)) | |
617 | (while (and (not (eolp)) cont) | |
618 | (if (not (re-search-forward (format "[^%c]*%c" c c) nil t)) | |
619 | (if (member ex-token '("global" "vglobal")) | |
620 | (error | |
621 | "Missing closing delimiter for global regexp") | |
622 | (goto-char (point-max)))) | |
8626cfa2 | 623 | (if (not (viper-looking-back |
52fa07ba MK |
624 | (format "[^\\\\]\\(\\\\\\\\\\)*\\\\%c" c))) |
625 | (setq cont nil)))) | |
626 | (setq ex-token | |
627 | (if (= (mark t) (point)) "" | |
628 | (buffer-substring (1- (point)) (mark t)))) | |
1e70790f MK |
629 | (backward-char 1) |
630 | ;; if the user doesn't specify the final pattern delimiter, we're | |
631 | ;; at newline now. In this case, insert the initial delimiter | |
632 | ;; specified in variable c | |
633 | (if (looking-at "\n") | |
634 | (progn | |
635 | (insert c) | |
636 | (backward-char 1))) | |
637 | ) | |
52fa07ba MK |
638 | (setq ex-token nil)) |
639 | c))) | |
bbe6126c | 640 | |
52fa07ba | 641 | ;; get an ex command |
8626cfa2 | 642 | (defun viper-get-ex-command () |
52fa07ba | 643 | (save-window-excursion |
8626cfa2 MK |
644 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
645 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
646 | (if (looking-at "/") (forward-char 1)) |
647 | (skip-chars-forward " \t") | |
648 | (cond ((looking-at "[a-z]") | |
8626cfa2 | 649 | (viper-get-ex-com-subr) |
52fa07ba | 650 | (if (eq ex-token-type 'non-command) |
8626cfa2 | 651 | (error "`%s': %s" ex-token viper-BadExCommand))) |
52fa07ba MK |
652 | ((looking-at "[!=><&~]") |
653 | (setq ex-token (char-to-string (following-char))) | |
654 | (forward-char 1)) | |
8626cfa2 | 655 | (t (error viper-BadExCommand))))) |
52fa07ba MK |
656 | |
657 | ;; Get an Ex option g or c | |
8626cfa2 | 658 | (defun viper-get-ex-opt-gc (c) |
52fa07ba | 659 | (save-window-excursion |
8626cfa2 MK |
660 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
661 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
662 | (if (looking-at (format "%c" c)) (forward-char 1)) |
663 | (skip-chars-forward " \t") | |
664 | (cond ((looking-at "g") | |
665 | (setq ex-token "g") | |
666 | (forward-char 1) | |
667 | t) | |
668 | ((looking-at "c") | |
669 | (setq ex-token "c") | |
670 | (forward-char 1) | |
671 | t) | |
672 | (t nil)))) | |
673 | ||
674 | ;; Compute default addresses. WHOLE-FLAG means use the whole buffer | |
8626cfa2 | 675 | (defun viper-default-ex-addresses (&optional whole-flag) |
52fa07ba MK |
676 | (cond ((null ex-addresses) |
677 | (setq ex-addresses | |
678 | (if whole-flag | |
679 | (cons (point-max) (cons (point-min) nil)) | |
680 | (cons (point) (cons (point) nil))))) | |
681 | ((null (cdr ex-addresses)) | |
682 | (setq ex-addresses | |
683 | (cons (car ex-addresses) ex-addresses))))) | |
684 | ||
685 | ;; Get an ex-address as a marker and set ex-flag if a flag is found | |
8626cfa2 | 686 | (defun viper-get-ex-address () |
9b70a748 MK |
687 | (let ((address (point-marker)) |
688 | (cont t)) | |
52fa07ba MK |
689 | (setq ex-token "") |
690 | (setq ex-flag nil) | |
691 | (while cont | |
8626cfa2 | 692 | (viper-get-ex-token) |
52fa07ba MK |
693 | (cond ((eq ex-token-type 'command) |
694 | (if (member ex-token '("print" "list" "#")) | |
695 | (progn | |
696 | (setq ex-flag t | |
697 | cont nil)) | |
698 | (error "Address expected in this Ex command"))) | |
699 | ((eq ex-token-type 'end-mark) | |
700 | (setq cont nil)) | |
701 | ((eq ex-token-type 'whole) | |
702 | (error "Trailing address expected")) | |
703 | ((eq ex-token-type 'comma) | |
8626cfa2 MK |
704 | (error "`%s': %s" ex-token viper-SpuriousText)) |
705 | (t (let ((ans (viper-get-ex-address-subr address (point-marker)))) | |
52fa07ba MK |
706 | (if ans (setq address ans)))))) |
707 | address)) | |
708 | ||
709 | ;; Returns an address as a point | |
8626cfa2 | 710 | (defun viper-get-ex-address-subr (old-address dot) |
52fa07ba MK |
711 | (let ((address nil)) |
712 | (if (null old-address) (setq old-address dot)) | |
713 | (cond ((eq ex-token-type 'dot) | |
714 | (setq address dot)) | |
715 | ((eq ex-token-type 'add-number) | |
716 | (save-excursion | |
717 | (goto-char old-address) | |
718 | (forward-line (if (= old-address 0) (1- ex-token) ex-token)) | |
719 | (setq address (point-marker)))) | |
720 | ((eq ex-token-type 'sub-number) | |
721 | (save-excursion | |
722 | (goto-char old-address) | |
723 | (forward-line (- ex-token)) | |
724 | (setq address (point-marker)))) | |
725 | ((eq ex-token-type 'abs-number) | |
726 | (save-excursion | |
727 | (goto-char (point-min)) | |
728 | (if (= ex-token 0) (setq address 0) | |
729 | (forward-line (1- ex-token)) | |
730 | (setq address (point-marker))))) | |
731 | ((eq ex-token-type 'end) | |
732 | (setq address (point-max-marker))) | |
733 | ((eq ex-token-type 'plus) t) ; do nothing | |
734 | ((eq ex-token-type 'minus) t) ; do nothing | |
735 | ((eq ex-token-type 'search-forward) | |
736 | (save-excursion | |
737 | (ex-search-address t) | |
738 | (setq address (point-marker)))) | |
739 | ((eq ex-token-type 'search-backward) | |
740 | (save-excursion | |
741 | (ex-search-address nil) | |
742 | (setq address (point-marker)))) | |
743 | ((eq ex-token-type 'goto-mark) | |
744 | (save-excursion | |
745 | (if (null ex-token) | |
746 | (exchange-point-and-mark) | |
8626cfa2 | 747 | (goto-char (viper-register-to-point |
52fa07ba MK |
748 | (1+ (- ex-token ?a)) 'enforce-buffer))) |
749 | (setq address (point-marker))))) | |
750 | address)) | |
751 | ||
752 | ||
753 | ;; Search pattern and set address | |
754 | (defun ex-search-address (forward) | |
755 | (if (string= ex-token "") | |
8626cfa2 MK |
756 | (if (null viper-s-string) |
757 | (error viper-NoPrevSearch) | |
758 | (setq ex-token viper-s-string)) | |
759 | (setq viper-s-string ex-token)) | |
52fa07ba MK |
760 | (if forward |
761 | (progn | |
762 | (forward-line 1) | |
763 | (re-search-forward ex-token)) | |
764 | (forward-line -1) | |
765 | (re-search-backward ex-token))) | |
766 | ||
767 | ;; Get a buffer name and set `ex-count' and `ex-flag' if found | |
8626cfa2 | 768 | (defun viper-get-ex-buffer () |
52fa07ba MK |
769 | (setq ex-buffer nil) |
770 | (setq ex-count nil) | |
771 | (setq ex-flag nil) | |
772 | (save-window-excursion | |
8626cfa2 MK |
773 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
774 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
775 | (skip-chars-forward " \t") |
776 | (if (looking-at "[a-zA-Z]") | |
777 | (progn | |
778 | (setq ex-buffer (following-char)) | |
779 | (forward-char 1) | |
780 | (skip-chars-forward " \t"))) | |
781 | (if (looking-at "[0-9]") | |
782 | (progn | |
783 | (set-mark (point)) | |
784 | (re-search-forward "[0-9][0-9]*") | |
785 | (setq ex-count (string-to-int (buffer-substring (point) (mark t)))) | |
786 | (skip-chars-forward " \t"))) | |
787 | (if (looking-at "[pl#]") | |
788 | (progn | |
789 | (setq ex-flag t) | |
790 | (forward-char 1))) | |
791 | (if (not (looking-at "[\n|]")) | |
8626cfa2 | 792 | (error "`%s': %s" ex-token viper-SpuriousText)))) |
52fa07ba | 793 | |
8626cfa2 | 794 | (defun viper-get-ex-count () |
52fa07ba MK |
795 | (setq ex-variant nil |
796 | ex-count nil | |
797 | ex-flag nil) | |
798 | (save-window-excursion | |
8626cfa2 MK |
799 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
800 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
801 | (skip-chars-forward " \t") |
802 | (if (looking-at "!") | |
803 | (progn | |
804 | (setq ex-variant t) | |
805 | (forward-char 1))) | |
806 | (skip-chars-forward " \t") | |
807 | (if (looking-at "[0-9]") | |
808 | (progn | |
809 | (set-mark (point)) | |
810 | (re-search-forward "[0-9][0-9]*") | |
811 | (setq ex-count (string-to-int (buffer-substring (point) (mark t)))) | |
812 | (skip-chars-forward " \t"))) | |
813 | (if (looking-at "[pl#]") | |
814 | (progn | |
815 | (setq ex-flag t) | |
816 | (forward-char 1))) | |
817 | (if (not (looking-at "[\n|]")) | |
818 | (error "`%s': %s" | |
8626cfa2 MK |
819 | (buffer-substring |
820 | (point-min) (1- (point-max))) viper-BadExCommand)))) | |
52fa07ba MK |
821 | |
822 | ;; Expand \% and \# in ex command | |
823 | (defun ex-expand-filsyms (cmd buf) | |
824 | (let (cf pf ret) | |
825 | (save-excursion | |
826 | (set-buffer buf) | |
827 | (setq cf buffer-file-name) | |
828 | (setq pf (ex-next nil t))) ; this finds alternative file name | |
829 | (if (and (null cf) (string-match "[^\\]%\\|\\`%" cmd)) | |
830 | (error "No current file to substitute for `%%'")) | |
831 | (if (and (null pf) (string-match "[^\\]#\\|\\`#" cmd)) | |
832 | (error "No alternate file to substitute for `#'")) | |
833 | (save-excursion | |
8626cfa2 | 834 | (set-buffer (get-buffer-create viper-ex-tmp-buf-name)) |
52fa07ba MK |
835 | (erase-buffer) |
836 | (insert cmd) | |
837 | (goto-char (point-min)) | |
838 | (while (re-search-forward "%\\|#" nil t) | |
839 | (let ((data (match-data)) | |
840 | (char (buffer-substring (match-beginning 0) (match-end 0)))) | |
8626cfa2 | 841 | (if (viper-looking-back (concat "\\\\" char)) |
52fa07ba MK |
842 | (replace-match char) |
843 | (store-match-data data) | |
844 | (if (string= char "%") | |
845 | (replace-match cf) | |
846 | (replace-match pf))))) | |
847 | (end-of-line) | |
848 | (setq ret (buffer-substring (point-min) (point))) | |
849 | (message "%s" ret)) | |
850 | ret)) | |
851 | ||
852 | ;; Get a file name and set ex-variant, `ex-append' and `ex-offset' if found | |
8626cfa2 | 853 | (defun viper-get-ex-file () |
52fa07ba MK |
854 | (let (prompt) |
855 | (setq ex-file nil | |
856 | ex-variant nil | |
857 | ex-append nil | |
858 | ex-offset nil | |
859 | ex-cmdfile nil) | |
860 | (save-excursion | |
861 | (save-window-excursion | |
8626cfa2 MK |
862 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
863 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
864 | (skip-chars-forward " \t") |
865 | (if (looking-at "!") | |
8626cfa2 | 866 | (if (and (not (viper-looking-back "[ \t]")) |
52fa07ba MK |
867 | ;; read doesn't have a corresponding :r! form, so ! is |
868 | ;; immediately interpreted as a shell command. | |
869 | (not (string= ex-token "read"))) | |
870 | (progn | |
871 | (setq ex-variant t) | |
872 | (forward-char 1) | |
873 | (skip-chars-forward " \t")) | |
874 | (setq ex-cmdfile t) | |
875 | (forward-char 1) | |
876 | (skip-chars-forward " \t"))) | |
877 | (if (looking-at ">>") | |
878 | (progn | |
879 | (setq ex-append t | |
880 | ex-variant t) | |
881 | (forward-char 2) | |
882 | (skip-chars-forward " \t"))) | |
883 | (if (looking-at "+") | |
884 | (progn | |
885 | (forward-char 1) | |
886 | (set-mark (point)) | |
887 | (re-search-forward "[ \t\n]") | |
888 | (backward-char 1) | |
889 | (setq ex-offset (buffer-substring (point) (mark t))) | |
890 | (forward-char 1) | |
891 | (skip-chars-forward " \t"))) | |
892 | ;; this takes care of :r, :w, etc., when they get file names | |
893 | ;; from the history list | |
894 | (if (member ex-token '("read" "write" "edit" "visual" "next")) | |
895 | (progn | |
896 | (setq ex-file (buffer-substring (point) (1- (point-max)))) | |
897 | (setq ex-file | |
898 | ;; For :e, match multiple non-white strings separated | |
899 | ;; by white. For others, find the first non-white string | |
900 | (if (string-match | |
901 | (if (string= ex-token "edit") | |
902 | "[^ \t\n]+\\([ \t]+[^ \t\n]+\\)*" | |
903 | "[^ \t\n]+") | |
904 | ex-file) | |
905 | (progn | |
906 | ;; if file name comes from history, don't leave | |
907 | ;; minibuffer when the user types space | |
8626cfa2 | 908 | (setq viper-incomplete-ex-cmd nil) |
52fa07ba MK |
909 | ;; this must be the last clause in this progn |
910 | (substring ex-file (match-beginning 0) (match-end 0)) | |
911 | ) | |
912 | "")) | |
913 | ;; this leaves only the command name in the work area | |
914 | ;; file names are gone | |
915 | (delete-region (point) (1- (point-max))) | |
916 | )) | |
917 | (goto-char (point-max)) | |
918 | (skip-chars-backward " \t\n") | |
919 | (setq prompt (buffer-substring (point-min) (point))) | |
920 | )) | |
921 | ||
8626cfa2 | 922 | (setq viper-last-ex-prompt prompt) |
52fa07ba MK |
923 | |
924 | ;; If we just finished reading command, redisplay prompt | |
8626cfa2 MK |
925 | (if viper-incomplete-ex-cmd |
926 | (setq ex-file (viper-ex-read-file-name (format ":%s " prompt))) | |
52fa07ba MK |
927 | ;; file was typed in-line |
928 | (setq ex-file (or ex-file ""))) | |
929 | )) | |
bbe6126c | 930 | |
bbe6126c | 931 | |
52fa07ba MK |
932 | ;; Completes file name or exits minibuffer. If Ex command accepts multiple |
933 | ;; file names, arranges to re-enter the minibuffer. | |
8626cfa2 | 934 | (defun viper-complete-filename-or-exit () |
52fa07ba | 935 | (interactive) |
8626cfa2 | 936 | (setq viper-keep-reading-filename t) |
52fa07ba MK |
937 | ;; don't exit if directory---ex-commands don't |
938 | (cond ((ex-cmd-accepts-multiple-files-p ex-token) (exit-minibuffer)) | |
939 | ;; apparently the argument to an Ex command is | |
940 | ;; supposed to be a shell command | |
8626cfa2 | 941 | ((viper-looking-back "^[ \t]*!.*") |
52fa07ba MK |
942 | (setq ex-cmdfile t) |
943 | (insert " ")) | |
944 | (t | |
945 | (setq ex-cmdfile nil) | |
946 | (minibuffer-complete-word)))) | |
bbe6126c | 947 | |
8626cfa2 | 948 | (defun viper-handle-! () |
52fa07ba MK |
949 | (interactive) |
950 | (if (and (string= | |
8626cfa2 | 951 | (buffer-string) (viper-abbreviate-file-name default-directory)) |
52fa07ba MK |
952 | (member ex-token '("read" "write"))) |
953 | (erase-buffer)) | |
954 | (insert "!")) | |
955 | ||
956 | (defun ex-cmd-accepts-multiple-files-p (token) | |
957 | (member token '("edit" "next" "Next"))) | |
958 | ||
959 | ;; If user doesn't enter anything, then "" is returned, i.e., the | |
960 | ;; prompt-directory is not returned. | |
8626cfa2 | 961 | (defun viper-ex-read-file-name (prompt) |
52fa07ba MK |
962 | (let* ((str "") |
963 | (minibuffer-local-completion-map | |
964 | (copy-keymap minibuffer-local-completion-map)) | |
965 | beg end cont val) | |
966 | ||
8626cfa2 MK |
967 | (viper-add-keymap ex-read-filename-map |
968 | (if viper-emacs-p | |
52fa07ba MK |
969 | minibuffer-local-completion-map |
970 | read-file-name-map)) | |
971 | ||
8626cfa2 | 972 | (setq cont (setq viper-keep-reading-filename t)) |
52fa07ba | 973 | (while cont |
8626cfa2 | 974 | (setq viper-keep-reading-filename nil |
52fa07ba MK |
975 | val (read-file-name (concat prompt str) nil default-directory)) |
976 | (if (string-match " " val) | |
977 | (setq val (concat "\\\"" val "\\\""))) | |
978 | (setq str (concat str (if (equal val "") "" " ") | |
979 | val (if (equal val "") "" " "))) | |
980 | ||
981 | ;; Only edit, next, and Next commands accept multiple files. | |
8626cfa2 | 982 | ;; viper-keep-reading-filename is set in the anonymous function that is |
52fa07ba | 983 | ;; bound to " " in ex-read-filename-map. |
8626cfa2 | 984 | (setq cont (and viper-keep-reading-filename |
52fa07ba MK |
985 | (ex-cmd-accepts-multiple-files-p ex-token))) |
986 | ) | |
987 | ||
988 | (setq beg (string-match "[^ \t]" str) ; delete leading blanks | |
989 | end (string-match "[ \t]*$" str)) ; delete trailing blanks | |
990 | (if (member ex-token '("read" "write")) | |
991 | (if (string-match "[\t ]*!" str) | |
992 | ;; this is actually a shell command | |
993 | (progn | |
994 | (setq ex-cmdfile t) | |
995 | (setq beg (1+ beg)) | |
8626cfa2 MK |
996 | (setq viper-last-ex-prompt |
997 | (concat viper-last-ex-prompt " !"))))) | |
52fa07ba | 998 | (substring str (or beg 0) end))) |
bbe6126c | 999 | |
52fa07ba | 1000 | ;; Execute ex command using the value of addresses |
8626cfa2 MK |
1001 | (defun viper-execute-ex-command () |
1002 | (viper-deactivate-mark) | |
52fa07ba MK |
1003 | (cond ((string= ex-token "args") (ex-args)) |
1004 | ((string= ex-token "copy") (ex-copy nil)) | |
1005 | ((string= ex-token "cd") (ex-cd)) | |
1006 | ((string= ex-token "chdir") (ex-cd)) | |
1007 | ((string= ex-token "delete") (ex-delete)) | |
1008 | ((string= ex-token "edit") (ex-edit)) | |
8626cfa2 | 1009 | ((string= ex-token "file") (viper-info-on-file)) |
52fa07ba MK |
1010 | ((string= ex-token "goto") (ex-goto)) |
1011 | ((string= ex-token "help") (ex-help)) | |
1012 | ((string= ex-token "join") (ex-line "join")) | |
1013 | ((string= ex-token "kmark") (ex-mark)) | |
1014 | ((string= ex-token "mark") (ex-mark)) | |
1015 | ((string= ex-token "map") (ex-map)) | |
1016 | ((string= ex-token "move") (ex-copy t)) | |
1017 | ((string= ex-token "next") (ex-next ex-cycle-other-window)) | |
1018 | ((string= ex-token "Next") (ex-next (not ex-cycle-other-window))) | |
1019 | ((string= ex-token "RelatedFile") (ex-next-related-buffer 1)) | |
1020 | ((string= ex-token "put") (ex-put)) | |
1021 | ((string= ex-token "pwd") (ex-pwd)) | |
1022 | ((string= ex-token "preserve") (ex-preserve)) | |
1023 | ((string= ex-token "PreviousRelatedFile") (ex-next-related-buffer -1)) | |
1024 | ((string= ex-token "quit") (ex-quit)) | |
1025 | ((string= ex-token "read") (ex-read)) | |
1026 | ((string= ex-token "recover") (ex-recover)) | |
1027 | ((string= ex-token "rewind") (ex-rewind)) | |
8626cfa2 | 1028 | ((string= ex-token "submitReport") (viper-submit-report)) |
52fa07ba MK |
1029 | ((string= ex-token "set") (ex-set)) |
1030 | ((string= ex-token "shell") (ex-shell)) | |
1031 | ((string= ex-token "source") (ex-source)) | |
1032 | ((string= ex-token "sr") (ex-substitute t t)) | |
1033 | ((string= ex-token "substitute") (ex-substitute)) | |
1034 | ((string= ex-token "suspend") (suspend-emacs)) | |
1035 | ((string= ex-token "stop") (suspend-emacs)) | |
1036 | ((string= ex-token "transfer") (ex-copy nil)) | |
1037 | ((string= ex-token "buffer") (if ex-cycle-other-window | |
8626cfa2 MK |
1038 | (viper-switch-to-buffer-other-window) |
1039 | (viper-switch-to-buffer))) | |
52fa07ba | 1040 | ((string= ex-token "Buffer") (if ex-cycle-other-window |
8626cfa2 MK |
1041 | (viper-switch-to-buffer) |
1042 | (viper-switch-to-buffer-other-window))) | |
52fa07ba | 1043 | ((string= ex-token "tag") (ex-tag)) |
8626cfa2 | 1044 | ((string= ex-token "undo") (viper-undo)) |
52fa07ba | 1045 | ((string= ex-token "unmap") (ex-unmap)) |
8626cfa2 | 1046 | ((string= ex-token "version") (viper-version)) |
52fa07ba MK |
1047 | ((string= ex-token "visual") (ex-edit)) |
1048 | ((string= ex-token "write") (ex-write nil)) | |
1049 | ((string= ex-token "Write") (save-some-buffers)) | |
1050 | ((string= ex-token "wq") (ex-write t)) | |
1051 | ((string= ex-token "WWrite") (save-some-buffers t)) ; don't ask | |
1052 | ((string= ex-token "xit") (ex-write t)) | |
1053 | ((string= ex-token "yank") (ex-yank)) | |
1054 | ((string= ex-token "!") (ex-command)) | |
1055 | ((string= ex-token "=") (ex-line-no)) | |
1056 | ((string= ex-token ">") (ex-line "right")) | |
1057 | ((string= ex-token "<") (ex-line "left")) | |
1058 | ((string= ex-token "&") (ex-substitute t)) | |
1059 | ((string= ex-token "~") (ex-substitute t t)) | |
1060 | ((or (string= ex-token "append") | |
1061 | (string= ex-token "change") | |
1062 | (string= ex-token "insert") | |
1063 | (string= ex-token "open")) | |
1064 | (error "`%s': Obsolete command, not supported by Viper" ex-token)) | |
1065 | ((or (string= ex-token "abbreviate") | |
1066 | (string= ex-token "unabbreviate")) | |
1067 | (error | |
1068 | "`%s': Vi abbrevs are obsolete. Use the more powerful Emacs abbrevs" | |
1069 | ex-token)) | |
1070 | ((or (string= ex-token "list") | |
1071 | (string= ex-token "print") | |
1072 | (string= ex-token "z") | |
1073 | (string= ex-token "#")) | |
1074 | (error "`%s': Command not implemented in Viper" ex-token)) | |
8626cfa2 | 1075 | (t (error "`%s': %s" ex-token viper-BadExCommand)))) |
52fa07ba | 1076 | |
8626cfa2 | 1077 | (defun viper-undisplayed-files () |
52fa07ba MK |
1078 | (mapcar |
1079 | (function | |
1080 | (lambda (b) | |
1081 | (if (null (get-buffer-window b)) | |
1082 | (let ((f (buffer-file-name b))) | |
1083 | (if f f | |
1084 | (if ex-cycle-through-non-files | |
1085 | (let ((s (buffer-name b))) | |
1086 | (if (string= " " (substring s 0 1)) | |
1087 | nil | |
1088 | s)) | |
1089 | nil))) | |
1090 | nil))) | |
1091 | (buffer-list))) | |
1092 | ||
1093 | ||
1094 | (defun ex-args () | |
8626cfa2 | 1095 | (let ((l (viper-undisplayed-files)) |
52fa07ba MK |
1096 | (args "") |
1097 | (file-count 1)) | |
1098 | (while (not (null l)) | |
1099 | (if (car l) | |
1100 | (setq args (format "%s %d) %s\n" args file-count (car l)) | |
1101 | file-count (1+ file-count))) | |
1102 | (setq l (cdr l))) | |
1103 | (if (string= args "") | |
1104 | (message "All files are already displayed") | |
1105 | (save-excursion | |
1106 | (save-window-excursion | |
8626cfa2 | 1107 | (with-output-to-temp-buffer " *viper-info*" |
52fa07ba MK |
1108 | (princ "\n\nThese files are not displayed in any window.\n") |
1109 | (princ "\n=============\n") | |
1110 | (princ args) | |
1111 | (princ "\n=============\n") | |
1112 | (princ "\nThe numbers can be given as counts to :next. ") | |
1113 | (princ "\n\nPress any key to continue...\n\n")) | |
8626cfa2 | 1114 | (viper-read-event)))))) |
52fa07ba MK |
1115 | |
1116 | ;; Ex cd command. Default directory of this buffer changes | |
1117 | (defun ex-cd () | |
8626cfa2 | 1118 | (viper-get-ex-file) |
52fa07ba MK |
1119 | (if (string= ex-file "") |
1120 | (setq ex-file "~")) | |
1121 | (setq default-directory (file-name-as-directory (expand-file-name ex-file)))) | |
1122 | ||
1123 | ;; Ex copy and move command. DEL-FLAG means delete | |
1124 | (defun ex-copy (del-flag) | |
8626cfa2 MK |
1125 | (viper-default-ex-addresses) |
1126 | (let ((address (viper-get-ex-address)) | |
52fa07ba MK |
1127 | (end (car ex-addresses)) (beg (car (cdr ex-addresses)))) |
1128 | (goto-char end) | |
1129 | (save-excursion | |
1130 | (push-mark beg t) | |
8626cfa2 | 1131 | (viper-enlarge-region (mark t) (point)) |
52fa07ba MK |
1132 | (if del-flag |
1133 | (kill-region (point) (mark t)) | |
1134 | (copy-region-as-kill (point) (mark t))) | |
1135 | (if ex-flag | |
1136 | (progn | |
1137 | (with-output-to-temp-buffer "*copy text*" | |
1138 | (princ | |
1139 | (if (or del-flag ex-g-flag ex-g-variant) | |
1140 | (current-kill 0) | |
1141 | (buffer-substring (point) (mark t))))) | |
1142 | (condition-case nil | |
1143 | (progn | |
1144 | (read-string "[Hit return to continue] ") | |
1145 | (save-excursion (kill-buffer "*copy text*"))) | |
1146 | (quit (save-excursion (kill-buffer "*copy text*")) | |
1147 | (signal 'quit nil)))))) | |
1148 | (if (= address 0) | |
1149 | (goto-char (point-min)) | |
1150 | (goto-char address) | |
1151 | (forward-line 1)) | |
1152 | (insert (current-kill 0)))) | |
1153 | ||
1154 | ;; Ex delete command | |
1155 | (defun ex-delete () | |
8626cfa2 MK |
1156 | (viper-default-ex-addresses) |
1157 | (viper-get-ex-buffer) | |
52fa07ba | 1158 | (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) |
8626cfa2 | 1159 | (if (> beg end) (error viper-FirstAddrExceedsSecond)) |
52fa07ba | 1160 | (save-excursion |
8626cfa2 | 1161 | (viper-enlarge-region beg end) |
52fa07ba MK |
1162 | (exchange-point-and-mark) |
1163 | (if ex-count | |
1164 | (progn | |
1165 | (set-mark (point)) | |
1166 | (forward-line (1- ex-count))) | |
1167 | (set-mark end)) | |
8626cfa2 | 1168 | (viper-enlarge-region (point) (mark t)) |
52fa07ba MK |
1169 | (if ex-flag |
1170 | ;; show text to be deleted and ask for confirmation | |
1171 | (progn | |
1172 | (with-output-to-temp-buffer " *delete text*" | |
1173 | (princ (buffer-substring (point) (mark t)))) | |
1174 | (condition-case nil | |
1175 | (read-string "[Hit return to continue] ") | |
1176 | (quit | |
1177 | (save-excursion (kill-buffer " *delete text*")) | |
1178 | (error ""))) | |
1179 | (save-excursion (kill-buffer " *delete text*"))) | |
1180 | (if ex-buffer | |
8626cfa2 MK |
1181 | (cond ((viper-valid-register ex-buffer '(Letter)) |
1182 | (viper-append-to-register | |
52fa07ba | 1183 | (downcase ex-buffer) (point) (mark t))) |
8626cfa2 | 1184 | ((viper-valid-register ex-buffer) |
52fa07ba | 1185 | (copy-to-register ex-buffer (point) (mark t) nil)) |
8626cfa2 | 1186 | (t (error viper-InvalidRegister ex-buffer)))) |
52fa07ba MK |
1187 | (kill-region (point) (mark t)))))) |
1188 | ||
1189 | ||
1190 | ||
1191 | ;; Ex edit command | |
1192 | ;; In Viper, `e' and `e!' behave identically. In both cases, the user is | |
1193 | ;; asked if current buffer should really be discarded. | |
1194 | ;; This command can take multiple file names. It replaces the current buffer | |
1195 | ;; with the first file in its argument list | |
1196 | (defun ex-edit (&optional file) | |
1197 | (if (not file) | |
8626cfa2 | 1198 | (viper-get-ex-file)) |
52fa07ba | 1199 | (cond ((and (string= ex-file "") buffer-file-name) |
8626cfa2 | 1200 | (setq ex-file (viper-abbreviate-file-name (buffer-file-name)))) |
52fa07ba | 1201 | ((string= ex-file "") |
8626cfa2 | 1202 | (error viper-NoFileSpecified))) |
52fa07ba MK |
1203 | |
1204 | (let (msg do-edit) | |
1205 | (if buffer-file-name | |
1206 | (cond ((buffer-modified-p) | |
1207 | (setq msg | |
1208 | (format "Buffer %s is modified. Discard changes? " | |
1209 | (buffer-name)) | |
1210 | do-edit t)) | |
1211 | ((not (verify-visited-file-modtime (current-buffer))) | |
1212 | (setq msg | |
1213 | (format "File %s changed on disk. Reread from disk? " | |
1214 | buffer-file-name) | |
1215 | do-edit t)) | |
1216 | (t (setq do-edit nil)))) | |
1217 | ||
1218 | (if do-edit | |
1219 | (if (yes-or-no-p msg) | |
1220 | (progn | |
1221 | (set-buffer-modified-p nil) | |
1222 | (kill-buffer (current-buffer))) | |
1223 | (message "Buffer %s was left intact" (buffer-name)))) | |
1224 | ) ; let | |
1225 | ||
1226 | (if (null (setq file (get-file-buffer ex-file))) | |
1227 | (progn | |
1228 | (ex-find-file ex-file) | |
ab124470 | 1229 | (or (eq major-mode 'dired-mode) |
8626cfa2 | 1230 | (viper-change-state-to-vi)) |
52fa07ba MK |
1231 | (goto-char (point-min))) |
1232 | (switch-to-buffer file)) | |
1233 | (if ex-offset | |
1234 | (progn | |
1235 | (save-window-excursion | |
8626cfa2 MK |
1236 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
1237 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
1238 | (delete-region (point-min) (point-max)) |
1239 | (insert ex-offset "\n") | |
1240 | (goto-char (point-min))) | |
8626cfa2 | 1241 | (goto-char (viper-get-ex-address)) |
52fa07ba | 1242 | (beginning-of-line))) |
8626cfa2 | 1243 | (ex-fixup-history viper-last-ex-prompt ex-file)) |
52fa07ba | 1244 | |
ab124470 MK |
1245 | ;; Find-file FILESPEC if it appears to specify a single file. |
1246 | ;; Otherwise, assume that FILES{EC is a wildcard. | |
1247 | ;; In this case, split it into substrings separated by newlines. | |
52fa07ba MK |
1248 | ;; Each line is assumed to be a file name. find-file's each file thus obtained. |
1249 | (defun ex-find-file (filespec) | |
ab124470 MK |
1250 | (let ((nonstandard-filename-chars "[^-a-zA-Z0-9_./,~$\\]")) |
1251 | (cond ((file-exists-p filespec) (find-file filespec)) | |
1252 | ((string-match nonstandard-filename-chars filespec) | |
1253 | (funcall ex-nontrivial-find-file-function filespec)) | |
1254 | (t (find-file filespec))) | |
52fa07ba | 1255 | )) |
bbe6126c | 1256 | |
6c2e12f4 | 1257 | |
52fa07ba | 1258 | ;; Ex global command |
1e70790f MK |
1259 | ;; This is executed in response to: |
1260 | ;; :global "pattern" ex-command | |
1261 | ;; :vglobal "pattern" ex-command | |
1262 | ;; :global executes ex-command on all lines matching <pattern> | |
1263 | ;; :vglobal executes ex-command on all lines that don't match <pattern> | |
1264 | ;; | |
1265 | ;; With VARIANT nil, this functions executes :global | |
1266 | ;; With VARIANT t, executes :vglobal | |
52fa07ba MK |
1267 | (defun ex-global (variant) |
1268 | (let ((gcommand ex-token)) | |
1269 | (if (or ex-g-flag ex-g-variant) | |
1270 | (error "`%s' within `global' is not allowed" gcommand) | |
1271 | (if variant | |
1272 | (setq ex-g-flag nil | |
1273 | ex-g-variant t) | |
1274 | (setq ex-g-flag t | |
1275 | ex-g-variant nil))) | |
8626cfa2 | 1276 | (viper-get-ex-pat) |
52fa07ba MK |
1277 | (if (null ex-token) |
1278 | (error "`%s': Missing regular expression" gcommand))) | |
1279 | ||
1280 | (if (string= ex-token "") | |
8626cfa2 MK |
1281 | (if (null viper-s-string) |
1282 | (error viper-NoPrevSearch) | |
1283 | (setq ex-g-pat viper-s-string)) | |
52fa07ba | 1284 | (setq ex-g-pat ex-token |
8626cfa2 | 1285 | viper-s-string ex-token)) |
52fa07ba MK |
1286 | (if (null ex-addresses) |
1287 | (setq ex-addresses (list (point-max) (point-min))) | |
8626cfa2 | 1288 | (viper-default-ex-addresses)) |
1e70790f MK |
1289 | (let ((marks nil) |
1290 | (mark-count 0) | |
1291 | (end (car ex-addresses)) | |
1292 | (beg (car (cdr ex-addresses))) | |
1293 | com-str) | |
8626cfa2 | 1294 | (if (> beg end) (error viper-FirstAddrExceedsSecond)) |
52fa07ba | 1295 | (save-excursion |
8626cfa2 | 1296 | (viper-enlarge-region beg end) |
52fa07ba MK |
1297 | (exchange-point-and-mark) |
1298 | (let ((cont t) (limit (point-marker))) | |
1299 | (exchange-point-and-mark) | |
1300 | ;; skip the last line if empty | |
1301 | (beginning-of-line) | |
8626cfa2 | 1302 | (if (eobp) (viper-backward-char-carefully)) |
52fa07ba MK |
1303 | (while (and cont (not (bobp)) (>= (point) limit)) |
1304 | (beginning-of-line) | |
1305 | (set-mark (point)) | |
1306 | (end-of-line) | |
1307 | (let ((found (re-search-backward ex-g-pat (mark t) t))) | |
1308 | (if (or (and ex-g-flag found) | |
1309 | (and ex-g-variant (not found))) | |
1310 | (progn | |
1311 | (end-of-line) | |
1312 | (setq mark-count (1+ mark-count)) | |
1313 | (setq marks (cons (point-marker) marks))))) | |
1314 | (beginning-of-line) | |
1315 | (if (bobp) (setq cont nil) | |
1316 | (forward-line -1) | |
1317 | (end-of-line))))) | |
1318 | (save-window-excursion | |
8626cfa2 MK |
1319 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
1320 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
1321 | (setq com-str (buffer-substring (1+ (point)) (1- (point-max))))) |
1322 | (while marks | |
1323 | (goto-char (car marks)) | |
8626cfa2 | 1324 | (viper-ex com-str) |
52fa07ba MK |
1325 | (setq mark-count (1- mark-count)) |
1326 | (setq marks (cdr marks))))) | |
1327 | ||
1328 | ;; Ex goto command | |
1329 | (defun ex-goto () | |
1330 | (if (null ex-addresses) | |
1331 | (setq ex-addresses (cons (point) nil))) | |
1332 | (push-mark (point) t) | |
1333 | (goto-char (car ex-addresses)) | |
1334 | (beginning-of-line)) | |
1335 | ||
1336 | ;; Ex line commands. COM is join, shift-right or shift-left | |
1337 | (defun ex-line (com) | |
8626cfa2 MK |
1338 | (viper-default-ex-addresses) |
1339 | (viper-get-ex-count) | |
52fa07ba | 1340 | (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point) |
8626cfa2 | 1341 | (if (> beg end) (error viper-FirstAddrExceedsSecond)) |
52fa07ba | 1342 | (save-excursion |
8626cfa2 | 1343 | (viper-enlarge-region beg end) |
52fa07ba MK |
1344 | (exchange-point-and-mark) |
1345 | (if ex-count | |
1346 | (progn | |
1347 | (set-mark (point)) | |
1348 | (forward-line ex-count))) | |
1349 | (if ex-flag | |
1350 | ;; show text to be joined and ask for confirmation | |
1351 | (progn | |
1352 | (with-output-to-temp-buffer " *text*" | |
1353 | (princ (buffer-substring (point) (mark t)))) | |
1354 | (condition-case nil | |
1355 | (progn | |
1356 | (read-string "[Hit return to continue] ") | |
1357 | (ex-line-subr com (point) (mark t))) | |
1358 | (quit (ding))) | |
1359 | (save-excursion (kill-buffer " *text*"))) | |
1360 | (ex-line-subr com (point) (mark t))) | |
1361 | (setq point (point))) | |
1362 | (goto-char (1- point)) | |
1363 | (beginning-of-line))) | |
1364 | ||
1365 | (defun ex-line-subr (com beg end) | |
1366 | (cond ((string= com "join") | |
1367 | (goto-char (min beg end)) | |
1368 | (while (and (not (eobp)) (< (point) (max beg end))) | |
1369 | (end-of-line) | |
1370 | (if (and (<= (point) (max beg end)) (not (eobp))) | |
1371 | (progn | |
1372 | (forward-line 1) | |
1373 | (delete-region (point) (1- (point))) | |
1374 | (if (not ex-variant) (fixup-whitespace)))))) | |
1375 | ((or (string= com "right") (string= com "left")) | |
1376 | (indent-rigidly | |
1377 | (min beg end) (max beg end) | |
8626cfa2 | 1378 | (if (string= com "right") viper-shift-width (- viper-shift-width))) |
52fa07ba MK |
1379 | (goto-char (max beg end)) |
1380 | (end-of-line) | |
8626cfa2 | 1381 | (viper-forward-char-carefully)))) |
52fa07ba MK |
1382 | |
1383 | ||
1384 | ;; Ex mark command | |
1385 | (defun ex-mark () | |
1386 | (let (char) | |
1387 | (if (null ex-addresses) | |
1388 | (setq ex-addresses | |
1389 | (cons (point) nil))) | |
1390 | (save-window-excursion | |
8626cfa2 MK |
1391 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
1392 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
1393 | (skip-chars-forward " \t") |
1394 | (if (looking-at "[a-z]") | |
1395 | (progn | |
1396 | (setq char (following-char)) | |
1397 | (forward-char 1) | |
1398 | (skip-chars-forward " \t") | |
1399 | (if (not (looking-at "[\n|]")) | |
8626cfa2 | 1400 | (error "`%s': %s" ex-token viper-SpuriousText))) |
52fa07ba MK |
1401 | (error "`%s' requires a following letter" ex-token))) |
1402 | (save-excursion | |
1403 | (goto-char (car ex-addresses)) | |
1404 | (point-to-register (1+ (- char ?a)))))) | |
6c2e12f4 | 1405 | |
52fa07ba MK |
1406 | |
1407 | ||
1408 | ;; Alternate file is the file next to the first one in the buffer ring | |
1409 | (defun ex-next (cycle-other-window &optional find-alt-file) | |
1410 | (catch 'ex-edit | |
1411 | (let (count l) | |
1412 | (if (not find-alt-file) | |
bbe6126c | 1413 | (progn |
8626cfa2 | 1414 | (viper-get-ex-file) |
52fa07ba MK |
1415 | (if (or (char-or-string-p ex-offset) |
1416 | (and (not (string= "" ex-file)) | |
1417 | (not (string-match "^[0-9]+$" ex-file)))) | |
bbe6126c | 1418 | (progn |
52fa07ba MK |
1419 | (ex-edit t) |
1420 | (throw 'ex-edit nil)) | |
1421 | (setq count (string-to-int ex-file)) | |
1422 | (if (= count 0) (setq count 1)) | |
1423 | (if (< count 0) (error "Usage: `next <count>' (count >= 0)")))) | |
1424 | (setq count 1)) | |
8626cfa2 | 1425 | (setq l (viper-undisplayed-files)) |
52fa07ba MK |
1426 | (while (> count 0) |
1427 | (while (and (not (null l)) (null (car l))) | |
1428 | (setq l (cdr l))) | |
1429 | (setq count (1- count)) | |
1430 | (if (> count 0) | |
1431 | (setq l (cdr l)))) | |
1432 | (if find-alt-file (car l) | |
1433 | (progn | |
1434 | (if (and (car l) (get-file-buffer (car l))) | |
1435 | (let* ((w (if cycle-other-window | |
1436 | (get-lru-window) (selected-window))) | |
1437 | (b (window-buffer w))) | |
1438 | (set-window-buffer w (get-file-buffer (car l))) | |
1439 | (bury-buffer b) | |
1440 | ;; this puts "next <count>" in the ex-command history | |
8626cfa2 | 1441 | (ex-fixup-history viper-last-ex-prompt ex-file)) |
52fa07ba MK |
1442 | (error "Not that many undisplayed files"))))))) |
1443 | ||
1444 | ||
1445 | (defun ex-next-related-buffer (direction &optional no-recursion) | |
1446 | ||
8626cfa2 | 1447 | (viper-ring-rotate1 viper-related-files-and-buffers-ring direction) |
52fa07ba MK |
1448 | |
1449 | (let ((file-or-buffer-name | |
8626cfa2 MK |
1450 | (viper-current-ring-item viper-related-files-and-buffers-ring)) |
1451 | (old-ring viper-related-files-and-buffers-ring) | |
52fa07ba MK |
1452 | (old-win (selected-window)) |
1453 | skip-rest buf wind) | |
1454 | ||
8626cfa2 MK |
1455 | (or (and (ring-p viper-related-files-and-buffers-ring) |
1456 | (> (ring-length viper-related-files-and-buffers-ring) 0)) | |
52fa07ba MK |
1457 | (error "This buffer has no related files or buffers")) |
1458 | ||
1459 | (or (stringp file-or-buffer-name) | |
1460 | (error | |
1461 | "File and buffer names must be strings, %S" file-or-buffer-name)) | |
1462 | ||
1463 | (setq buf (cond ((get-buffer file-or-buffer-name)) | |
1464 | ((file-exists-p file-or-buffer-name) | |
1465 | (find-file-noselect file-or-buffer-name)) | |
1466 | )) | |
1467 | ||
8626cfa2 | 1468 | (if (not (viper-buffer-live-p buf)) |
52fa07ba MK |
1469 | (error "Didn't find buffer %S or file %S" |
1470 | file-or-buffer-name | |
8626cfa2 | 1471 | (viper-abbreviate-file-name |
52fa07ba MK |
1472 | (expand-file-name file-or-buffer-name)))) |
1473 | ||
1474 | (if (equal buf (current-buffer)) | |
1475 | (or no-recursion | |
1476 | ;; try again | |
1477 | (progn | |
1478 | (setq skip-rest t) | |
1479 | (ex-next-related-buffer direction 'norecursion)))) | |
1480 | ||
1481 | (if skip-rest | |
1482 | () | |
1483 | ;; setup buffer | |
8626cfa2 | 1484 | (if (setq wind (viper-get-visible-buffer-window buf)) |
52fa07ba | 1485 | () |
8626cfa2 | 1486 | (setq wind (get-lru-window (if viper-xemacs-p nil 'visible))) |
52fa07ba | 1487 | (set-window-buffer wind buf)) |
bbe6126c | 1488 | |
8626cfa2 | 1489 | (if (viper-window-display-p) |
52fa07ba MK |
1490 | (progn |
1491 | (raise-frame (window-frame wind)) | |
1492 | (if (equal (window-frame wind) (window-frame old-win)) | |
1493 | (save-window-excursion (select-window wind) (sit-for 1)) | |
1494 | (select-window wind))) | |
1495 | (save-window-excursion (select-window wind) (sit-for 1))) | |
1496 | ||
1497 | (save-excursion | |
1498 | (set-buffer buf) | |
8626cfa2 | 1499 | (setq viper-related-files-and-buffers-ring old-ring)) |
bbe6126c | 1500 | |
8626cfa2 | 1501 | (setq viper-local-search-start-marker (point-marker)) |
52fa07ba | 1502 | ))) |
bbe6126c | 1503 | |
52fa07ba MK |
1504 | |
1505 | ;; Force auto save | |
1506 | (defun ex-preserve () | |
1507 | (message "Autosaving all buffers that need to be saved...") | |
1508 | (do-auto-save t)) | |
1509 | ||
1510 | ;; Ex put | |
1511 | (defun ex-put () | |
1512 | (let ((point (if (null ex-addresses) (point) (car ex-addresses)))) | |
8626cfa2 MK |
1513 | (viper-get-ex-buffer) |
1514 | (setq viper-use-register ex-buffer) | |
52fa07ba | 1515 | (goto-char point) |
8626cfa2 | 1516 | (if (bobp) (viper-Put-back 1) (viper-put-back 1)))) |
52fa07ba MK |
1517 | |
1518 | ;; Ex print working directory | |
1519 | (defun ex-pwd () | |
1520 | (message default-directory)) | |
1521 | ||
1522 | ;; Ex quit command | |
1523 | (defun ex-quit () | |
1524 | ;; skip "!", if it is q!. In Viper q!, w!, etc., behave as q, w, etc. | |
1525 | (save-excursion | |
8626cfa2 MK |
1526 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
1527 | (set-buffer viper-ex-work-buf) | |
52fa07ba | 1528 | (if (looking-at "!") (forward-char 1))) |
1e70790f | 1529 | (if (< viper-expert-level 3) |
52fa07ba MK |
1530 | (save-buffers-kill-emacs) |
1531 | (kill-buffer (current-buffer)))) | |
6c2e12f4 | 1532 | |
6c2e12f4 | 1533 | |
52fa07ba MK |
1534 | ;; Ex read command |
1535 | (defun ex-read () | |
8626cfa2 | 1536 | (viper-get-ex-file) |
52fa07ba MK |
1537 | (let ((point (if (null ex-addresses) (point) (car ex-addresses))) |
1538 | command) | |
1539 | (goto-char point) | |
8626cfa2 | 1540 | (viper-add-newline-at-eob-if-necessary) |
52fa07ba MK |
1541 | (if (not (or (bobp) (eobp))) (forward-line 1)) |
1542 | (if (and (not ex-variant) (string= ex-file "")) | |
1543 | (progn | |
1544 | (if (null buffer-file-name) | |
8626cfa2 | 1545 | (error viper-NoFileSpecified)) |
52fa07ba MK |
1546 | (setq ex-file buffer-file-name))) |
1547 | (if ex-cmdfile | |
1548 | (progn | |
1549 | (setq command (ex-expand-filsyms ex-file (current-buffer))) | |
1550 | (shell-command command t)) | |
1551 | (insert-file-contents ex-file))) | |
8626cfa2 | 1552 | (ex-fixup-history viper-last-ex-prompt ex-file)) |
52fa07ba MK |
1553 | |
1554 | ;; this function fixes ex-history for some commands like ex-read, ex-edit | |
1555 | (defun ex-fixup-history (&rest args) | |
8626cfa2 MK |
1556 | (setq viper-ex-history |
1557 | (cons (mapconcat 'identity args " ") (cdr viper-ex-history)))) | |
52fa07ba MK |
1558 | |
1559 | ||
1560 | ;; Ex recover from emacs \#file\# | |
1561 | (defun ex-recover () | |
8626cfa2 | 1562 | (viper-get-ex-file) |
52fa07ba | 1563 | (if (or ex-append ex-offset) |
8626cfa2 | 1564 | (error "`recover': %s" viper-SpuriousText)) |
52fa07ba MK |
1565 | (if (string= ex-file "") |
1566 | (progn | |
1567 | (if (null buffer-file-name) | |
1568 | (error "This buffer isn't visiting any file")) | |
1569 | (setq ex-file buffer-file-name)) | |
1570 | (setq ex-file (expand-file-name ex-file))) | |
1571 | (if (and (not (string= ex-file (buffer-file-name))) | |
1572 | (buffer-modified-p) | |
1573 | (not ex-variant)) | |
1574 | (error "No write since last change \(:rec! overrides\)")) | |
1575 | (recover-file ex-file)) | |
1576 | ||
1577 | ;; Tell that `rewind' is obsolete and to use `:next count' instead | |
1578 | (defun ex-rewind () | |
1579 | (message | |
1580 | "Use `:n <count>' instead. Counts are obtained from the `:args' command")) | |
1581 | ||
1582 | ||
1583 | ;; read variable name for ex-set | |
1584 | (defun ex-set-read-variable () | |
1585 | (let ((minibuffer-local-completion-map | |
1586 | (copy-keymap minibuffer-local-completion-map)) | |
1587 | (cursor-in-echo-area t) | |
1588 | str batch) | |
1589 | (define-key | |
1590 | minibuffer-local-completion-map " " 'minibuffer-complete-and-exit) | |
1591 | (define-key minibuffer-local-completion-map "=" 'exit-minibuffer) | |
8626cfa2 | 1592 | (if (viper-set-unread-command-events |
52fa07ba MK |
1593 | (ex-get-inline-cmd-args "[ \t]*[a-zA-Z]*[ \t]*" nil "\C-m")) |
1594 | (progn | |
1595 | (setq batch t) | |
8626cfa2 | 1596 | (viper-set-unread-command-events ?\C-m))) |
52fa07ba MK |
1597 | (message ":set <Variable> [= <Value>]") |
1598 | (or batch (sit-for 2)) | |
1599 | ||
1600 | (while (string-match "^[ \\t\\n]*$" | |
1601 | (setq str | |
1602 | (completing-read ":set " ex-variable-alist))) | |
e36a387d | 1603 | (message ":set <Variable> [= <Value>]") |
52fa07ba | 1604 | ;; if there are unread events, don't wait |
8626cfa2 | 1605 | (or (viper-set-unread-command-events "") (sit-for 2)) |
52fa07ba MK |
1606 | ) ; while |
1607 | str)) | |
1608 | ||
1609 | ||
1610 | (defun ex-set () | |
1611 | (let ((var (ex-set-read-variable)) | |
1612 | (val 0) | |
1613 | (set-cmd "setq") | |
1614 | (ask-if-save t) | |
1615 | (auto-cmd-label "; don't touch or else...") | |
1616 | (delete-turn-on-auto-fill-pattern | |
8626cfa2 | 1617 | "([ \t]*add-hook[ \t]+'viper-insert-state-hooks[ \t]+'turn-on-auto-fill.*)") |
52fa07ba MK |
1618 | actual-lisp-cmd lisp-cmd-del-pattern |
1619 | val2 orig-var) | |
1620 | (setq orig-var var) | |
e36a387d MK |
1621 | (cond ((string= var "all") |
1622 | (setq ask-if-save nil | |
1623 | set-cmd nil)) | |
1624 | ((member var '("ai" "autoindent")) | |
8626cfa2 | 1625 | (setq var "viper-auto-indent" |
52fa07ba MK |
1626 | set-cmd "setq" |
1627 | ask-if-save nil | |
1628 | val "t")) | |
e36a387d | 1629 | ((member var '("ai-g" "autoindent-global")) |
8626cfa2 MK |
1630 | (kill-local-variable 'viper-auto-indent) |
1631 | (setq var "viper-auto-indent" | |
52fa07ba MK |
1632 | set-cmd "setq-default" |
1633 | val "t")) | |
1634 | ((member var '("noai" "noautoindent")) | |
8626cfa2 | 1635 | (setq var "viper-auto-indent" |
52fa07ba MK |
1636 | ask-if-save nil |
1637 | val "nil")) | |
e36a387d | 1638 | ((member var '("noai-g" "noautoindent-global")) |
8626cfa2 MK |
1639 | (kill-local-variable 'viper-auto-indent) |
1640 | (setq var "viper-auto-indent" | |
52fa07ba MK |
1641 | set-cmd "setq-default" |
1642 | val "nil")) | |
1643 | ((member var '("ic" "ignorecase")) | |
8626cfa2 | 1644 | (setq var "viper-case-fold-search" |
52fa07ba MK |
1645 | val "t")) |
1646 | ((member var '("noic" "noignorecase")) | |
8626cfa2 | 1647 | (setq var "viper-case-fold-search" |
52fa07ba MK |
1648 | val "nil")) |
1649 | ((member var '("ma" "magic")) | |
8626cfa2 | 1650 | (setq var "viper-re-search" |
52fa07ba | 1651 | val "t")) |
e36a387d | 1652 | ((member var '("noma" "nomagic")) |
8626cfa2 | 1653 | (setq var "viper-re-search" |
52fa07ba MK |
1654 | val "nil")) |
1655 | ((member var '("ro" "readonly")) | |
1656 | (setq var "buffer-read-only" | |
1657 | val "t")) | |
1658 | ((member var '("noro" "noreadonly")) | |
1659 | (setq var "buffer-read-only" | |
1660 | val "nil")) | |
1661 | ((member var '("sm" "showmatch")) | |
1662 | (setq var "blink-matching-paren" | |
1663 | val "t")) | |
1664 | ((member var '("nosm" "noshowmatch")) | |
1665 | (setq var "blink-matching-paren" | |
1666 | val "nil")) | |
1667 | ((member var '("ws" "wrapscan")) | |
8626cfa2 | 1668 | (setq var "viper-search-wrap-around-t" |
52fa07ba MK |
1669 | val "t")) |
1670 | ((member var '("nows" "nowrapscan")) | |
8626cfa2 | 1671 | (setq var "viper-search-wrap-around-t" |
52fa07ba | 1672 | val "nil"))) |
e36a387d | 1673 | (if (and set-cmd (eq val 0)) ; value must be set by the user |
52fa07ba MK |
1674 | (let ((cursor-in-echo-area t)) |
1675 | (message ":set %s = <Value>" var) | |
1676 | ;; if there are unread events, don't wait | |
8626cfa2 | 1677 | (or (viper-set-unread-command-events "") (sit-for 2)) |
52fa07ba MK |
1678 | (setq val (read-string (format ":set %s = " var))) |
1679 | (ex-fixup-history "set" orig-var val) | |
1680 | ||
1681 | ;; check numerical values | |
1682 | (if (member var | |
1683 | '("sw" "shiftwidth" | |
1684 | "ts" "tabstop" | |
e36a387d | 1685 | "ts-g" "tabstop-global" |
52fa07ba MK |
1686 | "wm" "wrapmargin")) |
1687 | (condition-case nil | |
1688 | (or (numberp (setq val2 (car (read-from-string val)))) | |
1689 | (error "%s: Invalid value, numberp, %S" var val)) | |
1690 | (error | |
1691 | (error "%s: Invalid value, numberp, %S" var val)))) | |
1692 | ||
1693 | (cond | |
1694 | ((member var '("sw" "shiftwidth")) | |
8626cfa2 | 1695 | (setq var "viper-shift-width")) |
52fa07ba MK |
1696 | ((member var '("ts" "tabstop")) |
1697 | ;; make it take effect in curr buff and new bufs | |
1698 | (setq var "tab-width" | |
1699 | set-cmd "setq" | |
1700 | ask-if-save nil)) | |
e36a387d | 1701 | ((member var '("ts-g" "tabstop-global")) |
52fa07ba MK |
1702 | (kill-local-variable 'tab-width) |
1703 | (setq var "tab-width" | |
1704 | set-cmd "setq-default")) | |
1705 | ((member var '("wm" "wrapmargin")) | |
1706 | ;; make it take effect in curr buff and new bufs | |
1707 | (kill-local-variable 'fill-column) | |
1708 | (setq var "fill-column" | |
1709 | val (format "(- (window-width) %s)" val) | |
1710 | set-cmd "setq-default")) | |
1711 | ((member var '("sh" "shell")) | |
1712 | (setq var "explicit-shell-file-name" | |
1713 | val (format "\"%s\"" val))))) | |
1714 | (ex-fixup-history "set" orig-var)) | |
1715 | ||
e36a387d MK |
1716 | (if set-cmd |
1717 | (setq actual-lisp-cmd | |
1718 | (format "\n(%s %s %s) %s" set-cmd var val auto-cmd-label) | |
1719 | lisp-cmd-del-pattern | |
1720 | (format "^\n?[ \t]*([ \t]*%s[ \t]+%s[ \t].*)[ \t]*%s" | |
1721 | set-cmd var auto-cmd-label))) | |
52fa07ba MK |
1722 | |
1723 | (if (and ask-if-save | |
1724 | (y-or-n-p (format "Do you want to save this setting in %s " | |
8626cfa2 | 1725 | viper-custom-file-name))) |
52fa07ba | 1726 | (progn |
8626cfa2 MK |
1727 | (viper-save-string-in-file |
1728 | actual-lisp-cmd viper-custom-file-name | |
52fa07ba MK |
1729 | ;; del pattern |
1730 | lisp-cmd-del-pattern) | |
1731 | (if (string= var "fill-column") | |
1732 | (if (> val2 0) | |
8626cfa2 | 1733 | (viper-save-string-in-file |
52fa07ba | 1734 | (concat |
8626cfa2 | 1735 | "(add-hook 'viper-insert-state-hooks 'turn-on-auto-fill) " |
52fa07ba | 1736 | auto-cmd-label) |
8626cfa2 | 1737 | viper-custom-file-name |
52fa07ba | 1738 | delete-turn-on-auto-fill-pattern) |
8626cfa2 MK |
1739 | (viper-save-string-in-file |
1740 | nil viper-custom-file-name delete-turn-on-auto-fill-pattern) | |
1741 | (viper-save-string-in-file | |
1742 | nil viper-custom-file-name | |
52fa07ba MK |
1743 | ;; del pattern |
1744 | lisp-cmd-del-pattern) | |
1745 | )) | |
1746 | )) | |
1747 | ||
e36a387d MK |
1748 | (if set-cmd |
1749 | (message "%s %s %s" | |
1750 | set-cmd var | |
1751 | (if (string-match "^[ \t]*$" val) | |
1752 | (format "%S" val) | |
1753 | val))) | |
1754 | (if actual-lisp-cmd | |
1755 | (eval (car (read-from-string actual-lisp-cmd)))) | |
1756 | (if (string= var "fill-column") | |
1757 | (if (> val2 0) | |
1758 | (auto-fill-mode 1) | |
1759 | (auto-fill-mode -1))) | |
1760 | (if (string= var "all") (ex-show-vars)) | |
52fa07ba | 1761 | )) |
6c2e12f4 | 1762 | |
52fa07ba MK |
1763 | ;; In inline args, skip regex-forw and (optionally) chars-back. |
1764 | ;; Optional 3d arg is a string that should replace ' ' to prevent its | |
1765 | ;; special meaning | |
1766 | (defun ex-get-inline-cmd-args (regex-forw &optional chars-back replace-str) | |
1767 | (save-excursion | |
8626cfa2 MK |
1768 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
1769 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
1770 | (goto-char (point-min)) |
1771 | (re-search-forward regex-forw nil t) | |
1772 | (let ((beg (point)) | |
1773 | end) | |
1774 | (goto-char (point-max)) | |
1775 | (if chars-back | |
1776 | (skip-chars-backward chars-back) | |
1777 | (skip-chars-backward " \t\n\C-m")) | |
1778 | (setq end (point)) | |
1779 | ;; replace SPC with `=' to suppress the special meaning SPC has | |
1780 | ;; in Ex commands | |
1781 | (goto-char beg) | |
1782 | (if replace-str | |
1783 | (while (re-search-forward " +" nil t) | |
1784 | (replace-match replace-str nil t) | |
8626cfa2 | 1785 | (viper-forward-char-carefully))) |
52fa07ba MK |
1786 | (goto-char end) |
1787 | (buffer-substring beg end)))) | |
1788 | ||
1789 | ||
1790 | ;; Ex shell command | |
1791 | (defun ex-shell () | |
1792 | (shell)) | |
1793 | ||
1794 | ;; Viper help. Invokes Info | |
1795 | (defun ex-help () | |
1796 | (condition-case nil | |
1797 | (progn | |
1798 | (pop-to-buffer (get-buffer-create "*info*")) | |
8626cfa2 | 1799 | (info (if viper-xemacs-p "viper.info" "viper")) |
52fa07ba MK |
1800 | (message "Type `i' to search for a specific topic")) |
1801 | (error (beep 1) | |
8626cfa2 | 1802 | (with-output-to-temp-buffer " *viper-info*" |
52fa07ba MK |
1803 | (princ (format " |
1804 | The Info file for Viper does not seem to be installed. | |
1805 | ||
1806 | This file is part of the standard distribution of %sEmacs. | |
1807 | Please contact your system administrator. " | |
8626cfa2 | 1808 | (if viper-xemacs-p "X" "") |
52fa07ba MK |
1809 | )))))) |
1810 | ||
8626cfa2 | 1811 | ;; Ex source command. Loads the file specified as argument or `~/.viper' |
52fa07ba | 1812 | (defun ex-source () |
8626cfa2 | 1813 | (viper-get-ex-file) |
52fa07ba | 1814 | (if (string= ex-file "") |
8626cfa2 | 1815 | (load viper-custom-file-name) |
52fa07ba MK |
1816 | (load ex-file))) |
1817 | ||
1818 | ;; Ex substitute command | |
8626cfa2 | 1819 | ;; If REPEAT use previous regexp which is ex-reg-exp or viper-s-string |
52fa07ba MK |
1820 | (defun ex-substitute (&optional repeat r-flag) |
1821 | (let ((opt-g nil) | |
1822 | (opt-c nil) | |
1823 | (matched-pos nil) | |
8626cfa2 | 1824 | (case-fold-search viper-case-fold-search) |
52fa07ba | 1825 | delim pat repl) |
8626cfa2 | 1826 | (if repeat (setq ex-token nil) (setq delim (viper-get-ex-pat))) |
52fa07ba MK |
1827 | (if (null ex-token) |
1828 | (progn | |
8626cfa2 | 1829 | (setq pat (if r-flag viper-s-string ex-reg-exp)) |
52fa07ba MK |
1830 | (or (stringp pat) |
1831 | (error "No previous pattern to use in substitution")) | |
1832 | (setq repl ex-repl | |
1833 | delim (string-to-char pat))) | |
8626cfa2 MK |
1834 | (setq pat (if (string= ex-token "") viper-s-string ex-token)) |
1835 | (setq viper-s-string pat | |
52fa07ba | 1836 | ex-reg-exp pat) |
8626cfa2 | 1837 | (setq delim (viper-get-ex-pat)) |
52fa07ba MK |
1838 | (if (null ex-token) |
1839 | (setq ex-token "" | |
1840 | ex-repl "") | |
1841 | (setq repl ex-token | |
1842 | ex-repl ex-token))) | |
8626cfa2 | 1843 | (while (viper-get-ex-opt-gc delim) |
52fa07ba | 1844 | (if (string= ex-token "g") (setq opt-g t) (setq opt-c t))) |
8626cfa2 | 1845 | (viper-get-ex-count) |
52fa07ba MK |
1846 | (if ex-count |
1847 | (save-excursion | |
1848 | (if ex-addresses (goto-char (car ex-addresses))) | |
1849 | (set-mark (point)) | |
1850 | (forward-line (1- ex-count)) | |
1851 | (setq ex-addresses (cons (point) (cons (mark t) nil)))) | |
1852 | (if (null ex-addresses) | |
1853 | (setq ex-addresses (cons (point) (cons (point) nil))) | |
1854 | (if (null (cdr ex-addresses)) | |
1855 | (setq ex-addresses (cons (car ex-addresses) ex-addresses))))) | |
1856 | ;(setq G opt-g) | |
1857 | (let ((beg (car ex-addresses)) | |
1858 | (end (car (cdr ex-addresses))) | |
1859 | eol-mark) | |
1860 | (save-excursion | |
8626cfa2 | 1861 | (viper-enlarge-region beg end) |
52fa07ba MK |
1862 | (let ((limit (save-excursion |
1863 | (goto-char (max (point) (mark t))) | |
1864 | (point-marker)))) | |
1865 | (goto-char (min (point) (mark t))) | |
1866 | (while (< (point) limit) | |
1867 | (end-of-line) | |
1868 | (setq eol-mark (point-marker)) | |
1869 | (beginning-of-line) | |
1870 | (if opt-g | |
1871 | (progn | |
1872 | (while (and (not (eolp)) | |
1873 | (re-search-forward pat eol-mark t)) | |
1874 | (if (or (not opt-c) (y-or-n-p "Replace? ")) | |
1875 | (progn | |
1876 | (setq matched-pos (point)) | |
1877 | (if (not (stringp repl)) | |
1878 | (error "Can't perform Ex substitution: No previous replacement pattern")) | |
1879 | (replace-match repl t)))) | |
1880 | (end-of-line) | |
8626cfa2 | 1881 | (viper-forward-char-carefully)) |
52fa07ba MK |
1882 | (if (null pat) |
1883 | (error | |
1884 | "Can't repeat Ex substitution: No previous regular expression")) | |
1885 | (if (and (re-search-forward pat eol-mark t) | |
1886 | (or (not opt-c) (y-or-n-p "Replace? "))) | |
1887 | (progn | |
1888 | (setq matched-pos (point)) | |
1889 | (if (not (stringp repl)) | |
1890 | (error "Can't perform Ex substitution: No previous replacement pattern")) | |
1891 | (replace-match repl t))) | |
1892 | (end-of-line) | |
8626cfa2 | 1893 | (viper-forward-char-carefully)))))) |
52fa07ba MK |
1894 | (if matched-pos (goto-char matched-pos)) |
1895 | (beginning-of-line) | |
1896 | (if opt-c (message "done")))) | |
8f2685cb | 1897 | |
52fa07ba MK |
1898 | ;; Ex tag command |
1899 | (defun ex-tag () | |
1900 | (let (tag) | |
1901 | (save-window-excursion | |
8626cfa2 MK |
1902 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
1903 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
1904 | (skip-chars-forward " \t") |
1905 | (set-mark (point)) | |
1906 | (skip-chars-forward "^ |\t\n") | |
1907 | (setq tag (buffer-substring (mark t) (point)))) | |
1908 | (if (not (string= tag "")) (setq ex-tag tag)) | |
8626cfa2 | 1909 | (viper-change-state-to-emacs) |
52fa07ba MK |
1910 | (condition-case conds |
1911 | (progn | |
1912 | (if (string= tag "") | |
1913 | (find-tag ex-tag t) | |
1914 | (find-tag-other-window ex-tag)) | |
8626cfa2 | 1915 | (viper-change-state-to-vi)) |
52fa07ba | 1916 | (error |
8626cfa2 MK |
1917 | (viper-change-state-to-vi) |
1918 | (viper-message-conditions conds))))) | |
bbe6126c | 1919 | |
52fa07ba MK |
1920 | ;; Ex write command |
1921 | (defun ex-write (q-flag) | |
8626cfa2 MK |
1922 | (viper-default-ex-addresses t) |
1923 | (viper-get-ex-file) | |
9b70a748 MK |
1924 | (let ((end (car ex-addresses)) |
1925 | (beg (car (cdr ex-addresses))) | |
1926 | (orig-buf (current-buffer)) | |
1927 | (orig-buf-file-name (buffer-file-name)) | |
1928 | (orig-buf-name (buffer-name)) | |
1929 | (buff-changed-p (buffer-modified-p)) | |
52fa07ba MK |
1930 | temp-buf writing-same-file region |
1931 | file-exists writing-whole-file) | |
8626cfa2 | 1932 | (if (> beg end) (error viper-FirstAddrExceedsSecond)) |
52fa07ba MK |
1933 | (if ex-cmdfile |
1934 | (progn | |
8626cfa2 | 1935 | (viper-enlarge-region beg end) |
52fa07ba MK |
1936 | (shell-command-on-region (point) (mark t) ex-file)) |
1937 | (if (and (string= ex-file "") (not (buffer-file-name))) | |
1938 | (setq ex-file | |
1939 | (read-file-name | |
1940 | (format "Buffer %s isn't visiting any file. File to save in: " | |
1941 | (buffer-name))))) | |
1942 | ||
1943 | (setq writing-whole-file (and (= (point-min) beg) (= (point-max) end)) | |
1944 | ex-file (if (string= ex-file "") | |
1945 | (buffer-file-name) | |
1946 | (expand-file-name ex-file))) | |
1947 | ;; if ex-file is a directory use the file portion of the buffer file name | |
1948 | (if (and (file-directory-p ex-file) | |
1949 | buffer-file-name | |
1950 | (not (file-directory-p buffer-file-name))) | |
1951 | (setq ex-file | |
9b70a748 MK |
1952 | (concat (file-name-as-directory ex-file) |
1953 | (file-name-nondirectory buffer-file-name)))) | |
1954 | ||
52fa07ba MK |
1955 | (setq file-exists (file-exists-p ex-file) |
1956 | writing-same-file (string= ex-file (buffer-file-name))) | |
1957 | ||
1958 | (if (and writing-whole-file writing-same-file) | |
1959 | (if (not (buffer-modified-p)) | |
1960 | (message "(No changes need to be saved)") | |
1961 | (save-buffer) | |
9b70a748 MK |
1962 | (save-restriction |
1963 | (widen) | |
1964 | (ex-write-info file-exists ex-file (point-min) (point-max)) | |
1965 | )) | |
1966 | ;; writing some other file or portion of the current file | |
1967 | (cond ((and file-exists | |
1968 | (not writing-same-file) | |
1969 | (not (yes-or-no-p | |
1970 | (format "File %s exists. Overwrite? " ex-file)))) | |
1971 | (error "Quit")) | |
1972 | ((and writing-whole-file (not ex-append)) | |
1973 | (unwind-protect | |
1974 | (progn | |
1975 | (set-visited-file-name ex-file) | |
1976 | (set-buffer-modified-p t) | |
1977 | (save-buffer)) | |
1978 | ;; restore the buffer file name | |
1979 | (set-visited-file-name orig-buf-file-name) | |
1980 | (set-buffer-modified-p buff-changed-p) | |
1981 | ;; If the buffer wasn't visiting a file, restore buffer name. | |
1982 | ;; Name could've been changed by packages such as uniquify. | |
1983 | (or orig-buf-file-name | |
1984 | (progn | |
1985 | (unlock-buffer) | |
1986 | (rename-buffer orig-buf-name)))) | |
1987 | (save-restriction | |
1988 | (widen) | |
1989 | (ex-write-info | |
1990 | file-exists ex-file (point-min) (point-max)))) | |
1991 | (t ; writing a region | |
1992 | (unwind-protect | |
1993 | (save-excursion | |
8626cfa2 | 1994 | (viper-enlarge-region beg end) |
9b70a748 MK |
1995 | (setq region (buffer-substring (point) (mark t))) |
1996 | ;; create temp buffer for the region | |
1997 | (setq temp-buf (get-buffer-create " *ex-write*")) | |
1998 | (set-buffer temp-buf) | |
1999 | (set-visited-file-name ex-file 'noquerry) | |
2000 | (erase-buffer) | |
2001 | (if (and file-exists ex-append) | |
2002 | (insert-file-contents ex-file)) | |
2003 | (goto-char (point-max)) | |
2004 | (insert region) | |
2005 | (save-buffer) | |
2006 | (ex-write-info | |
2007 | file-exists ex-file (point-min) (point-max)) | |
2008 | )) | |
2009 | (set-buffer temp-buf) | |
2010 | (set-buffer-modified-p nil) | |
2011 | (kill-buffer temp-buf)) | |
52fa07ba | 2012 | )) |
9b70a748 | 2013 | (set-buffer orig-buf) |
52fa07ba MK |
2014 | ;; this prevents the loss of data if writing part of the buffer |
2015 | (if (and (buffer-file-name) writing-same-file) | |
2016 | (set-visited-file-modtime)) | |
2017 | (or writing-whole-file | |
2018 | (not writing-same-file) | |
2019 | (set-buffer-modified-p t)) | |
2020 | (if q-flag | |
1e70790f | 2021 | (if (< viper-expert-level 2) |
52fa07ba MK |
2022 | (save-buffers-kill-emacs) |
2023 | (kill-buffer (current-buffer)))) | |
2024 | ))) | |
2025 | ||
bbe6126c | 2026 | |
52fa07ba MK |
2027 | (defun ex-write-info (exists file-name beg end) |
2028 | (message "`%s'%s %d lines, %d characters" | |
8626cfa2 | 2029 | (viper-abbreviate-file-name file-name) |
52fa07ba MK |
2030 | (if exists "" " [New file]") |
2031 | (count-lines beg (min (1+ end) (point-max))) | |
2032 | (- end beg))) | |
2033 | ||
2034 | ;; Ex yank command | |
2035 | (defun ex-yank () | |
8626cfa2 MK |
2036 | (viper-default-ex-addresses) |
2037 | (viper-get-ex-buffer) | |
52fa07ba | 2038 | (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) |
8626cfa2 | 2039 | (if (> beg end) (error viper-FirstAddrExceedsSecond)) |
52fa07ba | 2040 | (save-excursion |
8626cfa2 | 2041 | (viper-enlarge-region beg end) |
52fa07ba MK |
2042 | (exchange-point-and-mark) |
2043 | (if (or ex-g-flag ex-g-variant) | |
2044 | (error "Can't execute `yank' within `global'")) | |
2045 | (if ex-count | |
bbe6126c | 2046 | (progn |
52fa07ba MK |
2047 | (set-mark (point)) |
2048 | (forward-line (1- ex-count))) | |
2049 | (set-mark end)) | |
8626cfa2 MK |
2050 | (viper-enlarge-region (point) (mark t)) |
2051 | (if ex-flag (error "`yank': %s" viper-SpuriousText)) | |
52fa07ba | 2052 | (if ex-buffer |
8626cfa2 MK |
2053 | (cond ((viper-valid-register ex-buffer '(Letter)) |
2054 | (viper-append-to-register | |
52fa07ba | 2055 | (downcase ex-buffer) (point) (mark t))) |
8626cfa2 | 2056 | ((viper-valid-register ex-buffer) |
52fa07ba | 2057 | (copy-to-register ex-buffer (point) (mark t) nil)) |
8626cfa2 | 2058 | (t (error viper-InvalidRegister ex-buffer)))) |
52fa07ba MK |
2059 | (copy-region-as-kill (point) (mark t))))) |
2060 | ||
2061 | ;; Execute shell command | |
2062 | (defun ex-command () | |
2063 | (let (command) | |
2064 | (save-window-excursion | |
8626cfa2 MK |
2065 | (setq viper-ex-work-buf (get-buffer-create viper-ex-work-buf-name)) |
2066 | (set-buffer viper-ex-work-buf) | |
52fa07ba MK |
2067 | (skip-chars-forward " \t") |
2068 | (setq command (buffer-substring (point) (point-max))) | |
2069 | (end-of-line)) | |
2070 | (setq command (ex-expand-filsyms command (current-buffer))) | |
2071 | (if (and (> (length command) 0) (string= "!" (substring command 0 1))) | |
8626cfa2 MK |
2072 | (if viper-ex-last-shell-com |
2073 | (setq command | |
2074 | (concat viper-ex-last-shell-com (substring command 1))) | |
52fa07ba | 2075 | (error "No previous shell command"))) |
8626cfa2 | 2076 | (setq viper-ex-last-shell-com command) |
52fa07ba MK |
2077 | (if (null ex-addresses) |
2078 | (shell-command command) | |
2079 | (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses)))) | |
2080 | (if (null beg) (setq beg end)) | |
6c2e12f4 | 2081 | (save-excursion |
52fa07ba MK |
2082 | (goto-char beg) |
2083 | (set-mark end) | |
8626cfa2 | 2084 | (viper-enlarge-region (point) (mark t)) |
52fa07ba MK |
2085 | (shell-command-on-region (point) (mark t) command t)) |
2086 | (goto-char beg))))) | |
2087 | ||
2088 | ;; Print line number | |
2089 | (defun ex-line-no () | |
2090 | (message "%d" | |
2091 | (1+ (count-lines | |
2092 | (point-min) | |
2093 | (if (null ex-addresses) (point-max) (car ex-addresses)))))) | |
2094 | ||
2095 | ;; Give information on the file visited by the current buffer | |
8626cfa2 | 2096 | (defun viper-info-on-file () |
6c2e12f4 | 2097 | (interactive) |
8626cfa2 MK |
2098 | (let ((pos1 (viper-line-pos 'start)) |
2099 | (pos2 (viper-line-pos 'end)) | |
52fa07ba | 2100 | lines file info) |
8626cfa2 | 2101 | (setq lines (count-lines (point-min) (viper-line-pos 'end)) |
52fa07ba | 2102 | file (if (buffer-file-name) |
8626cfa2 | 2103 | (concat (viper-abbreviate-file-name (buffer-file-name)) ":") |
52fa07ba MK |
2104 | (concat (buffer-name) " [Not visiting any file]:")) |
2105 | info (format "line=%d/%d pos=%d/%d col=%d %s" | |
2106 | (if (= pos1 pos2) | |
2107 | (1+ lines) | |
2108 | lines) | |
2109 | (count-lines (point-min) (point-max)) | |
2110 | (point) (1- (point-max)) | |
2111 | (1+ (current-column)) | |
2112 | (if (buffer-modified-p) "[Modified]" "[Unchanged]"))) | |
2113 | (if (< (+ 1 (length info) (length file)) | |
2114 | (window-width (minibuffer-window))) | |
2115 | (message (concat file " " info)) | |
2116 | (save-window-excursion | |
8626cfa2 | 2117 | (with-output-to-temp-buffer " *viper-info*" |
52fa07ba MK |
2118 | (princ (concat "\n" |
2119 | file "\n\n\t" info | |
2120 | "\n\n\nPress any key to continue...\n\n"))) | |
8626cfa2 MK |
2121 | (viper-read-event) |
2122 | (kill-buffer " *viper-info*"))) | |
fad2477b | 2123 | )) |
6c2e12f4 | 2124 | |
e36a387d MK |
2125 | ;; display all variables set through :set |
2126 | (defun ex-show-vars () | |
8626cfa2 MK |
2127 | (with-output-to-temp-buffer " *viper-info*" |
2128 | (princ (if viper-auto-indent | |
e36a387d | 2129 | "autoindent (local)\n" "noautoindent (local)\n")) |
8626cfa2 | 2130 | (princ (if (default-value 'viper-auto-indent) |
e36a387d | 2131 | "autoindent (global) \n" "noautoindent (global) \n")) |
8626cfa2 MK |
2132 | (princ (if viper-case-fold-search "ignorecase\n" "noignorecase\n")) |
2133 | (princ (if viper-re-search "magic\n" "nomagic\n")) | |
e36a387d MK |
2134 | (princ (if buffer-read-only "readonly\n" "noreadonly\n")) |
2135 | (princ (if blink-matching-paren "showmatch\n" "noshowmatch\n")) | |
8626cfa2 MK |
2136 | (princ (if viper-search-wrap-around-t "wrapscan\n" "nowrapscan\n")) |
2137 | (princ (format "shiftwidth \t\t= %S\n" viper-shift-width)) | |
e36a387d MK |
2138 | (princ (format "tabstop (local) \t= %S\n" tab-width)) |
2139 | (princ (format "tabstop (global) \t= %S\n" (default-value 'tab-width))) | |
2140 | (princ (format "wrapmargin (local) \t= %S\n" | |
2141 | (- (window-width) fill-column))) | |
2142 | (princ (format "wrapmargin (global) \t= %S\n" | |
2143 | (- (window-width) (default-value 'fill-column)))) | |
2144 | (princ (format "shell \t\t\t= %S\n" (if (boundp 'explicit-shell-file-name) | |
2145 | explicit-shell-file-name | |
2146 | 'none))) | |
2147 | )) | |
2148 | ||
2149 | ||
2150 | ||
2151 | ||
6c2e12f4 | 2152 | |
52fa07ba | 2153 | ;;; viper-ex.el ends here |