allout.el 282 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954
  1. ;;; allout.el --- extensive outline mode for use alone and with other modes
  2. ;; Copyright (C) 1992-1994, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Ken Manheimer <ken dot manheimer at gmail...>
  4. ;; Maintainer: Ken Manheimer <ken dot manheimer at gmail...>
  5. ;; Created: Dec 1991 -- first release to usenet
  6. ;; Version: 2.3
  7. ;; Keywords: outlines, wp, languages, PGP, GnuPG
  8. ;; Website: http://myriadicity.net/software-and-systems/craft/emacs-allout
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Allout outline minor mode provides extensive outline formatting and
  22. ;; and manipulation beyond standard emacs outline mode. Some features:
  23. ;;
  24. ;; - Classic outline-mode topic-oriented navigation and exposure adjustment
  25. ;; - Topic-oriented editing including coherent topic and subtopic
  26. ;; creation, promotion, demotion, cut/paste across depths, etc.
  27. ;; - Incremental search with dynamic exposure and reconcealment of text
  28. ;; - Customizable bullet format -- enables programming-language specific
  29. ;; outlining, for code-folding editing. (Allout code itself is to try it;
  30. ;; formatted as an outline -- do ESC-x eval-buffer in allout.el; but
  31. ;; emacs local file variables need to be enabled when the
  32. ;; file was visited -- see `enable-local-variables'.)
  33. ;; - Configurable per-file initial exposure settings
  34. ;; - Symmetric-key and key-pair topic encryption. Encryption is via the
  35. ;; Emacs 'epg' library. See allout-toggle-current-subtree-encryption
  36. ;; docstring.
  37. ;; - Automatic topic-number maintenance
  38. ;; - "Hot-spot" operation, for single-keystroke maneuvering and
  39. ;; exposure control (see the allout-mode docstring)
  40. ;; - Easy rendering of exposed portions into numbered, latex, indented, etc
  41. ;; outline styles
  42. ;; - Careful attention to whitespace -- enabling blank lines between items
  43. ;; and maintenance of hanging indentation (in paragraph auto-fill and
  44. ;; across topic promotion and demotion) of topic bodies consistent with
  45. ;; indentation of their topic header.
  46. ;;
  47. ;; and more.
  48. ;;
  49. ;; See the `allout-mode' function's docstring for an introduction to the
  50. ;; mode.
  51. ;;
  52. ;; Directions to the latest development version and helpful notes are
  53. ;; available at http://myriadicity.net/Sundry/EmacsAllout .
  54. ;;
  55. ;; The outline menubar additions provide quick reference to many of the
  56. ;; features. See the docstring of the variables `allout-layout' and
  57. ;; `allout-auto-activation' for details on automatic activation of
  58. ;; `allout-mode' as a minor mode. (`allout-init' is deprecated in favor of
  59. ;; a purely customization-based method.)
  60. ;;
  61. ;; Note -- the lines beginning with `;;;_' are outline topic headers.
  62. ;; Customize `allout-auto-activation' to enable, then revisit this
  63. ;; buffer to give it a whirl.
  64. ;; ken manheimer (ken dot manheimer at gmail dot com)
  65. ;;; Code:
  66. (declare-function epa-passphrase-callback-function
  67. "epa" (context key-id handback))
  68. ;;;_* Dependency loads
  69. (require 'overlay)
  70. (eval-when-compile
  71. ;; `cl' is required for `assert'. `assert' is not covered by a standard
  72. ;; autoload, but it is a macro, so that eval-when-compile is sufficient
  73. ;; to byte-compile it in, or to do the require when the buffer evalled.
  74. (require 'cl)
  75. )
  76. ;;;_* USER CUSTOMIZATION VARIABLES:
  77. ;;;_ > defgroup allout, allout-keybindings
  78. (defgroup allout nil
  79. "Extensive outline minor-mode, for use stand-alone and with other modes.
  80. See Allout Auto Activation for automatic activation."
  81. :prefix "allout-"
  82. :group 'outlines)
  83. (defgroup allout-keybindings nil
  84. "Allout outline mode keyboard bindings configuration."
  85. :group 'allout)
  86. ;;;_ + Layout, Mode, and Topic Header Configuration
  87. (defvar allout-command-prefix) ; defined below
  88. ;;;_ > allout-keybindings incidentals:
  89. ;;;_ : internal key binding stuff - in this section for load-order.
  90. ;;;_ = allout-mode-map
  91. (defvar allout-mode-map 'allout-mode-map
  92. "Keybindings place-holder for (allout) outline minor mode.
  93. Do NOT set the value of this variable. Instead, customize
  94. `allout-command-prefix', `allout-prefixed-keybindings', and
  95. `allout-unprefixed-keybindings'.")
  96. ;;;_ = allout-mode-map-value
  97. (defvar allout-mode-map-value nil
  98. "Keymap for allout outline minor mode.
  99. Do NOT set the value of this variable. Instead, customize
  100. `allout-command-prefix', `allout-prefixed-keybindings', and
  101. `allout-unprefixed-keybindings'.")
  102. ;;;_ = make allout-mode-map-value an alias for allout-mode-map:
  103. ;; this needs to be revised when the value is changed, sigh.
  104. (defalias 'allout-mode-map allout-mode-map-value)
  105. ;;;_ > allout-compose-and-institute-keymap (&optional varname value)
  106. (defun allout-compose-and-institute-keymap (&optional varname value)
  107. "Create the allout keymap according to the keybinding specs, and set it.
  108. Useful standalone or to effect customizations of the
  109. respective allout-mode keybinding variables, `allout-command-prefix',
  110. `allout-prefixed-keybindings', and `allout-unprefixed-keybindings'"
  111. ;; Set the customization variable, if any:
  112. (when varname
  113. (set-default varname value))
  114. (let ((map (make-sparse-keymap)))
  115. (when (boundp 'allout-prefixed-keybindings)
  116. ;; tolerate first definitions of the variables:
  117. (dolist (entry allout-prefixed-keybindings)
  118. (define-key map
  119. ;; XXX vector vs non-vector key descriptions?
  120. (vconcat allout-command-prefix
  121. (car (read-from-string (car entry))))
  122. (cadr entry))))
  123. (when (boundp 'allout-unprefixed-keybindings)
  124. (dolist (entry allout-unprefixed-keybindings)
  125. (define-key map (car (read-from-string (car entry))) (cadr entry))))
  126. (substitute-key-definition 'beginning-of-line 'allout-beginning-of-line
  127. map global-map)
  128. (substitute-key-definition 'move-beginning-of-line 'allout-beginning-of-line
  129. map global-map)
  130. (substitute-key-definition 'end-of-line 'allout-end-of-line
  131. map global-map)
  132. (substitute-key-definition 'move-end-of-line 'allout-end-of-line
  133. map global-map)
  134. (allout-institute-keymap map)))
  135. ;;;_ > allout-institute-keymap (map)
  136. (defun allout-institute-keymap (map)
  137. "Associate allout-mode bindings with allout as a minor mode."
  138. ;; Architecture:
  139. ;; allout-mode-map var is a keymap by virtue of being a defalias for
  140. ;; allout-mode-map-value, which has the actual keymap value.
  141. ;; allout-mode-map's symbol value is just 'allout-mode-map, so it can be
  142. ;; used in minor-mode-map-alist to indirect to the actual
  143. ;; allout-mode-map-var value, which can be adjusted and reassigned.
  144. ;; allout-mode-map-value for keymap reference in various places:
  145. (setq allout-mode-map-value map)
  146. ;; the function value keymap of allout-mode-map is used in
  147. ;; minor-mode-map-alist - update it:
  148. (fset allout-mode-map allout-mode-map-value))
  149. ;;;_ * initialize the mode map:
  150. ;; ensure that allout-mode-map has some setting even if allout-mode hasn't
  151. ;; been invoked:
  152. (allout-compose-and-institute-keymap)
  153. ;;;_ = allout-command-prefix
  154. (defcustom allout-command-prefix "\C-c "
  155. "Key sequence to be used as prefix for outline mode command key bindings.
  156. Default is `\C-c<space>'; just `\C-c' is more short-and-sweet, if you're
  157. willing to let allout use a bunch of \C-c keybindings."
  158. :type 'string
  159. :group 'allout-keybindings
  160. :set 'allout-compose-and-institute-keymap)
  161. ;;;_ = allout-keybindings-binding
  162. (define-widget 'allout-keybindings-binding 'lazy
  163. "Structure of allout keybindings customization items."
  164. :type '(repeat
  165. (list (string :tag "Key" :value "[(meta control shift ?f)]")
  166. (function :tag "Function name"
  167. :value allout-forward-current-level))))
  168. ;;;_ = allout-prefixed-keybindings
  169. (defcustom allout-prefixed-keybindings
  170. '(("[(control ?n)]" allout-next-visible-heading)
  171. ("[(control ?p)]" allout-previous-visible-heading)
  172. ("[(control ?u)]" allout-up-current-level)
  173. ("[(control ?f)]" allout-forward-current-level)
  174. ("[(control ?b)]" allout-backward-current-level)
  175. ("[(control ?a)]" allout-beginning-of-current-entry)
  176. ("[(control ?e)]" allout-end-of-entry)
  177. ("[(control ?i)]" allout-show-children)
  178. ("[(control ?s)]" allout-show-current-subtree)
  179. ("[(control ?t)]" allout-toggle-current-subtree-exposure)
  180. ;; Let user customize if they want to preempt describe-prefix-bindings ^h use.
  181. ;; ("[(control ?h)]" allout-hide-current-subtree)
  182. ("[?h]" allout-hide-current-subtree)
  183. ("[(control ?o)]" allout-show-current-entry)
  184. ("[?!]" allout-show-all)
  185. ("[?x]" allout-toggle-current-subtree-encryption)
  186. ("[? ]" allout-open-sibtopic)
  187. ("[?.]" allout-open-subtopic)
  188. ("[?,]" allout-open-supertopic)
  189. ("[?']" allout-shift-in)
  190. ("[?>]" allout-shift-in)
  191. ("[?<]" allout-shift-out)
  192. ("[(control ?m)]" allout-rebullet-topic)
  193. ("[?*]" allout-rebullet-current-heading)
  194. ("[?#]" allout-number-siblings)
  195. ("[(control ?k)]" allout-kill-topic)
  196. ("[(meta ?k)]" allout-copy-topic-as-kill)
  197. ("[?@]" allout-resolve-xref)
  198. ("[?=?c]" allout-copy-exposed-to-buffer)
  199. ("[?=?i]" allout-indented-exposed-to-buffer)
  200. ("[?=?t]" allout-latexify-exposed)
  201. ("[?=?p]" allout-flatten-exposed-to-buffer)
  202. )
  203. "Allout-mode key bindings that are prefixed with `allout-command-prefix'.
  204. See `allout-unprefixed-keybindings' for the list of keybindings
  205. that are not prefixed.
  206. Use vector format for the keys:
  207. - put literal keys after a `?' question mark, eg: `?a', `?.'
  208. - enclose control, shift, or meta-modified keys as sequences within
  209. parentheses, with the literal key, as above, preceded by the name(s)
  210. of the modifiers, eg: [(control ?a)]
  211. See the existing keys for examples.
  212. Functions can be bound to multiple keys, but binding keys to
  213. multiple functions will not work - the last binding for a key
  214. prevails."
  215. :version "24.1"
  216. :type 'allout-keybindings-binding
  217. :group 'allout-keybindings
  218. :set 'allout-compose-and-institute-keymap
  219. )
  220. ;;;_ = allout-unprefixed-keybindings
  221. (defcustom allout-unprefixed-keybindings
  222. '(("[(control ?k)]" allout-kill-line)
  223. ("[(meta ?k)]" allout-copy-line-as-kill)
  224. ("[(control ?y)]" allout-yank)
  225. ("[(meta ?y)]" allout-yank-pop)
  226. )
  227. "Allout-mode functions bound to keys without any added prefix.
  228. This is in contrast to the majority of allout-mode bindings on
  229. `allout-prefixed-bindings', whose bindings are created with a
  230. preceding command key.
  231. Use vector format for the keys:
  232. - put literal keys after a `?' question mark, eg: `?a', `?.'
  233. - enclose control, shift, or meta-modified keys as sequences within
  234. parentheses, with the literal key, as above, preceded by the name(s)
  235. of the modifiers, eg: [(control ?a)]
  236. See the existing keys for examples."
  237. :version "24.1"
  238. :type 'allout-keybindings-binding
  239. :group 'allout-keybindings
  240. :set 'allout-compose-and-institute-keymap
  241. )
  242. ;;;_ > allout-auto-activation-helper (var value)
  243. ;;;###autoload
  244. (defun allout-auto-activation-helper (var value)
  245. "Institute `allout-auto-activation'.
  246. Intended to be used as the `allout-auto-activation' :set function."
  247. (set-default var value)
  248. (allout-setup))
  249. ;;;_ > allout-setup ()
  250. ;;;###autoload
  251. (defun allout-setup ()
  252. "Do fundamental Emacs session for allout auto-activation.
  253. Establishes allout processing as part of visiting a file if
  254. `allout-auto-activation' is non-nil, or removes it otherwise.
  255. The proper way to use this is through customizing the setting of
  256. `allout-auto-activation'."
  257. (if (not allout-auto-activation)
  258. (remove-hook 'find-file-hook 'allout-find-file-hook)
  259. (add-hook 'find-file-hook 'allout-find-file-hook)))
  260. ;;;_ = allout-auto-activation
  261. ;;;###autoload
  262. (defcustom allout-auto-activation nil
  263. "Configure allout outline mode auto-activation.
  264. Control whether and how allout outline mode is automatically
  265. activated when files are visited with non-nil buffer-specific
  266. file variable `allout-layout'.
  267. When allout-auto-activation is \"On\" (t), allout mode is
  268. activated in buffers with non-nil `allout-layout', and the
  269. specified layout is applied.
  270. With value \"ask\", auto-mode-activation is enabled, and endorsement for
  271. performing auto-layout is asked of the user each time.
  272. With value \"activate\", only auto-mode-activation is enabled.
  273. Auto-layout is not.
  274. With value nil, inhibit any automatic allout-mode activation."
  275. :set 'allout-auto-activation-helper
  276. ;; FIXME: Using strings here is unusual and less efficient than symbols.
  277. :type '(choice (const :tag "On" t)
  278. (const :tag "Ask about layout" "ask")
  279. (const :tag "Mode only" "activate")
  280. (const :tag "Off" nil))
  281. :group 'allout)
  282. (allout-setup)
  283. ;;;_ = allout-default-layout
  284. (defcustom allout-default-layout '(-2 : 0)
  285. "Default allout outline layout specification.
  286. This setting specifies the outline exposure to use when
  287. `allout-layout' has the local value t. This docstring describes the
  288. layout specifications.
  289. A list value specifies a default layout for the current buffer,
  290. to be applied upon activation of `allout-mode'. Any non-nil
  291. value will automatically trigger `allout-mode', provided
  292. `allout-auto-activation' has been customized to enable it.
  293. The types of elements in the layout specification are:
  294. INTEGER -- dictate the relative depth to open the corresponding topic(s),
  295. where:
  296. -- negative numbers force the topic to be closed before opening
  297. to the absolute value of the number, so all siblings are open
  298. only to that level.
  299. -- positive numbers open to the relative depth indicated by the
  300. number, but do not force already opened subtopics to be closed.
  301. -- 0 means to close topic -- hide all subitems.
  302. : -- repeat spec -- apply the preceding element to all siblings at
  303. current level, *up to* those siblings that would be covered by specs
  304. following the `:' on the list. Ie, apply to all topics at level but
  305. trailing ones accounted for by trailing specs. (Only the first of
  306. multiple colons at the same level is honored -- later ones are ignored.)
  307. * -- completely exposes the topic, including bodies
  308. + -- exposes all subtopics, but not the bodies
  309. - -- exposes the body of the corresponding topic, but not subtopics
  310. LIST -- a nested layout spec, to be applied intricately to its
  311. corresponding item(s)
  312. Examples:
  313. (-2 : 0)
  314. Collapse the top-level topics to show their children and
  315. grandchildren, but completely collapse the final top-level topic.
  316. (-1 () : 1 0)
  317. Close the first topic so only the immediate subtopics are shown,
  318. leave the subsequent topics exposed as they are until the second
  319. second to last topic, which is exposed at least one level, and
  320. completely close the last topic.
  321. (-2 : -1 *)
  322. Expose children and grandchildren of all topics at current
  323. level except the last two; expose children of the second to
  324. last and completely expose the last one, including its subtopics.
  325. See `allout-expose-topic' for more about the exposure process.
  326. Also, allout's mode-specific provisions will make topic prefixes default
  327. to the comment-start string, if any, of the language of the file. This
  328. is modulo the setting of `allout-use-mode-specific-leader', which see."
  329. :type 'allout-layout-type
  330. :group 'allout)
  331. ;;;_ : allout-layout-type
  332. (define-widget 'allout-layout-type 'lazy
  333. "Allout layout format customization basic building blocks."
  334. :type '(repeat
  335. (choice (integer :tag "integer (<= zero is strict)")
  336. (const :tag ": (repeat prior)" :)
  337. (const :tag "* (completely expose)" *)
  338. (const :tag "+ (expose all offspring, headlines only)" +)
  339. (const :tag "- (expose topic body but not offspring)" -)
  340. (allout-layout-type :tag "<Nested layout>"))))
  341. ;;;_ = allout-inhibit-auto-fill
  342. (defcustom allout-inhibit-auto-fill nil
  343. "If non-nil, auto-fill will be inhibited in the allout buffers.
  344. You can customize this setting to set it for all allout buffers, or set it
  345. in individual buffers if you want to inhibit auto-fill only in particular
  346. buffers. (You could use a function on `allout-mode-hook' to inhibit
  347. auto-fill according, eg, to the major mode.)
  348. If you don't set this and auto-fill-mode is enabled, allout will use the
  349. value that `normal-auto-fill-function', if any, when allout mode starts, or
  350. else allout's special hanging-indent maintaining auto-fill function,
  351. `allout-auto-fill'."
  352. :type 'boolean
  353. :group 'allout)
  354. (make-variable-buffer-local 'allout-inhibit-auto-fill)
  355. ;;;_ = allout-inhibit-auto-fill-on-headline
  356. (defcustom allout-inhibit-auto-fill-on-headline nil
  357. "If non-nil, auto-fill will be inhibited while on topic's header line."
  358. :version "24.1"
  359. :type 'boolean
  360. :group 'allout)
  361. (make-variable-buffer-local 'allout-inhibit-auto-fill-on-headline)
  362. ;;;_ = allout-use-hanging-indents
  363. (defcustom allout-use-hanging-indents t
  364. "If non-nil, topic body text auto-indent defaults to indent of the header.
  365. Ie, it is indented to be just past the header prefix. This is
  366. relevant mostly for use with `indented-text-mode', or other situations
  367. where auto-fill occurs."
  368. :type 'boolean
  369. :group 'allout)
  370. (make-variable-buffer-local 'allout-use-hanging-indents)
  371. ;;;###autoload
  372. (put 'allout-use-hanging-indents 'safe-local-variable
  373. (if (fboundp 'booleanp) 'booleanp (lambda (x) (member x '(t nil)))))
  374. ;;;_ = allout-reindent-bodies
  375. (defcustom allout-reindent-bodies (if allout-use-hanging-indents
  376. 'text)
  377. "Non-nil enables auto-adjust of topic body hanging indent with depth shifts.
  378. When active, topic body lines that are indented even with or beyond
  379. their topic header are reindented to correspond with depth shifts of
  380. the header.
  381. A value of t enables reindent in non-programming-code buffers, ie
  382. those that do not have the variable `comment-start' set. A value of
  383. `force' enables reindent whether or not `comment-start' is set."
  384. :type '(choice (const nil) (const t) (const text) (const force))
  385. :group 'allout)
  386. (make-variable-buffer-local 'allout-reindent-bodies)
  387. ;;;###autoload
  388. (put 'allout-reindent-bodies 'safe-local-variable
  389. (lambda (x) (memq x '(nil t text force))))
  390. ;;;_ = allout-show-bodies
  391. (defcustom allout-show-bodies nil
  392. "If non-nil, show entire body when exposing a topic, rather than
  393. just the header."
  394. :type 'boolean
  395. :group 'allout)
  396. (make-variable-buffer-local 'allout-show-bodies)
  397. ;;;###autoload
  398. (put 'allout-show-bodies 'safe-local-variable
  399. (if (fboundp 'booleanp) 'booleanp (lambda (x) (member x '(t nil)))))
  400. ;;;_ = allout-beginning-of-line-cycles
  401. (defcustom allout-beginning-of-line-cycles t
  402. "If non-nil, \\[allout-beginning-of-line] will cycle through smart-placement options.
  403. Cycling only happens on when the command is repeated, not when it
  404. follows a different command.
  405. Smart-placement means that repeated calls to this function will
  406. advance as follows:
  407. - if the cursor is on a non-headline body line and not on the first column:
  408. then it goes to the first column
  409. - if the cursor is on the first column of a non-headline body line:
  410. then it goes to the start of the headline within the item body
  411. - if the cursor is on the headline and not the start of the headline:
  412. then it goes to the start of the headline
  413. - if the cursor is on the start of the headline:
  414. then it goes to the bullet character (for hotspot navigation)
  415. - if the cursor is on the bullet character:
  416. then it goes to the first column of that line (the headline)
  417. - if the cursor is on the first column of the headline:
  418. then it goes to the start of the headline within the item body.
  419. In this fashion, you can use the beginning-of-line command to do
  420. its normal job and then, when repeated, advance through the
  421. entry, cycling back to start.
  422. If this configuration variable is nil, then the cursor is just
  423. advanced to the beginning of the line and remains there on
  424. repeated calls."
  425. :type 'boolean :group 'allout)
  426. ;;;_ = allout-end-of-line-cycles
  427. (defcustom allout-end-of-line-cycles t
  428. "If non-nil, \\[allout-end-of-line] will cycle through smart-placement options.
  429. Cycling only happens on when the command is repeated, not when it
  430. follows a different command.
  431. Smart placement means that repeated calls to this function will
  432. advance as follows:
  433. - if the cursor is not on the end-of-line,
  434. then it goes to the end-of-line
  435. - if the cursor is on the end-of-line but not the end-of-entry,
  436. then it goes to the end-of-entry, exposing it if necessary
  437. - if the cursor is on the end-of-entry,
  438. then it goes to the end of the head line
  439. In this fashion, you can use the end-of-line command to do its
  440. normal job and then, when repeated, advance through the entry,
  441. cycling back to start.
  442. If this configuration variable is nil, then the cursor is just
  443. advanced to the end of the line and remains there on repeated
  444. calls."
  445. :type 'boolean :group 'allout)
  446. ;;;_ = allout-header-prefix
  447. (defcustom allout-header-prefix "."
  448. ;; this string is treated as literal match. it will be `regexp-quote'd, so
  449. ;; one cannot use regular expressions to match varying header prefixes.
  450. "Leading string which helps distinguish topic headers.
  451. Outline topic header lines are identified by a leading topic
  452. header prefix, which mostly have the value of this var at their front.
  453. Level 1 topics are exceptions. They consist of only a single
  454. character, which is typically set to the `allout-primary-bullet'."
  455. :type 'string
  456. :group 'allout)
  457. (make-variable-buffer-local 'allout-header-prefix)
  458. ;;;###autoload
  459. (put 'allout-header-prefix 'safe-local-variable 'stringp)
  460. ;;;_ = allout-primary-bullet
  461. (defcustom allout-primary-bullet "*"
  462. "Bullet used for top-level outline topics.
  463. Outline topic header lines are identified by a leading topic header
  464. prefix, which is concluded by bullets that includes the value of this
  465. var and the respective allout-*-bullets-string vars.
  466. The value of an asterisk (`*') provides for backwards compatibility
  467. with the original Emacs outline mode. See `allout-plain-bullets-string'
  468. and `allout-distinctive-bullets-string' for the range of available
  469. bullets."
  470. :type 'string
  471. :group 'allout)
  472. (make-variable-buffer-local 'allout-primary-bullet)
  473. ;;;###autoload
  474. (put 'allout-primary-bullet 'safe-local-variable 'stringp)
  475. ;;;_ = allout-plain-bullets-string
  476. (defcustom allout-plain-bullets-string ".,"
  477. "The bullets normally used in outline topic prefixes.
  478. See `allout-distinctive-bullets-string' for the other kind of
  479. bullets.
  480. DO NOT include the close-square-bracket, `]', as a bullet.
  481. Outline mode has to be reactivated in order for changes to the value
  482. of this var to take effect."
  483. :type 'string
  484. :group 'allout)
  485. (make-variable-buffer-local 'allout-plain-bullets-string)
  486. ;;;###autoload
  487. (put 'allout-plain-bullets-string 'safe-local-variable 'stringp)
  488. ;;;_ = allout-distinctive-bullets-string
  489. (defcustom allout-distinctive-bullets-string "*+-=>()[{}&!?#%\"X@$~_\\:;^"
  490. "Persistent outline header bullets used to distinguish special topics.
  491. These bullets are distinguish topics with particular character.
  492. They are not used by default in the topic creation routines, but
  493. are offered as options when you modify topic creation with a
  494. universal argument (\\[universal-argument]), or during rebulleting (\\[allout-rebullet-current-heading]).
  495. Distinctive bullets are not cycled when topics are shifted or
  496. otherwise automatically rebulleted, so their marking is
  497. persistent until deliberately changed. Their significance is
  498. purely by convention, however. Some conventions suggest
  499. themselves:
  500. `(' - open paren -- an aside or incidental point
  501. `?' - question mark -- uncertain or outright question
  502. `!' - exclamation point/bang -- emphatic
  503. `[' - open square bracket -- meta-note, about item instead of item's subject
  504. `\"' - double quote -- a quotation or other citation
  505. `=' - equal sign -- an assignment, some kind of definition
  506. `^' - carat -- relates to something above
  507. Some are more elusive, but their rationale may be recognizable:
  508. `+' - plus -- pending consideration, completion
  509. `_' - underscore -- done, completed
  510. `&' - ampersand -- addendum, furthermore
  511. \(Some other non-plain bullets have special meaning to the
  512. software. By default:
  513. `~' marks encryptable topics -- see `allout-topic-encryption-bullet'
  514. `#' marks auto-numbered bullets -- see `allout-numbered-bullet'.)
  515. See `allout-plain-bullets-string' for the standard, alternating
  516. bullets.
  517. You must run `set-allout-regexp' in order for outline mode to
  518. adopt changes of this value.
  519. DO NOT include the close-square-bracket, `]', on either of the bullet
  520. strings."
  521. :type 'string
  522. :group 'allout)
  523. (make-variable-buffer-local 'allout-distinctive-bullets-string)
  524. ;;;###autoload
  525. (put 'allout-distinctive-bullets-string 'safe-local-variable 'stringp)
  526. ;;;_ = allout-use-mode-specific-leader
  527. (defcustom allout-use-mode-specific-leader t
  528. "When non-nil, use mode-specific topic-header prefixes.
  529. Allout outline mode will use the mode-specific `allout-mode-leaders' or
  530. comment-start string, if any, to lead the topic prefix string, so topic
  531. headers look like comments in the programming language. It will also use
  532. the comment-start string, with an `_' appended, for `allout-primary-bullet'.
  533. String values are used as literals, not regular expressions, so
  534. do not escape any regular-expression characters.
  535. Value t means to first check for assoc value in `allout-mode-leaders'
  536. alist, then use comment-start string, if any, then use default (`.').
  537. \(See note about use of comment-start strings, below.)
  538. Set to the symbol for either of `allout-mode-leaders' or
  539. `comment-start' to use only one of them, respectively.
  540. Value nil means to always use the default (`.') and leave
  541. `allout-primary-bullet' unaltered.
  542. comment-start strings that do not end in spaces are tripled in
  543. the header-prefix, and an `_' underscore is tacked on the end, to
  544. distinguish them from regular comment strings. comment-start
  545. strings that do end in spaces are not tripled, but an underscore
  546. is substituted for the space. [This presumes that the space is
  547. for appearance, not comment syntax. You can use
  548. `allout-mode-leaders' to override this behavior, when
  549. undesired.]"
  550. :type '(choice (const t) (const nil) string
  551. (const allout-mode-leaders)
  552. (const comment-start))
  553. :group 'allout)
  554. ;;;###autoload
  555. (put 'allout-use-mode-specific-leader 'safe-local-variable
  556. (lambda (x) (or (memq x '(t nil allout-mode-leaders comment-start))
  557. (stringp x))))
  558. ;;;_ = allout-mode-leaders
  559. (defvar allout-mode-leaders '()
  560. "Specific allout-prefix leading strings per major modes.
  561. Use this if the mode's comment-start string isn't what you
  562. prefer, or if the mode lacks a comment-start string. See
  563. `allout-use-mode-specific-leader' for more details.
  564. If you're constructing a string that will comment-out outline
  565. structuring so it can be included in program code, append an extra
  566. character, like an \"_\" underscore, to distinguish the lead string
  567. from regular comments that start at the beginning-of-line.")
  568. ;;;_ = allout-old-style-prefixes
  569. (defcustom allout-old-style-prefixes nil
  570. "When non-nil, use only old-and-crusty `outline-mode' `*' topic prefixes.
  571. Non-nil restricts the topic creation and modification
  572. functions to asterix-padded prefixes, so they look exactly
  573. like the original Emacs-outline style prefixes.
  574. Whatever the setting of this variable, both old and new style prefixes
  575. are always respected by the topic maneuvering functions."
  576. :type 'boolean
  577. :group 'allout)
  578. (make-variable-buffer-local 'allout-old-style-prefixes)
  579. ;;;###autoload
  580. (put 'allout-old-style-prefixes 'safe-local-variable
  581. (if (fboundp 'booleanp) 'booleanp (lambda (x) (member x '(t nil)))))
  582. ;;;_ = allout-stylish-prefixes -- alternating bullets
  583. (defcustom allout-stylish-prefixes t
  584. "Do fancy stuff with topic prefix bullets according to level, etc.
  585. Non-nil enables topic creation, modification, and repositioning
  586. functions to vary the topic bullet char (the char that marks the topic
  587. depth) just preceding the start of the topic text) according to level.
  588. Otherwise, only asterisks (`*') and distinctive bullets are used.
  589. This is how an outline can look (but sans indentation) with stylish
  590. prefixes:
  591. * Top level
  592. .* A topic
  593. . + One level 3 subtopic
  594. . . One level 4 subtopic
  595. . . A second 4 subtopic
  596. . + Another level 3 subtopic
  597. . #1 A numbered level 4 subtopic
  598. . #2 Another
  599. . ! Another level 4 subtopic with a different distinctive bullet
  600. . #4 And another numbered level 4 subtopic
  601. This would be an outline with stylish prefixes inhibited (but the
  602. numbered and other distinctive bullets retained):
  603. * Top level
  604. .* A topic
  605. . * One level 3 subtopic
  606. . * One level 4 subtopic
  607. . * A second 4 subtopic
  608. . * Another level 3 subtopic
  609. . #1 A numbered level 4 subtopic
  610. . #2 Another
  611. . ! Another level 4 subtopic with a different distinctive bullet
  612. . #4 And another numbered level 4 subtopic
  613. Stylish and constant prefixes (as well as old-style prefixes) are
  614. always respected by the topic maneuvering functions, regardless of
  615. this variable setting.
  616. The setting of this var is not relevant when `allout-old-style-prefixes'
  617. is non-nil."
  618. :type 'boolean
  619. :group 'allout)
  620. (make-variable-buffer-local 'allout-stylish-prefixes)
  621. ;;;###autoload
  622. (put 'allout-stylish-prefixes 'safe-local-variable
  623. (if (fboundp 'booleanp) 'booleanp (lambda (x) (member x '(t nil)))))
  624. ;;;_ = allout-numbered-bullet
  625. (defcustom allout-numbered-bullet "#"
  626. "String designating bullet of topics that have auto-numbering; nil for none.
  627. Topics having this bullet have automatic maintenance of a sibling
  628. sequence-number tacked on, just after the bullet. Conventionally set
  629. to \"#\", you can set it to a bullet of your choice. A nil value
  630. disables numbering maintenance."
  631. :type '(choice (const nil) string)
  632. :group 'allout)
  633. (make-variable-buffer-local 'allout-numbered-bullet)
  634. ;;;###autoload
  635. (put 'allout-numbered-bullet 'safe-local-variable
  636. (if (fboundp 'string-or-null-p)
  637. 'string-or-null-p
  638. (lambda (x) (or (stringp x) (null x)))))
  639. ;;;_ = allout-file-xref-bullet
  640. (defcustom allout-file-xref-bullet "@"
  641. "Bullet signifying file cross-references, for `allout-resolve-xref'.
  642. Set this var to the bullet you want to use for file cross-references."
  643. :type '(choice (const nil) string)
  644. :group 'allout)
  645. ;;;###autoload
  646. (put 'allout-file-xref-bullet 'safe-local-variable
  647. (if (fboundp 'string-or-null-p)
  648. 'string-or-null-p
  649. (lambda (x) (or (stringp x) (null x)))))
  650. ;;;_ = allout-presentation-padding
  651. (defcustom allout-presentation-padding 2
  652. "Presentation-format white-space padding factor, for greater indent."
  653. :type 'integer
  654. :group 'allout)
  655. (make-variable-buffer-local 'allout-presentation-padding)
  656. ;;;###autoload
  657. (put 'allout-presentation-padding 'safe-local-variable 'integerp)
  658. ;;;_ = allout-flattened-numbering-abbreviation
  659. (define-obsolete-variable-alias 'allout-abbreviate-flattened-numbering
  660. 'allout-flattened-numbering-abbreviation "24.1")
  661. (defcustom allout-flattened-numbering-abbreviation nil
  662. "If non-nil, `allout-flatten-exposed-to-buffer' abbreviates topic
  663. numbers to minimal amount with some context. Otherwise, entire
  664. numbers are always used."
  665. :version "24.1"
  666. :type 'boolean
  667. :group 'allout)
  668. ;;;_ + LaTeX formatting
  669. ;;;_ - allout-number-pages
  670. (defcustom allout-number-pages nil
  671. "Non-nil turns on page numbering for LaTeX formatting of an outline."
  672. :type 'boolean
  673. :group 'allout)
  674. ;;;_ - allout-label-style
  675. (defcustom allout-label-style "\\large\\bf"
  676. "Font and size of labels for LaTeX formatting of an outline."
  677. :type 'string
  678. :group 'allout)
  679. ;;;_ - allout-head-line-style
  680. (defcustom allout-head-line-style "\\large\\sl "
  681. "Font and size of entries for LaTeX formatting of an outline."
  682. :type 'string
  683. :group 'allout)
  684. ;;;_ - allout-body-line-style
  685. (defcustom allout-body-line-style " "
  686. "Font and size of entries for LaTeX formatting of an outline."
  687. :type 'string
  688. :group 'allout)
  689. ;;;_ - allout-title-style
  690. (defcustom allout-title-style "\\Large\\bf"
  691. "Font and size of titles for LaTeX formatting of an outline."
  692. :type 'string
  693. :group 'allout)
  694. ;;;_ - allout-title
  695. (defcustom allout-title '(or buffer-file-name (buffer-name))
  696. "Expression to be evaluated to determine the title for LaTeX
  697. formatted copy."
  698. :type 'sexp
  699. :group 'allout)
  700. ;;;_ - allout-line-skip
  701. (defcustom allout-line-skip ".05cm"
  702. "Space between lines for LaTeX formatting of an outline."
  703. :type 'string
  704. :group 'allout)
  705. ;;;_ - allout-indent
  706. (defcustom allout-indent ".3cm"
  707. "LaTeX formatted depth-indent spacing."
  708. :type 'string
  709. :group 'allout)
  710. ;;;_ + Topic encryption
  711. ;;;_ = allout-encryption group
  712. (defgroup allout-encryption nil
  713. "Settings for topic encryption features of allout outliner."
  714. :group 'allout)
  715. ;;;_ = allout-topic-encryption-bullet
  716. (defcustom allout-topic-encryption-bullet "~"
  717. "Bullet signifying encryption of the entry's body."
  718. :type '(choice (const nil) string)
  719. :version "22.1"
  720. :group 'allout-encryption)
  721. ;;;_ = allout-encrypt-unencrypted-on-saves
  722. (defcustom allout-encrypt-unencrypted-on-saves t
  723. "If non-nil, topics pending encryption are encrypted during buffer saves.
  724. This prevents file-system exposure of un-encrypted contents of
  725. items marked for encryption.
  726. When non-nil, if the topic currently being edited is decrypted,
  727. it will be encrypted for saving but automatically decrypted
  728. before any subsequent user interaction, so it is once again clear
  729. text for editing though the file system copy is encrypted.
  730. \(Auto-saves are handled differently. Buffers with plain-text
  731. exposed encrypted topics are exempted from auto saves until all
  732. such topics are encrypted.)"
  733. :type 'boolean
  734. :version "23.1"
  735. :group 'allout-encryption)
  736. (make-variable-buffer-local 'allout-encrypt-unencrypted-on-saves)
  737. (defvar allout-auto-save-temporarily-disabled nil
  738. "True while topic encryption is pending and auto-saving was active.
  739. The value of `buffer-saved-size' at the time of decryption is used,
  740. for restoring when all encryptions are established.")
  741. (defvar allout-just-did-undo nil
  742. "True just after undo commands, until allout-post-command-business.")
  743. (make-variable-buffer-local 'allout-just-did-undo)
  744. ;;;_ + Developer
  745. ;;;_ = allout-developer group
  746. (defgroup allout-developer nil
  747. "Allout settings developers care about, including topic encryption and more."
  748. :group 'allout)
  749. ;;;_ = allout-run-unit-tests-on-load
  750. (defcustom allout-run-unit-tests-on-load nil
  751. "When non-nil, unit tests will be run at end of loading the allout module.
  752. Generally, allout code developers are the only ones who'll want to set this.
  753. \(If set, this makes it an even better practice to exercise changes by
  754. doing byte-compilation with a repeat count, so the file is loaded after
  755. compilation.)
  756. See `allout-run-unit-tests' to see what's run."
  757. :type 'boolean
  758. :group 'allout-developer)
  759. ;;;_ + Miscellaneous customization
  760. ;;;_ = allout-enable-file-variable-adjustment
  761. (defcustom allout-enable-file-variable-adjustment t
  762. "If non-nil, some allout outline actions edit Emacs local file var text.
  763. This can range from changes to existing entries, addition of new ones,
  764. and creation of a new local variables section when necessary.
  765. Emacs file variables adjustments are also inhibited if `enable-local-variables'
  766. is nil.
  767. Operations potentially causing edits include allout encryption routines.
  768. For details, see `allout-toggle-current-subtree-encryption's docstring."
  769. :type 'boolean
  770. :group 'allout)
  771. (make-variable-buffer-local 'allout-enable-file-variable-adjustment)
  772. ;;;_* CODE -- no user customizations below.
  773. ;;;_ #1 Internal Outline Formatting and Configuration
  774. ;;;_ : Version
  775. ;;;_ = allout-version
  776. (defvar allout-version "2.3"
  777. "Version of currently loaded outline package. (allout.el)")
  778. ;;;_ > allout-version
  779. (defun allout-version (&optional here)
  780. "Return string describing the loaded outline version."
  781. (interactive "P")
  782. (let ((msg (concat "Allout Outline Mode v " allout-version)))
  783. (if here (insert msg))
  784. (message "%s" msg)
  785. msg))
  786. ;;;_ : Mode activation (defined here because it's referenced early)
  787. ;;;_ = allout-mode
  788. (defvar allout-mode nil "Allout outline mode minor-mode flag.")
  789. (make-variable-buffer-local 'allout-mode)
  790. ;;;_ = allout-layout nil
  791. (defvar allout-layout nil ; LEAVE GLOBAL VALUE NIL -- see docstring.
  792. "Buffer-specific setting for allout layout.
  793. In buffers where this is non-nil (and if `allout-auto-activation'
  794. has been customized to enable this behavior), `allout-mode' will be
  795. automatically activated. The layout dictated by the value will be used to
  796. set the initial exposure when `allout-mode' is activated.
  797. *You should not setq-default this variable non-nil unless you want every
  798. visited file to be treated as an allout file.*
  799. The value would typically be set by a file local variable. For
  800. example, the following lines at the bottom of an Emacs Lisp file:
  801. ;;;Local variables:
  802. ;;;allout-layout: (0 : -1 -1 0)
  803. ;;;End:
  804. dictate activation of `allout-mode' mode when the file is visited
  805. \(presuming proper `allout-auto-activation' customization),
  806. followed by the equivalent of `(allout-expose-topic 0 : -1 -1 0)'.
  807. \(This is the layout used for the allout.el source file.)
  808. `allout-default-layout' describes the specification format.
  809. `allout-layout' can additionally have the value t, in which
  810. case the value of `allout-default-layout' is used.")
  811. (make-variable-buffer-local 'allout-layout)
  812. ;;;###autoload
  813. (put 'allout-layout 'safe-local-variable
  814. (lambda (x) (or (numberp x) (listp x) (memq x '(: * + -)))))
  815. ;;;_ : Topic header format
  816. ;;;_ = allout-regexp
  817. (defvar allout-regexp ""
  818. "Regular expression to match the beginning of a heading line.
  819. Any line whose beginning matches this regexp is considered a
  820. heading. This var is set according to the user configuration vars
  821. by `set-allout-regexp'.")
  822. (make-variable-buffer-local 'allout-regexp)
  823. ;;;_ = allout-bullets-string
  824. (defvar allout-bullets-string ""
  825. "A string dictating the valid set of outline topic bullets.
  826. This var should *not* be set by the user -- it is set by `set-allout-regexp',
  827. and is produced from the elements of `allout-plain-bullets-string'
  828. and `allout-distinctive-bullets-string'.")
  829. (make-variable-buffer-local 'allout-bullets-string)
  830. ;;;_ = allout-bullets-string-len
  831. (defvar allout-bullets-string-len 0
  832. "Length of current buffers' `allout-plain-bullets-string'.")
  833. (make-variable-buffer-local 'allout-bullets-string-len)
  834. ;;;_ = allout-depth-specific-regexp
  835. (defvar allout-depth-specific-regexp ""
  836. "Regular expression to match a heading line prefix for a particular depth.
  837. This expression is used to search for depth-specific topic
  838. headers at depth 2 and greater. Use `allout-depth-one-regexp'
  839. for to seek topics at depth one.
  840. This var is set according to the user configuration vars by
  841. `set-allout-regexp'. It is prepared with format strings for two
  842. decimal numbers, which should each be one less than the depth of the
  843. topic prefix to be matched.")
  844. (make-variable-buffer-local 'allout-depth-specific-regexp)
  845. ;;;_ = allout-depth-one-regexp
  846. (defvar allout-depth-one-regexp ""
  847. "Regular expression to match a heading line prefix for depth one.
  848. This var is set according to the user configuration vars by
  849. `set-allout-regexp'. It is prepared with format strings for two
  850. decimal numbers, which should each be one less than the depth of the
  851. topic prefix to be matched.")
  852. (make-variable-buffer-local 'allout-depth-one-regexp)
  853. ;;;_ = allout-line-boundary-regexp
  854. (defvar allout-line-boundary-regexp ()
  855. "`allout-regexp' prepended with a newline for the search target.
  856. This is properly set by `set-allout-regexp'.")
  857. (make-variable-buffer-local 'allout-line-boundary-regexp)
  858. ;;;_ = allout-bob-regexp
  859. (defvar allout-bob-regexp ()
  860. "Like `allout-line-boundary-regexp', for headers at beginning of buffer.")
  861. (make-variable-buffer-local 'allout-bob-regexp)
  862. ;;;_ = allout-header-subtraction
  863. (defvar allout-header-subtraction (1- (length allout-header-prefix))
  864. "Allout-header prefix length to subtract when computing topic depth.")
  865. (make-variable-buffer-local 'allout-header-subtraction)
  866. ;;;_ = allout-plain-bullets-string-len
  867. (defvar allout-plain-bullets-string-len (length allout-plain-bullets-string)
  868. "Length of `allout-plain-bullets-string', updated by `set-allout-regexp'.")
  869. (make-variable-buffer-local 'allout-plain-bullets-string-len)
  870. ;;;_ = allout-doublecheck-at-and-shallower
  871. (defconst allout-doublecheck-at-and-shallower 3
  872. "Validate apparent topics of this depth and shallower as being non-aberrant.
  873. Verified with `allout-aberrant-container-p'. The usefulness of
  874. this check is limited to shallow depths, because the
  875. determination of aberrance is according to the mistaken item
  876. being followed by a legitimate item of excessively greater depth.
  877. The classic example of a mistaken item, for a standard allout
  878. outline configuration, is a body line that begins with an `...'
  879. ellipsis. This happens to contain a legitimate depth-2 header
  880. prefix, constituted by two `..' dots at the beginning of the
  881. line. The only thing that can distinguish it *in principle* from
  882. a legitimate one is if the following real header is at a depth
  883. that is discontinuous from the depth of 2 implied by the
  884. ellipsis, ie depth 4 or more. As the depth being tested gets
  885. greater, the likelihood of this kind of disqualification is
  886. lower, and the usefulness of this test is lower.
  887. Extending the depth of the doublecheck increases the amount it is
  888. applied, increasing the cost of the test - on casual estimation,
  889. for outlines with many deep topics, geometrically (O(n)?).
  890. Taken together with decreasing likelihood that the test will be
  891. useful at greater depths, more modest doublecheck limits are more
  892. suitably economical.")
  893. ;;;_ X allout-reset-header-lead (header-lead)
  894. (defun allout-reset-header-lead (header-lead)
  895. "Reset the leading string used to identify topic headers."
  896. (interactive "sNew lead string: ")
  897. (setq allout-header-prefix header-lead)
  898. (setq allout-header-subtraction (1- (length allout-header-prefix)))
  899. (set-allout-regexp))
  900. ;;;_ X allout-lead-with-comment-string (header-lead)
  901. (defun allout-lead-with-comment-string (&optional header-lead)
  902. "Set the topic-header leading string to specified string.
  903. Useful for encapsulating outline structure in programming
  904. language comments. Returns the leading string."
  905. (interactive "P")
  906. (if (not (stringp header-lead))
  907. (setq header-lead (read-string
  908. "String prefix for topic headers: ")))
  909. (setq allout-reindent-bodies nil)
  910. (allout-reset-header-lead header-lead)
  911. header-lead)
  912. ;;;_ > allout-infer-header-lead-and-primary-bullet ()
  913. (defun allout-infer-header-lead-and-primary-bullet ()
  914. "Determine appropriate `allout-header-prefix' and `allout-primary-bullet'.
  915. Works according to settings of:
  916. `comment-start'
  917. `allout-header-prefix' (default)
  918. `allout-use-mode-specific-leader'
  919. and `allout-mode-leaders'.
  920. Apply this via (re)activation of `allout-mode', rather than
  921. invoking it directly."
  922. (let* ((use-leader (and (boundp 'allout-use-mode-specific-leader)
  923. (if (or (stringp allout-use-mode-specific-leader)
  924. (memq allout-use-mode-specific-leader
  925. '(allout-mode-leaders
  926. comment-start
  927. t)))
  928. allout-use-mode-specific-leader
  929. ;; Oops -- garbled value, equate with effect of t:
  930. t)))
  931. (leader
  932. (cond
  933. ((not use-leader) nil)
  934. ;; Use the explicitly designated leader:
  935. ((stringp use-leader) use-leader)
  936. (t (or (and (memq use-leader '(t allout-mode-leaders))
  937. ;; Get it from outline mode leaders?
  938. (cdr (assq major-mode allout-mode-leaders)))
  939. ;; ... didn't get from allout-mode-leaders...
  940. (and (memq use-leader '(t comment-start))
  941. comment-start
  942. ;; Use comment-start, maybe tripled, and with
  943. ;; underscore:
  944. (concat
  945. (if (string= " "
  946. (substring comment-start
  947. (1- (length comment-start))))
  948. ;; Use comment-start, sans trailing space:
  949. (substring comment-start 0 -1)
  950. (concat comment-start comment-start comment-start))
  951. ;; ... and append underscore, whichever:
  952. "_")))))))
  953. (if (not leader)
  954. nil
  955. (setq allout-header-prefix leader)
  956. (if (not allout-old-style-prefixes)
  957. ;; setting allout-primary-bullet makes the top level topics use --
  958. ;; actually, be -- the special prefix:
  959. (setq allout-primary-bullet leader))
  960. allout-header-prefix)))
  961. (defalias 'allout-infer-header-lead
  962. 'allout-infer-header-lead-and-primary-bullet)
  963. ;;;_ > allout-infer-body-reindent ()
  964. (defun allout-infer-body-reindent ()
  965. "Determine proper setting for `allout-reindent-bodies'.
  966. Depends on default setting of `allout-reindent-bodies' (which see)
  967. and presence of setting for `comment-start', to tell whether the
  968. file is programming code."
  969. (if (and allout-reindent-bodies
  970. comment-start
  971. (not (eq 'force allout-reindent-bodies)))
  972. (setq allout-reindent-bodies nil)))
  973. ;;;_ > set-allout-regexp ()
  974. (defun set-allout-regexp ()
  975. "Generate proper topic-header regexp form for outline functions.
  976. Works with respect to `allout-plain-bullets-string' and
  977. `allout-distinctive-bullets-string'.
  978. Also refresh various data structures that hinge on the regexp."
  979. (interactive)
  980. ;; Derive allout-bullets-string from user configured components:
  981. (setq allout-bullets-string "")
  982. (let ((strings (list 'allout-plain-bullets-string
  983. 'allout-distinctive-bullets-string
  984. 'allout-primary-bullet))
  985. cur-string
  986. cur-len
  987. cur-char
  988. index)
  989. (while strings
  990. (setq index 0)
  991. (setq cur-len (length (setq cur-string (symbol-value (car strings)))))
  992. (while (< index cur-len)
  993. (setq cur-char (aref cur-string index))
  994. (setq allout-bullets-string
  995. (concat allout-bullets-string
  996. (cond
  997. ; Single dash would denote a
  998. ; sequence, repeated denotes
  999. ; a dash:
  1000. ((eq cur-char ?-) "--")
  1001. ; literal close-square-bracket
  1002. ; doesn't work right in the
  1003. ; expr, exclude it:
  1004. ((eq cur-char ?\]) "")
  1005. (t (regexp-quote (char-to-string cur-char))))))
  1006. (setq index (1+ index)))
  1007. (setq strings (cdr strings)))
  1008. )
  1009. ;; Derive next for repeated use in allout-pending-bullet:
  1010. (setq allout-plain-bullets-string-len (length allout-plain-bullets-string))
  1011. (setq allout-header-subtraction (1- (length allout-header-prefix)))
  1012. (let (new-part old-part formfeed-part)
  1013. (setq new-part (concat "\\("
  1014. (regexp-quote allout-header-prefix)
  1015. "[ \t]*"
  1016. ;; already regexp-quoted in a custom way:
  1017. "[" allout-bullets-string "]"
  1018. "\\)")
  1019. old-part (concat "\\("
  1020. (regexp-quote allout-primary-bullet)
  1021. "\\|"
  1022. (regexp-quote allout-header-prefix)
  1023. "\\)"
  1024. "+"
  1025. " ?[^" allout-primary-bullet "]")
  1026. formfeed-part "\\(\^L\\)"
  1027. allout-regexp (concat new-part
  1028. "\\|"
  1029. old-part
  1030. "\\|"
  1031. formfeed-part)
  1032. allout-line-boundary-regexp (concat "\n" new-part
  1033. "\\|"
  1034. "\n" old-part
  1035. "\\|"
  1036. "\n" formfeed-part)
  1037. allout-bob-regexp (concat "\\`" new-part
  1038. "\\|"
  1039. "\\`" old-part
  1040. "\\|"
  1041. "\\`" formfeed-part
  1042. ))
  1043. (setq allout-depth-specific-regexp
  1044. (concat "\\(^\\|\\`\\)"
  1045. "\\("
  1046. ;; new-style spacers-then-bullet string:
  1047. "\\("
  1048. (allout-format-quote (regexp-quote allout-header-prefix))
  1049. " \\{%s\\}"
  1050. "[" (allout-format-quote allout-bullets-string) "]"
  1051. "\\)"
  1052. ;; old-style all-bullets string, if primary not multi-char:
  1053. (if (< 0 allout-header-subtraction)
  1054. ""
  1055. (concat "\\|\\("
  1056. (allout-format-quote
  1057. (regexp-quote allout-primary-bullet))
  1058. (allout-format-quote
  1059. (regexp-quote allout-primary-bullet))
  1060. (allout-format-quote
  1061. (regexp-quote allout-primary-bullet))
  1062. "\\{%s\\}"
  1063. ;; disqualify greater depths:
  1064. "[^"
  1065. (allout-format-quote allout-primary-bullet)
  1066. "]\\)"
  1067. ))
  1068. "\\)"
  1069. ))
  1070. (setq allout-depth-one-regexp
  1071. (concat "\\(^\\|\\`\\)"
  1072. "\\("
  1073. "\\("
  1074. (regexp-quote allout-header-prefix)
  1075. ;; disqualify any bullet char following any amount of
  1076. ;; intervening whitespace:
  1077. " *"
  1078. (concat "[^ " allout-bullets-string "]")
  1079. "\\)"
  1080. (if (< 0 allout-header-subtraction)
  1081. ;; Need not support anything like the old
  1082. ;; bullet style if the prefix is multi-char.
  1083. ""
  1084. (concat "\\|"
  1085. (regexp-quote allout-primary-bullet)
  1086. ;; disqualify deeper primary-bullet sequences:
  1087. "[^" allout-primary-bullet "]"))
  1088. "\\)"
  1089. ))))
  1090. ;;;_ : Menu bar
  1091. (defvar allout-mode-exposure-menu)
  1092. (defvar allout-mode-editing-menu)
  1093. (defvar allout-mode-navigation-menu)
  1094. (defvar allout-mode-misc-menu)
  1095. (defun produce-allout-mode-menubar-entries ()
  1096. (require 'easymenu)
  1097. (easy-menu-define allout-mode-exposure-menu
  1098. allout-mode-map-value
  1099. "Allout outline exposure menu."
  1100. '("Exposure"
  1101. ["Show Entry" allout-show-current-entry t]
  1102. ["Show Children" allout-show-children t]
  1103. ["Show Subtree" allout-show-current-subtree t]
  1104. ["Hide Subtree" allout-hide-current-subtree t]
  1105. ["Hide Leaves" allout-hide-current-leaves t]
  1106. "----"
  1107. ["Show All" allout-show-all t]))
  1108. (easy-menu-define allout-mode-editing-menu
  1109. allout-mode-map-value
  1110. "Allout outline editing menu."
  1111. '("Headings"
  1112. ["Open Sibling" allout-open-sibtopic t]
  1113. ["Open Subtopic" allout-open-subtopic t]
  1114. ["Open Supertopic" allout-open-supertopic t]
  1115. "----"
  1116. ["Shift Topic In" allout-shift-in t]
  1117. ["Shift Topic Out" allout-shift-out t]
  1118. ["Rebullet Topic" allout-rebullet-topic t]
  1119. ["Rebullet Heading" allout-rebullet-current-heading t]
  1120. ["Number Siblings" allout-number-siblings t]
  1121. "----"
  1122. ["Toggle Topic Encryption"
  1123. allout-toggle-current-subtree-encryption
  1124. (> (allout-current-depth) 1)]))
  1125. (easy-menu-define allout-mode-navigation-menu
  1126. allout-mode-map-value
  1127. "Allout outline navigation menu."
  1128. '("Navigation"
  1129. ["Next Visible Heading" allout-next-visible-heading t]
  1130. ["Previous Visible Heading"
  1131. allout-previous-visible-heading t]
  1132. "----"
  1133. ["Up Level" allout-up-current-level t]
  1134. ["Forward Current Level" allout-forward-current-level t]
  1135. ["Backward Current Level"
  1136. allout-backward-current-level t]
  1137. "----"
  1138. ["Beginning of Entry"
  1139. allout-beginning-of-current-entry t]
  1140. ["End of Entry" allout-end-of-entry t]
  1141. ["End of Subtree" allout-end-of-current-subtree t]))
  1142. (easy-menu-define allout-mode-misc-menu
  1143. allout-mode-map-value
  1144. "Allout outlines miscellaneous bindings."
  1145. '("Misc"
  1146. ["Version" allout-version t]
  1147. "----"
  1148. ["Duplicate Exposed" allout-copy-exposed-to-buffer t]
  1149. ["Duplicate Exposed, numbered"
  1150. allout-flatten-exposed-to-buffer t]
  1151. ["Duplicate Exposed, indented"
  1152. allout-indented-exposed-to-buffer t]
  1153. "----"
  1154. ["Set Header Lead" allout-reset-header-lead t]
  1155. ["Set New Exposure" allout-expose-topic t])))
  1156. ;;;_ : Allout Modal-Variables Utilities
  1157. ;;;_ = allout-mode-prior-settings
  1158. (defvar allout-mode-prior-settings nil
  1159. "Internal `allout-mode' use; settings to be resumed on mode deactivation.
  1160. See `allout-add-resumptions' and `allout-do-resumptions'.")
  1161. (make-variable-buffer-local 'allout-mode-prior-settings)
  1162. ;;;_ > allout-add-resumptions (&rest pairs)
  1163. (defun allout-add-resumptions (&rest pairs)
  1164. "Set name/value PAIRS.
  1165. Old settings are preserved for later resumption using `allout-do-resumptions'.
  1166. The new values are set as a buffer local. On resumption, the prior buffer
  1167. scope of the variable is restored along with its value. If it was a void
  1168. buffer-local value, then it is left as nil on resumption.
  1169. The pairs are lists whose car is the name of the variable and car of the
  1170. cdr is the new value: `(some-var some-value)'. The pairs can actually be
  1171. triples, where the third element qualifies the disposition of the setting,
  1172. as described further below.
  1173. If the optional third element is the symbol `extend', then the new value
  1174. created by `cons'ing the second element of the pair onto the front of the
  1175. existing value.
  1176. If the optional third element is the symbol `append', then the new value is
  1177. extended from the existing one by `append'ing a list containing the second
  1178. element of the pair onto the end of the existing value.
  1179. Extension, and resumptions in general, should not be used for hook
  1180. functions -- use the `local' mode of `add-hook' for that, instead.
  1181. The settings are stored on `allout-mode-prior-settings'."
  1182. (while pairs
  1183. (let* ((pair (pop pairs))
  1184. (name (car pair))
  1185. (value (cadr pair))
  1186. (qualifier (if (> (length pair) 2)
  1187. (caddr pair)))
  1188. prior-value)
  1189. (if (not (symbolp name))
  1190. (error "Pair's name, %S, must be a symbol, not %s"
  1191. name (type-of name)))
  1192. (setq prior-value (condition-case nil
  1193. (symbol-value name)
  1194. (void-variable nil)))
  1195. (when (not (assoc name allout-mode-prior-settings))
  1196. ;; Not already added as a resumption, create the prior setting entry.
  1197. (if (local-variable-p name (current-buffer))
  1198. ;; is already local variable -- preserve the prior value:
  1199. (push (list name prior-value) allout-mode-prior-settings)
  1200. ;; wasn't local variable, indicate so for resumption by killing
  1201. ;; local value, and make it local:
  1202. (push (list name) allout-mode-prior-settings)
  1203. (make-local-variable name)))
  1204. (if qualifier
  1205. (cond ((eq qualifier 'extend)
  1206. (if (not (listp prior-value))
  1207. (error "extension of non-list prior value attempted")
  1208. (set name (cons value prior-value))))
  1209. ((eq qualifier 'append)
  1210. (if (not (listp prior-value))
  1211. (error "appending of non-list prior value attempted")
  1212. (set name (append prior-value (list value)))))
  1213. (t (error "unrecognized setting qualifier `%s' encountered"
  1214. qualifier)))
  1215. (set name value)))))
  1216. ;;;_ > allout-do-resumptions ()
  1217. (defun allout-do-resumptions ()
  1218. "Resume all name/value settings registered by `allout-add-resumptions'.
  1219. This is used when concluding allout-mode, to resume selected variables to
  1220. their settings before allout-mode was started."
  1221. (while allout-mode-prior-settings
  1222. (let* ((pair (pop allout-mode-prior-settings))
  1223. (name (car pair))
  1224. (value-cell (cdr pair)))
  1225. (if (not value-cell)
  1226. ;; Prior value was global:
  1227. (kill-local-variable name)
  1228. ;; Prior value was explicit:
  1229. (set name (car value-cell))))))
  1230. ;;;_ : Mode-specific incidentals
  1231. ;;;_ > allout-unprotected (expr)
  1232. (defmacro allout-unprotected (expr)
  1233. "Enable internal outline operations to alter invisible text."
  1234. `(let ((inhibit-read-only (if (not buffer-read-only) t))
  1235. (inhibit-field-text-motion t))
  1236. ,expr))
  1237. ;;;_ = allout-mode-hook
  1238. (defvar allout-mode-hook nil
  1239. "Hook run when allout mode starts.")
  1240. ;;;_ = allout-mode-deactivate-hook
  1241. (define-obsolete-variable-alias 'allout-mode-deactivate-hook
  1242. 'allout-mode-off-hook "24.1")
  1243. (defvar allout-mode-deactivate-hook nil
  1244. "Hook run when allout mode ends.")
  1245. ;;;_ = allout-exposure-category
  1246. (defvar allout-exposure-category nil
  1247. "Symbol for use as allout invisible-text overlay category.")
  1248. ;;;_ = allout-exposure-change-functions
  1249. (define-obsolete-variable-alias 'allout-exposure-change-hook
  1250. 'allout-exposure-change-functions "24.3")
  1251. (defcustom allout-exposure-change-functions nil
  1252. "Abnormal hook run after allout outline subtree exposure changes.
  1253. It is run at the conclusion of `allout-flag-region'.
  1254. Functions on the hook must take three arguments:
  1255. - FROM -- integer indicating the point at the start of the change.
  1256. - TO -- integer indicating the point of the end of the change.
  1257. - FLAG -- change mode: nil for exposure, otherwise concealment.
  1258. This hook might be invoked multiple times by a single command."
  1259. :type 'hook
  1260. :group 'allout
  1261. :version "24.3")
  1262. ;;;_ = allout-structure-added-functions
  1263. (define-obsolete-variable-alias 'allout-structure-added-hook
  1264. 'allout-structure-added-functions "24.3")
  1265. (defcustom allout-structure-added-functions nil
  1266. "Abnormal hook run after adding items to an Allout outline.
  1267. Functions on the hook should take two arguments:
  1268. - NEW-START -- integer indicating position of start of the first new item.
  1269. - NEW-END -- integer indicating position of end of the last new item.
  1270. This hook might be invoked multiple times by a single command."
  1271. :type 'hook
  1272. :group 'allout
  1273. :version "24.3")
  1274. ;;;_ = allout-structure-deleted-functions
  1275. (define-obsolete-variable-alias 'allout-structure-deleted-hook
  1276. 'allout-structure-deleted-functions "24.3")
  1277. (defcustom allout-structure-deleted-functions nil
  1278. "Abnormal hook run after deleting subtrees from an Allout outline.
  1279. Functions on the hook must take two arguments:
  1280. - DEPTH -- integer indicating the depth of the subtree that was deleted.
  1281. - REMOVED-FROM -- integer indicating the point where the subtree was removed.
  1282. Some edits that remove or invalidate items may be missed by this hook:
  1283. specifically edits that native allout routines do not control.
  1284. This hook might be invoked multiple times by a single command."
  1285. :type 'hook
  1286. :group 'allout
  1287. :version "24.3")
  1288. ;;;_ = allout-structure-shifted-functions
  1289. (define-obsolete-variable-alias 'allout-structure-shifted-hook
  1290. 'allout-structure-shifted-functions "24.3")
  1291. (defcustom allout-structure-shifted-functions nil
  1292. "Abnormal hook run after shifting items in an Allout outline.
  1293. Functions on the hook should take two arguments:
  1294. - DEPTH-CHANGE -- integer indicating depth increase, negative for decrease
  1295. - START -- integer indicating the start point of the shifted parent item.
  1296. Some edits that shift items can be missed by this hook: specifically edits
  1297. that native allout routines do not control.
  1298. This hook might be invoked multiple times by a single command."
  1299. :type 'hook
  1300. :group 'allout
  1301. :version "24.3")
  1302. ;;;_ = allout-after-copy-or-kill-hook
  1303. (defcustom allout-after-copy-or-kill-hook nil
  1304. "Normal hook run after copying outline text.."
  1305. :type 'hook
  1306. :group 'allout
  1307. :version "24.3")
  1308. ;;;_ = allout-post-undo-hook
  1309. (defcustom allout-post-undo-hook nil
  1310. "Normal hook run after undo activity.
  1311. The item that's current when the hook is run *may* be the one
  1312. that was affected by the undo.."
  1313. :type 'hook
  1314. :group 'allout
  1315. :version "24.3")
  1316. ;;;_ = allout-outside-normal-auto-fill-function
  1317. (defvar allout-outside-normal-auto-fill-function nil
  1318. "Value of `normal-auto-fill-function' outside of allout mode.
  1319. Used by `allout-auto-fill' to do the mandated `normal-auto-fill-function'
  1320. wrapped within allout's automatic `fill-prefix' setting.")
  1321. (make-variable-buffer-local 'allout-outside-normal-auto-fill-function)
  1322. ;;;_ = prevent redundant activation by desktop mode:
  1323. (add-to-list 'desktop-minor-mode-handlers '(allout-mode . nil))
  1324. ;;;_ = allout-passphrase-verifier-string
  1325. (defvar allout-passphrase-verifier-string nil
  1326. "Setting used to test solicited encryption passphrases against the one
  1327. already associated with a file.
  1328. It consists of an encrypted random string useful only to verify that a
  1329. passphrase entered by the user is effective for decryption. The passphrase
  1330. itself is *not* recorded in the file anywhere, and the encrypted contents
  1331. are random binary characters to avoid exposing greater susceptibility to
  1332. search attacks.
  1333. The verifier string is retained as an Emacs file variable, as well as in
  1334. the Emacs buffer state, if file variable adjustments are enabled. See
  1335. `allout-enable-file-variable-adjustment' for details about that.")
  1336. (make-variable-buffer-local 'allout-passphrase-verifier-string)
  1337. (make-obsolete-variable 'allout-passphrase-verifier-string
  1338. 'allout-passphrase-verifier-string "23.3")
  1339. ;;;###autoload
  1340. (put 'allout-passphrase-verifier-string 'safe-local-variable 'stringp)
  1341. ;;;_ = allout-passphrase-hint-string
  1342. (defvar allout-passphrase-hint-string ""
  1343. "Variable used to retain reminder string for file's encryption passphrase.
  1344. See the description of `allout-passphrase-hint-handling' for details about how
  1345. the reminder is deployed.
  1346. The hint is retained as an Emacs file variable, as well as in the Emacs buffer
  1347. state, if file variable adjustments are enabled. See
  1348. `allout-enable-file-variable-adjustment' for details about that.")
  1349. (make-variable-buffer-local 'allout-passphrase-hint-string)
  1350. (setq-default allout-passphrase-hint-string "")
  1351. (make-obsolete-variable 'allout-passphrase-hint-string
  1352. 'allout-passphrase-hint-string "23.3")
  1353. ;;;###autoload
  1354. (put 'allout-passphrase-hint-string 'safe-local-variable 'stringp)
  1355. ;;;_ = allout-after-save-decrypt
  1356. (defvar allout-after-save-decrypt nil
  1357. "Internal variable, is nil or has the value of two points:
  1358. - the location of a topic to be decrypted after saving is done
  1359. - where to situate the cursor after the decryption is performed
  1360. This is used to decrypt the topic that was currently being edited, if it
  1361. was encrypted automatically as part of a file write or autosave.")
  1362. (make-variable-buffer-local 'allout-after-save-decrypt)
  1363. ;;;_ = allout-encryption-plaintext-sanitization-regexps
  1364. (defvar allout-encryption-plaintext-sanitization-regexps nil
  1365. "List of regexps whose matches are removed from plaintext before encryption.
  1366. This is for the sake of removing artifacts, like escapes, that are added on
  1367. and not actually part of the original plaintext. The removal is done just
  1368. prior to encryption.
  1369. Entries must be symbols that are bound to the desired values.
  1370. Each value can be a regexp or a list with a regexp followed by a
  1371. substitution string. If it's just a regexp, all its matches are removed
  1372. before the text is encrypted. If it's a regexp and a substitution, the
  1373. substitution is used against the regexp matches, a la `replace-match'.")
  1374. (make-variable-buffer-local 'allout-encryption-plaintext-sanitization-regexps)
  1375. ;;;_ = allout-encryption-ciphertext-rejection-regexps
  1376. (defvar allout-encryption-ciphertext-rejection-regexps nil
  1377. "Variable for regexps matching plaintext to remove before encryption.
  1378. This is used to detect strings in encryption results that would
  1379. register as allout mode structural elements, for example, as a
  1380. topic prefix.
  1381. Entries must be symbols that are bound to the desired regexp values.
  1382. Encryptions that result in matches will be retried, up to
  1383. `allout-encryption-ciphertext-rejection-limit' times, after which
  1384. an error is raised.")
  1385. (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-regexps)
  1386. ;;;_ = allout-encryption-ciphertext-rejection-ceiling
  1387. (defvar allout-encryption-ciphertext-rejection-ceiling 5
  1388. "Limit on number of times encryption ciphertext is rejected.
  1389. See `allout-encryption-ciphertext-rejection-regexps' for rejection reasons.")
  1390. (make-variable-buffer-local 'allout-encryption-ciphertext-rejection-ceiling)
  1391. ;;;_ > allout-mode-p ()
  1392. ;; Must define this macro above any uses, or byte compilation will lack
  1393. ;; proper def, if file isn't loaded -- eg, during emacs build!
  1394. ;;;###autoload
  1395. (defmacro allout-mode-p ()
  1396. "Return t if `allout-mode' is active in current buffer."
  1397. 'allout-mode)
  1398. ;;;_ > allout-write-contents-hook-handler ()
  1399. (defun allout-write-contents-hook-handler ()
  1400. "Implement `allout-encrypt-unencrypted-on-saves' for file writes
  1401. Return nil if all goes smoothly, or else return an informative
  1402. message if an error is encountered. The message will serve as a
  1403. non-nil return on `write-contents-functions' to prevent saving of
  1404. the buffer while it has decrypted content.
  1405. This behavior depends on Emacs versions that implement the
  1406. `write-contents-functions' hook."
  1407. (if (or (not (allout-mode-p))
  1408. (not (boundp 'allout-encrypt-unencrypted-on-saves))
  1409. (not allout-encrypt-unencrypted-on-saves))
  1410. nil
  1411. (if (save-excursion (goto-char (point-min))
  1412. (allout-next-topic-pending-encryption))
  1413. (progn
  1414. (message "auto-encrypting pending topics")
  1415. (sit-for 0)
  1416. (condition-case failure
  1417. (progn
  1418. (setq allout-after-save-decrypt
  1419. (allout-encrypt-decrypted))
  1420. ;; aok - return nil:
  1421. nil)
  1422. (error
  1423. ;; whoops - probably some still-decrypted items, return non-nil:
  1424. (let ((text (format (concat "%s contents write inhibited due to"
  1425. " encrypted topic encryption error:"
  1426. " %s")
  1427. (buffer-name (current-buffer))
  1428. failure)))
  1429. (message text)(sit-for 2)
  1430. text)))))
  1431. ))
  1432. ;;;_ > allout-after-saves-handler ()
  1433. (defun allout-after-saves-handler ()
  1434. "Decrypt topic encrypted for save, if it's currently being edited.
  1435. Ie, if it was pending encryption and contained the point in its body before
  1436. the save.
  1437. We use values stored in `allout-after-save-decrypt' to locate the topic
  1438. and the place for the cursor after the decryption is done."
  1439. (if (not (and (allout-mode-p)
  1440. (boundp 'allout-after-save-decrypt)
  1441. allout-after-save-decrypt))
  1442. t
  1443. (goto-char (car allout-after-save-decrypt))
  1444. (let ((was-modified (buffer-modified-p)))
  1445. (allout-toggle-subtree-encryption)
  1446. (if (not was-modified)
  1447. (set-buffer-modified-p nil)))
  1448. (goto-char (cadr allout-after-save-decrypt))
  1449. (setq allout-after-save-decrypt nil))
  1450. )
  1451. ;;;_ > allout-called-interactively-p ()
  1452. (defmacro allout-called-interactively-p ()
  1453. "A version of `called-interactively-p' independent of Emacs version."
  1454. ;; ... to ease maintenance of allout without betraying deprecation.
  1455. (if (ignore-errors (called-interactively-p 'interactive) t)
  1456. '(called-interactively-p 'interactive)
  1457. '(called-interactively-p)))
  1458. ;;;_ = allout-inhibit-aberrance-doublecheck nil
  1459. ;; In some exceptional moments, disparate topic depths need to be allowed
  1460. ;; momentarily, eg when one topic is being yanked into another and they're
  1461. ;; about to be reconciled. let-binding allout-inhibit-aberrance-doublecheck
  1462. ;; prevents the aberrance doublecheck to allow, eg, the reconciliation
  1463. ;; processing to happen in the presence of such discrepancies. It should
  1464. ;; almost never be needed, however.
  1465. (defvar allout-inhibit-aberrance-doublecheck nil
  1466. "Internal state, for momentarily inhibits aberrance doublecheck.
  1467. This should only be momentarily let-bound non-nil, not set
  1468. non-nil in a lasting way.")
  1469. ;;;_ #2 Mode environment and activation
  1470. ;;;_ = allout-explicitly-deactivated
  1471. (defvar allout-explicitly-deactivated nil
  1472. "If t, `allout-mode's last deactivation was deliberate.
  1473. So `allout-post-command-business' should not reactivate it...")
  1474. (make-variable-buffer-local 'allout-explicitly-deactivated)
  1475. ;;;_ > allout-init (mode)
  1476. (defun allout-init (mode)
  1477. "DEPRECATED - configure allout activation by customizing
  1478. `allout-auto-activation'. This function remains around, limited
  1479. from what it did before, for backwards compatibility.
  1480. MODE is the activation mode - see `allout-auto-activation' for
  1481. valid values."
  1482. (declare (obsolete allout-auto-activation "23.3"))
  1483. (custom-set-variables (list 'allout-auto-activation (format "%s" mode)))
  1484. (format "%s" mode))
  1485. ;;;_ > allout-setup-menubar ()
  1486. (defun allout-setup-menubar ()
  1487. "Populate the current buffer's menubar with `allout-mode' stuff."
  1488. (let ((menus (list allout-mode-exposure-menu
  1489. allout-mode-editing-menu
  1490. allout-mode-navigation-menu
  1491. allout-mode-misc-menu))
  1492. cur)
  1493. (while menus
  1494. (setq cur (car menus)
  1495. menus (cdr menus))
  1496. (easy-menu-add cur))))
  1497. ;;;_ > allout-overlay-preparations
  1498. (defun allout-overlay-preparations ()
  1499. "Set the properties of the allout invisible-text overlay and others."
  1500. (setplist 'allout-exposure-category nil)
  1501. (put 'allout-exposure-category 'invisible 'allout)
  1502. (put 'allout-exposure-category 'evaporate t)
  1503. ;; ??? We use isearch-open-invisible *and* isearch-mode-end-hook. The
  1504. ;; latter would be sufficient, but it seems that a separate behavior --
  1505. ;; the _transient_ opening of invisible text during isearch -- is keyed to
  1506. ;; presence of the isearch-open-invisible property -- even though this
  1507. ;; property controls the isearch _arrival_ behavior. This is the case at
  1508. ;; least in emacs 21, 22.1, and xemacs 21.4.
  1509. (put 'allout-exposure-category 'isearch-open-invisible
  1510. 'allout-isearch-end-handler)
  1511. (if (featurep 'xemacs)
  1512. (put 'allout-exposure-category 'start-open t)
  1513. (put 'allout-exposure-category 'insert-in-front-hooks
  1514. '(allout-overlay-insert-in-front-handler)))
  1515. (put 'allout-exposure-category 'modification-hooks
  1516. '(allout-overlay-interior-modification-handler)))
  1517. ;;;_ > define-minor-mode allout-mode
  1518. ;;;_ : Defun:
  1519. ;;;###autoload
  1520. (define-minor-mode allout-mode
  1521. ;;;_ . Doc string:
  1522. "Toggle Allout outline mode.
  1523. With a prefix argument ARG, enable Allout outline mode if ARG is
  1524. positive, and disable it otherwise. If called from Lisp, enable
  1525. the mode if ARG is omitted or nil.
  1526. \\<allout-mode-map-value>
  1527. Allout outline mode is a minor mode that provides extensive
  1528. outline oriented formatting and manipulation. It enables
  1529. structural editing of outlines, as well as navigation and
  1530. exposure. It also is specifically aimed at accommodating
  1531. syntax-sensitive text like programming languages. (For example,
  1532. see the allout code itself, which is organized as an allout
  1533. outline.)
  1534. In addition to typical outline navigation and exposure, allout includes:
  1535. - topic-oriented authoring, including keystroke-based topic creation,
  1536. repositioning, promotion/demotion, cut, and paste
  1537. - incremental search with dynamic exposure and reconcealment of hidden text
  1538. - adjustable format, so programming code can be developed in outline-structure
  1539. - easy topic encryption and decryption, symmetric or key-pair
  1540. - \"Hot-spot\" operation, for single-keystroke maneuvering and exposure control
  1541. - integral outline layout, for automatic initial exposure when visiting a file
  1542. - independent extensibility, using comprehensive exposure and authoring hooks
  1543. and many other features.
  1544. Below is a description of the key bindings, and then description
  1545. of special `allout-mode' features and terminology. See also the
  1546. outline menubar additions for quick reference to many of the
  1547. features. Customize `allout-auto-activation' to prepare your
  1548. Emacs session for automatic activation of `allout-mode'.
  1549. The bindings are those listed in `allout-prefixed-keybindings'
  1550. and `allout-unprefixed-keybindings'. We recommend customizing
  1551. `allout-command-prefix' to use just `\\C-c' as the command
  1552. prefix, if the allout bindings don't conflict with any personal
  1553. bindings you have on \\C-c. In any case, outline structure
  1554. navigation and authoring is simplified by positioning the cursor
  1555. on an item's bullet character, the \"hot-spot\" -- then you can
  1556. invoke allout commands with just the un-prefixed,
  1557. un-control-shifted command letters. This is described further in
  1558. the HOT-SPOT Operation section.
  1559. Exposure Control:
  1560. ----------------
  1561. \\[allout-hide-current-subtree] `allout-hide-current-subtree'
  1562. \\[allout-show-children] `allout-show-children'
  1563. \\[allout-show-current-subtree] `allout-show-current-subtree'
  1564. \\[allout-show-current-entry] `allout-show-current-entry'
  1565. \\[allout-show-all] `allout-show-all'
  1566. Navigation:
  1567. ----------
  1568. \\[allout-next-visible-heading] `allout-next-visible-heading'
  1569. \\[allout-previous-visible-heading] `allout-previous-visible-heading'
  1570. \\[allout-up-current-level] `allout-up-current-level'
  1571. \\[allout-forward-current-level] `allout-forward-current-level'
  1572. \\[allout-backward-current-level] `allout-backward-current-level'
  1573. \\[allout-end-of-entry] `allout-end-of-entry'
  1574. \\[allout-beginning-of-current-entry] `allout-beginning-of-current-entry' (alternately, goes to hot-spot)
  1575. \\[allout-beginning-of-line] `allout-beginning-of-line' -- like regular beginning-of-line, but
  1576. if immediately repeated cycles to the beginning of the current item
  1577. and then to the hot-spot (if `allout-beginning-of-line-cycles' is set).
  1578. Topic Header Production:
  1579. -----------------------
  1580. \\[allout-open-sibtopic] `allout-open-sibtopic' Create a new sibling after current topic.
  1581. \\[allout-open-subtopic] `allout-open-subtopic' ... an offspring of current topic.
  1582. \\[allout-open-supertopic] `allout-open-supertopic' ... a sibling of the current topic's parent.
  1583. Topic Level and Prefix Adjustment:
  1584. ---------------------------------
  1585. \\[allout-shift-in] `allout-shift-in' Shift current topic and all offspring deeper
  1586. \\[allout-shift-out] `allout-shift-out' ... less deep
  1587. \\[allout-rebullet-current-heading] `allout-rebullet-current-heading' Prompt for alternate bullet for
  1588. current topic
  1589. \\[allout-rebullet-topic] `allout-rebullet-topic' Reconcile bullets of topic and
  1590. its offspring -- distinctive bullets are not changed, others
  1591. are alternated according to nesting depth.
  1592. \\[allout-number-siblings] `allout-number-siblings' Number bullets of topic and siblings --
  1593. the offspring are not affected.
  1594. With repeat count, revoke numbering.
  1595. Topic-oriented Killing and Yanking:
  1596. ----------------------------------
  1597. \\[allout-kill-topic] `allout-kill-topic' Kill current topic, including offspring.
  1598. \\[allout-copy-topic-as-kill] `allout-copy-topic-as-kill' Copy current topic, including offspring.
  1599. \\[allout-kill-line] `allout-kill-line' Kill line, attending to outline structure.
  1600. \\[allout-copy-line-as-kill] `allout-copy-line-as-kill' Copy line but don't delete it.
  1601. \\[allout-yank] `allout-yank' Yank, adjusting depth of yanked topic to
  1602. depth of heading if yanking into bare topic
  1603. heading (ie, prefix sans text).
  1604. \\[allout-yank-pop] `allout-yank-pop' Is to `allout-yank' as `yank-pop' is to `yank'.
  1605. Topic-oriented Encryption:
  1606. -------------------------
  1607. \\[allout-toggle-current-subtree-encryption] `allout-toggle-current-subtree-encryption'
  1608. Encrypt/Decrypt topic content
  1609. Misc commands:
  1610. -------------
  1611. M-x outlineify-sticky Activate outline mode for current buffer,
  1612. and establish a default file-var setting
  1613. for `allout-layout'.
  1614. \\[allout-mark-topic] `allout-mark-topic'
  1615. \\[allout-copy-exposed-to-buffer] `allout-copy-exposed-to-buffer'
  1616. Duplicate outline, sans concealed text, to
  1617. buffer with name derived from derived from that
  1618. of current buffer -- \"*BUFFERNAME exposed*\".
  1619. \\[allout-flatten-exposed-to-buffer] `allout-flatten-exposed-to-buffer'
  1620. Like above `copy-exposed', but convert topic
  1621. prefixes to section.subsection... numeric
  1622. format.
  1623. \\[customize-variable] allout-auto-activation
  1624. Prepare Emacs session for allout outline mode
  1625. auto-activation.
  1626. Topic Encryption
  1627. Outline mode supports gpg encryption of topics, with support for
  1628. symmetric and key-pair modes, and auto-encryption of topics
  1629. pending encryption on save.
  1630. Topics pending encryption are, by default, automatically
  1631. encrypted during file saves, including checkpoint saves, to avoid
  1632. exposing the plain text of encrypted topics in the file system.
  1633. If the content of the topic containing the cursor was encrypted
  1634. for a save, it is automatically decrypted for continued editing.
  1635. NOTE: A few GnuPG v2 versions improperly preserve incorrect
  1636. symmetric decryption keys, preventing entry of the correct key on
  1637. subsequent decryption attempts until the cache times-out. That
  1638. can take several minutes. (Decryption of other entries is not
  1639. affected.) Upgrade your EasyPG version, if you can, and you can
  1640. deliberately clear your gpg-agent's cache by sending it a `-HUP'
  1641. signal.
  1642. See `allout-toggle-current-subtree-encryption' function docstring
  1643. and `allout-encrypt-unencrypted-on-saves' customization variable
  1644. for details.
  1645. HOT-SPOT Operation
  1646. Hot-spot operation provides a means for easy, single-keystroke outline
  1647. navigation and exposure control.
  1648. When the text cursor is positioned directly on the bullet character of
  1649. a topic, regular characters (a to z) invoke the commands of the
  1650. corresponding allout-mode keymap control chars. For example, \"f\"
  1651. would invoke the command typically bound to \"C-c<space>C-f\"
  1652. \(\\[allout-forward-current-level] `allout-forward-current-level').
  1653. Thus, by positioning the cursor on a topic bullet, you can
  1654. execute the outline navigation and manipulation commands with a
  1655. single keystroke. Regular navigation keys (eg, \\[forward-char], \\[next-line]) don't get
  1656. this special translation, so you can use them to get out of the
  1657. hot-spot and back to normal editing operation.
  1658. In allout-mode, the normal beginning-of-line command (\\[allout-beginning-of-line]) is
  1659. replaced with one that makes it easy to get to the hot-spot. If you
  1660. repeat it immediately it cycles (if `allout-beginning-of-line-cycles'
  1661. is set) to the beginning of the item and then, if you hit it again
  1662. immediately, to the hot-spot. Similarly, `allout-beginning-of-current-entry'
  1663. \(\\[allout-beginning-of-current-entry]) moves to the hot-spot when the cursor is already located
  1664. at the beginning of the current entry.
  1665. Extending Allout
  1666. Allout exposure and authoring activities all have associated
  1667. hooks, by which independent code can cooperate with allout
  1668. without changes to the allout core. Here are key ones:
  1669. `allout-mode-hook'
  1670. `allout-mode-deactivate-hook' (deprecated)
  1671. `allout-mode-off-hook'
  1672. `allout-exposure-change-functions'
  1673. `allout-structure-added-functions'
  1674. `allout-structure-deleted-functions'
  1675. `allout-structure-shifted-functions'
  1676. `allout-after-copy-or-kill-hook'
  1677. `allout-post-undo-hook'
  1678. Terminology
  1679. Topic hierarchy constituents -- TOPICS and SUBTOPICS:
  1680. ITEM: A unitary outline element, including the HEADER and ENTRY text.
  1681. TOPIC: An ITEM and any ITEMs contained within it, ie having greater DEPTH
  1682. and with no intervening items of lower DEPTH than the container.
  1683. CURRENT ITEM:
  1684. The visible ITEM most immediately containing the cursor.
  1685. DEPTH: The degree of nesting of an ITEM; it increases with containment.
  1686. The DEPTH is determined by the HEADER PREFIX. The DEPTH is also
  1687. called the:
  1688. LEVEL: The same as DEPTH.
  1689. ANCESTORS:
  1690. Those ITEMs whose TOPICs contain an ITEM.
  1691. PARENT: An ITEM's immediate ANCESTOR. It has a DEPTH one less than that
  1692. of the ITEM.
  1693. OFFSPRING:
  1694. The ITEMs contained within an ITEM's TOPIC.
  1695. SUBTOPIC:
  1696. An OFFSPRING of its ANCESTOR TOPICs.
  1697. CHILD:
  1698. An immediate SUBTOPIC of its PARENT.
  1699. SIBLINGS:
  1700. TOPICs having the same PARENT and DEPTH.
  1701. Topic text constituents:
  1702. HEADER: The first line of an ITEM, include the ITEM PREFIX and HEADER
  1703. text.
  1704. ENTRY: The text content of an ITEM, before any OFFSPRING, but including
  1705. the HEADER text and distinct from the ITEM PREFIX.
  1706. BODY: Same as ENTRY.
  1707. PREFIX: The leading text of an ITEM which distinguishes it from normal
  1708. ENTRY text. Allout recognizes the outline structure according
  1709. to the strict PREFIX format. It consists of a PREFIX-LEAD string,
  1710. PREFIX-PADDING, and a BULLET. The BULLET might be followed by a
  1711. number, indicating the ordinal number of the topic among its
  1712. siblings, or an asterisk indicating encryption, plus an optional
  1713. space. After that is the ITEM HEADER text, which is not part of
  1714. the PREFIX.
  1715. The relative length of the PREFIX determines the nesting DEPTH
  1716. of the ITEM.
  1717. PREFIX-LEAD:
  1718. The string at the beginning of a HEADER PREFIX, by default a `.'.
  1719. It can be customized by changing the setting of
  1720. `allout-header-prefix' and then reinitializing `allout-mode'.
  1721. When the PREFIX-LEAD is set to the comment-string of a
  1722. programming language, outline structuring can be embedded in
  1723. program code without interfering with processing of the text
  1724. (by Emacs or the language processor) as program code. This
  1725. setting happens automatically when allout mode is used in
  1726. programming-mode buffers. See `allout-use-mode-specific-leader'
  1727. docstring for more detail.
  1728. PREFIX-PADDING:
  1729. Spaces or asterisks which separate the PREFIX-LEAD and the
  1730. bullet, determining the ITEM's DEPTH.
  1731. BULLET: A character at the end of the ITEM PREFIX, it must be one of
  1732. the characters listed on `allout-plain-bullets-string' or
  1733. `allout-distinctive-bullets-string'. When creating a TOPIC,
  1734. plain BULLETs are by default used, according to the DEPTH of the
  1735. TOPIC. Choice among the distinctive BULLETs is offered when you
  1736. provide a universal argument (\\[universal-argument]) to the
  1737. TOPIC creation command, or when explicitly rebulleting a TOPIC. The
  1738. significance of the various distinctive bullets is purely by
  1739. convention. See the documentation for the above bullet strings for
  1740. more details.
  1741. EXPOSURE:
  1742. The state of a TOPIC which determines the on-screen visibility
  1743. of its OFFSPRING and contained ENTRY text.
  1744. CONCEALED:
  1745. TOPICs and ENTRY text whose EXPOSURE is inhibited. Concealed
  1746. text is represented by \"...\" ellipses.
  1747. CONCEALED TOPICs are effectively collapsed within an ANCESTOR.
  1748. CLOSED: A TOPIC whose immediate OFFSPRING and body-text is CONCEALED.
  1749. OPEN: A TOPIC that is not CLOSED, though its OFFSPRING or BODY may be."
  1750. ;;;_ . Code
  1751. :lighter " Allout"
  1752. :keymap 'allout-mode-map
  1753. (let ((use-layout (if (listp allout-layout)
  1754. allout-layout
  1755. allout-default-layout)))
  1756. (if (not (allout-mode-p))
  1757. (progn
  1758. ;; Deactivation:
  1759. ; Activation not explicitly
  1760. ; requested, and either in
  1761. ; active state or *de*activation
  1762. ; specifically requested:
  1763. (allout-do-resumptions)
  1764. (remove-from-invisibility-spec '(allout . t))
  1765. (remove-hook 'pre-command-hook 'allout-pre-command-business t)
  1766. (remove-hook 'post-command-hook 'allout-post-command-business t)
  1767. (remove-hook 'before-change-functions 'allout-before-change-handler t)
  1768. (remove-hook 'isearch-mode-end-hook 'allout-isearch-end-handler t)
  1769. (remove-hook 'write-contents-functions
  1770. 'allout-write-contents-hook-handler t)
  1771. (remove-overlays (point-min) (point-max)
  1772. 'category 'allout-exposure-category))
  1773. ;; Activating:
  1774. (if allout-old-style-prefixes
  1775. ;; Inhibit all the fancy formatting:
  1776. (allout-add-resumptions '(allout-primary-bullet "*")))
  1777. (allout-overlay-preparations) ; Doesn't hurt to redo this.
  1778. (allout-infer-header-lead-and-primary-bullet)
  1779. (allout-infer-body-reindent)
  1780. (set-allout-regexp)
  1781. (allout-add-resumptions '(allout-encryption-ciphertext-rejection-regexps
  1782. allout-line-boundary-regexp
  1783. extend)
  1784. '(allout-encryption-ciphertext-rejection-regexps
  1785. allout-bob-regexp
  1786. extend))
  1787. (allout-compose-and-institute-keymap)
  1788. (produce-allout-mode-menubar-entries)
  1789. (add-to-invisibility-spec '(allout . t))
  1790. (allout-add-resumptions '(line-move-ignore-invisible t))
  1791. (add-hook 'pre-command-hook 'allout-pre-command-business nil t)
  1792. (add-hook 'post-command-hook 'allout-post-command-business nil t)
  1793. (add-hook 'before-change-functions 'allout-before-change-handler nil t)
  1794. (add-hook 'isearch-mode-end-hook 'allout-isearch-end-handler nil t)
  1795. (add-hook 'write-contents-functions 'allout-write-contents-hook-handler
  1796. nil t)
  1797. ;; Stash auto-fill settings and adjust so custom allout auto-fill
  1798. ;; func will be used if auto-fill is active or activated. (The
  1799. ;; custom func respects topic headline, maintains hanging-indents,
  1800. ;; etc.)
  1801. (allout-add-resumptions (list 'allout-former-auto-filler
  1802. auto-fill-function)
  1803. ;; Register allout-auto-fill to be used if
  1804. ;; filling is active:
  1805. (list 'allout-outside-normal-auto-fill-function
  1806. normal-auto-fill-function)
  1807. '(normal-auto-fill-function allout-auto-fill)
  1808. ;; Paragraphs are broken by topic headlines.
  1809. (list 'paragraph-start
  1810. (concat paragraph-start "\\|^\\("
  1811. allout-regexp "\\)"))
  1812. (list 'paragraph-separate
  1813. (concat paragraph-separate "\\|^\\("
  1814. allout-regexp "\\)")))
  1815. (if (and auto-fill-function (not allout-inhibit-auto-fill))
  1816. ;; allout-auto-fill will use the stashed values and so forth.
  1817. (allout-add-resumptions '(auto-fill-function allout-auto-fill)))
  1818. (allout-setup-menubar)
  1819. ;; Do auto layout if warranted:
  1820. (when (and allout-layout
  1821. allout-auto-activation
  1822. use-layout
  1823. (and (not (string= allout-auto-activation "activate"))
  1824. (if (string= allout-auto-activation "ask")
  1825. (if (y-or-n-p (format-message
  1826. "Expose %s with layout `%s'? "
  1827. (buffer-name) use-layout))
  1828. t
  1829. (message "Skipped %s layout." (buffer-name))
  1830. nil)
  1831. t)))
  1832. (save-excursion
  1833. (message "Adjusting `%s' exposure..." (buffer-name))
  1834. (goto-char 0)
  1835. (allout-this-or-next-heading)
  1836. (condition-case err
  1837. (progn
  1838. (apply 'allout-expose-topic (list use-layout))
  1839. (message "Adjusting `%s' exposure... done."
  1840. (buffer-name)))
  1841. ;; Problem applying exposure -- notify user, but don't
  1842. ;; interrupt, eg, file visit:
  1843. (error (message "%s" (car (cdr err)))
  1844. (sit-for 1))))
  1845. ) ; when allout-layout
  1846. ) ; if (allout-mode-p)
  1847. ) ; let (())
  1848. ) ; define-minor-mode
  1849. ;;;_ > allout-minor-mode alias
  1850. (defalias 'allout-minor-mode 'allout-mode)
  1851. ;;;_ > allout-unload-function
  1852. (defun allout-unload-function ()
  1853. "Unload the allout outline library."
  1854. (save-current-buffer
  1855. (dolist (buffer (buffer-list))
  1856. (set-buffer buffer)
  1857. (when (allout-mode-p) (allout-mode -1))))
  1858. ;; continue standard unloading
  1859. nil)
  1860. ;;;_ - Position Assessment
  1861. ;;;_ > allout-hidden-p (&optional pos)
  1862. (defsubst allout-hidden-p (&optional pos)
  1863. "Non-nil if the character after point was made invisible by allout."
  1864. (eq (get-char-property (or pos (point)) 'invisible) 'allout))
  1865. ;;;_ > allout-overlay-insert-in-front-handler (ol after beg end
  1866. ;;; &optional prelen)
  1867. (defun allout-overlay-insert-in-front-handler (ol after beg _end
  1868. &optional _prelen)
  1869. "Shift the overlay so stuff inserted in front of it is excluded."
  1870. (if after
  1871. ;; ??? Shouldn't moving the overlay should be unnecessary, if overlay
  1872. ;; front-advance on the overlay worked as expected?
  1873. (move-overlay ol (1+ beg) (overlay-end ol))))
  1874. ;;;_ > allout-overlay-interior-modification-handler (ol after beg end
  1875. ;;; &optional prelen)
  1876. (defun allout-overlay-interior-modification-handler (ol after beg end
  1877. &optional _prelen)
  1878. "Get confirmation before making arbitrary changes to invisible text.
  1879. We expose the invisible text and ask for confirmation. Refusal or
  1880. `keyboard-quit' abandons the changes, with keyboard-quit additionally
  1881. reclosing the opened text.
  1882. No confirmation is necessary when `inhibit-read-only' is set -- eg, allout
  1883. internal functions use this feature cohesively bunch changes."
  1884. (when (and (not inhibit-read-only) (not after))
  1885. (let ((start (point))
  1886. (ol-start (overlay-start ol))
  1887. (ol-end (overlay-end ol))
  1888. first)
  1889. (goto-char beg)
  1890. (while (< (point) end)
  1891. (when (allout-hidden-p)
  1892. (allout-show-to-offshoot)
  1893. (if (allout-hidden-p)
  1894. (save-excursion (forward-char 1)
  1895. (allout-show-to-offshoot)))
  1896. (when (not first)
  1897. (setq first (point))))
  1898. (goto-char (if (featurep 'xemacs)
  1899. (next-property-change (1+ (point)) nil end)
  1900. (next-char-property-change (1+ (point)) end))))
  1901. (when first
  1902. (goto-char first)
  1903. (condition-case nil
  1904. (if (not
  1905. (yes-or-no-p
  1906. (substitute-command-keys
  1907. (concat "Modify concealed text? (\"no\" just aborts,"
  1908. " \\[keyboard-quit] also reconceals) "))))
  1909. (progn (goto-char start)
  1910. (error "Concealed-text change refused")))
  1911. (quit (allout-flag-region ol-start ol-end nil)
  1912. (allout-flag-region ol-start ol-end t)
  1913. (error "Concealed-text change abandoned, text reconcealed"))))
  1914. (goto-char start))))
  1915. ;;;_ > allout-before-change-handler (beg end)
  1916. (defun allout-before-change-handler (beg end)
  1917. "Protect against changes to invisible text.
  1918. See `allout-overlay-interior-modification-handler' for details."
  1919. (when (and (allout-mode-p) undo-in-progress)
  1920. (setq allout-just-did-undo t)
  1921. (if (allout-hidden-p)
  1922. (allout-show-children)))
  1923. ;; allout-overlay-interior-modification-handler on an overlay handles
  1924. ;; this in other emacs, via `allout-exposure-category's 'modification-hooks.
  1925. (when (and (featurep 'xemacs) (allout-mode-p))
  1926. ;; process all of the pending overlays:
  1927. (save-excursion
  1928. (goto-char beg)
  1929. (let ((overlay (allout-get-invisibility-overlay)))
  1930. (if overlay
  1931. (allout-overlay-interior-modification-handler
  1932. overlay nil beg end nil))))))
  1933. ;;;_ > allout-isearch-end-handler (&optional overlay)
  1934. (defun allout-isearch-end-handler (&optional _overlay)
  1935. "Reconcile allout outline exposure on arriving in hidden text after isearch.
  1936. Optional OVERLAY parameter is for when this function is used by
  1937. `isearch-open-invisible' overlay property. It is otherwise unused, so this
  1938. function can also be used as an `isearch-mode-end-hook'."
  1939. (if (and (allout-mode-p) (allout-hidden-p))
  1940. (allout-show-to-offshoot)))
  1941. ;;;_ #3 Internal Position State-Tracking -- "allout-recent-*" funcs
  1942. ;; All the basic outline functions that directly do string matches to
  1943. ;; evaluate heading prefix location set the variables
  1944. ;; `allout-recent-prefix-beginning' and `allout-recent-prefix-end'
  1945. ;; when successful. Functions starting with `allout-recent-' all
  1946. ;; use this state, providing the means to avoid redundant searches
  1947. ;; for just-established data. This optimization can provide
  1948. ;; significant speed improvement, but it must be employed carefully.
  1949. ;;;_ = allout-recent-prefix-beginning
  1950. (defvar allout-recent-prefix-beginning 0
  1951. "Buffer point of the start of the last topic prefix encountered.")
  1952. (make-variable-buffer-local 'allout-recent-prefix-beginning)
  1953. ;;;_ = allout-recent-prefix-end
  1954. (defvar allout-recent-prefix-end 0
  1955. "Buffer point of the end of the last topic prefix encountered.")
  1956. (make-variable-buffer-local 'allout-recent-prefix-end)
  1957. ;;;_ = allout-recent-depth
  1958. (defvar allout-recent-depth 0
  1959. "Depth of the last topic prefix encountered.")
  1960. (make-variable-buffer-local 'allout-recent-depth)
  1961. ;;;_ = allout-recent-end-of-subtree
  1962. (defvar allout-recent-end-of-subtree 0
  1963. "Buffer point last returned by `allout-end-of-current-subtree'.")
  1964. (make-variable-buffer-local 'allout-recent-end-of-subtree)
  1965. ;;;_ > allout-prefix-data ()
  1966. (defsubst allout-prefix-data ()
  1967. "Register allout-prefix state data.
  1968. For reference by `allout-recent' funcs. Return
  1969. the new value of `allout-recent-prefix-beginning'."
  1970. (setq allout-recent-prefix-end (or (match-end 1) (match-end 2) (match-end 3))
  1971. allout-recent-prefix-beginning (or (match-beginning 1)
  1972. (match-beginning 2)
  1973. (match-beginning 3))
  1974. allout-recent-depth (max 1 (- allout-recent-prefix-end
  1975. allout-recent-prefix-beginning
  1976. allout-header-subtraction)))
  1977. allout-recent-prefix-beginning)
  1978. ;;;_ > nullify-allout-prefix-data ()
  1979. (defsubst nullify-allout-prefix-data ()
  1980. "Mark allout prefix data as being uninformative."
  1981. (setq allout-recent-prefix-end (point)
  1982. allout-recent-prefix-beginning (point)
  1983. allout-recent-depth 0)
  1984. allout-recent-prefix-beginning)
  1985. ;;;_ > allout-recent-depth ()
  1986. (defsubst allout-recent-depth ()
  1987. "Return depth of last heading encountered by an outline maneuvering function.
  1988. All outline functions which directly do string matches to assess
  1989. headings set the variables `allout-recent-prefix-beginning' and
  1990. `allout-recent-prefix-end' if successful. This function uses those settings
  1991. to return the current depth."
  1992. allout-recent-depth)
  1993. ;;;_ > allout-recent-prefix ()
  1994. (defsubst allout-recent-prefix ()
  1995. "Like `allout-recent-depth', but returns text of last encountered prefix.
  1996. All outline functions which directly do string matches to assess
  1997. headings set the variables `allout-recent-prefix-beginning' and
  1998. `allout-recent-prefix-end' if successful. This function uses those settings
  1999. to return the current prefix."
  2000. (buffer-substring-no-properties allout-recent-prefix-beginning
  2001. allout-recent-prefix-end))
  2002. ;;;_ > allout-recent-bullet ()
  2003. (defmacro allout-recent-bullet ()
  2004. "Like `allout-recent-prefix', but returns bullet of last encountered prefix.
  2005. All outline functions which directly do string matches to assess
  2006. headings set the variables `allout-recent-prefix-beginning' and
  2007. `allout-recent-prefix-end' if successful. This function uses those settings
  2008. to return the current depth of the most recently matched topic."
  2009. '(buffer-substring-no-properties (1- allout-recent-prefix-end)
  2010. allout-recent-prefix-end))
  2011. ;;;_ #4 Navigation
  2012. ;;;_ - Position Assessment
  2013. ;;;_ : Location Predicates
  2014. ;;;_ > allout-do-doublecheck ()
  2015. (defsubst allout-do-doublecheck ()
  2016. "True if current item conditions qualify for checking on topic aberrance."
  2017. (and
  2018. ;; presume integrity of outline and yanked content during yank -- necessary
  2019. ;; to allow for level disparity of yank location and yanked text:
  2020. (not allout-inhibit-aberrance-doublecheck)
  2021. ;; allout-doublecheck-at-and-shallower is ceiling for doublecheck:
  2022. (<= allout-recent-depth allout-doublecheck-at-and-shallower)))
  2023. ;;;_ > allout-aberrant-container-p ()
  2024. (defun allout-aberrant-container-p ()
  2025. "True if topic, or next sibling with children, contains them discontinuously.
  2026. Discontinuous means an immediate offspring that is nested more
  2027. than one level deeper than the topic.
  2028. If topic has no offspring, then the next sibling with offspring will
  2029. determine whether or not this one is determined to be aberrant.
  2030. If true, then the allout-recent-* settings are calibrated on the
  2031. offspring that qualifies it as aberrant, ie with depth that
  2032. exceeds the topic by more than one."
  2033. ;; This is most clearly understood when considering standard-prefix-leader
  2034. ;; low-level topics, which can all too easily match text not intended as
  2035. ;; headers. For example, any line with a leading '.' or '*' and lacking a
  2036. ;; following bullet qualifies without this protection. (A sequence of
  2037. ;; them can occur naturally, eg a typical textual bullet list.) We
  2038. ;; disqualify such low-level sequences when they are followed by a
  2039. ;; discontinuously contained child, inferring that the sequences are not
  2040. ;; actually connected with their prospective context.
  2041. (let ((depth (allout-depth))
  2042. (start-point (point))
  2043. done aberrant)
  2044. (save-match-data
  2045. (save-excursion
  2046. (while (and (not done)
  2047. (re-search-forward allout-line-boundary-regexp nil 0))
  2048. (allout-prefix-data)
  2049. (goto-char allout-recent-prefix-beginning)
  2050. (cond
  2051. ;; sibling -- continue:
  2052. ((eq allout-recent-depth depth))
  2053. ;; first offspring is excessive -- aberrant:
  2054. ((> allout-recent-depth (1+ depth))
  2055. (setq done t aberrant t))
  2056. ;; next non-sibling is lower-depth -- not aberrant:
  2057. (t (setq done t))))))
  2058. (if aberrant
  2059. aberrant
  2060. (goto-char start-point)
  2061. ;; recalibrate allout-recent-*
  2062. (allout-depth)
  2063. nil)))
  2064. ;;;_ > allout-on-current-heading-p ()
  2065. (defun allout-on-current-heading-p ()
  2066. "Return non-nil if point is on current visible topics' header line.
  2067. Actually, returns prefix beginning point."
  2068. (save-excursion
  2069. (allout-beginning-of-current-line)
  2070. (save-match-data
  2071. (and (looking-at allout-regexp)
  2072. (allout-prefix-data)
  2073. (or (not (allout-do-doublecheck))
  2074. (not (allout-aberrant-container-p)))))))
  2075. ;;;_ > allout-on-heading-p ()
  2076. (defalias 'allout-on-heading-p 'allout-on-current-heading-p)
  2077. ;;;_ > allout-e-o-prefix-p ()
  2078. (defun allout-e-o-prefix-p ()
  2079. "True if point is located where current topic prefix ends, heading begins."
  2080. (and (save-match-data
  2081. (save-excursion (let ((inhibit-field-text-motion t))
  2082. (beginning-of-line))
  2083. (looking-at allout-regexp))
  2084. (= (point) (save-excursion (allout-end-of-prefix)(point))))))
  2085. ;;;_ : Location attributes
  2086. ;;;_ > allout-depth ()
  2087. (defun allout-depth ()
  2088. "Return depth of topic most immediately containing point.
  2089. Does not do doublecheck for aberrant topic header.
  2090. Return zero if point is not within any topic.
  2091. Like `allout-current-depth', but respects hidden as well as visible topics."
  2092. (save-excursion
  2093. (let ((start-point (point)))
  2094. (if (and (allout-goto-prefix)
  2095. (not (< start-point (point))))
  2096. allout-recent-depth
  2097. (progn
  2098. ;; Oops, no prefix, nullify it:
  2099. (nullify-allout-prefix-data)
  2100. ;; ... and return 0:
  2101. 0)))))
  2102. ;;;_ > allout-current-depth ()
  2103. (defun allout-current-depth ()
  2104. "Return depth of visible topic most immediately containing point.
  2105. Return zero if point is not within any topic."
  2106. (save-excursion
  2107. (if (allout-back-to-current-heading)
  2108. (max 1
  2109. (- allout-recent-prefix-end
  2110. allout-recent-prefix-beginning
  2111. allout-header-subtraction))
  2112. 0)))
  2113. ;;;_ > allout-get-current-prefix ()
  2114. (defun allout-get-current-prefix ()
  2115. "Topic prefix of the current topic."
  2116. (save-excursion
  2117. (if (allout-goto-prefix)
  2118. (allout-recent-prefix))))
  2119. ;;;_ > allout-get-bullet ()
  2120. (defun allout-get-bullet ()
  2121. "Return bullet of containing topic (visible or not)."
  2122. (save-excursion
  2123. (and (allout-goto-prefix)
  2124. (allout-recent-bullet))))
  2125. ;;;_ > allout-current-bullet ()
  2126. (defun allout-current-bullet ()
  2127. "Return bullet of current (visible) topic heading, or none if none found."
  2128. (condition-case nil
  2129. (save-excursion
  2130. (allout-back-to-current-heading)
  2131. (buffer-substring-no-properties (- allout-recent-prefix-end 1)
  2132. allout-recent-prefix-end))
  2133. ;; Quick and dirty provision, ostensibly for missing bullet:
  2134. (args-out-of-range nil))
  2135. )
  2136. ;;;_ > allout-get-prefix-bullet (prefix)
  2137. (defun allout-get-prefix-bullet (prefix)
  2138. "Return the bullet of the header prefix string PREFIX."
  2139. ;; Doesn't make sense if we're old-style prefixes, but this just
  2140. ;; oughtn't be called then, so forget about it...
  2141. (if (string-match allout-regexp prefix)
  2142. (substring prefix (1- (match-end 2)) (match-end 2))))
  2143. ;;;_ > allout-sibling-index (&optional depth)
  2144. (defun allout-sibling-index (&optional depth)
  2145. "Item number of this prospective topic among its siblings.
  2146. If optional arg DEPTH is greater than current depth, then we're
  2147. opening a new level, and return 0.
  2148. If less than this depth, ascend to that depth and count..."
  2149. (save-excursion
  2150. (cond ((and depth (<= depth 0) 0))
  2151. ((or (null depth) (= depth (allout-depth)))
  2152. (let ((index 1))
  2153. (while (allout-previous-sibling allout-recent-depth nil)
  2154. (setq index (1+ index)))
  2155. index))
  2156. ((< depth allout-recent-depth)
  2157. (allout-ascend-to-depth depth)
  2158. (allout-sibling-index))
  2159. (0))))
  2160. ;;;_ > allout-topic-flat-index ()
  2161. (defun allout-topic-flat-index ()
  2162. "Return a list indicating point's numeric section.subsect.subsubsect...
  2163. Outermost is first."
  2164. (let* ((depth (allout-depth))
  2165. (next-index (allout-sibling-index depth))
  2166. (rev-sibls nil))
  2167. (while (> next-index 0)
  2168. (setq rev-sibls (cons next-index rev-sibls))
  2169. (setq depth (1- depth))
  2170. (setq next-index (allout-sibling-index depth)))
  2171. rev-sibls)
  2172. )
  2173. ;;;_ - Navigation routines
  2174. ;;;_ > allout-beginning-of-current-line ()
  2175. (defun allout-beginning-of-current-line ()
  2176. "Like beginning of line, but to visible text."
  2177. ;; This combination of move-beginning-of-line and beginning-of-line is
  2178. ;; deliberate, but the (beginning-of-line) may now be superfluous.
  2179. (let ((inhibit-field-text-motion t))
  2180. (move-beginning-of-line 1)
  2181. (beginning-of-line)
  2182. (while (and (not (bobp)) (or (not (bolp)) (allout-hidden-p)))
  2183. (beginning-of-line)
  2184. (if (or (allout-hidden-p) (not (bolp)))
  2185. (forward-char -1)))))
  2186. ;;;_ > allout-end-of-current-line ()
  2187. (defun allout-end-of-current-line ()
  2188. "Move to the end of line, past concealed text if any."
  2189. ;; This is for symmetry with `allout-beginning-of-current-line' --
  2190. ;; `move-end-of-line' doesn't suffer the same problem as
  2191. ;; `move-beginning-of-line'.
  2192. (let ((inhibit-field-text-motion t))
  2193. (end-of-line)
  2194. (while (allout-hidden-p)
  2195. (end-of-line)
  2196. (if (allout-hidden-p) (forward-char 1)))))
  2197. ;;;_ > allout-beginning-of-line ()
  2198. (defun allout-beginning-of-line ()
  2199. "Beginning-of-line with `allout-beginning-of-line-cycles' behavior, if set."
  2200. (interactive)
  2201. (if (or (not allout-beginning-of-line-cycles)
  2202. (not (equal last-command this-command)))
  2203. (progn
  2204. (if (and (not (bolp))
  2205. (allout-hidden-p (1- (point))))
  2206. (goto-char (allout-previous-single-char-property-change
  2207. (1- (point)) 'invisible)))
  2208. (move-beginning-of-line 1))
  2209. (allout-depth)
  2210. (let ((beginning-of-body
  2211. (save-excursion
  2212. (while (and (allout-do-doublecheck)
  2213. (allout-aberrant-container-p)
  2214. (allout-previous-visible-heading 1)))
  2215. (allout-beginning-of-current-entry)
  2216. (point))))
  2217. (cond ((= (current-column) 0)
  2218. (goto-char beginning-of-body))
  2219. ((< (point) beginning-of-body)
  2220. (allout-beginning-of-current-line))
  2221. ((= (point) beginning-of-body)
  2222. (goto-char (allout-current-bullet-pos)))
  2223. (t (allout-beginning-of-current-line)
  2224. (if (< (point) beginning-of-body)
  2225. ;; we were on the headline after its start:
  2226. (goto-char beginning-of-body)))))))
  2227. ;;;_ > allout-end-of-line ()
  2228. (defun allout-end-of-line ()
  2229. "End-of-line with `allout-end-of-line-cycles' behavior, if set."
  2230. (interactive)
  2231. (if (or (not allout-end-of-line-cycles)
  2232. (not (equal last-command this-command)))
  2233. (allout-end-of-current-line)
  2234. (let ((end-of-entry (save-excursion
  2235. (allout-end-of-entry)
  2236. (point))))
  2237. (cond ((not (eolp))
  2238. (allout-end-of-current-line))
  2239. ((or (allout-hidden-p) (save-excursion
  2240. (forward-char -1)
  2241. (allout-hidden-p)))
  2242. (allout-back-to-current-heading)
  2243. (allout-show-current-entry)
  2244. (allout-show-children)
  2245. (allout-end-of-entry))
  2246. ((>= (point) end-of-entry)
  2247. (allout-back-to-current-heading)
  2248. (allout-end-of-current-line))
  2249. (t
  2250. (if (not (allout-mark-active-p))
  2251. (push-mark))
  2252. (allout-end-of-entry))))))
  2253. ;;;_ > allout-mark-active-p ()
  2254. (defun allout-mark-active-p ()
  2255. "True if the mark is currently or always active."
  2256. ;; `(cond (boundp...))' (or `(if ...)') invokes special byte-compiler
  2257. ;; provisions, at least in GNU Emacs to prevent warnings about lack of,
  2258. ;; eg, region-active-p.
  2259. (cond ((boundp 'mark-active)
  2260. mark-active)
  2261. ((fboundp 'region-active-p)
  2262. (region-active-p))
  2263. (t)))
  2264. ;;;_ > allout-next-heading ()
  2265. (defsubst allout-next-heading ()
  2266. "Move to the heading for the topic (possibly invisible) after this one.
  2267. Returns the location of the heading, or nil if none found.
  2268. We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
  2269. (save-match-data
  2270. (if (looking-at allout-regexp)
  2271. (forward-char 1))
  2272. (when (re-search-forward allout-line-boundary-regexp nil 0)
  2273. (allout-prefix-data)
  2274. (goto-char allout-recent-prefix-beginning)
  2275. (while (not (bolp))
  2276. (forward-char -1))
  2277. (and (allout-do-doublecheck)
  2278. ;; this will set allout-recent-* on the first non-aberrant topic,
  2279. ;; whether it's the current one or one that disqualifies it:
  2280. (allout-aberrant-container-p))
  2281. ;; this may or may not be the same as above depending on doublecheck:
  2282. (goto-char allout-recent-prefix-beginning))))
  2283. ;;;_ > allout-this-or-next-heading
  2284. (defun allout-this-or-next-heading ()
  2285. "Position cursor on current or next heading."
  2286. ;; A throwaway non-macro that is defined after allout-next-heading
  2287. ;; and usable by allout-mode.
  2288. (if (not (allout-goto-prefix-doublechecked)) (allout-next-heading)))
  2289. ;;;_ > allout-previous-heading ()
  2290. (defun allout-previous-heading ()
  2291. "Move to the prior (possibly invisible) heading line.
  2292. Return the location of the beginning of the heading, or nil if not found.
  2293. We skip anomalous low-level topics, a la `allout-aberrant-container-p'."
  2294. (if (bobp)
  2295. nil
  2296. (let ((start-point (point)))
  2297. ;; allout-goto-prefix-doublechecked calls us, so we can't use it here.
  2298. (allout-goto-prefix)
  2299. (save-match-data
  2300. (when (or (re-search-backward allout-line-boundary-regexp nil 0)
  2301. (looking-at allout-bob-regexp))
  2302. (goto-char (allout-prefix-data))
  2303. (if (and (allout-do-doublecheck)
  2304. (allout-aberrant-container-p))
  2305. (or (allout-previous-heading)
  2306. (and (goto-char start-point)
  2307. ;; recalibrate allout-recent-*:
  2308. (allout-depth)
  2309. nil))
  2310. (point)))))))
  2311. ;;;_ > allout-get-invisibility-overlay ()
  2312. (defun allout-get-invisibility-overlay ()
  2313. "Return the overlay at point that dictates allout invisibility."
  2314. (let ((overlays (overlays-at (point)))
  2315. got)
  2316. (while (and overlays (not got))
  2317. (if (equal (overlay-get (car overlays) 'invisible) 'allout)
  2318. (setq got (car overlays))
  2319. (pop overlays)))
  2320. got))
  2321. ;;;_ > allout-back-to-visible-text ()
  2322. (defun allout-back-to-visible-text ()
  2323. "Move to most recent prior character that is visible, and return point."
  2324. (if (allout-hidden-p)
  2325. (goto-char (overlay-start (allout-get-invisibility-overlay))))
  2326. (point))
  2327. ;;;_ - Subtree Charting
  2328. ;;;_ " These routines either produce or assess charts, which are
  2329. ;;; nested lists of the locations of topics within a subtree.
  2330. ;;;
  2331. ;;; Charts enable efficient subtree navigation by providing a reusable basis
  2332. ;;; for elaborate, compound assessment and adjustment of a subtree.
  2333. ;;;_ > allout-chart-subtree (&optional levels visible orig-depth prev-depth)
  2334. (defun allout-chart-subtree (&optional levels visible orig-depth prev-depth)
  2335. "Produce a location \"chart\" of subtopics of the containing topic.
  2336. Optional argument LEVELS specifies a depth limit (relative to start
  2337. depth) for the chart. Null LEVELS means no limit.
  2338. When optional argument VISIBLE is non-nil, the chart includes
  2339. only the visible subelements of the charted subjects.
  2340. The remaining optional args are for internal use by the function.
  2341. Point is left at the end of the subtree.
  2342. Charts are used to capture outline structure, so that outline-altering
  2343. routines need to assess the structure only once, and then use the chart
  2344. for their elaborate manipulations.
  2345. The chart entries for the topics are in reverse order, so the
  2346. last topic is listed first. The entry for each topic consists of
  2347. an integer indicating the point at the beginning of the topic
  2348. prefix. Charts for offspring consist of a list containing,
  2349. recursively, the charts for the respective subtopics. The chart
  2350. for a topics' offspring precedes the entry for the topic itself.
  2351. The other function parameters are for internal recursion, and should
  2352. not be specified by external callers. ORIG-DEPTH is depth of topic at
  2353. starting point, and PREV-DEPTH is depth of prior topic."
  2354. (let ((original (not orig-depth)) ; `orig-depth' set only in recursion.
  2355. chart curr-depth)
  2356. (if original ; Just starting?
  2357. ; Register initial settings and
  2358. ; position to first offspring:
  2359. (progn (setq orig-depth (allout-depth))
  2360. (or prev-depth (setq prev-depth (1+ orig-depth)))
  2361. (if visible
  2362. (allout-next-visible-heading 1)
  2363. (allout-next-heading))))
  2364. ;; Loop over the current levels' siblings. Besides being more
  2365. ;; efficient than tail-recursing over a level, it avoids exceeding
  2366. ;; the typically quite constrained Emacs max-lisp-eval-depth.
  2367. ;;
  2368. ;; Probably would speed things up to implement loop-based stack
  2369. ;; operation rather than recursing for lower levels. Bah.
  2370. (while (and (not (eobp))
  2371. ; Still within original topic?
  2372. (< orig-depth (setq curr-depth allout-recent-depth))
  2373. (cond ((= prev-depth curr-depth)
  2374. ;; Register this one and move on:
  2375. (setq chart (cons allout-recent-prefix-beginning chart))
  2376. (if (and levels (<= levels 1))
  2377. ;; At depth limit -- skip sublevels:
  2378. (or (allout-next-sibling curr-depth)
  2379. ;; or no more siblings -- proceed to
  2380. ;; next heading at lesser depth:
  2381. (while (and (<= curr-depth
  2382. allout-recent-depth)
  2383. (if visible
  2384. (allout-next-visible-heading 1)
  2385. (allout-next-heading)))))
  2386. (if visible
  2387. (allout-next-visible-heading 1)
  2388. (allout-next-heading))))
  2389. ((and (< prev-depth curr-depth)
  2390. (or (not levels)
  2391. (> levels 0)))
  2392. ;; Recurse on deeper level of curr topic:
  2393. (setq chart
  2394. (cons (allout-chart-subtree (and levels
  2395. (1- levels))
  2396. visible
  2397. orig-depth
  2398. curr-depth)
  2399. chart))
  2400. ;; ... then continue with this one.
  2401. )
  2402. ;; ... else nil if we've ascended back to prev-depth.
  2403. )))
  2404. (if original ; We're at the last sibling on
  2405. ; the original level. Position
  2406. ; to the end of it:
  2407. (progn (and (not (eobp)) (forward-char -1))
  2408. (and (= (preceding-char) ?\n)
  2409. (= (aref (buffer-substring (max 1 (- (point) 3))
  2410. (point))
  2411. 1)
  2412. ?\n)
  2413. (forward-char -1))
  2414. (setq allout-recent-end-of-subtree (point))))
  2415. chart ; (nreverse chart) not necessary,
  2416. ; and maybe not preferable.
  2417. ))
  2418. ;;;_ > allout-chart-siblings (&optional start end)
  2419. (defun allout-chart-siblings (&optional _start _end)
  2420. "Produce a list of locations of this and succeeding sibling topics.
  2421. Effectively a top-level chart of siblings. See `allout-chart-subtree'
  2422. for an explanation of charts."
  2423. (save-excursion
  2424. (when (allout-goto-prefix-doublechecked)
  2425. (let ((chart (list (point))))
  2426. (while (allout-next-sibling)
  2427. (setq chart (cons (point) chart)))
  2428. (if chart (setq chart (nreverse chart)))))))
  2429. ;;;_ > allout-chart-to-reveal (chart depth)
  2430. (defun allout-chart-to-reveal (chart depth)
  2431. "Return a flat list of hidden points in subtree CHART, up to DEPTH.
  2432. If DEPTH is nil, include hidden points at any depth.
  2433. Note that point can be left at any of the points on chart, or at the
  2434. start point."
  2435. (let (result here)
  2436. (while (and (or (null depth) (> depth 0))
  2437. chart)
  2438. (setq here (car chart))
  2439. (if (listp here)
  2440. (let ((further (allout-chart-to-reveal here (if (null depth)
  2441. depth
  2442. (1- depth)))))
  2443. ;; We're on the start of a subtree -- recurse with it, if there's
  2444. ;; more depth to go:
  2445. (if further (setq result (append further result)))
  2446. (setq chart (cdr chart)))
  2447. (goto-char here)
  2448. (if (allout-hidden-p)
  2449. (setq result (cons here result)))
  2450. (setq chart (cdr chart))))
  2451. result))
  2452. ;;;_ X allout-chart-spec (chart spec &optional exposing)
  2453. ;; (defun allout-chart-spec (chart spec &optional exposing)
  2454. ;; "Not yet (if ever) implemented.
  2455. ;; Produce exposure directives given topic/subtree CHART and an exposure SPEC.
  2456. ;; Exposure spec indicates the locations to be exposed and the prescribed
  2457. ;; exposure status. Optional arg EXPOSING is an integer, with 0
  2458. ;; indicating pending concealment, anything higher indicating depth to
  2459. ;; which subtopic headers should be exposed, and negative numbers
  2460. ;; indicating (negative of) the depth to which subtopic headers and
  2461. ;; bodies should be exposed.
  2462. ;; The produced list can have two types of entries. Bare numbers
  2463. ;; indicate points in the buffer where topic headers that should be
  2464. ;; exposed reside.
  2465. ;; - bare negative numbers indicates that the topic starting at the
  2466. ;; point which is the negative of the number should be opened,
  2467. ;; including their entries.
  2468. ;; - bare positive values indicate that this topic header should be
  2469. ;; opened.
  2470. ;; - Lists signify the beginning and end points of regions that should
  2471. ;; be flagged, and the flag to employ. (For concealment: `(\?r)', and
  2472. ;; exposure:"
  2473. ;; (while spec
  2474. ;; (cond ((listp spec)
  2475. ;; )
  2476. ;; )
  2477. ;; (setq spec (cdr spec)))
  2478. ;; )
  2479. ;;;_ - Within Topic
  2480. ;;;_ > allout-goto-prefix ()
  2481. (defun allout-goto-prefix ()
  2482. "Put point at beginning of immediately containing outline topic.
  2483. Goes to most immediate subsequent topic if none immediately containing.
  2484. Not sensitive to topic visibility.
  2485. Returns the point at the beginning of the prefix, or nil if none."
  2486. (save-match-data
  2487. (let (done)
  2488. (while (and (not done)
  2489. (search-backward "\n" nil 1))
  2490. (forward-char 1)
  2491. (if (looking-at allout-regexp)
  2492. (setq done (allout-prefix-data))
  2493. (forward-char -1)))
  2494. (if (bobp)
  2495. (cond ((looking-at allout-regexp)
  2496. (allout-prefix-data))
  2497. ((allout-next-heading))
  2498. (done))
  2499. done))))
  2500. ;;;_ > allout-goto-prefix-doublechecked ()
  2501. (defun allout-goto-prefix-doublechecked ()
  2502. "Put point at beginning of immediately containing outline topic.
  2503. Like `allout-goto-prefix', but shallow topics (according to
  2504. `allout-doublecheck-at-and-shallower') are checked and
  2505. disqualified for child containment discontinuity, according to
  2506. `allout-aberrant-container-p'."
  2507. (if (allout-goto-prefix)
  2508. (if (and (allout-do-doublecheck)
  2509. (allout-aberrant-container-p))
  2510. (allout-previous-heading)
  2511. (point))))
  2512. ;;;_ > allout-end-of-prefix ()
  2513. (defun allout-end-of-prefix (&optional ignore-decorations)
  2514. "Position cursor at beginning of header text.
  2515. If optional IGNORE-DECORATIONS is non-nil, put just after bullet,
  2516. otherwise skip white space between bullet and ensuing text."
  2517. (if (not (allout-goto-prefix-doublechecked))
  2518. nil
  2519. (goto-char allout-recent-prefix-end)
  2520. (save-match-data
  2521. (if ignore-decorations
  2522. t
  2523. (while (looking-at "[0-9]") (forward-char 1))
  2524. (if (and (not (eolp)) (looking-at "\\s-")) (forward-char 1))))
  2525. ;; Reestablish where we are:
  2526. (allout-current-depth)))
  2527. ;;;_ > allout-current-bullet-pos ()
  2528. (defun allout-current-bullet-pos ()
  2529. "Return position of current (visible) topic's bullet."
  2530. (if (not (allout-current-depth))
  2531. nil
  2532. (1- allout-recent-prefix-end)))
  2533. ;;;_ > allout-back-to-current-heading (&optional interactive)
  2534. (defun allout-back-to-current-heading (&optional interactive)
  2535. "Move to heading line of current topic, or beginning if not in a topic.
  2536. If interactive, we position at the end of the prefix.
  2537. Return value of resulting point, unless we started outside
  2538. of (before any) topics, in which case we return nil."
  2539. (interactive "p")
  2540. (allout-beginning-of-current-line)
  2541. (let ((bol-point (point)))
  2542. (when (allout-goto-prefix-doublechecked)
  2543. (if (<= (point) bol-point)
  2544. (progn
  2545. (setq bol-point (point))
  2546. (allout-beginning-of-current-line)
  2547. (if (not (= bol-point (point)))
  2548. (if (looking-at allout-regexp)
  2549. (allout-prefix-data)))
  2550. (if interactive
  2551. (allout-end-of-prefix)
  2552. (point)))
  2553. (goto-char (point-min))
  2554. nil))))
  2555. ;;;_ > allout-back-to-heading ()
  2556. (defalias 'allout-back-to-heading 'allout-back-to-current-heading)
  2557. ;;;_ > allout-pre-next-prefix ()
  2558. (defun allout-pre-next-prefix ()
  2559. "Skip forward to just before the next heading line.
  2560. Returns that character position."
  2561. (if (allout-next-heading)
  2562. (goto-char (1- allout-recent-prefix-beginning))))
  2563. ;;;_ > allout-end-of-subtree (&optional current include-trailing-blank)
  2564. (defun allout-end-of-subtree (&optional current include-trailing-blank)
  2565. "Put point at the end of the last leaf in the containing topic.
  2566. Optional CURRENT means put point at the end of the containing
  2567. visible topic.
  2568. Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
  2569. any, as part of the subtree. Otherwise, that trailing blank will be
  2570. excluded as delimiting whitespace between topics.
  2571. Returns the value of point."
  2572. (interactive "P")
  2573. (if current
  2574. (allout-back-to-current-heading)
  2575. (allout-goto-prefix-doublechecked))
  2576. (let ((level allout-recent-depth))
  2577. (allout-next-heading)
  2578. (while (and (not (eobp))
  2579. (> allout-recent-depth level))
  2580. (allout-next-heading))
  2581. (if (eobp)
  2582. (allout-end-of-entry)
  2583. (forward-char -1))
  2584. (if (and (not include-trailing-blank) (= ?\n (preceding-char)))
  2585. (forward-char -1))
  2586. (setq allout-recent-end-of-subtree (point))))
  2587. ;;;_ > allout-end-of-current-subtree (&optional include-trailing-blank)
  2588. (defun allout-end-of-current-subtree (&optional include-trailing-blank)
  2589. "Put point at end of last leaf in currently visible containing topic.
  2590. Optional INCLUDE-TRAILING-BLANK means include a trailing blank line, if
  2591. any, as part of the subtree. Otherwise, that trailing blank will be
  2592. excluded as delimiting whitespace between topics.
  2593. Returns the value of point."
  2594. (interactive)
  2595. (allout-end-of-subtree t include-trailing-blank))
  2596. ;;;_ > allout-beginning-of-current-entry (&optional interactive)
  2597. (defun allout-beginning-of-current-entry (&optional interactive)
  2598. "When not already there, position point at beginning of current topic header.
  2599. If already there, move cursor to bullet for hot-spot operation.
  2600. \(See `allout-mode' doc string for details of hot-spot operation.)"
  2601. (interactive "p")
  2602. (let ((start-point (point)))
  2603. (move-beginning-of-line 1)
  2604. (if (< 0 (allout-current-depth))
  2605. (goto-char allout-recent-prefix-end)
  2606. (goto-char (point-min)))
  2607. (allout-end-of-prefix)
  2608. (if (and interactive
  2609. (= (point) start-point))
  2610. (goto-char (allout-current-bullet-pos)))))
  2611. ;;;_ > allout-end-of-entry (&optional inclusive)
  2612. (defun allout-end-of-entry (&optional inclusive)
  2613. "Position the point at the end of the current topics' entry.
  2614. Optional INCLUSIVE means also include trailing empty line, if any. When
  2615. unset, whitespace between items separates them even when the items are
  2616. collapsed."
  2617. (interactive)
  2618. (allout-pre-next-prefix)
  2619. (if (and (not inclusive) (not (bobp)) (= ?\n (preceding-char)))
  2620. (forward-char -1))
  2621. (point))
  2622. ;;;_ > allout-end-of-current-heading ()
  2623. (defun allout-end-of-current-heading ()
  2624. (interactive)
  2625. (allout-beginning-of-current-entry)
  2626. (search-forward "\n" nil t)
  2627. (forward-char -1))
  2628. (defalias 'allout-end-of-heading 'allout-end-of-current-heading)
  2629. ;;;_ > allout-get-body-text ()
  2630. (defun allout-get-body-text ()
  2631. "Return the unmangled body text of the topic immediately containing point."
  2632. (save-excursion
  2633. (allout-end-of-prefix)
  2634. (if (not (search-forward "\n" nil t))
  2635. nil
  2636. (backward-char 1)
  2637. (let ((pre-body (point)))
  2638. (if (not pre-body)
  2639. nil
  2640. (allout-end-of-entry t)
  2641. (if (not (= pre-body (point)))
  2642. (buffer-substring-no-properties (1+ pre-body) (point))))
  2643. )
  2644. )
  2645. )
  2646. )
  2647. ;;;_ - Depth-wise
  2648. ;;;_ > allout-ascend-to-depth (depth)
  2649. (defun allout-ascend-to-depth (depth)
  2650. "Ascend to depth DEPTH, returning depth if successful, nil if not."
  2651. (if (and (> depth 0)(<= depth (allout-depth)))
  2652. (let (last-ascended)
  2653. (while (and (< depth allout-recent-depth)
  2654. (setq last-ascended (allout-ascend))))
  2655. (goto-char allout-recent-prefix-beginning)
  2656. (if (allout-called-interactively-p) (allout-end-of-prefix))
  2657. (and last-ascended allout-recent-depth))))
  2658. ;;;_ > allout-ascend (&optional dont-move-if-unsuccessful)
  2659. (defun allout-ascend (&optional dont-move-if-unsuccessful)
  2660. "Ascend one level, returning resulting depth if successful, nil if not.
  2661. Point is left at the beginning of the level whether or not
  2662. successful, unless optional DONT-MOVE-IF-UNSUCCESSFUL is set, in
  2663. which case point is returned to its original starting location."
  2664. (if dont-move-if-unsuccessful
  2665. (setq dont-move-if-unsuccessful (point)))
  2666. (prog1
  2667. (if (allout-beginning-of-level)
  2668. (let ((bolevel (point))
  2669. (bolevel-depth allout-recent-depth))
  2670. (allout-previous-heading)
  2671. (cond ((< allout-recent-depth bolevel-depth)
  2672. allout-recent-depth)
  2673. ((= allout-recent-depth bolevel-depth)
  2674. (if dont-move-if-unsuccessful
  2675. (goto-char dont-move-if-unsuccessful))
  2676. (allout-depth)
  2677. nil)
  2678. (t
  2679. ;; some topic after very first is lower depth than first:
  2680. (goto-char bolevel)
  2681. (allout-depth)
  2682. nil))))
  2683. (if (allout-called-interactively-p) (allout-end-of-prefix))))
  2684. ;;;_ > allout-descend-to-depth (depth)
  2685. (defun allout-descend-to-depth (depth)
  2686. "Descend to depth DEPTH within current topic.
  2687. Returning depth if successful, nil if not."
  2688. (let ((start-point (point))
  2689. (start-depth (allout-depth)))
  2690. (while
  2691. (and (> (allout-depth) 0)
  2692. (not (= depth allout-recent-depth)) ; ... not there yet
  2693. (allout-next-heading) ; ... go further
  2694. (< start-depth allout-recent-depth))) ; ... still in topic
  2695. (if (and (> (allout-depth) 0)
  2696. (= allout-recent-depth depth))
  2697. depth
  2698. (goto-char start-point)
  2699. nil))
  2700. )
  2701. ;;;_ > allout-up-current-level (arg)
  2702. (defun allout-up-current-level (_arg)
  2703. "Move out ARG levels from current visible topic."
  2704. (interactive "p")
  2705. (let ((start-point (point)))
  2706. (allout-back-to-current-heading)
  2707. (if (not (allout-ascend))
  2708. (progn (goto-char start-point)
  2709. (error "Can't ascend past outermost level"))
  2710. (if (allout-called-interactively-p) (allout-end-of-prefix))
  2711. allout-recent-prefix-beginning)))
  2712. ;;;_ - Linear
  2713. ;;;_ > allout-next-sibling (&optional depth backward)
  2714. (defun allout-next-sibling (&optional depth backward)
  2715. "Like `allout-forward-current-level', but respects invisible topics.
  2716. Traverse at optional DEPTH, or current depth if none specified.
  2717. Go backward if optional arg BACKWARD is non-nil.
  2718. Return the start point of the new topic if successful, nil otherwise."
  2719. (if (if backward (bobp) (eobp))
  2720. nil
  2721. (let ((target-depth (or depth (allout-depth)))
  2722. (start-point (point))
  2723. (start-prefix-beginning allout-recent-prefix-beginning)
  2724. (count 0)
  2725. leaping
  2726. last-depth)
  2727. (while (and
  2728. ;; done too few single steps to resort to the leap routine:
  2729. (not leaping)
  2730. ;; not at limit:
  2731. (not (if backward (bobp) (eobp)))
  2732. ;; still traversable:
  2733. (if backward (allout-previous-heading) (allout-next-heading))
  2734. ;; we're below the target depth
  2735. (> (setq last-depth allout-recent-depth) target-depth))
  2736. (setq count (1+ count))
  2737. (if (> count 7) ; lists are commonly 7 +- 2, right?-)
  2738. (setq leaping t)))
  2739. (cond (leaping
  2740. (or (allout-next-sibling-leap target-depth backward)
  2741. (progn
  2742. (goto-char start-point)
  2743. (if depth (allout-depth) target-depth)
  2744. nil)))
  2745. ((and (not (eobp))
  2746. (and (> (or last-depth (allout-depth)) 0)
  2747. (= allout-recent-depth target-depth))
  2748. (not (= start-prefix-beginning
  2749. allout-recent-prefix-beginning)))
  2750. allout-recent-prefix-beginning)
  2751. (t
  2752. (goto-char start-point)
  2753. (if depth (allout-depth) target-depth)
  2754. nil)))))
  2755. ;;;_ > allout-next-sibling-leap (&optional depth backward)
  2756. (defun allout-next-sibling-leap (&optional depth backward)
  2757. "Like `allout-next-sibling', but by direct search for topic at depth.
  2758. Traverse at optional DEPTH, or current depth if none specified.
  2759. Go backward if optional arg BACKWARD is non-nil.
  2760. Return the start point of the new topic if successful, nil otherwise.
  2761. Costs more than regular `allout-next-sibling' for short traversals:
  2762. - we have to check the prior (next, if traveling backwards)
  2763. item to confirm connectivity with the prior topic, and
  2764. - if confirmed, we have to reestablish the allout-recent-* settings with
  2765. some extra navigation
  2766. - if confirmation fails, we have to do more work to recover
  2767. It is an increasingly big win when there are many intervening
  2768. offspring before the next sibling, however, so
  2769. `allout-next-sibling' resorts to this if it finds itself in that
  2770. situation."
  2771. (if (if backward (bobp) (eobp))
  2772. nil
  2773. (let* ((start-point (point))
  2774. (target-depth (or depth (allout-depth)))
  2775. (search-whitespace-regexp nil)
  2776. (depth-biased (- target-depth 2))
  2777. (expression (if (<= target-depth 1)
  2778. allout-depth-one-regexp
  2779. (format allout-depth-specific-regexp
  2780. depth-biased depth-biased)))
  2781. found
  2782. done)
  2783. (while (not done)
  2784. (setq found (save-match-data
  2785. (if backward
  2786. (re-search-backward expression nil 'to-limit)
  2787. (forward-char 1)
  2788. (re-search-forward expression nil 'to-limit))))
  2789. (if (and found (allout-aberrant-container-p))
  2790. (setq found nil))
  2791. (setq done (or found (if backward (bobp) (eobp)))))
  2792. (if (not found)
  2793. (progn (goto-char start-point)
  2794. nil)
  2795. ;; rationale: if any intervening items were at a lower depth, we
  2796. ;; would now be on the first offspring at the target depth -- ie,
  2797. ;; the preceding item (per the search direction) must be at a
  2798. ;; lesser depth. that's all we need to check.
  2799. (if backward (allout-next-heading) (allout-previous-heading))
  2800. (if (< allout-recent-depth target-depth)
  2801. ;; return to start and reestablish allout-recent-*:
  2802. (progn
  2803. (goto-char start-point)
  2804. (allout-depth)
  2805. nil)
  2806. (goto-char found)
  2807. ;; locate cursor and set allout-recent-*:
  2808. (allout-goto-prefix))))))
  2809. ;;;_ > allout-previous-sibling (&optional depth backward)
  2810. (defun allout-previous-sibling (&optional depth backward)
  2811. "Like `allout-forward-current-level' backwards, respecting invisible topics.
  2812. Optional DEPTH specifies depth to traverse, default current depth.
  2813. Optional BACKWARD reverses direction.
  2814. Return depth if successful, nil otherwise."
  2815. (allout-next-sibling depth (not backward))
  2816. )
  2817. ;;;_ > allout-snug-back ()
  2818. (defun allout-snug-back ()
  2819. "Position cursor at end of previous topic.
  2820. Presumes point is at the start of a topic prefix."
  2821. (if (or (bobp) (eobp))
  2822. nil
  2823. (forward-char -1))
  2824. (if (or (bobp) (not (= ?\n (preceding-char))))
  2825. nil
  2826. (forward-char -1))
  2827. (point))
  2828. ;;;_ > allout-beginning-of-level ()
  2829. (defun allout-beginning-of-level ()
  2830. "Go back to the first sibling at this level, visible or not."
  2831. (allout-end-of-level 'backward))
  2832. ;;;_ > allout-end-of-level (&optional backward)
  2833. (defun allout-end-of-level (&optional _backward)
  2834. "Go to the last sibling at this level, visible or not."
  2835. (let ((depth (allout-depth)))
  2836. (while (allout-previous-sibling depth nil))
  2837. (prog1 allout-recent-depth
  2838. (if (allout-called-interactively-p) (allout-end-of-prefix)))))
  2839. ;;;_ > allout-next-visible-heading (arg)
  2840. (defun allout-next-visible-heading (arg)
  2841. "Move to the next ARGth visible heading line, backward if ARG is negative.
  2842. Move to buffer limit in indicated direction if headings are exhausted."
  2843. (interactive "p")
  2844. (let* ((inhibit-field-text-motion t)
  2845. (backward (if (< arg 0) (setq arg (* -1 arg))))
  2846. (step (if backward -1 1))
  2847. (progress (allout-current-bullet-pos))
  2848. prev got)
  2849. (while (> arg 0)
  2850. (while (and
  2851. ;; Boundary condition:
  2852. (not (if backward (bobp)(eobp)))
  2853. ;; Move, skipping over all concealed lines in one fell swoop:
  2854. (prog1 (condition-case nil (or (line-move step) t)
  2855. (error nil))
  2856. (allout-beginning-of-current-line)
  2857. ;; line-move can wind up on the same line if long.
  2858. ;; when moving forward, that would yield no-progress
  2859. (when (and (not backward)
  2860. (<= (point) progress))
  2861. ;; ensure progress by doing line-move from end-of-line:
  2862. (end-of-line)
  2863. (condition-case nil (or (line-move step) t)
  2864. (error nil))
  2865. (allout-beginning-of-current-line)
  2866. (setq progress (point))))
  2867. ;; Deal with apparent header line:
  2868. (save-match-data
  2869. (if (not (looking-at allout-regexp))
  2870. ;; not a header line, keep looking:
  2871. t
  2872. (allout-prefix-data)
  2873. (if (and (allout-do-doublecheck)
  2874. (allout-aberrant-container-p))
  2875. ;; skip this aberrant prospective header line:
  2876. t
  2877. ;; this prospective headerline qualifies -- register:
  2878. (setq got allout-recent-prefix-beginning)
  2879. ;; and break the loop:
  2880. nil)))))
  2881. ;; Register this got, it may be the last:
  2882. (if got (setq prev got))
  2883. (setq arg (1- arg)))
  2884. (cond (got ; Last move was to a prefix:
  2885. (allout-end-of-prefix))
  2886. (prev ; Last move wasn't, but prev was:
  2887. (goto-char prev)
  2888. (allout-end-of-prefix))
  2889. ((not backward) (end-of-line) nil))))
  2890. ;;;_ > allout-previous-visible-heading (arg)
  2891. (defun allout-previous-visible-heading (arg)
  2892. "Move to the previous heading line.
  2893. With argument, repeats or can move forward if negative.
  2894. A heading line is one that starts with a `*' (or that `allout-regexp'
  2895. matches)."
  2896. (interactive "p")
  2897. (prog1 (allout-next-visible-heading (- arg))
  2898. (if (allout-called-interactively-p) (allout-end-of-prefix))))
  2899. ;;;_ > allout-forward-current-level (arg)
  2900. (defun allout-forward-current-level (arg)
  2901. "Position point at the next heading of the same level.
  2902. Takes optional repeat-count, goes backward if count is negative.
  2903. Returns resulting position, else nil if none found."
  2904. (interactive "p")
  2905. (let ((start-depth (allout-current-depth))
  2906. (start-arg arg)
  2907. (backward (> 0 arg)))
  2908. (if (= 0 start-depth)
  2909. (error "No siblings, not in a topic..."))
  2910. (if backward (setq arg (* -1 arg)))
  2911. (allout-back-to-current-heading)
  2912. (while (and (not (zerop arg))
  2913. (if backward
  2914. (allout-previous-sibling)
  2915. (allout-next-sibling)))
  2916. (setq arg (1- arg)))
  2917. (if (not (allout-called-interactively-p))
  2918. nil
  2919. (allout-end-of-prefix)
  2920. (if (not (zerop arg))
  2921. (error "Hit %s level %d topic, traversed %d of %d requested"
  2922. (if backward "first" "last")
  2923. allout-recent-depth
  2924. (- (abs start-arg) arg)
  2925. (abs start-arg))))))
  2926. ;;;_ > allout-backward-current-level (arg)
  2927. (defun allout-backward-current-level (arg)
  2928. "Inverse of `allout-forward-current-level'."
  2929. (interactive "p")
  2930. (if (allout-called-interactively-p)
  2931. (let ((current-prefix-arg (* -1 arg)))
  2932. (call-interactively 'allout-forward-current-level))
  2933. (allout-forward-current-level (* -1 arg))))
  2934. ;;;_ #5 Alteration
  2935. ;;;_ - Fundamental
  2936. ;;;_ = allout-post-goto-bullet
  2937. (defvar allout-post-goto-bullet nil
  2938. "Outline internal var, for `allout-pre-command-business' hot-spot operation.
  2939. When set, tells post-processing to reposition on topic bullet, and
  2940. then unset it. Set by `allout-pre-command-business' when implementing
  2941. hot-spot operation, where literal characters typed over a topic bullet
  2942. are mapped to the command of the corresponding control-key on the
  2943. `allout-mode-map-value'.")
  2944. (make-variable-buffer-local 'allout-post-goto-bullet)
  2945. ;;;_ = allout-command-counter
  2946. (defvar allout-command-counter 0
  2947. "Counter that monotonically increases in allout-mode buffers.
  2948. Set by `allout-pre-command-business', to support allout addons in
  2949. coordinating with allout activity.")
  2950. (make-variable-buffer-local 'allout-command-counter)
  2951. ;;;_ = allout-this-command-hid-text
  2952. (defvar allout-this-command-hid-text nil
  2953. "True if the most recent allout-mode command hid any text.")
  2954. (make-variable-buffer-local 'allout-this-command-hid-text)
  2955. ;;;_ > allout-post-command-business ()
  2956. (defun allout-post-command-business ()
  2957. "Outline `post-command-hook' function.
  2958. - Implement (and clear) `allout-post-goto-bullet', for hot-spot
  2959. outline commands.
  2960. - Move the cursor to the beginning of the entry if it is hidden
  2961. and collapsing activity just happened.
  2962. - If the command we're following was an undo, check for change in
  2963. the status of encrypted items and adjust auto-save inhibitions
  2964. accordingly.
  2965. - Decrypt topic currently being edited if it was encrypted for a save."
  2966. (if (not (allout-mode-p)) ; In allout-mode.
  2967. nil
  2968. (when allout-just-did-undo
  2969. (setq allout-just-did-undo nil)
  2970. (run-hooks 'allout-post-undo-hook)
  2971. (cond ((and (= buffer-saved-size -1)
  2972. allout-auto-save-temporarily-disabled)
  2973. ;; user possibly undid a decryption, disinhibit auto-save:
  2974. (allout-maybe-resume-auto-save-info-after-encryption))
  2975. ((save-excursion
  2976. (save-restriction
  2977. (widen)
  2978. (goto-char (point-min))
  2979. (not (allout-next-topic-pending-encryption))))
  2980. ;; plain-text encrypted items are present, inhibit auto-save:
  2981. (allout-inhibit-auto-save-info-for-decryption (buffer-size)))))
  2982. (if (and (boundp 'allout-after-save-decrypt)
  2983. allout-after-save-decrypt)
  2984. (allout-after-saves-handler))
  2985. ;; Implement allout-post-goto-bullet, if set:
  2986. (if (and allout-post-goto-bullet
  2987. (allout-current-bullet-pos))
  2988. (progn (goto-char (allout-current-bullet-pos))
  2989. (setq allout-post-goto-bullet nil))
  2990. (when (and (allout-hidden-p) allout-this-command-hid-text)
  2991. (allout-beginning-of-current-entry)))))
  2992. ;;;_ > allout-pre-command-business ()
  2993. (defun allout-pre-command-business ()
  2994. "Outline `pre-command-hook' function for outline buffers.
  2995. Among other things, implements special behavior when the cursor is on the
  2996. topic bullet character.
  2997. When the cursor is on the bullet character, self-insert
  2998. characters are reinterpreted as the corresponding
  2999. control-character in the `allout-mode-map-value'. The
  3000. `allout-mode' `post-command-hook' insures that the cursor which
  3001. has moved as a result of such reinterpretation is positioned on
  3002. the bullet character of the destination topic.
  3003. The upshot is that you can get easy, single (ie, unmodified) key
  3004. outline maneuvering operations by positioning the cursor on the bullet
  3005. char. When in this mode you can use regular cursor-positioning
  3006. command/keystrokes to relocate the cursor off of a bullet character to
  3007. return to regular interpretation of self-insert characters."
  3008. (if (not (allout-mode-p))
  3009. nil
  3010. (setq allout-command-counter (1+ allout-command-counter))
  3011. (setq allout-this-command-hid-text nil)
  3012. ;; Do hot-spot navigation.
  3013. (if (and (eq this-command 'self-insert-command)
  3014. (eq (point)(allout-current-bullet-pos)))
  3015. (allout-hotspot-key-handler))))
  3016. ;;;_ > allout-hotspot-key-handler ()
  3017. (defun allout-hotspot-key-handler ()
  3018. "Catchall handling of key bindings in hot-spots.
  3019. Translates unmodified keystrokes to corresponding allout commands, when
  3020. they would qualify if prefixed with the `allout-command-prefix', and sets
  3021. `this-command' accordingly.
  3022. Returns the qualifying command, if any, else nil."
  3023. (interactive)
  3024. (let* ((modified (event-modifiers last-command-event))
  3025. (key-num (cond ((numberp last-command-event) last-command-event)
  3026. ;; for XEmacs character type:
  3027. ((and (fboundp 'characterp)
  3028. (apply 'characterp (list last-command-event)))
  3029. (apply 'char-to-int (list last-command-event)))
  3030. (t 0)))
  3031. mapped-binding)
  3032. (if (zerop key-num)
  3033. nil
  3034. (if (and
  3035. ;; exclude control chars and escape:
  3036. (not modified)
  3037. (<= 33 key-num)
  3038. (setq mapped-binding
  3039. (or
  3040. ;; try control-modified versions of keys:
  3041. (key-binding (vconcat allout-command-prefix
  3042. (vector
  3043. (if (and (<= 97 key-num) ; "a"
  3044. (>= 122 key-num)) ; "z"
  3045. (- key-num 96) key-num)))
  3046. t)
  3047. ;; try non-modified versions of keys:
  3048. (key-binding (vconcat allout-command-prefix
  3049. (vector key-num))
  3050. t))))
  3051. ;; Qualified as an allout command -- do hot-spot operation.
  3052. (setq allout-post-goto-bullet t)
  3053. ;; accept-defaults nil, or else we get allout-item-icon-key-handler.
  3054. (setq mapped-binding (key-binding (vector key-num))))
  3055. (while (keymapp mapped-binding)
  3056. (setq mapped-binding
  3057. (lookup-key mapped-binding (vector (read-char)))))
  3058. (when mapped-binding
  3059. (setq this-command mapped-binding)))))
  3060. ;;;_ > allout-find-file-hook ()
  3061. (defun allout-find-file-hook ()
  3062. "Activate `allout-mode' on non-nil `allout-auto-activation', `allout-layout'.
  3063. See `allout-auto-activation' for setup instructions."
  3064. (if (and allout-auto-activation
  3065. (not (allout-mode-p))
  3066. allout-layout)
  3067. (allout-mode)))
  3068. ;;;_ - Topic Format Assessment
  3069. ;;;_ > allout-solicit-alternate-bullet (depth &optional current-bullet)
  3070. (defun allout-solicit-alternate-bullet (depth &optional current-bullet)
  3071. "Prompt for and return a bullet char as an alternative to the current one.
  3072. Offer one suitable for current depth DEPTH as default."
  3073. (let* ((default-bullet (or (and (stringp current-bullet) current-bullet)
  3074. (allout-bullet-for-depth depth)))
  3075. (sans-escapes (regexp-sans-escapes allout-bullets-string))
  3076. choice)
  3077. (save-excursion
  3078. (goto-char (allout-current-bullet-pos))
  3079. (setq choice (solicit-char-in-string
  3080. (format-message
  3081. "Select bullet: %s (`%s' default): "
  3082. sans-escapes
  3083. (allout-substring-no-properties default-bullet))
  3084. sans-escapes
  3085. t)))
  3086. (message "")
  3087. (if (string= choice "") default-bullet choice))
  3088. )
  3089. ;;;_ > allout-distinctive-bullet (bullet)
  3090. (defun allout-distinctive-bullet (bullet)
  3091. "True if BULLET is one of those on `allout-distinctive-bullets-string'."
  3092. (string-match (regexp-quote bullet) allout-distinctive-bullets-string))
  3093. ;;;_ > allout-numbered-type-prefix (&optional prefix)
  3094. (defun allout-numbered-type-prefix (&optional prefix)
  3095. "True if current header prefix bullet is numbered bullet."
  3096. (and allout-numbered-bullet
  3097. (string= allout-numbered-bullet
  3098. (if prefix
  3099. (allout-get-prefix-bullet prefix)
  3100. (allout-get-bullet)))))
  3101. ;;;_ > allout-encrypted-type-prefix (&optional prefix)
  3102. (defun allout-encrypted-type-prefix (&optional prefix)
  3103. "True if current header prefix bullet is for an encrypted entry (body)."
  3104. (and allout-topic-encryption-bullet
  3105. (string= allout-topic-encryption-bullet
  3106. (if prefix
  3107. (allout-get-prefix-bullet prefix)
  3108. (allout-get-bullet)))))
  3109. ;;;_ > allout-bullet-for-depth (&optional depth)
  3110. (defun allout-bullet-for-depth (&optional depth)
  3111. "Return outline topic bullet suited to optional DEPTH, or current depth."
  3112. ;; Find bullet in plain-bullets-string modulo DEPTH.
  3113. (if allout-stylish-prefixes
  3114. (char-to-string (aref allout-plain-bullets-string
  3115. (% (max 0 (- depth 2))
  3116. allout-plain-bullets-string-len)))
  3117. allout-primary-bullet)
  3118. )
  3119. ;;;_ - Topic Production
  3120. ;;;_ > allout-make-topic-prefix (&optional prior-bullet
  3121. (defun allout-make-topic-prefix (&optional prior-bullet
  3122. new
  3123. depth
  3124. instead
  3125. number-control
  3126. index)
  3127. ;; Depth null means use current depth, non-null means we're either
  3128. ;; opening a new topic after current topic, lower or higher, or we're
  3129. ;; changing level of current topic.
  3130. ;; Instead dominates specified bullet-char.
  3131. ;;;_ . Doc string:
  3132. "Generate a topic prefix suitable for optional arg DEPTH, or current depth.
  3133. All the arguments are optional.
  3134. PRIOR-BULLET indicates the bullet of the prefix being changed, or
  3135. nil if none. This bullet may be preserved (other options
  3136. notwithstanding) if it is on the `allout-distinctive-bullets-string',
  3137. for instance.
  3138. Second arg NEW indicates that a new topic is being opened after the
  3139. topic at point, if non-nil. Default bullet for new topics, eg, may
  3140. be set (contingent to other args) to numbered bullets if previous
  3141. sibling is one. The implication otherwise is that the current topic
  3142. is being adjusted -- shifted or rebulleted -- and we don't consider
  3143. bullet or previous sibling.
  3144. Third arg DEPTH forces the topic prefix to that depth, regardless of
  3145. the current topics' depth.
  3146. If INSTEAD is:
  3147. - nil, then the bullet char for the context is used, per distinction or depth
  3148. - a (numeric) character, then character's string representation is used
  3149. - a string, then the user is asked for bullet with the first char as default
  3150. - anything else, the user is solicited with bullet char per context as default
  3151. \(INSTEAD overrides other options, including, eg, a distinctive
  3152. PRIOR-BULLET.)
  3153. Fifth arg, NUMBER-CONTROL, matters only if `allout-numbered-bullet'
  3154. is non-nil *and* no specific INSTEAD was specified. Then
  3155. NUMBER-CONTROL non-nil forces prefix to either numbered or
  3156. unnumbered format, depending on the value of the sixth arg, INDEX.
  3157. \(Note that NUMBER-CONTROL does *not* apply to level 1 topics. Sorry...)
  3158. If NUMBER-CONTROL is non-nil and sixth arg INDEX is non-nil then
  3159. the prefix of the topic is forced to be numbered. Non-nil
  3160. NUMBER-CONTROL and nil INDEX forces non-numbered format on the
  3161. bullet. Non-nil NUMBER-CONTROL and non-nil, non-number INDEX means
  3162. that the index for the numbered prefix will be derived, by counting
  3163. siblings back to start of level. If INDEX is a number, then that
  3164. number is used as the index for the numbered prefix (allowing, eg,
  3165. sequential renumbering to not require this function counting back the
  3166. index for each successive sibling)."
  3167. ;;;_ . Code:
  3168. ;; The options are ordered in likely frequency of use, most common
  3169. ;; highest, least lowest. Ie, more likely to be doing prefix
  3170. ;; adjustments than soliciting, and yet more than numbering.
  3171. ;; Current prefix is least dominant, but most likely to be commonly
  3172. ;; specified...
  3173. (let* (body
  3174. numbering
  3175. denumbering
  3176. (depth (or depth (allout-depth)))
  3177. (header-lead allout-header-prefix)
  3178. (bullet-char
  3179. ;; Getting value for bullet char is practically the whole job:
  3180. (cond
  3181. ; Simplest situation -- level 1:
  3182. ((<= depth 1) (setq header-lead "") allout-primary-bullet)
  3183. ; Simple, too: all asterisks:
  3184. (allout-old-style-prefixes
  3185. ;; Cheat -- make body the whole thing, null out header-lead and
  3186. ;; bullet-char:
  3187. (setq body (make-string depth
  3188. (string-to-char allout-primary-bullet)))
  3189. (setq header-lead "")
  3190. "")
  3191. ;; (Neither level 1 nor old-style, so we're space padding.
  3192. ;; Sneak it in the condition of the next case, whatever it is.)
  3193. ;; Solicitation overrides numbering and other cases:
  3194. ((progn (setq body (make-string (- depth 2) ?\ ))
  3195. ;; The actual condition:
  3196. instead)
  3197. (let ((got (cond ((stringp instead)
  3198. (if (> (length instead) 0)
  3199. (allout-solicit-alternate-bullet
  3200. depth (substring instead 0 1))))
  3201. ((characterp instead) (char-to-string instead))
  3202. (t (allout-solicit-alternate-bullet depth)))))
  3203. ;; Gotta check whether we're numbering and got a numbered bullet:
  3204. (setq numbering (and allout-numbered-bullet
  3205. (not (and number-control (not index)))
  3206. (string= got allout-numbered-bullet)))
  3207. ;; Now return what we got, regardless:
  3208. got))
  3209. ;; Numbering invoked through args:
  3210. ((and allout-numbered-bullet number-control)
  3211. (if (setq numbering (not (setq denumbering (not index))))
  3212. allout-numbered-bullet
  3213. (if (and prior-bullet
  3214. (not (string= allout-numbered-bullet
  3215. prior-bullet)))
  3216. prior-bullet
  3217. (allout-bullet-for-depth depth))))
  3218. ;;; Neither soliciting nor controlled numbering ;;;
  3219. ;;; (may be controlled denumbering, tho) ;;;
  3220. ;; Check wrt previous sibling:
  3221. ((and new ; only check for new prefixes
  3222. (<= depth (allout-depth))
  3223. allout-numbered-bullet ; ... & numbering enabled
  3224. (not denumbering)
  3225. (let ((sibling-bullet
  3226. (save-excursion
  3227. ;; Locate correct sibling:
  3228. (or (>= depth (allout-depth))
  3229. (allout-ascend-to-depth depth))
  3230. (allout-get-bullet))))
  3231. (if (and sibling-bullet
  3232. (string= allout-numbered-bullet sibling-bullet))
  3233. (setq numbering sibling-bullet)))))
  3234. ;; Distinctive prior bullet?
  3235. ((and prior-bullet
  3236. (allout-distinctive-bullet prior-bullet)
  3237. ;; Either non-numbered:
  3238. (or (not (and allout-numbered-bullet
  3239. (string= prior-bullet allout-numbered-bullet)))
  3240. ;; or numbered, and not denumbering:
  3241. (setq numbering (not denumbering)))
  3242. ;; Here 'tis:
  3243. prior-bullet))
  3244. ;; Else, standard bullet per depth:
  3245. ((allout-bullet-for-depth depth)))))
  3246. (concat header-lead
  3247. body
  3248. bullet-char
  3249. (if numbering
  3250. (format "%d" (cond ((and index (numberp index)) index)
  3251. (new (1+ (allout-sibling-index depth)))
  3252. ((allout-sibling-index))))))
  3253. )
  3254. )
  3255. ;;;_ > allout-open-topic (relative-depth &optional before offer-recent-bullet)
  3256. (defun allout-open-topic (relative-depth &optional before offer-recent-bullet)
  3257. "Open a new topic at depth DEPTH.
  3258. New topic is situated after current one, unless optional flag BEFORE
  3259. is non-nil, or unless current line is completely empty -- lacking even
  3260. whitespace -- in which case open is done on the current line.
  3261. When adding an offspring, it will be added immediately after the parent if
  3262. the other offspring are exposed, or after the last child if the offspring
  3263. are hidden. (The intervening offspring will be exposed in the latter
  3264. case.)
  3265. If OFFER-RECENT-BULLET is true, offer to use the bullet of the prior sibling.
  3266. Nuances:
  3267. - Creation of new topics is with respect to the visible topic
  3268. containing the cursor, regardless of intervening concealed ones.
  3269. - New headers are generally created after/before the body of a
  3270. topic. However, they are created right at cursor location if the
  3271. cursor is on a blank line, even if that breaks the current topic
  3272. body. This is intentional, to provide a simple means for
  3273. deliberately dividing topic bodies.
  3274. - Double spacing of topic lists is preserved. Also, the first
  3275. level two topic is created double-spaced (and so would be
  3276. subsequent siblings, if that's left intact). Otherwise,
  3277. single-spacing is used.
  3278. - Creation of sibling or nested topics is with respect to the topic
  3279. you're starting from, even when creating backwards. This way you
  3280. can easily create a sibling in front of the current topic without
  3281. having to go to its preceding sibling, and then open forward
  3282. from there."
  3283. (allout-beginning-of-current-line)
  3284. (save-match-data
  3285. (let* ((inhibit-field-text-motion t)
  3286. (depth (+ (allout-current-depth) relative-depth))
  3287. (opening-on-blank (if (looking-at "^$")
  3288. (not (setq before nil))))
  3289. ;; bunch o vars set while computing ref-topic
  3290. opening-numbered
  3291. ref-depth
  3292. ref-bullet
  3293. (ref-topic (save-excursion
  3294. (cond ((< relative-depth 0)
  3295. (allout-ascend-to-depth depth))
  3296. ((>= relative-depth 1) nil)
  3297. (t (allout-back-to-current-heading)))
  3298. (setq ref-depth allout-recent-depth)
  3299. (setq ref-bullet
  3300. (if (> allout-recent-prefix-end 1)
  3301. (allout-recent-bullet)
  3302. ""))
  3303. (setq opening-numbered
  3304. (save-excursion
  3305. (and allout-numbered-bullet
  3306. (or (<= relative-depth 0)
  3307. (allout-descend-to-depth depth))
  3308. (if (allout-numbered-type-prefix)
  3309. allout-numbered-bullet))))
  3310. (point)))
  3311. dbl-space
  3312. doing-beginning
  3313. start end)
  3314. (if (not opening-on-blank)
  3315. ; Positioning and vertical
  3316. ; padding -- only if not
  3317. ; opening-on-blank:
  3318. (progn
  3319. (goto-char ref-topic)
  3320. (setq dbl-space ; Determine double space action:
  3321. (or (and (<= relative-depth 0) ; not descending;
  3322. (save-excursion
  3323. ;; at b-o-b or preceded by a blank line?
  3324. (or (> 0 (forward-line -1))
  3325. (looking-at "^\\s-*$")
  3326. (bobp)))
  3327. (save-excursion
  3328. ;; succeeded by a blank line?
  3329. (allout-end-of-current-subtree)
  3330. (looking-at "\n\n")))
  3331. (and (= ref-depth 1)
  3332. (or before
  3333. (= depth 1)
  3334. (save-excursion
  3335. ;; Don't already have following
  3336. ;; vertical padding:
  3337. (not (allout-pre-next-prefix)))))))
  3338. ;; Position to prior heading, if inserting backwards, and not
  3339. ;; going outwards:
  3340. (if (and before (>= relative-depth 0))
  3341. (progn (allout-back-to-current-heading)
  3342. (setq doing-beginning (bobp))
  3343. (if (not (bobp))
  3344. (allout-previous-heading)))
  3345. (if (and before (bobp))
  3346. (open-line 1)))
  3347. (if (<= relative-depth 0)
  3348. ;; Not going inwards, don't snug up:
  3349. (if doing-beginning
  3350. (if (not dbl-space)
  3351. (open-line 1)
  3352. (open-line 2))
  3353. (if before
  3354. (progn (end-of-line)
  3355. (allout-pre-next-prefix)
  3356. (while (and (= ?\n (following-char))
  3357. (save-excursion
  3358. (forward-char 1)
  3359. (allout-hidden-p)))
  3360. (forward-char 1))
  3361. (if (not (looking-at "^$"))
  3362. (open-line 1)))
  3363. (allout-end-of-current-subtree)
  3364. (if (looking-at "\n\n") (forward-char 1))))
  3365. ;; Going inwards -- double-space if first offspring is
  3366. ;; double-spaced, otherwise snug up.
  3367. (allout-end-of-entry)
  3368. (if (eobp)
  3369. (newline 1)
  3370. (line-move 1))
  3371. (allout-beginning-of-current-line)
  3372. (backward-char 1)
  3373. (if (bolp)
  3374. ;; Blank lines between current header body and next
  3375. ;; header -- get to last substantive (non-white-space)
  3376. ;; line in body:
  3377. (progn (setq dbl-space t)
  3378. (re-search-backward "[^ \t\n]" nil t)))
  3379. (if (looking-at "\n\n")
  3380. (setq dbl-space t))
  3381. (if (save-excursion
  3382. (allout-next-heading)
  3383. (when (> allout-recent-depth ref-depth)
  3384. ;; This is an offspring.
  3385. (forward-line -1)
  3386. (looking-at "^\\s-*$")))
  3387. (progn (forward-line 1)
  3388. (open-line 1)
  3389. (forward-line 1)))
  3390. (allout-end-of-current-line))
  3391. ;;(if doing-beginning (goto-char doing-beginning))
  3392. (if (not (bobp))
  3393. ;; We insert a newline char rather than using open-line to
  3394. ;; avoid rear-stickiness inheritance of read-only property.
  3395. (progn (if (and (not (> depth ref-depth))
  3396. (not before))
  3397. (open-line 1)
  3398. (if (and (not dbl-space) (> depth ref-depth))
  3399. (newline 1)
  3400. (if dbl-space
  3401. (open-line 1)
  3402. (if (not before)
  3403. (newline 1)))))
  3404. (if (and dbl-space (not (> relative-depth 0)))
  3405. (newline 1))
  3406. (if (and (not (eobp))
  3407. (or (not (bolp))
  3408. (and (not (bobp))
  3409. ;; bolp doesn't detect concealed
  3410. ;; trailing newlines, compensate:
  3411. (save-excursion
  3412. (forward-char -1)
  3413. (allout-hidden-p)))))
  3414. (forward-char 1))))
  3415. ))
  3416. (setq start (point))
  3417. (insert (concat (allout-make-topic-prefix opening-numbered t depth)
  3418. " "))
  3419. (setq end (1+ (point)))
  3420. (allout-rebullet-heading (and offer-recent-bullet ref-bullet)
  3421. depth nil nil t)
  3422. (if (> relative-depth 0)
  3423. (save-excursion (goto-char ref-topic)
  3424. (allout-show-children)))
  3425. (end-of-line)
  3426. (run-hook-with-args 'allout-structure-added-functions start end)
  3427. )
  3428. )
  3429. )
  3430. ;;;_ > allout-open-subtopic (arg)
  3431. (defun allout-open-subtopic (arg)
  3432. "Open new topic header at deeper level than the current one.
  3433. Negative universal ARG means to open deeper, but place the new topic
  3434. prior to the current one."
  3435. (interactive "p")
  3436. (allout-open-topic 1 (> 0 arg) (< 1 arg)))
  3437. ;;;_ > allout-open-sibtopic (arg)
  3438. (defun allout-open-sibtopic (arg)
  3439. "Open new topic header at same level as the current one.
  3440. Positive universal ARG means to use the bullet of the prior sibling.
  3441. Negative universal ARG means to place the new topic prior to the current
  3442. one."
  3443. (interactive "p")
  3444. (allout-open-topic 0 (> 0 arg) (not (= 1 arg))))
  3445. ;;;_ > allout-open-supertopic (arg)
  3446. (defun allout-open-supertopic (arg)
  3447. "Open new topic header at shallower level than the current one.
  3448. Negative universal ARG means to open shallower, but place the new
  3449. topic prior to the current one."
  3450. (interactive "p")
  3451. (allout-open-topic -1 (> 0 arg) (< 1 arg)))
  3452. ;;;_ - Outline Alteration
  3453. ;;;_ : Topic Modification
  3454. ;;;_ = allout-former-auto-filler
  3455. (defvar allout-former-auto-filler nil
  3456. "Name of modal fill function being wrapped by `allout-auto-fill'.")
  3457. ;;;_ > allout-auto-fill ()
  3458. (defun allout-auto-fill ()
  3459. "`allout-mode' autofill function.
  3460. Maintains outline hanging topic indentation if
  3461. `allout-use-hanging-indents' is set."
  3462. (when (and (not allout-inhibit-auto-fill)
  3463. (or (not allout-inhibit-auto-fill-on-headline)
  3464. (not (allout-on-current-heading-p))))
  3465. (let ((fill-prefix (if allout-use-hanging-indents
  3466. ;; Check for topic header indentation:
  3467. (save-match-data
  3468. (save-excursion
  3469. (beginning-of-line)
  3470. (if (looking-at allout-regexp)
  3471. ;; ... construct indentation to account for
  3472. ;; length of topic prefix:
  3473. (make-string (progn (allout-end-of-prefix)
  3474. (current-column))
  3475. ?\ ))))))
  3476. (use-auto-fill-function
  3477. (if (and (eq allout-outside-normal-auto-fill-function
  3478. 'allout-auto-fill)
  3479. (eq auto-fill-function 'allout-auto-fill))
  3480. 'do-auto-fill
  3481. (or allout-outside-normal-auto-fill-function
  3482. auto-fill-function))))
  3483. (if (or allout-former-auto-filler allout-use-hanging-indents)
  3484. (funcall use-auto-fill-function)))))
  3485. ;;;_ > allout-reindent-body (old-depth new-depth &optional number)
  3486. (defun allout-reindent-body (old-depth new-depth &optional _number)
  3487. "Reindent body lines which were indented at OLD-DEPTH to NEW-DEPTH.
  3488. Optional arg NUMBER indicates numbering is being added, and it must
  3489. be accommodated.
  3490. Note that refill of indented paragraphs is not done."
  3491. (save-excursion
  3492. (allout-end-of-prefix)
  3493. (let* ((new-margin (current-column))
  3494. excess old-indent-begin old-indent-end
  3495. ;; We want the column where the header-prefix text started
  3496. ;; *before* the prefix was changed, so we infer it relative
  3497. ;; to the new margin and the shift in depth:
  3498. (old-margin (+ old-depth (- new-margin new-depth))))
  3499. ;; Process lines up to (but excluding) next topic header:
  3500. (allout-unprotected
  3501. (save-match-data
  3502. (while
  3503. (and (re-search-forward "\n\\(\\s-*\\)"
  3504. nil
  3505. t)
  3506. ;; Register the indent data, before we reset the
  3507. ;; match data with a subsequent `looking-at':
  3508. (setq old-indent-begin (match-beginning 1)
  3509. old-indent-end (match-end 1))
  3510. (not (looking-at allout-regexp)))
  3511. (if (> 0 (setq excess (- (- old-indent-end old-indent-begin)
  3512. old-margin)))
  3513. ;; Text starts left of old margin -- don't adjust:
  3514. nil
  3515. ;; Text was hanging at or right of old left margin --
  3516. ;; reindent it, preserving its existing indentation
  3517. ;; beyond the old margin:
  3518. (delete-region old-indent-begin old-indent-end)
  3519. (indent-to (+ new-margin excess (current-column))))))))))
  3520. ;;;_ > allout-rebullet-current-heading (arg)
  3521. (defun allout-rebullet-current-heading (arg)
  3522. "Solicit new bullet for current visible heading."
  3523. (interactive "p")
  3524. (let ((initial-col (current-column))
  3525. (on-bullet (eq (point)(allout-current-bullet-pos)))
  3526. from to
  3527. (backwards (if (< arg 0)
  3528. (setq arg (* arg -1)))))
  3529. (while (> arg 0)
  3530. (save-excursion (allout-back-to-current-heading)
  3531. (allout-end-of-prefix)
  3532. (setq from allout-recent-prefix-beginning
  3533. to allout-recent-prefix-end)
  3534. (allout-rebullet-heading t ;;; instead
  3535. nil ;;; depth
  3536. nil ;;; number-control
  3537. nil ;;; index
  3538. t) ;;; do-successors
  3539. (run-hook-with-args 'allout-exposure-change-functions
  3540. from to t))
  3541. (setq arg (1- arg))
  3542. (if (<= arg 0)
  3543. nil
  3544. (setq initial-col nil) ; Override positioning back to init col
  3545. (if (not backwards)
  3546. (allout-next-visible-heading 1)
  3547. (allout-goto-prefix-doublechecked)
  3548. (allout-next-visible-heading -1))))
  3549. (message "Done.")
  3550. (cond (on-bullet (goto-char (allout-current-bullet-pos)))
  3551. (initial-col (move-to-column initial-col)))))
  3552. ;;;_ > allout-rebullet-heading (&optional instead ...)
  3553. (defun allout-rebullet-heading (&optional instead
  3554. new-depth
  3555. number-control
  3556. index
  3557. do-successors)
  3558. "Adjust bullet of current topic prefix.
  3559. All args are optional.
  3560. If INSTEAD is:
  3561. - nil, then the bullet char for the context is used, per distinction or depth
  3562. - a (numeric) character, then character's string representation is used
  3563. - a string, then the user is asked for bullet with the first char as default
  3564. - anything else, the user is solicited with bullet char per context as default
  3565. Second arg DEPTH forces the topic prefix to that depth, regardless
  3566. of the topic's current depth.
  3567. Third arg NUMBER-CONTROL can force the prefix to or away from
  3568. numbered form. It has effect only if `allout-numbered-bullet' is
  3569. non-nil and soliciting was not explicitly invoked (via first arg).
  3570. Its effect, numbering or denumbering, then depends on the setting
  3571. of the fourth arg, INDEX.
  3572. If NUMBER-CONTROL is non-nil and fourth arg INDEX is nil, then the
  3573. prefix of the topic is forced to be non-numbered. Null index and
  3574. non-nil NUMBER-CONTROL forces denumbering. Non-nil INDEX (and
  3575. non-nil NUMBER-CONTROL) forces a numbered-prefix form. If non-nil
  3576. INDEX is a number, then that number is used for the numbered
  3577. prefix. Non-nil and non-number means that the index for the
  3578. numbered prefix will be derived by allout-make-topic-prefix.
  3579. Fifth arg DO-SUCCESSORS t means re-resolve count on succeeding
  3580. siblings.
  3581. Cf vars `allout-stylish-prefixes', `allout-old-style-prefixes',
  3582. and `allout-numbered-bullet', which all affect the behavior of
  3583. this function."
  3584. (let* ((current-depth (allout-depth))
  3585. (new-depth (or new-depth current-depth))
  3586. (mb allout-recent-prefix-beginning)
  3587. (me allout-recent-prefix-end)
  3588. (current-bullet (buffer-substring-no-properties (- me 1) me))
  3589. (has-annotation (get-text-property mb 'allout-was-hidden))
  3590. (new-prefix (allout-make-topic-prefix current-bullet
  3591. nil
  3592. new-depth
  3593. instead
  3594. number-control
  3595. index)))
  3596. ;; Is new one identical to old?
  3597. (if (and (= current-depth new-depth)
  3598. (string= current-bullet
  3599. (substring new-prefix (1- (length new-prefix)))))
  3600. ;; Nothing to do:
  3601. t
  3602. ;; New prefix probably different from old:
  3603. ; get rid of old one:
  3604. (allout-unprotected (delete-region mb me))
  3605. (goto-char mb)
  3606. ; Dispense with number if
  3607. ; numbered-bullet prefix:
  3608. (save-match-data
  3609. (if (and allout-numbered-bullet
  3610. (string= allout-numbered-bullet current-bullet)
  3611. (looking-at "[0-9]+"))
  3612. (allout-unprotected
  3613. (delete-region (match-beginning 0)(match-end 0)))))
  3614. ;; convey 'allout-was-hidden annotation, if original had it:
  3615. (if has-annotation
  3616. (put-text-property 0 (length new-prefix) 'allout-was-hidden t
  3617. new-prefix))
  3618. ; Put in new prefix:
  3619. (allout-unprotected (insert new-prefix))
  3620. ;; Reindent the body if elected, margin changed, and not encrypted body:
  3621. (if (and allout-reindent-bodies
  3622. (not (= new-depth current-depth))
  3623. (not (allout-encrypted-topic-p)))
  3624. (allout-reindent-body current-depth new-depth))
  3625. (run-hook-with-args 'allout-exposure-change-functions mb me nil)
  3626. ;; Recursively rectify successive siblings of orig topic if
  3627. ;; caller elected for it:
  3628. (if do-successors
  3629. (save-excursion
  3630. (while (allout-next-sibling new-depth nil)
  3631. (setq index
  3632. (cond ((numberp index) (1+ index))
  3633. ((not number-control) (allout-sibling-index))))
  3634. (if (allout-numbered-type-prefix)
  3635. (allout-rebullet-heading nil ;;; instead
  3636. new-depth ;;; new-depth
  3637. number-control;;; number-control
  3638. index ;;; index
  3639. nil))))) ;;;(dont!)do-successors
  3640. ) ; (if (and (= current-depth new-depth)...))
  3641. ) ; let* ((current-depth (allout-depth))...)
  3642. ) ; defun
  3643. ;;;_ > allout-rebullet-topic (arg)
  3644. (defun allout-rebullet-topic (arg &optional sans-offspring)
  3645. "Rebullet the visible topic containing point and all contained subtopics.
  3646. Descends into invisible as well as visible topics, however.
  3647. When optional SANS-OFFSPRING is non-nil, subtopics are not
  3648. shifted. (Shifting a topic outwards without shifting its
  3649. offspring is disallowed, since this would create a \"containment
  3650. discontinuity\", where the depth difference between a topic and
  3651. its immediate offspring is greater than one.)
  3652. With repeat count, shift topic depth by that amount."
  3653. (interactive "P")
  3654. (let ((start-col (current-column)))
  3655. (save-excursion
  3656. ;; Normalize arg:
  3657. (cond ((null arg) (setq arg 0))
  3658. ((listp arg) (setq arg (car arg))))
  3659. ;; Fill the user in, in case we're shifting a big topic:
  3660. (if (not (zerop arg)) (message "Shifting..."))
  3661. (allout-back-to-current-heading)
  3662. (if (<= (+ allout-recent-depth arg) 0)
  3663. (error "Attempt to shift topic below level 1"))
  3664. (allout-rebullet-topic-grunt arg nil nil nil nil sans-offspring)
  3665. (if (not (zerop arg)) (message "Shifting... done.")))
  3666. (move-to-column (max 0 (+ start-col arg)))))
  3667. ;;;_ > allout-rebullet-topic-grunt (&optional relative-depth ...)
  3668. (defun allout-rebullet-topic-grunt (&optional relative-depth
  3669. starting-depth
  3670. starting-point
  3671. index
  3672. do-successors
  3673. sans-offspring)
  3674. "Like `allout-rebullet-topic', but on nearest containing topic
  3675. \(visible or not).
  3676. See `allout-rebullet-heading' for rebulleting behavior.
  3677. All arguments are optional.
  3678. First arg RELATIVE-DEPTH means to shift the depth of the entire
  3679. topic that amount.
  3680. Several subsequent args are for internal recursive use by the function
  3681. itself: STARTING-DEPTH, STARTING-POINT, and INDEX.
  3682. Finally, if optional SANS-OFFSPRING is non-nil then the offspring
  3683. are not shifted. (Shifting a topic outwards without shifting
  3684. its offspring is disallowed, since this would create a
  3685. \"containment discontinuity\", where the depth difference between
  3686. a topic and its immediate offspring is greater than one.)"
  3687. ;; XXX the recursion here is peculiar, and in general the routine may
  3688. ;; need simplification with refactoring.
  3689. (if (and sans-offspring
  3690. relative-depth
  3691. (< relative-depth 0))
  3692. (error (concat "Attempt to shift topic outwards without offspring,"
  3693. " would cause containment discontinuity.")))
  3694. (let* ((relative-depth (or relative-depth 0))
  3695. (new-depth (allout-depth))
  3696. (starting-depth (or starting-depth new-depth))
  3697. (on-starting-call (null starting-point))
  3698. (index (or index
  3699. ;; Leave index null on starting call, so rebullet-heading
  3700. ;; calculates it at what might be new depth:
  3701. (and (or (zerop relative-depth)
  3702. (not on-starting-call))
  3703. (allout-sibling-index))))
  3704. (starting-index index)
  3705. (moving-outwards (< 0 relative-depth))
  3706. (starting-point (or starting-point (point)))
  3707. (local-point (point)))
  3708. ;; Sanity check for excessive promotion done only on starting call:
  3709. (and on-starting-call
  3710. moving-outwards
  3711. (> 0 (+ starting-depth relative-depth))
  3712. (error "Attempt to shift topic out beyond level 1"))
  3713. (cond ((= starting-depth new-depth)
  3714. ;; We're at depth to work on this one.
  3715. ;; When shifting out we work on the children before working on
  3716. ;; the parent to avoid interim `allout-aberrant-container-p'
  3717. ;; aberrancy, and vice-versa when shifting in:
  3718. (if (>= relative-depth 0)
  3719. (allout-rebullet-heading nil
  3720. (+ starting-depth relative-depth)
  3721. nil ;;; number
  3722. index
  3723. nil)) ;;; do-successors
  3724. (when (not sans-offspring)
  3725. ;; ... and work on subsequent ones which are at greater depth:
  3726. (setq index 0)
  3727. (allout-next-heading)
  3728. (while (and (not (eobp))
  3729. (< starting-depth (allout-depth)))
  3730. (setq index (1+ index))
  3731. (allout-rebullet-topic-grunt relative-depth
  3732. (1+ starting-depth)
  3733. starting-point
  3734. index)))
  3735. (when (< relative-depth 0)
  3736. (save-excursion
  3737. (goto-char local-point)
  3738. (allout-rebullet-heading nil ;;; instead
  3739. (+ starting-depth relative-depth)
  3740. nil ;;; number
  3741. starting-index
  3742. nil)))) ;;; do-successors
  3743. ((< starting-depth new-depth)
  3744. ;; Rare case -- subtopic more than one level deeper than parent.
  3745. ;; Treat this one at an even deeper level:
  3746. (allout-rebullet-topic-grunt relative-depth
  3747. new-depth
  3748. starting-point
  3749. index
  3750. sans-offspring)))
  3751. (if on-starting-call
  3752. (progn
  3753. ;; Rectify numbering of former siblings of the adjusted topic,
  3754. ;; if topic has changed depth
  3755. (if (or do-successors
  3756. (and (not (zerop relative-depth))
  3757. (or (= allout-recent-depth starting-depth)
  3758. (= allout-recent-depth (+ starting-depth
  3759. relative-depth)))))
  3760. (allout-rebullet-heading nil nil nil nil t))
  3761. ;; Now rectify numbering of new siblings of the adjusted topic,
  3762. ;; if depth has been changed:
  3763. (progn (goto-char starting-point)
  3764. (if (not (zerop relative-depth))
  3765. (allout-rebullet-heading nil nil nil nil t)))))
  3766. )
  3767. )
  3768. ;;;_ > allout-renumber-to-depth (&optional depth)
  3769. (defun allout-renumber-to-depth (&optional depth)
  3770. "Renumber siblings at current depth.
  3771. Affects superior topics if optional arg DEPTH is less than current depth.
  3772. Returns final depth."
  3773. ;; Proceed by level, processing subsequent siblings on each,
  3774. ;; ascending until we get shallower than the start depth:
  3775. (let ((ascender (allout-depth))
  3776. was-eobp)
  3777. (while (and (not (eobp))
  3778. (allout-depth)
  3779. (>= allout-recent-depth depth)
  3780. (>= ascender depth))
  3781. ; Skip over all topics at
  3782. ; lesser depths, which can not
  3783. ; have been disturbed:
  3784. (while (and (not (setq was-eobp (eobp)))
  3785. (> allout-recent-depth ascender))
  3786. (allout-next-heading))
  3787. ; Prime ascender for ascension:
  3788. (setq ascender (1- allout-recent-depth))
  3789. (if (>= allout-recent-depth depth)
  3790. (allout-rebullet-heading nil ;;; instead
  3791. nil ;;; depth
  3792. nil ;;; number-control
  3793. nil ;;; index
  3794. t)) ;;; do-successors
  3795. (if was-eobp (goto-char (point-max)))))
  3796. allout-recent-depth)
  3797. ;;;_ > allout-number-siblings (&optional denumber)
  3798. (defun allout-number-siblings (&optional denumber)
  3799. "Assign numbered topic prefix to this topic and its siblings.
  3800. With universal argument, denumber -- assign default bullet to this
  3801. topic and its siblings.
  3802. With repeated universal argument (`^U^U'), solicit bullet for each
  3803. rebulleting each topic at this level."
  3804. (interactive "P")
  3805. (save-excursion
  3806. (allout-back-to-current-heading)
  3807. (allout-beginning-of-level)
  3808. (let ((depth allout-recent-depth)
  3809. (index (if (not denumber) 1))
  3810. (use-bullet (equal '(16) denumber))
  3811. (more t))
  3812. (while more
  3813. (allout-rebullet-heading use-bullet ;;; instead
  3814. depth ;;; depth
  3815. t ;;; number-control
  3816. index ;;; index
  3817. nil) ;;; do-successors
  3818. (if index (setq index (1+ index)))
  3819. (setq more (allout-next-sibling depth nil))))))
  3820. ;;;_ > allout-shift-in (arg)
  3821. (defun allout-shift-in (arg)
  3822. "Increase depth of current heading and any items collapsed within it.
  3823. With a negative argument, the item is shifted out using
  3824. `allout-shift-out', instead.
  3825. With an argument greater than one, shift-in the item but not its
  3826. offspring, making the item into a sibling of its former children,
  3827. and a child of sibling that formerly preceded it.
  3828. You are not allowed to shift the first offspring of a topic
  3829. inwards, because that would yield a \"containment
  3830. discontinuity\", where the depth difference between a topic and
  3831. its immediate offspring is greater than one. The first topic in
  3832. the file can be adjusted to any positive depth, however."
  3833. (interactive "p")
  3834. (if (< arg 0)
  3835. (allout-shift-out (* arg -1))
  3836. ;; refuse to create a containment discontinuity:
  3837. (save-excursion
  3838. (allout-back-to-current-heading)
  3839. (if (not (bobp))
  3840. (let* ((current-depth allout-recent-depth)
  3841. (start-point (point))
  3842. (predecessor-depth (progn
  3843. (forward-char -1)
  3844. (allout-goto-prefix-doublechecked)
  3845. (if (< (point) start-point)
  3846. allout-recent-depth
  3847. 0))))
  3848. (if (and (> predecessor-depth 0)
  3849. (> (1+ current-depth)
  3850. (1+ predecessor-depth)))
  3851. (error (concat "Disallowed shift deeper than"
  3852. " containing topic's children."))
  3853. (allout-back-to-current-heading)
  3854. (if (< allout-recent-depth (1+ current-depth))
  3855. (allout-show-children))))))
  3856. (let ((where (point)))
  3857. (allout-rebullet-topic 1 (and (> arg 1) 'sans-offspring))
  3858. (run-hook-with-args 'allout-structure-shifted-functions arg where))))
  3859. ;;;_ > allout-shift-out (arg)
  3860. (defun allout-shift-out (arg)
  3861. "Decrease depth of current heading and any topics collapsed within it.
  3862. This will make the item a sibling of its former container.
  3863. With a negative argument, the item is shifted in using
  3864. `allout-shift-in', instead.
  3865. With an argument greater than one, shift-out the item's offspring
  3866. but not the item itself, making the former children siblings of
  3867. the item.
  3868. With an argument greater than 1, the item's offspring are shifted
  3869. out without shifting the item. This will make the immediate
  3870. subtopics into siblings of the item."
  3871. (interactive "p")
  3872. (if (< arg 0)
  3873. (allout-shift-in (* arg -1))
  3874. ;; Get proper exposure in this area:
  3875. (save-excursion (if (allout-ascend)
  3876. (allout-show-children)))
  3877. ;; Show collapsed children if there's a successor which will become
  3878. ;; their sibling:
  3879. (if (and (allout-current-topic-collapsed-p)
  3880. (save-excursion (allout-next-sibling)))
  3881. (allout-show-children))
  3882. (let ((where (and (allout-depth) allout-recent-prefix-beginning)))
  3883. (save-excursion
  3884. (if (> arg 1)
  3885. ;; Shift the offspring but not the topic:
  3886. (let ((children-chart (allout-chart-subtree 1)))
  3887. (if (listp (car children-chart))
  3888. ;; whoops:
  3889. (setq children-chart (allout-flatten children-chart)))
  3890. (save-excursion
  3891. (dolist (child-point children-chart)
  3892. (goto-char child-point)
  3893. (allout-shift-out 1))))
  3894. (allout-rebullet-topic (* arg -1))))
  3895. (run-hook-with-args 'allout-structure-shifted-functions (* arg -1) where))))
  3896. ;;;_ : Surgery (kill-ring) functions with special provisions for outlines:
  3897. ;;;_ > allout-kill-line (&optional arg)
  3898. (defun allout-kill-line (&optional arg)
  3899. "Kill line, adjusting subsequent lines suitably for outline mode."
  3900. (interactive "*P")
  3901. (if (or (not (allout-mode-p))
  3902. (not (bolp))
  3903. (not (save-match-data (looking-at allout-regexp))))
  3904. ;; Just do a regular kill:
  3905. (kill-line arg)
  3906. ;; Ah, have to watch out for adjustments:
  3907. (let* ((beg (point))
  3908. end
  3909. (beg-hidden (allout-hidden-p))
  3910. (end-hidden (save-excursion (allout-end-of-current-line)
  3911. (setq end (point))
  3912. (allout-hidden-p)))
  3913. (depth (allout-depth)))
  3914. (allout-annotate-hidden beg end)
  3915. (unwind-protect
  3916. (if (and (not beg-hidden) (not end-hidden))
  3917. (allout-unprotected (kill-line arg))
  3918. (kill-line arg))
  3919. (run-hooks 'allout-after-copy-or-kill-hook)
  3920. (allout-deannotate-hidden beg end)
  3921. (if allout-numbered-bullet
  3922. (save-excursion ; Renumber subsequent topics if needed:
  3923. (if (not (save-match-data (looking-at allout-regexp)))
  3924. (allout-next-heading))
  3925. (allout-renumber-to-depth depth)))
  3926. (run-hook-with-args 'allout-structure-deleted-functions depth (point))))))
  3927. ;;;_ > allout-copy-line-as-kill ()
  3928. (defun allout-copy-line-as-kill ()
  3929. "Like `allout-kill-topic', but save to kill ring instead of deleting."
  3930. (interactive)
  3931. (let ((buffer-read-only t))
  3932. (condition-case nil
  3933. (allout-kill-line)
  3934. (buffer-read-only nil))))
  3935. ;;;_ > allout-kill-topic ()
  3936. (defun allout-kill-topic ()
  3937. "Kill topic together with subtopics.
  3938. Trailing whitespace is killed with a topic if that whitespace:
  3939. - would separate the topic from a subsequent sibling
  3940. - would separate the topic from the end of buffer
  3941. - would not be added to whitespace already separating the topic from the
  3942. previous one.
  3943. Topic exposure is marked with text-properties, to be used by
  3944. `allout-yank-processing' for exposure recovery."
  3945. (interactive)
  3946. (let* ((inhibit-field-text-motion t)
  3947. (beg (prog1 (allout-back-to-current-heading) (beginning-of-line)))
  3948. end
  3949. (depth allout-recent-depth))
  3950. (allout-end-of-current-subtree)
  3951. (if (and (/= (current-column) 0) (not (eobp)))
  3952. (forward-char 1))
  3953. (if (not (eobp))
  3954. (if (and (save-match-data (looking-at "\n"))
  3955. (or (save-excursion
  3956. (or (not (allout-next-heading))
  3957. (= depth allout-recent-depth)))
  3958. (and (> (- beg (point-min)) 3)
  3959. (string= (buffer-substring (- beg 2) beg) "\n\n"))))
  3960. (forward-char 1)))
  3961. (allout-annotate-hidden beg (setq end (point)))
  3962. (unwind-protect ; for possible barf-if-buffer-read-only.
  3963. (allout-unprotected (kill-region beg end))
  3964. (allout-deannotate-hidden beg end)
  3965. (run-hooks 'allout-after-copy-or-kill-hook)
  3966. (save-excursion
  3967. (allout-renumber-to-depth depth))
  3968. (run-hook-with-args 'allout-structure-deleted-functions depth (point)))))
  3969. ;;;_ > allout-copy-topic-as-kill ()
  3970. (defun allout-copy-topic-as-kill ()
  3971. "Like `allout-kill-topic', but save to kill ring instead of deleting."
  3972. (interactive)
  3973. (let ((buffer-read-only t))
  3974. (condition-case nil
  3975. (allout-kill-topic)
  3976. (buffer-read-only (message "Topic copied...")))))
  3977. ;;;_ > allout-annotate-hidden (begin end)
  3978. (defun allout-annotate-hidden (begin end)
  3979. "Qualify text with properties to indicate exposure status."
  3980. (let ((was-modified (buffer-modified-p))
  3981. (buffer-read-only nil))
  3982. (allout-deannotate-hidden begin end)
  3983. (save-excursion
  3984. (goto-char begin)
  3985. (let (done next prev overlay)
  3986. (while (not done)
  3987. ;; at or advance to start of next hidden region:
  3988. (if (not (allout-hidden-p))
  3989. (setq next
  3990. (max (1+ (point))
  3991. (allout-next-single-char-property-change (point)
  3992. 'invisible
  3993. nil end))))
  3994. (if (or (not next) (eq prev next))
  3995. ;; still not at start of hidden area -- must not be any left.
  3996. (setq done t)
  3997. (goto-char next)
  3998. (setq prev next)
  3999. (if (not (allout-hidden-p))
  4000. ;; still not at start of hidden area.
  4001. (setq done t)
  4002. (setq overlay (allout-get-invisibility-overlay))
  4003. (setq next (overlay-end overlay)
  4004. prev next)
  4005. ;; advance to end of this hidden area:
  4006. (when next
  4007. (goto-char next)
  4008. (allout-unprotected
  4009. (let ((buffer-undo-list t))
  4010. (put-text-property (overlay-start overlay) next
  4011. 'allout-was-hidden t)))))))))
  4012. (set-buffer-modified-p was-modified)))
  4013. ;;;_ > allout-deannotate-hidden (begin end)
  4014. (defun allout-deannotate-hidden (begin end)
  4015. "Remove allout hidden-text annotation between BEGIN and END."
  4016. (allout-unprotected
  4017. (let ((inhibit-read-only t)
  4018. (buffer-undo-list t))
  4019. (remove-text-properties begin (min end (point-max))
  4020. '(allout-was-hidden t)))))
  4021. ;;;_ > allout-hide-by-annotation (begin end)
  4022. (defun allout-hide-by-annotation (begin end)
  4023. "Translate text properties indicating exposure status into actual exposure."
  4024. (save-excursion
  4025. (goto-char begin)
  4026. (let ((was-modified (buffer-modified-p))
  4027. done next prev)
  4028. (while (not done)
  4029. ;; at or advance to start of next annotation:
  4030. (if (not (get-text-property (point) 'allout-was-hidden))
  4031. (setq next (allout-next-single-char-property-change
  4032. (point) 'allout-was-hidden nil end)))
  4033. (if (or (not next) (eq prev next))
  4034. ;; no more or not advancing -- must not be any left.
  4035. (setq done t)
  4036. (goto-char next)
  4037. (setq prev next)
  4038. (if (not (get-text-property (point) 'allout-was-hidden))
  4039. ;; still not at start of annotation.
  4040. (setq done t)
  4041. ;; advance to just after end of this annotation:
  4042. (setq next (allout-next-single-char-property-change
  4043. (point) 'allout-was-hidden nil end))
  4044. (let ((o (make-overlay prev next nil 'front-advance)))
  4045. (overlay-put o 'category 'allout-exposure-category)
  4046. (overlay-put o 'evaporate t))
  4047. (allout-deannotate-hidden prev next)
  4048. (setq prev next)
  4049. (if next (goto-char next)))))
  4050. (set-buffer-modified-p was-modified))))
  4051. ;;;_ > allout-yank-processing ()
  4052. (defun allout-yank-processing (&optional _arg)
  4053. "Incidental allout-specific business to be done just after text yanks.
  4054. Does depth adjustment of yanked topics, when:
  4055. 1 the stuff being yanked starts with a valid outline header prefix, and
  4056. 2 it is being yanked at the end of a line which consists of only a valid
  4057. topic prefix.
  4058. Also, adjusts numbering of subsequent siblings when appropriate.
  4059. Depth adjustment alters the depth of all the topics being yanked
  4060. the amount it takes to make the first topic have the depth of the
  4061. header into which it's being yanked.
  4062. The point is left in front of yanked, adjusted topics, rather than
  4063. at the end (and vice-versa with the mark). Non-adjusted yanks,
  4064. however, are left exactly like normal, non-allout-specific yanks."
  4065. (interactive "*P")
  4066. ; Get to beginning, leaving
  4067. ; region around subject:
  4068. (if (< (allout-mark-marker t) (point))
  4069. (exchange-point-and-mark))
  4070. (save-match-data
  4071. (let* ((subj-beg (point))
  4072. (into-bol (bolp))
  4073. (subj-end (allout-mark-marker t))
  4074. ;; 'resituate' if yanking an entire topic into topic header:
  4075. (resituate (and (let ((allout-inhibit-aberrance-doublecheck t))
  4076. (allout-e-o-prefix-p))
  4077. (looking-at allout-regexp)
  4078. (allout-prefix-data)))
  4079. ;; `rectify-numbering' if resituating (where several topics may
  4080. ;; be resituating) or yanking a topic into a topic slot (bol):
  4081. (rectify-numbering (or resituate
  4082. (and into-bol (looking-at allout-regexp)))))
  4083. (if resituate
  4084. ;; Yanking a topic into the start of a topic -- reconcile to fit:
  4085. (let* ((inhibit-field-text-motion t)
  4086. (prefix-len (if (not (match-end 1))
  4087. 1
  4088. (- (match-end 1) subj-beg)))
  4089. (subj-depth allout-recent-depth)
  4090. (prefix-bullet (allout-recent-bullet))
  4091. (adjust-to-depth
  4092. ;; Nil if adjustment unnecessary, otherwise depth to which
  4093. ;; adjustment should be made:
  4094. (save-excursion
  4095. (and (goto-char subj-end)
  4096. (eolp)
  4097. (goto-char subj-beg)
  4098. (and (looking-at allout-regexp)
  4099. (progn
  4100. (beginning-of-line)
  4101. (not (= (point) subj-beg)))
  4102. (looking-at allout-regexp)
  4103. (allout-prefix-data))
  4104. allout-recent-depth)))
  4105. (more t))
  4106. (setq rectify-numbering allout-numbered-bullet)
  4107. (if adjust-to-depth
  4108. ; Do the adjustment:
  4109. (progn
  4110. (save-restriction
  4111. (narrow-to-region subj-beg subj-end)
  4112. ; Trim off excessive blank
  4113. ; line at end, if any:
  4114. (goto-char (point-max))
  4115. (if (looking-at "^$")
  4116. (allout-unprotected (delete-char -1)))
  4117. ; Work backwards, with each
  4118. ; shallowest level,
  4119. ; successively excluding the
  4120. ; last processed topic from
  4121. ; the narrow region:
  4122. (while more
  4123. (allout-back-to-current-heading)
  4124. ; go as high as we can in each bunch:
  4125. (while (allout-ascend t))
  4126. (save-excursion
  4127. (allout-unprotected
  4128. (allout-rebullet-topic-grunt (- adjust-to-depth
  4129. subj-depth)))
  4130. (allout-depth))
  4131. (if (setq more (not (bobp)))
  4132. (progn (widen)
  4133. (forward-char -1)
  4134. (narrow-to-region subj-beg (point))))))
  4135. ;; Remove new heading prefix:
  4136. (allout-unprotected
  4137. (progn
  4138. (delete-region (point) (+ (point)
  4139. prefix-len
  4140. (- adjust-to-depth
  4141. subj-depth)))
  4142. ; and delete residual subj
  4143. ; prefix digits and space:
  4144. (while (looking-at "[0-9]") (delete-char 1))
  4145. (delete-char -1)
  4146. (if (not (eolp))
  4147. (forward-char))))
  4148. ;; Assert new topic's bullet - minimal effort if unchanged:
  4149. (allout-rebullet-heading (string-to-char prefix-bullet)))
  4150. (exchange-point-and-mark))))
  4151. (if rectify-numbering
  4152. (progn
  4153. (save-excursion
  4154. ; Give some preliminary feedback:
  4155. (message "... reconciling numbers")
  4156. ; ... and renumber, in case necessary:
  4157. (goto-char subj-beg)
  4158. (if (allout-goto-prefix-doublechecked)
  4159. (allout-unprotected
  4160. (allout-rebullet-heading nil ;;; instead
  4161. (allout-depth) ;;; depth
  4162. nil ;;; number-control
  4163. nil ;;; index
  4164. t)))
  4165. (message ""))))
  4166. (if (or into-bol resituate)
  4167. (allout-hide-by-annotation (point) (allout-mark-marker t))
  4168. (allout-deannotate-hidden (allout-mark-marker t) (point)))
  4169. (if (not resituate)
  4170. (exchange-point-and-mark))
  4171. (run-hook-with-args 'allout-structure-added-functions subj-beg subj-end))))
  4172. ;;;_ > allout-yank (&optional arg)
  4173. (defun allout-yank (&optional arg)
  4174. "`allout-mode' yank, with depth and numbering adjustment of yanked topics.
  4175. Non-topic yanks work no differently than normal yanks.
  4176. If a topic is being yanked into a bare topic prefix, the depth of the
  4177. yanked topic is adjusted to the depth of the topic prefix.
  4178. 1 we're yanking in an `allout-mode' buffer
  4179. 2 the stuff being yanked starts with a valid outline header prefix, and
  4180. 3 it is being yanked at the end of a line which consists of only a valid
  4181. topic prefix.
  4182. If these conditions hold then the depth of the yanked topics are all
  4183. adjusted the amount it takes to make the first one at the depth of the
  4184. header into which it's being yanked.
  4185. The point is left in front of yanked, adjusted topics, rather than
  4186. at the end (and vice-versa with the mark). Non-adjusted yanks,
  4187. however, (ones that don't qualify for adjustment) are handled
  4188. exactly like normal yanks.
  4189. Numbering of yanked topics, and the successive siblings at the depth
  4190. into which they're being yanked, is adjusted.
  4191. `allout-yank-pop' works with `allout-yank' just like normal `yank-pop'
  4192. works with normal `yank' in non-outline buffers."
  4193. (interactive "*P")
  4194. (setq this-command 'yank)
  4195. (allout-unprotected
  4196. (yank arg))
  4197. (if (allout-mode-p)
  4198. (allout-yank-processing)))
  4199. ;;;_ > allout-yank-pop (&optional arg)
  4200. (defun allout-yank-pop (&optional arg)
  4201. "Yank-pop like `allout-yank' when popping to bare outline prefixes.
  4202. Adapts level of popped topics to level of fresh prefix.
  4203. Note -- prefix changes to distinctive bullets will stick, if followed
  4204. by pops to non-distinctive yanks. Bug..."
  4205. (interactive "*p")
  4206. (setq this-command 'yank)
  4207. (yank-pop arg)
  4208. (if (allout-mode-p)
  4209. (allout-yank-processing)))
  4210. ;;;_ - Specialty bullet functions
  4211. ;;;_ : File Cross references
  4212. ;;;_ > allout-resolve-xref ()
  4213. (defun allout-resolve-xref ()
  4214. "Pop to file associated with current heading, if it has an xref bullet.
  4215. \(Works according to setting of `allout-file-xref-bullet')."
  4216. (interactive)
  4217. (if (not allout-file-xref-bullet)
  4218. (error
  4219. "Outline cross references disabled -- no `allout-file-xref-bullet'")
  4220. (if (not (string= (allout-current-bullet) allout-file-xref-bullet))
  4221. (error "Current heading lacks cross-reference bullet `%s'"
  4222. allout-file-xref-bullet)
  4223. (let ((inhibit-field-text-motion t)
  4224. file-name)
  4225. (save-match-data
  4226. (save-excursion
  4227. (let* ((text-start allout-recent-prefix-end)
  4228. (heading-end (point-at-eol)))
  4229. (goto-char text-start)
  4230. (setq file-name
  4231. (if (re-search-forward "\\s-\\(\\S-*\\)" heading-end t)
  4232. (buffer-substring (match-beginning 1)
  4233. (match-end 1)))))))
  4234. (setq file-name (expand-file-name file-name))
  4235. (if (or (file-exists-p file-name)
  4236. (if (file-writable-p file-name)
  4237. (y-or-n-p (format "%s not there, create one? "
  4238. file-name))
  4239. (error "%s not found and can't be created" file-name)))
  4240. (condition-case failure
  4241. (find-file-other-window file-name)
  4242. (error failure))
  4243. (error "%s not found" file-name))
  4244. )
  4245. )
  4246. )
  4247. )
  4248. ;;;_ #6 Exposure Control
  4249. ;;;_ - Fundamental
  4250. ;;;_ > allout-flag-region (from to flag)
  4251. (defun allout-flag-region (from to flag)
  4252. "Conceal text between FROM and TO if FLAG is non-nil, else reveal it.
  4253. After the exposure changes are made, run the abnormal hook
  4254. `allout-exposure-change-functions' with the same arguments as
  4255. this function."
  4256. ;; We use outline invisibility spec.
  4257. (remove-overlays from to 'category 'allout-exposure-category)
  4258. (when flag
  4259. (let ((o (make-overlay from to nil 'front-advance)))
  4260. (overlay-put o 'category 'allout-exposure-category)
  4261. (overlay-put o 'evaporate t)
  4262. (when (featurep 'xemacs)
  4263. (let ((props (symbol-plist 'allout-exposure-category)))
  4264. (while props
  4265. (condition-case nil
  4266. ;; as of 2008-02-27, xemacs lacks modification-hooks
  4267. (overlay-put o (pop props) (pop props))
  4268. (error nil))))))
  4269. (setq allout-this-command-hid-text t))
  4270. (run-hook-with-args 'allout-exposure-change-functions from to flag))
  4271. ;;;_ > allout-flag-current-subtree (flag)
  4272. (defun allout-flag-current-subtree (flag)
  4273. "Conceal currently-visible topic's subtree if FLAG non-nil, else reveal it."
  4274. (save-excursion
  4275. (allout-back-to-current-heading)
  4276. (let ((inhibit-field-text-motion t))
  4277. (end-of-line))
  4278. (allout-flag-region (point)
  4279. ;; Exposing must not leave trailing blanks hidden,
  4280. ;; but can leave them exposed when hiding, so we
  4281. ;; can use flag's inverse as the
  4282. ;; include-trailing-blank cue:
  4283. (allout-end-of-current-subtree (not flag))
  4284. flag)))
  4285. ;;;_ - Topic-specific
  4286. ;;;_ > allout-show-entry ()
  4287. (defun allout-show-entry ()
  4288. "Like `allout-show-current-entry', but reveals entries in hidden topics.
  4289. This is a way to give restricted peek at a concealed locality without the
  4290. expense of exposing its context, but can leave the outline with aberrant
  4291. exposure. `allout-show-offshoot' should be used after the peek to rectify
  4292. the exposure."
  4293. (interactive)
  4294. (save-excursion
  4295. (let (beg end)
  4296. (allout-goto-prefix-doublechecked)
  4297. (setq beg (if (allout-hidden-p) (1- (point)) (point)))
  4298. (setq end (allout-pre-next-prefix))
  4299. (allout-flag-region beg end nil)
  4300. (list beg end))))
  4301. ;;;_ > allout-show-children (&optional level strict)
  4302. (defun allout-show-children (&optional level strict)
  4303. "If point is visible, show all direct subheadings of this heading.
  4304. Otherwise, do `allout-show-to-offshoot', and then show subheadings.
  4305. Optional LEVEL specifies how many levels below the current level
  4306. should be shown, or all levels if t. Default is 1.
  4307. Optional STRICT means don't resort to -show-to-offshoot, no matter
  4308. what. This is basically so -show-to-offshoot, which is called by
  4309. this function, can employ the pure offspring-revealing capabilities of
  4310. it.
  4311. Returns point at end of subtree that was opened, if any. (May get a
  4312. point of non-opened subtree?)"
  4313. (interactive "p")
  4314. (let ((start-point (point)))
  4315. (if (and (not strict)
  4316. (allout-hidden-p))
  4317. (progn (allout-show-to-offshoot) ; Point's concealed, open to
  4318. ; expose it.
  4319. ;; Then recurse, but with "strict" set so we don't
  4320. ;; infinite regress:
  4321. (allout-show-children level t))
  4322. (save-excursion
  4323. (allout-beginning-of-current-line)
  4324. (save-restriction
  4325. (let* (depth
  4326. ;; translate the level spec for this routine to the ones
  4327. ;; used by -chart-subtree and -chart-to-reveal:
  4328. (chart-level (cond ((not level) 1)
  4329. ((eq level t) nil)
  4330. (t level)))
  4331. (chart (allout-chart-subtree chart-level))
  4332. (to-reveal (or (allout-chart-to-reveal chart chart-level)
  4333. ;; interactive, show discontinuous children:
  4334. (and chart
  4335. (allout-called-interactively-p)
  4336. (save-excursion
  4337. (allout-back-to-current-heading)
  4338. (setq depth (allout-current-depth))
  4339. (and (allout-next-heading)
  4340. (> allout-recent-depth
  4341. (1+ depth))))
  4342. (message
  4343. "Discontinuous offspring; use `%s %s'%s."
  4344. (substitute-command-keys
  4345. "\\[universal-argument]")
  4346. (substitute-command-keys
  4347. "\\[allout-shift-out]")
  4348. " to elevate them.")
  4349. (allout-chart-to-reveal
  4350. chart (- allout-recent-depth depth))))))
  4351. (goto-char start-point)
  4352. (when (and strict (allout-hidden-p))
  4353. ;; Concealed root would already have been taken care of,
  4354. ;; unless strict was set.
  4355. (allout-flag-region (point) (allout-snug-back) nil)
  4356. (when allout-show-bodies
  4357. (goto-char (car to-reveal))
  4358. (allout-show-current-entry)))
  4359. (while to-reveal
  4360. (goto-char (car to-reveal))
  4361. (allout-flag-region (save-excursion (allout-snug-back) (point))
  4362. (progn (search-forward "\n" nil t)
  4363. (1- (point)))
  4364. nil)
  4365. (when allout-show-bodies
  4366. (goto-char (car to-reveal))
  4367. (allout-show-current-entry))
  4368. (setq to-reveal (cdr to-reveal)))))))
  4369. ;; Compensate for `save-excursion's maintenance of point
  4370. ;; within invisible text:
  4371. (goto-char start-point)))
  4372. ;;;_ > allout-show-to-offshoot ()
  4373. (defun allout-show-to-offshoot ()
  4374. "Like `allout-show-entry', but reveals all concealed ancestors, as well.
  4375. Useful for coherently exposing to a random point in a hidden region."
  4376. (interactive)
  4377. (save-excursion
  4378. (let ((inhibit-field-text-motion t)
  4379. (orig-pt (point))
  4380. (orig-pref (allout-goto-prefix-doublechecked))
  4381. (last-at (point))
  4382. (bag-it 0))
  4383. (while (or (> bag-it 1) (allout-hidden-p))
  4384. (while (allout-hidden-p)
  4385. (move-beginning-of-line 1)
  4386. (if (allout-hidden-p) (forward-char -1)))
  4387. (if (= last-at (setq last-at (point)))
  4388. ;; Oops, we're not making any progress! Show the current topic
  4389. ;; completely, and try one more time here, if we haven't already.
  4390. (progn (beginning-of-line)
  4391. (allout-show-current-subtree)
  4392. (goto-char orig-pt)
  4393. (setq bag-it (1+ bag-it))
  4394. (if (> bag-it 1)
  4395. (error "allout-show-to-offshoot: %s"
  4396. "Stumped by aberrant nesting.")))
  4397. (if (> bag-it 0) (setq bag-it 0))
  4398. (allout-show-children)
  4399. (goto-char orig-pref)))
  4400. (goto-char orig-pt)))
  4401. (if (allout-hidden-p)
  4402. (allout-show-entry)))
  4403. ;;;_ > allout-hide-current-entry ()
  4404. (defun allout-hide-current-entry ()
  4405. "Hide the body directly following this heading."
  4406. (interactive)
  4407. (allout-back-to-current-heading)
  4408. (save-excursion
  4409. (let ((inhibit-field-text-motion t))
  4410. (end-of-line))
  4411. (allout-flag-region (point)
  4412. (progn (allout-end-of-entry) (point))
  4413. t)))
  4414. ;;;_ > allout-show-current-entry (&optional arg)
  4415. (defun allout-show-current-entry (&optional arg)
  4416. "Show body following current heading, or hide entry with universal argument."
  4417. (interactive "P")
  4418. (if arg
  4419. (allout-hide-current-entry)
  4420. (save-excursion (allout-show-to-offshoot))
  4421. (save-excursion
  4422. (allout-flag-region (point)
  4423. (progn (allout-end-of-entry t) (point))
  4424. nil)
  4425. )))
  4426. ;;;_ > allout-show-current-subtree (&optional arg)
  4427. (defun allout-show-current-subtree (&optional arg)
  4428. "Show everything within the current topic.
  4429. With a repeat-count, expose this topic and its siblings."
  4430. (interactive "P")
  4431. (save-excursion
  4432. (if (<= (allout-current-depth) 0)
  4433. ;; Outside any topics -- try to get to the first:
  4434. (if (not (allout-next-heading))
  4435. (error "No topics")
  4436. ;; got to first, outermost topic -- set to expose it and siblings:
  4437. (message "Above outermost topic -- exposing all.")
  4438. (allout-flag-region (point-min)(point-max) nil))
  4439. (allout-beginning-of-current-line)
  4440. (if (not arg)
  4441. (allout-flag-current-subtree nil)
  4442. (allout-beginning-of-level)
  4443. (allout-expose-topic '(* :))))))
  4444. ;;;_ > allout-current-topic-collapsed-p (&optional include-single-liners)
  4445. (defun allout-current-topic-collapsed-p (&optional include-single-liners)
  4446. "True if the currently visible containing topic is already collapsed.
  4447. Single line topics intrinsically can be considered as being both
  4448. collapsed and uncollapsed. If optional INCLUDE-SINGLE-LINERS is
  4449. true, then single-line topics are considered to be collapsed. By
  4450. default, they are treated as being uncollapsed."
  4451. (save-match-data
  4452. (save-excursion
  4453. (and
  4454. ;; Is the topic all on one line (allowing for trailing blank line)?
  4455. (>= (progn (allout-back-to-current-heading)
  4456. (let ((inhibit-field-text-motion t))
  4457. (move-end-of-line 1))
  4458. (point))
  4459. (allout-end-of-current-subtree (not (looking-at "\n\n"))))
  4460. (or include-single-liners
  4461. (progn (backward-char 1) (allout-hidden-p)))))))
  4462. ;;;_ > allout-hide-current-subtree (&optional just-close)
  4463. (defun allout-hide-current-subtree (&optional just-close)
  4464. "Close the current topic, or containing topic if this one is already closed.
  4465. If this topic is closed and it's a top level topic, close this topic
  4466. and its siblings.
  4467. If optional arg JUST-CLOSE is non-nil, do not close the parent or
  4468. siblings, even if the target topic is already closed."
  4469. (interactive)
  4470. (let* ((from (point))
  4471. (sibs-msg "Top-level topic already closed -- closing siblings...")
  4472. (current-exposed (not (allout-current-topic-collapsed-p t))))
  4473. (cond (current-exposed (allout-flag-current-subtree t))
  4474. (just-close nil)
  4475. ((allout-ascend) (allout-hide-current-subtree))
  4476. (t (goto-char 0)
  4477. (message sibs-msg)
  4478. (allout-goto-prefix-doublechecked)
  4479. (allout-expose-topic '(0 :))
  4480. (message (concat sibs-msg " Done."))))
  4481. (goto-char from)))
  4482. ;;;_ > allout-toggle-current-subtree-exposure
  4483. (defun allout-toggle-current-subtree-exposure ()
  4484. "Show or hide the current subtree depending on its current state."
  4485. ;; thanks to tassilo for suggesting this.
  4486. (interactive)
  4487. (save-excursion
  4488. (allout-back-to-heading)
  4489. (if (allout-hidden-p (point-at-eol))
  4490. (allout-show-current-subtree)
  4491. (allout-hide-current-subtree))))
  4492. ;;;_ > allout-show-current-branches ()
  4493. (defun allout-show-current-branches ()
  4494. "Show all subheadings of this heading, but not their bodies."
  4495. (interactive)
  4496. (let ((inhibit-field-text-motion t))
  4497. (beginning-of-line))
  4498. (allout-show-children t))
  4499. ;;;_ > allout-hide-current-leaves ()
  4500. (defun allout-hide-current-leaves ()
  4501. "Hide the bodies of the current topic and all its offspring."
  4502. (interactive)
  4503. (allout-back-to-current-heading)
  4504. (allout-hide-region-body (point) (progn (allout-end-of-current-subtree)
  4505. (point))))
  4506. ;;;_ - Region and beyond
  4507. ;;;_ > allout-show-all ()
  4508. (defun allout-show-all ()
  4509. "Show all of the text in the buffer."
  4510. (interactive)
  4511. (message "Exposing entire buffer...")
  4512. (allout-flag-region (point-min) (point-max) nil)
  4513. (message "Exposing entire buffer... Done."))
  4514. ;;;_ > allout-hide-bodies ()
  4515. (defun allout-hide-bodies ()
  4516. "Hide all of buffer except headings."
  4517. (interactive)
  4518. (allout-hide-region-body (point-min) (point-max)))
  4519. ;;;_ > allout-hide-region-body (start end)
  4520. (defun allout-hide-region-body (start end)
  4521. "Hide all body lines in the region, but not headings."
  4522. (save-match-data
  4523. (save-excursion
  4524. (save-restriction
  4525. (narrow-to-region start end)
  4526. (goto-char (point-min))
  4527. (let ((inhibit-field-text-motion t))
  4528. (while (not (eobp))
  4529. (end-of-line)
  4530. (allout-flag-region (point) (allout-end-of-entry) t)
  4531. (if (not (eobp))
  4532. (forward-char
  4533. (if (looking-at "\n\n")
  4534. 2 1)))))))))
  4535. ;;;_ > allout-expose-topic (spec)
  4536. (defun allout-expose-topic (spec)
  4537. "Apply exposure specs to successive outline topic items.
  4538. Use the more convenient frontend, `allout-new-exposure', if you don't
  4539. need evaluation of the arguments, or even better, the `allout-layout'
  4540. variable-keyed mode-activation/auto-exposure feature of allout outline
  4541. mode. See the respective documentation strings for more details.
  4542. Cursor is left at start position.
  4543. SPEC is either a number or a list.
  4544. Successive specs on a list are applied to successive sibling topics.
  4545. A simple spec (either a number, one of a few symbols, or the null
  4546. list) dictates the exposure for the corresponding topic.
  4547. Non-null lists recursively designate exposure specs for respective
  4548. subtopics of the current topic.
  4549. The `:' repeat spec is used to specify exposure for any number of
  4550. successive siblings, up to the trailing ones for which there are
  4551. explicit specs following the `:'.
  4552. Simple (numeric and null-list) specs are interpreted as follows:
  4553. Numbers indicate the relative depth to open the corresponding topic.
  4554. - negative numbers force the topic to be closed before opening to the
  4555. absolute value of the number, so all siblings are open only to
  4556. that level.
  4557. - positive numbers open to the relative depth indicated by the
  4558. number, but do not force already opened subtopics to be closed.
  4559. - 0 means to close topic -- hide all offspring.
  4560. : - `repeat'
  4561. apply prior element to all siblings at current level, *up to*
  4562. those siblings that would be covered by specs following the `:'
  4563. on the list. Ie, apply to all topics at level but the last
  4564. ones. (Only first of multiple colons at same level is
  4565. respected -- subsequent ones are discarded.)
  4566. * - completely opens the topic, including bodies.
  4567. + - shows all the sub headers, but not the bodies
  4568. - - exposes the body of the corresponding topic.
  4569. Examples:
  4570. \(allout-expose-topic \\='(-1 : 0))
  4571. Close this and all following topics at current level, exposing
  4572. only their immediate children, but close down the last topic
  4573. at this current level completely.
  4574. \(allout-expose-topic \\='(-1 () : 1 0))
  4575. Close current topic so only the immediate subtopics are shown;
  4576. show the children in the second to last topic, and completely
  4577. close the last one.
  4578. \(allout-expose-topic \\='(-2 : -1 *))
  4579. Expose children and grandchildren of all topics at current
  4580. level except the last two; expose children of the second to
  4581. last and completely open the last one."
  4582. (interactive "xExposure spec: ")
  4583. (if (not (listp spec))
  4584. nil
  4585. (let ((depth (allout-depth))
  4586. (max-pos 0)
  4587. prev-elem curr-elem
  4588. stay)
  4589. (while spec
  4590. (setq prev-elem curr-elem
  4591. curr-elem (car spec)
  4592. spec (cdr spec))
  4593. (cond ; Do current element:
  4594. ((null curr-elem) nil)
  4595. ((symbolp curr-elem)
  4596. (cond ((eq curr-elem '*) (allout-show-current-subtree)
  4597. (if (> allout-recent-end-of-subtree max-pos)
  4598. (setq max-pos allout-recent-end-of-subtree)))
  4599. ((eq curr-elem '+)
  4600. (if (not (allout-hidden-p))
  4601. (save-excursion (allout-hide-current-subtree t)))
  4602. (allout-show-current-branches)
  4603. (if (> allout-recent-end-of-subtree max-pos)
  4604. (setq max-pos allout-recent-end-of-subtree)))
  4605. ((eq curr-elem '-) (allout-show-current-entry))
  4606. ((eq curr-elem ':)
  4607. (setq stay t)
  4608. ;; Expand the `repeat' spec to an explicit version,
  4609. ;; w.r.t. remaining siblings:
  4610. (let ((residue ; = # of sibs not covered by remaining spec
  4611. ;; Dang, could be nice to make use of the chart, sigh:
  4612. (- (length (allout-chart-siblings))
  4613. (length spec))))
  4614. (if (< 0 residue)
  4615. ;; Some residue -- cover it with prev-elem:
  4616. (setq spec (append (make-list residue prev-elem)
  4617. spec)))))))
  4618. ((numberp curr-elem)
  4619. (if (and (>= 0 curr-elem) (not (allout-hidden-p)))
  4620. (save-excursion (allout-hide-current-subtree t)
  4621. (if (> 0 curr-elem)
  4622. nil
  4623. (if (> allout-recent-end-of-subtree max-pos)
  4624. (setq max-pos
  4625. allout-recent-end-of-subtree)))))
  4626. (if (> (abs curr-elem) 0)
  4627. (progn (allout-show-children (abs curr-elem))
  4628. (if (> allout-recent-end-of-subtree max-pos)
  4629. (setq max-pos allout-recent-end-of-subtree)))))
  4630. ((listp curr-elem)
  4631. (if (allout-descend-to-depth (1+ depth))
  4632. (let ((got (allout-expose-topic curr-elem)))
  4633. (if (and got (> got max-pos)) (setq max-pos got))))))
  4634. (cond (stay (setq stay nil))
  4635. ((listp (car spec)) nil)
  4636. ((> max-pos (point))
  4637. ;; Capitalize on max-pos state to get us nearer next sibling:
  4638. (progn (goto-char (min (point-max) max-pos))
  4639. (allout-next-heading)))
  4640. ((allout-next-sibling depth))))
  4641. max-pos)))
  4642. ;;;_ > allout-old-expose-topic (spec &rest followers)
  4643. (defun allout-old-expose-topic (spec &rest followers)
  4644. "Deprecated. Use `allout-expose-topic' (with different schema
  4645. format) instead.
  4646. Dictate wholesale exposure scheme for current topic, according to SPEC.
  4647. SPEC is either a number or a list. Optional successive args
  4648. dictate exposure for subsequent siblings of current topic.
  4649. A simple spec (either a number, a special symbol, or the null list)
  4650. dictates the overall exposure for a topic. Non null lists are
  4651. composite specs whose first element dictates the overall exposure for
  4652. a topic, with the subsequent elements in the list interpreted as specs
  4653. that dictate the exposure for the successive offspring of the topic.
  4654. Simple (numeric and null-list) specs are interpreted as follows:
  4655. - Numbers indicate the relative depth to open the corresponding topic:
  4656. - negative numbers force the topic to be close before opening to the
  4657. absolute value of the number.
  4658. - positive numbers just open to the relative depth indicated by the number.
  4659. - 0 just closes
  4660. - `*' completely opens the topic, including bodies.
  4661. - `+' shows all the sub headers, but not the bodies
  4662. - `-' exposes the body and immediate offspring of the corresponding topic.
  4663. If the spec is a list, the first element must be a number, which
  4664. dictates the exposure depth of the topic as a whole. Subsequent
  4665. elements of the list are nested SPECs, dictating the specific exposure
  4666. for the corresponding offspring of the topic.
  4667. Optional FOLLOWERS arguments dictate exposure for succeeding siblings."
  4668. (interactive "xExposure spec: ")
  4669. (let ((inhibit-field-text-motion t)
  4670. (depth (allout-current-depth))
  4671. max-pos)
  4672. (cond ((null spec) nil)
  4673. ((symbolp spec)
  4674. (if (eq spec '*) (allout-show-current-subtree))
  4675. (if (eq spec '+) (allout-show-current-branches))
  4676. (if (eq spec '-) (allout-show-current-entry)))
  4677. ((numberp spec)
  4678. (if (>= 0 spec)
  4679. (save-excursion (allout-hide-current-subtree t)
  4680. (end-of-line)
  4681. (if (or (not max-pos)
  4682. (> (point) max-pos))
  4683. (setq max-pos (point)))
  4684. (if (> 0 spec)
  4685. (setq spec (* -1 spec)))))
  4686. (if (> spec 0)
  4687. (allout-show-children spec)))
  4688. ((listp spec)
  4689. ;(let ((got (allout-old-expose-topic (car spec))))
  4690. ; (if (and got (or (not max-pos) (> got max-pos)))
  4691. ; (setq max-pos got)))
  4692. (let ((new-depth (+ (allout-current-depth) 1))
  4693. got)
  4694. (setq max-pos (allout-old-expose-topic (car spec)))
  4695. (setq spec (cdr spec))
  4696. (if (and spec
  4697. (allout-descend-to-depth new-depth)
  4698. (not (allout-hidden-p)))
  4699. (progn (setq got (apply 'allout-old-expose-topic spec))
  4700. (if (and got (or (not max-pos) (> got max-pos)))
  4701. (setq max-pos got)))))))
  4702. (while (and followers
  4703. (progn (if (and max-pos (< (point) max-pos))
  4704. (progn (goto-char max-pos)
  4705. (setq max-pos nil)))
  4706. (end-of-line)
  4707. (allout-next-sibling depth)))
  4708. (allout-old-expose-topic (car followers))
  4709. (setq followers (cdr followers)))
  4710. max-pos))
  4711. ;;;_ > allout-new-exposure '()
  4712. (defmacro allout-new-exposure (&rest spec)
  4713. "Literal frontend for `allout-expose-topic', doesn't evaluate arguments.
  4714. Some arguments that would need to be quoted in `allout-expose-topic'
  4715. need not be quoted in `allout-new-exposure'.
  4716. Cursor is left at start position.
  4717. Use this instead of obsolete `allout-exposure'.
  4718. Examples:
  4719. \(allout-new-exposure (-1 () () () 1) 0)
  4720. Close current topic at current level so only the immediate
  4721. subtopics are shown, except also show the children of the
  4722. third subtopic; and close the next topic at the current level.
  4723. \(allout-new-exposure : -1 0)
  4724. Close all topics at current level to expose only their
  4725. immediate children, except for the last topic at the current
  4726. level, in which even its immediate children are hidden.
  4727. \(allout-new-exposure -2 : -1 *)
  4728. Expose children and grandchildren of first topic at current
  4729. level, and expose children of subsequent topics at current
  4730. level *except* for the last, which should be opened completely."
  4731. `(save-excursion
  4732. (if (not (or (allout-goto-prefix-doublechecked)
  4733. (allout-next-heading)))
  4734. (error "allout-new-exposure: Can't find any outline topics"))
  4735. (allout-expose-topic ',spec)))
  4736. ;;;_ #7 Systematic outline presentation -- copying, printing, flattening
  4737. ;;;_ - Mapping and processing of topics
  4738. ;;;_ ( See also Subtree Charting, in Navigation code.)
  4739. ;;;_ > allout-stringify-flat-index (flat-index)
  4740. (defun allout-stringify-flat-index (flat-index &optional context)
  4741. "Convert list representing section/subsection/... to document string.
  4742. Optional arg CONTEXT indicates interior levels to include."
  4743. (let ((delim ".")
  4744. result
  4745. numstr
  4746. (context-depth (or (and context 2) 1)))
  4747. ;; Take care of the explicit context:
  4748. (while (> context-depth 0)
  4749. (setq numstr (int-to-string (car flat-index))
  4750. flat-index (cdr flat-index)
  4751. result (if flat-index
  4752. (cons delim (cons numstr result))
  4753. (cons numstr result))
  4754. context-depth (if flat-index (1- context-depth) 0)))
  4755. (setq delim " ")
  4756. ;; Take care of the indentation:
  4757. (if flat-index
  4758. (progn
  4759. (while flat-index
  4760. (setq result
  4761. (cons delim
  4762. (cons (make-string
  4763. (1+ (truncate (if (zerop (car flat-index))
  4764. 1
  4765. (log (car flat-index) 10))))
  4766. ? )
  4767. result)))
  4768. (setq flat-index (cdr flat-index)))
  4769. ;; Dispose of single extra delim:
  4770. (setq result (cdr result))))
  4771. (apply 'concat result)))
  4772. ;;;_ > allout-stringify-flat-index-plain (flat-index)
  4773. (defun allout-stringify-flat-index-plain (flat-index)
  4774. "Convert list representing section/subsection/... to document string."
  4775. (let ((delim ".")
  4776. result)
  4777. (while flat-index
  4778. (setq result (cons (int-to-string (car flat-index))
  4779. (if result
  4780. (cons delim result))))
  4781. (setq flat-index (cdr flat-index)))
  4782. (apply 'concat result)))
  4783. ;;;_ > allout-stringify-flat-index-indented (flat-index)
  4784. (defun allout-stringify-flat-index-indented (flat-index)
  4785. "Convert list representing section/subsection/... to document string."
  4786. (let ((delim ".")
  4787. result
  4788. numstr)
  4789. ;; Take care of the explicit context:
  4790. (setq numstr (int-to-string (car flat-index))
  4791. flat-index (cdr flat-index)
  4792. result (if flat-index
  4793. (cons delim (cons numstr result))
  4794. (cons numstr result)))
  4795. (setq delim " ")
  4796. ;; Take care of the indentation:
  4797. (if flat-index
  4798. (progn
  4799. (while flat-index
  4800. (setq result
  4801. (cons delim
  4802. (cons (make-string
  4803. (1+ (truncate (if (zerop (car flat-index))
  4804. 1
  4805. (log (car flat-index) 10))))
  4806. ? )
  4807. result)))
  4808. (setq flat-index (cdr flat-index)))
  4809. ;; Dispose of single extra delim:
  4810. (setq result (cdr result))))
  4811. (apply 'concat result)))
  4812. ;;;_ > allout-listify-exposed (&optional start end format)
  4813. (defun allout-listify-exposed (&optional start end format)
  4814. "Produce a list representing exposed topics in current region.
  4815. This list can then be used by `allout-process-exposed' to manipulate
  4816. the subject region.
  4817. Optional START and END indicate bounds of region.
  4818. Optional arg, FORMAT, designates an alternate presentation form for
  4819. the prefix:
  4820. list -- Present prefix as numeric section.subsection..., starting with
  4821. section indicated by the list, innermost nesting first.
  4822. `indent' (symbol) -- Convert header prefixes to all white space,
  4823. except for distinctive bullets.
  4824. The elements of the list produced are lists that represents a topic
  4825. header and body. The elements of that list are:
  4826. - a number representing the depth of the topic,
  4827. - a string representing the header-prefix, including trailing whitespace and
  4828. bullet.
  4829. - a string representing the bullet character,
  4830. - and a series of strings, each containing one line of the exposed
  4831. portion of the topic entry."
  4832. (interactive "r")
  4833. (save-excursion
  4834. (let*
  4835. ((inhibit-field-text-motion t)
  4836. ;; state vars:
  4837. strings prefix result depth new-depth out gone-out bullet beg
  4838. next done)
  4839. (goto-char start)
  4840. (beginning-of-line)
  4841. ;; Goto initial topic, and register preceding stuff, if any:
  4842. (if (> (allout-goto-prefix-doublechecked) start)
  4843. ;; First topic follows beginning point -- register preliminary stuff:
  4844. (setq result
  4845. (list (list 0 "" nil
  4846. (buffer-substring-no-properties start
  4847. (1- (point)))))))
  4848. (while (and (not done)
  4849. (not (eobp)) ; Loop until we've covered the region.
  4850. (not (> (point) end)))
  4851. (setq depth allout-recent-depth ; Current topics depth,
  4852. bullet (allout-recent-bullet) ; ... bullet,
  4853. prefix (allout-recent-prefix)
  4854. beg (progn (allout-end-of-prefix t) (point))) ; and beginning.
  4855. (setq done ; The boundary for the current topic:
  4856. (not (allout-next-visible-heading 1)))
  4857. (setq new-depth allout-recent-depth)
  4858. (setq gone-out out
  4859. out (< new-depth depth))
  4860. (beginning-of-line)
  4861. (setq next (point))
  4862. (goto-char beg)
  4863. (setq strings nil)
  4864. (while (> next (point)) ; Get all the exposed text in
  4865. (setq strings
  4866. (cons (buffer-substring-no-properties
  4867. beg
  4868. ;To hidden text or end of line:
  4869. (progn
  4870. (end-of-line)
  4871. (allout-back-to-visible-text)))
  4872. strings))
  4873. (when (< (point) next) ; Resume from after hid text, if any.
  4874. (line-move 1)
  4875. (beginning-of-line))
  4876. (setq beg (point)))
  4877. ;; Accumulate list for this topic:
  4878. (setq strings (nreverse strings))
  4879. (setq result
  4880. (cons
  4881. (if format
  4882. (let ((special (if (string-match
  4883. (regexp-quote bullet)
  4884. allout-distinctive-bullets-string)
  4885. bullet)))
  4886. (cond ((listp format)
  4887. (list depth
  4888. (if allout-flattened-numbering-abbreviation
  4889. (allout-stringify-flat-index format
  4890. gone-out)
  4891. (allout-stringify-flat-index-plain
  4892. format))
  4893. strings
  4894. special))
  4895. ((eq format 'indent)
  4896. (if special
  4897. (list depth
  4898. (concat (make-string (1+ depth) ? )
  4899. (substring prefix -1))
  4900. strings)
  4901. (list depth
  4902. (make-string depth ? )
  4903. strings)))
  4904. (t (error "allout-listify-exposed: %s %s"
  4905. "invalid format" format))))
  4906. (list depth prefix strings))
  4907. result))
  4908. ;; Reassess format, if any:
  4909. (if (and format (listp format))
  4910. (cond ((= new-depth depth)
  4911. (setq format (cons (1+ (car format))
  4912. (cdr format))))
  4913. ((> new-depth depth) ; descending -- assume by 1:
  4914. (setq format (cons 1 format)))
  4915. (t
  4916. ; Pop the residue:
  4917. (while (< new-depth depth)
  4918. (setq format (cdr format))
  4919. (setq depth (1- depth)))
  4920. ; And increment the current one:
  4921. (setq format
  4922. (cons (1+ (or (car format)
  4923. -1))
  4924. (cdr format)))))))
  4925. ;; Put the list with first at front, to last at back:
  4926. (nreverse result))))
  4927. ;;;_ > allout-region-active-p ()
  4928. (defmacro allout-region-active-p ()
  4929. (cond ((fboundp 'use-region-p) '(use-region-p))
  4930. ((fboundp 'region-active-p) '(region-active-p))
  4931. (t 'mark-active)))
  4932. ;;_ > allout-process-exposed (&optional func from to frombuf
  4933. ;;; tobuf format)
  4934. (defun allout-process-exposed (&optional func from to frombuf tobuf
  4935. format _start-num)
  4936. "Map function on exposed parts of current topic; results to another buffer.
  4937. All args are options; default values itemized below.
  4938. Apply FUNCTION to exposed portions FROM position TO position in buffer
  4939. FROMBUF to buffer TOBUF. Sixth optional arg, FORMAT, designates an
  4940. alternate presentation form:
  4941. `flat' -- Present prefix as numeric section.subsection..., starting with
  4942. section indicated by the START-NUM, innermost nesting first.
  4943. X`flat-indented' -- Prefix is like `flat' for first topic at each
  4944. X level, but subsequent topics have only leaf topic
  4945. X number, padded with blanks to line up with first.
  4946. `indent' (symbol) -- Convert header prefixes to all white space,
  4947. except for distinctive bullets.
  4948. Defaults:
  4949. FUNCTION: `allout-insert-listified'
  4950. FROM: region start, if region active, else start of buffer
  4951. TO: region end, if region active, else end of buffer
  4952. FROMBUF: current buffer
  4953. TOBUF: buffer name derived: \"*current-buffer-name exposed*\"
  4954. FORMAT: nil"
  4955. ; Resolve arguments,
  4956. ; defaulting if necessary:
  4957. (if (not func) (setq func 'allout-insert-listified))
  4958. (if (not (and from to))
  4959. (if (allout-region-active-p)
  4960. (setq from (region-beginning) to (region-end))
  4961. (setq from (point-min) to (point-max))))
  4962. (if frombuf
  4963. (if (not (bufferp frombuf))
  4964. ;; Specified but not a buffer -- get it:
  4965. (let ((got (get-buffer frombuf)))
  4966. (if (not got)
  4967. (error "allout-process-exposed: source buffer %s not found."
  4968. frombuf)
  4969. (setq frombuf got))))
  4970. ;; not specified -- default it:
  4971. (setq frombuf (current-buffer)))
  4972. (if tobuf
  4973. (if (not (bufferp tobuf))
  4974. (setq tobuf (get-buffer-create tobuf)))
  4975. ;; not specified -- default it:
  4976. (setq tobuf (concat "*" (buffer-name frombuf) " exposed*")))
  4977. (if (listp format)
  4978. (nreverse format))
  4979. (let* ((listified
  4980. (progn (set-buffer frombuf)
  4981. (allout-listify-exposed from to format))))
  4982. (set-buffer tobuf)
  4983. (mapc func listified)
  4984. (pop-to-buffer tobuf)))
  4985. ;;;_ - Copy exposed
  4986. ;;;_ > allout-insert-listified (listified)
  4987. (defun allout-insert-listified (listified)
  4988. "Insert contents of listified outline portion in current buffer.
  4989. LISTIFIED is a list representing each topic header and body:
  4990. `(depth prefix text)'
  4991. or `(depth prefix text bullet-plus)'
  4992. If `bullet-plus' is specified, it is inserted just after the entire prefix."
  4993. (setq listified (cdr listified))
  4994. (let ((prefix (prog1
  4995. (car listified)
  4996. (setq listified (cdr listified))))
  4997. (text (prog1
  4998. (car listified)
  4999. (setq listified (cdr listified))))
  5000. (bullet-plus (car listified)))
  5001. (insert prefix)
  5002. (if bullet-plus (insert (concat " " bullet-plus)))
  5003. (while text
  5004. (insert (car text))
  5005. (if (setq text (cdr text))
  5006. (insert "\n")))
  5007. (insert "\n")))
  5008. ;;;_ > allout-copy-exposed-to-buffer (&optional arg tobuf format)
  5009. (defun allout-copy-exposed-to-buffer (&optional arg tobuf format)
  5010. "Duplicate exposed portions of current outline to another buffer.
  5011. Other buffer has current buffers name with \" exposed\" appended to it.
  5012. With repeat count, copy the exposed parts of only the current topic.
  5013. Optional second arg TOBUF is target buffer name.
  5014. Optional third arg FORMAT, if non-nil, symbolically designates an
  5015. alternate presentation format for the outline:
  5016. `flat' - Convert topic header prefixes to numeric
  5017. section.subsection... identifiers.
  5018. `indent' - Convert header prefixes to all white space, except for
  5019. distinctive bullets.
  5020. `indent-flat' - The best of both - only the first of each level has
  5021. the full path, the rest have only the section number
  5022. of the leaf, preceded by the right amount of indentation."
  5023. (interactive "P")
  5024. (if (not tobuf)
  5025. (setq tobuf (get-buffer-create (concat "*" (buffer-name) " exposed*"))))
  5026. (let* ((start-pt (point))
  5027. (beg (if arg (allout-back-to-current-heading) (point-min)))
  5028. (end (if arg (allout-end-of-current-subtree) (point-max)))
  5029. (buf (current-buffer))
  5030. (start-list ()))
  5031. (if (eq format 'flat)
  5032. (setq format (if arg (save-excursion
  5033. (goto-char beg)
  5034. (allout-topic-flat-index))
  5035. '(1))))
  5036. (with-current-buffer tobuf (erase-buffer))
  5037. (allout-process-exposed 'allout-insert-listified
  5038. beg
  5039. end
  5040. (current-buffer)
  5041. tobuf
  5042. format start-list)
  5043. (goto-char (point-min))
  5044. (pop-to-buffer buf)
  5045. (goto-char start-pt)))
  5046. ;;;_ > allout-flatten-exposed-to-buffer (&optional arg tobuf)
  5047. (defun allout-flatten-exposed-to-buffer (&optional arg tobuf)
  5048. "Present numeric outline of outline's exposed portions in another buffer.
  5049. The resulting outline is not compatible with outline mode -- use
  5050. `allout-copy-exposed-to-buffer' if you want that.
  5051. Use `allout-indented-exposed-to-buffer' for indented presentation.
  5052. With repeat count, copy the exposed portions of only current topic.
  5053. Other buffer has current buffer's name with \" exposed\" appended to
  5054. it, unless optional second arg TOBUF is specified, in which case it is
  5055. used verbatim."
  5056. (interactive "P")
  5057. (allout-copy-exposed-to-buffer arg tobuf 'flat))
  5058. ;;;_ > allout-indented-exposed-to-buffer (&optional arg tobuf)
  5059. (defun allout-indented-exposed-to-buffer (&optional arg tobuf)
  5060. "Present indented outline of outline's exposed portions in another buffer.
  5061. The resulting outline is not compatible with outline mode -- use
  5062. `allout-copy-exposed-to-buffer' if you want that.
  5063. Use `allout-flatten-exposed-to-buffer' for numeric sectional presentation.
  5064. With repeat count, copy the exposed portions of only current topic.
  5065. Other buffer has current buffer's name with \" exposed\" appended to
  5066. it, unless optional second arg TOBUF is specified, in which case it is
  5067. used verbatim."
  5068. (interactive "P")
  5069. (allout-copy-exposed-to-buffer arg tobuf 'indent))
  5070. ;;;_ - LaTeX formatting
  5071. ;;;_ > allout-latex-verb-quote (string &optional flow)
  5072. (defun allout-latex-verb-quote (string &optional _flow)
  5073. "Return copy of STRING for literal reproduction across LaTeX processing.
  5074. Expresses the original characters (including carriage returns) of the
  5075. string across LaTeX processing."
  5076. (mapconcat (function
  5077. (lambda (char)
  5078. (cond ((memq char '(?\\ ?$ ?% ?# ?& ?{ ?} ?_ ?^ ?- ?*))
  5079. (concat "\\char" (number-to-string char) "{}"))
  5080. ((= char ?\n) "\\\\")
  5081. (t (char-to-string char)))))
  5082. string
  5083. ""))
  5084. ;;;_ > allout-latex-verbatim-quote-curr-line ()
  5085. (defun allout-latex-verbatim-quote-curr-line ()
  5086. "Express line for exact (literal) representation across LaTeX processing.
  5087. Adjust line contents so it is unaltered (from the original line)
  5088. across LaTeX processing, within the context of a `verbatim'
  5089. environment. Leaves point at the end of the line."
  5090. (let ((inhibit-field-text-motion t))
  5091. (beginning-of-line)
  5092. (let (;(beg (point))
  5093. (end (point-at-eol)))
  5094. (save-match-data
  5095. (while (re-search-forward "\\\\"
  5096. ;;"\\\\\\|\\{\\|\\}\\|\\_\\|\\$\\|\\\"\\|\\&\\|\\^\\|\\-\\|\\*\\|#"
  5097. end ; bounded by end-of-line
  5098. 1) ; no matches, move to end & return nil
  5099. (goto-char (match-beginning 2))
  5100. (insert "\\")
  5101. (setq end (1+ end))
  5102. (goto-char (1+ (match-end 2))))))))
  5103. ;;;_ > allout-insert-latex-header (buffer)
  5104. (defun allout-insert-latex-header (buffer)
  5105. "Insert initial LaTeX commands at point in BUFFER."
  5106. ;; Much of this is being derived from the stuff in appendix of E in
  5107. ;; the TeXBook, pg 421.
  5108. (set-buffer buffer)
  5109. (let ((doc-style (format "\n\\documentstyle{%s}\n"
  5110. "report"))
  5111. (page-numbering (if allout-number-pages
  5112. "\\pagestyle{empty}\n"
  5113. ""))
  5114. (titlecmd (format "\\newcommand{\\titlecmd}[1]{{%s #1}}\n"
  5115. allout-title-style))
  5116. (labelcmd (format "\\newcommand{\\labelcmd}[1]{{%s #1}}\n"
  5117. allout-label-style))
  5118. (headlinecmd (format "\\newcommand{\\headlinecmd}[1]{{%s #1}}\n"
  5119. allout-head-line-style))
  5120. (bodylinecmd (format "\\newcommand{\\bodylinecmd}[1]{{%s #1}}\n"
  5121. allout-body-line-style))
  5122. (setlength (format "%s%s%s%s"
  5123. "\\newlength{\\stepsize}\n"
  5124. "\\setlength{\\stepsize}{"
  5125. allout-indent
  5126. "}\n"))
  5127. (oneheadline (format "%s%s%s%s%s%s%s"
  5128. "\\newcommand{\\OneHeadLine}[3]{%\n"
  5129. "\\noindent%\n"
  5130. "\\hspace*{#2\\stepsize}%\n"
  5131. "\\labelcmd{#1}\\hspace*{.2cm}"
  5132. "\\headlinecmd{#3}\\\\["
  5133. allout-line-skip
  5134. "]\n}\n"))
  5135. (onebodyline (format "%s%s%s%s%s%s"
  5136. "\\newcommand{\\OneBodyLine}[2]{%\n"
  5137. "\\noindent%\n"
  5138. "\\hspace*{#1\\stepsize}%\n"
  5139. "\\bodylinecmd{#2}\\\\["
  5140. allout-line-skip
  5141. "]\n}\n"))
  5142. (begindoc "\\begin{document}\n\\begin{center}\n")
  5143. (title (format "%s%s%s%s"
  5144. "\\titlecmd{"
  5145. (allout-latex-verb-quote (if allout-title
  5146. (condition-case nil
  5147. (eval allout-title)
  5148. (error "<unnamed buffer>"))
  5149. "Unnamed Outline"))
  5150. "}\n"
  5151. "\\end{center}\n\n"))
  5152. (hsize "\\hsize = 7.5 true in\n")
  5153. (hoffset "\\hoffset = -1.5 true in\n")
  5154. (vspace "\\vspace{.1cm}\n\n"))
  5155. (insert (concat doc-style
  5156. page-numbering
  5157. titlecmd
  5158. labelcmd
  5159. headlinecmd
  5160. bodylinecmd
  5161. setlength
  5162. oneheadline
  5163. onebodyline
  5164. begindoc
  5165. title
  5166. hsize
  5167. hoffset
  5168. vspace)
  5169. )))
  5170. ;;;_ > allout-insert-latex-trailer (buffer)
  5171. (defun allout-insert-latex-trailer (buffer)
  5172. "Insert concluding LaTeX commands at point in BUFFER."
  5173. (set-buffer buffer)
  5174. (insert "\n\\end{document}\n"))
  5175. ;;;_ > allout-latexify-one-item (depth prefix bullet text)
  5176. (defun allout-latexify-one-item (depth _prefix bullet text)
  5177. "Insert LaTeX commands for formatting one outline item.
  5178. Args are the topics numeric DEPTH, the header PREFIX lead string, the
  5179. BULLET string, and a list of TEXT strings for the body."
  5180. (let* ((head-line (if text (car text)))
  5181. (body-lines (cdr text))
  5182. (curr-line)
  5183. body-content bop)
  5184. ; Do the head line:
  5185. (insert (concat "\\OneHeadLine{\\verb\1 "
  5186. (allout-latex-verb-quote bullet)
  5187. "\1}{"
  5188. depth
  5189. "}{\\verb\1 "
  5190. (if head-line
  5191. (allout-latex-verb-quote head-line)
  5192. "")
  5193. "\1}\n"))
  5194. (if (not body-lines)
  5195. nil
  5196. ;;(insert "\\beginlines\n")
  5197. (insert "\\begin{verbatim}\n")
  5198. (while body-lines
  5199. (setq curr-line (car body-lines))
  5200. (if (and (not body-content)
  5201. (not (string-match "^\\s-*$" curr-line)))
  5202. (setq body-content t))
  5203. ; Mangle any occurrences of
  5204. ; "\end{verbatim}" in text,
  5205. ; it's special:
  5206. (if (and body-content
  5207. (setq bop (string-match "\\end{verbatim}" curr-line)))
  5208. (setq curr-line (concat (substring curr-line 0 bop)
  5209. ">"
  5210. (substring curr-line bop))))
  5211. ;;(insert "|" (car body-lines) "|")
  5212. (insert curr-line)
  5213. (allout-latex-verbatim-quote-curr-line)
  5214. (insert "\n")
  5215. (setq body-lines (cdr body-lines)))
  5216. (if body-content
  5217. (setq body-content nil)
  5218. (forward-char -1)
  5219. (insert "\\ ")
  5220. (forward-char 1))
  5221. ;;(insert "\\endlines\n")
  5222. (insert "\\end{verbatim}\n")
  5223. )))
  5224. ;;;_ > allout-latexify-exposed (arg &optional tobuf)
  5225. (defun allout-latexify-exposed (arg &optional tobuf)
  5226. "Format current topics exposed portions to TOBUF for LaTeX processing.
  5227. TOBUF defaults to a buffer named the same as the current buffer, but
  5228. with \"*\" prepended and \" latex-formed*\" appended.
  5229. With repeat count, copy the exposed portions of entire buffer."
  5230. (interactive "P")
  5231. (if (not tobuf)
  5232. (setq tobuf
  5233. (get-buffer-create (concat "*" (buffer-name) " latexified*"))))
  5234. (let* ((start-pt (point))
  5235. (beg (if arg (point-min) (allout-back-to-current-heading)))
  5236. (end (if arg (point-max) (allout-end-of-current-subtree)))
  5237. (buf (current-buffer)))
  5238. (set-buffer tobuf)
  5239. (erase-buffer)
  5240. (allout-insert-latex-header tobuf)
  5241. (goto-char (point-max))
  5242. (allout-process-exposed 'allout-latexify-one-item
  5243. beg
  5244. end
  5245. buf
  5246. tobuf)
  5247. (goto-char (point-max))
  5248. (allout-insert-latex-trailer tobuf)
  5249. (goto-char (point-min))
  5250. (pop-to-buffer buf)
  5251. (goto-char start-pt)))
  5252. ;;;_ #8 Encryption
  5253. ;;;_ > allout-toggle-current-subtree-encryption (&optional keymode-cue)
  5254. (defun allout-toggle-current-subtree-encryption (&optional keymode-cue)
  5255. "Encrypt clear or decrypt encoded topic text.
  5256. Allout uses Emacs `epg' library to perform encryption. Symmetric
  5257. and keypair encryption are supported. All encryption is ascii
  5258. armored.
  5259. Entry encryption defaults to symmetric key mode unless keypair
  5260. recipients are associated with the file (see
  5261. `epa-file-encrypt-to') or the function is invoked with a
  5262. \(KEYMODE-CUE) universal argument greater than 1.
  5263. When encrypting, KEYMODE-CUE universal argument greater than 1
  5264. causes prompting for recipients for public-key keypair
  5265. encryption. Selecting no recipients results in symmetric key
  5266. encryption.
  5267. Further, encrypting with a KEYMODE-CUE universal argument greater
  5268. than 4 - eg, preceded by a doubled Ctrl-U - causes association of
  5269. the specified recipients with the file, replacing those currently
  5270. associated with it. This can be used to dissociate any
  5271. recipients with the file, by selecting no recipients in the
  5272. dialog.
  5273. Encrypted topic's bullets are set to a `~' to signal that the
  5274. contents of the topic (body and subtopics, but not heading) is
  5275. pending encryption or encrypted. `*' asterisk immediately after
  5276. the bullet signals that the body is encrypted, its absence means
  5277. the topic is meant to be encrypted but is not currently. When a
  5278. file with topics pending encryption is saved, topics pending
  5279. encryption are encrypted. See `allout-encrypt-unencrypted-on-saves'
  5280. for auto-encryption specifics.
  5281. *NOTE WELL* that automatic encryption that happens during saves will
  5282. default to symmetric encryption -- you must deliberately (re)encrypt key-pair
  5283. encrypted topics if you want them to continue to use the key-pair cipher.
  5284. Level-one topics, with prefix consisting solely of an `*' asterisk, cannot be
  5285. encrypted. If you want to encrypt the contents of a top-level topic, use
  5286. \\[allout-shift-in] to increase its depth."
  5287. (interactive "P")
  5288. (save-excursion
  5289. (allout-back-to-current-heading)
  5290. (allout-toggle-subtree-encryption keymode-cue)))
  5291. ;;;_ > allout-toggle-subtree-encryption (&optional keymode-cue)
  5292. (defun allout-toggle-subtree-encryption (&optional keymode-cue)
  5293. "Encrypt clear text or decrypt encoded topic contents (body and subtopics.)
  5294. Entry encryption defaults to symmetric key mode unless keypair
  5295. recipients are associated with the file (see
  5296. `epa-file-encrypt-to') or the function is invoked with a
  5297. \(KEYMODE-CUE) universal argument greater than 1.
  5298. When encrypting, KEYMODE-CUE universal argument greater than 1
  5299. causes prompting for recipients for public-key keypair
  5300. encryption. Selecting no recipients results in symmetric key
  5301. encryption.
  5302. Further, encrypting with a KEYMODE-CUE universal argument greater
  5303. than 4 - eg, preceded by a doubled Ctrl-U - causes association of
  5304. the specified recipients with the file, replacing those currently
  5305. associated with it. This can be used to dissociate any
  5306. recipients with the file, by selecting no recipients in the
  5307. dialog.
  5308. Encryption and decryption uses the Emacs `epg' library.
  5309. Encrypted text will be ascii-armored.
  5310. See `allout-toggle-current-subtree-encryption' for more details."
  5311. (interactive "P")
  5312. (save-excursion
  5313. (allout-end-of-prefix t)
  5314. (if (= allout-recent-depth 1)
  5315. (error (concat "Cannot encrypt or decrypt level 1 topics -"
  5316. " shift it in to make it encryptable")))
  5317. (let* ((allout-buffer (current-buffer))
  5318. ;; for use with allout-auto-save-temporarily-disabled, if necessary:
  5319. (was-buffer-saved-size buffer-saved-size)
  5320. ;; Assess location:
  5321. (bullet-pos allout-recent-prefix-beginning)
  5322. (after-bullet-pos (point))
  5323. (was-encrypted
  5324. (progn (if (= (point-max) after-bullet-pos)
  5325. (error "no body to encrypt"))
  5326. (allout-encrypted-topic-p)))
  5327. (was-collapsed (if (not (search-forward "\n" nil t))
  5328. nil
  5329. (backward-char 1)
  5330. (allout-hidden-p)))
  5331. (subtree-beg (1+ (point)))
  5332. (subtree-end (allout-end-of-subtree))
  5333. (subject-text (buffer-substring-no-properties subtree-beg
  5334. subtree-end))
  5335. (subtree-end-char (char-after (1- subtree-end)))
  5336. (subtree-trailing-char (char-after subtree-end))
  5337. ;; kluge -- result-text needs to be nil, but we also want to
  5338. ;; check for the error condition
  5339. (result-text (if (or (string= "" subject-text)
  5340. (string= "\n" subject-text))
  5341. (error "No topic contents to %scrypt"
  5342. (if was-encrypted "de" "en"))
  5343. nil))
  5344. ;; Assess key parameters:
  5345. (was-coding-system buffer-file-coding-system))
  5346. (when (not was-encrypted)
  5347. ;; ensure that non-ascii chars pending encryption are noticed before
  5348. ;; they're encrypted, so the coding system is set to accommodate
  5349. ;; them.
  5350. (setq buffer-file-coding-system
  5351. (allout-select-safe-coding-system subtree-beg subtree-end))
  5352. ;; if the coding system for the text being encrypted is different
  5353. ;; than that prevailing, then there a real risk that the coding
  5354. ;; system can't be noticed by emacs when the file is visited. to
  5355. ;; mitigate that, offer to preserve the coding system using a file
  5356. ;; local variable.
  5357. (if (and (not (equal buffer-file-coding-system
  5358. was-coding-system))
  5359. (yes-or-no-p
  5360. (format (concat "Register coding system %s as file local"
  5361. " var? Necessary when only encrypted text"
  5362. " is in that coding system. ")
  5363. buffer-file-coding-system)))
  5364. (allout-adjust-file-variable "buffer-file-coding-system"
  5365. buffer-file-coding-system)))
  5366. (setq result-text
  5367. (allout-encrypt-string subject-text was-encrypted
  5368. (current-buffer) keymode-cue))
  5369. ;; Replace the subtree with the processed product.
  5370. (allout-unprotected
  5371. (progn
  5372. (set-buffer allout-buffer)
  5373. (delete-region subtree-beg subtree-end)
  5374. (insert result-text)
  5375. (if was-collapsed
  5376. (allout-flag-region (1- subtree-beg) (point) t))
  5377. ;; adjust trailing-blank-lines to preserve topic spacing:
  5378. (if (not was-encrypted)
  5379. (if (and (= subtree-end-char ?\n)
  5380. (= subtree-trailing-char ?\n))
  5381. (insert subtree-trailing-char)))
  5382. ;; Ensure that the item has an encrypted-entry bullet:
  5383. (if (not (string= (buffer-substring-no-properties
  5384. (1- after-bullet-pos) after-bullet-pos)
  5385. allout-topic-encryption-bullet))
  5386. (progn (goto-char (1- after-bullet-pos))
  5387. (delete-char 1)
  5388. (insert allout-topic-encryption-bullet)))
  5389. (if was-encrypted
  5390. ;; Remove the is-encrypted bullet qualifier:
  5391. (progn (goto-char after-bullet-pos)
  5392. (delete-char 1))
  5393. ;; Add the is-encrypted bullet qualifier:
  5394. (goto-char after-bullet-pos)
  5395. (insert "*"))))
  5396. ;; adjust buffer's auto-save eligibility:
  5397. (if was-encrypted
  5398. (allout-inhibit-auto-save-info-for-decryption was-buffer-saved-size)
  5399. (allout-maybe-resume-auto-save-info-after-encryption))
  5400. (run-hook-with-args 'allout-structure-added-functions
  5401. bullet-pos subtree-end))))
  5402. (declare-function epg-context-set-passphrase-callback "epg"
  5403. (context passphrase-callback))
  5404. (declare-function epg-list-keys "epg" (context &optional name mode))
  5405. (declare-function epg-decrypt-string "epg" (context cipher))
  5406. (declare-function epg-encrypt-string "epg"
  5407. (context plain recipients &optional sign always-trust))
  5408. (declare-function epg-user-id-string "epg" (user-id) t)
  5409. (declare-function epg-key-user-id-list "epg" (key) t)
  5410. ;;;_ > allout-encrypt-string (text decrypt allout-buffer keymode-cue
  5411. ;;; &optional rejected)
  5412. (defun allout-encrypt-string (text decrypt allout-buffer keymode-cue
  5413. &optional rejected)
  5414. "Encrypt or decrypt message TEXT.
  5415. Returns the resulting string, or nil if the transformation fails.
  5416. If DECRYPT is true (default false), then decrypt instead of encrypt.
  5417. ALLOUT-BUFFER identifies the buffer containing the text.
  5418. Entry encryption defaults to symmetric key mode unless keypair
  5419. recipients are associated with the file (see
  5420. `epa-file-encrypt-to') or the function is invoked with a
  5421. \(KEYMODE-CUE) universal argument greater than 1.
  5422. When encrypting, KEYMODE-CUE universal argument greater than 1
  5423. causes prompting for recipients for public-key keypair
  5424. encryption. Selecting no recipients results in symmetric key
  5425. encryption.
  5426. Further, encrypting with a KEYMODE-CUE universal argument greater
  5427. than 4 - eg, preceded by a doubled Ctrl-U - causes association of
  5428. the specified recipients with the file, replacing those currently
  5429. associated with it. This can be used to dissociate any
  5430. recipients with the file, by selecting no recipients in the
  5431. dialog.
  5432. Optional REJECTED is for internal use, to convey the number of
  5433. rejections due to matches against
  5434. `allout-encryption-ciphertext-rejection-regexps', as limited by
  5435. `allout-encryption-ciphertext-rejection-ceiling'.
  5436. NOTE: A few GnuPG v2 versions improperly preserve incorrect
  5437. symmetric decryption keys, preventing entry of the correct key on
  5438. subsequent decryption attempts until the cache times-out. That
  5439. can take several minutes. (Decryption of other entries is not
  5440. affected.) Upgrade your EasyPG version, if you can, and you can
  5441. deliberately clear your gpg-agent's cache by sending it a `-HUP'
  5442. signal."
  5443. (require 'epg)
  5444. (require 'epa)
  5445. (let* ((epg-context (let* ((context (epg-make-context nil t)))
  5446. (epg-context-set-passphrase-callback
  5447. context #'epa-passphrase-callback-function)
  5448. context))
  5449. (encoding (with-current-buffer allout-buffer
  5450. buffer-file-coding-system))
  5451. (multibyte (with-current-buffer allout-buffer
  5452. enable-multibyte-characters))
  5453. ;; "sanitization" avoids encryption results that are outline structure.
  5454. (sani-regexps 'allout-encryption-plaintext-sanitization-regexps)
  5455. (strip-plaintext-regexps (if (not decrypt)
  5456. (allout-get-configvar-values
  5457. sani-regexps)))
  5458. (rejection-regexps 'allout-encryption-ciphertext-rejection-regexps)
  5459. (reject-ciphertext-regexps (if (not decrypt)
  5460. (allout-get-configvar-values
  5461. rejection-regexps)))
  5462. (rejected (or rejected 0))
  5463. (rejections-left (- allout-encryption-ciphertext-rejection-ceiling
  5464. rejected))
  5465. (keypair-mode (cond (decrypt 'decrypting)
  5466. ((<= (prefix-numeric-value keymode-cue) 1)
  5467. 'default)
  5468. ((<= (prefix-numeric-value keymode-cue) 4)
  5469. 'prompt)
  5470. ((> (prefix-numeric-value keymode-cue) 4)
  5471. 'prompt-save)))
  5472. (keypair-message (concat "Select encryption recipients.\n"
  5473. "Symmetric encryption is done if no"
  5474. " recipients are selected. "))
  5475. (encrypt-to (and (boundp 'epa-file-encrypt-to) epa-file-encrypt-to))
  5476. recipients
  5477. massaged-text
  5478. result-text
  5479. )
  5480. ;; Massage the subject text for encoding and filtering.
  5481. (with-temp-buffer
  5482. (insert text)
  5483. ;; convey the text characteristics of the original buffer:
  5484. (set-buffer-multibyte multibyte)
  5485. (when encoding
  5486. (set-buffer-file-coding-system encoding)
  5487. (if (not decrypt)
  5488. (encode-coding-region (point-min) (point-max) encoding)))
  5489. ;; remove sanitization regexps matches before encrypting:
  5490. (when (and strip-plaintext-regexps (not decrypt))
  5491. (dolist (re strip-plaintext-regexps)
  5492. (let ((re (if (listp re) (car re) re))
  5493. (replacement (if (listp re) (cadr re) "")))
  5494. (goto-char (point-min))
  5495. (save-match-data
  5496. (while (re-search-forward re nil t)
  5497. (replace-match replacement nil nil))))))
  5498. (setq massaged-text (buffer-substring-no-properties (point-min)
  5499. (point-max))))
  5500. ;; determine key mode and, if keypair, recipients:
  5501. (setq recipients
  5502. (case keypair-mode
  5503. (decrypting nil)
  5504. (default (if encrypt-to (epg-list-keys epg-context encrypt-to)))
  5505. ((prompt prompt-save)
  5506. (save-window-excursion
  5507. (epa-select-keys epg-context keypair-message)))))
  5508. (setq result-text
  5509. (if decrypt
  5510. (condition-case err
  5511. (epg-decrypt-string epg-context
  5512. (encode-coding-string massaged-text
  5513. (or encoding 'utf-8)))
  5514. (epg-error
  5515. (signal 'egp-error
  5516. (cons (concat (cadr err) " - gpg version problem?")
  5517. (cddr err)))))
  5518. (replace-regexp-in-string "\n$" ""
  5519. (epg-encrypt-string epg-context
  5520. (encode-coding-string massaged-text
  5521. (or encoding 'utf-8))
  5522. recipients))))
  5523. ;; validate result -- non-empty
  5524. (if (not result-text)
  5525. (error "%scryption failed." (if decrypt "De" "En")))
  5526. (when (eq keypair-mode 'prompt-save)
  5527. ;; set epa-file-encrypt-to in the buffer:
  5528. (setq epa-file-encrypt-to (mapcar (lambda (key)
  5529. (epg-user-id-string
  5530. (car (epg-key-user-id-list key))))
  5531. recipients))
  5532. ;; change the file variable:
  5533. (allout-adjust-file-variable "epa-file-encrypt-to" epa-file-encrypt-to))
  5534. (cond
  5535. ;; Retry (within limit) if ciphertext contains rejections:
  5536. ((and (not decrypt)
  5537. ;; Check for disqualification of this ciphertext:
  5538. (let ((regexps reject-ciphertext-regexps)
  5539. reject-it)
  5540. (while (and regexps (not reject-it))
  5541. (setq reject-it (string-match (car regexps) result-text))
  5542. (pop regexps))
  5543. reject-it))
  5544. (setq rejections-left (1- rejections-left))
  5545. (if (<= rejections-left 0)
  5546. (error (concat "Ciphertext rejected too many times"
  5547. " (%s), per `%s'")
  5548. allout-encryption-ciphertext-rejection-ceiling
  5549. 'allout-encryption-ciphertext-rejection-regexps)
  5550. ;; try again (gpg-agent may have the key cached):
  5551. (allout-encrypt-string text decrypt allout-buffer keypair-mode
  5552. (1+ rejected))))
  5553. ;; Barf if encryption yields extraordinary control chars:
  5554. ((and (not decrypt)
  5555. (string-match "[\C-a\C-k\C-o-\C-z\C-@]"
  5556. result-text))
  5557. (error (concat "Encryption produced non-armored text, which"
  5558. "conflicts with allout mode -- reconfigure!")))
  5559. (t result-text))))
  5560. ;;;_ > allout-inhibit-auto-save-info-for-decryption
  5561. (defun allout-inhibit-auto-save-info-for-decryption (was-buffer-saved-size)
  5562. "Temporarily prevent auto-saves in this buffer when an item is decrypted.
  5563. WAS-BUFFER-SAVED-SIZE is the value of `buffer-saved-size' *before*
  5564. the decryption."
  5565. (when (not (or (= buffer-saved-size -1) (= was-buffer-saved-size -1)))
  5566. (setq allout-auto-save-temporarily-disabled was-buffer-saved-size
  5567. buffer-saved-size -1)))
  5568. ;;;_ > allout-maybe-resume-auto-save-info-after-encryption ()
  5569. (defun allout-maybe-resume-auto-save-info-after-encryption ()
  5570. "Restore auto-save info, *if* there are no topics pending encryption."
  5571. (when (and allout-auto-save-temporarily-disabled
  5572. (= buffer-saved-size -1)
  5573. (save-excursion
  5574. (save-restriction
  5575. (widen)
  5576. (goto-char (point-min))
  5577. (not (allout-next-topic-pending-encryption)))))
  5578. (setq buffer-saved-size allout-auto-save-temporarily-disabled
  5579. allout-auto-save-temporarily-disabled nil)))
  5580. ;;;_ > allout-encrypted-topic-p ()
  5581. (defun allout-encrypted-topic-p ()
  5582. "True if the current topic is encryptable and encrypted."
  5583. (save-excursion
  5584. (allout-end-of-prefix t)
  5585. (and (string= (buffer-substring-no-properties (1- (point)) (point))
  5586. allout-topic-encryption-bullet)
  5587. (save-match-data (looking-at "\\*")))
  5588. )
  5589. )
  5590. ;;;_ > allout-next-topic-pending-encryption ()
  5591. (defun allout-next-topic-pending-encryption ()
  5592. "Return the point of the next topic pending encryption, or nil if none.
  5593. Such a topic has the `allout-topic-encryption-bullet' without an
  5594. immediately following `*' that would mark the topic as being encrypted.
  5595. It must also have content."
  5596. (let (done got content-beg)
  5597. (save-match-data
  5598. (while (not done)
  5599. (if (not (re-search-forward
  5600. (format "\\(\\`\\|\n\\)%s *%s[^*]"
  5601. (regexp-quote allout-header-prefix)
  5602. (regexp-quote allout-topic-encryption-bullet))
  5603. nil t))
  5604. (setq got nil
  5605. done t)
  5606. (goto-char (setq got (match-beginning 0)))
  5607. (if (save-match-data (looking-at "\n"))
  5608. (forward-char 1))
  5609. (setq got (point)))
  5610. (cond ((not got)
  5611. (setq done t))
  5612. ((not (search-forward "\n"))
  5613. (setq got nil
  5614. done t))
  5615. ((eobp)
  5616. (setq got nil
  5617. done t))
  5618. (t
  5619. (setq content-beg (point))
  5620. (backward-char 1)
  5621. (allout-end-of-subtree)
  5622. (if (<= (point) content-beg)
  5623. ;; Continue looking
  5624. (setq got nil)
  5625. ;; Got it!
  5626. (setq done t)))
  5627. )
  5628. )
  5629. (if got
  5630. (goto-char got))
  5631. )
  5632. )
  5633. )
  5634. ;;;_ > allout-encrypt-decrypted ()
  5635. (defun allout-encrypt-decrypted ()
  5636. "Encrypt topics pending encryption except those containing exemption point.
  5637. If a topic that is currently being edited was encrypted, we return a list
  5638. containing the location of the topic and the location of the cursor just
  5639. before the topic was encrypted. This can be used, eg, to decrypt the topic
  5640. and exactly resituate the cursor if this is being done as part of a file
  5641. save. See `allout-encrypt-unencrypted-on-saves' for more info."
  5642. (interactive "p")
  5643. (save-match-data
  5644. (save-excursion
  5645. (let* ((current-mark (point-marker))
  5646. (current-mark-position (marker-position current-mark))
  5647. was-modified
  5648. bo-subtree
  5649. editing-topic editing-point)
  5650. (goto-char (point-min))
  5651. (while (allout-next-topic-pending-encryption)
  5652. (setq was-modified (buffer-modified-p))
  5653. (when (save-excursion
  5654. (and (boundp 'allout-encrypt-unencrypted-on-saves)
  5655. allout-encrypt-unencrypted-on-saves
  5656. (setq bo-subtree (re-search-forward "$"))
  5657. (not (allout-hidden-p))
  5658. (>= current-mark (point))
  5659. (allout-end-of-current-subtree)
  5660. (<= current-mark (point))))
  5661. (setq editing-topic (point)
  5662. ;; we had to wait for this 'til now so prior topics are
  5663. ;; encrypted, any relevant text shifts are in place:
  5664. editing-point (- current-mark-position
  5665. (count-trailing-whitespace-region
  5666. bo-subtree current-mark-position))))
  5667. (allout-toggle-subtree-encryption)
  5668. (if (not was-modified)
  5669. (set-buffer-modified-p nil))
  5670. )
  5671. (if (not was-modified)
  5672. (set-buffer-modified-p nil))
  5673. (if editing-topic (list editing-topic editing-point))
  5674. )
  5675. )
  5676. )
  5677. )
  5678. ;;;_ #9 miscellaneous
  5679. ;;;_ : Mode:
  5680. ;;;_ > outlineify-sticky ()
  5681. ;; outlinify-sticky is correct spelling; provide this alias for sticklers:
  5682. ;;;###autoload
  5683. (defalias 'outlinify-sticky 'outlineify-sticky)
  5684. ;;;###autoload
  5685. (defun outlineify-sticky (&optional _arg)
  5686. "Activate outline mode and establish file var so it is started subsequently.
  5687. See `allout-layout' and customization of `allout-auto-activation'
  5688. for details on preparing Emacs for automatic allout activation."
  5689. (interactive "P")
  5690. (if (allout-mode-p) (allout-mode)) ; deactivate so we can re-activate...
  5691. (allout-mode)
  5692. (save-excursion
  5693. (goto-char (point-min))
  5694. (if (allout-goto-prefix)
  5695. t
  5696. (allout-open-topic 2)
  5697. (insert (substitute-command-keys
  5698. (concat "Dummy outline topic header -- see"
  5699. " `allout-mode' docstring: `\\[describe-mode]'.")))
  5700. (allout-adjust-file-variable
  5701. "allout-layout" (or allout-layout '(-1 : 0))))))
  5702. ;;;_ > allout-file-vars-section-data ()
  5703. (defun allout-file-vars-section-data ()
  5704. "Return data identifying the file-vars section, or nil if none.
  5705. Returns a list of the form (BEGINNING-POINT PREFIX-STRING SUFFIX-STRING)."
  5706. ;; minimally gleaned from emacs 21.4 files.el hack-local-variables function.
  5707. (let (beg prefix suffix)
  5708. (save-excursion
  5709. (goto-char (point-max))
  5710. (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
  5711. (if (let ((case-fold-search t))
  5712. (not (search-forward "Local Variables:" nil t)))
  5713. nil
  5714. (setq beg (- (point) 16))
  5715. (setq suffix (buffer-substring-no-properties
  5716. (point)
  5717. (progn (if (search-forward "\n" nil t)
  5718. (forward-char -1))
  5719. (point))))
  5720. (setq prefix (buffer-substring-no-properties
  5721. (progn (if (search-backward "\n" nil t)
  5722. (forward-char 1))
  5723. (point))
  5724. beg))
  5725. (list beg prefix suffix))
  5726. )
  5727. )
  5728. )
  5729. ;;;_ > allout-adjust-file-variable (varname value)
  5730. (defun allout-adjust-file-variable (varname value)
  5731. "Adjust the setting of an Emacs file variable named VARNAME to VALUE.
  5732. This activity is inhibited if either `enable-local-variables' or
  5733. `allout-enable-file-variable-adjustment' are nil.
  5734. When enabled, an entry for the variable is created if not already present,
  5735. or changed if established with a different value. The section for the file
  5736. variables, itself, is created if not already present. When created, the
  5737. section lines (including the section line) exist as second-level topics in
  5738. a top-level topic at the end of the file.
  5739. `enable-local-variables' must be true for any of this to happen."
  5740. (if (not (and enable-local-variables
  5741. allout-enable-file-variable-adjustment))
  5742. nil
  5743. (save-excursion
  5744. (let ((inhibit-field-text-motion t)
  5745. (section-data (allout-file-vars-section-data))
  5746. beg prefix suffix)
  5747. (if section-data
  5748. (setq beg (car section-data)
  5749. prefix (cadr section-data)
  5750. suffix (car (cddr section-data)))
  5751. ;; create the section
  5752. (goto-char (point-max))
  5753. (open-line 1)
  5754. (allout-open-topic 0)
  5755. (end-of-line)
  5756. (insert "Local emacs vars.\n")
  5757. (allout-open-topic 1)
  5758. (setq beg (point)
  5759. suffix ""
  5760. prefix (buffer-substring-no-properties (progn
  5761. (beginning-of-line)
  5762. (point))
  5763. beg))
  5764. (goto-char beg)
  5765. (insert "Local variables:\n")
  5766. (allout-open-topic 0)
  5767. (insert "End:\n")
  5768. )
  5769. ;; look for existing entry or create one, leaving point for insertion
  5770. ;; of new value:
  5771. (goto-char beg)
  5772. (allout-show-to-offshoot)
  5773. (if (search-forward (concat "\n" prefix varname ":") nil t)
  5774. (let* ((value-beg (point))
  5775. (line-end (progn (if (search-forward "\n" nil t)
  5776. (forward-char -1))
  5777. (point)))
  5778. (value-end (- line-end (length suffix))))
  5779. (if (> value-end value-beg)
  5780. (delete-region value-beg value-end)))
  5781. (end-of-line)
  5782. (open-line 1)
  5783. (forward-line 1)
  5784. (insert (concat prefix varname ":")))
  5785. (insert (format " %S%s" value suffix))
  5786. )
  5787. )
  5788. )
  5789. )
  5790. ;;;_ > allout-get-configvar-values (varname)
  5791. (defun allout-get-configvar-values (configvar-name)
  5792. "Return a list of values of the symbols in list bound to CONFIGVAR-NAME.
  5793. The user is prompted for removal of symbols that are unbound, and they
  5794. otherwise are ignored.
  5795. CONFIGVAR-NAME should be the name of the configuration variable,
  5796. not its value."
  5797. (let ((configvar-value (symbol-value configvar-name))
  5798. got)
  5799. (dolist (sym configvar-value)
  5800. (if (not (boundp sym))
  5801. (if (yes-or-no-p (format-message
  5802. "%s entry `%s' is unbound -- remove it? "
  5803. configvar-name sym))
  5804. (delq sym (symbol-value configvar-name)))
  5805. (push (symbol-value sym) got)))
  5806. (reverse got)))
  5807. ;;;_ : Topics:
  5808. ;;;_ > allout-mark-topic ()
  5809. (defun allout-mark-topic ()
  5810. "Put the region around topic currently containing point."
  5811. (interactive)
  5812. (let ((inhibit-field-text-motion t))
  5813. (beginning-of-line))
  5814. (allout-goto-prefix-doublechecked)
  5815. (push-mark (point))
  5816. (allout-end-of-current-subtree)
  5817. (exchange-point-and-mark))
  5818. ;;;_ : UI:
  5819. ;;;_ > solicit-char-in-string (prompt string &optional do-defaulting)
  5820. (defun solicit-char-in-string (prompt string &optional do-defaulting)
  5821. "Solicit (with first arg PROMPT) choice of a character from string STRING.
  5822. Optional arg DO-DEFAULTING indicates to accept empty input (CR)."
  5823. (let ((new-prompt prompt)
  5824. got)
  5825. (while (not got)
  5826. (message "%s" new-prompt)
  5827. ;; We do our own reading here, so we can circumvent, eg, special
  5828. ;; treatment for `?' character. (Oughta use minibuffer keymap instead.)
  5829. (setq got
  5830. (char-to-string (let ((cursor-in-echo-area nil)) (read-char))))
  5831. (setq got
  5832. (cond ((string-match (regexp-quote got) string) got)
  5833. ((and do-defaulting (string= got "\r"))
  5834. ;; Return empty string to default:
  5835. "")
  5836. ((string= got "\C-g") (signal 'quit nil))
  5837. (t
  5838. (setq new-prompt (concat prompt
  5839. got
  5840. " ...pick from: "
  5841. string
  5842. ""))
  5843. nil))))
  5844. ;; got something out of loop -- return it:
  5845. got)
  5846. )
  5847. ;;;_ : Strings:
  5848. ;;;_ > regexp-sans-escapes (string)
  5849. (defun regexp-sans-escapes (regexp &optional successive-backslashes)
  5850. "Return a copy of REGEXP with all character escapes stripped out.
  5851. Representations of actual backslashes -- `\\\\\\\\' -- are left as a
  5852. single backslash.
  5853. Optional arg SUCCESSIVE-BACKSLASHES is used internally for recursion."
  5854. (if (string= regexp "")
  5855. ""
  5856. ;; Set successive-backslashes to number if current char is
  5857. ;; backslash, or else to nil:
  5858. (setq successive-backslashes
  5859. (if (= (aref regexp 0) ?\\)
  5860. (if successive-backslashes (1+ successive-backslashes) 1)
  5861. nil))
  5862. (if (or (not successive-backslashes) (= 2 successive-backslashes))
  5863. ;; Include first char:
  5864. (concat (substring regexp 0 1)
  5865. (regexp-sans-escapes (substring regexp 1)))
  5866. ;; Exclude first char, but maintain count:
  5867. (regexp-sans-escapes (substring regexp 1) successive-backslashes))))
  5868. ;;;_ > count-trailing-whitespace-region (beg end)
  5869. (defun count-trailing-whitespace-region (beg end)
  5870. "Return number of trailing whitespace chars between BEG and END.
  5871. If BEG is bigger than END we return 0."
  5872. (if (> beg end)
  5873. 0
  5874. (save-match-data
  5875. (save-excursion
  5876. (goto-char beg)
  5877. (let ((count 0))
  5878. (while (re-search-forward "[ ][ ]*$" end t)
  5879. (goto-char (1+ (match-beginning 2)))
  5880. (setq count (1+ count)))
  5881. count)))))
  5882. ;;;_ > allout-format-quote (string)
  5883. (defun allout-format-quote (string)
  5884. "Return a copy of string with all \"%\" characters doubled."
  5885. (apply 'concat
  5886. (mapcar (lambda (char) (if (= char ?%) "%%" (char-to-string char)))
  5887. string)))
  5888. ;;;_ : lists
  5889. ;;;_ > allout-flatten (list)
  5890. (defun allout-flatten (list)
  5891. "Return a list of all atoms in list."
  5892. ;; classic.
  5893. (cond ((null list) nil)
  5894. ((atom (car list)) (cons (car list) (allout-flatten (cdr list))))
  5895. (t (append (allout-flatten (car list)) (allout-flatten (cdr list))))))
  5896. ;;;_ : Compatibility:
  5897. ;;;_ : xemacs undo-in-progress provision:
  5898. (unless (boundp 'undo-in-progress)
  5899. (defvar undo-in-progress nil
  5900. "Placeholder defvar for XEmacs compatibility from allout.el.")
  5901. (defadvice undo-more (around allout activate)
  5902. ;; This defadvice used only in emacs that lack undo-in-progress, eg xemacs.
  5903. (let ((undo-in-progress t)) ad-do-it)))
  5904. ;;;_ > allout-mark-marker to accommodate divergent emacsen:
  5905. (defun allout-mark-marker (&optional force buffer)
  5906. "Accommodate the different signature for `mark-marker' across Emacsen.
  5907. XEmacs takes two optional args, while Emacs does not,
  5908. so pass them along when appropriate."
  5909. (if (featurep 'xemacs)
  5910. (apply 'mark-marker force buffer)
  5911. (mark-marker)))
  5912. ;;;_ > subst-char-in-string if necessary
  5913. (if (not (fboundp 'subst-char-in-string))
  5914. (defun subst-char-in-string (fromchar tochar string &optional inplace)
  5915. "Replace FROMCHAR with TOCHAR in STRING each time it occurs.
  5916. Unless optional argument INPLACE is non-nil, return a new string."
  5917. (let ((i (length string))
  5918. (newstr (if inplace string (copy-sequence string))))
  5919. (while (> i 0)
  5920. (setq i (1- i))
  5921. (if (eq (aref newstr i) fromchar)
  5922. (aset newstr i tochar)))
  5923. newstr)))
  5924. ;;;_ > wholenump if necessary
  5925. (if (not (fboundp 'wholenump))
  5926. (defalias 'wholenump 'natnump))
  5927. ;;;_ > remove-overlays if necessary
  5928. (if (not (fboundp 'remove-overlays))
  5929. (defun remove-overlays (&optional beg end name val)
  5930. "Clear BEG and END of overlays whose property NAME has value VAL.
  5931. Overlays might be moved and/or split.
  5932. BEG and END default respectively to the beginning and end of buffer."
  5933. (unless beg (setq beg (point-min)))
  5934. (unless end (setq end (point-max)))
  5935. (if (< end beg)
  5936. (setq beg (prog1 end (setq end beg))))
  5937. (save-excursion
  5938. (dolist (o (overlays-in beg end))
  5939. (when (eq (overlay-get o name) val)
  5940. ;; Either push this overlay outside beg...end
  5941. ;; or split it to exclude beg...end
  5942. ;; or delete it entirely (if it is contained in beg...end).
  5943. (if (< (overlay-start o) beg)
  5944. (if (> (overlay-end o) end)
  5945. (progn
  5946. (move-overlay (copy-overlay o)
  5947. (overlay-start o) beg)
  5948. (move-overlay o end (overlay-end o)))
  5949. (move-overlay o (overlay-start o) beg))
  5950. (if (> (overlay-end o) end)
  5951. (move-overlay o end (overlay-end o))
  5952. (delete-overlay o)))))))
  5953. )
  5954. ;;;_ > copy-overlay if necessary -- xemacs ~ 21.4
  5955. (if (not (fboundp 'copy-overlay))
  5956. (defun copy-overlay (o)
  5957. "Return a copy of overlay O."
  5958. (let ((o1 (make-overlay (overlay-start o) (overlay-end o)
  5959. ;; FIXME: there's no easy way to find the
  5960. ;; insertion-type of the two markers.
  5961. (overlay-buffer o)))
  5962. (props (overlay-properties o)))
  5963. (while props
  5964. (overlay-put o1 (pop props) (pop props)))
  5965. o1)))
  5966. ;;;_ > add-to-invisibility-spec if necessary -- xemacs ~ 21.4
  5967. (if (not (fboundp 'add-to-invisibility-spec))
  5968. (defun add-to-invisibility-spec (element)
  5969. "Add ELEMENT to `buffer-invisibility-spec'.
  5970. See documentation for `buffer-invisibility-spec' for the kind of elements
  5971. that can be added."
  5972. (if (eq buffer-invisibility-spec t)
  5973. (setq buffer-invisibility-spec (list t)))
  5974. (setq buffer-invisibility-spec
  5975. (cons element buffer-invisibility-spec))))
  5976. ;;;_ > remove-from-invisibility-spec if necessary -- xemacs ~ 21.4
  5977. (if (not (fboundp 'remove-from-invisibility-spec))
  5978. (defun remove-from-invisibility-spec (element)
  5979. "Remove ELEMENT from `buffer-invisibility-spec'."
  5980. (if (consp buffer-invisibility-spec)
  5981. (setq buffer-invisibility-spec (delete element
  5982. buffer-invisibility-spec)))))
  5983. ;;;_ > move-beginning-of-line if necessary -- older emacs, xemacs
  5984. (if (not (fboundp 'move-beginning-of-line))
  5985. (defun move-beginning-of-line (arg)
  5986. "Move point to beginning of current line as displayed.
  5987. \(This disregards invisible newlines such as those
  5988. which are part of the text that an image rests on.)
  5989. With argument ARG not nil or 1, move forward ARG - 1 lines first.
  5990. If point reaches the beginning or end of buffer, it stops there.
  5991. To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
  5992. (interactive "p")
  5993. (or arg (setq arg 1))
  5994. (if (/= arg 1)
  5995. (condition-case nil (line-move (1- arg)) (error nil)))
  5996. ;; Move to beginning-of-line, ignoring fields and invisible text.
  5997. (skip-chars-backward "^\n")
  5998. (while (and (not (bobp))
  5999. (let ((prop
  6000. (get-char-property (1- (point)) 'invisible)))
  6001. (if (eq buffer-invisibility-spec t)
  6002. prop
  6003. (or (memq prop buffer-invisibility-spec)
  6004. (assq prop buffer-invisibility-spec)))))
  6005. (goto-char (if (featurep 'xemacs)
  6006. (previous-property-change (point))
  6007. (previous-char-property-change (point))))
  6008. (skip-chars-backward "^\n"))
  6009. (vertical-motion 0))
  6010. )
  6011. ;;;_ > move-end-of-line if necessary -- Emacs < 22.1, xemacs
  6012. (if (not (fboundp 'move-end-of-line))
  6013. (defun move-end-of-line (arg)
  6014. "Move point to end of current line as displayed.
  6015. \(This disregards invisible newlines such as those
  6016. which are part of the text that an image rests on.)
  6017. With argument ARG not nil or 1, move forward ARG - 1 lines first.
  6018. If point reaches the beginning or end of buffer, it stops there.
  6019. To ignore intangibility, bind `inhibit-point-motion-hooks' to t."
  6020. (interactive "p")
  6021. (or arg (setq arg 1))
  6022. (let (done)
  6023. (while (not done)
  6024. (let ((newpos
  6025. (save-excursion
  6026. (let ((goal-column 0))
  6027. (and (condition-case nil
  6028. (or (line-move arg) t)
  6029. (error nil))
  6030. (not (bobp))
  6031. (progn
  6032. (while
  6033. (and
  6034. (not (bobp))
  6035. (let ((prop
  6036. (get-char-property (1- (point))
  6037. 'invisible)))
  6038. (if (eq buffer-invisibility-spec t)
  6039. prop
  6040. (or (memq prop
  6041. buffer-invisibility-spec)
  6042. (assq prop
  6043. buffer-invisibility-spec)))))
  6044. (goto-char
  6045. (previous-char-property-change (point))))
  6046. (backward-char 1)))
  6047. (point)))))
  6048. (goto-char newpos)
  6049. (if (and (> (point) newpos)
  6050. (eq (preceding-char) ?\n))
  6051. (backward-char 1)
  6052. (if (and (> (point) newpos) (not (eobp))
  6053. (not (eq (following-char) ?\n)))
  6054. ;; If we skipped something intangible
  6055. ;; and now we're not really at eol,
  6056. ;; keep going.
  6057. (setq arg 1)
  6058. (setq done t)))))))
  6059. )
  6060. ;;;_ > allout-next-single-char-property-change -- alias unless lacking
  6061. (defalias 'allout-next-single-char-property-change
  6062. (if (fboundp 'next-single-char-property-change)
  6063. 'next-single-char-property-change
  6064. 'next-single-property-change)
  6065. ;; No docstring because xemacs defalias doesn't support it.
  6066. )
  6067. ;;;_ > allout-previous-single-char-property-change -- alias unless lacking
  6068. (defalias 'allout-previous-single-char-property-change
  6069. (if (fboundp 'previous-single-char-property-change)
  6070. 'previous-single-char-property-change
  6071. 'previous-single-property-change)
  6072. ;; No docstring because xemacs defalias doesn't support it.
  6073. )
  6074. ;;;_ > allout-select-safe-coding-system
  6075. (defalias 'allout-select-safe-coding-system
  6076. (if (fboundp 'select-safe-coding-system)
  6077. 'select-safe-coding-system
  6078. 'detect-coding-region)
  6079. )
  6080. ;;;_ > allout-substring-no-properties
  6081. ;; define as alias first, so byte compiler is happy.
  6082. (defalias 'allout-substring-no-properties 'substring-no-properties)
  6083. ;; then supplant with definition if underlying alias absent.
  6084. (if (not (fboundp 'substring-no-properties))
  6085. (defun allout-substring-no-properties (string &optional start end)
  6086. (substring string (or start 0) end))
  6087. )
  6088. ;;;_ #10 Unfinished
  6089. ;;;_ > allout-bullet-isearch (&optional bullet)
  6090. (defun allout-bullet-isearch (&optional bullet)
  6091. "Isearch (regexp) for topic with bullet BULLET."
  6092. (interactive)
  6093. (if (not bullet)
  6094. (setq bullet (solicit-char-in-string
  6095. "ISearch for topic with bullet: "
  6096. (regexp-sans-escapes allout-bullets-string))))
  6097. (let ((isearch-regexp t)
  6098. (isearch-string (concat "^"
  6099. allout-header-prefix
  6100. "[ \t]*"
  6101. bullet)))
  6102. (isearch-repeat 'forward)
  6103. (isearch-mode t)))
  6104. ;;;_ #11 Unit tests -- this should be last item before "Provide"
  6105. ;;;_ > allout-run-unit-tests ()
  6106. (defun allout-run-unit-tests ()
  6107. "Run the various allout unit tests."
  6108. (message "Running allout tests...")
  6109. (allout-test-resumptions)
  6110. (message "Running allout tests... Done.")
  6111. (sit-for .5))
  6112. ;;;_ : test resumptions:
  6113. ;;;_ > allout-tests-obliterate-variable (name)
  6114. (defun allout-tests-obliterate-variable (name)
  6115. "Completely unbind variable with NAME."
  6116. (if (local-variable-p name (current-buffer)) (kill-local-variable name))
  6117. (while (boundp name) (makunbound name)))
  6118. ;;;_ > allout-test-resumptions ()
  6119. (defvar allout-tests-globally-unbound nil
  6120. "Fodder for allout resumptions tests -- defvar just for byte compiler.")
  6121. (defvar allout-tests-globally-true nil
  6122. "Fodder for allout resumptions tests -- defvar just for byte compiler.")
  6123. (defvar allout-tests-locally-true nil
  6124. "Fodder for allout resumptions tests -- defvar just for byte compiler.")
  6125. (defun allout-test-resumptions ()
  6126. "Exercise allout resumptions."
  6127. ;; for each resumption case, we also test that the right local/global
  6128. ;; scopes are affected during resumption effects:
  6129. ;; ensure that previously unbound variables return to the unbound state.
  6130. (with-temp-buffer
  6131. (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
  6132. (allout-add-resumptions '(allout-tests-globally-unbound t))
  6133. (assert (not (default-boundp 'allout-tests-globally-unbound)))
  6134. (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
  6135. (assert (boundp 'allout-tests-globally-unbound))
  6136. (assert (equal allout-tests-globally-unbound t))
  6137. (allout-do-resumptions)
  6138. (assert (not (local-variable-p 'allout-tests-globally-unbound
  6139. (current-buffer))))
  6140. (assert (not (boundp 'allout-tests-globally-unbound))))
  6141. ;; ensure that variable with prior global value is resumed
  6142. (with-temp-buffer
  6143. (allout-tests-obliterate-variable 'allout-tests-globally-true)
  6144. (setq allout-tests-globally-true t)
  6145. (allout-add-resumptions '(allout-tests-globally-true nil))
  6146. (assert (equal (default-value 'allout-tests-globally-true) t))
  6147. (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
  6148. (assert (equal allout-tests-globally-true nil))
  6149. (allout-do-resumptions)
  6150. (assert (not (local-variable-p 'allout-tests-globally-true
  6151. (current-buffer))))
  6152. (assert (boundp 'allout-tests-globally-true))
  6153. (assert (equal allout-tests-globally-true t)))
  6154. ;; ensure that prior local value is resumed
  6155. (with-temp-buffer
  6156. (allout-tests-obliterate-variable 'allout-tests-locally-true)
  6157. (set (make-local-variable 'allout-tests-locally-true) t)
  6158. (assert (not (default-boundp 'allout-tests-locally-true))
  6159. nil (concat "Test setup mistake -- variable supposed to"
  6160. " not have global binding, but it does."))
  6161. (assert (local-variable-p 'allout-tests-locally-true (current-buffer))
  6162. nil (concat "Test setup mistake -- variable supposed to have"
  6163. " local binding, but it lacks one."))
  6164. (allout-add-resumptions '(allout-tests-locally-true nil))
  6165. (assert (not (default-boundp 'allout-tests-locally-true)))
  6166. (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
  6167. (assert (equal allout-tests-locally-true nil))
  6168. (allout-do-resumptions)
  6169. (assert (boundp 'allout-tests-locally-true))
  6170. (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
  6171. (assert (equal allout-tests-locally-true t))
  6172. (assert (not (default-boundp 'allout-tests-locally-true))))
  6173. ;; ensure that last of multiple resumptions holds, for various scopes.
  6174. (with-temp-buffer
  6175. (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
  6176. (allout-tests-obliterate-variable 'allout-tests-globally-true)
  6177. (setq allout-tests-globally-true t)
  6178. (allout-tests-obliterate-variable 'allout-tests-locally-true)
  6179. (set (make-local-variable 'allout-tests-locally-true) t)
  6180. (allout-add-resumptions '(allout-tests-globally-unbound t)
  6181. '(allout-tests-globally-true nil)
  6182. '(allout-tests-locally-true nil))
  6183. (allout-add-resumptions '(allout-tests-globally-unbound 2)
  6184. '(allout-tests-globally-true 3)
  6185. '(allout-tests-locally-true 4))
  6186. ;; reestablish many of the basic conditions are maintained after re-add:
  6187. (assert (not (default-boundp 'allout-tests-globally-unbound)))
  6188. (assert (local-variable-p 'allout-tests-globally-unbound (current-buffer)))
  6189. (assert (equal allout-tests-globally-unbound 2))
  6190. (assert (default-boundp 'allout-tests-globally-true))
  6191. (assert (local-variable-p 'allout-tests-globally-true (current-buffer)))
  6192. (assert (equal allout-tests-globally-true 3))
  6193. (assert (not (default-boundp 'allout-tests-locally-true)))
  6194. (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
  6195. (assert (equal allout-tests-locally-true 4))
  6196. (allout-do-resumptions)
  6197. (assert (not (local-variable-p 'allout-tests-globally-unbound
  6198. (current-buffer))))
  6199. (assert (not (boundp 'allout-tests-globally-unbound)))
  6200. (assert (not (local-variable-p 'allout-tests-globally-true
  6201. (current-buffer))))
  6202. (assert (boundp 'allout-tests-globally-true))
  6203. (assert (equal allout-tests-globally-true t))
  6204. (assert (boundp 'allout-tests-locally-true))
  6205. (assert (local-variable-p 'allout-tests-locally-true (current-buffer)))
  6206. (assert (equal allout-tests-locally-true t))
  6207. (assert (not (default-boundp 'allout-tests-locally-true))))
  6208. ;; ensure that deliberately unbinding registered variables doesn't foul things
  6209. (with-temp-buffer
  6210. (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
  6211. (allout-tests-obliterate-variable 'allout-tests-globally-true)
  6212. (setq allout-tests-globally-true t)
  6213. (allout-tests-obliterate-variable 'allout-tests-locally-true)
  6214. (set (make-local-variable 'allout-tests-locally-true) t)
  6215. (allout-add-resumptions '(allout-tests-globally-unbound t)
  6216. '(allout-tests-globally-true nil)
  6217. '(allout-tests-locally-true nil))
  6218. (allout-tests-obliterate-variable 'allout-tests-globally-unbound)
  6219. (allout-tests-obliterate-variable 'allout-tests-globally-true)
  6220. (allout-tests-obliterate-variable 'allout-tests-locally-true)
  6221. (allout-do-resumptions))
  6222. )
  6223. ;;;_ % Run unit tests if `allout-run-unit-tests-after-load' is true:
  6224. (when allout-run-unit-tests-on-load
  6225. (allout-run-unit-tests))
  6226. ;;;_ #12 Provide
  6227. (provide 'allout)
  6228. ;;;_* Local emacs vars.
  6229. ;; The following `allout-layout' local variable setting:
  6230. ;; - closes all topics from the first topic to just before the third-to-last,
  6231. ;; - shows the children of the third to last (config vars)
  6232. ;; - and the second to last (code section),
  6233. ;; - and closes the last topic (this local-variables section).
  6234. ;;Local variables:
  6235. ;;allout-layout: (0 : -1 -1 0)
  6236. ;;End:
  6237. ;;; allout.el ends here