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)
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.
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
48 (defconst vip-xemacs-p (string-match "\\(Lucid\\|XEmacs\\)" emacs-version))
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 ()
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-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.")
65 (defun vip-has-face-support-p ()
66 (cond ((vip-window-display-p))
68 (vip-emacs-p (memq (vip-device-type) '(pc)))
69 (vip-xemacs-p (memq (vip-device-type) '(tty pc)))))
74 (defmacro vip-deflocalvar (var default-value &optional documentation)
76 (defvar (, var) (, default-value)
77 (, (format "%s\n\(buffer local\)" documentation)))
78 (make-variable-buffer-local '(, var))
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)
86 '(setq count (1- count))
89 (defmacro vip-buffer-live-p (buf)
90 (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf))))))
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)))))
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)))))
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)))))
119 ;; last elt of a sequence
120 (defsubst vip-seq-last-elt (seq)
121 (elt seq (1- (length seq))))
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)))
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)))
145 (and (<= pos (point-max)) (<= (point-min) pos))))))
148 (defvar vip-minibuffer-overlay-priority 300)
149 (defvar vip-replace-overlay-priority 400)
150 (defvar vip-search-overlay-priority 500)
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))
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))
187 (fset 'vip-characterp
189 (if vip-xemacs-p 'characterp 'integerp)))
191 (defsubst vip-color-display-p ()
194 (eq (device-class (selected-device)) 'color)))
196 (defsubst vip-get-cursor-color ()
198 (cdr (assoc 'cursor-color (frame-parameters)))
199 (color-instance-name (frame-property (selected-frame) 'cursor-color))))
203 (cond ((eq (vip-device-type) 'pm)
204 (fset 'vip-color-defined-p
205 (function (lambda (color) (assoc color pm-color-alist))))))
207 ;; needed to smooth out the difference between Emacs and XEmacs
208 (defsubst vip-italicize-face (face)
210 (make-face-italic face)
211 (make-face-italic face nil 'noerror)))
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)))
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)))
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)))))
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)))))
238 (defsubst vip-restore-cursor-color ()
239 (vip-change-cursor-color
240 (vip-overlay-get vip-replace-overlay 'vip-cursor-color)))
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)
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)
266 (error "%S: Invalid op in vip-check-version" op))))
267 (cond ((memq op '(= > >=)) nil)
268 ((memq op '(< <=)) t))))
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))
274 ;; (with-output-to-temp-buffer " *vip-info*"
275 ;; (switch-to-buffer " *vip-info*")
279 ;;This version of Viper requires
281 ;;\t Emacs 19.29 and higher
283 ;;\t XEmacs 19.12 and higher
285 ;;It is unlikely to work under Emacs version %s
286 ;;that you are using... " emacs-version))
288 ;; (if noninteractive
292 ;; (insert "\n\nType any key to continue... ")
293 ;; (vip-read-event)))
294 ;; (kill-buffer " *vip-info*")))
297 (defun vip-get-visible-buffer-window (wind)
299 (get-buffer-window wind t)
300 (get-buffer-window wind 'visible)))
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))
317 (goto-char (+ (vip-line-pos 'start) (vip-line-pos 'end) 2)))
319 (back-to-indentation))
321 (setq result (point))
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).
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))
337 (set var (make-marker)))
338 (move-marker (eval var) pos buffer))
341 ;; Print CONDITIONS as a message.
342 (defun vip-message-conditions (conditions)
343 (let ((case (car conditions)) (msg (cdr conditions)))
346 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
351 ;;; List/alist utilities
353 ;; Convert LIST to an alist
354 (defun vip-list-to-alist (lst)
357 (setq alist (cons (list (car lst)) alist))
358 (setq lst (cdr lst)))
361 ;; Convert ALIST to a list.
362 (defun vip-alist-to-list (alst)
365 (setq lst (cons (car (car alst)) lst))
366 (setq alst (cdr alst)))
369 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
370 (defun vip-filter-alist (regexp alst)
372 (let ((outalst) (inalst alst))
374 (if (string-match regexp (car (car inalst)))
375 (setq outalst (cons (car inalst) outalst)))
376 (setq inalst (cdr inalst)))
379 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
380 (defun vip-filter-list (regexp lst)
382 (let ((outlst) (inlst lst))
384 (if (string-match regexp (car inlst))
385 (setq outlst (cons (car inlst) outlst)))
386 (setq inlst (cdr inlst)))
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)
397 ;;filter-append the second list
399 ;; delete all occurrences
400 (while (setq elt (assoc (car (car temp)) lis2))
401 (setq lis2 (delq elt lis2)))
402 (setq temp (cdr temp)))
407 ;;; Support for :e and file globbing
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.
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'
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
421 ;; using cond in anticipation of further additions
422 (cond (ex-unix-type-shell-options)
424 (command (cond (vip-ms-style-os-p (format "\"ls -1 %s\"" filespec))
425 (t (format "ls -1 %s" filespec))))
428 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
432 (call-process gshell nil t nil
436 (call-process gshell nil t nil
439 (goto-char (point-min))
440 ;; Issue an error, if no match.
443 (skip-chars-forward " \t\n\j")
444 (if (looking-at "ls:")
445 (vip-forward-Word 1))
450 (buffer-substring (point) (vip-line-pos 'end)))
452 (goto-char (point-min))
453 (setq file-list (vip-get-filenames-from-buffer 'one-per-line)))
455 (mapcar 'find-file file-list)
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.
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'."
468 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
471 (goto-char (point-min))
473 (vip-glob-ms-windows-files (vip-get-filenames-from-buffer)))
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
481 (defun vip-get-filenames-from-buffer (&optional one-per-line)
482 (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
484 (skip-chars-forward skip-chars)
486 (if (cond ((looking-at "\"")
488 (re-search-forward "[^\"]+" nil t)) ; noerror
491 (re-search-forward "[^']+" nil t)) ; noerror
494 (concat "[^" skip-chars "]+") nil t))) ;noerror
496 (buffer-substring (match-beginning 0) (match-end 0))))
499 (skip-chars-forward " \t\n")
500 (setq result (cons fname result)))
503 ;; convert MS-DOS wildcards to regexp
504 (defun vip-wildcard-to-regexp (wcard)
506 (set-buffer (setq tmp-buf (get-buffer-create vip-ex-tmp-buf-name)))
509 (goto-char (point-min))
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 ".")))
521 ;; glob windows files
522 ;; LIST is expected to be in reverse order
523 (defun vip-glob-ms-windows-files (list)
528 (setq tmp2 (cons (directory-files
529 ;; the directory part
530 (or (file-name-directory (car tmp))
532 t ; return full names
533 ;; the regexp part: globs the file names
535 (vip-wildcard-to-regexp
536 (file-name-nondirectory (car tmp)))
539 (setq tmp (cdr tmp)))
540 (reverse (apply 'append tmp2))))
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))
552 (setcar ring (cond ((> dir 0)
553 (ring-plus1 (car ring) (ring-length ring)))
555 (ring-minus1 (car ring) (ring-length ring)))
556 ;; don't rotate if dir = 0
558 (vip-current-ring-item ring)
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)))
569 ;; current ring item; if N is given, then so many items back from the
571 (defun vip-current-ring-item (ring &optional n)
573 (if (and (ring-p ring) (> (ring-length ring) 0))
574 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
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))
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))))
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))
613 (idx (max 0 (ring-minus1 hd ln)))
614 (top-elt (aref vec idx)))
617 (while (< (1+ idx) veclen)
618 (aset vec idx (aref vec (1+ idx)))
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
629 (defun vip-ring-insert (ring item)
630 (let* ((ln (ring-length ring))
631 (vec (cdr (cdr ring)))
632 (veclen (length vec))
634 (vecpos-after-hd (if (= hd 0) ln hd))
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)))
646 (aset vec vecpos-after-hd item))
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)
662 (substring string 0 (min max-len (length string)))))
663 (cond ((null truncated-str) "")
664 ((> (length string) max-len)
666 pre-string truncated-str abbrev-sign post-string))
667 (t (format "%s%s%s" pre-string truncated-str post-string)))))
669 ;; tells if we are over a whitespace-only line
670 (defsubst vip-over-whitespace-line ()
673 (looking-at "^[ \t]*$")))
676 ;;; Saving settings in custom file
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)))
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)))
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))))
715 (goto-char (point-min))
716 (if pattern (delete-matching-lines pattern))
717 (goto-char (point-max))
718 (if string (insert string))
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
733 (match-beginning 0) (match-end 0) (current-buffer))))
735 (vip-overlay-put vip-search-overlay 'priority vip-search-overlay-priority)
736 (if (vip-has-face-support-p)
738 (vip-overlay-put vip-search-overlay 'face vip-search-face)
740 (vip-overlay-put vip-search-overlay 'face nil))))
744 (defsubst vip-move-replace-overlay (beg end)
745 (vip-move-overlay vip-replace-overlay beg end))
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)))
753 vip-replace-overlay (if vip-emacs-p 'evaporate 'detachable) nil)
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)
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))))
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)))
778 (defsubst vip-replace-start ()
779 (vip-overlay-start vip-replace-overlay))
780 (defsubst vip-replace-end ()
781 (vip-overlay-end vip-replace-overlay))
786 (defun vip-set-minibuffer-overlay ()
787 (vip-check-minibuffer-overlay)
788 (if (vip-has-face-support-p)
791 vip-minibuffer-overlay 'face vip-minibuffer-current-face)
793 vip-minibuffer-overlay 'priority vip-minibuffer-overlay-priority)
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
801 (vip-overlay-put vip-minibuffer-overlay 'start-open nil)
802 (vip-overlay-put vip-minibuffer-overlay 'end-open nil)))
805 (defun vip-check-minibuffer-overlay ()
806 (or (vip-overlay-p vip-minibuffer-overlay)
807 (setq vip-minibuffer-overlay
809 (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer))
810 ;; make overlay open-ended
812 1 (1+ (buffer-size)) (current-buffer) nil 'rear-advance)))
816 (defsubst vip-is-in-minibuffer ()
817 (string-match "\*Minibuf-" (buffer-name)))
821 ;;; XEmacs compatibility
823 (defun vip-abbreviate-file-name (file)
825 (abbreviate-file-name file)
826 ;; XEmacs requires addl argument
827 (abbreviate-file-name file t)))
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)
833 (sit-for (/ val 1000.0) nodisp)
834 (sit-for 0 val nodisp)))
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)))
843 (defsubst vip-mark-marker ()
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))
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 ()
859 (zmacs-deactivate-region)
862 (defsubst vip-leave-region-active ()
864 (setq zmacs-region-stays t)))
867 (defsubst vip-events-to-keys (events)
868 (cond (vip-xemacs-p (events-to-keys events))
872 (defun vip-eval-after-load (file form)
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)))))
883 ;; This is here because Emacs changed the way local hooks work.
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.
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)
903 (append old (list function)) ; don't nconc
904 (cons function old))))))
906 ;; This is here because of Emacs's changes in the semantics of add/remove-hooks
907 ;; and due to the bugs they introduced.
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
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))))
928 ;; like read-event, but in XEmacs also try to convert to char, if possible
929 (defun vip-read-event-convert-to-char ()
933 (setq event (next-command-event))
934 (or (event-to-character event)
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)
947 (use-global-map vip-overriding-map)
948 (setq key (elt (read-key-sequence nil) 0))
949 (use-global-map global-map)
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))
964 (cond ((key-press-event-p event)
966 ((button-event-p event)
967 (concat "mouse-" (prin1-to-string (event-button event))))
969 (error "vip-event-key: Unknown event, %S" event))))
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))
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))
982 event (- event ?\C-? 1)))
983 (t (event-basic-type event)))
985 (if (vip-characterp basis)
988 (list 'control '\?) ; taking care of an emacs bug
989 (intern (char-to-string basis)))))
991 (append mod (list basis))
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)
998 (setq key-name (symbol-name key))
999 (if (= (length key-name) 1) ; character event
1000 (string-to-char key-name)
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))
1009 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
1013 (car (read-from-string
1016 (mapconcat 'identity mod-char-list "-\\")
1022 (mapconcat 'identity mod-char-list "-")
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)
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)
1048 (setq temp (mapcar 'vip-key-to-character event-seq))))
1049 (mapconcat 'char-to-string temp ""))
1050 (t (prin1-to-string event-seq)))))
1052 (defun vip-key-press-events-to-chars (events)
1053 (mapconcat (if vip-emacs-p
1056 (lambda (elt) (char-to-string (event-to-character elt)))))
1061 (defsubst vip-fast-keysequence-p ()
1062 (not (vip-sit-for-short vip-fast-keyseq-timeout t)))
1064 (defun vip-read-char-exclusive ()
1066 (echo-keystrokes 1))
1069 (setq char (read-char))
1071 ;; skip event if not char
1075 ;; key is supposed to be in viper's representation, e.g., (control l), a
1077 (defun vip-key-to-character (key)
1078 (cond ((eq key 'space) ?\ )
1079 ((eq key 'delete) ?\C-?)
1080 ((eq key 'backspace) ?\C-h)
1082 (= 1 (length (symbol-name key))))
1083 (string-to-char (symbol-name 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)))))
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))
1104 ;;; Movement utilities
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
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.")
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'.")
1122 (vip-deflocalvar vip-strict-ALPHA-chars "a-zA-Z0-9_"
1123 "Regexp matching the set of alphanumeric characters acceptable to strict
1125 (vip-deflocalvar vip-strict-SEP-chars " \t\n"
1126 "Regexp matching the set of alphanumeric characters acceptable to strict
1129 (vip-deflocalvar vip-SEP-char-class " -"
1130 "String of syntax classes for Vi separators.
1131 Usually contains ` ', linefeed, TAB or formfeed.")
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."
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 `_'
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))))
1151 (if (eq vip-syntax-preference 'strict-vi)
1152 (looking-at (concat "[" vip-strict-ALPHA-chars addl-chars "]"))
1154 ;; convert string to list
1155 (append (vconcat addl-chars) nil))
1156 (memq (char-syntax char)
1157 (append (vconcat vip-ALPHA-char-class) nil)))))
1160 (defsubst vip-looking-at-separator ()
1161 (let ((char (char-after (point))))
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))))))
1167 (defsubst vip-looking-at-alphasep (&optional addl-chars)
1168 (or (vip-looking-at-separator) (vip-looking-at-alpha addl-chars)))
1170 (defsubst vip-skip-alpha-forward (&optional addl-chars)
1171 (or (stringp addl-chars) (setq addl-chars ""))
1174 (cond ((eq vip-syntax-preference 'strict-vi)
1176 (t vip-ALPHA-char-class ))
1177 (cond ((eq vip-syntax-preference 'strict-vi)
1178 (concat vip-strict-ALPHA-chars addl-chars))
1181 (defsubst vip-skip-alpha-backward (&optional addl-chars)
1182 (or (stringp addl-chars) (setq addl-chars ""))
1185 (cond ((eq vip-syntax-preference 'strict-vi)
1187 (t vip-ALPHA-char-class ))
1188 (cond ((eq vip-syntax-preference 'strict-vi)
1189 (concat vip-strict-ALPHA-chars addl-chars))
1192 ;; weird syntax tables may confuse strict-vi style
1193 (defsubst vip-skip-all-separators-forward (&optional within-line)
1194 (vip-skip-syntax 'forward
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
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)))))
1208 (defsubst vip-skip-nonalphasep-forward ()
1209 (if (eq vip-syntax-preference 'strict-vi)
1211 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars))
1212 (skip-syntax-forward
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
1221 "^" vip-ALPHA-char-class vip-SEP-char-class) (vip-line-pos 'start))))
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)
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)))
1236 (+ (funcall skip-syntax-func syntax limit)
1237 (funcall skip-chars-func addl-chars limit)))
1238 (setq total (+ total local)))
1245 (provide 'viper-util)
1247 ;;; viper-util.el ends here