term.el 175 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544
  1. ;;; term.el --- general command interpreter in a window stuff
  2. ;; Copyright (C) 1988, 1990, 1992, 1994-1995, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Per Bothner <per@bothner.com>
  5. ;; Maintainer: Dan Nicolaescu <dann@ics.uci.edu>, Per Bothner <per@bothner.com>
  6. ;; Based on comint mode written by: Olin Shivers <shivers@cs.cmu.edu>
  7. ;; Keywords: processes
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;; Marck 13 2001
  20. ;; Fixes for CJK support by Yong Lu <lyongu@yahoo.com>.
  21. ;; Dir/Hostname tracking and ANSI colorization by
  22. ;; Marco Melgazzi <marco@techie.com>.
  23. ;; To see what I've modified and where it came from search for '-mm'
  24. ;;; Commentary:
  25. ;; Speed considerations and a few caveats
  26. ;; --------------------------------------
  27. ;;
  28. ;; While the message passing and the colorization surely introduce some
  29. ;; overhead this has became so small that IMHO it is surely outweighed by
  30. ;; the benefits you get but, as usual, YMMV.
  31. ;;
  32. ;; Important caveat, when deciding the cursor/'gray keys' keycodes I had to
  33. ;; make a choice: on my Linux box this choice allows me to run all the
  34. ;; ncurses applications without problems but make these keys
  35. ;; incomprehensible to all the cursesX programs. Your mileage may vary so
  36. ;; you may consider changing the default 'emulation'. Just search for this
  37. ;; piece of code and modify it as you like:
  38. ;;
  39. ;; ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
  40. ;; ;; For my configuration it's definitely better \eOA but YMMV. -mm
  41. ;; ;; For example: vi works with \eOA while elm wants \e[A ...
  42. ;; (defun term-send-up () (interactive) (term-send-raw-string "\eOA"))
  43. ;; (defun term-send-down () (interactive) (term-send-raw-string "\eOB"))
  44. ;; (defun term-send-right () (interactive) (term-send-raw-string "\eOC"))
  45. ;; (defun term-send-left () (interactive) (term-send-raw-string "\eOD"))
  46. ;;
  47. ;;
  48. ;; IMPORTANT: additions & changes
  49. ;; ------------------------------
  50. ;;
  51. ;; With this enhanced ansi-term.el you will get a reliable mechanism of
  52. ;; directory/username/host tracking: the only drawback is that you will
  53. ;; have to modify your shell start-up script. It's worth it, believe me :).
  54. ;;
  55. ;; When you rlogin/su/telnet and the account you access has a modified
  56. ;; startup script, you will be able to access the remote files as usual
  57. ;; with C-x C-f, if it's needed you will have to enter a password,
  58. ;; otherwise the file should get loaded straight away.
  59. ;;
  60. ;; This is useful even if you work only on one host: it often happens that,
  61. ;; for maintenance reasons, you have to edit files 'as root': before
  62. ;; patching term.el, I su-ed in a term.el buffer and used vi :), now I
  63. ;; simply do a C-x C-f and, via ange-ftp, the file is automatically loaded
  64. ;; 'as-root'. ( If you don't want to enter the root password every time you
  65. ;; can put it in your .netrc: note that this is -not- advisable if you're
  66. ;; connected to the internet or if somebody else works on your workstation!)
  67. ;;
  68. ;; If you use wu-ftpd you can use some of its features to avoid root ftp
  69. ;; access to the rest of the world: just put in /etc/ftphosts something like
  70. ;;
  71. ;; # Local access
  72. ;; allow root 127.0.0.1
  73. ;;
  74. ;; # By default nobody can't do anything
  75. ;; deny root *
  76. ;;
  77. ;;
  78. ;; ----------------------------------------
  79. ;;
  80. ;; If, instead of 'term', you call 'ansi-term', you get multiple term
  81. ;; buffers, after every new call ansi-term opens a new *ansi-term*<xx> window,
  82. ;; where <xx> is, as usual, a number...
  83. ;;
  84. ;; ----------------------------------------
  85. ;;
  86. ;; With the term-buffer-maximum-size you can finally decide how many
  87. ;; scrollback lines to keep: its default is 2048 but you can change it as
  88. ;; usual.
  89. ;;
  90. ;; ----------------------------------------
  91. ;;
  92. ;;
  93. ;; ANSI colorization should work well, I've decided to limit the interpreter
  94. ;; to five outstanding commands (like ESC [ 01;04;32;41;07m.
  95. ;; You shouldn't need more, if you do, tell me and I'll increase it. It's
  96. ;; so easy you could do it yourself...
  97. ;;
  98. ;; Blink, is not supported. Currently it's mapped as bold.
  99. ;;
  100. ;; Important caveat:
  101. ;; -----------------
  102. ;; if you want custom colors in term.el redefine term-default-fg-color
  103. ;; and term-default-bg-color BEFORE loading it.
  104. ;;
  105. ;; ----------------------------------------
  106. ;;
  107. ;; If you'd like to check out my complete configuration, you can download
  108. ;; it from http://www.polito.it/~s64912/things.html, it's ~500k in size and
  109. ;; contains my .cshrc, .emacs and my whole site-lisp subdirectory. (notice
  110. ;; that this term.el may be newer/older than the one in there, please
  111. ;; check!)
  112. ;;
  113. ;; This complete configuration contains, among other things, a complete
  114. ;; rectangular marking solution (based on rect-mark.el and
  115. ;; pc-bindings.el) and should be a good example of how extensively Emacs
  116. ;; can be configured on a ppp-connected ws.
  117. ;;
  118. ;; ----------------------------------------
  119. ;;
  120. ;; TODO:
  121. ;;
  122. ;; - Add hooks to allow raw-mode keys to be configurable
  123. ;; - Which keys are better ? \eOA or \e[A ?
  124. ;;
  125. ;;
  126. ;; Changes:
  127. ;;
  128. ;; V4.0 January 1997
  129. ;;
  130. ;; - Huge reworking of the faces code: now we only have roughly 20-30
  131. ;; faces for everything so we're even faster than the old md-term.el !
  132. ;; - Finished removing all the J-Shell code.
  133. ;;
  134. ;; V3.0 January 1997
  135. ;;
  136. ;; - Now all the supportable ANSI commands work well.
  137. ;; - Reworked a little the code: much less jsh-inspired stuff
  138. ;;
  139. ;; V2.3 November
  140. ;;
  141. ;; - Now all the faces are accessed through an array: much cleaner code.
  142. ;;
  143. ;; V2.2 November 4 1996
  144. ;;
  145. ;; - Implemented ANSI output colorization ( a bit rough but enough for
  146. ;; color_ls )
  147. ;;
  148. ;; - Implemented a maximum limit for the scroll buffer (stolen from
  149. ;; comint.el)
  150. ;;
  151. ;; v2.1 October 28 1996, first public release
  152. ;;
  153. ;; - Some new keybindings for term-char mode ( notably home/end/...)
  154. ;; - Directory, hostname and username tracking via ange-ftp
  155. ;; - Multi-term capability via the ansi-term call
  156. ;;
  157. ;; ----------------------------------------------------------------
  158. ;; You should/could have something like this in your .emacs to take
  159. ;; full advantage of this package
  160. ;;
  161. ;; (add-hook 'term-mode-hook
  162. ;; (function
  163. ;; (lambda ()
  164. ;; (setq term-prompt-regexp "^[^#$%>\n]*[#$%>] *")
  165. ;; (make-local-variable 'mouse-yank-at-point)
  166. ;; (make-local-variable 'transient-mark-mode)
  167. ;; (setq mouse-yank-at-point t)
  168. ;; (setq transient-mark-mode nil)
  169. ;; (auto-fill-mode -1)
  170. ;; (setq tab-width 8 ))))
  171. ;;
  172. ;;
  173. ;; ----------------------------------------
  174. ;;
  175. ;; If you want to use color ls the best setup is to have a different file
  176. ;; when you use eterm ( see above, mine is named .emacs_dircolors ). This
  177. ;; is necessary because some terminals, rxvt for example, need non-ansi
  178. ;; hacks to work ( for example on my rxvt white is wired to fg, and to
  179. ;; obtain normal white I have to do bold-white :)
  180. ;;
  181. ;; ----------------------------------------
  182. ;;
  183. ;;
  184. ;; # Configuration file for the color ls utility
  185. ;; # This file goes in the /etc directory, and must be world readable.
  186. ;; # You can copy this file to .dir_colors in your $HOME directory to
  187. ;; # override the system defaults.
  188. ;;
  189. ;; # COLOR needs one of these arguments: 'tty' colorizes output to ttys, but
  190. ;; # not pipes. 'all' adds color characters to all output. 'none' shuts
  191. ;; # colorization off.
  192. ;; COLOR tty
  193. ;; OPTIONS -F
  194. ;;
  195. ;; # Below, there should be one TERM entry for each termtype that is
  196. ;; # colorizable
  197. ;; TERM eterm
  198. ;;
  199. ;; # EIGHTBIT, followed by '1' for on, '0' for off. (8-bit output)
  200. ;; EIGHTBIT 1
  201. ;;
  202. ;; # Below are the color init strings for the basic file types. A color init
  203. ;; # string consists of one or more of the following numeric codes:
  204. ;; # Attribute codes:
  205. ;; # 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
  206. ;; # Text color codes:
  207. ;; # 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
  208. ;; # Background color codes:
  209. ;; # 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
  210. ;; NORMAL 00 # global default, although everything should be something.
  211. ;; FILE 00 # normal file
  212. ;; DIR 00;37 # directory
  213. ;; LINK 00;36 # symbolic link
  214. ;; FIFO 00;37 # pipe
  215. ;; SOCK 40;35 # socket
  216. ;; BLK 33;01 # block device driver
  217. ;; CHR 33;01 # character device driver
  218. ;;
  219. ;; # This is for files with execute permission:
  220. ;; EXEC 00;32
  221. ;;
  222. ;; # List any file extensions like '.gz' or '.tar' that you would like ls
  223. ;; # to colorize below. Put the extension, a space, and the color init
  224. ;; # string. (and any comments you want to add after a '#')
  225. ;; .tar 01;33 # archives or compressed
  226. ;; .tgz 01;33
  227. ;; .arj 01;33
  228. ;; .taz 01;33
  229. ;; .lzh 01;33
  230. ;; .zip 01;33
  231. ;; .z 01;33
  232. ;; .Z 01;33
  233. ;; .gz 01;33
  234. ;; .jpg 01;35 # image formats
  235. ;; .gif 01;35
  236. ;; .bmp 01;35
  237. ;; .xbm 01;35
  238. ;; .xpm 01;35
  239. ;;
  240. ;;
  241. ;; ----------------------------------------
  242. ;;
  243. ;; Notice: for directory/host/user tracking you need to have something
  244. ;; like this in your shell startup script ( this is for tcsh but should
  245. ;; be quite easy to port to other shells )
  246. ;;
  247. ;; ----------------------------------------
  248. ;;
  249. ;;
  250. ;; set os = `uname`
  251. ;; set host = `hostname`
  252. ;; set date = `date`
  253. ;;
  254. ;; # su does not change this but I'd like it to
  255. ;;
  256. ;; set user = `whoami`
  257. ;;
  258. ;; # ...
  259. ;;
  260. ;; if ( eterm =~ $TERM ) then
  261. ;;
  262. ;; echo --------------------------------------------------------------
  263. ;; echo Hello $user
  264. ;; echo Today is $date
  265. ;; echo We are on $host running $os under Emacs term mode
  266. ;; echo --------------------------------------------------------------
  267. ;;
  268. ;; setenv EDITOR emacsclient
  269. ;;
  270. ;; # Notice: $host and $user have been set before to 'hostname' and 'whoami'
  271. ;; # this is necessary because, f.e., certain versions of 'su' do not change
  272. ;; # $user, YMMV: if you don't want to fiddle with them define a couple
  273. ;; # of new variables and use these instead.
  274. ;; # NOTICE that there is a space between "AnSiT?" and $whatever NOTICE
  275. ;;
  276. ;; # These are because we want the real cwd in the messages, not the login
  277. ;; # time one !
  278. ;;
  279. ;; set cwd_hack='$cwd'
  280. ;; set host_hack='$host'
  281. ;; set user_hack='$user'
  282. ;;
  283. ;; # Notice that the ^[ character is an ESC, not two chars. You can
  284. ;; # get it in various ways, for example by typing
  285. ;; # echo -e '\033' > escape.file
  286. ;; # or by using your favorite editor
  287. ;;
  288. ;; foreach temp (cd pushd)
  289. ;; alias $temp "$temp \!* ; echo 'AnSiTc' $cwd_hack"
  290. ;; end
  291. ;; alias popd 'popd ;echo "AnSiTc" $cwd'
  292. ;;
  293. ;; # Every command that can modify the user/host/directory should be aliased
  294. ;; # as follows for the tracking mechanism to work.
  295. ;;
  296. ;; foreach temp ( rlogin telnet rsh sh ksh csh tcsh zsh bash tcl su )
  297. ;; alias $temp "$temp \!* ; echo 'AnSiTh' $host_hack ; \
  298. ;; echo 'AnSiTu' $user_hack ;echo 'AnSiTc' $cwd_hack"
  299. ;; end
  300. ;;
  301. ;; # Start up & use color ls
  302. ;;
  303. ;; echo "AnSiTh" $host
  304. ;; echo "AnSiTu" $user
  305. ;; echo "AnSiTc" $cwd
  306. ;;
  307. ;; # some housekeeping
  308. ;;
  309. ;; unset cwd_hack
  310. ;; unset host_hack
  311. ;; unset user_hack
  312. ;; unset temp
  313. ;;
  314. ;; eval `/bin/dircolors /home/marco/.emacs_dircolors`
  315. ;; endif
  316. ;;
  317. ;; # ...
  318. ;;
  319. ;; # Let's not clutter user space
  320. ;;
  321. ;; unset os
  322. ;; unset date
  323. ;;
  324. ;;
  325. ;;; Original Commentary:
  326. ;; ---------------------
  327. ;; The changelog is at the end of this file.
  328. ;; Please send me bug reports, bug fixes, and extensions, so that I can
  329. ;; merge them into the master source.
  330. ;; - Per Bothner (bothner@cygnus.com)
  331. ;; This file defines a general command-interpreter-in-a-buffer package
  332. ;; (term mode). The idea is that you can build specific process-in-a-buffer
  333. ;; modes on top of term mode -- e.g., lisp, shell, scheme, T, soar, ....
  334. ;; This way, all these specific packages share a common base functionality,
  335. ;; and a common set of bindings, which makes them easier to use (and
  336. ;; saves code, implementation time, etc., etc.).
  337. ;; For hints on converting existing process modes (e.g., tex-mode,
  338. ;; background, dbx, gdb, kermit, prolog, telnet) to use term-mode
  339. ;; instead of shell-mode, see the notes at the end of this file.
  340. ;; Brief Command Documentation:
  341. ;;============================================================================
  342. ;; Term Mode Commands: (common to all derived modes, like cmushell & cmulisp
  343. ;; mode)
  344. ;;
  345. ;; m-p term-previous-input Cycle backwards in input history
  346. ;; m-n term-next-input Cycle forwards
  347. ;; m-r term-previous-matching-input Previous input matching a regexp
  348. ;; m-s comint-next-matching-input Next input that matches
  349. ;; return term-send-input
  350. ;; c-c c-a term-bol Beginning of line; skip prompt.
  351. ;; c-d term-delchar-or-maybe-eof Delete char unless at end of buff.
  352. ;; c-c c-u term-kill-input ^u
  353. ;; c-c c-w backward-kill-word ^w
  354. ;; c-c c-c term-interrupt-subjob ^c
  355. ;; c-c c-z term-stop-subjob ^z
  356. ;; c-c c-\ term-quit-subjob ^\
  357. ;; c-c c-o term-kill-output Delete last batch of process output
  358. ;; c-c c-r term-show-output Show last batch of process output
  359. ;; c-c c-h term-dynamic-list-input-ring List input history
  360. ;;
  361. ;; Not bound by default in term-mode
  362. ;; term-send-invisible Read a line w/o echo, and send to proc
  363. ;; (These are bound in shell-mode)
  364. ;; term-dynamic-complete Complete filename at point.
  365. ;; term-dynamic-list-completions List completions in help buffer.
  366. ;; term-replace-by-expanded-filename Expand and complete filename at point;
  367. ;; replace with expanded/completed name.
  368. ;; term-kill-subjob No mercy.
  369. ;; term-show-maximum-output Show as much output as possible.
  370. ;; term-continue-subjob Send CONT signal to buffer's process
  371. ;; group. Useful if you accidentally
  372. ;; suspend your process (with C-c C-z).
  373. ;; term-mode-hook is the term mode hook. Basically for your keybindings.
  374. ;; term-load-hook is run after loading in this package.
  375. ;;; Code:
  376. ;; This is passed to the inferior in the EMACS environment variable,
  377. ;; so it is important to increase it if there are protocol-relevant changes.
  378. (defconst term-protocol-version "0.96")
  379. (eval-when-compile
  380. (require 'ange-ftp)
  381. (require 'cl))
  382. (require 'ring)
  383. (require 'ehelp)
  384. (defgroup term nil
  385. "General command interpreter in a window."
  386. :group 'processes)
  387. ;;; Buffer Local Variables:
  388. ;;============================================================================
  389. ;; Term mode buffer local variables:
  390. ;; term-prompt-regexp - string term-bol uses to match prompt.
  391. ;; term-delimiter-argument-list - list For delimiters and arguments
  392. ;; term-last-input-start - marker Handy if inferior always echoes
  393. ;; term-last-input-end - marker For term-kill-output command
  394. ;; For the input history mechanism:
  395. (defvar term-input-ring-size 32 "Size of input history ring.")
  396. ;; term-input-ring-size - integer
  397. ;; term-input-ring - ring
  398. ;; term-input-ring-index - number ...
  399. ;; term-input-autoexpand - symbol ...
  400. ;; term-input-ignoredups - boolean ...
  401. ;; term-last-input-match - string ...
  402. ;; term-dynamic-complete-functions - hook For the completion mechanism
  403. ;; term-completion-fignore - list ...
  404. ;; term-get-old-input - function Hooks for specific
  405. ;; term-input-filter-functions - hook process-in-a-buffer
  406. ;; term-input-filter - function modes.
  407. ;; term-input-send - function
  408. ;; term-scroll-to-bottom-on-output - symbol ...
  409. ;; term-scroll-show-maximum-output - boolean...
  410. (defvar term-height) ; Number of lines in window.
  411. (defvar term-width) ; Number of columns in window.
  412. (defvar term-home-marker) ; Marks the "home" position for cursor addressing.
  413. (defvar term-saved-home-marker nil
  414. "When using alternate sub-buffer,
  415. contains saved term-home-marker from original sub-buffer.")
  416. (defvar term-start-line-column 0
  417. "(current-column) at start of screen line, or nil if unknown.")
  418. (defvar term-current-column 0 "If non-nil, is cache for (current-column).")
  419. (defvar term-current-row 0
  420. "Current vertical row (relative to home-marker) or nil if unknown.")
  421. (defvar term-insert-mode nil)
  422. (defvar term-vertical-motion)
  423. (defvar term-terminal-state 0
  424. "State of the terminal emulator:
  425. state 0: Normal state
  426. state 1: Last character was a graphic in the last column.
  427. If next char is graphic, first move one column right
  428. \(and line warp) before displaying it.
  429. This emulates (more or less) the behavior of xterm.
  430. state 2: seen ESC
  431. state 3: seen ESC [ (or ESC [ ?)
  432. state 4: term-terminal-parameter contains pending output.")
  433. (defvar term-kill-echo-list nil
  434. "A queue of strings whose echo we want suppressed.")
  435. (defvar term-terminal-parameter)
  436. (defvar term-terminal-previous-parameter)
  437. (defvar term-current-face 'default)
  438. (defvar term-scroll-start 0 "Top-most line (inclusive) of scrolling region.")
  439. (defvar term-scroll-end) ; Number of line (zero-based) after scrolling region.
  440. (defvar term-pager-count nil
  441. "Number of lines before we need to page; if nil, paging is disabled.")
  442. (defvar term-saved-cursor nil)
  443. (defvar term-command-hook)
  444. (defvar term-log-buffer nil)
  445. (defvar term-scroll-with-delete nil
  446. "If t, forward scrolling should be implemented by delete to
  447. top-most line(s); and if nil, scrolling should be implemented
  448. by moving term-home-marker. It is set to t if there is a
  449. \(non-default) scroll-region OR the alternate buffer is used.")
  450. (defvar term-pending-delete-marker) ; New user input in line mode
  451. ; needs to be deleted, because it gets echoed by the inferior.
  452. ; To reduce flicker, we defer the delete until the next output.
  453. (defvar term-old-mode-map nil "Saves the old keymap when in char mode.")
  454. (defvar term-old-mode-line-format) ; Saves old mode-line-format while paging.
  455. (defvar term-pager-old-local-map nil "Saves old keymap while paging.")
  456. (defvar term-pager-old-filter) ; Saved process-filter while paging.
  457. (defcustom explicit-shell-file-name nil
  458. "If non-nil, is file name to use for explicitly requested inferior shell."
  459. :type '(choice (const nil) file)
  460. :group 'term)
  461. (defvar term-prompt-regexp "^"
  462. "Regexp to recognize prompts in the inferior process.
  463. Defaults to \"^\", the null string at BOL.
  464. Good choices:
  465. Canonical Lisp: \"^[^> \\n]*>+:? *\" (Lucid, franz, kcl, T, cscheme, oaklisp)
  466. Lucid Common Lisp: \"^\\\\(>\\\\|\\\\(->\\\\)+\\\\) *\"
  467. franz: \"^\\\\(->\\\\|<[0-9]*>:\\\\) *\"
  468. kcl: \"^>+ *\"
  469. shell: \"^[^#$%>\\n]*[#$%>] *\"
  470. T: \"^>+ *\"
  471. This is a good thing to set in mode hooks.")
  472. (defvar term-delimiter-argument-list ()
  473. "List of characters to recognize as separate arguments in input.
  474. Strings comprising a character in this list will separate the arguments
  475. surrounding them, and also be regarded as arguments in their own right
  476. \(unlike whitespace). See `term-arguments'.
  477. Defaults to the empty list.
  478. For shells, a good value is (?\\| ?& ?< ?> ?\\( ?\\) ?\\;).
  479. This is a good thing to set in mode hooks.")
  480. (defcustom term-input-autoexpand nil
  481. "If non-nil, expand input command history references on completion.
  482. This mirrors the optional behavior of tcsh (its autoexpand and histlit).
  483. If the value is `input', then the expansion is seen on input.
  484. If the value is `history', then the expansion is only when inserting
  485. into the buffer's input ring. See also `term-magic-space' and
  486. `term-dynamic-complete'.
  487. This variable is buffer-local."
  488. :type '(choice (const nil) (const t) (const input) (const history))
  489. :group 'term)
  490. (defcustom term-input-ignoredups nil
  491. "If non-nil, don't add input matching the last on the input ring.
  492. This mirrors the optional behavior of bash.
  493. This variable is buffer-local."
  494. :type 'boolean
  495. :group 'term)
  496. (defcustom term-input-ring-file-name nil
  497. "If non-nil, name of the file to read/write input history.
  498. See also `term-read-input-ring' and `term-write-input-ring'.
  499. This variable is buffer-local, and is a good thing to set in mode hooks."
  500. :type 'boolean
  501. :group 'term)
  502. (defcustom term-scroll-to-bottom-on-output nil
  503. "Controls whether interpreter output causes window to scroll.
  504. If nil, then do not scroll. If t or `all', scroll all windows showing buffer.
  505. If `this', scroll only the selected window.
  506. If `others', scroll only those that are not the selected window.
  507. The default is nil.
  508. See variable `term-scroll-show-maximum-output'.
  509. This variable is buffer-local."
  510. :type 'boolean
  511. :group 'term)
  512. (defcustom term-scroll-show-maximum-output nil
  513. "Controls how interpreter output causes window to scroll.
  514. If non-nil, then show the maximum output when the window is scrolled.
  515. See variable `term-scroll-to-bottom-on-output'.
  516. This variable is buffer-local."
  517. :type 'boolean
  518. :group 'term)
  519. ;; Where gud-display-frame should put the debugging arrow. This is
  520. ;; set by the marker-filter, which scans the debugger's output for
  521. ;; indications of the current pc.
  522. (defvar term-pending-frame nil)
  523. ;;; Here are the per-interpreter hooks.
  524. (defvar term-get-old-input (function term-get-old-input-default)
  525. "Function that submits old text in term mode.
  526. This function is called when return is typed while the point is in old text.
  527. It returns the text to be submitted as process input. The default is
  528. `term-get-old-input-default', which grabs the current line, and strips off
  529. leading text matching `term-prompt-regexp'.")
  530. (defvar term-dynamic-complete-functions
  531. '(term-replace-by-expanded-history term-dynamic-complete-filename)
  532. "List of functions called to perform completion.
  533. Functions should return non-nil if completion was performed.
  534. See also `term-dynamic-complete'.
  535. This is a good thing to set in mode hooks.")
  536. (defvar term-input-filter
  537. (function (lambda (str) (not (string-match "\\`\\s *\\'" str))))
  538. "Predicate for filtering additions to input history.
  539. Only inputs answering true to this function are saved on the input
  540. history list. Default is to save anything that isn't all whitespace.")
  541. (defvar term-input-filter-functions '()
  542. "Functions to call before input is sent to the process.
  543. These functions get one argument, a string containing the text to send.
  544. This variable is buffer-local.")
  545. (defvar term-input-sender (function term-simple-send)
  546. "Function to actually send to PROCESS the STRING submitted by user.
  547. Usually this is just `term-simple-send', but if your mode needs to
  548. massage the input string, this is your hook. This is called from
  549. the user command `term-send-input'. `term-simple-send' just sends
  550. the string plus a newline.")
  551. (defcustom term-eol-on-send t
  552. "Non-nil means go to the end of the line before sending input.
  553. See `term-send-input'."
  554. :type 'boolean
  555. :group 'term)
  556. (defcustom term-mode-hook '()
  557. "Called upon entry into term mode.
  558. This is run before the process is cranked up."
  559. :type 'hook
  560. :group 'term)
  561. (defcustom term-exec-hook '()
  562. "Called each time a process is exec'd by `term-exec'.
  563. This is called after the process is cranked up. It is useful for things that
  564. must be done each time a process is executed in a term mode buffer (e.g.,
  565. `process-kill-without-query'). In contrast, `term-mode-hook' is only
  566. executed once when the buffer is created."
  567. :type 'hook
  568. :group 'term)
  569. (defvar term-mode-map
  570. (let ((map (make-sparse-keymap)))
  571. (define-key map "\ep" 'term-previous-input)
  572. (define-key map "\en" 'term-next-input)
  573. (define-key map "\er" 'term-previous-matching-input)
  574. (define-key map "\es" 'term-next-matching-input)
  575. (unless (featurep 'xemacs)
  576. (define-key map [?\A-\M-r]
  577. 'term-previous-matching-input-from-input)
  578. (define-key map [?\A-\M-s] 'term-next-matching-input-from-input))
  579. (define-key map "\e\C-l" 'term-show-output)
  580. (define-key map "\C-m" 'term-send-input)
  581. (define-key map "\C-d" 'term-delchar-or-maybe-eof)
  582. (define-key map "\C-c\C-a" 'term-bol)
  583. (define-key map "\C-c\C-u" 'term-kill-input)
  584. (define-key map "\C-c\C-w" 'backward-kill-word)
  585. (define-key map "\C-c\C-c" 'term-interrupt-subjob)
  586. (define-key map "\C-c\C-z" 'term-stop-subjob)
  587. (define-key map "\C-c\C-\\" 'term-quit-subjob)
  588. (define-key map "\C-c\C-m" 'term-copy-old-input)
  589. (define-key map "\C-c\C-o" 'term-kill-output)
  590. (define-key map "\C-c\C-r" 'term-show-output)
  591. (define-key map "\C-c\C-e" 'term-show-maximum-output)
  592. (define-key map "\C-c\C-l" 'term-dynamic-list-input-ring)
  593. (define-key map "\C-c\C-n" 'term-next-prompt)
  594. (define-key map "\C-c\C-p" 'term-previous-prompt)
  595. (define-key map "\C-c\C-d" 'term-send-eof)
  596. (define-key map "\C-c\C-k" 'term-char-mode)
  597. (define-key map "\C-c\C-j" 'term-line-mode)
  598. (define-key map "\C-c\C-q" 'term-pager-toggle)
  599. ;; ;; completion:
  600. ;; (define-key map [menu-bar completion]
  601. ;; (cons "Complete" (make-sparse-keymap "Complete")))
  602. ;; (define-key map [menu-bar completion complete-expand]
  603. ;; '("Expand File Name" . term-replace-by-expanded-filename))
  604. ;; (define-key map [menu-bar completion complete-listing]
  605. ;; '("File Completion Listing" . term-dynamic-list-filename-completions))
  606. ;; (define-key map [menu-bar completion complete-file]
  607. ;; '("Complete File Name" . term-dynamic-complete-filename))
  608. ;; (define-key map [menu-bar completion complete]
  609. ;; '("Complete Before Point" . term-dynamic-complete))
  610. ;; ;; Put them in the menu bar:
  611. ;; (setq menu-bar-final-items (append '(terminal completion inout signals)
  612. ;; menu-bar-final-items))
  613. map))
  614. (defvar term-raw-map nil
  615. "Keyboard map for sending characters directly to the inferior process.")
  616. (defvar term-escape-char nil
  617. "Escape character for char sub-mode of term mode.
  618. Do not change it directly; use `term-set-escape-char' instead.")
  619. (defvar term-raw-escape-map nil)
  620. (defvar term-pager-break-map nil)
  621. (defvar term-ptyp t
  622. "True if communications via pty; false if by pipe. Buffer local.
  623. This is to work around a bug in Emacs process signaling.")
  624. (defvar term-last-input-match ""
  625. "Last string searched for by term input history search, for defaulting.
  626. Buffer local variable.")
  627. (defvar term-input-ring nil)
  628. (defvar term-last-input-start)
  629. (defvar term-last-input-end)
  630. (defvar term-input-ring-index nil
  631. "Index of last matched history element.")
  632. (defvar term-matching-input-from-input-string ""
  633. "Input previously used to match input history.")
  634. ; This argument to set-process-filter disables reading from the process,
  635. ; assuming this is Emacs 19.20 or newer.
  636. (defvar term-pager-filter t)
  637. (put 'term-replace-by-expanded-history 'menu-enable 'term-input-autoexpand)
  638. (put 'term-input-ring 'permanent-local t)
  639. (put 'term-input-ring-index 'permanent-local t)
  640. (put 'term-input-autoexpand 'permanent-local t)
  641. (put 'term-input-filter-functions 'permanent-local t)
  642. (put 'term-scroll-to-bottom-on-output 'permanent-local t)
  643. (put 'term-scroll-show-maximum-output 'permanent-local t)
  644. (put 'term-ptyp 'permanent-local t)
  645. (defmacro term-in-char-mode () '(eq (current-local-map) term-raw-map))
  646. (defmacro term-in-line-mode () '(not (term-in-char-mode)))
  647. ;; True if currently doing PAGER handling.
  648. (defmacro term-pager-enabled () 'term-pager-count)
  649. (defmacro term-handling-pager () 'term-pager-old-local-map)
  650. (defmacro term-using-alternate-sub-buffer () 'term-saved-home-marker)
  651. (defvar term-signals-menu)
  652. (defvar term-terminal-menu)
  653. ;; Let's silence the byte-compiler -mm
  654. (defvar term-ansi-at-host nil)
  655. (defvar term-ansi-at-dir nil)
  656. (defvar term-ansi-at-user nil)
  657. (defvar term-ansi-at-message nil)
  658. (defvar term-ansi-at-save-user nil)
  659. (defvar term-ansi-at-save-pwd nil)
  660. (defvar term-ansi-at-save-anon nil)
  661. (defvar term-ansi-current-bold nil)
  662. (defvar term-ansi-current-color 0)
  663. (defvar term-ansi-face-already-done nil)
  664. (defvar term-ansi-current-bg-color 0)
  665. (defvar term-ansi-current-underline nil)
  666. (defvar term-ansi-current-reverse nil)
  667. (defvar term-ansi-current-invisible nil)
  668. ;; Four should be enough, if you want more, just add. -mm
  669. (defvar term-terminal-more-parameters 0)
  670. (defvar term-terminal-previous-parameter-2 -1)
  671. (defvar term-terminal-previous-parameter-3 -1)
  672. (defvar term-terminal-previous-parameter-4 -1)
  673. ;;; faces -mm
  674. (defcustom term-default-fg-color
  675. ;; FIXME: This depends on the current frame, so depending on when
  676. ;; it's loaded, the result may be different.
  677. (face-foreground term-current-face)
  678. "Default color for foreground in `term'."
  679. :group 'term
  680. :type 'string)
  681. (defcustom term-default-bg-color
  682. ;; FIXME: This depends on the current frame, so depending on when
  683. ;; it's loaded, the result may be different.
  684. (face-background term-current-face)
  685. "Default color for background in `term'."
  686. :group 'term
  687. :type 'string)
  688. ;; Use the same colors that xterm uses, see `xterm-standard-colors'.
  689. (defvar ansi-term-color-vector
  690. [unspecified "black" "red3" "green3" "yellow3" "blue2"
  691. "magenta3" "cyan3" "white"])
  692. ;; Inspiration came from comint.el -mm
  693. (defcustom term-buffer-maximum-size 2048
  694. "The maximum size in lines for term buffers.
  695. Term buffers are truncated from the top to be no greater than this number.
  696. Notice that a setting of 0 means \"don't truncate anything\". This variable
  697. is buffer-local."
  698. :group 'term
  699. :type 'integer)
  700. (when (featurep 'xemacs)
  701. (defvar term-terminal-menu
  702. '("Terminal"
  703. [ "Character mode" term-char-mode (term-in-line-mode)]
  704. [ "Line mode" term-line-mode (term-in-char-mode)]
  705. [ "Enable paging" term-pager-toggle (not term-pager-count)]
  706. [ "Disable paging" term-pager-toggle term-pager-count])))
  707. ;; Menu bars:
  708. (unless (featurep 'xemacs)
  709. ;; terminal:
  710. (let (newmap)
  711. (setq newmap (make-sparse-keymap "Terminal"))
  712. (define-key newmap [terminal-pager-enable]
  713. '(menu-item "Enable paging" term-fake-pager-enable
  714. :help "Enable paging feature"))
  715. (define-key newmap [terminal-pager-disable]
  716. '(menu-item "Disable paging" term-fake-pager-disable
  717. :help "Disable paging feature"))
  718. (define-key newmap [terminal-char-mode]
  719. '(menu-item "Character mode" term-char-mode
  720. :help "Switch to char (raw) sub-mode of term mode"))
  721. (define-key newmap [terminal-line-mode]
  722. '(menu-item "Line mode" term-line-mode
  723. :help "Switch to line (cooked) sub-mode of term mode"))
  724. (setq term-terminal-menu (cons "Terminal" newmap))
  725. ;; completion: (line mode only)
  726. (defvar term-completion-menu (make-sparse-keymap "Complete"))
  727. (define-key term-mode-map [menu-bar completion]
  728. (cons "Complete" term-completion-menu))
  729. (define-key term-completion-menu [complete-expand]
  730. '("Expand File Name" . term-replace-by-expanded-filename))
  731. (define-key term-completion-menu [complete-listing]
  732. '("File Completion Listing" . term-dynamic-list-filename-completions))
  733. (define-key term-completion-menu [menu-bar completion complete-file]
  734. '("Complete File Name" . term-dynamic-complete-filename))
  735. (define-key term-completion-menu [menu-bar completion complete]
  736. '("Complete Before Point" . term-dynamic-complete))
  737. ;; Input history: (line mode only)
  738. (defvar term-inout-menu (make-sparse-keymap "In/Out"))
  739. (define-key term-mode-map [menu-bar inout]
  740. (cons "In/Out" term-inout-menu))
  741. (define-key term-inout-menu [kill-output]
  742. '("Kill Current Output Group" . term-kill-output))
  743. (define-key term-inout-menu [next-prompt]
  744. '("Forward Output Group" . term-next-prompt))
  745. (define-key term-inout-menu [previous-prompt]
  746. '("Backward Output Group" . term-previous-prompt))
  747. (define-key term-inout-menu [show-maximum-output]
  748. '("Show Maximum Output" . term-show-maximum-output))
  749. (define-key term-inout-menu [show-output]
  750. '("Show Current Output Group" . term-show-output))
  751. (define-key term-inout-menu [kill-input]
  752. '("Kill Current Input" . term-kill-input))
  753. (define-key term-inout-menu [copy-input]
  754. '("Copy Old Input" . term-copy-old-input))
  755. (define-key term-inout-menu [forward-matching-history]
  756. '("Forward Matching Input..." . term-forward-matching-input))
  757. (define-key term-inout-menu [backward-matching-history]
  758. '("Backward Matching Input..." . term-backward-matching-input))
  759. (define-key term-inout-menu [next-matching-history]
  760. '("Next Matching Input..." . term-next-matching-input))
  761. (define-key term-inout-menu [previous-matching-history]
  762. '("Previous Matching Input..." . term-previous-matching-input))
  763. (define-key term-inout-menu [next-matching-history-from-input]
  764. '("Next Matching Current Input" . term-next-matching-input-from-input))
  765. (define-key term-inout-menu [previous-matching-history-from-input]
  766. '("Previous Matching Current Input" .
  767. term-previous-matching-input-from-input))
  768. (define-key term-inout-menu [next-history]
  769. '("Next Input" . term-next-input))
  770. (define-key term-inout-menu [previous-history]
  771. '("Previous Input" . term-previous-input))
  772. (define-key term-inout-menu [list-history]
  773. '("List Input History" . term-dynamic-list-input-ring))
  774. (define-key term-inout-menu [expand-history]
  775. '("Expand History Before Point" . term-replace-by-expanded-history))
  776. ;; Signals
  777. (setq newmap (make-sparse-keymap "Signals"))
  778. (define-key term-mode-map [menu-bar signals]
  779. (setq term-signals-menu (cons "Signals" newmap)))
  780. (define-key newmap [eof]
  781. '(menu-item "EOF" term-send-eof
  782. :help "Send an EOF to the current buffer's process"))
  783. (define-key newmap [kill]
  784. '(menu-item "KILL" term-kill-subjob
  785. :help "Send kill signal to the current subjob"))
  786. (define-key newmap [quit]
  787. '(menu-item "QUIT" term-quit-subjob
  788. :help "Send quit signal to the current subjob."))
  789. (define-key newmap [cont]
  790. '(menu-item "CONT" term-continue-subjob
  791. :help "Send CONT signal to process buffer's process group"))
  792. (define-key newmap [stop]
  793. '(menu-item "STOP" term-stop-subjob
  794. :help "Stop the current subjob"))
  795. (define-key newmap [brk]
  796. '(menu-item "BREAK" term-interrupt-subjob
  797. :help "Interrupt the current subjob"))
  798. ))
  799. ;; Set up term-raw-map, etc.
  800. (defun term-set-escape-char (c)
  801. "Change `term-escape-char' and keymaps that depend on it."
  802. (when term-escape-char
  803. (define-key term-raw-map term-escape-char 'term-send-raw))
  804. (setq c (make-string 1 c))
  805. (define-key term-raw-map c term-raw-escape-map)
  806. ;; Define standard bindings in term-raw-escape-map
  807. (define-key term-raw-escape-map "\C-v"
  808. (lookup-key (current-global-map) "\C-v"))
  809. (define-key term-raw-escape-map "\C-u"
  810. (lookup-key (current-global-map) "\C-u"))
  811. (define-key term-raw-escape-map c 'term-send-raw)
  812. (define-key term-raw-escape-map "\C-q" 'term-pager-toggle)
  813. ;; The keybinding for term-char-mode is needed by the menubar code.
  814. (define-key term-raw-escape-map "\C-k" 'term-char-mode)
  815. (define-key term-raw-escape-map "\C-j" 'term-line-mode)
  816. ;; It's convenient to have execute-extended-command here.
  817. (define-key term-raw-escape-map [?\M-x] 'execute-extended-command))
  818. (let* ((map (make-keymap))
  819. (esc-map (make-keymap))
  820. (i 0))
  821. (while (< i 128)
  822. (define-key map (make-string 1 i) 'term-send-raw)
  823. ;; Avoid O and [. They are used in escape sequences for various keys.
  824. (unless (or (eq i ?O) (eq i 91))
  825. (define-key esc-map (make-string 1 i) 'term-send-raw-meta))
  826. (setq i (1+ i)))
  827. (define-key map [remap self-insert-command] 'term-send-raw)
  828. (define-key map "\e" esc-map)
  829. (setq term-raw-map map)
  830. (setq term-raw-escape-map (copy-keymap 'Control-X-prefix))
  831. ;; Added nearly all the 'gray keys' -mm
  832. (if (featurep 'xemacs)
  833. (define-key term-raw-map [button2] 'term-mouse-paste)
  834. (define-key term-raw-map [mouse-2] 'term-mouse-paste)
  835. (define-key term-raw-map [menu-bar terminal] term-terminal-menu)
  836. (define-key term-raw-map [menu-bar signals] term-signals-menu))
  837. (define-key term-raw-map [up] 'term-send-up)
  838. (define-key term-raw-map [down] 'term-send-down)
  839. (define-key term-raw-map [right] 'term-send-right)
  840. (define-key term-raw-map [left] 'term-send-left)
  841. (define-key term-raw-map [delete] 'term-send-del)
  842. (define-key term-raw-map [deletechar] 'term-send-del)
  843. (define-key term-raw-map [backspace] 'term-send-backspace)
  844. (define-key term-raw-map [home] 'term-send-home)
  845. (define-key term-raw-map [end] 'term-send-end)
  846. (define-key term-raw-map [insert] 'term-send-insert)
  847. (define-key term-raw-map [S-prior] 'scroll-down)
  848. (define-key term-raw-map [S-next] 'scroll-up)
  849. (define-key term-raw-map [S-insert] 'term-paste)
  850. (define-key term-raw-map [prior] 'term-send-prior)
  851. (define-key term-raw-map [next] 'term-send-next))
  852. (term-set-escape-char ?\C-c)
  853. (defvar overflow-newline-into-fringe)
  854. (defun term-window-width ()
  855. (if (featurep 'xemacs)
  856. (1- (window-width))
  857. (if (and window-system overflow-newline-into-fringe)
  858. (window-width)
  859. (1- (window-width)))))
  860. (put 'term-mode 'mode-class 'special)
  861. ;; Use this variable as a display table for `term-mode'.
  862. (defvar term-display-table
  863. (let ((dt (or (copy-sequence standard-display-table)
  864. (make-display-table)))
  865. i)
  866. ;; avoid changing the display table for ^J
  867. (setq i 0)
  868. (while (< i 10)
  869. (aset dt i (vector i))
  870. (setq i (1+ i)))
  871. (setq i 11)
  872. (while (< i 32)
  873. (aset dt i (vector i))
  874. (setq i (1+ i)))
  875. (setq i 128)
  876. (while (< i 256)
  877. (aset dt i (vector i))
  878. (setq i (1+ i)))
  879. dt))
  880. (defun term-ansi-reset ()
  881. (setq term-current-face (nconc
  882. (if term-default-bg-color
  883. (list :background term-default-bg-color))
  884. (if term-default-fg-color
  885. (list :foreground term-default-fg-color))))
  886. (setq term-ansi-current-underline nil)
  887. (setq term-ansi-current-bold nil)
  888. (setq term-ansi-current-reverse nil)
  889. (setq term-ansi-current-color 0)
  890. (setq term-ansi-current-invisible nil)
  891. (setq term-ansi-face-already-done t)
  892. (setq term-ansi-current-bg-color 0))
  893. (defun term-mode ()
  894. "Major mode for interacting with an inferior interpreter.
  895. The interpreter name is same as buffer name, sans the asterisks.
  896. There are two submodes: line mode and char mode. By default, you are
  897. in char mode. In char sub-mode, each character (except
  898. `term-escape-char') is sent immediately to the subprocess.
  899. The escape character is equivalent to the usual meaning of C-x.
  900. In line mode, you send a line of input at a time; use
  901. \\[term-send-input] to send.
  902. In line mode, this maintains an input history of size
  903. `term-input-ring-size', and you can access it with the commands
  904. \\[term-next-input], \\[term-previous-input], and
  905. \\[term-dynamic-list-input-ring]. Input ring history expansion can be
  906. achieved with the commands \\[term-replace-by-expanded-history] or
  907. \\[term-magic-space]. Input ring expansion is controlled by the
  908. variable `term-input-autoexpand', and addition is controlled by the
  909. variable `term-input-ignoredups'.
  910. Input to, and output from, the subprocess can cause the window to scroll to
  911. the end of the buffer. See variables `term-scroll-to-bottom-on-input',
  912. and `term-scroll-to-bottom-on-output'.
  913. If you accidentally suspend your process, use \\[term-continue-subjob]
  914. to continue it.
  915. This mode can be customized to create specific modes for running
  916. particular subprocesses. This can be done by setting the hooks
  917. `term-input-filter-functions', `term-input-filter',
  918. `term-input-sender' and `term-get-old-input' to appropriate functions,
  919. and the variable `term-prompt-regexp' to the appropriate regular
  920. expression.
  921. Commands in raw mode:
  922. \\{term-raw-map}
  923. Commands in line mode:
  924. \\{term-mode-map}
  925. Entry to this mode runs the hooks on `term-mode-hook'."
  926. (interactive)
  927. ;; Do not remove this. All major modes must do this.
  928. (kill-all-local-variables)
  929. (setq major-mode 'term-mode)
  930. (setq mode-name "Term")
  931. (use-local-map term-mode-map)
  932. ;; we do not want indent to sneak in any tabs
  933. (setq indent-tabs-mode nil)
  934. (setq buffer-display-table term-display-table)
  935. (make-local-variable 'term-home-marker)
  936. (setq term-home-marker (copy-marker 0))
  937. (make-local-variable 'term-saved-home-marker)
  938. (make-local-variable 'term-height)
  939. (make-local-variable 'term-width)
  940. (setq term-width (term-window-width))
  941. (setq term-height (1- (window-height)))
  942. (make-local-variable 'term-terminal-parameter)
  943. (make-local-variable 'term-saved-cursor)
  944. (make-local-variable 'term-last-input-start)
  945. (setq term-last-input-start (make-marker))
  946. (make-local-variable 'term-last-input-end)
  947. (setq term-last-input-end (make-marker))
  948. (make-local-variable 'term-last-input-match)
  949. (setq term-last-input-match "")
  950. (make-local-variable 'term-prompt-regexp) ; Don't set; default
  951. (make-local-variable 'term-input-ring-size) ; ...to global val.
  952. (make-local-variable 'term-input-ring)
  953. (make-local-variable 'term-input-ring-file-name)
  954. (or (and (boundp 'term-input-ring) term-input-ring)
  955. (setq term-input-ring (make-ring term-input-ring-size)))
  956. (make-local-variable 'term-input-ring-index)
  957. (or (and (boundp 'term-input-ring-index) term-input-ring-index)
  958. (setq term-input-ring-index nil))
  959. (make-local-variable 'term-command-hook)
  960. (setq term-command-hook (symbol-function 'term-command-hook))
  961. ;; I'm not sure these saves are necessary but, since I
  962. ;; haven't tested the whole thing on a net connected machine with
  963. ;; a properly configured ange-ftp, I've decided to be conservative
  964. ;; and put them in. -mm
  965. (make-local-variable 'term-ansi-at-host)
  966. (setq term-ansi-at-host (system-name))
  967. (make-local-variable 'term-ansi-at-dir)
  968. (setq term-ansi-at-dir default-directory)
  969. (make-local-variable 'term-ansi-at-message)
  970. (setq term-ansi-at-message nil)
  971. ;; For user tracking purposes -mm
  972. (make-local-variable 'ange-ftp-default-user)
  973. (make-local-variable 'ange-ftp-default-password)
  974. (make-local-variable 'ange-ftp-generate-anonymous-password)
  975. ;; You may want to have different scroll-back sizes -mm
  976. (make-local-variable 'term-buffer-maximum-size)
  977. ;; Of course these have to be buffer-local -mm
  978. (make-local-variable 'term-ansi-current-bold)
  979. (make-local-variable 'term-ansi-current-color)
  980. (make-local-variable 'term-ansi-face-already-done)
  981. (make-local-variable 'term-ansi-current-bg-color)
  982. (make-local-variable 'term-ansi-current-underline)
  983. (make-local-variable 'term-ansi-current-reverse)
  984. (make-local-variable 'term-ansi-current-invisible)
  985. (make-local-variable 'term-terminal-parameter)
  986. (make-local-variable 'term-terminal-previous-parameter)
  987. (make-local-variable 'term-terminal-previous-parameter-2)
  988. (make-local-variable 'term-terminal-previous-parameter-3)
  989. (make-local-variable 'term-terminal-previous-parameter-4)
  990. (make-local-variable 'term-terminal-more-parameters)
  991. (make-local-variable 'term-terminal-state)
  992. (make-local-variable 'term-kill-echo-list)
  993. (make-local-variable 'term-start-line-column)
  994. (make-local-variable 'term-current-column)
  995. (make-local-variable 'term-current-row)
  996. (make-local-variable 'term-log-buffer)
  997. (make-local-variable 'term-scroll-start)
  998. (make-local-variable 'term-scroll-end)
  999. (setq term-scroll-end term-height)
  1000. (make-local-variable 'term-scroll-with-delete)
  1001. (make-local-variable 'term-pager-count)
  1002. (make-local-variable 'term-pager-old-local-map)
  1003. (make-local-variable 'term-old-mode-map)
  1004. (make-local-variable 'term-insert-mode)
  1005. (make-local-variable 'term-dynamic-complete-functions)
  1006. (make-local-variable 'term-completion-fignore)
  1007. (make-local-variable 'term-get-old-input)
  1008. (make-local-variable 'term-matching-input-from-input-string)
  1009. (make-local-variable 'term-input-autoexpand)
  1010. (make-local-variable 'term-input-ignoredups)
  1011. (make-local-variable 'term-delimiter-argument-list)
  1012. (make-local-variable 'term-input-filter-functions)
  1013. (make-local-variable 'term-input-filter)
  1014. (make-local-variable 'term-input-sender)
  1015. (make-local-variable 'term-eol-on-send)
  1016. (make-local-variable 'term-scroll-to-bottom-on-output)
  1017. (make-local-variable 'term-scroll-show-maximum-output)
  1018. (make-local-variable 'term-ptyp)
  1019. (make-local-variable 'term-exec-hook)
  1020. (make-local-variable 'term-vertical-motion)
  1021. (make-local-variable 'term-pending-delete-marker)
  1022. (setq term-pending-delete-marker (make-marker))
  1023. (make-local-variable 'term-current-face)
  1024. (term-ansi-reset)
  1025. (make-local-variable 'term-pending-frame)
  1026. (setq term-pending-frame nil)
  1027. ;; Cua-mode's keybindings interfere with the term keybindings, disable it.
  1028. (set (make-local-variable 'cua-mode) nil)
  1029. (run-mode-hooks 'term-mode-hook)
  1030. (when (featurep 'xemacs)
  1031. (set-buffer-menubar
  1032. (append current-menubar (list term-terminal-menu))))
  1033. (or term-input-ring
  1034. (setq term-input-ring (make-ring term-input-ring-size)))
  1035. (term-update-mode-line))
  1036. (defun term-reset-size (height width)
  1037. (setq term-height height)
  1038. (setq term-width width)
  1039. (setq term-start-line-column nil)
  1040. (setq term-current-row nil)
  1041. (setq term-current-column nil)
  1042. (term-set-scroll-region 0 height))
  1043. ;; Recursive routine used to check if any string in term-kill-echo-list
  1044. ;; matches part of the buffer before point.
  1045. ;; If so, delete that matched part of the buffer - this suppresses echo.
  1046. ;; Also, remove that string from the term-kill-echo-list.
  1047. ;; We *also* remove any older string on the list, as a sanity measure,
  1048. ;; in case something gets out of sync. (Except for type-ahead, there
  1049. ;; should only be one element in the list.)
  1050. (defun term-check-kill-echo-list ()
  1051. (let ((cur term-kill-echo-list) (found nil) (save-point (point)))
  1052. (unwind-protect
  1053. (progn
  1054. (end-of-line)
  1055. (while cur
  1056. (let* ((str (car cur)) (len (length str)) (start (- (point) len)))
  1057. (if (and (>= start (point-min))
  1058. (string= str (buffer-substring start (point))))
  1059. (progn (delete-char (- len))
  1060. (setq term-kill-echo-list (cdr cur))
  1061. (setq term-current-column nil)
  1062. (setq term-current-row nil)
  1063. (setq term-start-line-column nil)
  1064. (setq cur nil found t))
  1065. (setq cur (cdr cur))))))
  1066. (when (not found)
  1067. (goto-char save-point)))
  1068. found))
  1069. (defun term-check-size (process)
  1070. (when (or (/= term-height (1- (window-height)))
  1071. (/= term-width (term-window-width)))
  1072. (term-reset-size (1- (window-height)) (term-window-width))
  1073. (set-process-window-size process term-height term-width)))
  1074. (defun term-send-raw-string (chars)
  1075. (deactivate-mark)
  1076. (let ((proc (get-buffer-process (current-buffer))))
  1077. (if (not proc)
  1078. (error "Current buffer has no process")
  1079. ;; Note that (term-current-row) must be called *after*
  1080. ;; (point) has been updated to (process-mark proc).
  1081. (goto-char (process-mark proc))
  1082. (when (term-pager-enabled)
  1083. (setq term-pager-count (term-current-row)))
  1084. (process-send-string proc chars))))
  1085. (defun term-send-raw ()
  1086. "Send the last character typed through the terminal-emulator
  1087. without any interpretation."
  1088. (interactive)
  1089. (let ((keys (this-command-keys)))
  1090. (term-send-raw-string (string (aref keys (1- (length keys)))))))
  1091. (defun term-send-raw-meta ()
  1092. (interactive)
  1093. (let ((char last-input-event))
  1094. (when (symbolp last-input-event)
  1095. ;; Convert `return' to C-m, etc.
  1096. (let ((tmp (get char 'event-symbol-elements)))
  1097. (when tmp
  1098. (setq char (car tmp)))
  1099. (when (symbolp char)
  1100. (setq tmp (get char 'ascii-character))
  1101. (when tmp
  1102. (setq char tmp)))))
  1103. (setq char (event-basic-type char))
  1104. (term-send-raw-string (if (and (numberp char)
  1105. (> char 127)
  1106. (< char 256))
  1107. (make-string 1 char)
  1108. (format "\e%c" char)))))
  1109. (defun term-mouse-paste (click)
  1110. "Insert the primary selection at the position clicked on."
  1111. (interactive "e")
  1112. (if (featurep 'xemacs)
  1113. (term-send-raw-string
  1114. (or (condition-case () (x-get-selection) (error ()))
  1115. (error "No selection available")))
  1116. ;; Give temporary modes such as isearch a chance to turn off.
  1117. (run-hooks 'mouse-leave-buffer-hook)
  1118. (setq this-command 'yank)
  1119. (mouse-set-point click)
  1120. (term-send-raw-string
  1121. (or (cond ; From `mouse-yank-primary':
  1122. ((eq system-type 'windows-nt)
  1123. (or (x-get-selection 'PRIMARY)
  1124. (x-get-selection-value)))
  1125. ((fboundp 'x-get-selection-value)
  1126. (or (x-get-selection-value)
  1127. (x-get-selection 'PRIMARY)))
  1128. (t
  1129. (x-get-selection 'PRIMARY)))
  1130. (error "No selection is available")))))
  1131. (defun term-paste ()
  1132. "Insert the last stretch of killed text at point."
  1133. (interactive)
  1134. (term-send-raw-string (current-kill 0)))
  1135. ;; Which would be better: "\e[A" or "\eOA"? readline accepts either.
  1136. ;; For my configuration it's definitely better \eOA but YMMV. -mm
  1137. ;; For example: vi works with \eOA while elm wants \e[A ...
  1138. ;; (terminfo: kcuu1, kcud1, kcuf1, kcub1, khome, kend, kpp, knp, kdch1, kbs)
  1139. (defun term-send-up () (interactive) (term-send-raw-string "\eOA"))
  1140. (defun term-send-down () (interactive) (term-send-raw-string "\eOB"))
  1141. (defun term-send-right () (interactive) (term-send-raw-string "\eOC"))
  1142. (defun term-send-left () (interactive) (term-send-raw-string "\eOD"))
  1143. (defun term-send-home () (interactive) (term-send-raw-string "\e[1~"))
  1144. (defun term-send-insert() (interactive) (term-send-raw-string "\e[2~"))
  1145. (defun term-send-end () (interactive) (term-send-raw-string "\e[4~"))
  1146. (defun term-send-prior () (interactive) (term-send-raw-string "\e[5~"))
  1147. (defun term-send-next () (interactive) (term-send-raw-string "\e[6~"))
  1148. (defun term-send-del () (interactive) (term-send-raw-string "\e[3~"))
  1149. (defun term-send-backspace () (interactive) (term-send-raw-string "\C-?"))
  1150. (defun term-char-mode ()
  1151. "Switch to char (\"raw\") sub-mode of term mode.
  1152. Each character you type is sent directly to the inferior without
  1153. intervention from Emacs, except for the escape character (usually C-c)."
  1154. (interactive)
  1155. ;; FIXME: Emit message? Cfr ilisp-raw-message
  1156. (when (term-in-line-mode)
  1157. (setq term-old-mode-map (current-local-map))
  1158. (use-local-map term-raw-map)
  1159. ;; Send existing partial line to inferior (without newline).
  1160. (let ((pmark (process-mark (get-buffer-process (current-buffer))))
  1161. (save-input-sender term-input-sender))
  1162. (when (> (point) pmark)
  1163. (unwind-protect
  1164. (progn
  1165. (setq term-input-sender
  1166. (symbol-function 'term-send-string))
  1167. (end-of-line)
  1168. (term-send-input))
  1169. (setq term-input-sender save-input-sender))))
  1170. (term-update-mode-line)))
  1171. (defun term-line-mode ()
  1172. "Switch to line (\"cooked\") sub-mode of term mode.
  1173. This means that Emacs editing commands work as normally, until
  1174. you type \\[term-send-input] which sends the current line to the inferior."
  1175. (interactive)
  1176. (when (term-in-char-mode)
  1177. (use-local-map term-old-mode-map)
  1178. (term-update-mode-line)))
  1179. (defun term-update-mode-line ()
  1180. (let ((term-mode (if (term-in-char-mode) "char" "line"))
  1181. (term-page (when (term-pager-enabled) " page"))
  1182. (serial-item-speed)
  1183. (serial-item-config)
  1184. (proc (get-buffer-process (current-buffer))))
  1185. (when (and (term-check-proc (current-buffer))
  1186. (equal (process-type nil) 'serial))
  1187. (let ((temp (serial-speed)))
  1188. (setq serial-item-speed
  1189. `(:propertize
  1190. ,(or (and temp (format " %d" temp)) "")
  1191. help-echo "mouse-1: Change the speed of the serial port"
  1192. mouse-face mode-line-highlight
  1193. local-map (keymap (mode-line keymap
  1194. (down-mouse-1 . serial-mode-line-speed-menu-1))))))
  1195. (let ((temp (process-contact proc :summary)))
  1196. (setq serial-item-config
  1197. `(:propertize
  1198. ,(or (and temp (format " %s" temp)) "")
  1199. help-echo "mouse-1: Change the configuration of the serial port"
  1200. mouse-face mode-line-highlight
  1201. local-map (keymap (mode-line keymap
  1202. (down-mouse-1 . serial-mode-line-config-menu-1)))))))
  1203. (setq mode-line-process
  1204. (list ": " term-mode term-page
  1205. serial-item-speed
  1206. serial-item-config
  1207. " %s")))
  1208. (force-mode-line-update))
  1209. (defun term-check-proc (buffer)
  1210. "True if there is a process associated w/buffer BUFFER, and it
  1211. is alive. BUFFER can be either a buffer or the name of one."
  1212. (let ((proc (get-buffer-process buffer)))
  1213. (and proc (memq (process-status proc) '(run stop open listen connect)))))
  1214. ;;;###autoload
  1215. (defun make-term (name program &optional startfile &rest switches)
  1216. "Make a term process NAME in a buffer, running PROGRAM.
  1217. The name of the buffer is made by surrounding NAME with `*'s.
  1218. If there is already a running process in that buffer, it is not restarted.
  1219. Optional third arg STARTFILE is the name of a file to send the contents of to
  1220. the process. Any more args are arguments to PROGRAM."
  1221. (let ((buffer (get-buffer-create (concat "*" name "*"))))
  1222. ;; If no process, or nuked process, crank up a new one and put buffer in
  1223. ;; term mode. Otherwise, leave buffer and existing process alone.
  1224. (cond ((not (term-check-proc buffer))
  1225. (with-current-buffer buffer
  1226. (term-mode)) ; Install local vars, mode, keymap, ...
  1227. (term-exec buffer name program startfile switches)))
  1228. buffer))
  1229. ;;;###autoload
  1230. (defun term (program)
  1231. "Start a terminal-emulator in a new buffer.
  1232. The buffer is in Term mode; see `term-mode' for the
  1233. commands to use in that buffer.
  1234. \\<term-raw-map>Type \\[switch-to-buffer] to switch to another buffer."
  1235. (interactive (list (read-from-minibuffer "Run program: "
  1236. (or explicit-shell-file-name
  1237. (getenv "ESHELL")
  1238. (getenv "SHELL")
  1239. "/bin/sh"))))
  1240. (set-buffer (make-term "terminal" program))
  1241. (term-mode)
  1242. (term-char-mode)
  1243. (switch-to-buffer "*terminal*"))
  1244. (defun term-exec (buffer name command startfile switches)
  1245. "Start up a process in buffer for term modes.
  1246. Blasts any old process running in the buffer. Doesn't set the buffer mode.
  1247. You can use this to cheaply run a series of processes in the same term
  1248. buffer. The hook `term-exec-hook' is run after each exec."
  1249. (with-current-buffer buffer
  1250. (let ((proc (get-buffer-process buffer))) ; Blast any old process.
  1251. (when proc (delete-process proc)))
  1252. ;; Crank up a new process
  1253. (let ((proc (term-exec-1 name buffer command switches)))
  1254. (make-local-variable 'term-ptyp)
  1255. (setq term-ptyp process-connection-type) ; t if pty, nil if pipe.
  1256. ;; Jump to the end, and set the process mark.
  1257. (goto-char (point-max))
  1258. (set-marker (process-mark proc) (point))
  1259. (set-process-filter proc 'term-emulate-terminal)
  1260. (set-process-sentinel proc 'term-sentinel)
  1261. ;; Feed it the startfile.
  1262. (when startfile
  1263. ;;This is guaranteed to wait long enough
  1264. ;;but has bad results if the term does not prompt at all
  1265. ;; (while (= size (buffer-size))
  1266. ;; (sleep-for 1))
  1267. ;;I hope 1 second is enough!
  1268. (sleep-for 1)
  1269. (goto-char (point-max))
  1270. (insert-file-contents startfile)
  1271. (term-send-string
  1272. proc (delete-and-extract-region (point) (point-max)))))
  1273. (run-hooks 'term-exec-hook)
  1274. buffer))
  1275. (defun term-sentinel (proc msg)
  1276. "Sentinel for term buffers.
  1277. The main purpose is to get rid of the local keymap."
  1278. (let ((buffer (process-buffer proc)))
  1279. (when (memq (process-status proc) '(signal exit))
  1280. (if (null (buffer-name buffer))
  1281. ;; buffer killed
  1282. (set-process-buffer proc nil)
  1283. (with-current-buffer buffer
  1284. ;; Write something in the compilation buffer
  1285. ;; and hack its mode line.
  1286. ;; Get rid of local keymap.
  1287. (use-local-map nil)
  1288. (term-handle-exit (process-name proc) msg)
  1289. ;; Since the buffer and mode line will show that the
  1290. ;; process is dead, we can delete it now. Otherwise it
  1291. ;; will stay around until M-x list-processes.
  1292. (delete-process proc))))))
  1293. (defun term-handle-exit (process-name msg)
  1294. "Write process exit (or other change) message MSG in the current buffer."
  1295. (let ((buffer-read-only nil)
  1296. (omax (point-max))
  1297. (opoint (point)))
  1298. ;; Record where we put the message, so we can ignore it
  1299. ;; later on.
  1300. (goto-char omax)
  1301. (insert ?\n "Process " process-name " " msg)
  1302. ;; Force mode line redisplay soon.
  1303. (force-mode-line-update)
  1304. (when (and opoint (< opoint omax))
  1305. (goto-char opoint))))
  1306. (defvar term-term-name "eterm-color"
  1307. "Name to use for TERM.
  1308. Using \"emacs\" loses, because bash disables editing if $TERM == emacs.")
  1309. ;; Format string, usage:
  1310. ;; (format term-termcap-string emacs-term-name "TERMCAP=" 24 80)
  1311. (defvar term-termcap-format
  1312. "%s%s:li#%d:co#%d:cl=\\E[H\\E[J:cd=\\E[J:bs:am:xn:cm=\\E[%%i%%d;%%dH\
  1313. :nd=\\E[C:up=\\E[A:ce=\\E[K:ho=\\E[H:pt\
  1314. :al=\\E[L:dl=\\E[M:DL=\\E[%%dM:AL=\\E[%%dL:cs=\\E[%%i%%d;%%dr:sf=^J\
  1315. :dc=\\E[P:DC=\\E[%%dP:IC=\\E[%%d@:im=\\E[4h:ei=\\E[4l:mi:\
  1316. :so=\\E[7m:se=\\E[m:us=\\E[4m:ue=\\E[m:md=\\E[1m:mr=\\E[7m:me=\\E[m\
  1317. :UP=\\E[%%dA:DO=\\E[%%dB:LE=\\E[%%dD:RI=\\E[%%dC\
  1318. :kl=\\EOD:kd=\\EOB:kr=\\EOC:ku=\\EOA:kN=\\E[6~:kP=\\E[5~:@7=\\E[4~:kh=\\E[1~\
  1319. :mk=\\E[8m:cb=\\E[1K:op=\\E[39;49m:Co#8:pa#64:AB=\\E[4%%dm:AF=\\E[3%%dm:cr=^M\
  1320. :bl=^G:do=^J:le=^H:ta=^I:se=\\E[27m:ue=\\E24m\
  1321. :kb=^?:kD=^[[3~:sc=\\E7:rc=\\E8:r1=\\Ec:"
  1322. ;; : -undefine ic
  1323. ;; don't define :te=\\E[2J\\E[?47l\\E8:ti=\\E7\\E[?47h\
  1324. "Termcap capabilities supported.")
  1325. ;; This auxiliary function cranks up the process for term-exec in
  1326. ;; the appropriate environment.
  1327. (defun term-exec-1 (name buffer command switches)
  1328. ;; We need to do an extra (fork-less) exec to run stty.
  1329. ;; (This would not be needed if we had suitable Emacs primitives.)
  1330. ;; The 'if ...; then shift; fi' hack is because Bourne shell
  1331. ;; loses one arg when called with -c, and newer shells (bash, ksh) don't.
  1332. ;; Thus we add an extra dummy argument "..", and then remove it.
  1333. (let ((process-environment
  1334. (nconc
  1335. (list
  1336. (format "TERM=%s" term-term-name)
  1337. (format "TERMINFO=%s" data-directory)
  1338. (format term-termcap-format "TERMCAP="
  1339. term-term-name term-height term-width)
  1340. ;; We are going to get rid of the binding for EMACS,
  1341. ;; probably in Emacs 23, because it breaks
  1342. ;; `./configure' of some packages that expect it to
  1343. ;; say where to find EMACS.
  1344. (format "EMACS=%s (term:%s)" emacs-version term-protocol-version)
  1345. (format "INSIDE_EMACS=%s,term:%s" emacs-version term-protocol-version)
  1346. (format "LINES=%d" term-height)
  1347. (format "COLUMNS=%d" term-width))
  1348. process-environment))
  1349. (process-connection-type t)
  1350. ;; We should suppress conversion of end-of-line format.
  1351. (inhibit-eol-conversion t)
  1352. ;; The process's output contains not just chars but also binary
  1353. ;; escape codes, so we need to see the raw output. We will have to
  1354. ;; do the decoding by hand on the parts that are made of chars.
  1355. (coding-system-for-read 'binary))
  1356. (apply 'start-process name buffer
  1357. "/bin/sh" "-c"
  1358. (format "stty -nl echo rows %d columns %d sane 2>/dev/null;\
  1359. if [ $1 = .. ]; then shift; fi; exec \"$@\""
  1360. term-height term-width)
  1361. ".."
  1362. command switches)))
  1363. ;;; Input history processing in a buffer
  1364. ;; ===========================================================================
  1365. ;; Useful input history functions, courtesy of the Ergo group.
  1366. ;; Eleven commands:
  1367. ;; term-dynamic-list-input-ring List history in help buffer.
  1368. ;; term-previous-input Previous input...
  1369. ;; term-previous-matching-input ...matching a string.
  1370. ;; term-previous-matching-input-from-input ... matching the current input.
  1371. ;; term-next-input Next input...
  1372. ;; term-next-matching-input ...matching a string.
  1373. ;; term-next-matching-input-from-input ... matching the current input.
  1374. ;; term-backward-matching-input Backwards input...
  1375. ;; term-forward-matching-input ...matching a string.
  1376. ;; term-replace-by-expanded-history Expand history at point;
  1377. ;; replace with expanded history.
  1378. ;; term-magic-space Expand history and insert space.
  1379. ;;
  1380. ;; Three functions:
  1381. ;; term-read-input-ring Read into term-input-ring...
  1382. ;; term-write-input-ring Write to term-input-ring-file-name.
  1383. ;; term-replace-by-expanded-history-before-point Workhorse function.
  1384. (defun term-read-input-ring (&optional silent)
  1385. "Set the buffer's `term-input-ring' from a history file.
  1386. The name of the file is given by the variable `term-input-ring-file-name'.
  1387. The history ring is of size `term-input-ring-size', regardless of file size.
  1388. If `term-input-ring-file-name' is nil this function does nothing.
  1389. If the optional argument SILENT is non-nil, we say nothing about a
  1390. failure to read the history file.
  1391. This function is useful for major mode commands and mode hooks.
  1392. The structure of the history file should be one input command per line,
  1393. with the most recent command last.
  1394. See also `term-input-ignoredups' and `term-write-input-ring'."
  1395. (cond ((or (null term-input-ring-file-name)
  1396. (equal term-input-ring-file-name ""))
  1397. nil)
  1398. ((not (file-readable-p term-input-ring-file-name))
  1399. (or silent
  1400. (message "Cannot read history file %s"
  1401. term-input-ring-file-name)))
  1402. (t
  1403. (let ((file term-input-ring-file-name)
  1404. (count 0)
  1405. (ring (make-ring term-input-ring-size)))
  1406. (with-temp-buffer
  1407. (insert-file-contents file)
  1408. ;; Save restriction in case file is already visited...
  1409. ;; Watch for those date stamps in history files!
  1410. (goto-char (point-max))
  1411. (while (and (< count term-input-ring-size)
  1412. (re-search-backward "^[ \t]*\\([^#\n].*\\)[ \t]*$"
  1413. nil t))
  1414. (let ((history (buffer-substring (match-beginning 1)
  1415. (match-end 1))))
  1416. (when (or (null term-input-ignoredups)
  1417. (ring-empty-p ring)
  1418. (not (string-equal (ring-ref ring 0) history)))
  1419. (ring-insert-at-beginning ring history)))
  1420. (setq count (1+ count))))
  1421. (setq term-input-ring ring
  1422. term-input-ring-index nil)))))
  1423. (defun term-write-input-ring ()
  1424. "Write the buffer's `term-input-ring' to a history file.
  1425. The name of the file is given by the variable `term-input-ring-file-name'.
  1426. The original contents of the file are lost if `term-input-ring' is not empty.
  1427. If `term-input-ring-file-name' is nil this function does nothing.
  1428. Useful within process sentinels.
  1429. See also `term-read-input-ring'."
  1430. (cond ((or (null term-input-ring-file-name)
  1431. (equal term-input-ring-file-name "")
  1432. (null term-input-ring) (ring-empty-p term-input-ring))
  1433. nil)
  1434. ((not (file-writable-p term-input-ring-file-name))
  1435. (message "Cannot write history file %s" term-input-ring-file-name))
  1436. (t
  1437. (let* ((history-buf (get-buffer-create " *Temp Input History*"))
  1438. (ring term-input-ring)
  1439. (file term-input-ring-file-name)
  1440. (index (ring-length ring)))
  1441. ;; Write it all out into a buffer first. Much faster, but messier,
  1442. ;; than writing it one line at a time.
  1443. (with-current-buffer history-buf
  1444. (erase-buffer)
  1445. (while (> index 0)
  1446. (setq index (1- index))
  1447. (insert (ring-ref ring index) ?\n))
  1448. (write-region (buffer-string) nil file nil 'no-message)
  1449. (kill-buffer nil))))))
  1450. (defun term-dynamic-list-input-ring ()
  1451. "List in help buffer the buffer's input history."
  1452. (interactive)
  1453. (if (or (not (ring-p term-input-ring))
  1454. (ring-empty-p term-input-ring))
  1455. (message "No history")
  1456. (let ((history nil)
  1457. (history-buffer " *Input History*")
  1458. (index (1- (ring-length term-input-ring)))
  1459. (conf (current-window-configuration)))
  1460. ;; We have to build up a list ourselves from the ring vector.
  1461. (while (>= index 0)
  1462. (setq history (cons (ring-ref term-input-ring index) history)
  1463. index (1- index)))
  1464. ;; Change "completion" to "history reference"
  1465. ;; to make the display accurate.
  1466. (with-output-to-temp-buffer history-buffer
  1467. (display-completion-list history)
  1468. (set-buffer history-buffer)
  1469. (forward-line 3)
  1470. (while (search-backward "completion" nil 'move)
  1471. (replace-match "history reference")))
  1472. (sit-for 0)
  1473. (message "Hit space to flush")
  1474. (let ((ch (read-event)))
  1475. (if (eq ch ?\s)
  1476. (set-window-configuration conf)
  1477. (setq unread-command-events (list ch)))))))
  1478. (defun term-regexp-arg (prompt)
  1479. ;; Return list of regexp and prefix arg using PROMPT.
  1480. (let* (;; Don't clobber this.
  1481. (last-command last-command)
  1482. (regexp (read-from-minibuffer prompt nil nil nil
  1483. 'minibuffer-history-search-history)))
  1484. (list (if (string-equal regexp "")
  1485. (setcar minibuffer-history-search-history
  1486. (nth 1 minibuffer-history-search-history))
  1487. regexp)
  1488. (prefix-numeric-value current-prefix-arg))))
  1489. (defun term-search-arg (arg)
  1490. ;; First make sure there is a ring and that we are after the process mark
  1491. (cond ((not (term-after-pmark-p))
  1492. (error "Not at command line"))
  1493. ((or (null term-input-ring)
  1494. (ring-empty-p term-input-ring))
  1495. (error "Empty input ring"))
  1496. ((zerop arg)
  1497. ;; arg of zero resets search from beginning, and uses arg of 1
  1498. (setq term-input-ring-index nil)
  1499. 1)
  1500. (t
  1501. arg)))
  1502. (defun term-search-start (arg)
  1503. ;; Index to start a directional search, starting at term-input-ring-index
  1504. (if term-input-ring-index
  1505. ;; If a search is running, offset by 1 in direction of arg
  1506. (mod (+ term-input-ring-index (if (> arg 0) 1 -1))
  1507. (ring-length term-input-ring))
  1508. ;; For a new search, start from beginning or end, as appropriate
  1509. (if (>= arg 0)
  1510. 0 ; First elt for forward search
  1511. (1- (ring-length term-input-ring))))) ; Last elt for backward search
  1512. (defun term-previous-input-string (arg)
  1513. "Return the string ARG places along the input ring.
  1514. Moves relative to `term-input-ring-index'."
  1515. (ring-ref term-input-ring (if term-input-ring-index
  1516. (mod (+ arg term-input-ring-index)
  1517. (ring-length term-input-ring))
  1518. arg)))
  1519. (defun term-previous-input (arg)
  1520. "Cycle backwards through input history."
  1521. (interactive "*p")
  1522. (term-previous-matching-input "." arg))
  1523. (defun term-next-input (arg)
  1524. "Cycle forwards through input history."
  1525. (interactive "*p")
  1526. (term-previous-input (- arg)))
  1527. (defun term-previous-matching-input-string (regexp arg)
  1528. "Return the string matching REGEXP ARG places along the input ring.
  1529. Moves relative to `term-input-ring-index'."
  1530. (let* ((pos (term-previous-matching-input-string-position regexp arg)))
  1531. (when pos (ring-ref term-input-ring pos))))
  1532. (defun term-previous-matching-input-string-position
  1533. (regexp arg &optional start)
  1534. "Return the index matching REGEXP ARG places along the input ring.
  1535. Moves relative to START, or `term-input-ring-index'."
  1536. (when (or (not (ring-p term-input-ring))
  1537. (ring-empty-p term-input-ring))
  1538. (error "No history"))
  1539. (let* ((len (ring-length term-input-ring))
  1540. (motion (if (> arg 0) 1 -1))
  1541. (n (mod (- (or start (term-search-start arg)) motion) len))
  1542. (tried-each-ring-item nil)
  1543. (prev nil))
  1544. ;; Do the whole search as many times as the argument says.
  1545. (while (and (/= arg 0) (not tried-each-ring-item))
  1546. ;; Step once.
  1547. (setq prev n
  1548. n (mod (+ n motion) len))
  1549. ;; If we haven't reached a match, step some more.
  1550. (while (and (< n len) (not tried-each-ring-item)
  1551. (not (string-match regexp (ring-ref term-input-ring n))))
  1552. (setq n (mod (+ n motion) len)
  1553. ;; If we have gone all the way around in this search.
  1554. tried-each-ring-item (= n prev)))
  1555. (setq arg (if (> arg 0) (1- arg) (1+ arg))))
  1556. ;; Now that we know which ring element to use, if we found it, return that.
  1557. (when (string-match regexp (ring-ref term-input-ring n))
  1558. n)))
  1559. (defun term-previous-matching-input (regexp n)
  1560. "Search backwards through input history for match for REGEXP.
  1561. \(Previous history elements are earlier commands.)
  1562. With prefix argument N, search for Nth previous match.
  1563. If N is negative, find the next or Nth next match."
  1564. (interactive (term-regexp-arg "Previous input matching (regexp): "))
  1565. (setq n (term-search-arg n))
  1566. (let ((pos (term-previous-matching-input-string-position regexp n)))
  1567. ;; Has a match been found?
  1568. (if (null pos)
  1569. (error "Not found")
  1570. (setq term-input-ring-index pos)
  1571. (message "History item: %d" (1+ pos))
  1572. (delete-region
  1573. ;; Can't use kill-region as it sets this-command
  1574. (process-mark (get-buffer-process (current-buffer))) (point))
  1575. (insert (ring-ref term-input-ring pos)))))
  1576. (defun term-next-matching-input (regexp n)
  1577. "Search forwards through input history for match for REGEXP.
  1578. \(Later history elements are more recent commands.)
  1579. With prefix argument N, search for Nth following match.
  1580. If N is negative, find the previous or Nth previous match."
  1581. (interactive (term-regexp-arg "Next input matching (regexp): "))
  1582. (term-previous-matching-input regexp (- n)))
  1583. (defun term-previous-matching-input-from-input (n)
  1584. "Search backwards through input history for match for current input.
  1585. \(Previous history elements are earlier commands.)
  1586. With prefix argument N, search for Nth previous match.
  1587. If N is negative, search forwards for the -Nth following match."
  1588. (interactive "p")
  1589. (when (not (memq last-command '(term-previous-matching-input-from-input
  1590. term-next-matching-input-from-input)))
  1591. ;; Starting a new search
  1592. (setq term-matching-input-from-input-string
  1593. (buffer-substring
  1594. (process-mark (get-buffer-process (current-buffer)))
  1595. (point))
  1596. term-input-ring-index nil))
  1597. (term-previous-matching-input
  1598. (concat "^" (regexp-quote term-matching-input-from-input-string))
  1599. n))
  1600. (defun term-next-matching-input-from-input (n)
  1601. "Search forwards through input history for match for current input.
  1602. \(Following history elements are more recent commands.)
  1603. With prefix argument N, search for Nth following match.
  1604. If N is negative, search backwards for the -Nth previous match."
  1605. (interactive "p")
  1606. (term-previous-matching-input-from-input (- n)))
  1607. (defun term-replace-by-expanded-history (&optional silent)
  1608. "Expand input command history references before point.
  1609. Expansion is dependent on the value of `term-input-autoexpand'.
  1610. This function depends on the buffer's idea of the input history, which may not
  1611. match the command interpreter's idea, assuming it has one.
  1612. Assumes history syntax is like typical Un*x shells'. However, since Emacs
  1613. cannot know the interpreter's idea of input line numbers, assuming it has one,
  1614. it cannot expand absolute input line number references.
  1615. If the optional argument SILENT is non-nil, never complain
  1616. even if history reference seems erroneous.
  1617. See `term-magic-space' and `term-replace-by-expanded-history-before-point'.
  1618. Returns t if successful."
  1619. (interactive)
  1620. (when (and term-input-autoexpand
  1621. (string-match "[!^]" (funcall term-get-old-input))
  1622. (save-excursion (beginning-of-line)
  1623. (looking-at term-prompt-regexp)))
  1624. ;; Looks like there might be history references in the command.
  1625. (let ((previous-modified-tick (buffer-modified-tick)))
  1626. (message "Expanding history references...")
  1627. (term-replace-by-expanded-history-before-point silent)
  1628. (/= previous-modified-tick (buffer-modified-tick)))))
  1629. (defun term-replace-by-expanded-history-before-point (silent)
  1630. "Expand directory stack reference before point.
  1631. See `term-replace-by-expanded-history'. Returns t if successful."
  1632. (save-excursion
  1633. (let ((toend (- (line-end-position) (point)))
  1634. (start (progn (term-bol nil) (point))))
  1635. (while (progn
  1636. (skip-chars-forward "^!^" (- (line-end-position) toend))
  1637. (< (point) (- (line-end-position) toend)))
  1638. ;; This seems a bit complex. We look for references such as !!, !-num,
  1639. ;; !foo, !?foo, !{bar}, !?{bar}, ^oh, ^my^, ^god^it, ^never^ends^.
  1640. ;; If that wasn't enough, the plings can be suffixed with argument
  1641. ;; range specifiers.
  1642. ;; Argument ranges are complex too, so we hive off the input line,
  1643. ;; referenced with plings, with the range string to `term-args'.
  1644. (setq term-input-ring-index nil)
  1645. (cond ((or (= (preceding-char) ?\\)
  1646. (term-within-quotes start (point)))
  1647. ;; The history is quoted, or we're in quotes.
  1648. (goto-char (1+ (point))))
  1649. ((looking-at "![0-9]+\\($\\|[^-]\\)")
  1650. ;; We cannot know the interpreter's idea of input line numbers.
  1651. (goto-char (match-end 0))
  1652. (message "Absolute reference cannot be expanded"))
  1653. ((looking-at "!-\\([0-9]+\\)\\(:?[0-9^$*-]+\\)?")
  1654. ;; Just a number of args from `number' lines backward.
  1655. (let ((number (1- (string-to-number
  1656. (buffer-substring (match-beginning 1)
  1657. (match-end 1))))))
  1658. (if (<= number (ring-length term-input-ring))
  1659. (progn
  1660. (replace-match
  1661. (term-args (term-previous-input-string number)
  1662. (match-beginning 2) (match-end 2))
  1663. t t)
  1664. (setq term-input-ring-index number)
  1665. (message "History item: %d" (1+ number)))
  1666. (goto-char (match-end 0))
  1667. (message "Relative reference exceeds input history size"))))
  1668. ((or (looking-at "!!?:?\\([0-9^$*-]+\\)") (looking-at "!!"))
  1669. ;; Just a number of args from the previous input line.
  1670. (replace-match
  1671. (term-args (term-previous-input-string 0)
  1672. (match-beginning 1) (match-end 1))
  1673. t t)
  1674. (message "History item: previous"))
  1675. ((looking-at
  1676. "!\\??\\({\\(.+\\)}\\|\\(\\sw+\\)\\)\\(:?[0-9^$*-]+\\)?")
  1677. ;; Most recent input starting with or containing (possibly
  1678. ;; protected) string, maybe just a number of args. Phew.
  1679. (let* ((mb1 (match-beginning 1)) (me1 (match-end 1))
  1680. (mb2 (match-beginning 2)) (me2 (match-end 2))
  1681. (exp (buffer-substring (or mb2 mb1) (or me2 me1)))
  1682. (pref (if (save-match-data (looking-at "!\\?")) "" "^"))
  1683. (pos (save-match-data
  1684. (term-previous-matching-input-string-position
  1685. (concat pref (regexp-quote exp)) 1))))
  1686. (if (null pos)
  1687. (progn
  1688. (goto-char (match-end 0))
  1689. (or silent
  1690. (progn (message "Not found")
  1691. (ding))))
  1692. (setq term-input-ring-index pos)
  1693. (replace-match
  1694. (term-args (ring-ref term-input-ring pos)
  1695. (match-beginning 4) (match-end 4))
  1696. t t)
  1697. (message "History item: %d" (1+ pos)))))
  1698. ((looking-at "\\^\\([^^]+\\)\\^?\\([^^]*\\)\\^?")
  1699. ;; Quick substitution on the previous input line.
  1700. (let ((old (buffer-substring (match-beginning 1) (match-end 1)))
  1701. (new (buffer-substring (match-beginning 2) (match-end 2)))
  1702. (pos nil))
  1703. (replace-match (term-previous-input-string 0) t t)
  1704. (setq pos (point))
  1705. (goto-char (match-beginning 0))
  1706. (if (not (search-forward old pos t))
  1707. (or silent
  1708. (error "Not found"))
  1709. (replace-match new t t)
  1710. (message "History item: substituted"))))
  1711. (t
  1712. (goto-char (match-end 0))))))))
  1713. (defun term-magic-space (arg)
  1714. "Expand input history references before point and insert ARG spaces.
  1715. A useful command to bind to SPC. See `term-replace-by-expanded-history'."
  1716. (interactive "p")
  1717. (term-replace-by-expanded-history)
  1718. (self-insert-command arg))
  1719. (defun term-within-quotes (beg end)
  1720. "Return t if the number of quotes between BEG and END is odd.
  1721. Quotes are single and double."
  1722. (let ((countsq (term-how-many-region "\\(^\\|[^\\\\]\\)\'" beg end))
  1723. (countdq (term-how-many-region "\\(^\\|[^\\\\]\\)\"" beg end)))
  1724. (or (= (mod countsq 2) 1) (= (mod countdq 2) 1))))
  1725. (defun term-how-many-region (regexp beg end)
  1726. "Return number of matches for REGEXP from BEG to END."
  1727. (let ((count 0))
  1728. (save-excursion
  1729. (save-match-data
  1730. (goto-char beg)
  1731. (while (re-search-forward regexp end t)
  1732. (setq count (1+ count)))))
  1733. count))
  1734. (defun term-args (string begin end)
  1735. ;; From STRING, return the args depending on the range specified in the text
  1736. ;; from BEGIN to END. If BEGIN is nil, assume all args. Ignore leading `:'.
  1737. ;; Range can be x-y, x-, -y, where x/y can be [0-9], *, ^, $.
  1738. (save-match-data
  1739. (if (null begin)
  1740. (term-arguments string 0 nil)
  1741. (let* ((range (buffer-substring
  1742. (if (eq (char-after begin) ?:) (1+ begin) begin) end))
  1743. (nth (cond ((string-match "^[*^]" range) 1)
  1744. ((string-match "^-" range) 0)
  1745. ((string-equal range "$") nil)
  1746. (t (string-to-number range))))
  1747. (mth (cond ((string-match "[-*$]$" range) nil)
  1748. ((string-match "-" range)
  1749. (string-to-number (substring range (match-end 0))))
  1750. (t nth))))
  1751. (term-arguments string nth mth)))))
  1752. ;; Return a list of arguments from ARG. Break it up at the
  1753. ;; delimiters in term-delimiter-argument-list. Returned list is backwards.
  1754. (defun term-delim-arg (arg)
  1755. (if (null term-delimiter-argument-list)
  1756. (list arg)
  1757. (let ((args nil)
  1758. (pos 0)
  1759. (len (length arg)))
  1760. (while (< pos len)
  1761. (let ((char (aref arg pos))
  1762. (start pos))
  1763. (if (memq char term-delimiter-argument-list)
  1764. (while (and (< pos len) (eq (aref arg pos) char))
  1765. (setq pos (1+ pos)))
  1766. (while (and (< pos len)
  1767. (not (memq (aref arg pos)
  1768. term-delimiter-argument-list)))
  1769. (setq pos (1+ pos))))
  1770. (setq args (cons (substring arg start pos) args))))
  1771. args)))
  1772. (defun term-arguments (string nth mth)
  1773. "Return from STRING the NTH to MTH arguments.
  1774. NTH and/or MTH can be nil, which means the last argument.
  1775. Returned arguments are separated by single spaces.
  1776. We assume whitespace separates arguments, except within quotes.
  1777. Also, a run of one or more of a single character
  1778. in `term-delimiter-argument-list' is a separate argument.
  1779. Argument 0 is the command name."
  1780. (let ((argpart "[^ \n\t\"'`]+\\|\\(\"[^\"]*\"\\|'[^']*'\\|`[^`]*`\\)")
  1781. (args ()) (pos 0)
  1782. (count 0)
  1783. beg str quotes)
  1784. ;; Build a list of all the args until we have as many as we want.
  1785. (while (and (or (null mth) (<= count mth))
  1786. (string-match argpart string pos))
  1787. (if (and beg (= pos (match-beginning 0)))
  1788. ;; It's contiguous, part of the same arg.
  1789. (setq pos (match-end 0)
  1790. quotes (or quotes (match-beginning 1)))
  1791. ;; It's a new separate arg.
  1792. (if beg
  1793. ;; Put the previous arg, if there was one, onto ARGS.
  1794. (setq str (substring string beg pos)
  1795. args (if quotes (cons str args)
  1796. (nconc (term-delim-arg str) args))
  1797. count (1+ count)))
  1798. (setq quotes (match-beginning 1))
  1799. (setq beg (match-beginning 0))
  1800. (setq pos (match-end 0))))
  1801. (if beg
  1802. (setq str (substring string beg pos)
  1803. args (if quotes (cons str args)
  1804. (nconc (term-delim-arg str) args))
  1805. count (1+ count)))
  1806. (let ((n (or nth (1- count)))
  1807. (m (if mth (1- (- count mth)) 0)))
  1808. (mapconcat
  1809. (function (lambda (a) a)) (nthcdr n (nreverse (nthcdr m args))) " "))))
  1810. ;;;
  1811. ;;; Input processing stuff [line mode]
  1812. ;;;
  1813. (defun term-send-input ()
  1814. "Send input to process.
  1815. After the process output mark, sends all text from the process mark to
  1816. point as input to the process. Before the process output mark, calls value
  1817. of variable `term-get-old-input' to retrieve old input, copies it to the
  1818. process mark, and sends it. A terminal newline is also inserted into the
  1819. buffer and sent to the process. The list of function names contained in the
  1820. value of `term-input-filter-functions' is called on the input before sending
  1821. it. The input is entered into the input history ring, if the value of variable
  1822. `term-input-filter' returns non-nil when called on the input.
  1823. Any history reference may be expanded depending on the value of the variable
  1824. `term-input-autoexpand'. The list of function names contained in the value
  1825. of `term-input-filter-functions' is called on the input before sending it.
  1826. The input is entered into the input history ring, if the value of variable
  1827. `term-input-filter' returns non-nil when called on the input.
  1828. If variable `term-eol-on-send' is non-nil, then point is moved to the
  1829. end of line before sending the input.
  1830. The values of `term-get-old-input', `term-input-filter-functions', and
  1831. `term-input-filter' are chosen according to the command interpreter running
  1832. in the buffer. E.g.,
  1833. If the interpreter is the csh,
  1834. term-get-old-input is the default: take the current line, discard any
  1835. initial string matching regexp term-prompt-regexp.
  1836. term-input-filter-functions monitors input for \"cd\", \"pushd\", and
  1837. \"popd\" commands. When it sees one, it cd's the buffer.
  1838. term-input-filter is the default: returns t if the input isn't all white
  1839. space.
  1840. If the term is Lucid Common Lisp,
  1841. term-get-old-input snarfs the sexp ending at point.
  1842. term-input-filter-functions does nothing.
  1843. term-input-filter returns nil if the input matches input-filter-regexp,
  1844. which matches (1) all whitespace (2) :a, :c, etc.
  1845. Similarly for Soar, Scheme, etc."
  1846. (interactive)
  1847. ;; Note that the input string does not include its terminal newline.
  1848. (let ((proc (get-buffer-process (current-buffer))))
  1849. (if (not proc) (error "Current buffer has no process")
  1850. (let* ((pmark (process-mark proc))
  1851. (pmark-val (marker-position pmark))
  1852. (input-is-new (>= (point) pmark-val))
  1853. (intxt (if input-is-new
  1854. (progn (if term-eol-on-send (end-of-line))
  1855. (buffer-substring pmark (point)))
  1856. (funcall term-get-old-input)))
  1857. (input (if (not (eq term-input-autoexpand 'input))
  1858. ;; Just whatever's already there
  1859. intxt
  1860. ;; Expand and leave it visible in buffer
  1861. (term-replace-by-expanded-history t)
  1862. (buffer-substring pmark (point))))
  1863. (history (if (not (eq term-input-autoexpand 'history))
  1864. input
  1865. ;; This is messy 'cos ultimately the original
  1866. ;; functions used do insertion, rather than return
  1867. ;; strings. We have to expand, then insert back.
  1868. (term-replace-by-expanded-history t)
  1869. (let ((copy (buffer-substring pmark (point))))
  1870. (delete-region pmark (point))
  1871. (insert input)
  1872. copy))))
  1873. (when (term-pager-enabled)
  1874. (save-excursion
  1875. (goto-char (process-mark proc))
  1876. (setq term-pager-count (term-current-row))))
  1877. (when (and (funcall term-input-filter history)
  1878. (or (null term-input-ignoredups)
  1879. (not (ring-p term-input-ring))
  1880. (ring-empty-p term-input-ring)
  1881. (not (string-equal (ring-ref term-input-ring 0)
  1882. history))))
  1883. (ring-insert term-input-ring history))
  1884. (let ((functions term-input-filter-functions))
  1885. (while functions
  1886. (funcall (car functions) (concat input "\n"))
  1887. (setq functions (cdr functions))))
  1888. (setq term-input-ring-index nil)
  1889. ;; Update the markers before we send the input
  1890. ;; in case we get output amidst sending the input.
  1891. (set-marker term-last-input-start pmark)
  1892. (set-marker term-last-input-end (point))
  1893. (when input-is-new
  1894. ;; Set up to delete, because inferior should echo.
  1895. (when (marker-buffer term-pending-delete-marker)
  1896. (delete-region term-pending-delete-marker pmark))
  1897. (set-marker term-pending-delete-marker pmark-val)
  1898. (set-marker (process-mark proc) (point)))
  1899. (goto-char pmark)
  1900. (funcall term-input-sender proc input)))))
  1901. (defun term-get-old-input-default ()
  1902. "Default for `term-get-old-input'.
  1903. Take the current line, and discard any initial text matching
  1904. `term-prompt-regexp'."
  1905. (save-excursion
  1906. (beginning-of-line)
  1907. (term-skip-prompt)
  1908. (let ((beg (point)))
  1909. (end-of-line)
  1910. (buffer-substring beg (point)))))
  1911. (defun term-copy-old-input ()
  1912. "Insert after prompt old input at point as new input to be edited.
  1913. Calls `term-get-old-input' to get old input."
  1914. (interactive)
  1915. (let ((input (funcall term-get-old-input))
  1916. (process (get-buffer-process (current-buffer))))
  1917. (if (not process)
  1918. (error "Current buffer has no process")
  1919. (goto-char (process-mark process))
  1920. (insert input))))
  1921. (defun term-skip-prompt ()
  1922. "Skip past the text matching regexp `term-prompt-regexp'.
  1923. If this takes us past the end of the current line, don't skip at all."
  1924. (let ((eol (line-end-position)))
  1925. (when (and (looking-at term-prompt-regexp)
  1926. (<= (match-end 0) eol))
  1927. (goto-char (match-end 0)))))
  1928. (defun term-after-pmark-p ()
  1929. "Is point after the process output marker?"
  1930. ;; Since output could come into the buffer after we looked at the point
  1931. ;; but before we looked at the process marker's value, we explicitly
  1932. ;; serialize. This is just because I don't know whether or not Emacs
  1933. ;; services input during execution of lisp commands.
  1934. (let ((proc-pos (marker-position
  1935. (process-mark (get-buffer-process (current-buffer))))))
  1936. (<= proc-pos (point))))
  1937. (defun term-simple-send (proc string)
  1938. "Default function for sending to PROC input STRING.
  1939. This just sends STRING plus a newline. To override this,
  1940. set the hook `term-input-sender'."
  1941. (term-send-string proc string)
  1942. (term-send-string proc "\n"))
  1943. (defun term-bol (arg)
  1944. "Go to the beginning of line, then skip past the prompt, if any.
  1945. If a prefix argument is given (\\[universal-argument]), then no prompt skip
  1946. -- go straight to column 0.
  1947. The prompt skip is done by skipping text matching the regular expression
  1948. `term-prompt-regexp', a buffer local variable."
  1949. (interactive "P")
  1950. (beginning-of-line)
  1951. (when (null arg) (term-skip-prompt)))
  1952. ;; These two functions are for entering text you don't want echoed or
  1953. ;; saved -- typically passwords to ftp, telnet, or somesuch.
  1954. ;; Just enter m-x term-send-invisible and type in your line.
  1955. (defun term-read-noecho (prompt &optional stars)
  1956. "Read a single line of text from user without echoing, and return it.
  1957. Prompt with argument PROMPT, a string. Optional argument STARS causes
  1958. input to be echoed with '*' characters on the prompt line. Input ends with
  1959. RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line. C-g aborts (if
  1960. `inhibit-quit' is set because e.g. this function was called from a process
  1961. filter and C-g is pressed, this function returns nil rather than a string).
  1962. Note that the keystrokes comprising the text can still be recovered
  1963. \(temporarily) with \\[view-lossage]. This may be a security bug for some
  1964. applications."
  1965. (let ((ans "")
  1966. (c 0)
  1967. (echo-keystrokes 0)
  1968. (cursor-in-echo-area t)
  1969. (done nil))
  1970. (while (not done)
  1971. (if stars
  1972. (message "%s%s" prompt (make-string (length ans) ?*))
  1973. (message "%s" prompt))
  1974. (setq c (read-char))
  1975. (cond ((= c ?\C-g)
  1976. ;; This function may get called from a process filter, where
  1977. ;; inhibit-quit is set. In later versions of Emacs read-char
  1978. ;; may clear quit-flag itself and return C-g. That would make
  1979. ;; it impossible to quit this loop in a simple way, so
  1980. ;; re-enable it here (for backward-compatibility the check for
  1981. ;; quit-flag below would still be necessary, so this seems
  1982. ;; like the simplest way to do things).
  1983. (setq quit-flag t
  1984. done t))
  1985. ((or (= c ?\r) (= c ?\n) (= c ?\e))
  1986. (setq done t))
  1987. ((= c ?\C-u)
  1988. (setq ans ""))
  1989. ((and (/= c ?\b) (/= c ?\177))
  1990. (setq ans (concat ans (char-to-string c))))
  1991. ((> (length ans) 0)
  1992. (setq ans (substring ans 0 -1)))))
  1993. (if quit-flag
  1994. ;; Emulate a true quit, except that we have to return a value.
  1995. (prog1
  1996. (setq quit-flag nil)
  1997. (message "Quit")
  1998. (beep t))
  1999. (message "")
  2000. ans)))
  2001. (defun term-send-invisible (str &optional proc)
  2002. "Read a string without echoing.
  2003. Then send it to the process running in the current buffer. A new-line
  2004. is additionally sent. String is not saved on term input history list.
  2005. Security bug: your string can still be temporarily recovered with
  2006. \\[view-lossage]."
  2007. (interactive "P") ; Defeat snooping via C-x esc
  2008. (when (not (stringp str))
  2009. (setq str (term-read-noecho "Non-echoed text: " t)))
  2010. (when (not proc)
  2011. (setq proc (get-buffer-process (current-buffer))))
  2012. (if (not proc) (error "Current buffer has no process")
  2013. (setq term-kill-echo-list (nconc term-kill-echo-list
  2014. (cons str nil)))
  2015. (term-send-string proc str)
  2016. (term-send-string proc "\n")))
  2017. ;;; Low-level process communication
  2018. (defcustom term-input-chunk-size 512
  2019. "Long inputs send to term processes are broken up into chunks of this size.
  2020. If your process is choking on big inputs, try lowering the value."
  2021. :group 'term
  2022. :type 'integer)
  2023. (defun term-send-string (proc str)
  2024. "Send to PROC the contents of STR as input.
  2025. This is equivalent to `process-send-string', except that long input strings
  2026. are broken up into chunks of size `term-input-chunk-size'. Processes
  2027. are given a chance to output between chunks. This can help prevent processes
  2028. from hanging when you send them long inputs on some OS's."
  2029. (let* ((len (length str))
  2030. (i (min len term-input-chunk-size)))
  2031. (process-send-string proc (substring str 0 i))
  2032. (while (< i len)
  2033. (let ((next-i (+ i term-input-chunk-size)))
  2034. (accept-process-output)
  2035. (process-send-string proc (substring str i (min len next-i)))
  2036. (setq i next-i)))))
  2037. (defun term-send-region (proc start end)
  2038. "Send to PROC the region delimited by START and END.
  2039. This is a replacement for `process-send-region' that tries to keep
  2040. your process from hanging on long inputs. See `term-send-string'."
  2041. (term-send-string proc (buffer-substring start end)))
  2042. ;;; Random input hackage
  2043. (defun term-kill-output ()
  2044. "Kill all output from interpreter since last input."
  2045. (interactive)
  2046. (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
  2047. (kill-region term-last-input-end pmark)
  2048. (goto-char pmark)
  2049. (insert "*** output flushed ***\n")
  2050. (set-marker pmark (point))))
  2051. (defun term-show-output ()
  2052. "Display start of this batch of interpreter output at top of window.
  2053. Sets mark to the value of point when this command is run."
  2054. (interactive)
  2055. (goto-char term-last-input-end)
  2056. (backward-char)
  2057. (beginning-of-line)
  2058. (set-window-start (selected-window) (point))
  2059. (end-of-line))
  2060. (defun term-interrupt-subjob ()
  2061. "Interrupt the current subjob."
  2062. (interactive)
  2063. (interrupt-process nil term-ptyp))
  2064. (defun term-kill-subjob ()
  2065. "Send kill signal to the current subjob."
  2066. (interactive)
  2067. (kill-process nil term-ptyp))
  2068. (defun term-quit-subjob ()
  2069. "Send quit signal to the current subjob."
  2070. (interactive)
  2071. (quit-process nil term-ptyp))
  2072. (defun term-stop-subjob ()
  2073. "Stop the current subjob.
  2074. WARNING: if there is no current subjob, you can end up suspending
  2075. the top-level process running in the buffer. If you accidentally do
  2076. this, use \\[term-continue-subjob] to resume the process. (This
  2077. is not a problem with most shells, since they ignore this signal.)"
  2078. (interactive)
  2079. (stop-process nil term-ptyp))
  2080. (defun term-continue-subjob ()
  2081. "Send CONT signal to process buffer's process group.
  2082. Useful if you accidentally suspend the top-level process."
  2083. (interactive)
  2084. (continue-process nil term-ptyp))
  2085. (defun term-kill-input ()
  2086. "Kill all text from last stuff output by interpreter to point."
  2087. (interactive)
  2088. (let* ((pmark (process-mark (get-buffer-process (current-buffer))))
  2089. (p-pos (marker-position pmark)))
  2090. (when (> (point) p-pos)
  2091. (kill-region pmark (point)))))
  2092. (defun term-delchar-or-maybe-eof (arg)
  2093. "Delete ARG characters forward, or send an EOF to process if at end of
  2094. buffer."
  2095. (interactive "p")
  2096. (if (eobp)
  2097. (process-send-eof)
  2098. (delete-char arg)))
  2099. (defun term-send-eof ()
  2100. "Send an EOF to the current buffer's process."
  2101. (interactive)
  2102. (process-send-eof))
  2103. (defun term-backward-matching-input (regexp n)
  2104. "Search backward through buffer for match for REGEXP.
  2105. Matches are searched for on lines that match `term-prompt-regexp'.
  2106. With prefix argument N, search for Nth previous match.
  2107. If N is negative, find the next or Nth next match."
  2108. (interactive (term-regexp-arg "Backward input matching (regexp): "))
  2109. (let* ((re (concat term-prompt-regexp ".*" regexp))
  2110. (pos (save-excursion (end-of-line (if (> n 0) 0 1))
  2111. (when (re-search-backward re nil t n)
  2112. (point)))))
  2113. (if (null pos)
  2114. (progn (message "Not found")
  2115. (ding))
  2116. (goto-char pos)
  2117. (term-bol nil))))
  2118. (defun term-forward-matching-input (regexp n)
  2119. "Search forward through buffer for match for REGEXP.
  2120. Matches are searched for on lines that match `term-prompt-regexp'.
  2121. With prefix argument N, search for Nth following match.
  2122. If N is negative, find the previous or Nth previous match."
  2123. (interactive (term-regexp-arg "Forward input matching (regexp): "))
  2124. (term-backward-matching-input regexp (- n)))
  2125. (defun term-next-prompt (n)
  2126. "Move to end of Nth next prompt in the buffer.
  2127. See `term-prompt-regexp'."
  2128. (interactive "p")
  2129. (let ((paragraph-start term-prompt-regexp))
  2130. (end-of-line (if (> n 0) 1 0))
  2131. (forward-paragraph n)
  2132. (term-skip-prompt)))
  2133. (defun term-previous-prompt (n)
  2134. "Move to end of Nth previous prompt in the buffer.
  2135. See `term-prompt-regexp'."
  2136. (interactive "p")
  2137. (term-next-prompt (- n)))
  2138. ;;; Support for source-file processing commands.
  2139. ;;============================================================================
  2140. ;; Many command-interpreters (e.g., Lisp, Scheme, Soar) have
  2141. ;; commands that process files of source text (e.g. loading or compiling
  2142. ;; files). So the corresponding process-in-a-buffer modes have commands
  2143. ;; for doing this (e.g., lisp-load-file). The functions below are useful
  2144. ;; for defining these commands.
  2145. ;;
  2146. ;; Alas, these guys don't do exactly the right thing for Lisp, Scheme
  2147. ;; and Soar, in that they don't know anything about file extensions.
  2148. ;; So the compile/load interface gets the wrong default occasionally.
  2149. ;; The load-file/compile-file default mechanism could be smarter -- it
  2150. ;; doesn't know about the relationship between filename extensions and
  2151. ;; whether the file is source or executable. If you compile foo.lisp
  2152. ;; with compile-file, then the next load-file should use foo.bin for
  2153. ;; the default, not foo.lisp. This is tricky to do right, particularly
  2154. ;; because the extension for executable files varies so much (.o, .bin,
  2155. ;; .lbin, .mo, .vo, .ao, ...).
  2156. ;; TERM-SOURCE-DEFAULT -- determines defaults for source-file processing
  2157. ;; commands.
  2158. ;;
  2159. ;; TERM-CHECK-SOURCE -- if FNAME is in a modified buffer, asks you if you
  2160. ;; want to save the buffer before issuing any process requests to the command
  2161. ;; interpreter.
  2162. ;;
  2163. ;; TERM-GET-SOURCE -- used by the source-file processing commands to prompt
  2164. ;; for the file to process.
  2165. ;; (TERM-SOURCE-DEFAULT previous-dir/file source-modes)
  2166. ;;============================================================================
  2167. ;; This function computes the defaults for the load-file and compile-file
  2168. ;; commands for tea, soar, cmulisp, and cmuscheme modes.
  2169. ;;
  2170. ;; - PREVIOUS-DIR/FILE is a pair (directory . filename) from the last
  2171. ;; source-file processing command, or nil if there hasn't been one yet.
  2172. ;; - SOURCE-MODES is a list used to determine what buffers contain source
  2173. ;; files: if the major mode of the buffer is in SOURCE-MODES, it's source.
  2174. ;; Typically, (lisp-mode) or (scheme-mode).
  2175. ;;
  2176. ;; If the command is given while the cursor is inside a string, *and*
  2177. ;; the string is an existing filename, *and* the filename is not a directory,
  2178. ;; then the string is taken as default. This allows you to just position
  2179. ;; your cursor over a string that's a filename and have it taken as default.
  2180. ;;
  2181. ;; If the command is given in a file buffer whose major mode is in
  2182. ;; SOURCE-MODES, then the filename is the default file, and the
  2183. ;; file's directory is the default directory.
  2184. ;;
  2185. ;; If the buffer isn't a source file buffer (e.g., it's the process buffer),
  2186. ;; then the default directory & file are what was used in the last source-file
  2187. ;; processing command (i.e., PREVIOUS-DIR/FILE). If this is the first time
  2188. ;; the command has been run (PREVIOUS-DIR/FILE is nil), the default directory
  2189. ;; is the cwd, with no default file. (\"no default file\" = nil)
  2190. ;;
  2191. ;; SOURCE-REGEXP is typically going to be something like (tea-mode)
  2192. ;; for T programs, (lisp-mode) for Lisp programs, (soar-mode lisp-mode)
  2193. ;; for Soar programs, etc.
  2194. ;;
  2195. ;; The function returns a pair: (default-directory . default-file).
  2196. (defun term-source-default (previous-dir/file source-modes)
  2197. (cond ((and buffer-file-name (memq major-mode source-modes))
  2198. (cons (file-name-directory buffer-file-name)
  2199. (file-name-nondirectory buffer-file-name)))
  2200. (previous-dir/file)
  2201. (t
  2202. (cons default-directory nil))))
  2203. ;; (TERM-CHECK-SOURCE fname)
  2204. ;;============================================================================
  2205. ;; Prior to loading or compiling (or otherwise processing) a file (in the CMU
  2206. ;; process-in-a-buffer modes), this function can be called on the filename.
  2207. ;; If the file is loaded into a buffer, and the buffer is modified, the user
  2208. ;; is queried to see if he wants to save the buffer before proceeding with
  2209. ;; the load or compile.
  2210. (defun term-check-source (fname)
  2211. (let ((buff (get-file-buffer fname)))
  2212. (when (and buff
  2213. (buffer-modified-p buff)
  2214. (y-or-n-p (format "Save buffer %s first? "
  2215. (buffer-name buff))))
  2216. ;; save BUFF.
  2217. (with-current-buffer buff
  2218. (save-buffer)))))
  2219. ;; (TERM-GET-SOURCE prompt prev-dir/file source-modes mustmatch-p)
  2220. ;;============================================================================
  2221. ;; TERM-GET-SOURCE is used to prompt for filenames in command-interpreter
  2222. ;; commands that process source files (like loading or compiling a file).
  2223. ;; It prompts for the filename, provides a default, if there is one,
  2224. ;; and returns the result filename.
  2225. ;;
  2226. ;; See TERM-SOURCE-DEFAULT for more on determining defaults.
  2227. ;;
  2228. ;; PROMPT is the prompt string. PREV-DIR/FILE is the (directory . file) pair
  2229. ;; from the last source processing command. SOURCE-MODES is a list of major
  2230. ;; modes used to determine what file buffers contain source files. (These
  2231. ;; two arguments are used for determining defaults). If MUSTMATCH-P is true,
  2232. ;; then the filename reader will only accept a file that exists.
  2233. ;;
  2234. ;; A typical use:
  2235. ;; (interactive (term-get-source "Compile file: " prev-lisp-dir/file
  2236. ;; '(lisp-mode) t))
  2237. ;; This is pretty stupid about strings. It decides we're in a string
  2238. ;; if there's a quote on both sides of point on the current line.
  2239. (defun term-extract-string ()
  2240. "Return string around `point' that starts the current line or nil."
  2241. (save-excursion
  2242. (let* ((point (point))
  2243. (bol (line-beginning-position))
  2244. (eol (line-end-position))
  2245. (start (and (search-backward "\"" bol t)
  2246. (1+ (point))))
  2247. (end (progn (goto-char point)
  2248. (and (search-forward "\"" eol t)
  2249. (1- (point))))))
  2250. (and start end
  2251. (buffer-substring start end)))))
  2252. (defun term-get-source (prompt prev-dir/file source-modes mustmatch-p)
  2253. (let* ((def (term-source-default prev-dir/file source-modes))
  2254. (stringfile (term-extract-string))
  2255. (sfile-p (and stringfile
  2256. (condition-case ()
  2257. (file-exists-p stringfile)
  2258. (error nil))
  2259. (not (file-directory-p stringfile))))
  2260. (defdir (if sfile-p (file-name-directory stringfile)
  2261. (car def)))
  2262. (deffile (if sfile-p (file-name-nondirectory stringfile)
  2263. (cdr def)))
  2264. (ans (read-file-name (if deffile (format "%s(default %s) "
  2265. prompt deffile)
  2266. prompt)
  2267. defdir
  2268. (concat defdir deffile)
  2269. mustmatch-p)))
  2270. (list (expand-file-name (substitute-in-file-name ans)))))
  2271. ;; I am somewhat divided on this string-default feature. It seems
  2272. ;; to violate the principle-of-least-astonishment, in that it makes
  2273. ;; the default harder to predict, so you actually have to look and see
  2274. ;; what the default really is before choosing it. This can trip you up.
  2275. ;; On the other hand, it can be useful, I guess. I would appreciate feedback
  2276. ;; on this.
  2277. ;; -Olin
  2278. ;;; Simple process query facility.
  2279. ;; ===========================================================================
  2280. ;; This function is for commands that want to send a query to the process
  2281. ;; and show the response to the user. For example, a command to get the
  2282. ;; arglist for a Common Lisp function might send a "(arglist 'foo)" query
  2283. ;; to an inferior Common Lisp process.
  2284. ;;
  2285. ;; This simple facility just sends strings to the inferior process and pops
  2286. ;; up a window for the process buffer so you can see what the process
  2287. ;; responds with. We don't do anything fancy like try to intercept what the
  2288. ;; process responds with and put it in a pop-up window or on the message
  2289. ;; line. We just display the buffer. Low tech. Simple. Works good.
  2290. ;; Send to the inferior process PROC the string STR. Pop-up but do not select
  2291. ;; a window for the inferior process so that its response can be seen.
  2292. (defun term-proc-query (proc str)
  2293. (let* ((proc-buf (process-buffer proc))
  2294. (proc-mark (process-mark proc)))
  2295. (display-buffer proc-buf)
  2296. (set-buffer proc-buf) ; but it's not the selected *window*
  2297. (let ((proc-win (get-buffer-window proc-buf))
  2298. (proc-pt (marker-position proc-mark)))
  2299. (term-send-string proc str) ; send the query
  2300. (accept-process-output proc) ; wait for some output
  2301. ;; Try to position the proc window so you can see the answer.
  2302. ;; This is bogus code. If you delete the (sit-for 0), it breaks.
  2303. ;; I don't know why. Wizards invited to improve it.
  2304. (when (not (pos-visible-in-window-p proc-pt proc-win))
  2305. (let ((opoint (window-point proc-win)))
  2306. (set-window-point proc-win proc-mark) (sit-for 0)
  2307. (if (not (pos-visible-in-window-p opoint proc-win))
  2308. (push-mark opoint)
  2309. (set-window-point proc-win opoint)))))))
  2310. ;; Returns the current column in the current screen line.
  2311. ;; Note: (current-column) yields column in buffer line.
  2312. (defun term-horizontal-column ()
  2313. (- (term-current-column) (term-start-line-column)))
  2314. ;; Calls either vertical-motion or term-buffer-vertical-motion
  2315. (defmacro term-vertical-motion (count)
  2316. (list 'funcall 'term-vertical-motion count))
  2317. ; An emulation of vertical-motion that is independent of having a window.
  2318. ; Instead, it uses the term-width variable as the logical window width.
  2319. (defun term-buffer-vertical-motion (count)
  2320. (cond ((= count 0)
  2321. (move-to-column (* term-width (/ (current-column) term-width)))
  2322. 0)
  2323. ((> count 0)
  2324. (let ((H)
  2325. (todo (+ count (/ (current-column) term-width))))
  2326. (end-of-line)
  2327. ;; The loop iterates over buffer lines;
  2328. ;; H is the number of screen lines in the current line, i.e.
  2329. ;; the ceiling of dividing the buffer line width by term-width.
  2330. (while (and (<= (setq H (max (/ (+ (current-column) term-width -1)
  2331. term-width)
  2332. 1))
  2333. todo)
  2334. (not (eobp)))
  2335. (setq todo (- todo H))
  2336. (forward-char) ;; Move past the ?\n
  2337. (end-of-line)) ;; and on to the end of the next line.
  2338. (if (and (>= todo H) (> todo 0))
  2339. (+ (- count todo) H -1) ;; Hit end of buffer.
  2340. (move-to-column (* todo term-width))
  2341. count)))
  2342. (t ;; (< count 0) ;; Similar algorithm, but for upward motion.
  2343. (let ((H)
  2344. (todo (- count)))
  2345. (while (and (<= (setq H (max (/ (+ (current-column) term-width -1)
  2346. term-width)
  2347. 1))
  2348. todo)
  2349. (progn (beginning-of-line)
  2350. (not (bobp))))
  2351. (setq todo (- todo H))
  2352. (backward-char)) ;; Move to end of previous line.
  2353. (if (and (>= todo H) (> todo 0))
  2354. (+ count todo (- 1 H)) ;; Hit beginning of buffer.
  2355. (move-to-column (* (- H todo 1) term-width))
  2356. count)))))
  2357. ;; The term-start-line-column variable is used as a cache.
  2358. (defun term-start-line-column ()
  2359. (cond (term-start-line-column)
  2360. ((let ((save-pos (point)))
  2361. (term-vertical-motion 0)
  2362. (setq term-start-line-column (current-column))
  2363. (goto-char save-pos)
  2364. term-start-line-column))))
  2365. ;; Same as (current-column), but uses term-current-column as a cache.
  2366. (defun term-current-column ()
  2367. (cond (term-current-column)
  2368. ((setq term-current-column (current-column)))))
  2369. ;; Move DELTA column right (or left if delta < 0 limiting at column 0).
  2370. (defun term-move-columns (delta)
  2371. (setq term-current-column (max 0 (+ (term-current-column) delta)))
  2372. (let ((point-at-eol (line-end-position)))
  2373. (move-to-column term-current-column t)
  2374. ;; If move-to-column extends the current line it will use the face
  2375. ;; from the last character on the line, set the face for the chars
  2376. ;; to default.
  2377. (when (> (point) point-at-eol)
  2378. (put-text-property point-at-eol (point) 'face 'default))))
  2379. ;; Insert COUNT copies of CHAR in the default face.
  2380. (defun term-insert-char (char count)
  2381. (let ((old-point (point)))
  2382. (insert-char char count)
  2383. (put-text-property old-point (point) 'face 'default)))
  2384. (defun term-current-row ()
  2385. (cond (term-current-row)
  2386. ((setq term-current-row
  2387. (save-restriction
  2388. (save-excursion
  2389. (narrow-to-region term-home-marker (point-max))
  2390. (- (term-vertical-motion -9999))))))))
  2391. (defun term-adjust-current-row-cache (delta)
  2392. (when term-current-row
  2393. (setq term-current-row
  2394. (max 0 (+ term-current-row delta)))))
  2395. (defun term-terminal-pos ()
  2396. (save-excursion ; save-restriction
  2397. (let ((save-col (term-current-column))
  2398. x y)
  2399. (term-vertical-motion 0)
  2400. (setq x (- save-col (current-column)))
  2401. (setq y (term-vertical-motion term-height))
  2402. (cons x y))))
  2403. ;;Function that handles term messages: code by rms (and you can see the
  2404. ;;difference ;-) -mm
  2405. (defun term-handle-ansi-terminal-messages (message)
  2406. ;; Is there a command here?
  2407. (while (string-match "\eAnSiT.+\n" message)
  2408. ;; Extract the command code and the argument.
  2409. (let* ((start (match-beginning 0))
  2410. (command-code (aref message (+ start 6)))
  2411. (argument
  2412. (save-match-data
  2413. (substring message
  2414. (+ start 8)
  2415. (string-match "\r?\n" message
  2416. (+ start 8)))))
  2417. ignore)
  2418. ;; Delete this command from MESSAGE.
  2419. (setq message (replace-match "" t t message))
  2420. ;; If we recognize the type of command, set the appropriate variable.
  2421. (cond ((= command-code ?c)
  2422. (setq term-ansi-at-dir argument))
  2423. ((= command-code ?h)
  2424. (setq term-ansi-at-host argument))
  2425. ((= command-code ?u)
  2426. (setq term-ansi-at-user argument))
  2427. ;; Otherwise ignore this one.
  2428. (t
  2429. (setq ignore t)))
  2430. ;; Update default-directory based on the changes this command made.
  2431. (if ignore
  2432. nil
  2433. (setq default-directory
  2434. (file-name-as-directory
  2435. (if (and (string= term-ansi-at-host (system-name))
  2436. (string= term-ansi-at-user (user-real-login-name)))
  2437. (expand-file-name term-ansi-at-dir)
  2438. (if (string= term-ansi-at-user (user-real-login-name))
  2439. (concat "/" term-ansi-at-host ":" term-ansi-at-dir)
  2440. (concat "/" term-ansi-at-user "@" term-ansi-at-host ":"
  2441. term-ansi-at-dir)))))
  2442. ;; I'm not sure this is necessary,
  2443. ;; but it's best to be on the safe side.
  2444. (if (string= term-ansi-at-host (system-name))
  2445. (progn
  2446. (setq ange-ftp-default-user term-ansi-at-save-user)
  2447. (setq ange-ftp-default-password term-ansi-at-save-pwd)
  2448. (setq ange-ftp-generate-anonymous-password term-ansi-at-save-anon))
  2449. (setq term-ansi-at-save-user ange-ftp-default-user)
  2450. (setq term-ansi-at-save-pwd ange-ftp-default-password)
  2451. (setq term-ansi-at-save-anon ange-ftp-generate-anonymous-password)
  2452. (setq ange-ftp-default-user nil)
  2453. (setq ange-ftp-default-password nil)
  2454. (setq ange-ftp-generate-anonymous-password nil)))))
  2455. message)
  2456. ;; Terminal emulation
  2457. ;; This is the standard process filter for term buffers.
  2458. ;; It emulates (most of the features of) a VT100/ANSI-style terminal.
  2459. (defun term-emulate-terminal (proc str)
  2460. (with-current-buffer (process-buffer proc)
  2461. (let* ((i 0) char funny
  2462. count ; number of decoded chars in substring
  2463. count-bytes ; number of bytes
  2464. decoded-substring
  2465. save-point save-marker old-point temp win
  2466. (buffer-undo-list t)
  2467. (selected (selected-window))
  2468. last-win
  2469. handled-ansi-message
  2470. (str-length (length str)))
  2471. (save-selected-window
  2472. ;; Let's handle the messages. -mm
  2473. (let* ((newstr (term-handle-ansi-terminal-messages str)))
  2474. (when (not (eq str newstr))
  2475. (setq handled-ansi-message t
  2476. str newstr)))
  2477. (setq str-length (length str))
  2478. (when (marker-buffer term-pending-delete-marker)
  2479. ;; Delete text following term-pending-delete-marker.
  2480. (delete-region term-pending-delete-marker (process-mark proc))
  2481. (set-marker term-pending-delete-marker nil))
  2482. (if (eq (window-buffer) (current-buffer))
  2483. (progn
  2484. (setq term-vertical-motion (symbol-function 'vertical-motion))
  2485. (term-check-size proc))
  2486. (setq term-vertical-motion
  2487. (symbol-function 'term-buffer-vertical-motion)))
  2488. (setq save-marker (copy-marker (process-mark proc)))
  2489. (when (/= (point) (process-mark proc))
  2490. (setq save-point (point-marker))
  2491. (goto-char (process-mark proc)))
  2492. (save-restriction
  2493. ;; If the buffer is in line mode, and there is a partial
  2494. ;; input line, save the line (by narrowing to leave it
  2495. ;; outside the restriction ) until we're done with output.
  2496. (when (and (> (point-max) (process-mark proc))
  2497. (term-in-line-mode))
  2498. (narrow-to-region (point-min) (process-mark proc)))
  2499. (when term-log-buffer
  2500. (princ str term-log-buffer))
  2501. (cond ((eq term-terminal-state 4) ;; Have saved pending output.
  2502. (setq str (concat term-terminal-parameter str))
  2503. (setq term-terminal-parameter nil)
  2504. (setq str-length (length str))
  2505. (setq term-terminal-state 0)))
  2506. (while (< i str-length)
  2507. (setq char (aref str i))
  2508. (cond ((< term-terminal-state 2)
  2509. ;; Look for prefix of regular chars
  2510. (setq funny
  2511. (string-match "[\r\n\000\007\033\t\b\032\016\017]"
  2512. str i))
  2513. (when (not funny) (setq funny str-length))
  2514. (cond ((> funny i)
  2515. ;; Decode the string before counting
  2516. ;; characters, to avoid garbling of certain
  2517. ;; multibyte characters (bug#1006).
  2518. (setq decoded-substring
  2519. (decode-coding-string
  2520. (substring str i funny)
  2521. locale-coding-system))
  2522. (cond ((eq term-terminal-state 1)
  2523. ;; We are in state 1, we need to wrap
  2524. ;; around. Go to the beginning of
  2525. ;; the next line and switch to state
  2526. ;; 0.
  2527. (term-down 1 t)
  2528. (term-move-columns (- (term-current-column)))
  2529. (setq term-terminal-state 0)))
  2530. (setq count (length decoded-substring))
  2531. (setq temp (- (+ (term-horizontal-column) count)
  2532. term-width))
  2533. (cond ((<= temp 0)) ;; All count chars fit in line.
  2534. ((> count temp) ;; Some chars fit.
  2535. ;; This iteration, handle only what fits.
  2536. (setq count (- count temp))
  2537. (setq count-bytes
  2538. (length
  2539. (encode-coding-string
  2540. (substring decoded-substring 0 count)
  2541. 'binary)))
  2542. (setq temp 0)
  2543. (setq funny (+ count-bytes i)))
  2544. ((or (not (or term-pager-count
  2545. term-scroll-with-delete))
  2546. (> (term-handle-scroll 1) 0))
  2547. (term-adjust-current-row-cache 1)
  2548. (setq count (min count term-width))
  2549. (setq count-bytes
  2550. (length
  2551. (encode-coding-string
  2552. (substring decoded-substring 0 count)
  2553. 'binary)))
  2554. (setq funny (+ count-bytes i))
  2555. (setq term-start-line-column
  2556. term-current-column))
  2557. (t ;; Doing PAGER processing.
  2558. (setq count 0 funny i)
  2559. (setq term-current-column nil)
  2560. (setq term-start-line-column nil)))
  2561. (setq old-point (point))
  2562. ;; Insert a string, check how many columns
  2563. ;; we moved, then delete that many columns
  2564. ;; following point if not eob nor insert-mode.
  2565. (let ((old-column (current-column))
  2566. columns pos)
  2567. (insert (decode-coding-string (substring str i funny) locale-coding-system))
  2568. (setq term-current-column (current-column)
  2569. columns (- term-current-column old-column))
  2570. (when (not (or (eobp) term-insert-mode))
  2571. (setq pos (point))
  2572. (term-move-columns columns)
  2573. (delete-region pos (point)))
  2574. ;; In insert mode if the current line
  2575. ;; has become too long it needs to be
  2576. ;; chopped off.
  2577. (when term-insert-mode
  2578. (setq pos (point))
  2579. (end-of-line)
  2580. (when (> (current-column) term-width)
  2581. (delete-region (- (point) (- (current-column) term-width))
  2582. (point)))
  2583. (goto-char pos)))
  2584. (setq term-current-column nil)
  2585. (put-text-property old-point (point)
  2586. 'face term-current-face)
  2587. ;; If the last char was written in last column,
  2588. ;; back up one column, but remember we did so.
  2589. ;; Thus we emulate xterm/vt100-style line-wrapping.
  2590. (cond ((eq temp 0)
  2591. (term-move-columns -1)
  2592. (setq term-terminal-state 1)))
  2593. (setq i (1- funny)))
  2594. ((and (setq term-terminal-state 0)
  2595. (eq char ?\^I)) ; TAB (terminfo: ht)
  2596. (setq count (term-current-column))
  2597. ;; The line cannot exceed term-width. TAB at
  2598. ;; the end of a line should not cause wrapping.
  2599. (setq count (min term-width
  2600. (+ count 8 (- (mod count 8)))))
  2601. (if (> term-width count)
  2602. (progn
  2603. (term-move-columns
  2604. (- count (term-current-column)))
  2605. (setq term-current-column count))
  2606. (when (> term-width (term-current-column))
  2607. (term-move-columns
  2608. (1- (- term-width (term-current-column)))))
  2609. (when (= term-width (term-current-column))
  2610. (term-move-columns -1))))
  2611. ((eq char ?\r) ;; (terminfo: cr)
  2612. (term-vertical-motion 0)
  2613. (setq term-current-column term-start-line-column))
  2614. ((eq char ?\n) ;; (terminfo: cud1, ind)
  2615. (unless (and term-kill-echo-list
  2616. (term-check-kill-echo-list))
  2617. (term-down 1 t)))
  2618. ((eq char ?\b) ;; (terminfo: cub1)
  2619. (term-move-columns -1))
  2620. ((eq char ?\033) ; Escape
  2621. (setq term-terminal-state 2))
  2622. ((eq char 0)) ; NUL: Do nothing
  2623. ((eq char ?\016)) ; Shift Out - ignored
  2624. ((eq char ?\017)) ; Shift In - ignored
  2625. ((eq char ?\^G) ;; (terminfo: bel)
  2626. (beep t))
  2627. ((and (eq char ?\032)
  2628. (not handled-ansi-message))
  2629. (let ((end (string-match "\r?$" str i)))
  2630. (if end
  2631. (funcall term-command-hook
  2632. (prog1 (substring str (1+ i) end)
  2633. (setq i (match-end 0))))
  2634. (setq term-terminal-parameter (substring str i))
  2635. (setq term-terminal-state 4)
  2636. (setq i str-length))))
  2637. (t ; insert char FIXME: Should never happen
  2638. (term-move-columns 1)
  2639. (backward-delete-char 1)
  2640. (insert char))))
  2641. ((eq term-terminal-state 2) ; Seen Esc
  2642. (cond ((eq char ?\133) ;; ?\133 = ?[
  2643. ;; Some modifications to cope with multiple
  2644. ;; settings like ^[[01;32;43m -mm
  2645. ;; Note that now the init value of
  2646. ;; term-terminal-previous-parameter has been
  2647. ;; changed to -1
  2648. (setq term-terminal-parameter 0)
  2649. (setq term-terminal-previous-parameter -1)
  2650. (setq term-terminal-previous-parameter-2 -1)
  2651. (setq term-terminal-previous-parameter-3 -1)
  2652. (setq term-terminal-previous-parameter-4 -1)
  2653. (setq term-terminal-more-parameters 0)
  2654. (setq term-terminal-state 3))
  2655. ((eq char ?D) ;; scroll forward
  2656. (term-handle-deferred-scroll)
  2657. (term-down 1 t)
  2658. (setq term-terminal-state 0))
  2659. ;; ((eq char ?E) ;; (terminfo: nw), not used for
  2660. ;; ;; now, but this is a working
  2661. ;; ;; implementation
  2662. ;; (term-down 1)
  2663. ;; (term-goto term-current-row 0)
  2664. ;; (setq term-terminal-state 0))
  2665. ((eq char ?M) ;; scroll reversed (terminfo: ri)
  2666. (if (or (< (term-current-row) term-scroll-start)
  2667. (>= (1- (term-current-row))
  2668. term-scroll-start))
  2669. ;; Scrolling up will not move outside
  2670. ;; the scroll region.
  2671. (term-down -1)
  2672. ;; Scrolling the scroll region is needed.
  2673. (term-down -1 t))
  2674. (setq term-terminal-state 0))
  2675. ((eq char ?7) ;; Save cursor (terminfo: sc)
  2676. (term-handle-deferred-scroll)
  2677. (setq term-saved-cursor
  2678. (list (term-current-row)
  2679. (term-horizontal-column)
  2680. term-ansi-current-bg-color
  2681. term-ansi-current-bold
  2682. term-ansi-current-color
  2683. term-ansi-current-invisible
  2684. term-ansi-current-reverse
  2685. term-ansi-current-underline
  2686. term-current-face)
  2687. )
  2688. (setq term-terminal-state 0))
  2689. ((eq char ?8) ;; Restore cursor (terminfo: rc)
  2690. (when term-saved-cursor
  2691. (term-goto (nth 0 term-saved-cursor)
  2692. (nth 1 term-saved-cursor))
  2693. (setq term-ansi-current-bg-color
  2694. (nth 2 term-saved-cursor)
  2695. term-ansi-current-bold
  2696. (nth 3 term-saved-cursor)
  2697. term-ansi-current-color
  2698. (nth 4 term-saved-cursor)
  2699. term-ansi-current-invisible
  2700. (nth 5 term-saved-cursor)
  2701. term-ansi-current-reverse
  2702. (nth 6 term-saved-cursor)
  2703. term-ansi-current-underline
  2704. (nth 7 term-saved-cursor)
  2705. term-current-face
  2706. (nth 8 term-saved-cursor)))
  2707. (setq term-terminal-state 0))
  2708. ((eq char ?c) ;; \Ec - Reset (terminfo: rs1)
  2709. ;; This is used by the "clear" program.
  2710. (setq term-terminal-state 0)
  2711. (term-reset-terminal))
  2712. ;; The \E#8 reset sequence for xterm. We
  2713. ;; probably don't need to handle it, but this
  2714. ;; is the code to parse it.
  2715. ;; ((eq char ?#)
  2716. ;; (when (eq (aref str (1+ i)) ?8)
  2717. ;; (setq i (1+ i))
  2718. ;; (setq term-scroll-start 0)
  2719. ;; (setq term-scroll-end term-height)
  2720. ;; (setq term-terminal-state 0)))
  2721. ((setq term-terminal-state 0))))
  2722. ((eq term-terminal-state 3) ; Seen Esc [
  2723. (cond ((and (>= char ?0) (<= char ?9))
  2724. (setq term-terminal-parameter
  2725. (+ (* 10 term-terminal-parameter) (- char ?0))))
  2726. ((eq char ?\;)
  2727. ;; Some modifications to cope with multiple
  2728. ;; settings like ^[[01;32;43m -mm
  2729. (setq term-terminal-more-parameters 1)
  2730. (setq term-terminal-previous-parameter-4
  2731. term-terminal-previous-parameter-3)
  2732. (setq term-terminal-previous-parameter-3
  2733. term-terminal-previous-parameter-2)
  2734. (setq term-terminal-previous-parameter-2
  2735. term-terminal-previous-parameter)
  2736. (setq term-terminal-previous-parameter
  2737. term-terminal-parameter)
  2738. (setq term-terminal-parameter 0))
  2739. ((eq char ??)) ; Ignore ?
  2740. (t
  2741. (term-handle-ansi-escape proc char)
  2742. (setq term-terminal-more-parameters 0)
  2743. (setq term-terminal-previous-parameter-4 -1)
  2744. (setq term-terminal-previous-parameter-3 -1)
  2745. (setq term-terminal-previous-parameter-2 -1)
  2746. (setq term-terminal-previous-parameter -1)
  2747. (setq term-terminal-state 0)))))
  2748. (when (term-handling-pager)
  2749. ;; Finish stuff to get ready to handle PAGER.
  2750. (if (> (% (current-column) term-width) 0)
  2751. (setq term-terminal-parameter
  2752. (substring str i))
  2753. ;; We're at column 0. Goto end of buffer; to compensate,
  2754. ;; prepend a ?\r for later. This looks more consistent.
  2755. (if (zerop i)
  2756. (setq term-terminal-parameter
  2757. (concat "\r" (substring str i)))
  2758. (setq term-terminal-parameter (substring str (1- i)))
  2759. (aset term-terminal-parameter 0 ?\r))
  2760. (goto-char (point-max)))
  2761. (setq term-terminal-state 4)
  2762. (make-local-variable 'term-pager-old-filter)
  2763. (setq term-pager-old-filter (process-filter proc))
  2764. (set-process-filter proc term-pager-filter)
  2765. (setq i str-length))
  2766. (setq i (1+ i))))
  2767. (when (>= (term-current-row) term-height)
  2768. (term-handle-deferred-scroll))
  2769. (set-marker (process-mark proc) (point))
  2770. (when save-point
  2771. (goto-char save-point)
  2772. (set-marker save-point nil))
  2773. ;; Check for a pending filename-and-line number to display.
  2774. ;; We do this before scrolling, because we might create a new window.
  2775. (when (and term-pending-frame
  2776. (eq (window-buffer selected) (current-buffer)))
  2777. (term-display-line (car term-pending-frame)
  2778. (cdr term-pending-frame))
  2779. (setq term-pending-frame nil)
  2780. ;; We have created a new window, so check the window size.
  2781. (term-check-size proc))
  2782. ;; Scroll each window displaying the buffer but (by default)
  2783. ;; only if the point matches the process-mark we started with.
  2784. (setq win selected)
  2785. ;; Avoid infinite loop in strange case where minibuffer window
  2786. ;; is selected but not active.
  2787. (while (window-minibuffer-p win)
  2788. (setq win (next-window win nil t)))
  2789. (setq last-win win)
  2790. (while (progn
  2791. (setq win (next-window win nil t))
  2792. (when (eq (window-buffer win) (process-buffer proc))
  2793. (let ((scroll term-scroll-to-bottom-on-output))
  2794. (select-window win)
  2795. (when (or (= (point) save-marker)
  2796. (eq scroll t) (eq scroll 'all)
  2797. ;; Maybe user wants point to jump to the end.
  2798. (and (eq selected win)
  2799. (or (eq scroll 'this) (not save-point)))
  2800. (and (eq scroll 'others)
  2801. (not (eq selected win))))
  2802. (goto-char term-home-marker)
  2803. (recenter 0)
  2804. (goto-char (process-mark proc))
  2805. (if (not (pos-visible-in-window-p (point) win))
  2806. (recenter -1)))
  2807. ;; Optionally scroll so that the text
  2808. ;; ends at the bottom of the window.
  2809. (when (and term-scroll-show-maximum-output
  2810. (>= (point) (process-mark proc)))
  2811. (save-excursion
  2812. (goto-char (point-max))
  2813. (recenter -1)))))
  2814. (not (eq win last-win))))
  2815. ;; Stolen from comint.el and adapted -mm
  2816. (when (> term-buffer-maximum-size 0)
  2817. (save-excursion
  2818. (goto-char (process-mark (get-buffer-process (current-buffer))))
  2819. (forward-line (- term-buffer-maximum-size))
  2820. (beginning-of-line)
  2821. (delete-region (point-min) (point))))
  2822. (set-marker save-marker nil)))
  2823. ;; This might be expensive, but we need it to handle something
  2824. ;; like `sleep 5 | less -c' in more-or-less real time.
  2825. (when (get-buffer-window (current-buffer))
  2826. (redisplay))))
  2827. (defun term-handle-deferred-scroll ()
  2828. (let ((count (- (term-current-row) term-height)))
  2829. (when (>= count 0)
  2830. (save-excursion
  2831. (goto-char term-home-marker)
  2832. (term-vertical-motion (1+ count))
  2833. (set-marker term-home-marker (point))
  2834. (setq term-current-row (1- term-height))))))
  2835. (defun term-reset-terminal ()
  2836. "Reset the terminal, delete all the content and set the face to the default one."
  2837. (erase-buffer)
  2838. (term-ansi-reset)
  2839. (setq term-current-row 0)
  2840. (setq term-current-column 1)
  2841. (setq term-scroll-start 0)
  2842. (setq term-scroll-end term-height)
  2843. (setq term-insert-mode nil)
  2844. ;; FIXME: No idea why this is here, it looks wrong. --Stef
  2845. (setq term-ansi-face-already-done nil))
  2846. ;; New function to deal with ansi colorized output, as you can see you can
  2847. ;; have any bold/underline/fg/bg/reverse combination. -mm
  2848. (defvar term-bold-attribute '(:weight bold)
  2849. "Attribute to use for the bold terminal attribute.
  2850. Set it to nil to disable bold.")
  2851. (defun term-handle-colors-array (parameter)
  2852. (cond
  2853. ;; Bold (terminfo: bold)
  2854. ((eq parameter 1)
  2855. (setq term-ansi-current-bold t))
  2856. ;; Underline
  2857. ((eq parameter 4)
  2858. (setq term-ansi-current-underline t))
  2859. ;; Blink (unsupported by Emacs), will be translated to bold.
  2860. ;; This may change in the future though.
  2861. ((eq parameter 5)
  2862. (setq term-ansi-current-bold t))
  2863. ;; Reverse (terminfo: smso)
  2864. ((eq parameter 7)
  2865. (setq term-ansi-current-reverse t))
  2866. ;; Invisible
  2867. ((eq parameter 8)
  2868. (setq term-ansi-current-invisible t))
  2869. ;; Reset underline (terminfo: rmul)
  2870. ((eq parameter 24)
  2871. (setq term-ansi-current-underline nil))
  2872. ;; Reset reverse (terminfo: rmso)
  2873. ((eq parameter 27)
  2874. (setq term-ansi-current-reverse nil))
  2875. ;; Foreground
  2876. ((and (>= parameter 30) (<= parameter 37))
  2877. (setq term-ansi-current-color (- parameter 29)))
  2878. ;; Reset foreground
  2879. ((eq parameter 39)
  2880. (setq term-ansi-current-color 0))
  2881. ;; Background
  2882. ((and (>= parameter 40) (<= parameter 47))
  2883. (setq term-ansi-current-bg-color (- parameter 39)))
  2884. ;; Reset background
  2885. ((eq parameter 49)
  2886. (setq term-ansi-current-bg-color 0))
  2887. ;; 0 (Reset) or unknown (reset anyway)
  2888. (t
  2889. (term-ansi-reset)))
  2890. ;; (message "Debug: U-%d R-%d B-%d I-%d D-%d F-%d B-%d"
  2891. ;; term-ansi-current-underline
  2892. ;; term-ansi-current-reverse
  2893. ;; term-ansi-current-bold
  2894. ;; term-ansi-current-invisible
  2895. ;; term-ansi-face-already-done
  2896. ;; term-ansi-current-color
  2897. ;; term-ansi-current-bg-color)
  2898. (unless term-ansi-face-already-done
  2899. (if term-ansi-current-invisible
  2900. (let ((color
  2901. (if term-ansi-current-reverse
  2902. (if (= term-ansi-current-color 0)
  2903. term-default-fg-color
  2904. (elt ansi-term-color-vector term-ansi-current-color))
  2905. (if (= term-ansi-current-bg-color 0)
  2906. term-default-bg-color
  2907. (elt ansi-term-color-vector term-ansi-current-bg-color)))))
  2908. (setq term-current-face
  2909. (list :background color
  2910. :foreground color))
  2911. ) ;; No need to bother with anything else if it's invisible.
  2912. (setq term-current-face
  2913. (if term-ansi-current-reverse
  2914. (if (= term-ansi-current-color 0)
  2915. (list :background term-default-fg-color
  2916. :foreground term-default-bg-color)
  2917. (list :background
  2918. (elt ansi-term-color-vector term-ansi-current-color)
  2919. :foreground
  2920. (elt ansi-term-color-vector term-ansi-current-bg-color)))
  2921. (if (= term-ansi-current-color 0)
  2922. (list :foreground term-default-fg-color
  2923. :background term-default-bg-color)
  2924. (list :foreground
  2925. (elt ansi-term-color-vector term-ansi-current-color)
  2926. :background
  2927. (elt ansi-term-color-vector term-ansi-current-bg-color)))))
  2928. (when term-ansi-current-bold
  2929. (setq term-current-face
  2930. (append term-bold-attribute term-current-face)))
  2931. (when term-ansi-current-underline
  2932. (setq term-current-face
  2933. (list* :underline t term-current-face)))))
  2934. ;; (message "Debug %S" term-current-face)
  2935. ;; FIXME: shouldn't we set term-ansi-face-already-done to t here? --Stef
  2936. (setq term-ansi-face-already-done nil))
  2937. ;; Handle a character assuming (eq terminal-state 2) -
  2938. ;; i.e. we have previously seen Escape followed by ?[.
  2939. (defun term-handle-ansi-escape (proc char)
  2940. (cond
  2941. ((or (eq char ?H) ;; cursor motion (terminfo: cup,home)
  2942. ;; (eq char ?f) ;; xterm seems to handle this sequence too, not
  2943. ;; needed for now
  2944. )
  2945. (when (<= term-terminal-parameter 0)
  2946. (setq term-terminal-parameter 1))
  2947. (when (<= term-terminal-previous-parameter 0)
  2948. (setq term-terminal-previous-parameter 1))
  2949. (when (> term-terminal-previous-parameter term-height)
  2950. (setq term-terminal-previous-parameter term-height))
  2951. (when (> term-terminal-parameter term-width)
  2952. (setq term-terminal-parameter term-width))
  2953. (term-goto
  2954. (1- term-terminal-previous-parameter)
  2955. (1- term-terminal-parameter)))
  2956. ;; \E[A - cursor up (terminfo: cuu, cuu1)
  2957. ((eq char ?A)
  2958. (term-handle-deferred-scroll)
  2959. (let ((tcr (term-current-row)))
  2960. (term-down
  2961. (if (< (- tcr term-terminal-parameter) term-scroll-start)
  2962. ;; If the amount to move is before scroll start, move
  2963. ;; to scroll start.
  2964. (- term-scroll-start tcr)
  2965. (if (>= term-terminal-parameter tcr)
  2966. (- tcr)
  2967. (- (max 1 term-terminal-parameter)))) t)))
  2968. ;; \E[B - cursor down (terminfo: cud)
  2969. ((eq char ?B)
  2970. (let ((tcr (term-current-row)))
  2971. (unless (= tcr (1- term-scroll-end))
  2972. (term-down
  2973. (if (> (+ tcr term-terminal-parameter) term-scroll-end)
  2974. (- term-scroll-end 1 tcr)
  2975. (max 1 term-terminal-parameter)) t))))
  2976. ;; \E[C - cursor right (terminfo: cuf, cuf1)
  2977. ((eq char ?C)
  2978. (term-move-columns
  2979. (max 1
  2980. (if (>= (+ term-terminal-parameter (term-current-column)) term-width)
  2981. (- term-width (term-current-column) 1)
  2982. term-terminal-parameter))))
  2983. ;; \E[D - cursor left (terminfo: cub)
  2984. ((eq char ?D)
  2985. (term-move-columns (- (max 1 term-terminal-parameter))))
  2986. ;; \E[J - clear to end of screen (terminfo: ed, clear)
  2987. ((eq char ?J)
  2988. (term-erase-in-display term-terminal-parameter))
  2989. ;; \E[K - clear to end of line (terminfo: el, el1)
  2990. ((eq char ?K)
  2991. (term-erase-in-line term-terminal-parameter))
  2992. ;; \E[L - insert lines (terminfo: il, il1)
  2993. ((eq char ?L)
  2994. (term-insert-lines (max 1 term-terminal-parameter)))
  2995. ;; \E[M - delete lines (terminfo: dl, dl1)
  2996. ((eq char ?M)
  2997. (term-delete-lines (max 1 term-terminal-parameter)))
  2998. ;; \E[P - delete chars (terminfo: dch, dch1)
  2999. ((eq char ?P)
  3000. (term-delete-chars (max 1 term-terminal-parameter)))
  3001. ;; \E[@ - insert spaces (terminfo: ich)
  3002. ((eq char ?@)
  3003. (term-insert-spaces (max 1 term-terminal-parameter)))
  3004. ;; \E[?h - DEC Private Mode Set
  3005. ((eq char ?h)
  3006. (cond ((eq term-terminal-parameter 4) ;; (terminfo: smir)
  3007. (setq term-insert-mode t))
  3008. ;; ((eq term-terminal-parameter 47) ;; (terminfo: smcup)
  3009. ;; (term-switch-to-alternate-sub-buffer t))
  3010. ))
  3011. ;; \E[?l - DEC Private Mode Reset
  3012. ((eq char ?l)
  3013. (cond ((eq term-terminal-parameter 4) ;; (terminfo: rmir)
  3014. (setq term-insert-mode nil))
  3015. ;; ((eq term-terminal-parameter 47) ;; (terminfo: rmcup)
  3016. ;; (term-switch-to-alternate-sub-buffer nil))
  3017. ))
  3018. ;; Modified to allow ansi coloring -mm
  3019. ;; \E[m - Set/reset modes, set bg/fg
  3020. ;;(terminfo: smso,rmso,smul,rmul,rev,bold,sgr0,invis,op,setab,setaf)
  3021. ((eq char ?m)
  3022. (when (= term-terminal-more-parameters 1)
  3023. (when (>= term-terminal-previous-parameter-4 0)
  3024. (term-handle-colors-array term-terminal-previous-parameter-4))
  3025. (when (>= term-terminal-previous-parameter-3 0)
  3026. (term-handle-colors-array term-terminal-previous-parameter-3))
  3027. (when (>= term-terminal-previous-parameter-2 0)
  3028. (term-handle-colors-array term-terminal-previous-parameter-2))
  3029. (term-handle-colors-array term-terminal-previous-parameter))
  3030. (term-handle-colors-array term-terminal-parameter))
  3031. ;; \E[6n - Report cursor position (terminfo: u7)
  3032. ((eq char ?n)
  3033. (term-handle-deferred-scroll)
  3034. (process-send-string proc
  3035. ;; (terminfo: u6)
  3036. (format "\e[%s;%sR"
  3037. (1+ (term-current-row))
  3038. (1+ (term-horizontal-column)))))
  3039. ;; \E[r - Set scrolling region (terminfo: csr)
  3040. ((eq char ?r)
  3041. (term-set-scroll-region
  3042. (1- term-terminal-previous-parameter)
  3043. (1- term-terminal-parameter)))
  3044. (t)))
  3045. (defun term-set-scroll-region (top bottom)
  3046. "Set scrolling region.
  3047. TOP is the top-most line (inclusive) of the new scrolling region,
  3048. while BOTTOM is the line following the new scrolling region (e.g. exclusive).
  3049. The top-most line is line 0."
  3050. (setq term-scroll-start
  3051. (if (or (< top 0) (>= top term-height))
  3052. 0
  3053. top))
  3054. (setq term-scroll-end
  3055. (if (or (<= bottom term-scroll-start) (> bottom term-height))
  3056. term-height
  3057. bottom))
  3058. (setq term-scroll-with-delete
  3059. (or (term-using-alternate-sub-buffer)
  3060. (not (and (= term-scroll-start 0)
  3061. (= term-scroll-end term-height)))))
  3062. (term-move-columns (- (term-current-column)))
  3063. (term-goto 0 0))
  3064. ;; (defun term-switch-to-alternate-sub-buffer (set)
  3065. ;; ;; If asked to switch to (from) the alternate sub-buffer, and already (not)
  3066. ;; ;; using it, do nothing. This test is needed for some programs (including
  3067. ;; ;; Emacs) that emit the ti termcap string twice, for unknown reason.
  3068. ;; (term-handle-deferred-scroll)
  3069. ;; (if (eq set (not (term-using-alternate-sub-buffer)))
  3070. ;; (let ((row (term-current-row))
  3071. ;; (col (term-horizontal-column)))
  3072. ;; (cond (set
  3073. ;; (goto-char (point-max))
  3074. ;; (if (not (eq (preceding-char) ?\n))
  3075. ;; (term-insert-char ?\n 1))
  3076. ;; (setq term-scroll-with-delete t)
  3077. ;; (setq term-saved-home-marker (copy-marker term-home-marker))
  3078. ;; (set-marker term-home-marker (point)))
  3079. ;; (t
  3080. ;; (setq term-scroll-with-delete
  3081. ;; (not (and (= term-scroll-start 0)
  3082. ;; (= term-scroll-end term-height))))
  3083. ;; (set-marker term-home-marker term-saved-home-marker)
  3084. ;; (set-marker term-saved-home-marker nil)
  3085. ;; (setq term-saved-home-marker nil)
  3086. ;; (goto-char term-home-marker)))
  3087. ;; (setq term-current-column nil)
  3088. ;; (setq term-current-row 0)
  3089. ;; (term-goto row col))))
  3090. ;; Default value for the symbol term-command-hook.
  3091. (defun term-command-hook (string)
  3092. (cond ((equal string "")
  3093. t)
  3094. ((= (aref string 0) ?\032)
  3095. ;; gdb (when invoked with -fullname) prints:
  3096. ;; \032\032FULLFILENAME:LINENUMBER:CHARPOS:BEG_OR_MIDDLE:PC\n
  3097. (let* ((first-colon (string-match ":" string 1))
  3098. (second-colon
  3099. (string-match ":" string (1+ first-colon)))
  3100. (filename (substring string 1 first-colon))
  3101. (fileline (string-to-number
  3102. (substring string (1+ first-colon) second-colon))))
  3103. (setq term-pending-frame (cons filename fileline))))
  3104. ((= (aref string 0) ?/)
  3105. (cd (substring string 1)))
  3106. ;; Allowing the inferior to call functions in Emacs is
  3107. ;; probably too big a security hole.
  3108. ;; ((= (aref string 0) ?!)
  3109. ;; (eval (car (read-from-string string 1))))
  3110. (t)));; Otherwise ignore it
  3111. ;; Make sure the file named TRUE-FILE is in a buffer that appears on the screen
  3112. ;; and that its line LINE is visible.
  3113. ;; Put the overlay-arrow on the line LINE in that buffer.
  3114. ;; This is mainly used by gdb.
  3115. (defun term-display-line (true-file line)
  3116. (term-display-buffer-line (find-file-noselect true-file) line))
  3117. (defun term-display-buffer-line (buffer line)
  3118. (let* ((window (display-buffer buffer t))
  3119. (pos))
  3120. (with-current-buffer buffer
  3121. (save-restriction
  3122. (widen)
  3123. (goto-char (point-min))
  3124. (forward-line (1- line))
  3125. (setq pos (point))
  3126. (setq overlay-arrow-string "=>")
  3127. (or overlay-arrow-position
  3128. (setq overlay-arrow-position (make-marker)))
  3129. (set-marker overlay-arrow-position (point) (current-buffer)))
  3130. (cond ((or (< pos (point-min)) (> pos (point-max)))
  3131. (widen)
  3132. (goto-char pos))))
  3133. (set-window-point window overlay-arrow-position)))
  3134. ;; The buffer-local marker term-home-marker defines the "home position"
  3135. ;; (in terms of cursor motion). However, we move the term-home-marker
  3136. ;; "down" as needed so that is no more that a window-full above (point-max).
  3137. (defun term-goto-home ()
  3138. (term-handle-deferred-scroll)
  3139. (goto-char term-home-marker)
  3140. (setq term-current-row 0)
  3141. (setq term-current-column (current-column))
  3142. (setq term-start-line-column term-current-column))
  3143. (defun term-goto (row col)
  3144. (term-handle-deferred-scroll)
  3145. (cond ((and term-current-row (>= row term-current-row))
  3146. ;; I assume this is a worthwhile optimization.
  3147. (term-vertical-motion 0)
  3148. (setq term-current-column term-start-line-column)
  3149. (setq row (- row term-current-row)))
  3150. (t
  3151. (term-goto-home)))
  3152. (term-down row)
  3153. (term-move-columns col))
  3154. ;; The page is full, so enter "pager" mode, and wait for input.
  3155. (defun term-process-pager ()
  3156. (when (not term-pager-break-map)
  3157. (let* ((map (make-keymap))
  3158. ;; (i 0)
  3159. tmp)
  3160. ;; (while (< i 128)
  3161. ;; (define-key map (make-string 1 i) 'term-send-raw)
  3162. ;; (setq i (1+ i)))
  3163. (define-key map "\e"
  3164. (lookup-key (current-global-map) "\e"))
  3165. (define-key map "\C-x"
  3166. (lookup-key (current-global-map) "\C-x"))
  3167. (define-key map "\C-u"
  3168. (lookup-key (current-global-map) "\C-u"))
  3169. (define-key map " " 'term-pager-page)
  3170. (define-key map "\r" 'term-pager-line)
  3171. (define-key map "?" 'term-pager-help)
  3172. (define-key map "h" 'term-pager-help)
  3173. (define-key map "b" 'term-pager-back-page)
  3174. (define-key map "\177" 'term-pager-back-line)
  3175. (define-key map "q" 'term-pager-discard)
  3176. (define-key map "D" 'term-pager-disable)
  3177. (define-key map "<" 'term-pager-bob)
  3178. (define-key map ">" 'term-pager-eob)
  3179. ;; Add menu bar.
  3180. (unless (featurep 'xemacs)
  3181. (define-key map [menu-bar terminal] term-terminal-menu)
  3182. (define-key map [menu-bar signals] term-signals-menu)
  3183. (setq tmp (make-sparse-keymap "More pages?"))
  3184. (define-key tmp [help] '("Help" . term-pager-help))
  3185. (define-key tmp [disable]
  3186. '("Disable paging" . term-fake-pager-disable))
  3187. (define-key tmp [discard]
  3188. '("Discard remaining output" . term-pager-discard))
  3189. (define-key tmp [eob] '("Goto to end" . term-pager-eob))
  3190. (define-key tmp [bob] '("Goto to beginning" . term-pager-bob))
  3191. (define-key tmp [line] '("1 line forwards" . term-pager-line))
  3192. (define-key tmp [bline] '("1 line backwards" . term-pager-back-line))
  3193. (define-key tmp [back] '("1 page backwards" . term-pager-back-page))
  3194. (define-key tmp [page] '("1 page forwards" . term-pager-page))
  3195. (define-key map [menu-bar page] (cons "More pages?" tmp))
  3196. )
  3197. (setq term-pager-break-map map)))
  3198. ;; (let ((process (get-buffer-process (current-buffer))))
  3199. ;; (stop-process process))
  3200. (setq term-pager-old-local-map (current-local-map))
  3201. (use-local-map term-pager-break-map)
  3202. (make-local-variable 'term-old-mode-line-format)
  3203. (setq term-old-mode-line-format mode-line-format)
  3204. (setq mode-line-format
  3205. (list "-- **MORE** "
  3206. mode-line-buffer-identification
  3207. " [Type ? for help] "
  3208. "%-"))
  3209. (force-mode-line-update))
  3210. (defun term-pager-line (lines)
  3211. (interactive "p")
  3212. (let* ((moved (vertical-motion (1+ lines)))
  3213. (deficit (- lines moved)))
  3214. (when (> moved lines)
  3215. (backward-char))
  3216. (cond ((<= deficit 0) ;; OK, had enough in the buffer for request.
  3217. (recenter (1- term-height)))
  3218. ((term-pager-continue deficit)))))
  3219. (defun term-pager-page (arg)
  3220. "Proceed past the **MORE** break, allowing the next page of output to appear."
  3221. (interactive "p")
  3222. (term-pager-line (* arg term-height)))
  3223. ;; Pager mode command to go to beginning of buffer.
  3224. (defun term-pager-bob ()
  3225. (interactive)
  3226. (goto-char (point-min))
  3227. (when (= (vertical-motion term-height) term-height)
  3228. (backward-char))
  3229. (recenter (1- term-height)))
  3230. ;; Pager mode command to go to end of buffer.
  3231. (defun term-pager-eob ()
  3232. (interactive)
  3233. (goto-char term-home-marker)
  3234. (recenter 0)
  3235. (goto-char (process-mark (get-buffer-process (current-buffer)))))
  3236. (defun term-pager-back-line (lines)
  3237. (interactive "p")
  3238. (vertical-motion (- 1 lines))
  3239. (if (not (bobp))
  3240. (backward-char)
  3241. (beep)
  3242. ;; Move cursor to end of window.
  3243. (vertical-motion term-height)
  3244. (backward-char))
  3245. (recenter (1- term-height)))
  3246. (defun term-pager-back-page (arg)
  3247. (interactive "p")
  3248. (term-pager-back-line (* arg term-height)))
  3249. (defun term-pager-discard ()
  3250. (interactive)
  3251. (setq term-terminal-parameter "")
  3252. (interrupt-process nil t)
  3253. (term-pager-continue term-height))
  3254. ;; Disable pager processing.
  3255. ;; Only callable while in pager mode. (Contrast term-disable-pager.)
  3256. (defun term-pager-disable ()
  3257. (interactive)
  3258. (if (term-handling-pager)
  3259. (term-pager-continue nil)
  3260. (setq term-pager-count nil))
  3261. (term-update-mode-line))
  3262. ;; Enable pager processing.
  3263. (defun term-pager-enable ()
  3264. (interactive)
  3265. (or (term-pager-enabled)
  3266. (setq term-pager-count 0)) ;; Or maybe set to (term-current-row) ??
  3267. (term-update-mode-line))
  3268. (defun term-pager-toggle ()
  3269. (interactive)
  3270. (if (term-pager-enabled) (term-pager-disable) (term-pager-enable)))
  3271. (unless (featurep 'xemacs)
  3272. (defalias 'term-fake-pager-enable 'term-pager-toggle)
  3273. (defalias 'term-fake-pager-disable 'term-pager-toggle)
  3274. (put 'term-char-mode 'menu-enable '(term-in-line-mode))
  3275. (put 'term-line-mode 'menu-enable '(term-in-char-mode))
  3276. (put 'term-fake-pager-enable 'menu-enable '(not term-pager-count))
  3277. (put 'term-fake-pager-disable 'menu-enable 'term-pager-count))
  3278. (defun term-pager-help ()
  3279. "Provide help on commands available in a terminal-emulator **MORE** break."
  3280. (interactive)
  3281. (message "Terminal-emulator pager break help...")
  3282. (sit-for 0)
  3283. (with-electric-help
  3284. (function (lambda ()
  3285. (princ (substitute-command-keys
  3286. "\\<term-pager-break-map>\
  3287. Terminal-emulator MORE break.\n\
  3288. Type one of the following keys:\n\n\
  3289. \\[term-pager-page]\t\tMove forward one page.\n\
  3290. \\[term-pager-line]\t\tMove forward one line.\n\
  3291. \\[universal-argument] N \\[term-pager-page]\tMove N pages forward.\n\
  3292. \\[universal-argument] N \\[term-pager-line]\tMove N lines forward.\n\
  3293. \\[universal-argument] N \\[term-pager-back-line]\tMove N lines back.\n\
  3294. \\[universal-argument] N \\[term-pager-back-page]\t\tMove N pages back.\n\
  3295. \\[term-pager-bob]\t\tMove to the beginning of the buffer.\n\
  3296. \\[term-pager-eob]\t\tMove to the end of the buffer.\n\
  3297. \\[term-pager-discard]\t\tKill pending output and kill process.\n\
  3298. \\[term-pager-disable]\t\tDisable PAGER handling.\n\n\
  3299. \\{term-pager-break-map}\n\
  3300. Any other key is passed through to the program
  3301. running under the terminal emulator and disables pager processing until
  3302. all pending output has been dealt with."))
  3303. nil))))
  3304. (defun term-pager-continue (new-count)
  3305. (let ((process (get-buffer-process (current-buffer))))
  3306. (use-local-map term-pager-old-local-map)
  3307. (setq term-pager-old-local-map nil)
  3308. (setq mode-line-format term-old-mode-line-format)
  3309. (force-mode-line-update)
  3310. (setq term-pager-count new-count)
  3311. (set-process-filter process term-pager-old-filter)
  3312. (funcall term-pager-old-filter process "")
  3313. (continue-process process)))
  3314. ;; Make sure there are DOWN blank lines below the current one.
  3315. ;; Return 0 if we're unable (because of PAGER handling), else return DOWN.
  3316. (defun term-handle-scroll (down)
  3317. (let ((scroll-needed
  3318. (- (+ (term-current-row) down)
  3319. (if (< down 0) term-scroll-start term-scroll-end))))
  3320. (when (or (and (< down 0) (< scroll-needed 0))
  3321. (and (> down 0) (> scroll-needed 0)))
  3322. (let ((save-point (copy-marker (point))) (save-top))
  3323. (goto-char term-home-marker)
  3324. (cond (term-scroll-with-delete
  3325. (if (< down 0)
  3326. (progn
  3327. ;; Delete scroll-needed lines at term-scroll-end,
  3328. ;; then insert scroll-needed lines.
  3329. (term-vertical-motion term-scroll-end)
  3330. (end-of-line)
  3331. (setq save-top (point))
  3332. (term-vertical-motion scroll-needed)
  3333. (end-of-line)
  3334. (delete-region save-top (point))
  3335. (goto-char save-point)
  3336. (setq down (- scroll-needed down))
  3337. (term-vertical-motion down))
  3338. ;; Delete scroll-needed lines at term-scroll-start.
  3339. (term-vertical-motion term-scroll-start)
  3340. (setq save-top (point))
  3341. (term-vertical-motion scroll-needed)
  3342. (delete-region save-top (point))
  3343. (goto-char save-point)
  3344. (term-vertical-motion down)
  3345. (term-adjust-current-row-cache (- scroll-needed)))
  3346. (setq term-current-column nil)
  3347. (term-insert-char ?\n (abs scroll-needed)))
  3348. ((and (numberp term-pager-count)
  3349. (< (setq term-pager-count (- term-pager-count down))
  3350. 0))
  3351. (setq down 0)
  3352. (term-process-pager))
  3353. (t
  3354. (term-adjust-current-row-cache (- scroll-needed))
  3355. (term-vertical-motion scroll-needed)
  3356. (set-marker term-home-marker (point))))
  3357. (goto-char save-point)
  3358. (set-marker save-point nil))))
  3359. down)
  3360. (defun term-down (down &optional check-for-scroll)
  3361. "Move down DOWN screen lines vertically."
  3362. (let ((start-column (term-horizontal-column)))
  3363. (when (and check-for-scroll (or term-scroll-with-delete term-pager-count))
  3364. (setq down (term-handle-scroll down)))
  3365. (unless (and (= term-current-row 0) (< down 0))
  3366. (term-adjust-current-row-cache down)
  3367. (when (or (/= (point) (point-max)) (< down 0))
  3368. (setq down (- down (term-vertical-motion down)))))
  3369. (cond ((>= down 0)
  3370. ;; Extend buffer with extra blank lines if needed.
  3371. (term-insert-char ?\n down)
  3372. (setq term-current-column 0)
  3373. (setq term-start-line-column 0))
  3374. (t
  3375. (when (= term-current-row 0)
  3376. ;; Insert lines if at the beginning.
  3377. (save-excursion (term-insert-char ?\n (- down)))
  3378. (save-excursion
  3379. (let (p)
  3380. ;; Delete lines from the end.
  3381. (forward-line term-height)
  3382. (setq p (point))
  3383. (forward-line (- down))
  3384. (delete-region p (point)))))
  3385. (setq term-current-column 0)
  3386. (setq term-start-line-column (current-column))))
  3387. (when start-column
  3388. (term-move-columns start-column))))
  3389. ;; Assuming point is at the beginning of a screen line,
  3390. ;; if the line above point wraps around, add a ?\n to undo the wrapping.
  3391. ;; FIXME: Probably should be called more than it is.
  3392. (defun term-unwrap-line ()
  3393. (when (not (bolp)) (insert-before-markers ?\n)))
  3394. (defun term-erase-in-line (kind)
  3395. (when (= kind 1) ;; erase left of point
  3396. (let ((cols (term-horizontal-column)) (saved-point (point)))
  3397. (term-vertical-motion 0)
  3398. (delete-region (point) saved-point)
  3399. (term-insert-char ? cols)))
  3400. (when (not (eq kind 1)) ;; erase right of point
  3401. (let ((saved-point (point))
  3402. (wrapped (and (zerop (term-horizontal-column))
  3403. (not (zerop (term-current-column))))))
  3404. (term-vertical-motion 1)
  3405. (delete-region saved-point (point))
  3406. ;; wrapped is true if we're at the beginning of screen line,
  3407. ;; but not a buffer line. If we delete the current screen line
  3408. ;; that will make the previous line no longer wrap, and (because
  3409. ;; of the way Emacs display works) point will be at the end of
  3410. ;; the previous screen line rather then the beginning of the
  3411. ;; current one. To avoid that, we make sure that current line
  3412. ;; contain a space, to force the previous line to continue to wrap.
  3413. ;; We could do this always, but it seems preferable to not add the
  3414. ;; extra space when wrapped is false.
  3415. (when wrapped
  3416. (insert ? ))
  3417. (insert ?\n)
  3418. (put-text-property saved-point (point) 'face 'default)
  3419. (goto-char saved-point))))
  3420. (defun term-erase-in-display (kind)
  3421. "Erase (that is blank out) part of the window.
  3422. If KIND is 0, erase from (point) to (point-max);
  3423. if KIND is 1, erase from home to point; else erase from home to point-max."
  3424. (term-handle-deferred-scroll)
  3425. (cond ((eq term-terminal-parameter 0)
  3426. (let ((need-unwrap (bolp)))
  3427. (delete-region (point) (point-max))
  3428. (when need-unwrap (term-unwrap-line))))
  3429. ((let ((row (term-current-row))
  3430. (col (term-horizontal-column))
  3431. (start-region term-home-marker)
  3432. (end-region (if (eq kind 1) (point) (point-max))))
  3433. (delete-region start-region end-region)
  3434. (term-unwrap-line)
  3435. (when (eq kind 1)
  3436. (term-insert-char ?\n row))
  3437. (setq term-current-column nil)
  3438. (setq term-current-row nil)
  3439. (term-goto row col)))))
  3440. (defun term-delete-chars (count)
  3441. (let ((save-point (point)))
  3442. (term-vertical-motion 1)
  3443. (term-unwrap-line)
  3444. (goto-char save-point)
  3445. (move-to-column (+ (term-current-column) count) t)
  3446. (delete-region save-point (point))))
  3447. ;; Insert COUNT spaces after point, but do not change any of
  3448. ;; following screen lines. Hence we may have to delete characters
  3449. ;; at the end of this screen line to make room.
  3450. (defun term-insert-spaces (count)
  3451. (let ((save-point (point)) (save-eol) (pnt-at-eol))
  3452. (term-vertical-motion 1)
  3453. (when (bolp)
  3454. (backward-char))
  3455. (setq save-eol (point)
  3456. pnt-at-eol (line-end-position))
  3457. (move-to-column (+ (term-start-line-column) (- term-width count)) t)
  3458. ;; If move-to-column extends the current line it will use the face
  3459. ;; from the last character on the line, set the face for the chars
  3460. ;; to default.
  3461. (when (>= (point) pnt-at-eol)
  3462. (put-text-property pnt-at-eol (point) 'face 'default))
  3463. (when (> save-eol (point))
  3464. (delete-region (point) save-eol))
  3465. (goto-char save-point)
  3466. (term-insert-char ? count)
  3467. (goto-char save-point)))
  3468. (defun term-delete-lines (lines)
  3469. (let ((start (point))
  3470. (save-current-column term-current-column)
  3471. (save-start-line-column term-start-line-column)
  3472. (save-current-row (term-current-row)))
  3473. ;; The number of inserted lines shouldn't exceed the scroll region end.
  3474. ;; The `term-scroll-end' line is part of the scrolling region, so
  3475. ;; we need to go one line past it in order to ensure correct
  3476. ;; scrolling.
  3477. (when (> (+ save-current-row lines) (1+ term-scroll-end))
  3478. (setq lines (- lines (- (+ save-current-row lines) (1+ term-scroll-end)))))
  3479. (term-down lines)
  3480. (delete-region start (point))
  3481. (term-down (- (1+ term-scroll-end) save-current-row lines))
  3482. (term-insert-char ?\n lines)
  3483. (setq term-current-column save-current-column)
  3484. (setq term-start-line-column save-start-line-column)
  3485. (setq term-current-row save-current-row)
  3486. (goto-char start)))
  3487. (defun term-insert-lines (lines)
  3488. (let ((start (point))
  3489. (start-deleted)
  3490. (save-current-column term-current-column)
  3491. (save-start-line-column term-start-line-column)
  3492. (save-current-row (term-current-row)))
  3493. ;; Inserting lines should take into account the scroll region.
  3494. ;; The `term-scroll-end' line is part of the scrolling region, so
  3495. ;; we need to go one line past it in order to ensure correct
  3496. ;; scrolling.
  3497. (if (< save-current-row term-scroll-start)
  3498. ;; If point is before scroll start,
  3499. (progn
  3500. (setq lines (- lines (- term-scroll-start save-current-row)))
  3501. (term-down (- term-scroll-start save-current-row))
  3502. (setq start (point)))
  3503. ;; The number of inserted lines shouldn't exceed the scroll region end.
  3504. (when (> (+ save-current-row lines) (1+ term-scroll-end))
  3505. (setq lines (- lines (- (+ save-current-row lines)(1+ term-scroll-end)))))
  3506. (term-down (- (1+ term-scroll-end) save-current-row lines)))
  3507. (setq start-deleted (point))
  3508. (term-down lines)
  3509. (delete-region start-deleted (point))
  3510. (goto-char start)
  3511. (setq term-current-column save-current-column)
  3512. (setq term-start-line-column save-start-line-column)
  3513. (setq term-current-row save-current-row)
  3514. (term-insert-char ?\n lines)
  3515. (goto-char start)))
  3516. (defun term-start-output-log (name)
  3517. "Record raw inferior process output in a buffer."
  3518. (interactive (list (if term-log-buffer
  3519. nil
  3520. (read-buffer "Record output in buffer: "
  3521. (format "%s output-log"
  3522. (buffer-name (current-buffer)))
  3523. nil))))
  3524. (if (or (null name) (equal name ""))
  3525. (progn (setq term-log-buffer nil)
  3526. (message "Output logging off."))
  3527. (if (get-buffer name)
  3528. nil
  3529. (with-current-buffer (get-buffer-create name)
  3530. (fundamental-mode)
  3531. (buffer-disable-undo (current-buffer))
  3532. (erase-buffer)))
  3533. (setq term-log-buffer (get-buffer name))
  3534. (message "Recording terminal emulator output into buffer \"%s\""
  3535. (buffer-name term-log-buffer))))
  3536. (defun term-stop-output-log ()
  3537. "Discontinue raw inferior process logging."
  3538. (interactive)
  3539. (term-start-output-log nil))
  3540. (defun term-show-maximum-output ()
  3541. "Put the end of the buffer at the bottom of the window."
  3542. (interactive)
  3543. (goto-char (point-max))
  3544. (recenter -1))
  3545. ;;; Do the user's customization...
  3546. (defvar term-load-hook nil
  3547. "This hook is run when term is loaded in.
  3548. This is a good place to put keybindings.")
  3549. (run-hooks 'term-load-hook)
  3550. ;;; Filename/command/history completion in a buffer
  3551. ;; ===========================================================================
  3552. ;; Useful completion functions, courtesy of the Ergo group.
  3553. ;; Six commands:
  3554. ;; term-dynamic-complete Complete or expand command, filename,
  3555. ;; history at point.
  3556. ;; term-dynamic-complete-filename Complete filename at point.
  3557. ;; term-dynamic-list-filename-completions List completions in help buffer.
  3558. ;; term-replace-by-expanded-filename Expand and complete filename at point;
  3559. ;; replace with expanded/completed name.
  3560. ;; These are not installed in the term-mode keymap. But they are
  3561. ;; available for people who want them. Shell-mode installs them:
  3562. ;; (define-key shell-mode-map "\t" 'term-dynamic-complete)
  3563. ;; (define-key shell-mode-map "\M-?"
  3564. ;; 'term-dynamic-list-filename-completions)))
  3565. ;;
  3566. ;; Commands like this are fine things to put in load hooks if you
  3567. ;; want them present in specific modes.
  3568. (defcustom term-completion-autolist nil
  3569. "If non-nil, automatically list possibilities on partial completion.
  3570. This mirrors the optional behavior of tcsh."
  3571. :group 'term
  3572. :type 'boolean)
  3573. (defcustom term-completion-addsuffix t
  3574. "If non-nil, add a `/' to completed directories, ` ' to file names.
  3575. If a cons pair, it should be of the form (DIRSUFFIX . FILESUFFIX) where
  3576. DIRSUFFIX and FILESUFFIX are strings added on unambiguous or exact
  3577. completion. This mirrors the optional behavior of tcsh."
  3578. :group 'term
  3579. :type '(choice (const :tag "No suffix" nil)
  3580. (cons (string :tag "dirsuffix") (string :tag "filesuffix"))
  3581. (other :tag "Suffix" t)))
  3582. (defcustom term-completion-recexact nil
  3583. "If non-nil, use shortest completion if characters cannot be added.
  3584. This mirrors the optional behavior of tcsh.
  3585. A non-nil value is useful if `term-completion-autolist' is non-nil too."
  3586. :group 'term
  3587. :type 'boolean)
  3588. (defcustom term-completion-fignore nil
  3589. "List of suffixes to be disregarded during file completion.
  3590. This mirrors the optional behavior of bash and tcsh.
  3591. Note that this applies to `term-dynamic-complete-filename' only."
  3592. :group 'term
  3593. :type '(choice (const nil)
  3594. (repeat :tag "List of suffixes" string)))
  3595. (defvar term-file-name-prefix ""
  3596. "Prefix prepended to absolute file names taken from process input.
  3597. This is used by term's and shell's completion functions, and by shell's
  3598. directory tracking functions.")
  3599. (defun term-directory (directory)
  3600. ;; Return expanded DIRECTORY, with `term-file-name-prefix' if absolute.
  3601. (expand-file-name (if (file-name-absolute-p directory)
  3602. (concat term-file-name-prefix directory)
  3603. directory)))
  3604. (defun term-word (word-chars)
  3605. "Return the word of WORD-CHARS at point, or nil if none is found.
  3606. Word constituents are considered to be those in WORD-CHARS, which is like the
  3607. inside of a \"[...]\" (see `skip-chars-forward')."
  3608. (save-excursion
  3609. (let ((limit (point))
  3610. (word (concat "[" word-chars "]"))
  3611. (non-word (concat "[^" word-chars "]")))
  3612. (when (re-search-backward non-word nil 'move)
  3613. (forward-char 1))
  3614. ;; Anchor the search forwards.
  3615. (if (or (eolp) (looking-at non-word))
  3616. nil
  3617. (re-search-forward (concat word "+") limit)
  3618. (buffer-substring (match-beginning 0) (match-end 0))))))
  3619. (defun term-match-partial-filename ()
  3620. "Return the filename at point, or nil if none is found.
  3621. Environment variables are substituted. See `term-word'."
  3622. (let ((filename (term-word "~/A-Za-z0-9+@:_.$#,={}-")))
  3623. (and filename (substitute-in-file-name filename))))
  3624. (defun term-dynamic-complete ()
  3625. "Dynamically perform completion at point.
  3626. Calls the functions in `term-dynamic-complete-functions' to perform
  3627. completion until a function returns non-nil, at which point completion is
  3628. assumed to have occurred."
  3629. (interactive)
  3630. (let ((functions term-dynamic-complete-functions))
  3631. (while (and functions (null (funcall (car functions))))
  3632. (setq functions (cdr functions)))))
  3633. (defun term-dynamic-complete-filename ()
  3634. "Dynamically complete the filename at point.
  3635. Completes if after a filename. See `term-match-partial-filename' and
  3636. `term-dynamic-complete-as-filename'.
  3637. This function is similar to `term-replace-by-expanded-filename', except that
  3638. it won't change parts of the filename already entered in the buffer; it just
  3639. adds completion characters to the end of the filename. A completions listing
  3640. may be shown in a help buffer if completion is ambiguous.
  3641. Completion is dependent on the value of `term-completion-addsuffix',
  3642. `term-completion-recexact' and `term-completion-fignore', and the timing of
  3643. completions listing is dependent on the value of `term-completion-autolist'.
  3644. Returns t if successful."
  3645. (interactive)
  3646. (when (term-match-partial-filename)
  3647. (prog2 (or (eq (selected-window) (minibuffer-window))
  3648. (message "Completing file name..."))
  3649. (term-dynamic-complete-as-filename))))
  3650. (defun term-dynamic-complete-as-filename ()
  3651. "Dynamically complete at point as a filename.
  3652. See `term-dynamic-complete-filename'. Returns t if successful."
  3653. (let* ((completion-ignore-case nil)
  3654. (completion-ignored-extensions term-completion-fignore)
  3655. (success t)
  3656. (dirsuffix (cond ((not term-completion-addsuffix) "")
  3657. ((not (consp term-completion-addsuffix)) "/")
  3658. (t (car term-completion-addsuffix))))
  3659. (filesuffix (cond ((not term-completion-addsuffix) "")
  3660. ((not (consp term-completion-addsuffix)) " ")
  3661. (t (cdr term-completion-addsuffix))))
  3662. (filename (or (term-match-partial-filename) ""))
  3663. (pathdir (file-name-directory filename))
  3664. (pathnondir (file-name-nondirectory filename))
  3665. (directory (if pathdir (term-directory pathdir) default-directory))
  3666. (completion (file-name-completion pathnondir directory))
  3667. (mini-flag (eq (selected-window) (minibuffer-window))))
  3668. (cond ((null completion)
  3669. (message "No completions of %s" filename)
  3670. (setq success nil))
  3671. ((eq completion t) ; Means already completed "file".
  3672. (when term-completion-addsuffix (insert " "))
  3673. (or mini-flag (message "Sole completion")))
  3674. ((string-equal completion "") ; Means completion on "directory/".
  3675. (term-dynamic-list-filename-completions))
  3676. (t ; Completion string returned.
  3677. (let ((file (concat (file-name-as-directory directory) completion)))
  3678. (insert (substring (directory-file-name completion)
  3679. (length pathnondir)))
  3680. (cond ((symbolp (file-name-completion completion directory))
  3681. ;; We inserted a unique completion.
  3682. (insert (if (file-directory-p file) dirsuffix filesuffix))
  3683. (or mini-flag (message "Completed")))
  3684. ((and term-completion-recexact term-completion-addsuffix
  3685. (string-equal pathnondir completion)
  3686. (file-exists-p file))
  3687. ;; It's not unique, but user wants shortest match.
  3688. (insert (if (file-directory-p file) dirsuffix filesuffix))
  3689. (or mini-flag (message "Completed shortest")))
  3690. ((or term-completion-autolist
  3691. (string-equal pathnondir completion))
  3692. ;; It's not unique, list possible completions.
  3693. (term-dynamic-list-filename-completions))
  3694. (t
  3695. (or mini-flag (message "Partially completed")))))))
  3696. success))
  3697. (defun term-replace-by-expanded-filename ()
  3698. "Dynamically expand and complete the filename at point.
  3699. Replace the filename with an expanded, canonicalized and completed replacement.
  3700. \"Expanded\" means environment variables (e.g., $HOME) and `~'s are replaced
  3701. with the corresponding directories. \"Canonicalized\" means `..' and `.' are
  3702. removed, and the filename is made absolute instead of relative. For expansion
  3703. see `expand-file-name' and `substitute-in-file-name'. For completion see
  3704. `term-dynamic-complete-filename'."
  3705. (interactive)
  3706. (replace-match (expand-file-name (term-match-partial-filename)) t t)
  3707. (term-dynamic-complete-filename))
  3708. (defun term-dynamic-simple-complete (stub candidates)
  3709. "Dynamically complete STUB from CANDIDATES list.
  3710. This function inserts completion characters at point by completing STUB from
  3711. the strings in CANDIDATES. A completions listing may be shown in a help buffer
  3712. if completion is ambiguous.
  3713. Returns nil if no completion was inserted.
  3714. Returns `sole' if completed with the only completion match.
  3715. Returns `shortest' if completed with the shortest of the completion matches.
  3716. Returns `partial' if completed as far as possible with the completion matches.
  3717. Returns `listed' if a completion listing was shown.
  3718. See also `term-dynamic-complete-filename'."
  3719. (let* ((completion-ignore-case nil)
  3720. (candidates (mapcar (function (lambda (x) (list x))) candidates))
  3721. (completions (all-completions stub candidates)))
  3722. (cond ((null completions)
  3723. (message "No completions of %s" stub)
  3724. nil)
  3725. ((= 1 (length completions)) ; Gotcha!
  3726. (let ((completion (car completions)))
  3727. (if (string-equal completion stub)
  3728. (message "Sole completion")
  3729. (insert (substring completion (length stub)))
  3730. (message "Completed"))
  3731. (when term-completion-addsuffix (insert " "))
  3732. 'sole))
  3733. (t ; There's no unique completion.
  3734. (let ((completion (try-completion stub candidates)))
  3735. ;; Insert the longest substring.
  3736. (insert (substring completion (length stub)))
  3737. (cond ((and term-completion-recexact term-completion-addsuffix
  3738. (string-equal stub completion)
  3739. (member completion completions))
  3740. ;; It's not unique, but user wants shortest match.
  3741. (insert " ")
  3742. (message "Completed shortest")
  3743. 'shortest)
  3744. ((or term-completion-autolist
  3745. (string-equal stub completion))
  3746. ;; It's not unique, list possible completions.
  3747. (term-dynamic-list-completions completions)
  3748. 'listed)
  3749. (t
  3750. (message "Partially completed")
  3751. 'partial)))))))
  3752. (make-obsolete 'term-dynamic-simple-complete 'completion-in-region "23.2")
  3753. (defun term-dynamic-list-filename-completions ()
  3754. "List in help buffer possible completions of the filename at point."
  3755. (interactive)
  3756. (let* ((completion-ignore-case nil)
  3757. (filename (or (term-match-partial-filename) ""))
  3758. (pathdir (file-name-directory filename))
  3759. (pathnondir (file-name-nondirectory filename))
  3760. (directory (if pathdir (term-directory pathdir) default-directory))
  3761. (completions (file-name-all-completions pathnondir directory)))
  3762. (if completions
  3763. (term-dynamic-list-completions completions)
  3764. (message "No completions of %s" filename))))
  3765. (defun term-dynamic-list-completions (completions)
  3766. "List in help buffer sorted COMPLETIONS.
  3767. Typing SPC flushes the help buffer."
  3768. (let ((conf (current-window-configuration)))
  3769. (with-output-to-temp-buffer "*Completions*"
  3770. (display-completion-list (sort completions 'string-lessp)))
  3771. (message "Hit space to flush")
  3772. (let (key first)
  3773. (if (with-current-buffer (get-buffer "*Completions*")
  3774. (setq key (read-key-sequence nil)
  3775. first (aref key 0))
  3776. (and (consp first)
  3777. (eq (window-buffer (posn-window (event-start first)))
  3778. (get-buffer "*Completions*"))
  3779. (eq (key-binding key) 'mouse-choose-completion)))
  3780. ;; If the user does mouse-choose-completion with the mouse,
  3781. ;; execute the command, then delete the completion window.
  3782. (progn
  3783. (choose-completion first)
  3784. (set-window-configuration conf))
  3785. (if (eq first ?\s)
  3786. (set-window-configuration conf)
  3787. (setq unread-command-events (listify-key-sequence key)))))))
  3788. ;; I need a make-term that doesn't surround with *s -mm
  3789. (defun term-ansi-make-term (name program &optional startfile &rest switches)
  3790. "Make a term process NAME in a buffer, running PROGRAM.
  3791. The name of the buffer is NAME.
  3792. If there is already a running process in that buffer, it is not restarted.
  3793. Optional third arg STARTFILE is the name of a file to send the contents of to
  3794. the process. Any more args are arguments to PROGRAM."
  3795. (let ((buffer (get-buffer-create name )))
  3796. ;; If no process, or nuked process, crank up a new one and put buffer in
  3797. ;; term mode. Otherwise, leave buffer and existing process alone.
  3798. (cond ((not (term-check-proc buffer))
  3799. (with-current-buffer buffer
  3800. (term-mode)) ; Install local vars, mode, keymap, ...
  3801. (term-exec buffer name program startfile switches)))
  3802. buffer))
  3803. (defvar term-ansi-buffer-name nil)
  3804. (defvar term-ansi-default-program nil)
  3805. (defvar term-ansi-buffer-base-name nil)
  3806. ;;;###autoload
  3807. (defun ansi-term (program &optional new-buffer-name)
  3808. "Start a terminal-emulator in a new buffer."
  3809. (interactive (list (read-from-minibuffer "Run program: "
  3810. (or explicit-shell-file-name
  3811. (getenv "ESHELL")
  3812. (getenv "SHELL")
  3813. "/bin/sh"))))
  3814. ;; Pick the name of the new buffer.
  3815. (setq term-ansi-buffer-name
  3816. (if new-buffer-name
  3817. new-buffer-name
  3818. (if term-ansi-buffer-base-name
  3819. (if (eq term-ansi-buffer-base-name t)
  3820. (file-name-nondirectory program)
  3821. term-ansi-buffer-base-name)
  3822. "ansi-term")))
  3823. (setq term-ansi-buffer-name (concat "*" term-ansi-buffer-name "*"))
  3824. ;; In order to have more than one term active at a time
  3825. ;; I'd like to have the term names have the *term-ansi-term<?>* form,
  3826. ;; for now they have the *term-ansi-term*<?> form but we'll see...
  3827. (setq term-ansi-buffer-name (generate-new-buffer-name term-ansi-buffer-name))
  3828. (setq term-ansi-buffer-name (term-ansi-make-term term-ansi-buffer-name program))
  3829. (set-buffer term-ansi-buffer-name)
  3830. (term-mode)
  3831. (term-char-mode)
  3832. ;; I wanna have find-file on C-x C-f -mm
  3833. ;; your mileage may definitely vary, maybe it's better to put this in your
  3834. ;; .emacs ...
  3835. (term-set-escape-char ?\C-x)
  3836. (switch-to-buffer term-ansi-buffer-name))
  3837. ;;; Serial terminals
  3838. ;; ===========================================================================
  3839. (defun serial-port-is-file-p ()
  3840. "Guess whether serial ports are files on this system.
  3841. Return t if this is a Unix-based system, where serial ports are
  3842. files, such as /dev/ttyS0.
  3843. Return nil if this is Windows or DOS, where serial ports have
  3844. special identifiers such as COM1."
  3845. (not (memq system-type '(windows-nt cygwin ms-dos))))
  3846. (defvar serial-name-history
  3847. (if (serial-port-is-file-p)
  3848. (or (when (file-exists-p "/dev/ttys0") (list "/dev/ttys0"))
  3849. (when (file-exists-p "/dev/ttyS0") (list "/dev/ttyS0")))
  3850. (list "COM1"))
  3851. "History of serial ports used by `serial-read-name'.")
  3852. (defvar serial-speed-history
  3853. ;; Initialized with reasonable values for newbies.
  3854. (list "9600" ;; Given twice because 9600 b/s is the most common speed
  3855. "1200" "2400" "4800" "9600" "14400" "19200"
  3856. "28800" "38400" "57600" "115200")
  3857. "History of serial port speeds used by `serial-read-speed'.")
  3858. (defun serial-nice-speed-history ()
  3859. "Return `serial-speed-history' cleaned up for a mouse-menu."
  3860. (let ((x) (y))
  3861. (setq x
  3862. (sort
  3863. (copy-sequence serial-speed-history)
  3864. (lambda (a b) (when (and (stringp a) (stringp b))
  3865. (> (string-to-number a) (string-to-number b))))))
  3866. (dolist (i x) (when (not (equal i (car y))) (push i y)))
  3867. y))
  3868. (defconst serial-no-speed "nil"
  3869. "String for `serial-read-speed' for special serial ports.
  3870. If `serial-read-speed' reads this string from the user, it
  3871. returns nil, which is recognized by `serial-process-configure'
  3872. for special serial ports that cannot be configured.")
  3873. (defun serial-supported-or-barf ()
  3874. "Signal an error if serial processes are not supported."
  3875. (unless (fboundp 'make-serial-process)
  3876. (error "Serial processes are not supported on this system")))
  3877. (defun serial-read-name ()
  3878. "Read a serial port name from the user.
  3879. Try to be nice by providing useful defaults and history.
  3880. On Windows, prepend \\.\ to the port name unless it already
  3881. contains a backslash. This handles the legacy ports COM1-COM9 as
  3882. well as the newer ports COM10 and higher."
  3883. (serial-supported-or-barf)
  3884. (let* ((file-name-history serial-name-history)
  3885. (h (car file-name-history))
  3886. (x (if (serial-port-is-file-p)
  3887. (read-file-name
  3888. ;; `prompt': The most recently used port is provided as
  3889. ;; the default value, which is used when the user
  3890. ;; simply presses return.
  3891. (if (stringp h) (format "Serial port (default %s): " h)
  3892. "Serial port: ")
  3893. ;; `directory': Most systems have their serial ports
  3894. ;; in the same directory, so start in the directory
  3895. ;; of the most recently used port, or in a reasonable
  3896. ;; default directory.
  3897. (or (and h (file-name-directory h))
  3898. (and (file-exists-p "/dev/") "/dev/")
  3899. (and (file-exists-p "/") "/"))
  3900. ;; `default': This causes (read-file-name) to return
  3901. ;; the empty string if he user simply presses return.
  3902. ;; Using nil here may result in a default directory
  3903. ;; of the current buffer, which is not useful for
  3904. ;; serial port.
  3905. "")
  3906. (read-from-minibuffer
  3907. (if (stringp h) (format "Serial port (default %s): " h)
  3908. "Serial port: ")
  3909. nil nil nil '(file-name-history . 1) nil nil))))
  3910. (if (or (null x) (and (stringp x) (zerop (length x))))
  3911. (setq x h)
  3912. (setq serial-name-history file-name-history))
  3913. (when (or (null x) (and (stringp x) (zerop (length x))))
  3914. (error "No serial port selected"))
  3915. (when (and (not (serial-port-is-file-p))
  3916. (not (string-match "\\\\" x)))
  3917. (set 'x (concat "\\\\.\\" x)))
  3918. x))
  3919. (defun serial-read-speed ()
  3920. "Read a serial port speed (in bits per second) from the user.
  3921. Try to be nice by providing useful defaults and history."
  3922. (serial-supported-or-barf)
  3923. (let* ((history serial-speed-history)
  3924. (h (car history))
  3925. (x (read-from-minibuffer
  3926. (cond ((string= h serial-no-speed)
  3927. "Speed (default nil = set by port): ")
  3928. (h
  3929. (format "Speed (default %s b/s): " h))
  3930. (t
  3931. (format "Speed (b/s): ")))
  3932. nil nil nil '(history . 1) nil nil)))
  3933. (when (or (null x) (and (stringp x) (zerop (length x))))
  3934. (setq x h))
  3935. (when (or (null x) (not (stringp x)) (zerop (length x)))
  3936. (error "Invalid speed"))
  3937. (if (string= x serial-no-speed)
  3938. (setq x nil)
  3939. (setq x (string-to-number x))
  3940. (when (or (null x) (not (integerp x)) (<= x 0))
  3941. (error "Invalid speed")))
  3942. (setq serial-speed-history history)
  3943. x))
  3944. ;;;###autoload
  3945. (defun serial-term (port speed)
  3946. "Start a terminal-emulator for a serial port in a new buffer.
  3947. PORT is the path or name of the serial port. For example, this
  3948. could be \"/dev/ttyS0\" on Unix. On Windows, this could be
  3949. \"COM1\" or \"\\\\.\\COM10\".
  3950. SPEED is the speed of the serial port in bits per second. 9600
  3951. is a common value. SPEED can be nil, see
  3952. `serial-process-configure' for details.
  3953. The buffer is in Term mode; see `term-mode' for the commands to
  3954. use in that buffer.
  3955. \\<term-raw-map>Type \\[switch-to-buffer] to switch to another buffer."
  3956. (interactive (list (serial-read-name) (serial-read-speed)))
  3957. (serial-supported-or-barf)
  3958. (let* ((process (make-serial-process
  3959. :port port
  3960. :speed speed
  3961. :coding 'no-conversion
  3962. :noquery t))
  3963. (buffer (process-buffer process)))
  3964. (with-current-buffer buffer
  3965. (term-mode)
  3966. (term-char-mode)
  3967. (goto-char (point-max))
  3968. (set-marker (process-mark process) (point))
  3969. (set-process-filter process 'term-emulate-terminal)
  3970. (set-process-sentinel process 'term-sentinel))
  3971. (switch-to-buffer buffer)
  3972. buffer))
  3973. (defvar serial-mode-line-speed-menu nil)
  3974. (defvar serial-mode-line-config-menu nil)
  3975. (defun serial-speed ()
  3976. "Return the speed of the serial port of the current buffer's process.
  3977. The return value may be nil for a special serial port."
  3978. (process-contact (get-buffer-process (current-buffer)) :speed))
  3979. (defun serial-mode-line-speed-menu-1 (event)
  3980. (interactive "e")
  3981. (save-selected-window
  3982. (select-window (posn-window (event-start event)))
  3983. (serial-update-speed-menu)
  3984. (let* ((selection (serial-mode-line-speed-menu event))
  3985. (binding (and selection (lookup-key serial-mode-line-speed-menu
  3986. (vector (car selection))))))
  3987. (when binding (call-interactively binding)))))
  3988. (defun serial-mode-line-speed-menu (event)
  3989. (x-popup-menu event serial-mode-line-speed-menu))
  3990. (defun serial-update-speed-menu ()
  3991. (setq serial-mode-line-speed-menu (make-sparse-keymap "Speed (b/s)"))
  3992. (define-key serial-mode-line-speed-menu [serial-mode-line-speed-menu-other]
  3993. '(menu-item "Other..."
  3994. (lambda (event) (interactive "e")
  3995. (let ((speed (serial-read-speed)))
  3996. (serial-process-configure :speed speed)
  3997. (term-update-mode-line)
  3998. (message "Speed set to %d b/s" speed)))))
  3999. (dolist (str (serial-nice-speed-history))
  4000. (let ((num (or (and (stringp str) (string-to-number str)) 0)))
  4001. (define-key
  4002. serial-mode-line-speed-menu
  4003. (vector (make-symbol (format "serial-mode-line-speed-menu-%s" str)))
  4004. `(menu-item
  4005. ,str
  4006. (lambda (event) (interactive "e")
  4007. (serial-process-configure :speed ,num)
  4008. (term-update-mode-line)
  4009. (message "Speed set to %d b/s" ,num))
  4010. :button (:toggle . (= (serial-speed) ,num)))))))
  4011. (defun serial-mode-line-config-menu-1 (event)
  4012. (interactive "e")
  4013. (save-selected-window
  4014. (select-window (posn-window (event-start event)))
  4015. (serial-update-config-menu)
  4016. (let* ((selection (serial-mode-line-config-menu event))
  4017. (binding (and selection (lookup-key serial-mode-line-config-menu
  4018. (vector (car selection))))))
  4019. (when binding (call-interactively binding)))))
  4020. (defun serial-mode-line-config-menu (event)
  4021. (x-popup-menu event serial-mode-line-config-menu))
  4022. (defun serial-update-config-menu ()
  4023. (setq serial-mode-line-config-menu (make-sparse-keymap "Configuration"))
  4024. (let ((config (process-contact
  4025. (get-buffer-process (current-buffer)) t)))
  4026. (dolist (y '((:flowcontrol hw "Hardware flowcontrol (RTS/CTS)")
  4027. (:flowcontrol sw "Software flowcontrol (XON/XOFF)")
  4028. (:flowcontrol nil "No flowcontrol")
  4029. (:stopbits 2 "2 stopbits")
  4030. (:stopbits 1 "1 stopbit")
  4031. (:parity odd "Odd parity")
  4032. (:parity even "Even parity")
  4033. (:parity nil "No parity")
  4034. (:bytesize 7 "7 bits per byte")
  4035. (:bytesize 8 "8 bits per byte")))
  4036. (define-key serial-mode-line-config-menu
  4037. (vector (make-symbol (format "%s-%s" (nth 0 y) (nth 1 y))))
  4038. `(menu-item
  4039. ,(nth 2 y)
  4040. (lambda (event) (interactive "e")
  4041. (serial-process-configure ,(nth 0 y) ',(nth 1 y))
  4042. (term-update-mode-line)
  4043. (message "%s" ,(nth 2 y)))
  4044. ;; Use :toggle instead of :radio because a non-standard port
  4045. ;; configuration may not match any menu items.
  4046. :button (:toggle . ,(equal (plist-get config (nth 0 y))
  4047. (nth 1 y))))))))
  4048. ;;; Converting process modes to use term mode
  4049. ;; ===========================================================================
  4050. ;; Renaming variables
  4051. ;; Most of the work is renaming variables and functions. These are the common
  4052. ;; ones:
  4053. ;; Local variables:
  4054. ;; last-input-start term-last-input-start
  4055. ;; last-input-end term-last-input-end
  4056. ;; shell-prompt-pattern term-prompt-regexp
  4057. ;; shell-set-directory-error-hook <no equivalent>
  4058. ;; Miscellaneous:
  4059. ;; shell-set-directory <unnecessary>
  4060. ;; shell-mode-map term-mode-map
  4061. ;; Commands:
  4062. ;; shell-send-input term-send-input
  4063. ;; shell-send-eof term-delchar-or-maybe-eof
  4064. ;; kill-shell-input term-kill-input
  4065. ;; interrupt-shell-subjob term-interrupt-subjob
  4066. ;; stop-shell-subjob term-stop-subjob
  4067. ;; quit-shell-subjob term-quit-subjob
  4068. ;; kill-shell-subjob term-kill-subjob
  4069. ;; kill-output-from-shell term-kill-output
  4070. ;; show-output-from-shell term-show-output
  4071. ;; copy-last-shell-input Use term-previous-input/term-next-input
  4072. ;;
  4073. ;; SHELL-SET-DIRECTORY is gone, its functionality taken over by
  4074. ;; SHELL-DIRECTORY-TRACKER, the shell mode's term-input-filter-functions.
  4075. ;; Term mode does not provide functionality equivalent to
  4076. ;; shell-set-directory-error-hook; it is gone.
  4077. ;;
  4078. ;; term-last-input-start is provided for modes which want to munge
  4079. ;; the buffer after input is sent, perhaps because the inferior
  4080. ;; insists on echoing the input. The LAST-INPUT-START variable in
  4081. ;; the old shell package was used to implement a history mechanism,
  4082. ;; but you should think twice before using term-last-input-start
  4083. ;; for this; the input history ring often does the job better.
  4084. ;;
  4085. ;; If you are implementing some process-in-a-buffer mode, called foo-mode, do
  4086. ;; *not* create the term-mode local variables in your foo-mode function.
  4087. ;; This is not modular. Instead, call term-mode, and let *it* create the
  4088. ;; necessary term-specific local variables. Then create the
  4089. ;; foo-mode-specific local variables in foo-mode. Set the buffer's keymap to
  4090. ;; be foo-mode-map, and its mode to be foo-mode. Set the term-mode hooks
  4091. ;; (term-{prompt-regexp, input-filter, input-filter-functions,
  4092. ;; get-old-input) that need to be different from the defaults. Call
  4093. ;; foo-mode-hook, and you're done. Don't run the term-mode hook yourself;
  4094. ;; term-mode will take care of it. The following example, from shell.el,
  4095. ;; is typical:
  4096. ;;
  4097. ;; (defvar shell-mode-map '())
  4098. ;; (cond ((not shell-mode-map)
  4099. ;; (setq shell-mode-map (copy-keymap term-mode-map))
  4100. ;; (define-key shell-mode-map "\C-c\C-f" 'shell-forward-command)
  4101. ;; (define-key shell-mode-map "\C-c\C-b" 'shell-backward-command)
  4102. ;; (define-key shell-mode-map "\t" 'term-dynamic-complete)
  4103. ;; (define-key shell-mode-map "\M-?"
  4104. ;; 'term-dynamic-list-filename-completions)))
  4105. ;;
  4106. ;; (defun shell-mode ()
  4107. ;; (interactive)
  4108. ;; (term-mode)
  4109. ;; (setq term-prompt-regexp shell-prompt-pattern)
  4110. ;; (setq major-mode 'shell-mode)
  4111. ;; (setq mode-name "Shell")
  4112. ;; (use-local-map shell-mode-map)
  4113. ;; (make-local-variable 'shell-directory-stack)
  4114. ;; (setq shell-directory-stack nil)
  4115. ;; (add-hook 'term-input-filter-functions 'shell-directory-tracker)
  4116. ;; (run-mode-hooks 'shell-mode-hook))
  4117. ;;
  4118. ;;
  4119. ;; Completion for term-mode users
  4120. ;;
  4121. ;; For modes that use term-mode, term-dynamic-complete-functions is the
  4122. ;; hook to add completion functions to. Functions on this list should return
  4123. ;; non-nil if completion occurs (i.e., further completion should not occur).
  4124. ;; You could use completion-in-region to do the bulk of the
  4125. ;; completion job.
  4126. (provide 'term)
  4127. ;;; term.el ends here