Instructions to create pretest or release tarballs.
[emacs.git] / lisp / emulation / viper-init.el
blob8f0b99f29727b8138342f07d2b3a7bc3b3ce742f
1 ;;; viper-init.el --- some common definitions for Viper
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
7 ;; This file is part of GNU Emacs.
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.
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.
19 ;; You should have received a copy of the GNU General Public License
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.
24 ;;; Commentary:
26 ;;; Code:
28 (provide 'viper-init)
30 ;; compiler pacifier
31 (defvar mark-even-if-inactive)
32 (defvar quail-mode)
33 (defvar iso-accents-mode)
34 (defvar viper-current-state)
35 (defvar viper-version)
36 (defvar viper-expert-level)
37 (defvar current-input-method)
38 (defvar default-input-method)
39 (defvar describe-current-input-method-function)
40 ;; end pacifier
43 ;; Viper version
44 (defun viper-version ()
45 (interactive)
46 (message "Viper version is %s" viper-version))
48 ;; Is it XEmacs?
49 (defconst viper-xemacs-p (string-match "XEmacs" emacs-version))
50 ;; Is it Emacs?
51 (defconst viper-emacs-p (not viper-xemacs-p))
52 ;; Tell whether we are running as a window application or on a TTY
53 (defsubst viper-device-type ()
54 (if viper-emacs-p
55 window-system
56 (device-type (selected-device))))
57 ;; in XEmacs: device-type is tty on tty and stream in batch.
58 (defun viper-window-display-p ()
59 (and (viper-device-type) (not (memq (viper-device-type) '(tty stream pc)))))
61 (defcustom viper-ms-style-os-p (memq system-type
62 '(ms-dos windows-nt windows-95))
63 "Tells if Emacs is running under an MS-style OS: ms-dos, windows-nt, W95."
64 :type 'boolean
65 :tag "Is it Microsoft-made OS?"
66 :group 'viper-misc)
67 (defcustom viper-vms-os-p (memq system-type '(vax-vms axp-vms))
68 "Tells if Emacs is running under VMS."
69 :type 'boolean
70 :tag "Is it VMS?"
71 :group 'viper-misc)
73 (defcustom viper-force-faces nil
74 "If t, Viper will think that it is running on a display that supports faces.
75 This is provided as a temporary relief for users of graphics-capable terminals
76 that Viper doesn't know about.
77 In all likelihood, you don't need to bother with this setting."
78 :type 'boolean
79 :group 'viper-highlighting)
81 (defun viper-has-face-support-p ()
82 (cond ((viper-window-display-p))
83 (viper-force-faces)
84 (viper-emacs-p (memq (viper-device-type) '(pc)))
85 (viper-xemacs-p (memq (viper-device-type) '(tty pc)))))
88 ;;; Macros
90 (defmacro viper-deflocalvar (var default-value &optional documentation)
91 `(progn
92 (defvar ,var ,default-value
93 ,(format "%s\n\(buffer local\)" documentation))
94 (make-variable-buffer-local ',var)))
96 ;; (viper-loop COUNT BODY) Execute BODY COUNT times.
97 (defmacro viper-loop (count &rest body)
98 `(let ((count ,count))
99 (while (> count 0)
100 ,@body
101 (setq count (1- count)))))
103 (defmacro viper-buffer-live-p (buf)
104 `(and ,buf (get-buffer ,buf) (buffer-name (get-buffer ,buf))))
106 ;; return buffer-specific macro definition, given a full macro definition
107 (defmacro viper-kbd-buf-alist (macro-elt)
108 `(nth 1 ,macro-elt))
109 ;; get a pair: (curr-buffer . macro-definition)
110 (defmacro viper-kbd-buf-pair (macro-elt)
111 `(assoc (buffer-name) (viper-kbd-buf-alist ,macro-elt)))
112 ;; get macro definition for current buffer
113 (defmacro viper-kbd-buf-definition (macro-elt)
114 `(cdr (viper-kbd-buf-pair ,macro-elt)))
116 ;; return mode-specific macro definitions, given a full macro definition
117 (defmacro viper-kbd-mode-alist (macro-elt)
118 `(nth 2 ,macro-elt))
119 ;; get a pair: (major-mode . macro-definition)
120 (defmacro viper-kbd-mode-pair (macro-elt)
121 `(assoc major-mode (viper-kbd-mode-alist ,macro-elt)))
122 ;; get macro definition for the current major mode
123 (defmacro viper-kbd-mode-definition (macro-elt)
124 `(cdr (viper-kbd-mode-pair ,macro-elt)))
126 ;; return global macro definition, given a full macro definition
127 (defmacro viper-kbd-global-pair (macro-elt)
128 `(nth 3 ,macro-elt))
129 ;; get global macro definition from an elt of macro-alist
130 (defmacro viper-kbd-global-definition (macro-elt)
131 `(cdr (viper-kbd-global-pair ,macro-elt)))
133 ;; last elt of a sequence
134 (defsubst viper-seq-last-elt (seq)
135 (elt seq (1- (length seq))))
137 (defsubst viper-string-to-list (string)
138 (append (vconcat string) nil))
140 (defsubst viper-charlist-to-string (list)
141 (mapconcat 'char-to-string list ""))
143 ;; like char-after/before, but saves typing
144 (defun viper-char-at-pos (direction &optional offset)
145 (or (integerp offset) (setq offset 0))
146 (if (eq direction 'forward)
147 (char-after (+ (point) offset))
148 (char-before (- (point) offset))))
151 (defvar viper-minibuffer-overlay-priority 300)
152 (defvar viper-replace-overlay-priority 400)
153 (defvar viper-search-overlay-priority 500)
156 ;;; Viper minor modes
158 ;; Mode for vital things like \e, C-z.
159 (viper-deflocalvar viper-vi-intercept-minor-mode nil)
161 (viper-deflocalvar viper-vi-basic-minor-mode nil
162 "Viper's minor mode for Vi bindings.")
164 (viper-deflocalvar viper-vi-local-user-minor-mode nil
165 "Auxiliary minor mode for user-defined local bindings in Vi state.")
167 (viper-deflocalvar viper-vi-global-user-minor-mode nil
168 "Auxiliary minor mode for user-defined global bindings in Vi state.")
170 (viper-deflocalvar viper-vi-state-modifier-minor-mode nil
171 "Minor mode used to make major-mode-specific modification to Vi state.")
173 (viper-deflocalvar viper-vi-diehard-minor-mode nil
174 "This minor mode is in effect when the user wants Viper to be Vi.")
176 (viper-deflocalvar viper-vi-kbd-minor-mode nil
177 "Minor mode for Ex command macros in Vi state.
178 The corresponding keymap stores key bindings of Vi macros defined with
179 the Ex command :map.")
181 ;; Mode for vital things like \e, C-z.
182 (viper-deflocalvar viper-insert-intercept-minor-mode nil)
184 (viper-deflocalvar viper-insert-basic-minor-mode nil
185 "Viper's minor mode for bindings in Insert mode.")
187 (viper-deflocalvar viper-insert-local-user-minor-mode nil
188 "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
189 This is a way to overshadow normal Insert mode bindings locally to certain
190 designated buffers.")
192 (viper-deflocalvar viper-insert-global-user-minor-mode nil
193 "Auxiliary minor mode for global user-defined bindings in Insert state.")
195 (viper-deflocalvar viper-insert-state-modifier-minor-mode nil
196 "Minor mode used to make major-mode-specific modification to Insert state.")
198 (viper-deflocalvar viper-insert-diehard-minor-mode nil
199 "Minor mode that simulates Vi very closely.
200 Not recommened, except for the novice user.")
202 (viper-deflocalvar viper-insert-kbd-minor-mode nil
203 "Minor mode for Ex command macros Insert state.
204 The corresponding keymap stores key bindings of Vi macros defined with
205 the Ex command :map!.")
207 (viper-deflocalvar viper-replace-minor-mode nil
208 "Minor mode in effect in replace state (cw, C, and the like commands).")
210 ;; Mode for vital things like \C-z and \C-x) This is set to t, when viper-mode
211 ;; is invoked. So, any new buffer will have C-z defined as switch to Vi,
212 ;; unless we switched states in this buffer
213 (viper-deflocalvar viper-emacs-intercept-minor-mode nil)
215 (viper-deflocalvar viper-emacs-local-user-minor-mode nil
216 "Minor mode for local user bindings effective in Emacs state.
217 Users can use it to override Emacs bindings when Viper is in its Emacs
218 state.")
220 (viper-deflocalvar viper-emacs-global-user-minor-mode nil
221 "Minor mode for global user bindings in effect in Emacs state.
222 Users can use it to override Emacs bindings when Viper is in its Emacs
223 state.")
225 (viper-deflocalvar viper-emacs-kbd-minor-mode nil
226 "Minor mode for Vi style macros in Emacs state.
227 The corresponding keymap stores key bindings of Vi macros defined with
228 `viper-record-kbd-macro' command. There is no Ex-level command to do this
229 interactively.")
231 (viper-deflocalvar viper-emacs-state-modifier-minor-mode nil
232 "Minor mode used to make major-mode-specific modification to Emacs state.
233 For instance, a Vi purist may want to bind `dd' in Dired mode to a function
234 that deletes a file.")
236 (viper-deflocalvar viper-vi-minibuffer-minor-mode nil
237 "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
239 (viper-deflocalvar viper-insert-minibuffer-minor-mode nil
240 "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
244 ;; Some common error messages
246 (defconst viper-SpuriousText "Spurious text after command" "")
247 (defconst viper-BadExCommand "Not an editor command" "")
248 (defconst viper-InvalidCommandArgument "Invalid command argument" "")
249 (defconst viper-NoPrevSearch "No previous search string" "")
250 (defconst viper-EmptyRegister "`%c': Nothing in this register" "")
251 (defconst viper-InvalidRegister "`%c': Invalid register" "")
252 (defconst viper-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
253 (defconst viper-InvalidTextmarker "`%c': Invalid text marker" "")
254 (defconst viper-InvalidViCommand "Invalid command" "")
255 (defconst viper-BadAddress "Ill-formed address" "")
256 (defconst viper-FirstAddrExceedsSecond "First address exceeds second" "")
257 (defconst viper-NoFileSpecified "No file specified" "")
259 ;; Is t until viper-mode executes for the very first time.
260 ;; Prevents recursive descend into startup messages.
261 (defvar viper-first-time t)
263 (defvar viper-expert-level (if (boundp 'viper-expert-level) viper-expert-level 0)
264 "User's expert level.
265 The minor mode viper-vi-diehard-minor-mode is in effect when
266 viper-expert-level is 1 or 2 or when viper-want-emacs-keys-in-vi is t.
267 The minor mode viper-insert-diehard-minor-mode is in effect when
268 viper-expert-level is 1 or 2 or if viper-want-emacs-keys-in-insert is t.
269 Use `M-x viper-set-expert-level' to change this.")
271 ;; Max expert level supported by Viper. This is NOT a user option.
272 ;; It is here to make it hard for the user from resetting it.
273 (defconst viper-max-expert-level 5)
276 ;;; ISO characters and MULE
278 ;; If non-nil, ISO accents will be turned on in insert/replace emacs states and
279 ;; turned off in vi-state. For some users, this behavior may be too
280 ;; primitive. In this case, use insert/emacs/vi state hooks.
281 (viper-deflocalvar viper-automatic-iso-accents nil "")
282 ;; Set iso-accents-mode to ARG. Check if it is bound first
283 (defsubst viper-set-iso-accents-mode (arg)
284 (if (boundp 'iso-accents-mode)
285 (setq iso-accents-mode arg)))
287 ;; Internal flag used to control when viper mule hooks are run.
288 ;; Don't change this!
289 (defvar viper-mule-hook-flag t)
290 ;; If non-nil, the default intl. input method is turned on.
291 (viper-deflocalvar viper-special-input-method nil "")
293 ;; viper hook to run on input-method activation
294 (defun viper-activate-input-method-action ()
295 (if (null viper-mule-hook-flag)
297 (setq viper-special-input-method t)
298 ;; turn off special input methods in vi-state
299 (if (eq viper-current-state 'vi-state)
300 (viper-set-input-method nil))
301 (if (memq viper-current-state '(vi-state insert-state replace-state))
302 (message "Viper special input method%s: on"
303 (if (or current-input-method default-input-method)
304 (format " %S"
305 (or current-input-method default-input-method))
306 "")))
309 ;; viper hook to run on input-method deactivation
310 (defun viper-inactivate-input-method-action ()
311 (if (null viper-mule-hook-flag)
313 (setq viper-special-input-method nil)
314 (if (memq viper-current-state '(vi-state insert-state replace-state))
315 (message "Viper special input method%s: off"
316 (if (or current-input-method default-input-method)
317 (format " %S"
318 (or current-input-method default-input-method))
319 "")))))
321 (defun viper-inactivate-input-method ()
322 (cond ((and viper-emacs-p (fboundp 'inactivate-input-method))
323 (inactivate-input-method))
324 ((and viper-xemacs-p (boundp 'current-input-method))
325 ;; XEmacs had broken quil-mode for some time, so we are working around
326 ;; it here
327 (setq quail-mode nil)
328 (if (featurep 'quail)
329 (quail-delete-overlays))
330 (setq describe-current-input-method-function nil)
331 (setq current-input-method nil)
332 (run-hooks 'input-method-inactivate-hook)
333 (force-mode-line-update))
335 (defun viper-activate-input-method ()
336 (cond ((and viper-emacs-p (fboundp 'activate-input-method))
337 (activate-input-method default-input-method))
338 ((and viper-xemacs-p (fboundp 'quail-mode))
339 (quail-mode 1))))
341 ;; Set quail-mode to ARG
342 (defun viper-set-input-method (arg)
343 (setq viper-mule-hook-flag t) ; just a precaution
344 (let (viper-mule-hook-flag) ; temporarily inactivate viper mule hooks
345 (cond ((and arg (> (prefix-numeric-value arg) 0) default-input-method)
346 ;; activate input method
347 (viper-activate-input-method))
348 (t ; deactivate input method
349 (viper-inactivate-input-method)))
353 ;; VI-style Undo
355 ;; Used to 'undo' complex commands, such as replace and insert commands.
356 (viper-deflocalvar viper-undo-needs-adjustment nil)
357 (put 'viper-undo-needs-adjustment 'permanent-local t)
359 ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
360 ;; complex command that must be undone atomically. If inserted, it is
361 ;; erased by viper-change-state-to-vi and viper-repeat.
362 (defconst viper-buffer-undo-list-mark 'viper)
364 (defcustom viper-keep-point-on-undo nil
365 "*Non-nil means not to move point while undoing commands.
366 This style is different from Emacs and Vi. Try it to see if
367 it better fits your working style."
368 :type 'boolean
369 :tag "Preserve Position of Point After Undo"
370 :group 'viper)
372 ;; Replace mode and changing text
374 ;; Hack used to pass global states around for short period of time
375 (viper-deflocalvar viper-intermediate-command nil "")
377 ;; This is used to pass the right Vi command key sequence to
378 ;; viper-set-destructive-command whenever (this-command-keys) doesn't give the
379 ;; right result. For instance, in commands like c/bla<RET>,
380 ;; (this-command-keys) will return ^M, which invoked exit-minibuffer, while we
381 ;; need "c/"
382 (defconst viper-this-command-keys nil)
384 ;; Indicates that the current destructive command has started in replace mode.
385 (viper-deflocalvar viper-began-as-replace nil "")
387 (defcustom viper-allow-multiline-replace-regions t
388 "If non-nil, Viper will allow multi-line replace regions.
389 This is an extension to standard Vi.
390 If nil, commands that attempt to replace text spanning multiple lines first
391 delete the text being replaced, as in standard Vi."
392 :type 'boolean
393 :group 'viper)
395 (defcustom viper-replace-overlay-cursor-color "Red"
396 "*Cursor color when Viper is in Replace state."
397 :type 'string
398 :group 'viper)
399 (defcustom viper-insert-state-cursor-color "Green"
400 "Cursor color when Viper is in insert state."
401 :type 'string
402 :group 'viper)
404 ;; internal var, used to remember the default cursor color of emacs frames
405 (defvar viper-vi-state-cursor-color nil)
407 (viper-deflocalvar viper-replace-overlay nil "")
408 (put 'viper-replace-overlay 'permanent-local t)
410 (defcustom viper-replace-region-end-delimiter "$"
411 "A string marking the end of replacement regions.
412 It is used only with TTYs or if `viper-use-replace-region-delimiters'
413 is non-nil."
414 :type 'string
415 :group 'viper)
416 (defcustom viper-replace-region-start-delimiter ""
417 "A string marking the beginning of replacement regions.
418 It is used only with TTYs or if `viper-use-replace-region-delimiters'
419 is non-nil."
420 :type 'string
421 :group 'viper)
422 (defcustom viper-use-replace-region-delimiters
423 (or (not (viper-has-face-support-p))
424 (and viper-xemacs-p (eq (viper-device-type) 'tty)))
425 "*If non-nil, Viper will always use `viper-replace-region-end-delimiter' and
426 `viper-replace-region-start-delimiter' to delimit replacement regions, even on
427 color displays. By default, the delimiters are used only on TTYs."
428 :type 'boolean
429 :group 'viper)
431 (defcustom viper-read-buffer-function 'read-buffer
432 "Function to use for prompting the user for a buffer name."
433 :type 'symbol
434 :group 'viper)
436 ;; XEmacs requires glyphs
437 (if viper-xemacs-p
438 (progn
439 (or (glyphp viper-replace-region-end-delimiter)
440 (setq viper-replace-region-end-delimiter
441 (make-glyph viper-replace-region-end-delimiter)))
442 (or (glyphp viper-replace-region-start-delimiter)
443 (setq viper-replace-region-start-delimiter
444 (make-glyph viper-replace-region-start-delimiter)))
448 ;; These are local marker that must be initialized to nil and moved with
449 ;; `viper-move-marker-locally'
451 ;; Remember the last position inside the replace region.
452 (viper-deflocalvar viper-last-posn-in-replace-region nil)
453 ;; Remember the last position while inserting
454 (viper-deflocalvar viper-last-posn-while-in-insert-state nil)
455 (put 'viper-last-posn-in-replace-region 'permanent-local t)
456 (put 'viper-last-posn-while-in-insert-state 'permanent-local t)
458 (viper-deflocalvar viper-sitting-in-replace nil "")
459 (put 'viper-sitting-in-replace 'permanent-local t)
461 ;; Remember the number of characters that have to be deleted in replace
462 ;; mode to compensate for the inserted characters.
463 (viper-deflocalvar viper-replace-chars-to-delete 0 "")
464 ;; This variable is used internally by the before/after changed functions to
465 ;; determine how many chars were deleted by the change. This can't be
466 ;; determined inside after-change-functions because those get the length of the
467 ;; deleted region, not the number of chars deleted (which are two different
468 ;; things under MULE).
469 (viper-deflocalvar viper-replace-region-chars-deleted 0 "")
471 ;; Insertion ring and command ring
472 (defcustom viper-insertion-ring-size 14
473 "The size of history of inserted text.
474 This is a list where Viper keeps the history of previously inserted pieces of
475 text."
476 :type 'integer
477 :group 'viper-misc)
478 ;; The insertion ring.
479 (defvar viper-insertion-ring nil)
480 ;; This is temp insertion ring. Used to do rotation for display purposes.
481 ;; When rotation just started, it is initialized to viper-insertion-ring.
482 (defvar viper-temp-insertion-ring nil)
483 (defvar viper-last-inserted-string-from-insertion-ring "")
485 (defcustom viper-command-ring-size 14
486 "The size of history of Vi commands repeatable with dot."
487 :type 'integer
488 :group 'viper-misc)
489 ;; The command ring.
490 (defvar viper-command-ring nil)
491 ;; This is temp command ring. Used to do rotation for display purposes.
492 ;; When rotation just started, it is initialized to viper-command-ring.
493 (defvar viper-temp-command-ring nil)
495 ;; Fast keyseq and ESC keyseq timeouts
496 (defcustom viper-fast-keyseq-timeout 200
497 "*Key sequence separated by no more than this many milliseconds is viewed as a Vi-style macro, if such a macro is defined.
498 Setting this too high may slow down your typing. Setting this value too low
499 will make it hard to use Vi-style timeout macros."
500 :type 'integer
501 :group 'viper-misc)
503 (defcustom viper-ESC-keyseq-timeout (if (viper-window-display-p)
504 0 viper-fast-keyseq-timeout)
505 "*Key sequence beginning with ESC and separated by no more than this many milliseconds is considered to be generated by a keyboard function key.
506 Setting this too high may slow down switching from insert to vi state. Setting
507 this value too low will make it impossible to use function keys in insert mode
508 on a dumb terminal."
509 :type 'integer
510 :group 'viper-misc)
512 (defcustom viper-translate-all-ESC-keysequences (not (viper-window-display-p))
513 "Allow translation of all key sequences into commands.
514 Normally, Viper lets Emacs translate only those ESC key sequences that are
515 defined in the low-level key-translation-map or function-key-map, such as those
516 emitted by the arrow and function keys. Other sequences, e.g., \\e/, are
517 treated as ESC command followed by a `/'. This is done for people who type fast
518 and tend to hit other characters right after they hit ESC. Other people like
519 Emacs to translate ESC sequences all the time.
520 The default is to translate all sequences only when using a dumb terminal.
521 This permits you to use ESC as a meta key in insert mode."
522 :type 'boolean
523 :group 'viper-misc)
525 ;; Modes and related variables
527 ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
528 (viper-deflocalvar viper-current-state 'emacs-state)
531 ;; Autoindent in insert
533 ;; Variable that keeps track of whether C-t has been pressed.
534 (viper-deflocalvar viper-cted nil "")
536 ;; Preserve the indent value, used by C-d in insert mode.
537 (viper-deflocalvar viper-current-indent 0)
539 ;; Whether to preserve the indent, used by C-d in insert mode.
540 (viper-deflocalvar viper-preserve-indent nil)
542 (viper-deflocalvar viper-auto-indent nil "")
543 (defcustom viper-auto-indent nil
544 "*Enable autoindent, if t.
545 This is a buffer-local variable."
546 :type 'boolean
547 :group 'viper)
549 (viper-deflocalvar viper-electric-mode t "")
550 (defcustom viper-electric-mode t
551 "*If t, electrify Viper.
552 Currently, this only electrifies auto-indentation, making it appropriate to the
553 mode of the buffer.
554 This means that auto-indentation will depart from standard Vi and will indent
555 appropriate to the mode of the buffer. This is especially useful for editing
556 programs and LaTeX documents."
557 :type 'boolean
558 :group 'viper)
560 (defcustom viper-shift-width 8
561 "*The value of the shiftwidth.
562 This determines the number of columns by which the Ctl-t moves the cursor in
563 the Insert state."
564 :type 'integer
565 :group 'viper)
567 ;; Variables for repeating destructive commands
569 (defcustom viper-keep-point-on-repeat t
570 "*If t, don't move point when repeating previous command.
571 This is useful for doing repeated changes with the '.' key.
572 The user can change this to nil, if she likes when the cursor moves
573 to a new place after repeating previous Vi command."
574 :type 'boolean
575 :group 'viper)
577 ;; Remember insert point as a marker. This is a local marker that must be
578 ;; initialized to nil and moved with `viper-move-marker-locally'.
579 (viper-deflocalvar viper-insert-point nil)
580 (put 'viper-insert-point 'permanent-local t)
582 ;; This remembers the point before dabbrev-expand was called.
583 ;; If viper-insert-point turns out to be bigger than that, it is reset
584 ;; back to viper-pre-command-point.
585 ;; The reason this is needed is because dabbrev-expand (and possibly
586 ;; others) may jump to before the insertion point, delete something and
587 ;; then reinsert a bigger piece. For instance: bla^blo
588 ;; If dabbrev-expand is called after `blo' and ^ undicates viper-insert-point,
589 ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
590 ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
591 ;; will insert the expansion, and we get: blablo^
592 ;; Whatever we insert next goes before the ^, i.e., before the
593 ;; viper-insert-point marker. So, Viper will think that nothing was
594 ;; inserted. Remembering the orig position of the marker circumvents the
595 ;; problem.
596 ;; We don't know of any command, except dabbrev-expand, that has the same
597 ;; problem. However, the same trick can be used if such a command is
598 ;; discovered later.
600 (viper-deflocalvar viper-pre-command-point nil)
601 (put 'viper-pre-command-point 'permanent-local t) ; this is probably an overkill
603 ;; This is used for saving inserted text.
604 (defvar viper-last-insertion nil)
606 ;; Remembers the last replaced region.
607 (defvar viper-last-replace-region "")
609 ;; Remember com point as a marker.
610 ;; This is a local marker. Should be moved with `viper-move-marker-locally'
611 (viper-deflocalvar viper-com-point nil)
613 ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
614 ;; It is used to re-execute last destructive command.
615 ;; M-COM is a Lisp symbol representing the function to be executed.
616 ;; VAL is the prefix argument that was used with that command.
617 ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
618 ;; additional information on how the function in M-COM is to be handled.
619 ;; REG is the register used by command
620 ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
621 ;; commands).
622 ;; COMMAND-KEYS are the keys that were typed to invoke the command.
623 (defvar viper-d-com nil)
625 ;; The character remembered by the Vi `r' command.
626 (defvar viper-d-char nil)
628 ;; Name of register to store deleted or yanked strings
629 (defvar viper-use-register nil)
632 ;;; Variables for Moves and Searches
634 (defgroup viper-search nil
635 "Variables that define the search and query-replace behavior of Viper."
636 :prefix "viper-"
637 :group 'viper)
639 ;; For use by `;' command.
640 (defvar viper-f-char nil)
642 ;; For use by `.' command.
643 (defvar viper-F-char nil)
645 ;; For use by `;' command.
646 (defvar viper-f-forward nil)
648 ;; For use by `;' command.
649 (defvar viper-f-offset nil)
651 ;; Last search string
652 (defvar viper-s-string "")
654 (defcustom viper-quote-string "> "
655 "String inserted at the beginning of quoted region."
656 :type 'string
657 :group 'viper)
659 ;; If t, search is forward.
660 (defvar viper-s-forward nil)
662 (defcustom viper-case-fold-search nil
663 "*If not nil, search ignores cases."
664 :type 'boolean
665 :group 'viper-search)
667 (defcustom viper-re-search t
668 "*If not nil, search is regexp search, otherwise vanilla search."
669 :type 'boolean
670 :tag "Regexp Search"
671 :group 'viper-search)
673 (defcustom viper-search-scroll-threshold 2
674 "*If search lands within this threshnold from the window top/bottom,
675 the window will be scrolled up or down appropriately, to reveal context.
676 If you want Viper search to behave as usual in Vi, set this variable to a
677 negative number."
678 :type 'boolean
679 :group 'viper-search)
681 (defcustom viper-re-query-replace t
682 "*If t then do regexp replace, if nil then do string replace."
683 :type 'boolean
684 :tag "Regexp Query Replace"
685 :group 'viper-search)
687 (defcustom viper-re-replace t
688 "*If t, do regexp replace. nil means do string replace."
689 :type 'boolean
690 :tag "Regexp Replace"
691 :group 'viper-search)
693 (defcustom viper-parse-sexp-ignore-comments t
694 "*If t, `%' ignores the parentheses that occur inside comments."
695 :type 'boolean
696 :group 'viper)
698 (viper-deflocalvar viper-ex-style-motion t "")
699 (defcustom viper-ex-style-motion t
700 "*If t, the commands l,h do not cross lines, etc (Ex-style).
701 If nil, these commands cross line boundaries."
702 :type 'boolean
703 :group 'viper)
705 (viper-deflocalvar viper-ex-style-editing t "")
706 (defcustom viper-ex-style-editing t
707 "*If t, Ex-style behavior while editing in Vi command and insert states.
708 `Backspace' and `Delete' don't cross line boundaries in insert.
709 `X' and `x' can't delete characters across line boundary in Vi, etc.
710 Note: this doesn't preclude `Backspace' and `Delete' from deleting characters
711 by moving past the insertion point. This is a feature, not a bug.
713 If nil, the above commands can work across lines."
714 :type 'boolean
715 :group 'viper)
717 (viper-deflocalvar viper-ESC-moves-cursor-back viper-ex-style-editing "")
718 (defcustom viper-ESC-moves-cursor-back nil
719 "*If t, ESC moves cursor back when changing from insert to vi state.
720 If nil, the cursor stays where it was when ESC was hit."
721 :type 'boolean
722 :group 'viper)
724 (viper-deflocalvar viper-delete-backwards-in-replace nil "")
725 (defcustom viper-delete-backwards-in-replace nil
726 "*If t, DEL key will delete characters while moving the cursor backwards.
727 If nil, the cursor will move backwards without deleting anything."
728 :type 'boolean
729 :group 'viper)
731 (defcustom viper-buffer-search-char nil
732 "*Key used for buffer-searching. Must be a character type, e.g., ?g."
733 :type '(choice (const nil) character)
734 :group 'viper-search)
736 (defcustom viper-search-wrap-around-t t
737 "*If t, search wraps around."
738 :type 'boolean
739 :tag "Search Wraps Around"
740 :group 'viper-search)
742 (viper-deflocalvar viper-related-files-and-buffers-ring nil "")
743 (defcustom viper-related-files-and-buffers-ring nil
744 "*List of file and buffer names that are considered to be related to the current buffer.
745 Related buffers can be cycled through via :R and :P commands."
746 :type 'boolean
747 :group 'viper-misc)
748 (put 'viper-related-files-and-buffers-ring 'permanent-local t)
750 ;; Used to find out if we are done with searching the current buffer.
751 (viper-deflocalvar viper-local-search-start-marker nil)
752 ;; As above, but global
753 (defvar viper-search-start-marker (make-marker))
755 ;; the search overlay
756 (viper-deflocalvar viper-search-overlay nil)
759 (defvar viper-heading-start
760 (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
761 "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
762 "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
763 "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
764 "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
765 "^.+:-") ; prolog
766 "*Regexps for Headings. Used by \[\[ and \]\].")
768 (defvar viper-heading-end
769 (concat "^}\\|" ; C/C++
770 "^\\\\end{\\|" ; latex
771 "^@end \\|" ; texinfo
772 ")\n\n[ \t\n]*\\|" ; lisp
773 "\\.\\s-*$") ; prolog
774 "*Regexps to end Headings/Sections. Used by \[\].")
777 ;; These two vars control the interaction of jumps performed by ' and `.
778 ;; In this new version, '' doesn't erase the marks set by ``, so one can
779 ;; use both kinds of jumps interchangeably and without loosing positions
780 ;; inside the lines.
782 ;; Remembers position of the last jump done using ``'.
783 (viper-deflocalvar viper-last-jump nil)
784 ;; Remembers position of the last jump done using `''.
785 (viper-deflocalvar viper-last-jump-ignore 0)
787 ;; History variables
789 ;; History of search strings.
790 (defvar viper-search-history (list ""))
791 ;; History of query-replace strings used as a source.
792 (defvar viper-replace1-history nil)
793 ;; History of query-replace strings used as replacement.
794 (defvar viper-replace2-history nil)
795 ;; History of region quoting strings.
796 (defvar viper-quote-region-history (list viper-quote-string))
797 ;; History of Ex-style commands.
798 (defvar viper-ex-history nil)
799 ;; History of shell commands.
800 (defvar viper-shell-history nil)
803 ;; Last shell command. There are two of these, one for Ex (in viper-ex)
804 ;; and one for Vi.
806 ;; Last shell command executed with ! command.
807 (defvar viper-last-shell-com nil)
810 ;;; Face-saving tricks
812 (defun viper-hide-face (face)
813 (if (and (viper-has-face-support-p) viper-emacs-p)
814 (add-to-list 'facemenu-unlisted-faces face)))
817 (defgroup viper-highlighting nil
818 "Hilighting of replace region, search pattern, minibuffer, etc."
819 :prefix "viper-"
820 :group 'viper)
823 (defface viper-search-face
824 '((((class color)) (:foreground "Black" :background "khaki"))
825 (t (:underline t :stipple "gray3")))
826 "*Face used to flash out the search pattern."
827 :group 'viper-highlighting)
828 ;; An internal variable. Viper takes the face from here.
829 (defvar viper-search-face 'viper-search-face
830 "Face used to flash out the search pattern.
831 DO NOT CHANGE this variable. Instead, use the customization widget
832 to customize the actual face object `viper-search-face'
833 this variable represents.")
834 (viper-hide-face 'viper-search-face)
837 (defface viper-replace-overlay-face
838 '((((class color)) (:foreground "Black" :background "darkseagreen2"))
839 (t (:underline t :stipple "gray3")))
840 "*Face for highlighting replace regions on a window display."
841 :group 'viper-highlighting)
842 ;; An internal variable. Viper takes the face from here.
843 (defvar viper-replace-overlay-face 'viper-replace-overlay-face
844 "Face for highlighting replace regions on a window display.
845 DO NOT CHANGE this variable. Instead, use the customization widget
846 to customize the actual face object `viper-replace-overlay-face'
847 this variable represents.")
848 (viper-hide-face 'viper-replace-overlay-face)
851 (defface viper-minibuffer-emacs-face
852 '((((class color)) (:foreground "Black" :background "darkseagreen2"))
853 (t (:bold t)))
854 "Face used in the Minibuffer when it is in Emacs state."
855 :group 'viper-highlighting)
856 ;; An internal variable. Viper takes the face from here.
857 (defvar viper-minibuffer-emacs-face 'viper-minibuffer-emacs-face
858 "Face used in the Minibuffer when it is in Emacs state.
859 DO NOT CHANGE this variable. Instead, use the customization widget
860 to customize the actual face object `viper-minibuffer-emacs-face'
861 this variable represents.")
862 (viper-hide-face 'viper-minibuffer-emacs-face)
865 (defface viper-minibuffer-insert-face
866 '((((class color)) (:foreground "Black" :background "pink"))
867 (t (:italic t)))
868 "Face used in the Minibuffer when it is in Insert state."
869 :group 'viper-highlighting)
870 ;; An internal variable. Viper takes the face from here.
871 (defvar viper-minibuffer-insert-face 'viper-minibuffer-insert-face
872 "Face used in the Minibuffer when it is in Insert state.
873 DO NOT CHANGE this variable. Instead, use the customization widget
874 to customize the actual face object `viper-minibuffer-insert-face'
875 this variable represents.")
876 (viper-hide-face 'viper-minibuffer-insert-face)
879 (defface viper-minibuffer-vi-face
880 '((((class color)) (:foreground "DarkGreen" :background "grey"))
881 (t (:inverse-video t)))
882 "Face used in the Minibuffer when it is in Vi state."
883 :group 'viper-highlighting)
884 ;; An internal variable. Viper takes the face from here.
885 (defvar viper-minibuffer-vi-face 'viper-minibuffer-vi-face
886 "Face used in the Minibuffer when it is in Vi state.
887 DO NOT CHANGE this variable. Instead, use the customization widget
888 to customize the actual face object `viper-minibuffer-vi-face'
889 this variable represents.")
890 (viper-hide-face 'viper-minibuffer-vi-face)
892 ;; the current face to be used in the minibuffer
893 (viper-deflocalvar
894 viper-minibuffer-current-face viper-minibuffer-emacs-face "")
897 ;;; Miscellaneous
899 (defvar viper-inhibit-startup-message nil
900 "Whether Viper startup message should be inhibited.")
902 (defcustom viper-spell-function 'ispell-region
903 "Spell function used by #s<move> command to spell."
904 :type 'function
905 :group 'viper-misc)
907 (defcustom viper-tags-file-name "TAGS"
908 "The tags file used by Viper."
909 :type 'string
910 :group 'viper-misc)
912 (defcustom viper-change-notification-threshold 1
913 "Notify the user when this many lines or characters have been deleted/yanked.
914 For line-deleting/yanking commands (like `dd', `yy'), the value denotes the
915 number of lines. For character-based commands (such as `x', `dw', etc.), the
916 value refers to the number of characters affected."
917 :type 'integer
918 :group 'viper-misc)
920 ;; Minibuffer
922 (defcustom viper-vi-style-in-minibuffer t
923 "If t, use vi-style editing in minibuffer.
924 Should be set in `~/.viper' file."
925 :type 'boolean
926 :group 'viper)
928 ;; overlay used in the minibuffer to indicate which state it is in
929 (viper-deflocalvar viper-minibuffer-overlay nil)
930 (put 'viper-minibuffer-overlay 'permanent-local t)
932 ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
933 ;; This is needed because beginning with Emacs 19.26, the standard
934 ;; `minibuffer-exit-hook' is run *after* exiting the minibuffer
935 (defvar viper-minibuffer-exit-hook nil)
938 ;; Mode line
939 (defconst viper-vi-state-id "<V> "
940 "Mode line tag identifying the Vi mode of Viper.")
941 (defconst viper-emacs-state-id "<E> "
942 "Mode line tag identifying the Emacs mode of Viper.")
943 (defconst viper-insert-state-id "<I> "
944 "Mode line tag identifying the Insert mode of Viper.")
945 (defconst viper-replace-state-id "<R> "
946 "Mode line tag identifying the Replace mode of Viper.")
949 (defgroup viper-hooks nil
950 "Viper hooks."
951 :prefix "viper-"
952 :group 'viper)
954 (defcustom viper-vi-state-hook 'viper-restore-cursor-type
955 "*Hooks run just before the switch to Vi mode is completed."
956 :type 'hook
957 :group 'viper-hooks)
958 (defcustom viper-insert-state-hook 'viper-set-insert-cursor-type
959 "*Hooks run just before the switch to Insert mode is completed."
960 :type 'hook
961 :group 'viper-hooks)
962 (defcustom viper-replace-state-hook 'viper-restore-cursor-type
963 "*Hooks run just before the switch to Replace mode is completed."
964 :type 'hook
965 :group 'viper-hooks)
966 (defcustom viper-emacs-state-hook 'viper-restore-cursor-type
967 "*Hooks run just before the switch to Emacs mode is completed."
968 :type 'hook
969 :group 'viper-hooks)
971 (defcustom viper-load-hook nil
972 "Hooks run just after loading Viper."
973 :type 'hook
974 :group 'viper-hooks)
976 (defun viper-restore-cursor-type ()
977 (condition-case nil
978 (if viper-xemacs-p
979 (setq bar-cursor nil)
980 (setq cursor-type default-cursor-type))
981 (error)))
983 (defun viper-set-insert-cursor-type ()
984 (if viper-xemacs-p
985 (setq bar-cursor 2)
986 (setq cursor-type '(bar . 2))))
989 ;;; Local Variables:
990 ;;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
991 ;;; End:
993 ;;; viper-init.el ends here