org.el 477 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090
  1. ;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 4.13
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software; you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation; either version 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around a NOTES file that contains
  32. ;; information about projects as plain text. Org-mode is implemented on top
  33. ;; of outline-mode - ideal to keep the content of large files well structured.
  34. ;; It supports ToDo items, deadlines and time stamps, which can be extracted
  35. ;; to create a daily/weekly agenda that also integrates the diary of the Emacs
  36. ;; calendar. Tables are easily created with a built-in table editor. Plain
  37. ;; text URL-like links connect to websites, emails (VM, RMAIL, WANDERLUST),
  38. ;; Usenet messages (Gnus), BBDB entries, and any files related to the
  39. ;; projects. For printing and sharing of notes, an Org-mode file (or a part
  40. ;; of it) can be exported as a structured ASCII file, or as HTML.
  41. ;;
  42. ;; Installation
  43. ;; ------------
  44. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  45. ;; only need to copy the following lines to your .emacs file. The last two
  46. ;; lines define *global* keys for the commands `org-store-link' and
  47. ;; `org-agenda' - please choose suitable keys yourself.
  48. ;;
  49. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  50. ;; (define-key global-map "\C-cl" 'org-store-link)
  51. ;; (define-key global-map "\C-ca" 'org-agenda)
  52. ;;
  53. ;; If you have downloaded Org-mode from the Web, you must byte-compile
  54. ;; org.el and put it on your load path. In addition to the Emacs Lisp
  55. ;; lines above, you also need to add the following lines to .emacs:
  56. ;;
  57. ;; (autoload 'org-mode "org" "Org mode" t)
  58. ;; (autoload 'org-diary "org" "Diary entries from Org mode")
  59. ;; (autoload 'org-agenda "org" "Multi-file agenda from Org mode" t)
  60. ;; (autoload 'org-store-link "org" "Store a link to the current location" t)
  61. ;; (autoload 'orgtbl-mode "org" "Org tables as a minor mode" t)
  62. ;; (autoload 'turn-on-orgtbl "org" "Org tables as a minor mode")
  63. ;;
  64. ;; This setup will put all files with extension ".org" into Org-mode. As
  65. ;; an alternative, make the first line of a file look like this:
  66. ;;
  67. ;; MY PROJECTS -*- mode: org; -*-
  68. ;;
  69. ;; which will select Org-mode for this buffer no matter what the file's
  70. ;; name is.
  71. ;;
  72. ;; Documentation
  73. ;; -------------
  74. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  75. ;; distribution also contains a PDF version of it. At the homepage of
  76. ;; Org-mode, you can read the same text online as HTML. There is also an
  77. ;; excellent reference card made by Philip Rooke. This card can be found
  78. ;; in the etc/ directory of Emacs 22.
  79. ;;
  80. ;; Changes since version 4.00:
  81. ;; ---------------------------
  82. ;; Version 4.13
  83. ;; - The list of agenda files can be maintainted in an external file.
  84. ;; - Bug fixes.
  85. ;;
  86. ;; Version 4.12
  87. ;; - Templates for remember buffer. Note that the remember setup changes.
  88. ;; To set up templates, see `org-remember-templates'.
  89. ;; - The time in new time stamps can be rounded, see new option
  90. ;; `org-time-stamp-rounding-minutes'.
  91. ;; - Bug fixes (there are *always* more bugs).
  92. ;;
  93. ;; Version 4.10
  94. ;; - Bug fixes.
  95. ;;
  96. ;; Version 4.09
  97. ;; - Bug fixes.
  98. ;; - Small improvements to font-lock support.
  99. ;; - MHE support finalized.
  100. ;;
  101. ;; Version 4.08
  102. ;; - Bug fixes.
  103. ;; - Improved MHE support
  104. ;;
  105. ;; Version 4.07
  106. ;; - Bug fixes.
  107. ;; - Leading stars in headlines can be hidden, so make the outline look
  108. ;; cleaner.
  109. ;; - Mouse-1 can be used to follow links.
  110. ;;
  111. ;; Version 4.06
  112. ;; - HTML exporter treats targeted internal links.
  113. ;; - Bug fixes.
  114. ;;
  115. ;; Version 4.05
  116. ;; - Changes to internal link system (thanks to David Wainberg for ideas).
  117. ;; - in-file links: [[Search String]] instead of <file:::Search String>
  118. ;; - automatic links to "radio targets".
  119. ;; - CamelCase not longer active by default, configure org-activate-camels
  120. ;; if you want to turn it back on.
  121. ;; - After following a link, `C-c &' jumps back to it.
  122. ;; - MH-E link support (thanks to Thomas Baumann).
  123. ;; - Special table lines are no longer exported.
  124. ;; - Bug fixes and minor improvements.
  125. ;;
  126. ;; Version 4.04
  127. ;; - Cleanup tags display in agenda.
  128. ;; - Bug fixes.
  129. ;;
  130. ;; Version 4.03
  131. ;; - Table alignment fixed for use with wide characters.
  132. ;; - `C-c -' leaves cursor in current table line.
  133. ;; - The current TAG can be incorporated into the agenda prefix.
  134. ;; See option `org-agenda-prefix-format' for details.
  135. ;;
  136. ;; Version 4.02
  137. ;; - Minor bug fixes and improvements around tag searches.
  138. ;; - XEmacs compatibility fixes.
  139. ;;
  140. ;; Version 4.01
  141. ;; - Tags can also be set remotely from agenda buffer.
  142. ;; - Boolean logic for tag searches.
  143. ;; - Additional agenda commands can be configured through the variable
  144. ;; `org-agenda-custom-commands'.
  145. ;; - Minor bug fixes.
  146. ;;
  147. ;;; Code:
  148. (eval-when-compile (require 'cl) (require 'calendar))
  149. (require 'outline)
  150. (require 'time-date)
  151. (require 'easymenu)
  152. (defvar calc-embedded-close-formula) ; defined by the calc package
  153. (defvar calc-embedded-open-formula) ; defined by the calc package
  154. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  155. ;;; Customization variables
  156. (defvar org-version "4.13"
  157. "The version number of the file org.el.")
  158. (defun org-version ()
  159. (interactive)
  160. (message "Org-mode version %s" org-version))
  161. ;; The following constant is for compatibility with different versions
  162. ;; of outline.el.
  163. (defconst org-noutline-p (featurep 'noutline)
  164. "Are we using the new outline mode?")
  165. (defgroup org nil
  166. "Outline-based notes management and organizer."
  167. :tag "Org"
  168. :group 'outlines
  169. :group 'hypermedia
  170. :group 'calendar)
  171. (defgroup org-startup nil
  172. "Options concerning startup of Org-mode."
  173. :tag "Org Startup"
  174. :group 'org)
  175. (defcustom org-CUA-compatible nil
  176. "Non-nil means use alternative key bindings for S-<cursor movement>.
  177. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  178. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  179. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  180. alternative bindings. Setting this variable to t will replace the following
  181. keys both in Org-mode and in the Org-agenda buffer.
  182. S-RET -> C-S-RET
  183. S-up -> M-p
  184. S-down -> M-n
  185. S-left -> M--
  186. S-right -> M-+
  187. If you do not like the alternative keys, take a look at the variable
  188. `org-disputed-keys'.
  189. This option is only relevant at load-time of Org-mode. Changing it requires
  190. a restart of Emacs to become effective."
  191. :group 'org-startup
  192. :type 'boolean)
  193. (defvar org-disputed-keys
  194. '((S-up [(shift up)] [(meta ?p)])
  195. (S-down [(shift down)] [(meta ?n)])
  196. (S-left [(shift left)] [(meta ?-)])
  197. (S-right [(shift right)] [(meta ?+)])
  198. (S-return [(shift return)] [(control shift return)]))
  199. "Keys for which Org-mode and other modes compete.
  200. This is an alist, cars are symbols for lookup, 1st element is the default key,
  201. second element will be used when `org-CUA-compatible' is t.")
  202. (defun org-key (key)
  203. "Select a key according to `org-CUA-compatible'."
  204. (nth (if org-CUA-compatible 2 1)
  205. (or (assq key org-disputed-keys)
  206. (error "Invalid Key %s in `org-key'" key))))
  207. (defcustom org-startup-folded t
  208. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  209. This can also be configured on a per-file basis by adding one of
  210. the following lines anywhere in the buffer:
  211. #+STARTUP: fold
  212. #+STARTUP: nofold
  213. #+STARTUP: content"
  214. :group 'org-startup
  215. :type '(choice
  216. (const :tag "nofold: show all" nil)
  217. (const :tag "fold: overview" t)
  218. (const :tag "content: all headlines" content)))
  219. (defcustom org-startup-truncated t
  220. "Non-nil means, entering Org-mode will set `truncate-lines'.
  221. This is useful since some lines containing links can be very long and
  222. uninteresting. Also tables look terrible when wrapped."
  223. :group 'org-startup
  224. :type 'boolean)
  225. (defcustom org-startup-with-deadline-check nil
  226. "Non-nil means, entering Org-mode will run the deadline check.
  227. This means, if you start editing an org file, you will get an
  228. immediate reminder of any due deadlines.
  229. This can also be configured on a per-file basis by adding one of
  230. the following lines anywhere in the buffer:
  231. #+STARTUP: dlcheck
  232. #+STARTUP: nodlcheck"
  233. :group 'org-startup
  234. :type 'boolean)
  235. (defcustom org-insert-mode-line-in-empty-file nil
  236. "Non-nil means insert the first line setting Org-mode in empty files.
  237. When the function `org-mode' is called interactively in an empty file, this
  238. normally means that the file name does not automatically trigger Org-mode.
  239. To ensure that the file will always be in Org-mode in the future, a
  240. line enforcing Org-mode will be inserted into the buffer, if this option
  241. has been set."
  242. :group 'org-startup
  243. :type 'boolean)
  244. (defgroup org-keywords nil
  245. "Options concerning TODO items in Org-mode."
  246. :tag "Org Keywords"
  247. :group 'org)
  248. (defcustom org-todo-keywords '("TODO" "DONE")
  249. "List of TODO entry keywords.
  250. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  251. considered to mean that the entry is \"done\". All the other mean that
  252. action is required, and will make the entry show up in todo lists, diaries
  253. etc.
  254. The command \\[org-todo] cycles an entry through these states, and an
  255. additional state where no keyword is present. For details about this
  256. cycling, see also the variable `org-todo-interpretation'
  257. Changes become only effective after restarting Emacs."
  258. :group 'org-keywords
  259. :type '(repeat (string :tag "Keyword")))
  260. (defcustom org-todo-interpretation 'sequence
  261. "Controls how TODO keywords are interpreted.
  262. \\<org-mode-map>Possible values are `sequence' and `type'.
  263. This variable is only relevant if `org-todo-keywords' contains more than two
  264. states. There are two ways how these keywords can be used:
  265. - As a sequence in the process of working on a TODO item, for example
  266. (setq org-todo-keywords '(\"TODO\" \"STARTED\" \"VERIFY\" \"DONE\")
  267. org-todo-interpretation 'sequence)
  268. - As different types of TODO items, for example
  269. (setq org-todo-keywords '(\"URGENT\" \"RELAXED\" \"REMIND\" \"FOR_TOM\" \"DONE\")
  270. org-todo-interpretation 'type)
  271. When the states are interpreted as a sequence, \\[org-todo] always cycles
  272. to the next state, in order to walk through all different states. So with
  273. \\[org-todo], you turn an empty entry into the state TODO. When you started
  274. working on the item, you use \\[org-todo] again to switch it to \"STARTED\",
  275. later to VERIFY and finally to DONE.
  276. When the states are interpreted as types, \\[org-todo] still cycles through
  277. when it is called several times in direct succession, in order to initially
  278. select the type. However, if not called immediately after a previous
  279. \\[org-todo], it switches from each type directly to DONE. So with the
  280. above example, you could use `\\[org-todo] \\[org-todo]' to label an entry
  281. RELAXED. If you later return to this entry and press \\[org-todo] again,
  282. RELAXED will not be changed REMIND, but directly to DONE.
  283. You can create a large number of types. To initially select a
  284. type, it is then best to use \\[universal-argument] \\[org-todo] in order to specify the
  285. type with completion. Of course, you can also type the keyword
  286. directly into the buffer. M-TAB completes TODO keywords at the
  287. beginning of a headline."
  288. :group 'org-keywords
  289. :type '(choice (const sequence)
  290. (const type)))
  291. (defcustom org-default-priority ?B
  292. "The default priority of TODO items.
  293. This is the priority an item get if no explicit priority is given."
  294. :group 'org-keywords
  295. :type 'character)
  296. (defcustom org-lowest-priority ?C
  297. "The lowest priority of TODO items. A character like ?A, ?B etc."
  298. :group 'org-keywords
  299. :type 'character)
  300. (defcustom org-deadline-string "DEADLINE:"
  301. "String to mark deadline entries.
  302. A deadline is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-deadline].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-scheduled-string "SCHEDULED:"
  309. "String to mark scheduled TODO entries.
  310. A schedule is this string, followed by a time stamp. Should be a word,
  311. terminated by a colon. You can insert a schedule keyword and
  312. a timestamp with \\[org-schedule].
  313. Changes become only effective after restarting Emacs."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-closed-string "CLOSED:"
  317. "String used as the prefix for timestamps logging closing a TODO entry."
  318. :group 'org-keywords
  319. :type 'string)
  320. (defcustom org-comment-string "COMMENT"
  321. "Entries starting with this keyword will never be exported.
  322. An entry can be toggled between COMMENT and normal with
  323. \\[org-toggle-comment].
  324. Changes become only effective after restarting Emacs."
  325. :group 'org-keywords
  326. :type 'string)
  327. (defcustom org-quote-string "QUOTE"
  328. "Entries starting with this keyword will be exported in fixed-width font.
  329. Quoting applies only to the text in the entry following the headline, and does
  330. not extend beyond the next headline, even if that is lower level.
  331. An entry can be toggled between QUOTE and normal with
  332. \\[org-toggle-fixed-width-section]."
  333. :group 'org-keywords
  334. :type 'string)
  335. (defcustom org-after-todo-state-change-hook nil
  336. "Hook which is run after the state of a TODO item was changed.
  337. The new state (a string with a TODO keyword, or nil) is available in the
  338. Lisp variable `state'."
  339. :group 'org-keywords
  340. :type 'hook)
  341. ;; Variables for pre-computed regular expressions, all buffer local
  342. (defvar org-todo-kwd-priority-p nil
  343. "Do TODO items have priorities?")
  344. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  345. (defvar org-todo-kwd-max-priority nil
  346. "Maximum priority of TODO items.")
  347. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  348. (defvar org-ds-keyword-length 12
  349. "Maximum length of the Deadline and SCHEDULED keywords.")
  350. (make-variable-buffer-local 'org-ds-keyword-length)
  351. (defvar org-done-string nil
  352. "The last string in `org-todo-keywords', indicating an item is DONE.")
  353. (make-variable-buffer-local 'org-done-string)
  354. (defvar org-todo-regexp nil
  355. "Matches any of the TODO state keywords.")
  356. (make-variable-buffer-local 'org-todo-regexp)
  357. (defvar org-not-done-regexp nil
  358. "Matches any of the TODO state keywords except the last one.")
  359. (make-variable-buffer-local 'org-not-done-regexp)
  360. (defvar org-todo-line-regexp nil
  361. "Matches a headline and puts TODO state into group 2 if present.")
  362. (make-variable-buffer-local 'org-todo-line-regexp)
  363. (defvar org-nl-done-regexp nil
  364. "Matches newline followed by a headline with the DONE keyword.")
  365. (make-variable-buffer-local 'org-nl-done-regexp)
  366. (defvar org-looking-at-done-regexp nil
  367. "Matches the DONE keyword a point.")
  368. (make-variable-buffer-local 'org-looking-at-done-regexp)
  369. (defvar org-deadline-regexp nil
  370. "Matches the DEADLINE keyword.")
  371. (make-variable-buffer-local 'org-deadline-regexp)
  372. (defvar org-deadline-time-regexp nil
  373. "Matches the DEADLINE keyword together with a time stamp.")
  374. (make-variable-buffer-local 'org-deadline-time-regexp)
  375. (defvar org-deadline-line-regexp nil
  376. "Matches the DEADLINE keyword and the rest of the line.")
  377. (make-variable-buffer-local 'org-deadline-line-regexp)
  378. (defvar org-scheduled-regexp nil
  379. "Matches the SCHEDULED keyword.")
  380. (make-variable-buffer-local 'org-scheduled-regexp)
  381. (defvar org-scheduled-time-regexp nil
  382. "Matches the SCHEDULED keyword together with a time stamp.")
  383. (make-variable-buffer-local 'org-scheduled-time-regexp)
  384. (defvar org-category nil
  385. "Variable used by org files to set a category for agenda display.
  386. Such files should use a file variable to set it, for example
  387. -*- mode: org; org-category: \"ELisp\"
  388. or contain a special line
  389. #+CATEGORY: ELisp
  390. If the file does not specify a category, then file's base name
  391. is used instead.")
  392. (make-variable-buffer-local 'org-category)
  393. (defgroup org-time nil
  394. "Options concerning time stamps and deadlines in Org-mode."
  395. :tag "Org Time"
  396. :group 'org)
  397. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  398. "Formats for `format-time-string' which are used for time stamps.
  399. It is not recommended to change this constant.")
  400. (defcustom org-time-stamp-rounding-minutes 0
  401. "Number of minutes to round time stamps to upon insertion.
  402. When zero, insert the time unmodified. Useful rounding numbers
  403. should be factors of 60, so for example 5, 10, 15.
  404. When this is not zero, you can still force an exact time-stamp by using
  405. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  406. :group 'org-time
  407. :type 'integer)
  408. (defcustom org-deadline-warning-days 30
  409. "No. of days before expiration during which a deadline becomes active.
  410. This variable governs the display in the org file."
  411. :group 'org-time
  412. :type 'number)
  413. (defcustom org-popup-calendar-for-date-prompt t
  414. "Non-nil means, pop up a calendar when prompting for a date.
  415. In the calendar, the date can be selected with mouse-1. However, the
  416. minibuffer will also be active, and you can simply enter the date as well.
  417. When nil, only the minibuffer will be available."
  418. :group 'org-time
  419. :type 'boolean)
  420. (defcustom org-calendar-follow-timestamp-change t
  421. "Non-nil means, make the calendar window follow timestamp changes.
  422. When a timestamp is modified and the calendar window is visible, it will be
  423. moved to the new date."
  424. :group 'org-time
  425. :type 'boolean)
  426. (defcustom org-log-done nil
  427. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  428. When the state of an entry is changed from nothing to TODO, remove a previous
  429. closing date."
  430. :group 'org-time
  431. :type 'boolean)
  432. (defgroup org-agenda nil
  433. "Options concerning agenda display Org-mode."
  434. :tag "Org Agenda"
  435. :group 'org)
  436. (defcustom org-agenda-files nil
  437. "The files to be used for agenda display.
  438. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  439. \\[org-remove-file]. You can also use customize to edit the list.
  440. If the value of the variable is not a list but a single file name, then
  441. the list of agenda files is actually stored in that file, one agenda file
  442. per line"
  443. :group 'org-agenda
  444. :type '(choice
  445. (repeat :tag "List of files" file)
  446. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  447. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  448. "Custom commands for the agenda.
  449. These commands will be offered on the splash screen displayed by the
  450. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  451. key The key (a single char as a string) to be associated with the command.
  452. type The command type, any of the following symbols:
  453. todo Entries with a specific TODO keyword, in all agenda files.
  454. tags Tags match in all agenda files.
  455. todo-tree Sparse tree of specific TODO keyword in *current* file.
  456. tags-tree Sparse tree with all tags matches in *current* file.
  457. occur-tree Occur sparse tree for current file.
  458. match What to search for:
  459. - a single keyword for TODO keyword searches
  460. - a tags match expression for tags searches
  461. - a regular expression for occur searches"
  462. :group 'org-agenda
  463. :type '(repeat
  464. (list (string :tag "Key")
  465. (choice :tag "Type"
  466. (const :tag "Tags search in all agenda files" tags)
  467. (const :tag "TODO keyword search in all agenda files" todo)
  468. (const :tag "Tags sparse tree in current buffer" tags-tree)
  469. (const :tag "TODO keyword tree in current buffer" todo-tree)
  470. (const :tag "Occur tree in current buffer" occur-tree))
  471. (string :tag "Match"))))
  472. (defcustom org-select-timeline-window t
  473. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  474. When nil, cursor will remain in the current window."
  475. :group 'org-agenda
  476. :type 'boolean)
  477. (defcustom org-select-agenda-window t
  478. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  479. When nil, cursor will remain in the current window."
  480. :group 'org-agenda
  481. :type 'boolean)
  482. (defcustom org-fit-agenda-window t
  483. "Non-nil means, change window size of agenda to fit content."
  484. :group 'org-agenda
  485. :type 'boolean)
  486. (defcustom org-agenda-show-all-dates t
  487. "Non-nil means, `org-agenda' shows every day in the selected range.
  488. When nil, only the days which actually have entries are shown."
  489. :group 'org-agenda
  490. :type 'boolean)
  491. ;; FIXME: First day of month works only for current month because it would
  492. ;; require a variable ndays treatment.
  493. (defcustom org-agenda-start-on-weekday 1
  494. "Non-nil means, start the overview always on the specified weekday.
  495. 0 Denotes Sunday, 1 denotes Monday etc.
  496. When nil, always start on the current day."
  497. :group 'org-agenda
  498. :type '(choice (const :tag "Today" nil)
  499. (const :tag "First day of month" t)
  500. (number :tag "Weekday No.")))
  501. (defcustom org-agenda-ndays 7
  502. "Number of days to include in overview display."
  503. :group 'org-agenda
  504. :type 'number)
  505. (defcustom org-agenda-include-all-todo t
  506. "Non-nil means, the agenda will always contain all TODO entries.
  507. When nil, date-less entries will only be shown if `org-agenda' is called
  508. with a prefix argument.
  509. When non-nil, the TODO entries will be listed at the top of the agenda, before
  510. the entries for specific days."
  511. :group 'org-agenda
  512. :type 'boolean)
  513. (defcustom org-agenda-include-diary nil
  514. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  515. :group 'org-agenda
  516. :type 'boolean)
  517. (defcustom org-calendar-to-agenda-key [?c]
  518. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  519. The command `org-calendar-goto-agenda' will be bound to this key. The
  520. default is the character `c' because then `c' can be used to switch back and
  521. forth between agenda and calendar."
  522. :group 'org-agenda
  523. :type 'sexp)
  524. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  525. "Sorting structure for the agenda items of a single day.
  526. This is a list of symbols which will be used in sequence to determine
  527. if an entry should be listed before another entry. The following
  528. symbols are recognized:
  529. time-up Put entries with time-of-day indications first, early first
  530. time-down Put entries with time-of-day indications first, late first
  531. category-keep Keep the default order of categories, corresponding to the
  532. sequence in `org-agenda-files'.
  533. category-up Sort alphabetically by category, A-Z.
  534. category-down Sort alphabetically by category, Z-A.
  535. priority-up Sort numerically by priority, high priority last.
  536. priority-down Sort numerically by priority, high priority first.
  537. The different possibilities will be tried in sequence, and testing stops
  538. if one comparison returns a \"not-equal\". For example, the default
  539. '(time-up category-keep priority-down)
  540. means: Pull out all entries having a specified time of day and sort them,
  541. in order to make a time schedule for the current day the first thing in the
  542. agenda listing for the day. Of the entries without a time indication, keep
  543. the grouped in categories, don't sort the categories, but keep them in
  544. the sequence given in `org-agenda-files'. Within each category sort by
  545. priority.
  546. Leaving out `category-keep' would mean that items will be sorted across
  547. categories by priority."
  548. :group 'org-agenda
  549. :type '(repeat
  550. (choice
  551. (const time-up)
  552. (const time-down)
  553. (const category-keep)
  554. (const category-up)
  555. (const category-down)
  556. (const priority-up)
  557. (const priority-down))))
  558. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  559. "Format specification for the prefix of items in the agenda buffer.
  560. This format works similar to a printf format, with the following meaning:
  561. %c the category of the item, \"Diary\" for entries from the diary, or
  562. as given by the CATEGORY keyword or derived from the file name.
  563. %T the first tag of the item.
  564. %t the time-of-day specification if one applies to the entry, in the
  565. format HH:MM
  566. %s Scheduling/Deadline information, a short string
  567. All specifiers work basically like the standard `%s' of printf, but may
  568. contain two additional characters: A question mark just after the `%' and
  569. a whitespace/punctuation character just before the final letter.
  570. If the first character after `%' is a question mark, the entire field
  571. will only be included if the corresponding value applies to the
  572. current entry. This is useful for fields which should have fixed
  573. width when present, but zero width when absent. For example,
  574. \"%?-12t\" will result in a 12 character time field if a time of the
  575. day is specified, but will completely disappear in entries which do
  576. not contain a time.
  577. If there is punctuation or whitespace character just before the final
  578. format letter, this character will be appended to the field value if
  579. the value is not empty. For example, the format \"%-12:c\" leads to
  580. \"Diary: \" if the category is \"Diary\". If the category were be
  581. empty, no additional colon would be interted.
  582. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  583. - Indent the line with two space characters
  584. - Give the category in a 12 chars wide field, padded with whitespace on
  585. the right (because of `-'). Append a colon if there is a category
  586. (because of `:').
  587. - If there is a time-of-day, put it into a 12 chars wide field. If no
  588. time, don't put in an empty field, just skip it (because of '?').
  589. - Finally, put the scheduling information and append a whitespace.
  590. As another example, if you don't want the time-of-day of entries in
  591. the prefix, you could use:
  592. (setq org-agenda-prefix-format \" %-11:c% s\")
  593. See also the variables `org-agenda-remove-times-when-in-prefix' and
  594. `org-agenda-remove-tags-when-in-prefix'."
  595. :type 'string
  596. :group 'org-agenda)
  597. (defcustom org-timeline-prefix-format " % s"
  598. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  599. :type 'string
  600. :group 'org-agenda)
  601. (defvar org-prefix-format-compiled nil
  602. "The compiled version of the most recently used prefix format.
  603. Depending on which command was used last, this may be the compiled version
  604. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  605. (defcustom org-agenda-use-time-grid t
  606. "Non-nil means, show a time grid in the agenda schedule.
  607. A time grid is a set of lines for specific times (like every two hours between
  608. 8:00 and 20:00). The items scheduled for a day at specific times are
  609. sorted in between these lines.
  610. For details about when the grid will be shown, and what it will look like, see
  611. the variable `org-agenda-time-grid'."
  612. :group 'org-agenda
  613. :type 'boolean)
  614. (defcustom org-agenda-time-grid
  615. '((daily today require-timed)
  616. "----------------"
  617. (800 1000 1200 1400 1600 1800 2000))
  618. "The settings for time grid for agenda display.
  619. This is a list of three items. The first item is again a list. It contains
  620. symbols specifying conditions when the grid should be displayed:
  621. daily if the agenda shows a single day
  622. weekly if the agenda shows an entire week
  623. today show grid on current date, independent of daily/weekly display
  624. require-timed show grid only if at least on item has a time specification
  625. The second item is a string which will be places behing the grid time.
  626. The third item is a list of integers, indicating the times that should have
  627. a grid line."
  628. :group 'org-agenda
  629. :type
  630. '(list
  631. (set :greedy t :tag "Grid Display Options"
  632. (const :tag "Show grid in single day agenda display" daily)
  633. (const :tag "Show grid in weekly agenda display" weekly)
  634. (const :tag "Always show grid for today" today)
  635. (const :tag "Show grid only if any timed entries are present"
  636. require-timed)
  637. (const :tag "Skip grid times already present in an entry"
  638. remove-match))
  639. (string :tag "Grid String")
  640. (repeat :tag "Grid Times" (integer :tag "Time"))))
  641. (defcustom org-agenda-remove-times-when-in-prefix t
  642. "Non-nil means, remove duplicate time specifications in agenda items.
  643. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  644. time-of-day specification in a headline or diary entry is extracted and
  645. placed into the prefix. If this option is non-nil, the original specification
  646. \(a timestamp or -range, or just a plain time(range) specification like
  647. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  648. cluttered.
  649. The option can be t or nil. It may also be the symbol `beg', indicating
  650. that the time should only be removed what it is located at the beginning of
  651. the headline/diary entry."
  652. :group 'org-agenda
  653. :type '(choice
  654. (const :tag "Always" t)
  655. (const :tag "Never" nil)
  656. (const :tag "When at beginning of entry" beg)))
  657. (defcustom org-sort-agenda-notime-is-late t
  658. "Non-nil means, items without time are considered late.
  659. This is only relevant for sorting. When t, items which have no explicit
  660. time like 15:30 will be considered as 24:01, i.e. later than any items which
  661. do have a time. When nil, the default time is before 0:00. You can use this
  662. option to decide if the schedule for today should come before or after timeless
  663. agenda entries."
  664. :group 'org-agenda
  665. :type 'boolean)
  666. (defcustom org-agenda-remove-tags-when-in-prefix nil
  667. "Non-nil means, the tags from copy of headline in agenda.
  668. When this is the symbol `prefix', only remove tags when
  669. `org-agenda-prefix-format' contains a `%T' specifier."
  670. :group 'org-agenda
  671. :type '(choice
  672. (const :tag "Always" t)
  673. (const :tag "Never" nil)
  674. (const :tag "When prefix format contains %T" prefix)))
  675. (defgroup org-structure nil
  676. "Options concerning structure editing in Org-mode."
  677. :tag "Org Structure"
  678. :group 'org)
  679. (defcustom org-cycle-include-plain-lists nil
  680. "Non-nil means, include plain lists into visibility cycling.
  681. This means that during cycling, plain list items will *temporarily* be
  682. interpreted as outline headlines with a level given by 1000+i where i is the
  683. indentation of the bullet. In all other operations, plain list items are
  684. not seen as headlines. For example, you cannot assign a TODO keyword to
  685. such an item."
  686. :group 'org-structure
  687. :type 'boolean)
  688. (defcustom org-cycle-emulate-tab t
  689. "Where should `org-cycle' emulate TAB.
  690. nil Never
  691. white Only in completely white lines
  692. t Everywhere except in headlines"
  693. :group 'org-structure
  694. :type '(choice (const :tag "Never" nil)
  695. (const :tag "Only in completely white lines" white)
  696. (const :tag "Everywhere except in headlines" t)
  697. ))
  698. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  699. "Hook that is run after `org-cycle' has changed the buffer visibility.
  700. The function(s) in this hook must accept a single argument which indicates
  701. the new state that was set by the most recent `org-cycle' command. The
  702. argument is a symbol. After a global state change, it can have the values
  703. `overview', `content', or `all'. After a local state change, it can have
  704. the values `folded', `children', or `subtree'."
  705. :group 'org-structure
  706. :type 'hook)
  707. (defcustom org-highlight-sparse-tree-matches t
  708. "Non-nil means, highlight all matches that define a sparse tree.
  709. The highlights will automatically disappear the next time the buffer is
  710. changed by an edit command."
  711. :group 'org-structure
  712. :type 'boolean)
  713. (defcustom org-show-hierarchy-above t
  714. "Non-nil means, show full hierarchy when showing a spot in the tree.
  715. Turning this off makes sparse trees more compact, but also less clear."
  716. :group 'org-structure
  717. :type 'boolean)
  718. (defcustom org-show-following-heading t
  719. "Non-nil means, show heading following match in `org-occur'.
  720. When doing an `org-occur' it is useful to show the headline which
  721. follows the match, even if they do not match the regexp. This makes it
  722. easier to edit directly inside the sparse tree. However, if you use
  723. `org-occur' mainly as an overview, the following headlines are
  724. unnecessary clutter."
  725. :group 'org-structure
  726. :type 'boolean)
  727. (defcustom org-occur-hook '(org-first-headline-recenter)
  728. "Hook that is run after `org-occur' has constructed a sparse tree.
  729. This can be used to recenter the window to show as much of the structure
  730. as possible."
  731. :group 'org-structure
  732. :type 'hook)
  733. (defcustom org-odd-levels-only nil
  734. "Non-nil means, skip even levels and only use odd levels for the outline.
  735. This has the effect that two stars are being added/taken away in
  736. promotion/demotion commands. It also influences how levels are
  737. handled by the exporters.
  738. Changing it requires restart of `font-lock-mode' to become effective
  739. for fontification also in regions already fontified."
  740. :group 'org-structure
  741. :group 'org-font-lock
  742. :type 'boolean)
  743. (defcustom org-adapt-indentation t
  744. "Non-nil means, adapt indentation when promoting and demoting.
  745. When this is set and the *entire* text in an entry is indented, the
  746. indentation is increased by one space in a demotion command, and
  747. decreased by one in a promotion command. If any line in the entry
  748. body starts at column 0, indentation is not changed at all."
  749. :group 'org-structure
  750. :type 'boolean)
  751. (defcustom org-plain-list-ordered-item-terminator t
  752. "The character that makes a line with leading number an ordered list item.
  753. Valid values are ?. and ?\). To get both terminators, use t. While
  754. ?. may look nicer, it creates the danger that a line with leading
  755. number may be incorrectly interpreted as an item. ?\) therefore is
  756. the safe choice."
  757. :group 'org-structure
  758. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  759. (const :tag "paren like in \"2)\"" ?\))
  760. (const :tab "both" t)))
  761. (defcustom org-auto-renumber-ordered-lists t
  762. "Non-nil means, automatically renumber ordered plain lists.
  763. Renumbering happens when the sequence have been changed with
  764. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  765. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  766. :group 'org-structure
  767. :type 'boolean)
  768. (defcustom org-enable-fixed-width-editor t
  769. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  770. This currently only means, they are never auto-wrapped.
  771. When nil, such lines will be treated like ordinary lines.
  772. See also the QUOTE keyword."
  773. :group 'org-structure
  774. :type 'boolean)
  775. (defcustom org-archive-location "%s_archive::"
  776. "The location where subtrees should be archived.
  777. This string consists of two parts, separated by a double-colon.
  778. The first part is a file name - when omitted, archiving happens in the same
  779. file. %s will be replaced by the current file name (without directory part).
  780. Archiving to a different file is useful to keep archived entries from
  781. contributing to the Org-mode Agenda.
  782. The part after the double colon is a headline. The archived entries will be
  783. filed under that headline. When omitted, the subtrees are simply filed away
  784. at the end of the file, as top-level entries.
  785. Here are a few examples:
  786. \"%s_archive::\"
  787. If the current file is Projects.org, archive in file
  788. Projects.org_archive, as top-level trees. This is the default.
  789. \"::* Archived Tasks\"
  790. Archive in the current file, under the top-level headline
  791. \"* Archived Tasks\".
  792. \"~/org/archive.org::\"
  793. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  794. \"basement::** Finished Tasks\"
  795. Archive in file ./basement (relative path), as level 3 trees
  796. below the level 2 heading \"** Finished Tasks\".
  797. You may set this option on a per-file basis by adding to the buffer a
  798. line like
  799. #+ARCHIVE: basement::** Finished Tasks"
  800. :group 'org-structure
  801. :type 'string)
  802. (defcustom org-archive-mark-done t
  803. "Non-nil means, mark archived entries as DONE."
  804. :group 'org-structure
  805. :type 'boolean)
  806. (defcustom org-archive-stamp-time t
  807. "Non-nil means, add a time stamp to archived entries.
  808. The time stamp will be added directly after the TODO state keyword in the
  809. first line, so it is probably best to use this in combinations with
  810. `org-archive-mark-done'."
  811. :group 'org-structure
  812. :type 'boolean)
  813. (defgroup org-tags nil
  814. "Options concerning startup of Org-mode."
  815. :tag "Org Tags"
  816. :group 'org)
  817. (defcustom org-tags-column 48
  818. "The column to which tags should be indented in a headline.
  819. If this number is positive, it specifies the column. If it is negative,
  820. it means that the tags should be flushright to that column. For example,
  821. -79 works well for a normal 80 character screen."
  822. :group 'org-tags
  823. :type 'integer)
  824. (defcustom org-auto-align-tags t
  825. "Non-nil means, realign tags after pro/demotion of TODO state change.
  826. These operations change the length of a headline and therefore shift
  827. the tags around. With this options turned on, after each such operation
  828. the tags are again aligned to `org-tags-column'."
  829. :group 'org-tags
  830. :type 'boolean)
  831. (defcustom org-use-tag-inheritance t
  832. "Non-nil means, tags in levels apply also for sublevels.
  833. When nil, only the tags directly given in a specific line apply there.
  834. If you turn off this option, you very likely want to turn on the
  835. companion option `org-tags-match-list-sublevels'."
  836. :group 'org-tags
  837. :type 'boolean)
  838. (defcustom org-tags-match-list-sublevels nil
  839. "Non-nil means list also sublevels of headlines matching tag search.
  840. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  841. the sublevels of a headline matching a tag search often also match
  842. the same search. Listing all of them can create very long lists.
  843. Setting this variable to nil causes subtrees to be skipped.
  844. This option is off by default, because inheritance in on. If you turn
  845. inheritance off, you very likely want to turn this option on.
  846. As a special case, if the tag search is restricted to TODO items, the
  847. value of this variable is ignored and sublevels are always checked, to
  848. make sure all corresponding TODO items find their way into the list."
  849. :group 'org-tags
  850. :type 'boolean)
  851. (defvar org-tags-history nil
  852. "History of minibuffer reads for tags.")
  853. (defvar org-last-tags-completion-table nil
  854. "The last used completion table for tags.")
  855. (defgroup org-link nil
  856. "Options concerning links in Org-mode."
  857. :tag "Org Link"
  858. :group 'org)
  859. (defcustom org-tab-follows-link nil
  860. "Non-nil means, on links TAB will follow the link.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link
  863. :type 'boolean)
  864. (defcustom org-return-follows-link nil
  865. "Non-nil means, on links RET will follow the link.
  866. Needs to be set before org.el is loaded."
  867. :group 'org-link
  868. :type 'boolean)
  869. (defcustom org-mark-ring-length 4
  870. "Number of different positions to be recorded in the ring
  871. Changing this requires a restart of Emacs to work correctly."
  872. :group 'org-link
  873. :type 'interger)
  874. (defcustom org-link-format "<%s>"
  875. "Default format for linkes in the buffer.
  876. This is a format string for printf, %s will be replaced by the link text.
  877. If you want to make sure that your link is always properly terminated,
  878. include angle brackets into this format, like \"<%s>\". Some people also
  879. recommend an additional URL: prefix, so the format would be \"<URL:%s>\"."
  880. :group 'org-link
  881. :type '(choice
  882. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  883. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  884. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  885. (string :tag "Other" :value "<%s>")))
  886. (defcustom org-allow-space-in-links t
  887. "Non-nil means, file names in links may contain space characters.
  888. When nil, it becomes possible to put several links into a line.
  889. Note that in tables, a link never extends accross fields, so in a table
  890. it is always possible to put several links into a line.
  891. Changing this variable requires a restart of Emacs of become effective."
  892. :group 'org-link
  893. :type 'boolean)
  894. (defcustom org-radio-targets t
  895. "Non-nil means activate text matching a link target.
  896. Radio targets are strings in triple angular brackets, like <<<My Target>>>.
  897. When this option is set, any occurrence of \"my target\" in normal text
  898. becomes a link."
  899. :group 'org-link
  900. :type 'boolean)
  901. (defcustom org-activate-camels nil
  902. "Non-nil means, treat words in CamelCase as in-file links.
  903. Changing this requires restart of Emacs to become effective."
  904. :group 'org-link
  905. :type 'boolean)
  906. (defcustom org-context-in-file-links t
  907. "Non-nil means, file links from `org-store-link' contain context.
  908. A search string will be added to the file name with :: as separator and
  909. used to find the context when the link is activated by the command
  910. `org-open-at-point'.
  911. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  912. negates this setting for the duration of the command."
  913. :group 'org-link
  914. :type 'boolean)
  915. (defcustom org-file-link-context-use-camel-case nil
  916. "Non-nil means, use CamelCase to store a search context in a file link.
  917. When nil, the search string simply consists of the words of the string."
  918. :group 'org-link
  919. :type 'boolean)
  920. (defcustom org-keep-stored-link-after-insertion nil
  921. "Non-nil means, keep link in list for entire session.
  922. The command `org-store-link' adds a link pointing to the current
  923. location to an internal list. These links accumulate during a session.
  924. The command `org-insert-link' can be used to insert links into any
  925. Org-mode file (offering completion for all stored links). When this
  926. option is nil, every link which has been inserted once using \\[org-insert-link]
  927. will be removed from the list, to make completing the unused links
  928. more efficient."
  929. :group 'org-link
  930. :type 'boolean)
  931. (defcustom org-link-frame-setup
  932. '((vm . vm-visit-folder-other-frame)
  933. (gnus . gnus-other-frame)
  934. (file . find-file-other-window))
  935. "Setup the frame configuration for following links.
  936. When following a link with Emacs, it may often be useful to display
  937. this link in another window or frame. This variable can be used to
  938. set this up for the different types of links.
  939. For VM, use any of
  940. `vm-visit-folder'
  941. `vm-visit-folder-other-frame'
  942. For Gnus, use any of
  943. `gnus'
  944. `gnus-other-frame'
  945. For FILE, use any of
  946. `find-file'
  947. `find-file-other-window'
  948. `find-file-other-frame'
  949. For the calendar, use the variable `calendar-setup'.
  950. For BBDB, it is currently only possible to display the matches in
  951. another window."
  952. :group 'org-link
  953. :type '(list
  954. (cons (const vm)
  955. (choice
  956. (const vm-visit-folder)
  957. (const vm-visit-folder-other-window)
  958. (const vm-visit-folder-other-frame)))
  959. (cons (const gnus)
  960. (choice
  961. (const gnus)
  962. (const gnus-other-frame)))
  963. (cons (const file)
  964. (choice
  965. (const find-file)
  966. (const find-file-other-window)
  967. (const find-file-other-frame)))))
  968. (defcustom org-usenet-links-prefer-google nil
  969. "Non-nil means, `org-store-link' will create web links to Google groups.
  970. When nil, Gnus will be used for such links.
  971. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  972. negates this setting for the duration of the command."
  973. :group 'org-link
  974. :type 'boolean)
  975. (defcustom org-open-non-existing-files nil
  976. "Non-nil means, `org-open-file' will open non-existing file.
  977. When nil, an error will be generated."
  978. :group 'org-link
  979. :type 'boolean)
  980. (defcustom org-confirm-shell-links t
  981. "Non-nil means, ask for confirmation before executing shell links.
  982. The default is true, to keep new users from shooting into their own foot."
  983. :group 'org-link
  984. :type 'boolean)
  985. (defconst org-file-apps-defaults-gnu
  986. '((t . mailcap))
  987. "Default file applications on a UNIX or GNU/Linux system.
  988. See `org-file-apps'.")
  989. (defconst org-file-apps-defaults-macosx
  990. '((t . "open %s")
  991. ("ps" . "gv %s")
  992. ("ps.gz" . "gv %s")
  993. ("eps" . "gv %s")
  994. ("eps.gz" . "gv %s")
  995. ("dvi" . "xdvi %s")
  996. ("fig" . "xfig %s"))
  997. "Default file applications on a MacOS X system.
  998. The system \"open\" is known as a default, but we use X11 applications
  999. for some files for which the OS does not have a good default.
  1000. See `org-file-apps'.")
  1001. (defconst org-file-apps-defaults-windowsnt
  1002. '((t . (w32-shell-execute "open" file)))
  1003. "Default file applications on a Windows NT system.
  1004. The system \"open\" is used for most files.
  1005. See `org-file-apps'.")
  1006. (defcustom org-file-apps
  1007. '(
  1008. ("txt" . emacs)
  1009. ("tex" . emacs)
  1010. ("ltx" . emacs)
  1011. ("org" . emacs)
  1012. ("el" . emacs)
  1013. )
  1014. "External applications for opening `file:path' items in a document.
  1015. Org-mode uses system defaults for different file types, but
  1016. you can use this variable to set the application for a given file
  1017. extension. The entries in this list are cons cells with a file extension
  1018. and the corresponding command. Possible values for the command are:
  1019. `emacs' The file will be visited by the current Emacs process.
  1020. `default' Use the default application for this file type.
  1021. string A command to be executed by a shell; %s will be replaced
  1022. by the path to the file.
  1023. sexp A Lisp form which will be evaluated. The file path will
  1024. be available in the Lisp variable `file'.
  1025. For more examples, see the system specific constants
  1026. `org-file-apps-defaults-macosx'
  1027. `org-file-apps-defaults-windowsnt'
  1028. `org-file-apps-defaults-gnu'."
  1029. :group 'org-link
  1030. :type '(repeat
  1031. (cons (choice :value ""
  1032. (string :tag "Extension")
  1033. (const :tag "Default for unrecognized files" t)
  1034. (const :tag "Links to a directory" directory))
  1035. (choice :value ""
  1036. (const :tag "Visit with Emacs" emacs)
  1037. (const :tag "Use system default" default)
  1038. (string :tag "Command")
  1039. (sexp :tag "Lisp form")))))
  1040. (defcustom org-mhe-search-all-folders nil
  1041. "Non-nil means, that the search for the mh-message will be extended to
  1042. all folders if the message cannot be found in the folder given in the link.
  1043. Searching all folders is very effective with one of the search engines
  1044. supported by MH-E, but will be slow with pick."
  1045. :group 'org-link
  1046. :type 'boolean)
  1047. (defgroup org-remember nil
  1048. "Options concerning interaction with remember.el."
  1049. :tag "Org Remember"
  1050. :group 'org)
  1051. (defcustom org-directory "~/org"
  1052. "Directory with org files.
  1053. This directory will be used as default to prompt for org files.
  1054. Used by the hooks for remember.el."
  1055. :group 'org-remember
  1056. :type 'directory)
  1057. (defcustom org-default-notes-file "~/.notes"
  1058. "Default target for storing notes.
  1059. Used by the hooks for remember.el. This can be a string, or nil to mean
  1060. the value of `remember-data-file'."
  1061. :group 'org-remember
  1062. :type '(choice
  1063. (const :tag "Default from remember-data-file" nil)
  1064. file))
  1065. (defcustom org-remember-templates nil
  1066. "Templates for the creation of remember buffers.
  1067. When nil, just let remember make the buffer.
  1068. When not nil, this is a list of 3-element lists. In each entry, the first
  1069. element is a character, a unique key to select this template.
  1070. The second element is the template. The third element is optional and can
  1071. specify a destination file for remember items created with this template.
  1072. The default file is given by `org-default-notes-file'.
  1073. The template specifies the structure of the remember buffer. It should have
  1074. a first line starting with a star, to act as the org-mode headline.
  1075. Furthermore, the following %-escapes will be replaced with content:
  1076. %t time stamp, date only
  1077. %T time stamp with date and time
  1078. %u inactive time stamp, date only
  1079. %U inactive time stamp with date and time
  1080. %n user name
  1081. %a annotation, normally the link created with org-store-link
  1082. %i initial content, the region when remember is called with C-u.
  1083. If %i is indented, the entire inserted text will be indented as well.
  1084. %? This will be removed, and the cursor placed at this position."
  1085. :group 'org-remember
  1086. :type '(repeat :tag "enabled"
  1087. (list :value (?a "\n" nil)
  1088. (character :tag "Selection Key")
  1089. (string :tag "Template")
  1090. (file :tag "Destination file (optional)"))))
  1091. (defcustom org-reverse-note-order nil
  1092. "Non-nil means, store new notes at the beginning of a file or entry.
  1093. When nil, new notes will be filed to the end of a file or entry."
  1094. :group 'org-remember
  1095. :type '(choice
  1096. (const :tag "Reverse always" t)
  1097. (const :tag "Reverse never" nil)
  1098. (repeat :tag "By file name regexp"
  1099. (cons regexp boolean))))
  1100. (defgroup org-table nil
  1101. "Options concerning tables in Org-mode."
  1102. :tag "Org Table"
  1103. :group 'org)
  1104. (defcustom org-enable-table-editor 'optimized
  1105. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  1106. When nil, such lines will be treated like ordinary lines.
  1107. When equal to the symbol `optimized', the table editor will be optimized to
  1108. do the following:
  1109. - Use automatic overwrite mode in front of whitespace in table fields.
  1110. This make the structure of the table stay in tact as long as the edited
  1111. field does not exceed the column width.
  1112. - Minimize the number of realigns. Normally, the table is aligned each time
  1113. TAB or RET are pressed to move to another field. With optimization this
  1114. happens only if changes to a field might have changed the column width.
  1115. Optimization requires replacing the functions `self-insert-command',
  1116. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1117. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1118. very good at guessing when a re-align will be necessary, but you can always
  1119. force one with \\[org-ctrl-c-ctrl-c].
  1120. If you would like to use the optimized version in Org-mode, but the
  1121. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1122. This variable can be used to turn on and off the table editor during a session,
  1123. but in order to toggle optimization, a restart is required.
  1124. See also the variable `org-table-auto-blank-field'."
  1125. :group 'org-table
  1126. :type '(choice
  1127. (const :tag "off" nil)
  1128. (const :tag "on" t)
  1129. (const :tag "on, optimized" optimized)))
  1130. ;; FIXME: We could have a third option which makes it jump only over the first
  1131. ;; hline in a table.
  1132. (defcustom org-table-tab-jumps-over-hlines t
  1133. "Non-nil means, tab in the last column of a table with jump over a hline.
  1134. If a horizontal separator line is following the current line,
  1135. `org-table-next-field' can either create a new row before that line, or jump
  1136. over the line. When this option is nil, a new line will be created before
  1137. this line."
  1138. :group 'org-table
  1139. :type 'boolean)
  1140. (defcustom org-table-auto-blank-field t
  1141. "Non-nil means, automatically blank table field when starting to type into it.
  1142. This only happens when typing immediately after a field motion
  1143. command (TAB, S-TAB or RET).
  1144. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  1145. :group 'org-table
  1146. :type 'boolean)
  1147. (defcustom org-table-default-size "5x2"
  1148. "The default size for newly created tables, Columns x Rows."
  1149. :group 'org-table
  1150. :type 'string)
  1151. (defcustom org-table-automatic-realign t
  1152. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  1153. When nil, aligning is only done with \\[org-table-align], or after column
  1154. removal/insertion."
  1155. :group 'org-table
  1156. :type 'boolean)
  1157. (defcustom org-table-spaces-around-separators '(1 . 1)
  1158. "The number of spaces to be placed before and after separators."
  1159. :group 'org-table
  1160. :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
  1161. (defcustom org-table-spaces-around-invisible-separators '(1 . 2)
  1162. "The number of spaces to be placed before and after separators.
  1163. This option applies when the column separators have been made invisible."
  1164. :group 'org-table
  1165. :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
  1166. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  1167. "Regular expression for recognizing numbers in table columns.
  1168. If a table column contains mostly numbers, it will be aligned to the
  1169. right. If not, it will be aligned to the left.
  1170. The default value of this option is a regular expression which allows
  1171. anything which looks remotely like a number as used in scientific
  1172. context. For example, all of the following will be considered a
  1173. number:
  1174. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  1175. Other options offered by the customize interface are more restrictive."
  1176. :group 'org-table
  1177. :type '(choice
  1178. (const :tag "Positive Integers"
  1179. "^[0-9]+$")
  1180. (const :tag "Integers"
  1181. "^[-+]?[0-9]+$")
  1182. (const :tag "Floating Point Numbers"
  1183. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  1184. (const :tag "Floating Point Number or Integer"
  1185. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  1186. (const :tag "Exponential, Floating point, Integer"
  1187. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  1188. (const :tag "Very General Number-Like"
  1189. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  1190. (string :tag "Regexp:")))
  1191. (defcustom org-table-number-fraction 0.5
  1192. "Fraction of numbers in a column required to make the column align right.
  1193. In a column all non-white fields are considered. If at least this
  1194. fraction of fields is matched by `org-table-number-fraction',
  1195. alignment to the right border applies."
  1196. :group 'org-table
  1197. :type 'number)
  1198. (defcustom org-export-highlight-first-table-line t
  1199. "Non-nil means, highlight the first table line.
  1200. In HTML export, this means use <th> instead of <td>.
  1201. In tables created with table.el, this applies to the first table line.
  1202. In Org-mode tables, all lines before the first horizontal separator
  1203. line will be formatted with <th> tags."
  1204. :group 'org-table
  1205. :type 'boolean)
  1206. (defcustom org-table-tab-recognizes-table.el t
  1207. "Non-nil means, TAB will automatically notice a table.el table.
  1208. When it sees such a table, it moves point into it and - if necessary -
  1209. calls `table-recognize-table'."
  1210. :group 'org-table
  1211. :type 'boolean)
  1212. (defgroup org-table-calculation nil
  1213. "Options concerning tables in Org-mode."
  1214. :tag "Org Table Calculation"
  1215. :group 'org)
  1216. (defcustom org-table-copy-increment t
  1217. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  1218. :group 'org-table-calculation
  1219. :type 'boolean)
  1220. (defcustom org-calc-default-modes
  1221. '(calc-internal-prec 12
  1222. calc-float-format (float 5)
  1223. calc-angle-mode deg
  1224. calc-prefer-frac nil
  1225. calc-symbolic-mode nil
  1226. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  1227. calc-display-working-message t
  1228. )
  1229. "List with Calc mode settings for use in calc-eval for table formulas.
  1230. The list must contain alternating symbols (Calc modes variables and values).
  1231. Don't remove any of the default settings, just change the values. Org-mode
  1232. relies on the variables to be present in the list."
  1233. :group 'org-table-calculation
  1234. :type 'plist)
  1235. (defcustom org-table-formula-evaluate-inline t
  1236. "Non-nil means, TAB and RET evaluate a formula in current table field.
  1237. If the current field starts with an equal sign, it is assumed to be a formula
  1238. which should be evaluated as described in the manual and in the documentation
  1239. string of the command `org-table-eval-formula'. This feature requires the
  1240. Emacs calc package.
  1241. When this variable is nil, formula calculation is only available through
  1242. the command \\[org-table-eval-formula]."
  1243. :group 'org-table-calculation
  1244. :type 'boolean)
  1245. (defcustom org-table-formula-use-constants t
  1246. "Non-nil means, interpret constants in formulas in tables.
  1247. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  1248. by the value given in `org-table-formula-constants', or by a value obtained
  1249. from the `constants.el' package."
  1250. :group 'org-table-calculation
  1251. :type 'boolean)
  1252. (defcustom org-table-formula-constants nil
  1253. "Alist with constant names and values, for use in table formulas.
  1254. The car of each element is a name of a constant, without the `$' before it.
  1255. The cdr is the value as a string. For example, if you'd like to use the
  1256. speed of light in a formula, you would configure
  1257. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  1258. and then use it in an equation like `$1*$c'."
  1259. :group 'org-table-calculation
  1260. :type '(repeat
  1261. (cons (string :tag "name")
  1262. (string :tag "value"))))
  1263. (defcustom org-table-formula-numbers-only nil
  1264. "Non-nil means, calculate only with numbers in table formulas.
  1265. Then all input fields will be converted to a number, and the result
  1266. must also be a number. When nil, calc's full potential is available
  1267. in table calculations, including symbolics etc."
  1268. :group 'org-table-calculation
  1269. :type 'boolean)
  1270. (defcustom org-table-allow-automatic-line-recalculation t
  1271. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  1272. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  1273. :group 'org-table-calculation
  1274. :type 'boolean)
  1275. (defgroup org-export nil
  1276. "Options for exporting org-listings."
  1277. :tag "Org Export"
  1278. :group 'org)
  1279. (defcustom org-export-language-setup
  1280. '(("en" "Author" "Date" "Table of Contents")
  1281. ("da" "Ophavsmand" "Dato" "Indhold")
  1282. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1283. ("es" "Autor" "Fecha" "\xccndice")
  1284. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1285. ("it" "Autore" "Data" "Indice")
  1286. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1287. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1288. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1289. "Terms used in export text, translated to different languages.
  1290. Use the variable `org-export-default-language' to set the language,
  1291. or use the +OPTION lines for a per-file setting."
  1292. :group 'org-export
  1293. :type '(repeat
  1294. (list
  1295. (string :tag "HTML language tag")
  1296. (string :tag "Author")
  1297. (string :tag "Date")
  1298. (string :tag "Table of Contents"))))
  1299. (defcustom org-export-default-language "en"
  1300. "The default language of HTML export, as a string.
  1301. This should have an association in `org-export-language-setup'."
  1302. :group 'org-export
  1303. :type 'string)
  1304. (defcustom org-export-html-style
  1305. "<style type=\"text/css\">
  1306. html {
  1307. font-family: Times, serif;
  1308. font-size: 12pt;
  1309. }
  1310. .title { text-align: center; }
  1311. .todo, .deadline { color: red; }
  1312. .done { color: green; }
  1313. .target { background-color: lavender; }
  1314. pre {
  1315. border: 1pt solid #AEBDCC;
  1316. background-color: #F3F5F7;
  1317. padding: 5pt;
  1318. font-family: courier, monospace;
  1319. }
  1320. table { border-collapse: collapse; }
  1321. td, th {
  1322. vertical-align: top;
  1323. border: 1pt solid #ADB9CC;
  1324. }
  1325. </style>"
  1326. "The default style specification for exported HTML files.
  1327. Since there are different ways of setting style information, this variable
  1328. needs to contain the full HTML structure to provide a style, including the
  1329. surrounding HTML tags. The style specifications should include definitions
  1330. for new classes todo, done, title, and deadline. For example, legal values
  1331. would be:
  1332. <style type=\"text/css\">
  1333. p { font-weight: normal; color: gray; }
  1334. h1 { color: black; }
  1335. .title { text-align: center; }
  1336. .todo, .deadline { color: red; }
  1337. .done { color: green; }
  1338. </style>
  1339. or, if you want to keep the style in a file,
  1340. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1341. As the value of this option simply gets inserted into the HTML <head> header,
  1342. you can \"misuse\" it to add arbitrary text to the header."
  1343. :group 'org-export
  1344. :type 'string)
  1345. (defcustom org-export-headline-levels 3
  1346. "The last level which is still exported as a headline.
  1347. Inferior levels will produce itemize lists when exported.
  1348. Note that a numeric prefix argument to an exporter function overrides
  1349. this setting.
  1350. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1351. :group 'org-export
  1352. :type 'number)
  1353. (defcustom org-export-with-section-numbers t
  1354. "Non-nil means, add section numbers to headlines when exporting.
  1355. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1356. :group 'org-export
  1357. :type 'boolean)
  1358. (defcustom org-export-with-toc t
  1359. "Non-nil means, create a table of contents in exported files.
  1360. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1361. Headlines which contain any TODO items will be marked with \"(*)\" in
  1362. ASCII export, and with red color in HTML output.
  1363. In HTML output, the TOC will be clickable.
  1364. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1365. :group 'org-export
  1366. :type 'boolean)
  1367. (defcustom org-export-plain-list-max-depth 20
  1368. "Maximum depth of hand-formatted lists in HTML export.
  1369. Org-mode parses hand-formatted enumeration and bullet lists and
  1370. transforms them to HTML open export. Different indentation of the
  1371. bullet or number indicates different list nesting levels. To avoid
  1372. confusion, only a single level is allowed by default. When this is
  1373. larger than 1, deeper indentation leads to deeper list nesting. For
  1374. example, the default value of 3 allows the following list to be
  1375. formatted correctly in HTML:
  1376. * Fruit
  1377. - Apple
  1378. - Banana
  1379. 1. from Africa
  1380. 2. from South America
  1381. - Pineapple
  1382. * Bread
  1383. * Dairy products"
  1384. :group 'org-export
  1385. :type 'integer)
  1386. (defcustom org-export-preserve-breaks nil
  1387. "Non-nil means, preserve all line breaks when exporting.
  1388. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1389. export, line breaks will always be preserved, regardless of this variable.
  1390. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1391. :group 'org-export
  1392. :type 'boolean)
  1393. (defcustom org-export-html-inline-images t
  1394. "Non-nil means, inline images into exported HTML pages.
  1395. The link will still be to the original location of the image file.
  1396. So if you are moving the page, lets say to your public HTML site,
  1397. you will have to move the image and maybe change the link."
  1398. :group 'org-export
  1399. :type 'boolean)
  1400. (defcustom org-export-html-expand t
  1401. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1402. When nil, these tags will be exported as plain text and therefore
  1403. not be interpreted by a browser.
  1404. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1405. :group 'org-export
  1406. :type 'boolean)
  1407. (defcustom org-export-with-fixed-width t
  1408. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1409. This can be used to have pre-formatted text, fragments of code etc. For
  1410. example:
  1411. : ;; Some Lisp examples
  1412. : (while (defc cnt)
  1413. : (ding))
  1414. will be looking just like this in also HTML. In ASCII export, this option
  1415. has no effect.
  1416. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1417. :group 'org-export
  1418. :type 'boolean)
  1419. (defcustom org-export-with-tables t
  1420. "If non-nil, lines starting with \"|\" define a table.
  1421. For example:
  1422. | Name | Address | Birthday |
  1423. |-------------+----------+-----------|
  1424. | Arthur Dent | England | 29.2.2100 |
  1425. In ASCII export, this option has no effect.
  1426. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1427. :group 'org-export
  1428. :type 'boolean)
  1429. (defcustom org-export-table-remove-special-lines t
  1430. "Remove special lines and marking characters in calculating tables.
  1431. This removes the special marking character column from tables that are set
  1432. up for spreadsheet calculations. It also removes the entire lines
  1433. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1434. the values of constants may be useful to have."
  1435. :group 'org-export
  1436. :type 'boolean)
  1437. (defcustom org-export-prefer-native-exporter-for-tables nil
  1438. "Non-nil means, always export tables created with table.el natively.
  1439. Natively means, use the HTML code generator in table.el.
  1440. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1441. the table does not use row- or column-spanning). This has the
  1442. advantage, that the automatic HTML conversions for math symbols and
  1443. sub/superscripts can be applied. Org-mode's HTML generator is also
  1444. much faster."
  1445. :group 'org-export
  1446. :type 'boolean)
  1447. (defcustom org-export-html-table-tag
  1448. "<table border=1 cellspacing=0 cellpadding=6>"
  1449. "The HTML tag used to start a table.
  1450. This must be a <table> tag, but you may change the options like
  1451. borders and spacing."
  1452. :group 'org-export
  1453. :type 'string)
  1454. (defcustom org-export-with-emphasize t
  1455. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1456. If the export target supports emphasizing text, the word will be
  1457. typeset in bold, italic, or underlined, respectively. Works only for
  1458. single words, but you can say: I *really* *mean* *this*.
  1459. In ASCII export, this option has no effect.
  1460. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1461. :group 'org-export
  1462. :type 'boolean)
  1463. (defcustom org-match-sexp-depth 3
  1464. "Number of stacked braces for sub/superscript matching.
  1465. This has to be set before loading org.el to be effective."
  1466. :group 'org-export
  1467. :type 'integer)
  1468. ;; FIXME: Should () parens be removed as well in sub/superscripts?
  1469. (defcustom org-export-with-sub-superscripts t
  1470. "Non-nil means, interpret \"_\" and \"^\" for export.
  1471. When this option is turned on, you can use TeX-like syntax for sub- and
  1472. superscripts. Several characters after \"_\" or \"^\" will be
  1473. considered as a single item - so grouping with {} is normally not
  1474. needed. For example, the following things will be parsed as single
  1475. sub- or superscripts.
  1476. 10^24 or 10^tau several digits will be considered 1 item.
  1477. 10^-12 or 10^-tau a leading sign with digits or a word
  1478. x^2-y^3 will be read as x^2 - y^3, because items are
  1479. terminated by almost any nonword/nondigit char.
  1480. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1481. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1482. sub/superscript.
  1483. In ASCII export, this option has no effect.
  1484. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1485. :group 'org-export
  1486. :type 'boolean)
  1487. (defcustom org-export-with-TeX-macros t
  1488. "Non-nil means, interpret simple TeX-like macros when exporting.
  1489. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1490. No only real TeX macros will work here, but the standard HTML entities
  1491. for math can be used as macro names as well. For a list of supported
  1492. names in HTML export, see the constant `org-html-entities'.
  1493. In ASCII export, this option has no effect.
  1494. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1495. :group 'org-export
  1496. :type 'boolean)
  1497. (defcustom org-export-html-with-timestamp nil
  1498. "If non-nil, write `org-export-html-html-helper-timestamp'
  1499. into the exported HTML text. Otherwise, the buffer will just be saved
  1500. to a file."
  1501. :group 'org-export
  1502. :type 'boolean)
  1503. (defcustom org-export-html-html-helper-timestamp
  1504. "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
  1505. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1506. :group 'org-export
  1507. :type 'string)
  1508. (defcustom org-export-ascii-show-new-buffer t
  1509. "Non-nil means, popup buffer containing the exported ASCII text.
  1510. Otherwise the buffer will just be saved to a file and stay hidden."
  1511. :group 'org-export
  1512. :type 'boolean)
  1513. (defcustom org-export-html-show-new-buffer nil
  1514. "Non-nil means, popup buffer containing the exported html text.
  1515. Otherwise, the buffer will just be saved to a file and stay hidden."
  1516. :group 'org-export
  1517. :type 'boolean)
  1518. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1519. "The file name for the iCalendar file covering all agenda files.
  1520. This file is created with the command \\[org-export-icalendar-all-agenda-files]."
  1521. :group 'org-export
  1522. :type 'file)
  1523. (defcustom org-icalendar-include-todo nil
  1524. "Non-nil means, export to iCalendar files should also cover TODO items."
  1525. :group 'org-export
  1526. :type 'boolean)
  1527. (defcustom org-icalendar-combined-name "OrgMode"
  1528. "Calendar name for the combined iCalendar representing all agenda files."
  1529. :group 'org-export
  1530. :type 'string)
  1531. (defgroup org-font-lock nil
  1532. "Faces and settings for highlighting in Org-mode."
  1533. :tag "Org Font Lock"
  1534. :group 'org)
  1535. (defcustom org-level-color-stars-only nil
  1536. "Non-nil means fontify only the stars in each headline.
  1537. When nil, the entire headline is fontified.
  1538. Changing it requires restart of `font-lock-mode' to become effective
  1539. also in regions already fontified."
  1540. :group 'org-font-lock
  1541. :type 'boolean)
  1542. (defcustom org-hide-leading-stars nil
  1543. "Non-nil means, hide the first N-1 stars in a headline.
  1544. This works by using the face `org-hide' for these stars. This
  1545. face is white for a light background, and black for a dark
  1546. background. You may have to customize the face `org-hide' to
  1547. make this work.
  1548. Changing it requires restart of `font-lock-mode' to become effective
  1549. also in regions already fontified."
  1550. :group 'org-font-lock
  1551. :type 'boolean)
  1552. (defcustom org-fontify-done-headline nil
  1553. "Non-nil means, change the face of a headline if it is marked DONE.
  1554. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1555. When this is non-nil, the headline after the keyword is set to the
  1556. `org-headline-done' as an additional indication."
  1557. :group 'org-font-lock
  1558. :type 'boolean)
  1559. (defcustom org-fontify-emphasized-text t
  1560. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1561. Changing this variable requires a restart of Emacs to take effect."
  1562. :group 'org-font-lock
  1563. :type 'boolean)
  1564. (defface org-hide
  1565. '(
  1566. (((type tty) (class color)) (:foreground "white"))
  1567. (((class color) (background light)) (:foreground "white"))
  1568. (((class color) (background dark)) (:foreground "black"))
  1569. ; (((class color) (backgro6und light)) (:foreground "grey90"))
  1570. ; (((class color) (background dark)) (:foreground "grey10"))
  1571. (t (:inverse-video nil)))
  1572. "Face used for level 1 headlines."
  1573. :group 'org-font-lock)
  1574. (defface org-level-1 ;; font-lock-function-name-face
  1575. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1576. (((class color) (background light)) (:foreground "Blue"))
  1577. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1578. (t (:inverse-video t :bold t)))
  1579. "Face used for level 1 headlines."
  1580. :group 'org-font-lock)
  1581. (defface org-level-2 ;; font-lock-variable-name-face
  1582. '((((type tty) (class color)) (:foreground "yellow" :weight light))
  1583. (((class color) (background light)) (:foreground "DarkGoldenrod"))
  1584. (((class color) (background dark)) (:foreground "LightGoldenrod"))
  1585. (t (:bold t :italic t)))
  1586. "Face used for level 2 headlines."
  1587. :group 'org-font-lock)
  1588. (defface org-level-3 ;; font-lock-keyword-face
  1589. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1590. (((class color) (background light)) (:foreground "Purple"))
  1591. (((class color) (background dark)) (:foreground "Cyan"))
  1592. (t (:bold t)))
  1593. "Face used for level 3 headlines."
  1594. :group 'org-font-lock)
  1595. (defface org-level-4 ;; font-lock-comment-face
  1596. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1597. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1598. (((class color) (background light)) (:foreground "Firebrick"))
  1599. (((class color) (background dark)) (:foreground "chocolate1"))
  1600. (t (:bold t :italic t)))
  1601. "Face used for level 4 headlines."
  1602. :group 'org-font-lock)
  1603. (defface org-level-5 ;; font-lock-type-face
  1604. '((((type tty) (class color)) (:foreground "green"))
  1605. (((class color) (background light)) (:foreground "ForestGreen"))
  1606. (((class color) (background dark)) (:foreground "PaleGreen"))
  1607. (t (:bold t :underline t)))
  1608. "Face used for level 5 headlines."
  1609. :group 'org-font-lock)
  1610. (defface org-level-6 ;; font-lock-constant-face
  1611. '((((type tty) (class color)) (:foreground "magenta"))
  1612. (((class color) (background light)) (:foreground "CadetBlue"))
  1613. (((class color) (background dark)) (:foreground "Aquamarine"))
  1614. (t (:bold t :underline t)))
  1615. "Face used for level 6 headlines."
  1616. :group 'org-font-lock)
  1617. (defface org-level-7 ;; font-lock-builtin-face
  1618. '((((type tty) (class color)) (:foreground "blue" :weight light))
  1619. (((class color) (background light)) (:foreground "Orchid"))
  1620. (((class color) (background dark)) (:foreground "LightSteelBlue"))
  1621. (t (:bold t)))
  1622. "Face used for level 7 headlines."
  1623. :group 'org-font-lock)
  1624. (defface org-level-8 ;; font-lock-string-face
  1625. '((((type tty) (class color)) (:foreground "green"))
  1626. (((class color) (background light)) (:foreground "RosyBrown"))
  1627. (((class color) (background dark)) (:foreground "LightSalmon"))
  1628. (t (:italic t)))
  1629. "Face used for level 8 headlines."
  1630. :group 'org-font-lock)
  1631. (defface org-special-keyword ;; font-lock-string-face
  1632. '((((type tty) (class color)) (:foreground "green"))
  1633. (((class color) (background light)) (:foreground "RosyBrown"))
  1634. (((class color) (background dark)) (:foreground "LightSalmon"))
  1635. (t (:italic t)))
  1636. "Face used for special keywords."
  1637. :group 'org-font-lock)
  1638. (defface org-warning ;; font-lock-warning-face
  1639. '((((type tty) (class color)) (:foreground "red"))
  1640. (((class color) (background light)) (:foreground "Red" :bold t))
  1641. (((class color) (background dark)) (:foreground "Red1" :bold t))
  1642. ; (((class color) (background dark)) (:foreground "Pink" :bold t))
  1643. (t (:inverse-video t :bold t)))
  1644. "Face for deadlines and TODO keywords."
  1645. :group 'org-font-lock)
  1646. (defface org-headline-done ;; font-lock-string-face
  1647. '((((type tty) (class color)) (:foreground "green"))
  1648. (((class color) (background light)) (:foreground "RosyBrown"))
  1649. (((class color) (background dark)) (:foreground "LightSalmon"))
  1650. (t (:italic t)))
  1651. "Face used to indicate that a headline is DONE. See also the variable
  1652. `org-fontify-done-headline'."
  1653. :group 'org-font-lock)
  1654. ;; Inheritance does not yet work for xemacs. So we just copy...
  1655. (defface org-deadline-announce
  1656. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1657. (((class color) (background light)) (:foreground "Blue"))
  1658. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1659. (t (:inverse-video t :bold t)))
  1660. "Face for upcoming deadlines."
  1661. :group 'org-font-lock)
  1662. (defface org-scheduled-today
  1663. '((((type tty) (class color)) (:foreground "green"))
  1664. (((class color) (background light)) (:foreground "DarkGreen"))
  1665. (((class color) (background dark)) (:foreground "PaleGreen"))
  1666. (t (:bold t :underline t)))
  1667. "Face for items scheduled for a certain day."
  1668. :group 'org-font-lock)
  1669. (defface org-scheduled-previously
  1670. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1671. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1672. (((class color) (background light)) (:foreground "Firebrick"))
  1673. (((class color) (background dark)) (:foreground "chocolate1"))
  1674. (t (:bold t :italic t)))
  1675. "Face for items scheduled previously, and not yet done."
  1676. :group 'org-font-lock)
  1677. (defface org-formula
  1678. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1679. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1680. (((class color) (background light)) (:foreground "Firebrick"))
  1681. (((class color) (background dark)) (:foreground "chocolate1"))
  1682. (t (:bold t :italic t)))
  1683. "Face for formulas."
  1684. :group 'org-font-lock)
  1685. (defface org-link
  1686. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1687. (((class color) (background light)) (:foreground "Purple"))
  1688. (((class color) (background dark)) (:foreground "Cyan"))
  1689. (t (:bold t)))
  1690. "Face for links."
  1691. :group 'org-font-lock)
  1692. (defface org-tag
  1693. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1694. (((class color) (background light)) (:foreground "Purple" :weight bold))
  1695. (((class color) (background dark)) (:foreground "Cyan" :weight bold))
  1696. (t (:bold t)))
  1697. "Face for tags."
  1698. :group 'org-font-lock)
  1699. (defface org-done ;; font-lock-type-face
  1700. '((((type tty) (class color)) (:foreground "green"))
  1701. (((class color) (background light)) (:foreground "ForestGreen" :bold t))
  1702. (((class color) (background dark)) (:foreground "PaleGreen" :bold t))
  1703. (t (:bold t :underline t)))
  1704. "Face used for DONE."
  1705. :group 'org-font-lock)
  1706. (defface org-table ;; font-lock-function-name-face
  1707. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1708. (((class color) (background light)) (:foreground "Blue"))
  1709. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1710. (t (:inverse-video t :bold t)))
  1711. "Face used for tables."
  1712. :group 'org-font-lock)
  1713. (defface org-time-grid ;; font-lock-variable-name-face
  1714. '((((type tty) (class color)) (:foreground "yellow" :weight light))
  1715. (((class color) (background light)) (:foreground "DarkGoldenrod"))
  1716. (((class color) (background dark)) (:foreground "LightGoldenrod"))
  1717. (t (:bold t :italic t)))
  1718. "Face used for time grids."
  1719. :group 'org-font-lock)
  1720. (defvar org-level-faces
  1721. '(org-level-1 org-level-2 org-level-3 org-level-4
  1722. org-level-5 org-level-6 org-level-7 org-level-8
  1723. ))
  1724. (defvar org-n-levels (length org-level-faces))
  1725. (defun org-set-regexps-and-options ()
  1726. "Precompute regular expressions for current buffer."
  1727. (when (eq major-mode 'org-mode)
  1728. (let ((re (org-make-options-regexp
  1729. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  1730. "STARTUP" "ARCHIVE")))
  1731. (splitre "[ \t]+")
  1732. kwds int key value cat arch)
  1733. (save-excursion
  1734. (save-restriction
  1735. (widen)
  1736. (goto-char (point-min))
  1737. (while (re-search-forward re nil t)
  1738. (setq key (match-string 1) value (match-string 2))
  1739. (cond
  1740. ((equal key "CATEGORY")
  1741. (if (string-match "[ \t]+$" value)
  1742. (setq value (replace-match "" t t value)))
  1743. (setq cat (intern value)))
  1744. ((equal key "SEQ_TODO")
  1745. (setq int 'sequence
  1746. kwds (append kwds (org-split-string value splitre))))
  1747. ((equal key "PRI_TODO")
  1748. (setq int 'priority
  1749. kwds (append kwds (org-split-string value splitre))))
  1750. ((equal key "TYP_TODO")
  1751. (setq int 'type
  1752. kwds (append kwds (org-split-string value splitre))))
  1753. ((equal key "STARTUP")
  1754. (let ((opts (org-split-string value splitre))
  1755. (set '(("fold" org-startup-folded t)
  1756. ("nofold" org-startup-folded nil)
  1757. ("content" org-startup-folded content)
  1758. ("hidestars" org-hide-leading-stars t)
  1759. ("showstars" org-hide-leading-stars nil)
  1760. ("odd" org-odd-levels-only t)
  1761. ("oddeven" org-odd-levels-only nil)
  1762. ("dlcheck" org-startup-with-deadline-check t)
  1763. ("nodlcheck" org-startup-with-deadline-check nil)))
  1764. l var val)
  1765. (while (setq l (assoc (pop opts) set))
  1766. (setq var (nth 1 l) val (nth 2 l))
  1767. (set (make-local-variable var) val))))
  1768. ((equal key "ARCHIVE")
  1769. (string-match " *$" value)
  1770. (setq arch (replace-match "" t t value))
  1771. (remove-text-properties 0 (length arch)
  1772. '(face t fontified t) arch)))
  1773. )))
  1774. (and cat (set (make-local-variable 'org-category) cat))
  1775. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  1776. (and arch (set (make-local-variable 'org-archive-location) arch))
  1777. (and int (set (make-local-variable 'org-todo-interpretation) int)))
  1778. ;; Compute the regular expressions and other local variables
  1779. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  1780. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  1781. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  1782. (length org-scheduled-string)))
  1783. org-done-string
  1784. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  1785. org-todo-regexp
  1786. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  1787. "\\|") "\\)\\>")
  1788. org-not-done-regexp
  1789. (concat "\\<\\("
  1790. (mapconcat 'regexp-quote
  1791. (nreverse (cdr (reverse org-todo-keywords)))
  1792. "\\|")
  1793. "\\)\\>")
  1794. org-todo-line-regexp
  1795. (concat "^\\(\\*+\\)[ \t]*\\("
  1796. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  1797. "\\)? *\\(.*\\)")
  1798. org-nl-done-regexp
  1799. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  1800. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  1801. org-deadline-regexp (concat "\\<" org-deadline-string)
  1802. org-deadline-time-regexp
  1803. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  1804. org-deadline-line-regexp
  1805. (concat "\\<\\(" org-deadline-string "\\).*")
  1806. org-scheduled-regexp
  1807. (concat "\\<" org-scheduled-string)
  1808. org-scheduled-time-regexp
  1809. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  1810. (org-set-font-lock-defaults)))
  1811. ;; Tell the compiler about dynamically scoped variables,
  1812. ;; and variables from other packages
  1813. (defvar zmacs-regions) ; XEmacs regions
  1814. (defvar original-date) ; dynamically scoped in calendar
  1815. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  1816. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  1817. (defvar org-html-entities) ; defined later in this file
  1818. (defvar org-goto-start-pos) ; dynamically scoped parameter
  1819. (defvar org-time-was-given) ; dynamically scoped parameter
  1820. (defvar org-ts-what) ; dynamically scoped parameter
  1821. (defvar mark-active) ; Emacs only, not available in XEmacs.
  1822. (defvar timecnt) ; dynamically scoped parameter
  1823. (defvar levels-open) ; dynamically scoped parameter
  1824. (defvar title) ; dynamically scoped parameter
  1825. (defvar author) ; dynamically scoped parameter
  1826. (defvar email) ; dynamically scoped parameter
  1827. (defvar text) ; dynamically scoped parameter
  1828. (defvar entry) ; dynamically scoped parameter
  1829. (defvar date) ; dynamically scoped parameter
  1830. (defvar language) ; dynamically scoped parameter
  1831. (defvar options) ; dynamically scoped parameter
  1832. (defvar ans1) ; dynamically scoped parameter
  1833. (defvar ans2) ; dynamically scoped parameter
  1834. (defvar starting-day) ; local variable
  1835. (defvar include-all-loc) ; local variable
  1836. (defvar vm-message-pointer) ; from vm
  1837. (defvar vm-folder-directory) ; from vm
  1838. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  1839. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  1840. (defvar gnus-group-name) ; from gnus
  1841. (defvar gnus-article-current) ; from gnus
  1842. (defvar w3m-current-url) ; from w3m
  1843. (defvar mh-progs) ; from MH-E
  1844. (defvar mh-current-folder) ; from MH-E
  1845. (defvar mh-show-folder-buffer) ; from MH-E
  1846. (defvar mh-index-folder) ; from MH-E
  1847. (defvar mh-searcher) ; from MH-E
  1848. (defvar org-selected-point) ; dynamically scoped parameter
  1849. (defvar calendar-mode-map) ; from calendar.el
  1850. (defvar last-arg) ; local variable
  1851. (defvar remember-save-after-remembering) ; from remember.el
  1852. (defvar remember-data-file) ; from remember.el
  1853. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  1854. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  1855. ;;; Define the mode
  1856. (defvar org-mode-map (copy-keymap outline-mode-map)
  1857. "Keymap for Org-mode.")
  1858. (defvar org-struct-menu) ; defined later in this file
  1859. (defvar org-org-menu) ; defined later in this file
  1860. (defvar org-tbl-menu) ; defined later in this file
  1861. ;; We use a before-change function to check if a table might need
  1862. ;; an update.
  1863. (defvar org-table-may-need-update t
  1864. "Indicates that a table might need an update.
  1865. This variable is set by `org-before-change-function'.
  1866. `org-table-align' sets it back to nil.")
  1867. (defvar org-mode-hook nil)
  1868. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  1869. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  1870. ;;;###autoload
  1871. (define-derived-mode org-mode outline-mode "Org"
  1872. "Outline-based notes management and organizer, alias
  1873. \"Carsten's outline-mode for keeping track of everything.\"
  1874. Org-mode develops organizational tasks around a NOTES file which
  1875. contains information about projects as plain text. Org-mode is
  1876. implemented on top of outline-mode, which is ideal to keep the content
  1877. of large files well structured. It supports ToDo items, deadlines and
  1878. time stamps, which magically appear in the diary listing of the Emacs
  1879. calendar. Tables are easily created with a built-in table editor.
  1880. Plain text URL-like links connect to websites, emails (VM), Usenet
  1881. messages (Gnus), BBDB entries, and any files related to the project.
  1882. For printing and sharing of notes, an Org-mode file (or a part of it)
  1883. can be exported as a structured ASCII or HTML file.
  1884. The following commands are available:
  1885. \\{org-mode-map}"
  1886. (easy-menu-add org-org-menu)
  1887. (easy-menu-add org-tbl-menu)
  1888. (org-install-agenda-files-menu)
  1889. (setq outline-regexp "\\*+")
  1890. ; (setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  1891. (setq outline-level 'org-outline-level)
  1892. (if org-startup-truncated (setq truncate-lines t))
  1893. (org-set-regexps-and-options)
  1894. (set (make-local-variable 'font-lock-unfontify-region-function)
  1895. 'org-unfontify-region)
  1896. ;; Activate before-change-function
  1897. (set (make-local-variable 'org-table-may-need-update) t)
  1898. (org-add-hook 'before-change-functions 'org-before-change-function nil
  1899. 'local)
  1900. ;; Paragraphs and auto-filling
  1901. (org-set-autofill-regexps)
  1902. (org-update-radio-target-regexp)
  1903. ;; Settings for Calc embedded mode
  1904. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  1905. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  1906. (if (and org-insert-mode-line-in-empty-file
  1907. (interactive-p)
  1908. (= (point-min) (point-max)))
  1909. (insert " -*- mode: org -*-\n\n"))
  1910. ;; Get rid of Outline menus, they are not needed
  1911. ;; Need to do this here because define-derived-mode sets up
  1912. ;; the keymap so late.
  1913. (if (featurep 'xemacs)
  1914. (progn
  1915. (delete-menu-item '("Headings"))
  1916. (delete-menu-item '("Show"))
  1917. (delete-menu-item '("Hide"))
  1918. (set-menubar-dirty-flag))
  1919. (define-key org-mode-map [menu-bar headings] 'undefined)
  1920. (define-key org-mode-map [menu-bar hide] 'undefined)
  1921. (define-key org-mode-map [menu-bar show] 'undefined))
  1922. (unless org-inhibit-startup
  1923. (if org-startup-with-deadline-check
  1924. (call-interactively 'org-check-deadlines)
  1925. (cond
  1926. ((eq org-startup-folded t)
  1927. (org-cycle '(4)))
  1928. ((eq org-startup-folded 'content)
  1929. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  1930. (org-cycle '(4)) (org-cycle '(4))))))))
  1931. (defsubst org-current-line (&optional pos)
  1932. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  1933. (defun org-current-time ()
  1934. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  1935. (if (> org-time-stamp-rounding-minutes 0)
  1936. (let ((r org-time-stamp-rounding-minutes)
  1937. (time (decode-time)))
  1938. (apply 'encode-time
  1939. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  1940. (nthcdr 2 time))))
  1941. (current-time)))
  1942. ;; FIXME: Do we need to copy?
  1943. (defun org-string-props (string &rest properties)
  1944. "Add PROPERTIES to string."
  1945. (add-text-properties 0 (length string) properties string)
  1946. string)
  1947. ;;; Font-Lock stuff
  1948. (defvar org-mouse-map (make-sparse-keymap))
  1949. (define-key org-mouse-map
  1950. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  1951. (define-key org-mouse-map
  1952. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  1953. (define-key org-mouse-map [follow-link] 'mouse-face)
  1954. (when org-tab-follows-link
  1955. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  1956. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  1957. (when org-return-follows-link
  1958. (define-key org-mouse-map [(return)] 'org-open-at-point)
  1959. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  1960. (require 'font-lock)
  1961. (defconst org-non-link-chars "\t\n\r|<>\000")
  1962. (defconst org-link-regexp
  1963. (if org-allow-space-in-links
  1964. (concat
  1965. "\\(https?\\|ftp\\|mailto\\|file\\|news\\|bbdb\\|vm\\|wl\\|mhe\\|rmail\\|gnus\\|shell\\):\\([^" org-non-link-chars "]+[^ " org-non-link-chars "]\\)")
  1966. (concat
  1967. "\\(https?\\|ftp\\|mailto\\|file\\|news\\|bbdb\\|vm\\|wl\\|mhe\\|rmail\\|gnus\\|shell\\):\\([^ " org-non-link-chars "]+\\)")
  1968. )
  1969. "Regular expression for matching links.")
  1970. (defconst org-link-maybe-angles-regexp
  1971. (concat "<?\\(" org-link-regexp "\\)>?")
  1972. "Matches a link and optionally surrounding angle brackets.")
  1973. (defconst org-protected-link-regexp
  1974. (concat "\000" org-link-regexp "\000")
  1975. "Matches a link and optionally surrounding angle brackets.")
  1976. (defconst org-bracket-link-regexp
  1977. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  1978. "Matches a link in double brackets.")
  1979. (defconst org-ts-lengths
  1980. (cons (length (format-time-string (car org-time-stamp-formats)))
  1981. (length (format-time-string (cdr org-time-stamp-formats))))
  1982. "This holds the lengths of the two different time formats.")
  1983. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  1984. "Regular expression for fast time stamp matching.")
  1985. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  1986. "Regular expression for fast time stamp matching.")
  1987. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  1988. "Regular expression matching time strings for analysis.")
  1989. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  1990. "Regular expression matching time stamps, with groups.")
  1991. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  1992. "Regular expression matching a time stamp range.")
  1993. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  1994. org-ts-regexp "\\)?")
  1995. "Regular expression matching a time stamp or time stamp range.")
  1996. (defun org-activate-links (limit)
  1997. "Run through the buffer and add overlays to links."
  1998. (if (re-search-forward org-link-regexp limit t)
  1999. (progn
  2000. (add-text-properties (match-beginning 0) (match-end 0)
  2001. (list 'mouse-face 'highlight
  2002. 'keymap org-mouse-map))
  2003. t)))
  2004. (defun org-activate-links2 (limit)
  2005. "Run through the buffer and add overlays to links."
  2006. (if (re-search-forward org-bracket-link-regexp limit t)
  2007. (progn
  2008. (add-text-properties (match-beginning 0) (match-end 0)
  2009. (list 'mouse-face 'highlight
  2010. 'keymap org-mouse-map))
  2011. t)))
  2012. (defun org-activate-dates (limit)
  2013. "Run through the buffer and add overlays to dates."
  2014. (if (re-search-forward org-tsr-regexp limit t)
  2015. (progn
  2016. (add-text-properties (match-beginning 0) (match-end 0)
  2017. (list 'mouse-face 'highlight
  2018. 'keymap org-mouse-map))
  2019. t)))
  2020. (defvar org-target-link-regexp nil
  2021. "Regular expression matching radio targets in plain text.")
  2022. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2023. "Regular expression matching a link target.")
  2024. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2025. "Regular expression matching a link target.")
  2026. (defun org-activate-target-links (limit)
  2027. "Run through the buffer and add overlays to target matches."
  2028. (when (and org-radio-targets org-target-link-regexp)
  2029. (let ((case-fold-search t))
  2030. (if (re-search-forward org-target-link-regexp limit t)
  2031. (progn
  2032. (add-text-properties (match-beginning 0) (match-end 0)
  2033. (list 'mouse-face 'highlight
  2034. 'keymap org-mouse-map
  2035. 'org-linked-text t))
  2036. t)))))
  2037. (defun org-update-radio-target-regexp ()
  2038. "Find all radio targets in this file and update the regular expression."
  2039. (interactive)
  2040. (when org-radio-targets
  2041. (setq org-target-link-regexp
  2042. (org-make-target-link-regexp (org-all-targets 'radio)))
  2043. (font-lock-mode -1)
  2044. (font-lock-mode 1)))
  2045. (defun org-all-targets (&optional radio)
  2046. "Return a list of all targets in this file.
  2047. With optional argument RADIO, only find radio targets."
  2048. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2049. rtn)
  2050. (save-excursion
  2051. (goto-char (point-min))
  2052. (while (re-search-forward re nil t)
  2053. (add-to-list 'rtn (downcase
  2054. (if (fboundp 'match-string-no-properties)
  2055. (match-string-no-properties 1)
  2056. (match-string 1)))))
  2057. rtn)))
  2058. (defun org-make-target-link-regexp (targets)
  2059. "Make regular expression matching all strings in TARGETS.
  2060. The regular expression finds the targets also if there is a line break
  2061. between words."
  2062. (and targets
  2063. (concat
  2064. "\\<\\("
  2065. (mapconcat
  2066. (lambda (x)
  2067. (while (string-match " +" x)
  2068. (setq x (replace-match "\\s-+" t t x)))
  2069. x)
  2070. targets
  2071. "\\|")
  2072. "\\)\\>")))
  2073. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2074. "Matches CamelCase words, possibly with a star before it.")
  2075. (defun org-activate-camels (limit)
  2076. "Run through the buffer and add overlays to dates."
  2077. (if org-activate-camels
  2078. (if (re-search-forward org-camel-regexp limit t)
  2079. (progn
  2080. (add-text-properties (match-beginning 0) (match-end 0)
  2081. (list 'mouse-face 'highlight
  2082. 'keymap org-mouse-map))
  2083. t))))
  2084. (defun org-activate-tags (limit)
  2085. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2086. (progn
  2087. (add-text-properties (match-beginning 1) (match-end 1)
  2088. (list 'mouse-face 'highlight
  2089. 'keymap org-mouse-map))
  2090. t)))
  2091. (defun org-font-lock-level ()
  2092. (save-excursion
  2093. (org-back-to-heading t)
  2094. (- (match-end 0) (match-beginning 0))))
  2095. (defun org-outline-level ()
  2096. (save-excursion
  2097. (looking-at outline-regexp)
  2098. (if (match-beginning 1)
  2099. (+ (org-get-string-indentation (match-string 1)) 1000)
  2100. (- (match-end 0) (match-beginning 0)))))
  2101. (defvar org-font-lock-keywords nil)
  2102. (defun org-set-font-lock-defaults ()
  2103. (let* ((em org-fontify-emphasized-text)
  2104. (org-font-lock-extra-keywords
  2105. (list
  2106. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2107. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2108. '(org-activate-links (0 'org-link t))
  2109. '(org-activate-links2 (0 'org-link t))
  2110. '(org-activate-target-links (0 'org-link t))
  2111. '(org-activate-dates (0 'org-link t))
  2112. '(org-activate-camels (0 'org-link t))
  2113. '(org-activate-tags (1 'org-tag t))
  2114. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2115. '(1 'org-warning t))
  2116. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2117. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2118. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2119. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2120. (if em '("\\(\\W\\|^\\)\\(\\*\\w+\\*\\)\\(\\W\\|$\\)" 2 'bold))
  2121. (if em '("\\(\\W\\|^\\)\\(/\\w+/\\)\\(\\W\\|$\\)" 2 'italic))
  2122. (if em '("\\(\\W\\|^\\)\\(_\\w+_\\)\\(\\W\\|$\\)" 2 'underline))
  2123. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2124. "\\|" org-quote-string "\\)\\>")
  2125. '(1 'org-special-keyword t))
  2126. '("^#.*" (0 'font-lock-comment-face t))
  2127. (if org-fontify-done-headline
  2128. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2129. '(1 'org-done t) '(2 'org-headline-done t))
  2130. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2131. '(1 'org-done t)))
  2132. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2133. (1 'org-table t))
  2134. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2135. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2136. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2137. )))
  2138. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2139. ;; Now set the full font-lock-keywords
  2140. (set (make-local-variable 'org-font-lock-keywords)
  2141. org-font-lock-extra-keywords)
  2142. (set (make-local-variable 'font-lock-defaults)
  2143. '(org-font-lock-keywords t nil nil backward-paragraph))
  2144. (kill-local-variable 'font-lock-keywords) nil))
  2145. (defvar org-m nil)
  2146. (defvar org-l nil)
  2147. (defvar org-f nil)
  2148. (defun org-get-level-face (n)
  2149. "Get the right face for match N in font-lock matching of healdines."
  2150. (setq org-l (- (match-end 2) (match-beginning 1)))
  2151. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2152. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2153. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2154. (cond
  2155. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2156. ((eq n 2) org-f)
  2157. (t (if org-level-color-stars-only nil org-f))))
  2158. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2159. "Remove fontification and activation overlays from links."
  2160. (font-lock-default-unfontify-region beg end)
  2161. (let* ((buffer-undo-list t)
  2162. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2163. (inhibit-modification-hooks t)
  2164. deactivate-mark buffer-file-name buffer-file-truename)
  2165. (remove-text-properties beg end
  2166. '(mouse-face nil keymap nil org-linked-text nil))))
  2167. ;;; Visibility cycling
  2168. (defvar org-cycle-global-status nil)
  2169. (defvar org-cycle-subtree-status nil)
  2170. (defun org-cycle (&optional arg)
  2171. "Visibility cycling for Org-mode.
  2172. - When this function is called with a prefix argument, rotate the entire
  2173. buffer through 3 states (global cycling)
  2174. 1. OVERVIEW: Show only top-level headlines.
  2175. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2176. 3. SHOW ALL: Show everything.
  2177. - When point is at the beginning of a headline, rotate the subtree started
  2178. by this line through 3 different states (local cycling)
  2179. 1. FOLDED: Only the main headline is shown.
  2180. 2. CHILDREN: The main headline and the direct children are shown.
  2181. From this state, you can move to one of the children
  2182. and zoom in further.
  2183. 3. SUBTREE: Show the entire subtree, including body text.
  2184. - When there is a numeric prefix, go up to a heading with level ARG, do
  2185. a `show-subtree' and return to the previous cursor position. If ARG
  2186. is negative, go up that many levels.
  2187. - When point is not at the beginning of a headline, execute
  2188. `indent-relative', like TAB normally does. See the option
  2189. `org-cycle-emulate-tab' for details.
  2190. - Special case: if point is the the beginning of the buffer and there is
  2191. no headline in line 1, this function will act as if called with prefix arg."
  2192. (interactive "P")
  2193. (if (or (and (bobp) (not (looking-at outline-regexp)))
  2194. (equal arg '(4)))
  2195. ;; special case: use global cycling
  2196. (setq arg t))
  2197. (let ((outline-regexp
  2198. (if org-cycle-include-plain-lists
  2199. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2200. outline-regexp)))
  2201. (cond
  2202. ((org-at-table-p 'any)
  2203. ;; Enter the table or move to the next field in the table
  2204. (or (org-table-recognize-table.el)
  2205. (progn
  2206. (org-table-justify-field-maybe)
  2207. (org-table-next-field))))
  2208. ((eq arg t) ;; Global cycling
  2209. (cond
  2210. ((and (eq last-command this-command)
  2211. (eq org-cycle-global-status 'overview))
  2212. ;; We just created the overview - now do table of contents
  2213. ;; This can be slow in very large buffers, so indicate action
  2214. (message "CONTENTS...")
  2215. (save-excursion
  2216. ;; Visit all headings and show their offspring
  2217. (goto-char (point-max))
  2218. (catch 'exit
  2219. (while (and (progn (condition-case nil
  2220. (outline-previous-visible-heading 1)
  2221. (error (goto-char (point-min))))
  2222. t)
  2223. (looking-at outline-regexp))
  2224. (show-branches)
  2225. (if (bobp) (throw 'exit nil))))
  2226. (message "CONTENTS...done"))
  2227. (setq org-cycle-global-status 'contents)
  2228. (run-hook-with-args 'org-cycle-hook 'contents))
  2229. ((and (eq last-command this-command)
  2230. (eq org-cycle-global-status 'contents))
  2231. ;; We just showed the table of contents - now show everything
  2232. (show-all)
  2233. (message "SHOW ALL")
  2234. (setq org-cycle-global-status 'all)
  2235. (run-hook-with-args 'org-cycle-hook 'all))
  2236. (t
  2237. ;; Default action: go to overview
  2238. (hide-sublevels 1)
  2239. (message "OVERVIEW")
  2240. (setq org-cycle-global-status 'overview)
  2241. (run-hook-with-args 'org-cycle-hook 'overview))))
  2242. ((integerp arg)
  2243. ;; Show-subtree, ARG levels up from here.
  2244. (save-excursion
  2245. (org-back-to-heading)
  2246. (outline-up-heading (if (< arg 0) (- arg)
  2247. (- (funcall outline-level) arg)))
  2248. (org-show-subtree)))
  2249. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2250. ;; At a heading: rotate between three different views
  2251. (org-back-to-heading)
  2252. (let ((goal-column 0) eoh eol eos)
  2253. ;; First, some boundaries
  2254. (save-excursion
  2255. (org-back-to-heading)
  2256. (save-excursion
  2257. (beginning-of-line 2)
  2258. (while (and (not (eobp)) ;; this is like `next-line'
  2259. (get-char-property (1- (point)) 'invisible))
  2260. (beginning-of-line 2)) (setq eol (point)))
  2261. (outline-end-of-heading) (setq eoh (point))
  2262. (org-end-of-subtree t) (setq eos (point))
  2263. (outline-next-heading))
  2264. ;; Find out what to do next and set `this-command'
  2265. (cond
  2266. ((= eos eoh)
  2267. ;; Nothing is hidden behind this heading
  2268. (message "EMPTY ENTRY")
  2269. (setq org-cycle-subtree-status nil))
  2270. ((>= eol eos)
  2271. ;; Entire subtree is hidden in one line: open it
  2272. (org-show-entry)
  2273. (show-children)
  2274. (message "CHILDREN")
  2275. (setq org-cycle-subtree-status 'children)
  2276. (run-hook-with-args 'org-cycle-hook 'children))
  2277. ((and (eq last-command this-command)
  2278. (eq org-cycle-subtree-status 'children))
  2279. ;; We just showed the children, now show everything.
  2280. (org-show-subtree)
  2281. (message "SUBTREE")
  2282. (setq org-cycle-subtree-status 'subtree)
  2283. (run-hook-with-args 'org-cycle-hook 'subtree))
  2284. (t
  2285. ;; Default action: hide the subtree.
  2286. (hide-subtree)
  2287. (message "FOLDED")
  2288. (setq org-cycle-subtree-status 'folded)
  2289. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2290. ;; TAB emulation
  2291. (buffer-read-only (org-back-to-heading))
  2292. ((if (and (eq org-cycle-emulate-tab 'white)
  2293. (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
  2294. t
  2295. (eq org-cycle-emulate-tab t))
  2296. (if (and (looking-at "[ \n\r\t]")
  2297. (string-match "^[ \t]*$" (buffer-substring
  2298. (point-at-bol) (point))))
  2299. (progn
  2300. (beginning-of-line 1)
  2301. (and (looking-at "[ \t]+") (replace-match ""))))
  2302. (indent-relative))
  2303. (t (save-excursion
  2304. (org-back-to-heading)
  2305. (org-cycle))))))
  2306. (defun org-optimize-window-after-visibility-change (state)
  2307. "Adjust the window after a change in outline visibility.
  2308. This function is the default value of the hook `org-cycle-hook'."
  2309. (cond
  2310. ((eq state 'overview) (org-first-headline-recenter 1))
  2311. ((eq state 'content) nil)
  2312. ((eq state 'all) nil)
  2313. ((eq state 'folded) nil)
  2314. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  2315. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1)))))
  2316. (defun org-subtree-end-visible-p ()
  2317. "Is the end of the current subtree visible?"
  2318. (pos-visible-in-window-p
  2319. (save-excursion (org-end-of-subtree t) (point))))
  2320. (defun org-first-headline-recenter (&optional N)
  2321. "Move cursor to the first headline and recenter the headline.
  2322. Optional argument N means, put the headline into the Nth line of the window."
  2323. (goto-char (point-min))
  2324. (when (re-search-forward (concat "^" outline-regexp) nil t)
  2325. (beginning-of-line)
  2326. (recenter (prefix-numeric-value N))))
  2327. (defvar org-goto-window-configuration nil)
  2328. (defvar org-goto-marker nil)
  2329. (defvar org-goto-map (make-sparse-keymap))
  2330. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  2331. (while (setq cmd (pop cmds))
  2332. (substitute-key-definition cmd cmd org-goto-map global-map)))
  2333. (define-key org-goto-map "\C-m" 'org-goto-ret)
  2334. (define-key org-goto-map [(left)] 'org-goto-left)
  2335. (define-key org-goto-map [(right)] 'org-goto-right)
  2336. (define-key org-goto-map [(?q)] 'org-goto-quit)
  2337. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  2338. (define-key org-goto-map "\C-i" 'org-cycle)
  2339. (define-key org-goto-map [(tab)] 'org-cycle)
  2340. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  2341. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  2342. (define-key org-goto-map "n" 'outline-next-visible-heading)
  2343. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  2344. (define-key org-goto-map "f" 'outline-forward-same-level)
  2345. (define-key org-goto-map "b" 'outline-backward-same-level)
  2346. (define-key org-goto-map "u" 'outline-up-heading)
  2347. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  2348. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  2349. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  2350. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  2351. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  2352. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2353. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  2354. (defconst org-goto-help
  2355. "Select a location to jump to, press RET
  2356. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  2357. (defun org-goto ()
  2358. "Go to a different location of the document, keeping current visibility.
  2359. When you want to go to a different location in a document, the fastest way
  2360. is often to fold the entire buffer and then dive into the tree. This
  2361. method has the disadvantage, that the previous location will be folded,
  2362. which may not be what you want.
  2363. This command works around this by showing a copy of the current buffer in
  2364. overview mode. You can dive into the tree in that copy, to find the
  2365. location you want to reach. When pressing RET, the command returns to the
  2366. original buffer in which the visibility is still unchanged. It then jumps
  2367. to the new location, making it and the headline hierarchy above it visible."
  2368. (interactive)
  2369. (let* ((org-goto-start-pos (point))
  2370. (selected-point
  2371. (org-get-location (current-buffer) org-goto-help)))
  2372. (if selected-point
  2373. (progn
  2374. (org-mark-ring-push org-goto-start-pos)
  2375. (goto-char selected-point)
  2376. (if (or (org-invisible-p) (org-invisible-p2))
  2377. (org-show-hierarchy-above)))
  2378. (error "Quit"))))
  2379. (defun org-get-location (buf help)
  2380. "Let the user select a location in the Org-mode buffer BUF.
  2381. This function uses a recursive edit. It returns the selected position
  2382. or nil."
  2383. (let (org-selected-point)
  2384. (save-excursion
  2385. (save-window-excursion
  2386. (delete-other-windows)
  2387. (switch-to-buffer (get-buffer-create "*org-goto*"))
  2388. (with-output-to-temp-buffer "*Help*"
  2389. (princ help))
  2390. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  2391. (setq buffer-read-only nil)
  2392. (erase-buffer)
  2393. (insert-buffer-substring buf)
  2394. (let ((org-startup-truncated t)
  2395. (org-startup-folded t)
  2396. (org-startup-with-deadline-check nil))
  2397. (org-mode))
  2398. (setq buffer-read-only t)
  2399. (if (boundp 'org-goto-start-pos)
  2400. (goto-char org-goto-start-pos)
  2401. (goto-char (point-min)))
  2402. (org-beginning-of-line)
  2403. (message "Select location and press RET")
  2404. ;; now we make sure that during selection, ony very few keys work
  2405. ;; and that it is impossible to switch to another window.
  2406. (let ((gm (current-global-map))
  2407. (overriding-local-map org-goto-map))
  2408. (unwind-protect
  2409. (progn
  2410. (use-global-map org-goto-map)
  2411. (recursive-edit))
  2412. (use-global-map gm)))))
  2413. (kill-buffer "*org-goto*")
  2414. org-selected-point))
  2415. ;; FIXME: It may not be a good idea to temper with the prefix argument...
  2416. (defun org-goto-ret (&optional arg)
  2417. "Finish `org-goto' by going to the new location."
  2418. (interactive "P")
  2419. (setq org-selected-point (point)
  2420. current-prefix-arg arg)
  2421. (throw 'exit nil))
  2422. (defun org-goto-left ()
  2423. "Finish `org-goto' by going to the new location."
  2424. (interactive)
  2425. (if (org-on-heading-p)
  2426. (progn
  2427. (beginning-of-line 1)
  2428. (setq org-selected-point (point)
  2429. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2430. (throw 'exit nil))
  2431. (error "Not on a heading")))
  2432. (defun org-goto-right ()
  2433. "Finish `org-goto' by going to the new location."
  2434. (interactive)
  2435. (if (org-on-heading-p)
  2436. (progn
  2437. (outline-end-of-subtree)
  2438. (or (eobp) (forward-char 1))
  2439. (setq org-selected-point (point)
  2440. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2441. (throw 'exit nil))
  2442. (error "Not on a heading")))
  2443. (defun org-goto-quit ()
  2444. "Finish `org-goto' without cursor motion."
  2445. (interactive)
  2446. (setq org-selected-point nil)
  2447. (throw 'exit nil))
  2448. ;;; Promotion, Demotion, Inserting new headlines
  2449. (defvar org-ignore-region nil
  2450. "To temporarily disable the active region.")
  2451. (defun org-insert-heading (&optional force-heading)
  2452. "Insert a new heading or item with same depth at point."
  2453. (interactive "P")
  2454. (when (or force-heading (not (org-insert-item)))
  2455. (let* ((head (save-excursion
  2456. (condition-case nil
  2457. (org-back-to-heading)
  2458. (error (outline-next-heading)))
  2459. (prog1 (match-string 0)
  2460. (funcall outline-level)))))
  2461. (unless (bolp) (newline))
  2462. (insert head)
  2463. (unless (eolp)
  2464. (save-excursion (newline-and-indent)))
  2465. (unless (equal (char-before) ?\ )
  2466. (insert " "))
  2467. (run-hooks 'org-insert-heading-hook))))
  2468. (defun org-insert-item ()
  2469. "Insert a new item at the current level.
  2470. Return t when things worked, nil when we are not in an item."
  2471. (when (save-excursion
  2472. (condition-case nil
  2473. (progn
  2474. (org-beginning-of-item)
  2475. (org-at-item-p)
  2476. t)
  2477. (error nil)))
  2478. (unless (bolp) (newline))
  2479. (insert (match-string 0))
  2480. (org-maybe-renumber-ordered-list)
  2481. t))
  2482. (defun org-insert-todo-heading (arg)
  2483. "Insert a new heading with the same level and TODO state as current heading.
  2484. If the heading has no TODO state, or if the state is DONE, use the first
  2485. state (TODO by default). Also with prefix arg, force first state."
  2486. (interactive "P")
  2487. (org-insert-heading)
  2488. (save-excursion
  2489. (org-back-to-heading)
  2490. (outline-previous-heading)
  2491. (looking-at org-todo-line-regexp))
  2492. (if (or arg
  2493. (not (match-beginning 2))
  2494. (equal (match-string 2) org-done-string))
  2495. (insert (car org-todo-keywords) " ")
  2496. (insert (match-string 2) " ")))
  2497. (defun org-promote-subtree ()
  2498. "Promote the entire subtree.
  2499. See also `org-promote'."
  2500. (interactive)
  2501. (save-excursion
  2502. (org-map-tree 'org-promote)))
  2503. (defun org-demote-subtree ()
  2504. "Demote the entire subtree. See `org-demote'.
  2505. See also `org-promote'."
  2506. (interactive)
  2507. (save-excursion
  2508. (org-map-tree 'org-demote)))
  2509. (defun org-do-promote ()
  2510. "Promote the current heading higher up the tree.
  2511. If the region is active in `transient-mark-mode', promote all headings
  2512. in the region."
  2513. (interactive)
  2514. (save-excursion
  2515. (if (org-region-active-p)
  2516. (org-map-region 'org-promote (region-beginning) (region-end))
  2517. (org-promote)))
  2518. (org-fix-position-after-promote))
  2519. (defun org-do-demote ()
  2520. "Demote the current heading lower down the tree.
  2521. If the region is active in `transient-mark-mode', demote all headings
  2522. in the region."
  2523. (interactive)
  2524. (save-excursion
  2525. (if (org-region-active-p)
  2526. (org-map-region 'org-demote (region-beginning) (region-end))
  2527. (org-demote)))
  2528. (org-fix-position-after-promote))
  2529. (defun org-fix-position-after-promote ()
  2530. "Make sure that after pro/demotion cursor position is right."
  2531. (and (equal (char-after) ?\ )
  2532. (equal (char-before) ?*)
  2533. (forward-char 1)))
  2534. (defun org-get-legal-level (level change)
  2535. "Rectify a level change under the influence of `org-odd-levels-only'
  2536. LEVEL is a current level, CHANGE is by how much the level should be
  2537. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  2538. even level numbers will become the next higher odd number."
  2539. (if org-odd-levels-only
  2540. (cond ((not change) (1+ (* 2 (/ level 2))))
  2541. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  2542. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  2543. (max 1 (+ level change))))
  2544. (defun org-promote ()
  2545. "Promote the current heading higher up the tree.
  2546. If the region is active in `transient-mark-mode', promote all headings
  2547. in the region."
  2548. (org-back-to-heading t)
  2549. (let* ((level (save-match-data (funcall outline-level)))
  2550. (up-head (make-string (org-get-legal-level level -1) ?*))
  2551. (diff (abs (- level (length up-head)))))
  2552. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  2553. (replace-match up-head nil t)
  2554. ;; Fixup tag positioning
  2555. (and org-auto-align-tags (org-set-tags nil t))
  2556. (if org-adapt-indentation
  2557. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  2558. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  2559. (defun org-demote ()
  2560. "Demote the current heading lower down the tree.
  2561. If the region is active in `transient-mark-mode', demote all headings
  2562. in the region."
  2563. (org-back-to-heading t)
  2564. (let* ((level (save-match-data (funcall outline-level)))
  2565. (down-head (make-string (org-get-legal-level level 1) ?*))
  2566. (diff (abs (- level (length down-head)))))
  2567. (replace-match down-head nil t)
  2568. ;; Fixup tag positioning
  2569. (and org-auto-align-tags (org-set-tags nil t))
  2570. (if org-adapt-indentation
  2571. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  2572. (defun org-map-tree (fun)
  2573. "Call FUN for every heading underneath the current one."
  2574. (org-back-to-heading)
  2575. (let ((level (funcall outline-level)))
  2576. (save-excursion
  2577. (funcall fun)
  2578. (while (and (progn
  2579. (outline-next-heading)
  2580. (> (funcall outline-level) level))
  2581. (not (eobp)))
  2582. (funcall fun)))))
  2583. (defun org-map-region (fun beg end)
  2584. "Call FUN for every heading between BEG and END."
  2585. (let ((org-ignore-region t))
  2586. (save-excursion
  2587. (setq end (copy-marker end))
  2588. (goto-char beg)
  2589. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  2590. (< (point) end))
  2591. (funcall fun))
  2592. (while (and (progn
  2593. (outline-next-heading)
  2594. (< (point) end))
  2595. (not (eobp)))
  2596. (funcall fun)))))
  2597. (defun org-fixup-indentation (from to prohibit)
  2598. "Change the indentation in the current entry by re-replacing FROM with TO.
  2599. However, if the regexp PROHIBIT matches at all, don't do anything.
  2600. This is being used to change indentation along with the length of the
  2601. heading marker. But if there are any lines which are not indented, nothing
  2602. is changed at all."
  2603. (save-excursion
  2604. (let ((end (save-excursion (outline-next-heading)
  2605. (point-marker))))
  2606. (unless (save-excursion (re-search-forward prohibit end t))
  2607. (while (re-search-forward from end t)
  2608. (replace-match to)
  2609. (beginning-of-line 2)))
  2610. (move-marker end nil))))
  2611. ;;; Vertical tree motion, cutting and pasting of subtrees
  2612. (defun org-move-subtree-up (&optional arg)
  2613. "Move the current subtree up past ARG headlines of the same level."
  2614. (interactive "p")
  2615. (org-move-subtree-down (- (prefix-numeric-value arg))))
  2616. (defun org-move-subtree-down (&optional arg)
  2617. "Move the current subtree down past ARG headlines of the same level."
  2618. (interactive "p")
  2619. (setq arg (prefix-numeric-value arg))
  2620. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  2621. 'outline-get-last-sibling))
  2622. (ins-point (make-marker))
  2623. (cnt (abs arg))
  2624. beg end txt folded)
  2625. ;; Select the tree
  2626. (org-back-to-heading)
  2627. (setq beg (point))
  2628. (save-match-data
  2629. (save-excursion (outline-end-of-heading)
  2630. (setq folded (org-invisible-p)))
  2631. (outline-end-of-subtree))
  2632. (outline-next-heading)
  2633. (setq end (point))
  2634. ;; Find insertion point, with error handling
  2635. (goto-char beg)
  2636. (while (> cnt 0)
  2637. (or (and (funcall movfunc) (looking-at outline-regexp))
  2638. (progn (goto-char beg)
  2639. (error "Cannot move past superior level or buffer limit")))
  2640. (setq cnt (1- cnt)))
  2641. (if (> arg 0)
  2642. ;; Moving forward - still need to move over subtree
  2643. (progn (outline-end-of-subtree)
  2644. (outline-next-heading)
  2645. (if (not (or (looking-at (concat "^" outline-regexp))
  2646. (bolp)))
  2647. (newline))))
  2648. (move-marker ins-point (point))
  2649. (setq txt (buffer-substring beg end))
  2650. (delete-region beg end)
  2651. (insert txt)
  2652. (goto-char ins-point)
  2653. (if folded (hide-subtree))
  2654. (move-marker ins-point nil)))
  2655. (defvar org-subtree-clip ""
  2656. "Clipboard for cut and paste of subtrees.
  2657. This is actually only a copy of the kill, because we use the normal kill
  2658. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  2659. (defvar org-subtree-clip-folded nil
  2660. "Was the last copied subtree folded?
  2661. This is used to fold the tree back after pasting.")
  2662. (defun org-cut-subtree ()
  2663. "Cut the current subtree into the clipboard.
  2664. This is a short-hand for marking the subtree and then cutting it."
  2665. (interactive)
  2666. (org-copy-subtree 'cut))
  2667. (defun org-copy-subtree (&optional cut)
  2668. "Cut the current subtree into the clipboard.
  2669. This is a short-hand for marking the subtree and then copying it.
  2670. If CUT is non nil, actually cut the subtree."
  2671. (interactive)
  2672. (let (beg end folded)
  2673. (org-back-to-heading)
  2674. (setq beg (point))
  2675. (save-match-data
  2676. (save-excursion (outline-end-of-heading)
  2677. (setq folded (org-invisible-p)))
  2678. (outline-end-of-subtree))
  2679. (if (equal (char-after) ?\n) (forward-char 1))
  2680. (setq end (point))
  2681. (goto-char beg)
  2682. (when (> end beg)
  2683. (setq org-subtree-clip-folded folded)
  2684. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  2685. (setq org-subtree-clip (current-kill 0))
  2686. (message "%s: Subtree with %d characters"
  2687. (if cut "Cut" "Copied")
  2688. (length org-subtree-clip)))))
  2689. (defun org-paste-subtree (&optional level tree)
  2690. "Paste the clipboard as a subtree, with modification of headline level.
  2691. The entire subtree is promoted or demoted in order to match a new headline
  2692. level. By default, the new level is derived from the visible headings
  2693. before and after the insertion point, and taken to be the inferior headline
  2694. level of the two. So if the previous visible heading is level 3 and the
  2695. next is level 4 (or vice versa), level 4 will be used for insertion.
  2696. This makes sure that the subtree remains an independent subtree and does
  2697. not swallow low level entries.
  2698. You can also force a different level, either by using a numeric prefix
  2699. argument, or by inserting the heading marker by hand. For example, if the
  2700. cursor is after \"*****\", then the tree will be shifted to level 5.
  2701. If you want to insert the tree as is, just use \\[yank].
  2702. If optional TREE is given, use this text instead of the kill ring."
  2703. (interactive "P")
  2704. (unless (org-kill-is-subtree-p tree)
  2705. (error
  2706. (substitute-command-keys
  2707. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  2708. (let* ((txt (or tree (current-kill 0)))
  2709. (^re (concat "^\\(" outline-regexp "\\)"))
  2710. (re (concat "\\(" outline-regexp "\\)"))
  2711. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  2712. (old-level (if (string-match ^re txt)
  2713. (- (match-end 0) (match-beginning 0))
  2714. -1))
  2715. (force-level (cond (level (prefix-numeric-value level))
  2716. ((string-match
  2717. ^re_ (buffer-substring (point-at-bol) (point)))
  2718. (- (match-end 0) (match-beginning 0)))
  2719. (t nil)))
  2720. (previous-level (save-excursion
  2721. (condition-case nil
  2722. (progn
  2723. (outline-previous-visible-heading 1)
  2724. (if (looking-at re)
  2725. (- (match-end 0) (match-beginning 0))
  2726. 1))
  2727. (error 1))))
  2728. (next-level (save-excursion
  2729. (condition-case nil
  2730. (progn
  2731. (outline-next-visible-heading 1)
  2732. (if (looking-at re)
  2733. (- (match-end 0) (match-beginning 0))
  2734. 1))
  2735. (error 1))))
  2736. (new-level (or force-level (max previous-level next-level)))
  2737. (shift (if (or (= old-level -1)
  2738. (= new-level -1)
  2739. (= old-level new-level))
  2740. 0
  2741. (- new-level old-level)))
  2742. (shift1 shift)
  2743. (delta (if (> shift 0) -1 1))
  2744. (func (if (> shift 0) 'org-demote 'org-promote))
  2745. (org-odd-levels-only nil)
  2746. beg end)
  2747. ;; Remove the forces level indicator
  2748. (if force-level
  2749. (delete-region (point-at-bol) (point)))
  2750. ;; Make sure we start at the beginning of an empty line
  2751. (if (not (bolp)) (insert "\n"))
  2752. (if (not (looking-at "[ \t]*$"))
  2753. (progn (insert "\n") (backward-char 1)))
  2754. ;; Paste
  2755. (setq beg (point))
  2756. (insert txt)
  2757. (setq end (point))
  2758. (goto-char beg)
  2759. ;; Shift if necessary
  2760. (if (= shift 0)
  2761. (message "Pasted at level %d, without shift" new-level)
  2762. (save-restriction
  2763. (narrow-to-region beg end)
  2764. (while (not (= shift 0))
  2765. (org-map-region func (point-min) (point-max))
  2766. (setq shift (+ delta shift)))
  2767. (goto-char (point-min))
  2768. (message "Pasted at level %d, with shift by %d levels"
  2769. new-level shift1)))
  2770. (if (and (eq org-subtree-clip (current-kill 0))
  2771. org-subtree-clip-folded)
  2772. ;; The tree was folded before it was killed/copied
  2773. (hide-subtree))))
  2774. (defun org-kill-is-subtree-p (&optional txt)
  2775. "Check if the current kill is an outline subtree, or a set of trees.
  2776. Returns nil if kill does not start with a headline, or if the first
  2777. headline level is not the largest headline level in the tree.
  2778. So this will actually accept several entries of equal levels as well,
  2779. which is OK for `org-paste-subtree'.
  2780. If optional TXT is given, check this string instead of the current kill."
  2781. (let* ((kill (or txt (current-kill 0) ""))
  2782. (start-level (and (string-match (concat "\\`" outline-regexp) kill)
  2783. (- (match-end 0) (match-beginning 0))))
  2784. (re (concat "^" outline-regexp))
  2785. (start 1))
  2786. (if (not start-level)
  2787. nil ;; does not even start with a heading
  2788. (catch 'exit
  2789. (while (setq start (string-match re kill (1+ start)))
  2790. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  2791. (throw 'exit nil)))
  2792. t))))
  2793. ;;; Plain list items
  2794. (defun org-at-item-p ()
  2795. "Is point in a line starting a hand-formatted item?"
  2796. (let ((llt org-plain-list-ordered-item-terminator))
  2797. (save-excursion
  2798. (goto-char (point-at-bol))
  2799. (looking-at
  2800. (cond
  2801. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  2802. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  2803. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  2804. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  2805. (defun org-get-indentation ()
  2806. "Get the indentation of the current line, interpreting tabs."
  2807. (save-excursion
  2808. (beginning-of-line 1)
  2809. (skip-chars-forward " \t")
  2810. (current-column)))
  2811. (defun org-beginning-of-item ()
  2812. "Go to the beginning of the current hand-formatted item.
  2813. If the cursor is not in an item, throw an error."
  2814. (let ((pos (point))
  2815. (limit (save-excursion (org-back-to-heading)
  2816. (beginning-of-line 2) (point)))
  2817. ind ind1)
  2818. (if (org-at-item-p)
  2819. (beginning-of-line 1)
  2820. (beginning-of-line 1)
  2821. (skip-chars-forward " \t")
  2822. (setq ind (current-column))
  2823. (if (catch 'exit
  2824. (while t
  2825. (beginning-of-line 0)
  2826. (if (< (point) limit) (throw 'exit nil))
  2827. (unless (looking-at " \t]*$")
  2828. (skip-chars-forward " \t")
  2829. (setq ind1 (current-column))
  2830. (if (< ind1 ind)
  2831. (throw 'exit (org-at-item-p))))))
  2832. nil
  2833. (goto-char pos)
  2834. (error "Not in an item")))))
  2835. (defun org-end-of-item ()
  2836. "Go to the end of the current hand-formatted item.
  2837. If the cursor is not in an item, throw an error."
  2838. (let ((pos (point))
  2839. (limit (save-excursion (outline-next-heading) (point)))
  2840. (ind (save-excursion
  2841. (org-beginning-of-item)
  2842. (skip-chars-forward " \t")
  2843. (current-column)))
  2844. ind1)
  2845. (if (catch 'exit
  2846. (while t
  2847. (beginning-of-line 2)
  2848. (if (>= (point) limit) (throw 'exit t))
  2849. (unless (looking-at "[ \t]*$")
  2850. (skip-chars-forward " \t")
  2851. (setq ind1 (current-column))
  2852. (if (<= ind1 ind) (throw 'exit t)))))
  2853. (beginning-of-line 1)
  2854. (goto-char pos)
  2855. (error "Not in an item"))))
  2856. (defun org-move-item-down (arg)
  2857. "Move the plain list item at point down, i.e. swap with following item.
  2858. Subitems (items with larger indentation) are considered part of the item,
  2859. so this really moves item trees."
  2860. (interactive "p")
  2861. (let (beg end ind ind1 (pos (point)) txt)
  2862. (org-beginning-of-item)
  2863. (setq beg (point))
  2864. (setq ind (org-get-indentation))
  2865. (org-end-of-item)
  2866. (setq end (point))
  2867. (setq ind1 (org-get-indentation))
  2868. (if (and (org-at-item-p) (= ind ind1))
  2869. (progn
  2870. (org-end-of-item)
  2871. (setq txt (buffer-substring beg end))
  2872. (save-excursion
  2873. (delete-region beg end))
  2874. (setq pos (point))
  2875. (insert txt)
  2876. (goto-char pos)
  2877. (org-maybe-renumber-ordered-list))
  2878. (goto-char pos)
  2879. (error "Cannot move this item further down"))))
  2880. (defun org-move-item-up (arg)
  2881. "Move the plain list item at point up, i.e. swap with previous item.
  2882. Subitems (items with larger indentation) are considered part of the item,
  2883. so this really moves item trees."
  2884. (interactive "p")
  2885. (let (beg end ind ind1 (pos (point)) txt)
  2886. (org-beginning-of-item)
  2887. (setq beg (point))
  2888. (setq ind (org-get-indentation))
  2889. (org-end-of-item)
  2890. (setq end (point))
  2891. (goto-char beg)
  2892. (catch 'exit
  2893. (while t
  2894. (beginning-of-line 0)
  2895. (if (looking-at "[ \t]*$")
  2896. nil
  2897. (if (<= (setq ind1 (org-get-indentation)) ind)
  2898. (throw 'exit t)))))
  2899. (condition-case nil
  2900. (org-beginning-of-item)
  2901. (error (goto-char beg)
  2902. (error "Cannot move this item further up")))
  2903. (setq ind1 (org-get-indentation))
  2904. (if (and (org-at-item-p) (= ind ind1))
  2905. (progn
  2906. (setq txt (buffer-substring beg end))
  2907. (save-excursion
  2908. (delete-region beg end))
  2909. (setq pos (point))
  2910. (insert txt)
  2911. (goto-char pos)
  2912. (org-maybe-renumber-ordered-list))
  2913. (goto-char pos)
  2914. (error "Cannot move this item further up"))))
  2915. (defun org-maybe-renumber-ordered-list ()
  2916. "Renumber the ordered list at point if setup allows it.
  2917. This tests the user option `org-auto-renumber-ordered-lists' before
  2918. doing the renumbering."
  2919. (and org-auto-renumber-ordered-lists
  2920. (org-at-item-p)
  2921. (match-beginning 3)
  2922. (org-renumber-ordered-list 1)))
  2923. (defun org-get-string-indentation (s)
  2924. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  2925. (let ((n -1) (i 0) (w tab-width) c)
  2926. (catch 'exit
  2927. (while (< (setq n (1+ n)) (length s))
  2928. (setq c (aref s n))
  2929. (cond ((= c ?\ ) (setq i (1+ i)))
  2930. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  2931. (t (throw 'exit t)))))
  2932. i))
  2933. (defun org-renumber-ordered-list (arg)
  2934. "Renumber an ordered plain list.
  2935. Cursor next to be in the first line of an item, the line that starts
  2936. with something like \"1.\" or \"2)\"."
  2937. (interactive "p")
  2938. (unless (and (org-at-item-p)
  2939. (match-beginning 3))
  2940. (error "This is not an ordered list"))
  2941. (let ((line (org-current-line))
  2942. (col (current-column))
  2943. (ind (org-get-string-indentation
  2944. (buffer-substring (point-at-bol) (match-beginning 3))))
  2945. ;; (term (substring (match-string 3) -1))
  2946. ind1 (n (1- arg)))
  2947. ;; find where this list begins
  2948. (catch 'exit
  2949. (while t
  2950. (catch 'next
  2951. (beginning-of-line 0)
  2952. (if (looking-at "[ \t]*$") (throw 'next t))
  2953. (skip-chars-forward " \t") (setq ind1 (current-column))
  2954. (if (or (< ind1 ind)
  2955. (and (= ind1 ind)
  2956. (not (org-at-item-p))))
  2957. (throw 'exit t)))))
  2958. ;; Walk forward and replace these numbers
  2959. (catch 'exit
  2960. (while t
  2961. (catch 'next
  2962. (beginning-of-line 2)
  2963. (if (eobp) (throw 'exit nil))
  2964. (if (looking-at "[ \t]*$") (throw 'next nil))
  2965. (skip-chars-forward " \t") (setq ind1 (current-column))
  2966. (if (> ind1 ind) (throw 'next t))
  2967. (if (< ind1 ind) (throw 'exit t))
  2968. (if (not (org-at-item-p)) (throw 'exit nil))
  2969. (if (not (match-beginning 3))
  2970. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  2971. (delete-region (match-beginning 3) (1- (match-end 3)))
  2972. (goto-char (match-beginning 3))
  2973. (insert (format "%d" (setq n (1+ n)))))))
  2974. (goto-line line)
  2975. (move-to-column col)))
  2976. (defvar org-last-indent-begin-marker (make-marker))
  2977. (defvar org-last-indent-end-marker (make-marker))
  2978. (defun org-outdent-item (arg)
  2979. "Outdent a local list item."
  2980. (interactive "p")
  2981. (org-indent-item (- arg)))
  2982. (defun org-indent-item (arg)
  2983. "Indent a local list item."
  2984. (interactive "p")
  2985. (unless (org-at-item-p)
  2986. (error "Not on an item"))
  2987. (let (beg end ind ind1)
  2988. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  2989. (setq beg org-last-indent-begin-marker
  2990. end org-last-indent-end-marker)
  2991. (org-beginning-of-item)
  2992. (setq beg (move-marker org-last-indent-begin-marker (point)))
  2993. (org-end-of-item)
  2994. (setq end (move-marker org-last-indent-end-marker (point))))
  2995. (goto-char beg)
  2996. (skip-chars-forward " \t") (setq ind (current-column))
  2997. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  2998. (while (< (point) end)
  2999. (beginning-of-line 1)
  3000. (skip-chars-forward " \t") (setq ind1 (current-column))
  3001. (delete-region (point-at-bol) (point))
  3002. (indent-to-column (+ ind1 arg))
  3003. (beginning-of-line 2))
  3004. (goto-char beg)))
  3005. ;;; Archiving
  3006. (defun org-archive-subtree ()
  3007. "Move the current subtree to the archive.
  3008. The archive can be a certain top-level heading in the current file, or in
  3009. a different file. The tree will be moved to that location, the subtree
  3010. heading be marked DONE, and the current time will be added."
  3011. (interactive)
  3012. ;; Save all relevant TODO keyword-relatex variables
  3013. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3014. (tr-org-todo-keywords org-todo-keywords)
  3015. (tr-org-todo-interpretation org-todo-interpretation)
  3016. (tr-org-done-string org-done-string)
  3017. (tr-org-todo-regexp org-todo-regexp)
  3018. (tr-org-todo-line-regexp org-todo-line-regexp)
  3019. (this-buffer (current-buffer))
  3020. file heading buffer level newfile-p)
  3021. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3022. (progn
  3023. (setq file (format (match-string 1 org-archive-location)
  3024. (file-name-nondirectory buffer-file-name))
  3025. heading (match-string 2 org-archive-location)))
  3026. (error "Invalid `org-archive-location'"))
  3027. (if (> (length file) 0)
  3028. (setq newfile-p (not (file-exists-p file))
  3029. buffer (find-file-noselect file))
  3030. (setq buffer (current-buffer)))
  3031. (unless buffer
  3032. (error "Cannot access file \"%s\"" file))
  3033. (if (and (> (length heading) 0)
  3034. (string-match "^\\*+" heading))
  3035. (setq level (match-end 0))
  3036. (setq heading nil level 0))
  3037. (save-excursion
  3038. ;; We first only copy, in case something goes wrong
  3039. ;; we need to protect this-command, to avoid kill-region sets it,
  3040. ;; which would lead to duplication of subtrees
  3041. (let (this-command) (org-copy-subtree))
  3042. (set-buffer buffer)
  3043. ;; Enforce org-mode for the archive buffer
  3044. (if (not (eq major-mode 'org-mode))
  3045. ;; Force the mode for future visits.
  3046. (let ((org-insert-mode-line-in-empty-file t))
  3047. (call-interactively 'org-mode)))
  3048. (when newfile-p
  3049. (goto-char (point-max))
  3050. (insert (format "\nArchived entries from file %s\n\n"
  3051. (buffer-file-name this-buffer))))
  3052. ;; Force the TODO keywords of the original buffer
  3053. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3054. (org-todo-keywords tr-org-todo-keywords)
  3055. (org-todo-interpretation tr-org-todo-interpretation)
  3056. (org-done-string tr-org-done-string)
  3057. (org-todo-regexp tr-org-todo-regexp)
  3058. (org-todo-line-regexp tr-org-todo-line-regexp))
  3059. (goto-char (point-min))
  3060. (if heading
  3061. (progn
  3062. (if (re-search-forward
  3063. (concat "\\(^\\|\r\\)"
  3064. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3065. nil t)
  3066. (goto-char (match-end 0))
  3067. ;; Heading not found, just insert it at the end
  3068. (goto-char (point-max))
  3069. (or (bolp) (insert "\n"))
  3070. (insert "\n" heading "\n")
  3071. (end-of-line 0))
  3072. ;; Make the heading visible, and the following as well
  3073. (let ((org-show-following-heading t)) (org-show-hierarchy-above))
  3074. (if (re-search-forward
  3075. (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
  3076. nil t)
  3077. (progn (goto-char (match-beginning 0)) (insert "\n")
  3078. (beginning-of-line 0))
  3079. (goto-char (point-max)) (insert "\n")))
  3080. (goto-char (point-max)) (insert "\n"))
  3081. ;; Paste
  3082. (org-paste-subtree (1+ level))
  3083. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3084. (if org-archive-mark-done
  3085. (org-todo (length org-todo-keywords)))
  3086. ;; Move cursor to right after the TODO keyword
  3087. (when org-archive-stamp-time
  3088. (beginning-of-line 1)
  3089. (looking-at org-todo-line-regexp)
  3090. (goto-char (or (match-end 2) (match-beginning 3)))
  3091. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3092. (org-current-time))
  3093. ")"))
  3094. ;; Save the buffer, if it is not the same buffer.
  3095. (if (not (eq this-buffer buffer)) (save-buffer))))
  3096. ;; Here we are back in the original buffer. Everything seems to have
  3097. ;; worked. So now cut the tree and finish up.
  3098. (let (this-command) (org-cut-subtree))
  3099. (if (looking-at "[ \t]*$") (kill-line))
  3100. (message "Subtree archived %s"
  3101. (if (eq this-buffer buffer)
  3102. (concat "under heading: " heading)
  3103. (concat "in file: " (abbreviate-file-name file))))))
  3104. ;;; Completion
  3105. (defun org-complete (&optional arg)
  3106. "Perform completion on word at point.
  3107. At the beginning of a headline, this completes TODO keywords as given in
  3108. `org-todo-keywords'.
  3109. If the current word is preceded by a backslash, completes the TeX symbols
  3110. that are supported for HTML support.
  3111. If the current word is preceded by \"#+\", completes special words for
  3112. setting file options.
  3113. At all other locations, this simply calls `ispell-complete-word'."
  3114. (interactive "P")
  3115. (catch 'exit
  3116. (let* ((end (point))
  3117. (beg1 (save-excursion
  3118. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3119. (skip-chars-backward "a-zA-Z_@0-9")
  3120. (point)))
  3121. (beg (save-excursion
  3122. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3123. (skip-chars-backward "a-zA-Z0-9_:$")
  3124. (point)))
  3125. (camel (equal (char-before beg) ?*))
  3126. (tag (equal (char-before beg1) ?:))
  3127. (texp (equal (char-before beg) ?\\))
  3128. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3129. beg)
  3130. "#+"))
  3131. (completion-ignore-case opt)
  3132. (type nil)
  3133. (tbl nil)
  3134. (table (cond
  3135. (opt
  3136. (setq type :opt)
  3137. (mapcar (lambda (x)
  3138. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3139. (cons (match-string 2 x) (match-string 1 x)))
  3140. (org-split-string (org-get-current-options) "\n")))
  3141. (texp
  3142. (setq type :tex)
  3143. org-html-entities)
  3144. ((string-match "\\`\\*+[ \t]*\\'"
  3145. (buffer-substring (point-at-bol) beg))
  3146. (setq type :todo)
  3147. (mapcar 'list org-todo-keywords))
  3148. (camel
  3149. (setq type :camel)
  3150. (save-excursion
  3151. (goto-char (point-min))
  3152. (while (re-search-forward org-todo-line-regexp nil t)
  3153. (push (list
  3154. (if org-file-link-context-use-camel-case
  3155. (org-make-org-heading-camel (match-string 3) t)
  3156. (org-make-org-heading-search-string
  3157. (match-string 3) t)))
  3158. tbl)))
  3159. tbl)
  3160. (tag (setq type :tag beg beg1)
  3161. (org-get-buffer-tags))
  3162. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3163. (pattern (buffer-substring-no-properties beg end))
  3164. (completion (try-completion pattern table)))
  3165. (cond ((eq completion t)
  3166. (if (equal type :opt)
  3167. (insert (substring (cdr (assoc (upcase pattern) table))
  3168. (length pattern)))))
  3169. ((null completion)
  3170. (message "Can't find completion for \"%s\"" pattern)
  3171. (ding))
  3172. ((not (string= pattern completion))
  3173. (delete-region beg end)
  3174. (if (string-match " +$" completion)
  3175. (setq completion (replace-match "" t t completion)))
  3176. (insert completion)
  3177. (if (get-buffer-window "*Completions*")
  3178. (delete-window (get-buffer-window "*Completions*")))
  3179. (if (assoc completion table)
  3180. (if (eq type :todo) (insert " ")
  3181. (if (eq type :tag) (insert ":"))))
  3182. (if (and (equal type :opt) (assoc completion table))
  3183. (message "%s" (substitute-command-keys
  3184. "Press \\[org-complete] again to insert example settings"))))
  3185. (t
  3186. (message "Making completion list...")
  3187. (let ((list (sort (all-completions pattern table) 'string<)))
  3188. (with-output-to-temp-buffer "*Completions*"
  3189. (condition-case nil
  3190. ;; Protection needed for XEmacs and emacs 21
  3191. (display-completion-list list pattern)
  3192. (error (display-completion-list list)))))
  3193. (message "Making completion list...%s" "done"))))))
  3194. ;;; Comments, TODO and DEADLINE
  3195. (defun org-toggle-comment ()
  3196. "Change the COMMENT state of an entry."
  3197. (interactive)
  3198. (save-excursion
  3199. (org-back-to-heading)
  3200. (if (looking-at (concat outline-regexp
  3201. "\\( +\\<" org-comment-string "\\>\\)"))
  3202. (replace-match "" t t nil 1)
  3203. (if (looking-at outline-regexp)
  3204. (progn
  3205. (goto-char (match-end 0))
  3206. (insert " " org-comment-string))))))
  3207. (defvar org-last-todo-state-is-todo nil
  3208. "This is non-nil when the last TODO state change led to a TODO state.
  3209. If the last change removed the TODO tag or switched to DONE, then
  3210. this is nil.")
  3211. (defun org-todo (&optional arg)
  3212. "Change the TODO state of an item.
  3213. The state of an item is given by a keyword at the start of the heading,
  3214. like
  3215. *** TODO Write paper
  3216. *** DONE Call mom
  3217. The different keywords are specified in the variable `org-todo-keywords'.
  3218. By default the available states are \"TODO\" and \"DONE\".
  3219. So for this example: when the item starts with TODO, it is changed to DONE.
  3220. When it starts with DONE, the DONE is removed. And when neither TODO nor
  3221. DONE are present, add TODO at the beginning of the heading.
  3222. With prefix arg, use completion to determine the new state. With numeric
  3223. prefix arg, switch to that state."
  3224. (interactive "P")
  3225. (save-excursion
  3226. (org-back-to-heading)
  3227. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  3228. (or (looking-at (concat " +" org-todo-regexp " *"))
  3229. (looking-at " *"))
  3230. (let* ((this (match-string 1))
  3231. (completion-ignore-case t)
  3232. (member (member this org-todo-keywords))
  3233. (tail (cdr member))
  3234. (state (cond
  3235. ((equal arg '(4))
  3236. ;; Read a state with completion
  3237. (completing-read "State: " (mapcar (lambda(x) (list x))
  3238. org-todo-keywords)
  3239. nil t))
  3240. ((eq arg 'right)
  3241. (if this
  3242. (if tail (car tail) nil)
  3243. (car org-todo-keywords)))
  3244. ((eq arg 'left)
  3245. (if (equal member org-todo-keywords)
  3246. nil
  3247. (if this
  3248. (nth (- (length org-todo-keywords) (length tail) 2)
  3249. org-todo-keywords)
  3250. org-done-string)))
  3251. (arg
  3252. ;; user requests a specific state
  3253. (nth (1- (prefix-numeric-value arg))
  3254. org-todo-keywords))
  3255. ((null member) (car org-todo-keywords))
  3256. ((null tail) nil) ;; -> first entry
  3257. ((eq org-todo-interpretation 'sequence)
  3258. (car tail))
  3259. ((memq org-todo-interpretation '(type priority))
  3260. (if (eq this-command last-command)
  3261. (car tail)
  3262. (if (> (length tail) 0) org-done-string nil)))
  3263. (t nil)))
  3264. (next (if state (concat " " state " ") " ")))
  3265. (replace-match next t t)
  3266. (setq org-last-todo-state-is-todo
  3267. (not (equal state org-done-string)))
  3268. (when org-log-done
  3269. (if (equal state org-done-string)
  3270. (org-log-done)
  3271. (if (not this)
  3272. (org-log-done t))))
  3273. ;; Fixup tag positioning
  3274. (and org-auto-align-tags (org-set-tags nil t))
  3275. (run-hooks 'org-after-todo-state-change-hook)))
  3276. ;; Fixup cursor location if close to the keyword
  3277. (if (and (outline-on-heading-p)
  3278. (not (bolp))
  3279. (save-excursion (beginning-of-line 1)
  3280. (looking-at org-todo-line-regexp))
  3281. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  3282. (progn
  3283. (goto-char (or (match-end 2) (match-end 1)))
  3284. (just-one-space))))
  3285. (defun org-log-done (&optional undone)
  3286. "Add a time stamp logging that a TODO entry has been closed.
  3287. When UNDONE is non-nil, remove such a time stamp again."
  3288. (interactive)
  3289. (let (beg end col)
  3290. (save-excursion
  3291. (org-back-to-heading t)
  3292. (setq beg (point))
  3293. (looking-at (concat outline-regexp " *"))
  3294. (goto-char (match-end 0))
  3295. (setq col (current-column))
  3296. (outline-next-heading)
  3297. (setq end (point))
  3298. (goto-char beg)
  3299. (when (re-search-forward (concat
  3300. "[\r\n]\\([ \t]*"
  3301. (regexp-quote org-closed-string)
  3302. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  3303. (delete-region (match-beginning 1) (match-end 1)))
  3304. (unless undone
  3305. (org-back-to-heading t)
  3306. (skip-chars-forward "^\n\r")
  3307. (goto-char (min (1+ (point)) (point-max)))
  3308. (when (not (member (char-before) '(?\r ?\n)))
  3309. (insert "\n"))
  3310. (indent-to col)
  3311. (insert org-closed-string " "
  3312. (format-time-string
  3313. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  3314. (org-current-time))
  3315. "\n")))))
  3316. (defun org-show-todo-tree (arg)
  3317. "Make a compact tree which shows all headlines marked with TODO.
  3318. The tree will show the lines where the regexp matches, and all higher
  3319. headlines above the match.
  3320. With \\[universal-argument] prefix, also show the DONE entries.
  3321. With a numeric prefix N, construct a sparse tree for the Nth element
  3322. of `org-todo-keywords'."
  3323. (interactive "P")
  3324. (let ((case-fold-search nil)
  3325. (kwd-re
  3326. (cond ((null arg) org-not-done-regexp)
  3327. ((equal arg '(4)) org-todo-regexp)
  3328. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  3329. (regexp-quote (nth (1- (prefix-numeric-value arg))
  3330. org-todo-keywords)))
  3331. (t (error "Invalid prefix argument: %s" arg)))))
  3332. (message "%d TODO entries found"
  3333. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  3334. (defun org-deadline ()
  3335. "Insert the DEADLINE: string to make a deadline.
  3336. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3337. to modify it to the correct date."
  3338. (interactive)
  3339. (insert
  3340. org-deadline-string " "
  3341. (format-time-string (car org-time-stamp-formats)
  3342. (org-read-date nil 'to-time)))
  3343. (message "%s" (substitute-command-keys
  3344. "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
  3345. (defun org-schedule ()
  3346. "Insert the SCHEDULED: string to schedule a TODO item.
  3347. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3348. to modify it to the correct date."
  3349. (interactive)
  3350. (insert
  3351. org-scheduled-string " "
  3352. (format-time-string (car org-time-stamp-formats)
  3353. (org-read-date nil 'to-time)))
  3354. (message "%s" (substitute-command-keys
  3355. "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
  3356. (defun org-occur (regexp &optional callback)
  3357. "Make a compact tree which shows all matches of REGEXP.
  3358. The tree will show the lines where the regexp matches, and all higher
  3359. headlines above the match. It will also show the heading after the match,
  3360. to make sure editing the matching entry is easy.
  3361. If CALLBACK is non-nil, it is a function which is called to confirm
  3362. that the match should indeed be shown."
  3363. (interactive "sRegexp: ")
  3364. (org-remove-occur-highlights nil nil t)
  3365. (setq regexp (org-check-occur-regexp regexp))
  3366. (let ((cnt 0))
  3367. (save-excursion
  3368. (goto-char (point-min))
  3369. (hide-sublevels 1)
  3370. (while (re-search-forward regexp nil t)
  3371. (when (or (not callback)
  3372. (save-match-data (funcall callback)))
  3373. (setq cnt (1+ cnt))
  3374. (org-highlight-new-match (match-beginning 0) (match-end 0))
  3375. (org-show-hierarchy-above))))
  3376. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  3377. nil 'local)
  3378. (run-hooks 'org-occur-hook)
  3379. (if (interactive-p)
  3380. (message "%d match(es) for regexp %s" cnt regexp))
  3381. cnt))
  3382. (defun org-show-hierarchy-above ()
  3383. "Make sure point and the headings hierarchy above is visible."
  3384. (catch 'exit
  3385. (if (org-on-heading-p t)
  3386. (org-flag-heading nil) ; only show the heading
  3387. (and (or (org-invisible-p) (org-invisible-p2))
  3388. (org-show-hidden-entry))) ; show entire entry
  3389. (save-excursion
  3390. (and org-show-following-heading
  3391. (outline-next-heading)
  3392. (org-flag-heading nil))) ; show the next heading
  3393. (when org-show-hierarchy-above
  3394. (save-excursion ; show all higher headings
  3395. (while (and (condition-case nil
  3396. (progn (org-up-heading-all 1) t)
  3397. (error nil))
  3398. (not (bobp)))
  3399. (org-flag-heading nil))))))
  3400. ;; Overlay compatibility functions
  3401. (defun org-make-overlay (beg end &optional buffer)
  3402. (if (featurep 'xemacs)
  3403. (make-extent beg end buffer)
  3404. (make-overlay beg end buffer)))
  3405. (defun org-delete-overlay (ovl)
  3406. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3407. (defun org-detatch-overlay (ovl)
  3408. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3409. (defun org-move-overlay (ovl beg end &optional buffer)
  3410. (if (featurep 'xemacs)
  3411. (set-extent-endpoints ovl beg end buffer)
  3412. (move-overlay ovl beg end buffer)))
  3413. (defun org-overlay-put (ovl prop value)
  3414. (if (featurep 'xemacs)
  3415. (set-extent-property ovl prop value)
  3416. (overlay-put ovl prop value)))
  3417. (defvar org-occur-highlights nil)
  3418. (defun org-highlight-new-match (beg end)
  3419. "Highlight from BEG to END and mark the highlight is an occur headline."
  3420. (let ((ov (org-make-overlay beg end)))
  3421. (org-overlay-put ov 'face 'secondary-selection)
  3422. (push ov org-occur-highlights)))
  3423. (defun org-remove-occur-highlights (&optional beg end noremove)
  3424. "Remove the occur highlights from the buffer.
  3425. BEG and END are ignored. If NOREMOVE is nil, remove this function
  3426. from the `before-change-functions' in the current buffer."
  3427. (interactive)
  3428. (mapc 'org-delete-overlay org-occur-highlights)
  3429. (setq org-occur-highlights nil)
  3430. (unless noremove
  3431. (remove-hook 'before-change-functions
  3432. 'org-remove-occur-highlights 'local)))
  3433. ;;; Priorities
  3434. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  3435. "Regular expression matching the priority indicator.")
  3436. (defvar org-remove-priority-next-time nil)
  3437. (defun org-priority-up ()
  3438. "Increase the priority of the current item."
  3439. (interactive)
  3440. (org-priority 'up))
  3441. (defun org-priority-down ()
  3442. "Decrease the priority of the current item."
  3443. (interactive)
  3444. (org-priority 'down))
  3445. (defun org-priority (&optional action)
  3446. "Change the priority of an item by ARG.
  3447. ACTION can be set, up, or down."
  3448. (interactive)
  3449. (setq action (or action 'set))
  3450. (let (current new news have remove)
  3451. (save-excursion
  3452. (org-back-to-heading)
  3453. (if (looking-at org-priority-regexp)
  3454. (setq current (string-to-char (match-string 2))
  3455. have t)
  3456. (setq current org-default-priority))
  3457. (cond
  3458. ((eq action 'set)
  3459. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  3460. (setq new (read-char-exclusive))
  3461. (cond ((equal new ?\ ) (setq remove t))
  3462. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  3463. (error "Priority must be between `%c' and `%c'"
  3464. ?A org-lowest-priority))))
  3465. ((eq action 'up)
  3466. (setq new (1- current)))
  3467. ((eq action 'down)
  3468. (setq new (1+ current)))
  3469. (t (error "Invalid action")))
  3470. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  3471. (setq news (format "%c" new))
  3472. (if have
  3473. (if remove
  3474. (replace-match "" t t nil 1)
  3475. (replace-match news t t nil 2))
  3476. (if remove
  3477. (error "No priority cookie found in line")
  3478. (looking-at org-todo-line-regexp)
  3479. (if (match-end 2)
  3480. (progn
  3481. (goto-char (match-end 2))
  3482. (insert " [#" news "]"))
  3483. (goto-char (match-beginning 3))
  3484. (insert "[#" news "] ")))))
  3485. (if remove
  3486. (message "Priority removed")
  3487. (message "Priority of current item set to %s" news))))
  3488. (defun org-get-priority (s)
  3489. "Find priority cookie and return priority."
  3490. (save-match-data
  3491. (if (not (string-match org-priority-regexp s))
  3492. (* 1000 (- org-lowest-priority org-default-priority))
  3493. (* 1000 (- org-lowest-priority
  3494. (string-to-char (match-string 2 s)))))))
  3495. ;;; Timestamps
  3496. (defvar org-last-changed-timestamp nil)
  3497. (defun org-time-stamp (arg)
  3498. "Prompt for a date/time and insert a time stamp.
  3499. If the user specifies a time like HH:MM, or if this command is called
  3500. with a prefix argument, the time stamp will contain date and time.
  3501. Otherwise, only the date will be included. All parts of a date not
  3502. specified by the user will be filled in from the current date/time.
  3503. So if you press just return without typing anything, the time stamp
  3504. will represent the current date/time. If there is already a timestamp
  3505. at the cursor, it will be modified."
  3506. (interactive "P")
  3507. (let ((fmt (if arg (cdr org-time-stamp-formats)
  3508. (car org-time-stamp-formats)))
  3509. (org-time-was-given nil)
  3510. time)
  3511. (cond
  3512. ((and (org-at-timestamp-p)
  3513. (eq last-command 'org-time-stamp)
  3514. (eq this-command 'org-time-stamp))
  3515. (insert "--")
  3516. (setq time (let ((this-command this-command))
  3517. (org-read-date arg 'totime)))
  3518. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3519. (insert (format-time-string fmt time)))
  3520. ((org-at-timestamp-p)
  3521. (setq time (let ((this-command this-command))
  3522. (org-read-date arg 'totime)))
  3523. (and (org-at-timestamp-p) (replace-match
  3524. (setq org-last-changed-timestamp
  3525. (format-time-string fmt time))
  3526. t t))
  3527. (message "Timestamp updated"))
  3528. (t
  3529. (setq time (let ((this-command this-command))
  3530. (org-read-date arg 'totime)))
  3531. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3532. (insert (format-time-string fmt time))))))
  3533. (defun org-time-stamp-inactive (&optional arg)
  3534. "Insert an inactive time stamp.
  3535. An inactive time stamp is enclosed in square brackets instead of angle
  3536. brackets. It is inactive in the sense that it does not trigger agenda entries,
  3537. does not link to the calendar and cannot be changed with the S-cursor keys.
  3538. So these are more for recording a certain time/date."
  3539. ;; FIXME: Would it be better not to ask for a date/time here?
  3540. (interactive "P")
  3541. (let ((fmt (if arg (cdr org-time-stamp-formats)
  3542. (car org-time-stamp-formats)))
  3543. (org-time-was-given nil)
  3544. time)
  3545. (setq time (org-read-date arg 'totime))
  3546. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3547. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  3548. (insert (format-time-string fmt time))))
  3549. (defvar org-date-ovl (org-make-overlay 1 1))
  3550. (org-overlay-put org-date-ovl 'face 'org-warning)
  3551. (org-detatch-overlay org-date-ovl)
  3552. ;;; FIXME: Make the function take "Fri" as "next friday"
  3553. ;;; because these are mostly being used to record the current time.
  3554. (defun org-read-date (&optional with-time to-time)
  3555. "Read a date and make things smooth for the user.
  3556. The prompt will suggest to enter an ISO date, but you can also enter anything
  3557. which will at least partially be understood by `parse-time-string'.
  3558. Unrecognized parts of the date will default to the current day, month, year,
  3559. hour and minute. For example,
  3560. 3-2-5 --> 2003-02-05
  3561. feb 15 --> currentyear-02-15
  3562. sep 12 9 --> 2009-09-12
  3563. 12:45 --> today 12:45
  3564. 22 sept 0:34 --> currentyear-09-22 0:34
  3565. 12 --> currentyear-currentmonth-12
  3566. etc.
  3567. The function understands only English month and weekday abbreviations,
  3568. but this can be configured with the variables `parse-time-months' and
  3569. `parse-time-weekdays'.
  3570. While prompting, a calendar is popped up - you can also select the
  3571. date with the mouse (button 1). The calendar shows a period of three
  3572. months. To scroll it to other months, use the keys `>' and `<'.
  3573. If you don't like the calendar, turn it off with
  3574. \(setq org-popup-calendar-for-date-prompt nil)
  3575. With optional argument TO-TIME, the date will immediately be converted
  3576. to an internal time.
  3577. With an optional argument WITH-TIME, the prompt will suggest to also
  3578. insert a time. Note that when WITH-TIME is not set, you can still
  3579. enter a time, and this function will inform the calling routine about
  3580. this change. The calling routine may then choose to change the format
  3581. used to insert the time stamp into the buffer to include the time."
  3582. (require 'parse-time)
  3583. (let* ((org-time-stamp-rounding-minutes
  3584. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  3585. (ct (org-current-time))
  3586. (default-time
  3587. ;; Default time is either today, or, when entering a range,
  3588. ;; the range start.
  3589. (if (save-excursion
  3590. (re-search-backward
  3591. (concat org-ts-regexp "--\\=")
  3592. (- (point) 20) t))
  3593. (apply
  3594. 'encode-time
  3595. (mapcar (lambda(x) (or x 0)) ;; FIXME: Problem with timezone?
  3596. (parse-time-string (match-string 1))))
  3597. ct))
  3598. (calendar-move-hook nil)
  3599. (view-diary-entries-initially nil)
  3600. (timestr (format-time-string
  3601. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  3602. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  3603. ans ans1 ans2
  3604. second minute hour day month year tl)
  3605. (if org-popup-calendar-for-date-prompt
  3606. ;; Also show a calendar for date selection
  3607. ;; Copied (with modifications) from planner.el by John Wiegley
  3608. (save-excursion
  3609. (save-window-excursion
  3610. (calendar)
  3611. (calendar-forward-day (- (time-to-days default-time)
  3612. (calendar-absolute-from-gregorian
  3613. (calendar-current-date))))
  3614. (org-eval-in-calendar nil)
  3615. (let* ((old-map (current-local-map))
  3616. (map (copy-keymap calendar-mode-map))
  3617. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  3618. (define-key map (kbd "RET") 'org-calendar-select)
  3619. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  3620. 'org-calendar-select-mouse)
  3621. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  3622. 'org-calendar-select-mouse)
  3623. (define-key minibuffer-local-map [(meta shift left)]
  3624. (lambda () (interactive)
  3625. (org-eval-in-calendar '(calendar-backward-month 1))))
  3626. (define-key minibuffer-local-map [(meta shift right)]
  3627. (lambda () (interactive)
  3628. (org-eval-in-calendar '(calendar-forward-month 1))))
  3629. (define-key minibuffer-local-map [(shift up)]
  3630. (lambda () (interactive)
  3631. (org-eval-in-calendar '(calendar-backward-week 1))))
  3632. (define-key minibuffer-local-map [(shift down)]
  3633. (lambda () (interactive)
  3634. (org-eval-in-calendar '(calendar-forward-week 1))))
  3635. (define-key minibuffer-local-map [(shift left)]
  3636. (lambda () (interactive)
  3637. (org-eval-in-calendar '(calendar-backward-day 1))))
  3638. (define-key minibuffer-local-map [(shift right)]
  3639. (lambda () (interactive)
  3640. (org-eval-in-calendar '(calendar-forward-day 1))))
  3641. (define-key minibuffer-local-map ">"
  3642. (lambda () (interactive)
  3643. (org-eval-in-calendar '(scroll-calendar-left 1))))
  3644. (define-key minibuffer-local-map "<"
  3645. (lambda () (interactive)
  3646. (org-eval-in-calendar '(scroll-calendar-right 1))))
  3647. (unwind-protect
  3648. (progn
  3649. (use-local-map map)
  3650. (setq ans (read-string prompt "" nil nil))
  3651. (if (not (string-match "\\S-" ans)) (setq ans nil))
  3652. (setq ans (or ans1 ans ans2)))
  3653. (use-local-map old-map)))))
  3654. ;; Naked prompt only
  3655. (setq ans (read-string prompt "" nil timestr)))
  3656. (org-detatch-overlay org-date-ovl)
  3657. (if (string-match
  3658. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  3659. (progn
  3660. (setq year (if (match-end 2)
  3661. (string-to-number (match-string 2 ans))
  3662. (string-to-number (format-time-string "%Y")))
  3663. month (string-to-number (match-string 3 ans))
  3664. day (string-to-number (match-string 4 ans)))
  3665. (if (< year 100) (setq year (+ 2000 year)))
  3666. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  3667. t nil ans))))
  3668. (setq tl (parse-time-string ans)
  3669. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  3670. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  3671. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  3672. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  3673. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  3674. second (or (nth 0 tl) 0))
  3675. (if (and (boundp 'org-time-was-given)
  3676. (nth 2 tl))
  3677. (setq org-time-was-given t))
  3678. (if (< year 100) (setq year (+ 2000 year)))
  3679. (if to-time
  3680. (encode-time second minute hour day month year)
  3681. (if (or (nth 1 tl) (nth 2 tl))
  3682. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  3683. (format "%04d-%02d-%02d" year month day)))))
  3684. (defun org-eval-in-calendar (form)
  3685. "Eval FORM in the calendar window and return to current window.
  3686. Also, store the cursor date in variable ans2."
  3687. (let ((sw (selected-window)))
  3688. (select-window (get-buffer-window "*Calendar*"))
  3689. (eval form)
  3690. (when (calendar-cursor-to-date)
  3691. (let* ((date (calendar-cursor-to-date))
  3692. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3693. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  3694. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  3695. (select-window sw)))
  3696. (defun org-calendar-select ()
  3697. "Return to `org-read-date' with the date currently selected.
  3698. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  3699. (interactive)
  3700. (when (calendar-cursor-to-date)
  3701. (let* ((date (calendar-cursor-to-date))
  3702. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3703. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  3704. (if (active-minibuffer-window) (exit-minibuffer))))
  3705. (defun org-calendar-select-mouse (ev)
  3706. "Return to `org-read-date' with the date currently selected.
  3707. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  3708. (interactive "e")
  3709. (mouse-set-point ev)
  3710. (when (calendar-cursor-to-date)
  3711. (let* ((date (calendar-cursor-to-date))
  3712. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3713. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  3714. (if (active-minibuffer-window) (exit-minibuffer))))
  3715. (defun org-check-deadlines (ndays)
  3716. "Check if there are any deadlines due or past due.
  3717. A deadline is considered due if it happens within `org-deadline-warning-days'
  3718. days from today's date. If the deadline appears in an entry marked DONE,
  3719. it is not shown. The prefix arg NDAYS can be used to test that many
  3720. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  3721. (interactive "P")
  3722. (let* ((org-warn-days
  3723. (cond
  3724. ((equal ndays '(4)) 100000)
  3725. (ndays (prefix-numeric-value ndays))
  3726. (t org-deadline-warning-days)))
  3727. (case-fold-search nil)
  3728. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  3729. (callback
  3730. (lambda ()
  3731. (and (let ((d1 (time-to-days (current-time)))
  3732. (d2 (time-to-days
  3733. (org-time-string-to-time (match-string 1)))))
  3734. (< (- d2 d1) org-warn-days))
  3735. (not (org-entry-is-done-p))))))
  3736. (message "%d deadlines past-due or due within %d days"
  3737. (org-occur regexp callback)
  3738. org-warn-days)))
  3739. (defun org-evaluate-time-range (&optional to-buffer)
  3740. "Evaluate a time range by computing the difference between start and end.
  3741. Normally the result is just printed in the echo area, but with prefix arg
  3742. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  3743. If the time range is actually in a table, the result is inserted into the
  3744. next column.
  3745. For time difference computation, a year is assumed to be exactly 365
  3746. days in order to avoid rounding problems."
  3747. (interactive "P")
  3748. (save-excursion
  3749. (unless (org-at-date-range-p)
  3750. (goto-char (point-at-bol))
  3751. (re-search-forward org-tr-regexp (point-at-eol) t))
  3752. (if (not (org-at-date-range-p))
  3753. (error "Not at a time-stamp range, and none found in current line")))
  3754. (let* ((ts1 (match-string 1))
  3755. (ts2 (match-string 2))
  3756. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  3757. (match-end (match-end 0))
  3758. (time1 (org-time-string-to-time ts1))
  3759. (time2 (org-time-string-to-time ts2))
  3760. (t1 (time-to-seconds time1))
  3761. (t2 (time-to-seconds time2))
  3762. (diff (abs (- t2 t1)))
  3763. (negative (< (- t2 t1) 0))
  3764. ;; (ys (floor (* 365 24 60 60)))
  3765. (ds (* 24 60 60))
  3766. (hs (* 60 60))
  3767. (fy "%dy %dd %02d:%02d")
  3768. (fy1 "%dy %dd")
  3769. (fd "%dd %02d:%02d")
  3770. (fd1 "%dd")
  3771. (fh "%02d:%02d")
  3772. y d h m align)
  3773. ;; FIXME: Should I re-introduce years, make year refer to same date?
  3774. ;; This would be the only useful way to have years, actually.
  3775. (if havetime
  3776. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  3777. y 0
  3778. d (floor (/ diff ds)) diff (mod diff ds)
  3779. h (floor (/ diff hs)) diff (mod diff hs)
  3780. m (floor (/ diff 60)))
  3781. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  3782. y 0
  3783. d (floor (+ (/ diff ds) 0.5))
  3784. h 0 m 0))
  3785. (if (not to-buffer)
  3786. (message (org-make-tdiff-string y d h m))
  3787. (when (org-at-table-p)
  3788. (goto-char match-end)
  3789. (setq align t)
  3790. (and (looking-at " *|") (goto-char (match-end 0))))
  3791. (if (looking-at
  3792. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  3793. (replace-match ""))
  3794. (if negative (insert " -"))
  3795. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  3796. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  3797. (insert " " (format fh h m))))
  3798. (if align (org-table-align))
  3799. (message "Time difference inserted"))))
  3800. (defun org-make-tdiff-string (y d h m)
  3801. (let ((fmt "")
  3802. (l nil))
  3803. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  3804. l (push y l)))
  3805. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  3806. l (push d l)))
  3807. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  3808. l (push h l)))
  3809. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  3810. l (push m l)))
  3811. (apply 'format fmt (nreverse l))))
  3812. (defun org-time-string-to-time (s)
  3813. (apply 'encode-time (org-parse-time-string s)))
  3814. (defun org-parse-time-string (s &optional nodefault)
  3815. "Parse the standard Org-mode time string.
  3816. This should be a lot faster than the normal `parse-time-string'.
  3817. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  3818. hour and minute fields will be nil if not given."
  3819. (if (string-match org-ts-regexp1 s)
  3820. (list 0
  3821. (if (or (match-beginning 8) (not nodefault))
  3822. (string-to-number (or (match-string 8 s) "0")))
  3823. (if (or (match-beginning 7) (not nodefault))
  3824. (string-to-number (or (match-string 7 s) "0")))
  3825. (string-to-number (match-string 4 s))
  3826. (string-to-number (match-string 3 s))
  3827. (string-to-number (match-string 2 s))
  3828. nil nil nil)
  3829. (make-list 9 0)))
  3830. (defun org-timestamp-up (&optional arg)
  3831. "Increase the date item at the cursor by one.
  3832. If the cursor is on the year, change the year. If it is on the month or
  3833. the day, change that.
  3834. With prefix ARG, change by that many units."
  3835. (interactive "p")
  3836. (org-timestamp-change (prefix-numeric-value arg)))
  3837. (defun org-timestamp-down (&optional arg)
  3838. "Decrease the date item at the cursor by one.
  3839. If the cursor is on the year, change the year. If it is on the month or
  3840. the day, change that.
  3841. With prefix ARG, change by that many units."
  3842. (interactive "p")
  3843. (org-timestamp-change (- (prefix-numeric-value arg))))
  3844. (defun org-timestamp-up-day (&optional arg)
  3845. "Increase the date in the time stamp by one day.
  3846. With prefix ARG, change that many days."
  3847. (interactive "p")
  3848. (if (and (not (org-at-timestamp-p))
  3849. (org-on-heading-p))
  3850. (org-todo 'up)
  3851. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  3852. (defun org-timestamp-down-day (&optional arg)
  3853. "Decrease the date in the time stamp by one day.
  3854. With prefix ARG, change that many days."
  3855. (interactive "p")
  3856. (if (and (not (org-at-timestamp-p))
  3857. (org-on-heading-p))
  3858. (org-todo 'down)
  3859. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  3860. (defsubst org-pos-in-match-range (pos n)
  3861. (and (match-beginning n)
  3862. (<= (match-beginning n) pos)
  3863. (>= (match-end n) pos)))
  3864. (defun org-at-timestamp-p ()
  3865. "Determine if the cursor is in or at a timestamp."
  3866. (interactive)
  3867. (let* ((tsr org-ts-regexp2)
  3868. (pos (point))
  3869. (ans (or (looking-at tsr)
  3870. (save-excursion
  3871. (skip-chars-backward "^<\n\r\t")
  3872. (if (> (point) 1) (backward-char 1))
  3873. (and (looking-at tsr)
  3874. (> (- (match-end 0) pos) -1))))))
  3875. (and (boundp 'org-ts-what)
  3876. (setq org-ts-what
  3877. (cond
  3878. ((org-pos-in-match-range pos 2) 'year)
  3879. ((org-pos-in-match-range pos 3) 'month)
  3880. ((org-pos-in-match-range pos 7) 'hour)
  3881. ((org-pos-in-match-range pos 8) 'minute)
  3882. ((or (org-pos-in-match-range pos 4)
  3883. (org-pos-in-match-range pos 5)) 'day)
  3884. (t 'day))))
  3885. ans))
  3886. (defun org-timestamp-change (n &optional what)
  3887. "Change the date in the time stamp at point.
  3888. The date will be changed by N times WHAT. WHAT can be `day', `month',
  3889. `year', `minute', `second'. If WHAT is not given, the cursor position
  3890. in the timestamp determines what will be changed."
  3891. (let ((fmt (car org-time-stamp-formats))
  3892. org-ts-what
  3893. (pos (point))
  3894. ts time time0)
  3895. (if (not (org-at-timestamp-p))
  3896. (error "Not at a timestamp"))
  3897. (setq org-ts-what (or what org-ts-what))
  3898. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  3899. (- (match-end 0) (match-beginning 0))))
  3900. 1)
  3901. (cdr org-time-stamp-formats)
  3902. (car org-time-stamp-formats)))
  3903. (setq ts (match-string 0))
  3904. (replace-match "")
  3905. (setq time0 (org-parse-time-string ts))
  3906. (setq time
  3907. (apply 'encode-time
  3908. (append
  3909. (list (or (car time0) 0))
  3910. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  3911. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  3912. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  3913. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  3914. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  3915. (nthcdr 6 time0))))
  3916. (if (eq what 'calendar)
  3917. (let ((cal-date
  3918. (save-excursion
  3919. (save-match-data
  3920. (set-buffer "*Calendar*")
  3921. (calendar-cursor-to-date)))))
  3922. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  3923. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  3924. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  3925. (setcar time0 (or (car time0) 0))
  3926. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  3927. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  3928. (setq time (apply 'encode-time time0))))
  3929. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  3930. (goto-char pos)
  3931. ;; Try to recenter the calendar window, if any
  3932. (if (and org-calendar-follow-timestamp-change
  3933. (get-buffer-window "*Calendar*" t)
  3934. (memq org-ts-what '(day month year)))
  3935. (org-recenter-calendar (time-to-days time)))))
  3936. (defun org-recenter-calendar (date)
  3937. "If the calendar is visible, recenter it to DATE."
  3938. (let* ((win (selected-window))
  3939. (cwin (get-buffer-window "*Calendar*" t))
  3940. (calendar-move-hook nil))
  3941. (when cwin
  3942. (select-window cwin)
  3943. (calendar-goto-date (if (listp date) date
  3944. (calendar-gregorian-from-absolute date)))
  3945. (select-window win))))
  3946. (defun org-goto-calendar (&optional arg)
  3947. "Go to the Emacs calendar at the current date.
  3948. If there is a time stamp in the current line, go to that date.
  3949. A prefix ARG can be used to force the current date."
  3950. (interactive "P")
  3951. (let ((tsr org-ts-regexp) diff
  3952. (calendar-move-hook nil)
  3953. (view-diary-entries-initially nil))
  3954. (if (or (org-at-timestamp-p)
  3955. (save-excursion
  3956. (beginning-of-line 1)
  3957. (looking-at (concat ".*" tsr))))
  3958. (let ((d1 (time-to-days (current-time)))
  3959. (d2 (time-to-days
  3960. (org-time-string-to-time (match-string 1)))))
  3961. (setq diff (- d2 d1))))
  3962. (calendar)
  3963. (calendar-goto-today)
  3964. (if (and diff (not arg)) (calendar-forward-day diff))))
  3965. (defun org-date-from-calendar ()
  3966. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  3967. If there is already a time stamp at the cursor position, update it."
  3968. (interactive)
  3969. (org-timestamp-change 0 'calendar))
  3970. ;;; Agenda, and Diary Integration
  3971. ;;; Define the mode
  3972. (defvar org-agenda-mode-map (make-sparse-keymap)
  3973. "Keymap for `org-agenda-mode'.")
  3974. (defvar org-agenda-menu) ; defined later in this file.
  3975. (defvar org-agenda-follow-mode nil)
  3976. (defvar org-agenda-show-log nil)
  3977. (defvar org-agenda-buffer-name "*Org Agenda*")
  3978. (defvar org-agenda-redo-command nil)
  3979. (defvar org-agenda-mode-hook nil)
  3980. (defvar org-agenda-type nil)
  3981. (defvar org-agenda-force-single-file nil)
  3982. ;;;###autoload
  3983. (defun org-agenda-mode ()
  3984. "Mode for time-sorted view on action items in Org-mode files.
  3985. The following commands are available:
  3986. \\{org-agenda-mode-map}"
  3987. (interactive)
  3988. (kill-all-local-variables)
  3989. (setq major-mode 'org-agenda-mode)
  3990. (setq mode-name "Org-Agenda")
  3991. (use-local-map org-agenda-mode-map)
  3992. (easy-menu-add org-agenda-menu)
  3993. (if org-startup-truncated (setq truncate-lines t))
  3994. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  3995. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  3996. (unless org-agenda-keep-modes
  3997. (setq org-agenda-follow-mode nil
  3998. org-agenda-show-log nil))
  3999. (easy-menu-change
  4000. '("Agenda") "Agenda Files"
  4001. (append
  4002. (list
  4003. (vector
  4004. (if (get 'org-agenda-files 'org-restrict)
  4005. "Restricted to single file"
  4006. "Edit File List")
  4007. '(org-edit-agenda-file-list)
  4008. (not (get 'org-agenda-files 'org-restrict)))
  4009. "--")
  4010. (mapcar 'org-file-menu-entry (org-agenda-files))))
  4011. (org-agenda-set-mode-name)
  4012. (apply
  4013. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  4014. (list 'org-agenda-mode-hook)))
  4015. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  4016. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  4017. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  4018. (define-key org-agenda-mode-map " " 'org-agenda-show)
  4019. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  4020. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  4021. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  4022. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  4023. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  4024. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  4025. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  4026. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  4027. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  4028. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  4029. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  4030. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  4031. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  4032. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4033. (while l (define-key org-agenda-mode-map
  4034. (int-to-string (pop l)) 'digit-argument)))
  4035. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  4036. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  4037. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  4038. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  4039. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  4040. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  4041. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  4042. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  4043. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  4044. (define-key org-agenda-mode-map "n" 'next-line)
  4045. (define-key org-agenda-mode-map "p" 'previous-line)
  4046. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  4047. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  4048. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  4049. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  4050. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  4051. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  4052. (eval-after-load "calendar"
  4053. '(define-key calendar-mode-map org-calendar-to-agenda-key
  4054. 'org-calendar-goto-agenda))
  4055. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  4056. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  4057. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  4058. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  4059. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  4060. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  4061. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  4062. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  4063. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  4064. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  4065. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  4066. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  4067. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  4068. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  4069. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  4070. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  4071. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  4072. "Local keymap for agenda entries from Org-mode.")
  4073. (define-key org-agenda-keymap
  4074. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  4075. (define-key org-agenda-keymap
  4076. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  4077. (define-key org-agenda-keymap [follow-link] 'mouse-face)
  4078. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  4079. '("Agenda"
  4080. ("Agenda Files")
  4081. "--"
  4082. ["Show" org-agenda-show t]
  4083. ["Go To (other window)" org-agenda-goto t]
  4084. ["Go To (one window)" org-agenda-switch-to t]
  4085. ["Follow Mode" org-agenda-follow-mode
  4086. :style toggle :selected org-agenda-follow-mode :active t]
  4087. "--"
  4088. ["Cycle TODO" org-agenda-todo t]
  4089. ("Tags"
  4090. ["Show all Tags" org-agenda-show-tags t]
  4091. ["Set Tags" org-agenda-set-tags t])
  4092. ("Reschedule"
  4093. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  4094. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  4095. "--"
  4096. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  4097. ("Priority"
  4098. ["Set Priority" org-agenda-priority t]
  4099. ["Increase Priority" org-agenda-priority-up t]
  4100. ["Decrease Priority" org-agenda-priority-down t]
  4101. ["Show Priority" org-agenda-show-priority t])
  4102. "--"
  4103. ;; ["New agenda command" org-agenda t]
  4104. ["Rebuild buffer" org-agenda-redo t]
  4105. "--"
  4106. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  4107. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  4108. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  4109. "--"
  4110. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  4111. :style radio :selected (equal org-agenda-ndays 1)]
  4112. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  4113. :style radio :selected (equal org-agenda-ndays 7)]
  4114. "--"
  4115. ["Show Logbook entries" org-agenda-log-mode
  4116. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  4117. ["Include Diary" org-agenda-toggle-diary
  4118. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  4119. ["Use Time Grid" org-agenda-toggle-time-grid
  4120. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  4121. "--"
  4122. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  4123. ("Calendar Commands"
  4124. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  4125. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  4126. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  4127. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  4128. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  4129. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  4130. "--"
  4131. ["Quit" org-agenda-quit t]
  4132. ["Exit and Release Buffers" org-agenda-exit t]
  4133. ))
  4134. ;;;###autoload
  4135. (defun org-agenda (arg)
  4136. "Dispatch agenda commands to collect entries to the agenda buffer.
  4137. Prompts for a character to select a command. Any prefix arg will be passed
  4138. on to the selected command. The default selections are:
  4139. a Call `org-agenda' to display the agenda for the current day or week.
  4140. t Call `org-todo-list' to display the global todo list.
  4141. T Call `org-todo-list' to display the global todo list, select only
  4142. entries with a specific TODO keyword (the user gets a prompt).
  4143. m Call `org-tags-view' to display headlines with tags matching
  4144. a condition (the user is prompted for the condition).
  4145. M Like `m', but select only TODO entries, no ordinary headlines.
  4146. More commands can be added by configuring the variable
  4147. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  4148. searches can be pre-defined in this way.
  4149. If the current buffer is in Org-mode and visiting a file, you can also
  4150. first press `1' to indicate that the agenda should be temporarily (until the
  4151. next use of \\[org-agenda]) restricted to the current file."
  4152. (interactive "P")
  4153. (catch 'exit
  4154. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  4155. (custom org-agenda-custom-commands)
  4156. c entry key type string)
  4157. (put 'org-agenda-files 'org-restrict nil)
  4158. (save-window-excursion
  4159. (delete-other-windows)
  4160. (switch-to-buffer-other-window " *Agenda Commands*")
  4161. (erase-buffer)
  4162. (insert
  4163. "Press key for an agenda command:
  4164. --------------------------------
  4165. a Agenda for current week or day
  4166. t List of all TODO entries T Entries with special TODO kwd
  4167. m Match a TAGS query M Like m, but only TODO entries
  4168. C Configure your own agenda commands")
  4169. (while (setq entry (pop custom))
  4170. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  4171. (insert (format "\n%-4s%-14s: %s"
  4172. key
  4173. (cond
  4174. ((eq type 'tags) "Tags query")
  4175. ((eq type 'todo) "TODO keyword")
  4176. ((eq type 'tags-tree) "Tags tree")
  4177. ((eq type 'todo-tree) "TODO kwd tree")
  4178. ((eq type 'occur-tree) "Occur tree")
  4179. (t "???"))
  4180. (org-string-props string 'face 'org-link))))
  4181. (goto-char (point-min))
  4182. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  4183. (message "Press key for agenda command%s"
  4184. (if restrict-ok ", or [1] to restrict to current file" ""))
  4185. (setq c (read-char-exclusive))
  4186. (message "")
  4187. (when (equal c ?1)
  4188. (if restrict-ok
  4189. (put 'org-agenda-files 'org-restrict (list buffer-file-name))
  4190. (error "Cannot restrict agenda to current buffer"))
  4191. (message "Press key for agenda command%s"
  4192. (if restrict-ok " (restricted to current file)" ""))
  4193. (setq c (read-char-exclusive))
  4194. (message "")))
  4195. (require 'calendar) ; FIXME: can we avoid this for some commands?
  4196. ;; For example the todo list should not need it (but does...)
  4197. (cond
  4198. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  4199. ((equal c ?a) (call-interactively 'org-agenda-list))
  4200. ((equal c ?t) (call-interactively 'org-todo-list))
  4201. ((equal c ?T)
  4202. (setq current-prefix-arg (or arg '(4)))
  4203. (call-interactively 'org-todo-list))
  4204. ((equal c ?m) (call-interactively 'org-tags-view))
  4205. ((equal c ?M)
  4206. (setq current-prefix-arg (or arg '(4)))
  4207. (call-interactively 'org-tags-view))
  4208. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  4209. (setq type (nth 1 entry) string (nth 2 entry))
  4210. (cond
  4211. ((eq type 'tags)
  4212. (org-tags-view current-prefix-arg string))
  4213. ((eq type 'todo)
  4214. (org-todo-list string))
  4215. ((eq type 'tags-tree)
  4216. (org-check-for-org-mode)
  4217. (org-tags-sparse-tree current-prefix-arg string))
  4218. ((eq type 'todo-tree)
  4219. (org-check-for-org-mode)
  4220. (org-occur (concat "^" outline-regexp "[ \t]*"
  4221. (regexp-quote string) "\\>")))
  4222. ((eq type 'occur-tree)
  4223. (org-check-for-org-mode)
  4224. (org-occur string))
  4225. (t (error "Invalid custom agenda command type %s" type))))
  4226. (t (error "Invalid key"))))))
  4227. (defun org-check-for-org-mode ()
  4228. "Make sure current buffer is in org-mode. Error if not."
  4229. (or (eq major-mode 'org-mode)
  4230. (error "Cannot execute org-mode agenda command on buffer in %s."
  4231. major-mode)))
  4232. (defun org-fit-agenda-window ()
  4233. "Fit the window to the buffer size."
  4234. (and org-fit-agenda-window
  4235. (fboundp 'fit-window-to-buffer)
  4236. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  4237. (/ (frame-height) 2))))
  4238. (defun org-agenda-files (&optional unrestricted)
  4239. "Get the list of agenda files.
  4240. Optional UNRESTRICTED means return the full list even if a restriction
  4241. is currently in place."
  4242. (cond
  4243. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  4244. ((stringp org-agenda-files) (org-read-agenda-file-list))
  4245. ((listp org-agenda-files) org-agenda-files)
  4246. (t (error "Invalid value of `org-agenda-files'"))))
  4247. (defvar org-window-configuration)
  4248. (defun org-edit-agenda-file-list ()
  4249. "Edit the list of agenda files.
  4250. Depending on setup, this either uses customize to edit the variable
  4251. `org-agenda-files', or it visits the file that is holding the list. In the
  4252. latter case, the buffer is set up in a way that saving it automatically kills
  4253. the buffer and restores the previous window configuration."
  4254. (interactive)
  4255. (if (stringp org-agenda-files)
  4256. (let ((cw (current-window-configuration)))
  4257. (find-file org-agenda-files)
  4258. (set (make-local-variable 'org-window-configuration) cw)
  4259. (org-add-hook 'after-save-hook
  4260. (lambda ()
  4261. (set-window-configuration
  4262. (prog1 org-window-configuration
  4263. (kill-buffer (current-buffer))))
  4264. (org-install-agenda-files-menu)
  4265. (message "New agenda file list installed"))
  4266. nil 'local)
  4267. (message (substitute-command-keys
  4268. "Edit list and finish with \\[save-buffer]")))
  4269. (customize-variable 'org-agenda-files)))
  4270. (defun org-store-new-agenda-file-list (list)
  4271. "Set new value for the agenda file list and save it correcly."
  4272. (if (stringp org-agenda-files)
  4273. (let ((f org-agenda-files) b)
  4274. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  4275. (with-temp-file f
  4276. (insert (mapconcat 'identity list) "\n") "\n"))
  4277. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  4278. (setq org-agenda-files list)
  4279. (customize-save-variable 'org-agenda-files org-agenda-files))))
  4280. (defun org-read-agenda-file-list ()
  4281. "Read the list of agenda files from a file."
  4282. (when (stringp org-agenda-files)
  4283. (with-temp-buffer
  4284. (insert-file-contents org-agenda-files)
  4285. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  4286. (defvar org-agenda-markers nil
  4287. "List of all currently active markers created by `org-agenda'.")
  4288. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  4289. "Creation time of the last agenda marker.")
  4290. (defun org-agenda-new-marker (&optional pos)
  4291. "Return a new agenda marker.
  4292. Org-mode keeps a list of these markers and resets them when they are
  4293. no longer in use."
  4294. (let ((m (copy-marker (or pos (point)))))
  4295. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  4296. (push m org-agenda-markers)
  4297. m))
  4298. (defun org-agenda-maybe-reset-markers (&optional force)
  4299. "Reset markers created by `org-agenda'. But only if they are old enough."
  4300. (if (or force
  4301. (> (- (time-to-seconds (current-time))
  4302. org-agenda-last-marker-time)
  4303. 5))
  4304. (while org-agenda-markers
  4305. (move-marker (pop org-agenda-markers) nil))))
  4306. (defvar org-agenda-new-buffers nil
  4307. "Buffers created to visit agenda files.")
  4308. (defun org-get-agenda-file-buffer (file)
  4309. "Get a buffer visiting FILE. If the buffer needs to be created, add
  4310. it to the list of buffers which might be released later."
  4311. (let ((buf (find-buffer-visiting file)))
  4312. (if buf
  4313. buf ; just return it
  4314. ;; Make a new buffer and remember it
  4315. (setq buf (find-file-noselect file))
  4316. (if buf (push buf org-agenda-new-buffers))
  4317. buf)))
  4318. (defun org-release-buffers (blist)
  4319. "Release all buffers in list, asking the user for confirmation when needed.
  4320. When a buffer is unmodified, it is just killed. When modified, it is saved
  4321. \(if the user agrees) and then killed."
  4322. (let (buf file)
  4323. (while (setq buf (pop blist))
  4324. (setq file (buffer-file-name buf))
  4325. (when (and (buffer-modified-p buf)
  4326. file
  4327. (y-or-n-p (format "Save file %s? " file)))
  4328. (with-current-buffer buf (save-buffer)))
  4329. (kill-buffer buf))))
  4330. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  4331. (defun org-timeline (&optional include-all keep-modes)
  4332. "Show a time-sorted view of the entries in the current org file.
  4333. Only entries with a time stamp of today or later will be listed. With
  4334. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  4335. under the current date.
  4336. If the buffer contains an active region, only check the region for
  4337. dates."
  4338. (interactive "P")
  4339. (require 'calendar)
  4340. (org-agenda-maybe-reset-markers 'force)
  4341. (org-compile-prefix-format org-timeline-prefix-format)
  4342. (let* ((dopast t)
  4343. (dotodo include-all)
  4344. (doclosed org-agenda-show-log)
  4345. (org-agenda-keep-modes keep-modes)
  4346. (entry buffer-file-name)
  4347. (org-agenda-files (list buffer-file-name))
  4348. (date (calendar-current-date))
  4349. (win (selected-window))
  4350. (pos1 (point))
  4351. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  4352. (end (if (org-region-active-p) (region-end) (point-max)))
  4353. (day-numbers (org-get-all-dates beg end 'no-ranges
  4354. t doclosed)) ; always include today
  4355. (today (time-to-days (current-time)))
  4356. (org-respect-restriction t)
  4357. (past t)
  4358. args
  4359. s e rtn d)
  4360. (setq org-agenda-redo-command
  4361. (list 'progn
  4362. (list 'switch-to-buffer-other-window (current-buffer))
  4363. (list 'org-timeline (list 'quote include-all) t)))
  4364. (if (not dopast)
  4365. ;; Remove past dates from the list of dates.
  4366. (setq day-numbers (delq nil (mapcar (lambda(x)
  4367. (if (>= x today) x nil))
  4368. day-numbers))))
  4369. (switch-to-buffer-other-window
  4370. (get-buffer-create org-agenda-buffer-name))
  4371. (setq buffer-read-only nil)
  4372. (erase-buffer)
  4373. (org-agenda-mode) (setq buffer-read-only nil)
  4374. (set (make-local-variable 'org-agenda-type) 'timeline)
  4375. (if doclosed (push :closed args))
  4376. (push :timestamp args)
  4377. (if dotodo (push :todo args))
  4378. (while (setq d (pop day-numbers))
  4379. (if (and (>= d today)
  4380. dopast
  4381. past)
  4382. (progn
  4383. (setq past nil)
  4384. (insert (make-string 79 ?-) "\n")))
  4385. (setq date (calendar-gregorian-from-absolute d))
  4386. (setq s (point))
  4387. (setq rtn (apply 'org-agenda-get-day-entries
  4388. entry date args))
  4389. (if (or rtn (equal d today))
  4390. (progn
  4391. (insert (calendar-day-name date) " "
  4392. (number-to-string (extract-calendar-day date)) " "
  4393. (calendar-month-name (extract-calendar-month date)) " "
  4394. (number-to-string (extract-calendar-year date)) "\n")
  4395. (put-text-property s (1- (point)) 'face
  4396. 'org-link)
  4397. (if (equal d today)
  4398. (put-text-property s (1- (point)) 'org-today t))
  4399. (insert (org-finalize-agenda-entries rtn) "\n")
  4400. (put-text-property s (1- (point)) 'day d))))
  4401. (goto-char (point-min))
  4402. (setq buffer-read-only t)
  4403. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  4404. (point-min)))
  4405. (when (not org-select-timeline-window)
  4406. (select-window win)
  4407. (goto-char pos1))))
  4408. ;;;###autoload
  4409. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  4410. "Produce a weekly view from all files in variable `org-agenda-files'.
  4411. The view will be for the current week, but from the overview buffer you
  4412. will be able to go to other weeks.
  4413. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  4414. also be shown, under the current date.
  4415. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  4416. on the days are also shown. See the variable `org-log-done' for how
  4417. to turn on logging.
  4418. START-DAY defaults to TODAY, or to the most recent match for the weekday
  4419. given in `org-agenda-start-on-weekday'.
  4420. NDAYS defaults to `org-agenda-ndays'."
  4421. (interactive "P")
  4422. (org-agenda-maybe-reset-markers 'force)
  4423. (org-compile-prefix-format org-agenda-prefix-format)
  4424. (require 'calendar)
  4425. (let* ((org-agenda-start-on-weekday
  4426. (if (or (equal ndays 1)
  4427. (and (null ndays) (equal 1 org-agenda-ndays)))
  4428. nil org-agenda-start-on-weekday))
  4429. (org-agenda-keep-modes keep-modes)
  4430. (thefiles (copy-sequence (org-agenda-files))) ;; FIXME: Why copy???
  4431. (files thefiles)
  4432. (win (selected-window))
  4433. (today (time-to-days (current-time)))
  4434. (sd (or start-day today))
  4435. (start (if (or (null org-agenda-start-on-weekday)
  4436. (< org-agenda-ndays 7))
  4437. sd
  4438. (let* ((nt (calendar-day-of-week
  4439. (calendar-gregorian-from-absolute sd)))
  4440. (n1 org-agenda-start-on-weekday)
  4441. (d (- nt n1)))
  4442. (- sd (+ (if (< d 0) 7 0) d)))))
  4443. (day-numbers (list start))
  4444. (inhibit-redisplay t)
  4445. s e rtn rtnall file date d start-pos end-pos todayp nd)
  4446. (setq org-agenda-redo-command
  4447. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  4448. ;; Make the list of days
  4449. (setq ndays (or ndays org-agenda-ndays)
  4450. nd ndays)
  4451. (while (> ndays 1)
  4452. (push (1+ (car day-numbers)) day-numbers)
  4453. (setq ndays (1- ndays)))
  4454. (setq day-numbers (nreverse day-numbers))
  4455. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  4456. (progn
  4457. (delete-other-windows)
  4458. (switch-to-buffer-other-window
  4459. (get-buffer-create org-agenda-buffer-name))))
  4460. (setq buffer-read-only nil)
  4461. (erase-buffer)
  4462. (org-agenda-mode) (setq buffer-read-only nil)
  4463. (set (make-local-variable 'org-agenda-type) 'agenda)
  4464. (set (make-local-variable 'starting-day) (car day-numbers))
  4465. (set (make-local-variable 'include-all-loc) include-all)
  4466. (when (and (or include-all org-agenda-include-all-todo)
  4467. (member today day-numbers))
  4468. (setq files thefiles
  4469. rtnall nil)
  4470. (while (setq file (pop files))
  4471. (catch 'nextfile
  4472. (org-check-agenda-file file)
  4473. (setq date (calendar-gregorian-from-absolute today)
  4474. rtn (org-agenda-get-day-entries
  4475. file date :todo))
  4476. (setq rtnall (append rtnall rtn))))
  4477. (when rtnall
  4478. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  4479. (add-text-properties (point-min) (1- (point))
  4480. (list 'face 'org-link))
  4481. (insert (org-finalize-agenda-entries rtnall) "\n")))
  4482. (while (setq d (pop day-numbers))
  4483. (setq date (calendar-gregorian-from-absolute d)
  4484. s (point))
  4485. (if (or (setq todayp (= d today))
  4486. (and (not start-pos) (= d sd)))
  4487. (setq start-pos (point))
  4488. (if (and start-pos (not end-pos))
  4489. (setq end-pos (point))))
  4490. (setq files thefiles
  4491. rtnall nil)
  4492. (while (setq file (pop files))
  4493. (catch 'nextfile
  4494. (org-check-agenda-file file)
  4495. (if org-agenda-show-log
  4496. (setq rtn (org-agenda-get-day-entries
  4497. file date
  4498. :deadline :scheduled :timestamp :closed))
  4499. (setq rtn (org-agenda-get-day-entries
  4500. file date
  4501. :deadline :scheduled :timestamp)))
  4502. (setq rtnall (append rtnall rtn))))
  4503. (if org-agenda-include-diary
  4504. (progn
  4505. (require 'diary-lib)
  4506. (setq rtn (org-get-entries-from-diary date))
  4507. (setq rtnall (append rtnall rtn))))
  4508. (if (or rtnall org-agenda-show-all-dates)
  4509. (progn
  4510. (insert (format "%-9s %2d %s %4d\n"
  4511. (calendar-day-name date)
  4512. (extract-calendar-day date)
  4513. (calendar-month-name (extract-calendar-month date))
  4514. (extract-calendar-year date)))
  4515. (put-text-property s (1- (point)) 'face
  4516. 'org-link)
  4517. (if rtnall (insert
  4518. (org-finalize-agenda-entries
  4519. (org-agenda-add-time-grid-maybe
  4520. rtnall nd todayp))
  4521. "\n"))
  4522. (put-text-property s (1- (point)) 'day d))))
  4523. (goto-char (point-min))
  4524. (setq buffer-read-only t)
  4525. (org-fit-agenda-window)
  4526. (unless (and (pos-visible-in-window-p (point-min))
  4527. (pos-visible-in-window-p (point-max)))
  4528. (goto-char (1- (point-max)))
  4529. (recenter -1)
  4530. (if (not (pos-visible-in-window-p (or start-pos 1)))
  4531. (progn
  4532. (goto-char (or start-pos 1))
  4533. (recenter 1))))
  4534. (goto-char (or start-pos 1))
  4535. (if (not org-select-agenda-window) (select-window win))
  4536. (message "")))
  4537. (defvar org-select-this-todo-keyword nil)
  4538. ;;;###autoload
  4539. (defun org-todo-list (arg &optional keep-modes)
  4540. "Show all TODO entries from all agenda file in a single list.
  4541. The prefix arg can be used to select a specific TODO keyword and limit
  4542. the list to these. When using \\[universal-argument], you will be prompted
  4543. for a keyword. A numeric prefix directly selects the Nth keyword in
  4544. `org-todo-keywords'."
  4545. (interactive "P")
  4546. (org-agenda-maybe-reset-markers 'force)
  4547. (org-compile-prefix-format org-agenda-prefix-format)
  4548. (let* ((org-agenda-keep-modes keep-modes)
  4549. (today (time-to-days (current-time)))
  4550. (date (calendar-gregorian-from-absolute today))
  4551. (win (selected-window))
  4552. (kwds org-todo-keywords)
  4553. (completion-ignore-case t)
  4554. (org-select-this-todo-keyword
  4555. (if (stringp arg) arg
  4556. (and arg (integerp arg) (nth (1- arg) org-todo-keywords))))
  4557. rtn rtnall files file pos)
  4558. (when (equal arg '(4))
  4559. (setq org-select-this-todo-keyword
  4560. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  4561. nil t)))
  4562. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  4563. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  4564. (progn
  4565. (delete-other-windows)
  4566. (switch-to-buffer-other-window
  4567. (get-buffer-create org-agenda-buffer-name))))
  4568. (setq buffer-read-only nil)
  4569. (erase-buffer)
  4570. (org-agenda-mode) (setq buffer-read-only nil)
  4571. (set (make-local-variable 'org-agenda-type) 'todo)
  4572. (set (make-local-variable 'last-arg) arg)
  4573. (set (make-local-variable 'org-todo-keywords) kwds)
  4574. (set (make-local-variable 'org-agenda-redo-command)
  4575. '(org-todo-list (or current-prefix-arg last-arg) t))
  4576. (setq files (org-agenda-files)
  4577. rtnall nil)
  4578. (while (setq file (pop files))
  4579. (catch 'nextfile
  4580. (org-check-agenda-file file)
  4581. (setq rtn (org-agenda-get-day-entries file date :todo))
  4582. (setq rtnall (append rtnall rtn))))
  4583. (insert "Global list of TODO items of type: ")
  4584. (add-text-properties (point-min) (1- (point))
  4585. (list 'face 'org-link))
  4586. (setq pos (point))
  4587. (insert (or org-select-this-todo-keyword "ALL") "\n")
  4588. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  4589. (setq pos (point))
  4590. (insert
  4591. "Available with `N r': (0)ALL "
  4592. (let ((n 0))
  4593. (mapconcat (lambda (x)
  4594. (format "(%d)%s" (setq n (1+ n)) x))
  4595. org-todo-keywords " "))
  4596. "\n")
  4597. (add-text-properties pos (1- (point)) (list 'face 'org-link))
  4598. (when rtnall
  4599. (insert (org-finalize-agenda-entries rtnall) "\n"))
  4600. (goto-char (point-min))
  4601. (setq buffer-read-only t)
  4602. (org-fit-agenda-window)
  4603. (if (not org-select-agenda-window) (select-window win))))
  4604. (defun org-check-agenda-file (file)
  4605. "Make sure FILE exists. If not, ask user what to do."
  4606. (when (not (file-exists-p file))
  4607. (message "non-existent file %s. [R]emove from list or [A]bort?"
  4608. (abbreviate-file-name file))
  4609. (let ((r (downcase (read-char-exclusive))))
  4610. (cond
  4611. ((equal r ?r)
  4612. (org-remove-file file)
  4613. (throw 'nextfile t))
  4614. (t (error "Abort"))))))
  4615. (defun org-agenda-check-type (error &rest types)
  4616. "Check if agenda buffer is of allowed type.
  4617. If ERROR is non-nil, throw an error, otherwise just return nil."
  4618. (if (memq org-agenda-type types)
  4619. t
  4620. (if error
  4621. (error "Now allowed in %s-type agenda buffers" org-agenda-type)
  4622. nil)))
  4623. (defun org-agenda-quit ()
  4624. "Exit agenda by removing the window or the buffer."
  4625. (interactive)
  4626. (let ((buf (current-buffer)))
  4627. (if (not (one-window-p)) (delete-window))
  4628. (kill-buffer buf)
  4629. (org-agenda-maybe-reset-markers 'force)))
  4630. (defun org-agenda-exit ()
  4631. "Exit agenda by removing the window or the buffer.
  4632. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  4633. Org-mode buffers visited directly by the user will not be touched."
  4634. (interactive)
  4635. (org-release-buffers org-agenda-new-buffers)
  4636. (setq org-agenda-new-buffers nil)
  4637. (org-agenda-quit))
  4638. (defun org-agenda-redo ()
  4639. "Rebuild Agenda.
  4640. When this is the global TODO list, a prefix argument will be interpreted."
  4641. (interactive)
  4642. (message "Rebuilding agenda buffer...")
  4643. (eval org-agenda-redo-command)
  4644. (message "Rebuilding agenda buffer...done"))
  4645. (defun org-agenda-goto-today ()
  4646. "Go to today."
  4647. (interactive)
  4648. (org-agenda-check-type t 'timeline 'agenda)
  4649. (if (boundp 'starting-day)
  4650. (let ((cmd (car org-agenda-redo-command))
  4651. (iall (nth 1 org-agenda-redo-command))
  4652. (nday (nth 3 org-agenda-redo-command))
  4653. (keep (nth 4 org-agenda-redo-command)))
  4654. (eval (list cmd iall nil nday keep)))
  4655. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  4656. (point-min)))))
  4657. (defun org-agenda-later (arg)
  4658. "Go forward in time by `org-agenda-ndays' days.
  4659. With prefix ARG, go forward that many times `org-agenda-ndays'."
  4660. (interactive "p")
  4661. (org-agenda-check-type t 'agenda)
  4662. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  4663. (+ starting-day (* arg org-agenda-ndays)) nil t))
  4664. (defun org-agenda-earlier (arg)
  4665. "Go back in time by `org-agenda-ndays' days.
  4666. With prefix ARG, go back that many times `org-agenda-ndays'."
  4667. (interactive "p")
  4668. (org-agenda-check-type t 'agenda)
  4669. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  4670. (- starting-day (* arg org-agenda-ndays)) nil t))
  4671. (defun org-agenda-week-view ()
  4672. "Switch to weekly view for agenda."
  4673. (interactive)
  4674. (org-agenda-check-type t 'agenda)
  4675. (setq org-agenda-ndays 7)
  4676. (org-agenda-list include-all-loc
  4677. (or (get-text-property (point) 'day)
  4678. starting-day)
  4679. nil t)
  4680. (org-agenda-set-mode-name)
  4681. (message "Switched to week view"))
  4682. (defun org-agenda-day-view ()
  4683. "Switch to daily view for agenda."
  4684. (interactive)
  4685. (org-agenda-check-type t 'agenda)
  4686. (setq org-agenda-ndays 1)
  4687. (org-agenda-list include-all-loc
  4688. (or (get-text-property (point) 'day)
  4689. starting-day)
  4690. nil t)
  4691. (org-agenda-set-mode-name)
  4692. (message "Switched to day view"))
  4693. (defun org-agenda-next-date-line (&optional arg)
  4694. "Jump to the next line indicating a date in agenda buffer."
  4695. (interactive "p")
  4696. (org-agenda-check-type t 'agenda 'timeline)
  4697. (beginning-of-line 1)
  4698. (if (looking-at "^\\S-") (forward-char 1))
  4699. (if (not (re-search-forward "^\\S-" nil t arg))
  4700. (progn
  4701. (backward-char 1)
  4702. (error "No next date after this line in this buffer")))
  4703. (goto-char (match-beginning 0)))
  4704. (defun org-agenda-previous-date-line (&optional arg)
  4705. "Jump to the previous line indicating a date in agenda buffer."
  4706. (interactive "p")
  4707. (org-agenda-check-type t 'agenda 'timeline)
  4708. (beginning-of-line 1)
  4709. (if (not (re-search-backward "^\\S-" nil t arg))
  4710. (error "No previous date before this line in this buffer")))
  4711. ;; Initialize the highlight
  4712. (defvar org-hl (org-make-overlay 1 1))
  4713. (org-overlay-put org-hl 'face 'highlight)
  4714. (defun org-highlight (begin end &optional buffer)
  4715. "Highlight a region with overlay."
  4716. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  4717. org-hl begin end (or buffer (current-buffer))))
  4718. (defun org-unhighlight ()
  4719. "Detach overlay INDEX."
  4720. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  4721. (defun org-agenda-follow-mode ()
  4722. "Toggle follow mode in an agenda buffer."
  4723. (interactive)
  4724. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  4725. (org-agenda-set-mode-name)
  4726. (message "Follow mode is %s"
  4727. (if org-agenda-follow-mode "on" "off")))
  4728. (defun org-agenda-log-mode ()
  4729. "Toggle log mode in an agenda buffer."
  4730. (interactive)
  4731. (org-agenda-check-type t 'agenda 'timeline)
  4732. (setq org-agenda-show-log (not org-agenda-show-log))
  4733. (org-agenda-set-mode-name)
  4734. (org-agenda-redo)
  4735. (message "Log mode is %s"
  4736. (if org-agenda-show-log "on" "off")))
  4737. (defun org-agenda-toggle-diary ()
  4738. "Toggle diary inclusion in an agenda buffer."
  4739. (interactive)
  4740. (org-agenda-check-type t 'agenda)
  4741. (setq org-agenda-include-diary (not org-agenda-include-diary))
  4742. (org-agenda-redo)
  4743. (org-agenda-set-mode-name)
  4744. (message "Diary inclusion turned %s"
  4745. (if org-agenda-include-diary "on" "off")))
  4746. (defun org-agenda-toggle-time-grid ()
  4747. "Toggle time grid in an agenda buffer."
  4748. (interactive)
  4749. (org-agenda-check-type t 'agenda)
  4750. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  4751. (org-agenda-redo)
  4752. (org-agenda-set-mode-name)
  4753. (message "Time-grid turned %s"
  4754. (if org-agenda-use-time-grid "on" "off")))
  4755. (defun org-agenda-set-mode-name ()
  4756. "Set the mode name to indicate all the small mode settings."
  4757. (setq mode-name
  4758. (concat "Org-Agenda"
  4759. (if (equal org-agenda-ndays 1) " Day" "")
  4760. (if (equal org-agenda-ndays 7) " Week" "")
  4761. (if org-agenda-follow-mode " Follow" "")
  4762. (if org-agenda-include-diary " Diary" "")
  4763. (if org-agenda-use-time-grid " Grid" "")
  4764. (if org-agenda-show-log " Log" "")))
  4765. (force-mode-line-update))
  4766. (defun org-agenda-post-command-hook ()
  4767. (and (eolp) (not (bolp)) (backward-char 1))
  4768. (if (and org-agenda-follow-mode
  4769. (get-text-property (point) 'org-marker))
  4770. (org-agenda-show)))
  4771. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  4772. (defun org-get-entries-from-diary (date)
  4773. "Get the (Emacs Calendar) diary entries for DATE."
  4774. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  4775. (diary-display-hook '(fancy-diary-display))
  4776. (list-diary-entries-hook
  4777. (cons 'org-diary-default-entry list-diary-entries-hook))
  4778. (diary-file-name-prefix-function nil) ; turn this feature off
  4779. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  4780. entries
  4781. (org-disable-agenda-to-diary t))
  4782. (save-excursion
  4783. (save-window-excursion
  4784. (list-diary-entries date 1)))
  4785. (if (not (get-buffer fancy-diary-buffer))
  4786. (setq entries nil)
  4787. (with-current-buffer fancy-diary-buffer
  4788. (setq buffer-read-only nil)
  4789. (if (= (point-max) 1)
  4790. ;; No entries
  4791. (setq entries nil)
  4792. ;; Omit the date and other unnecessary stuff
  4793. (org-agenda-cleanup-fancy-diary)
  4794. ;; Add prefix to each line and extend the text properties
  4795. (if (= (point-max) 1)
  4796. (setq entries nil)
  4797. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  4798. (set-buffer-modified-p nil)
  4799. (kill-buffer fancy-diary-buffer)))
  4800. (when entries
  4801. (setq entries (org-split-string entries "\n"))
  4802. (setq entries
  4803. (mapcar
  4804. (lambda (x)
  4805. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  4806. ;; Extend the text properties to the beginning of the line
  4807. (add-text-properties
  4808. 0 (length x)
  4809. (text-properties-at (1- (length x)) x)
  4810. x)
  4811. x)
  4812. entries)))))
  4813. (defun org-agenda-cleanup-fancy-diary ()
  4814. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  4815. This gets rid of the date, the underline under the date, and
  4816. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  4817. date. It also removes lines that contain only whitespace."
  4818. (goto-char (point-min))
  4819. (if (looking-at ".*?:[ \t]*")
  4820. (progn
  4821. (replace-match "")
  4822. (re-search-forward "\n=+$" nil t)
  4823. (replace-match "")
  4824. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  4825. (re-search-forward "\n=+$" nil t)
  4826. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  4827. (goto-char (point-min))
  4828. (while (re-search-forward "^ +\n" nil t)
  4829. (replace-match ""))
  4830. (goto-char (point-min))
  4831. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  4832. (replace-match "")))
  4833. ;; Make sure entries from the diary have the right text properties.
  4834. (eval-after-load "diary-lib"
  4835. '(if (boundp 'diary-modify-entry-list-string-function)
  4836. ;; We can rely on the hook, nothing to do
  4837. nil
  4838. ;; Hook not avaiable, must use advice to make this work
  4839. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  4840. "Make the position visible."
  4841. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  4842. (stringp string)
  4843. buffer-file-name)
  4844. (setq string (org-modify-diary-entry-string string))))))
  4845. (defun org-modify-diary-entry-string (string)
  4846. "Add text properties to string, allowing org-mode to act on it."
  4847. (add-text-properties
  4848. 0 (length string)
  4849. (list 'mouse-face 'highlight
  4850. 'keymap org-agenda-keymap
  4851. 'help-echo
  4852. (format
  4853. "mouse-2 or RET jump to diary file %s"
  4854. (abbreviate-file-name buffer-file-name))
  4855. 'org-agenda-diary-link t
  4856. 'org-marker (org-agenda-new-marker (point-at-bol)))
  4857. string)
  4858. string)
  4859. (defun org-diary-default-entry ()
  4860. "Add a dummy entry to the diary.
  4861. Needed to avoid empty dates which mess up holiday display."
  4862. ;; Catch the error if dealing with the new add-to-diary-alist
  4863. (when org-disable-agenda-to-diary
  4864. (condition-case nil
  4865. (add-to-diary-list original-date "Org-mode dummy" "")
  4866. (error
  4867. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  4868. (defun org-cycle-agenda-files ()
  4869. "Cycle through the files in `org-agenda-files'.
  4870. If the current buffer visits an agenda file, find the next one in the list.
  4871. If the current buffer does not, find the first agenda file."
  4872. (interactive)
  4873. (let* ((fs (org-agenda-files t))
  4874. (files (append fs (list (car fs))))
  4875. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  4876. file)
  4877. (unless files (error "No agenda files"))
  4878. (catch 'exit
  4879. (while (setq file (pop files))
  4880. (if (equal (file-truename file) tcf)
  4881. (when (car files)
  4882. (find-file (car files))
  4883. (throw 'exit t))))
  4884. (find-file (car fs)))))
  4885. (defun org-agenda-file-to-end ()
  4886. "Move/add the current file to the end of the agenda file list.
  4887. If the file is not present in the list, it is appended to the list. If it is
  4888. present, it is moved there."
  4889. (interactive)
  4890. (org-agenda-file-to-front 'to-end))
  4891. (defun org-agenda-file-to-front (&optional to-end)
  4892. "Move/add the current file to the top of the agenda file list.
  4893. If the file is not present in the list, it is added to the front. If it is
  4894. present, it is moved there. With optional argument TO-END, add/move to the
  4895. end of the list."
  4896. (interactive "P")
  4897. (let ((file-alist (mapcar (lambda (x)
  4898. (cons (file-truename x) x))
  4899. (org-agenda-files t)))
  4900. (ctf (file-truename buffer-file-name))
  4901. x had)
  4902. (setq x (assoc ctf file-alist) had x)
  4903. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  4904. (if to-end
  4905. (setq file-alist (append (delq x file-alist) (list x)))
  4906. (setq file-alist (cons x (delq x file-alist))))
  4907. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  4908. (org-install-agenda-files-menu)
  4909. (message "File %s to %s of agenda file list"
  4910. (if had "moved" "added") (if to-end "end" "front"))))
  4911. (defun org-remove-file (&optional file)
  4912. "Remove current file from the list of files in variable `org-agenda-files'.
  4913. These are the files which are being checked for agenda entries.
  4914. Optional argument FILE means, use this file instead of the current."
  4915. (interactive)
  4916. (let* ((file (or file buffer-file-name))
  4917. (true-file (file-truename file))
  4918. (afile (abbreviate-file-name file))
  4919. (files (delq nil (mapcar
  4920. (lambda (x)
  4921. (if (equal true-file
  4922. (file-truename x))
  4923. nil x))
  4924. (org-agenda-files t)))))
  4925. (if (not (= (length files) (length (org-agenda-files t))))
  4926. (progn
  4927. (org-store-new-agenda-file-list files)
  4928. (org-install-agenda-files-menu)
  4929. (message "Removed file: %s" afile))
  4930. (message "File was not in list: %s" afile))))
  4931. (defun org-file-menu-entry (file)
  4932. (vector file (list 'find-file file) t))
  4933. ;; FIXME: Maybe we removed a buffer visited through the menu from
  4934. ;; org-agenda-new-buffers, so that the buffer will not be removed
  4935. ;; when exiting the agenda????
  4936. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive)
  4937. "Return a list of all relevant day numbers from BEG to END buffer positions.
  4938. If NO-RANGES is non-nil, include only the start and end dates of a range,
  4939. not every single day in the range. If FORCE-TODAY is non-nil, make
  4940. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  4941. inactive time stamps (those in square brackets) are included."
  4942. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  4943. dates date day day1 day2 ts1 ts2)
  4944. (if force-today
  4945. (setq dates (list (time-to-days (current-time)))))
  4946. (save-excursion
  4947. (goto-char beg)
  4948. (while (re-search-forward re end t)
  4949. (setq day (time-to-days (org-time-string-to-time
  4950. (substring (match-string 1) 0 10))))
  4951. (or (memq day dates) (push day dates)))
  4952. (unless no-ranges
  4953. (goto-char beg)
  4954. (while (re-search-forward org-tr-regexp end t)
  4955. (setq ts1 (substring (match-string 1) 0 10)
  4956. ts2 (substring (match-string 2) 0 10)
  4957. day1 (time-to-days (org-time-string-to-time ts1))
  4958. day2 (time-to-days (org-time-string-to-time ts2)))
  4959. (while (< (setq day1 (1+ day1)) day2)
  4960. (or (memq day1 dates) (push day1 dates)))))
  4961. (sort dates '<))))
  4962. ;;;###autoload
  4963. (defun org-diary (&rest args)
  4964. "Return diary information from org-files.
  4965. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  4966. It accesses org files and extracts information from those files to be
  4967. listed in the diary. The function accepts arguments specifying what
  4968. items should be listed. The following arguments are allowed:
  4969. :timestamp List the headlines of items containing a date stamp or
  4970. date range matching the selected date. Deadlines will
  4971. also be listed, on the expiration day.
  4972. :deadline List any deadlines past due, or due within
  4973. `org-deadline-warning-days'. The listing occurs only
  4974. in the diary for *today*, not at any other date. If
  4975. an entry is marked DONE, it is no longer listed.
  4976. :scheduled List all items which are scheduled for the given date.
  4977. The diary for *today* also contains items which were
  4978. scheduled earlier and are not yet marked DONE.
  4979. :todo List all TODO items from the org-file. This may be a
  4980. long list - so this is not turned on by default.
  4981. Like deadlines, these entries only show up in the
  4982. diary for *today*, not at any other date.
  4983. The call in the diary file should look like this:
  4984. &%%(org-diary) ~/path/to/some/orgfile.org
  4985. Use a separate line for each org file to check. Or, if you omit the file name,
  4986. all files listed in `org-agenda-files' will be checked automatically:
  4987. &%%(org-diary)
  4988. If you don't give any arguments (as in the example above), the default
  4989. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  4990. also be written as
  4991. &%%(org-diary :deadline :timestamp :scheduled)
  4992. The function expects the lisp variables `entry' and `date' to be provided
  4993. by the caller, because this is how the calendar works. Don't use this
  4994. function from a program - use `org-agenda-get-day-entries' instead."
  4995. (org-agenda-maybe-reset-markers)
  4996. (org-compile-prefix-format org-agenda-prefix-format)
  4997. (setq args (or args '(:deadline :scheduled :timestamp)))
  4998. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  4999. (list entry)
  5000. (org-agenda-files t)))
  5001. file rtn results)
  5002. ;; If this is called during org-agenda, don't return any entries to
  5003. ;; the calendar. Org Agenda will list these entries itself.
  5004. (if org-disable-agenda-to-diary (setq files nil))
  5005. (while (setq file (pop files))
  5006. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  5007. (setq results (append results rtn)))
  5008. (if results
  5009. (concat (org-finalize-agenda-entries results) "\n"))))
  5010. (defvar org-category-table nil)
  5011. (defun org-get-category-table ()
  5012. "Get the table of categories and positions in current buffer."
  5013. (let (tbl)
  5014. (save-excursion
  5015. (goto-char (point-min))
  5016. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  5017. (push (cons (point) (org-trim (match-string 2))) tbl)))
  5018. tbl))
  5019. (defun org-get-category (&optional pos)
  5020. "Get the category applying to position POS."
  5021. (if (not org-category-table)
  5022. (cond
  5023. ((null org-category)
  5024. (setq org-category
  5025. (if buffer-file-name
  5026. (file-name-sans-extension
  5027. (file-name-nondirectory buffer-file-name))
  5028. "???")))
  5029. ((symbolp org-category) (symbol-name org-category))
  5030. (t org-category))
  5031. (let ((tbl org-category-table)
  5032. (pos (or pos (point))))
  5033. (while (and tbl (> (caar tbl) pos))
  5034. (pop tbl))
  5035. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  5036. org-category-table))))))
  5037. (defun org-agenda-get-day-entries (file date &rest args)
  5038. "Does the work for `org-diary' and `org-agenda'.
  5039. FILE is the path to a file to be checked for entries. DATE is date like
  5040. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5041. which kind of entries should be extracted. For details about these, see
  5042. the documentation of `org-diary'."
  5043. (setq args (or args '(:deadline :scheduled :timestamp)))
  5044. (let* ((org-startup-with-deadline-check nil)
  5045. (org-startup-folded nil)
  5046. (buffer (if (file-exists-p file)
  5047. (org-get-agenda-file-buffer file)
  5048. (error "No such file %s" file)))
  5049. arg results rtn)
  5050. (if (not buffer)
  5051. ;; If file does not exist, make sure an error message ends up in diary
  5052. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5053. (with-current-buffer buffer
  5054. (unless (eq major-mode 'org-mode)
  5055. (error "Agenda file %s is not in `org-mode'" file))
  5056. (setq org-category-table (org-get-category-table))
  5057. (let ((case-fold-search nil))
  5058. (save-excursion
  5059. (save-restriction
  5060. (if org-respect-restriction
  5061. (if (org-region-active-p)
  5062. ;; Respect a region to restrict search
  5063. (narrow-to-region (region-beginning) (region-end)))
  5064. ;; If we work for the calendar or many files,
  5065. ;; get rid of any restriction
  5066. (widen))
  5067. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  5068. (while (setq arg (pop args))
  5069. (cond
  5070. ((and (eq arg :todo)
  5071. (equal date (calendar-current-date)))
  5072. (setq rtn (org-agenda-get-todos))
  5073. (setq results (append results rtn)))
  5074. ((eq arg :timestamp)
  5075. (setq rtn (org-agenda-get-blocks))
  5076. (setq results (append results rtn))
  5077. (setq rtn (org-agenda-get-timestamps))
  5078. (setq results (append results rtn)))
  5079. ((eq arg :scheduled)
  5080. (setq rtn (org-agenda-get-scheduled))
  5081. (setq results (append results rtn)))
  5082. ((eq arg :closed)
  5083. (setq rtn (org-agenda-get-closed))
  5084. (setq results (append results rtn)))
  5085. ((and (eq arg :deadline)
  5086. (equal date (calendar-current-date)))
  5087. (setq rtn (org-agenda-get-deadlines))
  5088. (setq results (append results rtn))))))))
  5089. results))))
  5090. (defun org-entry-is-done-p ()
  5091. "Is the current entry marked DONE?"
  5092. (save-excursion
  5093. (and (re-search-backward "[\r\n]\\*" nil t)
  5094. (looking-at org-nl-done-regexp))))
  5095. (defun org-at-date-range-p ()
  5096. "Is the cursor inside a date range?"
  5097. (interactive)
  5098. (save-excursion
  5099. (catch 'exit
  5100. (let ((pos (point)))
  5101. (skip-chars-backward "^<\r\n")
  5102. (skip-chars-backward "<")
  5103. (and (looking-at org-tr-regexp)
  5104. (>= (match-end 0) pos)
  5105. (throw 'exit t))
  5106. (skip-chars-backward "^<\r\n")
  5107. (skip-chars-backward "<")
  5108. (and (looking-at org-tr-regexp)
  5109. (>= (match-end 0) pos)
  5110. (throw 'exit t)))
  5111. nil)))
  5112. (defun org-agenda-get-todos ()
  5113. "Return the TODO information for agenda display."
  5114. (let* ((props (list 'face nil
  5115. 'done-face 'org-done
  5116. 'mouse-face 'highlight
  5117. 'keymap org-agenda-keymap
  5118. 'help-echo
  5119. (format "mouse-2 or RET jump to org file %s"
  5120. (abbreviate-file-name buffer-file-name))))
  5121. (regexp (concat "[\n\r]\\*+ *\\("
  5122. (if org-select-this-todo-keyword
  5123. (concat "\\<\\(" org-select-this-todo-keyword
  5124. "\\)\\>")
  5125. org-not-done-regexp)
  5126. "[^\n\r]*\\)"))
  5127. marker priority category tags
  5128. ee txt)
  5129. (goto-char (point-min))
  5130. (while (re-search-forward regexp nil t)
  5131. (goto-char (match-beginning 1))
  5132. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  5133. category (org-get-category)
  5134. tags (org-get-tags-at (point))
  5135. txt (org-format-agenda-item "" (match-string 1) category tags)
  5136. priority
  5137. (+ (org-get-priority txt)
  5138. (if org-todo-kwd-priority-p
  5139. (- org-todo-kwd-max-priority -2
  5140. (length
  5141. (member (match-string 2) org-todo-keywords)))
  5142. 1)))
  5143. (add-text-properties
  5144. 0 (length txt) (append (list 'org-marker marker 'org-hd-marker marker
  5145. 'priority priority 'category category)
  5146. props)
  5147. txt)
  5148. (push txt ee)
  5149. (goto-char (match-end 1)))
  5150. (nreverse ee)))
  5151. (defconst org-agenda-no-heading-message
  5152. "No heading for this item in buffer or region.")
  5153. (defun org-agenda-get-timestamps ()
  5154. "Return the date stamp information for agenda display."
  5155. (let* ((props (list 'face nil
  5156. 'mouse-face 'highlight
  5157. 'keymap org-agenda-keymap
  5158. 'help-echo
  5159. (format "mouse-2 or RET jump to org file %s"
  5160. (abbreviate-file-name buffer-file-name))))
  5161. (regexp (regexp-quote
  5162. (substring
  5163. (format-time-string
  5164. (car org-time-stamp-formats)
  5165. (apply 'encode-time ; DATE bound by calendar
  5166. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5167. 0 11)))
  5168. marker hdmarker deadlinep scheduledp donep tmp priority category
  5169. ee txt timestr tags)
  5170. (goto-char (point-min))
  5171. (while (re-search-forward regexp nil t)
  5172. (if (not (save-match-data (org-at-date-range-p)))
  5173. (progn
  5174. (setq marker (org-agenda-new-marker (match-beginning 0))
  5175. category (org-get-category (match-beginning 0))
  5176. tmp (buffer-substring (max (point-min)
  5177. (- (match-beginning 0)
  5178. org-ds-keyword-length))
  5179. (match-beginning 0))
  5180. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5181. deadlinep (string-match org-deadline-regexp tmp)
  5182. scheduledp (string-match org-scheduled-regexp tmp)
  5183. donep (org-entry-is-done-p))
  5184. (if (string-match ">" timestr)
  5185. ;; substring should only run to end of time stamp
  5186. (setq timestr (substring timestr 0 (match-end 0))))
  5187. (save-excursion
  5188. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5189. (progn
  5190. (goto-char (match-end 1))
  5191. (setq hdmarker (org-agenda-new-marker)
  5192. tags (org-get-tags-at))
  5193. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5194. (setq txt (org-format-agenda-item
  5195. (format "%s%s"
  5196. (if deadlinep "Deadline: " "")
  5197. (if scheduledp "Scheduled: " ""))
  5198. (match-string 1) category tags timestr)))
  5199. (setq txt org-agenda-no-heading-message))
  5200. (setq priority (org-get-priority txt))
  5201. (add-text-properties
  5202. 0 (length txt) (append (list 'org-marker marker
  5203. 'org-hd-marker hdmarker) props)
  5204. txt)
  5205. (if deadlinep
  5206. (add-text-properties
  5207. 0 (length txt)
  5208. (list 'face
  5209. (if donep 'org-done 'org-warning)
  5210. 'undone-face 'org-warning
  5211. 'done-face 'org-done
  5212. 'category category
  5213. 'priority (+ 100 priority))
  5214. txt)
  5215. (if scheduledp
  5216. (add-text-properties
  5217. 0 (length txt)
  5218. (list 'face 'org-scheduled-today
  5219. 'undone-face 'org-scheduled-today
  5220. 'done-face 'org-done
  5221. 'category category
  5222. priority (+ 99 priority))
  5223. txt)
  5224. (add-text-properties
  5225. 0 (length txt)
  5226. (list 'priority priority 'category category) txt)))
  5227. (push txt ee))
  5228. (outline-next-heading))))
  5229. (nreverse ee)))
  5230. (defun org-agenda-get-closed ()
  5231. "Return the logged TODO entries for agenda display."
  5232. (let* ((props (list 'mouse-face 'highlight
  5233. 'keymap org-agenda-keymap
  5234. 'help-echo
  5235. (format "mouse-2 or RET jump to org file %s"
  5236. (abbreviate-file-name buffer-file-name))))
  5237. (regexp (concat
  5238. "\\<" org-closed-string " *\\["
  5239. (regexp-quote
  5240. (substring
  5241. (format-time-string
  5242. (car org-time-stamp-formats)
  5243. (apply 'encode-time ; DATE bound by calendar
  5244. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5245. 1 11))))
  5246. marker hdmarker priority category tags
  5247. ee txt timestr)
  5248. (goto-char (point-min))
  5249. (while (re-search-forward regexp nil t)
  5250. (if (not (save-match-data (org-at-date-range-p)))
  5251. (progn
  5252. (setq marker (org-agenda-new-marker (match-beginning 0))
  5253. category (org-get-category (match-beginning 0))
  5254. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5255. ;; donep (org-entry-is-done-p)
  5256. )
  5257. (if (string-match "\\]" timestr)
  5258. ;; substring should only run to end of time stamp
  5259. (setq timestr (substring timestr 0 (match-end 0))))
  5260. (save-excursion
  5261. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5262. (progn
  5263. (goto-char (match-end 1))
  5264. (setq hdmarker (org-agenda-new-marker)
  5265. tags (org-get-tags-at))
  5266. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5267. (setq txt (org-format-agenda-item
  5268. "Closed: "
  5269. (match-string 1) category tags timestr)))
  5270. (setq txt org-agenda-no-heading-message))
  5271. (setq priority 100000)
  5272. (add-text-properties
  5273. 0 (length txt) (append (list 'org-marker marker
  5274. 'org-hd-marker hdmarker
  5275. 'face 'org-done
  5276. 'priority priority
  5277. 'category category
  5278. 'undone-face 'org-warning
  5279. 'done-face 'org-done) props)
  5280. txt)
  5281. (push txt ee))
  5282. (outline-next-heading))))
  5283. (nreverse ee)))
  5284. (defun org-agenda-get-deadlines ()
  5285. "Return the deadline information for agenda display."
  5286. (let* ((wdays org-deadline-warning-days)
  5287. (props (list 'mouse-face 'highlight
  5288. 'keymap org-agenda-keymap
  5289. 'help-echo
  5290. (format "mouse-2 or RET jump to org file %s"
  5291. (abbreviate-file-name buffer-file-name))))
  5292. (regexp org-deadline-time-regexp)
  5293. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5294. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5295. d2 diff pos pos1 category tags
  5296. ee txt head)
  5297. (goto-char (point-min))
  5298. (while (re-search-forward regexp nil t)
  5299. (setq pos (1- (match-beginning 1))
  5300. d2 (time-to-days
  5301. (org-time-string-to-time (match-string 1)))
  5302. diff (- d2 d1))
  5303. ;; When to show a deadline in the calendar:
  5304. ;; If the expiration is within wdays warning time.
  5305. ;; Past-due deadlines are only shown on the current date
  5306. (if (and (< diff wdays) todayp (not (= diff 0)))
  5307. (save-excursion
  5308. (setq category (org-get-category))
  5309. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5310. (progn
  5311. (goto-char (match-end 0))
  5312. (setq pos1 (match-end 1))
  5313. (setq tags (org-get-tags-at pos1))
  5314. (setq head (buffer-substring-no-properties
  5315. (point)
  5316. (progn (skip-chars-forward "^\r\n")
  5317. (point))))
  5318. (if (string-match org-looking-at-done-regexp head)
  5319. (setq txt nil)
  5320. (setq txt (org-format-agenda-item
  5321. (format "In %3d d.: " diff) head category tags))))
  5322. (setq txt org-agenda-no-heading-message))
  5323. (when txt
  5324. (add-text-properties
  5325. 0 (length txt)
  5326. (append
  5327. (list 'org-marker (org-agenda-new-marker pos)
  5328. 'org-hd-marker (org-agenda-new-marker pos1)
  5329. 'priority (+ (- 10 diff) (org-get-priority txt))
  5330. 'category category
  5331. 'face (cond ((<= diff 0) 'org-warning)
  5332. ((<= diff 5) 'org-scheduled-previously)
  5333. (t nil))
  5334. 'undone-face (cond
  5335. ((<= diff 0) 'org-warning)
  5336. ((<= diff 5) 'org-scheduled-previously)
  5337. (t nil))
  5338. 'done-face 'org-done)
  5339. props)
  5340. txt)
  5341. (push txt ee)))))
  5342. ee))
  5343. (defun org-agenda-get-scheduled ()
  5344. "Return the scheduled information for agenda display."
  5345. (let* ((props (list 'face 'org-scheduled-previously
  5346. 'undone-face 'org-scheduled-previously
  5347. 'done-face 'org-done
  5348. 'mouse-face 'highlight
  5349. 'keymap org-agenda-keymap
  5350. 'help-echo
  5351. (format "mouse-2 or RET jump to org file %s"
  5352. (abbreviate-file-name buffer-file-name))))
  5353. (regexp org-scheduled-time-regexp)
  5354. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5355. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5356. d2 diff pos pos1 category tags
  5357. ee txt head)
  5358. (goto-char (point-min))
  5359. (while (re-search-forward regexp nil t)
  5360. (setq pos (1- (match-beginning 1))
  5361. d2 (time-to-days
  5362. (org-time-string-to-time (match-string 1)))
  5363. diff (- d2 d1))
  5364. ;; When to show a scheduled item in the calendar:
  5365. ;; If it is on or past the date.
  5366. (if (and (< diff 0) todayp)
  5367. (save-excursion
  5368. (setq category (org-get-category))
  5369. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5370. (progn
  5371. (goto-char (match-end 0))
  5372. (setq pos1 (match-end 1))
  5373. (setq tags (org-get-tags-at))
  5374. (setq head (buffer-substring-no-properties
  5375. (point)
  5376. (progn (skip-chars-forward "^\r\n") (point))))
  5377. (if (string-match org-looking-at-done-regexp head)
  5378. (setq txt nil)
  5379. (setq txt (org-format-agenda-item
  5380. (format "Sched.%2dx: " (- 1 diff)) head
  5381. category tags))))
  5382. (setq txt org-agenda-no-heading-message))
  5383. (when txt
  5384. (add-text-properties
  5385. 0 (length txt)
  5386. (append (list 'org-marker (org-agenda-new-marker pos)
  5387. 'org-hd-marker (org-agenda-new-marker pos1)
  5388. 'priority (+ (- 5 diff) (org-get-priority txt))
  5389. 'category category)
  5390. props) txt)
  5391. (push txt ee)))))
  5392. ee))
  5393. (defun org-agenda-get-blocks ()
  5394. "Return the date-range information for agenda display."
  5395. (let* ((props (list 'face nil
  5396. 'mouse-face 'highlight
  5397. 'keymap org-agenda-keymap
  5398. 'help-echo
  5399. (format "mouse-2 or RET jump to org file %s"
  5400. (abbreviate-file-name buffer-file-name))))
  5401. (regexp org-tr-regexp)
  5402. (d0 (calendar-absolute-from-gregorian date))
  5403. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  5404. (goto-char (point-min))
  5405. (while (re-search-forward regexp nil t)
  5406. (setq timestr (match-string 0)
  5407. s1 (match-string 1)
  5408. s2 (match-string 2)
  5409. d1 (time-to-days (org-time-string-to-time s1))
  5410. d2 (time-to-days (org-time-string-to-time s2)))
  5411. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  5412. ;; Only allow days between the limits, because the normal
  5413. ;; date stamps will catch the limits.
  5414. (save-excursion
  5415. (setq marker (org-agenda-new-marker (point)))
  5416. (setq category (org-get-category))
  5417. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5418. (progn
  5419. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  5420. (goto-char (match-end 1))
  5421. (setq tags (org-get-tags-at))
  5422. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5423. (setq txt (org-format-agenda-item
  5424. (format (if (= d1 d2) "" "(%d/%d): ")
  5425. (1+ (- d0 d1)) (1+ (- d2 d1)))
  5426. (match-string 1) category tags
  5427. (if (= d0 d1) timestr))))
  5428. (setq txt org-agenda-no-heading-message))
  5429. (add-text-properties
  5430. 0 (length txt) (append (list 'org-marker marker
  5431. 'org-hd-marker hdmarker
  5432. 'priority (org-get-priority txt)
  5433. 'category category)
  5434. props)
  5435. txt)
  5436. (push txt ee)))
  5437. (outline-next-heading))
  5438. ;; Sort the entries by expiration date.
  5439. (nreverse ee)))
  5440. (defconst org-plain-time-of-day-regexp
  5441. (concat
  5442. "\\(\\<[012]?[0-9]"
  5443. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  5444. "\\(--?"
  5445. "\\(\\<[012]?[0-9]"
  5446. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  5447. "\\)?")
  5448. "Regular expression to match a plain time or time range.
  5449. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  5450. groups carry important information:
  5451. 0 the full match
  5452. 1 the first time, range or not
  5453. 8 the second time, if it is a range.")
  5454. (defconst org-stamp-time-of-day-regexp
  5455. (concat
  5456. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  5457. "\\([012][0-9]:[0-5][0-9]\\)>"
  5458. "\\(--?"
  5459. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  5460. "Regular expression to match a timestamp time or time range.
  5461. After a match, the following groups carry important information:
  5462. 0 the full match
  5463. 1 date plus weekday, for backreferencing to make sure both times on same day
  5464. 2 the first time, range or not
  5465. 4 the second time, if it is a range.")
  5466. (defvar org-prefix-has-time nil
  5467. "A flag, set by `org-compile-prefix-format'.
  5468. The flag is set if the currently compiled format contains a `%t'.")
  5469. (defvar org-prefix-has-tag nil
  5470. "A flag, set by `org-compile-prefix-format'.
  5471. The flag is set if the currently compiled format contains a `%T'.")
  5472. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  5473. "Format TXT to be inserted into the agenda buffer.
  5474. In particular, it adds the prefix and corresponding text properties. EXTRA
  5475. must be a string and replaces the `%s' specifier in the prefix format.
  5476. CATEGORY (string, symbol or nil) may be used to overrule the default
  5477. category taken from local variable or file name. It will replace the `%c'
  5478. specifier in the format. DOTIME, when non-nil, indicates that a
  5479. time-of-day should be extracted from TXT for sorting of this entry, and for
  5480. the `%t' specifier in the format. When DOTIME is a string, this string is
  5481. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  5482. only the correctly processes TXT should be returned - this is used by
  5483. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  5484. (save-match-data
  5485. ;; Diary entries sometimes have extra whitespace at the beginning
  5486. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  5487. (let* ((category (or category
  5488. org-category
  5489. (if buffer-file-name
  5490. (file-name-sans-extension
  5491. (file-name-nondirectory buffer-file-name))
  5492. "")))
  5493. (tag (or (nth (1- (or (length tags) 0)) tags) ""))
  5494. time ;; needed for the eval of the prefix format
  5495. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  5496. (time-of-day (and dotime (org-get-time-of-day ts)))
  5497. stamp plain s0 s1 s2 rtn)
  5498. (when (and dotime time-of-day org-prefix-has-time)
  5499. ;; Extract starting and ending time and move them to prefix
  5500. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  5501. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  5502. (setq s0 (match-string 0 ts)
  5503. s1 (match-string (if plain 1 2) ts)
  5504. s2 (match-string (if plain 8 4) ts))
  5505. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  5506. ;; them, we might want to remove them there to avoid duplication.
  5507. ;; The user can turn this off with a variable.
  5508. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  5509. (string-match (concat (regexp-quote s0) " *") txt)
  5510. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  5511. (= (match-beginning 0) 0)
  5512. t))
  5513. (setq txt (replace-match "" nil nil txt))))
  5514. ;; Normalize the time(s) to 24 hour
  5515. (if s1 (setq s1 (org-get-time-of-day s1 'string)))
  5516. (if s2 (setq s2 (org-get-time-of-day s2 'string))))
  5517. (when (and (or (eq org-agenda-remove-tags-when-in-prefix t)
  5518. (and org-agenda-remove-tags-when-in-prefix
  5519. org-prefix-has-tag))
  5520. (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" txt))
  5521. (setq txt (replace-match "" t t txt)))
  5522. ;; Create the final string
  5523. (if noprefix
  5524. (setq rtn txt)
  5525. ;; Prepare the variables needed in the eval of the compiled format
  5526. (setq time (cond (s2 (concat s1 "-" s2))
  5527. (s1 (concat s1 "......"))
  5528. (t ""))
  5529. extra (or extra "")
  5530. category (if (symbolp category) (symbol-name category) category))
  5531. ;; Evaluate the compiled format
  5532. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  5533. ;; And finally add the text properties
  5534. (add-text-properties
  5535. 0 (length rtn) (list 'category (downcase category)
  5536. 'tags tags
  5537. 'prefix-length (- (length rtn) (length txt))
  5538. 'time-of-day time-of-day
  5539. 'dotime dotime)
  5540. rtn)
  5541. rtn)))
  5542. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  5543. (catch 'exit
  5544. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  5545. ((and todayp (member 'today (car org-agenda-time-grid))))
  5546. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  5547. ((member 'weekly (car org-agenda-time-grid)))
  5548. (t (throw 'exit list)))
  5549. (let* ((have (delq nil (mapcar
  5550. (lambda (x) (get-text-property 1 'time-of-day x))
  5551. list)))
  5552. (string (nth 1 org-agenda-time-grid))
  5553. (gridtimes (nth 2 org-agenda-time-grid))
  5554. (req (car org-agenda-time-grid))
  5555. (remove (member 'remove-match req))
  5556. new time)
  5557. (if (and (member 'require-timed req) (not have))
  5558. ;; don't show empty grid
  5559. (throw 'exit list))
  5560. (while (setq time (pop gridtimes))
  5561. (unless (and remove (member time have))
  5562. (setq time (int-to-string time))
  5563. (push (org-format-agenda-item
  5564. nil string "" nil ;; FIXME: put a category for the grid?
  5565. (concat (substring time 0 -2) ":" (substring time -2)))
  5566. new)
  5567. (put-text-property
  5568. 1 (length (car new)) 'face 'org-time-grid (car new))))
  5569. (if (member 'time-up org-agenda-sorting-strategy)
  5570. (append new list)
  5571. (append list new)))))
  5572. (defun org-compile-prefix-format (format)
  5573. "Compile the prefix format into a Lisp form that can be evaluated.
  5574. The resulting form is returned and stored in the variable
  5575. `org-prefix-format-compiled'."
  5576. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  5577. (let ((start 0) varform vars var (s format)e c f opt)
  5578. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  5579. s start)
  5580. (setq var (cdr (assoc (match-string 4 s)
  5581. '(("c" . category) ("t" . time) ("s" . extra)
  5582. ("T" . tag))))
  5583. c (or (match-string 3 s) "")
  5584. opt (match-beginning 1)
  5585. start (1+ (match-beginning 0)))
  5586. (if (equal var 'time) (setq org-prefix-has-time t))
  5587. (if (equal var 'tag) (setq org-prefix-has-tag t))
  5588. (setq f (concat "%" (match-string 2 s) "s"))
  5589. (if opt
  5590. (setq varform
  5591. `(if (equal "" ,var)
  5592. ""
  5593. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  5594. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  5595. (setq s (replace-match "%s" t nil s))
  5596. (push varform vars))
  5597. (setq vars (nreverse vars))
  5598. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  5599. (defun org-get-time-of-day (s &optional string)
  5600. "Check string S for a time of day.
  5601. If found, return it as a military time number between 0 and 2400.
  5602. If not found, return nil.
  5603. The optional STRING argument forces conversion into a 5 character wide string
  5604. HH:MM."
  5605. (save-match-data
  5606. (when
  5607. (or
  5608. (string-match
  5609. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  5610. (string-match
  5611. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  5612. (let* ((t0 (+ (* 100
  5613. (+ (string-to-number (match-string 1 s))
  5614. (if (and (match-beginning 4)
  5615. (equal (downcase (match-string 4 s)) "pm"))
  5616. 12 0)))
  5617. (if (match-beginning 3)
  5618. (string-to-number (match-string 3 s))
  5619. 0)))
  5620. (t1 (concat " "
  5621. (if (< t0 100) "0" "") (if (< t0 10) "0" "")
  5622. (int-to-string t0))))
  5623. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  5624. (defun org-finalize-agenda-entries (list)
  5625. "Sort and concatenate the agenda items."
  5626. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  5627. (defsubst org-cmp-priority (a b)
  5628. "Compare the priorities of string A and B."
  5629. (let ((pa (or (get-text-property 1 'priority a) 0))
  5630. (pb (or (get-text-property 1 'priority b) 0)))
  5631. (cond ((> pa pb) +1)
  5632. ((< pa pb) -1)
  5633. (t nil))))
  5634. (defsubst org-cmp-category (a b)
  5635. "Compare the string values of categories of strings A and B."
  5636. (let ((ca (or (get-text-property 1 'category a) ""))
  5637. (cb (or (get-text-property 1 'category b) "")))
  5638. (cond ((string-lessp ca cb) -1)
  5639. ((string-lessp cb ca) +1)
  5640. (t nil))))
  5641. (defsubst org-cmp-time (a b)
  5642. "Compare the time-of-day values of strings A and B."
  5643. (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
  5644. (ta (or (get-text-property 1 'time-of-day a) def))
  5645. (tb (or (get-text-property 1 'time-of-day b) def)))
  5646. (cond ((< ta tb) -1)
  5647. ((< tb ta) +1)
  5648. (t nil))))
  5649. (defun org-entries-lessp (a b)
  5650. "Predicate for sorting agenda entries."
  5651. ;; The following variables will be used when the form is evaluated.
  5652. (let* ((time-up (org-cmp-time a b))
  5653. (time-down (if time-up (- time-up) nil))
  5654. (priority-up (org-cmp-priority a b))
  5655. (priority-down (if priority-up (- priority-up) nil))
  5656. (category-up (org-cmp-category a b))
  5657. (category-down (if category-up (- category-up) nil))
  5658. (category-keep (if category-up +1 nil))) ; FIXME +1 or -1?
  5659. (cdr (assoc
  5660. (eval (cons 'or org-agenda-sorting-strategy))
  5661. '((-1 . t) (1 . nil) (nil . nil))))))
  5662. (defun org-agenda-show-priority ()
  5663. "Show the priority of the current item.
  5664. This priority is composed of the main priority given with the [#A] cookies,
  5665. and by additional input from the age of a schedules or deadline entry."
  5666. (interactive)
  5667. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  5668. (message "Priority is %d" (if pri pri -1000))))
  5669. (defun org-agenda-show-tags ()
  5670. "Show the tags applicable to the current item."
  5671. (interactive)
  5672. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  5673. (if tags
  5674. (message "Tags are :%s:" (mapconcat 'identity tags ":"))
  5675. (message "No tags associated with this line"))))
  5676. (defun org-agenda-goto (&optional highlight)
  5677. "Go to the Org-mode file which contains the item at point."
  5678. (interactive)
  5679. (let* ((marker (or (get-text-property (point) 'org-marker)
  5680. (org-agenda-error)))
  5681. (buffer (marker-buffer marker))
  5682. (pos (marker-position marker)))
  5683. (switch-to-buffer-other-window buffer)
  5684. (widen)
  5685. (goto-char pos)
  5686. (when (eq major-mode 'org-mode)
  5687. (org-show-hidden-entry)
  5688. (save-excursion
  5689. (and (outline-next-heading)
  5690. (org-flag-heading nil)))) ; show the next heading
  5691. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  5692. (defun org-agenda-switch-to ()
  5693. "Go to the Org-mode file which contains the item at point."
  5694. (interactive)
  5695. (let* ((marker (or (get-text-property (point) 'org-marker)
  5696. (org-agenda-error)))
  5697. (buffer (marker-buffer marker))
  5698. (pos (marker-position marker)))
  5699. (switch-to-buffer buffer)
  5700. (delete-other-windows)
  5701. (widen)
  5702. (goto-char pos)
  5703. (when (eq major-mode 'org-mode)
  5704. (org-show-hidden-entry)
  5705. (save-excursion
  5706. (and (outline-next-heading)
  5707. (org-flag-heading nil)))))) ; show the next heading
  5708. (defun org-agenda-goto-mouse (ev)
  5709. "Go to the Org-mode file which contains the item at the mouse click."
  5710. (interactive "e")
  5711. (mouse-set-point ev)
  5712. (org-agenda-goto))
  5713. (defun org-agenda-show ()
  5714. "Display the Org-mode file which contains the item at point."
  5715. (interactive)
  5716. (let ((win (selected-window)))
  5717. (org-agenda-goto t)
  5718. (select-window win)))
  5719. (defun org-agenda-recenter (arg)
  5720. "Display the Org-mode file which contains the item at point and recenter."
  5721. (interactive "P")
  5722. (let ((win (selected-window)))
  5723. (org-agenda-goto t)
  5724. (recenter arg)
  5725. (select-window win)))
  5726. (defun org-agenda-show-mouse (ev)
  5727. "Display the Org-mode file which contains the item at the mouse click."
  5728. (interactive "e")
  5729. (mouse-set-point ev)
  5730. (org-agenda-show))
  5731. (defun org-agenda-check-no-diary ()
  5732. "Check if the entry is a diary link and abort if yes."
  5733. (if (get-text-property (point) 'org-agenda-diary-link)
  5734. (org-agenda-error)))
  5735. (defun org-agenda-error ()
  5736. (error "Command not allowed in this line"))
  5737. (defvar org-last-heading-marker (make-marker)
  5738. "Marker pointing to the headline that last changed its TODO state
  5739. by a remote command from the agenda.")
  5740. (defun org-agenda-todo (&optional arg)
  5741. "Cycle TODO state of line at point, also in Org-mode file.
  5742. This changes the line at point, all other lines in the agenda referring to
  5743. the same tree node, and the headline of the tree node in the Org-mode file."
  5744. (interactive "P")
  5745. (org-agenda-check-no-diary)
  5746. (let* ((col (current-column))
  5747. (marker (or (get-text-property (point) 'org-marker)
  5748. (org-agenda-error)))
  5749. (buffer (marker-buffer marker))
  5750. (pos (marker-position marker))
  5751. (hdmarker (get-text-property (point) 'org-hd-marker))
  5752. (buffer-read-only nil)
  5753. newhead)
  5754. (with-current-buffer buffer
  5755. (widen)
  5756. (goto-char pos)
  5757. (org-show-hidden-entry)
  5758. (save-excursion
  5759. (and (outline-next-heading)
  5760. (org-flag-heading nil))) ; show the next heading
  5761. (org-todo arg)
  5762. (forward-char 1)
  5763. (setq newhead (org-get-heading))
  5764. (save-excursion
  5765. (org-back-to-heading)
  5766. (move-marker org-last-heading-marker (point))))
  5767. (beginning-of-line 1)
  5768. (save-excursion
  5769. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  5770. (move-to-column col)))
  5771. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  5772. "Change all lines in the agenda buffer which match HDMARKER.
  5773. The new content of the line will be NEWHEAD (as modified by
  5774. `org-format-agenda-item'). HDMARKER is checked with
  5775. `equal' against all `org-hd-marker' text properties in the file.
  5776. If FIXFACE is non-nil, the face of each item is modified acording to
  5777. the new TODO state."
  5778. (let* (props m pl undone-face done-face finish new dotime cat tags)
  5779. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  5780. (save-excursion
  5781. (goto-char (point-max))
  5782. (beginning-of-line 1)
  5783. (while (not finish)
  5784. (setq finish (bobp))
  5785. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  5786. (equal m hdmarker))
  5787. (setq props (text-properties-at (point))
  5788. dotime (get-text-property (point) 'dotime)
  5789. cat (get-text-property (point) 'category)
  5790. tags (get-text-property (point) 'tags)
  5791. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  5792. pl (get-text-property (point) 'prefix-length)
  5793. undone-face (get-text-property (point) 'undone-face)
  5794. done-face (get-text-property (point) 'done-face))
  5795. (move-to-column pl)
  5796. (if (looking-at ".*")
  5797. (progn
  5798. (replace-match new t t)
  5799. (beginning-of-line 1)
  5800. (add-text-properties (point-at-bol) (point-at-eol) props)
  5801. (if fixface
  5802. (add-text-properties
  5803. (point-at-bol) (point-at-eol)
  5804. (list 'face
  5805. (if org-last-todo-state-is-todo
  5806. undone-face done-face))))
  5807. (beginning-of-line 1))
  5808. (error "Line update did not work")))
  5809. (beginning-of-line 0)))))
  5810. (defun org-agenda-priority-up ()
  5811. "Increase the priority of line at point, also in Org-mode file."
  5812. (interactive)
  5813. (org-agenda-priority 'up))
  5814. (defun org-agenda-priority-down ()
  5815. "Decrease the priority of line at point, also in Org-mode file."
  5816. (interactive)
  5817. (org-agenda-priority 'down))
  5818. (defun org-agenda-priority (&optional force-direction)
  5819. "Set the priority of line at point, also in Org-mode file.
  5820. This changes the line at point, all other lines in the agenda referring to
  5821. the same tree node, and the headline of the tree node in the Org-mode file."
  5822. (interactive)
  5823. (org-agenda-check-no-diary)
  5824. (let* ((marker (or (get-text-property (point) 'org-marker)
  5825. (org-agenda-error)))
  5826. (buffer (marker-buffer marker))
  5827. (pos (marker-position marker))
  5828. (hdmarker (get-text-property (point) 'org-hd-marker))
  5829. (buffer-read-only nil)
  5830. newhead)
  5831. (with-current-buffer buffer
  5832. (widen)
  5833. (goto-char pos)
  5834. (org-show-hidden-entry)
  5835. (save-excursion
  5836. (and (outline-next-heading)
  5837. (org-flag-heading nil))) ; show the next heading
  5838. (funcall 'org-priority force-direction)
  5839. (end-of-line 1)
  5840. (setq newhead (org-get-heading)))
  5841. (org-agenda-change-all-lines newhead hdmarker)
  5842. (beginning-of-line 1)))
  5843. (defun org-get-tags-at (&optional pos)
  5844. "Get a list of all headline targs applicable at POS.
  5845. POS defaults to point. If tags are inherited, the list contains
  5846. the targets in the same sequence as the headlines appear, i.e.
  5847. the tags of the current headline come last."
  5848. (interactive)
  5849. (let (tags)
  5850. (save-excursion
  5851. (goto-char (or pos (point)))
  5852. (save-match-data
  5853. (org-back-to-heading t)
  5854. (condition-case nil
  5855. (while t
  5856. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  5857. (setq tags (append (org-split-string (match-string 1) ":") tags)))
  5858. (or org-use-tag-inheritance (error ""))
  5859. (org-up-heading-all 1))
  5860. (error nil))))
  5861. (message "%s" tags)
  5862. tags))
  5863. (defun org-agenda-set-tags ()
  5864. "Set tags for the current headline."
  5865. (interactive)
  5866. (org-agenda-check-no-diary)
  5867. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  5868. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  5869. (org-agenda-error)))
  5870. (buffer (marker-buffer hdmarker))
  5871. (pos (marker-position hdmarker))
  5872. (buffer-read-only nil)
  5873. newhead)
  5874. (with-current-buffer buffer
  5875. (widen)
  5876. (goto-char pos)
  5877. (org-show-hidden-entry)
  5878. (save-excursion
  5879. (and (outline-next-heading)
  5880. (org-flag-heading nil))) ; show the next heading
  5881. (call-interactively 'org-set-tags)
  5882. (end-of-line 1)
  5883. (setq newhead (org-get-heading)))
  5884. (org-agenda-change-all-lines newhead hdmarker)
  5885. (beginning-of-line 1)))
  5886. (defun org-agenda-date-later (arg &optional what)
  5887. "Change the date of this item to one day later."
  5888. (interactive "p")
  5889. (org-agenda-check-type t 'agenda 'timeline)
  5890. (org-agenda-check-no-diary)
  5891. (let* ((marker (or (get-text-property (point) 'org-marker)
  5892. (org-agenda-error)))
  5893. (buffer (marker-buffer marker))
  5894. (pos (marker-position marker)))
  5895. (with-current-buffer buffer
  5896. (widen)
  5897. (goto-char pos)
  5898. (if (not (org-at-timestamp-p))
  5899. (error "Cannot find time stamp"))
  5900. (org-timestamp-change arg (or what 'day))
  5901. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  5902. (defun org-agenda-date-earlier (arg &optional what)
  5903. "Change the date of this item to one day earlier."
  5904. (interactive "p")
  5905. (org-agenda-date-later (- arg) what))
  5906. (defun org-agenda-date-prompt (arg)
  5907. "Change the date of this item. Date is prompted for, with default today.
  5908. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  5909. be used to request time specification in the time stamp."
  5910. (interactive "P")
  5911. (org-agenda-check-type t 'agenda 'timeline)
  5912. (org-agenda-check-no-diary)
  5913. (let* ((marker (or (get-text-property (point) 'org-marker)
  5914. (org-agenda-error)))
  5915. (buffer (marker-buffer marker))
  5916. (pos (marker-position marker)))
  5917. (with-current-buffer buffer
  5918. (widen)
  5919. (goto-char pos)
  5920. (if (not (org-at-timestamp-p))
  5921. (error "Cannot find time stamp"))
  5922. (org-time-stamp arg)
  5923. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  5924. (defun org-get-heading ()
  5925. "Return the heading of the current entry, without the stars."
  5926. (save-excursion
  5927. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  5928. ;;FIXME???????? (and (bolp) (end-of-line 1))
  5929. (if (and (re-search-backward "[\r\n]\\*" nil t)
  5930. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  5931. (match-string 1)
  5932. "")))
  5933. (defun org-agenda-diary-entry ()
  5934. "Make a diary entry, like the `i' command from the calendar.
  5935. All the standard commands work: block, weekly etc."
  5936. (interactive)
  5937. (org-agenda-check-type t 'agenda 'timeline)
  5938. (require 'diary-lib)
  5939. (let* ((char (progn
  5940. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  5941. (read-char-exclusive)))
  5942. (cmd (cdr (assoc char
  5943. '((?d . insert-diary-entry)
  5944. (?w . insert-weekly-diary-entry)
  5945. (?m . insert-monthly-diary-entry)
  5946. (?y . insert-yearly-diary-entry)
  5947. (?a . insert-anniversary-diary-entry)
  5948. (?b . insert-block-diary-entry)
  5949. (?c . insert-cyclic-diary-entry)))))
  5950. (oldf (symbol-function 'calendar-cursor-to-date))
  5951. (point (point))
  5952. (mark (or (mark t) (point))))
  5953. (unless cmd
  5954. (error "No command associated with <%c>" char))
  5955. (unless (and (get-text-property point 'day)
  5956. (or (not (equal ?b char))
  5957. (get-text-property mark 'day)))
  5958. (error "Don't know which date to use for diary entry"))
  5959. ;; We implement this by hacking the `calendar-cursor-to-date' function
  5960. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  5961. (let ((calendar-mark-ring
  5962. (list (calendar-gregorian-from-absolute
  5963. (or (get-text-property mark 'day)
  5964. (get-text-property point 'day))))))
  5965. (unwind-protect
  5966. (progn
  5967. (fset 'calendar-cursor-to-date
  5968. (lambda (&optional error)
  5969. (calendar-gregorian-from-absolute
  5970. (get-text-property point 'day))))
  5971. (call-interactively cmd))
  5972. (fset 'calendar-cursor-to-date oldf)))))
  5973. (defun org-agenda-execute-calendar-command (cmd)
  5974. "Execute a calendar command from the agenda, with the date associated to
  5975. the cursor position."
  5976. (org-agenda-check-type t 'agenda 'timeline)
  5977. (require 'diary-lib)
  5978. (unless (get-text-property (point) 'day)
  5979. (error "Don't know which date to use for calendar command"))
  5980. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  5981. (point (point))
  5982. (date (calendar-gregorian-from-absolute
  5983. (get-text-property point 'day)))
  5984. (displayed-day (extract-calendar-day date))
  5985. (displayed-month (extract-calendar-month date))
  5986. (displayed-year (extract-calendar-year date)))
  5987. (unwind-protect
  5988. (progn
  5989. (fset 'calendar-cursor-to-date
  5990. (lambda (&optional error)
  5991. (calendar-gregorian-from-absolute
  5992. (get-text-property point 'day))))
  5993. (call-interactively cmd))
  5994. (fset 'calendar-cursor-to-date oldf))))
  5995. (defun org-agenda-phases-of-moon ()
  5996. "Display the phases of the moon for the 3 months around the cursor date."
  5997. (interactive)
  5998. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  5999. (defun org-agenda-holidays ()
  6000. "Display the holidays for the 3 months around the cursor date."
  6001. (interactive)
  6002. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  6003. (defun org-agenda-sunrise-sunset (arg)
  6004. "Display sunrise and sunset for the cursor date.
  6005. Latitude and longitude can be specified with the variables
  6006. `calendar-latitude' and `calendar-longitude'. When called with prefix
  6007. argument, latitude and longitude will be prompted for."
  6008. (interactive "P")
  6009. (let ((calendar-longitude (if arg nil calendar-longitude))
  6010. (calendar-latitude (if arg nil calendar-latitude))
  6011. (calendar-location-name
  6012. (if arg "the given coordinates" calendar-location-name)))
  6013. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  6014. (defun org-agenda-goto-calendar ()
  6015. "Open the Emacs calendar with the date at the cursor."
  6016. (interactive)
  6017. (org-agenda-check-type t 'agenda 'timeline)
  6018. (let* ((day (or (get-text-property (point) 'day)
  6019. (error "Don't know which date to open in calendar")))
  6020. (date (calendar-gregorian-from-absolute day))
  6021. (calendar-move-hook nil)
  6022. (view-diary-entries-initially nil))
  6023. (calendar)
  6024. (calendar-goto-date date)))
  6025. (defun org-calendar-goto-agenda ()
  6026. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  6027. This is a command that has to be installed in `calendar-mode-map'."
  6028. (interactive)
  6029. (org-agenda-list nil (calendar-absolute-from-gregorian
  6030. (calendar-cursor-to-date))
  6031. nil t))
  6032. (defun org-agenda-convert-date ()
  6033. (interactive)
  6034. (org-agenda-check-type t 'agenda 'timeline)
  6035. (let ((day (get-text-property (point) 'day))
  6036. date s)
  6037. (unless day
  6038. (error "Don't know which date to convert"))
  6039. (setq date (calendar-gregorian-from-absolute day))
  6040. (setq s (concat
  6041. "Gregorian: " (calendar-date-string date) "\n"
  6042. "ISO: " (calendar-iso-date-string date) "\n"
  6043. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  6044. "Julian: " (calendar-julian-date-string date) "\n"
  6045. "Astron. JD: " (calendar-astro-date-string date)
  6046. " (Julian date number at noon UTC)\n"
  6047. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  6048. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  6049. "French: " (calendar-french-date-string date) "\n"
  6050. "Mayan: " (calendar-mayan-date-string date) "\n"
  6051. "Coptic: " (calendar-coptic-date-string date) "\n"
  6052. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  6053. "Persian: " (calendar-persian-date-string date) "\n"
  6054. "Chinese: " (calendar-chinese-date-string date) "\n"))
  6055. (with-output-to-temp-buffer "*Dates*"
  6056. (princ s))
  6057. (if (fboundp 'fit-window-to-buffer)
  6058. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  6059. ;;; Tags
  6060. (defun org-scan-tags (action matcher &optional todo-only)
  6061. "Scan headline tags with inheritance and produce output ACTION.
  6062. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  6063. evaluated, testing if a given set of tags qualifies a headline for
  6064. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  6065. are included in the output."
  6066. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  6067. (mapconcat 'regexp-quote
  6068. (nreverse (cdr (reverse org-todo-keywords)))
  6069. "\\|")
  6070. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  6071. (props (list 'face nil
  6072. 'done-face 'org-done
  6073. 'undone-face nil
  6074. 'mouse-face 'highlight
  6075. 'keymap org-agenda-keymap
  6076. 'help-echo
  6077. (format "mouse-2 or RET jump to org file %s"
  6078. (abbreviate-file-name buffer-file-name))))
  6079. lspos
  6080. tags tags-list tags-alist (llast 0) rtn level category i txt
  6081. todo marker)
  6082. (save-excursion
  6083. (goto-char (point-min))
  6084. (when (eq action 'sparse-tree) (hide-sublevels 1))
  6085. (while (re-search-forward re nil t)
  6086. (setq todo (if (match-end 1) (match-string 2))
  6087. tags (if (match-end 4) (match-string 4)))
  6088. (goto-char (setq lspos (1+ (match-beginning 0))))
  6089. (setq level (funcall outline-level)
  6090. category (org-get-category))
  6091. (setq i llast llast level)
  6092. ;; remove tag lists from same and sublevels
  6093. (while (>= i level)
  6094. (when (setq entry (assoc i tags-alist))
  6095. (setq tags-alist (delete entry tags-alist)))
  6096. (setq i (1- i)))
  6097. ;; add the nex tags
  6098. (when tags
  6099. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  6100. tags-alist
  6101. (cons (cons level tags) tags-alist)))
  6102. ;; compile tags for current headline
  6103. (setq tags-list
  6104. (if org-use-tag-inheritance
  6105. (apply 'append (mapcar 'cdr tags-alist))
  6106. tags))
  6107. (when (and (or (not todo-only) todo)
  6108. (eval matcher))
  6109. ;; list this headline
  6110. (if (eq action 'sparse-tree)
  6111. (progn
  6112. (org-show-hierarchy-above))
  6113. (setq txt (org-format-agenda-item
  6114. ""
  6115. (concat
  6116. (if org-tags-match-list-sublevels
  6117. (make-string (1- level) ?.) "")
  6118. (org-get-heading))
  6119. category tags-list))
  6120. (goto-char lspos)
  6121. (setq marker (org-agenda-new-marker))
  6122. (add-text-properties
  6123. 0 (length txt)
  6124. (append (list 'org-marker marker 'org-hd-marker marker
  6125. 'category category)
  6126. props)
  6127. txt)
  6128. (push txt rtn))
  6129. ;; if we are to skip sublevels, jump to end of subtree
  6130. (point)
  6131. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  6132. (nreverse rtn)))
  6133. (defun org-tags-sparse-tree (&optional arg match)
  6134. "Create a sparse tree according to tags search string MATCH.
  6135. MATCH can contain positive and negative selection of tags, like
  6136. \"+WORK+URGENT-WITHBOSS\"."
  6137. (interactive "P")
  6138. (let ((org-show-following-heading nil)
  6139. (org-show-hierarchy-above nil))
  6140. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  6141. (defun org-make-tags-matcher (match)
  6142. "Create the TAGS matcher form for the tags-selecting string MATCH."
  6143. (unless match
  6144. ;; Get a new match request, with completion
  6145. (setq org-last-tags-completion-table
  6146. (or (org-get-buffer-tags)
  6147. org-last-tags-completion-table))
  6148. (setq match (completing-read
  6149. "Tags: " 'org-tags-completion-function nil nil nil
  6150. 'org-tags-history)))
  6151. ;; parse the string and create a lisp form
  6152. (let ((match0 match) minus tag mm matcher orterms term orlist)
  6153. (setq orterms (org-split-string match "|"))
  6154. (while (setq term (pop orterms))
  6155. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  6156. (setq minus (and (match-end 1)
  6157. (equal (match-string 1 term) "-"))
  6158. tag (match-string 2 term)
  6159. term (substring term (match-end 0))
  6160. mm (list 'member (downcase tag) 'tags-list)
  6161. mm (if minus (list 'not mm) mm))
  6162. (push mm matcher))
  6163. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  6164. orlist)
  6165. (setq matcher nil))
  6166. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  6167. ;; Return the string and lisp forms of the matcher
  6168. (cons match0 matcher)))
  6169. ;;;###autoload
  6170. (defun org-tags-view (&optional todo-only match keep-modes)
  6171. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  6172. The prefix arg TODO-ONLY limits the search to TODO entries."
  6173. (interactive "P")
  6174. (org-agenda-maybe-reset-markers 'force)
  6175. (org-compile-prefix-format org-agenda-prefix-format)
  6176. (let* ((org-agenda-keep-modes keep-modes)
  6177. (org-tags-match-list-sublevels
  6178. (if todo-only t org-tags-match-list-sublevels))
  6179. (win (selected-window))
  6180. (completion-ignore-case t)
  6181. rtn rtnall files file pos matcher
  6182. buffer)
  6183. (setq matcher (org-make-tags-matcher match)
  6184. match (car matcher) matcher (cdr matcher))
  6185. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6186. (progn
  6187. (delete-other-windows)
  6188. (switch-to-buffer-other-window
  6189. (get-buffer-create org-agenda-buffer-name))))
  6190. (setq buffer-read-only nil)
  6191. (erase-buffer)
  6192. (org-agenda-mode) (setq buffer-read-only nil)
  6193. (set (make-local-variable 'org-agenda-type) 'tags)
  6194. (set (make-local-variable 'org-agenda-redo-command)
  6195. (list 'org-tags-view (list 'quote todo-only)
  6196. (list 'if 'current-prefix-arg nil match) t))
  6197. (setq files (org-agenda-files)
  6198. rtnall nil)
  6199. (while (setq file (pop files))
  6200. (catch 'nextfile
  6201. (org-check-agenda-file file)
  6202. (setq buffer (if (file-exists-p file)
  6203. (org-get-agenda-file-buffer file)
  6204. (error "No such file %s" file)))
  6205. (if (not buffer)
  6206. ;; If file does not exist, merror message to agenda
  6207. (setq rtn (list
  6208. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6209. rtnall (append rtnall rtn))
  6210. (with-current-buffer buffer
  6211. (unless (eq major-mode 'org-mode)
  6212. (error "Agenda file %s is not in `org-mode'" file))
  6213. (save-excursion
  6214. (save-restriction
  6215. (if org-respect-restriction
  6216. (if (org-region-active-p)
  6217. ;; Respect a region to restrict search
  6218. (narrow-to-region (region-beginning) (region-end)))
  6219. ;; If we work for the calendar or many files,
  6220. ;; get rid of any restriction
  6221. (widen))
  6222. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  6223. (setq rtnall (append rtnall rtn))))))))
  6224. (insert "Headlines with TAGS match: ")
  6225. (add-text-properties (point-min) (1- (point))
  6226. (list 'face 'org-link))
  6227. (setq pos (point))
  6228. (insert match "\n")
  6229. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6230. (setq pos (point))
  6231. (insert "Press `C-u r' to search again with new search string\n")
  6232. (add-text-properties pos (1- (point)) (list 'face 'org-link))
  6233. (when rtnall
  6234. (insert (mapconcat 'identity rtnall "\n")))
  6235. (goto-char (point-min))
  6236. (setq buffer-read-only t)
  6237. (org-fit-agenda-window)
  6238. (if (not org-select-agenda-window) (select-window win))))
  6239. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  6240. (defun org-set-tags (&optional arg just-align)
  6241. "Set the tags for the current headline.
  6242. With prefix ARG, realign all tags in headings in the current buffer."
  6243. (interactive)
  6244. (let* (;(inherit (org-get-inherited-tags))
  6245. (re (concat "^" outline-regexp))
  6246. (col (current-column))
  6247. (current (org-get-tags))
  6248. tags hd empty invis)
  6249. (if arg
  6250. (save-excursion
  6251. (goto-char (point-min))
  6252. (while (re-search-forward re nil t)
  6253. (org-set-tags nil t))
  6254. (message "All tags realigned to column %d" org-tags-column))
  6255. (if just-align
  6256. (setq tags current)
  6257. (setq org-last-tags-completion-table
  6258. (or (org-get-buffer-tags)
  6259. org-last-tags-completion-table))
  6260. (setq tags
  6261. (let ((org-add-colon-after-tag-completion t))
  6262. (completing-read "Tags: " 'org-tags-completion-function
  6263. nil nil current 'org-tags-history)))
  6264. (while (string-match "[-+&]+" tags)
  6265. (setq tags (replace-match ":" t t tags))))
  6266. ;; FIXME: still optimize this by not checking when JUST-ALIGN?
  6267. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  6268. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  6269. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  6270. (if (equal current "")
  6271. (progn
  6272. (end-of-line 1)
  6273. (or empty (insert-before-markers " ")))
  6274. (beginning-of-line 1)
  6275. (setq invis (org-invisible-p))
  6276. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  6277. (setq hd (match-string 1))
  6278. (delete-region (match-beginning 0) (match-end 0))
  6279. (insert-before-markers (org-trim hd) (if empty "" " ")))
  6280. ;; FIXME: What happens when adding a new tag??? Seems OK!!!
  6281. (unless (equal tags "")
  6282. (move-to-column (max (current-column)
  6283. (if (> org-tags-column 0)
  6284. org-tags-column
  6285. (- (- org-tags-column) (length tags))))
  6286. t)
  6287. (insert-before-markers tags)
  6288. (if (and (not invis) (org-invisible-p))
  6289. (outline-flag-region (point-at-bol) (point) nil)))
  6290. (move-to-column col))))
  6291. (defun org-tags-completion-function (string predicate &optional flag)
  6292. (let (s1 s2 rtn (ctable org-last-tags-completion-table))
  6293. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  6294. (setq s1 (match-string 1 string)
  6295. s2 (match-string 2 string))
  6296. (setq s1 "" s2 string))
  6297. (cond
  6298. ((eq flag nil)
  6299. ;; try completion
  6300. (setq rtn (try-completion s2 ctable))
  6301. (if (stringp rtn)
  6302. (concat s1 s2 (substring rtn (length s2))
  6303. (if (and org-add-colon-after-tag-completion
  6304. (assoc rtn ctable))
  6305. ":" "")))
  6306. )
  6307. ((eq flag t)
  6308. ;; all-completions
  6309. (all-completions s2 ctable)
  6310. )
  6311. ((eq flag 'lambda)
  6312. ;; exact match?
  6313. (assoc s2 ctable)))
  6314. ))
  6315. (defun org-get-tags ()
  6316. "Get the TAGS string in the current headline."
  6317. (unless (org-on-heading-p)
  6318. (error "Not on a heading"))
  6319. (save-excursion
  6320. (beginning-of-line 1)
  6321. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  6322. (match-string 1)
  6323. "")))
  6324. (defun org-get-buffer-tags ()
  6325. "Get a table of all tags used in the buffer, for completion."
  6326. (let (tags)
  6327. (save-excursion
  6328. (goto-char (point-min))
  6329. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  6330. (mapc (lambda (x) (add-to-list 'tags x))
  6331. (org-split-string (match-string 1) ":"))))
  6332. (mapcar 'list tags)))
  6333. ;;; Link Stuff
  6334. (defun org-find-file-at-mouse (ev)
  6335. "Open file link or URL at mouse."
  6336. (interactive "e")
  6337. (mouse-set-point ev)
  6338. (org-open-at-point 'in-emacs))
  6339. (defun org-open-at-mouse (ev)
  6340. "Open file link or URL at mouse."
  6341. (interactive "e")
  6342. (mouse-set-point ev)
  6343. (org-open-at-point))
  6344. (defun org-open-at-point (&optional in-emacs)
  6345. "Open link at or after point.
  6346. If there is no link at point, this function will search forward up to
  6347. the end of the current subtree.
  6348. Normally, files will be opened by an appropriate application. If the
  6349. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6350. (interactive "P")
  6351. (org-remove-occur-highlights nil nil t)
  6352. (if (org-at-timestamp-p)
  6353. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6354. (substring (match-string 1) 0 10)))
  6355. 1)
  6356. (let (type path link line search (pos (point)))
  6357. (catch 'match
  6358. (save-excursion
  6359. (skip-chars-forward "^]\n\r")
  6360. (when (and (re-search-backward "\\[\\[" nil t)
  6361. (looking-at org-bracket-link-regexp)
  6362. (<= (match-beginning 0) pos)
  6363. (>= (match-end 0) pos))
  6364. (setq link (match-string 1))
  6365. (while (string-match " *\n *" link)
  6366. (setq link (replace-match " " t t link)))
  6367. (if (string-match org-link-regexp link)
  6368. (setq type (match-string 1 link)
  6369. path (match-string 2 link))
  6370. (setq type "thisfile"
  6371. path link))
  6372. (throw 'match t)))
  6373. (when (get-text-property (point) 'org-linked-text)
  6374. (setq type "thisfile"
  6375. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6376. (1+ (point)) (point))
  6377. path (buffer-substring
  6378. (previous-single-property-change pos 'org-linked-text)
  6379. (next-single-property-change pos 'org-linked-text)))
  6380. (throw 'match t))
  6381. (save-excursion
  6382. (skip-chars-backward
  6383. (concat (if org-allow-space-in-links "^" "^ ")
  6384. org-non-link-chars))
  6385. (when (or (looking-at org-link-regexp)
  6386. (and (re-search-forward org-link-regexp (point-at-eol) t)
  6387. (<= (match-beginning 0) pos)
  6388. (>= (match-end 0) pos)))
  6389. (setq type (match-string 1)
  6390. path (match-string 2))
  6391. (throw 'match t)))
  6392. (save-excursion
  6393. (skip-chars-backward "^ \t\n\r")
  6394. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  6395. (setq type "tags"
  6396. path (match-string 1))
  6397. (while (string-match ":" path)
  6398. (setq path (replace-match "+" t t path)))
  6399. (throw 'match t)))
  6400. (save-excursion
  6401. (skip-chars-backward "a-zA-Z_")
  6402. (when (and org-activate-camels
  6403. (looking-at org-camel-regexp))
  6404. (setq type "camel" path (match-string 0))
  6405. (if (equal (char-before) ?*)
  6406. (setq path (concat "*" path))))
  6407. (throw 'match t))
  6408. (save-excursion
  6409. (when (re-search-forward
  6410. org-link-regexp
  6411. (save-excursion
  6412. (condition-case nil
  6413. (progn (outline-end-of-subtree) (max pos (point)))
  6414. (error (end-of-line 1) (point))))
  6415. t)
  6416. (setq type (match-string 1)
  6417. path (match-string 2)))))
  6418. (unless path
  6419. (error "No link found"))
  6420. ;; Remove any trailing spaces in path
  6421. (if (string-match " +\\'" path)
  6422. (setq path (replace-match "" t t path)))
  6423. (cond
  6424. ((member type '("http" "https" "ftp" "mailto" "news"))
  6425. (browse-url (concat type ":" path)))
  6426. ((string= type "tags")
  6427. (org-tags-view in-emacs path))
  6428. ((or (string= type "camel")
  6429. (string= type "thisfile"))
  6430. (org-mark-ring-push)
  6431. (org-link-search
  6432. path
  6433. (cond ((equal in-emacs '(4)) 'occur)
  6434. ((equal in-emacs '(16)) 'org-occur)
  6435. (t nil))))
  6436. ((string= type "file")
  6437. (if (string-match "::?\\([0-9]+\\)\\'" path) ;; second : optional
  6438. (setq line (string-to-number (match-string 1 path))
  6439. path (substring path 0 (match-beginning 0)))
  6440. (if (string-match "::\\(.+\\)\\'" path)
  6441. (setq search (match-string 1 path)
  6442. path (substring path 0 (match-beginning 0)))))
  6443. (org-open-file path in-emacs line search))
  6444. ((string= type "news")
  6445. (org-follow-gnus-link path))
  6446. ((string= type "bbdb")
  6447. (org-follow-bbdb-link path))
  6448. ((string= type "gnus")
  6449. (let (group article)
  6450. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6451. (error "Error in Gnus link"))
  6452. (setq group (match-string 1 path)
  6453. article (match-string 3 path))
  6454. (org-follow-gnus-link group article)))
  6455. ((string= type "vm")
  6456. (let (folder article)
  6457. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6458. (error "Error in VM link"))
  6459. (setq folder (match-string 1 path)
  6460. article (match-string 3 path))
  6461. ;; in-emacs is the prefix arg, will be interpreted as read-only
  6462. (org-follow-vm-link folder article in-emacs)))
  6463. ((string= type "wl")
  6464. (let (folder article)
  6465. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6466. (error "Error in Wanderlust link"))
  6467. (setq folder (match-string 1 path)
  6468. article (match-string 3 path))
  6469. (org-follow-wl-link folder article)))
  6470. ((string= type "mhe")
  6471. (let (folder article)
  6472. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6473. (error "Error in MHE link"))
  6474. (setq folder (match-string 1 path)
  6475. article (match-string 3 path))
  6476. (org-follow-mhe-link folder article)))
  6477. ((string= type "rmail")
  6478. (let (folder article)
  6479. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6480. (error "Error in RMAIL link"))
  6481. (setq folder (match-string 1 path)
  6482. article (match-string 3 path))
  6483. (org-follow-rmail-link folder article)))
  6484. ((string= type "shell")
  6485. (let ((cmd path))
  6486. (while (string-match "@{" cmd)
  6487. (setq cmd (replace-match "<" t t cmd)))
  6488. (while (string-match "@}" cmd)
  6489. (setq cmd (replace-match ">" t t cmd)))
  6490. (if (or (not org-confirm-shell-links)
  6491. (yes-or-no-p (format "Execute \"%s\" in the shell? " cmd)))
  6492. (shell-command cmd)
  6493. (error "Abort"))))
  6494. (t
  6495. (browse-url-at-point))))))
  6496. (defun org-link-search (s &optional type)
  6497. "Search for a link search option.
  6498. When S is a CamelCaseWord, search for a target, or for a sentence containing
  6499. the words. If S is surrounded by forward slashes, it is interpreted as a
  6500. regular expression. In org-mode files, this will create an `org-occur'
  6501. sparse tree. In ordinary files, `occur' will be used to list matches.
  6502. If the current buffer is in `dired-mode', grep will be used to search
  6503. in all files."
  6504. (let ((case-fold-search t)
  6505. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6506. (pos (point))
  6507. (pre "") (post "")
  6508. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  6509. (cond ((save-excursion
  6510. (goto-char (point-min))
  6511. (and
  6512. (re-search-forward
  6513. (concat "<<" (regexp-quote s0) ">>") nil t)
  6514. (setq pos (match-beginning 0))))
  6515. ;; There is an exact target for this
  6516. (goto-char pos))
  6517. ((string-match "^/\\(.*\\)/$" s)
  6518. ;; A regular expression
  6519. (cond
  6520. ((eq major-mode 'org-mode)
  6521. (org-occur (match-string 1 s)))
  6522. ;;((eq major-mode 'dired-mode)
  6523. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6524. (t (org-do-occur (match-string 1 s)))))
  6525. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  6526. t)
  6527. ;; A camel or a normal search string
  6528. (when (equal (string-to-char s) ?*)
  6529. ;; Anchor on headlines, post may include tags.
  6530. (setq pre "^\\*+[ \t]*\\(\\sw+\\)?[ \t]*"
  6531. post "[ \t]*\\([ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  6532. s (substring s 1)))
  6533. (remove-text-properties
  6534. 0 (length s)
  6535. '(face nil mouse-face nil keymap nil fontified nil) s)
  6536. ;; Make a series of regular expressions to find a match
  6537. (setq words
  6538. (if camel
  6539. (org-camel-to-words s)
  6540. (org-split-string s "[ \n\r\t]+"))
  6541. re0 (concat "<<" (regexp-quote s0) ">>")
  6542. re2 (concat "\\<" (mapconcat 'downcase words "[ \t]+") "\\>")
  6543. re2a (concat "\\<" (mapconcat 'downcase words "[ \t\r\n]+") "\\>")
  6544. re4 (concat "\\<" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\>")
  6545. re1 (concat pre re2 post)
  6546. re3 (concat pre re4 post)
  6547. re5 (concat pre ".*" re4)
  6548. re2 (concat pre re2)
  6549. re2a (concat pre re2a)
  6550. re4 (concat pre re4)
  6551. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6552. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6553. re5 "\\)"
  6554. ))
  6555. (cond
  6556. ((eq type 'org-occur) (org-occur reall))
  6557. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6558. (t (goto-char (point-min))
  6559. (if (or (org-search-not-link re0 nil t)
  6560. (org-search-not-link re1 nil t)
  6561. (org-search-not-link re2 nil t)
  6562. (org-search-not-link re2a nil t) ;; FIXME: Right place???
  6563. (org-search-not-link re3 nil t)
  6564. (org-search-not-link re4 nil t)
  6565. (org-search-not-link re5 nil t)
  6566. )
  6567. (goto-char (match-beginning 0))
  6568. (goto-char pos)
  6569. (error "No match")))))
  6570. (t
  6571. ;; Normal string-search
  6572. (goto-char (point-min))
  6573. (if (search-forward s nil t)
  6574. (goto-char (match-beginning 0))
  6575. (error "No match"))))
  6576. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  6577. (defun org-search-not-link (&rest args)
  6578. "Execute `re-search-forward', but only accept matches that are not a link."
  6579. (catch 'exit
  6580. (let ((pos (point)) p1)
  6581. (while (apply 're-search-forward args)
  6582. (setq p1 (point))
  6583. (if (not (save-match-data
  6584. (and (re-search-backward "\\[\\[" nil t)
  6585. (looking-at org-bracket-link-regexp)
  6586. (<= (match-beginning 0) p1)
  6587. (>= (match-end 0) p1))))
  6588. (progn (goto-char (match-end 0))
  6589. (throw 'exit (point)))
  6590. (goto-char (match-end 0)))))))
  6591. (defun org-do-occur (regexp &optional cleanup)
  6592. "Call the Emacs command `occur'.
  6593. If CLEANUP is non-nil, remove the printout of the regular expression
  6594. in the *Occur* buffer. This is useful if the regex is long and not useful
  6595. to read."
  6596. (occur regexp)
  6597. (when cleanup
  6598. (let ((cwin (selected-window)) win beg end)
  6599. (when (setq win (get-buffer-window "*Occur*"))
  6600. (select-window win))
  6601. (goto-char (point-min))
  6602. (when (re-search-forward "match[a-z]+" nil t)
  6603. (setq beg (match-end 0))
  6604. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6605. (setq end (1- (match-beginning 0)))))
  6606. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  6607. (goto-char (point-min))
  6608. (select-window cwin))))
  6609. (defvar org-mark-ring nil
  6610. "Mark ring for positions before jumps in Org-mode.")
  6611. (defvar org-mark-ring-last-goto nil
  6612. "Last position in the mark ring used to go back.")
  6613. ;; Fill and close the ring
  6614. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6615. (loop for i from 1 to org-mark-ring-length do
  6616. (push (make-marker) org-mark-ring))
  6617. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6618. org-mark-ring)
  6619. (defun org-mark-ring-push (&optional pos buffer)
  6620. "Put the current position or POS into the mark ring and rotate it."
  6621. (interactive)
  6622. (setq pos (or pos (point)))
  6623. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6624. (move-marker (car org-mark-ring)
  6625. (or pos (point))
  6626. (or buffer (current-buffer)))
  6627. (message
  6628. (substitute-command-keys
  6629. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6630. (defun org-mark-ring-goto (&optional n)
  6631. "Jump to the previous position in the mark ring.
  6632. With prefix arg N, jump back that many stored positions. When
  6633. called several times in succession, walk through the entire ring.
  6634. Org-mode commands jumping to a different position in the current file,
  6635. or to another Org-mode file, automatically push the old position
  6636. onto the ring."
  6637. (interactive "p")
  6638. (let (p m)
  6639. (if (eq last-command this-command)
  6640. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6641. (setq p org-mark-ring))
  6642. (setq org-mark-ring-last-goto p)
  6643. (setq m (car p))
  6644. (switch-to-buffer (marker-buffer m))
  6645. (goto-char m)
  6646. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  6647. (defun org-camel-to-words (s)
  6648. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  6649. (let ((case-fold-search nil)
  6650. words)
  6651. (while (string-match "[a-z][A-Z]" s)
  6652. (push (substring s 0 (1+ (match-beginning 0))) words)
  6653. (setq s (substring s (1+ (match-beginning 0)))))
  6654. (nreverse (cons s words))))
  6655. (defun org-remove-angle-brackets (s)
  6656. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6657. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6658. s)
  6659. (defun org-add-angle-brackets (s)
  6660. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6661. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6662. s)
  6663. (defun org-follow-bbdb-link (name)
  6664. "Follow a BBDB link to NAME."
  6665. (require 'bbdb)
  6666. (let ((inhibit-redisplay t)
  6667. (bbdb-electric-p nil))
  6668. (catch 'exit
  6669. ;; Exact match on name
  6670. (bbdb-name (concat "\\`" name "\\'") nil)
  6671. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6672. ;; Exact match on name
  6673. (bbdb-company (concat "\\`" name "\\'") nil)
  6674. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6675. ;; Partial match on name
  6676. (bbdb-name name nil)
  6677. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6678. ;; Partial match on company
  6679. (bbdb-company name nil)
  6680. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6681. ;; General match including network address and notes
  6682. (bbdb name nil)
  6683. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  6684. (delete-window (get-buffer-window "*BBDB*"))
  6685. (error "No matching BBDB record")))))
  6686. (defun org-follow-gnus-link (&optional group article)
  6687. "Follow a Gnus link to GROUP and ARTICLE."
  6688. (require 'gnus)
  6689. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  6690. (if group (gnus-fetch-group group))
  6691. (if article
  6692. (or (gnus-summary-goto-article article nil 'force)
  6693. (if (fboundp 'gnus-summary-insert-cached-articles)
  6694. (progn
  6695. (gnus-summary-insert-cached-articles)
  6696. (gnus-summary-goto-article article nil 'force))
  6697. (message "Message could not be found.")))))
  6698. (defun org-follow-vm-link (&optional folder article readonly)
  6699. "Follow a VM link to FOLDER and ARTICLE."
  6700. (require 'vm)
  6701. (setq article (org-add-angle-brackets article))
  6702. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  6703. ;; ange-ftp or efs or tramp access
  6704. (let ((user (or (match-string 1 folder) (user-login-name)))
  6705. (host (match-string 2 folder))
  6706. (file (match-string 3 folder)))
  6707. (cond
  6708. ((featurep 'tramp)
  6709. ;; use tramp to access the file
  6710. (if (featurep 'xemacs)
  6711. (setq folder (format "[%s@%s]%s" user host file))
  6712. (setq folder (format "/%s@%s:%s" user host file))))
  6713. (t
  6714. ;; use ange-ftp or efs
  6715. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  6716. (setq folder (format "/%s@%s:%s" user host file))))))
  6717. (when folder
  6718. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  6719. (sit-for 0.1)
  6720. (when article
  6721. (vm-select-folder-buffer)
  6722. (widen)
  6723. (let ((case-fold-search t))
  6724. (goto-char (point-min))
  6725. (if (not (re-search-forward
  6726. (concat "^" "message-id: *" (regexp-quote article))))
  6727. (error "Could not find the specified message in this folder"))
  6728. (vm-isearch-update)
  6729. (vm-isearch-narrow)
  6730. (vm-beginning-of-message)
  6731. (vm-summarize)))))
  6732. (defun org-follow-wl-link (folder article)
  6733. "Follow a Wanderlust link to FOLDER and ARTICLE."
  6734. (setq article (org-add-angle-brackets article))
  6735. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  6736. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  6737. (wl-summary-redisplay))
  6738. (defun org-follow-rmail-link (folder article)
  6739. "Follow an RMAIL link to FOLDER and ARTICLE."
  6740. (setq article (org-add-angle-brackets article))
  6741. (let (message-number)
  6742. (save-excursion
  6743. (save-window-excursion
  6744. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  6745. (setq message-number
  6746. (save-restriction
  6747. (widen)
  6748. (goto-char (point-max))
  6749. (if (re-search-backward
  6750. (concat "^Message-ID:\\s-+" (regexp-quote
  6751. (or article "")))
  6752. nil t)
  6753. (rmail-what-message))))))
  6754. (if message-number
  6755. (progn
  6756. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  6757. (rmail-show-message message-number)
  6758. message-number)
  6759. (error "Message not found"))))
  6760. ;; mh-e integration based on planner-mode
  6761. (defun org-mhe-get-message-real-folder ()
  6762. "Return the name of the current message real folder, so if you use
  6763. sequences, it will now work."
  6764. (save-excursion
  6765. (let* ((folder
  6766. (if (equal major-mode 'mh-folder-mode)
  6767. mh-current-folder
  6768. ;; Refer to the show buffer
  6769. mh-show-folder-buffer))
  6770. (end-index
  6771. (if (boundp 'mh-index-folder)
  6772. (min (length mh-index-folder) (length folder))))
  6773. )
  6774. ;; a simple test on mh-index-data does not work, because
  6775. ;; mh-index-data is always nil in a show buffer.
  6776. (if (and (boundp 'mh-index-folder)
  6777. (string= mh-index-folder (substring folder 0 end-index)))
  6778. (if (equal major-mode 'mh-show-mode)
  6779. (save-window-excursion
  6780. (when (buffer-live-p (get-buffer folder))
  6781. (progn
  6782. (pop-to-buffer folder)
  6783. (org-mhe-get-message-folder-from-index)
  6784. )
  6785. ))
  6786. (org-mhe-get-message-folder-from-index)
  6787. )
  6788. folder
  6789. )
  6790. )))
  6791. (defun org-mhe-get-message-folder-from-index ()
  6792. "Returns the name of the message folder in a index folder buffer."
  6793. (save-excursion
  6794. (mh-index-previous-folder)
  6795. (re-search-forward "^\\(+.*\\)$" nil t)
  6796. (message (match-string 1))))
  6797. (defun org-mhe-get-message-folder ()
  6798. "Return the name of the current message folder. Be careful if you
  6799. use sequences."
  6800. (save-excursion
  6801. (if (equal major-mode 'mh-folder-mode)
  6802. mh-current-folder
  6803. ;; Refer to the show buffer
  6804. mh-show-folder-buffer)))
  6805. (defun org-mhe-get-message-num ()
  6806. "Return the number of the current message. Be careful if you
  6807. use sequences."
  6808. (save-excursion
  6809. (if (equal major-mode 'mh-folder-mode)
  6810. (mh-get-msg-num nil)
  6811. ;; Refer to the show buffer
  6812. (mh-show-buffer-message-number))))
  6813. (defun org-mhe-get-header (header)
  6814. "Return a header of the message in folder mode. This will create a
  6815. show buffer for the corresponding message. If you have a more clever
  6816. idea..."
  6817. (let* ((folder (org-mhe-get-message-folder))
  6818. (num (org-mhe-get-message-num))
  6819. (buffer (get-buffer-create (concat "show-" folder)))
  6820. (header-field))
  6821. (with-current-buffer buffer
  6822. (mh-display-msg num folder)
  6823. (if (equal major-mode 'mh-folder-mode)
  6824. (mh-header-display)
  6825. (mh-show-header-display))
  6826. (set-buffer buffer)
  6827. (setq header-field (mh-get-header-field header))
  6828. (if (equal major-mode 'mh-folder-mode)
  6829. (mh-show)
  6830. (mh-show-show))
  6831. header-field)))
  6832. (defun org-follow-mhe-link (folder article)
  6833. "Follow an MHE link to FOLDER and ARTICLE.
  6834. If ARTICLE is nil FOLDER is shown. If the configuration variable
  6835. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  6836. ARTICLE is searched in all folders. Indexed searches (swish++,
  6837. namazu, and others supported by MH-E) will always search in all
  6838. folders."
  6839. (require 'mh-e)
  6840. (require 'mh-search)
  6841. (require 'mh-utils)
  6842. (mh-find-path)
  6843. (if (not article)
  6844. (mh-visit-folder (mh-normalize-folder-name folder))
  6845. (setq article (org-add-angle-brackets article))
  6846. (mh-search-choose)
  6847. (if (equal mh-searcher 'pick)
  6848. (progn
  6849. (mh-search folder (list "--message-id" article))
  6850. (when (and org-mhe-search-all-folders
  6851. (not (org-mhe-get-message-real-folder)))
  6852. (kill-this-buffer)
  6853. (mh-search "+" (list "--message-id" article))))
  6854. (mh-search "+" article))
  6855. (if (org-mhe-get-message-real-folder)
  6856. (mh-show-msg 1)
  6857. (kill-this-buffer)
  6858. (error "Message not found"))))
  6859. (defun org-open-file (path &optional in-emacs line search)
  6860. "Open the file at PATH.
  6861. First, this expands any special file name abbreviations. Then the
  6862. configuration variable `org-file-apps' is checked if it contains an
  6863. entry for this file type, and if yes, the corresponding command is launched.
  6864. If no application is found, Emacs simply visits the file.
  6865. With optional argument IN-EMACS, Emacs will visit the file.
  6866. Optional LINE specifies a line to go to, optional SEARCH a string to
  6867. search for. If LINE or SEARCH is given, the file will always be
  6868. opened in Emacs.
  6869. If the file does not exist, an error is thrown."
  6870. (setq in-emacs (or in-emacs line search))
  6871. (let* ((file (if (equal path "")
  6872. buffer-file-name
  6873. (convert-standard-filename (org-expand-file-name path))))
  6874. (dirp (file-directory-p file))
  6875. (dfile (downcase file))
  6876. (old-buffer (current-buffer))
  6877. (old-pos (point))
  6878. (old-mode major-mode)
  6879. ext cmd apps)
  6880. (if (and (not (file-exists-p file))
  6881. (not org-open-non-existing-files))
  6882. (error "No such file: %s" file))
  6883. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6884. (setq ext (match-string 1 dfile))
  6885. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6886. (setq ext (match-string 1 dfile))))
  6887. (setq apps (append org-file-apps (org-default-apps)))
  6888. (if in-emacs
  6889. (setq cmd 'emacs)
  6890. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  6891. (cdr (assoc ext apps))
  6892. (cdr (assoc t apps)))))
  6893. (when (eq cmd 'mailcap)
  6894. (require 'mailcap)
  6895. (mailcap-parse-mailcaps)
  6896. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6897. (command (mailcap-mime-info mime-type)))
  6898. (if (stringp command)
  6899. (setq cmd command)
  6900. (setq cmd 'emacs))))
  6901. (cond
  6902. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6903. (setq cmd (format cmd (concat "\"" file "\"")))
  6904. (save-window-excursion
  6905. (shell-command (concat cmd " &"))))
  6906. ((or (stringp cmd)
  6907. (eq cmd 'emacs))
  6908. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  6909. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  6910. (if line (goto-line line)
  6911. (if search (org-link-search search))))
  6912. ((consp cmd)
  6913. (eval cmd))
  6914. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6915. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  6916. (or (not (equal old-buffer (current-buffer)))
  6917. (not (equal old-pos (point))))
  6918. (org-mark-ring-push old-pos old-buffer))))
  6919. (defun org-default-apps ()
  6920. "Return the default applications for this operating system."
  6921. (cond
  6922. ((eq system-type 'darwin)
  6923. org-file-apps-defaults-macosx)
  6924. ((eq system-type 'windows-nt)
  6925. org-file-apps-defaults-windowsnt)
  6926. (t org-file-apps-defaults-gnu)))
  6927. (defun org-expand-file-name (path)
  6928. "Replace special path abbreviations and expand the file name."
  6929. (expand-file-name path))
  6930. (defvar org-insert-link-history nil
  6931. "Minibuffer history for links inserted with `org-insert-link'.")
  6932. (defvar org-stored-links nil
  6933. "Contains the links stored with `org-store-link'.")
  6934. ;;;###autoload
  6935. (defun org-store-link (arg)
  6936. "\\<org-mode-map>Store an org-link to the current location.
  6937. This link can later be inserted into an org-buffer with
  6938. \\[org-insert-link].
  6939. For some link types, a prefix arg is interpreted:
  6940. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  6941. For file links, arg negates `org-context-in-file-links'."
  6942. (interactive "P")
  6943. (let (link cpltxt txt (pos (point)))
  6944. (cond
  6945. ((eq major-mode 'bbdb-mode)
  6946. (setq cpltxt (concat
  6947. "bbdb:"
  6948. (or (bbdb-record-name (bbdb-current-record))
  6949. (bbdb-record-company (bbdb-current-record))))
  6950. link (org-make-link cpltxt)))
  6951. ((eq major-mode 'calendar-mode)
  6952. (let ((cd (calendar-cursor-to-date)))
  6953. (setq link
  6954. (format-time-string
  6955. (car org-time-stamp-formats)
  6956. (apply 'encode-time
  6957. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  6958. nil nil nil))))))
  6959. ((or (eq major-mode 'vm-summary-mode)
  6960. (eq major-mode 'vm-presentation-mode))
  6961. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  6962. (vm-follow-summary-cursor)
  6963. (save-excursion
  6964. (vm-select-folder-buffer)
  6965. (let* ((message (car vm-message-pointer))
  6966. (folder buffer-file-name)
  6967. (subject (vm-su-subject message))
  6968. (author (vm-su-full-name message))
  6969. (message-id (vm-su-message-id message)))
  6970. (setq message-id (org-remove-angle-brackets message-id))
  6971. (setq folder (abbreviate-file-name folder))
  6972. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  6973. folder)
  6974. (setq folder (replace-match "" t t folder)))
  6975. (setq cpltxt (concat author " on: " subject))
  6976. (setq link (concat cpltxt "\n "
  6977. (org-make-link
  6978. "vm:" folder "#" message-id))))))
  6979. ((eq major-mode 'wl-summary-mode)
  6980. (let* ((msgnum (wl-summary-message-number))
  6981. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  6982. msgnum 'message-id))
  6983. (wl-message-entity (elmo-msgdb-overview-get-entity
  6984. msgnum (wl-summary-buffer-msgdb)))
  6985. (author (wl-summary-line-from)) ; FIXME: how to get author name?
  6986. (subject "???")) ; FIXME: How to get subject of email?
  6987. (setq message-id (org-remove-angle-brackets message-id))
  6988. (setq cpltxt (concat author " on: " subject))
  6989. (setq link (concat cpltxt "\n "
  6990. (org-make-link
  6991. "wl:" wl-summary-buffer-folder-name
  6992. "#" message-id)))))
  6993. ((or (equal major-mode 'mh-folder-mode)
  6994. (equal major-mode 'mh-show-mode))
  6995. (let ((from-header (org-mhe-get-header "From:"))
  6996. (to-header (org-mhe-get-header "To:"))
  6997. (subject (org-mhe-get-header "Subject:")))
  6998. (setq cpltxt (concat from-header " on: " subject))
  6999. (setq link (concat cpltxt "\n "
  7000. (org-make-link
  7001. "mhe:" (org-mhe-get-message-real-folder) "#"
  7002. (org-remove-angle-brackets
  7003. (org-mhe-get-header "Message-Id:")))))))
  7004. ((eq major-mode 'rmail-mode)
  7005. (save-excursion
  7006. (save-restriction
  7007. (rmail-narrow-to-non-pruned-header)
  7008. (let ((folder buffer-file-name)
  7009. (message-id (mail-fetch-field "message-id"))
  7010. (author (mail-fetch-field "from"))
  7011. (subject (mail-fetch-field "subject")))
  7012. (setq message-id (org-remove-angle-brackets message-id))
  7013. (setq cpltxt (concat author " on: " subject))
  7014. (setq link (concat cpltxt "\n "
  7015. (org-make-link
  7016. "rmail:" folder "#" message-id)))))))
  7017. ((eq major-mode 'gnus-group-mode)
  7018. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  7019. (gnus-group-group-name)) ; version
  7020. ((fboundp 'gnus-group-name)
  7021. (gnus-group-name))
  7022. (t "???"))))
  7023. (setq cpltxt (concat
  7024. (if (org-xor arg org-usenet-links-prefer-google)
  7025. "http://groups.google.com/groups?group="
  7026. "gnus:")
  7027. group)
  7028. link (org-make-link cpltxt))))
  7029. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  7030. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  7031. (gnus-summary-beginning-of-article)
  7032. (let* ((group (car gnus-article-current))
  7033. (article (cdr gnus-article-current))
  7034. (header (gnus-summary-article-header article))
  7035. (author (mail-header-from header))
  7036. (message-id (mail-header-id header))
  7037. (date (mail-header-date header))
  7038. (subject (gnus-summary-subject-string)))
  7039. (setq cpltxt (concat author " on: " subject))
  7040. (if (org-xor arg org-usenet-links-prefer-google)
  7041. (setq link
  7042. (concat
  7043. cpltxt "\n "
  7044. (format "http://groups.google.com/groups?as_umsgid=%s"
  7045. (org-fixup-message-id-for-http message-id))))
  7046. (setq link (concat cpltxt "\n"
  7047. (org-make-link
  7048. "gnus:" group
  7049. "#" (number-to-string article)))))))
  7050. ((eq major-mode 'w3-mode)
  7051. (setq cpltxt (url-view-url t)
  7052. link (org-make-link cpltxt)))
  7053. ((eq major-mode 'w3m-mode)
  7054. (setq cpltxt w3m-current-url
  7055. link (org-make-link cpltxt)))
  7056. ((eq major-mode 'org-mode)
  7057. ;; Just link to current headline
  7058. (setq cpltxt (concat "file:"
  7059. (abbreviate-file-name buffer-file-name)))
  7060. ;; Add a context search string
  7061. (when (org-xor org-context-in-file-links arg)
  7062. ;; Check if we are on a target
  7063. (if (save-excursion
  7064. (skip-chars-forward "^>\n\r")
  7065. (and (re-search-backward "<<" nil t)
  7066. (looking-at "<<\\(.*?\\)>>")
  7067. (<= (match-beginning 0) pos)
  7068. (>= (match-end 0) pos)))
  7069. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  7070. (setq txt (cond
  7071. ((org-on-heading-p) nil)
  7072. ((org-region-active-p)
  7073. (buffer-substring (region-beginning) (region-end)))
  7074. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  7075. (setq cpltxt
  7076. (concat cpltxt "::"
  7077. (if org-file-link-context-use-camel-case
  7078. (org-make-org-heading-camel txt)
  7079. (org-make-org-heading-search-string txt))))))
  7080. (if (string-match "::\\'" cpltxt)
  7081. (setq cpltxt (substring cpltxt 0 -2)))
  7082. (setq link (org-make-link cpltxt)))
  7083. (buffer-file-name
  7084. ;; Just link to this file here.
  7085. (setq cpltxt (concat "file:"
  7086. (abbreviate-file-name buffer-file-name)))
  7087. ;; Add a context string
  7088. (when (org-xor org-context-in-file-links arg)
  7089. (setq txt (if (org-region-active-p)
  7090. (buffer-substring (region-beginning) (region-end))
  7091. (buffer-substring (point-at-bol) (point-at-eol))))
  7092. (setq cpltxt
  7093. (concat cpltxt "::"
  7094. (if org-file-link-context-use-camel-case
  7095. (org-make-org-heading-camel txt)
  7096. (org-make-org-heading-search-string txt)))))
  7097. (setq link (org-make-link cpltxt)))
  7098. ((interactive-p)
  7099. (error "Cannot link to a buffer which is not visiting a file"))
  7100. (t (setq link nil)))
  7101. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7102. (if (and (interactive-p) link)
  7103. (progn
  7104. (setq org-stored-links
  7105. (cons (cons (or cpltxt link) link) org-stored-links))
  7106. (message "Stored: %s" (or cpltxt link)))
  7107. link)))
  7108. (defun org-make-org-heading-search-string (&optional string heading)
  7109. "Make search string for STRING or current headline."
  7110. (interactive)
  7111. (let ((s (or string (org-get-heading))))
  7112. (unless (and string (not heading))
  7113. ;; We are using a headline, clean up garbage in there.
  7114. (if (string-match org-todo-regexp s)
  7115. (setq s (replace-match "" t t s)))
  7116. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  7117. (setq s (replace-match "" t t s)))
  7118. (setq s (org-trim s))
  7119. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7120. org-comment-string "\\)") s)
  7121. (setq s (replace-match "" t t s)))
  7122. (while (string-match org-ts-regexp s)
  7123. (setq s (replace-match "" t t s))))
  7124. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7125. (setq s (replace-match " " t t s)))
  7126. (or string (setq s (concat "*" s))) ; Add * for headlines
  7127. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7128. (defun org-make-org-heading-camel (&optional string heading)
  7129. "Make a CamelCase string for STRING or the current headline."
  7130. (interactive)
  7131. (let ((s (or string (org-get-heading))))
  7132. (unless (and string (not heading))
  7133. ;; We are using a headline, clean up garbage in there.
  7134. (if (string-match org-todo-regexp s)
  7135. (setq s (replace-match "" t t s)))
  7136. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  7137. (setq s (replace-match "" t t s)))
  7138. (setq s (org-trim s))
  7139. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7140. org-comment-string "\\)") s)
  7141. (setq s (replace-match "" t t s)))
  7142. (while (string-match org-ts-regexp s)
  7143. (setq s (replace-match "" t t s))))
  7144. (while (string-match "[^a-zA-Z_ \t]+" s)
  7145. (setq s (replace-match " " t t s)))
  7146. (or string (setq s (concat "*" s))) ; Add * for headlines
  7147. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  7148. (defun org-make-link (&rest strings)
  7149. "Concatenate STRINGS, format resulting string with `org-link-format'."
  7150. (format org-link-format (apply 'concat strings)))
  7151. (defun org-make-link2 (link &optional description)
  7152. "Make a link with brackets."
  7153. (concat "[[" link "]"
  7154. (if description (concat "[" description "]") "")
  7155. "]"))
  7156. (defun org-xor (a b)
  7157. "Exclusive or."
  7158. (if a (not b) b))
  7159. (defun org-get-header (header)
  7160. "Find a header field in the current buffer."
  7161. (save-excursion
  7162. (goto-char (point-min))
  7163. (let ((case-fold-search t) s)
  7164. (cond
  7165. ((eq header 'from)
  7166. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  7167. (setq s (match-string 1)))
  7168. (while (string-match "\"" s)
  7169. (setq s (replace-match "" t t s)))
  7170. (if (string-match "[<(].*" s)
  7171. (setq s (replace-match "" t t s))))
  7172. ((eq header 'message-id)
  7173. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  7174. (setq s (match-string 1))))
  7175. ((eq header 'subject)
  7176. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  7177. (setq s (match-string 1)))))
  7178. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  7179. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  7180. s)))
  7181. (defun org-fixup-message-id-for-http (s)
  7182. "Replace special characters in a message id, so it can be used in an http query."
  7183. (while (string-match "<" s)
  7184. (setq s (replace-match "%3C" t t s)))
  7185. (while (string-match ">" s)
  7186. (setq s (replace-match "%3E" t t s)))
  7187. (while (string-match "@" s)
  7188. (setq s (replace-match "%40" t t s)))
  7189. s)
  7190. (defun org-insert-link (&optional complete-file)
  7191. "Insert a link. At the prompt, enter the link.
  7192. Completion can be used to select a link previously stored with
  7193. `org-store-link'. When the empty string is entered (i.e. if you just
  7194. press RET at the prompt), the link defaults to the most recently
  7195. stored link. As SPC triggers completion in the minibuffer, you need to
  7196. use M-SPC or C-q SPC to force the insertion of a space character.
  7197. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  7198. selected using completion. The path to the file will be relative to
  7199. the current directory if the file is in the current directory or a
  7200. subdirectory. Otherwise, the link will be the absolute path as
  7201. completed in the minibuffer (i.e. normally ~/path/to/file).
  7202. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  7203. is in the current directory or below."
  7204. (interactive "P")
  7205. (let ((link (if complete-file
  7206. (read-file-name "File: ")
  7207. (completing-read
  7208. "Link: " org-stored-links nil nil nil
  7209. org-insert-link-history
  7210. (or (car (car org-stored-links))))))
  7211. linktxt matched)
  7212. (if (or (not link) (equal link ""))
  7213. (error "No links available"))
  7214. (if complete-file
  7215. (let ((pwd (file-name-as-directory (expand-file-name "."))))
  7216. (cond
  7217. ((equal complete-file '(16))
  7218. (insert
  7219. (org-make-link
  7220. "file:" (abbreviate-file-name (expand-file-name link)))))
  7221. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7222. (expand-file-name link))
  7223. (insert
  7224. (org-make-link
  7225. "file:" (match-string 1 (expand-file-name link)))))
  7226. (t (insert (org-make-link "file:" link)))))
  7227. (setq linktxt (cdr (assoc link org-stored-links)))
  7228. (if (not org-keep-stored-link-after-insertion)
  7229. (setq org-stored-links (delq (assoc link org-stored-links)
  7230. org-stored-links)))
  7231. (if (not linktxt) (setq link (org-make-link link)))
  7232. (setq link (or linktxt link))
  7233. (when (string-match "<\\<file:\\(.+?\\)::\\([^>]+\\)>" link)
  7234. (let* ((path (match-string 1 link))
  7235. (case-fold-search nil)
  7236. (search (match-string 2 link)))
  7237. (when (save-match-data
  7238. (equal (file-truename buffer-file-name)
  7239. (file-truename path)))
  7240. ;; We are linking to this same file
  7241. (if (and org-file-link-context-use-camel-case
  7242. (save-match-data
  7243. (string-match (concat "^" org-camel-regexp "$") search)))
  7244. (setq link (replace-match search t t link)
  7245. matched t)
  7246. (setq link (replace-match (concat "[[" search "]]")
  7247. t t link)
  7248. matched t)))))
  7249. (let ((lines (org-split-string link "\n")))
  7250. (insert (car lines))
  7251. (setq matched (or matched (string-match org-link-regexp (car lines))))
  7252. (setq lines (cdr lines))
  7253. (while lines
  7254. (insert "\n")
  7255. (if (save-excursion
  7256. (beginning-of-line 0)
  7257. (looking-at "[ \t]+\\S-"))
  7258. (indent-relative))
  7259. (setq matched (or matched
  7260. (string-match org-link-regexp (car lines))))
  7261. (insert (car lines))
  7262. (setq lines (cdr lines))))
  7263. (unless matched
  7264. (error "Add link type: http(s),ftp,mailto,file,news,bbdb,vm,wl,rmail,gnus, or shell")))))
  7265. ;;; Hooks for remember.el
  7266. ;;;###autoload
  7267. (defun org-remember-annotation ()
  7268. "Return a link to the current location as an annotation for remember.el.
  7269. If you are using Org-mode files as target for data storage with
  7270. remember.el, then the annotations should include a link compatible with the
  7271. conventions in Org-mode. This function returns such a link."
  7272. (org-store-link nil))
  7273. (defconst org-remember-help
  7274. "Select a destination location for the note.
  7275. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  7276. RET at beg-of-buf -> Append to file as level 2 headline
  7277. RET on headline -> Store as sublevel entry to current headline
  7278. <left>/<right> -> before/after current headline, same headings level")
  7279. ;;;###autoload
  7280. (defun org-remember-apply-template ()
  7281. "Initialize *remember* buffer with template, invode `org-mode'.
  7282. This function should be placed into `remember-mode-hook' and in fact requires
  7283. to be run from that hook to fucntion properly."
  7284. (when org-remember-templates
  7285. (let* ((entry (if (= (length org-remember-templates) 1)
  7286. (cdar org-remember-templates)
  7287. (message "Select template: %s"
  7288. (mapconcat
  7289. (lambda (x) (char-to-string (car x)))
  7290. org-remember-templates " "))
  7291. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  7292. (tpl (if (consp (cdr entry)) (cadr entry) (cdr entry)))
  7293. (file (if (consp (cdr entry)) (nth 2 entry)))
  7294. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  7295. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  7296. (v-u (concat "[" (substring v-t 1 -1) "]"))
  7297. (v-U (concat "[" (substring v-T 1 -1) "]"))
  7298. (v-a annotation) ; defined in `remember-mode'
  7299. (v-i initial) ; defined in `remember-mode'
  7300. (v-n user-full-name)
  7301. )
  7302. (unless tpl (setq tpl "") (message "No template") (ding))
  7303. (insert tpl) (goto-char (point-min))
  7304. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  7305. (when (and initial (equal (match-string 0) "%i"))
  7306. (save-match-data
  7307. (let* ((lead (buffer-substring
  7308. (point-at-bol) (match-beginning 0))))
  7309. (setq v-i (mapconcat 'identity
  7310. (org-split-string initial "\n")
  7311. (concat "\n" lead))))))
  7312. (replace-match
  7313. (or (eval (intern (concat "v-" (match-string 1)))) "")
  7314. t t))
  7315. (let ((org-startup-folded nil)
  7316. (org-startup-with-deadline-check nil))
  7317. (org-mode))
  7318. ; (use-local-map org-mode-map)
  7319. ; (org-set-font-lock-defaults)
  7320. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  7321. (set (make-local-variable 'org-default-notes-file) file))
  7322. (goto-char (point-min))
  7323. (if (re-search-forward "%\\?" nil t) (replace-match "")))))
  7324. ;;;###autoload
  7325. (defun org-remember-handler ()
  7326. "Store stuff from remember.el into an org file.
  7327. First prompts for an org file. If the user just presses return, the value
  7328. of `org-default-notes-file' is used.
  7329. Then the command offers the headings tree of the selected file in order to
  7330. file the text at a specific location.
  7331. You can either immediately press RET to get the note appended to the
  7332. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  7333. find a better place. Then press RET or <left> or <right> in insert the note.
  7334. Key Cursor position Note gets inserted
  7335. -----------------------------------------------------------------------------
  7336. RET buffer-start as level 2 heading at end of file
  7337. RET on headline as sublevel of the heading at cursor
  7338. RET no heading at cursor position, level taken from context.
  7339. Or use prefix arg to specify level manually.
  7340. <left> on headline as same level, before current heading
  7341. <right> on headline as same level, after current heading
  7342. So the fastest way to store the note is to press RET RET to append it to
  7343. the default file. This way your current train of thought is not
  7344. interrupted, in accordance with the principles of remember.el. But with
  7345. little extra effort, you can push it directly to the correct location.
  7346. Before being stored away, the function ensures that the text has a
  7347. headline, i.e. a first line that starts with a \"*\". If not, a headline
  7348. is constructed from the current date and some additional data.
  7349. If the variable `org-adapt-indentation' is non-nil, the entire text is
  7350. also indented so that it starts in the same column as the headline
  7351. \(i.e. after the stars).
  7352. See also the variable `org-reverse-note-order'."
  7353. (catch 'quit
  7354. (let* ((txt (buffer-substring (point-min) (point-max)))
  7355. (fastp current-prefix-arg)
  7356. (file (if fastp org-default-notes-file (org-get-org-file)))
  7357. (visiting (find-buffer-visiting file))
  7358. (org-startup-with-deadline-check nil)
  7359. (org-startup-folded nil)
  7360. spos level indent reversed)
  7361. ;; Modify text so that it becomes a nice subtree which can be inserted
  7362. ;; into an org tree.
  7363. (let* ((lines (split-string txt "\n"))
  7364. first)
  7365. ;; remove empty lines at the beginning
  7366. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  7367. (setq lines (cdr lines)))
  7368. (setq first (car lines) lines (cdr lines))
  7369. (if (string-match "^\\*+" first)
  7370. ;; Is already a headline
  7371. (setq indent nil)
  7372. ;; We need to add a headline: Use time and first buffer line
  7373. (setq lines (cons first lines)
  7374. first (concat "* " (current-time-string)
  7375. " (" (remember-buffer-desc) ")")
  7376. indent " "))
  7377. (if (and org-adapt-indentation indent)
  7378. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  7379. (setq txt (concat first "\n"
  7380. (mapconcat 'identity lines "\n"))))
  7381. ;; Find the file
  7382. (if (not visiting)
  7383. (find-file-noselect file))
  7384. (with-current-buffer (get-file-buffer file)
  7385. (setq reversed (org-notes-order-reversed-p))
  7386. (save-excursion
  7387. (save-restriction
  7388. (widen)
  7389. ;; Ask the User for a location
  7390. (setq spos (if fastp 1 (org-get-location
  7391. (current-buffer)
  7392. org-remember-help)))
  7393. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  7394. ; not handle this note
  7395. (goto-char spos)
  7396. (cond ((bobp)
  7397. ;; Put it at the start or end, as level 2
  7398. (save-restriction
  7399. (widen)
  7400. (goto-char (if reversed (point-min) (point-max)))
  7401. (if (not (bolp)) (newline))
  7402. (org-paste-subtree 2 txt)))
  7403. ((and (org-on-heading-p nil) (not current-prefix-arg))
  7404. ;; Put it below this entry, at the beg/end of the subtree
  7405. (org-back-to-heading)
  7406. (setq level (funcall outline-level))
  7407. (if reversed
  7408. (outline-end-of-heading)
  7409. (outline-end-of-subtree))
  7410. (if (not (bolp)) (newline))
  7411. (beginning-of-line 1)
  7412. (org-paste-subtree (1+ level) txt))
  7413. (t
  7414. ;; Put it right there, with automatic level determined by
  7415. ;; org-paste-subtree or from prefix arg
  7416. (org-paste-subtree current-prefix-arg txt)))
  7417. (when remember-save-after-remembering
  7418. (save-buffer)
  7419. (if (not visiting) (kill-buffer (current-buffer)))))))))
  7420. t) ;; return t to indicate that we took care of this note.
  7421. (defun org-get-org-file ()
  7422. "Read a filename, with default directory `org-directory'."
  7423. (let ((default (or org-default-notes-file remember-data-file)))
  7424. (read-file-name (format "File name [%s]: " default)
  7425. (file-name-as-directory org-directory)
  7426. default)))
  7427. (defun org-notes-order-reversed-p ()
  7428. "Check if the current file should receive notes in reversed order."
  7429. (cond
  7430. ((not org-reverse-note-order) nil)
  7431. ((eq t org-reverse-note-order) t)
  7432. ((not (listp org-reverse-note-order)) nil)
  7433. (t (catch 'exit
  7434. (let ((all org-reverse-note-order)
  7435. entry)
  7436. (while (setq entry (pop all))
  7437. (if (string-match (car entry) buffer-file-name)
  7438. (throw 'exit (cdr entry))))
  7439. nil)))))
  7440. ;;; Tables
  7441. ;; Watch out: Here we are talking about two different kind of tables.
  7442. ;; Most of the code is for the tables created with the Org-mode table editor.
  7443. ;; Sometimes, we talk about tables created and edited with the table.el
  7444. ;; Emacs package. We call the former org-type tables, and the latter
  7445. ;; table.el-type tables.
  7446. (defun org-before-change-function (beg end)
  7447. "Every change indicates that a table might need an update."
  7448. (setq org-table-may-need-update t))
  7449. (defconst org-table-line-regexp "^[ \t]*|"
  7450. "Detects an org-type table line.")
  7451. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7452. "Detects an org-type table line.")
  7453. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7454. "Detects a table line marked for automatic recalculation.")
  7455. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7456. "Detects a table line marked for automatic recalculation.")
  7457. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7458. "Detects a table line marked for automatic recalculation.")
  7459. (defconst org-table-hline-regexp "^[ \t]*|-"
  7460. "Detects an org-type table hline.")
  7461. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7462. "Detects a table-type table hline.")
  7463. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7464. "Detects an org-type or table-type table.")
  7465. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7466. "Searching from within a table (any type) this finds the first line
  7467. outside the table.")
  7468. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7469. "Searching from within a table (any type) this finds the first line
  7470. outside the table.")
  7471. (defun org-table-create-with-table.el ()
  7472. "Use the table.el package to insert a new table.
  7473. If there is already a table at point, convert between Org-mode tables
  7474. and table.el tables."
  7475. (interactive)
  7476. (require 'table)
  7477. (cond
  7478. ((org-at-table.el-p)
  7479. (if (y-or-n-p "Convert table to Org-mode table? ")
  7480. (org-table-convert)))
  7481. ((org-at-table-p)
  7482. (if (y-or-n-p "Convert table to table.el table? ")
  7483. (org-table-convert)))
  7484. (t (call-interactively 'table-insert))))
  7485. (defun org-table-create (&optional size)
  7486. "Query for a size and insert a table skeleton.
  7487. SIZE is a string Columns x Rows like for example \"3x2\"."
  7488. (interactive "P")
  7489. (unless size
  7490. (setq size (read-string
  7491. (concat "Table size Columns x Rows [e.g. "
  7492. org-table-default-size "]: ")
  7493. "" nil org-table-default-size)))
  7494. (let* ((pos (point))
  7495. (indent (make-string (current-column) ?\ ))
  7496. (split (org-split-string size " *x *"))
  7497. (rows (string-to-number (nth 1 split)))
  7498. (columns (string-to-number (car split)))
  7499. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7500. "\n")))
  7501. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7502. (point-at-bol) (point)))
  7503. (beginning-of-line 1)
  7504. (newline))
  7505. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7506. (dotimes (i rows) (insert line))
  7507. (goto-char pos)
  7508. (if (> rows 1)
  7509. ;; Insert a hline after the first row.
  7510. (progn
  7511. (end-of-line 1)
  7512. (insert "\n|-")
  7513. (goto-char pos)))
  7514. (org-table-align)))
  7515. (defun org-table-convert-region (beg0 end0 nspace)
  7516. "Convert region to a table.
  7517. The region goes from BEG0 to END0, but these borders will be moved
  7518. slightly, to make sure a beginning of line in the first line is included.
  7519. When NSPACE is non-nil, it indicates the minimum number of spaces that
  7520. separate columns (default: just one space)."
  7521. (let* ((beg (min beg0 end0))
  7522. (end (max beg0 end0))
  7523. (tabsep t)
  7524. re)
  7525. (goto-char beg)
  7526. (beginning-of-line 1)
  7527. (setq beg (move-marker (make-marker) (point)))
  7528. (goto-char end)
  7529. (if (bolp) (backward-char 1) (end-of-line 1))
  7530. (setq end (move-marker (make-marker) (point)))
  7531. ;; Lets see if this is tab-separated material. If every nonempty line
  7532. ;; contains a tab, we will assume that it is tab-separated material
  7533. (if nspace
  7534. (setq tabsep nil)
  7535. (goto-char beg)
  7536. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  7537. (if nspace (setq tabsep nil))
  7538. (if tabsep
  7539. (setq re "^\\|\t")
  7540. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  7541. (max 1 (prefix-numeric-value nspace)))))
  7542. (goto-char beg)
  7543. (while (re-search-forward re end t)
  7544. (replace-match "|" t t))
  7545. (goto-char beg)
  7546. (insert " ")
  7547. (org-table-align)))
  7548. (defun org-table-import (file arg)
  7549. "Import FILE as a table.
  7550. The file is assumed to be tab-separated. Such files can be produced by most
  7551. spreadsheet and database applications. If no tabs (at least one per line)
  7552. are found, lines will be split on whitespace into fields."
  7553. (interactive "f\nP")
  7554. (or (bolp) (newline))
  7555. (let ((beg (point))
  7556. (pm (point-max)))
  7557. (insert-file-contents file)
  7558. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7559. (defun org-table-export ()
  7560. "Export table as a tab-separated file.
  7561. Such a file can be imported into a spreadsheet program like Excel."
  7562. (interactive)
  7563. (let* ((beg (org-table-begin))
  7564. (end (org-table-end))
  7565. (table (buffer-substring beg end))
  7566. (file (read-file-name "Export table to: "))
  7567. buf)
  7568. (unless (or (not (file-exists-p file))
  7569. (y-or-n-p (format "Overwrite file %s? " file)))
  7570. (error "Abort"))
  7571. (with-current-buffer (find-file-noselect file)
  7572. (setq buf (current-buffer))
  7573. (erase-buffer)
  7574. (fundamental-mode)
  7575. (insert table)
  7576. (goto-char (point-min))
  7577. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7578. (replace-match "" t t)
  7579. (end-of-line 1))
  7580. (goto-char (point-min))
  7581. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7582. (replace-match "" t t)
  7583. (goto-char (min (1+ (point)) (point-max))))
  7584. (goto-char (point-min))
  7585. (while (re-search-forward "^-[-+]*$" nil t)
  7586. (replace-match "")
  7587. (if (looking-at "\n")
  7588. (delete-char 1)))
  7589. (goto-char (point-min))
  7590. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7591. (replace-match "\t" t t))
  7592. (save-buffer))
  7593. (kill-buffer buf)))
  7594. (defvar org-table-aligned-begin-marker (make-marker)
  7595. "Marker at the beginning of the table last aligned.
  7596. Used to check if cursor still is in that table, to minimize realignment.")
  7597. (defvar org-table-aligned-end-marker (make-marker)
  7598. "Marker at the end of the table last aligned.
  7599. Used to check if cursor still is in that table, to minimize realignment.")
  7600. (defvar org-table-last-alignment nil
  7601. "List of flags for flushright alignment, from the last re-alignment.
  7602. This is being used to correctly align a single field after TAB or RET.")
  7603. (defvar org-table-last-column-widths nil
  7604. "List of max width of fields in each column.
  7605. This is being used to correctly align a single field after TAB or RET.")
  7606. (defvar org-last-recalc-line nil)
  7607. (defun org-table-align ()
  7608. "Align the table at point by aligning all vertical bars."
  7609. (interactive)
  7610. ;; (message "align") (sit-for 2)
  7611. (let* (
  7612. ;; Limits of table
  7613. (beg (org-table-begin))
  7614. (end (org-table-end))
  7615. ;; Current cursor position
  7616. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  7617. (colpos (org-table-current-column))
  7618. (winstart (window-start))
  7619. text lines (new "") lengths l typenums ty fields maxfields i
  7620. column
  7621. (indent "") cnt frac
  7622. rfmt hfmt
  7623. (spaces (if (org-in-invisibility-spec-p '(org-table))
  7624. org-table-spaces-around-invisible-separators
  7625. org-table-spaces-around-separators))
  7626. (sp1 (car spaces))
  7627. (sp2 (cdr spaces))
  7628. (rfmt1 (concat
  7629. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7630. (hfmt1 (concat
  7631. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7632. emptystrings)
  7633. (untabify beg end)
  7634. ;; (message "Aligning table...")
  7635. ;; Get the rows
  7636. (setq lines (org-split-string
  7637. (buffer-substring-no-properties beg end) "\n"))
  7638. ;; Store the indentation of the first line
  7639. (if (string-match "^ *" (car lines))
  7640. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7641. ;; Mark the hlines
  7642. (setq lines (mapcar (lambda (l)
  7643. (if (string-match "^ *|-" l)
  7644. nil
  7645. (if (string-match "[ \t]+$" l)
  7646. (substring l 0 (match-beginning 0))
  7647. l)))
  7648. lines))
  7649. ;; Get the data fields
  7650. (setq fields (mapcar
  7651. (lambda (l)
  7652. (org-split-string l " *| *"))
  7653. (delq nil (copy-sequence lines))))
  7654. ;; How many fields in the longest line?
  7655. (condition-case nil
  7656. (setq maxfields (apply 'max (mapcar 'length fields)))
  7657. (error
  7658. (kill-region beg end)
  7659. (org-table-create org-table-default-size)
  7660. (error "Empty table - created default table")))
  7661. ;; A list of empty string to fill any short rows on output
  7662. (setq emptystrings (make-list maxfields ""))
  7663. ;; Get the maximum length of a field and the most common datatype
  7664. ;; for each column
  7665. (setq i -1)
  7666. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7667. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7668. ;; maximum length
  7669. (push (apply 'max 1 (mapcar 'string-width column)) lengths)
  7670. ;; compute the fraction stepwise, ignoring empty fields
  7671. (setq cnt 0 frac 0.0)
  7672. (mapcar
  7673. (lambda (x)
  7674. (if (equal x "")
  7675. nil
  7676. (setq frac ( / (+ (* frac cnt)
  7677. (if (string-match org-table-number-regexp x) 1 0))
  7678. (setq cnt (1+ cnt))))))
  7679. column)
  7680. (push (>= frac org-table-number-fraction) typenums))
  7681. (setq lengths (nreverse lengths)
  7682. typenums (nreverse typenums))
  7683. (setq org-table-last-alignment typenums
  7684. org-table-last-column-widths lengths)
  7685. ;; Compute the formats needed for output of the table
  7686. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7687. (while (setq l (pop lengths))
  7688. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7689. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7690. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7691. (setq rfmt (concat rfmt "\n")
  7692. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7693. ;; Produce the new table
  7694. ;;(while lines
  7695. ;; (setq l (pop lines))
  7696. ;; (if l
  7697. ;; (setq new (concat new (apply 'format rfmt
  7698. ;; (append (pop fields) emptystrings))))
  7699. ;; (setq new (concat new hfmt))))
  7700. (setq new (mapconcat
  7701. (lambda (l)
  7702. (if l (apply 'format rfmt
  7703. (append (pop fields) emptystrings))
  7704. hfmt))
  7705. lines ""))
  7706. ;; Replace the old one
  7707. (delete-region beg end)
  7708. (move-marker end nil)
  7709. (move-marker org-table-aligned-begin-marker (point))
  7710. (insert new)
  7711. (move-marker org-table-aligned-end-marker (point))
  7712. ;; Try to move to the old location (approximately)
  7713. (goto-line linepos)
  7714. (set-window-start (selected-window) winstart 'noforce)
  7715. (org-table-goto-column colpos)
  7716. (setq org-table-may-need-update nil)
  7717. (if (org-in-invisibility-spec-p '(org-table))
  7718. (org-table-add-invisible-to-vertical-lines))
  7719. ))
  7720. (defun org-table-begin (&optional table-type)
  7721. "Find the beginning of the table and return its position.
  7722. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7723. (save-excursion
  7724. (if (not (re-search-backward
  7725. (if table-type org-table-any-border-regexp
  7726. org-table-border-regexp)
  7727. nil t))
  7728. (error "Can't find beginning of table")
  7729. (goto-char (match-beginning 0))
  7730. (beginning-of-line 2)
  7731. (point))))
  7732. (defun org-table-end (&optional table-type)
  7733. "Find the end of the table and return its position.
  7734. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7735. (save-excursion
  7736. (if (not (re-search-forward
  7737. (if table-type org-table-any-border-regexp
  7738. org-table-border-regexp)
  7739. nil t))
  7740. (goto-char (point-max))
  7741. (goto-char (match-beginning 0)))
  7742. (point-marker)))
  7743. (defun org-table-justify-field-maybe (&optional new)
  7744. "Justify the current field, text to left, number to right.
  7745. Optional argument NEW may specify text to replace the current field content."
  7746. (cond
  7747. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7748. ((org-at-table-hline-p)
  7749. ;; FIXME: I used to enforce realign here, but I think this is not needed.
  7750. ;; (setq org-table-may-need-update t)
  7751. )
  7752. ((and (not new)
  7753. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7754. (current-buffer)))
  7755. (< (point) org-table-aligned-begin-marker)
  7756. (>= (point) org-table-aligned-end-marker)))
  7757. ;; This is not the same table, force a full re-align
  7758. (setq org-table-may-need-update t))
  7759. (t ;; realign the current field, based on previous full realign
  7760. (let* ((pos (point)) s
  7761. (col (org-table-current-column))
  7762. (num (nth (1- col) org-table-last-alignment))
  7763. l f n o e)
  7764. (when (> col 0)
  7765. (skip-chars-backward "^|\n")
  7766. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7767. (progn
  7768. (setq s (match-string 1)
  7769. o (match-string 0)
  7770. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7771. e (not (= (match-beginning 2) (match-end 2))))
  7772. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7773. l (if e "|" (setq org-table-may-need-update t) ""))
  7774. n (format f s t t))
  7775. (if new
  7776. (if (<= (length new) l)
  7777. (setq n (format f new t t)) ;; FIXME: why t t?????
  7778. (setq n (concat new "|") org-table-may-need-update t)))
  7779. (or (equal n o)
  7780. (let (org-table-may-need-update)
  7781. (replace-match n))))
  7782. (setq org-table-may-need-update t))
  7783. (goto-char pos))))))
  7784. (defun org-table-next-field ()
  7785. "Go to the next field in the current table, creating new lines as needed.
  7786. Before doing so, re-align the table if necessary."
  7787. (interactive)
  7788. (org-table-maybe-eval-formula)
  7789. (org-table-maybe-recalculate-line)
  7790. (if (and org-table-automatic-realign
  7791. org-table-may-need-update)
  7792. (org-table-align))
  7793. (let ((end (org-table-end)))
  7794. (if (org-at-table-hline-p)
  7795. (end-of-line 1))
  7796. (condition-case nil
  7797. (progn
  7798. (re-search-forward "|" end)
  7799. (if (looking-at "[ \t]*$")
  7800. (re-search-forward "|" end))
  7801. (if (and (looking-at "-")
  7802. org-table-tab-jumps-over-hlines
  7803. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7804. (goto-char (match-beginning 1)))
  7805. (if (looking-at "-")
  7806. (progn
  7807. (beginning-of-line 0)
  7808. (org-table-insert-row 'below))
  7809. (if (looking-at " ") (forward-char 1))))
  7810. (error
  7811. (org-table-insert-row 'below)))))
  7812. (defun org-table-previous-field ()
  7813. "Go to the previous field in the table.
  7814. Before doing so, re-align the table if necessary."
  7815. (interactive)
  7816. (org-table-justify-field-maybe)
  7817. (org-table-maybe-recalculate-line)
  7818. (if (and org-table-automatic-realign
  7819. org-table-may-need-update)
  7820. (org-table-align))
  7821. (if (org-at-table-hline-p)
  7822. (end-of-line 1))
  7823. (re-search-backward "|" (org-table-begin))
  7824. (re-search-backward "|" (org-table-begin))
  7825. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7826. (re-search-backward "|" (org-table-begin)))
  7827. (if (looking-at "| ?")
  7828. (goto-char (match-end 0))))
  7829. (defun org-table-next-row ()
  7830. "Go to the next row (same column) in the current table.
  7831. Before doing so, re-align the table if necessary."
  7832. (interactive)
  7833. (org-table-maybe-eval-formula)
  7834. (org-table-maybe-recalculate-line)
  7835. (if (or (looking-at "[ \t]*$")
  7836. (save-excursion (skip-chars-backward " \t") (bolp)))
  7837. (newline)
  7838. (if (and org-table-automatic-realign
  7839. org-table-may-need-update)
  7840. (org-table-align))
  7841. (let ((col (org-table-current-column)))
  7842. (beginning-of-line 2)
  7843. (if (or (not (org-at-table-p))
  7844. (org-at-table-hline-p))
  7845. (progn
  7846. (beginning-of-line 0)
  7847. (org-table-insert-row 'below)))
  7848. (org-table-goto-column col)
  7849. (skip-chars-backward "^|\n\r")
  7850. (if (looking-at " ") (forward-char 1)))))
  7851. (defun org-table-copy-down (n)
  7852. "Copy a field down in the current column.
  7853. If the field at the cursor is empty, copy into it the content of the nearest
  7854. non-empty field above. With argument N, use the Nth non-empty field.
  7855. If the current field is not empty, it is copied down to the next row, and
  7856. the cursor is moved with it. Therefore, repeating this command causes the
  7857. column to be filled row-by-row.
  7858. If the variable `org-table-copy-increment' is non-nil and the field is an
  7859. integer, it will be incremented while copying."
  7860. (interactive "p")
  7861. (let* ((colpos (org-table-current-column))
  7862. (field (org-table-get-field))
  7863. (non-empty (string-match "[^ \t]" field))
  7864. (beg (org-table-begin))
  7865. txt)
  7866. (org-table-check-inside-data-field)
  7867. (if non-empty
  7868. (progn
  7869. (setq txt (org-trim field))
  7870. (org-table-next-row)
  7871. (org-table-blank-field))
  7872. (save-excursion
  7873. (setq txt
  7874. (catch 'exit
  7875. (while (progn (beginning-of-line 1)
  7876. (re-search-backward org-table-dataline-regexp
  7877. beg t))
  7878. (org-table-goto-column colpos t)
  7879. (if (and (looking-at
  7880. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  7881. (= (setq n (1- n)) 0))
  7882. (throw 'exit (match-string 1))))))))
  7883. (if txt
  7884. (progn
  7885. (if (and org-table-copy-increment
  7886. (string-match "^[0-9]+$" txt))
  7887. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  7888. (insert txt)
  7889. (org-table-maybe-recalculate-line)
  7890. (org-table-align))
  7891. (error "No non-empty field found"))))
  7892. (defun org-table-check-inside-data-field ()
  7893. "Is point inside a table data field?
  7894. I.e. not on a hline or before the first or after the last column?
  7895. This actually throws an error, so it aborts the current command."
  7896. (if (or (not (org-at-table-p))
  7897. (= (org-table-current-column) 0)
  7898. (org-at-table-hline-p)
  7899. (looking-at "[ \t]*$"))
  7900. (error "Not in table data field")))
  7901. (defvar org-table-clip nil
  7902. "Clipboard for table regions.")
  7903. (defun org-table-blank-field ()
  7904. "Blank the current table field or active region."
  7905. (interactive)
  7906. (org-table-check-inside-data-field)
  7907. (if (and (interactive-p) (org-region-active-p))
  7908. (let (org-table-clip)
  7909. (org-table-cut-region (region-beginning) (region-end)))
  7910. (skip-chars-backward "^|")
  7911. (backward-char 1)
  7912. (if (looking-at "|[^|\n]+")
  7913. (let* ((pos (match-beginning 0))
  7914. (match (match-string 0))
  7915. (len (string-width match)))
  7916. (replace-match (concat "|" (make-string (1- len) ?\ )))
  7917. (goto-char (+ 2 pos))
  7918. (substring match 1)))))
  7919. (defun org-table-get-field (&optional n replace)
  7920. "Return the value of the field in column N of current row.
  7921. N defaults to current field.
  7922. If REPLACE is a string, replace field with this value. The return value
  7923. is always the old value."
  7924. (and n (org-table-goto-column n))
  7925. (skip-chars-backward "^|\n")
  7926. (backward-char 1)
  7927. (if (looking-at "|[^|\r\n]*")
  7928. (let* ((pos (match-beginning 0))
  7929. (val (buffer-substring (1+ pos) (match-end 0))))
  7930. (if replace
  7931. (replace-match (concat "|" replace)))
  7932. (goto-char (min (point-at-eol) (+ 2 pos)))
  7933. val)
  7934. (forward-char 1) ""))
  7935. (defun org-table-current-column ()
  7936. "Find out which column we are in.
  7937. When called interactively, column is also displayed in echo area."
  7938. (interactive)
  7939. (if (interactive-p) (org-table-check-inside-data-field))
  7940. (save-excursion
  7941. (let ((cnt 0) (pos (point)))
  7942. (beginning-of-line 1)
  7943. (while (search-forward "|" pos t)
  7944. (setq cnt (1+ cnt)))
  7945. (if (interactive-p) (message "This is table column %d" cnt))
  7946. cnt)))
  7947. (defun org-table-goto-column (n &optional on-delim force)
  7948. "Move the cursor to the Nth column in the current table line.
  7949. With optional argument ON-DELIM, stop with point before the left delimiter
  7950. of the field.
  7951. If there are less than N fields, just go to after the last delimiter.
  7952. However, when FORCE is non-nil, create new columns if necessary."
  7953. (interactive "p")
  7954. (let ((pos (point-at-eol)))
  7955. (beginning-of-line 1)
  7956. (when (> n 0)
  7957. (while (and (> (setq n (1- n)) -1)
  7958. (or (search-forward "|" pos t)
  7959. (and force
  7960. (progn (end-of-line 1)
  7961. (skip-chars-backward "^|")
  7962. (insert " | "))))))
  7963. ; (backward-char 2) t)))))
  7964. (when (and force (not (looking-at ".*|")))
  7965. (save-excursion (end-of-line 1) (insert " | ")))
  7966. (if on-delim
  7967. (backward-char 1)
  7968. (if (looking-at " ") (forward-char 1))))))
  7969. (defun org-at-table-p (&optional table-type)
  7970. "Return t if the cursor is inside an org-type table.
  7971. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  7972. (if org-enable-table-editor
  7973. (save-excursion
  7974. (beginning-of-line 1)
  7975. (looking-at (if table-type org-table-any-line-regexp
  7976. org-table-line-regexp)))
  7977. nil))
  7978. (defun org-at-table.el-p ()
  7979. "Return t if and only if we are at a table.el table."
  7980. (and (org-at-table-p 'any)
  7981. (save-excursion
  7982. (goto-char (org-table-begin 'any))
  7983. (looking-at org-table1-hline-regexp))))
  7984. (defun org-table-recognize-table.el ()
  7985. "If there is a table.el table nearby, recognize it and move into it."
  7986. (if org-table-tab-recognizes-table.el
  7987. (if (org-at-table.el-p)
  7988. (progn
  7989. (beginning-of-line 1)
  7990. (if (looking-at org-table-dataline-regexp)
  7991. nil
  7992. (if (looking-at org-table1-hline-regexp)
  7993. (progn
  7994. (beginning-of-line 2)
  7995. (if (looking-at org-table-any-border-regexp)
  7996. (beginning-of-line -1)))))
  7997. (if (re-search-forward "|" (org-table-end t) t)
  7998. (progn
  7999. (require 'table)
  8000. (if (table--at-cell-p (point))
  8001. t
  8002. (message "recognizing table.el table...")
  8003. (table-recognize-table)
  8004. (message "recognizing table.el table...done")))
  8005. (error "This should not happen..."))
  8006. t)
  8007. nil)
  8008. nil))
  8009. (defun org-at-table-hline-p ()
  8010. "Return t if the cursor is inside a hline in a table."
  8011. (if org-enable-table-editor
  8012. (save-excursion
  8013. (beginning-of-line 1)
  8014. (looking-at org-table-hline-regexp))
  8015. nil))
  8016. (defun org-table-insert-column ()
  8017. "Insert a new column into the table."
  8018. (interactive)
  8019. (if (not (org-at-table-p))
  8020. (error "Not at a table"))
  8021. (org-table-find-dataline)
  8022. (let* ((col (max 1 (org-table-current-column)))
  8023. (beg (org-table-begin))
  8024. (end (org-table-end))
  8025. ;; Current cursor position
  8026. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8027. (colpos col))
  8028. (goto-char beg)
  8029. (while (< (point) end)
  8030. (if (org-at-table-hline-p)
  8031. nil
  8032. (org-table-goto-column col t)
  8033. (insert "| "))
  8034. (beginning-of-line 2))
  8035. (move-marker end nil)
  8036. (goto-line linepos)
  8037. (org-table-goto-column colpos)
  8038. (org-table-align)
  8039. (org-table-modify-formulas 'insert col)))
  8040. (defun org-table-find-dataline ()
  8041. "Find a dataline in the current table, which is needed for column commands."
  8042. (if (and (org-at-table-p)
  8043. (not (org-at-table-hline-p)))
  8044. t
  8045. (let ((col (current-column))
  8046. (end (org-table-end)))
  8047. (move-to-column col)
  8048. (while (and (< (point) end)
  8049. (or (not (= (current-column) col))
  8050. (org-at-table-hline-p)))
  8051. (beginning-of-line 2)
  8052. (move-to-column col))
  8053. (if (and (org-at-table-p)
  8054. (not (org-at-table-hline-p)))
  8055. t
  8056. (error
  8057. "Please position cursor in a data line for column operations")))))
  8058. (defun org-table-delete-column ()
  8059. "Delete a column into the table."
  8060. (interactive)
  8061. (if (not (org-at-table-p))
  8062. (error "Not at a table"))
  8063. (org-table-find-dataline)
  8064. (org-table-check-inside-data-field)
  8065. (let* ((col (org-table-current-column))
  8066. (beg (org-table-begin))
  8067. (end (org-table-end))
  8068. ;; Current cursor position
  8069. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8070. (colpos col))
  8071. (goto-char beg)
  8072. (while (< (point) end)
  8073. (if (org-at-table-hline-p)
  8074. nil
  8075. (org-table-goto-column col t)
  8076. (and (looking-at "|[^|\n]+|")
  8077. (replace-match "|")))
  8078. (beginning-of-line 2))
  8079. (move-marker end nil)
  8080. (goto-line linepos)
  8081. (org-table-goto-column colpos)
  8082. (org-table-align)
  8083. (org-table-modify-formulas 'remove col)))
  8084. (defun org-table-move-column-right ()
  8085. "Move column to the right."
  8086. (interactive)
  8087. (org-table-move-column nil))
  8088. (defun org-table-move-column-left ()
  8089. "Move column to the left."
  8090. (interactive)
  8091. (org-table-move-column 'left))
  8092. (defun org-table-move-column (&optional left)
  8093. "Move the current column to the right. With arg LEFT, move to the left."
  8094. (interactive "P")
  8095. (if (not (org-at-table-p))
  8096. (error "Not at a table"))
  8097. (org-table-find-dataline)
  8098. (org-table-check-inside-data-field)
  8099. (let* ((col (org-table-current-column))
  8100. (col1 (if left (1- col) col))
  8101. (beg (org-table-begin))
  8102. (end (org-table-end))
  8103. ;; Current cursor position
  8104. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8105. (colpos (if left (1- col) (1+ col))))
  8106. (if (and left (= col 1))
  8107. (error "Cannot move column further left"))
  8108. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8109. (error "Cannot move column further right"))
  8110. (goto-char beg)
  8111. (while (< (point) end)
  8112. (if (org-at-table-hline-p)
  8113. nil
  8114. (org-table-goto-column col1 t)
  8115. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8116. (replace-match "|\\2|\\1|")))
  8117. (beginning-of-line 2))
  8118. (move-marker end nil)
  8119. (goto-line linepos)
  8120. (org-table-goto-column colpos)
  8121. (org-table-align)
  8122. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  8123. (defun org-table-move-row-down ()
  8124. "Move table row down."
  8125. (interactive)
  8126. (org-table-move-row nil))
  8127. (defun org-table-move-row-up ()
  8128. "Move table row up."
  8129. (interactive)
  8130. (org-table-move-row 'up))
  8131. (defun org-table-move-row (&optional up)
  8132. "Move the current table line down. With arg UP, move it up."
  8133. (interactive "P")
  8134. (let ((col (current-column))
  8135. (pos (point))
  8136. (tonew (if up 0 2))
  8137. txt)
  8138. (beginning-of-line tonew)
  8139. (if (not (org-at-table-p))
  8140. (progn
  8141. (goto-char pos)
  8142. (error "Cannot move row further")))
  8143. (goto-char pos)
  8144. (beginning-of-line 1)
  8145. (setq pos (point))
  8146. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8147. (delete-region (point) (1+ (point-at-eol)))
  8148. (beginning-of-line tonew)
  8149. (insert txt)
  8150. (beginning-of-line 0)
  8151. (move-to-column col)))
  8152. (defun org-table-insert-row (&optional arg)
  8153. "Insert a new row above the current line into the table.
  8154. With prefix ARG, insert below the current line."
  8155. (interactive "P")
  8156. (if (not (org-at-table-p))
  8157. (error "Not at a table"))
  8158. (let* ((line
  8159. (org-expand-wide-chars
  8160. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  8161. new)
  8162. (if (string-match "^[ \t]*|-" line)
  8163. (setq new (mapcar (lambda (x) (if (member x '(?| ?+)) ?| ?\ )) line))
  8164. (setq new (mapcar (lambda (x) (if (equal x ?|) ?| ?\ )) line)))
  8165. ;; Fix the first field if necessary
  8166. (setq new (concat new))
  8167. (if (string-match "^[ \t]*| *[#$] *|" line)
  8168. (setq new (replace-match (match-string 0 line) t t new)))
  8169. (beginning-of-line (if arg 2 1))
  8170. (let (org-table-may-need-update)
  8171. (insert-before-markers new)
  8172. (insert-before-markers "\n"))
  8173. (beginning-of-line 0)
  8174. (re-search-forward "| ?" (point-at-eol) t)
  8175. (and org-table-may-need-update (org-table-align))))
  8176. (defun org-table-insert-hline (&optional arg)
  8177. "Insert a horizontal-line below the current line into the table.
  8178. With prefix ARG, insert above the current line."
  8179. (interactive "P")
  8180. (if (not (org-at-table-p))
  8181. (error "Not at a table"))
  8182. (let ((line
  8183. (org-expand-wide-chars
  8184. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  8185. (col (current-column))
  8186. start)
  8187. (if (string-match "^[ \t]*|-" line)
  8188. (setq line
  8189. (mapcar (lambda (x) (if (member x '(?| ?+))
  8190. (prog1 (if start ?+ ?|) (setq start t))
  8191. (if start ?- ?\ )))
  8192. line))
  8193. (setq line
  8194. (mapcar (lambda (x) (if (equal x ?|)
  8195. (prog1 (if start ?+ ?|) (setq start t))
  8196. (if start ?- ?\ )))
  8197. line)))
  8198. (beginning-of-line (if arg 1 2))
  8199. (apply 'insert line)
  8200. (if (equal (char-before (point)) ?+)
  8201. (progn (backward-delete-char 1) (insert "|")))
  8202. (insert "\n")
  8203. (beginning-of-line (if arg 1 -1))
  8204. (move-to-column col)))
  8205. (defun org-expand-wide-chars (s)
  8206. "Expand wide characters to spaces."
  8207. (let (w a)
  8208. (mapconcat
  8209. (lambda (x)
  8210. (if (> (setq w (string-width (setq a (char-to-string x)))) 1)
  8211. (make-string w ?\ )
  8212. a))
  8213. s "")))
  8214. (defun org-table-kill-row ()
  8215. "Delete the current row or horizontal line from the table."
  8216. (interactive)
  8217. (if (not (org-at-table-p))
  8218. (error "Not at a table"))
  8219. (let ((col (current-column)))
  8220. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8221. (if (not (org-at-table-p)) (beginning-of-line 0))
  8222. (move-to-column col)))
  8223. (defun org-table-sort-lines (beg end numericp)
  8224. "Sort table lines in region.
  8225. Point and mark define the first and last line to include. Both point and
  8226. mark should be in the column that is used for sorting. For example, to
  8227. sort according to column 3, put the mark in the first line to sort, in
  8228. table column 3. Put point into the last line to be included in the sorting,
  8229. also in table column 3. The command will prompt for the sorting method
  8230. \(n for numerical, a for alphanumeric)."
  8231. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  8232. (setq numericp (string-match "[nN]" numericp))
  8233. (org-table-align) ;; Just to be safe
  8234. (let* (bcol ecol cmp column lns)
  8235. (goto-char beg)
  8236. (org-table-check-inside-data-field)
  8237. (setq column (org-table-current-column)
  8238. beg (move-marker (make-marker) (point-at-bol)))
  8239. (goto-char end)
  8240. (org-table-check-inside-data-field)
  8241. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  8242. (untabify beg end)
  8243. (goto-char beg)
  8244. (org-table-goto-column column)
  8245. (skip-chars-backward "^|")
  8246. (setq bcol (current-column))
  8247. (org-table-goto-column (1+ column))
  8248. (skip-chars-backward "^|")
  8249. (setq ecol (1- (current-column)))
  8250. (setq cmp (if numericp
  8251. (lambda (a b) (< (car a) (car b)))
  8252. (lambda (a b) (string< (car a) (car b)))))
  8253. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  8254. (split-string (buffer-substring beg end) "\n")))
  8255. (if numericp
  8256. (setq lns (mapcar (lambda(x)
  8257. (cons (string-to-number (car x)) (cdr x)))
  8258. lns)))
  8259. (delete-region beg end)
  8260. (move-marker beg nil)
  8261. (move-marker end nil)
  8262. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  8263. (message "%d lines sorted %s based on column %d"
  8264. (length lns)
  8265. (if numericp "numerically" "alphabetically") column)))
  8266. (defun org-table-cut-region (beg end)
  8267. "Copy region in table to the clipboard and blank all relevant fields."
  8268. (interactive "r")
  8269. (org-table-copy-region beg end 'cut))
  8270. (defun org-table-copy-region (beg end &optional cut)
  8271. "Copy rectangular region in table to clipboard.
  8272. A special clipboard is used which can only be accessed
  8273. with `org-table-paste-rectangle'."
  8274. (interactive "rP")
  8275. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8276. region cols
  8277. (rpl (if cut " " nil)))
  8278. (goto-char beg)
  8279. (org-table-check-inside-data-field)
  8280. (setq l01 (count-lines (point-min) (point))
  8281. c01 (org-table-current-column))
  8282. (goto-char end)
  8283. (org-table-check-inside-data-field)
  8284. (setq l02 (count-lines (point-min) (point))
  8285. c02 (org-table-current-column))
  8286. (setq l1 (min l01 l02) l2 (max l01 l02)
  8287. c1 (min c01 c02) c2 (max c01 c02))
  8288. (catch 'exit
  8289. (while t
  8290. (catch 'nextline
  8291. (if (> l1 l2) (throw 'exit t))
  8292. (goto-line l1)
  8293. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8294. (setq cols nil ic1 c1 ic2 c2)
  8295. (while (< ic1 (1+ ic2))
  8296. (push (org-table-get-field ic1 rpl) cols)
  8297. (setq ic1 (1+ ic1)))
  8298. (push (nreverse cols) region)
  8299. (setq l1 (1+ l1)))))
  8300. (setq org-table-clip (nreverse region))
  8301. (if cut (org-table-align))
  8302. org-table-clip))
  8303. (defun org-table-paste-rectangle ()
  8304. "Paste a rectangular region into a table.
  8305. The upper right corner ends up in the current field. All involved fields
  8306. will be overwritten. If the rectangle does not fit into the present table,
  8307. the table is enlarged as needed. The process ignores horizontal separator
  8308. lines."
  8309. (interactive)
  8310. (unless (and org-table-clip (listp org-table-clip))
  8311. (error "First cut/copy a region to paste!"))
  8312. (org-table-check-inside-data-field)
  8313. (let* ((clip org-table-clip)
  8314. (line (count-lines (point-min) (point)))
  8315. (col (org-table-current-column))
  8316. (org-enable-table-editor t)
  8317. (org-table-automatic-realign nil)
  8318. c cols field)
  8319. (while (setq cols (pop clip))
  8320. (while (org-at-table-hline-p) (beginning-of-line 2))
  8321. (if (not (org-at-table-p))
  8322. (progn (end-of-line 0) (org-table-next-field)))
  8323. (setq c col)
  8324. (while (setq field (pop cols))
  8325. (org-table-goto-column c nil 'force)
  8326. (org-table-get-field nil field)
  8327. (setq c (1+ c)))
  8328. (beginning-of-line 2))
  8329. (goto-line line)
  8330. (org-table-goto-column col)
  8331. (org-table-align)))
  8332. (defun org-table-convert ()
  8333. "Convert from `org-mode' table to table.el and back.
  8334. Obviously, this only works within limits. When an Org-mode table is
  8335. converted to table.el, all horizontal separator lines get lost, because
  8336. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8337. A table.el table can be converted to an Org-mode table only if it does not
  8338. do row or column spanning. Multiline cells will become multiple cells.
  8339. Beware, Org-mode does not test if the table can be successfully converted - it
  8340. blindly applies a recipe that works for simple tables."
  8341. (interactive)
  8342. (require 'table)
  8343. (if (org-at-table.el-p)
  8344. ;; convert to Org-mode table
  8345. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8346. (end (move-marker (make-marker) (org-table-end t))))
  8347. (table-unrecognize-region beg end)
  8348. (goto-char beg)
  8349. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8350. (replace-match ""))
  8351. (goto-char beg))
  8352. (if (org-at-table-p)
  8353. ;; convert to table.el table
  8354. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8355. (end (move-marker (make-marker) (org-table-end))))
  8356. ;; first, get rid of all horizontal lines
  8357. (goto-char beg)
  8358. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8359. (replace-match ""))
  8360. ;; insert a hline before first
  8361. (goto-char beg)
  8362. (org-table-insert-hline 'above)
  8363. (beginning-of-line -1)
  8364. ;; insert a hline after each line
  8365. (while (progn (beginning-of-line 3) (< (point) end))
  8366. (org-table-insert-hline))
  8367. (goto-char beg)
  8368. (setq end (move-marker end (org-table-end)))
  8369. ;; replace "+" at beginning and ending of hlines
  8370. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8371. (replace-match "\\1+-"))
  8372. (goto-char beg)
  8373. (while (re-search-forward "-|[ \t]*$" end t)
  8374. (replace-match "-+"))
  8375. (goto-char beg)))))
  8376. (defun org-table-wrap-region (arg)
  8377. "Wrap several fields in a column like a paragraph.
  8378. This is useful if you'd like to spread the contents of a field over several
  8379. lines, in order to keep the table compact.
  8380. If there is an active region, and both point and mark are in the same column,
  8381. the text in the column is wrapped to minimum width for the given number of
  8382. lines. Generally, this makes the table more compact. A prefix ARG may be
  8383. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8384. formats the selected text to two lines. If the region was longer than two
  8385. lines, the remaining lines remain empty. A negative prefix argument reduces
  8386. the current number of lines by that amount. The wrapped text is pasted back
  8387. into the table. If you formatted it to more lines than it was before, fields
  8388. further down in the table get overwritten - so you might need to make space in
  8389. the table first.
  8390. If there is no region, the current field is split at the cursor position and
  8391. the text fragment to the right of the cursor is prepended to the field one
  8392. line down.
  8393. If there is no region, but you specify a prefix ARG, the current field gets
  8394. blank, and the content is appended to the field above."
  8395. (interactive "P")
  8396. (org-table-check-inside-data-field)
  8397. (if (org-region-active-p)
  8398. ;; There is a region: fill as a paragraph
  8399. (let ((beg (region-beginning))
  8400. nlines)
  8401. (org-table-cut-region (region-beginning) (region-end))
  8402. (if (> (length (car org-table-clip)) 1)
  8403. (error "Region must be limited to single column"))
  8404. (setq nlines (if arg
  8405. (if (< arg 1)
  8406. (+ (length org-table-clip) arg)
  8407. arg)
  8408. (length org-table-clip)))
  8409. (setq org-table-clip
  8410. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8411. nil nlines)))
  8412. (goto-char beg)
  8413. (org-table-paste-rectangle))
  8414. ;; No region, split the current field at point
  8415. (if arg
  8416. ;; combine with field above
  8417. (let ((s (org-table-blank-field))
  8418. (col (org-table-current-column)))
  8419. (beginning-of-line 0)
  8420. (while (org-at-table-hline-p) (beginning-of-line 0))
  8421. (org-table-goto-column col)
  8422. (skip-chars-forward "^|")
  8423. (skip-chars-backward " ")
  8424. (insert " " (org-trim s))
  8425. (org-table-align))
  8426. ;; split field
  8427. (when (looking-at "\\([^|]+\\)+|")
  8428. (let ((s (match-string 1)))
  8429. (replace-match " |")
  8430. (goto-char (match-beginning 0))
  8431. (org-table-next-row)
  8432. (insert (org-trim s) " ")
  8433. (org-table-align))))))
  8434. (defun org-trim (s)
  8435. "Remove whitespace at beginning and end of string."
  8436. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  8437. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  8438. s)
  8439. (defun org-wrap (string &optional width lines)
  8440. "Wrap string to either a number of lines, or a width in characters.
  8441. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8442. that costs. If there is a word longer than WIDTH, the text is actually
  8443. wrapped to the length of that word.
  8444. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8445. many lines, whatever width that takes.
  8446. The return value is a list of lines, without newlines at the end."
  8447. (let* ((words (org-split-string string "[ \t\n]+"))
  8448. (maxword (apply 'max (mapcar 'string-width words)))
  8449. w ll)
  8450. (cond (width
  8451. (org-do-wrap words (max maxword width)))
  8452. (lines
  8453. (setq w maxword)
  8454. (setq ll (org-do-wrap words maxword))
  8455. (if (<= (length ll) lines)
  8456. ll
  8457. (setq ll words)
  8458. (while (> (length ll) lines)
  8459. (setq w (1+ w))
  8460. (setq ll (org-do-wrap words w)))
  8461. ll))
  8462. (t (error "Cannot wrap this")))))
  8463. (defun org-do-wrap (words width)
  8464. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8465. (let (lines line)
  8466. (while words
  8467. (setq line (pop words))
  8468. (while (and words (< (+ (length line) (length (car words))) width))
  8469. (setq line (concat line " " (pop words))))
  8470. (setq lines (push line lines)))
  8471. (nreverse lines)))
  8472. ;; FIXME: I think I can make this more efficient
  8473. (defun org-split-string (string &optional separators)
  8474. "Splits STRING into substrings at SEPARATORS.
  8475. No empty strings are returned if there are matches at the beginning
  8476. and end of string."
  8477. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8478. (start 0)
  8479. notfirst
  8480. (list nil))
  8481. (while (and (string-match rexp string
  8482. (if (and notfirst
  8483. (= start (match-beginning 0))
  8484. (< start (length string)))
  8485. (1+ start) start))
  8486. (< (match-beginning 0) (length string)))
  8487. (setq notfirst t)
  8488. (or (eq (match-beginning 0) 0)
  8489. (and (eq (match-beginning 0) (match-end 0))
  8490. (eq (match-beginning 0) start))
  8491. (setq list
  8492. (cons (substring string start (match-beginning 0))
  8493. list)))
  8494. (setq start (match-end 0)))
  8495. (or (eq start (length string))
  8496. (setq list
  8497. (cons (substring string start)
  8498. list)))
  8499. (nreverse list)))
  8500. (defun org-table-add-invisible-to-vertical-lines ()
  8501. "Add an `invisible' property to vertical lines of current table."
  8502. (interactive)
  8503. (let* ((beg (org-table-begin))
  8504. (end (org-table-end))
  8505. (end1))
  8506. (save-excursion
  8507. (goto-char beg)
  8508. (while (< (point) end)
  8509. (setq end1 (point-at-eol))
  8510. (if (looking-at org-table-dataline-regexp)
  8511. (while (re-search-forward "|" end1 t)
  8512. (add-text-properties (1- (point)) (point)
  8513. '(invisible org-table)))
  8514. (while (re-search-forward "[+|]" end1 t)
  8515. (add-text-properties (1- (point)) (point)
  8516. '(invisible org-table))))
  8517. (beginning-of-line 2)))))
  8518. (defun org-table-toggle-vline-visibility (&optional arg)
  8519. "Toggle the visibility of table vertical lines.
  8520. The effect is immediate and on all tables in the file.
  8521. With prefix ARG, make lines invisible when ARG is positive, make lines
  8522. visible when ARG is not positive."
  8523. (interactive "P")
  8524. (let ((action (cond
  8525. ((and arg (> (prefix-numeric-value arg) 0)) 'on)
  8526. ((and arg (< (prefix-numeric-value arg) 1)) 'off)
  8527. (t (if (org-in-invisibility-spec-p '(org-table))
  8528. 'off
  8529. 'on)))))
  8530. (if (eq action 'off)
  8531. (progn
  8532. (org-remove-from-invisibility-spec '(org-table))
  8533. (org-table-map-tables 'org-table-align)
  8534. (message "Vertical table lines visible")
  8535. (if (org-at-table-p)
  8536. (org-table-align)))
  8537. (org-add-to-invisibility-spec '(org-table))
  8538. (org-table-map-tables 'org-table-align)
  8539. (message "Vertical table lines invisible"))
  8540. (redraw-frame (selected-frame))))
  8541. (defun org-table-map-tables (function)
  8542. "Apply FUNCTION to the start of all tables in the buffer."
  8543. (save-excursion
  8544. (save-restriction
  8545. (widen)
  8546. (goto-char (point-min))
  8547. (while (re-search-forward org-table-any-line-regexp nil t)
  8548. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8549. (beginning-of-line 1)
  8550. (if (looking-at org-table-line-regexp)
  8551. (save-excursion (funcall function)))
  8552. (re-search-forward org-table-any-border-regexp nil 1)))))
  8553. (defun org-table-sum (&optional beg end nlast)
  8554. "Sum numbers in region of current table column.
  8555. The result will be displayed in the echo area, and will be available
  8556. as kill to be inserted with \\[yank].
  8557. If there is an active region, it is interpreted as a rectangle and all
  8558. numbers in that rectangle will be summed. If there is no active
  8559. region and point is located in a table column, sum all numbers in that
  8560. column.
  8561. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8562. numbers are assumed to be times as well (in decimal hours) and the
  8563. numbers are added as such.
  8564. If NLAST is a number, only the NLAST fields will actually be summed."
  8565. (interactive)
  8566. (save-excursion
  8567. (let (col (timecnt 0) diff h m s org-table-clip)
  8568. (cond
  8569. ((and beg end)) ; beg and end given explicitly
  8570. ((org-region-active-p)
  8571. (setq beg (region-beginning) end (region-end)))
  8572. (t
  8573. (setq col (org-table-current-column))
  8574. (goto-char (org-table-begin))
  8575. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8576. (error "No table data"))
  8577. (org-table-goto-column col)
  8578. ;not needed? (skip-chars-backward "^|")
  8579. (setq beg (point))
  8580. (goto-char (org-table-end))
  8581. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8582. (error "No table data"))
  8583. (org-table-goto-column col)
  8584. ;not needed? (skip-chars-forward "^|")
  8585. (setq end (point))))
  8586. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8587. (items1 (cond ((not nlast) items)
  8588. ((>= nlast (length items)) items)
  8589. (t (setq items (reverse items))
  8590. (setcdr (nthcdr (1- nlast) items) nil)
  8591. (nreverse items))))
  8592. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8593. items1)))
  8594. (res (apply '+ numbers))
  8595. (sres (if (= timecnt 0)
  8596. (format "%g" res)
  8597. (setq diff (* 3600 res)
  8598. h (floor (/ diff 3600)) diff (mod diff 3600)
  8599. m (floor (/ diff 60)) diff (mod diff 60)
  8600. s diff)
  8601. (format "%d:%02d:%02d" h m s))))
  8602. (kill-new sres)
  8603. (if (interactive-p)
  8604. (message "%s"
  8605. (substitute-command-keys
  8606. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8607. (length numbers) sres))))
  8608. sres))))
  8609. (defun org-table-get-number-for-summing (s)
  8610. (let (n)
  8611. (if (string-match "^ *|? *" s)
  8612. (setq s (replace-match "" nil nil s)))
  8613. (if (string-match " *|? *$" s)
  8614. (setq s (replace-match "" nil nil s)))
  8615. (setq n (string-to-number s))
  8616. (cond
  8617. ((and (string-match "0" s)
  8618. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8619. ((string-match "\\`[ \t]+\\'" s) nil)
  8620. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8621. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8622. (m (string-to-number (or (match-string 2 s) "0")))
  8623. (s (string-to-number (or (match-string 4 s) "0"))))
  8624. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  8625. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8626. ((equal n 0) nil)
  8627. (t n))))
  8628. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  8629. "Get a calc vector from a column, accorting to descriptor DESC.
  8630. Optional arguments TBEG and COL can give the beginning of the table and
  8631. the current column, to avoid unnecessary parsing."
  8632. (save-excursion
  8633. (or tbeg (setq tbeg (org-table-begin)))
  8634. (or col (setq col (org-table-current-column)))
  8635. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  8636. (cond
  8637. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  8638. (setq n1 (- (match-end 1) (match-beginning 1)))
  8639. (if (match-beginning 3)
  8640. (setq n2 (- (match-end 2) (match-beginning 3))))
  8641. (setq n (if n2 (max n1 n2) n1))
  8642. (setq n1 (if n2 (min n1 n2)))
  8643. (setq nn n)
  8644. (while (and (> nn 0)
  8645. (re-search-backward org-table-hline-regexp tbeg t))
  8646. (push (org-current-line) hline-list)
  8647. (setq nn (1- nn)))
  8648. (setq hline-list (nreverse hline-list))
  8649. (goto-line (nth (1- n) hline-list))
  8650. (when (re-search-forward org-table-dataline-regexp)
  8651. (org-table-goto-column col)
  8652. (setq beg (point)))
  8653. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  8654. (when (re-search-backward org-table-dataline-regexp)
  8655. (org-table-goto-column col)
  8656. (setq end (point)))
  8657. (setq l (apply 'append (org-table-copy-region beg end)))
  8658. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  8659. (if (equal x "") "0" x))
  8660. l ",") "]"))
  8661. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  8662. (setq n1 (string-to-number (match-string 1 desc))
  8663. n2 (string-to-number (match-string 2 desc)))
  8664. (beginning-of-line 1)
  8665. (save-excursion
  8666. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  8667. (org-table-goto-column col)
  8668. (setq beg (point))))
  8669. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  8670. (org-table-goto-column col)
  8671. (setq end (point)))
  8672. (setq l (apply 'append (org-table-copy-region beg end)))
  8673. (concat "[" (mapconcat
  8674. (lambda (x) (setq x (org-trim x))
  8675. (if (equal x "") "0" x))
  8676. l ",") "]"))
  8677. ((string-match "\\([0-9]+\\)" desc)
  8678. (beginning-of-line 1)
  8679. (when (re-search-backward org-table-dataline-regexp tbeg t
  8680. (string-to-number (match-string 0 desc)))
  8681. (org-table-goto-column col)
  8682. (org-trim (org-table-get-field))))))))
  8683. (defvar org-table-formula-history nil)
  8684. (defvar org-table-column-names nil
  8685. "Alist with column names, derived from the `!' line.")
  8686. (defvar org-table-column-name-regexp nil
  8687. "Regular expression matching the current column names.")
  8688. (defvar org-table-local-parameters nil
  8689. "Alist with parameter names, derived from the `$' line.")
  8690. (defvar org-table-named-field-locations nil
  8691. "Alist with locations of named fields.")
  8692. (defun org-table-get-formula (&optional equation named)
  8693. "Read a formula from the minibuffer, offer stored formula as default."
  8694. (let* ((name (car (rassoc (list (org-current-line)
  8695. (org-table-current-column))
  8696. org-table-named-field-locations)))
  8697. (scol (if named
  8698. (if name name
  8699. (error "Not in a named field"))
  8700. (int-to-string (org-table-current-column))))
  8701. (dummy (and name (not named)
  8702. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  8703. (error "Abort")))
  8704. (org-table-may-need-update nil)
  8705. (stored-list (org-table-get-stored-formulas))
  8706. (stored (cdr (assoc scol stored-list)))
  8707. (eq (cond
  8708. ((and stored equation (string-match "^ *=? *$" equation))
  8709. stored)
  8710. ((stringp equation)
  8711. equation)
  8712. (t (read-string
  8713. (format "%s formula $%s=" (if named "Field" "Column") scol)
  8714. (or stored "") 'org-table-formula-history
  8715. ;stored
  8716. ))))
  8717. mustsave)
  8718. (when (not (string-match "\\S-" eq))
  8719. ;; remove formula
  8720. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8721. (org-table-store-formulas stored-list)
  8722. (error "Formula removed"))
  8723. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8724. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8725. (if (and name (not named))
  8726. ;; We set the column equation, delete the named one.
  8727. (setq stored-list (delq (assoc name stored-list) stored-list)
  8728. mustsave t))
  8729. (if stored
  8730. (setcdr (assoc scol stored-list) eq)
  8731. (setq stored-list (cons (cons scol eq) stored-list)))
  8732. (if (or mustsave (not (equal stored eq)))
  8733. (org-table-store-formulas stored-list))
  8734. eq))
  8735. (defun org-table-store-formulas (alist)
  8736. "Store the list of formulas below the current table."
  8737. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  8738. (save-excursion
  8739. (goto-char (org-table-end))
  8740. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  8741. (delete-region (point) (match-end 0)))
  8742. (insert "#+TBLFM: "
  8743. (mapconcat (lambda (x)
  8744. (concat "$" (car x) "=" (cdr x)))
  8745. alist "::")
  8746. "\n")))
  8747. (defun org-table-get-stored-formulas ()
  8748. "Return an alist with the t=stored formulas directly after current table."
  8749. (interactive)
  8750. (let (scol eq eq-alist strings string seen)
  8751. (save-excursion
  8752. (goto-char (org-table-end))
  8753. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8754. (setq strings (org-split-string (match-string 2) " *:: *"))
  8755. (while (setq string (pop strings))
  8756. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  8757. (setq scol (match-string 1 string)
  8758. eq (match-string 2 string)
  8759. eq-alist (cons (cons scol eq) eq-alist))
  8760. (if (member scol seen)
  8761. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8762. (push scol seen))))))
  8763. (nreverse eq-alist)))
  8764. (defun org-table-modify-formulas (action &rest columns)
  8765. "Modify the formulas stored below the current table.
  8766. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  8767. expected, for the other actions only a single column number is needed."
  8768. (let ((list (org-table-get-stored-formulas))
  8769. (nmax (length (org-split-string
  8770. (buffer-substring (point-at-bol) (point-at-eol))
  8771. "|")))
  8772. col col1 col2 scol si sc1 sc2)
  8773. (cond
  8774. ((null list)) ; No action needed if there are no stored formulas
  8775. ((eq action 'remove)
  8776. (setq col (car columns)
  8777. scol (int-to-string col))
  8778. (org-table-replace-in-formulas list scol "INVALID")
  8779. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  8780. (loop for i from (1+ col) upto nmax by 1 do
  8781. (setq si (int-to-string i))
  8782. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  8783. (if (assoc si list) (setcar (assoc si list)
  8784. (int-to-string (1- i))))))
  8785. ((eq action 'insert)
  8786. (setq col (car columns))
  8787. (loop for i from nmax downto col by 1 do
  8788. (setq si (int-to-string i))
  8789. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  8790. (if (assoc si list) (setcar (assoc si list)
  8791. (int-to-string (1+ i))))))
  8792. ((eq action 'swap)
  8793. (setq col1 (car columns) col2 (nth 1 columns)
  8794. sc1 (int-to-string col1) sc2 (int-to-string col2))
  8795. ;; Hopefully, ZqZ will never be a name in a table... FIXME:
  8796. (org-table-replace-in-formulas list sc1 "ZqZ")
  8797. (org-table-replace-in-formulas list sc2 sc1)
  8798. (org-table-replace-in-formulas list "ZqZ" sc2)
  8799. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZ"))
  8800. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  8801. (if (assoc "ZqZ" list) (setcar (assoc "ZqZ" list) sc2)))
  8802. (t (error "Invalid action in `org-table-modify-formulas'")))
  8803. (if list (org-table-store-formulas list))))
  8804. (defun org-table-replace-in-formulas (list s1 s2)
  8805. (let (elt re s)
  8806. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  8807. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  8808. re (concat (regexp-quote s1) "\\>"))
  8809. (while (setq elt (pop list))
  8810. (setq s (cdr elt))
  8811. (while (string-match re s)
  8812. (setq s (replace-match s2 t t s)))
  8813. (setcdr elt s))))
  8814. (defun org-table-get-specials ()
  8815. "Get the column names and local parameters for this table."
  8816. (save-excursion
  8817. (let ((beg (org-table-begin)) (end (org-table-end))
  8818. names name fields fields1 field cnt c v line col)
  8819. (setq org-table-column-names nil
  8820. org-table-local-parameters nil
  8821. org-table-named-field-locations nil)
  8822. (goto-char beg)
  8823. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  8824. (setq names (org-split-string (match-string 1) " *| *")
  8825. cnt 1)
  8826. (while (setq name (pop names))
  8827. (setq cnt (1+ cnt))
  8828. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  8829. (push (cons name (int-to-string cnt)) org-table-column-names))))
  8830. (setq org-table-column-names (nreverse org-table-column-names))
  8831. (setq org-table-column-name-regexp
  8832. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  8833. (goto-char beg)
  8834. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  8835. (setq fields (org-split-string (match-string 1) " *| *"))
  8836. (while (setq field (pop fields))
  8837. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  8838. (push (cons (match-string 1 field) (match-string 2 field))
  8839. org-table-local-parameters))))
  8840. (goto-char beg)
  8841. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  8842. (setq c (match-string 1)
  8843. fields (org-split-string (match-string 2) " *| *"))
  8844. (save-excursion
  8845. (beginning-of-line (if (equal c "_") 2 0))
  8846. (setq line (org-current-line) col 1)
  8847. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  8848. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  8849. (while (and fields1 (setq field (pop fields)))
  8850. (setq v (pop fields1) col (1+ col))
  8851. (when (and (stringp field) (stringp v)
  8852. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  8853. (push (cons field v) org-table-local-parameters)
  8854. (push (list field line col) org-table-named-field-locations)))))))
  8855. (defun org-this-word ()
  8856. ;; Get the current word
  8857. (save-excursion
  8858. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  8859. (end (progn (skip-chars-forward "^ \t\n") (point))))
  8860. (buffer-substring-no-properties beg end))))
  8861. (defun org-table-maybe-eval-formula ()
  8862. "Check if the current field starts with \"=\" or \":=\".
  8863. If yes, store the formula and apply it."
  8864. ;; We already know we are in a table. Get field will only return a formula
  8865. ;; when appropriate. It might return a separator line, but no problem.
  8866. (when org-table-formula-evaluate-inline
  8867. (let* ((field (org-trim (or (org-table-get-field) "")))
  8868. named eq)
  8869. (when (string-match "^:?=\\(.*\\)" field)
  8870. (setq named (equal (string-to-char field) ?:)
  8871. eq (match-string 1 field))
  8872. (if (fboundp 'calc-eval)
  8873. (org-table-eval-formula (if named '(4) nil) eq))))))
  8874. (defvar org-recalc-commands nil
  8875. "List of commands triggering the recalculation of a line.
  8876. Will be filled automatically during use.")
  8877. (defvar org-recalc-marks
  8878. '((" " . "Unmarked: no special line, no automatic recalculation")
  8879. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  8880. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  8881. ("!" . "Column name definition line. Reference in formula as $name.")
  8882. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  8883. ("_" . "Names for values in row below this one.")
  8884. ("^" . "Names for values in row above this one.")))
  8885. (defun org-table-rotate-recalc-marks (&optional newchar)
  8886. "Rotate the recalculation mark in the first column.
  8887. If in any row, the first field is not consistent with a mark,
  8888. insert a new column for the markers.
  8889. When there is an active region, change all the lines in the region,
  8890. after prompting for the marking character.
  8891. After each change, a message will be displayed indicating the meaning
  8892. of the new mark."
  8893. (interactive)
  8894. (unless (org-at-table-p) (error "Not at a table"))
  8895. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  8896. (beg (org-table-begin))
  8897. (end (org-table-end))
  8898. (l (org-current-line))
  8899. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  8900. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  8901. (have-col
  8902. (save-excursion
  8903. (goto-char beg)
  8904. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  8905. (col (org-table-current-column))
  8906. (forcenew (car (assoc newchar org-recalc-marks)))
  8907. epos new)
  8908. (when l1
  8909. (message "Change region to what mark? Type # * ! $ or SPC: ")
  8910. (setq newchar (char-to-string (read-char-exclusive))
  8911. forcenew (car (assoc newchar org-recalc-marks))))
  8912. (if (and newchar (not forcenew))
  8913. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  8914. newchar))
  8915. (if l1 (goto-line l1))
  8916. (save-excursion
  8917. (beginning-of-line 1)
  8918. (unless (looking-at org-table-dataline-regexp)
  8919. (error "Not at a table data line")))
  8920. (unless have-col
  8921. (org-table-goto-column 1)
  8922. (org-table-insert-column)
  8923. (org-table-goto-column (1+ col)))
  8924. (setq epos (point-at-eol))
  8925. (save-excursion
  8926. (beginning-of-line 1)
  8927. (org-table-get-field
  8928. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  8929. (concat " "
  8930. (setq new (or forcenew
  8931. (cadr (member (match-string 1) marks))))
  8932. " ")
  8933. " # ")))
  8934. (if (and l1 l2)
  8935. (progn
  8936. (goto-line l1)
  8937. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  8938. (and (looking-at org-table-dataline-regexp)
  8939. (org-table-get-field 1 (concat " " new " "))))
  8940. (goto-line l1)))
  8941. (if (not (= epos (point-at-eol))) (org-table-align))
  8942. (goto-line l)
  8943. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  8944. (defun org-table-maybe-recalculate-line ()
  8945. "Recompute the current line if marked for it, and if we haven't just done it."
  8946. (interactive)
  8947. (and org-table-allow-automatic-line-recalculation
  8948. (not (and (memq last-command org-recalc-commands)
  8949. (equal org-last-recalc-line (org-current-line))))
  8950. (save-excursion (beginning-of-line 1)
  8951. (looking-at org-table-auto-recalculate-regexp))
  8952. (fboundp 'calc-eval)
  8953. (org-table-recalculate) t))
  8954. (defvar org-table-formula-debug nil
  8955. "Non-nil means, debug table formulas.
  8956. When nil, simply write \"#ERROR\" in corrupted fields.")
  8957. (defvar modes)
  8958. (defsubst org-set-calc-mode (var &optional value)
  8959. (if (stringp var)
  8960. (setq var (assoc var '(("D" calc-angle-mode deg)
  8961. ("R" calc-angle-mode rad)
  8962. ("F" calc-prefer-frac t)
  8963. ("S" calc-symbolic-mode t)))
  8964. value (nth 2 var) var (nth 1 var)))
  8965. (if (memq var modes)
  8966. (setcar (cdr (memq var modes)) value)
  8967. (cons var (cons value modes)))
  8968. modes)
  8969. (defun org-table-eval-formula (&optional arg equation
  8970. suppress-align suppress-const
  8971. suppress-store)
  8972. "Replace the table field value at the cursor by the result of a calculation.
  8973. This function makes use of Dave Gillespie's Calc package, in my view the
  8974. most exciting program ever written for GNU Emacs. So you need to have Calc
  8975. installed in order to use this function.
  8976. In a table, this command replaces the value in the current field with the
  8977. result of a formula. It also installs the formula as the \"current\" column
  8978. formula, by storing it in a special line below the table. When called
  8979. with a `C-u' prefix, the current field must ba a named field, and the
  8980. formula is installed as valid in only this specific field.
  8981. When called, the command first prompts for a formula, which is read in
  8982. the minibuffer. Previously entered formulas are available through the
  8983. history list, and the last used formula is offered as a default.
  8984. These stored formulas are adapted correctly when moving, inserting, or
  8985. deleting columns with the corresponding commands.
  8986. The formula can be any algebraic expression understood by the Calc package.
  8987. For details, see the Org-mode manual.
  8988. This function can also be called from Lisp programs and offers
  8989. additional arguments: EQUATION can be the formula to apply. If this
  8990. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  8991. used to speed-up recursive calls by by-passing unnecessary aligns.
  8992. SUPPRESS-CONST suppresses the interpretation of constants in the
  8993. formula, assuming that this has been done already outside the function.
  8994. SUPPRESS-STORE means the formula should not be stored, either because
  8995. it is already stored, or because it is a modified equation that should
  8996. not overwrite the stored one."
  8997. (interactive "P")
  8998. (require 'calc)
  8999. (org-table-check-inside-data-field)
  9000. (org-table-get-specials)
  9001. (let* (fields
  9002. (ndown (if (integerp arg) arg 1))
  9003. (org-table-automatic-realign nil)
  9004. (case-fold-search nil)
  9005. (down (> ndown 1))
  9006. (formula (if (and equation suppress-store)
  9007. equation
  9008. (org-table-get-formula equation (equal arg '(4)))))
  9009. (n0 (org-table-current-column))
  9010. (modes (copy-sequence org-calc-default-modes))
  9011. n form fmt x ev orig c)
  9012. ;; Parse the format string. Since we have a lot of modes, this is
  9013. ;; a lot of work. However, I think calc still uses most of the time.
  9014. (if (string-match ";" formula)
  9015. (let ((tmp (org-split-string formula ";")))
  9016. (setq formula (car tmp)
  9017. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9018. (nth 1 tmp)))
  9019. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  9020. (setq c (string-to-char (match-string 1 fmt))
  9021. n (string-to-number (or (match-string 1 fmt) "")))
  9022. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9023. (setq modes (org-set-calc-mode
  9024. 'calc-float-format
  9025. (list (cdr (assoc c '((?n . float) (?f . fix)
  9026. (?s . sci) (?e . eng))))
  9027. n))))
  9028. (setq fmt (replace-match "" t t fmt)))
  9029. (while (string-match "[DRFS]" fmt)
  9030. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9031. (setq fmt (replace-match "" t t fmt)))
  9032. (unless (string-match "\\S-" fmt)
  9033. (setq fmt nil))))
  9034. (if (and (not suppress-const) org-table-formula-use-constants)
  9035. (setq formula (org-table-formula-substitute-names formula)))
  9036. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9037. (while (> ndown 0)
  9038. (setq fields (org-split-string
  9039. (buffer-substring
  9040. (point-at-bol) (point-at-eol)) " *| *"))
  9041. (if org-table-formula-numbers-only
  9042. (setq fields (mapcar
  9043. (lambda (x) (number-to-string (string-to-number x)))
  9044. fields)))
  9045. (setq ndown (1- ndown))
  9046. (setq form (copy-sequence formula))
  9047. ;; Insert the references to fields in same row
  9048. (while (string-match "\\$\\([0-9]+\\)?" form)
  9049. (setq n (if (match-beginning 1)
  9050. (string-to-number (match-string 1 form))
  9051. n0)
  9052. x (nth (1- n) fields))
  9053. (unless x (error "Invalid field specifier \"%s\""
  9054. (match-string 0 form)))
  9055. (if (equal x "") (setq x "0"))
  9056. (setq form (replace-match (concat "(" x ")") t t form)))
  9057. ;; Insert ranges in current column
  9058. (while (string-match "\\&[-I0-9]+" form)
  9059. (setq form (replace-match
  9060. (save-match-data
  9061. (org-table-get-vertical-vector (match-string 0 form)
  9062. nil n0))
  9063. t t form)))
  9064. (setq ev (calc-eval (cons form modes)
  9065. (if org-table-formula-numbers-only 'num)))
  9066. (when org-table-formula-debug
  9067. (with-output-to-temp-buffer "*Help*"
  9068. (princ (format "Substitution history of formula
  9069. Orig: %s
  9070. $xyz-> %s
  9071. $1-> %s\n" orig formula form))
  9072. (if (listp ev)
  9073. (princ (format " %s^\nError: %s"
  9074. (make-string (car ev) ?\-) (nth 1 ev)))
  9075. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9076. ev (or fmt "NONE")
  9077. (if fmt (format fmt (string-to-number ev)) ev)))))
  9078. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  9079. (unless (and (interactive-p) (not ndown))
  9080. (unless (let (inhibit-redisplay)
  9081. (y-or-n-p "Debugging Formula. Continue to next? "))
  9082. (org-table-align)
  9083. (error "Abort"))
  9084. (delete-window (get-buffer-window "*Help*"))
  9085. (message "")))
  9086. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9087. (org-table-justify-field-maybe
  9088. (if fmt (format fmt (string-to-number ev)) ev))
  9089. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9090. (call-interactively 'org-return)
  9091. (setq ndown 0)))
  9092. (and down (org-table-maybe-recalculate-line))
  9093. (or suppress-align (and org-table-may-need-update
  9094. (org-table-align)))))
  9095. (defun org-table-recalculate (&optional all noalign)
  9096. "Recalculate the current table line by applying all stored formulas."
  9097. (interactive "P")
  9098. (or (memq this-command org-recalc-commands)
  9099. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9100. (unless (org-at-table-p) (error "Not at a table"))
  9101. (org-table-get-specials)
  9102. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9103. (lambda (a b) (string< (car a) (car b)))))
  9104. (inhibit-redisplay t)
  9105. (line-re org-table-dataline-regexp)
  9106. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9107. (thiscol (org-table-current-column))
  9108. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  9109. ;; Insert constants in all formulas
  9110. (setq eqlist
  9111. (mapcar (lambda (x)
  9112. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9113. x)
  9114. eqlist))
  9115. ;; Split the equation list
  9116. (while (setq eq (pop eqlist))
  9117. (if (<= (string-to-char (car eq)) ?9)
  9118. (push eq eqlnum)
  9119. (push eq eqlname)))
  9120. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9121. (if all
  9122. (progn
  9123. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9124. (goto-char (setq beg (org-table-begin)))
  9125. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9126. ;; This is a table with marked lines, only compute selected lines
  9127. (setq line-re org-table-recalculate-regexp)
  9128. ;; Move forward to the first non-header line
  9129. (if (and (re-search-forward org-table-dataline-regexp end t)
  9130. (re-search-forward org-table-hline-regexp end t)
  9131. (re-search-forward org-table-dataline-regexp end t))
  9132. (setq beg (match-beginning 0))
  9133. nil))) ;; just leave beg where it is
  9134. (setq beg (point-at-bol)
  9135. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9136. (goto-char beg)
  9137. (and all (message "Re-applying formulas to full table..."))
  9138. (while (re-search-forward line-re end t)
  9139. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  9140. ;; Unprotected line, recalculate
  9141. (and all (message "Re-applying formulas to full table...(line %d)"
  9142. (setq cnt (1+ cnt))))
  9143. (setq org-last-recalc-line (org-current-line))
  9144. (setq eql eqlnum)
  9145. (while (setq entry (pop eql))
  9146. (goto-line org-last-recalc-line)
  9147. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9148. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  9149. (goto-line thisline)
  9150. (org-table-goto-column thiscol)
  9151. (or noalign (and org-table-may-need-update (org-table-align))
  9152. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9153. ;; Now do the names fields
  9154. (while (setq eq (pop eqlname))
  9155. (setq name (car eq)
  9156. a (assoc name org-table-named-field-locations))
  9157. (when a
  9158. (message "Re-applying formula to named field: %s" name)
  9159. (goto-line (nth 1 a))
  9160. (org-table-goto-column (nth 2 a))
  9161. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  9162. ;; back to initial position
  9163. (goto-line thisline)
  9164. (org-table-goto-column thiscol)
  9165. (or noalign (and org-table-may-need-update (org-table-align))
  9166. (and all (message "Re-applying formulas...done")))))
  9167. (defun org-table-formula-substitute-names (f)
  9168. "Replace $const with values in string F."
  9169. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  9170. ;; First, check for column names
  9171. (while (setq start (string-match org-table-column-name-regexp f start))
  9172. (setq start (1+ start))
  9173. (setq a (assoc (match-string 1 f) org-table-column-names))
  9174. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9175. ;; Expand ranges to vectors
  9176. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  9177. (setq n1 (string-to-number (match-string 1 f))
  9178. n2 (string-to-number (match-string 2 f))
  9179. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  9180. s (concat "[($" (number-to-string (1- nn1)) ")"))
  9181. (loop for i from nn1 upto nn2 do
  9182. (setq s (concat s ",($" (int-to-string i) ")")))
  9183. (setq s (concat s "]"))
  9184. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  9185. (setq f (replace-match s t t f)))
  9186. ;; Parameters and constants
  9187. (setq start 0)
  9188. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  9189. (setq start (1+ start))
  9190. (if (setq a (save-match-data
  9191. (org-table-get-constant (match-string 1 f))))
  9192. (setq f (replace-match (concat "(" a ")") t t f))))
  9193. (if org-table-formula-debug
  9194. (put-text-property 0 (length f) :orig-formula f1 f))
  9195. f))
  9196. (defun org-table-get-constant (const)
  9197. "Find the value for a parameter or constant in a formula.
  9198. Parameters get priority."
  9199. (or (cdr (assoc const org-table-local-parameters))
  9200. (cdr (assoc const org-table-formula-constants))
  9201. (and (fboundp 'constants-get) (constants-get const))
  9202. "#UNDEFINED_NAME"))
  9203. (defvar org-edit-formulas-map (make-sparse-keymap))
  9204. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  9205. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  9206. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  9207. (defvar org-pos)
  9208. (defun org-table-edit-formulas ()
  9209. "Edit the formulas of the current table in a separate buffer."
  9210. (interactive)
  9211. (unless (org-at-table-p)
  9212. (error "Not at a table"))
  9213. (org-table-get-specials)
  9214. (let ((eql (org-table-get-stored-formulas))
  9215. (pos (move-marker (make-marker) (point)))
  9216. (wc (current-window-configuration))
  9217. entry loc s)
  9218. (switch-to-buffer-other-window "*Edit Formulas*")
  9219. (erase-buffer)
  9220. (fundamental-mode)
  9221. (set (make-local-variable 'org-pos) pos)
  9222. (set (make-local-variable 'org-window-configuration) wc)
  9223. (use-local-map org-edit-formulas-map)
  9224. (setq s "# Edit formulas and finish with `C-c C-c'.
  9225. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  9226. # Use `C-c ?' to get information about $name at point.
  9227. # To cancel editing, press `C-c C-q'.\n")
  9228. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  9229. (insert s)
  9230. (while (setq entry (pop eql))
  9231. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  9232. (setq s (format "# Named formula, referring to column %d in line %d\n"
  9233. (nth 2 loc) (nth 1 loc)))
  9234. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  9235. (insert s))
  9236. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  9237. (remove-text-properties 0 (length s) '(face nil) s)
  9238. (insert s))
  9239. (goto-char (point-min))
  9240. (message "Edit formulas and finish with `C-c C-c'.")))
  9241. (defun org-show-variable ()
  9242. "Show the location/value of the $ expression at point."
  9243. (interactive)
  9244. (let (var (pos org-pos) (win (selected-window)) e)
  9245. (save-excursion
  9246. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  9247. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  9248. (setq var (match-string 1))
  9249. (error "No variable at point")))
  9250. (cond
  9251. ((setq e (assoc var org-table-named-field-locations))
  9252. (switch-to-buffer-other-window (marker-buffer pos))
  9253. (goto-line (nth 1 e))
  9254. (org-table-goto-column (nth 2 e))
  9255. (select-window win)
  9256. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9257. ((setq e (assoc var org-table-column-names))
  9258. (switch-to-buffer-other-window (marker-buffer pos))
  9259. (goto-char pos)
  9260. (goto-char (org-table-begin))
  9261. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9262. (org-table-end) t)
  9263. (progn
  9264. (goto-char (match-beginning 1))
  9265. (message "Named column (column %s)" (cdr e)))
  9266. (error "Column name not found"))
  9267. (select-window win))
  9268. ((string-match "^[0-9]$" var)
  9269. ;; column number
  9270. (switch-to-buffer-other-window (marker-buffer pos))
  9271. (goto-char pos)
  9272. (goto-char (org-table-begin))
  9273. (recenter 1)
  9274. (if (re-search-forward org-table-dataline-regexp
  9275. (org-table-end) t)
  9276. (progn
  9277. (goto-char (match-beginning 0))
  9278. (org-table-goto-column (string-to-number var))
  9279. (message "Column %s" var))
  9280. (error "Column name not found"))
  9281. (select-window win))
  9282. ((setq e (assoc var org-table-local-parameters))
  9283. (switch-to-buffer-other-window (marker-buffer pos))
  9284. (goto-char pos)
  9285. (goto-char (org-table-begin))
  9286. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9287. (progn
  9288. (goto-char (match-beginning 1))
  9289. (message "Local parameter."))
  9290. (error "Parameter not found"))
  9291. (select-window win))
  9292. (t
  9293. (cond
  9294. ((setq e (assoc var org-table-formula-constants))
  9295. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  9296. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9297. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  9298. (t (error "Undefined name $%s" var)))))))
  9299. (defun org-finish-edit-formulas (&optional arg)
  9300. "Parse the buffer for formula definitions and install them.
  9301. With prefix ARG, apply the new formulas to the table."
  9302. (interactive "P")
  9303. (let ((pos org-pos) eql)
  9304. (goto-char (point-min))
  9305. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  9306. (push (cons (match-string 1) (match-string 2)) eql))
  9307. (set-window-configuration org-window-configuration)
  9308. (select-window (get-buffer-window (marker-buffer pos)))
  9309. (goto-char pos)
  9310. (unless (org-at-table-p)
  9311. (error "Lost table position - cannot install formulae"))
  9312. (org-table-store-formulas eql)
  9313. (move-marker pos nil)
  9314. (kill-buffer "*Edit Formulas*")
  9315. (if arg
  9316. (org-table-recalculate 'all)
  9317. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9318. (defun org-abort-edit-formulas ()
  9319. "Abort editing formulas, without installing the changes."
  9320. (interactive)
  9321. (let ((pos org-pos))
  9322. (set-window-configuration org-window-configuration)
  9323. (select-window (get-buffer-window (marker-buffer pos)))
  9324. (goto-char pos)
  9325. (message "Formula editing aborted without installing changes")))
  9326. ;;; The orgtbl minor mode
  9327. ;; Define a minor mode which can be used in other modes in order to
  9328. ;; integrate the org-mode table editor.
  9329. ;; This is really a hack, because the org-mode table editor uses several
  9330. ;; keys which normally belong to the major mode, for example the TAB and
  9331. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9332. ;; necessary to operate the table editor, but wraps the commands into a
  9333. ;; function which tests if the cursor is currently inside a table. If that
  9334. ;; is the case, the table editor command is executed. However, when any of
  9335. ;; those keys is used outside a table, the function uses `key-binding' to
  9336. ;; look up if the key has an associated command in another currently active
  9337. ;; keymap (minor modes, major mode, global), and executes that command.
  9338. ;; There might be problems if any of the keys used by the table editor is
  9339. ;; otherwise used as a prefix key.
  9340. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9341. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9342. ;; addresses this by checking explicitly for both bindings.
  9343. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9344. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9345. ;; Some modes bind other commands to simple characters, for example
  9346. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9347. ;; active, this binding is ignored inside tables and replaced with a
  9348. ;; modified self-insert.
  9349. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  9350. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  9351. In the optimized version, the table editor takes over all simple keys that
  9352. normally just insert a character. In tables, the characters are inserted
  9353. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  9354. for empty fields). Outside tables, the correct binding of the keys is
  9355. restored.
  9356. The default for this option is t if the optimized version is also used in
  9357. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  9358. this variable requires a restart of Emacs to become effective."
  9359. :group 'org-table
  9360. :type 'boolean)
  9361. (defvar orgtbl-mode nil
  9362. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9363. table editor in arbitrary modes.")
  9364. (make-variable-buffer-local 'orgtbl-mode)
  9365. (defvar orgtbl-mode-map (make-keymap)
  9366. "Keymap for `orgtbl-mode'.")
  9367. ;;;###autoload
  9368. (defun turn-on-orgtbl ()
  9369. "Unconditionally turn on `orgtbl-mode'."
  9370. (orgtbl-mode 1))
  9371. ;;;###autoload
  9372. (defun orgtbl-mode (&optional arg)
  9373. "The `org-mode' table editor as a minor mode for use in other modes."
  9374. (interactive)
  9375. (if (eq major-mode 'org-mode)
  9376. ;; Exit without error, in case some hook functions calls this
  9377. ;; by accident in org-mode.
  9378. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9379. (setq orgtbl-mode
  9380. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9381. (if orgtbl-mode
  9382. (progn
  9383. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9384. ;; Make sure we are first in minor-mode-map-alist
  9385. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9386. (and c (setq minor-mode-map-alist
  9387. (cons c (delq c minor-mode-map-alist)))))
  9388. (set (make-local-variable (quote org-table-may-need-update)) t)
  9389. (org-add-hook 'before-change-functions 'org-before-change-function
  9390. nil 'local)
  9391. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  9392. auto-fill-inhibit-regexp)
  9393. (set (make-local-variable 'auto-fill-inhibit-regexp)
  9394. (if auto-fill-inhibit-regexp
  9395. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  9396. "[ \t]*|"))
  9397. (easy-menu-add orgtbl-mode-menu)
  9398. (run-hooks 'orgtbl-mode-hook))
  9399. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9400. (remove-hook 'before-change-functions 'org-before-change-function t)
  9401. (easy-menu-remove orgtbl-mode-menu)
  9402. (force-mode-line-update 'all))))
  9403. ;; Install it as a minor mode.
  9404. (put 'orgtbl-mode :included t)
  9405. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9406. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9407. (defun orgtbl-make-binding (fun n &rest keys)
  9408. "Create a function for binding in the table minor mode.
  9409. FUN is the command to call inside a table. N is used to create a unique
  9410. command name. KEYS are keys that should be checked in for a command
  9411. to execute outside of tables."
  9412. (eval
  9413. (list 'defun
  9414. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9415. '(arg)
  9416. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9417. "Outside of tables, run the binding of `"
  9418. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9419. "'.")
  9420. '(interactive "p")
  9421. (list 'if
  9422. '(org-at-table-p)
  9423. (list 'call-interactively (list 'quote fun))
  9424. (list 'let '(orgtbl-mode)
  9425. (list 'call-interactively
  9426. (append '(or)
  9427. (mapcar (lambda (k)
  9428. (list 'key-binding k))
  9429. keys)
  9430. '('orgtbl-error))))))))
  9431. (defun orgtbl-error ()
  9432. "Error when there is no default binding for a table key."
  9433. (interactive)
  9434. (error "This key is has no function outside tables"))
  9435. (defun orgtbl-setup ()
  9436. "Setup orgtbl keymaps."
  9437. (let ((nfunc 0)
  9438. (bindings
  9439. (list
  9440. '([(meta shift left)] org-table-delete-column)
  9441. '([(meta left)] org-table-move-column-left)
  9442. '([(meta right)] org-table-move-column-right)
  9443. '([(meta shift right)] org-table-insert-column)
  9444. '([(meta shift up)] org-table-kill-row)
  9445. '([(meta shift down)] org-table-insert-row)
  9446. '([(meta up)] org-table-move-row-up)
  9447. '([(meta down)] org-table-move-row-down)
  9448. '("\C-c\C-w" org-table-cut-region)
  9449. '("\C-c\M-w" org-table-copy-region)
  9450. '("\C-c\C-y" org-table-paste-rectangle)
  9451. '("\C-c-" org-table-insert-hline)
  9452. ; '([(shift tab)] org-table-previous-field)
  9453. '("\C-m" org-table-next-row)
  9454. (list (org-key 'S-return) 'org-table-copy-down)
  9455. '([(meta return)] org-table-wrap-region)
  9456. '("\C-c\C-q" org-table-wrap-region)
  9457. '("\C-c?" org-table-current-column)
  9458. '("\C-c " org-table-blank-field)
  9459. '("\C-c+" org-table-sum)
  9460. '("\C-c|" org-table-toggle-vline-visibility)
  9461. '("\C-c=" org-table-eval-formula)
  9462. '("\C-c'" org-table-edit-formulas)
  9463. '("\C-c*" org-table-recalculate)
  9464. '("\C-c^" org-table-sort-lines)
  9465. '([(control ?#)] org-table-rotate-recalc-marks)))
  9466. elt key fun cmd)
  9467. (while (setq elt (pop bindings))
  9468. (setq nfunc (1+ nfunc))
  9469. (setq key (car elt)
  9470. fun (nth 1 elt)
  9471. cmd (orgtbl-make-binding fun nfunc key))
  9472. (define-key orgtbl-mode-map key cmd))
  9473. ;; Special treatment needed for TAB and RET
  9474. (define-key orgtbl-mode-map [(return)]
  9475. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9476. (define-key orgtbl-mode-map "\C-m"
  9477. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9478. (define-key orgtbl-mode-map [(tab)]
  9479. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9480. (define-key orgtbl-mode-map "\C-i"
  9481. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  9482. (define-key orgtbl-mode-map "\C-i"
  9483. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  9484. (define-key orgtbl-mode-map "\C-c\C-c"
  9485. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  9486. (when orgtbl-optimized
  9487. ;; If the user wants maximum table support, we need to hijack
  9488. ;; some standard editing functions
  9489. (org-remap orgtbl-mode-map
  9490. 'self-insert-command 'orgtbl-self-insert-command
  9491. 'delete-char 'orgtbl-delete-char
  9492. 'delete-backward-char 'orgtbl-delete-backward-char)
  9493. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  9494. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9495. '("OrgTbl"
  9496. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9497. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9498. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9499. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9500. "--"
  9501. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9502. ["Copy Field from Above"
  9503. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9504. "--"
  9505. ("Column"
  9506. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9507. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9508. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9509. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"])
  9510. ("Row"
  9511. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9512. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9513. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9514. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9515. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9516. "--"
  9517. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9518. ("Rectangle"
  9519. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9520. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9521. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9522. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9523. "--"
  9524. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9525. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9526. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9527. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9528. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9529. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9530. ["Sum Column/Rectangle" org-table-sum
  9531. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9532. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9533. ["Debug Formulas"
  9534. (setq org-table-formula-debug (not org-table-formula-debug))
  9535. :style toggle :selected org-table-formula-debug]
  9536. ))
  9537. t)
  9538. (defun orgtbl-tab ()
  9539. "Justification and field motion for `orgtbl-mode'."
  9540. (interactive)
  9541. (org-table-justify-field-maybe)
  9542. (org-table-next-field))
  9543. (defun orgtbl-ret ()
  9544. "Justification and field motion for `orgtbl-mode'."
  9545. (interactive)
  9546. (org-table-justify-field-maybe)
  9547. (org-table-next-row))
  9548. (defun orgtbl-self-insert-command (N)
  9549. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9550. If the cursor is in a table looking at whitespace, the whitespace is
  9551. overwritten, and the table is not marked as requiring realignment."
  9552. (interactive "p")
  9553. (if (and (org-at-table-p)
  9554. (or
  9555. (and org-table-auto-blank-field
  9556. (member last-command
  9557. '(orgtbl-hijacker-command-100
  9558. orgtbl-hijacker-command-101
  9559. orgtbl-hijacker-command-102
  9560. orgtbl-hijacker-command-103
  9561. orgtbl-hijacker-command-104
  9562. orgtbl-hijacker-command-105))
  9563. (org-table-blank-field))
  9564. t)
  9565. (eq N 1)
  9566. (looking-at "[^|\n]* +|"))
  9567. (let (org-table-may-need-update)
  9568. (goto-char (1- (match-end 0)))
  9569. (delete-backward-char 1)
  9570. (goto-char (match-beginning 0))
  9571. (self-insert-command N))
  9572. (setq org-table-may-need-update t)
  9573. (let (orgtbl-mode)
  9574. (call-interactively (key-binding (vector last-input-event))))))
  9575. (defun org-force-self-insert (N)
  9576. "Needed to enforce self-insert under remapping."
  9577. (interactive "p")
  9578. (self-insert-command N))
  9579. (defun orgtbl-delete-backward-char (N)
  9580. "Like `delete-backward-char', insert whitespace at field end in tables.
  9581. When deleting backwards, in tables this function will insert whitespace in
  9582. front of the next \"|\" separator, to keep the table aligned. The table will
  9583. still be marked for re-alignment, because a narrow field may lead to a
  9584. reduced column width."
  9585. (interactive "p")
  9586. (if (and (org-at-table-p)
  9587. (eq N 1)
  9588. (string-match "|" (buffer-substring (point-at-bol) (point)))
  9589. (looking-at ".*?|"))
  9590. (let ((pos (point)))
  9591. (backward-delete-char N)
  9592. (skip-chars-forward "^|")
  9593. (insert " ")
  9594. (goto-char (1- pos)))
  9595. (delete-backward-char N)))
  9596. (defun orgtbl-delete-char (N)
  9597. "Like `delete-char', but insert whitespace at field end in tables.
  9598. When deleting characters, in tables this function will insert whitespace in
  9599. front of the next \"|\" separator, to keep the table aligned. The table
  9600. will still be marked for re-alignment, because a narrow field may lead to
  9601. a reduced column width."
  9602. (interactive "p")
  9603. (if (and (org-at-table-p)
  9604. (not (bolp))
  9605. (not (= (char-after) ?|))
  9606. (eq N 1))
  9607. (if (looking-at ".*?|")
  9608. (let ((pos (point)))
  9609. (replace-match (concat
  9610. (substring (match-string 0) 1 -1)
  9611. " |"))
  9612. (goto-char pos)))
  9613. (delete-char N)))
  9614. ;;; Exporting
  9615. (defconst org-level-max 20)
  9616. (defun org-export-find-first-heading-line (list)
  9617. "Remove all lines from LIST which are before the first headline."
  9618. (let ((orig-list list)
  9619. (re (concat "^" outline-regexp)))
  9620. (while (and list
  9621. (not (string-match re (car list))))
  9622. (pop list))
  9623. (or list orig-list)))
  9624. (defun org-skip-comments (lines)
  9625. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  9626. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  9627. (re2 "^\\(\\*+\\)[ \t\n\r]")
  9628. rtn line level)
  9629. (while (setq line (pop lines))
  9630. (cond
  9631. ((and (string-match re1 line)
  9632. (setq level (- (match-end 1) (match-beginning 1))))
  9633. ;; Beginning of a COMMENT subtree. Skip it.
  9634. (while (and (setq line (pop lines))
  9635. (or (not (string-match re2 line))
  9636. (> (- (match-end 1) (match-beginning 1)) level))))
  9637. (setq lines (cons line lines)))
  9638. ((string-match "^#" line)
  9639. ;; an ordinary comment line
  9640. )
  9641. ((and org-export-table-remove-special-lines
  9642. (string-match "^[ \t]*| *[!_^] *|" line))
  9643. ;; a special table line that should be removed
  9644. )
  9645. (t (setq rtn (cons line rtn)))))
  9646. (nreverse rtn)))
  9647. ;; ASCII
  9648. (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  9649. "Characters for underlining headings in ASCII export.")
  9650. (defconst org-html-entities
  9651. '(("nbsp")
  9652. ("iexcl")
  9653. ("cent")
  9654. ("pound")
  9655. ("curren")
  9656. ("yen")
  9657. ("brvbar")
  9658. ("sect")
  9659. ("uml")
  9660. ("copy")
  9661. ("ordf")
  9662. ("laquo")
  9663. ("not")
  9664. ("shy")
  9665. ("reg")
  9666. ("macr")
  9667. ("deg")
  9668. ("plusmn")
  9669. ("sup2")
  9670. ("sup3")
  9671. ("acute")
  9672. ("micro")
  9673. ("para")
  9674. ("middot")
  9675. ("odot"."o")
  9676. ("star"."*")
  9677. ("cedil")
  9678. ("sup1")
  9679. ("ordm")
  9680. ("raquo")
  9681. ("frac14")
  9682. ("frac12")
  9683. ("frac34")
  9684. ("iquest")
  9685. ("Agrave")
  9686. ("Aacute")
  9687. ("Acirc")
  9688. ("Atilde")
  9689. ("Auml")
  9690. ("Aring") ("AA"."&Aring;")
  9691. ("AElig")
  9692. ("Ccedil")
  9693. ("Egrave")
  9694. ("Eacute")
  9695. ("Ecirc")
  9696. ("Euml")
  9697. ("Igrave")
  9698. ("Iacute")
  9699. ("Icirc")
  9700. ("Iuml")
  9701. ("ETH")
  9702. ("Ntilde")
  9703. ("Ograve")
  9704. ("Oacute")
  9705. ("Ocirc")
  9706. ("Otilde")
  9707. ("Ouml")
  9708. ("times")
  9709. ("Oslash")
  9710. ("Ugrave")
  9711. ("Uacute")
  9712. ("Ucirc")
  9713. ("Uuml")
  9714. ("Yacute")
  9715. ("THORN")
  9716. ("szlig")
  9717. ("agrave")
  9718. ("aacute")
  9719. ("acirc")
  9720. ("atilde")
  9721. ("auml")
  9722. ("aring")
  9723. ("aelig")
  9724. ("ccedil")
  9725. ("egrave")
  9726. ("eacute")
  9727. ("ecirc")
  9728. ("euml")
  9729. ("igrave")
  9730. ("iacute")
  9731. ("icirc")
  9732. ("iuml")
  9733. ("eth")
  9734. ("ntilde")
  9735. ("ograve")
  9736. ("oacute")
  9737. ("ocirc")
  9738. ("otilde")
  9739. ("ouml")
  9740. ("divide")
  9741. ("oslash")
  9742. ("ugrave")
  9743. ("uacute")
  9744. ("ucirc")
  9745. ("uuml")
  9746. ("yacute")
  9747. ("thorn")
  9748. ("yuml")
  9749. ("fnof")
  9750. ("Alpha")
  9751. ("Beta")
  9752. ("Gamma")
  9753. ("Delta")
  9754. ("Epsilon")
  9755. ("Zeta")
  9756. ("Eta")
  9757. ("Theta")
  9758. ("Iota")
  9759. ("Kappa")
  9760. ("Lambda")
  9761. ("Mu")
  9762. ("Nu")
  9763. ("Xi")
  9764. ("Omicron")
  9765. ("Pi")
  9766. ("Rho")
  9767. ("Sigma")
  9768. ("Tau")
  9769. ("Upsilon")
  9770. ("Phi")
  9771. ("Chi")
  9772. ("Psi")
  9773. ("Omega")
  9774. ("alpha")
  9775. ("beta")
  9776. ("gamma")
  9777. ("delta")
  9778. ("epsilon")
  9779. ("varepsilon"."&epsilon;")
  9780. ("zeta")
  9781. ("eta")
  9782. ("theta")
  9783. ("iota")
  9784. ("kappa")
  9785. ("lambda")
  9786. ("mu")
  9787. ("nu")
  9788. ("xi")
  9789. ("omicron")
  9790. ("pi")
  9791. ("rho")
  9792. ("sigmaf") ("varsigma"."&sigmaf;")
  9793. ("sigma")
  9794. ("tau")
  9795. ("upsilon")
  9796. ("phi")
  9797. ("chi")
  9798. ("psi")
  9799. ("omega")
  9800. ("thetasym") ("vartheta"."&thetasym;")
  9801. ("upsih")
  9802. ("piv")
  9803. ("bull") ("bullet"."&bull;")
  9804. ("hellip") ("dots"."&hellip;")
  9805. ("prime")
  9806. ("Prime")
  9807. ("oline")
  9808. ("frasl")
  9809. ("weierp")
  9810. ("image")
  9811. ("real")
  9812. ("trade")
  9813. ("alefsym")
  9814. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  9815. ("uarr") ("uparrow"."&uarr;")
  9816. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  9817. ("darr")("downarrow"."&darr;")
  9818. ("harr") ("leftrightarrow"."&harr;")
  9819. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  9820. ("lArr") ("Leftarrow"."&lArr;")
  9821. ("uArr") ("Uparrow"."&uArr;")
  9822. ("rArr") ("Rightarrow"."&rArr;")
  9823. ("dArr") ("Downarrow"."&dArr;")
  9824. ("hArr") ("Leftrightarrow"."&hArr;")
  9825. ("forall")
  9826. ("part") ("partial"."&part;")
  9827. ("exist") ("exists"."&exist;")
  9828. ("empty") ("emptyset"."&empty;")
  9829. ("nabla")
  9830. ("isin") ("in"."&isin;")
  9831. ("notin")
  9832. ("ni")
  9833. ("prod")
  9834. ("sum")
  9835. ("minus")
  9836. ("lowast") ("ast"."&lowast;")
  9837. ("radic")
  9838. ("prop") ("proptp"."&prop;")
  9839. ("infin") ("infty"."&infin;")
  9840. ("ang") ("angle"."&ang;")
  9841. ("and") ("vee"."&and;")
  9842. ("or") ("wedge"."&or;")
  9843. ("cap")
  9844. ("cup")
  9845. ("int")
  9846. ("there4")
  9847. ("sim")
  9848. ("cong") ("simeq"."&cong;")
  9849. ("asymp")("approx"."&asymp;")
  9850. ("ne") ("neq"."&ne;")
  9851. ("equiv")
  9852. ("le")
  9853. ("ge")
  9854. ("sub") ("subset"."&sub;")
  9855. ("sup") ("supset"."&sup;")
  9856. ("nsub")
  9857. ("sube")
  9858. ("supe")
  9859. ("oplus")
  9860. ("otimes")
  9861. ("perp")
  9862. ("sdot") ("cdot"."&sdot;")
  9863. ("lceil")
  9864. ("rceil")
  9865. ("lfloor")
  9866. ("rfloor")
  9867. ("lang")
  9868. ("rang")
  9869. ("loz") ("Diamond"."&loz;")
  9870. ("spades") ("spadesuit"."&spades;")
  9871. ("clubs") ("clubsuit"."&clubs;")
  9872. ("hearts") ("diamondsuit"."&hearts;")
  9873. ("diams") ("diamondsuit"."&diams;")
  9874. ("quot")
  9875. ("amp")
  9876. ("lt")
  9877. ("gt")
  9878. ("OElig")
  9879. ("oelig")
  9880. ("Scaron")
  9881. ("scaron")
  9882. ("Yuml")
  9883. ("circ")
  9884. ("tilde")
  9885. ("ensp")
  9886. ("emsp")
  9887. ("thinsp")
  9888. ("zwnj")
  9889. ("zwj")
  9890. ("lrm")
  9891. ("rlm")
  9892. ("ndash")
  9893. ("mdash")
  9894. ("lsquo")
  9895. ("rsquo")
  9896. ("sbquo")
  9897. ("ldquo")
  9898. ("rdquo")
  9899. ("bdquo")
  9900. ("dagger")
  9901. ("Dagger")
  9902. ("permil")
  9903. ("lsaquo")
  9904. ("rsaquo")
  9905. ("euro")
  9906. ("arccos"."arccos")
  9907. ("arcsin"."arcsin")
  9908. ("arctan"."arctan")
  9909. ("arg"."arg")
  9910. ("cos"."cos")
  9911. ("cosh"."cosh")
  9912. ("cot"."cot")
  9913. ("coth"."coth")
  9914. ("csc"."csc")
  9915. ("deg"."deg")
  9916. ("det"."det")
  9917. ("dim"."dim")
  9918. ("exp"."exp")
  9919. ("gcd"."gcd")
  9920. ("hom"."hom")
  9921. ("inf"."inf")
  9922. ("ker"."ker")
  9923. ("lg"."lg")
  9924. ("lim"."lim")
  9925. ("liminf"."liminf")
  9926. ("limsup"."limsup")
  9927. ("ln"."ln")
  9928. ("log"."log")
  9929. ("max"."max")
  9930. ("min"."min")
  9931. ("Pr"."Pr")
  9932. ("sec"."sec")
  9933. ("sin"."sin")
  9934. ("sinh"."sinh")
  9935. ("sup"."sup")
  9936. ("tan"."tan")
  9937. ("tanh"."tanh")
  9938. )
  9939. "Entities for TeX->HTML translation.
  9940. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  9941. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  9942. In that case, \"\\ent\" will be translated to \"&other;\".
  9943. The list contains HTML entities for Latin-1, Greek and other symbols.
  9944. It is supplemented by a number of commonly used TeX macros with appropriate
  9945. translations. There is currently no way for users to extend this.")
  9946. (defun org-cleaned-string-for-export (string)
  9947. "Cleanup a buffer substring so that links can be created safely."
  9948. (interactive)
  9949. (let* ((cb (current-buffer))
  9950. (re-radio (and org-target-link-regexp
  9951. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  9952. rtn)
  9953. (save-excursion
  9954. (set-buffer (get-buffer-create " org-mode-tmp"))
  9955. (erase-buffer)
  9956. (insert string)
  9957. (org-mode)
  9958. ;; Find targets in comments and move them out of comments,
  9959. ;; but mark them as targets that should be invisible
  9960. (goto-char (point-min))
  9961. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  9962. (replace-match "\\1(INVISIBLE)"))
  9963. ;; Find matches for radio targets and turn them into links
  9964. (goto-char (point-min))
  9965. (when re-radio
  9966. (while (re-search-forward re-radio nil t)
  9967. (replace-match "\\1[[\\2]]")))
  9968. ;; Find all links that contain a newline and put them into a single line
  9969. (goto-char (point-min))
  9970. (while (re-search-forward "\\(\\[\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\]\\)" nil t)
  9971. (replace-match "\\1 \\2")
  9972. (goto-char (match-beginning 0)))
  9973. ;; Remove comments
  9974. (goto-char (point-min))
  9975. (while (re-search-forward "^#.*\n?" nil t)
  9976. (replace-match ""))
  9977. (setq rtn (buffer-string)))
  9978. (kill-buffer " org-mode-tmp")
  9979. rtn))
  9980. (defun org-solidify-link-text (s &optional alist)
  9981. "Take link text and make a safe target out of it."
  9982. (save-match-data
  9983. (let* ((rtn
  9984. (mapconcat
  9985. 'identity
  9986. (org-split-string s "[ \t\r\n]+") "--"))
  9987. (a (assoc rtn alist)))
  9988. (or (cdr a) rtn))))
  9989. (defun org-convert-to-odd-levels ()
  9990. "Convert an org-mode file with all levels allowed to one with odd levels.
  9991. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  9992. level 5 etc."
  9993. (interactive)
  9994. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  9995. (let ((org-odd-levels-only nil) n)
  9996. (save-excursion
  9997. (goto-char (point-min))
  9998. (while (re-search-forward "^\\*\\*+" nil t)
  9999. (setq n (1- (length (match-string 0))))
  10000. (while (>= (setq n (1- n)) 0)
  10001. (org-demote))
  10002. (end-of-line 1))))))
  10003. (defun org-convert-to-oddeven-levels ()
  10004. "Convert an org-mode file with only odd levels to one with odd and even levels.
  10005. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  10006. section with an even level, conversion would destroy the structure of the file. An error
  10007. is signaled in this case."
  10008. (interactive)
  10009. (goto-char (point-min))
  10010. ;; First check if there are no even levels
  10011. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  10012. (org-show-hierarchy-above)
  10013. (error "Not all levels are odd in this file. Conversion not possible."))
  10014. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  10015. (let ((org-odd-levels-only nil) n)
  10016. (save-excursion
  10017. (goto-char (point-min))
  10018. (while (re-search-forward "^\\*\\*+" nil t)
  10019. (setq n (/ (length (match-string 0)) 2))
  10020. (while (>= (setq n (1- n)) 0)
  10021. (org-promote))
  10022. (end-of-line 1))))))
  10023. (defun org-tr-level (n)
  10024. "Make N odd if required."
  10025. (if org-odd-levels-only (1+ (/ n 2)) n))
  10026. (defvar org-last-level nil) ; dynamically scoped variable
  10027. (defun org-export-as-ascii (arg)
  10028. "Export the outline as a pretty ASCII file.
  10029. If there is an active region, export only the region.
  10030. The prefix ARG specifies how many levels of the outline should become
  10031. underlined headlines. The default is 3."
  10032. (interactive "P")
  10033. (setq-default org-todo-line-regexp org-todo-line-regexp)
  10034. (let* ((region
  10035. (buffer-substring
  10036. (if (org-region-active-p) (region-beginning) (point-min))
  10037. (if (org-region-active-p) (region-end) (point-max))))
  10038. (lines (org-export-find-first-heading-line
  10039. (org-skip-comments
  10040. (org-split-string
  10041. (org-cleaned-string-for-export region)
  10042. "[\r\n]"))))
  10043. (org-startup-with-deadline-check nil)
  10044. (level 0) line txt
  10045. (umax nil)
  10046. (case-fold-search nil)
  10047. (filename (concat (file-name-sans-extension buffer-file-name)
  10048. ".txt"))
  10049. (buffer (find-file-noselect filename))
  10050. (levels-open (make-vector org-level-max nil))
  10051. (date (format-time-string "%Y/%m/%d" (current-time)))
  10052. (time (format-time-string "%X" (org-current-time)))
  10053. (author user-full-name)
  10054. (title (buffer-name))
  10055. (options nil)
  10056. (email user-mail-address)
  10057. (language org-export-default-language)
  10058. (text nil)
  10059. (todo nil)
  10060. (lang-words nil))
  10061. (setq org-last-level 1)
  10062. (org-init-section-numbers)
  10063. (find-file-noselect filename)
  10064. ;; Search for the export key lines
  10065. (org-parse-key-lines)
  10066. (setq lang-words (or (assoc language org-export-language-setup)
  10067. (assoc "en" org-export-language-setup)))
  10068. (if org-export-ascii-show-new-buffer
  10069. (switch-to-buffer-other-window buffer)
  10070. (set-buffer buffer))
  10071. (erase-buffer)
  10072. (fundamental-mode)
  10073. (if options (org-parse-export-options options))
  10074. (setq umax (if arg (prefix-numeric-value arg)
  10075. org-export-headline-levels))
  10076. ;; File header
  10077. (if title (org-insert-centered title ?=))
  10078. (insert "\n")
  10079. (if (or author email)
  10080. (insert (concat (nth 1 lang-words) ": " (or author "")
  10081. (if email (concat " <" email ">") "")
  10082. "\n")))
  10083. (if (and date time)
  10084. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  10085. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  10086. (insert "\n\n")
  10087. (if org-export-with-toc
  10088. (progn
  10089. (insert (nth 3 lang-words) "\n"
  10090. (make-string (length (nth 3 lang-words)) ?=) "\n")
  10091. (mapcar '(lambda (line)
  10092. (if (string-match org-todo-line-regexp
  10093. line)
  10094. ;; This is a headline
  10095. (progn
  10096. (setq level (- (match-end 1) (match-beginning 1))
  10097. level (org-tr-level level)
  10098. txt (match-string 3 line)
  10099. todo
  10100. (or (and (match-beginning 2)
  10101. (not (equal (match-string 2 line)
  10102. org-done-string)))
  10103. ; TODO, not DONE
  10104. (and (= level umax)
  10105. (org-search-todo-below
  10106. line lines level))))
  10107. (setq txt (org-html-expand-for-ascii txt))
  10108. (if org-export-with-section-numbers
  10109. (setq txt (concat (org-section-number level)
  10110. " " txt)))
  10111. (if (<= level umax)
  10112. (progn
  10113. (insert
  10114. (make-string (* (1- level) 4) ?\ )
  10115. (format (if todo "%s (*)\n" "%s\n") txt))
  10116. (setq org-last-level level))
  10117. ))))
  10118. lines)))
  10119. (org-init-section-numbers)
  10120. (while (setq line (pop lines))
  10121. ;; Remove the quoted HTML tags.
  10122. (setq line (org-html-expand-for-ascii line))
  10123. ;; Remove targets
  10124. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  10125. (setq line (replace-match "" t t line)))
  10126. ;; Replace internal links
  10127. (while (string-match org-bracket-link-regexp line)
  10128. (setq line (replace-match
  10129. (if (match-end 3) "[\\3]" "[\\1]")
  10130. t nil line)))
  10131. (cond
  10132. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  10133. ;; a Headline
  10134. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  10135. txt (match-string 2 line))
  10136. (org-ascii-level-start level txt umax))
  10137. (t (insert line "\n"))))
  10138. (normal-mode)
  10139. (save-buffer)
  10140. (goto-char (point-min))))
  10141. (defun org-search-todo-below (line lines level)
  10142. "Search the subtree below LINE for any TODO entries."
  10143. (let ((rest (cdr (memq line lines)))
  10144. (re org-todo-line-regexp)
  10145. line lv todo)
  10146. (catch 'exit
  10147. (while (setq line (pop rest))
  10148. (if (string-match re line)
  10149. (progn
  10150. (setq lv (- (match-end 1) (match-beginning 1))
  10151. todo (and (match-beginning 2)
  10152. (not (equal (match-string 2 line)
  10153. org-done-string))))
  10154. ; TODO, not DONE
  10155. (if (<= lv level) (throw 'exit nil))
  10156. (if todo (throw 'exit t))))))))
  10157. ;; FIXME: Try to handle <b> and <i> as faces via text properties.
  10158. ;; FIXME: Can I implement *bold*,/italic/ and _underline_ for ASCII export?
  10159. (defun org-html-expand-for-ascii (line)
  10160. "Handle quoted HTML for ASCII export."
  10161. (if org-export-html-expand
  10162. (while (string-match "@<[^<>\n]*>" line)
  10163. ;; We just remove the tags for now.
  10164. (setq line (replace-match "" nil nil line))))
  10165. line)
  10166. (defun org-insert-centered (s &optional underline)
  10167. "Insert the string S centered and underline it with character UNDERLINE."
  10168. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  10169. (insert (make-string ind ?\ ) s "\n")
  10170. (if underline
  10171. (insert (make-string ind ?\ )
  10172. (make-string (string-width s) underline)
  10173. "\n"))))
  10174. (defun org-ascii-level-start (level title umax)
  10175. "Insert a new level in ASCII export."
  10176. (let (char)
  10177. (if (> level umax)
  10178. (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
  10179. (if (or (not (equal (char-before) ?\n))
  10180. (not (equal (char-before (1- (point))) ?\n)))
  10181. (insert "\n"))
  10182. (setq char (nth (- umax level) (reverse org-ascii-underline)))
  10183. (if org-export-with-section-numbers
  10184. (setq title (concat (org-section-number level) " " title)))
  10185. (insert title "\n" (make-string (string-width title) char) "\n"))))
  10186. (defun org-export-copy-visible ()
  10187. "Copy the visible part of the buffer to another buffer, for printing.
  10188. Also removes the first line of the buffer if it specifies a mode,
  10189. and all options lines."
  10190. (interactive)
  10191. (let* ((filename (concat (file-name-sans-extension buffer-file-name)
  10192. ".txt"))
  10193. (buffer (find-file-noselect filename))
  10194. (ore (concat
  10195. (org-make-options-regexp
  10196. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  10197. "STARTUP" "ARCHIVE"
  10198. "TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"))
  10199. (if org-noutline-p "\\(\n\\|$\\)" "")))
  10200. s e)
  10201. (with-current-buffer buffer
  10202. (erase-buffer)
  10203. (text-mode))
  10204. (save-excursion
  10205. (setq s (goto-char (point-min)))
  10206. (while (not (= (point) (point-max)))
  10207. (goto-char (org-find-invisible))
  10208. (append-to-buffer buffer s (point))
  10209. (setq s (goto-char (org-find-visible)))))
  10210. (switch-to-buffer-other-window buffer)
  10211. (newline)
  10212. (goto-char (point-min))
  10213. (if (looking-at ".*-\\*- mode:.*\n")
  10214. (replace-match ""))
  10215. (while (re-search-forward ore nil t)
  10216. (replace-match ""))
  10217. (goto-char (point-min))))
  10218. (defun org-find-visible ()
  10219. (if (featurep 'noutline)
  10220. (let ((s (point)))
  10221. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  10222. (get-char-property s 'invisible)))
  10223. s)
  10224. (skip-chars-forward "^\n")
  10225. (point)))
  10226. (defun org-find-invisible ()
  10227. (if (featurep 'noutline)
  10228. (let ((s (point)))
  10229. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  10230. (not (get-char-property s 'invisible))))
  10231. s)
  10232. (skip-chars-forward "^\r")
  10233. (point)))
  10234. ;; HTML
  10235. (defun org-get-current-options ()
  10236. "Return a string with current options as keyword options.
  10237. Does include HTML export options as well as TODO and CATEGORY stuff."
  10238. (format
  10239. "#+TITLE: %s
  10240. #+AUTHOR: %s
  10241. #+EMAIL: %s
  10242. #+LANGUAGE: %s
  10243. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  10244. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  10245. #+CATEGORY: %s
  10246. #+SEQ_TODO: %s
  10247. #+TYP_TODO: %s
  10248. #+STARTUP: %s %s %s %s
  10249. #+ARCHIVE: %s
  10250. "
  10251. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  10252. org-export-headline-levels
  10253. org-export-with-section-numbers
  10254. org-export-with-toc
  10255. org-export-preserve-breaks
  10256. org-export-html-expand
  10257. org-export-with-fixed-width
  10258. org-export-with-tables
  10259. org-export-with-sub-superscripts
  10260. org-export-with-emphasize
  10261. org-export-with-TeX-macros
  10262. (file-name-nondirectory buffer-file-name)
  10263. (if (equal org-todo-interpretation 'sequence)
  10264. (mapconcat 'identity org-todo-keywords " ")
  10265. "TODO FEEDBACK VERIFY DONE")
  10266. (if (equal org-todo-interpretation 'type)
  10267. (mapconcat 'identity org-todo-keywords " ")
  10268. "Me Jason Marie DONE")
  10269. (cdr (assoc org-startup-folded
  10270. '((nil . "nofold")(t . "fold")(content . "content"))))
  10271. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  10272. (if org-odd-levels-only "odd" "oddeven")
  10273. (if org-hide-leading-stars "hidestars" "showstars")
  10274. org-archive-location
  10275. ))
  10276. (defun org-insert-export-options-template ()
  10277. "Insert into the buffer a template with information for exporting."
  10278. (interactive)
  10279. (if (not (bolp)) (newline))
  10280. (let ((s (org-get-current-options)))
  10281. (and (string-match "#\\+CATEGORY" s)
  10282. (setq s (substring s 0 (match-beginning 0))))
  10283. (insert s)))
  10284. (defun org-toggle-fixed-width-section (arg)
  10285. "Toggle the fixed-width export.
  10286. If there is no active region, the QUOTE keyword at the current headline is
  10287. inserted or removed. When present, it causes the text between this headline
  10288. and the next to be exported as fixed-width text, and unmodified.
  10289. If there is an active region, this command adds or removes a colon as the
  10290. first character of this line. If the first character of a line is a colon,
  10291. this line is also exported in fixed-width font."
  10292. (interactive "P")
  10293. (let* ((cc 0)
  10294. (regionp (org-region-active-p))
  10295. (beg (if regionp (region-beginning) (point)))
  10296. (end (if regionp (region-end)))
  10297. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  10298. (re "[ \t]*\\(:\\)")
  10299. off)
  10300. (if regionp
  10301. (save-excursion
  10302. (goto-char beg)
  10303. (setq cc (current-column))
  10304. (beginning-of-line 1)
  10305. (setq off (looking-at re))
  10306. (while (> nlines 0)
  10307. (setq nlines (1- nlines))
  10308. (beginning-of-line 1)
  10309. (cond
  10310. (arg
  10311. (move-to-column cc t)
  10312. (insert ":\n")
  10313. (forward-line -1))
  10314. ((and off (looking-at re))
  10315. (replace-match "" t t nil 1))
  10316. ((not off) (move-to-column cc t) (insert ":")))
  10317. (forward-line 1)))
  10318. (save-excursion
  10319. (org-back-to-heading)
  10320. (if (looking-at (concat outline-regexp
  10321. "\\( +\\<" org-quote-string "\\>\\)"))
  10322. (replace-match "" t t nil 1)
  10323. (if (looking-at outline-regexp)
  10324. (progn
  10325. (goto-char (match-end 0))
  10326. (insert " " org-quote-string))))))))
  10327. (defun org-export-as-html-and-open (arg)
  10328. "Export the outline as HTML and immediately open it with a browser.
  10329. If there is an active region, export only the region.
  10330. The prefix ARG specifies how many levels of the outline should become
  10331. headlines. The default is 3. Lower levels will become bulleted lists."
  10332. (interactive "P")
  10333. (org-export-as-html arg 'hidden)
  10334. (org-open-file buffer-file-name))
  10335. (defun org-export-as-html-batch ()
  10336. "Call `org-export-as-html', may be used in batch processing as
  10337. emacs --batch
  10338. --load=$HOME/lib/emacs/org.el
  10339. --eval \"(setq org-export-headline-levels 2)\"
  10340. --visit=MyFile --funcall org-export-as-html-batch"
  10341. (org-export-as-html org-export-headline-levels 'hidden))
  10342. (defun org-export-as-html (arg &optional hidden)
  10343. "Export the outline as a pretty HTML file.
  10344. If there is an active region, export only the region.
  10345. The prefix ARG specifies how many levels of the outline should become
  10346. headlines. The default is 3. Lower levels will become bulleted lists."
  10347. (interactive "P")
  10348. (setq-default org-todo-line-regexp org-todo-line-regexp)
  10349. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  10350. (setq-default org-done-string org-done-string)
  10351. (let* ((style org-export-html-style)
  10352. (odd org-odd-levels-only)
  10353. (region-p (org-region-active-p))
  10354. (region
  10355. (buffer-substring
  10356. (if region-p (region-beginning) (point-min))
  10357. (if region-p (region-end) (point-max))))
  10358. (all_lines
  10359. (org-skip-comments (org-split-string
  10360. (org-cleaned-string-for-export region)
  10361. "[\r\n]")))
  10362. (lines (org-export-find-first-heading-line all_lines))
  10363. (level 0) (line "") (origline "") txt todo
  10364. (umax nil)
  10365. (filename (concat (file-name-sans-extension buffer-file-name)
  10366. ".html"))
  10367. (buffer (find-file-noselect filename))
  10368. (levels-open (make-vector org-level-max nil))
  10369. (date (format-time-string "%Y/%m/%d" (current-time)))
  10370. (time (format-time-string "%X" (org-current-time)))
  10371. (author user-full-name)
  10372. (title (buffer-name))
  10373. (options nil)
  10374. (quote-re (concat "^\\*+[ \t]*" org-quote-string "\\>"))
  10375. (inquote nil)
  10376. (infixed nil)
  10377. (in-local-list nil)
  10378. (local-list-num nil)
  10379. (local-list-indent nil)
  10380. (llt org-plain-list-ordered-item-terminator)
  10381. (email user-mail-address)
  10382. (language org-export-default-language)
  10383. (text nil)
  10384. (lang-words nil)
  10385. (target-alist nil) tg
  10386. (head-count 0) cnt
  10387. (start 0)
  10388. ;; FIXME: The following returns always nil under XEmacs
  10389. (coding-system (and (fboundp 'coding-system-get)
  10390. (boundp 'buffer-file-coding-system)
  10391. buffer-file-coding-system))
  10392. (coding-system-for-write (or coding-system coding-system-for-write))
  10393. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  10394. (charset (and coding-system
  10395. (coding-system-get coding-system 'mime-charset)))
  10396. table-open type
  10397. table-buffer table-orig-buffer
  10398. ind start-is-num starter
  10399. )
  10400. (message "Exporting...")
  10401. (setq org-last-level 1)
  10402. (org-init-section-numbers)
  10403. ;; Search for the export key lines
  10404. (org-parse-key-lines)
  10405. (setq lang-words (or (assoc language org-export-language-setup)
  10406. (assoc "en" org-export-language-setup)))
  10407. ;; Switch to the output buffer
  10408. (if (or hidden (not org-export-html-show-new-buffer))
  10409. (set-buffer buffer)
  10410. (switch-to-buffer-other-window buffer))
  10411. (erase-buffer)
  10412. (fundamental-mode)
  10413. (let ((case-fold-search nil)
  10414. (org-odd-levels-only odd))
  10415. (if options (org-parse-export-options options))
  10416. (setq umax (if arg (prefix-numeric-value arg)
  10417. org-export-headline-levels))
  10418. ;; File header
  10419. (insert (format
  10420. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
  10421. \"http://www.w3.org/TR/REC-html40/loose.dtd\">
  10422. <html lang=\"%s\"><head>
  10423. <title>%s</title>
  10424. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
  10425. <meta name=generator content=\"Org-mode\">
  10426. <meta name=generated content=\"%s %s\">
  10427. <meta name=author content=\"%s\">
  10428. %s
  10429. </head><body>
  10430. "
  10431. language (org-html-expand title) (or charset "iso-8859-1")
  10432. date time author style))
  10433. (if title (insert (concat "<H1 class=\"title\">"
  10434. (org-html-expand title) "</H1>\n")))
  10435. (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  10436. (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  10437. email "&gt;</a>\n")))
  10438. (if (or author email) (insert "<br>\n"))
  10439. (if (and date time) (insert (concat (nth 2 lang-words) ": "
  10440. date " " time "<br>\n")))
  10441. (if text (insert (concat "<p>\n" (org-html-expand text))))
  10442. (if org-export-with-toc
  10443. (progn
  10444. (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
  10445. (insert "<ul>\n")
  10446. (setq lines
  10447. (mapcar '(lambda (line)
  10448. (if (string-match org-todo-line-regexp line)
  10449. ;; This is a headline
  10450. (progn
  10451. (setq level (- (match-end 1) (match-beginning 1))
  10452. level (org-tr-level level)
  10453. txt (save-match-data
  10454. (org-html-expand
  10455. (match-string 3 line)))
  10456. todo
  10457. (or (and (match-beginning 2)
  10458. (not (equal (match-string 2 line)
  10459. org-done-string)))
  10460. ; TODO, not DONE
  10461. (and (= level umax)
  10462. (org-search-todo-below
  10463. line lines level))))
  10464. (if org-export-with-section-numbers
  10465. (setq txt (concat (org-section-number level)
  10466. " " txt)))
  10467. (if (<= level umax)
  10468. (progn
  10469. (setq head-count (+ head-count 1))
  10470. (if (> level org-last-level)
  10471. (progn
  10472. (setq cnt (- level org-last-level))
  10473. (while (>= (setq cnt (1- cnt)) 0)
  10474. (insert "<ul>"))
  10475. (insert "\n")))
  10476. (if (< level org-last-level)
  10477. (progn
  10478. (setq cnt (- org-last-level level))
  10479. (while (>= (setq cnt (1- cnt)) 0)
  10480. (insert "</ul>"))
  10481. (insert "\n")))
  10482. ;; Check for targets
  10483. (while (string-match org-target-regexp line)
  10484. (setq tg (match-string 1 line)
  10485. line (replace-match
  10486. (concat "@<span class=\"target\">" tg "@</span> ")
  10487. t t line))
  10488. (push (cons (org-solidify-link-text tg)
  10489. (format "sec-%d" head-count))
  10490. target-alist))
  10491. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  10492. (setq txt (replace-match "" t t txt)))
  10493. (insert
  10494. (format
  10495. (if todo
  10496. "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
  10497. "<li><a href=\"#sec-%d\">%s</a>\n")
  10498. head-count txt))
  10499. (setq org-last-level level))
  10500. )))
  10501. line)
  10502. lines))
  10503. (while (> org-last-level 0)
  10504. (setq org-last-level (1- org-last-level))
  10505. (insert "</ul>\n"))
  10506. ))
  10507. (setq head-count 0)
  10508. (org-init-section-numbers)
  10509. (while (setq line (pop lines) origline line)
  10510. (catch 'nextline
  10511. ;; end of quote section?
  10512. (when (and inquote (string-match "^\\*+" line))
  10513. (insert "</pre>\n")
  10514. (setq inquote nil))
  10515. ;; inside a quote section?
  10516. (when inquote
  10517. (insert (org-html-protect line) "\n")
  10518. (throw 'nextline nil))
  10519. ;; verbatim lines
  10520. (when (and org-export-with-fixed-width
  10521. (string-match "^[ \t]*:\\(.*\\)" line))
  10522. (when (not infixed)
  10523. (setq infixed t)
  10524. (insert "<pre>\n"))
  10525. (insert (org-html-protect (match-string 1 line)) "\n")
  10526. (when (and lines
  10527. (not (string-match "^[ \t]*\\(:.*\\)"
  10528. (car lines))))
  10529. (setq infixed nil)
  10530. (insert "</pre>\n"))
  10531. (throw 'nextline nil))
  10532. ;; make targets to anchors
  10533. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  10534. (cond
  10535. ((match-end 2)
  10536. (setq line (replace-match
  10537. (concat "@<a name=\""
  10538. (org-solidify-link-text (match-string 1 line))
  10539. "\">\\nbsp@</a>")
  10540. t t line)))
  10541. ((and org-export-with-toc (equal (string-to-char line) ?*))
  10542. (setq line (replace-match
  10543. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  10544. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  10545. t t line)))
  10546. (t
  10547. (setq line (replace-match
  10548. (concat "@<a name=\""
  10549. (org-solidify-link-text (match-string 1 line))
  10550. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  10551. t t line)))))
  10552. ;; Replace internal links
  10553. (while (string-match org-bracket-link-regexp line)
  10554. (setq line (replace-match
  10555. (concat
  10556. "@<a href=\"#"
  10557. (org-solidify-link-text (match-string 1 line) target-alist)
  10558. "\">"
  10559. (match-string (if (match-end 3) 3 1) line)
  10560. "@</a>")
  10561. t t line)))
  10562. ;; Protect the external links
  10563. (setq start 0)
  10564. (while (string-match org-link-maybe-angles-regexp line start)
  10565. (setq start (match-end 0))
  10566. (setq line (replace-match
  10567. (concat "\000" (match-string 1 line) "\000")
  10568. t t line)))
  10569. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  10570. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  10571. (setq line (org-html-expand line))
  10572. ;; Format the links
  10573. (setq start 0)
  10574. (while (string-match org-protected-link-regexp line start)
  10575. (setq start (- (match-end 0) 2))
  10576. (setq type (match-string 1 line))
  10577. (cond
  10578. ((member type '("http" "https" "ftp" "mailto" "news"))
  10579. ;; standard URL
  10580. (setq line (replace-match
  10581. ; "<a href=\"\\1:\\2\">&lt;\\1:\\2&gt;</a>"
  10582. "<a href=\"\\1:\\2\">\\1:\\2</a>"
  10583. nil nil line)))
  10584. ((string= type "file")
  10585. ;; FILE link
  10586. (let* ((filename (match-string 2 line))
  10587. (abs-p (file-name-absolute-p filename))
  10588. (thefile (if abs-p (expand-file-name filename) filename))
  10589. (thefile (save-match-data
  10590. (if (string-match ":[0-9]+$" thefile)
  10591. (replace-match "" t t thefile)
  10592. thefile)))
  10593. (file-is-image-p
  10594. (save-match-data
  10595. (string-match (org-image-file-name-regexp) thefile))))
  10596. (setq line (replace-match
  10597. (if (and org-export-html-inline-images
  10598. file-is-image-p)
  10599. (concat "<img src=\"" thefile "\"/>")
  10600. (concat "<a href=\"" thefile "\">\\1:\\2</a>"))
  10601. nil nil line))))
  10602. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell"))
  10603. (setq line (replace-match
  10604. "<i>&lt;\\1:\\2&gt;</i>" nil nil line)))))
  10605. ;; TODO items
  10606. (if (and (string-match org-todo-line-regexp line)
  10607. (match-beginning 2))
  10608. (if (equal (match-string 2 line) org-done-string)
  10609. (setq line (replace-match
  10610. "<span class=\"done\">\\2</span>"
  10611. nil nil line 2))
  10612. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  10613. nil nil line 2))))
  10614. ;; DEADLINES
  10615. (if (string-match org-deadline-line-regexp line)
  10616. (progn
  10617. (if (save-match-data
  10618. (string-match "<a href"
  10619. (substring line 0 (match-beginning 0))))
  10620. nil ; Don't do the replacement - it is inside a link
  10621. (setq line (replace-match "<span class=\"deadline\">\\&</span>"
  10622. nil nil line 1)))))
  10623. (cond
  10624. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  10625. ;; This is a headline
  10626. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  10627. txt (match-string 2 line))
  10628. (if (<= level umax) (setq head-count (+ head-count 1)))
  10629. (when in-local-list
  10630. ;; Close any local lists before inserting a new header line
  10631. (while local-list-num
  10632. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  10633. (pop local-list-num))
  10634. (setq local-list-indent nil
  10635. in-local-list nil))
  10636. (org-html-level-start level txt umax
  10637. (and org-export-with-toc (<= level umax))
  10638. head-count)
  10639. ;; QUOTES
  10640. (when (string-match quote-re line)
  10641. (insert "<pre>")
  10642. (setq inquote t)))
  10643. ((and org-export-with-tables
  10644. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  10645. (if (not table-open)
  10646. ;; New table starts
  10647. (setq table-open t table-buffer nil table-orig-buffer nil))
  10648. ;; Accumulate lines
  10649. (setq table-buffer (cons line table-buffer)
  10650. table-orig-buffer (cons origline table-orig-buffer))
  10651. (when (or (not lines)
  10652. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  10653. (car lines))))
  10654. (setq table-open nil
  10655. table-buffer (nreverse table-buffer)
  10656. table-orig-buffer (nreverse table-orig-buffer))
  10657. (insert (org-format-table-html table-buffer table-orig-buffer))))
  10658. (t
  10659. ;; Normal lines
  10660. (when (and (> org-export-plain-list-max-depth 0)
  10661. (string-match
  10662. (cond
  10663. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10664. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10665. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10666. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  10667. line))
  10668. (setq ind (org-get-string-indentation line)
  10669. start-is-num (match-beginning 4)
  10670. starter (if (match-beginning 2) (match-string 2 line))
  10671. line (substring line (match-beginning 5)))
  10672. (unless (string-match "[^ \t]" line)
  10673. ;; empty line. Pretend indentation is large.
  10674. (setq ind (1+ (or (car local-list-indent) 1))))
  10675. (while (and in-local-list
  10676. (or (and (= ind (car local-list-indent))
  10677. (not starter))
  10678. (< ind (car local-list-indent))))
  10679. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  10680. (pop local-list-num) (pop local-list-indent)
  10681. (setq in-local-list local-list-indent))
  10682. (cond
  10683. ((and starter
  10684. (or (not in-local-list)
  10685. (> ind (car local-list-indent)))
  10686. (< (length local-list-indent)
  10687. org-export-plain-list-max-depth))
  10688. ;; Start new (level of ) list
  10689. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  10690. (push start-is-num local-list-num)
  10691. (push ind local-list-indent)
  10692. (setq in-local-list t))
  10693. (starter
  10694. ;; continue current list
  10695. (insert "<li>\n"))))
  10696. ;; Empty lines start a new paragraph. If hand-formatted lists
  10697. ;; are not fully interpreted, lines starting with "-", "+", "*"
  10698. ;; also start a new paragraph.
  10699. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (insert "<p>"))
  10700. (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
  10701. ))
  10702. (if org-export-html-with-timestamp
  10703. (insert org-export-html-html-helper-timestamp))
  10704. (insert "</body>\n</html>\n")
  10705. (normal-mode)
  10706. (save-buffer)
  10707. (goto-char (point-min)))))
  10708. (defun org-format-table-html (lines olines)
  10709. "Find out which HTML converter to use and return the HTML code."
  10710. (if (string-match "^[ \t]*|" (car lines))
  10711. ;; A normal org table
  10712. (org-format-org-table-html lines)
  10713. ;; Table made by table.el - test for spanning
  10714. (let* ((hlines (delq nil (mapcar
  10715. (lambda (x)
  10716. (if (string-match "^[ \t]*\\+-" x) x
  10717. nil))
  10718. lines)))
  10719. (first (car hlines))
  10720. (ll (and (string-match "\\S-+" first)
  10721. (match-string 0 first)))
  10722. (re (concat "^[ \t]*" (regexp-quote ll)))
  10723. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  10724. hlines))))
  10725. (if (and (not spanning)
  10726. (not org-export-prefer-native-exporter-for-tables))
  10727. ;; We can use my own converter with HTML conversions
  10728. (org-format-table-table-html lines)
  10729. ;; Need to use the code generator in table.el, with the original text.
  10730. (org-format-table-table-html-using-table-generate-source olines)))))
  10731. (defun org-format-org-table-html (lines)
  10732. "Format a table into HTML."
  10733. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  10734. (setq lines (nreverse lines))
  10735. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  10736. (setq lines (nreverse lines))
  10737. (when org-export-table-remove-special-lines
  10738. ;; Check if the table has a marking column. If yes remove the
  10739. ;; column and the special lines
  10740. (let* ((special
  10741. (not
  10742. (memq nil
  10743. (mapcar
  10744. (lambda (x)
  10745. (or (string-match "^[ \t]*|-" x)
  10746. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  10747. lines)))))
  10748. (if special
  10749. (setq lines
  10750. (delq nil
  10751. (mapcar
  10752. (lambda (x)
  10753. (if (string-match "^[ \t]*| *[!_^] *|" x)
  10754. nil ; ignore this line
  10755. (and (or (string-match "^[ \t]*|-+\\+" x)
  10756. (string-match "^[ \t]*|[^|]*|" x))
  10757. (replace-match "|" t t x))))
  10758. lines))))))
  10759. (let ((head (and org-export-highlight-first-table-line
  10760. (delq nil (mapcar
  10761. (lambda (x) (string-match "^[ \t]*|-" x))
  10762. (cdr lines)))))
  10763. line fields html)
  10764. (setq html (concat org-export-html-table-tag "\n"))
  10765. (while (setq line (pop lines))
  10766. (catch 'next-line
  10767. (if (string-match "^[ \t]*|-" line)
  10768. (progn
  10769. (setq head nil) ;; head ends here, first time around
  10770. ;; ignore this line
  10771. (throw 'next-line t)))
  10772. ;; Break the line into fields
  10773. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  10774. (setq html (concat
  10775. html
  10776. "<tr>"
  10777. (mapconcat (lambda (x)
  10778. (if head
  10779. (concat "<th>" x "</th>")
  10780. (concat "<td>" x "</td>")))
  10781. fields "")
  10782. "</tr>\n"))))
  10783. (setq html (concat html "</table>\n"))
  10784. html))
  10785. (defun org-fake-empty-table-line (line)
  10786. "Replace everything except \"|\" with spaces."
  10787. (let ((i (length line))
  10788. (newstr (copy-sequence line)))
  10789. (while (> i 0)
  10790. (setq i (1- i))
  10791. (if (not (eq (aref newstr i) ?|))
  10792. (aset newstr i ?\ )))
  10793. newstr))
  10794. (defun org-format-table-table-html (lines)
  10795. "Format a table generated by table.el into HTML.
  10796. This conversion does *not* use `table-generate-source' from table.el.
  10797. This has the advantage that Org-mode's HTML conversions can be used.
  10798. But it has the disadvantage, that no cell- or row-spanning is allowed."
  10799. (let (line field-buffer
  10800. (head org-export-highlight-first-table-line)
  10801. fields html empty)
  10802. (setq html (concat org-export-html-table-tag "\n"))
  10803. (while (setq line (pop lines))
  10804. (setq empty "&nbsp")
  10805. (catch 'next-line
  10806. (if (string-match "^[ \t]*\\+-" line)
  10807. (progn
  10808. (if field-buffer
  10809. (progn
  10810. (setq html (concat
  10811. html
  10812. "<tr>"
  10813. (mapconcat
  10814. (lambda (x)
  10815. (if (equal x "") (setq x empty))
  10816. (if head
  10817. (concat "<th>" x "</th>\n")
  10818. (concat "<td>" x "</td>\n")))
  10819. field-buffer "\n")
  10820. "</tr>\n"))
  10821. (setq head nil)
  10822. (setq field-buffer nil)))
  10823. ;; Ignore this line
  10824. (throw 'next-line t)))
  10825. ;; Break the line into fields and store the fields
  10826. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  10827. (if field-buffer
  10828. (setq field-buffer (mapcar
  10829. (lambda (x)
  10830. (concat x "<br>" (pop fields)))
  10831. field-buffer))
  10832. (setq field-buffer fields))))
  10833. (setq html (concat html "</table>\n"))
  10834. html))
  10835. (defun org-format-table-table-html-using-table-generate-source (lines)
  10836. "Format a table into html, using `table-generate-source' from table.el.
  10837. This has the advantage that cell- or row-spanning is allowed.
  10838. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  10839. (require 'table)
  10840. (with-current-buffer (get-buffer-create " org-tmp1 ")
  10841. (erase-buffer)
  10842. (insert (mapconcat 'identity lines "\n"))
  10843. (goto-char (point-min))
  10844. (if (not (re-search-forward "|[^+]" nil t))
  10845. (error "Error processing table"))
  10846. (table-recognize-table)
  10847. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  10848. (table-generate-source 'html " org-tmp2 ")
  10849. (set-buffer " org-tmp2 ")
  10850. (buffer-substring (point-min) (point-max))))
  10851. (defun org-html-protect (s)
  10852. ;; convert & to &amp;, < to &lt; and > to &gt;
  10853. (let ((start 0))
  10854. (while (string-match "&" s start)
  10855. (setq s (replace-match "&amp;" t t s)
  10856. start (1+ (match-beginning 0))))
  10857. (while (string-match "<" s)
  10858. (setq s (replace-match "&lt;" t t s)))
  10859. (while (string-match ">" s)
  10860. (setq s (replace-match "&gt;" t t s))))
  10861. s)
  10862. (defun org-html-expand (string)
  10863. "Prepare STRING for HTML export. Applies all active conversions."
  10864. ;; First check if there is a link in the line - if yes, apply conversions
  10865. ;; only before the start of the link.
  10866. ;; FIXME: This is no longer correct, because links now have an end.
  10867. (let* ((m (string-match org-link-regexp string))
  10868. (s (if m (substring string 0 m) string))
  10869. (r (if m (substring string m) "")))
  10870. ;; convert & to &amp;, < to &lt; and > to &gt;
  10871. (setq s (org-html-protect s))
  10872. (if org-export-html-expand
  10873. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  10874. (setq s (replace-match "<\\1>" nil nil s))))
  10875. (if org-export-with-emphasize
  10876. (setq s (org-export-html-convert-emphasize s)))
  10877. (if org-export-with-sub-superscripts
  10878. (setq s (org-export-html-convert-sub-super s)))
  10879. (if org-export-with-TeX-macros
  10880. (let ((start 0) wd ass)
  10881. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  10882. (setq wd (match-string 1 s))
  10883. (if (setq ass (assoc wd org-html-entities))
  10884. (setq s (replace-match (or (cdr ass)
  10885. (concat "&" (car ass) ";"))
  10886. t t s))
  10887. (setq start (+ start (length wd)))))))
  10888. (concat s r)))
  10889. (defun org-create-multibrace-regexp (left right n)
  10890. "Create a regular expression which will match a balanced sexp.
  10891. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  10892. as single character strings.
  10893. The regexp returned will match the entire expression including the
  10894. delimiters. It will also define a single group which contains the
  10895. match except for the outermost delimiters. The maximum depth of
  10896. stacked delimiters is N. Escaping delimiters is not possible."
  10897. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  10898. (or "\\|")
  10899. (re nothing)
  10900. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  10901. (while (> n 1)
  10902. (setq n (1- n)
  10903. re (concat re or next)
  10904. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  10905. (concat left "\\(" re "\\)" right)))
  10906. (defvar org-match-substring-regexp
  10907. (concat
  10908. "\\([^\\]\\)\\([_^]\\)\\("
  10909. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  10910. "\\|"
  10911. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  10912. "\\|"
  10913. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  10914. "The regular expression matching a sub- or superscript.")
  10915. (defun org-export-html-convert-sub-super (string)
  10916. "Convert sub- and superscripts in STRING to HTML."
  10917. (let (key c)
  10918. (while (string-match org-match-substring-regexp string)
  10919. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  10920. (setq c (or (match-string 8 string)
  10921. (match-string 6 string)
  10922. (match-string 5 string)))
  10923. (setq string (replace-match
  10924. (concat (match-string 1 string)
  10925. "<" key ">" c "</" key ">")
  10926. t t string)))
  10927. (while (string-match "\\\\\\([_^]\\)" string)
  10928. (setq string (replace-match (match-string 1 string) t t string))))
  10929. string)
  10930. (defun org-export-html-convert-emphasize (string)
  10931. (let (c (s 0))
  10932. (while (string-match "\\(\\W\\|^\\)\\([*/_]\\)\\(\\w+\\)\\2\\(\\W\\|$\\)" string s)
  10933. (setq c (cdr (assoc (match-string 2 string)
  10934. '(("*" . "b") ("/" . "i") ("_" . "u"))))
  10935. s (+ (match-end 0) 3)
  10936. string (replace-match
  10937. (concat "\\1<" c ">\\3</" c ">\\4") t nil string)))
  10938. string))
  10939. (defun org-parse-key-lines ()
  10940. "Find the special key lines with the information for exporters."
  10941. (save-excursion
  10942. (goto-char 0)
  10943. (let ((re (org-make-options-regexp
  10944. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  10945. key)
  10946. (while (re-search-forward re nil t)
  10947. (setq key (match-string 1))
  10948. (cond ((string-equal key "TITLE")
  10949. (setq title (match-string 2)))
  10950. ((string-equal key "AUTHOR")
  10951. (setq author (match-string 2)))
  10952. ((string-equal key "EMAIL")
  10953. (setq email (match-string 2)))
  10954. ((string-equal key "LANGUAGE")
  10955. (setq language (match-string 2)))
  10956. ((string-equal key "TEXT")
  10957. (setq text (concat text "\n" (match-string 2))))
  10958. ((string-equal key "OPTIONS")
  10959. (setq options (match-string 2))))))))
  10960. (defun org-parse-export-options (s)
  10961. "Parse the export options line."
  10962. (let ((op '(("H" . org-export-headline-levels)
  10963. ("num" . org-export-with-section-numbers)
  10964. ("toc" . org-export-with-toc)
  10965. ("\\n" . org-export-preserve-breaks)
  10966. ("@" . org-export-html-expand)
  10967. (":" . org-export-with-fixed-width)
  10968. ("|" . org-export-with-tables)
  10969. ("^" . org-export-with-sub-superscripts)
  10970. ("*" . org-export-with-emphasize)
  10971. ("TeX" . org-export-with-TeX-macros)))
  10972. o)
  10973. (while (setq o (pop op))
  10974. (if (string-match (concat (regexp-quote (car o)) ":\\([^ \t\n\r;,.]*\\)")
  10975. s)
  10976. (set (make-local-variable (cdr o))
  10977. (car (read-from-string (match-string 1 s))))))))
  10978. (defun org-html-level-start (level title umax with-toc head-count)
  10979. "Insert a new level in HTML export."
  10980. (let ((l (1+ (max level umax))))
  10981. (while (<= l org-level-max)
  10982. (if (aref levels-open (1- l))
  10983. (progn
  10984. (org-html-level-close l)
  10985. (aset levels-open (1- l) nil)))
  10986. (setq l (1+ l)))
  10987. (if (> level umax)
  10988. (progn
  10989. (if (aref levels-open (1- level))
  10990. (insert "<li>" title "<p>\n")
  10991. (aset levels-open (1- level) t)
  10992. (insert "<ul><li>" title "<p>\n")))
  10993. (if org-export-with-section-numbers
  10994. (setq title (concat (org-section-number level) " " title)))
  10995. (setq level (+ level 1))
  10996. (if with-toc
  10997. (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
  10998. level head-count title level))
  10999. (insert (format "\n<H%d>%s</H%d>\n" level title level))))))
  11000. (defun org-html-level-close (&rest args)
  11001. "Terminate one level in HTML export."
  11002. (insert "</ul>"))
  11003. ;; Variable holding the vector with section numbers
  11004. (defvar org-section-numbers (make-vector org-level-max 0))
  11005. (defun org-init-section-numbers ()
  11006. "Initialize the vector for the section numbers."
  11007. (let* ((level -1)
  11008. (numbers (nreverse (org-split-string "" "\\.")))
  11009. (depth (1- (length org-section-numbers)))
  11010. (i depth) number-string)
  11011. (while (>= i 0)
  11012. (if (> i level)
  11013. (aset org-section-numbers i 0)
  11014. (setq number-string (or (car numbers) "0"))
  11015. (if (string-match "\\`[A-Z]\\'" number-string)
  11016. (aset org-section-numbers i
  11017. (- (string-to-char number-string) ?A -1))
  11018. (aset org-section-numbers i (string-to-number number-string)))
  11019. (pop numbers))
  11020. (setq i (1- i)))))
  11021. (defun org-section-number (&optional level)
  11022. "Return a string with the current section number.
  11023. When LEVEL is non-nil, increase section numbers on that level."
  11024. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  11025. (when level
  11026. (when (> level -1)
  11027. (aset org-section-numbers
  11028. level (1+ (aref org-section-numbers level))))
  11029. (setq idx (1+ level))
  11030. (while (<= idx depth)
  11031. (if (not (= idx 1))
  11032. (aset org-section-numbers idx 0))
  11033. (setq idx (1+ idx))))
  11034. (setq idx 0)
  11035. (while (<= idx depth)
  11036. (setq n (aref org-section-numbers idx))
  11037. (setq string (concat string (if (not (string= string "")) "." "")
  11038. (int-to-string n)))
  11039. (setq idx (1+ idx)))
  11040. (save-match-data
  11041. (if (string-match "\\`\\([@0]\\.\\)+" string)
  11042. (setq string (replace-match "" nil nil string)))
  11043. (if (string-match "\\(\\.0\\)+\\'" string)
  11044. (setq string (replace-match "" nil nil string))))
  11045. string))
  11046. (defun org-export-icalendar-this-file ()
  11047. "Export current file as an iCalendar file.
  11048. The iCalendar file will be located in the same directory as the Org-mode
  11049. file, but with extension `.ics'."
  11050. (interactive)
  11051. (org-export-icalendar nil buffer-file-name))
  11052. ;;;###autoload
  11053. (defun org-export-icalendar-all-agenda-files ()
  11054. "Export all files in `org-agenda-files' to iCalendar .ics files.
  11055. Each iCalendar file will be located in the same directory as the Org-mode
  11056. file, but with extension `.ics'."
  11057. (interactive)
  11058. (apply 'org-export-icalendar nil (org-agenda-files t)))
  11059. ;;;###autoload
  11060. (defun org-export-icalendar-combine-agenda-files ()
  11061. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  11062. The file is stored under the name `org-combined-agenda-icalendar-file'."
  11063. (interactive)
  11064. (apply 'org-export-icalendar t (org-agenda-files t)))
  11065. (defun org-export-icalendar (combine &rest files)
  11066. "Create iCalendar files for all elements of FILES.
  11067. If COMBINE is non-nil, combine all calendar entries into a single large
  11068. file and store it under the name `org-combined-agenda-icalendar-file'."
  11069. (save-excursion
  11070. (let* (file ical-file ical-buffer category started org-agenda-new-buffers)
  11071. (when combine
  11072. (setq ical-file org-combined-agenda-icalendar-file
  11073. ical-buffer (org-get-agenda-file-buffer ical-file))
  11074. (set-buffer ical-buffer) (erase-buffer))
  11075. (while (setq file (pop files))
  11076. (catch 'nextfile
  11077. (org-check-agenda-file file)
  11078. (unless combine
  11079. (setq ical-file (concat (file-name-sans-extension file) ".ics"))
  11080. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  11081. (set-buffer ical-buffer) (erase-buffer))
  11082. (set-buffer (org-get-agenda-file-buffer file))
  11083. (setq category (or org-category
  11084. (file-name-sans-extension
  11085. (file-name-nondirectory buffer-file-name))))
  11086. (if (symbolp category) (setq category (symbol-name category)))
  11087. (let ((standard-output ical-buffer))
  11088. (if combine
  11089. (and (not started) (setq started t)
  11090. (org-start-icalendar-file org-icalendar-combined-name))
  11091. (org-start-icalendar-file category))
  11092. (org-print-icalendar-entries combine category)
  11093. (when (or (and combine (not files)) (not combine))
  11094. (org-finish-icalendar-file)
  11095. (set-buffer ical-buffer)
  11096. (save-buffer)
  11097. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  11098. (org-release-buffers org-agenda-new-buffers))))
  11099. (defvar org-after-save-iCalendar-file-hook nil
  11100. "Hook run after an iCalendar file has been saved.
  11101. The iCalendar buffer is still current when this hook is run.
  11102. A good way to use this is to tell a desktop calenndar application to re-read
  11103. the iCalendar file.")
  11104. (defun org-print-icalendar-entries (&optional combine category)
  11105. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  11106. When COMBINE is non nil, add the category to each line."
  11107. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  11108. (dts (org-ical-ts-to-string
  11109. (format-time-string (cdr org-time-stamp-formats) (current-time))
  11110. "DTSTART"))
  11111. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  11112. (save-excursion
  11113. (goto-char (point-min))
  11114. (while (re-search-forward org-ts-regexp nil t)
  11115. (setq pos (match-beginning 0)
  11116. ts (match-string 0)
  11117. inc t
  11118. hd (org-get-heading))
  11119. (if (looking-at re2)
  11120. (progn
  11121. (goto-char (match-end 0))
  11122. (setq ts2 (match-string 1) inc nil))
  11123. (setq ts2 ts
  11124. tmp (buffer-substring (max (point-min)
  11125. (- pos org-ds-keyword-length))
  11126. pos)
  11127. deadlinep (string-match org-deadline-regexp tmp)
  11128. scheduledp (string-match org-scheduled-regexp tmp)
  11129. ;; donep (org-entry-is-done-p)
  11130. ))
  11131. (if (or (string-match org-tr-regexp hd)
  11132. (string-match org-ts-regexp hd))
  11133. (setq hd (replace-match "" t t hd)))
  11134. (if combine
  11135. (setq hd (concat hd " (category " category ")")))
  11136. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  11137. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  11138. (princ (format "BEGIN:VEVENT
  11139. %s
  11140. %s
  11141. SUMMARY:%s
  11142. END:VEVENT\n"
  11143. (org-ical-ts-to-string ts "DTSTART")
  11144. (org-ical-ts-to-string ts2 "DTEND" inc)
  11145. hd)))
  11146. (when org-icalendar-include-todo
  11147. (goto-char (point-min))
  11148. (while (re-search-forward org-todo-line-regexp nil t)
  11149. (setq state (match-string 1))
  11150. (unless (equal state org-done-string)
  11151. (setq hd (match-string 3))
  11152. (if (string-match org-priority-regexp hd)
  11153. (setq pri (string-to-char (match-string 2 hd))
  11154. hd (concat (substring hd 0 (match-beginning 1))
  11155. (substring hd (- (match-end 1)))))
  11156. (setq pri org-default-priority))
  11157. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  11158. (- org-lowest-priority ?A))))))
  11159. (princ (format "BEGIN:VTODO
  11160. %s
  11161. SUMMARY:%s
  11162. SEQUENCE:1
  11163. PRIORITY:%d
  11164. END:VTODO\n"
  11165. dts hd pri))))))))
  11166. (defun org-start-icalendar-file (name)
  11167. "Start an iCalendar file by inserting the header."
  11168. (let ((user user-full-name)
  11169. (name (or name "unknown"))
  11170. (timezone (cadr (current-time-zone))))
  11171. (princ
  11172. (format "BEGIN:VCALENDAR
  11173. VERSION:2.0
  11174. X-WR-CALNAME:%s
  11175. PRODID:-//%s//Emacs with Org-mode//EN
  11176. X-WR-TIMEZONE:%s
  11177. CALSCALE:GREGORIAN\n" name user timezone))))
  11178. (defun org-finish-icalendar-file ()
  11179. "Finish an iCalendar file by inserting the END statement."
  11180. (princ "END:VCALENDAR\n"))
  11181. (defun org-ical-ts-to-string (s keyword &optional inc)
  11182. "Take a time string S and convert it to iCalendar format.
  11183. KEYWORD is added in front, to make a complete line like DTSTART....
  11184. When INC is non-nil, increase the hour by two (if time string contains
  11185. a time), or the day by one (if it does not contain a time)."
  11186. (let ((t1 (org-parse-time-string s 'nodefault))
  11187. t2 fmt have-time time)
  11188. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  11189. (setq t2 t1 have-time t)
  11190. (setq t2 (org-parse-time-string s)))
  11191. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  11192. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  11193. (when inc
  11194. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  11195. (setq time (encode-time s mi h d m y)))
  11196. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  11197. (concat keyword (format-time-string fmt time))))
  11198. ;;; Key bindings
  11199. ;; - Bindings in Org-mode map are currently
  11200. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  11201. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  11202. ;; e (?) useful from outline-mode
  11203. ;; i k @ expendable from outline-mode
  11204. ;; 0123456789 % & ()_{} " ` free
  11205. ;; Make `C-c C-x' a prefix key
  11206. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11207. ;; TAB key with modifiers
  11208. (define-key org-mode-map "\C-i" 'org-cycle)
  11209. (define-key org-mode-map [(tab)] 'org-cycle)
  11210. (define-key org-mode-map [(meta tab)] 'org-complete)
  11211. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  11212. ;; The following line is necessary under Suse GNU/Linux
  11213. (unless (featurep 'xemacs)
  11214. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  11215. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  11216. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  11217. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  11218. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11219. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  11220. (define-key org-mode-map [(meta return)] 'org-meta-return)
  11221. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  11222. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  11223. ;; Cursor keys with modifiers
  11224. (define-key org-mode-map [(meta left)] 'org-metaleft)
  11225. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  11226. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  11227. (define-key org-mode-map [(meta right)] 'org-metaright)
  11228. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  11229. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  11230. (define-key org-mode-map [(meta up)] 'org-metaup)
  11231. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  11232. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  11233. (define-key org-mode-map [(meta down)] 'org-metadown)
  11234. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  11235. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  11236. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11237. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  11238. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11239. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  11240. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11241. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  11242. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11243. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  11244. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  11245. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  11246. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  11247. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  11248. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  11249. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  11250. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  11251. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  11252. ;; All the other keys
  11253. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  11254. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  11255. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  11256. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  11257. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  11258. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  11259. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11260. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  11261. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  11262. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11263. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  11264. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  11265. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  11266. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  11267. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  11268. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  11269. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  11270. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11271. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11272. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11273. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11274. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  11275. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  11276. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11277. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11278. (define-key org-mode-map "\C-c]" 'org-remove-file)
  11279. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  11280. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  11281. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  11282. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11283. (define-key org-mode-map "\C-m" 'org-return)
  11284. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  11285. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  11286. (define-key org-mode-map "\C-c+" 'org-table-sum)
  11287. (define-key org-mode-map "\C-c|" 'org-table-toggle-vline-visibility)
  11288. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  11289. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  11290. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  11291. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11292. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11293. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11294. (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  11295. (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  11296. (define-key org-mode-map "\C-c\C-xv" 'org-export-copy-visible)
  11297. (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-copy-visible)
  11298. ;; OPML support is only an option for the future
  11299. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  11300. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  11301. (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  11302. (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  11303. (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  11304. (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  11305. (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  11306. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11307. (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  11308. (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  11309. (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  11310. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11311. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11312. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11313. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11314. (defsubst org-table-p () (org-at-table-p))
  11315. (defun org-self-insert-command (N)
  11316. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11317. If the cursor is in a table looking at whitespace, the whitespace is
  11318. overwritten, and the table is not marked as requiring realignment."
  11319. (interactive "p")
  11320. (if (and (org-table-p)
  11321. (or
  11322. (and org-table-auto-blank-field
  11323. (member last-command
  11324. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11325. (org-table-blank-field))
  11326. t)
  11327. (eq N 1)
  11328. (looking-at "[^|\n]* +|"))
  11329. (let (org-table-may-need-update)
  11330. (goto-char (1- (match-end 0)))
  11331. (delete-backward-char 1)
  11332. (goto-char (match-beginning 0))
  11333. (self-insert-command N))
  11334. (setq org-table-may-need-update t)
  11335. (self-insert-command N)))
  11336. ;; FIXME:
  11337. ;; The following two functions might still be optimized to trigger
  11338. ;; re-alignment less frequently.
  11339. (defun org-delete-backward-char (N)
  11340. "Like `delete-backward-char', insert whitespace at field end in tables.
  11341. When deleting backwards, in tables this function will insert whitespace in
  11342. front of the next \"|\" separator, to keep the table aligned. The table will
  11343. still be marked for re-alignment, because a narrow field may lead to a
  11344. reduced column width."
  11345. (interactive "p")
  11346. (if (and (org-table-p)
  11347. (eq N 1)
  11348. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11349. (looking-at ".*?|"))
  11350. (let ((pos (point)))
  11351. (backward-delete-char N)
  11352. (skip-chars-forward "^|")
  11353. (insert " ")
  11354. (goto-char (1- pos)))
  11355. (backward-delete-char N)))
  11356. (defun org-delete-char (N)
  11357. "Like `delete-char', but insert whitespace at field end in tables.
  11358. When deleting characters, in tables this function will insert whitespace in
  11359. front of the next \"|\" separator, to keep the table aligned. The table
  11360. will still be marked for re-alignment, because a narrow field may lead to
  11361. a reduced column width."
  11362. (interactive "p")
  11363. (if (and (org-table-p)
  11364. (not (bolp))
  11365. (not (= (char-after) ?|))
  11366. (eq N 1))
  11367. (if (looking-at ".*?|")
  11368. (let ((pos (point)))
  11369. (replace-match (concat
  11370. (substring (match-string 0) 1 -1)
  11371. " |"))
  11372. (goto-char pos)))
  11373. (delete-char N)))
  11374. ;; How to do this: Measure non-white length of current string
  11375. ;; If equal to column width, we should realign.
  11376. (defun org-remap (map &rest commands)
  11377. "In MAP, remap the functions given in COMMANDS.
  11378. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11379. (let (new old)
  11380. (while commands
  11381. (setq old (pop commands) new (pop commands))
  11382. (if (fboundp 'command-remapping)
  11383. (define-key map (vector 'remap old) new)
  11384. (substitute-key-definition old new map global-map)))))
  11385. (when (eq org-enable-table-editor 'optimized)
  11386. ;; If the user wants maximum table support, we need to hijack
  11387. ;; some standard editing functions
  11388. (org-remap org-mode-map
  11389. 'self-insert-command 'org-self-insert-command
  11390. 'delete-char 'org-delete-char
  11391. 'delete-backward-char 'org-delete-backward-char)
  11392. (define-key org-mode-map "|" 'org-force-self-insert))
  11393. (defun org-shiftcursor-error ()
  11394. "Throw an error because Shift-Cursor command was applied in wrong context."
  11395. (error "This command is active in special context like tables, headlines or timestamps"))
  11396. (defun org-shifttab ()
  11397. "Global visibility cycling or move to previous table field.
  11398. Calls `(org-cycle t)' or `org-table-previous-field', depending on context.
  11399. See the individual commands for more information."
  11400. (interactive)
  11401. (cond
  11402. ((org-at-table-p) (org-table-previous-field))
  11403. (t (org-cycle '(4)))))
  11404. (defun org-shiftmetaleft ()
  11405. "Promote subtree or delete table column.
  11406. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  11407. See the individual commands for more information."
  11408. (interactive)
  11409. (cond
  11410. ((org-at-table-p) (org-table-delete-column))
  11411. ((org-on-heading-p) (org-promote-subtree))
  11412. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11413. (t (org-shiftcursor-error))))
  11414. (defun org-shiftmetaright ()
  11415. "Demote subtree or insert table column.
  11416. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  11417. See the individual commands for more information."
  11418. (interactive)
  11419. (cond
  11420. ((org-at-table-p) (org-table-insert-column))
  11421. ((org-on-heading-p) (org-demote-subtree))
  11422. ((org-at-item-p) (call-interactively 'org-indent-item))
  11423. (t (org-shiftcursor-error))))
  11424. (defun org-shiftmetaup (&optional arg)
  11425. "Move subtree up or kill table row.
  11426. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11427. `org-move-item-up' depending on context. See the individual commands
  11428. for more information."
  11429. (interactive "P")
  11430. (cond
  11431. ((org-at-table-p) (org-table-kill-row))
  11432. ((org-on-heading-p) (org-move-subtree-up arg))
  11433. ((org-at-item-p) (org-move-item-up arg))
  11434. (t (org-shiftcursor-error))))
  11435. (defun org-shiftmetadown (&optional arg)
  11436. "Move subtree down or insert table row.
  11437. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11438. `org-move-item-down', depending on context. See the individual
  11439. commands for more information."
  11440. (interactive "P")
  11441. (cond
  11442. ((org-at-table-p) (org-table-insert-row arg))
  11443. ((org-on-heading-p) (org-move-subtree-down arg))
  11444. ((org-at-item-p) (org-move-item-down arg))
  11445. (t (org-shiftcursor-error))))
  11446. (defun org-metaleft (&optional arg)
  11447. "Promote heading or move table column to left.
  11448. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11449. With no specific context, calls the Emacs default `backward-word'.
  11450. See the individual commands for more information."
  11451. (interactive "P")
  11452. (cond
  11453. ((org-at-table-p) (org-table-move-column 'left))
  11454. ((or (org-on-heading-p) (org-region-active-p)) (org-do-promote))
  11455. (t (backward-word (prefix-numeric-value arg)))))
  11456. (defun org-metaright (&optional arg)
  11457. "Demote subtree or move table column to right.
  11458. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11459. With no specific context, calls the Emacs default `forward-word'.
  11460. See the individual commands for more information."
  11461. (interactive "P")
  11462. (cond
  11463. ((org-at-table-p) (org-table-move-column nil))
  11464. ((or (org-on-heading-p) (org-region-active-p)) (org-do-demote))
  11465. (t (forward-word (prefix-numeric-value arg)))))
  11466. (defun org-metaup (&optional arg)
  11467. "Move subtree up or move table row up.
  11468. Calls `org-move-subtree-up' or `org-table-move-row' or
  11469. `org-move-item-up', depending on context. See the individual commands
  11470. for more information."
  11471. (interactive "P")
  11472. (cond
  11473. ((org-at-table-p) (org-table-move-row 'up))
  11474. ((org-on-heading-p) (org-move-subtree-up arg))
  11475. ((org-at-item-p) (org-move-item-up arg))
  11476. (t (org-shiftcursor-error))))
  11477. (defun org-metadown (&optional arg)
  11478. "Move subtree down or move table row down.
  11479. Calls `org-move-subtree-down' or `org-table-move-row' or
  11480. `org-move-item-down', depending on context. See the individual
  11481. commands for more information."
  11482. (interactive "P")
  11483. (cond
  11484. ((org-at-table-p) (org-table-move-row nil))
  11485. ((org-on-heading-p) (org-move-subtree-down arg))
  11486. ((org-at-item-p) (org-move-item-down arg))
  11487. (t (org-shiftcursor-error))))
  11488. (defun org-shiftup (&optional arg)
  11489. "Increase item in timestamp or increase priority of current item.
  11490. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  11491. See the individual commands for more information."
  11492. (interactive "P")
  11493. (cond
  11494. ((org-at-timestamp-p) (org-timestamp-up arg))
  11495. (t (org-priority-up))))
  11496. (defun org-shiftdown (&optional arg)
  11497. "Decrease item in timestamp or decrease priority of current item.
  11498. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  11499. See the individual commands for more information."
  11500. (interactive "P")
  11501. (cond
  11502. ((org-at-timestamp-p) (org-timestamp-down arg))
  11503. (t (org-priority-down))))
  11504. (defun org-shiftright ()
  11505. "Next TODO keyword or timestamp one day later, depending on context."
  11506. (interactive)
  11507. (cond
  11508. ((org-at-timestamp-p) (org-timestamp-up-day))
  11509. ((org-on-heading-p) (org-todo 'right))
  11510. (t (org-shiftcursor-error))))
  11511. (defun org-shiftleft ()
  11512. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11513. (interactive)
  11514. (cond
  11515. ((org-at-timestamp-p) (org-timestamp-down-day))
  11516. ((org-on-heading-p) (org-todo 'left))
  11517. (t (org-shiftcursor-error))))
  11518. (defun org-copy-special ()
  11519. "Copy region in table or copy current subtree.
  11520. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11521. See the individual commands for more information."
  11522. (interactive)
  11523. (call-interactively
  11524. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11525. (defun org-cut-special ()
  11526. "Cut region in table or cut current subtree.
  11527. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11528. See the individual commands for more information."
  11529. (interactive)
  11530. (call-interactively
  11531. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11532. (defun org-paste-special (arg)
  11533. "Paste rectangular region into table, or past subtree relative to level.
  11534. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11535. See the individual commands for more information."
  11536. (interactive "P")
  11537. (if (org-at-table-p)
  11538. (org-table-paste-rectangle)
  11539. (org-paste-subtree arg)))
  11540. (defun org-ctrl-c-ctrl-c (&optional arg)
  11541. "Call realign table, or recognize a table.el table, or update keywords.
  11542. When the cursor is inside a table created by the table.el package,
  11543. activate that table. Otherwise, if the cursor is at a normal table
  11544. created with org.el, re-align that table. This command works even if
  11545. the automatic table editor has been turned off.
  11546. If the cursor is in a headline, prompt for tags and insert them into
  11547. the current line, aligned to `org-tags-column'. When in a headline and
  11548. called with prefix arg, realign all tags in the current buffer.
  11549. If the cursor is in one of the special #+KEYWORD lines, this triggers
  11550. scanning the buffer for these lines and updating the information.
  11551. If the cursor is on a #+TBLFM line, re-apply the formulae to the table."
  11552. (interactive "P")
  11553. (let ((org-enable-table-editor t))
  11554. (cond
  11555. ((and (boundp 'remember-buffer) (equal (buffer-name) remember-buffer))
  11556. (remember-buffer))
  11557. ((org-on-target-p) (org-update-radio-target-regexp))
  11558. ((org-on-heading-p) (org-set-tags arg))
  11559. ((org-at-table.el-p)
  11560. (require 'table)
  11561. (beginning-of-line 1)
  11562. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11563. (table-recognize-table))
  11564. ((org-at-table-p)
  11565. (org-table-maybe-eval-formula)
  11566. (if arg
  11567. (org-table-recalculate t)
  11568. (org-table-maybe-recalculate-line))
  11569. (org-table-align))
  11570. ((org-at-item-p)
  11571. (org-renumber-ordered-list (prefix-numeric-value arg)))
  11572. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11573. (cond
  11574. ((equal (match-string 1) "TBLFM")
  11575. ;; Recalculate the table before this line
  11576. (save-excursion
  11577. (beginning-of-line 1)
  11578. (skip-chars-backward " \r\n\t")
  11579. (if (org-at-table-p) (org-table-recalculate t))))
  11580. (t
  11581. (org-mode-restart))))
  11582. ((org-region-active-p)
  11583. (org-table-convert-region (region-beginning) (region-end) arg))
  11584. ((condition-case nil
  11585. (and (region-beginning) (region-end))
  11586. (error nil))
  11587. (if (y-or-n-p "Convert inactive region to table? ")
  11588. (org-table-convert-region (region-beginning) (region-end) arg)
  11589. (error "Abort")))
  11590. (t (error "C-c C-c can do nothing useful at this location.")))))
  11591. (defun org-mode-restart ()
  11592. "Restart Org-mode, to scan again for special lines.
  11593. Also updates the keyword regular expressions."
  11594. (interactive)
  11595. (let ((org-inhibit-startup t)) (org-mode))
  11596. (message "Org-mode restarted to refresh keyword and special line setup"))
  11597. (defun org-return ()
  11598. "Goto next table row or insert a newline.
  11599. Calls `org-table-next-row' or `newline', depending on context.
  11600. See the individual commands for more information."
  11601. (interactive)
  11602. (cond
  11603. ((org-at-table-p)
  11604. (org-table-justify-field-maybe)
  11605. (org-table-next-row))
  11606. (t (newline))))
  11607. (defun org-meta-return (&optional arg)
  11608. "Insert a new heading or wrap a region in a table.
  11609. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11610. See the individual commands for more information."
  11611. (interactive "P")
  11612. (cond
  11613. ((org-at-table-p)
  11614. (org-table-wrap-region arg))
  11615. (t (org-insert-heading arg))))
  11616. ;;; Menu entries
  11617. ;; Define the Org-mode menus
  11618. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11619. '("Tbl"
  11620. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  11621. ["Next Field" org-cycle (org-at-table-p)]
  11622. ["Previous Field" org-shifttab (org-at-table-p)]
  11623. ["Next Row" org-return (org-at-table-p)]
  11624. "--"
  11625. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11626. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11627. "--"
  11628. ("Column"
  11629. ["Move Column Left" org-metaleft (org-at-table-p)]
  11630. ["Move Column Right" org-metaright (org-at-table-p)]
  11631. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11632. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11633. ("Row"
  11634. ["Move Row Up" org-metaup (org-at-table-p)]
  11635. ["Move Row Down" org-metadown (org-at-table-p)]
  11636. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11637. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11638. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11639. "--"
  11640. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  11641. ("Rectangle"
  11642. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11643. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11644. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11645. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11646. "--"
  11647. ("Calculate"
  11648. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11649. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11650. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11651. "--"
  11652. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11653. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11654. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11655. "--"
  11656. ["Sum Column/Rectangle" org-table-sum
  11657. (or (org-at-table-p) (org-region-active-p))]
  11658. ["Which Column?" org-table-current-column (org-at-table-p)])
  11659. ["Debug Formulas"
  11660. (setq org-table-formula-debug (not org-table-formula-debug))
  11661. :style toggle :selected org-table-formula-debug]
  11662. "--"
  11663. ["Invisible Vlines" org-table-toggle-vline-visibility
  11664. :style toggle :selected (org-in-invisibility-spec-p '(org-table))]
  11665. "--"
  11666. ["Create" org-table-create (and (not (org-at-table-p))
  11667. org-enable-table-editor)]
  11668. ["Convert Region" org-ctrl-c-ctrl-c (not (org-at-table-p 'any))]
  11669. ["Import from File" org-table-import (not (org-at-table-p))]
  11670. ["Export to File" org-table-export (org-at-table-p)]
  11671. "--"
  11672. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11673. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11674. '("Org"
  11675. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  11676. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  11677. ["Sparse Tree" org-occur t]
  11678. ["Show All" show-all t]
  11679. "--"
  11680. ["New Heading" org-insert-heading t]
  11681. ("Navigate Headings"
  11682. ["Up" outline-up-heading t]
  11683. ["Next" outline-next-visible-heading t]
  11684. ["Previous" outline-previous-visible-heading t]
  11685. ["Next Same Level" outline-forward-same-level t]
  11686. ["Previous Same Level" outline-backward-same-level t]
  11687. "--"
  11688. ["Jump" org-goto t])
  11689. ("Edit Structure"
  11690. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11691. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11692. "--"
  11693. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11694. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11695. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11696. "--"
  11697. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11698. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11699. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11700. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11701. "--"
  11702. ["Archive Subtree" org-archive-subtree t]
  11703. "--"
  11704. ["Convert to odd levels" org-convert-to-odd-levels t]
  11705. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11706. "--"
  11707. ("TODO Lists"
  11708. ["TODO/DONE/-" org-todo t]
  11709. ["Show TODO Tree" org-show-todo-tree t]
  11710. ["Global TODO list" org-todo-list t]
  11711. "--"
  11712. ["Set Priority" org-priority t]
  11713. ["Priority Up" org-shiftup t]
  11714. ["Priority Down" org-shiftdown t])
  11715. ("Dates and Scheduling"
  11716. ["Timestamp" org-time-stamp t]
  11717. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11718. ("Change Date"
  11719. ["1 Day Later" org-timestamp-up-day t]
  11720. ["1 Day Earlier" org-timestamp-down-day t]
  11721. ["1 ... Later" org-shiftup t]
  11722. ["1 ... Earlier" org-shiftdown t])
  11723. ["Compute Time Range" org-evaluate-time-range t]
  11724. ["Schedule Item" org-schedule t]
  11725. ["Deadline" org-deadline t]
  11726. "--"
  11727. ["Goto Calendar" org-goto-calendar t]
  11728. ["Date from Calendar" org-date-from-calendar t])
  11729. "--"
  11730. ["Agenda Command" org-agenda t]
  11731. ("File List for Agenda")
  11732. ("Special views current file"
  11733. ["TODO Tree" org-show-todo-tree t]
  11734. ["Check Deadlines" org-check-deadlines t]
  11735. ["Timeline" org-timeline t]
  11736. ["Tags Tree" org-tags-sparse-tree t])
  11737. "--"
  11738. ("Hyperlinks"
  11739. ["Store Link (Global)" org-store-link t]
  11740. ["Insert Link" org-insert-link t]
  11741. ["Follow Link" org-open-at-point t])
  11742. "--"
  11743. ("Export"
  11744. ["ASCII" org-export-as-ascii t]
  11745. ["Extract Visible Text" org-export-copy-visible t]
  11746. ["HTML" org-export-as-html t]
  11747. ["HTML and Open" org-export-as-html-and-open t]
  11748. ; ["OPML" org-export-as-opml nil]
  11749. "--"
  11750. ["iCalendar this file" org-export-icalendar-this-file t]
  11751. ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
  11752. :active t :keys "C-c C-x C-i"]
  11753. ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
  11754. "--"
  11755. ["Option Template" org-insert-export-options-template t]
  11756. ["Toggle Fixed Width" org-toggle-fixed-width-section t])
  11757. "--"
  11758. ("Documentation"
  11759. ["Show Version" org-version t]
  11760. ["Info Documentation" org-info t])
  11761. ("Customize"
  11762. ["Browse Org Group" org-customize t]
  11763. "--"
  11764. ["Build Full Customize Menu" org-create-customize-menu
  11765. (fboundp 'customize-menu-create)])
  11766. "--"
  11767. ["Refresh setup" org-mode-restart t]
  11768. ))
  11769. (defun org-info (&optional node)
  11770. "Read documentation for Org-mode in the info system.
  11771. With optional NODE, go directly to that node."
  11772. (interactive)
  11773. (require 'info)
  11774. (Info-goto-node (format "(org)%s" (or node ""))))
  11775. (defun org-install-agenda-files-menu ()
  11776. (let ((bl (buffer-list)))
  11777. (save-excursion
  11778. (while bl
  11779. (set-buffer (pop bl))
  11780. (if (eq major-mode 'org-mode) (setq bl nil)))
  11781. (when (eq major-mode 'org-mode)
  11782. (easy-menu-change
  11783. '("Org") "File List for Agenda"
  11784. (append
  11785. (list
  11786. ["Edit File List" (org-edit-agenda-file-list) t]
  11787. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11788. ["Remove Current File from List" org-remove-file t]
  11789. ["Cycle through agenda files" org-cycle-agenda-files t]
  11790. "--")
  11791. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11792. ;;; Documentation
  11793. (defun org-customize ()
  11794. "Call the customize function with org as argument."
  11795. (interactive)
  11796. (customize-browse 'org))
  11797. (defun org-create-customize-menu ()
  11798. "Create a full customization menu for Org-mode, insert it into the menu."
  11799. (interactive)
  11800. (if (fboundp 'customize-menu-create)
  11801. (progn
  11802. (easy-menu-change
  11803. '("Org") "Customize"
  11804. `(["Browse Org group" org-customize t]
  11805. "--"
  11806. ,(customize-menu-create 'org)
  11807. ["Set" Custom-set t]
  11808. ["Save" Custom-save t]
  11809. ["Reset to Current" Custom-reset-current t]
  11810. ["Reset to Saved" Custom-reset-saved t]
  11811. ["Reset to Standard Settings" Custom-reset-standard t]))
  11812. (message "\"Org\"-menu now contains full customization menu"))
  11813. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11814. ;;; Miscellaneous stuff
  11815. (defun org-move-line-down (arg)
  11816. "Move the current line down. With prefix argument, move it past ARG lines."
  11817. (interactive "p")
  11818. (let ((col (current-column))
  11819. beg end pos)
  11820. (beginning-of-line 1) (setq beg (point))
  11821. (beginning-of-line 2) (setq end (point))
  11822. (beginning-of-line (+ 1 arg))
  11823. (setq pos (move-marker (make-marker) (point)))
  11824. (insert (delete-and-extract-region beg end))
  11825. (goto-char pos)
  11826. (move-to-column col)))
  11827. (defun org-move-line-up (arg)
  11828. "Move the current line up. With prefix argument, move it past ARG lines."
  11829. (interactive "p")
  11830. (let ((col (current-column))
  11831. beg end pos)
  11832. (beginning-of-line 1) (setq beg (point))
  11833. (beginning-of-line 2) (setq end (point))
  11834. (beginning-of-line (- arg))
  11835. (setq pos (move-marker (make-marker) (point)))
  11836. (insert (delete-and-extract-region beg end))
  11837. (goto-char pos)
  11838. (move-to-column col)))
  11839. ;; Paragraph filling stuff.
  11840. ;; We want this to be just right, so use the full arsenal.
  11841. ;; FIXME: This very likely does not work correctly for XEmacs, because the
  11842. ;; filladapt package works slightly differently.
  11843. (defun org-set-autofill-regexps ()
  11844. (interactive)
  11845. ;; In the paragraph separator we include headlines, because filling
  11846. ;; text in a line directly attached to a headline would otherwise
  11847. ;; fill the headline as well.
  11848. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  11849. ;; The paragraph starter includes hand-formatted lists.
  11850. (set (make-local-variable 'paragraph-start)
  11851. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  11852. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  11853. ;; But only if the user has not turned off tables or fixed-width regions
  11854. (set (make-local-variable 'auto-fill-inhibit-regexp)
  11855. (concat "\\*\\|#"
  11856. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  11857. (concat
  11858. "\\|[ \t]*["
  11859. (if org-enable-table-editor "|" "")
  11860. (if org-enable-fixed-width-editor ":" "")
  11861. "]"))))
  11862. ;; We use our own fill-paragraph function, to make sure that tables
  11863. ;; and fixed-width regions are not wrapped. That function will pass
  11864. ;; through to `fill-paragraph' when appropriate.
  11865. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  11866. ;; Adaptive filling: To get full control, first make sure that
  11867. ;; `adaptive-fill-regexp' never matches. Then install our won matcher.
  11868. (setq adaptive-fill-regexp "\000")
  11869. (setq adaptive-fill-function 'org-adaptive-fill-function))
  11870. (defun org-fill-paragraph (&optional justify)
  11871. "Re-align a table, pass through to fill-paragraph if no table."
  11872. (let ((table-p (org-at-table-p))
  11873. (table.el-p (org-at-table.el-p)))
  11874. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  11875. (table.el-p t) ; skip table.el tables
  11876. (table-p (org-table-align) t) ; align org-mode tables
  11877. (t nil)))) ; call paragraph-fill
  11878. ;; For reference, this is the default value of adaptive-fill-regexp
  11879. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  11880. (defun org-adaptive-fill-function ()
  11881. "Return a fill prefix for org-mode files.
  11882. In particular, this makes sure hanging paragraphs for hand-formatted lists
  11883. work correctly."
  11884. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  11885. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  11886. ;; Functions needed for Emacs/XEmacs region compatibility
  11887. (defun org-add-hook (hook function &optional append local)
  11888. "Add-hook, compatible with both Emacsen."
  11889. (if (and local (featurep 'xemacs))
  11890. (add-local-hook hook function append)
  11891. (add-hook hook function append local)))
  11892. (defun org-region-active-p ()
  11893. "Is `transient-mark-mode' on and the region active?
  11894. Works on both Emacs and XEmacs."
  11895. (if org-ignore-region
  11896. nil
  11897. (if (featurep 'xemacs)
  11898. (and zmacs-regions (region-active-p))
  11899. (and transient-mark-mode mark-active))))
  11900. (defun org-add-to-invisibility-spec (arg)
  11901. "Add elements to `buffer-invisibility-spec'.
  11902. See documentation for `buffer-invisibility-spec' for the kind of elements
  11903. that can be added."
  11904. (cond
  11905. ((fboundp 'add-to-invisibility-spec)
  11906. (add-to-invisibility-spec arg))
  11907. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  11908. (setq buffer-invisibility-spec (list arg)))
  11909. (t
  11910. (setq buffer-invisibility-spec
  11911. (cons arg buffer-invisibility-spec)))))
  11912. (defun org-remove-from-invisibility-spec (arg)
  11913. "Remove elements from `buffer-invisibility-spec'."
  11914. (if (fboundp 'remove-from-invisibility-spec)
  11915. (remove-from-invisibility-spec arg)
  11916. (if (consp buffer-invisibility-spec)
  11917. (setq buffer-invisibility-spec
  11918. (delete arg buffer-invisibility-spec)))))
  11919. (defun org-in-invisibility-spec-p (arg)
  11920. "Is ARG a member of `buffer-invisibility-spec'?"
  11921. (if (consp buffer-invisibility-spec)
  11922. (member arg buffer-invisibility-spec)
  11923. nil))
  11924. (defun org-image-file-name-regexp ()
  11925. "Return regexp matching the file names of images."
  11926. (if (fboundp 'image-file-name-regexp)
  11927. (image-file-name-regexp)
  11928. (let ((image-file-name-extensions
  11929. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  11930. "xbm" "xpm" "pbm" "pgm" "ppm")))
  11931. (concat "\\."
  11932. (regexp-opt (nconc (mapcar 'upcase
  11933. image-file-name-extensions)
  11934. image-file-name-extensions)
  11935. t)
  11936. "\\'"))))
  11937. ;; Functions needed for compatibility with old outline.el.
  11938. ;; Programming for the old outline.el (that uses selective display
  11939. ;; instead of `invisible' text properties) is a nightmare, mostly
  11940. ;; because regular expressions can no longer be anchored at
  11941. ;; beginning/end of line. Therefore a number of function need special
  11942. ;; treatment when the old outline.el is being used.
  11943. ;; The following functions capture almost the entire compatibility code
  11944. ;; between the different versions of outline-mode. The only other
  11945. ;; places where this is important are the font-lock-keywords, and in
  11946. ;; `org-export-copy-visible'. Search for `org-noutline-p' to find them.
  11947. ;; C-a should go to the beginning of a *visible* line, also in the
  11948. ;; new outline.el. I guess this should be patched into Emacs?
  11949. (defun org-beginning-of-line ()
  11950. "Go to the beginning of the current line. If that is invisible, continue
  11951. to a visible line beginning. This makes the function of C-a more intuitive."
  11952. (interactive)
  11953. (beginning-of-line 1)
  11954. (if (bobp)
  11955. nil
  11956. (backward-char 1)
  11957. (if (org-invisible-p)
  11958. (while (and (not (bobp)) (org-invisible-p))
  11959. (backward-char 1)
  11960. (beginning-of-line 1))
  11961. (forward-char 1))))
  11962. (when org-noutline-p
  11963. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  11964. ;; FIXME: should I use substitute-key-definition to reach other bindings
  11965. ;; of beginning-of-line?
  11966. (defun org-invisible-p ()
  11967. "Check if point is at a character currently not visible."
  11968. (if org-noutline-p
  11969. ;; Early versions of noutline don't have `outline-invisible-p'.
  11970. (if (fboundp 'outline-invisible-p)
  11971. (outline-invisible-p)
  11972. (get-char-property (point) 'invisible))
  11973. (save-excursion
  11974. (skip-chars-backward "^\r\n")
  11975. (equal (char-before) ?\r))))
  11976. (defun org-invisible-p2 ()
  11977. "Check if point is at a character currently not visible."
  11978. (save-excursion
  11979. (if org-noutline-p
  11980. (progn
  11981. (if (and (eolp) (not (bobp))) (backward-char 1))
  11982. ;; Early versions of noutline don't have `outline-invisible-p'.
  11983. (if (fboundp 'outline-invisible-p)
  11984. (outline-invisible-p)
  11985. (get-char-property (point) 'invisible)))
  11986. (skip-chars-backward "^\r\n")
  11987. (equal (char-before) ?\r))))
  11988. (defun org-back-to-heading (&optional invisible-ok)
  11989. "Move to previous heading line, or beg of this line if it's a heading.
  11990. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  11991. (if org-noutline-p
  11992. (outline-back-to-heading invisible-ok)
  11993. (if (and (memq (char-before) '(?\n ?\r))
  11994. (looking-at outline-regexp))
  11995. t
  11996. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  11997. outline-regexp)
  11998. nil t)
  11999. (if invisible-ok
  12000. (progn (goto-char (match-end 1))
  12001. (looking-at outline-regexp)))
  12002. (error "Before first heading")))))
  12003. (defun org-on-heading-p (&optional invisible-ok)
  12004. "Return t if point is on a (visible) heading line.
  12005. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  12006. (if org-noutline-p
  12007. (outline-on-heading-p 'invisible-ok)
  12008. (save-excursion
  12009. (skip-chars-backward "^\n\r")
  12010. (and (looking-at outline-regexp)
  12011. (or invisible-ok
  12012. (bobp)
  12013. (equal (char-before) ?\n))))))
  12014. (defun org-on-target-p ()
  12015. (let ((pos (point)))
  12016. (save-excursion
  12017. (skip-chars-forward "<")
  12018. (and (re-search-backward "<<" nil t)
  12019. (or (looking-at org-target-regexp)
  12020. (looking-at org-radio-target-regexp))
  12021. (<= (match-beginning 0) pos)
  12022. (>= (match-end 0) pos)))))
  12023. (defun org-up-heading-all (arg)
  12024. "Move to the heading line of which the present line is a subheading.
  12025. This function considers both visible and invisible heading lines.
  12026. With argument, move up ARG levels."
  12027. (if org-noutline-p
  12028. (if (fboundp 'outline-up-heading-all)
  12029. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12030. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  12031. (org-back-to-heading t)
  12032. (looking-at outline-regexp)
  12033. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  12034. (error "Cannot move up %d levels" arg)
  12035. (re-search-backward
  12036. (concat "[\n\r]" (regexp-quote
  12037. (make-string (- (match-end 0) (match-beginning 0) arg)
  12038. ?*))
  12039. "[^*]"))
  12040. (forward-char 1))))
  12041. (defun org-show-hidden-entry ()
  12042. "Show an entry where even the heading is hidden."
  12043. (save-excursion
  12044. (if (not org-noutline-p)
  12045. (progn
  12046. (org-back-to-heading t)
  12047. (org-flag-heading nil)))
  12048. (org-show-entry)))
  12049. (defun org-check-occur-regexp (regexp)
  12050. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  12051. Of course, only for the old outline mode."
  12052. (if org-noutline-p
  12053. regexp
  12054. (if (string-match "^\\^" regexp)
  12055. (concat "[\n\r]" (substring regexp 1))
  12056. regexp)))
  12057. (defun org-flag-heading (flag &optional entry)
  12058. "Flag the current heading. FLAG non-nil means make invisible.
  12059. When ENTRY is non-nil, show the entire entry."
  12060. (save-excursion
  12061. (org-back-to-heading t)
  12062. (if (not org-noutline-p)
  12063. ;; Make the current headline visible
  12064. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  12065. ;; Check if we should show the entire entry
  12066. (if entry
  12067. (progn
  12068. (org-show-entry)
  12069. (save-excursion ;; FIXME: Is this the fix for points in the -|
  12070. ;; middle of text? |
  12071. (and (outline-next-heading) ;; |
  12072. (org-flag-heading nil)))) ; show the next heading _|
  12073. (outline-flag-region (max 1 (1- (point)))
  12074. (save-excursion (outline-end-of-heading) (point))
  12075. (if org-noutline-p
  12076. flag
  12077. (if flag ?\r ?\n))))))
  12078. (defun org-end-of-subtree (&optional invisible-OK)
  12079. ;; This is an exact copy of the original function, but it uses
  12080. ;; `org-back-to-heading', to make it work also in invisible
  12081. ;; trees. And is uses an invisible-OK argument.
  12082. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12083. (org-back-to-heading invisible-OK)
  12084. (let ((first t)
  12085. (level (funcall outline-level)))
  12086. (while (and (not (eobp))
  12087. (or first (> (funcall outline-level) level)))
  12088. (setq first nil)
  12089. (outline-next-heading))
  12090. (if (memq (preceding-char) '(?\n ?\^M))
  12091. (progn
  12092. ;; Go to end of line before heading
  12093. (forward-char -1)
  12094. (if (memq (preceding-char) '(?\n ?\^M))
  12095. ;; leave blank line before heading
  12096. (forward-char -1))))))
  12097. (defun org-show-subtree ()
  12098. "Show everything after this heading at deeper levels."
  12099. (outline-flag-region
  12100. (point)
  12101. (save-excursion
  12102. (outline-end-of-subtree) (outline-next-heading) (point))
  12103. (if org-noutline-p nil ?\n)))
  12104. (defun org-show-entry ()
  12105. "Show the body directly following this heading.
  12106. Show the heading too, if it is currently invisible."
  12107. (interactive)
  12108. (save-excursion
  12109. (org-back-to-heading t)
  12110. (outline-flag-region
  12111. (1- (point))
  12112. (save-excursion
  12113. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12114. (or (match-beginning 1) (point-max)))
  12115. (if org-noutline-p nil ?\n))))
  12116. (defun org-make-options-regexp (kwds)
  12117. "Make a regular expression for keyword lines."
  12118. (concat
  12119. (if org-noutline-p "^" "[\n\r]")
  12120. "#?[ \t]*\\+\\("
  12121. (mapconcat 'regexp-quote kwds "\\|")
  12122. "\\):[ \t]*"
  12123. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  12124. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12125. (eval-after-load "bookmark"
  12126. '(if (boundp 'bookmark-after-jump-hook)
  12127. ;; We can use the hook
  12128. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12129. ;; Hook not available, use advice
  12130. (defadvice bookmark-jump (after org-make-visible activate)
  12131. "Make the position visible."
  12132. (org-bookmark-jump-unhide))))
  12133. (defun org-bookmark-jump-unhide ()
  12134. "Unhide the current position, to show the bookmark location."
  12135. (and (eq major-mode 'org-mode)
  12136. (or (org-invisible-p)
  12137. (save-excursion (goto-char (max (point-min) (1- (point))))
  12138. (org-invisible-p)))
  12139. (org-show-hierarchy-above)))
  12140. ;;; Finish up
  12141. (provide 'org)
  12142. (run-hooks 'org-load-hook)
  12143. ;; Experimental code
  12144. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12145. ;;; org.el ends here