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