new version
[emacs.git] / lisp / emulation / viper-init.el
blobbd1bddfebb013ffaf3fa468f1e06229cdb6b7d26
1 ;;; viper-init.el --- some common definitions for Viper
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
22 ;; Code
24 (provide 'viper-init)
26 ;; compiler pacifier
27 (defvar mark-even-if-inactive)
28 (defvar quail-mode)
29 (defvar iso-accents-mode)
30 (defvar viper-current-state)
31 (defvar viper-version)
32 (defvar viper-expert-level)
33 ;; end pacifier
36 ;; Viper version
37 (defun viper-version ()
38 (interactive)
39 (message "Viper version is %s" viper-version))
41 ;; Is it XEmacs?
42 (defconst viper-xemacs-p (string-match "XEmacs" emacs-version))
43 ;; Is it Emacs?
44 (defconst viper-emacs-p (not viper-xemacs-p))
45 ;; Tell whether we are running as a window application or on a TTY
46 (defsubst viper-device-type ()
47 (if viper-emacs-p
48 window-system
49 (device-type (selected-device))))
50 ;; in XEmacs: device-type is tty on tty and stream in batch.
51 (defun viper-window-display-p ()
52 (and (viper-device-type) (not (memq (viper-device-type) '(tty stream pc)))))
54 (defcustom viper-ms-style-os-p (memq system-type
55 '(ms-dos windows-nt windows-95))
56 "Tells if Emacs is running under an MS-style OS: ms-dos, windows-nt, W95."
57 :type 'boolean
58 :tag "Is it Microsoft-made OS?"
59 :group 'viper-misc)
60 (defcustom viper-vms-os-p (memq system-type '(vax-vms axp-vms))
61 "Tells if Emacs is running under VMS."
62 :type 'boolean
63 :tag "Is it VMS?"
64 :group 'viper-misc)
66 (defcustom viper-force-faces nil
67 "If t, Viper will think that it is running on a display that supports faces.
68 This is provided as a temporary relief for users of graphics-capable terminals
69 that Viper doesn't know about.
70 In all likelihood, you don't need to bother with this setting."
71 :type 'boolean
72 :group 'viper-highlighting)
74 (defun viper-has-face-support-p ()
75 (cond ((viper-window-display-p))
76 (viper-force-faces)
77 (viper-emacs-p (memq (viper-device-type) '(pc)))
78 (viper-xemacs-p (memq (viper-device-type) '(tty pc)))))
81 ;;; Macros
83 (defmacro viper-deflocalvar (var default-value &optional documentation)
84 (` (progn
85 (defvar (, var) (, default-value)
86 (, (format "%s\n\(buffer local\)" documentation)))
87 (make-variable-buffer-local '(, var))
88 )))
90 ;; (viper-loop COUNT BODY) Execute BODY COUNT times.
91 (defmacro viper-loop (count &rest body)
92 (` (let ((count (, count)))
93 (while (> count 0)
94 (progn
95 (,@ body)
96 (setq count (1- count))
98 )))
100 (defmacro viper-buffer-live-p (buf)
101 (` (and (, buf) (get-buffer (, buf)) (buffer-name (get-buffer (, buf))))))
103 ;; return buffer-specific macro definition, given a full macro definition
104 (defmacro viper-kbd-buf-alist (macro-elt)
105 (` (nth 1 (, macro-elt))))
106 ;; get a pair: (curr-buffer . macro-definition)
107 (defmacro viper-kbd-buf-pair (macro-elt)
108 (` (assoc (buffer-name) (viper-kbd-buf-alist (, macro-elt)))))
109 ;; get macro definition for current buffer
110 (defmacro viper-kbd-buf-definition (macro-elt)
111 (` (cdr (viper-kbd-buf-pair (, macro-elt)))))
113 ;; return mode-specific macro definitions, given a full macro definition
114 (defmacro viper-kbd-mode-alist (macro-elt)
115 (` (nth 2 (, macro-elt))))
116 ;; get a pair: (major-mode . macro-definition)
117 (defmacro viper-kbd-mode-pair (macro-elt)
118 (` (assoc major-mode (viper-kbd-mode-alist (, macro-elt)))))
119 ;; get macro definition for the current major mode
120 (defmacro viper-kbd-mode-definition (macro-elt)
121 (` (cdr (viper-kbd-mode-pair (, macro-elt)))))
123 ;; return global macro definition, given a full macro definition
124 (defmacro viper-kbd-global-pair (macro-elt)
125 (` (nth 3 (, macro-elt))))
126 ;; get global macro definition from an elt of macro-alist
127 (defmacro viper-kbd-global-definition (macro-elt)
128 (` (cdr (viper-kbd-global-pair (, macro-elt)))))
130 ;; last elt of a sequence
131 (defsubst viper-seq-last-elt (seq)
132 (elt seq (1- (length seq))))
134 (defsubst viper-string-to-list (string)
135 (append (vconcat string) nil))
137 (defsubst viper-charlist-to-string (list)
138 (mapconcat 'char-to-string list ""))
140 ;; like char-after/before, but saves typing
141 (defun viper-char-at-pos (direction &optional offset)
142 (or (integerp offset) (setq offset 0))
143 (if (eq direction 'forward)
144 (char-after (+ (point) offset))
145 (char-before (- (point) offset))))
148 (defvar viper-minibuffer-overlay-priority 300)
149 (defvar viper-replace-overlay-priority 400)
150 (defvar viper-search-overlay-priority 500)
153 ;;; Viper minor modes
155 ;; Mode for vital things like \e, C-z.
156 (viper-deflocalvar viper-vi-intercept-minor-mode nil)
158 (viper-deflocalvar viper-vi-basic-minor-mode nil
159 "Viper's minor mode for Vi bindings.")
161 (viper-deflocalvar viper-vi-local-user-minor-mode nil
162 "Auxiliary minor mode for user-defined local bindings in Vi state.")
164 (viper-deflocalvar viper-vi-global-user-minor-mode nil
165 "Auxiliary minor mode for user-defined global bindings in Vi state.")
167 (viper-deflocalvar viper-vi-state-modifier-minor-mode nil
168 "Minor mode used to make major-mode-specific modification to Vi state.")
170 (viper-deflocalvar viper-vi-diehard-minor-mode nil
171 "This minor mode is in effect when the user wants Viper to be Vi.")
173 (viper-deflocalvar viper-vi-kbd-minor-mode nil
174 "Minor mode for Ex command macros in Vi state.
175 The corresponding keymap stores key bindings of Vi macros defined with
176 the Ex command :map.")
178 ;; Mode for vital things like \e, C-z.
179 (viper-deflocalvar viper-insert-intercept-minor-mode nil)
181 (viper-deflocalvar viper-insert-basic-minor-mode nil
182 "Viper's minor mode for bindings in Insert mode.")
184 (viper-deflocalvar viper-insert-local-user-minor-mode nil
185 "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
186 This is a way to overshadow normal Insert mode bindings locally to certain
187 designated buffers.")
189 (viper-deflocalvar viper-insert-global-user-minor-mode nil
190 "Auxiliary minor mode for global user-defined bindings in Insert state.")
192 (viper-deflocalvar viper-insert-state-modifier-minor-mode nil
193 "Minor mode used to make major-mode-specific modification to Insert state.")
195 (viper-deflocalvar viper-insert-diehard-minor-mode nil
196 "Minor mode that simulates Vi very closely.
197 Not recommened, except for the novice user.")
199 (viper-deflocalvar viper-insert-kbd-minor-mode nil
200 "Minor mode for Ex command macros Insert state.
201 The corresponding keymap stores key bindings of Vi macros defined with
202 the Ex command :map!.")
204 (viper-deflocalvar viper-replace-minor-mode nil
205 "Minor mode in effect in replace state (cw, C, and the like commands).")
207 ;; Mode for vital things like \C-z and \C-x)
208 ;; This is t, by default. So, any new buffer will have C-z defined as
209 ;; switch to Vi, unless we switched states in this buffer
210 (viper-deflocalvar viper-emacs-intercept-minor-mode t)
212 (viper-deflocalvar viper-emacs-local-user-minor-mode t
213 "Minor mode for local user bindings effective in Emacs state.
214 Users can use it to override Emacs bindings when Viper is in its Emacs
215 state.")
217 (viper-deflocalvar viper-emacs-global-user-minor-mode t
218 "Minor mode for global user bindings in effect in Emacs state.
219 Users can use it to override Emacs bindings when Viper is in its Emacs
220 state.")
222 (viper-deflocalvar viper-emacs-kbd-minor-mode t
223 "Minor mode for Vi style macros in Emacs state.
224 The corresponding keymap stores key bindings of Vi macros defined with
225 `viper-record-kbd-macro' command. There is no Ex-level command to do this
226 interactively.")
228 (viper-deflocalvar viper-emacs-state-modifier-minor-mode t
229 "Minor mode used to make major-mode-specific modification to Emacs state.
230 For instance, a Vi purist may want to bind `dd' in Dired mode to a function
231 that deletes a file.")
233 (viper-deflocalvar viper-vi-minibuffer-minor-mode nil
234 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
236 (viper-deflocalvar viper-insert-minibuffer-minor-mode nil
237 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
241 ;; Some common error messages
243 (defconst viper-SpuriousText "Spurious text after command" "")
244 (defconst viper-BadExCommand "Not an editor command" "")
245 (defconst viper-InvalidCommandArgument "Invalid command argument" "")
246 (defconst viper-NoPrevSearch "No previous search string" "")
247 (defconst viper-EmptyRegister "`%c': Nothing in this register" "")
248 (defconst viper-InvalidRegister "`%c': Invalid register" "")
249 (defconst viper-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
250 (defconst viper-InvalidTextmarker "`%c': Invalid text marker" "")
251 (defconst viper-InvalidViCommand "Invalid command" "")
252 (defconst viper-BadAddress "Ill-formed address" "")
253 (defconst viper-FirstAddrExceedsSecond "First address exceeds second" "")
254 (defconst viper-NoFileSpecified "No file specified" "")
256 ;; Is t until viper-mode executes for the very first time.
257 ;; Prevents recursive descend into startup messages.
258 (defvar viper-first-time t)
260 (defvar viper-expert-level (if (boundp 'viper-expert-level) viper-expert-level 0)
261 "User's expert level.
262 The minor mode viper-vi-diehard-minor-mode is in effect when
263 viper-expert-level is 1 or 2 or when viper-want-emacs-keys-in-vi is t.
264 The minor mode viper-insert-diehard-minor-mode is in effect when
265 viper-expert-level is 1 or 2 or if viper-want-emacs-keys-in-insert is t.
266 Use `M-x viper-set-expert-level' to change this.")
268 ;; Max expert level supported by Viper. This is NOT a user option.
269 ;; It is here to make it hard for the user from resetting it.
270 (defconst viper-max-expert-level 5)
273 ;;; ISO characters and MULE
275 ;; If non-nil, ISO accents will be turned on in insert/replace emacs states and
276 ;; turned off in vi-state. For some users, this behavior may be too
277 ;; primitive. In this case, use insert/emacs/vi state hooks.
278 (viper-deflocalvar viper-automatic-iso-accents nil "")
279 ;; Set iso-accents-mode to ARG. Check if it is bound first
280 (defsubst viper-set-iso-accents-mode (arg)
281 (if (boundp 'iso-accents-mode)
282 (setq iso-accents-mode arg)))
284 ;; Internal flag used to control when viper mule hooks are run.
285 ;; Don't change this!
286 (defvar viper-mule-hook-flag t)
287 ;; If non-nil, the default intl. input method is turned on.
288 (viper-deflocalvar viper-special-input-method nil "")
290 ;; viper hook to run on input-method activation
291 (defun viper-activate-input-method-action ()
292 (if (null viper-mule-hook-flag)
294 (setq viper-special-input-method t)
295 ;; turn off special input methods in vi-state
296 (if (eq viper-current-state 'vi-state)
297 (viper-set-input-method nil))
298 (if (memq viper-current-state '(vi-state insert-state replace-state))
299 (message "Viper special input method%s: on"
300 (if (or current-input-method default-input-method)
301 (format " %S"
302 (or current-input-method default-input-method))
303 "")))
305 ;; viper hook to run on input-method deactivation
306 (defun viper-inactivate-input-method-action ()
307 (if (null viper-mule-hook-flag)
309 (setq viper-special-input-method nil)
310 (if (memq viper-current-state '(vi-state insert-state replace-state))
311 (message "Viper special input method%s: off"
312 (if (or current-input-method default-input-method)
313 (format " %S"
314 (or current-input-method default-input-method))
315 "")))))
317 (defun viper-inactivate-input-method ()
318 (cond ((and viper-emacs-p (fboundp 'inactivate-input-method))
319 (inactivate-input-method))
320 ((and viper-xemacs-p (boundp 'current-input-method))
321 ;; XEmacs had broken quil-mode for some time, so we are working around
322 ;; it here
323 (setq quail-mode nil)
324 (if (featurep 'quail)
325 (quail-delete-overlays))
326 (setq describe-current-input-method-function nil)
327 (setq current-input-method nil)
328 (run-hooks 'input-method-inactivate-hook)
329 (force-mode-line-update))
331 (defun viper-activate-input-method ()
332 (cond ((and viper-emacs-p (fboundp 'activate-input-method))
333 (activate-input-method default-input-method))
334 ((and viper-xemacs-p (fboundp 'quail-mode))
335 (quail-mode 1))))
337 ;; Set quail-mode to ARG
338 (defun viper-set-input-method (arg)
339 (setq viper-mule-hook-flag t) ; just a precaution
340 (let (viper-mule-hook-flag) ; temporarily inactivate viper mule hooks
341 (cond ((and arg (> (prefix-numeric-value arg) 0) default-input-method)
342 ;; activate input method
343 (viper-activate-input-method))
344 (t ; deactivate input method
345 (viper-inactivate-input-method)))
349 ;; VI-style Undo
351 ;; Used to 'undo' complex commands, such as replace and insert commands.
352 (viper-deflocalvar viper-undo-needs-adjustment nil)
353 (put 'viper-undo-needs-adjustment 'permanent-local t)
355 ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
356 ;; complex command that must be undone atomically. If inserted, it is
357 ;; erased by viper-change-state-to-vi and viper-repeat.
358 (defconst viper-buffer-undo-list-mark 'viper)
360 (defcustom viper-keep-point-on-undo nil
361 "*Non-nil means not to move point while undoing commands.
362 This style is different from Emacs and Vi. Try it to see if
363 it better fits your working style."
364 :type 'boolean
365 :tag "Preserve Position of Point After Undo"
366 :group 'viper)
368 ;; Replace mode and changing text
370 ;; Viper's own after/before change functions, which get viper-add-hook'ed to
371 ;; Emacs's
372 (viper-deflocalvar viper-after-change-functions nil "")
373 (viper-deflocalvar viper-before-change-functions nil "")
374 (viper-deflocalvar viper-post-command-hooks nil "")
375 (viper-deflocalvar viper-pre-command-hooks nil "")
377 ;; Can be used to pass global states around for short period of time
378 (viper-deflocalvar viper-intermediate-command nil "")
380 ;; This is used to pass the right Vi command key sequence to
381 ;; viper-set-destructive-command whenever (this-command-keys) doesn't give the
382 ;; right result. For instance, in commands like c/bla<RET>, (this-command-keys)
383 ;; will return ^M, which invoked exit-minibuffer, while we need "c/"
384 (defconst viper-this-command-keys nil)
386 ;; Indicates that the current destructive command has started in replace mode.
387 (viper-deflocalvar viper-began-as-replace nil "")
389 (defcustom viper-allow-multiline-replace-regions t
390 "If non-nil, Viper will allow multi-line replace regions.
391 This is an extension to standard Vi.
392 If nil, commands that attempt to replace text spanning multiple lines first
393 delete the text being replaced, as in standard Vi."
394 :type 'boolean
395 :group 'viper)
397 (defcustom viper-replace-overlay-cursor-color "Red"
398 "*Cursor color when Viper is in Replace state."
399 :type 'string
400 :group 'viper)
401 (defcustom viper-insert-state-cursor-color "Green"
402 "Cursor color when Viper is in insert state."
403 :type 'string
404 :group 'viper)
406 ;; place to save cursor colow when switching to insert mode
407 (viper-deflocalvar viper-saved-cursor-color nil "")
409 (viper-deflocalvar viper-replace-overlay nil "")
410 (put 'viper-replace-overlay 'permanent-local t)
412 (defcustom viper-replace-region-end-delimiter "$"
413 "A string marking the end of replacement regions.
414 It is used only with TTYs or if `viper-use-replace-region-delimiters'
415 is non-nil."
416 :type 'string
417 :group 'viper)
418 (defcustom viper-replace-region-start-delimiter ""
419 "A string marking the beginning of replacement regions.
420 It is used only with TTYs or if `viper-use-replace-region-delimiters'
421 is non-nil."
422 :type 'string
423 :group 'viper)
424 (defcustom viper-use-replace-region-delimiters
425 (or (not (viper-has-face-support-p))
426 (and viper-xemacs-p (eq (viper-device-type) 'tty)))
427 "*If non-nil, Viper will always use `viper-replace-region-end-delimiter' and
428 `viper-replace-region-start-delimiter' to delimit replacement regions, even on
429 color displays. By default, the delimiters are used only on TTYs."
430 :type 'boolean
431 :group 'viper)
433 ;; XEmacs requires glyphs
434 (if viper-xemacs-p
435 (progn
436 (or (glyphp viper-replace-region-end-delimiter)
437 (setq viper-replace-region-end-delimiter
438 (make-glyph viper-replace-region-end-delimiter)))
439 (or (glyphp viper-replace-region-start-delimiter)
440 (setq viper-replace-region-start-delimiter
441 (make-glyph viper-replace-region-start-delimiter)))
445 ;; These are local marker that must be initialized to nil and moved with
446 ;; `viper-move-marker-locally'
448 ;; Remember the last position inside the replace region.
449 (viper-deflocalvar viper-last-posn-in-replace-region nil)
450 ;; Remember the last position while inserting
451 (viper-deflocalvar viper-last-posn-while-in-insert-state nil)
452 (put 'viper-last-posn-in-replace-region 'permanent-local t)
453 (put 'viper-last-posn-while-in-insert-state 'permanent-local t)
455 (viper-deflocalvar viper-sitting-in-replace nil "")
456 (put 'viper-sitting-in-replace 'permanent-local t)
458 ;; Remember the number of characters that have to be deleted in replace
459 ;; mode to compensate for the inserted characters.
460 (viper-deflocalvar viper-replace-chars-to-delete 0 "")
461 ;; This variable is used internally by the before/after changed functions to
462 ;; determine how many chars were deleted by the change. This can't be
463 ;; determined inside after-change-functions because those get the length of the
464 ;; deleted region, not the number of chars deleted (which are two different
465 ;; things under MULE).
466 (viper-deflocalvar viper-replace-region-chars-deleted 0 "")
468 ;; Insertion ring and command ring
469 (defcustom viper-insertion-ring-size 14
470 "The size of history of inserted text.
471 This is a list where Viper keeps the history of previously inserted pieces of
472 text."
473 :type 'integer
474 :group 'viper-misc)
475 ;; The insertion ring.
476 (defvar viper-insertion-ring nil)
477 ;; This is temp insertion ring. Used to do rotation for display purposes.
478 ;; When rotation just started, it is initialized to viper-insertion-ring.
479 (defvar viper-temp-insertion-ring nil)
480 (defvar viper-last-inserted-string-from-insertion-ring "")
482 (defcustom viper-command-ring-size 14
483 "The size of history of Vi commands repeatable with dot."
484 :type 'integer
485 :group 'viper-misc)
486 ;; The command ring.
487 (defvar viper-command-ring nil)
488 ;; This is temp command ring. Used to do rotation for display purposes.
489 ;; When rotation just started, it is initialized to viper-command-ring.
490 (defvar viper-temp-command-ring nil)
492 ;; Fast keyseq and ESC keyseq timeouts
493 (defcustom viper-fast-keyseq-timeout 200
494 "*Key sequence separated by no more than this many milliseconds is viewed as a Vi-style macro, if such a macro is defined.
495 Setting this too high may slow down your typing. Setting this value too low
496 will make it hard to use Vi-stile timeout macros."
497 :type 'integer
498 :group 'viper-misc)
500 (defcustom viper-ESC-keyseq-timeout (if (viper-window-display-p)
501 0 viper-fast-keyseq-timeout)
502 "*Key sequence beginning with ESC and separated by no more than this many milliseconds is considered to be generated by a keyboard function key.
503 Setting this too high may slow down switching from insert to vi state. Setting
504 this value too low will make it impossible to use function keys in insert mode
505 on a dumb terminal."
506 :type 'integer
507 :group 'viper-misc)
509 ;; Modes and related variables
511 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
512 (viper-deflocalvar viper-current-state 'emacs-state)
515 ;; Autoindent in insert
517 ;; Variable that keeps track of whether C-t has been pressed.
518 (viper-deflocalvar viper-cted nil "")
520 ;; Preserve the indent value, used by C-d in insert mode.
521 (viper-deflocalvar viper-current-indent 0)
523 ;; Whether to preserve the indent, used by C-d in insert mode.
524 (viper-deflocalvar viper-preserve-indent nil)
526 (viper-deflocalvar viper-auto-indent nil "")
527 (defcustom viper-auto-indent nil
528 "*Enable autoindent, if t.
529 This is a buffer-local variable."
530 :type 'boolean
531 :group 'viper)
533 (viper-deflocalvar viper-electric-mode t "")
534 (defcustom viper-electric-mode t
535 "*If t, electrify Viper.
536 Currently, this only electrifies auto-indentation, making it appropriate to the
537 mode of the buffer.
538 This means that auto-indentation will depart from standard Vi and will indent
539 appropriate to the mode of the buffer. This is especially useful for editing
540 programs and LaTeX documents."
541 :type 'boolean
542 :group 'viper)
544 (defcustom viper-shift-width 8
545 "*The shiftwidth variable."
546 :type 'integer
547 :group 'viper)
549 ;; Variables for repeating destructive commands
551 (defcustom viper-keep-point-on-repeat t
552 "*If t, don't move point when repeating previous command.
553 This is useful for doing repeated changes with the '.' key.
554 The user can change this to nil, if she likes when the cursor moves
555 to a new place after repeating previous Vi command."
556 :type 'boolean
557 :group 'viper)
559 ;; Remember insert point as a marker. This is a local marker that must be
560 ;; initialized to nil and moved with `viper-move-marker-locally'.
561 (viper-deflocalvar viper-insert-point nil)
562 (put 'viper-insert-point 'permanent-local t)
564 ;; This remembers the point before dabbrev-expand was called.
565 ;; If viper-insert-point turns out to be bigger than that, it is reset
566 ;; back to viper-pre-command-point.
567 ;; The reason this is needed is because dabbrev-expand (and possibly
568 ;; others) may jump to before the insertion point, delete something and
569 ;; then reinsert a bigger piece. For instance: bla^blo
570 ;; If dabbrev-expand is called after `blo' and ^ undicates viper-insert-point,
571 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
572 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
573 ;; will insert the expansion, and we get: blablo^
574 ;; Whatever we insert next goes before the ^, i.e., before the
575 ;; viper-insert-point marker. So, Viper will think that nothing was
576 ;; inserted. Remembering the orig position of the marker circumvents the
577 ;; problem.
578 ;; We don't know of any command, except dabbrev-expand, that has the same
579 ;; problem. However, the same trick can be used if such a command is
580 ;; discovered later.
582 (viper-deflocalvar viper-pre-command-point nil)
583 (put 'viper-pre-command-point 'permanent-local t) ; this is probably an overkill
585 ;; This is used for saving inserted text.
586 (defvar viper-last-insertion nil)
588 ;; Remembers the last replaced region.
589 (defvar viper-last-replace-region "")
591 ;; Remember com point as a marker.
592 ;; This is a local marker. Should be moved with `viper-move-marker-locally'
593 (viper-deflocalvar viper-com-point nil)
595 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
596 ;; It is used to re-execute last destructive command.
597 ;; M-COM is a Lisp symbol representing the function to be executed.
598 ;; VAL is the prefix argument that was used with that command.
599 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
600 ;; additional information on how the function in M-COM is to be handled.
601 ;; REG is the register used by command
602 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
603 ;; commands).
604 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
605 (defvar viper-d-com nil)
607 ;; The character remembered by the Vi `r' command.
608 (defvar viper-d-char nil)
610 ;; Name of register to store deleted or yanked strings
611 (defvar viper-use-register nil)
614 ;;; Variables for Moves and Searches
616 (defgroup viper-search nil
617 "Variables that define the search and query-replace behavior of Viper."
618 :prefix "viper-"
619 :group 'viper)
621 ;; For use by `;' command.
622 (defvar viper-f-char nil)
624 ;; For use by `.' command.
625 (defvar viper-F-char nil)
627 ;; For use by `;' command.
628 (defvar viper-f-forward nil)
630 ;; For use by `;' command.
631 (defvar viper-f-offset nil)
633 ;; Last search string
634 (defvar viper-s-string "")
636 (defcustom viper-quote-string "> "
637 "String inserted at the beginning of quoted region."
638 :type 'string
639 :group 'viper)
641 ;; If t, search is forward.
642 (defvar viper-s-forward nil)
644 (defcustom viper-case-fold-search nil
645 "*If not nil, search ignores cases."
646 :type 'boolean
647 :group 'viper-search)
649 (defcustom viper-re-search t
650 "*If not nil, search is regexp search, otherwise vanilla search."
651 :type 'boolean
652 :tag "Regexp Search"
653 :group 'viper-search)
655 (defcustom viper-search-scroll-threshold 2
656 "*If search lands within this threshnold from the window top/bottom,
657 the window will be scrolled up or down appropriately, to reveal context.
658 If you want Viper search to behave as usual in Vi, set this variable to a
659 negative number."
660 :type 'boolean
661 :group 'viper-search)
663 (defcustom viper-re-query-replace t
664 "*If t then do regexp replace, if nil then do string replace."
665 :type 'boolean
666 :tag "Regexp Query Replace"
667 :group 'viper-search)
669 (defcustom viper-re-replace t
670 "*If t, do regexp replace. nil means do string replace."
671 :type 'boolean
672 :tag "Regexp Replace"
673 :group 'viper-search)
675 (defcustom viper-parse-sexp-ignore-comments t
676 "*If t, `%' ignores the parentheses that occur inside comments."
677 :type 'boolean
678 :group 'viper)
680 (viper-deflocalvar viper-ex-style-motion t "")
681 (defcustom viper-ex-style-motion t
682 "*If t, the commands l,h do not cross lines, etc (Ex-style).
683 If nil, these commands cross line boundaries."
684 :type 'boolean
685 :group 'viper)
687 (viper-deflocalvar viper-ex-style-editing t "")
688 (defcustom viper-ex-style-editing t
689 "*If t, Ex-style behavior while editing in Vi command and insert states.
690 `Backspace' and `Delete' don't cross line boundaries in insert.
691 `X' and `x' can't delete characters across line boundary in Vi, etc.
692 Note: this doesn't preclude `Backspace' and `Delete' from deleting characters
693 by moving past the insertion point. This is a feature, not a bug.
695 If nil, the above commands can work across lines."
696 :type 'boolean
697 :group 'viper)
699 (viper-deflocalvar viper-ESC-moves-cursor-back viper-ex-style-editing "")
700 (defcustom viper-ESC-moves-cursor-back nil
701 "*If t, ESC moves cursor back when changing from insert to vi state.
702 If nil, the cursor stays where it was when ESC was hit."
703 :type 'boolean
704 :group 'viper)
706 (viper-deflocalvar viper-delete-backwards-in-replace nil "")
707 (defcustom viper-delete-backwards-in-replace nil
708 "*If t, DEL key will delete characters while moving the cursor backwards.
709 If nil, the cursor will move backwards without deleting anything."
710 :type 'boolean
711 :group 'viper)
713 (defcustom viper-buffer-search-char nil
714 "*Key used for buffer-searching. Must be a character type, e.g., ?g."
715 :type '(choice (const nil) character)
716 :group 'viper-search)
718 (defcustom viper-search-wrap-around-t t
719 "*If t, search wraps around."
720 :type 'boolean
721 :tag "Search Wraps Around"
722 :group 'viper-search)
724 (viper-deflocalvar viper-related-files-and-buffers-ring nil "")
725 (defcustom viper-related-files-and-buffers-ring nil
726 "*List of file and buffer names that are considered to be related to the current buffer.
727 Related buffers can be cycled through via :R and :P commands."
728 :type 'boolean
729 :group 'viper-misc)
730 (put 'viper-related-files-and-buffers-ring 'permanent-local t)
732 ;; Used to find out if we are done with searching the current buffer.
733 (viper-deflocalvar viper-local-search-start-marker nil)
734 ;; As above, but global
735 (defvar viper-search-start-marker (make-marker))
737 ;; the search overlay
738 (viper-deflocalvar viper-search-overlay nil)
741 (defvar viper-heading-start
742 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
743 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
744 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
745 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
746 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
747 "^.+:-") ; prolog
748 "*Regexps for Headings. Used by \[\[ and \]\].")
750 (defvar viper-heading-end
751 (concat "^}\\|" ; C/C++
752 "^\\\\end{\\|" ; latex
753 "^@end \\|" ; texinfo
754 ")\n\n[ \t\n]*\\|" ; lisp
755 "\\.\\s-*$") ; prolog
756 "*Regexps to end Headings/Sections. Used by \[\].")
759 ;; These two vars control the interaction of jumps performed by ' and `.
760 ;; In this new version, '' doesn't erase the marks set by ``, so one can
761 ;; use both kinds of jumps interchangeably and without loosing positions
762 ;; inside the lines.
764 ;; Remembers position of the last jump done using ``'.
765 (viper-deflocalvar viper-last-jump nil)
766 ;; Remembers position of the last jump done using `''.
767 (viper-deflocalvar viper-last-jump-ignore 0)
769 ;; History variables
771 ;; History of search strings.
772 (defvar viper-search-history (list ""))
773 ;; History of query-replace strings used as a source.
774 (defvar viper-replace1-history nil)
775 ;; History of query-replace strings used as replacement.
776 (defvar viper-replace2-history nil)
777 ;; History of region quoting strings.
778 (defvar viper-quote-region-history (list viper-quote-string))
779 ;; History of Ex-style commands.
780 (defvar viper-ex-history nil)
781 ;; History of shell commands.
782 (defvar viper-shell-history nil)
785 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
786 ;; and one for Vi.
788 ;; Last shell command executed with ! command.
789 (defvar viper-last-shell-com nil)
792 ;;; Face-saving tricks
794 ;;(defcustom viper-replace-overlay-pixmap "gray3"
795 ;; "Pixmap to use for search face on non-color displays."
796 ;; :type 'string
797 ;; :group 'viper)
798 ;;(defcustom viper-search-face-pixmap "gray3"
799 ;; "Pixmap to use for search face on non-color displays."
800 ;; :type 'string
801 ;; :group 'viper)
803 (defun viper-hide-face (face)
804 (if (and (viper-has-face-support-p) viper-emacs-p)
805 (add-to-list 'facemenu-unlisted-faces face)))
808 (defgroup viper-highlighting nil
809 "Hilighting of replace region, search pattern, minibuffer, etc."
810 :prefix "viper-"
811 :group 'viper)
813 ;;(defvar viper-search-face
814 ;; (if (viper-has-face-support-p)
815 ;; (progn
816 ;; (make-face 'viper-search-face)
817 ;; (or (face-differs-from-default-p 'viper-search-face)
818 ;; ;; face wasn't set in .viper or .Xdefaults
819 ;; (if (viper-can-use-colors "Black" "khaki")
820 ;; (progn
821 ;; (set-face-background 'viper-search-face "khaki")
822 ;; (set-face-foreground 'viper-search-face "Black"))
823 ;; (set-face-underline-p 'viper-search-face t)
824 ;; (viper-set-face-pixmap 'viper-search-face
825 ;; viper-search-face-pixmap)))
826 ;; 'viper-search-face))
827 ;; "*Face used to flash out the search pattern.")
829 (defface viper-search-face
830 '((((class color)) (:foreground "Black" :background "khaki"))
831 (t (:underline t :stipple "gray3")))
832 "*Face used to flash out the search pattern."
833 :group 'viper-highlighting)
834 ;; An internal variable. Viper takes the face from here.
835 (defvar viper-search-face 'viper-search-face
836 "Face used to flash out the search pattern.
837 DO NOT CHANGE this variable. Instead, use the customization widget
838 to customize the actual face object `viper-search-face'
839 this variable represents.")
840 (viper-hide-face 'viper-search-face)
842 ;;(defvar viper-replace-overlay-face
843 ;; (if (viper-has-face-support-p)
844 ;; (progn
845 ;; (make-face 'viper-replace-overlay-face)
846 ;; (or (face-differs-from-default-p 'viper-replace-overlay-face)
847 ;; (progn
848 ;; (if (viper-can-use-colors "darkseagreen2" "Black")
849 ;; (progn
850 ;; (set-face-background
851 ;; 'viper-replace-overlay-face "darkseagreen2")
852 ;; (set-face-foreground 'viper-replace-overlay-face "Black")))
853 ;; (set-face-underline-p 'viper-replace-overlay-face t)
854 ;; (viper-set-face-pixmap
855 ;; 'viper-replace-overlay-face viper-replace-overlay-pixmap)))
856 ;; 'viper-replace-overlay-face))
857 ;; "*Face for highlighting replace regions on a window display.")
859 (defface viper-replace-overlay-face
860 '((((class color)) (:foreground "Black" :background "darkseagreen2"))
861 (t (:underline t :stipple "gray3")))
862 "*Face for highlighting replace regions on a window display."
863 :group 'viper-highlighting)
864 ;; An internal variable. Viper takes the face from here.
865 (defvar viper-replace-overlay-face 'viper-replace-overlay-face
866 "Face for highlighting replace regions on a window display.
867 DO NOT CHANGE this variable. Instead, use the customization widget
868 to customize the actual face object `viper-replace-overlay-face'
869 this variable represents.")
870 (viper-hide-face 'viper-replace-overlay-face)
872 ;;(defvar viper-minibuffer-emacs-face
873 ;; (if (viper-has-face-support-p)
874 ;; (progn
875 ;; (make-face 'viper-minibuffer-emacs-face)
876 ;; (or (face-differs-from-default-p 'viper-minibuffer-emacs-face)
877 ;; ;; face wasn't set in .viper or .Xdefaults
878 ;; (if viper-vi-style-in-minibuffer
879 ;; ;; emacs state is an exception in the minibuffer
880 ;; (if (viper-can-use-colors "darkseagreen2" "Black")
881 ;; (progn
882 ;; (set-face-background
883 ;; 'viper-minibuffer-emacs-face "darkseagreen2")
884 ;; (set-face-foreground
885 ;; 'viper-minibuffer-emacs-face "Black"))
886 ;; (copy-face 'modeline 'viper-minibuffer-emacs-face))
887 ;; ;; emacs state is the main state in the minibuffer
888 ;; (if (viper-can-use-colors "Black" "pink")
889 ;; (progn
890 ;; (set-face-background 'viper-minibuffer-emacs-face "pink")
891 ;; (set-face-foreground
892 ;; 'viper-minibuffer-emacs-face "Black"))
893 ;; (copy-face 'italic 'viper-minibuffer-emacs-face))
894 ;; ))
895 ;; 'viper-minibuffer-emacs-face))
896 ;; "Face used in the Minibuffer when it is in Emacs state.")
898 (defface viper-minibuffer-emacs-face
899 '((((class color)) (:foreground "Black" :background "darkseagreen2"))
900 (t (:bold t)))
901 "Face used in the Minibuffer when it is in Emacs state."
902 :group 'viper-highlighting)
903 ;; An internal variable. Viper takes the face from here.
904 (defvar viper-minibuffer-emacs-face 'viper-minibuffer-emacs-face
905 "Face used in the Minibuffer when it is in Emacs state.
906 DO NOT CHANGE this variable. Instead, use the customization widget
907 to customize the actual face object `viper-minibuffer-emacs-face'
908 this variable represents.")
909 (viper-hide-face 'viper-minibuffer-emacs-face)
911 ;;(defvar viper-minibuffer-insert-face
912 ;; (if (viper-has-face-support-p)
913 ;; (progn
914 ;; (make-face 'viper-minibuffer-insert-face)
915 ;; (or (face-differs-from-default-p 'viper-minibuffer-insert-face)
916 ;; (if viper-vi-style-in-minibuffer
917 ;; (if (viper-can-use-colors "Black" "pink")
918 ;; (progn
919 ;; (set-face-background 'viper-minibuffer-insert-face "pink")
920 ;; (set-face-foreground
921 ;; 'viper-minibuffer-insert-face "Black"))
922 ;; (copy-face 'italic 'viper-minibuffer-insert-face))
923 ;; ;; If Insert state is an exception
924 ;; (if (viper-can-use-colors "darkseagreen2" "Black")
925 ;; (progn
926 ;; (set-face-background
927 ;; 'viper-minibuffer-insert-face "darkseagreen2")
928 ;; (set-face-foreground
929 ;; 'viper-minibuffer-insert-face "Black"))
930 ;; (copy-face 'modeline 'viper-minibuffer-insert-face))
931 ;; (viper-italicize-face 'viper-minibuffer-insert-face)))
932 ;; 'viper-minibuffer-insert-face))
933 ;; "Face used in the Minibuffer when it is in Insert state.")
935 (defface viper-minibuffer-insert-face
936 '((((class color)) (:foreground "Black" :background "pink"))
937 (t (:italic t)))
938 "Face used in the Minibuffer when it is in Insert state."
939 :group 'viper-highlighting)
940 ;; An internal variable. Viper takes the face from here.
941 (defvar viper-minibuffer-insert-face 'viper-minibuffer-insert-face
942 "Face used in the Minibuffer when it is in Insert state.
943 DO NOT CHANGE this variable. Instead, use the customization widget
944 to customize the actual face object `viper-minibuffer-insert-face'
945 this variable represents.")
946 (viper-hide-face 'viper-minibuffer-insert-face)
948 ;;(defvar viper-minibuffer-vi-face
949 ;; (if (viper-has-face-support-p)
950 ;; (progn
951 ;; (make-face 'viper-minibuffer-vi-face)
952 ;; (or (face-differs-from-default-p 'viper-minibuffer-vi-face)
953 ;; (if viper-vi-style-in-minibuffer
954 ;; (if (viper-can-use-colors "Black" "grey")
955 ;; (progn
956 ;; (set-face-background 'viper-minibuffer-vi-face "grey")
957 ;; (set-face-foreground 'viper-minibuffer-vi-face "Black"))
958 ;; (copy-face 'bold 'viper-minibuffer-vi-face))
959 ;; (copy-face 'bold 'viper-minibuffer-vi-face)
960 ;; (invert-face 'viper-minibuffer-vi-face)))
961 ;; 'viper-minibuffer-vi-face))
962 ;; "Face used in the Minibuffer when it is in Vi state.")
964 (defface viper-minibuffer-vi-face
965 '((((class color)) (:foreground "DarkGreen" :background "grey"))
966 (t (:inverse-video t)))
967 "Face used in the Minibuffer when it is in Vi state."
968 :group 'viper-highlighting)
969 ;; An internal variable. Viper takes the face from here.
970 (defvar viper-minibuffer-vi-face 'viper-minibuffer-vi-face
971 "Face used in the Minibuffer when it is in Vi state.
972 DO NOT CHANGE this variable. Instead, use the customization widget
973 to customize the actual face object `viper-minibuffer-vi-face'
974 this variable represents.")
975 (viper-hide-face 'viper-minibuffer-vi-face)
977 ;; the current face to be used in the minibuffer
978 (viper-deflocalvar
979 viper-minibuffer-current-face viper-minibuffer-emacs-face "")
982 ;;; Miscellaneous
984 (defvar viper-inhibit-startup-message nil
985 "Whether Viper startup message should be inhibited.")
987 (defcustom viper-spell-function 'ispell-region
988 "Spell function used by #s<move> command to spell."
989 :type 'function
990 :group 'viper-misc)
992 (defcustom viper-tags-file-name "TAGS"
993 "The tags file used by Viper."
994 :type 'string
995 :group 'viper-misc)
997 ;; Minibuffer
999 (defcustom viper-vi-style-in-minibuffer t
1000 "If t, use vi-style editing in minibuffer.
1001 Should be set in `~/.viper' file."
1002 :type 'boolean
1003 :group 'viper)
1005 ;; overlay used in the minibuffer to indicate which state it is in
1006 (viper-deflocalvar viper-minibuffer-overlay nil)
1008 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
1009 ;; Beginning with Emacs 19.26, the standard `minibuffer-exit-hook' is run
1010 ;; *after* exiting the minibuffer
1011 (defvar viper-minibuffer-exit-hook '(viper-minibuffer-trim-tail))
1014 ;; Mode line
1015 (defconst viper-vi-state-id "<V> "
1016 "Mode line tag identifying the Vi mode of Viper.")
1017 (defconst viper-emacs-state-id "<E> "
1018 "Mode line tag identifying the Emacs mode of Viper.")
1019 (defconst viper-insert-state-id "<I> "
1020 "Mode line tag identifying the Insert mode of Viper.")
1021 (defconst viper-replace-state-id "<R> "
1022 "Mode line tag identifying the Replace mode of Viper.")
1025 (defgroup viper-hooks nil
1026 "Viper hooks."
1027 :prefix "viper-"
1028 :group 'viper)
1030 (defcustom viper-vi-state-hook nil
1031 "*Hooks run just before the switch to Vi mode is completed."
1032 :type 'hook
1033 :group 'viper-hooks)
1034 (defcustom viper-insert-state-hook nil
1035 "*Hooks run just before the switch to Insert mode is completed."
1036 :type 'hook
1037 :group 'viper-hooks)
1038 (defcustom viper-replace-state-hook nil
1039 "*Hooks run just before the switch to Replace mode is completed."
1040 :type 'hook
1041 :group 'viper-hooks)
1042 (defcustom viper-emacs-state-hook nil
1043 "*Hooks run just before the switch to Emacs mode is completed."
1044 :type 'hook
1045 :group 'viper-hooks)
1047 (defcustom viper-load-hook nil
1048 "Hooks run just after loading Viper."
1049 :type 'hook
1050 :group 'viper-hooks)
1053 ;;; Local Variables:
1054 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
1055 ;;; End:
1057 ;;; viper-ex.el ends here