]> code.delx.au - gnu-emacs/blob - lisp/emulation/viper-util.el
(vip-get-filenames-from-buffer): Add &optional.
[gnu-emacs] / lisp / emulation / viper-util.el
1 ;;; viper-util.el --- Utilities used by viper.el
2
3 ;; Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
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
22
23 ;; Code
24
25 (require 'ring)
26
27 ;; Compiler pacifier
28 (defvar vip-overriding-map)
29 (defvar pm-color-alist)
30 (defvar zmacs-region-stays)
31 (defvar vip-search-face)
32 (defvar vip-minibuffer-current-face)
33 (defvar vip-minibuffer-insert-face)
34 (defvar vip-minibuffer-vi-face)
35 (defvar vip-minibuffer-emacs-face)
36 (defvar vip-replace-overlay-face)
37 (defvar vip-minibuffer-overlay)
38 (defvar vip-replace-overlay)
39 (defvar vip-search-overlay)
40 (defvar vip-replace-overlay-cursor-color)
41 (defvar vip-intermediate-command)
42 (defvar vip-use-replace-region-delimiters)
43 (defvar vip-fast-keyseq-timeout)
44 (defvar vip-related-files-and-buffers-ring)
45 ;; end compiler pacifier
46
47 ;; Is it XEmacs?
48 (defconst vip-xemacs-p (string-match "\\(Lucid\\|XEmacs\\)" emacs-version))
49 ;; Is it Emacs?
50 (defconst vip-emacs-p (not vip-xemacs-p))
51 ;; Tell whether we are running as a window application or on a TTY
52 (defsubst vip-device-type ()
53 (if vip-emacs-p
54 window-system
55 (device-type (selected-device))))
56 ;; in XEmacs: device-type is tty on tty and stream in batch.
57 (defun vip-window-display-p ()
58 (and (vip-device-type) (not (memq (vip-device-type) '(tty stream)))))
59
60 (defvar vip-force-faces nil
61 "If t, Viper will think that it is running on a display that supports faces.
62 This is provided as a temporary relief for users of face-capable displays
63 that Viper doesn't know about.")
64
65 (defun vip-has-face-support-p ()
66 (cond ((vip-window-display-p))
67 (vip-force-faces)
68 (vip-emacs-p (memq (vip-device-type) '(pc)))
69 (vip-xemacs-p (memq (vip-device-type) '(tty pc)))))
70
71 \f
72 ;;; Macros
73
74 (defmacro vip-deflocalvar (var default-value &optional documentation)
75 (` (progn
76 (defvar (, var) (, default-value)
77 (, (format "%s\n\(buffer local\)" documentation)))
78 (make-variable-buffer-local '(, var))
79 )))
80
81 (defmacro vip-loop (count body)
82 "(vip-loop COUNT BODY) Execute BODY COUNT times."
83 (list 'let (list (list 'count count))
84 (list 'while '(> count 0)
85 body
86 '(setq count (1- count))
87 )))
88
89 (defmacro vip-buffer-live-p (buf)
90 (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf))))))
91
92 ;; return buffer-specific macro definition, given a full macro definition
93 (defmacro vip-kbd-buf-alist (macro-elt)
94 (` (nth 1 (, macro-elt))))
95 ;; get a pair: (curr-buffer . macro-definition)
96 (defmacro vip-kbd-buf-pair (macro-elt)
97 (` (assoc (buffer-name) (vip-kbd-buf-alist (, macro-elt)))))
98 ;; get macro definition for current buffer
99 (defmacro vip-kbd-buf-definition (macro-elt)
100 (` (cdr (vip-kbd-buf-pair (, macro-elt)))))
101
102 ;; return mode-specific macro definitions, given a full macro definition
103 (defmacro vip-kbd-mode-alist (macro-elt)
104 (` (nth 2 (, macro-elt))))
105 ;; get a pair: (major-mode . macro-definition)
106 (defmacro vip-kbd-mode-pair (macro-elt)
107 (` (assoc major-mode (vip-kbd-mode-alist (, macro-elt)))))
108 ;; get macro definition for the current major mode
109 (defmacro vip-kbd-mode-definition (macro-elt)
110 (` (cdr (vip-kbd-mode-pair (, macro-elt)))))
111
112 ;; return global macro definition, given a full macro definition
113 (defmacro vip-kbd-global-pair (macro-elt)
114 (` (nth 3 (, macro-elt))))
115 ;; get global macro definition from an elt of macro-alist
116 (defmacro vip-kbd-global-definition (macro-elt)
117 (` (cdr (vip-kbd-global-pair (, macro-elt)))))
118
119 ;; last elt of a sequence
120 (defsubst vip-seq-last-elt (seq)
121 (elt seq (1- (length seq))))
122
123 ;; Check if arg is a valid character for register
124 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
125 ;; Letter means lowercase letters, Letter means uppercase letters, and
126 ;; digit means digits from 1 to 9.
127 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
128 (defun vip-valid-register (reg &optional type)
129 (or type (setq type '(letter Letter digit)))
130 (or (if (memq 'letter type)
131 (and (<= ?a reg) (<= reg ?z)))
132 (if (memq 'digit type)
133 (and (<= ?1 reg) (<= reg ?9)))
134 (if (memq 'Letter type)
135 (and (<= ?A reg) (<= reg ?Z)))
136 ))
137
138 ;; checks if object is a marker, has a buffer, and points to within that buffer
139 (defun vip-valid-marker (marker)
140 (if (and (markerp marker) (marker-buffer marker))
141 (let ((buf (marker-buffer marker))
142 (pos (marker-position marker)))
143 (save-excursion
144 (set-buffer buf)
145 (and (<= pos (point-max)) (<= (point-min) pos))))))
146
147 \f
148 (defvar vip-minibuffer-overlay-priority 300)
149 (defvar vip-replace-overlay-priority 400)
150 (defvar vip-search-overlay-priority 500)
151
152 \f
153 ;;; XEmacs support
154
155 (if vip-xemacs-p
156 (progn
157 (fset 'vip-read-event (symbol-function 'next-command-event))
158 (fset 'vip-make-overlay (symbol-function 'make-extent))
159 (fset 'vip-overlay-start (symbol-function 'extent-start-position))
160 (fset 'vip-overlay-end (symbol-function 'extent-end-position))
161 (fset 'vip-overlay-put (symbol-function 'set-extent-property))
162 (fset 'vip-overlay-p (symbol-function 'extentp))
163 (fset 'vip-overlay-get (symbol-function 'extent-property))
164 (fset 'vip-move-overlay (symbol-function 'set-extent-endpoints))
165 (if (vip-window-display-p)
166 (fset 'vip-iconify (symbol-function 'iconify-frame)))
167 (cond ((vip-has-face-support-p)
168 (fset 'vip-get-face (symbol-function 'get-face))
169 (fset 'vip-color-defined-p
170 (symbol-function 'valid-color-name-p))
171 )))
172 (fset 'vip-read-event (symbol-function 'read-event))
173 (fset 'vip-make-overlay (symbol-function 'make-overlay))
174 (fset 'vip-overlay-start (symbol-function 'overlay-start))
175 (fset 'vip-overlay-end (symbol-function 'overlay-end))
176 (fset 'vip-overlay-put (symbol-function 'overlay-put))
177 (fset 'vip-overlay-p (symbol-function 'overlayp))
178 (fset 'vip-overlay-get (symbol-function 'overlay-get))
179 (fset 'vip-move-overlay (symbol-function 'move-overlay))
180 (if (vip-window-display-p)
181 (fset 'vip-iconify (symbol-function 'iconify-or-deiconify-frame)))
182 (cond ((vip-has-face-support-p)
183 (fset 'vip-get-face (symbol-function 'internal-get-face))
184 (fset 'vip-color-defined-p (symbol-function 'x-color-defined-p))
185 )))
186
187 (fset 'vip-characterp
188 (symbol-function
189 (if vip-xemacs-p 'characterp 'integerp)))
190
191 (defsubst vip-color-display-p ()
192 (if vip-emacs-p
193 (x-display-color-p)
194 (eq (device-class (selected-device)) 'color)))
195
196 (defsubst vip-get-cursor-color ()
197 (if vip-emacs-p
198 (cdr (assoc 'cursor-color (frame-parameters)))
199 (color-instance-name (frame-property (selected-frame) 'cursor-color))))
200
201
202 ;; OS/2
203 (cond ((eq (vip-device-type) 'pm)
204 (fset 'vip-color-defined-p
205 (function (lambda (color) (assoc color pm-color-alist))))))
206
207 ;; needed to smooth out the difference between Emacs and XEmacs
208 (defsubst vip-italicize-face (face)
209 (if vip-xemacs-p
210 (make-face-italic face)
211 (make-face-italic face nil 'noerror)))
212
213 ;; test if display is color and the colors are defined
214 (defsubst vip-can-use-colors (&rest colors)
215 (if (vip-color-display-p)
216 (not (memq nil (mapcar 'vip-color-defined-p colors)))
217 ))
218
219 (defun vip-hide-face (face)
220 (if (and (vip-has-face-support-p) vip-emacs-p)
221 (add-to-list 'facemenu-unlisted-faces face)))
222
223 ;; cursor colors
224 (defun vip-change-cursor-color (new-color)
225 (if (and (vip-window-display-p) (vip-color-display-p)
226 (stringp new-color) (vip-color-defined-p new-color)
227 (not (string= new-color (vip-get-cursor-color))))
228 (modify-frame-parameters
229 (selected-frame) (list (cons 'cursor-color new-color)))))
230
231 (defsubst vip-save-cursor-color ()
232 (if (and (vip-window-display-p) (vip-color-display-p))
233 (let ((color (vip-get-cursor-color)))
234 (if (and (stringp color) (vip-color-defined-p color)
235 (not (string= color vip-replace-overlay-cursor-color)))
236 (vip-overlay-put vip-replace-overlay 'vip-cursor-color color)))))
237
238 (defsubst vip-restore-cursor-color ()
239 (vip-change-cursor-color
240 (vip-overlay-get vip-replace-overlay 'vip-cursor-color)))
241
242 \f
243 ;; Check the current version against the major and minor version numbers
244 ;; using op: cur-vers op major.minor If emacs-major-version or
245 ;; emacs-minor-version are not defined, we assume that the current version
246 ;; is hopelessly outdated. We assume that emacs-major-version and
247 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
248 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
249 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
250 ;; incorrect. However, this gives correct result in our cases, since we are
251 ;; testing for sufficiently high Emacs versions.
252 (defun vip-check-version (op major minor &optional type-of-emacs)
253 (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
254 (and (cond ((eq type-of-emacs 'xemacs) vip-xemacs-p)
255 ((eq type-of-emacs 'emacs) vip-emacs-p)
256 (t t))
257 (cond ((eq op '=) (and (= emacs-minor-version minor)
258 (= emacs-major-version major)))
259 ((memq op '(> >= < <=))
260 (and (or (funcall op emacs-major-version major)
261 (= emacs-major-version major))
262 (if (= emacs-major-version major)
263 (funcall op emacs-minor-version minor)
264 t)))
265 (t
266 (error "%S: Invalid op in vip-check-version" op))))
267 (cond ((memq op '(= > >=)) nil)
268 ((memq op '(< <=)) t))))
269
270 ;;;; warn if it is a wrong version of emacs
271 ;;(if (or (vip-check-version '< 19 29 'emacs)
272 ;; (vip-check-version '< 19 12 'xemacs))
273 ;; (progn
274 ;; (with-output-to-temp-buffer " *vip-info*"
275 ;; (switch-to-buffer " *vip-info*")
276 ;; (insert
277 ;; (format "
278 ;;
279 ;;This version of Viper requires
280 ;;
281 ;;\t Emacs 19.29 and higher
282 ;;\t OR
283 ;;\t XEmacs 19.12 and higher
284 ;;
285 ;;It is unlikely to work under Emacs version %s
286 ;;that you are using... " emacs-version))
287 ;;
288 ;; (if noninteractive
289 ;; ()
290 ;; (beep 1)
291 ;; (beep 1)
292 ;; (insert "\n\nType any key to continue... ")
293 ;; (vip-read-event)))
294 ;; (kill-buffer " *vip-info*")))
295
296
297 (defun vip-get-visible-buffer-window (wind)
298 (if vip-xemacs-p
299 (get-buffer-window wind t)
300 (get-buffer-window wind 'visible)))
301
302
303 ;; Return line position.
304 ;; If pos is 'start then returns position of line start.
305 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
306 ;; Pos = 'indent returns beginning of indentation.
307 ;; Otherwise, returns point. Current point is not moved in any case."
308 (defun vip-line-pos (pos)
309 (let ((cur-pos (point))
310 (result))
311 (cond
312 ((equal pos 'start)
313 (beginning-of-line))
314 ((equal pos 'end)
315 (end-of-line))
316 ((equal pos 'mid)
317 (goto-char (+ (vip-line-pos 'start) (vip-line-pos 'end) 2)))
318 ((equal pos 'indent)
319 (back-to-indentation))
320 (t nil))
321 (setq result (point))
322 (goto-char cur-pos)
323 result))
324
325
326 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
327 ;; The first argument must eval to a variable name.
328 ;; Arguments: (var-name position &optional buffer).
329 ;;
330 ;; This is useful for moving markers that are supposed to be local.
331 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
332 ;; Then, each time this var is used in `vip-move-marker-locally' in a new
333 ;; buffer, a new marker will be created.
334 (defun vip-move-marker-locally (var pos &optional buffer)
335 (if (markerp (eval var))
336 ()
337 (set var (make-marker)))
338 (move-marker (eval var) pos buffer))
339
340
341 ;; Print CONDITIONS as a message.
342 (defun vip-message-conditions (conditions)
343 (let ((case (car conditions)) (msg (cdr conditions)))
344 (if (null msg)
345 (message "%s" case)
346 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
347 (beep 1)))
348
349
350 \f
351 ;;; List/alist utilities
352
353 ;; Convert LIST to an alist
354 (defun vip-list-to-alist (lst)
355 (let ((alist))
356 (while lst
357 (setq alist (cons (list (car lst)) alist))
358 (setq lst (cdr lst)))
359 alist))
360
361 ;; Convert ALIST to a list.
362 (defun vip-alist-to-list (alst)
363 (let ((lst))
364 (while alst
365 (setq lst (cons (car (car alst)) lst))
366 (setq alst (cdr alst)))
367 lst))
368
369 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
370 (defun vip-filter-alist (regexp alst)
371 (interactive "s x")
372 (let ((outalst) (inalst alst))
373 (while (car inalst)
374 (if (string-match regexp (car (car inalst)))
375 (setq outalst (cons (car inalst) outalst)))
376 (setq inalst (cdr inalst)))
377 outalst))
378
379 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
380 (defun vip-filter-list (regexp lst)
381 (interactive "s x")
382 (let ((outlst) (inlst lst))
383 (while (car inlst)
384 (if (string-match regexp (car inlst))
385 (setq outlst (cons (car inlst) outlst)))
386 (setq inlst (cdr inlst)))
387 outlst))
388
389
390 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
391 ;; LIS2 is modified by filtering it: deleting its members of the form
392 ;; \(car elt\) such that (car elt') is in LIS1.
393 (defun vip-append-filter-alist (lis1 lis2)
394 (let ((temp lis1)
395 elt)
396
397 ;;filter-append the second list
398 (while temp
399 ;; delete all occurrences
400 (while (setq elt (assoc (car (car temp)) lis2))
401 (setq lis2 (delq elt lis2)))
402 (setq temp (cdr temp)))
403
404 (nconc lis1 lis2)))
405
406 \f
407 ;;; Support for :e and file globbing
408
409 (defun vip-ex-nontrivial-find-file-unix (filespec)
410 "Glob the file spec and visit all files matching the spec.
411 This function is designed to work under Unix. It may also work under VMS.
412
413 Users who prefer other types of shells should write their own version of this
414 function and set the variable `ex-nontrivial-find-file-function'
415 appropriately."
416 (let ((gshell
417 (cond (ex-unix-type-shell shell-file-name)
418 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VAX VMS
419 (t "sh"))) ; probably Unix anyway
420 (gshell-options
421 ;; using cond in anticipation of further additions
422 (cond (ex-unix-type-shell-options)
423 ))
424 (command (cond (vip-ms-style-os-p (format "\"ls -1 %s\"" filespec))
425 (t (format "ls -1 %s" filespec))))
426 file-list)
427 (save-excursion
428 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
429 (erase-buffer)
430 (setq status
431 (if gshell-options
432 (call-process gshell nil t nil
433 gshell-options
434 "-c"
435 command)
436 (call-process gshell nil t nil
437 "-c"
438 command)))
439 (goto-char (point-min))
440 ;; Issue an error, if no match.
441 (if (> status 0)
442 (save-excursion
443 (skip-chars-forward " \t\n\j")
444 (if (looking-at "ls:")
445 (vip-forward-Word 1))
446 (error "%s: %s"
447 (if (stringp gshell)
448 gshell
449 "shell")
450 (buffer-substring (point) (vip-line-pos 'end)))
451 ))
452 (goto-char (point-min))
453 (setq file-list (vip-get-filenames-from-buffer 'one-per-line)))
454
455 (mapcar 'find-file file-list)
456 ))
457
458 (defun vip-ex-nontrivial-find-file-ms (filespec)
459 "Glob the file spec and visit all files matching the spec.
460 This function is designed to work under MS type systems, such as NT, W95, and
461 DOS. It may also work under OS/2.
462
463 The users of Unix-type shells should be able to use
464 `vip-ex-nontrivial-find-file-unix', making it into the value of the variable
465 `ex-nontrivial-find-file-function'. If this doesn't work, the user may have
466 to write a custom function, similar to `vip-ex-nontrivial-find-file-unix'."
467 (save-excursion
468 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
469 (erase-buffer)
470 (insert filespec)
471 (goto-char (point-min))
472 (mapcar 'find-file
473 (vip-glob-ms-windows-files (vip-get-filenames-from-buffer)))
474 ))
475
476
477 ;; Interpret the stuff in the buffer as a list of file names
478 ;; return a list of file names listed in the buffer beginning at point
479 ;; If optional arg is supplied, assume each filename is listed on a separate
480 ;; line
481 (defun vip-get-filenames-from-buffer (&optional one-per-line)
482 (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
483 result fname delim)
484 (skip-chars-forward skip-chars)
485 (while (not (eobp))
486 (if (cond ((looking-at "\"")
487 (setq delim ?\")
488 (re-search-forward "[^\"]+" nil t)) ; noerror
489 ((looking-at "'")
490 (setq delim ?')
491 (re-search-forward "[^']+" nil t)) ; noerror
492 (t
493 (re-search-forward
494 (concat "[^" skip-chars "]+") nil t))) ;noerror
495 (setq fname
496 (buffer-substring (match-beginning 0) (match-end 0))))
497 (if delim
498 (forward-char 1))
499 (skip-chars-forward " \t\n")
500 (setq result (cons fname result)))
501 result))
502
503 ;; convert MS-DOS wildcards to regexp
504 (defun vip-wildcard-to-regexp (wcard)
505 (save-excursion
506 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
507 (erase-buffer)
508 (insert wcard)
509 (goto-char (point-min))
510 (while (not (eobp))
511 (skip-chars-forward "^*?.\\\\")
512 (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
513 ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
514 ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
515 ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
516 )
517 (buffer-string)
518 ))
519
520
521 ;; glob windows files
522 ;; LIST is expected to be in reverse order
523 (defun vip-glob-ms-windows-files (list)
524 (let ((tmp list)
525 (case-fold-search t)
526 tmp2)
527 (while tmp
528 (setq tmp2 (cons (directory-files
529 ;; the directory part
530 (or (file-name-directory (car tmp))
531 "")
532 t ; return full names
533 ;; the regexp part: globs the file names
534 (concat "^"
535 (vip-wildcard-to-regexp
536 (file-name-nondirectory (car tmp)))
537 "$"))
538 tmp2))
539 (setq tmp (cdr tmp)))
540 (reverse (apply 'append tmp2))))
541
542
543
544
545 \f
546 ;;; Insertion ring
547
548 ;; Rotate RING's index. DIRection can be positive or negative.
549 (defun vip-ring-rotate1 (ring dir)
550 (if (and (ring-p ring) (> (ring-length ring) 0))
551 (progn
552 (setcar ring (cond ((> dir 0)
553 (ring-plus1 (car ring) (ring-length ring)))
554 ((< dir 0)
555 (ring-minus1 (car ring) (ring-length ring)))
556 ;; don't rotate if dir = 0
557 (t (car ring))))
558 (vip-current-ring-item ring)
559 )))
560
561 (defun vip-special-ring-rotate1 (ring dir)
562 (if (memq vip-intermediate-command
563 '(repeating-display-destructive-command
564 repeating-insertion-from-ring))
565 (vip-ring-rotate1 ring dir)
566 ;; don't rotate otherwise
567 (vip-ring-rotate1 ring 0)))
568
569 ;; current ring item; if N is given, then so many items back from the
570 ;; current
571 (defun vip-current-ring-item (ring &optional n)
572 (setq n (or n 0))
573 (if (and (ring-p ring) (> (ring-length ring) 0))
574 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
575
576 ;; push item onto ring. the second argument is a ring-variable, not value.
577 (defun vip-push-onto-ring (item ring-var)
578 (or (ring-p (eval ring-var))
579 (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
580 (or (null item) ; don't push nil
581 (and (stringp item) (string= item "")) ; or empty strings
582 (equal item (vip-current-ring-item (eval ring-var))) ; or old stuff
583 ;; Since vip-set-destructive-command checks if we are inside vip-repeat,
584 ;; we don't check whether this-command-keys is a `.'.
585 ;; The cmd vip-repeat makes a call to the current function only if
586 ;; `.' is executing a command from the command history. It doesn't
587 ;; call the push-onto-ring function if `.' is simply repeating the
588 ;; last destructive command.
589 ;; We only check for ESC (which happens when we do insert with a
590 ;; prefix argument, or if this-command-keys doesn't give anything
591 ;; meaningful (in that case we don't know what to show to the user).
592 (and (eq ring-var 'vip-command-ring)
593 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
594 (vip-array-to-string (this-command-keys))))
595 (vip-ring-insert (eval ring-var) item))
596 )
597
598
599 ;; removing elts from ring seems to break it
600 (defun vip-cleanup-ring (ring)
601 (or (< (ring-length ring) 2)
602 (null (vip-current-ring-item ring))
603 ;; last and previous equal
604 (if (equal (vip-current-ring-item ring) (vip-current-ring-item ring 1))
605 (vip-ring-pop ring))))
606
607 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
608 (defun vip-ring-pop (ring)
609 (let* ((ln (ring-length ring))
610 (vec (cdr (cdr ring)))
611 (veclen (length vec))
612 (hd (car ring))
613 (idx (max 0 (ring-minus1 hd ln)))
614 (top-elt (aref vec idx)))
615
616 ;; shift elements
617 (while (< (1+ idx) veclen)
618 (aset vec idx (aref vec (1+ idx)))
619 (setq idx (1+ idx)))
620 (aset vec idx nil)
621
622 (setq hd (max 0 (ring-minus1 hd ln)))
623 (if (= hd (1- ln)) (setq hd 0))
624 (setcar ring hd) ; move head
625 (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
626 top-elt
627 ))
628
629 (defun vip-ring-insert (ring item)
630 (let* ((ln (ring-length ring))
631 (vec (cdr (cdr ring)))
632 (veclen (length vec))
633 (hd (car ring))
634 (vecpos-after-hd (if (= hd 0) ln hd))
635 (idx ln))
636
637 (if (= ln veclen)
638 (progn
639 (aset vec hd item) ; hd is always 1+ the actual head index in vec
640 (setcar ring (ring-plus1 hd ln)))
641 (setcar (cdr ring) (1+ ln))
642 (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
643 (while (and (>= idx vecpos-after-hd) (> ln 0))
644 (aset vec idx (aref vec (1- idx)))
645 (setq idx (1- idx)))
646 (aset vec vecpos-after-hd item))
647 item))
648
649 \f
650 ;;; String utilities
651
652 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
653 ;; PRE-STRING is a string to prepend to the abbrev string.
654 ;; POST-STRING is a string to append to the abbrev string.
655 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
656 ;; if the orig string was truncated.
657 (defun vip-abbreviate-string (string max-len
658 pre-string post-string abbrev-sign)
659 (let (truncated-str)
660 (setq truncated-str
661 (if (stringp string)
662 (substring string 0 (min max-len (length string)))))
663 (cond ((null truncated-str) "")
664 ((> (length string) max-len)
665 (format "%s%s%s%s"
666 pre-string truncated-str abbrev-sign post-string))
667 (t (format "%s%s%s" pre-string truncated-str post-string)))))
668
669 ;; tells if we are over a whitespace-only line
670 (defsubst vip-over-whitespace-line ()
671 (save-excursion
672 (beginning-of-line)
673 (looking-at "^[ \t]*$")))
674
675 \f
676 ;;; Saving settings in custom file
677
678 ;; Save the current setting of VAR in CUSTOM-FILE.
679 ;; If given, MESSAGE is a message to be displayed after that.
680 ;; This message is erased after 2 secs, if erase-msg is non-nil.
681 ;; Arguments: var message custom-file &optional erase-message
682 (defun vip-save-setting (var message custom-file &optional erase-msg)
683 (let* ((var-name (symbol-name var))
684 (var-val (if (boundp var) (eval var)))
685 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
686 (buf (find-file-noselect (substitute-in-file-name custom-file)))
687 )
688 (message message)
689 (save-excursion
690 (set-buffer buf)
691 (goto-char (point-min))
692 (if (re-search-forward regexp nil t)
693 (let ((reg-end (1- (match-end 0))))
694 (search-backward var-name)
695 (delete-region (match-beginning 0) reg-end)
696 (goto-char (match-beginning 0))
697 (insert (format "%s '%S" var-name var-val)))
698 (goto-char (point-max))
699 (if (not (bolp)) (insert "\n"))
700 (insert (format "(setq %s '%S)\n" var-name var-val)))
701 (save-buffer))
702 (kill-buffer buf)
703 (if erase-msg
704 (progn
705 (sit-for 2)
706 (message "")))
707 ))
708
709 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
710 ;; match this pattern.
711 (defun vip-save-string-in-file (string custom-file &optional pattern)
712 (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
713 (save-excursion
714 (set-buffer buf)
715 (goto-char (point-min))
716 (if pattern (delete-matching-lines pattern))
717 (goto-char (point-max))
718 (if string (insert string))
719 (save-buffer))
720 (kill-buffer buf)
721 ))
722
723 \f
724 ;;; Overlays
725
726 ;; Search
727
728 (defun vip-flash-search-pattern ()
729 (if (vip-overlay-p vip-search-overlay)
730 (vip-move-overlay vip-search-overlay (match-beginning 0) (match-end 0))
731 (setq vip-search-overlay
732 (vip-make-overlay
733 (match-beginning 0) (match-end 0) (current-buffer))))
734
735 (vip-overlay-put vip-search-overlay 'priority vip-search-overlay-priority)
736 (if (vip-has-face-support-p)
737 (progn
738 (vip-overlay-put vip-search-overlay 'face vip-search-face)
739 (sit-for 2)
740 (vip-overlay-put vip-search-overlay 'face nil))))
741
742 ;; Replace state
743
744 (defsubst vip-move-replace-overlay (beg end)
745 (vip-move-overlay vip-replace-overlay beg end))
746
747 (defun vip-set-replace-overlay (beg end)
748 (if (vip-overlay-p vip-replace-overlay)
749 (vip-move-replace-overlay beg end)
750 (setq vip-replace-overlay (vip-make-overlay beg end (current-buffer)))
751 ;; never detach
752 (vip-overlay-put
753 vip-replace-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
754 (vip-overlay-put
755 vip-replace-overlay 'priority vip-replace-overlay-priority))
756 (if (vip-has-face-support-p)
757 (vip-overlay-put vip-replace-overlay 'face vip-replace-overlay-face))
758 (vip-save-cursor-color)
759 (vip-change-cursor-color vip-replace-overlay-cursor-color)
760 )
761
762
763 (defsubst vip-set-replace-overlay-glyphs (before-glyph after-glyph)
764 (if (or (not (vip-has-face-support-p))
765 vip-use-replace-region-delimiters)
766 (let ((before-name (if vip-xemacs-p 'begin-glyph 'before-string))
767 (after-name (if vip-xemacs-p 'end-glyph 'after-string)))
768 (vip-overlay-put vip-replace-overlay before-name before-glyph)
769 (vip-overlay-put vip-replace-overlay after-name after-glyph))))
770
771 (defsubst vip-hide-replace-overlay ()
772 (vip-set-replace-overlay-glyphs nil nil)
773 (vip-restore-cursor-color)
774 (if (vip-has-face-support-p)
775 (vip-overlay-put vip-replace-overlay 'face nil)))
776
777
778 (defsubst vip-replace-start ()
779 (vip-overlay-start vip-replace-overlay))
780 (defsubst vip-replace-end ()
781 (vip-overlay-end vip-replace-overlay))
782
783
784 ;; Minibuffer
785
786 (defun vip-set-minibuffer-overlay ()
787 (vip-check-minibuffer-overlay)
788 (if (vip-has-face-support-p)
789 (progn
790 (vip-overlay-put
791 vip-minibuffer-overlay 'face vip-minibuffer-current-face)
792 (vip-overlay-put
793 vip-minibuffer-overlay 'priority vip-minibuffer-overlay-priority)
794 ;; never detach
795 (vip-overlay-put
796 vip-minibuffer-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
797 ;; make vip-minibuffer-overlay open-ended
798 ;; In emacs, it is made open ended at creation time
799 (if vip-xemacs-p
800 (progn
801 (vip-overlay-put vip-minibuffer-overlay 'start-open nil)
802 (vip-overlay-put vip-minibuffer-overlay 'end-open nil)))
803 )))
804
805 (defun vip-check-minibuffer-overlay ()
806 (or (vip-overlay-p vip-minibuffer-overlay)
807 (setq vip-minibuffer-overlay
808 (if vip-xemacs-p
809 (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer))
810 ;; make overlay open-ended
811 (vip-make-overlay
812 1 (1+ (buffer-size)) (current-buffer) nil 'rear-advance)))
813 ))
814
815
816 (defsubst vip-is-in-minibuffer ()
817 (string-match "\*Minibuf-" (buffer-name)))
818
819
820 \f
821 ;;; XEmacs compatibility
822
823 (defun vip-abbreviate-file-name (file)
824 (if vip-emacs-p
825 (abbreviate-file-name file)
826 ;; XEmacs requires addl argument
827 (abbreviate-file-name file t)))
828
829 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
830 ;; in sit-for, so this function smoothes out the differences.
831 (defsubst vip-sit-for-short (val &optional nodisp)
832 (if vip-xemacs-p
833 (sit-for (/ val 1000.0) nodisp)
834 (sit-for 0 val nodisp)))
835
836 ;; EVENT may be a single event of a sequence of events
837 (defsubst vip-ESC-event-p (event)
838 (let ((ESC-keys '(?\e (control \[) escape))
839 (key (vip-event-key event)))
840 (member key ESC-keys)))
841
842
843 (defsubst vip-mark-marker ()
844 (if vip-xemacs-p
845 (mark-marker t)
846 (mark-marker)))
847
848 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
849 ;; is the same as (mark t).
850 (defsubst vip-set-mark-if-necessary ()
851 (setq mark-ring (delete (vip-mark-marker) mark-ring))
852 (set-mark-command nil))
853
854 ;; In transient mark mode (zmacs mode), it is annoying when regions become
855 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
856 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
857 (defun vip-deactivate-mark ()
858 (if vip-xemacs-p
859 (zmacs-deactivate-region)
860 (deactivate-mark)))
861
862 (defsubst vip-leave-region-active ()
863 (if vip-xemacs-p
864 (setq zmacs-region-stays t)))
865
866
867 (defsubst vip-events-to-keys (events)
868 (cond (vip-xemacs-p (events-to-keys events))
869 (t events)))
870
871
872 (defun vip-eval-after-load (file form)
873 (if vip-emacs-p
874 (eval-after-load file form)
875 (or (assoc file after-load-alist)
876 (setq after-load-alist (cons (list file) after-load-alist)))
877 (let ((elt (assoc file after-load-alist)))
878 (or (member form (cdr elt))
879 (setq elt (nconc elt (list form)))))
880 form
881 ))
882
883 ;; This is here because Emacs changed the way local hooks work.
884 ;;
885 ;;Add to the value of HOOK the function FUNCTION.
886 ;;FUNCTION is not added if already present.
887 ;;FUNCTION is added (if necessary) at the beginning of the hook list
888 ;;unless the optional argument APPEND is non-nil, in which case
889 ;;FUNCTION is added at the end.
890 ;;
891 ;;HOOK should be a symbol, and FUNCTION may be any valid function. If
892 ;;HOOK is void, it is first set to nil. If HOOK's value is a single
893 ;;function, it is changed to a list of functions."
894 (defun vip-add-hook (hook function &optional append)
895 (if (not (boundp hook)) (set hook nil))
896 ;; If the hook value is a single function, turn it into a list.
897 (let ((old (symbol-value hook)))
898 (if (or (not (listp old)) (eq (car old) 'lambda))
899 (setq old (list old)))
900 (if (member function old)
901 nil
902 (set hook (if append
903 (append old (list function)) ; don't nconc
904 (cons function old))))))
905
906 ;; This is here because of Emacs's changes in the semantics of add/remove-hooks
907 ;; and due to the bugs they introduced.
908 ;;
909 ;; Remove from the value of HOOK the function FUNCTION.
910 ;; HOOK should be a symbol, and FUNCTION may be any valid function. If
911 ;; FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
912 ;; list of hooks to run in HOOK, then nothing is done. See `vip-add-hook'."
913 (defun vip-remove-hook (hook function)
914 (if (or (not (boundp hook)) ;unbound symbol, or
915 (null (symbol-value hook)) ;value is nil, or
916 (null function)) ;function is nil, then
917 nil ;Do nothing.
918 (let ((hook-value (symbol-value hook)))
919 (if (consp hook-value)
920 ;; don't side-effect the list
921 (setq hook-value (delete function (copy-sequence hook-value)))
922 (if (equal hook-value function)
923 (setq hook-value nil)))
924 (set hook hook-value))))
925
926
927
928 ;; like read-event, but in XEmacs also try to convert to char, if possible
929 (defun vip-read-event-convert-to-char ()
930 (let (event)
931 (if vip-emacs-p
932 (read-event)
933 (setq event (next-command-event))
934 (or (event-to-character event)
935 event))
936 ))
937
938 ;; This function lets function-key-map convert key sequences into logical
939 ;; keys. This does a better job than vip-read-event when it comes to kbd
940 ;; macros, since it enables certain macros to be shared between X and TTY modes
941 ;; by correctly mapping key sequences for Left/Right/... (one an ascii
942 ;; terminal) into logical keys left, right, etc.
943 (defun vip-read-key ()
944 (let ((overriding-local-map vip-overriding-map)
945 (inhibit-quit t)
946 key)
947 (use-global-map vip-overriding-map)
948 (setq key (elt (read-key-sequence nil) 0))
949 (use-global-map global-map)
950 key))
951
952
953 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
954 ;; instead of nil, if '(nil) was previously inadvertently assigned to
955 ;; unread-command-events
956 (defun vip-event-key (event)
957 (or (and event (eventp event))
958 (error "vip-event-key: Wrong type argument, eventp, %S" event))
959 (let ((mod (event-modifiers event))
960 basis)
961 (setq basis
962 (cond
963 (vip-xemacs-p
964 (cond ((key-press-event-p event)
965 (event-key event))
966 ((button-event-p event)
967 (concat "mouse-" (prin1-to-string (event-button event))))
968 (t
969 (error "vip-event-key: Unknown event, %S" event))))
970 (t
971 ;; Emacs doesn't handle capital letters correctly, since
972 ;; \S-a isn't considered the same as A (it behaves as
973 ;; plain `a' instead). So we take care of this here
974 (cond ((and (vip-characterp event) (<= ?A event) (<= event ?Z))
975 (setq mod nil
976 event event))
977 ;; Emacs has the oddity whereby characters 128+char
978 ;; represent M-char *if* this appears inside a string.
979 ;; So, we convert them manually to (meta char).
980 ((and (vip-characterp event) (< ?\C-? event) (<= event 255))
981 (setq mod '(meta)
982 event (- event ?\C-? 1)))
983 (t (event-basic-type event)))
984 )))
985 (if (vip-characterp basis)
986 (setq basis
987 (if (= basis ?\C-?)
988 (list 'control '\?) ; taking care of an emacs bug
989 (intern (char-to-string basis)))))
990 (if mod
991 (append mod (list basis))
992 basis)))
993
994 (defun vip-key-to-emacs-key (key)
995 (let (key-name char-p modifiers mod-char-list base-key base-key-name)
996 (cond (vip-xemacs-p key)
997 ((symbolp key)
998 (setq key-name (symbol-name key))
999 (if (= (length key-name) 1) ; character event
1000 (string-to-char key-name)
1001 key))
1002 ((listp key)
1003 (setq modifiers (subseq key 0 (1- (length key)))
1004 base-key (vip-seq-last-elt key)
1005 base-key-name (symbol-name base-key)
1006 char-p (= (length base-key-name) 1))
1007 (setq mod-char-list
1008 (mapcar
1009 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
1010 modifiers))
1011 (if char-p
1012 (setq key-name
1013 (car (read-from-string
1014 (concat
1015 "?\\"
1016 (mapconcat 'identity mod-char-list "-\\")
1017 "-"
1018 base-key-name))))
1019 (setq key-name
1020 (intern
1021 (concat
1022 (mapconcat 'identity mod-char-list "-")
1023 "-"
1024 base-key-name))))))
1025 ))
1026
1027
1028 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1029 ;; convert events to keys and, if all keys are regular printable
1030 ;; characters, will return a string. Otherwise, will return a string
1031 ;; representing a vector of converted events. If the input was a Viper macro,
1032 ;; will return a string that represents this macro as a vector.
1033 (defun vip-array-to-string (event-seq)
1034 (let (temp temp2)
1035 (cond ((stringp event-seq) event-seq)
1036 ((vip-event-vector-p event-seq)
1037 (setq temp (mapcar 'vip-event-key event-seq))
1038 (cond ((vip-char-symbol-sequence-p temp)
1039 (mapconcat 'symbol-name temp ""))
1040 ((and (vip-char-array-p
1041 (setq temp2 (mapcar 'vip-key-to-character temp))))
1042 (mapconcat 'char-to-string temp2 ""))
1043 (t (prin1-to-string (vconcat temp)))))
1044 ((vip-char-symbol-sequence-p event-seq)
1045 (mapconcat 'symbol-name event-seq ""))
1046 ((and (vectorp event-seq)
1047 (vip-char-array-p
1048 (setq temp (mapcar 'vip-key-to-character event-seq))))
1049 (mapconcat 'char-to-string temp ""))
1050 (t (prin1-to-string event-seq)))))
1051
1052 (defun vip-key-press-events-to-chars (events)
1053 (mapconcat (if vip-emacs-p
1054 'char-to-string
1055 (function
1056 (lambda (elt) (char-to-string (event-to-character elt)))))
1057 events
1058 ""))
1059
1060
1061 (defsubst vip-fast-keysequence-p ()
1062 (not (vip-sit-for-short vip-fast-keyseq-timeout t)))
1063
1064 (defun vip-read-char-exclusive ()
1065 (let (char
1066 (echo-keystrokes 1))
1067 (while (null char)
1068 (condition-case nil
1069 (setq char (read-char))
1070 (error
1071 ;; skip event if not char
1072 (vip-read-event))))
1073 char))
1074
1075 ;; key is supposed to be in viper's representation, e.g., (control l), a
1076 ;; character, etc.
1077 (defun vip-key-to-character (key)
1078 (cond ((eq key 'space) ?\ )
1079 ((eq key 'delete) ?\C-?)
1080 ((eq key 'backspace) ?\C-h)
1081 ((and (symbolp key)
1082 (= 1 (length (symbol-name key))))
1083 (string-to-char (symbol-name key)))
1084 ((and (listp key)
1085 (eq (car key) 'control)
1086 (symbol-name (nth 1 key))
1087 (= 1 (length (symbol-name (nth 1 key)))))
1088 (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
1089 (t key)))
1090
1091
1092 (defun vip-setup-master-buffer (&rest other-files-or-buffers)
1093 "Set up the current buffer as a master buffer.
1094 Arguments become related buffers. This function should normally be used in
1095 the `Local variables' section of a file."
1096 (setq vip-related-files-and-buffers-ring
1097 (make-ring (1+ (length other-files-or-buffers))))
1098 (mapcar '(lambda (elt)
1099 (vip-ring-insert vip-related-files-and-buffers-ring elt))
1100 other-files-or-buffers)
1101 (vip-ring-insert vip-related-files-and-buffers-ring (buffer-name))
1102 )
1103
1104 ;;; Movement utilities
1105
1106 (defvar vip-syntax-preference 'strict-vi
1107 "*Syntax type characterizing Viper's alphanumeric symbols.
1108 `emacs' means only word constituents are considered to be alphanumeric.
1109 Word constituents are symbols specified as word constituents by the current
1110 syntax table.
1111 `extended' means word and symbol constituents.
1112 `reformed-vi' means Vi-ish behavior: word constituents and the symbol `_'.
1113 However, word constituents are determined according to Emacs syntax tables,
1114 which may be different from Vi in some major modes.
1115 `strict-vi' means Viper words are exactly as in Vi.")
1116
1117 (vip-deflocalvar vip-ALPHA-char-class "w"
1118 "String of syntax classes characterizing Viper's alphanumeric symbols.
1119 In addition, the symbol `_' may be considered alphanumeric if
1120 `vip-syntax-preference'is `reformed-vi'.")
1121
1122 (vip-deflocalvar vip-strict-ALPHA-chars "a-zA-Z0-9_"
1123 "Regexp matching the set of alphanumeric characters acceptable to strict
1124 Vi.")
1125 (vip-deflocalvar vip-strict-SEP-chars " \t\n"
1126 "Regexp matching the set of alphanumeric characters acceptable to strict
1127 Vi.")
1128
1129 (vip-deflocalvar vip-SEP-char-class " -"
1130 "String of syntax classes for Vi separators.
1131 Usually contains ` ', linefeed, TAB or formfeed.")
1132
1133 (defun vip-update-alphanumeric-class ()
1134 "Set the syntactic class of Viper alphanumeric symbols according to
1135 the variable `vip-ALPHA-char-class'. Should be called in order for changes to
1136 `vip-ALPHA-char-class' to take effect."
1137 (interactive)
1138 (setq-default
1139 vip-ALPHA-char-class
1140 (cond ((eq vip-syntax-preference 'emacs) "w") ; only word constituents
1141 ((eq vip-syntax-preference 'extended) "w_") ; word & symbol chars
1142 (t "w")))) ; vi syntax: word constituents and the symbol `_'
1143
1144 ;; addl-chars are characters to be temporarily considered as alphanumerical
1145 (defun vip-looking-at-alpha (&optional addl-chars)
1146 (or (stringp addl-chars) (setq addl-chars ""))
1147 (if (eq vip-syntax-preference 'reformed-vi)
1148 (setq addl-chars (concat addl-chars "_")))
1149 (let ((char (char-after (point))))
1150 (if char
1151 (if (eq vip-syntax-preference 'strict-vi)
1152 (looking-at (concat "[" vip-strict-ALPHA-chars addl-chars "]"))
1153 (or (memq char
1154 ;; convert string to list
1155 (append (vconcat addl-chars) nil))
1156 (memq (char-syntax char)
1157 (append (vconcat vip-ALPHA-char-class) nil)))))
1158 ))
1159
1160 (defsubst vip-looking-at-separator ()
1161 (let ((char (char-after (point))))
1162 (if char
1163 (or (eq char ?\n) ; RET is always a separator in Vi
1164 (memq (char-syntax char)
1165 (append (vconcat vip-SEP-char-class) nil))))))
1166
1167 (defsubst vip-looking-at-alphasep (&optional addl-chars)
1168 (or (vip-looking-at-separator) (vip-looking-at-alpha addl-chars)))
1169
1170 (defsubst vip-skip-alpha-forward (&optional addl-chars)
1171 (or (stringp addl-chars) (setq addl-chars ""))
1172 (vip-skip-syntax
1173 'forward
1174 (cond ((eq vip-syntax-preference 'strict-vi)
1175 "")
1176 (t vip-ALPHA-char-class ))
1177 (cond ((eq vip-syntax-preference 'strict-vi)
1178 (concat vip-strict-ALPHA-chars addl-chars))
1179 (t addl-chars))))
1180
1181 (defsubst vip-skip-alpha-backward (&optional addl-chars)
1182 (or (stringp addl-chars) (setq addl-chars ""))
1183 (vip-skip-syntax
1184 'backward
1185 (cond ((eq vip-syntax-preference 'strict-vi)
1186 "")
1187 (t vip-ALPHA-char-class ))
1188 (cond ((eq vip-syntax-preference 'strict-vi)
1189 (concat vip-strict-ALPHA-chars addl-chars))
1190 (t addl-chars))))
1191
1192 ;; weird syntax tables may confuse strict-vi style
1193 (defsubst vip-skip-all-separators-forward (&optional within-line)
1194 (vip-skip-syntax 'forward
1195 vip-SEP-char-class
1196 (or within-line "\n")
1197 (if within-line (vip-line-pos 'end))))
1198 (defsubst vip-skip-all-separators-backward (&optional within-line)
1199 (vip-skip-syntax 'backward
1200 vip-SEP-char-class
1201 (or within-line "\n")
1202 (if within-line (vip-line-pos 'start))))
1203 (defun vip-skip-nonseparators (direction)
1204 (let ((func (intern (format "skip-syntax-%S" direction))))
1205 (funcall func (concat "^" vip-SEP-char-class)
1206 (vip-line-pos (if (eq direction 'forward) 'end 'start)))))
1207
1208 (defsubst vip-skip-nonalphasep-forward ()
1209 (if (eq vip-syntax-preference 'strict-vi)
1210 (skip-chars-forward
1211 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
1212 (skip-syntax-forward
1213 (concat
1214 "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'end))))
1215 (defsubst vip-skip-nonalphasep-backward ()
1216 (if (eq vip-syntax-preference 'strict-vi)
1217 (skip-chars-backward
1218 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
1219 (skip-syntax-backward
1220 (concat
1221 "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'start))))
1222
1223 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1224 ;; Return the number of chars traveled.
1225 ;; Either SYNTAX or ADDL-CHARS can be nil, in which case they are interpreted
1226 ;; as an empty string.
1227 (defun vip-skip-syntax (direction syntax addl-chars &optional limit)
1228 (let ((total 0)
1229 (local 1)
1230 (skip-chars-func (intern (format "skip-chars-%S" direction)))
1231 (skip-syntax-func (intern (format "skip-syntax-%S" direction))))
1232 (or (stringp addl-chars) (setq addl-chars ""))
1233 (or (stringp syntax) (setq syntax ""))
1234 (while (and (not (= local 0)) (not (eobp)))
1235 (setq local
1236 (+ (funcall skip-syntax-func syntax limit)
1237 (funcall skip-chars-func addl-chars limit)))
1238 (setq total (+ total local)))
1239 total
1240 ))
1241
1242
1243
1244
1245 (provide 'viper-util)
1246
1247 ;;; viper-util.el ends here