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