window.el 332 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402
  1. ;;; window.el --- GNU Emacs window commands aside from those written in C
  2. ;; Copyright (C) 1985, 1989, 1992-1994, 2000-2015 Free Software
  3. ;; Foundation, Inc.
  4. ;; Maintainer: emacs-devel@gnu.org
  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. (defun internal--before-save-selected-window ()
  22. (cons (selected-window)
  23. ;; We save and restore all frames' selected windows, because
  24. ;; `select-window' can change the frame-selected-window of
  25. ;; whatever frame that window is in. Each text terminal's
  26. ;; top-frame is preserved by putting it last in the list.
  27. (apply #'append
  28. (mapcar (lambda (terminal)
  29. (let ((frames (frames-on-display-list terminal))
  30. (top-frame (tty-top-frame terminal))
  31. alist)
  32. (if top-frame
  33. (setq frames
  34. (cons top-frame
  35. (delq top-frame frames))))
  36. (dolist (f frames)
  37. (push (cons f (frame-selected-window f))
  38. alist))
  39. alist))
  40. (terminal-list)))))
  41. (defun internal--after-save-selected-window (state)
  42. (dolist (elt (cdr state))
  43. (and (frame-live-p (car elt))
  44. (window-live-p (cdr elt))
  45. (set-frame-selected-window (car elt) (cdr elt) 'norecord)))
  46. (when (window-live-p (car state))
  47. (select-window (car state) 'norecord)))
  48. (defmacro save-selected-window (&rest body)
  49. "Execute BODY, then select the previously selected window.
  50. The value returned is the value of the last form in BODY.
  51. This macro saves and restores the selected window, as well as the
  52. selected window in each frame. If the previously selected window
  53. is no longer live, then whatever window is selected at the end of
  54. BODY remains selected. If the previously selected window of some
  55. frame is no longer live at the end of BODY, that frame's selected
  56. window is left alone.
  57. This macro saves and restores the current buffer, since otherwise
  58. its normal operation could make a different buffer current. The
  59. order of recently selected windows and the buffer list ordering
  60. are not altered by this macro (unless they are altered in BODY)."
  61. (declare (indent 0) (debug t))
  62. `(let ((save-selected-window--state (internal--before-save-selected-window)))
  63. (save-current-buffer
  64. (unwind-protect
  65. (progn ,@body)
  66. (internal--after-save-selected-window save-selected-window--state)))))
  67. (defvar temp-buffer-window-setup-hook nil
  68. "Normal hook run by `with-temp-buffer-window' before buffer display.
  69. This hook is run by `with-temp-buffer-window' with the buffer to be
  70. displayed current.")
  71. (defvar temp-buffer-window-show-hook nil
  72. "Normal hook run by `with-temp-buffer-window' after buffer display.
  73. This hook is run by `with-temp-buffer-window' with the buffer
  74. displayed and current and its window selected.")
  75. (defun temp-buffer-window-setup (buffer-or-name)
  76. "Set up temporary buffer specified by BUFFER-OR-NAME.
  77. Return the buffer."
  78. (let ((old-dir default-directory)
  79. (buffer (get-buffer-create buffer-or-name)))
  80. (with-current-buffer buffer
  81. (kill-all-local-variables)
  82. (setq default-directory old-dir)
  83. (delete-all-overlays)
  84. (setq buffer-read-only nil)
  85. (setq buffer-file-name nil)
  86. (setq buffer-undo-list t)
  87. (let ((inhibit-read-only t)
  88. (inhibit-modification-hooks t))
  89. (erase-buffer)
  90. (run-hooks 'temp-buffer-window-setup-hook))
  91. ;; Return the buffer.
  92. buffer)))
  93. (defun temp-buffer-window-show (buffer &optional action)
  94. "Show temporary buffer BUFFER in a window.
  95. Return the window showing BUFFER. Pass ACTION as action argument
  96. to `display-buffer'."
  97. (let (window frame)
  98. (with-current-buffer buffer
  99. (set-buffer-modified-p nil)
  100. (setq buffer-read-only t)
  101. (goto-char (point-min))
  102. (when (let ((window-combination-limit
  103. ;; When `window-combination-limit' equals
  104. ;; `temp-buffer' or `temp-buffer-resize' and
  105. ;; `temp-buffer-resize-mode' is enabled in this
  106. ;; buffer bind it to t so resizing steals space
  107. ;; preferably from the window that was split.
  108. (if (or (eq window-combination-limit 'temp-buffer)
  109. (and (eq window-combination-limit
  110. 'temp-buffer-resize)
  111. temp-buffer-resize-mode))
  112. t
  113. window-combination-limit)))
  114. (setq window (display-buffer buffer action)))
  115. (setq frame (window-frame window))
  116. (unless (eq frame (selected-frame))
  117. (raise-frame frame))
  118. (setq minibuffer-scroll-window window)
  119. (set-window-hscroll window 0)
  120. (with-selected-window window
  121. (run-hooks 'temp-buffer-window-show-hook)
  122. (when temp-buffer-resize-mode
  123. (resize-temp-buffer-window window)))
  124. ;; Return the window.
  125. window))))
  126. (defmacro with-temp-buffer-window (buffer-or-name action quit-function &rest body)
  127. "Bind `standard-output' to BUFFER-OR-NAME, eval BODY, show the buffer.
  128. BUFFER-OR-NAME must specify either a live buffer, or the name of
  129. a buffer (if it does not exist, this macro creates it).
  130. Make the buffer specified by BUFFER-OR-NAME empty before running
  131. BODY and bind `standard-output' to that buffer, so that output
  132. generated with `prin1' and similar functions in BODY goes into
  133. that buffer. Do not make that buffer current for running the
  134. forms in BODY. Use `with-current-buffer-window' instead if you
  135. need to run BODY with that buffer current.
  136. At the end of BODY, mark the specified buffer unmodified and
  137. read-only, and display it in a window (but do not select it).
  138. The display happens by calling `display-buffer' passing it the
  139. ACTION argument. If `temp-buffer-resize-mode' is enabled, the
  140. corresponding window may be resized automatically.
  141. Return the value returned by BODY, unless QUIT-FUNCTION specifies
  142. a function. In that case, run that function with two arguments -
  143. the window showing the specified buffer and the value returned by
  144. BODY - and return the value returned by that function.
  145. If the buffer is displayed on a new frame, the window manager may
  146. decide to select that frame. In that case, it's usually a good
  147. strategy if QUIT-FUNCTION selects the window showing the buffer
  148. before reading any value from the minibuffer; for example, when
  149. asking a `yes-or-no-p' question.
  150. This runs the hook `temp-buffer-window-setup-hook' before BODY,
  151. with the specified buffer temporarily current. It runs the hook
  152. `temp-buffer-window-show-hook' after displaying the buffer, with
  153. that buffer temporarily current, and the window that was used to
  154. display it temporarily selected.
  155. This construct is similar to `with-output-to-temp-buffer' but,
  156. neither runs `temp-buffer-setup-hook' which usually puts the
  157. buffer in Help mode, nor `temp-buffer-show-function' (the ACTION
  158. argument replaces this)."
  159. (declare (debug t))
  160. (let ((buffer (make-symbol "buffer"))
  161. (window (make-symbol "window"))
  162. (value (make-symbol "value")))
  163. (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
  164. (vaction action)
  165. (vquit-function quit-function))
  166. `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
  167. (standard-output ,buffer)
  168. ,window ,value)
  169. (setq ,value (progn ,@body))
  170. (with-current-buffer ,buffer
  171. (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
  172. (if (functionp ,vquit-function)
  173. (funcall ,vquit-function ,window ,value)
  174. ,value)))))
  175. (defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
  176. "Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
  177. This construct is like `with-temp-buffer-window' but unlike that
  178. makes the buffer specified by BUFFER-OR-NAME current for running
  179. BODY."
  180. (declare (debug t))
  181. (let ((buffer (make-symbol "buffer"))
  182. (window (make-symbol "window"))
  183. (value (make-symbol "value")))
  184. (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
  185. (vaction action)
  186. (vquit-function quit-function))
  187. `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
  188. (standard-output ,buffer)
  189. ,window ,value)
  190. (with-current-buffer ,buffer
  191. (setq ,value (progn ,@body))
  192. (setq ,window (temp-buffer-window-show ,buffer ,vaction)))
  193. (if (functionp ,vquit-function)
  194. (funcall ,vquit-function ,window ,value)
  195. ,value)))))
  196. (defmacro with-displayed-buffer-window (buffer-or-name action quit-function &rest body)
  197. "Show a buffer BUFFER-OR-NAME and evaluate BODY in that buffer.
  198. This construct is like `with-current-buffer-window' but unlike that
  199. displays the buffer specified by BUFFER-OR-NAME before running BODY."
  200. (declare (debug t))
  201. (let ((buffer (make-symbol "buffer"))
  202. (window (make-symbol "window"))
  203. (value (make-symbol "value")))
  204. (macroexp-let2* nil ((vbuffer-or-name buffer-or-name)
  205. (vaction action)
  206. (vquit-function quit-function))
  207. `(let* ((,buffer (temp-buffer-window-setup ,vbuffer-or-name))
  208. (standard-output ,buffer)
  209. ,window ,value)
  210. (with-current-buffer ,buffer
  211. (setq ,window (temp-buffer-window-show
  212. ,buffer
  213. ;; Remove window-height when it's handled below.
  214. (if (functionp (cdr (assq 'window-height (cdr ,vaction))))
  215. (assq-delete-all 'window-height (copy-sequence ,vaction))
  216. ,vaction))))
  217. (let ((inhibit-read-only t)
  218. (inhibit-modification-hooks t))
  219. (setq ,value (progn ,@body)))
  220. (set-window-point ,window (point-min))
  221. (when (functionp (cdr (assq 'window-height (cdr ,vaction))))
  222. (ignore-errors
  223. (funcall (cdr (assq 'window-height (cdr ,vaction))) ,window)))
  224. (when (consp (cdr (assq 'preserve-size (cdr ,vaction))))
  225. (window-preserve-size
  226. ,window t (cadr (assq 'preserve-size (cdr ,vaction))))
  227. (window-preserve-size
  228. ,window nil (cddr (assq 'preserve-size (cdr ,vaction)))))
  229. (if (functionp ,vquit-function)
  230. (funcall ,vquit-function ,window ,value)
  231. ,value)))))
  232. ;; The following two functions are like `window-next-sibling' and
  233. ;; `window-prev-sibling' but the WINDOW argument is _not_ optional (so
  234. ;; they don't substitute the selected window for nil), and they return
  235. ;; nil when WINDOW doesn't have a parent (like a frame's root window or
  236. ;; a minibuffer window).
  237. (defun window-right (window)
  238. "Return WINDOW's right sibling.
  239. Return nil if WINDOW is the root window of its frame. WINDOW can
  240. be any window."
  241. (and window (window-parent window) (window-next-sibling window)))
  242. (defun window-left (window)
  243. "Return WINDOW's left sibling.
  244. Return nil if WINDOW is the root window of its frame. WINDOW can
  245. be any window."
  246. (and window (window-parent window) (window-prev-sibling window)))
  247. (defun window-child (window)
  248. "Return WINDOW's first child window.
  249. WINDOW can be any window."
  250. (or (window-top-child window) (window-left-child window)))
  251. (defun window-child-count (window)
  252. "Return number of WINDOW's child windows.
  253. WINDOW can be any window."
  254. (let ((count 0))
  255. (when (and (windowp window) (setq window (window-child window)))
  256. (while window
  257. (setq count (1+ count))
  258. (setq window (window-next-sibling window))))
  259. count))
  260. (defun window-last-child (window)
  261. "Return last child window of WINDOW.
  262. WINDOW can be any window."
  263. (when (and (windowp window) (setq window (window-child window)))
  264. (while (window-next-sibling window)
  265. (setq window (window-next-sibling window))))
  266. window)
  267. (defun window-normalize-buffer (buffer-or-name)
  268. "Return buffer specified by BUFFER-OR-NAME.
  269. BUFFER-OR-NAME must be either a buffer or a string naming a live
  270. buffer and defaults to the current buffer."
  271. (cond
  272. ((not buffer-or-name)
  273. (current-buffer))
  274. ((bufferp buffer-or-name)
  275. (if (buffer-live-p buffer-or-name)
  276. buffer-or-name
  277. (error "Buffer %s is not a live buffer" buffer-or-name)))
  278. ((get-buffer buffer-or-name))
  279. (t
  280. (error "No such buffer %s" buffer-or-name))))
  281. (defun window-normalize-frame (frame)
  282. "Return frame specified by FRAME.
  283. FRAME must be a live frame and defaults to the selected frame."
  284. (if frame
  285. (if (frame-live-p frame)
  286. frame
  287. (error "%s is not a live frame" frame))
  288. (selected-frame)))
  289. (defun window-normalize-window (window &optional live-only)
  290. "Return the window specified by WINDOW.
  291. If WINDOW is nil, return the selected window. Otherwise, if
  292. WINDOW is a live or an internal window, return WINDOW; if
  293. LIVE-ONLY is non-nil, return WINDOW for a live window only.
  294. Otherwise, signal an error."
  295. (cond
  296. ((null window)
  297. (selected-window))
  298. (live-only
  299. (if (window-live-p window)
  300. window
  301. (error "%s is not a live window" window)))
  302. ((window-valid-p window)
  303. window)
  304. (t
  305. (error "%s is not a valid window" window))))
  306. ;; Maybe this should go to frame.el.
  307. (defun frame-char-size (&optional window-or-frame horizontal)
  308. "Return the value of `frame-char-height' for WINDOW-OR-FRAME.
  309. If WINDOW-OR-FRAME is a live frame, return the value of
  310. `frame-char-height' for that frame. If WINDOW-OR-FRAME is a
  311. valid window, return the value of `frame-char-height' for that
  312. window's frame. In any other case, return the value of
  313. `frame-char-height' for the selected frame.
  314. Optional argument HORIZONTAL non-nil means to return the value of
  315. `frame-char-width' for WINDOW-OR-FRAME."
  316. (let ((frame
  317. (cond
  318. ((window-valid-p window-or-frame)
  319. (window-frame window-or-frame))
  320. ((frame-live-p window-or-frame)
  321. window-or-frame)
  322. (t (selected-frame)))))
  323. (if horizontal
  324. (frame-char-width frame)
  325. (frame-char-height frame))))
  326. (defvar ignore-window-parameters nil
  327. "If non-nil, standard functions ignore window parameters.
  328. The functions currently affected by this are `split-window',
  329. `delete-window', `delete-other-windows' and `other-window'.
  330. An application may bind this to a non-nil value around calls to
  331. these functions to inhibit processing of window parameters.")
  332. ;; This must go to C, finally (or get removed).
  333. (defconst window-safe-min-height 1
  334. "The absolute minimum number of lines of any window.
  335. Anything less might crash Emacs.")
  336. (defun window-safe-min-pixel-height (&optional window)
  337. "Return the absolute minimum pixel height of WINDOW."
  338. (* window-safe-min-height
  339. (frame-char-size (window-normalize-window window))))
  340. (defcustom window-min-height 4
  341. "The minimum total height, in lines, of any window.
  342. The value has to accommodate one text line, a mode and header
  343. line, a horizontal scroll bar and a bottom divider, if present.
  344. A value less than `window-safe-min-height' is ignored. The value
  345. of this variable is honored when windows are resized or split.
  346. Applications should never rebind this variable. To resize a
  347. window to a height less than the one specified here, an
  348. application should instead call `window-resize' with a non-nil
  349. IGNORE argument. In order to have `split-window' make a window
  350. shorter, explicitly specify the SIZE argument of that function."
  351. :type 'integer
  352. :version "24.1"
  353. :group 'windows)
  354. (defun window-min-pixel-height (&optional window)
  355. "Return the minimum pixel height of window WINDOW."
  356. (* (max window-min-height window-safe-min-height)
  357. (frame-char-size window)))
  358. ;; This must go to C, finally (or get removed).
  359. (defconst window-safe-min-width 2
  360. "The absolute minimum number of columns of a window.
  361. Anything less might crash Emacs.")
  362. (defun window-safe-min-pixel-width (&optional window)
  363. "Return the absolute minimum pixel width of WINDOW."
  364. (* window-safe-min-width
  365. (frame-char-size (window-normalize-window window) t)))
  366. (defcustom window-min-width 10
  367. "The minimum total width, in columns, of any window.
  368. The value has to accommodate two text columns as well as margins,
  369. fringes, a scroll bar and a right divider, if present. A value
  370. less than `window-safe-min-width' is ignored. The value of this
  371. variable is honored when windows are resized or split.
  372. Applications should never rebind this variable. To resize a
  373. window to a width less than the one specified here, an
  374. application should instead call `window-resize' with a non-nil
  375. IGNORE argument. In order to have `split-window' make a window
  376. narrower, explicitly specify the SIZE argument of that function."
  377. :type 'integer
  378. :version "24.1"
  379. :group 'windows)
  380. (defun window-min-pixel-width (&optional window)
  381. "Return the minimum pixel width of window WINDOW."
  382. (* (max window-min-width window-safe-min-width)
  383. (frame-char-size window t)))
  384. (defun window-safe-min-pixel-size (&optional window horizontal)
  385. "Return the absolute minimum pixel height of WINDOW.
  386. Optional argument HORIZONTAL non-nil means return the absolute
  387. minimum pixel width of WINDOW."
  388. (if horizontal
  389. (window-safe-min-pixel-width window)
  390. (window-safe-min-pixel-height window)))
  391. (defun window-min-pixel-size (&optional window horizontal)
  392. "Return the minimum pixel height of WINDOW.
  393. Optional argument HORIZONTAL non-nil means return the minimum
  394. pixel width of WINDOW."
  395. (if horizontal
  396. (window-min-pixel-width window)
  397. (window-min-pixel-height window)))
  398. (defun window-combined-p (&optional window horizontal)
  399. "Return non-nil if WINDOW has siblings in a given direction.
  400. WINDOW must be a valid window and defaults to the selected one.
  401. HORIZONTAL determines a direction for the window combination. If
  402. HORIZONTAL is omitted or nil, return non-nil if WINDOW is part of
  403. a vertical window combination. If HORIZONTAL is non-nil, return
  404. non-nil if WINDOW is part of a horizontal window combination."
  405. (setq window (window-normalize-window window))
  406. (let ((parent (window-parent window)))
  407. (and parent
  408. (if horizontal
  409. (window-left-child parent)
  410. (window-top-child parent)))))
  411. (defun window-combination-p (&optional window horizontal)
  412. "Return WINDOW's first child if WINDOW is a vertical combination.
  413. WINDOW can be any window and defaults to the selected one.
  414. Optional argument HORIZONTAL non-nil means return WINDOW's first
  415. child if WINDOW is a horizontal combination."
  416. (setq window (window-normalize-window window))
  417. (if horizontal
  418. (window-left-child window)
  419. (window-top-child window)))
  420. (defun window-combinations (window &optional horizontal)
  421. "Return largest number of windows vertically arranged within WINDOW.
  422. WINDOW must be a valid window and defaults to the selected one.
  423. If HORIZONTAL is non-nil, return the largest number of
  424. windows horizontally arranged within WINDOW."
  425. (setq window (window-normalize-window window))
  426. (cond
  427. ((window-live-p window)
  428. ;; If WINDOW is live, return 1.
  429. 1)
  430. ((if horizontal
  431. (window-left-child window)
  432. (window-top-child window))
  433. ;; If WINDOW is iso-combined, return the sum of the values for all
  434. ;; child windows of WINDOW.
  435. (let ((child (window-child window))
  436. (count 0))
  437. (while child
  438. (setq count
  439. (+ (window-combinations child horizontal)
  440. count))
  441. (setq child (window-right child)))
  442. count))
  443. (t
  444. ;; If WINDOW is not iso-combined, return the maximum value of any
  445. ;; child window of WINDOW.
  446. (let ((child (window-child window))
  447. (count 1))
  448. (while child
  449. (setq count
  450. (max (window-combinations child horizontal)
  451. count))
  452. (setq child (window-right child)))
  453. count))))
  454. (defun walk-window-tree-1 (fun walk-window-tree-window any &optional sub-only)
  455. "Helper function for `walk-window-tree' and `walk-window-subtree'."
  456. (let (walk-window-tree-buffer)
  457. (while walk-window-tree-window
  458. (setq walk-window-tree-buffer
  459. (window-buffer walk-window-tree-window))
  460. (when (or walk-window-tree-buffer any)
  461. (funcall fun walk-window-tree-window))
  462. (unless walk-window-tree-buffer
  463. (walk-window-tree-1
  464. fun (window-left-child walk-window-tree-window) any)
  465. (walk-window-tree-1
  466. fun (window-top-child walk-window-tree-window) any))
  467. (if sub-only
  468. (setq walk-window-tree-window nil)
  469. (setq walk-window-tree-window
  470. (window-right walk-window-tree-window))))))
  471. (defun walk-window-tree (fun &optional frame any minibuf)
  472. "Run function FUN on each live window of FRAME.
  473. FUN must be a function with one argument - a window. FRAME must
  474. be a live frame and defaults to the selected one. ANY, if
  475. non-nil, means to run FUN on all live and internal windows of
  476. FRAME.
  477. Optional argument MINIBUF t means run FUN on FRAME's minibuffer
  478. window even if it isn't active. MINIBUF nil or omitted means run
  479. FUN on FRAME's minibuffer window only if it's active. In both
  480. cases the minibuffer window must be part of FRAME. MINIBUF
  481. neither nil nor t means never run FUN on the minibuffer window.
  482. This function performs a pre-order, depth-first traversal of the
  483. window tree. If FUN changes the window tree, the result is
  484. unpredictable."
  485. (setq frame (window-normalize-frame frame))
  486. (walk-window-tree-1 fun (frame-root-window frame) any)
  487. (when (memq minibuf '(nil t))
  488. ;; Run FUN on FRAME's minibuffer window if requested.
  489. (let ((minibuffer-window (minibuffer-window frame)))
  490. (when (and (window-live-p minibuffer-window)
  491. (eq (window-frame minibuffer-window) frame)
  492. (or (eq minibuf t)
  493. (minibuffer-window-active-p minibuffer-window)))
  494. (funcall fun minibuffer-window)))))
  495. (defun walk-window-subtree (fun &optional window any)
  496. "Run function FUN on the subtree of windows rooted at WINDOW.
  497. WINDOW defaults to the selected window. FUN must be a function
  498. with one argument - a window. By default, run FUN only on live
  499. windows of the subtree. If the optional argument ANY is non-nil,
  500. run FUN on all live and internal windows of the subtree. If
  501. WINDOW is live, run FUN on WINDOW only.
  502. This function performs a pre-order, depth-first traversal of the
  503. subtree rooted at WINDOW. If FUN changes that tree, the result
  504. is unpredictable."
  505. (setq window (window-normalize-window window))
  506. (walk-window-tree-1 fun window any t))
  507. (defun window-with-parameter (parameter &optional value frame any minibuf)
  508. "Return first window on FRAME with PARAMETER non-nil.
  509. FRAME defaults to the selected frame. Optional argument VALUE
  510. non-nil means only return a window whose window-parameter value
  511. for PARAMETER equals VALUE (comparison is done with `equal').
  512. Optional argument ANY non-nil means consider internal windows
  513. too.
  514. Optional argument MINIBUF t means consider FRAME's minibuffer
  515. window even if it isn't active. MINIBUF nil or omitted means
  516. consider FRAME's minibuffer window only if it's active. In both
  517. cases the minibuffer window must be part of FRAME. MINIBUF
  518. neither nil nor t means never consider the minibuffer window."
  519. (let (this-value)
  520. (catch 'found
  521. (walk-window-tree
  522. (lambda (window)
  523. (when (and (setq this-value (window-parameter window parameter))
  524. (or (not value) (equal value this-value)))
  525. (throw 'found window)))
  526. frame any minibuf))))
  527. ;;; Atomic windows.
  528. (defun window-atom-root (&optional window)
  529. "Return root of atomic window WINDOW is a part of.
  530. WINDOW must be a valid window and defaults to the selected one.
  531. Return nil if WINDOW is not part of an atomic window."
  532. (setq window (window-normalize-window window))
  533. (let (root)
  534. (while (and window (window-parameter window 'window-atom))
  535. (setq root window)
  536. (setq window (window-parent window)))
  537. root))
  538. (defun window-make-atom (window)
  539. "Make WINDOW an atomic window.
  540. WINDOW must be an internal window. Return WINDOW."
  541. (if (not (window-child window))
  542. (error "Window %s is not an internal window" window)
  543. (walk-window-subtree
  544. (lambda (window)
  545. (unless (window-parameter window 'window-atom)
  546. (set-window-parameter window 'window-atom t)))
  547. window t)
  548. window))
  549. (defun display-buffer-in-atom-window (buffer alist)
  550. "Display BUFFER in an atomic window.
  551. This function displays BUFFER in a new window that will be
  552. combined with an existing window to form an atomic window. If
  553. the existing window is already part of an atomic window, add the
  554. new window to that atomic window. Operations like `split-window'
  555. or `delete-window', when applied to a constituent of an atomic
  556. window, are applied atomically to the root of that atomic window.
  557. ALIST is an association list of symbols and values. The
  558. following symbols can be used.
  559. `window' specifies the existing window the new window shall be
  560. combined with. Use `window-atom-root' to make the new window a
  561. sibling of an atomic window's root. If an internal window is
  562. specified here, all children of that window become part of the
  563. atomic window too. If no window is specified, the new window
  564. becomes a sibling of the selected window. By default, the
  565. `window-atom' parameter of the existing window is set to `main'
  566. provided it is live and was not set before.
  567. `side' denotes the side of the existing window where the new
  568. window shall be located. Valid values are `below', `right',
  569. `above' and `left'. The default is `below'. By default, the
  570. `window-atom' parameter of the new window is set to this value.
  571. The return value is the new window, nil when creating that window
  572. failed."
  573. (let* ((ignore-window-parameters t)
  574. (window-combination-limit t)
  575. (window-combination-resize 'atom)
  576. (window (cdr (assq 'window alist)))
  577. (side (cdr (assq 'side alist)))
  578. (atom (when window (window-parameter window 'window-atom)))
  579. root new)
  580. (setq window (window-normalize-window window))
  581. (setq root (window-atom-root window))
  582. ;; Split off new window.
  583. (when (setq new (split-window window nil side))
  584. (window-make-atom
  585. (if (and root (not (eq root window)))
  586. ;; When WINDOW was part of an atomic window and we did not
  587. ;; split its root, root atomic window at old root.
  588. root
  589. ;; Otherwise, root atomic window at WINDOW's new parent.
  590. (window-parent window)))
  591. ;; Assign `window-atom' parameters, if needed.
  592. (when (and (not atom) (window-live-p window))
  593. (set-window-parameter window 'window-atom 'main))
  594. (set-window-parameter new 'window-atom side)
  595. ;; Display BUFFER in NEW and return NEW.
  596. (window--display-buffer
  597. buffer new 'window alist display-buffer-mark-dedicated))))
  598. (defun window--atom-check-1 (window)
  599. "Subroutine of `window--atom-check'."
  600. (when window
  601. (if (window-parameter window 'window-atom)
  602. (let ((count 0))
  603. (when (or (catch 'reset
  604. (walk-window-subtree
  605. (lambda (window)
  606. (if (window-parameter window 'window-atom)
  607. (setq count (1+ count))
  608. (throw 'reset t)))
  609. window t))
  610. ;; count >= 1 must hold here. If there's no other
  611. ;; window around dissolve this atomic window.
  612. (= count 1))
  613. ;; Dissolve atomic window.
  614. (walk-window-subtree
  615. (lambda (window)
  616. (set-window-parameter window 'window-atom nil))
  617. window t)))
  618. ;; Check children.
  619. (unless (window-buffer window)
  620. (window--atom-check-1 (window-left-child window))
  621. (window--atom-check-1 (window-top-child window))))
  622. ;; Check right sibling
  623. (window--atom-check-1 (window-right window))))
  624. (defun window--atom-check (&optional frame)
  625. "Check atomicity of all windows on FRAME.
  626. FRAME defaults to the selected frame. If an atomic window is
  627. wrongly configured, reset the atomicity of all its windows on
  628. FRAME to nil. An atomic window is wrongly configured if it has
  629. no child windows or one of its child windows is not atomic."
  630. (window--atom-check-1 (frame-root-window frame)))
  631. ;; Side windows.
  632. (defvar window-sides '(left top right bottom)
  633. "Window sides.")
  634. (defcustom window-sides-vertical nil
  635. "If non-nil, left and right side windows are full height.
  636. Otherwise, top and bottom side windows are full width."
  637. :type 'boolean
  638. :group 'windows
  639. :version "24.1")
  640. (defcustom window-sides-slots '(nil nil nil nil)
  641. "Maximum number of side window slots.
  642. The value is a list of four elements specifying the number of
  643. side window slots on (in this order) the left, top, right and
  644. bottom side of each frame. If an element is a number, this means
  645. to display at most that many side windows on the corresponding
  646. side. If an element is nil, this means there's no bound on the
  647. number of slots on that side."
  648. :version "24.1"
  649. :risky t
  650. :type
  651. '(list
  652. :value (nil nil nil nil)
  653. (choice
  654. :tag "Left"
  655. :help-echo "Maximum slots of left side window."
  656. :value nil
  657. :format "%[Left%] %v\n"
  658. (const :tag "Unlimited" :format "%t" nil)
  659. (integer :tag "Number" :value 2 :size 5))
  660. (choice
  661. :tag "Top"
  662. :help-echo "Maximum slots of top side window."
  663. :value nil
  664. :format "%[Top%] %v\n"
  665. (const :tag "Unlimited" :format "%t" nil)
  666. (integer :tag "Number" :value 3 :size 5))
  667. (choice
  668. :tag "Right"
  669. :help-echo "Maximum slots of right side window."
  670. :value nil
  671. :format "%[Right%] %v\n"
  672. (const :tag "Unlimited" :format "%t" nil)
  673. (integer :tag "Number" :value 2 :size 5))
  674. (choice
  675. :tag "Bottom"
  676. :help-echo "Maximum slots of bottom side window."
  677. :value nil
  678. :format "%[Bottom%] %v\n"
  679. (const :tag "Unlimited" :format "%t" nil)
  680. (integer :tag "Number" :value 3 :size 5)))
  681. :group 'windows)
  682. (defun window--side-window-p (window)
  683. "Return non-nil if WINDOW is a side window or the parent of one."
  684. (or (window-parameter window 'window-side)
  685. (and (window-child window)
  686. (or (window-parameter
  687. (window-child window) 'window-side)
  688. (window-parameter
  689. (window-last-child window) 'window-side)))))
  690. (defun window--major-non-side-window (&optional frame)
  691. "Return the major non-side window of frame FRAME.
  692. The optional argument FRAME must be a live frame and defaults to
  693. the selected one.
  694. If FRAME has at least one side window, the major non-side window
  695. is either an internal non-side window such that all other
  696. non-side windows on FRAME descend from it, or the single live
  697. non-side window of FRAME. If FRAME has no side windows, return
  698. its root window."
  699. (let ((frame (window-normalize-frame frame))
  700. major sibling)
  701. ;; Set major to the _last_ window found by `walk-window-tree' that
  702. ;; is not a side window but has a side window as its sibling.
  703. (walk-window-tree
  704. (lambda (window)
  705. (and (not (window-parameter window 'window-side))
  706. (or (and (setq sibling (window-prev-sibling window))
  707. (window-parameter sibling 'window-side))
  708. (and (setq sibling (window-next-sibling window))
  709. (window-parameter sibling 'window-side)))
  710. (setq major window)))
  711. frame t 'nomini)
  712. (or major (frame-root-window frame))))
  713. (defun window--major-side-window (side)
  714. "Return major side window on SIDE.
  715. SIDE must be one of the symbols `left', `top', `right' or
  716. `bottom'. Return nil if no such window exists."
  717. (let ((root (frame-root-window))
  718. window)
  719. ;; (1) If a window on the opposite side exists, return that window's
  720. ;; sibling.
  721. ;; (2) If the new window shall span the entire side, return the
  722. ;; frame's root window.
  723. ;; (3) If a window on an orthogonal side exists, return that
  724. ;; window's sibling.
  725. ;; (4) Otherwise return the frame's root window.
  726. (cond
  727. ((or (and (eq side 'left)
  728. (setq window (window-with-parameter 'window-side 'right nil t)))
  729. (and (eq side 'top)
  730. (setq window (window-with-parameter 'window-side 'bottom nil t))))
  731. (window-prev-sibling window))
  732. ((or (and (eq side 'right)
  733. (setq window (window-with-parameter 'window-side 'left nil t)))
  734. (and (eq side 'bottom)
  735. (setq window (window-with-parameter 'window-side 'top nil t))))
  736. (window-next-sibling window))
  737. ((memq side '(left right))
  738. (cond
  739. (window-sides-vertical
  740. root)
  741. ((setq window (window-with-parameter 'window-side 'top nil t))
  742. (window-next-sibling window))
  743. ((setq window (window-with-parameter 'window-side 'bottom nil t))
  744. (window-prev-sibling window))
  745. (t root)))
  746. ((memq side '(top bottom))
  747. (cond
  748. ((not window-sides-vertical)
  749. root)
  750. ((setq window (window-with-parameter 'window-side 'left nil t))
  751. (window-next-sibling window))
  752. ((setq window (window-with-parameter 'window-side 'right nil t))
  753. (window-prev-sibling window))
  754. (t root))))))
  755. (defun display-buffer-in-major-side-window (buffer side slot &optional alist)
  756. "Display BUFFER in a new window on SIDE of the selected frame.
  757. SIDE must be one of `left', `top', `right' or `bottom'. SLOT
  758. specifies the slot to use. ALIST is an association list of
  759. symbols and values as passed to `display-buffer-in-side-window'.
  760. This function may be called only if no window on SIDE exists yet.
  761. The new window automatically becomes the \"major\" side window on
  762. SIDE. Return the new window, nil if its creation window failed."
  763. (let* ((left-or-right (memq side '(left right)))
  764. (major (window--major-side-window side))
  765. (on-side (cond
  766. ((eq side 'top) 'above)
  767. ((eq side 'bottom) 'below)
  768. (t side)))
  769. ;; The following two bindings will tell `split-window' to take
  770. ;; the space for the new window from `major' and not make a new
  771. ;; parent window unless needed.
  772. (window-combination-resize 'side)
  773. (window-combination-limit nil)
  774. (new (split-window major nil on-side)))
  775. (when new
  776. ;; Initialize `window-side' parameter of new window to SIDE.
  777. (set-window-parameter new 'window-side side)
  778. ;; Install `window-slot' parameter of new window.
  779. (set-window-parameter new 'window-slot slot)
  780. ;; Install `delete-window' parameter thus making sure that when
  781. ;; the new window is deleted, a side window on the opposite side
  782. ;; does not get resized.
  783. (set-window-parameter new 'delete-window 'delete-side-window)
  784. ;; Auto-adjust height/width of new window unless a size has been
  785. ;; explicitly requested.
  786. (unless (if left-or-right
  787. (cdr (assq 'window-width alist))
  788. (cdr (assq 'window-height alist)))
  789. (setq alist
  790. (cons
  791. (cons
  792. (if left-or-right 'window-width 'window-height)
  793. (/ (window-total-size (frame-root-window) left-or-right)
  794. ;; By default use a fourth of the size of the frame's
  795. ;; root window.
  796. 4))
  797. alist)))
  798. ;; Install BUFFER in new window and return NEW.
  799. (window--display-buffer buffer new 'window alist 'side))))
  800. (defun delete-side-window (window)
  801. "Delete side window WINDOW."
  802. (let ((window-combination-resize
  803. (window-parameter (window-parent window) 'window-side))
  804. (ignore-window-parameters t))
  805. (delete-window window)))
  806. (defun display-buffer-in-side-window (buffer alist)
  807. "Display BUFFER in a side window of the selected frame.
  808. ALIST is an association list of symbols and values. The
  809. following special symbols can be used in ALIST.
  810. `side' denotes the side of the frame where the new window shall
  811. be located. Valid values are `bottom', `right', `top' and
  812. `left'. The default is `bottom'.
  813. `slot' if non-nil, specifies the window slot where to display
  814. BUFFER. A value of zero or nil means use the middle slot on
  815. the specified side. A negative value means use a slot
  816. preceding (that is, above or on the left of) the middle slot.
  817. A positive value means use a slot following (that is, below or
  818. on the right of) the middle slot. The default is zero."
  819. (let ((side (or (cdr (assq 'side alist)) 'bottom))
  820. (slot (or (cdr (assq 'slot alist)) 0)))
  821. (cond
  822. ((not (memq side '(top bottom left right)))
  823. (error "Invalid side %s specified" side))
  824. ((not (numberp slot))
  825. (error "Invalid slot %s specified" slot)))
  826. (let* ((major (window-with-parameter 'window-side side nil t))
  827. ;; `major' is the major window on SIDE, `windows' the list of
  828. ;; life windows on SIDE.
  829. (windows
  830. (when major
  831. (let (windows)
  832. (walk-window-tree
  833. (lambda (window)
  834. (when (eq (window-parameter window 'window-side) side)
  835. (setq windows (cons window windows))))
  836. nil nil 'nomini)
  837. (nreverse windows))))
  838. (slots (when major (max 1 (window-child-count major))))
  839. (max-slots
  840. (nth (cond
  841. ((eq side 'left) 0)
  842. ((eq side 'top) 1)
  843. ((eq side 'right) 2)
  844. ((eq side 'bottom) 3))
  845. window-sides-slots))
  846. window this-window this-slot prev-window next-window
  847. best-window best-slot abs-slot)
  848. (cond
  849. ((and (numberp max-slots) (<= max-slots 0))
  850. ;; No side-slots available on this side. Don't create an error,
  851. ;; just return nil.
  852. nil)
  853. ((not windows)
  854. ;; No major window exists on this side, make one.
  855. (display-buffer-in-major-side-window buffer side slot alist))
  856. (t
  857. ;; Scan windows on SIDE.
  858. (catch 'found
  859. (dolist (window windows)
  860. (setq this-slot (window-parameter window 'window-slot))
  861. (cond
  862. ;; The following should not happen and probably be checked
  863. ;; by window--side-check.
  864. ((not (numberp this-slot)))
  865. ((= this-slot slot)
  866. ;; A window with a matching slot has been found.
  867. (setq this-window window)
  868. (throw 'found t))
  869. (t
  870. ;; Check if this window has a better slot value wrt the
  871. ;; slot of the window we want.
  872. (setq abs-slot
  873. (if (or (and (> this-slot 0) (> slot 0))
  874. (and (< this-slot 0) (< slot 0)))
  875. (abs (- slot this-slot))
  876. (+ (abs slot) (abs this-slot))))
  877. (unless (and best-slot (<= best-slot abs-slot))
  878. (setq best-window window)
  879. (setq best-slot abs-slot))
  880. (cond
  881. ((<= this-slot slot)
  882. (setq prev-window window))
  883. ((not next-window)
  884. (setq next-window window)))))))
  885. ;; `this-window' is the first window with the same SLOT.
  886. ;; `prev-window' is the window with the largest slot < SLOT. A new
  887. ;; window will be created after it.
  888. ;; `next-window' is the window with the smallest slot > SLOT. A new
  889. ;; window will be created before it.
  890. ;; `best-window' is the window with the smallest absolute difference
  891. ;; of its slot and SLOT.
  892. ;; Note: We dedicate the window used softly to its buffer to
  893. ;; avoid that "other" (non-side) buffer display functions steal
  894. ;; it from us. This must eventually become customizable via
  895. ;; ALIST (or, better, avoided in the "other" functions).
  896. (or (and this-window
  897. ;; Reuse `this-window'.
  898. (window--display-buffer buffer this-window 'reuse alist 'side))
  899. (and (or (not max-slots) (< slots max-slots))
  900. (or (and next-window
  901. ;; Make new window before `next-window'.
  902. (let ((next-side
  903. (if (memq side '(left right)) 'above 'left))
  904. (window-combination-resize 'side))
  905. (setq window (split-window next-window nil next-side))
  906. ;; When the new window is deleted, its space
  907. ;; is returned to other side windows.
  908. (set-window-parameter
  909. window 'delete-window 'delete-side-window)
  910. window))
  911. (and prev-window
  912. ;; Make new window after `prev-window'.
  913. (let ((prev-side
  914. (if (memq side '(left right)) 'below 'right))
  915. (window-combination-resize 'side))
  916. (setq window (split-window prev-window nil prev-side))
  917. ;; When the new window is deleted, its space
  918. ;; is returned to other side windows.
  919. (set-window-parameter
  920. window 'delete-window 'delete-side-window)
  921. window)))
  922. (set-window-parameter window 'window-slot slot)
  923. (window--display-buffer buffer window 'window alist 'side))
  924. (and best-window
  925. ;; Reuse `best-window'.
  926. (progn
  927. ;; Give best-window the new slot value.
  928. (set-window-parameter best-window 'window-slot slot)
  929. (window--display-buffer
  930. buffer best-window 'reuse alist 'side)))))))))
  931. (defun window--side-check (&optional frame)
  932. "Check the side window configuration of FRAME.
  933. FRAME defaults to the selected frame.
  934. A valid side window configuration preserves the following two
  935. invariants:
  936. - If there exists a window whose window-side parameter is
  937. non-nil, there must exist at least one live window whose
  938. window-side parameter is nil.
  939. - If a window W has a non-nil window-side parameter (i) it must
  940. have a parent window and that parent's window-side parameter
  941. must be either nil or the same as for W, and (ii) any child
  942. window of W must have the same window-side parameter as W.
  943. If the configuration is invalid, reset the window-side parameters
  944. of all windows on FRAME to nil."
  945. (let (left top right bottom none side parent parent-side)
  946. (when (or (catch 'reset
  947. (walk-window-tree
  948. (lambda (window)
  949. (setq side (window-parameter window 'window-side))
  950. (setq parent (window-parent window))
  951. (setq parent-side
  952. (and parent (window-parameter parent 'window-side)))
  953. ;; The following `cond' seems a bit tedious, but I'd
  954. ;; rather stick to using just the stack.
  955. (cond
  956. (parent-side
  957. (when (not (eq parent-side side))
  958. ;; A parent whose window-side is non-nil must
  959. ;; have a child with the same window-side.
  960. (throw 'reset t)))
  961. ((not side)
  962. (when (window-buffer window)
  963. ;; Record that we have at least one non-side,
  964. ;; live window.
  965. (setq none t)))
  966. ((if (memq side '(left top))
  967. (window-prev-sibling window)
  968. (window-next-sibling window))
  969. ;; Left and top major side windows must not have a
  970. ;; previous sibling, right and bottom major side
  971. ;; windows must not have a next sibling.
  972. (throw 'reset t))
  973. ;; Now check that there's no more than one major
  974. ;; window for any of left, top, right and bottom.
  975. ((eq side 'left)
  976. (if left (throw 'reset t) (setq left t)))
  977. ((eq side 'top)
  978. (if top (throw 'reset t) (setq top t)))
  979. ((eq side 'right)
  980. (if right (throw 'reset t) (setq right t)))
  981. ((eq side 'bottom)
  982. (if bottom (throw 'reset t) (setq bottom t)))
  983. (t
  984. (throw 'reset t))))
  985. frame t 'nomini))
  986. ;; If there's a side window, there must be at least one
  987. ;; non-side window.
  988. (and (or left top right bottom) (not none)))
  989. (walk-window-tree
  990. (lambda (window)
  991. (set-window-parameter window 'window-side nil))
  992. frame t 'nomini))))
  993. (defun window--check (&optional frame)
  994. "Check atomic and side windows on FRAME.
  995. FRAME defaults to the selected frame."
  996. (window--side-check frame)
  997. (window--atom-check frame))
  998. ;; Dumping frame/window contents.
  999. (defun window--dump-window (&optional window erase)
  1000. "Dump WINDOW to buffer *window-frame-dump*.
  1001. WINDOW must be a valid window and defaults to the selected one.
  1002. Optional argument ERASE non-nil means erase *window-frame-dump*
  1003. before writing to it."
  1004. (setq window (window-normalize-window window))
  1005. (with-current-buffer (get-buffer-create "*window-frame-dump*")
  1006. (when erase (erase-buffer))
  1007. (insert
  1008. (format "%s parent: %s\n" window (window-parent window))
  1009. (format "pixel left: %s top: %s size: %s x %s new: %s\n"
  1010. (window-pixel-left window) (window-pixel-top window)
  1011. (window-size window t t) (window-size window nil t)
  1012. (window-new-pixel window))
  1013. (format "char left: %s top: %s size: %s x %s new: %s\n"
  1014. (window-left-column window) (window-top-line window)
  1015. (window-total-size window t) (window-total-size window)
  1016. (window-new-total window))
  1017. (format "normal: %s x %s new: %s\n"
  1018. (window-normal-size window t) (window-normal-size window)
  1019. (window-new-normal window)))
  1020. (when (window-live-p window)
  1021. (let ((fringes (window-fringes window))
  1022. (margins (window-margins window)))
  1023. (insert
  1024. (format "body pixel: %s x %s char: %s x %s\n"
  1025. (window-body-width window t) (window-body-height window t)
  1026. (window-body-width window) (window-body-height window))
  1027. (format "width left fringe: %s left margin: %s right margin: %s\n"
  1028. (car fringes) (or (car margins) 0) (or (cdr margins) 0))
  1029. (format "width right fringe: %s scroll-bar: %s divider: %s\n"
  1030. (cadr fringes)
  1031. (window-scroll-bar-width window)
  1032. (window-right-divider-width window))
  1033. (format "height header-line: %s mode-line: %s divider: %s\n"
  1034. (window-header-line-height window)
  1035. (window-mode-line-height window)
  1036. (window-bottom-divider-width window)))))
  1037. (insert "\n")))
  1038. (defun window--dump-frame (&optional window-or-frame)
  1039. "Dump WINDOW-OR-FRAME to buffer *window-frame-dump*.
  1040. WINDOW-OR-FRAME can be a frame or a window and defaults to the
  1041. selected frame. When WINDOW-OR-FRAME is a window, dump that
  1042. window's frame. The buffer *window-frame-dump* is erased before
  1043. dumping to it."
  1044. (let* ((window
  1045. (cond
  1046. ((or (not window-or-frame)
  1047. (frame-live-p window-or-frame))
  1048. (frame-root-window window-or-frame))
  1049. ((or (window-live-p window-or-frame)
  1050. (window-child window-or-frame))
  1051. window-or-frame)
  1052. (t
  1053. (frame-root-window))))
  1054. (frame (window-frame window)))
  1055. (with-current-buffer (get-buffer-create "*window-frame-dump*")
  1056. (erase-buffer)
  1057. (insert
  1058. (format "frame pixel: %s x %s cols/lines: %s x %s units: %s x %s\n"
  1059. (frame-pixel-width frame) (frame-pixel-height frame)
  1060. (frame-total-cols frame) (frame-total-lines frame)
  1061. (frame-char-width frame) (frame-char-height frame))
  1062. (format "frame text pixel: %s x %s cols/lines: %s x %s\n"
  1063. (frame-text-width frame) (frame-text-height frame)
  1064. (frame-text-cols frame) (frame-text-lines frame))
  1065. (format "tool: %s scroll: %s/%s fringe: %s border: %s right: %s bottom: %s\n\n"
  1066. (if (fboundp 'tool-bar-height)
  1067. (tool-bar-height frame t)
  1068. "0")
  1069. (frame-scroll-bar-width frame)
  1070. (frame-scroll-bar-height frame)
  1071. (frame-fringe-width frame)
  1072. (frame-border-width frame)
  1073. (frame-right-divider-width frame)
  1074. (frame-bottom-divider-width frame)))
  1075. (walk-window-tree 'window--dump-window frame t t))))
  1076. ;;; Window sizes.
  1077. (defun window-total-size (&optional window horizontal round)
  1078. "Return the total height or width of WINDOW.
  1079. WINDOW must be a valid window and defaults to the selected one.
  1080. If HORIZONTAL is omitted or nil, return the total height of
  1081. WINDOW, in lines. If WINDOW is live, its total height includes,
  1082. in addition to the height of WINDOW's text, the heights of
  1083. WINDOW's mode and header line and a bottom divider, if any.
  1084. If HORIZONTAL is non-nil, return the total width of WINDOW, in
  1085. columns. If WINDOW is live, its total width includes, in
  1086. addition to the width of WINDOW's text, the widths of WINDOW's
  1087. fringes, margins, scroll bars and its right divider, if any.
  1088. If WINDOW is internal, return the respective size of the screen
  1089. areas spanned by its children.
  1090. Optional argument ROUND is handled as for `window-total-height'
  1091. and `window-total-width'."
  1092. (if horizontal
  1093. (window-total-width window round)
  1094. (window-total-height window round)))
  1095. (defun window-size (&optional window horizontal pixelwise round)
  1096. "Return the height or width of WINDOW.
  1097. WINDOW must be a valid window and defaults to the selected one.
  1098. If HORIZONTAL is omitted or nil, return the total height of
  1099. WINDOW, in lines, like `window-total-height'. Otherwise return
  1100. the total width, in columns, like `window-total-width'.
  1101. Optional argument PIXELWISE means return the pixel size of WINDOW
  1102. like `window-pixel-height' and `window-pixel-width'.
  1103. Optional argument ROUND is ignored if PIXELWISE is non-nil and
  1104. handled as for `window-total-height' and `window-total-width'
  1105. otherwise."
  1106. (if horizontal
  1107. (if pixelwise
  1108. (window-pixel-width window)
  1109. (window-total-width window round))
  1110. (if pixelwise
  1111. (window-pixel-height window)
  1112. (window-total-height window round))))
  1113. (defvar window-size-fixed nil
  1114. "Non-nil in a buffer means windows displaying the buffer are fixed-size.
  1115. If the value is `height', then only the window's height is fixed.
  1116. If the value is `width', then only the window's width is fixed.
  1117. Any other non-nil value fixes both the width and the height.
  1118. Emacs won't change the size of any window displaying that buffer,
  1119. unless it has no other choice (like when deleting a neighboring
  1120. window).")
  1121. (make-variable-buffer-local 'window-size-fixed)
  1122. (defun window--preservable-size (window &optional horizontal)
  1123. "Return height of WINDOW as `window-preserve-size' would preserve it.
  1124. Optional argument HORIZONTAL non-nil means to return the width of
  1125. WINDOW as `window-preserve-size' would preserve it."
  1126. (if horizontal
  1127. (window-body-width window t)
  1128. (+ (window-body-height window t)
  1129. (window-header-line-height window)
  1130. (window-mode-line-height window))))
  1131. (defun window-preserve-size (&optional window horizontal preserve)
  1132. "Preserve height of window WINDOW.
  1133. WINDOW must be a live window and defaults to the selected one.
  1134. Optional argument HORIZONTAL non-nil means preserve the width of
  1135. WINDOW.
  1136. PRESERVE t means to preserve the current height/width of WINDOW's
  1137. body in frame and window resizing operations whenever possible.
  1138. The height/width of WINDOW will change only if Emacs has no other
  1139. choice. Resizing a window whose height/width is preserved never
  1140. throws an error.
  1141. PRESERVE nil means to stop preserving the height/width of WINDOW,
  1142. lifting the respective restraint induced by a previous call of
  1143. `window-preserve-size' for WINDOW. Calling `enlarge-window',
  1144. `shrink-window', `split-window' or `fit-window-to-buffer' with
  1145. WINDOW as argument also removes the respective restraint.
  1146. Other values of PRESERVE are reserved for future use."
  1147. (setq window (window-normalize-window window t))
  1148. (let* ((parameter (window-parameter window 'window-preserved-size))
  1149. (width (nth 1 parameter))
  1150. (height (nth 2 parameter)))
  1151. (if horizontal
  1152. (set-window-parameter
  1153. window 'window-preserved-size
  1154. (list
  1155. (window-buffer window)
  1156. (and preserve (window--preservable-size window t))
  1157. height))
  1158. (set-window-parameter
  1159. window 'window-preserved-size
  1160. (list
  1161. (window-buffer window)
  1162. width
  1163. (and preserve (window--preservable-size window)))))))
  1164. (defun window-preserved-size (&optional window horizontal)
  1165. "Return preserved height of window WINDOW.
  1166. WINDOW must be a live window and defaults to the selected one.
  1167. Optional argument HORIZONTAL non-nil means to return preserved
  1168. width of WINDOW."
  1169. (setq window (window-normalize-window window t))
  1170. (let* ((parameter (window-parameter window 'window-preserved-size))
  1171. (buffer (nth 0 parameter))
  1172. (width (nth 1 parameter))
  1173. (height (nth 2 parameter)))
  1174. (when (eq buffer (window-buffer window))
  1175. (if horizontal width height))))
  1176. (defun window--preserve-size (window horizontal)
  1177. "Return non-nil when the height of WINDOW shall be preserved.
  1178. Optional argument HORIZONTAL non-nil means to return non-nil when
  1179. the width of WINDOW shall be preserved."
  1180. (let ((size (window-preserved-size window horizontal)))
  1181. (and (numberp size)
  1182. (= size (window--preservable-size window horizontal)))))
  1183. (defun window-safe-min-size (&optional window horizontal pixelwise)
  1184. "Return safe minimum size of WINDOW.
  1185. WINDOW must be a valid window and defaults to the selected one.
  1186. Optional argument HORIZONTAL non-nil means return the minimum
  1187. number of columns of WINDOW; otherwise return the minimum number
  1188. of WINDOW's lines.
  1189. Optional argument PIXELWISE non-nil means return the minimum pixel-size
  1190. of WINDOW."
  1191. (setq window (window-normalize-window window))
  1192. (if pixelwise
  1193. (if horizontal
  1194. (* window-safe-min-width
  1195. (frame-char-width (window-frame window)))
  1196. (* window-safe-min-height
  1197. (frame-char-height (window-frame window))))
  1198. (if horizontal window-safe-min-width window-safe-min-height)))
  1199. (defun window-min-size (&optional window horizontal ignore pixelwise)
  1200. "Return the minimum size of WINDOW.
  1201. WINDOW must be a valid window and defaults to the selected one.
  1202. Optional argument HORIZONTAL non-nil means return the minimum
  1203. number of columns of WINDOW; otherwise return the minimum number
  1204. of WINDOW's lines.
  1205. The optional argument IGNORE has the same meaning as for
  1206. `window-resizable'. Optional argument PIXELWISE non-nil means
  1207. return the minimum pixel-size of WINDOW."
  1208. (window--min-size-1
  1209. (window-normalize-window window) horizontal ignore pixelwise))
  1210. (defun window--min-size-ignore-p (window horizontal ignore)
  1211. "Return non-nil if IGNORE says to ignore height restrictions for WINDOW.
  1212. HORIZONTAL non-nil means to return non-nil if IGNORE says to
  1213. ignore width restrictions for WINDOW."
  1214. (if (window-valid-p ignore)
  1215. (eq window ignore)
  1216. (not (memq ignore '(nil preserved)))))
  1217. (defun window--min-size-1 (window horizontal ignore pixelwise)
  1218. "Internal function of `window-min-size'."
  1219. (let ((sub (window-child window)))
  1220. (if sub
  1221. (let ((value 0))
  1222. ;; WINDOW is an internal window.
  1223. (if (window-combined-p sub horizontal)
  1224. ;; The minimum size of an iso-combination is the sum of
  1225. ;; the minimum sizes of its child windows.
  1226. (while sub
  1227. (setq value (+ value
  1228. (window--min-size-1
  1229. sub horizontal ignore pixelwise)))
  1230. (setq sub (window-right sub)))
  1231. ;; The minimum size of an ortho-combination is the maximum
  1232. ;; of the minimum sizes of its child windows.
  1233. (while sub
  1234. (setq value (max value
  1235. (window--min-size-1
  1236. sub horizontal ignore pixelwise)))
  1237. (setq sub (window-right sub))))
  1238. value)
  1239. (with-current-buffer (window-buffer window)
  1240. (cond
  1241. ((window-minibuffer-p window)
  1242. (if pixelwise (frame-char-height (window-frame window)) 1))
  1243. ((window-size-fixed-p window horizontal ignore)
  1244. ;; The minimum size of a fixed size window is its size.
  1245. (window-size window horizontal pixelwise))
  1246. ((eq ignore 'safe)
  1247. ;; If IGNORE equals `safe' return the safe value.
  1248. (window-safe-min-size window horizontal pixelwise))
  1249. (horizontal
  1250. ;; For the minimum width of a window take fringes and
  1251. ;; scroll-bars into account. This is questionable and should
  1252. ;; be removed as soon as we are able to split (and resize)
  1253. ;; windows such that the new (or resized) windows can get a
  1254. ;; size less than the user-specified `window-min-height' and
  1255. ;; `window-min-width'.
  1256. (let* ((char-size (frame-char-size window t))
  1257. (fringes (window-fringes window))
  1258. (margins (window-margins window))
  1259. (pixel-width
  1260. (+ (window-safe-min-size window t t)
  1261. (* (or (car margins) 0) char-size)
  1262. (* (or (cdr margins) 0) char-size)
  1263. (car fringes) (cadr fringes)
  1264. (window-scroll-bar-width window)
  1265. (window-right-divider-width window))))
  1266. (if pixelwise
  1267. (max
  1268. (if window-resize-pixelwise
  1269. pixel-width
  1270. ;; Round up to next integral of columns.
  1271. (* (ceiling pixel-width char-size) char-size))
  1272. (if (window--min-size-ignore-p window horizontal ignore)
  1273. 0
  1274. (window-min-pixel-width window)))
  1275. (max
  1276. (ceiling pixel-width char-size)
  1277. (if (window--min-size-ignore-p window horizontal ignore)
  1278. 0
  1279. window-min-width)))))
  1280. ((let ((char-size (frame-char-size window))
  1281. (pixel-height
  1282. (+ (window-safe-min-size window nil t)
  1283. (window-header-line-height window)
  1284. (window-scroll-bar-height window)
  1285. (window-mode-line-height window)
  1286. (window-bottom-divider-width window))))
  1287. (if pixelwise
  1288. (max
  1289. (if window-resize-pixelwise
  1290. pixel-height
  1291. ;; Round up to next integral of lines.
  1292. (* (ceiling pixel-height char-size) char-size))
  1293. (if (window--min-size-ignore-p window horizontal ignore)
  1294. 0
  1295. (window-min-pixel-height window)))
  1296. (max (ceiling pixel-height char-size)
  1297. (if (window--min-size-ignore-p window horizontal ignore)
  1298. 0
  1299. window-min-height))))))))))
  1300. (defun window-sizable (window delta &optional horizontal ignore pixelwise)
  1301. "Return DELTA if DELTA lines can be added to WINDOW.
  1302. WINDOW must be a valid window and defaults to the selected one.
  1303. Optional argument HORIZONTAL non-nil means return DELTA if DELTA
  1304. columns can be added to WINDOW. A return value of zero means
  1305. that no lines (or columns) can be added to WINDOW.
  1306. This function looks only at WINDOW and, recursively, its child
  1307. windows. The function `window-resizable' looks at other windows
  1308. as well.
  1309. DELTA positive means WINDOW shall be enlarged by DELTA lines or
  1310. columns. If WINDOW cannot be enlarged by DELTA lines or columns
  1311. return the maximum value in the range 0..DELTA by which WINDOW
  1312. can be enlarged.
  1313. DELTA negative means WINDOW shall be shrunk by -DELTA lines or
  1314. columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
  1315. return the minimum value in the range DELTA..0 by which WINDOW
  1316. can be shrunk.
  1317. The optional argument IGNORE has the same meaning as for
  1318. `window-resizable'. Optional argument PIXELWISE non-nil means
  1319. interpret DELTA as pixels."
  1320. (setq window (window-normalize-window window))
  1321. (cond
  1322. ((< delta 0)
  1323. (max (- (window-min-size window horizontal ignore pixelwise)
  1324. (window-size window horizontal pixelwise))
  1325. delta))
  1326. ((> delta 0)
  1327. (if (window-size-fixed-p window horizontal ignore)
  1328. 0
  1329. delta))
  1330. (t 0)))
  1331. (defun window-sizable-p (window delta &optional horizontal ignore pixelwise)
  1332. "Return t if WINDOW can be resized by DELTA lines.
  1333. WINDOW must be a valid window and defaults to the selected one.
  1334. For the meaning of the arguments of this function see the
  1335. doc-string of `window-sizable'."
  1336. (setq window (window-normalize-window window))
  1337. (if (> delta 0)
  1338. (>= (window-sizable window delta horizontal ignore pixelwise)
  1339. delta)
  1340. (<= (window-sizable window delta horizontal ignore pixelwise)
  1341. delta)))
  1342. (defun window--size-fixed-1 (window horizontal ignore)
  1343. "Internal function for `window-size-fixed-p'."
  1344. (let ((sub (window-child window)))
  1345. (catch 'fixed
  1346. (if sub
  1347. ;; WINDOW is an internal window.
  1348. (if (window-combined-p sub horizontal)
  1349. ;; An iso-combination is fixed size if all its child
  1350. ;; windows are fixed-size.
  1351. (progn
  1352. (while sub
  1353. (unless (window--size-fixed-1 sub horizontal ignore)
  1354. ;; We found a non-fixed-size child window, so
  1355. ;; WINDOW's size is not fixed.
  1356. (throw 'fixed nil))
  1357. (setq sub (window-right sub)))
  1358. ;; All child windows are fixed-size, so WINDOW's size is
  1359. ;; fixed.
  1360. (throw 'fixed t))
  1361. ;; An ortho-combination is fixed-size if at least one of its
  1362. ;; child windows is fixed-size.
  1363. (while sub
  1364. (when (window--size-fixed-1 sub horizontal ignore)
  1365. ;; We found a fixed-size child window, so WINDOW's size
  1366. ;; is fixed.
  1367. (throw 'fixed t))
  1368. (setq sub (window-right sub))))
  1369. ;; WINDOW is a live window.
  1370. (and (or (not (windowp ignore)) (not (eq window ignore)))
  1371. (or (and (not (eq ignore 'preserved))
  1372. (window--preserve-size window horizontal))
  1373. (with-current-buffer (window-buffer window)
  1374. (if horizontal
  1375. (memq window-size-fixed '(width t))
  1376. (memq window-size-fixed '(height t))))))))))
  1377. (defun window-size-fixed-p (&optional window horizontal ignore)
  1378. "Return non-nil if WINDOW's height is fixed.
  1379. WINDOW must be a valid window and defaults to the selected one.
  1380. Optional argument HORIZONTAL non-nil means return non-nil if
  1381. WINDOW's width is fixed. The optional argument IGNORE has the
  1382. same meaning as for `window-resizable'.
  1383. If this function returns nil, this does not necessarily mean that
  1384. WINDOW can be resized in the desired direction. The function
  1385. `window-resizable' can tell that."
  1386. (when (or (windowp ignore) (memq ignore '(nil preserved)))
  1387. (window--size-fixed-1
  1388. (window-normalize-window window) horizontal ignore)))
  1389. (defun window--min-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
  1390. "Internal function for `window-min-delta'."
  1391. (if (not (window-parent window))
  1392. ;; If we can't go up, return zero.
  1393. 0
  1394. ;; Else try to find a non-fixed-size sibling of WINDOW.
  1395. (let* ((parent (window-parent window))
  1396. (sub (window-child parent)))
  1397. (catch 'done
  1398. (if (window-combined-p sub horizontal)
  1399. ;; In an iso-combination throw DELTA if we find at least one
  1400. ;; child window and that window is either not fixed-size or
  1401. ;; we can ignore fixed-sizeness.
  1402. (let ((skip (eq trail 'after)))
  1403. (while sub
  1404. (cond
  1405. ((eq sub window)
  1406. (setq skip (eq trail 'before)))
  1407. (skip)
  1408. ((window-size-fixed-p sub horizontal ignore))
  1409. (t
  1410. ;; We found a non-fixed-size child window.
  1411. (throw 'done delta)))
  1412. (setq sub (window-right sub))))
  1413. ;; In an ortho-combination set DELTA to the minimum value by
  1414. ;; which other child windows can shrink.
  1415. (while sub
  1416. (unless (eq sub window)
  1417. (setq delta
  1418. (min delta
  1419. (max (- (window-size sub horizontal pixelwise 'ceiling)
  1420. (window-min-size
  1421. sub horizontal ignore pixelwise))
  1422. 0))))
  1423. (setq sub (window-right sub))))
  1424. (if noup
  1425. delta
  1426. (window--min-delta-1
  1427. parent delta horizontal ignore trail nil pixelwise))))))
  1428. (defun window-min-delta (&optional window horizontal ignore trail noup nodown pixelwise)
  1429. "Return number of lines by which WINDOW can be shrunk.
  1430. WINDOW must be a valid window and defaults to the selected one.
  1431. Return zero if WINDOW cannot be shrunk.
  1432. Optional argument HORIZONTAL non-nil means return number of
  1433. columns by which WINDOW can be shrunk.
  1434. The optional argument IGNORE has the same meaning as for
  1435. `window-resizable'. Optional argument TRAIL restricts the
  1436. windows that can be enlarged. If its value is `before', only
  1437. windows to the left of or above WINDOW can be enlarged. If it is
  1438. `after', only windows to the right of or below WINDOW can be
  1439. enlarged.
  1440. Optional argument NOUP non-nil means don't go up in the window
  1441. tree, but try to enlarge windows within WINDOW's combination
  1442. only. Optional argument NODOWN non-nil means don't check whether
  1443. WINDOW itself (and its child windows) can be shrunk; check only
  1444. whether at least one other window can be enlarged appropriately.
  1445. Optional argument PIXELWISE non-nil means return number of pixels
  1446. by which WINDOW can be shrunk."
  1447. (setq window (window-normalize-window window))
  1448. (let ((size (window-size window horizontal pixelwise 'floor))
  1449. (minimum (window-min-size window horizontal ignore pixelwise)))
  1450. (cond
  1451. (nodown
  1452. ;; If NODOWN is t, try to recover the entire size of WINDOW.
  1453. (window--min-delta-1
  1454. window size horizontal ignore trail noup pixelwise))
  1455. ((<= size minimum)
  1456. ;; If NODOWN is nil and WINDOW's size is already at its minimum,
  1457. ;; there's nothing to recover.
  1458. 0)
  1459. (t
  1460. ;; Otherwise, try to recover whatever WINDOW is larger than its
  1461. ;; minimum size.
  1462. (window--min-delta-1
  1463. window (- size minimum) horizontal ignore trail noup pixelwise)))))
  1464. (defun frame-windows-min-size (&optional frame horizontal ignore pixelwise)
  1465. "Return minimum number of lines of FRAME's windows.
  1466. HORIZONTAL non-nil means return number of columns of FRAME's
  1467. windows. The optional argument IGNORE has the same meaning as
  1468. for `window-resizable'. PIXELWISE non-nil means return sizes in
  1469. pixels."
  1470. (setq frame (window-normalize-frame frame))
  1471. (let* ((root (frame-root-window frame))
  1472. (mini (window-next-sibling root)))
  1473. (+ (window-min-size root horizontal ignore pixelwise)
  1474. (if (and mini (not horizontal))
  1475. (window-min-size mini horizontal nil pixelwise)
  1476. 0))))
  1477. (defun window--max-delta-1 (window delta &optional horizontal ignore trail noup pixelwise)
  1478. "Internal function of `window-max-delta'."
  1479. (if (not (window-parent window))
  1480. ;; Can't go up. Return DELTA.
  1481. delta
  1482. (let* ((parent (window-parent window))
  1483. (sub (window-child parent)))
  1484. (catch 'fixed
  1485. (if (window-combined-p sub horizontal)
  1486. ;; For an iso-combination calculate how much we can get from
  1487. ;; other child windows.
  1488. (let ((skip (eq trail 'after)))
  1489. (while sub
  1490. (cond
  1491. ((eq sub window)
  1492. (setq skip (eq trail 'before)))
  1493. (skip)
  1494. (t
  1495. (setq delta
  1496. (+ delta
  1497. (max
  1498. (- (window-size sub horizontal pixelwise 'floor)
  1499. (window-min-size
  1500. sub horizontal ignore pixelwise))
  1501. 0)))))
  1502. (setq sub (window-right sub))))
  1503. ;; For an ortho-combination throw DELTA when at least one
  1504. ;; child window is fixed-size.
  1505. (while sub
  1506. (when (and (not (eq sub window))
  1507. (window-size-fixed-p sub horizontal ignore))
  1508. (throw 'fixed delta))
  1509. (setq sub (window-right sub))))
  1510. (if noup
  1511. ;; When NOUP is nil, DELTA is all we can get.
  1512. delta
  1513. ;; Else try with parent of WINDOW, passing the DELTA we
  1514. ;; recovered so far.
  1515. (window--max-delta-1
  1516. parent delta horizontal ignore trail nil pixelwise))))))
  1517. (defun window-max-delta (&optional window horizontal ignore trail noup nodown pixelwise)
  1518. "Return maximum number of lines by which WINDOW can be enlarged.
  1519. WINDOW must be a valid window and defaults to the selected one.
  1520. The return value is zero if WINDOW cannot be enlarged.
  1521. Optional argument HORIZONTAL non-nil means return maximum number
  1522. of columns by which WINDOW can be enlarged.
  1523. The optional argument IGNORE has the same meaning as for
  1524. `window-resizable'. Optional argument TRAIL restricts the
  1525. windows that can be enlarged. If its value is `before', only
  1526. windows to the left of or above WINDOW can be enlarged. If it is
  1527. `after', only windows to the right of or below WINDOW can be
  1528. enlarged.
  1529. Optional argument NOUP non-nil means don't go up in the window
  1530. tree but try to obtain the entire space from windows within
  1531. WINDOW's combination. Optional argument NODOWN non-nil means do
  1532. not check whether WINDOW itself (and its child windows) can be
  1533. enlarged; check only whether other windows can be shrunk
  1534. appropriately.
  1535. Optional argument PIXELWISE non-nil means return number of
  1536. pixels by which WINDOW can be enlarged."
  1537. (setq window (window-normalize-window window))
  1538. (if (and (not nodown) (window-size-fixed-p window horizontal ignore))
  1539. ;; With IGNORE and NOWDON nil return zero if WINDOW has fixed
  1540. ;; size.
  1541. 0
  1542. ;; WINDOW has no fixed size.
  1543. (window--max-delta-1 window 0 horizontal ignore trail noup pixelwise)))
  1544. ;; Make NOUP also inhibit the min-size check.
  1545. (defun window--resizable (window delta &optional horizontal ignore trail noup nodown pixelwise)
  1546. "Return DELTA if WINDOW can be resized vertically by DELTA lines.
  1547. WINDOW must be a valid window and defaults to the selected one.
  1548. Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
  1549. can be resized horizontally by DELTA columns. A return value of
  1550. zero means that WINDOW is not resizable.
  1551. DELTA positive means WINDOW shall be enlarged by DELTA lines or
  1552. columns. If WINDOW cannot be enlarged by DELTA lines or columns,
  1553. return the maximum value in the range 0..DELTA by which WINDOW
  1554. can be enlarged.
  1555. DELTA negative means WINDOW shall be shrunk by -DELTA lines or
  1556. columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
  1557. return the minimum value in the range DELTA..0 that can be used
  1558. for shrinking WINDOW.
  1559. The optional argument IGNORE has the same meaning as for
  1560. `window-resizable'. Optional argument TRAIL `before' means only
  1561. windows to the left of or below WINDOW can be shrunk. Optional
  1562. argument TRAIL `after' means only windows to the right of or
  1563. above WINDOW can be shrunk.
  1564. Optional argument NOUP non-nil means don't go up in the window
  1565. tree but check only whether space can be obtained from (or given
  1566. to) WINDOW's siblings. Optional argument NODOWN non-nil means
  1567. don't go down in the window tree. This means do not check
  1568. whether resizing would violate size restrictions of WINDOW or its
  1569. child windows.
  1570. Optional argument PIXELWISE non-nil means interpret DELTA as
  1571. number of pixels."
  1572. (setq window (window-normalize-window window))
  1573. (cond
  1574. ((< delta 0)
  1575. (max (- (window-min-delta
  1576. window horizontal ignore trail noup nodown pixelwise))
  1577. delta))
  1578. ((> delta 0)
  1579. (min (window-max-delta
  1580. window horizontal ignore trail noup nodown pixelwise)
  1581. delta))
  1582. (t 0)))
  1583. (defun window--resizable-p (window delta &optional horizontal ignore trail noup nodown pixelwise)
  1584. "Return t if WINDOW can be resized vertically by DELTA lines.
  1585. WINDOW must be a valid window and defaults to the selected one.
  1586. For the meaning of the arguments of this function see the
  1587. doc-string of `window--resizable'.
  1588. Optional argument PIXELWISE non-nil means interpret DELTA as
  1589. pixels."
  1590. (setq window (window-normalize-window window))
  1591. (if (> delta 0)
  1592. (>= (window--resizable
  1593. window delta horizontal ignore trail noup nodown pixelwise)
  1594. delta)
  1595. (<= (window--resizable
  1596. window delta horizontal ignore trail noup nodown pixelwise)
  1597. delta)))
  1598. (defun window-resizable (window delta &optional horizontal ignore pixelwise)
  1599. "Return DELTA if WINDOW can be resized vertically by DELTA lines.
  1600. WINDOW must be a valid window and defaults to the selected one.
  1601. Optional argument HORIZONTAL non-nil means return DELTA if WINDOW
  1602. can be resized horizontally by DELTA columns. A return value of
  1603. zero means that WINDOW is not resizable.
  1604. DELTA positive means WINDOW shall be enlarged by DELTA lines or
  1605. columns. If WINDOW cannot be enlarged by DELTA lines or columns
  1606. return the maximum value in the range 0..DELTA by which WINDOW
  1607. can be enlarged.
  1608. DELTA negative means WINDOW shall be shrunk by -DELTA lines or
  1609. columns. If WINDOW cannot be shrunk by -DELTA lines or columns,
  1610. return the minimum value in the range DELTA..0 that can be used
  1611. for shrinking WINDOW.
  1612. Optional argument IGNORE, if non-nil, means to ignore restraints
  1613. induced by fixed size windows or the values of the variables
  1614. `window-min-height' and `window-min-width'. The following values
  1615. have special meanings: `safe' means that in addition live windows
  1616. are allowed to get as small as `window-safe-min-height' lines and
  1617. `window-safe-min-width' columns. `preserved' means to ignore
  1618. only restrictions induced by `window-preserve-size'. If IGNORE
  1619. is a window, then ignore restrictions for that window only.
  1620. Optional argument PIXELWISE non-nil means interpret DELTA as
  1621. pixels."
  1622. (setq window (window-normalize-window window))
  1623. (window--resizable window delta horizontal ignore nil nil nil pixelwise))
  1624. (defun window-resizable-p (window delta &optional horizontal ignore pixelwise)
  1625. "Return t if WINDOW can be resized vertically by DELTA lines.
  1626. WINDOW must be a valid window and defaults to the selected one.
  1627. For the meaning of the arguments of this function see the
  1628. doc-string of `window-resizable'."
  1629. (setq window (window-normalize-window window))
  1630. (if (> delta 0)
  1631. (>= (window--resizable
  1632. window delta horizontal ignore nil nil nil pixelwise)
  1633. delta)
  1634. (<= (window--resizable
  1635. window delta horizontal ignore nil nil nil pixelwise)
  1636. delta)))
  1637. ;; Aliases of functions defined in window.c.
  1638. (defalias 'window-height 'window-total-height)
  1639. (defalias 'window-width 'window-body-width)
  1640. (defun window-full-height-p (&optional window)
  1641. "Return t if WINDOW is as high as its containing frame.
  1642. More precisely, return t if and only if the total height of
  1643. WINDOW equals the total height of the root window of WINDOW's
  1644. frame. WINDOW must be a valid window and defaults to the
  1645. selected one."
  1646. (setq window (window-normalize-window window))
  1647. (if (window-minibuffer-p window)
  1648. (eq window (frame-root-window (window-frame window)))
  1649. (= (window-pixel-height window)
  1650. (window-pixel-height (frame-root-window window)))))
  1651. (defun window-full-width-p (&optional window)
  1652. "Return t if WINDOW is as wide as its containing frame.
  1653. More precisely, return t if and only if the total width of WINDOW
  1654. equals the total width of the root window of WINDOW's frame.
  1655. WINDOW must be a valid window and defaults to the selected one."
  1656. (setq window (window-normalize-window window))
  1657. (= (window-pixel-width window)
  1658. (window-pixel-width (frame-root-window window))))
  1659. (defun window-body-size (&optional window horizontal pixelwise)
  1660. "Return the height or width of WINDOW's text area.
  1661. WINDOW must be a live window and defaults to the selected one.
  1662. If HORIZONTAL is omitted or nil, return the height of the text
  1663. area, like `window-body-height'. Otherwise, return the width of
  1664. the text area, like `window-body-width'. In either case, the
  1665. optional argument PIXELWISE is passed to the functions."
  1666. (if horizontal
  1667. (window-body-width window pixelwise)
  1668. (window-body-height window pixelwise)))
  1669. (declare-function font-info "font.c" (name &optional frame))
  1670. (defun window-font-width (&optional window face)
  1671. "Return average character width for the font of FACE used in WINDOW.
  1672. WINDOW must be a live window and defaults to the selected one.
  1673. If FACE is nil or omitted, the default face is used. If FACE is
  1674. remapped (see `face-remapping-alist'), the function returns the
  1675. information for the remapped face."
  1676. (with-selected-window (window-normalize-window window t)
  1677. (if (display-multi-font-p)
  1678. (let* ((face (if face face 'default))
  1679. (info (font-info (face-font face)))
  1680. (width (aref info 11)))
  1681. (if (> width 0)
  1682. width
  1683. (aref info 10)))
  1684. (frame-char-width))))
  1685. (defun window-font-height (&optional window face)
  1686. "Return character height for the font of FACE used in WINDOW.
  1687. WINDOW must be a live window and defaults to the selected one.
  1688. If FACE is nil or omitted, the default face is used. If FACE is
  1689. remapped (see `face-remapping-alist'), the function returns the
  1690. information for the remapped face."
  1691. (with-selected-window (window-normalize-window window t)
  1692. (if (display-multi-font-p)
  1693. (let* ((face (if face face 'default))
  1694. (info (font-info (face-font face))))
  1695. (aref info 3))
  1696. (frame-char-height))))
  1697. (defvar overflow-newline-into-fringe)
  1698. (defun window-max-chars-per-line (&optional window face)
  1699. "Return the number of characters that can be displayed on one line in WINDOW.
  1700. WINDOW must be a live window and defaults to the selected one.
  1701. The character width of FACE is used for the calculation. If FACE
  1702. is nil or omitted, the default face is used. If FACE is
  1703. remapped (see `face-remapping-alist'), the function uses the
  1704. remapped face.
  1705. This function is different from `window-body-width' in two
  1706. ways. First, it accounts for the portions of the line reserved
  1707. for the continuation glyph. Second, it accounts for the size of
  1708. the font."
  1709. (with-selected-window (window-normalize-window window t)
  1710. (let* ((window-width (window-body-width window t))
  1711. (font-width (window-font-width window face))
  1712. (ncols (/ window-width font-width)))
  1713. (if (and (display-graphic-p)
  1714. overflow-newline-into-fringe
  1715. (/= (frame-parameter nil 'left-fringe) 0)
  1716. (/= (frame-parameter nil 'right-fringe) 0))
  1717. ncols
  1718. (1- ncols)))))
  1719. (defun window-current-scroll-bars (&optional window)
  1720. "Return the current scroll bar types for WINDOW.
  1721. WINDOW must be a live window and defaults to the selected one.
  1722. The return value is a cons cell (VERTICAL . HORIZONTAL) where
  1723. VERTICAL specifies the current location of the vertical scroll
  1724. bar (`left', `right' or nil), and HORIZONTAL specifies the
  1725. current location of the horizontal scroll bar (`bottom' or nil).
  1726. Unlike `window-scroll-bars', this function reports the scroll bar
  1727. type actually used, once frame defaults and `scroll-bar-mode' are
  1728. taken into account."
  1729. (setq window (window-normalize-window window t))
  1730. (let ((vertical (nth 2 (window-scroll-bars window)))
  1731. (horizontal (nth 5 (window-scroll-bars window)))
  1732. (inherited (frame-current-scroll-bars (window-frame window))))
  1733. (when (eq vertical t)
  1734. (setq vertical (car inherited)))
  1735. (when (eq horizontal t)
  1736. (setq horizontal (cdr inherited)))
  1737. (cons vertical (and horizontal 'bottom))))
  1738. (defun walk-windows (fun &optional minibuf all-frames)
  1739. "Cycle through all live windows, calling FUN for each one.
  1740. FUN must specify a function with a window as its sole argument.
  1741. The optional arguments MINIBUF and ALL-FRAMES specify the set of
  1742. windows to include in the walk.
  1743. MINIBUF t means include the minibuffer window even if the
  1744. minibuffer is not active. MINIBUF nil or omitted means include
  1745. the minibuffer window only if the minibuffer is active. Any
  1746. other value means do not include the minibuffer window even if
  1747. the minibuffer is active.
  1748. ALL-FRAMES nil or omitted means consider all windows on the
  1749. selected frame, plus the minibuffer window if specified by the
  1750. MINIBUF argument. If the minibuffer counts, consider all windows
  1751. on all frames that share that minibuffer too. The following
  1752. non-nil values of ALL-FRAMES have special meanings:
  1753. - t means consider all windows on all existing frames.
  1754. - `visible' means consider all windows on all visible frames on
  1755. the current terminal.
  1756. - 0 (the number zero) means consider all windows on all visible
  1757. and iconified frames on the current terminal.
  1758. - A frame means consider all windows on that frame only.
  1759. Anything else means consider all windows on the selected frame
  1760. and no others.
  1761. This function changes neither the order of recently selected
  1762. windows nor the buffer list."
  1763. ;; If we start from the minibuffer window, don't fail to come
  1764. ;; back to it.
  1765. (when (window-minibuffer-p)
  1766. (setq minibuf t))
  1767. ;; Make sure to not mess up the order of recently selected
  1768. ;; windows. Use `save-selected-window' and `select-window'
  1769. ;; with second argument non-nil for this purpose.
  1770. (save-selected-window
  1771. (when (framep all-frames)
  1772. (select-window (frame-first-window all-frames) 'norecord))
  1773. (dolist (walk-windows-window (window-list-1 nil minibuf all-frames))
  1774. (funcall fun walk-windows-window))))
  1775. (defun window-at-side-p (&optional window side)
  1776. "Return t if WINDOW is at SIDE of its containing frame.
  1777. WINDOW must be a valid window and defaults to the selected one.
  1778. SIDE can be any of the symbols `left', `top', `right' or
  1779. `bottom'. The default value nil is handled like `bottom'."
  1780. (setq window (window-normalize-window window))
  1781. (let ((edge
  1782. (cond
  1783. ((eq side 'left) 0)
  1784. ((eq side 'top) 1)
  1785. ((eq side 'right) 2)
  1786. ((memq side '(bottom nil)) 3))))
  1787. (= (nth edge (window-pixel-edges window))
  1788. (nth edge (window-pixel-edges (frame-root-window window))))))
  1789. (defun window-at-side-list (&optional frame side)
  1790. "Return list of all windows on SIDE of FRAME.
  1791. FRAME must be a live frame and defaults to the selected frame.
  1792. SIDE can be any of the symbols `left', `top', `right' or
  1793. `bottom'. The default value nil is handled like `bottom'."
  1794. (setq frame (window-normalize-frame frame))
  1795. (let (windows)
  1796. (walk-window-tree
  1797. (lambda (window)
  1798. (when (window-at-side-p window side)
  1799. (setq windows (cons window windows))))
  1800. frame nil 'nomini)
  1801. (nreverse windows)))
  1802. (defun window--in-direction-2 (window posn &optional horizontal)
  1803. "Support function for `window-in-direction'."
  1804. (if horizontal
  1805. (let ((top (window-pixel-top window)))
  1806. (if (> top posn)
  1807. (- top posn)
  1808. (- posn top (window-pixel-height window))))
  1809. (let ((left (window-pixel-left window)))
  1810. (if (> left posn)
  1811. (- left posn)
  1812. (- posn left (window-pixel-width window))))))
  1813. ;; Predecessors to the below have been devised by Julian Assange in
  1814. ;; change-windows-intuitively.el and Hovav Shacham in windmove.el.
  1815. ;; Neither of these allow to selectively ignore specific windows
  1816. ;; (windows whose `no-other-window' parameter is non-nil) as targets of
  1817. ;; the movement.
  1818. (defun window-in-direction (direction &optional window ignore sign wrap mini)
  1819. "Return window in DIRECTION as seen from WINDOW.
  1820. More precisely, return the nearest window in direction DIRECTION
  1821. as seen from the position of `window-point' in window WINDOW.
  1822. DIRECTION must be one of `above', `below', `left' or `right'.
  1823. WINDOW must be a live window and defaults to the selected one.
  1824. Do not return a window whose `no-other-window' parameter is
  1825. non-nil. If the nearest window's `no-other-window' parameter is
  1826. non-nil, try to find another window in the indicated direction.
  1827. If, however, the optional argument IGNORE is non-nil, return that
  1828. window even if its `no-other-window' parameter is non-nil.
  1829. Optional argument SIGN a negative number means to use the right
  1830. or bottom edge of WINDOW as reference position instead of
  1831. `window-point'. SIGN a positive number means to use the left or
  1832. top edge of WINDOW as reference position.
  1833. Optional argument WRAP non-nil means to wrap DIRECTION around
  1834. frame borders. This means to return for WINDOW at the top of the
  1835. frame and DIRECTION `above' the minibuffer window if the frame
  1836. has one, and a window at the bottom of the frame otherwise.
  1837. Optional argument MINI nil means to return the minibuffer window
  1838. if and only if it is currently active. MINI non-nil means to
  1839. return the minibuffer window even when it's not active. However,
  1840. if WRAP non-nil, always act as if MINI were nil.
  1841. Return nil if no suitable window can be found."
  1842. (setq window (window-normalize-window window t))
  1843. (unless (memq direction '(above below left right))
  1844. (error "Wrong direction %s" direction))
  1845. (let* ((frame (window-frame window))
  1846. (hor (memq direction '(left right)))
  1847. (first (if hor
  1848. (window-pixel-left window)
  1849. (window-pixel-top window)))
  1850. (last (+ first (window-size window hor t)))
  1851. ;; The column / row value of `posn-at-point' can be nil for the
  1852. ;; mini-window, guard against that.
  1853. (posn
  1854. (cond
  1855. ((and (numberp sign) (< sign 0))
  1856. (if hor
  1857. (1- (+ (window-pixel-top window) (window-pixel-height window)))
  1858. (1- (+ (window-pixel-left window) (window-pixel-width window)))))
  1859. ((and (numberp sign) (> sign 0))
  1860. (if hor
  1861. (window-pixel-top window)
  1862. (window-pixel-left window)))
  1863. ((let ((posn-cons (nth 2 (posn-at-point (window-point window) window))))
  1864. (if hor
  1865. (+ (or (cdr posn-cons) 1) (window-pixel-top window))
  1866. (+ (or (car posn-cons) 1) (window-pixel-left window)))))))
  1867. (best-edge
  1868. (cond
  1869. ((eq direction 'below) (frame-pixel-height frame))
  1870. ((eq direction 'right) (frame-pixel-width frame))
  1871. (t -1)))
  1872. (best-edge-2 best-edge)
  1873. (best-diff-2 (if hor (frame-pixel-height frame) (frame-pixel-width frame)))
  1874. best best-2 best-diff-2-new)
  1875. (walk-window-tree
  1876. (lambda (w)
  1877. (let* ((w-top (window-pixel-top w))
  1878. (w-left (window-pixel-left w)))
  1879. (cond
  1880. ((or (eq window w)
  1881. ;; Ignore ourselves.
  1882. (and (window-parameter w 'no-other-window)
  1883. ;; Ignore W unless IGNORE is non-nil.
  1884. (not ignore))))
  1885. (hor
  1886. (cond
  1887. ((and (<= w-top posn)
  1888. (< posn (+ w-top (window-pixel-height w))))
  1889. ;; W is to the left or right of WINDOW and covers POSN.
  1890. (when (or (and (eq direction 'left)
  1891. (or (and (<= w-left first) (> w-left best-edge))
  1892. (and wrap
  1893. (window-at-side-p window 'left)
  1894. (window-at-side-p w 'right))))
  1895. (and (eq direction 'right)
  1896. (or (and (>= w-left last) (< w-left best-edge))
  1897. (and wrap
  1898. (window-at-side-p window 'right)
  1899. (window-at-side-p w 'left)))))
  1900. (setq best-edge w-left)
  1901. (setq best w)))
  1902. ((and (or (and (eq direction 'left)
  1903. (<= (+ w-left (window-pixel-width w)) first))
  1904. (and (eq direction 'right) (<= last w-left)))
  1905. ;; W is to the left or right of WINDOW but does not
  1906. ;; cover POSN.
  1907. (setq best-diff-2-new
  1908. (window--in-direction-2 w posn hor))
  1909. (or (< best-diff-2-new best-diff-2)
  1910. (and (= best-diff-2-new best-diff-2)
  1911. (if (eq direction 'left)
  1912. (> w-left best-edge-2)
  1913. (< w-left best-edge-2)))))
  1914. (setq best-edge-2 w-left)
  1915. (setq best-diff-2 best-diff-2-new)
  1916. (setq best-2 w))))
  1917. ((and (<= w-left posn)
  1918. (< posn (+ w-left (window-pixel-width w))))
  1919. ;; W is above or below WINDOW and covers POSN.
  1920. (when (or (and (eq direction 'above)
  1921. (or (and (<= w-top first) (> w-top best-edge))
  1922. (and wrap
  1923. (window-at-side-p window 'top)
  1924. (if (active-minibuffer-window)
  1925. (minibuffer-window-active-p w)
  1926. (window-at-side-p w 'bottom)))))
  1927. (and (eq direction 'below)
  1928. (or (and (>= w-top first) (< w-top best-edge))
  1929. (and wrap
  1930. (if (active-minibuffer-window)
  1931. (minibuffer-window-active-p window)
  1932. (window-at-side-p window 'bottom))
  1933. (window-at-side-p w 'top)))))
  1934. (setq best-edge w-top)
  1935. (setq best w)))
  1936. ((and (or (and (eq direction 'above)
  1937. (<= (+ w-top (window-pixel-height w)) first))
  1938. (and (eq direction 'below) (<= last w-top)))
  1939. ;; W is above or below WINDOW but does not cover POSN.
  1940. (setq best-diff-2-new
  1941. (window--in-direction-2 w posn hor))
  1942. (or (< best-diff-2-new best-diff-2)
  1943. (and (= best-diff-2-new best-diff-2)
  1944. (if (eq direction 'above)
  1945. (> w-top best-edge-2)
  1946. (< w-top best-edge-2)))))
  1947. (setq best-edge-2 w-top)
  1948. (setq best-diff-2 best-diff-2-new)
  1949. (setq best-2 w)))))
  1950. frame nil (and mini t))
  1951. (or best best-2)))
  1952. (defun get-window-with-predicate (predicate &optional minibuf all-frames default)
  1953. "Return a live window satisfying PREDICATE.
  1954. More precisely, cycle through all windows calling the function
  1955. PREDICATE on each one of them with the window as its sole
  1956. argument. Return the first window for which PREDICATE returns
  1957. non-nil. Windows are scanned starting with the window following
  1958. the selected window. If no window satisfies PREDICATE, return
  1959. DEFAULT.
  1960. MINIBUF t means include the minibuffer window even if the
  1961. minibuffer is not active. MINIBUF nil or omitted means include
  1962. the minibuffer window only if the minibuffer is active. Any
  1963. other value means do not include the minibuffer window even if
  1964. the minibuffer is active.
  1965. ALL-FRAMES nil or omitted means consider all windows on the selected
  1966. frame, plus the minibuffer window if specified by the MINIBUF
  1967. argument. If the minibuffer counts, consider all windows on all
  1968. frames that share that minibuffer too. The following non-nil
  1969. values of ALL-FRAMES have special meanings:
  1970. - t means consider all windows on all existing frames.
  1971. - `visible' means consider all windows on all visible frames on
  1972. the current terminal.
  1973. - 0 (the number zero) means consider all windows on all visible
  1974. and iconified frames on the current terminal.
  1975. - A frame means consider all windows on that frame only.
  1976. Anything else means consider all windows on the selected frame
  1977. and no others."
  1978. (catch 'found
  1979. (dolist (window (window-list-1
  1980. (next-window nil minibuf all-frames)
  1981. minibuf all-frames))
  1982. (when (funcall predicate window)
  1983. (throw 'found window)))
  1984. default))
  1985. (defalias 'some-window 'get-window-with-predicate)
  1986. (defun get-lru-window (&optional all-frames dedicated not-selected)
  1987. "Return the least recently used window on frames specified by ALL-FRAMES.
  1988. Return a full-width window if possible. A minibuffer window is
  1989. never a candidate. A dedicated window is never a candidate
  1990. unless DEDICATED is non-nil, so if all windows are dedicated, the
  1991. value is nil. Avoid returning the selected window if possible.
  1992. Optional argument NOT-SELECTED non-nil means never return the
  1993. selected window.
  1994. The following non-nil values of the optional argument ALL-FRAMES
  1995. have special meanings:
  1996. - t means consider all windows on all existing frames.
  1997. - `visible' means consider all windows on all visible frames on
  1998. the current terminal.
  1999. - 0 (the number zero) means consider all windows on all visible
  2000. and iconified frames on the current terminal.
  2001. - A frame means consider all windows on that frame only.
  2002. Any other value of ALL-FRAMES means consider all windows on the
  2003. selected frame and no others."
  2004. (let (best-window best-time second-best-window second-best-time time)
  2005. (dolist (window (window-list-1 nil 'nomini all-frames))
  2006. (when (and (or dedicated (not (window-dedicated-p window)))
  2007. (or (not not-selected) (not (eq window (selected-window)))))
  2008. (setq time (window-use-time window))
  2009. (if (or (eq window (selected-window))
  2010. (not (window-full-width-p window)))
  2011. (when (or (not second-best-time) (< time second-best-time))
  2012. (setq second-best-time time)
  2013. (setq second-best-window window))
  2014. (when (or (not best-time) (< time best-time))
  2015. (setq best-time time)
  2016. (setq best-window window)))))
  2017. (or best-window second-best-window)))
  2018. (defun get-mru-window (&optional all-frames dedicated not-selected)
  2019. "Return the most recently used window on frames specified by ALL-FRAMES.
  2020. A minibuffer window is never a candidate. A dedicated window is
  2021. never a candidate unless DEDICATED is non-nil, so if all windows
  2022. are dedicated, the value is nil. Optional argument NOT-SELECTED
  2023. non-nil means never return the selected window.
  2024. The following non-nil values of the optional argument ALL-FRAMES
  2025. have special meanings:
  2026. - t means consider all windows on all existing frames.
  2027. - `visible' means consider all windows on all visible frames on
  2028. the current terminal.
  2029. - 0 (the number zero) means consider all windows on all visible
  2030. and iconified frames on the current terminal.
  2031. - A frame means consider all windows on that frame only.
  2032. Any other value of ALL-FRAMES means consider all windows on the
  2033. selected frame and no others."
  2034. (let (best-window best-time time)
  2035. (dolist (window (window-list-1 nil 'nomini all-frames))
  2036. (setq time (window-use-time window))
  2037. (when (and (or dedicated (not (window-dedicated-p window)))
  2038. (or (not not-selected) (not (eq window (selected-window))))
  2039. (or (not best-time) (> time best-time)))
  2040. (setq best-time time)
  2041. (setq best-window window)))
  2042. best-window))
  2043. (defun get-largest-window (&optional all-frames dedicated not-selected)
  2044. "Return the largest window on frames specified by ALL-FRAMES.
  2045. A minibuffer window is never a candidate. A dedicated window is
  2046. never a candidate unless DEDICATED is non-nil, so if all windows
  2047. are dedicated, the value is nil. Optional argument NOT-SELECTED
  2048. non-nil means never return the selected window.
  2049. The following non-nil values of the optional argument ALL-FRAMES
  2050. have special meanings:
  2051. - t means consider all windows on all existing frames.
  2052. - `visible' means consider all windows on all visible frames on
  2053. the current terminal.
  2054. - 0 (the number zero) means consider all windows on all visible
  2055. and iconified frames on the current terminal.
  2056. - A frame means consider all windows on that frame only.
  2057. Any other value of ALL-FRAMES means consider all windows on the
  2058. selected frame and no others."
  2059. (let ((best-size 0)
  2060. best-window size)
  2061. (dolist (window (window-list-1 nil 'nomini all-frames))
  2062. (when (and (or dedicated (not (window-dedicated-p window)))
  2063. (or (not not-selected) (not (eq window (selected-window)))))
  2064. (setq size (* (window-pixel-height window)
  2065. (window-pixel-width window)))
  2066. (when (> size best-size)
  2067. (setq best-size size)
  2068. (setq best-window window))))
  2069. best-window))
  2070. (defun get-buffer-window-list (&optional buffer-or-name minibuf all-frames)
  2071. "Return list of all windows displaying BUFFER-OR-NAME, or nil if none.
  2072. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  2073. and defaults to the current buffer. If the selected window displays
  2074. BUFFER-OR-NAME, it will be the first in the resulting list.
  2075. MINIBUF t means include the minibuffer window even if the
  2076. minibuffer is not active. MINIBUF nil or omitted means include
  2077. the minibuffer window only if the minibuffer is active. Any
  2078. other value means do not include the minibuffer window even if
  2079. the minibuffer is active.
  2080. ALL-FRAMES nil or omitted means consider all windows on the
  2081. selected frame, plus the minibuffer window if specified by the
  2082. MINIBUF argument. If the minibuffer counts, consider all windows
  2083. on all frames that share that minibuffer too. The following
  2084. non-nil values of ALL-FRAMES have special meanings:
  2085. - t means consider all windows on all existing frames.
  2086. - `visible' means consider all windows on all visible frames on
  2087. the current terminal.
  2088. - 0 (the number zero) means consider all windows on all visible
  2089. and iconified frames on the current terminal.
  2090. - A frame means consider all windows on that frame only.
  2091. Anything else means consider all windows on the selected frame
  2092. and no others."
  2093. (let ((buffer (window-normalize-buffer buffer-or-name))
  2094. windows)
  2095. (dolist (window (window-list-1 (selected-window) minibuf all-frames))
  2096. (when (eq (window-buffer window) buffer)
  2097. (setq windows (cons window windows))))
  2098. (nreverse windows)))
  2099. (defun minibuffer-window-active-p (window)
  2100. "Return t if WINDOW is the currently active minibuffer window."
  2101. (eq window (active-minibuffer-window)))
  2102. (defun count-windows (&optional minibuf)
  2103. "Return the number of live windows on the selected frame.
  2104. The optional argument MINIBUF specifies whether the minibuffer
  2105. window shall be counted. See `walk-windows' for the precise
  2106. meaning of this argument."
  2107. (length (window-list-1 nil minibuf)))
  2108. ;;; Resizing windows.
  2109. (defun window--size-to-pixel (window size &optional horizontal pixelwise round-maybe)
  2110. "For WINDOW convert SIZE lines to pixels.
  2111. SIZE is supposed to specify a height of WINDOW in terms of text
  2112. lines. The return value is the number of pixels specifying that
  2113. height.
  2114. WINDOW must be a valid window. Optional argument HORIZONTAL
  2115. non-nil means convert SIZE columns to pixels.
  2116. Optional argument PIXELWISE non-nil means SIZE already specifies
  2117. pixels but may have to be adjusted to a multiple of the character
  2118. size of WINDOW's frame. Optional argument ROUND-MAYBE non-nil
  2119. means round to the nearest multiple of the character size of
  2120. WINDOW's frame if the option `window-resize-pixelwise' is nil."
  2121. (setq window (window-normalize-window window))
  2122. (let ((char-size (frame-char-size window horizontal)))
  2123. (if pixelwise
  2124. (if (and round-maybe (not window-resize-pixelwise))
  2125. (* (round size char-size) char-size)
  2126. size)
  2127. (* size char-size))))
  2128. (defun window--pixel-to-total-1 (window horizontal char-size)
  2129. "Subroutine of `window--pixel-to-total'."
  2130. (let ((child (window-child window)))
  2131. (if (window-combination-p window horizontal)
  2132. ;; In an iso-combination distribute sizes proportionally.
  2133. (let ((remainder (window-new-total window))
  2134. size best-child rem best-rem)
  2135. ;; Initialize total sizes to each child's floor.
  2136. (while child
  2137. (setq size (max (/ (window-size child horizontal t) char-size) 1))
  2138. (set-window-new-total child size)
  2139. (setq remainder (- remainder size))
  2140. (setq child (window-next-sibling child)))
  2141. ;; Distribute remainder.
  2142. (while (> remainder 0)
  2143. (setq child (window-last-child window))
  2144. (setq best-child nil)
  2145. (setq best-rem 0)
  2146. (while child
  2147. (when (and (<= (window-new-total child)
  2148. (/ (window-size child horizontal t) char-size))
  2149. (> (setq rem (% (window-size child horizontal t)
  2150. char-size))
  2151. best-rem))
  2152. (setq best-child child)
  2153. (setq best-rem rem))
  2154. (setq child (window-prev-sibling child)))
  2155. ;; We MUST have a best-child here.
  2156. (set-window-new-total best-child 1 t)
  2157. (setq remainder (1- remainder)))
  2158. ;; Recurse.
  2159. (setq child (window-child window))
  2160. (while child
  2161. (window--pixel-to-total-1 child horizontal char-size)
  2162. (setq child (window-next-sibling child))))
  2163. ;; In an ortho-combination assign new sizes directly.
  2164. (let ((size (window-new-total window)))
  2165. (while child
  2166. (set-window-new-total child size)
  2167. (window--pixel-to-total-1 child horizontal char-size)
  2168. (setq child (window-next-sibling child)))))))
  2169. (defun window--pixel-to-total (&optional frame horizontal)
  2170. "On FRAME assign new total window heights from pixel heights.
  2171. FRAME must be a live frame and defaults to the selected frame.
  2172. Optional argument HORIZONTAL non-nil means assign new total
  2173. window widths from pixel widths."
  2174. (setq frame (window-normalize-frame frame))
  2175. (let* ((char-size (frame-char-size frame horizontal))
  2176. (root (frame-root-window frame))
  2177. (root-size (window-size root horizontal t))
  2178. ;; We have to care about the minibuffer window only if it
  2179. ;; appears together with the root window on this frame.
  2180. (mini (let ((mini (minibuffer-window frame)))
  2181. (and (eq (window-frame mini) frame)
  2182. (not (eq mini root)) mini)))
  2183. (mini-size (and mini (window-size mini horizontal t))))
  2184. ;; We round the line/column sizes of windows here to the nearest
  2185. ;; integer. In some cases this can make windows appear _larger_
  2186. ;; than the containing frame (line/column-wise) because the latter's
  2187. ;; sizes are not (yet) rounded. We might eventually fix that.
  2188. (if (and mini (not horizontal))
  2189. (let (lines)
  2190. (set-window-new-total root (max (/ root-size char-size) 1))
  2191. (set-window-new-total mini (max (/ mini-size char-size) 1))
  2192. (setq lines (- (round (+ root-size mini-size) char-size)
  2193. (+ (window-new-total root) (window-new-total mini))))
  2194. (while (> lines 0)
  2195. (if (>= (% root-size (window-new-total root))
  2196. (% mini-size (window-new-total mini)))
  2197. (set-window-new-total root 1 t)
  2198. (set-window-new-total mini 1 t))
  2199. (setq lines (1- lines))))
  2200. (set-window-new-total root (round root-size char-size))
  2201. (when mini
  2202. ;; This is taken in the horizontal case only.
  2203. (set-window-new-total mini (round mini-size char-size))))
  2204. (unless (window-buffer root)
  2205. (window--pixel-to-total-1 root horizontal char-size))
  2206. ;; Apply the new sizes.
  2207. (window-resize-apply-total frame horizontal)))
  2208. (defun window--resize-reset (&optional frame horizontal)
  2209. "Reset resize values for all windows on FRAME.
  2210. FRAME defaults to the selected frame.
  2211. This function stores the current value of `window-size' applied
  2212. with argument HORIZONTAL in the new total size of all windows on
  2213. FRAME. It also resets the new normal size of each of these
  2214. windows."
  2215. (window--resize-reset-1
  2216. (frame-root-window (window-normalize-frame frame)) horizontal))
  2217. (defun window--resize-reset-1 (window horizontal)
  2218. "Internal function of `window--resize-reset'."
  2219. ;; Register old size in the new total size.
  2220. (set-window-new-pixel window (window-size window horizontal t))
  2221. (set-window-new-total window (window-size window horizontal))
  2222. ;; Reset new normal size.
  2223. (set-window-new-normal window)
  2224. (when (window-child window)
  2225. (window--resize-reset-1 (window-child window) horizontal))
  2226. (when (window-right window)
  2227. (window--resize-reset-1 (window-right window) horizontal)))
  2228. ;; The following routine is used to manually resize the minibuffer
  2229. ;; window and is currently used, for example, by ispell.el.
  2230. (defun window--resize-mini-window (window delta)
  2231. "Resize minibuffer window WINDOW by DELTA pixels.
  2232. If WINDOW cannot be resized by DELTA pixels make it as large (or
  2233. as small) as possible, but don't signal an error."
  2234. (when (window-minibuffer-p window)
  2235. (let* ((frame (window-frame window))
  2236. (root (frame-root-window frame))
  2237. (height (window-pixel-height window))
  2238. (min-delta
  2239. (- (window-pixel-height root)
  2240. (window-min-size root nil nil t))))
  2241. ;; Sanitize DELTA.
  2242. (cond
  2243. ((<= (+ height delta) 0)
  2244. (setq delta (- (frame-char-height (window-frame window)) height)))
  2245. ((> delta min-delta)
  2246. (setq delta min-delta)))
  2247. (unless (zerop delta)
  2248. ;; Resize now.
  2249. (window--resize-reset frame)
  2250. ;; Ideally we should be able to resize just the last child of root
  2251. ;; here. See the comment in `resize-root-window-vertically' for
  2252. ;; why we do not do that.
  2253. (window--resize-this-window root (- delta) nil nil t)
  2254. (set-window-new-pixel window (+ height delta))
  2255. ;; The following routine catches the case where we want to resize
  2256. ;; a minibuffer-only frame.
  2257. (when (resize-mini-window-internal window)
  2258. (window--pixel-to-total frame)
  2259. (run-window-configuration-change-hook frame))))))
  2260. (defun window--resize-apply-p (frame &optional horizontal)
  2261. "Return t when a window on FRAME shall be resized vertically.
  2262. Optional argument HORIZONTAL non-nil means return t when a window
  2263. shall be resized horizontally."
  2264. (catch 'apply
  2265. (walk-window-tree
  2266. (lambda (window)
  2267. (unless (= (window-new-pixel window)
  2268. (window-size window horizontal t))
  2269. (throw 'apply t)))
  2270. frame t)
  2271. nil))
  2272. (defun window-resize (window delta &optional horizontal ignore pixelwise)
  2273. "Resize WINDOW vertically by DELTA lines.
  2274. WINDOW can be an arbitrary window and defaults to the selected
  2275. one. An attempt to resize the root window of a frame will raise
  2276. an error though.
  2277. DELTA a positive number means WINDOW shall be enlarged by DELTA
  2278. lines. DELTA negative means WINDOW shall be shrunk by -DELTA
  2279. lines.
  2280. Optional argument HORIZONTAL non-nil means resize WINDOW
  2281. horizontally by DELTA columns. In this case a positive DELTA
  2282. means enlarge WINDOW by DELTA columns. DELTA negative means
  2283. WINDOW shall be shrunk by -DELTA columns.
  2284. Optional argument IGNORE, if non-nil, means to ignore restraints
  2285. induced by fixed size windows or the values of the variables
  2286. `window-min-height' and `window-min-width'. The following values
  2287. have special meanings: `safe' means that in addition live windows
  2288. are allowed to get as small as `window-safe-min-height' lines and
  2289. `window-safe-min-width' columns. `preserved' means to ignore
  2290. only restrictions induced by `window-preserve-size'. If IGNORE
  2291. is a window, then ignore restrictions for that window only.
  2292. Optional argument PIXELWISE non-nil means resize WINDOW by DELTA
  2293. pixels.
  2294. This function resizes other windows proportionally and never
  2295. deletes any windows. If you want to move only the low (right)
  2296. edge of WINDOW consider using `adjust-window-trailing-edge'
  2297. instead."
  2298. (setq window (window-normalize-window window))
  2299. (let* ((frame (window-frame window))
  2300. (minibuffer-window (minibuffer-window frame))
  2301. sibling)
  2302. (setq delta (window--size-to-pixel
  2303. window delta horizontal pixelwise t))
  2304. (cond
  2305. ((eq window (frame-root-window frame))
  2306. (error "Cannot resize the root window of a frame"))
  2307. ((window-minibuffer-p window)
  2308. (if horizontal
  2309. (error "Cannot resize minibuffer window horizontally")
  2310. (window--resize-mini-window window delta)))
  2311. ((and (not horizontal)
  2312. (window-full-height-p window)
  2313. (eq (window-frame minibuffer-window) frame)
  2314. (or (not resize-mini-windows)
  2315. (eq minibuffer-window (active-minibuffer-window))))
  2316. ;; If WINDOW is full height and either `resize-mini-windows' is
  2317. ;; nil or the minibuffer window is active, resize the minibuffer
  2318. ;; window.
  2319. (window--resize-mini-window minibuffer-window (- delta)))
  2320. ((or (window--resizable-p
  2321. window delta horizontal ignore nil nil nil t)
  2322. (and (not ignore)
  2323. (setq ignore 'preserved)
  2324. (window--resizable-p
  2325. window delta horizontal ignore nil nil nil t)))
  2326. (window--resize-reset frame horizontal)
  2327. (window--resize-this-window window delta horizontal ignore t)
  2328. (if (and (not window-combination-resize)
  2329. (window-combined-p window horizontal)
  2330. (setq sibling (or (window-right window) (window-left window)))
  2331. (window-sizable-p
  2332. sibling (- delta) horizontal ignore t))
  2333. ;; If window-combination-resize is nil, WINDOW is part of an
  2334. ;; iso-combination, and WINDOW's neighboring right or left
  2335. ;; sibling can be resized as requested, resize that sibling.
  2336. (let ((normal-delta
  2337. (/ (float delta)
  2338. (window-size (window-parent window) horizontal t))))
  2339. (window--resize-this-window sibling (- delta) horizontal nil t)
  2340. (set-window-new-normal
  2341. window (+ (window-normal-size window horizontal)
  2342. normal-delta))
  2343. (set-window-new-normal
  2344. sibling (- (window-normal-size sibling horizontal)
  2345. normal-delta)))
  2346. ;; Otherwise, resize all other windows in the same combination.
  2347. (window--resize-siblings window delta horizontal ignore))
  2348. (when (window--resize-apply-p frame horizontal)
  2349. (if (window-resize-apply frame horizontal)
  2350. (progn
  2351. (window--pixel-to-total frame horizontal)
  2352. (run-window-configuration-change-hook frame))
  2353. (error "Failed to apply resizing %s" window))))
  2354. (t
  2355. (error "Cannot resize window %s" window)))))
  2356. (defun window-resize-no-error (window delta &optional horizontal ignore pixelwise)
  2357. "Resize WINDOW vertically if it is resizable by DELTA lines.
  2358. This function is like `window-resize' but does not signal an
  2359. error when WINDOW cannot be resized. For the meaning of the
  2360. optional arguments see the documentation of `window-resize'.
  2361. Optional argument PIXELWISE non-nil means interpret DELTA as
  2362. pixels."
  2363. (when (window--resizable-p
  2364. window delta horizontal ignore nil nil nil pixelwise)
  2365. (window-resize window delta horizontal ignore pixelwise)))
  2366. (defun window--resize-child-windows-skip-p (window)
  2367. "Return non-nil if WINDOW shall be skipped by resizing routines."
  2368. (memq (window-new-normal window) '(ignore stuck skip)))
  2369. (defun window--resize-child-windows-normal (parent horizontal window this-delta &optional trail other-delta)
  2370. "Recursively set new normal height of child windows of window PARENT.
  2371. HORIZONTAL non-nil means set the new normal width of these
  2372. windows. WINDOW specifies a child window of PARENT that has been
  2373. resized by THIS-DELTA lines (columns).
  2374. Optional argument TRAIL either `before' or `after' means set values
  2375. only for windows before or after WINDOW. Optional argument
  2376. OTHER-DELTA, a number, specifies that this many lines (columns)
  2377. have been obtained from (or returned to) an ancestor window of
  2378. PARENT in order to resize WINDOW."
  2379. (let* ((delta-normal
  2380. (if (and (= (- this-delta)
  2381. (window-size window horizontal t))
  2382. (zerop other-delta))
  2383. ;; When WINDOW gets deleted and we can return its entire
  2384. ;; space to its siblings, use WINDOW's normal size as the
  2385. ;; normal delta.
  2386. (- (window-normal-size window horizontal))
  2387. ;; In any other case calculate the normal delta from the
  2388. ;; relation of THIS-DELTA to the total size of PARENT.
  2389. (/ (float this-delta)
  2390. (window-size parent horizontal t))))
  2391. (sub (window-child parent))
  2392. (parent-normal 0.0)
  2393. (skip (eq trail 'after)))
  2394. ;; Set parent-normal to the sum of the normal sizes of all child
  2395. ;; windows of PARENT that shall be resized, excluding only WINDOW
  2396. ;; and any windows specified by the optional TRAIL argument.
  2397. (while sub
  2398. (cond
  2399. ((eq sub window)
  2400. (setq skip (eq trail 'before)))
  2401. (skip)
  2402. (t
  2403. (setq parent-normal
  2404. (+ parent-normal (window-normal-size sub horizontal)))))
  2405. (setq sub (window-right sub)))
  2406. ;; Set the new normal size of all child windows of PARENT from what
  2407. ;; they should have contributed for recovering THIS-DELTA lines
  2408. ;; (columns).
  2409. (setq sub (window-child parent))
  2410. (setq skip (eq trail 'after))
  2411. (while sub
  2412. (cond
  2413. ((eq sub window)
  2414. (setq skip (eq trail 'before)))
  2415. (skip)
  2416. (t
  2417. (let ((old-normal (window-normal-size sub horizontal)))
  2418. (set-window-new-normal
  2419. sub (min 1.0 ; Don't get larger than 1.
  2420. (max (- old-normal
  2421. (* (/ old-normal parent-normal)
  2422. delta-normal))
  2423. ;; Don't drop below 0.
  2424. 0.0))))))
  2425. (setq sub (window-right sub)))
  2426. (when (numberp other-delta)
  2427. ;; Set the new normal size of windows from what they should have
  2428. ;; contributed for recovering OTHER-DELTA lines (columns).
  2429. (setq delta-normal (/ (float (window-size parent horizontal t))
  2430. (+ (window-size parent horizontal t)
  2431. other-delta)))
  2432. (setq sub (window-child parent))
  2433. (setq skip (eq trail 'after))
  2434. (while sub
  2435. (cond
  2436. ((eq sub window)
  2437. (setq skip (eq trail 'before)))
  2438. (skip)
  2439. (t
  2440. (set-window-new-normal
  2441. sub (min 1.0 ; Don't get larger than 1.
  2442. (max (* (window-new-normal sub) delta-normal)
  2443. ;; Don't drop below 0.
  2444. 0.0)))))
  2445. (setq sub (window-right sub))))
  2446. ;; Set the new normal size of WINDOW to what is left by the sum of
  2447. ;; the normal sizes of its siblings.
  2448. (set-window-new-normal
  2449. window
  2450. (let ((sum 0))
  2451. (setq sub (window-child parent))
  2452. (while sub
  2453. (cond
  2454. ((eq sub window))
  2455. ((not (numberp (window-new-normal sub)))
  2456. (setq sum (+ sum (window-normal-size sub horizontal))))
  2457. (t
  2458. (setq sum (+ sum (window-new-normal sub)))))
  2459. (setq sub (window-right sub)))
  2460. ;; Don't get larger than 1 or smaller than 0.
  2461. (min 1.0 (max (- 1.0 sum) 0.0))))))
  2462. (defun window--resize-child-windows (parent delta &optional horizontal window ignore trail edge char-size)
  2463. "Resize child windows of window PARENT vertically by DELTA pixels.
  2464. PARENT must be a vertically combined internal window.
  2465. Optional argument HORIZONTAL non-nil means resize child windows
  2466. of PARENT horizontally by DELTA pixels. In this case PARENT must
  2467. be a horizontally combined internal window.
  2468. WINDOW, if specified, must denote a child window of PARENT that
  2469. is resized by DELTA pixels.
  2470. The optional argument IGNORE has the same meaning as for
  2471. `window-resizable'.
  2472. Optional arguments TRAIL and EDGE, when non-nil, restrict the set
  2473. of windows that shall be resized. If TRAIL equals `before',
  2474. resize only windows on the left or above EDGE. If TRAIL equals
  2475. `after', resize only windows on the right or below EDGE. Also,
  2476. preferably only resize windows adjacent to EDGE.
  2477. If the optional argument CHAR-SIZE is a positive integer, it specifies
  2478. the number of pixels by which windows are incrementally resized.
  2479. If CHAR-SIZE is nil, this means to use the value of
  2480. `frame-char-height' or `frame-char-width' of WINDOW's frame.
  2481. Return the symbol `normalized' if new normal sizes have been
  2482. already set by this routine."
  2483. (let* ((first (window-child parent))
  2484. (last (window-last-child parent))
  2485. (parent-total (+ (window-size parent horizontal t)
  2486. delta))
  2487. (char-size (or char-size
  2488. (and window-resize-pixelwise 1)
  2489. (frame-char-size window horizontal)))
  2490. sub best-window best-value best-delta)
  2491. (if (and edge (memq trail '(before after))
  2492. (progn
  2493. (setq sub first)
  2494. (while (and (window-right sub)
  2495. (or (and (eq trail 'before)
  2496. (not (window--resize-child-windows-skip-p
  2497. (window-right sub))))
  2498. (and (eq trail 'after)
  2499. (window--resize-child-windows-skip-p sub))))
  2500. (setq sub (window-right sub)))
  2501. sub)
  2502. (if horizontal
  2503. (if (eq trail 'before)
  2504. (= (+ (window-pixel-left sub) (window-pixel-width sub))
  2505. edge)
  2506. (= (window-pixel-left sub) edge))
  2507. (if (eq trail 'before)
  2508. (= (+ (window-pixel-top sub) (window-pixel-height sub))
  2509. edge)
  2510. (= (window-pixel-top sub) edge)))
  2511. (window-sizable-p sub delta horizontal ignore t))
  2512. ;; Resize only windows adjacent to EDGE.
  2513. (progn
  2514. (window--resize-this-window
  2515. sub delta horizontal ignore t trail edge)
  2516. (if (and window (eq (window-parent sub) parent))
  2517. (progn
  2518. ;; Assign new normal sizes.
  2519. (set-window-new-normal
  2520. sub (/ (float (window-new-pixel sub)) parent-total))
  2521. (set-window-new-normal
  2522. window (- (window-normal-size window horizontal)
  2523. (- (window-new-normal sub)
  2524. (window-normal-size sub horizontal)))))
  2525. (window--resize-child-windows-normal
  2526. parent horizontal sub 0 trail delta))
  2527. ;; Return 'normalized to notify `window--resize-siblings' that
  2528. ;; normal sizes have been already set.
  2529. 'normalized)
  2530. ;; Resize all windows proportionally.
  2531. (setq sub last)
  2532. (while sub
  2533. (cond
  2534. ((or (window--resize-child-windows-skip-p sub)
  2535. ;; Ignore windows to skip and fixed-size child windows -
  2536. ;; in the latter case make it a window to skip.
  2537. (and (not ignore)
  2538. (window-size-fixed-p sub horizontal ignore)
  2539. (set-window-new-normal sub 'ignore))))
  2540. ((< delta 0)
  2541. ;; When shrinking store the number of lines/cols we can get
  2542. ;; from this window here together with the total/normal size
  2543. ;; factor.
  2544. (set-window-new-normal
  2545. sub
  2546. (cons
  2547. ;; We used to call this with NODOWN t, "fixed" 2011-05-11.
  2548. (window-min-delta sub horizontal ignore trail t nil t)
  2549. (- (/ (float (window-size sub horizontal t))
  2550. parent-total)
  2551. (window-normal-size sub horizontal)))))
  2552. ((> delta 0)
  2553. ;; When enlarging store the total/normal size factor only
  2554. (set-window-new-normal
  2555. sub
  2556. (- (/ (float (window-size sub horizontal t))
  2557. parent-total)
  2558. (window-normal-size sub horizontal)))))
  2559. (setq sub (window-left sub)))
  2560. (cond
  2561. ((< delta 0)
  2562. ;; Shrink windows by delta.
  2563. (setq best-window t)
  2564. (while (and best-window (not (zerop delta)))
  2565. (setq sub last)
  2566. (setq best-window nil)
  2567. (setq best-value most-negative-fixnum)
  2568. (while sub
  2569. (when (and (consp (window-new-normal sub))
  2570. (not (<= (car (window-new-normal sub)) 0))
  2571. (> (cdr (window-new-normal sub)) best-value))
  2572. (setq best-window sub)
  2573. (setq best-value (cdr (window-new-normal sub))))
  2574. (setq sub (window-left sub)))
  2575. (when best-window
  2576. (setq best-delta (min (car (window-new-normal best-window))
  2577. char-size (- delta)))
  2578. (setq delta (+ delta best-delta))
  2579. (set-window-new-pixel best-window (- best-delta) t)
  2580. (set-window-new-normal
  2581. best-window
  2582. (if (= (car (window-new-normal best-window)) best-delta)
  2583. 'skip ; We can't shrink best-window any further.
  2584. (cons (- (car (window-new-normal best-window)) best-delta)
  2585. (- (/ (float (window-new-pixel best-window))
  2586. parent-total)
  2587. (window-normal-size best-window horizontal))))))))
  2588. ((> delta 0)
  2589. ;; Enlarge windows by delta.
  2590. (setq best-window t)
  2591. (while (and best-window (not (zerop delta)))
  2592. (setq sub last)
  2593. (setq best-window nil)
  2594. (setq best-value most-positive-fixnum)
  2595. (while sub
  2596. (when (and (numberp (window-new-normal sub))
  2597. (< (window-new-normal sub) best-value))
  2598. (setq best-window sub)
  2599. (setq best-value (window-new-normal sub)))
  2600. (setq sub (window-left sub)))
  2601. (when best-window
  2602. (setq best-delta (min delta char-size))
  2603. (setq delta (- delta best-delta))
  2604. (set-window-new-pixel best-window best-delta t)
  2605. (set-window-new-normal
  2606. best-window
  2607. (- (/ (float (window-new-pixel best-window))
  2608. parent-total)
  2609. (window-normal-size best-window horizontal)))))))
  2610. (when best-window
  2611. (setq sub last)
  2612. (while sub
  2613. (when (or (consp (window-new-normal sub))
  2614. (numberp (window-new-normal sub)))
  2615. ;; Reset new normal size fields so `window-resize-apply'
  2616. ;; won't use them to apply new sizes.
  2617. (set-window-new-normal sub))
  2618. (unless (eq (window-new-normal sub) 'ignore)
  2619. ;; Resize this window's child windows (back-engineering
  2620. ;; delta from sub's old and new total sizes).
  2621. (let ((delta (- (window-new-pixel sub)
  2622. (window-size sub horizontal t))))
  2623. (unless (and (zerop delta) (not trail))
  2624. ;; For the TRAIL non-nil case we have to resize SUB
  2625. ;; recursively even if it's size does not change.
  2626. (window--resize-this-window
  2627. sub delta horizontal ignore nil trail edge))))
  2628. (setq sub (window-left sub)))))))
  2629. (defun window--resize-siblings (window delta &optional horizontal ignore trail edge char-size)
  2630. "Resize other windows when WINDOW is resized vertically by DELTA pixels.
  2631. Optional argument HORIZONTAL non-nil means resize other windows
  2632. when WINDOW is resized horizontally by DELTA pixels. WINDOW
  2633. itself is not resized by this function.
  2634. The optional argument IGNORE has the same meaning as for
  2635. `window-resizable'.
  2636. Optional arguments TRAIL and EDGE, when non-nil, refine the set
  2637. of windows that shall be resized. If TRAIL equals `before',
  2638. resize only windows on the left or above EDGE. If TRAIL equals
  2639. `after', resize only windows on the right or below EDGE. Also,
  2640. preferably only resize windows adjacent to EDGE."
  2641. (when (window-parent window)
  2642. (let* ((parent (window-parent window))
  2643. (sub (window-child parent)))
  2644. (if (window-combined-p sub horizontal)
  2645. ;; In an iso-combination try to extract DELTA from WINDOW's
  2646. ;; siblings.
  2647. (let ((skip (eq trail 'after))
  2648. this-delta other-delta)
  2649. ;; Decide which windows shall be left alone.
  2650. (while sub
  2651. (cond
  2652. ((eq sub window)
  2653. ;; Make sure WINDOW is left alone when
  2654. ;; resizing its siblings.
  2655. (set-window-new-normal sub 'ignore)
  2656. (setq skip (eq trail 'before)))
  2657. (skip
  2658. ;; Make sure this sibling is left alone when
  2659. ;; resizing its siblings.
  2660. (set-window-new-normal sub 'ignore))
  2661. ((not (window-size-fixed-p sub horizontal ignore))
  2662. ;; Set this-delta to t to signal that we found a sibling
  2663. ;; of WINDOW whose size is not fixed.
  2664. (setq this-delta t)))
  2665. (setq sub (window-right sub)))
  2666. ;; Set this-delta to what we can get from WINDOW's siblings.
  2667. (if (= (- delta) (window-size window horizontal t))
  2668. ;; A deletion, presumably. We must handle this case
  2669. ;; specially since `window--resizable' can't be used.
  2670. (if this-delta
  2671. ;; There's at least one resizable sibling we can
  2672. ;; give WINDOW's size to.
  2673. (setq this-delta delta)
  2674. ;; No resizable sibling exists.
  2675. (setq this-delta 0))
  2676. ;; Any other form of resizing.
  2677. (setq this-delta
  2678. (window--resizable
  2679. window delta horizontal ignore trail t nil t)))
  2680. ;; Set other-delta to what we still have to get from
  2681. ;; ancestor windows of parent.
  2682. (setq other-delta (- delta this-delta))
  2683. (unless (zerop other-delta)
  2684. ;; Unless we got everything from WINDOW's siblings, PARENT
  2685. ;; must be resized by other-delta lines or columns.
  2686. (set-window-new-pixel parent other-delta 'add))
  2687. (if (zerop this-delta)
  2688. ;; We haven't got anything from WINDOW's siblings but we
  2689. ;; must update the normal sizes to respect other-delta.
  2690. (window--resize-child-windows-normal
  2691. parent horizontal window this-delta trail other-delta)
  2692. ;; We did get something from WINDOW's siblings which means
  2693. ;; we have to resize their child windows.
  2694. (unless (eq (window--resize-child-windows
  2695. parent (- this-delta) horizontal
  2696. window ignore trail edge char-size)
  2697. ;; If `window--resize-child-windows' returns
  2698. ;; 'normalized, this means it has set the
  2699. ;; normal sizes already.
  2700. 'normalized)
  2701. ;; Set the normal sizes.
  2702. (window--resize-child-windows-normal
  2703. parent horizontal window this-delta trail other-delta))
  2704. ;; Set DELTA to what we still have to get from ancestor
  2705. ;; windows.
  2706. (setq delta other-delta)))
  2707. ;; In an ortho-combination all siblings of WINDOW must be
  2708. ;; resized by DELTA.
  2709. (set-window-new-pixel parent delta 'add)
  2710. (while sub
  2711. (unless (eq sub window)
  2712. (window--resize-this-window
  2713. sub delta horizontal ignore t))
  2714. (setq sub (window-right sub))))
  2715. (unless (zerop delta)
  2716. ;; "Go up."
  2717. (window--resize-siblings
  2718. parent delta horizontal ignore trail edge char-size)))))
  2719. (defun window--resize-this-window (window delta &optional horizontal ignore add trail edge char-size)
  2720. "Resize WINDOW vertically by DELTA pixels.
  2721. Optional argument HORIZONTAL non-nil means resize WINDOW
  2722. horizontally by DELTA pixels.
  2723. The optional argument IGNORE has the same meaning as for
  2724. `window-resizable'. Optional argument ADD non-nil means add
  2725. DELTA to the new total size of WINDOW.
  2726. Optional arguments TRAIL and EDGE, when non-nil, refine the set
  2727. of windows that shall be resized. If TRAIL equals `before',
  2728. resize only windows on the left or above EDGE. If TRAIL equals
  2729. `after', resize only windows on the right or below EDGE. Also,
  2730. preferably only resize windows adjacent to EDGE.
  2731. If the optional argument CHAR-SIZE is a positive integer, it specifies
  2732. the number of pixels by which windows are incrementally resized.
  2733. If CHAR-SIZE is nil, this means to use the value of
  2734. `frame-char-height' or `frame-char-width' of WINDOW's frame.
  2735. This function recursively resizes WINDOW's child windows to fit the
  2736. new size. Make sure that WINDOW is `window--resizable' before
  2737. calling this function. Note that this function does not resize
  2738. siblings of WINDOW or WINDOW's parent window. You have to
  2739. eventually call `window-resize-apply' in order to make resizing
  2740. actually take effect."
  2741. (when add
  2742. ;; Add DELTA to the new total size of WINDOW.
  2743. (set-window-new-pixel window delta t))
  2744. (let ((sub (window-child window)))
  2745. (cond
  2746. ((not sub))
  2747. ((window-combined-p sub horizontal)
  2748. ;; In an iso-combination resize child windows according to their
  2749. ;; normal sizes.
  2750. (window--resize-child-windows
  2751. window delta horizontal nil ignore trail edge char-size))
  2752. ;; In an ortho-combination resize each child window by DELTA.
  2753. (t
  2754. (while sub
  2755. (window--resize-this-window
  2756. sub delta horizontal ignore t trail edge char-size)
  2757. (setq sub (window-right sub)))))))
  2758. (defun window--resize-root-window (window delta horizontal ignore pixelwise)
  2759. "Resize root window WINDOW vertically by DELTA lines.
  2760. HORIZONTAL non-nil means resize root window WINDOW horizontally
  2761. by DELTA columns.
  2762. IGNORE non-nil means ignore any restrictions imposed by fixed
  2763. size windows, `window-min-height' or `window-min-width' settings.
  2764. This function is only called by the frame resizing routines. It
  2765. resizes windows proportionally and never deletes any windows."
  2766. (when (and (windowp window) (numberp delta))
  2767. (let ((pixel-delta
  2768. (if pixelwise
  2769. delta
  2770. (window--size-to-pixel window delta horizontal))))
  2771. (when (window-sizable-p window pixel-delta horizontal ignore t)
  2772. (window--resize-reset (window-frame window) horizontal)
  2773. (window--resize-this-window
  2774. window pixel-delta horizontal ignore t)))))
  2775. (defun window--resize-root-window-vertically (window delta pixelwise)
  2776. "Resize root window WINDOW vertically by DELTA lines.
  2777. If DELTA is less than zero and we can't shrink WINDOW by DELTA
  2778. lines, shrink it as much as possible. If DELTA is greater than
  2779. zero, this function can resize fixed-size windows in order to
  2780. recover the necessary lines. Return the number of lines that
  2781. were recovered.
  2782. Third argument PIXELWISE non-nil means to interpret DELTA as
  2783. pixels and return the number of pixels that were recovered.
  2784. This function is called by the minibuffer window resizing
  2785. routines."
  2786. (let* ((frame (window-frame window))
  2787. (pixel-delta
  2788. (cond
  2789. (pixelwise
  2790. delta)
  2791. ((numberp delta)
  2792. (* (frame-char-height frame) delta))
  2793. (t 0)))
  2794. ignore)
  2795. (cond
  2796. ((zerop pixel-delta))
  2797. ((< pixel-delta 0)
  2798. (setq pixel-delta (window-sizable window pixel-delta nil nil pixelwise))
  2799. (window--resize-reset frame)
  2800. ;; When shrinking the root window, emulate an edge drag in order
  2801. ;; to not resize other windows if we can avoid it (Bug#12419).
  2802. (window--resize-this-window
  2803. window pixel-delta nil ignore t 'before
  2804. (+ (window-pixel-top window) (window-pixel-height window)))
  2805. ;; Don't record new normal sizes to make sure that shrinking back
  2806. ;; proportionally works as intended.
  2807. (walk-window-tree
  2808. (lambda (window) (set-window-new-normal window 'ignore)) frame t))
  2809. ((> pixel-delta 0)
  2810. (window--resize-reset frame)
  2811. (unless (window-sizable window pixel-delta nil nil pixelwise)
  2812. (setq ignore t))
  2813. ;; When growing the root window, resize proportionally. This
  2814. ;; should give windows back their original sizes (hopefully).
  2815. (window--resize-this-window
  2816. window pixel-delta nil ignore t)))
  2817. ;; Return the possibly adjusted DELTA.
  2818. (if pixelwise
  2819. pixel-delta
  2820. (/ pixel-delta (frame-char-height frame)))))
  2821. (defun window--sanitize-window-sizes (frame horizontal)
  2822. "Assert that all windows on FRAME are large enough.
  2823. If necessary and possible, make sure that every window on frame
  2824. FRAME has its minimum height. Optional argument HORIZONTAL
  2825. non-nil means to make sure that every window on frame FRAME has
  2826. its minimum width. The minimum height/width of a window is the
  2827. respective value returned by `window-min-size' for that window.
  2828. Return t if all windows were resized appropriately. Return nil
  2829. if at least one window could not be resized as requested, which
  2830. may happen when the FRAME is not large enough to accommodate it."
  2831. (let ((value t))
  2832. (walk-window-tree
  2833. (lambda (window)
  2834. (let ((delta (- (window-min-size window horizontal nil t)
  2835. (window-size window horizontal t))))
  2836. (when (> delta 0)
  2837. (if (window-resizable-p window delta horizontal nil t)
  2838. (window-resize window delta horizontal nil t)
  2839. (setq value nil))))))
  2840. value))
  2841. (defun adjust-window-trailing-edge (window delta &optional horizontal pixelwise)
  2842. "Move WINDOW's bottom edge by DELTA lines.
  2843. Optional argument HORIZONTAL non-nil means move WINDOW's right
  2844. edge by DELTA columns. WINDOW must be a valid window and
  2845. defaults to the selected one.
  2846. Optional argument PIXELWISE non-nil means interpret DELTA as
  2847. number of pixels.
  2848. If DELTA is greater than zero, move the edge downwards or to the
  2849. right. If DELTA is less than zero, move the edge upwards or to
  2850. the left. If the edge can't be moved by DELTA lines or columns,
  2851. move it as far as possible in the desired direction."
  2852. (setq window (window-normalize-window window))
  2853. (let* ((frame (window-frame window))
  2854. (minibuffer-window (minibuffer-window frame))
  2855. (right window)
  2856. left first-left first-right this-delta min-delta max-delta ignore)
  2857. (unless pixelwise
  2858. (setq pixelwise t)
  2859. (setq delta (* delta (frame-char-size window horizontal))))
  2860. ;; Find the edge we want to move.
  2861. (while (and (or (not (window-combined-p right horizontal))
  2862. (not (window-right right)))
  2863. (setq right (window-parent right))))
  2864. (cond
  2865. ((and (not right) (not horizontal)
  2866. ;; Resize the minibuffer window if it's on the same frame as
  2867. ;; and immediately below WINDOW and it's either active or
  2868. ;; `resize-mini-windows' is nil.
  2869. (eq (window-frame minibuffer-window) frame)
  2870. (= (nth 1 (window-pixel-edges minibuffer-window))
  2871. (nth 3 (window-pixel-edges window)))
  2872. (or (not resize-mini-windows)
  2873. (eq minibuffer-window (active-minibuffer-window))))
  2874. (window--resize-mini-window minibuffer-window (- delta)))
  2875. ((or (not (setq left right)) (not (setq right (window-right right))))
  2876. (if horizontal
  2877. (user-error "No window on the right of this one")
  2878. (user-error "No window below this one")))
  2879. (t
  2880. ;; Set LEFT to the first resizable window on the left. This step is
  2881. ;; needed to handle fixed-size windows.
  2882. (setq first-left left)
  2883. (while (and left
  2884. (or (window-size-fixed-p left horizontal)
  2885. (and (< delta 0)
  2886. (<= (window-size left horizontal t)
  2887. (window-min-size left horizontal nil t)))))
  2888. (setq left
  2889. (or (window-left left)
  2890. (progn
  2891. (while (and (setq left (window-parent left))
  2892. (not (window-combined-p left horizontal))))
  2893. (window-left left)))))
  2894. (unless left
  2895. ;; We have to resize a size-preserved window. Start again with
  2896. ;; the window initially on the left.
  2897. (setq ignore 'preserved)
  2898. (setq left first-left)
  2899. (while (and left
  2900. (or (window-size-fixed-p left horizontal 'preserved)
  2901. (<= (window-size left horizontal t)
  2902. (window-min-size left horizontal 'preserved t))))
  2903. (setq left
  2904. (or (window-left left)
  2905. (progn
  2906. (while (and (setq left (window-parent left))
  2907. (not (window-combined-p left horizontal))))
  2908. (window-left left)))))
  2909. (unless left
  2910. (if horizontal
  2911. (user-error "No resizable window on the left of this one")
  2912. (user-error "No resizable window above this one"))))
  2913. ;; Set RIGHT to the first resizable window on the right. This step
  2914. ;; is needed to handle fixed-size windows.
  2915. (setq first-right right)
  2916. (while (and right
  2917. (or (window-size-fixed-p right horizontal)
  2918. (and (> delta 0)
  2919. (<= (window-size right horizontal t)
  2920. (window-min-size right horizontal 'preserved t)))))
  2921. (setq right
  2922. (or (window-right right)
  2923. (progn
  2924. (while (and (setq right (window-parent right))
  2925. (not (window-combined-p right horizontal))))
  2926. (window-right right)))))
  2927. (unless right
  2928. ;; We have to resize a size-preserved window. Start again with
  2929. ;; the window initially on the right.
  2930. (setq ignore 'preserved)
  2931. (setq right first-right)
  2932. (while (and right
  2933. (or (window-size-fixed-p right horizontal 'preserved))
  2934. (<= (window-size right horizontal t)
  2935. (window-min-size right horizontal 'preserved t)))
  2936. (setq right
  2937. (or (window-right right)
  2938. (progn
  2939. (while (and (setq right (window-parent right))
  2940. (not (window-combined-p right horizontal))))
  2941. (window-right right)))))
  2942. (unless right
  2943. (if horizontal
  2944. (user-error "No resizable window on the right of this one")
  2945. (user-error "No resizable window below this one"))))
  2946. ;; LEFT and RIGHT (which might be both internal windows) are now the
  2947. ;; two windows we want to resize.
  2948. (cond
  2949. ((> delta 0)
  2950. (setq max-delta
  2951. (window--max-delta-1
  2952. left 0 horizontal ignore 'after nil pixelwise))
  2953. (setq min-delta
  2954. (window--min-delta-1
  2955. right (- delta) horizontal ignore 'before nil pixelwise))
  2956. (when (or (< max-delta delta) (> min-delta (- delta)))
  2957. ;; We can't get the whole DELTA - move as far as possible.
  2958. (setq delta (min max-delta (- min-delta))))
  2959. (unless (zerop delta)
  2960. ;; Start resizing.
  2961. (window--resize-reset frame horizontal)
  2962. ;; Try to enlarge LEFT first.
  2963. (setq this-delta (window--resizable
  2964. left delta horizontal ignore 'after nil nil pixelwise))
  2965. (unless (zerop this-delta)
  2966. (window--resize-this-window
  2967. left this-delta horizontal ignore t 'before
  2968. (if horizontal
  2969. (+ (window-pixel-left left) (window-pixel-width left))
  2970. (+ (window-pixel-top left) (window-pixel-height left)))))
  2971. ;; Shrink windows on right of LEFT.
  2972. (window--resize-siblings
  2973. left delta horizontal ignore 'after
  2974. (if horizontal
  2975. (window-pixel-left right)
  2976. (window-pixel-top right)))))
  2977. ((< delta 0)
  2978. (setq max-delta
  2979. (window--max-delta-1
  2980. right 0 horizontal ignore 'before nil pixelwise))
  2981. (setq min-delta
  2982. (window--min-delta-1
  2983. left delta horizontal ignore 'after nil pixelwise))
  2984. (when (or (< max-delta (- delta)) (> min-delta delta))
  2985. ;; We can't get the whole DELTA - move as far as possible.
  2986. (setq delta (max (- max-delta) min-delta)))
  2987. (unless (zerop delta)
  2988. ;; Start resizing.
  2989. (window--resize-reset frame horizontal)
  2990. ;; Try to enlarge RIGHT.
  2991. (setq this-delta
  2992. (window--resizable
  2993. right (- delta) horizontal ignore 'before nil nil pixelwise))
  2994. (unless (zerop this-delta)
  2995. (window--resize-this-window
  2996. right this-delta horizontal ignore t 'after
  2997. (if horizontal
  2998. (window-pixel-left right)
  2999. (window-pixel-top right))))
  3000. ;; Shrink windows on left of RIGHT.
  3001. (window--resize-siblings
  3002. right (- delta) horizontal ignore 'before
  3003. (if horizontal
  3004. (+ (window-pixel-left left) (window-pixel-width left))
  3005. (+ (window-pixel-top left) (window-pixel-height left)))))))
  3006. (unless (zerop delta)
  3007. ;; Don't report an error in the standard case.
  3008. (when (window--resize-apply-p frame horizontal)
  3009. (if (window-resize-apply frame horizontal)
  3010. (progn
  3011. (window--pixel-to-total frame horizontal)
  3012. (run-window-configuration-change-hook frame))
  3013. ;; But do report an error if applying the changes fails.
  3014. (error "Failed adjusting window %s" window))))))))
  3015. (defun enlarge-window (delta &optional horizontal)
  3016. "Make the selected window DELTA lines taller.
  3017. Interactively, if no argument is given, make the selected window
  3018. one line taller. If optional argument HORIZONTAL is non-nil,
  3019. make selected window wider by DELTA columns. If DELTA is
  3020. negative, shrink selected window by -DELTA lines or columns."
  3021. (interactive "p")
  3022. (let ((minibuffer-window (minibuffer-window)))
  3023. (when (window-preserved-size nil horizontal)
  3024. (window-preserve-size nil horizontal))
  3025. (cond
  3026. ((zerop delta))
  3027. ((window-size-fixed-p nil horizontal)
  3028. (error "Selected window has fixed size"))
  3029. ((window-minibuffer-p)
  3030. (if horizontal
  3031. (error "Cannot resize minibuffer window horizontally")
  3032. (window--resize-mini-window (selected-window) delta)))
  3033. ((and (not horizontal)
  3034. (window-full-height-p)
  3035. (eq (window-frame minibuffer-window) (selected-frame))
  3036. (not resize-mini-windows))
  3037. ;; If the selected window is full height and `resize-mini-windows'
  3038. ;; is nil, resize the minibuffer window.
  3039. (window--resize-mini-window minibuffer-window (- delta)))
  3040. ((window--resizable-p nil delta horizontal)
  3041. (window-resize nil delta horizontal))
  3042. (t
  3043. (window-resize
  3044. nil (if (> delta 0)
  3045. (window-max-delta nil horizontal)
  3046. (- (window-min-delta nil horizontal)))
  3047. horizontal)))))
  3048. (defun shrink-window (delta &optional horizontal)
  3049. "Make the selected window DELTA lines smaller.
  3050. Interactively, if no argument is given, make the selected window
  3051. one line smaller. If optional argument HORIZONTAL is non-nil,
  3052. make selected window narrower by DELTA columns. If DELTA is
  3053. negative, enlarge selected window by -DELTA lines or columns.
  3054. Also see the `window-min-height' variable."
  3055. (interactive "p")
  3056. (let ((minibuffer-window (minibuffer-window)))
  3057. (when (window-preserved-size nil horizontal)
  3058. (window-preserve-size nil horizontal))
  3059. (cond
  3060. ((zerop delta))
  3061. ((window-size-fixed-p nil horizontal)
  3062. (error "Selected window has fixed size"))
  3063. ((window-minibuffer-p)
  3064. (if horizontal
  3065. (error "Cannot resize minibuffer window horizontally")
  3066. (window--resize-mini-window (selected-window) (- delta))))
  3067. ((and (not horizontal)
  3068. (window-full-height-p)
  3069. (eq (window-frame minibuffer-window) (selected-frame))
  3070. (not resize-mini-windows))
  3071. ;; If the selected window is full height and `resize-mini-windows'
  3072. ;; is nil, resize the minibuffer window.
  3073. (window--resize-mini-window minibuffer-window delta))
  3074. ((window--resizable-p nil (- delta) horizontal)
  3075. (window-resize nil (- delta) horizontal))
  3076. (t
  3077. (window-resize
  3078. nil (if (> delta 0)
  3079. (- (window-min-delta nil horizontal))
  3080. (window-max-delta nil horizontal))
  3081. horizontal)))))
  3082. (defun maximize-window (&optional window)
  3083. "Maximize WINDOW.
  3084. Make WINDOW as large as possible without deleting any windows.
  3085. WINDOW must be a valid window and defaults to the selected one.
  3086. If the option `window-resize-pixelwise' is non-nil maximize
  3087. WINDOW pixelwise."
  3088. (interactive)
  3089. (setq window (window-normalize-window window))
  3090. (window-resize
  3091. window (window-max-delta window nil nil nil nil nil window-resize-pixelwise)
  3092. nil nil window-resize-pixelwise)
  3093. (window-resize
  3094. window (window-max-delta window t nil nil nil nil window-resize-pixelwise)
  3095. t nil window-resize-pixelwise))
  3096. (defun minimize-window (&optional window)
  3097. "Minimize WINDOW.
  3098. Make WINDOW as small as possible without deleting any windows.
  3099. WINDOW must be a valid window and defaults to the selected one.
  3100. If the option `window-resize-pixelwise' is non-nil minimize
  3101. WINDOW pixelwise."
  3102. (interactive)
  3103. (setq window (window-normalize-window window))
  3104. (window-resize
  3105. window
  3106. (- (window-min-delta window nil nil nil nil nil window-resize-pixelwise))
  3107. nil nil window-resize-pixelwise)
  3108. (window-resize
  3109. window
  3110. (- (window-min-delta window t nil nil nil nil window-resize-pixelwise))
  3111. t nil window-resize-pixelwise))
  3112. ;;; Window edges
  3113. (defun window-edges (&optional window body absolute pixelwise)
  3114. "Return a list of the edge distances of WINDOW.
  3115. WINDOW must be a valid window and defaults to the selected one.
  3116. The list returned has the form (LEFT TOP RIGHT BOTTOM).
  3117. If the optional argument BODY is nil, this means to return the
  3118. edges corresponding to the total size of WINDOW. BODY non-nil
  3119. means to return the edges of WINDOW's body (aka text area). If
  3120. BODY is non-nil, WINDOW must specify a live window.
  3121. Optional argument ABSOLUTE nil means to return edges relative to
  3122. the position of WINDOW's native frame. ABSOLUTE non-nil means to
  3123. return coordinates relative to the origin - the position (0, 0) -
  3124. of FRAME's display. On non-graphical systems this argument has
  3125. no effect.
  3126. Optional argument PIXELWISE nil means to return the coordinates
  3127. in terms of the canonical character width and height of WINDOW's
  3128. frame, rounded if necessary. PIXELWISE non-nil means to return
  3129. the coordinates in pixels where the values for RIGHT and BOTTOM
  3130. are one more than the actual value of these edges. Note that if
  3131. ABSOLUTE is non-nil, PIXELWISE is implicitly non-nil too."
  3132. (let* ((window (window-normalize-window window body))
  3133. (frame (window-frame window))
  3134. (border-width (frame-border-width frame))
  3135. (char-width (frame-char-width frame))
  3136. (char-height (frame-char-height frame))
  3137. (left (if pixelwise
  3138. (+ (window-pixel-left window) border-width)
  3139. (+ (window-left-column window)
  3140. (/ border-width char-width))))
  3141. (left-body
  3142. (when body
  3143. (+ (window-pixel-left window) border-width
  3144. (if (eq (car (window-current-scroll-bars window)) 'left)
  3145. (window-scroll-bar-width window)
  3146. 0)
  3147. (nth 0 (window-fringes window))
  3148. (* (or (nth 0 (window-margins window)) 0) char-width))))
  3149. (top (if pixelwise
  3150. (+ (window-pixel-top window) border-width)
  3151. (+ (window-top-line window)
  3152. (/ border-width char-height))))
  3153. (top-body
  3154. (when body
  3155. (+ (window-pixel-top window) border-width
  3156. (window-header-line-height window))))
  3157. (right (+ left (if pixelwise
  3158. (window-pixel-width window)
  3159. (window-total-width window))))
  3160. (right-body (and body (+ left-body (window-body-width window t))))
  3161. (bottom (+ top (if pixelwise
  3162. (window-pixel-height window)
  3163. (window-total-height window))))
  3164. (bottom-body (and body (+ top-body (window-body-height window t))))
  3165. left-off right-off)
  3166. (if absolute
  3167. (let* ((native-edges (frame-edges frame 'native-edges))
  3168. (left-off (nth 0 native-edges))
  3169. (top-off (nth 1 native-edges)))
  3170. (if body
  3171. (list (+ left-body left-off) (+ top-body top-off)
  3172. (+ right-body left-off) (+ bottom-body top-off))
  3173. (list (+ left left-off) (+ top top-off)
  3174. (+ right left-off) (+ bottom top-off))))
  3175. (if body
  3176. (if pixelwise
  3177. (list left-body top-body right-body bottom-body)
  3178. (list (/ left-body char-width) (/ top-body char-height)
  3179. ;; Round up.
  3180. (/ (+ right-body char-width -1) char-width)
  3181. (/ (+ bottom-body char-height -1) char-height)))
  3182. (list left top right bottom)))))
  3183. (defun window-body-edges (&optional window)
  3184. "Return a list of the edge coordinates of WINDOW's body.
  3185. The return value is that of `window-edges' called with argument
  3186. BODY non-nil."
  3187. (window-edges window t))
  3188. (defalias 'window-inside-edges 'window-body-edges)
  3189. (defun window-pixel-edges (&optional window)
  3190. "Return a list of the edge pixel coordinates of WINDOW.
  3191. The return value is that of `window-edges' called with argument
  3192. PIXELWISE non-nil."
  3193. (window-edges window nil nil t))
  3194. (defun window-body-pixel-edges (&optional window)
  3195. "Return a list of the edge pixel coordinates of WINDOW's body.
  3196. The return value is that of `window-edges' called with arguments
  3197. BODY and PIXELWISE non-nil."
  3198. (window-edges window t nil t))
  3199. (defalias 'window-inside-pixel-edges 'window-body-pixel-edges)
  3200. (defun window-absolute-pixel-edges (&optional window)
  3201. "Return a list of the edge pixel coordinates of WINDOW.
  3202. The return value is that of `window-edges' called with argument
  3203. ABSOLUTE non-nil."
  3204. (window-edges window nil t t))
  3205. (defun window-absolute-body-pixel-edges (&optional window)
  3206. "Return a list of the edge pixel coordinates of WINDOW's text area.
  3207. The return value is that of `window-edges' called with arguments
  3208. BODY and ABSOLUTE non-nil."
  3209. (window-edges window t t t))
  3210. (defalias 'window-inside-absolute-pixel-edges 'window-absolute-body-pixel-edges)
  3211. (defun window-absolute-pixel-position (&optional position window)
  3212. "Return display coordinates of POSITION in WINDOW.
  3213. If the buffer position POSITION is visible in window WINDOW,
  3214. return the display coordinates of the upper/left corner of the
  3215. glyph at POSITION. The return value is a cons of the X- and
  3216. Y-coordinates of that corner, relative to an origin at (0, 0) of
  3217. WINDOW's display. Return nil if POSITION is not visible in
  3218. WINDOW.
  3219. WINDOW must be a live window and defaults to the selected window.
  3220. POSITION defaults to the value of `window-point' of WINDOW."
  3221. (let* ((window (window-normalize-window window t))
  3222. (pos-in-window
  3223. (pos-visible-in-window-p
  3224. (or position (window-point window)) window t)))
  3225. (when pos-in-window
  3226. (let ((edges (window-absolute-body-pixel-edges window)))
  3227. (cons (+ (nth 0 edges) (nth 0 pos-in-window))
  3228. (+ (nth 1 edges) (nth 1 pos-in-window)))))))
  3229. (defun frame-root-window-p (window)
  3230. "Return non-nil if WINDOW is the root window of its frame."
  3231. (eq window (frame-root-window window)))
  3232. (defun window--subtree (window &optional next)
  3233. "Return window subtree rooted at WINDOW.
  3234. Optional argument NEXT non-nil means include WINDOW's right
  3235. siblings in the return value.
  3236. See the documentation of `window-tree' for a description of the
  3237. return value."
  3238. (let (list)
  3239. (while window
  3240. (setq list
  3241. (cons
  3242. (cond
  3243. ((window-top-child window)
  3244. (cons t (cons (window-edges window)
  3245. (window--subtree (window-top-child window) t))))
  3246. ((window-left-child window)
  3247. (cons nil (cons (window-edges window)
  3248. (window--subtree (window-left-child window) t))))
  3249. (t window))
  3250. list))
  3251. (setq window (when next (window-next-sibling window))))
  3252. (nreverse list)))
  3253. (defun window-tree (&optional frame)
  3254. "Return the window tree of frame FRAME.
  3255. FRAME must be a live frame and defaults to the selected frame.
  3256. The return value is a list of the form (ROOT MINI), where ROOT
  3257. represents the window tree of the frame's root window, and MINI
  3258. is the frame's minibuffer window.
  3259. If the root window is not split, ROOT is the root window itself.
  3260. Otherwise, ROOT is a list (DIR EDGES W1 W2 ...) where DIR is nil
  3261. for a horizontal split, and t for a vertical split. EDGES gives
  3262. the combined size and position of the child windows in the split,
  3263. and the rest of the elements are the child windows in the split.
  3264. Each of the child windows may again be a window or a list
  3265. representing a window split, and so on. EDGES is a list (LEFT
  3266. TOP RIGHT BOTTOM) as returned by `window-edges'."
  3267. (setq frame (window-normalize-frame frame))
  3268. (window--subtree (frame-root-window frame) t))
  3269. (defun other-window (count &optional all-frames)
  3270. "Select another window in cyclic ordering of windows.
  3271. COUNT specifies the number of windows to skip, starting with the
  3272. selected window, before making the selection. If COUNT is
  3273. positive, skip COUNT windows forwards. If COUNT is negative,
  3274. skip -COUNT windows backwards. COUNT zero means do not skip any
  3275. window, so select the selected window. In an interactive call,
  3276. COUNT is the numeric prefix argument. Return nil.
  3277. If the `other-window' parameter of the selected window is a
  3278. function and `ignore-window-parameters' is nil, call that
  3279. function with the arguments COUNT and ALL-FRAMES.
  3280. This function does not select a window whose `no-other-window'
  3281. window parameter is non-nil.
  3282. This function uses `next-window' for finding the window to
  3283. select. The argument ALL-FRAMES has the same meaning as in
  3284. `next-window', but the MINIBUF argument of `next-window' is
  3285. always effectively nil."
  3286. (interactive "p")
  3287. (let* ((window (selected-window))
  3288. (function (and (not ignore-window-parameters)
  3289. (window-parameter window 'other-window)))
  3290. old-window old-count)
  3291. (if (functionp function)
  3292. (funcall function count all-frames)
  3293. ;; `next-window' and `previous-window' may return a window we are
  3294. ;; not allowed to select. Hence we need an exit strategy in case
  3295. ;; all windows are non-selectable.
  3296. (catch 'exit
  3297. (while (> count 0)
  3298. (setq window (next-window window nil all-frames))
  3299. (cond
  3300. ((eq window old-window)
  3301. (when (= count old-count)
  3302. ;; Keep out of infinite loops. When COUNT has not changed
  3303. ;; since we last looked at `window' we're probably in one.
  3304. (throw 'exit nil)))
  3305. ((window-parameter window 'no-other-window)
  3306. (unless old-window
  3307. ;; The first non-selectable window `next-window' got us:
  3308. ;; Remember it and the current value of COUNT.
  3309. (setq old-window window)
  3310. (setq old-count count)))
  3311. (t
  3312. (setq count (1- count)))))
  3313. (while (< count 0)
  3314. (setq window (previous-window window nil all-frames))
  3315. (cond
  3316. ((eq window old-window)
  3317. (when (= count old-count)
  3318. ;; Keep out of infinite loops. When COUNT has not changed
  3319. ;; since we last looked at `window' we're probably in one.
  3320. (throw 'exit nil)))
  3321. ((window-parameter window 'no-other-window)
  3322. (unless old-window
  3323. ;; The first non-selectable window `previous-window' got
  3324. ;; us: Remember it and the current value of COUNT.
  3325. (setq old-window window)
  3326. (setq old-count count)))
  3327. (t
  3328. (setq count (1+ count)))))
  3329. (select-window window)
  3330. ;; Always return nil.
  3331. nil))))
  3332. ;; This should probably return non-nil when the selected window is part
  3333. ;; of an atomic window whose root is the frame's root window.
  3334. (defun one-window-p (&optional nomini all-frames)
  3335. "Return non-nil if the selected window is the only window.
  3336. Optional arg NOMINI non-nil means don't count the minibuffer
  3337. even if it is active. Otherwise, the minibuffer is counted
  3338. when it is active.
  3339. Optional argument ALL-FRAMES specifies the set of frames to
  3340. consider, see also `next-window'. ALL-FRAMES nil or omitted
  3341. means consider windows on the selected frame only, plus the
  3342. minibuffer window if specified by the NOMINI argument. If the
  3343. minibuffer counts, consider all windows on all frames that share
  3344. that minibuffer too. The remaining non-nil values of ALL-FRAMES
  3345. with a special meaning are:
  3346. - t means consider all windows on all existing frames.
  3347. - `visible' means consider all windows on all visible frames on
  3348. the current terminal.
  3349. - 0 (the number zero) means consider all windows on all visible
  3350. and iconified frames on the current terminal.
  3351. - A frame means consider all windows on that frame only.
  3352. Anything else means consider all windows on the selected frame
  3353. and no others."
  3354. (let ((base-window (selected-window)))
  3355. (if (and nomini (eq base-window (minibuffer-window)))
  3356. (setq base-window (next-window base-window)))
  3357. (eq base-window
  3358. (next-window base-window (if nomini 'arg) all-frames))))
  3359. ;;; Deleting windows.
  3360. (defun window-deletable-p (&optional window)
  3361. "Return t if WINDOW can be safely deleted from its frame.
  3362. WINDOW must be a valid window and defaults to the selected one.
  3363. Return 'frame if deleting WINDOW should also delete its frame."
  3364. (setq window (window-normalize-window window))
  3365. (unless (or ignore-window-parameters
  3366. (eq (window-parameter window 'delete-window) t))
  3367. ;; Handle atomicity.
  3368. (when (window-parameter window 'window-atom)
  3369. (setq window (window-atom-root window))))
  3370. (let ((frame (window-frame window)))
  3371. (cond
  3372. ((frame-root-window-p window)
  3373. ;; WINDOW's frame can be deleted only if there are other frames
  3374. ;; on the same terminal, and it does not contain the active
  3375. ;; minibuffer.
  3376. (unless (or (eq frame (next-frame frame 0))
  3377. ;; We can delete our frame only if no other frame
  3378. ;; currently uses our minibuffer window.
  3379. (catch 'other
  3380. (dolist (other (frame-list))
  3381. (when (and (not (eq other frame))
  3382. (eq (window-frame (minibuffer-window other))
  3383. frame))
  3384. (throw 'other t))))
  3385. (let ((minibuf (active-minibuffer-window)))
  3386. (and minibuf (eq frame (window-frame minibuf)))))
  3387. 'frame))
  3388. ((or ignore-window-parameters
  3389. (not (eq window (window--major-non-side-window frame))))
  3390. ;; WINDOW can be deleted unless it is the major non-side window of
  3391. ;; its frame.
  3392. t))))
  3393. (defun window--in-subtree-p (window root)
  3394. "Return t if WINDOW is either ROOT or a member of ROOT's subtree."
  3395. (or (eq window root)
  3396. (let ((parent (window-parent window)))
  3397. (catch 'done
  3398. (while parent
  3399. (if (eq parent root)
  3400. (throw 'done t)
  3401. (setq parent (window-parent parent))))))))
  3402. (defun delete-window (&optional window)
  3403. "Delete WINDOW.
  3404. WINDOW must be a valid window and defaults to the selected one.
  3405. Return nil.
  3406. If the variable `ignore-window-parameters' is non-nil or the
  3407. `delete-window' parameter of WINDOW equals t, do not process any
  3408. parameters of WINDOW. Otherwise, if the `delete-window'
  3409. parameter of WINDOW specifies a function, call that function with
  3410. WINDOW as its sole argument and return the value returned by that
  3411. function.
  3412. Otherwise, if WINDOW is part of an atomic window, call
  3413. `delete-window' with the root of the atomic window as its
  3414. argument. Signal an error if WINDOW is either the only window on
  3415. its frame, the last non-side window, or part of an atomic window
  3416. that is its frame's root window."
  3417. (interactive)
  3418. (setq window (window-normalize-window window))
  3419. (let* ((frame (window-frame window))
  3420. (function (window-parameter window 'delete-window))
  3421. (parent (window-parent window))
  3422. atom-root)
  3423. (window--check frame)
  3424. (catch 'done
  3425. ;; Handle window parameters.
  3426. (cond
  3427. ;; Ignore window parameters if `ignore-window-parameters' tells
  3428. ;; us so or `delete-window' equals t.
  3429. ((or ignore-window-parameters (eq function t)))
  3430. ((functionp function)
  3431. ;; The `delete-window' parameter specifies the function to call.
  3432. ;; If that function is `ignore' nothing is done. It's up to the
  3433. ;; function called here to avoid infinite recursion.
  3434. (throw 'done (funcall function window)))
  3435. ((and (window-parameter window 'window-atom)
  3436. (setq atom-root (window-atom-root window))
  3437. (not (eq atom-root window)))
  3438. (if (eq atom-root (frame-root-window frame))
  3439. (error "Root of atomic window is root window of its frame")
  3440. (throw 'done (delete-window atom-root))))
  3441. ((not parent)
  3442. (error "Attempt to delete minibuffer or sole ordinary window"))
  3443. ((eq window (window--major-non-side-window frame))
  3444. (error "Attempt to delete last non-side window")))
  3445. (let* ((horizontal (window-left-child parent))
  3446. (size (window-size window horizontal t))
  3447. (frame-selected
  3448. (window--in-subtree-p (frame-selected-window frame) window))
  3449. ;; Emacs 23 preferably gives WINDOW's space to its left
  3450. ;; sibling.
  3451. (sibling (or (window-left window) (window-right window))))
  3452. (window--resize-reset frame horizontal)
  3453. (cond
  3454. ((and (not window-combination-resize)
  3455. sibling (window-sizable-p sibling size horizontal nil t))
  3456. ;; Resize WINDOW's sibling.
  3457. (window--resize-this-window sibling size horizontal nil t)
  3458. (set-window-new-normal
  3459. sibling (+ (window-normal-size sibling horizontal)
  3460. (window-normal-size window horizontal))))
  3461. ((window--resizable-p window (- size) horizontal nil nil nil t t)
  3462. ;; Can do without resizing fixed-size windows.
  3463. (window--resize-siblings window (- size) horizontal))
  3464. (t
  3465. ;; Can't do without resizing fixed-size windows.
  3466. (window--resize-siblings window (- size) horizontal t)))
  3467. ;; Actually delete WINDOW.
  3468. (delete-window-internal window)
  3469. (window--pixel-to-total frame horizontal)
  3470. (when (and frame-selected
  3471. (window-parameter
  3472. (frame-selected-window frame) 'no-other-window))
  3473. ;; `delete-window-internal' has selected a window that should
  3474. ;; not be selected, fix this here.
  3475. (other-window -1 frame))
  3476. (run-window-configuration-change-hook frame)
  3477. (window--check frame)
  3478. ;; Always return nil.
  3479. nil))))
  3480. (defun delete-other-windows (&optional window)
  3481. "Make WINDOW fill its frame.
  3482. WINDOW must be a valid window and defaults to the selected one.
  3483. Return nil.
  3484. If the variable `ignore-window-parameters' is non-nil or the
  3485. `delete-other-windows' parameter of WINDOW equals t, do not
  3486. process any parameters of WINDOW. Otherwise, if the
  3487. `delete-other-windows' parameter of WINDOW specifies a function,
  3488. call that function with WINDOW as its sole argument and return
  3489. the value returned by that function.
  3490. Otherwise, if WINDOW is part of an atomic window, call this
  3491. function with the root of the atomic window as its argument. If
  3492. WINDOW is a non-side window, make WINDOW the only non-side window
  3493. on the frame. Side windows are not deleted. If WINDOW is a side
  3494. window signal an error."
  3495. (interactive)
  3496. (setq window (window-normalize-window window))
  3497. (let* ((frame (window-frame window))
  3498. (function (window-parameter window 'delete-other-windows))
  3499. (window-side (window-parameter window 'window-side))
  3500. atom-root side-main)
  3501. (window--check frame)
  3502. (catch 'done
  3503. (cond
  3504. ;; Ignore window parameters if `ignore-window-parameters' is t or
  3505. ;; `delete-other-windows' is t.
  3506. ((or ignore-window-parameters (eq function t)))
  3507. ((functionp function)
  3508. ;; The `delete-other-windows' parameter specifies the function
  3509. ;; to call. If the function is `ignore' no windows are deleted.
  3510. ;; It's up to the function called to avoid infinite recursion.
  3511. (throw 'done (funcall function window)))
  3512. ((and (window-parameter window 'window-atom)
  3513. (setq atom-root (window-atom-root window))
  3514. (not (eq atom-root window)))
  3515. (if (eq atom-root (frame-root-window frame))
  3516. (error "Root of atomic window is root window of its frame")
  3517. (throw 'done (delete-other-windows atom-root))))
  3518. ((memq window-side window-sides)
  3519. (error "Cannot make side window the only window"))
  3520. ((and (window-minibuffer-p window)
  3521. (not (eq window (frame-root-window window))))
  3522. (error "Can't expand minibuffer to full frame")))
  3523. ;; If WINDOW is the major non-side window, do nothing.
  3524. (if (window-with-parameter 'window-side)
  3525. (setq side-main (window--major-non-side-window frame))
  3526. (setq side-main (frame-root-window frame)))
  3527. (unless (eq window side-main)
  3528. (delete-other-windows-internal window side-main)
  3529. (run-window-configuration-change-hook frame)
  3530. (window--check frame))
  3531. ;; Always return nil.
  3532. nil)))
  3533. (defun delete-other-windows-vertically (&optional window)
  3534. "Delete the windows in the same column with WINDOW, but not WINDOW itself.
  3535. This may be a useful alternative binding for \\[delete-other-windows]
  3536. if you often split windows horizontally."
  3537. (interactive)
  3538. (let* ((window (or window (selected-window)))
  3539. (edges (window-edges window))
  3540. (w window) delenda)
  3541. (while (not (eq (setq w (next-window w 1)) window))
  3542. (let ((e (window-edges w)))
  3543. (when (and (= (car e) (car edges))
  3544. (= (nth 2 e) (nth 2 edges)))
  3545. (push w delenda))))
  3546. (mapc 'delete-window delenda)))
  3547. ;;; Windows and buffers.
  3548. ;; `prev-buffers' and `next-buffers' are two reserved window slots used
  3549. ;; for (1) determining which buffer to show in the window when its
  3550. ;; buffer shall be buried or killed and (2) which buffer to show for
  3551. ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
  3552. ;; `prev-buffers' consists of <buffer, window-start, window-point>
  3553. ;; triples. The entries on this list are ordered by the time their
  3554. ;; buffer has been removed from the window, the most recently removed
  3555. ;; buffer's entry being first. The window-start and window-point
  3556. ;; components are `window-start' and `window-point' at the time the
  3557. ;; buffer was removed from the window which implies that the entry must
  3558. ;; be added when `set-window-buffer' removes the buffer from the window.
  3559. ;; `next-buffers' is the list of buffers that have been replaced
  3560. ;; recently by `switch-to-prev-buffer'. These buffers are the least
  3561. ;; preferred candidates of `switch-to-prev-buffer' and the preferred
  3562. ;; candidates of `switch-to-next-buffer' to switch to. This list is
  3563. ;; reset to nil by any action changing the window's buffer with the
  3564. ;; exception of `switch-to-prev-buffer' and `switch-to-next-buffer'.
  3565. ;; `switch-to-prev-buffer' pushes the buffer it just replaced on it,
  3566. ;; `switch-to-next-buffer' pops the last pushed buffer from it.
  3567. ;; Both `prev-buffers' and `next-buffers' may reference killed buffers
  3568. ;; if such a buffer was killed while the window was hidden within a
  3569. ;; window configuration. Such killed buffers get removed whenever
  3570. ;; `switch-to-prev-buffer' or `switch-to-next-buffer' encounter them.
  3571. ;; The following function is called by `set-window-buffer' _before_ it
  3572. ;; replaces the buffer of the argument window with the new buffer.
  3573. (defun record-window-buffer (&optional window)
  3574. "Record WINDOW's buffer.
  3575. WINDOW must be a live window and defaults to the selected one."
  3576. (let* ((window (window-normalize-window window t))
  3577. (buffer (window-buffer window))
  3578. (entry (assq buffer (window-prev-buffers window))))
  3579. ;; Reset WINDOW's next buffers. If needed, they are resurrected by
  3580. ;; `switch-to-prev-buffer' and `switch-to-next-buffer'.
  3581. (set-window-next-buffers window nil)
  3582. (when entry
  3583. ;; Remove all entries for BUFFER from WINDOW's previous buffers.
  3584. (set-window-prev-buffers
  3585. window (assq-delete-all buffer (window-prev-buffers window))))
  3586. ;; Don't record insignificant buffers.
  3587. (unless (eq (aref (buffer-name buffer) 0) ?\s)
  3588. ;; Add an entry for buffer to WINDOW's previous buffers.
  3589. (with-current-buffer buffer
  3590. (let ((start (window-start window))
  3591. (point (window-point window)))
  3592. (setq entry
  3593. (cons buffer
  3594. (if entry
  3595. ;; We have an entry, update marker positions.
  3596. (list (set-marker (nth 1 entry) start)
  3597. (set-marker (nth 2 entry) point))
  3598. ;; Make new markers.
  3599. (list (copy-marker start)
  3600. (copy-marker
  3601. ;; Preserve window-point-insertion-type
  3602. ;; (Bug#12588).
  3603. point window-point-insertion-type)))))
  3604. (set-window-prev-buffers
  3605. window (cons entry (window-prev-buffers window)))))
  3606. (run-hooks 'buffer-list-update-hook))))
  3607. (defun unrecord-window-buffer (&optional window buffer)
  3608. "Unrecord BUFFER in WINDOW.
  3609. WINDOW must be a live window and defaults to the selected one.
  3610. BUFFER must be a live buffer and defaults to the buffer of
  3611. WINDOW."
  3612. (let* ((window (window-normalize-window window t))
  3613. (buffer (or buffer (window-buffer window))))
  3614. (set-window-prev-buffers
  3615. window (assq-delete-all buffer (window-prev-buffers window)))
  3616. (set-window-next-buffers
  3617. window (delq buffer (window-next-buffers window)))))
  3618. (defun set-window-buffer-start-and-point (window buffer &optional start point)
  3619. "Set WINDOW's buffer to BUFFER.
  3620. WINDOW must be a live window and defaults to the selected one.
  3621. Optional argument START non-nil means set WINDOW's start position
  3622. to START. Optional argument POINT non-nil means set WINDOW's
  3623. point to POINT. If WINDOW is selected this also sets BUFFER's
  3624. `point' to POINT. If WINDOW is selected and the buffer it showed
  3625. before was current this also makes BUFFER the current buffer."
  3626. (setq window (window-normalize-window window t))
  3627. (let ((selected (eq window (selected-window)))
  3628. (current (eq (window-buffer window) (current-buffer))))
  3629. (set-window-buffer window buffer)
  3630. (when (and selected current)
  3631. (set-buffer buffer))
  3632. (when start
  3633. ;; Don't force window-start here (even if POINT is nil).
  3634. (set-window-start window start t))
  3635. (when point
  3636. (set-window-point window point))))
  3637. (defcustom switch-to-visible-buffer t
  3638. "If non-nil, allow switching to an already visible buffer.
  3639. If this variable is non-nil, `switch-to-prev-buffer' and
  3640. `switch-to-next-buffer' may switch to an already visible buffer.
  3641. If this variable is nil, `switch-to-prev-buffer' and
  3642. `switch-to-next-buffer' always try to avoid switching to a buffer
  3643. that is already visible in another window on the same frame."
  3644. :type 'boolean
  3645. :version "24.1"
  3646. :group 'windows)
  3647. (defun switch-to-prev-buffer (&optional window bury-or-kill)
  3648. "In WINDOW switch to previous buffer.
  3649. WINDOW must be a live window and defaults to the selected one.
  3650. Return the buffer switched to, nil if no suitable buffer could be
  3651. found.
  3652. Optional argument BURY-OR-KILL non-nil means the buffer currently
  3653. shown in WINDOW is about to be buried or killed and consequently
  3654. shall not be switched to in future invocations of this command.
  3655. As a special case, if BURY-OR-KILL equals `append', this means to
  3656. move the buffer to the end of WINDOW's previous buffers list so a
  3657. future invocation of `switch-to-prev-buffer' less likely switches
  3658. to it."
  3659. (interactive)
  3660. (let* ((window (window-normalize-window window t))
  3661. (frame (window-frame window))
  3662. (old-buffer (window-buffer window))
  3663. ;; Save this since it's destroyed by `set-window-buffer'.
  3664. (next-buffers (window-next-buffers window))
  3665. (pred (frame-parameter frame 'buffer-predicate))
  3666. entry new-buffer killed-buffers visible)
  3667. (when (window-minibuffer-p window)
  3668. ;; Don't switch in minibuffer window.
  3669. (unless (setq window (minibuffer-selected-window))
  3670. (error "Window %s is a minibuffer window" window)))
  3671. (when (window-dedicated-p window)
  3672. ;; Don't switch in dedicated window.
  3673. (error "Window %s is dedicated to buffer %s" window old-buffer))
  3674. (catch 'found
  3675. ;; Scan WINDOW's previous buffers first, skipping entries of next
  3676. ;; buffers.
  3677. (dolist (entry (window-prev-buffers window))
  3678. (when (and (setq new-buffer (car entry))
  3679. (or (buffer-live-p new-buffer)
  3680. (not (setq killed-buffers
  3681. (cons new-buffer killed-buffers))))
  3682. (not (eq new-buffer old-buffer))
  3683. (or (null pred) (funcall pred new-buffer))
  3684. ;; When BURY-OR-KILL is nil, avoid switching to a
  3685. ;; buffer in WINDOW's next buffers list.
  3686. (or bury-or-kill (not (memq new-buffer next-buffers))))
  3687. (if (and (not switch-to-visible-buffer)
  3688. (get-buffer-window new-buffer frame))
  3689. ;; Try to avoid showing a buffer visible in some other
  3690. ;; window.
  3691. (setq visible new-buffer)
  3692. (set-window-buffer-start-and-point
  3693. window new-buffer (nth 1 entry) (nth 2 entry))
  3694. (throw 'found t))))
  3695. ;; Scan reverted buffer list of WINDOW's frame next, skipping
  3696. ;; entries of next buffers. Note that when we bury or kill a
  3697. ;; buffer we don't reverse the global buffer list to avoid showing
  3698. ;; a buried buffer instead. Otherwise, we must reverse the global
  3699. ;; buffer list in order to make sure that switching to the
  3700. ;; previous/next buffer traverse it in opposite directions.
  3701. (dolist (buffer (if bury-or-kill
  3702. (buffer-list frame)
  3703. (nreverse (buffer-list frame))))
  3704. (when (and (buffer-live-p buffer)
  3705. (not (eq buffer old-buffer))
  3706. (or (null pred) (funcall pred buffer))
  3707. (not (eq (aref (buffer-name buffer) 0) ?\s))
  3708. (or bury-or-kill (not (memq buffer next-buffers))))
  3709. (if (and (not switch-to-visible-buffer)
  3710. (get-buffer-window buffer frame))
  3711. ;; Try to avoid showing a buffer visible in some other window.
  3712. (unless visible
  3713. (setq visible buffer))
  3714. (setq new-buffer buffer)
  3715. (set-window-buffer-start-and-point window new-buffer)
  3716. (throw 'found t))))
  3717. (unless bury-or-kill
  3718. ;; Scan reverted next buffers last (must not use nreverse
  3719. ;; here!).
  3720. (dolist (buffer (reverse next-buffers))
  3721. ;; Actually, buffer _must_ be live here since otherwise it
  3722. ;; would have been caught in the scan of previous buffers.
  3723. (when (and (or (buffer-live-p buffer)
  3724. (not (setq killed-buffers
  3725. (cons buffer killed-buffers))))
  3726. (not (eq buffer old-buffer))
  3727. (or (null pred) (funcall pred buffer))
  3728. (setq entry (assq buffer (window-prev-buffers window))))
  3729. (setq new-buffer buffer)
  3730. (set-window-buffer-start-and-point
  3731. window new-buffer (nth 1 entry) (nth 2 entry))
  3732. (throw 'found t))))
  3733. ;; Show a buffer visible in another window.
  3734. (when visible
  3735. (setq new-buffer visible)
  3736. (set-window-buffer-start-and-point window new-buffer)))
  3737. (if bury-or-kill
  3738. (let ((entry (and (eq bury-or-kill 'append)
  3739. (assq old-buffer (window-prev-buffers window)))))
  3740. ;; Remove `old-buffer' from WINDOW's previous and (restored list
  3741. ;; of) next buffers.
  3742. (set-window-prev-buffers
  3743. window (assq-delete-all old-buffer (window-prev-buffers window)))
  3744. (set-window-next-buffers window (delq old-buffer next-buffers))
  3745. (when entry
  3746. ;; Append old-buffer's entry to list of WINDOW's previous
  3747. ;; buffers so it's less likely to get switched to soon but
  3748. ;; `display-buffer-in-previous-window' can nevertheless find
  3749. ;; it.
  3750. (set-window-prev-buffers
  3751. window (append (window-prev-buffers window) (list entry)))))
  3752. ;; Move `old-buffer' to head of WINDOW's restored list of next
  3753. ;; buffers.
  3754. (set-window-next-buffers
  3755. window (cons old-buffer (delq old-buffer next-buffers))))
  3756. ;; Remove killed buffers from WINDOW's previous and next buffers.
  3757. (when killed-buffers
  3758. (dolist (buffer killed-buffers)
  3759. (set-window-prev-buffers
  3760. window (assq-delete-all buffer (window-prev-buffers window)))
  3761. (set-window-next-buffers
  3762. window (delq buffer (window-next-buffers window)))))
  3763. ;; Return new-buffer.
  3764. new-buffer))
  3765. (defun switch-to-next-buffer (&optional window)
  3766. "In WINDOW switch to next buffer.
  3767. WINDOW must be a live window and defaults to the selected one.
  3768. Return the buffer switched to, nil if no suitable buffer could be
  3769. found."
  3770. (interactive)
  3771. (let* ((window (window-normalize-window window t))
  3772. (frame (window-frame window))
  3773. (old-buffer (window-buffer window))
  3774. (next-buffers (window-next-buffers window))
  3775. (pred (frame-parameter frame 'buffer-predicate))
  3776. new-buffer entry killed-buffers visible)
  3777. (when (window-minibuffer-p window)
  3778. ;; Don't switch in minibuffer window.
  3779. (unless (setq window (minibuffer-selected-window))
  3780. (error "Window %s is a minibuffer window" window)))
  3781. (when (window-dedicated-p window)
  3782. ;; Don't switch in dedicated window.
  3783. (error "Window %s is dedicated to buffer %s" window old-buffer))
  3784. (catch 'found
  3785. ;; Scan WINDOW's next buffers first.
  3786. (dolist (buffer next-buffers)
  3787. (when (and (or (buffer-live-p buffer)
  3788. (not (setq killed-buffers
  3789. (cons buffer killed-buffers))))
  3790. (not (eq buffer old-buffer))
  3791. (or (null pred) (funcall pred buffer))
  3792. (setq entry (assq buffer (window-prev-buffers window))))
  3793. (setq new-buffer buffer)
  3794. (set-window-buffer-start-and-point
  3795. window new-buffer (nth 1 entry) (nth 2 entry))
  3796. (throw 'found t)))
  3797. ;; Scan the buffer list of WINDOW's frame next, skipping previous
  3798. ;; buffers entries.
  3799. (dolist (buffer (buffer-list frame))
  3800. (when (and (buffer-live-p buffer)
  3801. (not (eq buffer old-buffer))
  3802. (or (null pred) (funcall pred buffer))
  3803. (not (eq (aref (buffer-name buffer) 0) ?\s))
  3804. (not (assq buffer (window-prev-buffers window))))
  3805. (if (and (not switch-to-visible-buffer)
  3806. (get-buffer-window buffer frame))
  3807. ;; Try to avoid showing a buffer visible in some other window.
  3808. (setq visible buffer)
  3809. (setq new-buffer buffer)
  3810. (set-window-buffer-start-and-point window new-buffer)
  3811. (throw 'found t))))
  3812. ;; Scan WINDOW's reverted previous buffers last (must not use
  3813. ;; nreverse here!)
  3814. (dolist (entry (reverse (window-prev-buffers window)))
  3815. (when (and (setq new-buffer (car entry))
  3816. (or (buffer-live-p new-buffer)
  3817. (not (setq killed-buffers
  3818. (cons new-buffer killed-buffers))))
  3819. (not (eq new-buffer old-buffer))
  3820. (or (null pred) (funcall pred new-buffer)))
  3821. (if (and (not switch-to-visible-buffer)
  3822. (get-buffer-window new-buffer frame))
  3823. ;; Try to avoid showing a buffer visible in some other window.
  3824. (unless visible
  3825. (setq visible new-buffer))
  3826. (set-window-buffer-start-and-point
  3827. window new-buffer (nth 1 entry) (nth 2 entry))
  3828. (throw 'found t))))
  3829. ;; Show a buffer visible in another window.
  3830. (when visible
  3831. (setq new-buffer visible)
  3832. (set-window-buffer-start-and-point window new-buffer)))
  3833. ;; Remove `new-buffer' from and restore WINDOW's next buffers.
  3834. (set-window-next-buffers window (delq new-buffer next-buffers))
  3835. ;; Remove killed buffers from WINDOW's previous and next buffers.
  3836. (when killed-buffers
  3837. (dolist (buffer killed-buffers)
  3838. (set-window-prev-buffers
  3839. window (assq-delete-all buffer (window-prev-buffers window)))
  3840. (set-window-next-buffers
  3841. window (delq buffer (window-next-buffers window)))))
  3842. ;; Return new-buffer.
  3843. new-buffer))
  3844. (defun get-next-valid-buffer (list &optional buffer visible-ok frame)
  3845. "Search LIST for a valid buffer to display in FRAME.
  3846. Return nil when all buffers in LIST are undesirable for display,
  3847. otherwise return the first suitable buffer in LIST.
  3848. Buffers not visible in windows are preferred to visible buffers,
  3849. unless VISIBLE-OK is non-nil.
  3850. If the optional argument FRAME is nil, it defaults to the selected frame.
  3851. If BUFFER is non-nil, ignore occurrences of that buffer in LIST."
  3852. ;; This logic is more or less copied from other-buffer.
  3853. (setq frame (or frame (selected-frame)))
  3854. (let ((pred (frame-parameter frame 'buffer-predicate))
  3855. found buf)
  3856. (while (and (not found) list)
  3857. (setq buf (car list))
  3858. (if (and (not (eq buffer buf))
  3859. (buffer-live-p buf)
  3860. (or (null pred) (funcall pred buf))
  3861. (not (eq (aref (buffer-name buf) 0) ?\s))
  3862. (or visible-ok (null (get-buffer-window buf 'visible))))
  3863. (setq found buf)
  3864. (setq list (cdr list))))
  3865. (car list)))
  3866. (defun last-buffer (&optional buffer visible-ok frame)
  3867. "Return the last buffer in FRAME's buffer list.
  3868. If BUFFER is the last buffer, return the preceding buffer
  3869. instead. Buffers not visible in windows are preferred to visible
  3870. buffers, unless optional argument VISIBLE-OK is non-nil.
  3871. Optional third argument FRAME nil or omitted means use the
  3872. selected frame's buffer list. If no such buffer exists, return
  3873. the buffer `*scratch*', creating it if necessary."
  3874. (setq frame (or frame (selected-frame)))
  3875. (or (get-next-valid-buffer (nreverse (buffer-list frame))
  3876. buffer visible-ok frame)
  3877. (get-buffer "*scratch*")
  3878. (let ((scratch (get-buffer-create "*scratch*")))
  3879. (set-buffer-major-mode scratch)
  3880. scratch)))
  3881. (defcustom frame-auto-hide-function #'iconify-frame
  3882. "Function called to automatically hide frames.
  3883. The function is called with one argument - a frame.
  3884. Functions affected by this option are those that bury a buffer
  3885. shown in a separate frame like `quit-window' and `bury-buffer'."
  3886. :type '(choice (const :tag "Iconify" iconify-frame)
  3887. (const :tag "Delete" delete-frame)
  3888. (const :tag "Do nothing" ignore)
  3889. function)
  3890. :group 'windows
  3891. :group 'frames
  3892. :version "24.1")
  3893. (defun window--delete (&optional window dedicated-only kill)
  3894. "Delete WINDOW if possible.
  3895. WINDOW must be a live window and defaults to the selected one.
  3896. Optional argument DEDICATED-ONLY non-nil means to delete WINDOW
  3897. only if it's dedicated to its buffer. Optional argument KILL
  3898. means the buffer shown in window will be killed. Return non-nil
  3899. if WINDOW gets deleted or its frame is auto-hidden."
  3900. (setq window (window-normalize-window window t))
  3901. (unless (and dedicated-only (not (window-dedicated-p window)))
  3902. (let ((deletable (window-deletable-p window)))
  3903. (cond
  3904. ((eq deletable 'frame)
  3905. (let ((frame (window-frame window)))
  3906. (cond
  3907. (kill
  3908. (delete-frame frame))
  3909. ((functionp frame-auto-hide-function)
  3910. (funcall frame-auto-hide-function frame))))
  3911. 'frame)
  3912. (deletable
  3913. (delete-window window)
  3914. t)))))
  3915. (defun bury-buffer (&optional buffer-or-name)
  3916. "Put BUFFER-OR-NAME at the end of the list of all buffers.
  3917. There it is the least likely candidate for `other-buffer' to
  3918. return; thus, the least likely buffer for \\[switch-to-buffer] to
  3919. select by default.
  3920. You can specify a buffer name as BUFFER-OR-NAME, or an actual
  3921. buffer object. If BUFFER-OR-NAME is nil or omitted, bury the
  3922. current buffer. Also, if BUFFER-OR-NAME is nil or omitted,
  3923. remove the current buffer from the selected window if it is
  3924. displayed there."
  3925. (interactive)
  3926. (let* ((buffer (window-normalize-buffer buffer-or-name)))
  3927. ;; If `buffer-or-name' is not on the selected frame we unrecord it
  3928. ;; although it's not "here" (call it a feature).
  3929. (bury-buffer-internal buffer)
  3930. ;; Handle case where `buffer-or-name' is nil and the current buffer
  3931. ;; is shown in the selected window.
  3932. (cond
  3933. ((or buffer-or-name (not (eq buffer (window-buffer)))))
  3934. ((window--delete nil t))
  3935. (t
  3936. ;; Switch to another buffer in window.
  3937. (set-window-dedicated-p nil nil)
  3938. (switch-to-prev-buffer nil 'bury)))
  3939. ;; Always return nil.
  3940. nil))
  3941. (defun unbury-buffer ()
  3942. "Switch to the last buffer in the buffer list."
  3943. (interactive)
  3944. (switch-to-buffer (last-buffer)))
  3945. (defun next-buffer ()
  3946. "In selected window switch to next buffer."
  3947. (interactive)
  3948. (cond
  3949. ((window-minibuffer-p)
  3950. (error "Cannot switch buffers in minibuffer window"))
  3951. ((eq (window-dedicated-p) t)
  3952. (error "Window is strongly dedicated to its buffer"))
  3953. (t
  3954. (switch-to-next-buffer))))
  3955. (defun previous-buffer ()
  3956. "In selected window switch to previous buffer."
  3957. (interactive)
  3958. (cond
  3959. ((window-minibuffer-p)
  3960. (error "Cannot switch buffers in minibuffer window"))
  3961. ((eq (window-dedicated-p) t)
  3962. (error "Window is strongly dedicated to its buffer"))
  3963. (t
  3964. (switch-to-prev-buffer))))
  3965. (defun delete-windows-on (&optional buffer-or-name frame)
  3966. "Delete all windows showing BUFFER-OR-NAME.
  3967. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  3968. and defaults to the current buffer.
  3969. The following non-nil values of the optional argument FRAME
  3970. have special meanings:
  3971. - t means consider all windows on the selected frame only.
  3972. - `visible' means consider all windows on all visible frames on
  3973. the current terminal.
  3974. - 0 (the number zero) means consider all windows on all visible
  3975. and iconified frames on the current terminal.
  3976. - A frame means consider all windows on that frame only.
  3977. Any other value of FRAME means consider all windows on all
  3978. frames.
  3979. When a window showing BUFFER-OR-NAME is dedicated and the only
  3980. window of its frame, that frame is deleted when there are other
  3981. frames left."
  3982. (interactive "BDelete windows on (buffer):\nP")
  3983. (let ((buffer (window-normalize-buffer buffer-or-name))
  3984. ;; Handle the "inverted" meaning of the FRAME argument wrt other
  3985. ;; `window-list-1' based function.
  3986. (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
  3987. (dolist (window (window-list-1 nil nil all-frames))
  3988. (if (eq (window-buffer window) buffer)
  3989. (let ((deletable (window-deletable-p window)))
  3990. (cond
  3991. ((and (eq deletable 'frame) (window-dedicated-p window))
  3992. ;; Delete frame if and only if window is dedicated.
  3993. (delete-frame (window-frame window)))
  3994. ((eq deletable t)
  3995. ;; Delete window.
  3996. (delete-window window))
  3997. (t
  3998. ;; In window switch to previous buffer.
  3999. (set-window-dedicated-p window nil)
  4000. (switch-to-prev-buffer window 'bury))))
  4001. ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
  4002. (unrecord-window-buffer window buffer)))))
  4003. (defun replace-buffer-in-windows (&optional buffer-or-name)
  4004. "Replace BUFFER-OR-NAME with some other buffer in all windows showing it.
  4005. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  4006. and defaults to the current buffer.
  4007. When a window showing BUFFER-OR-NAME is dedicated, that window is
  4008. deleted. If that window is the only window on its frame, the
  4009. frame is deleted too when there are other frames left. If there
  4010. are no other frames left, some other buffer is displayed in that
  4011. window.
  4012. This function removes the buffer denoted by BUFFER-OR-NAME from
  4013. all window-local buffer lists."
  4014. (interactive "bBuffer to replace: ")
  4015. (let ((buffer (window-normalize-buffer buffer-or-name)))
  4016. (dolist (window (window-list-1 nil nil t))
  4017. (if (eq (window-buffer window) buffer)
  4018. (unless (window--delete window t t)
  4019. ;; Switch to another buffer in window.
  4020. (set-window-dedicated-p window nil)
  4021. (switch-to-prev-buffer window 'kill))
  4022. ;; Unrecord BUFFER in WINDOW.
  4023. (unrecord-window-buffer window buffer)))))
  4024. (defun quit-restore-window (&optional window bury-or-kill)
  4025. "Quit WINDOW and deal with its buffer.
  4026. WINDOW must be a live window and defaults to the selected one.
  4027. According to information stored in WINDOW's `quit-restore' window
  4028. parameter either (1) delete WINDOW and its frame, (2) delete
  4029. WINDOW, (3) restore the buffer previously displayed in WINDOW,
  4030. or (4) make WINDOW display some other buffer than the present
  4031. one. If non-nil, reset `quit-restore' parameter to nil.
  4032. Optional second argument BURY-OR-KILL tells how to proceed with
  4033. the buffer of WINDOW. The following values are handled:
  4034. nil means to not handle the buffer in a particular way. This
  4035. means that if WINDOW is not deleted by this function, invoking
  4036. `switch-to-prev-buffer' will usually show the buffer again.
  4037. `append' means that if WINDOW is not deleted, move its buffer to
  4038. the end of WINDOW's previous buffers so it's less likely that a
  4039. future invocation of `switch-to-prev-buffer' will switch to it.
  4040. Also, move the buffer to the end of the frame's buffer list.
  4041. `bury' means that if WINDOW is not deleted, remove its buffer
  4042. from WINDOW'S list of previous buffers. Also, move the buffer
  4043. to the end of the frame's buffer list. This value provides the
  4044. most reliable remedy to not have `switch-to-prev-buffer' switch
  4045. to this buffer again without killing the buffer.
  4046. `kill' means to kill WINDOW's buffer."
  4047. (setq window (window-normalize-window window t))
  4048. (let* ((buffer (window-buffer window))
  4049. (quit-restore (window-parameter window 'quit-restore))
  4050. (prev-buffer
  4051. (let* ((prev-buffers (window-prev-buffers window))
  4052. (prev-buffer (caar prev-buffers)))
  4053. (and (or (not (eq prev-buffer buffer))
  4054. (and (cdr prev-buffers)
  4055. (not (eq (setq prev-buffer (cadr prev-buffers))
  4056. buffer))))
  4057. prev-buffer)))
  4058. quad entry)
  4059. (cond
  4060. ((and (not prev-buffer)
  4061. (or (eq (nth 1 quit-restore) 'frame)
  4062. (and (eq (nth 1 quit-restore) 'window)
  4063. ;; If the window has been created on an existing
  4064. ;; frame and ended up as the sole window on that
  4065. ;; frame, do not delete it (Bug#12764).
  4066. (not (eq window (frame-root-window window)))))
  4067. (eq (nth 3 quit-restore) buffer)
  4068. ;; Delete WINDOW if possible.
  4069. (window--delete window nil (eq bury-or-kill 'kill)))
  4070. ;; If the previously selected window is still alive, select it.
  4071. (when (window-live-p (nth 2 quit-restore))
  4072. (select-window (nth 2 quit-restore))))
  4073. ((and (listp (setq quad (nth 1 quit-restore)))
  4074. (buffer-live-p (car quad))
  4075. (eq (nth 3 quit-restore) buffer))
  4076. ;; Show another buffer stored in quit-restore parameter.
  4077. (when (and (integerp (nth 3 quad))
  4078. (if (window-combined-p window)
  4079. (/= (nth 3 quad) (window-total-height window))
  4080. (/= (nth 3 quad) (window-total-width window))))
  4081. ;; Try to resize WINDOW to its old height but don't signal an
  4082. ;; error.
  4083. (condition-case nil
  4084. (window-resize
  4085. window
  4086. (- (nth 3 quad) (if (window-combined-p window)
  4087. (window-total-height window)
  4088. (window-total-width window)))
  4089. (window-combined-p window t))
  4090. (error nil)))
  4091. (set-window-dedicated-p window nil)
  4092. ;; Restore WINDOW's previous buffer, start and point position.
  4093. (set-window-buffer-start-and-point
  4094. window (nth 0 quad) (nth 1 quad) (nth 2 quad))
  4095. ;; Deal with the buffer we just removed from WINDOW.
  4096. (setq entry (and (eq bury-or-kill 'append)
  4097. (assq buffer (window-prev-buffers window))))
  4098. (when bury-or-kill
  4099. ;; Remove buffer from WINDOW's previous and next buffers.
  4100. (set-window-prev-buffers
  4101. window (assq-delete-all buffer (window-prev-buffers window)))
  4102. (set-window-next-buffers
  4103. window (delq buffer (window-next-buffers window))))
  4104. (when entry
  4105. ;; Append old buffer's entry to list of WINDOW's previous
  4106. ;; buffers so it's less likely to get switched to soon but
  4107. ;; `display-buffer-in-previous-window' can nevertheless find it.
  4108. (set-window-prev-buffers
  4109. window (append (window-prev-buffers window) (list entry))))
  4110. ;; Reset the quit-restore parameter.
  4111. (set-window-parameter window 'quit-restore nil)
  4112. ;; Select old window.
  4113. (when (window-live-p (nth 2 quit-restore))
  4114. (select-window (nth 2 quit-restore))))
  4115. (t
  4116. ;; Show some other buffer in WINDOW and reset the quit-restore
  4117. ;; parameter.
  4118. (set-window-parameter window 'quit-restore nil)
  4119. ;; Make sure that WINDOW is no more dedicated.
  4120. (set-window-dedicated-p window nil)
  4121. (switch-to-prev-buffer window bury-or-kill)))
  4122. ;; Deal with the buffer.
  4123. (cond
  4124. ((not (buffer-live-p buffer)))
  4125. ((eq bury-or-kill 'kill)
  4126. (kill-buffer buffer))
  4127. (bury-or-kill
  4128. (bury-buffer-internal buffer)))))
  4129. (defun quit-window (&optional kill window)
  4130. "Quit WINDOW and bury its buffer.
  4131. WINDOW must be a live window and defaults to the selected one.
  4132. With prefix argument KILL non-nil, kill the buffer instead of
  4133. burying it.
  4134. According to information stored in WINDOW's `quit-restore' window
  4135. parameter either (1) delete WINDOW and its frame, (2) delete
  4136. WINDOW, (3) restore the buffer previously displayed in WINDOW,
  4137. or (4) make WINDOW display some other buffer than the present
  4138. one. If non-nil, reset `quit-restore' parameter to nil."
  4139. (interactive "P")
  4140. (quit-restore-window window (if kill 'kill 'bury)))
  4141. (defun quit-windows-on (&optional buffer-or-name kill frame)
  4142. "Quit all windows showing BUFFER-OR-NAME.
  4143. BUFFER-OR-NAME may be a buffer or the name of an existing buffer
  4144. and defaults to the current buffer. Optional argument KILL
  4145. non-nil means to kill BUFFER-OR-NAME. KILL nil means to bury
  4146. BUFFER-OR-NAME. Optional argument FRAME is handled as by
  4147. `delete-windows-on'.
  4148. This function calls `quit-window' on all candidate windows
  4149. showing BUFFER-OR-NAME."
  4150. (interactive "BQuit windows on (buffer):\nP")
  4151. (let ((buffer (window-normalize-buffer buffer-or-name))
  4152. ;; Handle the "inverted" meaning of the FRAME argument wrt other
  4153. ;; `window-list-1' based function.
  4154. (all-frames (cond ((not frame) t) ((eq frame t) nil) (t frame))))
  4155. (dolist (window (window-list-1 nil nil all-frames))
  4156. (if (eq (window-buffer window) buffer)
  4157. (quit-window kill window)
  4158. ;; If a window doesn't show BUFFER, unrecord BUFFER in it.
  4159. (unrecord-window-buffer window buffer)))))
  4160. (defun split-window (&optional window size side pixelwise)
  4161. "Make a new window adjacent to WINDOW.
  4162. WINDOW must be a valid window and defaults to the selected one.
  4163. Return the new window which is always a live window.
  4164. Optional argument SIZE a positive number means make WINDOW SIZE
  4165. lines or columns tall. If SIZE is negative, make the new window
  4166. -SIZE lines or columns tall. If and only if SIZE is non-nil, its
  4167. absolute value can be less than `window-min-height' or
  4168. `window-min-width'; so this command can make a new window as
  4169. small as one line or two columns. SIZE defaults to half of
  4170. WINDOW's size.
  4171. Optional third argument SIDE nil (or `below') specifies that the
  4172. new window shall be located below WINDOW. SIDE `above' means the
  4173. new window shall be located above WINDOW. In both cases SIZE
  4174. specifies the new number of lines for WINDOW (or the new window
  4175. if SIZE is negative) including space reserved for the mode and/or
  4176. header line.
  4177. SIDE t (or `right') specifies that the new window shall be
  4178. located on the right side of WINDOW. SIDE `left' means the new
  4179. window shall be located on the left of WINDOW. In both cases
  4180. SIZE specifies the new number of columns for WINDOW (or the new
  4181. window provided SIZE is negative) including space reserved for
  4182. fringes and the scrollbar or a divider column. Any other non-nil
  4183. value for SIDE is currently handled like t (or `right').
  4184. PIXELWISE, if non-nil, means to interpret SIZE pixelwise.
  4185. If the variable `ignore-window-parameters' is non-nil or the
  4186. `split-window' parameter of WINDOW equals t, do not process any
  4187. parameters of WINDOW. Otherwise, if the `split-window' parameter
  4188. of WINDOW specifies a function, call that function with all three
  4189. arguments and return the value returned by that function.
  4190. Otherwise, if WINDOW is part of an atomic window, \"split\" the
  4191. root of that atomic window. The new window does not become a
  4192. member of that atomic window.
  4193. If WINDOW is live, properties of the new window like margins and
  4194. scrollbars are inherited from WINDOW. If WINDOW is an internal
  4195. window, these properties as well as the buffer displayed in the
  4196. new window are inherited from the window selected on WINDOW's
  4197. frame. The selected window is not changed by this function."
  4198. (setq window (window-normalize-window window))
  4199. (let* ((side (cond
  4200. ((not side) 'below)
  4201. ((memq side '(below above right left)) side)
  4202. (t 'right)))
  4203. (horizontal (not (memq side '(below above))))
  4204. (frame (window-frame window))
  4205. (parent (window-parent window))
  4206. (function (window-parameter window 'split-window))
  4207. (window-side (window-parameter window 'window-side))
  4208. ;; Rebind the following two variables since in some cases we
  4209. ;; have to override their value.
  4210. (window-combination-limit window-combination-limit)
  4211. (window-combination-resize window-combination-resize)
  4212. (char-size (frame-char-size window horizontal))
  4213. (pixel-size
  4214. (when (numberp size)
  4215. (window--size-to-pixel window size horizontal pixelwise t)))
  4216. (divider-width (if horizontal
  4217. (frame-right-divider-width frame)
  4218. (frame-bottom-divider-width frame)))
  4219. atom-root ignore)
  4220. (window--check frame)
  4221. (catch 'done
  4222. (cond
  4223. ;; Ignore window parameters if either `ignore-window-parameters'
  4224. ;; is t or the `split-window' parameter equals t.
  4225. ((or ignore-window-parameters (eq function t)))
  4226. ((functionp function)
  4227. ;; The `split-window' parameter specifies the function to call.
  4228. ;; If that function is `ignore', do nothing.
  4229. (throw 'done (funcall function window size side)))
  4230. ;; If WINDOW is part of an atomic window, split the root window
  4231. ;; of that atomic window instead.
  4232. ((and (window-parameter window 'window-atom)
  4233. (setq atom-root (window-atom-root window))
  4234. (not (eq atom-root window)))
  4235. (throw 'done (split-window atom-root size side pixelwise)))
  4236. ;; If WINDOW is a side window or its first or last child is a
  4237. ;; side window, throw an error unless `window-combination-resize'
  4238. ;; equals 'side.
  4239. ((and (not (eq window-combination-resize 'side))
  4240. (window--side-window-p window))
  4241. (error "Cannot split side window or parent of side window"))
  4242. ;; If `window-combination-resize' is 'side and window has a side
  4243. ;; window sibling, bind `window-combination-limit' to t.
  4244. ((and (not (eq window-combination-resize 'side))
  4245. (or (and (window-prev-sibling window)
  4246. (window-parameter
  4247. (window-prev-sibling window) 'window-side))
  4248. (and (window-next-sibling window)
  4249. (window-parameter
  4250. (window-next-sibling window) 'window-side))))
  4251. (setq window-combination-limit t)))
  4252. ;; If `window-combination-resize' is t and SIZE is non-negative,
  4253. ;; bind `window-combination-limit' to t.
  4254. (when (and (eq window-combination-resize t)
  4255. pixel-size (> pixel-size 0))
  4256. (setq window-combination-limit t))
  4257. (let* ((parent-pixel-size
  4258. ;; `parent-pixel-size' is the pixel size of WINDOW's
  4259. ;; parent, provided it has one.
  4260. (when parent (window-size parent horizontal t)))
  4261. ;; `resize' non-nil means we are supposed to resize other
  4262. ;; windows in WINDOW's combination.
  4263. (resize
  4264. (and window-combination-resize
  4265. (or (window-parameter window 'window-side)
  4266. (not (eq window-combination-resize 'side)))
  4267. (not (eq window-combination-limit t))
  4268. ;; Resize makes sense in iso-combinations only.
  4269. (window-combined-p window horizontal)))
  4270. ;; `old-pixel-size' is the current pixel size of WINDOW.
  4271. (old-pixel-size (window-size window horizontal t))
  4272. ;; `new-size' is the specified or calculated size of the
  4273. ;; new window.
  4274. new-pixel-size new-parent new-normal)
  4275. (cond
  4276. ((not pixel-size)
  4277. (setq new-pixel-size
  4278. (if resize
  4279. ;; When resizing try to give the new window the
  4280. ;; average size of a window in its combination.
  4281. (max (min (- parent-pixel-size
  4282. (window-min-size parent horizontal nil t))
  4283. (/ parent-pixel-size
  4284. (1+ (window-combinations parent horizontal))))
  4285. (window-min-pixel-size))
  4286. ;; Else try to give the new window half the size
  4287. ;; of WINDOW (plus an eventual odd pixel).
  4288. (/ old-pixel-size 2)))
  4289. (unless window-resize-pixelwise
  4290. ;; Round to nearest char-size multiple.
  4291. (setq new-pixel-size
  4292. (* char-size (round new-pixel-size char-size)))))
  4293. ((>= pixel-size 0)
  4294. ;; SIZE non-negative specifies the new size of WINDOW.
  4295. ;; Note: Specifying a non-negative SIZE is practically
  4296. ;; always done as workaround for making the new window
  4297. ;; appear above or on the left of the new window (the
  4298. ;; ispell window is a typical example of that). In all
  4299. ;; these cases the SIDE argument should be set to 'above
  4300. ;; or 'left in order to support the 'resize option.
  4301. ;; Here we have to nest the windows instead, see above.
  4302. (setq new-pixel-size (- old-pixel-size pixel-size)))
  4303. (t
  4304. ;; SIZE negative specifies the size of the new window.
  4305. (setq new-pixel-size (- pixel-size))))
  4306. ;; Check SIZE.
  4307. (cond
  4308. ((not pixel-size)
  4309. (cond
  4310. (resize
  4311. ;; SIZE unspecified, resizing.
  4312. (unless (or (window-sizable-p
  4313. parent (- (+ new-pixel-size divider-width)) horizontal
  4314. nil t)
  4315. (window-sizable-p
  4316. parent (- (+ new-pixel-size divider-width)) horizontal
  4317. (setq ignore 'preserved) t))
  4318. (error "Window %s too small for splitting (1)" parent)))
  4319. ((and (> (+ new-pixel-size divider-width
  4320. (window-min-size window horizontal nil t))
  4321. old-pixel-size)
  4322. (> (+ new-pixel-size divider-width
  4323. (window-min-size
  4324. window horizontal (setq ignore 'preserved) t))
  4325. old-pixel-size))
  4326. ;; SIZE unspecified, no resizing.
  4327. (error "Window %s too small for splitting (2)" window))))
  4328. ((and (>= pixel-size 0)
  4329. (or (>= pixel-size old-pixel-size)
  4330. (< new-pixel-size
  4331. (window-safe-min-pixel-size window horizontal))))
  4332. ;; SIZE specified as new size of old window. If the new size
  4333. ;; is larger than the old size or the size of the new window
  4334. ;; would be less than the safe minimum, signal an error.
  4335. (error "Window %s too small for splitting (3)" window))
  4336. (resize
  4337. ;; SIZE specified, resizing.
  4338. (unless (or (window-sizable-p
  4339. parent (- (+ new-pixel-size divider-width)) horizontal
  4340. nil t)
  4341. (window-sizable-p
  4342. parent (- (+ new-pixel-size divider-width)) horizontal
  4343. (setq ignore 'preserved) t))
  4344. ;; If we cannot resize the parent give up.
  4345. (error "Window %s too small for splitting (4)" parent)))
  4346. ((or (< new-pixel-size
  4347. (window-safe-min-pixel-size window horizontal))
  4348. (< (- old-pixel-size new-pixel-size)
  4349. (window-safe-min-pixel-size window horizontal)))
  4350. ;; SIZE specification violates minimum size restrictions.
  4351. (error "Window %s too small for splitting (5)" window)))
  4352. (window--resize-reset frame horizontal)
  4353. (setq new-parent
  4354. ;; Make new-parent non-nil if we need a new parent window;
  4355. ;; either because we want to nest or because WINDOW is not
  4356. ;; iso-combined.
  4357. (or (eq window-combination-limit t)
  4358. (not (window-combined-p window horizontal))))
  4359. (setq new-normal
  4360. ;; Make new-normal the normal size of the new window.
  4361. (cond
  4362. (pixel-size (/ (float new-pixel-size)
  4363. (if new-parent old-pixel-size parent-pixel-size)))
  4364. (new-parent 0.5)
  4365. (resize (/ 1.0 (1+ (window-combinations parent horizontal))))
  4366. (t (/ (window-normal-size window horizontal) 2.0))))
  4367. (if resize
  4368. ;; Try to get space from OLD's siblings. We could go "up" and
  4369. ;; try getting additional space from surrounding windows but
  4370. ;; we won't be able to return space to those windows when we
  4371. ;; delete the one we create here. Hence we do not go up.
  4372. (progn
  4373. (window--resize-child-windows
  4374. parent (- new-pixel-size) horizontal nil ignore)
  4375. (let* ((normal (- 1.0 new-normal))
  4376. (sub (window-child parent)))
  4377. (while sub
  4378. (set-window-new-normal
  4379. sub (* (window-normal-size sub horizontal) normal))
  4380. (setq sub (window-right sub)))))
  4381. ;; Get entire space from WINDOW.
  4382. (set-window-new-pixel
  4383. window (- old-pixel-size new-pixel-size))
  4384. (window--resize-this-window
  4385. window (- new-pixel-size) horizontal ignore)
  4386. (set-window-new-normal
  4387. window (- (if new-parent 1.0 (window-normal-size window horizontal))
  4388. new-normal)))
  4389. (let* ((new (split-window-internal window new-pixel-size side new-normal)))
  4390. (window--pixel-to-total frame horizontal)
  4391. ;; Assign window-side parameters, if any.
  4392. (cond
  4393. ((eq window-combination-resize 'side)
  4394. (let ((window-side
  4395. (cond
  4396. (window-side window-side)
  4397. ((eq side 'above) 'top)
  4398. ((eq side 'below) 'bottom)
  4399. (t side))))
  4400. ;; We made a new side window.
  4401. (set-window-parameter new 'window-side window-side)
  4402. (when (and new-parent (window-parameter window 'window-side))
  4403. ;; We've been splitting a side root window. Give the
  4404. ;; new parent the same window-side parameter.
  4405. (set-window-parameter
  4406. (window-parent new) 'window-side window-side))))
  4407. ((eq window-combination-resize 'atom)
  4408. ;; Make sure `window--check-frame' won't destroy an existing
  4409. ;; atomic window in case the new window gets nested inside.
  4410. (unless (window-parameter window 'window-atom)
  4411. (set-window-parameter window 'window-atom t))
  4412. (when new-parent
  4413. (set-window-parameter (window-parent new) 'window-atom t))
  4414. (set-window-parameter new 'window-atom t)))
  4415. ;; Sanitize sizes.
  4416. (window--sanitize-window-sizes frame horizontal)
  4417. (run-window-configuration-change-hook frame)
  4418. (run-window-scroll-functions new)
  4419. (window--check frame)
  4420. ;; Always return the new window.
  4421. new)))))
  4422. ;; I think this should be the default; I think people will prefer it--rms.
  4423. (defcustom split-window-keep-point t
  4424. "If non-nil, \\[split-window-below] preserves point in the new window.
  4425. If nil, adjust point in the two windows to minimize redisplay.
  4426. This option applies only to `split-window-below' and functions
  4427. that call it. The low-level `split-window' function always keeps
  4428. the original point in both windows."
  4429. :type 'boolean
  4430. :group 'windows)
  4431. (defun split-window-below (&optional size)
  4432. "Split the selected window into two windows, one above the other.
  4433. The selected window is above. The newly split-off window is
  4434. below and displays the same buffer. Return the new window.
  4435. If optional argument SIZE is omitted or nil, both windows get the
  4436. same height, or close to it. If SIZE is positive, the upper
  4437. \(selected) window gets SIZE lines. If SIZE is negative, the
  4438. lower (new) window gets -SIZE lines.
  4439. If the variable `split-window-keep-point' is non-nil, both
  4440. windows get the same value of point as the selected window.
  4441. Otherwise, the window starts are chosen so as to minimize the
  4442. amount of redisplay; this is convenient on slow terminals."
  4443. (interactive "P")
  4444. (let ((old-window (selected-window))
  4445. (old-point (window-point))
  4446. (size (and size (prefix-numeric-value size)))
  4447. moved-by-window-height moved new-window bottom)
  4448. (when (and size (< size 0) (< (- size) window-min-height))
  4449. ;; `split-window' would not signal an error here.
  4450. (error "Size of new window too small"))
  4451. (setq new-window (split-window nil size))
  4452. (unless split-window-keep-point
  4453. (with-current-buffer (window-buffer)
  4454. ;; Use `save-excursion' around vertical movements below
  4455. ;; (Bug#10971). Note: When the selected window's buffer has a
  4456. ;; header line, up to two lines of the buffer may not show up
  4457. ;; in the resulting configuration.
  4458. (save-excursion
  4459. (goto-char (window-start))
  4460. (setq moved (vertical-motion (window-height)))
  4461. (set-window-start new-window (point))
  4462. (when (> (point) (window-point new-window))
  4463. (set-window-point new-window (point)))
  4464. (when (= moved (window-height))
  4465. (setq moved-by-window-height t)
  4466. (vertical-motion -1))
  4467. (setq bottom (point)))
  4468. (and moved-by-window-height
  4469. (<= bottom (point))
  4470. (set-window-point old-window (1- bottom)))
  4471. (and moved-by-window-height
  4472. (<= (window-start new-window) old-point)
  4473. (set-window-point new-window old-point)
  4474. (select-window new-window))))
  4475. ;; Always copy quit-restore parameter in interactive use.
  4476. (let ((quit-restore (window-parameter old-window 'quit-restore)))
  4477. (when quit-restore
  4478. (set-window-parameter new-window 'quit-restore quit-restore)))
  4479. new-window))
  4480. (defalias 'split-window-vertically 'split-window-below)
  4481. (defun split-window-right (&optional size)
  4482. "Split the selected window into two side-by-side windows.
  4483. The selected window is on the left. The newly split-off window
  4484. is on the right and displays the same buffer. Return the new
  4485. window.
  4486. If optional argument SIZE is omitted or nil, both windows get the
  4487. same width, or close to it. If SIZE is positive, the left-hand
  4488. \(selected) window gets SIZE columns. If SIZE is negative, the
  4489. right-hand (new) window gets -SIZE columns. Here, SIZE includes
  4490. the width of the window's scroll bar; if there are no scroll
  4491. bars, it includes the width of the divider column to the window's
  4492. right, if any."
  4493. (interactive "P")
  4494. (let ((old-window (selected-window))
  4495. (size (and size (prefix-numeric-value size)))
  4496. new-window)
  4497. (when (and size (< size 0) (< (- size) window-min-width))
  4498. ;; `split-window' would not signal an error here.
  4499. (error "Size of new window too small"))
  4500. (setq new-window (split-window nil size t))
  4501. ;; Always copy quit-restore parameter in interactive use.
  4502. (let ((quit-restore (window-parameter old-window 'quit-restore)))
  4503. (when quit-restore
  4504. (set-window-parameter new-window 'quit-restore quit-restore)))
  4505. new-window))
  4506. (defalias 'split-window-horizontally 'split-window-right)
  4507. ;;; Balancing windows.
  4508. ;; The following routine uses the recycled code from an old version of
  4509. ;; `window--resize-child-windows'. It's not very pretty, but coding it the way the
  4510. ;; new `window--resize-child-windows' code does would hardly make it any shorter or
  4511. ;; more readable (FWIW we'd need three loops - one to calculate the
  4512. ;; minimum sizes per window, one to enlarge or shrink windows until the
  4513. ;; new parent-size matches, and one where we shrink the largest/enlarge
  4514. ;; the smallest window).
  4515. (defun balance-windows-2 (window horizontal)
  4516. "Subroutine of `balance-windows-1'.
  4517. WINDOW must be a vertical combination (horizontal if HORIZONTAL
  4518. is non-nil)."
  4519. (let* ((char-size (if window-resize-pixelwise
  4520. 1
  4521. (frame-char-size window horizontal)))
  4522. (first (window-child window))
  4523. (sub first)
  4524. (number-of-children 0)
  4525. (parent-size (window-new-pixel window))
  4526. (total-sum parent-size)
  4527. failed size sub-total sub-delta sub-amount rest)
  4528. (while sub
  4529. (setq number-of-children (1+ number-of-children))
  4530. (when (window-size-fixed-p sub horizontal)
  4531. (setq total-sum
  4532. (- total-sum (window-size sub horizontal t)))
  4533. (set-window-new-normal sub 'ignore))
  4534. (setq sub (window-right sub)))
  4535. (setq failed t)
  4536. (while (and failed (> number-of-children 0))
  4537. (setq size (/ total-sum number-of-children))
  4538. (setq failed nil)
  4539. (setq sub first)
  4540. (while (and sub (not failed))
  4541. ;; Ignore child windows that should be ignored or are stuck.
  4542. (unless (window--resize-child-windows-skip-p sub)
  4543. (setq sub-total (window-size sub horizontal t))
  4544. (setq sub-delta (- size sub-total))
  4545. (setq sub-amount
  4546. (window-sizable sub sub-delta horizontal nil t))
  4547. ;; Register the new total size for this child window.
  4548. (set-window-new-pixel sub (+ sub-total sub-amount))
  4549. (unless (= sub-amount sub-delta)
  4550. (setq total-sum (- total-sum sub-total sub-amount))
  4551. (setq number-of-children (1- number-of-children))
  4552. ;; We failed and need a new round.
  4553. (setq failed t)
  4554. (set-window-new-normal sub 'skip)))
  4555. (setq sub (window-right sub))))
  4556. ;; How can we be sure that `number-of-children' is NOT zero here ?
  4557. (setq rest (% total-sum number-of-children))
  4558. ;; Fix rounding by trying to enlarge non-stuck windows by one line
  4559. ;; (column) until `rest' is zero.
  4560. (setq sub first)
  4561. (while (and sub (> rest 0))
  4562. (unless (window--resize-child-windows-skip-p window)
  4563. (set-window-new-pixel sub (min rest char-size) t)
  4564. (setq rest (- rest char-size)))
  4565. (setq sub (window-right sub)))
  4566. ;; Fix rounding by trying to enlarge stuck windows by one line
  4567. ;; (column) until `rest' equals zero.
  4568. (setq sub first)
  4569. (while (and sub (> rest 0))
  4570. (unless (eq (window-new-normal sub) 'ignore)
  4571. (set-window-new-pixel sub (min rest char-size) t)
  4572. (setq rest (- rest char-size)))
  4573. (setq sub (window-right sub)))
  4574. (setq sub first)
  4575. (while sub
  4576. ;; Record new normal sizes.
  4577. (set-window-new-normal
  4578. sub (/ (if (eq (window-new-normal sub) 'ignore)
  4579. (window-size sub horizontal t)
  4580. (window-new-pixel sub))
  4581. (float parent-size)))
  4582. ;; Recursively balance each window's child windows.
  4583. (balance-windows-1 sub horizontal)
  4584. (setq sub (window-right sub)))))
  4585. (defun balance-windows-1 (window &optional horizontal)
  4586. "Subroutine of `balance-windows'."
  4587. (if (window-child window)
  4588. (let ((sub (window-child window)))
  4589. (if (window-combined-p sub horizontal)
  4590. (balance-windows-2 window horizontal)
  4591. (let ((size (window-new-pixel window)))
  4592. (while sub
  4593. (set-window-new-pixel sub size)
  4594. (balance-windows-1 sub horizontal)
  4595. (setq sub (window-right sub))))))))
  4596. (defun balance-windows (&optional window-or-frame)
  4597. "Balance the sizes of windows of WINDOW-OR-FRAME.
  4598. WINDOW-OR-FRAME is optional and defaults to the selected frame.
  4599. If WINDOW-OR-FRAME denotes a frame, balance the sizes of all
  4600. windows of that frame. If WINDOW-OR-FRAME denotes a window,
  4601. recursively balance the sizes of all child windows of that
  4602. window."
  4603. (interactive)
  4604. (let* ((window
  4605. (cond
  4606. ((or (not window-or-frame)
  4607. (frame-live-p window-or-frame))
  4608. (frame-root-window window-or-frame))
  4609. ((or (window-live-p window-or-frame)
  4610. (window-child window-or-frame))
  4611. window-or-frame)
  4612. (t
  4613. (error "Not a window or frame %s" window-or-frame))))
  4614. (frame (window-frame window)))
  4615. ;; Balance vertically.
  4616. (window--resize-reset (window-frame window))
  4617. (balance-windows-1 window)
  4618. (when (window--resize-apply-p frame)
  4619. (window-resize-apply frame)
  4620. (window--pixel-to-total frame)
  4621. (run-window-configuration-change-hook frame))
  4622. ;; Balance horizontally.
  4623. (window--resize-reset (window-frame window) t)
  4624. (balance-windows-1 window t)
  4625. (when (window--resize-apply-p frame t)
  4626. (window-resize-apply frame t)
  4627. (window--pixel-to-total frame t)
  4628. (run-window-configuration-change-hook frame))))
  4629. (defun window-fixed-size-p (&optional window direction)
  4630. "Return t if WINDOW cannot be resized in DIRECTION.
  4631. WINDOW defaults to the selected window. DIRECTION can be
  4632. nil (i.e. any), `height' or `width'."
  4633. (with-current-buffer (window-buffer window)
  4634. (when (and (boundp 'window-size-fixed) window-size-fixed)
  4635. (not (and direction
  4636. (member (cons direction window-size-fixed)
  4637. '((height . width) (width . height))))))))
  4638. ;;; A different solution to balance-windows.
  4639. (defvar window-area-factor 1
  4640. "Factor by which the window area should be over-estimated.
  4641. This is used by `balance-windows-area'.
  4642. Changing this globally has no effect.")
  4643. (make-variable-buffer-local 'window-area-factor)
  4644. (defun balance-windows-area-adjust (window delta horizontal pixelwise)
  4645. "Wrapper around `window-resize' with error checking.
  4646. Arguments WINDOW, DELTA and HORIZONTAL are passed on to that function."
  4647. ;; `window-resize' may fail if delta is too large.
  4648. (while (>= (abs delta) 1)
  4649. (condition-case nil
  4650. (progn
  4651. ;; It was wrong to use `window-resize' here. Somehow
  4652. ;; `balance-windows-area' depends on resizing windows
  4653. ;; asymmetrically.
  4654. (adjust-window-trailing-edge window delta horizontal pixelwise)
  4655. (setq delta 0))
  4656. (error
  4657. ;;(message "adjust: %s" (error-message-string err))
  4658. (setq delta (/ delta 2))))))
  4659. (defun balance-windows-area ()
  4660. "Make all visible windows the same area (approximately).
  4661. See also `window-area-factor' to change the relative size of
  4662. specific buffers."
  4663. (interactive)
  4664. (let* ((unchanged 0) (carry 0) (round 0)
  4665. ;; Remove fixed-size windows.
  4666. (wins (delq nil (mapcar (lambda (win)
  4667. (if (not (window-fixed-size-p win)) win))
  4668. (window-list nil 'nomini))))
  4669. (changelog nil)
  4670. (pixelwise window-resize-pixelwise)
  4671. next)
  4672. ;; Resizing a window changes the size of surrounding windows in complex
  4673. ;; ways, so it's difficult to balance them all. The introduction of
  4674. ;; `adjust-window-trailing-edge' made it a bit easier, but it is still
  4675. ;; very difficult to do. `balance-window' above takes an off-line
  4676. ;; approach: get the whole window tree, then balance it, then try to
  4677. ;; adjust the windows so they fit the result.
  4678. ;; Here, instead, we take a "local optimization" approach, where we just
  4679. ;; go through all the windows several times until nothing needs to be
  4680. ;; changed. The main problem with this approach is that it's difficult
  4681. ;; to make sure it terminates, so we use some heuristic to try and break
  4682. ;; off infinite loops.
  4683. ;; After a round without any change, we allow a second, to give a chance
  4684. ;; to the carry to propagate a minor imbalance from the end back to
  4685. ;; the beginning.
  4686. (while (< unchanged 2)
  4687. ;; (message "New round")
  4688. (setq unchanged (1+ unchanged) round (1+ round))
  4689. (dolist (win wins)
  4690. (setq next win)
  4691. (while (progn (setq next (next-window next))
  4692. (window-fixed-size-p next)))
  4693. ;; (assert (eq next (or (cadr (member win wins)) (car wins))))
  4694. (let* ((horiz
  4695. (< (car (window-pixel-edges win)) (car (window-pixel-edges next))))
  4696. (areadiff (/ (- (* (window-size next nil pixelwise)
  4697. (window-size next t pixelwise)
  4698. (buffer-local-value 'window-area-factor
  4699. (window-buffer next)))
  4700. (* (window-size win nil pixelwise)
  4701. (window-size win t pixelwise)
  4702. (buffer-local-value 'window-area-factor
  4703. (window-buffer win))))
  4704. (max (buffer-local-value 'window-area-factor
  4705. (window-buffer win))
  4706. (buffer-local-value 'window-area-factor
  4707. (window-buffer next)))))
  4708. (edgesize (if horiz
  4709. (+ (window-size win nil pixelwise)
  4710. (window-size next nil pixelwise))
  4711. (+ (window-size win t pixelwise)
  4712. (window-size next t pixelwise))))
  4713. (diff (/ areadiff edgesize)))
  4714. (when (zerop diff)
  4715. ;; Maybe diff is actually closer to 1 than to 0.
  4716. (setq diff (/ (* 3 areadiff) (* 2 edgesize))))
  4717. (when (and (zerop diff) (not (zerop areadiff)))
  4718. (setq diff (/ (+ areadiff carry) edgesize))
  4719. ;; Change things smoothly.
  4720. (if (or (> diff 1) (< diff -1)) (setq diff (/ diff 2))))
  4721. (if (zerop diff)
  4722. ;; Make sure negligible differences don't accumulate to
  4723. ;; become significant.
  4724. (setq carry (+ carry areadiff))
  4725. ;; This used `adjust-window-trailing-edge' before and uses
  4726. ;; `window-resize' now. Error wrapping is still needed.
  4727. (balance-windows-area-adjust win diff horiz pixelwise)
  4728. ;; (sit-for 0.5)
  4729. (let ((change (cons win (window-pixel-edges win))))
  4730. ;; If the same change has been seen already for this window,
  4731. ;; we're most likely in an endless loop, so don't count it as
  4732. ;; a change.
  4733. (unless (member change changelog)
  4734. (push change changelog)
  4735. (setq unchanged 0 carry 0)))))))
  4736. ;; We've now basically balanced all the windows.
  4737. ;; But there may be some minor off-by-one imbalance left over,
  4738. ;; so let's do some fine tuning.
  4739. ;; (bw-finetune wins)
  4740. ;; (message "Done in %d rounds" round)
  4741. ))
  4742. ;;; Window states, how to get them and how to put them in a window.
  4743. (defun window--state-get-1 (window &optional writable)
  4744. "Helper function for `window-state-get'."
  4745. (let* ((type
  4746. (cond
  4747. ((window-top-child window) 'vc)
  4748. ((window-left-child window) 'hc)
  4749. (t 'leaf)))
  4750. (buffer (window-buffer window))
  4751. (selected (eq window (selected-window)))
  4752. (head
  4753. `(,type
  4754. ,@(unless (window-next-sibling window) `((last . t)))
  4755. (pixel-width . ,(window-pixel-width window))
  4756. (pixel-height . ,(window-pixel-height window))
  4757. (total-width . ,(window-total-width window))
  4758. (total-height . ,(window-total-height window))
  4759. (normal-height . ,(window-normal-size window))
  4760. (normal-width . ,(window-normal-size window t))
  4761. ,@(unless (window-live-p window)
  4762. `((combination-limit . ,(window-combination-limit window))))
  4763. ,@(let ((parameters (window-parameters window))
  4764. list)
  4765. ;; Make copies of those window parameters whose
  4766. ;; persistence property is `writable' if WRITABLE is
  4767. ;; non-nil and non-nil if WRITABLE is nil.
  4768. (dolist (par parameters)
  4769. (let ((pers (cdr (assq (car par)
  4770. window-persistent-parameters))))
  4771. (when (and pers (or (not writable) (eq pers 'writable)))
  4772. (setq list (cons (cons (car par) (cdr par)) list)))))
  4773. ;; Add `clone-of' parameter if necessary.
  4774. (let ((pers (cdr (assq 'clone-of
  4775. window-persistent-parameters))))
  4776. (when (and pers (or (not writable) (eq pers 'writable))
  4777. (not (assq 'clone-of list)))
  4778. (setq list (cons (cons 'clone-of window) list))))
  4779. (when list
  4780. `((parameters . ,list))))
  4781. ,@(when buffer
  4782. ;; All buffer related things go in here.
  4783. (let ((point (window-point window))
  4784. (start (window-start window)))
  4785. `((buffer
  4786. ,(buffer-name buffer)
  4787. (selected . ,selected)
  4788. (hscroll . ,(window-hscroll window))
  4789. (fringes . ,(window-fringes window))
  4790. (margins . ,(window-margins window))
  4791. (scroll-bars . ,(window-scroll-bars window))
  4792. (vscroll . ,(window-vscroll window))
  4793. (dedicated . ,(window-dedicated-p window))
  4794. (point . ,(if writable point
  4795. (copy-marker point
  4796. (buffer-local-value
  4797. 'window-point-insertion-type
  4798. buffer))))
  4799. (start . ,(if writable start (copy-marker start)))))))))
  4800. (tail
  4801. (when (memq type '(vc hc))
  4802. (let (list)
  4803. (setq window (window-child window))
  4804. (while window
  4805. (setq list (cons (window--state-get-1 window writable) list))
  4806. (setq window (window-right window)))
  4807. (nreverse list)))))
  4808. (append head tail)))
  4809. (defun window-state-get (&optional window writable)
  4810. "Return state of WINDOW as a Lisp object.
  4811. WINDOW can be any window and defaults to the root window of the
  4812. selected frame.
  4813. Optional argument WRITABLE non-nil means do not use markers for
  4814. sampling `window-point' and `window-start'. Together, WRITABLE
  4815. and the variable `window-persistent-parameters' specify which
  4816. window parameters are saved by this function. WRITABLE should be
  4817. non-nil when the return value shall be written to a file and read
  4818. back in another session. Otherwise, an application may run into
  4819. an `invalid-read-syntax' error while attempting to read back the
  4820. value from file.
  4821. The return value can be used as argument for `window-state-put'
  4822. to put the state recorded here into an arbitrary window. The
  4823. value can be also stored on disk and read back in a new session."
  4824. (setq window
  4825. (if window
  4826. (if (window-valid-p window)
  4827. window
  4828. (error "%s is not a live or internal window" window))
  4829. (frame-root-window)))
  4830. ;; The return value is a cons whose car specifies some constraints on
  4831. ;; the size of WINDOW. The cdr lists the states of the child windows
  4832. ;; of WINDOW.
  4833. (cons
  4834. ;; Frame related things would go into a function, say `frame-state',
  4835. ;; calling `window-state-get' to insert the frame's root window.
  4836. `((min-height . ,(window-min-size window))
  4837. (min-width . ,(window-min-size window t))
  4838. (min-height-ignore . ,(window-min-size window nil t))
  4839. (min-width-ignore . ,(window-min-size window t t))
  4840. (min-height-safe . ,(window-min-size window nil 'safe))
  4841. (min-width-safe . ,(window-min-size window t 'safe))
  4842. (min-pixel-height . ,(window-min-size window nil nil t))
  4843. (min-pixel-width . ,(window-min-size window t nil t))
  4844. (min-pixel-height-ignore . ,(window-min-size window nil t t))
  4845. (min-pixel-width-ignore . ,(window-min-size window t t t))
  4846. (min-pixel-height-safe . ,(window-min-size window nil 'safe t))
  4847. (min-pixel-width-safe . ,(window-min-size window t 'safe t)))
  4848. (window--state-get-1 window writable)))
  4849. (defvar window-state-put-list nil
  4850. "Helper variable for `window-state-put'.")
  4851. (defvar window-state-put-stale-windows nil
  4852. "Helper variable for `window-state-put'.")
  4853. (defun window--state-put-1 (state &optional window ignore totals pixelwise)
  4854. "Helper function for `window-state-put'."
  4855. (let ((type (car state)))
  4856. (setq state (cdr state))
  4857. (cond
  4858. ((eq type 'leaf)
  4859. ;; For a leaf window just add unprocessed entries to
  4860. ;; `window-state-put-list'.
  4861. (push (cons window state) window-state-put-list))
  4862. ((memq type '(vc hc))
  4863. (let* ((horizontal (eq type 'hc))
  4864. (total (window-size window horizontal pixelwise))
  4865. (first t)
  4866. size new)
  4867. (dolist (item state)
  4868. ;; Find the next child window. WINDOW always points to the
  4869. ;; real window that we want to fill with what we find here.
  4870. (when (memq (car item) '(leaf vc hc))
  4871. (if (assq 'last item)
  4872. ;; The last child window. Below `window--state-put-1'
  4873. ;; will put into it whatever ITEM has in store.
  4874. (setq new nil)
  4875. ;; Not the last child window, prepare for splitting
  4876. ;; WINDOW. SIZE is the new (and final) size of the old
  4877. ;; window.
  4878. (setq size
  4879. (if totals
  4880. ;; Use total size.
  4881. (if pixelwise
  4882. (cdr (assq (if horizontal
  4883. 'pixel-width
  4884. 'pixel-height)
  4885. item))
  4886. (cdr (assq (if horizontal
  4887. 'total-width
  4888. 'total-height)
  4889. item)))
  4890. ;; Use normalized size and round.
  4891. (round
  4892. (* total
  4893. (cdr (assq (if horizontal 'normal-width 'normal-height)
  4894. item))))))
  4895. ;; Use safe sizes, we try to resize later.
  4896. (setq size (max size
  4897. (if horizontal
  4898. (* window-safe-min-width
  4899. (if pixelwise
  4900. (frame-char-width (window-frame window))
  4901. 1))
  4902. (* window-safe-min-height
  4903. (if pixelwise
  4904. (frame-char-height (window-frame window))
  4905. 1)))))
  4906. (if (window-sizable-p window (- size) horizontal 'safe pixelwise)
  4907. (let* ((window-combination-limit
  4908. (assq 'combination-limit item)))
  4909. ;; We must inherit the combination limit, otherwise
  4910. ;; we might mess up handling of atomic and side
  4911. ;; window.
  4912. (setq new (split-window window size horizontal pixelwise)))
  4913. ;; Give up if we can't resize window down to safe sizes.
  4914. (error "Cannot resize window %s" window))
  4915. (when first
  4916. (setq first nil)
  4917. ;; When creating the first child window add for parent
  4918. ;; unprocessed entries to `window-state-put-list'.
  4919. (setq window-state-put-list
  4920. (cons (cons (window-parent window) state)
  4921. window-state-put-list))))
  4922. ;; Now process the current window (either the one we've just
  4923. ;; split or the last child of its parent).
  4924. (window--state-put-1 item window ignore totals)
  4925. ;; Continue with the last window split off.
  4926. (setq window new))))))))
  4927. (defun window--state-put-2 (ignore pixelwise)
  4928. "Helper function for `window-state-put'."
  4929. (dolist (item window-state-put-list)
  4930. (let ((window (car item))
  4931. (combination-limit (cdr (assq 'combination-limit item)))
  4932. (parameters (cdr (assq 'parameters item)))
  4933. (state (cdr (assq 'buffer item))))
  4934. (when combination-limit
  4935. (set-window-combination-limit window combination-limit))
  4936. ;; Reset window's parameters and assign saved ones (we might want
  4937. ;; a `remove-window-parameters' function here).
  4938. (dolist (parameter (window-parameters window))
  4939. (set-window-parameter window (car parameter) nil))
  4940. (when parameters
  4941. (dolist (parameter parameters)
  4942. (set-window-parameter window (car parameter) (cdr parameter))))
  4943. ;; Process buffer related state.
  4944. (when state
  4945. (let ((buffer (get-buffer (car state))))
  4946. (if buffer
  4947. (with-current-buffer buffer
  4948. (set-window-buffer window buffer)
  4949. (set-window-hscroll window (cdr (assq 'hscroll state)))
  4950. (apply 'set-window-fringes
  4951. (cons window (cdr (assq 'fringes state))))
  4952. (let ((margins (cdr (assq 'margins state))))
  4953. (set-window-margins window (car margins) (cdr margins)))
  4954. (let ((scroll-bars (cdr (assq 'scroll-bars state))))
  4955. (set-window-scroll-bars
  4956. window (car scroll-bars) (nth 2 scroll-bars)
  4957. (nth 3 scroll-bars) (nth 5 scroll-bars)))
  4958. (set-window-vscroll window (cdr (assq 'vscroll state)))
  4959. ;; Adjust vertically.
  4960. (if (memq window-size-fixed '(t height))
  4961. ;; A fixed height window, try to restore the
  4962. ;; original size.
  4963. (let ((delta
  4964. (- (cdr (assq
  4965. (if pixelwise 'pixel-height 'total-height)
  4966. item))
  4967. (window-size window nil pixelwise)))
  4968. window-size-fixed)
  4969. (when (window--resizable-p
  4970. window delta nil nil nil nil nil pixelwise)
  4971. (window-resize window delta nil nil pixelwise)))
  4972. ;; Else check whether the window is not high enough.
  4973. (let* ((min-size
  4974. (window-min-size window nil ignore pixelwise))
  4975. (delta
  4976. (- min-size (window-size window nil pixelwise))))
  4977. (when (and (> delta 0)
  4978. (window--resizable-p
  4979. window delta nil ignore nil nil nil pixelwise))
  4980. (window-resize window delta nil ignore pixelwise))))
  4981. ;; Adjust horizontally.
  4982. (if (memq window-size-fixed '(t width))
  4983. ;; A fixed width window, try to restore the original
  4984. ;; size.
  4985. (let ((delta
  4986. (- (cdr (assq
  4987. (if pixelwise 'pixel-width 'total-width)
  4988. item))
  4989. (window-size window t pixelwise)))
  4990. window-size-fixed)
  4991. (when (window--resizable-p
  4992. window delta nil nil nil nil nil pixelwise)
  4993. (window-resize window delta nil nil pixelwise)))
  4994. ;; Else check whether the window is not wide enough.
  4995. (let* ((min-size (window-min-size window t ignore pixelwise))
  4996. (delta (- min-size (window-size window t pixelwise))))
  4997. (when (and (> delta 0)
  4998. (window--resizable-p
  4999. window delta t ignore nil nil nil pixelwise))
  5000. (window-resize window delta t ignore pixelwise))))
  5001. ;; Set dedicated status.
  5002. (set-window-dedicated-p window (cdr (assq 'dedicated state)))
  5003. ;; Install positions (maybe we should do this after all
  5004. ;; windows have been created and sized).
  5005. (ignore-errors
  5006. (set-window-start window (cdr (assq 'start state)))
  5007. (set-window-point window (cdr (assq 'point state))))
  5008. ;; Select window if it's the selected one.
  5009. (when (cdr (assq 'selected state))
  5010. (select-window window)))
  5011. ;; We don't want to raise an error in case the buffer does
  5012. ;; not exist anymore, so we switch to a previous one and
  5013. ;; save the window with the intention of deleting it later
  5014. ;; if possible.
  5015. (switch-to-prev-buffer window)
  5016. (push window window-state-put-stale-windows)))))))
  5017. (defun window-state-put (state &optional window ignore)
  5018. "Put window state STATE into WINDOW.
  5019. STATE should be the state of a window returned by an earlier
  5020. invocation of `window-state-get'. Optional argument WINDOW must
  5021. specify a valid window and defaults to the selected one. If
  5022. WINDOW is not live, replace WINDOW by a live one before putting
  5023. STATE into it.
  5024. Optional argument IGNORE non-nil means ignore minimum window
  5025. sizes and fixed size restrictions. IGNORE equal `safe' means
  5026. windows can get as small as `window-safe-min-height' and
  5027. `window-safe-min-width'."
  5028. (setq window-state-put-stale-windows nil)
  5029. (setq window (window-normalize-window window))
  5030. ;; When WINDOW is internal, reduce it to a live one to put STATE into,
  5031. ;; see Bug#16793.
  5032. (unless (window-live-p window)
  5033. (let ((root (frame-root-window window)))
  5034. (if (eq window root)
  5035. (setq window (frame-first-window root))
  5036. (setq root window)
  5037. (setq window (catch 'live
  5038. (walk-window-subtree
  5039. (lambda (window)
  5040. (when (window-live-p window)
  5041. (throw 'live window)))
  5042. root))))
  5043. (delete-other-windows-internal window root)))
  5044. (set-window-dedicated-p window nil)
  5045. (let* ((frame (window-frame window))
  5046. (head (car state))
  5047. ;; We check here (1) whether the total sizes of root window of
  5048. ;; STATE and that of WINDOW are equal so we can avoid
  5049. ;; calculating new sizes, and (2) if we do have to resize
  5050. ;; whether we can do so without violating size restrictions.
  5051. (pixelwise (and (cdr (assq 'pixel-width state))
  5052. (cdr (assq 'pixel-height state))))
  5053. (totals (or (and pixelwise
  5054. (= (window-pixel-width window)
  5055. (cdr (assq 'pixel-width state)))
  5056. (= (window-pixel-height window)
  5057. (cdr (assq 'pixel-height state))))
  5058. (and (= (window-total-width window)
  5059. (cdr (assq 'total-width state)))
  5060. (= (window-total-height window)
  5061. (cdr (assq 'total-height state))))))
  5062. (min-height (cdr (assq
  5063. (if pixelwise 'min-pixel-height 'min-height)
  5064. head)))
  5065. (min-width (cdr (assq
  5066. (if pixelwise 'min-pixel-width 'min-weight)
  5067. head))))
  5068. (if (and (not totals)
  5069. (or (> min-height (window-size window nil pixelwise))
  5070. (> min-width (window-size window t pixelwise)))
  5071. (or (not ignore)
  5072. (and (setq min-height
  5073. (cdr (assq
  5074. (if pixelwise
  5075. 'min-pixel-height-ignore
  5076. 'min-height-ignore)
  5077. head)))
  5078. (setq min-width
  5079. (cdr (assq
  5080. (if pixelwise
  5081. 'min-pixel-width-ignore
  5082. 'min-width-ignore)
  5083. head)))
  5084. (or (> min-height
  5085. (window-size window nil pixelwise))
  5086. (> min-width
  5087. (window-size window t pixelwise)))
  5088. (or (not (eq ignore 'safe))
  5089. (and (setq min-height
  5090. (cdr (assq
  5091. (if pixelwise
  5092. 'min-pixel-height-safe
  5093. 'min-height-safe)
  5094. head)))
  5095. (setq min-width
  5096. (cdr (assq
  5097. (if pixelwise
  5098. 'min-pixel-width-safe
  5099. 'min-width-safe)
  5100. head)))
  5101. (or (> min-height
  5102. (window-size window nil pixelwise))
  5103. (> min-width
  5104. (window-size window t pixelwise))))))))
  5105. ;; The check above might not catch all errors due to rounding
  5106. ;; issues - so IGNORE equal 'safe might not always produce the
  5107. ;; minimum possible state. But such configurations hardly make
  5108. ;; sense anyway.
  5109. (error "Window %s too small to accommodate state" window)
  5110. (setq state (cdr state))
  5111. (setq window-state-put-list nil)
  5112. ;; Work on the windows of a temporary buffer to make sure that
  5113. ;; splitting proceeds regardless of any buffer local values of
  5114. ;; `window-size-fixed'. Release that buffer after the buffers of
  5115. ;; all live windows have been set by `window--state-put-2'.
  5116. (with-temp-buffer
  5117. (set-window-buffer window (current-buffer))
  5118. (window--state-put-1 state window nil totals pixelwise)
  5119. (window--state-put-2 ignore pixelwise))
  5120. (while window-state-put-stale-windows
  5121. (let ((window (pop window-state-put-stale-windows)))
  5122. (when (eq (window-deletable-p window) t)
  5123. (delete-window window))))
  5124. (window--check frame))))
  5125. (defun display-buffer-record-window (type window buffer)
  5126. "Record information for window used by `display-buffer'.
  5127. TYPE specifies the type of the calling operation and must be one
  5128. of the symbols `reuse' (when WINDOW existed already and was
  5129. reused for displaying BUFFER), `window' (when WINDOW was created
  5130. on an already existing frame), or `frame' (when WINDOW was
  5131. created on a new frame). WINDOW is the window used for or created
  5132. by the `display-buffer' routines. BUFFER is the buffer that
  5133. shall be displayed.
  5134. This function installs or updates the quit-restore parameter of
  5135. WINDOW. The quit-restore parameter is a list of four elements:
  5136. The first element is one of the symbols `window', `frame', `same' or
  5137. `other'. The second element is either one of the symbols `window'
  5138. or `frame' or a list whose elements are the buffer previously
  5139. shown in the window, that buffer's window start and window point,
  5140. and the window's height. The third element is the window
  5141. selected at the time the parameter was created. The fourth
  5142. element is BUFFER."
  5143. (cond
  5144. ((eq type 'reuse)
  5145. (if (eq (window-buffer window) buffer)
  5146. ;; WINDOW shows BUFFER already. Update WINDOW's quit-restore
  5147. ;; parameter, if any.
  5148. (let ((quit-restore (window-parameter window 'quit-restore)))
  5149. (when (consp quit-restore)
  5150. (setcar quit-restore 'same)
  5151. ;; The selected-window might have changed in
  5152. ;; between (Bug#20353).
  5153. (unless (or (eq window (selected-window))
  5154. (eq window (nth 2 quit-restore)))
  5155. (setcar (cddr quit-restore) (selected-window)))))
  5156. ;; WINDOW shows another buffer.
  5157. (with-current-buffer (window-buffer window)
  5158. (set-window-parameter
  5159. window 'quit-restore
  5160. (list 'other
  5161. ;; A quadruple of WINDOW's buffer, start, point and height.
  5162. (list (current-buffer) (window-start window)
  5163. ;; Preserve window-point-insertion-type (Bug#12588).
  5164. (copy-marker
  5165. (window-point window) window-point-insertion-type)
  5166. (if (window-combined-p window)
  5167. (window-total-height window)
  5168. (window-total-width window)))
  5169. (selected-window) buffer)))))
  5170. ((eq type 'window)
  5171. ;; WINDOW has been created on an existing frame.
  5172. (set-window-parameter
  5173. window 'quit-restore
  5174. (list 'window 'window (selected-window) buffer)))
  5175. ((eq type 'frame)
  5176. ;; WINDOW has been created on a new frame.
  5177. (set-window-parameter
  5178. window 'quit-restore
  5179. (list 'frame 'frame (selected-window) buffer)))))
  5180. (defcustom display-buffer-function nil
  5181. "If non-nil, function to call to handle `display-buffer'.
  5182. It will receive two args, the buffer and a flag which if non-nil
  5183. means that the currently selected window is not acceptable. It
  5184. should choose or create a window, display the specified buffer in
  5185. it, and return the window.
  5186. The specified function should call `display-buffer-record-window'
  5187. with corresponding arguments to set up the quit-restore parameter
  5188. of the window used."
  5189. :type '(choice
  5190. (const nil)
  5191. (function :tag "function"))
  5192. :group 'windows)
  5193. (make-obsolete-variable 'display-buffer-function
  5194. 'display-buffer-alist "24.3")
  5195. ;; Eventually, we want to turn this into a defvar; instead of
  5196. ;; customizing this, the user should use a `pop-up-frame-parameters'
  5197. ;; alist entry in `display-buffer-base-action'.
  5198. (defcustom pop-up-frame-alist nil
  5199. "Alist of parameters for automatically generated new frames.
  5200. If non-nil, the value you specify here is used by the default
  5201. `pop-up-frame-function' for the creation of new frames.
  5202. Since `pop-up-frame-function' is used by `display-buffer' for
  5203. making new frames, any value specified here by default affects
  5204. the automatic generation of new frames via `display-buffer' and
  5205. all functions based on it. The behavior of `make-frame' is not
  5206. affected by this variable."
  5207. :type '(repeat (cons :format "%v"
  5208. (symbol :tag "Parameter")
  5209. (sexp :tag "Value")))
  5210. :group 'frames)
  5211. (defcustom pop-up-frame-function
  5212. (lambda () (make-frame pop-up-frame-alist))
  5213. "Function used by `display-buffer' for creating a new frame.
  5214. This function is called with no arguments and should return a new
  5215. frame. The default value calls `make-frame' with the argument
  5216. `pop-up-frame-alist'."
  5217. :type 'function
  5218. :group 'frames)
  5219. (defcustom special-display-buffer-names nil
  5220. "List of names of buffers that should be displayed specially.
  5221. Displaying a buffer with `display-buffer' or `pop-to-buffer', if
  5222. its name is in this list, displays the buffer in a way specified
  5223. by `special-display-function'. `special-display-popup-frame'
  5224. \(the default for `special-display-function') usually displays
  5225. the buffer in a separate frame made with the parameters specified
  5226. by `special-display-frame-alist'. If `special-display-function'
  5227. has been set to some other function, that function is called with
  5228. the buffer as first, and nil as second argument.
  5229. Alternatively, an element of this list can be specified as
  5230. \(BUFFER-NAME FRAME-PARAMETERS), where BUFFER-NAME is a buffer
  5231. name and FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
  5232. `special-display-popup-frame' will interpret such pairs as frame
  5233. parameters when it creates a special frame, overriding the
  5234. corresponding values from `special-display-frame-alist'.
  5235. As a special case, if FRAME-PARAMETERS contains (same-window . t)
  5236. `special-display-popup-frame' displays that buffer in the
  5237. selected window. If FRAME-PARAMETERS contains (same-frame . t),
  5238. it displays that buffer in a window on the selected frame.
  5239. If `special-display-function' specifies some other function than
  5240. `special-display-popup-frame', that function is called with the
  5241. buffer named BUFFER-NAME as first, and FRAME-PARAMETERS as second
  5242. argument.
  5243. Finally, an element of this list can be also specified as
  5244. \(BUFFER-NAME FUNCTION OTHER-ARGS). In that case,
  5245. `special-display-popup-frame' will call FUNCTION with the buffer
  5246. named BUFFER-NAME as first argument, and OTHER-ARGS as the
  5247. second.
  5248. Any alternative function specified here is responsible for
  5249. setting up the quit-restore parameter of the window used.
  5250. If this variable appears \"not to work\", because you added a
  5251. name to it but the corresponding buffer is displayed in the
  5252. selected window, look at the values of `same-window-buffer-names'
  5253. and `same-window-regexps'. Those variables take precedence over
  5254. this one.
  5255. See also `special-display-regexps'."
  5256. :type '(repeat
  5257. (choice :tag "Buffer"
  5258. :value ""
  5259. (string :format "%v")
  5260. (cons :tag "With parameters"
  5261. :format "%v"
  5262. :value ("" . nil)
  5263. (string :format "%v")
  5264. (repeat :tag "Parameters"
  5265. (cons :format "%v"
  5266. (symbol :tag "Parameter")
  5267. (sexp :tag "Value"))))
  5268. (list :tag "With function"
  5269. :format "%v"
  5270. :value ("" . nil)
  5271. (string :format "%v")
  5272. (function :tag "Function")
  5273. (repeat :tag "Arguments" (sexp)))))
  5274. :group 'windows
  5275. :group 'frames)
  5276. (make-obsolete-variable 'special-display-buffer-names 'display-buffer-alist "24.3")
  5277. (put 'special-display-buffer-names 'risky-local-variable t)
  5278. (defcustom special-display-regexps nil
  5279. "List of regexps saying which buffers should be displayed specially.
  5280. Displaying a buffer with `display-buffer' or `pop-to-buffer', if
  5281. any regexp in this list matches its name, displays it specially
  5282. using `special-display-function'. `special-display-popup-frame'
  5283. \(the default for `special-display-function') usually displays
  5284. the buffer in a separate frame made with the parameters specified
  5285. by `special-display-frame-alist'. If `special-display-function'
  5286. has been set to some other function, that function is called with
  5287. the buffer as first, and nil as second argument.
  5288. Alternatively, an element of this list can be specified as
  5289. \(REGEXP FRAME-PARAMETERS), where REGEXP is a regexp as above and
  5290. FRAME-PARAMETERS an alist of (PARAMETER . VALUE) pairs.
  5291. `special-display-popup-frame' will then interpret these pairs as
  5292. frame parameters when creating a special frame for a buffer whose
  5293. name matches REGEXP, overriding the corresponding values from
  5294. `special-display-frame-alist'.
  5295. As a special case, if FRAME-PARAMETERS contains (same-window . t)
  5296. `special-display-popup-frame' displays buffers matching REGEXP in
  5297. the selected window. (same-frame . t) in FRAME-PARAMETERS means
  5298. to display such buffers in a window on the selected frame.
  5299. If `special-display-function' specifies some other function than
  5300. `special-display-popup-frame', that function is called with the
  5301. buffer whose name matched REGEXP as first, and FRAME-PARAMETERS
  5302. as second argument.
  5303. Finally, an element of this list can be also specified as
  5304. \(REGEXP FUNCTION OTHER-ARGS). `special-display-popup-frame'
  5305. will then call FUNCTION with the buffer whose name matched
  5306. REGEXP as first, and OTHER-ARGS as second argument.
  5307. Any alternative function specified here is responsible for
  5308. setting up the quit-restore parameter of the window used.
  5309. If this variable appears \"not to work\", because you added a
  5310. name to it but the corresponding buffer is displayed in the
  5311. selected window, look at the values of `same-window-buffer-names'
  5312. and `same-window-regexps'. Those variables take precedence over
  5313. this one.
  5314. See also `special-display-buffer-names'."
  5315. :type '(repeat
  5316. (choice :tag "Buffer"
  5317. :value ""
  5318. (regexp :format "%v")
  5319. (cons :tag "With parameters"
  5320. :format "%v"
  5321. :value ("" . nil)
  5322. (regexp :format "%v")
  5323. (repeat :tag "Parameters"
  5324. (cons :format "%v"
  5325. (symbol :tag "Parameter")
  5326. (sexp :tag "Value"))))
  5327. (list :tag "With function"
  5328. :format "%v"
  5329. :value ("" . nil)
  5330. (regexp :format "%v")
  5331. (function :tag "Function")
  5332. (repeat :tag "Arguments" (sexp)))))
  5333. :group 'windows
  5334. :group 'frames)
  5335. (make-obsolete-variable 'special-display-regexps 'display-buffer-alist "24.3")
  5336. (put 'special-display-regexps 'risky-local-variable t)
  5337. (defun special-display-p (buffer-name)
  5338. "Return non-nil if a buffer named BUFFER-NAME gets a special frame.
  5339. More precisely, return t if `special-display-buffer-names' or
  5340. `special-display-regexps' contain a string entry equaling or
  5341. matching BUFFER-NAME. If `special-display-buffer-names' or
  5342. `special-display-regexps' contain a list entry whose car equals
  5343. or matches BUFFER-NAME, the return value is the cdr of that
  5344. entry."
  5345. (let (tmp)
  5346. (cond
  5347. ((member buffer-name special-display-buffer-names)
  5348. t)
  5349. ((setq tmp (assoc buffer-name special-display-buffer-names))
  5350. (cdr tmp))
  5351. ((catch 'found
  5352. (dolist (regexp special-display-regexps)
  5353. (cond
  5354. ((stringp regexp)
  5355. (when (string-match-p regexp buffer-name)
  5356. (throw 'found t)))
  5357. ((and (consp regexp) (stringp (car regexp))
  5358. (string-match-p (car regexp) buffer-name))
  5359. (throw 'found (cdr regexp))))))))))
  5360. (defcustom special-display-frame-alist
  5361. '((height . 14) (width . 80) (unsplittable . t))
  5362. "Alist of parameters for special frames.
  5363. Special frames are used for buffers whose names are listed in
  5364. `special-display-buffer-names' and for buffers whose names match
  5365. one of the regular expressions in `special-display-regexps'.
  5366. This variable can be set in your init file, like this:
  5367. (setq special-display-frame-alist '((width . 80) (height . 20)))
  5368. These supersede the values given in `default-frame-alist'."
  5369. :type '(repeat (cons :format "%v"
  5370. (symbol :tag "Parameter")
  5371. (sexp :tag "Value")))
  5372. :group 'frames)
  5373. (make-obsolete-variable 'special-display-frame-alist 'display-buffer-alist "24.3")
  5374. (defun special-display-popup-frame (buffer &optional args)
  5375. "Pop up a frame displaying BUFFER and return its window.
  5376. If BUFFER is already displayed in a visible or iconified frame,
  5377. raise that frame. Otherwise, display BUFFER in a new frame.
  5378. Optional argument ARGS is a list specifying additional
  5379. information.
  5380. If ARGS is an alist, use it as a list of frame parameters. If
  5381. these parameters contain (same-window . t), display BUFFER in
  5382. the selected window. If they contain (same-frame . t), display
  5383. BUFFER in a window of the selected frame.
  5384. If ARGS is a list whose car is a symbol, use (car ARGS) as a
  5385. function to do the work. Pass it BUFFER as first argument, and
  5386. pass the elements of (cdr ARGS) as the remaining arguments."
  5387. (if (and args (symbolp (car args)))
  5388. (apply (car args) buffer (cdr args))
  5389. (let ((window (get-buffer-window buffer 0)))
  5390. (or
  5391. ;; If we have a window already, make it visible.
  5392. (when window
  5393. (let ((frame (window-frame window)))
  5394. (make-frame-visible frame)
  5395. (raise-frame frame)
  5396. (display-buffer-record-window 'reuse window buffer)
  5397. window))
  5398. ;; Reuse the current window if the user requested it.
  5399. (when (cdr (assq 'same-window args))
  5400. (condition-case nil
  5401. (progn (switch-to-buffer buffer nil t) (selected-window))
  5402. (error nil)))
  5403. ;; Stay on the same frame if requested.
  5404. (when (or (cdr (assq 'same-frame args)) (cdr (assq 'same-window args)))
  5405. (let* ((pop-up-windows t)
  5406. pop-up-frames
  5407. special-display-buffer-names special-display-regexps)
  5408. (display-buffer buffer)))
  5409. ;; If no window yet, make one in a new frame.
  5410. (let* ((frame
  5411. (with-current-buffer buffer
  5412. (make-frame (append args special-display-frame-alist))))
  5413. (window (frame-selected-window frame)))
  5414. (display-buffer-record-window 'frame window buffer)
  5415. (unless (eq buffer (window-buffer window))
  5416. (set-window-buffer window buffer)
  5417. (set-window-prev-buffers window nil))
  5418. (set-window-dedicated-p window t)
  5419. window)))))
  5420. (defcustom special-display-function 'special-display-popup-frame
  5421. "Function to call for displaying special buffers.
  5422. This function is called with two arguments - the buffer and,
  5423. optionally, a list - and should return a window displaying that
  5424. buffer. The default value usually makes a separate frame for the
  5425. buffer using `special-display-frame-alist' to specify the frame
  5426. parameters. See the definition of `special-display-popup-frame'
  5427. for how to specify such a function.
  5428. A buffer is special when its name is either listed in
  5429. `special-display-buffer-names' or matches a regexp in
  5430. `special-display-regexps'.
  5431. The specified function should call `display-buffer-record-window'
  5432. with corresponding arguments to set up the quit-restore parameter
  5433. of the window used."
  5434. :type 'function
  5435. :group 'frames)
  5436. (make-obsolete-variable 'special-display-function 'display-buffer-alist "24.3")
  5437. (defcustom same-window-buffer-names nil
  5438. "List of names of buffers that should appear in the \"same\" window.
  5439. `display-buffer' and `pop-to-buffer' show a buffer whose name is
  5440. on this list in the selected rather than some other window.
  5441. An element of this list can be a cons cell instead of just a
  5442. string. In that case, the cell's car must be a string specifying
  5443. the buffer name. This is for compatibility with
  5444. `special-display-buffer-names'; the cdr of the cons cell is
  5445. ignored.
  5446. See also `same-window-regexps'."
  5447. :type '(repeat (string :format "%v"))
  5448. :group 'windows)
  5449. (defcustom same-window-regexps nil
  5450. "List of regexps saying which buffers should appear in the \"same\" window.
  5451. `display-buffer' and `pop-to-buffer' show a buffer whose name
  5452. matches a regexp on this list in the selected rather than some
  5453. other window.
  5454. An element of this list can be a cons cell instead of just a
  5455. string. In that case, the cell's car must be a regexp matching
  5456. the buffer name. This is for compatibility with
  5457. `special-display-regexps'; the cdr of the cons cell is ignored.
  5458. See also `same-window-buffer-names'."
  5459. :type '(repeat (regexp :format "%v"))
  5460. :group 'windows)
  5461. (defun same-window-p (buffer-name)
  5462. "Return non-nil if a buffer named BUFFER-NAME would be shown in the \"same\" window.
  5463. This function returns non-nil if `display-buffer' or
  5464. `pop-to-buffer' would show a buffer named BUFFER-NAME in the
  5465. selected rather than (as usual) some other window. See
  5466. `same-window-buffer-names' and `same-window-regexps'."
  5467. (cond
  5468. ((not (stringp buffer-name)))
  5469. ;; The elements of `same-window-buffer-names' can be buffer
  5470. ;; names or cons cells whose cars are buffer names.
  5471. ((member buffer-name same-window-buffer-names))
  5472. ((assoc buffer-name same-window-buffer-names))
  5473. ((catch 'found
  5474. (dolist (regexp same-window-regexps)
  5475. ;; The elements of `same-window-regexps' can be regexps
  5476. ;; or cons cells whose cars are regexps.
  5477. (when (or (and (stringp regexp)
  5478. (string-match-p regexp buffer-name))
  5479. (and (consp regexp) (stringp (car regexp))
  5480. (string-match-p (car regexp) buffer-name)))
  5481. (throw 'found t)))))))
  5482. (defcustom pop-up-frames nil
  5483. "Whether `display-buffer' should make a separate frame.
  5484. If nil, never make a separate frame.
  5485. If the value is `graphic-only', make a separate frame
  5486. on graphic displays only.
  5487. Any other non-nil value means always make a separate frame."
  5488. :type '(choice
  5489. (const :tag "Never" nil)
  5490. (const :tag "On graphic displays only" graphic-only)
  5491. (const :tag "Always" t))
  5492. :group 'windows)
  5493. (defcustom display-buffer-reuse-frames nil
  5494. "Non-nil means `display-buffer' should reuse frames.
  5495. If the buffer in question is already displayed in a frame, raise
  5496. that frame."
  5497. :type 'boolean
  5498. :version "21.1"
  5499. :group 'windows)
  5500. (make-obsolete-variable
  5501. 'display-buffer-reuse-frames
  5502. "use a `reusable-frames' alist entry in `display-buffer-alist'."
  5503. "24.3")
  5504. (defcustom pop-up-windows t
  5505. "Non-nil means `display-buffer' should make a new window."
  5506. :type 'boolean
  5507. :group 'windows)
  5508. (defcustom split-window-preferred-function 'split-window-sensibly
  5509. "Function called by `display-buffer' routines to split a window.
  5510. This function is called with a window as single argument and is
  5511. supposed to split that window and return the new window. If the
  5512. window can (or shall) not be split, it is supposed to return nil.
  5513. The default is to call the function `split-window-sensibly' which
  5514. tries to split the window in a way which seems most suitable.
  5515. You can customize the options `split-height-threshold' and/or
  5516. `split-width-threshold' in order to have `split-window-sensibly'
  5517. prefer either vertical or horizontal splitting.
  5518. If you set this to any other function, bear in mind that the
  5519. `display-buffer' routines may call this function two times. The
  5520. argument of the first call is the largest window on its frame.
  5521. If that call fails to return a live window, the function is
  5522. called again with the least recently used window as argument. If
  5523. that call fails too, `display-buffer' will use an existing window
  5524. to display its buffer.
  5525. The window selected at the time `display-buffer' was invoked is
  5526. still selected when this function is called. Hence you can
  5527. compare the window argument with the value of `selected-window'
  5528. if you intend to split the selected window instead or if you do
  5529. not want to split the selected window."
  5530. :type 'function
  5531. :version "23.1"
  5532. :group 'windows)
  5533. (defcustom split-height-threshold 80
  5534. "Minimum height for splitting windows sensibly.
  5535. If this is an integer, `split-window-sensibly' may split a window
  5536. vertically only if it has at least this many lines. If this is
  5537. nil, `split-window-sensibly' is not allowed to split a window
  5538. vertically. If, however, a window is the only window on its
  5539. frame, `split-window-sensibly' may split it vertically
  5540. disregarding the value of this variable."
  5541. :type '(choice (const nil) (integer :tag "lines"))
  5542. :version "23.1"
  5543. :group 'windows)
  5544. (defcustom split-width-threshold 160
  5545. "Minimum width for splitting windows sensibly.
  5546. If this is an integer, `split-window-sensibly' may split a window
  5547. horizontally only if it has at least this many columns. If this
  5548. is nil, `split-window-sensibly' is not allowed to split a window
  5549. horizontally."
  5550. :type '(choice (const nil) (integer :tag "columns"))
  5551. :version "23.1"
  5552. :group 'windows)
  5553. (defun window-splittable-p (window &optional horizontal)
  5554. "Return non-nil if `split-window-sensibly' may split WINDOW.
  5555. Optional argument HORIZONTAL nil or omitted means check whether
  5556. `split-window-sensibly' may split WINDOW vertically. HORIZONTAL
  5557. non-nil means check whether WINDOW may be split horizontally.
  5558. WINDOW may be split vertically when the following conditions
  5559. hold:
  5560. - `window-size-fixed' is either nil or equals `width' for the
  5561. buffer of WINDOW.
  5562. - `split-height-threshold' is an integer and WINDOW is at least as
  5563. high as `split-height-threshold'.
  5564. - When WINDOW is split evenly, the emanating windows are at least
  5565. `window-min-height' lines tall and can accommodate at least one
  5566. line plus - if WINDOW has one - a mode line.
  5567. WINDOW may be split horizontally when the following conditions
  5568. hold:
  5569. - `window-size-fixed' is either nil or equals `height' for the
  5570. buffer of WINDOW.
  5571. - `split-width-threshold' is an integer and WINDOW is at least as
  5572. wide as `split-width-threshold'.
  5573. - When WINDOW is split evenly, the emanating windows are at least
  5574. `window-min-width' or two (whichever is larger) columns wide."
  5575. (when (and (window-live-p window) (not (window--side-window-p window)))
  5576. (with-current-buffer (window-buffer window)
  5577. (if horizontal
  5578. ;; A window can be split horizontally when its width is not
  5579. ;; fixed, it is at least `split-width-threshold' columns wide
  5580. ;; and at least twice as wide as `window-min-width' and 2 (the
  5581. ;; latter value is hardcoded).
  5582. (and (memq window-size-fixed '(nil height))
  5583. ;; Testing `window-full-width-p' here hardly makes any
  5584. ;; sense nowadays. This can be done more intuitively by
  5585. ;; setting up `split-width-threshold' appropriately.
  5586. (numberp split-width-threshold)
  5587. (>= (window-width window)
  5588. (max split-width-threshold
  5589. (* 2 (max window-min-width 2)))))
  5590. ;; A window can be split vertically when its height is not
  5591. ;; fixed, it is at least `split-height-threshold' lines high,
  5592. ;; and it is at least twice as high as `window-min-height' and 2
  5593. ;; if it has a mode line or 1.
  5594. (and (memq window-size-fixed '(nil width))
  5595. (numberp split-height-threshold)
  5596. (>= (window-height window)
  5597. (max split-height-threshold
  5598. (* 2 (max window-min-height
  5599. (if mode-line-format 2 1))))))))))
  5600. (defun split-window-sensibly (&optional window)
  5601. "Split WINDOW in a way suitable for `display-buffer'.
  5602. WINDOW defaults to the currently selected window.
  5603. If `split-height-threshold' specifies an integer, WINDOW is at
  5604. least `split-height-threshold' lines tall and can be split
  5605. vertically, split WINDOW into two windows one above the other and
  5606. return the lower window. Otherwise, if `split-width-threshold'
  5607. specifies an integer, WINDOW is at least `split-width-threshold'
  5608. columns wide and can be split horizontally, split WINDOW into two
  5609. windows side by side and return the window on the right. If this
  5610. can't be done either and WINDOW is the only window on its frame,
  5611. try to split WINDOW vertically disregarding any value specified
  5612. by `split-height-threshold'. If that succeeds, return the lower
  5613. window. Return nil otherwise.
  5614. By default `display-buffer' routines call this function to split
  5615. the largest or least recently used window. To change the default
  5616. customize the option `split-window-preferred-function'.
  5617. You can enforce this function to not split WINDOW horizontally,
  5618. by setting (or binding) the variable `split-width-threshold' to
  5619. nil. If, in addition, you set `split-height-threshold' to zero,
  5620. chances increase that this function does split WINDOW vertically.
  5621. In order to not split WINDOW vertically, set (or bind) the
  5622. variable `split-height-threshold' to nil. Additionally, you can
  5623. set `split-width-threshold' to zero to make a horizontal split
  5624. more likely to occur.
  5625. Have a look at the function `window-splittable-p' if you want to
  5626. know how `split-window-sensibly' determines whether WINDOW can be
  5627. split."
  5628. (let ((window (or window (selected-window))))
  5629. (or (and (window-splittable-p window)
  5630. ;; Split window vertically.
  5631. (with-selected-window window
  5632. (split-window-below)))
  5633. (and (window-splittable-p window t)
  5634. ;; Split window horizontally.
  5635. (with-selected-window window
  5636. (split-window-right)))
  5637. (and (eq window (frame-root-window (window-frame window)))
  5638. (not (window-minibuffer-p window))
  5639. ;; If WINDOW is the only window on its frame and is not the
  5640. ;; minibuffer window, try to split it vertically disregarding
  5641. ;; the value of `split-height-threshold'.
  5642. (let ((split-height-threshold 0))
  5643. (when (window-splittable-p window)
  5644. (with-selected-window window
  5645. (split-window-below))))))))
  5646. (defun window--try-to-split-window (window &optional alist)
  5647. "Try to split WINDOW.
  5648. Return value returned by `split-window-preferred-function' if it
  5649. represents a live window, nil otherwise."
  5650. (and (window-live-p window)
  5651. (not (frame-parameter (window-frame window) 'unsplittable))
  5652. (let* ((window-combination-limit
  5653. ;; When `window-combination-limit' equals
  5654. ;; `display-buffer' or equals `resize-window' and a
  5655. ;; `window-height' or `window-width' alist entry are
  5656. ;; present, bind it to t so resizing steals space
  5657. ;; preferably from the window that was split.
  5658. (if (or (eq window-combination-limit 'display-buffer)
  5659. (and (eq window-combination-limit 'window-size)
  5660. (or (cdr (assq 'window-height alist))
  5661. (cdr (assq 'window-width alist)))))
  5662. t
  5663. window-combination-limit))
  5664. (new-window
  5665. ;; Since `split-window-preferred-function' might
  5666. ;; throw an error use `condition-case'.
  5667. (condition-case nil
  5668. (funcall split-window-preferred-function window)
  5669. (error nil))))
  5670. (and (window-live-p new-window) new-window))))
  5671. (defun window--frame-usable-p (frame)
  5672. "Return FRAME if it can be used to display a buffer."
  5673. (when (frame-live-p frame)
  5674. (let ((window (frame-root-window frame)))
  5675. ;; `frame-root-window' may be an internal window which is considered
  5676. ;; "dead" by `window-live-p'. Hence if `window' is not live we
  5677. ;; implicitly know that `frame' has a visible window we can use.
  5678. (unless (and (window-live-p window)
  5679. (or (window-minibuffer-p window)
  5680. ;; If the window is soft-dedicated, the frame is usable.
  5681. ;; Actually, even if the window is really dedicated,
  5682. ;; the frame is still usable by splitting it.
  5683. ;; At least Emacs-22 allowed it, and it is desirable
  5684. ;; when displaying same-frame windows.
  5685. nil ; (eq t (window-dedicated-p window))
  5686. ))
  5687. frame))))
  5688. (defcustom even-window-sizes t
  5689. "If non-nil `display-buffer' will try to even window sizes.
  5690. Otherwise `display-buffer' will leave the window configuration
  5691. alone. Special values are `height-only' to even heights only and
  5692. `width-only' to even widths only. Any other value means to even
  5693. any of them."
  5694. :type '(choice
  5695. (const :tag "Never" nil)
  5696. (const :tag "Side-by-side windows only" width-only)
  5697. (const :tag "Windows above or below only" height-only)
  5698. (const :tag "Always" t))
  5699. :version "25.1"
  5700. :group 'windows)
  5701. (defvaralias 'even-window-heights 'even-window-sizes)
  5702. (defun window--even-window-sizes (window)
  5703. "Even sizes of WINDOW and selected window.
  5704. Even only if these windows are the only children of their parent,
  5705. `even-window-sizes' has the appropriate value and the selected
  5706. window is larger than WINDOW."
  5707. (when (and (= (window-child-count (window-parent window)) 2)
  5708. (eq (window-parent) (window-parent window)))
  5709. (cond
  5710. ((and (not (memq even-window-sizes '(nil height-only)))
  5711. (window-combined-p window t)
  5712. (> (window-total-width) (window-total-width window)))
  5713. (condition-case nil
  5714. (enlarge-window
  5715. (/ (- (window-total-width window) (window-total-width)) 2) t)
  5716. (error nil)))
  5717. ((and (not (memq even-window-sizes '(nil width-only)))
  5718. (window-combined-p window)
  5719. (> (window-total-height) (window-total-height window)))
  5720. (condition-case nil
  5721. (enlarge-window
  5722. (/ (- (window-total-height window) (window-total-height)) 2))
  5723. (error nil))))))
  5724. (defun window--display-buffer (buffer window type &optional alist dedicated)
  5725. "Display BUFFER in WINDOW.
  5726. TYPE must be one of the symbols `reuse', `window' or `frame' and
  5727. is passed unaltered to `display-buffer-record-window'. ALIST is
  5728. the alist argument of `display-buffer'. Set `window-dedicated-p'
  5729. to DEDICATED if non-nil. Return WINDOW if BUFFER and WINDOW are
  5730. live."
  5731. (when (and (buffer-live-p buffer) (window-live-p window))
  5732. (display-buffer-record-window type window buffer)
  5733. (unless (eq buffer (window-buffer window))
  5734. (set-window-dedicated-p window nil)
  5735. (set-window-buffer window buffer)
  5736. (when dedicated
  5737. (set-window-dedicated-p window dedicated))
  5738. (when (memq type '(window frame))
  5739. (set-window-prev-buffers window nil)))
  5740. (let ((parameter (window-parameter window 'quit-restore))
  5741. (height (cdr (assq 'window-height alist)))
  5742. (width (cdr (assq 'window-width alist)))
  5743. (size (cdr (assq 'window-size alist)))
  5744. (preserve-size (cdr (assq 'preserve-size alist))))
  5745. (cond
  5746. ((or (eq type 'frame)
  5747. (and (eq (car parameter) 'same)
  5748. (eq (nth 1 parameter) 'frame)))
  5749. ;; Adjust size of frame if asked for.
  5750. (cond
  5751. ((not size))
  5752. ((consp size)
  5753. (let ((width (car size))
  5754. (height (cdr size))
  5755. (frame (window-frame window)))
  5756. (when (and (numberp width) (numberp height))
  5757. (set-frame-height
  5758. frame (+ (frame-height frame)
  5759. (- height (window-total-height window))))
  5760. (set-frame-width
  5761. frame (+ (frame-width frame)
  5762. (- width (window-total-width window)))))))
  5763. ((functionp size)
  5764. (ignore-errors (funcall size window)))))
  5765. ((or (eq type 'window)
  5766. (and (eq (car parameter) 'same)
  5767. (eq (nth 1 parameter) 'window)))
  5768. ;; Adjust height of window if asked for.
  5769. (cond
  5770. ((not height))
  5771. ((numberp height)
  5772. (let* ((new-height
  5773. (if (integerp height)
  5774. height
  5775. (round
  5776. (* (window-total-height (frame-root-window window))
  5777. height))))
  5778. (delta (- new-height (window-total-height window))))
  5779. (when (and (window--resizable-p window delta nil 'safe)
  5780. (window-combined-p window))
  5781. (window-resize window delta nil 'safe))))
  5782. ((functionp height)
  5783. (ignore-errors (funcall height window))))
  5784. ;; Adjust width of window if asked for.
  5785. (cond
  5786. ((not width))
  5787. ((numberp width)
  5788. (let* ((new-width
  5789. (if (integerp width)
  5790. width
  5791. (round
  5792. (* (window-total-width (frame-root-window window))
  5793. width))))
  5794. (delta (- new-width (window-total-width window))))
  5795. (when (and (window--resizable-p window delta t 'safe)
  5796. (window-combined-p window t))
  5797. (window-resize window delta t 'safe))))
  5798. ((functionp width)
  5799. (ignore-errors (funcall width window))))
  5800. ;; Preserve window size if asked for.
  5801. (when (consp preserve-size)
  5802. (window-preserve-size window t (car preserve-size))
  5803. (window-preserve-size window nil (cdr preserve-size))))))
  5804. window))
  5805. (defun window--maybe-raise-frame (frame)
  5806. (let ((visible (frame-visible-p frame)))
  5807. (unless (or (not visible)
  5808. ;; Assume the selected frame is already visible enough.
  5809. (eq frame (selected-frame))
  5810. ;; Assume the frame from which we invoked the
  5811. ;; minibuffer is visible.
  5812. (and (minibuffer-window-active-p (selected-window))
  5813. (eq frame (window-frame (minibuffer-selected-window)))))
  5814. (raise-frame frame))))
  5815. ;; FIXME: Not implemented.
  5816. ;; FIXME: By the way, there could be more levels of dedication:
  5817. ;; - `barely' dedicated doesn't prevent reuse of the window, only records that
  5818. ;; the window hasn't been used for something else yet.
  5819. ;; - `soft' (`softly') dedicated only allows reuse when asked explicitly.
  5820. ;; - `strongly' never allows reuse.
  5821. (defvar display-buffer-mark-dedicated nil
  5822. "If non-nil, `display-buffer' marks the windows it creates as dedicated.
  5823. The actual non-nil value of this variable will be copied to the
  5824. `window-dedicated-p' flag.")
  5825. (defconst display-buffer--action-function-custom-type
  5826. '(choice :tag "Function"
  5827. (const :tag "--" ignore) ; default for insertion
  5828. (const display-buffer-reuse-window)
  5829. (const display-buffer-pop-up-window)
  5830. (const display-buffer-same-window)
  5831. (const display-buffer-pop-up-frame)
  5832. (const display-buffer-below-selected)
  5833. (const display-buffer-at-bottom)
  5834. (const display-buffer-in-previous-window)
  5835. (const display-buffer-use-some-window)
  5836. (const display-buffer-use-some-frame)
  5837. (function :tag "Other function"))
  5838. "Custom type for `display-buffer' action functions.")
  5839. (defconst display-buffer--action-custom-type
  5840. `(cons :tag "Action"
  5841. (choice :tag "Action functions"
  5842. ,display-buffer--action-function-custom-type
  5843. (repeat
  5844. :tag "List of functions"
  5845. ,display-buffer--action-function-custom-type))
  5846. (alist :tag "Action arguments"
  5847. :key-type symbol
  5848. :value-type (sexp :tag "Value")))
  5849. "Custom type for `display-buffer' actions.")
  5850. (defvar display-buffer-overriding-action '(nil . nil)
  5851. "Overriding action to perform to display a buffer.
  5852. It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
  5853. function or a list of functions. Each function should accept two
  5854. arguments: a buffer to display and an alist similar to ALIST.
  5855. See `display-buffer' for details.")
  5856. (put 'display-buffer-overriding-action 'risky-local-variable t)
  5857. (defcustom display-buffer-alist nil
  5858. "Alist of conditional actions for `display-buffer'.
  5859. This is a list of elements (CONDITION . ACTION), where:
  5860. CONDITION is either a regexp matching buffer names, or a
  5861. function that takes two arguments - a buffer name and the
  5862. ACTION argument of `display-buffer' - and returns a boolean.
  5863. ACTION is a cons cell (FUNCTION . ALIST), where FUNCTION is a
  5864. function or a list of functions. Each such function should
  5865. accept two arguments: a buffer to display and an alist of the
  5866. same form as ALIST. See `display-buffer' for details.
  5867. `display-buffer' scans this alist until it either finds a
  5868. matching regular expression or the function specified by a
  5869. condition returns non-nil. In any of these cases, it adds the
  5870. associated action to the list of actions it will try."
  5871. :type `(alist :key-type
  5872. (choice :tag "Condition"
  5873. regexp
  5874. (function :tag "Matcher function"))
  5875. :value-type ,display-buffer--action-custom-type)
  5876. :risky t
  5877. :version "24.1"
  5878. :group 'windows)
  5879. (defcustom display-buffer-base-action '(nil . nil)
  5880. "User-specified default action for `display-buffer'.
  5881. It should be a cons cell (FUNCTION . ALIST), where FUNCTION is a
  5882. function or a list of functions. Each function should accept two
  5883. arguments: a buffer to display and an alist similar to ALIST.
  5884. See `display-buffer' for details."
  5885. :type display-buffer--action-custom-type
  5886. :risky t
  5887. :version "24.1"
  5888. :group 'windows)
  5889. (defconst display-buffer-fallback-action
  5890. '((display-buffer--maybe-same-window ;FIXME: why isn't this redundant?
  5891. display-buffer-reuse-window
  5892. display-buffer--maybe-pop-up-frame-or-window
  5893. display-buffer-in-previous-window
  5894. display-buffer-use-some-window
  5895. ;; If all else fails, pop up a new frame.
  5896. display-buffer-pop-up-frame))
  5897. "Default fallback action for `display-buffer'.
  5898. This is the action used by `display-buffer' if no other actions
  5899. specified, e.g. by the user options `display-buffer-alist' or
  5900. `display-buffer-base-action'. See `display-buffer'.")
  5901. (put 'display-buffer-fallback-action 'risky-local-variable t)
  5902. (defun display-buffer-assq-regexp (buffer-name alist action)
  5903. "Retrieve ALIST entry corresponding to BUFFER-NAME.
  5904. ACTION is the action argument passed to `display-buffer'."
  5905. (catch 'match
  5906. (dolist (entry alist)
  5907. (let ((key (car entry)))
  5908. (when (or (and (stringp key)
  5909. (string-match-p key buffer-name))
  5910. (and (functionp key)
  5911. (funcall key buffer-name action)))
  5912. (throw 'match (cdr entry)))))))
  5913. (defvar display-buffer--same-window-action
  5914. '(display-buffer-same-window
  5915. (inhibit-same-window . nil))
  5916. "A `display-buffer' action for displaying in the same window.")
  5917. (put 'display-buffer--same-window-action 'risky-local-variable t)
  5918. (defvar display-buffer--other-frame-action
  5919. '((display-buffer-reuse-window
  5920. display-buffer-pop-up-frame)
  5921. (reusable-frames . 0)
  5922. (inhibit-same-window . t))
  5923. "A `display-buffer' action for displaying in another frame.")
  5924. (put 'display-buffer--other-frame-action 'risky-local-variable t)
  5925. (defun display-buffer (buffer-or-name &optional action frame)
  5926. "Display BUFFER-OR-NAME in some window, without selecting it.
  5927. BUFFER-OR-NAME must be a buffer or the name of an existing
  5928. buffer. Return the window chosen for displaying BUFFER-OR-NAME,
  5929. or nil if no such window is found.
  5930. Optional argument ACTION, if non-nil, should specify a display
  5931. action. Its form is described below.
  5932. Optional argument FRAME, if non-nil, acts like an additional
  5933. ALIST entry (reusable-frames . FRAME) to the action list of ACTION,
  5934. specifying the frame(s) to search for a window that is already
  5935. displaying the buffer. See `display-buffer-reuse-window'
  5936. If ACTION is non-nil, it should have the form (FUNCTION . ALIST),
  5937. where FUNCTION is either a function or a list of functions, and
  5938. ALIST is an arbitrary association list (alist).
  5939. Each such FUNCTION should accept two arguments: the buffer to
  5940. display and an alist. Based on those arguments, it should
  5941. display the buffer and return the window. If the caller is
  5942. prepared to handle the case of not displaying the buffer
  5943. and returning nil from `display-buffer' it should pass
  5944. \(allow-no-window . t) as an element of the ALIST.
  5945. The `display-buffer' function builds a function list and an alist
  5946. by combining the functions and alists specified in
  5947. `display-buffer-overriding-action', `display-buffer-alist', the
  5948. ACTION argument, `display-buffer-base-action', and
  5949. `display-buffer-fallback-action' (in order). Then it calls each
  5950. function in the combined function list in turn, passing the
  5951. buffer as the first argument and the combined alist as the second
  5952. argument, until one of the functions returns non-nil.
  5953. If ACTION is nil, the function list and the alist are built using
  5954. only the other variables mentioned above.
  5955. Available action functions include:
  5956. `display-buffer-same-window'
  5957. `display-buffer-reuse-window'
  5958. `display-buffer-pop-up-frame'
  5959. `display-buffer-pop-up-window'
  5960. `display-buffer-in-previous-window'
  5961. `display-buffer-use-some-window'
  5962. `display-buffer-use-some-frame'
  5963. Recognized alist entries include:
  5964. `inhibit-same-window' -- A non-nil value prevents the same
  5965. window from being used for display.
  5966. `inhibit-switch-frame' -- A non-nil value prevents any other
  5967. frame from being raised or selected,
  5968. even if the window is displayed there.
  5969. `reusable-frames' -- Value specifies frame(s) to search for a
  5970. window that already displays the buffer.
  5971. See `display-buffer-reuse-window'.
  5972. `pop-up-frame-parameters' -- Value specifies an alist of frame
  5973. parameters to give a new frame, if
  5974. one is created.
  5975. `window-height' -- Value specifies either an integer (the number
  5976. of lines of a new window), a floating point number (the
  5977. fraction of a new window with respect to the height of the
  5978. frame's root window) or a function to be called with one
  5979. argument - a new window. The function is supposed to adjust
  5980. the height of the window; its return value is ignored.
  5981. Suitable functions are `shrink-window-if-larger-than-buffer'
  5982. and `fit-window-to-buffer'.
  5983. `window-width' -- Value specifies either an integer (the number
  5984. of columns of a new window), a floating point number (the
  5985. fraction of a new window with respect to the width of the
  5986. frame's root window) or a function to be called with one
  5987. argument - a new window. The function is supposed to adjust
  5988. the width of the window; its return value is ignored.
  5989. `allow-no-window' -- A non-nil value indicates readiness for the case
  5990. of not displaying the buffer and FUNCTION can safely return
  5991. a non-window value to suppress displaying.
  5992. `preserve-size' -- Value should be either '(t . nil)' to
  5993. preserve the width of the window, '(nil . t)' to preserve its
  5994. height or '(t . t)' to preserve both.
  5995. The ACTION argument to `display-buffer' can also have a non-nil
  5996. and non-list value. This means to display the buffer in a window
  5997. other than the selected one, even if it is already displayed in
  5998. the selected window. If called interactively with a prefix
  5999. argument, ACTION is t."
  6000. (interactive (list (read-buffer "Display buffer: " (other-buffer))
  6001. (if current-prefix-arg t)))
  6002. (let ((buffer (if (bufferp buffer-or-name)
  6003. buffer-or-name
  6004. (get-buffer buffer-or-name)))
  6005. ;; Make sure that when we split windows the old window keeps
  6006. ;; point, bug#14829.
  6007. (split-window-keep-point t)
  6008. ;; Handle the old form of the first argument.
  6009. (inhibit-same-window (and action (not (listp action)))))
  6010. (unless (listp action) (setq action nil))
  6011. (if display-buffer-function
  6012. ;; If `display-buffer-function' is defined, let it do the job.
  6013. (funcall display-buffer-function buffer inhibit-same-window)
  6014. ;; Otherwise, use the defined actions.
  6015. (let* ((user-action
  6016. (display-buffer-assq-regexp
  6017. (buffer-name buffer) display-buffer-alist action))
  6018. (special-action (display-buffer--special-action buffer))
  6019. ;; Extra actions from the arguments to this function:
  6020. (extra-action
  6021. (cons nil (append (if inhibit-same-window
  6022. '((inhibit-same-window . t)))
  6023. (if frame
  6024. `((reusable-frames . ,frame))))))
  6025. ;; Construct action function list and action alist.
  6026. (actions (list display-buffer-overriding-action
  6027. user-action special-action action extra-action
  6028. display-buffer-base-action
  6029. display-buffer-fallback-action))
  6030. (functions (apply 'append
  6031. (mapcar (lambda (x)
  6032. (setq x (car x))
  6033. (if (functionp x) (list x) x))
  6034. actions)))
  6035. (alist (apply 'append (mapcar 'cdr actions)))
  6036. window)
  6037. (unless (buffer-live-p buffer)
  6038. (error "Invalid buffer"))
  6039. (while (and functions (not window))
  6040. (setq window (funcall (car functions) buffer alist)
  6041. functions (cdr functions)))
  6042. (and (windowp window) window)))))
  6043. (defun display-buffer-other-frame (buffer)
  6044. "Display buffer BUFFER preferably in another frame.
  6045. This uses the function `display-buffer' as a subroutine; see
  6046. its documentation for additional customization information."
  6047. (interactive "BDisplay buffer in other frame: ")
  6048. (display-buffer buffer display-buffer--other-frame-action t))
  6049. ;;; `display-buffer' action functions:
  6050. (defun display-buffer-use-some-frame (buffer alist)
  6051. "Display BUFFER in an existing frame that meets a predicate
  6052. \(by default any frame other than the current frame). If
  6053. successful, return the window used; otherwise return nil.
  6054. If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
  6055. raising the frame.
  6056. If ALIST has a non-nil `frame-predicate' entry, its value is a
  6057. function taking one argument (a frame), returning non-nil if the
  6058. frame is a candidate; this function replaces the default
  6059. predicate.
  6060. If ALIST has a non-nil `inhibit-same-window' entry, avoid using
  6061. the currently selected window (only useful with a frame-predicate
  6062. that allows the selected frame)."
  6063. (let* ((predicate (or (cdr (assq 'frame-predicate alist))
  6064. (lambda (frame)
  6065. (and
  6066. (not (eq frame (selected-frame)))
  6067. (not (window-dedicated-p
  6068. (or
  6069. (get-lru-window frame)
  6070. (frame-first-window frame)))))
  6071. )))
  6072. (frame (car (filtered-frame-list predicate)))
  6073. (window (and frame (get-lru-window frame nil (cdr (assq 'inhibit-same-window alist))))))
  6074. (when window
  6075. (prog1
  6076. (window--display-buffer
  6077. buffer window 'frame alist display-buffer-mark-dedicated)
  6078. (unless (cdr (assq 'inhibit-switch-frame alist))
  6079. (window--maybe-raise-frame frame))))
  6080. ))
  6081. (defun display-buffer-same-window (buffer alist)
  6082. "Display BUFFER in the selected window.
  6083. This fails if ALIST has a non-nil `inhibit-same-window' entry, or
  6084. if the selected window is a minibuffer window or is dedicated to
  6085. another buffer; in that case, return nil. Otherwise, return the
  6086. selected window."
  6087. (unless (or (cdr (assq 'inhibit-same-window alist))
  6088. (window-minibuffer-p)
  6089. (window-dedicated-p))
  6090. (window--display-buffer buffer (selected-window) 'reuse alist)))
  6091. (defun display-buffer--maybe-same-window (buffer alist)
  6092. "Conditionally display BUFFER in the selected window.
  6093. If `same-window-p' returns non-nil for BUFFER's name, call
  6094. `display-buffer-same-window' and return its value. Otherwise,
  6095. return nil."
  6096. (and (same-window-p (buffer-name buffer))
  6097. (display-buffer-same-window buffer alist)))
  6098. (defun display-buffer-reuse-window (buffer alist)
  6099. "Return a window that is already displaying BUFFER.
  6100. Return nil if no usable window is found.
  6101. If ALIST has a non-nil `inhibit-same-window' entry, the selected
  6102. window is not eligible for reuse.
  6103. If ALIST contains a `reusable-frames' entry, its value determines
  6104. which frames to search for a reusable window:
  6105. nil -- the selected frame (actually the last non-minibuffer frame)
  6106. A frame -- just that frame
  6107. `visible' -- all visible frames
  6108. 0 -- all frames on the current terminal
  6109. t -- all frames.
  6110. If ALIST contains no `reusable-frames' entry, search just the
  6111. selected frame if `display-buffer-reuse-frames' and
  6112. `pop-up-frames' are both nil; search all frames on the current
  6113. terminal if either of those variables is non-nil.
  6114. If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
  6115. event that a window on another frame is chosen, avoid raising
  6116. that frame."
  6117. (let* ((alist-entry (assq 'reusable-frames alist))
  6118. (frames (cond (alist-entry (cdr alist-entry))
  6119. ((if (eq pop-up-frames 'graphic-only)
  6120. (display-graphic-p)
  6121. pop-up-frames)
  6122. 0)
  6123. (display-buffer-reuse-frames 0)
  6124. (t (last-nonminibuffer-frame))))
  6125. (window (if (and (eq buffer (window-buffer))
  6126. (not (cdr (assq 'inhibit-same-window alist))))
  6127. (selected-window)
  6128. (car (delq (selected-window)
  6129. (get-buffer-window-list buffer 'nomini
  6130. frames))))))
  6131. (when (window-live-p window)
  6132. (prog1 (window--display-buffer buffer window 'reuse alist)
  6133. (unless (cdr (assq 'inhibit-switch-frame alist))
  6134. (window--maybe-raise-frame (window-frame window)))))))
  6135. (defun display-buffer--special-action (buffer)
  6136. "Return special display action for BUFFER, if any.
  6137. If `special-display-p' returns non-nil for BUFFER, return an
  6138. appropriate display action involving `special-display-function'.
  6139. See `display-buffer' for the format of display actions."
  6140. (and special-display-function
  6141. ;; `special-display-p' returns either t or a list of frame
  6142. ;; parameters to pass to `special-display-function'.
  6143. (let ((pars (special-display-p (buffer-name buffer))))
  6144. (when pars
  6145. (list (list #'display-buffer-reuse-window
  6146. `(lambda (buffer _alist)
  6147. (funcall special-display-function
  6148. buffer ',(if (listp pars) pars)))))))))
  6149. (defun display-buffer-pop-up-frame (buffer alist)
  6150. "Display BUFFER in a new frame.
  6151. This works by calling `pop-up-frame-function'. If successful,
  6152. return the window used; otherwise return nil.
  6153. If ALIST has a non-nil `inhibit-switch-frame' entry, avoid
  6154. raising the new frame.
  6155. If ALIST has a non-nil `pop-up-frame-parameters' entry, the
  6156. corresponding value is an alist of frame parameters to give the
  6157. new frame."
  6158. (let* ((params (cdr (assq 'pop-up-frame-parameters alist)))
  6159. (pop-up-frame-alist (append params pop-up-frame-alist))
  6160. (fun pop-up-frame-function)
  6161. frame window)
  6162. (when (and fun
  6163. ;; Make BUFFER current so `make-frame' will use it as the
  6164. ;; new frame's buffer (Bug#15133).
  6165. (with-current-buffer buffer
  6166. (setq frame (funcall fun)))
  6167. (setq window (frame-selected-window frame)))
  6168. (prog1 (window--display-buffer
  6169. buffer window 'frame alist display-buffer-mark-dedicated)
  6170. (unless (cdr (assq 'inhibit-switch-frame alist))
  6171. (window--maybe-raise-frame frame))))))
  6172. (defun display-buffer-pop-up-window (buffer alist)
  6173. "Display BUFFER by popping up a new window.
  6174. The new window is created on the selected frame, or in
  6175. `last-nonminibuffer-frame' if no windows can be created there.
  6176. If successful, return the new window; otherwise return nil.
  6177. If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
  6178. event that the new window is created on another frame, avoid
  6179. raising the frame."
  6180. (let ((frame (or (window--frame-usable-p (selected-frame))
  6181. (window--frame-usable-p (last-nonminibuffer-frame))))
  6182. window)
  6183. (when (and (or (not (frame-parameter frame 'unsplittable))
  6184. ;; If the selected frame cannot be split, look at
  6185. ;; `last-nonminibuffer-frame'.
  6186. (and (eq frame (selected-frame))
  6187. (setq frame (last-nonminibuffer-frame))
  6188. (window--frame-usable-p frame)
  6189. (not (frame-parameter frame 'unsplittable))))
  6190. ;; Attempt to split largest or least recently used window.
  6191. (setq window (or (window--try-to-split-window
  6192. (get-largest-window frame t) alist)
  6193. (window--try-to-split-window
  6194. (get-lru-window frame t) alist))))
  6195. (prog1 (window--display-buffer
  6196. buffer window 'window alist display-buffer-mark-dedicated)
  6197. (unless (cdr (assq 'inhibit-switch-frame alist))
  6198. (window--maybe-raise-frame (window-frame window)))))))
  6199. (defun display-buffer--maybe-pop-up-frame-or-window (buffer alist)
  6200. "Try displaying BUFFER based on `pop-up-frames' or `pop-up-windows'.
  6201. If `pop-up-frames' is non-nil (and not `graphic-only' on a
  6202. text-only terminal), try with `display-buffer-pop-up-frame'.
  6203. If that cannot be done, and `pop-up-windows' is non-nil, try
  6204. again with `display-buffer-pop-up-window'."
  6205. (or (and (if (eq pop-up-frames 'graphic-only)
  6206. (display-graphic-p)
  6207. pop-up-frames)
  6208. (display-buffer-pop-up-frame buffer alist))
  6209. (and pop-up-windows
  6210. (display-buffer-pop-up-window buffer alist))))
  6211. (defun display-buffer-below-selected (buffer alist)
  6212. "Try displaying BUFFER in a window below the selected window.
  6213. This either splits the selected window or reuses the window below
  6214. the selected one."
  6215. (let (window)
  6216. (or (and (setq window (window-in-direction 'below))
  6217. (eq buffer (window-buffer window))
  6218. (window--display-buffer buffer window 'reuse alist))
  6219. (and (not (frame-parameter nil 'unsplittable))
  6220. (let ((split-height-threshold 0)
  6221. split-width-threshold)
  6222. (setq window (window--try-to-split-window (selected-window) alist)))
  6223. (window--display-buffer
  6224. buffer window 'window alist display-buffer-mark-dedicated))
  6225. (and (setq window (window-in-direction 'below))
  6226. (not (window-dedicated-p window))
  6227. (window--display-buffer
  6228. buffer window 'reuse alist display-buffer-mark-dedicated)))))
  6229. (defun display-buffer-at-bottom (buffer alist)
  6230. "Try displaying BUFFER in a window at the bottom of the selected frame.
  6231. This either reuses such a window provided it shows BUFFER
  6232. already, splits a window at the bottom of the frame or the
  6233. frame's root window, or reuses some window at the bottom of the
  6234. selected frame."
  6235. (let (bottom-window bottom-window-shows-buffer window)
  6236. (walk-window-tree
  6237. (lambda (window)
  6238. (cond
  6239. ((window-in-direction 'below window))
  6240. ((and (not bottom-window-shows-buffer)
  6241. (eq buffer (window-buffer window)))
  6242. (setq bottom-window-shows-buffer t)
  6243. (setq bottom-window window))
  6244. ((not bottom-window)
  6245. (setq bottom-window window)))
  6246. nil nil 'nomini))
  6247. (or (and bottom-window-shows-buffer
  6248. (window--display-buffer
  6249. buffer bottom-window 'reuse alist display-buffer-mark-dedicated))
  6250. (and (not (frame-parameter nil 'unsplittable))
  6251. (let (split-width-threshold)
  6252. (setq window (window--try-to-split-window bottom-window alist)))
  6253. (window--display-buffer
  6254. buffer window 'window alist display-buffer-mark-dedicated))
  6255. (and (not (frame-parameter nil 'unsplittable))
  6256. (setq window
  6257. (condition-case nil
  6258. (split-window (window--major-non-side-window))
  6259. (error nil)))
  6260. (window--display-buffer
  6261. buffer window 'window alist display-buffer-mark-dedicated))
  6262. (and (setq window bottom-window)
  6263. (not (window-dedicated-p window))
  6264. (window--display-buffer
  6265. buffer window 'reuse alist display-buffer-mark-dedicated)))))
  6266. (defun display-buffer-in-previous-window (buffer alist)
  6267. "Display BUFFER in a window previously showing it.
  6268. If ALIST has a non-nil `inhibit-same-window' entry, the selected
  6269. window is not eligible for reuse.
  6270. If ALIST contains a `reusable-frames' entry, its value determines
  6271. which frames to search for a reusable window:
  6272. nil -- the selected frame (actually the last non-minibuffer frame)
  6273. A frame -- just that frame
  6274. `visible' -- all visible frames
  6275. 0 -- all frames on the current terminal
  6276. t -- all frames.
  6277. If ALIST contains no `reusable-frames' entry, search just the
  6278. selected frame if `display-buffer-reuse-frames' and
  6279. `pop-up-frames' are both nil; search all frames on the current
  6280. terminal if either of those variables is non-nil.
  6281. If ALIST has a `previous-window' entry, the window specified by
  6282. that entry will override any other window found by the methods
  6283. above, even if that window never showed BUFFER before."
  6284. (let* ((alist-entry (assq 'reusable-frames alist))
  6285. (inhibit-same-window
  6286. (cdr (assq 'inhibit-same-window alist)))
  6287. (frames (cond
  6288. (alist-entry (cdr alist-entry))
  6289. ((if (eq pop-up-frames 'graphic-only)
  6290. (display-graphic-p)
  6291. pop-up-frames)
  6292. 0)
  6293. (display-buffer-reuse-frames 0)
  6294. (t (last-nonminibuffer-frame))))
  6295. best-window second-best-window window)
  6296. ;; Scan windows whether they have shown the buffer recently.
  6297. (catch 'best
  6298. (dolist (window (window-list-1 (frame-first-window) 'nomini frames))
  6299. (when (and (assq buffer (window-prev-buffers window))
  6300. (not (window-dedicated-p window)))
  6301. (if (eq window (selected-window))
  6302. (unless inhibit-same-window
  6303. (setq second-best-window window))
  6304. (setq best-window window)
  6305. (throw 'best t)))))
  6306. ;; When ALIST has a `previous-window' entry, that entry may override
  6307. ;; anything we found so far.
  6308. (when (and (setq window (cdr (assq 'previous-window alist)))
  6309. (window-live-p window)
  6310. (not (window-dedicated-p window)))
  6311. (if (eq window (selected-window))
  6312. (unless inhibit-same-window
  6313. (setq second-best-window window))
  6314. (setq best-window window)))
  6315. ;; Return best or second best window found.
  6316. (when (setq window (or best-window second-best-window))
  6317. (window--display-buffer buffer window 'reuse alist))))
  6318. (defun display-buffer-use-some-window (buffer alist)
  6319. "Display BUFFER in an existing window.
  6320. Search for a usable window, set that window to the buffer, and
  6321. return the window. If no suitable window is found, return nil.
  6322. If ALIST has a non-nil `inhibit-switch-frame' entry, then in the
  6323. event that a window in another frame is chosen, avoid raising
  6324. that frame."
  6325. (let* ((not-this-window (cdr (assq 'inhibit-same-window alist)))
  6326. (frame (or (window--frame-usable-p (selected-frame))
  6327. (window--frame-usable-p (last-nonminibuffer-frame))))
  6328. (window
  6329. ;; Reuse an existing window.
  6330. (or (get-lru-window frame nil not-this-window)
  6331. (let ((window (get-buffer-window buffer 'visible)))
  6332. (unless (and not-this-window
  6333. (eq window (selected-window)))
  6334. window))
  6335. (get-largest-window 'visible nil not-this-window)
  6336. (let ((window (get-buffer-window buffer 0)))
  6337. (unless (and not-this-window
  6338. (eq window (selected-window)))
  6339. window))
  6340. (get-largest-window 0 nil not-this-window)))
  6341. (quit-restore (and (window-live-p window)
  6342. (window-parameter window 'quit-restore)))
  6343. (quad (nth 1 quit-restore)))
  6344. (when (window-live-p window)
  6345. ;; If the window was used by `display-buffer' before, try to
  6346. ;; resize it to its old height but don't signal an error.
  6347. (when (and (listp quad)
  6348. (integerp (nth 3 quad))
  6349. (> (nth 3 quad) (window-total-height window)))
  6350. (condition-case nil
  6351. (window-resize window (- (nth 3 quad) (window-total-height window)))
  6352. (error nil)))
  6353. (prog1
  6354. (window--display-buffer buffer window 'reuse alist)
  6355. (window--even-window-sizes window)
  6356. (unless (cdr (assq 'inhibit-switch-frame alist))
  6357. (window--maybe-raise-frame (window-frame window)))))))
  6358. (defun display-buffer-no-window (_buffer alist)
  6359. "Display BUFFER in no window.
  6360. If ALIST has a non-nil `allow-no-window' entry, then don't display
  6361. a window at all. This makes possible to override the default action
  6362. and avoid displaying the buffer. It is assumed that when the caller
  6363. specifies a non-nil `allow-no-window' then it can handle a nil value
  6364. returned from `display-buffer' in this case."
  6365. (when (cdr (assq 'allow-no-window alist))
  6366. 'fail))
  6367. ;;; Display + selection commands:
  6368. (defun pop-to-buffer (buffer &optional action norecord)
  6369. "Select buffer BUFFER in some window, preferably a different one.
  6370. BUFFER may be a buffer, a string (a buffer name), or nil. If it
  6371. is a string not naming an existent buffer, create a buffer with
  6372. that name. If BUFFER is nil, choose some other buffer. Return
  6373. the buffer.
  6374. This uses `display-buffer' as a subroutine. The optional ACTION
  6375. argument is passed to `display-buffer' as its ACTION argument.
  6376. See `display-buffer' for more information. ACTION is t if called
  6377. interactively with a prefix argument, which means to pop to a
  6378. window other than the selected one even if the buffer is already
  6379. displayed in the selected window.
  6380. If the window to show BUFFER is not on the selected
  6381. frame, raise that window's frame and give it input focus.
  6382. Optional third arg NORECORD non-nil means do not put this buffer
  6383. at the front of the list of recently selected ones."
  6384. (interactive (list (read-buffer "Pop to buffer: " (other-buffer))
  6385. (if current-prefix-arg t)))
  6386. (setq buffer (window-normalize-buffer-to-switch-to buffer))
  6387. ;; This should be done by `select-window' below.
  6388. ;; (set-buffer buffer)
  6389. (let* ((old-frame (selected-frame))
  6390. (window (display-buffer buffer action))
  6391. (frame (window-frame window)))
  6392. ;; If we chose another frame, make sure it gets input focus.
  6393. (unless (eq frame old-frame)
  6394. (select-frame-set-input-focus frame norecord))
  6395. ;; Make sure new window is selected (Bug#8615), (Bug#6954).
  6396. (select-window window norecord)
  6397. buffer))
  6398. (defun pop-to-buffer-same-window (buffer &optional norecord)
  6399. "Select buffer BUFFER in some window, preferably the same one.
  6400. BUFFER may be a buffer, a string (a buffer name), or nil. If it
  6401. is a string not naming an existent buffer, create a buffer with
  6402. that name. If BUFFER is nil, choose some other buffer. Return
  6403. the buffer.
  6404. Optional argument NORECORD, if non-nil means do not put this
  6405. buffer at the front of the list of recently selected ones.
  6406. Unlike `pop-to-buffer', this function prefers using the selected
  6407. window over popping up a new window or frame."
  6408. (pop-to-buffer buffer display-buffer--same-window-action norecord))
  6409. (defun read-buffer-to-switch (prompt)
  6410. "Read the name of a buffer to switch to, prompting with PROMPT.
  6411. Return the name of the buffer as a string.
  6412. This function is intended for the `switch-to-buffer' family of
  6413. commands since these need to omit the name of the current buffer
  6414. from the list of completions and default values."
  6415. (let ((rbts-completion-table (internal-complete-buffer-except)))
  6416. (minibuffer-with-setup-hook
  6417. (lambda ()
  6418. (setq minibuffer-completion-table rbts-completion-table)
  6419. ;; Since rbts-completion-table is built dynamically, we
  6420. ;; can't just add it to the default value of
  6421. ;; icomplete-with-completion-tables, so we add it
  6422. ;; here manually.
  6423. (if (and (boundp 'icomplete-with-completion-tables)
  6424. (listp icomplete-with-completion-tables))
  6425. (set (make-local-variable 'icomplete-with-completion-tables)
  6426. (cons rbts-completion-table
  6427. icomplete-with-completion-tables))))
  6428. (read-buffer prompt (other-buffer (current-buffer))
  6429. (confirm-nonexistent-file-or-buffer)))))
  6430. (defun window-normalize-buffer-to-switch-to (buffer-or-name)
  6431. "Normalize BUFFER-OR-NAME argument of buffer switching functions.
  6432. If BUFFER-OR-NAME is nil, return the buffer returned by
  6433. `other-buffer'. Else, if a buffer specified by BUFFER-OR-NAME
  6434. exists, return that buffer. If no such buffer exists, create a
  6435. buffer with the name BUFFER-OR-NAME and return that buffer."
  6436. (if buffer-or-name
  6437. (or (get-buffer buffer-or-name)
  6438. (let ((buffer (get-buffer-create buffer-or-name)))
  6439. (set-buffer-major-mode buffer)
  6440. buffer))
  6441. (other-buffer)))
  6442. (defcustom switch-to-buffer-preserve-window-point nil
  6443. "If non-nil, `switch-to-buffer' tries to preserve `window-point'.
  6444. If this is nil, `switch-to-buffer' displays the buffer at that
  6445. buffer's `point'. If this is `already-displayed', it tries to
  6446. display the buffer at its previous position in the selected
  6447. window, provided the buffer is currently displayed in some other
  6448. window on any visible or iconified frame. If this is t, it
  6449. unconditionally tries to display the buffer at its previous
  6450. position in the selected window.
  6451. This variable is ignored if the buffer is already displayed in
  6452. the selected window or never appeared in it before, or if
  6453. `switch-to-buffer' calls `pop-to-buffer' to display the buffer."
  6454. :type '(choice
  6455. (const :tag "Never" nil)
  6456. (const :tag "If already displayed elsewhere" already-displayed)
  6457. (const :tag "Always" t))
  6458. :group 'windows
  6459. :version "24.3")
  6460. (defcustom switch-to-buffer-in-dedicated-window nil
  6461. "Allow switching to buffer in strongly dedicated windows.
  6462. If non-nil, allow `switch-to-buffer' to proceed when called
  6463. interactively and the selected window is strongly dedicated to
  6464. its buffer.
  6465. The following values are recognized:
  6466. nil - disallow switching; signal an error
  6467. prompt - prompt user whether to allow switching
  6468. pop - perform `pop-to-buffer' instead
  6469. t - undedicate selected window and switch
  6470. When called non-interactively, `switch-to-buffer' always signals
  6471. an error when the selected window is dedicated to its buffer and
  6472. FORCE-SAME-WINDOW is non-nil."
  6473. :type '(choice
  6474. (const :tag "Disallow" nil)
  6475. (const :tag "Prompt" prompt)
  6476. (const :tag "Pop" pop)
  6477. (const :tag "Allow" t))
  6478. :group 'windows
  6479. :version "25.1")
  6480. (defun switch-to-buffer (buffer-or-name &optional norecord force-same-window)
  6481. "Display buffer BUFFER-OR-NAME in the selected window.
  6482. WARNING: This is NOT the way to work on another buffer temporarily
  6483. within a Lisp program! Use `set-buffer' instead. That avoids
  6484. messing with the window-buffer correspondences.
  6485. If the selected window cannot display the specified buffer
  6486. because it is a minibuffer window or strongly dedicated to
  6487. another buffer, call `pop-to-buffer' to select the buffer in
  6488. another window. In interactive use, if the selected window is
  6489. strongly dedicated to its buffer, the value of the option
  6490. `switch-to-buffer-in-dedicated-window' specifies how to proceed.
  6491. If called interactively, read the buffer name using the
  6492. minibuffer. The variable `confirm-nonexistent-file-or-buffer'
  6493. determines whether to request confirmation before creating a new
  6494. buffer.
  6495. BUFFER-OR-NAME may be a buffer, a string (a buffer name), or nil.
  6496. If BUFFER-OR-NAME is a string that does not identify an existing
  6497. buffer, create a buffer with that name. If BUFFER-OR-NAME is
  6498. nil, switch to the buffer returned by `other-buffer'.
  6499. If optional argument NORECORD is non-nil, do not put the buffer
  6500. at the front of the buffer list, and do not make the window
  6501. displaying it the most recently selected one.
  6502. If optional argument FORCE-SAME-WINDOW is non-nil, the buffer
  6503. must be displayed in the selected window when called
  6504. non-interactively; if that is impossible, signal an error rather
  6505. than calling `pop-to-buffer'.
  6506. The option `switch-to-buffer-preserve-window-point' can be used
  6507. to make the buffer appear at its last position in the selected
  6508. window.
  6509. Return the buffer switched to."
  6510. (interactive
  6511. (let ((force-same-window
  6512. (cond
  6513. ((window-minibuffer-p) nil)
  6514. ((not (eq (window-dedicated-p) t)) 'force-same-window)
  6515. ((pcase switch-to-buffer-in-dedicated-window
  6516. (`nil (user-error
  6517. "Cannot switch buffers in a dedicated window"))
  6518. (`prompt
  6519. (if (y-or-n-p
  6520. (format "Window is dedicated to %s; undedicate it"
  6521. (window-buffer)))
  6522. (progn
  6523. (set-window-dedicated-p nil nil)
  6524. 'force-same-window)
  6525. (user-error
  6526. "Cannot switch buffers in a dedicated window")))
  6527. (`pop nil)
  6528. (_ (set-window-dedicated-p nil nil) 'force-same-window))))))
  6529. (list (read-buffer-to-switch "Switch to buffer: ") nil force-same-window)))
  6530. (let ((buffer (window-normalize-buffer-to-switch-to buffer-or-name)))
  6531. (cond
  6532. ;; Don't call set-window-buffer if it's not needed since it
  6533. ;; might signal an error (e.g. if the window is dedicated).
  6534. ((eq buffer (window-buffer)))
  6535. ((window-minibuffer-p)
  6536. (if force-same-window
  6537. (user-error "Cannot switch buffers in minibuffer window")
  6538. (pop-to-buffer buffer norecord)))
  6539. ((eq (window-dedicated-p) t)
  6540. (if force-same-window
  6541. (user-error "Cannot switch buffers in a dedicated window")
  6542. (pop-to-buffer buffer norecord)))
  6543. (t
  6544. (let* ((entry (assq buffer (window-prev-buffers)))
  6545. (displayed (and (eq switch-to-buffer-preserve-window-point
  6546. 'already-displayed)
  6547. (get-buffer-window buffer 0))))
  6548. (set-window-buffer nil buffer)
  6549. (when (and entry
  6550. (or (eq switch-to-buffer-preserve-window-point t)
  6551. displayed))
  6552. ;; Try to restore start and point of buffer in the selected
  6553. ;; window (Bug#4041).
  6554. (set-window-start (selected-window) (nth 1 entry) t)
  6555. (set-window-point nil (nth 2 entry))))))
  6556. (unless norecord
  6557. (select-window (selected-window)))
  6558. (set-buffer buffer)))
  6559. (defun switch-to-buffer-other-window (buffer-or-name &optional norecord)
  6560. "Select the buffer specified by BUFFER-OR-NAME in another window.
  6561. BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
  6562. nil. Return the buffer switched to.
  6563. If called interactively, prompt for the buffer name using the
  6564. minibuffer. The variable `confirm-nonexistent-file-or-buffer'
  6565. determines whether to request confirmation before creating a new
  6566. buffer.
  6567. If BUFFER-OR-NAME is a string and does not identify an existing
  6568. buffer, create a new buffer with that name. If BUFFER-OR-NAME is
  6569. nil, switch to the buffer returned by `other-buffer'.
  6570. Optional second argument NORECORD non-nil means do not put this
  6571. buffer at the front of the list of recently selected ones.
  6572. This uses the function `display-buffer' as a subroutine; see its
  6573. documentation for additional customization information."
  6574. (interactive
  6575. (list (read-buffer-to-switch "Switch to buffer in other window: ")))
  6576. (let ((pop-up-windows t))
  6577. (pop-to-buffer buffer-or-name t norecord)))
  6578. (defun switch-to-buffer-other-frame (buffer-or-name &optional norecord)
  6579. "Switch to buffer BUFFER-OR-NAME in another frame.
  6580. BUFFER-OR-NAME may be a buffer, a string (a buffer name), or
  6581. nil. Return the buffer switched to.
  6582. If called interactively, prompt for the buffer name using the
  6583. minibuffer. The variable `confirm-nonexistent-file-or-buffer'
  6584. determines whether to request confirmation before creating a new
  6585. buffer.
  6586. If BUFFER-OR-NAME is a string and does not identify an existing
  6587. buffer, create a new buffer with that name. If BUFFER-OR-NAME is
  6588. nil, switch to the buffer returned by `other-buffer'.
  6589. Optional second arg NORECORD non-nil means do not put this
  6590. buffer at the front of the list of recently selected ones.
  6591. This uses the function `display-buffer' as a subroutine; see its
  6592. documentation for additional customization information."
  6593. (interactive
  6594. (list (read-buffer-to-switch "Switch to buffer in other frame: ")))
  6595. (pop-to-buffer buffer-or-name display-buffer--other-frame-action norecord))
  6596. (defun set-window-text-height (window height)
  6597. "Set the height in lines of the text display area of WINDOW to HEIGHT.
  6598. WINDOW must be a live window and defaults to the selected one.
  6599. HEIGHT doesn't include the mode line or header line, if any, or
  6600. any partial-height lines in the text display area.
  6601. Note that the current implementation of this function cannot
  6602. always set the height exactly, but attempts to be conservative,
  6603. by allocating more lines than are actually needed in the case
  6604. where some error may be present."
  6605. (setq window (window-normalize-window window t))
  6606. (let ((delta (- height (window-text-height window))))
  6607. (unless (zerop delta)
  6608. ;; Setting window-min-height to a value like 1 can lead to very
  6609. ;; bizarre displays because it also allows Emacs to make *other*
  6610. ;; windows one line tall, which means that there's no more space
  6611. ;; for the mode line.
  6612. (let ((window-min-height (min 2 height)))
  6613. (window-resize window delta)))))
  6614. (defun enlarge-window-horizontally (delta)
  6615. "Make selected window DELTA columns wider.
  6616. Interactively, if no argument is given, make selected window one
  6617. column wider."
  6618. (interactive "p")
  6619. (enlarge-window delta t))
  6620. (defun shrink-window-horizontally (delta)
  6621. "Make selected window DELTA columns narrower.
  6622. Interactively, if no argument is given, make selected window one
  6623. column narrower."
  6624. (interactive "p")
  6625. (shrink-window delta t))
  6626. (defun count-screen-lines (&optional beg end count-final-newline window)
  6627. "Return the number of screen lines in the region.
  6628. The number of screen lines may be different from the number of actual lines,
  6629. due to line breaking, display table, etc.
  6630. Optional arguments BEG and END default to `point-min' and `point-max'
  6631. respectively.
  6632. If region ends with a newline, ignore it unless optional third argument
  6633. COUNT-FINAL-NEWLINE is non-nil.
  6634. The optional fourth argument WINDOW specifies the window used for obtaining
  6635. parameters such as width, horizontal scrolling, and so on. The default is
  6636. to use the selected window's parameters.
  6637. Like `vertical-motion', `count-screen-lines' always uses the current buffer,
  6638. regardless of which buffer is displayed in WINDOW. This makes possible to use
  6639. `count-screen-lines' in any buffer, whether or not it is currently displayed
  6640. in some window."
  6641. (unless beg
  6642. (setq beg (point-min)))
  6643. (unless end
  6644. (setq end (point-max)))
  6645. (if (= beg end)
  6646. 0
  6647. (save-excursion
  6648. (save-restriction
  6649. (widen)
  6650. (narrow-to-region (min beg end)
  6651. (if (and (not count-final-newline)
  6652. (= ?\n (char-before (max beg end))))
  6653. (1- (max beg end))
  6654. (max beg end)))
  6655. (goto-char (point-min))
  6656. (1+ (vertical-motion (buffer-size) window))))))
  6657. (defun window-buffer-height (window)
  6658. "Return the height (in screen lines) of the buffer that WINDOW is displaying.
  6659. WINDOW must be a live window and defaults to the selected one."
  6660. (setq window (window-normalize-window window t))
  6661. (with-current-buffer (window-buffer window)
  6662. (max 1
  6663. (count-screen-lines (point-min) (point-max)
  6664. ;; If buffer ends with a newline, ignore it when
  6665. ;; counting height unless point is after it.
  6666. (eobp)
  6667. window))))
  6668. ;;; Resizing windows and frames to fit their contents exactly.
  6669. (defcustom fit-window-to-buffer-horizontally nil
  6670. "Non-nil means `fit-window-to-buffer' can resize windows horizontally.
  6671. If this is nil, `fit-window-to-buffer' never resizes windows
  6672. horizontally. If this is `only', it can resize windows
  6673. horizontally only. Any other value means `fit-window-to-buffer'
  6674. can resize windows in both dimensions."
  6675. :type 'boolean
  6676. :version "24.4"
  6677. :group 'help)
  6678. ;; `fit-frame-to-buffer' eventually wants to know the real frame sizes
  6679. ;; counting title bar and outer borders.
  6680. (defcustom fit-frame-to-buffer nil
  6681. "Non-nil means `fit-window-to-buffer' can fit a frame to its buffer.
  6682. A frame is fit if and only if its root window is a live window
  6683. and this option is non-nil. If this is `horizontally', frames
  6684. are resized horizontally only. If this is `vertically', frames
  6685. are resized vertically only. Any other non-nil value means
  6686. frames can be resized in both dimensions."
  6687. :type 'boolean
  6688. :version "24.4"
  6689. :group 'help)
  6690. (defcustom fit-frame-to-buffer-margins '(nil nil nil nil)
  6691. "Margins around frame for `fit-frame-to-buffer'.
  6692. This specifies the numbers of pixels to be left free on the left,
  6693. above, on the right, and below a frame fitted to its buffer. Set
  6694. this to avoid obscuring other desktop objects like the taskbar.
  6695. The default is nil for each side, which means to not add margins.
  6696. The value specified here can be overridden for a specific frame
  6697. by that frame's `fit-frame-to-buffer-margins' parameter, if
  6698. present. See also `fit-frame-to-buffer-sizes'."
  6699. :version "24.4"
  6700. :type '(list
  6701. (choice
  6702. :tag "Left"
  6703. :value nil
  6704. :format "%[LeftMargin%] %v "
  6705. (const :tag "None" :format "%t" nil)
  6706. (integer :tag "Pixels" :size 5))
  6707. (choice
  6708. :tag "Top"
  6709. :value nil
  6710. :format "%[TopMargin%] %v "
  6711. (const :tag "None" :format "%t" nil)
  6712. (integer :tag "Pixels" :size 5))
  6713. (choice
  6714. :tag "Right"
  6715. :value nil
  6716. :format "%[RightMargin%] %v "
  6717. (const :tag "None" :format "%t" nil)
  6718. (integer :tag "Pixels" :size 5))
  6719. (choice
  6720. :tag "Bottom"
  6721. :value nil
  6722. :format "%[BottomMargin%] %v "
  6723. (const :tag "None" :format "%t" nil)
  6724. (integer :tag "Pixels" :size 5)))
  6725. :group 'help)
  6726. (defcustom fit-frame-to-buffer-sizes '(nil nil nil nil)
  6727. "Size boundaries of frame for `fit-frame-to-buffer'.
  6728. This list specifies the total maximum and minimum lines and
  6729. maximum and minimum columns of the root window of any frame that
  6730. shall be fit to its buffer. If any of these values is non-nil,
  6731. it overrides the corresponding argument of `fit-frame-to-buffer'.
  6732. On window systems where the menubar can wrap, fitting a frame to
  6733. its buffer may swallow the last line(s). Specifying an
  6734. appropriate minimum width value here can avoid such wrapping.
  6735. See also `fit-frame-to-buffer-margins'."
  6736. :version "24.4"
  6737. :type '(list
  6738. (choice
  6739. :tag "Maximum Height"
  6740. :value nil
  6741. :format "%[MaxHeight%] %v "
  6742. (const :tag "None" :format "%t" nil)
  6743. (integer :tag "Lines" :size 5))
  6744. (choice
  6745. :tag "Minimum Height"
  6746. :value nil
  6747. :format "%[MinHeight%] %v "
  6748. (const :tag "None" :format "%t" nil)
  6749. (integer :tag "Lines" :size 5))
  6750. (choice
  6751. :tag "Maximum Width"
  6752. :value nil
  6753. :format "%[MaxWidth%] %v "
  6754. (const :tag "None" :format "%t" nil)
  6755. (integer :tag "Columns" :size 5))
  6756. (choice
  6757. :tag "Minimum Width"
  6758. :value nil
  6759. :format "%[MinWidth%] %v\n"
  6760. (const :tag "None" :format "%t" nil)
  6761. (integer :tag "Columns" :size 5)))
  6762. :group 'help)
  6763. (declare-function x-display-pixel-height "xfns.c" (&optional terminal))
  6764. (defun window--sanitize-margin (margin left right)
  6765. "Return MARGIN if it's a number between LEFT and RIGHT."
  6766. (when (and (numberp margin)
  6767. (<= left (- right margin)) (<= margin right))
  6768. margin))
  6769. (declare-function tool-bar-height "xdisp.c" (&optional frame pixelwise))
  6770. (defun fit-frame-to-buffer (&optional frame max-height min-height max-width min-width only)
  6771. "Adjust size of FRAME to display the contents of its buffer exactly.
  6772. FRAME can be any live frame and defaults to the selected one.
  6773. Fit only if FRAME's root window is live. MAX-HEIGHT, MIN-HEIGHT,
  6774. MAX-WIDTH and MIN-WIDTH specify bounds on the new total size of
  6775. FRAME's root window. MIN-HEIGHT and MIN-WIDTH default to the values of
  6776. `window-min-height' and `window-min-width' respectively.
  6777. If the optional argument ONLY is `vertically', resize the frame
  6778. vertically only. If ONLY is `horizontally', resize the frame
  6779. horizontally only.
  6780. The new position and size of FRAME can be additionally determined
  6781. by customizing the options `fit-frame-to-buffer-sizes' and
  6782. `fit-frame-to-buffer-margins' or the corresponding parameters of
  6783. FRAME."
  6784. (interactive)
  6785. (unless (and (fboundp 'x-display-pixel-height)
  6786. ;; We need the respective sizes now.
  6787. (fboundp 'display-monitor-attributes-list))
  6788. (user-error "Cannot resize frame in non-graphic Emacs"))
  6789. (setq frame (window-normalize-frame frame))
  6790. (when (window-live-p (frame-root-window frame))
  6791. (with-selected-window (frame-root-window frame)
  6792. (let* ((window (frame-root-window frame))
  6793. (char-width (frame-char-width))
  6794. (char-height (frame-char-height))
  6795. (monitor-attributes (car (display-monitor-attributes-list
  6796. (frame-parameter frame 'display))))
  6797. (geometry (cdr (assq 'geometry monitor-attributes)))
  6798. (display-width (- (nth 2 geometry) (nth 0 geometry)))
  6799. (display-height (- (nth 3 geometry) (nth 1 geometry)))
  6800. (workarea (cdr (assq 'workarea monitor-attributes)))
  6801. ;; Handle margins.
  6802. (margins (or (frame-parameter frame 'fit-frame-to-buffer-margins)
  6803. fit-frame-to-buffer-margins))
  6804. (left-margin (if (nth 0 margins)
  6805. (or (window--sanitize-margin
  6806. (nth 0 margins) 0 display-width)
  6807. 0)
  6808. (nth 0 workarea)))
  6809. (top-margin (if (nth 1 margins)
  6810. (or (window--sanitize-margin
  6811. (nth 1 margins) 0 display-height)
  6812. 0)
  6813. (nth 1 workarea)))
  6814. (workarea-width (nth 2 workarea))
  6815. (right-margin (if (nth 2 margins)
  6816. (- display-width
  6817. (or (window--sanitize-margin
  6818. (nth 2 margins) left-margin display-width)
  6819. 0))
  6820. (nth 2 workarea)))
  6821. (workarea-height (nth 3 workarea))
  6822. (bottom-margin (if (nth 3 margins)
  6823. (- display-height
  6824. (or (window--sanitize-margin
  6825. (nth 3 margins) top-margin display-height)
  6826. 0))
  6827. (nth 3 workarea)))
  6828. ;; The pixel width of FRAME (which does not include the
  6829. ;; window manager's decorations).
  6830. (frame-width (frame-pixel-width))
  6831. ;; The pixel width of the body of FRAME's root window.
  6832. (window-body-width (window-body-width nil t))
  6833. ;; The difference in pixels between total and body width of
  6834. ;; FRAME's window.
  6835. (window-extra-width (- (window-pixel-width) window-body-width))
  6836. ;; The difference in pixels between the frame's pixel width
  6837. ;; and the window's body width. This is the space we can't
  6838. ;; use for fitting.
  6839. (extra-width (- frame-width window-body-width))
  6840. ;; The maximum width we can use for fitting.
  6841. (fit-width (- workarea-width extra-width))
  6842. ;; The pixel position of FRAME's left border. We usually
  6843. ;; try to leave this alone.
  6844. (left
  6845. (let ((left (frame-parameter nil 'left)))
  6846. (if (consp left)
  6847. (funcall (car left) (cadr left))
  6848. left)))
  6849. ;; The pixel height of FRAME (which does not include title
  6850. ;; line, decorations, and sometimes neither the menu nor
  6851. ;; the toolbar).
  6852. (frame-height (frame-pixel-height))
  6853. ;; The pixel height of FRAME's root window (we don't care
  6854. ;; about the window's body height since the return value of
  6855. ;; `window-text-pixel-size' includes header and mode line).
  6856. (window-height (window-pixel-height))
  6857. ;; The difference in pixels between the frame's pixel
  6858. ;; height and the window's height.
  6859. (extra-height (- frame-height window-height))
  6860. ;; When tool-bar-mode is enabled and we just created a new
  6861. ;; frame, reserve lines for toolbar resizing. Needed
  6862. ;; because for reasons unknown to me Emacs (1) reserves one
  6863. ;; line for the toolbar when making the initial frame and
  6864. ;; toolbars are enabled, and (2) later adds the remaining
  6865. ;; lines needed. Our code runs IN BETWEEN (1) and (2).
  6866. ;; YMMV when you're on a system that behaves differently.
  6867. (toolbar-extra-height
  6868. (let ((quit-restore (window-parameter window 'quit-restore))
  6869. ;; This may have to change when we allow arbitrary
  6870. ;; pixel height toolbars.
  6871. (lines (tool-bar-height)))
  6872. (* char-height
  6873. (if (and quit-restore (eq (car quit-restore) 'frame)
  6874. (not (zerop lines)))
  6875. (1- lines)
  6876. 0))))
  6877. ;; The pixel position of FRAME's top border.
  6878. (top
  6879. (let ((top (frame-parameter nil 'top)))
  6880. (if (consp top)
  6881. (funcall (car top) (cadr top))
  6882. top)))
  6883. ;; Sanitize minimum and maximum sizes.
  6884. (sizes (or (frame-parameter frame 'fit-frame-to-buffer-sizes)
  6885. fit-frame-to-buffer-sizes))
  6886. (max-height
  6887. (cond
  6888. ((numberp (nth 0 sizes)) (* (nth 0 sizes) char-height))
  6889. ((numberp max-height) (* max-height char-height))
  6890. (t display-height)))
  6891. (min-height
  6892. (cond
  6893. ((numberp (nth 1 sizes)) (* (nth 1 sizes) char-height))
  6894. ((numberp min-height) (* min-height char-height))
  6895. (t (* window-min-height char-height))))
  6896. (max-width
  6897. (cond
  6898. ((numberp (nth 2 sizes))
  6899. (- (* (nth 2 sizes) char-width) window-extra-width))
  6900. ((numberp max-width)
  6901. (- (* max-width char-width) window-extra-width))
  6902. (t display-width)))
  6903. (min-width
  6904. (cond
  6905. ((numberp (nth 3 sizes))
  6906. (- (* (nth 3 sizes) char-width) window-extra-width))
  6907. ((numberp min-width)
  6908. (- (* min-width char-width) window-extra-width))
  6909. (t (* window-min-width char-width))))
  6910. ;; Note: Currently, for a new frame the sizes of the header
  6911. ;; and mode line may be estimated incorrectly
  6912. (value (window-text-pixel-size
  6913. nil t t workarea-width workarea-height t))
  6914. (width (+ (car value) (window-right-divider-width)))
  6915. (height
  6916. (+ (cdr value)
  6917. (window-bottom-divider-width)
  6918. (window-scroll-bar-height))))
  6919. ;; Don't change height or width when the window's size is fixed
  6920. ;; in either direction or ONLY forbids it.
  6921. (cond
  6922. ((or (eq window-size-fixed 'width) (eq only 'vertically))
  6923. (setq width nil))
  6924. ((or (eq window-size-fixed 'height) (eq only 'horizontally))
  6925. (setq height nil)))
  6926. ;; Fit width to constraints.
  6927. (when width
  6928. (unless frame-resize-pixelwise
  6929. ;; Round to character sizes.
  6930. (setq width (* (/ (+ width char-width -1) char-width)
  6931. char-width)))
  6932. ;; Fit to maximum and minimum widths.
  6933. (setq width (max (min width max-width) min-width))
  6934. ;; Add extra width.
  6935. (setq width (+ width extra-width))
  6936. ;; Preserve margins.
  6937. (let ((right (+ left width)))
  6938. (cond
  6939. ((> right right-margin)
  6940. ;; Move frame to left (we don't know its real width).
  6941. (setq left (max left-margin (- left (- right right-margin)))))
  6942. ((< left left-margin)
  6943. ;; Move frame to right.
  6944. (setq left left-margin)))))
  6945. ;; Fit height to constraints.
  6946. (when height
  6947. (unless frame-resize-pixelwise
  6948. (setq height (* (/ (+ height char-height -1) char-height)
  6949. char-height)))
  6950. ;; Fit to maximum and minimum heights.
  6951. (setq height (max (min height max-height) min-height))
  6952. ;; Add extra height.
  6953. (setq height (+ height extra-height))
  6954. ;; Preserve margins.
  6955. (let ((bottom (+ top height)))
  6956. (cond
  6957. ((> bottom bottom-margin)
  6958. ;; Move frame up (we don't know its real height).
  6959. (setq top (max top-margin (- top (- bottom bottom-margin)))))
  6960. ((< top top-margin)
  6961. ;; Move frame down.
  6962. (setq top top-margin)))))
  6963. ;; Apply changes.
  6964. (set-frame-position frame left top)
  6965. ;; Clumsily try to translate our calculations to what
  6966. ;; `set-frame-size' wants.
  6967. (when width
  6968. (setq width (- (+ (frame-text-width) width)
  6969. extra-width window-body-width)))
  6970. (when height
  6971. (setq height (- (+ (frame-text-height) height)
  6972. extra-height window-height)))
  6973. (set-frame-size
  6974. frame
  6975. (if width
  6976. (if frame-resize-pixelwise
  6977. width
  6978. (/ width char-width))
  6979. (frame-text-width))
  6980. (if height
  6981. (if frame-resize-pixelwise
  6982. height
  6983. (/ height char-height))
  6984. (frame-text-height))
  6985. frame-resize-pixelwise)))))
  6986. (defun fit-window-to-buffer (&optional window max-height min-height max-width min-width preserve-size)
  6987. "Adjust size of WINDOW to display its buffer's contents exactly.
  6988. WINDOW must be a live window and defaults to the selected one.
  6989. If WINDOW is part of a vertical combination, adjust WINDOW's
  6990. height. The new height is calculated from the actual height of
  6991. the accessible portion of its buffer. The optional argument
  6992. MAX-HEIGHT specifies a maximum height and defaults to the height
  6993. of WINDOW's frame. The optional argument MIN-HEIGHT specifies a
  6994. minimum height and defaults to `window-min-height'. Both
  6995. MAX-HEIGHT and MIN-HEIGHT are specified in lines and include mode
  6996. and header line and a bottom divider, if any.
  6997. If WINDOW is part of a horizontal combination and the value of
  6998. the option `fit-window-to-buffer-horizontally' is non-nil, adjust
  6999. WINDOW's width. The new width of WINDOW is calculated from the
  7000. maximum length of its buffer's lines that follow the current
  7001. start position of WINDOW. The optional argument MAX-WIDTH
  7002. specifies a maximum width and defaults to the width of WINDOW's
  7003. frame. The optional argument MIN-WIDTH specifies a minimum width
  7004. and defaults to `window-min-width'. Both MAX-WIDTH and MIN-WIDTH
  7005. are specified in columns and include fringes, margins, a
  7006. scrollbar and a vertical divider, if any.
  7007. If the optional argument `preserve-size' is non-nil, preserve the
  7008. size of WINDOW (see `window-preserve-size').
  7009. Fit pixelwise if the option `window-resize-pixelwise' is non-nil.
  7010. If WINDOW is its frame's root window and the option
  7011. `fit-frame-to-buffer' is non-nil, call `fit-frame-to-buffer' to
  7012. adjust the frame's size.
  7013. Note that even if this function makes WINDOW large enough to show
  7014. _all_ parts of its buffer you might not see the first part when
  7015. WINDOW was scrolled. If WINDOW is resized horizontally, you will
  7016. not see the top of its buffer unless WINDOW starts at its minimum
  7017. accessible position."
  7018. (interactive)
  7019. (setq window (window-normalize-window window t))
  7020. (if (eq window (frame-root-window window))
  7021. (when fit-frame-to-buffer
  7022. ;; Fit WINDOW's frame to buffer.
  7023. (fit-frame-to-buffer
  7024. (window-frame window)
  7025. max-height min-height max-width min-width
  7026. (and (memq fit-frame-to-buffer '(vertically horizontally))
  7027. fit-frame-to-buffer)))
  7028. (with-selected-window window
  7029. (let* ((pixelwise window-resize-pixelwise)
  7030. (char-height (frame-char-height))
  7031. (char-width (frame-char-width))
  7032. (total-height (window-size window nil pixelwise))
  7033. (body-height (window-body-height window pixelwise))
  7034. (body-width (window-body-width window pixelwise))
  7035. (min-height
  7036. ;; Sanitize MIN-HEIGHT.
  7037. (if (numberp min-height)
  7038. ;; Can't get smaller than `window-safe-min-height'.
  7039. (max (if pixelwise
  7040. (* char-height min-height)
  7041. min-height)
  7042. (if pixelwise
  7043. (window-safe-min-pixel-height window)
  7044. window-safe-min-height))
  7045. ;; Preserve header and mode line if present.
  7046. (max (if pixelwise
  7047. (* char-height window-min-height)
  7048. window-min-height)
  7049. (window-min-size window nil window pixelwise))))
  7050. (max-height
  7051. ;; Sanitize MAX-HEIGHT.
  7052. (if (numberp max-height)
  7053. (min
  7054. (+ total-height
  7055. (window-max-delta
  7056. window nil window nil nil nil pixelwise))
  7057. (if pixelwise
  7058. (* char-height max-height)
  7059. max-height))
  7060. (+ total-height (window-max-delta
  7061. window nil window nil nil nil pixelwise))))
  7062. height)
  7063. (cond
  7064. ;; If WINDOW is vertically combined, try to resize it
  7065. ;; vertically.
  7066. ((and (not (eq fit-window-to-buffer-horizontally 'only))
  7067. (not (window-size-fixed-p window 'preserved))
  7068. (window-combined-p))
  7069. ;; Vertically we always want to fit the entire buffer.
  7070. ;; WINDOW'S height can't get larger than its frame's pixel
  7071. ;; height. Its width remains fixed.
  7072. (setq height (+ (cdr (window-text-pixel-size
  7073. nil nil t nil (frame-pixel-height) t))
  7074. (window-scroll-bar-height window)
  7075. (window-bottom-divider-width)))
  7076. ;; Round height.
  7077. (unless pixelwise
  7078. (setq height (/ (+ height char-height -1) char-height)))
  7079. (unless (= height total-height)
  7080. (window-preserve-size window)
  7081. (window-resize-no-error
  7082. window
  7083. (- (max min-height (min max-height height)) total-height)
  7084. nil window pixelwise)
  7085. (when preserve-size
  7086. (window-preserve-size window nil t))))
  7087. ;; If WINDOW is horizontally combined, try to resize it
  7088. ;; horizontally.
  7089. ((and fit-window-to-buffer-horizontally
  7090. (not (window-size-fixed-p window t 'preserved))
  7091. (window-combined-p nil t))
  7092. (let* ((total-width (window-size window t pixelwise))
  7093. (min-width
  7094. ;; Sanitize MIN-WIDTH.
  7095. (if (numberp min-width)
  7096. ;; Can't get smaller than `window-safe-min-width'.
  7097. (max (if pixelwise
  7098. (* char-width min-width)
  7099. min-width)
  7100. (if pixelwise
  7101. (window-safe-min-pixel-width)
  7102. window-safe-min-width))
  7103. ;; Preserve fringes, margins, scrollbars if present.
  7104. (max (if pixelwise
  7105. (* char-width window-min-width)
  7106. window-min-width)
  7107. (window-min-size nil nil window pixelwise))))
  7108. (max-width
  7109. ;; Sanitize MAX-WIDTH.
  7110. (if (numberp max-width)
  7111. (min (+ total-width
  7112. (window-max-delta
  7113. window t window nil nil nil pixelwise))
  7114. (if pixelwise
  7115. (* char-width max-width)
  7116. max-width))
  7117. (+ total-width (window-max-delta
  7118. window t window nil nil nil pixelwise))))
  7119. ;; When fitting horizontally, assume that WINDOW's
  7120. ;; start position remains unaltered. WINDOW can't get
  7121. ;; wider than its frame's pixel width, its height
  7122. ;; remains unaltered.
  7123. (width (+ (car (window-text-pixel-size
  7124. nil (window-start) (point-max)
  7125. (frame-pixel-width)
  7126. ;; Add one char-height to assure that
  7127. ;; we're on the safe side. This
  7128. ;; overshoots when the first line below
  7129. ;; the bottom is wider than the window.
  7130. (* body-height
  7131. (if pixelwise 1 char-height))))
  7132. (window-right-divider-width))))
  7133. (unless pixelwise
  7134. (setq width (/ (+ width char-width -1) char-width)))
  7135. (unless (= width body-width)
  7136. (window-preserve-size window t)
  7137. (window-resize-no-error
  7138. window
  7139. (- (max min-width
  7140. (min max-width
  7141. (+ total-width (- width body-width))))
  7142. total-width)
  7143. t window pixelwise)
  7144. (when preserve-size
  7145. (window-preserve-size window t t))))))))))
  7146. (defun window-safely-shrinkable-p (&optional window)
  7147. "Return t if WINDOW can be shrunk without shrinking other windows.
  7148. WINDOW defaults to the selected window."
  7149. (with-selected-window (or window (selected-window))
  7150. (let ((edges (window-edges)))
  7151. (or (= (nth 2 edges) (nth 2 (window-edges (previous-window))))
  7152. (= (nth 0 edges) (nth 0 (window-edges (next-window))))))))
  7153. (defun shrink-window-if-larger-than-buffer (&optional window)
  7154. "Shrink height of WINDOW if its buffer doesn't need so many lines.
  7155. More precisely, shrink WINDOW vertically to be as small as
  7156. possible, while still showing the full contents of its buffer.
  7157. WINDOW must be a live window and defaults to the selected one.
  7158. Do not shrink WINDOW to less than `window-min-height' lines. Do
  7159. nothing if the buffer contains more lines than the present window
  7160. height, or if some of the window's contents are scrolled out of
  7161. view, or if shrinking this window would also shrink another
  7162. window, or if the window is the only window of its frame.
  7163. Return non-nil if the window was shrunk, nil otherwise."
  7164. (interactive)
  7165. (setq window (window-normalize-window window t))
  7166. ;; Make sure that WINDOW is vertically combined and `point-min' is
  7167. ;; visible (for whatever reason that's needed). The remaining issues
  7168. ;; should be taken care of by `fit-window-to-buffer'.
  7169. (when (and (window-combined-p window)
  7170. (pos-visible-in-window-p (point-min) window))
  7171. (fit-window-to-buffer window (window-total-height window))))
  7172. (defun kill-buffer-and-window ()
  7173. "Kill the current buffer and delete the selected window."
  7174. (interactive)
  7175. (let ((window-to-delete (selected-window))
  7176. (buffer-to-kill (current-buffer))
  7177. (delete-window-hook (lambda () (ignore-errors (delete-window)))))
  7178. (unwind-protect
  7179. (progn
  7180. (add-hook 'kill-buffer-hook delete-window-hook t t)
  7181. (if (kill-buffer (current-buffer))
  7182. ;; If `delete-window' failed before, we rerun it to regenerate
  7183. ;; the error so it can be seen in the echo area.
  7184. (when (eq (selected-window) window-to-delete)
  7185. (delete-window))))
  7186. ;; If the buffer is not dead for some reason (probably because
  7187. ;; of a `quit' signal), remove the hook again.
  7188. (ignore-errors
  7189. (with-current-buffer buffer-to-kill
  7190. (remove-hook 'kill-buffer-hook delete-window-hook t))))))
  7191. (defvar recenter-last-op nil
  7192. "Indicates the last recenter operation performed.
  7193. Possible values: `top', `middle', `bottom', integer or float numbers.
  7194. It can also be nil, which means the first value in `recenter-positions'.")
  7195. (defcustom recenter-positions '(middle top bottom)
  7196. "Cycling order for `recenter-top-bottom'.
  7197. A list of elements with possible values `top', `middle', `bottom',
  7198. integer or float numbers that define the cycling order for
  7199. the command `recenter-top-bottom'.
  7200. Top and bottom destinations are `scroll-margin' lines from the true
  7201. window top and bottom. Middle redraws the frame and centers point
  7202. vertically within the window. Integer number moves current line to
  7203. the specified absolute window-line. Float number between 0.0 and 1.0
  7204. means the percentage of the screen space from the top. The default
  7205. cycling order is middle -> top -> bottom."
  7206. :type '(repeat (choice
  7207. (const :tag "Top" top)
  7208. (const :tag "Middle" middle)
  7209. (const :tag "Bottom" bottom)
  7210. (integer :tag "Line number")
  7211. (float :tag "Percentage")))
  7212. :version "23.2"
  7213. :group 'windows)
  7214. (defun recenter-top-bottom (&optional arg)
  7215. "Move current buffer line to the specified window line.
  7216. With no prefix argument, successive calls place point according
  7217. to the cycling order defined by `recenter-positions'.
  7218. A prefix argument is handled like `recenter':
  7219. With numeric prefix ARG, move current line to window-line ARG.
  7220. With plain `C-u', move current line to window center."
  7221. (interactive "P")
  7222. (cond
  7223. (arg (recenter arg)) ; Always respect ARG.
  7224. (t
  7225. (setq recenter-last-op
  7226. (if (eq this-command last-command)
  7227. (car (or (cdr (member recenter-last-op recenter-positions))
  7228. recenter-positions))
  7229. (car recenter-positions)))
  7230. (let ((this-scroll-margin
  7231. (min (max 0 scroll-margin)
  7232. (truncate (/ (window-body-height) 4.0)))))
  7233. (cond ((eq recenter-last-op 'middle)
  7234. (recenter))
  7235. ((eq recenter-last-op 'top)
  7236. (recenter this-scroll-margin))
  7237. ((eq recenter-last-op 'bottom)
  7238. (recenter (- -1 this-scroll-margin)))
  7239. ((integerp recenter-last-op)
  7240. (recenter recenter-last-op))
  7241. ((floatp recenter-last-op)
  7242. (recenter (round (* recenter-last-op (window-height))))))))))
  7243. (define-key global-map [?\C-l] 'recenter-top-bottom)
  7244. (defun move-to-window-line-top-bottom (&optional arg)
  7245. "Position point relative to window.
  7246. With a prefix argument ARG, acts like `move-to-window-line'.
  7247. With no argument, positions point at center of window.
  7248. Successive calls position point at positions defined
  7249. by `recenter-positions'."
  7250. (interactive "P")
  7251. (cond
  7252. (arg (move-to-window-line arg)) ; Always respect ARG.
  7253. (t
  7254. (setq recenter-last-op
  7255. (if (eq this-command last-command)
  7256. (car (or (cdr (member recenter-last-op recenter-positions))
  7257. recenter-positions))
  7258. (car recenter-positions)))
  7259. (let ((this-scroll-margin
  7260. (min (max 0 scroll-margin)
  7261. (truncate (/ (window-body-height) 4.0)))))
  7262. (cond ((eq recenter-last-op 'middle)
  7263. (call-interactively 'move-to-window-line))
  7264. ((eq recenter-last-op 'top)
  7265. (move-to-window-line this-scroll-margin))
  7266. ((eq recenter-last-op 'bottom)
  7267. (move-to-window-line (- -1 this-scroll-margin)))
  7268. ((integerp recenter-last-op)
  7269. (move-to-window-line recenter-last-op))
  7270. ((floatp recenter-last-op)
  7271. (move-to-window-line (round (* recenter-last-op (window-height))))))))))
  7272. (define-key global-map [?\M-r] 'move-to-window-line-top-bottom)
  7273. ;;; Scrolling commands.
  7274. ;;; Scrolling commands which do not signal errors at top/bottom
  7275. ;;; of buffer at first key-press (instead move to top/bottom
  7276. ;;; of buffer).
  7277. (defcustom scroll-error-top-bottom nil
  7278. "Move point to top/bottom of buffer before signaling a scrolling error.
  7279. A value of nil means just signal an error if no more scrolling possible.
  7280. A value of t means point moves to the beginning or the end of the buffer
  7281. \(depending on scrolling direction) when no more scrolling possible.
  7282. When point is already on that position, then signal an error."
  7283. :type 'boolean
  7284. :group 'windows
  7285. :version "24.1")
  7286. (defun scroll-up-command (&optional arg)
  7287. "Scroll text of selected window upward ARG lines; or near full screen if no ARG.
  7288. If `scroll-error-top-bottom' is non-nil and `scroll-up' cannot
  7289. scroll window further, move cursor to the bottom line.
  7290. When point is already on that position, then signal an error.
  7291. A near full screen is `next-screen-context-lines' less than a full screen.
  7292. Negative ARG means scroll downward.
  7293. If ARG is the atom `-', scroll downward by nearly full screen."
  7294. (interactive "^P")
  7295. (cond
  7296. ((null scroll-error-top-bottom)
  7297. (scroll-up arg))
  7298. ((eq arg '-)
  7299. (scroll-down-command nil))
  7300. ((< (prefix-numeric-value arg) 0)
  7301. (scroll-down-command (- (prefix-numeric-value arg))))
  7302. ((eobp)
  7303. (scroll-up arg)) ; signal error
  7304. (t
  7305. (condition-case nil
  7306. (scroll-up arg)
  7307. (end-of-buffer
  7308. (if arg
  7309. ;; When scrolling by ARG lines can't be done,
  7310. ;; move by ARG lines instead.
  7311. (forward-line arg)
  7312. ;; When ARG is nil for full-screen scrolling,
  7313. ;; move to the bottom of the buffer.
  7314. (goto-char (point-max))))))))
  7315. (put 'scroll-up-command 'scroll-command t)
  7316. (defun scroll-down-command (&optional arg)
  7317. "Scroll text of selected window down ARG lines; or near full screen if no ARG.
  7318. If `scroll-error-top-bottom' is non-nil and `scroll-down' cannot
  7319. scroll window further, move cursor to the top line.
  7320. When point is already on that position, then signal an error.
  7321. A near full screen is `next-screen-context-lines' less than a full screen.
  7322. Negative ARG means scroll upward.
  7323. If ARG is the atom `-', scroll upward by nearly full screen."
  7324. (interactive "^P")
  7325. (cond
  7326. ((null scroll-error-top-bottom)
  7327. (scroll-down arg))
  7328. ((eq arg '-)
  7329. (scroll-up-command nil))
  7330. ((< (prefix-numeric-value arg) 0)
  7331. (scroll-up-command (- (prefix-numeric-value arg))))
  7332. ((bobp)
  7333. (scroll-down arg)) ; signal error
  7334. (t
  7335. (condition-case nil
  7336. (scroll-down arg)
  7337. (beginning-of-buffer
  7338. (if arg
  7339. ;; When scrolling by ARG lines can't be done,
  7340. ;; move by ARG lines instead.
  7341. (forward-line (- arg))
  7342. ;; When ARG is nil for full-screen scrolling,
  7343. ;; move to the top of the buffer.
  7344. (goto-char (point-min))))))))
  7345. (put 'scroll-down-command 'scroll-command t)
  7346. ;;; Scrolling commands which scroll a line instead of full screen.
  7347. (defun scroll-up-line (&optional arg)
  7348. "Scroll text of selected window upward ARG lines; or one line if no ARG.
  7349. If ARG is omitted or nil, scroll upward by one line.
  7350. This is different from `scroll-up-command' that scrolls a full screen."
  7351. (interactive "p")
  7352. (scroll-up (or arg 1)))
  7353. (put 'scroll-up-line 'scroll-command t)
  7354. (defun scroll-down-line (&optional arg)
  7355. "Scroll text of selected window down ARG lines; or one line if no ARG.
  7356. If ARG is omitted or nil, scroll down by one line.
  7357. This is different from `scroll-down-command' that scrolls a full screen."
  7358. (interactive "p")
  7359. (scroll-down (or arg 1)))
  7360. (put 'scroll-down-line 'scroll-command t)
  7361. (defun scroll-other-window-down (&optional lines)
  7362. "Scroll the \"other window\" down.
  7363. For more details, see the documentation for `scroll-other-window'."
  7364. (interactive "P")
  7365. (scroll-other-window
  7366. ;; Just invert the argument's meaning.
  7367. ;; We can do that without knowing which window it will be.
  7368. (if (eq lines '-) nil
  7369. (if (null lines) '-
  7370. (- (prefix-numeric-value lines))))))
  7371. (defun beginning-of-buffer-other-window (arg)
  7372. "Move point to the beginning of the buffer in the other window.
  7373. Leave mark at previous position.
  7374. With arg N, put point N/10 of the way from the true beginning."
  7375. (interactive "P")
  7376. (let ((orig-window (selected-window))
  7377. (window (other-window-for-scrolling)))
  7378. ;; We use unwind-protect rather than save-window-excursion
  7379. ;; because the latter would preserve the things we want to change.
  7380. (unwind-protect
  7381. (progn
  7382. (select-window window)
  7383. ;; Set point and mark in that window's buffer.
  7384. (with-no-warnings
  7385. (beginning-of-buffer arg))
  7386. ;; Set point accordingly.
  7387. (recenter '(t)))
  7388. (select-window orig-window))))
  7389. (defun end-of-buffer-other-window (arg)
  7390. "Move point to the end of the buffer in the other window.
  7391. Leave mark at previous position.
  7392. With arg N, put point N/10 of the way from the true end."
  7393. (interactive "P")
  7394. ;; See beginning-of-buffer-other-window for comments.
  7395. (let ((orig-window (selected-window))
  7396. (window (other-window-for-scrolling)))
  7397. (unwind-protect
  7398. (progn
  7399. (select-window window)
  7400. (with-no-warnings
  7401. (end-of-buffer arg))
  7402. (recenter '(t)))
  7403. (select-window orig-window))))
  7404. (defvar mouse-autoselect-window-timer nil
  7405. "Timer used by delayed window autoselection.")
  7406. (defvar mouse-autoselect-window-position-1 nil
  7407. "First mouse position recorded by delayed window autoselection.")
  7408. (defvar mouse-autoselect-window-position nil
  7409. "Last mouse position recorded by delayed window autoselection.")
  7410. (defvar mouse-autoselect-window-window nil
  7411. "Last window recorded by delayed window autoselection.")
  7412. (defvar mouse-autoselect-window-state nil
  7413. "When non-nil, special state of delayed window autoselection.
  7414. Possible values are `suspend' (suspend autoselection after a menu or
  7415. scrollbar interaction) and `select' (the next invocation of
  7416. `handle-select-window' shall select the window immediately).")
  7417. (defun mouse-autoselect-window-cancel (&optional force)
  7418. "Cancel delayed window autoselection.
  7419. Optional argument FORCE means cancel unconditionally."
  7420. (unless (and (not force)
  7421. ;; Don't cancel for select-window or select-frame events
  7422. ;; or when the user drags a scroll bar.
  7423. (or (memq this-command
  7424. '(handle-select-window handle-switch-frame))
  7425. (and (eq this-command 'scroll-bar-toolkit-scroll)
  7426. (memq (nth 4 (event-end last-input-event))
  7427. '(handle end-scroll)))))
  7428. (setq mouse-autoselect-window-state nil)
  7429. (setq mouse-autoselect-window-position-1 nil)
  7430. (when (timerp mouse-autoselect-window-timer)
  7431. (cancel-timer mouse-autoselect-window-timer))
  7432. (remove-hook 'pre-command-hook 'mouse-autoselect-window-cancel)))
  7433. (defun mouse-autoselect-window-start (mouse-position &optional window suspend)
  7434. "Start delayed window autoselection.
  7435. MOUSE-POSITION is the last position where the mouse was seen as returned
  7436. by `mouse-position'. Optional argument WINDOW non-nil denotes the
  7437. window where the mouse was seen. Optional argument SUSPEND non-nil
  7438. means suspend autoselection."
  7439. ;; Record values for MOUSE-POSITION, WINDOW, and SUSPEND.
  7440. (setq mouse-autoselect-window-position mouse-position)
  7441. (when window (setq mouse-autoselect-window-window window))
  7442. (setq mouse-autoselect-window-state (when suspend 'suspend))
  7443. ;; Install timer which runs `mouse-autoselect-window-select' after
  7444. ;; `mouse-autoselect-window' seconds.
  7445. (setq mouse-autoselect-window-timer
  7446. (run-at-time
  7447. (abs mouse-autoselect-window) nil 'mouse-autoselect-window-select)))
  7448. (defun mouse-autoselect-window-select ()
  7449. "Select window with delayed window autoselection.
  7450. If the mouse position has stabilized in a non-selected window, select
  7451. that window. The minibuffer window is selected only if the minibuffer
  7452. is active. This function is run by `mouse-autoselect-window-timer'."
  7453. (ignore-errors
  7454. (let* ((mouse-position (mouse-position))
  7455. (window
  7456. (ignore-errors
  7457. (window-at (cadr mouse-position) (cddr mouse-position)
  7458. (car mouse-position)))))
  7459. (cond
  7460. ((or (and (fboundp 'menu-or-popup-active-p) (menu-or-popup-active-p))
  7461. (and window
  7462. (let ((coords (coordinates-in-window-p
  7463. (cdr mouse-position) window)))
  7464. (and (not (consp coords))
  7465. (not (memq coords '(left-margin right-margin)))))))
  7466. ;; A menu / popup dialog is active or the mouse is not on the
  7467. ;; text region of WINDOW: Suspend autoselection temporarily.
  7468. (mouse-autoselect-window-start mouse-position nil t))
  7469. ((or (eq mouse-autoselect-window-state 'suspend)
  7470. ;; When the mouse is at its first recorded position, restart
  7471. ;; delayed autoselection. This works around a scenario with
  7472. ;; two two-window frames with identical dimensions: select the
  7473. ;; first window of the first frame, switch to the second
  7474. ;; frame, move the mouse to its second window, minimize the
  7475. ;; second frame. Now the second window of the first frame
  7476. ;; gets selected although the mouse never really "moved" into
  7477. ;; that window.
  7478. (and (numberp mouse-autoselect-window)
  7479. (equal (mouse-position) mouse-autoselect-window-position-1)))
  7480. ;; Delayed autoselection was temporarily suspended, reenable it.
  7481. (mouse-autoselect-window-start mouse-position))
  7482. ((and window (not (eq window (selected-window)))
  7483. (or (not (numberp mouse-autoselect-window))
  7484. (and (>= mouse-autoselect-window 0)
  7485. ;; If `mouse-autoselect-window' is non-negative,
  7486. ;; select window if it's the same as before.
  7487. (eq window mouse-autoselect-window-window))
  7488. ;; Otherwise select window iff the mouse is at the same
  7489. ;; position as before. Observe that the first test
  7490. ;; after starting autoselection usually fails since the
  7491. ;; value of `mouse-autoselect-window-position' recorded
  7492. ;; there is the position where the mouse has entered the
  7493. ;; new window and not necessarily where the mouse has
  7494. ;; stopped moving.
  7495. (equal mouse-position mouse-autoselect-window-position))
  7496. ;; The minibuffer is a candidate window if it's active.
  7497. (or (not (window-minibuffer-p window))
  7498. (eq window (active-minibuffer-window))))
  7499. ;; Mouse position has stabilized in non-selected window: Cancel
  7500. ;; delayed autoselection and try to select that window.
  7501. (mouse-autoselect-window-cancel t)
  7502. ;; Select window where mouse appears unless the selected window is the
  7503. ;; minibuffer. Use `unread-command-events' in order to execute pre-
  7504. ;; and post-command hooks and trigger idle timers. To avoid delaying
  7505. ;; autoselection again, set `mouse-autoselect-window-state'."
  7506. (unless (window-minibuffer-p)
  7507. (setq mouse-autoselect-window-state 'select)
  7508. (setq unread-command-events
  7509. (cons (list 'select-window (list window))
  7510. unread-command-events))))
  7511. ((or (and window (eq window (selected-window)))
  7512. (not (numberp mouse-autoselect-window))
  7513. (equal mouse-position mouse-autoselect-window-position))
  7514. ;; Mouse position has either stabilized in the selected window or at
  7515. ;; `mouse-autoselect-window-position': Cancel delayed autoselection.
  7516. (mouse-autoselect-window-cancel t))
  7517. (t
  7518. ;; Mouse position has not stabilized yet, resume delayed
  7519. ;; autoselection.
  7520. (mouse-autoselect-window-start mouse-position window))))))
  7521. (defun handle-select-window (event)
  7522. "Handle select-window events."
  7523. (interactive "^e")
  7524. (let ((window (posn-window (event-start event))))
  7525. (unless (or (not (window-live-p window))
  7526. ;; Don't switch if we're currently in the minibuffer.
  7527. ;; This tries to work around problems where the
  7528. ;; minibuffer gets unselected unexpectedly, and where
  7529. ;; you then have to move your mouse all the way down to
  7530. ;; the minibuffer to select it.
  7531. (window-minibuffer-p)
  7532. ;; Don't switch to minibuffer window unless it's active.
  7533. (and (window-minibuffer-p window)
  7534. (not (minibuffer-window-active-p window)))
  7535. ;; Don't switch when autoselection shall be delayed.
  7536. (and (numberp mouse-autoselect-window)
  7537. (not (eq mouse-autoselect-window-state 'select))
  7538. (let ((position (mouse-position)))
  7539. ;; Cancel any delayed autoselection.
  7540. (mouse-autoselect-window-cancel t)
  7541. ;; Start delayed autoselection from current mouse
  7542. ;; position and window.
  7543. (setq mouse-autoselect-window-position-1 position)
  7544. (mouse-autoselect-window-start position window)
  7545. ;; Executing a command cancels delayed autoselection.
  7546. (add-hook
  7547. 'pre-command-hook 'mouse-autoselect-window-cancel))))
  7548. (when mouse-autoselect-window
  7549. ;; Reset state of delayed autoselection.
  7550. (setq mouse-autoselect-window-state nil)
  7551. ;; Run `mouse-leave-buffer-hook' when autoselecting window.
  7552. (run-hooks 'mouse-leave-buffer-hook))
  7553. ;; Clear echo area.
  7554. (message nil)
  7555. (select-window window))))
  7556. (defun truncated-partial-width-window-p (&optional window)
  7557. "Return non-nil if lines in WINDOW are specifically truncated due to its width.
  7558. WINDOW must be a live window and defaults to the selected one.
  7559. Return nil if WINDOW is not a partial-width window
  7560. (regardless of the value of `truncate-lines').
  7561. Otherwise, consult the value of `truncate-partial-width-windows'
  7562. for the buffer shown in WINDOW."
  7563. (setq window (window-normalize-window window t))
  7564. (unless (window-full-width-p window)
  7565. (let ((t-p-w-w (buffer-local-value 'truncate-partial-width-windows
  7566. (window-buffer window))))
  7567. (if (integerp t-p-w-w)
  7568. (< (window-width window) t-p-w-w)
  7569. t-p-w-w))))
  7570. ;; Automatically inform subprocesses of changes to window size.
  7571. (defcustom window-adjust-process-window-size-function
  7572. 'window-adjust-process-window-size-smallest
  7573. "Control how Emacs chooses inferior process window sizes.
  7574. Emacs uses this function to tell processes the space they have
  7575. available for displaying their output. After each window
  7576. configuration change, Emacs calls the value of
  7577. `window-adjust-process-window-size-function' for each process
  7578. with a buffer being displayed in at least one window.
  7579. This function is responsible for combining the sizes of the
  7580. displayed windows and returning a cons (WIDTH . HEIGHT)
  7581. describing the width and height with which Emacs will call
  7582. `set-process-window-size' for that process. If the function
  7583. returns nil, Emacs does not call `set-process-window-size'.
  7584. This function is called with the process buffer as the current
  7585. buffer and with two arguments: the process and a list of windows
  7586. displaying process. Modes can make this variable buffer-local;
  7587. additionally, the `adjust-window-size-function' process property
  7588. overrides the global or buffer-local value of
  7589. `window-adjust-process-window-size-function'."
  7590. :type '(choice
  7591. (const :tag "Minimum area of any window"
  7592. window-adjust-process-window-size-smallest)
  7593. (const :tag "Maximum area of any window"
  7594. window-adjust-process-window-size-largest)
  7595. (const :tag "Do not adjust process window sizes" ignore)
  7596. function)
  7597. :group 'windows
  7598. :version "25.1")
  7599. (defun window-adjust-process-window-size (reducer process windows)
  7600. "Adjust the process window size of PROCESS.
  7601. WINDOWS is a list of windows associated with PROCESS. REDUCER is
  7602. a two-argument function used to combine the widths and heights of
  7603. the given windows."
  7604. (when windows
  7605. (let ((width (window-body-width (car windows)))
  7606. (height (window-body-height (car windows))))
  7607. (dolist (window (cdr windows))
  7608. (setf width (funcall reducer width (window-body-width window)))
  7609. (setf height (funcall reducer height (window-body-height window))))
  7610. (cons width height))))
  7611. (defun window-adjust-process-window-size-smallest (process windows)
  7612. "Adjust the process window size of PROCESS.
  7613. WINDOWS is a list of windows associated with PROCESS. Choose the
  7614. smallest area available for displaying PROCESS's output."
  7615. (window-adjust-process-window-size #'min process windows))
  7616. (defun window-adjust-process-window-size-largest (process windows)
  7617. "Adjust the process window size of PROCESS.
  7618. WINDOWS is a list of windows associated with PROCESS. Choose the
  7619. largest area available for displaying PROCESS's output."
  7620. (window-adjust-process-window-size #'max process windows))
  7621. (defun window--process-window-list ()
  7622. "Return an alist mapping processes to associated windows.
  7623. A window is associated with a process if that window is
  7624. displaying that processes's buffer."
  7625. (let ((processes (process-list))
  7626. (process-windows nil))
  7627. (walk-windows
  7628. (lambda (window)
  7629. (let ((buffer (window-buffer window))
  7630. (iter processes))
  7631. (while (let ((process (car iter)))
  7632. (if (and (process-live-p process)
  7633. (eq buffer (process-buffer process)))
  7634. (let ((procwin (assq process process-windows)))
  7635. ;; Add this window to the list of windows
  7636. ;; displaying process.
  7637. (if procwin
  7638. (push window (cdr procwin))
  7639. (push (list process window) process-windows))
  7640. ;; We found our process for this window, so
  7641. ;; stop iterating over the process list.
  7642. nil)
  7643. (setf iter (cdr iter)))))))
  7644. 1 t)
  7645. process-windows))
  7646. (defun window--adjust-process-windows ()
  7647. "Update process window sizes to match the current window configuration."
  7648. (dolist (procwin (window--process-window-list))
  7649. (let ((process (car procwin)))
  7650. (with-demoted-errors "Error adjusting window size: %S"
  7651. (with-current-buffer (process-buffer process)
  7652. (let ((size (funcall
  7653. (or (process-get process 'adjust-window-size-function)
  7654. window-adjust-process-window-size-function)
  7655. process (cdr procwin))))
  7656. (when size
  7657. (set-process-window-size process (cdr size) (car size)))))))))
  7658. (add-hook 'window-configuration-change-hook 'window--adjust-process-windows)
  7659. ;; Some of these are in tutorial--default-keys, so update that if you
  7660. ;; change these.
  7661. (define-key ctl-x-map "0" 'delete-window)
  7662. (define-key ctl-x-map "1" 'delete-other-windows)
  7663. (define-key ctl-x-map "2" 'split-window-below)
  7664. (define-key ctl-x-map "3" 'split-window-right)
  7665. (define-key ctl-x-map "o" 'other-window)
  7666. (define-key ctl-x-map "^" 'enlarge-window)
  7667. (define-key ctl-x-map "}" 'enlarge-window-horizontally)
  7668. (define-key ctl-x-map "{" 'shrink-window-horizontally)
  7669. (define-key ctl-x-map "-" 'shrink-window-if-larger-than-buffer)
  7670. (define-key ctl-x-map "+" 'balance-windows)
  7671. (define-key ctl-x-4-map "0" 'kill-buffer-and-window)
  7672. ;;; window.el ends here