vip.el 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059
  1. ;;; vip.el --- a VI Package for GNU Emacs
  2. ;; Copyright (C) 1986-1988, 1992-1993, 1998, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Masahiko Sato <ms@sail.stanford.edu>
  5. ;; Keywords: emulations
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; A full-featured vi(1) emulator.
  19. ;;
  20. ;; In Japan, the author's address is: masahiko@sato.riec.tohoku.junet
  21. ;;
  22. ;; Send suggestions and bug reports to one of the above addresses.
  23. ;; When you report a bug, be sure to include the version number of VIP and
  24. ;; Emacs you are using.
  25. ;; Execute info command by typing "M-x info" to get information on VIP.
  26. ;;; Code:
  27. (defgroup vip nil
  28. "A VI Package for GNU Emacs."
  29. :prefix "vip-"
  30. :group 'emulations)
  31. ;; external variables
  32. (defvar vip-emacs-local-map nil
  33. "Local map used in Emacs mode. (Buffer-specific.)")
  34. (defvar vip-insert-local-map nil
  35. "Local map used in insert command mode. (Buffer-specific.)")
  36. (make-variable-buffer-local 'vip-emacs-local-map)
  37. (make-variable-buffer-local 'vip-insert-local-map)
  38. (defvar vip-insert-point nil
  39. "Remember insert point as a marker. (Buffer-specific.)")
  40. (set-default 'vip-insert-point (make-marker))
  41. (make-variable-buffer-local 'vip-insert-point)
  42. (defvar vip-com-point nil
  43. "Remember com point as a marker. (Buffer-specific.)")
  44. (set-default 'vip-com-point (make-marker))
  45. (make-variable-buffer-local 'vip-com-point)
  46. (defvar vip-current-mode nil
  47. "Current mode. One of `emacs-mode', `vi-mode', `insert-mode'.")
  48. (make-variable-buffer-local 'vip-current-mode)
  49. (setq-default vip-current-mode 'emacs-mode)
  50. (defvar vip-emacs-mode-line-buffer-identification nil
  51. "Value of mode-line-buffer-identification in Emacs mode within vip.")
  52. (make-variable-buffer-local 'vip-emacs-mode-line-buffer-identification)
  53. (setq-default vip-emacs-mode-line-buffer-identification
  54. '("Emacs: %17b"))
  55. (defvar vip-current-major-mode nil
  56. "vip-current-major-mode is the major-mode vi considers it is now.
  57. \(buffer specific\)")
  58. (make-variable-buffer-local 'vip-current-major-mode)
  59. (defvar vip-last-shell-com nil
  60. "Last shell command executed by ! command.")
  61. (defvar vip-use-register nil
  62. "Name of register to store deleted or yanked strings.")
  63. (defvar vip-d-com nil
  64. "How to reexecute last destructive command. Value is list (M-COM VAL COM).")
  65. (defcustom vip-shift-width 8
  66. "The number of columns shifted by > and < command."
  67. :type 'integer
  68. :group 'vip)
  69. (defcustom vip-re-replace nil
  70. "If t then do regexp replace, if nil then do string replace."
  71. :type 'boolean
  72. :group 'vip)
  73. (defvar vip-d-char nil
  74. "The character remembered by the vi \"r\" command.")
  75. (defvar vip-f-char nil
  76. "For use by \";\" command.")
  77. (defvar vip-F-char nil
  78. "For use by \".\" command.")
  79. (defvar vip-f-forward nil
  80. "For use by \";\" command.")
  81. (defvar vip-f-offset nil
  82. "For use by \";\" command.")
  83. (defcustom vip-search-wrap-around t
  84. "If t, search wraps around."
  85. :type 'boolean
  86. :group 'vip)
  87. (defcustom vip-re-search nil
  88. "If t, search is reg-exp search, otherwise vanilla search."
  89. :type 'boolean
  90. :group 'vip)
  91. (defvar vip-s-string nil
  92. "Last vip search string.")
  93. (defvar vip-s-forward nil
  94. "If t, search is forward.")
  95. (defcustom vip-case-fold-search nil
  96. "If t, search ignores cases."
  97. :type 'boolean
  98. :group 'vip)
  99. (defcustom vip-re-query-replace nil
  100. "If t then do regexp replace, if nil then do string replace."
  101. :type 'boolean
  102. :group 'vip)
  103. (defcustom vip-open-with-indent nil
  104. "If t, indent when open a new line."
  105. :type 'boolean
  106. :group 'vip)
  107. (defcustom vip-help-in-insert-mode nil
  108. "If t then C-h is bound to help-command in insert mode.
  109. If nil then it is bound to `delete-backward-char'."
  110. :type 'boolean
  111. :group 'vip)
  112. (defvar vip-quote-string "> "
  113. "String inserted at the beginning of region.")
  114. (defvar vip-tags-file-name "TAGS")
  115. (defvar vip-inhibit-startup-message nil)
  116. (defvar vip-startup-file (convert-standard-filename "~/.vip")
  117. "Filename used as startup file for vip.")
  118. ;; key bindings
  119. (defvar vip-mode-map
  120. (let ((map (make-keymap)))
  121. (define-key map "\C-a" 'beginning-of-line)
  122. (define-key map "\C-b" 'vip-scroll-back)
  123. (define-key map "\C-c" 'vip-ctl-c)
  124. (define-key map "\C-d" 'vip-scroll-up)
  125. (define-key map "\C-e" 'vip-scroll-up-one)
  126. (define-key map "\C-f" 'vip-scroll)
  127. (define-key map "\C-g" 'vip-keyboard-quit)
  128. (define-key map "\C-h" 'help-command)
  129. (define-key map "\C-m" 'vip-scroll-back)
  130. (define-key map "\C-n" 'vip-other-window)
  131. (define-key map "\C-o" 'vip-open-line-at-point)
  132. (define-key map "\C-u" 'vip-scroll-down)
  133. (define-key map "\C-x" 'vip-ctl-x)
  134. (define-key map "\C-y" 'vip-scroll-down-one)
  135. (define-key map "\C-z" 'vip-change-mode-to-emacs)
  136. (define-key map "\e" 'vip-ESC)
  137. (define-key map " " 'vip-scroll)
  138. (define-key map "!" 'vip-command-argument)
  139. (define-key map "\"" 'vip-command-argument)
  140. (define-key map "#" 'vip-command-argument)
  141. (define-key map "$" 'vip-goto-eol)
  142. (define-key map "%" 'vip-paren-match)
  143. (define-key map "&" 'vip-nil)
  144. (define-key map "'" 'vip-goto-mark-and-skip-white)
  145. (define-key map "(" 'vip-backward-sentence)
  146. (define-key map ")" 'vip-forward-sentence)
  147. (define-key map "*" 'call-last-kbd-macro)
  148. (define-key map "+" 'vip-next-line-at-bol)
  149. (define-key map "," 'vip-repeat-find-opposite)
  150. (define-key map "-" 'vip-previous-line-at-bol)
  151. (define-key map "." 'vip-repeat)
  152. (define-key map "/" 'vip-search-forward)
  153. (define-key map "0" 'vip-beginning-of-line)
  154. (define-key map "1" 'vip-digit-argument)
  155. (define-key map "2" 'vip-digit-argument)
  156. (define-key map "3" 'vip-digit-argument)
  157. (define-key map "4" 'vip-digit-argument)
  158. (define-key map "5" 'vip-digit-argument)
  159. (define-key map "6" 'vip-digit-argument)
  160. (define-key map "7" 'vip-digit-argument)
  161. (define-key map "8" 'vip-digit-argument)
  162. (define-key map "9" 'vip-digit-argument)
  163. (define-key map ":" 'vip-ex)
  164. (define-key map ";" 'vip-repeat-find)
  165. (define-key map "<" 'vip-command-argument)
  166. (define-key map "=" 'vip-command-argument)
  167. (define-key map ">" 'vip-command-argument)
  168. (define-key map "?" 'vip-search-backward)
  169. (define-key map "@" 'vip-nil)
  170. (define-key map "A" 'vip-Append)
  171. (define-key map "B" 'vip-backward-Word)
  172. (define-key map "C" 'vip-ctl-c-equivalent)
  173. (define-key map "D" 'vip-kill-line)
  174. (define-key map "E" 'vip-end-of-Word)
  175. (define-key map "F" 'vip-find-char-backward)
  176. (define-key map "G" 'vip-goto-line)
  177. (define-key map "H" 'vip-window-top)
  178. (define-key map "I" 'vip-Insert)
  179. (define-key map "J" 'vip-join-lines)
  180. (define-key map "K" 'vip-kill-buffer)
  181. (define-key map "L" 'vip-window-bottom)
  182. (define-key map "M" 'vip-window-middle)
  183. (define-key map "N" 'vip-search-Next)
  184. (define-key map "O" 'vip-Open-line)
  185. (define-key map "P" 'vip-Put-back)
  186. (define-key map "Q" 'vip-query-replace)
  187. (define-key map "R" 'vip-replace-string)
  188. (define-key map "S" 'vip-switch-to-buffer-other-window)
  189. (define-key map "T" 'vip-goto-char-backward)
  190. (define-key map "U" 'vip-nil)
  191. (define-key map "V" 'vip-find-file-other-window)
  192. (define-key map "W" 'vip-forward-Word)
  193. (define-key map "X" 'vip-ctl-x-equivalent)
  194. (define-key map "Y" 'vip-yank-line)
  195. (define-key map "ZZ" 'save-buffers-kill-emacs)
  196. (define-key map "[" 'vip-nil)
  197. (define-key map "\\" 'vip-escape-to-emacs)
  198. (define-key map "]" 'vip-nil)
  199. (define-key map "^" 'vip-bol-and-skip-white)
  200. (define-key map "_" 'vip-nil)
  201. (define-key map "`" 'vip-goto-mark)
  202. (define-key map "a" 'vip-append)
  203. (define-key map "b" 'vip-backward-word)
  204. (define-key map "c" 'vip-command-argument)
  205. (define-key map "d" 'vip-command-argument)
  206. (define-key map "e" 'vip-end-of-word)
  207. (define-key map "f" 'vip-find-char-forward)
  208. (define-key map "g" 'vip-info-on-file)
  209. (define-key map "h" 'vip-backward-char)
  210. (define-key map "i" 'vip-insert)
  211. (define-key map "j" 'vip-next-line)
  212. (define-key map "k" 'vip-previous-line)
  213. (define-key map "l" 'vip-forward-char)
  214. (define-key map "m" 'vip-mark-point)
  215. (define-key map "n" 'vip-search-next)
  216. (define-key map "o" 'vip-open-line)
  217. (define-key map "p" 'vip-put-back)
  218. (define-key map "q" 'vip-nil)
  219. (define-key map "r" 'vip-replace-char)
  220. (define-key map "s" 'vip-switch-to-buffer)
  221. (define-key map "t" 'vip-goto-char-forward)
  222. (define-key map "u" 'vip-undo)
  223. (define-key map "v" 'vip-find-file)
  224. (define-key map "w" 'vip-forward-word)
  225. (define-key map "x" 'vip-delete-char)
  226. (define-key map "y" 'vip-command-argument)
  227. (define-key map "zH" 'vip-line-to-top)
  228. (define-key map "zM" 'vip-line-to-middle)
  229. (define-key map "zL" 'vip-line-to-bottom)
  230. (define-key map "z\C-m" 'vip-line-to-top)
  231. (define-key map "z." 'vip-line-to-middle)
  232. (define-key map "z-" 'vip-line-to-bottom)
  233. (define-key map "{" 'vip-backward-paragraph)
  234. (define-key map "|" 'vip-goto-col)
  235. (define-key map "}" 'vip-forward-paragraph)
  236. (define-key map "~" 'vip-nil)
  237. (define-key map "\177" 'vip-delete-backward-char)
  238. map))
  239. (defun vip-version ()
  240. (interactive)
  241. (message "VIP version 3.5 of September 15, 1987"))
  242. ;; basic set up
  243. ;;;###autoload
  244. (defun vip-setup ()
  245. "Set up bindings for C-x 7 and C-z that are useful for VIP users."
  246. (define-key ctl-x-map "7" 'vip-buffer-in-two-windows)
  247. (global-set-key "\C-z" 'vip-change-mode-to-vi))
  248. (defmacro vip-loop (count body)
  249. "(COUNT BODY) Execute BODY COUNT times."
  250. (list 'let (list (list 'count count))
  251. (list 'while (list '> 'count 0)
  252. body
  253. (list 'setq 'count (list '1- 'count)))))
  254. (defun vip-push-mark-silent (&optional location)
  255. "Set mark at LOCATION (point, by default) and push old mark on mark ring.
  256. No message."
  257. (if (null (mark t))
  258. nil
  259. (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
  260. (if (> (length mark-ring) mark-ring-max)
  261. (progn
  262. (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
  263. (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
  264. (set-mark (or location (point))))
  265. (defun vip-goto-col (arg)
  266. "Go to ARG's column."
  267. (interactive "P")
  268. (let ((val (vip-p-val arg))
  269. (com (vip-getcom arg)))
  270. (save-excursion
  271. (end-of-line)
  272. (if (> val (1+ (current-column))) (error "")))
  273. (if com (move-marker vip-com-point (point)))
  274. (beginning-of-line)
  275. (forward-char (1- val))
  276. (if com (vip-execute-com 'vip-goto-col val com))))
  277. (defun vip-copy-keymap (map)
  278. (if (null map) (make-sparse-keymap) (copy-keymap map)))
  279. ;; changing mode
  280. (defun vip-change-mode (new-mode)
  281. "Change mode to NEW-MODE---either emacs-mode, vi-mode, or insert-mode."
  282. (or (eq new-mode vip-current-mode)
  283. (progn
  284. (cond ((eq new-mode 'vi-mode)
  285. (if (eq vip-current-mode 'insert-mode)
  286. (progn
  287. (vip-copy-region-as-kill (point) vip-insert-point)
  288. (vip-repeat-insert-command))
  289. (setq vip-emacs-local-map (current-local-map)
  290. vip-emacs-mode-line-buffer-identification
  291. mode-line-buffer-identification
  292. vip-insert-local-map (vip-copy-keymap
  293. (current-local-map))))
  294. (vip-change-mode-line "Vi: ")
  295. (use-local-map vip-mode-map))
  296. ((eq new-mode 'insert-mode)
  297. (move-marker vip-insert-point (point))
  298. (if (eq vip-current-mode 'emacs-mode)
  299. (setq vip-emacs-local-map (current-local-map)
  300. vip-emacs-mode-line-buffer-identification
  301. mode-line-buffer-identification
  302. vip-insert-local-map (vip-copy-keymap
  303. (current-local-map)))
  304. (setq vip-insert-local-map (vip-copy-keymap
  305. vip-emacs-local-map)))
  306. (vip-change-mode-line "Insert")
  307. (use-local-map vip-insert-local-map)
  308. (define-key vip-insert-local-map "\e" 'vip-change-mode-to-vi)
  309. (define-key vip-insert-local-map "\C-z" 'vip-ESC)
  310. (define-key vip-insert-local-map "\C-h"
  311. (if vip-help-in-insert-mode 'help-command
  312. 'delete-backward-char))
  313. (define-key vip-insert-local-map "\C-w"
  314. 'vip-delete-backward-word))
  315. ((eq new-mode 'emacs-mode)
  316. (vip-change-mode-line "Emacs:")
  317. (use-local-map vip-emacs-local-map)))
  318. (setq vip-current-mode new-mode)
  319. (force-mode-line-update))))
  320. (defun vip-copy-region-as-kill (beg end)
  321. "If BEG and END do not belong to the same buffer, it copies empty region."
  322. (condition-case nil
  323. (copy-region-as-kill beg end)
  324. (error (copy-region-as-kill beg beg))))
  325. (defun vip-change-mode-line (string)
  326. "Assuming that the mode line format contains the string \"Emacs:\", this
  327. function replaces the string by \"Vi: \" etc."
  328. (setq mode-line-buffer-identification
  329. (if (string= string "Emacs:")
  330. vip-emacs-mode-line-buffer-identification
  331. (list (concat string " %17b")))))
  332. ;;;###autoload
  333. (defun vip-mode ()
  334. "Turn on VIP emulation of VI."
  335. (interactive)
  336. (if (not vip-inhibit-startup-message)
  337. (progn
  338. (switch-to-buffer "VIP Startup Message")
  339. (erase-buffer)
  340. (insert
  341. "VIP is a Vi emulation package for GNU Emacs. VIP provides most Vi commands
  342. including Ex commands. VIP is however different from Vi in several points.
  343. You can get more information on VIP by:
  344. 1. Typing `M-x info' and selecting menu item \"vip\".
  345. 2. Typing `C-h k' followed by a key whose description you want.
  346. 3. Printing VIP manual which can be found as GNU/man/vip.texinfo
  347. 4. Printing VIP Reference Card which can be found as GNU/etc/vipcard.tex
  348. This startup message appears whenever you load VIP unless you type `y' now.
  349. Type `n' to quit this window for now.\n")
  350. (goto-char (point-min))
  351. (if (y-or-n-p "Inhibit VIP startup message? ")
  352. (progn
  353. (with-current-buffer
  354. (find-file-noselect
  355. (substitute-in-file-name vip-startup-file))
  356. (goto-char (point-max))
  357. (insert "\n(setq vip-inhibit-startup-message t)\n")
  358. (save-buffer)
  359. (kill-buffer (current-buffer)))
  360. (message "VIP startup message inhibited.")
  361. (sit-for 2)))
  362. (kill-buffer (current-buffer))
  363. (message "")
  364. (setq vip-inhibit-startup-message t)))
  365. (vip-change-mode-to-vi))
  366. (defun vip-change-mode-to-vi ()
  367. "Change mode to vi mode."
  368. (interactive)
  369. (vip-change-mode 'vi-mode))
  370. (defun vip-change-mode-to-insert ()
  371. "Change mode to insert mode."
  372. (interactive)
  373. (vip-change-mode 'insert-mode))
  374. (defun vip-change-mode-to-emacs ()
  375. "Change mode to Emacs mode."
  376. (interactive)
  377. (vip-change-mode 'emacs-mode))
  378. ;; escape to emacs mode temporarily
  379. (defun vip-escape-to-emacs (arg &optional events)
  380. "Escape to Emacs mode for one Emacs command.
  381. ARG is used as the prefix value for the executed command. If
  382. EVENTS is a list of events, which become the beginning of the command."
  383. (interactive "P")
  384. (let (com key (old-map (current-local-map)))
  385. (if events (setq unread-command-events events))
  386. (setq prefix-arg arg)
  387. (use-local-map vip-emacs-local-map)
  388. (unwind-protect
  389. (setq com (key-binding (setq key (read-key-sequence nil))))
  390. (use-local-map old-map))
  391. (command-execute com prefix-arg)
  392. (setq prefix-arg nil) ;; reset prefix arg
  393. ))
  394. (defun vip-message-conditions (conditions)
  395. "Print CONDITIONS as a message."
  396. (let ((case (car conditions)) (msg (cdr conditions)))
  397. (if (null msg)
  398. (message "%s" case)
  399. (message "%s %s" case (prin1-to-string msg)))
  400. (ding)))
  401. (defun vip-ESC (arg)
  402. "Emulate ESC key in Emacs mode."
  403. (interactive "P")
  404. (vip-escape-to-emacs arg '(?\e)))
  405. (defun vip-ctl-c (arg)
  406. "Emulate C-c key in Emacs mode."
  407. (interactive "P")
  408. (vip-escape-to-emacs arg '(?\C-c)))
  409. (defun vip-ctl-x (arg)
  410. "Emulate C-x key in Emacs mode."
  411. (interactive "P")
  412. (vip-escape-to-emacs arg '(?\C-x)))
  413. (defun vip-ctl-h (arg)
  414. "Emulate C-h key in Emacs mode."
  415. (interactive "P")
  416. (vip-escape-to-emacs arg '(?\C-h)))
  417. ;; prefix argument for vi mode
  418. ;; In vi mode, prefix argument is a dotted pair (NUM . COM) where NUM
  419. ;; represents the numeric value of the prefix argument and COM represents
  420. ;; command prefix such as "c", "d", "m" and "y".
  421. (defun vip-prefix-arg-value (char value com)
  422. "Compute numeric prefix arg value. Invoked by CHAR. VALUE is the value
  423. obtained so far, and COM is the command part obtained so far."
  424. (while (and (>= char ?0) (<= char ?9))
  425. (setq value (+ (* (if (numberp value) value 0) 10) (- char ?0)))
  426. (setq char (read-char)))
  427. (setq prefix-arg value)
  428. (if com (setq prefix-arg (cons prefix-arg com)))
  429. (while (= char ?U)
  430. (vip-describe-arg prefix-arg)
  431. (setq char (read-char)))
  432. (setq unread-command-events (list char)))
  433. (defun vip-prefix-arg-com (char value com)
  434. "Vi operator as prefix argument."
  435. (let ((cont t))
  436. (while (and cont
  437. (or (= char ?c) (= char ?d) (= char ?y)
  438. (= char ?!) (= char ?<) (= char ?>) (= char ?=)
  439. (= char ?#) (= char ?r) (= char ?R) (= char ?\")))
  440. (if com
  441. ;; this means that we already have a command character, so we
  442. ;; construct a com list and exit while. however, if char is "
  443. ;; it is an error.
  444. (progn
  445. ;; new com is (CHAR . OLDCOM)
  446. (if (or (= char ?#) (= char ?\")) (error ""))
  447. (setq com (cons char com))
  448. (setq cont nil))
  449. ;; if com is nil we set com as char, and read more. again, if char
  450. ;; is ", we read the name of register and store it in vip-use-register.
  451. ;; if char is !, =, or #, a complete com is formed so we exit while.
  452. (cond ((or (= char ?!) (= char ?=))
  453. (setq com char)
  454. (setq char (read-char))
  455. (setq cont nil))
  456. ((= char ?#)
  457. ;; read a char and encode it as com
  458. (setq com (+ 128 (read-char)))
  459. (setq char (read-char))
  460. (setq cont nil))
  461. ((or (= char ?<) (= char ?>))
  462. (setq com char)
  463. (setq char (read-char))
  464. (if (= com char) (setq com (cons char com)))
  465. (setq cont nil))
  466. ((= char ?\")
  467. (let ((reg (read-char)))
  468. (if (or (and (<= ?A reg) (<= reg ?z))
  469. (and (<= ?1 reg) (<= reg ?9)))
  470. (setq vip-use-register reg)
  471. (error ""))
  472. (setq char (read-char))))
  473. (t
  474. (setq com char)
  475. (setq char (read-char)))))))
  476. (if (atom com)
  477. ;; com is a single char, so we construct prefix-arg
  478. ;; and if char is ?, describe prefix arg, otherwise exit by
  479. ;; pushing the char back
  480. (progn
  481. (setq prefix-arg (cons value com))
  482. (while (= char ?U)
  483. (vip-describe-arg prefix-arg)
  484. (setq char (read-char)))
  485. (setq unread-command-events (list char)))
  486. ;; as com is non-nil, this means that we have a command to execute
  487. (if (or (= (car com) ?r) (= (car com) ?R))
  488. ;; execute appropriate region command.
  489. (let ((char (car com)) (com (cdr com)))
  490. (setq prefix-arg (cons value com))
  491. (if (= char ?r) (vip-region prefix-arg)
  492. (vip-Region prefix-arg))
  493. ;; reset prefix-arg
  494. (setq prefix-arg nil))
  495. ;; otherwise, reset prefix arg and call appropriate command
  496. (setq value (if (null value) 1 value))
  497. (setq prefix-arg nil)
  498. (cond ((equal com '(?c . ?c)) (vip-line (cons value ?C)))
  499. ((equal com '(?d . ?d)) (vip-line (cons value ?D)))
  500. ((equal com '(?d . ?y)) (vip-yank-defun))
  501. ((equal com '(?y . ?y)) (vip-line (cons value ?Y)))
  502. ((equal com '(?< . ?<)) (vip-line (cons value ?<)))
  503. ((equal com '(?> . ?>)) (vip-line (cons value ?>)))
  504. ((equal com '(?! . ?!)) (vip-line (cons value ?!)))
  505. ((equal com '(?= . ?=)) (vip-line (cons value ?=)))
  506. (t (error ""))))))
  507. (defun vip-describe-arg (arg)
  508. (let (val com)
  509. (setq val (vip-P-val arg)
  510. com (vip-getcom arg))
  511. (if (null val)
  512. (if (null com)
  513. (message "Value is nil, and command is nil.")
  514. (message "Value is nil, and command is %c." com))
  515. (if (null com)
  516. (message "Value is %d, and command is nil." val)
  517. (message "Value is %d, and command is %c." val com)))))
  518. (defun vip-digit-argument (arg)
  519. "Begin numeric argument for the next command."
  520. (interactive "P")
  521. (vip-prefix-arg-value last-command-event nil
  522. (if (consp arg) (cdr arg) nil)))
  523. (defun vip-command-argument (arg)
  524. "Accept a motion command as an argument."
  525. (interactive "P")
  526. (condition-case conditions
  527. (vip-prefix-arg-com
  528. last-command-event
  529. (cond ((null arg) nil)
  530. ((consp arg) (car arg))
  531. ((numberp arg) arg)
  532. (t (error "strange arg")))
  533. (cond ((null arg) nil)
  534. ((consp arg) (cdr arg))
  535. ((numberp arg) nil)
  536. (t (error "strange arg"))))
  537. (quit
  538. (setq vip-use-register nil)
  539. (signal 'quit nil))))
  540. (defun vip-p-val (arg)
  541. "Get value part of prefix-argument ARG."
  542. (cond ((null arg) 1)
  543. ((consp arg) (if (null (car arg)) 1 (car arg)))
  544. (t arg)))
  545. (defun vip-P-val (arg)
  546. "Get value part of prefix-argument ARG."
  547. (cond ((consp arg) (car arg))
  548. (t arg)))
  549. (defun vip-getcom (arg)
  550. "Get com part of prefix-argument ARG."
  551. (cond ((null arg) nil)
  552. ((consp arg) (cdr arg))
  553. (t nil)))
  554. (defun vip-getCom (arg)
  555. "Get com part of prefix-argument ARG and modify it."
  556. (let ((com (vip-getcom arg)))
  557. (cond ((equal com ?c) ?C)
  558. ((equal com ?d) ?D)
  559. ((equal com ?y) ?Y)
  560. (t com))))
  561. ;; repeat last destructive command
  562. (defun vip-append-to-register (reg start end)
  563. "Append region to text in register REG.
  564. START and END are buffer positions indicating what to append."
  565. (set-register reg (concat (or (get-register reg) "")
  566. (buffer-substring start end))))
  567. (defun vip-execute-com (m-com val com)
  568. "(M-COM VAL COM) Execute command COM. The list (M-COM VAL COM) is set
  569. to vip-d-com for later use by vip-repeat"
  570. (let ((reg vip-use-register))
  571. (if com
  572. (cond ((= com ?c) (vip-change vip-com-point (point)))
  573. ((= com (- ?c)) (vip-change-subr vip-com-point (point)))
  574. ((or (= com ?C) (= com (- ?C)))
  575. (save-excursion
  576. (set-mark vip-com-point)
  577. (vip-enlarge-region (mark) (point))
  578. (if vip-use-register
  579. (progn
  580. (cond ((and (<= ?a vip-use-register)
  581. (<= vip-use-register ?z))
  582. (copy-to-register
  583. vip-use-register (mark) (point) nil))
  584. ((and (<= ?A vip-use-register)
  585. (<= vip-use-register ?Z))
  586. (vip-append-to-register
  587. (+ vip-use-register 32) (mark) (point)))
  588. (t (setq vip-use-register nil)
  589. (error "")))
  590. (setq vip-use-register nil)))
  591. (delete-region (mark) (point)))
  592. (open-line 1)
  593. (if (= com ?C) (vip-change-mode-to-insert) (yank)))
  594. ((= com ?d)
  595. (if vip-use-register
  596. (progn
  597. (cond ((and (<= ?a vip-use-register)
  598. (<= vip-use-register ?z))
  599. (copy-to-register
  600. vip-use-register vip-com-point (point) nil))
  601. ((and (<= ?A vip-use-register)
  602. (<= vip-use-register ?Z))
  603. (vip-append-to-register
  604. (+ vip-use-register 32) vip-com-point (point)))
  605. (t (setq vip-use-register nil)
  606. (error "")))
  607. (setq vip-use-register nil)))
  608. (setq last-command
  609. (if (eq last-command 'd-command) 'kill-region nil))
  610. (kill-region vip-com-point (point))
  611. (setq this-command 'd-command))
  612. ((= com ?D)
  613. (save-excursion
  614. (set-mark vip-com-point)
  615. (vip-enlarge-region (mark) (point))
  616. (if vip-use-register
  617. (progn
  618. (cond ((and (<= ?a vip-use-register)
  619. (<= vip-use-register ?z))
  620. (copy-to-register
  621. vip-use-register (mark) (point) nil))
  622. ((and (<= ?A vip-use-register)
  623. (<= vip-use-register ?Z))
  624. (vip-append-to-register
  625. (+ vip-use-register 32) (mark) (point)))
  626. (t (setq vip-use-register nil)
  627. (error "")))
  628. (setq vip-use-register nil)))
  629. (setq last-command
  630. (if (eq last-command 'D-command) 'kill-region nil))
  631. (kill-region (mark) (point))
  632. (if (eq m-com 'vip-line) (setq this-command 'D-command)))
  633. (back-to-indentation))
  634. ((= com ?y)
  635. (if vip-use-register
  636. (progn
  637. (cond ((and (<= ?a vip-use-register)
  638. (<= vip-use-register ?z))
  639. (copy-to-register
  640. vip-use-register vip-com-point (point) nil))
  641. ((and (<= ?A vip-use-register)
  642. (<= vip-use-register ?Z))
  643. (vip-append-to-register
  644. (+ vip-use-register 32) vip-com-point (point)))
  645. (t (setq vip-use-register nil)
  646. (error "")))
  647. (setq vip-use-register nil)))
  648. (setq last-command nil)
  649. (copy-region-as-kill vip-com-point (point))
  650. (goto-char vip-com-point))
  651. ((= com ?Y)
  652. (save-excursion
  653. (set-mark vip-com-point)
  654. (vip-enlarge-region (mark) (point))
  655. (if vip-use-register
  656. (progn
  657. (cond ((and (<= ?a vip-use-register)
  658. (<= vip-use-register ?z))
  659. (copy-to-register
  660. vip-use-register (mark) (point) nil))
  661. ((and (<= ?A vip-use-register)
  662. (<= vip-use-register ?Z))
  663. (vip-append-to-register
  664. (+ vip-use-register 32) (mark) (point)))
  665. (t (setq vip-use-register nil)
  666. (error "")))
  667. (setq vip-use-register nil)))
  668. (setq last-command nil)
  669. (copy-region-as-kill (mark) (point)))
  670. (goto-char vip-com-point))
  671. ((or (= com ?!) (= com (- ?!)))
  672. (save-excursion
  673. (set-mark vip-com-point)
  674. (vip-enlarge-region (mark) (point))
  675. (shell-command-on-region
  676. (mark) (point)
  677. (if (= com ?!)
  678. (setq vip-last-shell-com (vip-read-string "!"))
  679. vip-last-shell-com)
  680. t)))
  681. ((= com ?=)
  682. (save-excursion
  683. (set-mark vip-com-point)
  684. (vip-enlarge-region (mark) (point))
  685. (if (> (mark) (point)) (exchange-point-and-mark))
  686. (indent-region (mark) (point) nil)))
  687. ((= com ?<)
  688. (save-excursion
  689. (set-mark vip-com-point)
  690. (vip-enlarge-region (mark) (point))
  691. (indent-rigidly (mark) (point) (- vip-shift-width)))
  692. (goto-char vip-com-point))
  693. ((= com ?>)
  694. (save-excursion
  695. (set-mark vip-com-point)
  696. (vip-enlarge-region (mark) (point))
  697. (indent-rigidly (mark) (point) vip-shift-width))
  698. (goto-char vip-com-point))
  699. ((>= com 128)
  700. ;; this is special command #
  701. (vip-special-prefix-com (- com 128)))))
  702. (setq vip-d-com (list m-com val (if (or (= com ?c) (= com ?C) (= com ?!))
  703. (- com) com)
  704. reg))))
  705. (defun vip-repeat (arg)
  706. "(ARG) Re-execute last destructive command. vip-d-com has the form
  707. \(COM ARG CH REG), where COM is the command to be re-executed, ARG is the
  708. argument for COM, CH is a flag for repeat, and REG is optional and if exists
  709. is the name of the register for COM."
  710. (interactive "P")
  711. (if (eq last-command 'vip-undo)
  712. ;; if the last command was vip-undo, then undo-more
  713. (vip-undo-more)
  714. ;; otherwise execute the command stored in vip-d-com. if arg is non-nil
  715. ;; its prefix value is used as new prefix value for the command.
  716. (let ((m-com (car vip-d-com))
  717. (val (vip-P-val arg))
  718. (com (car (cdr (cdr vip-d-com))))
  719. (reg (nth 3 vip-d-com)))
  720. (if (null val) (setq val (car (cdr vip-d-com))))
  721. (if (null m-com) (error "No previous command to repeat"))
  722. (setq vip-use-register reg)
  723. (funcall m-com (cons val com)))))
  724. (defun vip-special-prefix-com (char)
  725. "This command is invoked interactively by the key sequence #<char>"
  726. (cond ((= char ?c)
  727. (downcase-region (min vip-com-point (point))
  728. (max vip-com-point (point))))
  729. ((= char ?C)
  730. (upcase-region (min vip-com-point (point))
  731. (max vip-com-point (point))))
  732. ((= char ?g)
  733. (set-mark vip-com-point)
  734. (vip-global-execute))
  735. ((= char ?q)
  736. (set-mark vip-com-point)
  737. (vip-quote-region))
  738. ((= char ?s) (ispell-region vip-com-point (point)))))
  739. ;; undoing
  740. (defun vip-undo ()
  741. "Undo previous change."
  742. (interactive)
  743. (message "undo!")
  744. (undo-start)
  745. (undo-more 2)
  746. (setq this-command 'vip-undo))
  747. (defun vip-undo-more ()
  748. "Continue undoing previous changes."
  749. (message "undo more!")
  750. (undo-more 1)
  751. (setq this-command 'vip-undo))
  752. ;; utilities
  753. (defun vip-string-tail (str)
  754. (if (or (null str) (string= str "")) nil
  755. (substring str 1)))
  756. (defun vip-yank-defun ()
  757. (mark-defun)
  758. (copy-region-as-kill (point) (mark)))
  759. (defun vip-enlarge-region (beg end)
  760. "Enlarge region between BEG and END."
  761. (if (< beg end)
  762. (progn (goto-char beg) (set-mark end))
  763. (goto-char end)
  764. (set-mark beg))
  765. (beginning-of-line)
  766. (exchange-point-and-mark)
  767. (if (or (not (eobp)) (not (bolp))) (with-no-warnings (next-line 1)))
  768. (beginning-of-line)
  769. (if (> beg end) (exchange-point-and-mark)))
  770. (defun vip-global-execute ()
  771. "Call last keyboard macro for each line in the region."
  772. (if (> (point) (mark)) (exchange-point-and-mark))
  773. (beginning-of-line)
  774. (call-last-kbd-macro)
  775. (while (< (point) (mark))
  776. (forward-line 1)
  777. (beginning-of-line)
  778. (call-last-kbd-macro)))
  779. (defun vip-quote-region ()
  780. "Quote region by inserting the user supplied string at the beginning of
  781. each line in the region."
  782. (setq vip-quote-string
  783. (let ((str
  784. (vip-read-string (format "quote string (default %s): "
  785. vip-quote-string))))
  786. (if (string= str "") vip-quote-string str)))
  787. (vip-enlarge-region (point) (mark))
  788. (if (> (point) (mark)) (exchange-point-and-mark))
  789. (insert vip-quote-string)
  790. (beginning-of-line)
  791. (forward-line 1)
  792. (while (and (< (point) (mark)) (bolp))
  793. (insert vip-quote-string)
  794. (beginning-of-line)
  795. (forward-line 1)))
  796. (defun vip-end-with-a-newline-p (string)
  797. "Check if the string ends with a newline."
  798. (or (string= string "")
  799. (= (aref string (1- (length string))) ?\n)))
  800. (defvar vip-save-minibuffer-local-map)
  801. (defun vip-read-string (prompt &optional init)
  802. (setq vip-save-minibuffer-local-map (copy-keymap minibuffer-local-map))
  803. (define-key minibuffer-local-map "\C-h" 'backward-char)
  804. (define-key minibuffer-local-map "\C-w" 'backward-word)
  805. (define-key minibuffer-local-map "\e" 'exit-minibuffer)
  806. (let (str)
  807. (condition-case conditions
  808. (setq str (read-string prompt init))
  809. (quit
  810. (setq minibuffer-local-map vip-save-minibuffer-local-map)
  811. (signal 'quit nil)))
  812. (setq minibuffer-local-map vip-save-minibuffer-local-map)
  813. str))
  814. ;; insertion commands
  815. (defun vip-repeat-insert-command ()
  816. "This function is called when mode changes from insertion mode to
  817. vi command mode. It will repeat the insertion command if original insertion
  818. command was invoked with argument > 1."
  819. (let ((i-com (car vip-d-com)) (val (car (cdr vip-d-com))))
  820. (if (and val (> val 1)) ;; first check that val is non-nil
  821. (progn
  822. (setq vip-d-com (list i-com (1- val) ?r))
  823. (vip-repeat nil)
  824. (setq vip-d-com (list i-com val ?r))))))
  825. (defun vip-insert (arg) ""
  826. (interactive "P")
  827. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  828. (setq vip-d-com (list 'vip-insert val ?r))
  829. (if com (vip-loop val (yank))
  830. (vip-change-mode-to-insert))))
  831. (defun vip-append (arg)
  832. "Append after point."
  833. (interactive "P")
  834. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  835. (setq vip-d-com (list 'vip-append val ?r))
  836. (if (not (eolp)) (forward-char))
  837. (if (equal com ?r)
  838. (vip-loop val (yank))
  839. (vip-change-mode-to-insert))))
  840. (defun vip-Append (arg)
  841. "Append at end of line."
  842. (interactive "P")
  843. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  844. (setq vip-d-com (list 'vip-Append val ?r))
  845. (end-of-line)
  846. (if (equal com ?r)
  847. (vip-loop val (yank))
  848. (vip-change-mode-to-insert))))
  849. (defun vip-Insert (arg)
  850. "Insert before first non-white."
  851. (interactive "P")
  852. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  853. (setq vip-d-com (list 'vip-Insert val ?r))
  854. (back-to-indentation)
  855. (if (equal com ?r)
  856. (vip-loop val (yank))
  857. (vip-change-mode-to-insert))))
  858. (defun vip-open-line (arg)
  859. "Open line below."
  860. (interactive "P")
  861. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  862. (setq vip-d-com (list 'vip-open-line val ?r))
  863. (let ((col (current-indentation)))
  864. (if (equal com ?r)
  865. (vip-loop val
  866. (progn
  867. (end-of-line)
  868. (newline 1)
  869. (if vip-open-with-indent (indent-to col))
  870. (yank)))
  871. (end-of-line)
  872. (newline 1)
  873. (if vip-open-with-indent (indent-to col))
  874. (vip-change-mode-to-insert)))))
  875. (defun vip-Open-line (arg)
  876. "Open line above."
  877. (interactive "P")
  878. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  879. (setq vip-d-com (list 'vip-Open-line val ?r))
  880. (let ((col (current-indentation)))
  881. (if (equal com ?r)
  882. (vip-loop val
  883. (progn
  884. (beginning-of-line)
  885. (open-line 1)
  886. (if vip-open-with-indent (indent-to col))
  887. (yank)))
  888. (beginning-of-line)
  889. (open-line 1)
  890. (if vip-open-with-indent (indent-to col))
  891. (vip-change-mode-to-insert)))))
  892. (defun vip-open-line-at-point (arg)
  893. "Open line at point."
  894. (interactive "P")
  895. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  896. (setq vip-d-com (list 'vip-open-line-at-point val ?r))
  897. (if (equal com ?r)
  898. (vip-loop val
  899. (progn
  900. (open-line 1)
  901. (yank)))
  902. (open-line 1)
  903. (vip-change-mode-to-insert))))
  904. (defun vip-substitute (arg)
  905. "Substitute characters."
  906. (interactive "P")
  907. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  908. (save-excursion
  909. (set-mark (point))
  910. (forward-char val)
  911. (if (equal com ?r)
  912. (vip-change-subr (mark) (point))
  913. (vip-change (mark) (point))))
  914. (setq vip-d-com (list 'vip-substitute val ?r))))
  915. (defun vip-substitute-line (arg)
  916. "Substitute lines."
  917. (interactive "p")
  918. (vip-line (cons arg ?C)))
  919. ;; line command
  920. (defun vip-line (arg)
  921. (let ((val (car arg)) (com (cdr arg)))
  922. (move-marker vip-com-point (point))
  923. (with-no-warnings (next-line (1- val)))
  924. (vip-execute-com 'vip-line val com)))
  925. (defun vip-yank-line (arg)
  926. "Yank ARG lines (in vi's sense)"
  927. (interactive "P")
  928. (let ((val (vip-p-val arg)))
  929. (vip-line (cons val ?Y))))
  930. ;; region command
  931. (defun vip-region (arg)
  932. (interactive "P")
  933. (let ((val (vip-P-val arg))
  934. (com (vip-getcom arg)))
  935. (move-marker vip-com-point (point))
  936. (exchange-point-and-mark)
  937. (vip-execute-com 'vip-region val com)))
  938. (defun vip-Region (arg)
  939. (interactive "P")
  940. (let ((val (vip-P-val arg))
  941. (com (vip-getCom arg)))
  942. (move-marker vip-com-point (point))
  943. (exchange-point-and-mark)
  944. (vip-execute-com 'vip-Region val com)))
  945. (defun vip-replace-char (arg)
  946. "Replace the following ARG chars by the character read."
  947. (interactive "P")
  948. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  949. (setq vip-d-com (list 'vip-replace-char val ?r))
  950. (vip-replace-char-subr (if (equal com ?r) vip-d-char (read-char)) val)))
  951. (defun vip-replace-char-subr (char arg)
  952. (delete-char arg t)
  953. (setq vip-d-char char)
  954. (vip-loop (if (> arg 0) arg (- arg)) (insert char))
  955. (backward-char arg))
  956. (defun vip-replace-string ()
  957. "Replace string. If you supply null string as the string to be replaced,
  958. the query replace mode will toggle between string replace and regexp replace."
  959. (interactive)
  960. (let (str)
  961. (setq str (vip-read-string
  962. (if vip-re-replace "Replace regexp: " "Replace string: ")))
  963. (if (string= str "")
  964. (progn
  965. (setq vip-re-replace (not vip-re-replace))
  966. (message "Replace mode changed to %s."
  967. (if vip-re-replace "regexp replace"
  968. "string replace")))
  969. (if vip-re-replace
  970. ;; (replace-regexp
  971. ;; str
  972. ;; (vip-read-string (format "Replace regexp \"%s\" with: " str)))
  973. (while (re-search-forward str nil t)
  974. (replace-match (vip-read-string
  975. (format "Replace regexp \"%s\" with: " str))
  976. nil nil))
  977. (with-no-warnings
  978. (replace-string
  979. str
  980. (vip-read-string (format "Replace \"%s\" with: " str))))))))
  981. ;; basic cursor movement. j, k, l, m commands.
  982. (defun vip-forward-char (arg)
  983. "Move point right ARG characters (left if ARG negative).On reaching end
  984. of buffer, stop and signal error."
  985. (interactive "P")
  986. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  987. (if com (move-marker vip-com-point (point)))
  988. (forward-char val)
  989. (if com (vip-execute-com 'vip-forward-char val com))))
  990. (defun vip-backward-char (arg)
  991. "Move point left ARG characters (right if ARG negative). On reaching
  992. beginning of buffer, stop and signal error."
  993. (interactive "P")
  994. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  995. (if com (move-marker vip-com-point (point)))
  996. (backward-char val)
  997. (if com (vip-execute-com 'vip-backward-char val com))))
  998. ;; word command
  999. (defun vip-forward-word (arg)
  1000. "Forward word."
  1001. (interactive "P")
  1002. (let ((val (vip-p-val arg))
  1003. (com (vip-getcom arg)))
  1004. (if com (move-marker vip-com-point (point)))
  1005. (forward-word val)
  1006. (skip-chars-forward " \t\n")
  1007. (if com
  1008. (progn
  1009. (if (or (= com ?c) (= com (- ?c)))
  1010. (progn (backward-word 1) (forward-word 1)))
  1011. (if (or (= com ?d) (= com ?y))
  1012. (progn
  1013. (backward-word 1)
  1014. (forward-word 1)
  1015. (skip-chars-forward " \t")))
  1016. (vip-execute-com 'vip-forward-word val com)))))
  1017. (defun vip-end-of-word (arg)
  1018. "Move point to end of current word."
  1019. (interactive "P")
  1020. (let ((val (vip-p-val arg))
  1021. (com (vip-getcom arg)))
  1022. (if com (move-marker vip-com-point (point)))
  1023. (forward-char)
  1024. (forward-word val)
  1025. (backward-char)
  1026. (if com
  1027. (progn
  1028. (forward-char)
  1029. (vip-execute-com 'vip-end-of-word val com)))))
  1030. (defun vip-backward-word (arg)
  1031. "Backward word."
  1032. (interactive "P")
  1033. (let ((val (vip-p-val arg))
  1034. (com (vip-getcom arg)))
  1035. (if com (move-marker vip-com-point (point)))
  1036. (backward-word val)
  1037. (if com (vip-execute-com 'vip-backward-word val com))))
  1038. (defun vip-forward-Word (arg)
  1039. "Forward word delimited by white character."
  1040. (interactive "P")
  1041. (let ((val (vip-p-val arg))
  1042. (com (vip-getcom arg)))
  1043. (if com (move-marker vip-com-point (point)))
  1044. (re-search-forward "[^ \t\n]*[ \t\n]+" nil t val)
  1045. (if com
  1046. (progn
  1047. (if (or (= com ?c) (= com (- ?c)))
  1048. (progn (backward-word 1) (forward-word 1)))
  1049. (if (or (= com ?d) (= com ?y))
  1050. (progn
  1051. (backward-word 1)
  1052. (forward-word 1)
  1053. (skip-chars-forward " \t")))
  1054. (vip-execute-com 'vip-forward-Word val com)))))
  1055. (defun vip-end-of-Word (arg)
  1056. "Move forward to end of word delimited by white character."
  1057. (interactive "P")
  1058. (let ((val (vip-p-val arg))
  1059. (com (vip-getcom arg)))
  1060. (if com (move-marker vip-com-point (point)))
  1061. (forward-char)
  1062. (if (re-search-forward "[^ \t\n]+" nil t val) (backward-char))
  1063. (if com
  1064. (progn
  1065. (forward-char)
  1066. (vip-execute-com 'vip-end-of-Word val com)))))
  1067. (defun vip-backward-Word (arg)
  1068. "Backward word delimited by white character."
  1069. (interactive "P")
  1070. (let ((val (vip-p-val arg))
  1071. (com (vip-getcom arg)))
  1072. (if com (move-marker vip-com-point (point)))
  1073. (if (re-search-backward "[ \t\n]+[^ \t\n]+" nil t val)
  1074. (forward-char)
  1075. (goto-char (point-min)))
  1076. (if com (vip-execute-com 'vip-backward-Word val com))))
  1077. (defun vip-beginning-of-line (arg)
  1078. "Go to beginning of line."
  1079. (interactive "P")
  1080. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1081. (if com (move-marker vip-com-point (point)))
  1082. (beginning-of-line val)
  1083. (if com (vip-execute-com 'vip-beginning-of-line val com))))
  1084. (defun vip-bol-and-skip-white (arg)
  1085. "Beginning of line at first non-white character."
  1086. (interactive "P")
  1087. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1088. (if com (move-marker vip-com-point (point)))
  1089. (back-to-indentation)
  1090. (if com (vip-execute-com 'vip-bol-and-skip-white val com))))
  1091. (defun vip-goto-eol (arg)
  1092. "Go to end of line."
  1093. (interactive "P")
  1094. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1095. (if com (move-marker vip-com-point (point)))
  1096. (end-of-line val)
  1097. (if com (vip-execute-com 'vip-goto-eol val com))))
  1098. (defun vip-next-line (arg)
  1099. "Go to next line."
  1100. (interactive "P")
  1101. (let ((val (vip-p-val arg)) (com (vip-getCom arg)))
  1102. (if com (move-marker vip-com-point (point)))
  1103. (line-move val)
  1104. (setq this-command 'next-line)
  1105. (if com (vip-execute-com 'vip-next-line val com))))
  1106. (defun vip-next-line-at-bol (arg)
  1107. "Next line at beginning of line."
  1108. (interactive "P")
  1109. (let ((val (vip-p-val arg)) (com (vip-getCom arg)))
  1110. (if com (move-marker vip-com-point (point)))
  1111. (with-no-warnings (next-line val))
  1112. (back-to-indentation)
  1113. (if com (vip-execute-com 'vip-next-line-at-bol val com))))
  1114. (defun vip-previous-line (arg)
  1115. "Go to previous line."
  1116. (interactive "P")
  1117. (let ((val (vip-p-val arg)) (com (vip-getCom arg)))
  1118. (if com (move-marker vip-com-point (point)))
  1119. (with-no-warnings (next-line (- val)))
  1120. (setq this-command 'previous-line)
  1121. (if com (vip-execute-com 'vip-previous-line val com))))
  1122. (defun vip-previous-line-at-bol (arg)
  1123. "Previous line at beginning of line."
  1124. (interactive "P")
  1125. (let ((val (vip-p-val arg)) (com (vip-getCom arg)))
  1126. (if com (move-marker vip-com-point (point)))
  1127. (with-no-warnings (next-line (- val)))
  1128. (back-to-indentation)
  1129. (if com (vip-execute-com 'vip-previous-line val com))))
  1130. (defun vip-change-to-eol (arg)
  1131. "Change to end of line."
  1132. (interactive "P")
  1133. (vip-goto-eol (cons arg ?c)))
  1134. (defun vip-kill-line (arg)
  1135. "Delete line."
  1136. (interactive "P")
  1137. (vip-goto-eol (cons arg ?d)))
  1138. ;; moving around
  1139. (defun vip-goto-line (arg)
  1140. "Go to ARG's line. Without ARG go to end of buffer."
  1141. (interactive "P")
  1142. (let ((val (vip-P-val arg)) (com (vip-getCom arg)))
  1143. (move-marker vip-com-point (point))
  1144. (set-mark (point))
  1145. (if (null val)
  1146. (goto-char (point-max))
  1147. (goto-char (point-min))
  1148. (forward-line (1- val)))
  1149. (back-to-indentation)
  1150. (if com (vip-execute-com 'vip-goto-line val com))))
  1151. (defun vip-find-char (arg char forward offset)
  1152. "Find ARG's occurrence of CHAR on the current line. If FORWARD then
  1153. search is forward, otherwise backward. OFFSET is used to adjust point
  1154. after search."
  1155. (let ((arg (if forward arg (- arg))) point)
  1156. (save-excursion
  1157. (save-restriction
  1158. (if (> arg 0)
  1159. (narrow-to-region
  1160. ;; forward search begins here
  1161. (if (eolp) (error "") (point))
  1162. ;; forward search ends here
  1163. (progn (with-no-warnings (next-line 1)) (beginning-of-line) (point)))
  1164. (narrow-to-region
  1165. ;; backward search begins from here
  1166. (if (bolp) (error "") (point))
  1167. ;; backward search ends here
  1168. (progn (beginning-of-line) (point))))
  1169. ;; if arg > 0, point is forwarded before search.
  1170. (if (> arg 0) (goto-char (1+ (point-min)))
  1171. (goto-char (point-max)))
  1172. (let ((case-fold-search nil))
  1173. (search-forward (char-to-string char) nil 0 arg))
  1174. (setq point (point))
  1175. (if (or (and (> arg 0) (= point (point-max)))
  1176. (and (< arg 0) (= point (point-min))))
  1177. (error ""))))
  1178. (goto-char (+ point (if (> arg 0) (if offset -2 -1) (if offset 1 0))))))
  1179. (defun vip-find-char-forward (arg)
  1180. "Find char on the line. If called interactively read the char to find
  1181. from the terminal, and if called from vip-repeat, the char last used is
  1182. used. This behavior is controlled by the sign of prefix numeric value."
  1183. (interactive "P")
  1184. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1185. (if (> val 0)
  1186. ;; this means that the function was called interactively
  1187. (setq vip-f-char (read-char)
  1188. vip-f-forward t
  1189. vip-f-offset nil)
  1190. (setq val (- val)))
  1191. (if com (move-marker vip-com-point (point)))
  1192. (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t nil)
  1193. (setq val (- val))
  1194. (if com
  1195. (progn
  1196. (setq vip-F-char vip-f-char);; set new vip-F-char
  1197. (forward-char)
  1198. (vip-execute-com 'vip-find-char-forward val com)))))
  1199. (defun vip-goto-char-forward (arg)
  1200. "Go up to char ARG forward on line."
  1201. (interactive "P")
  1202. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1203. (if (> val 0)
  1204. ;; this means that the function was called interactively
  1205. (setq vip-f-char (read-char)
  1206. vip-f-forward t
  1207. vip-f-offset t)
  1208. (setq val (- val)))
  1209. (if com (move-marker vip-com-point (point)))
  1210. (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) t t)
  1211. (setq val (- val))
  1212. (if com
  1213. (progn
  1214. (setq vip-F-char vip-f-char);; set new vip-F-char
  1215. (forward-char)
  1216. (vip-execute-com 'vip-goto-char-forward val com)))))
  1217. (defun vip-find-char-backward (arg)
  1218. "Find char ARG on line backward."
  1219. (interactive "P")
  1220. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1221. (if (> val 0)
  1222. ;; this means that the function was called interactively
  1223. (setq vip-f-char (read-char)
  1224. vip-f-forward nil
  1225. vip-f-offset nil)
  1226. (setq val (- val)))
  1227. (if com (move-marker vip-com-point (point)))
  1228. (vip-find-char
  1229. val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil nil)
  1230. (setq val (- val))
  1231. (if com
  1232. (progn
  1233. (setq vip-F-char vip-f-char);; set new vip-F-char
  1234. (vip-execute-com 'vip-find-char-backward val com)))))
  1235. (defun vip-goto-char-backward (arg)
  1236. "Go up to char ARG backward on line."
  1237. (interactive "P")
  1238. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1239. (if (> val 0)
  1240. ;; this means that the function was called interactively
  1241. (setq vip-f-char (read-char)
  1242. vip-f-forward nil
  1243. vip-f-offset t)
  1244. (setq val (- val)))
  1245. (if com (move-marker vip-com-point (point)))
  1246. (vip-find-char val (if (> (vip-p-val arg) 0) vip-f-char vip-F-char) nil t)
  1247. (setq val (- val))
  1248. (if com
  1249. (progn
  1250. (setq vip-F-char vip-f-char);; set new vip-F-char
  1251. (vip-execute-com 'vip-goto-char-backward val com)))))
  1252. (defun vip-repeat-find (arg)
  1253. "Repeat previous find command."
  1254. (interactive "P")
  1255. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1256. (if com (move-marker vip-com-point (point)))
  1257. (vip-find-char val vip-f-char vip-f-forward vip-f-offset)
  1258. (if com
  1259. (progn
  1260. (if vip-f-forward (forward-char))
  1261. (vip-execute-com 'vip-repeat-find val com)))))
  1262. (defun vip-repeat-find-opposite (arg)
  1263. "Repeat previous find command in the opposite direction."
  1264. (interactive "P")
  1265. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1266. (if com (move-marker vip-com-point (point)))
  1267. (vip-find-char val vip-f-char (not vip-f-forward) vip-f-offset)
  1268. (if com
  1269. (progn
  1270. (if vip-f-forward (forward-char))
  1271. (vip-execute-com 'vip-repeat-find-opposite val com)))))
  1272. ;; window scrolling etc.
  1273. (defun vip-other-window (arg)
  1274. "Switch to other window."
  1275. (interactive "p")
  1276. (other-window arg)
  1277. (or (not (eq vip-current-mode 'emacs-mode))
  1278. (string= (buffer-name (current-buffer)) " *Minibuf-1*")
  1279. (vip-change-mode-to-vi)))
  1280. (defun vip-window-top (arg)
  1281. "Go to home window line."
  1282. (interactive "P")
  1283. (let ((val (vip-p-val arg))
  1284. (com (vip-getCom arg)))
  1285. (if com (move-marker vip-com-point (point)))
  1286. (move-to-window-line (1- val))
  1287. (if com (vip-execute-com 'vip-window-top val com))))
  1288. (defun vip-window-middle (arg)
  1289. "Go to middle window line."
  1290. (interactive "P")
  1291. (let ((val (vip-p-val arg))
  1292. (com (vip-getCom arg)))
  1293. (if com (move-marker vip-com-point (point)))
  1294. (move-to-window-line (+ (/ (1- (window-height)) 2) (1- val)))
  1295. (if com (vip-execute-com 'vip-window-middle val com))))
  1296. (defun vip-window-bottom (arg)
  1297. "Go to last window line."
  1298. (interactive "P")
  1299. (let ((val (vip-p-val arg))
  1300. (com (vip-getCom arg)))
  1301. (if com (move-marker vip-com-point (point)))
  1302. (move-to-window-line (- val))
  1303. (if com (vip-execute-com 'vip-window-bottom val com))))
  1304. (defun vip-line-to-top (arg)
  1305. "Put current line on the home line."
  1306. (interactive "p")
  1307. (recenter (1- arg)))
  1308. (defun vip-line-to-middle (arg)
  1309. "Put current line on the middle line."
  1310. (interactive "p")
  1311. (recenter (+ (1- arg) (/ (1- (window-height)) 2))))
  1312. (defun vip-line-to-bottom (arg)
  1313. "Put current line on the last line."
  1314. (interactive "p")
  1315. (recenter (- (window-height) (1+ arg))))
  1316. ;; paren match
  1317. (defun vip-paren-match (arg)
  1318. "Go to the matching parenthesis."
  1319. (interactive "P")
  1320. (let ((com (vip-getcom arg)))
  1321. (if (numberp arg)
  1322. (if (or (> arg 99) (< arg 1))
  1323. (error "Prefix must be between 1 and 99")
  1324. (goto-char
  1325. (if (> (point-max) 80000)
  1326. (* (/ (point-max) 100) arg)
  1327. (/ (* (point-max) arg) 100)))
  1328. (back-to-indentation))
  1329. (cond ((looking-at "[\(\[{]")
  1330. (if com (move-marker vip-com-point (point)))
  1331. (forward-sexp 1)
  1332. (if com
  1333. (vip-execute-com 'vip-paren-match nil com)
  1334. (backward-char)))
  1335. ((looking-at "[])}]")
  1336. (forward-char)
  1337. (if com (move-marker vip-com-point (point)))
  1338. (backward-sexp 1)
  1339. (if com (vip-execute-com 'vip-paren-match nil com)))
  1340. (t (error ""))))))
  1341. ;; sentence and paragraph
  1342. (defun vip-forward-sentence (arg)
  1343. "Forward sentence."
  1344. (interactive "P")
  1345. (let ((val (vip-p-val arg))
  1346. (com (vip-getcom arg)))
  1347. (if com (move-marker vip-com-point (point)))
  1348. (forward-sentence val)
  1349. (if com (vip-execute-com 'vip-forward-sentence nil com))))
  1350. (defun vip-backward-sentence (arg)
  1351. "Backward sentence."
  1352. (interactive "P")
  1353. (let ((val (vip-p-val arg))
  1354. (com (vip-getcom arg)))
  1355. (if com (move-marker vip-com-point (point)))
  1356. (backward-sentence val)
  1357. (if com (vip-execute-com 'vip-backward-sentence nil com))))
  1358. (defun vip-forward-paragraph (arg)
  1359. "Forward paragraph."
  1360. (interactive "P")
  1361. (let ((val (vip-p-val arg))
  1362. (com (vip-getCom arg)))
  1363. (if com (move-marker vip-com-point (point)))
  1364. (forward-paragraph val)
  1365. (if com (vip-execute-com 'vip-forward-paragraph nil com))))
  1366. (defun vip-backward-paragraph (arg)
  1367. "Backward paragraph."
  1368. (interactive "P")
  1369. (let ((val (vip-p-val arg))
  1370. (com (vip-getCom arg)))
  1371. (if com (move-marker vip-com-point (point)))
  1372. (backward-paragraph val)
  1373. (if com (vip-execute-com 'vip-backward-paragraph nil com))))
  1374. ;; scrolling
  1375. (defun vip-scroll (arg)
  1376. "Scroll to next screen."
  1377. (interactive "p")
  1378. (if (> arg 0)
  1379. (while (> arg 0)
  1380. (scroll-up)
  1381. (setq arg (1- arg)))
  1382. (while (> 0 arg)
  1383. (scroll-down)
  1384. (setq arg (1+ arg)))))
  1385. (defun vip-scroll-back (arg)
  1386. "Scroll to previous screen."
  1387. (interactive "p")
  1388. (vip-scroll (- arg)))
  1389. (defun vip-scroll-down (arg)
  1390. "Scroll up half screen."
  1391. (interactive "P")
  1392. (if (null arg) (scroll-down (/ (window-height) 2))
  1393. (scroll-down arg)))
  1394. (defun vip-scroll-down-one (arg)
  1395. "Scroll up one line."
  1396. (interactive "p")
  1397. (scroll-down arg))
  1398. (defun vip-scroll-up (arg)
  1399. "Scroll down half screen."
  1400. (interactive "P")
  1401. (if (null arg) (scroll-up (/ (window-height) 2))
  1402. (scroll-up arg)))
  1403. (defun vip-scroll-up-one (arg)
  1404. "Scroll down one line."
  1405. (interactive "p")
  1406. (scroll-up arg))
  1407. ;; splitting window
  1408. (defun vip-buffer-in-two-windows ()
  1409. "Show current buffer in two windows."
  1410. (interactive)
  1411. (delete-other-windows)
  1412. (split-window-below))
  1413. ;; searching
  1414. (defun vip-search-forward (arg)
  1415. "Search a string forward. ARG is used to find the ARG's occurrence
  1416. of the string. Default is vanilla search. Search mode can be toggled by
  1417. giving null search string."
  1418. (interactive "P")
  1419. (let ((val (vip-P-val arg)) (com (vip-getcom arg)))
  1420. (setq vip-s-forward t
  1421. vip-s-string (vip-read-string (if vip-re-search "RE-/" "/")))
  1422. (if (string= vip-s-string "")
  1423. (progn
  1424. (setq vip-re-search (not vip-re-search))
  1425. (message "Search mode changed to %s search."
  1426. (if vip-re-search "regular expression"
  1427. "vanilla")))
  1428. (vip-search vip-s-string t val)
  1429. (if com
  1430. (progn
  1431. (move-marker vip-com-point (mark))
  1432. (vip-execute-com 'vip-search-next val com))))))
  1433. (defun vip-search-backward (arg)
  1434. "Search a string backward. ARG is used to find the ARG's occurrence
  1435. of the string. Default is vanilla search. Search mode can be toggled by
  1436. giving null search string."
  1437. (interactive "P")
  1438. (let ((val (vip-P-val arg)) (com (vip-getcom arg)))
  1439. (setq vip-s-forward nil
  1440. vip-s-string (vip-read-string (if vip-re-search "RE-?" "?")))
  1441. (if (string= vip-s-string "")
  1442. (progn
  1443. (setq vip-re-search (not vip-re-search))
  1444. (message "Search mode changed to %s search."
  1445. (if vip-re-search "regular expression"
  1446. "vanilla")))
  1447. (vip-search vip-s-string nil val)
  1448. (if com
  1449. (progn
  1450. (move-marker vip-com-point (mark))
  1451. (vip-execute-com 'vip-search-next val com))))))
  1452. (defun vip-search (string forward arg &optional no-offset init-point)
  1453. "(STRING FORWARD COUNT &optional NO-OFFSET) Search COUNT's occurrence of
  1454. STRING. Search will be forward if FORWARD, otherwise backward."
  1455. (let ((val (vip-p-val arg)) (com (vip-getcom arg))
  1456. (null-arg (null (vip-P-val arg))) (offset (not no-offset))
  1457. (case-fold-search vip-case-fold-search)
  1458. (start-point (or init-point (point))))
  1459. (if forward
  1460. (condition-case conditions
  1461. (progn
  1462. (if (and offset (not (eobp))) (forward-char))
  1463. (if vip-re-search
  1464. (progn
  1465. (re-search-forward string nil nil val)
  1466. (re-search-backward string))
  1467. (search-forward string nil nil val)
  1468. (search-backward string))
  1469. (push-mark start-point))
  1470. (search-failed
  1471. (if (and null-arg vip-search-wrap-around)
  1472. (progn
  1473. (goto-char (point-min))
  1474. (vip-search string forward (cons 1 com) t start-point))
  1475. (goto-char start-point)
  1476. (signal 'search-failed (cdr conditions)))))
  1477. (condition-case conditions
  1478. (progn
  1479. (if vip-re-search
  1480. (re-search-backward string nil nil val)
  1481. (search-backward string nil nil val))
  1482. (push-mark start-point))
  1483. (search-failed
  1484. (if (and null-arg vip-search-wrap-around)
  1485. (progn
  1486. (goto-char (point-max))
  1487. (vip-search string forward (cons 1 com) t start-point))
  1488. (goto-char start-point)
  1489. (signal 'search-failed (cdr conditions))))))))
  1490. (defun vip-search-next (arg)
  1491. "Repeat previous search."
  1492. (interactive "P")
  1493. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1494. (if (null vip-s-string) (error "No previous search string"))
  1495. (vip-search vip-s-string vip-s-forward arg)
  1496. (if com (vip-execute-com 'vip-search-next val com))))
  1497. (defun vip-search-Next (arg)
  1498. "Repeat previous search in the reverse direction."
  1499. (interactive "P")
  1500. (let ((val (vip-p-val arg)) (com (vip-getcom arg)))
  1501. (if (null vip-s-string) (error "No previous search string"))
  1502. (vip-search vip-s-string (not vip-s-forward) arg)
  1503. (if com (vip-execute-com 'vip-search-Next val com))))
  1504. ;; visiting and killing files, buffers
  1505. (defun vip-switch-to-buffer ()
  1506. "Switch to buffer in the current window."
  1507. (interactive)
  1508. (let (buffer)
  1509. (setq buffer
  1510. (read-buffer
  1511. (format "switch to buffer \(%s\): "
  1512. (buffer-name (other-buffer (current-buffer))))))
  1513. (switch-to-buffer buffer)
  1514. (vip-change-mode-to-vi)))
  1515. (defun vip-switch-to-buffer-other-window ()
  1516. "Switch to buffer in another window."
  1517. (interactive)
  1518. (let (buffer)
  1519. (setq buffer
  1520. (read-buffer
  1521. (format "Switch to buffer \(%s\): "
  1522. (buffer-name (other-buffer (current-buffer))))))
  1523. (switch-to-buffer-other-window buffer)
  1524. (vip-change-mode-to-vi)))
  1525. (defun vip-kill-buffer ()
  1526. "Kill a buffer."
  1527. (interactive)
  1528. (let (buffer buffer-name)
  1529. (setq buffer-name
  1530. (read-buffer
  1531. (format "Kill buffer \(%s\): "
  1532. (buffer-name (current-buffer)))))
  1533. (setq buffer
  1534. (if (null buffer-name)
  1535. (current-buffer)
  1536. (get-buffer buffer-name)))
  1537. (if (null buffer) (error "Buffer %s nonexistent" buffer-name))
  1538. (if (or (not (buffer-modified-p buffer))
  1539. (y-or-n-p "Buffer is modified, are you sure? "))
  1540. (kill-buffer buffer)
  1541. (error "Buffer not killed"))))
  1542. (defun vip-find-file ()
  1543. "Visit file in the current window."
  1544. (interactive)
  1545. (let (file)
  1546. (setq file (read-file-name "visit file: "))
  1547. (switch-to-buffer (find-file-noselect file))
  1548. (vip-change-mode-to-vi)))
  1549. (defun vip-find-file-other-window ()
  1550. "Visit file in another window."
  1551. (interactive)
  1552. (let (file)
  1553. (setq file (read-file-name "Visit file: "))
  1554. (switch-to-buffer-other-window (find-file-noselect file))
  1555. (vip-change-mode-to-vi)))
  1556. (defun vip-info-on-file ()
  1557. "Give information of the file associated to the current buffer."
  1558. (interactive)
  1559. (message "\"%s\" line %d of %d"
  1560. (if (buffer-file-name) (buffer-file-name) "")
  1561. (1+ (count-lines (point-min) (point)))
  1562. (1+ (count-lines (point-min) (point-max)))))
  1563. ;; yank and pop
  1564. (defun vip-yank (text)
  1565. "yank TEXT silently."
  1566. (save-excursion
  1567. (vip-push-mark-silent (point))
  1568. (insert text)
  1569. (exchange-point-and-mark))
  1570. (skip-chars-forward " \t"))
  1571. (defun vip-put-back (arg)
  1572. "Put back after point/below line."
  1573. (interactive "P")
  1574. (let ((val (vip-p-val arg))
  1575. (text (if vip-use-register
  1576. (if (and (<= ?1 vip-use-register) (<= vip-use-register ?9))
  1577. (current-kill (- vip-use-register ?1) 'do-not-rotate)
  1578. (get-register vip-use-register))
  1579. (current-kill 0))))
  1580. (if (null text)
  1581. (if vip-use-register
  1582. (let ((reg vip-use-register))
  1583. (setq vip-use-register nil)
  1584. (error "Nothing in register %c" reg))
  1585. (error "")))
  1586. (setq vip-use-register nil)
  1587. (if (vip-end-with-a-newline-p text)
  1588. (progn
  1589. (with-no-warnings (next-line 1))
  1590. (beginning-of-line))
  1591. (if (and (not (eolp)) (not (eobp))) (forward-char)))
  1592. (setq vip-d-com (list 'vip-put-back val nil vip-use-register))
  1593. (vip-loop val (vip-yank text))))
  1594. (defun vip-Put-back (arg)
  1595. "Put back at point/above line."
  1596. (interactive "P")
  1597. (let ((val (vip-p-val arg))
  1598. (text (if vip-use-register
  1599. (if (and (<= ?1 vip-use-register) (<= vip-use-register ?9))
  1600. (current-kill (- vip-use-register ?1) 'do-not-rotate)
  1601. (get-register vip-use-register))
  1602. (current-kill 0))))
  1603. (if (null text)
  1604. (if vip-use-register
  1605. (let ((reg vip-use-register))
  1606. (setq vip-use-register nil)
  1607. (error "Nothing in register %c" reg))
  1608. (error "")))
  1609. (setq vip-use-register nil)
  1610. (if (vip-end-with-a-newline-p text) (beginning-of-line))
  1611. (setq vip-d-com (list 'vip-Put-back val nil vip-use-register))
  1612. (vip-loop val (vip-yank text))))
  1613. (defun vip-delete-char (arg)
  1614. "Delete character."
  1615. (interactive "P")
  1616. (let ((val (vip-p-val arg)))
  1617. (setq vip-d-com (list 'vip-delete-char val nil))
  1618. (if vip-use-register
  1619. (progn
  1620. (if (and (<= ?A vip-use-register) (<= vip-use-register ?Z))
  1621. (vip-append-to-register
  1622. (+ vip-use-register 32) (point) (- (point) val))
  1623. (copy-to-register vip-use-register (point) (- (point) val) nil))
  1624. (setq vip-use-register nil)))
  1625. (delete-char val t)))
  1626. (defun vip-delete-backward-char (arg)
  1627. "Delete previous character."
  1628. (interactive "P")
  1629. (let ((val (vip-p-val arg)))
  1630. (setq vip-d-com (list 'vip-delete-backward-char val nil))
  1631. (if vip-use-register
  1632. (progn
  1633. (if (and (<= ?A vip-use-register) (<= vip-use-register ?Z))
  1634. (vip-append-to-register
  1635. (+ vip-use-register 32) (point) (+ (point) val))
  1636. (copy-to-register vip-use-register (point) (+ (point) val) nil))
  1637. (setq vip-use-register nil)))
  1638. (delete-backward-char val t)))
  1639. ;; join lines.
  1640. (defun vip-join-lines (arg)
  1641. "Join this line to next, if ARG is nil. Otherwise, join ARG lines"
  1642. (interactive "*P")
  1643. (let ((val (vip-P-val arg)))
  1644. (setq vip-d-com (list 'vip-join-lines val nil))
  1645. (vip-loop (if (null val) 1 (1- val))
  1646. (progn
  1647. (end-of-line)
  1648. (if (not (eobp))
  1649. (progn
  1650. (forward-line 1)
  1651. (delete-region (point) (1- (point)))
  1652. (fixup-whitespace)))))))
  1653. ;; making small changes
  1654. (defvar vip-c-string)
  1655. (defun vip-change (beg end)
  1656. (setq vip-c-string
  1657. (vip-read-string (format "%s => " (buffer-substring beg end))))
  1658. (vip-change-subr beg end))
  1659. (defun vip-change-subr (beg end)
  1660. (if vip-use-register
  1661. (progn
  1662. (copy-to-register vip-use-register beg end nil)
  1663. (setq vip-use-register nil)))
  1664. (kill-region beg end)
  1665. (setq this-command 'vip-change)
  1666. (insert vip-c-string))
  1667. ;; query replace
  1668. (defun vip-query-replace ()
  1669. "Query replace. If you supply null string as the string to be replaced,
  1670. the query replace mode will toggle between string replace and regexp replace."
  1671. (interactive)
  1672. (let (str)
  1673. (setq str (vip-read-string
  1674. (if vip-re-query-replace "Query replace regexp: "
  1675. "Query replace: ")))
  1676. (if (string= str "")
  1677. (progn
  1678. (setq vip-re-query-replace (not vip-re-query-replace))
  1679. (message "Query replace mode changed to %s."
  1680. (if vip-re-query-replace "regexp replace"
  1681. "string replace")))
  1682. (if vip-re-query-replace
  1683. (query-replace-regexp
  1684. str
  1685. (vip-read-string (format "Query replace regexp \"%s\" with: " str)))
  1686. (query-replace
  1687. str
  1688. (vip-read-string (format "Query replace \"%s\" with: " str)))))))
  1689. ;; marking
  1690. (defun vip-mark-beginning-of-buffer ()
  1691. (interactive)
  1692. (set-mark (point))
  1693. (goto-char (point-min))
  1694. (exchange-point-and-mark)
  1695. (message "mark set at the beginning of buffer"))
  1696. (defun vip-mark-end-of-buffer ()
  1697. (interactive)
  1698. (set-mark (point))
  1699. (goto-char (point-max))
  1700. (exchange-point-and-mark)
  1701. (message "mark set at the end of buffer"))
  1702. (defun vip-mark-point (char)
  1703. (interactive "c")
  1704. (cond ((and (<= ?a char) (<= char ?z))
  1705. (point-to-register (- char (- ?a ?\C-a)) nil))
  1706. ((= char ?<) (vip-mark-beginning-of-buffer))
  1707. ((= char ?>) (vip-mark-end-of-buffer))
  1708. ((= char ?.) (push-mark))
  1709. ((= char ?,) (set-mark-command 1))
  1710. ((= char ?D) (mark-defun))
  1711. (t (error ""))))
  1712. (defun vip-goto-mark (arg)
  1713. "Go to mark."
  1714. (interactive "P")
  1715. (let ((char (read-char)) (com (vip-getcom arg)))
  1716. (vip-goto-mark-subr char com nil)))
  1717. (defun vip-goto-mark-and-skip-white (arg)
  1718. "Go to mark and skip to first non-white on line."
  1719. (interactive "P")
  1720. (let ((char (read-char)) (com (vip-getCom arg)))
  1721. (vip-goto-mark-subr char com t)))
  1722. (defun vip-goto-mark-subr (char com skip-white)
  1723. (cond ((and (<= ?a char) (<= char ?z))
  1724. (let ((buff (current-buffer)))
  1725. (if com (move-marker vip-com-point (point)))
  1726. (goto-char (register-to-point (- char (- ?a ?\C-a))))
  1727. (if skip-white (back-to-indentation))
  1728. (vip-change-mode-to-vi)
  1729. (if com
  1730. (if (equal buff (current-buffer))
  1731. (vip-execute-com (if skip-white
  1732. 'vip-goto-mark-and-skip-white
  1733. 'vip-goto-mark)
  1734. nil com)
  1735. (switch-to-buffer buff)
  1736. (goto-char vip-com-point)
  1737. (vip-change-mode-to-vi)
  1738. (error "")))))
  1739. ((and (not skip-white) (= char ?`))
  1740. (if com (move-marker vip-com-point (point)))
  1741. (exchange-point-and-mark)
  1742. (if com (vip-execute-com 'vip-goto-mark nil com)))
  1743. ((and skip-white (= char ?'))
  1744. (if com (move-marker vip-com-point (point)))
  1745. (exchange-point-and-mark)
  1746. (back-to-indentation)
  1747. (if com (vip-execute-com 'vip-goto-mark-and-skip-white nil com)))
  1748. (t (error ""))))
  1749. (defun vip-exchange-point-and-mark ()
  1750. (interactive)
  1751. (exchange-point-and-mark)
  1752. (back-to-indentation))
  1753. (defun vip-keyboard-quit ()
  1754. "Abort partially formed or running command."
  1755. (interactive)
  1756. (setq vip-use-register nil)
  1757. (keyboard-quit))
  1758. (defun vip-ctl-c-equivalent (arg)
  1759. "Emulate C-c in Emacs mode."
  1760. (interactive "P")
  1761. (vip-ctl-key-equivalent "\C-c" arg))
  1762. (defun vip-ctl-x-equivalent (arg)
  1763. "Emulate C-x in Emacs mode."
  1764. (interactive "P")
  1765. (vip-ctl-key-equivalent "\C-x" arg))
  1766. (defun vip-ctl-key-equivalent (key arg)
  1767. (let ((char (read-char)))
  1768. (if (and (<= ?A char) (<= char ?Z))
  1769. (setq char (- char (- ?A ?\C-a))))
  1770. (vip-escape-to-emacs arg (list (aref key 0) char))))
  1771. ;; commands in insertion mode
  1772. (defun vip-delete-backward-word (arg)
  1773. "Delete previous word."
  1774. (interactive "p")
  1775. (save-excursion
  1776. (set-mark (point))
  1777. (backward-word arg)
  1778. (delete-region (point) (mark))))
  1779. ;; implement ex commands
  1780. (defvar ex-token-type nil
  1781. "type of token. if non-nil, gives type of address. if nil, it
  1782. is a command.")
  1783. (defvar ex-token nil
  1784. "value of token.")
  1785. (defvar ex-addresses nil
  1786. "list of ex addresses")
  1787. (defvar ex-flag nil
  1788. "flag for ex flag")
  1789. (defvar ex-buffer nil
  1790. "name of ex buffer")
  1791. (defvar ex-count nil
  1792. "value of ex count")
  1793. (defvar ex-g-flag nil
  1794. "flag for global command")
  1795. (defvar ex-g-variant nil
  1796. "if t global command is executed on lines not matching ex-g-pat")
  1797. (defvar ex-reg-exp nil
  1798. "save reg-exp used in substitute")
  1799. (defvar ex-repl nil
  1800. "replace pattern for substitute")
  1801. (defvar ex-g-pat nil
  1802. "pattern for global command")
  1803. (defvar ex-map (make-sparse-keymap)
  1804. "save commands for mapped keys")
  1805. (defvar ex-tag nil
  1806. "save ex tag")
  1807. (defvar ex-file nil)
  1808. (defvar ex-variant nil)
  1809. (defvar ex-offset nil)
  1810. (defvar ex-append nil)
  1811. (defun vip-nil ()
  1812. (interactive)
  1813. (error ""))
  1814. (defun vip-looking-back (str)
  1815. "returns t if looking back reg-exp STR before point."
  1816. (and (save-excursion (re-search-backward str nil t))
  1817. (= (point) (match-end 0))))
  1818. (defun vip-check-sub (str)
  1819. "check if ex-token is an initial segment of STR"
  1820. (let ((length (length ex-token)))
  1821. (if (and (<= length (length str))
  1822. (string= ex-token (substring str 0 length)))
  1823. (setq ex-token str)
  1824. (setq ex-token-type "non-command"))))
  1825. (defun vip-get-ex-com-subr ()
  1826. "get a complete ex command"
  1827. (set-mark (point))
  1828. (re-search-forward "[a-z][a-z]*")
  1829. (setq ex-token-type "command")
  1830. (setq ex-token (buffer-substring (point) (mark)))
  1831. (exchange-point-and-mark)
  1832. (cond ((looking-at "a")
  1833. (cond ((looking-at "ab") (vip-check-sub "abbreviate"))
  1834. ((looking-at "ar") (vip-check-sub "args"))
  1835. (t (vip-check-sub "append"))))
  1836. ((looking-at "[bh]") (setq ex-token-type "non-command"))
  1837. ((looking-at "c")
  1838. (if (looking-at "co") (vip-check-sub "copy")
  1839. (vip-check-sub "change")))
  1840. ((looking-at "d") (vip-check-sub "delete"))
  1841. ((looking-at "e")
  1842. (if (looking-at "ex") (vip-check-sub "ex")
  1843. (vip-check-sub "edit")))
  1844. ((looking-at "f") (vip-check-sub "file"))
  1845. ((looking-at "g") (vip-check-sub "global"))
  1846. ((looking-at "i") (vip-check-sub "insert"))
  1847. ((looking-at "j") (vip-check-sub "join"))
  1848. ((looking-at "l") (vip-check-sub "list"))
  1849. ((looking-at "m")
  1850. (cond ((looking-at "map") (vip-check-sub "map"))
  1851. ((looking-at "mar") (vip-check-sub "mark"))
  1852. (t (vip-check-sub "move"))))
  1853. ((looking-at "n")
  1854. (if (looking-at "nu") (vip-check-sub "number")
  1855. (vip-check-sub "next")))
  1856. ((looking-at "o") (vip-check-sub "open"))
  1857. ((looking-at "p")
  1858. (cond ((looking-at "pre") (vip-check-sub "preserve"))
  1859. ((looking-at "pu") (vip-check-sub "put"))
  1860. (t (vip-check-sub "print"))))
  1861. ((looking-at "q") (vip-check-sub "quit"))
  1862. ((looking-at "r")
  1863. (cond ((looking-at "rec") (vip-check-sub "recover"))
  1864. ((looking-at "rew") (vip-check-sub "rewind"))
  1865. (t (vip-check-sub "read"))))
  1866. ((looking-at "s")
  1867. (cond ((looking-at "se") (vip-check-sub "set"))
  1868. ((looking-at "sh") (vip-check-sub "shell"))
  1869. ((looking-at "so") (vip-check-sub "source"))
  1870. ((looking-at "st") (vip-check-sub "stop"))
  1871. (t (vip-check-sub "substitute"))))
  1872. ((looking-at "t")
  1873. (if (looking-at "ta") (vip-check-sub "tag")
  1874. (vip-check-sub "t")))
  1875. ((looking-at "u")
  1876. (cond ((looking-at "una") (vip-check-sub "unabbreviate"))
  1877. ((looking-at "unm") (vip-check-sub "unmap"))
  1878. (t (vip-check-sub "undo"))))
  1879. ((looking-at "v")
  1880. (cond ((looking-at "ve") (vip-check-sub "version"))
  1881. ((looking-at "vi") (vip-check-sub "visual"))
  1882. (t (vip-check-sub "v"))))
  1883. ((looking-at "w")
  1884. (if (looking-at "wq") (vip-check-sub "wq")
  1885. (vip-check-sub "write")))
  1886. ((looking-at "x") (vip-check-sub "xit"))
  1887. ((looking-at "y") (vip-check-sub "yank"))
  1888. ((looking-at "z") (vip-check-sub "z")))
  1889. (exchange-point-and-mark))
  1890. (defun vip-get-ex-token ()
  1891. "get an ex-token which is either an address or a command.
  1892. a token has type \(command, address, end-mark\) and value."
  1893. (with-current-buffer " *ex-working-space*"
  1894. (skip-chars-forward " \t")
  1895. (cond ((looking-at "[k#]")
  1896. (setq ex-token-type "command")
  1897. (setq ex-token (char-to-string (following-char)))
  1898. (forward-char 1))
  1899. ((looking-at "[a-z]") (vip-get-ex-com-subr))
  1900. ((looking-at "\\.")
  1901. (forward-char 1)
  1902. (setq ex-token-type "dot"))
  1903. ((looking-at "[0-9]")
  1904. (set-mark (point))
  1905. (re-search-forward "[0-9]*")
  1906. (setq ex-token-type
  1907. (cond ((string= ex-token-type "plus") "add-number")
  1908. ((string= ex-token-type "minus") "sub-number")
  1909. (t "abs-number")))
  1910. (setq ex-token (string-to-number (buffer-substring (point) (mark)))))
  1911. ((looking-at "\\$")
  1912. (forward-char 1)
  1913. (setq ex-token-type "end"))
  1914. ((looking-at "%")
  1915. (forward-char 1)
  1916. (setq ex-token-type "whole"))
  1917. ((looking-at "+")
  1918. (cond ((or (looking-at "+[-+]") (looking-at "+[\n|]"))
  1919. (forward-char 1)
  1920. (insert "1")
  1921. (backward-char 1)
  1922. (setq ex-token-type "plus"))
  1923. ((looking-at "+[0-9]")
  1924. (forward-char 1)
  1925. (setq ex-token-type "plus"))
  1926. (t
  1927. (error "Badly formed address"))))
  1928. ((looking-at "-")
  1929. (cond ((or (looking-at "-[-+]") (looking-at "-[\n|]"))
  1930. (forward-char 1)
  1931. (insert "1")
  1932. (backward-char 1)
  1933. (setq ex-token-type "minus"))
  1934. ((looking-at "-[0-9]")
  1935. (forward-char 1)
  1936. (setq ex-token-type "minus"))
  1937. (t
  1938. (error "Badly formed address"))))
  1939. ((looking-at "/")
  1940. (forward-char 1)
  1941. (set-mark (point))
  1942. (let ((cont t))
  1943. (while (and (not (eolp)) cont)
  1944. ;;(re-search-forward "[^/]*/")
  1945. (re-search-forward "[^/]*\\(/\\|\n\\)")
  1946. (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
  1947. (setq cont nil))))
  1948. (backward-char 1)
  1949. (setq ex-token (buffer-substring (point) (mark)))
  1950. (if (looking-at "/") (forward-char 1))
  1951. (setq ex-token-type "search-forward"))
  1952. ((looking-at "\\?")
  1953. (forward-char 1)
  1954. (set-mark (point))
  1955. (let ((cont t))
  1956. (while (and (not (eolp)) cont)
  1957. ;;(re-search-forward "[^\\?]*\\?")
  1958. (re-search-forward "[^\\?]*\\(\\?\\|\n\\)")
  1959. (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\\\?"))
  1960. (setq cont nil))
  1961. (backward-char 1)
  1962. (if (not (looking-at "\n")) (forward-char 1))))
  1963. (setq ex-token-type "search-backward")
  1964. (setq ex-token (buffer-substring (1- (point)) (mark))))
  1965. ((looking-at ",")
  1966. (forward-char 1)
  1967. (setq ex-token-type "comma"))
  1968. ((looking-at ";")
  1969. (forward-char 1)
  1970. (setq ex-token-type "semi-colon"))
  1971. ((looking-at "[!=><&~]")
  1972. (setq ex-token-type "command")
  1973. (setq ex-token (char-to-string (following-char)))
  1974. (forward-char 1))
  1975. ((looking-at "'")
  1976. (setq ex-token-type "goto-mark")
  1977. (forward-char 1)
  1978. (cond ((looking-at "'") (setq ex-token nil))
  1979. ((looking-at "[a-z]") (setq ex-token (following-char)))
  1980. (t (error "Marks are ' and a-z")))
  1981. (forward-char 1))
  1982. ((looking-at "\n")
  1983. (setq ex-token-type "end-mark")
  1984. (setq ex-token "goto"))
  1985. (t
  1986. (error "invalid token")))))
  1987. (defun vip-ex (&optional string)
  1988. "ex commands within VIP."
  1989. (interactive)
  1990. (or string
  1991. (setq ex-g-flag nil
  1992. ex-g-variant nil))
  1993. (let ((com-str (or string (vip-read-string ":")))
  1994. (address nil) (cont t) (dot (point)))
  1995. (with-current-buffer (get-buffer-create " *ex-working-space*")
  1996. (delete-region (point-min) (point-max))
  1997. (insert com-str "\n")
  1998. (goto-char (point-min)))
  1999. (setq ex-token-type "")
  2000. (setq ex-addresses nil)
  2001. (while cont
  2002. (vip-get-ex-token)
  2003. (cond ((or (string= ex-token-type "command")
  2004. (string= ex-token-type "end-mark"))
  2005. (if address (setq ex-addresses (cons address ex-addresses)))
  2006. (cond ((string= ex-token "global")
  2007. (ex-global nil)
  2008. (setq cont nil))
  2009. ((string= ex-token "v")
  2010. (ex-global t)
  2011. (setq cont nil))
  2012. (t
  2013. (vip-execute-ex-command)
  2014. (with-current-buffer " *ex-working-space*"
  2015. (skip-chars-forward " \t")
  2016. (cond ((looking-at "|")
  2017. (forward-char 1))
  2018. ((looking-at "\n")
  2019. (setq cont nil))
  2020. (t (error "Extra character at end of a command")))))))
  2021. ((string= ex-token-type "non-command")
  2022. (error "%s: Not an editor command" ex-token))
  2023. ((string= ex-token-type "whole")
  2024. (setq ex-addresses
  2025. (cons (point-max) (cons (point-min) ex-addresses))))
  2026. ((string= ex-token-type "comma")
  2027. (setq ex-addresses
  2028. (cons (if (null address) (point) address) ex-addresses)))
  2029. ((string= ex-token-type "semi-colon")
  2030. (if address (setq dot address))
  2031. (setq ex-addresses
  2032. (cons (if (null address) (point) address) ex-addresses)))
  2033. (t (let ((ans (vip-get-ex-address-subr address dot)))
  2034. (if ans (setq address ans))))))))
  2035. (defun vip-get-ex-pat ()
  2036. "get a regular expression and set ex-variant if found"
  2037. (with-current-buffer " *ex-working-space*"
  2038. (skip-chars-forward " \t")
  2039. (if (looking-at "!")
  2040. (progn
  2041. (setq ex-g-variant (not ex-g-variant)
  2042. ex-g-flag (not ex-g-flag))
  2043. (forward-char 1)
  2044. (skip-chars-forward " \t")))
  2045. (if (looking-at "/")
  2046. (progn
  2047. (forward-char 1)
  2048. (set-mark (point))
  2049. (let ((cont t))
  2050. (while (and (not (eolp)) cont)
  2051. (re-search-forward "[^/]*\\(/\\|\n\\)")
  2052. ;;(re-search-forward "[^/]*/")
  2053. (if (not (vip-looking-back "[^\\\\]\\(\\\\\\\\\\)*\\\\/"))
  2054. (setq cont nil))))
  2055. (setq ex-token
  2056. (if (= (mark) (point)) ""
  2057. (buffer-substring (1- (point)) (mark))))
  2058. (backward-char 1))
  2059. (setq ex-token nil))))
  2060. (defun vip-get-ex-command ()
  2061. "get an ex command"
  2062. (with-current-buffer " *ex-working-space*"
  2063. (if (looking-at "/") (forward-char 1))
  2064. (skip-chars-forward " \t")
  2065. (cond ((looking-at "[a-z]")
  2066. (vip-get-ex-com-subr)
  2067. (if (string= ex-token-type "non-command")
  2068. (error "%s: not an editor command" ex-token)))
  2069. ((looking-at "[!=><&~]")
  2070. (setq ex-token (char-to-string (following-char)))
  2071. (forward-char 1))
  2072. (t (error "Could not find an ex command")))))
  2073. (defun vip-get-ex-opt-gc ()
  2074. "get an ex option g or c"
  2075. (with-current-buffer " *ex-working-space*"
  2076. (if (looking-at "/") (forward-char 1))
  2077. (skip-chars-forward " \t")
  2078. (cond ((looking-at "g")
  2079. (setq ex-token "g")
  2080. (forward-char 1)
  2081. t)
  2082. ((looking-at "c")
  2083. (setq ex-token "c")
  2084. (forward-char 1)
  2085. t)
  2086. (t nil))))
  2087. (defun vip-default-ex-addresses (&optional whole-flag)
  2088. "compute default addresses. whole-flag means whole buffer."
  2089. (cond ((null ex-addresses)
  2090. (setq ex-addresses
  2091. (if whole-flag
  2092. (cons (point-max) (cons (point-min) nil))
  2093. (cons (point) (cons (point) nil)))))
  2094. ((null (cdr ex-addresses))
  2095. (setq ex-addresses
  2096. (cons (car ex-addresses) ex-addresses)))))
  2097. (defun vip-get-ex-address ()
  2098. "get an ex-address as a marker and set ex-flag if a flag is found"
  2099. (let ((address (point-marker)) (cont t))
  2100. (setq ex-token "")
  2101. (setq ex-flag nil)
  2102. (while cont
  2103. (vip-get-ex-token)
  2104. (cond ((string= ex-token-type "command")
  2105. (if (or (string= ex-token "print") (string= ex-token "list")
  2106. (string= ex-token "#"))
  2107. (progn
  2108. (setq ex-flag t)
  2109. (setq cont nil))
  2110. (error "address expected")))
  2111. ((string= ex-token-type "end-mark")
  2112. (setq cont nil))
  2113. ((string= ex-token-type "whole")
  2114. (error "a trailing address is expected"))
  2115. ((string= ex-token-type "comma")
  2116. (error "Extra characters after an address"))
  2117. (t (let ((ans (vip-get-ex-address-subr address (point-marker))))
  2118. (if ans (setq address ans))))))
  2119. address))
  2120. (defun vip-get-ex-address-subr (old-address dot)
  2121. "returns an address as a point"
  2122. (let ((address nil))
  2123. (if (null old-address) (setq old-address dot))
  2124. (cond ((string= ex-token-type "dot")
  2125. (setq address dot))
  2126. ((string= ex-token-type "add-number")
  2127. (save-excursion
  2128. (goto-char old-address)
  2129. (forward-line (if (= old-address 0) (1- ex-token) ex-token))
  2130. (setq address (point-marker))))
  2131. ((string= ex-token-type "sub-number")
  2132. (save-excursion
  2133. (goto-char old-address)
  2134. (forward-line (- ex-token))
  2135. (setq address (point-marker))))
  2136. ((string= ex-token-type "abs-number")
  2137. (save-excursion
  2138. (goto-char (point-min))
  2139. (if (= ex-token 0) (setq address 0)
  2140. (forward-line (1- ex-token))
  2141. (setq address (point-marker)))))
  2142. ((string= ex-token-type "end")
  2143. (setq address (point-max-marker)))
  2144. ((string= ex-token-type "plus") t);; do nothing
  2145. ((string= ex-token-type "minus") t);; do nothing
  2146. ((string= ex-token-type "search-forward")
  2147. (save-excursion
  2148. (ex-search-address t)
  2149. (setq address (point-marker))))
  2150. ((string= ex-token-type "search-backward")
  2151. (save-excursion
  2152. (ex-search-address nil)
  2153. (setq address (point-marker))))
  2154. ((string= ex-token-type "goto-mark")
  2155. (save-excursion
  2156. (if (null ex-token)
  2157. (exchange-point-and-mark)
  2158. (goto-char (register-to-point (- ex-token (- ?a ?\C-a)))))
  2159. (setq address (point-marker)))))
  2160. address))
  2161. (defun ex-search-address (forward)
  2162. "search pattern and set address"
  2163. (if (string= ex-token "")
  2164. (if (null vip-s-string) (error "No previous search string")
  2165. (setq ex-token vip-s-string))
  2166. (setq vip-s-string ex-token))
  2167. (if forward
  2168. (progn
  2169. (forward-line 1)
  2170. (re-search-forward ex-token))
  2171. (forward-line -1)
  2172. (re-search-backward ex-token)))
  2173. (defun vip-get-ex-buffer ()
  2174. "get a buffer name and set ex-count and ex-flag if found"
  2175. (setq ex-buffer nil)
  2176. (setq ex-count nil)
  2177. (setq ex-flag nil)
  2178. (with-current-buffer " *ex-working-space*"
  2179. (skip-chars-forward " \t")
  2180. (if (looking-at "[a-zA-Z]")
  2181. (progn
  2182. (setq ex-buffer (following-char))
  2183. (forward-char 1)
  2184. (skip-chars-forward " \t")))
  2185. (if (looking-at "[0-9]")
  2186. (progn
  2187. (set-mark (point))
  2188. (re-search-forward "[0-9][0-9]*")
  2189. (setq ex-count (string-to-number (buffer-substring (point) (mark))))
  2190. (skip-chars-forward " \t")))
  2191. (if (looking-at "[pl#]")
  2192. (progn
  2193. (setq ex-flag t)
  2194. (forward-char 1)))
  2195. (if (not (looking-at "[\n|]"))
  2196. (error "Invalid extra characters"))))
  2197. (defun vip-get-ex-count ()
  2198. (setq ex-variant nil
  2199. ex-count nil
  2200. ex-flag nil)
  2201. (with-current-buffer " *ex-working-space*"
  2202. (skip-chars-forward " \t")
  2203. (if (looking-at "!")
  2204. (progn
  2205. (setq ex-variant t)
  2206. (forward-char 1)))
  2207. (skip-chars-forward " \t")
  2208. (if (looking-at "[0-9]")
  2209. (progn
  2210. (set-mark (point))
  2211. (re-search-forward "[0-9][0-9]*")
  2212. (setq ex-count (string-to-number (buffer-substring (point) (mark))))
  2213. (skip-chars-forward " \t")))
  2214. (if (looking-at "[pl#]")
  2215. (progn
  2216. (setq ex-flag t)
  2217. (forward-char 1)))
  2218. (if (not (looking-at "[\n|]"))
  2219. (error "Invalid extra characters"))))
  2220. (defun vip-get-ex-file ()
  2221. "get a file name and set ex-variant, ex-append and ex-offset if found"
  2222. (setq ex-file nil
  2223. ex-variant nil
  2224. ex-append nil
  2225. ex-offset nil)
  2226. (with-current-buffer " *ex-working-space*"
  2227. (skip-chars-forward " \t")
  2228. (if (looking-at "!")
  2229. (progn
  2230. (setq ex-variant t)
  2231. (forward-char 1)
  2232. (skip-chars-forward " \t")))
  2233. (if (looking-at ">>")
  2234. (progn
  2235. (setq ex-append t
  2236. ex-variant t)
  2237. (forward-char 2)
  2238. (skip-chars-forward " \t")))
  2239. (if (looking-at "+")
  2240. (progn
  2241. (forward-char 1)
  2242. (set-mark (point))
  2243. (re-search-forward "[ \t\n]")
  2244. (backward-char 1)
  2245. (setq ex-offset (buffer-substring (point) (mark)))
  2246. (forward-char 1)
  2247. (skip-chars-forward " \t")))
  2248. (set-mark (point))
  2249. (re-search-forward "[ \t\n]")
  2250. (backward-char 1)
  2251. (setq ex-file (buffer-substring (point) (mark)))))
  2252. (defun vip-execute-ex-command ()
  2253. "execute ex command using the value of addresses."
  2254. (cond ((string= ex-token "goto") (ex-goto))
  2255. ((string= ex-token "copy") (ex-copy nil))
  2256. ((string= ex-token "delete") (ex-delete))
  2257. ((string= ex-token "edit") (ex-edit))
  2258. ((string= ex-token "file") (vip-info-on-file))
  2259. ;((string= ex-token "global") (ex-global nil))
  2260. ((string= ex-token "join") (ex-line "join"))
  2261. ((string= ex-token "k") (ex-mark))
  2262. ((string= ex-token "mark") (ex-mark))
  2263. ((string= ex-token "map") (ex-map))
  2264. ((string= ex-token "move") (ex-copy t))
  2265. ((string= ex-token "put") (ex-put))
  2266. ((string= ex-token "quit") (ex-quit))
  2267. ((string= ex-token "read") (ex-read))
  2268. ((string= ex-token "set") (ex-set))
  2269. ((string= ex-token "shell") (ex-shell))
  2270. ((string= ex-token "substitute") (ex-substitute))
  2271. ((string= ex-token "stop") (suspend-emacs))
  2272. ((string= ex-token "t") (ex-copy nil))
  2273. ((string= ex-token "tag") (ex-tag))
  2274. ((string= ex-token "undo") (vip-undo))
  2275. ((string= ex-token "unmap") (ex-unmap))
  2276. ;((string= ex-token "v") (ex-global t))
  2277. ((string= ex-token "version") (vip-version))
  2278. ((string= ex-token "visual") (ex-edit))
  2279. ((string= ex-token "write") (ex-write nil))
  2280. ((string= ex-token "wq") (ex-write t))
  2281. ((string= ex-token "yank") (ex-yank))
  2282. ((string= ex-token "!") (ex-command))
  2283. ((string= ex-token "=") (ex-line-no))
  2284. ((string= ex-token ">") (ex-line "right"))
  2285. ((string= ex-token "<") (ex-line "left"))
  2286. ((string= ex-token "&") (ex-substitute t))
  2287. ((string= ex-token "~") (ex-substitute t t))
  2288. ((or (string= ex-token "append")
  2289. (string= ex-token "args")
  2290. (string= ex-token "change")
  2291. (string= ex-token "insert")
  2292. (string= ex-token "open")
  2293. )
  2294. (error "%s: no such command from VIP" ex-token))
  2295. ((or (string= ex-token "abbreviate")
  2296. (string= ex-token "list")
  2297. (string= ex-token "next")
  2298. (string= ex-token "print")
  2299. (string= ex-token "preserve")
  2300. (string= ex-token "recover")
  2301. (string= ex-token "rewind")
  2302. (string= ex-token "source")
  2303. (string= ex-token "unabbreviate")
  2304. (string= ex-token "xit")
  2305. (string= ex-token "z")
  2306. )
  2307. (error "%s: not implemented in VIP" ex-token))
  2308. (t (error "%s: Not an editor command" ex-token))))
  2309. (defun ex-goto ()
  2310. "ex goto command"
  2311. (if (null ex-addresses)
  2312. (setq ex-addresses (cons (point) nil)))
  2313. (push-mark (point))
  2314. (goto-char (car ex-addresses))
  2315. (beginning-of-line))
  2316. (defun ex-copy (del-flag)
  2317. "ex copy and move command. DEL-FLAG means delete."
  2318. (vip-default-ex-addresses)
  2319. (let ((address (vip-get-ex-address))
  2320. (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  2321. (goto-char end)
  2322. (save-excursion
  2323. (set-mark beg)
  2324. (vip-enlarge-region (mark) (point))
  2325. (if del-flag (kill-region (point) (mark))
  2326. (copy-region-as-kill (point) (mark)))
  2327. (if ex-flag
  2328. (progn
  2329. (with-output-to-temp-buffer "*copy text*"
  2330. (princ
  2331. (if (or del-flag ex-g-flag ex-g-variant)
  2332. (current-kill 0)
  2333. (buffer-substring (point) (mark)))))
  2334. (condition-case nil
  2335. (progn
  2336. (vip-read-string "[Hit return to continue] ")
  2337. (save-excursion (kill-buffer "*copy text*")))
  2338. (quit
  2339. (save-excursion (kill-buffer "*copy text*"))
  2340. (signal 'quit nil))))))
  2341. (if (= address 0)
  2342. (goto-char (point-min))
  2343. (goto-char address)
  2344. (forward-line 1))
  2345. (insert (current-kill 0))))
  2346. (defun ex-delete ()
  2347. "ex delete"
  2348. (vip-default-ex-addresses)
  2349. (vip-get-ex-buffer)
  2350. (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  2351. (if (> beg end) (error "First address exceeds second"))
  2352. (save-excursion
  2353. (vip-enlarge-region beg end)
  2354. (exchange-point-and-mark)
  2355. (if ex-count
  2356. (progn
  2357. (set-mark (point))
  2358. (forward-line (1- ex-count)))
  2359. (set-mark end))
  2360. (vip-enlarge-region (point) (mark))
  2361. (if ex-flag
  2362. ;; show text to be deleted and ask for confirmation
  2363. (progn
  2364. (with-output-to-temp-buffer " *delete text*"
  2365. (princ (buffer-substring (point) (mark))))
  2366. (condition-case conditions
  2367. (vip-read-string "[Hit return to continue] ")
  2368. (quit
  2369. (save-excursion (kill-buffer " *delete text*"))
  2370. (error "")))
  2371. (save-excursion (kill-buffer " *delete text*")))
  2372. (if ex-buffer
  2373. (if (and (<= ?A ex-buffer) (<= ex-buffer ?Z))
  2374. (vip-append-to-register
  2375. (+ ex-buffer 32) (point) (mark))
  2376. (copy-to-register ex-buffer (point) (mark) nil)))
  2377. (delete-region (point) (mark))))))
  2378. (defun ex-edit ()
  2379. "ex-edit"
  2380. (vip-get-ex-file)
  2381. (if (and (not ex-variant) (buffer-modified-p) buffer-file-name)
  2382. (error "No write since last change \(:e! overrides\)"))
  2383. (vip-change-mode-to-emacs)
  2384. (set-buffer
  2385. (find-file-noselect (concat default-directory ex-file)))
  2386. (vip-change-mode-to-vi)
  2387. (goto-char (point-min))
  2388. (if ex-offset
  2389. (progn
  2390. (with-current-buffer " *ex-working-space*"
  2391. (delete-region (point-min) (point-max))
  2392. (insert ex-offset "\n")
  2393. (goto-char (point-min)))
  2394. (goto-char (vip-get-ex-address))
  2395. (beginning-of-line))))
  2396. (defun ex-global (variant)
  2397. "ex global command"
  2398. (if (or ex-g-flag ex-g-variant)
  2399. (error "Global within global not allowed")
  2400. (if variant
  2401. (setq ex-g-flag nil
  2402. ex-g-variant t)
  2403. (setq ex-g-flag t
  2404. ex-g-variant nil)))
  2405. (vip-get-ex-pat)
  2406. (if (null ex-token)
  2407. (error "Missing regular expression for global command"))
  2408. (if (string= ex-token "")
  2409. (if (null vip-s-string) (error "No previous search string")
  2410. (setq ex-g-pat vip-s-string))
  2411. (setq ex-g-pat ex-token
  2412. vip-s-string ex-token))
  2413. (if (null ex-addresses)
  2414. (setq ex-addresses (list (point-max) (point-min))))
  2415. (let ((marks nil) (mark-count 0)
  2416. com-str (end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  2417. (if (> beg end) (error "First address exceeds second"))
  2418. (save-excursion
  2419. (vip-enlarge-region beg end)
  2420. (exchange-point-and-mark)
  2421. (let ((cont t) (limit (point-marker)))
  2422. (exchange-point-and-mark)
  2423. ;; skip the last line if empty
  2424. (beginning-of-line)
  2425. (if (and (eobp) (not (bobp))) (backward-char 1))
  2426. (while (and cont (not (bobp)) (>= (point) limit))
  2427. (beginning-of-line)
  2428. (set-mark (point))
  2429. (end-of-line)
  2430. (let ((found (re-search-backward ex-g-pat (mark) t)))
  2431. (if (or (and ex-g-flag found)
  2432. (and ex-g-variant (not found)))
  2433. (progn
  2434. (end-of-line)
  2435. (setq mark-count (1+ mark-count))
  2436. (setq marks (cons (point-marker) marks)))))
  2437. (beginning-of-line)
  2438. (if (bobp) (setq cont nil)
  2439. (forward-line -1)
  2440. (end-of-line)))))
  2441. (with-current-buffer " *ex-working-space*"
  2442. (setq com-str (buffer-substring (1+ (point)) (1- (point-max)))))
  2443. (while marks
  2444. (goto-char (car marks))
  2445. ;; report progress of execution on a slow machine.
  2446. ;;(message "Executing global command...")
  2447. ;;(if (zerop (% mark-count 10))
  2448. ;; (message "Executing global command...%d" mark-count))
  2449. (vip-ex com-str)
  2450. (setq mark-count (1- mark-count))
  2451. (setq marks (cdr marks)))))
  2452. ;;(message "Executing global command...done")))
  2453. (defun ex-line (com)
  2454. "ex line commands. COM is join, shift-right or shift-left."
  2455. (vip-default-ex-addresses)
  2456. (vip-get-ex-count)
  2457. (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))) point)
  2458. (if (> beg end) (error "First address exceeds second"))
  2459. (save-excursion
  2460. (vip-enlarge-region beg end)
  2461. (exchange-point-and-mark)
  2462. (if ex-count
  2463. (progn
  2464. (set-mark (point))
  2465. (forward-line ex-count)))
  2466. (if ex-flag
  2467. ;; show text to be joined and ask for confirmation
  2468. (progn
  2469. (with-output-to-temp-buffer " *text*"
  2470. (princ (buffer-substring (point) (mark))))
  2471. (condition-case conditions
  2472. (progn
  2473. (vip-read-string "[Hit return to continue] ")
  2474. (ex-line-subr com (point) (mark)))
  2475. (quit
  2476. (ding)))
  2477. (save-excursion (kill-buffer " *text*")))
  2478. (ex-line-subr com (point) (mark)))
  2479. (setq point (point)))
  2480. (goto-char (1- point))
  2481. (beginning-of-line)))
  2482. (defun ex-line-subr (com beg end)
  2483. (cond ((string= com "join")
  2484. (goto-char (min beg end))
  2485. (while (and (not (eobp)) (< (point) (max beg end)))
  2486. (end-of-line)
  2487. (if (and (<= (point) (max beg end)) (not (eobp)))
  2488. (progn
  2489. (forward-line 1)
  2490. (delete-region (point) (1- (point)))
  2491. (if (not ex-variant) (fixup-whitespace))))))
  2492. ((or (string= com "right") (string= com "left"))
  2493. (indent-rigidly
  2494. (min beg end) (max beg end)
  2495. (if (string= com "right") vip-shift-width (- vip-shift-width)))
  2496. (goto-char (max beg end))
  2497. (end-of-line)
  2498. (forward-char 1))))
  2499. (defun ex-mark ()
  2500. "ex mark"
  2501. (let (char)
  2502. (if (null ex-addresses)
  2503. (setq ex-addresses
  2504. (cons (point) nil)))
  2505. (with-current-buffer " *ex-working-space*"
  2506. (skip-chars-forward " \t")
  2507. (if (looking-at "[a-z]")
  2508. (progn
  2509. (setq char (following-char))
  2510. (forward-char 1)
  2511. (skip-chars-forward " \t")
  2512. (if (not (looking-at "[\n|]"))
  2513. (error "Extra characters at end of \"k\" command")))
  2514. (if (looking-at "[\n|]")
  2515. (error "\"k\" requires a following letter")
  2516. (error "Mark must specify a letter"))))
  2517. (save-excursion
  2518. (goto-char (car ex-addresses))
  2519. (point-to-register (- char (- ?a ?\C-a)) nil))))
  2520. (defun ex-map ()
  2521. "ex map"
  2522. (let (char string)
  2523. (with-current-buffer " *ex-working-space*"
  2524. (skip-chars-forward " \t")
  2525. (setq char (char-to-string (following-char)))
  2526. (forward-char 1)
  2527. (skip-chars-forward " \t")
  2528. (if (looking-at "[\n|]") (error "Missing rhs"))
  2529. (set-mark (point))
  2530. (with-no-warnings
  2531. (end-of-buffer))
  2532. (backward-char 1)
  2533. (setq string (buffer-substring (mark) (point))))
  2534. (if (not (lookup-key ex-map char))
  2535. (define-key ex-map char
  2536. (or (lookup-key vip-mode-map char) 'vip-nil)))
  2537. (define-key vip-mode-map char
  2538. (eval
  2539. (list 'quote
  2540. (cons 'lambda
  2541. (list '(count)
  2542. '(interactive "p")
  2543. (list 'execute-kbd-macro string 'count))))))))
  2544. (defun ex-unmap ()
  2545. "ex unmap"
  2546. (let (char)
  2547. (with-current-buffer " *ex-working-space*"
  2548. (skip-chars-forward " \t")
  2549. (setq char (char-to-string (following-char)))
  2550. (forward-char 1)
  2551. (skip-chars-forward " \t")
  2552. (if (not (looking-at "[\n|]")) (error "Macro must be a character")))
  2553. (if (not (lookup-key ex-map char))
  2554. (error "That macro wasn't mapped"))
  2555. (define-key vip-mode-map char (lookup-key ex-map char))
  2556. (define-key ex-map char nil)))
  2557. (defun ex-put ()
  2558. "ex put"
  2559. (let ((point (if (null ex-addresses) (point) (car ex-addresses))))
  2560. (vip-get-ex-buffer)
  2561. (setq vip-use-register ex-buffer)
  2562. (goto-char point)
  2563. (if (= point 0) (vip-Put-back 1) (vip-put-back 1))))
  2564. (defun ex-quit ()
  2565. "ex quit"
  2566. (let (char)
  2567. (with-current-buffer " *ex-working-space*"
  2568. (skip-chars-forward " \t")
  2569. (setq char (following-char)))
  2570. (if (= char ?!) (kill-emacs t) (save-buffers-kill-emacs))))
  2571. (defun ex-read ()
  2572. "ex read"
  2573. (let ((point (if (null ex-addresses) (point) (car ex-addresses)))
  2574. (variant nil) command file)
  2575. (goto-char point)
  2576. (if (not (= point 0)) (with-no-warnings (next-line 1)))
  2577. (beginning-of-line)
  2578. (with-current-buffer " *ex-working-space*"
  2579. (skip-chars-forward " \t")
  2580. (if (looking-at "!")
  2581. (progn
  2582. (setq variant t)
  2583. (forward-char 1)
  2584. (skip-chars-forward " \t")
  2585. (set-mark (point))
  2586. (end-of-line)
  2587. (setq command (buffer-substring (mark) (point))))
  2588. (set-mark (point))
  2589. (re-search-forward "[ \t\n]")
  2590. (backward-char 1)
  2591. (setq file (buffer-substring (point) (mark)))))
  2592. (if variant
  2593. (shell-command command t)
  2594. (with-no-warnings
  2595. (insert-file file)))))
  2596. (defun ex-set ()
  2597. (eval (list 'setq
  2598. (read-variable "Variable: ")
  2599. (eval (read-minibuffer "Value: ")))))
  2600. (defun ex-shell ()
  2601. "ex shell"
  2602. (vip-change-mode-to-emacs)
  2603. (shell))
  2604. (defun ex-substitute (&optional repeat r-flag)
  2605. "ex substitute.
  2606. If REPEAT use previous reg-exp which is ex-reg-exp or
  2607. vip-s-string"
  2608. (let (pat repl (opt-g nil) (opt-c nil) (matched-pos nil))
  2609. (if repeat (setq ex-token nil) (vip-get-ex-pat))
  2610. (if (null ex-token)
  2611. (setq pat (if r-flag vip-s-string ex-reg-exp)
  2612. repl ex-repl)
  2613. (setq pat (if (string= ex-token "") vip-s-string ex-token))
  2614. (setq vip-s-string pat
  2615. ex-reg-exp pat)
  2616. (vip-get-ex-pat)
  2617. (if (null ex-token)
  2618. (setq ex-token ""
  2619. ex-repl "")
  2620. (setq repl ex-token
  2621. ex-repl ex-token)))
  2622. (while (vip-get-ex-opt-gc)
  2623. (if (string= ex-token "g") (setq opt-g t) (setq opt-c t)))
  2624. (vip-get-ex-count)
  2625. (if ex-count
  2626. (save-excursion
  2627. (if ex-addresses (goto-char (car ex-addresses)))
  2628. (set-mark (point))
  2629. (forward-line (1- ex-count))
  2630. (setq ex-addresses (cons (point) (cons (mark) nil))))
  2631. (if (null ex-addresses)
  2632. (setq ex-addresses (cons (point) (cons (point) nil)))
  2633. (if (null (cdr ex-addresses))
  2634. (setq ex-addresses (cons (car ex-addresses) ex-addresses)))))
  2635. ;(setq G opt-g)
  2636. (let ((beg (car ex-addresses)) (end (car (cdr ex-addresses)))
  2637. (cont t) eol-mark)
  2638. (save-excursion
  2639. (vip-enlarge-region beg end)
  2640. (let ((limit (save-excursion
  2641. (goto-char (max (point) (mark)))
  2642. (point-marker))))
  2643. (goto-char (min (point) (mark)))
  2644. (while (< (point) limit)
  2645. (end-of-line)
  2646. (setq eol-mark (point-marker))
  2647. (beginning-of-line)
  2648. (if opt-g
  2649. (progn
  2650. (while (and (not (eolp))
  2651. (re-search-forward pat eol-mark t))
  2652. (if (or (not opt-c) (y-or-n-p "Replace? "))
  2653. (progn
  2654. (setq matched-pos (point))
  2655. (replace-match repl))))
  2656. (end-of-line)
  2657. (forward-char))
  2658. (if (and (re-search-forward pat eol-mark t)
  2659. (or (not opt-c) (y-or-n-p "Replace? ")))
  2660. (progn
  2661. (setq matched-pos (point))
  2662. (replace-match repl)))
  2663. (end-of-line)
  2664. (forward-char))))))
  2665. (if matched-pos (goto-char matched-pos))
  2666. (beginning-of-line)
  2667. (if opt-c (message "done"))))
  2668. (defun ex-tag ()
  2669. "ex tag"
  2670. (let (tag)
  2671. (with-current-buffer " *ex-working-space*"
  2672. (skip-chars-forward " \t")
  2673. (set-mark (point))
  2674. (skip-chars-forward "^ |\t\n")
  2675. (setq tag (buffer-substring (mark) (point))))
  2676. (if (not (string= tag "")) (setq ex-tag tag))
  2677. (vip-change-mode-to-emacs)
  2678. (condition-case conditions
  2679. (progn
  2680. (if (string= tag "")
  2681. (find-tag ex-tag t)
  2682. (find-tag-other-window ex-tag))
  2683. (vip-change-mode-to-vi))
  2684. (error
  2685. (vip-change-mode-to-vi)
  2686. (vip-message-conditions conditions)))))
  2687. (defun ex-write (q-flag)
  2688. "ex write"
  2689. (vip-default-ex-addresses t)
  2690. (vip-get-ex-file)
  2691. (if (string= ex-file "")
  2692. (progn
  2693. (if (null buffer-file-name)
  2694. (error "No file associated with this buffer"))
  2695. (setq ex-file buffer-file-name))
  2696. (setq ex-file (expand-file-name ex-file)))
  2697. (if (and (not (string= ex-file (buffer-file-name)))
  2698. (file-exists-p ex-file)
  2699. (not ex-variant))
  2700. (error "\"%s\" File exists - use w! to override" ex-file))
  2701. (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  2702. (if (> beg end) (error "First address exceeds second"))
  2703. (save-excursion
  2704. (vip-enlarge-region beg end)
  2705. (write-region (point) (mark) ex-file ex-append t)))
  2706. (if (null buffer-file-name) (setq buffer-file-name ex-file))
  2707. (if q-flag (save-buffers-kill-emacs)))
  2708. (defun ex-yank ()
  2709. "ex yank"
  2710. (vip-default-ex-addresses)
  2711. (vip-get-ex-buffer)
  2712. (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  2713. (if (> beg end) (error "First address exceeds second"))
  2714. (save-excursion
  2715. (vip-enlarge-region beg end)
  2716. (exchange-point-and-mark)
  2717. (if (or ex-g-flag ex-g-variant) (error "Can't yank within global"))
  2718. (if ex-count
  2719. (progn
  2720. (set-mark (point))
  2721. (forward-line (1- ex-count)))
  2722. (set-mark end))
  2723. (vip-enlarge-region (point) (mark))
  2724. (if ex-flag (error "Extra characters at end of command"))
  2725. (if ex-buffer
  2726. (copy-to-register ex-buffer (point) (mark) nil))
  2727. (copy-region-as-kill (point) (mark)))))
  2728. (defun ex-command ()
  2729. "execute shell command"
  2730. (let (command)
  2731. (with-current-buffer " *ex-working-space*"
  2732. (skip-chars-forward " \t")
  2733. (set-mark (point))
  2734. (end-of-line)
  2735. (setq command (buffer-substring (mark) (point))))
  2736. (if (null ex-addresses)
  2737. (shell-command command)
  2738. (let ((end (car ex-addresses)) (beg (car (cdr ex-addresses))))
  2739. (if (null beg) (setq beg end))
  2740. (save-excursion
  2741. (goto-char beg)
  2742. (set-mark end)
  2743. (vip-enlarge-region (point) (mark))
  2744. (shell-command-on-region (point) (mark) command t))
  2745. (goto-char beg)))))
  2746. (defun ex-line-no ()
  2747. "print line number"
  2748. (message "%d"
  2749. (1+ (count-lines
  2750. (point-min)
  2751. (if (null ex-addresses) (point-max) (car ex-addresses))))))
  2752. (if (file-exists-p vip-startup-file) (load vip-startup-file))
  2753. (provide 'vip)
  2754. ;;; vip.el ends here