1 ;;; viper-util.el --- Utilities used by viper.el
3 ;; Copyright (C) 1994-1997, 1999-2018 Free Software Foundation, Inc.
5 ;; 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 of the License, or
13 ;; (at your option) any later version.
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. If not, see <https://www.gnu.org/licenses/>.
31 (defvar viper-overriding-map
)
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
)
47 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
55 (defalias 'viper-overlay-p
56 (if (featurep 'xemacs
) 'extentp
'overlayp
))
57 (defalias 'viper-make-overlay
58 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
59 (defalias 'viper-overlay-live-p
60 (if (featurep 'xemacs
) 'extent-live-p
'overlayp
))
61 (defalias 'viper-move-overlay
62 (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
))
63 (defalias 'viper-overlay-start
64 (if (featurep 'xemacs
) 'extent-start-position
'overlay-start
))
65 (defalias 'viper-overlay-end
66 (if (featurep 'xemacs
) 'extent-end-position
'overlay-end
))
67 (defalias 'viper-overlay-get
68 (if (featurep 'xemacs
) 'extent-property
'overlay-get
))
69 (defalias 'viper-overlay-put
70 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
71 (defalias 'viper-read-event
72 (if (featurep 'xemacs
) 'next-command-event
'read-event
))
73 (defalias 'viper-characterp
74 (if (featurep 'xemacs
) 'characterp
'integerp
))
75 (defalias 'viper-int-to-char
76 (if (featurep 'xemacs
) 'int-to-char
'identity
))
77 (defalias 'viper-get-face
78 (if (featurep 'xemacs
) 'get-face
'facep
))
79 (defalias 'viper-color-defined-p
80 (if (featurep 'xemacs
) 'valid-color-name-p
'x-color-defined-p
))
81 (defalias 'viper-iconify
82 (if (featurep 'xemacs
) 'iconify-frame
'iconify-or-deiconify-frame
))
85 ;; CHAR is supposed to be a char or an integer (positive or negative)
86 ;; LIST is a list of chars, nil, and negative numbers
87 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
88 ;; Introduced for compatibility with XEmacs, where integers are not the same as
90 (defun viper-memq-char (char list
)
91 (cond ((and (integerp char
) (>= char
0))
92 (memq (viper-int-to-char char
) list
))
95 ;; Check if char-or-int and char are the same as characters
96 (defun viper-char-equal (char-or-int char
)
97 (cond ((and (integerp char-or-int
) (>= char-or-int
0))
98 (= (viper-int-to-char char-or-int
) char
))
99 ((eq char-or-int char
))))
101 ;; Like =, but accommodates null and also is t for eq-objects
102 (defun viper= (char char1
)
103 (cond ((eq char char1
) t
)
104 ((and (viper-characterp char
) (viper-characterp char1
))
108 (defsubst viper-color-display-p
()
109 (if (featurep 'xemacs
) (eq (device-class (selected-device)) 'color
)
110 (x-display-color-p)))
112 (defun viper-get-cursor-color (&optional frame
)
113 (if (featurep 'xemacs
)
115 (frame-property (or frame
(selected-frame)) 'cursor-color
))
116 (cdr (assoc 'cursor-color
(frame-parameters)))))
118 (defmacro viper-frame-value
(variable)
119 "Return the value of VARIABLE local to the current frame, if there is one.
120 Otherwise return the normal value."
121 `(if (featurep 'xemacs
)
123 ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
124 ;; so we do it by hand instead.
125 ;; Buffer-local values take precedence over frame-local ones.
126 (if (local-variable-p ',variable
)
128 ;; Distinguish between no frame parameter and a frame parameter
129 ;; with a value of nil.
130 (let ((fp (assoc ',variable
(frame-parameters))))
135 (defun viper-change-cursor-color (new-color &optional frame
)
136 (if (and (viper-window-display-p) (viper-color-display-p)
137 (stringp new-color
) (viper-color-defined-p new-color
)
138 (not (string= new-color
(viper-get-cursor-color))))
139 (if (featurep 'xemacs
)
141 (or frame
(selected-frame))
142 'cursor-color
(make-color-instance new-color
))
143 (modify-frame-parameters
144 (or frame
(selected-frame))
145 (list (cons 'cursor-color new-color
))))))
147 ;; Note that the colors this function uses might not be those
148 ;; associated with FRAME, if there are frame-local values.
149 ;; This was equally true before the advent of viper-frame-value.
150 ;; Now it could be changed by passing frame to v-f-v.
151 (defun viper-set-cursor-color-according-to-state (&optional frame
)
152 (cond ((eq viper-current-state
'replace-state
)
153 (viper-change-cursor-color
154 (viper-frame-value viper-replace-overlay-cursor-color
)
156 ((and (eq viper-current-state
'emacs-state
)
157 (viper-frame-value viper-emacs-state-cursor-color
))
158 (viper-change-cursor-color
159 (viper-frame-value viper-emacs-state-cursor-color
)
161 ((eq viper-current-state
'insert-state
)
162 (viper-change-cursor-color
163 (viper-frame-value viper-insert-state-cursor-color
)
166 (viper-change-cursor-color
167 (viper-frame-value viper-vi-state-cursor-color
)
170 ;; By default, saves current frame cursor color before changing viper state
171 (defun viper-save-cursor-color (before-which-mode)
172 (if (and (viper-window-display-p) (viper-color-display-p))
173 (let ((color (viper-get-cursor-color)))
174 (if (and (stringp color
) (viper-color-defined-p color
)
175 ;; there is something fishy in that the color is not saved if
176 ;; it is the same as frames default cursor color. need to be
180 viper-replace-overlay-cursor-color
))))
181 (modify-frame-parameters
185 (cond ((eq before-which-mode
'before-replace-mode
)
186 'viper-saved-cursor-color-in-replace-mode
)
187 ((eq before-which-mode
'before-emacs-mode
)
188 'viper-saved-cursor-color-in-emacs-mode
)
190 'viper-saved-cursor-color-in-insert-mode
))
194 (defun viper-get-saved-cursor-color-in-replace-mode ()
197 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
199 'viper-saved-cursor-color-in-replace-mode
)
200 (or (and (eq viper-current-state
'emacs-mode
)
201 (viper-frame-value viper-emacs-state-cursor-color
))
202 (viper-frame-value viper-vi-state-cursor-color
))))
204 (defun viper-get-saved-cursor-color-in-insert-mode ()
207 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
209 'viper-saved-cursor-color-in-insert-mode
)
210 (or (and (eq viper-current-state
'emacs-mode
)
211 (viper-frame-value viper-emacs-state-cursor-color
))
212 (viper-frame-value viper-vi-state-cursor-color
))))
214 (defun viper-get-saved-cursor-color-in-emacs-mode ()
217 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
219 'viper-saved-cursor-color-in-emacs-mode
)
220 (viper-frame-value viper-vi-state-cursor-color
)))
222 ;; restore cursor color from replace overlay
223 (defun viper-restore-cursor-color(after-which-mode)
224 (if (viper-overlay-p viper-replace-overlay
)
225 (viper-change-cursor-color
226 (cond ((eq after-which-mode
'after-replace-mode
)
227 (viper-get-saved-cursor-color-in-replace-mode))
228 ((eq after-which-mode
'after-emacs-mode
)
229 (viper-get-saved-cursor-color-in-emacs-mode))
230 (t (viper-get-saved-cursor-color-in-insert-mode)))
234 ;; Check the current version against the major and minor version numbers
235 ;; using op: cur-vers op major.minor If emacs-major-version or
236 ;; emacs-minor-version are not defined, we assume that the current version
237 ;; is hopelessly outdated. We assume that emacs-major-version and
238 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
239 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
240 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
241 ;; incorrect. However, this gives correct result in our cases, since we are
242 ;; testing for sufficiently high Emacs versions.
243 (defun viper-check-version (op major minor
&optional type-of-emacs
)
244 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
245 (and (cond ((eq type-of-emacs
'xemacs
) (featurep 'xemacs
))
246 ((eq type-of-emacs
'emacs
) (featurep 'emacs
))
248 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
249 (= emacs-major-version major
)))
250 ((memq op
'(> >= < <=))
251 (and (or (funcall op emacs-major-version major
)
252 (= emacs-major-version major
))
253 (if (= emacs-major-version major
)
254 (funcall op emacs-minor-version minor
)
257 (error "%S: Invalid op in viper-check-version" op
))))
258 (cond ((memq op
'(= > >=)) nil
)
259 ((memq op
'(< <=)) t
))))
262 (defun viper-get-visible-buffer-window (wind)
263 (if (featurep 'xemacs
)
264 (get-buffer-window wind t
)
265 (get-buffer-window wind
'visible
)))
268 ;; Return line position.
269 ;; If pos is 'start then returns position of line start.
270 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
271 ;; Pos = 'indent returns beginning of indentation.
272 ;; Otherwise, returns point. Current point is not moved in any case."
273 (defun viper-line-pos (pos)
274 (let ((cur-pos (point))
282 (goto-char (+ (viper-line-pos 'start
) (viper-line-pos 'end
) 2)))
284 (back-to-indentation))
286 (setq result
(point))
290 ;; Emacs used to count each multibyte character as several positions in the buffer,
291 ;; so we had to use Emacs's chars-in-region to count characters. Since 20.3,
292 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
293 ;; counting each char as just one pos. So, now we can simply subtract beg from
294 ;; end to determine the number of characters in a region.
295 (defun viper-chars-in-region (beg end
&optional preserve-sign
)
296 ;;(let ((count (abs (if (fboundp 'chars-in-region)
297 ;; (chars-in-region beg end)
299 (let ((count (abs (- end beg
))))
300 (if (and (< end beg
) preserve-sign
)
304 ;; Test if POS is between BEG and END
305 (defsubst viper-pos-within-region
(pos beg end
)
306 (and (>= pos
(min beg end
)) (>= (max beg end
) pos
)))
309 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
310 ;; The first argument must eval to a variable name.
311 ;; Arguments: (var-name position &optional buffer).
313 ;; This is useful for moving markers that are supposed to be local.
314 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
315 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
316 ;; buffer, a new marker will be created.
317 (defun viper-move-marker-locally (var pos
&optional buffer
)
318 (if (markerp (eval var
))
320 (set var
(make-marker)))
321 (move-marker (eval var
) pos buffer
))
324 ;; Print CONDITIONS as a message.
325 (defun viper-message-conditions (conditions)
326 (let ((case (car conditions
)) (msg (cdr conditions
)))
329 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
334 ;;; List/alist utilities
336 ;; Convert LIST to an alist
337 (defun viper-list-to-alist (lst)
340 (setq alist
(cons (list (car lst
)) alist
))
341 (setq lst
(cdr lst
)))
344 ;; Convert ALIST to a list.
345 (defun viper-alist-to-list (alst)
348 (setq lst
(cons (car (car alst
)) lst
))
349 (setq alst
(cdr alst
)))
352 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
353 (defun viper-filter-alist (regexp alst
)
355 (let ((outalst) (inalst alst
))
357 (if (string-match regexp
(car (car inalst
)))
358 (setq outalst
(cons (car inalst
) outalst
)))
359 (setq inalst
(cdr inalst
)))
362 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
363 (defun viper-filter-list (regexp lst
)
365 (let ((outlst) (inlst lst
))
367 (if (string-match regexp
(car inlst
))
368 (setq outlst
(cons (car inlst
) outlst
)))
369 (setq inlst
(cdr inlst
)))
373 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
374 ;; LIS2 is modified by filtering it: deleting its members of the form
375 ;; (car elt) such that (car elt') is in LIS1.
376 (defun viper-append-filter-alist (lis1 lis2
)
379 ;;filter-append the second list
381 ;; delete all occurrences
382 (while (setq elt
(assoc (car (car temp
)) lis2
))
383 (setq lis2
(delq elt lis2
)))
384 (setq temp
(cdr temp
)))
390 (declare-function viper-forward-Word
"viper-cmd" (arg))
392 ;;; Support for :e, :r, :w file globbing
394 ;; Glob the file spec.
395 ;; This function is designed to work under Unix.
396 (defun viper-glob-unix-files (filespec)
398 (cond (ex-unix-type-shell shell-file-name
)
399 (t "sh"))) ; probably Unix anyway
401 ;; using cond in anticipation of further additions
402 (cond (ex-unix-type-shell-options)
404 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec
))
405 (t (format "ls -1 -d %s" filespec
))))
407 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name
)
411 (call-process gshell nil t nil
415 (call-process gshell nil t nil
418 (goto-char (point-min))
419 ;; Issue an error, if no match.
420 (unless (eq 0 status
)
422 (skip-chars-forward " \t\n")
423 (if (looking-at "ls:")
424 (viper-forward-Word 1))
429 (buffer-substring (point) (viper-line-pos 'end
)))
431 (goto-char (point-min))
432 (viper-get-filenames-from-buffer 'one-per-line
))
436 ;; Interpret the stuff in the buffer as a list of file names
437 ;; return a list of file names listed in the buffer beginning at point
438 ;; If optional arg is supplied, assume each filename is listed on a separate
440 (defun viper-get-filenames-from-buffer (&optional one-per-line
)
441 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
443 (skip-chars-forward skip-chars
)
445 (if (cond ((looking-at "\"")
447 (re-search-forward "[^\"]+" nil t
)) ; noerror
450 (re-search-forward "[^']+" nil t
)) ; noerror
453 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
455 (buffer-substring (match-beginning 0) (match-end 0))))
458 (skip-chars-forward " \t\n")
459 (setq result
(cons fname result
)))
462 ;; convert MS-DOS wildcards to regexp
463 (defun viper-wildcard-to-regexp (wcard)
464 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name
)
467 (goto-char (point-min))
469 (skip-chars-forward "^*?.\\\\")
470 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
471 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
472 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
473 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
479 ;; glob windows files
480 ;; LIST is expected to be in reverse order
481 (defun viper-glob-mswindows-files (filespec)
482 (let ((case-fold-search t
)
484 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name
)
487 (goto-char (point-min))
488 (setq tmp
(viper-get-filenames-from-buffer))
490 (setq tmp2
(cons (directory-files
491 ;; the directory part
492 (or (file-name-directory (car tmp
))
494 t
; return full names
495 ;; the regexp part: globs the file names
497 (viper-wildcard-to-regexp
498 (file-name-nondirectory (car tmp
)))
501 (setq tmp
(cdr tmp
)))
502 (reverse (apply 'append tmp2
)))))
507 ;; Rotate RING's index. DIRection can be positive or negative.
508 (defun viper-ring-rotate1 (ring dir
)
509 (if (and (ring-p ring
) (> (ring-length ring
) 0))
511 (setcar ring
(cond ((> dir
0)
512 (ring-plus1 (car ring
) (ring-length ring
)))
514 (ring-minus1 (car ring
) (ring-length ring
)))
515 ;; don't rotate if dir = 0
517 (viper-current-ring-item ring
)
520 (defun viper-special-ring-rotate1 (ring dir
)
521 (if (memq viper-intermediate-command
522 '(repeating-display-destructive-command
523 repeating-insertion-from-ring
))
524 (viper-ring-rotate1 ring dir
)
525 ;; don't rotate otherwise
526 (viper-ring-rotate1 ring
0)))
528 ;; current ring item; if N is given, then so many items back from the
530 (defun viper-current-ring-item (ring &optional n
)
532 (if (and (ring-p ring
) (> (ring-length ring
) 0))
533 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
535 ;; Push item onto ring. The second argument is a ring-variable, not value.
536 (defun viper-push-onto-ring (item ring-var
)
537 (or (ring-p (eval ring-var
))
538 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
539 (or (null item
) ; don't push nil
540 (and (stringp item
) (string= item
"")) ; or empty strings
541 (equal item
(viper-current-ring-item (eval ring-var
))) ; or old stuff
542 ;; Since viper-set-destructive-command checks if we are inside
543 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
544 ;; cmd viper-repeat makes a call to the current function only if `.' is
545 ;; executing a command from the command history. It doesn't call the
546 ;; push-onto-ring function if `.' is simply repeating the last
547 ;; destructive command. We only check for ESC (which happens when we do
548 ;; insert with a prefix argument, or if this-command-keys doesn't give
549 ;; anything meaningful (in that case we don't know what to show to the
551 (and (eq ring-var
'viper-command-ring
)
552 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
553 (viper-array-to-string (this-command-keys))))
554 (viper-ring-insert (eval ring-var
) item
))
558 ;; removing elts from ring seems to break it
559 (defun viper-cleanup-ring (ring)
560 (or (< (ring-length ring
) 2)
561 (null (viper-current-ring-item ring
))
562 ;; last and previous equal
563 (if (equal (viper-current-ring-item ring
)
564 (viper-current-ring-item ring
1))
565 (viper-ring-pop ring
))))
567 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
568 (defun viper-ring-pop (ring)
569 (let* ((ln (ring-length ring
))
570 (vec (cdr (cdr ring
)))
571 (veclen (length vec
))
573 (idx (max 0 (ring-minus1 hd ln
)))
574 (top-elt (aref vec idx
)))
577 (while (< (1+ idx
) veclen
)
578 (aset vec idx
(aref vec
(1+ idx
)))
582 (setq hd
(max 0 (ring-minus1 hd ln
)))
583 (if (= hd
(1- ln
)) (setq hd
0))
584 (setcar ring hd
) ; move head
585 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
589 (defun viper-ring-insert (ring item
)
590 (let* ((ln (ring-length ring
))
591 (vec (cdr (cdr ring
)))
592 (veclen (length vec
))
594 (vecpos-after-hd (if (= hd
0) ln hd
))
599 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
600 (setcar ring
(ring-plus1 hd ln
)))
601 (setcar (cdr ring
) (1+ ln
))
602 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
603 (while (and (>= idx vecpos-after-hd
) (> ln
0))
604 (aset vec idx
(aref vec
(1- idx
)))
606 (aset vec vecpos-after-hd item
))
612 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
613 ;; PRE-STRING is a string to prepend to the abbrev string.
614 ;; POST-STRING is a string to append to the abbrev string.
615 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
616 ;; if the orig string was truncated.
617 (defun viper-abbreviate-string (string max-len
618 pre-string post-string abbrev-sign
)
622 (substring string
0 (min max-len
(length string
)))))
623 (cond ((null truncated-str
) "")
624 ((> (length string
) max-len
)
626 pre-string truncated-str abbrev-sign post-string
))
627 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
629 ;; tells if we are over a whitespace-only line
630 (defsubst viper-over-whitespace-line
()
633 (looking-at "^[ \t]*$")))
636 ;;; Saving settings in custom file
638 ;; Save the current setting of VAR in CUSTOM-FILE.
639 ;; If given, MESSAGE is a message to be displayed after that.
640 ;; This message is erased after 2 secs, if erase-msg is non-nil.
641 ;; Arguments: var message custom-file &optional erase-message
642 (defun viper-save-setting (var message custom-file
&optional erase-msg
)
643 (let* ((var-name (symbol-name var
))
644 (var-val (if (boundp var
) (eval var
)))
645 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
646 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
648 (message "%s" (or message
""))
649 (with-current-buffer buf
650 (goto-char (point-min))
651 (if (re-search-forward regexp nil t
)
652 (let ((reg-end (1- (match-end 0))))
653 (search-backward var-name
)
654 (delete-region (match-beginning 0) reg-end
)
655 (goto-char (match-beginning 0))
656 (insert (format "%s '%S" var-name var-val
)))
657 (goto-char (point-max))
658 (if (not (bolp)) (insert "\n"))
659 (insert (format "(setq %s '%S)\n" var-name var-val
)))
668 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
669 ;; match this pattern.
670 (defun viper-save-string-in-file (string custom-file
&optional pattern
)
671 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
672 (with-current-buffer buf
673 (let (buffer-read-only)
674 (goto-char (point-min))
675 (if pattern
(delete-matching-lines pattern
))
676 (goto-char (point-max))
677 (if string
(insert string
))
683 ;; This is a simple-minded check for whether a file is under version control.
684 ;; If file,v exists but file doesn't, this file is considered to be not checked
685 ;; in and not checked out for the purpose of patching (since patch won't be
686 ;; able to read such a file anyway).
687 ;; FILE is a string representing file name
688 ;;(defun viper-file-under-version-control (file)
689 ;; (let* ((filedir (file-name-directory file))
690 ;; (file-nondir (file-name-nondirectory file))
691 ;; (trial (concat file-nondir ",v"))
692 ;; (full-trial (concat filedir trial))
693 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
694 ;; (and (stringp file)
695 ;; (file-exists-p file)
698 ;; (file-exists-p full-trial)
699 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
700 ;; ;; don't be fooled by this!
701 ;; (not (equal (file-attributes file)
702 ;; (file-attributes full-trial))))
703 ;; ;; check if a version is in RCS/ directory
704 ;; (file-exists-p full-rcs-trial)))
708 (defsubst viper-file-checked-in-p
(file)
709 (and (featurep 'vc-hooks
)
710 ;; CVS files are considered not checked in
711 ;; FIXME: Should this deal with more than CVS?
712 (not (memq (vc-backend file
) '(nil CVS
)))
713 (if (fboundp 'vc-state
)
715 (not (memq (vc-state file
) '(edited needs-merge
)))
716 (not (stringp (vc-state file
))))
717 ;; XEmacs has no vc-state
718 (if (featurep 'xemacs
) (not (vc-locking-user file
))))))
720 ;; checkout if visited file is checked in
721 (defun viper-maybe-checkout (buf)
722 (let ((file (expand-file-name (buffer-file-name buf
)))
723 (checkout-function (key-binding "\C-x\C-q")))
724 (if (and (viper-file-checked-in-p file
)
728 "File %s is checked in. Check it out? "
729 (viper-abbreviate-file-name file
))))
730 (with-current-buffer buf
731 (command-execute checkout-function
)))))
737 (defun viper-put-on-search-overlay (beg end
)
738 (if (viper-overlay-p viper-search-overlay
)
739 (viper-move-overlay viper-search-overlay beg end
)
740 (setq viper-search-overlay
(viper-make-overlay beg end
(current-buffer)))
742 viper-search-overlay
'priority viper-search-overlay-priority
))
743 (viper-overlay-put viper-search-overlay
'face viper-search-face
))
747 (defun viper-flash-search-pattern ()
748 (if (not (viper-has-face-support-p))
750 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
752 (viper-overlay-put viper-search-overlay
'face nil
)))
754 (defun viper-hide-search-overlay ()
755 (if (not (viper-overlay-p viper-search-overlay
))
757 (setq viper-search-overlay
758 (viper-make-overlay (point-min) (point-min) (current-buffer)))
760 viper-search-overlay
'priority viper-search-overlay-priority
)))
761 (viper-overlay-put viper-search-overlay
'face nil
))
765 (defsubst viper-move-replace-overlay
(beg end
)
766 (viper-move-overlay viper-replace-overlay beg end
))
768 (defun viper-set-replace-overlay (beg end
)
769 (if (viper-overlay-live-p viper-replace-overlay
)
770 (viper-move-replace-overlay beg end
)
771 (setq viper-replace-overlay
(viper-make-overlay beg end
(current-buffer)))
774 viper-replace-overlay
(if (featurep 'emacs
) 'evaporate
'detachable
) nil
)
776 viper-replace-overlay
'priority viper-replace-overlay-priority
)
777 ;; If Emacs will start supporting overlay maps, as it currently supports
778 ;; text-property maps, we could do away with viper-replace-minor-mode and
779 ;; just have keymap attached to replace overlay.
781 ;; viper-replace-overlay
782 ;; (if (featurep 'xemacs) 'keymap 'local-map)
783 ;; viper-replace-map)
785 (if (viper-has-face-support-p)
787 viper-replace-overlay
'face viper-replace-overlay-face
))
788 (viper-save-cursor-color 'before-replace-mode
)
789 (viper-change-cursor-color
790 (viper-frame-value viper-replace-overlay-cursor-color
)))
793 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph
)
794 (or (viper-overlay-live-p viper-replace-overlay
)
795 (viper-set-replace-overlay (point-min) (point-min)))
796 (if (or (not (viper-has-face-support-p))
797 viper-use-replace-region-delimiters
)
798 (let ((before-name (if (featurep 'xemacs
) 'begin-glyph
'before-string
))
799 (after-name (if (featurep 'xemacs
) 'end-glyph
'after-string
)))
800 (viper-overlay-put viper-replace-overlay before-name before-glyph
)
801 (viper-overlay-put viper-replace-overlay after-name after-glyph
))))
803 (defun viper-hide-replace-overlay ()
804 (viper-set-replace-overlay-glyphs nil nil
)
805 (viper-restore-cursor-color 'after-replace-mode
)
806 (viper-restore-cursor-color 'after-insert-mode
)
807 (if (viper-has-face-support-p)
808 (viper-overlay-put viper-replace-overlay
'face nil
)))
811 (defsubst viper-replace-start
()
812 (viper-overlay-start viper-replace-overlay
))
813 (defsubst viper-replace-end
()
814 (viper-overlay-end viper-replace-overlay
))
819 (defun viper-set-minibuffer-overlay ()
820 (viper-check-minibuffer-overlay)
821 (when (viper-has-face-support-p)
823 viper-minibuffer-overlay
'face viper-minibuffer-current-face
)
825 viper-minibuffer-overlay
'priority viper-minibuffer-overlay-priority
)
828 viper-minibuffer-overlay
829 (if (featurep 'emacs
) 'evaporate
'detachable
)
831 ;; make viper-minibuffer-overlay open-ended
832 ;; In emacs, it is made open ended at creation time
833 (when (featurep 'xemacs
)
834 (viper-overlay-put viper-minibuffer-overlay
'start-open nil
)
835 (viper-overlay-put viper-minibuffer-overlay
'end-open nil
))))
837 (defun viper-check-minibuffer-overlay ()
838 (if (viper-overlay-live-p viper-minibuffer-overlay
)
840 viper-minibuffer-overlay
841 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
843 (setq viper-minibuffer-overlay
844 (if (featurep 'xemacs
)
845 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
846 ;; make overlay open-ended
848 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
850 (current-buffer) nil
'rear-advance
)))))
853 (defsubst viper-is-in-minibuffer
()
855 (string-match "\\*Minibuf-" (buffer-name))))
859 ;;; XEmacs compatibility
861 (defun viper-abbreviate-file-name (file)
862 (if (featurep 'xemacs
)
863 (abbreviate-file-name file t
) ; XEmacs requires addl argument
864 (abbreviate-file-name file
)))
866 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
867 ;; in sit-for, so this function smooths out the differences.
868 (defsubst viper-sit-for-short
(val &optional nodisp
)
869 (sit-for (/ val
1000.0) nodisp
))
871 ;; EVENT may be a single event of a sequence of events
872 (defsubst viper-ESC-event-p
(event)
873 (let ((ESC-keys '(?\e
(control \
[) escape
))
874 (key (viper-event-key event
)))
875 (member key ESC-keys
)))
877 ;; checks if object is a marker, has a buffer, and points to within that buffer
878 (defun viper-valid-marker (marker)
879 (if (and (markerp marker
) (marker-buffer marker
))
880 (let ((buf (marker-buffer marker
))
881 (pos (marker-position marker
)))
882 (with-current-buffer buf
883 (and (<= pos
(point-max)) (<= (point-min) pos
))))))
885 (defsubst viper-mark-marker
()
886 (if (featurep 'xemacs
) (mark-marker t
)
889 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
890 ;; is the same as (mark t).
891 (defsubst viper-set-mark-if-necessary
()
892 (setq mark-ring
(delete (viper-mark-marker) mark-ring
))
893 (set-mark-command nil
)
894 (setq viper-saved-mark
(point)))
896 ;; In transient mark mode (zmacs mode), it is annoying when regions become
897 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
898 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
899 (defun viper-deactivate-mark ()
900 (if (featurep 'xemacs
)
901 (zmacs-deactivate-region)
904 (defsubst viper-leave-region-active
()
905 (if (featurep 'xemacs
) (setq zmacs-region-stays t
)))
907 ;; Check if arg is a valid character for register
908 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
909 ;; Letter means lowercase letters, Letter means uppercase letters, and
910 ;; digit means digits from 1 to 9.
911 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
912 (defun viper-valid-register (reg &optional type
)
913 (or type
(setq type
'(letter Letter digit
)))
914 (or (if (memq 'letter type
)
915 (and (<= ?a reg
) (<= reg ?z
)))
916 (if (memq 'digit type
)
917 (and (<= ?
1 reg
) (<= reg ?
9)))
918 (if (memq 'Letter type
)
919 (and (<= ?A reg
) (<= reg ?Z
)))
924 ;; it is suggested that an event must be copied before it is assigned to
925 ;; last-command-event in XEmacs
926 (defun viper-copy-event (event)
927 (if (featurep 'xemacs
) (copy-event event
)
930 ;; Uses different timeouts for ESC-sequences and others
931 (defun viper-fast-keysequence-p ()
932 (not (viper-sit-for-short
933 (if (viper-ESC-event-p last-input-event
)
934 (viper-ESC-keyseq-timeout)
935 viper-fast-keyseq-timeout
)
938 ;; like read-event, but in XEmacs also try to convert to char, if possible
939 (defun viper-read-event-convert-to-char ()
941 (if (featurep 'xemacs
)
943 (setq event
(next-command-event))
944 (or (event-to-character event
)
948 ;; Viperized read-key-sequence
949 (defun viper-read-key-sequence (prompt &optional continue-echo
)
950 (let (inhibit-quit event keyseq
)
951 (setq keyseq
(read-key-sequence prompt continue-echo
))
952 (setq event
(if (featurep 'xemacs
)
953 (elt keyseq
0) ; XEmacs returns vector of events
954 (elt (listify-key-sequence keyseq
) 0)))
955 (if (viper-ESC-event-p event
)
956 (let (unread-command-events)
957 (if (viper-fast-keysequence-p)
958 (let ((viper-vi-global-user-minor-mode nil
)
959 (viper-vi-local-user-minor-mode nil
)
960 (viper-vi-intercept-minor-mode nil
)
961 (viper-insert-intercept-minor-mode nil
)
962 (viper-replace-minor-mode nil
) ; actually unnecessary
963 (viper-insert-global-user-minor-mode nil
)
964 (viper-insert-local-user-minor-mode nil
))
965 ;; Note: set unread-command-events only after testing for fast
966 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
967 ;; always t -- whether there is anything after ESC or not
968 (viper-set-unread-command-events keyseq
)
969 (setq keyseq
(read-key-sequence nil
)))
970 (viper-set-unread-command-events keyseq
)
971 (setq keyseq
(read-key-sequence nil
)))))
975 ;; This function lets function-key-map convert key sequences into logical
976 ;; keys. This does a better job than viper-read-event when it comes to kbd
977 ;; macros, since it enables certain macros to be shared between X and TTY modes
978 ;; by correctly mapping key sequences for Left/Right/... (on an ascii
979 ;; terminal) into logical keys left, right, etc.
980 (defun viper-read-key () ;; FIXME: Use `read-key'?
981 (let ((overriding-local-map viper-overriding-map
)
984 (use-global-map viper-overriding-map
)
986 (setq key
(elt (viper-read-key-sequence nil
) 0))
987 (use-global-map global-map
))
991 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
992 ;; instead of nil, if '(nil) was previously inadvertently assigned to
993 ;; unread-command-events
994 (defun viper-event-key (event)
995 (or (and event
(eventp event
))
996 (error "viper-event-key: Wrong type argument, eventp, %S" event
))
997 (when (if (featurep 'xemacs
)
998 (or (key-press-event-p event
) (mouse-event-p event
)) ; xemacs
1001 (let ((mod (event-modifiers event
))
1004 (if (featurep 'xemacs
)
1006 (cond ((key-press-event-p event
)
1008 ((button-event-p event
)
1009 (concat "mouse-" (prin1-to-string (event-button event
))))
1011 (error "viper-event-key: Unknown event, %S" event
)))
1012 ;; Emacs doesn't handle capital letters correctly, since
1013 ;; \S-a isn't considered the same as A (it behaves as
1014 ;; plain `a' instead). So we take care of this here
1015 (cond ((and (viper-characterp event
) (<= ?A event
) (<= event ?Z
))
1018 ;; Emacs has the oddity whereby characters 128+char
1019 ;; represent M-char *if* this appears inside a string.
1020 ;; So, we convert them manually to (meta char).
1021 ((and (viper-characterp event
)
1022 (< ?\C-? event
) (<= event
255))
1024 event
(- event ?\C-?
1)))
1025 ((and (null mod
) (eq event
'return
))
1027 ((and (null mod
) (eq event
'space
))
1029 ((and (null mod
) (eq event
'delete
))
1031 ((and (null mod
) (eq event
'backspace
))
1033 (t (event-basic-type event
)))
1034 ) ; (featurep 'xemacs)
1036 (if (viper-characterp basis
)
1038 (if (viper= basis ?\C-?
)
1039 (list 'control
'\?) ; taking care of an emacs bug
1040 (intern (char-to-string basis
)))))
1042 (append mod
(list basis
))
1045 (defun viper-last-command-char ()
1046 (if (featurep 'xemacs
)
1047 (event-to-character last-command-event
)
1048 last-command-event
))
1050 (defun viper-key-to-emacs-key (key)
1051 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
1052 (cond ((featurep 'xemacs
) key
)
1055 (setq key-name
(symbol-name key
))
1056 (cond ((= (length key-name
) 1) ; character event
1057 (string-to-char key-name
))
1058 ;; Emacs doesn't recognize `return' and `escape' as events on
1059 ;; dumb terminals, so we translate them into characters
1060 ((and (featurep 'emacs
) (not (viper-window-display-p))
1061 (string= key-name
"return"))
1063 ((and (featurep 'emacs
) (not (viper-window-display-p))
1064 (string= key-name
"escape"))
1066 ;; pass symbol-event as is
1070 (setq modifiers
(viper-subseq key
0 (1- (length key
)))
1071 base-key
(viper-seq-last-elt key
)
1072 base-key-name
(symbol-name base-key
)
1073 char-p
(= (length base-key-name
) 1))
1076 (lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1080 (car (read-from-string
1083 (mapconcat 'identity mod-char-list
"-\\")
1089 (mapconcat 'identity mod-char-list
"-")
1095 ;; LIS is assumed to be a list of events of characters
1096 (defun viper-eventify-list-xemacs (lis)
1097 (if (featurep 'xemacs
)
1100 (cond ((viper-characterp elt
) (character-to-event elt
))
1103 "viper-eventify-list-xemacs: can't convert to event, %S"
1108 ;; Smooths out the difference between Emacs's 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)
1117 (if (featurep 'emacs
)
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 (if (featurep 'xemacs
)
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
))))
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 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1246 ;; into characters at some point and then put back to string. The result is
1247 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1248 ;; effect of quoting - and preventing it from being special.
1249 (defconst viper-non-word-characters-reformed-vi
1250 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1251 ;; These are characters that are not to be considered as parts of a word in
1253 ;; Set each time state changes and at loading time
1254 (viper-deflocalvar viper-non-word-characters nil
)
1256 ;; must be buffer-local
1257 (viper-deflocalvar viper-ALPHA-char-class
"w"
1258 "String of syntax classes characterizing Viper's alphanumeric symbols.
1259 In addition, the symbol `_' may be considered alphanumeric if
1260 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1262 (defconst viper-strict-ALPHA-chars
"a-zA-Z0-9_"
1263 "Regexp matching the set of alphanumeric characters acceptable to strict
1265 (defconst viper-strict-SEP-chars
" \t\n"
1266 "Regexp matching the set of alphanumeric characters acceptable to strict
1268 (defconst viper-strict-SEP-chars-sans-newline
" \t"
1269 "Regexp matching the set of alphanumeric characters acceptable to strict
1272 (defconst viper-SEP-char-class
" -"
1273 "String of syntax classes for Vi separators.
1274 Usually contains ` ', linefeed, TAB or formfeed.")
1277 ;; Set Viper syntax classes and related variables according to
1278 ;; `viper-syntax-preference'.
1279 (defun viper-update-syntax-classes (&optional set-default
)
1280 (let ((preference (cond ((eq viper-syntax-preference
'emacs
)
1281 "w") ; Viper words have only Emacs word chars
1282 ((eq viper-syntax-preference
'extended
)
1283 "w_") ; Viper words have Emacs word & symbol chars
1284 (t "w"))) ; Viper words are Emacs words plus `_'
1285 (non-word-chars (cond ((eq viper-syntax-preference
'reformed-vi
)
1286 (viper-string-to-list
1287 viper-non-word-characters-reformed-vi
))
1290 (setq-default viper-ALPHA-char-class preference
1291 viper-non-word-characters non-word-chars
)
1292 (setq viper-ALPHA-char-class preference
1293 viper-non-word-characters non-word-chars
))
1296 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1297 ;; is nil. If nil, use setq.
1298 (defun viper-set-syntax-preference (&optional symbol value
)
1299 "Set Viper syntax preference.
1300 If called interactively or if SYMBOL is nil, sets syntax preference in current
1301 buffer. If called non-interactively, preferably via the customization widget,
1302 sets the default value."
1307 "Viper syntax preference: "
1308 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1309 nil
'require-match
)))
1310 (if (stringp value
) (setq value
(intern value
)))
1311 (or (memq value
'(strict-vi reformed-vi extended emacs
))
1312 (error "Invalid Viper syntax preference, %S" value
))
1314 (setq-default viper-syntax-preference value
)
1315 (setq viper-syntax-preference value
))
1316 (viper-update-syntax-classes))
1318 (defcustom viper-syntax-preference
'reformed-vi
1319 "Syntax type characterizing Viper's alphanumeric symbols.
1320 Affects movement and change commands that deal with Vi-style words.
1321 Works best when set in the hooks to various major modes.
1323 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1325 `reformed-vi' means Viper words are like Emacs words \(as determined using
1326 Emacs syntax tables, which are different for different major modes) with two
1327 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1328 symbols like `\\=`', `\\='', `:', `\"', `)', and `{' are excluded.
1329 This behaves very close to `strict-vi', but also works well with non-ASCII
1330 characters from various alphabets.
1332 `extended' means Viper word constituents are symbols that are marked as being
1333 parts of words OR symbols in Emacs syntax tables.
1334 This is most appropriate for major modes intended for editing programs.
1336 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1338 This option is appropriate if you like Emacs-style words."
1339 :type
'(radio (const strict-vi
) (const reformed-vi
)
1340 (const extended
) (const emacs
))
1341 :set
'viper-set-syntax-preference
1343 (make-variable-buffer-local 'viper-syntax-preference
)
1346 ;; addl-chars are characters to be temporarily considered as alphanumerical
1347 (defun viper-looking-at-alpha (&optional addl-chars
)
1348 (or (stringp addl-chars
) (setq addl-chars
""))
1349 (if (eq viper-syntax-preference
'reformed-vi
)
1350 (setq addl-chars
(concat addl-chars
"_")))
1351 (let ((char (char-after (point))))
1353 (if (eq viper-syntax-preference
'strict-vi
)
1354 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars
"]"))
1356 ;; or one of the additional chars being asked to include
1357 (viper-memq-char char
(viper-string-to-list addl-chars
))
1359 ;; not one of the excluded word chars (note:
1360 ;; viper-non-word-characters is a list)
1361 (not (viper-memq-char char viper-non-word-characters
))
1362 ;; char of the Viper-word syntax class
1363 (viper-memq-char (char-syntax char
)
1364 (viper-string-to-list viper-ALPHA-char-class
))))))
1367 (defun viper-looking-at-separator ()
1368 (let ((char (char-after (point))))
1370 (if (eq viper-syntax-preference
'strict-vi
)
1371 (viper-memq-char char
(viper-string-to-list viper-strict-SEP-chars
))
1372 (or (eq char ?
\n) ; RET is always a separator in Vi
1373 (viper-memq-char (char-syntax char
)
1374 (viper-string-to-list viper-SEP-char-class
)))))
1377 (defsubst viper-looking-at-alphasep
(&optional addl-chars
)
1378 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars
)))
1380 (defun viper-skip-alpha-forward (&optional addl-chars
)
1381 (or (stringp addl-chars
) (setq addl-chars
""))
1384 (cond ((eq viper-syntax-preference
'strict-vi
)
1386 (t viper-ALPHA-char-class
))
1387 (cond ((eq viper-syntax-preference
'strict-vi
)
1388 (concat viper-strict-ALPHA-chars addl-chars
))
1391 (defun viper-skip-alpha-backward (&optional addl-chars
)
1392 (or (stringp addl-chars
) (setq addl-chars
""))
1395 (cond ((eq viper-syntax-preference
'strict-vi
)
1397 (t viper-ALPHA-char-class
))
1398 (cond ((eq viper-syntax-preference
'strict-vi
)
1399 (concat viper-strict-ALPHA-chars addl-chars
))
1402 ;; weird syntax tables may confuse strict-vi style
1403 (defsubst viper-skip-all-separators-forward
(&optional within-line
)
1404 (if (eq viper-syntax-preference
'strict-vi
)
1406 (skip-chars-forward viper-strict-SEP-chars-sans-newline
)
1407 (skip-chars-forward viper-strict-SEP-chars
))
1408 (viper-skip-syntax 'forward
1409 viper-SEP-char-class
1410 (or within-line
"\n")
1411 (if within-line
(viper-line-pos 'end
)))))
1413 (defsubst viper-skip-all-separators-backward
(&optional within-line
)
1414 (if (eq viper-syntax-preference
'strict-vi
)
1416 (skip-chars-backward viper-strict-SEP-chars-sans-newline
)
1417 (skip-chars-backward viper-strict-SEP-chars
))
1418 (viper-skip-syntax 'backward
1419 viper-SEP-char-class
1420 (or within-line
"\n")
1421 (if within-line
(viper-line-pos 'start
)))))
1422 (defun viper-skip-nonseparators (direction)
1425 (concat "^" viper-SEP-char-class
)
1427 (viper-line-pos (if (eq direction
'forward
) 'end
'start
))))
1430 ;; skip over non-word constituents and non-separators
1431 (defun viper-skip-nonalphasep-forward ()
1432 (if (eq viper-syntax-preference
'strict-vi
)
1434 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1437 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1438 ;; Emacs may consider some of these as words, but we don't want them
1439 viper-non-word-characters
1440 (viper-line-pos 'end
))))
1442 (defun viper-skip-nonalphasep-backward ()
1443 (if (eq viper-syntax-preference
'strict-vi
)
1444 (skip-chars-backward
1445 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1448 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1449 ;; Emacs may consider some of these as words, but we don't want them
1450 viper-non-word-characters
1451 (viper-line-pos 'start
))))
1453 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1454 ;; Return the number of chars traveled.
1455 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1456 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1457 ;; words, even if Emacs syntax table says they are.
1458 (defun viper-skip-syntax (direction syntax addl-chars
&optional limit
)
1462 (if (eq direction
'forward
)
1463 'skip-chars-forward
'skip-chars-backward
))
1465 (if (eq direction
'forward
)
1466 'viper-forward-char-carefully
'viper-backward-char-carefully
))
1467 char-looked-at syntax-of-char-looked-at negated-syntax
)
1469 (cond ((listp addl-chars
) (viper-charlist-to-string addl-chars
))
1470 ((stringp addl-chars
) addl-chars
)
1473 (cond ((listp syntax
) syntax
)
1474 ((stringp syntax
) (viper-string-to-list syntax
))
1476 (if (memq ?^ syntax
) (setq negated-syntax t
))
1478 (while (and (not (= local
0))
1479 (cond ((eq direction
'forward
)
1482 (setq char-looked-at
(viper-char-at-pos direction
)
1483 ;; if outside the range, set to nil
1484 syntax-of-char-looked-at
(if char-looked-at
1485 (char-syntax char-looked-at
)))
1488 (cond ((and limit
(eq direction
'forward
))
1490 (limit ; backward & limit
1493 ;; char under/before cursor has appropriate syntax
1495 (not (memq syntax-of-char-looked-at syntax
))
1496 (memq syntax-of-char-looked-at syntax
))
1497 ;; if char-syntax class is "word", make sure it is not one
1498 ;; of the excluded characters
1499 (if (and (eq syntax-of-char-looked-at ?w
)
1500 (not negated-syntax
))
1501 (not (viper-memq-char
1502 char-looked-at viper-non-word-characters
))
1504 (funcall skip-syntax-func
1)
1506 (funcall skip-chars-func addl-chars limit
)))
1507 (setq total
(+ total local
)))
1511 ;; tells when point is at the beginning of field
1512 (defun viper-beginning-of-field ()
1514 (not (eq (get-char-property (point) 'field
)
1515 (get-char-property (1- (point)) 'field
)))))
1518 ;; this is copied from cl-extra.el
1519 ;; Return the subsequence of SEQ from START to END.
1520 ;; If END is omitted, it defaults to the length of the sequence.
1521 ;; If START or END is negative, it counts from the end.
1522 (defun viper-subseq (seq start
&optional end
)
1523 (if (stringp seq
) (substring seq start end
)
1525 (and end
(< end
0) (setq end
(+ end
(setq len
(length seq
)))))
1526 (if (< start
0) (setq start
(+ start
(or len
(setq len
(length seq
))))))
1528 (if (> start
0) (setq seq
(nthcdr start seq
)))
1531 (while (>= (setq end
(1- end
)) start
)
1532 (push (pop seq
) res
))
1534 (copy-sequence seq
)))
1536 (or end
(setq end
(or len
(length seq
))))
1537 (let ((res (make-vector (max (- end start
) 0) nil
))
1539 (while (< start end
)
1540 (aset res i
(aref seq start
))
1541 (setq i
(1+ i
) start
(1+ start
)))
1547 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1550 ;;; viper-util.el ends here