term.el 173 KB

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