ido.el 174 KB

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