viper.el 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. ;;; viper.el --- A full-featured Vi emulator for Emacs and XEmacs,
  2. ;; a VI Plan for Emacs Rescue,
  3. ;; and a venomous VI PERil.
  4. ;; Viper Is also a Package for Emacs Rebels.
  5. ;; Copyright (C) 1994-2015 Free Software Foundation, Inc.
  6. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  7. ;; Keywords: emulations
  8. ;; Version: 3.14.1
  9. ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
  10. ;; file on 20/3/2008, and the maintainer agreed that when a bug is
  11. ;; filed in the Emacs bug reporting system against this file, a copy
  12. ;; of the bug report be sent to the maintainer's email address.
  13. (defconst viper-version "3.14.2 of July 4, 2013"
  14. "The current version of Viper")
  15. ;; This file is part of GNU Emacs.
  16. ;; GNU Emacs is free software: you can redistribute it and/or modify
  17. ;; it under the terms of the GNU General Public License as published by
  18. ;; the Free Software Foundation, either version 3 of the License, or
  19. ;; (at your option) any later version.
  20. ;; GNU Emacs is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;; GNU General Public License for more details.
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  26. ;;; Commentary:
  27. ;; Viper is a full-featured Vi emulator for Emacs and XEmacs. It emulates and
  28. ;; improves upon the standard features of Vi and, at the same time, allows
  29. ;; full access to all Emacs facilities. Viper supports multiple undo,
  30. ;; file name completion, command, file, and search history and it extends
  31. ;; Vi in many other ways. Viper is highly customizable through the various
  32. ;; hooks, user variables, and keymaps. It is implemented as a collection
  33. ;; of minor modes and it is designed to provide full access to all Emacs
  34. ;; major and minor modes.
  35. ;;
  36. ;;; History:
  37. ;;
  38. ;; Viper is a new name for a package formerly known as VIP-19,
  39. ;; which was a successor of VIP version 3.5 by Masahiko Sato
  40. ;; <ms@sail.stanford.edu> and VIP version 4.2 by Aamod Sane
  41. ;; <sane@cs.uiuc.edu>. Some ideas from vip 4.4.2 by Aamod Sane
  42. ;; were also shamelessly plagiarized.
  43. ;;
  44. ;; Viper maintains some degree of compatibility with these older
  45. ;; packages. See the documentation for customization.
  46. ;;
  47. ;; The main difference between Viper and these older packages are:
  48. ;;
  49. ;; 1. Viper emulates Vi at several levels, from almost complete conformity
  50. ;; to a rather loose Vi-compliance.
  51. ;;
  52. ;; 2. Viper provides full access to all major and minor modes of Emacs
  53. ;; without the need to type extra keys.
  54. ;; The older versions of VIP (and other Vi emulators) do not work with
  55. ;; some major and minor modes.
  56. ;;
  57. ;; 3. Viper supports vi-style undo.
  58. ;;
  59. ;; 4. Viper fully emulates (and improves upon) vi's replacement mode.
  60. ;;
  61. ;; 5. Viper has a better interface to ex, including command, variable, and
  62. ;; file name completion.
  63. ;;
  64. ;; 6. Viper uses native Emacs history and completion features; it doesn't
  65. ;; rely on other packages (such as gmhist.el and completer.el) to provide
  66. ;; these features.
  67. ;;
  68. ;; 7. Viper supports Vi-style editing in the minibuffer, by allowing the
  69. ;; user to switch from Insert state to Vi state to Replace state, etc.
  70. ;;
  71. ;; 8. Viper keeps history of recently inserted pieces of text and recently
  72. ;; executed Vi-style destructive commands, such as `i', `d', etc.
  73. ;; These pieces of text can be inserted in later insertion commands;
  74. ;; the previous destructive commands can be re-executed.
  75. ;;
  76. ;; 9. Viper has Vi-style keyboard macros, which enhances the similar
  77. ;; facility in the original Vi.
  78. ;; First, one can execute any Emacs command while defining a
  79. ;; macro, not just the Vi commands. Second, macros are defined in a
  80. ;; WYSYWYG mode, using an interface to Emacs's WYSIWYG style of defining
  81. ;; macros. Third, in Viper, one can define macros that are specific to
  82. ;; a given buffer, a given major mode, or macros defined for all buffers.
  83. ;; The same macro name can have several different definitions:
  84. ;; one global, several definitions for various major modes, and
  85. ;; definitions for specific buffers.
  86. ;; Buffer-specific definitions override mode-specific
  87. ;; definitions, which, in turn, override global definitions.
  88. ;;
  89. ;;
  90. ;;; Installation:
  91. ;; -------------
  92. ;;
  93. ;; (require 'viper)
  94. ;;
  95. ;;; Acknowledgments:
  96. ;; ----------------
  97. ;; Bug reports and ideas contributed by many users have helped
  98. ;; improve Viper and the various versions of VIP.
  99. ;; See the manual for a complete list of contributors.
  100. ;;
  101. ;;
  102. ;;; Notes:
  103. ;;
  104. ;; 1. Major modes.
  105. ;; In most cases, Viper handles major modes correctly, i.e., they come up
  106. ;; in the right state (either vi-state or emacs-state). For instance, text
  107. ;; files come up in vi-state, while, say, Dired appears in emacs-state by
  108. ;; default.
  109. ;; However, some modes do not appear in the right mode in the beginning,
  110. ;; usually because they neglect to follow Emacs conventions (e.g., they don't
  111. ;; use kill-all-local-variables when they start). Some major modes
  112. ;; may fail to come up in emacs-state if they call hooks, such as
  113. ;; text-hook, for no good reason.
  114. ;;
  115. ;; As an immediate solution, you can hit C-z to bring about the right mode.
  116. ;; An interim solution is to add an appropriate hook to the mode like this:
  117. ;;
  118. ;; (add-hook 'your-favorite-mode 'viper-mode)
  119. ;; or
  120. ;; (add-hook 'your-favorite-mode 'viper-change-state-to-emacs)
  121. ;;
  122. ;; whichever applies. The right thing to do, however, is to complain to the
  123. ;; author of the respective package. (Sometimes they also neglect to equip
  124. ;; their modes with hooks, which is one more reason for complaining.)
  125. ;;
  126. ;; 2. Keymap handling
  127. ;; Each Viper state (insert, vi, replace) is implemented as a collection of
  128. ;; several minor modes, each with its own keymap.
  129. ;;
  130. ;; Viper's Vi state consists of seven minor modes:
  131. ;;
  132. ;; viper-vi-intercept-minor-mode
  133. ;; viper-vi-local-user-minor-mode
  134. ;; viper-vi-global-user-minor-mode
  135. ;; viper-vi-kbd-minor-mode
  136. ;; viper-vi-state-modifier-minor-mode
  137. ;; viper-vi-diehard-minor-mode
  138. ;; viper-vi-basic-minor-mode
  139. ;;
  140. ;; Bindings done to the keymap of the first mode overshadow those done to
  141. ;; the second, which, in turn, overshadows those done to the third, etc.
  142. ;;
  143. ;; The last viper-vi-basic-minor-mode contains most of the usual Vi bindings
  144. ;; in its edit mode. This mode provides access to all Emacs facilities.
  145. ;; Novice users, however, may want to set their viper-expert-level to 1 in
  146. ;; their viper-custom-file-name. This will enable viper-vi-diehard-minor-mode.
  147. ;; This minor mode's bindings make Viper simulate the usual Vi very closely.
  148. ;; For instance, C-c will not have its standard Emacs binding
  149. ;; and so many of the goodies of Emacs are not available.
  150. ;;
  151. ;; A skilled user should set viper-expert-level to at least 3. This will
  152. ;; enable `C-c' and many Emacs facilities will become available.
  153. ;; In this case, viper-vi-diehard-minor-mode is inactive.
  154. ;;
  155. ;; Viper gurus should have at least
  156. ;; (setq viper-expert-level 4)
  157. ;; in their viper-custom-file-name. This will unsuppress all Emacs keys
  158. ;; that are not essential for VI-style editing.
  159. ;; Pick-and-choose users may want to put
  160. ;; (setq viper-expert-level 5)
  161. ;; in viper-custom-file-name. Viper will then leave it up to the user to
  162. ;; set the variables viper-want-* See viper-set-expert-level for details.
  163. ;;
  164. ;; The very first minor mode, viper-vi-intercept-minor-mode, is of no
  165. ;; concern for the user. It is needed to bind Viper's vital keys, such as
  166. ;; ESC and C-z.
  167. ;;
  168. ;; The second mode, viper-vi-local-user-minor-mode, usually has an
  169. ;; empty keymap. However, the user can set bindings in this keymap, which
  170. ;; will overshadow the corresponding bindings in the other two minor
  171. ;; modes. This is useful, for example, for setting up ZZ in gnus,
  172. ;; rmail, mh-e, etc., to send message instead of saving it in a file.
  173. ;; Likewise, in Dired mode, you may want to bind ZN and ZP to commands
  174. ;; that would visit the next or the previous file in the Dired buffer.
  175. ;; Setting local keys is tricky, so don't do it directly. Instead, use
  176. ;; viper-add-local-keys function (see its doc).
  177. ;;
  178. ;; The third minor mode, viper-vi-global-user-minor-mode, is also intended
  179. ;; for the users but, unlike viper-vi-local-user-minor-mode, its key
  180. ;; bindings are seen in all Viper buffers. This mode keys can be done
  181. ;; with define-key command.
  182. ;;
  183. ;; The fourth minor mode, viper-vi-kbd-minor-mode, is used by keyboard
  184. ;; macros. Users are NOT supposed to modify this keymap directly.
  185. ;;
  186. ;; The fifth mode, viper-vi-state-modifier-minor-mode, can be used to set
  187. ;; key bindings that are visible in some major modes but not in others.
  188. ;;
  189. ;; Users are allowed to modify keymaps that belong to
  190. ;; viper-vi-local-user-minor-mode, viper-vi-global-user-minor-mode,
  191. ;; and viper-vi-state-modifier-minor-mode only.
  192. ;;
  193. ;; Viper's Insert state also has seven minor modes:
  194. ;;
  195. ;; viper-insert-intercept-minor-mode
  196. ;; viper-insert-local-user-minor-mode
  197. ;; viper-insert-global-user-minor-mode
  198. ;; viper-insert-kbd-minor-mode
  199. ;; viper-insert-state-modifier-minor-mode
  200. ;; viper-insert-diehard-minor-mode
  201. ;; viper-insert-basic-minor-mode
  202. ;;
  203. ;; As with VI's editing modes, the first mode,
  204. ;; viper-insert-intercept-minor-mode is used to bind vital keys that are not
  205. ;; to be changed by the user.
  206. ;;
  207. ;; The next mode, viper-insert-local-user-minor-mode, is used to customize
  208. ;; bindings in the insert state of Viper. The third mode,
  209. ;; viper-insert-global-user-minor-mode is like
  210. ;; viper-insert-local-user-minor-mode, except that its bindings are seen in
  211. ;; all Viper buffers. As with viper-vi-local-user-minor-mode, its bindings
  212. ;; should be done via the function viper-add-local-keys. Bindings for
  213. ;; viper-insert-global-user-minor-mode can be set with the define-key command.
  214. ;;
  215. ;; The next minor mode, viper-insert-kbd-minor-mode,
  216. ;; is used for keyboard VI-style macros defined with :map!.
  217. ;;
  218. ;; The fifth minor mode, viper-insert-state-modifier-minor-mode, is like
  219. ;; viper-vi-state-modifier-minor-mode, except that it is used in the Insert
  220. ;; state; it can be used to modify keys in a mode-specific fashion.
  221. ;;
  222. ;; The minor mode viper-insert-diehard-minor-mode is in effect when
  223. ;; the user wants a high degree of Vi compatibility (a bad idea, really!).
  224. ;; The last minor mode, viper-insert-basic-minor-mode, is always in effect
  225. ;; when Viper is in insert state. It binds a small number of keys needed for
  226. ;; Viper's operation.
  227. ;;
  228. ;; Finally, Viper provides minor modes for overriding bindings set by Emacs
  229. ;; modes when Viper is in Emacs state:
  230. ;;
  231. ;; viper-emacs-local-user-minor-mode
  232. ;; viper-emacs-global-user-minor-mode
  233. ;; viper-emacs-kbd-minor-mode
  234. ;; viper-emacs-state-modifier-minor-mode
  235. ;;
  236. ;; These minor modes are in effect when Viper is in Emacs state. The keymap
  237. ;; associated with viper-emacs-global-user-minor-mode,
  238. ;; viper-emacs-global-user-map, overrides the global and local keymaps as
  239. ;; well as the minor mode keymaps set by other modes. The keymap of
  240. ;; viper-emacs-local-user-minor-mode, viper-emacs-local-user-map, overrides
  241. ;; everything, but it is used on a per buffer basis.
  242. ;; The keymap associated with viper-emacs-state-modifier-minor-mode
  243. ;; overrides keys on a per-major-mode basis. The mode
  244. ;; viper-emacs-kbd-minor-mode is used to define Vi-style macros in Emacs
  245. ;; state.
  246. ;;
  247. ;; 3. There is also one minor mode that is used when Viper is in its
  248. ;; replace-state (used for commands like cw, C, etc.). This mode is
  249. ;; called
  250. ;;
  251. ;; viper-replace-minor-mode
  252. ;;
  253. ;; and its keymap is viper-replace-map. Replace minor mode is always
  254. ;; used in conjunction with the minor modes for insert-state, and its
  255. ;; keymap overshadows the keymaps for insert minor modes.
  256. ;;
  257. ;; 4. Defining buffer-local bindings in Vi and Insert modes.
  258. ;; As mentioned before, sometimes, it is convenient to have
  259. ;; buffer-specific of mode-specific key bindings in Vi and insert modes.
  260. ;; Viper provides a special function, viper-add-local-keys, to do precisely
  261. ;; this. For instance, is you need to add couple of mode-specific bindings
  262. ;; to Insert mode, you can put
  263. ;;
  264. ;; (viper-add-local-keys 'insert-state '((key1 . func1) (key2 .func2)))
  265. ;;
  266. ;; somewhere in a hook of this major mode. If you put something like this
  267. ;; in your own elisp function, this will define bindings specific to the
  268. ;; buffer that was current at the time of the call to viper-add-local-keys.
  269. ;; The only thing to make sure here is that the major mode of this buffer
  270. ;; is written according to Emacs conventions, which includes a call to
  271. ;; (kill-all-local-variables). See viper-add-local-keys for more details.
  272. ;;
  273. ;;
  274. ;; TO DO (volunteers?):
  275. ;;
  276. ;; 1. Some of the code that is inherited from VIP-3.5 is rather
  277. ;; convoluted. Instead of viper-command-argument, keymaps should bind the
  278. ;; actual commands. E.g., "dw" should be bound to a generic command
  279. ;; viper-delete that will delete things based on the value of
  280. ;; last-command-event. This would greatly simplify the logic and the code.
  281. ;;
  282. ;; 2. Somebody should venture to write a customization package a la
  283. ;; options.el that would allow the user to change values of variables
  284. ;; that meet certain specs (e.g., match a regexp) and whose doc string
  285. ;; starts with a '*'. Then, the user should be offered to save
  286. ;; variables that were changed. This will make user's customization job
  287. ;; much easier.
  288. ;;
  289. ;;; Code:
  290. ;; compiler pacifier
  291. (defvar mark-even-if-inactive)
  292. (defvar quail-mode)
  293. (defvar viper-expert-level)
  294. (defvar viper-mode-string)
  295. (defvar viper-major-mode-modifier-list)
  296. ;; end pacifier
  297. (require 'advice)
  298. (require 'viper-init)
  299. (require 'viper-keym)
  300. ;; better be defined before Viper custom group.
  301. (defvar viper-custom-file-name (locate-user-emacs-file "viper" ".viper")
  302. "Viper customization file.
  303. If set by the user, this must be done _before_ Viper is loaded in `~/.emacs'.")
  304. (defgroup viper nil
  305. "Vi emulation within Emacs.
  306. NOTE: Viper customization should be saved in `viper-custom-file-name'."
  307. :prefix "viper-"
  308. :group 'emulations)
  309. (require 'viper-cmd)
  310. (defgroup viper-misc nil
  311. "Miscellaneous Viper customization."
  312. :prefix "viper-"
  313. :group 'viper)
  314. (defcustom viper-always t
  315. "Non-nil means, arrange for vi-state to be a default when appropriate.
  316. This is different from `viper-mode' variable in that `viper-mode' determines
  317. whether to use Viper in the first place, while `viper-always', if nil, lets
  318. user decide when to invoke Viper in a major mode."
  319. :type 'boolean
  320. :tag "Always Invoke Viper"
  321. :group 'viper-misc)
  322. ;; Non-viper variables that need to be saved in case the user decides to
  323. ;; de-viperize emacs.
  324. (defvar viper-saved-non-viper-variables nil)
  325. (defcustom viper-mode (cond (noninteractive nil)
  326. (t 'ask))
  327. "To Viperize or not to Viperize.
  328. If t, viperize Emacs. If nil -- don't. If `ask', ask the user.
  329. This variable is used primarily when Viper is being loaded.
  330. Must be set in your init file before Viper is loaded.
  331. DO NOT set this variable interactively, unless you are using the customization
  332. widget."
  333. :type '(choice (const nil) (const t) (const ask))
  334. :tag "Set Viper Mode on Loading"
  335. :group 'viper-misc)
  336. (defcustom viper-vi-state-mode-list
  337. '(fundamental-mode
  338. makefile-mode
  339. awk-mode
  340. m4-mode
  341. xrdb-mode
  342. winmgr-mode
  343. autoconf-mode
  344. cvs-edit-mode
  345. html-mode html-helper-mode
  346. emacs-lisp-mode lisp-mode lisp-interaction-mode
  347. jde-mode java-mode
  348. cc-mode c-mode c++-mode objc-mode
  349. fortran-mode f90-mode
  350. basic-mode
  351. bat-mode
  352. asm-mode
  353. prolog-mode
  354. flora-mode
  355. sql-mode
  356. text-mode indented-text-mode
  357. tex-mode latex-mode bibtex-mode
  358. ps-mode
  359. ;; completion-list-mode
  360. diff-mode
  361. idl-mode
  362. perl-mode
  363. cperl-mode
  364. javascript-mode
  365. tcl-mode
  366. python-mode
  367. sh-mode ksh-mode csh-mode
  368. gnus-article-mode
  369. mh-show-mode
  370. )
  371. "Major modes that require Vi command state."
  372. :type '(repeat symbol)
  373. :group 'viper-misc)
  374. (defcustom viper-emacs-state-mode-list
  375. '(Custom-mode
  376. dired-mode
  377. efs-mode
  378. tar-mode
  379. egg-status-buffer-mode
  380. browse-kill-ring-mode
  381. recentf-mode
  382. recentf-dialog-mode
  383. occur-mode
  384. mh-folder-mode
  385. gnus-group-mode
  386. gnus-summary-mode
  387. completion-list-mode
  388. help-mode
  389. Info-mode
  390. Buffer-menu-mode
  391. compilation-mode
  392. rcirc-mode
  393. jde-javadoc-checker-report-mode
  394. view-mode
  395. vm-mode
  396. vm-summary-mode)
  397. "A list of major modes that should come up in Emacs state.
  398. Normally, Viper would bring buffers up in Emacs state, unless the corresponding
  399. major mode has been placed on `viper-vi-state-mode-list' or
  400. `viper-insert-state-mode-list'. So, don't place a new mode on this list,
  401. unless it is coming up in a wrong Viper state."
  402. :type '(repeat symbol)
  403. :group 'viper-misc)
  404. (defcustom viper-insert-state-mode-list
  405. '(internal-ange-ftp-mode
  406. comint-mode
  407. gud-mode
  408. inferior-emacs-lisp-mode
  409. erc-mode
  410. eshell-mode
  411. shell-mode)
  412. "A list of major modes that should come up in Vi Insert state."
  413. :type '(repeat symbol)
  414. :group 'viper-misc)
  415. ;; used to set viper-major-mode-modifier-list in defcustom
  416. (defun viper-apply-major-mode-modifiers (&optional symbol value)
  417. (if symbol
  418. (set symbol value))
  419. (mapcar (lambda (triple)
  420. (viper-modify-major-mode
  421. (nth 0 triple) (nth 1 triple) (eval (nth 2 triple))))
  422. viper-major-mode-modifier-list))
  423. ;; We change standard bindings in some major modes, making them slightly
  424. ;; different than in "normal" vi/insert/emacs states
  425. (defcustom viper-major-mode-modifier-list
  426. '((help-mode emacs-state viper-slash-and-colon-map)
  427. (comint-mode insert-state viper-comint-mode-modifier-map)
  428. (comint-mode vi-state viper-comint-mode-modifier-map)
  429. (gud-mode insert-state viper-comint-mode-modifier-map)
  430. (shell-mode insert-state viper-comint-mode-modifier-map)
  431. (inferior-emacs-lisp-mode insert-state viper-comint-mode-modifier-map)
  432. (shell-mode vi-state viper-comint-mode-modifier-map)
  433. (ange-ftp-shell-mode insert-state viper-comint-mode-modifier-map)
  434. (ange-ftp-shell-mode vi-state viper-comint-mode-modifier-map)
  435. (internal-ange-ftp-mode insert-state viper-comint-mode-modifier-map)
  436. (internal-ange-ftp-mode vi-state viper-comint-mode-modifier-map)
  437. (dired-mode emacs-state viper-dired-modifier-map)
  438. (tar-mode emacs-state viper-slash-and-colon-map)
  439. (mh-folder-mode emacs-state viper-slash-and-colon-map)
  440. (gnus-group-mode emacs-state viper-gnus-modifier-map)
  441. (gnus-summary-mode emacs-state viper-gnus-modifier-map)
  442. (Info-mode emacs-state viper-slash-and-colon-map)
  443. (Buffer-menu-mode emacs-state viper-slash-and-colon-map)
  444. (erc-mode insert-state viper-comint-mode-modifier-map)
  445. (erc-mode vi-state viper-comint-mode-modifier-map)
  446. )
  447. "List specifying how to modify the various major modes to enable some Viperisms.
  448. The list has the structure: ((mode viper-state keymap) (mode viper-state
  449. keymap) ...). If `mode' is on the list, the `keymap' will be made active (on
  450. the minor-mode-map-alist) in the specified viper state.
  451. If you change this list, have to restart Emacs for the change to take effect.
  452. However, if you did the change through the customization widget, then Emacs
  453. needs to be restarted only if you deleted a triple mode-state-keymap from the
  454. list. No need to restart Emacs in case of insertion or modification of an
  455. existing triple."
  456. :type '(repeat
  457. (list symbol
  458. (choice (const emacs-state)
  459. (const vi-state)
  460. (const insert-state))
  461. symbol))
  462. :set 'viper-apply-major-mode-modifiers
  463. :group 'viper-misc)
  464. ;;;###autoload
  465. (defun toggle-viper-mode ()
  466. "Toggle Viper on/off.
  467. If Viper is enabled, turn it off. Otherwise, turn it on."
  468. (interactive)
  469. (if (eq viper-mode t)
  470. (viper-go-away)
  471. (setq viper-mode nil)
  472. (viper-mode)))
  473. ;;;###autoload
  474. (defun viper-mode ()
  475. "Turn on Viper emulation of Vi in Emacs. See Info node `(viper)Top'."
  476. (interactive)
  477. (if (not noninteractive)
  478. (progn
  479. ;; if the user requested viper-mode explicitly
  480. (if viper-mode
  481. ()
  482. (setq viper-mode t)
  483. ;; FIXME: Don't reload!
  484. (load-library "viper"))
  485. (if viper-first-time ; Important check. Prevents mix-up of startup
  486. (progn ; and expert-level msgs when viper-mode recurses
  487. (setq viper-first-time nil)
  488. (if (not viper-inhibit-startup-message)
  489. (save-window-excursion
  490. (setq viper-inhibit-startup-message t)
  491. (delete-other-windows)
  492. (switch-to-buffer "Viper Startup Message")
  493. (erase-buffer)
  494. (insert
  495. (substitute-command-keys
  496. "Viper Is a Package for Emacs Rebels,
  497. a VI Plan for Emacs Rescue, and a venomous VI PERil.
  498. Incidentally, Viper emulates Vi under Emacs/XEmacs 20.
  499. It supports all of what is good in Vi and Ex, while extending
  500. and improving upon much of it.
  501. 1. Viper supports Vi at several levels. Level 1 is the closest to Vi,
  502. level 5 provides the most flexibility to depart from many Vi conventions.
  503. You will be asked to specify your user level in a following screen.
  504. If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will behave
  505. as in VI, to smooth transition to Viper for the beginners. However, to
  506. use Emacs productively, you are advised to reach user level 3 or higher.
  507. At user level 2 or higher, ^X and ^C have Emacs, not Vi, bindings;
  508. ^Z toggles Vi/Emacs states; ^G is Emacs's keyboard-quit (like ^C in Vi).
  509. 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
  510. do not cause Emacs to quit, except at user level 1 (for a novice).
  511. 3. ^X^C EXITS EMACS.
  512. 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
  513. undo. Another `u' changes direction.
  514. 6. Emacs Meta key is `C-\\' (in all modes) or `\\ ESC' (in Vi command mode).
  515. On a window system, the best way is to use the Meta-key on your keyboard.
  516. 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
  517. something funny happens. This would abort the current editing command.
  518. For more information on Viper:
  519. a. Type `:help' in Vi command mode
  520. b. Print Viper manual, found in ./etc/viper.dvi
  521. c. Print the Quick Reference, found in ./etc/viperCard.dvi
  522. To submit a bug report or to contact the author, type :submitReport in Vi
  523. command mode. To shoo Viper away and return to pure Emacs (horror!), type:
  524. M-x viper-go-away
  525. This startup message appears whenever you load Viper, unless you type `y' now."
  526. ))
  527. (goto-char (point-min))
  528. (if (y-or-n-p "Inhibit Viper startup message? ")
  529. (viper-save-setting
  530. 'viper-inhibit-startup-message
  531. "Viper startup message inhibited"
  532. viper-custom-file-name t))
  533. ;;(kill-buffer (current-buffer))
  534. (message
  535. "The last message is in buffer `Viper Startup Message'")
  536. (sit-for 4)
  537. ))
  538. (viper-set-expert-level 'dont-change-unless)))
  539. (or (memq major-mode viper-emacs-state-mode-list) ; don't switch to Vi
  540. (memq major-mode viper-insert-state-mode-list) ; don't switch
  541. (viper-change-state-to-vi))
  542. ))
  543. (if (eq major-mode 'viper-mode)
  544. (setq major-mode 'fundamental-mode))
  545. )
  546. ;; Apply a little heuristic to invoke vi state on major-modes
  547. ;; that are not listed in viper-vi-state-mode-list
  548. (defun this-major-mode-requires-vi-state (mode)
  549. (cond ((memq mode viper-vi-state-mode-list) t)
  550. ((memq mode viper-emacs-state-mode-list) nil)
  551. ((memq mode viper-insert-state-mode-list) nil)
  552. (t (and (eq (key-binding "a") 'self-insert-command)
  553. (eq (key-binding " ") 'self-insert-command)))))
  554. ;; This hook designed to enable Vi-style editing in comint-based modes."
  555. (defun viper-comint-mode-hook ()
  556. (set (make-local-variable 'require-final-newline) nil)
  557. (setq viper-ex-style-editing nil
  558. viper-ex-style-motion nil)
  559. (viper-change-state-to-insert))
  560. ;; remove viper hooks from SYMBOL
  561. (defun viper-remove-hooks (symbol)
  562. (cond ((not (boundp symbol)) nil)
  563. ((not (listp (eval symbol))) nil)
  564. ((string-match "-hook" (symbol-name symbol))
  565. (remove-hook symbol 'viper-mode)
  566. (remove-hook symbol 'viper-change-state-to-emacs)
  567. (remove-hook symbol 'viper-change-state-to-insert)
  568. (remove-hook symbol 'viper-change-state-to-vi)
  569. (remove-hook symbol 'viper-minibuffer-post-command-hook)
  570. (remove-hook symbol 'viper-minibuffer-setup-sentinel)
  571. (remove-hook symbol 'viper-major-mode-change-sentinel)
  572. (remove-hook symbol 'set-viper-state-in-major-mode)
  573. (remove-hook symbol 'viper-post-command-sentinel)
  574. )))
  575. ;; Remove local value in all existing buffers
  576. ;; This doesn't delocalize vars (which would have been desirable)
  577. (defun viper-delocalize-var (symbol)
  578. (dolist (buf (buffer-list))
  579. (with-current-buffer buf
  580. (kill-local-variable symbol))))
  581. (defun viper-go-away ()
  582. "De-Viperize Emacs.
  583. This function tries to do as good a job as possible. However, it may undo some
  584. user customization, unrelated to Viper. For instance, if the user advised
  585. `read-file-name', `describe-key', and some others, then this advice will be
  586. undone.
  587. It also can't undo some Viper settings."
  588. (interactive)
  589. (viper-setup-ESC-to-escape nil)
  590. ;; restore non-viper vars
  591. (setq-default
  592. next-line-add-newlines
  593. (viper-standard-value
  594. 'next-line-add-newlines viper-saved-non-viper-variables)
  595. require-final-newline
  596. (viper-standard-value
  597. 'require-final-newline viper-saved-non-viper-variables)
  598. scroll-step
  599. (viper-standard-value 'scroll-step viper-saved-non-viper-variables)
  600. mode-line-buffer-identification
  601. (viper-standard-value
  602. 'mode-line-buffer-identification viper-saved-non-viper-variables)
  603. global-mode-string
  604. (delq 'viper-mode-string global-mode-string))
  605. (setq-default major-mode
  606. (viper-standard-value 'default-major-mode
  607. viper-saved-non-viper-variables))
  608. (if (featurep 'emacs)
  609. (setq-default
  610. mark-even-if-inactive
  611. (viper-standard-value
  612. 'mark-even-if-inactive viper-saved-non-viper-variables)))
  613. ;; Ideally, we would like to be able to de-localize local variables
  614. (unless
  615. (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  616. (viper-delocalize-var 'minor-mode-map-alist))
  617. (viper-delocalize-var 'require-final-newline)
  618. (if (featurep 'xemacs) (viper-delocalize-var 'bar-cursor))
  619. ;; deactivate all advices done by Viper.
  620. (ad-deactivate-regexp "viper-")
  621. (setq viper-mode nil)
  622. (when (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  623. (setq emulation-mode-map-alists
  624. (delq 'viper--intercept-key-maps
  625. (delq 'viper--key-maps emulation-mode-map-alists))
  626. ))
  627. (viper-delocalize-var 'viper-vi-minibuffer-minor-mode)
  628. (viper-delocalize-var 'viper-insert-minibuffer-minor-mode)
  629. (viper-delocalize-var 'viper-vi-intercept-minor-mode)
  630. (viper-delocalize-var 'viper-insert-intercept-minor-mode)
  631. (viper-delocalize-var 'viper-vi-local-user-minor-mode)
  632. (viper-delocalize-var 'viper-vi-kbd-minor-mode)
  633. (viper-delocalize-var 'viper-vi-global-user-minor-mode)
  634. (viper-delocalize-var 'viper-vi-state-modifier-minor-mode)
  635. (viper-delocalize-var 'viper-vi-diehard-minor-mode)
  636. (viper-delocalize-var 'viper-vi-basic-minor-mode)
  637. (viper-delocalize-var 'viper-replace-minor-mode)
  638. (viper-delocalize-var 'viper-insert-local-user-minor-mode)
  639. (viper-delocalize-var 'viper-insert-kbd-minor-mode)
  640. (viper-delocalize-var 'viper-insert-global-user-minor-mode)
  641. (viper-delocalize-var 'viper-insert-state-modifier-minor-mode)
  642. (viper-delocalize-var 'viper-insert-diehard-minor-mode)
  643. (viper-delocalize-var 'viper-insert-basic-minor-mode)
  644. (viper-delocalize-var 'viper-emacs-intercept-minor-mode)
  645. (viper-delocalize-var 'viper-emacs-local-user-minor-mode)
  646. (viper-delocalize-var 'viper-emacs-kbd-minor-mode)
  647. (viper-delocalize-var 'viper-emacs-global-user-minor-mode)
  648. (viper-delocalize-var 'viper-emacs-state-modifier-minor-mode)
  649. (viper-delocalize-var 'viper-current-state)
  650. (viper-delocalize-var 'viper-mode-string)
  651. (setq-default viper-vi-minibuffer-minor-mode nil
  652. viper-insert-minibuffer-minor-mode nil
  653. viper-vi-intercept-minor-mode nil
  654. viper-insert-intercept-minor-mode nil
  655. viper-vi-local-user-minor-mode nil
  656. viper-vi-kbd-minor-mode nil
  657. viper-vi-global-user-minor-mode nil
  658. viper-vi-state-modifier-minor-mode nil
  659. viper-vi-diehard-minor-mode nil
  660. viper-vi-basic-minor-mode nil
  661. viper-replace-minor-mode nil
  662. viper-insert-local-user-minor-mode nil
  663. viper-insert-kbd-minor-mode nil
  664. viper-insert-global-user-minor-mode nil
  665. viper-insert-state-modifier-minor-mode nil
  666. viper-insert-diehard-minor-mode nil
  667. viper-insert-basic-minor-mode nil
  668. viper-emacs-intercept-minor-mode nil
  669. viper-emacs-local-user-minor-mode nil
  670. viper-emacs-kbd-minor-mode nil
  671. viper-emacs-global-user-minor-mode nil
  672. viper-emacs-state-modifier-minor-mode nil
  673. viper-current-state 'emacs-state
  674. viper-mode-string viper-emacs-state-id
  675. )
  676. ;; remove all hooks set by viper
  677. (mapatoms 'viper-remove-hooks)
  678. (remove-hook 'comint-mode-hook 'viper-comint-mode-hook)
  679. (remove-hook 'erc-mode-hook 'viper-comint-mode-hook)
  680. (remove-hook 'change-major-mode-hook 'viper-major-mode-change-sentinel)
  681. ;; unbind Viper mouse bindings
  682. (viper-unbind-mouse-search-key)
  683. (viper-unbind-mouse-insert-key)
  684. ;; In emacs, we have to advice handle-switch-frame
  685. ;; This advice is undone earlier, when all advices matching "viper-" are
  686. ;; deactivated.
  687. (if (featurep 'xemacs)
  688. (remove-hook 'mouse-leave-frame-hook 'viper-remember-current-frame))
  689. ) ; end viper-go-away
  690. ;; list of buffers that just changed their major mode
  691. ;; used in a hack that triggers vi command mode whenever needed
  692. (defvar viper-new-major-mode-buffer-list nil)
  693. ;; set appropriate Viper state in buffers that changed major mode
  694. (defun set-viper-state-in-major-mode ()
  695. (mapc
  696. (lambda (buf)
  697. (if (viper-buffer-live-p buf)
  698. (with-current-buffer buf
  699. (cond ((and (this-major-mode-requires-vi-state major-mode)
  700. (eq viper-current-state 'emacs-state))
  701. (viper-mode))
  702. ((memq major-mode viper-emacs-state-mode-list)
  703. ;; not checking (eq viper-current-state 'emacs-state)
  704. ;; because viper-current-state could have gotten it by
  705. ;; default. We need viper-change-state-to-emacs here to have
  706. ;; the keymaps take effect.
  707. (viper-change-state-to-emacs))
  708. ((and (memq major-mode viper-insert-state-mode-list)
  709. (not (eq viper-current-state 'insert-state)))
  710. (viper-change-state-to-insert))
  711. )) ; with-current-buffer
  712. )) ; function
  713. viper-new-major-mode-buffer-list)
  714. ;; clear the list of bufs that changed major mode
  715. (setq viper-new-major-mode-buffer-list nil)
  716. ;; change the global value of hook
  717. (remove-hook 'viper-post-command-hooks 'set-viper-state-in-major-mode))
  718. ;; sets up post-command-hook to turn viper-mode, if the current mode is
  719. ;; fundamental
  720. (defun viper-major-mode-change-sentinel ()
  721. (save-match-data
  722. (or (string-match "\*Minibuf-" (buffer-name))
  723. (setq viper-new-major-mode-buffer-list
  724. (cons (current-buffer) viper-new-major-mode-buffer-list))))
  725. ;; change the global value of hook
  726. (add-hook 'viper-post-command-hooks 'set-viper-state-in-major-mode t))
  727. ;;; Handling of tty's ESC event
  728. ;; On a tty, an ESC event can either be the user hitting the escape key, or
  729. ;; some element of a byte sequence used to encode for example cursor keys.
  730. ;; So we try to recognize those events that correspond to the escape key and
  731. ;; turn them into `escape' events (same as used under GUIs). The heuristic we
  732. ;; use to distinguish the two cases is based, as usual, on a timeout, and on
  733. ;; the fact that the special ESC=>escape mapping only takes place if the whole
  734. ;; last key-sequence so far is just [?\e], i.e. either we're still in
  735. ;; read-key-sequence, or the last read-key-sequence only read [?\e], which
  736. ;; should ideally never happen because it should have been mapped to [escape].
  737. (defun viper--tty-ESC-filter (map)
  738. (if (and (equal (this-single-command-keys) [?\e])
  739. (sit-for (/ viper-fast-keyseq-timeout 1000)))
  740. [escape] map))
  741. (defun viper--lookup-key (map key)
  742. "Kind of like `lookup-key'.
  743. Two differences:
  744. - KEY is a single key, not a sequence.
  745. - the result is the \"raw\" binding, so it can be a `menu-item', rather than the
  746. binding contained in that menu item."
  747. (catch 'found
  748. (map-keymap (lambda (k b) (if (equal key k) (throw 'found b))) map)))
  749. (defun viper-catch-tty-ESC ()
  750. "Setup key mappings of current terminal to turn a tty's ESC into `escape'."
  751. (when (memq (terminal-live-p (frame-terminal)) '(t pc))
  752. (let ((esc-binding (viper-uncatch-tty-ESC)))
  753. (define-key input-decode-map
  754. [?\e] `(menu-item "" ,esc-binding :filter viper--tty-ESC-filter)))))
  755. (defun viper-uncatch-tty-ESC ()
  756. "Don't hack ESC into `escape' any more."
  757. (let ((b (viper--lookup-key input-decode-map ?\e)))
  758. (and (eq 'menu-item (car-safe b))
  759. (eq 'viper--tty-ESC-filter (nth 4 b))
  760. (define-key input-decode-map [?\e] (setq b (nth 2 b))))
  761. b))
  762. (defun viper-setup-ESC-to-escape (enable)
  763. (if enable
  764. (add-hook 'tty-setup-hook 'viper-catch-tty-ESC)
  765. (remove-hook 'tty-setup-hook 'viper-catch-tty-ESC))
  766. (let ((seen ()))
  767. (dolist (frame (frame-list))
  768. (let ((terminal (frame-terminal frame)))
  769. (unless (memq terminal seen)
  770. (push terminal seen)
  771. (with-selected-frame frame
  772. (if enable (viper-catch-tty-ESC) (viper-uncatch-tty-ESC))))))))
  773. ;; This sets major mode hooks to make them come up in vi-state.
  774. (defun viper-set-hooks ()
  775. ;; It is of course a misnomer to call viper-mode a `major mode'.
  776. ;; However, this has the effect that if the user didn't specify the
  777. ;; default mode, new buffers that fall back on the default will come up
  778. ;; in Fundamental Mode and Vi state.
  779. ;; When viper-mode is executed in such a case, it will set the major mode
  780. ;; back to fundamental-mode.
  781. (if (eq (default-value 'major-mode) 'fundamental-mode)
  782. ;; FIXME: We should use after-change-major-mode-hook instead!
  783. (setq-default major-mode 'viper-mode))
  784. (viper-setup-ESC-to-escape t)
  785. (add-hook 'change-major-mode-hook 'viper-major-mode-change-sentinel)
  786. (add-hook 'find-file-hooks 'set-viper-state-in-major-mode)
  787. ;; keep this because many modes we don't know about use this hook
  788. (defvar text-mode-hook)
  789. (add-hook 'text-mode-hook 'viper-mode)
  790. (defvar emerge-startup-hook)
  791. (add-hook 'emerge-startup-hook 'viper-change-state-to-emacs)
  792. ;; if viper is started from .emacs, it might be impossible to get certain
  793. ;; info about the display and windows until emacs initialization is complete
  794. ;; So do it via the window-setup-hook
  795. (add-hook 'window-setup-hook
  796. (lambda ()
  797. (modify-frame-parameters
  798. (selected-frame)
  799. (list (cons 'viper-vi-state-cursor-color
  800. (viper-get-cursor-color))))
  801. (setq viper-vi-state-cursor-color (viper-get-cursor-color))
  802. ))
  803. ;; Tell vc-diff to put *vc* in Vi mode
  804. (eval-after-load
  805. "vc"
  806. '(defadvice vc-diff (after viper-vc-ad activate)
  807. "Force Vi state in VC diff buffer."
  808. (viper-change-state-to-vi)))
  809. (eval-after-load
  810. "emerge"
  811. '(defadvice emerge-quit (after viper-emerge-advice activate)
  812. "Run `viper-change-state-to-vi' after quitting emerge."
  813. (viper-change-state-to-vi)))
  814. ;; passwd.el sets up its own buffer, which turns up in Vi mode,
  815. ;; thus overriding the local map. We don't need Vi mode here.
  816. (eval-after-load
  817. "passwd"
  818. '(defadvice read-passwd-1 (before viper-passwd-ad activate)
  819. "Switch to Emacs state while reading password."
  820. (viper-change-state-to-emacs)))
  821. (defadvice self-insert-command (around viper-self-insert-ad activate)
  822. "Ignore all self-inserting keys in the vi-state."
  823. ;; FIXME: Use remapping?
  824. (if (and (eq viper-current-state 'vi-state)
  825. ;; Do not use called-interactively-p here. XEmacs does not have it
  826. ;; and interactive-p is just fine.
  827. ;; (called-interactively-p 'interactive))
  828. (interactive-p))
  829. (beep 1)
  830. ad-do-it
  831. ))
  832. (defadvice set-cursor-color (after viper-set-cursor-color-ad activate)
  833. "Change cursor color in VI state."
  834. (modify-frame-parameters
  835. (selected-frame)
  836. (list (cons 'viper-vi-state-cursor-color (ad-get-arg 0))))
  837. (setq viper-vi-state-cursor-color (ad-get-arg 0))
  838. )
  839. (when (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  840. ;; needs to be as early as possible
  841. (add-to-ordered-list
  842. 'emulation-mode-map-alists 'viper--intercept-key-maps 100)
  843. ;; needs to be after cua-mode
  844. (add-to-ordered-list 'emulation-mode-map-alists 'viper--key-maps 500)
  845. )
  846. ;; Emacs shell, ange-ftp, and comint-based modes
  847. (add-hook 'comint-mode-hook 'viper-comint-mode-hook) ; comint
  848. (add-hook 'erc-mode-hook 'viper-comint-mode-hook) ; ERC
  849. (add-hook 'eshell-mode-hook
  850. (lambda () (setq viper-auto-indent nil)))
  851. (viper-set-emacs-state-searchstyle-macros nil 'dired-mode) ; dired
  852. (viper-set-emacs-state-searchstyle-macros nil 'tar-mode) ; tar
  853. (viper-set-emacs-state-searchstyle-macros nil 'mh-folder-mode) ; mhe
  854. (viper-set-emacs-state-searchstyle-macros nil 'gnus-group-mode) ; gnus
  855. (viper-set-emacs-state-searchstyle-macros nil 'gnus-summary-mode)
  856. (viper-set-emacs-state-searchstyle-macros nil 'Info-mode) ; info
  857. (viper-set-emacs-state-searchstyle-macros nil 'Buffer-menu-mode) ;buffer-menu
  858. ;; Modify major modes according to viper-major-mode-modifier-list
  859. (viper-apply-major-mode-modifiers)
  860. ;; For RMAIL users.
  861. ;; Put buf in Emacs state after edit.
  862. (eval-after-load
  863. "rmailedit"
  864. '(defadvice rmail-cease-edit (after viper-rmail-advice activate)
  865. "Switch to Emacs state when done editing message."
  866. (viper-change-state-to-emacs)))
  867. ;; ISO accents
  868. ;; Need to do it after loading iso-acc, or else this loading will wipe out
  869. ;; the advice.
  870. (eval-after-load
  871. "iso-acc"
  872. '(defadvice iso-accents-mode (around viper-iso-accents-advice activate)
  873. "Set viper-automatic-iso-accents to iso-accents-mode."
  874. (let ((arg (ad-get-arg 0)))
  875. ad-do-it
  876. (setq viper-automatic-iso-accents
  877. (if (eq viper-current-state 'vi-state)
  878. (if arg
  879. ;; if iso-accents-mode was called with positive arg, turn
  880. ;; accents on
  881. (> (prefix-numeric-value arg) 0)
  882. ;; else: toggle viper-automatic-iso-accents
  883. (not viper-automatic-iso-accents))
  884. ;; other states: accept what iso-accents-mode has done
  885. iso-accents-mode))
  886. ;; turn off ISO accents in vi-state
  887. (if (eq viper-current-state 'vi-state)
  888. (viper-set-iso-accents-mode nil))
  889. (if (memq viper-current-state '(vi-state insert-state replace-state))
  890. (message "Viper ISO accents mode: %s"
  891. (if viper-automatic-iso-accents "on" "off")))
  892. )))
  893. ;; International input methods
  894. (if (featurep 'emacs)
  895. (eval-after-load "mule-cmds"
  896. '(progn
  897. (defadvice deactivate-input-method (after viper-mule-advice activate)
  898. "Set viper-special-input-method to disable intl. input methods."
  899. (viper-deactivate-input-method-action))
  900. (defadvice activate-input-method (after viper-mule-advice activate)
  901. "Set viper-special-input-method to enable intl. input methods."
  902. (viper-activate-input-method-action))
  903. ))
  904. ;; XEmacs Although these hooks exist in Emacs, they don't seem to be always
  905. ;; called on input-method activation/deactivation, so we the above advise
  906. ;; functions instead.
  907. (eval-after-load "mule-cmds"
  908. '(progn
  909. (add-hook 'input-method-activate-hook
  910. 'viper-activate-input-method-action t)
  911. (add-hook 'input-method-deactivate-hook
  912. 'viper-deactivate-input-method-action t)))
  913. )
  914. (eval-after-load "mule-cmds"
  915. '(defadvice toggle-input-method (around viper-mule-advice activate)
  916. "Adjust input-method toggling in vi-state."
  917. (if (and viper-special-input-method (eq viper-current-state 'vi-state))
  918. (viper-deactivate-input-method)
  919. ad-do-it)))
  920. ) ; viper-set-hooks
  921. ;; these are primarily advices and Vi-ish variable settings
  922. (defun viper-non-hook-settings ()
  923. ;;;; Viper changes the default mode-line-buffer-identification
  924. ;;(setq-default mode-line-buffer-identification '(" %b"))
  925. ;; setup emacs-supported vi-style feel
  926. (setq next-line-add-newlines nil
  927. require-final-newline t)
  928. ;; don't bark when mark is inactive
  929. (if (featurep 'emacs)
  930. (setq mark-even-if-inactive t))
  931. (setq scroll-step 1)
  932. ;; Variable displaying the current Viper state in the mode line.
  933. (or (memq 'viper-mode-string global-mode-string)
  934. (setq global-mode-string
  935. (append '("" viper-mode-string) (cdr global-mode-string))))
  936. (if (featurep 'xemacs)
  937. ;; XEmacs
  938. (defadvice describe-key (before viper-describe-key-ad protect activate)
  939. "Force to read key via `viper-read-key-sequence'."
  940. (interactive (list (viper-read-key-sequence "Describe key: "))))
  941. ;; Emacs
  942. (defadvice describe-key (before viper-describe-key-ad protect activate)
  943. "Force to read key via `viper-read-key-sequence'."
  944. (interactive (let (key)
  945. (setq key (viper-read-key-sequence
  946. "Describe key (or click or menu item): "))
  947. (list key
  948. (prefix-numeric-value current-prefix-arg)
  949. ;; If KEY is a down-event, read also the
  950. ;; corresponding up-event.
  951. (and (vectorp key)
  952. (let ((last-idx (1- (length key))))
  953. (and (eventp (aref key last-idx))
  954. (memq 'down (event-modifiers
  955. (aref key last-idx)))))
  956. (or (and (eventp (aref key 0))
  957. (memq 'down (event-modifiers
  958. (aref key 0)))
  959. ;; For the C-down-mouse-2 popup menu,
  960. ;; there is no subsequent up-event
  961. (= (length key) 1))
  962. (and (> (length key) 1)
  963. (eventp (aref key 1))
  964. (memq 'down (event-modifiers (aref key 1)))))
  965. (read-event))))))
  966. ) ; (if (featurep 'xemacs)
  967. (if (featurep 'xemacs)
  968. ;; XEmacs
  969. (defadvice describe-key-briefly
  970. (before viper-describe-key-briefly-ad protect activate)
  971. "Force to read key via `viper-read-key-sequence'."
  972. (interactive (list (viper-read-key-sequence "Describe key briefly: "))))
  973. ;; Emacs
  974. (defadvice describe-key-briefly
  975. (before viper-describe-key-briefly-ad protect activate)
  976. "Force to read key via `viper-read-key-sequence'."
  977. (interactive (let (key)
  978. (setq key (viper-read-key-sequence
  979. "Describe key (or click or menu item): "))
  980. ;; If KEY is a down-event, read and discard the
  981. ;; corresponding up-event.
  982. (and (vectorp key)
  983. (let ((last-idx (1- (length key))))
  984. (and (eventp (aref key last-idx))
  985. (memq 'down (event-modifiers (aref key last-idx)))))
  986. (read-event))
  987. (list key
  988. (if current-prefix-arg
  989. (prefix-numeric-value current-prefix-arg))
  990. 1))))
  991. ) ; (if (featurep 'xemacs)
  992. (defadvice find-file (before viper-add-suffix-advice activate)
  993. "Use `read-file-name' for reading arguments."
  994. (interactive (cons (read-file-name "Find file: " nil default-directory)
  995. ;; XEmacs: if Mule & prefix arg, ask for coding system
  996. (cond ((and (featurep 'xemacs) (featurep 'mule))
  997. (list
  998. (and current-prefix-arg
  999. (read-coding-system "Coding-system: "))))
  1000. ;; Emacs: do wildcards
  1001. ((and (featurep 'emacs) (boundp 'find-file-wildcards))
  1002. (list find-file-wildcards))))
  1003. ))
  1004. (defadvice find-file-other-window (before viper-add-suffix-advice activate)
  1005. "Use `read-file-name' for reading arguments."
  1006. (interactive (cons (read-file-name "Find file in other window: "
  1007. nil default-directory)
  1008. ;; XEmacs: if Mule & prefix arg, ask for coding system
  1009. (cond ((and (featurep 'xemacs) (featurep 'mule))
  1010. (list
  1011. (and current-prefix-arg
  1012. (read-coding-system "Coding-system: "))))
  1013. ;; Emacs: do wildcards
  1014. ((and (featurep 'emacs) (boundp 'find-file-wildcards))
  1015. (list find-file-wildcards))))
  1016. ))
  1017. (defadvice find-file-other-frame (before viper-add-suffix-advice activate)
  1018. "Use `read-file-name' for reading arguments."
  1019. (interactive (cons (read-file-name "Find file in other frame: "
  1020. nil default-directory)
  1021. ;; XEmacs: if Mule & prefix arg, ask for coding system
  1022. (cond ((and (featurep 'xemacs) (featurep 'mule))
  1023. (list
  1024. (and current-prefix-arg
  1025. (read-coding-system "Coding-system: "))))
  1026. ;; Emacs: do wildcards
  1027. ((and (featurep 'emacs) (boundp 'find-file-wildcards))
  1028. (list find-file-wildcards))))
  1029. ))
  1030. (defadvice read-file-name (around viper-suffix-advice activate)
  1031. "Tell `exit-minibuffer' to run `viper-file-add-suffix' as a hook."
  1032. (let ((viper-minibuffer-exit-hook
  1033. (append viper-minibuffer-exit-hook
  1034. '(viper-minibuffer-trim-tail viper-file-add-suffix))))
  1035. ad-do-it))
  1036. (defadvice start-kbd-macro (after viper-kbd-advice activate)
  1037. "Remove Viper's intercepting bindings for C-x ).
  1038. This may be needed if the previous `:map' command terminated abnormally."
  1039. (define-key viper-vi-intercept-map "\C-x)" nil)
  1040. (define-key viper-insert-intercept-map "\C-x)" nil)
  1041. (define-key viper-emacs-intercept-map "\C-x)" nil))
  1042. (defadvice add-minor-mode (after
  1043. viper-advice-add-minor-mode
  1044. (toggle name &optional keymap after toggle-fun)
  1045. activate)
  1046. "Run viper-normalize-minor-mode-map-alist after adding a minor mode."
  1047. (viper-normalize-minor-mode-map-alist)
  1048. (unless
  1049. (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  1050. (setq-default minor-mode-map-alist minor-mode-map-alist)))
  1051. ;; catch frame switching event
  1052. (if (viper-window-display-p)
  1053. (if (featurep 'xemacs)
  1054. (add-hook 'mouse-leave-frame-hook
  1055. 'viper-remember-current-frame)
  1056. (defadvice handle-switch-frame (before viper-frame-advice activate)
  1057. "Remember the selected frame before the switch-frame event."
  1058. (viper-remember-current-frame (selected-frame)))) )
  1059. ) ; end viper-non-hook-settings
  1060. ;; Ask only if this-command/last-command are nil, i.e., when loading
  1061. (cond ((and (eq viper-mode 'ask) (null this-command) (null last-command))
  1062. (save-window-excursion
  1063. (with-output-to-temp-buffer " *viper-info*"
  1064. (princ "
  1065. You have loaded Viper, and are about to Viperize your Emacs!
  1066. Viper is a Package for Emacs Rebels and a venomous VI PERil,
  1067. It's time to decide: to Viperize or not to Viperize...
  1068. If you wish to Viperize AND make this your way of life, please put
  1069. (setq viper-mode t)
  1070. (require 'viper)
  1071. in your init file (preferably, close to the top).
  1072. These two lines must come in the order given."))
  1073. (if (y-or-n-p "Viperize? ")
  1074. (setq viper-mode t)
  1075. (setq viper-mode nil))
  1076. (message "")
  1077. (kill-buffer " *viper-info*")))
  1078. ;; If viper-mode is t, then just continue. Viper will kick in.
  1079. ((eq viper-mode t))
  1080. ;; Otherwise, it was asking Viper was not loaded through .emacs
  1081. ;; In this case, it was either through M-x viper-mode or via something
  1082. ;; else, like the custom widget. If Viper was loaded through
  1083. ;; M-x viper-mode, then viper will kick in anyway.
  1084. (t (setq viper-mode nil)))
  1085. (defun viper-load-custom-file ()
  1086. (if (and (file-exists-p viper-custom-file-name)
  1087. (not noninteractive))
  1088. (load viper-custom-file-name)))
  1089. ;; save non-viper vars that Viper might change
  1090. (if (null viper-saved-non-viper-variables)
  1091. (setq viper-saved-non-viper-variables
  1092. (list
  1093. (cons 'default-major-mode (list (default-value 'major-mode)))
  1094. (cons 'next-line-add-newlines (list next-line-add-newlines))
  1095. (cons 'require-final-newline (list require-final-newline))
  1096. (cons 'scroll-step (list scroll-step))
  1097. (cons 'mode-line-buffer-identification
  1098. (list (default-value 'mode-line-buffer-identification)))
  1099. (cons 'global-mode-string (list global-mode-string))
  1100. (if (featurep 'emacs)
  1101. (cons 'mark-even-if-inactive (list mark-even-if-inactive)))
  1102. )))
  1103. ;; Set some useful macros, advices
  1104. ;; These must be BEFORE viper-custom-file-name is loaded,
  1105. ;; so the user can unrecord them in viper-custom-file-name.
  1106. (if viper-mode
  1107. (progn
  1108. ;; set advices and some variables that give emacs Vi look.
  1109. (viper-non-hook-settings)
  1110. ;; repeat the 2nd previous command without rotating the command history
  1111. (viper-record-kbd-macro
  1112. (vector viper-repeat-from-history-key '\1) 'vi-state
  1113. [(meta x) v i p e r - r e p e a t - f r o m - h i s t o r y return] 't)
  1114. ;; repeat the 3d previous command without rotating the command history
  1115. (viper-record-kbd-macro
  1116. (vector viper-repeat-from-history-key '\2) 'vi-state
  1117. [(meta x) v i p e r - r e p e a t - f r o m - h i s t o r y return] 't)
  1118. ;; set macros for toggling case sensitivity and regexp search
  1119. (viper-set-searchstyle-toggling-macros nil)
  1120. ;; Make %%% toggle parsing comments for matching parentheses
  1121. (viper-set-parsing-style-toggling-macro nil)
  1122. ;; viper-custom-file-name is loaded if exists
  1123. (viper-load-custom-file)
  1124. ;; should be after loading custom file to avoid the pesky msg that
  1125. ;; mouse-search/insert keys are already bound
  1126. (viper-bind-mouse-search-key)
  1127. (viper-bind-mouse-insert-key)
  1128. ))
  1129. ;; Applying Viper customization -- runs after (load viper-custom-file-name)
  1130. ;; Save user settings or Viper defaults for vars controlled by
  1131. ;; viper-expert-level
  1132. (if (null viper-saved-user-settings)
  1133. (setq viper-saved-user-settings
  1134. (list (cons 'viper-want-ctl-h-help (list viper-want-ctl-h-help))
  1135. (cons 'viper-always (list viper-always))
  1136. (cons 'viper-no-multiple-ESC (list viper-no-multiple-ESC))
  1137. (cons 'viper-ex-style-motion (list viper-ex-style-motion))
  1138. (cons 'viper-ex-style-editing
  1139. (list viper-ex-style-editing))
  1140. (cons 'viper-want-emacs-keys-in-vi
  1141. (list viper-want-emacs-keys-in-vi))
  1142. (cons 'viper-electric-mode (list viper-electric-mode))
  1143. (cons 'viper-want-emacs-keys-in-insert
  1144. (list viper-want-emacs-keys-in-insert))
  1145. (cons 'viper-re-search (list viper-re-search)))))
  1146. (if viper-mode
  1147. (progn
  1148. (viper-set-minibuffer-style)
  1149. (if viper-buffer-search-char
  1150. (viper-buffer-search-enable))
  1151. (viper-update-syntax-classes 'set-default)
  1152. ))
  1153. ;;; Familiarize Viper with some minor modes that have their own keymaps
  1154. (if viper-mode
  1155. (progn
  1156. (viper-harness-minor-mode "compile")
  1157. (viper-harness-minor-mode "outline")
  1158. (viper-harness-minor-mode "allout")
  1159. (viper-harness-minor-mode "xref")
  1160. (viper-harness-minor-mode "lmenu")
  1161. (viper-harness-minor-mode "vc")
  1162. (viper-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX, which
  1163. (viper-harness-minor-mode "latex") ; sits in one of these two files
  1164. (viper-harness-minor-mode "cyrillic")
  1165. (viper-harness-minor-mode "russian")
  1166. (viper-harness-minor-mode "view-less")
  1167. (viper-harness-minor-mode "view")
  1168. (viper-harness-minor-mode "reftex")
  1169. (viper-harness-minor-mode "flyspell")
  1170. ))
  1171. ;; Intercept maps could go in viper-keym.el
  1172. ;; We keep them here in case someone redefines them in viper-custom-file-name
  1173. (define-key viper-vi-intercept-map viper-ESC-key 'viper-intercept-ESC-key)
  1174. (define-key viper-insert-intercept-map viper-ESC-key 'viper-intercept-ESC-key)
  1175. ;; This is taken care of by viper-insert-global-user-map.
  1176. ;;(define-key viper-replace-map viper-ESC-key 'viper-intercept-ESC-key)
  1177. ;; The default viper-toggle-key is \C-z; for the novice, it suspends or
  1178. ;; iconifies Emacs
  1179. (define-key viper-vi-intercept-map viper-toggle-key 'viper-toggle-key-action)
  1180. (define-key
  1181. viper-emacs-intercept-map viper-toggle-key 'viper-change-state-to-vi)
  1182. ;;; Removed to avoid bad interaction with cua-mode.
  1183. ;;; Escape from Emacs and Insert modes to Vi for one command
  1184. ;;(define-key viper-emacs-intercept-map "\C-c\\" 'viper-escape-to-vi)
  1185. ;;(define-key viper-insert-intercept-map "\C-c\\" 'viper-escape-to-vi)
  1186. (if viper-mode
  1187. (setq-default viper-emacs-intercept-minor-mode t
  1188. viper-emacs-local-user-minor-mode t
  1189. viper-emacs-global-user-minor-mode t
  1190. viper-emacs-kbd-minor-mode t
  1191. viper-emacs-state-modifier-minor-mode t))
  1192. (if (and viper-mode (eq viper-current-state 'emacs-state))
  1193. (setq viper-emacs-intercept-minor-mode t
  1194. viper-emacs-local-user-minor-mode t
  1195. viper-emacs-global-user-minor-mode t
  1196. viper-emacs-kbd-minor-mode t
  1197. viper-emacs-state-modifier-minor-mode t))
  1198. (if (and viper-mode
  1199. (or viper-always
  1200. (and (< viper-expert-level 5) (> viper-expert-level 0))))
  1201. (viper-set-hooks))
  1202. ;; Let all minor modes take effect after loading.
  1203. ;; This may not be enough, so we also set default minor-mode-alist.
  1204. ;; Without setting the default, new buffers that come up in emacs mode have
  1205. ;; minor-mode-map-alist = nil, unless we call viper-change-state-*
  1206. (if (and viper-mode (eq viper-current-state 'emacs-state))
  1207. (progn
  1208. (viper-change-state-to-emacs)
  1209. (unless
  1210. (and (fboundp 'add-to-ordered-list)
  1211. (boundp 'emulation-mode-map-alists))
  1212. (setq-default minor-mode-map-alist minor-mode-map-alist))
  1213. ))
  1214. (if (and viper-mode (this-major-mode-requires-vi-state major-mode))
  1215. (viper-mode))
  1216. (if viper-mode
  1217. (setq initial-major-mode
  1218. `(lambda ()
  1219. (funcall (quote ,initial-major-mode))
  1220. (set-viper-state-in-major-mode))
  1221. ))
  1222. (run-hooks 'viper-load-hook) ; the last chance to change something
  1223. (provide 'viper)
  1224. ;; Local Variables:
  1225. ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
  1226. ;; End:
  1227. ;;; viper.el ends here