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