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