viper-init.el 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. ;;; viper-init.el --- some common definitions for Viper
  2. ;; Copyright (C) 1997-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Package: viper
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; compiler pacifier
  19. (defvar mark-even-if-inactive)
  20. (defvar quail-mode)
  21. (defvar iso-accents-mode)
  22. (defvar viper-current-state)
  23. (defvar viper-version)
  24. (defvar viper-expert-level)
  25. (defvar current-input-method)
  26. (defvar default-input-method)
  27. (defvar describe-current-input-method-function)
  28. (defvar bar-cursor)
  29. (defvar cursor-type)
  30. ;; end pacifier
  31. ;; Viper version
  32. (defun viper-version ()
  33. (interactive)
  34. (message "Viper version is %s" viper-version))
  35. ;; Tell whether we are running as a window application or on a TTY
  36. (defsubst viper-device-type ()
  37. (if (featurep 'xemacs)
  38. (device-type (selected-device))
  39. window-system))
  40. (defun viper-color-display-p ()
  41. (condition-case nil
  42. (if (featurep 'xemacs)
  43. (eq (device-class (selected-device)) 'color)
  44. (display-color-p))
  45. (error nil)))
  46. ;; in XEmacs: device-type is tty on tty and stream in batch.
  47. (defun viper-window-display-p ()
  48. (and (viper-device-type) (not (memq (viper-device-type) '(tty stream pc)))))
  49. (defcustom viper-ms-style-os-p
  50. (memq system-type (if (featurep 'emacs) '(ms-dos windows-nt)
  51. '(ms-dos windows-nt windows-95)))
  52. "Non-nil if Emacs is running under an MS-style OS: MS-DOS, or MS-Windows."
  53. :type 'boolean
  54. :tag "Is it Microsoft-made OS?"
  55. :group 'viper-misc)
  56. (defcustom viper-suppress-input-method-change-message nil
  57. "If t, the message notifying about changes in the input method is not displayed.
  58. Normally, a message is displayed each time on enters the vi, insert or replace
  59. state."
  60. :type 'boolean
  61. :group 'viper-misc)
  62. (defcustom viper-force-faces nil
  63. "If t, Viper will think that it is running on a display that supports faces.
  64. This is provided as a temporary relief for users of graphics-capable terminals
  65. that Viper doesn't know about.
  66. In all likelihood, you don't need to bother with this setting."
  67. :type 'boolean
  68. :group 'viper-highlighting)
  69. (defun viper-has-face-support-p ()
  70. (cond ((viper-window-display-p))
  71. (viper-force-faces)
  72. ((viper-color-display-p))
  73. ((featurep 'emacs) (memq (viper-device-type) '(pc)))
  74. ((featurep 'xemacs) (memq (viper-device-type) '(tty pc)))))
  75. ;;; Macros
  76. (defmacro viper-deflocalvar (var default-value &optional documentation)
  77. `(progn
  78. (defvar ,var ,default-value
  79. ,(format "%s\n\(buffer local\)" documentation))
  80. (make-variable-buffer-local ',var)))
  81. ;; (viper-loop COUNT BODY) Execute BODY COUNT times.
  82. (defmacro viper-loop (count &rest body)
  83. `(let ((count ,count))
  84. (while (> count 0)
  85. ,@body
  86. (setq count (1- count)))))
  87. (defmacro viper-buffer-live-p (buf)
  88. `(and ,buf (get-buffer ,buf) (buffer-name (get-buffer ,buf))))
  89. ;; return buffer-specific macro definition, given a full macro definition
  90. (defmacro viper-kbd-buf-alist (macro-elt)
  91. `(nth 1 ,macro-elt))
  92. ;; get a pair: (curr-buffer . macro-definition)
  93. (defmacro viper-kbd-buf-pair (macro-elt)
  94. `(assoc (buffer-name) (viper-kbd-buf-alist ,macro-elt)))
  95. ;; get macro definition for current buffer
  96. (defmacro viper-kbd-buf-definition (macro-elt)
  97. `(cdr (viper-kbd-buf-pair ,macro-elt)))
  98. ;; return mode-specific macro definitions, given a full macro definition
  99. (defmacro viper-kbd-mode-alist (macro-elt)
  100. `(nth 2 ,macro-elt))
  101. ;; get a pair: (major-mode . macro-definition)
  102. (defmacro viper-kbd-mode-pair (macro-elt)
  103. `(assoc major-mode (viper-kbd-mode-alist ,macro-elt)))
  104. ;; get macro definition for the current major mode
  105. (defmacro viper-kbd-mode-definition (macro-elt)
  106. `(cdr (viper-kbd-mode-pair ,macro-elt)))
  107. ;; return global macro definition, given a full macro definition
  108. (defmacro viper-kbd-global-pair (macro-elt)
  109. `(nth 3 ,macro-elt))
  110. ;; get global macro definition from an elt of macro-alist
  111. (defmacro viper-kbd-global-definition (macro-elt)
  112. `(cdr (viper-kbd-global-pair ,macro-elt)))
  113. ;; last elt of a sequence
  114. (defsubst viper-seq-last-elt (seq)
  115. (elt seq (1- (length seq))))
  116. (defsubst viper-string-to-list (string)
  117. (append (vconcat string) nil))
  118. (defsubst viper-charlist-to-string (list)
  119. (mapconcat 'char-to-string list ""))
  120. ;; like char-after/before, but saves typing
  121. (defun viper-char-at-pos (direction &optional offset)
  122. (or (integerp offset) (setq offset 0))
  123. (if (eq direction 'forward)
  124. (char-after (+ (point) offset))
  125. (char-before (- (point) offset))))
  126. (defvar viper-minibuffer-overlay-priority 300)
  127. (defvar viper-replace-overlay-priority 400)
  128. (defvar viper-search-overlay-priority 500)
  129. ;;; Viper minor modes
  130. ;; Mode for vital things like \e, C-z.
  131. (viper-deflocalvar viper-vi-intercept-minor-mode nil)
  132. (viper-deflocalvar viper-vi-basic-minor-mode nil
  133. "Viper's minor mode for Vi bindings.")
  134. (viper-deflocalvar viper-vi-local-user-minor-mode nil
  135. "Auxiliary minor mode for user-defined local bindings in Vi state.")
  136. (viper-deflocalvar viper-vi-global-user-minor-mode nil
  137. "Auxiliary minor mode for user-defined global bindings in Vi state.")
  138. (viper-deflocalvar viper-vi-state-modifier-minor-mode nil
  139. "Minor mode used to make major-mode-specific modification to Vi state.")
  140. (viper-deflocalvar viper-vi-diehard-minor-mode nil
  141. "This minor mode is in effect when the user wants Viper to be Vi.")
  142. (viper-deflocalvar viper-vi-kbd-minor-mode nil
  143. "Minor mode for Ex command macros in Vi state.
  144. The corresponding keymap stores key bindings of Vi macros defined with
  145. the Ex command :map.")
  146. ;; Mode for vital things like \e, C-z.
  147. (viper-deflocalvar viper-insert-intercept-minor-mode nil)
  148. (viper-deflocalvar viper-insert-basic-minor-mode nil
  149. "Viper's minor mode for bindings in Insert mode.")
  150. (viper-deflocalvar viper-insert-local-user-minor-mode nil
  151. "Auxiliary minor mode for buffer-local user-defined bindings in Insert state.
  152. This is a way to overshadow normal Insert mode bindings locally to certain
  153. designated buffers.")
  154. (viper-deflocalvar viper-insert-global-user-minor-mode nil
  155. "Auxiliary minor mode for global user-defined bindings in Insert state.")
  156. (viper-deflocalvar viper-insert-state-modifier-minor-mode nil
  157. "Minor mode used to make major-mode-specific modification to Insert state.")
  158. (viper-deflocalvar viper-insert-diehard-minor-mode nil
  159. "Minor mode that simulates Vi very closely.
  160. Not recommended, except for the novice user.")
  161. (viper-deflocalvar viper-insert-kbd-minor-mode nil
  162. "Minor mode for Ex command macros Insert state.
  163. The corresponding keymap stores key bindings of Vi macros defined with
  164. the Ex command :map!.")
  165. (viper-deflocalvar viper-replace-minor-mode nil
  166. "Minor mode in effect in replace state (cw, C, and the like commands).")
  167. ;; Mode for vital things like \C-z and \C-x) This is set to t, when viper-mode
  168. ;; is invoked. So, any new buffer will have C-z defined as switch to Vi,
  169. ;; unless we switched states in this buffer
  170. (viper-deflocalvar viper-emacs-intercept-minor-mode nil)
  171. (viper-deflocalvar viper-emacs-local-user-minor-mode nil
  172. "Minor mode for local user bindings effective in Emacs state.
  173. Users can use it to override Emacs bindings when Viper is in its Emacs
  174. state.")
  175. (viper-deflocalvar viper-emacs-global-user-minor-mode nil
  176. "Minor mode for global user bindings in effect in Emacs state.
  177. Users can use it to override Emacs bindings when Viper is in its Emacs
  178. state.")
  179. (viper-deflocalvar viper-emacs-kbd-minor-mode nil
  180. "Minor mode for Vi style macros in Emacs state.
  181. The corresponding keymap stores key bindings of Vi macros defined with
  182. `viper-record-kbd-macro' command. There is no Ex-level command to do this
  183. interactively.")
  184. (viper-deflocalvar viper-emacs-state-modifier-minor-mode nil
  185. "Minor mode used to make major-mode-specific modification to Emacs state.
  186. For instance, a Vi purist may want to bind `dd' in Dired mode to a function
  187. that deletes a file.")
  188. (viper-deflocalvar viper-vi-minibuffer-minor-mode nil
  189. "Minor mode that forces Vi-style when the Minibuffer is in Vi state.")
  190. (viper-deflocalvar viper-insert-minibuffer-minor-mode nil
  191. "Minor mode that forces Vi-style when the Minibuffer is in Insert state.")
  192. ;; Some common error messages
  193. (defconst viper-SpuriousText "Spurious text after command" "")
  194. (defconst viper-BadExCommand "Not an editor command" "")
  195. (defconst viper-InvalidCommandArgument "Invalid command argument" "")
  196. (defconst viper-NoPrevSearch "No previous search string" "")
  197. (defconst viper-EmptyRegister "`%c': Nothing in this register" "")
  198. (defconst viper-InvalidRegister "`%c': Invalid register" "")
  199. (defconst viper-EmptyTextmarker "`%c': Text marker doesn't point anywhere" "")
  200. (defconst viper-InvalidTextmarker "`%c': Invalid text marker" "")
  201. (defconst viper-InvalidViCommand "Invalid command" "")
  202. (defconst viper-BadAddress "Ill-formed address" "")
  203. (defconst viper-FirstAddrExceedsSecond "First address exceeds second" "")
  204. (defconst viper-NoFileSpecified "No file specified" "")
  205. ;; Is t until viper-mode executes for the very first time.
  206. ;; Prevents recursive descend into startup messages.
  207. (defvar viper-first-time t)
  208. (defvar viper-expert-level (if (boundp 'viper-expert-level) viper-expert-level 0)
  209. "User's expert level.
  210. The minor mode viper-vi-diehard-minor-mode is in effect when
  211. viper-expert-level is 1 or 2 or when viper-want-emacs-keys-in-vi is t.
  212. The minor mode viper-insert-diehard-minor-mode is in effect when
  213. viper-expert-level is 1 or 2 or if viper-want-emacs-keys-in-insert is t.
  214. Use `M-x viper-set-expert-level' to change this.")
  215. ;; Max expert level supported by Viper. This is NOT a user option.
  216. ;; It is here to make it hard for the user from resetting it.
  217. (defconst viper-max-expert-level 5)
  218. ;;; ISO characters and MULE
  219. ;; If non-nil, ISO accents will be turned on in insert/replace emacs states and
  220. ;; turned off in vi-state. For some users, this behavior may be too
  221. ;; primitive. In this case, use insert/emacs/vi state hooks.
  222. (viper-deflocalvar viper-automatic-iso-accents nil "")
  223. ;; Set iso-accents-mode to ARG. Check if it is bound first
  224. (defsubst viper-set-iso-accents-mode (arg)
  225. (if (boundp 'iso-accents-mode)
  226. (setq iso-accents-mode arg)))
  227. ;; Internal flag used to control when viper mule hooks are run.
  228. ;; Don't change this!
  229. (defvar viper-mule-hook-flag t)
  230. ;; If non-nil, the default intl. input method is turned on.
  231. (viper-deflocalvar viper-special-input-method nil "")
  232. ;; viper hook to run on input-method activation
  233. (defun viper-activate-input-method-action ()
  234. (if (null viper-mule-hook-flag)
  235. ()
  236. (setq viper-special-input-method t)
  237. ;; turn off special input methods in vi-state
  238. (if (eq viper-current-state 'vi-state)
  239. (viper-set-input-method nil))
  240. (if (and (memq viper-current-state '(vi-state insert-state replace-state))
  241. (not viper-suppress-input-method-change-message))
  242. (message "Viper special input method%s: on"
  243. (if (or current-input-method default-input-method)
  244. (format " %S"
  245. (or current-input-method default-input-method))
  246. "")))
  247. ))
  248. ;; viper hook to run on input-method deactivation
  249. (defun viper-inactivate-input-method-action ()
  250. (if (null viper-mule-hook-flag)
  251. ()
  252. (setq viper-special-input-method nil)
  253. (if (and (memq viper-current-state '(vi-state insert-state replace-state))
  254. (not viper-suppress-input-method-change-message))
  255. (message "Viper special input method%s: off"
  256. (if (or current-input-method default-input-method)
  257. (format " %S"
  258. (or current-input-method default-input-method))
  259. "")))))
  260. (defun viper-inactivate-input-method ()
  261. (cond ((and (featurep 'emacs) (fboundp 'inactivate-input-method))
  262. (inactivate-input-method))
  263. ((and (featurep 'xemacs) (boundp 'current-input-method))
  264. ;; XEmacs had broken quail-mode for some time, so we are working around
  265. ;; it here
  266. (setq quail-mode nil)
  267. (if (featurep 'quail)
  268. (quail-delete-overlays))
  269. (setq describe-current-input-method-function nil)
  270. (setq current-input-method nil)
  271. (run-hooks 'input-method-inactivate-hook)
  272. (force-mode-line-update))
  273. ))
  274. (defun viper-activate-input-method ()
  275. (cond ((and (featurep 'emacs) (fboundp 'activate-input-method))
  276. (activate-input-method default-input-method))
  277. ((featurep 'xemacs)
  278. (if (fboundp 'quail-mode) (quail-mode 1)))))
  279. ;; Set quail-mode to ARG
  280. (defun viper-set-input-method (arg)
  281. (setq viper-mule-hook-flag t) ; just a precaution
  282. (let (viper-mule-hook-flag) ; temporarily deactivate viper mule hooks
  283. (cond ((and arg (> (prefix-numeric-value arg) 0) default-input-method)
  284. ;; activate input method
  285. (viper-activate-input-method))
  286. (t ; deactivate input method
  287. (viper-inactivate-input-method)))
  288. ))
  289. ;; VI-style Undo
  290. ;; Used to 'undo' complex commands, such as replace and insert commands.
  291. (viper-deflocalvar viper-undo-needs-adjustment nil)
  292. (put 'viper-undo-needs-adjustment 'permanent-local t)
  293. ;; A mark that Viper puts on buffer-undo-list. Marks the beginning of a
  294. ;; complex command that must be undone atomically. If inserted, it is
  295. ;; erased by viper-change-state-to-vi and viper-repeat.
  296. (defconst viper-buffer-undo-list-mark 'viper)
  297. (defcustom viper-keep-point-on-undo nil
  298. "*Non-nil means not to move point while undoing commands.
  299. This style is different from Emacs and Vi. Try it to see if
  300. it better fits your working style."
  301. :type 'boolean
  302. :tag "Preserve Position of Point After Undo"
  303. :group 'viper)
  304. ;; Replace mode and changing text
  305. ;; Hack used to pass global states around for short period of time
  306. (viper-deflocalvar viper-intermediate-command nil "")
  307. ;; This is used to pass the right Vi command key sequence to
  308. ;; viper-set-destructive-command whenever (this-command-keys) doesn't give the
  309. ;; right result. For instance, in commands like c/bla<RET>,
  310. ;; (this-command-keys) will return ^M, which invoked exit-minibuffer, while we
  311. ;; need "c/"
  312. (defconst viper-this-command-keys nil)
  313. ;; Indicates that the current destructive command has started in replace mode.
  314. (viper-deflocalvar viper-began-as-replace nil "")
  315. (defcustom viper-allow-multiline-replace-regions t
  316. "If non-nil, Viper will allow multi-line replace regions.
  317. This is an extension to standard Vi.
  318. If nil, commands that attempt to replace text spanning multiple lines first
  319. delete the text being replaced, as in standard Vi."
  320. :type 'boolean
  321. :group 'viper)
  322. (defcustom viper-replace-overlay-cursor-color "Red"
  323. "*Cursor color when Viper is in Replace state."
  324. :type 'string
  325. :group 'viper)
  326. (defcustom viper-insert-state-cursor-color "Green"
  327. "Cursor color when Viper is in insert state."
  328. :type 'string
  329. :group 'viper)
  330. ;; viper-emacs-state-cursor-color doesn't work well. Causes cursor colors to be
  331. ;; confused in some cases. So, this var is nulled for now.
  332. ;; (defcustom viper-emacs-state-cursor-color "Magenta"
  333. (defcustom viper-emacs-state-cursor-color nil
  334. "Cursor color when Viper is in Emacs state."
  335. :type 'string
  336. :group 'viper)
  337. ;; internal var, used to remember the default cursor color of emacs frames
  338. (defvar viper-vi-state-cursor-color nil)
  339. ;; Frame-local variables are obsolete from Emacs 22.2 onwards, so we
  340. ;; do it by hand with viper-frame-value (qv).
  341. (when (and (featurep 'xemacs)
  342. (fboundp 'make-variable-frame-local))
  343. (make-variable-frame-local 'viper-replace-overlay-cursor-color)
  344. (make-variable-frame-local 'viper-insert-state-cursor-color)
  345. (make-variable-frame-local 'viper-emacs-state-cursor-color)
  346. (make-variable-frame-local 'viper-vi-state-cursor-color))
  347. (viper-deflocalvar viper-replace-overlay nil "")
  348. (put 'viper-replace-overlay 'permanent-local t)
  349. (defcustom viper-replace-region-end-delimiter "$"
  350. "A string marking the end of replacement regions.
  351. It is used only with TTYs or if `viper-use-replace-region-delimiters'
  352. is non-nil."
  353. :type 'string
  354. :group 'viper)
  355. (defcustom viper-replace-region-start-delimiter ""
  356. "A string marking the beginning of replacement regions.
  357. It is used only with TTYs or if `viper-use-replace-region-delimiters'
  358. is non-nil."
  359. :type 'string
  360. :group 'viper)
  361. (defcustom viper-use-replace-region-delimiters
  362. (or (not (viper-has-face-support-p))
  363. (and (featurep 'xemacs) (eq (viper-device-type) 'tty)))
  364. "*If non-nil, Viper will always use `viper-replace-region-end-delimiter' and
  365. `viper-replace-region-start-delimiter' to delimit replacement regions, even on
  366. color displays. By default, the delimiters are used only on TTYs."
  367. :type 'boolean
  368. :group 'viper)
  369. (defcustom viper-read-buffer-function 'read-buffer
  370. "Function to use for prompting the user for a buffer name."
  371. :type 'symbol
  372. :group 'viper)
  373. ;; XEmacs requires glyphs
  374. (when (featurep 'xemacs)
  375. (or (glyphp viper-replace-region-end-delimiter)
  376. (setq viper-replace-region-end-delimiter
  377. (make-glyph viper-replace-region-end-delimiter)))
  378. (or (glyphp viper-replace-region-start-delimiter)
  379. (setq viper-replace-region-start-delimiter
  380. (make-glyph viper-replace-region-start-delimiter))))
  381. ;; These are local marker that must be initialized to nil and moved with
  382. ;; `viper-move-marker-locally'
  383. ;;
  384. ;; Remember the last position inside the replace region.
  385. (viper-deflocalvar viper-last-posn-in-replace-region nil)
  386. ;; Remember the last position while inserting
  387. (viper-deflocalvar viper-last-posn-while-in-insert-state nil)
  388. (put 'viper-last-posn-in-replace-region 'permanent-local t)
  389. (put 'viper-last-posn-while-in-insert-state 'permanent-local t)
  390. (viper-deflocalvar viper-sitting-in-replace nil "")
  391. (put 'viper-sitting-in-replace 'permanent-local t)
  392. ;; Remember the number of characters that have to be deleted in replace
  393. ;; mode to compensate for the inserted characters.
  394. (viper-deflocalvar viper-replace-chars-to-delete 0 "")
  395. ;; This variable is used internally by the before/after changed functions to
  396. ;; determine how many chars were deleted by the change. This can't be
  397. ;; determined inside after-change-functions because those get the length of the
  398. ;; deleted region, not the number of chars deleted (which are two different
  399. ;; things under MULE).
  400. (viper-deflocalvar viper-replace-region-chars-deleted 0 "")
  401. ;; Insertion ring and command ring
  402. (defcustom viper-insertion-ring-size 14
  403. "The size of history of inserted text.
  404. This is a list where Viper keeps the history of previously inserted pieces of
  405. text."
  406. :type 'integer
  407. :group 'viper-misc)
  408. ;; The insertion ring.
  409. (defvar viper-insertion-ring nil)
  410. ;; This is temp insertion ring. Used to do rotation for display purposes.
  411. ;; When rotation just started, it is initialized to viper-insertion-ring.
  412. (defvar viper-temp-insertion-ring nil)
  413. (defvar viper-last-inserted-string-from-insertion-ring "")
  414. (defcustom viper-command-ring-size 14
  415. "The size of history of Vi commands repeatable with dot."
  416. :type 'integer
  417. :group 'viper-misc)
  418. ;; The command ring.
  419. (defvar viper-command-ring nil)
  420. ;; This is temp command ring. Used to do rotation for display purposes.
  421. ;; When rotation just started, it is initialized to viper-command-ring.
  422. (defvar viper-temp-command-ring nil)
  423. ;; Fast keyseq and ESC keyseq timeouts
  424. (defcustom viper-fast-keyseq-timeout 200
  425. "*Key sequence separated by no more than this many milliseconds is viewed as a Vi-style macro, if such a macro is defined.
  426. Setting this too high may slow down your typing. Setting this value too low
  427. will make it hard to use Vi-style timeout macros."
  428. :type 'integer
  429. :group 'viper-misc)
  430. ;; This function determines if ESC key sequences are to be translated into
  431. ;; commands.
  432. (defun viper-translate-all-ESC-keysequences ()
  433. (not (viper-window-display-p)))
  434. ;; Modes and related variables
  435. ;; Current mode. One of: `emacs-state', `vi-state', `insert-state'
  436. (viper-deflocalvar viper-current-state 'emacs-state)
  437. ;; Autoindent in insert
  438. ;; Variable that keeps track of whether C-t has been pressed.
  439. (viper-deflocalvar viper-cted nil "")
  440. ;; Preserve the indent value, used by C-d in insert mode.
  441. (viper-deflocalvar viper-current-indent 0)
  442. ;; Whether to preserve the indent, used by C-d in insert mode.
  443. (viper-deflocalvar viper-preserve-indent nil)
  444. (viper-deflocalvar viper-auto-indent nil "")
  445. (defcustom viper-auto-indent nil
  446. "*Enable autoindent, if t.
  447. This is a buffer-local variable."
  448. :type 'boolean
  449. :group 'viper)
  450. (viper-deflocalvar viper-electric-mode t "")
  451. (defcustom viper-electric-mode t
  452. "*If t, electrify Viper.
  453. Currently, this only electrifies auto-indentation, making it appropriate to the
  454. mode of the buffer.
  455. This means that auto-indentation will depart from standard Vi and will indent
  456. appropriate to the mode of the buffer. This is especially useful for editing
  457. programs and LaTeX documents."
  458. :type 'boolean
  459. :group 'viper)
  460. (defcustom viper-shift-width 8
  461. "*The value of the shiftwidth.
  462. This determines the number of columns by which the Ctl-t moves the cursor in
  463. the Insert state."
  464. :type 'integer
  465. :group 'viper)
  466. ;; Variables for repeating destructive commands
  467. (defcustom viper-keep-point-on-repeat t
  468. "*If t, don't move point when repeating previous command.
  469. This is useful for doing repeated changes with the '.' key.
  470. The user can change this to nil, if she likes when the cursor moves
  471. to a new place after repeating previous Vi command."
  472. :type 'boolean
  473. :group 'viper)
  474. ;; Remember insert point as a marker. This is a local marker that must be
  475. ;; initialized to nil and moved with `viper-move-marker-locally'.
  476. (viper-deflocalvar viper-insert-point nil)
  477. (put 'viper-insert-point 'permanent-local t)
  478. ;; This remembers the point before dabbrev-expand was called.
  479. ;; If viper-insert-point turns out to be bigger than that, it is reset
  480. ;; back to viper-pre-command-point.
  481. ;; The reason this is needed is because dabbrev-expand (and possibly
  482. ;; others) may jump to before the insertion point, delete something and
  483. ;; then reinsert a bigger piece. For instance: bla^blo
  484. ;; If dabbrev-expand is called after `blo' and ^ indicates viper-insert-point,
  485. ;; then point jumps to the beginning of `blo'. If expansion is found, `blablo'
  486. ;; is deleted, and we have |^, where | denotes point. Next, dabbrev-expand
  487. ;; will insert the expansion, and we get: blablo^
  488. ;; Whatever we insert next goes before the ^, i.e., before the
  489. ;; viper-insert-point marker. So, Viper will think that nothing was
  490. ;; inserted. Remembering the orig position of the marker circumvents the
  491. ;; problem.
  492. ;; We don't know of any command, except dabbrev-expand, that has the same
  493. ;; problem. However, the same trick can be used if such a command is
  494. ;; discovered later.
  495. ;;
  496. (viper-deflocalvar viper-pre-command-point nil)
  497. (put 'viper-pre-command-point 'permanent-local t) ; this is probably an overkill
  498. ;; This is used for saving inserted text.
  499. (defvar viper-last-insertion nil)
  500. ;; Remembers the last replaced region.
  501. (defvar viper-last-replace-region "")
  502. ;; Remember com point as a marker.
  503. ;; This is a local marker. Should be moved with `viper-move-marker-locally'
  504. (viper-deflocalvar viper-com-point nil)
  505. ;; If non-nil, the value is a list (M-COM VAL COM REG inserted-text cmd-keys)
  506. ;; It is used to re-execute last destructive command.
  507. ;; M-COM is a Lisp symbol representing the function to be executed.
  508. ;; VAL is the prefix argument that was used with that command.
  509. ;; COM is an internal descriptor, such as ?r, ?c, ?C, which contains
  510. ;; additional information on how the function in M-COM is to be handled.
  511. ;; REG is the register used by command
  512. ;; INSERTED-TEXT is text inserted by that command (in case of o, c, C, i, r
  513. ;; commands).
  514. ;; COMMAND-KEYS are the keys that were typed to invoke the command.
  515. (defvar viper-d-com nil)
  516. ;; The character remembered by the Vi `r' command.
  517. (defvar viper-d-char nil)
  518. ;; Name of register to store deleted or yanked strings
  519. (defvar viper-use-register nil)
  520. ;;; Variables for Moves and Searches
  521. (defgroup viper-search nil
  522. "Variables that define the search and query-replace behavior of Viper."
  523. :prefix "viper-"
  524. :group 'viper)
  525. ;; For use by `;' command.
  526. (defvar viper-f-char nil)
  527. ;; For use by `.' command.
  528. (defvar viper-F-char nil)
  529. ;; For use by `;' command.
  530. (defvar viper-f-forward nil)
  531. ;; For use by `;' command.
  532. (defvar viper-f-offset nil)
  533. ;; Last search string
  534. (defvar viper-s-string "")
  535. (defcustom viper-quote-string "> "
  536. "String inserted at the beginning of quoted region."
  537. :type 'string
  538. :group 'viper)
  539. ;; If t, search is forward.
  540. (defvar viper-s-forward nil)
  541. (defcustom viper-case-fold-search nil
  542. "*If not nil, search ignores cases."
  543. :type 'boolean
  544. :group 'viper-search)
  545. (defcustom viper-re-search t
  546. "*If not nil, search is regexp search, otherwise vanilla search."
  547. :type 'boolean
  548. :tag "Regexp Search"
  549. :group 'viper-search)
  550. (defcustom viper-search-scroll-threshold 2
  551. "*If search lands within this threshold from the window top/bottom,
  552. the window will be scrolled up or down appropriately, to reveal context.
  553. If you want Viper search to behave as usual in Vi, set this variable to a
  554. negative number."
  555. :type 'boolean
  556. :group 'viper-search)
  557. (defcustom viper-re-query-replace t
  558. "*If t then do regexp replace, if nil then do string replace."
  559. :type 'boolean
  560. :tag "Regexp Query Replace"
  561. :group 'viper-search)
  562. (defcustom viper-re-replace t
  563. "*If t, do regexp replace. nil means do string replace."
  564. :type 'boolean
  565. :tag "Regexp Replace"
  566. :group 'viper-search)
  567. (defcustom viper-parse-sexp-ignore-comments t
  568. "*If t, `%' ignores the parentheses that occur inside comments."
  569. :type 'boolean
  570. :group 'viper)
  571. (viper-deflocalvar viper-ex-style-motion t "")
  572. (defcustom viper-ex-style-motion t
  573. "*If t, the commands l,h do not cross lines, etc (Ex-style).
  574. If nil, these commands cross line boundaries."
  575. :type 'boolean
  576. :group 'viper)
  577. (viper-deflocalvar viper-ex-style-editing t "")
  578. (defcustom viper-ex-style-editing t
  579. "*If t, Ex-style behavior while editing in Vi command and insert states.
  580. `Backspace' and `Delete' don't cross line boundaries in insert.
  581. `X' and `x' can't delete characters across line boundary in Vi, etc.
  582. Note: this doesn't preclude `Backspace' and `Delete' from deleting characters
  583. by moving past the insertion point. This is a feature, not a bug.
  584. If nil, the above commands can work across lines."
  585. :type 'boolean
  586. :group 'viper)
  587. (viper-deflocalvar viper-ESC-moves-cursor-back viper-ex-style-editing "")
  588. (defcustom viper-ESC-moves-cursor-back nil
  589. "*If t, ESC moves cursor back when changing from insert to vi state.
  590. If nil, the cursor stays where it was when ESC was hit."
  591. :type 'boolean
  592. :group 'viper)
  593. (viper-deflocalvar viper-delete-backwards-in-replace nil "")
  594. (defcustom viper-delete-backwards-in-replace nil
  595. "*If t, DEL key will delete characters while moving the cursor backwards.
  596. If nil, the cursor will move backwards without deleting anything."
  597. :type 'boolean
  598. :group 'viper)
  599. (defcustom viper-buffer-search-char nil
  600. "*Key used for buffer-searching. Must be a character type, e.g., ?g."
  601. :type '(choice (const nil) character)
  602. :group 'viper-search)
  603. (defcustom viper-search-wrap-around t
  604. "*If t, search wraps around."
  605. :type 'boolean
  606. :tag "Search Wraps Around"
  607. :group 'viper-search)
  608. (viper-deflocalvar viper-related-files-and-buffers-ring nil "")
  609. (defcustom viper-related-files-and-buffers-ring nil
  610. "*List of file and buffer names that are considered to be related to the current buffer.
  611. Related buffers can be cycled through via :R and :P commands."
  612. :type 'boolean
  613. :group 'viper-misc)
  614. (put 'viper-related-files-and-buffers-ring 'permanent-local t)
  615. ;; Used to find out if we are done with searching the current buffer.
  616. (viper-deflocalvar viper-local-search-start-marker nil)
  617. ;; As above, but global
  618. (defvar viper-search-start-marker (make-marker))
  619. ;; the search overlay
  620. (viper-deflocalvar viper-search-overlay nil)
  621. (defvar viper-heading-start
  622. (concat "^\\s-*(\\s-*defun\\s-\\|" ; lisp
  623. "^{\\s-*$\\|^[_a-zA-Z][^()]*[()].*{\\s-*$\\|" ; C/C++
  624. "^\\s-*class.*{\\|^\\s-*struct.*{\\|^\\s-*enum.*{\\|"
  625. "^\\\\[sb][a-z]*{.*}\\s-*$\\|" ; latex
  626. "^@node\\|@table\\|^@m?enu\\|^@itemize\\|^@if\\|" ; texinfo
  627. "^.+:-") ; prolog
  628. "*Regexps for Headings. Used by \[\[ and \]\].")
  629. (defvar viper-heading-end
  630. (concat "^}\\|" ; C/C++
  631. "^\\\\end{\\|" ; latex
  632. "^@end \\|" ; texinfo
  633. ")\n\n[ \t\n]*\\|" ; lisp
  634. "\\.\\s-*$") ; prolog
  635. "*Regexps to end Headings/Sections. Used by \[\].")
  636. ;; These two vars control the interaction of jumps performed by ' and `.
  637. ;; In this new version, '' doesn't erase the marks set by ``, so one can
  638. ;; use both kinds of jumps interchangeably and without losing positions
  639. ;; inside the lines.
  640. ;; Remembers position of the last jump done using ``'.
  641. (viper-deflocalvar viper-last-jump nil)
  642. ;; Remembers position of the last jump done using `''.
  643. (viper-deflocalvar viper-last-jump-ignore 0)
  644. ;; History variables
  645. ;; History of search strings.
  646. (defvar viper-search-history (list ""))
  647. ;; History of query-replace strings used as a source.
  648. (defvar viper-replace1-history nil)
  649. ;; History of query-replace strings used as replacement.
  650. (defvar viper-replace2-history nil)
  651. ;; History of region quoting strings.
  652. (defvar viper-quote-region-history (list viper-quote-string))
  653. ;; History of Ex-style commands.
  654. (defvar viper-ex-history nil)
  655. ;; History of shell commands.
  656. (defvar viper-shell-history nil)
  657. ;; Last shell command. There are two of these, one for Ex (in viper-ex)
  658. ;; and one for Vi.
  659. ;; Last shell command executed with ! command.
  660. (defvar viper-last-shell-com nil)
  661. ;;; Face-saving tricks
  662. (defgroup viper-highlighting nil
  663. "Highlighting of replace region, search pattern, minibuffer, etc."
  664. :prefix "viper-"
  665. :group 'viper)
  666. (defface viper-search
  667. '((((class color)) (:foreground "Black" :background "khaki"))
  668. (t (:underline t :stipple "gray3")))
  669. "*Face used to flash out the search pattern."
  670. :group 'viper-highlighting)
  671. ;; An internal variable. Viper takes the face from here.
  672. (defvar viper-search-face 'viper-search
  673. "Face used to flash out the search pattern.
  674. DO NOT CHANGE this variable. Instead, use the customization widget
  675. to customize the actual face object `viper-search'
  676. this variable represents.")
  677. (defface viper-replace-overlay
  678. '((((class color)) (:foreground "Black" :background "darkseagreen2"))
  679. (t (:underline t :stipple "gray3")))
  680. "*Face for highlighting replace regions on a window display."
  681. :group 'viper-highlighting)
  682. ;; An internal variable. Viper takes the face from here.
  683. (defvar viper-replace-overlay-face 'viper-replace-overlay
  684. "Face for highlighting replace regions on a window display.
  685. DO NOT CHANGE this variable. Instead, use the customization widget
  686. to customize the actual face object `viper-replace-overlay'
  687. this variable represents.")
  688. (defface viper-minibuffer-emacs
  689. '((((class color)) (:foreground "Black" :background "darkseagreen2"))
  690. (t (:weight bold)))
  691. "Face used in the Minibuffer when it is in Emacs state."
  692. :group 'viper-highlighting)
  693. ;; An internal variable. Viper takes the face from here.
  694. (defvar viper-minibuffer-emacs-face 'viper-minibuffer-emacs
  695. "Face used in the Minibuffer when it is in Emacs state.
  696. DO NOT CHANGE this variable. Instead, use the customization widget
  697. to customize the actual face object `viper-minibuffer-emacs'
  698. this variable represents.")
  699. (defface viper-minibuffer-insert
  700. '((((class color)) (:foreground "Black" :background "pink"))
  701. (t (:slant italic)))
  702. "Face used in the Minibuffer when it is in Insert state."
  703. :group 'viper-highlighting)
  704. ;; An internal variable. Viper takes the face from here.
  705. (defvar viper-minibuffer-insert-face 'viper-minibuffer-insert
  706. "Face used in the Minibuffer when it is in Insert state.
  707. DO NOT CHANGE this variable. Instead, use the customization widget
  708. to customize the actual face object `viper-minibuffer-insert'
  709. this variable represents.")
  710. (defface viper-minibuffer-vi
  711. '((((class color)) (:foreground "DarkGreen" :background "grey"))
  712. (t (:inverse-video t)))
  713. "Face used in the Minibuffer when it is in Vi state."
  714. :group 'viper-highlighting)
  715. ;; An internal variable. Viper takes the face from here.
  716. (defvar viper-minibuffer-vi-face 'viper-minibuffer-vi
  717. "Face used in the Minibuffer when it is in Vi state.
  718. DO NOT CHANGE this variable. Instead, use the customization widget
  719. to customize the actual face object `viper-minibuffer-vi'
  720. this variable represents.")
  721. ;; the current face to be used in the minibuffer
  722. (viper-deflocalvar
  723. viper-minibuffer-current-face viper-minibuffer-emacs-face "")
  724. ;;; Miscellaneous
  725. (defvar viper-inhibit-startup-message nil
  726. "Whether Viper startup message should be inhibited.")
  727. (defcustom viper-spell-function 'ispell-region
  728. "Spell function used by #s<move> command to spell."
  729. :type 'function
  730. :group 'viper-misc)
  731. (defcustom viper-tags-file-name "TAGS"
  732. "The tags file used by Viper."
  733. :type 'string
  734. :group 'viper-misc)
  735. (defcustom viper-change-notification-threshold 1
  736. "Notify the user when this many lines or characters have been deleted/yanked.
  737. For line-deleting/yanking commands (like `dd', `yy'), the value denotes the
  738. number of lines. For character-based commands (such as `x', `dw', etc.), the
  739. value refers to the number of characters affected."
  740. :type 'integer
  741. :group 'viper-misc)
  742. ;; Minibuffer
  743. (defcustom viper-vi-style-in-minibuffer t
  744. "If t, use vi-style editing in minibuffer.
  745. Should be set in `~/.viper' file."
  746. :type 'boolean
  747. :group 'viper)
  748. ;; overlay used in the minibuffer to indicate which state it is in
  749. (viper-deflocalvar viper-minibuffer-overlay nil)
  750. (put 'viper-minibuffer-overlay 'permanent-local t)
  751. ;; Hook, specific to Viper, which is run just *before* exiting the minibuffer.
  752. ;; This is needed because beginning with Emacs 19.26, the standard
  753. ;; `minibuffer-exit-hook' is run *after* exiting the minibuffer
  754. (defvar viper-minibuffer-exit-hook nil)
  755. ;; Mode line
  756. (defconst viper-vi-state-id "<V> "
  757. "Mode line tag identifying the Vi mode of Viper.")
  758. (defconst viper-emacs-state-id "<E> "
  759. "Mode line tag identifying the Emacs mode of Viper.")
  760. (defconst viper-insert-state-id "<I> "
  761. "Mode line tag identifying the Insert mode of Viper.")
  762. (defconst viper-replace-state-id "<R> "
  763. "Mode line tag identifying the Replace mode of Viper.")
  764. (defgroup viper-hooks nil
  765. "Viper hooks."
  766. :prefix "viper-"
  767. :group 'viper)
  768. (defcustom viper-vi-state-hook 'viper-restore-cursor-type
  769. "*Hooks run just before the switch to Vi mode is completed."
  770. :type 'hook
  771. :group 'viper-hooks)
  772. (defcustom viper-insert-state-hook 'viper-set-insert-cursor-type
  773. "*Hooks run just before the switch to Insert mode is completed."
  774. :type 'hook
  775. :group 'viper-hooks)
  776. (defcustom viper-replace-state-hook 'viper-restore-cursor-type
  777. "*Hooks run just before the switch to Replace mode is completed."
  778. :type 'hook
  779. :group 'viper-hooks)
  780. (defcustom viper-emacs-state-hook 'viper-restore-cursor-type
  781. "*Hooks run just before the switch to Emacs mode is completed."
  782. :type 'hook
  783. :group 'viper-hooks)
  784. (defcustom viper-load-hook nil
  785. "Hooks run just after loading Viper."
  786. :type 'hook
  787. :group 'viper-hooks)
  788. (defun viper-restore-cursor-type ()
  789. (condition-case nil
  790. (if (featurep 'xemacs)
  791. (set (make-local-variable 'bar-cursor) nil)
  792. (setq cursor-type (default-value 'cursor-type)))
  793. (error nil)))
  794. (defun viper-set-insert-cursor-type ()
  795. (if (featurep 'xemacs)
  796. (set (make-local-variable 'bar-cursor) 2)
  797. (setq cursor-type '(bar . 2))))
  798. (defun viper-ESC-keyseq-timeout ()
  799. "*Key sequence beginning with ESC and separated by no more than this many milliseconds is considered to be generated by a keyboard function key.
  800. Setting this too high may slow down switching from insert to vi state. Setting
  801. this value too low will make it impossible to use function keys in insert mode
  802. on a dumb terminal."
  803. (if (viper-window-display-p)
  804. 0 viper-fast-keyseq-timeout))
  805. (provide 'viper-init)
  806. ;; Local Variables:
  807. ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
  808. ;; End:
  809. ;;; viper-init.el ends here