viper.el 53 KB

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