viper.el 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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-2012 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.1 of August 15, 2009"
  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. ;;; Acknowledgements:
  96. ;; -----------------
  97. ;; Bug reports and ideas contributed by many users have helped
  98. ;; improve Viper and the various versions of VIP.
  99. ;; See the on-line 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
  146. ;; in their .viper file. This will enable viper-vi-diehard-minor-mode. This
  147. ;; 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 files. This will unsuppress all Emacs keys that are not
  158. ;; essential for VI-style editing.
  159. ;; Pick-and-choose users may want to put
  160. ;; (setq viper-expert-level 5)
  161. ;; in ~/.viper. Viper will then leave it up to the user to set the variables
  162. ;; 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 (convert-standard-filename "~/.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', which
  307. defaults to `~/.viper'."
  308. :prefix "viper-"
  309. :group 'emulations)
  310. (require 'viper-cmd)
  311. (defgroup viper-misc nil
  312. "Miscellaneous Viper customization."
  313. :prefix "viper-"
  314. :group 'viper)
  315. (defcustom viper-always t
  316. "Non-nil means, arrange for vi-state to be a default when appropriate.
  317. This is different from `viper-mode' variable in that `viper-mode' determines
  318. whether to use Viper in the first place, while `viper-always', if nil, lets
  319. user decide when to invoke Viper in a major mode."
  320. :type 'boolean
  321. :tag "Always Invoke Viper"
  322. :group 'viper-misc)
  323. ;; Non-viper variables that need to be saved in case the user decides to
  324. ;; de-viperize emacs.
  325. (defvar viper-saved-non-viper-variables nil)
  326. (defcustom viper-mode (cond (noninteractive nil)
  327. (t 'ask))
  328. "To Viperize or not to Viperize.
  329. If t, viperize Emacs. If nil -- don't. If `ask', ask the user.
  330. This variable is used primarily when Viper is being loaded.
  331. Must be set in `~/.emacs' before Viper is loaded.
  332. DO NOT set this variable interactively, unless you are using the customization
  333. widget."
  334. :type '(choice (const nil) (const t) (const ask))
  335. :tag "Set Viper Mode on Loading"
  336. :group 'viper-misc)
  337. (defcustom viper-vi-state-mode-list
  338. '(fundamental-mode
  339. makefile-mode
  340. awk-mode
  341. m4-mode
  342. xrdb-mode
  343. winmgr-mode
  344. autoconf-mode
  345. cvs-edit-mode
  346. html-mode html-helper-mode
  347. emacs-lisp-mode lisp-mode lisp-interaction-mode
  348. jde-mode java-mode
  349. cc-mode c-mode c++-mode objc-mode
  350. fortran-mode f90-mode
  351. basic-mode
  352. bat-mode
  353. asm-mode
  354. prolog-mode
  355. flora-mode
  356. sql-mode
  357. text-mode indented-text-mode
  358. tex-mode latex-mode bibtex-mode
  359. ps-mode
  360. ;; completion-list-mode
  361. diff-mode
  362. idl-mode
  363. perl-mode
  364. cperl-mode
  365. javascript-mode
  366. tcl-mode
  367. python-mode
  368. sh-mode ksh-mode csh-mode
  369. gnus-article-mode
  370. mh-show-mode
  371. )
  372. "Major modes that require Vi command state."
  373. :type '(repeat symbol)
  374. :group 'viper-misc)
  375. (defcustom viper-emacs-state-mode-list
  376. '(Custom-mode
  377. dired-mode
  378. efs-mode
  379. tar-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. (load-library "viper"))
  484. (if viper-first-time ; Important check. Prevents mix-up of startup
  485. (progn ; and expert-level msgs when viper-mode recurses
  486. (setq viper-first-time nil)
  487. (if (not viper-inhibit-startup-message)
  488. (save-window-excursion
  489. (setq viper-inhibit-startup-message t)
  490. (delete-other-windows)
  491. (switch-to-buffer "Viper Startup Message")
  492. (erase-buffer)
  493. (insert
  494. (substitute-command-keys
  495. "Viper Is a Package for Emacs Rebels,
  496. a VI Plan for Emacs Rescue, and a venomous VI PERil.
  497. Incidentally, Viper emulates Vi under Emacs/XEmacs 20.
  498. It supports all of what is good in Vi and Ex, while extending
  499. and improving upon much of it.
  500. 1. Viper supports Vi at several levels. Level 1 is the closest to Vi,
  501. level 5 provides the most flexibility to depart from many Vi conventions.
  502. You will be asked to specify your user level in a following screen.
  503. If you select user level 1 then the keys ^X, ^C, ^Z, and ^G will behave
  504. as in VI, to smooth transition to Viper for the beginners. However, to
  505. use Emacs productively, you are advised to reach user level 3 or higher.
  506. At user level 2 or higher, ^X and ^C have Emacs, not Vi, bindings;
  507. ^Z toggles Vi/Emacs states; ^G is Emacs's keyboard-quit (like ^C in Vi).
  508. 2. Vi exit functions (e.g., :wq, ZZ) work on INDIVIDUAL files -- they
  509. do not cause Emacs to quit, except at user level 1 (for a novice).
  510. 3. ^X^C EXITS EMACS.
  511. 4. Viper supports multiple undo: `u' will undo. Typing `.' will repeat
  512. undo. Another `u' changes direction.
  513. 6. Emacs Meta key is `C-\\' (in all modes) or `\\ ESC' (in Vi command mode).
  514. On a window system, the best way is to use the Meta-key on your keyboard.
  515. 7. Try \\[keyboard-quit] and \\[abort-recursive-edit] repeatedly,if
  516. something funny happens. This would abort the current editing command.
  517. For more information on Viper:
  518. a. Type `:help' in Vi command mode
  519. b. Print Viper manual, found in ./etc/viper.dvi
  520. c. Print the Quick Reference, found in ./etc/viperCard.dvi
  521. To submit a bug report or to contact the author, type :submitReport in Vi
  522. command mode. To shoo Viper away and return to pure Emacs (horror!), type:
  523. M-x viper-go-away
  524. This startup message appears whenever you load Viper, unless you type `y' now."
  525. ))
  526. (goto-char (point-min))
  527. (if (y-or-n-p "Inhibit Viper startup message? ")
  528. (viper-save-setting
  529. 'viper-inhibit-startup-message
  530. "Viper startup message inhibited"
  531. viper-custom-file-name t))
  532. ;;(kill-buffer (current-buffer))
  533. (message
  534. "The last message is in buffer `Viper Startup Message'")
  535. (sit-for 4)
  536. ))
  537. (viper-set-expert-level 'dont-change-unless)))
  538. (or (memq major-mode viper-emacs-state-mode-list) ; don't switch to Vi
  539. (memq major-mode viper-insert-state-mode-list) ; don't switch
  540. (viper-change-state-to-vi))
  541. ))
  542. (if (eq major-mode 'viper-mode)
  543. (setq major-mode 'fundamental-mode))
  544. )
  545. ;; Apply a little heuristic to invoke vi state on major-modes
  546. ;; that are not listed in viper-vi-state-mode-list
  547. (defun this-major-mode-requires-vi-state (mode)
  548. (cond ((memq mode viper-vi-state-mode-list) t)
  549. ((memq mode viper-emacs-state-mode-list) nil)
  550. ((memq mode viper-insert-state-mode-list) nil)
  551. (t (and (eq (key-binding "a") 'self-insert-command)
  552. (eq (key-binding " ") 'self-insert-command)))))
  553. ;; This hook designed to enable Vi-style editing in comint-based modes."
  554. (defun viper-comint-mode-hook ()
  555. (set (make-local-variable 'require-final-newline) nil)
  556. (setq viper-ex-style-editing nil
  557. viper-ex-style-motion nil)
  558. (viper-change-state-to-insert))
  559. ;; remove viper hooks from SYMBOL
  560. (defun viper-remove-hooks (symbol)
  561. (cond ((not (boundp symbol)) nil)
  562. ((not (listp (eval symbol))) nil)
  563. ((string-match "-hook" (symbol-name symbol))
  564. (remove-hook symbol 'viper-mode)
  565. (remove-hook symbol 'viper-change-state-to-emacs)
  566. (remove-hook symbol 'viper-change-state-to-insert)
  567. (remove-hook symbol 'viper-change-state-to-vi)
  568. (remove-hook symbol 'viper-minibuffer-post-command-hook)
  569. (remove-hook symbol 'viper-minibuffer-setup-sentinel)
  570. (remove-hook symbol 'viper-major-mode-change-sentinel)
  571. (remove-hook symbol 'set-viper-state-in-major-mode)
  572. (remove-hook symbol 'viper-post-command-sentinel)
  573. )))
  574. ;; Remove local value in all existing buffers
  575. ;; This doesn't delocalize vars (which would have been desirable)
  576. (defun viper-delocalize-var (symbol)
  577. (dolist (buf (buffer-list))
  578. (with-current-buffer buf
  579. (kill-local-variable symbol))))
  580. (defun viper-go-away ()
  581. "De-Viperize Emacs.
  582. This function tries to do as good a job as possible. However, it may undo some
  583. user customization, unrelated to Viper. For instance, if the user advised
  584. `read-file-name', `describe-key', and some others, then this advice will be
  585. undone.
  586. It also can't undo some Viper settings."
  587. (interactive)
  588. ;; restore non-viper vars
  589. (setq-default
  590. next-line-add-newlines
  591. (viper-standard-value
  592. 'next-line-add-newlines viper-saved-non-viper-variables)
  593. require-final-newline
  594. (viper-standard-value
  595. 'require-final-newline viper-saved-non-viper-variables)
  596. scroll-step
  597. (viper-standard-value 'scroll-step viper-saved-non-viper-variables)
  598. mode-line-buffer-identification
  599. (viper-standard-value
  600. 'mode-line-buffer-identification viper-saved-non-viper-variables)
  601. global-mode-string
  602. (delq 'viper-mode-string global-mode-string))
  603. (setq-default major-mode
  604. (viper-standard-value 'default-major-mode
  605. viper-saved-non-viper-variables))
  606. (if (featurep 'emacs)
  607. (setq-default
  608. mark-even-if-inactive
  609. (viper-standard-value
  610. 'mark-even-if-inactive viper-saved-non-viper-variables)))
  611. ;; Ideally, we would like to be able to de-localize local variables
  612. (unless
  613. (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  614. (viper-delocalize-var 'minor-mode-map-alist))
  615. (viper-delocalize-var 'require-final-newline)
  616. (if (featurep 'xemacs) (viper-delocalize-var 'bar-cursor))
  617. ;; deactivate all advices done by Viper.
  618. (ad-deactivate-regexp "viper-")
  619. (setq viper-mode nil)
  620. (when (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  621. (setq emulation-mode-map-alists
  622. (delq 'viper--intercept-key-maps
  623. (delq 'viper--key-maps emulation-mode-map-alists))
  624. ))
  625. (viper-delocalize-var 'viper-vi-minibuffer-minor-mode)
  626. (viper-delocalize-var 'viper-insert-minibuffer-minor-mode)
  627. (viper-delocalize-var 'viper-vi-intercept-minor-mode)
  628. (viper-delocalize-var 'viper-insert-intercept-minor-mode)
  629. (viper-delocalize-var 'viper-vi-local-user-minor-mode)
  630. (viper-delocalize-var 'viper-vi-kbd-minor-mode)
  631. (viper-delocalize-var 'viper-vi-global-user-minor-mode)
  632. (viper-delocalize-var 'viper-vi-state-modifier-minor-mode)
  633. (viper-delocalize-var 'viper-vi-diehard-minor-mode)
  634. (viper-delocalize-var 'viper-vi-basic-minor-mode)
  635. (viper-delocalize-var 'viper-replace-minor-mode)
  636. (viper-delocalize-var 'viper-insert-local-user-minor-mode)
  637. (viper-delocalize-var 'viper-insert-kbd-minor-mode)
  638. (viper-delocalize-var 'viper-insert-global-user-minor-mode)
  639. (viper-delocalize-var 'viper-insert-state-modifier-minor-mode)
  640. (viper-delocalize-var 'viper-insert-diehard-minor-mode)
  641. (viper-delocalize-var 'viper-insert-basic-minor-mode)
  642. (viper-delocalize-var 'viper-emacs-intercept-minor-mode)
  643. (viper-delocalize-var 'viper-emacs-local-user-minor-mode)
  644. (viper-delocalize-var 'viper-emacs-kbd-minor-mode)
  645. (viper-delocalize-var 'viper-emacs-global-user-minor-mode)
  646. (viper-delocalize-var 'viper-emacs-state-modifier-minor-mode)
  647. (viper-delocalize-var 'viper-current-state)
  648. (viper-delocalize-var 'viper-mode-string)
  649. (setq-default viper-vi-minibuffer-minor-mode nil
  650. viper-insert-minibuffer-minor-mode nil
  651. viper-vi-intercept-minor-mode nil
  652. viper-insert-intercept-minor-mode nil
  653. viper-vi-local-user-minor-mode nil
  654. viper-vi-kbd-minor-mode nil
  655. viper-vi-global-user-minor-mode nil
  656. viper-vi-state-modifier-minor-mode nil
  657. viper-vi-diehard-minor-mode nil
  658. viper-vi-basic-minor-mode nil
  659. viper-replace-minor-mode nil
  660. viper-insert-local-user-minor-mode nil
  661. viper-insert-kbd-minor-mode nil
  662. viper-insert-global-user-minor-mode nil
  663. viper-insert-state-modifier-minor-mode nil
  664. viper-insert-diehard-minor-mode nil
  665. viper-insert-basic-minor-mode nil
  666. viper-emacs-intercept-minor-mode nil
  667. viper-emacs-local-user-minor-mode nil
  668. viper-emacs-kbd-minor-mode nil
  669. viper-emacs-global-user-minor-mode nil
  670. viper-emacs-state-modifier-minor-mode nil
  671. viper-current-state 'emacs-state
  672. viper-mode-string viper-emacs-state-id
  673. )
  674. ;; remove all hooks set by viper
  675. (mapatoms 'viper-remove-hooks)
  676. (remove-hook 'comint-mode-hook 'viper-comint-mode-hook)
  677. (remove-hook 'erc-mode-hook 'viper-comint-mode-hook)
  678. (remove-hook 'change-major-mode-hook 'viper-major-mode-change-sentinel)
  679. ;; unbind Viper mouse bindings
  680. (viper-unbind-mouse-search-key)
  681. (viper-unbind-mouse-insert-key)
  682. ;; In emacs, we have to advice handle-switch-frame
  683. ;; This advice is undone earlier, when all advices matching "viper-" are
  684. ;; deactivated.
  685. (if (featurep 'xemacs)
  686. (remove-hook 'mouse-leave-frame-hook 'viper-remember-current-frame))
  687. ) ; end viper-go-away
  688. ;; list of buffers that just changed their major mode
  689. ;; used in a hack that triggers vi command mode whenever needed
  690. (defvar viper-new-major-mode-buffer-list nil)
  691. ;; set appropriate Viper state in buffers that changed major mode
  692. (defun set-viper-state-in-major-mode ()
  693. (mapc
  694. (lambda (buf)
  695. (if (viper-buffer-live-p buf)
  696. (with-current-buffer buf
  697. (cond ((and (this-major-mode-requires-vi-state major-mode)
  698. (eq viper-current-state 'emacs-state))
  699. (viper-mode))
  700. ((memq major-mode viper-emacs-state-mode-list)
  701. ;; not checking (eq viper-current-state 'emacs-state)
  702. ;; because viper-current-state could have gotten it by
  703. ;; default. We need viper-change-state-to-emacs here to have
  704. ;; the keymaps take effect.
  705. (viper-change-state-to-emacs))
  706. ((and (memq major-mode viper-insert-state-mode-list)
  707. (not (eq viper-current-state 'insert-state)))
  708. (viper-change-state-to-insert))
  709. )) ; with-current-buffer
  710. )) ; function
  711. viper-new-major-mode-buffer-list)
  712. ;; clear the list of bufs that changed major mode
  713. (setq viper-new-major-mode-buffer-list nil)
  714. ;; change the global value of hook
  715. (remove-hook 'viper-post-command-hooks 'set-viper-state-in-major-mode))
  716. ;; sets up post-command-hook to turn viper-mode, if the current mode is
  717. ;; fundamental
  718. (defun viper-major-mode-change-sentinel ()
  719. (save-match-data
  720. (or (string-match "\*Minibuf-" (buffer-name))
  721. (setq viper-new-major-mode-buffer-list
  722. (cons (current-buffer) viper-new-major-mode-buffer-list))))
  723. ;; change the global value of hook
  724. (add-hook 'viper-post-command-hooks 'set-viper-state-in-major-mode t))
  725. ;; This sets major mode hooks to make them come up in vi-state.
  726. (defun viper-set-hooks ()
  727. ;; It is of course a misnomer to call viper-mode a `major mode'.
  728. ;; However, this has the effect that if the user didn't specify the
  729. ;; default mode, new buffers that fall back on the default will come up
  730. ;; in Fundamental Mode and Vi state.
  731. ;; When viper-mode is executed in such a case, it will set the major mode
  732. ;; back to fundamental-mode.
  733. (if (eq (default-value 'major-mode) 'fundamental-mode)
  734. (setq-default major-mode 'viper-mode))
  735. (add-hook 'change-major-mode-hook 'viper-major-mode-change-sentinel)
  736. (add-hook 'find-file-hooks 'set-viper-state-in-major-mode)
  737. ;; keep this because many modes we don't know about use this hook
  738. (defvar text-mode-hook)
  739. (add-hook 'text-mode-hook 'viper-mode)
  740. (defvar emerge-startup-hook)
  741. (add-hook 'emerge-startup-hook 'viper-change-state-to-emacs)
  742. ;; Zap bad bindings in flyspell-mouse-map, which prevent ESC from working
  743. ;; over misspelled words (due to the overlay keymaps)
  744. (defvar flyspell-mode-hook)
  745. (defvar flyspell-mouse-map)
  746. (add-hook 'flyspell-mode-hook
  747. (lambda ()
  748. (define-key flyspell-mouse-map viper-ESC-key nil)))
  749. ;; if viper is started from .emacs, it might be impossible to get certain
  750. ;; info about the display and windows until emacs initialization is complete
  751. ;; So do it via the window-setup-hook
  752. (add-hook 'window-setup-hook
  753. (lambda ()
  754. (modify-frame-parameters
  755. (selected-frame)
  756. (list (cons 'viper-vi-state-cursor-color
  757. (viper-get-cursor-color))))
  758. (setq viper-vi-state-cursor-color (viper-get-cursor-color))
  759. ))
  760. ;; Tell vc-diff to put *vc* in Vi mode
  761. (eval-after-load
  762. "vc"
  763. '(defadvice vc-diff (after viper-vc-ad activate)
  764. "Force Vi state in VC diff buffer."
  765. (viper-change-state-to-vi)))
  766. (eval-after-load
  767. "emerge"
  768. '(defadvice emerge-quit (after viper-emerge-advice activate)
  769. "Run `viper-change-state-to-vi' after quitting emerge."
  770. (viper-change-state-to-vi)))
  771. ;; passwd.el sets up its own buffer, which turns up in Vi mode,
  772. ;; thus overriding the local map. We don't need Vi mode here.
  773. (eval-after-load
  774. "passwd"
  775. '(defadvice read-passwd-1 (before viper-passwd-ad activate)
  776. "Switch to Emacs state while reading password."
  777. (viper-change-state-to-emacs)))
  778. (defadvice self-insert-command (around viper-self-insert-ad activate)
  779. "Ignore all self-inserting keys in the vi-state."
  780. (if (and (eq viper-current-state 'vi-state)
  781. ;; Do not use called-interactively-p here. XEmacs does not have it
  782. ;; and interactive-p is just fine.
  783. ;; (called-interactively-p 'interactive))
  784. (interactive-p))
  785. (beep 1)
  786. ad-do-it
  787. ))
  788. (defadvice set-cursor-color (after viper-set-cursor-color-ad activate)
  789. "Change cursor color in VI state."
  790. (modify-frame-parameters
  791. (selected-frame)
  792. (list (cons 'viper-vi-state-cursor-color (ad-get-arg 0))))
  793. (setq viper-vi-state-cursor-color (ad-get-arg 0))
  794. )
  795. (when (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  796. ;; needs to be as early as possible
  797. (add-to-ordered-list
  798. 'emulation-mode-map-alists 'viper--intercept-key-maps 100)
  799. ;; needs to be after cua-mode
  800. (add-to-ordered-list 'emulation-mode-map-alists 'viper--key-maps 500)
  801. )
  802. ;; Emacs shell, ange-ftp, and comint-based modes
  803. (add-hook 'comint-mode-hook 'viper-comint-mode-hook) ; comint
  804. (add-hook 'erc-mode-hook 'viper-comint-mode-hook) ; ERC
  805. (add-hook 'eshell-mode-hook
  806. (lambda () (setq viper-auto-indent nil)))
  807. (viper-set-emacs-state-searchstyle-macros nil 'dired-mode) ; dired
  808. (viper-set-emacs-state-searchstyle-macros nil 'tar-mode) ; tar
  809. (viper-set-emacs-state-searchstyle-macros nil 'mh-folder-mode) ; mhe
  810. (viper-set-emacs-state-searchstyle-macros nil 'gnus-group-mode) ; gnus
  811. (viper-set-emacs-state-searchstyle-macros nil 'gnus-summary-mode)
  812. (viper-set-emacs-state-searchstyle-macros nil 'Info-mode) ; info
  813. (viper-set-emacs-state-searchstyle-macros nil 'Buffer-menu-mode) ;buffer-menu
  814. ;; Modify major modes according to viper-major-mode-modifier-list
  815. (viper-apply-major-mode-modifiers)
  816. ;; For RMAIL users.
  817. ;; Put buf in Emacs state after edit.
  818. (eval-after-load
  819. "rmailedit"
  820. '(defadvice rmail-cease-edit (after viper-rmail-advice activate)
  821. "Switch to Emacs state when done editing message."
  822. (viper-change-state-to-emacs)))
  823. ;; ISO accents
  824. ;; Need to do it after loading iso-acc, or else this loading will wipe out
  825. ;; the advice.
  826. (eval-after-load
  827. "iso-acc"
  828. '(defadvice iso-accents-mode (around viper-iso-accents-advice activate)
  829. "Set viper-automatic-iso-accents to iso-accents-mode."
  830. (let ((arg (ad-get-arg 0)))
  831. ad-do-it
  832. (setq viper-automatic-iso-accents
  833. (if (eq viper-current-state 'vi-state)
  834. (if arg
  835. ;; if iso-accents-mode was called with positive arg, turn
  836. ;; accents on
  837. (> (prefix-numeric-value arg) 0)
  838. ;; else: toggle viper-automatic-iso-accents
  839. (not viper-automatic-iso-accents))
  840. ;; other states: accept what iso-accents-mode has done
  841. iso-accents-mode))
  842. ;; turn off ISO accents in vi-state
  843. (if (eq viper-current-state 'vi-state)
  844. (viper-set-iso-accents-mode nil))
  845. (if (memq viper-current-state '(vi-state insert-state replace-state))
  846. (message "Viper ISO accents mode: %s"
  847. (if viper-automatic-iso-accents "on" "off")))
  848. )))
  849. ;; International input methods
  850. (if (featurep 'emacs)
  851. (eval-after-load "mule-cmds"
  852. '(progn
  853. (defadvice inactivate-input-method (after viper-mule-advice activate)
  854. "Set viper-special-input-method to disable intl. input methods."
  855. (viper-inactivate-input-method-action))
  856. (defadvice activate-input-method (after viper-mule-advice activate)
  857. "Set viper-special-input-method to enable intl. input methods."
  858. (viper-activate-input-method-action))
  859. ))
  860. ;; XEmacs Although these hooks exist in Emacs, they don't seem to be always
  861. ;; called on input-method activation/deactivation, so we the above advise
  862. ;; functions instead.
  863. (eval-after-load "mule-cmds"
  864. '(progn
  865. (add-hook 'input-method-activate-hook
  866. 'viper-activate-input-method-action t)
  867. (add-hook 'input-method-inactivate-hook
  868. 'viper-inactivate-input-method-action t)))
  869. )
  870. (eval-after-load "mule-cmds"
  871. '(defadvice toggle-input-method (around viper-mule-advice activate)
  872. "Adjust input-method toggling in vi-state."
  873. (if (and viper-special-input-method (eq viper-current-state 'vi-state))
  874. (viper-inactivate-input-method)
  875. ad-do-it)))
  876. ) ; viper-set-hooks
  877. ;; these are primarily advices and Vi-ish variable settings
  878. (defun viper-non-hook-settings ()
  879. ;;;; Viper changes the default mode-line-buffer-identification
  880. ;;(setq-default mode-line-buffer-identification '(" %b"))
  881. ;; setup emacs-supported vi-style feel
  882. (setq next-line-add-newlines nil
  883. require-final-newline t)
  884. ;; don't bark when mark is inactive
  885. (if (featurep 'emacs)
  886. (setq mark-even-if-inactive t))
  887. (setq scroll-step 1)
  888. ;; Variable displaying the current Viper state in the mode line.
  889. (or (memq 'viper-mode-string global-mode-string)
  890. (setq global-mode-string
  891. (append '("" viper-mode-string) (cdr global-mode-string))))
  892. (if (featurep 'xemacs)
  893. ;; XEmacs
  894. (defadvice describe-key (before viper-describe-key-ad protect activate)
  895. "Force to read key via `viper-read-key-sequence'."
  896. (interactive (list (viper-read-key-sequence "Describe key: "))))
  897. ;; Emacs
  898. (defadvice describe-key (before viper-describe-key-ad protect activate)
  899. "Force to read key via `viper-read-key-sequence'."
  900. (interactive (let (key)
  901. (setq key (viper-read-key-sequence
  902. "Describe key (or click or menu item): "))
  903. (list key
  904. (prefix-numeric-value current-prefix-arg)
  905. ;; If KEY is a down-event, read also the
  906. ;; corresponding up-event.
  907. (and (vectorp key)
  908. (let ((last-idx (1- (length key))))
  909. (and (eventp (aref key last-idx))
  910. (memq 'down (event-modifiers
  911. (aref key last-idx)))))
  912. (or (and (eventp (aref key 0))
  913. (memq 'down (event-modifiers
  914. (aref key 0)))
  915. ;; For the C-down-mouse-2 popup menu,
  916. ;; there is no subsequent up-event
  917. (= (length key) 1))
  918. (and (> (length key) 1)
  919. (eventp (aref key 1))
  920. (memq 'down (event-modifiers (aref key 1)))))
  921. (read-event))))))
  922. ) ; (if (featurep 'xemacs)
  923. (if (featurep 'xemacs)
  924. ;; XEmacs
  925. (defadvice describe-key-briefly
  926. (before viper-describe-key-briefly-ad protect activate)
  927. "Force to read key via `viper-read-key-sequence'."
  928. (interactive (list (viper-read-key-sequence "Describe key briefly: "))))
  929. ;; Emacs
  930. (defadvice describe-key-briefly
  931. (before viper-describe-key-briefly-ad protect activate)
  932. "Force to read key via `viper-read-key-sequence'."
  933. (interactive (let (key)
  934. (setq key (viper-read-key-sequence
  935. "Describe key (or click or menu item): "))
  936. ;; If KEY is a down-event, read and discard the
  937. ;; corresponding up-event.
  938. (and (vectorp key)
  939. (let ((last-idx (1- (length key))))
  940. (and (eventp (aref key last-idx))
  941. (memq 'down (event-modifiers (aref key last-idx)))))
  942. (read-event))
  943. (list key
  944. (if current-prefix-arg
  945. (prefix-numeric-value current-prefix-arg))
  946. 1))))
  947. ) ; (if (featurep 'xemacs)
  948. (defadvice find-file (before viper-add-suffix-advice activate)
  949. "Use `read-file-name' for reading arguments."
  950. (interactive (cons (read-file-name "Find file: " nil default-directory)
  951. ;; XEmacs: if Mule & prefix arg, ask for coding system
  952. (cond ((and (featurep 'xemacs) (featurep 'mule))
  953. (list
  954. (and current-prefix-arg
  955. (read-coding-system "Coding-system: "))))
  956. ;; Emacs: do wildcards
  957. ((and (featurep 'emacs) (boundp 'find-file-wildcards))
  958. (list find-file-wildcards))))
  959. ))
  960. (defadvice find-file-other-window (before viper-add-suffix-advice activate)
  961. "Use `read-file-name' for reading arguments."
  962. (interactive (cons (read-file-name "Find file in other window: "
  963. nil default-directory)
  964. ;; XEmacs: if Mule & prefix arg, ask for coding system
  965. (cond ((and (featurep 'xemacs) (featurep 'mule))
  966. (list
  967. (and current-prefix-arg
  968. (read-coding-system "Coding-system: "))))
  969. ;; Emacs: do wildcards
  970. ((and (featurep 'emacs) (boundp 'find-file-wildcards))
  971. (list find-file-wildcards))))
  972. ))
  973. (defadvice find-file-other-frame (before viper-add-suffix-advice activate)
  974. "Use `read-file-name' for reading arguments."
  975. (interactive (cons (read-file-name "Find file in other frame: "
  976. nil default-directory)
  977. ;; XEmacs: if Mule & prefix arg, ask for coding system
  978. (cond ((and (featurep 'xemacs) (featurep 'mule))
  979. (list
  980. (and current-prefix-arg
  981. (read-coding-system "Coding-system: "))))
  982. ;; Emacs: do wildcards
  983. ((and (featurep 'emacs) (boundp 'find-file-wildcards))
  984. (list find-file-wildcards))))
  985. ))
  986. (defadvice read-file-name (around viper-suffix-advice activate)
  987. "Tell `exit-minibuffer' to run `viper-file-add-suffix' as a hook."
  988. (let ((viper-minibuffer-exit-hook
  989. (append viper-minibuffer-exit-hook
  990. '(viper-minibuffer-trim-tail viper-file-add-suffix))))
  991. ad-do-it))
  992. (defadvice start-kbd-macro (after viper-kbd-advice activate)
  993. "Remove Viper's intercepting bindings for C-x ).
  994. This may be needed if the previous `:map' command terminated abnormally."
  995. (define-key viper-vi-intercept-map "\C-x)" nil)
  996. (define-key viper-insert-intercept-map "\C-x)" nil)
  997. (define-key viper-emacs-intercept-map "\C-x)" nil))
  998. (defadvice add-minor-mode (after
  999. viper-advice-add-minor-mode
  1000. (toggle name &optional keymap after toggle-fun)
  1001. activate)
  1002. "Run viper-normalize-minor-mode-map-alist after adding a minor mode."
  1003. (viper-normalize-minor-mode-map-alist)
  1004. (unless
  1005. (and (fboundp 'add-to-ordered-list) (boundp 'emulation-mode-map-alists))
  1006. (setq-default minor-mode-map-alist minor-mode-map-alist)))
  1007. ;; catch frame switching event
  1008. (if (viper-window-display-p)
  1009. (if (featurep 'xemacs)
  1010. (add-hook 'mouse-leave-frame-hook
  1011. 'viper-remember-current-frame)
  1012. (defadvice handle-switch-frame (before viper-frame-advice activate)
  1013. "Remember the selected frame before the switch-frame event."
  1014. (viper-remember-current-frame (selected-frame)))) )
  1015. ) ; end viper-non-hook-settings
  1016. ;; Ask only if this-command/last-command are nil, i.e., when loading
  1017. (cond ((and (eq viper-mode 'ask) (null this-command) (null last-command))
  1018. (save-window-excursion
  1019. (with-output-to-temp-buffer " *viper-info*"
  1020. (princ "
  1021. You have loaded Viper, and are about to Viperize your Emacs!
  1022. Viper is a Package for Emacs Rebels and a venomous VI PERil,
  1023. It's time to decide: to Viperize or not to Viperize...
  1024. If you wish to Viperize AND make this your way of life, please put
  1025. (setq viper-mode t)
  1026. (require 'viper)
  1027. in your .emacs file (preferably, close to the top).
  1028. These two lines must come in the order given.
  1029. ** Viper users:
  1030. **** The startup file name has been changed from .vip to .viper
  1031. **** All vip-* style names have been converted to viper-* style."))
  1032. (if (y-or-n-p "Viperize? ")
  1033. (setq viper-mode t)
  1034. (setq viper-mode nil))
  1035. (message "")
  1036. (kill-buffer " *viper-info*")))
  1037. ;; If viper-mode is t, then just continue. Viper will kick in.
  1038. ((eq viper-mode t))
  1039. ;; Otherwise, it was asking Viper was not loaded through .emacs
  1040. ;; In this case, it was either through M-x viper-mode or via something
  1041. ;; else, like the custom widget. If Viper was loaded through
  1042. ;; M-x viper-mode, then viper will kick in anyway.
  1043. (t (setq viper-mode nil)))
  1044. (defun viper-load-custom-file ()
  1045. (if (and (file-exists-p viper-custom-file-name)
  1046. (not noninteractive))
  1047. (load viper-custom-file-name)))
  1048. ;; save non-viper vars that Viper might change
  1049. (if (null viper-saved-non-viper-variables)
  1050. (setq viper-saved-non-viper-variables
  1051. (list
  1052. (cons 'default-major-mode (list (default-value 'major-mode)))
  1053. (cons 'next-line-add-newlines (list next-line-add-newlines))
  1054. (cons 'require-final-newline (list require-final-newline))
  1055. (cons 'scroll-step (list scroll-step))
  1056. (cons 'mode-line-buffer-identification
  1057. (list (default-value 'mode-line-buffer-identification)))
  1058. (cons 'global-mode-string (list global-mode-string))
  1059. (if (featurep 'emacs)
  1060. (cons 'mark-even-if-inactive (list mark-even-if-inactive)))
  1061. )))
  1062. ;; Set some useful macros, advices
  1063. ;; These must be BEFORE ~/.viper is loaded,
  1064. ;; so the user can unrecord them in ~/.viper.
  1065. (if viper-mode
  1066. (progn
  1067. ;; set advices and some variables that give emacs Vi look.
  1068. (viper-non-hook-settings)
  1069. ;; repeat the 2nd previous command without rotating the command history
  1070. (viper-record-kbd-macro
  1071. (vector viper-repeat-from-history-key '\1) 'vi-state
  1072. [(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)
  1073. ;; repeat the 3d previous command without rotating the command history
  1074. (viper-record-kbd-macro
  1075. (vector viper-repeat-from-history-key '\2) 'vi-state
  1076. [(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)
  1077. ;; set macros for toggling case sensitivity and regexp search
  1078. (viper-set-searchstyle-toggling-macros nil)
  1079. ;; Make %%% toggle parsing comments for matching parentheses
  1080. (viper-set-parsing-style-toggling-macro nil)
  1081. ;; ~/.viper is loaded if exists
  1082. (viper-load-custom-file)
  1083. ;; should be after loading custom file to avoid the pesky msg that
  1084. ;; mouse-search/insert keys are already bound
  1085. (viper-bind-mouse-search-key)
  1086. (viper-bind-mouse-insert-key)
  1087. ))
  1088. ;; Applying Viper customization -- runs after (load .viper)
  1089. ;; Save user settings or Viper defaults for vars controlled by
  1090. ;; viper-expert-level
  1091. (if (null viper-saved-user-settings)
  1092. (setq viper-saved-user-settings
  1093. (list (cons 'viper-want-ctl-h-help (list viper-want-ctl-h-help))
  1094. (cons 'viper-always (list viper-always))
  1095. (cons 'viper-no-multiple-ESC (list viper-no-multiple-ESC))
  1096. (cons 'viper-ex-style-motion (list viper-ex-style-motion))
  1097. (cons 'viper-ex-style-editing
  1098. (list viper-ex-style-editing))
  1099. (cons 'viper-want-emacs-keys-in-vi
  1100. (list viper-want-emacs-keys-in-vi))
  1101. (cons 'viper-electric-mode (list viper-electric-mode))
  1102. (cons 'viper-want-emacs-keys-in-insert
  1103. (list viper-want-emacs-keys-in-insert))
  1104. (cons 'viper-re-search (list viper-re-search)))))
  1105. (if viper-mode
  1106. (progn
  1107. (viper-set-minibuffer-style)
  1108. (if viper-buffer-search-char
  1109. (viper-buffer-search-enable))
  1110. (viper-update-syntax-classes 'set-default)
  1111. ))
  1112. ;;; Familiarize Viper with some minor modes that have their own keymaps
  1113. (if viper-mode
  1114. (progn
  1115. (viper-harness-minor-mode "compile")
  1116. (viper-harness-minor-mode "outline")
  1117. (viper-harness-minor-mode "allout")
  1118. (viper-harness-minor-mode "xref")
  1119. (viper-harness-minor-mode "lmenu")
  1120. (viper-harness-minor-mode "vc")
  1121. (viper-harness-minor-mode "ltx-math") ; LaTeX-math-mode in AUC-TeX, which
  1122. (viper-harness-minor-mode "latex") ; sits in one of these two files
  1123. (viper-harness-minor-mode "cyrillic")
  1124. (viper-harness-minor-mode "russian")
  1125. (viper-harness-minor-mode "view-less")
  1126. (viper-harness-minor-mode "view")
  1127. (viper-harness-minor-mode "reftex")
  1128. (viper-harness-minor-mode "flyspell")
  1129. ))
  1130. ;; Intercept maps could go in viper-keym.el
  1131. ;; We keep them here in case someone redefines them in ~/.viper
  1132. (define-key viper-vi-intercept-map viper-ESC-key 'viper-intercept-ESC-key)
  1133. (define-key viper-insert-intercept-map viper-ESC-key 'viper-intercept-ESC-key)
  1134. ;; This is taken care of by viper-insert-global-user-map.
  1135. ;;(define-key viper-replace-map viper-ESC-key 'viper-intercept-ESC-key)
  1136. ;; The default viper-toggle-key is \C-z; for the novice, it suspends or
  1137. ;; iconifies Emacs
  1138. (define-key viper-vi-intercept-map viper-toggle-key 'viper-toggle-key-action)
  1139. (define-key
  1140. viper-emacs-intercept-map viper-toggle-key 'viper-change-state-to-vi)
  1141. ;;; Removed to avoid bad interaction with cua-mode.
  1142. ;;; Escape from Emacs and Insert modes to Vi for one command
  1143. ;;(define-key viper-emacs-intercept-map "\C-c\\" 'viper-escape-to-vi)
  1144. ;;(define-key viper-insert-intercept-map "\C-c\\" 'viper-escape-to-vi)
  1145. (if viper-mode
  1146. (setq-default viper-emacs-intercept-minor-mode t
  1147. viper-emacs-local-user-minor-mode t
  1148. viper-emacs-global-user-minor-mode t
  1149. viper-emacs-kbd-minor-mode t
  1150. viper-emacs-state-modifier-minor-mode t))
  1151. (if (and viper-mode (eq viper-current-state 'emacs-state))
  1152. (setq viper-emacs-intercept-minor-mode t
  1153. viper-emacs-local-user-minor-mode t
  1154. viper-emacs-global-user-minor-mode t
  1155. viper-emacs-kbd-minor-mode t
  1156. viper-emacs-state-modifier-minor-mode t))
  1157. (if (and viper-mode
  1158. (or viper-always
  1159. (and (< viper-expert-level 5) (> viper-expert-level 0))))
  1160. (viper-set-hooks))
  1161. ;; Let all minor modes take effect after loading.
  1162. ;; This may not be enough, so we also set default minor-mode-alist.
  1163. ;; Without setting the default, new buffers that come up in emacs mode have
  1164. ;; minor-mode-map-alist = nil, unless we call viper-change-state-*
  1165. (if (and viper-mode (eq viper-current-state 'emacs-state))
  1166. (progn
  1167. (viper-change-state-to-emacs)
  1168. (unless
  1169. (and (fboundp 'add-to-ordered-list)
  1170. (boundp 'emulation-mode-map-alists))
  1171. (setq-default minor-mode-map-alist minor-mode-map-alist))
  1172. ))
  1173. (if (and viper-mode (this-major-mode-requires-vi-state major-mode))
  1174. (viper-mode))
  1175. (if viper-mode
  1176. (setq initial-major-mode
  1177. `(lambda ()
  1178. (funcall (quote ,initial-major-mode))
  1179. (set-viper-state-in-major-mode))
  1180. ))
  1181. (run-hooks 'viper-load-hook) ; the last chance to change something
  1182. (provide 'viper)
  1183. ;; Local Variables:
  1184. ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
  1185. ;; End:
  1186. ;;; viper.el ends here