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 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
30 (defvar viper-overriding-map
)
31 (defvar pm-color-alist
)
32 (defvar zmacs-region-stays
)
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
)
52 ;; A fix for NeXT Step
53 ;; Should go away, when NS people fix the design flaw, which leaves the
54 ;; two x-* functions undefined.
55 (if (and (not (fboundp 'x-display-color-p
)) (fboundp 'ns-display-color-p
))
56 (fset 'x-display-color-p
(symbol-function 'ns-display-color-p
)))
57 (if (and (not (fboundp 'x-color-defined-p
)) (fboundp 'ns-color-defined-p
))
58 (fset 'x-color-defined-p
(symbol-function 'ns-color-defined-p
)))
61 (defalias 'viper-overlay-p
62 (if (featurep 'xemacs
) 'extentp
'overlayp
))
63 (defalias 'viper-make-overlay
64 (if (featurep 'xemacs
) 'make-extent
'make-overlay
))
65 (defalias 'viper-overlay-live-p
66 (if (featurep 'xemacs
) 'extent-live-p
'overlayp
))
67 (defalias 'viper-move-overlay
68 (if (featurep 'xemacs
) 'set-extent-endpoints
'move-overlay
))
69 (defalias 'viper-overlay-start
70 (if (featurep 'xemacs
) 'extent-start-position
'overlay-start
))
71 (defalias 'viper-overlay-end
72 (if (featurep 'xemacs
) 'extent-end-position
'overlay-end
))
73 (defalias 'viper-overlay-get
74 (if (featurep 'xemacs
) 'extent-property
'overlay-get
))
75 (defalias 'viper-overlay-put
76 (if (featurep 'xemacs
) 'set-extent-property
'overlay-put
))
77 (defalias 'viper-read-event
78 (if (featurep 'xemacs
) 'next-command-event
'read-event
))
79 (defalias 'viper-characterp
80 (if (featurep 'xemacs
) 'characterp
'integerp
))
81 (defalias 'viper-int-to-char
82 (if (featurep 'xemacs
) 'int-to-char
'identity
))
83 (defalias 'viper-get-face
84 (if (featurep 'xemacs
) 'get-face
'internal-get-face
))
85 (defalias 'viper-color-defined-p
86 (if (featurep 'xemacs
) 'valid-color-name-p
'x-color-defined-p
))
87 (defalias 'viper-iconify
88 (if (featurep 'xemacs
) 'iconify-frame
'iconify-or-deiconify-frame
))
91 ;; CHAR is supposed to be a char or an integer (positive or negative)
92 ;; LIST is a list of chars, nil, and negative numbers
93 ;; Check if CHAR is a member by trying to convert in characters, if necessary.
94 ;; Introduced for compatibility with XEmacs, where integers are not the same as
96 (defun viper-memq-char (char list
)
97 (cond ((and (integerp char
) (>= char
0))
98 (memq (viper-int-to-char char
) list
))
101 ;; Check if char-or-int and char are the same as characters
102 (defun viper-char-equal (char-or-int char
)
103 (cond ((and (integerp char-or-int
) (>= char-or-int
0))
104 (= (viper-int-to-char char-or-int
) char
))
105 ((eq char-or-int char
))))
107 ;; Like =, but accommodates null and also is t for eq-objects
108 (defun viper= (char char1
)
109 (cond ((eq char char1
) t
)
110 ((and (viper-characterp char
) (viper-characterp char1
))
114 (defsubst viper-color-display-p
()
115 (viper-cond-compile-for-xemacs-or-emacs
116 (eq (device-class (selected-device)) 'color
) ; xemacs
117 (x-display-color-p) ; emacs
120 (defun viper-get-cursor-color (&optional frame
)
121 (viper-cond-compile-for-xemacs-or-emacs
123 (frame-property (or frame
(selected-frame)) 'cursor-color
)) ; xemacs
124 (cdr (assoc 'cursor-color
(frame-parameters))) ; emacs
129 (cond ((eq (viper-device-type) 'pm
)
130 (fset 'viper-color-defined-p
131 (lambda (color) (assoc color pm-color-alist
)))))
135 (defun viper-change-cursor-color (new-color &optional frame
)
136 (if (and (viper-window-display-p) (viper-color-display-p)
137 (stringp new-color
) (viper-color-defined-p new-color
)
138 (not (string= new-color
(viper-get-cursor-color))))
139 (viper-cond-compile-for-xemacs-or-emacs
141 (or frame
(selected-frame))
142 'cursor-color
(make-color-instance new-color
))
143 (modify-frame-parameters
144 (or frame
(selected-frame))
145 (list (cons 'cursor-color new-color
)))
149 (defun viper-set-cursor-color-according-to-state (&optional frame
)
150 (cond ((eq viper-current-state
'replace-state
)
151 (viper-change-cursor-color viper-replace-overlay-cursor-color frame
))
152 ((and (eq viper-current-state
'emacs-state
)
153 viper-emacs-state-cursor-color
)
154 (viper-change-cursor-color viper-emacs-state-cursor-color frame
))
155 ((eq viper-current-state
'insert-state
)
156 (viper-change-cursor-color viper-insert-state-cursor-color frame
))
158 (viper-change-cursor-color viper-vi-state-cursor-color frame
))))
160 ;; By default, saves current frame cursor color in the
161 ;; viper-saved-cursor-color-in-replace-mode property of viper-replace-overlay
162 (defun viper-save-cursor-color (before-which-mode)
163 (if (and (viper-window-display-p) (viper-color-display-p))
164 (let ((color (viper-get-cursor-color)))
165 (if (and (stringp color
) (viper-color-defined-p color
)
166 (not (string= color viper-replace-overlay-cursor-color
)))
167 (modify-frame-parameters
171 (cond ((eq before-which-mode
'before-replace-mode
)
172 'viper-saved-cursor-color-in-replace-mode
)
173 ((eq before-which-mode
'before-emacs-mode
)
174 'viper-saved-cursor-color-in-emacs-mode
)
176 'viper-saved-cursor-color-in-insert-mode
))
181 (defsubst viper-get-saved-cursor-color-in-replace-mode
()
184 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
186 'viper-saved-cursor-color-in-replace-mode
)
187 (if (and (eq viper-current-state
'emacs-mode
) viper-emacs-state-cursor-color
)
188 viper-emacs-state-cursor-color
189 viper-vi-state-cursor-color
)))
191 (defsubst viper-get-saved-cursor-color-in-insert-mode
()
194 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
196 'viper-saved-cursor-color-in-insert-mode
)
197 (if (and (eq viper-current-state
'emacs-mode
) viper-emacs-state-cursor-color
)
198 viper-emacs-state-cursor-color
199 viper-vi-state-cursor-color
)))
201 (defsubst viper-get-saved-cursor-color-in-emacs-mode
()
204 (if (featurep 'emacs
) 'frame-parameter
'frame-property
)
206 'viper-saved-cursor-color-in-emacs-mode
)
207 viper-vi-state-cursor-color
))
209 ;; restore cursor color from replace overlay
210 (defun viper-restore-cursor-color(after-which-mode)
211 (if (viper-overlay-p viper-replace-overlay
)
212 (viper-change-cursor-color
213 (cond ((eq after-which-mode
'after-replace-mode
)
214 (viper-get-saved-cursor-color-in-replace-mode))
215 ((eq after-which-mode
'after-emacs-mode
)
216 (viper-get-saved-cursor-color-in-emacs-mode))
217 (t (viper-get-saved-cursor-color-in-insert-mode)))
221 ;; Check the current version against the major and minor version numbers
222 ;; using op: cur-vers op major.minor If emacs-major-version or
223 ;; emacs-minor-version are not defined, we assume that the current version
224 ;; is hopelessly outdated. We assume that emacs-major-version and
225 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
226 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
227 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
228 ;; incorrect. However, this gives correct result in our cases, since we are
229 ;; testing for sufficiently high Emacs versions.
230 (defun viper-check-version (op major minor
&optional type-of-emacs
)
231 (if (and (boundp 'emacs-major-version
) (boundp 'emacs-minor-version
))
232 (and (cond ((eq type-of-emacs
'xemacs
) (featurep 'xemacs
))
233 ((eq type-of-emacs
'emacs
) (featurep 'emacs
))
235 (cond ((eq op
'=) (and (= emacs-minor-version minor
)
236 (= emacs-major-version major
)))
237 ((memq op
'(> >= < <=))
238 (and (or (funcall op emacs-major-version major
)
239 (= emacs-major-version major
))
240 (if (= emacs-major-version major
)
241 (funcall op emacs-minor-version minor
)
244 (error "%S: Invalid op in viper-check-version" op
))))
245 (cond ((memq op
'(= > >=)) nil
)
246 ((memq op
'(< <=)) t
))))
249 (defun viper-get-visible-buffer-window (wind)
250 (if (featurep 'xemacs
)
251 (get-buffer-window wind t
)
252 (get-buffer-window wind
'visible
)))
255 ;; Return line position.
256 ;; If pos is 'start then returns position of line start.
257 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
258 ;; Pos = 'indent returns beginning of indentation.
259 ;; Otherwise, returns point. Current point is not moved in any case."
260 (defun viper-line-pos (pos)
261 (let ((cur-pos (point))
269 (goto-char (+ (viper-line-pos 'start
) (viper-line-pos 'end
) 2)))
271 (back-to-indentation))
273 (setq result
(point))
277 ;; Emacs used to count each multibyte character as several positions in the buffer,
278 ;; so we had to use Emacs' chars-in-region to count characters. Since 20.3,
279 ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
280 ;; counting each char as just one pos. So, now we can simply subtract beg from
281 ;; end to determine the number of characters in a region.
282 (defun viper-chars-in-region (beg end
&optional preserve-sign
)
283 ;;(let ((count (abs (if (fboundp 'chars-in-region)
284 ;; (chars-in-region beg end)
286 (let ((count (abs (- end beg
))))
287 (if (and (< end beg
) preserve-sign
)
291 ;; Test if POS is between BEG and END
292 (defsubst viper-pos-within-region
(pos beg end
)
293 (and (>= pos
(min beg end
)) (>= (max beg end
) pos
)))
296 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
297 ;; The first argument must eval to a variable name.
298 ;; Arguments: (var-name position &optional buffer).
300 ;; This is useful for moving markers that are supposed to be local.
301 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
302 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
303 ;; buffer, a new marker will be created.
304 (defun viper-move-marker-locally (var pos
&optional buffer
)
305 (if (markerp (eval var
))
307 (set var
(make-marker)))
308 (move-marker (eval var
) pos buffer
))
311 ;; Print CONDITIONS as a message.
312 (defun viper-message-conditions (conditions)
313 (let ((case (car conditions
)) (msg (cdr conditions
)))
316 (message "%s: %s" case
(mapconcat 'prin1-to-string msg
" ")))
321 ;;; List/alist utilities
323 ;; Convert LIST to an alist
324 (defun viper-list-to-alist (lst)
327 (setq alist
(cons (list (car lst
)) alist
))
328 (setq lst
(cdr lst
)))
331 ;; Convert ALIST to a list.
332 (defun viper-alist-to-list (alst)
335 (setq lst
(cons (car (car alst
)) lst
))
336 (setq alst
(cdr alst
)))
339 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
340 (defun viper-filter-alist (regexp alst
)
342 (let ((outalst) (inalst alst
))
344 (if (string-match regexp
(car (car inalst
)))
345 (setq outalst
(cons (car inalst
) outalst
)))
346 (setq inalst
(cdr inalst
)))
349 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
350 (defun viper-filter-list (regexp lst
)
352 (let ((outlst) (inlst lst
))
354 (if (string-match regexp
(car inlst
))
355 (setq outlst
(cons (car inlst
) outlst
)))
356 (setq inlst
(cdr inlst
)))
360 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
361 ;; LIS2 is modified by filtering it: deleting its members of the form
362 ;; \(car elt\) such that (car elt') is in LIS1.
363 (defun viper-append-filter-alist (lis1 lis2
)
366 ;;filter-append the second list
368 ;; delete all occurrences
369 (while (setq elt
(assoc (car (car temp
)) lis2
))
370 (setq lis2
(delq elt lis2
)))
371 (setq temp
(cdr temp
)))
377 (declare-function viper-forward-Word
"viper-cmd" (arg))
379 ;;; Support for :e, :r, :w file globbing
381 ;; Glob the file spec.
382 ;; This function is designed to work under Unix. It might also work under VMS.
383 (defun viper-glob-unix-files (filespec)
385 (cond (ex-unix-type-shell shell-file-name
)
386 ((memq system-type
'(vax-vms axp-vms
)) "*dcl*") ; VAX VMS
387 (t "sh"))) ; probably Unix anyway
389 ;; using cond in anticipation of further additions
390 (cond (ex-unix-type-shell-options)
392 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec
))
393 (t (format "ls -1 -d %s" filespec
))))
396 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
400 (call-process gshell nil t nil
404 (call-process gshell nil t nil
407 (goto-char (point-min))
408 ;; Issue an error, if no match.
409 (unless (eq 0 status
)
411 (skip-chars-forward " \t\n\j")
412 (if (looking-at "ls:")
413 (viper-forward-Word 1))
418 (buffer-substring (point) (viper-line-pos 'end
)))
420 (goto-char (point-min))
421 (viper-get-filenames-from-buffer 'one-per-line
))
425 ;; Interpret the stuff in the buffer as a list of file names
426 ;; return a list of file names listed in the buffer beginning at point
427 ;; If optional arg is supplied, assume each filename is listed on a separate
429 (defun viper-get-filenames-from-buffer (&optional one-per-line
)
430 (let ((skip-chars (if one-per-line
"\t\n" " \t\n"))
432 (skip-chars-forward skip-chars
)
434 (if (cond ((looking-at "\"")
436 (re-search-forward "[^\"]+" nil t
)) ; noerror
439 (re-search-forward "[^']+" nil t
)) ; noerror
442 (concat "[^" skip-chars
"]+") nil t
))) ;noerror
444 (buffer-substring (match-beginning 0) (match-end 0))))
447 (skip-chars-forward " \t\n")
448 (setq result
(cons fname result
)))
451 ;; convert MS-DOS wildcards to regexp
452 (defun viper-wildcard-to-regexp (wcard)
454 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
457 (goto-char (point-min))
459 (skip-chars-forward "^*?.\\\\")
460 (cond ((eq (char-after (point)) ?
*) (insert ".")(forward-char 1))
461 ((eq (char-after (point)) ?.
) (insert "\\")(forward-char 1))
462 ((eq (char-after (point)) ?
\\) (insert "\\")(forward-char 1))
463 ((eq (char-after (point)) ??
) (delete-char 1)(insert ".")))
469 ;; glob windows files
470 ;; LIST is expected to be in reverse order
471 (defun viper-glob-mswindows-files (filespec)
472 (let ((case-fold-search t
)
475 (set-buffer (get-buffer-create viper-ex-tmp-buf-name
))
478 (goto-char (point-min))
479 (setq tmp
(viper-get-filenames-from-buffer))
481 (setq tmp2
(cons (directory-files
482 ;; the directory part
483 (or (file-name-directory (car tmp
))
485 t
; return full names
486 ;; the regexp part: globs the file names
488 (viper-wildcard-to-regexp
489 (file-name-nondirectory (car tmp
)))
492 (setq tmp
(cdr tmp
)))
493 (reverse (apply 'append tmp2
)))))
498 ;; Rotate RING's index. DIRection can be positive or negative.
499 (defun viper-ring-rotate1 (ring dir
)
500 (if (and (ring-p ring
) (> (ring-length ring
) 0))
502 (setcar ring
(cond ((> dir
0)
503 (ring-plus1 (car ring
) (ring-length ring
)))
505 (ring-minus1 (car ring
) (ring-length ring
)))
506 ;; don't rotate if dir = 0
508 (viper-current-ring-item ring
)
511 (defun viper-special-ring-rotate1 (ring dir
)
512 (if (memq viper-intermediate-command
513 '(repeating-display-destructive-command
514 repeating-insertion-from-ring
))
515 (viper-ring-rotate1 ring dir
)
516 ;; don't rotate otherwise
517 (viper-ring-rotate1 ring
0)))
519 ;; current ring item; if N is given, then so many items back from the
521 (defun viper-current-ring-item (ring &optional n
)
523 (if (and (ring-p ring
) (> (ring-length ring
) 0))
524 (aref (cdr (cdr ring
)) (mod (- (car ring
) 1 n
) (ring-length ring
)))))
526 ;; Push item onto ring. The second argument is a ring-variable, not value.
527 (defun viper-push-onto-ring (item ring-var
)
528 (or (ring-p (eval ring-var
))
529 (set ring-var
(make-ring (eval (intern (format "%S-size" ring-var
))))))
530 (or (null item
) ; don't push nil
531 (and (stringp item
) (string= item
"")) ; or empty strings
532 (equal item
(viper-current-ring-item (eval ring-var
))) ; or old stuff
533 ;; Since viper-set-destructive-command checks if we are inside
534 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
535 ;; cmd viper-repeat makes a call to the current function only if `.' is
536 ;; executing a command from the command history. It doesn't call the
537 ;; push-onto-ring function if `.' is simply repeating the last
538 ;; destructive command. We only check for ESC (which happens when we do
539 ;; insert with a prefix argument, or if this-command-keys doesn't give
540 ;; anything meaningful (in that case we don't know what to show to the
542 (and (eq ring-var
'viper-command-ring
)
543 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
544 (viper-array-to-string (this-command-keys))))
545 (viper-ring-insert (eval ring-var
) item
))
549 ;; removing elts from ring seems to break it
550 (defun viper-cleanup-ring (ring)
551 (or (< (ring-length ring
) 2)
552 (null (viper-current-ring-item ring
))
553 ;; last and previous equal
554 (if (equal (viper-current-ring-item ring
)
555 (viper-current-ring-item ring
1))
556 (viper-ring-pop ring
))))
558 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
559 (defun viper-ring-pop (ring)
560 (let* ((ln (ring-length ring
))
561 (vec (cdr (cdr ring
)))
562 (veclen (length vec
))
564 (idx (max 0 (ring-minus1 hd ln
)))
565 (top-elt (aref vec idx
)))
568 (while (< (1+ idx
) veclen
)
569 (aset vec idx
(aref vec
(1+ idx
)))
573 (setq hd
(max 0 (ring-minus1 hd ln
)))
574 (if (= hd
(1- ln
)) (setq hd
0))
575 (setcar ring hd
) ; move head
576 (setcar (cdr ring
) (max 0 (1- ln
))) ; adjust length
580 (defun viper-ring-insert (ring item
)
581 (let* ((ln (ring-length ring
))
582 (vec (cdr (cdr ring
)))
583 (veclen (length vec
))
585 (vecpos-after-hd (if (= hd
0) ln hd
))
590 (aset vec hd item
) ; hd is always 1+ the actual head index in vec
591 (setcar ring
(ring-plus1 hd ln
)))
592 (setcar (cdr ring
) (1+ ln
))
593 (setcar ring
(ring-plus1 vecpos-after-hd
(1+ ln
)))
594 (while (and (>= idx vecpos-after-hd
) (> ln
0))
595 (aset vec idx
(aref vec
(1- idx
)))
597 (aset vec vecpos-after-hd item
))
603 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
604 ;; PRE-STRING is a string to prepend to the abbrev string.
605 ;; POST-STRING is a string to append to the abbrev string.
606 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
607 ;; if the orig string was truncated.
608 (defun viper-abbreviate-string (string max-len
609 pre-string post-string abbrev-sign
)
613 (substring string
0 (min max-len
(length string
)))))
614 (cond ((null truncated-str
) "")
615 ((> (length string
) max-len
)
617 pre-string truncated-str abbrev-sign post-string
))
618 (t (format "%s%s%s" pre-string truncated-str post-string
)))))
620 ;; tells if we are over a whitespace-only line
621 (defsubst viper-over-whitespace-line
()
624 (looking-at "^[ \t]*$")))
627 ;;; Saving settings in custom file
629 ;; Save the current setting of VAR in CUSTOM-FILE.
630 ;; If given, MESSAGE is a message to be displayed after that.
631 ;; This message is erased after 2 secs, if erase-msg is non-nil.
632 ;; Arguments: var message custom-file &optional erase-message
633 (defun viper-save-setting (var message custom-file
&optional erase-msg
)
634 (let* ((var-name (symbol-name var
))
635 (var-val (if (boundp var
) (eval var
)))
636 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name
))
637 (buf (find-file-noselect (substitute-in-file-name custom-file
)))
639 (message "%s" (or message
""))
642 (goto-char (point-min))
643 (if (re-search-forward regexp nil t
)
644 (let ((reg-end (1- (match-end 0))))
645 (search-backward var-name
)
646 (delete-region (match-beginning 0) reg-end
)
647 (goto-char (match-beginning 0))
648 (insert (format "%s '%S" var-name var-val
)))
649 (goto-char (point-max))
650 (if (not (bolp)) (insert "\n"))
651 (insert (format "(setq %s '%S)\n" var-name var-val
)))
660 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
661 ;; match this pattern.
662 (defun viper-save-string-in-file (string custom-file
&optional pattern
)
663 (let ((buf (find-file-noselect (substitute-in-file-name custom-file
))))
666 (let (buffer-read-only)
667 (goto-char (point-min))
668 (if pattern
(delete-matching-lines pattern
))
669 (goto-char (point-max))
670 (if string
(insert string
))
676 ;; define remote file test
677 (defun viper-file-remote-p (file-name)
678 (file-remote-p file-name
))
681 ;; This is a simple-minded check for whether a file is under version control.
682 ;; If file,v exists but file doesn't, this file is considered to be not checked
683 ;; in and not checked out for the purpose of patching (since patch won't be
684 ;; able to read such a file anyway).
685 ;; FILE is a string representing file name
686 ;;(defun viper-file-under-version-control (file)
687 ;; (let* ((filedir (file-name-directory file))
688 ;; (file-nondir (file-name-nondirectory file))
689 ;; (trial (concat file-nondir ",v"))
690 ;; (full-trial (concat filedir trial))
691 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
692 ;; (and (stringp file)
693 ;; (file-exists-p file)
696 ;; (file-exists-p full-trial)
697 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
698 ;; ;; don't be fooled by this!
699 ;; (not (equal (file-attributes file)
700 ;; (file-attributes full-trial))))
701 ;; ;; check if a version is in RCS/ directory
702 ;; (file-exists-p full-rcs-trial)))
706 (defsubst viper-file-checked-in-p
(file)
707 (and (featurep 'vc-hooks
)
708 ;; CVS files are considered not checked in
709 ;; FIXME: Should this deal with more than CVS?
710 (not (memq (vc-backend file
) '(nil CVS
)))
711 (if (fboundp 'vc-state
)
713 (not (memq (vc-state file
) '(edited needs-merge
)))
714 (not (stringp (vc-state file
))))
715 ;; XEmacs has no vc-state
716 (if (featurep 'xemacs
)(not (vc-locking-user file
))))
719 ;; checkout if visited file is checked in
720 (defun viper-maybe-checkout (buf)
721 (let ((file (expand-file-name (buffer-file-name buf
)))
722 (checkout-function (key-binding "\C-x\C-q")))
723 (if (and (viper-file-checked-in-p file
)
727 "File %s is checked in. Check it out? "
728 (viper-abbreviate-file-name file
))))
729 (with-current-buffer buf
730 (command-execute checkout-function
)))))
736 (defun viper-put-on-search-overlay (beg end
)
737 (if (viper-overlay-p viper-search-overlay
)
738 (viper-move-overlay viper-search-overlay beg end
)
739 (setq viper-search-overlay
(viper-make-overlay beg end
(current-buffer)))
741 viper-search-overlay
'priority viper-search-overlay-priority
))
742 (viper-overlay-put viper-search-overlay
'face viper-search-face
))
746 (defun viper-flash-search-pattern ()
747 (if (not (viper-has-face-support-p))
749 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
751 (viper-overlay-put viper-search-overlay
'face nil
)))
753 (defun viper-hide-search-overlay ()
754 (if (not (viper-overlay-p viper-search-overlay
))
756 (setq viper-search-overlay
757 (viper-make-overlay (point-min) (point-min) (current-buffer)))
759 viper-search-overlay
'priority viper-search-overlay-priority
)))
760 (viper-overlay-put viper-search-overlay
'face nil
))
764 (defsubst viper-move-replace-overlay
(beg end
)
765 (viper-move-overlay viper-replace-overlay beg end
))
767 (defun viper-set-replace-overlay (beg end
)
768 (if (viper-overlay-live-p viper-replace-overlay
)
769 (viper-move-replace-overlay beg end
)
770 (setq viper-replace-overlay
(viper-make-overlay beg end
(current-buffer)))
773 viper-replace-overlay
(if (featurep 'emacs
) 'evaporate
'detachable
) nil
)
775 viper-replace-overlay
'priority viper-replace-overlay-priority
)
776 ;; If Emacs will start supporting overlay maps, as it currently supports
777 ;; text-property maps, we could do away with viper-replace-minor-mode and
778 ;; just have keymap attached to replace overlay.
780 ;; viper-replace-overlay
781 ;; (if (featurep 'xemacs) 'keymap 'local-map)
782 ;; viper-replace-map)
784 (if (viper-has-face-support-p)
786 viper-replace-overlay
'face viper-replace-overlay-face
))
787 (viper-save-cursor-color 'before-replace-mode
)
788 (viper-change-cursor-color viper-replace-overlay-cursor-color
)
792 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph
)
793 (or (viper-overlay-live-p viper-replace-overlay
)
794 (viper-set-replace-overlay (point-min) (point-min)))
795 (if (or (not (viper-has-face-support-p))
796 viper-use-replace-region-delimiters
)
797 (let ((before-name (if (featurep 'xemacs
) 'begin-glyph
'before-string
))
798 (after-name (if (featurep 'xemacs
) 'end-glyph
'after-string
)))
799 (viper-overlay-put viper-replace-overlay before-name before-glyph
)
800 (viper-overlay-put viper-replace-overlay after-name after-glyph
))))
802 (defun viper-hide-replace-overlay ()
803 (viper-set-replace-overlay-glyphs nil nil
)
804 (viper-restore-cursor-color 'after-replace-mode
)
805 (viper-restore-cursor-color 'after-insert-mode
)
806 (if (viper-has-face-support-p)
807 (viper-overlay-put viper-replace-overlay
'face nil
)))
810 (defsubst viper-replace-start
()
811 (viper-overlay-start viper-replace-overlay
))
812 (defsubst viper-replace-end
()
813 (viper-overlay-end viper-replace-overlay
))
818 (defun viper-set-minibuffer-overlay ()
819 (viper-check-minibuffer-overlay)
820 (if (viper-has-face-support-p)
823 viper-minibuffer-overlay
'face viper-minibuffer-current-face
)
825 viper-minibuffer-overlay
'priority viper-minibuffer-overlay-priority
)
828 viper-minibuffer-overlay
829 (if (featurep 'emacs
) 'evaporate
'detachable
)
831 ;; make viper-minibuffer-overlay open-ended
832 ;; In emacs, it is made open ended at creation time
833 (if (featurep 'xemacs
)
835 (viper-overlay-put viper-minibuffer-overlay
'start-open nil
)
836 (viper-overlay-put viper-minibuffer-overlay
'end-open nil
)))
839 (defun viper-check-minibuffer-overlay ()
840 (if (viper-overlay-live-p viper-minibuffer-overlay
)
842 viper-minibuffer-overlay
843 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
845 (setq viper-minibuffer-overlay
846 (if (featurep 'xemacs
)
847 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
848 ;; make overlay open-ended
850 (if (fboundp 'minibuffer-prompt-end
) (minibuffer-prompt-end) 1)
852 (current-buffer) nil
'rear-advance
)))
856 (defsubst viper-is-in-minibuffer
()
858 (string-match "\*Minibuf-" (buffer-name))))
862 ;;; XEmacs compatibility
864 (defun viper-abbreviate-file-name (file)
865 (viper-cond-compile-for-xemacs-or-emacs
866 ;; XEmacs requires addl argument
867 (abbreviate-file-name file t
)
869 (abbreviate-file-name file
)
872 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
873 ;; in sit-for, so this function smoothes out the differences.
874 (defsubst viper-sit-for-short
(val &optional nodisp
)
875 (sit-for (/ val
1000.0) nodisp
))
877 ;; EVENT may be a single event of a sequence of events
878 (defsubst viper-ESC-event-p
(event)
879 (let ((ESC-keys '(?\e
(control \
[) escape
))
880 (key (viper-event-key event
)))
881 (member key ESC-keys
)))
883 ;; checks if object is a marker, has a buffer, and points to within that buffer
884 (defun viper-valid-marker (marker)
885 (if (and (markerp marker
) (marker-buffer marker
))
886 (let ((buf (marker-buffer marker
))
887 (pos (marker-position marker
)))
890 (and (<= pos
(point-max)) (<= (point-min) pos
))))))
892 (defsubst viper-mark-marker
()
893 (viper-cond-compile-for-xemacs-or-emacs
894 (mark-marker t
) ; xemacs
895 (mark-marker) ; emacs
898 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
899 ;; is the same as (mark t).
900 (defsubst viper-set-mark-if-necessary
()
901 (setq mark-ring
(delete (viper-mark-marker) mark-ring
))
902 (set-mark-command nil
)
903 (setq viper-saved-mark
(point)))
905 ;; In transient mark mode (zmacs mode), it is annoying when regions become
906 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
907 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
908 (defun viper-deactivate-mark ()
909 (viper-cond-compile-for-xemacs-or-emacs
910 (zmacs-deactivate-region)
914 (defsubst viper-leave-region-active
()
915 (viper-cond-compile-for-xemacs-or-emacs
916 (setq zmacs-region-stays t
)
920 ;; Check if arg is a valid character for register
921 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
922 ;; Letter means lowercase letters, Letter means uppercase letters, and
923 ;; digit means digits from 1 to 9.
924 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
925 (defun viper-valid-register (reg &optional type
)
926 (or type
(setq type
'(letter Letter digit
)))
927 (or (if (memq 'letter type
)
928 (and (<= ?a reg
) (<= reg ?z
)))
929 (if (memq 'digit type
)
930 (and (<= ?
1 reg
) (<= reg ?
9)))
931 (if (memq 'Letter type
)
932 (and (<= ?A reg
) (<= reg ?Z
)))
937 ;; it is suggested that an event must be copied before it is assigned to
938 ;; last-command-event in XEmacs
939 (defun viper-copy-event (event)
940 (viper-cond-compile-for-xemacs-or-emacs
941 (copy-event event
) ; xemacs
945 ;; Uses different timeouts for ESC-sequences and others
946 (defsubst viper-fast-keysequence-p
()
947 (not (viper-sit-for-short
948 (if (viper-ESC-event-p last-input-event
)
949 viper-ESC-keyseq-timeout
950 viper-fast-keyseq-timeout
)
953 ;; like read-event, but in XEmacs also try to convert to char, if possible
954 (defun viper-read-event-convert-to-char ()
956 (viper-cond-compile-for-xemacs-or-emacs
958 (setq event
(next-command-event))
959 (or (event-to-character event
)
965 ;; Viperized read-key-sequence
966 (defun viper-read-key-sequence (prompt &optional continue-echo
)
967 (let (inhibit-quit event keyseq
)
968 (setq keyseq
(read-key-sequence prompt continue-echo
))
969 (setq event
(if (featurep 'xemacs
)
970 (elt keyseq
0) ; XEmacs returns vector of events
971 (elt (listify-key-sequence keyseq
) 0)))
972 (if (viper-ESC-event-p event
)
973 (let (unread-command-events)
974 (if (viper-fast-keysequence-p)
975 (let ((viper-vi-global-user-minor-mode nil
)
976 (viper-vi-local-user-minor-mode nil
)
977 (viper-vi-intercept-minor-mode nil
)
978 (viper-insert-intercept-minor-mode nil
)
979 (viper-replace-minor-mode nil
) ; actually unnecessary
980 (viper-insert-global-user-minor-mode nil
)
981 (viper-insert-local-user-minor-mode nil
))
982 ;; Note: set unread-command-events only after testing for fast
983 ;; keysequence. Otherwise, viper-fast-keysequence-p will be
984 ;; always t -- whether there is anything after ESC or not
985 (viper-set-unread-command-events keyseq
)
986 (setq keyseq
(read-key-sequence nil
)))
987 (viper-set-unread-command-events keyseq
)
988 (setq keyseq
(read-key-sequence nil
)))))
992 ;; This function lets function-key-map convert key sequences into logical
993 ;; keys. This does a better job than viper-read-event when it comes to kbd
994 ;; macros, since it enables certain macros to be shared between X and TTY modes
995 ;; by correctly mapping key sequences for Left/Right/... (one an ascii
996 ;; terminal) into logical keys left, right, etc.
997 (defun viper-read-key ()
998 (let ((overriding-local-map viper-overriding-map
)
1001 (use-global-map viper-overriding-map
)
1003 (setq key
(elt (viper-read-key-sequence nil
) 0))
1004 (use-global-map global-map
))
1008 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
1009 ;; instead of nil, if '(nil) was previously inadvertently assigned to
1010 ;; unread-command-events
1011 (defun viper-event-key (event)
1012 (or (and event
(eventp event
))
1013 (error "viper-event-key: Wrong type argument, eventp, %S" event
))
1014 (when (viper-cond-compile-for-xemacs-or-emacs
1015 (or (key-press-event-p event
) (mouse-event-p event
)) ; xemacs
1018 (let ((mod (event-modifiers event
))
1021 (viper-cond-compile-for-xemacs-or-emacs
1023 (cond ((key-press-event-p event
)
1025 ((button-event-p event
)
1026 (concat "mouse-" (prin1-to-string (event-button event
))))
1028 (error "viper-event-key: Unknown event, %S" event
)))
1029 ;; Emacs doesn't handle capital letters correctly, since
1030 ;; \S-a isn't considered the same as A (it behaves as
1031 ;; plain `a' instead). So we take care of this here
1032 (cond ((and (viper-characterp event
) (<= ?A event
) (<= event ?Z
))
1035 ;; Emacs has the oddity whereby characters 128+char
1036 ;; represent M-char *if* this appears inside a string.
1037 ;; So, we convert them manually to (meta char).
1038 ((and (viper-characterp event
)
1039 (< ?\C-? event
) (<= event
255))
1041 event
(- event ?\C-?
1)))
1042 ((and (null mod
) (eq event
'return
))
1044 ((and (null mod
) (eq event
'space
))
1046 ((and (null mod
) (eq event
'delete
))
1048 ((and (null mod
) (eq event
'backspace
))
1050 (t (event-basic-type event
)))
1051 ) ; viper-cond-compile-for-xemacs-or-emacs
1053 (if (viper-characterp basis
)
1055 (if (viper= basis ?\C-?
)
1056 (list 'control
'\?) ; taking care of an emacs bug
1057 (intern (char-to-string basis
)))))
1059 (append mod
(list basis
))
1062 (defun viper-key-to-emacs-key (key)
1063 (let (key-name char-p modifiers mod-char-list base-key base-key-name
)
1064 (cond ((featurep 'xemacs
) key
)
1067 (setq key-name
(symbol-name key
))
1068 (cond ((= (length key-name
) 1) ; character event
1069 (string-to-char key-name
))
1070 ;; Emacs doesn't recognize `return' and `escape' as events on
1071 ;; dumb terminals, so we translate them into characters
1072 ((and (featurep 'emacs
) (not (viper-window-display-p))
1073 (string= key-name
"return"))
1075 ((and (featurep 'emacs
) (not (viper-window-display-p))
1076 (string= key-name
"escape"))
1078 ;; pass symbol-event as is
1082 (setq modifiers
(viper-subseq key
0 (1- (length key
)))
1083 base-key
(viper-seq-last-elt key
)
1084 base-key-name
(symbol-name base-key
)
1085 char-p
(= (length base-key-name
) 1))
1088 '(lambda (elt) (upcase (substring (symbol-name elt
) 0 1)))
1092 (car (read-from-string
1095 (mapconcat 'identity mod-char-list
"-\\")
1101 (mapconcat 'identity mod-char-list
"-")
1107 ;; LIS is assumed to be a list of events of characters
1108 (defun viper-eventify-list-xemacs (lis)
1109 (if (featurep 'xemacs
)
1112 (cond ((viper-characterp elt
) (character-to-event elt
))
1115 "viper-eventify-list-xemacs: can't convert to event, %S"
1120 ;; Smoothes out the difference between Emacs' unread-command-events
1121 ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
1122 ;; events or a sequence of keys.
1124 ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
1125 ;; symbol in unread-command-events list may cause Emacs to turn this symbol
1126 ;; into an event. Below, we delete nil from event lists, since nil is the most
1127 ;; common symbol that might appear in this wrong context.
1128 (defun viper-set-unread-command-events (arg)
1129 (if (featurep 'emacs
)
1131 unread-command-events
1133 (cond ((eventp arg
) (list arg
))
1136 (listify-key-sequence arg
))
1138 "viper-set-unread-command-events: Invalid argument, %S"
1140 (if (not (eventp nil
))
1141 (setq new-events
(delq nil new-events
)))
1142 (append new-events unread-command-events
)))
1145 unread-command-events
1147 (cond ((viper-characterp arg
) (list (character-to-event arg
)))
1148 ((eventp arg
) (list arg
))
1149 ((stringp arg
) (mapcar 'character-to-event arg
))
1150 ((vectorp arg
) (append arg nil
)) ; turn into list
1151 ((listp arg
) (viper-eventify-list-xemacs arg
))
1153 "viper-set-unread-command-events: Invalid argument, %S" arg
)))
1154 unread-command-events
))))
1157 ;; Check if vec is a vector of key-press events representing characters
1159 (defun viper-event-vector-p (vec)
1161 (eval (cons 'and
(mapcar '(lambda (elt) (if (eventp elt
) t
)) vec
)))))
1164 ;; check if vec is a vector of character symbols
1165 (defun viper-char-symbol-sequence-p (vec)
1170 (mapcar (lambda (elt)
1171 (and (symbolp elt
) (= (length (symbol-name elt
)) 1)))
1175 (defun viper-char-array-p (array)
1176 (eval (cons 'and
(mapcar 'viper-characterp array
))))
1179 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1180 ;; convert events to keys and, if all keys are regular printable
1181 ;; characters, will return a string. Otherwise, will return a string
1182 ;; representing a vector of converted events. If the input was a Viper macro,
1183 ;; will return a string that represents this macro as a vector.
1184 (defun viper-array-to-string (event-seq)
1186 (cond ((stringp event-seq
) event-seq
)
1187 ((viper-event-vector-p event-seq
)
1188 (setq temp
(mapcar 'viper-event-key event-seq
))
1189 (cond ((viper-char-symbol-sequence-p temp
)
1190 (mapconcat 'symbol-name temp
""))
1191 ((and (viper-char-array-p
1192 (setq temp2
(mapcar 'viper-key-to-character temp
))))
1193 (mapconcat 'char-to-string temp2
""))
1194 (t (prin1-to-string (vconcat temp
)))))
1195 ((viper-char-symbol-sequence-p event-seq
)
1196 (mapconcat 'symbol-name event-seq
""))
1197 ((and (vectorp event-seq
)
1199 (setq temp
(mapcar 'viper-key-to-character event-seq
))))
1200 (mapconcat 'char-to-string temp
""))
1201 (t (prin1-to-string event-seq
)))))
1203 (defun viper-key-press-events-to-chars (events)
1204 (mapconcat (viper-cond-compile-for-xemacs-or-emacs
1205 (lambda (elt) (char-to-string (event-to-character elt
))) ; xemacs
1206 'char-to-string
; emacs
1212 (defun viper-read-char-exclusive ()
1214 (echo-keystrokes 1))
1217 (setq char
(read-char))
1219 ;; skip event if not char
1220 (viper-read-event))))
1223 ;; key is supposed to be in viper's representation, e.g., (control l), a
1225 (defun viper-key-to-character (key)
1226 (cond ((eq key
'space
) ?\
)
1227 ((eq key
'delete
) ?\C-?
)
1228 ((eq key
'return
) ?\C-m
)
1229 ((eq key
'backspace
) ?\C-h
)
1231 (= 1 (length (symbol-name key
))))
1232 (string-to-char (symbol-name key
)))
1234 (eq (car key
) 'control
)
1235 (symbol-name (nth 1 key
))
1236 (= 1 (length (symbol-name (nth 1 key
)))))
1237 (read (format "?\\C-%s" (symbol-name (nth 1 key
)))))
1241 (defun viper-setup-master-buffer (&rest other-files-or-buffers
)
1242 "Set up the current buffer as a master buffer.
1243 Arguments become related buffers. This function should normally be used in
1244 the `Local variables' section of a file."
1245 (setq viper-related-files-and-buffers-ring
1246 (make-ring (1+ (length other-files-or-buffers
))))
1247 (mapc '(lambda (elt)
1248 (viper-ring-insert viper-related-files-and-buffers-ring elt
))
1249 other-files-or-buffers
)
1250 (viper-ring-insert viper-related-files-and-buffers-ring
(buffer-name))
1253 ;;; Movement utilities
1255 ;; Characters that should not be considered as part of the word, in reformed-vi
1257 ;; Note: \\ (quoted \) must appear before `-' because this string is listified
1258 ;; into characters at some point and then put back to string. The result is
1259 ;; used in skip-chars-forward, which treats - specially. Here we achieve the
1260 ;; effect of quoting - and preventing it from being special.
1261 (defconst viper-non-word-characters-reformed-vi
1262 "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
1263 ;; These are characters that are not to be considered as parts of a word in
1265 ;; Set each time state changes and at loading time
1266 (viper-deflocalvar viper-non-word-characters nil
)
1268 ;; must be buffer-local
1269 (viper-deflocalvar viper-ALPHA-char-class
"w"
1270 "String of syntax classes characterizing Viper's alphanumeric symbols.
1271 In addition, the symbol `_' may be considered alphanumeric if
1272 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1274 (defconst viper-strict-ALPHA-chars
"a-zA-Z0-9_"
1275 "Regexp matching the set of alphanumeric characters acceptable to strict
1277 (defconst viper-strict-SEP-chars
" \t\n"
1278 "Regexp matching the set of alphanumeric characters acceptable to strict
1280 (defconst viper-strict-SEP-chars-sans-newline
" \t"
1281 "Regexp matching the set of alphanumeric characters acceptable to strict
1284 (defconst viper-SEP-char-class
" -"
1285 "String of syntax classes for Vi separators.
1286 Usually contains ` ', linefeed, TAB or formfeed.")
1289 ;; Set Viper syntax classes and related variables according to
1290 ;; `viper-syntax-preference'.
1291 (defun viper-update-syntax-classes (&optional set-default
)
1292 (let ((preference (cond ((eq viper-syntax-preference
'emacs
)
1293 "w") ; Viper words have only Emacs word chars
1294 ((eq viper-syntax-preference
'extended
)
1295 "w_") ; Viper words have Emacs word & symbol chars
1296 (t "w"))) ; Viper words are Emacs words plus `_'
1297 (non-word-chars (cond ((eq viper-syntax-preference
'reformed-vi
)
1298 (viper-string-to-list
1299 viper-non-word-characters-reformed-vi
))
1302 (setq-default viper-ALPHA-char-class preference
1303 viper-non-word-characters non-word-chars
)
1304 (setq viper-ALPHA-char-class preference
1305 viper-non-word-characters non-word-chars
))
1308 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1309 ;; is `nil'. If nil, use setq.
1310 (defun viper-set-syntax-preference (&optional symbol value
)
1311 "Set Viper syntax preference.
1312 If called interactively or if SYMBOL is nil, sets syntax preference in current
1313 buffer. If called non-interactively, preferably via the customization widget,
1314 sets the default value."
1319 "Viper syntax preference: "
1320 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1321 nil
'require-match
)))
1322 (if (stringp value
) (setq value
(intern value
)))
1323 (or (memq value
'(strict-vi reformed-vi extended emacs
))
1324 (error "Invalid Viper syntax preference, %S" value
))
1326 (setq-default viper-syntax-preference value
)
1327 (setq viper-syntax-preference value
))
1328 (viper-update-syntax-classes))
1330 (defcustom viper-syntax-preference
'reformed-vi
1331 "*Syntax type characterizing Viper's alphanumeric symbols.
1332 Affects movement and change commands that deal with Vi-style words.
1333 Works best when set in the hooks to various major modes.
1335 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1337 `reformed-vi' means Viper words are like Emacs words \(as determined using
1338 Emacs syntax tables, which are different for different major modes\) with two
1339 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1340 symbols, such as `,',:,\",),{, etc., are excluded.
1341 This behaves very close to `strict-vi', but also works well with non-ASCII
1342 characters from various alphabets.
1344 `extended' means Viper word constituents are symbols that are marked as being
1345 parts of words OR symbols in Emacs syntax tables.
1346 This is most appropriate for major modes intended for editing programs.
1348 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1350 This option is appropriate if you like Emacs-style words."
1351 :type
'(radio (const strict-vi
) (const reformed-vi
)
1352 (const extended
) (const emacs
))
1353 :set
'viper-set-syntax-preference
1355 (make-variable-buffer-local 'viper-syntax-preference
)
1358 ;; addl-chars are characters to be temporarily considered as alphanumerical
1359 (defun viper-looking-at-alpha (&optional addl-chars
)
1360 (or (stringp addl-chars
) (setq addl-chars
""))
1361 (if (eq viper-syntax-preference
'reformed-vi
)
1362 (setq addl-chars
(concat addl-chars
"_")))
1363 (let ((char (char-after (point))))
1365 (if (eq viper-syntax-preference
'strict-vi
)
1366 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars
"]"))
1368 ;; or one of the additional chars being asked to include
1369 (viper-memq-char char
(viper-string-to-list addl-chars
))
1371 ;; not one of the excluded word chars (note:
1372 ;; viper-non-word-characters is a list)
1373 (not (viper-memq-char char viper-non-word-characters
))
1374 ;; char of the Viper-word syntax class
1375 (viper-memq-char (char-syntax char
)
1376 (viper-string-to-list viper-ALPHA-char-class
))))))
1379 (defun viper-looking-at-separator ()
1380 (let ((char (char-after (point))))
1382 (if (eq viper-syntax-preference
'strict-vi
)
1383 (viper-memq-char char
(viper-string-to-list viper-strict-SEP-chars
))
1384 (or (eq char ?
\n) ; RET is always a separator in Vi
1385 (viper-memq-char (char-syntax char
)
1386 (viper-string-to-list viper-SEP-char-class
)))))
1389 (defsubst viper-looking-at-alphasep
(&optional addl-chars
)
1390 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars
)))
1392 (defun viper-skip-alpha-forward (&optional addl-chars
)
1393 (or (stringp addl-chars
) (setq addl-chars
""))
1396 (cond ((eq viper-syntax-preference
'strict-vi
)
1398 (t viper-ALPHA-char-class
))
1399 (cond ((eq viper-syntax-preference
'strict-vi
)
1400 (concat viper-strict-ALPHA-chars addl-chars
))
1403 (defun viper-skip-alpha-backward (&optional addl-chars
)
1404 (or (stringp addl-chars
) (setq addl-chars
""))
1407 (cond ((eq viper-syntax-preference
'strict-vi
)
1409 (t viper-ALPHA-char-class
))
1410 (cond ((eq viper-syntax-preference
'strict-vi
)
1411 (concat viper-strict-ALPHA-chars addl-chars
))
1414 ;; weird syntax tables may confuse strict-vi style
1415 (defsubst viper-skip-all-separators-forward
(&optional within-line
)
1416 (if (eq viper-syntax-preference
'strict-vi
)
1418 (skip-chars-forward viper-strict-SEP-chars-sans-newline
)
1419 (skip-chars-forward viper-strict-SEP-chars
))
1420 (viper-skip-syntax 'forward
1421 viper-SEP-char-class
1422 (or within-line
"\n")
1423 (if within-line
(viper-line-pos 'end
)))))
1425 (defsubst viper-skip-all-separators-backward
(&optional within-line
)
1426 (if (eq viper-syntax-preference
'strict-vi
)
1428 (skip-chars-backward viper-strict-SEP-chars-sans-newline
)
1429 (skip-chars-backward viper-strict-SEP-chars
))
1430 (viper-skip-syntax 'backward
1431 viper-SEP-char-class
1432 (or within-line
"\n")
1433 (if within-line
(viper-line-pos 'start
)))))
1434 (defun viper-skip-nonseparators (direction)
1437 (concat "^" viper-SEP-char-class
)
1439 (viper-line-pos (if (eq direction
'forward
) 'end
'start
))))
1442 ;; skip over non-word constituents and non-separators
1443 (defun viper-skip-nonalphasep-forward ()
1444 (if (eq viper-syntax-preference
'strict-vi
)
1446 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1449 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1450 ;; Emacs may consider some of these as words, but we don't want them
1451 viper-non-word-characters
1452 (viper-line-pos 'end
))))
1454 (defun viper-skip-nonalphasep-backward ()
1455 (if (eq viper-syntax-preference
'strict-vi
)
1456 (skip-chars-backward
1457 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars
))
1460 (concat "^" viper-ALPHA-char-class viper-SEP-char-class
)
1461 ;; Emacs may consider some of these as words, but we don't want them
1462 viper-non-word-characters
1463 (viper-line-pos 'start
))))
1465 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1466 ;; Return the number of chars traveled.
1467 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1468 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1469 ;; words, even if Emacs syntax table says they are.
1470 (defun viper-skip-syntax (direction syntax addl-chars
&optional limit
)
1474 (if (eq direction
'forward
)
1475 'skip-chars-forward
'skip-chars-backward
))
1477 (if (eq direction
'forward
)
1478 'viper-forward-char-carefully
'viper-backward-char-carefully
))
1479 char-looked-at syntax-of-char-looked-at negated-syntax
)
1481 (cond ((listp addl-chars
) (viper-charlist-to-string addl-chars
))
1482 ((stringp addl-chars
) addl-chars
)
1485 (cond ((listp syntax
) syntax
)
1486 ((stringp syntax
) (viper-string-to-list syntax
))
1488 (if (memq ?^ syntax
) (setq negated-syntax t
))
1490 (while (and (not (= local
0))
1491 (cond ((eq direction
'forward
)
1494 (setq char-looked-at
(viper-char-at-pos direction
)
1495 ;; if outside the range, set to nil
1496 syntax-of-char-looked-at
(if char-looked-at
1497 (char-syntax char-looked-at
)))
1500 (cond ((and limit
(eq direction
'forward
))
1502 (limit ; backward & limit
1505 ;; char under/before cursor has appropriate syntax
1507 (not (memq syntax-of-char-looked-at syntax
))
1508 (memq syntax-of-char-looked-at syntax
))
1509 ;; if char-syntax class is "word", make sure it is not one
1510 ;; of the excluded characters
1511 (if (and (eq syntax-of-char-looked-at ?w
)
1512 (not negated-syntax
))
1513 (not (viper-memq-char
1514 char-looked-at viper-non-word-characters
))
1516 (funcall skip-syntax-func
1)
1518 (funcall skip-chars-func addl-chars limit
)))
1519 (setq total
(+ total local
)))
1523 ;; tells when point is at the beginning of field
1524 (defun viper-beginning-of-field ()
1526 (not (eq (get-char-property (point) 'field
)
1527 (get-char-property (1- (point)) 'field
)))))
1530 ;; this is copied from cl-extra.el
1531 ;; Return the subsequence of SEQ from START to END.
1532 ;; If END is omitted, it defaults to the length of the sequence.
1533 ;; If START or END is negative, it counts from the end.
1534 (defun viper-subseq (seq start
&optional end
)
1535 (if (stringp seq
) (substring seq start end
)
1537 (and end
(< end
0) (setq end
(+ end
(setq len
(length seq
)))))
1538 (if (< start
0) (setq start
(+ start
(or len
(setq len
(length seq
))))))
1540 (if (> start
0) (setq seq
(nthcdr start seq
)))
1543 (while (>= (setq end
(1- end
)) start
)
1544 (push (pop seq
) res
))
1546 (copy-sequence seq
)))
1548 (or end
(setq end
(or len
(length seq
))))
1549 (let ((res (make-vector (max (- end start
) 0) nil
))
1551 (while (< start end
)
1552 (aset res i
(aref seq start
))
1553 (setq i
(1+ i
) start
(1+ start
)))
1558 (provide 'viper-util
)
1561 ;;; Local Variables:
1562 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1565 ;;; arch-tag: 7f023fd5-dd9e-4378-a397-9c179553b0e3
1566 ;;; viper-util.el ends here