1 ;;; viper-util.el --- Utilities used by viper.el
3 ;; Copyright (C) 1994, 95, 96, 97, 99, 2000, 01, 02, 2005 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
29 (defvar viper-overriding-map
)
30 (defvar pm-color-alist
)
31 (defvar zmacs-region-stays
)
32 (defvar viper-minibuffer-current-face
)
33 (defvar viper-minibuffer-insert-face
)
34 (defvar viper-minibuffer-vi-face
)
35 (defvar viper-minibuffer-emacs-face
)
36 (defvar viper-replace-overlay-face
)
37 (defvar viper-fast-keyseq-timeout
)
38 (defvar ex-unix-type-shell
)
39 (defvar ex-unix-type-shell-options
)
40 (defvar viper-ex-tmp-buf-name
)
41 (defvar viper-syntax-preference
)
42 (defvar viper-saved-mark
)
49 (let ((load-path (cons (expand-file-name ".") load-path
)))
50 (or (featurep 'viper-init
)
51 (load "viper-init.el" nil nil
'nosuffix
))
58 ;; A fix for NeXT Step
59 ;; Should go away, when NS people fix the design flaw, which leaves the
60 ;; two x-* functions undefined.
61 (if (and (not (fboundp 'x-display-color-p
)) (fboundp 'ns-display-color-p
))
62 (fset 'x-display-color-p
(symbol-function 'ns-display-color-p
)))
63 (if (and (not (fboundp 'x-color-defined-p
)) (fboundp 'ns-color-defined-p
))
64 (fset 'x-color-defined-p
(symbol-function 'ns-color-defined-p
)))
70 (viper-cond-compile-for-xemacs-or-emacs
72 (fset 'viper-overlay-p
(symbol-function 'extentp
))
73 (fset 'viper-make-overlay
(symbol-function 'make-extent
))
74 (fset 'viper-overlay-live-p
(symbol-function 'extent-live-p
))
75 (fset 'viper-move-overlay
(symbol-function 'set-extent-endpoints
))
76 (fset 'viper-overlay-start
(symbol-function 'extent-start-position
))
77 (fset 'viper-overlay-end
(symbol-function 'extent-end-position
))
78 (fset 'viper-overlay-get
(symbol-function 'extent-property
))
79 (fset 'viper-overlay-put
(symbol-function 'set-extent-property
))
80 (fset 'viper-read-event
(symbol-function 'next-command-event
))
81 (fset 'viper-characterp
(symbol-function 'characterp
))
82 (fset 'viper-int-to-char
(symbol-function 'int-to-char
))
83 (if (viper-window-display-p)
84 (fset 'viper-iconify
(symbol-function 'iconify-frame
)))
85 (cond ((viper-has-face-support-p)
86 (fset 'viper-get-face
(symbol-function 'get-face
))
87 (fset 'viper-color-defined-p
(symbol-function 'valid-color-name-p
))
90 (fset 'viper-overlay-p
(symbol-function 'overlayp
))
91 (fset 'viper-make-overlay
(symbol-function 'make-overlay
))
92 (fset 'viper-overlay-live-p
(symbol-function 'overlayp
))
93 (fset 'viper-move-overlay
(symbol-function 'move-overlay
))
94 (fset 'viper-overlay-start
(symbol-function 'overlay-start
))
95 (fset 'viper-overlay-end
(symbol-function 'overlay-end
))
96 (fset 'viper-overlay-get
(symbol-function 'overlay-get
))
97 (fset 'viper-overlay-put
(symbol-function 'overlay-put
))
98 (fset 'viper-read-event
(symbol-function 'read-event
))
99 (fset 'viper-characterp
(symbol-function 'integerp
))
100 (fset 'viper-int-to-char
(symbol-function 'identity
))
101 (if (viper-window-display-p)
102 (fset 'viper-iconify
(symbol-function 'iconify-or-deiconify-frame
)))
103 (cond ((viper-has-face-support-p)
104 (fset 'viper-get-face
(symbol-function 'internal-get-face
))
105 (fset 'viper-color-defined-p
(symbol-function 'x-color-defined-p
))
111 ;; CHAR is supposed to be a char or an integer (positive or negative)
112 ;; LIST is a list of chars, nil, and negative numbers
113 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
114 ;; Introduced for compatibility with XEmacs, where integers are not the same as
116 (defun viper-memq-char (char list
)
117 (cond ((and (integerp char
) (>= char
0))
118 (memq (viper-int-to-char char
) list
))
121 ;; Check if char-or-int and char are the same as characters
122 (defun viper-char-equal (char-or-int char
)
123 (cond ((and (integerp char-or-int
) (>= char-or-int
0))
124 (= (viper-int-to-char char-or-int
) char
))
125 ((eq char-or-int char
))))
127 ;; Like =, but accommodates null and also is t for eq-objects
128 (defun viper= (char char1
)
129 (cond ((eq char char1
) t
)
130 ((and (viper-characterp char
) (viper-characterp char1
))
134 (defsubst viper-color-display-p
()
135 (viper-cond-compile-for-xemacs-or-emacs
136 (eq (device-class (selected-device)) 'color
) ; xemacs
137 (x-display-color-p) ; emacs
140 (defsubst viper-get-cursor-color
()
141 (viper-cond-compile-for-xemacs-or-emacs
143 (color-instance-name (frame-property (selected-frame) 'cursor-color
))
144 (cdr (assoc 'cursor-color
(frame-parameters))) ; emacs
149 (cond ((eq (viper-device-type) 'pm
)
150 (fset 'viper-color-defined-p
151 (lambda (color) (assoc color pm-color-alist
)))))
155 (defun viper-change-cursor-color (new-color)
156 (if (and (viper-window-display-p) (viper-color-display-p)
157 (stringp new-color
) (viper-color-defined-p new-color
)
158 (not (string= new-color
(viper-get-cursor-color))))
159 (viper-cond-compile-for-xemacs-or-emacs
161 (selected-frame) 'cursor-color
(make-color-instance new-color
))
162 (modify-frame-parameters
163 (selected-frame) (list (cons 'cursor-color new-color
)))
167 ;; By default, saves current frame cursor color in the
168 ;; viper-saved-cursor-color-in-replace-mode property of viper-replace-overlay
169 (defun viper-save-cursor-color (before-which-mode)
170 (if (and (viper-window-display-p) (viper-color-display-p))
171 (let ((color (viper-get-cursor-color)))
172 (if (and (stringp color
) (viper-color-defined-p color
)
173 (not (string= color viper-replace-overlay-cursor-color
)))
174 (modify-frame-parameters
178 (if (eq before-which-mode
'before-replace-mode
)
179 'viper-saved-cursor-color-in-replace-mode
180 'viper-saved-cursor-color-in-insert-mode
)
185 (defsubst viper-get-saved-cursor-color-in-replace-mode
()
188 (if viper-emacs-p
'frame-parameter
'frame-property
)
190 'viper-saved-cursor-color-in-replace-mode
)
191 viper-vi-state-cursor-color
))
193 (defsubst viper-get-saved-cursor-color-in-insert-mode
()
196 (if viper-emacs-p
'frame-parameter
'frame-property
)
198 'viper-saved-cursor-color-in-insert-mode
)
199 viper-vi-state-cursor-color
))
201 ;; restore cursor color from replace overlay
202 (defun viper-restore-cursor-color(after-which-mode)
203 (if (viper-overlay-p viper-replace-overlay
)
204 (viper-change-cursor-color
205 (if (eq after-which-mode
'after-replace-mode
)
206 (viper-get-saved-cursor-color-in-replace-mode)
207 (viper-get-saved-cursor-color-in-insert-mode))
211 ;; Check the current version against the major and minor version numbers
212 ;; using op: cur-vers op major.minor If emacs-major-version or
213 ;; emacs-minor-version are not defined, we assume that the current version
214 ;; is hopelessly outdated. We assume that emacs-major-version and
215 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
216 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
217 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
218 ;; incorrect. However, this gives correct result in our cases, since we are
219 ;; testing for sufficiently high Emacs versions.
220 (defun viper-check-version (op major minor
&optional type-of-emacs
)
221 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
222 (and (cond ((eq type-of-emacs
'xemacs
) viper-xemacs-p
)
223 ((eq type-of-emacs
'emacs
) viper-emacs-p
)
225 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
226 (= emacs-major-version major
)))
227 ((memq op
'(> >= < <=))
228 (and (or (funcall op emacs-major-version major
)
229 (= emacs-major-version major
))
230 (if (= emacs-major-version major
)
231 (funcall op emacs-minor-version minor
)
234 (error "%S: Invalid op in viper-check-version" op
))))
235 (cond ((memq op
'(= > >=)) nil
)
236 ((memq op
'(< <=)) t
))))
239 (defun viper-get-visible-buffer-window (wind)
241 (get-buffer-window wind t
)
242 (get-buffer-window wind
'visible
)))
245 ;; Return line position.
246 ;; If pos is 'start then returns position of line start.
247 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
248 ;; Pos = 'indent returns beginning of indentation.
249 ;; Otherwise, returns point. Current point is not moved in any case."
250 (defun viper-line-pos (pos)
251 (let ((cur-pos (point))
259 (goto-char (+ (viper-line-pos 'start
) (viper-line-pos 'end
) 2)))
261 (back-to-indentation))
263 (setq result
(point))
267 ;; Emacs used to count each multibyte character as several positions in the buffer,
268 ;; so we had to use Emacs' chars-in-region to count characters. Since 20.3,
269 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
270 ;; counting each char as just one pos. So, now we can simply subtract beg from
271 ;; end to determine the number of characters in a region.
272 (defun viper-chars-in-region (beg end
&optional preserve-sign
)
273 ;;(let ((count (abs (if (fboundp 'chars-in-region)
274 ;; (chars-in-region beg end)
276 (let ((count (abs (- end beg
))))
277 (if (and (< end beg
) preserve-sign
)
281 ;; Test if POS is between BEG and END
282 (defsubst viper-pos-within-region
(pos beg end
)
283 (and (>= pos
(min beg end
)) (>= (max beg end
) pos
)))
286 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
287 ;; The first argument must eval to a variable name.
288 ;; Arguments: (var-name position &optional buffer).
290 ;; This is useful for moving markers that are supposed to be local.
291 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
292 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
293 ;; buffer, a new marker will be created.
294 (defun viper-move-marker-locally (var pos
&optional buffer
)
295 (if (markerp (eval var
))
297 (set var
(make-marker)))
298 (move-marker (eval var
) pos buffer
))
301 ;; Print CONDITIONS as a message.
302 (defun viper-message-conditions (conditions)
303 (let ((case (car conditions
)) (msg (cdr conditions
)))
306 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
311 ;;; List/alist utilities
313 ;; Convert LIST to an alist
314 (defun viper-list-to-alist (lst)
317 (setq alist
(cons (list (car lst
)) alist
))
318 (setq lst
(cdr lst
)))
321 ;; Convert ALIST to a list.
322 (defun viper-alist-to-list (alst)
325 (setq lst
(cons (car (car alst
)) lst
))
326 (setq alst
(cdr alst
)))
329 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
330 (defun viper-filter-alist (regexp alst
)
332 (let ((outalst) (inalst alst
))
334 (if (string-match regexp
(car (car inalst
)))
335 (setq outalst
(cons (car inalst
) outalst
)))
336 (setq inalst
(cdr inalst
)))
339 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
340 (defun viper-filter-list (regexp lst
)
342 (let ((outlst) (inlst lst
))
344 (if (string-match regexp
(car inlst
))
345 (setq outlst
(cons (car inlst
) outlst
)))
346 (setq inlst
(cdr inlst
)))
350 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
351 ;; LIS2 is modified by filtering it: deleting its members of the form
352 ;; \(car elt\) such that (car elt') is in LIS1.
353 (defun viper-append-filter-alist (lis1 lis2
)
356 ;;filter-append the second list
358 ;; delete all occurrences
359 (while (setq elt
(assoc (car (car temp
)) lis2
))
360 (setq lis2
(delq elt lis2
)))
361 (setq temp
(cdr temp
)))
367 ;;; Support for :e, :r, :w file globbing
369 ;; Glob the file spec.
370 ;; This function is designed to work under Unix. It might also work under VMS.
371 (defun viper-glob-unix-files (filespec)
373 (cond (ex-unix-type-shell shell-file-name
)
374 ((memq system-type
'(vax-vms axp-vms
)) "*dcl*") ; VAX VMS
375 (t "sh"))) ; probably Unix anyway
377 ;; using cond in anticipation of further additions
378 (cond (ex-unix-type-shell-options)
380 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec
))
381 (t (format "ls -1 -d %s" filespec
))))
384 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
388 (call-process gshell nil t nil
392 (call-process gshell nil t nil
395 (goto-char (point-min))
396 ;; Issue an error, if no match.
397 (unless (eq 0 status
)
399 (skip-chars-forward " \t\n\j")
400 (if (looking-at "ls:")
401 (viper-forward-Word 1))
406 (buffer-substring (point) (viper-line-pos 'end
)))
408 (goto-char (point-min))
409 (viper-get-filenames-from-buffer 'one-per-line
))
413 ;; Interpret the stuff in the buffer as a list of file names
414 ;; return a list of file names listed in the buffer beginning at point
415 ;; If optional arg is supplied, assume each filename is listed on a separate
417 (defun viper-get-filenames-from-buffer (&optional one-per-line
)
418 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
420 (skip-chars-forward skip-chars
)
422 (if (cond ((looking-at "\"")
424 (re-search-forward "[^\"]+" nil t
)) ; noerror
427 (re-search-forward "[^']+" nil t
)) ; noerror
430 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
432 (buffer-substring (match-beginning 0) (match-end 0))))
435 (skip-chars-forward " \t\n")
436 (setq result
(cons fname result
)))
439 ;; convert MS-DOS wildcards to regexp
440 (defun viper-wildcard-to-regexp (wcard)
442 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
445 (goto-char (point-min))
447 (skip-chars-forward "^*?.\\\\")
448 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
449 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
450 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
451 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
457 ;; glob windows files
458 ;; LIST is expected to be in reverse order
459 (defun viper-glob-mswindows-files (filespec)
460 (let ((case-fold-search t
)
463 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
466 (goto-char (point-min))
467 (setq tmp
(viper-get-filenames-from-buffer))
469 (setq tmp2
(cons (directory-files
470 ;; the directory part
471 (or (file-name-directory (car tmp
))
473 t
; return full names
474 ;; the regexp part: globs the file names
476 (viper-wildcard-to-regexp
477 (file-name-nondirectory (car tmp
)))
480 (setq tmp
(cdr tmp
)))
481 (reverse (apply 'append tmp2
)))))
486 ;; Rotate RING's index. DIRection can be positive or negative.
487 (defun viper-ring-rotate1 (ring dir
)
488 (if (and (ring-p ring
) (> (ring-length ring
) 0))
490 (setcar ring
(cond ((> dir
0)
491 (ring-plus1 (car ring
) (ring-length ring
)))
493 (ring-minus1 (car ring
) (ring-length ring
)))
494 ;; don't rotate if dir = 0
496 (viper-current-ring-item ring
)
499 (defun viper-special-ring-rotate1 (ring dir
)
500 (if (memq viper-intermediate-command
501 '(repeating-display-destructive-command
502 repeating-insertion-from-ring
))
503 (viper-ring-rotate1 ring dir
)
504 ;; don't rotate otherwise
505 (viper-ring-rotate1 ring
0)))
507 ;; current ring item; if N is given, then so many items back from the
509 (defun viper-current-ring-item (ring &optional n
)
511 (if (and (ring-p ring
) (> (ring-length ring
) 0))
512 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
514 ;; Push item onto ring. The second argument is a ring-variable, not value.
515 (defun viper-push-onto-ring (item ring-var
)
516 (or (ring-p (eval ring-var
))
517 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
518 (or (null item
) ; don't push nil
519 (and (stringp item
) (string= item
"")) ; or empty strings
520 (equal item
(viper-current-ring-item (eval ring-var
))) ; or old stuff
521 ;; Since viper-set-destructive-command checks if we are inside
522 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
523 ;; cmd viper-repeat makes a call to the current function only if `.' is
524 ;; executing a command from the command history. It doesn't call the
525 ;; push-onto-ring function if `.' is simply repeating the last
526 ;; destructive command. We only check for ESC (which happens when we do
527 ;; insert with a prefix argument, or if this-command-keys doesn't give
528 ;; anything meaningful (in that case we don't know what to show to the
530 (and (eq ring-var
'viper-command-ring
)
531 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
532 (viper-array-to-string (this-command-keys))))
533 (viper-ring-insert (eval ring-var
) item
))
537 ;; removing elts from ring seems to break it
538 (defun viper-cleanup-ring (ring)
539 (or (< (ring-length ring
) 2)
540 (null (viper-current-ring-item ring
))
541 ;; last and previous equal
542 (if (equal (viper-current-ring-item ring
)
543 (viper-current-ring-item ring
1))
544 (viper-ring-pop ring
))))
546 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
547 (defun viper-ring-pop (ring)
548 (let* ((ln (ring-length ring
))
549 (vec (cdr (cdr ring
)))
550 (veclen (length vec
))
552 (idx (max 0 (ring-minus1 hd ln
)))
553 (top-elt (aref vec idx
)))
556 (while (< (1+ idx
) veclen
)
557 (aset vec idx
(aref vec
(1+ idx
)))
561 (setq hd
(max 0 (ring-minus1 hd ln
)))
562 (if (= hd
(1- ln
)) (setq hd
0))
563 (setcar ring hd
) ; move head
564 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
568 (defun viper-ring-insert (ring item
)
569 (let* ((ln (ring-length ring
))
570 (vec (cdr (cdr ring
)))
571 (veclen (length vec
))
573 (vecpos-after-hd (if (= hd
0) ln hd
))
578 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
579 (setcar ring
(ring-plus1 hd ln
)))
580 (setcar (cdr ring
) (1+ ln
))
581 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
582 (while (and (>= idx vecpos-after-hd
) (> ln
0))
583 (aset vec idx
(aref vec
(1- idx
)))
585 (aset vec vecpos-after-hd item
))
591 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
592 ;; PRE-STRING is a string to prepend to the abbrev string.
593 ;; POST-STRING is a string to append to the abbrev string.
594 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
595 ;; if the orig string was truncated.
596 (defun viper-abbreviate-string (string max-len
597 pre-string post-string abbrev-sign
)
601 (substring string
0 (min max-len
(length string
)))))
602 (cond ((null truncated-str
) "")
603 ((> (length string
) max-len
)
605 pre-string truncated-str abbrev-sign post-string
))
606 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
608 ;; tells if we are over a whitespace-only line
609 (defsubst viper-over-whitespace-line
()
612 (looking-at "^[ \t]*$")))
615 ;;; Saving settings in custom file
617 ;; Save the current setting of VAR in CUSTOM-FILE.
618 ;; If given, MESSAGE is a message to be displayed after that.
619 ;; This message is erased after 2 secs, if erase-msg is non-nil.
620 ;; Arguments: var message custom-file &optional erase-message
621 (defun viper-save-setting (var message custom-file
&optional erase-msg
)
622 (let* ((var-name (symbol-name var
))
623 (var-val (if (boundp var
) (eval var
)))
624 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
625 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
630 (goto-char (point-min))
631 (if (re-search-forward regexp nil t
)
632 (let ((reg-end (1- (match-end 0))))
633 (search-backward var-name
)
634 (delete-region (match-beginning 0) reg-end
)
635 (goto-char (match-beginning 0))
636 (insert (format "%s '%S" var-name var-val
)))
637 (goto-char (point-max))
638 (if (not (bolp)) (insert "\n"))
639 (insert (format "(setq %s '%S)\n" var-name var-val
)))
648 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
649 ;; match this pattern.
650 (defun viper-save-string-in-file (string custom-file
&optional pattern
)
651 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
654 (let (buffer-read-only)
655 (goto-char (point-min))
656 (if pattern
(delete-matching-lines pattern
))
657 (goto-char (point-max))
658 (if string
(insert string
))
664 ;; define remote file test
665 (defun viper-file-remote-p (file-name)
666 (file-remote-p file-name
))
669 ;; This is a simple-minded check for whether a file is under version control.
670 ;; If file,v exists but file doesn't, this file is considered to be not checked
671 ;; in and not checked out for the purpose of patching (since patch won't be
672 ;; able to read such a file anyway).
673 ;; FILE is a string representing file name
674 ;;(defun viper-file-under-version-control (file)
675 ;; (let* ((filedir (file-name-directory file))
676 ;; (file-nondir (file-name-nondirectory file))
677 ;; (trial (concat file-nondir ",v"))
678 ;; (full-trial (concat filedir trial))
679 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
680 ;; (and (stringp file)
681 ;; (file-exists-p file)
684 ;; (file-exists-p full-trial)
685 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
686 ;; ;; don't be fooled by this!
687 ;; (not (equal (file-attributes file)
688 ;; (file-attributes full-trial))))
689 ;; ;; check if a version is in RCS/ directory
690 ;; (file-exists-p full-rcs-trial)))
694 (defsubst viper-file-checked-in-p
(file)
695 (and (featurep 'vc-hooks
)
696 ;; CVS files are considered not checked in
697 (not (memq (vc-backend file
) '(nil CVS
)))
698 (if (fboundp 'vc-state
)
700 (not (memq (vc-state file
) '(edited needs-merge
)))
701 (not (stringp (vc-state file
))))
702 ;; XEmacs has no vc-state
703 (not (vc-locking-user file
)))
706 ;; checkout if visited file is checked in
707 (defun viper-maybe-checkout (buf)
708 (let ((file (expand-file-name (buffer-file-name buf
)))
709 (checkout-function (key-binding "\C-x\C-q")))
710 (if (and (viper-file-checked-in-p file
)
714 "File %s is checked in. Check it out? "
715 (viper-abbreviate-file-name file
))))
716 (with-current-buffer buf
717 (command-execute checkout-function
)))))
723 (defun viper-put-on-search-overlay (beg end
)
724 (if (viper-overlay-p viper-search-overlay
)
725 (viper-move-overlay viper-search-overlay beg end
)
726 (setq viper-search-overlay
(viper-make-overlay beg end
(current-buffer)))
728 viper-search-overlay
'priority viper-search-overlay-priority
))
729 (viper-overlay-put viper-search-overlay
'face viper-search-face
))
733 (defun viper-flash-search-pattern ()
734 (if (not (viper-has-face-support-p))
736 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
738 (viper-overlay-put viper-search-overlay
'face nil
)))
740 (defun viper-hide-search-overlay ()
741 (if (not (viper-overlay-p viper-search-overlay
))
743 (setq viper-search-overlay
744 (viper-make-overlay (point-min) (point-min) (current-buffer)))
746 viper-search-overlay
'priority viper-search-overlay-priority
)))
747 (viper-overlay-put viper-search-overlay
'face nil
))
751 (defsubst viper-move-replace-overlay
(beg end
)
752 (viper-move-overlay viper-replace-overlay beg end
))
754 (defun viper-set-replace-overlay (beg end
)
755 (if (viper-overlay-live-p viper-replace-overlay
)
756 (viper-move-replace-overlay beg end
)
757 (setq viper-replace-overlay
(viper-make-overlay beg end
(current-buffer)))
760 viper-replace-overlay
(if viper-emacs-p
'evaporate
'detachable
) nil
)
762 viper-replace-overlay
'priority viper-replace-overlay-priority
)
763 ;; If Emacs will start supporting overlay maps, as it currently supports
764 ;; text-property maps, we could do away with viper-replace-minor-mode and
765 ;; just have keymap attached to replace overlay.
767 ;; viper-replace-overlay
768 ;; (if viper-xemacs-p 'keymap 'local-map)
769 ;; viper-replace-map)
771 (if (viper-has-face-support-p)
773 viper-replace-overlay
'face viper-replace-overlay-face
))
774 (viper-save-cursor-color 'before-replace-mode
)
775 (viper-change-cursor-color viper-replace-overlay-cursor-color
)
779 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph
)
780 (or (viper-overlay-live-p viper-replace-overlay
)
781 (viper-set-replace-overlay (point-min) (point-min)))
782 (if (or (not (viper-has-face-support-p))
783 viper-use-replace-region-delimiters
)
784 (let ((before-name (if viper-xemacs-p
'begin-glyph
'before-string
))
785 (after-name (if viper-xemacs-p
'end-glyph
'after-string
)))
786 (viper-overlay-put viper-replace-overlay before-name before-glyph
)
787 (viper-overlay-put viper-replace-overlay after-name after-glyph
))))
789 (defun viper-hide-replace-overlay ()
790 (viper-set-replace-overlay-glyphs nil nil
)
791 (viper-restore-cursor-color 'after-replace-mode
)
792 (viper-restore-cursor-color 'after-insert-mode
)
793 (if (viper-has-face-support-p)
794 (viper-overlay-put viper-replace-overlay
'face nil
)))
797 (defsubst viper-replace-start
()
798 (viper-overlay-start viper-replace-overlay
))
799 (defsubst viper-replace-end
()
800 (viper-overlay-end viper-replace-overlay
))
805 (defun viper-set-minibuffer-overlay ()
806 (viper-check-minibuffer-overlay)
807 (if (viper-has-face-support-p)
810 viper-minibuffer-overlay
'face viper-minibuffer-current-face
)
812 viper-minibuffer-overlay
'priority viper-minibuffer-overlay-priority
)
815 viper-minibuffer-overlay
816 (if viper-emacs-p
'evaporate
'detachable
)
818 ;; make viper-minibuffer-overlay open-ended
819 ;; In emacs, it is made open ended at creation time
822 (viper-overlay-put viper-minibuffer-overlay
'start-open nil
)
823 (viper-overlay-put viper-minibuffer-overlay
'end-open nil
)))
826 (defun viper-check-minibuffer-overlay ()
827 (if (viper-overlay-live-p viper-minibuffer-overlay
)
829 viper-minibuffer-overlay
830 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
832 (setq viper-minibuffer-overlay
834 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
835 ;; make overlay open-ended
837 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
839 (current-buffer) nil
'rear-advance
)))
843 (defsubst viper-is-in-minibuffer
()
845 (string-match "\*Minibuf-" (buffer-name))))
849 ;;; XEmacs compatibility
851 (defun viper-abbreviate-file-name (file)
852 (viper-cond-compile-for-xemacs-or-emacs
853 ;; XEmacs requires addl argument
854 (abbreviate-file-name file t
)
856 (abbreviate-file-name file
)
859 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
860 ;; in sit-for, so this function smoothes out the differences.
861 (defsubst viper-sit-for-short
(val &optional nodisp
)
863 (sit-for (/ val
1000.0) nodisp
)
864 (sit-for 0 val nodisp
)))
866 ;; EVENT may be a single event of a sequence of events
867 (defsubst viper-ESC-event-p
(event)
868 (let ((ESC-keys '(?\e
(control \
[) escape
))
869 (key (viper-event-key event
)))
870 (member key ESC-keys
)))
872 ;; checks if object is a marker, has a buffer, and points to within that buffer
873 (defun viper-valid-marker (marker)
874 (if (and (markerp marker
) (marker-buffer marker
))
875 (let ((buf (marker-buffer marker
))
876 (pos (marker-position marker
)))
879 (and (<= pos
(point-max)) (<= (point-min) pos
))))))
881 (defsubst viper-mark-marker
()
882 (viper-cond-compile-for-xemacs-or-emacs
883 (mark-marker t
) ; xemacs
884 (mark-marker) ; emacs
887 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
888 ;; is the same as (mark t).
889 (defsubst viper-set-mark-if-necessary
()
890 (setq mark-ring
(delete (viper-mark-marker) mark-ring
))
891 (set-mark-command nil
)
892 (setq viper-saved-mark
(point)))
894 ;; In transient mark mode (zmacs mode), it is annoying when regions become
895 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
896 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
897 (defun viper-deactivate-mark ()
898 (viper-cond-compile-for-xemacs-or-emacs
899 (zmacs-deactivate-region)
903 (defsubst viper-leave-region-active
()
904 (viper-cond-compile-for-xemacs-or-emacs
905 (setq zmacs-region-stays t
)
909 ;; Check if arg is a valid character for register
910 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
911 ;; Letter means lowercase letters, Letter means uppercase letters, and
912 ;; digit means digits from 1 to 9.
913 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
914 (defun viper-valid-register (reg &optional type
)
915 (or type
(setq type
'(letter Letter digit
)))
916 (or (if (memq 'letter type
)
917 (and (<= ?a reg
) (<= reg ?z
)))
918 (if (memq 'digit type
)
919 (and (<= ?
1 reg
) (<= reg ?
9)))
920 (if (memq 'Letter type
)
921 (and (<= ?A reg
) (<= reg ?Z
)))
926 ;; it is suggested that an event must be copied before it is assigned to
927 ;; last-command-event in XEmacs
928 (defun viper-copy-event (event)
929 (viper-cond-compile-for-xemacs-or-emacs
930 (copy-event event
) ; xemacs
934 ;; Uses different timeouts for ESC-sequences and others
935 (defsubst viper-fast-keysequence-p
()
936 (not (viper-sit-for-short
937 (if (viper-ESC-event-p last-input-event
)
938 viper-ESC-keyseq-timeout
939 viper-fast-keyseq-timeout
)
942 ;; like read-event, but in XEmacs also try to convert to char, if possible
943 (defun viper-read-event-convert-to-char ()
945 (viper-cond-compile-for-xemacs-or-emacs
947 (setq event
(next-command-event))
948 (or (event-to-character event
)
954 ;; Viperized read-key-sequence
955 (defun viper-read-key-sequence (prompt &optional continue-echo
)
956 (let (inhibit-quit event keyseq
)
957 (setq keyseq
(read-key-sequence prompt continue-echo
))
958 (setq event
(if viper-xemacs-p
959 (elt keyseq
0) ; XEmacs returns vector of events
960 (elt (listify-key-sequence keyseq
) 0)))
961 (if (viper-ESC-event-p event
)
962 (let (unread-command-events)
963 (if (viper-fast-keysequence-p)
964 (let ((viper-vi-global-user-minor-mode nil
)
965 (viper-vi-local-user-minor-mode nil
)
966 (viper-vi-intercept-minor-mode nil
)
967 (viper-insert-intercept-minor-mode nil
)
968 (viper-replace-minor-mode nil
) ; actually unnecessary
969 (viper-insert-global-user-minor-mode nil
)
970 (viper-insert-local-user-minor-mode nil
))
971 ;; Note: set unread-command-events only after testing for fast
972 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
973 ;; always t -- whether there is anything after ESC or not
974 (viper-set-unread-command-events keyseq
)
975 (setq keyseq
(read-key-sequence nil
)))
976 (viper-set-unread-command-events keyseq
)
977 (setq keyseq
(read-key-sequence nil
)))))
981 ;; This function lets function-key-map convert key sequences into logical
982 ;; keys. This does a better job than viper-read-event when it comes to kbd
983 ;; macros, since it enables certain macros to be shared between X and TTY modes
984 ;; by correctly mapping key sequences for Left/Right/... (one an ascii
985 ;; terminal) into logical keys left, right, etc.
986 (defun viper-read-key ()
987 (let ((overriding-local-map viper-overriding-map
)
990 (use-global-map viper-overriding-map
)
992 (setq key
(elt (viper-read-key-sequence nil
) 0))
993 (use-global-map global-map
))
997 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
998 ;; instead of nil, if '(nil) was previously inadvertently assigned to
999 ;; unread-command-events
1000 (defun viper-event-key (event)
1001 (or (and event
(eventp event
))
1002 (error "viper-event-key: Wrong type argument, eventp, %S" event
))
1003 (when (viper-cond-compile-for-xemacs-or-emacs
1004 (or (key-press-event-p event
) (mouse-event-p event
)) ; xemacs
1007 (let ((mod (event-modifiers event
))
1010 (viper-cond-compile-for-xemacs-or-emacs
1012 (cond ((key-press-event-p event
)
1014 ((button-event-p event
)
1015 (concat "mouse-" (prin1-to-string (event-button event
))))
1017 (error "viper-event-key: Unknown event, %S" event
)))
1018 ;; Emacs doesn't handle capital letters correctly, since
1019 ;; \S-a isn't considered the same as A (it behaves as
1020 ;; plain `a' instead). So we take care of this here
1021 (cond ((and (viper-characterp event
) (<= ?A event
) (<= event ?Z
))
1024 ;; Emacs has the oddity whereby characters 128+char
1025 ;; represent M-char *if* this appears inside a string.
1026 ;; So, we convert them manually to (meta char).
1027 ((and (viper-characterp event
)
1028 (< ?\C-? event
) (<= event
255))
1030 event
(- event ?\C-?
1)))
1031 ((and (null mod
) (eq event
'return
))
1033 ((and (null mod
) (eq event
'space
))
1035 ((and (null mod
) (eq event
'delete
))
1037 ((and (null mod
) (eq event
'backspace
))
1039 (t (event-basic-type event
)))
1040 ) ; viper-cond-compile-for-xemacs-or-emacs
1042 (if (viper-characterp basis
)
1044 (if (viper= basis ?\C-?
)
1045 (list 'control
'\?) ; taking care of an emacs bug
1046 (intern (char-to-string basis
)))))
1048 (append mod
(list basis
))
1051 (defun viper-key-to-emacs-key (key)
1052 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
1053 (cond (viper-xemacs-p key
)
1056 (setq key-name
(symbol-name key
))
1057 (cond ((= (length key-name
) 1) ; character event
1058 (string-to-char key-name
))
1059 ;; Emacs doesn't recognize `return' and `escape' as events on
1060 ;; dumb terminals, so we translate them into characters
1061 ((and viper-emacs-p
(not (viper-window-display-p))
1062 (string= key-name
"return"))
1064 ((and viper-emacs-p
(not (viper-window-display-p))
1065 (string= key-name
"escape"))
1067 ;; pass symbol-event as is
1071 (setq modifiers
(subseq key
0 (1- (length key
)))
1072 base-key
(viper-seq-last-elt key
)
1073 base-key-name
(symbol-name base-key
)
1074 char-p
(= (length base-key-name
) 1))
1077 '(lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1081 (car (read-from-string
1084 (mapconcat 'identity mod-char-list
"-\\")
1090 (mapconcat 'identity mod-char-list
"-")
1096 ;; LIS is assumed to be a list of events of characters
1097 (defun viper-eventify-list-xemacs (lis)
1100 (cond ((viper-characterp elt
) (character-to-event elt
))
1103 "viper-eventify-list-xemacs: can't convert to event, %S"
1108 ;; Smoothes out the difference between Emacs' unread-command-events
1109 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1110 ;; events or a sequence of keys.
1112 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1113 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1114 ;; into an event. Below, we delete nil from event lists, since nil is the most
1115 ;; common symbol that might appear in this wrong context.
1116 (defun viper-set-unread-command-events (arg)
1119 unread-command-events
1121 (cond ((eventp arg
) (list arg
))
1124 (listify-key-sequence arg
))
1126 "viper-set-unread-command-events: Invalid argument, %S"
1128 (if (not (eventp nil
))
1129 (setq new-events
(delq nil new-events
)))
1130 (append new-events unread-command-events
)))
1133 unread-command-events
1135 (cond ((viper-characterp arg
) (list (character-to-event arg
)))
1136 ((eventp arg
) (list arg
))
1137 ((stringp arg
) (mapcar 'character-to-event arg
))
1138 ((vectorp arg
) (append arg nil
)) ; turn into list
1139 ((listp arg
) (viper-eventify-list-xemacs arg
))
1141 "viper-set-unread-command-events: Invalid argument, %S" arg
)))
1142 unread-command-events
))))
1145 ;; Check if vec is a vector of key-press events representing characters
1147 (defun viper-event-vector-p (vec)
1149 (eval (cons 'and
(mapcar '(lambda (elt) (if (eventp elt
) t
)) vec
)))))
1152 ;; check if vec is a vector of character symbols
1153 (defun viper-char-symbol-sequence-p (vec)
1158 (mapcar (lambda (elt)
1159 (and (symbolp elt
) (= (length (symbol-name elt
)) 1)))
1163 (defun viper-char-array-p (array)
1164 (eval (cons 'and
(mapcar 'viper-characterp array
))))
1167 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1168 ;; convert events to keys and, if all keys are regular printable
1169 ;; characters, will return a string. Otherwise, will return a string
1170 ;; representing a vector of converted events. If the input was a Viper macro,
1171 ;; will return a string that represents this macro as a vector.
1172 (defun viper-array-to-string (event-seq)
1174 (cond ((stringp event-seq
) event-seq
)
1175 ((viper-event-vector-p event-seq
)
1176 (setq temp
(mapcar 'viper-event-key event-seq
))
1177 (cond ((viper-char-symbol-sequence-p temp
)
1178 (mapconcat 'symbol-name temp
""))
1179 ((and (viper-char-array-p
1180 (setq temp2
(mapcar 'viper-key-to-character temp
))))
1181 (mapconcat 'char-to-string temp2
""))
1182 (t (prin1-to-string (vconcat temp
)))))
1183 ((viper-char-symbol-sequence-p event-seq
)
1184 (mapconcat 'symbol-name event-seq
""))
1185 ((and (vectorp event-seq
)
1187 (setq temp
(mapcar 'viper-key-to-character event-seq
))))
1188 (mapconcat 'char-to-string temp
""))
1189 (t (prin1-to-string event-seq
)))))
1191 (defun viper-key-press-events-to-chars (events)
1192 (mapconcat (viper-cond-compile-for-xemacs-or-emacs
1193 (lambda (elt) (char-to-string (event-to-character elt
))) ; xemacs
1194 'char-to-string
; emacs
1200 (defun viper-read-char-exclusive ()
1202 (echo-keystrokes 1))
1205 (setq char
(read-char))
1207 ;; skip event if not char
1208 (viper-read-event))))
1211 ;; key is supposed to be in viper's representation, e.g., (control l), a
1213 (defun viper-key-to-character (key)
1214 (cond ((eq key
'space
) ?\
)
1215 ((eq key
'delete
) ?\C-?
)
1216 ((eq key
'return
) ?\C-m
)
1217 ((eq key
'backspace
) ?\C-h
)
1219 (= 1 (length (symbol-name key
))))
1220 (string-to-char (symbol-name key
)))
1222 (eq (car key
) 'control
)
1223 (symbol-name (nth 1 key
))
1224 (= 1 (length (symbol-name (nth 1 key
)))))
1225 (read (format "?\\C-%s" (symbol-name (nth 1 key
)))))
1229 (defun viper-setup-master-buffer (&rest other-files-or-buffers
)
1230 "Set up the current buffer as a master buffer.
1231 Arguments become related buffers. This function should normally be used in
1232 the `Local variables' section of a file."
1233 (setq viper-related-files-and-buffers-ring
1234 (make-ring (1+ (length other-files-or-buffers
))))
1235 (mapcar '(lambda (elt)
1236 (viper-ring-insert viper-related-files-and-buffers-ring elt
))
1237 other-files-or-buffers
)
1238 (viper-ring-insert viper-related-files-and-buffers-ring
(buffer-name))
1241 ;;; Movement utilities
1243 ;; Characters that should not be considered as part of the word, in reformed-vi
1245 (defconst viper-non-word-characters-reformed-vi
1246 "!@#$%^&*()-+=|\\~`{}[];:'\",<.>/?")
1247 ;; These are characters that are not to be considered as parts of a word in
1249 ;; Set each time state changes and at loading time
1250 (viper-deflocalvar viper-non-word-characters nil
)
1252 ;; must be buffer-local
1253 (viper-deflocalvar viper-ALPHA-char-class
"w"
1254 "String of syntax classes characterizing Viper's alphanumeric symbols.
1255 In addition, the symbol `_' may be considered alphanumeric if
1256 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1258 (defconst viper-strict-ALPHA-chars
"a-zA-Z0-9_"
1259 "Regexp matching the set of alphanumeric characters acceptable to strict
1261 (defconst viper-strict-SEP-chars
" \t\n"
1262 "Regexp matching the set of alphanumeric characters acceptable to strict
1264 (defconst viper-strict-SEP-chars-sans-newline
" \t"
1265 "Regexp matching the set of alphanumeric characters acceptable to strict
1268 (defconst viper-SEP-char-class
" -"
1269 "String of syntax classes for Vi separators.
1270 Usually contains ` ', linefeed, TAB or formfeed.")
1273 ;; Set Viper syntax classes and related variables according to
1274 ;; `viper-syntax-preference'.
1275 (defun viper-update-syntax-classes (&optional set-default
)
1276 (let ((preference (cond ((eq viper-syntax-preference
'emacs
)
1277 "w") ; Viper words have only Emacs word chars
1278 ((eq viper-syntax-preference
'extended
)
1279 "w_") ; Viper words have Emacs word & symbol chars
1280 (t "w"))) ; Viper words are Emacs words plus `_'
1281 (non-word-chars (cond ((eq viper-syntax-preference
'reformed-vi
)
1282 (viper-string-to-list
1283 viper-non-word-characters-reformed-vi
))
1286 (setq-default viper-ALPHA-char-class preference
1287 viper-non-word-characters non-word-chars
)
1288 (setq viper-ALPHA-char-class preference
1289 viper-non-word-characters non-word-chars
))
1292 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1293 ;; is `nil'. If nil, use setq.
1294 (defun viper-set-syntax-preference (&optional symbol value
)
1295 "Set Viper syntax preference.
1296 If called interactively or if SYMBOL is nil, sets syntax preference in current
1297 buffer. If called non-interactively, preferably via the customization widget,
1298 sets the default value."
1303 "Viper syntax preference: "
1304 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1305 nil
'require-match
)))
1306 (if (stringp value
) (setq value
(intern value
)))
1307 (or (memq value
'(strict-vi reformed-vi extended emacs
))
1308 (error "Invalid Viper syntax preference, %S" value
))
1310 (setq-default viper-syntax-preference value
)
1311 (setq viper-syntax-preference value
))
1312 (viper-update-syntax-classes))
1314 (defcustom viper-syntax-preference
'reformed-vi
1315 "*Syntax type characterizing Viper's alphanumeric symbols.
1316 Affects movement and change commands that deal with Vi-style words.
1317 Works best when set in the hooks to various major modes.
1319 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1321 `reformed-vi' means Viper words are like Emacs words \(as determined using
1322 Emacs syntax tables, which are different for different major modes\) with two
1323 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1324 symbols, such as `,',:,\",),{, etc., are excluded.
1325 This behaves very close to `strict-vi', but also works well with non-ASCII
1326 characters from various alphabets.
1328 `extended' means Viper word constituents are symbols that are marked as being
1329 parts of words OR symbols in Emacs syntax tables.
1330 This is most appropriate for major modes intended for editing programs.
1332 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1334 This option is appropriate if you like Emacs-style words."
1335 :type
'(radio (const strict-vi
) (const reformed-vi
)
1336 (const extended
) (const emacs
))
1337 :set
'viper-set-syntax-preference
1339 (make-variable-buffer-local 'viper-syntax-preference
)
1342 ;; addl-chars are characters to be temporarily considered as alphanumerical
1343 (defun viper-looking-at-alpha (&optional addl-chars
)
1344 (or (stringp addl-chars
) (setq addl-chars
""))
1345 (if (eq viper-syntax-preference
'reformed-vi
)
1346 (setq addl-chars
(concat addl-chars
"_")))
1347 (let ((char (char-after (point))))
1349 (if (eq viper-syntax-preference
'strict-vi
)
1350 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars
"]"))
1352 ;; or one of the additional chars being asked to include
1353 (viper-memq-char char
(viper-string-to-list addl-chars
))
1355 ;; not one of the excluded word chars (note:
1356 ;; viper-non-word-characters is a list)
1357 (not (viper-memq-char char viper-non-word-characters
))
1358 ;; char of the Viper-word syntax class
1359 (viper-memq-char (char-syntax char
)
1360 (viper-string-to-list viper-ALPHA-char-class
))))))
1363 (defun viper-looking-at-separator ()
1364 (let ((char (char-after (point))))
1366 (if (eq viper-syntax-preference
'strict-vi
)
1367 (viper-memq-char char
(viper-string-to-list viper-strict-SEP-chars
))
1368 (or (eq char ?
\n) ; RET is always a separator in Vi
1369 (viper-memq-char (char-syntax char
)
1370 (viper-string-to-list viper-SEP-char-class
)))))
1373 (defsubst viper-looking-at-alphasep
(&optional addl-chars
)
1374 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars
)))
1376 (defun viper-skip-alpha-forward (&optional addl-chars
)
1377 (or (stringp addl-chars
) (setq addl-chars
""))
1380 (cond ((eq viper-syntax-preference
'strict-vi
)
1382 (t viper-ALPHA-char-class
))
1383 (cond ((eq viper-syntax-preference
'strict-vi
)
1384 (concat viper-strict-ALPHA-chars addl-chars
))
1387 (defun viper-skip-alpha-backward (&optional addl-chars
)
1388 (or (stringp addl-chars
) (setq addl-chars
""))
1391 (cond ((eq viper-syntax-preference
'strict-vi
)
1393 (t viper-ALPHA-char-class
))
1394 (cond ((eq viper-syntax-preference
'strict-vi
)
1395 (concat viper-strict-ALPHA-chars addl-chars
))
1398 ;; weird syntax tables may confuse strict-vi style
1399 (defsubst viper-skip-all-separators-forward
(&optional within-line
)
1400 (if (eq viper-syntax-preference
'strict-vi
)
1402 (skip-chars-forward viper-strict-SEP-chars-sans-newline
)
1403 (skip-chars-forward viper-strict-SEP-chars
))
1404 (viper-skip-syntax 'forward
1405 viper-SEP-char-class
1406 (or within-line
"\n")
1407 (if within-line
(viper-line-pos 'end
)))))
1408 (defsubst viper-skip-all-separators-backward
(&optional within-line
)
1409 (if (eq viper-syntax-preference
'strict-vi
)
1411 (skip-chars-backward viper-strict-SEP-chars-sans-newline
)
1412 (skip-chars-backward viper-strict-SEP-chars
))
1413 (viper-skip-syntax 'backward
1414 viper-SEP-char-class
1415 (or within-line
"\n")
1416 (if within-line
(viper-line-pos 'start
)))))
1417 (defun viper-skip-nonseparators (direction)
1420 (concat "^" viper-SEP-char-class
)
1422 (viper-line-pos (if (eq direction
'forward
) 'end
'start
))))
1425 ;; skip over non-word constituents and non-separators
1426 (defun viper-skip-nonalphasep-forward ()
1427 (if (eq viper-syntax-preference
'strict-vi
)
1429 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1432 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1433 ;; Emacs may consider some of these as words, but we don't want them
1434 viper-non-word-characters
1435 (viper-line-pos 'end
))))
1436 (defun viper-skip-nonalphasep-backward ()
1437 (if (eq viper-syntax-preference
'strict-vi
)
1438 (skip-chars-backward
1439 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1442 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1443 ;; Emacs may consider some of these as words, but we don't want them
1444 viper-non-word-characters
1445 (viper-line-pos 'start
))))
1447 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1448 ;; Return the number of chars traveled.
1449 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1450 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1451 ;; words, even if Emacs syntax table says they are.
1452 (defun viper-skip-syntax (direction syntax addl-chars
&optional limit
)
1456 (if (eq direction
'forward
)
1457 'skip-chars-forward
'skip-chars-backward
))
1459 (if (eq direction
'forward
)
1460 'viper-forward-char-carefully
'viper-backward-char-carefully
))
1461 char-looked-at syntax-of-char-looked-at negated-syntax
)
1463 (cond ((listp addl-chars
) (viper-charlist-to-string addl-chars
))
1464 ((stringp addl-chars
) addl-chars
)
1467 (cond ((listp syntax
) syntax
)
1468 ((stringp syntax
) (viper-string-to-list syntax
))
1470 (if (memq ?^ syntax
) (setq negated-syntax t
))
1472 (while (and (not (= local
0))
1473 (cond ((eq direction
'forward
)
1476 (setq char-looked-at
(viper-char-at-pos direction
)
1477 ;; if outside the range, set to nil
1478 syntax-of-char-looked-at
(if char-looked-at
1479 (char-syntax char-looked-at
)))
1482 (cond ((and limit
(eq direction
'forward
))
1484 (limit ; backward & limit
1487 ;; char under/before cursor has appropriate syntax
1489 (not (memq syntax-of-char-looked-at syntax
))
1490 (memq syntax-of-char-looked-at syntax
))
1491 ;; if char-syntax class is "word", make sure it is not one
1492 ;; of the excluded characters
1493 (if (and (eq syntax-of-char-looked-at ?w
)
1494 (not negated-syntax
))
1495 (not (viper-memq-char
1496 char-looked-at viper-non-word-characters
))
1498 (funcall skip-syntax-func
1)
1500 (funcall skip-chars-func addl-chars limit
)))
1501 (setq total
(+ total local
)))
1507 (provide 'viper-util
)
1510 ;;; Local Variables:
1511 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1514 ;;; arch-tag: 7f023fd5-dd9e-4378-a397-9c179553b0e3
1515 ;;; viper-util.el ends here