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
()
197 (cdr (assoc 'cursor-color
(frame-parameters))))
200 (cond ((eq (vip-device-type) 'pm
)
201 (fset 'vip-color-defined-p
202 (function (lambda (color) (assoc color pm-color-alist
))))))
204 ;; needed to smooth out the difference between Emacs and XEmacs
205 (defsubst vip-italicize-face
(face)
207 (make-face-italic face
)
208 (make-face-italic face nil
'noerror
)))
210 ;; test if display is color and the colors are defined
211 (defsubst vip-can-use-colors
(&rest colors
)
212 (if (vip-color-display-p)
213 (not (memq nil
(mapcar 'vip-color-defined-p colors
)))
216 (defun vip-hide-face (face)
217 (if (and (vip-has-face-support-p) vip-emacs-p
)
218 (add-to-list 'facemenu-unlisted-faces face
)))
221 (defun vip-change-cursor-color (new-color)
222 (if (and (vip-window-display-p) (vip-color-display-p)
223 (stringp new-color
) (vip-color-defined-p new-color
)
224 (not (string= new-color
(vip-get-cursor-color))))
225 (modify-frame-parameters
226 (selected-frame) (list (cons 'cursor-color new-color
)))))
228 (defsubst vip-save-cursor-color
()
229 (if (and (vip-window-display-p) (vip-color-display-p))
230 (let ((color (vip-get-cursor-color)))
231 (if (and (stringp color
) (vip-color-defined-p color
)
232 (not (string= color vip-replace-overlay-cursor-color
)))
233 (vip-overlay-put vip-replace-overlay
'vip-cursor-color color
)))))
235 (defsubst vip-restore-cursor-color
()
236 (vip-change-cursor-color
237 (vip-overlay-get vip-replace-overlay
'vip-cursor-color
)))
240 ;; Check the current version against the major and minor version numbers
241 ;; using op: cur-vers op major.minor If emacs-major-version or
242 ;; emacs-minor-version are not defined, we assume that the current version
243 ;; is hopelessly outdated. We assume that emacs-major-version and
244 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
245 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
246 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
247 ;; incorrect. However, this gives correct result in our cases, since we are
248 ;; testing for sufficiently high Emacs versions.
249 (defun vip-check-version (op major minor
&optional type-of-emacs
)
250 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
251 (and (cond ((eq type-of-emacs
'xemacs
) vip-xemacs-p
)
252 ((eq type-of-emacs
'emacs
) vip-emacs-p
)
254 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
255 (= emacs-major-version major
)))
256 ((memq op
'(> >= < <=))
257 (and (or (funcall op emacs-major-version major
)
258 (= emacs-major-version major
))
259 (if (= emacs-major-version major
)
260 (funcall op emacs-minor-version minor
)
263 (error "%S: Invalid op in vip-check-version" op
))))
264 (cond ((memq op
'(= > >=)) nil
)
265 ((memq op
'(< <=)) t
))))
267 ;;;; warn if it is a wrong version of emacs
268 ;;(if (or (vip-check-version '< 19 29 'emacs)
269 ;; (vip-check-version '< 19 12 'xemacs))
271 ;; (with-output-to-temp-buffer " *vip-info*"
272 ;; (switch-to-buffer " *vip-info*")
276 ;;This version of Viper requires
278 ;;\t Emacs 19.29 and higher
280 ;;\t XEmacs 19.12 and higher
282 ;;It is unlikely to work under Emacs version %s
283 ;;that you are using... " emacs-version))
285 ;; (if noninteractive
289 ;; (insert "\n\nType any key to continue... ")
290 ;; (vip-read-event)))
291 ;; (kill-buffer " *vip-info*")))
294 (defun vip-get-visible-buffer-window (wind)
296 (get-buffer-window wind t
)
297 (get-buffer-window wind
'visible
)))
300 ;; Return line position.
301 ;; If pos is 'start then returns position of line start.
302 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
303 ;; Pos = 'indent returns beginning of indentation.
304 ;; Otherwise, returns point. Current point is not moved in any case."
305 (defun vip-line-pos (pos)
306 (let ((cur-pos (point))
314 (goto-char (+ (vip-line-pos 'start
) (vip-line-pos 'end
) 2)))
316 (back-to-indentation))
318 (setq result
(point))
323 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
324 ;; The first argument must eval to a variable name.
325 ;; Arguments: (var-name position &optional buffer).
327 ;; This is useful for moving markers that are supposed to be local.
328 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
329 ;; Then, each time this var is used in `vip-move-marker-locally' in a new
330 ;; buffer, a new marker will be created.
331 (defun vip-move-marker-locally (var pos
&optional buffer
)
332 (if (markerp (eval var
))
334 (set var
(make-marker)))
335 (move-marker (eval var
) pos buffer
))
338 ;; Print CONDITIONS as a message.
339 (defun vip-message-conditions (conditions)
340 (let ((case (car conditions
)) (msg (cdr conditions
)))
343 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
348 ;;; List/alist utilities
350 ;; Convert LIST to an alist
351 (defun vip-list-to-alist (lst)
354 (setq alist
(cons (list (car lst
)) alist
))
355 (setq lst
(cdr lst
)))
358 ;; Convert ALIST to a list.
359 (defun vip-alist-to-list (alst)
362 (setq lst
(cons (car (car alst
)) lst
))
363 (setq alst
(cdr alst
)))
366 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
367 (defun vip-filter-alist (regexp alst
)
369 (let ((outalst) (inalst alst
))
371 (if (string-match regexp
(car (car inalst
)))
372 (setq outalst
(cons (car inalst
) outalst
)))
373 (setq inalst
(cdr inalst
)))
376 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
377 (defun vip-filter-list (regexp lst
)
379 (let ((outlst) (inlst lst
))
381 (if (string-match regexp
(car inlst
))
382 (setq outlst
(cons (car inlst
) outlst
)))
383 (setq inlst
(cdr inlst
)))
387 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
388 ;; LIS2 is modified by filtering it: deleting its members of the form
389 ;; \(car elt\) such that (car elt') is in LIS1.
390 (defun vip-append-filter-alist (lis1 lis2
)
394 ;;filter-append the second list
396 ;; delete all occurrences
397 (while (setq elt
(assoc (car (car temp
)) lis2
))
398 (setq lis2
(delq elt lis2
)))
399 (setq temp
(cdr temp
)))
404 ;;; Support for :e and file globbing
406 (defun vip-ex-nontrivial-find-file-unix (filespec)
407 "Glob the file spec and visit all files matching the spec.
408 This function is designed to work under Unix. It may also work under VMS.
410 Users who prefer other types of shells should write their own version of this
411 function and set the variable `ex-nontrivial-find-file-function'
414 (cond (ex-unix-type-shell shell-file-name
)
415 ((memq system-type
'(vax-vms axp-vms
)) "*dcl*") ; VAX VMS
416 (t "sh"))) ; probably Unix anyway
418 ;; using cond in anticipation of further additions
419 (cond (ex-unix-type-shell-options)
421 (command (cond (vip-ms-style-os-p (format "\"ls -1 %s\"" filespec
))
422 (t (format "ls -1 %s" filespec
))))
425 (set-buffer (setq tmp-buf
(get-buffer-create vip-ex-tmp-buf-name
)))
429 (call-process gshell nil t nil
433 (call-process gshell nil t nil
436 (goto-char (point-min))
437 ;; Issue an error, if no match.
440 (skip-chars-forward " \t\n\j")
441 (if (looking-at "ls:")
442 (vip-forward-Word 1))
447 (buffer-substring (point) (vip-line-pos 'end
)))
449 (goto-char (point-min))
450 (setq file-list
(vip-get-filenames-from-buffer 'one-per-line
)))
452 (mapcar 'find-file file-list
)
455 (defun vip-ex-nontrivial-find-file-ms (filespec)
456 "Glob the file spec and visit all files matching the spec.
457 This function is designed to work under MS type systems, such as NT, W95, and
458 DOS. It may also work under OS/2.
460 The users of Unix-type shells should be able to use
461 `vip-ex-nontrivial-find-file-unix', making it into the value of the variable
462 `ex-nontrivial-find-file-function'. If this doesn't work, the user may have
463 to write a custom function, similar to `vip-ex-nontrivial-find-file-unix'."
465 (set-buffer (setq tmp-buf
(get-buffer-create vip-ex-tmp-buf-name
)))
468 (goto-char (point-min))
470 (vip-glob-ms-windows-files (vip-get-filenames-from-buffer)))
474 ;; Interpret the stuff in the buffer as a list of file names
475 ;; return a list of file names listed in the buffer beginning at point
476 ;; If optional arg is supplied, assume each filename is listed on a separate
478 (defun vip-get-filenames-from-buffer (one-per-line)
479 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
481 (skip-chars-forward skip-chars
)
483 (if (cond ((looking-at "\"")
485 (re-search-forward "[^\"]+" nil t
)) ; noerror
488 (re-search-forward "[^']+" nil t
)) ; noerror
491 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
493 (buffer-substring (match-beginning 0) (match-end 0))))
496 (skip-chars-forward " \t\n")
497 (setq result
(cons fname result
)))
500 ;; convert MS-DOS wildcards to regexp
501 (defun vip-wildcard-to-regexp (wcard)
503 (set-buffer (setq tmp-buf
(get-buffer-create vip-ex-tmp-buf-name
)))
506 (goto-char (point-min))
508 (skip-chars-forward "^*?.\\\\")
509 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
510 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
511 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
512 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
518 ;; glob windows files
519 ;; LIST is expected to be in reverse order
520 (defun vip-glob-ms-windows-files (list)
525 (setq tmp2
(cons (directory-files
526 ;; the directory part
527 (or (file-name-directory (car tmp
))
529 t
; return full names
530 ;; the regexp part: globs the file names
532 (vip-wildcard-to-regexp
533 (file-name-nondirectory (car tmp
)))
536 (setq tmp
(cdr tmp
)))
537 (reverse (apply 'append tmp2
))))
545 ;; Rotate RING's index. DIRection can be positive or negative.
546 (defun vip-ring-rotate1 (ring dir
)
547 (if (and (ring-p ring
) (> (ring-length ring
) 0))
549 (setcar ring
(cond ((> dir
0)
550 (ring-plus1 (car ring
) (ring-length ring
)))
552 (ring-minus1 (car ring
) (ring-length ring
)))
553 ;; don't rotate if dir = 0
555 (vip-current-ring-item ring
)
558 (defun vip-special-ring-rotate1 (ring dir
)
559 (if (memq vip-intermediate-command
560 '(repeating-display-destructive-command
561 repeating-insertion-from-ring
))
562 (vip-ring-rotate1 ring dir
)
563 ;; don't rotate otherwise
564 (vip-ring-rotate1 ring
0)))
566 ;; current ring item; if N is given, then so many items back from the
568 (defun vip-current-ring-item (ring &optional n
)
570 (if (and (ring-p ring
) (> (ring-length ring
) 0))
571 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
573 ;; push item onto ring. the second argument is a ring-variable, not value.
574 (defun vip-push-onto-ring (item ring-var
)
575 (or (ring-p (eval ring-var
))
576 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
577 (or (null item
) ; don't push nil
578 (and (stringp item
) (string= item
"")) ; or empty strings
579 (equal item
(vip-current-ring-item (eval ring-var
))) ; or old stuff
580 ;; Since vip-set-destructive-command checks if we are inside vip-repeat,
581 ;; we don't check whether this-command-keys is a `.'.
582 ;; The cmd vip-repeat makes a call to the current function only if
583 ;; `.' is executing a command from the command history. It doesn't
584 ;; call the push-onto-ring function if `.' is simply repeating the
585 ;; last destructive command.
586 ;; We only check for ESC (which happens when we do insert with a
587 ;; prefix argument, or if this-command-keys doesn't give anything
588 ;; meaningful (in that case we don't know what to show to the user).
589 (and (eq ring-var
'vip-command-ring
)
590 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
591 (vip-array-to-string (this-command-keys))))
592 (vip-ring-insert (eval ring-var
) item
))
596 ;; removing elts from ring seems to break it
597 (defun vip-cleanup-ring (ring)
598 (or (< (ring-length ring
) 2)
599 (null (vip-current-ring-item ring
))
600 ;; last and previous equal
601 (if (equal (vip-current-ring-item ring
) (vip-current-ring-item ring
1))
602 (vip-ring-pop ring
))))
604 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
605 (defun vip-ring-pop (ring)
606 (let* ((ln (ring-length ring
))
607 (vec (cdr (cdr ring
)))
608 (veclen (length vec
))
610 (idx (max 0 (ring-minus1 hd ln
)))
611 (top-elt (aref vec idx
)))
614 (while (< (1+ idx
) veclen
)
615 (aset vec idx
(aref vec
(1+ idx
)))
619 (setq hd
(max 0 (ring-minus1 hd ln
)))
620 (if (= hd
(1- ln
)) (setq hd
0))
621 (setcar ring hd
) ; move head
622 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
626 (defun vip-ring-insert (ring item
)
627 (let* ((ln (ring-length ring
))
628 (vec (cdr (cdr ring
)))
629 (veclen (length vec
))
631 (vecpos-after-hd (if (= hd
0) ln hd
))
636 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
637 (setcar ring
(ring-plus1 hd ln
)))
638 (setcar (cdr ring
) (1+ ln
))
639 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
640 (while (and (>= idx vecpos-after-hd
) (> ln
0))
641 (aset vec idx
(aref vec
(1- idx
)))
643 (aset vec vecpos-after-hd item
))
649 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
650 ;; PRE-STRING is a string to prepend to the abbrev string.
651 ;; POST-STRING is a string to append to the abbrev string.
652 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
653 ;; if the orig string was truncated.
654 (defun vip-abbreviate-string (string max-len
655 pre-string post-string abbrev-sign
)
659 (substring string
0 (min max-len
(length string
)))))
660 (cond ((null truncated-str
) "")
661 ((> (length string
) max-len
)
663 pre-string truncated-str abbrev-sign post-string
))
664 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
666 ;; tells if we are over a whitespace-only line
667 (defsubst vip-over-whitespace-line
()
670 (looking-at "^[ \t]*$")))
673 ;;; Saving settings in custom file
675 ;; Save the current setting of VAR in CUSTOM-FILE.
676 ;; If given, MESSAGE is a message to be displayed after that.
677 ;; This message is erased after 2 secs, if erase-msg is non-nil.
678 ;; Arguments: var message custom-file &optional erase-message
679 (defun vip-save-setting (var message custom-file
&optional erase-msg
)
680 (let* ((var-name (symbol-name var
))
681 (var-val (if (boundp var
) (eval var
)))
682 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
683 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
688 (goto-char (point-min))
689 (if (re-search-forward regexp nil t
)
690 (let ((reg-end (1- (match-end 0))))
691 (search-backward var-name
)
692 (delete-region (match-beginning 0) reg-end
)
693 (goto-char (match-beginning 0))
694 (insert (format "%s '%S" var-name var-val
)))
695 (goto-char (point-max))
696 (if (not (bolp)) (insert "\n"))
697 (insert (format "(setq %s '%S)\n" var-name var-val
)))
706 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
707 ;; match this pattern.
708 (defun vip-save-string-in-file (string custom-file
&optional pattern
)
709 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
712 (goto-char (point-min))
713 (if pattern
(delete-matching-lines pattern
))
714 (goto-char (point-max))
715 (if string
(insert string
))
725 (defun vip-flash-search-pattern ()
726 (if (vip-overlay-p vip-search-overlay
)
727 (vip-move-overlay vip-search-overlay
(match-beginning 0) (match-end 0))
728 (setq vip-search-overlay
730 (match-beginning 0) (match-end 0) (current-buffer))))
732 (vip-overlay-put vip-search-overlay
'priority vip-search-overlay-priority
)
733 (if (vip-has-face-support-p)
735 (vip-overlay-put vip-search-overlay
'face vip-search-face
)
737 (vip-overlay-put vip-search-overlay
'face nil
))))
741 (defsubst vip-move-replace-overlay
(beg end
)
742 (vip-move-overlay vip-replace-overlay beg end
))
744 (defun vip-set-replace-overlay (beg end
)
745 (if (vip-overlay-p vip-replace-overlay
)
746 (vip-move-replace-overlay beg end
)
747 (setq vip-replace-overlay
(vip-make-overlay beg end
(current-buffer)))
750 vip-replace-overlay
(if vip-emacs-p
'evaporate
'detachable
) nil
)
752 vip-replace-overlay
'priority vip-replace-overlay-priority
))
753 (if (vip-has-face-support-p)
754 (vip-overlay-put vip-replace-overlay
'face vip-replace-overlay-face
))
755 (vip-save-cursor-color)
756 (vip-change-cursor-color vip-replace-overlay-cursor-color
)
760 (defsubst vip-set-replace-overlay-glyphs
(before-glyph after-glyph
)
761 (if (or (not (vip-has-face-support-p))
762 vip-use-replace-region-delimiters
)
763 (let ((before-name (if vip-xemacs-p
'begin-glyph
'before-string
))
764 (after-name (if vip-xemacs-p
'end-glyph
'after-string
)))
765 (vip-overlay-put vip-replace-overlay before-name before-glyph
)
766 (vip-overlay-put vip-replace-overlay after-name after-glyph
))))
768 (defsubst vip-hide-replace-overlay
()
769 (vip-set-replace-overlay-glyphs nil nil
)
770 (vip-restore-cursor-color)
771 (if (vip-has-face-support-p)
772 (vip-overlay-put vip-replace-overlay
'face nil
)))
775 (defsubst vip-replace-start
()
776 (vip-overlay-start vip-replace-overlay
))
777 (defsubst vip-replace-end
()
778 (vip-overlay-end vip-replace-overlay
))
783 (defun vip-set-minibuffer-overlay ()
784 (vip-check-minibuffer-overlay)
785 (if (vip-has-face-support-p)
788 vip-minibuffer-overlay
'face vip-minibuffer-current-face
)
790 vip-minibuffer-overlay
'priority vip-minibuffer-overlay-priority
)
793 vip-minibuffer-overlay
(if vip-emacs-p
'evaporate
'detachable
) nil
)
794 ;; make vip-minibuffer-overlay open-ended
795 ;; In emacs, it is made open ended at creation time
798 (vip-overlay-put vip-minibuffer-overlay
'start-open nil
)
799 (vip-overlay-put vip-minibuffer-overlay
'end-open nil
)))
802 (defun vip-check-minibuffer-overlay ()
803 (or (vip-overlay-p vip-minibuffer-overlay
)
804 (setq vip-minibuffer-overlay
806 (vip-make-overlay 1 (1+ (buffer-size)) (current-buffer))
807 ;; make overlay open-ended
809 1 (1+ (buffer-size)) (current-buffer) nil
'rear-advance
)))
813 (defsubst vip-is-in-minibuffer
()
814 (string-match "\*Minibuf-" (buffer-name)))
818 ;;; XEmacs compatibility
820 (defun vip-abbreviate-file-name (file)
822 (abbreviate-file-name file
)
823 ;; XEmacs requires addl argument
824 (abbreviate-file-name file t
)))
826 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
827 ;; in sit-for, so this function smoothes out the differences.
828 (defsubst vip-sit-for-short
(val &optional nodisp
)
830 (sit-for (/ val
1000.0) nodisp
)
831 (sit-for 0 val nodisp
)))
833 ;; EVENT may be a single event of a sequence of events
834 (defsubst vip-ESC-event-p
(event)
835 (let ((ESC-keys '(?\e
(control \
[) escape
))
836 (key (vip-event-key event
)))
837 (member key ESC-keys
)))
840 (defsubst vip-mark-marker
()
845 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
846 ;; is the same as (mark t).
847 (defsubst vip-set-mark-if-necessary
()
848 (setq mark-ring
(delete (vip-mark-marker) mark-ring
))
849 (set-mark-command nil
))
851 ;; In transient mark mode (zmacs mode), it is annoying when regions become
852 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
853 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
854 (defun vip-deactivate-mark ()
856 (zmacs-deactivate-region)
859 (defsubst vip-leave-region-active
()
861 (setq zmacs-region-stays t
)))
864 (defsubst vip-events-to-keys
(events)
865 (cond (vip-xemacs-p (events-to-keys events
))
869 (defun vip-eval-after-load (file form
)
871 (eval-after-load file form
)
872 (or (assoc file after-load-alist
)
873 (setq after-load-alist
(cons (list file
) after-load-alist
)))
874 (let ((elt (assoc file after-load-alist
)))
875 (or (member form
(cdr elt
))
876 (setq elt
(nconc elt
(list form
)))))
880 ;; This is here because Emacs changed the way local hooks work.
882 ;;Add to the value of HOOK the function FUNCTION.
883 ;;FUNCTION is not added if already present.
884 ;;FUNCTION is added (if necessary) at the beginning of the hook list
885 ;;unless the optional argument APPEND is non-nil, in which case
886 ;;FUNCTION is added at the end.
888 ;;HOOK should be a symbol, and FUNCTION may be any valid function. If
889 ;;HOOK is void, it is first set to nil. If HOOK's value is a single
890 ;;function, it is changed to a list of functions."
891 (defun vip-add-hook (hook function
&optional append
)
892 (if (not (boundp hook
)) (set hook nil
))
893 ;; If the hook value is a single function, turn it into a list.
894 (let ((old (symbol-value hook
)))
895 (if (or (not (listp old
)) (eq (car old
) 'lambda
))
896 (setq old
(list old
)))
897 (if (member function old
)
900 (append old
(list function
)) ; don't nconc
901 (cons function old
))))))
903 ;; This is here because of Emacs's changes in the semantics of add/remove-hooks
904 ;; and due to the bugs they introduced.
906 ;; Remove from the value of HOOK the function FUNCTION.
907 ;; HOOK should be a symbol, and FUNCTION may be any valid function. If
908 ;; FUNCTION isn't the value of HOOK, or, if FUNCTION doesn't appear in the
909 ;; list of hooks to run in HOOK, then nothing is done. See `vip-add-hook'."
910 (defun vip-remove-hook (hook function
)
911 (if (or (not (boundp hook
)) ;unbound symbol, or
912 (null (symbol-value hook
)) ;value is nil, or
913 (null function
)) ;function is nil, then
915 (let ((hook-value (symbol-value hook
)))
916 (if (consp hook-value
)
917 ;; don't side-effect the list
918 (setq hook-value
(delete function
(copy-sequence hook-value
)))
919 (if (equal hook-value function
)
920 (setq hook-value nil
)))
921 (set hook hook-value
))))
925 ;; like read-event, but in XEmacs also try to convert to char, if possible
926 (defun vip-read-event-convert-to-char ()
930 (setq event
(next-command-event))
931 (or (event-to-character event
)
935 ;; This function lets function-key-map convert key sequences into logical
936 ;; keys. This does a better job than vip-read-event when it comes to kbd
937 ;; macros, since it enables certain macros to be shared between X and TTY
939 (defun vip-read-key ()
940 (let ((overriding-local-map vip-overriding-map
)
942 (use-global-map vip-overriding-map
)
943 (setq key
(elt (read-key-sequence nil
) 0))
944 (use-global-map global-map
)
948 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
949 ;; instead of nil, if '(nil) was previously inadvertently assigned to
950 ;; unread-command-events
951 (defun vip-event-key (event)
952 (or (and event
(eventp event
))
953 (error "vip-event-key: Wrong type argument, eventp, %S" event
))
954 (let ((mod (event-modifiers event
))
959 (cond ((key-press-event-p event
)
961 ((button-event-p event
)
962 (concat "mouse-" (prin1-to-string (event-button event
))))
964 (error "vip-event-key: Unknown event, %S" event
))))
966 ;; Emacs doesn't handle capital letters correctly, since
967 ;; \S-a isn't considered the same as A (it behaves as
968 ;; plain `a' instead). So we take care of this here
969 (cond ((and (vip-characterp event
) (<= ?A event
) (<= event ?Z
))
972 ;; Emacs has the oddity whereby characters 128+char
973 ;; represent M-char *if* this appears inside a string.
974 ;; So, we convert them manually to (meta char).
975 ((and (vip-characterp event
) (< ?\C-? event
) (<= event
255))
977 event
(- event ?\C-?
1)))
978 (t (event-basic-type event
)))
980 (if (vip-characterp basis
)
983 (list 'control
'\?) ; taking care of an emacs bug
984 (intern (char-to-string basis
)))))
986 (append mod
(list basis
))
989 (defun vip-key-to-emacs-key (key)
990 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
991 (cond (vip-xemacs-p key
)
993 (setq key-name
(symbol-name key
))
994 (if (= (length key-name
) 1) ; character event
995 (string-to-char key-name
)
998 (setq modifiers
(subseq key
0 (1- (length key
)))
999 base-key
(vip-seq-last-elt key
)
1000 base-key-name
(symbol-name base-key
)
1001 char-p
(= (length base-key-name
) 1))
1004 '(lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1008 (car (read-from-string
1011 (mapconcat 'identity mod-char-list
"-\\")
1017 (mapconcat 'identity mod-char-list
"-")
1023 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1024 ;; convert events to keys and, if all keys are regular printable
1025 ;; characters, will return a string. Otherwise, will return a string
1026 ;; representing a vector of converted events. If the input was a Viper macro,
1027 ;; will return a string that represents this macro as a vector.
1028 (defun vip-array-to-string (event-seq)
1030 (cond ((stringp event-seq
) event-seq
)
1031 ((vip-event-vector-p event-seq
)
1032 (setq temp
(mapcar 'vip-event-key event-seq
))
1033 (cond ((vip-char-symbol-sequence-p temp
)
1034 (mapconcat 'symbol-name temp
""))
1035 ((and (vip-char-array-p
1036 (setq temp2
(mapcar 'vip-key-to-character temp
))))
1037 (mapconcat 'char-to-string temp2
""))
1038 (t (prin1-to-string (vconcat temp
)))))
1039 ((vip-char-symbol-sequence-p event-seq
)
1040 (mapconcat 'symbol-name event-seq
""))
1041 ((and (vectorp event-seq
)
1043 (setq temp
(mapcar 'vip-key-to-character event-seq
))))
1044 (mapconcat 'char-to-string temp
""))
1045 (t (prin1-to-string event-seq
)))))
1047 (defun vip-key-press-events-to-chars (events)
1048 (mapconcat (if vip-emacs-p
1051 (lambda (elt) (char-to-string (event-to-character elt
)))))
1056 (defsubst vip-fast-keysequence-p
()
1057 (not (vip-sit-for-short vip-fast-keyseq-timeout t
)))
1059 (defun vip-read-char-exclusive ()
1061 (echo-keystrokes 1))
1064 (setq char
(read-char))
1066 ;; skip event if not char
1070 ;; key is supposed to be in viper's representation, e.g., (control l), a
1072 (defun vip-key-to-character (key)
1073 (cond ((eq key
'space
) ?\
)
1074 ((eq key
'delete
) ?\C-?
)
1075 ((eq key
'backspace
) ?\C-h
)
1077 (= 1 (length (symbol-name key
))))
1078 (string-to-char (symbol-name key
)))
1080 (eq (car key
) 'control
)
1081 (symbol-name (nth 1 key
))
1082 (= 1 (length (symbol-name (nth 1 key
)))))
1083 (read (format "?\\C-%s" (symbol-name (nth 1 key
)))))
1087 (defun vip-setup-master-buffer (&rest other-files-or-buffers
)
1088 "Set up the current buffer as a master buffer.
1089 Arguments become related buffers. This function should normally be used in
1090 the `Local variables' section of a file."
1091 (setq vip-related-files-and-buffers-ring
1092 (make-ring (1+ (length other-files-or-buffers
))))
1093 (mapcar '(lambda (elt)
1094 (vip-ring-insert vip-related-files-and-buffers-ring elt
))
1095 other-files-or-buffers
)
1096 (vip-ring-insert vip-related-files-and-buffers-ring
(buffer-name))
1099 ;;; Movement utilities
1101 (defvar vip-syntax-preference
'strict-vi
1102 "*Syntax type characterizing Viper's alphanumeric symbols.
1103 `emacs' means only word constituents are considered to be alphanumeric.
1104 Word constituents are symbols specified as word constituents by the current
1106 `extended' means word and symbol constituents.
1107 `reformed-vi' means Vi-ish behavior: word constituents and the symbol `_'.
1108 However, word constituents are determined according to Emacs syntax tables,
1109 which may be different from Vi in some major modes.
1110 `strict-vi' means Viper words are exactly as in Vi.")
1112 (vip-deflocalvar vip-ALPHA-char-class
"w"
1113 "String of syntax classes characterizing Viper's alphanumeric symbols.
1114 In addition, the symbol `_' may be considered alphanumeric if
1115 `vip-syntax-preference'is `reformed-vi'.")
1117 (vip-deflocalvar vip-strict-ALPHA-chars
"a-zA-Z0-9_"
1118 "Regexp matching the set of alphanumeric characters acceptable to strict
1120 (vip-deflocalvar vip-strict-SEP-chars
" \t\n"
1121 "Regexp matching the set of alphanumeric characters acceptable to strict
1124 (vip-deflocalvar vip-SEP-char-class
" -"
1125 "String of syntax classes for Vi separators.
1126 Usually contains ` ', linefeed, TAB or formfeed.")
1128 (defun vip-update-alphanumeric-class ()
1129 "Set the syntactic class of Viper alphanumeric symbols according to
1130 the variable `vip-ALPHA-char-class'. Should be called in order for changes to
1131 `vip-ALPHA-char-class' to take effect."
1134 vip-ALPHA-char-class
1135 (cond ((eq vip-syntax-preference
'emacs
) "w") ; only word constituents
1136 ((eq vip-syntax-preference
'extended
) "w_") ; word & symbol chars
1137 (t "w")))) ; vi syntax: word constituents and the symbol `_'
1139 ;; addl-chars are characters to be temporarily considered as alphanumerical
1140 (defun vip-looking-at-alpha (&optional addl-chars
)
1141 (or (stringp addl-chars
) (setq addl-chars
""))
1142 (if (eq vip-syntax-preference
'reformed-vi
)
1143 (setq addl-chars
(concat addl-chars
"_")))
1144 (let ((char (char-after (point))))
1146 (if (eq vip-syntax-preference
'strict-vi
)
1147 (looking-at (concat "[" vip-strict-ALPHA-chars addl-chars
"]"))
1149 ;; convert string to list
1150 (append (vconcat addl-chars
) nil
))
1151 (memq (char-syntax char
)
1152 (append (vconcat vip-ALPHA-char-class
) nil
)))))
1155 (defsubst vip-looking-at-separator
()
1156 (let ((char (char-after (point))))
1158 (or (eq char ?
\n) ; RET is always a separator in Vi
1159 (memq (char-syntax char
)
1160 (append (vconcat vip-SEP-char-class
) nil
))))))
1162 (defsubst vip-looking-at-alphasep
(&optional addl-chars
)
1163 (or (vip-looking-at-separator) (vip-looking-at-alpha addl-chars
)))
1165 (defsubst vip-skip-alpha-forward
(&optional addl-chars
)
1166 (or (stringp addl-chars
) (setq addl-chars
""))
1169 (cond ((eq vip-syntax-preference
'strict-vi
)
1171 (t vip-ALPHA-char-class
))
1172 (cond ((eq vip-syntax-preference
'strict-vi
)
1173 (concat vip-strict-ALPHA-chars addl-chars
))
1176 (defsubst vip-skip-alpha-backward
(&optional addl-chars
)
1177 (or (stringp addl-chars
) (setq addl-chars
""))
1180 (cond ((eq vip-syntax-preference
'strict-vi
)
1182 (t vip-ALPHA-char-class
))
1183 (cond ((eq vip-syntax-preference
'strict-vi
)
1184 (concat vip-strict-ALPHA-chars addl-chars
))
1187 ;; weird syntax tables may confuse strict-vi style
1188 (defsubst vip-skip-all-separators-forward
(&optional within-line
)
1189 (vip-skip-syntax 'forward
1191 (or within-line
"\n")
1192 (if within-line
(vip-line-pos 'end
))))
1193 (defsubst vip-skip-all-separators-backward
(&optional within-line
)
1194 (vip-skip-syntax 'backward
1196 (or within-line
"\n")
1197 (if within-line
(vip-line-pos 'start
))))
1198 (defun vip-skip-nonseparators (direction)
1199 (let ((func (intern (format "skip-syntax-%S" direction
))))
1200 (funcall func
(concat "^" vip-SEP-char-class
)
1201 (vip-line-pos (if (eq direction
'forward
) 'end
'start
)))))
1203 (defsubst vip-skip-nonalphasep-forward
()
1204 (if (eq vip-syntax-preference
'strict-vi
)
1206 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars
))
1207 (skip-syntax-forward
1209 "^" vip-ALPHA-char-class vip-SEP-char-class
) (vip-line-pos 'end
))))
1210 (defsubst vip-skip-nonalphasep-backward
()
1211 (if (eq vip-syntax-preference
'strict-vi
)
1212 (skip-chars-backward
1213 (concat "^" vip-strict-SEP-chars vip-strict-ALPHA-chars
))
1214 (skip-syntax-backward
1216 "^" vip-ALPHA-char-class vip-SEP-char-class
) (vip-line-pos 'start
))))
1218 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1219 ;; Return the number of chars traveled.
1220 ;; Either SYNTAX or ADDL-CHARS can be nil, in which case they are interpreted
1221 ;; as an empty string.
1222 (defun vip-skip-syntax (direction syntax addl-chars
&optional limit
)
1225 (skip-chars-func (intern (format "skip-chars-%S" direction
)))
1226 (skip-syntax-func (intern (format "skip-syntax-%S" direction
))))
1227 (or (stringp addl-chars
) (setq addl-chars
""))
1228 (or (stringp syntax
) (setq syntax
""))
1229 (while (and (not (= local
0)) (not (eobp)))
1231 (+ (funcall skip-syntax-func syntax limit
)
1232 (funcall skip-chars-func addl-chars limit
)))
1233 (setq total
(+ total local
)))
1240 (provide 'viper-util
)
1242 ;;; viper-util.el ends here