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 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
33 (defvar viper-overriding-map
)
34 (defvar pm-color-alist
)
35 (defvar viper-minibuffer-current-face
)
36 (defvar viper-minibuffer-insert-face
)
37 (defvar viper-minibuffer-vi-face
)
38 (defvar viper-minibuffer-emacs-face
)
39 (defvar viper-replace-overlay-face
)
40 (defvar viper-fast-keyseq-timeout
)
41 (defvar ex-unix-type-shell
)
42 (defvar ex-unix-type-shell-options
)
43 (defvar viper-ex-tmp-buf-name
)
44 (defvar viper-syntax-preference
)
45 (defvar viper-saved-mark
)
50 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
57 ;; A fix for NeXT Step
58 ;; Should go away, when NS people fix the design flaw, which leaves the
59 ;; two x-* functions undefined.
60 (if (and (not (fboundp 'x-display-color-p
)) (fboundp 'ns-display-color-p
))
61 (fset 'x-display-color-p
(symbol-function 'ns-display-color-p
)))
62 (if (and (not (fboundp 'x-color-defined-p
)) (fboundp 'ns-color-defined-p
))
63 (fset 'x-color-defined-p
(symbol-function 'ns-color-defined-p
)))
66 (defalias 'viper-overlay-p
67 (if (featurep 'xemacs
) 'extentp
'overlayp
))
68 (defalias 'viper-make-overlay
69 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
70 (defalias 'viper-overlay-live-p
71 (if (featurep 'xemacs
) 'extent-live-p
'overlayp
))
72 (defalias 'viper-move-overlay
73 (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
))
74 (defalias 'viper-overlay-start
75 (if (featurep 'xemacs
) 'extent-start-position
'overlay-start
))
76 (defalias 'viper-overlay-end
77 (if (featurep 'xemacs
) 'extent-end-position
'overlay-end
))
78 (defalias 'viper-overlay-get
79 (if (featurep 'xemacs
) 'extent-property
'overlay-get
))
80 (defalias 'viper-overlay-put
81 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
82 (defalias 'viper-read-event
83 (if (featurep 'xemacs
) 'next-command-event
'read-event
))
84 (defalias 'viper-characterp
85 (if (featurep 'xemacs
) 'characterp
'integerp
))
86 (defalias 'viper-int-to-char
87 (if (featurep 'xemacs
) 'int-to-char
'identity
))
88 (defalias 'viper-get-face
89 (if (featurep 'xemacs
) 'get-face
'internal-get-face
))
90 (defalias 'viper-color-defined-p
91 (if (featurep 'xemacs
) 'valid-color-name-p
'x-color-defined-p
))
92 (defalias 'viper-iconify
93 (if (featurep 'xemacs
) 'iconify-frame
'iconify-or-deiconify-frame
))
96 ;; CHAR is supposed to be a char or an integer (positive or negative)
97 ;; LIST is a list of chars, nil, and negative numbers
98 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
99 ;; Introduced for compatibility with XEmacs, where integers are not the same as
101 (defun viper-memq-char (char list
)
102 (cond ((and (integerp char
) (>= char
0))
103 (memq (viper-int-to-char char
) list
))
106 ;; Check if char-or-int and char are the same as characters
107 (defun viper-char-equal (char-or-int char
)
108 (cond ((and (integerp char-or-int
) (>= char-or-int
0))
109 (= (viper-int-to-char char-or-int
) char
))
110 ((eq char-or-int char
))))
112 ;; Like =, but accommodates null and also is t for eq-objects
113 (defun viper= (char char1
)
114 (cond ((eq char char1
) t
)
115 ((and (viper-characterp char
) (viper-characterp char1
))
119 (defsubst viper-color-display-p
()
120 (if (featurep 'xemacs
) (eq (device-class (selected-device)) 'color
)
121 (x-display-color-p)))
123 (defun viper-get-cursor-color (&optional frame
)
124 (if (featurep 'xemacs
)
126 (frame-property (or frame
(selected-frame)) 'cursor-color
))
127 (cdr (assoc 'cursor-color
(frame-parameters)))))
129 (defmacro viper-frame-value
(variable)
130 "Return the value of VARIABLE local to the current frame, if there is one.
131 Otherwise return the normal value."
132 `(if (featurep 'xemacs
)
134 ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
135 ;; so we do it by hand instead.
136 ;; Buffer-local values take precedence over frame-local ones.
137 (if (local-variable-p ',variable
)
139 ;; Distinguish between no frame parameter and a frame parameter
140 ;; with a value of nil.
141 (let ((fp (assoc ',variable
(frame-parameters))))
146 (cond ((eq (viper-device-type) 'pm
)
147 (fset 'viper-color-defined-p
148 (lambda (color) (assoc color pm-color-alist
)))))
152 (defun viper-change-cursor-color (new-color &optional frame
)
153 (if (and (viper-window-display-p) (viper-color-display-p)
154 (stringp new-color
) (viper-color-defined-p new-color
)
155 (not (string= new-color
(viper-get-cursor-color))))
156 (if (featurep 'xemacs
)
158 (or frame
(selected-frame))
159 'cursor-color
(make-color-instance new-color
))
160 (modify-frame-parameters
161 (or frame
(selected-frame))
162 (list (cons 'cursor-color new-color
))))))
164 ;; Note that the colors this function uses might not be those
165 ;; associated with FRAME, if there are frame-local values.
166 ;; This was equally true before the advent of viper-frame-value.
167 ;; Now it could be changed by passing frame to v-f-v.
168 (defun viper-set-cursor-color-according-to-state (&optional frame
)
169 (cond ((eq viper-current-state
'replace-state
)
170 (viper-change-cursor-color
171 (viper-frame-value viper-replace-overlay-cursor-color
)
173 ((and (eq viper-current-state
'emacs-state
)
174 (viper-frame-value viper-emacs-state-cursor-color
))
175 (viper-change-cursor-color
176 (viper-frame-value viper-emacs-state-cursor-color
)
178 ((eq viper-current-state
'insert-state
)
179 (viper-change-cursor-color
180 (viper-frame-value viper-insert-state-cursor-color
)
183 (viper-change-cursor-color
184 (viper-frame-value viper-vi-state-cursor-color
)
187 ;; By default, saves current frame cursor color in the
188 ;; viper-saved-cursor-color-in-replace-mode property of viper-replace-overlay
189 (defun viper-save-cursor-color (before-which-mode)
190 (if (and (viper-window-display-p) (viper-color-display-p))
191 (let ((color (viper-get-cursor-color)))
192 (if (and (stringp color
) (viper-color-defined-p color
)
195 viper-replace-overlay-cursor-color
))))
196 (modify-frame-parameters
200 (cond ((eq before-which-mode
'before-replace-mode
)
201 'viper-saved-cursor-color-in-replace-mode
)
202 ((eq before-which-mode
'before-emacs-mode
)
203 'viper-saved-cursor-color-in-emacs-mode
)
205 'viper-saved-cursor-color-in-insert-mode
))
209 (defsubst viper-get-saved-cursor-color-in-replace-mode
()
212 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
214 'viper-saved-cursor-color-in-replace-mode
)
215 (or (and (eq viper-current-state
'emacs-mode
)
216 (viper-frame-value viper-emacs-state-cursor-color
))
217 (viper-frame-value viper-vi-state-cursor-color
))))
219 (defsubst viper-get-saved-cursor-color-in-insert-mode
()
222 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
224 'viper-saved-cursor-color-in-insert-mode
)
225 (or (and (eq viper-current-state
'emacs-mode
)
226 (viper-frame-value viper-emacs-state-cursor-color
))
227 (viper-frame-value viper-vi-state-cursor-color
))))
229 (defsubst viper-get-saved-cursor-color-in-emacs-mode
()
232 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
234 'viper-saved-cursor-color-in-emacs-mode
)
235 (viper-frame-value viper-vi-state-cursor-color
)))
237 ;; restore cursor color from replace overlay
238 (defun viper-restore-cursor-color(after-which-mode)
239 (if (viper-overlay-p viper-replace-overlay
)
240 (viper-change-cursor-color
241 (cond ((eq after-which-mode
'after-replace-mode
)
242 (viper-get-saved-cursor-color-in-replace-mode))
243 ((eq after-which-mode
'after-emacs-mode
)
244 (viper-get-saved-cursor-color-in-emacs-mode))
245 (t (viper-get-saved-cursor-color-in-insert-mode)))
249 ;; Check the current version against the major and minor version numbers
250 ;; using op: cur-vers op major.minor If emacs-major-version or
251 ;; emacs-minor-version are not defined, we assume that the current version
252 ;; is hopelessly outdated. We assume that emacs-major-version and
253 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
254 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
255 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
256 ;; incorrect. However, this gives correct result in our cases, since we are
257 ;; testing for sufficiently high Emacs versions.
258 (defun viper-check-version (op major minor
&optional type-of-emacs
)
259 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
260 (and (cond ((eq type-of-emacs
'xemacs
) (featurep 'xemacs
))
261 ((eq type-of-emacs
'emacs
) (featurep 'emacs
))
263 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
264 (= emacs-major-version major
)))
265 ((memq op
'(> >= < <=))
266 (and (or (funcall op emacs-major-version major
)
267 (= emacs-major-version major
))
268 (if (= emacs-major-version major
)
269 (funcall op emacs-minor-version minor
)
272 (error "%S: Invalid op in viper-check-version" op
))))
273 (cond ((memq op
'(= > >=)) nil
)
274 ((memq op
'(< <=)) t
))))
277 (defun viper-get-visible-buffer-window (wind)
278 (if (featurep 'xemacs
)
279 (get-buffer-window wind t
)
280 (get-buffer-window wind
'visible
)))
283 ;; Return line position.
284 ;; If pos is 'start then returns position of line start.
285 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
286 ;; Pos = 'indent returns beginning of indentation.
287 ;; Otherwise, returns point. Current point is not moved in any case."
288 (defun viper-line-pos (pos)
289 (let ((cur-pos (point))
297 (goto-char (+ (viper-line-pos 'start
) (viper-line-pos 'end
) 2)))
299 (back-to-indentation))
301 (setq result
(point))
305 ;; Emacs used to count each multibyte character as several positions in the buffer,
306 ;; so we had to use Emacs' chars-in-region to count characters. Since 20.3,
307 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
308 ;; counting each char as just one pos. So, now we can simply subtract beg from
309 ;; end to determine the number of characters in a region.
310 (defun viper-chars-in-region (beg end
&optional preserve-sign
)
311 ;;(let ((count (abs (if (fboundp 'chars-in-region)
312 ;; (chars-in-region beg end)
314 (let ((count (abs (- end beg
))))
315 (if (and (< end beg
) preserve-sign
)
319 ;; Test if POS is between BEG and END
320 (defsubst viper-pos-within-region
(pos beg end
)
321 (and (>= pos
(min beg end
)) (>= (max beg end
) pos
)))
324 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
325 ;; The first argument must eval to a variable name.
326 ;; Arguments: (var-name position &optional buffer).
328 ;; This is useful for moving markers that are supposed to be local.
329 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
330 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
331 ;; buffer, a new marker will be created.
332 (defun viper-move-marker-locally (var pos
&optional buffer
)
333 (if (markerp (eval var
))
335 (set var
(make-marker)))
336 (move-marker (eval var
) pos buffer
))
339 ;; Print CONDITIONS as a message.
340 (defun viper-message-conditions (conditions)
341 (let ((case (car conditions
)) (msg (cdr conditions
)))
344 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
349 ;;; List/alist utilities
351 ;; Convert LIST to an alist
352 (defun viper-list-to-alist (lst)
355 (setq alist
(cons (list (car lst
)) alist
))
356 (setq lst
(cdr lst
)))
359 ;; Convert ALIST to a list.
360 (defun viper-alist-to-list (alst)
363 (setq lst
(cons (car (car alst
)) lst
))
364 (setq alst
(cdr alst
)))
367 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
368 (defun viper-filter-alist (regexp alst
)
370 (let ((outalst) (inalst alst
))
372 (if (string-match regexp
(car (car inalst
)))
373 (setq outalst
(cons (car inalst
) outalst
)))
374 (setq inalst
(cdr inalst
)))
377 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
378 (defun viper-filter-list (regexp lst
)
380 (let ((outlst) (inlst lst
))
382 (if (string-match regexp
(car inlst
))
383 (setq outlst
(cons (car inlst
) outlst
)))
384 (setq inlst
(cdr inlst
)))
388 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
389 ;; LIS2 is modified by filtering it: deleting its members of the form
390 ;; \(car elt\) such that (car elt') is in LIS1.
391 (defun viper-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
)))
405 (declare-function viper-forward-Word
"viper-cmd" (arg))
407 ;;; Support for :e, :r, :w file globbing
409 ;; Glob the file spec.
410 ;; This function is designed to work under Unix. It might also work under VMS.
411 (defun viper-glob-unix-files (filespec)
413 (cond (ex-unix-type-shell shell-file-name
)
414 ((memq system-type
'(vax-vms axp-vms
)) "*dcl*") ; VAX VMS
415 (t "sh"))) ; probably Unix anyway
417 ;; using cond in anticipation of further additions
418 (cond (ex-unix-type-shell-options)
420 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec
))
421 (t (format "ls -1 -d %s" filespec
))))
424 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
428 (call-process gshell nil t nil
432 (call-process gshell nil t nil
435 (goto-char (point-min))
436 ;; Issue an error, if no match.
437 (unless (eq 0 status
)
439 (skip-chars-forward " \t\n\j")
440 (if (looking-at "ls:")
441 (viper-forward-Word 1))
446 (buffer-substring (point) (viper-line-pos 'end
)))
448 (goto-char (point-min))
449 (viper-get-filenames-from-buffer 'one-per-line
))
453 ;; Interpret the stuff in the buffer as a list of file names
454 ;; return a list of file names listed in the buffer beginning at point
455 ;; If optional arg is supplied, assume each filename is listed on a separate
457 (defun viper-get-filenames-from-buffer (&optional one-per-line
)
458 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
460 (skip-chars-forward skip-chars
)
462 (if (cond ((looking-at "\"")
464 (re-search-forward "[^\"]+" nil t
)) ; noerror
467 (re-search-forward "[^']+" nil t
)) ; noerror
470 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
472 (buffer-substring (match-beginning 0) (match-end 0))))
475 (skip-chars-forward " \t\n")
476 (setq result
(cons fname result
)))
479 ;; convert MS-DOS wildcards to regexp
480 (defun viper-wildcard-to-regexp (wcard)
482 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
485 (goto-char (point-min))
487 (skip-chars-forward "^*?.\\\\")
488 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
489 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
490 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
491 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
497 ;; glob windows files
498 ;; LIST is expected to be in reverse order
499 (defun viper-glob-mswindows-files (filespec)
500 (let ((case-fold-search t
)
503 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
506 (goto-char (point-min))
507 (setq tmp
(viper-get-filenames-from-buffer))
509 (setq tmp2
(cons (directory-files
510 ;; the directory part
511 (or (file-name-directory (car tmp
))
513 t
; return full names
514 ;; the regexp part: globs the file names
516 (viper-wildcard-to-regexp
517 (file-name-nondirectory (car tmp
)))
520 (setq tmp
(cdr tmp
)))
521 (reverse (apply 'append tmp2
)))))
526 ;; Rotate RING's index. DIRection can be positive or negative.
527 (defun viper-ring-rotate1 (ring dir
)
528 (if (and (ring-p ring
) (> (ring-length ring
) 0))
530 (setcar ring
(cond ((> dir
0)
531 (ring-plus1 (car ring
) (ring-length ring
)))
533 (ring-minus1 (car ring
) (ring-length ring
)))
534 ;; don't rotate if dir = 0
536 (viper-current-ring-item ring
)
539 (defun viper-special-ring-rotate1 (ring dir
)
540 (if (memq viper-intermediate-command
541 '(repeating-display-destructive-command
542 repeating-insertion-from-ring
))
543 (viper-ring-rotate1 ring dir
)
544 ;; don't rotate otherwise
545 (viper-ring-rotate1 ring
0)))
547 ;; current ring item; if N is given, then so many items back from the
549 (defun viper-current-ring-item (ring &optional n
)
551 (if (and (ring-p ring
) (> (ring-length ring
) 0))
552 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
554 ;; Push item onto ring. The second argument is a ring-variable, not value.
555 (defun viper-push-onto-ring (item ring-var
)
556 (or (ring-p (eval ring-var
))
557 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
558 (or (null item
) ; don't push nil
559 (and (stringp item
) (string= item
"")) ; or empty strings
560 (equal item
(viper-current-ring-item (eval ring-var
))) ; or old stuff
561 ;; Since viper-set-destructive-command checks if we are inside
562 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
563 ;; cmd viper-repeat makes a call to the current function only if `.' is
564 ;; executing a command from the command history. It doesn't call the
565 ;; push-onto-ring function if `.' is simply repeating the last
566 ;; destructive command. We only check for ESC (which happens when we do
567 ;; insert with a prefix argument, or if this-command-keys doesn't give
568 ;; anything meaningful (in that case we don't know what to show to the
570 (and (eq ring-var
'viper-command-ring
)
571 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
572 (viper-array-to-string (this-command-keys))))
573 (viper-ring-insert (eval ring-var
) item
))
577 ;; removing elts from ring seems to break it
578 (defun viper-cleanup-ring (ring)
579 (or (< (ring-length ring
) 2)
580 (null (viper-current-ring-item ring
))
581 ;; last and previous equal
582 (if (equal (viper-current-ring-item ring
)
583 (viper-current-ring-item ring
1))
584 (viper-ring-pop ring
))))
586 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
587 (defun viper-ring-pop (ring)
588 (let* ((ln (ring-length ring
))
589 (vec (cdr (cdr ring
)))
590 (veclen (length vec
))
592 (idx (max 0 (ring-minus1 hd ln
)))
593 (top-elt (aref vec idx
)))
596 (while (< (1+ idx
) veclen
)
597 (aset vec idx
(aref vec
(1+ idx
)))
601 (setq hd
(max 0 (ring-minus1 hd ln
)))
602 (if (= hd
(1- ln
)) (setq hd
0))
603 (setcar ring hd
) ; move head
604 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
608 (defun viper-ring-insert (ring item
)
609 (let* ((ln (ring-length ring
))
610 (vec (cdr (cdr ring
)))
611 (veclen (length vec
))
613 (vecpos-after-hd (if (= hd
0) ln hd
))
618 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
619 (setcar ring
(ring-plus1 hd ln
)))
620 (setcar (cdr ring
) (1+ ln
))
621 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
622 (while (and (>= idx vecpos-after-hd
) (> ln
0))
623 (aset vec idx
(aref vec
(1- idx
)))
625 (aset vec vecpos-after-hd item
))
631 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
632 ;; PRE-STRING is a string to prepend to the abbrev string.
633 ;; POST-STRING is a string to append to the abbrev string.
634 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
635 ;; if the orig string was truncated.
636 (defun viper-abbreviate-string (string max-len
637 pre-string post-string abbrev-sign
)
641 (substring string
0 (min max-len
(length string
)))))
642 (cond ((null truncated-str
) "")
643 ((> (length string
) max-len
)
645 pre-string truncated-str abbrev-sign post-string
))
646 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
648 ;; tells if we are over a whitespace-only line
649 (defsubst viper-over-whitespace-line
()
652 (looking-at "^[ \t]*$")))
655 ;;; Saving settings in custom file
657 ;; Save the current setting of VAR in CUSTOM-FILE.
658 ;; If given, MESSAGE is a message to be displayed after that.
659 ;; This message is erased after 2 secs, if erase-msg is non-nil.
660 ;; Arguments: var message custom-file &optional erase-message
661 (defun viper-save-setting (var message custom-file
&optional erase-msg
)
662 (let* ((var-name (symbol-name var
))
663 (var-val (if (boundp var
) (eval var
)))
664 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
665 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
667 (message "%s" (or message
""))
670 (goto-char (point-min))
671 (if (re-search-forward regexp nil t
)
672 (let ((reg-end (1- (match-end 0))))
673 (search-backward var-name
)
674 (delete-region (match-beginning 0) reg-end
)
675 (goto-char (match-beginning 0))
676 (insert (format "%s '%S" var-name var-val
)))
677 (goto-char (point-max))
678 (if (not (bolp)) (insert "\n"))
679 (insert (format "(setq %s '%S)\n" var-name var-val
)))
688 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
689 ;; match this pattern.
690 (defun viper-save-string-in-file (string custom-file
&optional pattern
)
691 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
694 (let (buffer-read-only)
695 (goto-char (point-min))
696 (if pattern
(delete-matching-lines pattern
))
697 (goto-char (point-max))
698 (if string
(insert string
))
704 ;; This is a simple-minded check for whether a file is under version control.
705 ;; If file,v exists but file doesn't, this file is considered to be not checked
706 ;; in and not checked out for the purpose of patching (since patch won't be
707 ;; able to read such a file anyway).
708 ;; FILE is a string representing file name
709 ;;(defun viper-file-under-version-control (file)
710 ;; (let* ((filedir (file-name-directory file))
711 ;; (file-nondir (file-name-nondirectory file))
712 ;; (trial (concat file-nondir ",v"))
713 ;; (full-trial (concat filedir trial))
714 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
715 ;; (and (stringp file)
716 ;; (file-exists-p file)
719 ;; (file-exists-p full-trial)
720 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
721 ;; ;; don't be fooled by this!
722 ;; (not (equal (file-attributes file)
723 ;; (file-attributes full-trial))))
724 ;; ;; check if a version is in RCS/ directory
725 ;; (file-exists-p full-rcs-trial)))
729 (defsubst viper-file-checked-in-p
(file)
730 (and (featurep 'vc-hooks
)
731 ;; CVS files are considered not checked in
732 ;; FIXME: Should this deal with more than CVS?
733 (not (memq (vc-backend file
) '(nil CVS
)))
734 (if (fboundp 'vc-state
)
736 (not (memq (vc-state file
) '(edited needs-merge
)))
737 (not (stringp (vc-state file
))))
738 ;; XEmacs has no vc-state
739 (if (featurep 'xemacs
) (not (vc-locking-user file
))))))
741 ;; checkout if visited file is checked in
742 (defun viper-maybe-checkout (buf)
743 (let ((file (expand-file-name (buffer-file-name buf
)))
744 (checkout-function (key-binding "\C-x\C-q")))
745 (if (and (viper-file-checked-in-p file
)
749 "File %s is checked in. Check it out? "
750 (viper-abbreviate-file-name file
))))
751 (with-current-buffer buf
752 (command-execute checkout-function
)))))
758 (defun viper-put-on-search-overlay (beg end
)
759 (if (viper-overlay-p viper-search-overlay
)
760 (viper-move-overlay viper-search-overlay beg end
)
761 (setq viper-search-overlay
(viper-make-overlay beg end
(current-buffer)))
763 viper-search-overlay
'priority viper-search-overlay-priority
))
764 (viper-overlay-put viper-search-overlay
'face viper-search-face
))
768 (defun viper-flash-search-pattern ()
769 (if (not (viper-has-face-support-p))
771 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
773 (viper-overlay-put viper-search-overlay
'face nil
)))
775 (defun viper-hide-search-overlay ()
776 (if (not (viper-overlay-p viper-search-overlay
))
778 (setq viper-search-overlay
779 (viper-make-overlay (point-min) (point-min) (current-buffer)))
781 viper-search-overlay
'priority viper-search-overlay-priority
)))
782 (viper-overlay-put viper-search-overlay
'face nil
))
786 (defsubst viper-move-replace-overlay
(beg end
)
787 (viper-move-overlay viper-replace-overlay beg end
))
789 (defun viper-set-replace-overlay (beg end
)
790 (if (viper-overlay-live-p viper-replace-overlay
)
791 (viper-move-replace-overlay beg end
)
792 (setq viper-replace-overlay
(viper-make-overlay beg end
(current-buffer)))
795 viper-replace-overlay
(if (featurep 'emacs
) 'evaporate
'detachable
) nil
)
797 viper-replace-overlay
'priority viper-replace-overlay-priority
)
798 ;; If Emacs will start supporting overlay maps, as it currently supports
799 ;; text-property maps, we could do away with viper-replace-minor-mode and
800 ;; just have keymap attached to replace overlay.
802 ;; viper-replace-overlay
803 ;; (if (featurep 'xemacs) 'keymap 'local-map)
804 ;; viper-replace-map)
806 (if (viper-has-face-support-p)
808 viper-replace-overlay
'face viper-replace-overlay-face
))
809 (viper-save-cursor-color 'before-replace-mode
)
810 (viper-change-cursor-color
811 (viper-frame-value viper-replace-overlay-cursor-color
)))
814 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph
)
815 (or (viper-overlay-live-p viper-replace-overlay
)
816 (viper-set-replace-overlay (point-min) (point-min)))
817 (if (or (not (viper-has-face-support-p))
818 viper-use-replace-region-delimiters
)
819 (let ((before-name (if (featurep 'xemacs
) 'begin-glyph
'before-string
))
820 (after-name (if (featurep 'xemacs
) 'end-glyph
'after-string
)))
821 (viper-overlay-put viper-replace-overlay before-name before-glyph
)
822 (viper-overlay-put viper-replace-overlay after-name after-glyph
))))
824 (defun viper-hide-replace-overlay ()
825 (viper-set-replace-overlay-glyphs nil nil
)
826 (viper-restore-cursor-color 'after-replace-mode
)
827 (viper-restore-cursor-color 'after-insert-mode
)
828 (if (viper-has-face-support-p)
829 (viper-overlay-put viper-replace-overlay
'face nil
)))
832 (defsubst viper-replace-start
()
833 (viper-overlay-start viper-replace-overlay
))
834 (defsubst viper-replace-end
()
835 (viper-overlay-end viper-replace-overlay
))
840 (defun viper-set-minibuffer-overlay ()
841 (viper-check-minibuffer-overlay)
842 (when (viper-has-face-support-p)
844 viper-minibuffer-overlay
'face viper-minibuffer-current-face
)
846 viper-minibuffer-overlay
'priority viper-minibuffer-overlay-priority
)
849 viper-minibuffer-overlay
850 (if (featurep 'emacs
) 'evaporate
'detachable
)
852 ;; make viper-minibuffer-overlay open-ended
853 ;; In emacs, it is made open ended at creation time
854 (when (featurep 'xemacs
)
855 (viper-overlay-put viper-minibuffer-overlay
'start-open nil
)
856 (viper-overlay-put viper-minibuffer-overlay
'end-open nil
))))
858 (defun viper-check-minibuffer-overlay ()
859 (if (viper-overlay-live-p viper-minibuffer-overlay
)
861 viper-minibuffer-overlay
862 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
864 (setq viper-minibuffer-overlay
865 (if (featurep 'xemacs
)
866 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
867 ;; make overlay open-ended
869 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
871 (current-buffer) nil
'rear-advance
)))))
874 (defsubst viper-is-in-minibuffer
()
876 (string-match "\*Minibuf-" (buffer-name))))
880 ;;; XEmacs compatibility
882 (defun viper-abbreviate-file-name (file)
883 (if (featurep 'xemacs
)
884 (abbreviate-file-name file t
) ; XEmacs requires addl argument
885 (abbreviate-file-name file
)))
887 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
888 ;; in sit-for, so this function smoothes out the differences.
889 (defsubst viper-sit-for-short
(val &optional nodisp
)
890 (sit-for (/ val
1000.0) nodisp
))
892 ;; EVENT may be a single event of a sequence of events
893 (defsubst viper-ESC-event-p
(event)
894 (let ((ESC-keys '(?\e
(control \
[) escape
))
895 (key (viper-event-key event
)))
896 (member key ESC-keys
)))
898 ;; checks if object is a marker, has a buffer, and points to within that buffer
899 (defun viper-valid-marker (marker)
900 (if (and (markerp marker
) (marker-buffer marker
))
901 (let ((buf (marker-buffer marker
))
902 (pos (marker-position marker
)))
905 (and (<= pos
(point-max)) (<= (point-min) pos
))))))
907 (defsubst viper-mark-marker
()
908 (if (featurep 'xemacs
) (mark-marker t
)
911 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
912 ;; is the same as (mark t).
913 (defsubst viper-set-mark-if-necessary
()
914 (setq mark-ring
(delete (viper-mark-marker) mark-ring
))
915 (set-mark-command nil
)
916 (setq viper-saved-mark
(point)))
918 ;; In transient mark mode (zmacs mode), it is annoying when regions become
919 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
920 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
921 (defun viper-deactivate-mark ()
922 (if (featurep 'xemacs
)
923 (zmacs-deactivate-region)
926 (defsubst viper-leave-region-active
()
927 (if (featurep 'xemacs
) (setq zmacs-region-stays t
)))
929 ;; Check if arg is a valid character for register
930 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
931 ;; Letter means lowercase letters, Letter means uppercase letters, and
932 ;; digit means digits from 1 to 9.
933 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
934 (defun viper-valid-register (reg &optional type
)
935 (or type
(setq type
'(letter Letter digit
)))
936 (or (if (memq 'letter type
)
937 (and (<= ?a reg
) (<= reg ?z
)))
938 (if (memq 'digit type
)
939 (and (<= ?
1 reg
) (<= reg ?
9)))
940 (if (memq 'Letter type
)
941 (and (<= ?A reg
) (<= reg ?Z
)))
946 ;; it is suggested that an event must be copied before it is assigned to
947 ;; last-command-event in XEmacs
948 (defun viper-copy-event (event)
949 (if (featurep 'xemacs
) (copy-event event
)
952 ;; Uses different timeouts for ESC-sequences and others
953 (defsubst viper-fast-keysequence-p
()
954 (not (viper-sit-for-short
955 (if (viper-ESC-event-p last-input-event
)
956 viper-ESC-keyseq-timeout
957 viper-fast-keyseq-timeout
)
960 ;; like read-event, but in XEmacs also try to convert to char, if possible
961 (defun viper-read-event-convert-to-char ()
963 (if (featurep 'xemacs
)
965 (setq event
(next-command-event))
966 (or (event-to-character event
)
970 ;; Viperized read-key-sequence
971 (defun viper-read-key-sequence (prompt &optional continue-echo
)
972 (let (inhibit-quit event keyseq
)
973 (setq keyseq
(read-key-sequence prompt continue-echo
))
974 (setq event
(if (featurep 'xemacs
)
975 (elt keyseq
0) ; XEmacs returns vector of events
976 (elt (listify-key-sequence keyseq
) 0)))
977 (if (viper-ESC-event-p event
)
978 (let (unread-command-events)
979 (if (viper-fast-keysequence-p)
980 (let ((viper-vi-global-user-minor-mode nil
)
981 (viper-vi-local-user-minor-mode nil
)
982 (viper-vi-intercept-minor-mode nil
)
983 (viper-insert-intercept-minor-mode nil
)
984 (viper-replace-minor-mode nil
) ; actually unnecessary
985 (viper-insert-global-user-minor-mode nil
)
986 (viper-insert-local-user-minor-mode nil
))
987 ;; Note: set unread-command-events only after testing for fast
988 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
989 ;; always t -- whether there is anything after ESC or not
990 (viper-set-unread-command-events keyseq
)
991 (setq keyseq
(read-key-sequence nil
)))
992 (viper-set-unread-command-events keyseq
)
993 (setq keyseq
(read-key-sequence nil
)))))
997 ;; This function lets function-key-map convert key sequences into logical
998 ;; keys. This does a better job than viper-read-event when it comes to kbd
999 ;; macros, since it enables certain macros to be shared between X and TTY modes
1000 ;; by correctly mapping key sequences for Left/Right/... (on an ascii
1001 ;; terminal) into logical keys left, right, etc.
1002 (defun viper-read-key ()
1003 (let ((overriding-local-map viper-overriding-map
)
1006 (use-global-map viper-overriding-map
)
1008 (setq key
(elt (viper-read-key-sequence nil
) 0))
1009 (use-global-map global-map
))
1013 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
1014 ;; instead of nil, if '(nil) was previously inadvertently assigned to
1015 ;; unread-command-events
1016 (defun viper-event-key (event)
1017 (or (and event
(eventp event
))
1018 (error "viper-event-key: Wrong type argument, eventp, %S" event
))
1019 (when (if (featurep 'xemacs
)
1020 (or (key-press-event-p event
) (mouse-event-p event
)) ; xemacs
1023 (let ((mod (event-modifiers event
))
1026 (if (featurep 'xemacs
)
1028 (cond ((key-press-event-p event
)
1030 ((button-event-p event
)
1031 (concat "mouse-" (prin1-to-string (event-button event
))))
1033 (error "viper-event-key: Unknown event, %S" event
)))
1034 ;; Emacs doesn't handle capital letters correctly, since
1035 ;; \S-a isn't considered the same as A (it behaves as
1036 ;; plain `a' instead). So we take care of this here
1037 (cond ((and (viper-characterp event
) (<= ?A event
) (<= event ?Z
))
1040 ;; Emacs has the oddity whereby characters 128+char
1041 ;; represent M-char *if* this appears inside a string.
1042 ;; So, we convert them manually to (meta char).
1043 ((and (viper-characterp event
)
1044 (< ?\C-? event
) (<= event
255))
1046 event
(- event ?\C-?
1)))
1047 ((and (null mod
) (eq event
'return
))
1049 ((and (null mod
) (eq event
'space
))
1051 ((and (null mod
) (eq event
'delete
))
1053 ((and (null mod
) (eq event
'backspace
))
1055 (t (event-basic-type event
)))
1056 ) ; (featurep 'xemacs)
1058 (if (viper-characterp basis
)
1060 (if (viper= basis ?\C-?
)
1061 (list 'control
'\?) ; taking care of an emacs bug
1062 (intern (char-to-string basis
)))))
1064 (append mod
(list basis
))
1067 (defun viper-key-to-emacs-key (key)
1068 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
1069 (cond ((featurep 'xemacs
) key
)
1072 (setq key-name
(symbol-name key
))
1073 (cond ((= (length key-name
) 1) ; character event
1074 (string-to-char key-name
))
1075 ;; Emacs doesn't recognize `return' and `escape' as events on
1076 ;; dumb terminals, so we translate them into characters
1077 ((and (featurep 'emacs
) (not (viper-window-display-p))
1078 (string= key-name
"return"))
1080 ((and (featurep 'emacs
) (not (viper-window-display-p))
1081 (string= key-name
"escape"))
1083 ;; pass symbol-event as is
1087 (setq modifiers
(viper-subseq key
0 (1- (length key
)))
1088 base-key
(viper-seq-last-elt key
)
1089 base-key-name
(symbol-name base-key
)
1090 char-p
(= (length base-key-name
) 1))
1093 '(lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1097 (car (read-from-string
1100 (mapconcat 'identity mod-char-list
"-\\")
1106 (mapconcat 'identity mod-char-list
"-")
1112 ;; LIS is assumed to be a list of events of characters
1113 (defun viper-eventify-list-xemacs (lis)
1114 (if (featurep 'xemacs
)
1117 (cond ((viper-characterp elt
) (character-to-event elt
))
1120 "viper-eventify-list-xemacs: can't convert to event, %S"
1125 ;; Smoothes out the difference between Emacs' unread-command-events
1126 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1127 ;; events or a sequence of keys.
1129 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1130 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1131 ;; into an event. Below, we delete nil from event lists, since nil is the most
1132 ;; common symbol that might appear in this wrong context.
1133 (defun viper-set-unread-command-events (arg)
1134 (if (featurep 'emacs
)
1136 unread-command-events
1138 (cond ((eventp arg
) (list arg
))
1141 (listify-key-sequence arg
))
1143 "viper-set-unread-command-events: Invalid argument, %S"
1145 (if (not (eventp nil
))
1146 (setq new-events
(delq nil new-events
)))
1147 (append new-events unread-command-events
)))
1150 unread-command-events
1152 (cond ((viper-characterp arg
) (list (character-to-event arg
)))
1153 ((eventp arg
) (list arg
))
1154 ((stringp arg
) (mapcar 'character-to-event arg
))
1155 ((vectorp arg
) (append arg nil
)) ; turn into list
1156 ((listp arg
) (viper-eventify-list-xemacs arg
))
1158 "viper-set-unread-command-events: Invalid argument, %S" arg
)))
1159 unread-command-events
))))
1162 ;; Check if vec is a vector of key-press events representing characters
1164 (defun viper-event-vector-p (vec)
1166 (eval (cons 'and
(mapcar '(lambda (elt) (if (eventp elt
) t
)) vec
)))))
1169 ;; check if vec is a vector of character symbols
1170 (defun viper-char-symbol-sequence-p (vec)
1175 (mapcar (lambda (elt)
1176 (and (symbolp elt
) (= (length (symbol-name elt
)) 1)))
1180 (defun viper-char-array-p (array)
1181 (eval (cons 'and
(mapcar 'viper-characterp array
))))
1184 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1185 ;; convert events to keys and, if all keys are regular printable
1186 ;; characters, will return a string. Otherwise, will return a string
1187 ;; representing a vector of converted events. If the input was a Viper macro,
1188 ;; will return a string that represents this macro as a vector.
1189 (defun viper-array-to-string (event-seq)
1191 (cond ((stringp event-seq
) event-seq
)
1192 ((viper-event-vector-p event-seq
)
1193 (setq temp
(mapcar 'viper-event-key event-seq
))
1194 (cond ((viper-char-symbol-sequence-p temp
)
1195 (mapconcat 'symbol-name temp
""))
1196 ((and (viper-char-array-p
1197 (setq temp2
(mapcar 'viper-key-to-character temp
))))
1198 (mapconcat 'char-to-string temp2
""))
1199 (t (prin1-to-string (vconcat temp
)))))
1200 ((viper-char-symbol-sequence-p event-seq
)
1201 (mapconcat 'symbol-name event-seq
""))
1202 ((and (vectorp event-seq
)
1204 (setq temp
(mapcar 'viper-key-to-character event-seq
))))
1205 (mapconcat 'char-to-string temp
""))
1206 (t (prin1-to-string event-seq
)))))
1208 (defun viper-key-press-events-to-chars (events)
1209 (mapconcat (if (featurep 'xemacs
)
1210 (lambda (elt) (char-to-string (event-to-character elt
))) ; xemacs
1211 'char-to-string
; emacs
1217 (defun viper-read-char-exclusive ()
1219 (echo-keystrokes 1))
1222 (setq char
(read-char))
1224 ;; skip event if not char
1225 (viper-read-event))))
1228 ;; key is supposed to be in viper's representation, e.g., (control l), a
1230 (defun viper-key-to-character (key)
1231 (cond ((eq key
'space
) ?\
)
1232 ((eq key
'delete
) ?\C-?
)
1233 ((eq key
'return
) ?\C-m
)
1234 ((eq key
'backspace
) ?\C-h
)
1236 (= 1 (length (symbol-name key
))))
1237 (string-to-char (symbol-name key
)))
1239 (eq (car key
) 'control
)
1240 (symbol-name (nth 1 key
))
1241 (= 1 (length (symbol-name (nth 1 key
)))))
1242 (read (format "?\\C-%s" (symbol-name (nth 1 key
)))))
1246 (defun viper-setup-master-buffer (&rest other-files-or-buffers
)
1247 "Set up the current buffer as a master buffer.
1248 Arguments become related buffers. This function should normally be used in
1249 the `Local variables' section of a file."
1250 (setq viper-related-files-and-buffers-ring
1251 (make-ring (1+ (length other-files-or-buffers
))))
1252 (mapc '(lambda (elt)
1253 (viper-ring-insert viper-related-files-and-buffers-ring elt
))
1254 other-files-or-buffers
)
1255 (viper-ring-insert viper-related-files-and-buffers-ring
(buffer-name))
1258 ;;; Movement utilities
1260 ;; Characters that should not be considered as part of the word, in reformed-vi
1262 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1263 ;; into characters at some point and then put back to string. The result is
1264 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1265 ;; effect of quoting - and preventing it from being special.
1266 (defconst viper-non-word-characters-reformed-vi
1267 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1268 ;; These are characters that are not to be considered as parts of a word in
1270 ;; Set each time state changes and at loading time
1271 (viper-deflocalvar viper-non-word-characters nil
)
1273 ;; must be buffer-local
1274 (viper-deflocalvar viper-ALPHA-char-class
"w"
1275 "String of syntax classes characterizing Viper's alphanumeric symbols.
1276 In addition, the symbol `_' may be considered alphanumeric if
1277 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1279 (defconst viper-strict-ALPHA-chars
"a-zA-Z0-9_"
1280 "Regexp matching the set of alphanumeric characters acceptable to strict
1282 (defconst viper-strict-SEP-chars
" \t\n"
1283 "Regexp matching the set of alphanumeric characters acceptable to strict
1285 (defconst viper-strict-SEP-chars-sans-newline
" \t"
1286 "Regexp matching the set of alphanumeric characters acceptable to strict
1289 (defconst viper-SEP-char-class
" -"
1290 "String of syntax classes for Vi separators.
1291 Usually contains ` ', linefeed, TAB or formfeed.")
1294 ;; Set Viper syntax classes and related variables according to
1295 ;; `viper-syntax-preference'.
1296 (defun viper-update-syntax-classes (&optional set-default
)
1297 (let ((preference (cond ((eq viper-syntax-preference
'emacs
)
1298 "w") ; Viper words have only Emacs word chars
1299 ((eq viper-syntax-preference
'extended
)
1300 "w_") ; Viper words have Emacs word & symbol chars
1301 (t "w"))) ; Viper words are Emacs words plus `_'
1302 (non-word-chars (cond ((eq viper-syntax-preference
'reformed-vi
)
1303 (viper-string-to-list
1304 viper-non-word-characters-reformed-vi
))
1307 (setq-default viper-ALPHA-char-class preference
1308 viper-non-word-characters non-word-chars
)
1309 (setq viper-ALPHA-char-class preference
1310 viper-non-word-characters non-word-chars
))
1313 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1314 ;; is `nil'. If nil, use setq.
1315 (defun viper-set-syntax-preference (&optional symbol value
)
1316 "Set Viper syntax preference.
1317 If called interactively or if SYMBOL is nil, sets syntax preference in current
1318 buffer. If called non-interactively, preferably via the customization widget,
1319 sets the default value."
1324 "Viper syntax preference: "
1325 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1326 nil
'require-match
)))
1327 (if (stringp value
) (setq value
(intern value
)))
1328 (or (memq value
'(strict-vi reformed-vi extended emacs
))
1329 (error "Invalid Viper syntax preference, %S" value
))
1331 (setq-default viper-syntax-preference value
)
1332 (setq viper-syntax-preference value
))
1333 (viper-update-syntax-classes))
1335 (defcustom viper-syntax-preference
'reformed-vi
1336 "*Syntax type characterizing Viper's alphanumeric symbols.
1337 Affects movement and change commands that deal with Vi-style words.
1338 Works best when set in the hooks to various major modes.
1340 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1342 `reformed-vi' means Viper words are like Emacs words \(as determined using
1343 Emacs syntax tables, which are different for different major modes\) with two
1344 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1345 symbols, such as `,',:,\",),{, etc., are excluded.
1346 This behaves very close to `strict-vi', but also works well with non-ASCII
1347 characters from various alphabets.
1349 `extended' means Viper word constituents are symbols that are marked as being
1350 parts of words OR symbols in Emacs syntax tables.
1351 This is most appropriate for major modes intended for editing programs.
1353 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1355 This option is appropriate if you like Emacs-style words."
1356 :type
'(radio (const strict-vi
) (const reformed-vi
)
1357 (const extended
) (const emacs
))
1358 :set
'viper-set-syntax-preference
1360 (make-variable-buffer-local 'viper-syntax-preference
)
1363 ;; addl-chars are characters to be temporarily considered as alphanumerical
1364 (defun viper-looking-at-alpha (&optional addl-chars
)
1365 (or (stringp addl-chars
) (setq addl-chars
""))
1366 (if (eq viper-syntax-preference
'reformed-vi
)
1367 (setq addl-chars
(concat addl-chars
"_")))
1368 (let ((char (char-after (point))))
1370 (if (eq viper-syntax-preference
'strict-vi
)
1371 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars
"]"))
1373 ;; or one of the additional chars being asked to include
1374 (viper-memq-char char
(viper-string-to-list addl-chars
))
1376 ;; not one of the excluded word chars (note:
1377 ;; viper-non-word-characters is a list)
1378 (not (viper-memq-char char viper-non-word-characters
))
1379 ;; char of the Viper-word syntax class
1380 (viper-memq-char (char-syntax char
)
1381 (viper-string-to-list viper-ALPHA-char-class
))))))
1384 (defun viper-looking-at-separator ()
1385 (let ((char (char-after (point))))
1387 (if (eq viper-syntax-preference
'strict-vi
)
1388 (viper-memq-char char
(viper-string-to-list viper-strict-SEP-chars
))
1389 (or (eq char ?
\n) ; RET is always a separator in Vi
1390 (viper-memq-char (char-syntax char
)
1391 (viper-string-to-list viper-SEP-char-class
)))))
1394 (defsubst viper-looking-at-alphasep
(&optional addl-chars
)
1395 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars
)))
1397 (defun viper-skip-alpha-forward (&optional addl-chars
)
1398 (or (stringp addl-chars
) (setq addl-chars
""))
1401 (cond ((eq viper-syntax-preference
'strict-vi
)
1403 (t viper-ALPHA-char-class
))
1404 (cond ((eq viper-syntax-preference
'strict-vi
)
1405 (concat viper-strict-ALPHA-chars addl-chars
))
1408 (defun viper-skip-alpha-backward (&optional addl-chars
)
1409 (or (stringp addl-chars
) (setq addl-chars
""))
1412 (cond ((eq viper-syntax-preference
'strict-vi
)
1414 (t viper-ALPHA-char-class
))
1415 (cond ((eq viper-syntax-preference
'strict-vi
)
1416 (concat viper-strict-ALPHA-chars addl-chars
))
1419 ;; weird syntax tables may confuse strict-vi style
1420 (defsubst viper-skip-all-separators-forward
(&optional within-line
)
1421 (if (eq viper-syntax-preference
'strict-vi
)
1423 (skip-chars-forward viper-strict-SEP-chars-sans-newline
)
1424 (skip-chars-forward viper-strict-SEP-chars
))
1425 (viper-skip-syntax 'forward
1426 viper-SEP-char-class
1427 (or within-line
"\n")
1428 (if within-line
(viper-line-pos 'end
)))))
1430 (defsubst viper-skip-all-separators-backward
(&optional within-line
)
1431 (if (eq viper-syntax-preference
'strict-vi
)
1433 (skip-chars-backward viper-strict-SEP-chars-sans-newline
)
1434 (skip-chars-backward viper-strict-SEP-chars
))
1435 (viper-skip-syntax 'backward
1436 viper-SEP-char-class
1437 (or within-line
"\n")
1438 (if within-line
(viper-line-pos 'start
)))))
1439 (defun viper-skip-nonseparators (direction)
1442 (concat "^" viper-SEP-char-class
)
1444 (viper-line-pos (if (eq direction
'forward
) 'end
'start
))))
1447 ;; skip over non-word constituents and non-separators
1448 (defun viper-skip-nonalphasep-forward ()
1449 (if (eq viper-syntax-preference
'strict-vi
)
1451 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1454 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1455 ;; Emacs may consider some of these as words, but we don't want them
1456 viper-non-word-characters
1457 (viper-line-pos 'end
))))
1459 (defun viper-skip-nonalphasep-backward ()
1460 (if (eq viper-syntax-preference
'strict-vi
)
1461 (skip-chars-backward
1462 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1465 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1466 ;; Emacs may consider some of these as words, but we don't want them
1467 viper-non-word-characters
1468 (viper-line-pos 'start
))))
1470 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1471 ;; Return the number of chars traveled.
1472 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1473 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1474 ;; words, even if Emacs syntax table says they are.
1475 (defun viper-skip-syntax (direction syntax addl-chars
&optional limit
)
1479 (if (eq direction
'forward
)
1480 'skip-chars-forward
'skip-chars-backward
))
1482 (if (eq direction
'forward
)
1483 'viper-forward-char-carefully
'viper-backward-char-carefully
))
1484 char-looked-at syntax-of-char-looked-at negated-syntax
)
1486 (cond ((listp addl-chars
) (viper-charlist-to-string addl-chars
))
1487 ((stringp addl-chars
) addl-chars
)
1490 (cond ((listp syntax
) syntax
)
1491 ((stringp syntax
) (viper-string-to-list syntax
))
1493 (if (memq ?^ syntax
) (setq negated-syntax t
))
1495 (while (and (not (= local
0))
1496 (cond ((eq direction
'forward
)
1499 (setq char-looked-at
(viper-char-at-pos direction
)
1500 ;; if outside the range, set to nil
1501 syntax-of-char-looked-at
(if char-looked-at
1502 (char-syntax char-looked-at
)))
1505 (cond ((and limit
(eq direction
'forward
))
1507 (limit ; backward & limit
1510 ;; char under/before cursor has appropriate syntax
1512 (not (memq syntax-of-char-looked-at syntax
))
1513 (memq syntax-of-char-looked-at syntax
))
1514 ;; if char-syntax class is "word", make sure it is not one
1515 ;; of the excluded characters
1516 (if (and (eq syntax-of-char-looked-at ?w
)
1517 (not negated-syntax
))
1518 (not (viper-memq-char
1519 char-looked-at viper-non-word-characters
))
1521 (funcall skip-syntax-func
1)
1523 (funcall skip-chars-func addl-chars limit
)))
1524 (setq total
(+ total local
)))
1528 ;; tells when point is at the beginning of field
1529 (defun viper-beginning-of-field ()
1531 (not (eq (get-char-property (point) 'field
)
1532 (get-char-property (1- (point)) 'field
)))))
1535 ;; this is copied from cl-extra.el
1536 ;; Return the subsequence of SEQ from START to END.
1537 ;; If END is omitted, it defaults to the length of the sequence.
1538 ;; If START or END is negative, it counts from the end.
1539 (defun viper-subseq (seq start
&optional end
)
1540 (if (stringp seq
) (substring seq start end
)
1542 (and end
(< end
0) (setq end
(+ end
(setq len
(length seq
)))))
1543 (if (< start
0) (setq start
(+ start
(or len
(setq len
(length seq
))))))
1545 (if (> start
0) (setq seq
(nthcdr start seq
)))
1548 (while (>= (setq end
(1- end
)) start
)
1549 (push (pop seq
) res
))
1551 (copy-sequence seq
)))
1553 (or end
(setq end
(or len
(length seq
))))
1554 (let ((res (make-vector (max (- end start
) 0) nil
))
1556 (while (< start end
)
1557 (aset res i
(aref seq start
))
1558 (setq i
(1+ i
) start
(1+ start
)))
1563 ;;; Local Variables:
1564 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1567 ;; arch-tag: 7f023fd5-dd9e-4378-a397-9c179553b0e3
1568 ;;; viper-util.el ends here