window.el 229 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803
  1. ;;; window.el --- GNU Emacs window commands aside from those written in C
  2. ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: FSF
  5. ;; Keywords: internal
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Window tree functions.
  20. ;;; Code:
  21. (eval-when-compile (require 'cl))
  22. (defmacro save-selected-window (&rest body)
  23. "Execute BODY, then select the previously selected window.
  24. The value returned is the value of the last form in BODY.
  25. This macro saves and restores the selected window, as well as the
  26. selected window in each frame. If the previously selected window
  27. is no longer live, then whatever window is selected at the end of
  28. BODY remains selected. If the previously selected window of some
  29. frame is no longer live at the end of BODY, that frame's selected
  30. window is left alone.
  31. This macro saves and restores the current buffer, since otherwise
  32. its normal operation could make a different buffer current. The
  33. order of recently selected windows and the buffer list ordering
  34. are not altered by this macro (unless they are altered in BODY)."
  35. (declare (indent 0) (debug t))
  36. `(let ((save-selected-window-window (selected-window))
  37. ;; It is necessary to save all of these, because calling
  38. ;; select-window changes frame-selected-window for whatever
  39. ;; frame that window is in.
  40. (save-selected-window-alist
  41. (mapcar (lambda (frame) (cons frame (frame-selected-window frame)))
  42. (frame-list))))
  43. (save-current-buffer
  44. (unwind-protect
  45. (progn ,@body)
  46. (dolist (elt save-selected-window-alist)
  47. (and (frame-live-p (car elt))
  48. (window-live-p (cdr elt))
  49. (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
  50. (when (window-live-p save-selected-window-window)
  51. (select-window save-selected-window-window 'norecord))))))
  52. ;; The following two functions are like `window-next-sibling' and
  53. ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
  54. ;; they don't substitute the selected window for nil), and they return
  55. ;; nil when WINDOW doesn't have a parent (like a frame's root window or
  56. ;; a minibuffer window).
  57. (defun window-right (window)
  58. "Return WINDOW's right sibling.
  59. Return nil if WINDOW is the root window of its frame. WINDOW can
  60. be any window."
  61. (and window (window-parent window) (window-next-sibling window)))
  62. (defun window-left (window)
  63. "Return WINDOW's left sibling.
  64. Return nil if WINDOW is the root window of its frame. WINDOW can
  65. be any window."
  66. (and window (window-parent window) (window-prev-sibling window)))
  67. (defun window-child (window)
  68. "Return WINDOW's first child window."
  69. (or (window-top-child window) (window-left-child window)))
  70. (defun window-child-count (window)
  71. "Return number of WINDOW's child windows."
  72. (let ((count 0))
  73. (when (and (windowp window) (setq window (window-child window)))
  74. (while window
  75. (setq count (1+ count))
  76. (setq window (window-next-sibling window))))
  77. count))
  78. (defun window-last-child (window)
  79. "Return last child window of WINDOW."
  80. (when (and (windowp window) (setq window (window-child window)))
  81. (while (window-next-sibling window)
  82. (setq window (window-next-sibling window))))
  83. window)
  84. (defun window-valid-p (object)
  85. "Return t if OBJECT denotes a live window or internal window.
  86. Otherwise, return nil; this includes the case where OBJECT is a
  87. deleted window."
  88. (and (windowp object)
  89. (or (window-buffer object) (window-child object))
  90. t))
  91. (defun window-normalize-buffer (buffer-or-name)
  92. "Return buffer specified by BUFFER-OR-NAME.
  93. BUFFER-OR-NAME must be either a buffer or a string naming a live
  94. buffer and defaults to the current buffer."
  95. (cond
  96. ((not buffer-or-name)
  97. (current-buffer))
  98. ((bufferp buffer-or-name)
  99. (if (buffer-live-p buffer-or-name)
  100. buffer-or-name
  101. (error "Buffer %s is not a live buffer" buffer-or-name)))
  102. ((get-buffer buffer-or-name))
  103. (t
  104. (error "No such buffer %s" buffer-or-name))))
  105. (defun window-normalize-frame (frame)
  106. "Return frame specified by FRAME.
  107. FRAME must be a live frame and defaults to the selected frame."
  108. (if frame
  109. (if (frame-live-p frame)
  110. frame
  111. (error "%s is not a live frame" frame))
  112. (selected-frame)))
  113. (defun window-normalize-window (window &optional live-only)
  114. "Return window specified by WINDOW.
  115. If WINDOW is nil, return `selected-window'.
  116. If WINDOW is a live window or internal window, return WINDOW;
  117. if LIVE-ONLY is non-nil, return WINDOW for a live window only.
  118. Otherwise, signal an error."
  119. (cond ((null window)
  120. (selected-window))
  121. (live-only
  122. (if (window-live-p window)
  123. window
  124. (error "%s is not a live window" window)))
  125. ((if (window-valid-p window)
  126. window
  127. (error "%s is not a window" window)))))
  128. (defvar ignore-window-parameters nil
  129. "If non-nil, standard functions ignore window parameters.
  130. The functions currently affected by this are `split-window',
  131. `delete-window', `delete-other-windows' and `other-window'.
  132. An application may bind this to a non-nil value around calls to
  133. these functions to inhibit processing of window parameters.")
  134. (defconst window-safe-min-height 1
  135. "The absolute minimum number of lines of a window.
  136. Anything less might crash Emacs.")
  137. (defcustom window-min-height 4
  138. "The minimum number of lines of any window.
  139. The value has to accommodate a mode- or header-line if present.
  140. A value less than `window-safe-min-height' is ignored. The value
  141. of this variable is honored when windows are resized or split.
  142. Applications should never rebind this variable. To resize a
  143. window to a height less than the one specified here, an
  144. application should instead call `window-resize' with a non-nil
  145. IGNORE argument. In order to have `split-window' make a window
  146. shorter, explicitly specify the SIZE argument of that function."
  147. :type 'integer
  148. :version "24.1"
  149. :group 'windows)
  150. (defconst window-safe-min-width 2
  151. "The absolute minimum number of columns of a window.
  152. Anything less might crash Emacs.")
  153. (defcustom window-min-width 10
  154. "The minimum number of columns of any window.
  155. The value has to accommodate margins, fringes, or scrollbars if
  156. present. A value less than `window-safe-min-width' is ignored.
  157. The value of this variable is honored when windows are resized or
  158. split.
  159. Applications should never rebind this variable. To resize a
  160. window to a width less than the one specified here, an
  161. application should instead call `window-resize' with a non-nil
  162. IGNORE argument. In order to have `split-window' make a window
  163. narrower, explicitly specify the SIZE argument of that function."
  164. :type 'integer
  165. :version "24.1"
  166. :group 'windows)
  167. (defun window-combined-p (&optional window horizontal)
  168. "Return non-nil if WINDOW has siblings in a given direction.
  169. If WINDOW is omitted or nil, it defaults to the selected window.
  170. HORIZONTAL determines a direction for the window combination.
  171. If HORIZONTAL is omitted or nil, return non-nil if WINDOW is part
  172. of a vertical window combination.
  173. If HORIZONTAL is non-nil, return non-nil if WINDOW is part of a
  174. horizontal window combination."
  175. (setq window (window-normalize-window window))
  176. (let ((parent (window-parent window)))
  177. (and parent
  178. (if horizontal
  179. (window-left-child parent)
  180. (window-top-child parent)))))
  181. (defun window-combinations (window &optional horizontal)
  182. "Return largest number of windows vertically arranged within WINDOW.
  183. If WINDOW is omitted or nil, it defaults to the selected window.
  184. If HORIZONTAL is non-nil, return the largest number of
  185. windows horizontally arranged within WINDOW."
  186. (setq window (window-normalize-window window))
  187. (cond
  188. ((window-live-p window)
  189. ;; If WINDOW is live, return 1.
  190. 1)
  191. ((if horizontal
  192. (window-left-child window)
  193. (window-top-child window))
  194. ;; If WINDOW is iso-combined, return the sum of the values for all
  195. ;; child windows of WINDOW.
  196. (let ((child (window-child window))
  197. (count 0))
  198. (while child
  199. (setq count
  200. (+ (window-combinations child horizontal)
  201. count))
  202. (setq child (window-right child)))
  203. count))
  204. (t
  205. ;; If WINDOW is not iso-combined, return the maximum value of any
  206. ;; child window of WINDOW.
  207. (let ((child (window-child window))
  208. (count 1))
  209. (while child
  210. (setq count
  211. (max (window-combinations child horizontal)
  212. count))
  213. (setq child (window-right child)))
  214. count))))
  215. (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
  216. "Helper function for `walk-window-tree' and `walk-window-subtree'."
  217. (let (walk-window-tree-buffer)
  218. (while walk-window-tree-window
  219. (setq walk-window-tree-buffer
  220. (window-buffer walk-window-tree-window))
  221. (when (or walk-window-tree-buffer any)
  222. (funcall fun walk-window-tree-window))
  223. (unless walk-window-tree-buffer
  224. (walk-window-tree-1
  225. fun (window-left-child walk-window-tree-window) any)
  226. (walk-window-tree-1
  227. fun (window-top-child walk-window-tree-window) any))
  228. (if sub-only
  229. (setq walk-window-tree-window nil)
  230. (setq walk-window-tree-window
  231. (window-right walk-window-tree-window))))))
  232. (defun walk-window-tree (fun &optional frame any)
  233. "Run function FUN on each live window of FRAME.
  234. FUN must be a function with one argument - a window. FRAME must
  235. be a live frame and defaults to the selected one. ANY, if
  236. non-nil means to run FUN on all live and internal windows of
  237. FRAME.
  238. This function performs a pre-order, depth-first traversal of the
  239. window tree. If FUN changes the window tree, the result is
  240. unpredictable."
  241. (let ((walk-window-tree-frame (window-normalize-frame frame)))
  242. (walk-window-tree-1
  243. fun (frame-root-window walk-window-tree-frame) any)))
  244. (defun walk-window-subtree (fun &optional window any)
  245. "Run function FUN on the subtree of windows rooted at WINDOW.
  246. WINDOW defaults to the selected window. FUN must be a function
  247. with one argument - a window. By default, run FUN only on live
  248. windows of the subtree. If the optional argument ANY is non-nil,
  249. run FUN on all live and internal windows of the subtree. If
  250. WINDOW is live, run FUN on WINDOW only.
  251. This function performs a pre-order, depth-first traversal of the
  252. subtree rooted at WINDOW. If FUN changes that tree, the result
  253. is unpredictable."
  254. (setq window (window-normalize-window window))
  255. (walk-window-tree-1 fun window any t))
  256. (defun window-with-parameter (parameter &optional value frame any)
  257. "Return first window on FRAME with PARAMETER non-nil.
  258. FRAME defaults to the selected frame. Optional argument VALUE
  259. non-nil means only return a window whose window-parameter value
  260. for PARAMETER equals VALUE (comparison is done with `equal').
  261. Optional argument ANY non-nil means consider internal windows
  262. too."
  263. (let (this-value)
  264. (catch 'found
  265. (walk-window-tree
  266. (lambda (window)
  267. (when (and (setq this-value (window-parameter window parameter))
  268. (or (not value) (equal value this-value)))
  269. (throw 'found window)))
  270. frame any))))
  271. ;;; Atomic windows.
  272. (defun window-atom-root (&optional window)
  273. "Return root of atomic window WINDOW is a part of.
  274. WINDOW can be any window and defaults to the selected one.
  275. Return nil if WINDOW is not part of an atomic window."
  276. (setq window (window-normalize-window window))
  277. (let (root)
  278. (while (and window (window-parameter window 'window-atom))
  279. (setq root window)
  280. (setq window (window-parent window)))
  281. root))
  282. (defun window-make-atom (window)
  283. "Make WINDOW an atomic window.
  284. WINDOW must be an internal window. Return WINDOW."
  285. (if (not (window-child window))
  286. (error "Window %s is not an internal window" window)
  287. (walk-window-subtree
  288. (lambda (window)
  289. (set-window-parameter window 'window-atom t))
  290. window t)
  291. window))
  292. (defun window--atom-check-1 (window)
  293. "Subroutine of `window--atom-check'."
  294. (when window
  295. (if (window-parameter window 'window-atom)
  296. (let ((count 0))
  297. (when (or (catch 'reset
  298. (walk-window-subtree
  299. (lambda (window)
  300. (if (window-parameter window 'window-atom)
  301. (setq count (1+ count))
  302. (throw 'reset t)))
  303. window t))
  304. ;; count >= 1 must hold here. If there's no other
  305. ;; window around dissolve this atomic window.
  306. (= count 1))
  307. ;; Dissolve atomic window.
  308. (walk-window-subtree
  309. (lambda (window)
  310. (set-window-parameter window 'window-atom nil))
  311. window t)))
  312. ;; Check children.
  313. (unless (window-buffer window)
  314. (window--atom-check-1 (window-left-child window))
  315. (window--atom-check-1 (window-top-child window))))
  316. ;; Check right sibling
  317. (window--atom-check-1 (window-right window))))
  318. (defun window--atom-check (&optional frame)
  319. "Check atomicity of all windows on FRAME.
  320. FRAME defaults to the selected frame. If an atomic window is
  321. wrongly configured, reset the atomicity of all its windows on
  322. FRAME to nil. An atomic window is wrongly configured if it has
  323. no child windows or one of its child windows is not atomic."
  324. (window--atom-check-1 (frame-root-window frame)))
  325. ;; Side windows.
  326. (defvar window-sides '(left top right bottom)
  327. "Window sides.")
  328. (defcustom window-sides-vertical nil
  329. "If non-nil, left and right side windows are full height.
  330. Otherwise, top and bottom side windows are full width."
  331. :type 'boolean
  332. :group 'windows
  333. :version "24.1")
  334. (defcustom window-sides-slots '(nil nil nil nil)
  335. "Maximum number of side window slots.
  336. The value is a list of four elements specifying the number of
  337. side window slots on (in this order) the left, top, right and
  338. bottom side of each frame. If an element is a number, this means
  339. to display at most that many side windows on the corresponding
  340. side. If an element is nil, this means there's no bound on the
  341. number of slots on that side."
  342. :version "24.1"
  343. :risky t
  344. :type
  345. '(list
  346. :value (nil nil nil nil)
  347. (choice
  348. :tag "Left"
  349. :help-echo "Maximum slots of left side window."
  350. :value nil
  351. :format "%[Left%] %v\n"
  352. (const :tag "Unlimited" :format "%t" nil)
  353. (integer :tag "Number" :value 2 :size 5))
  354. (choice
  355. :tag "Top"
  356. :help-echo "Maximum slots of top side window."
  357. :value nil
  358. :format "%[Top%] %v\n"
  359. (const :tag "Unlimited" :format "%t" nil)
  360. (integer :tag "Number" :value 3 :size 5))
  361. (choice
  362. :tag "Right"
  363. :help-echo "Maximum slots of right side window."
  364. :value nil
  365. :format "%[Right%] %v\n"
  366. (const :tag "Unlimited" :format "%t" nil)
  367. (integer :tag "Number" :value 2 :size 5))
  368. (choice
  369. :tag "Bottom"
  370. :help-echo "Maximum slots of bottom side window."
  371. :value nil
  372. :format "%[Bottom%] %v\n"
  373. (const :tag "Unlimited" :format "%t" nil)
  374. (integer :tag "Number" :value 3 :size 5)))
  375. :group 'windows)
  376. (defun window--side-check (&optional frame)
  377. "Check the window-side parameter of all windows on FRAME.
  378. FRAME defaults to the selected frame. If the configuration is
  379. invalid, reset all window-side parameters to nil.
  380. A valid configuration has to preserve the following invariant:
  381. - If a window has a non-nil window-side parameter, it must have a
  382. parent window and the parent window's window-side parameter
  383. must be either nil or the same as for window.
  384. - If windows with non-nil window-side parameters exist, there
  385. must be at most one window of each side and non-side with a
  386. parent whose window-side parameter is nil and there must be no
  387. leaf window whose window-side parameter is nil."
  388. (let (normal none left top right bottom
  389. side parent parent-side)
  390. (when (or (catch 'reset
  391. (walk-window-tree
  392. (lambda (window)
  393. (setq side (window-parameter window 'window-side))
  394. (setq parent (window-parent window))
  395. (setq parent-side
  396. (and parent (window-parameter parent 'window-side)))
  397. ;; The following `cond' seems a bit tedious, but I'd
  398. ;; rather stick to using just the stack.
  399. (cond
  400. (parent-side
  401. (when (not (eq parent-side side))
  402. ;; A parent whose window-side is non-nil must
  403. ;; have a child with the same window-side.
  404. (throw 'reset t)))
  405. ;; Now check that there's more than one main window
  406. ;; for any of none, left, top, right and bottom.
  407. ((eq side 'none)
  408. (if none
  409. (throw 'reset t)
  410. (setq none t)))
  411. ((eq side 'left)
  412. (if left
  413. (throw 'reset t)
  414. (setq left t)))
  415. ((eq side 'top)
  416. (if top
  417. (throw 'reset t)
  418. (setq top t)))
  419. ((eq side 'right)
  420. (if right
  421. (throw 'reset t)
  422. (setq right t)))
  423. ((eq side 'bottom)
  424. (if bottom
  425. (throw 'reset t)
  426. (setq bottom t)))
  427. ((window-buffer window)
  428. ;; A leaf window without window-side parameter,
  429. ;; record its existence.
  430. (setq normal t))))
  431. frame t))
  432. (if none
  433. ;; At least one non-side window exists, so there must
  434. ;; be at least one side-window and no normal window.
  435. (or (not (or left top right bottom)) normal)
  436. ;; No non-side window exists, so there must be no side
  437. ;; window either.
  438. (or left top right bottom)))
  439. (walk-window-tree
  440. (lambda (window)
  441. (set-window-parameter window 'window-side nil))
  442. frame t))))
  443. (defun window--check (&optional frame)
  444. "Check atomic and side windows on FRAME.
  445. FRAME defaults to the selected frame."
  446. (window--side-check frame)
  447. (window--atom-check frame))
  448. ;;; Window sizes.
  449. (defvar window-size-fixed nil
  450. "Non-nil in a buffer means windows displaying the buffer are fixed-size.
  451. If the value is `height', then only the window's height is fixed.
  452. If the value is `width', then only the window's width is fixed.
  453. Any other non-nil value fixes both the width and the height.
  454. Emacs won't change the size of any window displaying that buffer,
  455. unless it has no other choice (like when deleting a neighboring
  456. window).")
  457. (make-variable-buffer-local 'window-size-fixed)
  458. (defun window--size-ignore (window ignore)
  459. "Return non-nil if IGNORE says to ignore size restrictions for WINDOW."
  460. (if (window-valid-p ignore) (eq window ignore) ignore))
  461. (defun window-min-size (&optional window horizontal ignore)
  462. "Return the minimum size of WINDOW.
  463. WINDOW can be an arbitrary window and defaults to the selected
  464. one. Optional argument HORIZONTAL non-nil means return the
  465. minimum number of columns of WINDOW; otherwise return the minimum
  466. number of WINDOW's lines.
  467. Optional argument IGNORE, if non-nil, means ignore restrictions
  468. imposed by fixed size windows, `window-min-height' or
  469. `window-min-width' settings. If IGNORE equals `safe', live
  470. windows may get as small as `window-safe-min-height' lines and
  471. `window-safe-min-width' columns. If IGNORE is a window, ignore
  472. restrictions for that window only. Any other non-nil value
  473. means ignore all of the above restrictions for all windows."
  474. (window--min-size-1
  475. (window-normalize-window window) horizontal ignore))
  476. (defun window--min-size-1 (window horizontal ignore)
  477. "Internal function of `window-min-size'."
  478. (let ((sub (window-child window)))
  479. (if sub
  480. (let ((value 0))
  481. ;; WINDOW is an internal window.
  482. (if (window-combined-p sub horizontal)
  483. ;; The minimum size of an iso-combination is the sum of
  484. ;; the minimum sizes of its child windows.
  485. (while sub
  486. (setq value (+ value
  487. (window--min-size-1 sub horizontal ignore)))
  488. (setq sub (window-right sub)))
  489. ;; The minimum size of an ortho-combination is the maximum of
  490. ;; the minimum sizes of its child windows.
  491. (while sub
  492. (setq value (max value
  493. (window--min-size-1 sub horizontal ignore)))
  494. (setq sub (window-right sub))))
  495. value)
  496. (with-current-buffer (window-buffer window)
  497. (cond
  498. ((and (not (window--size-ignore window ignore))
  499. (window-size-fixed-p window horizontal))
  500. ;; The minimum size of a fixed size window is its size.
  501. (window-total-size window horizontal))
  502. ((or (eq ignore 'safe) (eq ignore window))
  503. ;; If IGNORE equals `safe' or WINDOW return the safe values.
  504. (if horizontal window-safe-min-width window-safe-min-height))
  505. (horizontal
  506. ;; For the minimum width of a window take fringes and
  507. ;; scroll-bars into account. This is questionable and should
  508. ;; be removed as soon as we are able to split (and resize)
  509. ;; windows such that the new (or resized) windows can get a
  510. ;; size less than the user-specified `window-min-height' and
  511. ;; `window-min-width'.
  512. (let ((frame (window-frame window))
  513. (fringes (window-fringes window))
  514. (scroll-bars (window-scroll-bars window)))
  515. (max
  516. (+ window-safe-min-width
  517. (ceiling (car fringes) (frame-char-width frame))
  518. (ceiling (cadr fringes) (frame-char-width frame))
  519. (cond
  520. ((memq (nth 2 scroll-bars) '(left right))
  521. (nth 1 scroll-bars))
  522. ((memq (frame-parameter frame 'vertical-scroll-bars)
  523. '(left right))
  524. (ceiling (or (frame-parameter frame 'scroll-bar-width) 14)
  525. (frame-char-width)))
  526. (t 0)))
  527. (if (and (not (window--size-ignore window ignore))
  528. (numberp window-min-width))
  529. window-min-width
  530. 0))))
  531. (t
  532. ;; For the minimum height of a window take any mode- or
  533. ;; header-line into account.
  534. (max (+ window-safe-min-height
  535. (if header-line-format 1 0)
  536. (if mode-line-format 1 0))
  537. (if (and (not (window--size-ignore window ignore))
  538. (numberp window-min-height))
  539. window-min-height
  540. 0))))))))
  541. (defun window-sizable (window delta &optional horizontal ignore)
  542. "Return DELTA if DELTA lines can be added to WINDOW.
  543. Optional argument HORIZONTAL non-nil means return DELTA if DELTA
  544. columns can be added to WINDOW. A return value of zero means
  545. that no lines (or columns) can be added to WINDOW.
  546. This function looks only at WINDOW and, recursively, its child
  547. windows. The function `window-resizable' looks at other windows
  548. as well.
  549. DELTA positive means WINDOW shall be enlarged by DELTA lines or
  550. columns. If WINDOW cannot be enlarged by DELTA lines or columns
  551. return the maximum value in the range 0..DELTA by which WINDOW
  552. can be enlarged.
  553. DELTA negative means WINDOW shall be shrunk by -DELTA lines or
  554. columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
  555. return the minimum value in the range DELTA..0 by which WINDOW
  556. can be shrunk.
  557. Optional argument IGNORE non-nil means ignore restrictions
  558. imposed by fixed size windows, `window-min-height' or
  559. `window-min-width' settings. If IGNORE equals `safe', live
  560. windows may get as small as `window-safe-min-height' lines and
  561. `window-safe-min-width' columns. If IGNORE is a window, ignore
  562. restrictions for that window only. Any other non-nil value means
  563. ignore all of the above restrictions for all windows."
  564. (setq window (window-normalize-window window))
  565. (cond
  566. ((< delta 0)
  567. (max (- (window-min-size window horizontal ignore)
  568. (window-total-size window horizontal))
  569. delta))
  570. ((window--size-ignore window ignore)
  571. delta)
  572. ((> delta 0)
  573. (if (window-size-fixed-p window horizontal)
  574. 0
  575. delta))
  576. (t 0)))
  577. (defun window-sizable-p (window delta &optional horizontal ignore)
  578. "Return t if WINDOW can be resized by DELTA lines.
  579. For the meaning of the arguments of this function see the
  580. doc-string of `window-sizable'."
  581. (setq window (window-normalize-window window))
  582. (if (> delta 0)
  583. (>= (window-sizable window delta horizontal ignore) delta)
  584. (<= (window-sizable window delta horizontal ignore) delta)))
  585. (defun window--size-fixed-1 (window horizontal)
  586. "Internal function for `window-size-fixed-p'."
  587. (let ((sub (window-child window)))
  588. (catch 'fixed
  589. (if sub
  590. ;; WINDOW is an internal window.
  591. (if (window-combined-p sub horizontal)
  592. ;; An iso-combination is fixed size if all its child
  593. ;; windows are fixed-size.
  594. (progn
  595. (while sub
  596. (unless (window--size-fixed-1 sub horizontal)
  597. ;; We found a non-fixed-size child window, so
  598. ;; WINDOW's size is not fixed.
  599. (throw 'fixed nil))
  600. (setq sub (window-right sub)))
  601. ;; All child windows are fixed-size, so WINDOW's size is
  602. ;; fixed.
  603. (throw 'fixed t))
  604. ;; An ortho-combination is fixed-size if at least one of its
  605. ;; child windows is fixed-size.
  606. (while sub
  607. (when (window--size-fixed-1 sub horizontal)
  608. ;; We found a fixed-size child window, so WINDOW's size
  609. ;; is fixed.
  610. (throw 'fixed t))
  611. (setq sub (window-right sub))))
  612. ;; WINDOW is a live window.
  613. (with-current-buffer (window-buffer window)
  614. (if horizontal
  615. (memq window-size-fixed '(width t))
  616. (memq window-size-fixed '(height t))))))))
  617. (defun window-size-fixed-p (&optional window horizontal)
  618. "Return non-nil if WINDOW's height is fixed.
  619. WINDOW can be an arbitrary window and defaults to the selected
  620. window. Optional argument HORIZONTAL non-nil means return
  621. non-nil if WINDOW's width is fixed.
  622. If this function returns nil, this does not necessarily mean that
  623. WINDOW can be resized in the desired direction. The function
  624. `window-resizable' can tell that."
  625. (window--size-fixed-1
  626. (window-normalize-window window) horizontal))
  627. (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup)
  628. "Internal function for `window-min-delta'."
  629. (if (not (window-parent window))
  630. ;; If we can't go up, return zero.
  631. 0
  632. ;; Else try to find a non-fixed-size sibling of WINDOW.
  633. (let* ((parent (window-parent window))
  634. (sub (window-child parent)))
  635. (catch 'done
  636. (if (window-combined-p sub horizontal)
  637. ;; In an iso-combination throw DELTA if we find at least one
  638. ;; child window and that window is either not fixed-size or
  639. ;; we can ignore fixed-sizeness.
  640. (let ((skip (eq trail 'after)))
  641. (while sub
  642. (cond
  643. ((eq sub window)
  644. (setq skip (eq trail 'before)))
  645. (skip)
  646. ((and (not (window--size-ignore window ignore))
  647. (window-size-fixed-p sub horizontal)))
  648. (t
  649. ;; We found a non-fixed-size child window.
  650. (throw 'done delta)))
  651. (setq sub (window-right sub))))
  652. ;; In an ortho-combination set DELTA to the minimum value by
  653. ;; which other child windows can shrink.
  654. (while sub
  655. (unless (eq sub window)
  656. (setq delta
  657. (min delta
  658. (- (window-total-size sub horizontal)
  659. (window-min-size sub horizontal ignore)))))
  660. (setq sub (window-right sub))))
  661. (if noup
  662. delta
  663. (window--min-delta-1 parent delta horizontal ignore trail))))))
  664. (defun window-min-delta (&optional window horizontal ignore trail noup nodown)
  665. "Return number of lines by which WINDOW can be shrunk.
  666. WINDOW can be an arbitrary window and defaults to the selected
  667. window. Return zero if WINDOW cannot be shrunk.
  668. Optional argument HORIZONTAL non-nil means return number of
  669. columns by which WINDOW can be shrunk.
  670. Optional argument IGNORE non-nil means ignore restrictions
  671. imposed by fixed size windows, `window-min-height' or
  672. `window-min-width' settings. If IGNORE is a window, ignore
  673. restrictions for that window only. If IGNORE equals `safe',
  674. live windows may get as small as `window-safe-min-height' lines
  675. and `window-safe-min-width' columns. Any other non-nil value
  676. means ignore all of the above restrictions for all windows.
  677. Optional argument TRAIL restricts the windows that can be enlarged.
  678. If its value is `before', only windows to the left of or above WINDOW
  679. can be enlarged. If it is `after', only windows to the right of or
  680. below WINDOW can be enlarged.
  681. Optional argument NOUP non-nil means don't go up in the window
  682. tree, but try to enlarge windows within WINDOW's combination only.
  683. Optional argument NODOWN non-nil means don't check whether WINDOW
  684. itself (and its child windows) can be shrunk; check only whether
  685. at least one other window can be enlarged appropriately."
  686. (setq window (window-normalize-window window))
  687. (let ((size (window-total-size window horizontal))
  688. (minimum (window-min-size window horizontal ignore)))
  689. (cond
  690. (nodown
  691. ;; If NODOWN is t, try to recover the entire size of WINDOW.
  692. (window--min-delta-1 window size horizontal ignore trail noup))
  693. ((= size minimum)
  694. ;; If NODOWN is nil and WINDOW's size is already at its minimum,
  695. ;; there's nothing to recover.
  696. 0)
  697. (t
  698. ;; Otherwise, try to recover whatever WINDOW is larger than its
  699. ;; minimum size.
  700. (window--min-delta-1
  701. window (- size minimum) horizontal ignore trail noup)))))
  702. (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup)
  703. "Internal function of `window-max-delta'."
  704. (if (not (window-parent window))
  705. ;; Can't go up. Return DELTA.
  706. delta
  707. (let* ((parent (window-parent window))
  708. (sub (window-child parent)))
  709. (catch 'fixed
  710. (if (window-combined-p sub horizontal)
  711. ;; For an iso-combination calculate how much we can get from
  712. ;; other child windows.
  713. (let ((skip (eq trail 'after)))
  714. (while sub
  715. (cond
  716. ((eq sub window)
  717. (setq skip (eq trail 'before)))
  718. (skip)
  719. (t
  720. (setq delta
  721. (+ delta
  722. (- (window-total-size sub horizontal)
  723. (window-min-size sub horizontal ignore))))))
  724. (setq sub (window-right sub))))
  725. ;; For an ortho-combination throw DELTA when at least one
  726. ;; child window is fixed-size.
  727. (while sub
  728. (when (and (not (eq sub window))
  729. (not (window--size-ignore sub ignore))
  730. (window-size-fixed-p sub horizontal))
  731. (throw 'fixed delta))
  732. (setq sub (window-right sub))))
  733. (if noup
  734. ;; When NOUP is nil, DELTA is all we can get.
  735. delta
  736. ;; Else try with parent of WINDOW, passing the DELTA we
  737. ;; recovered so far.
  738. (window--max-delta-1 parent delta horizontal ignore trail))))))
  739. (defun window-max-delta (&optional window horizontal ignore trail noup nodown)
  740. "Return maximum number of lines by which WINDOW can be enlarged.
  741. WINDOW can be an arbitrary window and defaults to the selected
  742. window. The return value is zero if WINDOW cannot be enlarged.
  743. Optional argument HORIZONTAL non-nil means return maximum number
  744. of columns by which WINDOW can be enlarged.
  745. Optional argument IGNORE non-nil means ignore restrictions
  746. imposed by fixed size windows, `window-min-height' or
  747. `window-min-width' settings. If IGNORE is a window, ignore
  748. restrictions for that window only. If IGNORE equals `safe',
  749. live windows may get as small as `window-safe-min-height' lines
  750. and `window-safe-min-width' columns. Any other non-nil value means
  751. ignore all of the above restrictions for all windows.
  752. Optional argument TRAIL restricts the windows that can be enlarged.
  753. If its value is `before', only windows to the left of or above WINDOW
  754. can be enlarged. If it is `after', only windows to the right of or
  755. below WINDOW can be enlarged.
  756. Optional argument NOUP non-nil means don't go up in the window
  757. tree but try to obtain the entire space from windows within
  758. WINDOW's combination.
  759. Optional argument NODOWN non-nil means do not check whether
  760. WINDOW itself (and its child windows) can be enlarged; check
  761. only whether other windows can be shrunk appropriately."
  762. (setq window (window-normalize-window window))
  763. (if (and (not (window--size-ignore window ignore))
  764. (not nodown) (window-size-fixed-p window horizontal))
  765. ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
  766. ;; size.
  767. 0
  768. ;; WINDOW has no fixed size.
  769. (window--max-delta-1 window 0 horizontal ignore trail noup)))
  770. ;; Make NOUP also inhibit the min-size check.
  771. (defun window--resizable (window delta &optional horizontal ignore trail noup nodown)
  772. "Return DELTA if WINDOW can be resized vertically by DELTA lines.
  773. Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
  774. can be resized horizontally by DELTA columns. A return value of
  775. zero means that WINDOW is not resizable.
  776. DELTA positive means WINDOW shall be enlarged by DELTA lines or
  777. columns. If WINDOW cannot be enlarged by DELTA lines or columns,
  778. return the maximum value in the range 0..DELTA by which WINDOW
  779. can be enlarged.
  780. DELTA negative means WINDOW shall be shrunk by -DELTA lines or
  781. columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
  782. return the minimum value in the range DELTA..0 that can be used
  783. for shrinking WINDOW.
  784. Optional argument IGNORE non-nil means ignore restrictions
  785. imposed by fixed size windows, `window-min-height' or
  786. `window-min-width' settings. If IGNORE is a window, ignore
  787. restrictions for that window only. If IGNORE equals `safe',
  788. live windows may get as small as `window-safe-min-height' lines
  789. and `window-safe-min-width' columns. Any other non-nil value
  790. means ignore all of the above restrictions for all windows.
  791. Optional argument TRAIL `before' means only windows to the left
  792. of or below WINDOW can be shrunk. Optional argument TRAIL
  793. `after' means only windows to the right of or above WINDOW can be
  794. shrunk.
  795. Optional argument NOUP non-nil means don't go up in the window
  796. tree but check only whether space can be obtained from (or given
  797. to) WINDOW's siblings.
  798. Optional argument NODOWN non-nil means don't go down in the
  799. window tree. This means do not check whether resizing would
  800. violate size restrictions of WINDOW or its child windows."
  801. (setq window (window-normalize-window window))
  802. (cond
  803. ((< delta 0)
  804. (max (- (window-min-delta window horizontal ignore trail noup nodown))
  805. delta))
  806. ((> delta 0)
  807. (min (window-max-delta window horizontal ignore trail noup nodown)
  808. delta))
  809. (t 0)))
  810. (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown)
  811. "Return t if WINDOW can be resized vertically by DELTA lines.
  812. For the meaning of the arguments of this function see the
  813. doc-string of `window--resizable'."
  814. (setq window (window-normalize-window window))
  815. (if (> delta 0)
  816. (>= (window--resizable window delta horizontal ignore trail noup nodown)
  817. delta)
  818. (<= (window--resizable window delta horizontal ignore trail noup nodown)
  819. delta)))
  820. (defun window-resizable (window delta &optional horizontal ignore)
  821. "Return DELTA if WINDOW can be resized vertically by DELTA lines.
  822. Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
  823. can be resized horizontally by DELTA columns. A return value of
  824. zero means that WINDOW is not resizable.
  825. DELTA positive means WINDOW shall be enlarged by DELTA lines or
  826. columns. If WINDOW cannot be enlarged by DELTA lines or columns
  827. return the maximum value in the range 0..DELTA by which WINDOW
  828. can be enlarged.
  829. DELTA negative means WINDOW shall be shrunk by -DELTA lines or
  830. columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
  831. return the minimum value in the range DELTA..0 that can be used
  832. for shrinking WINDOW.
  833. Optional argument IGNORE non-nil means ignore restrictions
  834. imposed by fixed size windows, `window-min-height' or
  835. `window-min-width' settings. If IGNORE is a window, ignore
  836. restrictions for that window only. If IGNORE equals `safe',
  837. live windows may get as small as `window-safe-min-height' lines
  838. and `window-safe-min-width' columns. Any other non-nil value
  839. means ignore all of the above restrictions for all windows."
  840. (setq window (window-normalize-window window))
  841. (window--resizable window delta horizontal ignore))
  842. (defun window-total-size (&optional window horizontal)
  843. "Return the total height or width of WINDOW.
  844. If WINDOW is omitted or nil, it defaults to the selected window.
  845. If HORIZONTAL is omitted or nil, return the total height of
  846. WINDOW, in lines, like `window-total-height'. Otherwise return
  847. the total width, in columns, like `window-total-width'."
  848. (if horizontal
  849. (window-total-width window)
  850. (window-total-height window)))
  851. ;; Eventually we should make `window-height' obsolete.
  852. (defalias 'window-height 'window-total-height)
  853. ;; See discussion in bug#4543.
  854. (defun window-full-height-p (&optional window)
  855. "Return t if WINDOW is as high as its containing frame.
  856. More precisely, return t if and only if the total height of
  857. WINDOW equals the total height of the root window of WINDOW's
  858. frame. WINDOW can be any window and defaults to the selected
  859. one."
  860. (setq window (window-normalize-window window))
  861. (= (window-total-size window)
  862. (window-total-size (frame-root-window window))))
  863. (defun window-full-width-p (&optional window)
  864. "Return t if WINDOW is as wide as its containing frame.
  865. More precisely, return t if and only if the total width of WINDOW
  866. equals the total width of the root window of WINDOW's frame.
  867. WINDOW can be any window and defaults to the selected one."
  868. (setq window (window-normalize-window window))
  869. (= (window-total-size window t)
  870. (window-total-size (frame-root-window window) t)))
  871. (defun window-body-size (&optional window horizontal)
  872. "Return the height or width of WINDOW's text area.
  873. If WINDOW is omitted or nil, it defaults to the selected window.
  874. Signal an error if the window is not live.
  875. If HORIZONTAL is omitted or nil, return the height of the text
  876. area, like `window-body-height'. Otherwise, return the width of
  877. the text area, like `window-body-width'."
  878. (if horizontal
  879. (window-body-width window)
  880. (window-body-height window)))
  881. ;; Eventually we should make `window-height' obsolete.
  882. (defalias 'window-width 'window-body-width)
  883. (defun window-current-scroll-bars (&optional window)
  884. "Return the current scroll bar settings for WINDOW.
  885. WINDOW must be a live window and defaults to the selected one.
  886. The return value is a cons cell (VERTICAL . HORIZONTAL) where
  887. VERTICAL specifies the current location of the vertical scroll
  888. bars (`left', `right', or nil), and HORIZONTAL specifies the
  889. current location of the horizontal scroll bars (`top', `bottom',
  890. or nil).
  891. Unlike `window-scroll-bars', this function reports the scroll bar
  892. type actually used, once frame defaults and `scroll-bar-mode' are
  893. taken into account."
  894. (setq window (window-normalize-window window t))
  895. (let ((vert (nth 2 (window-scroll-bars window)))
  896. (hor nil))
  897. (when (or (eq vert t) (eq hor t))
  898. (let ((fcsb (frame-current-scroll-bars (window-frame window))))
  899. (if (eq vert t)
  900. (setq vert (car fcsb)))
  901. (if (eq hor t)
  902. (setq hor (cdr fcsb)))))
  903. (cons vert hor)))
  904. (defun walk-windows (fun &optional minibuf all-frames)
  905. "Cycle through all live windows, calling FUN for each one.
  906. FUN must specify a function with a window as its sole argument.
  907. The optional arguments MINIBUF and ALL-FRAMES specify the set of
  908. windows to include in the walk.
  909. MINIBUF t means include the minibuffer window even if the
  910. minibuffer is not active. MINIBUF nil or omitted means include
  911. the minibuffer window only if the minibuffer is active. Any
  912. other value means do not include the minibuffer window even if
  913. the minibuffer is active.
  914. ALL-FRAMES nil or omitted means consider all windows on the
  915. selected frame, plus the minibuffer window if specified by the
  916. MINIBUF argument. If the minibuffer counts, consider all windows
  917. on all frames that share that minibuffer too. The following
  918. non-nil values of ALL-FRAMES have special meanings:
  919. - t means consider all windows on all existing frames.
  920. - `visible' means consider all windows on all visible frames on
  921. the current terminal.
  922. - 0 (the number zero) means consider all windows on all visible
  923. and iconified frames on the current terminal.
  924. - A frame means consider all windows on that frame only.
  925. Anything else means consider all windows on the selected frame
  926. and no others.
  927. This function changes neither the order of recently selected
  928. windows nor the buffer list."
  929. ;; If we start from the minibuffer window, don't fail to come
  930. ;; back to it.
  931. (when (window-minibuffer-p (selected-window))
  932. (setq minibuf t))
  933. ;; Make sure to not mess up the order of recently selected
  934. ;; windows. Use `save-selected-window' and `select-window'
  935. ;; with second argument non-nil for this purpose.
  936. (save-selected-window
  937. (when (framep all-frames)
  938. (select-window (frame-first-window all-frames) 'norecord))
  939. (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
  940. (funcall fun walk-windows-window))))
  941. (defun window-point-1 (&optional window)
  942. "Return value of WINDOW's point.
  943. WINDOW can be any live window and defaults to the selected one.
  944. This function is like `window-point' with one exception: If
  945. WINDOW is selected, it returns the value of `point' of WINDOW's
  946. buffer regardless of whether that buffer is current or not."
  947. (setq window (window-normalize-window window t))
  948. (if (eq window (selected-window))
  949. (with-current-buffer (window-buffer window)
  950. (point))
  951. (window-point window)))
  952. (defun set-window-point-1 (window pos)
  953. "Set value of WINDOW's point to POS.
  954. WINDOW can be any live window and defaults to the selected one.
  955. This function is like `set-window-point' with one exception: If
  956. WINDOW is selected, it moves `point' of WINDOW's buffer to POS
  957. regardless of whether that buffer is current or not."
  958. (setq window (window-normalize-window window t))
  959. (if (eq window (selected-window))
  960. (with-current-buffer (window-buffer window)
  961. (goto-char pos))
  962. (set-window-point window pos)))
  963. (defun window-at-side-p (&optional window side)
  964. "Return t if WINDOW is at SIDE of its containing frame.
  965. WINDOW can be any window and defaults to the selected one. SIDE
  966. can be any of the symbols `left', `top', `right' or `bottom'.
  967. The default value nil is handled like `bottom'."
  968. (setq window (window-normalize-window window))
  969. (let ((edge
  970. (cond
  971. ((eq side 'left) 0)
  972. ((eq side 'top) 1)
  973. ((eq side 'right) 2)
  974. ((memq side '(bottom nil)) 3))))
  975. (= (nth edge (window-edges window))
  976. (nth edge (window-edges (frame-root-window window))))))
  977. (defun window-at-side-list (&optional frame side)
  978. "Return list of all windows on SIDE of FRAME.
  979. FRAME must be a live frame and defaults to the selected frame.
  980. SIDE can be any of the symbols `left', `top', `right' or
  981. `bottom'. The default value nil is handled like `bottom'."
  982. (setq frame (window-normalize-frame frame))
  983. (let (windows)
  984. (walk-window-tree
  985. (lambda (window)
  986. (when (window-at-side-p window side)
  987. (setq windows (cons window windows))))
  988. frame)
  989. (nreverse windows)))
  990. (defun window--in-direction-2 (window posn &optional horizontal)
  991. "Support function for `window-in-direction'."
  992. (if horizontal
  993. (let ((top (window-top-line window)))
  994. (if (> top posn)
  995. (- top posn)
  996. (- posn top (window-total-height window))))
  997. (let ((left (window-left-column window)))
  998. (if (> left posn)
  999. (- left posn)
  1000. (- posn left (window-total-width window))))))
  1001. (defun window-in-direction (direction &optional window ignore)
  1002. "Return window in DIRECTION as seen from WINDOW.
  1003. DIRECTION must be one of `above', `below', `left' or `right'.
  1004. WINDOW must be a live window and defaults to the selected one.
  1005. IGNORE non-nil means a window can be returned even if its
  1006. `no-other-window' parameter is non-nil."
  1007. (setq window (window-normalize-window window t))
  1008. (unless (memq direction '(above below left right))
  1009. (error "Wrong direction %s" direction))
  1010. (let* ((frame (window-frame window))
  1011. (hor (memq direction '(left right)))
  1012. (first (if hor
  1013. (window-left-column window)
  1014. (window-top-line window)))
  1015. (last (+ first (if hor
  1016. (window-total-width window)
  1017. (window-total-height window))))
  1018. (posn-cons (nth 6 (posn-at-point (window-point-1 window) window)))
  1019. ;; The column / row value of `posn-at-point' can be nil for the
  1020. ;; mini-window, guard against that.
  1021. (posn (if hor
  1022. (+ (or (cdr posn-cons) 1) (window-top-line window))
  1023. (+ (or (car posn-cons) 1) (window-left-column window))))
  1024. (best-edge
  1025. (cond
  1026. ((eq direction 'below) (frame-height frame))
  1027. ((eq direction 'right) (frame-width frame))
  1028. (t -1)))
  1029. (best-edge-2 best-edge)
  1030. (best-diff-2 (if hor (frame-height frame) (frame-width frame)))
  1031. best best-2 best-diff-2-new)
  1032. (walk-window-tree
  1033. (lambda (w)
  1034. (let* ((w-top (window-top-line w))
  1035. (w-left (window-left-column w)))
  1036. (cond
  1037. ((or (eq window w)
  1038. ;; Ignore ourselves.
  1039. (and (window-parameter w 'no-other-window)
  1040. ;; Ignore W unless IGNORE is non-nil.
  1041. (not ignore))))
  1042. (hor
  1043. (cond
  1044. ((and (<= w-top posn)
  1045. (< posn (+ w-top (window-total-height w))))
  1046. ;; W is to the left or right of WINDOW and covers POSN.
  1047. (when (or (and (eq direction 'left)
  1048. (<= w-left first) (> w-left best-edge))
  1049. (and (eq direction 'right)
  1050. (>= w-left last) (< w-left best-edge)))
  1051. (setq best-edge w-left)
  1052. (setq best w)))
  1053. ((and (or (and (eq direction 'left)
  1054. (<= (+ w-left (window-total-width w)) first))
  1055. (and (eq direction 'right) (<= last w-left)))
  1056. ;; W is to the left or right of WINDOW but does not
  1057. ;; cover POSN.
  1058. (setq best-diff-2-new
  1059. (window--in-direction-2 w posn hor))
  1060. (or (< best-diff-2-new best-diff-2)
  1061. (and (= best-diff-2-new best-diff-2)
  1062. (if (eq direction 'left)
  1063. (> w-left best-edge-2)
  1064. (< w-left best-edge-2)))))
  1065. (setq best-edge-2 w-left)
  1066. (setq best-diff-2 best-diff-2-new)
  1067. (setq best-2 w))))
  1068. (t
  1069. (cond
  1070. ((and (<= w-left posn)
  1071. (< posn (+ w-left (window-total-width w))))
  1072. ;; W is above or below WINDOW and covers POSN.
  1073. (when (or (and (eq direction 'above)
  1074. (<= w-top first) (> w-top best-edge))
  1075. (and (eq direction 'below)
  1076. (>= w-top first) (< w-top best-edge)))
  1077. (setq best-edge w-top)
  1078. (setq best w)))
  1079. ((and (or (and (eq direction 'above)
  1080. (<= (+ w-top (window-total-height w)) first))
  1081. (and (eq direction 'below) (<= last w-top)))
  1082. ;; W is above or below WINDOW but does not cover POSN.
  1083. (setq best-diff-2-new
  1084. (window--in-direction-2 w posn hor))
  1085. (or (< best-diff-2-new best-diff-2)
  1086. (and (= best-diff-2-new best-diff-2)
  1087. (if (eq direction 'above)
  1088. (> w-top best-edge-2)
  1089. (< w-top best-edge-2)))))
  1090. (setq best-edge-2 w-top)
  1091. (setq best-diff-2 best-diff-2-new)
  1092. (setq best-2 w)))))))
  1093. (window-frame window))
  1094. (or best best-2)))
  1095. (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
  1096. "Return a live window satisfying PREDICATE.
  1097. More precisely, cycle through all windows calling the function
  1098. PREDICATE on each one of them with the window as its sole
  1099. argument. Return the first window for which PREDICATE returns
  1100. non-nil. Windows are scanned starting with the window following
  1101. the selected window. If no window satisfies PREDICATE, return
  1102. DEFAULT.
  1103. MINIBUF t means include the minibuffer window even if the
  1104. minibuffer is not active. MINIBUF nil or omitted means include
  1105. the minibuffer window only if the minibuffer is active. Any
  1106. other value means do not include the minibuffer window even if
  1107. the minibuffer is active.
  1108. ALL-FRAMES nil or omitted means consider all windows on the selected
  1109. frame, plus the minibuffer window if specified by the MINIBUF
  1110. argument. If the minibuffer counts, consider all windows on all
  1111. frames that share that minibuffer too. The following non-nil
  1112. values of ALL-FRAMES have special meanings:
  1113. - t means consider all windows on all existing frames.
  1114. - `visible' means consider all windows on all visible frames on
  1115. the current terminal.
  1116. - 0 (the number zero) means consider all windows on all visible
  1117. and iconified frames on the current terminal.
  1118. - A frame means consider all windows on that frame only.
  1119. Anything else means consider all windows on the selected frame
  1120. and no others."
  1121. (catch 'found
  1122. (dolist (window (window-list-1
  1123. (next-window nil minibuf all-frames)
  1124. minibuf all-frames))
  1125. (when (funcall predicate window)
  1126. (throw 'found window)))
  1127. default))
  1128. (defalias 'some-window 'get-window-with-predicate)
  1129. (defun get-lru-window (&optional all-frames dedicated)
  1130. "Return the least recently used window on frames specified by ALL-FRAMES.
  1131. Return a full-width window if possible. A minibuffer window is
  1132. never a candidate. A dedicated window is never a candidate
  1133. unless DEDICATED is non-nil, so if all windows are dedicated, the
  1134. value is nil. Avoid returning the selected window if possible.
  1135. The following non-nil values of the optional argument ALL-FRAMES
  1136. have special meanings:
  1137. - t means consider all windows on all existing frames.
  1138. - `visible' means consider all windows on all visible frames on
  1139. the current terminal.
  1140. - 0 (the number zero) means consider all windows on all visible
  1141. and iconified frames on the current terminal.
  1142. - A frame means consider all windows on that frame only.
  1143. Any other value of ALL-FRAMES means consider all windows on the
  1144. selected frame and no others."
  1145. (let (best-window best-time second-best-window second-best-time time)
  1146. (dolist (window (window-list-1 nil 'nomini all-frames))
  1147. (when (or dedicated (not (window-dedicated-p window)))
  1148. (setq time (window-use-time window))
  1149. (if (or (eq window (selected-window))
  1150. (not (window-full-width-p window)))
  1151. (when (or (not second-best-time) (< time second-best-time))
  1152. (setq second-best-time time)
  1153. (setq second-best-window window))
  1154. (when (or (not best-time) (< time best-time))
  1155. (setq best-time time)
  1156. (setq best-window window)))))
  1157. (or best-window second-best-window)))
  1158. (defun get-mru-window (&optional all-frames)
  1159. "Return the most recently used window on frames specified by ALL-FRAMES.
  1160. Do not return a minibuffer window.
  1161. The following non-nil values of the optional argument ALL-FRAMES
  1162. have special meanings:
  1163. - t means consider all windows on all existing frames.
  1164. - `visible' means consider all windows on all visible frames on
  1165. the current terminal.
  1166. - 0 (the number zero) means consider all windows on all visible
  1167. and iconified frames on the current terminal.
  1168. - A frame means consider all windows on that frame only.
  1169. Any other value of ALL-FRAMES means consider all windows on the
  1170. selected frame and no others."
  1171. (let (best-window best-time time)
  1172. (dolist (window (window-list-1 nil 'nomini all-frames))
  1173. (setq time (window-use-time window))
  1174. (when (or (not best-time) (> time best-time))
  1175. (setq best-time time)
  1176. (setq best-window window)))
  1177. best-window))
  1178. (defun get-largest-window (&optional all-frames dedicated)
  1179. "Return the largest window on frames specified by ALL-FRAMES.
  1180. A minibuffer window is never a candidate. A dedicated window is
  1181. never a candidate unless DEDICATED is non-nil, so if all windows
  1182. are dedicated, the value is nil.
  1183. The following non-nil values of the optional argument ALL-FRAMES
  1184. have special meanings:
  1185. - t means consider all windows on all existing frames.
  1186. - `visible' means consider all windows on all visible frames on
  1187. the current terminal.
  1188. - 0 (the number zero) means consider all windows on all visible
  1189. and iconified frames on the current terminal.
  1190. - A frame means consider all windows on that frame only.
  1191. Any other value of ALL-FRAMES means consider all windows on the
  1192. selected frame and no others."
  1193. (let ((best-size 0)
  1194. best-window size)
  1195. (dolist (window (window-list-1 nil 'nomini all-frames))
  1196. (when (or dedicated (not (window-dedicated-p window)))
  1197. (setq size (* (window-total-size window)
  1198. (window-total-size window t)))
  1199. (when (> size best-size)
  1200. (setq best-size size)
  1201. (setq best-window window))))
  1202. best-window))
  1203. (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
  1204. "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
  1205. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  1206. and defaults to the current buffer. Windows are scanned starting
  1207. with the selected window.
  1208. MINIBUF t means include the minibuffer window even if the
  1209. minibuffer is not active. MINIBUF nil or omitted means include
  1210. the minibuffer window only if the minibuffer is active. Any
  1211. other value means do not include the minibuffer window even if
  1212. the minibuffer is active.
  1213. ALL-FRAMES nil or omitted means consider all windows on the
  1214. selected frame, plus the minibuffer window if specified by the
  1215. MINIBUF argument. If the minibuffer counts, consider all windows
  1216. on all frames that share that minibuffer too. The following
  1217. non-nil values of ALL-FRAMES have special meanings:
  1218. - t means consider all windows on all existing frames.
  1219. - `visible' means consider all windows on all visible frames on
  1220. the current terminal.
  1221. - 0 (the number zero) means consider all windows on all visible
  1222. and iconified frames on the current terminal.
  1223. - A frame means consider all windows on that frame only.
  1224. Anything else means consider all windows on the selected frame
  1225. and no others."
  1226. (let ((buffer (window-normalize-buffer buffer-or-name))
  1227. windows)
  1228. (dolist (window (window-list-1 (selected-window) minibuf all-frames))
  1229. (when (eq (window-buffer window) buffer)
  1230. (setq windows (cons window windows))))
  1231. (nreverse windows)))
  1232. (defun minibuffer-window-active-p (window)
  1233. "Return t if WINDOW is the currently active minibuffer window."
  1234. (eq window (active-minibuffer-window)))
  1235. (defun count-windows (&optional minibuf)
  1236. "Return the number of live windows on the selected frame.
  1237. The optional argument MINIBUF specifies whether the minibuffer
  1238. window shall be counted. See `walk-windows' for the precise
  1239. meaning of this argument."
  1240. (length (window-list-1 nil minibuf)))
  1241. ;;; Resizing windows.
  1242. (defun window--resize-reset (&optional frame horizontal)
  1243. "Reset resize values for all windows on FRAME.
  1244. FRAME defaults to the selected frame.
  1245. This function stores the current value of `window-total-size' applied
  1246. with argument HORIZONTAL in the new total size of all windows on
  1247. FRAME. It also resets the new normal size of each of these
  1248. windows."
  1249. (window--resize-reset-1
  1250. (frame-root-window (window-normalize-frame frame)) horizontal))
  1251. (defun window--resize-reset-1 (window horizontal)
  1252. "Internal function of `window--resize-reset'."
  1253. ;; Register old size in the new total size.
  1254. (set-window-new-total window (window-total-size window horizontal))
  1255. ;; Reset new normal size.
  1256. (set-window-new-normal window)
  1257. (when (window-child window)
  1258. (window--resize-reset-1 (window-child window) horizontal))
  1259. (when (window-right window)
  1260. (window--resize-reset-1 (window-right window) horizontal)))
  1261. ;; The following routine is used to manually resize the minibuffer
  1262. ;; window and is currently used, for example, by ispell.el.
  1263. (defun window--resize-mini-window (window delta)
  1264. "Resize minibuffer window WINDOW by DELTA lines.
  1265. If WINDOW cannot be resized by DELTA lines make it as large (or
  1266. as small) as possible, but don't signal an error."
  1267. (when (window-minibuffer-p window)
  1268. (let* ((frame (window-frame window))
  1269. (root (frame-root-window frame))
  1270. (height (window-total-size window))
  1271. (min-delta
  1272. (- (window-total-size root)
  1273. (window-min-size root))))
  1274. ;; Sanitize DELTA.
  1275. (cond
  1276. ((<= (+ height delta) 0)
  1277. (setq delta (- (- height 1))))
  1278. ((> delta min-delta)
  1279. (setq delta min-delta)))
  1280. ;; Resize now.
  1281. (window--resize-reset frame)
  1282. ;; Ideally we should be able to resize just the last child of root
  1283. ;; here. See the comment in `resize-root-window-vertically' for
  1284. ;; why we do not do that.
  1285. (window--resize-this-window root (- delta) nil nil t)
  1286. (set-window-new-total window (+ height delta))
  1287. ;; The following routine catches the case where we want to resize
  1288. ;; a minibuffer-only frame.
  1289. (resize-mini-window-internal window))))
  1290. (defun window-resize (window delta &optional horizontal ignore)
  1291. "Resize WINDOW vertically by DELTA lines.
  1292. WINDOW can be an arbitrary window and defaults to the selected
  1293. one. An attempt to resize the root window of a frame will raise
  1294. an error though.
  1295. DELTA a positive number means WINDOW shall be enlarged by DELTA
  1296. lines. DELTA negative means WINDOW shall be shrunk by -DELTA
  1297. lines.
  1298. Optional argument HORIZONTAL non-nil means resize WINDOW
  1299. horizontally by DELTA columns. In this case a positive DELTA
  1300. means enlarge WINDOW by DELTA columns. DELTA negative means
  1301. WINDOW shall be shrunk by -DELTA columns.
  1302. Optional argument IGNORE non-nil means ignore restrictions
  1303. imposed by fixed size windows, `window-min-height' or
  1304. `window-min-width' settings. If IGNORE is a window, ignore
  1305. restrictions for that window only. If IGNORE equals `safe',
  1306. live windows may get as small as `window-safe-min-height' lines
  1307. and `window-safe-min-width' columns. Any other non-nil value
  1308. means ignore all of the above restrictions for all windows.
  1309. This function resizes other windows proportionally and never
  1310. deletes any windows. If you want to move only the low (right)
  1311. edge of WINDOW consider using `adjust-window-trailing-edge'
  1312. instead."
  1313. (setq window (window-normalize-window window))
  1314. (let* ((frame (window-frame window))
  1315. (minibuffer-window (minibuffer-window frame))
  1316. sibling)
  1317. (cond
  1318. ((eq window (frame-root-window frame))
  1319. (error "Cannot resize the root window of a frame"))
  1320. ((window-minibuffer-p window)
  1321. (if horizontal
  1322. (error "Cannot resize minibuffer window horizontally")
  1323. (window--resize-mini-window window delta)))
  1324. ((and (not horizontal)
  1325. (window-full-height-p window)
  1326. (eq (window-frame minibuffer-window) frame)
  1327. (or (not resize-mini-windows)
  1328. (eq minibuffer-window (active-minibuffer-window))))
  1329. ;; If WINDOW is full height and either `resize-mini-windows' is
  1330. ;; nil or the minibuffer window is active, resize the minibuffer
  1331. ;; window.
  1332. (window--resize-mini-window minibuffer-window (- delta)))
  1333. ((window--resizable-p window delta horizontal ignore)
  1334. (window--resize-reset frame horizontal)
  1335. (window--resize-this-window window delta horizontal ignore t)
  1336. (if (and (not window-combination-resize)
  1337. (window-combined-p window horizontal)
  1338. (setq sibling (or (window-right window) (window-left window)))
  1339. (window-sizable-p sibling (- delta) horizontal ignore))
  1340. ;; If window-combination-resize is nil, WINDOW is part of an
  1341. ;; iso-combination, and WINDOW's neighboring right or left
  1342. ;; sibling can be resized as requested, resize that sibling.
  1343. (let ((normal-delta
  1344. (/ (float delta)
  1345. (window-total-size (window-parent window) horizontal))))
  1346. (window--resize-this-window sibling (- delta) horizontal nil t)
  1347. (set-window-new-normal
  1348. window (+ (window-normal-size window horizontal)
  1349. normal-delta))
  1350. (set-window-new-normal
  1351. sibling (- (window-normal-size sibling horizontal)
  1352. normal-delta)))
  1353. ;; Otherwise, resize all other windows in the same combination.
  1354. (window--resize-siblings window delta horizontal ignore))
  1355. (window-resize-apply frame horizontal))
  1356. (t
  1357. (error "Cannot resize window %s" window)))))
  1358. (defun window--resize-child-windows-skip-p (window)
  1359. "Return non-nil if WINDOW shall be skipped by resizing routines."
  1360. (memq (window-new-normal window) '(ignore stuck skip)))
  1361. (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
  1362. "Recursively set new normal height of child windows of window PARENT.
  1363. HORIZONTAL non-nil means set the new normal width of these
  1364. windows. WINDOW specifies a child window of PARENT that has been
  1365. resized by THIS-DELTA lines (columns).
  1366. Optional argument TRAIL either `before' or `after' means set values
  1367. only for windows before or after WINDOW. Optional argument
  1368. OTHER-DELTA, a number, specifies that this many lines (columns)
  1369. have been obtained from (or returned to) an ancestor window of
  1370. PARENT in order to resize WINDOW."
  1371. (let* ((delta-normal
  1372. (if (and (= (- this-delta) (window-total-size window horizontal))
  1373. (zerop other-delta))
  1374. ;; When WINDOW gets deleted and we can return its entire
  1375. ;; space to its siblings, use WINDOW's normal size as the
  1376. ;; normal delta.
  1377. (- (window-normal-size window horizontal))
  1378. ;; In any other case calculate the normal delta from the
  1379. ;; relation of THIS-DELTA to the total size of PARENT.
  1380. (/ (float this-delta) (window-total-size parent horizontal))))
  1381. (sub (window-child parent))
  1382. (parent-normal 0.0)
  1383. (skip (eq trail 'after)))
  1384. ;; Set parent-normal to the sum of the normal sizes of all child
  1385. ;; windows of PARENT that shall be resized, excluding only WINDOW
  1386. ;; and any windows specified by the optional TRAIL argument.
  1387. (while sub
  1388. (cond
  1389. ((eq sub window)
  1390. (setq skip (eq trail 'before)))
  1391. (skip)
  1392. (t
  1393. (setq parent-normal
  1394. (+ parent-normal (window-normal-size sub horizontal)))))
  1395. (setq sub (window-right sub)))
  1396. ;; Set the new normal size of all child windows of PARENT from what
  1397. ;; they should have contributed for recovering THIS-DELTA lines
  1398. ;; (columns).
  1399. (setq sub (window-child parent))
  1400. (setq skip (eq trail 'after))
  1401. (while sub
  1402. (cond
  1403. ((eq sub window)
  1404. (setq skip (eq trail 'before)))
  1405. (skip)
  1406. (t
  1407. (let ((old-normal (window-normal-size sub horizontal)))
  1408. (set-window-new-normal
  1409. sub (min 1.0 ; Don't get larger than 1.
  1410. (max (- old-normal
  1411. (* (/ old-normal parent-normal)
  1412. delta-normal))
  1413. ;; Don't drop below 0.
  1414. 0.0))))))
  1415. (setq sub (window-right sub)))
  1416. (when (numberp other-delta)
  1417. ;; Set the new normal size of windows from what they should have
  1418. ;; contributed for recovering OTHER-DELTA lines (columns).
  1419. (setq delta-normal (/ (float (window-total-size parent horizontal))
  1420. (+ (window-total-size parent horizontal)
  1421. other-delta)))
  1422. (setq sub (window-child parent))
  1423. (setq skip (eq trail 'after))
  1424. (while sub
  1425. (cond
  1426. ((eq sub window)
  1427. (setq skip (eq trail 'before)))
  1428. (skip)
  1429. (t
  1430. (set-window-new-normal
  1431. sub (min 1.0 ; Don't get larger than 1.
  1432. (max (* (window-new-normal sub) delta-normal)
  1433. ;; Don't drop below 0.
  1434. 0.0)))))
  1435. (setq sub (window-right sub))))
  1436. ;; Set the new normal size of WINDOW to what is left by the sum of
  1437. ;; the normal sizes of its siblings.
  1438. (set-window-new-normal
  1439. window
  1440. (let ((sum 0))
  1441. (setq sub (window-child parent))
  1442. (while sub
  1443. (cond
  1444. ((eq sub window))
  1445. ((not (numberp (window-new-normal sub)))
  1446. (setq sum (+ sum (window-normal-size sub horizontal))))
  1447. (t
  1448. (setq sum (+ sum (window-new-normal sub)))))
  1449. (setq sub (window-right sub)))
  1450. ;; Don't get larger than 1 or smaller than 0.
  1451. (min 1.0 (max (- 1.0 sum) 0.0))))))
  1452. (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge)
  1453. "Resize child windows of window PARENT vertically by DELTA lines.
  1454. PARENT must be a vertically combined internal window.
  1455. Optional argument HORIZONTAL non-nil means resize child windows of
  1456. PARENT horizontally by DELTA columns. In this case PARENT must
  1457. be a horizontally combined internal window.
  1458. WINDOW, if specified, must denote a child window of PARENT that
  1459. is resized by DELTA lines.
  1460. Optional argument IGNORE non-nil means ignore restrictions
  1461. imposed by fixed size windows, `window-min-height' or
  1462. `window-min-width' settings. If IGNORE equals `safe', live
  1463. windows may get as small as `window-safe-min-height' lines and
  1464. `window-safe-min-width' columns. If IGNORE is a window, ignore
  1465. restrictions for that window only. Any other non-nil value means
  1466. ignore all of the above restrictions for all windows.
  1467. Optional arguments TRAIL and EDGE, when non-nil, restrict the set
  1468. of windows that shall be resized. If TRAIL equals `before',
  1469. resize only windows on the left or above EDGE. If TRAIL equals
  1470. `after', resize only windows on the right or below EDGE. Also,
  1471. preferably only resize windows adjacent to EDGE.
  1472. Return the symbol `normalized' if new normal sizes have been
  1473. already set by this routine."
  1474. (let* ((first (window-child parent))
  1475. (sub first)
  1476. (parent-total (+ (window-total-size parent horizontal) delta))
  1477. best-window best-value)
  1478. (if (and edge (memq trail '(before after))
  1479. (progn
  1480. (setq sub first)
  1481. (while (and (window-right sub)
  1482. (or (and (eq trail 'before)
  1483. (not (window--resize-child-windows-skip-p
  1484. (window-right sub))))
  1485. (and (eq trail 'after)
  1486. (window--resize-child-windows-skip-p sub))))
  1487. (setq sub (window-right sub)))
  1488. sub)
  1489. (if horizontal
  1490. (if (eq trail 'before)
  1491. (= (+ (window-left-column sub)
  1492. (window-total-size sub t))
  1493. edge)
  1494. (= (window-left-column sub) edge))
  1495. (if (eq trail 'before)
  1496. (= (+ (window-top-line sub)
  1497. (window-total-size sub))
  1498. edge)
  1499. (= (window-top-line sub) edge)))
  1500. (window-sizable-p sub delta horizontal ignore))
  1501. ;; Resize only windows adjacent to EDGE.
  1502. (progn
  1503. (window--resize-this-window
  1504. sub delta horizontal ignore t trail edge)
  1505. (if (and window (eq (window-parent sub) parent))
  1506. (progn
  1507. ;; Assign new normal sizes.
  1508. (set-window-new-normal
  1509. sub (/ (float (window-new-total sub)) parent-total))
  1510. (set-window-new-normal
  1511. window (- (window-normal-size window horizontal)
  1512. (- (window-new-normal sub)
  1513. (window-normal-size sub horizontal)))))
  1514. (window--resize-child-windows-normal
  1515. parent horizontal sub 0 trail delta))
  1516. ;; Return 'normalized to notify `window--resize-siblings' that
  1517. ;; normal sizes have been already set.
  1518. 'normalized)
  1519. ;; Resize all windows proportionally.
  1520. (setq sub first)
  1521. (while sub
  1522. (cond
  1523. ((or (window--resize-child-windows-skip-p sub)
  1524. ;; Ignore windows to skip and fixed-size child windows -
  1525. ;; in the latter case make it a window to skip.
  1526. (and (not ignore)
  1527. (window-size-fixed-p sub horizontal)
  1528. (set-window-new-normal sub 'ignore))))
  1529. ((< delta 0)
  1530. ;; When shrinking store the number of lines/cols we can get
  1531. ;; from this window here together with the total/normal size
  1532. ;; factor.
  1533. (set-window-new-normal
  1534. sub
  1535. (cons
  1536. ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
  1537. (window-min-delta sub horizontal ignore trail t) ; t)
  1538. (- (/ (float (window-total-size sub horizontal))
  1539. parent-total)
  1540. (window-normal-size sub horizontal)))))
  1541. ((> delta 0)
  1542. ;; When enlarging store the total/normal size factor only
  1543. (set-window-new-normal
  1544. sub
  1545. (- (/ (float (window-total-size sub horizontal))
  1546. parent-total)
  1547. (window-normal-size sub horizontal)))))
  1548. (setq sub (window-right sub)))
  1549. (cond
  1550. ((< delta 0)
  1551. ;; Shrink windows by delta.
  1552. (setq best-window t)
  1553. (while (and best-window (not (zerop delta)))
  1554. (setq sub first)
  1555. (setq best-window nil)
  1556. (setq best-value most-negative-fixnum)
  1557. (while sub
  1558. (when (and (consp (window-new-normal sub))
  1559. (not (zerop (car (window-new-normal sub))))
  1560. (> (cdr (window-new-normal sub)) best-value))
  1561. (setq best-window sub)
  1562. (setq best-value (cdr (window-new-normal sub))))
  1563. (setq sub (window-right sub)))
  1564. (when best-window
  1565. (setq delta (1+ delta)))
  1566. (set-window-new-total best-window -1 t)
  1567. (set-window-new-normal
  1568. best-window
  1569. (if (= (car (window-new-normal best-window)) 1)
  1570. 'skip ; We can't shrink best-window any further.
  1571. (cons (1- (car (window-new-normal best-window)))
  1572. (- (/ (float (window-new-total best-window))
  1573. parent-total)
  1574. (window-normal-size best-window horizontal)))))))
  1575. ((> delta 0)
  1576. ;; Enlarge windows by delta.
  1577. (setq best-window t)
  1578. (while (and best-window (not (zerop delta)))
  1579. (setq sub first)
  1580. (setq best-window nil)
  1581. (setq best-value most-positive-fixnum)
  1582. (while sub
  1583. (when (and (numberp (window-new-normal sub))
  1584. (< (window-new-normal sub) best-value))
  1585. (setq best-window sub)
  1586. (setq best-value (window-new-normal sub)))
  1587. (setq sub (window-right sub)))
  1588. (when best-window
  1589. (setq delta (1- delta)))
  1590. (set-window-new-total best-window 1 t)
  1591. (set-window-new-normal
  1592. best-window
  1593. (- (/ (float (window-new-total best-window))
  1594. parent-total)
  1595. (window-normal-size best-window horizontal))))))
  1596. (when best-window
  1597. (setq sub first)
  1598. (while sub
  1599. (when (or (consp (window-new-normal sub))
  1600. (numberp (window-new-normal sub)))
  1601. ;; Reset new normal size fields so `window-resize-apply'
  1602. ;; won't use them to apply new sizes.
  1603. (set-window-new-normal sub))
  1604. (unless (eq (window-new-normal sub) 'ignore)
  1605. ;; Resize this window's child windows (back-engineering
  1606. ;; delta from sub's old and new total sizes).
  1607. (let ((delta (- (window-new-total sub)
  1608. (window-total-size sub horizontal))))
  1609. (unless (and (zerop delta) (not trail))
  1610. ;; For the TRAIL non-nil case we have to resize SUB
  1611. ;; recursively even if it's size does not change.
  1612. (window--resize-this-window
  1613. sub delta horizontal ignore nil trail edge))))
  1614. (setq sub (window-right sub)))))))
  1615. (defun window--resize-siblings (window delta &optional horizontal ignore trail edge)
  1616. "Resize other windows when WINDOW is resized vertically by DELTA lines.
  1617. Optional argument HORIZONTAL non-nil means resize other windows
  1618. when WINDOW is resized horizontally by DELTA columns. WINDOW
  1619. itself is not resized by this function.
  1620. Optional argument IGNORE non-nil means ignore restrictions
  1621. imposed by fixed size windows, `window-min-height' or
  1622. `window-min-width' settings. If IGNORE equals `safe', live
  1623. windows may get as small as `window-safe-min-height' lines and
  1624. `window-safe-min-width' columns. If IGNORE is a window, ignore
  1625. restrictions for that window only. Any other non-nil value means
  1626. ignore all of the above restrictions for all windows.
  1627. Optional arguments TRAIL and EDGE, when non-nil, refine the set
  1628. of windows that shall be resized. If TRAIL equals `before',
  1629. resize only windows on the left or above EDGE. If TRAIL equals
  1630. `after', resize only windows on the right or below EDGE. Also,
  1631. preferably only resize windows adjacent to EDGE."
  1632. (when (window-parent window)
  1633. (let* ((parent (window-parent window))
  1634. (sub (window-child parent)))
  1635. (if (window-combined-p sub horizontal)
  1636. ;; In an iso-combination try to extract DELTA from WINDOW's
  1637. ;; siblings.
  1638. (let ((skip (eq trail 'after))
  1639. this-delta other-delta)
  1640. ;; Decide which windows shall be left alone.
  1641. (while sub
  1642. (cond
  1643. ((eq sub window)
  1644. ;; Make sure WINDOW is left alone when
  1645. ;; resizing its siblings.
  1646. (set-window-new-normal sub 'ignore)
  1647. (setq skip (eq trail 'before)))
  1648. (skip
  1649. ;; Make sure this sibling is left alone when
  1650. ;; resizing its siblings.
  1651. (set-window-new-normal sub 'ignore))
  1652. ((or (window--size-ignore sub ignore)
  1653. (not (window-size-fixed-p sub horizontal)))
  1654. ;; Set this-delta to t to signal that we found a sibling
  1655. ;; of WINDOW whose size is not fixed.
  1656. (setq this-delta t)))
  1657. (setq sub (window-right sub)))
  1658. ;; Set this-delta to what we can get from WINDOW's siblings.
  1659. (if (= (- delta) (window-total-size window horizontal))
  1660. ;; A deletion, presumably. We must handle this case
  1661. ;; specially since `window--resizable' can't be used.
  1662. (if this-delta
  1663. ;; There's at least one resizable sibling we can
  1664. ;; give WINDOW's size to.
  1665. (setq this-delta delta)
  1666. ;; No resizable sibling exists.
  1667. (setq this-delta 0))
  1668. ;; Any other form of resizing.
  1669. (setq this-delta
  1670. (window--resizable window delta horizontal ignore trail t)))
  1671. ;; Set other-delta to what we still have to get from
  1672. ;; ancestor windows of parent.
  1673. (setq other-delta (- delta this-delta))
  1674. (unless (zerop other-delta)
  1675. ;; Unless we got everything from WINDOW's siblings, PARENT
  1676. ;; must be resized by other-delta lines or columns.
  1677. (set-window-new-total parent other-delta 'add))
  1678. (if (zerop this-delta)
  1679. ;; We haven't got anything from WINDOW's siblings but we
  1680. ;; must update the normal sizes to respect other-delta.
  1681. (window--resize-child-windows-normal
  1682. parent horizontal window this-delta trail other-delta)
  1683. ;; We did get something from WINDOW's siblings which means
  1684. ;; we have to resize their child windows.
  1685. (unless (eq (window--resize-child-windows
  1686. parent (- this-delta) horizontal
  1687. window ignore trail edge)
  1688. ;; If `window--resize-child-windows' returns
  1689. ;; 'normalized, this means it has set the
  1690. ;; normal sizes already.
  1691. 'normalized)
  1692. ;; Set the normal sizes.
  1693. (window--resize-child-windows-normal
  1694. parent horizontal window this-delta trail other-delta))
  1695. ;; Set DELTA to what we still have to get from ancestor
  1696. ;; windows.
  1697. (setq delta other-delta)))
  1698. ;; In an ortho-combination all siblings of WINDOW must be
  1699. ;; resized by DELTA.
  1700. (set-window-new-total parent delta 'add)
  1701. (while sub
  1702. (unless (eq sub window)
  1703. (window--resize-this-window sub delta horizontal ignore t))
  1704. (setq sub (window-right sub))))
  1705. (unless (zerop delta)
  1706. ;; "Go up."
  1707. (window--resize-siblings
  1708. parent delta horizontal ignore trail edge)))))
  1709. (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge)
  1710. "Resize WINDOW vertically by DELTA lines.
  1711. Optional argument HORIZONTAL non-nil means resize WINDOW
  1712. horizontally by DELTA columns.
  1713. Optional argument IGNORE non-nil means ignore restrictions
  1714. imposed by fixed size windows, `window-min-height' or
  1715. `window-min-width' settings. If IGNORE equals `safe', live
  1716. windows may get as small as `window-safe-min-height' lines and
  1717. `window-safe-min-width' columns. If IGNORE is a window, ignore
  1718. restrictions for that window only. Any other non-nil value
  1719. means ignore all of the above restrictions for all windows.
  1720. Optional argument ADD non-nil means add DELTA to the new total
  1721. size of WINDOW.
  1722. Optional arguments TRAIL and EDGE, when non-nil, refine the set
  1723. of windows that shall be resized. If TRAIL equals `before',
  1724. resize only windows on the left or above EDGE. If TRAIL equals
  1725. `after', resize only windows on the right or below EDGE. Also,
  1726. preferably only resize windows adjacent to EDGE.
  1727. This function recursively resizes WINDOW's child windows to fit the
  1728. new size. Make sure that WINDOW is `window--resizable' before
  1729. calling this function. Note that this function does not resize
  1730. siblings of WINDOW or WINDOW's parent window. You have to
  1731. eventually call `window-resize-apply' in order to make resizing
  1732. actually take effect."
  1733. (when add
  1734. ;; Add DELTA to the new total size of WINDOW.
  1735. (set-window-new-total window delta t))
  1736. (let ((sub (window-child window)))
  1737. (cond
  1738. ((not sub))
  1739. ((window-combined-p sub horizontal)
  1740. ;; In an iso-combination resize child windows according to their
  1741. ;; normal sizes.
  1742. (window--resize-child-windows
  1743. window delta horizontal nil ignore trail edge))
  1744. ;; In an ortho-combination resize each child window by DELTA.
  1745. (t
  1746. (while sub
  1747. (window--resize-this-window
  1748. sub delta horizontal ignore t trail edge)
  1749. (setq sub (window-right sub)))))))
  1750. (defun window--resize-root-window (window delta horizontal ignore)
  1751. "Resize root window WINDOW vertically by DELTA lines.
  1752. HORIZONTAL non-nil means resize root window WINDOW horizontally
  1753. by DELTA columns.
  1754. IGNORE non-nil means ignore any restrictions imposed by fixed
  1755. size windows, `window-min-height' or `window-min-width' settings.
  1756. This function is only called by the frame resizing routines. It
  1757. resizes windows proportionally and never deletes any windows."
  1758. (when (and (windowp window) (numberp delta)
  1759. (window-sizable-p window delta horizontal ignore))
  1760. (window--resize-reset (window-frame window) horizontal)
  1761. (window--resize-this-window window delta horizontal ignore t)))
  1762. (defun window--resize-root-window-vertically (window delta)
  1763. "Resize root window WINDOW vertically by DELTA lines.
  1764. If DELTA is less than zero and we can't shrink WINDOW by DELTA
  1765. lines, shrink it as much as possible. If DELTA is greater than
  1766. zero, this function can resize fixed-size windows in order to
  1767. recover the necessary lines.
  1768. Return the number of lines that were recovered.
  1769. This function is only called by the minibuffer window resizing
  1770. routines. It resizes windows proportionally and never deletes
  1771. any windows."
  1772. (when (numberp delta)
  1773. (let (ignore)
  1774. (cond
  1775. ((< delta 0)
  1776. (setq delta (window-sizable window delta)))
  1777. ((> delta 0)
  1778. (unless (window-sizable window delta)
  1779. (setq ignore t))))
  1780. (window--resize-reset (window-frame window))
  1781. ;; Ideally, we would resize just the last window in a combination
  1782. ;; but that's not feasible for the following reason: If we grow
  1783. ;; the minibuffer window and the last window cannot be shrunk any
  1784. ;; more, we shrink another window instead. But if we then shrink
  1785. ;; the minibuffer window again, the last window might get enlarged
  1786. ;; and the state after shrinking is not the state before growing.
  1787. ;; So, in practice, we'd need a history variable to record how to
  1788. ;; proceed. But I'm not sure how such a variable could work with
  1789. ;; repeated minibuffer window growing steps.
  1790. (window--resize-this-window window delta nil ignore t)
  1791. delta)))
  1792. (defun adjust-window-trailing-edge (window delta &optional horizontal)
  1793. "Move WINDOW's bottom edge by DELTA lines.
  1794. Optional argument HORIZONTAL non-nil means move WINDOW's right
  1795. edge by DELTA columns. WINDOW defaults to the selected window.
  1796. If DELTA is greater than zero, move the edge downwards or to the
  1797. right. If DELTA is less than zero, move the edge upwards or to
  1798. the left. If the edge can't be moved by DELTA lines or columns,
  1799. move it as far as possible in the desired direction."
  1800. (setq window (window-normalize-window window))
  1801. (let* ((frame (window-frame window))
  1802. (minibuffer-window (minibuffer-window frame))
  1803. (right window)
  1804. left this-delta min-delta max-delta)
  1805. ;; Find the edge we want to move.
  1806. (while (and (or (not (window-combined-p right horizontal))
  1807. (not (window-right right)))
  1808. (setq right (window-parent right))))
  1809. (cond
  1810. ((and (not right) (not horizontal)
  1811. ;; Resize the minibuffer window if it's on the same frame as
  1812. ;; and immediately below WINDOW and it's either active or
  1813. ;; `resize-mini-windows' is nil.
  1814. (eq (window-frame minibuffer-window) frame)
  1815. (= (nth 1 (window-edges minibuffer-window))
  1816. (nth 3 (window-edges window)))
  1817. (or (not resize-mini-windows)
  1818. (eq minibuffer-window (active-minibuffer-window))))
  1819. (window--resize-mini-window minibuffer-window (- delta)))
  1820. ((or (not (setq left right)) (not (setq right (window-right right))))
  1821. (if horizontal
  1822. (error "No window on the right of this one")
  1823. (error "No window below this one")))
  1824. (t
  1825. ;; Set LEFT to the first resizable window on the left. This step is
  1826. ;; needed to handle fixed-size windows.
  1827. (while (and left (window-size-fixed-p left horizontal))
  1828. (setq left
  1829. (or (window-left left)
  1830. (progn
  1831. (while (and (setq left (window-parent left))
  1832. (not (window-combined-p left horizontal))))
  1833. (window-left left)))))
  1834. (unless left
  1835. (if horizontal
  1836. (error "No resizable window on the left of this one")
  1837. (error "No resizable window above this one")))
  1838. ;; Set RIGHT to the first resizable window on the right. This step
  1839. ;; is needed to handle fixed-size windows.
  1840. (while (and right (window-size-fixed-p right horizontal))
  1841. (setq right
  1842. (or (window-right right)
  1843. (progn
  1844. (while (and (setq right (window-parent right))
  1845. (not (window-combined-p right horizontal))))
  1846. (window-right right)))))
  1847. (unless right
  1848. (if horizontal
  1849. (error "No resizable window on the right of this one")
  1850. (error "No resizable window below this one")))
  1851. ;; LEFT and RIGHT (which might be both internal windows) are now the
  1852. ;; two windows we want to resize.
  1853. (cond
  1854. ((> delta 0)
  1855. (setq max-delta (window--max-delta-1 left 0 horizontal nil 'after))
  1856. (setq min-delta (window--min-delta-1 right (- delta) horizontal nil 'before))
  1857. (when (or (< max-delta delta) (> min-delta (- delta)))
  1858. ;; We can't get the whole DELTA - move as far as possible.
  1859. (setq delta (min max-delta (- min-delta))))
  1860. (unless (zerop delta)
  1861. ;; Start resizing.
  1862. (window--resize-reset frame horizontal)
  1863. ;; Try to enlarge LEFT first.
  1864. (setq this-delta (window--resizable left delta horizontal))
  1865. (unless (zerop this-delta)
  1866. (window--resize-this-window
  1867. left this-delta horizontal nil t 'before
  1868. (if horizontal
  1869. (+ (window-left-column left) (window-total-size left t))
  1870. (+ (window-top-line left) (window-total-size left)))))
  1871. ;; Shrink windows on right of LEFT.
  1872. (window--resize-siblings
  1873. left delta horizontal nil 'after
  1874. (if horizontal
  1875. (window-left-column right)
  1876. (window-top-line right)))))
  1877. ((< delta 0)
  1878. (setq max-delta (window--max-delta-1 right 0 horizontal nil 'before))
  1879. (setq min-delta (window--min-delta-1 left delta horizontal nil 'after))
  1880. (when (or (< max-delta (- delta)) (> min-delta delta))
  1881. ;; We can't get the whole DELTA - move as far as possible.
  1882. (setq delta (max (- max-delta) min-delta)))
  1883. (unless (zerop delta)
  1884. ;; Start resizing.
  1885. (window--resize-reset frame horizontal)
  1886. ;; Try to enlarge RIGHT.
  1887. (setq this-delta (window--resizable right (- delta) horizontal))
  1888. (unless (zerop this-delta)
  1889. (window--resize-this-window
  1890. right this-delta horizontal nil t 'after
  1891. (if horizontal
  1892. (window-left-column right)
  1893. (window-top-line right))))
  1894. ;; Shrink windows on left of RIGHT.
  1895. (window--resize-siblings
  1896. right (- delta) horizontal nil 'before
  1897. (if horizontal
  1898. (+ (window-left-column left) (window-total-size left t))
  1899. (+ (window-top-line left) (window-total-size left)))))))
  1900. (unless (zerop delta)
  1901. ;; Don't report an error in the standard case.
  1902. (unless (window-resize-apply frame horizontal)
  1903. ;; But do report an error if applying the changes fails.
  1904. (error "Failed adjusting window %s" window)))))))
  1905. (defun enlarge-window (delta &optional horizontal)
  1906. "Make the selected window DELTA lines taller.
  1907. Interactively, if no argument is given, make the selected window
  1908. one line taller. If optional argument HORIZONTAL is non-nil,
  1909. make selected window wider by DELTA columns. If DELTA is
  1910. negative, shrink selected window by -DELTA lines or columns.
  1911. Return nil."
  1912. (interactive "p")
  1913. (let ((minibuffer-window (minibuffer-window)))
  1914. (cond
  1915. ((zerop delta))
  1916. ((window-size-fixed-p nil horizontal)
  1917. (error "Selected window has fixed size"))
  1918. ((window-minibuffer-p)
  1919. (if horizontal
  1920. (error "Cannot resize minibuffer window horizontally")
  1921. (window--resize-mini-window (selected-window) delta)))
  1922. ((and (not horizontal)
  1923. (window-full-height-p)
  1924. (eq (window-frame minibuffer-window) (selected-frame))
  1925. (not resize-mini-windows))
  1926. ;; If the selected window is full height and `resize-mini-windows'
  1927. ;; is nil, resize the minibuffer window.
  1928. (window--resize-mini-window minibuffer-window (- delta)))
  1929. ((window--resizable-p nil delta horizontal)
  1930. (window-resize nil delta horizontal))
  1931. (t
  1932. (window-resize
  1933. nil (if (> delta 0)
  1934. (window-max-delta nil horizontal)
  1935. (- (window-min-delta nil horizontal)))
  1936. horizontal)))))
  1937. (defun shrink-window (delta &optional horizontal)
  1938. "Make the selected window DELTA lines smaller.
  1939. Interactively, if no argument is given, make the selected window
  1940. one line smaller. If optional argument HORIZONTAL is non-nil,
  1941. make selected window narrower by DELTA columns. If DELTA is
  1942. negative, enlarge selected window by -DELTA lines or columns.
  1943. Also see the `window-min-height' variable.
  1944. Return nil."
  1945. (interactive "p")
  1946. (let ((minibuffer-window (minibuffer-window)))
  1947. (cond
  1948. ((zerop delta))
  1949. ((window-size-fixed-p nil horizontal)
  1950. (error "Selected window has fixed size"))
  1951. ((window-minibuffer-p)
  1952. (if horizontal
  1953. (error "Cannot resize minibuffer window horizontally")
  1954. (window--resize-mini-window (selected-window) (- delta))))
  1955. ((and (not horizontal)
  1956. (window-full-height-p)
  1957. (eq (window-frame minibuffer-window) (selected-frame))
  1958. (not resize-mini-windows))
  1959. ;; If the selected window is full height and `resize-mini-windows'
  1960. ;; is nil, resize the minibuffer window.
  1961. (window--resize-mini-window minibuffer-window delta))
  1962. ((window--resizable-p nil (- delta) horizontal)
  1963. (window-resize nil (- delta) horizontal))
  1964. (t
  1965. (window-resize
  1966. nil (if (> delta 0)
  1967. (- (window-min-delta nil horizontal))
  1968. (window-max-delta nil horizontal))
  1969. horizontal)))))
  1970. (defun maximize-window (&optional window)
  1971. "Maximize WINDOW.
  1972. Make WINDOW as large as possible without deleting any windows.
  1973. WINDOW can be any window and defaults to the selected window."
  1974. (interactive)
  1975. (setq window (window-normalize-window window))
  1976. (window-resize window (window-max-delta window))
  1977. (window-resize window (window-max-delta window t) t))
  1978. (defun minimize-window (&optional window)
  1979. "Minimize WINDOW.
  1980. Make WINDOW as small as possible without deleting any windows.
  1981. WINDOW can be any window and defaults to the selected window."
  1982. (interactive)
  1983. (setq window (window-normalize-window window))
  1984. (window-resize window (- (window-min-delta window)))
  1985. (window-resize window (- (window-min-delta window t)) t))
  1986. (defun frame-root-window-p (window)
  1987. "Return non-nil if WINDOW is the root window of its frame."
  1988. (eq window (frame-root-window window)))
  1989. (defun window--subtree (window &optional next)
  1990. "Return window subtree rooted at WINDOW.
  1991. Optional argument NEXT non-nil means include WINDOW's right
  1992. siblings in the return value.
  1993. See the documentation of `window-tree' for a description of the
  1994. return value."
  1995. (let (list)
  1996. (while window
  1997. (setq list
  1998. (cons
  1999. (cond
  2000. ((window-top-child window)
  2001. (cons t (cons (window-edges window)
  2002. (window--subtree (window-top-child window) t))))
  2003. ((window-left-child window)
  2004. (cons nil (cons (window-edges window)
  2005. (window--subtree (window-left-child window) t))))
  2006. (t window))
  2007. list))
  2008. (setq window (when next (window-next-sibling window))))
  2009. (nreverse list)))
  2010. (defun window-tree (&optional frame)
  2011. "Return the window tree of frame FRAME.
  2012. FRAME must be a live frame and defaults to the selected frame.
  2013. The return value is a list of the form (ROOT MINI), where ROOT
  2014. represents the window tree of the frame's root window, and MINI
  2015. is the frame's minibuffer window.
  2016. If the root window is not split, ROOT is the root window itself.
  2017. Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
  2018. for a horizontal split, and t for a vertical split. EDGES gives
  2019. the combined size and position of the child windows in the split,
  2020. and the rest of the elements are the child windows in the split.
  2021. Each of the child windows may again be a window or a list
  2022. representing a window split, and so on. EDGES is a list (LEFT
  2023. TOP RIGHT BOTTOM) as returned by `window-edges'."
  2024. (setq frame (window-normalize-frame frame))
  2025. (window--subtree (frame-root-window frame) t))
  2026. (defun other-window (count &optional all-frames)
  2027. "Select another window in cyclic ordering of windows.
  2028. COUNT specifies the number of windows to skip, starting with the
  2029. selected window, before making the selection. If COUNT is
  2030. positive, skip COUNT windows forwards. If COUNT is negative,
  2031. skip -COUNT windows backwards. COUNT zero means do not skip any
  2032. window, so select the selected window. In an interactive call,
  2033. COUNT is the numeric prefix argument. Return nil.
  2034. If the `other-window' parameter of the selected window is a
  2035. function and `ignore-window-parameters' is nil, call that
  2036. function with the arguments COUNT and ALL-FRAMES.
  2037. This function does not select a window whose `no-other-window'
  2038. window parameter is non-nil.
  2039. This function uses `next-window' for finding the window to
  2040. select. The argument ALL-FRAMES has the same meaning as in
  2041. `next-window', but the MINIBUF argument of `next-window' is
  2042. always effectively nil."
  2043. (interactive "p")
  2044. (let* ((window (selected-window))
  2045. (function (and (not ignore-window-parameters)
  2046. (window-parameter window 'other-window)))
  2047. old-window old-count)
  2048. (if (functionp function)
  2049. (funcall function count all-frames)
  2050. ;; `next-window' and `previous-window' may return a window we are
  2051. ;; not allowed to select. Hence we need an exit strategy in case
  2052. ;; all windows are non-selectable.
  2053. (catch 'exit
  2054. (while (> count 0)
  2055. (setq window (next-window window nil all-frames))
  2056. (cond
  2057. ((eq window old-window)
  2058. (when (= count old-count)
  2059. ;; Keep out of infinite loops. When COUNT has not changed
  2060. ;; since we last looked at `window' we're probably in one.
  2061. (throw 'exit nil)))
  2062. ((window-parameter window 'no-other-window)
  2063. (unless old-window
  2064. ;; The first non-selectable window `next-window' got us:
  2065. ;; Remember it and the current value of COUNT.
  2066. (setq old-window window)
  2067. (setq old-count count)))
  2068. (t
  2069. (setq count (1- count)))))
  2070. (while (< count 0)
  2071. (setq window (previous-window window nil all-frames))
  2072. (cond
  2073. ((eq window old-window)
  2074. (when (= count old-count)
  2075. ;; Keep out of infinite loops. When COUNT has not changed
  2076. ;; since we last looked at `window' we're probably in one.
  2077. (throw 'exit nil)))
  2078. ((window-parameter window 'no-other-window)
  2079. (unless old-window
  2080. ;; The first non-selectable window `previous-window' got
  2081. ;; us: Remember it and the current value of COUNT.
  2082. (setq old-window window)
  2083. (setq old-count count)))
  2084. (t
  2085. (setq count (1+ count)))))
  2086. (select-window window)
  2087. ;; Always return nil.
  2088. nil))))
  2089. ;; This should probably return non-nil when the selected window is part
  2090. ;; of an atomic window whose root is the frame's root window.
  2091. (defun one-window-p (&optional nomini all-frames)
  2092. "Return non-nil if the selected window is the only window.
  2093. Optional arg NOMINI non-nil means don't count the minibuffer
  2094. even if it is active. Otherwise, the minibuffer is counted
  2095. when it is active.
  2096. Optional argument ALL-FRAMES specifies the set of frames to
  2097. consider, see also `next-window'. ALL-FRAMES nil or omitted
  2098. means consider windows on the selected frame only, plus the
  2099. minibuffer window if specified by the NOMINI argument. If the
  2100. minibuffer counts, consider all windows on all frames that share
  2101. that minibuffer too. The remaining non-nil values of ALL-FRAMES
  2102. with a special meaning are:
  2103. - t means consider all windows on all existing frames.
  2104. - `visible' means consider all windows on all visible frames on
  2105. the current terminal.
  2106. - 0 (the number zero) means consider all windows on all visible
  2107. and iconified frames on the current terminal.
  2108. - A frame means consider all windows on that frame only.
  2109. Anything else means consider all windows on the selected frame
  2110. and no others."
  2111. (let ((base-window (selected-window)))
  2112. (if (and nomini (eq base-window (minibuffer-window)))
  2113. (setq base-window (next-window base-window)))
  2114. (eq base-window
  2115. (next-window base-window (if nomini 'arg) all-frames))))
  2116. ;;; Deleting windows.
  2117. (defun window-deletable-p (&optional window)
  2118. "Return t if WINDOW can be safely deleted from its frame.
  2119. Return `frame' if deleting WINDOW should also delete its
  2120. frame."
  2121. (setq window (window-normalize-window window))
  2122. (unless ignore-window-parameters
  2123. ;; Handle atomicity.
  2124. (when (window-parameter window 'window-atom)
  2125. (setq window (window-atom-root window))))
  2126. (let ((parent (window-parent window))
  2127. (frame (window-frame window)))
  2128. (cond
  2129. ((frame-root-window-p window)
  2130. ;; WINDOW's frame can be deleted only if there are other frames
  2131. ;; on the same terminal.
  2132. (unless (eq frame (next-frame frame 0))
  2133. 'frame))
  2134. ((or ignore-window-parameters
  2135. (not (eq (window-parameter window 'window-side) 'none))
  2136. (and parent (eq (window-parameter parent 'window-side) 'none)))
  2137. ;; WINDOW can be deleted unless it is the main window of its
  2138. ;; frame.
  2139. t))))
  2140. (defun window--in-subtree-p (window root)
  2141. "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
  2142. (or (eq window root)
  2143. (let ((parent (window-parent window)))
  2144. (catch 'done
  2145. (while parent
  2146. (if (eq parent root)
  2147. (throw 'done t)
  2148. (setq parent (window-parent parent))))))))
  2149. (defun delete-window (&optional window)
  2150. "Delete WINDOW.
  2151. WINDOW can be an arbitrary window and defaults to the selected
  2152. one. Return nil.
  2153. If the variable `ignore-window-parameters' is non-nil or the
  2154. `delete-window' parameter of WINDOW equals t, do not process any
  2155. parameters of WINDOW. Otherwise, if the `delete-window'
  2156. parameter of WINDOW specifies a function, call that function with
  2157. WINDOW as its sole argument and return the value returned by that
  2158. function.
  2159. Otherwise, if WINDOW is part of an atomic window, call
  2160. `delete-window' with the root of the atomic window as its
  2161. argument. If WINDOW is the only window on its frame or the last
  2162. non-side window, signal an error."
  2163. (interactive)
  2164. (setq window (window-normalize-window window))
  2165. (let* ((frame (window-frame window))
  2166. (function (window-parameter window 'delete-window))
  2167. (parent (window-parent window))
  2168. atom-root)
  2169. (window--check frame)
  2170. (catch 'done
  2171. ;; Handle window parameters.
  2172. (cond
  2173. ;; Ignore window parameters if `ignore-window-parameters' tells
  2174. ;; us so or `delete-window' equals t.
  2175. ((or ignore-window-parameters (eq function t)))
  2176. ((functionp function)
  2177. ;; The `delete-window' parameter specifies the function to call.
  2178. ;; If that function is `ignore' nothing is done. It's up to the
  2179. ;; function called here to avoid infinite recursion.
  2180. (throw 'done (funcall function window)))
  2181. ((and (window-parameter window 'window-atom)
  2182. (setq atom-root (window-atom-root window))
  2183. (not (eq atom-root window)))
  2184. (throw 'done (delete-window atom-root)))
  2185. ((and (eq (window-parameter window 'window-side) 'none)
  2186. (or (not parent)
  2187. (not (eq (window-parameter parent 'window-side) 'none))))
  2188. (error "Attempt to delete last non-side window"))
  2189. ((not parent)
  2190. (error "Attempt to delete minibuffer or sole ordinary window")))
  2191. (let* ((horizontal (window-left-child parent))
  2192. (size (window-total-size window horizontal))
  2193. (frame-selected
  2194. (window--in-subtree-p (frame-selected-window frame) window))
  2195. ;; Emacs 23 preferably gives WINDOW's space to its left
  2196. ;; sibling.
  2197. (sibling (or (window-left window) (window-right window))))
  2198. (window--resize-reset frame horizontal)
  2199. (cond
  2200. ((and (not window-combination-resize)
  2201. sibling (window-sizable-p sibling size))
  2202. ;; Resize WINDOW's sibling.
  2203. (window--resize-this-window sibling size horizontal nil t)
  2204. (set-window-new-normal
  2205. sibling (+ (window-normal-size sibling horizontal)
  2206. (window-normal-size window horizontal))))
  2207. ((window--resizable-p window (- size) horizontal nil nil nil t)
  2208. ;; Can do without resizing fixed-size windows.
  2209. (window--resize-siblings window (- size) horizontal))
  2210. (t
  2211. ;; Can't do without resizing fixed-size windows.
  2212. (window--resize-siblings window (- size) horizontal t)))
  2213. ;; Actually delete WINDOW.
  2214. (delete-window-internal window)
  2215. (when (and frame-selected
  2216. (window-parameter
  2217. (frame-selected-window frame) 'no-other-window))
  2218. ;; `delete-window-internal' has selected a window that should
  2219. ;; not be selected, fix this here.
  2220. (other-window -1 frame))
  2221. (run-window-configuration-change-hook frame)
  2222. (window--check frame)
  2223. ;; Always return nil.
  2224. nil))))
  2225. (defun delete-other-windows (&optional window)
  2226. "Make WINDOW fill its frame.
  2227. WINDOW may be any window and defaults to the selected one.
  2228. Return nil.
  2229. If the variable `ignore-window-parameters' is non-nil or the
  2230. `delete-other-windows' parameter of WINDOW equals t, do not
  2231. process any parameters of WINDOW. Otherwise, if the
  2232. `delete-other-windows' parameter of WINDOW specifies a function,
  2233. call that function with WINDOW as its sole argument and return
  2234. the value returned by that function.
  2235. Otherwise, if WINDOW is part of an atomic window, call this
  2236. function with the root of the atomic window as its argument. If
  2237. WINDOW is a non-side window, make WINDOW the only non-side window
  2238. on the frame. Side windows are not deleted. If WINDOW is a side
  2239. window signal an error."
  2240. (interactive)
  2241. (setq window (window-normalize-window window))
  2242. (let* ((frame (window-frame window))
  2243. (function (window-parameter window 'delete-other-windows))
  2244. (window-side (window-parameter window 'window-side))
  2245. atom-root side-main)
  2246. (window--check frame)
  2247. (catch 'done
  2248. (cond
  2249. ;; Ignore window parameters if `ignore-window-parameters' is t or
  2250. ;; `delete-other-windows' is t.
  2251. ((or ignore-window-parameters (eq function t)))
  2252. ((functionp function)
  2253. ;; The `delete-other-windows' parameter specifies the function
  2254. ;; to call. If the function is `ignore' no windows are deleted.
  2255. ;; It's up to the function called to avoid infinite recursion.
  2256. (throw 'done (funcall function window)))
  2257. ((and (window-parameter window 'window-atom)
  2258. (setq atom-root (window-atom-root window))
  2259. (not (eq atom-root window)))
  2260. (throw 'done (delete-other-windows atom-root)))
  2261. ((eq window-side 'none)
  2262. ;; Set side-main to the major non-side window.
  2263. (setq side-main (window-with-parameter 'window-side 'none frame t)))
  2264. ((memq window-side window-sides)
  2265. (error "Cannot make side window the only window")))
  2266. ;; If WINDOW is the main non-side window, do nothing.
  2267. (unless (eq window side-main)
  2268. (delete-other-windows-internal window side-main)
  2269. (run-window-configuration-change-hook frame)
  2270. (window--check frame))
  2271. ;; Always return nil.
  2272. nil)))
  2273. (defun delete-other-windows-vertically (&optional window)
  2274. "Delete the windows in the same column with WINDOW, but not WINDOW itself.
  2275. This may be a useful alternative binding for \\[delete-other-windows]
  2276. if you often split windows horizontally."
  2277. (interactive)
  2278. (let* ((window (or window (selected-window)))
  2279. (edges (window-edges window))
  2280. (w window) delenda)
  2281. (while (not (eq (setq w (next-window w 1)) window))
  2282. (let ((e (window-edges w)))
  2283. (when (and (= (car e) (car edges))
  2284. (= (caddr e) (caddr edges)))
  2285. (push w delenda))))
  2286. (mapc 'delete-window delenda)))
  2287. ;;; Windows and buffers.
  2288. ;; `prev-buffers' and `next-buffers' are two reserved window slots used
  2289. ;; for (1) determining which buffer to show in the window when its
  2290. ;; buffer shall be buried or killed and (2) which buffer to show for
  2291. ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
  2292. ;; `prev-buffers' consists of <buffer, window-start, window-point>
  2293. ;; triples. The entries on this list are ordered by the time their
  2294. ;; buffer has been removed from the window, the most recently removed
  2295. ;; buffer's entry being first. The window-start and window-point
  2296. ;; components are `window-start' and `window-point' at the time the
  2297. ;; buffer was removed from the window which implies that the entry must
  2298. ;; be added when `set-window-buffer' removes the buffer from the window.
  2299. ;; `next-buffers' is the list of buffers that have been replaced
  2300. ;; recently by `switch-to-prev-buffer'. These buffers are the least
  2301. ;; preferred candidates of `switch-to-prev-buffer' and the preferred
  2302. ;; candidates of `switch-to-next-buffer' to switch to. This list is
  2303. ;; reset to nil by any action changing the window's buffer with the
  2304. ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
  2305. ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
  2306. ;; `switch-to-next-buffer' pops the last pushed buffer from it.
  2307. ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
  2308. ;; if such a buffer was killed while the window was hidden within a
  2309. ;; window configuration. Such killed buffers get removed whenever
  2310. ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
  2311. ;; The following function is called by `set-window-buffer' _before_ it
  2312. ;; replaces the buffer of the argument window with the new buffer.
  2313. (defun record-window-buffer (&optional window)
  2314. "Record WINDOW's buffer.
  2315. WINDOW must be a live window and defaults to the selected one."
  2316. (let* ((window (window-normalize-window window t))
  2317. (buffer (window-buffer window))
  2318. (entry (assq buffer (window-prev-buffers window))))
  2319. ;; Reset WINDOW's next buffers. If needed, they are resurrected by
  2320. ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
  2321. (set-window-next-buffers window nil)
  2322. (when entry
  2323. ;; Remove all entries for BUFFER from WINDOW's previous buffers.
  2324. (set-window-prev-buffers
  2325. window (assq-delete-all buffer (window-prev-buffers window))))
  2326. ;; Don't record insignificant buffers.
  2327. (unless (eq (aref (buffer-name buffer) 0) ?\s)
  2328. ;; Add an entry for buffer to WINDOW's previous buffers.
  2329. (with-current-buffer buffer
  2330. (let ((start (window-start window))
  2331. (point (window-point-1 window)))
  2332. (setq entry
  2333. (cons buffer
  2334. (if entry
  2335. ;; We have an entry, update marker positions.
  2336. (list (set-marker (nth 1 entry) start)
  2337. (set-marker (nth 2 entry) point))
  2338. ;; Make new markers.
  2339. (list (copy-marker start)
  2340. (copy-marker point)))))
  2341. (set-window-prev-buffers
  2342. window (cons entry (window-prev-buffers window))))))))
  2343. (defun unrecord-window-buffer (&optional window buffer)
  2344. "Unrecord BUFFER in WINDOW.
  2345. WINDOW must be a live window and defaults to the selected one.
  2346. BUFFER must be a live buffer and defaults to the buffer of
  2347. WINDOW."
  2348. (let* ((window (window-normalize-window window t))
  2349. (buffer (or buffer (window-buffer window))))
  2350. (set-window-prev-buffers
  2351. window (assq-delete-all buffer (window-prev-buffers window)))
  2352. (set-window-next-buffers
  2353. window (delq buffer (window-next-buffers window)))))
  2354. (defun set-window-buffer-start-and-point (window buffer &optional start point)
  2355. "Set WINDOW's buffer to BUFFER.
  2356. Optional argument START non-nil means set WINDOW's start position
  2357. to START. Optional argument POINT non-nil means set WINDOW's
  2358. point to POINT. If WINDOW is selected this also sets BUFFER's
  2359. `point' to POINT. If WINDOW is selected and the buffer it showed
  2360. before was current this also makes BUFFER the current buffer."
  2361. (let ((selected (eq window (selected-window)))
  2362. (current (eq (window-buffer window) (current-buffer))))
  2363. (set-window-buffer window buffer)
  2364. (when (and selected current)
  2365. (set-buffer buffer))
  2366. (when start
  2367. ;; Don't force window-start here (even if POINT is nil).
  2368. (set-window-start window start t))
  2369. (when point
  2370. (set-window-point-1 window point))))
  2371. (defcustom switch-to-visible-buffer t
  2372. "If non-nil, allow switching to an already visible buffer.
  2373. If this variable is non-nil, `switch-to-prev-buffer' and
  2374. `switch-to-next-buffer' may switch to an already visible buffer
  2375. provided the buffer was shown in the argument window before. If
  2376. this variable is nil, `switch-to-prev-buffer' and
  2377. `switch-to-next-buffer' always try to avoid switching to a buffer
  2378. that is already visible in another window on the same frame."
  2379. :type 'boolean
  2380. :version "24.1"
  2381. :group 'windows)
  2382. (defun switch-to-prev-buffer (&optional window bury-or-kill)
  2383. "In WINDOW switch to previous buffer.
  2384. WINDOW must be a live window and defaults to the selected one.
  2385. Optional argument BURY-OR-KILL non-nil means the buffer currently
  2386. shown in WINDOW is about to be buried or killed and consequently
  2387. shall not be switched to in future invocations of this command."
  2388. (interactive)
  2389. (let* ((window (window-normalize-window window t))
  2390. (frame (window-frame window))
  2391. (old-buffer (window-buffer window))
  2392. ;; Save this since it's destroyed by `set-window-buffer'.
  2393. (next-buffers (window-next-buffers window))
  2394. entry new-buffer killed-buffers visible)
  2395. (when (window-dedicated-p window)
  2396. (error "Window %s is dedicated to buffer %s" window old-buffer))
  2397. (catch 'found
  2398. ;; Scan WINDOW's previous buffers first, skipping entries of next
  2399. ;; buffers.
  2400. (dolist (entry (window-prev-buffers window))
  2401. (when (and (setq new-buffer (car entry))
  2402. (or (buffer-live-p new-buffer)
  2403. (not (setq killed-buffers
  2404. (cons new-buffer killed-buffers))))
  2405. (not (eq new-buffer old-buffer))
  2406. (or bury-or-kill
  2407. (not (memq new-buffer next-buffers))))
  2408. (if (and (not switch-to-visible-buffer)
  2409. (get-buffer-window new-buffer frame))
  2410. ;; Try to avoid showing a buffer visible in some other window.
  2411. (setq visible new-buffer)
  2412. (set-window-buffer-start-and-point
  2413. window new-buffer (nth 1 entry) (nth 2 entry))
  2414. (throw 'found t))))
  2415. ;; Scan reverted buffer list of WINDOW's frame next, skipping
  2416. ;; entries of next buffers. Note that when we bury or kill a
  2417. ;; buffer we don't reverse the global buffer list to avoid showing
  2418. ;; a buried buffer instead. Otherwise, we must reverse the global
  2419. ;; buffer list in order to make sure that switching to the
  2420. ;; previous/next buffer traverse it in opposite directions.
  2421. (dolist (buffer (if bury-or-kill
  2422. (buffer-list frame)
  2423. (nreverse (buffer-list frame))))
  2424. (when (and (buffer-live-p buffer)
  2425. (not (eq buffer old-buffer))
  2426. (not (eq (aref (buffer-name buffer) 0) ?\s))
  2427. (or bury-or-kill (not (memq buffer next-buffers))))
  2428. (if (get-buffer-window buffer frame)
  2429. ;; Try to avoid showing a buffer visible in some other window.
  2430. (unless visible
  2431. (setq visible buffer))
  2432. (setq new-buffer buffer)
  2433. (set-window-buffer-start-and-point window new-buffer)
  2434. (throw 'found t))))
  2435. (unless bury-or-kill
  2436. ;; Scan reverted next buffers last (must not use nreverse
  2437. ;; here!).
  2438. (dolist (buffer (reverse next-buffers))
  2439. ;; Actually, buffer _must_ be live here since otherwise it
  2440. ;; would have been caught in the scan of previous buffers.
  2441. (when (and (or (buffer-live-p buffer)
  2442. (not (setq killed-buffers
  2443. (cons buffer killed-buffers))))
  2444. (not (eq buffer old-buffer))
  2445. (setq entry (assq buffer (window-prev-buffers window))))
  2446. (setq new-buffer buffer)
  2447. (set-window-buffer-start-and-point
  2448. window new-buffer (nth 1 entry) (nth 2 entry))
  2449. (throw 'found t))))
  2450. ;; Show a buffer visible in another window.
  2451. (when visible
  2452. (setq new-buffer visible)
  2453. (set-window-buffer-start-and-point window new-buffer)))
  2454. (if bury-or-kill
  2455. ;; Remove `old-buffer' from WINDOW's previous and (restored list
  2456. ;; of) next buffers.
  2457. (progn
  2458. (set-window-prev-buffers
  2459. window (assq-delete-all old-buffer (window-prev-buffers window)))
  2460. (set-window-next-buffers window (delq old-buffer next-buffers)))
  2461. ;; Move `old-buffer' to head of WINDOW's restored list of next
  2462. ;; buffers.
  2463. (set-window-next-buffers
  2464. window (cons old-buffer (delq old-buffer next-buffers))))
  2465. ;; Remove killed buffers from WINDOW's previous and next buffers.
  2466. (when killed-buffers
  2467. (dolist (buffer killed-buffers)
  2468. (set-window-prev-buffers
  2469. window (assq-delete-all buffer (window-prev-buffers window)))
  2470. (set-window-next-buffers
  2471. window (delq buffer (window-next-buffers window)))))
  2472. ;; Return new-buffer.
  2473. new-buffer))
  2474. (defun switch-to-next-buffer (&optional window)
  2475. "In WINDOW switch to next buffer.
  2476. WINDOW must be a live window and defaults to the selected one."
  2477. (interactive)
  2478. (let* ((window (window-normalize-window window t))
  2479. (frame (window-frame window))
  2480. (old-buffer (window-buffer window))
  2481. (next-buffers (window-next-buffers window))
  2482. new-buffer entry killed-buffers visible)
  2483. (when (window-dedicated-p window)
  2484. (error "Window %s is dedicated to buffer %s" window old-buffer))
  2485. (catch 'found
  2486. ;; Scan WINDOW's next buffers first.
  2487. (dolist (buffer next-buffers)
  2488. (when (and (or (buffer-live-p buffer)
  2489. (not (setq killed-buffers
  2490. (cons buffer killed-buffers))))
  2491. (not (eq buffer old-buffer))
  2492. (setq entry (assq buffer (window-prev-buffers window))))
  2493. (setq new-buffer buffer)
  2494. (set-window-buffer-start-and-point
  2495. window new-buffer (nth 1 entry) (nth 2 entry))
  2496. (throw 'found t)))
  2497. ;; Scan the buffer list of WINDOW's frame next, skipping previous
  2498. ;; buffers entries.
  2499. (dolist (buffer (buffer-list frame))
  2500. (when (and (buffer-live-p buffer) (not (eq buffer old-buffer))
  2501. (not (eq (aref (buffer-name buffer) 0) ?\s))
  2502. (not (assq buffer (window-prev-buffers window))))
  2503. (if (get-buffer-window buffer frame)
  2504. ;; Try to avoid showing a buffer visible in some other window.
  2505. (setq visible buffer)
  2506. (setq new-buffer buffer)
  2507. (set-window-buffer-start-and-point window new-buffer)
  2508. (throw 'found t))))
  2509. ;; Scan WINDOW's reverted previous buffers last (must not use
  2510. ;; nreverse here!)
  2511. (dolist (entry (reverse (window-prev-buffers window)))
  2512. (when (and (setq new-buffer (car entry))
  2513. (or (buffer-live-p new-buffer)
  2514. (not (setq killed-buffers
  2515. (cons new-buffer killed-buffers))))
  2516. (not (eq new-buffer old-buffer)))
  2517. (if (and (not switch-to-visible-buffer)
  2518. (get-buffer-window new-buffer frame))
  2519. ;; Try to avoid showing a buffer visible in some other window.
  2520. (unless visible
  2521. (setq visible new-buffer))
  2522. (set-window-buffer-start-and-point
  2523. window new-buffer (nth 1 entry) (nth 2 entry))
  2524. (throw 'found t))))
  2525. ;; Show a buffer visible in another window.
  2526. (when visible
  2527. (setq new-buffer visible)
  2528. (set-window-buffer-start-and-point window new-buffer)))
  2529. ;; Remove `new-buffer' from and restore WINDOW's next buffers.
  2530. (set-window-next-buffers window (delq new-buffer next-buffers))
  2531. ;; Remove killed buffers from WINDOW's previous and next buffers.
  2532. (when killed-buffers
  2533. (dolist (buffer killed-buffers)
  2534. (set-window-prev-buffers
  2535. window (assq-delete-all buffer (window-prev-buffers window)))
  2536. (set-window-next-buffers
  2537. window (delq buffer (window-next-buffers window)))))
  2538. ;; Return new-buffer.
  2539. new-buffer))
  2540. (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
  2541. "Search LIST for a valid buffer to display in FRAME.
  2542. Return nil when all buffers in LIST are undesirable for display,
  2543. otherwise return the first suitable buffer in LIST.
  2544. Buffers not visible in windows are preferred to visible buffers,
  2545. unless VISIBLE-OK is non-nil.
  2546. If the optional argument FRAME is nil, it defaults to the selected frame.
  2547. If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
  2548. ;; This logic is more or less copied from other-buffer.
  2549. (setq frame (or frame (selected-frame)))
  2550. (let ((pred (frame-parameter frame 'buffer-predicate))
  2551. found buf)
  2552. (while (and (not found) list)
  2553. (setq buf (car list))
  2554. (if (and (not (eq buffer buf))
  2555. (buffer-live-p buf)
  2556. (or (null pred) (funcall pred buf))
  2557. (not (eq (aref (buffer-name buf) 0) ?\s))
  2558. (or visible-ok (null (get-buffer-window buf 'visible))))
  2559. (setq found buf)
  2560. (setq list (cdr list))))
  2561. (car list)))
  2562. (defun last-buffer (&optional buffer visible-ok frame)
  2563. "Return the last buffer in FRAME's buffer list.
  2564. If BUFFER is the last buffer, return the preceding buffer
  2565. instead. Buffers not visible in windows are preferred to visible
  2566. buffers, unless optional argument VISIBLE-OK is non-nil.
  2567. Optional third argument FRAME nil or omitted means use the
  2568. selected frame's buffer list. If no such buffer exists, return
  2569. the buffer `*scratch*', creating it if necessary."
  2570. (setq frame (or frame (selected-frame)))
  2571. (or (get-next-valid-buffer (nreverse (buffer-list frame))
  2572. buffer visible-ok frame)
  2573. (get-buffer "*scratch*")
  2574. (let ((scratch (get-buffer-create "*scratch*")))
  2575. (set-buffer-major-mode scratch)
  2576. scratch)))
  2577. (defcustom frame-auto-hide-function #'iconify-frame
  2578. "Function called to automatically hide frames.
  2579. The function is called with one argument - a frame.
  2580. Functions affected by this option are those that bury a buffer
  2581. shown in a separate frame like `quit-window' and `bury-buffer'."
  2582. :type '(choice (const :tag "Iconify" iconify-frame)
  2583. (const :tag "Delete" delete-frame)
  2584. (const :tag "Do nothing" ignore)
  2585. function)
  2586. :group 'windows
  2587. :group 'frames
  2588. :version "24.1")
  2589. (defun window--delete (&optional window dedicated-only kill)
  2590. "Delete WINDOW if possible.
  2591. WINDOW must be a live window and defaults to the selected one.
  2592. Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
  2593. only if it's dedicated to its buffer. Optional argument KILL
  2594. means the buffer shown in window will be killed. Return non-nil
  2595. if WINDOW gets deleted or its frame is auto-hidden."
  2596. (setq window (window-normalize-window window t))
  2597. (unless (and dedicated-only (not (window-dedicated-p window)))
  2598. (let ((deletable (window-deletable-p window)))
  2599. (cond
  2600. ((eq deletable 'frame)
  2601. (let ((frame (window-frame window)))
  2602. (cond
  2603. (kill
  2604. (delete-frame frame))
  2605. ((functionp frame-auto-hide-function)
  2606. (funcall frame-auto-hide-function frame))))
  2607. 'frame)
  2608. (deletable
  2609. (delete-window window)
  2610. t)))))
  2611. (defun bury-buffer (&optional buffer-or-name)
  2612. "Put BUFFER-OR-NAME at the end of the list of all buffers.
  2613. There it is the least likely candidate for `other-buffer' to
  2614. return; thus, the least likely buffer for \\[switch-to-buffer] to
  2615. select by default.
  2616. You can specify a buffer name as BUFFER-OR-NAME, or an actual
  2617. buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
  2618. current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
  2619. remove the current buffer from the selected window if it is
  2620. displayed there."
  2621. (interactive)
  2622. (let* ((buffer (window-normalize-buffer buffer-or-name)))
  2623. ;; If `buffer-or-name' is not on the selected frame we unrecord it
  2624. ;; although it's not "here" (call it a feature).
  2625. (bury-buffer-internal buffer)
  2626. ;; Handle case where `buffer-or-name' is nil and the current buffer
  2627. ;; is shown in the selected window.
  2628. (cond
  2629. ((or buffer-or-name (not (eq buffer (window-buffer)))))
  2630. ((window--delete nil t))
  2631. (t
  2632. ;; Switch to another buffer in window.
  2633. (set-window-dedicated-p nil nil)
  2634. (switch-to-prev-buffer nil 'bury)))
  2635. ;; Always return nil.
  2636. nil))
  2637. (defun unbury-buffer ()
  2638. "Switch to the last buffer in the buffer list."
  2639. (interactive)
  2640. (switch-to-buffer (last-buffer)))
  2641. (defun next-buffer ()
  2642. "In selected window switch to next buffer."
  2643. (interactive)
  2644. (if (window-minibuffer-p)
  2645. (error "Cannot switch buffers in minibuffer window"))
  2646. (switch-to-next-buffer))
  2647. (defun previous-buffer ()
  2648. "In selected window switch to previous buffer."
  2649. (interactive)
  2650. (if (window-minibuffer-p)
  2651. (error "Cannot switch buffers in minibuffer window"))
  2652. (switch-to-prev-buffer))
  2653. (defun delete-windows-on (&optional buffer-or-name frame)
  2654. "Delete all windows showing BUFFER-OR-NAME.
  2655. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  2656. and defaults to the current buffer.
  2657. The following non-nil values of the optional argument FRAME
  2658. have special meanings:
  2659. - t means consider all windows on the selected frame only.
  2660. - `visible' means consider all windows on all visible frames on
  2661. the current terminal.
  2662. - 0 (the number zero) means consider all windows on all visible
  2663. and iconified frames on the current terminal.
  2664. - A frame means consider all windows on that frame only.
  2665. Any other value of FRAME means consider all windows on all
  2666. frames.
  2667. When a window showing BUFFER-OR-NAME is dedicated and the only
  2668. window of its frame, that frame is deleted when there are other
  2669. frames left."
  2670. (interactive "BDelete windows on (buffer):\nP")
  2671. (let ((buffer (window-normalize-buffer buffer-or-name))
  2672. ;; Handle the "inverted" meaning of the FRAME argument wrt other
  2673. ;; `window-list-1' based function.
  2674. (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
  2675. (dolist (window (window-list-1 nil nil all-frames))
  2676. (if (eq (window-buffer window) buffer)
  2677. (let ((deletable (window-deletable-p window)))
  2678. (cond
  2679. ((and (eq deletable 'frame) (window-dedicated-p window))
  2680. ;; Delete frame if and only if window is dedicated.
  2681. (delete-frame (window-frame window)))
  2682. ((eq deletable t)
  2683. ;; Delete window.
  2684. (delete-window window))
  2685. (t
  2686. ;; In window switch to previous buffer.
  2687. (set-window-dedicated-p window nil)
  2688. (switch-to-prev-buffer window 'bury))))
  2689. ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
  2690. (unrecord-window-buffer window buffer)))))
  2691. (defun replace-buffer-in-windows (&optional buffer-or-name)
  2692. "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
  2693. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  2694. and defaults to the current buffer.
  2695. When a window showing BUFFER-OR-NAME is dedicated, that window is
  2696. deleted. If that window is the only window on its frame, the
  2697. frame is deleted too when there are other frames left. If there
  2698. are no other frames left, some other buffer is displayed in that
  2699. window.
  2700. This function removes the buffer denoted by BUFFER-OR-NAME from
  2701. all window-local buffer lists."
  2702. (interactive "bBuffer to replace: ")
  2703. (let ((buffer (window-normalize-buffer buffer-or-name)))
  2704. (dolist (window (window-list-1 nil nil t))
  2705. (if (eq (window-buffer window) buffer)
  2706. (unless (window--delete window t t)
  2707. ;; Switch to another buffer in window.
  2708. (set-window-dedicated-p window nil)
  2709. (switch-to-prev-buffer window 'kill))
  2710. ;; Unrecord BUFFER in WINDOW.
  2711. (unrecord-window-buffer window buffer)))))
  2712. (defun quit-window (&optional kill window)
  2713. "Quit WINDOW and bury its buffer.
  2714. WINDOW must be a live window and defaults to the selected one.
  2715. With prefix argument KILL non-nil, kill the buffer instead of
  2716. burying it.
  2717. According to information stored in WINDOW's `quit-restore' window
  2718. parameter either (1) delete WINDOW and its frame, (2) delete
  2719. WINDOW, (3) restore the buffer previously displayed in WINDOW,
  2720. or (4) make WINDOW display some other buffer than the present
  2721. one. If non-nil, reset `quit-restore' parameter to nil."
  2722. (interactive "P")
  2723. (setq window (window-normalize-window window t))
  2724. (let* ((buffer (window-buffer window))
  2725. (quit-restore (window-parameter window 'quit-restore))
  2726. (prev-buffer
  2727. (let* ((prev-buffers (window-prev-buffers window))
  2728. (prev-buffer (caar prev-buffers)))
  2729. (and (or (not (eq prev-buffer buffer))
  2730. (and (cdr prev-buffers)
  2731. (not (eq (setq prev-buffer (cadr prev-buffers))
  2732. buffer))))
  2733. prev-buffer)))
  2734. quad resize)
  2735. (cond
  2736. ((and (not prev-buffer)
  2737. (memq (nth 1 quit-restore) '(window frame))
  2738. (eq (nth 3 quit-restore) buffer)
  2739. ;; Delete WINDOW if possible.
  2740. (window--delete window nil kill))
  2741. ;; If the previously selected window is still alive, select it.
  2742. (when (window-live-p (nth 2 quit-restore))
  2743. (select-window (nth 2 quit-restore))))
  2744. ((and (listp (setq quad (nth 1 quit-restore)))
  2745. (buffer-live-p (car quad))
  2746. (eq (nth 3 quit-restore) buffer))
  2747. ;; Show another buffer stored in quit-restore parameter.
  2748. (setq resize (with-current-buffer buffer
  2749. (and temp-buffer-resize-mode
  2750. (/= (nth 3 quad) (window-total-size window)))))
  2751. (set-window-dedicated-p window nil)
  2752. (when resize
  2753. ;; Try to resize WINDOW to its old height but don't signal an
  2754. ;; error.
  2755. (condition-case nil
  2756. (window-resize window (- (nth 3 quad) (window-total-size window)))
  2757. (error nil)))
  2758. ;; Restore WINDOW's previous buffer, start and point position.
  2759. (set-window-buffer-start-and-point
  2760. window (nth 0 quad) (nth 1 quad) (nth 2 quad))
  2761. ;; Unrecord WINDOW's buffer here (Bug#9937) to make sure it's not
  2762. ;; re-recorded by `set-window-buffer'.
  2763. (unrecord-window-buffer window buffer)
  2764. ;; Reset the quit-restore parameter.
  2765. (set-window-parameter window 'quit-restore nil)
  2766. ;; Select old window.
  2767. (when (window-live-p (nth 2 quit-restore))
  2768. (select-window (nth 2 quit-restore))))
  2769. (t
  2770. ;; Show some other buffer in WINDOW and reset the quit-restore
  2771. ;; parameter.
  2772. (set-window-parameter window 'quit-restore nil)
  2773. ;; Make sure that WINDOW is no more dedicated.
  2774. (set-window-dedicated-p window nil)
  2775. (switch-to-prev-buffer window 'bury-or-kill)))
  2776. ;; Kill WINDOW's old-buffer if requested
  2777. (if kill
  2778. (kill-buffer buffer)
  2779. (bury-buffer-internal buffer))))
  2780. (defun quit-windows-on (&optional buffer-or-name kill frame)
  2781. "Quit all windows showing BUFFER-OR-NAME.
  2782. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  2783. and defaults to the current buffer. Optional argument KILL
  2784. non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
  2785. BUFFER-OR-NAME. Optional argument FRAME is handled as by
  2786. `delete-windows-on'.
  2787. This function calls `quit-window' on all candidate windows
  2788. showing BUFFER-OR-NAME."
  2789. (interactive "BQuit windows on (buffer):\nP")
  2790. (let ((buffer (window-normalize-buffer buffer-or-name))
  2791. ;; Handle the "inverted" meaning of the FRAME argument wrt other
  2792. ;; `window-list-1' based function.
  2793. (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
  2794. (dolist (window (window-list-1 nil nil all-frames))
  2795. (if (eq (window-buffer window) buffer)
  2796. (quit-window kill window)
  2797. ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
  2798. (unrecord-window-buffer window buffer)))))
  2799. ;;; Splitting windows.
  2800. (defun window-split-min-size (&optional horizontal)
  2801. "Return minimum height of any window when splitting windows.
  2802. Optional argument HORIZONTAL non-nil means return minimum width."
  2803. (if horizontal
  2804. (max window-min-width window-safe-min-width)
  2805. (max window-min-height window-safe-min-height)))
  2806. (defun split-window (&optional window size side)
  2807. "Make a new window adjacent to WINDOW.
  2808. WINDOW can be any window and defaults to the selected one.
  2809. Return the new window which is always a live window.
  2810. Optional argument SIZE a positive number means make WINDOW SIZE
  2811. lines or columns tall. If SIZE is negative, make the new window
  2812. -SIZE lines or columns tall. If and only if SIZE is non-nil, its
  2813. absolute value can be less than `window-min-height' or
  2814. `window-min-width'; so this command can make a new window as
  2815. small as one line or two columns. SIZE defaults to half of
  2816. WINDOW's size. Interactively, SIZE is the prefix argument.
  2817. Optional third argument SIDE nil (or `below') specifies that the
  2818. new window shall be located below WINDOW. SIDE `above' means the
  2819. new window shall be located above WINDOW. In both cases SIZE
  2820. specifies the new number of lines for WINDOW (or the new window
  2821. if SIZE is negative) including space reserved for the mode and/or
  2822. header line.
  2823. SIDE t (or `right') specifies that the new window shall be
  2824. located on the right side of WINDOW. SIDE `left' means the new
  2825. window shall be located on the left of WINDOW. In both cases
  2826. SIZE specifies the new number of columns for WINDOW (or the new
  2827. window provided SIZE is negative) including space reserved for
  2828. fringes and the scrollbar or a divider column. Any other non-nil
  2829. value for SIDE is currently handled like t (or `right').
  2830. If the variable `ignore-window-parameters' is non-nil or the
  2831. `split-window' parameter of WINDOW equals t, do not process any
  2832. parameters of WINDOW. Otherwise, if the `split-window' parameter
  2833. of WINDOW specifies a function, call that function with all three
  2834. arguments and return the value returned by that function.
  2835. Otherwise, if WINDOW is part of an atomic window, \"split\" the
  2836. root of that atomic window. The new window does not become a
  2837. member of that atomic window.
  2838. If WINDOW is live, properties of the new window like margins and
  2839. scrollbars are inherited from WINDOW. If WINDOW is an internal
  2840. window, these properties as well as the buffer displayed in the
  2841. new window are inherited from the window selected on WINDOW's
  2842. frame. The selected window is not changed by this function."
  2843. (interactive "i")
  2844. (setq window (window-normalize-window window))
  2845. (let* ((side (cond
  2846. ((not side) 'below)
  2847. ((memq side '(below above right left)) side)
  2848. (t 'right)))
  2849. (horizontal (not (memq side '(nil below above))))
  2850. (frame (window-frame window))
  2851. (parent (window-parent window))
  2852. (function (window-parameter window 'split-window))
  2853. (window-side (window-parameter window 'window-side))
  2854. ;; Rebind `window-combination-limit' since in some cases we may
  2855. ;; have to override its value.
  2856. (window-combination-limit window-combination-limit)
  2857. atom-root)
  2858. (window--check frame)
  2859. (catch 'done
  2860. (cond
  2861. ;; Ignore window parameters if either `ignore-window-parameters'
  2862. ;; is t or the `split-window' parameter equals t.
  2863. ((or ignore-window-parameters (eq function t)))
  2864. ((functionp function)
  2865. ;; The `split-window' parameter specifies the function to call.
  2866. ;; If that function is `ignore', do nothing.
  2867. (throw 'done (funcall function window size side)))
  2868. ;; If WINDOW is part of an atomic window, split the root window
  2869. ;; of that atomic window instead.
  2870. ((and (window-parameter window 'window-atom)
  2871. (setq atom-root (window-atom-root window))
  2872. (not (eq atom-root window)))
  2873. (throw 'done (split-window atom-root size side))))
  2874. (when (and window-side
  2875. (or (not parent)
  2876. (not (window-parameter parent 'window-side))))
  2877. ;; WINDOW is a side root window. To make sure that a new parent
  2878. ;; window gets created set `window-combination-limit' to t.
  2879. (setq window-combination-limit t))
  2880. (when (and window-combination-resize size (> size 0))
  2881. ;; If `window-combination-resize' is non-nil and SIZE is a
  2882. ;; non-negative integer, we cannot reasonably resize other
  2883. ;; windows. Rather bind `window-combination-limit' to t to make
  2884. ;; sure that subsequent window deletions are handled correctly.
  2885. (setq window-combination-limit t))
  2886. (let* ((parent-size
  2887. ;; `parent-size' is the size of WINDOW's parent, provided
  2888. ;; it has one.
  2889. (when parent (window-total-size parent horizontal)))
  2890. ;; `resize' non-nil means we are supposed to resize other
  2891. ;; windows in WINDOW's combination.
  2892. (resize
  2893. (and window-combination-resize (not window-combination-limit)
  2894. ;; Resize makes sense in iso-combinations only.
  2895. (window-combined-p window horizontal)))
  2896. ;; `old-size' is the current size of WINDOW.
  2897. (old-size (window-total-size window horizontal))
  2898. ;; `new-size' is the specified or calculated size of the
  2899. ;; new window.
  2900. (new-size
  2901. (cond
  2902. ((not size)
  2903. (max (window-split-min-size horizontal)
  2904. (if resize
  2905. ;; When resizing try to give the new window the
  2906. ;; average size of a window in its combination.
  2907. (min (- parent-size
  2908. (window-min-size parent horizontal))
  2909. (/ parent-size
  2910. (1+ (window-combinations
  2911. parent horizontal))))
  2912. ;; Else try to give the new window half the size
  2913. ;; of WINDOW (plus an eventual odd line).
  2914. (+ (/ old-size 2) (% old-size 2)))))
  2915. ((>= size 0)
  2916. ;; SIZE non-negative specifies the new size of WINDOW.
  2917. ;; Note: Specifying a non-negative SIZE is practically
  2918. ;; always done as workaround for making the new window
  2919. ;; appear above or on the left of the new window (the
  2920. ;; ispell window is a typical example of that). In all
  2921. ;; these cases the SIDE argument should be set to 'above
  2922. ;; or 'left in order to support the 'resize option.
  2923. ;; Here we have to nest the windows instead, see above.
  2924. (- old-size size))
  2925. (t
  2926. ;; SIZE negative specifies the size of the new window.
  2927. (- size))))
  2928. new-parent new-normal)
  2929. ;; Check SIZE.
  2930. (cond
  2931. ((not size)
  2932. (cond
  2933. (resize
  2934. ;; SIZE unspecified, resizing.
  2935. (when (and (not (window-sizable-p parent (- new-size) horizontal))
  2936. ;; Try again with minimum split size.
  2937. (setq new-size
  2938. (max new-size (window-split-min-size horizontal)))
  2939. (not (window-sizable-p parent (- new-size) horizontal)))
  2940. (error "Window %s too small for splitting" parent)))
  2941. ((> (+ new-size (window-min-size window horizontal)) old-size)
  2942. ;; SIZE unspecified, no resizing.
  2943. (error "Window %s too small for splitting" window))))
  2944. ((and (>= size 0)
  2945. (or (>= size old-size)
  2946. (< new-size (if horizontal
  2947. window-safe-min-width
  2948. window-safe-min-width))))
  2949. ;; SIZE specified as new size of old window. If the new size
  2950. ;; is larger than the old size or the size of the new window
  2951. ;; would be less than the safe minimum, signal an error.
  2952. (error "Window %s too small for splitting" window))
  2953. (resize
  2954. ;; SIZE specified, resizing.
  2955. (unless (window-sizable-p parent (- new-size) horizontal)
  2956. ;; If we cannot resize the parent give up.
  2957. (error "Window %s too small for splitting" parent)))
  2958. ((or (< new-size
  2959. (if horizontal window-safe-min-width window-safe-min-height))
  2960. (< (- old-size new-size)
  2961. (if horizontal window-safe-min-width window-safe-min-height)))
  2962. ;; SIZE specification violates minimum size restrictions.
  2963. (error "Window %s too small for splitting" window)))
  2964. (window--resize-reset frame horizontal)
  2965. (setq new-parent
  2966. ;; Make new-parent non-nil if we need a new parent window;
  2967. ;; either because we want to nest or because WINDOW is not
  2968. ;; iso-combined.
  2969. (or window-combination-limit
  2970. (not (window-combined-p window horizontal))))
  2971. (setq new-normal
  2972. ;; Make new-normal the normal size of the new window.
  2973. (cond
  2974. (size (/ (float new-size) (if new-parent old-size parent-size)))
  2975. (new-parent 0.5)
  2976. (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
  2977. (t (/ (window-normal-size window horizontal) 2.0))))
  2978. (if resize
  2979. ;; Try to get space from OLD's siblings. We could go "up" and
  2980. ;; try getting additional space from surrounding windows but
  2981. ;; we won't be able to return space to those windows when we
  2982. ;; delete the one we create here. Hence we do not go up.
  2983. (progn
  2984. (window--resize-child-windows parent (- new-size) horizontal)
  2985. (let* ((normal (- 1.0 new-normal))
  2986. (sub (window-child parent)))
  2987. (while sub
  2988. (set-window-new-normal
  2989. sub (* (window-normal-size sub horizontal) normal))
  2990. (setq sub (window-right sub)))))
  2991. ;; Get entire space from WINDOW.
  2992. (set-window-new-total window (- old-size new-size))
  2993. (window--resize-this-window window (- new-size) horizontal)
  2994. (set-window-new-normal
  2995. window (- (if new-parent 1.0 (window-normal-size window horizontal))
  2996. new-normal)))
  2997. (let* ((new (split-window-internal window new-size side new-normal)))
  2998. ;; Inherit window-side parameters, if any.
  2999. (when (and window-side new-parent)
  3000. (set-window-parameter (window-parent new) 'window-side window-side)
  3001. (set-window-parameter new 'window-side window-side))
  3002. (run-window-configuration-change-hook frame)
  3003. (window--check frame)
  3004. ;; Always return the new window.
  3005. new)))))
  3006. ;; I think this should be the default; I think people will prefer it--rms.
  3007. (defcustom split-window-keep-point t
  3008. "If non-nil, \\[split-window-below] preserves point in the new window.
  3009. If nil, adjust point in the two windows to minimize redisplay.
  3010. This option applies only to `split-window-below' and functions
  3011. that call it. The low-level `split-window' function always keeps
  3012. the original point in both windows."
  3013. :type 'boolean
  3014. :group 'windows)
  3015. (defun split-window-below (&optional size)
  3016. "Split the selected window into two windows, one above the other.
  3017. The selected window is above. The newly split-off window is
  3018. below, and displays the same buffer. Return the new window.
  3019. If optional argument SIZE is omitted or nil, both windows get the
  3020. same height, or close to it. If SIZE is positive, the upper
  3021. \(selected) window gets SIZE lines. If SIZE is negative, the
  3022. lower (new) window gets -SIZE lines.
  3023. If the variable `split-window-keep-point' is non-nil, both
  3024. windows get the same value of point as the selected window.
  3025. Otherwise, the window starts are chosen so as to minimize the
  3026. amount of redisplay; this is convenient on slow terminals."
  3027. (interactive "P")
  3028. (let ((old-window (selected-window))
  3029. (old-point (window-point-1))
  3030. (size (and size (prefix-numeric-value size)))
  3031. moved-by-window-height moved new-window bottom)
  3032. (when (and size (< size 0) (< (- size) window-min-height))
  3033. ;; `split-window' would not signal an error here.
  3034. (error "Size of new window too small"))
  3035. (setq new-window (split-window nil size))
  3036. (unless split-window-keep-point
  3037. (with-current-buffer (window-buffer)
  3038. ;; Use `save-excursion' around vertical movements below
  3039. ;; (Bug#10971). Note: When the selected window's buffer has a
  3040. ;; header line, up to two lines of the buffer may not show up
  3041. ;; in the resulting configuration.
  3042. (save-excursion
  3043. (goto-char (window-start))
  3044. (setq moved (vertical-motion (window-height)))
  3045. (set-window-start new-window (point))
  3046. (when (> (point) (window-point new-window))
  3047. (set-window-point new-window (point)))
  3048. (when (= moved (window-height))
  3049. (setq moved-by-window-height t)
  3050. (vertical-motion -1))
  3051. (setq bottom (point)))
  3052. (and moved-by-window-height
  3053. (<= bottom (point))
  3054. (set-window-point-1 old-window (1- bottom)))
  3055. (and moved-by-window-height
  3056. (<= (window-start new-window) old-point)
  3057. (set-window-point new-window old-point)
  3058. (select-window new-window))))
  3059. ;; Always copy quit-restore parameter in interactive use.
  3060. (let ((quit-restore (window-parameter old-window 'quit-restore)))
  3061. (when quit-restore
  3062. (set-window-parameter new-window 'quit-restore quit-restore)))
  3063. new-window))
  3064. (defalias 'split-window-vertically 'split-window-below)
  3065. (defun split-window-right (&optional size)
  3066. "Split the selected window into two side-by-side windows.
  3067. The selected window is on the left. The newly split-off window
  3068. is on the right, and displays the same buffer. Return the new
  3069. window.
  3070. If optional argument SIZE is omitted or nil, both windows get the
  3071. same width, or close to it. If SIZE is positive, the left-hand
  3072. \(selected) window gets SIZE columns. If SIZE is negative, the
  3073. right-hand (new) window gets -SIZE columns. Here, SIZE includes
  3074. the width of the window's scroll bar; if there are no scroll
  3075. bars, it includes the width of the divider column to the window's
  3076. right, if any."
  3077. (interactive "P")
  3078. (let ((old-window (selected-window))
  3079. (size (and size (prefix-numeric-value size)))
  3080. new-window)
  3081. (when (and size (< size 0) (< (- size) window-min-width))
  3082. ;; `split-window' would not signal an error here.
  3083. (error "Size of new window too small"))
  3084. (setq new-window (split-window nil size t))
  3085. ;; Always copy quit-restore parameter in interactive use.
  3086. (let ((quit-restore (window-parameter old-window 'quit-restore)))
  3087. (when quit-restore
  3088. (set-window-parameter new-window 'quit-restore quit-restore)))
  3089. new-window))
  3090. (defalias 'split-window-horizontally 'split-window-right)
  3091. ;;; Balancing windows.
  3092. ;; The following routine uses the recycled code from an old version of
  3093. ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
  3094. ;; new `window--resize-child-windows' code does would hardly make it any shorter or
  3095. ;; more readable (FWIW we'd need three loops - one to calculate the
  3096. ;; minimum sizes per window, one to enlarge or shrink windows until the
  3097. ;; new parent-size matches, and one where we shrink the largest/enlarge
  3098. ;; the smallest window).
  3099. (defun balance-windows-2 (window horizontal)
  3100. "Subroutine of `balance-windows-1'.
  3101. WINDOW must be a vertical combination (horizontal if HORIZONTAL
  3102. is non-nil."
  3103. (let* ((first (window-child window))
  3104. (sub first)
  3105. (number-of-children 0)
  3106. (parent-size (window-new-total window))
  3107. (total-sum parent-size)
  3108. failed size sub-total sub-delta sub-amount rest)
  3109. (while sub
  3110. (setq number-of-children (1+ number-of-children))
  3111. (when (window-size-fixed-p sub horizontal)
  3112. (setq total-sum
  3113. (- total-sum (window-total-size sub horizontal)))
  3114. (set-window-new-normal sub 'ignore))
  3115. (setq sub (window-right sub)))
  3116. (setq failed t)
  3117. (while (and failed (> number-of-children 0))
  3118. (setq size (/ total-sum number-of-children))
  3119. (setq failed nil)
  3120. (setq sub first)
  3121. (while (and sub (not failed))
  3122. ;; Ignore child windows that should be ignored or are stuck.
  3123. (unless (window--resize-child-windows-skip-p sub)
  3124. (setq sub-total (window-total-size sub horizontal))
  3125. (setq sub-delta (- size sub-total))
  3126. (setq sub-amount
  3127. (window-sizable sub sub-delta horizontal))
  3128. ;; Register the new total size for this child window.
  3129. (set-window-new-total sub (+ sub-total sub-amount))
  3130. (unless (= sub-amount sub-delta)
  3131. (setq total-sum (- total-sum sub-total sub-amount))
  3132. (setq number-of-children (1- number-of-children))
  3133. ;; We failed and need a new round.
  3134. (setq failed t)
  3135. (set-window-new-normal sub 'skip)))
  3136. (setq sub (window-right sub))))
  3137. (setq rest (% total-sum number-of-children))
  3138. ;; Fix rounding by trying to enlarge non-stuck windows by one line
  3139. ;; (column) until `rest' is zero.
  3140. (setq sub first)
  3141. (while (and sub (> rest 0))
  3142. (unless (window--resize-child-windows-skip-p window)
  3143. (set-window-new-total sub 1 t)
  3144. (setq rest (1- rest)))
  3145. (setq sub (window-right sub)))
  3146. ;; Fix rounding by trying to enlarge stuck windows by one line
  3147. ;; (column) until `rest' equals zero.
  3148. (setq sub first)
  3149. (while (and sub (> rest 0))
  3150. (unless (eq (window-new-normal sub) 'ignore)
  3151. (set-window-new-total sub 1 t)
  3152. (setq rest (1- rest)))
  3153. (setq sub (window-right sub)))
  3154. (setq sub first)
  3155. (while sub
  3156. ;; Record new normal sizes.
  3157. (set-window-new-normal
  3158. sub (/ (if (eq (window-new-normal sub) 'ignore)
  3159. (window-total-size sub horizontal)
  3160. (window-new-total sub))
  3161. (float parent-size)))
  3162. ;; Recursively balance each window's child windows.
  3163. (balance-windows-1 sub horizontal)
  3164. (setq sub (window-right sub)))))
  3165. (defun balance-windows-1 (window &optional horizontal)
  3166. "Subroutine of `balance-windows'."
  3167. (if (window-child window)
  3168. (let ((sub (window-child window)))
  3169. (if (window-combined-p sub horizontal)
  3170. (balance-windows-2 window horizontal)
  3171. (let ((size (window-new-total window)))
  3172. (while sub
  3173. (set-window-new-total sub size)
  3174. (balance-windows-1 sub horizontal)
  3175. (setq sub (window-right sub))))))))
  3176. (defun balance-windows (&optional window-or-frame)
  3177. "Balance the sizes of windows of WINDOW-OR-FRAME.
  3178. WINDOW-OR-FRAME is optional and defaults to the selected frame.
  3179. If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
  3180. windows of that frame. If WINDOW-OR-FRAME denotes a window,
  3181. recursively balance the sizes of all child windows of that
  3182. window."
  3183. (interactive)
  3184. (let* ((window
  3185. (cond
  3186. ((or (not window-or-frame)
  3187. (frame-live-p window-or-frame))
  3188. (frame-root-window window-or-frame))
  3189. ((or (window-live-p window-or-frame)
  3190. (window-child window-or-frame))
  3191. window-or-frame)
  3192. (t
  3193. (error "Not a window or frame %s" window-or-frame))))
  3194. (frame (window-frame window)))
  3195. ;; Balance vertically.
  3196. (window--resize-reset (window-frame window))
  3197. (balance-windows-1 window)
  3198. (window-resize-apply frame)
  3199. ;; Balance horizontally.
  3200. (window--resize-reset (window-frame window) t)
  3201. (balance-windows-1 window t)
  3202. (window-resize-apply frame t)))
  3203. (defun window-fixed-size-p (&optional window direction)
  3204. "Return t if WINDOW cannot be resized in DIRECTION.
  3205. WINDOW defaults to the selected window. DIRECTION can be
  3206. nil (i.e. any), `height' or `width'."
  3207. (with-current-buffer (window-buffer window)
  3208. (when (and (boundp 'window-size-fixed) window-size-fixed)
  3209. (not (and direction
  3210. (member (cons direction window-size-fixed)
  3211. '((height . width) (width . height))))))))
  3212. ;;; A different solution to balance-windows.
  3213. (defvar window-area-factor 1
  3214. "Factor by which the window area should be over-estimated.
  3215. This is used by `balance-windows-area'.
  3216. Changing this globally has no effect.")
  3217. (make-variable-buffer-local 'window-area-factor)
  3218. (defun balance-windows-area-adjust (window delta horizontal)
  3219. "Wrapper around `window-resize' with error checking.
  3220. Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
  3221. ;; `window-resize' may fail if delta is too large.
  3222. (while (>= (abs delta) 1)
  3223. (condition-case nil
  3224. (progn
  3225. (window-resize window delta horizontal)
  3226. (setq delta 0))
  3227. (error
  3228. ;;(message "adjust: %s" (error-message-string err))
  3229. (setq delta (/ delta 2))))))
  3230. (defun balance-windows-area ()
  3231. "Make all visible windows the same area (approximately).
  3232. See also `window-area-factor' to change the relative size of
  3233. specific buffers."
  3234. (interactive)
  3235. (let* ((unchanged 0) (carry 0) (round 0)
  3236. ;; Remove fixed-size windows.
  3237. (wins (delq nil (mapcar (lambda (win)
  3238. (if (not (window-fixed-size-p win)) win))
  3239. (window-list nil 'nomini))))
  3240. (changelog nil)
  3241. next)
  3242. ;; Resizing a window changes the size of surrounding windows in complex
  3243. ;; ways, so it's difficult to balance them all. The introduction of
  3244. ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
  3245. ;; very difficult to do. `balance-window' above takes an off-line
  3246. ;; approach: get the whole window tree, then balance it, then try to
  3247. ;; adjust the windows so they fit the result.
  3248. ;; Here, instead, we take a "local optimization" approach, where we just
  3249. ;; go through all the windows several times until nothing needs to be
  3250. ;; changed. The main problem with this approach is that it's difficult
  3251. ;; to make sure it terminates, so we use some heuristic to try and break
  3252. ;; off infinite loops.
  3253. ;; After a round without any change, we allow a second, to give a chance
  3254. ;; to the carry to propagate a minor imbalance from the end back to
  3255. ;; the beginning.
  3256. (while (< unchanged 2)
  3257. ;; (message "New round")
  3258. (setq unchanged (1+ unchanged) round (1+ round))
  3259. (dolist (win wins)
  3260. (setq next win)
  3261. (while (progn (setq next (next-window next))
  3262. (window-fixed-size-p next)))
  3263. ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
  3264. (let* ((horiz
  3265. (< (car (window-edges win)) (car (window-edges next))))
  3266. (areadiff (/ (- (* (window-height next) (window-width next)
  3267. (buffer-local-value 'window-area-factor
  3268. (window-buffer next)))
  3269. (* (window-height win) (window-width win)
  3270. (buffer-local-value 'window-area-factor
  3271. (window-buffer win))))
  3272. (max (buffer-local-value 'window-area-factor
  3273. (window-buffer win))
  3274. (buffer-local-value 'window-area-factor
  3275. (window-buffer next)))))
  3276. (edgesize (if horiz
  3277. (+ (window-height win) (window-height next))
  3278. (+ (window-width win) (window-width next))))
  3279. (diff (/ areadiff edgesize)))
  3280. (when (zerop diff)
  3281. ;; Maybe diff is actually closer to 1 than to 0.
  3282. (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
  3283. (when (and (zerop diff) (not (zerop areadiff)))
  3284. (setq diff (/ (+ areadiff carry) edgesize))
  3285. ;; Change things smoothly.
  3286. (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
  3287. (if (zerop diff)
  3288. ;; Make sure negligible differences don't accumulate to
  3289. ;; become significant.
  3290. (setq carry (+ carry areadiff))
  3291. ;; This used `adjust-window-trailing-edge' before and uses
  3292. ;; `window-resize' now. Error wrapping is still needed.
  3293. (balance-windows-area-adjust win diff horiz)
  3294. ;; (sit-for 0.5)
  3295. (let ((change (cons win (window-edges win))))
  3296. ;; If the same change has been seen already for this window,
  3297. ;; we're most likely in an endless loop, so don't count it as
  3298. ;; a change.
  3299. (unless (member change changelog)
  3300. (push change changelog)
  3301. (setq unchanged 0 carry 0)))))))
  3302. ;; We've now basically balanced all the windows.
  3303. ;; But there may be some minor off-by-one imbalance left over,
  3304. ;; so let's do some fine tuning.
  3305. ;; (bw-finetune wins)
  3306. ;; (message "Done in %d rounds" round)
  3307. ))
  3308. ;;; Window states, how to get them and how to put them in a window.
  3309. (defun window--state-get-1 (window &optional writable)
  3310. "Helper function for `window-state-get'."
  3311. (let* ((type
  3312. (cond
  3313. ((window-top-child window) 'vc)
  3314. ((window-left-child window) 'hc)
  3315. (t 'leaf)))
  3316. (buffer (window-buffer window))
  3317. (selected (eq window (selected-window)))
  3318. (head
  3319. `(,type
  3320. ,@(unless (window-next-sibling window) `((last . t)))
  3321. (total-height . ,(window-total-size window))
  3322. (total-width . ,(window-total-size window t))
  3323. (normal-height . ,(window-normal-size window))
  3324. (normal-width . ,(window-normal-size window t))
  3325. (combination-limit . ,(window-combination-limit window))
  3326. ,@(let ((parameters (window-parameters window))
  3327. list)
  3328. ;; Make copies of those window parameters whose
  3329. ;; persistence property is `writable' if WRITABLE is
  3330. ;; non-nil and non-nil if WRITABLE is nil.
  3331. (dolist (par parameters)
  3332. (let ((pers (cdr (assq (car par)
  3333. window-persistent-parameters))))
  3334. (when (and pers (or (not writable) (eq pers 'writable)))
  3335. (setq list (cons (cons (car par) (cdr par)) list)))))
  3336. ;; Add `clone-of' parameter if necessary.
  3337. (let ((pers (cdr (assq 'clone-of
  3338. window-persistent-parameters))))
  3339. (when (and pers (or (not writable) (eq pers 'writable))
  3340. (not (assq 'clone-of list)))
  3341. (setq list (cons (cons 'clone-of window) list))))
  3342. (when list
  3343. `((parameters . ,list))))
  3344. ,@(when buffer
  3345. ;; All buffer related things go in here.
  3346. (let ((point (window-point-1 window))
  3347. (start (window-start window)))
  3348. `((buffer
  3349. ,(buffer-name buffer)
  3350. (selected . ,selected)
  3351. (hscroll . ,(window-hscroll window))
  3352. (fringes . ,(window-fringes window))
  3353. (margins . ,(window-margins window))
  3354. (scroll-bars . ,(window-scroll-bars window))
  3355. (vscroll . ,(window-vscroll window))
  3356. (dedicated . ,(window-dedicated-p window))
  3357. (point . ,(if writable point (copy-marker point)))
  3358. (start . ,(if writable start (copy-marker start)))))))))
  3359. (tail
  3360. (when (memq type '(vc hc))
  3361. (let (list)
  3362. (setq window (window-child window))
  3363. (while window
  3364. (setq list (cons (window--state-get-1 window writable) list))
  3365. (setq window (window-right window)))
  3366. (nreverse list)))))
  3367. (append head tail)))
  3368. (defun window-state-get (&optional window writable)
  3369. "Return state of WINDOW as a Lisp object.
  3370. WINDOW can be any window and defaults to the root window of the
  3371. selected frame.
  3372. Optional argument WRITABLE non-nil means do not use markers for
  3373. sampling `window-point' and `window-start'. Together, WRITABLE
  3374. and the variable `window-persistent-parameters' specify which
  3375. window parameters are saved by this function. WRITABLE should be
  3376. non-nil when the return value shall be written to a file and read
  3377. back in another session. Otherwise, an application may run into
  3378. an `invalid-read-syntax' error while attempting to read back the
  3379. value from file.
  3380. The return value can be used as argument for `window-state-put'
  3381. to put the state recorded here into an arbitrary window. The
  3382. value can be also stored on disk and read back in a new session."
  3383. (setq window
  3384. (if window
  3385. (if (window-valid-p window)
  3386. window
  3387. (error "%s is not a live or internal window" window))
  3388. (frame-root-window)))
  3389. ;; The return value is a cons whose car specifies some constraints on
  3390. ;; the size of WINDOW. The cdr lists the states of the child windows
  3391. ;; of WINDOW.
  3392. (cons
  3393. ;; Frame related things would go into a function, say `frame-state',
  3394. ;; calling `window-state-get' to insert the frame's root window.
  3395. `((min-height . ,(window-min-size window))
  3396. (min-width . ,(window-min-size window t))
  3397. (min-height-ignore . ,(window-min-size window nil t))
  3398. (min-width-ignore . ,(window-min-size window t t))
  3399. (min-height-safe . ,(window-min-size window nil 'safe))
  3400. (min-width-safe . ,(window-min-size window t 'safe)))
  3401. (window--state-get-1 window writable)))
  3402. (defvar window-state-put-list nil
  3403. "Helper variable for `window-state-put'.")
  3404. (defun window--state-put-1 (state &optional window ignore totals)
  3405. "Helper function for `window-state-put'."
  3406. (let ((type (car state)))
  3407. (setq state (cdr state))
  3408. (cond
  3409. ((eq type 'leaf)
  3410. ;; For a leaf window just add unprocessed entries to
  3411. ;; `window-state-put-list'.
  3412. (push (cons window state) window-state-put-list))
  3413. ((memq type '(vc hc))
  3414. (let* ((horizontal (eq type 'hc))
  3415. (total (window-total-size window horizontal))
  3416. (first t)
  3417. size new)
  3418. (dolist (item state)
  3419. ;; Find the next child window. WINDOW always points to the
  3420. ;; real window that we want to fill with what we find here.
  3421. (when (memq (car item) '(leaf vc hc))
  3422. (if (assq 'last item)
  3423. ;; The last child window. Below `window--state-put-1'
  3424. ;; will put into it whatever ITEM has in store.
  3425. (setq new nil)
  3426. ;; Not the last child window, prepare for splitting
  3427. ;; WINDOW. SIZE is the new (and final) size of the old
  3428. ;; window.
  3429. (setq size
  3430. (if totals
  3431. ;; Use total size.
  3432. (cdr (assq (if horizontal 'total-width 'total-height) item))
  3433. ;; Use normalized size and round.
  3434. (round (* total
  3435. (cdr (assq
  3436. (if horizontal 'normal-width 'normal-height)
  3437. item))))))
  3438. ;; Use safe sizes, we try to resize later.
  3439. (setq size (max size (if horizontal
  3440. window-safe-min-height
  3441. window-safe-min-width)))
  3442. (if (window-sizable-p window (- size) horizontal 'safe)
  3443. (let* ((window-combination-limit
  3444. (assq 'combination-limit item)))
  3445. ;; We must inherit the combination limit, otherwise
  3446. ;; we might mess up handling of atomic and side
  3447. ;; window.
  3448. (setq new (split-window window size horizontal)))
  3449. ;; Give up if we can't resize window down to safe sizes.
  3450. (error "Cannot resize window %s" window))
  3451. (when first
  3452. (setq first nil)
  3453. ;; When creating the first child window add for parent
  3454. ;; unprocessed entries to `window-state-put-list'.
  3455. (setq window-state-put-list
  3456. (cons (cons (window-parent window) state)
  3457. window-state-put-list))))
  3458. ;; Now process the current window (either the one we've just
  3459. ;; split or the last child of its parent).
  3460. (window--state-put-1 item window ignore totals)
  3461. ;; Continue with the last window split off.
  3462. (setq window new))))))))
  3463. (defun window--state-put-2 (ignore)
  3464. "Helper function for `window-state-put'."
  3465. (dolist (item window-state-put-list)
  3466. (let ((window (car item))
  3467. (combination-limit (cdr (assq 'combination-limit item)))
  3468. (parameters (cdr (assq 'parameters item)))
  3469. (state (cdr (assq 'buffer item))))
  3470. (when combination-limit
  3471. (set-window-combination-limit window combination-limit))
  3472. ;; Reset window's parameters and assign saved ones (we might want
  3473. ;; a `remove-window-parameters' function here).
  3474. (dolist (parameter (window-parameters window))
  3475. (set-window-parameter window (car parameter) nil))
  3476. (when parameters
  3477. (dolist (parameter parameters)
  3478. (set-window-parameter window (car parameter) (cdr parameter))))
  3479. ;; Process buffer related state.
  3480. (when state
  3481. ;; We don't want to raise an error here so we create a buffer if
  3482. ;; there's none.
  3483. (set-window-buffer window (get-buffer-create (car state)))
  3484. (with-current-buffer (window-buffer window)
  3485. (set-window-hscroll window (cdr (assq 'hscroll state)))
  3486. (apply 'set-window-fringes
  3487. (cons window (cdr (assq 'fringes state))))
  3488. (let ((margins (cdr (assq 'margins state))))
  3489. (set-window-margins window (car margins) (cdr margins)))
  3490. (let ((scroll-bars (cdr (assq 'scroll-bars state))))
  3491. (set-window-scroll-bars
  3492. window (car scroll-bars) (nth 2 scroll-bars) (nth 3 scroll-bars)))
  3493. (set-window-vscroll window (cdr (assq 'vscroll state)))
  3494. ;; Adjust vertically.
  3495. (if (memq window-size-fixed '(t height))
  3496. ;; A fixed height window, try to restore the original size.
  3497. (let ((delta (- (cdr (assq 'total-height item))
  3498. (window-total-height window)))
  3499. window-size-fixed)
  3500. (when (window--resizable-p window delta)
  3501. (window-resize window delta)))
  3502. ;; Else check whether the window is not high enough.
  3503. (let* ((min-size (window-min-size window nil ignore))
  3504. (delta (- min-size (window-total-size window))))
  3505. (when (and (> delta 0)
  3506. (window--resizable-p window delta nil ignore))
  3507. (window-resize window delta nil ignore))))
  3508. ;; Adjust horizontally.
  3509. (if (memq window-size-fixed '(t width))
  3510. ;; A fixed width window, try to restore the original size.
  3511. (let ((delta (- (cdr (assq 'total-width item))
  3512. (window-total-width window)))
  3513. window-size-fixed)
  3514. (when (window--resizable-p window delta)
  3515. (window-resize window delta)))
  3516. ;; Else check whether the window is not wide enough.
  3517. (let* ((min-size (window-min-size window t ignore))
  3518. (delta (- min-size (window-total-size window t))))
  3519. (when (and (> delta 0)
  3520. (window--resizable-p window delta t ignore))
  3521. (window-resize window delta t ignore))))
  3522. ;; Set dedicated status.
  3523. (set-window-dedicated-p window (cdr (assq 'dedicated state)))
  3524. ;; Install positions (maybe we should do this after all windows
  3525. ;; have been created and sized).
  3526. (ignore-errors
  3527. (set-window-start window (cdr (assq 'start state)))
  3528. (set-window-point window (cdr (assq 'point state))))
  3529. ;; Select window if it's the selected one.
  3530. (when (cdr (assq 'selected state))
  3531. (select-window window)))))))
  3532. (defun window-state-put (state &optional window ignore)
  3533. "Put window state STATE into WINDOW.
  3534. STATE should be the state of a window returned by an earlier
  3535. invocation of `window-state-get'. Optional argument WINDOW must
  3536. specify a live window and defaults to the selected one.
  3537. Optional argument IGNORE non-nil means ignore minimum window
  3538. sizes and fixed size restrictions. IGNORE equal `safe' means
  3539. windows can get as small as `window-safe-min-height' and
  3540. `window-safe-min-width'."
  3541. (setq window (window-normalize-window window t))
  3542. (let* ((frame (window-frame window))
  3543. (head (car state))
  3544. ;; We check here (1) whether the total sizes of root window of
  3545. ;; STATE and that of WINDOW are equal so we can avoid
  3546. ;; calculating new sizes, and (2) if we do have to resize
  3547. ;; whether we can do so without violating size restrictions.
  3548. (totals
  3549. (and (= (window-total-size window)
  3550. (cdr (assq 'total-height state)))
  3551. (= (window-total-size window t)
  3552. (cdr (assq 'total-width state)))))
  3553. (min-height (cdr (assq 'min-height head)))
  3554. (min-width (cdr (assq 'min-width head))))
  3555. (if (and (not totals)
  3556. (or (> min-height (window-total-size window))
  3557. (> min-width (window-total-size window t)))
  3558. (or (not ignore)
  3559. (and (setq min-height
  3560. (cdr (assq 'min-height-ignore head)))
  3561. (setq min-width
  3562. (cdr (assq 'min-width-ignore head)))
  3563. (or (> min-height (window-total-size window))
  3564. (> min-width (window-total-size window t)))
  3565. (or (not (eq ignore 'safe))
  3566. (and (setq min-height
  3567. (cdr (assq 'min-height-safe head)))
  3568. (setq min-width
  3569. (cdr (assq 'min-width-safe head)))
  3570. (or (> min-height
  3571. (window-total-size window))
  3572. (> min-width
  3573. (window-total-size window t))))))))
  3574. ;; The check above might not catch all errors due to rounding
  3575. ;; issues - so IGNORE equal 'safe might not always produce the
  3576. ;; minimum possible state. But such configurations hardly make
  3577. ;; sense anyway.
  3578. (error "Window %s too small to accommodate state" window)
  3579. (setq state (cdr state))
  3580. (setq window-state-put-list nil)
  3581. ;; Work on the windows of a temporary buffer to make sure that
  3582. ;; splitting proceeds regardless of any buffer local values of
  3583. ;; `window-size-fixed'. Release that buffer after the buffers of
  3584. ;; all live windows have been set by `window--state-put-2'.
  3585. (with-temp-buffer
  3586. (set-window-buffer window (current-buffer))
  3587. (window--state-put-1 state window nil totals)
  3588. (window--state-put-2 ignore))
  3589. (window--check frame))))
  3590. (defun display-buffer-record-window (type window buffer)
  3591. "Record information for window used by `display-buffer'.
  3592. TYPE specifies the type of the calling operation and must be one
  3593. of the symbols 'reuse (when WINDOW existed already and was
  3594. reused for displaying BUFFER), 'window (when WINDOW was created
  3595. on an already existing frame), or 'frame (when WINDOW was
  3596. created on a new frame). WINDOW is the window used for or created
  3597. by the `display-buffer' routines. BUFFER is the buffer that
  3598. shall be displayed.
  3599. This function installs or updates the quit-restore parameter of
  3600. WINDOW. The quit-restore parameter is a list of four elements:
  3601. The first element is one of the symbols 'window, 'frame, 'same or
  3602. 'other. The second element is either one of the symbols 'window
  3603. or 'frame or a list whose elements are the buffer previously
  3604. shown in the window, that buffer's window start and window point,
  3605. and the window's height. The third element is the window
  3606. selected at the time the parameter was created. The fourth
  3607. element is BUFFER."
  3608. (cond
  3609. ((eq type 'reuse)
  3610. (if (eq (window-buffer window) buffer)
  3611. ;; WINDOW shows BUFFER already.
  3612. (when (consp (window-parameter window 'quit-restore))
  3613. ;; If WINDOW has a quit-restore parameter, reset its car.
  3614. (setcar (window-parameter window 'quit-restore) 'same))
  3615. ;; WINDOW shows another buffer.
  3616. (set-window-parameter
  3617. window 'quit-restore
  3618. (list 'other
  3619. ;; A quadruple of WINDOW's buffer, start, point and height.
  3620. (list (window-buffer window) (window-start window)
  3621. (window-point-1 window) (window-total-size window))
  3622. (selected-window) buffer))))
  3623. ((eq type 'window)
  3624. ;; WINDOW has been created on an existing frame.
  3625. (set-window-parameter
  3626. window 'quit-restore
  3627. (list 'window 'window (selected-window) buffer)))
  3628. ((eq type 'frame)
  3629. ;; WINDOW has been created on a new frame.
  3630. (set-window-parameter
  3631. window 'quit-restore
  3632. (list 'frame 'frame (selected-window) buffer)))))
  3633. (defcustom display-buffer-function nil
  3634. "If non-nil, function to call to handle `display-buffer'.
  3635. It will receive two args, the buffer and a flag which if non-nil
  3636. means that the currently selected window is not acceptable. It
  3637. should choose or create a window, display the specified buffer in
  3638. it, and return the window.
  3639. The specified function should call `display-buffer-record-window'
  3640. with corresponding arguments to set up the quit-restore parameter
  3641. of the window used."
  3642. :type '(choice
  3643. (const nil)
  3644. (function :tag "function"))
  3645. :group 'windows)
  3646. (defcustom pop-up-frame-alist nil
  3647. "Alist of parameters for automatically generated new frames.
  3648. You can set this in your init file; for example,
  3649. (setq pop-up-frame-alist '((width . 80) (height . 20)))
  3650. If non-nil, the value you specify here is used by the default
  3651. `pop-up-frame-function' for the creation of new frames.
  3652. Since `pop-up-frame-function' is used by `display-buffer' for
  3653. making new frames, any value specified here by default affects
  3654. the automatic generation of new frames via `display-buffer' and
  3655. all functions based on it. The behavior of `make-frame' is not
  3656. affected by this variable."
  3657. :type '(repeat (cons :format "%v"
  3658. (symbol :tag "Parameter")
  3659. (sexp :tag "Value")))
  3660. :group 'frames)
  3661. (defcustom pop-up-frame-function
  3662. (lambda () (make-frame pop-up-frame-alist))
  3663. "Function used by `display-buffer' for creating a new frame.
  3664. This function is called with no arguments and should return a new
  3665. frame. The default value calls `make-frame' with the argument
  3666. `pop-up-frame-alist'."
  3667. :type 'function
  3668. :group 'frames)
  3669. (defcustom special-display-buffer-names nil
  3670. "List of names of buffers that should be displayed specially.
  3671. Displaying a buffer with `display-buffer' or `pop-to-buffer', if
  3672. its name is in this list, displays the buffer in a way specified
  3673. by `special-display-function'. `special-display-popup-frame'
  3674. \(the default for `special-display-function') usually displays
  3675. the buffer in a separate frame made with the parameters specified
  3676. by `special-display-frame-alist'. If `special-display-function'
  3677. has been set to some other function, that function is called with
  3678. the buffer as first, and nil as second argument.
  3679. Alternatively, an element of this list can be specified as
  3680. \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
  3681. name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
  3682. `special-display-popup-frame' will interpret such pairs as frame
  3683. parameters when it creates a special frame, overriding the
  3684. corresponding values from `special-display-frame-alist'.
  3685. As a special case, if FRAME-PARAMETERS contains (same-window . t)
  3686. `special-display-popup-frame' displays that buffer in the
  3687. selected window. If FRAME-PARAMETERS contains (same-frame . t),
  3688. it displays that buffer in a window on the selected frame.
  3689. If `special-display-function' specifies some other function than
  3690. `special-display-popup-frame', that function is called with the
  3691. buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
  3692. argument.
  3693. Finally, an element of this list can be also specified as
  3694. \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
  3695. `special-display-popup-frame' will call FUNCTION with the buffer
  3696. named BUFFER-NAME as first argument, and OTHER-ARGS as the
  3697. second.
  3698. Any alternative function specified here is responsible for
  3699. setting up the quit-restore parameter of the window used.
  3700. If this variable appears \"not to work\", because you added a
  3701. name to it but the corresponding buffer is displayed in the
  3702. selected window, look at the values of `same-window-buffer-names'
  3703. and `same-window-regexps'. Those variables take precedence over
  3704. this one.
  3705. See also `special-display-regexps'."
  3706. :type '(repeat
  3707. (choice :tag "Buffer"
  3708. :value ""
  3709. (string :format "%v")
  3710. (cons :tag "With parameters"
  3711. :format "%v"
  3712. :value ("" . nil)
  3713. (string :format "%v")
  3714. (repeat :tag "Parameters"
  3715. (cons :format "%v"
  3716. (symbol :tag "Parameter")
  3717. (sexp :tag "Value"))))
  3718. (list :tag "With function"
  3719. :format "%v"
  3720. :value ("" . nil)
  3721. (string :format "%v")
  3722. (function :tag "Function")
  3723. (repeat :tag "Arguments" (sexp)))))
  3724. :group 'windows
  3725. :group 'frames)
  3726. ;;;###autoload
  3727. (put 'special-display-buffer-names 'risky-local-variable t)
  3728. (defcustom special-display-regexps nil
  3729. "List of regexps saying which buffers should be displayed specially.
  3730. Displaying a buffer with `display-buffer' or `pop-to-buffer', if
  3731. any regexp in this list matches its name, displays it specially
  3732. using `special-display-function'. `special-display-popup-frame'
  3733. \(the default for `special-display-function') usually displays
  3734. the buffer in a separate frame made with the parameters specified
  3735. by `special-display-frame-alist'. If `special-display-function'
  3736. has been set to some other function, that function is called with
  3737. the buffer as first, and nil as second argument.
  3738. Alternatively, an element of this list can be specified as
  3739. \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
  3740. FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
  3741. `special-display-popup-frame' will then interpret these pairs as
  3742. frame parameters when creating a special frame for a buffer whose
  3743. name matches REGEXP, overriding the corresponding values from
  3744. `special-display-frame-alist'.
  3745. As a special case, if FRAME-PARAMETERS contains (same-window . t)
  3746. `special-display-popup-frame' displays buffers matching REGEXP in
  3747. the selected window. (same-frame . t) in FRAME-PARAMETERS means
  3748. to display such buffers in a window on the selected frame.
  3749. If `special-display-function' specifies some other function than
  3750. `special-display-popup-frame', that function is called with the
  3751. buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
  3752. as second argument.
  3753. Finally, an element of this list can be also specified as
  3754. \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
  3755. will then call FUNCTION with the buffer whose name matched
  3756. REGEXP as first, and OTHER-ARGS as second argument.
  3757. Any alternative function specified here is responsible for
  3758. setting up the quit-restore parameter of the window used.
  3759. If this variable appears \"not to work\", because you added a
  3760. name to it but the corresponding buffer is displayed in the
  3761. selected window, look at the values of `same-window-buffer-names'
  3762. and `same-window-regexps'. Those variables take precedence over
  3763. this one.
  3764. See also `special-display-buffer-names'."
  3765. :type '(repeat
  3766. (choice :tag "Buffer"
  3767. :value ""
  3768. (regexp :format "%v")
  3769. (cons :tag "With parameters"
  3770. :format "%v"
  3771. :value ("" . nil)
  3772. (regexp :format "%v")
  3773. (repeat :tag "Parameters"
  3774. (cons :format "%v"
  3775. (symbol :tag "Parameter")
  3776. (sexp :tag "Value"))))
  3777. (list :tag "With function"
  3778. :format "%v"
  3779. :value ("" . nil)
  3780. (regexp :format "%v")
  3781. (function :tag "Function")
  3782. (repeat :tag "Arguments" (sexp)))))
  3783. :group 'windows
  3784. :group 'frames)
  3785. (defun special-display-p (buffer-name)
  3786. "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
  3787. More precisely, return t if `special-display-buffer-names' or
  3788. `special-display-regexps' contain a string entry equaling or
  3789. matching BUFFER-NAME. If `special-display-buffer-names' or
  3790. `special-display-regexps' contain a list entry whose car equals
  3791. or matches BUFFER-NAME, the return value is the cdr of that
  3792. entry."
  3793. (let (tmp)
  3794. (cond
  3795. ((not (stringp buffer-name)))
  3796. ((member buffer-name special-display-buffer-names)
  3797. t)
  3798. ((setq tmp (assoc buffer-name special-display-buffer-names))
  3799. (cdr tmp))
  3800. ((catch 'found
  3801. (dolist (regexp special-display-regexps)
  3802. (cond
  3803. ((stringp regexp)
  3804. (when (string-match-p regexp buffer-name)
  3805. (throw 'found t)))
  3806. ((and (consp regexp) (stringp (car regexp))
  3807. (string-match-p (car regexp) buffer-name))
  3808. (throw 'found (cdr regexp))))))))))
  3809. (defcustom special-display-frame-alist
  3810. '((height . 14) (width . 80) (unsplittable . t))
  3811. "Alist of parameters for special frames.
  3812. Special frames are used for buffers whose names are listed in
  3813. `special-display-buffer-names' and for buffers whose names match
  3814. one of the regular expressions in `special-display-regexps'.
  3815. This variable can be set in your init file, like this:
  3816. (setq special-display-frame-alist '((width . 80) (height . 20)))
  3817. These supersede the values given in `default-frame-alist'."
  3818. :type '(repeat (cons :format "%v"
  3819. (symbol :tag "Parameter")
  3820. (sexp :tag "Value")))
  3821. :group 'frames)
  3822. (defun special-display-popup-frame (buffer &optional args)
  3823. "Display BUFFER and return the window chosen.
  3824. If BUFFER is already displayed in a visible or iconified frame,
  3825. raise that frame. Otherwise, display BUFFER in a new frame.
  3826. Optional argument ARGS is a list specifying additional
  3827. information.
  3828. If ARGS is an alist, use it as a list of frame parameters. If
  3829. these parameters contain (same-window . t), display BUFFER in
  3830. the selected window. If they contain (same-frame . t), display
  3831. BUFFER in a window of the selected frame.
  3832. If ARGS is a list whose car is a symbol, use (car ARGS) as a
  3833. function to do the work. Pass it BUFFER as first argument,
  3834. and (cdr ARGS) as second."
  3835. (if (and args (symbolp (car args)))
  3836. (apply (car args) buffer (cdr args))
  3837. (let ((window (get-buffer-window buffer 0)))
  3838. (or
  3839. ;; If we have a window already, make it visible.
  3840. (when window
  3841. (let ((frame (window-frame window)))
  3842. (make-frame-visible frame)
  3843. (raise-frame frame)
  3844. (display-buffer-record-window 'reuse window buffer)
  3845. window))
  3846. ;; Reuse the current window if the user requested it.
  3847. (when (cdr (assq 'same-window args))
  3848. (condition-case nil
  3849. (progn (switch-to-buffer buffer nil t) (selected-window))
  3850. (error nil)))
  3851. ;; Stay on the same frame if requested.
  3852. (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
  3853. (let* ((pop-up-windows t)
  3854. pop-up-frames
  3855. special-display-buffer-names special-display-regexps)
  3856. (display-buffer buffer)))
  3857. ;; If no window yet, make one in a new frame.
  3858. (let* ((frame
  3859. (with-current-buffer buffer
  3860. (make-frame (append args special-display-frame-alist))))
  3861. (window (frame-selected-window frame)))
  3862. (display-buffer-record-window 'frame window buffer)
  3863. ;; FIXME: Use window--display-buffer-2?
  3864. (set-window-buffer window buffer)
  3865. ;; Reset list of WINDOW's previous buffers to nil.
  3866. (set-window-prev-buffers window nil)
  3867. (set-window-dedicated-p window t)
  3868. window)))))
  3869. (defcustom special-display-function 'special-display-popup-frame
  3870. "Function to call for displaying special buffers.
  3871. This function is called with two arguments - the buffer and,
  3872. optionally, a list - and should return a window displaying that
  3873. buffer. The default value usually makes a separate frame for the
  3874. buffer using `special-display-frame-alist' to specify the frame
  3875. parameters. See the definition of `special-display-popup-frame'
  3876. for how to specify such a function.
  3877. A buffer is special when its name is either listed in
  3878. `special-display-buffer-names' or matches a regexp in
  3879. `special-display-regexps'.
  3880. The specified function should call `display-buffer-record-window'
  3881. with corresponding arguments to set up the quit-restore parameter
  3882. of the window used."
  3883. :type 'function
  3884. :group 'frames)
  3885. (defcustom same-window-buffer-names nil
  3886. "List of names of buffers that should appear in the \"same\" window.
  3887. `display-buffer' and `pop-to-buffer' show a buffer whose name is
  3888. on this list in the selected rather than some other window.
  3889. An element of this list can be a cons cell instead of just a
  3890. string. In that case, the cell's car must be a string specifying
  3891. the buffer name. This is for compatibility with
  3892. `special-display-buffer-names'; the cdr of the cons cell is
  3893. ignored.
  3894. See also `same-window-regexps'."
  3895. :type '(repeat (string :format "%v"))
  3896. :group 'windows)
  3897. (defcustom same-window-regexps nil
  3898. "List of regexps saying which buffers should appear in the \"same\" window.
  3899. `display-buffer' and `pop-to-buffer' show a buffer whose name
  3900. matches a regexp on this list in the selected rather than some
  3901. other window.
  3902. An element of this list can be a cons cell instead of just a
  3903. string. In that case, the cell's car must be a regexp matching
  3904. the buffer name. This is for compatibility with
  3905. `special-display-regexps'; the cdr of the cons cell is ignored.
  3906. See also `same-window-buffer-names'."
  3907. :type '(repeat (regexp :format "%v"))
  3908. :group 'windows)
  3909. (defun same-window-p (buffer-name)
  3910. "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
  3911. This function returns non-nil if `display-buffer' or
  3912. `pop-to-buffer' would show a buffer named BUFFER-NAME in the
  3913. selected rather than (as usual) some other window. See
  3914. `same-window-buffer-names' and `same-window-regexps'."
  3915. (cond
  3916. ((not (stringp buffer-name)))
  3917. ;; The elements of `same-window-buffer-names' can be buffer
  3918. ;; names or cons cells whose cars are buffer names.
  3919. ((member buffer-name same-window-buffer-names))
  3920. ((assoc buffer-name same-window-buffer-names))
  3921. ((catch 'found
  3922. (dolist (regexp same-window-regexps)
  3923. ;; The elements of `same-window-regexps' can be regexps
  3924. ;; or cons cells whose cars are regexps.
  3925. (when (or (and (stringp regexp)
  3926. (string-match-p regexp buffer-name))
  3927. (and (consp regexp) (stringp (car regexp))
  3928. (string-match-p (car regexp) buffer-name)))
  3929. (throw 'found t)))))))
  3930. (defcustom pop-up-frames nil
  3931. "Whether `display-buffer' should make a separate frame.
  3932. If nil, never make a separate frame.
  3933. If the value is `graphic-only', make a separate frame
  3934. on graphic displays only.
  3935. Any other non-nil value means always make a separate frame."
  3936. :type '(choice
  3937. (const :tag "Never" nil)
  3938. (const :tag "On graphic displays only" graphic-only)
  3939. (const :tag "Always" t))
  3940. :group 'windows)
  3941. (defcustom display-buffer-reuse-frames nil
  3942. "Non-nil means `display-buffer' should reuse frames.
  3943. If the buffer in question is already displayed in a frame, raise
  3944. that frame."
  3945. :type 'boolean
  3946. :version "21.1"
  3947. :group 'windows)
  3948. (defcustom pop-up-windows t
  3949. "Non-nil means `display-buffer' should make a new window."
  3950. :type 'boolean
  3951. :group 'windows)
  3952. (defcustom split-window-preferred-function 'split-window-sensibly
  3953. "Function called by `display-buffer' routines to split a window.
  3954. This function is called with a window as single argument and is
  3955. supposed to split that window and return the new window. If the
  3956. window can (or shall) not be split, it is supposed to return nil.
  3957. The default is to call the function `split-window-sensibly' which
  3958. tries to split the window in a way which seems most suitable.
  3959. You can customize the options `split-height-threshold' and/or
  3960. `split-width-threshold' in order to have `split-window-sensibly'
  3961. prefer either vertical or horizontal splitting.
  3962. If you set this to any other function, bear in mind that the
  3963. `display-buffer' routines may call this function two times. The
  3964. argument of the first call is the largest window on its frame.
  3965. If that call fails to return a live window, the function is
  3966. called again with the least recently used window as argument. If
  3967. that call fails too, `display-buffer' will use an existing window
  3968. to display its buffer.
  3969. The window selected at the time `display-buffer' was invoked is
  3970. still selected when this function is called. Hence you can
  3971. compare the window argument with the value of `selected-window'
  3972. if you intend to split the selected window instead or if you do
  3973. not want to split the selected window."
  3974. :type 'function
  3975. :version "23.1"
  3976. :group 'windows)
  3977. (defcustom split-height-threshold 80
  3978. "Minimum height for splitting windows sensibly.
  3979. If this is an integer, `split-window-sensibly' may split a window
  3980. vertically only if it has at least this many lines. If this is
  3981. nil, `split-window-sensibly' is not allowed to split a window
  3982. vertically. If, however, a window is the only window on its
  3983. frame, `split-window-sensibly' may split it vertically
  3984. disregarding the value of this variable."
  3985. :type '(choice (const nil) (integer :tag "lines"))
  3986. :version "23.1"
  3987. :group 'windows)
  3988. (defcustom split-width-threshold 160
  3989. "Minimum width for splitting windows sensibly.
  3990. If this is an integer, `split-window-sensibly' may split a window
  3991. horizontally only if it has at least this many columns. If this
  3992. is nil, `split-window-sensibly' is not allowed to split a window
  3993. horizontally."
  3994. :type '(choice (const nil) (integer :tag "columns"))
  3995. :version "23.1"
  3996. :group 'windows)
  3997. (defun window-splittable-p (window &optional horizontal)
  3998. "Return non-nil if `split-window-sensibly' may split WINDOW.
  3999. Optional argument HORIZONTAL nil or omitted means check whether
  4000. `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
  4001. non-nil means check whether WINDOW may be split horizontally.
  4002. WINDOW may be split vertically when the following conditions
  4003. hold:
  4004. - `window-size-fixed' is either nil or equals `width' for the
  4005. buffer of WINDOW.
  4006. - `split-height-threshold' is an integer and WINDOW is at least as
  4007. high as `split-height-threshold'.
  4008. - When WINDOW is split evenly, the emanating windows are at least
  4009. `window-min-height' lines tall and can accommodate at least one
  4010. line plus - if WINDOW has one - a mode line.
  4011. WINDOW may be split horizontally when the following conditions
  4012. hold:
  4013. - `window-size-fixed' is either nil or equals `height' for the
  4014. buffer of WINDOW.
  4015. - `split-width-threshold' is an integer and WINDOW is at least as
  4016. wide as `split-width-threshold'.
  4017. - When WINDOW is split evenly, the emanating windows are at least
  4018. `window-min-width' or two (whichever is larger) columns wide."
  4019. (when (window-live-p window)
  4020. (with-current-buffer (window-buffer window)
  4021. (if horizontal
  4022. ;; A window can be split horizontally when its width is not
  4023. ;; fixed, it is at least `split-width-threshold' columns wide
  4024. ;; and at least twice as wide as `window-min-width' and 2 (the
  4025. ;; latter value is hardcoded).
  4026. (and (memq window-size-fixed '(nil height))
  4027. ;; Testing `window-full-width-p' here hardly makes any
  4028. ;; sense nowadays. This can be done more intuitively by
  4029. ;; setting up `split-width-threshold' appropriately.
  4030. (numberp split-width-threshold)
  4031. (>= (window-width window)
  4032. (max split-width-threshold
  4033. (* 2 (max window-min-width 2)))))
  4034. ;; A window can be split vertically when its height is not
  4035. ;; fixed, it is at least `split-height-threshold' lines high,
  4036. ;; and it is at least twice as high as `window-min-height' and 2
  4037. ;; if it has a modeline or 1.
  4038. (and (memq window-size-fixed '(nil width))
  4039. (numberp split-height-threshold)
  4040. (>= (window-height window)
  4041. (max split-height-threshold
  4042. (* 2 (max window-min-height
  4043. (if mode-line-format 2 1))))))))))
  4044. (defun split-window-sensibly (window)
  4045. "Split WINDOW in a way suitable for `display-buffer'.
  4046. If `split-height-threshold' specifies an integer, WINDOW is at
  4047. least `split-height-threshold' lines tall and can be split
  4048. vertically, split WINDOW into two windows one above the other and
  4049. return the lower window. Otherwise, if `split-width-threshold'
  4050. specifies an integer, WINDOW is at least `split-width-threshold'
  4051. columns wide and can be split horizontally, split WINDOW into two
  4052. windows side by side and return the window on the right. If this
  4053. can't be done either and WINDOW is the only window on its frame,
  4054. try to split WINDOW vertically disregarding any value specified
  4055. by `split-height-threshold'. If that succeeds, return the lower
  4056. window. Return nil otherwise.
  4057. By default `display-buffer' routines call this function to split
  4058. the largest or least recently used window. To change the default
  4059. customize the option `split-window-preferred-function'.
  4060. You can enforce this function to not split WINDOW horizontally,
  4061. by setting (or binding) the variable `split-width-threshold' to
  4062. nil. If, in addition, you set `split-height-threshold' to zero,
  4063. chances increase that this function does split WINDOW vertically.
  4064. In order to not split WINDOW vertically, set (or bind) the
  4065. variable `split-height-threshold' to nil. Additionally, you can
  4066. set `split-width-threshold' to zero to make a horizontal split
  4067. more likely to occur.
  4068. Have a look at the function `window-splittable-p' if you want to
  4069. know how `split-window-sensibly' determines whether WINDOW can be
  4070. split."
  4071. (or (and (window-splittable-p window)
  4072. ;; Split window vertically.
  4073. (with-selected-window window
  4074. (split-window-below)))
  4075. (and (window-splittable-p window t)
  4076. ;; Split window horizontally.
  4077. (with-selected-window window
  4078. (split-window-right)))
  4079. (and (eq window (frame-root-window (window-frame window)))
  4080. (not (window-minibuffer-p window))
  4081. ;; If WINDOW is the only window on its frame and is not the
  4082. ;; minibuffer window, try to split it vertically disregarding
  4083. ;; the value of `split-height-threshold'.
  4084. (let ((split-height-threshold 0))
  4085. (when (window-splittable-p window)
  4086. (with-selected-window window
  4087. (split-window-below)))))))
  4088. (defun window--try-to-split-window (window)
  4089. "Try to split WINDOW.
  4090. Return value returned by `split-window-preferred-function' if it
  4091. represents a live window, nil otherwise."
  4092. (and (window-live-p window)
  4093. (not (frame-parameter (window-frame window) 'unsplittable))
  4094. (let ((new-window
  4095. ;; Since `split-window-preferred-function' might
  4096. ;; throw an error use `condition-case'.
  4097. (condition-case nil
  4098. (funcall split-window-preferred-function window)
  4099. (error nil))))
  4100. (and (window-live-p new-window) new-window))))
  4101. (defun window--frame-usable-p (frame)
  4102. "Return FRAME if it can be used to display a buffer."
  4103. (when (frame-live-p frame)
  4104. (let ((window (frame-root-window frame)))
  4105. ;; `frame-root-window' may be an internal window which is considered
  4106. ;; "dead" by `window-live-p'. Hence if `window' is not live we
  4107. ;; implicitly know that `frame' has a visible window we can use.
  4108. (unless (and (window-live-p window)
  4109. (or (window-minibuffer-p window)
  4110. ;; If the window is soft-dedicated, the frame is usable.
  4111. ;; Actually, even if the window is really dedicated,
  4112. ;; the frame is still usable by splitting it.
  4113. ;; At least Emacs-22 allowed it, and it is desirable
  4114. ;; when displaying same-frame windows.
  4115. nil ; (eq t (window-dedicated-p window))
  4116. ))
  4117. frame))))
  4118. (defcustom even-window-heights t
  4119. "If non-nil `display-buffer' will try to even window heights.
  4120. Otherwise `display-buffer' will leave the window configuration
  4121. alone. Heights are evened only when `display-buffer' chooses a
  4122. window that appears above or below the selected window."
  4123. :type 'boolean
  4124. :group 'windows)
  4125. (defun window--even-window-heights (window)
  4126. "Even heights of WINDOW and selected window.
  4127. Do this only if these windows are vertically adjacent to each
  4128. other, `even-window-heights' is non-nil, and the selected window
  4129. is higher than WINDOW."
  4130. (when (and even-window-heights
  4131. (not (eq window (selected-window)))
  4132. ;; Don't resize minibuffer windows.
  4133. (not (window-minibuffer-p (selected-window)))
  4134. (> (window-height (selected-window)) (window-height window))
  4135. (eq (window-frame window) (window-frame (selected-window)))
  4136. (let ((sel-edges (window-edges (selected-window)))
  4137. (win-edges (window-edges window)))
  4138. (and (= (nth 0 sel-edges) (nth 0 win-edges))
  4139. (= (nth 2 sel-edges) (nth 2 win-edges))
  4140. (or (= (nth 1 sel-edges) (nth 3 win-edges))
  4141. (= (nth 3 sel-edges) (nth 1 win-edges))))))
  4142. (let ((window-min-height 1))
  4143. ;; Don't throw an error if we can't even window heights for
  4144. ;; whatever reason.
  4145. (condition-case nil
  4146. (enlarge-window (/ (- (window-height window) (window-height)) 2))
  4147. (error nil)))))
  4148. (defun window--display-buffer-1 (window)
  4149. "Raise the frame containing WINDOW.
  4150. Do not raise the selected frame. Return WINDOW."
  4151. (let* ((frame (window-frame window))
  4152. (visible (frame-visible-p frame)))
  4153. (unless (or (not visible)
  4154. ;; Assume the selected frame is already visible enough.
  4155. (eq frame (selected-frame))
  4156. ;; Assume the frame from which we invoked the minibuffer
  4157. ;; is visible.
  4158. (and (minibuffer-window-active-p (selected-window))
  4159. (eq frame (window-frame (minibuffer-selected-window)))))
  4160. (raise-frame frame))
  4161. window))
  4162. (defun window--display-buffer-2 (buffer window &optional dedicated)
  4163. "Display BUFFER in WINDOW and make its frame visible.
  4164. Set `window-dedicated-p' to DEDICATED if non-nil.
  4165. Return WINDOW."
  4166. (when (and (buffer-live-p buffer) (window-live-p window))
  4167. (set-window-buffer window buffer)
  4168. (when dedicated
  4169. (set-window-dedicated-p window dedicated))
  4170. (window--display-buffer-1 window)))
  4171. ;; FIXME: Not implemented.
  4172. ;; FIXME: By the way, there could be more levels of dedication:
  4173. ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
  4174. ;; the window hasn't been used for something else yet.
  4175. ;; - `softly' dedicated only allows reuse when asked explicitly.
  4176. ;; - `strongly' never allows reuse.
  4177. (defvar display-buffer-mark-dedicated nil
  4178. "If non-nil, `display-buffer' marks the windows it creates as dedicated.
  4179. The actual non-nil value of this variable will be copied to the
  4180. `window-dedicated-p' flag.")
  4181. (defconst display-buffer--action-function-custom-type
  4182. '(choice :tag "Function"
  4183. (const :tag "--" ignore) ; default for insertion
  4184. (const display-buffer-reuse-window)
  4185. (const display-buffer-pop-up-window)
  4186. (const display-buffer-same-window)
  4187. (const display-buffer-pop-up-frame)
  4188. (const display-buffer-use-some-window)
  4189. (function :tag "Other function"))
  4190. "Custom type for `display-buffer' action functions.")
  4191. (defconst display-buffer--action-custom-type
  4192. `(cons :tag "Action"
  4193. (choice :tag "Action functions"
  4194. ,display-buffer--action-function-custom-type
  4195. (repeat
  4196. :tag "List of functions"
  4197. ,display-buffer--action-function-custom-type))
  4198. (alist :tag "Action arguments"
  4199. :key-type symbol
  4200. :value-type (sexp :tag "Value")))
  4201. "Custom type for `display-buffer' actions.")
  4202. (defvar display-buffer-overriding-action '(nil . nil)
  4203. "Overriding action to perform to display a buffer.
  4204. It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
  4205. function or a list of functions. Each function should accept two
  4206. arguments: a buffer to display and an alist similar to ALIST.
  4207. See `display-buffer' for details.")
  4208. (put 'display-buffer-overriding-action 'risky-local-variable t)
  4209. (defcustom display-buffer-alist nil
  4210. "Alist of conditional actions for `display-buffer'.
  4211. This is a list of elements (CONDITION . ACTION), where:
  4212. CONDITION is either a regexp matching buffer names, or a function
  4213. that takes a buffer and returns a boolean.
  4214. ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
  4215. function or a list of functions. Each such function should
  4216. accept two arguments: a buffer to display and an alist of the
  4217. same form as ALIST. See `display-buffer' for details."
  4218. :type `(alist :key-type
  4219. (choice :tag "Condition"
  4220. regexp
  4221. (function :tag "Matcher function"))
  4222. :value-type ,display-buffer--action-custom-type)
  4223. :risky t
  4224. :version "24.1"
  4225. :group 'windows)
  4226. (defcustom display-buffer-base-action '(nil . nil)
  4227. "User-specified default action for `display-buffer'.
  4228. It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
  4229. function or a list of functions. Each function should accept two
  4230. arguments: a buffer to display and an alist similar to ALIST.
  4231. See `display-buffer' for details."
  4232. :type display-buffer--action-custom-type
  4233. :risky t
  4234. :version "24.1"
  4235. :group 'windows)
  4236. (defconst display-buffer-fallback-action
  4237. '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
  4238. display-buffer-reuse-window
  4239. display-buffer--maybe-pop-up-frame-or-window
  4240. display-buffer-use-some-window
  4241. ;; If all else fails, pop up a new frame.
  4242. display-buffer-pop-up-frame))
  4243. "Default fallback action for `display-buffer'.
  4244. This is the action used by `display-buffer' if no other actions
  4245. specified, e.g. by the user options `display-buffer-alist' or
  4246. `display-buffer-base-action'. See `display-buffer'.")
  4247. (put 'display-buffer-fallback-action 'risky-local-variable t)
  4248. (defun display-buffer-assq-regexp (buffer-name alist)
  4249. "Retrieve ALIST entry corresponding to BUFFER-NAME."
  4250. (catch 'match
  4251. (dolist (entry alist)
  4252. (let ((key (car entry)))
  4253. (when (or (and (stringp key)
  4254. (string-match-p key buffer-name))
  4255. (and (symbolp key) (functionp key)
  4256. (funcall key buffer-name alist)))
  4257. (throw 'match (cdr entry)))))))
  4258. (defvar display-buffer--same-window-action
  4259. '(display-buffer-same-window
  4260. (inhibit-same-window . nil))
  4261. "A `display-buffer' action for displaying in the same window.")
  4262. (put 'display-buffer--same-window-action 'risky-local-variable t)
  4263. (defvar display-buffer--other-frame-action
  4264. '((display-buffer-reuse-window
  4265. display-buffer-pop-up-frame)
  4266. (reusable-frames . 0)
  4267. (inhibit-same-window . t))
  4268. "A `display-buffer' action for displaying in another frame.")
  4269. (put 'display-buffer--other-frame-action 'risky-local-variable t)
  4270. (defun display-buffer (buffer-or-name &optional action frame)
  4271. "Display BUFFER-OR-NAME in some window, without selecting it.
  4272. BUFFER-OR-NAME must be a buffer or the name of an existing
  4273. buffer. Return the window chosen for displaying BUFFER-OR-NAME,
  4274. or nil if no such window is found.
  4275. Optional argument ACTION should have the form (FUNCTION . ALIST).
  4276. FUNCTION is either a function or a list of functions.
  4277. ALIST is an arbitrary association list (alist).
  4278. Each such FUNCTION should accept two arguments: the buffer to
  4279. display and an alist. Based on those arguments, it should either
  4280. display the buffer and return the window, or return nil if unable
  4281. to display the buffer.
  4282. The `display-buffer' function builds a function list and an alist
  4283. by combining the functions and alists specified in
  4284. `display-buffer-overriding-action', `display-buffer-alist', the
  4285. ACTION argument, `display-buffer-base-action', and
  4286. `display-buffer-fallback-action' (in order). Then it calls each
  4287. function in the combined function list in turn, passing the
  4288. buffer as the first argument and the combined alist as the second
  4289. argument, until one of the functions returns non-nil.
  4290. Available action functions include:
  4291. `display-buffer-same-window'
  4292. `display-buffer-reuse-window'
  4293. `display-buffer-pop-up-frame'
  4294. `display-buffer-pop-up-window'
  4295. `display-buffer-use-some-window'
  4296. Recognized alist entries include:
  4297. `inhibit-same-window' -- A non-nil value prevents the same
  4298. window from being used for display.
  4299. `reusable-frames' -- Value specifies frame(s) to search for a
  4300. window that already displays the buffer.
  4301. See `display-buffer-reuse-window'.
  4302. The ACTION argument to `display-buffer' can also have a non-nil
  4303. and non-list value. This means to display the buffer in a window
  4304. other than the selected one, even if it is already displayed in
  4305. the selected window. If called interactively with a prefix
  4306. argument, ACTION is t.
  4307. Optional argument FRAME, if non-nil, acts like an additional
  4308. ALIST entry (reusable-frames . FRAME), specifying the frame(s) to
  4309. search for a window that is already displaying the buffer. See
  4310. `display-buffer-reuse-window'."
  4311. (interactive (list (read-buffer "Display buffer: " (other-buffer))
  4312. (if current-prefix-arg t)))
  4313. (let ((buffer (if (bufferp buffer-or-name)
  4314. buffer-or-name
  4315. (get-buffer buffer-or-name)))
  4316. ;; Handle the old form of the first argument.
  4317. (inhibit-same-window (and action (not (listp action)))))
  4318. (unless (listp action) (setq action nil))
  4319. (if display-buffer-function
  4320. ;; If `display-buffer-function' is defined, let it do the job.
  4321. (funcall display-buffer-function buffer inhibit-same-window)
  4322. ;; Otherwise, use the defined actions.
  4323. (let* ((user-action
  4324. (display-buffer-assq-regexp (buffer-name buffer)
  4325. display-buffer-alist))
  4326. (special-action (display-buffer--special-action buffer))
  4327. ;; Extra actions from the arguments to this function:
  4328. (extra-action
  4329. (cons nil (append (if inhibit-same-window
  4330. '((inhibit-same-window . t)))
  4331. (if frame
  4332. `((reusable-frames . ,frame))))))
  4333. ;; Construct action function list and action alist.
  4334. (actions (list display-buffer-overriding-action
  4335. user-action special-action action extra-action
  4336. display-buffer-base-action
  4337. display-buffer-fallback-action))
  4338. (functions (apply 'append
  4339. (mapcar (lambda (x)
  4340. (setq x (car x))
  4341. (if (functionp x) (list x) x))
  4342. actions)))
  4343. (alist (apply 'append (mapcar 'cdr actions)))
  4344. window)
  4345. (unless (buffer-live-p buffer)
  4346. (error "Invalid buffer"))
  4347. (while (and functions (not window))
  4348. (setq window (funcall (car functions) buffer alist)
  4349. functions (cdr functions)))
  4350. window))))
  4351. (defun display-buffer-other-frame (buffer)
  4352. "Display buffer BUFFER in another frame.
  4353. This uses the function `display-buffer' as a subroutine; see
  4354. its documentation for additional customization information."
  4355. (interactive "BDisplay buffer in other frame: ")
  4356. (display-buffer buffer display-buffer--other-frame-action t))
  4357. ;;; `display-buffer' action functions:
  4358. (defun display-buffer-same-window (buffer alist)
  4359. "Display BUFFER in the selected window.
  4360. This fails if ALIST has a non-nil `inhibit-same-window' entry, or
  4361. if the selected window is a minibuffer window or is dedicated to
  4362. another buffer; in that case, return nil. Otherwise, return the
  4363. selected window."
  4364. (unless (or (cdr (assq 'inhibit-same-window alist))
  4365. (window-minibuffer-p)
  4366. (window-dedicated-p))
  4367. (display-buffer-record-window 'reuse (selected-window) buffer)
  4368. (window--display-buffer-2 buffer (selected-window))))
  4369. (defun display-buffer--maybe-same-window (buffer alist)
  4370. "Conditionally display BUFFER in the selected window.
  4371. If `same-window-p' returns non-nil for BUFFER's name, call
  4372. `display-buffer-same-window' and return its value. Otherwise,
  4373. return nil."
  4374. (and (same-window-p (buffer-name buffer))
  4375. (display-buffer-same-window buffer alist)))
  4376. (defun display-buffer-reuse-window (buffer alist)
  4377. "Return a window that is already displaying BUFFER.
  4378. Return nil if no usable window is found.
  4379. If ALIST has a non-nil `inhibit-same-window' entry, the selected
  4380. window is not eligible for reuse.
  4381. If ALIST contains a `reusable-frames' entry, its value determines
  4382. which frames to search for a reusable window:
  4383. nil -- the selected frame (actually the last non-minibuffer frame)
  4384. A frame -- just that frame
  4385. `visible' -- all visible frames
  4386. 0 -- all frames on the current terminal
  4387. t -- all frames.
  4388. If ALIST contains no `reusable-frames' entry, search just the
  4389. selected frame if `display-buffer-reuse-frames' and
  4390. `pop-up-frames' are both nil; search all frames on the current
  4391. terminal if either of those variables is non-nil."
  4392. (let* ((alist-entry (assq 'reusable-frames alist))
  4393. (frames (cond (alist-entry (cdr alist-entry))
  4394. ((if (eq pop-up-frames 'graphic-only)
  4395. (display-graphic-p)
  4396. pop-up-frames)
  4397. 0)
  4398. (display-buffer-reuse-frames 0)
  4399. (t (last-nonminibuffer-frame))))
  4400. (window (if (and (eq buffer (window-buffer))
  4401. (not (cdr (assq 'inhibit-same-window alist))))
  4402. (selected-window)
  4403. (car (delq (selected-window)
  4404. (get-buffer-window-list buffer 'nomini
  4405. frames))))))
  4406. (when window
  4407. (display-buffer-record-window 'reuse window buffer)
  4408. (window--display-buffer-1 window))))
  4409. (defun display-buffer--special-action (buffer)
  4410. "Return special display action for BUFFER, if any.
  4411. If `special-display-p' returns non-nil for BUFFER, return an
  4412. appropriate display action involving `special-display-function'.
  4413. See `display-buffer' for the format of display actions."
  4414. (and special-display-function
  4415. ;; `special-display-p' returns either t or a list of frame
  4416. ;; parameters to pass to `special-display-function'.
  4417. (let ((pars (special-display-p (buffer-name buffer))))
  4418. (when pars
  4419. (list (list #'display-buffer-reuse-window
  4420. `(lambda (buffer _alist)
  4421. (funcall special-display-function
  4422. buffer ',(if (listp pars) pars)))))))))
  4423. (defun display-buffer-pop-up-frame (buffer _alist)
  4424. "Display BUFFER in a new frame.
  4425. This works by calling `pop-up-frame-function'. If successful,
  4426. return the window used; otherwise return nil."
  4427. (let ((fun pop-up-frame-function)
  4428. frame window)
  4429. (when (and fun
  4430. (setq frame (funcall fun))
  4431. (setq window (frame-selected-window frame)))
  4432. (display-buffer-record-window 'frame window buffer)
  4433. (window--display-buffer-2 buffer window display-buffer-mark-dedicated)
  4434. ;; Reset list of WINDOW's previous buffers to nil.
  4435. (set-window-prev-buffers window nil)
  4436. window)))
  4437. (defun display-buffer-pop-up-window (buffer _alist)
  4438. "Display BUFFER by popping up a new window.
  4439. The new window is created on the selected frame, or in
  4440. `last-nonminibuffer-frame' if no windows can be created there.
  4441. If successful, return the new window; otherwise return nil."
  4442. (let ((frame (or (window--frame-usable-p (selected-frame))
  4443. (window--frame-usable-p (last-nonminibuffer-frame))))
  4444. window)
  4445. (when (and (or (not (frame-parameter frame 'unsplittable))
  4446. ;; If the selected frame cannot be split, look at
  4447. ;; `last-nonminibuffer-frame'.
  4448. (and (eq frame (selected-frame))
  4449. (setq frame (last-nonminibuffer-frame))
  4450. (window--frame-usable-p frame)
  4451. (not (frame-parameter frame 'unsplittable))))
  4452. ;; Attempt to split largest or least recently used window.
  4453. (setq window (or (window--try-to-split-window
  4454. (get-largest-window frame t))
  4455. (window--try-to-split-window
  4456. (get-lru-window frame t)))))
  4457. (display-buffer-record-window 'window window buffer)
  4458. (window--display-buffer-2 buffer window display-buffer-mark-dedicated)
  4459. ;; Reset list of WINDOW's previous buffers to nil.
  4460. (set-window-prev-buffers window nil)
  4461. window)))
  4462. (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
  4463. "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
  4464. If `pop-up-frames' is non-nil (and not `graphic-only' on a
  4465. text-only terminal), try with `display-buffer-pop-up-frame'.
  4466. If that cannot be done, and `pop-up-windows' is non-nil, try
  4467. again with `display-buffer-pop-up-window'."
  4468. (or (and (if (eq pop-up-frames 'graphic-only)
  4469. (display-graphic-p)
  4470. pop-up-frames)
  4471. (display-buffer-pop-up-frame buffer alist))
  4472. (and pop-up-windows
  4473. (display-buffer-pop-up-window buffer alist))))
  4474. (defun display-buffer-use-some-window (buffer alist)
  4475. "Display BUFFER in an existing window.
  4476. Search for a usable window, set that window to the buffer, and
  4477. return the window. If no suitable window is found, return nil."
  4478. (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
  4479. (window-to-undedicate
  4480. ;; When NOT-THIS-WINDOW is non-nil, temporarily dedicate the
  4481. ;; selected window to its buffer, to prevent any of the
  4482. ;; `get-' routines below from choosing it. (Bug#1415)
  4483. (and not-this-window (not (window-dedicated-p))
  4484. (set-window-dedicated-p (selected-window) t)
  4485. (selected-window)))
  4486. (frame (or (window--frame-usable-p (selected-frame))
  4487. (window--frame-usable-p (last-nonminibuffer-frame))))
  4488. window)
  4489. (unwind-protect
  4490. (setq window
  4491. ;; Reuse an existing window.
  4492. (or (get-lru-window frame)
  4493. (let ((window (get-buffer-window buffer 'visible)))
  4494. (unless (and not-this-window
  4495. (eq window (selected-window)))
  4496. window))
  4497. (get-largest-window 'visible)
  4498. (let ((window (get-buffer-window buffer 0)))
  4499. (unless (and not-this-window
  4500. (eq window (selected-window)))
  4501. window))
  4502. (get-largest-window 0)))
  4503. (when (window-live-p window-to-undedicate)
  4504. ;; Restore dedicated status of selected window.
  4505. (set-window-dedicated-p window-to-undedicate nil)))
  4506. (when window
  4507. (display-buffer-record-window 'reuse window buffer)
  4508. (window--even-window-heights window)
  4509. (window--display-buffer-2 buffer window))))
  4510. ;;; Display + selection commands:
  4511. (defun pop-to-buffer (buffer &optional action norecord)
  4512. "Select buffer BUFFER in some window, preferably a different one.
  4513. BUFFER may be a buffer, a string (a buffer name), or nil. If it
  4514. is a string not naming an existent buffer, create a buffer with
  4515. that name. If BUFFER is nil, choose some other buffer. Return
  4516. the buffer.
  4517. This uses `display-buffer' as a subroutine. The optional ACTION
  4518. argument is passed to `display-buffer' as its ACTION argument.
  4519. See `display-buffer' for more information. ACTION is t if called
  4520. interactively with a prefix argument, which means to pop to a
  4521. window other than the selected one even if the buffer is already
  4522. displayed in the selected window.
  4523. If the window to show BUFFER is not on the selected
  4524. frame, raise that window's frame and give it input focus.
  4525. Optional third arg NORECORD non-nil means do not put this buffer
  4526. at the front of the list of recently selected ones."
  4527. (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
  4528. (if current-prefix-arg t)))
  4529. (setq buffer (window-normalize-buffer-to-switch-to buffer))
  4530. (set-buffer buffer)
  4531. (let* ((old-frame (selected-frame))
  4532. (window (display-buffer buffer action))
  4533. (frame (window-frame window)))
  4534. ;; If we chose another frame, make sure it gets input focus.
  4535. (unless (eq frame old-frame)
  4536. (select-frame-set-input-focus frame norecord))
  4537. ;; Make sure new window is selected (Bug#8615), (Bug#6954).
  4538. (select-window window norecord)
  4539. buffer))
  4540. (defun pop-to-buffer-same-window (buffer &optional norecord)
  4541. "Select buffer BUFFER in some window, preferably the same one.
  4542. This function behaves much like `switch-to-buffer', except it
  4543. displays with `special-display-function' if BUFFER has a match in
  4544. `special-display-buffer-names' or `special-display-regexps'.
  4545. Unlike `pop-to-buffer', this function prefers using the selected
  4546. window over popping up a new window or frame.
  4547. BUFFER may be a buffer, a string (a buffer name), or nil. If it
  4548. is a string not naming an existent buffer, create a buffer with
  4549. that name. If BUFFER is nil, choose some other buffer. Return
  4550. the buffer.
  4551. NORECORD, if non-nil means do not put this buffer at the front of
  4552. the list of recently selected ones."
  4553. (pop-to-buffer buffer display-buffer--same-window-action norecord))
  4554. (defun read-buffer-to-switch (prompt)
  4555. "Read the name of a buffer to switch to, prompting with PROMPT.
  4556. Return the name of the buffer as a string.
  4557. This function is intended for the `switch-to-buffer' family of
  4558. commands since these need to omit the name of the current buffer
  4559. from the list of completions and default values."
  4560. (let ((rbts-completion-table (internal-complete-buffer-except)))
  4561. (minibuffer-with-setup-hook
  4562. (lambda ()
  4563. (setq minibuffer-completion-table rbts-completion-table)
  4564. ;; Since rbts-completion-table is built dynamically, we
  4565. ;; can't just add it to the default value of
  4566. ;; icomplete-with-completion-tables, so we add it
  4567. ;; here manually.
  4568. (if (and (boundp 'icomplete-with-completion-tables)
  4569. (listp icomplete-with-completion-tables))
  4570. (set (make-local-variable 'icomplete-with-completion-tables)
  4571. (cons rbts-completion-table
  4572. icomplete-with-completion-tables))))
  4573. (read-buffer prompt (other-buffer (current-buffer))
  4574. (confirm-nonexistent-file-or-buffer)))))
  4575. (defun window-normalize-buffer-to-switch-to (buffer-or-name)
  4576. "Normalize BUFFER-OR-NAME argument of buffer switching functions.
  4577. If BUFFER-OR-NAME is nil, return the buffer returned by
  4578. `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
  4579. exists, return that buffer. If no such buffer exists, create a
  4580. buffer with the name BUFFER-OR-NAME and return that buffer."
  4581. (if buffer-or-name
  4582. (or (get-buffer buffer-or-name)
  4583. (let ((buffer (get-buffer-create buffer-or-name)))
  4584. (set-buffer-major-mode buffer)
  4585. buffer))
  4586. (other-buffer)))
  4587. (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
  4588. "Switch to buffer BUFFER-OR-NAME in the selected window.
  4589. If called interactively, prompt for the buffer name using the
  4590. minibuffer. The variable `confirm-nonexistent-file-or-buffer'
  4591. determines whether to request confirmation before creating a new
  4592. buffer.
  4593. BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
  4594. nil. If BUFFER-OR-NAME is a string that does not identify an
  4595. existing buffer, create a buffer with that name. If
  4596. BUFFER-OR-NAME is nil, switch to the buffer returned by
  4597. `other-buffer'.
  4598. Optional argument NORECORD non-nil means do not put the buffer
  4599. specified by BUFFER-OR-NAME at the front of the buffer list and
  4600. do not make the window displaying it the most recently selected
  4601. one.
  4602. If FORCE-SAME-WINDOW is non-nil, BUFFER-OR-NAME must be displayed
  4603. in the selected window; signal an error if that is
  4604. impossible (e.g. if the selected window is minibuffer-only). If
  4605. nil, BUFFER-OR-NAME may be displayed in another window.
  4606. Return the buffer switched to."
  4607. (interactive
  4608. (list (read-buffer-to-switch "Switch to buffer: ") nil 'force-same-window))
  4609. (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
  4610. (cond
  4611. ;; Don't call set-window-buffer if it's not needed since it
  4612. ;; might signal an error (e.g. if the window is dedicated).
  4613. ((eq buffer (window-buffer)))
  4614. ((window-minibuffer-p)
  4615. (if force-same-window
  4616. (error "Cannot switch buffers in minibuffer window")
  4617. (pop-to-buffer buffer norecord)))
  4618. ((eq (window-dedicated-p) t)
  4619. (if force-same-window
  4620. (error "Cannot switch buffers in a dedicated window")
  4621. (pop-to-buffer buffer norecord)))
  4622. (t (set-window-buffer nil buffer)))
  4623. (unless norecord
  4624. (select-window (selected-window)))
  4625. (set-buffer buffer)))
  4626. (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
  4627. "Select the buffer specified by BUFFER-OR-NAME in another window.
  4628. BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
  4629. nil. Return the buffer switched to.
  4630. If called interactively, prompt for the buffer name using the
  4631. minibuffer. The variable `confirm-nonexistent-file-or-buffer'
  4632. determines whether to request confirmation before creating a new
  4633. buffer.
  4634. If BUFFER-OR-NAME is a string and does not identify an existing
  4635. buffer, create a new buffer with that name. If BUFFER-OR-NAME is
  4636. nil, switch to the buffer returned by `other-buffer'.
  4637. Optional second argument NORECORD non-nil means do not put this
  4638. buffer at the front of the list of recently selected ones.
  4639. This uses the function `display-buffer' as a subroutine; see its
  4640. documentation for additional customization information."
  4641. (interactive
  4642. (list (read-buffer-to-switch "Switch to buffer in other window: ")))
  4643. (let ((pop-up-windows t))
  4644. (pop-to-buffer buffer-or-name t norecord)))
  4645. (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
  4646. "Switch to buffer BUFFER-OR-NAME in another frame.
  4647. BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
  4648. nil. Return the buffer switched to.
  4649. If called interactively, prompt for the buffer name using the
  4650. minibuffer. The variable `confirm-nonexistent-file-or-buffer'
  4651. determines whether to request confirmation before creating a new
  4652. buffer.
  4653. If BUFFER-OR-NAME is a string and does not identify an existing
  4654. buffer, create a new buffer with that name. If BUFFER-OR-NAME is
  4655. nil, switch to the buffer returned by `other-buffer'.
  4656. Optional second arg NORECORD non-nil means do not put this
  4657. buffer at the front of the list of recently selected ones.
  4658. This uses the function `display-buffer' as a subroutine; see its
  4659. documentation for additional customization information."
  4660. (interactive
  4661. (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
  4662. (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
  4663. (defun set-window-text-height (window height)
  4664. "Set the height in lines of the text display area of WINDOW to HEIGHT.
  4665. WINDOW must be a live window. HEIGHT doesn't include the mode
  4666. line or header line, if any, or any partial-height lines in the
  4667. text display area.
  4668. Note that the current implementation of this function cannot
  4669. always set the height exactly, but attempts to be conservative,
  4670. by allocating more lines than are actually needed in the case
  4671. where some error may be present."
  4672. (setq window (window-normalize-window window t))
  4673. (let ((delta (- height (window-text-height window))))
  4674. (unless (zerop delta)
  4675. ;; Setting window-min-height to a value like 1 can lead to very
  4676. ;; bizarre displays because it also allows Emacs to make *other*
  4677. ;; windows 1-line tall, which means that there's no more space for
  4678. ;; the modeline.
  4679. (let ((window-min-height (min 2 height))) ; One text line plus a modeline.
  4680. (window-resize window delta)))))
  4681. (defun enlarge-window-horizontally (delta)
  4682. "Make selected window DELTA columns wider.
  4683. Interactively, if no argument is given, make selected window one
  4684. column wider."
  4685. (interactive "p")
  4686. (enlarge-window delta t))
  4687. (defun shrink-window-horizontally (delta)
  4688. "Make selected window DELTA columns narrower.
  4689. Interactively, if no argument is given, make selected window one
  4690. column narrower."
  4691. (interactive "p")
  4692. (shrink-window delta t))
  4693. (defun count-screen-lines (&optional beg end count-final-newline window)
  4694. "Return the number of screen lines in the region.
  4695. The number of screen lines may be different from the number of actual lines,
  4696. due to line breaking, display table, etc.
  4697. Optional arguments BEG and END default to `point-min' and `point-max'
  4698. respectively.
  4699. If region ends with a newline, ignore it unless optional third argument
  4700. COUNT-FINAL-NEWLINE is non-nil.
  4701. The optional fourth argument WINDOW specifies the window used for obtaining
  4702. parameters such as width, horizontal scrolling, and so on. The default is
  4703. to use the selected window's parameters.
  4704. Like `vertical-motion', `count-screen-lines' always uses the current buffer,
  4705. regardless of which buffer is displayed in WINDOW. This makes possible to use
  4706. `count-screen-lines' in any buffer, whether or not it is currently displayed
  4707. in some window."
  4708. (unless beg
  4709. (setq beg (point-min)))
  4710. (unless end
  4711. (setq end (point-max)))
  4712. (if (= beg end)
  4713. 0
  4714. (save-excursion
  4715. (save-restriction
  4716. (widen)
  4717. (narrow-to-region (min beg end)
  4718. (if (and (not count-final-newline)
  4719. (= ?\n (char-before (max beg end))))
  4720. (1- (max beg end))
  4721. (max beg end)))
  4722. (goto-char (point-min))
  4723. (1+ (vertical-motion (buffer-size) window))))))
  4724. (defun window-buffer-height (window)
  4725. "Return the height (in screen lines) of the buffer that WINDOW is displaying."
  4726. (with-current-buffer (window-buffer window)
  4727. (max 1
  4728. (count-screen-lines (point-min) (point-max)
  4729. ;; If buffer ends with a newline, ignore it when
  4730. ;; counting height unless point is after it.
  4731. (eobp)
  4732. window))))
  4733. ;;; Resizing buffers to fit their contents exactly.
  4734. (defun fit-window-to-buffer (&optional window max-height min-height override)
  4735. "Adjust height of WINDOW to display its buffer's contents exactly.
  4736. WINDOW can be any live window and defaults to the selected one.
  4737. Optional argument MAX-HEIGHT specifies the maximum height of
  4738. WINDOW and defaults to the height of WINDOW's frame. Optional
  4739. argument MIN-HEIGHT specifies the minimum height of WINDOW and
  4740. defaults to `window-min-height'. Both MAX-HEIGHT and MIN-HEIGHT
  4741. are specified in lines and include the mode line and header line,
  4742. if any.
  4743. Optional argument OVERRIDE non-nil means override restrictions
  4744. imposed by `window-min-height' and `window-min-width' on the size
  4745. of WINDOW.
  4746. Return the number of lines by which WINDOW was enlarged or
  4747. shrunk. If an error occurs during resizing, return nil but don't
  4748. signal an error.
  4749. Note that even if this function makes WINDOW large enough to show
  4750. _all_ lines of its buffer you might not see the first lines when
  4751. WINDOW was scrolled."
  4752. (interactive)
  4753. ;; Do all the work in WINDOW and its buffer and restore the selected
  4754. ;; window and the current buffer when we're done.
  4755. (setq window (window-normalize-window window t))
  4756. ;; Can't resize a full height or fixed-size window.
  4757. (unless (or (window-size-fixed-p window)
  4758. (window-full-height-p window))
  4759. ;; `with-selected-window' should orderly restore the current buffer.
  4760. (with-selected-window window
  4761. ;; We are in WINDOW's buffer now.
  4762. (let* (;; Adjust MIN-HEIGHT.
  4763. (min-height
  4764. (if override
  4765. (window-min-size window nil window)
  4766. (max (or min-height window-min-height)
  4767. window-safe-min-height)))
  4768. (max-window-height
  4769. (window-total-size (frame-root-window window)))
  4770. ;; Adjust MAX-HEIGHT.
  4771. (max-height
  4772. (if (or override (not max-height))
  4773. max-window-height
  4774. (min max-height max-window-height)))
  4775. ;; Make `desired-height' the height necessary to show
  4776. ;; all of WINDOW's buffer, constrained by MIN-HEIGHT
  4777. ;; and MAX-HEIGHT.
  4778. (desired-height
  4779. (max
  4780. (min
  4781. (+ (count-screen-lines)
  4782. ;; For non-minibuffers count the mode line, if any.
  4783. (if (and (not (window-minibuffer-p window))
  4784. mode-line-format)
  4785. 1
  4786. 0)
  4787. ;; Count the header line, if any.
  4788. (if header-line-format 1 0))
  4789. max-height)
  4790. min-height))
  4791. (desired-delta
  4792. (- desired-height (window-total-size window)))
  4793. (delta
  4794. (if (> desired-delta 0)
  4795. (min desired-delta
  4796. (window-max-delta window nil window))
  4797. (max desired-delta
  4798. (- (window-min-delta window nil window))))))
  4799. ;; This `condition-case' shouldn't be necessary, but who knows?
  4800. (condition-case nil
  4801. (if (zerop delta)
  4802. ;; Return zero if DELTA became zero in the process.
  4803. 0
  4804. ;; Don't try to redisplay with the cursor at the end on its
  4805. ;; own line--that would force a scroll and spoil things.
  4806. (when (and (eobp) (bolp) (not (bobp)))
  4807. ;; It's silly to put `point' at the end of the previous
  4808. ;; line and so maybe force horizontal scrolling.
  4809. (set-window-point window (line-beginning-position 0)))
  4810. ;; Call `window-resize' with OVERRIDE argument equal WINDOW.
  4811. (window-resize window delta nil window)
  4812. ;; Check if the last line is surely fully visible. If
  4813. ;; not, enlarge the window.
  4814. (let ((end (save-excursion
  4815. (goto-char (point-max))
  4816. (when (and (bolp) (not (bobp)))
  4817. ;; Don't include final newline.
  4818. (backward-char 1))
  4819. (when truncate-lines
  4820. ;; If line-wrapping is turned off, test the
  4821. ;; beginning of the last line for
  4822. ;; visibility instead of the end, as the
  4823. ;; end of the line could be invisible by
  4824. ;; virtue of extending past the edge of the
  4825. ;; window.
  4826. (forward-line 0))
  4827. (point))))
  4828. (set-window-vscroll window 0)
  4829. ;; This loop might in some rare pathological cases raise
  4830. ;; an error - another reason for the `condition-case'.
  4831. (while (and (< desired-height max-height)
  4832. (= desired-height (window-total-size))
  4833. (not (pos-visible-in-window-p end)))
  4834. (window-resize window 1 nil window)
  4835. (setq desired-height (1+ desired-height)))))
  4836. (error (setq delta nil)))
  4837. delta))))
  4838. (defun window-safely-shrinkable-p (&optional window)
  4839. "Return t if WINDOW can be shrunk without shrinking other windows.
  4840. WINDOW defaults to the selected window."
  4841. (with-selected-window (or window (selected-window))
  4842. (let ((edges (window-edges)))
  4843. (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
  4844. (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
  4845. (defun shrink-window-if-larger-than-buffer (&optional window)
  4846. "Shrink height of WINDOW if its buffer doesn't need so many lines.
  4847. More precisely, shrink WINDOW vertically to be as small as
  4848. possible, while still showing the full contents of its buffer.
  4849. WINDOW defaults to the selected window.
  4850. Do not shrink WINDOW to less than `window-min-height' lines. Do
  4851. nothing if the buffer contains more lines than the present window
  4852. height, or if some of the window's contents are scrolled out of
  4853. view, or if shrinking this window would also shrink another
  4854. window, or if the window is the only window of its frame.
  4855. Return non-nil if the window was shrunk, nil otherwise."
  4856. (interactive)
  4857. (setq window (window-normalize-window window t))
  4858. ;; Make sure that WINDOW is vertically combined and `point-min' is
  4859. ;; visible (for whatever reason that's needed). The remaining issues
  4860. ;; should be taken care of by `fit-window-to-buffer'.
  4861. (when (and (window-combined-p window)
  4862. (pos-visible-in-window-p (point-min) window))
  4863. (fit-window-to-buffer window (window-total-size window))))
  4864. (defun kill-buffer-and-window ()
  4865. "Kill the current buffer and delete the selected window."
  4866. (interactive)
  4867. (let ((window-to-delete (selected-window))
  4868. (buffer-to-kill (current-buffer))
  4869. (delete-window-hook (lambda () (ignore-errors (delete-window)))))
  4870. (unwind-protect
  4871. (progn
  4872. (add-hook 'kill-buffer-hook delete-window-hook t t)
  4873. (if (kill-buffer (current-buffer))
  4874. ;; If `delete-window' failed before, we rerun it to regenerate
  4875. ;; the error so it can be seen in the echo area.
  4876. (when (eq (selected-window) window-to-delete)
  4877. (delete-window))))
  4878. ;; If the buffer is not dead for some reason (probably because
  4879. ;; of a `quit' signal), remove the hook again.
  4880. (ignore-errors
  4881. (with-current-buffer buffer-to-kill
  4882. (remove-hook 'kill-buffer-hook delete-window-hook t))))))
  4883. (defvar recenter-last-op nil
  4884. "Indicates the last recenter operation performed.
  4885. Possible values: `top', `middle', `bottom', integer or float numbers.")
  4886. (defcustom recenter-positions '(middle top bottom)
  4887. "Cycling order for `recenter-top-bottom'.
  4888. A list of elements with possible values `top', `middle', `bottom',
  4889. integer or float numbers that define the cycling order for
  4890. the command `recenter-top-bottom'.
  4891. Top and bottom destinations are `scroll-margin' lines from the true
  4892. window top and bottom. Middle redraws the frame and centers point
  4893. vertically within the window. Integer number moves current line to
  4894. the specified absolute window-line. Float number between 0.0 and 1.0
  4895. means the percentage of the screen space from the top. The default
  4896. cycling order is middle -> top -> bottom."
  4897. :type '(repeat (choice
  4898. (const :tag "Top" top)
  4899. (const :tag "Middle" middle)
  4900. (const :tag "Bottom" bottom)
  4901. (integer :tag "Line number")
  4902. (float :tag "Percentage")))
  4903. :version "23.2"
  4904. :group 'windows)
  4905. (defun recenter-top-bottom (&optional arg)
  4906. "Move current buffer line to the specified window line.
  4907. With no prefix argument, successive calls place point according
  4908. to the cycling order defined by `recenter-positions'.
  4909. A prefix argument is handled like `recenter':
  4910. With numeric prefix ARG, move current line to window-line ARG.
  4911. With plain `C-u', move current line to window center."
  4912. (interactive "P")
  4913. (cond
  4914. (arg (recenter arg)) ; Always respect ARG.
  4915. (t
  4916. (setq recenter-last-op
  4917. (if (eq this-command last-command)
  4918. (car (or (cdr (member recenter-last-op recenter-positions))
  4919. recenter-positions))
  4920. (car recenter-positions)))
  4921. (let ((this-scroll-margin
  4922. (min (max 0 scroll-margin)
  4923. (truncate (/ (window-body-height) 4.0)))))
  4924. (cond ((eq recenter-last-op 'middle)
  4925. (recenter))
  4926. ((eq recenter-last-op 'top)
  4927. (recenter this-scroll-margin))
  4928. ((eq recenter-last-op 'bottom)
  4929. (recenter (- -1 this-scroll-margin)))
  4930. ((integerp recenter-last-op)
  4931. (recenter recenter-last-op))
  4932. ((floatp recenter-last-op)
  4933. (recenter (round (* recenter-last-op (window-height))))))))))
  4934. (define-key global-map [?\C-l] 'recenter-top-bottom)
  4935. (defun move-to-window-line-top-bottom (&optional arg)
  4936. "Position point relative to window.
  4937. With a prefix argument ARG, acts like `move-to-window-line'.
  4938. With no argument, positions point at center of window.
  4939. Successive calls position point at positions defined
  4940. by `recenter-positions'."
  4941. (interactive "P")
  4942. (cond
  4943. (arg (move-to-window-line arg)) ; Always respect ARG.
  4944. (t
  4945. (setq recenter-last-op
  4946. (if (eq this-command last-command)
  4947. (car (or (cdr (member recenter-last-op recenter-positions))
  4948. recenter-positions))
  4949. (car recenter-positions)))
  4950. (let ((this-scroll-margin
  4951. (min (max 0 scroll-margin)
  4952. (truncate (/ (window-body-height) 4.0)))))
  4953. (cond ((eq recenter-last-op 'middle)
  4954. (call-interactively 'move-to-window-line))
  4955. ((eq recenter-last-op 'top)
  4956. (move-to-window-line this-scroll-margin))
  4957. ((eq recenter-last-op 'bottom)
  4958. (move-to-window-line (- -1 this-scroll-margin)))
  4959. ((integerp recenter-last-op)
  4960. (move-to-window-line recenter-last-op))
  4961. ((floatp recenter-last-op)
  4962. (move-to-window-line (round (* recenter-last-op (window-height))))))))))
  4963. (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
  4964. ;;; Scrolling commands.
  4965. ;;; Scrolling commands which do not signal errors at top/bottom
  4966. ;;; of buffer at first key-press (instead move to top/bottom
  4967. ;;; of buffer).
  4968. (defcustom scroll-error-top-bottom nil
  4969. "Move point to top/bottom of buffer before signaling a scrolling error.
  4970. A value of nil means just signal an error if no more scrolling possible.
  4971. A value of t means point moves to the beginning or the end of the buffer
  4972. \(depending on scrolling direction) when no more scrolling possible.
  4973. When point is already on that position, then signal an error."
  4974. :type 'boolean
  4975. :group 'scrolling
  4976. :version "24.1")
  4977. (defun scroll-up-command (&optional arg)
  4978. "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
  4979. If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
  4980. scroll window further, move cursor to the bottom line.
  4981. When point is already on that position, then signal an error.
  4982. A near full screen is `next-screen-context-lines' less than a full screen.
  4983. Negative ARG means scroll downward.
  4984. If ARG is the atom `-', scroll downward by nearly full screen."
  4985. (interactive "^P")
  4986. (cond
  4987. ((null scroll-error-top-bottom)
  4988. (scroll-up arg))
  4989. ((eq arg '-)
  4990. (scroll-down-command nil))
  4991. ((< (prefix-numeric-value arg) 0)
  4992. (scroll-down-command (- (prefix-numeric-value arg))))
  4993. ((eobp)
  4994. (scroll-up arg)) ; signal error
  4995. (t
  4996. (condition-case nil
  4997. (scroll-up arg)
  4998. (end-of-buffer
  4999. (if arg
  5000. ;; When scrolling by ARG lines can't be done,
  5001. ;; move by ARG lines instead.
  5002. (forward-line arg)
  5003. ;; When ARG is nil for full-screen scrolling,
  5004. ;; move to the bottom of the buffer.
  5005. (goto-char (point-max))))))))
  5006. (put 'scroll-up-command 'scroll-command t)
  5007. (defun scroll-down-command (&optional arg)
  5008. "Scroll text of selected window down ARG lines; or near full screen if no ARG.
  5009. If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
  5010. scroll window further, move cursor to the top line.
  5011. When point is already on that position, then signal an error.
  5012. A near full screen is `next-screen-context-lines' less than a full screen.
  5013. Negative ARG means scroll upward.
  5014. If ARG is the atom `-', scroll upward by nearly full screen."
  5015. (interactive "^P")
  5016. (cond
  5017. ((null scroll-error-top-bottom)
  5018. (scroll-down arg))
  5019. ((eq arg '-)
  5020. (scroll-up-command nil))
  5021. ((< (prefix-numeric-value arg) 0)
  5022. (scroll-up-command (- (prefix-numeric-value arg))))
  5023. ((bobp)
  5024. (scroll-down arg)) ; signal error
  5025. (t
  5026. (condition-case nil
  5027. (scroll-down arg)
  5028. (beginning-of-buffer
  5029. (if arg
  5030. ;; When scrolling by ARG lines can't be done,
  5031. ;; move by ARG lines instead.
  5032. (forward-line (- arg))
  5033. ;; When ARG is nil for full-screen scrolling,
  5034. ;; move to the top of the buffer.
  5035. (goto-char (point-min))))))))
  5036. (put 'scroll-down-command 'scroll-command t)
  5037. ;;; Scrolling commands which scroll a line instead of full screen.
  5038. (defun scroll-up-line (&optional arg)
  5039. "Scroll text of selected window upward ARG lines; or one line if no ARG.
  5040. If ARG is omitted or nil, scroll upward by one line.
  5041. This is different from `scroll-up-command' that scrolls a full screen."
  5042. (interactive "p")
  5043. (scroll-up (or arg 1)))
  5044. (put 'scroll-up-line 'scroll-command t)
  5045. (defun scroll-down-line (&optional arg)
  5046. "Scroll text of selected window down ARG lines; or one line if no ARG.
  5047. If ARG is omitted or nil, scroll down by one line.
  5048. This is different from `scroll-down-command' that scrolls a full screen."
  5049. (interactive "p")
  5050. (scroll-down (or arg 1)))
  5051. (put 'scroll-down-line 'scroll-command t)
  5052. (defun scroll-other-window-down (lines)
  5053. "Scroll the \"other window\" down.
  5054. For more details, see the documentation for `scroll-other-window'."
  5055. (interactive "P")
  5056. (scroll-other-window
  5057. ;; Just invert the argument's meaning.
  5058. ;; We can do that without knowing which window it will be.
  5059. (if (eq lines '-) nil
  5060. (if (null lines) '-
  5061. (- (prefix-numeric-value lines))))))
  5062. (defun beginning-of-buffer-other-window (arg)
  5063. "Move point to the beginning of the buffer in the other window.
  5064. Leave mark at previous position.
  5065. With arg N, put point N/10 of the way from the true beginning."
  5066. (interactive "P")
  5067. (let ((orig-window (selected-window))
  5068. (window (other-window-for-scrolling)))
  5069. ;; We use unwind-protect rather than save-window-excursion
  5070. ;; because the latter would preserve the things we want to change.
  5071. (unwind-protect
  5072. (progn
  5073. (select-window window)
  5074. ;; Set point and mark in that window's buffer.
  5075. (with-no-warnings
  5076. (beginning-of-buffer arg))
  5077. ;; Set point accordingly.
  5078. (recenter '(t)))
  5079. (select-window orig-window))))
  5080. (defun end-of-buffer-other-window (arg)
  5081. "Move point to the end of the buffer in the other window.
  5082. Leave mark at previous position.
  5083. With arg N, put point N/10 of the way from the true end."
  5084. (interactive "P")
  5085. ;; See beginning-of-buffer-other-window for comments.
  5086. (let ((orig-window (selected-window))
  5087. (window (other-window-for-scrolling)))
  5088. (unwind-protect
  5089. (progn
  5090. (select-window window)
  5091. (with-no-warnings
  5092. (end-of-buffer arg))
  5093. (recenter '(t)))
  5094. (select-window orig-window))))
  5095. (defvar mouse-autoselect-window-timer nil
  5096. "Timer used by delayed window autoselection.")
  5097. (defvar mouse-autoselect-window-position nil
  5098. "Last mouse position recorded by delayed window autoselection.")
  5099. (defvar mouse-autoselect-window-window nil
  5100. "Last window recorded by delayed window autoselection.")
  5101. (defvar mouse-autoselect-window-state nil
  5102. "When non-nil, special state of delayed window autoselection.
  5103. Possible values are `suspend' (suspend autoselection after a menu or
  5104. scrollbar interaction) and `select' (the next invocation of
  5105. `handle-select-window' shall select the window immediately).")
  5106. (defun mouse-autoselect-window-cancel (&optional force)
  5107. "Cancel delayed window autoselection.
  5108. Optional argument FORCE means cancel unconditionally."
  5109. (unless (and (not force)
  5110. ;; Don't cancel for select-window or select-frame events
  5111. ;; or when the user drags a scroll bar.
  5112. (or (memq this-command
  5113. '(handle-select-window handle-switch-frame))
  5114. (and (eq this-command 'scroll-bar-toolkit-scroll)
  5115. (memq (nth 4 (event-end last-input-event))
  5116. '(handle end-scroll)))))
  5117. (setq mouse-autoselect-window-state nil)
  5118. (when (timerp mouse-autoselect-window-timer)
  5119. (cancel-timer mouse-autoselect-window-timer))
  5120. (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
  5121. (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
  5122. "Start delayed window autoselection.
  5123. MOUSE-POSITION is the last position where the mouse was seen as returned
  5124. by `mouse-position'. Optional argument WINDOW non-nil denotes the
  5125. window where the mouse was seen. Optional argument SUSPEND non-nil
  5126. means suspend autoselection."
  5127. ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
  5128. (setq mouse-autoselect-window-position mouse-position)
  5129. (when window (setq mouse-autoselect-window-window window))
  5130. (setq mouse-autoselect-window-state (when suspend 'suspend))
  5131. ;; Install timer which runs `mouse-autoselect-window-select' after
  5132. ;; `mouse-autoselect-window' seconds.
  5133. (setq mouse-autoselect-window-timer
  5134. (run-at-time
  5135. (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
  5136. (defun mouse-autoselect-window-select ()
  5137. "Select window with delayed window autoselection.
  5138. If the mouse position has stabilized in a non-selected window, select
  5139. that window. The minibuffer window is selected only if the minibuffer
  5140. is active. This function is run by `mouse-autoselect-window-timer'."
  5141. (ignore-errors
  5142. (let* ((mouse-position (mouse-position))
  5143. (window
  5144. (ignore-errors
  5145. (window-at (cadr mouse-position) (cddr mouse-position)
  5146. (car mouse-position)))))
  5147. (cond
  5148. ((or (menu-or-popup-active-p)
  5149. (and window
  5150. (not (coordinates-in-window-p (cdr mouse-position) window))))
  5151. ;; A menu / popup dialog is active or the mouse is on the scroll-bar
  5152. ;; of WINDOW, temporarily suspend delayed autoselection.
  5153. (mouse-autoselect-window-start mouse-position nil t))
  5154. ((eq mouse-autoselect-window-state 'suspend)
  5155. ;; Delayed autoselection was temporarily suspended, reenable it.
  5156. (mouse-autoselect-window-start mouse-position))
  5157. ((and window (not (eq window (selected-window)))
  5158. (or (not (numberp mouse-autoselect-window))
  5159. (and (> mouse-autoselect-window 0)
  5160. ;; If `mouse-autoselect-window' is positive, select
  5161. ;; window if the window is the same as before.
  5162. (eq window mouse-autoselect-window-window))
  5163. ;; Otherwise select window if the mouse is at the same
  5164. ;; position as before. Observe that the first test after
  5165. ;; starting autoselection usually fails since the value of
  5166. ;; `mouse-autoselect-window-position' recorded there is the
  5167. ;; position where the mouse has entered the new window and
  5168. ;; not necessarily where the mouse has stopped moving.
  5169. (equal mouse-position mouse-autoselect-window-position))
  5170. ;; The minibuffer is a candidate window if it's active.
  5171. (or (not (window-minibuffer-p window))
  5172. (eq window (active-minibuffer-window))))
  5173. ;; Mouse position has stabilized in non-selected window: Cancel
  5174. ;; delayed autoselection and try to select that window.
  5175. (mouse-autoselect-window-cancel t)
  5176. ;; Select window where mouse appears unless the selected window is the
  5177. ;; minibuffer. Use `unread-command-events' in order to execute pre-
  5178. ;; and post-command hooks and trigger idle timers. To avoid delaying
  5179. ;; autoselection again, set `mouse-autoselect-window-state'."
  5180. (unless (window-minibuffer-p (selected-window))
  5181. (setq mouse-autoselect-window-state 'select)
  5182. (setq unread-command-events
  5183. (cons (list 'select-window (list window))
  5184. unread-command-events))))
  5185. ((or (and window (eq window (selected-window)))
  5186. (not (numberp mouse-autoselect-window))
  5187. (equal mouse-position mouse-autoselect-window-position))
  5188. ;; Mouse position has either stabilized in the selected window or at
  5189. ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
  5190. (mouse-autoselect-window-cancel t))
  5191. (t
  5192. ;; Mouse position has not stabilized yet, resume delayed
  5193. ;; autoselection.
  5194. (mouse-autoselect-window-start mouse-position window))))))
  5195. (defun handle-select-window (event)
  5196. "Handle select-window events."
  5197. (interactive "e")
  5198. (let ((window (posn-window (event-start event))))
  5199. (unless (or (not (window-live-p window))
  5200. ;; Don't switch if we're currently in the minibuffer.
  5201. ;; This tries to work around problems where the
  5202. ;; minibuffer gets unselected unexpectedly, and where
  5203. ;; you then have to move your mouse all the way down to
  5204. ;; the minibuffer to select it.
  5205. (window-minibuffer-p (selected-window))
  5206. ;; Don't switch to minibuffer window unless it's active.
  5207. (and (window-minibuffer-p window)
  5208. (not (minibuffer-window-active-p window)))
  5209. ;; Don't switch when autoselection shall be delayed.
  5210. (and (numberp mouse-autoselect-window)
  5211. (not (zerop mouse-autoselect-window))
  5212. (not (eq mouse-autoselect-window-state 'select))
  5213. (progn
  5214. ;; Cancel any delayed autoselection.
  5215. (mouse-autoselect-window-cancel t)
  5216. ;; Start delayed autoselection from current mouse
  5217. ;; position and window.
  5218. (mouse-autoselect-window-start (mouse-position) window)
  5219. ;; Executing a command cancels delayed autoselection.
  5220. (add-hook
  5221. 'pre-command-hook 'mouse-autoselect-window-cancel))))
  5222. (when mouse-autoselect-window
  5223. ;; Reset state of delayed autoselection.
  5224. (setq mouse-autoselect-window-state nil)
  5225. ;; Run `mouse-leave-buffer-hook' when autoselecting window.
  5226. (run-hooks 'mouse-leave-buffer-hook))
  5227. (select-window window))))
  5228. (defun truncated-partial-width-window-p (&optional window)
  5229. "Return non-nil if lines in WINDOW are specifically truncated due to its width.
  5230. WINDOW defaults to the selected window.
  5231. Return nil if WINDOW is not a partial-width window
  5232. (regardless of the value of `truncate-lines').
  5233. Otherwise, consult the value of `truncate-partial-width-windows'
  5234. for the buffer shown in WINDOW."
  5235. (unless window
  5236. (setq window (selected-window)))
  5237. (unless (window-full-width-p window)
  5238. (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
  5239. (window-buffer window))))
  5240. (if (integerp t-p-w-w)
  5241. (< (window-width window) t-p-w-w)
  5242. t-p-w-w))))
  5243. ;; Some of these are in tutorial--default-keys, so update that if you
  5244. ;; change these.
  5245. (define-key ctl-x-map "0" 'delete-window)
  5246. (define-key ctl-x-map "1" 'delete-other-windows)
  5247. (define-key ctl-x-map "2" 'split-window-below)
  5248. (define-key ctl-x-map "3" 'split-window-right)
  5249. (define-key ctl-x-map "o" 'other-window)
  5250. (define-key ctl-x-map "^" 'enlarge-window)
  5251. (define-key ctl-x-map "}" 'enlarge-window-horizontally)
  5252. (define-key ctl-x-map "{" 'shrink-window-horizontally)
  5253. (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
  5254. (define-key ctl-x-map "+" 'balance-windows)
  5255. (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
  5256. ;;; window.el ends here