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