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