term.el 171 KB

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