Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / emulation / viper-util.el
blob1ad24da1ef89ceb98702ab0bc4fbad476b1ba826
1 ;;; viper-util.el --- Utilities used by viper.el
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7 ;; Package: viper
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
28 (provide 'viper-util)
31 ;; Compiler pacifier
32 (defvar viper-overriding-map)
33 (defvar pm-color-alist)
34 (defvar viper-minibuffer-current-face)
35 (defvar viper-minibuffer-insert-face)
36 (defvar viper-minibuffer-vi-face)
37 (defvar viper-minibuffer-emacs-face)
38 (defvar viper-replace-overlay-face)
39 (defvar viper-fast-keyseq-timeout)
40 (defvar ex-unix-type-shell)
41 (defvar ex-unix-type-shell-options)
42 (defvar viper-ex-tmp-buf-name)
43 (defvar viper-syntax-preference)
44 (defvar viper-saved-mark)
46 (require 'ring)
48 (eval-and-compile
49 (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
51 ;; end pacifier
53 (require 'viper-init)
57 (defalias 'viper-overlay-p
58 (if (featurep 'xemacs) 'extentp 'overlayp))
59 (defalias 'viper-make-overlay
60 (if (featurep 'xemacs) 'make-extent 'make-overlay))
61 (defalias 'viper-overlay-live-p
62 (if (featurep 'xemacs) 'extent-live-p 'overlayp))
63 (defalias 'viper-move-overlay
64 (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay))
65 (defalias 'viper-overlay-start
66 (if (featurep 'xemacs) 'extent-start-position 'overlay-start))
67 (defalias 'viper-overlay-end
68 (if (featurep 'xemacs) 'extent-end-position 'overlay-end))
69 (defalias 'viper-overlay-get
70 (if (featurep 'xemacs) 'extent-property 'overlay-get))
71 (defalias 'viper-overlay-put
72 (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
73 (defalias 'viper-read-event
74 (if (featurep 'xemacs) 'next-command-event 'read-event))
75 (defalias 'viper-characterp
76 (if (featurep 'xemacs) 'characterp 'integerp))
77 (defalias 'viper-int-to-char
78 (if (featurep 'xemacs) 'int-to-char 'identity))
79 (defalias 'viper-get-face
80 (if (featurep 'xemacs) 'get-face 'internal-get-face))
81 (defalias 'viper-color-defined-p
82 (if (featurep 'xemacs) 'valid-color-name-p 'x-color-defined-p))
83 (defalias 'viper-iconify
84 (if (featurep 'xemacs) 'iconify-frame 'iconify-or-deiconify-frame))
87 ;; CHAR is supposed to be a char or an integer (positive or negative)
88 ;; LIST is a list of chars, nil, and negative numbers
89 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
90 ;; Introduced for compatibility with XEmacs, where integers are not the same as
91 ;; chars.
92 (defun viper-memq-char (char list)
93 (cond ((and (integerp char) (>= char 0))
94 (memq (viper-int-to-char char) list))
95 ((memq char list))))
97 ;; Check if char-or-int and char are the same as characters
98 (defun viper-char-equal (char-or-int char)
99 (cond ((and (integerp char-or-int) (>= char-or-int 0))
100 (= (viper-int-to-char char-or-int) char))
101 ((eq char-or-int char))))
103 ;; Like =, but accommodates null and also is t for eq-objects
104 (defun viper= (char char1)
105 (cond ((eq char char1) t)
106 ((and (viper-characterp char) (viper-characterp char1))
107 (= char char1))
108 (t nil)))
110 (defsubst viper-color-display-p ()
111 (if (featurep 'xemacs) (eq (device-class (selected-device)) 'color)
112 (x-display-color-p)))
114 (defun viper-get-cursor-color (&optional frame)
115 (if (featurep 'xemacs)
116 (color-instance-name
117 (frame-property (or frame (selected-frame)) 'cursor-color))
118 (cdr (assoc 'cursor-color (frame-parameters)))))
120 (defmacro viper-frame-value (variable)
121 "Return the value of VARIABLE local to the current frame, if there is one.
122 Otherwise return the normal value."
123 `(if (featurep 'xemacs)
124 ,variable
125 ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
126 ;; so we do it by hand instead.
127 ;; Buffer-local values take precedence over frame-local ones.
128 (if (local-variable-p ',variable)
129 ,variable
130 ;; Distinguish between no frame parameter and a frame parameter
131 ;; with a value of nil.
132 (let ((fp (assoc ',variable (frame-parameters))))
133 (if fp (cdr fp)
134 ,variable)))))
136 ;; OS/2
137 (cond ((eq (viper-device-type) 'pm)
138 (fset 'viper-color-defined-p
139 (lambda (color) (assoc color pm-color-alist)))))
142 ;; cursor colors
143 (defun viper-change-cursor-color (new-color &optional frame)
144 (if (and (viper-window-display-p) (viper-color-display-p)
145 (stringp new-color) (viper-color-defined-p new-color)
146 (not (string= new-color (viper-get-cursor-color))))
147 (if (featurep 'xemacs)
148 (set-frame-property
149 (or frame (selected-frame))
150 'cursor-color (make-color-instance new-color))
151 (modify-frame-parameters
152 (or frame (selected-frame))
153 (list (cons 'cursor-color new-color))))))
155 ;; Note that the colors this function uses might not be those
156 ;; associated with FRAME, if there are frame-local values.
157 ;; This was equally true before the advent of viper-frame-value.
158 ;; Now it could be changed by passing frame to v-f-v.
159 (defun viper-set-cursor-color-according-to-state (&optional frame)
160 (cond ((eq viper-current-state 'replace-state)
161 (viper-change-cursor-color
162 (viper-frame-value viper-replace-overlay-cursor-color)
163 frame))
164 ((and (eq viper-current-state 'emacs-state)
165 (viper-frame-value viper-emacs-state-cursor-color))
166 (viper-change-cursor-color
167 (viper-frame-value viper-emacs-state-cursor-color)
168 frame))
169 ((eq viper-current-state 'insert-state)
170 (viper-change-cursor-color
171 (viper-frame-value viper-insert-state-cursor-color)
172 frame))
174 (viper-change-cursor-color
175 (viper-frame-value viper-vi-state-cursor-color)
176 frame))))
178 ;; By default, saves current frame cursor color before changing viper state
179 (defun viper-save-cursor-color (before-which-mode)
180 (if (and (viper-window-display-p) (viper-color-display-p))
181 (let ((color (viper-get-cursor-color)))
182 (if (and (stringp color) (viper-color-defined-p color)
183 ;; there is something fishy in that the color is not saved if
184 ;; it is the same as frames default cursor color. need to be
185 ;; checked.
186 (not (string= color
187 (viper-frame-value
188 viper-replace-overlay-cursor-color))))
189 (modify-frame-parameters
190 (selected-frame)
191 (list
192 (cons
193 (cond ((eq before-which-mode 'before-replace-mode)
194 'viper-saved-cursor-color-in-replace-mode)
195 ((eq before-which-mode 'before-emacs-mode)
196 'viper-saved-cursor-color-in-emacs-mode)
198 'viper-saved-cursor-color-in-insert-mode))
199 color)))))))
202 (defun viper-get-saved-cursor-color-in-replace-mode ()
204 (funcall
205 (if (featurep 'emacs) 'frame-parameter 'frame-property)
206 (selected-frame)
207 'viper-saved-cursor-color-in-replace-mode)
208 (or (and (eq viper-current-state 'emacs-mode)
209 (viper-frame-value viper-emacs-state-cursor-color))
210 (viper-frame-value viper-vi-state-cursor-color))))
212 (defun viper-get-saved-cursor-color-in-insert-mode ()
214 (funcall
215 (if (featurep 'emacs) 'frame-parameter 'frame-property)
216 (selected-frame)
217 'viper-saved-cursor-color-in-insert-mode)
218 (or (and (eq viper-current-state 'emacs-mode)
219 (viper-frame-value viper-emacs-state-cursor-color))
220 (viper-frame-value viper-vi-state-cursor-color))))
222 (defun viper-get-saved-cursor-color-in-emacs-mode ()
224 (funcall
225 (if (featurep 'emacs) 'frame-parameter 'frame-property)
226 (selected-frame)
227 'viper-saved-cursor-color-in-emacs-mode)
228 (viper-frame-value viper-vi-state-cursor-color)))
230 ;; restore cursor color from replace overlay
231 (defun viper-restore-cursor-color(after-which-mode)
232 (if (viper-overlay-p viper-replace-overlay)
233 (viper-change-cursor-color
234 (cond ((eq after-which-mode 'after-replace-mode)
235 (viper-get-saved-cursor-color-in-replace-mode))
236 ((eq after-which-mode 'after-emacs-mode)
237 (viper-get-saved-cursor-color-in-emacs-mode))
238 (t (viper-get-saved-cursor-color-in-insert-mode)))
242 ;; Check the current version against the major and minor version numbers
243 ;; using op: cur-vers op major.minor If emacs-major-version or
244 ;; emacs-minor-version are not defined, we assume that the current version
245 ;; is hopelessly outdated. We assume that emacs-major-version and
246 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
247 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
248 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
249 ;; incorrect. However, this gives correct result in our cases, since we are
250 ;; testing for sufficiently high Emacs versions.
251 (defun viper-check-version (op major minor &optional type-of-emacs)
252 (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
253 (and (cond ((eq type-of-emacs 'xemacs) (featurep 'xemacs))
254 ((eq type-of-emacs 'emacs) (featurep 'emacs))
255 (t t))
256 (cond ((eq op '=) (and (= emacs-minor-version minor)
257 (= emacs-major-version major)))
258 ((memq op '(> >= < <=))
259 (and (or (funcall op emacs-major-version major)
260 (= emacs-major-version major))
261 (if (= emacs-major-version major)
262 (funcall op emacs-minor-version minor)
263 t)))
265 (error "%S: Invalid op in viper-check-version" op))))
266 (cond ((memq op '(= > >=)) nil)
267 ((memq op '(< <=)) t))))
270 (defun viper-get-visible-buffer-window (wind)
271 (if (featurep 'xemacs)
272 (get-buffer-window wind t)
273 (get-buffer-window wind 'visible)))
276 ;; Return line position.
277 ;; If pos is 'start then returns position of line start.
278 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
279 ;; Pos = 'indent returns beginning of indentation.
280 ;; Otherwise, returns point. Current point is not moved in any case."
281 (defun viper-line-pos (pos)
282 (let ((cur-pos (point))
283 (result))
284 (cond
285 ((equal pos 'start)
286 (beginning-of-line))
287 ((equal pos 'end)
288 (end-of-line))
289 ((equal pos 'mid)
290 (goto-char (+ (viper-line-pos 'start) (viper-line-pos 'end) 2)))
291 ((equal pos 'indent)
292 (back-to-indentation))
293 (t nil))
294 (setq result (point))
295 (goto-char cur-pos)
296 result))
298 ;; Emacs used to count each multibyte character as several positions in the buffer,
299 ;; so we had to use Emacs' chars-in-region to count characters. Since 20.3,
300 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
301 ;; counting each char as just one pos. So, now we can simply subtract beg from
302 ;; end to determine the number of characters in a region.
303 (defun viper-chars-in-region (beg end &optional preserve-sign)
304 ;;(let ((count (abs (if (fboundp 'chars-in-region)
305 ;; (chars-in-region beg end)
306 ;; (- end beg)))))
307 (let ((count (abs (- end beg))))
308 (if (and (< end beg) preserve-sign)
309 (- count)
310 count)))
312 ;; Test if POS is between BEG and END
313 (defsubst viper-pos-within-region (pos beg end)
314 (and (>= pos (min beg end)) (>= (max beg end) pos)))
317 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
318 ;; The first argument must eval to a variable name.
319 ;; Arguments: (var-name position &optional buffer).
321 ;; This is useful for moving markers that are supposed to be local.
322 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
323 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
324 ;; buffer, a new marker will be created.
325 (defun viper-move-marker-locally (var pos &optional buffer)
326 (if (markerp (eval var))
328 (set var (make-marker)))
329 (move-marker (eval var) pos buffer))
332 ;; Print CONDITIONS as a message.
333 (defun viper-message-conditions (conditions)
334 (let ((case (car conditions)) (msg (cdr conditions)))
335 (if (null msg)
336 (message "%s" case)
337 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
338 (beep 1)))
342 ;;; List/alist utilities
344 ;; Convert LIST to an alist
345 (defun viper-list-to-alist (lst)
346 (let ((alist))
347 (while lst
348 (setq alist (cons (list (car lst)) alist))
349 (setq lst (cdr lst)))
350 alist))
352 ;; Convert ALIST to a list.
353 (defun viper-alist-to-list (alst)
354 (let ((lst))
355 (while alst
356 (setq lst (cons (car (car alst)) lst))
357 (setq alst (cdr alst)))
358 lst))
360 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
361 (defun viper-filter-alist (regexp alst)
362 (interactive "s x")
363 (let ((outalst) (inalst alst))
364 (while (car inalst)
365 (if (string-match regexp (car (car inalst)))
366 (setq outalst (cons (car inalst) outalst)))
367 (setq inalst (cdr inalst)))
368 outalst))
370 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
371 (defun viper-filter-list (regexp lst)
372 (interactive "s x")
373 (let ((outlst) (inlst lst))
374 (while (car inlst)
375 (if (string-match regexp (car inlst))
376 (setq outlst (cons (car inlst) outlst)))
377 (setq inlst (cdr inlst)))
378 outlst))
381 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
382 ;; LIS2 is modified by filtering it: deleting its members of the form
383 ;; \(car elt\) such that (car elt') is in LIS1.
384 (defun viper-append-filter-alist (lis1 lis2)
385 (let ((temp lis1)
386 elt)
387 ;;filter-append the second list
388 (while temp
389 ;; delete all occurrences
390 (while (setq elt (assoc (car (car temp)) lis2))
391 (setq lis2 (delq elt lis2)))
392 (setq temp (cdr temp)))
394 (append lis1 lis2)))
398 (declare-function viper-forward-Word "viper-cmd" (arg))
400 ;;; Support for :e, :r, :w file globbing
402 ;; Glob the file spec.
403 ;; This function is designed to work under Unix.
404 (defun viper-glob-unix-files (filespec)
405 (let ((gshell
406 (cond (ex-unix-type-shell shell-file-name)
407 (t "sh"))) ; probably Unix anyway
408 (gshell-options
409 ;; using cond in anticipation of further additions
410 (cond (ex-unix-type-shell-options)
412 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec))
413 (t (format "ls -1 -d %s" filespec))))
414 status)
415 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
416 (erase-buffer)
417 (setq status
418 (if gshell-options
419 (call-process gshell nil t nil
420 gshell-options
421 "-c"
422 command)
423 (call-process gshell nil t nil
424 "-c"
425 command)))
426 (goto-char (point-min))
427 ;; Issue an error, if no match.
428 (unless (eq 0 status)
429 (save-excursion
430 (skip-chars-forward " \t\n\j")
431 (if (looking-at "ls:")
432 (viper-forward-Word 1))
433 (error "%s: %s"
434 (if (stringp gshell)
435 gshell
436 "shell")
437 (buffer-substring (point) (viper-line-pos 'end)))
439 (goto-char (point-min))
440 (viper-get-filenames-from-buffer 'one-per-line))
444 ;; Interpret the stuff in the buffer as a list of file names
445 ;; return a list of file names listed in the buffer beginning at point
446 ;; If optional arg is supplied, assume each filename is listed on a separate
447 ;; line
448 (defun viper-get-filenames-from-buffer (&optional one-per-line)
449 (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
450 result fname delim)
451 (skip-chars-forward skip-chars)
452 (while (not (eobp))
453 (if (cond ((looking-at "\"")
454 (setq delim ?\")
455 (re-search-forward "[^\"]+" nil t)) ; noerror
456 ((looking-at "'")
457 (setq delim ?')
458 (re-search-forward "[^']+" nil t)) ; noerror
460 (re-search-forward
461 (concat "[^" skip-chars "]+") nil t))) ;noerror
462 (setq fname
463 (buffer-substring (match-beginning 0) (match-end 0))))
464 (if delim
465 (forward-char 1))
466 (skip-chars-forward " \t\n")
467 (setq result (cons fname result)))
468 result))
470 ;; convert MS-DOS wildcards to regexp
471 (defun viper-wildcard-to-regexp (wcard)
472 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
473 (erase-buffer)
474 (insert wcard)
475 (goto-char (point-min))
476 (while (not (eobp))
477 (skip-chars-forward "^*?.\\\\")
478 (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
479 ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
480 ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
481 ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
483 (buffer-string)
487 ;; glob windows files
488 ;; LIST is expected to be in reverse order
489 (defun viper-glob-mswindows-files (filespec)
490 (let ((case-fold-search t)
491 tmp tmp2)
492 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
493 (erase-buffer)
494 (insert filespec)
495 (goto-char (point-min))
496 (setq tmp (viper-get-filenames-from-buffer))
497 (while tmp
498 (setq tmp2 (cons (directory-files
499 ;; the directory part
500 (or (file-name-directory (car tmp))
502 t ; return full names
503 ;; the regexp part: globs the file names
504 (concat "^"
505 (viper-wildcard-to-regexp
506 (file-name-nondirectory (car tmp)))
507 "$"))
508 tmp2))
509 (setq tmp (cdr tmp)))
510 (reverse (apply 'append tmp2)))))
513 ;;; Insertion ring
515 ;; Rotate RING's index. DIRection can be positive or negative.
516 (defun viper-ring-rotate1 (ring dir)
517 (if (and (ring-p ring) (> (ring-length ring) 0))
518 (progn
519 (setcar ring (cond ((> dir 0)
520 (ring-plus1 (car ring) (ring-length ring)))
521 ((< dir 0)
522 (ring-minus1 (car ring) (ring-length ring)))
523 ;; don't rotate if dir = 0
524 (t (car ring))))
525 (viper-current-ring-item ring)
528 (defun viper-special-ring-rotate1 (ring dir)
529 (if (memq viper-intermediate-command
530 '(repeating-display-destructive-command
531 repeating-insertion-from-ring))
532 (viper-ring-rotate1 ring dir)
533 ;; don't rotate otherwise
534 (viper-ring-rotate1 ring 0)))
536 ;; current ring item; if N is given, then so many items back from the
537 ;; current
538 (defun viper-current-ring-item (ring &optional n)
539 (setq n (or n 0))
540 (if (and (ring-p ring) (> (ring-length ring) 0))
541 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
543 ;; Push item onto ring. The second argument is a ring-variable, not value.
544 (defun viper-push-onto-ring (item ring-var)
545 (or (ring-p (eval ring-var))
546 (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
547 (or (null item) ; don't push nil
548 (and (stringp item) (string= item "")) ; or empty strings
549 (equal item (viper-current-ring-item (eval ring-var))) ; or old stuff
550 ;; Since viper-set-destructive-command checks if we are inside
551 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
552 ;; cmd viper-repeat makes a call to the current function only if `.' is
553 ;; executing a command from the command history. It doesn't call the
554 ;; push-onto-ring function if `.' is simply repeating the last
555 ;; destructive command. We only check for ESC (which happens when we do
556 ;; insert with a prefix argument, or if this-command-keys doesn't give
557 ;; anything meaningful (in that case we don't know what to show to the
558 ;; user).
559 (and (eq ring-var 'viper-command-ring)
560 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
561 (viper-array-to-string (this-command-keys))))
562 (viper-ring-insert (eval ring-var) item))
566 ;; removing elts from ring seems to break it
567 (defun viper-cleanup-ring (ring)
568 (or (< (ring-length ring) 2)
569 (null (viper-current-ring-item ring))
570 ;; last and previous equal
571 (if (equal (viper-current-ring-item ring)
572 (viper-current-ring-item ring 1))
573 (viper-ring-pop ring))))
575 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
576 (defun viper-ring-pop (ring)
577 (let* ((ln (ring-length ring))
578 (vec (cdr (cdr ring)))
579 (veclen (length vec))
580 (hd (car ring))
581 (idx (max 0 (ring-minus1 hd ln)))
582 (top-elt (aref vec idx)))
584 ;; shift elements
585 (while (< (1+ idx) veclen)
586 (aset vec idx (aref vec (1+ idx)))
587 (setq idx (1+ idx)))
588 (aset vec idx nil)
590 (setq hd (max 0 (ring-minus1 hd ln)))
591 (if (= hd (1- ln)) (setq hd 0))
592 (setcar ring hd) ; move head
593 (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
594 top-elt
597 (defun viper-ring-insert (ring item)
598 (let* ((ln (ring-length ring))
599 (vec (cdr (cdr ring)))
600 (veclen (length vec))
601 (hd (car ring))
602 (vecpos-after-hd (if (= hd 0) ln hd))
603 (idx ln))
605 (if (= ln veclen)
606 (progn
607 (aset vec hd item) ; hd is always 1+ the actual head index in vec
608 (setcar ring (ring-plus1 hd ln)))
609 (setcar (cdr ring) (1+ ln))
610 (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
611 (while (and (>= idx vecpos-after-hd) (> ln 0))
612 (aset vec idx (aref vec (1- idx)))
613 (setq idx (1- idx)))
614 (aset vec vecpos-after-hd item))
615 item))
618 ;;; String utilities
620 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
621 ;; PRE-STRING is a string to prepend to the abbrev string.
622 ;; POST-STRING is a string to append to the abbrev string.
623 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
624 ;; if the orig string was truncated.
625 (defun viper-abbreviate-string (string max-len
626 pre-string post-string abbrev-sign)
627 (let (truncated-str)
628 (setq truncated-str
629 (if (stringp string)
630 (substring string 0 (min max-len (length string)))))
631 (cond ((null truncated-str) "")
632 ((> (length string) max-len)
633 (format "%s%s%s%s"
634 pre-string truncated-str abbrev-sign post-string))
635 (t (format "%s%s%s" pre-string truncated-str post-string)))))
637 ;; tells if we are over a whitespace-only line
638 (defsubst viper-over-whitespace-line ()
639 (save-excursion
640 (beginning-of-line)
641 (looking-at "^[ \t]*$")))
644 ;;; Saving settings in custom file
646 ;; Save the current setting of VAR in CUSTOM-FILE.
647 ;; If given, MESSAGE is a message to be displayed after that.
648 ;; This message is erased after 2 secs, if erase-msg is non-nil.
649 ;; Arguments: var message custom-file &optional erase-message
650 (defun viper-save-setting (var message custom-file &optional erase-msg)
651 (let* ((var-name (symbol-name var))
652 (var-val (if (boundp var) (eval var)))
653 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
654 (buf (find-file-noselect (substitute-in-file-name custom-file)))
656 (message "%s" (or message ""))
657 (with-current-buffer buf
658 (goto-char (point-min))
659 (if (re-search-forward regexp nil t)
660 (let ((reg-end (1- (match-end 0))))
661 (search-backward var-name)
662 (delete-region (match-beginning 0) reg-end)
663 (goto-char (match-beginning 0))
664 (insert (format "%s '%S" var-name var-val)))
665 (goto-char (point-max))
666 (if (not (bolp)) (insert "\n"))
667 (insert (format "(setq %s '%S)\n" var-name var-val)))
668 (save-buffer))
669 (kill-buffer buf)
670 (if erase-msg
671 (progn
672 (sit-for 2)
673 (message "")))
676 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
677 ;; match this pattern.
678 (defun viper-save-string-in-file (string custom-file &optional pattern)
679 (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
680 (with-current-buffer buf
681 (let (buffer-read-only)
682 (goto-char (point-min))
683 (if pattern (delete-matching-lines pattern))
684 (goto-char (point-max))
685 (if string (insert string))
686 (save-buffer)))
687 (kill-buffer buf)
691 ;; This is a simple-minded check for whether a file is under version control.
692 ;; If file,v exists but file doesn't, this file is considered to be not checked
693 ;; in and not checked out for the purpose of patching (since patch won't be
694 ;; able to read such a file anyway).
695 ;; FILE is a string representing file name
696 ;;(defun viper-file-under-version-control (file)
697 ;; (let* ((filedir (file-name-directory file))
698 ;; (file-nondir (file-name-nondirectory file))
699 ;; (trial (concat file-nondir ",v"))
700 ;; (full-trial (concat filedir trial))
701 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
702 ;; (and (stringp file)
703 ;; (file-exists-p file)
704 ;; (or
705 ;; (and
706 ;; (file-exists-p full-trial)
707 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
708 ;; ;; don't be fooled by this!
709 ;; (not (equal (file-attributes file)
710 ;; (file-attributes full-trial))))
711 ;; ;; check if a version is in RCS/ directory
712 ;; (file-exists-p full-rcs-trial)))
713 ;; ))
716 (defsubst viper-file-checked-in-p (file)
717 (and (featurep 'vc-hooks)
718 ;; CVS files are considered not checked in
719 ;; FIXME: Should this deal with more than CVS?
720 (not (memq (vc-backend file) '(nil CVS)))
721 (if (fboundp 'vc-state)
722 (and
723 (not (memq (vc-state file) '(edited needs-merge)))
724 (not (stringp (vc-state file))))
725 ;; XEmacs has no vc-state
726 (if (featurep 'xemacs) (not (vc-locking-user file))))))
728 ;; checkout if visited file is checked in
729 (defun viper-maybe-checkout (buf)
730 (let ((file (expand-file-name (buffer-file-name buf)))
731 (checkout-function (key-binding "\C-x\C-q")))
732 (if (and (viper-file-checked-in-p file)
733 (or (beep 1) t)
734 (y-or-n-p
735 (format
736 "File %s is checked in. Check it out? "
737 (viper-abbreviate-file-name file))))
738 (with-current-buffer buf
739 (command-execute checkout-function)))))
744 ;;; Overlays
745 (defun viper-put-on-search-overlay (beg end)
746 (if (viper-overlay-p viper-search-overlay)
747 (viper-move-overlay viper-search-overlay beg end)
748 (setq viper-search-overlay (viper-make-overlay beg end (current-buffer)))
749 (viper-overlay-put
750 viper-search-overlay 'priority viper-search-overlay-priority))
751 (viper-overlay-put viper-search-overlay 'face viper-search-face))
753 ;; Search
755 (defun viper-flash-search-pattern ()
756 (if (not (viper-has-face-support-p))
758 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
759 (sit-for 2)
760 (viper-overlay-put viper-search-overlay 'face nil)))
762 (defun viper-hide-search-overlay ()
763 (if (not (viper-overlay-p viper-search-overlay))
764 (progn
765 (setq viper-search-overlay
766 (viper-make-overlay (point-min) (point-min) (current-buffer)))
767 (viper-overlay-put
768 viper-search-overlay 'priority viper-search-overlay-priority)))
769 (viper-overlay-put viper-search-overlay 'face nil))
771 ;; Replace state
773 (defsubst viper-move-replace-overlay (beg end)
774 (viper-move-overlay viper-replace-overlay beg end))
776 (defun viper-set-replace-overlay (beg end)
777 (if (viper-overlay-live-p viper-replace-overlay)
778 (viper-move-replace-overlay beg end)
779 (setq viper-replace-overlay (viper-make-overlay beg end (current-buffer)))
780 ;; never detach
781 (viper-overlay-put
782 viper-replace-overlay (if (featurep 'emacs) 'evaporate 'detachable) nil)
783 (viper-overlay-put
784 viper-replace-overlay 'priority viper-replace-overlay-priority)
785 ;; If Emacs will start supporting overlay maps, as it currently supports
786 ;; text-property maps, we could do away with viper-replace-minor-mode and
787 ;; just have keymap attached to replace overlay.
788 ;;(viper-overlay-put
789 ;; viper-replace-overlay
790 ;; (if (featurep 'xemacs) 'keymap 'local-map)
791 ;; viper-replace-map)
793 (if (viper-has-face-support-p)
794 (viper-overlay-put
795 viper-replace-overlay 'face viper-replace-overlay-face))
796 (viper-save-cursor-color 'before-replace-mode)
797 (viper-change-cursor-color
798 (viper-frame-value viper-replace-overlay-cursor-color)))
801 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph)
802 (or (viper-overlay-live-p viper-replace-overlay)
803 (viper-set-replace-overlay (point-min) (point-min)))
804 (if (or (not (viper-has-face-support-p))
805 viper-use-replace-region-delimiters)
806 (let ((before-name (if (featurep 'xemacs) 'begin-glyph 'before-string))
807 (after-name (if (featurep 'xemacs) 'end-glyph 'after-string)))
808 (viper-overlay-put viper-replace-overlay before-name before-glyph)
809 (viper-overlay-put viper-replace-overlay after-name after-glyph))))
811 (defun viper-hide-replace-overlay ()
812 (viper-set-replace-overlay-glyphs nil nil)
813 (viper-restore-cursor-color 'after-replace-mode)
814 (viper-restore-cursor-color 'after-insert-mode)
815 (if (viper-has-face-support-p)
816 (viper-overlay-put viper-replace-overlay 'face nil)))
819 (defsubst viper-replace-start ()
820 (viper-overlay-start viper-replace-overlay))
821 (defsubst viper-replace-end ()
822 (viper-overlay-end viper-replace-overlay))
825 ;; Minibuffer
827 (defun viper-set-minibuffer-overlay ()
828 (viper-check-minibuffer-overlay)
829 (when (viper-has-face-support-p)
830 (viper-overlay-put
831 viper-minibuffer-overlay 'face viper-minibuffer-current-face)
832 (viper-overlay-put
833 viper-minibuffer-overlay 'priority viper-minibuffer-overlay-priority)
834 ;; never detach
835 (viper-overlay-put
836 viper-minibuffer-overlay
837 (if (featurep 'emacs) 'evaporate 'detachable)
838 nil)
839 ;; make viper-minibuffer-overlay open-ended
840 ;; In emacs, it is made open ended at creation time
841 (when (featurep 'xemacs)
842 (viper-overlay-put viper-minibuffer-overlay 'start-open nil)
843 (viper-overlay-put viper-minibuffer-overlay 'end-open nil))))
845 (defun viper-check-minibuffer-overlay ()
846 (if (viper-overlay-live-p viper-minibuffer-overlay)
847 (viper-move-overlay
848 viper-minibuffer-overlay
849 (if (fboundp 'minibuffer-prompt-end) (minibuffer-prompt-end) 1)
850 (1+ (buffer-size)))
851 (setq viper-minibuffer-overlay
852 (if (featurep 'xemacs)
853 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
854 ;; make overlay open-ended
855 (viper-make-overlay
856 (if (fboundp 'minibuffer-prompt-end) (minibuffer-prompt-end) 1)
857 (1+ (buffer-size))
858 (current-buffer) nil 'rear-advance)))))
861 (defsubst viper-is-in-minibuffer ()
862 (save-match-data
863 (string-match "\*Minibuf-" (buffer-name))))
867 ;;; XEmacs compatibility
869 (defun viper-abbreviate-file-name (file)
870 (if (featurep 'xemacs)
871 (abbreviate-file-name file t) ; XEmacs requires addl argument
872 (abbreviate-file-name file)))
874 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
875 ;; in sit-for, so this function smoothes out the differences.
876 (defsubst viper-sit-for-short (val &optional nodisp)
877 (sit-for (/ val 1000.0) nodisp))
879 ;; EVENT may be a single event of a sequence of events
880 (defsubst viper-ESC-event-p (event)
881 (let ((ESC-keys '(?\e (control \[) escape))
882 (key (viper-event-key event)))
883 (member key ESC-keys)))
885 ;; checks if object is a marker, has a buffer, and points to within that buffer
886 (defun viper-valid-marker (marker)
887 (if (and (markerp marker) (marker-buffer marker))
888 (let ((buf (marker-buffer marker))
889 (pos (marker-position marker)))
890 (with-current-buffer buf
891 (and (<= pos (point-max)) (<= (point-min) pos))))))
893 (defsubst viper-mark-marker ()
894 (if (featurep 'xemacs) (mark-marker t)
895 (mark-marker)))
897 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
898 ;; is the same as (mark t).
899 (defsubst viper-set-mark-if-necessary ()
900 (setq mark-ring (delete (viper-mark-marker) mark-ring))
901 (set-mark-command nil)
902 (setq viper-saved-mark (point)))
904 ;; In transient mark mode (zmacs mode), it is annoying when regions become
905 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
906 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
907 (defun viper-deactivate-mark ()
908 (if (featurep 'xemacs)
909 (zmacs-deactivate-region)
910 (deactivate-mark)))
912 (defsubst viper-leave-region-active ()
913 (if (featurep 'xemacs) (setq zmacs-region-stays t)))
915 ;; Check if arg is a valid character for register
916 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
917 ;; Letter means lowercase letters, Letter means uppercase letters, and
918 ;; digit means digits from 1 to 9.
919 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
920 (defun viper-valid-register (reg &optional type)
921 (or type (setq type '(letter Letter digit)))
922 (or (if (memq 'letter type)
923 (and (<= ?a reg) (<= reg ?z)))
924 (if (memq 'digit type)
925 (and (<= ?1 reg) (<= reg ?9)))
926 (if (memq 'Letter type)
927 (and (<= ?A reg) (<= reg ?Z)))
932 ;; it is suggested that an event must be copied before it is assigned to
933 ;; last-command-event in XEmacs
934 (defun viper-copy-event (event)
935 (if (featurep 'xemacs) (copy-event event)
936 event))
938 ;; Uses different timeouts for ESC-sequences and others
939 (defun viper-fast-keysequence-p ()
940 (not (viper-sit-for-short
941 (if (viper-ESC-event-p last-input-event)
942 (viper-ESC-keyseq-timeout)
943 viper-fast-keyseq-timeout)
944 t)))
946 ;; like read-event, but in XEmacs also try to convert to char, if possible
947 (defun viper-read-event-convert-to-char ()
948 (let (event)
949 (if (featurep 'xemacs)
950 (progn
951 (setq event (next-command-event))
952 (or (event-to-character event)
953 event))
954 (read-event))))
956 ;; Viperized read-key-sequence
957 (defun viper-read-key-sequence (prompt &optional continue-echo)
958 (let (inhibit-quit event keyseq)
959 (setq keyseq (read-key-sequence prompt continue-echo))
960 (setq event (if (featurep 'xemacs)
961 (elt keyseq 0) ; XEmacs returns vector of events
962 (elt (listify-key-sequence keyseq) 0)))
963 (if (viper-ESC-event-p event)
964 (let (unread-command-events)
965 (if (viper-fast-keysequence-p)
966 (let ((viper-vi-global-user-minor-mode nil)
967 (viper-vi-local-user-minor-mode nil)
968 (viper-vi-intercept-minor-mode nil)
969 (viper-insert-intercept-minor-mode nil)
970 (viper-replace-minor-mode nil) ; actually unnecessary
971 (viper-insert-global-user-minor-mode nil)
972 (viper-insert-local-user-minor-mode nil))
973 ;; Note: set unread-command-events only after testing for fast
974 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
975 ;; always t -- whether there is anything after ESC or not
976 (viper-set-unread-command-events keyseq)
977 (setq keyseq (read-key-sequence nil)))
978 (viper-set-unread-command-events keyseq)
979 (setq keyseq (read-key-sequence nil)))))
980 keyseq))
983 ;; This function lets function-key-map convert key sequences into logical
984 ;; keys. This does a better job than viper-read-event when it comes to kbd
985 ;; macros, since it enables certain macros to be shared between X and TTY modes
986 ;; by correctly mapping key sequences for Left/Right/... (on an ascii
987 ;; terminal) into logical keys left, right, etc.
988 (defun viper-read-key ()
989 (let ((overriding-local-map viper-overriding-map)
990 (inhibit-quit t)
991 help-char key)
992 (use-global-map viper-overriding-map)
993 (unwind-protect
994 (setq key (elt (viper-read-key-sequence nil) 0))
995 (use-global-map global-map))
996 key))
999 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
1000 ;; instead of nil, if '(nil) was previously inadvertently assigned to
1001 ;; unread-command-events
1002 (defun viper-event-key (event)
1003 (or (and event (eventp event))
1004 (error "viper-event-key: Wrong type argument, eventp, %S" event))
1005 (when (if (featurep 'xemacs)
1006 (or (key-press-event-p event) (mouse-event-p event)) ; xemacs
1007 t ; emacs
1009 (let ((mod (event-modifiers event))
1010 basis)
1011 (setq basis
1012 (if (featurep 'xemacs)
1013 ;; XEmacs
1014 (cond ((key-press-event-p event)
1015 (event-key event))
1016 ((button-event-p event)
1017 (concat "mouse-" (prin1-to-string (event-button event))))
1019 (error "viper-event-key: Unknown event, %S" event)))
1020 ;; Emacs doesn't handle capital letters correctly, since
1021 ;; \S-a isn't considered the same as A (it behaves as
1022 ;; plain `a' instead). So we take care of this here
1023 (cond ((and (viper-characterp event) (<= ?A event) (<= event ?Z))
1024 (setq mod nil
1025 event event))
1026 ;; Emacs has the oddity whereby characters 128+char
1027 ;; represent M-char *if* this appears inside a string.
1028 ;; So, we convert them manually to (meta char).
1029 ((and (viper-characterp event)
1030 (< ?\C-? event) (<= event 255))
1031 (setq mod '(meta)
1032 event (- event ?\C-? 1)))
1033 ((and (null mod) (eq event 'return))
1034 (setq event ?\C-m))
1035 ((and (null mod) (eq event 'space))
1036 (setq event ?\ ))
1037 ((and (null mod) (eq event 'delete))
1038 (setq event ?\C-?))
1039 ((and (null mod) (eq event 'backspace))
1040 (setq event ?\C-h))
1041 (t (event-basic-type event)))
1042 ) ; (featurep 'xemacs)
1044 (if (viper-characterp basis)
1045 (setq basis
1046 (if (viper= basis ?\C-?)
1047 (list 'control '\?) ; taking care of an emacs bug
1048 (intern (char-to-string basis)))))
1049 (if mod
1050 (append mod (list basis))
1051 basis))))
1053 (defun viper-last-command-char ()
1054 (if (featurep 'xemacs)
1055 (event-to-character last-command-event)
1056 last-command-event))
1058 (defun viper-key-to-emacs-key (key)
1059 (let (key-name char-p modifiers mod-char-list base-key base-key-name)
1060 (cond ((featurep 'xemacs) key)
1062 ((symbolp key)
1063 (setq key-name (symbol-name key))
1064 (cond ((= (length key-name) 1) ; character event
1065 (string-to-char key-name))
1066 ;; Emacs doesn't recognize `return' and `escape' as events on
1067 ;; dumb terminals, so we translate them into characters
1068 ((and (featurep 'emacs) (not (viper-window-display-p))
1069 (string= key-name "return"))
1070 ?\C-m)
1071 ((and (featurep 'emacs) (not (viper-window-display-p))
1072 (string= key-name "escape"))
1073 ?\e)
1074 ;; pass symbol-event as is
1075 (t key)))
1077 ((listp key)
1078 (setq modifiers (viper-subseq key 0 (1- (length key)))
1079 base-key (viper-seq-last-elt key)
1080 base-key-name (symbol-name base-key)
1081 char-p (= (length base-key-name) 1))
1082 (setq mod-char-list
1083 (mapcar
1084 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
1085 modifiers))
1086 (if char-p
1087 (setq key-name
1088 (car (read-from-string
1089 (concat
1090 "?\\"
1091 (mapconcat 'identity mod-char-list "-\\")
1093 base-key-name))))
1094 (setq key-name
1095 (intern
1096 (concat
1097 (mapconcat 'identity mod-char-list "-")
1099 base-key-name))))))
1103 ;; LIS is assumed to be a list of events of characters
1104 (defun viper-eventify-list-xemacs (lis)
1105 (if (featurep 'xemacs)
1106 (mapcar
1107 (lambda (elt)
1108 (cond ((viper-characterp elt) (character-to-event elt))
1109 ((eventp elt) elt)
1110 (t (error
1111 "viper-eventify-list-xemacs: can't convert to event, %S"
1112 elt))))
1113 lis)))
1116 ;; Smoothes out the difference between Emacs' unread-command-events
1117 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1118 ;; events or a sequence of keys.
1120 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1121 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1122 ;; into an event. Below, we delete nil from event lists, since nil is the most
1123 ;; common symbol that might appear in this wrong context.
1124 (defun viper-set-unread-command-events (arg)
1125 (if (featurep 'emacs)
1126 (setq
1127 unread-command-events
1128 (let ((new-events
1129 (cond ((eventp arg) (list arg))
1130 ((listp arg) arg)
1131 ((sequencep arg)
1132 (listify-key-sequence arg))
1133 (t (error
1134 "viper-set-unread-command-events: Invalid argument, %S"
1135 arg)))))
1136 (if (not (eventp nil))
1137 (setq new-events (delq nil new-events)))
1138 (append new-events unread-command-events)))
1139 ;; XEmacs
1140 (setq
1141 unread-command-events
1142 (append
1143 (cond ((viper-characterp arg) (list (character-to-event arg)))
1144 ((eventp arg) (list arg))
1145 ((stringp arg) (mapcar 'character-to-event arg))
1146 ((vectorp arg) (append arg nil)) ; turn into list
1147 ((listp arg) (viper-eventify-list-xemacs arg))
1148 (t (error
1149 "viper-set-unread-command-events: Invalid argument, %S" arg)))
1150 unread-command-events))))
1153 ;; Check if vec is a vector of key-press events representing characters
1154 ;; XEmacs only
1155 (defun viper-event-vector-p (vec)
1156 (and (vectorp vec)
1157 (eval (cons 'and (mapcar '(lambda (elt) (if (eventp elt) t)) vec)))))
1160 ;; check if vec is a vector of character symbols
1161 (defun viper-char-symbol-sequence-p (vec)
1162 (and
1163 (sequencep vec)
1164 (eval
1165 (cons 'and
1166 (mapcar (lambda (elt)
1167 (and (symbolp elt) (= (length (symbol-name elt)) 1)))
1168 vec)))))
1171 (defun viper-char-array-p (array)
1172 (eval (cons 'and (mapcar 'viper-characterp array))))
1175 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1176 ;; convert events to keys and, if all keys are regular printable
1177 ;; characters, will return a string. Otherwise, will return a string
1178 ;; representing a vector of converted events. If the input was a Viper macro,
1179 ;; will return a string that represents this macro as a vector.
1180 (defun viper-array-to-string (event-seq)
1181 (let (temp temp2)
1182 (cond ((stringp event-seq) event-seq)
1183 ((viper-event-vector-p event-seq)
1184 (setq temp (mapcar 'viper-event-key event-seq))
1185 (cond ((viper-char-symbol-sequence-p temp)
1186 (mapconcat 'symbol-name temp ""))
1187 ((and (viper-char-array-p
1188 (setq temp2 (mapcar 'viper-key-to-character temp))))
1189 (mapconcat 'char-to-string temp2 ""))
1190 (t (prin1-to-string (vconcat temp)))))
1191 ((viper-char-symbol-sequence-p event-seq)
1192 (mapconcat 'symbol-name event-seq ""))
1193 ((and (vectorp event-seq)
1194 (viper-char-array-p
1195 (setq temp (mapcar 'viper-key-to-character event-seq))))
1196 (mapconcat 'char-to-string temp ""))
1197 (t (prin1-to-string event-seq)))))
1199 (defun viper-key-press-events-to-chars (events)
1200 (mapconcat (if (featurep 'xemacs)
1201 (lambda (elt) (char-to-string (event-to-character elt))) ; xemacs
1202 'char-to-string ; emacs
1204 events
1205 ""))
1208 (defun viper-read-char-exclusive ()
1209 (let (char
1210 (echo-keystrokes 1))
1211 (while (null char)
1212 (condition-case nil
1213 (setq char (read-char))
1214 (error
1215 ;; skip event if not char
1216 (viper-read-event))))
1217 char))
1219 ;; key is supposed to be in viper's representation, e.g., (control l), a
1220 ;; character, etc.
1221 (defun viper-key-to-character (key)
1222 (cond ((eq key 'space) ?\ )
1223 ((eq key 'delete) ?\C-?)
1224 ((eq key 'return) ?\C-m)
1225 ((eq key 'backspace) ?\C-h)
1226 ((and (symbolp key)
1227 (= 1 (length (symbol-name key))))
1228 (string-to-char (symbol-name key)))
1229 ((and (listp key)
1230 (eq (car key) 'control)
1231 (symbol-name (nth 1 key))
1232 (= 1 (length (symbol-name (nth 1 key)))))
1233 (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
1234 (t key)))
1237 (defun viper-setup-master-buffer (&rest other-files-or-buffers)
1238 "Set up the current buffer as a master buffer.
1239 Arguments become related buffers. This function should normally be used in
1240 the `Local variables' section of a file."
1241 (setq viper-related-files-and-buffers-ring
1242 (make-ring (1+ (length other-files-or-buffers))))
1243 (mapc '(lambda (elt)
1244 (viper-ring-insert viper-related-files-and-buffers-ring elt))
1245 other-files-or-buffers)
1246 (viper-ring-insert viper-related-files-and-buffers-ring (buffer-name))
1249 ;;; Movement utilities
1251 ;; Characters that should not be considered as part of the word, in reformed-vi
1252 ;; syntax mode.
1253 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1254 ;; into characters at some point and then put back to string. The result is
1255 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1256 ;; effect of quoting - and preventing it from being special.
1257 (defconst viper-non-word-characters-reformed-vi
1258 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1259 ;; These are characters that are not to be considered as parts of a word in
1260 ;; Viper.
1261 ;; Set each time state changes and at loading time
1262 (viper-deflocalvar viper-non-word-characters nil)
1264 ;; must be buffer-local
1265 (viper-deflocalvar viper-ALPHA-char-class "w"
1266 "String of syntax classes characterizing Viper's alphanumeric symbols.
1267 In addition, the symbol `_' may be considered alphanumeric if
1268 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1270 (defconst viper-strict-ALPHA-chars "a-zA-Z0-9_"
1271 "Regexp matching the set of alphanumeric characters acceptable to strict
1272 Vi.")
1273 (defconst viper-strict-SEP-chars " \t\n"
1274 "Regexp matching the set of alphanumeric characters acceptable to strict
1275 Vi.")
1276 (defconst viper-strict-SEP-chars-sans-newline " \t"
1277 "Regexp matching the set of alphanumeric characters acceptable to strict
1278 Vi.")
1280 (defconst viper-SEP-char-class " -"
1281 "String of syntax classes for Vi separators.
1282 Usually contains ` ', linefeed, TAB or formfeed.")
1285 ;; Set Viper syntax classes and related variables according to
1286 ;; `viper-syntax-preference'.
1287 (defun viper-update-syntax-classes (&optional set-default)
1288 (let ((preference (cond ((eq viper-syntax-preference 'emacs)
1289 "w") ; Viper words have only Emacs word chars
1290 ((eq viper-syntax-preference 'extended)
1291 "w_") ; Viper words have Emacs word & symbol chars
1292 (t "w"))) ; Viper words are Emacs words plus `_'
1293 (non-word-chars (cond ((eq viper-syntax-preference 'reformed-vi)
1294 (viper-string-to-list
1295 viper-non-word-characters-reformed-vi))
1296 (t nil))))
1297 (if set-default
1298 (setq-default viper-ALPHA-char-class preference
1299 viper-non-word-characters non-word-chars)
1300 (setq viper-ALPHA-char-class preference
1301 viper-non-word-characters non-word-chars))
1304 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1305 ;; is `nil'. If nil, use setq.
1306 (defun viper-set-syntax-preference (&optional symbol value)
1307 "Set Viper syntax preference.
1308 If called interactively or if SYMBOL is nil, sets syntax preference in current
1309 buffer. If called non-interactively, preferably via the customization widget,
1310 sets the default value."
1311 (interactive)
1312 (or value
1313 (setq value
1314 (completing-read
1315 "Viper syntax preference: "
1316 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1317 nil 'require-match)))
1318 (if (stringp value) (setq value (intern value)))
1319 (or (memq value '(strict-vi reformed-vi extended emacs))
1320 (error "Invalid Viper syntax preference, %S" value))
1321 (if symbol
1322 (setq-default viper-syntax-preference value)
1323 (setq viper-syntax-preference value))
1324 (viper-update-syntax-classes))
1326 (defcustom viper-syntax-preference 'reformed-vi
1327 "*Syntax type characterizing Viper's alphanumeric symbols.
1328 Affects movement and change commands that deal with Vi-style words.
1329 Works best when set in the hooks to various major modes.
1331 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1333 `reformed-vi' means Viper words are like Emacs words \(as determined using
1334 Emacs syntax tables, which are different for different major modes\) with two
1335 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1336 symbols, such as `,',:,\",),{, etc., are excluded.
1337 This behaves very close to `strict-vi', but also works well with non-ASCII
1338 characters from various alphabets.
1340 `extended' means Viper word constituents are symbols that are marked as being
1341 parts of words OR symbols in Emacs syntax tables.
1342 This is most appropriate for major modes intended for editing programs.
1344 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1345 syntax tables.
1346 This option is appropriate if you like Emacs-style words."
1347 :type '(radio (const strict-vi) (const reformed-vi)
1348 (const extended) (const emacs))
1349 :set 'viper-set-syntax-preference
1350 :group 'viper)
1351 (make-variable-buffer-local 'viper-syntax-preference)
1354 ;; addl-chars are characters to be temporarily considered as alphanumerical
1355 (defun viper-looking-at-alpha (&optional addl-chars)
1356 (or (stringp addl-chars) (setq addl-chars ""))
1357 (if (eq viper-syntax-preference 'reformed-vi)
1358 (setq addl-chars (concat addl-chars "_")))
1359 (let ((char (char-after (point))))
1360 (if char
1361 (if (eq viper-syntax-preference 'strict-vi)
1362 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars "]"))
1364 ;; or one of the additional chars being asked to include
1365 (viper-memq-char char (viper-string-to-list addl-chars))
1366 (and
1367 ;; not one of the excluded word chars (note:
1368 ;; viper-non-word-characters is a list)
1369 (not (viper-memq-char char viper-non-word-characters))
1370 ;; char of the Viper-word syntax class
1371 (viper-memq-char (char-syntax char)
1372 (viper-string-to-list viper-ALPHA-char-class))))))
1375 (defun viper-looking-at-separator ()
1376 (let ((char (char-after (point))))
1377 (if char
1378 (if (eq viper-syntax-preference 'strict-vi)
1379 (viper-memq-char char (viper-string-to-list viper-strict-SEP-chars))
1380 (or (eq char ?\n) ; RET is always a separator in Vi
1381 (viper-memq-char (char-syntax char)
1382 (viper-string-to-list viper-SEP-char-class)))))
1385 (defsubst viper-looking-at-alphasep (&optional addl-chars)
1386 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars)))
1388 (defun viper-skip-alpha-forward (&optional addl-chars)
1389 (or (stringp addl-chars) (setq addl-chars ""))
1390 (viper-skip-syntax
1391 'forward
1392 (cond ((eq viper-syntax-preference 'strict-vi)
1394 (t viper-ALPHA-char-class))
1395 (cond ((eq viper-syntax-preference 'strict-vi)
1396 (concat viper-strict-ALPHA-chars addl-chars))
1397 (t addl-chars))))
1399 (defun viper-skip-alpha-backward (&optional addl-chars)
1400 (or (stringp addl-chars) (setq addl-chars ""))
1401 (viper-skip-syntax
1402 'backward
1403 (cond ((eq viper-syntax-preference 'strict-vi)
1405 (t viper-ALPHA-char-class))
1406 (cond ((eq viper-syntax-preference 'strict-vi)
1407 (concat viper-strict-ALPHA-chars addl-chars))
1408 (t addl-chars))))
1410 ;; weird syntax tables may confuse strict-vi style
1411 (defsubst viper-skip-all-separators-forward (&optional within-line)
1412 (if (eq viper-syntax-preference 'strict-vi)
1413 (if within-line
1414 (skip-chars-forward viper-strict-SEP-chars-sans-newline)
1415 (skip-chars-forward viper-strict-SEP-chars))
1416 (viper-skip-syntax 'forward
1417 viper-SEP-char-class
1418 (or within-line "\n")
1419 (if within-line (viper-line-pos 'end)))))
1421 (defsubst viper-skip-all-separators-backward (&optional within-line)
1422 (if (eq viper-syntax-preference 'strict-vi)
1423 (if within-line
1424 (skip-chars-backward viper-strict-SEP-chars-sans-newline)
1425 (skip-chars-backward viper-strict-SEP-chars))
1426 (viper-skip-syntax 'backward
1427 viper-SEP-char-class
1428 (or within-line "\n")
1429 (if within-line (viper-line-pos 'start)))))
1430 (defun viper-skip-nonseparators (direction)
1431 (viper-skip-syntax
1432 direction
1433 (concat "^" viper-SEP-char-class)
1435 (viper-line-pos (if (eq direction 'forward) 'end 'start))))
1438 ;; skip over non-word constituents and non-separators
1439 (defun viper-skip-nonalphasep-forward ()
1440 (if (eq viper-syntax-preference 'strict-vi)
1441 (skip-chars-forward
1442 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
1443 (viper-skip-syntax
1444 'forward
1445 (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
1446 ;; Emacs may consider some of these as words, but we don't want them
1447 viper-non-word-characters
1448 (viper-line-pos 'end))))
1450 (defun viper-skip-nonalphasep-backward ()
1451 (if (eq viper-syntax-preference 'strict-vi)
1452 (skip-chars-backward
1453 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
1454 (viper-skip-syntax
1455 'backward
1456 (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
1457 ;; Emacs may consider some of these as words, but we don't want them
1458 viper-non-word-characters
1459 (viper-line-pos 'start))))
1461 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1462 ;; Return the number of chars traveled.
1463 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1464 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1465 ;; words, even if Emacs syntax table says they are.
1466 (defun viper-skip-syntax (direction syntax addl-chars &optional limit)
1467 (let ((total 0)
1468 (local 1)
1469 (skip-chars-func
1470 (if (eq direction 'forward)
1471 'skip-chars-forward 'skip-chars-backward))
1472 (skip-syntax-func
1473 (if (eq direction 'forward)
1474 'viper-forward-char-carefully 'viper-backward-char-carefully))
1475 char-looked-at syntax-of-char-looked-at negated-syntax)
1476 (setq addl-chars
1477 (cond ((listp addl-chars) (viper-charlist-to-string addl-chars))
1478 ((stringp addl-chars) addl-chars)
1479 (t "")))
1480 (setq syntax
1481 (cond ((listp syntax) syntax)
1482 ((stringp syntax) (viper-string-to-list syntax))
1483 (t nil)))
1484 (if (memq ?^ syntax) (setq negated-syntax t))
1486 (while (and (not (= local 0))
1487 (cond ((eq direction 'forward)
1488 (not (eobp)))
1489 (t (not (bobp)))))
1490 (setq char-looked-at (viper-char-at-pos direction)
1491 ;; if outside the range, set to nil
1492 syntax-of-char-looked-at (if char-looked-at
1493 (char-syntax char-looked-at)))
1494 (setq local
1495 (+ (if (and
1496 (cond ((and limit (eq direction 'forward))
1497 (< (point) limit))
1498 (limit ; backward & limit
1499 (> (point) limit))
1500 (t t)) ; no limit
1501 ;; char under/before cursor has appropriate syntax
1502 (if negated-syntax
1503 (not (memq syntax-of-char-looked-at syntax))
1504 (memq syntax-of-char-looked-at syntax))
1505 ;; if char-syntax class is "word", make sure it is not one
1506 ;; of the excluded characters
1507 (if (and (eq syntax-of-char-looked-at ?w)
1508 (not negated-syntax))
1509 (not (viper-memq-char
1510 char-looked-at viper-non-word-characters))
1512 (funcall skip-syntax-func 1)
1514 (funcall skip-chars-func addl-chars limit)))
1515 (setq total (+ total local)))
1516 total
1519 ;; tells when point is at the beginning of field
1520 (defun viper-beginning-of-field ()
1521 (or (bobp)
1522 (not (eq (get-char-property (point) 'field)
1523 (get-char-property (1- (point)) 'field)))))
1526 ;; this is copied from cl-extra.el
1527 ;; Return the subsequence of SEQ from START to END.
1528 ;; If END is omitted, it defaults to the length of the sequence.
1529 ;; If START or END is negative, it counts from the end.
1530 (defun viper-subseq (seq start &optional end)
1531 (if (stringp seq) (substring seq start end)
1532 (let (len)
1533 (and end (< end 0) (setq end (+ end (setq len (length seq)))))
1534 (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
1535 (cond ((listp seq)
1536 (if (> start 0) (setq seq (nthcdr start seq)))
1537 (if end
1538 (let ((res nil))
1539 (while (>= (setq end (1- end)) start)
1540 (push (pop seq) res))
1541 (nreverse res))
1542 (copy-sequence seq)))
1544 (or end (setq end (or len (length seq))))
1545 (let ((res (make-vector (max (- end start) 0) nil))
1546 (i 0))
1547 (while (< start end)
1548 (aset res i (aref seq start))
1549 (setq i (1+ i) start (1+ start)))
1550 res))))))
1554 ;; Local Variables:
1555 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1556 ;; End:
1558 ;; arch-tag: 7f023fd5-dd9e-4378-a397-9c179553b0e3
1559 ;;; viper-util.el ends here