elisp-4 284 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666
  1. This is /home/cyd/emacs/doc/lispref/../../info/elisp, produced by
  2. makeinfo version 4.13 from /home/cyd/emacs/doc/lispref/elisp.texi.
  3. This is the `GNU Emacs Lisp Reference Manual' corresponding to Emacs
  4. version 24.2.
  5. Copyright (C) 1990-1996, 1998-2012 Free Software Foundation, Inc.
  6. Permission is granted to copy, distribute and/or modify this
  7. document under the terms of the GNU Free Documentation License,
  8. Version 1.3 or any later version published by the Free Software
  9. Foundation; with the Invariant Sections being "GNU General Public
  10. License," with the Front-Cover texts being "A GNU Manual," and
  11. with the Back-Cover Texts as in (a) below. A copy of the license
  12. is included in the section entitled "GNU Free Documentation
  13. License."
  14. (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
  15. modify this GNU manual. Buying copies from the FSF supports it in
  16. developing GNU and promoting software freedom."
  17. INFO-DIR-SECTION GNU Emacs Lisp
  18. START-INFO-DIR-ENTRY
  19. * Elisp: (elisp). The Emacs Lisp Reference Manual.
  20. END-INFO-DIR-ENTRY
  21. 
  22. File: elisp, Node: Reading File Names, Next: Completion Variables, Prev: High-Level Completion, Up: Completion
  23. 20.6.5 Reading File Names
  24. -------------------------
  25. The high-level completion functions `read-file-name',
  26. `read-directory-name', and `read-shell-command' are designed to read
  27. file names, directory names, and shell commands, respectively. They
  28. provide special features, including automatic insertion of the default
  29. directory.
  30. -- Function: read-file-name prompt &optional directory default
  31. require-match initial predicate
  32. This function reads a file name, prompting with PROMPT and
  33. providing completion.
  34. As an exception, this function reads a file name using a graphical
  35. file dialog instead of the minibuffer, if all of the following are
  36. true:
  37. 1. It is invoked via a mouse command.
  38. 2. The selected frame is on a graphical display supporting such
  39. dialogs.
  40. 3. The variable `use-dialog-box' is non-`nil'. *Note Dialog
  41. Boxes: (emacs)Dialog Boxes.
  42. 4. The DIRECTORY argument, described below, does not specify a
  43. remote file. *Note Remote Files: (emacs)Remote Files.
  44. The exact behavior when using a graphical file dialog is
  45. platform-dependent. Here, we simply document the behavior when
  46. using the minibuffer.
  47. `read-file-name' does not automatically expand the returned file
  48. name. You must call `expand-file-name' yourself if an absolute
  49. file name is required.
  50. The optional argument REQUIRE-MATCH has the same meaning as in
  51. `completing-read'. *Note Minibuffer Completion::.
  52. The argument DIRECTORY specifies the directory to use for
  53. completing relative file names. It should be an absolute directory
  54. name. If the variable `insert-default-directory' is non-`nil',
  55. DIRECTORY is also inserted in the minibuffer as initial input. It
  56. defaults to the current buffer's value of `default-directory'.
  57. If you specify INITIAL, that is an initial file name to insert in
  58. the buffer (after DIRECTORY, if that is inserted). In this case,
  59. point goes at the beginning of INITIAL. The default for INITIAL
  60. is `nil'--don't insert any file name. To see what INITIAL does,
  61. try the command `C-x C-v' in a buffer visiting a file. *Please
  62. note:* we recommend using DEFAULT rather than INITIAL in most
  63. cases.
  64. If DEFAULT is non-`nil', then the function returns DEFAULT if the
  65. user exits the minibuffer with the same non-empty contents that
  66. `read-file-name' inserted initially. The initial minibuffer
  67. contents are always non-empty if `insert-default-directory' is
  68. non-`nil', as it is by default. DEFAULT is not checked for
  69. validity, regardless of the value of REQUIRE-MATCH. However, if
  70. REQUIRE-MATCH is non-`nil', the initial minibuffer contents should
  71. be a valid file (or directory) name. Otherwise `read-file-name'
  72. attempts completion if the user exits without any editing, and
  73. does not return DEFAULT. DEFAULT is also available through the
  74. history commands.
  75. If DEFAULT is `nil', `read-file-name' tries to find a substitute
  76. default to use in its place, which it treats in exactly the same
  77. way as if it had been specified explicitly. If DEFAULT is `nil',
  78. but INITIAL is non-`nil', then the default is the absolute file
  79. name obtained from DIRECTORY and INITIAL. If both DEFAULT and
  80. INITIAL are `nil' and the buffer is visiting a file,
  81. `read-file-name' uses the absolute file name of that file as
  82. default. If the buffer is not visiting a file, then there is no
  83. default. In that case, if the user types <RET> without any
  84. editing, `read-file-name' simply returns the pre-inserted contents
  85. of the minibuffer.
  86. If the user types <RET> in an empty minibuffer, this function
  87. returns an empty string, regardless of the value of REQUIRE-MATCH.
  88. This is, for instance, how the user can make the current buffer
  89. visit no file using `M-x set-visited-file-name'.
  90. If PREDICATE is non-`nil', it specifies a function of one argument
  91. that decides which file names are acceptable completion
  92. alternatives. A file name is an acceptable value if PREDICATE
  93. returns non-`nil' for it.
  94. Here is an example of using `read-file-name':
  95. (read-file-name "The file is ")
  96. ;; After evaluation of the preceding expression,
  97. ;; the following appears in the minibuffer:
  98. ---------- Buffer: Minibuffer ----------
  99. The file is /gp/gnu/elisp/-!-
  100. ---------- Buffer: Minibuffer ----------
  101. Typing `manual <TAB>' results in the following:
  102. ---------- Buffer: Minibuffer ----------
  103. The file is /gp/gnu/elisp/manual.texi-!-
  104. ---------- Buffer: Minibuffer ----------
  105. If the user types <RET>, `read-file-name' returns the file name as
  106. the string `"/gp/gnu/elisp/manual.texi"'.
  107. -- Variable: read-file-name-function
  108. If non-`nil', this should be a function that accepts the same
  109. arguments as `read-file-name'. When `read-file-name' is called,
  110. it calls this function with the supplied arguments instead of
  111. doing its usual work.
  112. -- User Option: read-file-name-completion-ignore-case
  113. If this variable is non-`nil', `read-file-name' ignores case when
  114. performing completion.
  115. -- Function: read-directory-name prompt &optional directory default
  116. require-match initial
  117. This function is like `read-file-name' but allows only directory
  118. names as completion alternatives.
  119. If DEFAULT is `nil' and INITIAL is non-`nil',
  120. `read-directory-name' constructs a substitute default by combining
  121. DIRECTORY (or the current buffer's default directory if DIRECTORY
  122. is `nil') and INITIAL. If both DEFAULT and INITIAL are `nil',
  123. this function uses DIRECTORY as substitute default, or the current
  124. buffer's default directory if DIRECTORY is `nil'.
  125. -- User Option: insert-default-directory
  126. This variable is used by `read-file-name', and thus, indirectly,
  127. by most commands reading file names. (This includes all commands
  128. that use the code letters `f' or `F' in their interactive form.
  129. *Note Code Characters for interactive: Interactive Codes.) Its
  130. value controls whether `read-file-name' starts by placing the name
  131. of the default directory in the minibuffer, plus the initial file
  132. name, if any. If the value of this variable is `nil', then
  133. `read-file-name' does not place any initial input in the
  134. minibuffer (unless you specify initial input with the INITIAL
  135. argument). In that case, the default directory is still used for
  136. completion of relative file names, but is not displayed.
  137. If this variable is `nil' and the initial minibuffer contents are
  138. empty, the user may have to explicitly fetch the next history
  139. element to access a default value. If the variable is non-`nil',
  140. the initial minibuffer contents are always non-empty and the user
  141. can always request a default value by immediately typing <RET> in
  142. an unedited minibuffer. (See above.)
  143. For example:
  144. ;; Here the minibuffer starts out with the default directory.
  145. (let ((insert-default-directory t))
  146. (read-file-name "The file is "))
  147. ---------- Buffer: Minibuffer ----------
  148. The file is ~lewis/manual/-!-
  149. ---------- Buffer: Minibuffer ----------
  150. ;; Here the minibuffer is empty and only the prompt
  151. ;; appears on its line.
  152. (let ((insert-default-directory nil))
  153. (read-file-name "The file is "))
  154. ---------- Buffer: Minibuffer ----------
  155. The file is -!-
  156. ---------- Buffer: Minibuffer ----------
  157. -- Function: read-shell-command prompt &optional initial history &rest
  158. args
  159. This function reads a shell command from the minibuffer, prompting
  160. with PROMPT and providing intelligent completion. It completes
  161. the first word of the command using candidates that are appropriate
  162. for command names, and the rest of the command words as file names.
  163. This function uses `minibuffer-local-shell-command-map' as the
  164. keymap for minibuffer input. The HISTORY argument specifies the
  165. history list to use; if is omitted or `nil', it defaults to
  166. `shell-command-history' (*note shell-command-history: Minibuffer
  167. History.). The optional argument INITIAL specifies the initial
  168. content of the minibuffer (*note Initial Input::). The rest of
  169. ARGS, if present, are used as the DEFAULT and INHERIT-INPUT-METHOD
  170. arguments in `read-from-minibuffer' (*note Text from Minibuffer::).
  171. -- Variable: minibuffer-local-shell-command-map
  172. This keymap is used by `read-shell-command' for completing command
  173. and file names that are part of a shell command. It uses
  174. `minibuffer-local-map' as its parent keymap, and binds <TAB> to
  175. `completion-at-point'.
  176. 
  177. File: elisp, Node: Completion Variables, Next: Programmed Completion, Prev: Reading File Names, Up: Completion
  178. 20.6.6 Completion Variables
  179. ---------------------------
  180. Here are some variables that can be used to alter the default
  181. completion behavior.
  182. -- User Option: completion-styles
  183. The value of this variable is a list of completion style (symbols)
  184. to use for performing completion. A "completion style" is a set of
  185. rules for generating completions. Each symbol occurring this list
  186. must have a corresponding entry in `completion-styles-alist'.
  187. -- Variable: completion-styles-alist
  188. This variable stores a list of available completion styles. Each
  189. element in the list has the form
  190. (STYLE TRY-COMPLETION ALL-COMPLETIONS DOC)
  191. Here, STYLE is the name of the completion style (a symbol), which
  192. may be used in the `completion-styles' variable to refer to this
  193. style; TRY-COMPLETION is the function that does the completion;
  194. ALL-COMPLETIONS is the function that lists the completions; and
  195. DOC is a string describing the completion style.
  196. The TRY-COMPLETION and ALL-COMPLETIONS functions should each
  197. accept four arguments: STRING, COLLECTION, PREDICATE, and POINT.
  198. The STRING, COLLECTION, and PREDICATE arguments have the same
  199. meanings as in `try-completion' (*note Basic Completion::), and
  200. the POINT argument is the position of point within STRING. Each
  201. function should return a non-`nil' value if it performed its job,
  202. and `nil' if it did not (e.g. if there is no way to complete
  203. STRING according to the completion style).
  204. When the user calls a completion command like
  205. `minibuffer-complete' (*note Completion Commands::), Emacs looks
  206. for the first style listed in `completion-styles' and calls its
  207. TRY-COMPLETION function. If this function returns `nil', Emacs
  208. moves to the next listed completion style and calls its
  209. TRY-COMPLETION function, and so on until one of the TRY-COMPLETION
  210. functions successfully performs completion and returns a non-`nil'
  211. value. A similar procedure is used for listing completions, via
  212. the ALL-COMPLETIONS functions.
  213. *Note Completion Styles: (emacs)Completion Styles, for a
  214. description of the available completion styles.
  215. -- User Option: completion-category-overrides
  216. This variable specifies special completion styles and other
  217. completion behaviors to use when completing certain types of text.
  218. Its value should be an alist with elements of the form `(CATEGORY
  219. . ALIST)'. CATEGORY is a symbol describing what is being
  220. completed; currently, the `buffer', `file', and `unicode-name'
  221. categories are defined, but others can be defined via specialized
  222. completion functions (*note Programmed Completion::). ALIST is an
  223. association list describing how completion should behave for the
  224. corresponding category. The following alist keys are supported:
  225. `styles'
  226. The value should be a list of completion styles (symbols).
  227. `cycle'
  228. The value should be a value for `completion-cycle-threshold'
  229. (*note Completion Options: (emacs)Completion Options.) for
  230. this category.
  231. Additional alist entries may be defined in the future.
  232. -- Variable: completion-extra-properties
  233. This variable is used to specify extra properties of the current
  234. completion command. It is intended to be let-bound by specialized
  235. completion commands. Its value should be a list of property and
  236. value pairs. The following properties are supported:
  237. `:annotation-function'
  238. The value should be a function to add annotations in the
  239. completions buffer. This function must accept one argument,
  240. a completion, and should either return `nil' or a string to
  241. be displayed next to the completion.
  242. `:exit-function'
  243. The value should be a function to run after performing
  244. completion. The function should accept two arguments, STRING
  245. and STATUS, where STRING is the text to which the field was
  246. completed, and STATUS indicates what kind of operation
  247. happened: `finished' if text is now complete, `sole' if the
  248. text cannot be further completed but completion is not
  249. finished, or `exact' if the text is a valid completion but
  250. may be further completed.
  251. 
  252. File: elisp, Node: Programmed Completion, Next: Completion in Buffers, Prev: Completion Variables, Up: Completion
  253. 20.6.7 Programmed Completion
  254. ----------------------------
  255. Sometimes it is not possible or convenient to create an alist or an
  256. obarray containing all the intended possible completions ahead of time.
  257. In such a case, you can supply your own function to compute the
  258. completion of a given string. This is called "programmed completion".
  259. Emacs uses programmed completion when completing file names (*note File
  260. Name Completion::), among many other cases.
  261. To use this feature, pass a function as the COLLECTION argument to
  262. `completing-read'. The function `completing-read' arranges to pass
  263. your completion function along to `try-completion', `all-completions',
  264. and other basic completion functions, which will then let your function
  265. do all the work.
  266. The completion function should accept three arguments:
  267. * The string to be completed.
  268. * A predicate function with which to filter possible matches, or
  269. `nil' if none. The function should call the predicate for each
  270. possible match, and ignore the match if the predicate returns
  271. `nil'.
  272. * A flag specifying the type of completion operation to perform.
  273. This is one of the following four values:
  274. `nil'
  275. This specifies a `try-completion' operation. The function
  276. should return `t' if the specified string is a unique and
  277. exact match; if there is more than one match, it should
  278. return the common substring of all matches (if the string is
  279. an exact match for one completion alternative but also
  280. matches other longer alternatives, the return value is the
  281. string); if there are no matches, it should return `nil'.
  282. `t'
  283. This specifies an `all-completions' operation. The function
  284. should return a list of all possible completions of the
  285. specified string.
  286. `lambda'
  287. This specifies a `test-completion' operation. The function
  288. should return `t' if the specified string is an exact match
  289. for some completion alternative; `nil' otherwise.
  290. `(boundaries . SUFFIX)'
  291. This specifies a `completion-boundaries' operation. The
  292. function should return `(boundaries START . END)', where
  293. START is the position of the beginning boundary in the
  294. specified string, and END is the position of the end boundary
  295. in SUFFIX.
  296. `metadata'
  297. This specifies a request for information about the state of
  298. the current completion. The function should return an alist,
  299. as described below. The alist may contain any number of
  300. elements.
  301. If the flag has any other value, the completion function should
  302. return `nil'.
  303. The following is a list of metadata entries that a completion
  304. function may return in response to a `metadata' flag argument:
  305. `category'
  306. The value should be a symbol describing what kind of text the
  307. completion function is trying to complete. If the symbol matches
  308. one of the keys in `completion-category-overrides', the usual
  309. completion behavior is overridden. *Note Completion Variables::.
  310. `annotation-function'
  311. The value should be a function for "annotating" completions. The
  312. function should take one argument, STRING, which is a possible
  313. completion. It should return a string, which is displayed after
  314. the completion STRING in the `*Completions*' buffer.
  315. `display-sort-function'
  316. The value should be a function for sorting completions. The
  317. function should take one argument, a list of completion strings,
  318. and return a sorted list of completion strings. It is allowed to
  319. alter the input list destructively.
  320. `cycle-sort-function'
  321. The value should be a function for sorting completions, when
  322. `completion-cycle-threshold' is non-`nil' and the user is cycling
  323. through completion alternatives. *Note Completion Options:
  324. (emacs)Completion Options. Its argument list and return value are
  325. the same as for `display-sort-function'.
  326. -- Function: completion-table-dynamic function
  327. This function is a convenient way to write a function that can act
  328. as a programmed completion function. The argument FUNCTION should
  329. be a function that takes one argument, a string, and returns an
  330. alist of possible completions of it. You can think of
  331. `completion-table-dynamic' as a transducer between that interface
  332. and the interface for programmed completion functions.
  333. 
  334. File: elisp, Node: Completion in Buffers, Prev: Programmed Completion, Up: Completion
  335. 20.6.8 Completion in Ordinary Buffers
  336. -------------------------------------
  337. Although completion is usually done in the minibuffer, the completion
  338. facility can also be used on the text in ordinary Emacs buffers. In
  339. many major modes, in-buffer completion is performed by the `C-M-i' or
  340. `M-<TAB>' command, bound to `completion-at-point'. *Note Symbol
  341. Completion: (emacs)Symbol Completion. This command uses the abnormal
  342. hook variable `completion-at-point-functions':
  343. -- Variable: completion-at-point-functions
  344. The value of this abnormal hook should be a list of functions,
  345. which are used to compute a completion table for completing the
  346. text at point. It can be used by major modes to provide
  347. mode-specific completion tables (*note Major Mode Conventions::).
  348. When the command `completion-at-point' runs, it calls the
  349. functions in the list one by one, without any argument. Each
  350. function should return `nil' if it is unable to produce a
  351. completion table for the text at point. Otherwise it should
  352. return a list of the form
  353. (START END COLLECTION . PROPS)
  354. START and END delimit the text to complete (which should enclose
  355. point). COLLECTION is a completion table for completing that
  356. text, in a form suitable for passing as the second argument to
  357. `try-completion' (*note Basic Completion::); completion
  358. alternatives will be generated from this completion table in the
  359. usual way, via the completion styles defined in `completion-styles'
  360. (*note Completion Variables::). PROPS is a property list for
  361. additional information; any of the properties in
  362. `completion-extra-properties' are recognized (*note Completion
  363. Variables::), as well as the following additional ones:
  364. `:predicate'
  365. The value should be a predicate that completion candidates
  366. need to satisfy.
  367. `:exclusive'
  368. If the value is `no', then if the completion table fails to
  369. match the text at point, `completion-at-point' moves on to the
  370. next function in `completion-at-point-functions' instead of
  371. reporting a completion failure.
  372. A function in `completion-at-point-functions' may also return a
  373. function. In that case, that returned function is called, with no
  374. argument, and it is entirely responsible for performing the
  375. completion. We discourage this usage; it is intended to help
  376. convert old code to using `completion-at-point'.
  377. The first function in `completion-at-point-functions' to return a
  378. non-`nil' value is used by `completion-at-point'. The remaining
  379. functions are not called. The exception to this is when there is
  380. an `:exclusive' specification, as described above.
  381. The following function provides a convenient way to perform
  382. completion on an arbitrary stretch of text in an Emacs buffer:
  383. -- Function: completion-in-region start end collection &optional
  384. predicate
  385. This function completes the text in the current buffer between the
  386. positions START and END, using COLLECTION. The argument
  387. COLLECTION has the same meaning as in `try-completion' (*note
  388. Basic Completion::).
  389. This function inserts the completion text directly into the current
  390. buffer. Unlike `completing-read' (*note Minibuffer Completion::),
  391. it does not activate the minibuffer.
  392. For this function to work, point must be somewhere between START
  393. and END.
  394. 
  395. File: elisp, Node: Yes-or-No Queries, Next: Multiple Queries, Prev: Completion, Up: Minibuffers
  396. 20.7 Yes-or-No Queries
  397. ======================
  398. This section describes functions used to ask the user a yes-or-no
  399. question. The function `y-or-n-p' can be answered with a single
  400. character; it is useful for questions where an inadvertent wrong answer
  401. will not have serious consequences. `yes-or-no-p' is suitable for more
  402. momentous questions, since it requires three or four characters to
  403. answer.
  404. If either of these functions is called in a command that was invoked
  405. using the mouse--more precisely, if `last-nonmenu-event' (*note Command
  406. Loop Info::) is either `nil' or a list--then it uses a dialog box or
  407. pop-up menu to ask the question. Otherwise, it uses keyboard input.
  408. You can force use either of the mouse or of keyboard input by binding
  409. `last-nonmenu-event' to a suitable value around the call.
  410. Strictly speaking, `yes-or-no-p' uses the minibuffer and `y-or-n-p'
  411. does not; but it seems best to describe them together.
  412. -- Function: y-or-n-p prompt
  413. This function asks the user a question, expecting input in the echo
  414. area. It returns `t' if the user types `y', `nil' if the user
  415. types `n'. This function also accepts <SPC> to mean yes and <DEL>
  416. to mean no. It accepts `C-]' to mean "quit", like `C-g', because
  417. the question might look like a minibuffer and for that reason the
  418. user might try to use `C-]' to get out. The answer is a single
  419. character, with no <RET> needed to terminate it. Upper and lower
  420. case are equivalent.
  421. "Asking the question" means printing PROMPT in the echo area,
  422. followed by the string `(y or n) '. If the input is not one of
  423. the expected answers (`y', `n', `<SPC>', `<DEL>', or something
  424. that quits), the function responds `Please answer y or n.', and
  425. repeats the request.
  426. This function does not actually use the minibuffer, since it does
  427. not allow editing of the answer. It actually uses the echo area
  428. (*note The Echo Area::), which uses the same screen space as the
  429. minibuffer. The cursor moves to the echo area while the question
  430. is being asked.
  431. The answers and their meanings, even `y' and `n', are not
  432. hardwired. The keymap `query-replace-map' specifies them. *Note
  433. Search and Replace::.
  434. In the following example, the user first types `q', which is
  435. invalid. At the next prompt the user types `y'.
  436. (defun ask ()
  437. (interactive)
  438. (y-or-n-p "Do you need a lift? "))
  439. ;; After evaluation of the preceding definition, `M-x ask'
  440. ;; causes the following prompt to appear in the echo area:
  441. ---------- Echo area ----------
  442. Do you need a lift? (y or n)
  443. ---------- Echo area ----------
  444. ;; If the user then types `q', the following appears:
  445. ---------- Echo area ----------
  446. Please answer y or n. Do you need a lift? (y or n)
  447. ---------- Echo area ----------
  448. ;; When the user types a valid answer,
  449. ;; it is displayed after the question:
  450. ---------- Echo area ----------
  451. Do you need a lift? (y or n) y
  452. ---------- Echo area ----------
  453. We show successive lines of echo area messages, but only one
  454. actually appears on the screen at a time.
  455. -- Function: y-or-n-p-with-timeout prompt seconds default
  456. Like `y-or-n-p', except that if the user fails to answer within
  457. SECONDS seconds, this function stops waiting and returns DEFAULT.
  458. It works by setting up a timer; see *note Timers::. The argument
  459. SECONDS may be an integer or a floating point number.
  460. -- Function: yes-or-no-p prompt
  461. This function asks the user a question, expecting input in the
  462. minibuffer. It returns `t' if the user enters `yes', `nil' if the
  463. user types `no'. The user must type <RET> to finalize the
  464. response. Upper and lower case are equivalent.
  465. `yes-or-no-p' starts by displaying PROMPT in the echo area,
  466. followed by `(yes or no) '. The user must type one of the
  467. expected responses; otherwise, the function responds `Please answer
  468. yes or no.', waits about two seconds and repeats the request.
  469. `yes-or-no-p' requires more work from the user than `y-or-n-p' and
  470. is appropriate for more crucial decisions.
  471. Here is an example:
  472. (yes-or-no-p "Do you really want to remove everything? ")
  473. ;; After evaluation of the preceding expression,
  474. ;; the following prompt appears,
  475. ;; with an empty minibuffer:
  476. ---------- Buffer: minibuffer ----------
  477. Do you really want to remove everything? (yes or no)
  478. ---------- Buffer: minibuffer ----------
  479. If the user first types `y <RET>', which is invalid because this
  480. function demands the entire word `yes', it responds by displaying
  481. these prompts, with a brief pause between them:
  482. ---------- Buffer: minibuffer ----------
  483. Please answer yes or no.
  484. Do you really want to remove everything? (yes or no)
  485. ---------- Buffer: minibuffer ----------
  486. 
  487. File: elisp, Node: Multiple Queries, Next: Reading a Password, Prev: Yes-or-No Queries, Up: Minibuffers
  488. 20.8 Asking Multiple Y-or-N Questions
  489. =====================================
  490. When you have a series of similar questions to ask, such as "Do you
  491. want to save this buffer" for each buffer in turn, you should use
  492. `map-y-or-n-p' to ask the collection of questions, rather than asking
  493. each question individually. This gives the user certain convenient
  494. facilities such as the ability to answer the whole series at once.
  495. -- Function: map-y-or-n-p prompter actor list &optional help
  496. action-alist no-cursor-in-echo-area
  497. This function asks the user a series of questions, reading a
  498. single-character answer in the echo area for each one.
  499. The value of LIST specifies the objects to ask questions about.
  500. It should be either a list of objects or a generator function. If
  501. it is a function, it should expect no arguments, and should return
  502. either the next object to ask about, or `nil', meaning to stop
  503. asking questions.
  504. The argument PROMPTER specifies how to ask each question. If
  505. PROMPTER is a string, the question text is computed like this:
  506. (format PROMPTER OBJECT)
  507. where OBJECT is the next object to ask about (as obtained from
  508. LIST).
  509. If not a string, PROMPTER should be a function of one argument
  510. (the next object to ask about) and should return the question
  511. text. If the value is a string, that is the question to ask the
  512. user. The function can also return `t', meaning do act on this
  513. object (and don't ask the user), or `nil', meaning ignore this
  514. object (and don't ask the user).
  515. The argument ACTOR says how to act on the answers that the user
  516. gives. It should be a function of one argument, and it is called
  517. with each object that the user says yes for. Its argument is
  518. always an object obtained from LIST.
  519. If the argument HELP is given, it should be a list of this form:
  520. (SINGULAR PLURAL ACTION)
  521. where SINGULAR is a string containing a singular noun that
  522. describes the objects conceptually being acted on, PLURAL is the
  523. corresponding plural noun, and ACTION is a transitive verb
  524. describing what ACTOR does.
  525. If you don't specify HELP, the default is `("object" "objects"
  526. "act on")'.
  527. Each time a question is asked, the user may enter `y', `Y', or
  528. <SPC> to act on that object; `n', `N', or <DEL> to skip that
  529. object; `!' to act on all following objects; <ESC> or `q' to exit
  530. (skip all following objects); `.' (period) to act on the current
  531. object and then exit; or `C-h' to get help. These are the same
  532. answers that `query-replace' accepts. The keymap
  533. `query-replace-map' defines their meaning for `map-y-or-n-p' as
  534. well as for `query-replace'; see *note Search and Replace::.
  535. You can use ACTION-ALIST to specify additional possible answers
  536. and what they mean. It is an alist of elements of the form `(CHAR
  537. FUNCTION HELP)', each of which defines one additional answer. In
  538. this element, CHAR is a character (the answer); FUNCTION is a
  539. function of one argument (an object from LIST); HELP is a string.
  540. When the user responds with CHAR, `map-y-or-n-p' calls FUNCTION.
  541. If it returns non-`nil', the object is considered "acted upon",
  542. and `map-y-or-n-p' advances to the next object in LIST. If it
  543. returns `nil', the prompt is repeated for the same object.
  544. Normally, `map-y-or-n-p' binds `cursor-in-echo-area' while
  545. prompting. But if NO-CURSOR-IN-ECHO-AREA is non-`nil', it does
  546. not do that.
  547. If `map-y-or-n-p' is called in a command that was invoked using the
  548. mouse--more precisely, if `last-nonmenu-event' (*note Command Loop
  549. Info::) is either `nil' or a list--then it uses a dialog box or
  550. pop-up menu to ask the question. In this case, it does not use
  551. keyboard input or the echo area. You can force use either of the
  552. mouse or of keyboard input by binding `last-nonmenu-event' to a
  553. suitable value around the call.
  554. The return value of `map-y-or-n-p' is the number of objects acted
  555. on.
  556. 
  557. File: elisp, Node: Reading a Password, Next: Minibuffer Commands, Prev: Multiple Queries, Up: Minibuffers
  558. 20.9 Reading a Password
  559. =======================
  560. To read a password to pass to another program, you can use the function
  561. `read-passwd'.
  562. -- Function: read-passwd prompt &optional confirm default
  563. This function reads a password, prompting with PROMPT. It does
  564. not echo the password as the user types it; instead, it echoes `.'
  565. for each character in the password.
  566. The optional argument CONFIRM, if non-`nil', says to read the
  567. password twice and insist it must be the same both times. If it
  568. isn't the same, the user has to type it over and over until the
  569. last two times match.
  570. The optional argument DEFAULT specifies the default password to
  571. return if the user enters empty input. If DEFAULT is `nil', then
  572. `read-passwd' returns the null string in that case.
  573. 
  574. File: elisp, Node: Minibuffer Commands, Next: Minibuffer Windows, Prev: Reading a Password, Up: Minibuffers
  575. 20.10 Minibuffer Commands
  576. =========================
  577. This section describes some commands meant for use in the minibuffer.
  578. -- Command: exit-minibuffer
  579. This command exits the active minibuffer. It is normally bound to
  580. keys in minibuffer local keymaps.
  581. -- Command: self-insert-and-exit
  582. This command exits the active minibuffer after inserting the last
  583. character typed on the keyboard (found in `last-command-event';
  584. *note Command Loop Info::).
  585. -- Command: previous-history-element n
  586. This command replaces the minibuffer contents with the value of the
  587. Nth previous (older) history element.
  588. -- Command: next-history-element n
  589. This command replaces the minibuffer contents with the value of the
  590. Nth more recent history element.
  591. -- Command: previous-matching-history-element pattern n
  592. This command replaces the minibuffer contents with the value of the
  593. Nth previous (older) history element that matches PATTERN (a
  594. regular expression).
  595. -- Command: next-matching-history-element pattern n
  596. This command replaces the minibuffer contents with the value of the
  597. Nth next (newer) history element that matches PATTERN (a regular
  598. expression).
  599. -- Command: previous-complete-history-element n
  600. This command replaces the minibuffer contents with the value of the
  601. Nth previous (older) history element that completes the current
  602. contents of the minibuffer before the point.
  603. -- Command: next-complete-history-element n
  604. This command replaces the minibuffer contents with the value of the
  605. Nth next (newer) history element that completes the current
  606. contents of the minibuffer before the point.
  607. 
  608. File: elisp, Node: Minibuffer Windows, Next: Minibuffer Contents, Prev: Minibuffer Commands, Up: Minibuffers
  609. 20.11 Minibuffer Windows
  610. ========================
  611. These functions access and select minibuffer windows and test whether
  612. they are active.
  613. -- Function: active-minibuffer-window
  614. This function returns the currently active minibuffer window, or
  615. `nil' if there is none.
  616. -- Function: minibuffer-window &optional frame
  617. This function returns the minibuffer window used for frame FRAME.
  618. If FRAME is `nil', that stands for the current frame. Note that
  619. the minibuffer window used by a frame need not be part of that
  620. frame--a frame that has no minibuffer of its own necessarily uses
  621. some other frame's minibuffer window.
  622. -- Function: set-minibuffer-window window
  623. This function specifies WINDOW as the minibuffer window to use.
  624. This affects where the minibuffer is displayed if you put text in
  625. it without invoking the usual minibuffer commands. It has no
  626. effect on the usual minibuffer input functions because they all
  627. start by choosing the minibuffer window according to the current
  628. frame.
  629. -- Function: window-minibuffer-p &optional window
  630. This function returns non-`nil' if WINDOW is a minibuffer window.
  631. WINDOW defaults to the selected window.
  632. It is not correct to determine whether a given window is a
  633. minibuffer by comparing it with the result of `(minibuffer-window)',
  634. because there can be more than one minibuffer window if there is more
  635. than one frame.
  636. -- Function: minibuffer-window-active-p window
  637. This function returns non-`nil' if WINDOW is the currently active
  638. minibuffer window.
  639. 
  640. File: elisp, Node: Minibuffer Contents, Next: Recursive Mini, Prev: Minibuffer Windows, Up: Minibuffers
  641. 20.12 Minibuffer Contents
  642. =========================
  643. These functions access the minibuffer prompt and contents.
  644. -- Function: minibuffer-prompt
  645. This function returns the prompt string of the currently active
  646. minibuffer. If no minibuffer is active, it returns `nil'.
  647. -- Function: minibuffer-prompt-end
  648. This function returns the current position of the end of the
  649. minibuffer prompt, if a minibuffer is current. Otherwise, it
  650. returns the minimum valid buffer position.
  651. -- Function: minibuffer-prompt-width
  652. This function returns the current display-width of the minibuffer
  653. prompt, if a minibuffer is current. Otherwise, it returns zero.
  654. -- Function: minibuffer-contents
  655. This function returns the editable contents of the minibuffer
  656. (that is, everything except the prompt) as a string, if a
  657. minibuffer is current. Otherwise, it returns the entire contents
  658. of the current buffer.
  659. -- Function: minibuffer-contents-no-properties
  660. This is like `minibuffer-contents', except that it does not copy
  661. text properties, just the characters themselves. *Note Text
  662. Properties::.
  663. -- Function: minibuffer-completion-contents
  664. This is like `minibuffer-contents', except that it returns only
  665. the contents before point. That is the part that completion
  666. commands operate on. *Note Minibuffer Completion::.
  667. -- Function: delete-minibuffer-contents
  668. This function erases the editable contents of the minibuffer (that
  669. is, everything except the prompt), if a minibuffer is current.
  670. Otherwise, it erases the entire current buffer.
  671. 
  672. File: elisp, Node: Recursive Mini, Next: Minibuffer Misc, Prev: Minibuffer Contents, Up: Minibuffers
  673. 20.13 Recursive Minibuffers
  674. ===========================
  675. These functions and variables deal with recursive minibuffers (*note
  676. Recursive Editing::):
  677. -- Function: minibuffer-depth
  678. This function returns the current depth of activations of the
  679. minibuffer, a nonnegative integer. If no minibuffers are active,
  680. it returns zero.
  681. -- User Option: enable-recursive-minibuffers
  682. If this variable is non-`nil', you can invoke commands (such as
  683. `find-file') that use minibuffers even while the minibuffer window
  684. is active. Such invocation produces a recursive editing level for
  685. a new minibuffer. The outer-level minibuffer is invisible while
  686. you are editing the inner one.
  687. If this variable is `nil', you cannot invoke minibuffer commands
  688. when the minibuffer window is active, not even if you switch to
  689. another window to do it.
  690. If a command name has a property `enable-recursive-minibuffers' that
  691. is non-`nil', then the command can use the minibuffer to read arguments
  692. even if it is invoked from the minibuffer. A command can also achieve
  693. this by binding `enable-recursive-minibuffers' to `t' in the
  694. interactive declaration (*note Using Interactive::). The minibuffer
  695. command `next-matching-history-element' (normally `M-s' in the
  696. minibuffer) does the latter.
  697. 
  698. File: elisp, Node: Minibuffer Misc, Prev: Recursive Mini, Up: Minibuffers
  699. 20.14 Minibuffer Miscellany
  700. ===========================
  701. -- Function: minibufferp &optional buffer-or-name
  702. This function returns non-`nil' if BUFFER-OR-NAME is a minibuffer.
  703. If BUFFER-OR-NAME is omitted, it tests the current buffer.
  704. -- Variable: minibuffer-setup-hook
  705. This is a normal hook that is run whenever the minibuffer is
  706. entered. *Note Hooks::.
  707. -- Variable: minibuffer-exit-hook
  708. This is a normal hook that is run whenever the minibuffer is
  709. exited. *Note Hooks::.
  710. -- Variable: minibuffer-help-form
  711. The current value of this variable is used to rebind `help-form'
  712. locally inside the minibuffer (*note Help Functions::).
  713. -- Variable: minibuffer-scroll-window
  714. If the value of this variable is non-`nil', it should be a window
  715. object. When the function `scroll-other-window' is called in the
  716. minibuffer, it scrolls this window.
  717. -- Function: minibuffer-selected-window
  718. This function returns the window that was selected when the
  719. minibuffer was entered. If selected window is not a minibuffer
  720. window, it returns `nil'.
  721. -- User Option: max-mini-window-height
  722. This variable specifies the maximum height for resizing minibuffer
  723. windows. If a float, it specifies a fraction of the height of the
  724. frame. If an integer, it specifies a number of lines.
  725. -- Function: minibuffer-message string &rest args
  726. This function displays STRING temporarily at the end of the
  727. minibuffer text, for a few seconds, or until the next input event
  728. arrives, whichever comes first. The variable
  729. `minibuffer-message-timeout' specifies the number of seconds to
  730. wait in the absence of input. It defaults to 2. If ARGS is
  731. non-`nil', the actual message is obtained by passing STRING and
  732. ARGS through `format'. *Note Formatting Strings::.
  733. -- Command: minibuffer-inactive-mode
  734. This is the major mode used in inactive minibuffers. It uses
  735. keymap `minibuffer-inactive-mode-map'. This can be useful if the
  736. minibuffer is in a separate frame. *Note Minibuffers and Frames::.
  737. 
  738. File: elisp, Node: Command Loop, Next: Keymaps, Prev: Minibuffers, Up: Top
  739. 21 Command Loop
  740. ***************
  741. When you run Emacs, it enters the "editor command loop" almost
  742. immediately. This loop reads key sequences, executes their definitions,
  743. and displays the results. In this chapter, we describe how these things
  744. are done, and the subroutines that allow Lisp programs to do them.
  745. * Menu:
  746. * Command Overview:: How the command loop reads commands.
  747. * Defining Commands:: Specifying how a function should read arguments.
  748. * Interactive Call:: Calling a command, so that it will read arguments.
  749. * Distinguish Interactive:: Making a command distinguish interactive calls.
  750. * Command Loop Info:: Variables set by the command loop for you to examine.
  751. * Adjusting Point:: Adjustment of point after a command.
  752. * Input Events:: What input looks like when you read it.
  753. * Reading Input:: How to read input events from the keyboard or mouse.
  754. * Special Events:: Events processed immediately and individually.
  755. * Waiting:: Waiting for user input or elapsed time.
  756. * Quitting:: How C-g works. How to catch or defer quitting.
  757. * Prefix Command Arguments:: How the commands to set prefix args work.
  758. * Recursive Editing:: Entering a recursive edit,
  759. and why you usually shouldn't.
  760. * Disabling Commands:: How the command loop handles disabled commands.
  761. * Command History:: How the command history is set up, and how accessed.
  762. * Keyboard Macros:: How keyboard macros are implemented.
  763. 
  764. File: elisp, Node: Command Overview, Next: Defining Commands, Up: Command Loop
  765. 21.1 Command Loop Overview
  766. ==========================
  767. The first thing the command loop must do is read a key sequence, which
  768. is a sequence of input events that translates into a command. It does
  769. this by calling the function `read-key-sequence'. Lisp programs can
  770. also call this function (*note Key Sequence Input::). They can also
  771. read input at a lower level with `read-key' or `read-event' (*note
  772. Reading One Event::), or discard pending input with `discard-input'
  773. (*note Event Input Misc::).
  774. The key sequence is translated into a command through the currently
  775. active keymaps. *Note Key Lookup::, for information on how this is
  776. done. The result should be a keyboard macro or an interactively
  777. callable function. If the key is `M-x', then it reads the name of
  778. another command, which it then calls. This is done by the command
  779. `execute-extended-command' (*note Interactive Call::).
  780. Prior to executing the command, Emacs runs `undo-boundary' to create
  781. an undo boundary. *Note Maintaining Undo::.
  782. To execute a command, Emacs first reads its arguments by calling
  783. `command-execute' (*note Interactive Call::). For commands written in
  784. Lisp, the `interactive' specification says how to read the arguments.
  785. This may use the prefix argument (*note Prefix Command Arguments::) or
  786. may read with prompting in the minibuffer (*note Minibuffers::). For
  787. example, the command `find-file' has an `interactive' specification
  788. which says to read a file name using the minibuffer. The function body
  789. of `find-file' does not use the minibuffer, so if you call `find-file'
  790. as a function from Lisp code, you must supply the file name string as
  791. an ordinary Lisp function argument.
  792. If the command is a keyboard macro (i.e. a string or vector), Emacs
  793. executes it using `execute-kbd-macro' (*note Keyboard Macros::).
  794. -- Variable: pre-command-hook
  795. This normal hook is run by the editor command loop before it
  796. executes each command. At that time, `this-command' contains the
  797. command that is about to run, and `last-command' describes the
  798. previous command. *Note Command Loop Info::.
  799. -- Variable: post-command-hook
  800. This normal hook is run by the editor command loop after it
  801. executes each command (including commands terminated prematurely
  802. by quitting or by errors). At that time, `this-command' refers to
  803. the command that just ran, and `last-command' refers to the
  804. command before that.
  805. This hook is also run when Emacs first enters the command loop (at
  806. which point `this-command' and `last-command' are both `nil').
  807. Quitting is suppressed while running `pre-command-hook' and
  808. `post-command-hook'. If an error happens while executing one of these
  809. hooks, it does not terminate execution of the hook; instead the error
  810. is silenced and the function in which the error occurred is removed
  811. from the hook.
  812. A request coming into the Emacs server (*note Emacs Server:
  813. (emacs)Emacs Server.) runs these two hooks just as a keyboard command
  814. does.
  815. 
  816. File: elisp, Node: Defining Commands, Next: Interactive Call, Prev: Command Overview, Up: Command Loop
  817. 21.2 Defining Commands
  818. ======================
  819. The special form `interactive' turns a Lisp function into a command.
  820. The `interactive' form must be located at top-level in the function
  821. body (usually as the first form in the body), or in the
  822. `interactive-form' property of the function symbol. When the
  823. `interactive' form is located in the function body, it does nothing
  824. when actually executed. Its presence serves as a flag, which tells the
  825. Emacs command loop that the function can be called interactively. The
  826. argument of the `interactive' form controls the reading of arguments
  827. for an interactive call.
  828. * Menu:
  829. * Using Interactive:: General rules for `interactive'.
  830. * Interactive Codes:: The standard letter-codes for reading arguments
  831. in various ways.
  832. * Interactive Examples:: Examples of how to read interactive arguments.
  833. 
  834. File: elisp, Node: Using Interactive, Next: Interactive Codes, Up: Defining Commands
  835. 21.2.1 Using `interactive'
  836. --------------------------
  837. This section describes how to write the `interactive' form that makes a
  838. Lisp function an interactively-callable command, and how to examine a
  839. command's `interactive' form.
  840. -- Special Form: interactive arg-descriptor
  841. This special form declares that a function is a command, and that
  842. it may therefore be called interactively (via `M-x' or by entering
  843. a key sequence bound to it). The argument ARG-DESCRIPTOR declares
  844. how to compute the arguments to the command when the command is
  845. called interactively.
  846. A command may be called from Lisp programs like any other
  847. function, but then the caller supplies the arguments and
  848. ARG-DESCRIPTOR has no effect.
  849. The `interactive' form must be located at top-level in the
  850. function body, or in the function symbol's `interactive-form'
  851. property (*note Symbol Plists::). It has its effect because the
  852. command loop looks for it before calling the function (*note
  853. Interactive Call::). Once the function is called, all its body
  854. forms are executed; at this time, if the `interactive' form occurs
  855. within the body, the form simply returns `nil' without even
  856. evaluating its argument.
  857. By convention, you should put the `interactive' form in the
  858. function body, as the first top-level form. If there is an
  859. `interactive' form in both the `interactive-form' symbol property
  860. and the function body, the former takes precedence. The
  861. `interactive-form' symbol property can be used to add an
  862. interactive form to an existing function, or change how its
  863. arguments are processed interactively, without redefining the
  864. function.
  865. There are three possibilities for the argument ARG-DESCRIPTOR:
  866. * It may be omitted or `nil'; then the command is called with no
  867. arguments. This leads quickly to an error if the command requires
  868. one or more arguments.
  869. * It may be a string; its contents are a sequence of elements
  870. separated by newlines, one for each argument(1). Each element
  871. consists of a code character (*note Interactive Codes::)
  872. optionally followed by a prompt (which some code characters use
  873. and some ignore). Here is an example:
  874. (interactive "P\nbFrobnicate buffer: ")
  875. The code letter `P' sets the command's first argument to the raw
  876. command prefix (*note Prefix Command Arguments::). `bFrobnicate
  877. buffer: ' prompts the user with `Frobnicate buffer: ' to enter the
  878. name of an existing buffer, which becomes the second and final
  879. argument.
  880. The prompt string can use `%' to include previous argument values
  881. (starting with the first argument) in the prompt. This is done
  882. using `format' (*note Formatting Strings::). For example, here is
  883. how you could read the name of an existing buffer followed by a
  884. new name to give to that buffer:
  885. (interactive "bBuffer to rename: \nsRename buffer %s to: ")
  886. If `*' appears at the beginning of the string, then an error is
  887. signaled if the buffer is read-only.
  888. If `@' appears at the beginning of the string, and if the key
  889. sequence used to invoke the command includes any mouse events, then
  890. the window associated with the first of those events is selected
  891. before the command is run.
  892. If `^' appears at the beginning of the string, and if the command
  893. was invoked through "shift-translation", set the mark and activate
  894. the region temporarily, or extend an already active region, before
  895. the command is run. If the command was invoked without
  896. shift-translation, and the region is temporarily active,
  897. deactivate the region before the command is run.
  898. Shift-translation is controlled on the user level by
  899. `shift-select-mode'; see *note Shift Selection: (emacs)Shift
  900. Selection.
  901. You can use `*', `@', and `^' together; the order does not matter.
  902. Actual reading of arguments is controlled by the rest of the
  903. prompt string (starting with the first character that is not `*',
  904. `@', or `^').
  905. * It may be a Lisp expression that is not a string; then it should
  906. be a form that is evaluated to get a list of arguments to pass to
  907. the command. Usually this form will call various functions to
  908. read input from the user, most often through the minibuffer (*note
  909. Minibuffers::) or directly from the keyboard (*note Reading
  910. Input::).
  911. Providing point or the mark as an argument value is also common,
  912. but if you do this _and_ read input (whether using the minibuffer
  913. or not), be sure to get the integer values of point or the mark
  914. after reading. The current buffer may be receiving subprocess
  915. output; if subprocess output arrives while the command is waiting
  916. for input, it could relocate point and the mark.
  917. Here's an example of what _not_ to do:
  918. (interactive
  919. (list (region-beginning) (region-end)
  920. (read-string "Foo: " nil 'my-history)))
  921. Here's how to avoid the problem, by examining point and the mark
  922. after reading the keyboard input:
  923. (interactive
  924. (let ((string (read-string "Foo: " nil 'my-history)))
  925. (list (region-beginning) (region-end) string)))
  926. *Warning:* the argument values should not include any data types
  927. that can't be printed and then read. Some facilities save
  928. `command-history' in a file to be read in the subsequent sessions;
  929. if a command's arguments contain a data type that prints using
  930. `#<...>' syntax, those facilities won't work.
  931. There are, however, a few exceptions: it is ok to use a limited
  932. set of expressions such as `(point)', `(mark)',
  933. `(region-beginning)', and `(region-end)', because Emacs recognizes
  934. them specially and puts the expression (rather than its value)
  935. into the command history. To see whether the expression you wrote
  936. is one of these exceptions, run the command, then examine `(car
  937. command-history)'.
  938. -- Function: interactive-form function
  939. This function returns the `interactive' form of FUNCTION. If
  940. FUNCTION is an interactively callable function (*note Interactive
  941. Call::), the value is the command's `interactive' form
  942. `(interactive SPEC)', which specifies how to compute its
  943. arguments. Otherwise, the value is `nil'. If FUNCTION is a
  944. symbol, its function definition is used.
  945. ---------- Footnotes ----------
  946. (1) Some elements actually supply two arguments.
  947. 
  948. File: elisp, Node: Interactive Codes, Next: Interactive Examples, Prev: Using Interactive, Up: Defining Commands
  949. 21.2.2 Code Characters for `interactive'
  950. ----------------------------------------
  951. The code character descriptions below contain a number of key words,
  952. defined here as follows:
  953. Completion
  954. Provide completion. <TAB>, <SPC>, and <RET> perform name
  955. completion because the argument is read using `completing-read'
  956. (*note Completion::). `?' displays a list of possible completions.
  957. Existing
  958. Require the name of an existing object. An invalid name is not
  959. accepted; the commands to exit the minibuffer do not exit if the
  960. current input is not valid.
  961. Default
  962. A default value of some sort is used if the user enters no text in
  963. the minibuffer. The default depends on the code character.
  964. No I/O
  965. This code letter computes an argument without reading any input.
  966. Therefore, it does not use a prompt string, and any prompt string
  967. you supply is ignored.
  968. Even though the code letter doesn't use a prompt string, you must
  969. follow it with a newline if it is not the last code character in
  970. the string.
  971. Prompt
  972. A prompt immediately follows the code character. The prompt ends
  973. either with the end of the string or with a newline.
  974. Special
  975. This code character is meaningful only at the beginning of the
  976. interactive string, and it does not look for a prompt or a newline.
  977. It is a single, isolated character.
  978. Here are the code character descriptions for use with `interactive':
  979. `*'
  980. Signal an error if the current buffer is read-only. Special.
  981. `@'
  982. Select the window mentioned in the first mouse event in the key
  983. sequence that invoked this command. Special.
  984. `^'
  985. If the command was invoked through shift-translation, set the mark
  986. and activate the region temporarily, or extend an already active
  987. region, before the command is run. If the command was invoked
  988. without shift-translation, and the region is temporarily active,
  989. deactivate the region before the command is run. Special.
  990. `a'
  991. A function name (i.e., a symbol satisfying `fboundp'). Existing,
  992. Completion, Prompt.
  993. `b'
  994. The name of an existing buffer. By default, uses the name of the
  995. current buffer (*note Buffers::). Existing, Completion, Default,
  996. Prompt.
  997. `B'
  998. A buffer name. The buffer need not exist. By default, uses the
  999. name of a recently used buffer other than the current buffer.
  1000. Completion, Default, Prompt.
  1001. `c'
  1002. A character. The cursor does not move into the echo area. Prompt.
  1003. `C'
  1004. A command name (i.e., a symbol satisfying `commandp'). Existing,
  1005. Completion, Prompt.
  1006. `d'
  1007. The position of point, as an integer (*note Point::). No I/O.
  1008. `D'
  1009. A directory name. The default is the current default directory of
  1010. the current buffer, `default-directory' (*note File Name
  1011. Expansion::). Existing, Completion, Default, Prompt.
  1012. `e'
  1013. The first or next mouse event in the key sequence that invoked the
  1014. command. More precisely, `e' gets events that are lists, so you
  1015. can look at the data in the lists. *Note Input Events::. No I/O.
  1016. You can use `e' more than once in a single command's interactive
  1017. specification. If the key sequence that invoked the command has N
  1018. events that are lists, the Nth `e' provides the Nth such event.
  1019. Events that are not lists, such as function keys and ASCII
  1020. characters, do not count where `e' is concerned.
  1021. `f'
  1022. A file name of an existing file (*note File Names::). The default
  1023. directory is `default-directory'. Existing, Completion, Default,
  1024. Prompt.
  1025. `F'
  1026. A file name. The file need not exist. Completion, Default,
  1027. Prompt.
  1028. `G'
  1029. A file name. The file need not exist. If the user enters just a
  1030. directory name, then the value is just that directory name, with no
  1031. file name within the directory added. Completion, Default, Prompt.
  1032. `i'
  1033. An irrelevant argument. This code always supplies `nil' as the
  1034. argument's value. No I/O.
  1035. `k'
  1036. A key sequence (*note Key Sequences::). This keeps reading events
  1037. until a command (or undefined command) is found in the current key
  1038. maps. The key sequence argument is represented as a string or
  1039. vector. The cursor does not move into the echo area. Prompt.
  1040. If `k' reads a key sequence that ends with a down-event, it also
  1041. reads and discards the following up-event. You can get access to
  1042. that up-event with the `U' code character.
  1043. This kind of input is used by commands such as `describe-key' and
  1044. `global-set-key'.
  1045. `K'
  1046. A key sequence, whose definition you intend to change. This works
  1047. like `k', except that it suppresses, for the last input event in
  1048. the key sequence, the conversions that are normally used (when
  1049. necessary) to convert an undefined key into a defined one.
  1050. `m'
  1051. The position of the mark, as an integer. No I/O.
  1052. `M'
  1053. Arbitrary text, read in the minibuffer using the current buffer's
  1054. input method, and returned as a string (*note Input Methods:
  1055. (emacs)Input Methods.). Prompt.
  1056. `n'
  1057. A number, read with the minibuffer. If the input is not a number,
  1058. the user has to try again. `n' never uses the prefix argument.
  1059. Prompt.
  1060. `N'
  1061. The numeric prefix argument; but if there is no prefix argument,
  1062. read a number as with `n'. The value is always a number. *Note
  1063. Prefix Command Arguments::. Prompt.
  1064. `p'
  1065. The numeric prefix argument. (Note that this `p' is lower case.)
  1066. No I/O.
  1067. `P'
  1068. The raw prefix argument. (Note that this `P' is upper case.) No
  1069. I/O.
  1070. `r'
  1071. Point and the mark, as two numeric arguments, smallest first.
  1072. This is the only code letter that specifies two successive
  1073. arguments rather than one. No I/O.
  1074. `s'
  1075. Arbitrary text, read in the minibuffer and returned as a string
  1076. (*note Text from Minibuffer::). Terminate the input with either
  1077. `C-j' or <RET>. (`C-q' may be used to include either of these
  1078. characters in the input.) Prompt.
  1079. `S'
  1080. An interned symbol whose name is read in the minibuffer. Any
  1081. whitespace character terminates the input. (Use `C-q' to include
  1082. whitespace in the string.) Other characters that normally
  1083. terminate a symbol (e.g., parentheses and brackets) do not do so
  1084. here. Prompt.
  1085. `U'
  1086. A key sequence or `nil'. Can be used after a `k' or `K' argument
  1087. to get the up-event that was discarded (if any) after `k' or `K'
  1088. read a down-event. If no up-event has been discarded, `U'
  1089. provides `nil' as the argument. No I/O.
  1090. `v'
  1091. A variable declared to be a user option (i.e., satisfying the
  1092. predicate `user-variable-p'). This reads the variable using
  1093. `read-variable'. *Note Definition of read-variable::. Existing,
  1094. Completion, Prompt.
  1095. `x'
  1096. A Lisp object, specified with its read syntax, terminated with a
  1097. `C-j' or <RET>. The object is not evaluated. *Note Object from
  1098. Minibuffer::. Prompt.
  1099. `X'
  1100. A Lisp form's value. `X' reads as `x' does, then evaluates the
  1101. form so that its value becomes the argument for the command.
  1102. Prompt.
  1103. `z'
  1104. A coding system name (a symbol). If the user enters null input,
  1105. the argument value is `nil'. *Note Coding Systems::. Completion,
  1106. Existing, Prompt.
  1107. `Z'
  1108. A coding system name (a symbol)--but only if this command has a
  1109. prefix argument. With no prefix argument, `Z' provides `nil' as
  1110. the argument value. Completion, Existing, Prompt.
  1111. 
  1112. File: elisp, Node: Interactive Examples, Prev: Interactive Codes, Up: Defining Commands
  1113. 21.2.3 Examples of Using `interactive'
  1114. --------------------------------------
  1115. Here are some examples of `interactive':
  1116. (defun foo1 () ; `foo1' takes no arguments,
  1117. (interactive) ; just moves forward two words.
  1118. (forward-word 2))
  1119. => foo1
  1120. (defun foo2 (n) ; `foo2' takes one argument,
  1121. (interactive "^p") ; which is the numeric prefix.
  1122. ; under `shift-select-mode',
  1123. ; will activate or extend region.
  1124. (forward-word (* 2 n)))
  1125. => foo2
  1126. (defun foo3 (n) ; `foo3' takes one argument,
  1127. (interactive "nCount:") ; which is read with the Minibuffer.
  1128. (forward-word (* 2 n)))
  1129. => foo3
  1130. (defun three-b (b1 b2 b3)
  1131. "Select three existing buffers.
  1132. Put them into three windows, selecting the last one."
  1133. (interactive "bBuffer1:\nbBuffer2:\nbBuffer3:")
  1134. (delete-other-windows)
  1135. (split-window (selected-window) 8)
  1136. (switch-to-buffer b1)
  1137. (other-window 1)
  1138. (split-window (selected-window) 8)
  1139. (switch-to-buffer b2)
  1140. (other-window 1)
  1141. (switch-to-buffer b3))
  1142. => three-b
  1143. (three-b "*scratch*" "declarations.texi" "*mail*")
  1144. => nil
  1145. 
  1146. File: elisp, Node: Interactive Call, Next: Distinguish Interactive, Prev: Defining Commands, Up: Command Loop
  1147. 21.3 Interactive Call
  1148. =====================
  1149. After the command loop has translated a key sequence into a command, it
  1150. invokes that command using the function `command-execute'. If the
  1151. command is a function, `command-execute' calls `call-interactively',
  1152. which reads the arguments and calls the command. You can also call
  1153. these functions yourself.
  1154. Note that the term "command", in this context, refers to an
  1155. interactively callable function (or function-like object), or a
  1156. keyboard macro. It does not refer to the key sequence used to invoke a
  1157. command (*note Keymaps::).
  1158. -- Function: commandp object &optional for-call-interactively
  1159. This function returns `t' if OBJECT is a command. Otherwise, it
  1160. returns `nil'.
  1161. Commands include strings and vectors (which are treated as keyboard
  1162. macros), lambda expressions that contain a top-level `interactive'
  1163. form (*note Using Interactive::), byte-code function objects made
  1164. from such lambda expressions, autoload objects that are declared
  1165. as interactive (non-`nil' fourth argument to `autoload'), and some
  1166. primitive functions. Also, a symbol is considered a command if it
  1167. has a non-`nil' `interactive-form' property, or if its function
  1168. definition satisfies `commandp'.
  1169. If FOR-CALL-INTERACTIVELY is non-`nil', then `commandp' returns
  1170. `t' only for objects that `call-interactively' could call--thus,
  1171. not for keyboard macros.
  1172. See `documentation' in *note Accessing Documentation::, for a
  1173. realistic example of using `commandp'.
  1174. -- Function: call-interactively command &optional record-flag keys
  1175. This function calls the interactively callable function COMMAND,
  1176. providing arguments according to its interactive calling
  1177. specifications. It returns whatever COMMAND returns.
  1178. If, for instance, you have a function with the following signature:
  1179. (defun foo (begin end)
  1180. (interactive "r")
  1181. ...)
  1182. then saying
  1183. (call-interactively 'foo)
  1184. will call `foo' with the region (`point' and `mark') as the
  1185. arguments.
  1186. An error is signaled if COMMAND is not a function or if it cannot
  1187. be called interactively (i.e., is not a command). Note that
  1188. keyboard macros (strings and vectors) are not accepted, even though
  1189. they are considered commands, because they are not functions. If
  1190. COMMAND is a symbol, then `call-interactively' uses its function
  1191. definition.
  1192. If RECORD-FLAG is non-`nil', then this command and its arguments
  1193. are unconditionally added to the list `command-history'.
  1194. Otherwise, the command is added only if it uses the minibuffer to
  1195. read an argument. *Note Command History::.
  1196. The argument KEYS, if given, should be a vector which specifies
  1197. the sequence of events to supply if the command inquires which
  1198. events were used to invoke it. If KEYS is omitted or `nil', the
  1199. default is the return value of `this-command-keys-vector'. *Note
  1200. Definition of this-command-keys-vector::.
  1201. -- Function: command-execute command &optional record-flag keys special
  1202. This function executes COMMAND. The argument COMMAND must satisfy
  1203. the `commandp' predicate; i.e., it must be an interactively
  1204. callable function or a keyboard macro.
  1205. A string or vector as COMMAND is executed with
  1206. `execute-kbd-macro'. A function is passed to `call-interactively'
  1207. (see above), along with the RECORD-FLAG and KEYS arguments.
  1208. If COMMAND is a symbol, its function definition is used in its
  1209. place. A symbol with an `autoload' definition counts as a command
  1210. if it was declared to stand for an interactively callable
  1211. function. Such a definition is handled by loading the specified
  1212. library and then rechecking the definition of the symbol.
  1213. The argument SPECIAL, if given, means to ignore the prefix
  1214. argument and not clear it. This is used for executing special
  1215. events (*note Special Events::).
  1216. -- Command: execute-extended-command prefix-argument
  1217. This function reads a command name from the minibuffer using
  1218. `completing-read' (*note Completion::). Then it uses
  1219. `command-execute' to call the specified command. Whatever that
  1220. command returns becomes the value of `execute-extended-command'.
  1221. If the command asks for a prefix argument, it receives the value
  1222. PREFIX-ARGUMENT. If `execute-extended-command' is called
  1223. interactively, the current raw prefix argument is used for
  1224. PREFIX-ARGUMENT, and thus passed on to whatever command is run.
  1225. `execute-extended-command' is the normal definition of `M-x', so
  1226. it uses the string `M-x ' as a prompt. (It would be better to
  1227. take the prompt from the events used to invoke
  1228. `execute-extended-command', but that is painful to implement.) A
  1229. description of the value of the prefix argument, if any, also
  1230. becomes part of the prompt.
  1231. (execute-extended-command 3)
  1232. ---------- Buffer: Minibuffer ----------
  1233. 3 M-x forward-word RET
  1234. ---------- Buffer: Minibuffer ----------
  1235. => t
  1236. 
  1237. File: elisp, Node: Distinguish Interactive, Next: Command Loop Info, Prev: Interactive Call, Up: Command Loop
  1238. 21.4 Distinguish Interactive Calls
  1239. ==================================
  1240. Sometimes a command should display additional visual feedback (such as
  1241. an informative message in the echo area) for interactive calls only.
  1242. There are three ways to do this. The recommended way to test whether
  1243. the function was called using `call-interactively' is to give it an
  1244. optional argument `print-message' and use the `interactive' spec to
  1245. make it non-`nil' in interactive calls. Here's an example:
  1246. (defun foo (&optional print-message)
  1247. (interactive "p")
  1248. (when print-message
  1249. (message "foo")))
  1250. We use `"p"' because the numeric prefix argument is never `nil'.
  1251. Defined in this way, the function does display the message when called
  1252. from a keyboard macro.
  1253. The above method with the additional argument is usually best,
  1254. because it allows callers to say "treat this call as interactive". But
  1255. you can also do the job by testing `called-interactively-p'.
  1256. -- Function: called-interactively-p kind
  1257. This function returns `t' when the calling function was called
  1258. using `call-interactively'.
  1259. The argument KIND should be either the symbol `interactive' or the
  1260. symbol `any'. If it is `interactive', then
  1261. `called-interactively-p' returns `t' only if the call was made
  1262. directly by the user--e.g., if the user typed a key sequence bound
  1263. to the calling function, but _not_ if the user ran a keyboard
  1264. macro that called the function (*note Keyboard Macros::). If KIND
  1265. is `any', `called-interactively-p' returns `t' for any kind of
  1266. interactive call, including keyboard macros.
  1267. If in doubt, use `any'; the only known proper use of `interactive'
  1268. is if you need to decide whether to display a helpful message
  1269. while a function is running.
  1270. A function is never considered to be called interactively if it was
  1271. called via Lisp evaluation (or with `apply' or `funcall').
  1272. Here is an example of using `called-interactively-p':
  1273. (defun foo ()
  1274. (interactive)
  1275. (when (called-interactively-p 'any)
  1276. (message "Interactive!")
  1277. 'foo-called-interactively))
  1278. ;; Type `M-x foo'.
  1279. -| Interactive!
  1280. (foo)
  1281. => nil
  1282. Here is another example that contrasts direct and indirect calls to
  1283. `called-interactively-p'.
  1284. (defun bar ()
  1285. (interactive)
  1286. (message "%s" (list (foo) (called-interactively-p 'any))))
  1287. ;; Type `M-x bar'.
  1288. -| (nil t)
  1289. 
  1290. File: elisp, Node: Command Loop Info, Next: Adjusting Point, Prev: Distinguish Interactive, Up: Command Loop
  1291. 21.5 Information from the Command Loop
  1292. ======================================
  1293. The editor command loop sets several Lisp variables to keep status
  1294. records for itself and for commands that are run. With the exception of
  1295. `this-command' and `last-command' it's generally a bad idea to change
  1296. any of these variables in a Lisp program.
  1297. -- Variable: last-command
  1298. This variable records the name of the previous command executed by
  1299. the command loop (the one before the current command). Normally
  1300. the value is a symbol with a function definition, but this is not
  1301. guaranteed.
  1302. The value is copied from `this-command' when a command returns to
  1303. the command loop, except when the command has specified a prefix
  1304. argument for the following command.
  1305. This variable is always local to the current terminal and cannot be
  1306. buffer-local. *Note Multiple Terminals::.
  1307. -- Variable: real-last-command
  1308. This variable is set up by Emacs just like `last-command', but
  1309. never altered by Lisp programs.
  1310. -- Variable: last-repeatable-command
  1311. This variable stores the most recently executed command that was
  1312. not part of an input event. This is the command `repeat' will try
  1313. to repeat, *Note Repeating: (emacs)Repeating.
  1314. -- Variable: this-command
  1315. This variable records the name of the command now being executed by
  1316. the editor command loop. Like `last-command', it is normally a
  1317. symbol with a function definition.
  1318. The command loop sets this variable just before running a command,
  1319. and copies its value into `last-command' when the command finishes
  1320. (unless the command specified a prefix argument for the following
  1321. command).
  1322. Some commands set this variable during their execution, as a flag
  1323. for whatever command runs next. In particular, the functions for
  1324. killing text set `this-command' to `kill-region' so that any kill
  1325. commands immediately following will know to append the killed text
  1326. to the previous kill.
  1327. If you do not want a particular command to be recognized as the
  1328. previous command in the case where it got an error, you must code that
  1329. command to prevent this. One way is to set `this-command' to `t' at the
  1330. beginning of the command, and set `this-command' back to its proper
  1331. value at the end, like this:
  1332. (defun foo (args...)
  1333. (interactive ...)
  1334. (let ((old-this-command this-command))
  1335. (setq this-command t)
  1336. ...do the work...
  1337. (setq this-command old-this-command)))
  1338. We do not bind `this-command' with `let' because that would restore the
  1339. old value in case of error--a feature of `let' which in this case does
  1340. precisely what we want to avoid.
  1341. -- Variable: this-original-command
  1342. This has the same value as `this-command' except when command
  1343. remapping occurs (*note Remapping Commands::). In that case,
  1344. `this-command' gives the command actually run (the result of
  1345. remapping), and `this-original-command' gives the command that was
  1346. specified to run but remapped into another command.
  1347. -- Function: this-command-keys
  1348. This function returns a string or vector containing the key
  1349. sequence that invoked the present command, plus any previous
  1350. commands that generated the prefix argument for this command. Any
  1351. events read by the command using `read-event' without a timeout
  1352. get tacked on to the end.
  1353. However, if the command has called `read-key-sequence', it returns
  1354. the last read key sequence. *Note Key Sequence Input::. The
  1355. value is a string if all events in the sequence were characters
  1356. that fit in a string. *Note Input Events::.
  1357. (this-command-keys)
  1358. ;; Now use `C-u C-x C-e' to evaluate that.
  1359. => "^U^X^E"
  1360. -- Function: this-command-keys-vector
  1361. Like `this-command-keys', except that it always returns the events
  1362. in a vector, so you don't need to deal with the complexities of
  1363. storing input events in a string (*note Strings of Events::).
  1364. -- Function: clear-this-command-keys &optional keep-record
  1365. This function empties out the table of events for
  1366. `this-command-keys' to return. Unless KEEP-RECORD is non-`nil',
  1367. it also empties the records that the function `recent-keys' (*note
  1368. Recording Input::) will subsequently return. This is useful after
  1369. reading a password, to prevent the password from echoing
  1370. inadvertently as part of the next command in certain cases.
  1371. -- Variable: last-nonmenu-event
  1372. This variable holds the last input event read as part of a key
  1373. sequence, not counting events resulting from mouse menus.
  1374. One use of this variable is for telling `x-popup-menu' where to pop
  1375. up a menu. It is also used internally by `y-or-n-p' (*note
  1376. Yes-or-No Queries::).
  1377. -- Variable: last-command-event
  1378. -- Variable: last-command-char
  1379. This variable is set to the last input event that was read by the
  1380. command loop as part of a command. The principal use of this
  1381. variable is in `self-insert-command', which uses it to decide which
  1382. character to insert.
  1383. last-command-event
  1384. ;; Now use `C-u C-x C-e' to evaluate that.
  1385. => 5
  1386. The value is 5 because that is the ASCII code for `C-e'.
  1387. The alias `last-command-char' is obsolete.
  1388. -- Variable: last-event-frame
  1389. This variable records which frame the last input event was
  1390. directed to. Usually this is the frame that was selected when the
  1391. event was generated, but if that frame has redirected input focus
  1392. to another frame, the value is the frame to which the event was
  1393. redirected. *Note Input Focus::.
  1394. If the last event came from a keyboard macro, the value is `macro'.
  1395. 
  1396. File: elisp, Node: Adjusting Point, Next: Input Events, Prev: Command Loop Info, Up: Command Loop
  1397. 21.6 Adjusting Point After Commands
  1398. ===================================
  1399. It is not easy to display a value of point in the middle of a sequence
  1400. of text that has the `display', `composition' or is invisible.
  1401. Therefore, after a command finishes and returns to the command loop, if
  1402. point is within such a sequence, the command loop normally moves point
  1403. to the edge of the sequence.
  1404. A command can inhibit this feature by setting the variable
  1405. `disable-point-adjustment':
  1406. -- Variable: disable-point-adjustment
  1407. If this variable is non-`nil' when a command returns to the
  1408. command loop, then the command loop does not check for those text
  1409. properties, and does not move point out of sequences that have
  1410. them.
  1411. The command loop sets this variable to `nil' before each command,
  1412. so if a command sets it, the effect applies only to that command.
  1413. -- Variable: global-disable-point-adjustment
  1414. If you set this variable to a non-`nil' value, the feature of
  1415. moving point out of these sequences is completely turned off.
  1416. 
  1417. File: elisp, Node: Input Events, Next: Reading Input, Prev: Adjusting Point, Up: Command Loop
  1418. 21.7 Input Events
  1419. =================
  1420. The Emacs command loop reads a sequence of "input events" that
  1421. represent keyboard or mouse activity. The events for keyboard activity
  1422. are characters or symbols; mouse events are always lists. This section
  1423. describes the representation and meaning of input events in detail.
  1424. -- Function: eventp object
  1425. This function returns non-`nil' if OBJECT is an input event or
  1426. event type.
  1427. Note that any symbol might be used as an event or an event type.
  1428. `eventp' cannot distinguish whether a symbol is intended by Lisp
  1429. code to be used as an event. Instead, it distinguishes whether the
  1430. symbol has actually been used in an event that has been read as
  1431. input in the current Emacs session. If a symbol has not yet been
  1432. so used, `eventp' returns `nil'.
  1433. * Menu:
  1434. * Keyboard Events:: Ordinary characters--keys with symbols on them.
  1435. * Function Keys:: Function keys--keys with names, not symbols.
  1436. * Mouse Events:: Overview of mouse events.
  1437. * Click Events:: Pushing and releasing a mouse button.
  1438. * Drag Events:: Moving the mouse before releasing the button.
  1439. * Button-Down Events:: A button was pushed and not yet released.
  1440. * Repeat Events:: Double and triple click (or drag, or down).
  1441. * Motion Events:: Just moving the mouse, not pushing a button.
  1442. * Focus Events:: Moving the mouse between frames.
  1443. * Misc Events:: Other events the system can generate.
  1444. * Event Examples:: Examples of the lists for mouse events.
  1445. * Classifying Events:: Finding the modifier keys in an event symbol.
  1446. Event types.
  1447. * Accessing Mouse:: Functions to extract info from mouse events.
  1448. * Accessing Scroll:: Functions to get info from scroll bar events.
  1449. * Strings of Events:: Special considerations for putting
  1450. keyboard character events in a string.
  1451. 
  1452. File: elisp, Node: Keyboard Events, Next: Function Keys, Up: Input Events
  1453. 21.7.1 Keyboard Events
  1454. ----------------------
  1455. There are two kinds of input you can get from the keyboard: ordinary
  1456. keys, and function keys. Ordinary keys correspond to characters; the
  1457. events they generate are represented in Lisp as characters. The event
  1458. type of a character event is the character itself (an integer); see
  1459. *note Classifying Events::.
  1460. An input character event consists of a "basic code" between 0 and
  1461. 524287, plus any or all of these "modifier bits":
  1462. meta
  1463. The 2**27 bit in the character code indicates a character typed
  1464. with the meta key held down.
  1465. control
  1466. The 2**26 bit in the character code indicates a non-ASCII control
  1467. character.
  1468. ASCII control characters such as `C-a' have special basic codes of
  1469. their own, so Emacs needs no special bit to indicate them. Thus,
  1470. the code for `C-a' is just 1.
  1471. But if you type a control combination not in ASCII, such as `%'
  1472. with the control key, the numeric value you get is the code for
  1473. `%' plus 2**26 (assuming the terminal supports non-ASCII control
  1474. characters).
  1475. shift
  1476. The 2**25 bit in the character code indicates an ASCII control
  1477. character typed with the shift key held down.
  1478. For letters, the basic code itself indicates upper versus lower
  1479. case; for digits and punctuation, the shift key selects an
  1480. entirely different character with a different basic code. In
  1481. order to keep within the ASCII character set whenever possible,
  1482. Emacs avoids using the 2**25 bit for those characters.
  1483. However, ASCII provides no way to distinguish `C-A' from `C-a', so
  1484. Emacs uses the 2**25 bit in `C-A' and not in `C-a'.
  1485. hyper
  1486. The 2**24 bit in the character code indicates a character typed
  1487. with the hyper key held down.
  1488. super
  1489. The 2**23 bit in the character code indicates a character typed
  1490. with the super key held down.
  1491. alt
  1492. The 2**22 bit in the character code indicates a character typed
  1493. with the alt key held down. (The key labeled <Alt> on most
  1494. keyboards is actually treated as the meta key, not this.)
  1495. It is best to avoid mentioning specific bit numbers in your program.
  1496. To test the modifier bits of a character, use the function
  1497. `event-modifiers' (*note Classifying Events::). When making key
  1498. bindings, you can use the read syntax for characters with modifier bits
  1499. (`\C-', `\M-', and so on). For making key bindings with `define-key',
  1500. you can use lists such as `(control hyper ?x)' to specify the
  1501. characters (*note Changing Key Bindings::). The function
  1502. `event-convert-list' converts such a list into an event type (*note
  1503. Classifying Events::).
  1504. 
  1505. File: elisp, Node: Function Keys, Next: Mouse Events, Prev: Keyboard Events, Up: Input Events
  1506. 21.7.2 Function Keys
  1507. --------------------
  1508. Most keyboards also have "function keys"--keys that have names or
  1509. symbols that are not characters. Function keys are represented in
  1510. Emacs Lisp as symbols; the symbol's name is the function key's label,
  1511. in lower case. For example, pressing a key labeled <F1> generates an
  1512. input event represented by the symbol `f1'.
  1513. The event type of a function key event is the event symbol itself.
  1514. *Note Classifying Events::.
  1515. Here are a few special cases in the symbol-naming convention for
  1516. function keys:
  1517. `backspace', `tab', `newline', `return', `delete'
  1518. These keys correspond to common ASCII control characters that have
  1519. special keys on most keyboards.
  1520. In ASCII, `C-i' and <TAB> are the same character. If the terminal
  1521. can distinguish between them, Emacs conveys the distinction to
  1522. Lisp programs by representing the former as the integer 9, and the
  1523. latter as the symbol `tab'.
  1524. Most of the time, it's not useful to distinguish the two. So
  1525. normally `local-function-key-map' (*note Translation Keymaps::) is
  1526. set up to map `tab' into 9. Thus, a key binding for character
  1527. code 9 (the character `C-i') also applies to `tab'. Likewise for
  1528. the other symbols in this group. The function `read-char'
  1529. likewise converts these events into characters.
  1530. In ASCII, <BS> is really `C-h'. But `backspace' converts into the
  1531. character code 127 (<DEL>), not into code 8 (<BS>). This is what
  1532. most users prefer.
  1533. `left', `up', `right', `down'
  1534. Cursor arrow keys
  1535. `kp-add', `kp-decimal', `kp-divide', ...
  1536. Keypad keys (to the right of the regular keyboard).
  1537. `kp-0', `kp-1', ...
  1538. Keypad keys with digits.
  1539. `kp-f1', `kp-f2', `kp-f3', `kp-f4'
  1540. Keypad PF keys.
  1541. `kp-home', `kp-left', `kp-up', `kp-right', `kp-down'
  1542. Keypad arrow keys. Emacs normally translates these into the
  1543. corresponding non-keypad keys `home', `left', ...
  1544. `kp-prior', `kp-next', `kp-end', `kp-begin', `kp-insert', `kp-delete'
  1545. Additional keypad duplicates of keys ordinarily found elsewhere.
  1546. Emacs normally translates these into the like-named non-keypad
  1547. keys.
  1548. You can use the modifier keys <ALT>, <CTRL>, <HYPER>, <META>,
  1549. <SHIFT>, and <SUPER> with function keys. The way to represent them is
  1550. with prefixes in the symbol name:
  1551. `A-'
  1552. The alt modifier.
  1553. `C-'
  1554. The control modifier.
  1555. `H-'
  1556. The hyper modifier.
  1557. `M-'
  1558. The meta modifier.
  1559. `S-'
  1560. The shift modifier.
  1561. `s-'
  1562. The super modifier.
  1563. Thus, the symbol for the key <F3> with <META> held down is `M-f3'.
  1564. When you use more than one prefix, we recommend you write them in
  1565. alphabetical order; but the order does not matter in arguments to the
  1566. key-binding lookup and modification functions.
  1567. 
  1568. File: elisp, Node: Mouse Events, Next: Click Events, Prev: Function Keys, Up: Input Events
  1569. 21.7.3 Mouse Events
  1570. -------------------
  1571. Emacs supports four kinds of mouse events: click events, drag events,
  1572. button-down events, and motion events. All mouse events are represented
  1573. as lists. The CAR of the list is the event type; this says which mouse
  1574. button was involved, and which modifier keys were used with it. The
  1575. event type can also distinguish double or triple button presses (*note
  1576. Repeat Events::). The rest of the list elements give position and time
  1577. information.
  1578. For key lookup, only the event type matters: two events of the same
  1579. type necessarily run the same command. The command can access the full
  1580. values of these events using the `e' interactive code. *Note
  1581. Interactive Codes::.
  1582. A key sequence that starts with a mouse event is read using the
  1583. keymaps of the buffer in the window that the mouse was in, not the
  1584. current buffer. This does not imply that clicking in a window selects
  1585. that window or its buffer--that is entirely under the control of the
  1586. command binding of the key sequence.
  1587. 
  1588. File: elisp, Node: Click Events, Next: Drag Events, Prev: Mouse Events, Up: Input Events
  1589. 21.7.4 Click Events
  1590. -------------------
  1591. When the user presses a mouse button and releases it at the same
  1592. location, that generates a "click" event. All mouse click event share
  1593. the same format:
  1594. (EVENT-TYPE POSITION CLICK-COUNT)
  1595. EVENT-TYPE
  1596. This is a symbol that indicates which mouse button was used. It is
  1597. one of the symbols `mouse-1', `mouse-2', ..., where the buttons
  1598. are numbered left to right.
  1599. You can also use prefixes `A-', `C-', `H-', `M-', `S-' and `s-'
  1600. for modifiers alt, control, hyper, meta, shift and super, just as
  1601. you would with function keys.
  1602. This symbol also serves as the event type of the event. Key
  1603. bindings describe events by their types; thus, if there is a key
  1604. binding for `mouse-1', that binding would apply to all events whose
  1605. EVENT-TYPE is `mouse-1'.
  1606. POSITION
  1607. This is the position where the mouse click occurred. The actual
  1608. format of POSITION depends on what part of a window was clicked on.
  1609. For mouse click events in the text area, mode line, header line,
  1610. or in the marginal areas, POSITION has this form:
  1611. (WINDOW POS-OR-AREA (X . Y) TIMESTAMP
  1612. OBJECT TEXT-POS (COL . ROW)
  1613. IMAGE (DX . DY) (WIDTH . HEIGHT))
  1614. The meanings of these list elements are documented below. *Note
  1615. Accessing Mouse::, for functions that let you easily access these
  1616. elements.
  1617. WINDOW
  1618. This is the window in which the click occurred.
  1619. POS-OR-AREA
  1620. This is the buffer position of the character clicked on in
  1621. the text area, or if clicked outside the text area, it is the
  1622. window area in which the click occurred. It is one of the
  1623. symbols `mode-line', `header-line', `vertical-line',
  1624. `left-margin', `right-margin', `left-fringe', or
  1625. `right-fringe'.
  1626. In one special case, POS-OR-AREA is a list containing a symbol
  1627. (one of the symbols listed above) instead of just the symbol.
  1628. This happens after the imaginary prefix keys for the event
  1629. are registered by Emacs. *Note Key Sequence Input::.
  1630. X, Y
  1631. These are the relative pixel coordinates of the click. For
  1632. clicks in the text area of a window, the coordinate origin
  1633. `(0 . 0)' is taken to be the top left corner of the text
  1634. area. *Note Window Sizes::. For clicks in a mode line or
  1635. header line, the coordinate origin is the top left corner of
  1636. the window itself. For fringes, margins, and the vertical
  1637. border, X does not have meaningful data. For fringes and
  1638. margins, Y is relative to the bottom edge of the header line.
  1639. In all cases, the X and Y coordinates increase rightward and
  1640. downward respectively.
  1641. TIMESTAMP
  1642. This is the time at which the event occurred, in milliseconds.
  1643. OBJECT
  1644. This is either `nil' if there is no string-type text property
  1645. at the click position, or a cons cell of the form (STRING .
  1646. STRING-POS) if there is one:
  1647. STRING
  1648. The string which was clicked on, including any
  1649. properties.
  1650. STRING-POS
  1651. The position in the string where the click occurred.
  1652. TEXT-POS
  1653. For clicks on a marginal area or on a fringe, this is the
  1654. buffer position of the first visible character in the
  1655. corresponding line in the window. For other events, it is
  1656. the current buffer position in the window.
  1657. COL, ROW
  1658. These are the actual column and row coordinate numbers of the
  1659. glyph under the X, Y position. If X lies beyond the last
  1660. column of actual text on its line, COL is reported by adding
  1661. fictional extra columns that have the default character
  1662. width. Row 0 is taken to be the header line if the window
  1663. has one, or the topmost row of the text area otherwise.
  1664. Column 0 is taken to be the leftmost column of the text area
  1665. for clicks on a window text area, or the leftmost mode line
  1666. or header line column for clicks there. For clicks on
  1667. fringes or vertical borders, these have no meaningful data.
  1668. For clicks on margins, COL is measured from the left edge of
  1669. the margin area and ROW is measured from the top of the
  1670. margin area.
  1671. IMAGE
  1672. This is the image object on which the click occurred. It is
  1673. either `nil' if there is no image at the position clicked on,
  1674. or it is an image object as returned by `find-image' if click
  1675. was in an image.
  1676. DX, DY
  1677. These are the pixel coordinates of the click, relative to the
  1678. top left corner of OBJECT, which is `(0 . 0)'. If OBJECT is
  1679. `nil', the coordinates are relative to the top left corner of
  1680. the character glyph clicked on.
  1681. WIDTH, HEIGHT
  1682. These are the pixel width and height of OBJECT or, if this is
  1683. `nil', those of the character glyph clicked on.
  1684. For mouse clicks on a scroll-bar, POSITION has this form:
  1685. (WINDOW AREA (PORTION . WHOLE) TIMESTAMP PART)
  1686. WINDOW
  1687. This is the window whose scroll-bar was clicked on.
  1688. AREA
  1689. This is the scroll bar where the click occurred. It is one
  1690. of the symbols `vertical-scroll-bar' or
  1691. `horizontal-scroll-bar'.
  1692. PORTION
  1693. This is the distance of the click from the top or left end of
  1694. the scroll bar.
  1695. WHOLE
  1696. This is the length of the entire scroll bar.
  1697. TIMESTAMP
  1698. This is the time at which the event occurred, in milliseconds.
  1699. PART
  1700. This is the part of the scroll-bar which was clicked on. It
  1701. is one of the symbols `above-handle', `handle',
  1702. `below-handle', `up', `down', `top', `bottom', and
  1703. `end-scroll'.
  1704. CLICK-COUNT
  1705. This is the number of rapid repeated presses so far of the same
  1706. mouse button. *Note Repeat Events::.
  1707. 
  1708. File: elisp, Node: Drag Events, Next: Button-Down Events, Prev: Click Events, Up: Input Events
  1709. 21.7.5 Drag Events
  1710. ------------------
  1711. With Emacs, you can have a drag event without even changing your
  1712. clothes. A "drag event" happens every time the user presses a mouse
  1713. button and then moves the mouse to a different character position before
  1714. releasing the button. Like all mouse events, drag events are
  1715. represented in Lisp as lists. The lists record both the starting mouse
  1716. position and the final position, like this:
  1717. (EVENT-TYPE
  1718. (WINDOW1 START-POSITION)
  1719. (WINDOW2 END-POSITION))
  1720. For a drag event, the name of the symbol EVENT-TYPE contains the
  1721. prefix `drag-'. For example, dragging the mouse with button 2 held
  1722. down generates a `drag-mouse-2' event. The second and third elements
  1723. of the event give the starting and ending position of the drag. They
  1724. have the same form as POSITION in a click event (*note Click Events::)
  1725. that is not on the scroll bar part of the window. You can access the
  1726. second element of any mouse event in the same way, with no need to
  1727. distinguish drag events from others.
  1728. The `drag-' prefix follows the modifier key prefixes such as `C-'
  1729. and `M-'.
  1730. If `read-key-sequence' receives a drag event that has no key
  1731. binding, and the corresponding click event does have a binding, it
  1732. changes the drag event into a click event at the drag's starting
  1733. position. This means that you don't have to distinguish between click
  1734. and drag events unless you want to.
  1735. 
  1736. File: elisp, Node: Button-Down Events, Next: Repeat Events, Prev: Drag Events, Up: Input Events
  1737. 21.7.6 Button-Down Events
  1738. -------------------------
  1739. Click and drag events happen when the user releases a mouse button.
  1740. They cannot happen earlier, because there is no way to distinguish a
  1741. click from a drag until the button is released.
  1742. If you want to take action as soon as a button is pressed, you need
  1743. to handle "button-down" events.(1) These occur as soon as a button is
  1744. pressed. They are represented by lists that look exactly like click
  1745. events (*note Click Events::), except that the EVENT-TYPE symbol name
  1746. contains the prefix `down-'. The `down-' prefix follows modifier key
  1747. prefixes such as `C-' and `M-'.
  1748. The function `read-key-sequence' ignores any button-down events that
  1749. don't have command bindings; therefore, the Emacs command loop ignores
  1750. them too. This means that you need not worry about defining
  1751. button-down events unless you want them to do something. The usual
  1752. reason to define a button-down event is so that you can track mouse
  1753. motion (by reading motion events) until the button is released. *Note
  1754. Motion Events::.
  1755. ---------- Footnotes ----------
  1756. (1) Button-down is the conservative antithesis of drag.
  1757. 
  1758. File: elisp, Node: Repeat Events, Next: Motion Events, Prev: Button-Down Events, Up: Input Events
  1759. 21.7.7 Repeat Events
  1760. --------------------
  1761. If you press the same mouse button more than once in quick succession
  1762. without moving the mouse, Emacs generates special "repeat" mouse events
  1763. for the second and subsequent presses.
  1764. The most common repeat events are "double-click" events. Emacs
  1765. generates a double-click event when you click a button twice; the event
  1766. happens when you release the button (as is normal for all click events).
  1767. The event type of a double-click event contains the prefix
  1768. `double-'. Thus, a double click on the second mouse button with <meta>
  1769. held down comes to the Lisp program as `M-double-mouse-2'. If a
  1770. double-click event has no binding, the binding of the corresponding
  1771. ordinary click event is used to execute it. Thus, you need not pay
  1772. attention to the double click feature unless you really want to.
  1773. When the user performs a double click, Emacs generates first an
  1774. ordinary click event, and then a double-click event. Therefore, you
  1775. must design the command binding of the double click event to assume
  1776. that the single-click command has already run. It must produce the
  1777. desired results of a double click, starting from the results of a
  1778. single click.
  1779. This is convenient, if the meaning of a double click somehow "builds
  1780. on" the meaning of a single click--which is recommended user interface
  1781. design practice for double clicks.
  1782. If you click a button, then press it down again and start moving the
  1783. mouse with the button held down, then you get a "double-drag" event
  1784. when you ultimately release the button. Its event type contains
  1785. `double-drag' instead of just `drag'. If a double-drag event has no
  1786. binding, Emacs looks for an alternate binding as if the event were an
  1787. ordinary drag.
  1788. Before the double-click or double-drag event, Emacs generates a
  1789. "double-down" event when the user presses the button down for the
  1790. second time. Its event type contains `double-down' instead of just
  1791. `down'. If a double-down event has no binding, Emacs looks for an
  1792. alternate binding as if the event were an ordinary button-down event.
  1793. If it finds no binding that way either, the double-down event is
  1794. ignored.
  1795. To summarize, when you click a button and then press it again right
  1796. away, Emacs generates a down event and a click event for the first
  1797. click, a double-down event when you press the button again, and finally
  1798. either a double-click or a double-drag event.
  1799. If you click a button twice and then press it again, all in quick
  1800. succession, Emacs generates a "triple-down" event, followed by either a
  1801. "triple-click" or a "triple-drag". The event types of these events
  1802. contain `triple' instead of `double'. If any triple event has no
  1803. binding, Emacs uses the binding that it would use for the corresponding
  1804. double event.
  1805. If you click a button three or more times and then press it again,
  1806. the events for the presses beyond the third are all triple events.
  1807. Emacs does not have separate event types for quadruple, quintuple, etc.
  1808. events. However, you can look at the event list to find out precisely
  1809. how many times the button was pressed.
  1810. -- Function: event-click-count event
  1811. This function returns the number of consecutive button presses
  1812. that led up to EVENT. If EVENT is a double-down, double-click or
  1813. double-drag event, the value is 2. If EVENT is a triple event,
  1814. the value is 3 or greater. If EVENT is an ordinary mouse event
  1815. (not a repeat event), the value is 1.
  1816. -- User Option: double-click-fuzz
  1817. To generate repeat events, successive mouse button presses must be
  1818. at approximately the same screen position. The value of
  1819. `double-click-fuzz' specifies the maximum number of pixels the
  1820. mouse may be moved (horizontally or vertically) between two
  1821. successive clicks to make a double-click.
  1822. This variable is also the threshold for motion of the mouse to
  1823. count as a drag.
  1824. -- User Option: double-click-time
  1825. To generate repeat events, the number of milliseconds between
  1826. successive button presses must be less than the value of
  1827. `double-click-time'. Setting `double-click-time' to `nil'
  1828. disables multi-click detection entirely. Setting it to `t'
  1829. removes the time limit; Emacs then detects multi-clicks by
  1830. position only.
  1831. 
  1832. File: elisp, Node: Motion Events, Next: Focus Events, Prev: Repeat Events, Up: Input Events
  1833. 21.7.8 Motion Events
  1834. --------------------
  1835. Emacs sometimes generates "mouse motion" events to describe motion of
  1836. the mouse without any button activity. Mouse motion events are
  1837. represented by lists that look like this:
  1838. (mouse-movement POSITION)
  1839. The second element of the list describes the current position of the
  1840. mouse, just as in a click event (*note Click Events::).
  1841. The special form `track-mouse' enables generation of motion events
  1842. within its body. Outside of `track-mouse' forms, Emacs does not
  1843. generate events for mere motion of the mouse, and these events do not
  1844. appear. *Note Mouse Tracking::.
  1845. 
  1846. File: elisp, Node: Focus Events, Next: Misc Events, Prev: Motion Events, Up: Input Events
  1847. 21.7.9 Focus Events
  1848. -------------------
  1849. Window systems provide general ways for the user to control which window
  1850. gets keyboard input. This choice of window is called the "focus".
  1851. When the user does something to switch between Emacs frames, that
  1852. generates a "focus event". The normal definition of a focus event, in
  1853. the global keymap, is to select a new frame within Emacs, as the user
  1854. would expect. *Note Input Focus::.
  1855. Focus events are represented in Lisp as lists that look like this:
  1856. (switch-frame NEW-FRAME)
  1857. where NEW-FRAME is the frame switched to.
  1858. Some X window managers are set up so that just moving the mouse into
  1859. a window is enough to set the focus there. Usually, there is no need
  1860. for a Lisp program to know about the focus change until some other kind
  1861. of input arrives. Emacs generates a focus event only when the user
  1862. actually types a keyboard key or presses a mouse button in the new
  1863. frame; just moving the mouse between frames does not generate a focus
  1864. event.
  1865. A focus event in the middle of a key sequence would garble the
  1866. sequence. So Emacs never generates a focus event in the middle of a key
  1867. sequence. If the user changes focus in the middle of a key
  1868. sequence--that is, after a prefix key--then Emacs reorders the events
  1869. so that the focus event comes either before or after the multi-event key
  1870. sequence, and not within it.
  1871. 
  1872. File: elisp, Node: Misc Events, Next: Event Examples, Prev: Focus Events, Up: Input Events
  1873. 21.7.10 Miscellaneous System Events
  1874. -----------------------------------
  1875. A few other event types represent occurrences within the system.
  1876. `(delete-frame (FRAME))'
  1877. This kind of event indicates that the user gave the window manager
  1878. a command to delete a particular window, which happens to be an
  1879. Emacs frame.
  1880. The standard definition of the `delete-frame' event is to delete
  1881. FRAME.
  1882. `(iconify-frame (FRAME))'
  1883. This kind of event indicates that the user iconified FRAME using
  1884. the window manager. Its standard definition is `ignore'; since the
  1885. frame has already been iconified, Emacs has no work to do. The
  1886. purpose of this event type is so that you can keep track of such
  1887. events if you want to.
  1888. `(make-frame-visible (FRAME))'
  1889. This kind of event indicates that the user deiconified FRAME using
  1890. the window manager. Its standard definition is `ignore'; since the
  1891. frame has already been made visible, Emacs has no work to do.
  1892. `(wheel-up POSITION)'
  1893. `(wheel-down POSITION)'
  1894. These kinds of event are generated by moving a mouse wheel. Their
  1895. usual meaning is a kind of scroll or zoom.
  1896. The element POSITION is a list describing the position of the
  1897. event, in the same format as used in a mouse-click event (*note
  1898. Click Events::).
  1899. This kind of event is generated only on some kinds of systems. On
  1900. some systems, `mouse-4' and `mouse-5' are used instead. For
  1901. portable code, use the variables `mouse-wheel-up-event' and
  1902. `mouse-wheel-down-event' defined in `mwheel.el' to determine what
  1903. event types to expect for the mouse wheel.
  1904. `(drag-n-drop POSITION FILES)'
  1905. This kind of event is generated when a group of files is selected
  1906. in an application outside of Emacs, and then dragged and dropped
  1907. onto an Emacs frame.
  1908. The element POSITION is a list describing the position of the
  1909. event, in the same format as used in a mouse-click event (*note
  1910. Click Events::), and FILES is the list of file names that were
  1911. dragged and dropped. The usual way to handle this event is by
  1912. visiting these files.
  1913. This kind of event is generated, at present, only on some kinds of
  1914. systems.
  1915. `help-echo'
  1916. This kind of event is generated when a mouse pointer moves onto a
  1917. portion of buffer text which has a `help-echo' text property. The
  1918. generated event has this form:
  1919. (help-echo FRAME HELP WINDOW OBJECT POS)
  1920. The precise meaning of the event parameters and the way these
  1921. parameters are used to display the help-echo text are described in
  1922. *note Text help-echo::.
  1923. `sigusr1'
  1924. `sigusr2'
  1925. These events are generated when the Emacs process receives the
  1926. signals `SIGUSR1' and `SIGUSR2'. They contain no additional data
  1927. because signals do not carry additional information. They can be
  1928. useful for debugging (*note Error Debugging::).
  1929. To catch a user signal, bind the corresponding event to an
  1930. interactive command in the `special-event-map' (*note Active
  1931. Keymaps::). The command is called with no arguments, and the
  1932. specific signal event is available in `last-input-event'. For
  1933. example:
  1934. (defun sigusr-handler ()
  1935. (interactive)
  1936. (message "Caught signal %S" last-input-event))
  1937. (define-key special-event-map [sigusr1] 'sigusr-handler)
  1938. To test the signal handler, you can make Emacs send a signal to
  1939. itself:
  1940. (signal-process (emacs-pid) 'sigusr1)
  1941. If one of these events arrives in the middle of a key sequence--that
  1942. is, after a prefix key--then Emacs reorders the events so that this
  1943. event comes either before or after the multi-event key sequence, not
  1944. within it.
  1945. 
  1946. File: elisp, Node: Event Examples, Next: Classifying Events, Prev: Misc Events, Up: Input Events
  1947. 21.7.11 Event Examples
  1948. ----------------------
  1949. If the user presses and releases the left mouse button over the same
  1950. location, that generates a sequence of events like this:
  1951. (down-mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864320))
  1952. (mouse-1 (#<window 18 on NEWS> 2613 (0 . 38) -864180))
  1953. While holding the control key down, the user might hold down the
  1954. second mouse button, and drag the mouse from one line to the next.
  1955. That produces two events, as shown here:
  1956. (C-down-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219))
  1957. (C-drag-mouse-2 (#<window 18 on NEWS> 3440 (0 . 27) -731219)
  1958. (#<window 18 on NEWS> 3510 (0 . 28) -729648))
  1959. While holding down the meta and shift keys, the user might press the
  1960. second mouse button on the window's mode line, and then drag the mouse
  1961. into another window. That produces a pair of events like these:
  1962. (M-S-down-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844))
  1963. (M-S-drag-mouse-2 (#<window 18 on NEWS> mode-line (33 . 31) -457844)
  1964. (#<window 20 on carlton-sanskrit.tex> 161 (33 . 3)
  1965. -453816))
  1966. To handle a SIGUSR1 signal, define an interactive function, and bind
  1967. it to the `signal usr1' event sequence:
  1968. (defun usr1-handler ()
  1969. (interactive)
  1970. (message "Got USR1 signal"))
  1971. (global-set-key [signal usr1] 'usr1-handler)
  1972. 
  1973. File: elisp, Node: Classifying Events, Next: Accessing Mouse, Prev: Event Examples, Up: Input Events
  1974. 21.7.12 Classifying Events
  1975. --------------------------
  1976. Every event has an "event type", which classifies the event for key
  1977. binding purposes. For a keyboard event, the event type equals the
  1978. event value; thus, the event type for a character is the character, and
  1979. the event type for a function key symbol is the symbol itself. For
  1980. events that are lists, the event type is the symbol in the CAR of the
  1981. list. Thus, the event type is always a symbol or a character.
  1982. Two events of the same type are equivalent where key bindings are
  1983. concerned; thus, they always run the same command. That does not
  1984. necessarily mean they do the same things, however, as some commands look
  1985. at the whole event to decide what to do. For example, some commands use
  1986. the location of a mouse event to decide where in the buffer to act.
  1987. Sometimes broader classifications of events are useful. For example,
  1988. you might want to ask whether an event involved the <META> key,
  1989. regardless of which other key or mouse button was used.
  1990. The functions `event-modifiers' and `event-basic-type' are provided
  1991. to get such information conveniently.
  1992. -- Function: event-modifiers event
  1993. This function returns a list of the modifiers that EVENT has. The
  1994. modifiers are symbols; they include `shift', `control', `meta',
  1995. `alt', `hyper' and `super'. In addition, the modifiers list of a
  1996. mouse event symbol always contains one of `click', `drag', and
  1997. `down'. For double or triple events, it also contains `double' or
  1998. `triple'.
  1999. The argument EVENT may be an entire event object, or just an event
  2000. type. If EVENT is a symbol that has never been used in an event
  2001. that has been read as input in the current Emacs session, then
  2002. `event-modifiers' can return `nil', even when EVENT actually has
  2003. modifiers.
  2004. Here are some examples:
  2005. (event-modifiers ?a)
  2006. => nil
  2007. (event-modifiers ?A)
  2008. => (shift)
  2009. (event-modifiers ?\C-a)
  2010. => (control)
  2011. (event-modifiers ?\C-%)
  2012. => (control)
  2013. (event-modifiers ?\C-\S-a)
  2014. => (control shift)
  2015. (event-modifiers 'f5)
  2016. => nil
  2017. (event-modifiers 's-f5)
  2018. => (super)
  2019. (event-modifiers 'M-S-f5)
  2020. => (meta shift)
  2021. (event-modifiers 'mouse-1)
  2022. => (click)
  2023. (event-modifiers 'down-mouse-1)
  2024. => (down)
  2025. The modifiers list for a click event explicitly contains `click',
  2026. but the event symbol name itself does not contain `click'.
  2027. -- Function: event-basic-type event
  2028. This function returns the key or mouse button that EVENT
  2029. describes, with all modifiers removed. The EVENT argument is as
  2030. in `event-modifiers'. For example:
  2031. (event-basic-type ?a)
  2032. => 97
  2033. (event-basic-type ?A)
  2034. => 97
  2035. (event-basic-type ?\C-a)
  2036. => 97
  2037. (event-basic-type ?\C-\S-a)
  2038. => 97
  2039. (event-basic-type 'f5)
  2040. => f5
  2041. (event-basic-type 's-f5)
  2042. => f5
  2043. (event-basic-type 'M-S-f5)
  2044. => f5
  2045. (event-basic-type 'down-mouse-1)
  2046. => mouse-1
  2047. -- Function: mouse-movement-p object
  2048. This function returns non-`nil' if OBJECT is a mouse movement
  2049. event.
  2050. -- Function: event-convert-list list
  2051. This function converts a list of modifier names and a basic event
  2052. type to an event type which specifies all of them. The basic
  2053. event type must be the last element of the list. For example,
  2054. (event-convert-list '(control ?a))
  2055. => 1
  2056. (event-convert-list '(control meta ?a))
  2057. => -134217727
  2058. (event-convert-list '(control super f1))
  2059. => C-s-f1
  2060. 
  2061. File: elisp, Node: Accessing Mouse, Next: Accessing Scroll, Prev: Classifying Events, Up: Input Events
  2062. 21.7.13 Accessing Mouse Events
  2063. ------------------------------
  2064. This section describes convenient functions for accessing the data in a
  2065. mouse button or motion event.
  2066. These two functions return the starting or ending position of a
  2067. mouse-button event, as a list of this form (*note Click Events::):
  2068. (WINDOW POS-OR-AREA (X . Y) TIMESTAMP
  2069. OBJECT TEXT-POS (COL . ROW)
  2070. IMAGE (DX . DY) (WIDTH . HEIGHT))
  2071. -- Function: event-start event
  2072. This returns the starting position of EVENT.
  2073. If EVENT is a click or button-down event, this returns the
  2074. location of the event. If EVENT is a drag event, this returns the
  2075. drag's starting position.
  2076. -- Function: event-end event
  2077. This returns the ending position of EVENT.
  2078. If EVENT is a drag event, this returns the position where the user
  2079. released the mouse button. If EVENT is a click or button-down
  2080. event, the value is actually the starting position, which is the
  2081. only position such events have.
  2082. These functions take a position list as described above, and return
  2083. various parts of it.
  2084. -- Function: posn-window position
  2085. Return the window that POSITION is in.
  2086. -- Function: posn-area position
  2087. Return the window area recorded in POSITION. It returns `nil'
  2088. when the event occurred in the text area of the window; otherwise,
  2089. it is a symbol identifying the area in which the event occurred.
  2090. -- Function: posn-point position
  2091. Return the buffer position in POSITION. When the event occurred
  2092. in the text area of the window, in a marginal area, or on a fringe,
  2093. this is an integer specifying a buffer position. Otherwise, the
  2094. value is undefined.
  2095. -- Function: posn-x-y position
  2096. Return the pixel-based x and y coordinates in POSITION, as a cons
  2097. cell `(X . Y)'. These coordinates are relative to the window
  2098. given by `posn-window'.
  2099. This example shows how to convert the window-relative coordinates
  2100. in the text area of a window into frame-relative coordinates:
  2101. (defun frame-relative-coordinates (position)
  2102. "Return frame-relative coordinates from POSITION.
  2103. POSITION is assumed to lie in a window text area."
  2104. (let* ((x-y (posn-x-y position))
  2105. (window (posn-window position))
  2106. (edges (window-inside-pixel-edges window)))
  2107. (cons (+ (car x-y) (car edges))
  2108. (+ (cdr x-y) (cadr edges)))))
  2109. -- Function: posn-col-row position
  2110. This function returns a cons cell `(COL . ROW)', containing the
  2111. estimated column and row corresponding to buffer position
  2112. POSITION. The return value is given in units of the frame's
  2113. default character width and height, as computed from the X and Y
  2114. values corresponding to POSITION. (So, if the actual characters
  2115. have non-default sizes, the actual row and column may differ from
  2116. these computed values.)
  2117. Note that ROW is counted from the top of the text area. If the
  2118. window possesses a header line (*note Header Lines::), it is _not_
  2119. counted as the first line.
  2120. -- Function: posn-actual-col-row position
  2121. Return the actual row and column in POSITION, as a cons cell `(COL
  2122. . ROW)'. The values are the actual row and column numbers in the
  2123. window. *Note Click Events::, for details. It returns `nil' if
  2124. POSITION does not include actual positions values.
  2125. -- Function: posn-string position
  2126. Return the string object in POSITION, either `nil', or a cons cell
  2127. `(STRING . STRING-POS)'.
  2128. -- Function: posn-image position
  2129. Return the image object in POSITION, either `nil', or an image
  2130. `(image ...)'.
  2131. -- Function: posn-object position
  2132. Return the image or string object in POSITION, either `nil', an
  2133. image `(image ...)', or a cons cell `(STRING . STRING-POS)'.
  2134. -- Function: posn-object-x-y position
  2135. Return the pixel-based x and y coordinates relative to the upper
  2136. left corner of the object in POSITION as a cons cell `(DX . DY)'.
  2137. If the POSITION is a buffer position, return the relative position
  2138. in the character at that position.
  2139. -- Function: posn-object-width-height position
  2140. Return the pixel width and height of the object in POSITION as a
  2141. cons cell `(WIDTH . HEIGHT)'. If the POSITION is a buffer
  2142. position, return the size of the character at that position.
  2143. -- Function: posn-timestamp position
  2144. Return the timestamp in POSITION. This is the time at which the
  2145. event occurred, in milliseconds.
  2146. These functions compute a position list given particular buffer
  2147. position or screen position. You can access the data in this position
  2148. list with the functions described above.
  2149. -- Function: posn-at-point &optional pos window
  2150. This function returns a position list for position POS in WINDOW.
  2151. POS defaults to point in WINDOW; WINDOW defaults to the selected
  2152. window.
  2153. `posn-at-point' returns `nil' if POS is not visible in WINDOW.
  2154. -- Function: posn-at-x-y x y &optional frame-or-window whole
  2155. This function returns position information corresponding to pixel
  2156. coordinates X and Y in a specified frame or window,
  2157. FRAME-OR-WINDOW, which defaults to the selected window. The
  2158. coordinates X and Y are relative to the frame or window used. If
  2159. WHOLE is `nil', the coordinates are relative to the window text
  2160. area, otherwise they are relative to the entire window area
  2161. including scroll bars, margins and fringes.
  2162. 
  2163. File: elisp, Node: Accessing Scroll, Next: Strings of Events, Prev: Accessing Mouse, Up: Input Events
  2164. 21.7.14 Accessing Scroll Bar Events
  2165. -----------------------------------
  2166. These functions are useful for decoding scroll bar events.
  2167. -- Function: scroll-bar-event-ratio event
  2168. This function returns the fractional vertical position of a scroll
  2169. bar event within the scroll bar. The value is a cons cell
  2170. `(PORTION . WHOLE)' containing two integers whose ratio is the
  2171. fractional position.
  2172. -- Function: scroll-bar-scale ratio total
  2173. This function multiplies (in effect) RATIO by TOTAL, rounding the
  2174. result to an integer. The argument RATIO is not a number, but
  2175. rather a pair `(NUM . DENOM)'--typically a value returned by
  2176. `scroll-bar-event-ratio'.
  2177. This function is handy for scaling a position on a scroll bar into
  2178. a buffer position. Here's how to do that:
  2179. (+ (point-min)
  2180. (scroll-bar-scale
  2181. (posn-x-y (event-start event))
  2182. (- (point-max) (point-min))))
  2183. Recall that scroll bar events have two integers forming a ratio,
  2184. in place of a pair of x and y coordinates.
  2185. 
  2186. File: elisp, Node: Strings of Events, Prev: Accessing Scroll, Up: Input Events
  2187. 21.7.15 Putting Keyboard Events in Strings
  2188. ------------------------------------------
  2189. In most of the places where strings are used, we conceptualize the
  2190. string as containing text characters--the same kind of characters found
  2191. in buffers or files. Occasionally Lisp programs use strings that
  2192. conceptually contain keyboard characters; for example, they may be key
  2193. sequences or keyboard macro definitions. However, storing keyboard
  2194. characters in a string is a complex matter, for reasons of historical
  2195. compatibility, and it is not always possible.
  2196. We recommend that new programs avoid dealing with these complexities
  2197. by not storing keyboard events in strings. Here is how to do that:
  2198. * Use vectors instead of strings for key sequences, when you plan to
  2199. use them for anything other than as arguments to `lookup-key' and
  2200. `define-key'. For example, you can use `read-key-sequence-vector'
  2201. instead of `read-key-sequence', and `this-command-keys-vector'
  2202. instead of `this-command-keys'.
  2203. * Use vectors to write key sequence constants containing meta
  2204. characters, even when passing them directly to `define-key'.
  2205. * When you have to look at the contents of a key sequence that might
  2206. be a string, use `listify-key-sequence' (*note Event Input Misc::)
  2207. first, to convert it to a list.
  2208. The complexities stem from the modifier bits that keyboard input
  2209. characters can include. Aside from the Meta modifier, none of these
  2210. modifier bits can be included in a string, and the Meta modifier is
  2211. allowed only in special cases.
  2212. The earliest GNU Emacs versions represented meta characters as codes
  2213. in the range of 128 to 255. At that time, the basic character codes
  2214. ranged from 0 to 127, so all keyboard character codes did fit in a
  2215. string. Many Lisp programs used `\M-' in string constants to stand for
  2216. meta characters, especially in arguments to `define-key' and similar
  2217. functions, and key sequences and sequences of events were always
  2218. represented as strings.
  2219. When we added support for larger basic character codes beyond 127,
  2220. and additional modifier bits, we had to change the representation of
  2221. meta characters. Now the flag that represents the Meta modifier in a
  2222. character is 2**27 and such numbers cannot be included in a string.
  2223. To support programs with `\M-' in string constants, there are
  2224. special rules for including certain meta characters in a string. Here
  2225. are the rules for interpreting a string as a sequence of input
  2226. characters:
  2227. * If the keyboard character value is in the range of 0 to 127, it
  2228. can go in the string unchanged.
  2229. * The meta variants of those characters, with codes in the range of
  2230. 2**27 to 2**27+127, can also go in the string, but you must change
  2231. their numeric values. You must set the 2**7 bit instead of the
  2232. 2**27 bit, resulting in a value between 128 and 255. Only a
  2233. unibyte string can include these codes.
  2234. * Non-ASCII characters above 256 can be included in a multibyte
  2235. string.
  2236. * Other keyboard character events cannot fit in a string. This
  2237. includes keyboard events in the range of 128 to 255.
  2238. Functions such as `read-key-sequence' that construct strings of
  2239. keyboard input characters follow these rules: they construct vectors
  2240. instead of strings, when the events won't fit in a string.
  2241. When you use the read syntax `\M-' in a string, it produces a code
  2242. in the range of 128 to 255--the same code that you get if you modify
  2243. the corresponding keyboard event to put it in the string. Thus, meta
  2244. events in strings work consistently regardless of how they get into the
  2245. strings.
  2246. However, most programs would do well to avoid these issues by
  2247. following the recommendations at the beginning of this section.
  2248. 
  2249. File: elisp, Node: Reading Input, Next: Special Events, Prev: Input Events, Up: Command Loop
  2250. 21.8 Reading Input
  2251. ==================
  2252. The editor command loop reads key sequences using the function
  2253. `read-key-sequence', which uses `read-event'. These and other
  2254. functions for event input are also available for use in Lisp programs.
  2255. See also `momentary-string-display' in *note Temporary Displays::, and
  2256. `sit-for' in *note Waiting::. *Note Terminal Input::, for functions
  2257. and variables for controlling terminal input modes and debugging
  2258. terminal input.
  2259. For higher-level input facilities, see *note Minibuffers::.
  2260. * Menu:
  2261. * Key Sequence Input:: How to read one key sequence.
  2262. * Reading One Event:: How to read just one event.
  2263. * Event Mod:: How Emacs modifies events as they are read.
  2264. * Invoking the Input Method:: How reading an event uses the input method.
  2265. * Quoted Character Input:: Asking the user to specify a character.
  2266. * Event Input Misc:: How to reread or throw away input events.
  2267. 
  2268. File: elisp, Node: Key Sequence Input, Next: Reading One Event, Up: Reading Input
  2269. 21.8.1 Key Sequence Input
  2270. -------------------------
  2271. The command loop reads input a key sequence at a time, by calling
  2272. `read-key-sequence'. Lisp programs can also call this function; for
  2273. example, `describe-key' uses it to read the key to describe.
  2274. -- Function: read-key-sequence prompt &optional continue-echo
  2275. dont-downcase-last switch-frame-ok command-loop
  2276. This function reads a key sequence and returns it as a string or
  2277. vector. It keeps reading events until it has accumulated a
  2278. complete key sequence; that is, enough to specify a non-prefix
  2279. command using the currently active keymaps. (Remember that a key
  2280. sequence that starts with a mouse event is read using the keymaps
  2281. of the buffer in the window that the mouse was in, not the current
  2282. buffer.)
  2283. If the events are all characters and all can fit in a string, then
  2284. `read-key-sequence' returns a string (*note Strings of Events::).
  2285. Otherwise, it returns a vector, since a vector can hold all kinds
  2286. of events--characters, symbols, and lists. The elements of the
  2287. string or vector are the events in the key sequence.
  2288. Reading a key sequence includes translating the events in various
  2289. ways. *Note Translation Keymaps::.
  2290. The argument PROMPT is either a string to be displayed in the echo
  2291. area as a prompt, or `nil', meaning not to display a prompt. The
  2292. argument CONTINUE-ECHO, if non-`nil', means to echo this key as a
  2293. continuation of the previous key.
  2294. Normally any upper case event is converted to lower case if the
  2295. original event is undefined and the lower case equivalent is
  2296. defined. The argument DONT-DOWNCASE-LAST, if non-`nil', means do
  2297. not convert the last event to lower case. This is appropriate for
  2298. reading a key sequence to be defined.
  2299. The argument SWITCH-FRAME-OK, if non-`nil', means that this
  2300. function should process a `switch-frame' event if the user
  2301. switches frames before typing anything. If the user switches
  2302. frames in the middle of a key sequence, or at the start of the
  2303. sequence but SWITCH-FRAME-OK is `nil', then the event will be put
  2304. off until after the current key sequence.
  2305. The argument COMMAND-LOOP, if non-`nil', means that this key
  2306. sequence is being read by something that will read commands one
  2307. after another. It should be `nil' if the caller will read just
  2308. one key sequence.
  2309. In the following example, Emacs displays the prompt `?' in the
  2310. echo area, and then the user types `C-x C-f'.
  2311. (read-key-sequence "?")
  2312. ---------- Echo Area ----------
  2313. ?C-x C-f
  2314. ---------- Echo Area ----------
  2315. => "^X^F"
  2316. The function `read-key-sequence' suppresses quitting: `C-g' typed
  2317. while reading with this function works like any other character,
  2318. and does not set `quit-flag'. *Note Quitting::.
  2319. -- Function: read-key-sequence-vector prompt &optional continue-echo
  2320. dont-downcase-last switch-frame-ok command-loop
  2321. This is like `read-key-sequence' except that it always returns the
  2322. key sequence as a vector, never as a string. *Note Strings of
  2323. Events::.
  2324. If an input character is upper-case (or has the shift modifier) and
  2325. has no key binding, but its lower-case equivalent has one, then
  2326. `read-key-sequence' converts the character to lower case. Note that
  2327. `lookup-key' does not perform case conversion in this way.
  2328. When reading input results in such a "shift-translation", Emacs sets
  2329. the variable `this-command-keys-shift-translated' to a non-`nil' value.
  2330. Lisp programs can examine this variable if they need to modify their
  2331. behavior when invoked by shift-translated keys. For example, the
  2332. function `handle-shift-selection' examines the value of this variable
  2333. to determine how to activate or deactivate the region (*note
  2334. handle-shift-selection: The Mark.).
  2335. The function `read-key-sequence' also transforms some mouse events.
  2336. It converts unbound drag events into click events, and discards unbound
  2337. button-down events entirely. It also reshuffles focus events and
  2338. miscellaneous window events so that they never appear in a key sequence
  2339. with any other events.
  2340. When mouse events occur in special parts of a window, such as a mode
  2341. line or a scroll bar, the event type shows nothing special--it is the
  2342. same symbol that would normally represent that combination of mouse
  2343. button and modifier keys. The information about the window part is kept
  2344. elsewhere in the event--in the coordinates. But `read-key-sequence'
  2345. translates this information into imaginary "prefix keys", all of which
  2346. are symbols: `header-line', `horizontal-scroll-bar', `menu-bar',
  2347. `mode-line', `vertical-line', and `vertical-scroll-bar'. You can define
  2348. meanings for mouse clicks in special window parts by defining key
  2349. sequences using these imaginary prefix keys.
  2350. For example, if you call `read-key-sequence' and then click the
  2351. mouse on the window's mode line, you get two events, like this:
  2352. (read-key-sequence "Click on the mode line: ")
  2353. => [mode-line
  2354. (mouse-1
  2355. (#<window 6 on NEWS> mode-line
  2356. (40 . 63) 5959987))]
  2357. -- Variable: num-input-keys
  2358. This variable's value is the number of key sequences processed so
  2359. far in this Emacs session. This includes key sequences read from
  2360. the terminal and key sequences read from keyboard macros being
  2361. executed.
  2362. 
  2363. File: elisp, Node: Reading One Event, Next: Event Mod, Prev: Key Sequence Input, Up: Reading Input
  2364. 21.8.2 Reading One Event
  2365. ------------------------
  2366. The lowest level functions for command input are `read-event',
  2367. `read-char', and `read-char-exclusive'.
  2368. -- Function: read-event &optional prompt inherit-input-method seconds
  2369. This function reads and returns the next event of command input,
  2370. waiting if necessary until an event is available. Events can come
  2371. directly from the user or from a keyboard macro.
  2372. If the optional argument PROMPT is non-`nil', it should be a
  2373. string to display in the echo area as a prompt. Otherwise,
  2374. `read-event' does not display any message to indicate it is waiting
  2375. for input; instead, it prompts by echoing: it displays
  2376. descriptions of the events that led to or were read by the current
  2377. command. *Note The Echo Area::.
  2378. If INHERIT-INPUT-METHOD is non-`nil', then the current input
  2379. method (if any) is employed to make it possible to enter a
  2380. non-ASCII character. Otherwise, input method handling is disabled
  2381. for reading this event.
  2382. If `cursor-in-echo-area' is non-`nil', then `read-event' moves the
  2383. cursor temporarily to the echo area, to the end of any message
  2384. displayed there. Otherwise `read-event' does not move the cursor.
  2385. If SECONDS is non-`nil', it should be a number specifying the
  2386. maximum time to wait for input, in seconds. If no input arrives
  2387. within that time, `read-event' stops waiting and returns `nil'. A
  2388. floating-point value for SECONDS means to wait for a fractional
  2389. number of seconds. Some systems support only a whole number of
  2390. seconds; on these systems, SECONDS is rounded down. If SECONDS is
  2391. `nil', `read-event' waits as long as necessary for input to arrive.
  2392. If SECONDS is `nil', Emacs is considered idle while waiting for
  2393. user input to arrive. Idle timers--those created with
  2394. `run-with-idle-timer' (*note Idle Timers::)--can run during this
  2395. period. However, if SECONDS is non-`nil', the state of idleness
  2396. remains unchanged. If Emacs is non-idle when `read-event' is
  2397. called, it remains non-idle throughout the operation of
  2398. `read-event'; if Emacs is idle (which can happen if the call
  2399. happens inside an idle timer), it remains idle.
  2400. If `read-event' gets an event that is defined as a help character,
  2401. then in some cases `read-event' processes the event directly
  2402. without returning. *Note Help Functions::. Certain other events,
  2403. called "special events", are also processed directly within
  2404. `read-event' (*note Special Events::).
  2405. Here is what happens if you call `read-event' and then press the
  2406. right-arrow function key:
  2407. (read-event)
  2408. => right
  2409. -- Function: read-char &optional prompt inherit-input-method seconds
  2410. This function reads and returns a character of command input. If
  2411. the user generates an event which is not a character (i.e. a mouse
  2412. click or function key event), `read-char' signals an error. The
  2413. arguments work as in `read-event'.
  2414. In the first example, the user types the character `1' (ASCII code
  2415. 49). The second example shows a keyboard macro definition that
  2416. calls `read-char' from the minibuffer using `eval-expression'.
  2417. `read-char' reads the keyboard macro's very next character, which
  2418. is `1'. Then `eval-expression' displays its return value in the
  2419. echo area.
  2420. (read-char)
  2421. => 49
  2422. ;; We assume here you use `M-:' to evaluate this.
  2423. (symbol-function 'foo)
  2424. => "^[:(read-char)^M1"
  2425. (execute-kbd-macro 'foo)
  2426. -| 49
  2427. => nil
  2428. -- Function: read-char-exclusive &optional prompt inherit-input-method
  2429. seconds
  2430. This function reads and returns a character of command input. If
  2431. the user generates an event which is not a character,
  2432. `read-char-exclusive' ignores it and reads another event, until it
  2433. gets a character. The arguments work as in `read-event'.
  2434. None of the above functions suppress quitting.
  2435. -- Variable: num-nonmacro-input-events
  2436. This variable holds the total number of input events received so
  2437. far from the terminal--not counting those generated by keyboard
  2438. macros.
  2439. We emphasize that, unlike `read-key-sequence', the functions
  2440. `read-event', `read-char', and `read-char-exclusive' do not perform the
  2441. translations described in *note Translation Keymaps::. If you wish to
  2442. read a single key taking these translations into account, use the
  2443. function `read-key':
  2444. -- Function: read-key &optional prompt
  2445. This function reads a single key. It is "intermediate" between
  2446. `read-key-sequence' and `read-event'. Unlike the former, it reads
  2447. a single key, not a key sequence. Unlike the latter, it does not
  2448. return a raw event, but decodes and translates the user input
  2449. according to `input-decode-map', `local-function-key-map', and
  2450. `key-translation-map' (*note Translation Keymaps::).
  2451. The argument PROMPT is either a string to be displayed in the echo
  2452. area as a prompt, or `nil', meaning not to display a prompt.
  2453. -- Function: read-char-choice prompt chars &optional inhibit-quit
  2454. This function uses `read-key' to read and return a single
  2455. character. It ignores any input that is not a member of CHARS, a
  2456. list of accepted characters. Optionally, it will also ignore
  2457. keyboard-quit events while it is waiting for valid input. If you
  2458. bind `help-form' (*note Help Functions::) to a non-`nil' value
  2459. while calling `read-char-choice', then pressing `help-char' causes
  2460. it to evaluate `help-form' and display the result. It then
  2461. continues to wait for a valid input character, or keyboard-quit.
  2462. 
  2463. File: elisp, Node: Event Mod, Next: Invoking the Input Method, Prev: Reading One Event, Up: Reading Input
  2464. 21.8.3 Modifying and Translating Input Events
  2465. ---------------------------------------------
  2466. Emacs modifies every event it reads according to
  2467. `extra-keyboard-modifiers', then translates it through
  2468. `keyboard-translate-table' (if applicable), before returning it from
  2469. `read-event'.
  2470. -- Variable: extra-keyboard-modifiers
  2471. This variable lets Lisp programs "press" the modifier keys on the
  2472. keyboard. The value is a character. Only the modifiers of the
  2473. character matter. Each time the user types a keyboard key, it is
  2474. altered as if those modifier keys were held down. For instance, if
  2475. you bind `extra-keyboard-modifiers' to `?\C-\M-a', then all
  2476. keyboard input characters typed during the scope of the binding
  2477. will have the control and meta modifiers applied to them. The
  2478. character `?\C-@', equivalent to the integer 0, does not count as
  2479. a control character for this purpose, but as a character with no
  2480. modifiers. Thus, setting `extra-keyboard-modifiers' to zero
  2481. cancels any modification.
  2482. When using a window system, the program can "press" any of the
  2483. modifier keys in this way. Otherwise, only the <CTL> and <META>
  2484. keys can be virtually pressed.
  2485. Note that this variable applies only to events that really come
  2486. from the keyboard, and has no effect on mouse events or any other
  2487. events.
  2488. -- Variable: keyboard-translate-table
  2489. This terminal-local variable is the translate table for keyboard
  2490. characters. It lets you reshuffle the keys on the keyboard without
  2491. changing any command bindings. Its value is normally a
  2492. char-table, or else `nil'. (It can also be a string or vector,
  2493. but this is considered obsolete.)
  2494. If `keyboard-translate-table' is a char-table (*note
  2495. Char-Tables::), then each character read from the keyboard is
  2496. looked up in this char-table. If the value found there is
  2497. non-`nil', then it is used instead of the actual input character.
  2498. Note that this translation is the first thing that happens to a
  2499. character after it is read from the terminal. Record-keeping
  2500. features such as `recent-keys' and dribble files record the
  2501. characters after translation.
  2502. Note also that this translation is done before the characters are
  2503. supplied to input methods (*note Input Methods::). Use
  2504. `translation-table-for-input' (*note Translation of Characters::),
  2505. if you want to translate characters after input methods operate.
  2506. -- Function: keyboard-translate from to
  2507. This function modifies `keyboard-translate-table' to translate
  2508. character code FROM into character code TO. It creates the
  2509. keyboard translate table if necessary.
  2510. Here's an example of using the `keyboard-translate-table' to make
  2511. `C-x', `C-c' and `C-v' perform the cut, copy and paste operations:
  2512. (keyboard-translate ?\C-x 'control-x)
  2513. (keyboard-translate ?\C-c 'control-c)
  2514. (keyboard-translate ?\C-v 'control-v)
  2515. (global-set-key [control-x] 'kill-region)
  2516. (global-set-key [control-c] 'kill-ring-save)
  2517. (global-set-key [control-v] 'yank)
  2518. On a graphical terminal that supports extended ASCII input, you can
  2519. still get the standard Emacs meanings of one of those characters by
  2520. typing it with the shift key. That makes it a different character as
  2521. far as keyboard translation is concerned, but it has the same usual
  2522. meaning.
  2523. *Note Translation Keymaps::, for mechanisms that translate event
  2524. sequences at the level of `read-key-sequence'.
  2525. 
  2526. File: elisp, Node: Invoking the Input Method, Next: Quoted Character Input, Prev: Event Mod, Up: Reading Input
  2527. 21.8.4 Invoking the Input Method
  2528. --------------------------------
  2529. The event-reading functions invoke the current input method, if any
  2530. (*note Input Methods::). If the value of `input-method-function' is
  2531. non-`nil', it should be a function; when `read-event' reads a printing
  2532. character (including <SPC>) with no modifier bits, it calls that
  2533. function, passing the character as an argument.
  2534. -- Variable: input-method-function
  2535. If this is non-`nil', its value specifies the current input method
  2536. function.
  2537. *Warning:* don't bind this variable with `let'. It is often
  2538. buffer-local, and if you bind it around reading input (which is
  2539. exactly when you _would_ bind it), switching buffers
  2540. asynchronously while Emacs is waiting will cause the value to be
  2541. restored in the wrong buffer.
  2542. The input method function should return a list of events which should
  2543. be used as input. (If the list is `nil', that means there is no input,
  2544. so `read-event' waits for another event.) These events are processed
  2545. before the events in `unread-command-events' (*note Event Input
  2546. Misc::). Events returned by the input method function are not passed
  2547. to the input method function again, even if they are printing
  2548. characters with no modifier bits.
  2549. If the input method function calls `read-event' or
  2550. `read-key-sequence', it should bind `input-method-function' to `nil'
  2551. first, to prevent recursion.
  2552. The input method function is not called when reading the second and
  2553. subsequent events of a key sequence. Thus, these characters are not
  2554. subject to input method processing. The input method function should
  2555. test the values of `overriding-local-map' and
  2556. `overriding-terminal-local-map'; if either of these variables is
  2557. non-`nil', the input method should put its argument into a list and
  2558. return that list with no further processing.
  2559. 
  2560. File: elisp, Node: Quoted Character Input, Next: Event Input Misc, Prev: Invoking the Input Method, Up: Reading Input
  2561. 21.8.5 Quoted Character Input
  2562. -----------------------------
  2563. You can use the function `read-quoted-char' to ask the user to specify
  2564. a character, and allow the user to specify a control or meta character
  2565. conveniently, either literally or as an octal character code. The
  2566. command `quoted-insert' uses this function.
  2567. -- Function: read-quoted-char &optional prompt
  2568. This function is like `read-char', except that if the first
  2569. character read is an octal digit (0-7), it reads any number of
  2570. octal digits (but stopping if a non-octal digit is found), and
  2571. returns the character represented by that numeric character code.
  2572. If the character that terminates the sequence of octal digits is
  2573. <RET>, it is discarded. Any other terminating character is used
  2574. as input after this function returns.
  2575. Quitting is suppressed when the first character is read, so that
  2576. the user can enter a `C-g'. *Note Quitting::.
  2577. If PROMPT is supplied, it specifies a string for prompting the
  2578. user. The prompt string is always displayed in the echo area,
  2579. followed by a single `-'.
  2580. In the following example, the user types in the octal number 177
  2581. (which is 127 in decimal).
  2582. (read-quoted-char "What character")
  2583. ---------- Echo Area ----------
  2584. What character 1 7 7-
  2585. ---------- Echo Area ----------
  2586. => 127
  2587. 
  2588. File: elisp, Node: Event Input Misc, Prev: Quoted Character Input, Up: Reading Input
  2589. 21.8.6 Miscellaneous Event Input Features
  2590. -----------------------------------------
  2591. This section describes how to "peek ahead" at events without using them
  2592. up, how to check for pending input, and how to discard pending input.
  2593. See also the function `read-passwd' (*note Reading a Password::).
  2594. -- Variable: unread-command-events
  2595. This variable holds a list of events waiting to be read as command
  2596. input. The events are used in the order they appear in the list,
  2597. and removed one by one as they are used.
  2598. The variable is needed because in some cases a function reads an
  2599. event and then decides not to use it. Storing the event in this
  2600. variable causes it to be processed normally, by the command loop
  2601. or by the functions to read command input.
  2602. For example, the function that implements numeric prefix arguments
  2603. reads any number of digits. When it finds a non-digit event, it
  2604. must unread the event so that it can be read normally by the
  2605. command loop. Likewise, incremental search uses this feature to
  2606. unread events with no special meaning in a search, because these
  2607. events should exit the search and then execute normally.
  2608. The reliable and easy way to extract events from a key sequence so
  2609. as to put them in `unread-command-events' is to use
  2610. `listify-key-sequence' (see below).
  2611. Normally you add events to the front of this list, so that the
  2612. events most recently unread will be reread first.
  2613. Events read from this list are not normally added to the current
  2614. command's key sequence (as returned by e.g. `this-command-keys'),
  2615. as the events will already have been added once as they were read
  2616. for the first time. An element of the form `(`t' . EVENT)' forces
  2617. EVENT to be added to the current command's key sequence.
  2618. -- Function: listify-key-sequence key
  2619. This function converts the string or vector KEY to a list of
  2620. individual events, which you can put in `unread-command-events'.
  2621. -- Function: input-pending-p
  2622. This function determines whether any command input is currently
  2623. available to be read. It returns immediately, with value `t' if
  2624. there is available input, `nil' otherwise. On rare occasions it
  2625. may return `t' when no input is available.
  2626. -- Variable: last-input-event
  2627. -- Variable: last-input-char
  2628. This variable records the last terminal input event read, whether
  2629. as part of a command or explicitly by a Lisp program.
  2630. In the example below, the Lisp program reads the character `1',
  2631. ASCII code 49. It becomes the value of `last-input-event', while
  2632. `C-e' (we assume `C-x C-e' command is used to evaluate this
  2633. expression) remains the value of `last-command-event'.
  2634. (progn (print (read-char))
  2635. (print last-command-event)
  2636. last-input-event)
  2637. -| 49
  2638. -| 5
  2639. => 49
  2640. The alias `last-input-char' is obsolete.
  2641. -- Macro: while-no-input body...
  2642. This construct runs the BODY forms and returns the value of the
  2643. last one--but only if no input arrives. If any input arrives
  2644. during the execution of the BODY forms, it aborts them (working
  2645. much like a quit). The `while-no-input' form returns `nil' if
  2646. aborted by a real quit, and returns `t' if aborted by arrival of
  2647. other input.
  2648. If a part of BODY binds `inhibit-quit' to non-`nil', arrival of
  2649. input during those parts won't cause an abort until the end of
  2650. that part.
  2651. If you want to be able to distinguish all possible values computed
  2652. by BODY from both kinds of abort conditions, write the code like
  2653. this:
  2654. (while-no-input
  2655. (list
  2656. (progn . BODY)))
  2657. -- Function: discard-input
  2658. This function discards the contents of the terminal input buffer
  2659. and cancels any keyboard macro that might be in the process of
  2660. definition. It returns `nil'.
  2661. In the following example, the user may type a number of characters
  2662. right after starting the evaluation of the form. After the
  2663. `sleep-for' finishes sleeping, `discard-input' discards any
  2664. characters typed during the sleep.
  2665. (progn (sleep-for 2)
  2666. (discard-input))
  2667. => nil
  2668. 
  2669. File: elisp, Node: Special Events, Next: Waiting, Prev: Reading Input, Up: Command Loop
  2670. 21.9 Special Events
  2671. ===================
  2672. Certain "special events" are handled at a very low level--as soon as
  2673. they are read. The `read-event' function processes these events
  2674. itself, and never returns them. Instead, it keeps waiting for the
  2675. first event that is not special and returns that one.
  2676. Special events do not echo, they are never grouped into key
  2677. sequences, and they never appear in the value of `last-command-event'
  2678. or `(this-command-keys)'. They do not discard a numeric argument, they
  2679. cannot be unread with `unread-command-events', they may not appear in a
  2680. keyboard macro, and they are not recorded in a keyboard macro while you
  2681. are defining one.
  2682. Special events do, however, appear in `last-input-event' immediately
  2683. after they are read, and this is the way for the event's definition to
  2684. find the actual event.
  2685. The events types `iconify-frame', `make-frame-visible',
  2686. `delete-frame', `drag-n-drop', and user signals like `sigusr1' are
  2687. normally handled in this way. The keymap which defines how to handle
  2688. special events--and which events are special--is in the variable
  2689. `special-event-map' (*note Active Keymaps::).
  2690. 
  2691. File: elisp, Node: Waiting, Next: Quitting, Prev: Special Events, Up: Command Loop
  2692. 21.10 Waiting for Elapsed Time or Input
  2693. =======================================
  2694. The wait functions are designed to wait for a certain amount of time to
  2695. pass or until there is input. For example, you may wish to pause in
  2696. the middle of a computation to allow the user time to view the display.
  2697. `sit-for' pauses and updates the screen, and returns immediately if
  2698. input comes in, while `sleep-for' pauses without updating the screen.
  2699. -- Function: sit-for seconds &optional nodisp
  2700. This function performs redisplay (provided there is no pending
  2701. input from the user), then waits SECONDS seconds, or until input is
  2702. available. The usual purpose of `sit-for' is to give the user
  2703. time to read text that you display. The value is `t' if `sit-for'
  2704. waited the full time with no input arriving (*note Event Input
  2705. Misc::). Otherwise, the value is `nil'.
  2706. The argument SECONDS need not be an integer. If it is a floating
  2707. point number, `sit-for' waits for a fractional number of seconds.
  2708. Some systems support only a whole number of seconds; on these
  2709. systems, SECONDS is rounded down.
  2710. The expression `(sit-for 0)' is equivalent to `(redisplay)', i.e.
  2711. it requests a redisplay, without any delay, if there is no pending
  2712. input. *Note Forcing Redisplay::.
  2713. If NODISP is non-`nil', then `sit-for' does not redisplay, but it
  2714. still returns as soon as input is available (or when the timeout
  2715. elapses).
  2716. In batch mode (*note Batch Mode::), `sit-for' cannot be
  2717. interrupted, even by input from the standard input descriptor. It
  2718. is thus equivalent to `sleep-for', which is described below.
  2719. It is also possible to call `sit-for' with three arguments, as
  2720. `(sit-for SECONDS MILLISEC NODISP)', but that is considered
  2721. obsolete.
  2722. -- Function: sleep-for seconds &optional millisec
  2723. This function simply pauses for SECONDS seconds without updating
  2724. the display. It pays no attention to available input. It returns
  2725. `nil'.
  2726. The argument SECONDS need not be an integer. If it is a floating
  2727. point number, `sleep-for' waits for a fractional number of seconds.
  2728. Some systems support only a whole number of seconds; on these
  2729. systems, SECONDS is rounded down.
  2730. The optional argument MILLISEC specifies an additional waiting
  2731. period measured in milliseconds. This adds to the period
  2732. specified by SECONDS. If the system doesn't support waiting
  2733. fractions of a second, you get an error if you specify nonzero
  2734. MILLISEC.
  2735. Use `sleep-for' when you wish to guarantee a delay.
  2736. *Note Time of Day::, for functions to get the current time.
  2737. 
  2738. File: elisp, Node: Quitting, Next: Prefix Command Arguments, Prev: Waiting, Up: Command Loop
  2739. 21.11 Quitting
  2740. ==============
  2741. Typing `C-g' while a Lisp function is running causes Emacs to "quit"
  2742. whatever it is doing. This means that control returns to the innermost
  2743. active command loop.
  2744. Typing `C-g' while the command loop is waiting for keyboard input
  2745. does not cause a quit; it acts as an ordinary input character. In the
  2746. simplest case, you cannot tell the difference, because `C-g' normally
  2747. runs the command `keyboard-quit', whose effect is to quit. However,
  2748. when `C-g' follows a prefix key, they combine to form an undefined key.
  2749. The effect is to cancel the prefix key as well as any prefix argument.
  2750. In the minibuffer, `C-g' has a different definition: it aborts out
  2751. of the minibuffer. This means, in effect, that it exits the minibuffer
  2752. and then quits. (Simply quitting would return to the command loop
  2753. _within_ the minibuffer.) The reason why `C-g' does not quit directly
  2754. when the command reader is reading input is so that its meaning can be
  2755. redefined in the minibuffer in this way. `C-g' following a prefix key
  2756. is not redefined in the minibuffer, and it has its normal effect of
  2757. canceling the prefix key and prefix argument. This too would not be
  2758. possible if `C-g' always quit directly.
  2759. When `C-g' does directly quit, it does so by setting the variable
  2760. `quit-flag' to `t'. Emacs checks this variable at appropriate times
  2761. and quits if it is not `nil'. Setting `quit-flag' non-`nil' in any way
  2762. thus causes a quit.
  2763. At the level of C code, quitting cannot happen just anywhere; only
  2764. at the special places that check `quit-flag'. The reason for this is
  2765. that quitting at other places might leave an inconsistency in Emacs's
  2766. internal state. Because quitting is delayed until a safe place,
  2767. quitting cannot make Emacs crash.
  2768. Certain functions such as `read-key-sequence' or `read-quoted-char'
  2769. prevent quitting entirely even though they wait for input. Instead of
  2770. quitting, `C-g' serves as the requested input. In the case of
  2771. `read-key-sequence', this serves to bring about the special behavior of
  2772. `C-g' in the command loop. In the case of `read-quoted-char', this is
  2773. so that `C-q' can be used to quote a `C-g'.
  2774. You can prevent quitting for a portion of a Lisp function by binding
  2775. the variable `inhibit-quit' to a non-`nil' value. Then, although `C-g'
  2776. still sets `quit-flag' to `t' as usual, the usual result of this--a
  2777. quit--is prevented. Eventually, `inhibit-quit' will become `nil'
  2778. again, such as when its binding is unwound at the end of a `let' form.
  2779. At that time, if `quit-flag' is still non-`nil', the requested quit
  2780. happens immediately. This behavior is ideal when you wish to make sure
  2781. that quitting does not happen within a "critical section" of the
  2782. program.
  2783. In some functions (such as `read-quoted-char'), `C-g' is handled in
  2784. a special way that does not involve quitting. This is done by reading
  2785. the input with `inhibit-quit' bound to `t', and setting `quit-flag' to
  2786. `nil' before `inhibit-quit' becomes `nil' again. This excerpt from the
  2787. definition of `read-quoted-char' shows how this is done; it also shows
  2788. that normal quitting is permitted after the first character of input.
  2789. (defun read-quoted-char (&optional prompt)
  2790. "...DOCUMENTATION..."
  2791. (let ((message-log-max nil) done (first t) (code 0) char)
  2792. (while (not done)
  2793. (let ((inhibit-quit first)
  2794. ...)
  2795. (and prompt (message "%s-" prompt))
  2796. (setq char (read-event))
  2797. (if inhibit-quit (setq quit-flag nil)))
  2798. ...set the variable `code'...)
  2799. code))
  2800. -- Variable: quit-flag
  2801. If this variable is non-`nil', then Emacs quits immediately, unless
  2802. `inhibit-quit' is non-`nil'. Typing `C-g' ordinarily sets
  2803. `quit-flag' non-`nil', regardless of `inhibit-quit'.
  2804. -- Variable: inhibit-quit
  2805. This variable determines whether Emacs should quit when `quit-flag'
  2806. is set to a value other than `nil'. If `inhibit-quit' is
  2807. non-`nil', then `quit-flag' has no special effect.
  2808. -- Macro: with-local-quit body...
  2809. This macro executes BODY forms in sequence, but allows quitting, at
  2810. least locally, within BODY even if `inhibit-quit' was non-`nil'
  2811. outside this construct. It returns the value of the last form in
  2812. BODY, unless exited by quitting, in which case it returns `nil'.
  2813. If `inhibit-quit' is `nil' on entry to `with-local-quit', it only
  2814. executes the BODY, and setting `quit-flag' causes a normal quit.
  2815. However, if `inhibit-quit' is non-`nil' so that ordinary quitting
  2816. is delayed, a non-`nil' `quit-flag' triggers a special kind of
  2817. local quit. This ends the execution of BODY and exits the
  2818. `with-local-quit' body with `quit-flag' still non-`nil', so that
  2819. another (ordinary) quit will happen as soon as that is allowed.
  2820. If `quit-flag' is already non-`nil' at the beginning of BODY, the
  2821. local quit happens immediately and the body doesn't execute at all.
  2822. This macro is mainly useful in functions that can be called from
  2823. timers, process filters, process sentinels, `pre-command-hook',
  2824. `post-command-hook', and other places where `inhibit-quit' is
  2825. normally bound to `t'.
  2826. -- Command: keyboard-quit
  2827. This function signals the `quit' condition with `(signal 'quit
  2828. nil)'. This is the same thing that quitting does. (See `signal'
  2829. in *note Errors::.)
  2830. You can specify a character other than `C-g' to use for quitting.
  2831. See the function `set-input-mode' in *note Terminal Input::.
  2832. 
  2833. File: elisp, Node: Prefix Command Arguments, Next: Recursive Editing, Prev: Quitting, Up: Command Loop
  2834. 21.12 Prefix Command Arguments
  2835. ==============================
  2836. Most Emacs commands can use a "prefix argument", a number specified
  2837. before the command itself. (Don't confuse prefix arguments with prefix
  2838. keys.) The prefix argument is at all times represented by a value,
  2839. which may be `nil', meaning there is currently no prefix argument.
  2840. Each command may use the prefix argument or ignore it.
  2841. There are two representations of the prefix argument: "raw" and
  2842. "numeric". The editor command loop uses the raw representation
  2843. internally, and so do the Lisp variables that store the information, but
  2844. commands can request either representation.
  2845. Here are the possible values of a raw prefix argument:
  2846. * `nil', meaning there is no prefix argument. Its numeric value is
  2847. 1, but numerous commands make a distinction between `nil' and the
  2848. integer 1.
  2849. * An integer, which stands for itself.
  2850. * A list of one element, which is an integer. This form of prefix
  2851. argument results from one or a succession of `C-u's with no
  2852. digits. The numeric value is the integer in the list, but some
  2853. commands make a distinction between such a list and an integer
  2854. alone.
  2855. * The symbol `-'. This indicates that `M--' or `C-u -' was typed,
  2856. without following digits. The equivalent numeric value is -1, but
  2857. some commands make a distinction between the integer -1 and the
  2858. symbol `-'.
  2859. We illustrate these possibilities by calling the following function
  2860. with various prefixes:
  2861. (defun display-prefix (arg)
  2862. "Display the value of the raw prefix arg."
  2863. (interactive "P")
  2864. (message "%s" arg))
  2865. Here are the results of calling `display-prefix' with various raw
  2866. prefix arguments:
  2867. M-x display-prefix -| nil
  2868. C-u M-x display-prefix -| (4)
  2869. C-u C-u M-x display-prefix -| (16)
  2870. C-u 3 M-x display-prefix -| 3
  2871. M-3 M-x display-prefix -| 3 ; (Same as `C-u 3'.)
  2872. C-u - M-x display-prefix -| -
  2873. M-- M-x display-prefix -| - ; (Same as `C-u -'.)
  2874. C-u - 7 M-x display-prefix -| -7
  2875. M-- 7 M-x display-prefix -| -7 ; (Same as `C-u -7'.)
  2876. Emacs uses two variables to store the prefix argument: `prefix-arg'
  2877. and `current-prefix-arg'. Commands such as `universal-argument' that
  2878. set up prefix arguments for other commands store them in `prefix-arg'.
  2879. In contrast, `current-prefix-arg' conveys the prefix argument to the
  2880. current command, so setting it has no effect on the prefix arguments
  2881. for future commands.
  2882. Normally, commands specify which representation to use for the prefix
  2883. argument, either numeric or raw, in the `interactive' specification.
  2884. (*Note Using Interactive::.) Alternatively, functions may look at the
  2885. value of the prefix argument directly in the variable
  2886. `current-prefix-arg', but this is less clean.
  2887. -- Function: prefix-numeric-value arg
  2888. This function returns the numeric meaning of a valid raw prefix
  2889. argument value, ARG. The argument may be a symbol, a number, or a
  2890. list. If it is `nil', the value 1 is returned; if it is `-', the
  2891. value -1 is returned; if it is a number, that number is returned;
  2892. if it is a list, the CAR of that list (which should be a number) is
  2893. returned.
  2894. -- Variable: current-prefix-arg
  2895. This variable holds the raw prefix argument for the _current_
  2896. command. Commands may examine it directly, but the usual method
  2897. for accessing it is with `(interactive "P")'.
  2898. -- Variable: prefix-arg
  2899. The value of this variable is the raw prefix argument for the
  2900. _next_ editing command. Commands such as `universal-argument'
  2901. that specify prefix arguments for the following command work by
  2902. setting this variable.
  2903. -- Variable: last-prefix-arg
  2904. The raw prefix argument value used by the previous command.
  2905. The following commands exist to set up prefix arguments for the
  2906. following command. Do not call them for any other reason.
  2907. -- Command: universal-argument
  2908. This command reads input and specifies a prefix argument for the
  2909. following command. Don't call this command yourself unless you
  2910. know what you are doing.
  2911. -- Command: digit-argument arg
  2912. This command adds to the prefix argument for the following
  2913. command. The argument ARG is the raw prefix argument as it was
  2914. before this command; it is used to compute the updated prefix
  2915. argument. Don't call this command yourself unless you know what
  2916. you are doing.
  2917. -- Command: negative-argument arg
  2918. This command adds to the numeric argument for the next command.
  2919. The argument ARG is the raw prefix argument as it was before this
  2920. command; its value is negated to form the new prefix argument.
  2921. Don't call this command yourself unless you know what you are
  2922. doing.
  2923. 
  2924. File: elisp, Node: Recursive Editing, Next: Disabling Commands, Prev: Prefix Command Arguments, Up: Command Loop
  2925. 21.13 Recursive Editing
  2926. =======================
  2927. The Emacs command loop is entered automatically when Emacs starts up.
  2928. This top-level invocation of the command loop never exits; it keeps
  2929. running as long as Emacs does. Lisp programs can also invoke the
  2930. command loop. Since this makes more than one activation of the command
  2931. loop, we call it "recursive editing". A recursive editing level has
  2932. the effect of suspending whatever command invoked it and permitting the
  2933. user to do arbitrary editing before resuming that command.
  2934. The commands available during recursive editing are the same ones
  2935. available in the top-level editing loop and defined in the keymaps.
  2936. Only a few special commands exit the recursive editing level; the others
  2937. return to the recursive editing level when they finish. (The special
  2938. commands for exiting are always available, but they do nothing when
  2939. recursive editing is not in progress.)
  2940. All command loops, including recursive ones, set up all-purpose error
  2941. handlers so that an error in a command run from the command loop will
  2942. not exit the loop.
  2943. Minibuffer input is a special kind of recursive editing. It has a
  2944. few special wrinkles, such as enabling display of the minibuffer and the
  2945. minibuffer window, but fewer than you might suppose. Certain keys
  2946. behave differently in the minibuffer, but that is only because of the
  2947. minibuffer's local map; if you switch windows, you get the usual Emacs
  2948. commands.
  2949. To invoke a recursive editing level, call the function
  2950. `recursive-edit'. This function contains the command loop; it also
  2951. contains a call to `catch' with tag `exit', which makes it possible to
  2952. exit the recursive editing level by throwing to `exit' (*note Catch and
  2953. Throw::). If you throw a value other than `t', then `recursive-edit'
  2954. returns normally to the function that called it. The command `C-M-c'
  2955. (`exit-recursive-edit') does this. Throwing a `t' value causes
  2956. `recursive-edit' to quit, so that control returns to the command loop
  2957. one level up. This is called "aborting", and is done by `C-]'
  2958. (`abort-recursive-edit').
  2959. Most applications should not use recursive editing, except as part of
  2960. using the minibuffer. Usually it is more convenient for the user if you
  2961. change the major mode of the current buffer temporarily to a special
  2962. major mode, which should have a command to go back to the previous mode.
  2963. (The `e' command in Rmail uses this technique.) Or, if you wish to
  2964. give the user different text to edit "recursively", create and select a
  2965. new buffer in a special mode. In this mode, define a command to
  2966. complete the processing and go back to the previous buffer. (The `m'
  2967. command in Rmail does this.)
  2968. Recursive edits are useful in debugging. You can insert a call to
  2969. `debug' into a function definition as a sort of breakpoint, so that you
  2970. can look around when the function gets there. `debug' invokes a
  2971. recursive edit but also provides the other features of the debugger.
  2972. Recursive editing levels are also used when you type `C-r' in
  2973. `query-replace' or use `C-x q' (`kbd-macro-query').
  2974. -- Command: recursive-edit
  2975. This function invokes the editor command loop. It is called
  2976. automatically by the initialization of Emacs, to let the user begin
  2977. editing. When called from a Lisp program, it enters a recursive
  2978. editing level.
  2979. If the current buffer is not the same as the selected window's
  2980. buffer, `recursive-edit' saves and restores the current buffer.
  2981. Otherwise, if you switch buffers, the buffer you switched to is
  2982. current after `recursive-edit' returns.
  2983. In the following example, the function `simple-rec' first advances
  2984. point one word, then enters a recursive edit, printing out a
  2985. message in the echo area. The user can then do any editing
  2986. desired, and then type `C-M-c' to exit and continue executing
  2987. `simple-rec'.
  2988. (defun simple-rec ()
  2989. (forward-word 1)
  2990. (message "Recursive edit in progress")
  2991. (recursive-edit)
  2992. (forward-word 1))
  2993. => simple-rec
  2994. (simple-rec)
  2995. => nil
  2996. -- Command: exit-recursive-edit
  2997. This function exits from the innermost recursive edit (including
  2998. minibuffer input). Its definition is effectively `(throw 'exit
  2999. nil)'.
  3000. -- Command: abort-recursive-edit
  3001. This function aborts the command that requested the innermost
  3002. recursive edit (including minibuffer input), by signaling `quit'
  3003. after exiting the recursive edit. Its definition is effectively
  3004. `(throw 'exit t)'. *Note Quitting::.
  3005. -- Command: top-level
  3006. This function exits all recursive editing levels; it does not
  3007. return a value, as it jumps completely out of any computation
  3008. directly back to the main command loop.
  3009. -- Function: recursion-depth
  3010. This function returns the current depth of recursive edits. When
  3011. no recursive edit is active, it returns 0.
  3012. 
  3013. File: elisp, Node: Disabling Commands, Next: Command History, Prev: Recursive Editing, Up: Command Loop
  3014. 21.14 Disabling Commands
  3015. ========================
  3016. "Disabling a command" marks the command as requiring user confirmation
  3017. before it can be executed. Disabling is used for commands which might
  3018. be confusing to beginning users, to prevent them from using the
  3019. commands by accident.
  3020. The low-level mechanism for disabling a command is to put a
  3021. non-`nil' `disabled' property on the Lisp symbol for the command.
  3022. These properties are normally set up by the user's init file (*note
  3023. Init File::) with Lisp expressions such as this:
  3024. (put 'upcase-region 'disabled t)
  3025. For a few commands, these properties are present by default (you can
  3026. remove them in your init file if you wish).
  3027. If the value of the `disabled' property is a string, the message
  3028. saying the command is disabled includes that string. For example:
  3029. (put 'delete-region 'disabled
  3030. "Text deleted this way cannot be yanked back!\n")
  3031. *Note Disabling: (emacs)Disabling, for the details on what happens
  3032. when a disabled command is invoked interactively. Disabling a command
  3033. has no effect on calling it as a function from Lisp programs.
  3034. -- Command: enable-command command
  3035. Allow COMMAND (a symbol) to be executed without special
  3036. confirmation from now on, and alter the user's init file (*note
  3037. Init File::) so that this will apply to future sessions.
  3038. -- Command: disable-command command
  3039. Require special confirmation to execute COMMAND from now on, and
  3040. alter the user's init file so that this will apply to future
  3041. sessions.
  3042. -- Variable: disabled-command-function
  3043. The value of this variable should be a function. When the user
  3044. invokes a disabled command interactively, this function is called
  3045. instead of the disabled command. It can use `this-command-keys'
  3046. to determine what the user typed to run the command, and thus find
  3047. the command itself.
  3048. The value may also be `nil'. Then all commands work normally,
  3049. even disabled ones.
  3050. By default, the value is a function that asks the user whether to
  3051. proceed.
  3052. 
  3053. File: elisp, Node: Command History, Next: Keyboard Macros, Prev: Disabling Commands, Up: Command Loop
  3054. 21.15 Command History
  3055. =====================
  3056. The command loop keeps a history of the complex commands that have been
  3057. executed, to make it convenient to repeat these commands. A "complex
  3058. command" is one for which the interactive argument reading uses the
  3059. minibuffer. This includes any `M-x' command, any `M-:' command, and
  3060. any command whose `interactive' specification reads an argument from
  3061. the minibuffer. Explicit use of the minibuffer during the execution of
  3062. the command itself does not cause the command to be considered complex.
  3063. -- Variable: command-history
  3064. This variable's value is a list of recent complex commands, each
  3065. represented as a form to evaluate. It continues to accumulate all
  3066. complex commands for the duration of the editing session, but when
  3067. it reaches the maximum size (*note Minibuffer History::), the
  3068. oldest elements are deleted as new ones are added.
  3069. command-history
  3070. => ((switch-to-buffer "chistory.texi")
  3071. (describe-key "^X^[")
  3072. (visit-tags-table "~/emacs/src/")
  3073. (find-tag "repeat-complex-command"))
  3074. This history list is actually a special case of minibuffer history
  3075. (*note Minibuffer History::), with one special twist: the elements are
  3076. expressions rather than strings.
  3077. There are a number of commands devoted to the editing and recall of
  3078. previous commands. The commands `repeat-complex-command', and
  3079. `list-command-history' are described in the user manual (*note
  3080. Repetition: (emacs)Repetition.). Within the minibuffer, the usual
  3081. minibuffer history commands are available.
  3082. 
  3083. File: elisp, Node: Keyboard Macros, Prev: Command History, Up: Command Loop
  3084. 21.16 Keyboard Macros
  3085. =====================
  3086. A "keyboard macro" is a canned sequence of input events that can be
  3087. considered a command and made the definition of a key. The Lisp
  3088. representation of a keyboard macro is a string or vector containing the
  3089. events. Don't confuse keyboard macros with Lisp macros (*note
  3090. Macros::).
  3091. -- Function: execute-kbd-macro kbdmacro &optional count loopfunc
  3092. This function executes KBDMACRO as a sequence of events. If
  3093. KBDMACRO is a string or vector, then the events in it are executed
  3094. exactly as if they had been input by the user. The sequence is
  3095. _not_ expected to be a single key sequence; normally a keyboard
  3096. macro definition consists of several key sequences concatenated.
  3097. If KBDMACRO is a symbol, then its function definition is used in
  3098. place of KBDMACRO. If that is another symbol, this process
  3099. repeats. Eventually the result should be a string or vector. If
  3100. the result is not a symbol, string, or vector, an error is
  3101. signaled.
  3102. The argument COUNT is a repeat count; KBDMACRO is executed that
  3103. many times. If COUNT is omitted or `nil', KBDMACRO is executed
  3104. once. If it is 0, KBDMACRO is executed over and over until it
  3105. encounters an error or a failing search.
  3106. If LOOPFUNC is non-`nil', it is a function that is called, without
  3107. arguments, prior to each iteration of the macro. If LOOPFUNC
  3108. returns `nil', then this stops execution of the macro.
  3109. *Note Reading One Event::, for an example of using
  3110. `execute-kbd-macro'.
  3111. -- Variable: executing-kbd-macro
  3112. This variable contains the string or vector that defines the
  3113. keyboard macro that is currently executing. It is `nil' if no
  3114. macro is currently executing. A command can test this variable so
  3115. as to behave differently when run from an executing macro. Do not
  3116. set this variable yourself.
  3117. -- Variable: defining-kbd-macro
  3118. This variable is non-`nil' if and only if a keyboard macro is
  3119. being defined. A command can test this variable so as to behave
  3120. differently while a macro is being defined. The value is `append'
  3121. while appending to the definition of an existing macro. The
  3122. commands `start-kbd-macro', `kmacro-start-macro' and
  3123. `end-kbd-macro' set this variable--do not set it yourself.
  3124. The variable is always local to the current terminal and cannot be
  3125. buffer-local. *Note Multiple Terminals::.
  3126. -- Variable: last-kbd-macro
  3127. This variable is the definition of the most recently defined
  3128. keyboard macro. Its value is a string or vector, or `nil'.
  3129. The variable is always local to the current terminal and cannot be
  3130. buffer-local. *Note Multiple Terminals::.
  3131. -- Variable: kbd-macro-termination-hook
  3132. This normal hook is run when a keyboard macro terminates,
  3133. regardless of what caused it to terminate (reaching the macro end
  3134. or an error which ended the macro prematurely).
  3135. 
  3136. File: elisp, Node: Keymaps, Next: Modes, Prev: Command Loop, Up: Top
  3137. 22 Keymaps
  3138. **********
  3139. The command bindings of input events are recorded in data structures
  3140. called "keymaps". Each entry in a keymap associates (or "binds") an
  3141. individual event type, either to another keymap or to a command. When
  3142. an event type is bound to a keymap, that keymap is used to look up the
  3143. next input event; this continues until a command is found. The whole
  3144. process is called "key lookup".
  3145. * Menu:
  3146. * Key Sequences:: Key sequences as Lisp objects.
  3147. * Keymap Basics:: Basic concepts of keymaps.
  3148. * Format of Keymaps:: What a keymap looks like as a Lisp object.
  3149. * Creating Keymaps:: Functions to create and copy keymaps.
  3150. * Inheritance and Keymaps:: How one keymap can inherit the bindings
  3151. of another keymap.
  3152. * Prefix Keys:: Defining a key with a keymap as its definition.
  3153. * Active Keymaps:: How Emacs searches the active keymaps
  3154. for a key binding.
  3155. * Searching Keymaps:: A pseudo-Lisp summary of searching active maps.
  3156. * Controlling Active Maps:: Each buffer has a local keymap
  3157. to override the standard (global) bindings.
  3158. A minor mode can also override them.
  3159. * Key Lookup:: Finding a key's binding in one keymap.
  3160. * Functions for Key Lookup:: How to request key lookup.
  3161. * Changing Key Bindings:: Redefining a key in a keymap.
  3162. * Remapping Commands:: A keymap can translate one command to another.
  3163. * Translation Keymaps:: Keymaps for translating sequences of events.
  3164. * Key Binding Commands:: Interactive interfaces for redefining keys.
  3165. * Scanning Keymaps:: Looking through all keymaps, for printing help.
  3166. * Menu Keymaps:: Defining a menu as a keymap.
  3167. 
  3168. File: elisp, Node: Key Sequences, Next: Keymap Basics, Up: Keymaps
  3169. 22.1 Key Sequences
  3170. ==================
  3171. A "key sequence", or "key" for short, is a sequence of one or more
  3172. input events that form a unit. Input events include characters,
  3173. function keys, and mouse actions (*note Input Events::). The Emacs
  3174. Lisp representation for a key sequence is a string or vector. Unless
  3175. otherwise stated, any Emacs Lisp function that accepts a key sequence
  3176. as an argument can handle both representations.
  3177. In the string representation, alphanumeric characters ordinarily
  3178. stand for themselves; for example, `"a"' represents `a' and `"2"'
  3179. represents `2'. Control character events are prefixed by the substring
  3180. `"\C-"', and meta characters by `"\M-"'; for example, `"\C-x"'
  3181. represents the key `C-x'. In addition, the <TAB>, <RET>, <ESC>, and
  3182. <DEL> events are represented by `"\t"', `"\r"', `"\e"', and `"\d"'
  3183. respectively. The string representation of a complete key sequence is
  3184. the concatenation of the string representations of the constituent
  3185. events; thus, `"\C-xl"' represents the key sequence `C-x l'.
  3186. Key sequences containing function keys, mouse button events, or
  3187. non-ASCII characters such as `C-=' or `H-a' cannot be represented as
  3188. strings; they have to be represented as vectors.
  3189. In the vector representation, each element of the vector represents
  3190. an input event, in its Lisp form. *Note Input Events::. For example,
  3191. the vector `[?\C-x ?l]' represents the key sequence `C-x l'.
  3192. For examples of key sequences written in string and vector
  3193. representations, *note Init Rebinding: (emacs)Init Rebinding.
  3194. -- Macro: kbd keyseq-text
  3195. This macro converts the text KEYSEQ-TEXT (a string constant) into
  3196. a key sequence (a string or vector constant). The contents of
  3197. KEYSEQ-TEXT should describe the key sequence using almost the same
  3198. syntax used in this manual. More precisely, it uses the same
  3199. syntax that Edit Macro mode uses for editing keyboard macros
  3200. (*note Edit Keyboard Macro: (emacs)Edit Keyboard Macro.); you must
  3201. surround function key names with `<...>'.
  3202. (kbd "C-x") => "\C-x"
  3203. (kbd "C-x C-f") => "\C-x\C-f"
  3204. (kbd "C-x 4 C-f") => "\C-x4\C-f"
  3205. (kbd "X") => "X"
  3206. (kbd "RET") => "\^M"
  3207. (kbd "C-c SPC") => "\C-c "
  3208. (kbd "<f1> SPC") => [f1 32]
  3209. (kbd "C-M-<down>") => [C-M-down]
  3210. This macro is not meant for use with arguments that vary--only
  3211. with string constants.
  3212. 
  3213. File: elisp, Node: Keymap Basics, Next: Format of Keymaps, Prev: Key Sequences, Up: Keymaps
  3214. 22.2 Keymap Basics
  3215. ==================
  3216. A keymap is a Lisp data structure that specifies "key bindings" for
  3217. various key sequences.
  3218. A single keymap directly specifies definitions for individual
  3219. events. When a key sequence consists of a single event, its binding in
  3220. a keymap is the keymap's definition for that event. The binding of a
  3221. longer key sequence is found by an iterative process: first find the
  3222. definition of the first event (which must itself be a keymap); then
  3223. find the second event's definition in that keymap, and so on until all
  3224. the events in the key sequence have been processed.
  3225. If the binding of a key sequence is a keymap, we call the key
  3226. sequence a "prefix key". Otherwise, we call it a "complete key"
  3227. (because no more events can be added to it). If the binding is `nil',
  3228. we call the key "undefined". Examples of prefix keys are `C-c', `C-x',
  3229. and `C-x 4'. Examples of defined complete keys are `X', <RET>, and
  3230. `C-x 4 C-f'. Examples of undefined complete keys are `C-x C-g', and
  3231. `C-c 3'. *Note Prefix Keys::, for more details.
  3232. The rule for finding the binding of a key sequence assumes that the
  3233. intermediate bindings (found for the events before the last) are all
  3234. keymaps; if this is not so, the sequence of events does not form a
  3235. unit--it is not really one key sequence. In other words, removing one
  3236. or more events from the end of any valid key sequence must always yield
  3237. a prefix key. For example, `C-f C-n' is not a key sequence; `C-f' is
  3238. not a prefix key, so a longer sequence starting with `C-f' cannot be a
  3239. key sequence.
  3240. The set of possible multi-event key sequences depends on the bindings
  3241. for prefix keys; therefore, it can be different for different keymaps,
  3242. and can change when bindings are changed. However, a one-event sequence
  3243. is always a key sequence, because it does not depend on any prefix keys
  3244. for its well-formedness.
  3245. At any time, several primary keymaps are "active"--that is, in use
  3246. for finding key bindings. These are the "global map", which is shared
  3247. by all buffers; the "local keymap", which is usually associated with a
  3248. specific major mode; and zero or more "minor mode keymaps", which
  3249. belong to currently enabled minor modes. (Not all minor modes have
  3250. keymaps.) The local keymap bindings shadow (i.e., take precedence
  3251. over) the corresponding global bindings. The minor mode keymaps shadow
  3252. both local and global keymaps. *Note Active Keymaps::, for details.
  3253. 
  3254. File: elisp, Node: Format of Keymaps, Next: Creating Keymaps, Prev: Keymap Basics, Up: Keymaps
  3255. 22.3 Format of Keymaps
  3256. ======================
  3257. Each keymap is a list whose CAR is the symbol `keymap'. The remaining
  3258. elements of the list define the key bindings of the keymap. A symbol
  3259. whose function definition is a keymap is also a keymap. Use the
  3260. function `keymapp' (see below) to test whether an object is a keymap.
  3261. Several kinds of elements may appear in a keymap, after the symbol
  3262. `keymap' that begins it:
  3263. `(TYPE . BINDING)'
  3264. This specifies one binding, for events of type TYPE. Each
  3265. ordinary binding applies to events of a particular "event type",
  3266. which is always a character or a symbol. *Note Classifying
  3267. Events::. In this kind of binding, BINDING is a command.
  3268. `(TYPE ITEM-NAME . BINDING)'
  3269. This specifies a binding which is also a simple menu item that
  3270. displays as ITEM-NAME in the menu. *Note Simple Menu Items::.
  3271. `(TYPE ITEM-NAME HELP-STRING . BINDING)'
  3272. This is a simple menu item with help string HELP-STRING.
  3273. `(TYPE menu-item . DETAILS)'
  3274. This specifies a binding which is also an extended menu item. This
  3275. allows use of other features. *Note Extended Menu Items::.
  3276. `(t . BINDING)'
  3277. This specifies a "default key binding"; any event not bound by
  3278. other elements of the keymap is given BINDING as its binding.
  3279. Default bindings allow a keymap to bind all possible event types
  3280. without having to enumerate all of them. A keymap that has a
  3281. default binding completely masks any lower-precedence keymap,
  3282. except for events explicitly bound to `nil' (see below).
  3283. `CHAR-TABLE'
  3284. If an element of a keymap is a char-table, it counts as holding
  3285. bindings for all character events with no modifier bits (*note
  3286. modifier bits::): element N is the binding for the character with
  3287. code N. This is a compact way to record lots of bindings. A
  3288. keymap with such a char-table is called a "full keymap". Other
  3289. keymaps are called "sparse keymaps".
  3290. `STRING'
  3291. Aside from elements that specify bindings for keys, a keymap can
  3292. also have a string as an element. This is called the "overall
  3293. prompt string" and makes it possible to use the keymap as a menu.
  3294. *Note Defining Menus::.
  3295. When the binding is `nil', it doesn't constitute a definition but it
  3296. does take precedence over a default binding or a binding in the parent
  3297. keymap. On the other hand, a binding of `nil' does _not_ override
  3298. lower-precedence keymaps; thus, if the local map gives a binding of
  3299. `nil', Emacs uses the binding from the global map.
  3300. Keymaps do not directly record bindings for the meta characters.
  3301. Instead, meta characters are regarded for purposes of key lookup as
  3302. sequences of two characters, the first of which is <ESC> (or whatever
  3303. is currently the value of `meta-prefix-char'). Thus, the key `M-a' is
  3304. internally represented as `<ESC> a', and its global binding is found at
  3305. the slot for `a' in `esc-map' (*note Prefix Keys::).
  3306. This conversion applies only to characters, not to function keys or
  3307. other input events; thus, `M-<end>' has nothing to do with `<ESC>
  3308. <end>'.
  3309. Here as an example is the local keymap for Lisp mode, a sparse
  3310. keymap. It defines bindings for <DEL>, `C-c C-z', `C-M-q', and `C-M-x'
  3311. (the actual value also contains a menu binding, which is omitted here
  3312. for the sake of brevity).
  3313. lisp-mode-map
  3314. =>
  3315. (keymap
  3316. (3 keymap
  3317. ;; C-c C-z
  3318. (26 . run-lisp))
  3319. (27 keymap
  3320. ;; `C-M-x', treated as `<ESC> C-x'
  3321. (24 . lisp-send-defun))
  3322. ;; This part is inherited from `lisp-mode-shared-map'.
  3323. keymap
  3324. ;; <DEL>
  3325. (127 . backward-delete-char-untabify)
  3326. (27 keymap
  3327. ;; `C-M-q', treated as `<ESC> C-q'
  3328. (17 . indent-sexp)))
  3329. -- Function: keymapp object
  3330. This function returns `t' if OBJECT is a keymap, `nil' otherwise.
  3331. More precisely, this function tests for a list whose CAR is
  3332. `keymap', or for a symbol whose function definition satisfies
  3333. `keymapp'.
  3334. (keymapp '(keymap))
  3335. => t
  3336. (fset 'foo '(keymap))
  3337. (keymapp 'foo)
  3338. => t
  3339. (keymapp (current-global-map))
  3340. => t
  3341. 
  3342. File: elisp, Node: Creating Keymaps, Next: Inheritance and Keymaps, Prev: Format of Keymaps, Up: Keymaps
  3343. 22.4 Creating Keymaps
  3344. =====================
  3345. Here we describe the functions for creating keymaps.
  3346. -- Function: make-sparse-keymap &optional prompt
  3347. This function creates and returns a new sparse keymap with no
  3348. entries. (A sparse keymap is the kind of keymap you usually
  3349. want.) The new keymap does not contain a char-table, unlike
  3350. `make-keymap', and does not bind any events.
  3351. (make-sparse-keymap)
  3352. => (keymap)
  3353. If you specify PROMPT, that becomes the overall prompt string for
  3354. the keymap. You should specify this only for menu keymaps (*note
  3355. Defining Menus::). A keymap with an overall prompt string will
  3356. always present a mouse menu or a keyboard menu if it is active for
  3357. looking up the next input event. Don't specify an overall prompt
  3358. string for the main map of a major or minor mode, because that
  3359. would cause the command loop to present a keyboard menu every time.
  3360. -- Function: make-keymap &optional prompt
  3361. This function creates and returns a new full keymap. That keymap
  3362. contains a char-table (*note Char-Tables::) with slots for all
  3363. characters without modifiers. The new keymap initially binds all
  3364. these characters to `nil', and does not bind any other kind of
  3365. event. The argument PROMPT specifies a prompt string, as in
  3366. `make-sparse-keymap'.
  3367. (make-keymap)
  3368. => (keymap #^[t nil nil nil ... nil nil keymap])
  3369. A full keymap is more efficient than a sparse keymap when it holds
  3370. lots of bindings; for just a few, the sparse keymap is better.
  3371. -- Function: copy-keymap keymap
  3372. This function returns a copy of KEYMAP. Any keymaps that appear
  3373. directly as bindings in KEYMAP are also copied recursively, and so
  3374. on to any number of levels. However, recursive copying does not
  3375. take place when the definition of a character is a symbol whose
  3376. function definition is a keymap; the same symbol appears in the
  3377. new copy.
  3378. (setq map (copy-keymap (current-local-map)))
  3379. => (keymap
  3380. ;; (This implements meta characters.)
  3381. (27 keymap
  3382. (83 . center-paragraph)
  3383. (115 . center-line))
  3384. (9 . tab-to-tab-stop))
  3385. (eq map (current-local-map))
  3386. => nil
  3387. (equal map (current-local-map))
  3388. => t
  3389. 
  3390. File: elisp, Node: Inheritance and Keymaps, Next: Prefix Keys, Prev: Creating Keymaps, Up: Keymaps
  3391. 22.5 Inheritance and Keymaps
  3392. ============================
  3393. A keymap can inherit the bindings of another keymap, which we call the
  3394. "parent keymap". Such a keymap looks like this:
  3395. (keymap ELEMENTS... . PARENT-KEYMAP)
  3396. The effect is that this keymap inherits all the bindings of
  3397. PARENT-KEYMAP, whatever they may be at the time a key is looked up, but
  3398. can add to them or override them with ELEMENTS.
  3399. If you change the bindings in PARENT-KEYMAP using `define-key' or
  3400. other key-binding functions, these changed bindings are visible in the
  3401. inheriting keymap, unless shadowed by the bindings made by ELEMENTS.
  3402. The converse is not true: if you use `define-key' to change bindings in
  3403. the inheriting keymap, these changes are recorded in ELEMENTS, but have
  3404. no effect on PARENT-KEYMAP.
  3405. The proper way to construct a keymap with a parent is to use
  3406. `set-keymap-parent'; if you have code that directly constructs a keymap
  3407. with a parent, please convert the program to use `set-keymap-parent'
  3408. instead.
  3409. -- Function: keymap-parent keymap
  3410. This returns the parent keymap of KEYMAP. If KEYMAP has no
  3411. parent, `keymap-parent' returns `nil'.
  3412. -- Function: set-keymap-parent keymap parent
  3413. This sets the parent keymap of KEYMAP to PARENT, and returns
  3414. PARENT. If PARENT is `nil', this function gives KEYMAP no parent
  3415. at all.
  3416. If KEYMAP has submaps (bindings for prefix keys), they too receive
  3417. new parent keymaps that reflect what PARENT specifies for those
  3418. prefix keys.
  3419. Here is an example showing how to make a keymap that inherits from
  3420. `text-mode-map':
  3421. (let ((map (make-sparse-keymap)))
  3422. (set-keymap-parent map text-mode-map)
  3423. map)
  3424. A non-sparse keymap can have a parent too, but this is not very
  3425. useful. A non-sparse keymap always specifies something as the binding
  3426. for every numeric character code without modifier bits, even if it is
  3427. `nil', so these character's bindings are never inherited from the
  3428. parent keymap.
  3429. Sometimes you want to make a keymap that inherits from more than one
  3430. map. You can use the function `make-composed-keymap' for this.
  3431. -- Function: make-composed-keymap maps &optional parent
  3432. This function returns a new keymap composed of the existing
  3433. keymap(s) MAPS, and optionally inheriting from a parent keymap
  3434. PARENT. MAPS can be a single keymap or a list of more than one.
  3435. When looking up a key in the resulting new map, Emacs searches in
  3436. each of the MAPS in turn, and then in PARENT, stopping at the
  3437. first match. A `nil' binding in any one of MAPS overrides any
  3438. binding in PARENT, but it does not override any non-`nil' binding
  3439. in any other of the MAPS.
  3440. For example, here is how Emacs sets the parent of `help-mode-map', such
  3441. that it inherits from both `button-buffer-map' and `special-mode-map':
  3442. (defvar help-mode-map
  3443. (let ((map (make-sparse-keymap)))
  3444. (set-keymap-parent map
  3445. (make-composed-keymap button-buffer-map special-mode-map))
  3446. ... map) ... )
  3447. 
  3448. File: elisp, Node: Prefix Keys, Next: Active Keymaps, Prev: Inheritance and Keymaps, Up: Keymaps
  3449. 22.6 Prefix Keys
  3450. ================
  3451. A "prefix key" is a key sequence whose binding is a keymap. The keymap
  3452. defines what to do with key sequences that extend the prefix key. For
  3453. example, `C-x' is a prefix key, and it uses a keymap that is also
  3454. stored in the variable `ctl-x-map'. This keymap defines bindings for
  3455. key sequences starting with `C-x'.
  3456. Some of the standard Emacs prefix keys use keymaps that are also
  3457. found in Lisp variables:
  3458. * `esc-map' is the global keymap for the <ESC> prefix key. Thus,
  3459. the global definitions of all meta characters are actually found
  3460. here. This map is also the function definition of `ESC-prefix'.
  3461. * `help-map' is the global keymap for the `C-h' prefix key.
  3462. * `mode-specific-map' is the global keymap for the prefix key `C-c'.
  3463. This map is actually global, not mode-specific, but its name
  3464. provides useful information about `C-c' in the output of `C-h b'
  3465. (`display-bindings'), since the main use of this prefix key is for
  3466. mode-specific bindings.
  3467. * `ctl-x-map' is the global keymap used for the `C-x' prefix key.
  3468. This map is found via the function cell of the symbol
  3469. `Control-X-prefix'.
  3470. * `mule-keymap' is the global keymap used for the `C-x <RET>' prefix
  3471. key.
  3472. * `ctl-x-4-map' is the global keymap used for the `C-x 4' prefix key.
  3473. * `ctl-x-5-map' is the global keymap used for the `C-x 5' prefix key.
  3474. * `2C-mode-map' is the global keymap used for the `C-x 6' prefix key.
  3475. * `vc-prefix-map' is the global keymap used for the `C-x v' prefix
  3476. key.
  3477. * `goto-map' is the global keymap used for the `M-g' prefix key.
  3478. * `search-map' is the global keymap used for the `M-s' prefix key.
  3479. * `facemenu-keymap' is the global keymap used for the `M-o' prefix
  3480. key.
  3481. * The other Emacs prefix keys are `C-x @', `C-x a i', `C-x <ESC>'
  3482. and `<ESC> <ESC>'. They use keymaps that have no special names.
  3483. The keymap binding of a prefix key is used for looking up the event
  3484. that follows the prefix key. (It may instead be a symbol whose function
  3485. definition is a keymap. The effect is the same, but the symbol serves
  3486. as a name for the prefix key.) Thus, the binding of `C-x' is the
  3487. symbol `Control-X-prefix', whose function cell holds the keymap for
  3488. `C-x' commands. (The same keymap is also the value of `ctl-x-map'.)
  3489. Prefix key definitions can appear in any active keymap. The
  3490. definitions of `C-c', `C-x', `C-h' and <ESC> as prefix keys appear in
  3491. the global map, so these prefix keys are always available. Major and
  3492. minor modes can redefine a key as a prefix by putting a prefix key
  3493. definition for it in the local map or the minor mode's map. *Note
  3494. Active Keymaps::.
  3495. If a key is defined as a prefix in more than one active map, then its
  3496. various definitions are in effect merged: the commands defined in the
  3497. minor mode keymaps come first, followed by those in the local map's
  3498. prefix definition, and then by those from the global map.
  3499. In the following example, we make `C-p' a prefix key in the local
  3500. keymap, in such a way that `C-p' is identical to `C-x'. Then the
  3501. binding for `C-p C-f' is the function `find-file', just like `C-x C-f'.
  3502. The key sequence `C-p 6' is not found in any active keymap.
  3503. (use-local-map (make-sparse-keymap))
  3504. => nil
  3505. (local-set-key "\C-p" ctl-x-map)
  3506. => nil
  3507. (key-binding "\C-p\C-f")
  3508. => find-file
  3509. (key-binding "\C-p6")
  3510. => nil
  3511. -- Function: define-prefix-command symbol &optional mapvar prompt
  3512. This function prepares SYMBOL for use as a prefix key's binding:
  3513. it creates a sparse keymap and stores it as SYMBOL's function
  3514. definition. Subsequently binding a key sequence to SYMBOL will
  3515. make that key sequence into a prefix key. The return value is
  3516. `symbol'.
  3517. This function also sets SYMBOL as a variable, with the keymap as
  3518. its value. But if MAPVAR is non-`nil', it sets MAPVAR as a
  3519. variable instead.
  3520. If PROMPT is non-`nil', that becomes the overall prompt string for
  3521. the keymap. The prompt string should be given for menu keymaps
  3522. (*note Defining Menus::).
  3523. 
  3524. File: elisp, Node: Active Keymaps, Next: Searching Keymaps, Prev: Prefix Keys, Up: Keymaps
  3525. 22.7 Active Keymaps
  3526. ===================
  3527. Emacs normally contains many keymaps; at any given time, just a few of
  3528. them are "active", meaning that they participate in the interpretation
  3529. of user input. All the active keymaps are used together to determine
  3530. what command to execute when a key is entered.
  3531. Normally the active keymaps are the `keymap' property keymap, the
  3532. keymaps of any enabled minor modes, the current buffer's local keymap,
  3533. and the global keymap, in that order. Emacs searches for each input
  3534. key sequence in all these keymaps. *Note Searching Keymaps::, for more
  3535. details of this procedure.
  3536. When the key sequence starts with a mouse event (optionally preceded
  3537. by a symbolic prefix), the active keymaps are determined based on the
  3538. position in that event. If the event happened on a string embedded
  3539. with a `display', `before-string', or `after-string' property (*note
  3540. Special Properties::), the non-`nil' map properties of the string
  3541. override those of the buffer (if the underlying buffer text contains
  3542. map properties in its text properties or overlays, they are ignored).
  3543. The "global keymap" holds the bindings of keys that are defined
  3544. regardless of the current buffer, such as `C-f'. The variable
  3545. `global-map' holds this keymap, which is always active.
  3546. Each buffer may have another keymap, its "local keymap", which may
  3547. contain new or overriding definitions for keys. The current buffer's
  3548. local keymap is always active except when `overriding-local-map'
  3549. overrides it. The `local-map' text or overlay property can specify an
  3550. alternative local keymap for certain parts of the buffer; see *note
  3551. Special Properties::.
  3552. Each minor mode can have a keymap; if it does, the keymap is active
  3553. when the minor mode is enabled. Modes for emulation can specify
  3554. additional active keymaps through the variable
  3555. `emulation-mode-map-alists'.
  3556. The highest precedence normal keymap comes from the `keymap' text or
  3557. overlay property. If that is non-`nil', it is the first keymap to be
  3558. processed, in normal circumstances.
  3559. However, there are also special ways for programs to substitute
  3560. other keymaps for some of those. The variable `overriding-local-map',
  3561. if non-`nil', specifies a keymap that replaces all the usual active
  3562. keymaps except the global keymap. Another way to do this is with
  3563. `overriding-terminal-local-map'; it operates on a per-terminal basis.
  3564. These variables are documented below.
  3565. Since every buffer that uses the same major mode normally uses the
  3566. same local keymap, you can think of the keymap as local to the mode. A
  3567. change to the local keymap of a buffer (using `local-set-key', for
  3568. example) is seen also in the other buffers that share that keymap.
  3569. The local keymaps that are used for Lisp mode and some other major
  3570. modes exist even if they have not yet been used. These local keymaps
  3571. are the values of variables such as `lisp-mode-map'. For most major
  3572. modes, which are less frequently used, the local keymap is constructed
  3573. only when the mode is used for the first time in a session.
  3574. The minibuffer has local keymaps, too; they contain various
  3575. completion and exit commands. *Note Intro to Minibuffers::.
  3576. Emacs has other keymaps that are used in a different way--translating
  3577. events within `read-key-sequence'. *Note Translation Keymaps::.
  3578. *Note Standard Keymaps::, for a list of some standard keymaps.
  3579. -- Function: current-active-maps &optional olp position
  3580. This returns the list of active keymaps that would be used by the
  3581. command loop in the current circumstances to look up a key
  3582. sequence. Normally it ignores `overriding-local-map' and
  3583. `overriding-terminal-local-map', but if OLP is non-`nil' then it
  3584. pays attention to them. POSITION can optionally be either an
  3585. event position as returned by `event-start' or a buffer position,
  3586. and may change the keymaps as described for `key-binding'.
  3587. -- Function: key-binding key &optional accept-defaults no-remap
  3588. position
  3589. This function returns the binding for KEY according to the current
  3590. active keymaps. The result is `nil' if KEY is undefined in the
  3591. keymaps.
  3592. The argument ACCEPT-DEFAULTS controls checking for default
  3593. bindings, as in `lookup-key' (*note Functions for Key Lookup::).
  3594. When commands are remapped (*note Remapping Commands::),
  3595. `key-binding' normally processes command remappings so as to
  3596. return the remapped command that will actually be executed.
  3597. However, if NO-REMAP is non-`nil', `key-binding' ignores
  3598. remappings and returns the binding directly specified for KEY.
  3599. If KEY starts with a mouse event (perhaps following a prefix
  3600. event), the maps to be consulted are determined based on the
  3601. event's position. Otherwise, they are determined based on the
  3602. value of point. However, you can override either of them by
  3603. specifying POSITION. If POSITION is non-`nil', it should be
  3604. either a buffer position or an event position like the value of
  3605. `event-start'. Then the maps consulted are determined based on
  3606. POSITION.
  3607. An error is signaled if KEY is not a string or a vector.
  3608. (key-binding "\C-x\C-f")
  3609. => find-file
  3610. 
  3611. File: elisp, Node: Searching Keymaps, Next: Controlling Active Maps, Prev: Active Keymaps, Up: Keymaps
  3612. 22.8 Searching the Active Keymaps
  3613. =================================
  3614. After translation of event subsequences (*note Translation Keymaps::)
  3615. Emacs looks for them in the active keymaps. Here is a pseudo-Lisp
  3616. description of the order and conditions for searching them:
  3617. (or (cond
  3618. (overriding-terminal-local-map
  3619. (FIND-IN overriding-terminal-local-map))
  3620. (overriding-local-map
  3621. (FIND-IN overriding-local-map))
  3622. ((or (FIND-IN (get-char-property (point) 'keymap))
  3623. (FIND-IN-ANY emulation-mode-map-alists)
  3624. (FIND-IN-ANY minor-mode-overriding-map-alist)
  3625. (FIND-IN-ANY minor-mode-map-alist)
  3626. (if (get-text-property (point) 'local-map)
  3627. (FIND-IN (get-char-property (point) 'local-map))
  3628. (FIND-IN (current-local-map))))))
  3629. (FIND-IN (current-global-map)))
  3630. FIND-IN and FIND-IN-ANY are pseudo functions that search in one keymap
  3631. and in an alist of keymaps, respectively. (Searching a single keymap
  3632. for a binding is called "key lookup"; see *note Key Lookup::.) If the
  3633. key sequence starts with a mouse event, or a symbolic prefix event
  3634. followed by a mouse event, that event's position is used instead of
  3635. point and the current buffer. Mouse events on an embedded string use
  3636. non-`nil' text properties from that string instead of the buffer.
  3637. When a match is found (*note Key Lookup::), if the binding in the
  3638. keymap is a function, the search is over. However if the keymap entry
  3639. is a symbol with a value or a string, Emacs replaces the input key
  3640. sequences with the variable's value or the string, and restarts the
  3641. search of the active keymaps.
  3642. The function finally found might also be remapped. *Note Remapping
  3643. Commands::.
  3644. 
  3645. File: elisp, Node: Controlling Active Maps, Next: Key Lookup, Prev: Searching Keymaps, Up: Keymaps
  3646. 22.9 Controlling the Active Keymaps
  3647. ===================================
  3648. -- Variable: global-map
  3649. This variable contains the default global keymap that maps Emacs
  3650. keyboard input to commands. The global keymap is normally this
  3651. keymap. The default global keymap is a full keymap that binds
  3652. `self-insert-command' to all of the printing characters.
  3653. It is normal practice to change the bindings in the global keymap,
  3654. but you should not assign this variable any value other than the
  3655. keymap it starts out with.
  3656. -- Function: current-global-map
  3657. This function returns the current global keymap. This is the same
  3658. as the value of `global-map' unless you change one or the other.
  3659. The return value is a reference, not a copy; if you use
  3660. `define-key' or other functions on it you will alter global
  3661. bindings.
  3662. (current-global-map)
  3663. => (keymap [set-mark-command beginning-of-line ...
  3664. delete-backward-char])
  3665. -- Function: current-local-map
  3666. This function returns the current buffer's local keymap, or `nil'
  3667. if it has none. In the following example, the keymap for the
  3668. `*scratch*' buffer (using Lisp Interaction mode) is a sparse keymap
  3669. in which the entry for <ESC>, ASCII code 27, is another sparse
  3670. keymap.
  3671. (current-local-map)
  3672. => (keymap
  3673. (10 . eval-print-last-sexp)
  3674. (9 . lisp-indent-line)
  3675. (127 . backward-delete-char-untabify)
  3676. (27 keymap
  3677. (24 . eval-defun)
  3678. (17 . indent-sexp)))
  3679. `current-local-map' returns a reference to the local keymap, not a
  3680. copy of it; if you use `define-key' or other functions on it you will
  3681. alter local bindings.
  3682. -- Function: current-minor-mode-maps
  3683. This function returns a list of the keymaps of currently enabled
  3684. minor modes.
  3685. -- Function: use-global-map keymap
  3686. This function makes KEYMAP the new current global keymap. It
  3687. returns `nil'.
  3688. It is very unusual to change the global keymap.
  3689. -- Function: use-local-map keymap
  3690. This function makes KEYMAP the new local keymap of the current
  3691. buffer. If KEYMAP is `nil', then the buffer has no local keymap.
  3692. `use-local-map' returns `nil'. Most major mode commands use this
  3693. function.
  3694. -- Variable: minor-mode-map-alist
  3695. This variable is an alist describing keymaps that may or may not be
  3696. active according to the values of certain variables. Its elements
  3697. look like this:
  3698. (VARIABLE . KEYMAP)
  3699. The keymap KEYMAP is active whenever VARIABLE has a non-`nil'
  3700. value. Typically VARIABLE is the variable that enables or
  3701. disables a minor mode. *Note Keymaps and Minor Modes::.
  3702. Note that elements of `minor-mode-map-alist' do not have the same
  3703. structure as elements of `minor-mode-alist'. The map must be the
  3704. CDR of the element; a list with the map as the second element will
  3705. not do. The CDR can be either a keymap (a list) or a symbol whose
  3706. function definition is a keymap.
  3707. When more than one minor mode keymap is active, the earlier one in
  3708. `minor-mode-map-alist' takes priority. But you should design
  3709. minor modes so that they don't interfere with each other. If you
  3710. do this properly, the order will not matter.
  3711. See *note Keymaps and Minor Modes::, for more information about
  3712. minor modes. See also `minor-mode-key-binding' (*note Functions
  3713. for Key Lookup::).
  3714. -- Variable: minor-mode-overriding-map-alist
  3715. This variable allows major modes to override the key bindings for
  3716. particular minor modes. The elements of this alist look like the
  3717. elements of `minor-mode-map-alist': `(VARIABLE . KEYMAP)'.
  3718. If a variable appears as an element of
  3719. `minor-mode-overriding-map-alist', the map specified by that
  3720. element totally replaces any map specified for the same variable in
  3721. `minor-mode-map-alist'.
  3722. `minor-mode-overriding-map-alist' is automatically buffer-local in
  3723. all buffers.
  3724. -- Variable: overriding-local-map
  3725. If non-`nil', this variable holds a keymap to use instead of the
  3726. buffer's local keymap, any text property or overlay keymaps, and
  3727. any minor mode keymaps. This keymap, if specified, overrides all
  3728. other maps that would have been active, except for the current
  3729. global map.
  3730. -- Variable: overriding-terminal-local-map
  3731. If non-`nil', this variable holds a keymap to use instead of
  3732. `overriding-local-map', the buffer's local keymap, text property
  3733. or overlay keymaps, and all the minor mode keymaps.
  3734. This variable is always local to the current terminal and cannot be
  3735. buffer-local. *Note Multiple Terminals::. It is used to implement
  3736. incremental search mode.
  3737. -- Variable: overriding-local-map-menu-flag
  3738. If this variable is non-`nil', the value of `overriding-local-map'
  3739. or `overriding-terminal-local-map' can affect the display of the
  3740. menu bar. The default value is `nil', so those map variables have
  3741. no effect on the menu bar.
  3742. Note that these two map variables do affect the execution of key
  3743. sequences entered using the menu bar, even if they do not affect
  3744. the menu bar display. So if a menu bar key sequence comes in, you
  3745. should clear the variables before looking up and executing that
  3746. key sequence. Modes that use the variables would typically do
  3747. this anyway; normally they respond to events that they do not
  3748. handle by "unreading" them and exiting.
  3749. -- Variable: special-event-map
  3750. This variable holds a keymap for special events. If an event type
  3751. has a binding in this keymap, then it is special, and the binding
  3752. for the event is run directly by `read-event'. *Note Special
  3753. Events::.
  3754. -- Variable: emulation-mode-map-alists
  3755. This variable holds a list of keymap alists to use for emulations
  3756. modes. It is intended for modes or packages using multiple
  3757. minor-mode keymaps. Each element is a keymap alist which has the
  3758. same format and meaning as `minor-mode-map-alist', or a symbol
  3759. with a variable binding which is such an alist. The "active"
  3760. keymaps in each alist are used before `minor-mode-map-alist' and
  3761. `minor-mode-overriding-map-alist'.
  3762. 
  3763. File: elisp, Node: Key Lookup, Next: Functions for Key Lookup, Prev: Controlling Active Maps, Up: Keymaps
  3764. 22.10 Key Lookup
  3765. ================
  3766. "Key lookup" is the process of finding the binding of a key sequence
  3767. from a given keymap. The execution or use of the binding is not part
  3768. of key lookup.
  3769. Key lookup uses just the event type of each event in the key
  3770. sequence; the rest of the event is ignored. In fact, a key sequence
  3771. used for key lookup may designate a mouse event with just its types (a
  3772. symbol) instead of the entire event (a list). *Note Input Events::.
  3773. Such a "key sequence" is insufficient for `command-execute' to run, but
  3774. it is sufficient for looking up or rebinding a key.
  3775. When the key sequence consists of multiple events, key lookup
  3776. processes the events sequentially: the binding of the first event is
  3777. found, and must be a keymap; then the second event's binding is found in
  3778. that keymap, and so on until all the events in the key sequence are used
  3779. up. (The binding thus found for the last event may or may not be a
  3780. keymap.) Thus, the process of key lookup is defined in terms of a
  3781. simpler process for looking up a single event in a keymap. How that is
  3782. done depends on the type of object associated with the event in that
  3783. keymap.
  3784. Let's use the term "keymap entry" to describe the value found by
  3785. looking up an event type in a keymap. (This doesn't include the item
  3786. string and other extra elements in a keymap element for a menu item,
  3787. because `lookup-key' and other key lookup functions don't include them
  3788. in the returned value.) While any Lisp object may be stored in a keymap
  3789. as a keymap entry, not all make sense for key lookup. Here is a table
  3790. of the meaningful types of keymap entries:
  3791. `nil'
  3792. `nil' means that the events used so far in the lookup form an
  3793. undefined key. When a keymap fails to mention an event type at
  3794. all, and has no default binding, that is equivalent to a binding
  3795. of `nil' for that event type.
  3796. COMMAND
  3797. The events used so far in the lookup form a complete key, and
  3798. COMMAND is its binding. *Note What Is a Function::.
  3799. ARRAY
  3800. The array (either a string or a vector) is a keyboard macro. The
  3801. events used so far in the lookup form a complete key, and the
  3802. array is its binding. See *note Keyboard Macros::, for more
  3803. information.
  3804. KEYMAP
  3805. The events used so far in the lookup form a prefix key. The next
  3806. event of the key sequence is looked up in KEYMAP.
  3807. LIST
  3808. The meaning of a list depends on what it contains:
  3809. * If the CAR of LIST is the symbol `keymap', then the list is a
  3810. keymap, and is treated as a keymap (see above).
  3811. * If the CAR of LIST is `lambda', then the list is a lambda
  3812. expression. This is presumed to be a function, and is treated
  3813. as such (see above). In order to execute properly as a key
  3814. binding, this function must be a command--it must have an
  3815. `interactive' specification. *Note Defining Commands::.
  3816. * If the CAR of LIST is a keymap and the CDR is an event type,
  3817. then this is an "indirect entry":
  3818. (OTHERMAP . OTHERTYPE)
  3819. When key lookup encounters an indirect entry, it looks up
  3820. instead the binding of OTHERTYPE in OTHERMAP and uses that.
  3821. This feature permits you to define one key as an alias for
  3822. another key. For example, an entry whose CAR is the keymap
  3823. called `esc-map' and whose CDR is 32 (the code for <SPC>)
  3824. means, "Use the global binding of `Meta-<SPC>', whatever that
  3825. may be".
  3826. SYMBOL
  3827. The function definition of SYMBOL is used in place of SYMBOL. If
  3828. that too is a symbol, then this process is repeated, any number of
  3829. times. Ultimately this should lead to an object that is a keymap,
  3830. a command, or a keyboard macro. A list is allowed if it is a
  3831. keymap or a command, but indirect entries are not understood when
  3832. found via symbols.
  3833. Note that keymaps and keyboard macros (strings and vectors) are not
  3834. valid functions, so a symbol with a keymap, string, or vector as
  3835. its function definition is invalid as a function. It is, however,
  3836. valid as a key binding. If the definition is a keyboard macro,
  3837. then the symbol is also valid as an argument to `command-execute'
  3838. (*note Interactive Call::).
  3839. The symbol `undefined' is worth special mention: it means to treat
  3840. the key as undefined. Strictly speaking, the key is defined, and
  3841. its binding is the command `undefined'; but that command does the
  3842. same thing that is done automatically for an undefined key: it
  3843. rings the bell (by calling `ding') but does not signal an error.
  3844. `undefined' is used in local keymaps to override a global key
  3845. binding and make the key "undefined" locally. A local binding of
  3846. `nil' would fail to do this because it would not override the
  3847. global binding.
  3848. ANYTHING ELSE
  3849. If any other type of object is found, the events used so far in the
  3850. lookup form a complete key, and the object is its binding, but the
  3851. binding is not executable as a command.
  3852. In short, a keymap entry may be a keymap, a command, a keyboard
  3853. macro, a symbol that leads to one of them, or an indirection or `nil'.
  3854. 
  3855. File: elisp, Node: Functions for Key Lookup, Next: Changing Key Bindings, Prev: Key Lookup, Up: Keymaps
  3856. 22.11 Functions for Key Lookup
  3857. ==============================
  3858. Here are the functions and variables pertaining to key lookup.
  3859. -- Function: lookup-key keymap key &optional accept-defaults
  3860. This function returns the definition of KEY in KEYMAP. All the
  3861. other functions described in this chapter that look up keys use
  3862. `lookup-key'. Here are examples:
  3863. (lookup-key (current-global-map) "\C-x\C-f")
  3864. => find-file
  3865. (lookup-key (current-global-map) (kbd "C-x C-f"))
  3866. => find-file
  3867. (lookup-key (current-global-map) "\C-x\C-f12345")
  3868. => 2
  3869. If the string or vector KEY is not a valid key sequence according
  3870. to the prefix keys specified in KEYMAP, it must be "too long" and
  3871. have extra events at the end that do not fit into a single key
  3872. sequence. Then the value is a number, the number of events at the
  3873. front of KEY that compose a complete key.
  3874. If ACCEPT-DEFAULTS is non-`nil', then `lookup-key' considers
  3875. default bindings as well as bindings for the specific events in
  3876. KEY. Otherwise, `lookup-key' reports only bindings for the
  3877. specific sequence KEY, ignoring default bindings except when you
  3878. explicitly ask about them. (To do this, supply `t' as an element
  3879. of KEY; see *note Format of Keymaps::.)
  3880. If KEY contains a meta character (not a function key), that
  3881. character is implicitly replaced by a two-character sequence: the
  3882. value of `meta-prefix-char', followed by the corresponding non-meta
  3883. character. Thus, the first example below is handled by conversion
  3884. into the second example.
  3885. (lookup-key (current-global-map) "\M-f")
  3886. => forward-word
  3887. (lookup-key (current-global-map) "\ef")
  3888. => forward-word
  3889. Unlike `read-key-sequence', this function does not modify the
  3890. specified events in ways that discard information (*note Key
  3891. Sequence Input::). In particular, it does not convert letters to
  3892. lower case and it does not change drag events to clicks.
  3893. -- Command: undefined
  3894. Used in keymaps to undefine keys. It calls `ding', but does not
  3895. cause an error.
  3896. -- Function: local-key-binding key &optional accept-defaults
  3897. This function returns the binding for KEY in the current local
  3898. keymap, or `nil' if it is undefined there.
  3899. The argument ACCEPT-DEFAULTS controls checking for default
  3900. bindings, as in `lookup-key' (above).
  3901. -- Function: global-key-binding key &optional accept-defaults
  3902. This function returns the binding for command KEY in the current
  3903. global keymap, or `nil' if it is undefined there.
  3904. The argument ACCEPT-DEFAULTS controls checking for default
  3905. bindings, as in `lookup-key' (above).
  3906. -- Function: minor-mode-key-binding key &optional accept-defaults
  3907. This function returns a list of all the active minor mode bindings
  3908. of KEY. More precisely, it returns an alist of pairs `(MODENAME .
  3909. BINDING)', where MODENAME is the variable that enables the minor
  3910. mode, and BINDING is KEY's binding in that mode. If KEY has no
  3911. minor-mode bindings, the value is `nil'.
  3912. If the first binding found is not a prefix definition (a keymap or
  3913. a symbol defined as a keymap), all subsequent bindings from other
  3914. minor modes are omitted, since they would be completely shadowed.
  3915. Similarly, the list omits non-prefix bindings that follow prefix
  3916. bindings.
  3917. The argument ACCEPT-DEFAULTS controls checking for default
  3918. bindings, as in `lookup-key' (above).
  3919. -- User Option: meta-prefix-char
  3920. This variable is the meta-prefix character code. It is used for
  3921. translating a meta character to a two-character sequence so it can
  3922. be looked up in a keymap. For useful results, the value should be
  3923. a prefix event (*note Prefix Keys::). The default value is 27,
  3924. which is the ASCII code for <ESC>.
  3925. As long as the value of `meta-prefix-char' remains 27, key lookup
  3926. translates `M-b' into `<ESC> b', which is normally defined as the
  3927. `backward-word' command. However, if you were to set
  3928. `meta-prefix-char' to 24, the code for `C-x', then Emacs will
  3929. translate `M-b' into `C-x b', whose standard binding is the
  3930. `switch-to-buffer' command. (Don't actually do this!) Here is an
  3931. illustration of what would happen:
  3932. meta-prefix-char ; The default value.
  3933. => 27
  3934. (key-binding "\M-b")
  3935. => backward-word
  3936. ?\C-x ; The print representation
  3937. => 24 ; of a character.
  3938. (setq meta-prefix-char 24)
  3939. => 24
  3940. (key-binding "\M-b")
  3941. => switch-to-buffer ; Now, typing `M-b' is
  3942. ; like typing `C-x b'.
  3943. (setq meta-prefix-char 27) ; Avoid confusion!
  3944. => 27 ; Restore the default value!
  3945. This translation of one event into two happens only for
  3946. characters, not for other kinds of input events. Thus, `M-<F1>',
  3947. a function key, is not converted into `<ESC> <F1>'.
  3948. 
  3949. File: elisp, Node: Changing Key Bindings, Next: Remapping Commands, Prev: Functions for Key Lookup, Up: Keymaps
  3950. 22.12 Changing Key Bindings
  3951. ===========================
  3952. The way to rebind a key is to change its entry in a keymap. If you
  3953. change a binding in the global keymap, the change is effective in all
  3954. buffers (though it has no direct effect in buffers that shadow the
  3955. global binding with a local one). If you change the current buffer's
  3956. local map, that usually affects all buffers using the same major mode.
  3957. The `global-set-key' and `local-set-key' functions are convenient
  3958. interfaces for these operations (*note Key Binding Commands::). You
  3959. can also use `define-key', a more general function; then you must
  3960. explicitly specify the map to change.
  3961. When choosing the key sequences for Lisp programs to rebind, please
  3962. follow the Emacs conventions for use of various keys (*note Key Binding
  3963. Conventions::).
  3964. In writing the key sequence to rebind, it is good to use the special
  3965. escape sequences for control and meta characters (*note String Type::).
  3966. The syntax `\C-' means that the following character is a control
  3967. character and `\M-' means that the following character is a meta
  3968. character. Thus, the string `"\M-x"' is read as containing a single
  3969. `M-x', `"\C-f"' is read as containing a single `C-f', and `"\M-\C-x"'
  3970. and `"\C-\M-x"' are both read as containing a single `C-M-x'. You can
  3971. also use this escape syntax in vectors, as well as others that aren't
  3972. allowed in strings; one example is `[?\C-\H-x home]'. *Note Character
  3973. Type::.
  3974. The key definition and lookup functions accept an alternate syntax
  3975. for event types in a key sequence that is a vector: you can use a list
  3976. containing modifier names plus one base event (a character or function
  3977. key name). For example, `(control ?a)' is equivalent to `?\C-a' and
  3978. `(hyper control left)' is equivalent to `C-H-left'. One advantage of
  3979. such lists is that the precise numeric codes for the modifier bits
  3980. don't appear in compiled files.
  3981. The functions below signal an error if KEYMAP is not a keymap, or if
  3982. KEY is not a string or vector representing a key sequence. You can use
  3983. event types (symbols) as shorthand for events that are lists. The
  3984. `kbd' macro (*note Key Sequences::) is a convenient way to specify the
  3985. key sequence.
  3986. -- Function: define-key keymap key binding
  3987. This function sets the binding for KEY in KEYMAP. (If KEY is more
  3988. than one event long, the change is actually made in another keymap
  3989. reached from KEYMAP.) The argument BINDING can be any Lisp
  3990. object, but only certain types are meaningful. (For a list of
  3991. meaningful types, see *note Key Lookup::.) The value returned by
  3992. `define-key' is BINDING.
  3993. If KEY is `[t]', this sets the default binding in KEYMAP. When an
  3994. event has no binding of its own, the Emacs command loop uses the
  3995. keymap's default binding, if there is one.
  3996. Every prefix of KEY must be a prefix key (i.e., bound to a keymap)
  3997. or undefined; otherwise an error is signaled. If some prefix of
  3998. KEY is undefined, then `define-key' defines it as a prefix key so
  3999. that the rest of KEY can be defined as specified.
  4000. If there was previously no binding for KEY in KEYMAP, the new
  4001. binding is added at the beginning of KEYMAP. The order of
  4002. bindings in a keymap makes no difference for keyboard input, but it
  4003. does matter for menu keymaps (*note Menu Keymaps::).
  4004. This example creates a sparse keymap and makes a number of bindings
  4005. in it:
  4006. (setq map (make-sparse-keymap))
  4007. => (keymap)
  4008. (define-key map "\C-f" 'forward-char)
  4009. => forward-char
  4010. map
  4011. => (keymap (6 . forward-char))
  4012. ;; Build sparse submap for `C-x' and bind `f' in that.
  4013. (define-key map (kbd "C-x f") 'forward-word)
  4014. => forward-word
  4015. map
  4016. => (keymap
  4017. (24 keymap ; C-x
  4018. (102 . forward-word)) ; f
  4019. (6 . forward-char)) ; C-f
  4020. ;; Bind `C-p' to the `ctl-x-map'.
  4021. (define-key map (kbd "C-p") ctl-x-map)
  4022. ;; `ctl-x-map'
  4023. => [nil ... find-file ... backward-kill-sentence]
  4024. ;; Bind `C-f' to `foo' in the `ctl-x-map'.
  4025. (define-key map (kbd "C-p C-f") 'foo)
  4026. => 'foo
  4027. map
  4028. => (keymap ; Note `foo' in `ctl-x-map'.
  4029. (16 keymap [nil ... foo ... backward-kill-sentence])
  4030. (24 keymap
  4031. (102 . forward-word))
  4032. (6 . forward-char))
  4033. Note that storing a new binding for `C-p C-f' actually works by
  4034. changing an entry in `ctl-x-map', and this has the effect of changing
  4035. the bindings of both `C-p C-f' and `C-x C-f' in the default global map.
  4036. The function `substitute-key-definition' scans a keymap for keys
  4037. that have a certain binding and rebinds them with a different binding.
  4038. Another feature which is cleaner and can often produce the same results
  4039. to remap one command into another (*note Remapping Commands::).
  4040. -- Function: substitute-key-definition olddef newdef keymap &optional
  4041. oldmap
  4042. This function replaces OLDDEF with NEWDEF for any keys in KEYMAP
  4043. that were bound to OLDDEF. In other words, OLDDEF is replaced
  4044. with NEWDEF wherever it appears. The function returns `nil'.
  4045. For example, this redefines `C-x C-f', if you do it in an Emacs
  4046. with standard bindings:
  4047. (substitute-key-definition
  4048. 'find-file 'find-file-read-only (current-global-map))
  4049. If OLDMAP is non-`nil', that changes the behavior of
  4050. `substitute-key-definition': the bindings in OLDMAP determine
  4051. which keys to rebind. The rebindings still happen in KEYMAP, not
  4052. in OLDMAP. Thus, you can change one map under the control of the
  4053. bindings in another. For example,
  4054. (substitute-key-definition
  4055. 'delete-backward-char 'my-funny-delete
  4056. my-map global-map)
  4057. puts the special deletion command in `my-map' for whichever keys
  4058. are globally bound to the standard deletion command.
  4059. Here is an example showing a keymap before and after substitution:
  4060. (setq map '(keymap
  4061. (?1 . olddef-1)
  4062. (?2 . olddef-2)
  4063. (?3 . olddef-1)))
  4064. => (keymap (49 . olddef-1) (50 . olddef-2) (51 . olddef-1))
  4065. (substitute-key-definition 'olddef-1 'newdef map)
  4066. => nil
  4067. map
  4068. => (keymap (49 . newdef) (50 . olddef-2) (51 . newdef))
  4069. -- Function: suppress-keymap keymap &optional nodigits
  4070. This function changes the contents of the full keymap KEYMAP by
  4071. remapping `self-insert-command' to the command `undefined' (*note
  4072. Remapping Commands::). This has the effect of undefining all
  4073. printing characters, thus making ordinary insertion of text
  4074. impossible. `suppress-keymap' returns `nil'.
  4075. If NODIGITS is `nil', then `suppress-keymap' defines digits to run
  4076. `digit-argument', and `-' to run `negative-argument'. Otherwise
  4077. it makes them undefined like the rest of the printing characters.
  4078. The `suppress-keymap' function does not make it impossible to
  4079. modify a buffer, as it does not suppress commands such as `yank'
  4080. and `quoted-insert'. To prevent any modification of a buffer, make
  4081. it read-only (*note Read Only Buffers::).
  4082. Since this function modifies KEYMAP, you would normally use it on
  4083. a newly created keymap. Operating on an existing keymap that is
  4084. used for some other purpose is likely to cause trouble; for
  4085. example, suppressing `global-map' would make it impossible to use
  4086. most of Emacs.
  4087. This function can be used to initialize the local keymap of a major
  4088. mode for which insertion of text is not desirable. But usually
  4089. such a mode should be derived from `special-mode' (*note Basic
  4090. Major Modes::); then its keymap will automatically inherit from
  4091. `special-mode-map', which is already suppressed. Here is how
  4092. `special-mode-map' is defined:
  4093. (defvar special-mode-map
  4094. (let ((map (make-sparse-keymap)))
  4095. (suppress-keymap map)
  4096. (define-key map "q" 'quit-window)
  4097. ...
  4098. map))
  4099. 
  4100. File: elisp, Node: Remapping Commands, Next: Translation Keymaps, Prev: Changing Key Bindings, Up: Keymaps
  4101. 22.13 Remapping Commands
  4102. ========================
  4103. A special kind of key binding can be used to "remap" one command to
  4104. another, without having to refer to the key sequence(s) bound to the
  4105. original command. To use this feature, make a key binding for a key
  4106. sequence that starts with the dummy event `remap', followed by the
  4107. command name you want to remap; for the binding, specify the new
  4108. definition (usually a command name, but possibly any other valid
  4109. definition for a key binding).
  4110. For example, suppose My mode provides a special command
  4111. `my-kill-line', which should be invoked instead of `kill-line'. To
  4112. establish this, its mode keymap should contain the following remapping:
  4113. (define-key my-mode-map [remap kill-line] 'my-kill-line)
  4114. Then, whenever `my-mode-map' is active, if the user types `C-k' (the
  4115. default global key sequence for `kill-line') Emacs will instead run
  4116. `my-kill-line'.
  4117. Note that remapping only takes place through active keymaps; for
  4118. example, putting a remapping in a prefix keymap like `ctl-x-map'
  4119. typically has no effect, as such keymaps are not themselves active. In
  4120. addition, remapping only works through a single level; in the following
  4121. example,
  4122. (define-key my-mode-map [remap kill-line] 'my-kill-line)
  4123. (define-key my-mode-map [remap my-kill-line] 'my-other-kill-line)
  4124. `kill-line' is _not_ remapped to `my-other-kill-line'. Instead, if an
  4125. ordinary key binding specifies `kill-line', it is remapped to
  4126. `my-kill-line'; if an ordinary binding specifies `my-kill-line', it is
  4127. remapped to `my-other-kill-line'.
  4128. To undo the remapping of a command, remap it to `nil'; e.g.
  4129. (define-key my-mode-map [remap kill-line] nil)
  4130. -- Function: command-remapping command &optional position keymaps
  4131. This function returns the remapping for COMMAND (a symbol), given
  4132. the current active keymaps. If COMMAND is not remapped (which is
  4133. the usual situation), or not a symbol, the function returns `nil'.
  4134. `position' can optionally specify a buffer position or an event
  4135. position to determine the keymaps to use, as in `key-binding'.
  4136. If the optional argument `keymaps' is non-`nil', it specifies a
  4137. list of keymaps to search in. This argument is ignored if
  4138. `position' is non-`nil'.
  4139. 
  4140. File: elisp, Node: Translation Keymaps, Next: Key Binding Commands, Prev: Remapping Commands, Up: Keymaps
  4141. 22.14 Keymaps for Translating Sequences of Events
  4142. =================================================
  4143. This section describes keymaps that are used during reading a key
  4144. sequence, to translate certain event sequences into others.
  4145. `read-key-sequence' checks every subsequence of the key sequence being
  4146. read, as it is read, against `input-decode-map', then
  4147. `local-function-key-map', and then against `key-translation-map'.
  4148. -- Variable: input-decode-map
  4149. This variable holds a keymap that describes the character
  4150. sequences sent by function keys on an ordinary character terminal.
  4151. This keymap has the same structure as other keymaps, but is used
  4152. differently: it specifies translations to make while reading key
  4153. sequences, rather than bindings for key sequences.
  4154. If `input-decode-map' "binds" a key sequence K to a vector V, then
  4155. when K appears as a subsequence _anywhere_ in a key sequence, it
  4156. is replaced with the events in V.
  4157. For example, VT100 terminals send `<ESC> O P' when the keypad
  4158. <PF1> key is pressed. Therefore, we want Emacs to translate that
  4159. sequence of events into the single event `pf1'. We accomplish
  4160. this by "binding" `<ESC> O P' to `[pf1]' in `input-decode-map',
  4161. when using a VT100.
  4162. Thus, typing `C-c <PF1>' sends the character sequence `C-c <ESC> O
  4163. P'; later the function `read-key-sequence' translates this back
  4164. into `C-c <PF1>', which it returns as the vector `[?\C-c pf1]'.
  4165. The value of `input-decode-map' is usually set up automatically
  4166. according to the terminal's Terminfo or Termcap entry, but
  4167. sometimes those need help from terminal-specific Lisp files.
  4168. Emacs comes with terminal-specific files for many common
  4169. terminals; their main purpose is to make entries in
  4170. `input-decode-map' beyond those that can be deduced from Termcap
  4171. and Terminfo. *Note Terminal-Specific::.
  4172. -- Variable: local-function-key-map
  4173. This variable holds a keymap similar to `input-decode-map' except
  4174. that it describes key sequences which should be translated to
  4175. alternative interpretations that are usually preferred. It applies
  4176. after `input-decode-map' and before `key-translation-map'.
  4177. Entries in `local-function-key-map' are ignored if they conflict
  4178. with bindings made in the minor mode, local, or global keymaps.
  4179. I.e. the remapping only applies if the original key sequence would
  4180. otherwise not have any binding.
  4181. `local-function-key-map' inherits from `function-key-map', but the
  4182. latter should not be used directly.
  4183. -- Variable: key-translation-map
  4184. This variable is another keymap used just like `input-decode-map'
  4185. to translate input events into other events. It differs from
  4186. `input-decode-map' in that it goes to work after
  4187. `local-function-key-map' is finished rather than before; it
  4188. receives the results of translation by `local-function-key-map'.
  4189. Just like `input-decode-map', but unlike `local-function-key-map',
  4190. this keymap is applied regardless of whether the input
  4191. key-sequence has a normal binding. Note however that actual key
  4192. bindings can have an effect on `key-translation-map', even though
  4193. they are overridden by it. Indeed, actual key bindings override
  4194. `local-function-key-map' and thus may alter the key sequence that
  4195. `key-translation-map' receives. Clearly, it is better to avoid
  4196. this type of situation.
  4197. The intent of `key-translation-map' is for users to map one
  4198. character set to another, including ordinary characters normally
  4199. bound to `self-insert-command'.
  4200. You can use `input-decode-map', `local-function-key-map', and
  4201. `key-translation-map' for more than simple aliases, by using a
  4202. function, instead of a key sequence, as the "translation" of a key.
  4203. Then this function is called to compute the translation of that key.
  4204. The key translation function receives one argument, which is the
  4205. prompt that was specified in `read-key-sequence'--or `nil' if the key
  4206. sequence is being read by the editor command loop. In most cases you
  4207. can ignore the prompt value.
  4208. If the function reads input itself, it can have the effect of
  4209. altering the event that follows. For example, here's how to define
  4210. `C-c h' to turn the character that follows into a Hyper character:
  4211. (defun hyperify (prompt)
  4212. (let ((e (read-event)))
  4213. (vector (if (numberp e)
  4214. (logior (lsh 1 24) e)
  4215. (if (memq 'hyper (event-modifiers e))
  4216. e
  4217. (add-event-modifier "H-" e))))))
  4218. (defun add-event-modifier (string e)
  4219. (let ((symbol (if (symbolp e) e (car e))))
  4220. (setq symbol (intern (concat string
  4221. (symbol-name symbol))))
  4222. (if (symbolp e)
  4223. symbol
  4224. (cons symbol (cdr e)))))
  4225. (define-key local-function-key-map "\C-ch" 'hyperify)
  4226. If you have enabled keyboard character set decoding using
  4227. `set-keyboard-coding-system', decoding is done after the translations
  4228. listed above. *Note Terminal I/O Encoding::. However, in future Emacs
  4229. versions, character set decoding may be done at an earlier stage.
  4230. 
  4231. File: elisp, Node: Key Binding Commands, Next: Scanning Keymaps, Prev: Translation Keymaps, Up: Keymaps
  4232. 22.15 Commands for Binding Keys
  4233. ===============================
  4234. This section describes some convenient interactive interfaces for
  4235. changing key bindings. They work by calling `define-key'.
  4236. People often use `global-set-key' in their init files (*note Init
  4237. File::) for simple customization. For example,
  4238. (global-set-key (kbd "C-x C-\\") 'next-line)
  4239. or
  4240. (global-set-key [?\C-x ?\C-\\] 'next-line)
  4241. or
  4242. (global-set-key [(control ?x) (control ?\\)] 'next-line)
  4243. redefines `C-x C-\' to move down a line.
  4244. (global-set-key [M-mouse-1] 'mouse-set-point)
  4245. redefines the first (leftmost) mouse button, entered with the Meta key,
  4246. to set point where you click.
  4247. Be careful when using non-ASCII text characters in Lisp
  4248. specifications of keys to bind. If these are read as multibyte text, as
  4249. they usually will be in a Lisp file (*note Loading Non-ASCII::), you
  4250. must type the keys as multibyte too. For instance, if you use this:
  4251. (global-set-key "ö" 'my-function) ; bind o-umlaut
  4252. or
  4253. (global-set-key ?ö 'my-function) ; bind o-umlaut
  4254. and your language environment is multibyte Latin-1, these commands
  4255. actually bind the multibyte character with code 246, not the byte code
  4256. 246 (`M-v') sent by a Latin-1 terminal. In order to use this binding,
  4257. you need to teach Emacs how to decode the keyboard by using an
  4258. appropriate input method (*note Input Methods: (emacs)Input Methods.).
  4259. -- Command: global-set-key key binding
  4260. This function sets the binding of KEY in the current global map to
  4261. BINDING.
  4262. (global-set-key KEY BINDING)
  4263. ==
  4264. (define-key (current-global-map) KEY BINDING)
  4265. -- Command: global-unset-key key
  4266. This function removes the binding of KEY from the current global
  4267. map.
  4268. One use of this function is in preparation for defining a longer
  4269. key that uses KEY as a prefix--which would not be allowed if KEY
  4270. has a non-prefix binding. For example:
  4271. (global-unset-key "\C-l")
  4272. => nil
  4273. (global-set-key "\C-l\C-l" 'redraw-display)
  4274. => nil
  4275. This function is implemented simply using `define-key':
  4276. (global-unset-key KEY)
  4277. ==
  4278. (define-key (current-global-map) KEY nil)
  4279. -- Command: local-set-key key binding
  4280. This function sets the binding of KEY in the current local keymap
  4281. to BINDING.
  4282. (local-set-key KEY BINDING)
  4283. ==
  4284. (define-key (current-local-map) KEY BINDING)
  4285. -- Command: local-unset-key key
  4286. This function removes the binding of KEY from the current local
  4287. map.
  4288. (local-unset-key KEY)
  4289. ==
  4290. (define-key (current-local-map) KEY nil)
  4291. 
  4292. File: elisp, Node: Scanning Keymaps, Next: Menu Keymaps, Prev: Key Binding Commands, Up: Keymaps
  4293. 22.16 Scanning Keymaps
  4294. ======================
  4295. This section describes functions used to scan all the current keymaps
  4296. for the sake of printing help information.
  4297. -- Function: accessible-keymaps keymap &optional prefix
  4298. This function returns a list of all the keymaps that can be
  4299. reached (via zero or more prefix keys) from KEYMAP. The value is
  4300. an association list with elements of the form `(KEY . MAP)',
  4301. where KEY is a prefix key whose definition in KEYMAP is MAP.
  4302. The elements of the alist are ordered so that the KEY increases in
  4303. length. The first element is always `([] . KEYMAP)', because the
  4304. specified keymap is accessible from itself with a prefix of no
  4305. events.
  4306. If PREFIX is given, it should be a prefix key sequence; then
  4307. `accessible-keymaps' includes only the submaps whose prefixes start
  4308. with PREFIX. These elements look just as they do in the value of
  4309. `(accessible-keymaps)'; the only difference is that some elements
  4310. are omitted.
  4311. In the example below, the returned alist indicates that the key
  4312. <ESC>, which is displayed as `^[', is a prefix key whose
  4313. definition is the sparse keymap `(keymap (83 . center-paragraph)
  4314. (115 . foo))'.
  4315. (accessible-keymaps (current-local-map))
  4316. =>(([] keymap
  4317. (27 keymap ; Note this keymap for <ESC> is repeated below.
  4318. (83 . center-paragraph)
  4319. (115 . center-line))
  4320. (9 . tab-to-tab-stop))
  4321. ("^[" keymap
  4322. (83 . center-paragraph)
  4323. (115 . foo)))
  4324. In the following example, `C-h' is a prefix key that uses a sparse
  4325. keymap starting with `(keymap (118 . describe-variable)...)'.
  4326. Another prefix, `C-x 4', uses a keymap which is also the value of
  4327. the variable `ctl-x-4-map'. The event `mode-line' is one of
  4328. several dummy events used as prefixes for mouse actions in special
  4329. parts of a window.
  4330. (accessible-keymaps (current-global-map))
  4331. => (([] keymap [set-mark-command beginning-of-line ...
  4332. delete-backward-char])
  4333. ("^H" keymap (118 . describe-variable) ...
  4334. (8 . help-for-help))
  4335. ("^X" keymap [x-flush-mouse-queue ...
  4336. backward-kill-sentence])
  4337. ("^[" keymap [mark-sexp backward-sexp ...
  4338. backward-kill-word])
  4339. ("^X4" keymap (15 . display-buffer) ...)
  4340. ([mode-line] keymap
  4341. (S-mouse-2 . mouse-split-window-horizontally) ...))
  4342. These are not all the keymaps you would see in actuality.
  4343. -- Function: map-keymap function keymap
  4344. The function `map-keymap' calls FUNCTION once for each binding in
  4345. KEYMAP. It passes two arguments, the event type and the value of
  4346. the binding. If KEYMAP has a parent, the parent's bindings are
  4347. included as well. This works recursively: if the parent has
  4348. itself a parent, then the grandparent's bindings are also included
  4349. and so on.
  4350. This function is the cleanest way to examine all the bindings in a
  4351. keymap.
  4352. -- Function: where-is-internal command &optional keymap firstonly
  4353. noindirect no-remap
  4354. This function is a subroutine used by the `where-is' command
  4355. (*note Help: (emacs)Help.). It returns a list of all key
  4356. sequences (of any length) that are bound to COMMAND in a set of
  4357. keymaps.
  4358. The argument COMMAND can be any object; it is compared with all
  4359. keymap entries using `eq'.
  4360. If KEYMAP is `nil', then the maps used are the current active
  4361. keymaps, disregarding `overriding-local-map' (that is, pretending
  4362. its value is `nil'). If KEYMAP is a keymap, then the maps
  4363. searched are KEYMAP and the global keymap. If KEYMAP is a list of
  4364. keymaps, only those keymaps are searched.
  4365. Usually it's best to use `overriding-local-map' as the expression
  4366. for KEYMAP. Then `where-is-internal' searches precisely the
  4367. keymaps that are active. To search only the global map, pass the
  4368. value `(keymap)' (an empty keymap) as KEYMAP.
  4369. If FIRSTONLY is `non-ascii', then the value is a single vector
  4370. representing the first key sequence found, rather than a list of
  4371. all possible key sequences. If FIRSTONLY is `t', then the value
  4372. is the first key sequence, except that key sequences consisting
  4373. entirely of ASCII characters (or meta variants of ASCII
  4374. characters) are preferred to all other key sequences and that the
  4375. return value can never be a menu binding.
  4376. If NOINDIRECT is non-`nil', `where-is-internal' doesn't follow
  4377. indirect keymap bindings. This makes it possible to search for an
  4378. indirect definition itself.
  4379. The fifth argument, NO-REMAP, determines how this function treats
  4380. command remappings (*note Remapping Commands::). There are two
  4381. cases of interest:
  4382. If a command OTHER-COMMAND is remapped to COMMAND:
  4383. If NO-REMAP is `nil', find the bindings for OTHER-COMMAND and
  4384. treat them as though they are also bindings for COMMAND. If
  4385. NO-REMAP is non-`nil', include the vector `[remap
  4386. OTHER-COMMAND]' in the list of possible key sequences,
  4387. instead of finding those bindings.
  4388. If COMMAND is remapped to OTHER-COMMAND:
  4389. If NO-REMAP is `nil', return the bindings for OTHER-COMMAND
  4390. rather than COMMAND. If NO-REMAP is non-`nil', return the
  4391. bindings for COMMAND, ignoring the fact that it is remapped.
  4392. -- Command: describe-bindings &optional prefix buffer-or-name
  4393. This function creates a listing of all current key bindings, and
  4394. displays it in a buffer named `*Help*'. The text is grouped by
  4395. modes--minor modes first, then the major mode, then global
  4396. bindings.
  4397. If PREFIX is non-`nil', it should be a prefix key; then the
  4398. listing includes only keys that start with PREFIX.
  4399. The listing describes meta characters as <ESC> followed by the
  4400. corresponding non-meta character.
  4401. When several characters with consecutive ASCII codes have the same
  4402. definition, they are shown together, as `FIRSTCHAR..LASTCHAR'. In
  4403. this instance, you need to know the ASCII codes to understand
  4404. which characters this means. For example, in the default global
  4405. map, the characters `<SPC> .. ~' are described by a single line.
  4406. <SPC> is ASCII 32, `~' is ASCII 126, and the characters between
  4407. them include all the normal printing characters, (e.g., letters,
  4408. digits, punctuation, etc.); all these characters are bound to
  4409. `self-insert-command'.
  4410. If BUFFER-OR-NAME is non-`nil', it should be a buffer or a buffer
  4411. name. Then `describe-bindings' lists that buffer's bindings,
  4412. instead of the current buffer's.
  4413. 
  4414. File: elisp, Node: Menu Keymaps, Prev: Scanning Keymaps, Up: Keymaps
  4415. 22.17 Menu Keymaps
  4416. ==================
  4417. A keymap can operate as a menu as well as defining bindings for
  4418. keyboard keys and mouse buttons. Menus are usually actuated with the
  4419. mouse, but they can function with the keyboard also. If a menu keymap
  4420. is active for the next input event, that activates the keyboard menu
  4421. feature.
  4422. * Menu:
  4423. * Defining Menus:: How to make a keymap that defines a menu.
  4424. * Mouse Menus:: How users actuate the menu with the mouse.
  4425. * Keyboard Menus:: How users actuate the menu with the keyboard.
  4426. * Menu Example:: Making a simple menu.
  4427. * Menu Bar:: How to customize the menu bar.
  4428. * Tool Bar:: A tool bar is a row of images.
  4429. * Modifying Menus:: How to add new items to a menu.
  4430. 
  4431. File: elisp, Node: Defining Menus, Next: Mouse Menus, Up: Menu Keymaps
  4432. 22.17.1 Defining Menus
  4433. ----------------------
  4434. A keymap acts as a menu if it has an "overall prompt string", which is
  4435. a string that appears as an element of the keymap. (*Note Format of
  4436. Keymaps::.) The string should describe the purpose of the menu's
  4437. commands. Emacs displays the overall prompt string as the menu title
  4438. in some cases, depending on the toolkit (if any) used for displaying
  4439. menus.(1) Keyboard menus also display the overall prompt string.
  4440. The easiest way to construct a keymap with a prompt string is to
  4441. specify the string as an argument when you call `make-keymap',
  4442. `make-sparse-keymap' (*note Creating Keymaps::), or
  4443. `define-prefix-command' (*note Definition of define-prefix-command::).
  4444. If you do not want the keymap to operate as a menu, don't specify a
  4445. prompt string for it.
  4446. -- Function: keymap-prompt keymap
  4447. This function returns the overall prompt string of KEYMAP, or
  4448. `nil' if it has none.
  4449. The menu's items are the bindings in the keymap. Each binding
  4450. associates an event type to a definition, but the event types have no
  4451. significance for the menu appearance. (Usually we use pseudo-events,
  4452. symbols that the keyboard cannot generate, as the event types for menu
  4453. item bindings.) The menu is generated entirely from the bindings that
  4454. correspond in the keymap to these events.
  4455. The order of items in the menu is the same as the order of bindings
  4456. in the keymap. Since `define-key' puts new bindings at the front, you
  4457. should define the menu items starting at the bottom of the menu and
  4458. moving to the top, if you care about the order. When you add an item to
  4459. an existing menu, you can specify its position in the menu using
  4460. `define-key-after' (*note Modifying Menus::).
  4461. * Menu:
  4462. * Simple Menu Items:: A simple kind of menu key binding,
  4463. limited in capabilities.
  4464. * Extended Menu Items:: More powerful menu item definitions
  4465. let you specify keywords to enable
  4466. various features.
  4467. * Menu Separators:: Drawing a horizontal line through a menu.
  4468. * Alias Menu Items:: Using command aliases in menu items.
  4469. * Toolkit Differences:: Not all toolkits provide the same features.
  4470. ---------- Footnotes ----------
  4471. (1) It is required for menus which do not use a toolkit, e.g. under
  4472. MS-DOS.
  4473. 
  4474. File: elisp, Node: Simple Menu Items, Next: Extended Menu Items, Up: Defining Menus
  4475. 22.17.1.1 Simple Menu Items
  4476. ...........................
  4477. The simpler (and original) way to define a menu item is to bind some
  4478. event type (it doesn't matter what event type) to a binding like this:
  4479. (ITEM-STRING . REAL-BINDING)
  4480. The CAR, ITEM-STRING, is the string to be displayed in the menu. It
  4481. should be short--preferably one to three words. It should describe the
  4482. action of the command it corresponds to. Note that not all graphical
  4483. toolkits can display non-ASCII text in menus (it will work for keyboard
  4484. menus and will work to a large extent with the GTK+ toolkit).
  4485. You can also supply a second string, called the help string, as
  4486. follows:
  4487. (ITEM-STRING HELP . REAL-BINDING)
  4488. HELP specifies a "help-echo" string to display while the mouse is on
  4489. that item in the same way as `help-echo' text properties (*note Help
  4490. display::).
  4491. As far as `define-key' is concerned, ITEM-STRING and HELP-STRING are
  4492. part of the event's binding. However, `lookup-key' returns just
  4493. REAL-BINDING, and only REAL-BINDING is used for executing the key.
  4494. If REAL-BINDING is `nil', then ITEM-STRING appears in the menu but
  4495. cannot be selected.
  4496. If REAL-BINDING is a symbol and has a non-`nil' `menu-enable'
  4497. property, that property is an expression that controls whether the menu
  4498. item is enabled. Every time the keymap is used to display a menu,
  4499. Emacs evaluates the expression, and it enables the menu item only if
  4500. the expression's value is non-`nil'. When a menu item is disabled, it
  4501. is displayed in a "fuzzy" fashion, and cannot be selected.
  4502. The menu bar does not recalculate which items are enabled every time
  4503. you look at a menu. This is because the X toolkit requires the whole
  4504. tree of menus in advance. To force recalculation of the menu bar, call
  4505. `force-mode-line-update' (*note Mode Line Format::).
  4506. 
  4507. File: elisp, Node: Extended Menu Items, Next: Menu Separators, Prev: Simple Menu Items, Up: Defining Menus
  4508. 22.17.1.2 Extended Menu Items
  4509. .............................
  4510. An extended-format menu item is a more flexible and also cleaner
  4511. alternative to the simple format. You define an event type with a
  4512. binding that's a list starting with the symbol `menu-item'. For a
  4513. non-selectable string, the binding looks like this:
  4514. (menu-item ITEM-NAME)
  4515. A string starting with two or more dashes specifies a separator line;
  4516. see *note Menu Separators::.
  4517. To define a real menu item which can be selected, the extended format
  4518. binding looks like this:
  4519. (menu-item ITEM-NAME REAL-BINDING
  4520. . ITEM-PROPERTY-LIST)
  4521. Here, ITEM-NAME is an expression which evaluates to the menu item
  4522. string. Thus, the string need not be a constant. The third element,
  4523. REAL-BINDING, is the command to execute. The tail of the list,
  4524. ITEM-PROPERTY-LIST, has the form of a property list which contains
  4525. other information.
  4526. Here is a table of the properties that are supported:
  4527. `:enable FORM'
  4528. The result of evaluating FORM determines whether the item is
  4529. enabled (non-`nil' means yes). If the item is not enabled, you
  4530. can't really click on it.
  4531. `:visible FORM'
  4532. The result of evaluating FORM determines whether the item should
  4533. actually appear in the menu (non-`nil' means yes). If the item
  4534. does not appear, then the menu is displayed as if this item were
  4535. not defined at all.
  4536. `:help HELP'
  4537. The value of this property, HELP, specifies a "help-echo" string
  4538. to display while the mouse is on that item. This is displayed in
  4539. the same way as `help-echo' text properties (*note Help display::).
  4540. Note that this must be a constant string, unlike the `help-echo'
  4541. property for text and overlays.
  4542. `:button (TYPE . SELECTED)'
  4543. This property provides a way to define radio buttons and toggle
  4544. buttons. The CAR, TYPE, says which: it should be `:toggle' or
  4545. `:radio'. The CDR, SELECTED, should be a form; the result of
  4546. evaluating it says whether this button is currently selected.
  4547. A "toggle" is a menu item which is labeled as either "on" or "off"
  4548. according to the value of SELECTED. The command itself should
  4549. toggle SELECTED, setting it to `t' if it is `nil', and to `nil' if
  4550. it is `t'. Here is how the menu item to toggle the
  4551. `debug-on-error' flag is defined:
  4552. (menu-item "Debug on Error" toggle-debug-on-error
  4553. :button (:toggle
  4554. . (and (boundp 'debug-on-error)
  4555. debug-on-error)))
  4556. This works because `toggle-debug-on-error' is defined as a command
  4557. which toggles the variable `debug-on-error'.
  4558. "Radio buttons" are a group of menu items, in which at any time one
  4559. and only one is "selected". There should be a variable whose value
  4560. says which one is selected at any time. The SELECTED form for
  4561. each radio button in the group should check whether the variable
  4562. has the right value for selecting that button. Clicking on the
  4563. button should set the variable so that the button you clicked on
  4564. becomes selected.
  4565. `:key-sequence KEY-SEQUENCE'
  4566. This property specifies which key sequence is likely to be bound
  4567. to the same command invoked by this menu item. If you specify the
  4568. right key sequence, that makes preparing the menu for display run
  4569. much faster.
  4570. If you specify the wrong key sequence, it has no effect; before
  4571. Emacs displays KEY-SEQUENCE in the menu, it verifies that
  4572. KEY-SEQUENCE is really equivalent to this menu item.
  4573. `:key-sequence nil'
  4574. This property indicates that there is normally no key binding
  4575. which is equivalent to this menu item. Using this property saves
  4576. time in preparing the menu for display, because Emacs does not
  4577. need to search the keymaps for a keyboard equivalent for this menu
  4578. item.
  4579. However, if the user has rebound this item's definition to a key
  4580. sequence, Emacs ignores the `:keys' property and finds the keyboard
  4581. equivalent anyway.
  4582. `:keys STRING'
  4583. This property specifies that STRING is the string to display as
  4584. the keyboard equivalent for this menu item. You can use the
  4585. `\\[...]' documentation construct in STRING.
  4586. `:filter FILTER-FN'
  4587. This property provides a way to compute the menu item dynamically.
  4588. The property value FILTER-FN should be a function of one argument;
  4589. when it is called, its argument will be REAL-BINDING. The
  4590. function should return the binding to use instead.
  4591. Emacs can call this function at any time that it does redisplay or
  4592. operates on menu data structures, so you should write it so it can
  4593. safely be called at any time.
  4594. 
  4595. File: elisp, Node: Menu Separators, Next: Alias Menu Items, Prev: Extended Menu Items, Up: Defining Menus
  4596. 22.17.1.3 Menu Separators
  4597. .........................
  4598. A menu separator is a kind of menu item that doesn't display any
  4599. text--instead, it divides the menu into subparts with a horizontal line.
  4600. A separator looks like this in the menu keymap:
  4601. (menu-item SEPARATOR-TYPE)
  4602. where SEPARATOR-TYPE is a string starting with two or more dashes.
  4603. In the simplest case, SEPARATOR-TYPE consists of only dashes. That
  4604. specifies the default kind of separator. (For compatibility, `""' and
  4605. `-' also count as separators.)
  4606. Certain other values of SEPARATOR-TYPE specify a different style of
  4607. separator. Here is a table of them:
  4608. `"--no-line"'
  4609. `"--space"'
  4610. An extra vertical space, with no actual line.
  4611. `"--single-line"'
  4612. A single line in the menu's foreground color.
  4613. `"--double-line"'
  4614. A double line in the menu's foreground color.
  4615. `"--single-dashed-line"'
  4616. A single dashed line in the menu's foreground color.
  4617. `"--double-dashed-line"'
  4618. A double dashed line in the menu's foreground color.
  4619. `"--shadow-etched-in"'
  4620. A single line with a 3D sunken appearance. This is the default,
  4621. used separators consisting of dashes only.
  4622. `"--shadow-etched-out"'
  4623. A single line with a 3D raised appearance.
  4624. `"--shadow-etched-in-dash"'
  4625. A single dashed line with a 3D sunken appearance.
  4626. `"--shadow-etched-out-dash"'
  4627. A single dashed line with a 3D raised appearance.
  4628. `"--shadow-double-etched-in"'
  4629. Two lines with a 3D sunken appearance.
  4630. `"--shadow-double-etched-out"'
  4631. Two lines with a 3D raised appearance.
  4632. `"--shadow-double-etched-in-dash"'
  4633. Two dashed lines with a 3D sunken appearance.
  4634. `"--shadow-double-etched-out-dash"'
  4635. Two dashed lines with a 3D raised appearance.
  4636. You can also give these names in another style, adding a colon after
  4637. the double-dash and replacing each single dash with capitalization of
  4638. the following word. Thus, `"--:singleLine"', is equivalent to
  4639. `"--single-line"'.
  4640. You can use a longer form to specify keywords such as `:enable' and
  4641. `:visible' for a menu separator:
  4642. `(menu-item SEPARATOR-TYPE nil . ITEM-PROPERTY-LIST)'
  4643. For example:
  4644. (menu-item "--" nil :visible (boundp 'foo))
  4645. Some systems and display toolkits don't really handle all of these
  4646. separator types. If you use a type that isn't supported, the menu
  4647. displays a similar kind of separator that is supported.
  4648. 
  4649. File: elisp, Node: Alias Menu Items, Next: Toolkit Differences, Prev: Menu Separators, Up: Defining Menus
  4650. 22.17.1.4 Alias Menu Items
  4651. ..........................
  4652. Sometimes it is useful to make menu items that use the "same" command
  4653. but with different enable conditions. The best way to do this in Emacs
  4654. now is with extended menu items; before that feature existed, it could
  4655. be done by defining alias commands and using them in menu items.
  4656. Here's an example that makes two aliases for `toggle-read-only' and
  4657. gives them different enable conditions:
  4658. (defalias 'make-read-only 'toggle-read-only)
  4659. (put 'make-read-only 'menu-enable '(not buffer-read-only))
  4660. (defalias 'make-writable 'toggle-read-only)
  4661. (put 'make-writable 'menu-enable 'buffer-read-only)
  4662. When using aliases in menus, often it is useful to display the
  4663. equivalent key bindings for the "real" command name, not the aliases
  4664. (which typically don't have any key bindings except for the menu
  4665. itself). To request this, give the alias symbol a non-`nil'
  4666. `menu-alias' property. Thus,
  4667. (put 'make-read-only 'menu-alias t)
  4668. (put 'make-writable 'menu-alias t)
  4669. causes menu items for `make-read-only' and `make-writable' to show the
  4670. keyboard bindings for `toggle-read-only'.
  4671. 
  4672. File: elisp, Node: Toolkit Differences, Prev: Alias Menu Items, Up: Defining Menus
  4673. 22.17.1.5 Toolkit Differences
  4674. .............................
  4675. The various toolkits with which you can build Emacs do not all support
  4676. the same set of features for menus. Some code works as expected with
  4677. one toolkit, but not under another.
  4678. One example is menu actions or buttons in a top-level menu bar. The
  4679. following works with the Lucid toolkit or on MS Windows, but not with
  4680. GTK or Nextstep, where clicking on the item has no effect.
  4681. (defun menu-action-greet ()
  4682. (interactive)
  4683. (message "Hello Emacs User!"))
  4684. (defun top-level-menu ()
  4685. (interactive)
  4686. (define-key lisp-interaction-mode-map [menu-bar m]
  4687. '(menu-item "Action Button" menu-action-greet)))
  4688. 
  4689. File: elisp, Node: Mouse Menus, Next: Keyboard Menus, Prev: Defining Menus, Up: Menu Keymaps
  4690. 22.17.2 Menus and the Mouse
  4691. ---------------------------
  4692. The usual way to make a menu keymap produce a menu is to make it the
  4693. definition of a prefix key. (A Lisp program can explicitly pop up a
  4694. menu and receive the user's choice--see *note Pop-Up Menus::.)
  4695. If the prefix key ends with a mouse event, Emacs handles the menu
  4696. keymap by popping up a visible menu, so that the user can select a
  4697. choice with the mouse. When the user clicks on a menu item, the event
  4698. generated is whatever character or symbol has the binding that brought
  4699. about that menu item. (A menu item may generate a series of events if
  4700. the menu has multiple levels or comes from the menu bar.)
  4701. It's often best to use a button-down event to trigger the menu. Then
  4702. the user can select a menu item by releasing the button.
  4703. If the menu keymap contains a binding to a nested keymap, the nested
  4704. keymap specifies a "submenu". There will be a menu item, labeled by
  4705. the nested keymap's item string, and clicking on this item
  4706. automatically pops up the specified submenu. As a special exception,
  4707. if the menu keymap contains a single nested keymap and no other menu
  4708. items, the menu shows the contents of the nested keymap directly, not
  4709. as a submenu.
  4710. However, if Emacs is compiled without X toolkit support, submenus
  4711. are not supported. Each nested keymap is shown as a menu item, but
  4712. clicking on it does not automatically pop up the submenu. If you wish
  4713. to imitate the effect of submenus, you can do that by giving a nested
  4714. keymap an item string which starts with `@'. This causes Emacs to
  4715. display the nested keymap using a separate "menu pane"; the rest of the
  4716. item string after the `@' is the pane label. If Emacs is compiled
  4717. without X toolkit support, menu panes are not used; in that case, a `@'
  4718. at the beginning of an item string is omitted when the menu label is
  4719. displayed, and has no other effect.
  4720. 
  4721. File: elisp, Node: Keyboard Menus, Next: Menu Example, Prev: Mouse Menus, Up: Menu Keymaps
  4722. 22.17.3 Menus and the Keyboard
  4723. ------------------------------
  4724. When a prefix key ending with a keyboard event (a character or function
  4725. key) has a definition that is a menu keymap, the keymap operates as a
  4726. keyboard menu; the user specifies the next event by choosing a menu
  4727. item with the keyboard.
  4728. Emacs displays the keyboard menu with the map's overall prompt
  4729. string, followed by the alternatives (the item strings of the map's
  4730. bindings), in the echo area. If the bindings don't all fit at once,
  4731. the user can type <SPC> to see the next line of alternatives.
  4732. Successive uses of <SPC> eventually get to the end of the menu and then
  4733. cycle around to the beginning. (The variable `menu-prompt-more-char'
  4734. specifies which character is used for this; <SPC> is the default.)
  4735. When the user has found the desired alternative from the menu, he or
  4736. she should type the corresponding character--the one whose binding is
  4737. that alternative.
  4738. -- Variable: menu-prompt-more-char
  4739. This variable specifies the character to use to ask to see the
  4740. next line of a menu. Its initial value is 32, the code for <SPC>.
  4741. 
  4742. File: elisp, Node: Menu Example, Next: Menu Bar, Prev: Keyboard Menus, Up: Menu Keymaps
  4743. 22.17.4 Menu Example
  4744. --------------------
  4745. Here is a complete example of defining a menu keymap. It is the
  4746. definition of the `Replace' submenu in the `Edit' menu in the menu bar,
  4747. and it uses the extended menu item format (*note Extended Menu
  4748. Items::). First we create the keymap, and give it a name:
  4749. (defvar menu-bar-replace-menu (make-sparse-keymap "Replace"))
  4750. Next we define the menu items:
  4751. (define-key menu-bar-replace-menu [tags-repl-continue]
  4752. '(menu-item "Continue Replace" tags-loop-continue
  4753. :help "Continue last tags replace operation"))
  4754. (define-key menu-bar-replace-menu [tags-repl]
  4755. '(menu-item "Replace in tagged files" tags-query-replace
  4756. :help "Interactively replace a regexp in all tagged files"))
  4757. (define-key menu-bar-replace-menu [separator-replace-tags]
  4758. '(menu-item "--"))
  4759. ;; ...
  4760. Note the symbols which the bindings are "made for"; these appear inside
  4761. square brackets, in the key sequence being defined. In some cases,
  4762. this symbol is the same as the command name; sometimes it is different.
  4763. These symbols are treated as "function keys", but they are not real
  4764. function keys on the keyboard. They do not affect the functioning of
  4765. the menu itself, but they are "echoed" in the echo area when the user
  4766. selects from the menu, and they appear in the output of `where-is' and
  4767. `apropos'.
  4768. The menu in this example is intended for use with the mouse. If a
  4769. menu is intended for use with the keyboard, that is, if it is bound to
  4770. a key sequence ending with a keyboard event, then the menu items should
  4771. be bound to characters or "real" function keys, that can be typed with
  4772. the keyboard.
  4773. The binding whose definition is `("--")' is a separator line. Like
  4774. a real menu item, the separator has a key symbol, in this case
  4775. `separator-replace-tags'. If one menu has two separators, they must
  4776. have two different key symbols.
  4777. Here is how we make this menu appear as an item in the parent menu:
  4778. (define-key menu-bar-edit-menu [replace]
  4779. (list 'menu-item "Replace" menu-bar-replace-menu))
  4780. Note that this incorporates the submenu keymap, which is the value of
  4781. the variable `menu-bar-replace-menu', rather than the symbol
  4782. `menu-bar-replace-menu' itself. Using that symbol in the parent menu
  4783. item would be meaningless because `menu-bar-replace-menu' is not a
  4784. command.
  4785. If you wanted to attach the same replace menu to a mouse click, you
  4786. can do it this way:
  4787. (define-key global-map [C-S-down-mouse-1]
  4788. menu-bar-replace-menu)
  4789. 
  4790. File: elisp, Node: Menu Bar, Next: Tool Bar, Prev: Menu Example, Up: Menu Keymaps
  4791. 22.17.5 The Menu Bar
  4792. --------------------
  4793. On graphical displays, there is usually a "menu bar" at the top of each
  4794. frame. *Note Menu Bars: (emacs)Menu Bars. Menu bar items are
  4795. subcommands of the fake "function key" `menu-bar', as defined in the
  4796. active keymaps.
  4797. To add an item to the menu bar, invent a fake "function key" of your
  4798. own (let's call it KEY), and make a binding for the key sequence
  4799. `[menu-bar KEY]'. Most often, the binding is a menu keymap, so that
  4800. pressing a button on the menu bar item leads to another menu.
  4801. When more than one active keymap defines the same "function key" for
  4802. the menu bar, the item appears just once. If the user clicks on that
  4803. menu bar item, it brings up a single, combined menu containing all the
  4804. subcommands of that item--the global subcommands, the local
  4805. subcommands, and the minor mode subcommands.
  4806. The variable `overriding-local-map' is normally ignored when
  4807. determining the menu bar contents. That is, the menu bar is computed
  4808. from the keymaps that would be active if `overriding-local-map' were
  4809. `nil'. *Note Active Keymaps::.
  4810. Here's an example of setting up a menu bar item:
  4811. ;; Make a menu keymap (with a prompt string)
  4812. ;; and make it the menu bar item's definition.
  4813. (define-key global-map [menu-bar words]
  4814. (cons "Words" (make-sparse-keymap "Words")))
  4815. ;; Define specific subcommands in this menu.
  4816. (define-key global-map
  4817. [menu-bar words forward]
  4818. '("Forward word" . forward-word))
  4819. (define-key global-map
  4820. [menu-bar words backward]
  4821. '("Backward word" . backward-word))
  4822. A local keymap can cancel a menu bar item made by the global keymap
  4823. by rebinding the same fake function key with `undefined' as the
  4824. binding. For example, this is how Dired suppresses the `Edit' menu bar
  4825. item:
  4826. (define-key dired-mode-map [menu-bar edit] 'undefined)
  4827. Here, `edit' is the fake function key used by the global map for the
  4828. `Edit' menu bar item. The main reason to suppress a global menu bar
  4829. item is to regain space for mode-specific items.
  4830. -- Variable: menu-bar-final-items
  4831. Normally the menu bar shows global items followed by items defined
  4832. by the local maps.
  4833. This variable holds a list of fake function keys for items to
  4834. display at the end of the menu bar rather than in normal sequence.
  4835. The default value is `(help-menu)'; thus, the `Help' menu item
  4836. normally appears at the end of the menu bar, following local menu
  4837. items.
  4838. -- Variable: menu-bar-update-hook
  4839. This normal hook is run by redisplay to update the menu bar
  4840. contents, before redisplaying the menu bar. You can use it to
  4841. update submenus whose contents should vary. Since this hook is
  4842. run frequently, we advise you to ensure that the functions it
  4843. calls do not take much time in the usual case.
  4844. Next to every menu bar item, Emacs displays a key binding that runs
  4845. the same command (if such a key binding exists). This serves as a
  4846. convenient hint for users who do not know the key binding. If a
  4847. command has multiple bindings, Emacs normally displays the first one it
  4848. finds. You can specify one particular key binding by assigning an
  4849. `:advertised-binding' symbol property to the command. *Note Keys in
  4850. Documentation::.
  4851. 
  4852. File: elisp, Node: Tool Bar, Next: Modifying Menus, Prev: Menu Bar, Up: Menu Keymaps
  4853. 22.17.6 Tool bars
  4854. -----------------
  4855. A "tool bar" is a row of clickable icons at the top of a frame, just
  4856. below the menu bar. *Note Tool Bars: (emacs)Tool Bars.
  4857. On each frame, the frame parameter `tool-bar-lines' controls how
  4858. many lines' worth of height to reserve for the tool bar. A zero value
  4859. suppresses the tool bar. If the value is nonzero, and
  4860. `auto-resize-tool-bars' is non-`nil', the tool bar expands and
  4861. contracts automatically as needed to hold the specified contents. If
  4862. the value is `grow-only', the tool bar expands automatically, but does
  4863. not contract automatically.
  4864. The tool bar contents are controlled by a menu keymap attached to a
  4865. fake "function key" called `tool-bar' (much like the way the menu bar
  4866. is controlled). So you define a tool bar item using `define-key', like
  4867. this:
  4868. (define-key global-map [tool-bar KEY] ITEM)
  4869. where KEY is a fake "function key" to distinguish this item from other
  4870. items, and ITEM is a menu item key binding (*note Extended Menu
  4871. Items::), which says how to display this item and how it behaves.
  4872. The usual menu keymap item properties, `:visible', `:enable',
  4873. `:button', and `:filter', are useful in tool bar bindings and have
  4874. their normal meanings. The REAL-BINDING in the item must be a command,
  4875. not a keymap; in other words, it does not work to define a tool bar
  4876. icon as a prefix key.
  4877. The `:help' property specifies a "help-echo" string to display while
  4878. the mouse is on that item. This is displayed in the same way as
  4879. `help-echo' text properties (*note Help display::).
  4880. In addition, you should use the `:image' property; this is how you
  4881. specify the image to display in the tool bar:
  4882. `:image IMAGE'
  4883. IMAGES is either a single image specification or a vector of four
  4884. image specifications. If you use a vector of four, one of them is
  4885. used, depending on circumstances:
  4886. item 0
  4887. Used when the item is enabled and selected.
  4888. item 1
  4889. Used when the item is enabled and deselected.
  4890. item 2
  4891. Used when the item is disabled and selected.
  4892. item 3
  4893. Used when the item is disabled and deselected.
  4894. If IMAGE is a single image specification, Emacs draws the tool bar
  4895. button in disabled state by applying an edge-detection algorithm to the
  4896. image.
  4897. The `:rtl' property specifies an alternative image to use for
  4898. right-to-left languages. Only the GTK+ version of Emacs supports this
  4899. at present.
  4900. Like the menu bar, the tool bar can display separators (*note Menu
  4901. Separators::). Tool bar separators are vertical rather than
  4902. horizontal, though, and only a single style is supported. They are
  4903. represented in the tool bar keymap by `(menu-item "--")' entries;
  4904. properties like `:visible' are not supported for tool bar separators.
  4905. Separators are rendered natively in GTK+ and Nextstep tool bars; in the
  4906. other cases, they are rendered using an image of a vertical line.
  4907. The default tool bar is defined so that items specific to editing do
  4908. not appear for major modes whose command symbol has a `mode-class'
  4909. property of `special' (*note Major Mode Conventions::). Major modes
  4910. may add items to the global bar by binding `[tool-bar FOO]' in their
  4911. local map. It makes sense for some major modes to replace the default
  4912. tool bar items completely, since not many can be accommodated
  4913. conveniently, and the default bindings make this easy by using an
  4914. indirection through `tool-bar-map'.
  4915. -- Variable: tool-bar-map
  4916. By default, the global map binds `[tool-bar]' as follows:
  4917. (global-set-key [tool-bar]
  4918. `(menu-item ,(purecopy "tool bar") ignore
  4919. :filter tool-bar-make-keymap))
  4920. The function `tool-bar-make-keymap', in turn, derives the actual
  4921. tool bar map dynamically from the value of the variable
  4922. `tool-bar-map'. Hence, you should normally adjust the default
  4923. (global) tool bar by changing that map. Some major modes, such as
  4924. Info mode, completely replace the global tool bar by making
  4925. `tool-bar-map' buffer-local and setting it to a different keymap.
  4926. There are two convenience functions for defining tool bar items, as
  4927. follows.
  4928. -- Function: tool-bar-add-item icon def key &rest props
  4929. This function adds an item to the tool bar by modifying
  4930. `tool-bar-map'. The image to use is defined by ICON, which is the
  4931. base name of an XPM, XBM or PBM image file to be located by
  4932. `find-image'. Given a value `"exit"', say, `exit.xpm', `exit.pbm'
  4933. and `exit.xbm' would be searched for in that order on a color
  4934. display. On a monochrome display, the search order is `.pbm',
  4935. `.xbm' and `.xpm'. The binding to use is the command DEF, and KEY
  4936. is the fake function key symbol in the prefix keymap. The
  4937. remaining arguments PROPS are additional property list elements to
  4938. add to the menu item specification.
  4939. To define items in some local map, bind `tool-bar-map' with `let'
  4940. around calls of this function:
  4941. (defvar foo-tool-bar-map
  4942. (let ((tool-bar-map (make-sparse-keymap)))
  4943. (tool-bar-add-item ...)
  4944. ...
  4945. tool-bar-map))
  4946. -- Function: tool-bar-add-item-from-menu command icon &optional map
  4947. &rest props
  4948. This function is a convenience for defining tool bar items which
  4949. are consistent with existing menu bar bindings. The binding of
  4950. COMMAND is looked up in the menu bar in MAP (default `global-map')
  4951. and modified to add an image specification for ICON, which is
  4952. found in the same way as by `tool-bar-add-item'. The resulting
  4953. binding is then placed in `tool-bar-map', so use this function
  4954. only for global tool bar items.
  4955. MAP must contain an appropriate keymap bound to `[menu-bar]'. The
  4956. remaining arguments PROPS are additional property list elements to
  4957. add to the menu item specification.
  4958. -- Function: tool-bar-local-item-from-menu command icon in-map
  4959. &optional from-map &rest props
  4960. This function is used for making non-global tool bar items. Use it
  4961. like `tool-bar-add-item-from-menu' except that IN-MAP specifies
  4962. the local map to make the definition in. The argument FROM-MAP is
  4963. like the MAP argument of `tool-bar-add-item-from-menu'.
  4964. -- Variable: auto-resize-tool-bars
  4965. If this variable is non-`nil', the tool bar automatically resizes
  4966. to show all defined tool bar items--but not larger than a quarter
  4967. of the frame's height.
  4968. If the value is `grow-only', the tool bar expands automatically,
  4969. but does not contract automatically. To contract the tool bar, the
  4970. user has to redraw the frame by entering `C-l'.
  4971. If Emacs is built with GTK or Nextstep, the tool bar can only show
  4972. one line, so this variable has no effect.
  4973. -- Variable: auto-raise-tool-bar-buttons
  4974. If this variable is non-`nil', tool bar items display in raised
  4975. form when the mouse moves over them.
  4976. -- Variable: tool-bar-button-margin
  4977. This variable specifies an extra margin to add around tool bar
  4978. items. The value is an integer, a number of pixels. The default
  4979. is 4.
  4980. -- Variable: tool-bar-button-relief
  4981. This variable specifies the shadow width for tool bar items. The
  4982. value is an integer, a number of pixels. The default is 1.
  4983. -- Variable: tool-bar-border
  4984. This variable specifies the height of the border drawn below the
  4985. tool bar area. An integer value specifies height as a number of
  4986. pixels. If the value is one of `internal-border-width' (the
  4987. default) or `border-width', the tool bar border height corresponds
  4988. to the corresponding frame parameter.
  4989. You can define a special meaning for clicking on a tool bar item with
  4990. the shift, control, meta, etc., modifiers. You do this by setting up
  4991. additional items that relate to the original item through the fake
  4992. function keys. Specifically, the additional items should use the
  4993. modified versions of the same fake function key used to name the
  4994. original item.
  4995. Thus, if the original item was defined this way,
  4996. (define-key global-map [tool-bar shell]
  4997. '(menu-item "Shell" shell
  4998. :image (image :type xpm :file "shell.xpm")))
  4999. then here is how you can define clicking on the same tool bar image with
  5000. the shift modifier:
  5001. (define-key global-map [tool-bar S-shell] 'some-command)
  5002. *Note Function Keys::, for more information about how to add
  5003. modifiers to function keys.
  5004. 
  5005. File: elisp, Node: Modifying Menus, Prev: Tool Bar, Up: Menu Keymaps
  5006. 22.17.7 Modifying Menus
  5007. -----------------------
  5008. When you insert a new item in an existing menu, you probably want to
  5009. put it in a particular place among the menu's existing items. If you
  5010. use `define-key' to add the item, it normally goes at the front of the
  5011. menu. To put it elsewhere in the menu, use `define-key-after':
  5012. -- Function: define-key-after map key binding &optional after
  5013. Define a binding in MAP for KEY, with value BINDING, just like
  5014. `define-key', but position the binding in MAP after the binding
  5015. for the event AFTER. The argument KEY should be of length one--a
  5016. vector or string with just one element. But AFTER should be a
  5017. single event type--a symbol or a character, not a sequence. The
  5018. new binding goes after the binding for AFTER. If AFTER is `t' or
  5019. is omitted, then the new binding goes last, at the end of the
  5020. keymap. However, new bindings are added before any inherited
  5021. keymap.
  5022. Here is an example:
  5023. (define-key-after my-menu [drink]
  5024. '("Drink" . drink-command) 'eat)
  5025. makes a binding for the fake function key <DRINK> and puts it
  5026. right after the binding for <EAT>.
  5027. Here is how to insert an item called `Work' in the `Signals' menu
  5028. of Shell mode, after the item `break':
  5029. (define-key-after
  5030. (lookup-key shell-mode-map [menu-bar signals])
  5031. [work] '("Work" . work-command) 'break)
  5032. 
  5033. File: elisp, Node: Modes, Next: Documentation, Prev: Keymaps, Up: Top
  5034. 23 Major and Minor Modes
  5035. ************************
  5036. A "mode" is a set of definitions that customize Emacs and can be turned
  5037. on and off while you edit. There are two varieties of modes: "major
  5038. modes", which are mutually exclusive and used for editing particular
  5039. kinds of text, and "minor modes", which provide features that users can
  5040. enable individually.
  5041. This chapter describes how to write both major and minor modes, how
  5042. to indicate them in the mode line, and how they run hooks supplied by
  5043. the user. For related topics such as keymaps and syntax tables, see
  5044. *note Keymaps::, and *note Syntax Tables::.
  5045. * Menu:
  5046. * Hooks:: How to use hooks; how to write code that provides hooks.
  5047. * Major Modes:: Defining major modes.
  5048. * Minor Modes:: Defining minor modes.
  5049. * Mode Line Format:: Customizing the text that appears in the mode line.
  5050. * Imenu:: Providing a menu of definitions made in a buffer.
  5051. * Font Lock Mode:: How modes can highlight text according to syntax.
  5052. * Auto-Indentation:: How to teach Emacs to indent for a major mode.
  5053. * Desktop Save Mode:: How modes can have buffer state saved between
  5054. Emacs sessions.
  5055. 
  5056. File: elisp, Node: Hooks, Next: Major Modes, Up: Modes
  5057. 23.1 Hooks
  5058. ==========
  5059. A "hook" is a variable where you can store a function or functions to
  5060. be called on a particular occasion by an existing program. Emacs
  5061. provides hooks for the sake of customization. Most often, hooks are set
  5062. up in the init file (*note Init File::), but Lisp programs can set them
  5063. also. *Note Standard Hooks::, for a list of some standard hook
  5064. variables.
  5065. Most of the hooks in Emacs are "normal hooks". These variables
  5066. contain lists of functions to be called with no arguments. By
  5067. convention, whenever the hook name ends in `-hook', that tells you it
  5068. is normal. We try to make all hooks normal, as much as possible, so
  5069. that you can use them in a uniform way.
  5070. Every major mode command is supposed to run a normal hook called the
  5071. "mode hook" as one of the last steps of initialization. This makes it
  5072. easy for a user to customize the behavior of the mode, by overriding
  5073. the buffer-local variable assignments already made by the mode. Most
  5074. minor mode functions also run a mode hook at the end. But hooks are
  5075. used in other contexts too. For example, the hook `suspend-hook' runs
  5076. just before Emacs suspends itself (*note Suspending Emacs::).
  5077. The recommended way to add a hook function to a hook is by calling
  5078. `add-hook' (*note Setting Hooks::). The hook functions may be any of
  5079. the valid kinds of functions that `funcall' accepts (*note What Is a
  5080. Function::). Most normal hook variables are initially void; `add-hook'
  5081. knows how to deal with this. You can add hooks either globally or
  5082. buffer-locally with `add-hook'.
  5083. If the hook variable's name does not end with `-hook', that
  5084. indicates it is probably an "abnormal hook". That means the hook
  5085. functions are called with arguments, or their return values are used in
  5086. some way. The hook's documentation says how the functions are called.
  5087. You can use `add-hook' to add a function to an abnormal hook, but you
  5088. must write the function to follow the hook's calling convention.
  5089. By convention, abnormal hook names end in `-functions' or `-hooks'.
  5090. If the variable's name ends in `-function', then its value is just a
  5091. single function, not a list of functions.
  5092. * Menu:
  5093. * Running Hooks:: How to run a hook.
  5094. * Setting Hooks:: How to put functions on a hook, or remove them.
  5095. 
  5096. File: elisp, Node: Running Hooks, Next: Setting Hooks, Up: Hooks
  5097. 23.1.1 Running Hooks
  5098. --------------------
  5099. In this section, we document the `run-hooks' function, which is used to
  5100. run a normal hook. We also document the functions for running various
  5101. kinds of abnormal hooks.
  5102. -- Function: run-hooks &rest hookvars
  5103. This function takes one or more normal hook variable names as
  5104. arguments, and runs each hook in turn. Each argument should be a
  5105. symbol that is a normal hook variable. These arguments are
  5106. processed in the order specified.
  5107. If a hook variable has a non-`nil' value, that value should be a
  5108. list of functions. `run-hooks' calls all the functions, one by
  5109. one, with no arguments.
  5110. The hook variable's value can also be a single function--either a
  5111. lambda expression or a symbol with a function definition--which
  5112. `run-hooks' calls. But this usage is obsolete.
  5113. If the hook variable is buffer-local, the buffer-local variable
  5114. will be used instead of the global variable. However, if the
  5115. buffer-local variable contains the element `t', the global hook
  5116. variable will be run as well.
  5117. -- Function: run-hook-with-args hook &rest args
  5118. This function runs an abnormal hook by calling all the hook
  5119. functions in HOOK, passing each one the arguments ARGS.
  5120. -- Function: run-hook-with-args-until-failure hook &rest args
  5121. This function runs an abnormal hook by calling each hook function
  5122. in turn, stopping if one of them "fails" by returning `nil'. Each
  5123. hook function is passed the arguments ARGS. If this function
  5124. stops because one of the hook functions fails, it returns `nil';
  5125. otherwise it returns a non-`nil' value.
  5126. -- Function: run-hook-with-args-until-success hook &rest args
  5127. This function runs an abnormal hook by calling each hook function,
  5128. stopping if one of them "succeeds" by returning a non-`nil' value.
  5129. Each hook function is passed the arguments ARGS. If this function
  5130. stops because one of the hook functions returns a non-`nil' value,
  5131. it returns that value; otherwise it returns `nil'.
  5132. -- Macro: with-wrapper-hook hook args &rest body
  5133. This macro runs the abnormal hook `hook' as a series of nested
  5134. "wrapper functions" around the BODY forms. The effect is similar
  5135. to nested `around' advices (*note Around-Advice::).
  5136. Each hook function should accept an argument list consisting of a
  5137. function FUN, followed by the additional arguments listed in ARGS.
  5138. The first hook function is passed a function FUN that, if it is
  5139. called with arguments ARGS, performs BODY (i.e., the default
  5140. operation). The FUN passed to each successive hook function is
  5141. constructed from all the preceding hook functions (and BODY); if
  5142. this FUN is called with arguments ARGS, it does what the
  5143. `with-wrapper-hook' call would if the preceding hook functions were
  5144. the only ones in HOOK.
  5145. Each hook function may call its FUN argument as many times as it
  5146. wishes, including never. In that case, such a hook function acts
  5147. to replace the default definition altogether, and any preceding
  5148. hook functions. Of course, a subsequent hook function may do the
  5149. same thing.
  5150. Each hook function definition is used to construct the FUN passed
  5151. to the next hook function in HOOK, if any. The last or
  5152. "outermost" FUN is called once to produce the overall effect.
  5153. When might you want to use a wrapper hook? The function
  5154. `filter-buffer-substring' illustrates a common case. There is a
  5155. basic functionality, performed by BODY--in this case, to extract a
  5156. buffer-substring. Then any number of hook functions can act in
  5157. sequence to modify that string, before returning the final result.
  5158. A wrapper-hook also allows for a hook function to completely
  5159. replace the default definition (by not calling FUN).
  5160. -- Function: run-hook-wrapped hook wrap-function &rest args
  5161. This function is similar to `run-hook-with-args-until-success'.
  5162. Like that function, it runs the functions on the abnormal hook
  5163. `hook', stopping at the first one that returns non-`nil'. Instead
  5164. of calling the hook functions directly, though, it actually calls
  5165. `wrap-function' with arguments `fun' and `args'.
  5166. 
  5167. File: elisp, Node: Setting Hooks, Prev: Running Hooks, Up: Hooks
  5168. 23.1.2 Setting Hooks
  5169. --------------------
  5170. Here's an example that uses a mode hook to turn on Auto Fill mode when
  5171. in Lisp Interaction mode:
  5172. (add-hook 'lisp-interaction-mode-hook 'auto-fill-mode)
  5173. -- Function: add-hook hook function &optional append local
  5174. This function is the handy way to add function FUNCTION to hook
  5175. variable HOOK. You can use it for abnormal hooks as well as for
  5176. normal hooks. FUNCTION can be any Lisp function that can accept
  5177. the proper number of arguments for HOOK. For example,
  5178. (add-hook 'text-mode-hook 'my-text-hook-function)
  5179. adds `my-text-hook-function' to the hook called `text-mode-hook'.
  5180. If FUNCTION is already present in HOOK (comparing using `equal'),
  5181. then `add-hook' does not add it a second time.
  5182. If FUNCTION has a non-`nil' property `permanent-local-hook', then
  5183. `kill-all-local-variables' (or changing major modes) won't delete
  5184. it from the hook variable's local value.
  5185. For a normal hook, hook functions should be designed so that the
  5186. order in which they are executed does not matter. Any dependence
  5187. on the order is asking for trouble. However, the order is
  5188. predictable: normally, FUNCTION goes at the front of the hook
  5189. list, so it is executed first (barring another `add-hook' call).
  5190. If the optional argument APPEND is non-`nil', the new hook
  5191. function goes at the end of the hook list and is executed last.
  5192. `add-hook' can handle the cases where HOOK is void or its value is
  5193. a single function; it sets or changes the value to a list of
  5194. functions.
  5195. If LOCAL is non-`nil', that says to add FUNCTION to the
  5196. buffer-local hook list instead of to the global hook list. This
  5197. makes the hook buffer-local and adds `t' to the buffer-local
  5198. value. The latter acts as a flag to run the hook functions in the
  5199. default value as well as in the local value.
  5200. -- Function: remove-hook hook function &optional local
  5201. This function removes FUNCTION from the hook variable HOOK. It
  5202. compares FUNCTION with elements of HOOK using `equal', so it works
  5203. for both symbols and lambda expressions.
  5204. If LOCAL is non-`nil', that says to remove FUNCTION from the
  5205. buffer-local hook list instead of from the global hook list.
  5206. 
  5207. File: elisp, Node: Major Modes, Next: Minor Modes, Prev: Hooks, Up: Modes
  5208. 23.2 Major Modes
  5209. ================
  5210. Major modes specialize Emacs for editing particular kinds of text.
  5211. Each buffer has one major mode at a time. Every major mode is
  5212. associated with a "major mode command", whose name should end in
  5213. `-mode'. This command takes care of switching to that mode in the
  5214. current buffer, by setting various buffer-local variables such as a
  5215. local keymap. *Note Major Mode Conventions::.
  5216. The least specialized major mode is called "Fundamental mode", which
  5217. has no mode-specific definitions or variable settings.
  5218. -- Command: fundamental-mode
  5219. This is the major mode command for Fundamental mode. Unlike other
  5220. mode commands, it does _not_ run any mode hooks (*note Major Mode
  5221. Conventions::), since you are not supposed to customize this mode.
  5222. The easiest way to write a major mode is to use the macro
  5223. `define-derived-mode', which sets up the new mode as a variant of an
  5224. existing major mode. *Note Derived Modes::. We recommend using
  5225. `define-derived-mode' even if the new mode is not an obvious derivative
  5226. of another mode, as it automatically enforces many coding conventions
  5227. for you. *Note Basic Major Modes::, for common modes to derive from.
  5228. The standard GNU Emacs Lisp directory tree contains the code for
  5229. several major modes, in files such as `text-mode.el', `texinfo.el',
  5230. `lisp-mode.el', and `rmail.el'. You can study these libraries to see
  5231. how modes are written.
  5232. -- User Option: major-mode
  5233. The buffer-local value of this variable holds the symbol for the
  5234. current major mode. Its default value holds the default major
  5235. mode for new buffers. The standard default value is
  5236. `fundamental-mode'.
  5237. If the default value is `nil', then whenever Emacs creates a new
  5238. buffer via a command such as `C-x b' (`switch-to-buffer'), the new
  5239. buffer is put in the major mode of the previously current buffer.
  5240. As an exception, if the major mode of the previous buffer has a
  5241. `mode-class' symbol property with value `special', the new buffer
  5242. is put in Fundamental mode (*note Major Mode Conventions::).
  5243. * Menu:
  5244. * Major Mode Conventions:: Coding conventions for keymaps, etc.
  5245. * Auto Major Mode:: How Emacs chooses the major mode automatically.
  5246. * Mode Help:: Finding out how to use a mode.
  5247. * Derived Modes:: Defining a new major mode based on another major
  5248. mode.
  5249. * Basic Major Modes:: Modes that other modes are often derived from.
  5250. * Mode Hooks:: Hooks run at the end of major mode functions.
  5251. * Tabulated List Mode:: Parent mode for buffers containing tabulated data.
  5252. * Generic Modes:: Defining a simple major mode that supports
  5253. comment syntax and Font Lock mode.
  5254. * Example Major Modes:: Text mode and Lisp modes.
  5255. 
  5256. Local Variables:
  5257. coding: iso-8859-1
  5258. End: