ido.el 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779
  1. ;;; ido.el --- interactively do things with buffers and files
  2. ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
  3. ;; Author: Kim F. Storm <storm@cua.dk>
  4. ;; Based on: iswitchb by Stephen Eglen <stephen@cns.ed.ac.uk>
  5. ;; Keywords: extensions convenience
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Ido - interactive do - switches between buffers and opens files and
  19. ;; directories with a minimum of keystrokes. It is a superset of
  20. ;; iswitchb, the interactive buffer switching package by Stephen Eglen.
  21. ;; Interactive substring matching
  22. ;; ------------------------------
  23. ;;
  24. ;; As you type in a substring, the list of buffers or files currently
  25. ;; matching the substring are displayed as you type. The list is
  26. ;; ordered so that the most recent buffers or files visited come at
  27. ;; the start of the list.
  28. ;;
  29. ;; The buffer or file at the start of the list will be the one visited
  30. ;; when you press RETURN. By typing more of the substring, the list is
  31. ;; narrowed down so that gradually the buffer or file you want will be
  32. ;; at the top of the list. Alternatively, you can use C-s and C-r (or
  33. ;; the right and left arrow keys) to rotate buffer or file names in the
  34. ;; list until the one you want is at the top of the list.
  35. ;;
  36. ;; Completion is also available so that you can see what is common to
  37. ;; all of the matching buffers or files as you type.
  38. ;;
  39. ;; Example:
  40. ;;
  41. ;; If I have two buffers called "123456" and "123", with "123456" the
  42. ;; most recent, when I use ido-switch-buffer, I first of all get
  43. ;; presented with the list of all the buffers
  44. ;;
  45. ;; Buffer: {123456 | 123}
  46. ;;
  47. ;; If I then press 2:
  48. ;; Buffer: 2[3]{123456 | 123}
  49. ;;
  50. ;; The list in {...} are the matching buffers, most recent first
  51. ;; (buffers visible in the current frame are put at the end of the
  52. ;; list by default). At any time I can select the item at the head of
  53. ;; the list by pressing RET. I can also put the first element at the
  54. ;; end of the list by pressing C-s or [right], or bring the last
  55. ;; element to the head of the list by pressing C-r or [left].
  56. ;;
  57. ;; The item in [...] indicates what can be added to my input by
  58. ;; pressing TAB. In this case, I will get "3" added to my input.
  59. ;; So, I press TAB:
  60. ;; Buffer: 23{123456 | 123}
  61. ;;
  62. ;; At this point, I still have two matching buffers.
  63. ;; If I want the first buffer in the list, I simply press RET. If I
  64. ;; wanted the second in the list, I could press C-s to move it to the
  65. ;; top of the list and then RET to select it.
  66. ;;
  67. ;; However, if I type 4, I only have one match left:
  68. ;; Buffer: 234[123456]
  69. ;;
  70. ;; Since there is only one matching buffer left, it is given in [] and
  71. ;; it is shown in the `ido-only-match' face (ForestGreen). I can now
  72. ;; press TAB or RET to go to that buffer.
  73. ;;
  74. ;; If I want to create a new buffer named "234", I press C-j instead of
  75. ;; TAB or RET.
  76. ;;
  77. ;; If instead, I type "a":
  78. ;; Buffer: 234a [No match]
  79. ;; There are no matching buffers. If I press RET or TAB, I can be
  80. ;; prompted to create a new buffer called "234a".
  81. ;;
  82. ;; Of course, where this function comes in really useful is when you
  83. ;; can specify the buffer using only a few keystrokes. In the above
  84. ;; example, the quickest way to get to the "123456" file would be
  85. ;; just to type 4 and then RET (assuming there isn't any newer buffer
  86. ;; with 4 in its name).
  87. ;; Likewise, if you use C-x C-f (ido-find-file), the list of files and
  88. ;; directories in the current directory is provided in the same
  89. ;; fashion as the buffers above. The files and directories are
  90. ;; normally sorted in alphabetical order, but the most recently
  91. ;; visited directory is placed first to speed up navigating to
  92. ;; directories that you have visited recently.
  93. ;;
  94. ;; In addition to scrolling through the list using [right] and [left],
  95. ;; you can use [up] and [down] to quickly scroll the list to the next
  96. ;; or previous subdirectory.
  97. ;;
  98. ;; To go down into a subdirectory, and continue the file selection on
  99. ;; the files in that directory, simply move the directory to the head
  100. ;; of the list and hit RET.
  101. ;;
  102. ;; To go up to the parent directory, delete any partial file name
  103. ;; already specified (e.g. using [backspace]) and hit [backspace].
  104. ;;
  105. ;; To go to the root directory (on the current drive), enter two
  106. ;; slashes. On MS-DOS or Windows, to select the root of another
  107. ;; drive, enter X:/ where X is the drive letter. You can also visit
  108. ;; files on other hosts using the ange-ftp notations `/host:' and
  109. ;; `/user@host:'. See the variable `ido-slow-ftp-hosts' if you want
  110. ;; to inhibit the ido substring matching for ftp access.
  111. ;;
  112. ;; If for some reason you cannot specify the proper file using
  113. ;; ido-find-file, you can press C-f to enter the normal find-file.
  114. ;; You can also press C-b to drop into ido-switch-buffer.
  115. ;; See the doc string of ido-switch-buffer and ido-find-file for full
  116. ;; keybindings and features.
  117. ;; (describe-function 'ido-find-file)
  118. ;; Hidden buffers and files
  119. ;; ------------------------
  120. ;;
  121. ;; Normally, ido does not include hidden buffers (whose name starts
  122. ;; with a space) and hidden files and directories (whose name starts
  123. ;; with `.') in the list of possible completions. However, if the
  124. ;; substring you enter does not match any of the visible buffers or
  125. ;; files, ido will automatically look for completions among the hidden
  126. ;; buffers or files.
  127. ;;
  128. ;; You can toggle display of the hidden buffers and files with C-a.
  129. ;; Additional functionality
  130. ;; ------------------------
  131. ;;
  132. ;; After C-x b, the buffer at the head of the list can be killed by
  133. ;; pressing C-k. If the buffer needs saving, you will be queried
  134. ;; before the buffer is killed.
  135. ;;
  136. ;; Likewise, after C-x C-f, you can delete (i.e. physically remove)
  137. ;; the file at the head of the list with C-k. You will always be
  138. ;; asked for confirmation before the file is deleted.
  139. ;;
  140. ;; If you enter C-x b to switch to a buffer visiting a given file, and
  141. ;; you find that the file you are after is not in any buffer, you can
  142. ;; press C-f to immediately drop into ido-find-file. And you can
  143. ;; switch back to buffer selection with C-b.
  144. ;; Prefix matching
  145. ;; ---------------
  146. ;;
  147. ;; The standard way of completion with Unix-shells and Emacs is to insert a
  148. ;; PREFIX and then hitting TAB (or another completion key). Cause of this
  149. ;; behavior has become second nature to a lot of emacs users `ido' offers in
  150. ;; addition to the default substring-matching-method (look above) also the
  151. ;; prefix-matching-method. The kind of matching is the only difference to
  152. ;; the description of the substring-matching above.
  153. ;;
  154. ;; You can toggle prefix matching with C-p.
  155. ;;
  156. ;; Example:
  157. ;;
  158. ;; If you have again two Buffers "123456" and "123" then hitting "2" does
  159. ;; not match because "2" is not a PREFIX in any of the buffer-names.
  160. ;; Flexible matching
  161. ;; -----------------
  162. ;;
  163. ;; If you set ido-enable-flex-matching, ido will do a more flexible
  164. ;; matching (unless regexp matching is active) to find possible matches
  165. ;; among the available buffer or file names if no matches are found using
  166. ;; the normal prefix or substring matching.
  167. ;;
  168. ;; The flexible matching implies that any item which simply contains all
  169. ;; of the entered characters in the specified sequence will match.
  170. ;;
  171. ;; Example:
  172. ;;
  173. ;; If you have four files "alpha", "beta", "gamma", and "delta",
  174. ;; entering "aa" will match "alpha" and "gamma", while "ea" matches
  175. ;; "beta" and "delta". If prefix matching is also active, "aa" only
  176. ;; matches "alpha", while "ea" does not match any files.
  177. ;; Regexp matching
  178. ;; ---------------
  179. ;;
  180. ;; There is limited provision for regexp matching within ido,
  181. ;; enabled through `ido-enable-regexp' (toggle with C-t).
  182. ;; This allows you to type `[ch]$' for example and see all file names
  183. ;; ending in `c' or `h'.
  184. ;;
  185. ;; Note: ido-style completion is inhibited when you enable regexp matching.
  186. ;; Customization
  187. ;; -------------
  188. ;;
  189. ;; Customize the `ido' group to change the `ido' functionality.
  190. ;;
  191. ;; To modify the keybindings, use the ido-setup-hook. For example:
  192. ;;(add-hook 'ido-setup-hook 'ido-my-keys)
  193. ;;
  194. ;;(defun ido-my-keys ()
  195. ;; "Add my keybindings for ido."
  196. ;; (define-key ido-completion-map " " 'ido-next-match)
  197. ;; )
  198. ;; Seeing all the matching buffers or files
  199. ;; ----------------------------------------
  200. ;;
  201. ;; If you have many matching files, they may not all fit onto one
  202. ;; line of the minibuffer. Normally, the minibuffer window will grow
  203. ;; to show you more of the matching files (depending on the setting
  204. ;; of the variables `resize-mini-windows' and `max-mini-window-height').
  205. ;; If you want ido to behave differently from the default minibuffer
  206. ;; resizing behavior, set the variable `ido-max-window-height'.
  207. ;;
  208. ;; Also, to improve the responsiveness of ido, the maximum number of
  209. ;; matching items is limited to 12, but you can increase or removed
  210. ;; this limit via the `ido-max-prospects' variable.
  211. ;; To see a full list of all matching buffers in a separate buffer,
  212. ;; hit ? or press TAB when there are no further completions to the
  213. ;; substring. Repeated TAB presses will scroll you through this
  214. ;; separate buffer.
  215. ;; Changing the list of files
  216. ;; --------------------------
  217. ;; By default, the list of current files is most recent first,
  218. ;; oldest last, with the exception that the files visible in the
  219. ;; current frame are put at the end of the list. A hook exists to
  220. ;; allow other functions to order the list. For example, if you add:
  221. ;;
  222. ;; (add-hook 'ido-make-buffer-list-hook 'ido-summary-buffers-to-end)
  223. ;;
  224. ;; then all files matching "Summary" are moved to the end of the
  225. ;; list. (I find this handy for keeping the INBOX Summary and so on
  226. ;; out of the way.) It also moves files matching "output\*$" to the
  227. ;; end of the list (these are created by AUCTeX when compiling.)
  228. ;; Other functions could be made available which alter the list of
  229. ;; matching files (either deleting or rearranging elements.)
  230. ;; Highlighting
  231. ;; ------------
  232. ;; The highlighting of matching items is controlled via ido-use-faces.
  233. ;; The faces used are ido-first-match, ido-only-match and
  234. ;; ido-subdir.
  235. ;; Coloring of the matching item was suggested by
  236. ;; Carsten Dominik (dominik@strw.leidenuniv.nl).
  237. ;; Replacement for read-buffer and read-file-name
  238. ;; ----------------------------------------------
  239. ;; ido-read-buffer and ido-read-file-name have been written to be drop
  240. ;; in replacements for the normal buffer and file name reading
  241. ;; functions `read-buffer' and `read-file-name'.
  242. ;; To use ido for all buffer and file selections in Emacs, customize the
  243. ;; variable `ido-everywhere'.
  244. ;; Using ido-like behavior in other lisp packages
  245. ;; -----------------------------------------------
  246. ;; If you don't want to rely on the `ido-everywhere' functionality,
  247. ;; ido-read-buffer, ido-read-file-name, and ido-read-directory-name
  248. ;; can be used by other packages to read a buffer name, a file name,
  249. ;; or a directory name in the `ido' way.
  250. ;;; Acknowledgements
  251. ;; Infinite amounts of gratitude goes to Stephen Eglen <stephen@cns.ed.ac.uk>
  252. ;; who wrote iswitch-buffer mode - from which I ripped off 99% of the code
  253. ;; for ido-switch-buffer and found the inspiration for ido-find-file.
  254. ;; The ido package would never have existed without his work.
  255. ;; Also thanks to Klaus Berndl, Rohit Namjoshi, Robert Fenk, Alex
  256. ;; Schroeder, Bill Benedetto, Stephen Eglen, and many others for bug
  257. ;; fixes and improvements.
  258. ;;; History
  259. ;; Since I discovered Stephen Eglen's excellent iswitchb package, I just
  260. ;; couldn't live without it, but once being addicted to switching buffers
  261. ;; with a minimum of keystrokes, I soon found that opening files in the
  262. ;; old-fashioned way was just too slow - so I decided to write a package
  263. ;; which could open files with the same speed and ease as iswitchb could
  264. ;; switch buffers.
  265. ;; I originally wrote a separate ifindf.el package based on a copy of
  266. ;; iswitchb.el, which did for opening files what iswitchb did for
  267. ;; switching buffers. Along the way, I corrected a few errors in
  268. ;; ifindf which could have found its way back into iswitchb, but since
  269. ;; most of the functionality of the two package was practically
  270. ;; identical, I decided that the proper thing to do was to merge my
  271. ;; ifindf package back into iswitchb.
  272. ;;
  273. ;; This is basically what ido (interactively do) is all about; but I
  274. ;; found it awkward to merge my changes into the "iswitchb-" namespace,
  275. ;; so I invented a common "ido-" namespace for the merged packages.
  276. ;;
  277. ;; This version is based on ido.el version 1.57 released on
  278. ;; gnu.emacs.sources adapted for emacs 22.1 to use command remapping
  279. ;; and optionally hooking the read-buffer and read-file-name functions.
  280. ;;
  281. ;; Prefix matching was added by Klaus Berndl <klaus.berndl@sdm.de> based on
  282. ;; an idea of Yuji Minejima <ggb01164@nifty.ne.jp> and his mcomplete-package.
  283. ;;; Code:
  284. (defvar recentf-list)
  285. ;;; User Variables
  286. ;;
  287. ;; These are some things you might want to change.
  288. (defun ido-fractionp (n)
  289. (and (numberp n) (> n 0.0) (<= n 1.0)))
  290. (defgroup ido nil
  291. "Switch between files using substrings."
  292. :group 'extensions
  293. :group 'convenience
  294. :version "22.1"
  295. :link '(emacs-commentary-link :tag "Commentary" "ido.el")
  296. :link '(emacs-library-link :tag "Lisp File" "ido.el"))
  297. ;;;###autoload
  298. (defcustom ido-mode nil
  299. "Determines for which functional group \(buffer and files) ido behavior
  300. should be enabled. The following values are possible:
  301. - `buffer': Turn only on ido buffer behavior \(switching, killing,
  302. displaying...)
  303. - `file': Turn only on ido file behavior \(finding, writing, inserting...)
  304. - `both': Turn on ido buffer and file behavior.
  305. - `nil': Turn off any ido switching.
  306. Setting this variable directly does not take effect;
  307. use either \\[customize] or the function `ido-mode'."
  308. :set #'(lambda (_symbol value)
  309. (ido-mode value))
  310. :initialize 'custom-initialize-default
  311. :require 'ido
  312. :link '(emacs-commentary-link "ido.el")
  313. :set-after '(ido-save-directory-list-file
  314. ;; This will clear ido-unc-hosts-cache, so set it
  315. ;; before loading history file.
  316. ido-unc-hosts)
  317. :type '(choice (const :tag "Turn on only buffer" buffer)
  318. (const :tag "Turn on only file" file)
  319. (const :tag "Turn on both buffer and file" both)
  320. (const :tag "Switch off all" nil))
  321. :group 'ido)
  322. (defcustom ido-case-fold case-fold-search
  323. "Non-nil if searching of buffer and file names should ignore case."
  324. :type 'boolean
  325. :group 'ido)
  326. (defcustom ido-ignore-buffers
  327. '("\\` ")
  328. "List of regexps or functions matching buffer names to ignore.
  329. For example, traditional behavior is not to list buffers whose names begin
  330. with a space, for which the regexp is `\\` '. See the source file for
  331. example functions that filter buffer names."
  332. :type '(repeat (choice regexp function))
  333. :group 'ido)
  334. (defcustom ido-ignore-files
  335. '("\\`CVS/" "\\`#" "\\`.#" "\\`\\.\\./" "\\`\\./")
  336. "List of regexps or functions matching file names to ignore.
  337. For example, traditional behavior is not to list files whose names begin
  338. with a #, for which the regexp is `\\`#'. See the source file for
  339. example functions that filter filenames."
  340. :type '(repeat (choice regexp function))
  341. :group 'ido)
  342. (defcustom ido-ignore-extensions t
  343. "Non-nil means ignore files in `completion-ignored-extensions' list."
  344. :type 'boolean
  345. :group 'ido)
  346. (defcustom ido-show-dot-for-dired nil
  347. "Non-nil means to always put . as the first item in file name lists.
  348. This allows the current directory to be opened immediately with `dired'."
  349. :type 'boolean
  350. :group 'ido)
  351. (defcustom ido-file-extensions-order nil
  352. "List of file extensions specifying preferred order of file selections.
  353. Each element is either a string with `.' as the first char, an empty
  354. string matching files without extension, or t which is the default order
  355. for files with an unlisted file extension."
  356. :type '(repeat (choice string
  357. (const :tag "Default order" t)))
  358. :group 'ido)
  359. (defcustom ido-ignore-directories
  360. '("\\`CVS/" "\\`\\.\\./" "\\`\\./")
  361. "List of regexps or functions matching sub-directory names to ignore."
  362. :type '(repeat (choice regexp function))
  363. :group 'ido)
  364. (defcustom ido-ignore-directories-merge nil
  365. "List of regexps or functions matching directory names to ignore during merge.
  366. Directory names matched by one of the regexps in this list are not inserted
  367. in merged file and directory lists."
  368. :type '(repeat (choice regexp function))
  369. :group 'ido)
  370. ;; Examples for setting the value of ido-ignore-buffers
  371. ;;(defun ido-ignore-c-mode (name)
  372. ;; "Ignore all c mode buffers -- example function for ido."
  373. ;; (with-current-buffer name
  374. ;; (derived-mode-p 'c-mode)))
  375. ;;
  376. ;;(setq ido-ignore-buffers '("^ " ido-ignore-c-mode))
  377. ;; Examples for setting the value of ido-ignore-files
  378. ;;(setq ido-ignore-files '("^ " "\\.c\\'" "\\.h\\'"))
  379. (defcustom ido-default-file-method 'raise-frame
  380. "How to visit a new file when using `ido-find-file'.
  381. Possible values:
  382. `selected-window' Show new file in selected window
  383. `other-window' Show new file in another window (same frame)
  384. `display' Display file in another window without selecting to it
  385. `other-frame' Show new file in another frame
  386. `maybe-frame' If a file is visible in another frame, prompt to ask if you
  387. you want to see the file in the same window of the current
  388. frame or in the other frame
  389. `raise-frame' If a file is visible in another frame, raise that
  390. frame; otherwise, visit the file in the same window"
  391. :type '(choice (const :tag "Visit in selected window" selected-window)
  392. (const :tag "Visit in other window" other-window)
  393. (const :tag "Display (no select) in other window" display)
  394. (const :tag "Visit in other frame" other-frame)
  395. (const :tag "Ask to visit in other frame" maybe-frame)
  396. (const :tag "Raise frame if already visited" raise-frame))
  397. :group 'ido)
  398. (defcustom ido-default-buffer-method 'raise-frame
  399. "How to switch to new buffer when using `ido-switch-buffer'.
  400. See `ido-default-file-method' for details."
  401. :type '(choice (const :tag "Show in selected window" selected-window)
  402. (const :tag "Show in other window" other-window)
  403. (const :tag "Display (no select) in other window" display)
  404. (const :tag "Show in other frame" other-frame)
  405. (const :tag "Ask to show in other frame" maybe-frame)
  406. (const :tag "Raise frame if already shown" raise-frame))
  407. :group 'ido)
  408. (defcustom ido-enable-flex-matching nil
  409. "Non-nil means that `ido' will do flexible string matching.
  410. Flexible matching means that if the entered string does not
  411. match any item, any item containing the entered characters
  412. in the given sequence will match."
  413. :type 'boolean
  414. :group 'ido)
  415. (defcustom ido-enable-regexp nil
  416. "Non-nil means that `ido' will do regexp matching.
  417. Value can be toggled within `ido' using `ido-toggle-regexp'."
  418. :type 'boolean
  419. :group 'ido)
  420. (defcustom ido-enable-prefix nil
  421. "Non-nil means only match if the entered text is a prefix of file name.
  422. This behavior is like the standard Emacs completion.
  423. If nil, match if the entered text is an arbitrary substring.
  424. Value can be toggled within `ido' using `ido-toggle-prefix'."
  425. :type 'boolean
  426. :group 'ido)
  427. (defcustom ido-enable-dot-prefix nil
  428. "Non-nil means to match leading dot as prefix.
  429. I.e. hidden files and buffers will match only if you type a dot
  430. as first char even if `ido-enable-prefix' is nil."
  431. :type 'boolean
  432. :group 'ido)
  433. (defcustom ido-confirm-unique-completion nil
  434. "Non-nil means that even a unique completion must be confirmed.
  435. This means that \\[ido-complete] must always be followed by \\[ido-exit-minibuffer]
  436. even when there is only one unique completion."
  437. :type 'boolean
  438. :group 'ido)
  439. (defcustom ido-cannot-complete-command 'ido-completion-help
  440. "Command run when `ido-complete' can't complete any more.
  441. The most useful values are `ido-completion-help', which pops up a
  442. window with completion alternatives, or `ido-next-match' or
  443. `ido-prev-match', which cycle the buffer list."
  444. :type 'function
  445. :group 'ido)
  446. (defcustom ido-record-commands t
  447. "Non-nil means that `ido' will record commands in command history.
  448. Note that the non-ido equivalent command is recorded."
  449. :type 'boolean
  450. :group 'ido)
  451. (defcustom ido-max-prospects 12
  452. "Non-zero means that the prospect list will be limited to that number of items.
  453. For a long list of prospects, building the full list for the minibuffer can take a
  454. non-negligible amount of time; setting this variable reduces that time."
  455. :type 'integer
  456. :group 'ido)
  457. (defcustom ido-max-file-prompt-width 0.35
  458. "Non-zero means that the prompt string be limited to that number of characters.
  459. If value is a floating point number, it specifies a fraction of the frame width."
  460. :type '(choice
  461. (integer :tag "Characters" :value 20)
  462. (restricted-sexp :tag "Fraction of frame width"
  463. :value 0.35
  464. :match-alternatives (ido-fractionp)))
  465. :group 'ido)
  466. (defcustom ido-max-window-height nil
  467. "Non-nil specifies a value to override `max-mini-window-height'."
  468. :type '(choice
  469. (const :tag "Don't override" nil)
  470. (integer :tag "Number of lines" :value 1)
  471. (restricted-sexp
  472. :tag "Fraction of window height"
  473. :value 0.25
  474. :match-alternatives (ido-fractionp)))
  475. :group 'ido)
  476. (defcustom ido-enable-last-directory-history t
  477. "Non-nil means that `ido' will remember latest selected directory names.
  478. See `ido-last-directory-list' and `ido-save-directory-list-file'."
  479. :type 'boolean
  480. :group 'ido)
  481. (defcustom ido-max-work-directory-list 50
  482. "Maximum number of working directories to record.
  483. This is the list of directories where files have most recently been opened.
  484. See `ido-work-directory-list' and `ido-save-directory-list-file'."
  485. :type 'integer
  486. :group 'ido)
  487. (defcustom ido-work-directory-list-ignore-regexps nil
  488. "List of regexps matching directories which should not be recorded.
  489. Directory names matched by one of the regexps in this list are not inserted in
  490. the `ido-work-directory-list' list."
  491. :type '(repeat regexp)
  492. :group 'ido)
  493. (defcustom ido-use-filename-at-point nil
  494. "Non-nil means that ido shall look for a filename at point.
  495. May use `ffap-guesser' to guess whether text at point is a filename.
  496. If found, use that as the starting point for filename selection."
  497. :type '(choice
  498. (const :tag "Disabled" nil)
  499. (const :tag "Guess filename" guess)
  500. (other :tag "Use literal filename" t))
  501. :group 'ido)
  502. (defcustom ido-use-url-at-point nil
  503. "Non-nil means that ido shall look for a URL at point.
  504. If found, call `find-file-at-point' to visit it."
  505. :type 'boolean
  506. :group 'ido)
  507. (defcustom ido-enable-tramp-completion t
  508. "Non-nil means that ido shall perform tramp method and server name completion.
  509. A tramp file name uses the following syntax: /method:user@host:filename."
  510. :type 'boolean
  511. :group 'ido)
  512. (defcustom ido-record-ftp-work-directories t
  513. "Non-nil means record ftp file names in the work directory list."
  514. :type 'boolean
  515. :group 'ido)
  516. (defcustom ido-merge-ftp-work-directories nil
  517. "If nil, merging ignores ftp file names in the work directory list."
  518. :type 'boolean
  519. :group 'ido)
  520. (defcustom ido-cache-ftp-work-directory-time 1.0
  521. "Maximum time to cache contents of an ftp directory (in hours).
  522. Use C-l in prompt to refresh list.
  523. If zero, ftp directories are not cached."
  524. :type 'number
  525. :group 'ido)
  526. (defcustom ido-slow-ftp-hosts nil
  527. "List of slow ftp hosts where ido prompting should not be used.
  528. If an ftp host is on this list, ido automatically switches to the non-ido
  529. equivalent function, e.g. `find-file' rather than `ido-find-file'."
  530. :type '(repeat string)
  531. :group 'ido)
  532. (defcustom ido-slow-ftp-host-regexps nil
  533. "List of regexps matching slow ftp hosts (see `ido-slow-ftp-hosts')."
  534. :type '(repeat regexp)
  535. :group 'ido)
  536. (defvar ido-unc-hosts-cache t
  537. "Cached value from `ido-unc-hosts' function.")
  538. (defcustom ido-unc-hosts nil
  539. "List of known UNC host names to complete after initial //.
  540. If value is a function, that function is called to search network for
  541. hosts on first use of UNC path."
  542. :type '(choice (repeat :tag "List of UNC host names" string)
  543. (function-item :tag "Use `NET VIEW'"
  544. :value ido-unc-hosts-net-view)
  545. (function :tag "Your own function"))
  546. :set #'(lambda (symbol value)
  547. (set symbol value)
  548. (setq ido-unc-hosts-cache t))
  549. :group 'ido)
  550. (defcustom ido-downcase-unc-hosts t
  551. "Non-nil if UNC host names should be downcased."
  552. :type 'boolean
  553. :group 'ido)
  554. (defcustom ido-ignore-unc-host-regexps nil
  555. "List of regexps matching UNC hosts to ignore.
  556. Case is ignored if `ido-downcase-unc-hosts' is set."
  557. :type '(repeat regexp)
  558. :group 'ido)
  559. (defcustom ido-cache-unc-host-shares-time 8.0
  560. "Maximum time to cache shares of an UNC host (in hours).
  561. Use C-l in prompt to refresh list.
  562. If zero, UNC host shares are not cached."
  563. :type 'number
  564. :group 'ido)
  565. (defcustom ido-max-work-file-list 10
  566. "Maximum number of names of recently opened files to record.
  567. This is the list of the file names (sans directory) which have most recently
  568. been opened. See `ido-work-file-list' and `ido-save-directory-list-file'."
  569. :type 'integer
  570. :group 'ido)
  571. (defcustom ido-work-directory-match-only t
  572. "Non-nil means to skip non-matching directories in the directory history.
  573. When some text is already entered at the `ido-find-file' prompt, using
  574. \\[ido-prev-work-directory] or \\[ido-next-work-directory] will skip directories
  575. without any matching entries."
  576. :type 'boolean
  577. :group 'ido)
  578. (defcustom ido-auto-merge-work-directories-length 0
  579. "Automatically switch to merged work directories during file name input.
  580. The value is number of characters to type before switching to merged mode.
  581. If zero, the switch happens when no matches are found in the current directory.
  582. Automatic merging is disabled if the value is negative."
  583. :type 'integer
  584. :group 'ido)
  585. (defcustom ido-auto-merge-delay-time 0.70
  586. "Delay in seconds to wait for more input before doing auto merge."
  587. :type 'number
  588. :group 'ido)
  589. (defcustom ido-auto-merge-inhibit-characters-regexp "[][*?~]"
  590. "Regexp matching characters which should inhibit automatic merging.
  591. When a (partial) file name matches this regexp, merging is inhibited."
  592. :type 'regexp
  593. :group 'ido)
  594. (defcustom ido-merged-indicator "^"
  595. "The string appended to first choice if it has multiple directory choices."
  596. :type 'string
  597. :group 'ido)
  598. (defcustom ido-max-dir-file-cache 100
  599. "Maximum number of working directories to be cached.
  600. This is the size of the cache of `file-name-all-completions' results.
  601. Each cache entry is time stamped with the modification time of the
  602. directory. Some systems, like Windows, have unreliable directory
  603. modification times, so you may choose to disable caching on such
  604. systems, or explicitly refresh the cache contents using the command
  605. `ido-reread-directory' command (C-l) in the minibuffer.
  606. See also `ido-dir-file-cache' and `ido-save-directory-list-file'."
  607. :type 'integer
  608. :group 'ido)
  609. (defcustom ido-max-directory-size 30000
  610. "Maximum size (in bytes) for directories to use ido completion.
  611. If you enter a directory with a size larger than this size, ido will
  612. not provide the normal completion. To show the completions, use C-a."
  613. :type '(choice (const :tag "No limit" nil)
  614. (integer :tag "Size in bytes" 30000))
  615. :group 'ido)
  616. (defcustom ido-rotate-file-list-default nil
  617. "Non-nil means that `ido' will always rotate file list to get default in front."
  618. :type 'boolean
  619. :group 'ido)
  620. (defcustom ido-enter-matching-directory 'only
  621. "Additional methods to enter sub-directory of first/only matching item.
  622. If value is 'first, enter first matching sub-directory when typing a slash.
  623. If value is 'only, typing a slash only enters the sub-directory if it is
  624. the only matching item.
  625. If value is t, automatically enter a sub-directory when it is the only
  626. matching item, even without typing a slash."
  627. :type '(choice (const :tag "Never" nil)
  628. (const :tag "Slash enters first directory" first)
  629. (const :tag "Slash enters first and only directory" only)
  630. (other :tag "Always enter unique directory" t))
  631. :group 'ido)
  632. (defcustom ido-create-new-buffer 'prompt
  633. "Specify whether a new buffer is created if no buffer matches substring.
  634. Choices are 'always to create new buffers unconditionally, 'prompt to
  635. ask user whether to create buffer, or 'never to never create new buffer."
  636. :type '(choice (const always)
  637. (const prompt)
  638. (const never))
  639. :group 'ido)
  640. (defcustom ido-setup-hook nil
  641. "Hook run after the ido variables and keymap have been setup.
  642. The dynamic variable `ido-cur-item' contains the current type of item that
  643. is read by ido; possible values are file, dir, buffer, and list.
  644. Additional keys can be defined in `ido-completion-map'."
  645. :type 'hook
  646. :group 'ido)
  647. (defcustom ido-separator nil
  648. "String used by ido to separate the alternatives in the minibuffer.
  649. Obsolete. Set 3rd element of `ido-decorations' instead."
  650. :type '(choice string (const nil))
  651. :group 'ido)
  652. (defcustom ido-decorations '( "{" "}" " | " " | ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")
  653. "List of strings used by ido to display the alternatives in the minibuffer.
  654. There are 11 elements in this list:
  655. 1st and 2nd elements are used as brackets around the prospect list,
  656. 3rd element is the separator between prospects (ignored if `ido-separator' is set),
  657. 4th element is the string inserted at the end of a truncated list of prospects,
  658. 5th and 6th elements are used as brackets around the common match string which
  659. can be completed using TAB,
  660. 7th element is the string displayed when there are no matches, and
  661. 8th element is displayed if there is a single match (and faces are not used),
  662. 9th element is displayed when the current directory is non-readable,
  663. 10th element is displayed when directory exceeds `ido-max-directory-size',
  664. 11th element is displayed to confirm creating new file or buffer."
  665. :type '(repeat string)
  666. :group 'ido)
  667. (defcustom ido-use-virtual-buffers nil
  668. "If non-nil, refer to past buffers as well as existing ones.
  669. Essentially it works as follows: Say you are visiting a file and
  670. the buffer gets cleaned up by midnight.el. Later, you want to
  671. switch to that buffer, but find it's no longer open. With
  672. virtual buffers enabled, the buffer name stays in the buffer
  673. list (using the `ido-virtual' face, and always at the end), and if
  674. you select it, it opens the file back up again. This allows you
  675. to think less about whether recently opened files are still open
  676. or not. Most of the time you can quit Emacs, restart, and then
  677. switch to a file buffer that was previously open as if it still
  678. were.
  679. This feature relies upon the `recentf' package, which will be
  680. enabled if this variable is configured to a non-nil value."
  681. :version "24.1"
  682. :type 'boolean
  683. :group 'ido)
  684. (defcustom ido-use-faces t
  685. "Non-nil means use ido faces to highlighting first match, only match and
  686. subdirs in the alternatives."
  687. :type 'boolean
  688. :group 'ido)
  689. (defface ido-first-match '((t (:bold t)))
  690. "Face used by ido for highlighting first match."
  691. :group 'ido)
  692. (defface ido-only-match '((((class color))
  693. (:foreground "ForestGreen"))
  694. (t (:italic t)))
  695. "Face used by ido for highlighting only match."
  696. :group 'ido)
  697. (defface ido-subdir '((((min-colors 88) (class color))
  698. (:foreground "red1"))
  699. (((class color))
  700. (:foreground "red"))
  701. (t (:underline t)))
  702. "Face used by ido for highlighting subdirs in the alternatives."
  703. :group 'ido)
  704. (defface ido-virtual '((t (:inherit font-lock-builtin-face)))
  705. "Face used by ido for matching virtual buffer names."
  706. :version "24.1"
  707. :group 'ido)
  708. (defface ido-indicator '((((min-colors 88) (class color))
  709. (:foreground "yellow1"
  710. :background "red1"
  711. :width condensed))
  712. (((class color))
  713. (:foreground "yellow"
  714. :background "red"
  715. :width condensed))
  716. (t (:inverse-video t)))
  717. "Face used by ido for highlighting its indicators."
  718. :group 'ido)
  719. (defface ido-incomplete-regexp
  720. '((t
  721. (:inherit font-lock-warning-face)))
  722. "Ido face for indicating incomplete regexps."
  723. :group 'ido)
  724. (defcustom ido-make-file-list-hook nil
  725. "List of functions to run when the list of matching files is created.
  726. Each function on the list may modify the dynamically bound variable
  727. `ido-temp-list' which contains the current list of matching files."
  728. :type 'hook
  729. :group 'ido)
  730. (defcustom ido-make-dir-list-hook nil
  731. "List of functions to run when the list of matching directories is created.
  732. Each function on the list may modify the dynamically bound variable
  733. `ido-temp-list' which contains the current list of matching directories."
  734. :type 'hook
  735. :group 'ido)
  736. (defcustom ido-make-buffer-list-hook nil
  737. "List of functions to run when the list of matching buffers is created.
  738. Each function on the list may modify the dynamically bound variable
  739. `ido-temp-list' which contains the current list of matching buffer names."
  740. :type 'hook
  741. :group 'ido)
  742. (defcustom ido-rewrite-file-prompt-functions nil
  743. "List of functions to run when the find-file prompt is created.
  744. Each function on the list may modify the following dynamically bound
  745. variables:
  746. dirname - the (abbreviated) directory name
  747. to be modified by the hook functions
  748. max-width - the max width of the resulting dirname; nil means no limit
  749. prompt - the basic prompt (e.g. \"Find File: \")
  750. literal - the string shown if doing \"literal\" find; set to nil to omit
  751. vc-off - the string shown if version control is inhibited; set to nil to omit
  752. prefix - either nil or a fixed prefix for the dirname
  753. The following variables are available, but should not be changed:
  754. `ido-current-directory' - the unabbreviated directory name
  755. item - equals `file' or `dir' depending on the current mode."
  756. :type 'hook
  757. :group 'ido)
  758. (defvar ido-rewrite-file-prompt-rules nil
  759. "*Alist of rewriting rules for directory names in ido prompts.
  760. A list of elements of the form (FROM . TO) or (FROM . FUNC), each
  761. meaning to rewrite the directory name if matched by FROM by either
  762. substituting the matched string by TO or calling the function FUNC
  763. with the current directory name as its only argument and using the
  764. return value as the new directory name. In addition, each FUNC may
  765. also modify the dynamic variables described for the variable
  766. `ido-rewrite-file-prompt-functions'.")
  767. (defcustom ido-completion-buffer "*Ido Completions*"
  768. "Name of completion buffer used by ido.
  769. Set to nil to disable completion buffers popping up."
  770. :type 'string
  771. :group 'ido)
  772. (defcustom ido-completion-buffer-all-completions nil
  773. "Non-nil means to show all completions in completion buffer.
  774. Otherwise, only the current list of matches is shown."
  775. :type 'boolean
  776. :group 'ido)
  777. (defvar ido-all-frames 'visible
  778. "*Argument to pass to `walk-windows' when finding visible files.
  779. See documentation of `walk-windows' for useful values.")
  780. (defcustom ido-minibuffer-setup-hook nil
  781. "Ido-specific customization of minibuffer setup.
  782. This hook is run during minibuffer setup if `ido' is active.
  783. It is intended for use in customizing ido for interoperation
  784. with other packages. For instance:
  785. \(add-hook 'ido-minibuffer-setup-hook
  786. \(function
  787. \(lambda ()
  788. \(make-local-variable 'max-mini-window-height)
  789. \(setq max-mini-window-height 3))))
  790. will constrain Emacs to a maximum minibuffer height of 3 lines when
  791. ido is running. Copied from `icomplete-minibuffer-setup-hook'."
  792. :type 'hook
  793. :group 'ido)
  794. (defcustom ido-save-directory-list-file (convert-standard-filename "~/.ido.last")
  795. "File in which the ido state is saved between invocations.
  796. Variables stored are: `ido-last-directory-list', `ido-work-directory-list',
  797. `ido-work-file-list', and `ido-dir-file-cache'.
  798. Must be set before enabling ido mode."
  799. :type 'string
  800. :group 'ido)
  801. (defcustom ido-read-file-name-as-directory-commands '()
  802. "List of commands which uses `read-file-name' to read a directory name.
  803. When `ido-everywhere' is non-nil, the commands in this list will read
  804. the directory using `ido-read-directory-name'."
  805. :type '(repeat symbol)
  806. :group 'ido)
  807. (defcustom ido-read-file-name-non-ido '()
  808. "List of commands which shall not read file names the ido way.
  809. When `ido-everywhere' is non-nil, the commands in this list will read
  810. the file name using normal `read-file-name' style."
  811. :type '(repeat symbol)
  812. :group 'ido)
  813. (defcustom ido-before-fallback-functions '()
  814. "List of functions to call before calling a fallback command.
  815. The fallback command is passed as an argument to the functions."
  816. :type 'hook
  817. :group 'ido)
  818. ;;; Internal Variables
  819. ;; Persistent variables
  820. (defvar ido-completion-map nil
  821. "Currently active keymap for ido commands.")
  822. (defvar ido-common-completion-map nil
  823. "Keymap for all ido commands.")
  824. (defvar ido-file-completion-map nil
  825. "Keymap for ido file commands.")
  826. (defvar ido-file-dir-completion-map nil
  827. "Keymap for ido file and directory commands.")
  828. (defvar ido-buffer-completion-map nil
  829. "Keymap for ido buffer commands.")
  830. (defvar ido-file-history nil
  831. "History of files selected using `ido-find-file'.")
  832. (defvar ido-buffer-history nil
  833. "History of buffers selected using `ido-switch-buffer'.")
  834. (defvar ido-last-directory-list nil
  835. "List of last selected directory names.
  836. See `ido-enable-last-directory-history' for details.")
  837. (defvar ido-work-directory-list nil
  838. "List of actual working directory names.
  839. The current directory is inserted at the front of this list whenever a
  840. file is opened with `ido-find-file' and family.")
  841. (defvar ido-work-file-list nil
  842. "List of actual work file names.
  843. Opening a file with `ido-find-file' and similar functions
  844. inserts the current file name (relative to its containing directory)
  845. at the front of this list.")
  846. (defvar ido-dir-file-cache nil
  847. "List of `file-name-all-completions' results.
  848. Each element in the list is of the form (DIR (MTIME) FILE...).")
  849. (defvar ido-ignore-item-temp-list nil
  850. "List of items to ignore in current ido invocation.
  851. Intended to be let-bound by functions which call ido repeatedly.
  852. Should never be set permanently.")
  853. ;; Temporary storage
  854. (defvar ido-eoinput 1
  855. "Point where minibuffer input ends and completion info begins.
  856. Copied from `icomplete-eoinput'.")
  857. (make-variable-buffer-local 'ido-eoinput)
  858. (defvar ido-common-match-string nil
  859. "Stores the string that is common to all matching files.")
  860. (defvar ido-rescan nil
  861. "Non-nil means we need to regenerate the list of matching items.")
  862. (defvar ido-rotate nil
  863. "Non-nil means we are rotating list of matches.")
  864. (defvar ido-text nil
  865. "Stores the users string as it is typed in.")
  866. (defvar ido-text-init nil
  867. "The initial string for the users string it is typed in.")
  868. (defvar ido-input-stack nil
  869. "Stores the users strings when user hits M-b/M-f.")
  870. (defvar ido-matches nil
  871. "List of files currently matching `ido-text'.")
  872. (defvar ido-report-no-match t
  873. "Report [No Match] when no completions matches `ido-text'.")
  874. (defvar ido-exit nil
  875. "Flag to monitor how `ido-find-file' exits.
  876. If equal to `takeprompt', we use the prompt as the file name to be
  877. selected.")
  878. (defvar ido-current-directory nil
  879. "Current directory for `ido-find-file'.")
  880. (defvar ido-auto-merge-timer nil
  881. "Delay timer for auto merge.")
  882. (defvar ido-use-mycompletion-depth 0
  883. "Non-nil means use `ido' completion feedback.
  884. Is set by ido functions to the current `minibuffer-depth',
  885. so that it doesn't interfere with other minibuffer usage.")
  886. (defvar ido-incomplete-regexp nil
  887. "Non-nil if an incomplete regexp is entered.")
  888. (defvar ido-initial-position nil
  889. "Non-nil means to explicitly cursor on entry to minibuffer.
  890. Value is an integer which is number of chars to right of prompt.")
  891. (defvar ido-virtual-buffers nil
  892. "List of virtual buffers, that is, past visited files.
  893. This is a copy of `recentf-list', pared down and with faces applied.
  894. Only used if `ido-use-virtual-buffers' is non-nil.")
  895. ;;; Variables with dynamic bindings.
  896. ;;; Declared here to keep the byte compiler quiet.
  897. ;; Stores the current ido item type ('file, 'dir, 'buffer, or 'list).
  898. (defvar ido-cur-item)
  899. ;;; Stores the current default item
  900. (defvar ido-default-item)
  901. ;; Stores the current list of items that will be searched through.
  902. ;; The list is ordered, so that the most interesting item comes first,
  903. ;; although by default, the files visible in the current frame are put
  904. ;; at the end of the list. Created by `ido-make-item-list'.
  905. (defvar ido-cur-list)
  906. ;; Stores the choice list for ido-completing-read
  907. (defvar ido-choice-list)
  908. ;; Stores the list of items which are ignored when building
  909. ;; `ido-cur-list'. It is in no specific order.
  910. (defvar ido-ignored-list)
  911. ;; Remember if current directory is non-readable (so we cannot do completion).
  912. (defvar ido-directory-nonreadable)
  913. ;; Remember if current directory is 'huge' (so we don't want to do completion).
  914. (defvar ido-directory-too-big)
  915. ;; Keep current item list if non-nil.
  916. (defvar ido-keep-item-list)
  917. ;; Process ido-ignore-* lists.
  918. (defvar ido-process-ignore-lists)
  919. ;; Don't process ido-ignore- lists once.
  920. (defvar ido-process-ignore-lists-inhibit)
  921. ;; Buffer from which ido was entered.
  922. (defvar ido-entry-buffer)
  923. ;; Non-nil if matching file must be selected.
  924. (defvar ido-require-match)
  925. ;; Non-nil if we should add [confirm] to prompt
  926. (defvar ido-show-confirm-message)
  927. ;; Stores a temporary version of the file list being created.
  928. (defvar ido-temp-list)
  929. ;; Non-nil if default list element should be rotated into place.
  930. (defvar ido-rotate-temp)
  931. ;; Stores current index in ido-work-directory-list.
  932. (defvar ido-work-directory-index)
  933. ;; Stores current index in ido-work-file-list.
  934. (defvar ido-work-file-index)
  935. ;; Set when merged work directory list is in use.
  936. (defvar ido-use-merged-list)
  937. ;; Set when merged work directory list not yet built.
  938. (defvar ido-try-merged-list)
  939. ;; Saved state prior to last work directory merge.
  940. ;; Value is a list (ido-text dir cur-list ignored-list matches).
  941. (defvar ido-pre-merge-state)
  942. ;; Original value of vc-handled-backends for use in ido-toggle-vc.
  943. (defvar ido-saved-vc-hb)
  944. ;; Stores temporary state of literal find file.
  945. (defvar ido-find-literal)
  946. ;; Set to 'ignore to inhibit switching between find-file/switch-buffer.
  947. (defvar ido-context-switch-command)
  948. ;; Dynamically bound in ido-read-internal.
  949. (defvar ido-completing-read)
  950. ;;; FUNCTIONS
  951. (defun ido-active (&optional merge)
  952. (if merge
  953. ido-use-merged-list
  954. (and (boundp 'ido-completing-read)
  955. (or (featurep 'xemacs)
  956. (= ido-use-mycompletion-depth (minibuffer-depth))))))
  957. (defvar ido-trace-enable nil)
  958. (defun ido-trace (p &optional s retval)
  959. (if ido-trace-enable
  960. (let ((b (get-buffer-create " *IDO Trace*"))
  961. (deactivate-mark deactivate-mark))
  962. (save-excursion
  963. (save-restriction
  964. (set-buffer b)
  965. (insert p ": " (if (stringp s) s (format "%S" s)) "\n")))))
  966. retval)
  967. (defun ido-toggle-trace (arg)
  968. (interactive "P")
  969. (setq ido-trace-enable (or arg (not ido-trace-enable)))
  970. (if ido-trace-enable
  971. (message "IDO trace on"))
  972. (let ((b (get-buffer " *IDO Trace*")))
  973. (if b
  974. (if ido-trace-enable
  975. (kill-buffer b)
  976. (pop-to-buffer b t t)
  977. (setq truncate-lines t)))))
  978. (defun ido-local-file-exists-p (file)
  979. "Tell if FILE exists locally."
  980. (let (file-name-handler-alist)
  981. (file-exists-p file)))
  982. (defun ido-unc-hosts (&optional query)
  983. "Return list of UNC host names."
  984. (let ((hosts
  985. (cond
  986. ((listp ido-unc-hosts)
  987. ido-unc-hosts) ;; static list or nil
  988. ((listp ido-unc-hosts-cache)
  989. ido-unc-hosts-cache) ;; result of net search
  990. ((and query (fboundp ido-unc-hosts))
  991. (message (propertize "Searching for UNC hosts..." 'face 'highlight))
  992. (setq ido-unc-hosts-cache (funcall ido-unc-hosts))
  993. (message nil)
  994. ido-unc-hosts-cache)
  995. (query
  996. (setq ido-unc-hosts-cache nil))
  997. (t (fboundp ido-unc-hosts)))))
  998. (when query
  999. (let ((case-fold-search ido-downcase-unc-hosts)
  1000. res host re-list re)
  1001. (while hosts
  1002. (setq host (car hosts)
  1003. hosts (cdr hosts)
  1004. re-list (and ido-process-ignore-lists
  1005. ido-ignore-unc-host-regexps))
  1006. (while re-list
  1007. (setq re (car re-list)
  1008. re-list (cdr re-list))
  1009. (if (string-match re host)
  1010. (setq re-list nil
  1011. host nil)))
  1012. (when host
  1013. (when ido-downcase-unc-hosts
  1014. (setq host (downcase host)))
  1015. (setq res (cons host res))))
  1016. (setq hosts (sort res #'string<))))
  1017. hosts))
  1018. (defun ido-unc-hosts-net-view ()
  1019. "Query network for list of UNC host names using `NET VIEW'."
  1020. (let (hosts)
  1021. (with-temp-buffer
  1022. (shell-command "net view" t)
  1023. (goto-char (point-min))
  1024. (while (re-search-forward "^\\\\\\\\\\([[:graph:]]+\\)" nil t)
  1025. (setq hosts (cons (match-string 1) hosts))))
  1026. hosts))
  1027. (defun ido-is-tramp-root (&optional dir)
  1028. (and ido-enable-tramp-completion
  1029. (string-match "\\`/[^/]+[@:]\\'"
  1030. (or dir ido-current-directory))))
  1031. (defun ido-is-unc-root (&optional dir)
  1032. (and (ido-unc-hosts)
  1033. (string-equal "//"
  1034. (or dir ido-current-directory))))
  1035. (defun ido-is-unc-host (&optional dir)
  1036. (and (ido-unc-hosts)
  1037. (string-match "\\`//[^/]+/\\'"
  1038. (or dir ido-current-directory))))
  1039. (defun ido-is-root-directory (&optional dir)
  1040. (setq dir (or dir ido-current-directory))
  1041. (or
  1042. (string-equal "/" dir)
  1043. (and (memq system-type '(windows-nt ms-dos))
  1044. (string-match "\\`[a-zA-Z]:[/\\]\\'" dir))
  1045. (if ido-enable-tramp-completion
  1046. (ido-is-tramp-root dir)
  1047. (string-match "\\`/[^:/][^:/]+:\\'" dir))))
  1048. (defun ido-is-ftp-directory (&optional dir)
  1049. (string-match
  1050. (if ido-enable-tramp-completion
  1051. "\\`/[^/:][^/:]+:" ;; like tramp-file-name-regexp-unified, but doesn't match single drive letters
  1052. "\\`/[^/:][^/:]+:/")
  1053. (or dir ido-current-directory)))
  1054. (defun ido-is-slow-ftp-host (&optional dir)
  1055. (and (or ido-slow-ftp-hosts ido-slow-ftp-host-regexps)
  1056. (setq dir (or dir ido-current-directory))
  1057. ;; (featurep 'ange-ftp)
  1058. ;; (ange-ftp-ftp-name dir)
  1059. (string-match
  1060. (if ido-enable-tramp-completion
  1061. "\\`/\\([^/]+[@:]\\)*\\([^@/:][^@/:]+\\):"
  1062. "\\`/\\([^/:]*@\\)?\\([^@/:][^@/:]+\\):/")
  1063. dir)
  1064. (let ((host (substring dir (match-beginning 2) (match-end 2))))
  1065. (or (member host ido-slow-ftp-hosts)
  1066. (let ((re ido-slow-ftp-host-regexps))
  1067. (while (and re (not (string-match (car re) host)))
  1068. (setq re (cdr re)))
  1069. re)))))
  1070. (defun ido-time-stamp (&optional time)
  1071. ;; Time is a floating point number (fractions of 1 hour)
  1072. (setq time (or time (current-time)))
  1073. (/ (+ (* (car time) 65536.0) (car (cdr time))) 3600.0))
  1074. (defun ido-cache-ftp-valid (&optional time)
  1075. (and (numberp ido-cache-ftp-work-directory-time)
  1076. (> ido-cache-ftp-work-directory-time 0)
  1077. (or (not time)
  1078. (< (- (ido-time-stamp) time) ido-cache-ftp-work-directory-time))))
  1079. (defun ido-cache-unc-valid (&optional time)
  1080. (and (numberp ido-cache-unc-host-shares-time)
  1081. (> ido-cache-unc-host-shares-time 0)
  1082. (or (not time)
  1083. (< (- (ido-time-stamp) time) ido-cache-unc-host-shares-time))))
  1084. (defun ido-may-cache-directory (&optional dir)
  1085. (setq dir (or dir ido-current-directory))
  1086. (cond
  1087. ((and (ido-is-root-directory dir)
  1088. (or ido-enable-tramp-completion
  1089. (memq system-type '(windows-nt ms-dos))))
  1090. nil)
  1091. ((ido-is-unc-host dir)
  1092. (ido-cache-unc-valid))
  1093. ((ido-is-ftp-directory dir)
  1094. (ido-cache-ftp-valid))
  1095. ((ido-directory-too-big-p dir)
  1096. nil)
  1097. (t t)))
  1098. (defun ido-pp (list &optional sep)
  1099. (let ((print-level nil) (eval-expression-print-level nil)
  1100. (print-length nil) (eval-expression-print-length nil))
  1101. (insert "\n;; ----- " (symbol-name list) " -----\n(\n ")
  1102. (setq list (symbol-value list))
  1103. (while list
  1104. (let* ((elt (car list))
  1105. (s (if (consp elt) (car elt) elt)))
  1106. (if (and (stringp s) (= (length s) 0))
  1107. (setq s nil))
  1108. (if s
  1109. (prin1 elt (current-buffer)))
  1110. (if (and (setq list (cdr list)) s)
  1111. (insert (or sep "\n ")))))
  1112. (insert "\n)\n")))
  1113. (defun ido-save-history ()
  1114. "Save ido history and cache information between sessions."
  1115. (interactive)
  1116. (when (and ido-last-directory-list ido-save-directory-list-file)
  1117. (let ((buf (get-buffer-create " *ido session*"))
  1118. (version-control 'never))
  1119. (unwind-protect
  1120. (with-current-buffer buf
  1121. (erase-buffer)
  1122. (insert ";;; -*- coding: utf-8 -*-\n")
  1123. (setq buffer-file-coding-system 'utf-8)
  1124. (ido-pp 'ido-last-directory-list)
  1125. (ido-pp 'ido-work-directory-list)
  1126. (ido-pp 'ido-work-file-list)
  1127. (ido-pp 'ido-dir-file-cache "\n\n ")
  1128. (if (listp ido-unc-hosts-cache)
  1129. (ido-pp 'ido-unc-hosts-cache)
  1130. (insert "\n;; ----- ido-unc-hosts-cache -----\nt\n"))
  1131. (write-file ido-save-directory-list-file nil))
  1132. (kill-buffer buf)))))
  1133. (defun ido-load-history (&optional arg)
  1134. "Load ido history and cache information from previous session.
  1135. With prefix argument, reload history unconditionally."
  1136. (interactive "P")
  1137. (if (or arg (and ido-save-directory-list-file (not ido-last-directory-list)))
  1138. (let ((file (expand-file-name ido-save-directory-list-file))
  1139. buf)
  1140. (when (file-readable-p file)
  1141. (setq buf (get-buffer-create " *ido session*"))
  1142. (unwind-protect
  1143. (with-current-buffer buf
  1144. (erase-buffer)
  1145. (insert-file-contents file)
  1146. (condition-case nil
  1147. (setq ido-last-directory-list (read (current-buffer))
  1148. ido-work-directory-list (read (current-buffer))
  1149. ido-work-file-list (read (current-buffer))
  1150. ido-dir-file-cache (read (current-buffer))
  1151. ido-unc-hosts-cache (read (current-buffer)))
  1152. (error nil)))
  1153. (kill-buffer buf)))))
  1154. (ido-wash-history))
  1155. (defun ido-wash-history ()
  1156. "Clean-up ido history and cache information.
  1157. Removes badly formatted data and ignored directories."
  1158. (interactive)
  1159. ;; Check format of each of our lists, discard bogus elements
  1160. (setq ido-last-directory-list
  1161. (and (listp ido-last-directory-list)
  1162. (let ((l ido-last-directory-list) r)
  1163. (while l
  1164. (if (and (consp (car l))
  1165. (stringp (car (car l)))
  1166. (stringp (cdr (car l))))
  1167. (setq r (cons (car l) r)))
  1168. (setq l (cdr l)))
  1169. (nreverse r))))
  1170. (setq ido-work-directory-list
  1171. (and (listp ido-work-directory-list)
  1172. (let ((l ido-work-directory-list) r)
  1173. (while l
  1174. (if (and (stringp (car l))
  1175. (or ido-record-ftp-work-directories
  1176. (not (ido-is-ftp-directory (car l)))))
  1177. (setq r (cons (car l) r)))
  1178. (setq l (cdr l)))
  1179. (nreverse r))))
  1180. (setq ido-work-file-list
  1181. (and (listp ido-work-file-list)
  1182. (let ((l ido-work-file-list) r)
  1183. (while l
  1184. (if (stringp (car l))
  1185. (setq r (cons (car l) r)))
  1186. (setq l (cdr l)))
  1187. (nreverse r))))
  1188. (setq ido-dir-file-cache
  1189. (and (listp ido-dir-file-cache)
  1190. (let ((l ido-dir-file-cache) r)
  1191. (while l
  1192. (if (and (listp (car l))
  1193. (> (length (car l)) 2)
  1194. (let ((dir (car (car l)))
  1195. (time (car (cdr (car l))))
  1196. (files (cdr (cdr (car l)))))
  1197. (and
  1198. (stringp dir)
  1199. (consp time)
  1200. (cond
  1201. ((integerp (car time))
  1202. (and (/= (car time) 0)
  1203. (integerp (car (cdr time)))
  1204. (/= (car (cdr time)) 0)
  1205. (ido-may-cache-directory dir)))
  1206. ((eq (car time) 'ftp)
  1207. (and (numberp (cdr time))
  1208. (ido-is-ftp-directory dir)
  1209. (ido-cache-ftp-valid (cdr time))))
  1210. ((eq (car time) 'unc)
  1211. (and (numberp (cdr time))
  1212. (ido-is-unc-host dir)
  1213. (ido-cache-unc-valid (cdr time))))
  1214. (t nil))
  1215. (let ((s files) (ok t))
  1216. (while s
  1217. (if (stringp (car s))
  1218. (setq s (cdr s))
  1219. (setq s nil ok nil)))
  1220. ok))))
  1221. (setq r (cons (car l) r)))
  1222. (setq l (cdr l)))
  1223. (nreverse r))))
  1224. ;; Remove ignored directories from work directory list
  1225. ;; according to ido-work-directory-list-ignore-regexps
  1226. (if ido-work-directory-list
  1227. (let ((dirs (reverse ido-work-directory-list)))
  1228. (setq ido-work-directory-list nil)
  1229. (while dirs
  1230. (ido-record-work-directory (car dirs))
  1231. (setq dirs (cdr dirs)))))
  1232. ;; Get rid of text properties
  1233. (let ((l ido-last-directory-list) e)
  1234. (while l
  1235. (setq e (car l) l (cdr l))
  1236. (set-text-properties 0 (length (car e)) nil (car e))
  1237. (set-text-properties 0 (length (cdr e)) nil (cdr e))))
  1238. (let ((l ido-work-directory-list) e)
  1239. (while l
  1240. (setq e (car l) l (cdr l))
  1241. (set-text-properties 0 (length e) nil e)))
  1242. (let ((l ido-work-file-list) e)
  1243. (while l
  1244. (setq e (car l) l (cdr l))
  1245. (set-text-properties 0 (length e) nil e)))
  1246. (let ((l ido-dir-file-cache) e d)
  1247. (while l
  1248. (setq e (car l) l (cdr l))
  1249. (if (listp e)
  1250. (while e
  1251. (setq d (car e) e (cdr e))
  1252. (if (not (consp d))
  1253. (set-text-properties 0 (length d) nil d)))))))
  1254. (defun ido-kill-emacs-hook ()
  1255. ;; ido kill emacs hook
  1256. (ido-save-history))
  1257. (defun ido-common-initialization ()
  1258. (ido-init-completion-maps)
  1259. (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
  1260. (add-hook 'choose-completion-string-functions 'ido-choose-completion-string))
  1261. (define-minor-mode ido-everywhere
  1262. "Toggle use of Ido for all buffer/file reading.
  1263. With a prefix argument ARG, enable this feature if ARG is
  1264. positive, and disable it otherwise. If called from Lisp, enable
  1265. the mode if ARG is omitted or nil."
  1266. :global t
  1267. :group 'ido
  1268. (when (get 'ido-everywhere 'file)
  1269. (setq read-file-name-function (car (get 'ido-everywhere 'file)))
  1270. (put 'ido-everywhere 'file nil))
  1271. (when (get 'ido-everywhere 'buffer)
  1272. (setq read-buffer-function (car (get 'ido-everywhere 'buffer)))
  1273. (put 'ido-everywhere 'buffer nil))
  1274. (when ido-everywhere
  1275. (when (memq ido-mode '(both file))
  1276. (put 'ido-everywhere 'file (cons read-file-name-function nil))
  1277. (setq read-file-name-function 'ido-read-file-name))
  1278. (when (memq ido-mode '(both buffer))
  1279. (put 'ido-everywhere 'buffer (cons read-buffer-function nil))
  1280. (setq read-buffer-function 'ido-read-buffer))))
  1281. (defvar ido-minor-mode-map-entry nil)
  1282. ;;;###autoload
  1283. (defun ido-mode (&optional arg)
  1284. "Toggle ido mode on or off.
  1285. With ARG, turn ido-mode on if arg is positive, off otherwise.
  1286. Turning on ido-mode will remap (via a minor-mode keymap) the default
  1287. keybindings for the `find-file' and `switch-to-buffer' families of
  1288. commands to the ido versions of these functions.
  1289. However, if ARG arg equals 'files, remap only commands for files, or
  1290. if it equals 'buffers, remap only commands for buffer switching.
  1291. This function also adds a hook to the minibuffer."
  1292. (interactive "P")
  1293. (setq ido-mode
  1294. (cond
  1295. ((null arg) (if ido-mode nil 'both))
  1296. ((eq arg t) 'both)
  1297. ((eq arg 'files) 'file)
  1298. ((eq arg 'buffers) 'buffer)
  1299. ((memq arg '(file buffer both)) arg)
  1300. ((> (prefix-numeric-value arg) 0) 'both)
  1301. (t nil)))
  1302. (ido-everywhere (if ido-everywhere 1 -1))
  1303. (when ido-mode
  1304. (ido-common-initialization)
  1305. (ido-load-history)
  1306. (add-hook 'kill-emacs-hook 'ido-kill-emacs-hook)
  1307. (let ((map (make-sparse-keymap)))
  1308. (when (memq ido-mode '(file both))
  1309. (define-key map [remap find-file] 'ido-find-file)
  1310. (define-key map [remap find-file-read-only] 'ido-find-file-read-only)
  1311. (define-key map [remap find-alternate-file] 'ido-find-alternate-file)
  1312. (define-key map [remap write-file] 'ido-write-file)
  1313. (define-key map [remap insert-file] 'ido-insert-file)
  1314. (define-key map [remap list-directory] 'ido-list-directory)
  1315. (define-key map [remap dired] 'ido-dired)
  1316. (define-key map [remap find-file-other-window]
  1317. 'ido-find-file-other-window)
  1318. (define-key map [remap find-file-read-only-other-window]
  1319. 'ido-find-file-read-only-other-window)
  1320. (define-key map [remap find-file-other-frame]
  1321. 'ido-find-file-other-frame)
  1322. (define-key map [remap find-file-read-only-other-frame]
  1323. 'ido-find-file-read-only-other-frame))
  1324. (when (memq ido-mode '(buffer both))
  1325. (define-key map [remap switch-to-buffer] 'ido-switch-buffer)
  1326. (define-key map [remap switch-to-buffer-other-window]
  1327. 'ido-switch-buffer-other-window)
  1328. (define-key map [remap switch-to-buffer-other-frame]
  1329. 'ido-switch-buffer-other-frame)
  1330. (define-key map [remap insert-buffer] 'ido-insert-buffer)
  1331. (define-key map [remap kill-buffer] 'ido-kill-buffer)
  1332. (define-key map [remap display-buffer] 'ido-display-buffer))
  1333. (if ido-minor-mode-map-entry
  1334. (setcdr ido-minor-mode-map-entry map)
  1335. (setq ido-minor-mode-map-entry (cons 'ido-mode map))
  1336. (add-to-list 'minor-mode-map-alist ido-minor-mode-map-entry))))
  1337. (when (called-interactively-p 'any)
  1338. (message "Ido mode %s" (if ido-mode "enabled" "disabled"))))
  1339. ;;; IDO KEYMAP
  1340. (defun ido-init-completion-maps ()
  1341. "Set up the completion keymaps used by `ido'."
  1342. ;; Common map
  1343. (let ((map (make-sparse-keymap)))
  1344. (define-key map "\C-a" 'ido-toggle-ignore)
  1345. (define-key map "\C-c" 'ido-toggle-case)
  1346. (define-key map "\C-e" 'ido-edit-input)
  1347. (define-key map "\t" 'ido-complete)
  1348. (define-key map " " 'ido-complete-space)
  1349. (define-key map "\C-j" 'ido-select-text)
  1350. (define-key map "\C-m" 'ido-exit-minibuffer)
  1351. (define-key map "\C-p" 'ido-toggle-prefix)
  1352. (define-key map "\C-r" 'ido-prev-match)
  1353. (define-key map "\C-s" 'ido-next-match)
  1354. (define-key map "\C-t" 'ido-toggle-regexp)
  1355. (define-key map "\C-z" 'ido-undo-merge-work-directory)
  1356. (define-key map [(control ?\s)] 'ido-restrict-to-matches)
  1357. (define-key map [(meta ?\s)] 'ido-take-first-match)
  1358. (define-key map [(control ?@)] 'ido-restrict-to-matches)
  1359. (define-key map [right] 'ido-next-match)
  1360. (define-key map [left] 'ido-prev-match)
  1361. (define-key map "?" 'ido-completion-help)
  1362. ;; Magic commands.
  1363. (define-key map "\C-b" 'ido-magic-backward-char)
  1364. (define-key map "\C-f" 'ido-magic-forward-char)
  1365. (define-key map "\C-d" 'ido-magic-delete-char)
  1366. (set-keymap-parent map minibuffer-local-map)
  1367. (setq ido-common-completion-map map))
  1368. ;; File and directory map
  1369. (let ((map (make-sparse-keymap)))
  1370. (define-key map "\C-x\C-b" 'ido-enter-switch-buffer)
  1371. (define-key map "\C-x\C-f" 'ido-fallback-command)
  1372. (define-key map "\C-x\C-d" 'ido-enter-dired)
  1373. (define-key map [down] 'ido-next-match-dir)
  1374. (define-key map [up] 'ido-prev-match-dir)
  1375. (define-key map [(meta up)] 'ido-prev-work-directory)
  1376. (define-key map [(meta down)] 'ido-next-work-directory)
  1377. (define-key map [backspace] 'ido-delete-backward-updir)
  1378. (define-key map "\d" 'ido-delete-backward-updir)
  1379. (define-key map [remap delete-backward-char] 'ido-delete-backward-updir) ; BS
  1380. (define-key map [remap backward-kill-word] 'ido-delete-backward-word-updir) ; M-DEL
  1381. (define-key map [(control backspace)] 'ido-up-directory)
  1382. (define-key map "\C-l" 'ido-reread-directory)
  1383. (define-key map [(meta ?d)] 'ido-wide-find-dir-or-delete-dir)
  1384. (define-key map [(meta ?b)] 'ido-push-dir)
  1385. (define-key map [(meta ?v)] 'ido-push-dir-first)
  1386. (define-key map [(meta ?f)] 'ido-wide-find-file-or-pop-dir)
  1387. (define-key map [(meta ?k)] 'ido-forget-work-directory)
  1388. (define-key map [(meta ?m)] 'ido-make-directory)
  1389. (define-key map [(meta ?n)] 'ido-next-work-directory)
  1390. (define-key map [(meta ?o)] 'ido-prev-work-file)
  1391. (define-key map [(meta control ?o)] 'ido-next-work-file)
  1392. (define-key map [(meta ?p)] 'ido-prev-work-directory)
  1393. (define-key map [(meta ?s)] 'ido-merge-work-directories)
  1394. (set-keymap-parent map ido-common-completion-map)
  1395. (setq ido-file-dir-completion-map map))
  1396. ;; File only map
  1397. (let ((map (make-sparse-keymap)))
  1398. (define-key map "\C-k" 'ido-delete-file-at-head)
  1399. (define-key map "\C-o" 'ido-copy-current-word)
  1400. (define-key map "\C-w" 'ido-copy-current-file-name)
  1401. (define-key map [(meta ?l)] 'ido-toggle-literal)
  1402. (set-keymap-parent map ido-file-dir-completion-map)
  1403. (setq ido-file-completion-map map))
  1404. ;; Buffer map
  1405. (let ((map (make-sparse-keymap)))
  1406. (define-key map "\C-x\C-f" 'ido-enter-find-file)
  1407. (define-key map "\C-x\C-b" 'ido-fallback-command)
  1408. (define-key map "\C-k" 'ido-kill-buffer-at-head)
  1409. (define-key map "\C-o" 'ido-toggle-virtual-buffers)
  1410. (set-keymap-parent map ido-common-completion-map)
  1411. (setq ido-buffer-completion-map map)))
  1412. (defun ido-setup-completion-map ()
  1413. "Set up the keymap for `ido'."
  1414. ;; generated every time so that it can inherit new functions.
  1415. (let ((map (make-sparse-keymap))
  1416. (viper-p (if (boundp 'viper-mode) viper-mode)))
  1417. (when viper-p
  1418. (define-key map [remap viper-intercept-ESC-key] 'ignore))
  1419. (cond
  1420. ((memq ido-cur-item '(file dir))
  1421. (when ido-context-switch-command
  1422. (define-key map "\C-x\C-b" ido-context-switch-command)
  1423. (define-key map "\C-x\C-d" 'ignore))
  1424. (when viper-p
  1425. (define-key map [remap viper-backward-char] 'ido-delete-backward-updir)
  1426. (define-key map [remap viper-del-backward-char-in-insert] 'ido-delete-backward-updir)
  1427. (define-key map [remap viper-delete-backward-word] 'ido-delete-backward-word-updir))
  1428. (set-keymap-parent map
  1429. (if (eq ido-cur-item 'file)
  1430. ido-file-completion-map
  1431. ido-file-dir-completion-map)))
  1432. ((eq ido-cur-item 'buffer)
  1433. (when ido-context-switch-command
  1434. (define-key map "\C-x\C-f" ido-context-switch-command))
  1435. (set-keymap-parent map ido-buffer-completion-map))
  1436. (t
  1437. (set-keymap-parent map ido-common-completion-map)))
  1438. (setq ido-completion-map map)))
  1439. (defun ido-final-slash (dir &optional fix-it)
  1440. ;; return DIR if DIR has final slash.
  1441. ;; else if FIX-IT is non-nil, return DIR/
  1442. ;; else return nil.
  1443. (setq dir (ido-name dir))
  1444. (cond
  1445. ((string-match "/\\'" dir) dir)
  1446. ((ido-is-tramp-root dir) dir)
  1447. (fix-it (concat dir "/"))
  1448. (t nil)))
  1449. (defun ido-no-final-slash (s)
  1450. ;; Remove optional final slash from string S
  1451. (let ((l (1- (length s))))
  1452. (if (and (> l 0) (eq (aref s l) ?/))
  1453. (substring s 0 l)
  1454. s)))
  1455. (defun ido-nonreadable-directory-p (dir)
  1456. ;; Return t if dir is a directory, but not readable
  1457. ;; Do not check for non-readable directories via tramp, as this causes a premature
  1458. ;; connect on incomplete tramp paths (after entering just method:).
  1459. (let ((ido-enable-tramp-completion nil))
  1460. (and (ido-final-slash dir)
  1461. (not (ido-is-unc-host dir))
  1462. (file-directory-p dir)
  1463. (not (file-readable-p dir)))))
  1464. (defun ido-directory-too-big-p (dir)
  1465. ;; Return t if dir is a directory, but too big to show
  1466. ;; Do not check for non-readable directories via tramp, as this causes a premature
  1467. ;; connect on incomplete tramp paths (after entering just method:).
  1468. (let ((ido-enable-tramp-completion nil))
  1469. (and (numberp ido-max-directory-size)
  1470. (ido-final-slash dir)
  1471. (not (ido-is-unc-host dir))
  1472. (file-directory-p dir)
  1473. (> (nth 7 (file-attributes dir)) ido-max-directory-size))))
  1474. (defun ido-set-current-directory (dir &optional subdir no-merge)
  1475. ;; Set ido's current directory to DIR or DIR/SUBDIR
  1476. (unless (and ido-enable-tramp-completion
  1477. (string-match "\\`/[^/]*@\\'" dir))
  1478. (setq dir (ido-final-slash dir t)))
  1479. (setq ido-use-merged-list nil
  1480. ido-try-merged-list (not no-merge))
  1481. (when subdir
  1482. (setq dir (concat dir subdir))
  1483. (unless (and ido-enable-tramp-completion
  1484. (string-match "\\`/[^/]*@\\'" dir))
  1485. (setq dir (ido-final-slash dir t))))
  1486. (and ido-completion-buffer
  1487. (get-buffer ido-completion-buffer)
  1488. (kill-buffer ido-completion-buffer))
  1489. (cond
  1490. ((equal dir ido-current-directory)
  1491. nil)
  1492. ((ido-is-unc-root dir)
  1493. (ido-trace "unc" dir)
  1494. (setq ido-current-directory dir)
  1495. (setq ido-directory-nonreadable nil)
  1496. (setq ido-directory-too-big nil)
  1497. t)
  1498. (t
  1499. (ido-trace "cd" dir)
  1500. (setq ido-current-directory dir)
  1501. (and ido-completion-buffer
  1502. (get-buffer ido-completion-buffer)
  1503. (kill-buffer ido-completion-buffer))
  1504. (setq ido-directory-nonreadable (ido-nonreadable-directory-p dir))
  1505. (setq ido-directory-too-big (and (not ido-directory-nonreadable)
  1506. (ido-directory-too-big-p dir)))
  1507. t)))
  1508. (defun ido-set-current-home (&optional dir)
  1509. ;; Set ido's current directory to user's home directory
  1510. (ido-set-current-directory (expand-file-name (or dir "~/"))))
  1511. (defun ido-record-command (command arg)
  1512. ;; Add (command arg) to command-history if ido-record-commands is t
  1513. (if ido-record-commands
  1514. (let ((cmd (list command arg)))
  1515. (if (or (not command-history)
  1516. (not (equal cmd (car command-history))))
  1517. (setq command-history (cons cmd command-history))))))
  1518. (defun ido-make-prompt (item prompt)
  1519. ;; Make the prompt for ido-read-internal
  1520. (cond
  1521. ((and (memq item '(file dir)) ido-current-directory)
  1522. (let ((dirname (abbreviate-file-name ido-current-directory))
  1523. (max-width (if (and ido-max-file-prompt-width (floatp ido-max-file-prompt-width))
  1524. (floor (* (frame-width) ido-max-file-prompt-width))
  1525. ido-max-file-prompt-width))
  1526. (literal (and (boundp 'ido-find-literal) ido-find-literal "(literal) "))
  1527. (vc-off (and ido-saved-vc-hb (not vc-handled-backends) "[-VC] "))
  1528. (prefix nil)
  1529. (rule ido-rewrite-file-prompt-rules))
  1530. (let ((case-fold-search nil))
  1531. (while rule
  1532. (if (and (consp (car rule))
  1533. (string-match (car (car rule)) dirname))
  1534. (setq dirname
  1535. (if (stringp (cdr (car rule)))
  1536. (replace-match (cdr (car rule)) t nil dirname)
  1537. (funcall (cdr (car rule)) dirname))))
  1538. (setq rule (cdr rule))))
  1539. (run-hooks 'ido-rewrite-file-prompt-functions)
  1540. (concat prompt
  1541. ; (if ido-process-ignore-lists "" "&")
  1542. (or literal "")
  1543. (or vc-off "")
  1544. (or prefix "")
  1545. (let ((l (length dirname)))
  1546. (if (and max-width (> max-width 0) (> l max-width))
  1547. (let* ((s (substring dirname (- max-width)))
  1548. (i (string-match "/" s)))
  1549. (concat "..." (if i (substring s i) s)))
  1550. dirname)))))
  1551. (t prompt)))
  1552. ;; Here is very briefly how ido-find-file works:
  1553. ;;
  1554. ;; (ido-find-file)
  1555. ;; (ido-file-internal method)
  1556. ;; set ido-current-directory
  1557. ;; (ido-read-internal 'file ...)
  1558. ;; (while ...
  1559. ;; (ido-make-item-list ...)
  1560. ;; (ido-set-matches)
  1561. ;; (completing-read ... ido-text-init ...)
  1562. ;;
  1563. ;; ... here user is allowed to type characters and commands
  1564. ;; a command may set ido-exit and call (exit-minibuffer)
  1565. ;; to make ido-read-internal do advanced tasks (or return)
  1566. ;;
  1567. ;; ... ido-tidy and ido-exhibit are pre- and post-hooks
  1568. ;; which are run before and after each user command.
  1569. ;;
  1570. ;; return value from completing-read is stored in ido-final-text
  1571. ;; - ido-exit may cause further actions to be taken:
  1572. ;; 'refresh - repeat loop (make-item-list, set-matches)
  1573. ;; 'edit - edit the prompt string, then repeat loop
  1574. ;; 'keep - repeat loop but don't (re)make-item-list
  1575. ;; 'updir - go up one directory, repeat loop
  1576. ;; else set ido-selected based on ido-final-text,
  1577. ;; optionally update ido-current-directory and repeat loop, or
  1578. ;; exit with the return value of ido-selected (file name)
  1579. ;; selected file name is returned from ido-read-internal,
  1580. ;; ido-exit and method determines what action is taken
  1581. ;; e.g. the file name may be ignored or joined with ido-current-directory, and
  1582. ;; the relevant function is called (find-file, write-file, etc).
  1583. (defun ido-read-internal (item prompt hist &optional default require-match initial)
  1584. "Perform the `ido-read-buffer' and `ido-read-file-name' functions.
  1585. Return the name of a buffer or file selected.
  1586. PROMPT is the prompt to give to the user.
  1587. DEFAULT if given is the default item to start with.
  1588. If REQUIRE-MATCH is non-nil, an existing file must be selected.
  1589. If INITIAL is non-nil, it specifies the initial input string."
  1590. (let
  1591. ((ido-cur-item item)
  1592. (ido-entry-buffer (current-buffer))
  1593. (ido-process-ignore-lists t)
  1594. (ido-process-ignore-lists-inhibit nil)
  1595. (ido-set-default-item t)
  1596. ido-default-item
  1597. ido-selected
  1598. ido-final-text
  1599. (done nil)
  1600. (icomplete-mode nil) ;; prevent icomplete starting up
  1601. ;; Exported dynamic variables:
  1602. ido-cur-list
  1603. ido-ignored-list
  1604. (ido-rotate-temp nil)
  1605. (ido-keep-item-list nil)
  1606. (ido-use-merged-list nil)
  1607. (ido-try-merged-list t)
  1608. (ido-pre-merge-state nil)
  1609. (ido-case-fold ido-case-fold)
  1610. (ido-enable-prefix ido-enable-prefix)
  1611. (ido-enable-regexp ido-enable-regexp)
  1612. (ido-show-confirm-message nil)
  1613. )
  1614. (ido-setup-completion-map)
  1615. (setq ido-text-init initial)
  1616. (setq ido-input-stack nil)
  1617. (run-hooks 'ido-setup-hook)
  1618. (while (not done)
  1619. (ido-trace "\n_LOOP_" ido-text-init)
  1620. (setq ido-exit nil)
  1621. (setq ido-rescan t)
  1622. (setq ido-rotate nil)
  1623. (setq ido-text "")
  1624. (when ido-set-default-item
  1625. (setq ido-default-item
  1626. (cond
  1627. ((eq item 'buffer)
  1628. (if (bufferp default) (buffer-name default) default))
  1629. ((stringp default)
  1630. (if (memq item '(file dir))
  1631. (file-name-nondirectory default)
  1632. default))
  1633. ((eq item 'file)
  1634. (and ido-enable-last-directory-history
  1635. (let ((d (assoc ido-current-directory ido-last-directory-list)))
  1636. (and d (cdr d)))))))
  1637. (if (member ido-default-item ido-ignore-item-temp-list)
  1638. (setq ido-default-item nil))
  1639. (ido-trace "new default" ido-default-item)
  1640. (if ido-default-item
  1641. (setq ido-initial-position 0))
  1642. (setq ido-set-default-item nil))
  1643. (if ido-process-ignore-lists-inhibit
  1644. (setq ido-process-ignore-lists nil))
  1645. (if (and ido-use-merged-list (memq ido-try-merged-list '(t wide)) (not ido-keep-item-list))
  1646. (let ((olist ido-cur-list)
  1647. (oign ido-ignored-list)
  1648. (omat ido-matches)
  1649. (l (ido-make-merged-file-list ido-text-init
  1650. (eq ido-use-merged-list 'auto)
  1651. (eq ido-try-merged-list 'wide))))
  1652. (ido-trace "merged" l)
  1653. (cond
  1654. ((not l)
  1655. (if (eq ido-try-merged-list 'wide)
  1656. (setq ido-pre-merge-state
  1657. (list "" ido-current-directory olist oign omat)
  1658. ido-cur-list nil
  1659. ido-ignored-list nil
  1660. ido-matches nil
  1661. ido-keep-item-list t
  1662. ido-try-merged-list (if (eq ido-use-merged-list 'auto) 'auto nil)
  1663. ido-use-merged-list nil)
  1664. (setq ido-cur-list olist
  1665. ido-ignored-list oign
  1666. ido-matches omat
  1667. ido-keep-item-list t
  1668. ido-try-merged-list (if (eq ido-use-merged-list 'auto) 'auto nil)
  1669. ido-use-merged-list nil)))
  1670. ((eq l t)
  1671. (setq ido-use-merged-list nil))
  1672. ((eq l 'input-pending-p)
  1673. (setq ido-try-merged-list t
  1674. ido-use-merged-list nil))
  1675. (t
  1676. (setq ido-pre-merge-state
  1677. (list ido-text-init ido-current-directory olist oign omat))
  1678. (ido-set-current-directory (car (cdr (car l))))
  1679. (if (ido-final-slash ido-text-init)
  1680. (setq ido-text-init ""))
  1681. (setq ido-cur-list l
  1682. ido-ignored-list nil
  1683. ido-matches l
  1684. ido-rescan nil
  1685. ido-keep-item-list t
  1686. ido-use-merged-list t)
  1687. (ido-trace "Merged" t)
  1688. ))))
  1689. (cond
  1690. (ido-keep-item-list
  1691. (setq ido-keep-item-list nil
  1692. ido-rescan nil))
  1693. ((eq ido-cur-item 'file)
  1694. (setq ido-ignored-list nil
  1695. ido-cur-list (and (not ido-directory-nonreadable)
  1696. (not ido-directory-too-big)
  1697. (ido-make-file-list ido-default-item))))
  1698. ((eq ido-cur-item 'dir)
  1699. (setq ido-ignored-list nil
  1700. ido-cur-list (and (not ido-directory-nonreadable)
  1701. (not ido-directory-too-big)
  1702. (ido-make-dir-list ido-default-item))))
  1703. ((eq ido-cur-item 'buffer)
  1704. (setq ido-ignored-list nil
  1705. ido-cur-list (ido-make-buffer-list ido-default-item)))
  1706. ((eq ido-cur-item 'list)
  1707. (setq ido-ignored-list nil
  1708. ido-cur-list (ido-make-choice-list ido-default-item)))
  1709. (t nil))
  1710. (setq ido-rotate-temp nil)
  1711. (if ido-process-ignore-lists-inhibit
  1712. (setq ido-process-ignore-lists t
  1713. ido-process-ignore-lists-inhibit nil))
  1714. (ido-set-matches)
  1715. (if (and ido-matches (eq ido-try-merged-list 'auto))
  1716. (setq ido-try-merged-list t))
  1717. (let ((max-mini-window-height (or ido-max-window-height
  1718. (and (boundp 'max-mini-window-height)
  1719. max-mini-window-height)))
  1720. (ido-completing-read t)
  1721. (ido-require-match require-match)
  1722. (ido-use-mycompletion-depth (1+ (minibuffer-depth)))
  1723. (show-paren-mode nil)
  1724. ;; Postpone history adding till later
  1725. (history-add-new-input nil))
  1726. ;; prompt the user for the file name
  1727. (setq ido-exit nil)
  1728. (setq ido-final-text
  1729. (catch 'ido
  1730. (read-from-minibuffer (ido-make-prompt item prompt)
  1731. (prog1 ido-text-init
  1732. (setq ido-text-init nil))
  1733. ido-completion-map nil hist))))
  1734. (ido-trace "read-from-minibuffer" ido-final-text)
  1735. (and ido-completion-buffer
  1736. (get-buffer ido-completion-buffer)
  1737. (kill-buffer ido-completion-buffer))
  1738. (ido-trace "\n_EXIT_" ido-exit)
  1739. (cond
  1740. ((eq ido-exit 'refresh)
  1741. (if (and (eq ido-use-merged-list 'auto)
  1742. (or (input-pending-p)))
  1743. (setq ido-use-merged-list nil
  1744. ido-keep-item-list t))
  1745. nil)
  1746. ((eq ido-exit 'done)
  1747. (setq done t
  1748. ido-selected ido-text
  1749. ido-exit nil))
  1750. ((memq ido-exit '(edit chdir))
  1751. (cond
  1752. ((memq ido-cur-item '(file dir))
  1753. (let* ((read-file-name-function nil)
  1754. (edit (eq ido-exit 'edit))
  1755. (d ido-current-directory)
  1756. (f ido-text-init)
  1757. (new t))
  1758. (setq ido-text-init "")
  1759. (while new
  1760. (setq new (if edit
  1761. (condition-case nil
  1762. (read-file-name (concat prompt "[EDIT] ")
  1763. (expand-file-name d)
  1764. (concat d f) nil f)
  1765. (quit (concat d f)))
  1766. f)
  1767. d (or (file-name-directory new) "/")
  1768. f (file-name-nondirectory new)
  1769. edit t)
  1770. (if (or
  1771. (file-directory-p d)
  1772. (and (yes-or-no-p (format "Create directory %s? " d))
  1773. (condition-case nil
  1774. (progn (make-directory d t) t)
  1775. (error
  1776. (message "Could not create directory")
  1777. (sit-for 1)
  1778. nil))))
  1779. (progn
  1780. (ido-set-current-directory d nil (eq ido-exit 'chdir))
  1781. (setq ido-text-init f
  1782. new nil))))))
  1783. (t
  1784. (setq ido-text-init
  1785. (condition-case nil
  1786. (read-string (concat prompt "[EDIT] ") ido-final-text)
  1787. (quit ido-final-text)))))
  1788. nil)
  1789. ((eq ido-exit 'keep)
  1790. (setq ido-keep-item-list t))
  1791. ((memq ido-exit '(dired fallback find-file switch-to-buffer insert-buffer insert-file))
  1792. (setq done t))
  1793. ((memq ido-exit '(updir push))
  1794. ;; cannot go up if already at the root-dir (Unix) or at the
  1795. ;; root-dir of a certain drive (Windows or MS-DOS).
  1796. (if (ido-is-tramp-root)
  1797. (when (string-match "\\`\\(/\\([^/]+[:@]\\)*\\)\\([^/]+\\)[:@]\\'" ido-current-directory)
  1798. (setq ido-text-init (match-string 3 ido-current-directory))
  1799. (ido-set-current-directory (match-string 1 ido-current-directory))
  1800. (setq ido-set-default-item t))
  1801. (unless (ido-is-root-directory)
  1802. (when (eq ido-exit 'push)
  1803. (setq ido-input-stack (cons (cons ido-cur-item ido-text) ido-input-stack))
  1804. (setq ido-cur-item 'dir)
  1805. (setq ido-text-init (file-name-nondirectory (substring ido-current-directory 0 -1)))
  1806. (ido-trace "push" ido-input-stack))
  1807. (ido-set-current-directory (file-name-directory (substring ido-current-directory 0 -1)))
  1808. (setq ido-set-default-item t))))
  1809. ((eq ido-exit 'pop)
  1810. (ido-trace "pop" ido-input-stack)
  1811. (let ((elt (car ido-input-stack)))
  1812. (setq ido-input-stack (cdr ido-input-stack))
  1813. (ido-set-current-directory (concat ido-current-directory ido-text))
  1814. (setq ido-cur-item (car elt))
  1815. (setq ido-text-init (cdr elt))))
  1816. ((eq ido-exit 'pop-all)
  1817. (ido-trace "pop-all" ido-input-stack)
  1818. (while ido-input-stack
  1819. (let ((elt (car ido-input-stack)))
  1820. (setq ido-input-stack (cdr ido-input-stack))
  1821. (ido-set-current-directory (concat ido-current-directory ido-text))
  1822. (setq ido-cur-item (car elt))
  1823. (setq ido-text-init (cdr elt)))))
  1824. ;; Handling the require-match must be done in a better way.
  1825. ((and require-match
  1826. (not (memq require-match '(confirm confirm-after-completion)))
  1827. (not (if ido-directory-too-big
  1828. (file-exists-p (concat ido-current-directory ido-final-text))
  1829. (ido-existing-item-p))))
  1830. (error "Must specify valid item"))
  1831. (t
  1832. (setq ido-selected
  1833. (if (or (eq ido-exit 'takeprompt)
  1834. (null ido-matches))
  1835. ido-final-text
  1836. ;; else take head of list
  1837. (ido-name (car ido-matches))))
  1838. (cond
  1839. ((memq item '(buffer list))
  1840. (setq done t))
  1841. ((string-equal "./" ido-selected)
  1842. nil)
  1843. ((string-equal "../" ido-selected)
  1844. ;; cannot go up if already at the root-dir (Unix) or at the
  1845. ;; root-dir of a certain drive (Windows or MS-DOS).
  1846. (or (ido-is-root-directory)
  1847. (ido-set-current-directory (file-name-directory (substring ido-current-directory 0 -1))))
  1848. (setq ido-set-default-item t))
  1849. ((and (string-match (if ido-enable-tramp-completion ".[:@]\\'" ".:\\'") ido-selected)
  1850. (ido-is-root-directory) ;; Ange-ftp or Tramp
  1851. (not (ido-local-file-exists-p ido-selected)))
  1852. (ido-set-current-directory ido-current-directory ido-selected)
  1853. (ido-trace "tramp prefix" ido-selected)
  1854. (if (ido-is-slow-ftp-host)
  1855. (setq ido-exit 'fallback
  1856. done t)
  1857. (setq ido-set-default-item t)))
  1858. ((or (string-match "[/\\][^/\\]" ido-selected)
  1859. (and (memq system-type '(windows-nt ms-dos))
  1860. (string-match "\\`[a-zA-Z]:" ido-selected)))
  1861. (ido-set-current-directory (file-name-directory ido-selected))
  1862. (setq ido-set-default-item t))
  1863. ((string-match "\\`~" ido-selected)
  1864. (ido-set-current-home ido-selected))
  1865. ((ido-final-slash ido-selected)
  1866. (if ido-enable-last-directory-history
  1867. (let ((x (assoc ido-current-directory ido-last-directory-list)))
  1868. (if x
  1869. (setcdr x ido-selected)
  1870. (setq ido-last-directory-list
  1871. (cons (cons ido-current-directory ido-selected) ido-last-directory-list)))))
  1872. (ido-set-current-directory ido-current-directory ido-selected)
  1873. (if ido-input-stack
  1874. ; automatically pop stack elements which match existing files or directories
  1875. (let (elt)
  1876. (while (and (setq elt (car ido-input-stack))
  1877. (file-exists-p (concat ido-current-directory (cdr elt))))
  1878. (if (setq ido-input-stack (cdr ido-input-stack))
  1879. (ido-set-current-directory ido-current-directory (cdr elt))
  1880. (setq ido-text-init (cdr elt)))
  1881. (setq ido-cur-item (car elt))))
  1882. (setq ido-set-default-item t)))
  1883. (t
  1884. (setq done t))))))
  1885. (add-to-history (cond
  1886. ((consp hist)
  1887. (or (car hist) 'minibuffer-history))
  1888. (hist hist)
  1889. (t 'minibuffer-history))
  1890. ido-selected)
  1891. ido-selected))
  1892. (defun ido-edit-input ()
  1893. "Edit absolute file name entered so far with ido; terminate by RET.
  1894. If cursor is not at the end of the user input, move to end of input."
  1895. (interactive)
  1896. (if (not (eobp))
  1897. (end-of-line)
  1898. (setq ido-text-init (if ido-matches (ido-name (car ido-matches)) ido-text))
  1899. (setq ido-exit 'edit)
  1900. (exit-minibuffer)))
  1901. ;;; MAIN FUNCTIONS
  1902. (defun ido-buffer-internal (method &optional fallback prompt default initial switch-cmd)
  1903. ;; Internal function for ido-switch-buffer and friends
  1904. (if (not ido-mode)
  1905. (progn
  1906. (run-hook-with-args 'ido-before-fallback-functions
  1907. (or fallback 'switch-to-buffer))
  1908. (call-interactively (or fallback 'switch-to-buffer)))
  1909. (let* ((ido-context-switch-command switch-cmd)
  1910. (ido-current-directory nil)
  1911. (ido-directory-nonreadable nil)
  1912. (ido-directory-too-big nil)
  1913. (ido-use-virtual-buffers ido-use-virtual-buffers)
  1914. (require-match (confirm-nonexistent-file-or-buffer))
  1915. (buf (ido-read-internal 'buffer (or prompt "Buffer: ") 'ido-buffer-history default
  1916. require-match initial))
  1917. filename)
  1918. ;; Choose the buffer name: either the text typed in, or the head
  1919. ;; of the list of matches
  1920. (cond
  1921. ((eq ido-exit 'find-file)
  1922. (ido-file-internal
  1923. (if (memq method '(other-window other-frame)) method ido-default-file-method)
  1924. nil nil nil nil ido-text))
  1925. ((eq ido-exit 'insert-file)
  1926. (ido-file-internal 'insert 'insert-file nil "Insert file: " nil ido-text 'ido-enter-insert-buffer))
  1927. ((eq ido-exit 'fallback)
  1928. (let ((read-buffer-function nil))
  1929. (setq this-command (or fallback 'switch-to-buffer))
  1930. (run-hook-with-args 'ido-before-fallback-functions this-command)
  1931. (call-interactively this-command)))
  1932. ;; Check buf is non-nil.
  1933. ((not buf) nil)
  1934. ((= (length buf) 0) nil)
  1935. ;; View buffer if it exists
  1936. ((get-buffer buf)
  1937. (add-to-history 'buffer-name-history buf)
  1938. (if (eq method 'insert)
  1939. (progn
  1940. (ido-record-command 'insert-buffer buf)
  1941. (push-mark
  1942. (save-excursion
  1943. (insert-buffer-substring (get-buffer buf))
  1944. (point))))
  1945. (ido-visit-buffer buf method t)))
  1946. ;; check for a virtual buffer reference
  1947. ((and ido-use-virtual-buffers ido-virtual-buffers
  1948. (setq filename (assoc buf ido-virtual-buffers)))
  1949. (ido-visit-buffer (find-file-noselect (cdr filename)) method t))
  1950. ((and (eq ido-create-new-buffer 'prompt)
  1951. (null require-match)
  1952. (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
  1953. nil)
  1954. ;; buffer doesn't exist
  1955. ((and (eq ido-create-new-buffer 'never)
  1956. (null require-match))
  1957. (message "No buffer matching `%s'" buf))
  1958. ((and (eq ido-create-new-buffer 'prompt)
  1959. (null require-match)
  1960. (not (y-or-n-p (format "No buffer matching `%s', create one? " buf))))
  1961. nil)
  1962. ;; create a new buffer
  1963. (t
  1964. (add-to-history 'buffer-name-history buf)
  1965. (setq buf (get-buffer-create buf))
  1966. (if (fboundp 'set-buffer-major-mode)
  1967. (set-buffer-major-mode buf))
  1968. (ido-visit-buffer buf method t))))))
  1969. (defun ido-record-work-directory (&optional dir)
  1970. (when (and (numberp ido-max-work-directory-list) (> ido-max-work-directory-list 0))
  1971. (if (and (setq dir (or dir ido-current-directory)) (> (length dir) 0))
  1972. (let ((items ido-work-directory-list-ignore-regexps)
  1973. (case-fold-search nil))
  1974. (while (and items dir)
  1975. (if (string-match (car items) dir)
  1976. (setq dir nil))
  1977. (setq items (cdr items)))
  1978. (if dir
  1979. (setq ido-work-directory-list (cons dir (delete dir ido-work-directory-list))))))
  1980. (if (> (length ido-work-directory-list) ido-max-work-directory-list)
  1981. (setcdr (nthcdr (1- ido-max-work-directory-list) ido-work-directory-list) nil))))
  1982. (defun ido-forget-work-directory ()
  1983. (interactive)
  1984. (when (and ido-current-directory ido-work-directory-list)
  1985. (setq ido-work-directory-list (delete ido-current-directory ido-work-directory-list))
  1986. (when ido-use-merged-list
  1987. (ido-undo-merge-work-directory)
  1988. (setq ido-exit 'refresh
  1989. ido-try-merged-list t
  1990. ido-use-merged-list t
  1991. ido-text-init ido-text
  1992. ido-rotate-temp t)
  1993. (exit-minibuffer))))
  1994. (defun ido-record-work-file (name)
  1995. ;; Save NAME in ido-work-file-list
  1996. (when (and (numberp ido-max-work-file-list) (> ido-max-work-file-list 0))
  1997. (or
  1998. (and ido-work-file-list (equal (car ido-work-file-list) name))
  1999. (setq ido-work-file-list (cons name (delete name ido-work-file-list))))
  2000. (if (> (length ido-work-file-list) ido-max-work-file-list)
  2001. (setcdr (nthcdr (1- ido-max-work-file-list) ido-work-file-list) nil))))
  2002. (defun ido-expand-directory (dir)
  2003. ;; Expand DIR or use DEFAULT-DIRECTORY if nil.
  2004. ;; Add final slash to result in case it was missing from DEFAULT-DIRECTORY.
  2005. (ido-final-slash (expand-file-name (or dir default-directory)) t))
  2006. (defun ido-file-internal (method &optional fallback default prompt item initial switch-cmd)
  2007. ;; Internal function for ido-find-file and friends
  2008. (unless item
  2009. (setq item 'file))
  2010. (let ((ido-current-directory (ido-expand-directory default))
  2011. (ido-context-switch-command switch-cmd)
  2012. ido-directory-nonreadable ido-directory-too-big
  2013. filename)
  2014. (if (or (not ido-mode) (ido-is-slow-ftp-host))
  2015. (setq filename t
  2016. ido-exit 'fallback)
  2017. (setq ido-directory-nonreadable
  2018. (ido-nonreadable-directory-p ido-current-directory)
  2019. ido-directory-too-big
  2020. (and (not ido-directory-nonreadable)
  2021. (ido-directory-too-big-p ido-current-directory))))
  2022. (when (and (eq item 'file)
  2023. (or ido-use-url-at-point ido-use-filename-at-point))
  2024. (let (fn d)
  2025. (require 'ffap)
  2026. ;; Duplicate code from ffap-guesser as we want different
  2027. ;; behavior for files and URLs.
  2028. (cond
  2029. ((with-no-warnings
  2030. (and ido-use-url-at-point
  2031. ffap-url-regexp
  2032. (ffap-fixup-url (or (ffap-url-at-point)
  2033. (ffap-gopher-at-point)))))
  2034. (setq ido-exit 'ffap
  2035. filename t))
  2036. ((and ido-use-filename-at-point
  2037. (setq fn (with-no-warnings
  2038. (if (eq ido-use-filename-at-point 'guess)
  2039. (ffap-guesser)
  2040. (ffap-string-at-point))))
  2041. (not (string-match "^http:/" fn))
  2042. (let ((absolute-fn (expand-file-name fn)))
  2043. (setq d (if (file-directory-p absolute-fn)
  2044. (file-name-as-directory absolute-fn)
  2045. (file-name-directory absolute-fn))))
  2046. (file-directory-p d))
  2047. (setq ido-current-directory d)
  2048. (setq initial (file-name-nondirectory fn))))))
  2049. (let (ido-saved-vc-hb
  2050. (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
  2051. (ido-work-directory-index -1)
  2052. (ido-work-file-index -1)
  2053. (ido-find-literal nil))
  2054. (unless filename
  2055. (setq ido-saved-vc-hb vc-handled-backends)
  2056. (let ((minibuffer-completing-file-name t))
  2057. (setq filename (ido-read-internal item
  2058. (or prompt "Find file: ")
  2059. 'ido-file-history
  2060. (and (eq method 'alt-file) buffer-file-name)
  2061. (confirm-nonexistent-file-or-buffer) initial))))
  2062. ;; Choose the file name: either the text typed in, or the head
  2063. ;; of the list of matches
  2064. (cond
  2065. ((eq ido-exit 'fallback)
  2066. ;; Need to guard setting of default-directory here, since
  2067. ;; we don't want to change directory of current buffer.
  2068. (let ((default-directory ido-current-directory)
  2069. (read-file-name-function nil))
  2070. (setq this-command (or fallback 'find-file))
  2071. (run-hook-with-args 'ido-before-fallback-functions this-command)
  2072. (call-interactively this-command)))
  2073. ((eq ido-exit 'switch-to-buffer)
  2074. (ido-buffer-internal
  2075. (if (memq method '(other-window other-frame)) method ido-default-buffer-method)
  2076. nil nil nil ido-text))
  2077. ((eq ido-exit 'insert-buffer)
  2078. (ido-buffer-internal 'insert 'insert-buffer "Insert buffer: " nil ido-text 'ido-enter-insert-file))
  2079. ((eq ido-exit 'dired)
  2080. (dired (concat ido-current-directory (or ido-text ""))))
  2081. ((eq ido-exit 'ffap)
  2082. (find-file-at-point))
  2083. ((eq method 'alt-file)
  2084. (ido-record-work-file filename)
  2085. (setq default-directory ido-current-directory)
  2086. (ido-record-work-directory)
  2087. (find-alternate-file filename))
  2088. ((memq method '(dired list-directory))
  2089. (if (equal filename ".")
  2090. (setq filename ""))
  2091. (let* ((dirname (ido-final-slash (concat ido-current-directory filename) t))
  2092. (file (substring dirname 0 -1)))
  2093. (cond
  2094. ((file-directory-p dirname)
  2095. (ido-record-command method dirname)
  2096. (ido-record-work-directory dirname)
  2097. (funcall method dirname))
  2098. ((file-directory-p ido-current-directory)
  2099. (cond
  2100. ((file-exists-p file)
  2101. (ido-record-command method ido-current-directory)
  2102. (ido-record-work-directory)
  2103. (funcall method ido-current-directory)
  2104. (if (eq method 'dired)
  2105. (with-no-warnings
  2106. (dired-goto-file (expand-file-name file)))))
  2107. ((string-match "[[*?]" filename)
  2108. (setq dirname (concat ido-current-directory filename))
  2109. (ido-record-command method dirname)
  2110. (ido-record-work-directory)
  2111. (funcall method dirname))
  2112. ((y-or-n-p (format "Directory %s does not exist. Create it? " filename))
  2113. (ido-record-command method dirname)
  2114. (ido-record-work-directory dirname)
  2115. (make-directory-internal dirname)
  2116. (funcall method dirname))
  2117. (t
  2118. ;; put make-directory command on history
  2119. (ido-record-command 'make-directory dirname))))
  2120. (t (error "No such directory")))))
  2121. ((eq method 'write)
  2122. (ido-record-work-file filename)
  2123. (setq default-directory ido-current-directory)
  2124. (setq filename (concat ido-current-directory filename))
  2125. (ido-record-command 'write-file filename)
  2126. (add-to-history 'file-name-history filename)
  2127. (ido-record-work-directory)
  2128. (write-file filename t))
  2129. ((eq method 'read-only)
  2130. (ido-record-work-file filename)
  2131. (setq filename (concat ido-current-directory filename))
  2132. (ido-record-command fallback filename)
  2133. (ido-record-work-directory)
  2134. (run-hook-with-args 'ido-before-fallback-functions fallback)
  2135. (funcall fallback filename))
  2136. ((eq method 'insert)
  2137. (ido-record-work-file filename)
  2138. (setq filename (concat ido-current-directory filename))
  2139. (ido-record-command
  2140. (if ido-find-literal 'insert-file-literally 'insert-file)
  2141. filename)
  2142. (add-to-history 'file-name-history filename)
  2143. (ido-record-work-directory)
  2144. (insert-file-1 filename
  2145. (if ido-find-literal
  2146. #'insert-file-contents-literally
  2147. #'insert-file-contents)))
  2148. (filename
  2149. (ido-record-work-file filename)
  2150. (setq filename (concat ido-current-directory filename))
  2151. (ido-record-command 'find-file filename)
  2152. (add-to-history 'file-name-history filename)
  2153. (ido-record-work-directory)
  2154. (ido-visit-buffer (find-file-noselect filename nil ido-find-literal) method))))))
  2155. (defun ido-existing-item-p ()
  2156. ;; Return non-nil if there is a matching item
  2157. (not (null ido-matches)))
  2158. ;;; COMPLETION CODE
  2159. (defun ido-set-common-completion ()
  2160. ;; Find common completion of `ido-text' in `ido-matches'
  2161. ;; The result is stored in `ido-common-match-string'
  2162. (let (val)
  2163. (setq ido-common-match-string nil)
  2164. (if (and ido-matches
  2165. (not ido-enable-regexp) ;; testing
  2166. (stringp ido-text)
  2167. (> (length ido-text) 0))
  2168. (if (setq val (ido-find-common-substring ido-matches ido-text))
  2169. (setq ido-common-match-string val)))
  2170. val))
  2171. (defun ido-complete ()
  2172. "Try and complete the current pattern amongst the file names."
  2173. (interactive)
  2174. (let (res)
  2175. (cond
  2176. (ido-incomplete-regexp
  2177. ;; Do nothing
  2178. )
  2179. ((and (memq ido-cur-item '(file dir))
  2180. (string-match "[$]" ido-text))
  2181. (let ((evar (substitute-in-file-name (concat ido-current-directory ido-text))))
  2182. (if (not (file-exists-p (file-name-directory evar)))
  2183. (message "Expansion generates non-existing directory name")
  2184. (if (file-directory-p evar)
  2185. (ido-set-current-directory evar)
  2186. (let ((d (or (file-name-directory evar) "/"))
  2187. (f (file-name-nondirectory evar)))
  2188. (when (file-directory-p d)
  2189. (ido-set-current-directory d)
  2190. (setq ido-text-init f))))
  2191. (setq ido-exit 'refresh)
  2192. (exit-minibuffer))))
  2193. (ido-directory-too-big
  2194. (setq ido-directory-too-big nil)
  2195. (setq ido-text-init ido-text)
  2196. (setq ido-exit 'refresh)
  2197. (exit-minibuffer))
  2198. ((not ido-matches)
  2199. (when ido-completion-buffer
  2200. (call-interactively (setq this-command ido-cannot-complete-command))))
  2201. ((and (= 1 (length ido-matches))
  2202. (not (and ido-enable-tramp-completion
  2203. (string-equal ido-current-directory "/")
  2204. (string-match ".[@:]\\'" (ido-name (car ido-matches)))))
  2205. (not (ido-local-file-exists-p (ido-name (car ido-matches)))))
  2206. ;; only one choice, so select it.
  2207. (if (not ido-confirm-unique-completion)
  2208. (exit-minibuffer)
  2209. (setq ido-rescan (not ido-enable-prefix))
  2210. (delete-region (minibuffer-prompt-end) (point))
  2211. (insert (ido-name (car ido-matches)))))
  2212. (t ;; else there could be some completions
  2213. (setq res ido-common-match-string)
  2214. (if (and (not (memq res '(t nil)))
  2215. (not (equal res ido-text)))
  2216. ;; found something to complete, so put it in the minibuffer.
  2217. (progn
  2218. ;; move exact match to front if not in prefix mode
  2219. (setq ido-rescan (not ido-enable-prefix))
  2220. (delete-region (minibuffer-prompt-end) (point))
  2221. (insert res))
  2222. ;; else nothing to complete
  2223. (call-interactively (setq this-command ido-cannot-complete-command))
  2224. )))))
  2225. (defun ido-complete-space ()
  2226. "Try completion unless inserting the space makes sense."
  2227. (interactive)
  2228. (if (and (stringp ido-common-match-string)
  2229. (stringp ido-text)
  2230. (cond
  2231. ((> (length ido-common-match-string) (length ido-text))
  2232. (= (aref ido-common-match-string (length ido-text)) ? ))
  2233. (ido-matches
  2234. (let (insert-space
  2235. (re (concat (regexp-quote ido-text) " "))
  2236. (comp ido-matches))
  2237. (while comp
  2238. (if (string-match re (ido-name (car comp)))
  2239. (setq comp nil insert-space t)
  2240. (setq comp (cdr comp))))
  2241. insert-space))
  2242. (t nil)))
  2243. (insert " ")
  2244. (ido-complete)))
  2245. (defun ido-undo-merge-work-directory (&optional text try refresh)
  2246. "Undo or redo last ido directory merge operation.
  2247. If no merge has yet taken place, toggle automatic merging option."
  2248. (interactive)
  2249. (cond
  2250. (ido-pre-merge-state
  2251. (ido-set-current-directory (nth 1 ido-pre-merge-state))
  2252. (setq ido-text-init (or text (car ido-pre-merge-state))
  2253. ido-cur-list (nth 2 ido-pre-merge-state)
  2254. ido-ignored-list (nth 3 ido-pre-merge-state)
  2255. ido-matches (nth 4 ido-pre-merge-state)
  2256. ido-use-merged-list nil
  2257. ido-try-merged-list try
  2258. ido-keep-item-list (not refresh)
  2259. ido-rescan nil
  2260. ido-exit 'refresh
  2261. ido-pre-merge-state nil)
  2262. (exit-minibuffer))
  2263. (text
  2264. nil)
  2265. (ido-try-merged-list
  2266. (setq ido-try-merged-list nil))
  2267. (ido-matches
  2268. (setq ido-try-merged-list t))
  2269. ((not ido-use-merged-list)
  2270. (ido-merge-work-directories))))
  2271. ;;; Magic C-f
  2272. (defun ido-magic-forward-char (arg)
  2273. "Move forward in user input or perform magic action.
  2274. If no user input is present, or at end of input, perform magic actions:
  2275. C-x C-b ... C-f switch to `ido-find-file'.
  2276. C-x C-f ... C-f fallback to non-ido `find-file'.
  2277. C-x C-d ... C-f fallback to non-ido brief `dired'.
  2278. C-x d ... C-f fallback to non-ido `dired'."
  2279. (interactive "P")
  2280. (cond
  2281. ((or arg (not (eobp)))
  2282. (forward-char (min (prefix-numeric-value arg)
  2283. (- (point-max) (point)))))
  2284. ((memq ido-cur-item '(file dir))
  2285. (ido-fallback-command))
  2286. (ido-context-switch-command
  2287. (call-interactively ido-context-switch-command))
  2288. ((eq ido-cur-item 'buffer)
  2289. (ido-enter-find-file))))
  2290. ;;; Magic C-b
  2291. (defun ido-magic-backward-char (arg)
  2292. "Move backward in user input or perform magic action.
  2293. If no user input is present, or at start of input, perform magic actions:
  2294. C-x C-f C-b switch to `ido-switch-buffer'.
  2295. C-x C-d C-b switch to `ido-switch-buffer'.
  2296. C-x d C-b switch to `ido-switch-buffer'.
  2297. C-x C-b C-b fallback to non-ido `switch-to-buffer'."
  2298. (interactive "P")
  2299. (cond
  2300. ((or arg (> (point) (minibuffer-prompt-end)))
  2301. (forward-char
  2302. (- (min (prefix-numeric-value arg)
  2303. (- (point) (minibuffer-prompt-end))))))
  2304. ((eq last-command this-command)
  2305. (when (and (memq ido-cur-item '(file dir))
  2306. (not (bobp)))
  2307. (ido-push-dir))) ; else do nothing
  2308. ((eq ido-cur-item 'buffer)
  2309. (ido-fallback-command))
  2310. (ido-context-switch-command
  2311. (call-interactively ido-context-switch-command))
  2312. (t
  2313. (ido-enter-switch-buffer))))
  2314. ;;; Magic C-d
  2315. (defun ido-magic-delete-char (arg)
  2316. "Delete following char in user input or perform magic action.
  2317. If at end of user input, perform magic actions:
  2318. C-x C-f ... C-d enter `dired' on current directory."
  2319. (interactive "P")
  2320. (cond
  2321. ((or arg (not (eobp)))
  2322. (delete-char (min (prefix-numeric-value arg)
  2323. (- (point-max) (point)))))
  2324. (ido-context-switch-command
  2325. nil)
  2326. ((memq ido-cur-item '(file dir))
  2327. (ido-enter-dired))))
  2328. ;;; TOGGLE FUNCTIONS
  2329. (defun ido-toggle-case ()
  2330. "Toggle the value of `ido-case-fold'."
  2331. (interactive)
  2332. (setq ido-case-fold (not ido-case-fold))
  2333. ;; ask for list to be regenerated.
  2334. (setq ido-rescan t))
  2335. (defun ido-toggle-regexp ()
  2336. "Toggle the value of `ido-enable-regexp'."
  2337. (interactive)
  2338. (setq ido-enable-regexp (not ido-enable-regexp))
  2339. ;; ask for list to be regenerated.
  2340. (setq ido-rescan t))
  2341. (defun ido-toggle-prefix ()
  2342. "Toggle the value of `ido-enable-prefix'."
  2343. (interactive)
  2344. (setq ido-enable-prefix (not ido-enable-prefix))
  2345. ;; ask for list to be regenerated.
  2346. (setq ido-rescan t))
  2347. (defun ido-toggle-ignore ()
  2348. "Toggle ignoring files specified with `ido-ignore-files'."
  2349. (interactive)
  2350. (if (and (not (eobp)) (> (point) (minibuffer-prompt-end)))
  2351. (goto-char (minibuffer-prompt-end))
  2352. (if ido-directory-too-big
  2353. (progn
  2354. (message "Reading directory...")
  2355. (setq ido-directory-too-big nil))
  2356. (setq ido-process-ignore-lists (not ido-process-ignore-lists)))
  2357. (setq ido-text-init ido-text)
  2358. (setq ido-exit 'refresh)
  2359. (exit-minibuffer)))
  2360. (defun ido-toggle-vc ()
  2361. "Disable version control for this file."
  2362. (interactive)
  2363. (if (and ido-mode (eq ido-cur-item 'file))
  2364. (progn
  2365. (setq vc-handled-backends
  2366. (if vc-handled-backends nil ido-saved-vc-hb))
  2367. (setq ido-text-init ido-text)
  2368. (setq ido-exit 'keep)
  2369. (exit-minibuffer))))
  2370. (defun ido-toggle-literal ()
  2371. "Toggle literal reading of this file."
  2372. (interactive)
  2373. (if (and ido-mode (eq ido-cur-item 'file))
  2374. (progn
  2375. (setq ido-find-literal (not ido-find-literal))
  2376. (setq ido-text-init ido-text)
  2377. (setq ido-exit 'keep)
  2378. (exit-minibuffer))))
  2379. (defun ido-toggle-virtual-buffers ()
  2380. "Toggle the use of virtual buffers.
  2381. See `ido-use-virtual-buffers' for explanation of virtual buffer."
  2382. (interactive)
  2383. (when (and ido-mode (eq ido-cur-item 'buffer))
  2384. (setq ido-use-virtual-buffers (not ido-use-virtual-buffers))
  2385. (setq ido-text-init ido-text)
  2386. (setq ido-exit 'refresh)
  2387. (exit-minibuffer)))
  2388. (defun ido-reread-directory ()
  2389. "Read current directory again.
  2390. May be useful if cached version is no longer valid, but directory
  2391. timestamp has not changed (e.g. with ftp or on Windows)."
  2392. (interactive)
  2393. (if (and ido-mode (memq ido-cur-item '(file dir)))
  2394. (progn
  2395. (if (ido-is-unc-root)
  2396. (setq ido-unc-hosts-cache t)
  2397. (ido-remove-cached-dir ido-current-directory))
  2398. (setq ido-text-init ido-text)
  2399. (setq ido-rotate-temp t)
  2400. (setq ido-exit 'refresh)
  2401. (exit-minibuffer))))
  2402. (defun ido-exit-minibuffer ()
  2403. "Exit minibuffer, but make sure we have a match if one is needed."
  2404. (interactive)
  2405. (if (and (or (not ido-require-match)
  2406. (if (memq ido-require-match '(confirm confirm-after-completion))
  2407. (if (or (eq ido-cur-item 'dir)
  2408. (eq last-command this-command))
  2409. t
  2410. (setq ido-show-confirm-message t)
  2411. nil))
  2412. (ido-existing-item-p))
  2413. (not ido-incomplete-regexp))
  2414. (exit-minibuffer)))
  2415. (defun ido-select-text ()
  2416. "Select the buffer or file named by the prompt.
  2417. If no buffer or file exactly matching the prompt exists, maybe create a new one."
  2418. (interactive)
  2419. (setq ido-exit 'takeprompt)
  2420. (exit-minibuffer))
  2421. (defun ido-fallback-command ()
  2422. "Fallback to non-ido version of current command."
  2423. (interactive)
  2424. (let ((i (length ido-text)))
  2425. (while (> i 0)
  2426. (push (aref ido-text (setq i (1- i))) unread-command-events)))
  2427. (setq ido-exit 'fallback)
  2428. (exit-minibuffer))
  2429. (defun ido-enter-find-file ()
  2430. "Drop into `find-file' from buffer switching."
  2431. (interactive)
  2432. (setq ido-exit 'find-file)
  2433. (exit-minibuffer))
  2434. (defun ido-enter-switch-buffer ()
  2435. "Drop into `ido-switch-buffer' from file switching."
  2436. (interactive)
  2437. (setq ido-exit 'switch-to-buffer)
  2438. (exit-minibuffer))
  2439. (defun ido-enter-dired ()
  2440. "Drop into `dired' from file switching."
  2441. (interactive)
  2442. (setq ido-exit 'dired)
  2443. (exit-minibuffer))
  2444. (defun ido-enter-insert-buffer ()
  2445. "Drop into `insert-buffer' from insert file."
  2446. (interactive)
  2447. (setq ido-exit 'insert-buffer)
  2448. (exit-minibuffer))
  2449. (defun ido-enter-insert-file ()
  2450. "Drop into `insert-file' from insert buffer."
  2451. (interactive)
  2452. (setq ido-exit 'insert-file)
  2453. (exit-minibuffer))
  2454. (defun ido-up-directory (&optional clear)
  2455. "Go up one directory level."
  2456. (interactive "P")
  2457. (setq ido-text-init (if clear nil ido-text))
  2458. (setq ido-exit 'updir)
  2459. (setq ido-rotate-temp t)
  2460. (exit-minibuffer))
  2461. (defun ido-delete-backward-updir (count)
  2462. "Delete char backwards, or at beginning of buffer, go up one level."
  2463. (interactive "P")
  2464. (cond
  2465. ((= (minibuffer-prompt-end) (point))
  2466. (if (not count)
  2467. (ido-up-directory t)))
  2468. ((and ido-pre-merge-state (string-equal (car ido-pre-merge-state) ido-text))
  2469. (ido-undo-merge-work-directory (substring ido-text 0 -1) t t))
  2470. ((eq this-original-command 'viper-backward-char)
  2471. (funcall this-original-command (prefix-numeric-value count)))
  2472. ((eq this-original-command 'viper-del-backward-char-in-insert)
  2473. (funcall this-original-command))
  2474. (t
  2475. (delete-char (- (prefix-numeric-value count))))))
  2476. (defun ido-delete-backward-word-updir (count)
  2477. "Delete all chars backwards, or at beginning of buffer, go up one level."
  2478. (interactive "P")
  2479. (if (= (minibuffer-prompt-end) (point))
  2480. (if (not count)
  2481. (ido-up-directory t))
  2482. (if (eq this-original-command 'viper-delete-backward-word)
  2483. (funcall this-original-command (prefix-numeric-value count))
  2484. (backward-kill-word (prefix-numeric-value count)))))
  2485. (defun ido-get-work-directory (&optional incr must-match)
  2486. (let ((n (length ido-work-directory-list))
  2487. (i ido-work-directory-index)
  2488. (j 0)
  2489. dir)
  2490. (if (or (not ido-text) (= (length ido-text) 0))
  2491. (setq must-match nil))
  2492. (while (< j n)
  2493. (setq i (+ i incr)
  2494. j (1+ j))
  2495. (if (> incr 0)
  2496. (if (>= i n) (setq i 0))
  2497. (if (< i 0) (setq i (1- n))))
  2498. (setq dir (nth i ido-work-directory-list))
  2499. (if (and dir
  2500. (not (equal dir ido-current-directory))
  2501. (file-directory-p dir)
  2502. (or (not must-match)
  2503. ;; TODO. check for nonreadable and too-big.
  2504. (ido-set-matches-1
  2505. (if (eq ido-cur-item 'file)
  2506. (ido-make-file-list-1 dir)
  2507. (ido-make-dir-list-1 dir)))))
  2508. (setq j n)
  2509. (setq dir nil)))
  2510. (if dir
  2511. (setq ido-work-directory-index i))
  2512. dir))
  2513. (defun ido-prev-work-directory ()
  2514. "Change to next working directory in list."
  2515. (interactive)
  2516. (let ((dir (ido-get-work-directory 1 ido-work-directory-match-only)))
  2517. (when dir
  2518. (ido-set-current-directory dir)
  2519. (setq ido-exit 'refresh)
  2520. (setq ido-text-init ido-text)
  2521. (setq ido-rotate-temp t)
  2522. (exit-minibuffer))))
  2523. (defun ido-next-work-directory ()
  2524. "Change to previous working directory in list."
  2525. (interactive)
  2526. (let ((dir (ido-get-work-directory -1 ido-work-directory-match-only)))
  2527. (when dir
  2528. (ido-set-current-directory dir)
  2529. (setq ido-exit 'refresh)
  2530. (setq ido-text-init ido-text)
  2531. (setq ido-rotate-temp t)
  2532. (exit-minibuffer))))
  2533. (defun ido-merge-work-directories ()
  2534. "Search (and merge) work directories for files matching the current input string."
  2535. (interactive)
  2536. (setq ido-use-merged-list t ido-try-merged-list t)
  2537. (setq ido-exit 'refresh)
  2538. (setq ido-text-init ido-text)
  2539. (setq ido-rotate-temp t)
  2540. (exit-minibuffer))
  2541. (defun ido-wide-find-file (&optional file)
  2542. "Prompt for FILE to search for using find, starting from current directory."
  2543. (interactive)
  2544. (unless file
  2545. (let ((enable-recursive-minibuffers t))
  2546. (setq file
  2547. (condition-case nil
  2548. (read-string (concat "Wide find file: " ido-current-directory) ido-text)
  2549. (quit "")))))
  2550. (when (> (length file) 0)
  2551. (setq ido-use-merged-list t ido-try-merged-list 'wide)
  2552. (setq ido-exit 'refresh)
  2553. (setq ido-text-init file)
  2554. (setq ido-rotate-temp t)
  2555. (exit-minibuffer)))
  2556. (defun ido-wide-find-dir (&optional dir)
  2557. "Prompt for DIR to search for using find, starting from current directory."
  2558. (interactive)
  2559. (unless dir
  2560. (let ((enable-recursive-minibuffers t))
  2561. (setq dir
  2562. (condition-case nil
  2563. (read-string (concat "Wide find directory: " ido-current-directory) ido-text)
  2564. (quit "")))))
  2565. (when (> (length dir) 0)
  2566. (setq ido-use-merged-list t ido-try-merged-list 'wide)
  2567. (setq ido-exit 'refresh)
  2568. (setq ido-text-init (ido-final-slash dir t))
  2569. (setq ido-rotate-temp t)
  2570. (exit-minibuffer)))
  2571. (defun ido-wide-find-dir-or-delete-dir (&optional _dir)
  2572. "Prompt for DIR to search for using find, starting from current directory.
  2573. If input stack is non-empty, delete current directory component."
  2574. (interactive)
  2575. (if ido-input-stack
  2576. (ido-delete-backward-word-updir 1)
  2577. (ido-wide-find-dir)))
  2578. (defun ido-take-first-match ()
  2579. "Use first matching item as input text."
  2580. (interactive)
  2581. (when ido-matches
  2582. (setq ido-text-init (ido-name (car ido-matches)))
  2583. (setq ido-exit 'refresh)
  2584. (exit-minibuffer)))
  2585. (defun ido-push-dir ()
  2586. "Move to previous directory in file name, push current input on stack."
  2587. (interactive)
  2588. (setq ido-exit 'push)
  2589. (exit-minibuffer))
  2590. (defun ido-push-dir-first ()
  2591. "Move to previous directory in file name, push first match on stack."
  2592. (interactive)
  2593. (if ido-matches
  2594. (setq ido-text (ido-name (car ido-matches))))
  2595. (setq ido-exit 'push)
  2596. (exit-minibuffer))
  2597. (defun ido-pop-dir (arg)
  2598. "Pop directory from input stack back to input.
  2599. With \\[universal-argument], pop all elements."
  2600. (interactive "P")
  2601. (when ido-input-stack
  2602. (setq ido-exit (if arg 'pop-all 'pop))
  2603. (exit-minibuffer)))
  2604. (defun ido-wide-find-file-or-pop-dir (arg)
  2605. (interactive "P")
  2606. (if ido-input-stack
  2607. (ido-pop-dir arg)
  2608. (ido-wide-find-file)))
  2609. (defun ido-make-directory (&optional dir)
  2610. "Prompt for DIR to create in current directory."
  2611. (interactive)
  2612. (unless dir
  2613. (let ((enable-recursive-minibuffers t))
  2614. (setq dir
  2615. (read-string (concat "Make directory: " ido-current-directory) ido-text))))
  2616. (when (> (length dir) 0)
  2617. (setq dir (concat ido-current-directory dir))
  2618. (unless (file-exists-p dir)
  2619. (make-directory dir t)
  2620. (ido-set-current-directory dir)
  2621. (setq ido-exit 'refresh)
  2622. (setq ido-text-init nil)
  2623. (setq ido-rotate-temp t)
  2624. (exit-minibuffer))))
  2625. (defun ido-get-work-file (incr)
  2626. (let ((n (length ido-work-file-list))
  2627. (i (+ ido-work-file-index incr))
  2628. name)
  2629. (if (> incr 0)
  2630. (if (>= i n) (setq i 0))
  2631. (if (< i 0) (setq i (1- n))))
  2632. (setq name (nth i ido-work-file-list))
  2633. (setq ido-work-file-index i)
  2634. name))
  2635. (defun ido-prev-work-file ()
  2636. "Change to next working file name in list."
  2637. (interactive)
  2638. (let ((name (ido-get-work-file 1)))
  2639. (when name
  2640. (setq ido-text-init name)
  2641. (setq ido-exit 'refresh)
  2642. (exit-minibuffer))))
  2643. (defun ido-next-work-file ()
  2644. "Change to previous working file name in list."
  2645. (interactive)
  2646. (let ((name (ido-get-work-file -1)))
  2647. (when name
  2648. (setq ido-text-init name)
  2649. (setq ido-exit 'refresh)
  2650. (exit-minibuffer))))
  2651. (defun ido-copy-current-file-name (all)
  2652. "Insert file name of current buffer.
  2653. If repeated, insert text from buffer instead."
  2654. (interactive "P")
  2655. (let* ((bfname (or (buffer-file-name ido-entry-buffer)
  2656. (buffer-name ido-entry-buffer)))
  2657. (name (and bfname (file-name-nondirectory bfname))))
  2658. (when name
  2659. (setq ido-text-init
  2660. (if (or all
  2661. (eq last-command this-command)
  2662. (not (equal (file-name-directory bfname) ido-current-directory))
  2663. (not (string-match "\\.[^.]*\\'" name)))
  2664. name
  2665. (substring name 0 (1+ (match-beginning 0)))))
  2666. (setq ido-exit 'refresh
  2667. ido-try-merged-list nil)
  2668. (exit-minibuffer))))
  2669. (defun ido-copy-current-word (_all)
  2670. "Insert current word (file or directory name) from current buffer."
  2671. (interactive "P")
  2672. (let ((word (with-current-buffer ido-entry-buffer
  2673. (let ((p (point)) start-line end-line start-name)
  2674. (if (and mark-active (/= p (mark)))
  2675. (setq start-name (mark))
  2676. (beginning-of-line)
  2677. (setq start-line (point))
  2678. (end-of-line)
  2679. (setq end-line (point))
  2680. (goto-char p)
  2681. (if (re-search-backward "[^-_a-zA-Z0-9:./\\~@]" start-line 1)
  2682. (forward-char 1))
  2683. (setq start-name (point))
  2684. (re-search-forward "[-_a-zA-Z0-9:./\\~@]*" end-line 1)
  2685. (if (= start-name (point))
  2686. (setq start-name nil)))
  2687. (and start-name
  2688. (buffer-substring-no-properties start-name (point)))))))
  2689. (if (cond
  2690. ((not word) nil)
  2691. ((string-match "\\`[~/]" word)
  2692. (setq ido-text-init word
  2693. ido-try-merged-list nil
  2694. ido-exit 'chdir))
  2695. ((string-match "/" word)
  2696. (setq ido-text-init (concat ido-current-directory word)
  2697. ido-try-merged-list nil
  2698. ido-exit 'chdir))
  2699. (t
  2700. (setq ido-text-init word
  2701. ido-try-merged-list nil
  2702. ido-exit 'refresh)))
  2703. (exit-minibuffer))))
  2704. (defun ido-next-match ()
  2705. "Put first element of `ido-matches' at the end of the list."
  2706. (interactive)
  2707. (if ido-matches
  2708. (let ((next (cadr ido-matches)))
  2709. (setq ido-cur-list (ido-chop ido-cur-list next))
  2710. (setq ido-matches (ido-chop ido-matches next))
  2711. (setq ido-rescan nil))))
  2712. (defun ido-prev-match ()
  2713. "Put last element of `ido-matches' at the front of the list."
  2714. (interactive)
  2715. (if ido-matches
  2716. (let ((prev (car (last ido-matches))))
  2717. (setq ido-cur-list (ido-chop ido-cur-list prev))
  2718. (setq ido-matches (ido-chop ido-matches prev))
  2719. (setq ido-rescan nil))))
  2720. (defun ido-next-match-dir ()
  2721. "Find next directory in match list.
  2722. If work directories have been merged, cycle through directories for
  2723. first matching file."
  2724. (interactive)
  2725. (if ido-use-merged-list
  2726. (if ido-matches
  2727. (let* ((elt (car ido-matches))
  2728. (dirs (cdr elt)))
  2729. (when (> (length dirs) 1)
  2730. (setcdr elt (ido-chop dirs (cadr dirs))))
  2731. (setq ido-rescan nil)))
  2732. (let ((cnt (length ido-matches))
  2733. (i 1))
  2734. (while (and (< i cnt) (not (ido-final-slash (nth i ido-matches))))
  2735. (setq i (1+ i)))
  2736. (if (< i cnt)
  2737. (setq ido-cur-list (ido-chop ido-cur-list (nth i ido-matches)))))))
  2738. (defun ido-prev-match-dir ()
  2739. "Find previous directory in match list.
  2740. If work directories have been merged, cycle through directories
  2741. for first matching file."
  2742. (interactive)
  2743. (if ido-use-merged-list
  2744. (if ido-matches
  2745. (let* ((elt (car ido-matches))
  2746. (dirs (cdr elt)))
  2747. (when (> (length dirs) 1)
  2748. (setcdr elt (ido-chop dirs (car (last dirs)))))
  2749. (setq ido-rescan nil)))
  2750. (let* ((cnt (length ido-matches))
  2751. (i (1- cnt)))
  2752. (while (and (> i 0) (not (ido-final-slash (nth i ido-matches))))
  2753. (setq i (1- i)))
  2754. (if (> i 0)
  2755. (setq ido-cur-list (ido-chop ido-cur-list (nth i ido-matches)))))))
  2756. (defun ido-restrict-to-matches ()
  2757. "Set current item list to the currently matched items."
  2758. (interactive)
  2759. (when ido-matches
  2760. (setq ido-cur-list ido-matches
  2761. ido-text-init ""
  2762. ido-rescan nil
  2763. ido-exit 'keep)
  2764. (exit-minibuffer)))
  2765. (defun ido-chop (items elem)
  2766. "Remove all elements before ELEM and put them at the end of ITEMS."
  2767. (let ((ret nil)
  2768. (next nil)
  2769. (sofar nil))
  2770. (while (not ret)
  2771. (setq next (car items))
  2772. (if (equal next elem)
  2773. (setq ret (append items (nreverse sofar)))
  2774. ;; else
  2775. (progn
  2776. (setq items (cdr items))
  2777. (setq sofar (cons next sofar)))))
  2778. ret))
  2779. (defun ido-name (item)
  2780. ;; Return file name for current item, whether in a normal list
  2781. ;; or a merged work directory list.
  2782. (if (consp item) (car item) item))
  2783. ;;; CREATE LIST OF ALL CURRENT FILES
  2784. (defun ido-all-completions ()
  2785. ;; Return unsorted list of all completions.
  2786. (let ((ido-process-ignore-lists nil)
  2787. (ido-directory-too-big nil))
  2788. (cond
  2789. ((eq ido-cur-item 'file)
  2790. (ido-make-file-list-1 ido-current-directory))
  2791. ((eq ido-cur-item 'dir)
  2792. (ido-make-dir-list-1 ido-current-directory))
  2793. ((eq ido-cur-item 'buffer)
  2794. (ido-make-buffer-list-1))
  2795. ((eq ido-cur-item 'list)
  2796. ido-choice-list)
  2797. (t nil))))
  2798. ;; File list sorting
  2799. (defun ido-file-lessp (a b)
  2800. ;; Simple compare two file names.
  2801. (string-lessp (ido-no-final-slash a) (ido-no-final-slash b)))
  2802. (defun ido-file-extension-lessp (a b)
  2803. ;; Compare file names according to ido-file-extensions-order list.
  2804. (let ((n (compare-strings a 0 nil b 0 nil nil))
  2805. lessp p)
  2806. (if (eq n t)
  2807. nil
  2808. (if (< n 0)
  2809. (setq n (1- (- n))
  2810. p a a b b p
  2811. lessp t)
  2812. (setq n (1- n)))
  2813. (cond
  2814. ((= n 0)
  2815. lessp)
  2816. ((= (aref a n) ?.)
  2817. (ido-file-extension-aux a b n lessp))
  2818. (t
  2819. (while (and (> n 2) (/= (aref a n) ?.))
  2820. (setq n (1- n)))
  2821. (if (> n 1)
  2822. (ido-file-extension-aux a b n lessp)
  2823. lessp))))))
  2824. (defun ido-file-extension-aux (a b n lessp)
  2825. (let ((oa (ido-file-extension-order a n))
  2826. (ob (ido-file-extension-order b n)))
  2827. (cond
  2828. ((and oa ob)
  2829. (cond
  2830. ((= oa ob)
  2831. lessp)
  2832. (lessp
  2833. (> oa ob))
  2834. (t
  2835. (< oa ob))))
  2836. (oa
  2837. (not lessp))
  2838. (ob
  2839. lessp)
  2840. (t
  2841. lessp))))
  2842. (defun ido-file-extension-order (s n)
  2843. (let ((l ido-file-extensions-order)
  2844. (i 0) o do)
  2845. (while l
  2846. (cond
  2847. ((eq (car l) t)
  2848. (setq do i
  2849. l (cdr l)))
  2850. ((eq (compare-strings s n nil (car l) 0 nil nil) t)
  2851. (setq o i
  2852. l nil))
  2853. (t
  2854. (setq l (cdr l))))
  2855. (setq i (1+ i)))
  2856. (or o do)))
  2857. (defun ido-sort-merged-list (items promote)
  2858. ;; Input is list of ("file" . "dir") cons cells.
  2859. ;; Output is sorted list of ("file "dir" ...) lists
  2860. (let ((l (sort items (lambda (a b) (string-lessp (car b) (car a)))))
  2861. res a cur)
  2862. (while l
  2863. (setq a (car l)
  2864. l (cdr l))
  2865. (if (and res (string-equal (car (car res)) (car a)))
  2866. (progn
  2867. (setcdr (car (if cur (cdr res) res)) (cons (cdr a) (cdr (car res))))
  2868. (if (and promote (string-equal ido-current-directory (cdr a)))
  2869. (setq cur t)))
  2870. (setq res (cons (list (car a) (cdr a)) res)
  2871. cur nil)))
  2872. res))
  2873. (defun ido-wide-find-dirs-or-files (dir file &optional prefix finddir)
  2874. ;; As ido-run-find-command, but returns a list of cons pairs ("file" . "dir")
  2875. (let ((filenames
  2876. (split-string
  2877. (shell-command-to-string
  2878. (concat "find "
  2879. (shell-quote-argument dir)
  2880. " -name "
  2881. (shell-quote-argument
  2882. (concat (if prefix "" "*") file "*"))
  2883. " -type " (if finddir "d" "f") " -print"))))
  2884. filename d f
  2885. res)
  2886. (while filenames
  2887. (setq filename (car filenames)
  2888. filenames (cdr filenames))
  2889. (if (and (file-name-absolute-p filename)
  2890. (file-exists-p filename))
  2891. (setq d (file-name-directory filename)
  2892. f (file-name-nondirectory filename)
  2893. res (cons (cons (if finddir (ido-final-slash f t) f) d) res))))
  2894. res))
  2895. (defun ido-flatten-merged-list (items)
  2896. ;; Create a list of directory names based on a merged directory list.
  2897. (let (res)
  2898. (while items
  2899. (let* ((item (car items))
  2900. (file (car item))
  2901. (dirs (cdr item)))
  2902. (while dirs
  2903. (setq res (cons (concat (car dirs) file) res)
  2904. dirs (cdr dirs))))
  2905. (setq items (cdr items)))
  2906. res))
  2907. (defun ido-make-merged-file-list-1 (text auto wide)
  2908. (let (res)
  2909. (if (and (ido-final-slash text) ido-dir-file-cache)
  2910. (if wide
  2911. (setq res (ido-wide-find-dirs-or-files
  2912. ido-current-directory (substring text 0 -1) ido-enable-prefix t))
  2913. ;; Use list of cached directories
  2914. (let ((re (concat (regexp-quote (substring text 0 -1)) "[^/:]*/\\'"))
  2915. (dirs ido-dir-file-cache)
  2916. dir b d f)
  2917. (if nil ;; simple
  2918. (while dirs
  2919. (setq dir (car (car dirs))
  2920. dirs (cdr dirs))
  2921. (when (and (string-match re dir)
  2922. (not (ido-ignore-item-p dir ido-ignore-directories-merge))
  2923. (file-directory-p dir))
  2924. (setq b (substring dir 0 -1)
  2925. f (concat (file-name-nondirectory b) "/")
  2926. d (file-name-directory b)
  2927. res (cons (cons f d) res))))
  2928. (while dirs
  2929. (setq dir (car dirs)
  2930. d (car dir)
  2931. dirs (cdr dirs))
  2932. (when (not (ido-ignore-item-p d ido-ignore-directories-merge))
  2933. (setq dir (cdr (cdr dir)))
  2934. (while dir
  2935. (setq f (car dir)
  2936. dir (cdr dir))
  2937. (if (and (string-match re f)
  2938. (not (ido-ignore-item-p f ido-ignore-directories)))
  2939. (setq res (cons (cons f d) res)))))
  2940. (if (and auto (input-pending-p))
  2941. (setq dirs nil
  2942. res t))))))
  2943. (if wide
  2944. (setq res (ido-wide-find-dirs-or-files
  2945. ido-current-directory text ido-enable-prefix nil))
  2946. (let ((ido-text text)
  2947. (dirs ido-work-directory-list)
  2948. (must-match (and text (> (length text) 0)))
  2949. dir fl)
  2950. (if (and auto (not (member ido-current-directory dirs)))
  2951. (setq dirs (cons ido-current-directory dirs)))
  2952. (while dirs
  2953. (setq dir (car dirs)
  2954. dirs (cdr dirs))
  2955. (when (and dir (stringp dir)
  2956. (or ido-merge-ftp-work-directories
  2957. (not (ido-is-ftp-directory dir)))
  2958. (file-directory-p dir)
  2959. ;; TODO. check for nonreadable and too-big.
  2960. (setq fl (if (eq ido-cur-item 'file)
  2961. (ido-make-file-list-1 dir t)
  2962. (ido-make-dir-list-1 dir t))))
  2963. (if must-match
  2964. (setq fl (ido-set-matches-1 fl)))
  2965. (if fl
  2966. (setq res (nconc fl res))))
  2967. (if (and auto (input-pending-p))
  2968. (setq dirs nil
  2969. res t))))))
  2970. res))
  2971. (defun ido-make-merged-file-list (text auto wide)
  2972. (let (res)
  2973. (message "Searching for `%s'...." text)
  2974. (condition-case nil
  2975. (if (eq t (setq res
  2976. (while-no-input
  2977. (ido-make-merged-file-list-1 text auto wide))))
  2978. (setq res 'input-pending-p))
  2979. (quit
  2980. (setq res t
  2981. ido-try-merged-list nil
  2982. ido-use-merged-list nil)))
  2983. (when (and res (listp res))
  2984. (setq res (ido-sort-merged-list res auto)))
  2985. (when (and (or ido-rotate-temp ido-rotate-file-list-default)
  2986. (listp res)
  2987. (> (length text) 0))
  2988. (let ((elt (assoc text res)))
  2989. (when (and elt (not (eq elt (car res))))
  2990. (setq res (delq elt res))
  2991. (setq res (cons elt res)))))
  2992. (message nil)
  2993. res))
  2994. (defun ido-make-buffer-list-1 (&optional frame visible)
  2995. ;; Return list of non-ignored buffer names
  2996. (delq nil
  2997. (mapcar
  2998. (lambda (x)
  2999. (let ((name (buffer-name x)))
  3000. (if (not (or (ido-ignore-item-p name ido-ignore-buffers) (member name visible)))
  3001. name)))
  3002. (buffer-list frame))))
  3003. (defun ido-make-buffer-list (default)
  3004. ;; Return the current list of buffers.
  3005. ;; Currently visible buffers are put at the end of the list.
  3006. ;; The hook `ido-make-buffer-list-hook' is run after the list has been
  3007. ;; created to allow the user to further modify the order of the buffer names
  3008. ;; in this list. If DEFAULT is non-nil, and corresponds to an existing buffer,
  3009. ;; it is put to the start of the list.
  3010. (let* ((ido-current-buffers (ido-get-buffers-in-frames 'current))
  3011. (ido-temp-list (ido-make-buffer-list-1 (selected-frame) ido-current-buffers)))
  3012. (if ido-temp-list
  3013. (nconc ido-temp-list ido-current-buffers)
  3014. (setq ido-temp-list ido-current-buffers))
  3015. (if default
  3016. (setq ido-temp-list
  3017. (cons default (delete default ido-temp-list))))
  3018. (if ido-use-virtual-buffers
  3019. (ido-add-virtual-buffers-to-list))
  3020. (run-hooks 'ido-make-buffer-list-hook)
  3021. ido-temp-list))
  3022. (defun ido-add-virtual-buffers-to-list ()
  3023. "Add recently visited files, and bookmark files, to the buffer list.
  3024. This is to make them appear as if they were \"virtual buffers\"."
  3025. ;; If no buffers matched, and virtual buffers are being used, then
  3026. ;; consult the list of past visited files, to see if we can find
  3027. ;; the file which the user might thought was still open.
  3028. (unless recentf-mode (recentf-mode 1))
  3029. (setq ido-virtual-buffers nil)
  3030. (let (name)
  3031. (dolist (head recentf-list)
  3032. (and (setq name (file-name-nondirectory head))
  3033. (null (get-file-buffer head))
  3034. (not (assoc name ido-virtual-buffers))
  3035. (not (member name ido-temp-list))
  3036. (not (ido-ignore-item-p name ido-ignore-buffers))
  3037. ;;(file-exists-p head)
  3038. (push (cons name head) ido-virtual-buffers))))
  3039. (when ido-virtual-buffers
  3040. (if ido-use-faces
  3041. (dolist (comp ido-virtual-buffers)
  3042. (put-text-property 0 (length (car comp))
  3043. 'face 'ido-virtual
  3044. (car comp))))
  3045. (setq ido-temp-list
  3046. (nconc ido-temp-list
  3047. (nreverse (mapcar #'car ido-virtual-buffers))))))
  3048. (defun ido-make-choice-list (default)
  3049. ;; Return the current list of choices.
  3050. ;; If DEFAULT is non-nil, and corresponds to an element of choices,
  3051. ;; it is put to the start of the list.
  3052. (let ((ido-temp-list ido-choice-list))
  3053. (if default
  3054. (progn
  3055. (setq ido-temp-list
  3056. (delete default ido-temp-list))
  3057. (setq ido-temp-list
  3058. (cons default ido-temp-list))))
  3059. ; (run-hooks 'ido-make-choice-list-hook)
  3060. ido-temp-list))
  3061. (defun ido-to-end (items)
  3062. ;; Move the elements from ITEMS to the end of `ido-temp-list'
  3063. (mapc
  3064. (lambda (elem)
  3065. (setq ido-temp-list (delq elem ido-temp-list)))
  3066. items)
  3067. (if ido-temp-list
  3068. (nconc ido-temp-list items)
  3069. (setq ido-temp-list items)))
  3070. (defun ido-file-name-all-completions-1 (dir)
  3071. (cond
  3072. ((ido-nonreadable-directory-p dir) '())
  3073. ;; do not check (ido-directory-too-big-p dir) here.
  3074. ;; Caller must have done that if necessary.
  3075. ((and ido-enable-tramp-completion
  3076. (string-match "\\`/[^/]+[:@]\\'" dir))
  3077. ;; Strip method:user@host: part of tramp completions.
  3078. ;; Tramp completions do not include leading slash.
  3079. (let* ((len (1- (length dir)))
  3080. (non-essential t)
  3081. (compl
  3082. (or (file-name-all-completions "" dir)
  3083. ;; work around bug in ange-ftp.
  3084. ;; /ftp:user@host: => nil
  3085. ;; /ftp:user@host:./ => ok
  3086. (and
  3087. (not (string= "/ftp:" dir))
  3088. (file-remote-p dir)
  3089. ;; tramp-ftp-file-name-p is available only when tramp
  3090. ;; has been loaded.
  3091. (fboundp 'tramp-ftp-file-name-p)
  3092. (funcall 'tramp-ftp-file-name-p dir)
  3093. (string-match ":\\'" dir)
  3094. (file-name-all-completions "" (concat dir "./"))))))
  3095. (if (and compl
  3096. (> (length (car compl)) len)
  3097. (string= (substring (car compl) 0 len) (substring dir 1)))
  3098. (mapcar (lambda (c) (substring c len)) compl)
  3099. compl)))
  3100. (t
  3101. (file-name-all-completions "" dir))))
  3102. (defun ido-file-name-all-completions (dir)
  3103. ;; Return name of all files in DIR
  3104. ;; Uses and updates ido-dir-file-cache
  3105. (cond
  3106. ((ido-is-unc-root dir)
  3107. (mapcar
  3108. (lambda (host)
  3109. (if (string-match "/\\'" host) host (concat host "/")))
  3110. (ido-unc-hosts t)))
  3111. ((and (numberp ido-max-dir-file-cache) (> ido-max-dir-file-cache 0)
  3112. (stringp dir) (> (length dir) 0)
  3113. (ido-may-cache-directory dir))
  3114. (let* ((cached (assoc dir ido-dir-file-cache))
  3115. (ctime (nth 1 cached))
  3116. (ftp (ido-is-ftp-directory dir))
  3117. (unc (ido-is-unc-host dir))
  3118. (attr (if (or ftp unc) nil (file-attributes dir)))
  3119. (mtime (nth 5 attr))
  3120. valid)
  3121. (when cached ; should we use the cached entry ?
  3122. (cond
  3123. (ftp
  3124. (setq valid (and (eq (car ctime) 'ftp)
  3125. (ido-cache-ftp-valid (cdr ctime)))))
  3126. (unc
  3127. (setq valid (and (eq (car ctime) 'unc)
  3128. (ido-cache-unc-valid (cdr ctime)))))
  3129. (t
  3130. (if attr
  3131. (setq valid (and (= (car ctime) (car mtime))
  3132. (= (car (cdr ctime)) (car (cdr mtime))))))))
  3133. (unless valid
  3134. (setq ido-dir-file-cache (delq cached ido-dir-file-cache)
  3135. cached nil)))
  3136. (unless cached
  3137. (cond
  3138. (unc
  3139. (setq mtime (cons 'unc (ido-time-stamp))))
  3140. ((and ftp (file-readable-p dir))
  3141. (setq mtime (cons 'ftp (ido-time-stamp)))))
  3142. (if mtime
  3143. (setq cached (cons dir (cons mtime (ido-file-name-all-completions-1 dir)))
  3144. ido-dir-file-cache (cons cached ido-dir-file-cache)))
  3145. (if (> (length ido-dir-file-cache) ido-max-dir-file-cache)
  3146. (setcdr (nthcdr (1- ido-max-dir-file-cache) ido-dir-file-cache) nil)))
  3147. (and cached
  3148. (cdr (cdr cached)))))
  3149. (t
  3150. (ido-file-name-all-completions-1 dir))))
  3151. (defun ido-remove-cached-dir (dir)
  3152. ;; Remove dir from ido-dir-file-cache
  3153. (if (and ido-dir-file-cache
  3154. (stringp dir) (> (length dir) 0))
  3155. (let ((cached (assoc dir ido-dir-file-cache)))
  3156. (if cached
  3157. (setq ido-dir-file-cache (delq cached ido-dir-file-cache))))))
  3158. (defun ido-make-file-list-1 (dir &optional merged)
  3159. ;; Return list of non-ignored files in DIR
  3160. ;; If MERGED is non-nil, each file is cons'ed with DIR
  3161. (and (or (ido-is-tramp-root dir) (ido-is-unc-root dir)
  3162. (file-directory-p dir))
  3163. (delq nil
  3164. (mapcar
  3165. (lambda (name)
  3166. (if (not (ido-ignore-item-p name ido-ignore-files t))
  3167. (if merged (cons name dir) name)))
  3168. (ido-file-name-all-completions dir)))))
  3169. (defun ido-make-file-list (default)
  3170. ;; Return the current list of files.
  3171. ;; Currently visible files are put at the end of the list.
  3172. ;; The hook `ido-make-file-list-hook' is run after the list has been
  3173. ;; created to allow the user to further modify the order of the file names
  3174. ;; in this list.
  3175. (let ((ido-temp-list (ido-make-file-list-1 ido-current-directory)))
  3176. (setq ido-temp-list (sort ido-temp-list
  3177. (if ido-file-extensions-order
  3178. #'ido-file-extension-lessp
  3179. #'ido-file-lessp)))
  3180. (unless (ido-is-tramp-root ido-current-directory)
  3181. (let ((default-directory ido-current-directory))
  3182. (ido-to-end ;; move ftp hosts and visited files to end
  3183. (delq nil (mapcar
  3184. (lambda (x) (if (or (and (string-match ".:\\'" x)
  3185. (not (ido-local-file-exists-p x)))
  3186. (and (not (ido-final-slash x))
  3187. (let (file-name-handler-alist)
  3188. (get-file-buffer x)))) x))
  3189. ido-temp-list)))))
  3190. (ido-to-end ;; move . files to end
  3191. (delq nil (mapcar
  3192. (lambda (x) (if (string-equal (substring x 0 1) ".") x))
  3193. ido-temp-list)))
  3194. (if (and default (member default ido-temp-list))
  3195. (if (or ido-rotate-temp ido-rotate-file-list-default)
  3196. (unless (equal default (car ido-temp-list))
  3197. (let ((l ido-temp-list) k)
  3198. (while (and l (cdr l) (not (equal default (car (cdr l)))))
  3199. (setq l (cdr l)))
  3200. (setq k (cdr l))
  3201. (setcdr l nil)
  3202. (nconc k ido-temp-list)
  3203. (setq ido-temp-list k)))
  3204. (setq ido-temp-list
  3205. (delete default ido-temp-list))
  3206. (setq ido-temp-list
  3207. (cons default ido-temp-list))))
  3208. (when ido-show-dot-for-dired
  3209. (setq ido-temp-list (delete "." ido-temp-list))
  3210. (setq ido-temp-list (cons "." ido-temp-list)))
  3211. (run-hooks 'ido-make-file-list-hook)
  3212. ido-temp-list))
  3213. (defun ido-make-dir-list-1 (dir &optional merged)
  3214. ;; Return list of non-ignored subdirs in DIR
  3215. ;; If MERGED is non-nil, each subdir is cons'ed with DIR
  3216. (and (or (ido-is-tramp-root dir) (file-directory-p dir))
  3217. (delq nil
  3218. (mapcar
  3219. (lambda (name)
  3220. (and (ido-final-slash name) (not (ido-ignore-item-p name ido-ignore-directories))
  3221. (if merged (cons name dir) name)))
  3222. (ido-file-name-all-completions dir)))))
  3223. (defun ido-make-dir-list (default)
  3224. ;; Return the current list of directories.
  3225. ;; The hook `ido-make-dir-list-hook' is run after the list has been
  3226. ;; created to allow the user to further modify the order of the
  3227. ;; directory names in this list.
  3228. (let ((ido-temp-list (ido-make-dir-list-1 ido-current-directory)))
  3229. (setq ido-temp-list (sort ido-temp-list #'ido-file-lessp))
  3230. (ido-to-end ;; move . files to end
  3231. (delq nil (mapcar
  3232. (lambda (x) (if (string-equal (substring x 0 1) ".") x))
  3233. ido-temp-list)))
  3234. (if (and default (member default ido-temp-list))
  3235. (if (or ido-rotate-temp ido-rotate-file-list-default)
  3236. (unless (equal default (car ido-temp-list))
  3237. (let ((l ido-temp-list) k)
  3238. (while (and l (cdr l) (not (equal default (car (cdr l)))))
  3239. (setq l (cdr l)))
  3240. (setq k (cdr l))
  3241. (setcdr l nil)
  3242. (nconc k ido-temp-list)
  3243. (setq ido-temp-list k)))
  3244. (setq ido-temp-list
  3245. (delete default ido-temp-list))
  3246. (setq ido-temp-list
  3247. (cons default ido-temp-list))))
  3248. (setq ido-temp-list (delete "." ido-temp-list))
  3249. (unless ido-input-stack
  3250. (setq ido-temp-list (cons "." ido-temp-list)))
  3251. (run-hooks 'ido-make-dir-list-hook)
  3252. ido-temp-list))
  3253. ;; List of the files visible in the current frame.
  3254. (defvar ido-bufs-in-frame)
  3255. (defun ido-get-buffers-in-frames (&optional current)
  3256. ;; Return the list of buffers that are visible in the current frame.
  3257. ;; If optional argument `current' is given, restrict searching to the
  3258. ;; current frame, rather than all frames, regardless of value of
  3259. ;; `ido-all-frames'.
  3260. (let ((ido-bufs-in-frame nil))
  3261. (walk-windows 'ido-get-bufname nil
  3262. (if current
  3263. nil
  3264. ido-all-frames))
  3265. ido-bufs-in-frame))
  3266. (defun ido-get-bufname (win)
  3267. ;; Used by `ido-get-buffers-in-frames' to walk through all windows
  3268. (let ((buf (buffer-name (window-buffer win))))
  3269. (unless (or (member buf ido-bufs-in-frame)
  3270. (member buf ido-ignore-item-temp-list))
  3271. ;; Only add buf if it is not already in list.
  3272. ;; This prevents same buf in two different windows being
  3273. ;; put into the list twice.
  3274. (setq ido-bufs-in-frame
  3275. (cons buf ido-bufs-in-frame)))))
  3276. ;;; FIND MATCHING ITEMS
  3277. (defun ido-set-matches-1 (items &optional do-full)
  3278. ;; Return list of matches in items
  3279. (let* ((case-fold-search ido-case-fold)
  3280. (slash (and (not ido-enable-prefix) (ido-final-slash ido-text)))
  3281. (text (if slash (substring ido-text 0 -1) ido-text))
  3282. (rex0 (if ido-enable-regexp text (regexp-quote text)))
  3283. (rexq (concat rex0 (if slash ".*/" "")))
  3284. (re (if ido-enable-prefix (concat "\\`" rexq) rexq))
  3285. (full-re (and do-full (not ido-enable-regexp) (not (string-match "\$\\'" rex0))
  3286. (concat "\\`" rex0 (if slash "/" "") "\\'")))
  3287. (suffix-re (and do-full slash
  3288. (not ido-enable-regexp) (not (string-match "\$\\'" rex0))
  3289. (concat rex0 "/\\'")))
  3290. (prefix-re (and full-re (not ido-enable-prefix)
  3291. (concat "\\`" rexq)))
  3292. (non-prefix-dot (or (not ido-enable-dot-prefix)
  3293. (not ido-process-ignore-lists)
  3294. ido-enable-prefix
  3295. (= (length ido-text) 0)))
  3296. full-matches suffix-matches prefix-matches matches)
  3297. (setq ido-incomplete-regexp nil)
  3298. (condition-case error
  3299. (mapc
  3300. (lambda (item)
  3301. (let ((name (ido-name item)))
  3302. (if (and (or non-prefix-dot
  3303. (if (= (aref ido-text 0) ?.)
  3304. (= (aref name 0) ?.)
  3305. (/= (aref name 0) ?.)))
  3306. (string-match re name))
  3307. (cond
  3308. ((and (eq ido-cur-item 'buffer)
  3309. (or (not (stringp ido-default-item))
  3310. (not (string= name ido-default-item)))
  3311. (string= name (buffer-name ido-entry-buffer)))
  3312. (setq matches (cons item matches)))
  3313. ((and full-re (string-match full-re name))
  3314. (setq full-matches (cons item full-matches)))
  3315. ((and suffix-re (string-match suffix-re name))
  3316. (setq suffix-matches (cons item suffix-matches)))
  3317. ((and prefix-re (string-match prefix-re name))
  3318. (setq prefix-matches (cons item prefix-matches)))
  3319. (t (setq matches (cons item matches))))))
  3320. t)
  3321. items)
  3322. (invalid-regexp
  3323. (setq ido-incomplete-regexp t
  3324. ;; Consider the invalid regexp message internally as a
  3325. ;; special-case single match, and handle appropriately
  3326. ;; elsewhere.
  3327. matches (cdr error))))
  3328. (when prefix-matches
  3329. (ido-trace "prefix match" prefix-matches)
  3330. ;; Bug#2042.
  3331. (setq matches (nconc prefix-matches matches)))
  3332. (when suffix-matches
  3333. (ido-trace "suffix match" (list text suffix-re suffix-matches))
  3334. (setq matches (nconc suffix-matches matches)))
  3335. (when full-matches
  3336. (ido-trace "full match" (list text full-re full-matches))
  3337. (setq matches (nconc full-matches matches)))
  3338. (when (and (null matches)
  3339. ido-enable-flex-matching
  3340. (> (length ido-text) 1)
  3341. (not ido-enable-regexp))
  3342. (setq re (mapconcat #'regexp-quote (split-string ido-text "") ".*"))
  3343. (if ido-enable-prefix
  3344. (setq re (concat "\\`" re)))
  3345. (mapc
  3346. (lambda (item)
  3347. (let ((name (ido-name item)))
  3348. (if (string-match re name)
  3349. (setq matches (cons item matches)))))
  3350. items))
  3351. matches))
  3352. (defun ido-set-matches ()
  3353. ;; Set `ido-matches' to the list of items matching prompt
  3354. (when ido-rescan
  3355. (setq ido-matches (ido-set-matches-1 (reverse ido-cur-list) (not ido-rotate))
  3356. ido-rotate nil)))
  3357. (defun ido-ignore-item-p (name re-list &optional ignore-ext)
  3358. ;; Return t if the buffer or file NAME should be ignored.
  3359. (or (member name ido-ignore-item-temp-list)
  3360. (and
  3361. ido-process-ignore-lists re-list
  3362. (save-match-data
  3363. (let ((ext-list (and ignore-ext ido-ignore-extensions
  3364. completion-ignored-extensions))
  3365. (case-fold-search ido-case-fold)
  3366. ignorep nextstr
  3367. (flen (length name)) slen)
  3368. (while ext-list
  3369. (setq nextstr (car ext-list))
  3370. (if (cond
  3371. ((stringp nextstr)
  3372. (and (>= flen (setq slen (length nextstr)))
  3373. (string-equal (substring name (- flen slen)) nextstr)))
  3374. ((functionp nextstr) (funcall nextstr name))
  3375. (t nil))
  3376. (setq ignorep t
  3377. ext-list nil
  3378. re-list nil)
  3379. (setq ext-list (cdr ext-list))))
  3380. (while re-list
  3381. (setq nextstr (car re-list))
  3382. (if (cond
  3383. ((stringp nextstr) (string-match nextstr name))
  3384. ((functionp nextstr) (funcall nextstr name))
  3385. (t nil))
  3386. (setq ignorep t
  3387. re-list nil)
  3388. (setq re-list (cdr re-list))))
  3389. ;; return the result
  3390. (if ignorep
  3391. (setq ido-ignored-list (cons name ido-ignored-list)))
  3392. ignorep)))))
  3393. ;; Private variable used by `ido-word-matching-substring'.
  3394. (defvar ido-change-word-sub)
  3395. (defun ido-find-common-substring (items subs)
  3396. ;; Return common string following SUBS in each element of ITEMS.
  3397. (let (res
  3398. alist
  3399. ido-change-word-sub)
  3400. (setq ido-change-word-sub
  3401. (if ido-enable-regexp
  3402. subs
  3403. (regexp-quote subs)))
  3404. (setq res (mapcar #'ido-word-matching-substring items))
  3405. (setq res (delq nil res)) ;; remove any nil elements (shouldn't happen)
  3406. (setq alist (mapcar #'ido-makealist res)) ;; could use an OBARRAY
  3407. ;; try-completion returns t if there is an exact match.
  3408. (let* ((completion-ignore-case ido-case-fold)
  3409. (comp (try-completion subs alist)))
  3410. (if (eq comp t)
  3411. subs
  3412. comp))))
  3413. (defun ido-word-matching-substring (word)
  3414. ;; Return part of WORD before 1st match to `ido-change-word-sub'.
  3415. ;; If `ido-change-word-sub' cannot be found in WORD, return nil.
  3416. (let ((case-fold-search ido-case-fold))
  3417. (let ((m (string-match ido-change-word-sub (ido-name word))))
  3418. (if m
  3419. (substring (ido-name word) m)
  3420. ;; else no match
  3421. nil))))
  3422. (defun ido-makealist (res)
  3423. ;; Return dotted pair (RES . 1).
  3424. (cons res 1))
  3425. (defun ido-choose-completion-string (choice &rest ignored)
  3426. (when (ido-active)
  3427. ;; Insert the completion into the buffer where completion was requested.
  3428. (and ido-completion-buffer
  3429. (get-buffer ido-completion-buffer)
  3430. (kill-buffer ido-completion-buffer))
  3431. (cond
  3432. ((ido-active t) ;; ido-use-merged-list
  3433. (setq ido-current-directory ""
  3434. ido-text choice
  3435. ido-exit 'done))
  3436. ((not (ido-final-slash choice))
  3437. (setq ido-text choice
  3438. ido-exit 'done))
  3439. (t
  3440. (ido-set-current-directory ido-current-directory choice)
  3441. (setq ido-exit 'refresh)))
  3442. (exit-minibuffer)
  3443. t))
  3444. (defun ido-completion-help ()
  3445. "Show possible completions in a *File Completions* buffer."
  3446. (interactive)
  3447. (setq ido-rescan nil)
  3448. (let ((temp-buf (and ido-completion-buffer
  3449. (get-buffer ido-completion-buffer)))
  3450. display-it full-list)
  3451. (if (and (eq last-command this-command) temp-buf)
  3452. ;; scroll buffer
  3453. (let (win (buf (current-buffer)))
  3454. (display-buffer temp-buf nil nil)
  3455. (set-buffer temp-buf)
  3456. (setq win (get-buffer-window temp-buf))
  3457. (if (pos-visible-in-window-p (point-max) win)
  3458. (if (or ido-completion-buffer-all-completions
  3459. (boundp 'ido-completion-buffer-full))
  3460. (set-window-start win (point-min))
  3461. (with-no-warnings
  3462. (set (make-local-variable 'ido-completion-buffer-full) t))
  3463. (setq full-list t
  3464. display-it t))
  3465. (scroll-other-window))
  3466. (set-buffer buf))
  3467. (setq display-it t))
  3468. (if (and ido-completion-buffer display-it)
  3469. (with-output-to-temp-buffer ido-completion-buffer
  3470. (let ((completion-list (sort
  3471. (cond
  3472. (ido-directory-too-big
  3473. (message "Reading directory...")
  3474. (setq ido-directory-too-big nil
  3475. ido-ignored-list nil
  3476. ido-cur-list (ido-all-completions)
  3477. ido-rescan t)
  3478. (ido-set-matches)
  3479. (or ido-matches ido-cur-list))
  3480. (ido-use-merged-list
  3481. (ido-flatten-merged-list (or ido-matches ido-cur-list)))
  3482. ((or full-list ido-completion-buffer-all-completions)
  3483. (ido-all-completions))
  3484. (t
  3485. (copy-sequence (or ido-matches ido-cur-list))))
  3486. #'ido-file-lessp)))
  3487. (if (featurep 'xemacs)
  3488. ;; XEmacs extents are put on by default, doesn't seem to be
  3489. ;; any way of switching them off.
  3490. ;; This obscure code avoids a byte compiler warning in Emacs.
  3491. (let ((f 'display-completion-list))
  3492. (funcall f completion-list
  3493. :help-string "ido "
  3494. :activate-callback
  3495. (lambda (x y z) (message "Doesn't work yet, sorry!"))))
  3496. ;; else running Emacs
  3497. ;;(add-hook 'completion-setup-hook 'completion-setup-function)
  3498. (display-completion-list completion-list)))))))
  3499. ;;; KILL CURRENT BUFFER
  3500. (defun ido-kill-buffer-at-head ()
  3501. "Kill the buffer at the head of `ido-matches'.
  3502. If cursor is not at the end of the user input, delete to end of input."
  3503. (interactive)
  3504. (if (not (eobp))
  3505. (delete-region (point) (line-end-position))
  3506. (let ((enable-recursive-minibuffers t)
  3507. (buf (ido-name (car ido-matches)))
  3508. (nextbuf (cadr ido-matches)))
  3509. (cond
  3510. ((get-buffer buf)
  3511. ;; If next match names a buffer use the buffer object; buffer
  3512. ;; name may be changed by packages such as uniquify.
  3513. (when (and nextbuf (get-buffer nextbuf))
  3514. (setq nextbuf (get-buffer nextbuf)))
  3515. (if (null (kill-buffer buf))
  3516. ;; Buffer couldn't be killed.
  3517. (setq ido-rescan t)
  3518. ;; Else `kill-buffer' succeeds so re-make the buffer list
  3519. ;; taking into account packages like uniquify may rename
  3520. ;; buffers.
  3521. (if (bufferp nextbuf)
  3522. (setq nextbuf (buffer-name nextbuf)))
  3523. (setq ido-default-item nextbuf
  3524. ido-text-init ido-text
  3525. ido-exit 'refresh)
  3526. (exit-minibuffer)))
  3527. ;; Handle virtual buffers
  3528. ((assoc buf ido-virtual-buffers)
  3529. (setq recentf-list
  3530. (delete (cdr (assoc buf ido-virtual-buffers)) recentf-list))
  3531. (setq ido-cur-list (delete buf ido-cur-list))
  3532. (setq ido-rescan t))))))
  3533. ;;; DELETE CURRENT FILE
  3534. (defun ido-delete-file-at-head ()
  3535. "Delete the file at the head of `ido-matches'.
  3536. If cursor is not at the end of the user input, delete to end of input."
  3537. (interactive)
  3538. (if (not (eobp))
  3539. (delete-region (point) (line-end-position))
  3540. (let ((enable-recursive-minibuffers t)
  3541. (file (ido-name (car ido-matches))))
  3542. (if file
  3543. (setq file (concat ido-current-directory file)))
  3544. (when (and file
  3545. (file-exists-p file)
  3546. (not (file-directory-p file))
  3547. (file-writable-p ido-current-directory)
  3548. (yes-or-no-p (concat "Delete " file "? ")))
  3549. (delete-file file)
  3550. ;; Check if file still exists.
  3551. (if (file-exists-p file)
  3552. ;; file could not be deleted
  3553. (setq ido-rescan t)
  3554. ;; else file was killed so remove name from list.
  3555. (setq ido-cur-list (delq (car ido-matches) ido-cur-list)))))))
  3556. ;;; VISIT CHOSEN BUFFER
  3557. (defun ido-visit-buffer (buffer method &optional record)
  3558. "Switch to BUFFER according to METHOD.
  3559. Record command in `command-history' if optional RECORD is non-nil."
  3560. (if (bufferp buffer)
  3561. (setq buffer (buffer-name buffer)))
  3562. (let (win newframe)
  3563. (cond
  3564. ((eq method 'kill)
  3565. (if record
  3566. (ido-record-command 'kill-buffer buffer))
  3567. (kill-buffer buffer))
  3568. ((eq method 'other-window)
  3569. (if record
  3570. (ido-record-command 'switch-to-buffer buffer))
  3571. (switch-to-buffer-other-window buffer))
  3572. ((eq method 'display)
  3573. (display-buffer buffer))
  3574. ((eq method 'other-frame)
  3575. (switch-to-buffer-other-frame buffer)
  3576. (select-frame-set-input-focus (selected-frame)))
  3577. ((and (memq method '(raise-frame maybe-frame))
  3578. window-system
  3579. (setq win (ido-buffer-window-other-frame buffer))
  3580. (or (eq method 'raise-frame)
  3581. (y-or-n-p "Jump to frame? ")))
  3582. (setq newframe (window-frame win))
  3583. (select-frame-set-input-focus newframe)
  3584. (select-window win))
  3585. ;; (eq method 'selected-window)
  3586. (t
  3587. ;; No buffer in other frames...
  3588. (if record
  3589. (ido-record-command 'switch-to-buffer buffer))
  3590. (switch-to-buffer buffer)
  3591. ))))
  3592. (defun ido-buffer-window-other-frame (buffer)
  3593. ;; Return window pointer if BUFFER is visible in another frame.
  3594. ;; If BUFFER is visible in the current frame, return nil.
  3595. (let ((blist (ido-get-buffers-in-frames 'current)))
  3596. ;;If the buffer is visible in current frame, return nil
  3597. (if (member buffer blist)
  3598. nil
  3599. ;; maybe in other frame or icon
  3600. (get-buffer-window buffer 0) ; better than 'visible
  3601. )))
  3602. ;;; ----------- IDONIZED FUNCTIONS ------------
  3603. ;;;###autoload
  3604. (defun ido-switch-buffer ()
  3605. "Switch to another buffer.
  3606. The buffer is displayed according to `ido-default-buffer-method' -- the
  3607. default is to show it in the same window, unless it is already visible
  3608. in another frame.
  3609. As you type in a string, all of the buffers matching the string are
  3610. displayed if substring-matching is used \(default). Look at
  3611. `ido-enable-prefix' and `ido-toggle-prefix'. When you have found the
  3612. buffer you want, it can then be selected. As you type, most keys have
  3613. their normal keybindings, except for the following: \\<ido-buffer-completion-map>
  3614. RET Select the buffer at the front of the list of matches. If the
  3615. list is empty, possibly prompt to create new buffer.
  3616. \\[ido-select-text] Select the current prompt as the buffer.
  3617. If no buffer is found, prompt for a new one.
  3618. \\[ido-next-match] Put the first element at the end of the list.
  3619. \\[ido-prev-match] Put the last element at the start of the list.
  3620. \\[ido-complete] Complete a common suffix to the current string that
  3621. matches all buffers. If there is only one match, select that buffer.
  3622. If there is no common suffix, show a list of all matching buffers
  3623. in a separate window.
  3624. \\[ido-edit-input] Edit input string.
  3625. \\[ido-fallback-command] Fallback to non-ido version of current command.
  3626. \\[ido-toggle-regexp] Toggle regexp searching.
  3627. \\[ido-toggle-prefix] Toggle between substring and prefix matching.
  3628. \\[ido-toggle-case] Toggle case-sensitive searching of buffer names.
  3629. \\[ido-completion-help] Show list of matching buffers in separate window.
  3630. \\[ido-enter-find-file] Drop into `ido-find-file'.
  3631. \\[ido-kill-buffer-at-head] Kill buffer at head of buffer list.
  3632. \\[ido-toggle-ignore] Toggle ignoring buffers listed in `ido-ignore-buffers'."
  3633. (interactive)
  3634. (ido-buffer-internal ido-default-buffer-method))
  3635. ;;;###autoload
  3636. (defun ido-switch-buffer-other-window ()
  3637. "Switch to another buffer and show it in another window.
  3638. The buffer name is selected interactively by typing a substring.
  3639. For details of keybindings, see `ido-switch-buffer'."
  3640. (interactive)
  3641. (ido-buffer-internal 'other-window 'switch-to-buffer-other-window))
  3642. ;;;###autoload
  3643. (defun ido-display-buffer ()
  3644. "Display a buffer in another window but don't select it.
  3645. The buffer name is selected interactively by typing a substring.
  3646. For details of keybindings, see `ido-switch-buffer'."
  3647. (interactive)
  3648. (ido-buffer-internal 'display 'display-buffer nil nil nil 'ignore))
  3649. ;;;###autoload
  3650. (defun ido-kill-buffer ()
  3651. "Kill a buffer.
  3652. The buffer name is selected interactively by typing a substring.
  3653. For details of keybindings, see `ido-switch-buffer'."
  3654. (interactive)
  3655. (ido-buffer-internal 'kill 'kill-buffer "Kill buffer: " (buffer-name (current-buffer)) nil 'ignore))
  3656. ;;;###autoload
  3657. (defun ido-insert-buffer ()
  3658. "Insert contents of a buffer in current buffer after point.
  3659. The buffer name is selected interactively by typing a substring.
  3660. For details of keybindings, see `ido-switch-buffer'."
  3661. (interactive)
  3662. (ido-buffer-internal 'insert 'insert-buffer "Insert buffer: " nil nil 'ido-enter-insert-file))
  3663. ;;;###autoload
  3664. (defun ido-switch-buffer-other-frame ()
  3665. "Switch to another buffer and show it in another frame.
  3666. The buffer name is selected interactively by typing a substring.
  3667. For details of keybindings, see `ido-switch-buffer'."
  3668. (interactive)
  3669. (if ido-mode
  3670. (ido-buffer-internal 'other-frame)
  3671. (call-interactively 'switch-to-buffer-other-frame)))
  3672. ;;;###autoload
  3673. (defun ido-find-file-in-dir (dir)
  3674. "Switch to another file starting from DIR."
  3675. (interactive "DDir: ")
  3676. (setq dir (file-name-as-directory dir))
  3677. (ido-file-internal ido-default-file-method nil dir nil nil nil 'ignore))
  3678. ;;;###autoload
  3679. (defun ido-find-file ()
  3680. "Edit file with name obtained via minibuffer.
  3681. The file is displayed according to `ido-default-file-method' -- the
  3682. default is to show it in the same window, unless it is already
  3683. visible in another frame.
  3684. The file name is selected interactively by typing a substring. As you
  3685. type in a string, all of the filenames matching the string are displayed
  3686. if substring-matching is used \(default). Look at `ido-enable-prefix' and
  3687. `ido-toggle-prefix'. When you have found the filename you want, it can
  3688. then be selected. As you type, most keys have their normal keybindings,
  3689. except for the following: \\<ido-file-completion-map>
  3690. RET Select the file at the front of the list of matches. If the
  3691. list is empty, possibly prompt to create new file.
  3692. \\[ido-select-text] Select the current prompt as the buffer or file.
  3693. If no buffer or file is found, prompt for a new one.
  3694. \\[ido-next-match] Put the first element at the end of the list.
  3695. \\[ido-prev-match] Put the last element at the start of the list.
  3696. \\[ido-complete] Complete a common suffix to the current string that
  3697. matches all files. If there is only one match, select that file.
  3698. If there is no common suffix, show a list of all matching files
  3699. in a separate window.
  3700. \\[ido-edit-input] Edit input string (including directory).
  3701. \\[ido-prev-work-directory] or \\[ido-next-work-directory] go to previous/next directory in work directory history.
  3702. \\[ido-merge-work-directories] search for file in the work directory history.
  3703. \\[ido-forget-work-directory] removes current directory from the work directory history.
  3704. \\[ido-prev-work-file] or \\[ido-next-work-file] cycle through the work file history.
  3705. \\[ido-wide-find-file-or-pop-dir] and \\[ido-wide-find-dir-or-delete-dir] prompts and uses find to locate files or directories.
  3706. \\[ido-make-directory] prompts for a directory to create in current directory.
  3707. \\[ido-fallback-command] Fallback to non-ido version of current command.
  3708. \\[ido-toggle-regexp] Toggle regexp searching.
  3709. \\[ido-toggle-prefix] Toggle between substring and prefix matching.
  3710. \\[ido-toggle-case] Toggle case-sensitive searching of file names.
  3711. \\[ido-toggle-literal] Toggle literal reading of this file.
  3712. \\[ido-completion-help] Show list of matching files in separate window.
  3713. \\[ido-toggle-ignore] Toggle ignoring files listed in `ido-ignore-files'."
  3714. (interactive)
  3715. (ido-file-internal ido-default-file-method))
  3716. ;;;###autoload
  3717. (defun ido-find-file-other-window ()
  3718. "Switch to another file and show it in another window.
  3719. The file name is selected interactively by typing a substring.
  3720. For details of keybindings, see `ido-find-file'."
  3721. (interactive)
  3722. (ido-file-internal 'other-window 'find-file-other-window))
  3723. ;;;###autoload
  3724. (defun ido-find-alternate-file ()
  3725. "Switch to another file and show it in another window.
  3726. The file name is selected interactively by typing a substring.
  3727. For details of keybindings, see `ido-find-file'."
  3728. (interactive)
  3729. (ido-file-internal 'alt-file 'find-alternate-file nil "Find alternate file: "))
  3730. ;;;###autoload
  3731. (defun ido-find-file-read-only ()
  3732. "Edit file read-only with name obtained via minibuffer.
  3733. The file name is selected interactively by typing a substring.
  3734. For details of keybindings, see `ido-find-file'."
  3735. (interactive)
  3736. (ido-file-internal 'read-only 'find-file-read-only nil "Find file read-only: "))
  3737. ;;;###autoload
  3738. (defun ido-find-file-read-only-other-window ()
  3739. "Edit file read-only in other window with name obtained via minibuffer.
  3740. The file name is selected interactively by typing a substring.
  3741. For details of keybindings, see `ido-find-file'."
  3742. (interactive)
  3743. (ido-file-internal 'read-only 'find-file-read-only-other-window nil "Find file read-only other window: "))
  3744. ;;;###autoload
  3745. (defun ido-find-file-read-only-other-frame ()
  3746. "Edit file read-only in other frame with name obtained via minibuffer.
  3747. The file name is selected interactively by typing a substring.
  3748. For details of keybindings, see `ido-find-file'."
  3749. (interactive)
  3750. (ido-file-internal 'read-only 'find-file-read-only-other-frame nil "Find file read-only other frame: "))
  3751. ;;;###autoload
  3752. (defun ido-display-file ()
  3753. "Display a file in another window but don't select it.
  3754. The file name is selected interactively by typing a substring.
  3755. For details of keybindings, see `ido-find-file'."
  3756. (interactive)
  3757. (ido-file-internal 'display nil nil nil nil nil 'ignore))
  3758. ;;;###autoload
  3759. (defun ido-find-file-other-frame ()
  3760. "Switch to another file and show it in another frame.
  3761. The file name is selected interactively by typing a substring.
  3762. For details of keybindings, see `ido-find-file'."
  3763. (interactive)
  3764. (ido-file-internal 'other-frame 'find-file-other-frame))
  3765. ;;;###autoload
  3766. (defun ido-write-file ()
  3767. "Write current buffer to a file.
  3768. The file name is selected interactively by typing a substring.
  3769. For details of keybindings, see `ido-find-file'."
  3770. (interactive)
  3771. (let ((ido-process-ignore-lists t)
  3772. (ido-work-directory-match-only nil)
  3773. (ido-ignore-files (cons "[^/]\\'" ido-ignore-files))
  3774. (ido-report-no-match nil)
  3775. (ido-confirm-unique-completion t)
  3776. (ido-auto-merge-work-directories-length -1))
  3777. (ido-file-internal 'write 'write-file nil "Write file: " nil nil 'ignore)))
  3778. ;;;###autoload
  3779. (defun ido-insert-file ()
  3780. "Insert contents of file in current buffer.
  3781. The file name is selected interactively by typing a substring.
  3782. For details of keybindings, see `ido-find-file'."
  3783. (interactive)
  3784. (ido-file-internal 'insert 'insert-file nil "Insert file: " nil nil 'ido-enter-insert-buffer))
  3785. ;;;###autoload
  3786. (defun ido-dired ()
  3787. "Call `dired' the ido way.
  3788. The directory is selected interactively by typing a substring.
  3789. For details of keybindings, see `ido-find-file'."
  3790. (interactive)
  3791. (let ((ido-report-no-match nil)
  3792. (ido-auto-merge-work-directories-length -1))
  3793. (ido-file-internal 'dired 'dired nil "Dired: " 'dir)))
  3794. (defun ido-list-directory ()
  3795. "Call `list-directory' the ido way.
  3796. The directory is selected interactively by typing a substring.
  3797. For details of keybindings, see `ido-find-file'."
  3798. (interactive)
  3799. (let ((ido-report-no-match nil)
  3800. (ido-auto-merge-work-directories-length -1))
  3801. (ido-file-internal 'list-directory 'list-directory nil "List directory: " 'dir)))
  3802. ;;; XEmacs hack for showing default buffer
  3803. ;; The first time we enter the minibuffer, Emacs puts up the default
  3804. ;; buffer to switch to, but XEmacs doesn't -- presumably there is a
  3805. ;; subtle difference in the two versions of post-command-hook. The
  3806. ;; default is shown for both whenever we delete all of our text
  3807. ;; though, indicating its just a problem the first time we enter the
  3808. ;; function. To solve this, we use another entry hook for emacs to
  3809. ;; show the default the first time we enter the minibuffer.
  3810. ;;; ICOMPLETE TYPE CODE
  3811. (defun ido-initiate-auto-merge (buffer)
  3812. (ido-trace "\n*merge timeout*" buffer)
  3813. (setq ido-auto-merge-timer nil)
  3814. (when (and (buffer-live-p buffer)
  3815. (ido-active)
  3816. (boundp 'ido-eoinput) ido-eoinput)
  3817. (let ((contents (buffer-substring-no-properties (minibuffer-prompt-end) ido-eoinput)))
  3818. (ido-trace "request merge")
  3819. (setq ido-use-merged-list 'auto
  3820. ido-text-init contents
  3821. ido-rotate-temp t
  3822. ido-exit 'refresh)
  3823. (with-current-buffer buffer
  3824. (ido-tidy))
  3825. (throw 'ido contents))))
  3826. (defun ido-exhibit ()
  3827. "Post command hook for `ido'."
  3828. ;; Find matching files and display a list in the minibuffer.
  3829. ;; Copied from `icomplete-exhibit' with two changes:
  3830. ;; 1. It prints a default file name when there is no text yet entered.
  3831. ;; 2. It calls my completion routine rather than the standard completion.
  3832. (when (ido-active)
  3833. (let ((contents (buffer-substring-no-properties (minibuffer-prompt-end) (point-max)))
  3834. (buffer-undo-list t)
  3835. try-single-dir-match
  3836. refresh)
  3837. (when ido-trace-enable
  3838. (ido-trace "\nexhibit" this-command)
  3839. (ido-trace "dir" ido-current-directory)
  3840. (ido-trace "contents" contents)
  3841. (ido-trace "list" ido-cur-list)
  3842. (ido-trace "matches" ido-matches)
  3843. (ido-trace "rescan" ido-rescan))
  3844. (save-excursion
  3845. (goto-char (point-max))
  3846. ;; Register the end of input, so we know where the extra stuff (match-status info) begins:
  3847. (unless (boundp 'ido-eoinput)
  3848. ;; In case it got wiped out by major mode business:
  3849. (make-local-variable 'ido-eoinput))
  3850. (setq ido-eoinput (point))
  3851. ;; Handle explicit directory changes
  3852. (cond
  3853. ((memq ido-cur-item '(buffer list))
  3854. )
  3855. ((= (length contents) 0)
  3856. )
  3857. ((= (length contents) 1)
  3858. (cond
  3859. ((and (ido-is-tramp-root) (string-equal contents "/"))
  3860. (ido-set-current-directory ido-current-directory contents)
  3861. (setq refresh t))
  3862. ((and (ido-unc-hosts) (string-equal contents "/")
  3863. (let ((ido-enable-tramp-completion nil))
  3864. (ido-is-root-directory)))
  3865. (ido-set-current-directory "//")
  3866. (setq refresh t))
  3867. ))
  3868. ((and (string-match (if ido-enable-tramp-completion ".[:@]\\'" ".:\\'") contents)
  3869. (ido-is-root-directory) ;; Ange-ftp or tramp
  3870. (not (ido-local-file-exists-p contents)))
  3871. (ido-set-current-directory ido-current-directory contents)
  3872. (when (ido-is-slow-ftp-host)
  3873. (setq ido-exit 'fallback)
  3874. (exit-minibuffer))
  3875. (setq refresh t))
  3876. ((ido-final-slash contents) ;; xxx/
  3877. (ido-trace "final slash" contents)
  3878. (cond
  3879. ((string-equal contents "~/")
  3880. (ido-set-current-home)
  3881. (setq refresh t))
  3882. ((string-equal contents "../")
  3883. (ido-up-directory t)
  3884. (setq refresh t))
  3885. ((string-equal contents "./")
  3886. (setq refresh t))
  3887. ((string-match "\\`~[-_a-zA-Z0-9]+[$]?/\\'" contents)
  3888. (ido-trace "new home" contents)
  3889. (ido-set-current-home contents)
  3890. (setq refresh t))
  3891. ((string-match "[$][A-Za-z0-9_]+/\\'" contents)
  3892. (let ((exp (condition-case ()
  3893. (expand-file-name
  3894. (substitute-in-file-name (substring contents 0 -1))
  3895. ido-current-directory)
  3896. (error nil))))
  3897. (ido-trace contents exp)
  3898. (when (and exp (file-directory-p exp))
  3899. (ido-set-current-directory (file-name-directory exp))
  3900. (setq ido-text-init (file-name-nondirectory exp))
  3901. (setq refresh t))))
  3902. ((and (memq system-type '(windows-nt ms-dos))
  3903. (string-equal (substring contents 1) ":/"))
  3904. (ido-set-current-directory (file-name-directory contents))
  3905. (setq refresh t))
  3906. ((string-equal (substring contents -2 -1) "/")
  3907. (ido-set-current-directory
  3908. (if (memq system-type '(windows-nt ms-dos))
  3909. (expand-file-name "/" ido-current-directory)
  3910. "/"))
  3911. (setq refresh t))
  3912. ((and (or ido-directory-nonreadable ido-directory-too-big)
  3913. (file-directory-p (concat ido-current-directory (file-name-directory contents))))
  3914. (ido-set-current-directory
  3915. (concat ido-current-directory (file-name-directory contents)))
  3916. (setq refresh t))
  3917. (t
  3918. (ido-trace "try single dir")
  3919. (setq try-single-dir-match t))))
  3920. ((and (string-equal (substring contents -2 -1) "/")
  3921. (not (string-match "[$]" contents)))
  3922. (ido-set-current-directory
  3923. (cond
  3924. ((= (length contents) 2)
  3925. "/")
  3926. (ido-matches
  3927. (concat ido-current-directory (ido-name (car ido-matches))))
  3928. (t
  3929. (concat ido-current-directory (substring contents 0 -1)))))
  3930. (setq ido-text-init (substring contents -1))
  3931. (setq refresh t))
  3932. ((and (not ido-use-merged-list)
  3933. (not (ido-final-slash contents))
  3934. (eq ido-try-merged-list t)
  3935. (numberp ido-auto-merge-work-directories-length)
  3936. (> ido-auto-merge-work-directories-length 0)
  3937. (= (length contents) ido-auto-merge-work-directories-length)
  3938. (not (and ido-auto-merge-inhibit-characters-regexp
  3939. (string-match ido-auto-merge-inhibit-characters-regexp contents)))
  3940. (not (input-pending-p)))
  3941. (setq ido-use-merged-list 'auto
  3942. ido-text-init contents
  3943. ido-rotate-temp t)
  3944. (setq refresh t))
  3945. (t nil))
  3946. (when refresh
  3947. (ido-trace "refresh on /" ido-text-init)
  3948. (setq ido-exit 'refresh)
  3949. (exit-minibuffer))
  3950. ;; Update the list of matches
  3951. (setq ido-text contents)
  3952. (ido-set-matches)
  3953. (ido-trace "new " ido-matches)
  3954. (when (and ido-enter-matching-directory
  3955. ido-matches
  3956. (or (eq ido-enter-matching-directory 'first)
  3957. (null (cdr ido-matches)))
  3958. (ido-final-slash (ido-name (car ido-matches)))
  3959. (or try-single-dir-match
  3960. (eq ido-enter-matching-directory t)))
  3961. (ido-trace "single match" (car ido-matches))
  3962. (ido-set-current-directory
  3963. (concat ido-current-directory (ido-name (car ido-matches))))
  3964. (setq ido-exit 'refresh)
  3965. (exit-minibuffer))
  3966. (when (and (not ido-matches)
  3967. (not ido-directory-nonreadable)
  3968. (not ido-directory-too-big)
  3969. ;; ido-rescan ?
  3970. ido-process-ignore-lists
  3971. ido-ignored-list)
  3972. (let ((ido-process-ignore-lists nil)
  3973. (ido-rotate ido-rotate)
  3974. (ido-cur-list ido-ignored-list))
  3975. (ido-trace "try all" ido-ignored-list)
  3976. (ido-set-matches))
  3977. (when ido-matches
  3978. (ido-trace "found " ido-matches)
  3979. (setq ido-rescan t)
  3980. (setq ido-process-ignore-lists-inhibit t)
  3981. (setq ido-text-init ido-text)
  3982. (setq ido-exit 'refresh)
  3983. (exit-minibuffer)))
  3984. (when (and
  3985. ido-rescan
  3986. (not ido-matches)
  3987. (memq ido-cur-item '(file dir))
  3988. (not (ido-is-root-directory))
  3989. (> (length contents) 1)
  3990. (not (string-match "[$]" contents))
  3991. (not ido-directory-nonreadable)
  3992. (not ido-directory-too-big))
  3993. (ido-trace "merge?")
  3994. (if ido-use-merged-list
  3995. (ido-undo-merge-work-directory contents nil)
  3996. (when (and (eq ido-try-merged-list t)
  3997. (numberp ido-auto-merge-work-directories-length)
  3998. (= ido-auto-merge-work-directories-length 0)
  3999. (not (and ido-auto-merge-inhibit-characters-regexp
  4000. (string-match ido-auto-merge-inhibit-characters-regexp contents)))
  4001. (not (input-pending-p)))
  4002. (ido-trace "\n*start timer*")
  4003. (setq ido-auto-merge-timer
  4004. (run-with-timer ido-auto-merge-delay-time nil 'ido-initiate-auto-merge (current-buffer))))))
  4005. (setq ido-rescan t)
  4006. (if (and ido-use-merged-list
  4007. ido-matches
  4008. (not (string-equal (car (cdr (car ido-matches))) ido-current-directory)))
  4009. (progn
  4010. (ido-set-current-directory (car (cdr (car ido-matches))))
  4011. (setq ido-use-merged-list t
  4012. ido-exit 'keep
  4013. ido-text-init ido-text)
  4014. (exit-minibuffer)))
  4015. ;; Insert the match-status information:
  4016. (ido-set-common-completion)
  4017. (let ((inf (ido-completions contents)))
  4018. (setq ido-show-confirm-message nil)
  4019. (ido-trace "inf" inf)
  4020. (insert inf))
  4021. ))))
  4022. (defun ido-completions (name)
  4023. ;; Return the string that is displayed after the user's text.
  4024. ;; Modified from `icomplete-completions'.
  4025. (let* ((comps ido-matches)
  4026. (ind (and (consp (car comps)) (> (length (cdr (car comps))) 1)
  4027. ido-merged-indicator))
  4028. first)
  4029. (if (and ind ido-use-faces)
  4030. (put-text-property 0 1 'face 'ido-indicator ind))
  4031. (if (and ido-use-faces comps)
  4032. (let* ((fn (ido-name (car comps)))
  4033. (ln (length fn)))
  4034. (setq first (format "%s" fn))
  4035. (put-text-property 0 ln 'face
  4036. (if (= (length comps) 1)
  4037. (if ido-incomplete-regexp
  4038. 'ido-incomplete-regexp
  4039. 'ido-only-match)
  4040. 'ido-first-match)
  4041. first)
  4042. (if ind (setq first (concat first ind)))
  4043. (setq comps (cons first (cdr comps)))))
  4044. (cond ((null comps)
  4045. (cond
  4046. (ido-show-confirm-message
  4047. (or (nth 10 ido-decorations) " [Confirm]"))
  4048. (ido-directory-nonreadable
  4049. (or (nth 8 ido-decorations) " [Not readable]"))
  4050. (ido-directory-too-big
  4051. (or (nth 9 ido-decorations) " [Too big]"))
  4052. (ido-report-no-match
  4053. (nth 6 ido-decorations)) ;; [No match]
  4054. (t "")))
  4055. (ido-incomplete-regexp
  4056. (concat " " (car comps)))
  4057. ((null (cdr comps)) ;one match
  4058. (concat (if (if (not ido-enable-regexp)
  4059. (= (length (ido-name (car comps))) (length name))
  4060. ;; We can't rely on the length of the input
  4061. ;; for regexps, so explicitly check for a
  4062. ;; complete match
  4063. (string-match name (ido-name (car comps)))
  4064. (string-equal (match-string 0 (ido-name (car comps)))
  4065. (ido-name (car comps))))
  4066. ""
  4067. ;; when there is one match, show the matching file name in full
  4068. (concat (nth 4 ido-decorations) ;; [ ... ]
  4069. (ido-name (car comps))
  4070. (nth 5 ido-decorations)))
  4071. (if (not ido-use-faces) (nth 7 ido-decorations)))) ;; [Matched]
  4072. (t ;multiple matches
  4073. (let* ((items (if (> ido-max-prospects 0) (1+ ido-max-prospects) 999))
  4074. (alternatives
  4075. (apply
  4076. #'concat
  4077. (cdr (apply
  4078. #'nconc
  4079. (mapcar
  4080. (lambda (com)
  4081. (setq com (ido-name com))
  4082. (setq items (1- items))
  4083. (cond
  4084. ((< items 0) ())
  4085. ((= items 0) (list (nth 3 ido-decorations))) ; " | ..."
  4086. (t
  4087. (list (or ido-separator (nth 2 ido-decorations)) ; " | "
  4088. (let ((str (substring com 0)))
  4089. (if (and ido-use-faces
  4090. (not (string= str first))
  4091. (ido-final-slash str))
  4092. (put-text-property 0 (length str) 'face 'ido-subdir str))
  4093. str)))))
  4094. comps))))))
  4095. (concat
  4096. ;; put in common completion item -- what you get by pressing tab
  4097. (if (and (stringp ido-common-match-string)
  4098. (> (length ido-common-match-string) (length name)))
  4099. (concat (nth 4 ido-decorations) ;; [ ... ]
  4100. (substring ido-common-match-string (length name))
  4101. (nth 5 ido-decorations)))
  4102. ;; list all alternatives
  4103. (nth 0 ido-decorations) ;; { ... }
  4104. alternatives
  4105. (nth 1 ido-decorations)))))))
  4106. (defun ido-minibuffer-setup ()
  4107. "Minibuffer setup hook for `ido'."
  4108. ;; Copied from `icomplete-minibuffer-setup-hook'.
  4109. (when (ido-active)
  4110. (add-hook 'pre-command-hook 'ido-tidy nil t)
  4111. (add-hook 'post-command-hook 'ido-exhibit nil t)
  4112. (when (featurep 'xemacs)
  4113. (ido-exhibit)
  4114. (goto-char (point-min)))
  4115. (run-hooks 'ido-minibuffer-setup-hook)
  4116. (when ido-initial-position
  4117. (goto-char (+ (minibuffer-prompt-end) ido-initial-position))
  4118. (setq ido-initial-position nil))))
  4119. (defun ido-tidy ()
  4120. "Pre command hook for `ido'."
  4121. ;; Remove completions display, if any, prior to new user input.
  4122. ;; Copied from `icomplete-tidy'."
  4123. (when ido-auto-merge-timer
  4124. (ido-trace "\n*cancel timer*" this-command)
  4125. (cancel-timer ido-auto-merge-timer)
  4126. (setq ido-auto-merge-timer nil))
  4127. (if (ido-active)
  4128. (if (and (boundp 'ido-eoinput)
  4129. ido-eoinput)
  4130. (if (> ido-eoinput (point-max))
  4131. ;; Oops, got rug pulled out from under us - reinit:
  4132. (setq ido-eoinput (point-max))
  4133. (let ((buffer-undo-list t))
  4134. (delete-region ido-eoinput (point-max))))
  4135. ;; Reestablish the local variable 'cause minibuffer-setup is weird:
  4136. (make-local-variable 'ido-eoinput)
  4137. (setq ido-eoinput 1))))
  4138. (defun ido-summary-buffers-to-end ()
  4139. ;; Move the summaries to the end of the buffer list.
  4140. ;; This is an example function which can be hooked on to
  4141. ;; `ido-make-buffer-list-hook'. Any buffer matching the regexps
  4142. ;; `Summary' or `output\*$'are put to the end of the list.
  4143. (let ((summaries (delq nil (mapcar
  4144. (lambda (x)
  4145. (if (or
  4146. (string-match "Summary" x)
  4147. (string-match "output\\*\\'" x))
  4148. x))
  4149. ido-temp-list))))
  4150. (ido-to-end summaries)))
  4151. ;;; Helper functions for other programs
  4152. (put 'dired-do-rename 'ido 'ignore)
  4153. (put 'ibuffer-find-file 'ido 'find-file)
  4154. (put 'dired-other-window 'ido 'dir)
  4155. ;;;###autoload
  4156. (defun ido-read-buffer (prompt &optional default require-match)
  4157. "Ido replacement for the built-in `read-buffer'.
  4158. Return the name of a buffer selected.
  4159. PROMPT is the prompt to give to the user. DEFAULT if given is the default
  4160. buffer to be selected, which will go to the front of the list.
  4161. If REQUIRE-MATCH is non-nil, an existing buffer must be selected."
  4162. (let* ((ido-current-directory nil)
  4163. (ido-directory-nonreadable nil)
  4164. (ido-directory-too-big nil)
  4165. (ido-context-switch-command 'ignore)
  4166. (buf (ido-read-internal 'buffer prompt 'ido-buffer-history default require-match)))
  4167. (if (eq ido-exit 'fallback)
  4168. (let ((read-buffer-function nil))
  4169. (run-hook-with-args 'ido-before-fallback-functions 'read-buffer)
  4170. (read-buffer prompt default require-match))
  4171. buf)))
  4172. ;;;###autoload
  4173. (defun ido-read-file-name (prompt &optional dir default-filename mustmatch initial predicate)
  4174. "Ido replacement for the built-in `read-file-name'.
  4175. Read file name, prompting with PROMPT and completing in directory DIR.
  4176. See `read-file-name' for additional parameters."
  4177. (let (filename)
  4178. (cond
  4179. ((or (eq predicate 'file-directory-p)
  4180. (eq (get this-command 'ido) 'dir)
  4181. (memq this-command ido-read-file-name-as-directory-commands))
  4182. (setq filename
  4183. (ido-read-directory-name prompt dir default-filename mustmatch initial))
  4184. (if (eq ido-exit 'fallback)
  4185. (setq filename 'fallback)))
  4186. ((and (not (eq (get this-command 'ido) 'ignore))
  4187. (not (memq this-command ido-read-file-name-non-ido))
  4188. (or (null predicate) (eq predicate 'file-exists-p)))
  4189. (let* (ido-saved-vc-hb
  4190. (ido-context-switch-command
  4191. (if (eq (get this-command 'ido) 'find-file) nil 'ignore))
  4192. (vc-handled-backends (and (boundp 'vc-handled-backends) vc-handled-backends))
  4193. (minibuffer-completing-file-name t)
  4194. (ido-current-directory (ido-expand-directory dir))
  4195. (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
  4196. (ido-directory-too-big (and (not ido-directory-nonreadable)
  4197. (ido-directory-too-big-p ido-current-directory)))
  4198. (ido-work-directory-index -1)
  4199. (ido-show-dot-for-dired (and ido-show-dot-for-dired
  4200. (not default-filename)))
  4201. (ido-work-file-index -1)
  4202. (ido-find-literal nil))
  4203. (setq ido-exit nil)
  4204. (setq filename
  4205. (ido-read-internal 'file prompt 'ido-file-history default-filename mustmatch initial))
  4206. (cond
  4207. ((eq ido-exit 'fallback)
  4208. (setq filename 'fallback))
  4209. ((eq ido-exit 'dired)
  4210. (setq filename ido-current-directory))
  4211. (filename
  4212. (setq filename
  4213. (concat ido-current-directory filename))))))
  4214. (t
  4215. (setq filename 'fallback)))
  4216. (if (eq filename 'fallback)
  4217. (let ((read-file-name-function nil))
  4218. (run-hook-with-args 'ido-before-fallback-functions 'read-file-name)
  4219. (read-file-name prompt dir default-filename mustmatch initial predicate))
  4220. filename)))
  4221. ;;;###autoload
  4222. (defun ido-read-directory-name (prompt &optional dir default-dirname mustmatch initial)
  4223. "Ido replacement for the built-in `read-directory-name'.
  4224. Read directory name, prompting with PROMPT and completing in directory DIR.
  4225. See `read-directory-name' for additional parameters."
  4226. (let* (filename
  4227. (minibuffer-completing-file-name t)
  4228. (ido-context-switch-command 'ignore)
  4229. ido-saved-vc-hb
  4230. (ido-current-directory (ido-expand-directory dir))
  4231. (ido-directory-nonreadable (not (file-readable-p ido-current-directory)))
  4232. (ido-directory-too-big (and (not ido-directory-nonreadable)
  4233. (ido-directory-too-big-p ido-current-directory)))
  4234. (ido-work-directory-index -1)
  4235. (ido-work-file-index -1))
  4236. (setq filename
  4237. (ido-read-internal 'dir prompt 'ido-file-history default-dirname mustmatch initial))
  4238. (if filename
  4239. (if (and (stringp filename) (string-equal filename "."))
  4240. ido-current-directory
  4241. (concat ido-current-directory filename)))))
  4242. ;;;###autoload
  4243. (defun ido-completing-read (prompt choices &optional _predicate require-match
  4244. initial-input hist def _inherit-input-method)
  4245. "Ido replacement for the built-in `completing-read'.
  4246. Read a string in the minibuffer with ido-style completion.
  4247. PROMPT is a string to prompt with; normally it ends in a colon and a space.
  4248. CHOICES is a list of strings which are the possible completions.
  4249. PREDICATE and INHERIT-INPUT-METHOD is currently ignored; it is included
  4250. to be compatible with `completing-read'.
  4251. If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
  4252. the input is (or completes to) an element of CHOICES or is null.
  4253. If the input is null, `ido-completing-read' returns DEF, or an empty
  4254. string if DEF is nil, regardless of the value of REQUIRE-MATCH.
  4255. If INITIAL-INPUT is non-nil, insert it in the minibuffer initially,
  4256. with point positioned at the end.
  4257. HIST, if non-nil, specifies a history list.
  4258. DEF, if non-nil, is the default value."
  4259. (let ((ido-current-directory nil)
  4260. (ido-directory-nonreadable nil)
  4261. (ido-directory-too-big nil)
  4262. (ido-context-switch-command 'ignore)
  4263. (ido-choice-list choices))
  4264. ;; Initialize ido before invoking ido-read-internal
  4265. (ido-common-initialization)
  4266. (ido-read-internal 'list prompt hist def require-match initial-input)))
  4267. (defun ido-unload-function ()
  4268. "Unload the Ido library."
  4269. (ido-mode -1)
  4270. (setq minor-mode-map-alist (assq-delete-all 'ido-mode minor-mode-map-alist))
  4271. ;; continue standard unloading
  4272. nil)
  4273. (provide 'ido)
  4274. ;;; ido.el ends here