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