tpu-edt.el 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. ;;; tpu-edt.el --- Emacs emulating TPU emulating EDT
  2. ;; Copyright (C) 1993-1995, 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Rob Riepel <riepel@networking.stanford.edu>
  4. ;; Maintainer: Rob Riepel <riepel@networking.stanford.edu>
  5. ;; Version: 4.5
  6. ;; Keywords: emulations
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;; TPU-edt is based on tpu.el by Jeff Kowalski and Bob Covey.
  19. ;;; Commentary:
  20. ;; %% TPU-edt -- Emacs emulating TPU emulating EDT
  21. ;; %% Contents
  22. ;; % Introduction
  23. ;; % Differences Between TPU-edt and DEC TPU/edt
  24. ;; % Starting TPU-edt
  25. ;; % Customizing TPU-edt using the Emacs Initialization File
  26. ;; % Regular Expressions in TPU-edt
  27. ;; %% Introduction
  28. ;; TPU-edt emulates the popular DEC VMS editor EDT (actually, it emulates
  29. ;; DEC TPU's EDT emulation, hence the name TPU-edt). TPU-edt features the
  30. ;; following TPU/edt functionality:
  31. ;; . EDT keypad
  32. ;; . On-line help
  33. ;; . Repeat counts
  34. ;; . Scroll margins
  35. ;; . Learn sequences
  36. ;; . Free cursor mode
  37. ;; . Rectangular cut and paste
  38. ;; . Multiple windows and buffers
  39. ;; . TPU line-mode REPLACE command
  40. ;; . Wild card search and substitution
  41. ;; . Configurable through an initialization file
  42. ;; . History recall of search strings, file names, and commands
  43. ;; Please note that TPU-edt does NOT emulate TPU. It emulates TPU's EDT
  44. ;; emulation. Very few TPU line-mode commands are supported.
  45. ;; TPU-edt, like its VMS cousin, works on VT-series terminals with DEC
  46. ;; style keyboards. VT terminal emulators, including xterm with the
  47. ;; appropriate key translations, work just fine too.
  48. ;; TPU-edt works with X-windows. This is accomplished through a TPU-edt
  49. ;; X key map. The tpu-mapper command creates this map and stores it in a
  50. ;; file. See the tpu-mapper command help for more information, or just
  51. ;; run it and follow the directions.
  52. ;; %% Differences Between TPU-edt and DEC TPU/edt
  53. ;; In some cases, Emacs doesn't support text highlighting, so selected
  54. ;; regions are not shown in inverse video. Emacs uses the concept of "the
  55. ;; mark". The mark is set at one end of a selected region; the cursor is
  56. ;; at the other. In cases where the selected region cannot be shown in
  57. ;; inverse video an at sign (@) appears in the mode line when mark is set.
  58. ;; The native Emacs command ^X^X (Control-X twice) exchanges the cursor
  59. ;; with the mark; this provides a handy way to find the location of the
  60. ;; mark.
  61. ;; In TPU the cursor can be either bound or free. Bound means the cursor
  62. ;; cannot wander outside the text of the file being edited. Free means
  63. ;; the arrow keys can move the cursor past the ends of lines. Free is the
  64. ;; default mode in TPU; bound is the only mode in EDT. Bound is the only
  65. ;; mode in the base version of TPU-edt; optional extensions add an
  66. ;; approximation of free mode, see the commentary in tpu-extras.el for
  67. ;; details.
  68. ;; Like TPU, Emacs uses multiple buffers. Some buffers are used to hold
  69. ;; files you are editing; other "internal" buffers are used for Emacs's own
  70. ;; purposes (like showing you help). Here are some commands for dealing
  71. ;; with buffers.
  72. ;; Gold-B moves to next buffer, including internal buffers
  73. ;; Gold-N moves to next buffer containing a file
  74. ;; Gold-M brings up a buffer menu (like TPU "show buffers")
  75. ;; Emacs is very fond of throwing up new windows. Dealing with all these
  76. ;; windows can be a little confusing at first, so here are a few commands
  77. ;; to that may help:
  78. ;; Gold-Next_Scr moves to the next window on the screen
  79. ;; Gold-Prev_Scr moves to the previous window on the screen
  80. ;; Gold-TAB also moves to the next window on the screen
  81. ;; Control-x 1 deletes all but the current window
  82. ;; Control-x 0 deletes the current window
  83. ;; Note that the buffers associated with deleted windows still exist!
  84. ;; Like TPU, TPU-edt has a "command" function, invoked with Gold-KP7 or
  85. ;; Do. Most of the commands available are Emacs commands. Some TPU
  86. ;; commands are available, they are: replace, exit, quit, include, and
  87. ;; Get (unfortunately, "get" is an internal Emacs function, so we are
  88. ;; stuck with "Get" - to make life easier, Get is available as Gold-g).
  89. ;; TPU-edt supports the recall of commands, file names, and search
  90. ;; strings. The history of strings recalled differs slightly from
  91. ;; TPU/edt, but it is still very convenient.
  92. ;; Help is available! The traditional help keys (Help and PF2) display
  93. ;; a small help file showing the default keypad layout, control key
  94. ;; functions, and Gold key functions. Pressing any key inside of help
  95. ;; splits the screen and prints a description of the function of the
  96. ;; pressed key. Gold-PF2 invokes the native Emacs help, with its
  97. ;; zillions of options.
  98. ;; Thanks to Emacs, TPU-edt has some extensions that may make your life
  99. ;; easier, or at least more interesting. For example, Gold-r toggles
  100. ;; TPU-edt rectangular mode. In rectangular mode, Remove and Insert work
  101. ;; on rectangles. Likewise, Gold-* toggles TPU-edt regular expression
  102. ;; mode. In regular expression mode Find, Find Next, and the line-mode
  103. ;; replace command work with regular expressions. [A regular expression
  104. ;; is a pattern that denotes a set of strings; like VMS wildcards.]
  105. ;; Emacs also gives TPU-edt the undo and occur functions. Undo does
  106. ;; what it says; it undoes the last change. Multiple undos in a row
  107. ;; undo multiple changes. For your convenience, undo is available on
  108. ;; Gold-u. Occur shows all the lines containing a specific string in
  109. ;; another window. Moving to that window, and typing ^C^C (Control-C
  110. ;; twice) on a particular line moves you back to the original window
  111. ;; at that line. Occur is on Gold-o.
  112. ;; Finally, as you edit, remember that all the power of Emacs is at
  113. ;; your disposal. It really is a fantastic tool. You may even want to
  114. ;; take some time and read the Emacs tutorial; perhaps not to learn the
  115. ;; native Emacs key bindings, but to get a feel for all the things
  116. ;; Emacs can do for you. The Emacs tutorial is available from the
  117. ;; Emacs help function: "Gold-PF2 t"
  118. ;; %% Starting TPU-edt
  119. ;; All you have to do to start TPU-edt, is turn it on. This can be
  120. ;; done from the command line when running Emacs.
  121. ;; prompt> emacs -f tpu-edt
  122. ;; If you've already started Emacs, turn on TPU-edt using the tpu-edt
  123. ;; command. First press `M-x' (that's usually `ESC' followed by `x')
  124. ;; and type `tpu-edt' followed by a carriage return.
  125. ;; If you like TPU-edt and want to use it all the time, you can start
  126. ;; TPU-edt using the Emacs initialization file, .emacs. Simply create
  127. ;; a .emacs file in your home directory containing the line:
  128. ;; (tpu-edt)
  129. ;; That's all you need to do to start TPU-edt.
  130. ;; %% Customizing TPU-edt using the Emacs Initialization File
  131. ;; The following is a sample Emacs initialization file. It shows how to
  132. ;; invoke TPU-edt, and how to customize it.
  133. ;; ; .emacs - a sample Emacs initialization file
  134. ;; ; Turn on TPU-edt
  135. ;; (tpu-edt)
  136. ;; ; Set scroll margins 10% (top) and 15% (bottom).
  137. ;; (tpu-set-scroll-margins "10%" "15%")
  138. ;; ; Load the vtxxx terminal control functions.
  139. ;; (load "vt-control" t)
  140. ;; ; TPU-edt treats words like EDT; here's how to add word separators.
  141. ;; ; Note that backslash (\) and double quote (") are quoted with '\'.
  142. ;; (tpu-add-word-separators "]\\[-_,.\"=+()'/*#:!&;$")
  143. ;; ; Emacs is happy to save files without a final newline; other Unix
  144. ;; ; programs hate that! Here we make sure that files end with newlines.
  145. ;; (setq require-final-newline t)
  146. ;; ; Emacs uses Control-s and Control-q. Problems can occur when using
  147. ;; ; Emacs on terminals that use these codes for flow control (Xon/Xoff
  148. ;; ; flow control). These lines disable Emacs's use of these characters.
  149. ;; (global-unset-key "\C-s")
  150. ;; (global-unset-key "\C-q")
  151. ;; ; The Emacs universal-argument function is very useful.
  152. ;; ; This line maps universal-argument to Gold-PF1.
  153. ;; (define-key tpu-gold-map [kp_f1] 'universal-argument) ; Gold-PF1
  154. ;; ; Make KP7 move by paragraphs, instead of pages.
  155. ;; (define-key tpu-global-map [kf_7] 'tpu-paragraph) ; KP7
  156. ;; ; Repeat the preceding mappings for X-windows.
  157. ;; (cond
  158. ;; (window-system
  159. ;; (define-key tpu-global-map [kp_7] 'tpu-paragraph) ; KP7
  160. ;; (define-key tpu-gold-map [kp_f1] 'universal-argument))) ; GOLD-PF1
  161. ;; ; Display the TPU-edt version.
  162. ;; (tpu-version)
  163. ;; %% Regular Expressions in TPU-edt
  164. ;; Gold-* toggles TPU-edt regular expression mode. In regular expression
  165. ;; mode, find, find next, replace, and substitute accept Emacs regular
  166. ;; expressions. A complete list of Emacs regular expressions can be found
  167. ;; using the Emacs "info" command (it's somewhat like the VMS help
  168. ;; command). Try the following sequence of commands:
  169. ;; DO info <enter info mode>
  170. ;; m emacs <select the "emacs" topic>
  171. ;; m regexs <select the "regular expression" topic>
  172. ;; Type "q" to quit out of info mode.
  173. ;; There is a problem in regular expression mode when searching for empty
  174. ;; strings, like beginning-of-line (^) and end-of-line ($). When searching
  175. ;; for these strings, find-next may find the current string, instead of the
  176. ;; next one. This can cause global replace and substitute commands to loop
  177. ;; forever in the same location. For this reason, commands like
  178. ;; replace "^" "> " <add "> " to beginning of line>
  179. ;; replace "$" "00711" <add "00711" to end of line>
  180. ;; may not work properly.
  181. ;; Commands like those above are very useful for adding text to the
  182. ;; beginning or end of lines. They might work on a line-by-line basis, but
  183. ;; go into an infinite loop if the "all" response is specified. If the
  184. ;; goal is to add a string to the beginning or end of a particular set of
  185. ;; lines TPU-edt provides functions to do this.
  186. ;; Gold-^ Add a string at BOL in region or buffer
  187. ;; Gold-$ Add a string at EOL in region or buffer
  188. ;; There is also a TPU-edt interface to the native Emacs string replacement
  189. ;; commands. Gold-/ invokes this command. It accepts regular expressions
  190. ;; if TPU-edt is in regular expression mode. Given a repeat count, it will
  191. ;; perform the replacement without prompting for confirmation.
  192. ;; This command replaces empty strings correctly, however, it has its
  193. ;; drawbacks. As a native Emacs command, it has a different interface
  194. ;; than the emulated TPU commands. Also, it works only in the forward
  195. ;; direction, regardless of the current TPU-edt direction.
  196. ;;; Todo/Bugs:
  197. ;; We shouldn't use vt100 ESC sequences since it is uselessly fighting
  198. ;; against function-key-map. Better use real key names.
  199. ;;; Code:
  200. ;; we use picture-mode functions
  201. (require 'picture)
  202. (defgroup tpu nil
  203. "Emacs emulating TPU emulating EDT."
  204. :prefix "tpu-"
  205. :group 'emulations)
  206. ;;;
  207. ;;; Version Information
  208. ;;;
  209. (defconst tpu-version "4.5" "TPU-edt version number.")
  210. ;;;
  211. ;;; User Configurable Variables
  212. ;;;
  213. (defcustom tpu-have-ispell t
  214. "Non-nil means `tpu-spell-check' uses `ispell-region' for spell checking.
  215. Otherwise, use `spell-region'."
  216. :type 'boolean
  217. :group 'tpu)
  218. (make-obsolete-variable 'tpu-have-ispell "the `spell' package is obsolete."
  219. "23.1")
  220. (defcustom tpu-kill-buffers-silently nil
  221. "If non-nil, TPU-edt kills modified buffers without asking."
  222. :type 'boolean
  223. :group 'tpu)
  224. (defcustom tpu-percent-scroll 75
  225. "Percentage of the screen to scroll for next/previous screen commands."
  226. :type 'integer
  227. :group 'tpu)
  228. (defcustom tpu-pan-columns 16
  229. "Number of columns the tpu-pan functions scroll left or right."
  230. :type 'integer
  231. :group 'tpu)
  232. ;;;
  233. ;;; Global Keymaps
  234. ;;;
  235. (defvar tpu-gold-map
  236. (let ((map (make-keymap)))
  237. ;; Previously we used escape sequences here. We now instead presume
  238. ;; that term/*.el does its job to map the escape sequence to the right
  239. ;; key-symbol.
  240. (define-key map [up] 'tpu-move-to-beginning) ; up-arrow
  241. (define-key map [down] 'tpu-move-to-end) ; down-arrow
  242. (define-key map [right] 'end-of-line) ; right-arrow
  243. (define-key map [left] 'beginning-of-line) ; left-arrow
  244. ;; (define-key map [find] nil) ; Find
  245. ;; (define-key map [insert] nil) ; Insert Here
  246. (define-key map [delete] 'tpu-store-text) ; Remove
  247. (define-key map [select] 'tpu-unselect) ; Select
  248. (define-key map [prior] 'tpu-previous-window) ; Prev Screen
  249. (define-key map [next] 'tpu-next-window) ; Next Screen
  250. ;; (define-key map [f1] nil) ; F1
  251. ;; (define-key map [f2] nil) ; F2
  252. ;; (define-key map [f3] nil) ; F3
  253. ;; (define-key map [f4] nil) ; F4
  254. ;; (define-key map [f5] nil) ; F5
  255. ;; (define-key map [f6] nil) ; F6
  256. ;; (define-key map [f7] nil) ; F7
  257. ;; (define-key map [f8] nil) ; F8
  258. ;; (define-key map [f9] nil) ; F9
  259. ;; (define-key map [f10] nil) ; F10
  260. ;; (define-key map [f11] nil) ; F11
  261. ;; (define-key map [f12] nil) ; F12
  262. ;; (define-key map [f13] nil) ; F13
  263. ;; (define-key map [f14] nil) ; F14
  264. (define-key map [help] 'describe-bindings) ; HELP
  265. ;; (define-key map [menu] nil) ; DO
  266. (define-key map [f17] 'tpu-drop-breadcrumb) ; F17
  267. ;; (define-key map [f18] nil) ; F18
  268. ;; (define-key map [f19] nil) ; F19
  269. ;; (define-key map [f20] nil) ; F20
  270. (define-key map [kp-f1] 'keyboard-quit) ; PF1
  271. (define-key map [kp-f2] 'help-for-help) ; PF2
  272. (define-key map [kp-f3] 'tpu-search) ; PF3
  273. (define-key map [kp-f4] 'tpu-undelete-lines) ; PF4
  274. (define-key map [kp-0] 'open-line) ; KP0
  275. (define-key map [kp-1] 'tpu-change-case) ; KP1
  276. (define-key map [kp-2] 'tpu-delete-to-eol) ; KP2
  277. (define-key map [kp-3] 'tpu-special-insert) ; KP3
  278. (define-key map [kp-4] 'tpu-move-to-end) ; KP4
  279. (define-key map [kp-5] 'tpu-move-to-beginning) ; KP5
  280. (define-key map [kp-6] 'tpu-paste) ; KP6
  281. (define-key map [kp-7] 'execute-extended-command) ; KP7
  282. (define-key map [kp-8] 'tpu-fill) ; KP8
  283. (define-key map [kp-9] 'tpu-replace) ; KP9
  284. (define-key map [kp-subtract] 'tpu-undelete-words) ; KP-
  285. (define-key map [kp-separator] 'tpu-undelete-char) ; KP,
  286. (define-key map [kp-decimal] 'tpu-unselect) ; KP.
  287. (define-key map [kp-enter] 'tpu-substitute) ; KPenter
  288. ;;
  289. (define-key map "\C-A" 'tpu-toggle-overwrite-mode) ; ^A
  290. ;; (define-key map "\C-B" nil) ; ^B
  291. ;; (define-key map "\C-C" nil) ; ^C
  292. ;; (define-key map "\C-D" nil) ; ^D
  293. ;; (define-key map "\C-E" nil) ; ^E
  294. (define-key map "\C-F" 'set-visited-file-name) ; ^F
  295. (define-key map "\C-g" 'keyboard-quit) ; safety first
  296. (define-key map "\C-h" 'delete-other-windows) ; BS
  297. (define-key map "\C-i" 'other-window) ; TAB
  298. ;; (define-key map "\C-J" nil) ; ^J
  299. (define-key map "\C-K" 'tpu-define-macro-key) ; ^K
  300. (define-key map "\C-l" 'downcase-region) ; ^L
  301. ;; (define-key map "\C-M" nil) ; ^M
  302. ;; (define-key map "\C-N" nil) ; ^N
  303. ;; (define-key map "\C-O" nil) ; ^O
  304. ;; (define-key map "\C-P" nil) ; ^P
  305. ;; (define-key map "\C-Q" nil) ; ^Q
  306. ;; (define-key map "\C-R" nil) ; ^R
  307. ;; (define-key map "\C-S" nil) ; ^S
  308. (define-key map "\C-T" 'tpu-toggle-control-keys) ; ^T
  309. (define-key map "\C-u" 'upcase-region) ; ^U
  310. ;; (define-key map "\C-V" nil) ; ^V
  311. (define-key map "\C-w" 'tpu-write-current-buffers) ; ^W
  312. ;; (define-key map "\C-X" nil) ; ^X
  313. ;; (define-key map "\C-Y" nil) ; ^Y
  314. ;; (define-key map "\C-Z" nil) ; ^Z
  315. (define-key map " " 'undo) ; SPC
  316. ;; (define-key map "!" nil) ; !
  317. ;; (define-key map "#" nil) ; #
  318. (define-key map "$" 'tpu-add-at-eol) ; $
  319. (define-key map "%" 'tpu-goto-percent) ; %
  320. ;; (define-key map "&" nil) ; &
  321. ;; (define-key map "(" nil) ; (
  322. ;; (define-key map ")" nil) ; )
  323. (define-key map "*" 'tpu-toggle-regexp) ; *
  324. ;; (define-key map "+" nil) ; +
  325. (define-key map "," 'tpu-goto-breadcrumb) ; ,
  326. (define-key map "-" 'negative-argument) ; -
  327. (define-key map "." 'tpu-drop-breadcrumb) ; .
  328. (define-key map "/" 'tpu-emacs-replace) ; /
  329. (define-key map "0" 'digit-argument) ; 0
  330. (define-key map "1" 'digit-argument) ; 1
  331. (define-key map "2" 'digit-argument) ; 2
  332. (define-key map "3" 'digit-argument) ; 3
  333. (define-key map "4" 'digit-argument) ; 4
  334. (define-key map "5" 'digit-argument) ; 5
  335. (define-key map "6" 'digit-argument) ; 6
  336. (define-key map "7" 'digit-argument) ; 7
  337. (define-key map "8" 'digit-argument) ; 8
  338. (define-key map "9" 'digit-argument) ; 9
  339. ;; (define-key map ":" nil) ; :
  340. (define-key map ";" 'tpu-trim-line-ends) ; ;
  341. ;; (define-key map "<" nil) ; <
  342. ;; (define-key map "=" nil) ; =
  343. ;; (define-key map ">" nil) ; >
  344. (define-key map "?" 'tpu-spell-check) ; ?
  345. ;; (define-key map "A" 'tpu-toggle-newline-and-indent) ; A
  346. ;; (define-key map "B" 'tpu-next-buffer) ; B
  347. ;; (define-key map "C" 'repeat-complex-command) ; C
  348. ;; (define-key map "D" 'shell-command) ; D
  349. ;; (define-key map "E" 'tpu-exit) ; E
  350. ;; (define-key map "F" 'tpu-cursor-free-mode) ; F
  351. ;; (define-key map "G" 'tpu-get) ; G
  352. ;; (define-key map "H" nil) ; H
  353. ;; (define-key map "I" 'tpu-include) ; I
  354. ;; (define-key map "K" 'tpu-kill-buffer) ; K
  355. (define-key map "L" 'tpu-what-line) ; L
  356. ;; (define-key map "M" 'buffer-menu) ; M
  357. ;; (define-key map "N" 'tpu-next-file-buffer) ; N
  358. ;; (define-key map "O" 'occur) ; O
  359. (define-key map "P" 'lpr-buffer) ; P
  360. ;; (define-key map "Q" 'tpu-quit) ; Q
  361. ;; (define-key map "R" 'tpu-toggle-rectangle) ; R
  362. ;; (define-key map "S" 'replace) ; S
  363. ;; (define-key map "T" 'tpu-line-to-top-of-window) ; T
  364. ;; (define-key map "U" 'undo) ; U
  365. ;; (define-key map "V" 'tpu-version) ; V
  366. ;; (define-key map "W" 'save-buffer) ; W
  367. ;; (define-key map "X" 'tpu-save-all-buffers-kill-emacs) ; X
  368. ;; (define-key map "Y" 'copy-region-as-kill) ; Y
  369. ;; (define-key map "Z" 'suspend-emacs) ; Z
  370. (define-key map "[" 'blink-matching-open) ; [
  371. ;; (define-key map "\\" nil) ; \
  372. (define-key map "]" 'blink-matching-open) ; ]
  373. (define-key map "^" 'tpu-add-at-bol) ; ^
  374. (define-key map "_" 'split-window-below) ; -
  375. (define-key map "`" 'what-line) ; `
  376. (define-key map "a" 'tpu-toggle-newline-and-indent) ; a
  377. (define-key map "b" 'tpu-next-buffer) ; b
  378. (define-key map "c" 'repeat-complex-command) ; c
  379. (define-key map "d" 'shell-command) ; d
  380. (define-key map "e" 'tpu-exit) ; e
  381. (define-key map "f" 'tpu-cursor-free-mode) ; f
  382. (define-key map "g" 'tpu-get) ; g
  383. ;; (define-key map "h" nil) ; h
  384. (define-key map "i" 'tpu-include) ; i
  385. (define-key map "k" 'tpu-kill-buffer) ; k
  386. (define-key map "l" 'goto-line) ; l
  387. (define-key map "m" 'buffer-menu) ; m
  388. (define-key map "n" 'tpu-next-file-buffer) ; n
  389. (define-key map "o" 'occur) ; o
  390. (define-key map "p" 'lpr-region) ; p
  391. (define-key map "q" 'tpu-quit) ; q
  392. (define-key map "r" 'tpu-toggle-rectangle) ; r
  393. (define-key map "s" 'replace) ; s
  394. (define-key map "t" 'tpu-line-to-top-of-window) ; t
  395. (define-key map "u" 'undo) ; u
  396. (define-key map "v" 'tpu-version) ; v
  397. (define-key map "w" 'save-buffer) ; w
  398. (define-key map "x" 'tpu-save-all-buffers-kill-emacs) ; x
  399. (define-key map "y" 'copy-region-as-kill) ; y
  400. (define-key map "z" 'suspend-emacs) ; z
  401. ;; (define-key map "{" nil) ; {
  402. (define-key map "|" 'split-window-right) ; |
  403. ;; (define-key map "}" nil) ; }
  404. (define-key map "~" 'exchange-point-and-mark) ; ~
  405. (define-key map "\177" 'delete-window) ; <X]
  406. map)
  407. "Maps the function keys on the VT100 keyboard preceded by PF1.
  408. GOLD is the ASCII 7-bit escape sequence <ESC>OP.")
  409. (define-obsolete-variable-alias 'GOLD-map 'tpu-gold-map "23.1")
  410. (defvar tpu-global-map
  411. (let ((map (make-sparse-keymap)))
  412. ;; Previously defined in CSI-map. We now presume that term/*.el does
  413. ;; its job to map the escape sequence to the right key-symbol.
  414. (define-key map [find] 'tpu-search) ; Find
  415. (define-key map [insert] 'tpu-paste) ; Insert Here
  416. (define-key map [delete] 'tpu-cut) ; Remove
  417. (define-key map [select] 'tpu-select) ; Select
  418. (define-key map [prior] 'tpu-scroll-window-down) ; Prev Screen
  419. (define-key map [next] 'tpu-scroll-window-up) ; Next Screen
  420. ;; (define-key map [f1] nil) ; F1
  421. ;; (define-key map [f2] nil) ; F2
  422. ;; (define-key map [f3] nil) ; F3
  423. ;; (define-key map [f4] nil) ; F4
  424. ;; (define-key map [f5] nil) ; F5
  425. ;; (define-key map [f6] nil) ; F6
  426. ;; (define-key map [f7] nil) ; F7
  427. ;; (define-key map [f8] nil) ; F8
  428. ;; (define-key map [f9] nil) ; F9
  429. (define-key map [f10] 'tpu-exit) ; F10
  430. (define-key map [f11] 'tpu-insert-escape) ; F11 (ESC)
  431. (define-key map [f12] 'tpu-next-beginning-of-line) ; F12 (BS)
  432. (define-key map [f13] 'tpu-delete-previous-word) ; F13 (LF)
  433. (define-key map [f14] 'tpu-toggle-overwrite-mode) ; F14
  434. (define-key map [help] 'tpu-help) ; HELP
  435. (define-key map [menu] 'execute-extended-command) ; DO
  436. (define-key map [f17] 'tpu-goto-breadcrumb) ; F17
  437. ;; (define-key map [f18] nil) ; F18
  438. ;; (define-key map [f19] nil) ; F19
  439. ;; (define-key map [f20] nil) ; F20
  440. ;; Previously defined in SS3-map. We now presume that term/*.el does
  441. ;; its job to map the escape sequence to the right key-symbol.
  442. (define-key map [kp-f1] tpu-gold-map) ; GOLD map
  443. ;;
  444. (define-key map [up] 'tpu-previous-line) ; up
  445. (define-key map [down] 'tpu-next-line) ; down
  446. (define-key map [right] 'tpu-forward-char) ; right
  447. (define-key map [left] 'tpu-backward-char) ; left
  448. (define-key map [kp-f2] 'tpu-help) ; PF2
  449. (define-key map [kp-f3] 'tpu-search-again) ; PF3
  450. (define-key map [kp-f4] 'tpu-delete-current-line) ; PF4
  451. (define-key map [kp-0] 'tpu-line) ; KP0
  452. (define-key map [kp-1] 'tpu-word) ; KP1
  453. (define-key map [kp-2] 'tpu-end-of-line) ; KP2
  454. (define-key map [kp-3] 'tpu-char) ; KP3
  455. (define-key map [kp-4] 'tpu-advance-direction) ; KP4
  456. (define-key map [kp-5] 'tpu-backup-direction) ; KP5
  457. (define-key map [kp-6] 'tpu-cut) ; KP6
  458. (define-key map [kp-7] 'tpu-page) ; KP7
  459. (define-key map [kp-8] 'tpu-scroll-window) ; KP8
  460. (define-key map [kp-9] 'tpu-append-region) ; KP9
  461. (define-key map [kp-subtract] 'tpu-delete-current-word) ; KP-
  462. (define-key map [kp-separator] 'tpu-delete-current-char) ; KP,
  463. (define-key map [kp-decimal] 'tpu-select) ; KP.
  464. (define-key map [kp-enter] 'newline) ; KPenter
  465. map)
  466. "TPU-edt global keymap.")
  467. ;;;
  468. ;;; Global Variables
  469. ;;;
  470. (defvar tpu-last-replaced-text ""
  471. "Last text deleted by a TPU-edt replace command.")
  472. (defvar tpu-last-deleted-region ""
  473. "Last text deleted by a TPU-edt remove command.")
  474. (defvar tpu-last-deleted-lines ""
  475. "Last text deleted by a TPU-edt line-delete command.")
  476. (defvar tpu-last-deleted-words ""
  477. "Last text deleted by a TPU-edt word-delete command.")
  478. (defvar tpu-last-deleted-char ""
  479. "Last character deleted by a TPU-edt character-delete command.")
  480. (defvar tpu-searching-forward t
  481. "If non-nil, TPU-edt is searching in the forward direction.")
  482. (defvar tpu-search-last-string ""
  483. "Last text searched for by the TPU-edt search commands.")
  484. (defvar tpu-search-overlay (make-overlay 1 1)
  485. "Search highlight overlay.")
  486. (overlay-put tpu-search-overlay 'face 'bold)
  487. (defvar tpu-replace-overlay (make-overlay 1 1)
  488. "Replace highlight overlay.")
  489. (overlay-put tpu-replace-overlay 'face 'highlight)
  490. (defvar tpu-regexp-p nil
  491. "If non-nil, TPU-edt uses regexp search and replace routines.")
  492. (defvar tpu-rectangular-p nil
  493. "If non-nil, TPU-edt removes and inserts rectangles.")
  494. (defvar tpu-advance t
  495. "True when TPU-edt is operating in the forward direction.")
  496. (defvar tpu-reverse nil
  497. "True when TPU-edt is operating in the backward direction.")
  498. (defvar tpu-control-keys nil
  499. "If non-nil, control keys are set to perform TPU functions.")
  500. (defvar tpu-xkeys-file nil
  501. "File containing TPU-edt X key map.")
  502. (defvar tpu-rectangle-string nil
  503. "Mode line string to identify rectangular mode.")
  504. (defvar tpu-direction-string nil
  505. "Mode line string to identify current direction.")
  506. (defvar tpu-add-at-bol-hist nil
  507. "History variable for tpu-edt-add-at-bol function.")
  508. (defvar tpu-add-at-eol-hist nil
  509. "History variable for tpu-edt-add-at-eol function.")
  510. (defvar tpu-regexp-prompt-hist nil
  511. "History variable for search and replace functions.")
  512. ;;;
  513. ;;; Buffer Local Variables
  514. ;;;
  515. (defvar tpu-newline-and-indent-p nil
  516. "If non-nil, Return produces a newline and indents.")
  517. (make-variable-buffer-local 'tpu-newline-and-indent-p)
  518. (defvar tpu-newline-and-indent-string nil
  519. "Mode line string to identify AutoIndent mode.")
  520. (make-variable-buffer-local 'tpu-newline-and-indent-string)
  521. (defvar tpu-saved-delete-func nil
  522. "Saved value of the delete key.")
  523. (make-variable-buffer-local 'tpu-saved-delete-func)
  524. (defvar tpu-buffer-local-map nil
  525. "TPU-edt buffer local key map.")
  526. (make-variable-buffer-local 'tpu-buffer-local-map)
  527. ;;;
  528. ;;; Mode Line - Modify the mode line to show the following
  529. ;;;
  530. ;;; o Mark state.
  531. ;;; o Direction of motion.
  532. ;;; o Active rectangle mode.
  533. ;;; o Active auto indent mode.
  534. ;;;
  535. (defvar tpu-original-mm-alist minor-mode-alist)
  536. (defvar tpu-mark-flag "")
  537. (make-variable-buffer-local 'tpu-mark-flag)
  538. (defun tpu-set-mode-line (for-tpu)
  539. "Set ``minor-mode-alist'' for TPU-edt, or reset it to default Emacs."
  540. (let ((entries '((tpu-newline-and-indent-p tpu-newline-and-indent-string)
  541. (tpu-rectangular-p tpu-rectangle-string)
  542. (tpu-direction-string tpu-direction-string)
  543. (tpu-mark-flag tpu-mark-flag))))
  544. (dolist (entry entries)
  545. (if for-tpu
  546. (add-to-list 'minor-mode-alist entry)
  547. (setq minor-mode-alist (remove entry minor-mode-alist))))))
  548. (defun tpu-update-mode-line nil
  549. "Make sure mode-line in the current buffer reflects all changes."
  550. (setq tpu-mark-flag (if transient-mark-mode "" (if (tpu-mark) " @" " ")))
  551. (force-mode-line-update))
  552. (cond ((featurep 'xemacs)
  553. (add-hook 'zmacs-deactivate-region-hook 'tpu-update-mode-line)
  554. (add-hook 'zmacs-activate-region-hook 'tpu-update-mode-line))
  555. (t
  556. (add-hook 'activate-mark-hook 'tpu-update-mode-line)
  557. (add-hook 'deactivate-mark-hook 'tpu-update-mode-line)))
  558. ;;;
  559. ;;; Match Markers -
  560. ;;;
  561. ;;; Set in: Search
  562. ;;;
  563. ;;; Used in: Replace, Substitute, Store-Text, Cut/Remove,
  564. ;;; Append, and Change-Case
  565. ;;;
  566. (defvar tpu-match-beginning-mark (make-marker))
  567. (defvar tpu-match-end-mark (make-marker))
  568. (defun tpu-set-match nil
  569. "Set markers at match beginning and end."
  570. ;; Add one to beginning mark so it stays with the first character of
  571. ;; the string even if characters are added just before the string.
  572. (setq tpu-match-beginning-mark (copy-marker (match-beginning 0) t))
  573. (setq tpu-match-end-mark (copy-marker (match-end 0))))
  574. (defun tpu-unset-match nil
  575. "Unset match beginning and end markers."
  576. (set-marker tpu-match-beginning-mark nil)
  577. (set-marker tpu-match-end-mark nil))
  578. (defun tpu-match-beginning nil
  579. "Return the location of the last match beginning."
  580. (marker-position tpu-match-beginning-mark))
  581. (defun tpu-match-end nil
  582. "Return the location of the last match end."
  583. (marker-position tpu-match-end-mark))
  584. (defun tpu-check-match nil
  585. "Return t if point is between tpu-match markers.
  586. Otherwise sets the tpu-match markers to nil and returns nil."
  587. ;; make sure 1- marker is in this buffer
  588. ;; 2- point is at or after beginning marker
  589. ;; 3- point is before ending marker, or in the case of
  590. ;; zero length regions (like bol, or eol) that the
  591. ;; beginning, end, and point are equal.
  592. (cond ((and
  593. (equal (marker-buffer tpu-match-beginning-mark) (current-buffer))
  594. (>= (point) (marker-position tpu-match-beginning-mark))
  595. (or
  596. (< (point) (marker-position tpu-match-end-mark))
  597. (and (= (marker-position tpu-match-beginning-mark)
  598. (marker-position tpu-match-end-mark))
  599. (= (marker-position tpu-match-end-mark) (point))))) t)
  600. (t
  601. (tpu-unset-match) nil)))
  602. (defun tpu-show-match-markers nil
  603. "Show the values of the match markers."
  604. (interactive)
  605. (if (markerp tpu-match-beginning-mark)
  606. (message "(%s, %s) in %s -- current %s in %s"
  607. (marker-position tpu-match-beginning-mark)
  608. (marker-position tpu-match-end-mark)
  609. (marker-buffer tpu-match-end-mark)
  610. (point) (current-buffer))))
  611. ;;;
  612. ;;; Utilities
  613. ;;;
  614. (defun tpu-mark nil
  615. "TPU-edt version of the mark function.
  616. Return the appropriate value of the mark for the current
  617. version of Emacs."
  618. (cond ((featurep 'xemacs) (mark (not zmacs-regions)))
  619. (t (and mark-active (mark (not transient-mark-mode))))))
  620. (defun tpu-set-mark (pos)
  621. "TPU-edt version of the `set-mark' function.
  622. Sets the mark at POS and activates the region according to the
  623. current version of Emacs."
  624. (set-mark pos)
  625. (when (featurep 'xemacs) (when pos (zmacs-activate-region))))
  626. (defun tpu-string-prompt (prompt history-symbol)
  627. "Read a string with PROMPT."
  628. (read-from-minibuffer prompt nil nil nil history-symbol))
  629. (defvar tpu-last-answer nil "Most recent response to tpu-y-or-n-p.")
  630. (defun tpu-y-or-n-p (prompt &optional not-yes)
  631. "Prompt for a y or n answer with positive default.
  632. Optional second argument NOT-YES changes default to negative.
  633. Like Emacs `y-or-n-p', but also accepts space as y and DEL as n."
  634. (message "%s[%s]" prompt (if not-yes "n" "y"))
  635. (let ((doit t))
  636. (while doit
  637. (setq doit nil)
  638. (let ((ans (read-char)))
  639. (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\ ))
  640. (setq tpu-last-answer t))
  641. ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
  642. (setq tpu-last-answer nil))
  643. ((= ans ?\r) (setq tpu-last-answer (not not-yes)))
  644. (t
  645. (setq doit t) (beep)
  646. (message "Please answer y or n. %s[%s]"
  647. prompt (if not-yes "n" "y")))))))
  648. tpu-last-answer)
  649. (defun tpu-local-set-key (key func)
  650. "Replace a key in the TPU-edt local key map.
  651. Create the key map if necessary."
  652. (cond ((not (keymapp tpu-buffer-local-map))
  653. (setq tpu-buffer-local-map (if (current-local-map)
  654. (copy-keymap (current-local-map))
  655. (make-sparse-keymap)))
  656. (use-local-map tpu-buffer-local-map)))
  657. (local-set-key key func))
  658. (defun tpu-current-line ()
  659. "Return the vertical position of point in the selected window.
  660. Top line is 0. Counts each text line only once, even if it wraps."
  661. (or
  662. (cdr (nth 6 (posn-at-point)))
  663. (if (eq (window-start) (point)) 0
  664. (1- (count-screen-lines (window-start) (point) 'count-final-newline)))))
  665. ;;;
  666. ;;; Breadcrumbs
  667. ;;;
  668. (defvar tpu-breadcrumb-plist nil
  669. "The set of user-defined markers (breadcrumbs), as a plist.")
  670. (defun tpu-drop-breadcrumb (num)
  671. "Drops a breadcrumb that can be returned to later with goto-breadcrumb."
  672. (interactive "p")
  673. (put tpu-breadcrumb-plist num (list (current-buffer) (point)))
  674. (message "Mark %d set." num))
  675. (defun tpu-goto-breadcrumb (num)
  676. "Return to a breadcrumb set with drop-breadcrumb."
  677. (interactive "p")
  678. (cond ((get tpu-breadcrumb-plist num)
  679. (switch-to-buffer (car (get tpu-breadcrumb-plist num)))
  680. (goto-char (cadr (get tpu-breadcrumb-plist num)))
  681. (message "mark %d found." num))
  682. (t
  683. (message "mark %d not found." num))))
  684. ;;;
  685. ;;; Miscellaneous
  686. ;;;
  687. (defun tpu-change-case (num)
  688. "Change the case of the character under the cursor or region.
  689. Accepts a prefix argument of the number of characters to invert."
  690. (interactive "p")
  691. (cond ((tpu-mark)
  692. (let ((beg (region-beginning)) (end (region-end)))
  693. (while (> end beg)
  694. (funcall (if (= (downcase (char-after beg)) (char-after beg))
  695. 'upcase-region 'downcase-region)
  696. beg (1+ beg))
  697. (setq beg (1+ beg)))
  698. (tpu-unselect t)))
  699. ((tpu-check-match)
  700. (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  701. (while (> end beg)
  702. (funcall (if (= (downcase (char-after beg)) (char-after beg))
  703. 'upcase-region 'downcase-region)
  704. beg (1+ beg))
  705. (setq beg (1+ beg)))
  706. (tpu-unset-match)))
  707. (t
  708. (while (> num 0)
  709. (funcall (if (= (downcase (following-char)) (following-char))
  710. 'upcase-region 'downcase-region)
  711. (point) (1+ (point)))
  712. (forward-char (if tpu-reverse -1 1))
  713. (setq num (1- num))))))
  714. (defun tpu-fill (num)
  715. "Fill paragraph or marked region.
  716. With argument, fill and justify."
  717. (interactive "P")
  718. (cond ((tpu-mark)
  719. (fill-region (point) (tpu-mark) num)
  720. (tpu-unselect t))
  721. (t
  722. (fill-paragraph num))))
  723. (defun tpu-version nil
  724. "Print the TPU-edt version number."
  725. (interactive)
  726. (message
  727. "TPU-edt version %s by Rob Riepel (riepel@networking.stanford.edu)"
  728. tpu-version))
  729. (defun tpu-reset-screen-size (height width)
  730. "Set the screen size."
  731. (interactive "nnew screen height: \nnnew screen width: ")
  732. (set-frame-height (selected-frame) height)
  733. (set-frame-width (selected-frame) width))
  734. (defun tpu-toggle-newline-and-indent nil
  735. "Toggle between 'newline and indent' and 'simple newline'."
  736. (interactive)
  737. (cond (tpu-newline-and-indent-p
  738. (setq tpu-newline-and-indent-string "")
  739. (setq tpu-newline-and-indent-p nil)
  740. (tpu-local-set-key "\C-m" 'newline))
  741. (t
  742. (setq tpu-newline-and-indent-string " AutoIndent")
  743. (setq tpu-newline-and-indent-p t)
  744. (tpu-local-set-key "\C-m" 'newline-and-indent)))
  745. (tpu-update-mode-line)
  746. (and (called-interactively-p 'interactive)
  747. (message "Carriage return inserts a newline%s"
  748. (if tpu-newline-and-indent-p " and indents." "."))))
  749. (defun tpu-spell-check nil
  750. "Check the spelling of the region, or of the entire buffer,
  751. if no region is selected."
  752. (interactive)
  753. (let ((m (tpu-mark)))
  754. (apply (if tpu-have-ispell 'ispell-region
  755. 'spell-region)
  756. (if m
  757. (if (> m (point)) (list (point) m)
  758. (list m (point)))
  759. (list (point-min) (point-max))))
  760. (if m (tpu-unselect t))))
  761. (defun tpu-toggle-overwrite-mode nil
  762. "Switch in and out of overwrite mode."
  763. (interactive)
  764. (cond (overwrite-mode
  765. (tpu-local-set-key "\177" tpu-saved-delete-func)
  766. (overwrite-mode 0))
  767. (t
  768. (setq tpu-saved-delete-func (local-key-binding "\177"))
  769. (tpu-local-set-key "\177" 'picture-backward-clear-column)
  770. (overwrite-mode 1))))
  771. (defun tpu-special-insert (num)
  772. "Insert a character or control code according to its ASCII decimal value."
  773. (interactive "P")
  774. (if overwrite-mode (delete-char 1))
  775. (insert (or num 0)))
  776. (defun tpu-quoted-insert (num)
  777. "Read next input character and insert it.
  778. This is useful for inserting control characters."
  779. (interactive "*p")
  780. (let ((char (read-char)) )
  781. (if overwrite-mode (delete-char num))
  782. (insert-char char num)))
  783. ;;;
  784. ;;; TPU line-mode commands
  785. ;;;
  786. (defun tpu-include (file)
  787. "TPU-like include file."
  788. (interactive "fInclude file: ")
  789. (insert-file-contents file)
  790. (message ""))
  791. (defun tpu-get (file)
  792. "TPU-like get file."
  793. (interactive "FFile to get: ")
  794. (find-file file find-file-wildcards))
  795. (defun tpu-what-line nil
  796. "Tell what line the point is on,
  797. and the total number of lines in the buffer."
  798. (interactive)
  799. (if (eobp)
  800. (message "You are at the End of Buffer. The last line is %d."
  801. (count-lines 1 (point-max)))
  802. (let* ((cur (count-lines 1 (1+ (point))))
  803. (max (count-lines 1 (point-max)))
  804. (pct (/ (* 100 (+ cur (/ max 200))) max)))
  805. (message "You are on line %d out of %d (%d%%)." cur max pct))))
  806. (defun tpu-exit nil
  807. "Exit the way TPU does, save current buffer and ask about others."
  808. (interactive)
  809. (if (not (eq (recursion-depth) 0))
  810. (exit-recursive-edit)
  811. (progn (save-buffer) (save-buffers-kill-emacs))))
  812. (defun tpu-quit nil
  813. "Quit the way TPU does, ask to make sure changes should be abandoned."
  814. (interactive)
  815. (let ((list (buffer-list))
  816. (working t))
  817. (while (and list working)
  818. (let ((buffer (car list)))
  819. (if (and (buffer-file-name buffer) (buffer-modified-p buffer))
  820. (if (tpu-y-or-n-p
  821. "Modifications will not be saved, continue quitting? ")
  822. (kill-emacs t) (setq working nil)))
  823. (setq list (cdr list))))
  824. (if working (kill-emacs t))))
  825. ;;;
  826. ;;; Command and Function Aliases
  827. ;;;
  828. ;;;###autoload
  829. (define-minor-mode tpu-edt-mode
  830. "Toggle TPU/edt emulation on or off.
  831. With a prefix argument ARG, enable the mode if ARG is positive,
  832. and disable it otherwise. If called from Lisp, enable the mode
  833. if ARG is omitted or nil."
  834. :global t
  835. (if tpu-edt-mode (tpu-edt-on) (tpu-edt-off)))
  836. (defalias 'TPU-EDT-MODE 'tpu-edt-mode)
  837. ;;;###autoload
  838. (defalias 'tpu-edt 'tpu-edt-on)
  839. (defalias 'TPU-EDT 'tpu-edt-on)
  840. ;; Note: The following functions have no `tpu-' prefix. This is unavoidable.
  841. ;; The real TPU/edt editor has interactive commands with these names,
  842. ;; so tpu-edt.el users expect things like M-x exit RET and M-x help RET
  843. ;; to work. Therefore it really is necessary to define these functions,
  844. ;; even in cases where they redefine existing Emacs functions.
  845. (defalias 'exit 'tpu-exit)
  846. (defalias 'EXIT 'tpu-exit)
  847. (defalias 'Get 'tpu-get)
  848. (defalias 'GET 'tpu-get)
  849. (defalias 'include 'tpu-include)
  850. (defalias 'INCLUDE 'tpu-include)
  851. (defalias 'quit 'tpu-quit)
  852. (defalias 'QUIT 'tpu-quit)
  853. (defalias 'spell 'tpu-spell-check)
  854. (defalias 'SPELL 'tpu-spell-check)
  855. (defalias 'what\ line 'tpu-what-line)
  856. (defalias 'WHAT\ LINE 'tpu-what-line)
  857. (defalias 'replace 'tpu-lm-replace)
  858. (defalias 'REPLACE 'tpu-lm-replace)
  859. (defalias 'help 'tpu-help)
  860. (defalias 'HELP 'tpu-help)
  861. (defalias 'set\ cursor\ free 'tpu-set-cursor-free)
  862. (defalias 'SET\ CURSOR\ FREE 'tpu-set-cursor-free)
  863. (defalias 'set\ cursor\ bound 'tpu-set-cursor-bound)
  864. (defalias 'SET\ CURSOR\ BOUND 'tpu-set-cursor-bound)
  865. (defalias 'set\ scroll\ margins 'tpu-set-scroll-margins)
  866. (defalias 'SET\ SCROLL\ MARGINS 'tpu-set-scroll-margins)
  867. ;; Real TPU error messages end in periods.
  868. ;; Define this to avoid openly flouting Emacs coding standards.
  869. (defalias 'tpu-error 'error)
  870. ;;;
  871. ;;; Help
  872. ;;;
  873. (defvar tpu-help-keypad-map "\f
  874. _______________________ _______________________________
  875. | HELP | Do | | | | | |
  876. |KeyDefs| | | | | | |
  877. |_______|_______________| |_______|_______|_______|_______|
  878. _______________________ _______________________________
  879. | Find |Insert |Remove | | Gold | HELP |FndNxt | Del L |
  880. | | |Sto Tex| | key |E-Help | Find |Undel L|
  881. |_______|_______|_______| |_______|_______|_______|_______|
  882. |Select |Pre Scr|Nex Scr| | Page | Sect |Append | Del W |
  883. | Reset |Pre Win|Nex Win| | Do | Fill |Replace|Undel W|
  884. |_______|_______|_______| |_______|_______|_______|_______|
  885. |Move up| |Forward|Reverse|Remove | Del C |
  886. | Top | |Bottom | Top |Insert |Undel C|
  887. _______|_______|_______ |_______|_______|_______|_______|
  888. |Mov Lef|Mov Dow|Mov Rig| | Word | EOL | Char | |
  889. |StaOfLi|Bottom |EndOfLi| |ChngCas|Del EOL|SpecIns| Enter |
  890. |_______|_______|_______| |_______|_______|_______| |
  891. | Line |Select | Subs |
  892. | Open Line | Reset | |
  893. |_______________|_______|_______|
  894. ")
  895. (defvar tpu-help-text "
  896. \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
  897. Control Characters
  898. ^A toggle insert and overwrite
  899. ^B recall
  900. ^E end of line
  901. ^G Cancel current operation
  902. ^H beginning of line
  903. ^J delete previous word
  904. ^K learn
  905. ^L insert page break
  906. ^R remember (during learn), re-center
  907. ^U delete to beginning of line
  908. ^V quote
  909. ^W refresh
  910. ^Z exit
  911. ^X^X exchange point and mark - useful for checking region boundaries
  912. \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
  913. Gold-<key> Functions
  914. B Next Buffer - display the next buffer (all buffers)
  915. C Recall - edit and possibly repeat previous commands
  916. E Exit - save current buffer and ask about others
  917. G Get - load a file into a new edit buffer
  918. I Include - include a file in this buffer
  919. K Kill Buffer - abandon edits and delete buffer
  920. M Buffer Menu - display a list of all buffers
  921. N Next File Buffer - display next buffer containing a file
  922. O Occur - show following lines containing REGEXP
  923. Q Quit - exit without saving anything
  924. R Toggle rectangular mode for remove and insert
  925. S Search and substitute - line mode REPLACE command
  926. ^T Toggle control key bindings between TPU and Emacs
  927. U Undo - undo the last edit
  928. W Write - save current buffer
  929. X Exit - save all modified buffers and exit
  930. \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
  931. More extensive documentation on TPU-edt can be found in the `Commentary'
  932. section of tpu-edt.el. This section can be accessed through the standard
  933. Emacs help facility using the `p' option. Once you exit TPU-edt Help, one
  934. of the following key sequences is sure to get you there.
  935. ^h p if you're not yet using TPU-edt
  936. Gold-PF2 p if you're using TPU-edt
  937. Alternatively, fire up Emacs help from the command prompt, with
  938. M-x help-for-help <CR> p <CR>
  939. Where `M-x' might be any of `Gold-KP7', 'Do', or 'ESC-x'.
  940. When you successfully invoke this part of the Emacs help facility, you
  941. will see a buffer named `*Finder*' listing a number of topics. Look for
  942. tpu-edt under `emulations'.
  943. \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\f
  944. *** No more help, use P to view previous screen")
  945. (defvar tpu-help-enter (format "%s" "\eOM")) ; tpu-help enter key symbol
  946. (defvar tpu-help-return (format "%s" "\r")) ; tpu-help enter key symbol
  947. (defvar tpu-help-N "N") ; tpu-help "N" symbol
  948. (defvar tpu-help-n "n") ; tpu-help "n" symbol
  949. (defvar tpu-help-P "P") ; tpu-help "P" symbol
  950. (defvar tpu-help-p "p") ; tpu-help "p" symbol
  951. (defun tpu-help nil
  952. "Display TPU-edt help."
  953. (interactive)
  954. ;; Save current window configuration
  955. (save-window-excursion
  956. ;; Create and fill help buffer if necessary
  957. (if (not (get-buffer "*TPU-edt Help*"))
  958. (progn (generate-new-buffer "*TPU-edt Help*")
  959. (switch-to-buffer "*TPU-edt Help*")
  960. (insert tpu-help-keypad-map)
  961. (insert tpu-help-text)
  962. (setq buffer-read-only t)))
  963. ;; Display the help buffer
  964. (switch-to-buffer "*TPU-edt Help*")
  965. (delete-other-windows)
  966. (tpu-move-to-beginning)
  967. (forward-line 1)
  968. (tpu-line-to-top-of-window)
  969. ;; Prompt for keys to describe, based on screen state (split/not split)
  970. (let ((key nil) (fkey nil) (split nil))
  971. (while (not (equal tpu-help-return fkey))
  972. (if split
  973. (setq key
  974. (read-key-sequence
  975. "Press the key you want help on (RET=exit, ENTER=redisplay, N=next, P=prev): "))
  976. (setq key
  977. (read-key-sequence
  978. "Press the key you want help on (RET to exit, N next screen, P prev screen): ")))
  979. ;; Process the read key
  980. ;;
  981. ;; ENTER - Display just the help window
  982. ;; N or n - Next help or describe-key screen
  983. ;; P or p - Previous help or describe-key screen
  984. ;; RETURN - Exit from TPU-help
  985. ;; default - describe the key
  986. ;;
  987. (setq fkey (format "%s" key))
  988. (cond ((equal tpu-help-enter fkey)
  989. (setq split nil)
  990. (delete-other-windows))
  991. ((or (equal tpu-help-N fkey) (equal tpu-help-n fkey))
  992. (cond (split
  993. (condition-case nil
  994. (scroll-other-window 8)
  995. (error nil)))
  996. (t
  997. (forward-page)
  998. (forward-line 1)
  999. (tpu-line-to-top-of-window))))
  1000. ((or (equal tpu-help-P fkey) (equal tpu-help-p fkey))
  1001. (cond (split
  1002. (condition-case nil
  1003. (scroll-other-window -8)
  1004. (error nil)))
  1005. (t
  1006. (forward-line -1)
  1007. (backward-page)
  1008. (forward-line 1)
  1009. (tpu-line-to-top-of-window))))
  1010. ((not (equal tpu-help-return fkey))
  1011. (setq split t)
  1012. (describe-key key)
  1013. ;; If the key is undefined, leave the
  1014. ;; message in the mini-buffer for 3 seconds
  1015. (if (not (key-binding key)) (sit-for 3))))))))
  1016. ;;;
  1017. ;;; Auto-insert
  1018. ;;;
  1019. (defun tpu-insert-escape nil
  1020. "Insert an escape character, and so becomes the escape-key alias."
  1021. (interactive)
  1022. (insert "\e"))
  1023. (defun tpu-insert-formfeed nil
  1024. "Insert a formfeed character."
  1025. (interactive)
  1026. (insert "\C-L"))
  1027. ;;;
  1028. ;;; Define key
  1029. ;;;
  1030. (defvar tpu-saved-control-r nil "Saved value of Control-r.")
  1031. (defun tpu-end-define-macro-key (key)
  1032. "End the current macro definition."
  1033. (interactive "kPress the key you want to use to do what was just learned: ")
  1034. (end-kbd-macro nil)
  1035. (global-set-key key last-kbd-macro)
  1036. (global-set-key "\C-r" tpu-saved-control-r))
  1037. (defun tpu-define-macro-key nil
  1038. "Bind a set of keystrokes to a single key, or key combination."
  1039. (interactive)
  1040. (setq tpu-saved-control-r (global-key-binding "\C-r"))
  1041. (global-set-key "\C-r" 'tpu-end-define-macro-key)
  1042. (start-kbd-macro nil))
  1043. ;;;
  1044. ;;; Buffers and Windows
  1045. ;;;
  1046. (defun tpu-kill-buffer nil
  1047. "Kill the current buffer.
  1048. If `tpu-kill-buffers-silently' is non-nil,
  1049. kill modified buffers without asking."
  1050. (interactive)
  1051. (if tpu-kill-buffers-silently (set-buffer-modified-p nil))
  1052. (kill-buffer (current-buffer)))
  1053. (defun tpu-save-all-buffers-kill-emacs nil
  1054. "Save all buffers and exit Emacs."
  1055. (interactive)
  1056. (let ((delete-old-versions t))
  1057. (save-buffers-kill-emacs t)))
  1058. (defun tpu-write-current-buffers nil
  1059. "Save all modified buffers without exiting."
  1060. (interactive)
  1061. (save-some-buffers t))
  1062. (defun tpu-next-buffer nil
  1063. "Go to next buffer in ring."
  1064. (interactive)
  1065. (switch-to-buffer (car (reverse (buffer-list)))))
  1066. (defun tpu-next-file-buffer nil
  1067. "Go to next buffer in ring that is visiting a file or directory."
  1068. (interactive)
  1069. (let ((list (tpu-make-file-buffer-list (buffer-list))))
  1070. (setq list (delq (current-buffer) list))
  1071. (if (not list) (tpu-error "No other buffers."))
  1072. (switch-to-buffer (car (reverse list)))))
  1073. (defun tpu-make-file-buffer-list (buffer-list)
  1074. "Return names from BUFFER-LIST excluding those beginning with a space or star."
  1075. (delq nil (mapcar (lambda (b)
  1076. (if (or (= (aref (buffer-name b) 0) ?\s)
  1077. (= (aref (buffer-name b) 0) ?*)) nil b))
  1078. buffer-list)))
  1079. (defun tpu-next-window nil
  1080. "Move to the next window."
  1081. (interactive)
  1082. (if (one-window-p) (message "There is only one window on screen.")
  1083. (other-window 1)))
  1084. (defun tpu-previous-window nil
  1085. "Move to the previous window."
  1086. (interactive)
  1087. (if (one-window-p) (message "There is only one window on screen.")
  1088. (select-window (previous-window))))
  1089. ;;;
  1090. ;;; Search
  1091. ;;;
  1092. (defun tpu-toggle-regexp nil
  1093. "Switch in and out of regular expression search and replace mode."
  1094. (interactive)
  1095. (setq tpu-regexp-p (not tpu-regexp-p))
  1096. (tpu-set-search)
  1097. (and (called-interactively-p 'interactive)
  1098. (message "Regular expression search and substitute %sabled."
  1099. (if tpu-regexp-p "en" "dis"))))
  1100. (defun tpu-regexp-prompt (prompt)
  1101. "Read a string, adding 'RE' to the prompt if tpu-regexp-p is set."
  1102. (let ((re-prompt (concat (if tpu-regexp-p "RE ") prompt)))
  1103. (read-from-minibuffer re-prompt nil nil nil 'tpu-regexp-prompt-hist)))
  1104. (defun tpu-search-highlight nil
  1105. (if (tpu-check-match)
  1106. (move-overlay tpu-search-overlay
  1107. (tpu-match-beginning) (tpu-match-end) (current-buffer))
  1108. (unless (equal (overlay-start tpu-search-overlay)
  1109. (overlay-end tpu-search-overlay))
  1110. (move-overlay tpu-search-overlay 1 1 (current-buffer)))))
  1111. (defun tpu-search nil
  1112. "Search for a string or regular expression.
  1113. The search is performed in the current direction."
  1114. (interactive)
  1115. (tpu-set-search)
  1116. (tpu-search-internal ""))
  1117. (defun tpu-search-forward nil
  1118. "Search for a string or regular expression.
  1119. The search is begins in the forward direction."
  1120. (interactive)
  1121. (setq tpu-searching-forward t)
  1122. (tpu-set-search t)
  1123. (tpu-search-internal ""))
  1124. (defun tpu-search-reverse nil
  1125. "Search for a string or regular expression.
  1126. The search is begins in the reverse direction."
  1127. (interactive)
  1128. (setq tpu-searching-forward nil)
  1129. (tpu-set-search t)
  1130. (tpu-search-internal ""))
  1131. (defun tpu-search-again nil
  1132. "Search for the same string or regular expression as last time.
  1133. The search is performed in the current direction."
  1134. (interactive)
  1135. (tpu-search-internal tpu-search-last-string))
  1136. ;; tpu-set-search defines the search functions used by the TPU-edt internal
  1137. ;; search function. It should be called whenever the direction changes, or
  1138. ;; the regular expression mode is turned on or off. It can also be called
  1139. ;; to ensure that the next search will be in the current direction. It is
  1140. ;; called from:
  1141. ;; tpu-advance tpu-backup
  1142. ;; tpu-toggle-regexp tpu-toggle-search-direction (t)
  1143. ;; tpu-search tpu-lm-replace
  1144. ;; tpu-search-forward (t) tpu-search-reverse (t)
  1145. ;; tpu-search-forward-exit (t) tpu-search-backward-exit (t)
  1146. (declare-function tpu-emacs-search "tpu-edt")
  1147. (declare-function tpu-emacs-rev-search "tpu-edt")
  1148. (defun tpu-set-search (&optional arg)
  1149. "Set the search functions and set the search direction to the current direction.
  1150. If an argument is specified, don't set the search direction."
  1151. (if (not arg) (setq tpu-searching-forward tpu-advance))
  1152. (cond (tpu-searching-forward
  1153. (cond (tpu-regexp-p
  1154. (fset 'tpu-emacs-search 're-search-forward)
  1155. (fset 'tpu-emacs-rev-search 're-search-backward))
  1156. (t
  1157. (fset 'tpu-emacs-search 'search-forward)
  1158. (fset 'tpu-emacs-rev-search 'search-backward))))
  1159. (t
  1160. (cond (tpu-regexp-p
  1161. (fset 'tpu-emacs-search 're-search-backward)
  1162. (fset 'tpu-emacs-rev-search 're-search-forward))
  1163. (t
  1164. (fset 'tpu-emacs-search 'search-backward)
  1165. (fset 'tpu-emacs-rev-search 'search-forward))))))
  1166. (defun tpu-search-internal (pat &optional quiet)
  1167. "Search for a string or regular expression."
  1168. (setq tpu-search-last-string
  1169. (if (not (string= "" pat)) pat (tpu-regexp-prompt "Search: ")))
  1170. (tpu-unset-match)
  1171. (tpu-adjust-search)
  1172. (let ((case-fold-search
  1173. (and case-fold-search (tpu-check-search-case tpu-search-last-string))))
  1174. (cond ((tpu-emacs-search tpu-search-last-string nil t)
  1175. (tpu-set-match) (goto-char (tpu-match-beginning)))
  1176. (t
  1177. (tpu-adjust-search t)
  1178. (let ((found nil) (pos nil))
  1179. (save-excursion
  1180. (let ((tpu-searching-forward (not tpu-searching-forward)))
  1181. (tpu-adjust-search)
  1182. (setq found (tpu-emacs-rev-search tpu-search-last-string nil t))
  1183. (setq pos (match-beginning 0))))
  1184. (cond
  1185. (found
  1186. (cond ((tpu-y-or-n-p
  1187. (format "Found in %s direction. Go there? "
  1188. (if tpu-searching-forward "reverse" "forward")))
  1189. (goto-char pos) (tpu-set-match)
  1190. (tpu-toggle-search-direction))))
  1191. (t
  1192. (if (not quiet)
  1193. (message
  1194. "%sSearch failed: \"%s\""
  1195. (if tpu-regexp-p "RE " "") tpu-search-last-string)))))))))
  1196. (defalias 'tpu-search-internal-core (symbol-function 'tpu-search-internal))
  1197. (defun tpu-check-search-case (string)
  1198. "Return t if string contains upper case."
  1199. ;; if using regexp, eliminate upper case forms (\B \W \S.)
  1200. (if tpu-regexp-p
  1201. (let ((pat (copy-sequence string)) (case-fold-search nil) (pos 0))
  1202. (while (setq pos (string-match "\\\\\\\\" pat)) (aset pat (+ 1 pos) ?.))
  1203. (while (setq pos (string-match "\\\\B" pat)) (aset pat (+ 1 pos) ?.))
  1204. (while (setq pos (string-match "\\\\W" pat)) (aset pat (+ 1 pos) ?.))
  1205. (while (setq pos (string-match "\\\\S." pat))
  1206. (aset pat (+ 1 pos) ?.) (aset pat (+ 2 pos) ?.))
  1207. (string-equal pat (downcase pat)))
  1208. (string-equal string (downcase string))))
  1209. (defun tpu-adjust-search (&optional arg)
  1210. "For forward searches, move forward a character before searching,
  1211. and backward a character after a failed search. Arg means end of search."
  1212. (if tpu-searching-forward
  1213. (cond (arg (if (not (bobp)) (forward-char -1)))
  1214. (t (if (not (eobp)) (forward-char 1))))))
  1215. (defun tpu-toggle-search-direction nil
  1216. "Toggle the TPU-edt search direction.
  1217. Used for reversing a search in progress."
  1218. (interactive)
  1219. (setq tpu-searching-forward (not tpu-searching-forward))
  1220. (tpu-set-search t)
  1221. (and (called-interactively-p 'interactive)
  1222. (message "Searching %sward."
  1223. (if tpu-searching-forward "for" "back"))))
  1224. (defun tpu-search-forward-exit nil
  1225. "Set search direction forward and exit minibuffer."
  1226. (interactive)
  1227. (setq tpu-searching-forward t)
  1228. (tpu-set-search t)
  1229. (exit-minibuffer))
  1230. (defun tpu-search-backward-exit nil
  1231. "Set search direction backward and exit minibuffer."
  1232. (interactive)
  1233. (setq tpu-searching-forward nil)
  1234. (tpu-set-search t)
  1235. (exit-minibuffer))
  1236. ;;;
  1237. ;;; Select / Unselect
  1238. ;;;
  1239. (defun tpu-select (&optional quiet)
  1240. "Set the mark to define one end of a region."
  1241. (interactive "P")
  1242. (cond ((tpu-mark)
  1243. (tpu-unselect quiet))
  1244. (t
  1245. (tpu-set-mark (point))
  1246. (tpu-update-mode-line)
  1247. (if (not quiet) (message "Move the text cursor to select text.")))))
  1248. (defun tpu-unselect (&optional quiet)
  1249. "Remove the mark to unselect the current region."
  1250. (interactive "P")
  1251. (deactivate-mark)
  1252. (setq mark-ring nil)
  1253. (tpu-set-mark nil)
  1254. (tpu-update-mode-line)
  1255. (if (not quiet) (message "Selection canceled.")))
  1256. ;;;
  1257. ;;; Delete / Cut
  1258. ;;;
  1259. (defun tpu-toggle-rectangle nil
  1260. "Toggle rectangular mode for remove and insert."
  1261. (interactive)
  1262. (setq tpu-rectangular-p (not tpu-rectangular-p))
  1263. (setq tpu-rectangle-string (if tpu-rectangular-p " Rect" ""))
  1264. (tpu-update-mode-line)
  1265. (and (called-interactively-p 'interactive)
  1266. (message "Rectangular cut and paste %sabled."
  1267. (if tpu-rectangular-p "en" "dis"))))
  1268. (defun tpu-arrange-rectangle nil
  1269. "Adjust point and mark to upper left and lower right corners of a rectangle."
  1270. (let ((mc (current-column))
  1271. (pc (progn (exchange-point-and-mark) (current-column))))
  1272. (cond ((> (point) (tpu-mark)) ; point on lower line
  1273. (cond ((> pc mc) ; point @ lower-right
  1274. (exchange-point-and-mark)) ; point -> upper-left
  1275. (t ; point @ lower-left
  1276. (move-to-column mc t) ; point -> lower-right
  1277. (exchange-point-and-mark) ; point -> upper-right
  1278. (move-to-column pc t)))) ; point -> upper-left
  1279. (t ; point on upper line
  1280. (cond ((> pc mc) ; point @ upper-right
  1281. (move-to-column mc t) ; point -> upper-left
  1282. (exchange-point-and-mark) ; point -> lower-left
  1283. (move-to-column pc t) ; point -> lower-right
  1284. (exchange-point-and-mark))))))) ; point -> upper-left
  1285. (defun tpu-cut-text nil
  1286. "Delete the selected region.
  1287. The text is saved for the tpu-paste command."
  1288. (interactive)
  1289. (cond ((tpu-mark)
  1290. (cond (tpu-rectangular-p
  1291. (tpu-arrange-rectangle)
  1292. (picture-clear-rectangle (point) (tpu-mark) (not overwrite-mode))
  1293. (tpu-unselect t))
  1294. (t
  1295. (setq tpu-last-deleted-region
  1296. (buffer-substring (tpu-mark) (point)))
  1297. (delete-region (tpu-mark) (point))
  1298. (tpu-unselect t))))
  1299. ((tpu-check-match)
  1300. (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  1301. (setq tpu-last-deleted-region (buffer-substring beg end))
  1302. (delete-region beg end)
  1303. (tpu-unset-match)))
  1304. (t
  1305. (tpu-error "No selection active."))))
  1306. (defun tpu-store-text nil
  1307. "Copy the selected region to the cut buffer without deleting it.
  1308. The text is saved for the tpu-paste command."
  1309. (interactive)
  1310. (cond ((tpu-mark)
  1311. (cond (tpu-rectangular-p
  1312. (save-excursion
  1313. (tpu-arrange-rectangle)
  1314. (setq picture-killed-rectangle
  1315. (extract-rectangle (point) (tpu-mark))))
  1316. (tpu-unselect t))
  1317. (t
  1318. (setq tpu-last-deleted-region
  1319. (buffer-substring (tpu-mark) (point)))
  1320. (tpu-unselect t))))
  1321. ((tpu-check-match)
  1322. (setq tpu-last-deleted-region
  1323. (buffer-substring (tpu-match-beginning) (tpu-match-end)))
  1324. (tpu-unset-match))
  1325. (t
  1326. (tpu-error "No selection active."))))
  1327. (defun tpu-cut (arg)
  1328. "Copy selected region to the cut buffer.
  1329. In the absence of an argument, delete the selected region too."
  1330. (interactive "P")
  1331. (if arg (tpu-store-text) (tpu-cut-text)))
  1332. (defun tpu-append-region (arg)
  1333. "Append selected region to the tpu-cut buffer.
  1334. In the absence of an argument, delete the selected region too."
  1335. (interactive "P")
  1336. (cond ((tpu-mark)
  1337. (let ((beg (region-beginning)) (end (region-end)))
  1338. (setq tpu-last-deleted-region
  1339. (concat tpu-last-deleted-region
  1340. (buffer-substring beg end)))
  1341. (if (not arg) (delete-region beg end))
  1342. (tpu-unselect t)))
  1343. ((tpu-check-match)
  1344. (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  1345. (setq tpu-last-deleted-region
  1346. (concat tpu-last-deleted-region
  1347. (buffer-substring beg end)))
  1348. (if (not arg) (delete-region beg end))
  1349. (tpu-unset-match)))
  1350. (t
  1351. (tpu-error "No selection active."))))
  1352. (defun tpu-delete-current-line (num)
  1353. "Delete one or specified number of lines after point.
  1354. This includes the newline character at the end of each line.
  1355. They are saved for the TPU-edt undelete-lines command."
  1356. (interactive "p")
  1357. (let ((beg (point)))
  1358. (forward-line num)
  1359. (if (not (eq (preceding-char) ?\n))
  1360. (insert "\n"))
  1361. (setq tpu-last-deleted-lines
  1362. (buffer-substring beg (point)))
  1363. (delete-region beg (point))))
  1364. (defun tpu-delete-to-eol (num)
  1365. "Delete text up to end of line.
  1366. With argument, delete up to the Nth line-end past point.
  1367. They are saved for the TPU-edt undelete-lines command."
  1368. (interactive "p")
  1369. (let ((beg (point)))
  1370. (forward-char 1)
  1371. (end-of-line num)
  1372. (setq tpu-last-deleted-lines
  1373. (buffer-substring beg (point)))
  1374. (delete-region beg (point))))
  1375. (defun tpu-delete-to-bol (num)
  1376. "Delete text back to beginning of line.
  1377. With argument, delete up to the Nth line-end past point.
  1378. They are saved for the TPU-edt undelete-lines command."
  1379. (interactive "p")
  1380. (let ((beg (point)))
  1381. (tpu-next-beginning-of-line num)
  1382. (setq tpu-last-deleted-lines
  1383. (buffer-substring (point) beg))
  1384. (delete-region (point) beg)))
  1385. (defun tpu-delete-current-word (num)
  1386. "Delete one or specified number of words after point.
  1387. They are saved for the TPU-edt undelete-words command."
  1388. (interactive "p")
  1389. (let ((beg (point)))
  1390. (tpu-forward-to-word num)
  1391. (setq tpu-last-deleted-words
  1392. (buffer-substring beg (point)))
  1393. (delete-region beg (point))))
  1394. (defun tpu-delete-previous-word (num)
  1395. "Delete one or specified number of words before point.
  1396. They are saved for the TPU-edt undelete-words command."
  1397. (interactive "p")
  1398. (let ((beg (point)))
  1399. (tpu-backward-to-word num)
  1400. (setq tpu-last-deleted-words
  1401. (buffer-substring (point) beg))
  1402. (delete-region beg (point))))
  1403. (defun tpu-delete-current-char (num)
  1404. "Delete one or specified number of characters after point.
  1405. The last character deleted is saved for the TPU-edt undelete-char command."
  1406. (interactive "p")
  1407. (while (and (> num 0) (not (eobp)))
  1408. (setq tpu-last-deleted-char (char-after (point)))
  1409. (cond (overwrite-mode
  1410. (picture-clear-column 1)
  1411. (forward-char 1))
  1412. (t
  1413. (delete-char 1)))
  1414. (setq num (1- num))))
  1415. ;;;
  1416. ;;; Undelete / Paste
  1417. ;;;
  1418. (defun tpu-paste (num)
  1419. "Insert the last region or rectangle of killed text.
  1420. With argument reinserts the text that many times."
  1421. (interactive "p")
  1422. (while (> num 0)
  1423. (cond (tpu-rectangular-p
  1424. (let ((beg (point)))
  1425. (save-excursion
  1426. (picture-yank-rectangle (not overwrite-mode))
  1427. (message ""))
  1428. (goto-char beg)))
  1429. (t
  1430. (insert tpu-last-deleted-region)))
  1431. (setq num (1- num))))
  1432. (defun tpu-undelete-lines (num)
  1433. "Insert lines deleted by last TPU-edt line-deletion command.
  1434. With argument reinserts lines that many times."
  1435. (interactive "p")
  1436. (let ((beg (point)))
  1437. (while (> num 0)
  1438. (insert tpu-last-deleted-lines)
  1439. (setq num (1- num)))
  1440. (goto-char beg)))
  1441. (defun tpu-undelete-words (num)
  1442. "Insert words deleted by last TPU-edt word-deletion command.
  1443. With argument reinserts words that many times."
  1444. (interactive "p")
  1445. (let ((beg (point)))
  1446. (while (> num 0)
  1447. (insert tpu-last-deleted-words)
  1448. (setq num (1- num)))
  1449. (goto-char beg)))
  1450. (defun tpu-undelete-char (num)
  1451. "Insert character deleted by last TPU-edt character-deletion command.
  1452. With argument reinserts the character that many times."
  1453. (interactive "p")
  1454. (while (> num 0)
  1455. (if overwrite-mode (prog1 (forward-char -1) (delete-char 1)))
  1456. (insert tpu-last-deleted-char)
  1457. (forward-char -1)
  1458. (setq num (1- num))))
  1459. ;;;
  1460. ;;; Replace and Substitute
  1461. ;;;
  1462. (defun tpu-replace nil
  1463. "Replace the selected region with the contents of the cut buffer."
  1464. (interactive)
  1465. (cond ((tpu-mark)
  1466. (let ((beg (region-beginning)) (end (region-end)))
  1467. (setq tpu-last-replaced-text (buffer-substring beg end))
  1468. (delete-region beg end)
  1469. (insert tpu-last-deleted-region)
  1470. (tpu-unselect t)))
  1471. ((tpu-check-match)
  1472. (let ((beg (tpu-match-beginning)) (end (tpu-match-end)))
  1473. (setq tpu-last-replaced-text (buffer-substring beg end))
  1474. (replace-match tpu-last-deleted-region
  1475. (not case-replace) (not tpu-regexp-p))
  1476. (tpu-unset-match)))
  1477. (t
  1478. (tpu-error "No selection active."))))
  1479. (defun tpu-substitute (num)
  1480. "Replace the selected region with the contents of the cut buffer,
  1481. and repeat most recent search. A numeric argument serves as a repeat count.
  1482. A negative argument means replace all occurrences of the search string."
  1483. (interactive "p")
  1484. (cond ((or (tpu-mark) (tpu-check-match))
  1485. (while (and (not (= num 0)) (or (tpu-mark) (tpu-check-match)))
  1486. (let ((beg (point)))
  1487. (tpu-replace)
  1488. (if tpu-searching-forward (forward-char -1) (goto-char beg))
  1489. (if (= num 1) (tpu-search-internal tpu-search-last-string)
  1490. (tpu-search-internal-core tpu-search-last-string)))
  1491. (setq num (1- num))))
  1492. (t
  1493. (tpu-error "No selection active."))))
  1494. (defun tpu-lm-replace (from to)
  1495. "Interactively search for OLD-string and substitute NEW-string."
  1496. (interactive (list (tpu-regexp-prompt "Old String: ")
  1497. (tpu-regexp-prompt "New String: ")))
  1498. (let ((doit t) (strings 0))
  1499. ;; Can't replace null strings
  1500. (if (string= "" from) (tpu-error "No string to replace."))
  1501. ;; Find the first occurrence
  1502. (tpu-set-search)
  1503. (tpu-search-internal from t)
  1504. ;; Loop on replace question - yes, no, all, last, or quit.
  1505. (while doit
  1506. (if (not (tpu-check-match)) (setq doit nil)
  1507. (progn
  1508. (move-overlay tpu-replace-overlay
  1509. (tpu-match-beginning) (tpu-match-end) (current-buffer))
  1510. (message "Replace? Type Yes, No, All, Last, or Quit: ")
  1511. (let ((ans (read-char)))
  1512. (cond ((or (= ans ?y) (= ans ?Y) (= ans ?\r) (= ans ?\ ))
  1513. (let ((beg (point)))
  1514. (replace-match to (not case-replace) (not tpu-regexp-p))
  1515. (setq strings (1+ strings))
  1516. (if tpu-searching-forward (forward-char -1) (goto-char beg)))
  1517. (tpu-search-internal from t))
  1518. ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
  1519. (tpu-search-internal from t))
  1520. ((or (= ans ?a) (= ans ?A))
  1521. (save-excursion
  1522. (let ((beg (point)))
  1523. (replace-match to (not case-replace) (not tpu-regexp-p))
  1524. (setq strings (1+ strings))
  1525. (if tpu-searching-forward (forward-char -1) (goto-char beg)))
  1526. (tpu-search-internal-core from t)
  1527. (while (tpu-check-match)
  1528. (let ((beg (point)))
  1529. (replace-match to (not case-replace) (not tpu-regexp-p))
  1530. (setq strings (1+ strings))
  1531. (if tpu-searching-forward (forward-char -1) (goto-char beg)))
  1532. (tpu-search-internal-core from t)))
  1533. (setq doit nil))
  1534. ((or (= ans ?l) (= ans ?L))
  1535. (let ((beg (point)))
  1536. (replace-match to (not case-replace) (not tpu-regexp-p))
  1537. (setq strings (1+ strings))
  1538. (if tpu-searching-forward (forward-char -1) (goto-char beg)))
  1539. (setq doit nil))
  1540. ((or (= ans ?q) (= ans ?Q))
  1541. (tpu-unset-match)
  1542. (setq doit nil)))))))
  1543. (move-overlay tpu-replace-overlay 1 1 (current-buffer))
  1544. (message "Replaced %s occurrence%s." strings (if (not (= 1 strings)) "s" ""))))
  1545. (defun tpu-emacs-replace (&optional dont-ask)
  1546. "A TPU-edt interface to the Emacs replace functions.
  1547. If TPU-edt is currently in regular expression mode, the Emacs regular
  1548. expression replace functions are used. If an argument is supplied,
  1549. replacements are performed without asking. Only works in forward direction."
  1550. (interactive "P")
  1551. (cond (dont-ask
  1552. (setq current-prefix-arg nil)
  1553. (call-interactively
  1554. (if tpu-regexp-p 'replace-regexp 'replace-string)))
  1555. (t
  1556. (call-interactively
  1557. (if tpu-regexp-p 'query-replace-regexp 'query-replace)))))
  1558. (defun tpu-add-at-bol (text)
  1559. "Add text to the beginning of each line in a region,
  1560. or each line in the entire buffer if no region is selected."
  1561. (interactive
  1562. (list (tpu-string-prompt "String to add: " 'tpu-add-at-bol-hist)))
  1563. (if (string= "" text) (tpu-error "No string specified."))
  1564. (cond ((tpu-mark)
  1565. (save-excursion
  1566. (if (> (point) (tpu-mark)) (exchange-point-and-mark))
  1567. (while (and (< (point) (tpu-mark)) (re-search-forward "^" (tpu-mark) t))
  1568. (if (< (point) (tpu-mark)) (replace-match text))))
  1569. (tpu-unselect t))
  1570. (t
  1571. (save-excursion
  1572. (goto-char (point-min))
  1573. (while (and (re-search-forward "^" nil t) (not (eobp)))
  1574. (replace-match text))))))
  1575. (defun tpu-add-at-eol (text)
  1576. "Add text to the end of each line in a region,
  1577. or each line of the entire buffer if no region is selected."
  1578. (interactive
  1579. (list (tpu-string-prompt "String to add: " 'tpu-add-at-eol-hist)))
  1580. (if (string= "" text) (tpu-error "No string specified."))
  1581. (cond ((tpu-mark)
  1582. (save-excursion
  1583. (if (> (point) (tpu-mark)) (exchange-point-and-mark))
  1584. (while (< (point) (tpu-mark))
  1585. (end-of-line)
  1586. (if (<= (point) (tpu-mark)) (insert text))
  1587. (forward-line)))
  1588. (tpu-unselect t))
  1589. (t
  1590. (save-excursion
  1591. (goto-char (point-min))
  1592. (while (not (eobp))
  1593. (end-of-line) (insert text) (forward-line))))))
  1594. (defun tpu-trim-line-ends nil
  1595. "Remove trailing whitespace from every line in the buffer."
  1596. (interactive)
  1597. (save-match-data
  1598. (save-excursion
  1599. (goto-char (point-min))
  1600. (while (re-search-forward "[ \t][ \t]*$" nil t)
  1601. (delete-region (match-beginning 0) (match-end 0))))))
  1602. ;;;
  1603. ;;; Movement by character
  1604. ;;;
  1605. (defun tpu-char (num)
  1606. "Move to the next character in the current direction.
  1607. A repeat count means move that many characters."
  1608. (interactive "p")
  1609. (if tpu-advance (tpu-forward-char num) (tpu-backward-char num)))
  1610. (defun tpu-forward-char (num)
  1611. "Move right ARG characters (left if ARG is negative)."
  1612. (interactive "p")
  1613. (forward-char num))
  1614. (defun tpu-backward-char (num)
  1615. "Move left ARG characters (right if ARG is negative)."
  1616. (interactive "p")
  1617. (backward-char num))
  1618. ;;;
  1619. ;;; Movement by word
  1620. ;;;
  1621. (defvar tpu-word-separator-list '()
  1622. "List of additional word separators.")
  1623. (defvar tpu-skip-chars "^ \t"
  1624. "Characters to skip when moving by word.
  1625. Additional word separators are added to this string.")
  1626. (defun tpu-word (num)
  1627. "Move to the beginning of the next word in the current direction.
  1628. A repeat count means move that many words."
  1629. (interactive "p")
  1630. (if tpu-advance (tpu-forward-to-word num) (tpu-backward-to-word num)))
  1631. (defun tpu-forward-to-word (num)
  1632. "Move forward until encountering the beginning of a word.
  1633. With argument, do this that many times."
  1634. (interactive "p")
  1635. (while (and (> num 0) (not (eobp)))
  1636. (let* ((beg (point))
  1637. (end (prog2 (end-of-line) (point) (goto-char beg))))
  1638. (cond ((eolp)
  1639. (forward-char 1))
  1640. ((memq (char-after (point)) tpu-word-separator-list)
  1641. (forward-char 1)
  1642. (skip-chars-forward " \t" end))
  1643. (t
  1644. (skip-chars-forward tpu-skip-chars end)
  1645. (skip-chars-forward " \t" end))))
  1646. (setq num (1- num))))
  1647. (defun tpu-backward-to-word (num)
  1648. "Move backward until encountering the beginning of a word.
  1649. With argument, do this that many times."
  1650. (interactive "p")
  1651. (while (and (> num 0) (not (bobp)))
  1652. (let* ((beg (point))
  1653. (end (prog2 (beginning-of-line) (point) (goto-char beg))))
  1654. (cond ((bolp)
  1655. ( forward-char -1))
  1656. ((memq (char-after (1- (point))) tpu-word-separator-list)
  1657. (forward-char -1))
  1658. (t
  1659. (skip-chars-backward " \t" end)
  1660. (skip-chars-backward tpu-skip-chars end)
  1661. (if (and (not (bolp)) (= ? (char-syntax (char-after (point)))))
  1662. (forward-char -1)))))
  1663. (setq num (1- num))))
  1664. (defun tpu-add-word-separators (separators)
  1665. "Add new word separators for TPU-edt word commands."
  1666. (interactive "sSeparators: ")
  1667. (let* ((n 0) (length (length separators)))
  1668. (while (< n length)
  1669. (let ((char (aref separators n))
  1670. (ss (substring separators n (1+ n))))
  1671. (cond ((not (memq char tpu-word-separator-list))
  1672. (setq tpu-word-separator-list
  1673. (append ss tpu-word-separator-list))
  1674. (cond ((= char ?-)
  1675. (setq tpu-skip-chars (concat tpu-skip-chars "\\-")))
  1676. ((= char ?\\)
  1677. (setq tpu-skip-chars (concat tpu-skip-chars "\\\\")))
  1678. ((= char ?^)
  1679. (setq tpu-skip-chars (concat tpu-skip-chars "\\^")))
  1680. (t
  1681. (setq tpu-skip-chars (concat tpu-skip-chars ss))))))
  1682. (setq n (1+ n))))))
  1683. (defun tpu-reset-word-separators nil
  1684. "Reset word separators to default value."
  1685. (interactive)
  1686. (setq tpu-word-separator-list nil)
  1687. (setq tpu-skip-chars "^ \t"))
  1688. (defun tpu-set-word-separators (separators)
  1689. "Set new word separators for TPU-edt word commands."
  1690. (interactive "sSeparators: ")
  1691. (tpu-reset-word-separators)
  1692. (tpu-add-word-separators separators))
  1693. ;;;
  1694. ;;; Movement by line
  1695. ;;;
  1696. (defun tpu-next-line (num)
  1697. "Move to next line.
  1698. Prefix argument serves as a repeat count."
  1699. (interactive "p")
  1700. (line-move num)
  1701. (setq this-command 'next-line))
  1702. (defun tpu-previous-line (num)
  1703. "Move to previous line.
  1704. Prefix argument serves as a repeat count."
  1705. (interactive "p")
  1706. (line-move (- num))
  1707. (setq this-command 'previous-line))
  1708. (defun tpu-next-beginning-of-line (num)
  1709. "Move to beginning of line; if at beginning, move to beginning of next line.
  1710. Accepts a prefix argument for the number of lines to move."
  1711. (interactive "p")
  1712. (backward-char 1)
  1713. (forward-visible-line (- 1 num)))
  1714. (defun tpu-end-of-line (num)
  1715. "Move to the next end of line in the current direction.
  1716. A repeat count means move that many lines."
  1717. (interactive "p")
  1718. (if tpu-advance (tpu-next-end-of-line num) (tpu-previous-end-of-line num)))
  1719. (defun tpu-next-end-of-line (num)
  1720. "Move to end of line; if at end, move to end of next line.
  1721. Accepts a prefix argument for the number of lines to move."
  1722. (interactive "p")
  1723. (forward-char 1)
  1724. (end-of-line num))
  1725. (defun tpu-previous-end-of-line (num)
  1726. "Move EOL upward.
  1727. Accepts a prefix argument for the number of lines to move."
  1728. (interactive "p")
  1729. (end-of-line (- 1 num)))
  1730. (defun tpu-current-end-of-line nil
  1731. "Move point to end of current line."
  1732. (interactive)
  1733. (let ((beg (point)))
  1734. (end-of-line)
  1735. (if (= beg (point)) (message "You are already at the end of a line."))))
  1736. (defun tpu-line (num)
  1737. "Move to the beginning of the next line in the current direction.
  1738. A repeat count means move that many lines."
  1739. (interactive "p")
  1740. (if tpu-advance (tpu-forward-line num) (tpu-backward-line num)))
  1741. (defun tpu-forward-line (num)
  1742. "Move to beginning of next line.
  1743. Prefix argument serves as a repeat count."
  1744. (interactive "p")
  1745. (forward-line num))
  1746. (defun tpu-backward-line (num)
  1747. "Move to beginning of previous line.
  1748. Prefix argument serves as repeat count."
  1749. (interactive "p")
  1750. (or (bolp) (>= 0 num) (setq num (- num 1)))
  1751. (forward-line (- num)))
  1752. ;;;
  1753. ;;; Movement by paragraph
  1754. ;;;
  1755. (defun tpu-paragraph (num)
  1756. "Move to the next paragraph in the current direction.
  1757. A repeat count means move that many paragraphs."
  1758. (interactive "p")
  1759. (if tpu-advance
  1760. (tpu-next-paragraph num) (tpu-previous-paragraph num)))
  1761. (defun tpu-next-paragraph (num)
  1762. "Move to beginning of the next paragraph.
  1763. Accepts a prefix argument for the number of paragraphs."
  1764. (interactive "p")
  1765. (beginning-of-line)
  1766. (while (and (not (eobp)) (> num 0))
  1767. (if (re-search-forward "^[ \t]*$" nil t)
  1768. (if (re-search-forward "[^ \t\n]" nil t)
  1769. (goto-char (match-beginning 0))
  1770. (goto-char (point-max))))
  1771. (setq num (1- num)))
  1772. (beginning-of-line))
  1773. (defun tpu-previous-paragraph (num)
  1774. "Move to beginning of previous paragraph.
  1775. Accepts a prefix argument for the number of paragraphs."
  1776. (interactive "p")
  1777. (end-of-line)
  1778. (while (and (not (bobp)) (> num 0))
  1779. (if (not (and (re-search-backward "^[ \t]*$" nil t)
  1780. (re-search-backward "[^ \t\n]" nil t)
  1781. (re-search-backward "^[ \t]*$" nil t)
  1782. (progn (re-search-forward "[^ \t\n]" nil t)
  1783. (goto-char (match-beginning 0)))))
  1784. (goto-char (point-min)))
  1785. (setq num (1- num)))
  1786. (beginning-of-line))
  1787. ;;;
  1788. ;;; Movement by page
  1789. ;;;
  1790. (defun tpu-page (num)
  1791. "Move to the next page in the current direction.
  1792. A repeat count means move that many pages."
  1793. (interactive "p")
  1794. (if tpu-advance (forward-page num) (backward-page num))
  1795. (if (eobp) (recenter -1)))
  1796. ;;;
  1797. ;;; Scrolling and movement within the buffer
  1798. ;;;
  1799. (defun tpu-scroll-window (num)
  1800. "Scroll the display to the next section in the current direction.
  1801. A repeat count means scroll that many sections."
  1802. (interactive "p")
  1803. (if tpu-advance (tpu-scroll-window-up num) (tpu-scroll-window-down num)))
  1804. (defun tpu-scroll-window-down (num)
  1805. "Scroll the display down to the next section.
  1806. A repeat count means scroll that many sections."
  1807. (interactive "p")
  1808. (let* ((beg (tpu-current-line))
  1809. (height (1- (window-height)))
  1810. (lines (* num (/ (* height tpu-percent-scroll) 100))))
  1811. (line-move (- lines))
  1812. (if (> lines beg) (recenter 0))))
  1813. (defun tpu-scroll-window-up (num)
  1814. "Scroll the display up to the next section.
  1815. A repeat count means scroll that many sections."
  1816. (interactive "p")
  1817. (let* ((beg (tpu-current-line))
  1818. (height (1- (window-height)))
  1819. (lines (* num (/ (* height tpu-percent-scroll) 100))))
  1820. (line-move lines)
  1821. (if (>= (+ lines beg) height) (recenter -1))))
  1822. (defun tpu-pan-right (num)
  1823. "Pan right tpu-pan-columns (16 by default).
  1824. Accepts a prefix argument for the number of tpu-pan-columns to scroll."
  1825. (interactive "p")
  1826. (scroll-left (* tpu-pan-columns num)))
  1827. (defun tpu-pan-left (num)
  1828. "Pan left tpu-pan-columns (16 by default).
  1829. Accepts a prefix argument for the number of tpu-pan-columns to scroll."
  1830. (interactive "p")
  1831. (scroll-right (* tpu-pan-columns num)))
  1832. (defun tpu-move-to-beginning nil
  1833. "Move cursor to the beginning of buffer, but don't set the mark."
  1834. (interactive)
  1835. (goto-char (point-min)))
  1836. (defun tpu-move-to-end nil
  1837. "Move cursor to the end of buffer, but don't set the mark."
  1838. (interactive)
  1839. (goto-char (point-max))
  1840. (recenter -1))
  1841. (defun tpu-goto-percent (perc)
  1842. "Move point to ARG percentage of the buffer."
  1843. (interactive "NGoto-percentage: ")
  1844. (if (or (> perc 100) (< perc 0))
  1845. (tpu-error "Percentage %d out of range 0 < percent < 100." perc)
  1846. (goto-char (/ (* (point-max) perc) 100))))
  1847. (defun tpu-beginning-of-window nil
  1848. "Move cursor to top of window."
  1849. (interactive)
  1850. (move-to-window-line 0))
  1851. (defun tpu-end-of-window nil
  1852. "Move cursor to bottom of window."
  1853. (interactive)
  1854. (move-to-window-line -1))
  1855. (defun tpu-line-to-bottom-of-window nil
  1856. "Move the current line to the bottom of the window."
  1857. (interactive)
  1858. (recenter -1))
  1859. (defun tpu-line-to-top-of-window nil
  1860. "Move the current line to the top of the window."
  1861. (interactive)
  1862. (recenter 0))
  1863. ;;;
  1864. ;;; Direction
  1865. ;;;
  1866. (defun tpu-advance-direction nil
  1867. "Set TPU Advance mode so keypad commands move forward."
  1868. (interactive)
  1869. (setq tpu-direction-string " Advance")
  1870. (setq tpu-advance t)
  1871. (setq tpu-reverse nil)
  1872. (tpu-set-search)
  1873. (tpu-update-mode-line))
  1874. (defun tpu-backup-direction nil
  1875. "Set TPU Backup mode so keypad commands move backward."
  1876. (interactive)
  1877. (setq tpu-direction-string " Reverse")
  1878. (setq tpu-advance nil)
  1879. (setq tpu-reverse t)
  1880. (tpu-set-search)
  1881. (tpu-update-mode-line))
  1882. (defun tpu-toggle-direction nil
  1883. "Change the current TPU direction."
  1884. (interactive)
  1885. (if tpu-advance (tpu-backup-direction) (tpu-advance-direction)))
  1886. ;;;
  1887. ;;; Minibuffer map additions to make KP_enter = RET
  1888. ;;;
  1889. ;; Standard Emacs settings under xterm in function-key-map map
  1890. ;; "\eOM" to [kp-enter] and [kp-enter] to RET, but since the output of the map
  1891. ;; is not fed back into the map, the key stays as kp-enter :-(.
  1892. (define-key minibuffer-local-map [kp-enter] 'exit-minibuffer)
  1893. ;; These are not necessary because they are inherited.
  1894. ;; (define-key minibuffer-local-ns-map [kp-enter] 'exit-minibuffer)
  1895. ;; (define-key minibuffer-local-completion-map [kp-enter] 'exit-minibuffer)
  1896. (define-key minibuffer-local-must-match-map [kp-enter] 'minibuffer-complete-and-exit)
  1897. ;;;
  1898. ;;; Minibuffer map additions to set search direction
  1899. ;;;
  1900. (define-key minibuffer-local-map [kp-4] 'tpu-search-forward-exit) ;KP4
  1901. (define-key minibuffer-local-map [kp-5] 'tpu-search-backward-exit) ;KP5
  1902. ;;;
  1903. ;;; Functions to set, reset, and toggle the control key bindings
  1904. ;;;
  1905. (defvar tpu-control-keys-map
  1906. (let ((map (make-sparse-keymap)))
  1907. (define-key map "\C-\\" 'quoted-insert) ; ^\
  1908. (define-key map "\C-a" 'tpu-toggle-overwrite-mode) ; ^A
  1909. (define-key map "\C-b" 'repeat-complex-command) ; ^B
  1910. (define-key map "\C-e" 'tpu-current-end-of-line) ; ^E
  1911. (define-key map "\C-h" 'tpu-next-beginning-of-line) ; ^H (BS)
  1912. (define-key map "\C-j" 'tpu-delete-previous-word) ; ^J (LF)
  1913. (define-key map "\C-k" 'tpu-define-macro-key) ; ^K
  1914. (define-key map "\C-l" 'tpu-insert-formfeed) ; ^L (FF)
  1915. (define-key map "\C-r" 'recenter) ; ^R
  1916. (define-key map "\C-u" 'tpu-delete-to-bol) ; ^U
  1917. (define-key map "\C-v" 'tpu-quoted-insert) ; ^V
  1918. (define-key map "\C-w" 'redraw-display) ; ^W
  1919. (define-key map "\C-z" 'tpu-exit) ; ^Z
  1920. map))
  1921. (defun tpu-set-control-keys ()
  1922. "Set control keys to TPU style functions."
  1923. (tpu-reset-control-keys 'tpu))
  1924. (defun tpu-reset-control-keys (tpu-style)
  1925. "Set control keys to TPU or Emacs style functions."
  1926. (let ((parent (keymap-parent tpu-global-map)))
  1927. (if tpu-style
  1928. (if (eq parent tpu-control-keys-map)
  1929. nil ;All done already.
  1930. ;; Insert tpu-control-keys-map in the global map.
  1931. (set-keymap-parent tpu-control-keys-map parent)
  1932. (set-keymap-parent tpu-global-map tpu-control-keys-map))
  1933. (if (not (eq parent tpu-control-keys-map))
  1934. nil ;All done already.
  1935. ;; Remove tpu-control-keys-map from the global map.
  1936. (set-keymap-parent tpu-global-map (keymap-parent parent))
  1937. (set-keymap-parent tpu-control-keys-map nil)))
  1938. (setq tpu-control-keys tpu-style)))
  1939. (defun tpu-toggle-control-keys nil
  1940. "Toggle control key bindings between TPU-edt and Emacs."
  1941. (interactive)
  1942. (tpu-reset-control-keys (not tpu-control-keys))
  1943. (and (called-interactively-p 'interactive)
  1944. (message "Control keys function with %s bindings."
  1945. (if tpu-control-keys "TPU-edt" "Emacs"))))
  1946. ;;;
  1947. ;;; Emacs version 19 minibuffer history support
  1948. ;;;
  1949. (defun tpu-next-history-element (n)
  1950. "Insert the next element of the minibuffer history into the minibuffer."
  1951. (interactive "p")
  1952. (next-history-element n)
  1953. (goto-char (point-max)))
  1954. (defun tpu-previous-history-element (n)
  1955. "Insert the previous element of the minibuffer history into the minibuffer."
  1956. (interactive "p")
  1957. (previous-history-element n)
  1958. (goto-char (point-max)))
  1959. (defun tpu-arrow-history nil
  1960. "Modify minibuffer maps to use arrows for history recall."
  1961. (interactive)
  1962. (dolist (cur (where-is-internal 'tpu-previous-line))
  1963. (define-key read-expression-map cur 'tpu-previous-history-element)
  1964. (define-key minibuffer-local-map cur 'tpu-previous-history-element)
  1965. ;; These are inherited anyway. --Stef
  1966. ;; (define-key minibuffer-local-ns-map cur 'tpu-previous-history-element)
  1967. ;; (define-key minibuffer-local-completion-map cur 'tpu-previous-history-element)
  1968. ;; (define-key minibuffer-local-must-match-map cur 'tpu-previous-history-element)
  1969. )
  1970. (dolist (cur (where-is-internal 'tpu-next-line))
  1971. (define-key read-expression-map cur 'tpu-next-history-element)
  1972. (define-key minibuffer-local-map cur 'tpu-next-history-element)
  1973. ;; These are inherited anyway. --Stef
  1974. ;; (define-key minibuffer-local-ns-map cur 'tpu-next-history-element)
  1975. ;; (define-key minibuffer-local-completion-map cur 'tpu-next-history-element)
  1976. ;; (define-key minibuffer-local-must-match-map cur 'tpu-next-history-element)
  1977. ))
  1978. ;;;
  1979. ;;; Emacs version 19 X-windows key definition support
  1980. ;;;
  1981. (defun tpu-load-xkeys (file)
  1982. "Load the TPU-edt X-windows key definitions FILE.
  1983. If FILE is nil, try to load a default file. The default file names are
  1984. `~/.tpu-lucid-keys' for XEmacs, and `~/.tpu-keys' for Emacs."
  1985. (interactive "fX key definition file: ")
  1986. (cond (file
  1987. (setq file (expand-file-name file)))
  1988. (tpu-xkeys-file
  1989. (setq file (expand-file-name tpu-xkeys-file)))
  1990. ((featurep 'xemacs)
  1991. (setq file (convert-standard-filename
  1992. (expand-file-name "~/.tpu-lucid-keys"))))
  1993. (t
  1994. (setq file (convert-standard-filename
  1995. (expand-file-name "~/.tpu-keys")))
  1996. (and (not (file-exists-p file))
  1997. (file-exists-p
  1998. (convert-standard-filename
  1999. (expand-file-name "~/.tpu-gnu-keys")))
  2000. (tpu-copy-keyfile
  2001. (convert-standard-filename
  2002. (expand-file-name "~/.tpu-gnu-keys")) file))))
  2003. (cond ((file-readable-p file)
  2004. (load-file file))
  2005. (t
  2006. ;; This used to force the user to build `file'. With the
  2007. ;; new code, such a file may not be necessary. In case it
  2008. ;; is, issue a message giving a hint as to how to build it.
  2009. (message "%s not found: use M-x tpu-mapper to create it"
  2010. (abbreviate-file-name file)))))
  2011. (defun tpu-copy-keyfile (oldname newname)
  2012. "Copy the TPU-edt X key definitions file to the new default name."
  2013. (interactive "fOld name: \nFNew name: ")
  2014. (if (not (get-buffer "*TPU-Notice*")) (generate-new-buffer "*TPU-Notice*"))
  2015. (set-buffer "*TPU-Notice*")
  2016. (erase-buffer)
  2017. (insert "
  2018. NOTICE --
  2019. The default name of the TPU-edt key definition file has changed
  2020. from `~/.tpu-gnu-keys' to `~/.tpu-keys'. With your permission,
  2021. your key definitions will be copied to the new file. If you'll
  2022. never use older versions of Emacs, you can remove the old file.
  2023. If the copy fails, you'll be asked if you want to create a new
  2024. key definitions file. Do you want to copy your key definition
  2025. file now?
  2026. ")
  2027. (save-window-excursion
  2028. (switch-to-buffer-other-window "*TPU-Notice*")
  2029. (shrink-window-if-larger-than-buffer)
  2030. (goto-char (point-min))
  2031. (beep)
  2032. (and (tpu-y-or-n-p "Copy key definitions to the new file now? ")
  2033. (condition-case conditions
  2034. (copy-file oldname newname)
  2035. (error (message "Sorry, couldn't copy - %s." (cdr conditions)))))
  2036. (kill-buffer "*TPU-Notice*")))
  2037. (defvar tpu-edt-old-global-values nil)
  2038. ;;;
  2039. ;;; Start and Stop TPU-edt
  2040. ;;;
  2041. ;;;###autoload
  2042. (defun tpu-edt-on ()
  2043. "Turn on TPU/edt emulation."
  2044. (interactive)
  2045. ;; To clean things up (and avoid cycles in the global map).
  2046. (tpu-edt-off)
  2047. ;; First, activate tpu-global-map, while protecting the original keymap.
  2048. (set-keymap-parent tpu-global-map global-map)
  2049. (setq global-map tpu-global-map)
  2050. (use-global-map global-map)
  2051. ;; Then do the normal TPU setup.
  2052. (transient-mark-mode t)
  2053. (add-hook 'post-command-hook 'tpu-search-highlight)
  2054. (tpu-set-mode-line t)
  2055. (tpu-advance-direction)
  2056. ;; set page delimiter, display line truncation, and scrolling like TPU
  2057. (dolist (varval '((page-delimiter . "\f")
  2058. (truncate-lines . t)
  2059. (scroll-step . 1)))
  2060. (push (cons (car varval) (default-value (car varval)))
  2061. tpu-edt-old-global-values)
  2062. (set-default (car varval) (cdr varval)))
  2063. (tpu-set-control-keys)
  2064. (and window-system (tpu-load-xkeys nil))
  2065. (tpu-arrow-history)
  2066. ;; Then protect tpu-global-map from user modifications.
  2067. (let ((map (make-sparse-keymap)))
  2068. (set-keymap-parent map global-map)
  2069. (setq global-map map)
  2070. (use-global-map map))
  2071. (setq tpu-edt-mode t))
  2072. (defun tpu-edt-off ()
  2073. "Turn off TPU/edt emulation. Note that the keypad is left on."
  2074. (interactive)
  2075. (tpu-reset-control-keys nil)
  2076. (remove-hook 'post-command-hook 'tpu-search-highlight)
  2077. (tpu-set-mode-line nil)
  2078. (while tpu-edt-old-global-values
  2079. (let ((varval (pop tpu-edt-old-global-values)))
  2080. (set-default (car varval) (cdr varval))))
  2081. ;; Remove tpu-global-map from the global map.
  2082. (let ((map global-map))
  2083. (while map
  2084. (let ((parent (keymap-parent map)))
  2085. (if (eq tpu-global-map parent)
  2086. (set-keymap-parent map (keymap-parent parent))
  2087. (setq map parent)))))
  2088. ;; Only has an effect if the advice in tpu-extras has been activated.
  2089. (condition-case nil
  2090. (with-no-warnings (ad-disable-regexp "\\`tpu-"))
  2091. (error nil))
  2092. (setq tpu-edt-mode nil))
  2093. ;;;### (autoloads (tpu-set-cursor-bound tpu-set-cursor-free tpu-set-scroll-margins
  2094. ;;;;;; tpu-cursor-free-mode) "tpu-extras" "tpu-extras.el" "76f06905db4c5bfb3b86491a51512a0e")
  2095. ;;; Generated autoloads from tpu-extras.el
  2096. (autoload 'tpu-cursor-free-mode "tpu-extras" "\
  2097. Minor mode to allow the cursor to move freely about the screen.
  2098. With a prefix argument ARG, enable the mode if ARG is positive,
  2099. and disable it otherwise. If called from Lisp, enable the mode
  2100. if ARG is omitted or nil.
  2101. \(fn &optional ARG)" t nil)
  2102. (autoload 'tpu-set-scroll-margins "tpu-extras" "\
  2103. Set scroll margins.
  2104. \(fn TOP BOTTOM)" t nil)
  2105. (autoload 'tpu-set-cursor-free "tpu-extras" "\
  2106. Allow the cursor to move freely about the screen.
  2107. \(fn)" t nil)
  2108. (autoload 'tpu-set-cursor-bound "tpu-extras" "\
  2109. Constrain the cursor to the flow of the text.
  2110. \(fn)" t nil)
  2111. ;;;***
  2112. (provide 'tpu-edt)
  2113. ;;; tpu-edt.el ends here