2000-10-29 Michael Kifer <kifer@cs.sunysb.edu>
[emacs.git] / lisp / emulation / viper-util.el
blob393dc4d527650e3abac9ab879a595ac40347e217
1 ;;; viper-util.el --- Utilities used by viper.el
3 ;; Copyright (C) 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
23 ;; Code
25 ;; Compiler pacifier
26 (defvar viper-overriding-map)
27 (defvar pm-color-alist)
28 (defvar zmacs-region-stays)
29 (defvar viper-minibuffer-current-face)
30 (defvar viper-minibuffer-insert-face)
31 (defvar viper-minibuffer-vi-face)
32 (defvar viper-minibuffer-emacs-face)
33 (defvar viper-replace-overlay-face)
34 (defvar viper-fast-keyseq-timeout)
35 (defvar ex-unix-type-shell)
36 (defvar ex-unix-type-shell-options)
37 (defvar viper-ex-tmp-buf-name)
38 (defvar viper-syntax-preference)
40 (require 'cl)
41 (require 'ring)
43 (if noninteractive
44 (eval-when-compile
45 (let ((load-path (cons (expand-file-name ".") load-path)))
46 (or (featurep 'viper-init)
47 (load "viper-init.el" nil nil 'nosuffix))
48 )))
49 ;; end pacifier
51 (require 'viper-init)
54 ;; A fix for NeXT Step
55 ;; Should go away, when NS people fix the design flaw, which leaves the
56 ;; two x-* functions undefined.
57 (if (and (not (fboundp 'x-display-color-p)) (fboundp 'ns-display-color-p))
58 (fset 'x-display-color-p (symbol-function 'ns-display-color-p)))
59 (if (and (not (fboundp 'x-color-defined-p)) (fboundp 'ns-color-defined-p))
60 (fset 'x-color-defined-p (symbol-function 'ns-color-defined-p)))
63 ;;; XEmacs support
66 (if viper-xemacs-p
67 (progn
68 (fset 'viper-read-event (symbol-function 'next-command-event))
69 (fset 'viper-make-overlay (symbol-function 'make-extent))
70 (fset 'viper-overlay-start (symbol-function 'extent-start-position))
71 (fset 'viper-overlay-end (symbol-function 'extent-end-position))
72 (fset 'viper-overlay-put (symbol-function 'set-extent-property))
73 (fset 'viper-overlay-p (symbol-function 'extentp))
74 (fset 'viper-overlay-get (symbol-function 'extent-property))
75 (fset 'viper-move-overlay (symbol-function 'set-extent-endpoints))
76 (fset 'viper-overlay-live-p (symbol-function 'extent-live-p))
77 (if (viper-window-display-p)
78 (fset 'viper-iconify (symbol-function 'iconify-frame)))
79 (cond ((viper-has-face-support-p)
80 (fset 'viper-get-face (symbol-function 'get-face))
81 (fset 'viper-color-defined-p
82 (symbol-function 'valid-color-name-p))
83 )))
84 (fset 'viper-read-event (symbol-function 'read-event))
85 (fset 'viper-make-overlay (symbol-function 'make-overlay))
86 (fset 'viper-overlay-start (symbol-function 'overlay-start))
87 (fset 'viper-overlay-end (symbol-function 'overlay-end))
88 (fset 'viper-overlay-put (symbol-function 'overlay-put))
89 (fset 'viper-overlay-p (symbol-function 'overlayp))
90 (fset 'viper-overlay-get (symbol-function 'overlay-get))
91 (fset 'viper-move-overlay (symbol-function 'move-overlay))
92 (fset 'viper-overlay-live-p (symbol-function 'overlayp))
93 (if (viper-window-display-p)
94 (fset 'viper-iconify (symbol-function 'iconify-or-deiconify-frame)))
95 (cond ((viper-has-face-support-p)
96 (fset 'viper-get-face (symbol-function 'internal-get-face))
97 (fset 'viper-color-defined-p (symbol-function 'x-color-defined-p))
98 )))
101 (fset 'viper-characterp
102 (symbol-function
103 (if viper-xemacs-p 'characterp 'integerp)))
105 ; CHAR is supposed to be a char or an integer; LIST is a list of chars, nil,
106 ; and negative numbers
107 ; Check if CHAR is a member by trying to convert into integers, if necessary.
108 ; Introduced for compatibility with XEmacs, where integers are not the same as
109 ; chars.
110 (defun viper-memq-char (char list)
111 (cond (viper-emacs-p (memq char list))
112 ((null char) (memq char list))
113 ((characterp char) (memq char list))
114 ((integerp char) (memq (int-to-char char) list))
115 (t nil)))
117 ;; Like =, but accommodates null and also is t for eq-objects
118 (defun viper= (char char1)
119 (cond ((eq char char1) t)
120 ((and (viper-characterp char) (viper-characterp char1))
121 (= char char1))
122 (t nil)))
124 (defsubst viper-color-display-p ()
125 (if viper-emacs-p
126 (x-display-color-p)
127 (eq (device-class (selected-device)) 'color)))
129 (defsubst viper-get-cursor-color ()
130 (if viper-emacs-p
131 (cdr (assoc 'cursor-color (frame-parameters)))
132 (color-instance-name (frame-property (selected-frame) 'cursor-color))))
135 ;; OS/2
136 (cond ((eq (viper-device-type) 'pm)
137 (fset 'viper-color-defined-p
138 (lambda (color) (assoc color pm-color-alist)))))
141 ;; cursor colors
142 (defun viper-change-cursor-color (new-color)
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 viper-emacs-p
147 (modify-frame-parameters
148 (selected-frame) (list (cons 'cursor-color new-color)))
149 (set-frame-property
150 (selected-frame) 'cursor-color (make-color-instance new-color)))
153 ;; By default, saves current frame cursor color in the
154 ;; viper-saved-cursor-color-in-replace-mode property of viper-replace-overlay
155 (defun viper-save-cursor-color (before-which-mode)
156 (if (and (viper-window-display-p) (viper-color-display-p))
157 (let ((color (viper-get-cursor-color)))
158 (if (and (stringp color) (viper-color-defined-p color)
159 (not (string= color viper-replace-overlay-cursor-color)))
160 (modify-frame-parameters
161 (selected-frame)
162 (list
163 (cons
164 (if (eq before-which-mode 'before-replace-mode)
165 'viper-saved-cursor-color-in-replace-mode
166 'viper-saved-cursor-color-in-insert-mode)
167 color)))
168 ))))
171 (defsubst viper-get-saved-cursor-color-in-replace-mode ()
173 (funcall
174 (if viper-emacs-p 'frame-parameter 'frame-property)
175 (selected-frame)
176 'viper-saved-cursor-color-in-replace-mode)
177 viper-vi-state-cursor-color))
179 (defsubst viper-get-saved-cursor-color-in-insert-mode ()
181 (funcall
182 (if viper-emacs-p 'frame-parameter 'frame-property)
183 (selected-frame)
184 'viper-saved-cursor-color-in-insert-mode)
185 viper-vi-state-cursor-color))
187 ;; restore cursor color from replace overlay
188 (defun viper-restore-cursor-color(after-which-mode)
189 (if (viper-overlay-p viper-replace-overlay)
190 (viper-change-cursor-color
191 (if (eq after-which-mode 'after-replace-mode)
192 (viper-get-saved-cursor-color-in-replace-mode)
193 (viper-get-saved-cursor-color-in-insert-mode))
197 ;; Check the current version against the major and minor version numbers
198 ;; using op: cur-vers op major.minor If emacs-major-version or
199 ;; emacs-minor-version are not defined, we assume that the current version
200 ;; is hopelessly outdated. We assume that emacs-major-version and
201 ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
202 ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
203 ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
204 ;; incorrect. However, this gives correct result in our cases, since we are
205 ;; testing for sufficiently high Emacs versions.
206 (defun viper-check-version (op major minor &optional type-of-emacs)
207 (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
208 (and (cond ((eq type-of-emacs 'xemacs) viper-xemacs-p)
209 ((eq type-of-emacs 'emacs) viper-emacs-p)
210 (t t))
211 (cond ((eq op '=) (and (= emacs-minor-version minor)
212 (= emacs-major-version major)))
213 ((memq op '(> >= < <=))
214 (and (or (funcall op emacs-major-version major)
215 (= emacs-major-version major))
216 (if (= emacs-major-version major)
217 (funcall op emacs-minor-version minor)
218 t)))
220 (error "%S: Invalid op in viper-check-version" op))))
221 (cond ((memq op '(= > >=)) nil)
222 ((memq op '(< <=)) t))))
225 (defun viper-get-visible-buffer-window (wind)
226 (if viper-xemacs-p
227 (get-buffer-window wind t)
228 (get-buffer-window wind 'visible)))
231 ;; Return line position.
232 ;; If pos is 'start then returns position of line start.
233 ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
234 ;; Pos = 'indent returns beginning of indentation.
235 ;; Otherwise, returns point. Current point is not moved in any case."
236 (defun viper-line-pos (pos)
237 (let ((cur-pos (point))
238 (result))
239 (cond
240 ((equal pos 'start)
241 (beginning-of-line))
242 ((equal pos 'end)
243 (end-of-line))
244 ((equal pos 'mid)
245 (goto-char (+ (viper-line-pos 'start) (viper-line-pos 'end) 2)))
246 ((equal pos 'indent)
247 (back-to-indentation))
248 (t nil))
249 (setq result (point))
250 (goto-char cur-pos)
251 result))
253 ;; Emacs counts each multibyte character as several positions in the buffer, so
254 ;; we use Emacs' chars-in-region. XEmacs is counting each char as just one pos,
255 ;; so we can simply subtract.
256 (defun viper-chars-in-region (beg end &optional preserve-sign)
257 (let ((count (abs (if (fboundp 'chars-in-region)
258 (chars-in-region beg end)
259 (- end beg)))))
260 (if (and (< end beg) preserve-sign)
261 (- count)
262 count)))
264 ;; Test if POS is between BEG and END
265 (defsubst viper-pos-within-region (pos beg end)
266 (and (>= pos (min beg end)) (>= (max beg end) pos)))
269 ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
270 ;; The first argument must eval to a variable name.
271 ;; Arguments: (var-name position &optional buffer).
273 ;; This is useful for moving markers that are supposed to be local.
274 ;; For this, VAR-NAME should be made buffer-local with nil as a default.
275 ;; Then, each time this var is used in `viper-move-marker-locally' in a new
276 ;; buffer, a new marker will be created.
277 (defun viper-move-marker-locally (var pos &optional buffer)
278 (if (markerp (eval var))
280 (set var (make-marker)))
281 (move-marker (eval var) pos buffer))
284 ;; Print CONDITIONS as a message.
285 (defun viper-message-conditions (conditions)
286 (let ((case (car conditions)) (msg (cdr conditions)))
287 (if (null msg)
288 (message "%s" case)
289 (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
290 (beep 1)))
294 ;;; List/alist utilities
296 ;; Convert LIST to an alist
297 (defun viper-list-to-alist (lst)
298 (let ((alist))
299 (while lst
300 (setq alist (cons (list (car lst)) alist))
301 (setq lst (cdr lst)))
302 alist))
304 ;; Convert ALIST to a list.
305 (defun viper-alist-to-list (alst)
306 (let ((lst))
307 (while alst
308 (setq lst (cons (car (car alst)) lst))
309 (setq alst (cdr alst)))
310 lst))
312 ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
313 (defun viper-filter-alist (regexp alst)
314 (interactive "s x")
315 (let ((outalst) (inalst alst))
316 (while (car inalst)
317 (if (string-match regexp (car (car inalst)))
318 (setq outalst (cons (car inalst) outalst)))
319 (setq inalst (cdr inalst)))
320 outalst))
322 ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
323 (defun viper-filter-list (regexp lst)
324 (interactive "s x")
325 (let ((outlst) (inlst lst))
326 (while (car inlst)
327 (if (string-match regexp (car inlst))
328 (setq outlst (cons (car inlst) outlst)))
329 (setq inlst (cdr inlst)))
330 outlst))
333 ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
334 ;; LIS2 is modified by filtering it: deleting its members of the form
335 ;; \(car elt\) such that (car elt') is in LIS1.
336 (defun viper-append-filter-alist (lis1 lis2)
337 (let ((temp lis1)
338 elt)
340 ;;filter-append the second list
341 (while temp
342 ;; delete all occurrences
343 (while (setq elt (assoc (car (car temp)) lis2))
344 (setq lis2 (delq elt lis2)))
345 (setq temp (cdr temp)))
347 (nconc lis1 lis2)))
350 ;;; Support for :e, :r, :w file globbing
352 ;; Glob the file spec.
353 ;; This function is designed to work under Unix. It might also work under VMS.
354 (defun viper-glob-unix-files (filespec)
355 (let ((gshell
356 (cond (ex-unix-type-shell shell-file-name)
357 ((memq system-type '(vax-vms axp-vms)) "*dcl*") ; VAX VMS
358 (t "sh"))) ; probably Unix anyway
359 (gshell-options
360 ;; using cond in anticipation of further additions
361 (cond (ex-unix-type-shell-options)
363 (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec))
364 (t (format "ls -1 -d %s" filespec))))
365 status)
366 (save-excursion
367 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
368 (erase-buffer)
369 (setq status
370 (if gshell-options
371 (call-process gshell nil t nil
372 gshell-options
373 "-c"
374 command)
375 (call-process gshell nil t nil
376 "-c"
377 command)))
378 (goto-char (point-min))
379 ;; Issue an error, if no match.
380 (if (> status 0)
381 (save-excursion
382 (skip-chars-forward " \t\n\j")
383 (if (looking-at "ls:")
384 (viper-forward-Word 1))
385 (error "%s: %s"
386 (if (stringp gshell)
387 gshell
388 "shell")
389 (buffer-substring (point) (viper-line-pos 'end)))
391 (goto-char (point-min))
392 (viper-get-filenames-from-buffer 'one-per-line))
396 ;; Interpret the stuff in the buffer as a list of file names
397 ;; return a list of file names listed in the buffer beginning at point
398 ;; If optional arg is supplied, assume each filename is listed on a separate
399 ;; line
400 (defun viper-get-filenames-from-buffer (&optional one-per-line)
401 (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
402 result fname delim)
403 (skip-chars-forward skip-chars)
404 (while (not (eobp))
405 (if (cond ((looking-at "\"")
406 (setq delim ?\")
407 (re-search-forward "[^\"]+" nil t)) ; noerror
408 ((looking-at "'")
409 (setq delim ?')
410 (re-search-forward "[^']+" nil t)) ; noerror
412 (re-search-forward
413 (concat "[^" skip-chars "]+") nil t))) ;noerror
414 (setq fname
415 (buffer-substring (match-beginning 0) (match-end 0))))
416 (if delim
417 (forward-char 1))
418 (skip-chars-forward " \t\n")
419 (setq result (cons fname result)))
420 result))
422 ;; convert MS-DOS wildcards to regexp
423 (defun viper-wildcard-to-regexp (wcard)
424 (save-excursion
425 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
426 (erase-buffer)
427 (insert wcard)
428 (goto-char (point-min))
429 (while (not (eobp))
430 (skip-chars-forward "^*?.\\\\")
431 (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
432 ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
433 ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
434 ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
436 (buffer-string)
440 ;; glob windows files
441 ;; LIST is expected to be in reverse order
442 (defun viper-glob-mswindows-files (filespec)
443 (let ((case-fold-search t)
444 tmp tmp2)
445 (save-excursion
446 (set-buffer (get-buffer-create viper-ex-tmp-buf-name))
447 (erase-buffer)
448 (insert filespec)
449 (goto-char (point-min))
450 (setq tmp (viper-get-filenames-from-buffer))
451 (while tmp
452 (setq tmp2 (cons (directory-files
453 ;; the directory part
454 (or (file-name-directory (car tmp))
456 t ; return full names
457 ;; the regexp part: globs the file names
458 (concat "^"
459 (viper-wildcard-to-regexp
460 (file-name-nondirectory (car tmp)))
461 "$"))
462 tmp2))
463 (setq tmp (cdr tmp)))
464 (reverse (apply 'append tmp2)))))
467 ;;; Insertion ring
469 ;; Rotate RING's index. DIRection can be positive or negative.
470 (defun viper-ring-rotate1 (ring dir)
471 (if (and (ring-p ring) (> (ring-length ring) 0))
472 (progn
473 (setcar ring (cond ((> dir 0)
474 (ring-plus1 (car ring) (ring-length ring)))
475 ((< dir 0)
476 (ring-minus1 (car ring) (ring-length ring)))
477 ;; don't rotate if dir = 0
478 (t (car ring))))
479 (viper-current-ring-item ring)
482 (defun viper-special-ring-rotate1 (ring dir)
483 (if (memq viper-intermediate-command
484 '(repeating-display-destructive-command
485 repeating-insertion-from-ring))
486 (viper-ring-rotate1 ring dir)
487 ;; don't rotate otherwise
488 (viper-ring-rotate1 ring 0)))
490 ;; current ring item; if N is given, then so many items back from the
491 ;; current
492 (defun viper-current-ring-item (ring &optional n)
493 (setq n (or n 0))
494 (if (and (ring-p ring) (> (ring-length ring) 0))
495 (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
497 ;; Push item onto ring. The second argument is a ring-variable, not value.
498 (defun viper-push-onto-ring (item ring-var)
499 (or (ring-p (eval ring-var))
500 (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
501 (or (null item) ; don't push nil
502 (and (stringp item) (string= item "")) ; or empty strings
503 (equal item (viper-current-ring-item (eval ring-var))) ; or old stuff
504 ;; Since viper-set-destructive-command checks if we are inside
505 ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
506 ;; cmd viper-repeat makes a call to the current function only if `.' is
507 ;; executing a command from the command history. It doesn't call the
508 ;; push-onto-ring function if `.' is simply repeating the last
509 ;; destructive command. We only check for ESC (which happens when we do
510 ;; insert with a prefix argument, or if this-command-keys doesn't give
511 ;; anything meaningful (in that case we don't know what to show to the
512 ;; user).
513 (and (eq ring-var 'viper-command-ring)
514 (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
515 (viper-array-to-string (this-command-keys))))
516 (viper-ring-insert (eval ring-var) item))
520 ;; removing elts from ring seems to break it
521 (defun viper-cleanup-ring (ring)
522 (or (< (ring-length ring) 2)
523 (null (viper-current-ring-item ring))
524 ;; last and previous equal
525 (if (equal (viper-current-ring-item ring)
526 (viper-current-ring-item ring 1))
527 (viper-ring-pop ring))))
529 ;; ring-remove seems to be buggy, so we concocted this for our purposes.
530 (defun viper-ring-pop (ring)
531 (let* ((ln (ring-length ring))
532 (vec (cdr (cdr ring)))
533 (veclen (length vec))
534 (hd (car ring))
535 (idx (max 0 (ring-minus1 hd ln)))
536 (top-elt (aref vec idx)))
538 ;; shift elements
539 (while (< (1+ idx) veclen)
540 (aset vec idx (aref vec (1+ idx)))
541 (setq idx (1+ idx)))
542 (aset vec idx nil)
544 (setq hd (max 0 (ring-minus1 hd ln)))
545 (if (= hd (1- ln)) (setq hd 0))
546 (setcar ring hd) ; move head
547 (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
548 top-elt
551 (defun viper-ring-insert (ring item)
552 (let* ((ln (ring-length ring))
553 (vec (cdr (cdr ring)))
554 (veclen (length vec))
555 (hd (car ring))
556 (vecpos-after-hd (if (= hd 0) ln hd))
557 (idx ln))
559 (if (= ln veclen)
560 (progn
561 (aset vec hd item) ; hd is always 1+ the actual head index in vec
562 (setcar ring (ring-plus1 hd ln)))
563 (setcar (cdr ring) (1+ ln))
564 (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
565 (while (and (>= idx vecpos-after-hd) (> ln 0))
566 (aset vec idx (aref vec (1- idx)))
567 (setq idx (1- idx)))
568 (aset vec vecpos-after-hd item))
569 item))
572 ;;; String utilities
574 ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
575 ;; PRE-STRING is a string to prepend to the abbrev string.
576 ;; POST-STRING is a string to append to the abbrev string.
577 ;; ABBREV_SIGN is a string to be inserted before POST-STRING
578 ;; if the orig string was truncated.
579 (defun viper-abbreviate-string (string max-len
580 pre-string post-string abbrev-sign)
581 (let (truncated-str)
582 (setq truncated-str
583 (if (stringp string)
584 (substring string 0 (min max-len (length string)))))
585 (cond ((null truncated-str) "")
586 ((> (length string) max-len)
587 (format "%s%s%s%s"
588 pre-string truncated-str abbrev-sign post-string))
589 (t (format "%s%s%s" pre-string truncated-str post-string)))))
591 ;; tells if we are over a whitespace-only line
592 (defsubst viper-over-whitespace-line ()
593 (save-excursion
594 (beginning-of-line)
595 (looking-at "^[ \t]*$")))
598 ;;; Saving settings in custom file
600 ;; Save the current setting of VAR in CUSTOM-FILE.
601 ;; If given, MESSAGE is a message to be displayed after that.
602 ;; This message is erased after 2 secs, if erase-msg is non-nil.
603 ;; Arguments: var message custom-file &optional erase-message
604 (defun viper-save-setting (var message custom-file &optional erase-msg)
605 (let* ((var-name (symbol-name var))
606 (var-val (if (boundp var) (eval var)))
607 (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
608 (buf (find-file-noselect (substitute-in-file-name custom-file)))
610 (message message)
611 (save-excursion
612 (set-buffer buf)
613 (goto-char (point-min))
614 (if (re-search-forward regexp nil t)
615 (let ((reg-end (1- (match-end 0))))
616 (search-backward var-name)
617 (delete-region (match-beginning 0) reg-end)
618 (goto-char (match-beginning 0))
619 (insert (format "%s '%S" var-name var-val)))
620 (goto-char (point-max))
621 (if (not (bolp)) (insert "\n"))
622 (insert (format "(setq %s '%S)\n" var-name var-val)))
623 (save-buffer))
624 (kill-buffer buf)
625 (if erase-msg
626 (progn
627 (sit-for 2)
628 (message "")))
631 ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
632 ;; match this pattern.
633 (defun viper-save-string-in-file (string custom-file &optional pattern)
634 (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
635 (save-excursion
636 (set-buffer buf)
637 (let (buffer-read-only)
638 (goto-char (point-min))
639 (if pattern (delete-matching-lines pattern))
640 (goto-char (point-max))
641 (if string (insert string))
642 (save-buffer)))
643 (kill-buffer buf)
647 ;; define remote file test
648 (or (fboundp 'viper-file-remote-p) ; user supplied his own function: use it
649 (defun viper-file-remote-p (file-name)
650 (car (cond ((featurep 'efs-auto) (efs-ftp-path file-name))
651 ((fboundp 'file-remote-p) (file-remote-p file-name))
652 (t (require 'ange-ftp)
653 ;; Can happen only in Emacs, since XEmacs has file-remote-p
654 (ange-ftp-ftp-name file-name))))))
658 ;; This is a simple-minded check for whether a file is under version control.
659 ;; If file,v exists but file doesn't, this file is considered to be not checked
660 ;; in and not checked out for the purpose of patching (since patch won't be
661 ;; able to read such a file anyway).
662 ;; FILE is a string representing file name
663 ;;(defun viper-file-under-version-control (file)
664 ;; (let* ((filedir (file-name-directory file))
665 ;; (file-nondir (file-name-nondirectory file))
666 ;; (trial (concat file-nondir ",v"))
667 ;; (full-trial (concat filedir trial))
668 ;; (full-rcs-trial (concat filedir "RCS/" trial)))
669 ;; (and (stringp file)
670 ;; (file-exists-p file)
671 ;; (or
672 ;; (and
673 ;; (file-exists-p full-trial)
674 ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
675 ;; ;; don't be fooled by this!
676 ;; (not (equal (file-attributes file)
677 ;; (file-attributes full-trial))))
678 ;; ;; check if a version is in RCS/ directory
679 ;; (file-exists-p full-rcs-trial)))
680 ;; ))
683 (defsubst viper-file-checked-in-p (file)
684 (and (featurep 'vc-hooks)
685 ;; CVS files are considered not checked in
686 (not (memq (vc-backend file) '(nil CVS)))
687 (not (vc-locking-user file))))
689 ;; checkout if visited file is checked in
690 (defun viper-maybe-checkout (buf)
691 (let ((file (expand-file-name (buffer-file-name buf)))
692 (checkout-function (key-binding "\C-x\C-q")))
693 (if (and (viper-file-checked-in-p file)
694 (or (beep 1) t)
695 (y-or-n-p
696 (format
697 "File %s is checked in. Check it out? "
698 (viper-abbreviate-file-name file))))
699 (with-current-buffer buf
700 (command-execute checkout-function)))))
705 ;;; Overlays
706 (defun viper-put-on-search-overlay (beg end)
707 (if (viper-overlay-p viper-search-overlay)
708 (viper-move-overlay viper-search-overlay beg end)
709 (setq viper-search-overlay (viper-make-overlay beg end (current-buffer)))
710 (viper-overlay-put
711 viper-search-overlay 'priority viper-search-overlay-priority))
712 (viper-overlay-put viper-search-overlay 'face viper-search-face))
714 ;; Search
716 (defun viper-flash-search-pattern ()
717 (if (not (viper-has-face-support-p))
719 (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
720 (sit-for 2)
721 (viper-overlay-put viper-search-overlay 'face nil)))
723 (defun viper-hide-search-overlay ()
724 (if (not (viper-overlay-p viper-search-overlay))
725 (progn
726 (setq viper-search-overlay
727 (viper-make-overlay (point-min) (point-min) (current-buffer)))
728 (viper-overlay-put
729 viper-search-overlay 'priority viper-search-overlay-priority)))
730 (viper-overlay-put viper-search-overlay 'face nil))
732 ;; Replace state
734 (defsubst viper-move-replace-overlay (beg end)
735 (viper-move-overlay viper-replace-overlay beg end))
737 (defun viper-set-replace-overlay (beg end)
738 (if (viper-overlay-live-p viper-replace-overlay)
739 (viper-move-replace-overlay beg end)
740 (setq viper-replace-overlay (viper-make-overlay beg end (current-buffer)))
741 ;; never detach
742 (viper-overlay-put
743 viper-replace-overlay (if viper-emacs-p 'evaporate 'detachable) nil)
744 (viper-overlay-put
745 viper-replace-overlay 'priority viper-replace-overlay-priority)
746 ;; If Emacs will start supporting overlay maps, as it currently supports
747 ;; text-property maps, we could do away with viper-replace-minor-mode and
748 ;; just have keymap attached to replace overlay.
749 ;;(viper-overlay-put
750 ;; viper-replace-overlay
751 ;; (if viper-xemacs-p 'keymap 'local-map)
752 ;; viper-replace-map)
754 (if (viper-has-face-support-p)
755 (viper-overlay-put
756 viper-replace-overlay 'face viper-replace-overlay-face))
757 (viper-save-cursor-color 'before-replace-mode)
758 (viper-change-cursor-color viper-replace-overlay-cursor-color)
762 (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph)
763 (or (viper-overlay-live-p viper-replace-overlay)
764 (viper-set-replace-overlay (point-min) (point-min)))
765 (if (or (not (viper-has-face-support-p))
766 viper-use-replace-region-delimiters)
767 (let ((before-name (if viper-xemacs-p 'begin-glyph 'before-string))
768 (after-name (if viper-xemacs-p 'end-glyph 'after-string)))
769 (viper-overlay-put viper-replace-overlay before-name before-glyph)
770 (viper-overlay-put viper-replace-overlay after-name after-glyph))))
772 (defun viper-hide-replace-overlay ()
773 (viper-set-replace-overlay-glyphs nil nil)
774 (viper-restore-cursor-color 'after-replace-mode)
775 (viper-restore-cursor-color 'after-insert-mode)
776 (if (viper-has-face-support-p)
777 (viper-overlay-put viper-replace-overlay 'face nil)))
780 (defsubst viper-replace-start ()
781 (viper-overlay-start viper-replace-overlay))
782 (defsubst viper-replace-end ()
783 (viper-overlay-end viper-replace-overlay))
786 ;; Minibuffer
788 (defun viper-set-minibuffer-overlay ()
789 (viper-check-minibuffer-overlay)
790 (if (viper-has-face-support-p)
791 (progn
792 (viper-overlay-put
793 viper-minibuffer-overlay 'face viper-minibuffer-current-face)
794 (viper-overlay-put
795 viper-minibuffer-overlay 'priority viper-minibuffer-overlay-priority)
796 ;; never detach
797 (viper-overlay-put
798 viper-minibuffer-overlay
799 (if viper-emacs-p 'evaporate 'detachable)
800 nil)
801 ;; make viper-minibuffer-overlay open-ended
802 ;; In emacs, it is made open ended at creation time
803 (if viper-xemacs-p
804 (progn
805 (viper-overlay-put viper-minibuffer-overlay 'start-open nil)
806 (viper-overlay-put viper-minibuffer-overlay 'end-open nil)))
809 (defun viper-check-minibuffer-overlay ()
810 (or (viper-overlay-p viper-minibuffer-overlay)
811 (setq viper-minibuffer-overlay
812 (if viper-xemacs-p
813 (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
814 ;; make overlay open-ended
815 (viper-make-overlay
816 1 (1+ (buffer-size)) (current-buffer) nil 'rear-advance)))
820 (defsubst viper-is-in-minibuffer ()
821 (save-match-data
822 (string-match "\*Minibuf-" (buffer-name))))
826 ;;; XEmacs compatibility
828 (defun viper-abbreviate-file-name (file)
829 (if viper-emacs-p
830 (abbreviate-file-name file)
831 ;; XEmacs requires addl argument
832 (abbreviate-file-name file t)))
834 ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
835 ;; in sit-for, so this function smoothes out the differences.
836 (defsubst viper-sit-for-short (val &optional nodisp)
837 (if viper-xemacs-p
838 (sit-for (/ val 1000.0) nodisp)
839 (sit-for 0 val nodisp)))
841 ;; EVENT may be a single event of a sequence of events
842 (defsubst viper-ESC-event-p (event)
843 (let ((ESC-keys '(?\e (control \[) escape))
844 (key (viper-event-key event)))
845 (member key ESC-keys)))
847 ;; checks if object is a marker, has a buffer, and points to within that buffer
848 (defun viper-valid-marker (marker)
849 (if (and (markerp marker) (marker-buffer marker))
850 (let ((buf (marker-buffer marker))
851 (pos (marker-position marker)))
852 (save-excursion
853 (set-buffer buf)
854 (and (<= pos (point-max)) (<= (point-min) pos))))))
856 (defsubst viper-mark-marker ()
857 (if viper-xemacs-p
858 (mark-marker t)
859 (mark-marker)))
861 ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
862 ;; is the same as (mark t).
863 (defsubst viper-set-mark-if-necessary ()
864 (setq mark-ring (delete (viper-mark-marker) mark-ring))
865 (set-mark-command nil)
866 (setq viper-saved-mark (point)))
868 ;; In transient mark mode (zmacs mode), it is annoying when regions become
869 ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
870 ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
871 (defun viper-deactivate-mark ()
872 (if viper-xemacs-p
873 (zmacs-deactivate-region)
874 (deactivate-mark)))
876 (defsubst viper-leave-region-active ()
877 (if viper-xemacs-p
878 (setq zmacs-region-stays t)))
880 ;; Check if arg is a valid character for register
881 ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
882 ;; Letter means lowercase letters, Letter means uppercase letters, and
883 ;; digit means digits from 1 to 9.
884 ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
885 (defun viper-valid-register (reg &optional type)
886 (or type (setq type '(letter Letter digit)))
887 (or (if (memq 'letter type)
888 (and (<= ?a reg) (<= reg ?z)))
889 (if (memq 'digit type)
890 (and (<= ?1 reg) (<= reg ?9)))
891 (if (memq 'Letter type)
892 (and (<= ?A reg) (<= reg ?Z)))
896 (defsubst viper-events-to-keys (events)
897 (cond (viper-xemacs-p (events-to-keys events))
898 (t events)))
901 ;; it is suggested that an event must be copied before it is assigned to
902 ;; last-command-event in XEmacs
903 (defun viper-copy-event (event)
904 (if viper-xemacs-p
905 (copy-event event)
906 event))
908 ;; like read-event, but in XEmacs also try to convert to char, if possible
909 (defun viper-read-event-convert-to-char ()
910 (let (event)
911 (if viper-emacs-p
912 (read-event)
913 (setq event (next-command-event))
914 (or (event-to-character event)
915 event))
918 ;; This function lets function-key-map convert key sequences into logical
919 ;; keys. This does a better job than viper-read-event when it comes to kbd
920 ;; macros, since it enables certain macros to be shared between X and TTY modes
921 ;; by correctly mapping key sequences for Left/Right/... (one an ascii
922 ;; terminal) into logical keys left, right, etc.
923 (defun viper-read-key ()
924 (let ((overriding-local-map viper-overriding-map)
925 (inhibit-quit t)
926 help-char key)
927 (use-global-map viper-overriding-map)
928 (unwind-protect
929 (setq key (elt (read-key-sequence nil) 0))
930 (use-global-map global-map))
931 key))
934 ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
935 ;; instead of nil, if '(nil) was previously inadvertently assigned to
936 ;; unread-command-events
937 (defun viper-event-key (event)
938 (or (and event (eventp event))
939 (error "viper-event-key: Wrong type argument, eventp, %S" event))
940 (when (cond (viper-xemacs-p (or (key-press-event-p event)
941 (mouse-event-p event)))
942 (t t))
943 (let ((mod (event-modifiers event))
944 basis)
945 (setq basis
946 (cond
947 (viper-xemacs-p
948 (cond ((key-press-event-p event)
949 (event-key event))
950 ((button-event-p event)
951 (concat "mouse-" (prin1-to-string (event-button event))))
953 (error "viper-event-key: Unknown event, %S" event))))
955 ;; Emacs doesn't handle capital letters correctly, since
956 ;; \S-a isn't considered the same as A (it behaves as
957 ;; plain `a' instead). So we take care of this here
958 (cond ((and (viper-characterp event) (<= ?A event) (<= event ?Z))
959 (setq mod nil
960 event event))
961 ;; Emacs has the oddity whereby characters 128+char
962 ;; represent M-char *if* this appears inside a string.
963 ;; So, we convert them manually to (meta char).
964 ((and (viper-characterp event)
965 (< ?\C-? event) (<= event 255))
966 (setq mod '(meta)
967 event (- event ?\C-? 1)))
968 ((and (null mod) (eq event 'return))
969 (setq event ?\C-m))
970 ((and (null mod) (eq event 'space))
971 (setq event ?\ ))
972 ((and (null mod) (eq event 'delete))
973 (setq event ?\C-?))
974 ((and (null mod) (eq event 'backspace))
975 (setq event ?\C-h))
976 (t (event-basic-type event)))
978 (if (viper-characterp basis)
979 (setq basis
980 (if (viper= basis ?\C-?)
981 (list 'control '\?) ; taking care of an emacs bug
982 (intern (char-to-string basis)))))
983 (if mod
984 (append mod (list basis))
985 basis))))
987 (defun viper-key-to-emacs-key (key)
988 (let (key-name char-p modifiers mod-char-list base-key base-key-name)
989 (cond (viper-xemacs-p key)
991 ((symbolp key)
992 (setq key-name (symbol-name key))
993 (cond ((= (length key-name) 1) ; character event
994 (string-to-char key-name))
995 ;; Emacs doesn't recognize `return' and `escape' as events on
996 ;; dumb terminals, so we translate them into characters
997 ((and viper-emacs-p (not (viper-window-display-p))
998 (string= key-name "return"))
999 ?\C-m)
1000 ((and viper-emacs-p (not (viper-window-display-p))
1001 (string= key-name "escape"))
1002 ?\e)
1003 ;; pass symbol-event as is
1004 (t key)))
1006 ((listp key)
1007 (setq modifiers (subseq key 0 (1- (length key)))
1008 base-key (viper-seq-last-elt key)
1009 base-key-name (symbol-name base-key)
1010 char-p (= (length base-key-name) 1))
1011 (setq mod-char-list
1012 (mapcar
1013 '(lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
1014 modifiers))
1015 (if char-p
1016 (setq key-name
1017 (car (read-from-string
1018 (concat
1019 "?\\"
1020 (mapconcat 'identity mod-char-list "-\\")
1022 base-key-name))))
1023 (setq key-name
1024 (intern
1025 (concat
1026 (mapconcat 'identity mod-char-list "-")
1028 base-key-name))))))
1032 ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
1033 ;; convert events to keys and, if all keys are regular printable
1034 ;; characters, will return a string. Otherwise, will return a string
1035 ;; representing a vector of converted events. If the input was a Viper macro,
1036 ;; will return a string that represents this macro as a vector.
1037 (defun viper-array-to-string (event-seq)
1038 (let (temp temp2)
1039 (cond ((stringp event-seq) event-seq)
1040 ((viper-event-vector-p event-seq)
1041 (setq temp (mapcar 'viper-event-key event-seq))
1042 (cond ((viper-char-symbol-sequence-p temp)
1043 (mapconcat 'symbol-name temp ""))
1044 ((and (viper-char-array-p
1045 (setq temp2 (mapcar 'viper-key-to-character temp))))
1046 (mapconcat 'char-to-string temp2 ""))
1047 (t (prin1-to-string (vconcat temp)))))
1048 ((viper-char-symbol-sequence-p event-seq)
1049 (mapconcat 'symbol-name event-seq ""))
1050 ((and (vectorp event-seq)
1051 (viper-char-array-p
1052 (setq temp (mapcar 'viper-key-to-character event-seq))))
1053 (mapconcat 'char-to-string temp ""))
1054 (t (prin1-to-string event-seq)))))
1056 (defun viper-key-press-events-to-chars (events)
1057 (mapconcat (if viper-emacs-p
1058 'char-to-string
1059 (lambda (elt) (char-to-string (event-to-character elt))))
1060 events
1061 ""))
1064 ;; Uses different timeouts for ESC-sequences and others
1065 (defsubst viper-fast-keysequence-p ()
1066 (not (viper-sit-for-short
1067 (if (viper-ESC-event-p last-input-event)
1068 viper-ESC-keyseq-timeout
1069 viper-fast-keyseq-timeout)
1070 t)))
1072 (defun viper-read-char-exclusive ()
1073 (let (char
1074 (echo-keystrokes 1))
1075 (while (null char)
1076 (condition-case nil
1077 (setq char (read-char))
1078 (error
1079 ;; skip event if not char
1080 (viper-read-event))))
1081 char))
1083 ;; key is supposed to be in viper's representation, e.g., (control l), a
1084 ;; character, etc.
1085 (defun viper-key-to-character (key)
1086 (cond ((eq key 'space) ?\ )
1087 ((eq key 'delete) ?\C-?)
1088 ((eq key 'return) ?\C-m)
1089 ((eq key 'backspace) ?\C-h)
1090 ((and (symbolp key)
1091 (= 1 (length (symbol-name key))))
1092 (string-to-char (symbol-name key)))
1093 ((and (listp key)
1094 (eq (car key) 'control)
1095 (symbol-name (nth 1 key))
1096 (= 1 (length (symbol-name (nth 1 key)))))
1097 (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
1098 (t key)))
1101 (defun viper-setup-master-buffer (&rest other-files-or-buffers)
1102 "Set up the current buffer as a master buffer.
1103 Arguments become related buffers. This function should normally be used in
1104 the `Local variables' section of a file."
1105 (setq viper-related-files-and-buffers-ring
1106 (make-ring (1+ (length other-files-or-buffers))))
1107 (mapcar '(lambda (elt)
1108 (viper-ring-insert viper-related-files-and-buffers-ring elt))
1109 other-files-or-buffers)
1110 (viper-ring-insert viper-related-files-and-buffers-ring (buffer-name))
1113 ;;; Movement utilities
1115 ;; Characters that should not be considered as part of the word, in reformed-vi
1116 ;; syntax mode.
1117 (defconst viper-non-word-characters-reformed-vi
1118 "!@#$%^&*()-+=|\\~`{}[];:'\",<.>/?")
1119 ;; These are characters that are not to be considered as parts of a word in
1120 ;; Viper.
1121 ;; Set each time state changes and at loading time
1122 (viper-deflocalvar viper-non-word-characters nil)
1124 ;; must be buffer-local
1125 (viper-deflocalvar viper-ALPHA-char-class "w"
1126 "String of syntax classes characterizing Viper's alphanumeric symbols.
1127 In addition, the symbol `_' may be considered alphanumeric if
1128 `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
1130 (defconst viper-strict-ALPHA-chars "a-zA-Z0-9_"
1131 "Regexp matching the set of alphanumeric characters acceptable to strict
1132 Vi.")
1133 (defconst viper-strict-SEP-chars " \t\n"
1134 "Regexp matching the set of alphanumeric characters acceptable to strict
1135 Vi.")
1136 (defconst viper-strict-SEP-chars-sans-newline " \t"
1137 "Regexp matching the set of alphanumeric characters acceptable to strict
1138 Vi.")
1140 (defconst viper-SEP-char-class " -"
1141 "String of syntax classes for Vi separators.
1142 Usually contains ` ', linefeed, TAB or formfeed.")
1145 ;; Set Viper syntax classes and related variables according to
1146 ;; `viper-syntax-preference'.
1147 (defun viper-update-syntax-classes (&optional set-default)
1148 (let ((preference (cond ((eq viper-syntax-preference 'emacs)
1149 "w") ; Viper words have only Emacs word chars
1150 ((eq viper-syntax-preference 'extended)
1151 "w_") ; Viper words have Emacs word & symbol chars
1152 (t "w"))) ; Viper words are Emacs words plus `_'
1153 (non-word-chars (cond ((eq viper-syntax-preference 'reformed-vi)
1154 (viper-string-to-list
1155 viper-non-word-characters-reformed-vi))
1156 (t nil))))
1157 (if set-default
1158 (setq-default viper-ALPHA-char-class preference
1159 viper-non-word-characters non-word-chars)
1160 (setq viper-ALPHA-char-class preference
1161 viper-non-word-characters non-word-chars))
1164 ;; SYMBOL is used because customize requires it, but it is ignored, unless it
1165 ;; is `nil'. If nil, use setq.
1166 (defun viper-set-syntax-preference (&optional symbol value)
1167 "Set Viper syntax preference.
1168 If called interactively or if SYMBOL is nil, sets syntax preference in current
1169 buffer. If called non-interactively, preferably via the customization widget,
1170 sets the default value."
1171 (interactive)
1172 (or value
1173 (setq value
1174 (completing-read
1175 "Viper syntax preference: "
1176 '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
1177 nil 'require-match)))
1178 (if (stringp value) (setq value (intern value)))
1179 (or (memq value '(strict-vi reformed-vi extended emacs))
1180 (error "Invalid Viper syntax preference, %S" value))
1181 (if symbol
1182 (setq-default viper-syntax-preference value)
1183 (setq viper-syntax-preference value))
1184 (viper-update-syntax-classes))
1186 (defcustom viper-syntax-preference 'reformed-vi
1187 "*Syntax type characterizing Viper's alphanumeric symbols.
1188 Affects movement and change commands that deal with Vi-style words.
1189 Works best when set in the hooks to various major modes.
1191 `strict-vi' means Viper words are (hopefully) exactly as in Vi.
1193 `reformed-vi' means Viper words are like Emacs words \(as determined using
1194 Emacs syntax tables, which are different for different major modes\) with two
1195 exceptions: the symbol `_' is always part of a word and typical Vi non-word
1196 symbols, such as `,',:,\",),{, etc., are excluded.
1197 This behaves very close to `strict-vi', but also works well with non-ASCII
1198 characters from various alphabets.
1200 `extended' means Viper word constituents are symbols that are marked as being
1201 parts of words OR symbols in Emacs syntax tables.
1202 This is most appropriate for major modes intended for editing programs.
1204 `emacs' means Viper words are the same as Emacs words as specified by Emacs
1205 syntax tables.
1206 This option is appropriate if you like Emacs-style words."
1207 :type '(radio (const strict-vi) (const reformed-vi)
1208 (const extended) (const emacs))
1209 :set 'viper-set-syntax-preference
1210 :group 'viper)
1211 (make-variable-buffer-local 'viper-syntax-preference)
1214 ;; addl-chars are characters to be temporarily considered as alphanumerical
1215 (defun viper-looking-at-alpha (&optional addl-chars)
1216 (or (stringp addl-chars) (setq addl-chars ""))
1217 (if (eq viper-syntax-preference 'reformed-vi)
1218 (setq addl-chars (concat addl-chars "_")))
1219 (let ((char (char-after (point))))
1220 (if char
1221 (if (eq viper-syntax-preference 'strict-vi)
1222 (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars "]"))
1224 ;; or one of the additional chars being asked to include
1225 (viper-memq-char char (viper-string-to-list addl-chars))
1226 (and
1227 ;; not one of the excluded word chars (note:
1228 ;; viper-non-word-characters is a list)
1229 (not (viper-memq-char char viper-non-word-characters))
1230 ;; char of the Viper-word syntax class
1231 (viper-memq-char (char-syntax char)
1232 (viper-string-to-list viper-ALPHA-char-class))))))
1235 (defun viper-looking-at-separator ()
1236 (let ((char (char-after (point))))
1237 (if char
1238 (if (eq viper-syntax-preference 'strict-vi)
1239 (viper-memq-char char (viper-string-to-list viper-strict-SEP-chars))
1240 (or (eq char ?\n) ; RET is always a separator in Vi
1241 (viper-memq-char (char-syntax char)
1242 (viper-string-to-list viper-SEP-char-class)))))
1245 (defsubst viper-looking-at-alphasep (&optional addl-chars)
1246 (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars)))
1248 (defun viper-skip-alpha-forward (&optional addl-chars)
1249 (or (stringp addl-chars) (setq addl-chars ""))
1250 (viper-skip-syntax
1251 'forward
1252 (cond ((eq viper-syntax-preference 'strict-vi)
1254 (t viper-ALPHA-char-class))
1255 (cond ((eq viper-syntax-preference 'strict-vi)
1256 (concat viper-strict-ALPHA-chars addl-chars))
1257 (t addl-chars))))
1259 (defun viper-skip-alpha-backward (&optional addl-chars)
1260 (or (stringp addl-chars) (setq addl-chars ""))
1261 (viper-skip-syntax
1262 'backward
1263 (cond ((eq viper-syntax-preference 'strict-vi)
1265 (t viper-ALPHA-char-class))
1266 (cond ((eq viper-syntax-preference 'strict-vi)
1267 (concat viper-strict-ALPHA-chars addl-chars))
1268 (t addl-chars))))
1270 ;; weird syntax tables may confuse strict-vi style
1271 (defsubst viper-skip-all-separators-forward (&optional within-line)
1272 (if (eq viper-syntax-preference 'strict-vi)
1273 (if within-line
1274 (skip-chars-forward viper-strict-SEP-chars-sans-newline)
1275 (skip-chars-forward viper-strict-SEP-chars))
1276 (viper-skip-syntax 'forward
1277 viper-SEP-char-class
1278 (or within-line "\n")
1279 (if within-line (viper-line-pos 'end)))))
1280 (defsubst viper-skip-all-separators-backward (&optional within-line)
1281 (if (eq viper-syntax-preference 'strict-vi)
1282 (if within-line
1283 (skip-chars-backward viper-strict-SEP-chars-sans-newline)
1284 (skip-chars-backward viper-strict-SEP-chars))
1285 (viper-skip-syntax 'backward
1286 viper-SEP-char-class
1287 (or within-line "\n")
1288 (if within-line (viper-line-pos 'start)))))
1289 (defun viper-skip-nonseparators (direction)
1290 (viper-skip-syntax
1291 direction
1292 (concat "^" viper-SEP-char-class)
1294 (viper-line-pos (if (eq direction 'forward) 'end 'start))))
1297 ;; skip over non-word constituents and non-separators
1298 (defun viper-skip-nonalphasep-forward ()
1299 (if (eq viper-syntax-preference 'strict-vi)
1300 (skip-chars-forward
1301 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
1302 (viper-skip-syntax
1303 'forward
1304 (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
1305 ;; Emacs may consider some of these as words, but we don't want them
1306 viper-non-word-characters
1307 (viper-line-pos 'end))))
1308 (defun viper-skip-nonalphasep-backward ()
1309 (if (eq viper-syntax-preference 'strict-vi)
1310 (skip-chars-backward
1311 (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
1312 (viper-skip-syntax
1313 'backward
1314 (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
1315 ;; Emacs may consider some of these as words, but we don't want them
1316 viper-non-word-characters
1317 (viper-line-pos 'start))))
1319 ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
1320 ;; Return the number of chars traveled.
1321 ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
1322 ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
1323 ;; words, even if Emacs syntax table says they are.
1324 (defun viper-skip-syntax (direction syntax addl-chars &optional limit)
1325 (let ((total 0)
1326 (local 1)
1327 (skip-chars-func
1328 (if (eq direction 'forward)
1329 'skip-chars-forward 'skip-chars-backward))
1330 (skip-syntax-func
1331 (if (eq direction 'forward)
1332 'viper-forward-char-carefully 'viper-backward-char-carefully))
1333 char-looked-at syntax-of-char-looked-at negated-syntax)
1334 (setq addl-chars
1335 (cond ((listp addl-chars) (viper-charlist-to-string addl-chars))
1336 ((stringp addl-chars) addl-chars)
1337 (t "")))
1338 (setq syntax
1339 (cond ((listp syntax) syntax)
1340 ((stringp syntax) (viper-string-to-list syntax))
1341 (t nil)))
1342 (if (memq ?^ syntax) (setq negated-syntax t))
1344 (while (and (not (= local 0))
1345 (cond ((eq direction 'forward)
1346 (not (eobp)))
1347 (t (not (bobp)))))
1348 (setq char-looked-at (viper-char-at-pos direction)
1349 ;; if outside the range, set to nil
1350 syntax-of-char-looked-at (if char-looked-at
1351 (char-syntax char-looked-at)))
1352 (setq local
1353 (+ (if (and
1354 (cond ((and limit (eq direction 'forward))
1355 (< (point) limit))
1356 (limit ; backward & limit
1357 (> (point) limit))
1358 (t t)) ; no limit
1359 ;; char under/before cursor has appropriate syntax
1360 (if negated-syntax
1361 (not (memq syntax-of-char-looked-at syntax))
1362 (memq syntax-of-char-looked-at syntax))
1363 ;; if char-syntax class is "word", make sure it is not one
1364 ;; of the excluded characters
1365 (if (and (eq syntax-of-char-looked-at ?w)
1366 (not negated-syntax))
1367 (not (viper-memq-char
1368 char-looked-at viper-non-word-characters))
1370 (funcall skip-syntax-func 1)
1372 (funcall skip-chars-func addl-chars limit)))
1373 (setq total (+ total local)))
1374 total
1379 (provide 'viper-util)
1382 ;;; Local Variables:
1383 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1384 ;;; End:
1386 ;;; viper-util.el ends here