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
)
51 (defalias 'viper-overlay-p
52 (if (featurep 'xemacs
) 'extentp
'overlayp
))
53 (defalias 'viper-make-overlay
54 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
55 (defalias 'viper-overlay-live-p
56 (if (featurep 'xemacs
) 'extent-live-p
'overlayp
))
57 (defalias 'viper-move-overlay
58 (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
))
59 (defalias 'viper-overlay-start
60 (if (featurep 'xemacs
) 'extent-start-position
'overlay-start
))
61 (defalias 'viper-overlay-end
62 (if (featurep 'xemacs
) 'extent-end-position
'overlay-end
))
63 (defalias 'viper-overlay-get
64 (if (featurep 'xemacs
) 'extent-property
'overlay-get
))
65 (defalias 'viper-overlay-put
66 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
67 (defalias 'viper-read-event
68 (if (featurep 'xemacs
) 'next-command-event
'read-event
))
69 (defalias 'viper-characterp
70 (if (featurep 'xemacs
) 'characterp
'integerp
))
71 (defalias 'viper-int-to-char
72 (if (featurep 'xemacs
) 'int-to-char
'identity
))
73 (defalias 'viper-get-face
74 (if (featurep 'xemacs
) 'get-face
'facep
))
75 (defalias 'viper-color-defined-p
76 (if (featurep 'xemacs
) 'valid-color-name-p
'x-color-defined-p
))
77 (defalias 'viper-iconify
78 (if (featurep 'xemacs
) 'iconify-frame
'iconify-or-deiconify-frame
))
81 ;; CHAR is supposed to be a char or an integer (positive or negative)
82 ;; LIST is a list of chars, nil, and negative numbers
83 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
84 ;; Introduced for compatibility with XEmacs, where integers are not the same as
86 (defun viper-memq-char (char list
)
87 (cond ((and (integerp char
) (>= char
0))
88 (memq (viper-int-to-char char
) list
))
91 ;; Check if char-or-int and char are the same as characters
92 (defun viper-char-equal (char-or-int char
)
93 (cond ((and (integerp char-or-int
) (>= char-or-int
0))
94 (= (viper-int-to-char char-or-int
) char
))
95 ((eq char-or-int char
))))
97 ;; Like =, but accommodates null and also is t for eq-objects
98 (defun viper= (char char1
)
99 (cond ((eq char char1
) t
)
100 ((and (viper-characterp char
) (viper-characterp char1
))
104 (defsubst viper-color-display-p
()
105 (if (featurep 'xemacs
) (eq (device-class (selected-device)) 'color
)
106 (x-display-color-p)))
108 (defun viper-get-cursor-color (&optional frame
)
109 (if (featurep 'xemacs
)
111 (frame-property (or frame
(selected-frame)) 'cursor-color
))
112 (cdr (assoc 'cursor-color
(frame-parameters)))))
114 (defmacro viper-frame-value
(variable)
115 "Return the value of VARIABLE local to the current frame, if there is one.
116 Otherwise return the normal value."
117 `(if (featurep 'xemacs
)
119 ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
120 ;; so we do it by hand instead.
121 ;; Buffer-local values take precedence over frame-local ones.
122 (if (local-variable-p ',variable
)
124 ;; Distinguish between no frame parameter and a frame parameter
125 ;; with a value of nil.
126 (let ((fp (assoc ',variable
(frame-parameters))))
131 (defun viper-change-cursor-color (new-color &optional frame
)
132 (if (and (viper-window-display-p) (viper-color-display-p)
133 (stringp new-color
) (viper-color-defined-p new-color
)
134 (not (string= new-color
(viper-get-cursor-color))))
135 (if (featurep 'xemacs
)
137 (or frame
(selected-frame))
138 'cursor-color
(make-color-instance new-color
))
139 (modify-frame-parameters
140 (or frame
(selected-frame))
141 (list (cons 'cursor-color new-color
))))))
143 ;; Note that the colors this function uses might not be those
144 ;; associated with FRAME, if there are frame-local values.
145 ;; This was equally true before the advent of viper-frame-value.
146 ;; Now it could be changed by passing frame to v-f-v.
147 (defun viper-set-cursor-color-according-to-state (&optional frame
)
148 (cond ((eq viper-current-state
'replace-state
)
149 (viper-change-cursor-color
150 (viper-frame-value viper-replace-overlay-cursor-color
)
152 ((and (eq viper-current-state
'emacs-state
)
153 (viper-frame-value viper-emacs-state-cursor-color
))
154 (viper-change-cursor-color
155 (viper-frame-value viper-emacs-state-cursor-color
)
157 ((eq viper-current-state
'insert-state
)
158 (viper-change-cursor-color
159 (viper-frame-value viper-insert-state-cursor-color
)
162 (viper-change-cursor-color
163 (viper-frame-value viper-vi-state-cursor-color
)
166 ;; By default, saves current frame cursor color before changing viper state
167 (defun viper-save-cursor-color (before-which-mode)
168 (if (and (viper-window-display-p) (viper-color-display-p))
169 (let ((color (viper-get-cursor-color)))
170 (if (and (stringp color
) (viper-color-defined-p color
)
171 ;; there is something fishy in that the color is not saved if
172 ;; it is the same as frames default cursor color. need to be
176 viper-replace-overlay-cursor-color
))))
177 (modify-frame-parameters
181 (cond ((eq before-which-mode
'before-replace-mode
)
182 'viper-saved-cursor-color-in-replace-mode
)
183 ((eq before-which-mode
'before-emacs-mode
)
184 'viper-saved-cursor-color-in-emacs-mode
)
186 'viper-saved-cursor-color-in-insert-mode
))
190 (defun viper-get-saved-cursor-color-in-replace-mode ()
193 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
195 'viper-saved-cursor-color-in-replace-mode
)
196 (or (and (eq viper-current-state
'emacs-mode
)
197 (viper-frame-value viper-emacs-state-cursor-color
))
198 (viper-frame-value viper-vi-state-cursor-color
))))
200 (defun viper-get-saved-cursor-color-in-insert-mode ()
203 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
205 'viper-saved-cursor-color-in-insert-mode
)
206 (or (and (eq viper-current-state
'emacs-mode
)
207 (viper-frame-value viper-emacs-state-cursor-color
))
208 (viper-frame-value viper-vi-state-cursor-color
))))
210 (defun viper-get-saved-cursor-color-in-emacs-mode ()
213 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
215 'viper-saved-cursor-color-in-emacs-mode
)
216 (viper-frame-value viper-vi-state-cursor-color
)))
218 ;; restore cursor color from replace overlay
219 (defun viper-restore-cursor-color(after-which-mode)
220 (if (viper-overlay-p viper-replace-overlay
)
221 (viper-change-cursor-color
222 (cond ((eq after-which-mode
'after-replace-mode
)
223 (viper-get-saved-cursor-color-in-replace-mode))
224 ((eq after-which-mode
'after-emacs-mode
)
225 (viper-get-saved-cursor-color-in-emacs-mode))
226 (t (viper-get-saved-cursor-color-in-insert-mode)))
230 ;; Check the current version against the major and minor version numbers
231 ;; using op: cur-vers op major.minor If emacs-major-version or
232 ;; emacs-minor-version are not defined, we assume that the current version
233 ;; is hopelessly outdated. We assume that emacs-major-version and
234 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
235 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
236 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
237 ;; incorrect. However, this gives correct result in our cases, since we are
238 ;; testing for sufficiently high Emacs versions.
239 (defun viper-check-version (op major minor
&optional type-of-emacs
)
240 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
241 (and (cond ((eq type-of-emacs
'xemacs
) (featurep 'xemacs
))
242 ((eq type-of-emacs
'emacs
) (featurep 'emacs
))
244 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
245 (= emacs-major-version major
)))
246 ((memq op
'(> >= < <=))
247 (and (or (funcall op emacs-major-version major
)
248 (= emacs-major-version major
))
249 (if (= emacs-major-version major
)
250 (funcall op emacs-minor-version minor
)
253 (error "%S: Invalid op in viper-check-version" op
))))
254 (cond ((memq op
'(= > >=)) nil
)
255 ((memq op
'(< <=)) t
))))
258 (defun viper-get-visible-buffer-window (wind)
259 (if (featurep 'xemacs
)
260 (get-buffer-window wind t
)
261 (get-buffer-window wind
'visible
)))
264 ;; Return line position.
265 ;; If pos is 'start then returns position of line start.
266 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
267 ;; Pos = 'indent returns beginning of indentation.
268 ;; Otherwise, returns point. Current point is not moved in any case."
269 (defun viper-line-pos (pos)
270 (let ((cur-pos (point))
278 (goto-char (+ (viper-line-pos 'start
) (viper-line-pos 'end
) 2)))
280 (back-to-indentation))
282 (setq result
(point))
286 ;; Emacs used to count each multibyte character as several positions in the buffer,
287 ;; so we had to use Emacs's chars-in-region to count characters. Since 20.3,
288 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
289 ;; counting each char as just one pos. So, now we can simply subtract beg from
290 ;; end to determine the number of characters in a region.
291 (defun viper-chars-in-region (beg end
&optional preserve-sign
)
292 ;;(let ((count (abs (if (fboundp 'chars-in-region)
293 ;; (chars-in-region beg end)
295 (let ((count (abs (- end beg
))))
296 (if (and (< end beg
) preserve-sign
)
300 ;; Test if POS is between BEG and END
301 (defsubst viper-pos-within-region
(pos beg end
)
302 (and (>= pos
(min beg end
)) (>= (max beg end
) pos
)))
305 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
306 ;; The first argument must eval to a variable name.
307 ;; Arguments: (var-name position &optional buffer).
309 ;; This is useful for moving markers that are supposed to be local.
310 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
311 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
312 ;; buffer, a new marker will be created.
313 (defun viper-move-marker-locally (var pos
&optional buffer
)
314 (if (markerp (eval var
))
316 (set var
(make-marker)))
317 (move-marker (eval var
) pos buffer
))
320 ;; Print CONDITIONS as a message.
321 (defun viper-message-conditions (conditions)
322 (let ((case (car conditions
)) (msg (cdr conditions
)))
325 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
330 ;;; List/alist utilities
332 ;; Convert LIST to an alist
333 (defun viper-list-to-alist (lst)
336 (setq alist
(cons (list (car lst
)) alist
))
337 (setq lst
(cdr lst
)))
340 ;; Convert ALIST to a list.
341 (defun viper-alist-to-list (alst)
344 (setq lst
(cons (car (car alst
)) lst
))
345 (setq alst
(cdr alst
)))
348 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
349 (defun viper-filter-alist (regexp alst
)
351 (let ((outalst) (inalst alst
))
353 (if (string-match regexp
(car (car inalst
)))
354 (setq outalst
(cons (car inalst
) outalst
)))
355 (setq inalst
(cdr inalst
)))
358 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
359 (defun viper-filter-list (regexp lst
)
361 (let ((outlst) (inlst lst
))
363 (if (string-match regexp
(car inlst
))
364 (setq outlst
(cons (car inlst
) outlst
)))
365 (setq inlst
(cdr inlst
)))
369 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
370 ;; LIS2 is modified by filtering it: deleting its members of the form
371 ;; (car elt) such that (car elt') is in LIS1.
372 (defun viper-append-filter-alist (lis1 lis2
)
375 ;;filter-append the second list
377 ;; delete all occurrences
378 (while (setq elt
(assoc (car (car temp
)) lis2
))
379 (setq lis2
(delq elt lis2
)))
380 (setq temp
(cdr temp
)))
386 (declare-function viper-forward-Word
"viper-cmd" (arg))
388 ;;; Support for :e, :r, :w file globbing
390 ;; Glob the file spec.
391 ;; This function is designed to work under Unix.
392 (defun viper-glob-unix-files (filespec)
394 (cond (ex-unix-type-shell shell-file-name
)
395 (t "sh"))) ; probably Unix anyway
397 ;; using cond in anticipation of further additions
398 (cond (ex-unix-type-shell-options)
400 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec
))
401 (t (format "ls -1 -d %s" filespec
))))
403 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name
)
407 (call-process gshell nil t nil
411 (call-process gshell nil t nil
414 (goto-char (point-min))
415 ;; Issue an error, if no match.
416 (unless (eq 0 status
)
418 (skip-chars-forward " \t\n")
419 (if (looking-at "ls:")
420 (viper-forward-Word 1))
425 (buffer-substring (point) (viper-line-pos 'end
)))
427 (goto-char (point-min))
428 (viper-get-filenames-from-buffer 'one-per-line
))
432 ;; Interpret the stuff in the buffer as a list of file names
433 ;; return a list of file names listed in the buffer beginning at point
434 ;; If optional arg is supplied, assume each filename is listed on a separate
436 (defun viper-get-filenames-from-buffer (&optional one-per-line
)
437 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
439 (skip-chars-forward skip-chars
)
441 (if (cond ((looking-at "\"")
443 (re-search-forward "[^\"]+" nil t
)) ; noerror
446 (re-search-forward "[^']+" nil t
)) ; noerror
449 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
451 (buffer-substring (match-beginning 0) (match-end 0))))
454 (skip-chars-forward " \t\n")
455 (setq result
(cons fname result
)))
458 ;; convert MS-DOS wildcards to regexp
459 (defun viper-wildcard-to-regexp (wcard)
460 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name
)
463 (goto-char (point-min))
465 (skip-chars-forward "^*?.\\\\")
466 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
467 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
468 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
469 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
475 ;; glob windows files
476 ;; LIST is expected to be in reverse order
477 (defun viper-glob-mswindows-files (filespec)
478 (let ((case-fold-search t
)
480 (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name
)
483 (goto-char (point-min))
484 (setq tmp
(viper-get-filenames-from-buffer))
486 (setq tmp2
(cons (directory-files
487 ;; the directory part
488 (or (file-name-directory (car tmp
))
490 t
; return full names
491 ;; the regexp part: globs the file names
493 (viper-wildcard-to-regexp
494 (file-name-nondirectory (car tmp
)))
497 (setq tmp
(cdr tmp
)))
498 (reverse (apply 'append tmp2
)))))
503 ;; Rotate RING's index. DIRection can be positive or negative.
504 (defun viper-ring-rotate1 (ring dir
)
505 (if (and (ring-p ring
) (> (ring-length ring
) 0))
507 (setcar ring
(cond ((> dir
0)
508 (ring-plus1 (car ring
) (ring-length ring
)))
510 (ring-minus1 (car ring
) (ring-length ring
)))
511 ;; don't rotate if dir = 0
513 (viper-current-ring-item ring
)
516 (defun viper-special-ring-rotate1 (ring dir
)
517 (if (memq viper-intermediate-command
518 '(repeating-display-destructive-command
519 repeating-insertion-from-ring
))
520 (viper-ring-rotate1 ring dir
)
521 ;; don't rotate otherwise
522 (viper-ring-rotate1 ring
0)))
524 ;; current ring item; if N is given, then so many items back from the
526 (defun viper-current-ring-item (ring &optional n
)
528 (if (and (ring-p ring
) (> (ring-length ring
) 0))
529 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
531 ;; Push item onto ring. The second argument is a ring-variable, not value.
532 (defun viper-push-onto-ring (item ring-var
)
533 (or (ring-p (eval ring-var
))
534 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
535 (or (null item
) ; don't push nil
536 (and (stringp item
) (string= item
"")) ; or empty strings
537 (equal item
(viper-current-ring-item (eval ring-var
))) ; or old stuff
538 ;; Since viper-set-destructive-command checks if we are inside
539 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
540 ;; cmd viper-repeat makes a call to the current function only if `.' is
541 ;; executing a command from the command history. It doesn't call the
542 ;; push-onto-ring function if `.' is simply repeating the last
543 ;; destructive command. We only check for ESC (which happens when we do
544 ;; insert with a prefix argument, or if this-command-keys doesn't give
545 ;; anything meaningful (in that case we don't know what to show to the
547 (and (eq ring-var
'viper-command-ring
)
548 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
549 (viper-array-to-string (this-command-keys))))
550 (viper-ring-insert (eval ring-var
) item
))
554 ;; removing elts from ring seems to break it
555 (defun viper-cleanup-ring (ring)
556 (or (< (ring-length ring
) 2)
557 (null (viper-current-ring-item ring
))
558 ;; last and previous equal
559 (if (equal (viper-current-ring-item ring
)
560 (viper-current-ring-item ring
1))
561 (viper-ring-pop ring
))))
563 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
564 (defun viper-ring-pop (ring)
565 (let* ((ln (ring-length ring
))
566 (vec (cdr (cdr ring
)))
567 (veclen (length vec
))
569 (idx (max 0 (ring-minus1 hd ln
)))
570 (top-elt (aref vec idx
)))
573 (while (< (1+ idx
) veclen
)
574 (aset vec idx
(aref vec
(1+ idx
)))
578 (setq hd
(max 0 (ring-minus1 hd ln
)))
579 (if (= hd
(1- ln
)) (setq hd
0))
580 (setcar ring hd
) ; move head
581 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
585 (defun viper-ring-insert (ring item
)
586 (let* ((ln (ring-length ring
))
587 (vec (cdr (cdr ring
)))
588 (veclen (length vec
))
590 (vecpos-after-hd (if (= hd
0) ln hd
))
595 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
596 (setcar ring
(ring-plus1 hd ln
)))
597 (setcar (cdr ring
) (1+ ln
))
598 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
599 (while (and (>= idx vecpos-after-hd
) (> ln
0))
600 (aset vec idx
(aref vec
(1- idx
)))
602 (aset vec vecpos-after-hd item
))
608 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
609 ;; PRE-STRING is a string to prepend to the abbrev string.
610 ;; POST-STRING is a string to append to the abbrev string.
611 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
612 ;; if the orig string was truncated.
613 (defun viper-abbreviate-string (string max-len
614 pre-string post-string abbrev-sign
)
618 (substring string
0 (min max-len
(length string
)))))
619 (cond ((null truncated-str
) "")
620 ((> (length string
) max-len
)
622 pre-string truncated-str abbrev-sign post-string
))
623 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
625 ;; tells if we are over a whitespace-only line
626 (defsubst viper-over-whitespace-line
()
629 (looking-at "^[ \t]*$")))
632 ;;; Saving settings in custom file
634 ;; Save the current setting of VAR in CUSTOM-FILE.
635 ;; If given, MESSAGE is a message to be displayed after that.
636 ;; This message is erased after 2 secs, if erase-msg is non-nil.
637 ;; Arguments: var message custom-file &optional erase-message
638 (defun viper-save-setting (var message custom-file
&optional erase-msg
)
639 (let* ((var-name (symbol-name var
))
640 (var-val (if (boundp var
) (eval var
)))
641 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
642 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
644 (message "%s" (or message
""))
645 (with-current-buffer buf
646 (goto-char (point-min))
647 (if (re-search-forward regexp nil t
)
648 (let ((reg-end (1- (match-end 0))))
649 (search-backward var-name
)
650 (delete-region (match-beginning 0) reg-end
)
651 (goto-char (match-beginning 0))
652 (insert (format "%s '%S" var-name var-val
)))
653 (goto-char (point-max))
654 (if (not (bolp)) (insert "\n"))
655 (insert (format "(setq %s '%S)\n" var-name var-val
)))
664 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
665 ;; match this pattern.
666 (defun viper-save-string-in-file (string custom-file
&optional pattern
)
667 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
668 (with-current-buffer buf
669 (let (buffer-read-only)
670 (goto-char (point-min))
671 (if pattern
(delete-matching-lines pattern
))
672 (goto-char (point-max))
673 (if string
(insert string
))
679 ;; This is a simple-minded check for whether a file is under version control.
680 ;; If file,v exists but file doesn't, this file is considered to be not checked
681 ;; in and not checked out for the purpose of patching (since patch won't be
682 ;; able to read such a file anyway).
683 ;; FILE is a string representing file name
684 ;;(defun viper-file-under-version-control (file)
685 ;; (let* ((filedir (file-name-directory file))
686 ;; (file-nondir (file-name-nondirectory file))
687 ;; (trial (concat file-nondir ",v"))
688 ;; (full-trial (concat filedir trial))
689 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
690 ;; (and (stringp file)
691 ;; (file-exists-p file)
694 ;; (file-exists-p full-trial)
695 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
696 ;; ;; don't be fooled by this!
697 ;; (not (equal (file-attributes file)
698 ;; (file-attributes full-trial))))
699 ;; ;; check if a version is in RCS/ directory
700 ;; (file-exists-p full-rcs-trial)))
704 (defsubst viper-file-checked-in-p
(file)
705 (and (featurep 'vc-hooks
)
706 ;; CVS files are considered not checked in
707 ;; FIXME: Should this deal with more than CVS?
708 (not (memq (vc-backend file
) '(nil CVS
)))
709 (if (fboundp 'vc-state
)
711 (not (memq (vc-state file
) '(edited needs-merge
)))
712 (not (stringp (vc-state file
))))
713 ;; XEmacs has no vc-state
714 (if (featurep 'xemacs
) (not (vc-locking-user file
))))))
716 ;; checkout if visited file is checked in
717 (defun viper-maybe-checkout (buf)
718 (let ((file (expand-file-name (buffer-file-name buf
)))
719 (checkout-function (key-binding "\C-x\C-q")))
720 (if (and (viper-file-checked-in-p file
)
724 "File %s is checked in. Check it out? "
725 (viper-abbreviate-file-name file
))))
726 (with-current-buffer buf
727 (command-execute checkout-function
)))))
733 (defun viper-put-on-search-overlay (beg end
)
734 (if (viper-overlay-p viper-search-overlay
)
735 (viper-move-overlay viper-search-overlay beg end
)
736 (setq viper-search-overlay
(viper-make-overlay beg end
(current-buffer)))
738 viper-search-overlay
'priority viper-search-overlay-priority
))
739 (viper-overlay-put viper-search-overlay
'face viper-search-face
))
743 (defun viper-flash-search-pattern ()
744 (if (not (viper-has-face-support-p))
746 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
748 (viper-overlay-put viper-search-overlay
'face nil
)))
750 (defun viper-hide-search-overlay ()
751 (if (not (viper-overlay-p viper-search-overlay
))
753 (setq viper-search-overlay
754 (viper-make-overlay (point-min) (point-min) (current-buffer)))
756 viper-search-overlay
'priority viper-search-overlay-priority
)))
757 (viper-overlay-put viper-search-overlay
'face nil
))
761 (defsubst viper-move-replace-overlay
(beg end
)
762 (viper-move-overlay viper-replace-overlay beg end
))
764 (defun viper-set-replace-overlay (beg end
)
765 (if (viper-overlay-live-p viper-replace-overlay
)
766 (viper-move-replace-overlay beg end
)
767 (setq viper-replace-overlay
(viper-make-overlay beg end
(current-buffer)))
770 viper-replace-overlay
(if (featurep 'emacs
) 'evaporate
'detachable
) nil
)
772 viper-replace-overlay
'priority viper-replace-overlay-priority
)
773 ;; If Emacs will start supporting overlay maps, as it currently supports
774 ;; text-property maps, we could do away with viper-replace-minor-mode and
775 ;; just have keymap attached to replace overlay.
777 ;; viper-replace-overlay
778 ;; (if (featurep 'xemacs) 'keymap 'local-map)
779 ;; viper-replace-map)
781 (if (viper-has-face-support-p)
783 viper-replace-overlay
'face viper-replace-overlay-face
))
784 (viper-save-cursor-color 'before-replace-mode
)
785 (viper-change-cursor-color
786 (viper-frame-value viper-replace-overlay-cursor-color
)))
789 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph
)
790 (or (viper-overlay-live-p viper-replace-overlay
)
791 (viper-set-replace-overlay (point-min) (point-min)))
792 (if (or (not (viper-has-face-support-p))
793 viper-use-replace-region-delimiters
)
794 (let ((before-name (if (featurep 'xemacs
) 'begin-glyph
'before-string
))
795 (after-name (if (featurep 'xemacs
) 'end-glyph
'after-string
)))
796 (viper-overlay-put viper-replace-overlay before-name before-glyph
)
797 (viper-overlay-put viper-replace-overlay after-name after-glyph
))))
799 (defun viper-hide-replace-overlay ()
800 (viper-set-replace-overlay-glyphs nil nil
)
801 (viper-restore-cursor-color 'after-replace-mode
)
802 (viper-restore-cursor-color 'after-insert-mode
)
803 (if (viper-has-face-support-p)
804 (viper-overlay-put viper-replace-overlay
'face nil
)))
807 (defsubst viper-replace-start
()
808 (viper-overlay-start viper-replace-overlay
))
809 (defsubst viper-replace-end
()
810 (viper-overlay-end viper-replace-overlay
))
815 (defun viper-set-minibuffer-overlay ()
816 (viper-check-minibuffer-overlay)
817 (when (viper-has-face-support-p)
819 viper-minibuffer-overlay
'face viper-minibuffer-current-face
)
821 viper-minibuffer-overlay
'priority viper-minibuffer-overlay-priority
)
824 viper-minibuffer-overlay
825 (if (featurep 'emacs
) 'evaporate
'detachable
)
827 ;; make viper-minibuffer-overlay open-ended
828 ;; In emacs, it is made open ended at creation time
829 (when (featurep 'xemacs
)
830 (viper-overlay-put viper-minibuffer-overlay
'start-open nil
)
831 (viper-overlay-put viper-minibuffer-overlay
'end-open nil
))))
833 (defun viper-check-minibuffer-overlay ()
834 (if (viper-overlay-live-p viper-minibuffer-overlay
)
836 viper-minibuffer-overlay
837 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
839 (setq viper-minibuffer-overlay
840 (if (featurep 'xemacs
)
841 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
842 ;; make overlay open-ended
844 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
846 (current-buffer) nil
'rear-advance
)))))
849 (defsubst viper-is-in-minibuffer
()
851 (string-match "\\*Minibuf-" (buffer-name))))
855 ;;; XEmacs compatibility
857 (defun viper-abbreviate-file-name (file)
858 (if (featurep 'xemacs
)
859 (abbreviate-file-name file t
) ; XEmacs requires addl argument
860 (abbreviate-file-name file
)))
862 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
863 ;; in sit-for, so this function smooths out the differences.
864 (defsubst viper-sit-for-short
(val &optional nodisp
)
865 (sit-for (/ val
1000.0) nodisp
))
867 ;; EVENT may be a single event of a sequence of events
868 (defsubst viper-ESC-event-p
(event)
869 (let ((ESC-keys '(?\e
(control \
[) escape
))
870 (key (viper-event-key event
)))
871 (member key ESC-keys
)))
873 ;; checks if object is a marker, has a buffer, and points to within that buffer
874 (defun viper-valid-marker (marker)
875 (if (and (markerp marker
) (marker-buffer marker
))
876 (let ((buf (marker-buffer marker
))
877 (pos (marker-position marker
)))
878 (with-current-buffer buf
879 (and (<= pos
(point-max)) (<= (point-min) pos
))))))
881 (defsubst viper-mark-marker
()
882 (if (featurep 'xemacs
) (mark-marker t
)
885 (defvar viper-saved-mark nil
886 "Where viper saves mark. This mark is resurrected by m^.")
888 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
889 ;; is the same as (mark t).
890 (defsubst viper-set-mark-if-necessary
()
891 (setq mark-ring
(delete (viper-mark-marker) mark-ring
))
892 (set-mark-command nil
)
893 (setq viper-saved-mark
(point)))
895 ;; In transient mark mode (zmacs mode), it is annoying when regions become
896 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
897 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
898 (defun viper-deactivate-mark ()
899 (if (featurep 'xemacs
)
900 (zmacs-deactivate-region)
903 (defsubst viper-leave-region-active
()
904 (if (featurep 'xemacs
) (setq zmacs-region-stays t
)))
906 ;; Check if arg is a valid character for register
907 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
908 ;; Letter means lowercase letters, Letter means uppercase letters, and
909 ;; digit means digits from 1 to 9.
910 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
911 (defun viper-valid-register (reg &optional type
)
912 (or type
(setq type
'(letter Letter digit
)))
913 (or (if (memq 'letter type
)
914 (and (<= ?a reg
) (<= reg ?z
)))
915 (if (memq 'digit type
)
916 (and (<= ?
1 reg
) (<= reg ?
9)))
917 (if (memq 'Letter type
)
918 (and (<= ?A reg
) (<= reg ?Z
)))
923 ;; it is suggested that an event must be copied before it is assigned to
924 ;; last-command-event in XEmacs
925 (defun viper-copy-event (event)
926 (if (featurep 'xemacs
) (copy-event event
)
929 ;; Uses different timeouts for ESC-sequences and others
930 (defun viper-fast-keysequence-p ()
931 (not (viper-sit-for-short
932 (if (viper-ESC-event-p last-input-event
)
933 (viper-ESC-keyseq-timeout)
934 viper-fast-keyseq-timeout
)
937 ;; like read-event, but in XEmacs also try to convert to char, if possible
938 (defun viper-read-event-convert-to-char ()
940 (if (featurep 'xemacs
)
942 (setq event
(next-command-event))
943 (or (event-to-character event
)
947 ;; Viperized read-key-sequence
948 (defun viper-read-key-sequence (prompt &optional continue-echo
)
949 (let (inhibit-quit event keyseq
)
950 (setq keyseq
(read-key-sequence prompt continue-echo
))
951 (setq event
(if (featurep 'xemacs
)
952 (elt keyseq
0) ; XEmacs returns vector of events
953 (elt (listify-key-sequence keyseq
) 0)))
954 (if (viper-ESC-event-p event
)
955 (let (unread-command-events)
956 (if (viper-fast-keysequence-p)
957 (let ((viper-vi-global-user-minor-mode nil
)
958 (viper-vi-local-user-minor-mode nil
)
959 (viper-vi-intercept-minor-mode nil
)
960 (viper-insert-intercept-minor-mode nil
)
961 (viper-replace-minor-mode nil
) ; actually unnecessary
962 (viper-insert-global-user-minor-mode nil
)
963 (viper-insert-local-user-minor-mode nil
))
964 ;; Note: set unread-command-events only after testing for fast
965 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
966 ;; always t -- whether there is anything after ESC or not
967 (viper-set-unread-command-events keyseq
)
968 (setq keyseq
(read-key-sequence nil
)))
969 (viper-set-unread-command-events keyseq
)
970 (setq keyseq
(read-key-sequence nil
)))))
974 ;; This function lets function-key-map convert key sequences into logical
975 ;; keys. This does a better job than viper-read-event when it comes to kbd
976 ;; macros, since it enables certain macros to be shared between X and TTY modes
977 ;; by correctly mapping key sequences for Left/Right/... (on an ascii
978 ;; terminal) into logical keys left, right, etc.
979 (defun viper-read-key () ;; FIXME: Use `read-key'?
980 (let ((overriding-local-map viper-overriding-map
)
983 (use-global-map viper-overriding-map
)
985 (setq key
(elt (viper-read-key-sequence nil
) 0))
986 (use-global-map global-map
))
990 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
991 ;; instead of nil, if '(nil) was previously inadvertently assigned to
992 ;; unread-command-events
993 (defun viper-event-key (event)
994 (or (and event
(eventp event
))
995 (error "viper-event-key: Wrong type argument, eventp, %S" event
))
996 (when (if (featurep 'xemacs
)
997 (or (key-press-event-p event
) (mouse-event-p event
)) ; xemacs
1000 (let ((mod (event-modifiers event
))
1003 (if (featurep 'xemacs
)
1005 (cond ((key-press-event-p event
)
1007 ((button-event-p event
)
1008 (concat "mouse-" (prin1-to-string (event-button event
))))
1010 (error "viper-event-key: Unknown event, %S" event
)))
1011 ;; Emacs doesn't handle capital letters correctly, since
1012 ;; \S-a isn't considered the same as A (it behaves as
1013 ;; plain `a' instead). So we take care of this here
1014 (cond ((and (viper-characterp event
) (<= ?A event
) (<= event ?Z
))
1017 ;; Emacs has the oddity whereby characters 128+char
1018 ;; represent M-char *if* this appears inside a string.
1019 ;; So, we convert them manually to (meta char).
1020 ((and (viper-characterp event
)
1021 (< ?\C-? event
) (<= event
255))
1023 event
(- event ?\C-?
1)))
1024 ((and (null mod
) (eq event
'return
))
1026 ((and (null mod
) (eq event
'space
))
1028 ((and (null mod
) (eq event
'delete
))
1030 ((and (null mod
) (eq event
'backspace
))
1032 (t (event-basic-type event
)))
1033 ) ; (featurep 'xemacs)
1035 (if (viper-characterp basis
)
1037 (if (viper= basis ?\C-?
)
1038 (list 'control
'\?) ; taking care of an emacs bug
1039 (intern (char-to-string basis
)))))
1041 (append mod
(list basis
))
1044 (defun viper-last-command-char ()
1045 (if (featurep 'xemacs
)
1046 (event-to-character last-command-event
)
1047 last-command-event
))
1049 (defun viper-key-to-emacs-key (key)
1050 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
1051 (cond ((featurep 'xemacs
) key
)
1054 (setq key-name
(symbol-name key
))
1055 (cond ((= (length key-name
) 1) ; character event
1056 (string-to-char key-name
))
1057 ;; Emacs doesn't recognize `return' and `escape' as events on
1058 ;; dumb terminals, so we translate them into characters
1059 ((and (featurep 'emacs
) (not (viper-window-display-p))
1060 (string= key-name
"return"))
1062 ((and (featurep 'emacs
) (not (viper-window-display-p))
1063 (string= key-name
"escape"))
1065 ;; pass symbol-event as is
1069 (setq modifiers
(viper-subseq key
0 (1- (length key
)))
1070 base-key
(viper-seq-last-elt key
)
1071 base-key-name
(symbol-name base-key
)
1072 char-p
(= (length base-key-name
) 1))
1075 (lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1079 (car (read-from-string
1082 (mapconcat 'identity mod-char-list
"-\\")
1088 (mapconcat 'identity mod-char-list
"-")
1094 ;; LIS is assumed to be a list of events of characters
1095 (defun viper-eventify-list-xemacs (lis)
1096 (if (featurep 'xemacs
)
1099 (cond ((viper-characterp elt
) (character-to-event elt
))
1102 "viper-eventify-list-xemacs: can't convert to event, %S"
1107 ;; Smooths out the difference between Emacs's unread-command-events
1108 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1109 ;; events or a sequence of keys.
1111 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1112 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1113 ;; into an event. Below, we delete nil from event lists, since nil is the most
1114 ;; common symbol that might appear in this wrong context.
1115 (defun viper-set-unread-command-events (arg)
1116 (if (featurep 'emacs
)
1118 unread-command-events
1120 (cond ((eventp arg
) (list arg
))
1123 (listify-key-sequence arg
))
1125 "viper-set-unread-command-events: Invalid argument, %S"
1127 (if (not (eventp nil
))
1128 (setq new-events
(delq nil new-events
)))
1129 (append new-events unread-command-events
)))
1132 unread-command-events
1134 (cond ((viper-characterp arg
) (list (character-to-event arg
)))
1135 ((eventp arg
) (list arg
))
1136 ((stringp arg
) (mapcar 'character-to-event arg
))
1137 ((vectorp arg
) (append arg nil
)) ; turn into list
1138 ((listp arg
) (viper-eventify-list-xemacs arg
))
1140 "viper-set-unread-command-events: Invalid argument, %S" arg
)))
1141 unread-command-events
))))
1144 ;; Check if vec is a vector of key-press events representing characters
1146 (defun viper-event-vector-p (vec)
1148 (eval (cons 'and
(mapcar (lambda (elt) (if (eventp elt
) t
)) vec
)))))
1151 ;; check if vec is a vector of character symbols
1152 (defun viper-char-symbol-sequence-p (vec)
1157 (mapcar (lambda (elt)
1158 (and (symbolp elt
) (= (length (symbol-name elt
)) 1)))
1162 (defun viper-char-array-p (array)
1163 (eval (cons 'and
(mapcar 'viper-characterp array
))))
1166 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1167 ;; convert events to keys and, if all keys are regular printable
1168 ;; characters, will return a string. Otherwise, will return a string
1169 ;; representing a vector of converted events. If the input was a Viper macro,
1170 ;; will return a string that represents this macro as a vector.
1171 (defun viper-array-to-string (event-seq)
1173 (cond ((stringp event-seq
) event-seq
)
1174 ((viper-event-vector-p event-seq
)
1175 (setq temp
(mapcar 'viper-event-key event-seq
))
1176 (cond ((viper-char-symbol-sequence-p temp
)
1177 (mapconcat 'symbol-name temp
""))
1178 ((and (viper-char-array-p
1179 (setq temp2
(mapcar 'viper-key-to-character temp
))))
1180 (mapconcat 'char-to-string temp2
""))
1181 (t (prin1-to-string (vconcat temp
)))))
1182 ((viper-char-symbol-sequence-p event-seq
)
1183 (mapconcat 'symbol-name event-seq
""))
1184 ((and (vectorp event-seq
)
1186 (setq temp
(mapcar 'viper-key-to-character event-seq
))))
1187 (mapconcat 'char-to-string temp
""))
1188 (t (prin1-to-string event-seq
)))))
1190 (defun viper-key-press-events-to-chars (events)
1191 (mapconcat (if (featurep 'xemacs
)
1192 (lambda (elt) (char-to-string (event-to-character elt
))) ; xemacs
1193 'char-to-string
; emacs
1199 (defun viper-read-char-exclusive ()
1201 (echo-keystrokes 1))
1204 (setq char
(read-char))
1206 ;; skip event if not char
1207 (viper-read-event))))
1210 ;; key is supposed to be in viper's representation, e.g., (control l), a
1212 (defun viper-key-to-character (key)
1213 (cond ((eq key
'space
) ?\
)
1214 ((eq key
'delete
) ?\C-?
)
1215 ((eq key
'return
) ?\C-m
)
1216 ((eq key
'backspace
) ?\C-h
)
1218 (= 1 (length (symbol-name key
))))
1219 (string-to-char (symbol-name key
)))
1221 (eq (car key
) 'control
)
1222 (symbol-name (nth 1 key
))
1223 (= 1 (length (symbol-name (nth 1 key
)))))
1224 (read (format "?\\C-%s" (symbol-name (nth 1 key
)))))
1228 (defun viper-setup-master-buffer (&rest other-files-or-buffers
)
1229 "Set up the current buffer as a master buffer.
1230 Arguments become related buffers. This function should normally be used in
1231 the `Local variables' section of a file."
1232 (setq viper-related-files-and-buffers-ring
1233 (make-ring (1+ (length other-files-or-buffers
))))
1235 (viper-ring-insert viper-related-files-and-buffers-ring elt
))
1236 other-files-or-buffers
)
1237 (viper-ring-insert viper-related-files-and-buffers-ring
(buffer-name))
1240 ;;; Movement utilities
1242 ;; Characters that should not be considered as part of the word, in reformed-vi
1244 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1245 ;; into characters at some point and then put back to string. The result is
1246 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1247 ;; effect of quoting - and preventing it from being special.
1248 (defconst viper-non-word-characters-reformed-vi
1249 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1250 ;; These are characters that are not to be considered as parts of a word in
1252 ;; Set each time state changes and at loading time
1253 (viper-deflocalvar viper-non-word-characters nil
)
1255 ;; must be buffer-local
1256 (viper-deflocalvar viper-ALPHA-char-class
"w"
1257 "String of syntax classes characterizing Viper's alphanumeric symbols.
1258 In addition, the symbol `_' may be considered alphanumeric if
1259 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1261 (defconst viper-strict-ALPHA-chars
"a-zA-Z0-9_"
1262 "Regexp matching the set of alphanumeric characters acceptable to strict
1264 (defconst viper-strict-SEP-chars
" \t\n"
1265 "Regexp matching the set of alphanumeric characters acceptable to strict
1267 (defconst viper-strict-SEP-chars-sans-newline
" \t"
1268 "Regexp matching the set of alphanumeric characters acceptable to strict
1271 (defconst viper-SEP-char-class
" -"
1272 "String of syntax classes for Vi separators.
1273 Usually contains ` ', linefeed, TAB or formfeed.")
1276 ;; Set Viper syntax classes and related variables according to
1277 ;; `viper-syntax-preference'.
1278 (defun viper-update-syntax-classes (&optional set-default
)
1279 (let ((preference (cond ((eq viper-syntax-preference
'emacs
)
1280 "w") ; Viper words have only Emacs word chars
1281 ((eq viper-syntax-preference
'extended
)
1282 "w_") ; Viper words have Emacs word & symbol chars
1283 (t "w"))) ; Viper words are Emacs words plus `_'
1284 (non-word-chars (cond ((eq viper-syntax-preference
'reformed-vi
)
1285 (viper-string-to-list
1286 viper-non-word-characters-reformed-vi
))
1289 (setq-default viper-ALPHA-char-class preference
1290 viper-non-word-characters non-word-chars
)
1291 (setq viper-ALPHA-char-class preference
1292 viper-non-word-characters non-word-chars
))
1295 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1296 ;; is nil. If nil, use setq.
1297 (defun viper-set-syntax-preference (&optional symbol value
)
1298 "Set Viper syntax preference.
1299 If called interactively or if SYMBOL is nil, sets syntax preference in current
1300 buffer. If called non-interactively, preferably via the customization widget,
1301 sets the default value."
1306 "Viper syntax preference: "
1307 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1308 nil
'require-match
)))
1309 (if (stringp value
) (setq value
(intern value
)))
1310 (or (memq value
'(strict-vi reformed-vi extended emacs
))
1311 (error "Invalid Viper syntax preference, %S" value
))
1313 (setq-default viper-syntax-preference value
)
1314 (setq viper-syntax-preference value
))
1315 (viper-update-syntax-classes))
1317 (defcustom viper-syntax-preference
'reformed-vi
1318 "Syntax type characterizing Viper's alphanumeric symbols.
1319 Affects movement and change commands that deal with Vi-style words.
1320 Works best when set in the hooks to various major modes.
1322 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1324 `reformed-vi' means Viper words are like Emacs words \(as determined using
1325 Emacs syntax tables, which are different for different major modes) with two
1326 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1327 symbols like `\\=`', `\\='', `:', `\"', `)', and `{' are excluded.
1328 This behaves very close to `strict-vi', but also works well with non-ASCII
1329 characters from various alphabets.
1331 `extended' means Viper word constituents are symbols that are marked as being
1332 parts of words OR symbols in Emacs syntax tables.
1333 This is most appropriate for major modes intended for editing programs.
1335 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1337 This option is appropriate if you like Emacs-style words."
1338 :type
'(radio (const strict-vi
) (const reformed-vi
)
1339 (const extended
) (const emacs
))
1340 :set
'viper-set-syntax-preference
1342 (make-variable-buffer-local 'viper-syntax-preference
)
1345 ;; addl-chars are characters to be temporarily considered as alphanumerical
1346 (defun viper-looking-at-alpha (&optional addl-chars
)
1347 (or (stringp addl-chars
) (setq addl-chars
""))
1348 (if (eq viper-syntax-preference
'reformed-vi
)
1349 (setq addl-chars
(concat addl-chars
"_")))
1350 (let ((char (char-after (point))))
1352 (if (eq viper-syntax-preference
'strict-vi
)
1353 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars
"]"))
1355 ;; or one of the additional chars being asked to include
1356 (viper-memq-char char
(viper-string-to-list addl-chars
))
1358 ;; not one of the excluded word chars (note:
1359 ;; viper-non-word-characters is a list)
1360 (not (viper-memq-char char viper-non-word-characters
))
1361 ;; char of the Viper-word syntax class
1362 (viper-memq-char (char-syntax char
)
1363 (viper-string-to-list viper-ALPHA-char-class
))))))
1366 (defun viper-looking-at-separator ()
1367 (let ((char (char-after (point))))
1369 (if (eq viper-syntax-preference
'strict-vi
)
1370 (viper-memq-char char
(viper-string-to-list viper-strict-SEP-chars
))
1371 (or (eq char ?
\n) ; RET is always a separator in Vi
1372 (viper-memq-char (char-syntax char
)
1373 (viper-string-to-list viper-SEP-char-class
)))))
1376 (defsubst viper-looking-at-alphasep
(&optional addl-chars
)
1377 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars
)))
1379 (defun viper-skip-alpha-forward (&optional addl-chars
)
1380 (or (stringp addl-chars
) (setq addl-chars
""))
1383 (cond ((eq viper-syntax-preference
'strict-vi
)
1385 (t viper-ALPHA-char-class
))
1386 (cond ((eq viper-syntax-preference
'strict-vi
)
1387 (concat viper-strict-ALPHA-chars addl-chars
))
1390 (defun viper-skip-alpha-backward (&optional addl-chars
)
1391 (or (stringp addl-chars
) (setq addl-chars
""))
1394 (cond ((eq viper-syntax-preference
'strict-vi
)
1396 (t viper-ALPHA-char-class
))
1397 (cond ((eq viper-syntax-preference
'strict-vi
)
1398 (concat viper-strict-ALPHA-chars addl-chars
))
1401 ;; weird syntax tables may confuse strict-vi style
1402 (defsubst viper-skip-all-separators-forward
(&optional within-line
)
1403 (if (eq viper-syntax-preference
'strict-vi
)
1405 (skip-chars-forward viper-strict-SEP-chars-sans-newline
)
1406 (skip-chars-forward viper-strict-SEP-chars
))
1407 (viper-skip-syntax 'forward
1408 viper-SEP-char-class
1409 (or within-line
"\n")
1410 (if within-line
(viper-line-pos 'end
)))))
1412 (defsubst viper-skip-all-separators-backward
(&optional within-line
)
1413 (if (eq viper-syntax-preference
'strict-vi
)
1415 (skip-chars-backward viper-strict-SEP-chars-sans-newline
)
1416 (skip-chars-backward viper-strict-SEP-chars
))
1417 (viper-skip-syntax 'backward
1418 viper-SEP-char-class
1419 (or within-line
"\n")
1420 (if within-line
(viper-line-pos 'start
)))))
1421 (defun viper-skip-nonseparators (direction)
1424 (concat "^" viper-SEP-char-class
)
1426 (viper-line-pos (if (eq direction
'forward
) 'end
'start
))))
1429 ;; skip over non-word constituents and non-separators
1430 (defun viper-skip-nonalphasep-forward ()
1431 (if (eq viper-syntax-preference
'strict-vi
)
1433 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1436 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1437 ;; Emacs may consider some of these as words, but we don't want them
1438 viper-non-word-characters
1439 (viper-line-pos 'end
))))
1441 (defun viper-skip-nonalphasep-backward ()
1442 (if (eq viper-syntax-preference
'strict-vi
)
1443 (skip-chars-backward
1444 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1447 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1448 ;; Emacs may consider some of these as words, but we don't want them
1449 viper-non-word-characters
1450 (viper-line-pos 'start
))))
1452 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1453 ;; Return the number of chars traveled.
1454 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1455 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1456 ;; words, even if Emacs syntax table says they are.
1457 (defun viper-skip-syntax (direction syntax addl-chars
&optional limit
)
1461 (if (eq direction
'forward
)
1462 'skip-chars-forward
'skip-chars-backward
))
1464 (if (eq direction
'forward
)
1465 'viper-forward-char-carefully
'viper-backward-char-carefully
))
1466 char-looked-at syntax-of-char-looked-at negated-syntax
)
1468 (cond ((listp addl-chars
) (viper-charlist-to-string addl-chars
))
1469 ((stringp addl-chars
) addl-chars
)
1472 (cond ((listp syntax
) syntax
)
1473 ((stringp syntax
) (viper-string-to-list syntax
))
1475 (if (memq ?^ syntax
) (setq negated-syntax t
))
1477 (while (and (not (= local
0))
1478 (cond ((eq direction
'forward
)
1481 (setq char-looked-at
(viper-char-at-pos direction
)
1482 ;; if outside the range, set to nil
1483 syntax-of-char-looked-at
(if char-looked-at
1484 (char-syntax char-looked-at
)))
1487 (cond ((and limit
(eq direction
'forward
))
1489 (limit ; backward & limit
1492 ;; char under/before cursor has appropriate syntax
1494 (not (memq syntax-of-char-looked-at syntax
))
1495 (memq syntax-of-char-looked-at syntax
))
1496 ;; if char-syntax class is "word", make sure it is not one
1497 ;; of the excluded characters
1498 (if (and (eq syntax-of-char-looked-at ?w
)
1499 (not negated-syntax
))
1500 (not (viper-memq-char
1501 char-looked-at viper-non-word-characters
))
1503 (funcall skip-syntax-func
1)
1505 (funcall skip-chars-func addl-chars limit
)))
1506 (setq total
(+ total local
)))
1510 ;; tells when point is at the beginning of field
1511 (defun viper-beginning-of-field ()
1513 (not (eq (get-char-property (point) 'field
)
1514 (get-char-property (1- (point)) 'field
)))))
1517 ;; this is copied from cl-extra.el
1518 ;; Return the subsequence of SEQ from START to END.
1519 ;; If END is omitted, it defaults to the length of the sequence.
1520 ;; If START or END is negative, it counts from the end.
1521 (defun viper-subseq (seq start
&optional end
)
1522 (if (stringp seq
) (substring seq start end
)
1524 (and end
(< end
0) (setq end
(+ end
(setq len
(length seq
)))))
1525 (if (< start
0) (setq start
(+ start
(or len
(setq len
(length seq
))))))
1527 (if (> start
0) (setq seq
(nthcdr start seq
)))
1530 (while (>= (setq end
(1- end
)) start
)
1531 (push (pop seq
) res
))
1533 (copy-sequence seq
)))
1535 (or end
(setq end
(or len
(length seq
))))
1536 (let ((res (make-vector (max (- end start
) 0) nil
))
1538 (while (< start end
)
1539 (aset res i
(aref seq start
))
1540 (setq i
(1+ i
) start
(1+ start
)))
1546 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1549 ;;; viper-util.el ends here