1 ;;; viper-util.el --- Utilities used by viper.el
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 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 <http://www.gnu.org/licenses/>.
31 (defvar viper-overriding-map
)
32 (defvar pm-color-alist
)
33 (defvar viper-minibuffer-current-face
)
34 (defvar viper-minibuffer-insert-face
)
35 (defvar viper-minibuffer-vi-face
)
36 (defvar viper-minibuffer-emacs-face
)
37 (defvar viper-replace-overlay-face
)
38 (defvar viper-fast-keyseq-timeout
)
39 (defvar ex-unix-type-shell
)
40 (defvar ex-unix-type-shell-options
)
41 (defvar viper-ex-tmp-buf-name
)
42 (defvar viper-syntax-preference
)
43 (defvar viper-saved-mark
)
48 (unless (fboundp 'declare-function
) (defmacro declare-function
(&rest r
))))
56 (defalias 'viper-overlay-p
57 (if (featurep 'xemacs
) 'extentp
'overlayp
))
58 (defalias 'viper-make-overlay
59 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
60 (defalias 'viper-overlay-live-p
61 (if (featurep 'xemacs
) 'extent-live-p
'overlayp
))
62 (defalias 'viper-move-overlay
63 (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
))
64 (defalias 'viper-overlay-start
65 (if (featurep 'xemacs
) 'extent-start-position
'overlay-start
))
66 (defalias 'viper-overlay-end
67 (if (featurep 'xemacs
) 'extent-end-position
'overlay-end
))
68 (defalias 'viper-overlay-get
69 (if (featurep 'xemacs
) 'extent-property
'overlay-get
))
70 (defalias 'viper-overlay-put
71 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
72 (defalias 'viper-read-event
73 (if (featurep 'xemacs
) 'next-command-event
'read-event
))
74 (defalias 'viper-characterp
75 (if (featurep 'xemacs
) 'characterp
'integerp
))
76 (defalias 'viper-int-to-char
77 (if (featurep 'xemacs
) 'int-to-char
'identity
))
78 (defalias 'viper-get-face
79 (if (featurep 'xemacs
) 'get-face
'internal-get-face
))
80 (defalias 'viper-color-defined-p
81 (if (featurep 'xemacs
) 'valid-color-name-p
'x-color-defined-p
))
82 (defalias 'viper-iconify
83 (if (featurep 'xemacs
) 'iconify-frame
'iconify-or-deiconify-frame
))
86 ;; CHAR is supposed to be a char or an integer (positive or negative)
87 ;; LIST is a list of chars, nil, and negative numbers
88 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
89 ;; Introduced for compatibility with XEmacs, where integers are not the same as
91 (defun viper-memq-char (char list
)
92 (cond ((and (integerp char
) (>= char
0))
93 (memq (viper-int-to-char char
) list
))
96 ;; Check if char-or-int and char are the same as characters
97 (defun viper-char-equal (char-or-int char
)
98 (cond ((and (integerp char-or-int
) (>= char-or-int
0))
99 (= (viper-int-to-char char-or-int
) char
))
100 ((eq char-or-int char
))))
102 ;; Like =, but accommodates null and also is t for eq-objects
103 (defun viper= (char char1
)
104 (cond ((eq char char1
) t
)
105 ((and (viper-characterp char
) (viper-characterp char1
))
109 (defsubst viper-color-display-p
()
110 (if (featurep 'xemacs
) (eq (device-class (selected-device)) 'color
)
111 (x-display-color-p)))
113 (defun viper-get-cursor-color (&optional frame
)
114 (if (featurep 'xemacs
)
116 (frame-property (or frame
(selected-frame)) 'cursor-color
))
117 (cdr (assoc 'cursor-color
(frame-parameters)))))
119 (defmacro viper-frame-value
(variable)
120 "Return the value of VARIABLE local to the current frame, if there is one.
121 Otherwise return the normal value."
122 `(if (featurep 'xemacs
)
124 ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
125 ;; so we do it by hand instead.
126 ;; Buffer-local values take precedence over frame-local ones.
127 (if (local-variable-p ',variable
)
129 ;; Distinguish between no frame parameter and a frame parameter
130 ;; with a value of nil.
131 (let ((fp (assoc ',variable
(frame-parameters))))
136 (cond ((eq (viper-device-type) 'pm
)
137 (fset 'viper-color-defined-p
138 (lambda (color) (assoc color pm-color-alist
)))))
142 (defun viper-change-cursor-color (new-color &optional frame
)
143 (if (and (viper-window-display-p) (viper-color-display-p)
144 (stringp new-color
) (viper-color-defined-p new-color
)
145 (not (string= new-color
(viper-get-cursor-color))))
146 (if (featurep 'xemacs
)
148 (or frame
(selected-frame))
149 'cursor-color
(make-color-instance new-color
))
150 (modify-frame-parameters
151 (or frame
(selected-frame))
152 (list (cons 'cursor-color new-color
))))))
154 ;; Note that the colors this function uses might not be those
155 ;; associated with FRAME, if there are frame-local values.
156 ;; This was equally true before the advent of viper-frame-value.
157 ;; Now it could be changed by passing frame to v-f-v.
158 (defun viper-set-cursor-color-according-to-state (&optional frame
)
159 (cond ((eq viper-current-state
'replace-state
)
160 (viper-change-cursor-color
161 (viper-frame-value viper-replace-overlay-cursor-color
)
163 ((and (eq viper-current-state
'emacs-state
)
164 (viper-frame-value viper-emacs-state-cursor-color
))
165 (viper-change-cursor-color
166 (viper-frame-value viper-emacs-state-cursor-color
)
168 ((eq viper-current-state
'insert-state
)
169 (viper-change-cursor-color
170 (viper-frame-value viper-insert-state-cursor-color
)
173 (viper-change-cursor-color
174 (viper-frame-value viper-vi-state-cursor-color
)
177 ;; By default, saves current frame cursor color in the
178 ;; viper-saved-cursor-color-in-replace-mode property of viper-replace-overlay
179 (defun viper-save-cursor-color (before-which-mode)
180 (if (and (viper-window-display-p) (viper-color-display-p))
181 (let ((color (viper-get-cursor-color)))
182 (if (and (stringp color
) (viper-color-defined-p color
)
185 viper-replace-overlay-cursor-color
))))
186 (modify-frame-parameters
190 (cond ((eq before-which-mode
'before-replace-mode
)
191 'viper-saved-cursor-color-in-replace-mode
)
192 ((eq before-which-mode
'before-emacs-mode
)
193 'viper-saved-cursor-color-in-emacs-mode
)
195 'viper-saved-cursor-color-in-insert-mode
))
199 (defun viper-get-saved-cursor-color-in-replace-mode ()
202 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
204 'viper-saved-cursor-color-in-replace-mode
)
205 (or (and (eq viper-current-state
'emacs-mode
)
206 (viper-frame-value viper-emacs-state-cursor-color
))
207 (viper-frame-value viper-vi-state-cursor-color
))))
209 (defun viper-get-saved-cursor-color-in-insert-mode ()
212 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
214 'viper-saved-cursor-color-in-insert-mode
)
215 (or (and (eq viper-current-state
'emacs-mode
)
216 (viper-frame-value viper-emacs-state-cursor-color
))
217 (viper-frame-value viper-vi-state-cursor-color
))))
219 (defun viper-get-saved-cursor-color-in-emacs-mode ()
222 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
224 'viper-saved-cursor-color-in-emacs-mode
)
225 (viper-frame-value viper-vi-state-cursor-color
)))
227 ;; restore cursor color from replace overlay
228 (defun viper-restore-cursor-color(after-which-mode)
229 (if (viper-overlay-p viper-replace-overlay
)
230 (viper-change-cursor-color
231 (cond ((eq after-which-mode
'after-replace-mode
)
232 (viper-get-saved-cursor-color-in-replace-mode))
233 ((eq after-which-mode
'after-emacs-mode
)
234 (viper-get-saved-cursor-color-in-emacs-mode))
235 (t (viper-get-saved-cursor-color-in-insert-mode)))
239 ;; Check the current version against the major and minor version numbers
240 ;; using op: cur-vers op major.minor If emacs-major-version or
241 ;; emacs-minor-version are not defined, we assume that the current version
242 ;; is hopelessly outdated. We assume that emacs-major-version and
243 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
244 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
245 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
246 ;; incorrect. However, this gives correct result in our cases, since we are
247 ;; testing for sufficiently high Emacs versions.
248 (defun viper-check-version (op major minor
&optional type-of-emacs
)
249 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
250 (and (cond ((eq type-of-emacs
'xemacs
) (featurep 'xemacs
))
251 ((eq type-of-emacs
'emacs
) (featurep 'emacs
))
253 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
254 (= emacs-major-version major
)))
255 ((memq op
'(> >= < <=))
256 (and (or (funcall op emacs-major-version major
)
257 (= emacs-major-version major
))
258 (if (= emacs-major-version major
)
259 (funcall op emacs-minor-version minor
)
262 (error "%S: Invalid op in viper-check-version" op
))))
263 (cond ((memq op
'(= > >=)) nil
)
264 ((memq op
'(< <=)) t
))))
267 (defun viper-get-visible-buffer-window (wind)
268 (if (featurep 'xemacs
)
269 (get-buffer-window wind t
)
270 (get-buffer-window wind
'visible
)))
273 ;; Return line position.
274 ;; If pos is 'start then returns position of line start.
275 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
276 ;; Pos = 'indent returns beginning of indentation.
277 ;; Otherwise, returns point. Current point is not moved in any case."
278 (defun viper-line-pos (pos)
279 (let ((cur-pos (point))
287 (goto-char (+ (viper-line-pos 'start
) (viper-line-pos 'end
) 2)))
289 (back-to-indentation))
291 (setq result
(point))
295 ;; Emacs used to count each multibyte character as several positions in the buffer,
296 ;; so we had to use Emacs' chars-in-region to count characters. Since 20.3,
297 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
298 ;; counting each char as just one pos. So, now we can simply subtract beg from
299 ;; end to determine the number of characters in a region.
300 (defun viper-chars-in-region (beg end
&optional preserve-sign
)
301 ;;(let ((count (abs (if (fboundp 'chars-in-region)
302 ;; (chars-in-region beg end)
304 (let ((count (abs (- end beg
))))
305 (if (and (< end beg
) preserve-sign
)
309 ;; Test if POS is between BEG and END
310 (defsubst viper-pos-within-region
(pos beg end
)
311 (and (>= pos
(min beg end
)) (>= (max beg end
) pos
)))
314 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
315 ;; The first argument must eval to a variable name.
316 ;; Arguments: (var-name position &optional buffer).
318 ;; This is useful for moving markers that are supposed to be local.
319 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
320 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
321 ;; buffer, a new marker will be created.
322 (defun viper-move-marker-locally (var pos
&optional buffer
)
323 (if (markerp (eval var
))
325 (set var
(make-marker)))
326 (move-marker (eval var
) pos buffer
))
329 ;; Print CONDITIONS as a message.
330 (defun viper-message-conditions (conditions)
331 (let ((case (car conditions
)) (msg (cdr conditions
)))
334 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
339 ;;; List/alist utilities
341 ;; Convert LIST to an alist
342 (defun viper-list-to-alist (lst)
345 (setq alist
(cons (list (car lst
)) alist
))
346 (setq lst
(cdr lst
)))
349 ;; Convert ALIST to a list.
350 (defun viper-alist-to-list (alst)
353 (setq lst
(cons (car (car alst
)) lst
))
354 (setq alst
(cdr alst
)))
357 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
358 (defun viper-filter-alist (regexp alst
)
360 (let ((outalst) (inalst alst
))
362 (if (string-match regexp
(car (car inalst
)))
363 (setq outalst
(cons (car inalst
) outalst
)))
364 (setq inalst
(cdr inalst
)))
367 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
368 (defun viper-filter-list (regexp lst
)
370 (let ((outlst) (inlst lst
))
372 (if (string-match regexp
(car inlst
))
373 (setq outlst
(cons (car inlst
) outlst
)))
374 (setq inlst
(cdr inlst
)))
378 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
379 ;; LIS2 is modified by filtering it: deleting its members of the form
380 ;; \(car elt\) such that (car elt') is in LIS1.
381 (defun viper-append-filter-alist (lis1 lis2
)
384 ;;filter-append the second list
386 ;; delete all occurrences
387 (while (setq elt
(assoc (car (car temp
)) lis2
))
388 (setq lis2
(delq elt lis2
)))
389 (setq temp
(cdr temp
)))
395 (declare-function viper-forward-Word
"viper-cmd" (arg))
397 ;;; Support for :e, :r, :w file globbing
399 ;; Glob the file spec.
400 ;; This function is designed to work under Unix.
401 (defun viper-glob-unix-files (filespec)
403 (cond (ex-unix-type-shell shell-file-name
)
404 (t "sh"))) ; probably Unix anyway
406 ;; using cond in anticipation of further additions
407 (cond (ex-unix-type-shell-options)
409 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec
))
410 (t (format "ls -1 -d %s" filespec
))))
413 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
417 (call-process gshell nil t nil
421 (call-process gshell nil t nil
424 (goto-char (point-min))
425 ;; Issue an error, if no match.
426 (unless (eq 0 status
)
428 (skip-chars-forward " \t\n\j")
429 (if (looking-at "ls:")
430 (viper-forward-Word 1))
435 (buffer-substring (point) (viper-line-pos 'end
)))
437 (goto-char (point-min))
438 (viper-get-filenames-from-buffer 'one-per-line
))
442 ;; Interpret the stuff in the buffer as a list of file names
443 ;; return a list of file names listed in the buffer beginning at point
444 ;; If optional arg is supplied, assume each filename is listed on a separate
446 (defun viper-get-filenames-from-buffer (&optional one-per-line
)
447 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
449 (skip-chars-forward skip-chars
)
451 (if (cond ((looking-at "\"")
453 (re-search-forward "[^\"]+" nil t
)) ; noerror
456 (re-search-forward "[^']+" nil t
)) ; noerror
459 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
461 (buffer-substring (match-beginning 0) (match-end 0))))
464 (skip-chars-forward " \t\n")
465 (setq result
(cons fname result
)))
468 ;; convert MS-DOS wildcards to regexp
469 (defun viper-wildcard-to-regexp (wcard)
471 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
474 (goto-char (point-min))
476 (skip-chars-forward "^*?.\\\\")
477 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
478 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
479 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
480 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
486 ;; glob windows files
487 ;; LIST is expected to be in reverse order
488 (defun viper-glob-mswindows-files (filespec)
489 (let ((case-fold-search t
)
492 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
495 (goto-char (point-min))
496 (setq tmp
(viper-get-filenames-from-buffer))
498 (setq tmp2
(cons (directory-files
499 ;; the directory part
500 (or (file-name-directory (car tmp
))
502 t
; return full names
503 ;; the regexp part: globs the file names
505 (viper-wildcard-to-regexp
506 (file-name-nondirectory (car tmp
)))
509 (setq tmp
(cdr tmp
)))
510 (reverse (apply 'append tmp2
)))))
515 ;; Rotate RING's index. DIRection can be positive or negative.
516 (defun viper-ring-rotate1 (ring dir
)
517 (if (and (ring-p ring
) (> (ring-length ring
) 0))
519 (setcar ring
(cond ((> dir
0)
520 (ring-plus1 (car ring
) (ring-length ring
)))
522 (ring-minus1 (car ring
) (ring-length ring
)))
523 ;; don't rotate if dir = 0
525 (viper-current-ring-item ring
)
528 (defun viper-special-ring-rotate1 (ring dir
)
529 (if (memq viper-intermediate-command
530 '(repeating-display-destructive-command
531 repeating-insertion-from-ring
))
532 (viper-ring-rotate1 ring dir
)
533 ;; don't rotate otherwise
534 (viper-ring-rotate1 ring
0)))
536 ;; current ring item; if N is given, then so many items back from the
538 (defun viper-current-ring-item (ring &optional n
)
540 (if (and (ring-p ring
) (> (ring-length ring
) 0))
541 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
543 ;; Push item onto ring. The second argument is a ring-variable, not value.
544 (defun viper-push-onto-ring (item ring-var
)
545 (or (ring-p (eval ring-var
))
546 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
547 (or (null item
) ; don't push nil
548 (and (stringp item
) (string= item
"")) ; or empty strings
549 (equal item
(viper-current-ring-item (eval ring-var
))) ; or old stuff
550 ;; Since viper-set-destructive-command checks if we are inside
551 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
552 ;; cmd viper-repeat makes a call to the current function only if `.' is
553 ;; executing a command from the command history. It doesn't call the
554 ;; push-onto-ring function if `.' is simply repeating the last
555 ;; destructive command. We only check for ESC (which happens when we do
556 ;; insert with a prefix argument, or if this-command-keys doesn't give
557 ;; anything meaningful (in that case we don't know what to show to the
559 (and (eq ring-var
'viper-command-ring
)
560 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
561 (viper-array-to-string (this-command-keys))))
562 (viper-ring-insert (eval ring-var
) item
))
566 ;; removing elts from ring seems to break it
567 (defun viper-cleanup-ring (ring)
568 (or (< (ring-length ring
) 2)
569 (null (viper-current-ring-item ring
))
570 ;; last and previous equal
571 (if (equal (viper-current-ring-item ring
)
572 (viper-current-ring-item ring
1))
573 (viper-ring-pop ring
))))
575 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
576 (defun viper-ring-pop (ring)
577 (let* ((ln (ring-length ring
))
578 (vec (cdr (cdr ring
)))
579 (veclen (length vec
))
581 (idx (max 0 (ring-minus1 hd ln
)))
582 (top-elt (aref vec idx
)))
585 (while (< (1+ idx
) veclen
)
586 (aset vec idx
(aref vec
(1+ idx
)))
590 (setq hd
(max 0 (ring-minus1 hd ln
)))
591 (if (= hd
(1- ln
)) (setq hd
0))
592 (setcar ring hd
) ; move head
593 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
597 (defun viper-ring-insert (ring item
)
598 (let* ((ln (ring-length ring
))
599 (vec (cdr (cdr ring
)))
600 (veclen (length vec
))
602 (vecpos-after-hd (if (= hd
0) ln hd
))
607 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
608 (setcar ring
(ring-plus1 hd ln
)))
609 (setcar (cdr ring
) (1+ ln
))
610 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
611 (while (and (>= idx vecpos-after-hd
) (> ln
0))
612 (aset vec idx
(aref vec
(1- idx
)))
614 (aset vec vecpos-after-hd item
))
620 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
621 ;; PRE-STRING is a string to prepend to the abbrev string.
622 ;; POST-STRING is a string to append to the abbrev string.
623 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
624 ;; if the orig string was truncated.
625 (defun viper-abbreviate-string (string max-len
626 pre-string post-string abbrev-sign
)
630 (substring string
0 (min max-len
(length string
)))))
631 (cond ((null truncated-str
) "")
632 ((> (length string
) max-len
)
634 pre-string truncated-str abbrev-sign post-string
))
635 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
637 ;; tells if we are over a whitespace-only line
638 (defsubst viper-over-whitespace-line
()
641 (looking-at "^[ \t]*$")))
644 ;;; Saving settings in custom file
646 ;; Save the current setting of VAR in CUSTOM-FILE.
647 ;; If given, MESSAGE is a message to be displayed after that.
648 ;; This message is erased after 2 secs, if erase-msg is non-nil.
649 ;; Arguments: var message custom-file &optional erase-message
650 (defun viper-save-setting (var message custom-file
&optional erase-msg
)
651 (let* ((var-name (symbol-name var
))
652 (var-val (if (boundp var
) (eval var
)))
653 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
654 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
656 (message "%s" (or message
""))
659 (goto-char (point-min))
660 (if (re-search-forward regexp nil t
)
661 (let ((reg-end (1- (match-end 0))))
662 (search-backward var-name
)
663 (delete-region (match-beginning 0) reg-end
)
664 (goto-char (match-beginning 0))
665 (insert (format "%s '%S" var-name var-val
)))
666 (goto-char (point-max))
667 (if (not (bolp)) (insert "\n"))
668 (insert (format "(setq %s '%S)\n" var-name var-val
)))
677 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
678 ;; match this pattern.
679 (defun viper-save-string-in-file (string custom-file
&optional pattern
)
680 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
683 (let (buffer-read-only)
684 (goto-char (point-min))
685 (if pattern
(delete-matching-lines pattern
))
686 (goto-char (point-max))
687 (if string
(insert string
))
693 ;; This is a simple-minded check for whether a file is under version control.
694 ;; If file,v exists but file doesn't, this file is considered to be not checked
695 ;; in and not checked out for the purpose of patching (since patch won't be
696 ;; able to read such a file anyway).
697 ;; FILE is a string representing file name
698 ;;(defun viper-file-under-version-control (file)
699 ;; (let* ((filedir (file-name-directory file))
700 ;; (file-nondir (file-name-nondirectory file))
701 ;; (trial (concat file-nondir ",v"))
702 ;; (full-trial (concat filedir trial))
703 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
704 ;; (and (stringp file)
705 ;; (file-exists-p file)
708 ;; (file-exists-p full-trial)
709 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
710 ;; ;; don't be fooled by this!
711 ;; (not (equal (file-attributes file)
712 ;; (file-attributes full-trial))))
713 ;; ;; check if a version is in RCS/ directory
714 ;; (file-exists-p full-rcs-trial)))
718 (defsubst viper-file-checked-in-p
(file)
719 (and (featurep 'vc-hooks
)
720 ;; CVS files are considered not checked in
721 ;; FIXME: Should this deal with more than CVS?
722 (not (memq (vc-backend file
) '(nil CVS
)))
723 (if (fboundp 'vc-state
)
725 (not (memq (vc-state file
) '(edited needs-merge
)))
726 (not (stringp (vc-state file
))))
727 ;; XEmacs has no vc-state
728 (if (featurep 'xemacs
) (not (vc-locking-user file
))))))
730 ;; checkout if visited file is checked in
731 (defun viper-maybe-checkout (buf)
732 (let ((file (expand-file-name (buffer-file-name buf
)))
733 (checkout-function (key-binding "\C-x\C-q")))
734 (if (and (viper-file-checked-in-p file
)
738 "File %s is checked in. Check it out? "
739 (viper-abbreviate-file-name file
))))
740 (with-current-buffer buf
741 (command-execute checkout-function
)))))
747 (defun viper-put-on-search-overlay (beg end
)
748 (if (viper-overlay-p viper-search-overlay
)
749 (viper-move-overlay viper-search-overlay beg end
)
750 (setq viper-search-overlay
(viper-make-overlay beg end
(current-buffer)))
752 viper-search-overlay
'priority viper-search-overlay-priority
))
753 (viper-overlay-put viper-search-overlay
'face viper-search-face
))
757 (defun viper-flash-search-pattern ()
758 (if (not (viper-has-face-support-p))
760 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
762 (viper-overlay-put viper-search-overlay
'face nil
)))
764 (defun viper-hide-search-overlay ()
765 (if (not (viper-overlay-p viper-search-overlay
))
767 (setq viper-search-overlay
768 (viper-make-overlay (point-min) (point-min) (current-buffer)))
770 viper-search-overlay
'priority viper-search-overlay-priority
)))
771 (viper-overlay-put viper-search-overlay
'face nil
))
775 (defsubst viper-move-replace-overlay
(beg end
)
776 (viper-move-overlay viper-replace-overlay beg end
))
778 (defun viper-set-replace-overlay (beg end
)
779 (if (viper-overlay-live-p viper-replace-overlay
)
780 (viper-move-replace-overlay beg end
)
781 (setq viper-replace-overlay
(viper-make-overlay beg end
(current-buffer)))
784 viper-replace-overlay
(if (featurep 'emacs
) 'evaporate
'detachable
) nil
)
786 viper-replace-overlay
'priority viper-replace-overlay-priority
)
787 ;; If Emacs will start supporting overlay maps, as it currently supports
788 ;; text-property maps, we could do away with viper-replace-minor-mode and
789 ;; just have keymap attached to replace overlay.
791 ;; viper-replace-overlay
792 ;; (if (featurep 'xemacs) 'keymap 'local-map)
793 ;; viper-replace-map)
795 (if (viper-has-face-support-p)
797 viper-replace-overlay
'face viper-replace-overlay-face
))
798 (viper-save-cursor-color 'before-replace-mode
)
799 (viper-change-cursor-color
800 (viper-frame-value viper-replace-overlay-cursor-color
)))
803 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph
)
804 (or (viper-overlay-live-p viper-replace-overlay
)
805 (viper-set-replace-overlay (point-min) (point-min)))
806 (if (or (not (viper-has-face-support-p))
807 viper-use-replace-region-delimiters
)
808 (let ((before-name (if (featurep 'xemacs
) 'begin-glyph
'before-string
))
809 (after-name (if (featurep 'xemacs
) 'end-glyph
'after-string
)))
810 (viper-overlay-put viper-replace-overlay before-name before-glyph
)
811 (viper-overlay-put viper-replace-overlay after-name after-glyph
))))
813 (defun viper-hide-replace-overlay ()
814 (viper-set-replace-overlay-glyphs nil nil
)
815 (viper-restore-cursor-color 'after-replace-mode
)
816 (viper-restore-cursor-color 'after-insert-mode
)
817 (if (viper-has-face-support-p)
818 (viper-overlay-put viper-replace-overlay
'face nil
)))
821 (defsubst viper-replace-start
()
822 (viper-overlay-start viper-replace-overlay
))
823 (defsubst viper-replace-end
()
824 (viper-overlay-end viper-replace-overlay
))
829 (defun viper-set-minibuffer-overlay ()
830 (viper-check-minibuffer-overlay)
831 (when (viper-has-face-support-p)
833 viper-minibuffer-overlay
'face viper-minibuffer-current-face
)
835 viper-minibuffer-overlay
'priority viper-minibuffer-overlay-priority
)
838 viper-minibuffer-overlay
839 (if (featurep 'emacs
) 'evaporate
'detachable
)
841 ;; make viper-minibuffer-overlay open-ended
842 ;; In emacs, it is made open ended at creation time
843 (when (featurep 'xemacs
)
844 (viper-overlay-put viper-minibuffer-overlay
'start-open nil
)
845 (viper-overlay-put viper-minibuffer-overlay
'end-open nil
))))
847 (defun viper-check-minibuffer-overlay ()
848 (if (viper-overlay-live-p viper-minibuffer-overlay
)
850 viper-minibuffer-overlay
851 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
853 (setq viper-minibuffer-overlay
854 (if (featurep 'xemacs
)
855 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
856 ;; make overlay open-ended
858 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
860 (current-buffer) nil
'rear-advance
)))))
863 (defsubst viper-is-in-minibuffer
()
865 (string-match "\*Minibuf-" (buffer-name))))
869 ;;; XEmacs compatibility
871 (defun viper-abbreviate-file-name (file)
872 (if (featurep 'xemacs
)
873 (abbreviate-file-name file t
) ; XEmacs requires addl argument
874 (abbreviate-file-name file
)))
876 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
877 ;; in sit-for, so this function smoothes out the differences.
878 (defsubst viper-sit-for-short
(val &optional nodisp
)
879 (sit-for (/ val
1000.0) nodisp
))
881 ;; EVENT may be a single event of a sequence of events
882 (defsubst viper-ESC-event-p
(event)
883 (let ((ESC-keys '(?\e
(control \
[) escape
))
884 (key (viper-event-key event
)))
885 (member key ESC-keys
)))
887 ;; checks if object is a marker, has a buffer, and points to within that buffer
888 (defun viper-valid-marker (marker)
889 (if (and (markerp marker
) (marker-buffer marker
))
890 (let ((buf (marker-buffer marker
))
891 (pos (marker-position marker
)))
894 (and (<= pos
(point-max)) (<= (point-min) pos
))))))
896 (defsubst viper-mark-marker
()
897 (if (featurep 'xemacs
) (mark-marker t
)
900 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
901 ;; is the same as (mark t).
902 (defsubst viper-set-mark-if-necessary
()
903 (setq mark-ring
(delete (viper-mark-marker) mark-ring
))
904 (set-mark-command nil
)
905 (setq viper-saved-mark
(point)))
907 ;; In transient mark mode (zmacs mode), it is annoying when regions become
908 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
909 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
910 (defun viper-deactivate-mark ()
911 (if (featurep 'xemacs
)
912 (zmacs-deactivate-region)
915 (defsubst viper-leave-region-active
()
916 (if (featurep 'xemacs
) (setq zmacs-region-stays t
)))
918 ;; Check if arg is a valid character for register
919 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
920 ;; Letter means lowercase letters, Letter means uppercase letters, and
921 ;; digit means digits from 1 to 9.
922 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
923 (defun viper-valid-register (reg &optional type
)
924 (or type
(setq type
'(letter Letter digit
)))
925 (or (if (memq 'letter type
)
926 (and (<= ?a reg
) (<= reg ?z
)))
927 (if (memq 'digit type
)
928 (and (<= ?
1 reg
) (<= reg ?
9)))
929 (if (memq 'Letter type
)
930 (and (<= ?A reg
) (<= reg ?Z
)))
935 ;; it is suggested that an event must be copied before it is assigned to
936 ;; last-command-event in XEmacs
937 (defun viper-copy-event (event)
938 (if (featurep 'xemacs
) (copy-event event
)
941 ;; Uses different timeouts for ESC-sequences and others
942 (defun viper-fast-keysequence-p ()
943 (not (viper-sit-for-short
944 (if (viper-ESC-event-p last-input-event
)
945 (viper-ESC-keyseq-timeout)
946 viper-fast-keyseq-timeout
)
949 ;; like read-event, but in XEmacs also try to convert to char, if possible
950 (defun viper-read-event-convert-to-char ()
952 (if (featurep 'xemacs
)
954 (setq event
(next-command-event))
955 (or (event-to-character event
)
959 ;; Viperized read-key-sequence
960 (defun viper-read-key-sequence (prompt &optional continue-echo
)
961 (let (inhibit-quit event keyseq
)
962 (setq keyseq
(read-key-sequence prompt continue-echo
))
963 (setq event
(if (featurep 'xemacs
)
964 (elt keyseq
0) ; XEmacs returns vector of events
965 (elt (listify-key-sequence keyseq
) 0)))
966 (if (viper-ESC-event-p event
)
967 (let (unread-command-events)
968 (if (viper-fast-keysequence-p)
969 (let ((viper-vi-global-user-minor-mode nil
)
970 (viper-vi-local-user-minor-mode nil
)
971 (viper-vi-intercept-minor-mode nil
)
972 (viper-insert-intercept-minor-mode nil
)
973 (viper-replace-minor-mode nil
) ; actually unnecessary
974 (viper-insert-global-user-minor-mode nil
)
975 (viper-insert-local-user-minor-mode nil
))
976 ;; Note: set unread-command-events only after testing for fast
977 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
978 ;; always t -- whether there is anything after ESC or not
979 (viper-set-unread-command-events keyseq
)
980 (setq keyseq
(read-key-sequence nil
)))
981 (viper-set-unread-command-events keyseq
)
982 (setq keyseq
(read-key-sequence nil
)))))
986 ;; This function lets function-key-map convert key sequences into logical
987 ;; keys. This does a better job than viper-read-event when it comes to kbd
988 ;; macros, since it enables certain macros to be shared between X and TTY modes
989 ;; by correctly mapping key sequences for Left/Right/... (on an ascii
990 ;; terminal) into logical keys left, right, etc.
991 (defun viper-read-key ()
992 (let ((overriding-local-map viper-overriding-map
)
995 (use-global-map viper-overriding-map
)
997 (setq key
(elt (viper-read-key-sequence nil
) 0))
998 (use-global-map global-map
))
1002 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
1003 ;; instead of nil, if '(nil) was previously inadvertently assigned to
1004 ;; unread-command-events
1005 (defun viper-event-key (event)
1006 (or (and event
(eventp event
))
1007 (error "viper-event-key: Wrong type argument, eventp, %S" event
))
1008 (when (if (featurep 'xemacs
)
1009 (or (key-press-event-p event
) (mouse-event-p event
)) ; xemacs
1012 (let ((mod (event-modifiers event
))
1015 (if (featurep 'xemacs
)
1017 (cond ((key-press-event-p event
)
1019 ((button-event-p event
)
1020 (concat "mouse-" (prin1-to-string (event-button event
))))
1022 (error "viper-event-key: Unknown event, %S" event
)))
1023 ;; Emacs doesn't handle capital letters correctly, since
1024 ;; \S-a isn't considered the same as A (it behaves as
1025 ;; plain `a' instead). So we take care of this here
1026 (cond ((and (viper-characterp event
) (<= ?A event
) (<= event ?Z
))
1029 ;; Emacs has the oddity whereby characters 128+char
1030 ;; represent M-char *if* this appears inside a string.
1031 ;; So, we convert them manually to (meta char).
1032 ((and (viper-characterp event
)
1033 (< ?\C-? event
) (<= event
255))
1035 event
(- event ?\C-?
1)))
1036 ((and (null mod
) (eq event
'return
))
1038 ((and (null mod
) (eq event
'space
))
1040 ((and (null mod
) (eq event
'delete
))
1042 ((and (null mod
) (eq event
'backspace
))
1044 (t (event-basic-type event
)))
1045 ) ; (featurep 'xemacs)
1047 (if (viper-characterp basis
)
1049 (if (viper= basis ?\C-?
)
1050 (list 'control
'\?) ; taking care of an emacs bug
1051 (intern (char-to-string basis
)))))
1053 (append mod
(list basis
))
1056 (defun viper-key-to-emacs-key (key)
1057 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
1058 (cond ((featurep 'xemacs
) key
)
1061 (setq key-name
(symbol-name key
))
1062 (cond ((= (length key-name
) 1) ; character event
1063 (string-to-char key-name
))
1064 ;; Emacs doesn't recognize `return' and `escape' as events on
1065 ;; dumb terminals, so we translate them into characters
1066 ((and (featurep 'emacs
) (not (viper-window-display-p))
1067 (string= key-name
"return"))
1069 ((and (featurep 'emacs
) (not (viper-window-display-p))
1070 (string= key-name
"escape"))
1072 ;; pass symbol-event as is
1076 (setq modifiers
(viper-subseq key
0 (1- (length key
)))
1077 base-key
(viper-seq-last-elt key
)
1078 base-key-name
(symbol-name base-key
)
1079 char-p
(= (length base-key-name
) 1))
1082 '(lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1086 (car (read-from-string
1089 (mapconcat 'identity mod-char-list
"-\\")
1095 (mapconcat 'identity mod-char-list
"-")
1101 ;; LIS is assumed to be a list of events of characters
1102 (defun viper-eventify-list-xemacs (lis)
1103 (if (featurep 'xemacs
)
1106 (cond ((viper-characterp elt
) (character-to-event elt
))
1109 "viper-eventify-list-xemacs: can't convert to event, %S"
1114 ;; Smoothes out the difference between Emacs' unread-command-events
1115 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1116 ;; events or a sequence of keys.
1118 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1119 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1120 ;; into an event. Below, we delete nil from event lists, since nil is the most
1121 ;; common symbol that might appear in this wrong context.
1122 (defun viper-set-unread-command-events (arg)
1123 (if (featurep 'emacs
)
1125 unread-command-events
1127 (cond ((eventp arg
) (list arg
))
1130 (listify-key-sequence arg
))
1132 "viper-set-unread-command-events: Invalid argument, %S"
1134 (if (not (eventp nil
))
1135 (setq new-events
(delq nil new-events
)))
1136 (append new-events unread-command-events
)))
1139 unread-command-events
1141 (cond ((viper-characterp arg
) (list (character-to-event arg
)))
1142 ((eventp arg
) (list arg
))
1143 ((stringp arg
) (mapcar 'character-to-event arg
))
1144 ((vectorp arg
) (append arg nil
)) ; turn into list
1145 ((listp arg
) (viper-eventify-list-xemacs arg
))
1147 "viper-set-unread-command-events: Invalid argument, %S" arg
)))
1148 unread-command-events
))))
1151 ;; Check if vec is a vector of key-press events representing characters
1153 (defun viper-event-vector-p (vec)
1155 (eval (cons 'and
(mapcar '(lambda (elt) (if (eventp elt
) t
)) vec
)))))
1158 ;; check if vec is a vector of character symbols
1159 (defun viper-char-symbol-sequence-p (vec)
1164 (mapcar (lambda (elt)
1165 (and (symbolp elt
) (= (length (symbol-name elt
)) 1)))
1169 (defun viper-char-array-p (array)
1170 (eval (cons 'and
(mapcar 'viper-characterp array
))))
1173 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1174 ;; convert events to keys and, if all keys are regular printable
1175 ;; characters, will return a string. Otherwise, will return a string
1176 ;; representing a vector of converted events. If the input was a Viper macro,
1177 ;; will return a string that represents this macro as a vector.
1178 (defun viper-array-to-string (event-seq)
1180 (cond ((stringp event-seq
) event-seq
)
1181 ((viper-event-vector-p event-seq
)
1182 (setq temp
(mapcar 'viper-event-key event-seq
))
1183 (cond ((viper-char-symbol-sequence-p temp
)
1184 (mapconcat 'symbol-name temp
""))
1185 ((and (viper-char-array-p
1186 (setq temp2
(mapcar 'viper-key-to-character temp
))))
1187 (mapconcat 'char-to-string temp2
""))
1188 (t (prin1-to-string (vconcat temp
)))))
1189 ((viper-char-symbol-sequence-p event-seq
)
1190 (mapconcat 'symbol-name event-seq
""))
1191 ((and (vectorp event-seq
)
1193 (setq temp
(mapcar 'viper-key-to-character event-seq
))))
1194 (mapconcat 'char-to-string temp
""))
1195 (t (prin1-to-string event-seq
)))))
1197 (defun viper-key-press-events-to-chars (events)
1198 (mapconcat (if (featurep 'xemacs
)
1199 (lambda (elt) (char-to-string (event-to-character elt
))) ; xemacs
1200 'char-to-string
; emacs
1206 (defun viper-read-char-exclusive ()
1208 (echo-keystrokes 1))
1211 (setq char
(read-char))
1213 ;; skip event if not char
1214 (viper-read-event))))
1217 ;; key is supposed to be in viper's representation, e.g., (control l), a
1219 (defun viper-key-to-character (key)
1220 (cond ((eq key
'space
) ?\
)
1221 ((eq key
'delete
) ?\C-?
)
1222 ((eq key
'return
) ?\C-m
)
1223 ((eq key
'backspace
) ?\C-h
)
1225 (= 1 (length (symbol-name key
))))
1226 (string-to-char (symbol-name key
)))
1228 (eq (car key
) 'control
)
1229 (symbol-name (nth 1 key
))
1230 (= 1 (length (symbol-name (nth 1 key
)))))
1231 (read (format "?\\C-%s" (symbol-name (nth 1 key
)))))
1235 (defun viper-setup-master-buffer (&rest other-files-or-buffers
)
1236 "Set up the current buffer as a master buffer.
1237 Arguments become related buffers. This function should normally be used in
1238 the `Local variables' section of a file."
1239 (setq viper-related-files-and-buffers-ring
1240 (make-ring (1+ (length other-files-or-buffers
))))
1241 (mapc '(lambda (elt)
1242 (viper-ring-insert viper-related-files-and-buffers-ring elt
))
1243 other-files-or-buffers
)
1244 (viper-ring-insert viper-related-files-and-buffers-ring
(buffer-name))
1247 ;;; Movement utilities
1249 ;; Characters that should not be considered as part of the word, in reformed-vi
1251 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1252 ;; into characters at some point and then put back to string. The result is
1253 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1254 ;; effect of quoting - and preventing it from being special.
1255 (defconst viper-non-word-characters-reformed-vi
1256 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1257 ;; These are characters that are not to be considered as parts of a word in
1259 ;; Set each time state changes and at loading time
1260 (viper-deflocalvar viper-non-word-characters nil
)
1262 ;; must be buffer-local
1263 (viper-deflocalvar viper-ALPHA-char-class
"w"
1264 "String of syntax classes characterizing Viper's alphanumeric symbols.
1265 In addition, the symbol `_' may be considered alphanumeric if
1266 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1268 (defconst viper-strict-ALPHA-chars
"a-zA-Z0-9_"
1269 "Regexp matching the set of alphanumeric characters acceptable to strict
1271 (defconst viper-strict-SEP-chars
" \t\n"
1272 "Regexp matching the set of alphanumeric characters acceptable to strict
1274 (defconst viper-strict-SEP-chars-sans-newline
" \t"
1275 "Regexp matching the set of alphanumeric characters acceptable to strict
1278 (defconst viper-SEP-char-class
" -"
1279 "String of syntax classes for Vi separators.
1280 Usually contains ` ', linefeed, TAB or formfeed.")
1283 ;; Set Viper syntax classes and related variables according to
1284 ;; `viper-syntax-preference'.
1285 (defun viper-update-syntax-classes (&optional set-default
)
1286 (let ((preference (cond ((eq viper-syntax-preference
'emacs
)
1287 "w") ; Viper words have only Emacs word chars
1288 ((eq viper-syntax-preference
'extended
)
1289 "w_") ; Viper words have Emacs word & symbol chars
1290 (t "w"))) ; Viper words are Emacs words plus `_'
1291 (non-word-chars (cond ((eq viper-syntax-preference
'reformed-vi
)
1292 (viper-string-to-list
1293 viper-non-word-characters-reformed-vi
))
1296 (setq-default viper-ALPHA-char-class preference
1297 viper-non-word-characters non-word-chars
)
1298 (setq viper-ALPHA-char-class preference
1299 viper-non-word-characters non-word-chars
))
1302 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1303 ;; is `nil'. If nil, use setq.
1304 (defun viper-set-syntax-preference (&optional symbol value
)
1305 "Set Viper syntax preference.
1306 If called interactively or if SYMBOL is nil, sets syntax preference in current
1307 buffer. If called non-interactively, preferably via the customization widget,
1308 sets the default value."
1313 "Viper syntax preference: "
1314 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1315 nil
'require-match
)))
1316 (if (stringp value
) (setq value
(intern value
)))
1317 (or (memq value
'(strict-vi reformed-vi extended emacs
))
1318 (error "Invalid Viper syntax preference, %S" value
))
1320 (setq-default viper-syntax-preference value
)
1321 (setq viper-syntax-preference value
))
1322 (viper-update-syntax-classes))
1324 (defcustom viper-syntax-preference
'reformed-vi
1325 "*Syntax type characterizing Viper's alphanumeric symbols.
1326 Affects movement and change commands that deal with Vi-style words.
1327 Works best when set in the hooks to various major modes.
1329 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1331 `reformed-vi' means Viper words are like Emacs words \(as determined using
1332 Emacs syntax tables, which are different for different major modes\) with two
1333 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1334 symbols, such as `,',:,\",),{, etc., are excluded.
1335 This behaves very close to `strict-vi', but also works well with non-ASCII
1336 characters from various alphabets.
1338 `extended' means Viper word constituents are symbols that are marked as being
1339 parts of words OR symbols in Emacs syntax tables.
1340 This is most appropriate for major modes intended for editing programs.
1342 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1344 This option is appropriate if you like Emacs-style words."
1345 :type
'(radio (const strict-vi
) (const reformed-vi
)
1346 (const extended
) (const emacs
))
1347 :set
'viper-set-syntax-preference
1349 (make-variable-buffer-local 'viper-syntax-preference
)
1352 ;; addl-chars are characters to be temporarily considered as alphanumerical
1353 (defun viper-looking-at-alpha (&optional addl-chars
)
1354 (or (stringp addl-chars
) (setq addl-chars
""))
1355 (if (eq viper-syntax-preference
'reformed-vi
)
1356 (setq addl-chars
(concat addl-chars
"_")))
1357 (let ((char (char-after (point))))
1359 (if (eq viper-syntax-preference
'strict-vi
)
1360 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars
"]"))
1362 ;; or one of the additional chars being asked to include
1363 (viper-memq-char char
(viper-string-to-list addl-chars
))
1365 ;; not one of the excluded word chars (note:
1366 ;; viper-non-word-characters is a list)
1367 (not (viper-memq-char char viper-non-word-characters
))
1368 ;; char of the Viper-word syntax class
1369 (viper-memq-char (char-syntax char
)
1370 (viper-string-to-list viper-ALPHA-char-class
))))))
1373 (defun viper-looking-at-separator ()
1374 (let ((char (char-after (point))))
1376 (if (eq viper-syntax-preference
'strict-vi
)
1377 (viper-memq-char char
(viper-string-to-list viper-strict-SEP-chars
))
1378 (or (eq char ?
\n) ; RET is always a separator in Vi
1379 (viper-memq-char (char-syntax char
)
1380 (viper-string-to-list viper-SEP-char-class
)))))
1383 (defsubst viper-looking-at-alphasep
(&optional addl-chars
)
1384 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars
)))
1386 (defun viper-skip-alpha-forward (&optional addl-chars
)
1387 (or (stringp addl-chars
) (setq addl-chars
""))
1390 (cond ((eq viper-syntax-preference
'strict-vi
)
1392 (t viper-ALPHA-char-class
))
1393 (cond ((eq viper-syntax-preference
'strict-vi
)
1394 (concat viper-strict-ALPHA-chars addl-chars
))
1397 (defun viper-skip-alpha-backward (&optional addl-chars
)
1398 (or (stringp addl-chars
) (setq addl-chars
""))
1401 (cond ((eq viper-syntax-preference
'strict-vi
)
1403 (t viper-ALPHA-char-class
))
1404 (cond ((eq viper-syntax-preference
'strict-vi
)
1405 (concat viper-strict-ALPHA-chars addl-chars
))
1408 ;; weird syntax tables may confuse strict-vi style
1409 (defsubst viper-skip-all-separators-forward
(&optional within-line
)
1410 (if (eq viper-syntax-preference
'strict-vi
)
1412 (skip-chars-forward viper-strict-SEP-chars-sans-newline
)
1413 (skip-chars-forward viper-strict-SEP-chars
))
1414 (viper-skip-syntax 'forward
1415 viper-SEP-char-class
1416 (or within-line
"\n")
1417 (if within-line
(viper-line-pos 'end
)))))
1419 (defsubst viper-skip-all-separators-backward
(&optional within-line
)
1420 (if (eq viper-syntax-preference
'strict-vi
)
1422 (skip-chars-backward viper-strict-SEP-chars-sans-newline
)
1423 (skip-chars-backward viper-strict-SEP-chars
))
1424 (viper-skip-syntax 'backward
1425 viper-SEP-char-class
1426 (or within-line
"\n")
1427 (if within-line
(viper-line-pos 'start
)))))
1428 (defun viper-skip-nonseparators (direction)
1431 (concat "^" viper-SEP-char-class
)
1433 (viper-line-pos (if (eq direction
'forward
) 'end
'start
))))
1436 ;; skip over non-word constituents and non-separators
1437 (defun viper-skip-nonalphasep-forward ()
1438 (if (eq viper-syntax-preference
'strict-vi
)
1440 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1443 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1444 ;; Emacs may consider some of these as words, but we don't want them
1445 viper-non-word-characters
1446 (viper-line-pos 'end
))))
1448 (defun viper-skip-nonalphasep-backward ()
1449 (if (eq viper-syntax-preference
'strict-vi
)
1450 (skip-chars-backward
1451 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1454 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1455 ;; Emacs may consider some of these as words, but we don't want them
1456 viper-non-word-characters
1457 (viper-line-pos 'start
))))
1459 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1460 ;; Return the number of chars traveled.
1461 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1462 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1463 ;; words, even if Emacs syntax table says they are.
1464 (defun viper-skip-syntax (direction syntax addl-chars
&optional limit
)
1468 (if (eq direction
'forward
)
1469 'skip-chars-forward
'skip-chars-backward
))
1471 (if (eq direction
'forward
)
1472 'viper-forward-char-carefully
'viper-backward-char-carefully
))
1473 char-looked-at syntax-of-char-looked-at negated-syntax
)
1475 (cond ((listp addl-chars
) (viper-charlist-to-string addl-chars
))
1476 ((stringp addl-chars
) addl-chars
)
1479 (cond ((listp syntax
) syntax
)
1480 ((stringp syntax
) (viper-string-to-list syntax
))
1482 (if (memq ?^ syntax
) (setq negated-syntax t
))
1484 (while (and (not (= local
0))
1485 (cond ((eq direction
'forward
)
1488 (setq char-looked-at
(viper-char-at-pos direction
)
1489 ;; if outside the range, set to nil
1490 syntax-of-char-looked-at
(if char-looked-at
1491 (char-syntax char-looked-at
)))
1494 (cond ((and limit
(eq direction
'forward
))
1496 (limit ; backward & limit
1499 ;; char under/before cursor has appropriate syntax
1501 (not (memq syntax-of-char-looked-at syntax
))
1502 (memq syntax-of-char-looked-at syntax
))
1503 ;; if char-syntax class is "word", make sure it is not one
1504 ;; of the excluded characters
1505 (if (and (eq syntax-of-char-looked-at ?w
)
1506 (not negated-syntax
))
1507 (not (viper-memq-char
1508 char-looked-at viper-non-word-characters
))
1510 (funcall skip-syntax-func
1)
1512 (funcall skip-chars-func addl-chars limit
)))
1513 (setq total
(+ total local
)))
1517 ;; tells when point is at the beginning of field
1518 (defun viper-beginning-of-field ()
1520 (not (eq (get-char-property (point) 'field
)
1521 (get-char-property (1- (point)) 'field
)))))
1524 ;; this is copied from cl-extra.el
1525 ;; Return the subsequence of SEQ from START to END.
1526 ;; If END is omitted, it defaults to the length of the sequence.
1527 ;; If START or END is negative, it counts from the end.
1528 (defun viper-subseq (seq start
&optional end
)
1529 (if (stringp seq
) (substring seq start end
)
1531 (and end
(< end
0) (setq end
(+ end
(setq len
(length seq
)))))
1532 (if (< start
0) (setq start
(+ start
(or len
(setq len
(length seq
))))))
1534 (if (> start
0) (setq seq
(nthcdr start seq
)))
1537 (while (>= (setq end
(1- end
)) start
)
1538 (push (pop seq
) res
))
1540 (copy-sequence seq
)))
1542 (or end
(setq end
(or len
(length seq
))))
1543 (let ((res (make-vector (max (- end start
) 0) nil
))
1545 (while (< start end
)
1546 (aset res i
(aref seq start
))
1547 (setq i
(1+ i
) start
(1+ start
)))
1553 ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1556 ;; arch-tag: 7f023fd5-dd9e-4378-a397-9c179553b0e3
1557 ;;; viper-util.el ends here