org.el 488 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348
  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.19a
  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.20
  83. ;; - Links use now the [[link][description]] format by default.
  84. ;; When inserting links, the user is prompted for a description.
  85. ;; - If a link has a description, only the description is displayed
  86. ;; the link part is hidden. Use C-c C-l to edit the link part.
  87. ;; - TAGS are now bold, but in the same color as the headline.
  88. ;;
  89. ;; Version 4.13
  90. ;; - The list of agenda files can be maintainted in an external file.
  91. ;; - Bug fixes.
  92. ;;
  93. ;; Version 4.12
  94. ;; - Templates for remember buffer. Note that the remember setup changes.
  95. ;; To set up templates, see `org-remember-templates'.
  96. ;; - The time in new time stamps can be rounded, see new option
  97. ;; `org-time-stamp-rounding-minutes'.
  98. ;; - Bug fixes (there are *always* more bugs).
  99. ;;
  100. ;; Version 4.10
  101. ;; - Bug fixes.
  102. ;;
  103. ;; Version 4.09
  104. ;; - Bug fixes.
  105. ;; - Small improvements to font-lock support.
  106. ;; - MHE support finalized.
  107. ;;
  108. ;; Version 4.08
  109. ;; - Bug fixes.
  110. ;; - Improved MHE support
  111. ;;
  112. ;; Version 4.07
  113. ;; - Bug fixes.
  114. ;; - Leading stars in headlines can be hidden, so make the outline look
  115. ;; cleaner.
  116. ;; - Mouse-1 can be used to follow links.
  117. ;;
  118. ;; Version 4.06
  119. ;; - HTML exporter treats targeted internal links.
  120. ;; - Bug fixes.
  121. ;;
  122. ;; Version 4.05
  123. ;; - Changes to internal link system (thanks to David Wainberg for ideas).
  124. ;; - in-file links: [[Search String]] instead of <file:::Search String>
  125. ;; - automatic links to "radio targets".
  126. ;; - CamelCase not longer active by default, configure org-activate-camels
  127. ;; if you want to turn it back on.
  128. ;; - After following a link, `C-c &' jumps back to it.
  129. ;; - MH-E link support (thanks to Thomas Baumann).
  130. ;; - Special table lines are no longer exported.
  131. ;; - Bug fixes and minor improvements.
  132. ;;
  133. ;; Version 4.04
  134. ;; - Cleanup tags display in agenda.
  135. ;; - Bug fixes.
  136. ;;
  137. ;; Version 4.03
  138. ;; - Table alignment fixed for use with wide characters.
  139. ;; - `C-c -' leaves cursor in current table line.
  140. ;; - The current TAG can be incorporated into the agenda prefix.
  141. ;; See option `org-agenda-prefix-format' for details.
  142. ;;
  143. ;; Version 4.02
  144. ;; - Minor bug fixes and improvements around tag searches.
  145. ;; - XEmacs compatibility fixes.
  146. ;;
  147. ;; Version 4.01
  148. ;; - Tags can also be set remotely from agenda buffer.
  149. ;; - Boolean logic for tag searches.
  150. ;; - Additional agenda commands can be configured through the variable
  151. ;; `org-agenda-custom-commands'.
  152. ;; - Minor bug fixes.
  153. ;;
  154. ;;; Code:
  155. (eval-when-compile (require 'cl) (require 'calendar))
  156. (require 'outline)
  157. (require 'time-date)
  158. (require 'easymenu)
  159. (defvar calc-embedded-close-formula) ; defined by the calc package
  160. (defvar calc-embedded-open-formula) ; defined by the calc package
  161. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  162. ;;; Customization variables
  163. (defvar org-version "4.19a"
  164. "The version number of the file org.el.")
  165. (defun org-version ()
  166. (interactive)
  167. (message "Org-mode version %s" org-version))
  168. ;; The following constant is for compatibility with different versions
  169. ;; of outline.el.
  170. (defconst org-noutline-p (featurep 'noutline)
  171. "Are we using the new outline mode?")
  172. (defconst org-format-transports-properties-p
  173. (let ((x "a"))
  174. (add-text-properties 0 1 '(test t) x)
  175. (get-text-property 0 'test (format "%s" x)))
  176. "Does format transport text properties?")
  177. (defgroup org nil
  178. "Outline-based notes management and organizer."
  179. :tag "Org"
  180. :group 'outlines
  181. :group 'hypermedia
  182. :group 'calendar)
  183. (defgroup org-startup nil
  184. "Options concerning startup of Org-mode."
  185. :tag "Org Startup"
  186. :group 'org)
  187. (defcustom org-startup-folded t
  188. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  189. This can also be configured on a per-file basis by adding one of
  190. the following lines anywhere in the buffer:
  191. #+STARTUP: fold
  192. #+STARTUP: nofold
  193. #+STARTUP: content"
  194. :group 'org-startup
  195. :type '(choice
  196. (const :tag "nofold: show all" nil)
  197. (const :tag "fold: overview" t)
  198. (const :tag "content: all headlines" content)))
  199. (defcustom org-startup-truncated t
  200. "Non-nil means, entering Org-mode will set `truncate-lines'.
  201. This is useful since some lines containing links can be very long and
  202. uninteresting. Also tables look terrible when wrapped."
  203. :group 'org-startup
  204. :type 'boolean)
  205. (defcustom org-startup-align-all-tables nil
  206. "Non-nil means, allign all tables when visiting a file.
  207. This is useful when the column width in tables is forced with <N> cookies
  208. in table fields. Such tables will look correct only after the first re-align."
  209. :group 'org-startup
  210. :type 'boolean)
  211. (defcustom org-startup-with-deadline-check nil
  212. "Non-nil means, entering Org-mode will run the deadline check.
  213. This means, if you start editing an org file, you will get an
  214. immediate reminder of any due deadlines.
  215. This can also be configured on a per-file basis by adding one of
  216. the following lines anywhere in the buffer:
  217. #+STARTUP: dlcheck
  218. #+STARTUP: nodlcheck"
  219. :group 'org-startup
  220. :type 'boolean)
  221. (defcustom org-insert-mode-line-in-empty-file nil
  222. "Non-nil means insert the first line setting Org-mode in empty files.
  223. When the function `org-mode' is called interactively in an empty file, this
  224. normally means that the file name does not automatically trigger Org-mode.
  225. To ensure that the file will always be in Org-mode in the future, a
  226. line enforcing Org-mode will be inserted into the buffer, if this option
  227. has been set."
  228. :group 'org-startup
  229. :type 'boolean)
  230. (defcustom org-CUA-compatible nil
  231. "Non-nil means use alternative key bindings for S-<cursor movement>.
  232. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  233. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  234. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  235. alternative bindings. Setting this variable to t will replace the following
  236. keys both in Org-mode and in the Org-agenda buffer.
  237. S-RET -> C-S-RET
  238. S-up -> M-p
  239. S-down -> M-n
  240. S-left -> M--
  241. S-right -> M-+
  242. If you do not like the alternative keys, take a look at the variable
  243. `org-disputed-keys'.
  244. This option is only relevant at load-time of Org-mode. Changing it requires
  245. a restart of Emacs to become effective."
  246. :group 'org-startup
  247. :type 'boolean)
  248. (defvar org-disputed-keys
  249. '((S-up [(shift up)] [(meta ?p)])
  250. (S-down [(shift down)] [(meta ?n)])
  251. (S-left [(shift left)] [(meta ?-)])
  252. (S-right [(shift right)] [(meta ?+)])
  253. (S-return [(shift return)] [(control shift return)]))
  254. "Keys for which Org-mode and other modes compete.
  255. This is an alist, cars are symbols for lookup, 1st element is the default key,
  256. second element will be used when `org-CUA-compatible' is t.")
  257. (defun org-key (key)
  258. "Select a key according to `org-CUA-compatible'."
  259. (nth (if org-CUA-compatible 2 1)
  260. (or (assq key org-disputed-keys)
  261. (error "Invalid Key %s in `org-key'" key))))
  262. (defgroup org-keywords nil
  263. "Options concerning TODO items in Org-mode."
  264. :tag "Org Keywords"
  265. :group 'org)
  266. (defcustom org-todo-keywords '("TODO" "DONE")
  267. "List of TODO entry keywords.
  268. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  269. considered to mean that the entry is \"done\". All the other mean that
  270. action is required, and will make the entry show up in todo lists, diaries
  271. etc.
  272. The command \\[org-todo] cycles an entry through these states, and an
  273. additional state where no keyword is present. For details about this
  274. cycling, see also the variable `org-todo-interpretation'
  275. Changes become only effective after restarting Emacs."
  276. :group 'org-keywords
  277. :type '(repeat (string :tag "Keyword")))
  278. (defcustom org-todo-interpretation 'sequence
  279. "Controls how TODO keywords are interpreted.
  280. \\<org-mode-map>Possible values are `sequence' and `type'.
  281. This variable is only relevant if `org-todo-keywords' contains more than two
  282. states. There are two ways how these keywords can be used:
  283. - As a sequence in the process of working on a TODO item, for example
  284. (setq org-todo-keywords '(\"TODO\" \"STARTED\" \"VERIFY\" \"DONE\")
  285. org-todo-interpretation 'sequence)
  286. - As different types of TODO items, for example
  287. (setq org-todo-keywords '(\"URGENT\" \"RELAXED\" \"REMIND\" \"FOR_TOM\" \"DONE\")
  288. org-todo-interpretation 'type)
  289. When the states are interpreted as a sequence, \\[org-todo] always cycles
  290. to the next state, in order to walk through all different states. So with
  291. \\[org-todo], you turn an empty entry into the state TODO. When you started
  292. working on the item, you use \\[org-todo] again to switch it to \"STARTED\",
  293. later to VERIFY and finally to DONE.
  294. When the states are interpreted as types, \\[org-todo] still cycles through
  295. when it is called several times in direct succession, in order to initially
  296. select the type. However, if not called immediately after a previous
  297. \\[org-todo], it switches from each type directly to DONE. So with the
  298. above example, you could use `\\[org-todo] \\[org-todo]' to label an entry
  299. RELAXED. If you later return to this entry and press \\[org-todo] again,
  300. RELAXED will not be changed REMIND, but directly to DONE.
  301. You can create a large number of types. To initially select a
  302. type, it is then best to use \\[universal-argument] \\[org-todo] in order to specify the
  303. type with completion. Of course, you can also type the keyword
  304. directly into the buffer. M-TAB completes TODO keywords at the
  305. beginning of a headline."
  306. :group 'org-keywords
  307. :type '(choice (const sequence)
  308. (const type)))
  309. (defcustom org-deadline-string "DEADLINE:"
  310. "String to mark deadline entries.
  311. A deadline is this string, followed by a time stamp. Should be a word,
  312. terminated by a colon. You can insert a schedule keyword and
  313. a timestamp with \\[org-deadline].
  314. Changes become only effective after restarting Emacs."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defcustom org-scheduled-string "SCHEDULED:"
  318. "String to mark scheduled TODO entries.
  319. A schedule is this string, followed by a time stamp. Should be a word,
  320. terminated by a colon. You can insert a schedule keyword and
  321. a timestamp with \\[org-schedule].
  322. Changes become only effective after restarting Emacs."
  323. :group 'org-keywords
  324. :type 'string)
  325. (defcustom org-closed-string "CLOSED:"
  326. "String used as the prefix for timestamps logging closing a TODO entry."
  327. :group 'org-keywords
  328. :type 'string)
  329. (defcustom org-comment-string "COMMENT"
  330. "Entries starting with this keyword will never be exported.
  331. An entry can be toggled between COMMENT and normal with
  332. \\[org-toggle-comment].
  333. Changes become only effective after restarting Emacs."
  334. :group 'org-keywords
  335. :type 'string)
  336. (defcustom org-quote-string "QUOTE"
  337. "Entries starting with this keyword will be exported in fixed-width font.
  338. Quoting applies only to the text in the entry following the headline, and does
  339. not extend beyond the next headline, even if that is lower level.
  340. An entry can be toggled between QUOTE and normal with
  341. \\[org-toggle-fixed-width-section]."
  342. :group 'org-keywords
  343. :type 'string)
  344. (defcustom org-after-todo-state-change-hook nil
  345. "Hook which is run after the state of a TODO item was changed.
  346. The new state (a string with a TODO keyword, or nil) is available in the
  347. Lisp variable `state'."
  348. :group 'org-keywords
  349. :type 'hook)
  350. (defcustom org-default-priority ?B
  351. "The default priority of TODO items.
  352. This is the priority an item get if no explicit priority is given."
  353. :group 'org-keywords
  354. :type 'character)
  355. (defcustom org-lowest-priority ?C
  356. "The lowest priority of TODO items. A character like ?A, ?B etc."
  357. :group 'org-keywords
  358. :type 'character)
  359. ;; Variables for pre-computed regular expressions, all buffer local
  360. (defvar org-todo-kwd-priority-p nil
  361. "Do TODO items have priorities?")
  362. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  363. (defvar org-todo-kwd-max-priority nil
  364. "Maximum priority of TODO items.")
  365. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  366. (defvar org-ds-keyword-length 12
  367. "Maximum length of the Deadline and SCHEDULED keywords.")
  368. (make-variable-buffer-local 'org-ds-keyword-length)
  369. (defvar org-done-string nil
  370. "The last string in `org-todo-keywords', indicating an item is DONE.")
  371. (make-variable-buffer-local 'org-done-string)
  372. (defvar org-todo-regexp nil
  373. "Matches any of the TODO state keywords.")
  374. (make-variable-buffer-local 'org-todo-regexp)
  375. (defvar org-not-done-regexp nil
  376. "Matches any of the TODO state keywords except the last one.")
  377. (make-variable-buffer-local 'org-not-done-regexp)
  378. (defvar org-todo-line-regexp nil
  379. "Matches a headline and puts TODO state into group 2 if present.")
  380. (make-variable-buffer-local 'org-todo-line-regexp)
  381. (defvar org-nl-done-regexp nil
  382. "Matches newline followed by a headline with the DONE keyword.")
  383. (make-variable-buffer-local 'org-nl-done-regexp)
  384. (defvar org-looking-at-done-regexp nil
  385. "Matches the DONE keyword a point.")
  386. (make-variable-buffer-local 'org-looking-at-done-regexp)
  387. (defvar org-deadline-regexp nil
  388. "Matches the DEADLINE keyword.")
  389. (make-variable-buffer-local 'org-deadline-regexp)
  390. (defvar org-deadline-time-regexp nil
  391. "Matches the DEADLINE keyword together with a time stamp.")
  392. (make-variable-buffer-local 'org-deadline-time-regexp)
  393. (defvar org-deadline-line-regexp nil
  394. "Matches the DEADLINE keyword and the rest of the line.")
  395. (make-variable-buffer-local 'org-deadline-line-regexp)
  396. (defvar org-scheduled-regexp nil
  397. "Matches the SCHEDULED keyword.")
  398. (make-variable-buffer-local 'org-scheduled-regexp)
  399. (defvar org-scheduled-time-regexp nil
  400. "Matches the SCHEDULED keyword together with a time stamp.")
  401. (make-variable-buffer-local 'org-scheduled-time-regexp)
  402. (defvar org-category nil
  403. "Variable used by org files to set a category for agenda display.
  404. Such files should use a file variable to set it, for example
  405. -*- mode: org; org-category: \"ELisp\"
  406. or contain a special line
  407. #+CATEGORY: ELisp
  408. If the file does not specify a category, then file's base name
  409. is used instead.")
  410. (make-variable-buffer-local 'org-category)
  411. (defgroup org-time nil
  412. "Options concerning time stamps and deadlines in Org-mode."
  413. :tag "Org Time"
  414. :group 'org)
  415. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  416. "Formats for `format-time-string' which are used for time stamps.
  417. It is not recommended to change this constant.")
  418. (defcustom org-time-stamp-rounding-minutes 0
  419. "Number of minutes to round time stamps to upon insertion.
  420. When zero, insert the time unmodified. Useful rounding numbers
  421. should be factors of 60, so for example 5, 10, 15.
  422. When this is not zero, you can still force an exact time-stamp by using
  423. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  424. :group 'org-time
  425. :type 'integer)
  426. (defcustom org-deadline-warning-days 30
  427. "No. of days before expiration during which a deadline becomes active.
  428. This variable governs the display in the org file."
  429. :group 'org-time
  430. :type 'number)
  431. (defcustom org-popup-calendar-for-date-prompt t
  432. "Non-nil means, pop up a calendar when prompting for a date.
  433. In the calendar, the date can be selected with mouse-1. However, the
  434. minibuffer will also be active, and you can simply enter the date as well.
  435. When nil, only the minibuffer will be available."
  436. :group 'org-time
  437. :type 'boolean)
  438. (defcustom org-calendar-follow-timestamp-change t
  439. "Non-nil means, make the calendar window follow timestamp changes.
  440. When a timestamp is modified and the calendar window is visible, it will be
  441. moved to the new date."
  442. :group 'org-time
  443. :type 'boolean)
  444. (defcustom org-log-done nil
  445. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  446. When the state of an entry is changed from nothing to TODO, remove a previous
  447. closing date."
  448. :group 'org-time
  449. :type 'boolean)
  450. (defgroup org-agenda nil
  451. "Options concerning agenda display Org-mode."
  452. :tag "Org Agenda"
  453. :group 'org)
  454. (defcustom org-agenda-files nil
  455. "The files to be used for agenda display.
  456. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  457. \\[org-remove-file]. You can also use customize to edit the list.
  458. If the value of the variable is not a list but a single file name, then
  459. the list of agenda files is actually stored in that file, one agenda file
  460. per line"
  461. :group 'org-agenda
  462. :type '(choice
  463. (repeat :tag "List of files" file)
  464. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  465. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  466. "Custom commands for the agenda.
  467. These commands will be offered on the splash screen displayed by the
  468. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  469. key The key (a single char as a string) to be associated with the command.
  470. type The command type, any of the following symbols:
  471. todo Entries with a specific TODO keyword, in all agenda files.
  472. tags Tags match in all agenda files.
  473. todo-tree Sparse tree of specific TODO keyword in *current* file.
  474. tags-tree Sparse tree with all tags matches in *current* file.
  475. occur-tree Occur sparse tree for current file.
  476. match What to search for:
  477. - a single keyword for TODO keyword searches
  478. - a tags match expression for tags searches
  479. - a regular expression for occur searches"
  480. :group 'org-agenda
  481. :type '(repeat
  482. (list (string :tag "Key")
  483. (choice :tag "Type"
  484. (const :tag "Tags search in all agenda files" tags)
  485. (const :tag "TODO keyword search in all agenda files" todo)
  486. (const :tag "Tags sparse tree in current buffer" tags-tree)
  487. (const :tag "TODO keyword tree in current buffer" todo-tree)
  488. (const :tag "Occur tree in current buffer" occur-tree))
  489. (string :tag "Match"))))
  490. (defcustom org-select-timeline-window t
  491. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  492. When nil, cursor will remain in the current window."
  493. :group 'org-agenda
  494. :type 'boolean)
  495. (defcustom org-select-agenda-window t
  496. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  497. When nil, cursor will remain in the current window."
  498. :group 'org-agenda
  499. :type 'boolean)
  500. (defcustom org-fit-agenda-window t
  501. "Non-nil means, change window size of agenda to fit content."
  502. :group 'org-agenda
  503. :type 'boolean)
  504. (defcustom org-agenda-show-all-dates t
  505. "Non-nil means, `org-agenda' shows every day in the selected range.
  506. When nil, only the days which actually have entries are shown."
  507. :group 'org-agenda
  508. :type 'boolean)
  509. ;; FIXME: First day of month works only for current month because it would
  510. ;; require a variable ndays treatment.
  511. (defcustom org-agenda-start-on-weekday 1
  512. "Non-nil means, start the overview always on the specified weekday.
  513. 0 Denotes Sunday, 1 denotes Monday etc.
  514. When nil, always start on the current day."
  515. :group 'org-agenda
  516. :type '(choice (const :tag "Today" nil)
  517. (const :tag "First day of month" t)
  518. (number :tag "Weekday No.")))
  519. (defcustom org-agenda-ndays 7
  520. "Number of days to include in overview display."
  521. :group 'org-agenda
  522. :type 'number)
  523. (defcustom org-agenda-include-all-todo t
  524. "Non-nil means, the agenda will always contain all TODO entries.
  525. When nil, date-less entries will only be shown if `org-agenda' is called
  526. with a prefix argument.
  527. When non-nil, the TODO entries will be listed at the top of the agenda, before
  528. the entries for specific days."
  529. :group 'org-agenda
  530. :type 'boolean)
  531. (defcustom org-agenda-include-diary nil
  532. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  533. :group 'org-agenda
  534. :type 'boolean)
  535. (defcustom org-calendar-to-agenda-key [?c]
  536. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  537. The command `org-calendar-goto-agenda' will be bound to this key. The
  538. default is the character `c' because then `c' can be used to switch back and
  539. forth between agenda and calendar."
  540. :group 'org-agenda
  541. :type 'sexp)
  542. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  543. "Sorting structure for the agenda items of a single day.
  544. This is a list of symbols which will be used in sequence to determine
  545. if an entry should be listed before another entry. The following
  546. symbols are recognized:
  547. time-up Put entries with time-of-day indications first, early first
  548. time-down Put entries with time-of-day indications first, late first
  549. category-keep Keep the default order of categories, corresponding to the
  550. sequence in `org-agenda-files'.
  551. category-up Sort alphabetically by category, A-Z.
  552. category-down Sort alphabetically by category, Z-A.
  553. priority-up Sort numerically by priority, high priority last.
  554. priority-down Sort numerically by priority, high priority first.
  555. The different possibilities will be tried in sequence, and testing stops
  556. if one comparison returns a \"not-equal\". For example, the default
  557. '(time-up category-keep priority-down)
  558. means: Pull out all entries having a specified time of day and sort them,
  559. in order to make a time schedule for the current day the first thing in the
  560. agenda listing for the day. Of the entries without a time indication, keep
  561. the grouped in categories, don't sort the categories, but keep them in
  562. the sequence given in `org-agenda-files'. Within each category sort by
  563. priority.
  564. Leaving out `category-keep' would mean that items will be sorted across
  565. categories by priority."
  566. :group 'org-agenda
  567. :type '(repeat
  568. (choice
  569. (const time-up)
  570. (const time-down)
  571. (const category-keep)
  572. (const category-up)
  573. (const category-down)
  574. (const priority-up)
  575. (const priority-down))))
  576. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  577. "Format specification for the prefix of items in the agenda buffer.
  578. This format works similar to a printf format, with the following meaning:
  579. %c the category of the item, \"Diary\" for entries from the diary, or
  580. as given by the CATEGORY keyword or derived from the file name.
  581. %T the first tag of the item.
  582. %t the time-of-day specification if one applies to the entry, in the
  583. format HH:MM
  584. %s Scheduling/Deadline information, a short string
  585. All specifiers work basically like the standard `%s' of printf, but may
  586. contain two additional characters: A question mark just after the `%' and
  587. a whitespace/punctuation character just before the final letter.
  588. If the first character after `%' is a question mark, the entire field
  589. will only be included if the corresponding value applies to the
  590. current entry. This is useful for fields which should have fixed
  591. width when present, but zero width when absent. For example,
  592. \"%?-12t\" will result in a 12 character time field if a time of the
  593. day is specified, but will completely disappear in entries which do
  594. not contain a time.
  595. If there is punctuation or whitespace character just before the final
  596. format letter, this character will be appended to the field value if
  597. the value is not empty. For example, the format \"%-12:c\" leads to
  598. \"Diary: \" if the category is \"Diary\". If the category were be
  599. empty, no additional colon would be interted.
  600. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  601. - Indent the line with two space characters
  602. - Give the category in a 12 chars wide field, padded with whitespace on
  603. the right (because of `-'). Append a colon if there is a category
  604. (because of `:').
  605. - If there is a time-of-day, put it into a 12 chars wide field. If no
  606. time, don't put in an empty field, just skip it (because of '?').
  607. - Finally, put the scheduling information and append a whitespace.
  608. As another example, if you don't want the time-of-day of entries in
  609. the prefix, you could use:
  610. (setq org-agenda-prefix-format \" %-11:c% s\")
  611. See also the variables `org-agenda-remove-times-when-in-prefix' and
  612. `org-agenda-remove-tags-when-in-prefix'."
  613. :type 'string
  614. :group 'org-agenda)
  615. (defcustom org-timeline-prefix-format " % s"
  616. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  617. :type 'string
  618. :group 'org-agenda)
  619. (defvar org-prefix-format-compiled nil
  620. "The compiled version of the most recently used prefix format.
  621. Depending on which command was used last, this may be the compiled version
  622. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  623. (defcustom org-agenda-use-time-grid t
  624. "Non-nil means, show a time grid in the agenda schedule.
  625. A time grid is a set of lines for specific times (like every two hours between
  626. 8:00 and 20:00). The items scheduled for a day at specific times are
  627. sorted in between these lines.
  628. For details about when the grid will be shown, and what it will look like, see
  629. the variable `org-agenda-time-grid'."
  630. :group 'org-agenda
  631. :type 'boolean)
  632. (defcustom org-agenda-time-grid
  633. '((daily today require-timed)
  634. "----------------"
  635. (800 1000 1200 1400 1600 1800 2000))
  636. "The settings for time grid for agenda display.
  637. This is a list of three items. The first item is again a list. It contains
  638. symbols specifying conditions when the grid should be displayed:
  639. daily if the agenda shows a single day
  640. weekly if the agenda shows an entire week
  641. today show grid on current date, independent of daily/weekly display
  642. require-timed show grid only if at least on item has a time specification
  643. The second item is a string which will be places behing the grid time.
  644. The third item is a list of integers, indicating the times that should have
  645. a grid line."
  646. :group 'org-agenda
  647. :type
  648. '(list
  649. (set :greedy t :tag "Grid Display Options"
  650. (const :tag "Show grid in single day agenda display" daily)
  651. (const :tag "Show grid in weekly agenda display" weekly)
  652. (const :tag "Always show grid for today" today)
  653. (const :tag "Show grid only if any timed entries are present"
  654. require-timed)
  655. (const :tag "Skip grid times already present in an entry"
  656. remove-match))
  657. (string :tag "Grid String")
  658. (repeat :tag "Grid Times" (integer :tag "Time"))))
  659. (defcustom org-agenda-remove-times-when-in-prefix t
  660. "Non-nil means, remove duplicate time specifications in agenda items.
  661. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  662. time-of-day specification in a headline or diary entry is extracted and
  663. placed into the prefix. If this option is non-nil, the original specification
  664. \(a timestamp or -range, or just a plain time(range) specification like
  665. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  666. cluttered.
  667. The option can be t or nil. It may also be the symbol `beg', indicating
  668. that the time should only be removed what it is located at the beginning of
  669. the headline/diary entry."
  670. :group 'org-agenda
  671. :type '(choice
  672. (const :tag "Always" t)
  673. (const :tag "Never" nil)
  674. (const :tag "When at beginning of entry" beg)))
  675. (defcustom org-sort-agenda-notime-is-late t
  676. "Non-nil means, items without time are considered late.
  677. This is only relevant for sorting. When t, items which have no explicit
  678. time like 15:30 will be considered as 24:01, i.e. later than any items which
  679. do have a time. When nil, the default time is before 0:00. You can use this
  680. option to decide if the schedule for today should come before or after timeless
  681. agenda entries."
  682. :group 'org-agenda
  683. :type 'boolean)
  684. (defcustom org-agenda-remove-tags-when-in-prefix nil
  685. "Non-nil means, the tags from copy of headline in agenda.
  686. When this is the symbol `prefix', only remove tags when
  687. `org-agenda-prefix-format' contains a `%T' specifier."
  688. :group 'org-agenda
  689. :type '(choice
  690. (const :tag "Always" t)
  691. (const :tag "Never" nil)
  692. (const :tag "When prefix format contains %T" prefix)))
  693. (defgroup org-structure nil
  694. "Options concerning structure editing in Org-mode."
  695. :tag "Org Structure"
  696. :group 'org)
  697. (defcustom org-cycle-include-plain-lists nil
  698. "Non-nil means, include plain lists into visibility cycling.
  699. This means that during cycling, plain list items will *temporarily* be
  700. interpreted as outline headlines with a level given by 1000+i where i is the
  701. indentation of the bullet. In all other operations, plain list items are
  702. not seen as headlines. For example, you cannot assign a TODO keyword to
  703. such an item."
  704. :group 'org-structure
  705. :type 'boolean)
  706. (defcustom org-cycle-emulate-tab t
  707. "Where should `org-cycle' emulate TAB.
  708. nil Never
  709. white Only in completely white lines
  710. t Everywhere except in headlines"
  711. :group 'org-structure
  712. :type '(choice (const :tag "Never" nil)
  713. (const :tag "Only in completely white lines" white)
  714. (const :tag "Everywhere except in headlines" t)
  715. ))
  716. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  717. "Hook that is run after `org-cycle' has changed the buffer visibility.
  718. The function(s) in this hook must accept a single argument which indicates
  719. the new state that was set by the most recent `org-cycle' command. The
  720. argument is a symbol. After a global state change, it can have the values
  721. `overview', `content', or `all'. After a local state change, it can have
  722. the values `folded', `children', or `subtree'."
  723. :group 'org-structure
  724. :type 'hook)
  725. (defcustom org-highlight-sparse-tree-matches t
  726. "Non-nil means, highlight all matches that define a sparse tree.
  727. The highlights will automatically disappear the next time the buffer is
  728. changed by an edit command."
  729. :group 'org-structure
  730. :type 'boolean)
  731. (defcustom org-show-hierarchy-above t
  732. "Non-nil means, show full hierarchy when showing a spot in the tree.
  733. Turning this off makes sparse trees more compact, but also less clear."
  734. :group 'org-structure
  735. :type 'boolean)
  736. (defcustom org-show-following-heading t
  737. "Non-nil means, show heading following match in `org-occur'.
  738. When doing an `org-occur' it is useful to show the headline which
  739. follows the match, even if they do not match the regexp. This makes it
  740. easier to edit directly inside the sparse tree. However, if you use
  741. `org-occur' mainly as an overview, the following headlines are
  742. unnecessary clutter."
  743. :group 'org-structure
  744. :type 'boolean)
  745. (defcustom org-occur-hook '(org-first-headline-recenter)
  746. "Hook that is run after `org-occur' has constructed a sparse tree.
  747. This can be used to recenter the window to show as much of the structure
  748. as possible."
  749. :group 'org-structure
  750. :type 'hook)
  751. (defcustom org-odd-levels-only nil
  752. "Non-nil means, skip even levels and only use odd levels for the outline.
  753. This has the effect that two stars are being added/taken away in
  754. promotion/demotion commands. It also influences how levels are
  755. handled by the exporters.
  756. Changing it requires restart of `font-lock-mode' to become effective
  757. for fontification also in regions already fontified."
  758. :group 'org-structure
  759. :group 'org-font-lock
  760. :type 'boolean)
  761. (defcustom org-adapt-indentation t
  762. "Non-nil means, adapt indentation when promoting and demoting.
  763. When this is set and the *entire* text in an entry is indented, the
  764. indentation is increased by one space in a demotion command, and
  765. decreased by one in a promotion command. If any line in the entry
  766. body starts at column 0, indentation is not changed at all."
  767. :group 'org-structure
  768. :type 'boolean)
  769. (defcustom org-plain-list-ordered-item-terminator t
  770. "The character that makes a line with leading number an ordered list item.
  771. Valid values are ?. and ?\). To get both terminators, use t. While
  772. ?. may look nicer, it creates the danger that a line with leading
  773. number may be incorrectly interpreted as an item. ?\) therefore is
  774. the safe choice."
  775. :group 'org-structure
  776. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  777. (const :tag "paren like in \"2)\"" ?\))
  778. (const :tab "both" t)))
  779. (defcustom org-auto-renumber-ordered-lists t
  780. "Non-nil means, automatically renumber ordered plain lists.
  781. Renumbering happens when the sequence have been changed with
  782. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  783. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  784. :group 'org-structure
  785. :type 'boolean)
  786. (defcustom org-enable-fixed-width-editor t
  787. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  788. This currently only means, they are never auto-wrapped.
  789. When nil, such lines will be treated like ordinary lines.
  790. See also the QUOTE keyword."
  791. :group 'org-structure
  792. :type 'boolean)
  793. (defgroup org-archive nil
  794. "Options concerning archiving in Org-mode."
  795. :tag "Org Archive"
  796. :group 'org)
  797. (defcustom org-archive-location "%s_archive::"
  798. "The location where subtrees should be archived.
  799. This string consists of two parts, separated by a double-colon.
  800. The first part is a file name - when omitted, archiving happens in the same
  801. file. %s will be replaced by the current file name (without directory part).
  802. Archiving to a different file is useful to keep archived entries from
  803. contributing to the Org-mode Agenda.
  804. The part after the double colon is a headline. The archived entries will be
  805. filed under that headline. When omitted, the subtrees are simply filed away
  806. at the end of the file, as top-level entries.
  807. Here are a few examples:
  808. \"%s_archive::\"
  809. If the current file is Projects.org, archive in file
  810. Projects.org_archive, as top-level trees. This is the default.
  811. \"::* Archived Tasks\"
  812. Archive in the current file, under the top-level headline
  813. \"* Archived Tasks\".
  814. \"~/org/archive.org::\"
  815. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  816. \"basement::** Finished Tasks\"
  817. Archive in file ./basement (relative path), as level 3 trees
  818. below the level 2 heading \"** Finished Tasks\".
  819. You may set this option on a per-file basis by adding to the buffer a
  820. line like
  821. #+ARCHIVE: basement::** Finished Tasks"
  822. :group 'org-archive
  823. :type 'string)
  824. (defcustom org-archive-mark-done t
  825. "Non-nil means, mark archived entries as DONE."
  826. :group 'org-archive
  827. :type 'boolean)
  828. (defcustom org-archive-stamp-time t
  829. "Non-nil means, add a time stamp to archived entries.
  830. The time stamp will be added directly after the TODO state keyword in the
  831. first line, so it is probably best to use this in combinations with
  832. `org-archive-mark-done'."
  833. :group 'org-archive
  834. :type 'boolean)
  835. (defgroup org-tags nil
  836. "Options concerning startup of Org-mode."
  837. :tag "Org Tags"
  838. :group 'org)
  839. (defcustom org-tags-column 48
  840. "The column to which tags should be indented in a headline.
  841. If this number is positive, it specifies the column. If it is negative,
  842. it means that the tags should be flushright to that column. For example,
  843. -79 works well for a normal 80 character screen."
  844. :group 'org-tags
  845. :type 'integer)
  846. (defcustom org-auto-align-tags t
  847. "Non-nil means, realign tags after pro/demotion of TODO state change.
  848. These operations change the length of a headline and therefore shift
  849. the tags around. With this options turned on, after each such operation
  850. the tags are again aligned to `org-tags-column'."
  851. :group 'org-tags
  852. :type 'boolean)
  853. (defcustom org-use-tag-inheritance t
  854. "Non-nil means, tags in levels apply also for sublevels.
  855. When nil, only the tags directly given in a specific line apply there.
  856. If you turn off this option, you very likely want to turn on the
  857. companion option `org-tags-match-list-sublevels'."
  858. :group 'org-tags
  859. :type 'boolean)
  860. (defcustom org-tags-match-list-sublevels nil
  861. "Non-nil means list also sublevels of headlines matching tag search.
  862. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  863. the sublevels of a headline matching a tag search often also match
  864. the same search. Listing all of them can create very long lists.
  865. Setting this variable to nil causes subtrees to be skipped.
  866. This option is off by default, because inheritance in on. If you turn
  867. inheritance off, you very likely want to turn this option on.
  868. As a special case, if the tag search is restricted to TODO items, the
  869. value of this variable is ignored and sublevels are always checked, to
  870. make sure all corresponding TODO items find their way into the list."
  871. :group 'org-tags
  872. :type 'boolean)
  873. (defvar org-tags-history nil
  874. "History of minibuffer reads for tags.")
  875. (defvar org-last-tags-completion-table nil
  876. "The last used completion table for tags.")
  877. (defgroup org-link nil
  878. "Options concerning links in Org-mode."
  879. :tag "Org Link"
  880. :group 'org)
  881. (defcustom org-descriptive-links t
  882. "Non-nil means, hide link part and only show description of bracket links.
  883. Bracket links are like [[link][descritpion]]. This variable sets the initial
  884. state in new org-mode buffers. The setting can then be toggled on a
  885. per-buffer basis from the Org->Hyperlinks menu."
  886. :group 'org-link
  887. :type 'boolean)
  888. (defcustom org-old-link-style nil
  889. "Non-nil means don't use bracket-style links when inserting a link.
  890. Deprecated, the bracket-style link system is better and should be used."
  891. :group 'org-link
  892. :type 'boolean)
  893. (defcustom org-tab-follows-link nil
  894. "Non-nil means, on links TAB will follow the link.
  895. Needs to be set before org.el is loaded."
  896. :group 'org-link
  897. :type 'boolean)
  898. (defcustom org-return-follows-link nil
  899. "Non-nil means, on links RET will follow the link.
  900. Needs to be set before org.el is loaded."
  901. :group 'org-link
  902. :type 'boolean)
  903. (defcustom org-mark-ring-length 4
  904. "Number of different positions to be recorded in the ring
  905. Changing this requires a restart of Emacs to work correctly."
  906. :group 'org-link
  907. :type 'interger)
  908. (defcustom org-link-format "<%s>"
  909. "Default format for external, URL-like linkes in the buffer.
  910. This is a format string for printf, %s will be replaced by the link text.
  911. If you want to make sure that your link is always properly terminated,
  912. include angle brackets into this format, like \"<%s>\". Some people also
  913. recommend an additional URL: prefix, so the format would be \"<URL:%s>\"."
  914. :group 'org-link
  915. :type '(choice
  916. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  917. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  918. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  919. (string :tag "Other" :value "<%s>")))
  920. ;; FIXME: What is the meaning of this in the age of bracket links????
  921. (defcustom org-allow-space-in-links t
  922. "Non-nil means, file names in links may contain space characters.
  923. When nil, it becomes possible to put several links into a line.
  924. Note that in tables, a link never extends accross fields, so in a table
  925. it is always possible to put several links into a line.
  926. Changing this variable requires a restart of Emacs of become effective."
  927. :group 'org-link
  928. :type 'boolean)
  929. (defcustom org-radio-targets t
  930. "Non-nil means activate text matching a link target.
  931. Radio targets are strings in triple angular brackets, like <<<My Target>>>.
  932. When this option is set, any occurrence of \"my target\" in normal text
  933. becomes a link."
  934. :group 'org-link
  935. :type 'boolean)
  936. (defcustom org-activate-camels nil
  937. "Non-nil means, treat words in CamelCase as in-file links.
  938. Changing this requires restart of Emacs to become effective."
  939. :group 'org-link
  940. :type 'boolean)
  941. (defcustom org-context-in-file-links t
  942. "Non-nil means, file links from `org-store-link' contain context.
  943. A search string will be added to the file name with :: as separator and
  944. used to find the context when the link is activated by the command
  945. `org-open-at-point'.
  946. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  947. negates this setting for the duration of the command."
  948. :group 'org-link
  949. :type 'boolean)
  950. (defcustom org-file-link-context-use-camel-case nil
  951. "Non-nil means, use CamelCase to store a search context in a file link.
  952. When nil, the search string simply consists of the words of the string.
  953. CamelCase is deprecated, and support for it may be dropped in the future."
  954. :group 'org-link
  955. :type 'boolean)
  956. (defcustom org-keep-stored-link-after-insertion nil
  957. "Non-nil means, keep link in list for entire session.
  958. The command `org-store-link' adds a link pointing to the current
  959. location to an internal list. These links accumulate during a session.
  960. The command `org-insert-link' can be used to insert links into any
  961. Org-mode file (offering completion for all stored links). When this
  962. option is nil, every link which has been inserted once using \\[org-insert-link]
  963. will be removed from the list, to make completing the unused links
  964. more efficient."
  965. :group 'org-link
  966. :type 'boolean)
  967. (defcustom org-link-frame-setup
  968. '((vm . vm-visit-folder-other-frame)
  969. (gnus . gnus-other-frame)
  970. (file . find-file-other-window))
  971. "Setup the frame configuration for following links.
  972. When following a link with Emacs, it may often be useful to display
  973. this link in another window or frame. This variable can be used to
  974. set this up for the different types of links.
  975. For VM, use any of
  976. `vm-visit-folder'
  977. `vm-visit-folder-other-frame'
  978. For Gnus, use any of
  979. `gnus'
  980. `gnus-other-frame'
  981. For FILE, use any of
  982. `find-file'
  983. `find-file-other-window'
  984. `find-file-other-frame'
  985. For the calendar, use the variable `calendar-setup'.
  986. For BBDB, it is currently only possible to display the matches in
  987. another window."
  988. :group 'org-link
  989. :type '(list
  990. (cons (const vm)
  991. (choice
  992. (const vm-visit-folder)
  993. (const vm-visit-folder-other-window)
  994. (const vm-visit-folder-other-frame)))
  995. (cons (const gnus)
  996. (choice
  997. (const gnus)
  998. (const gnus-other-frame)))
  999. (cons (const file)
  1000. (choice
  1001. (const find-file)
  1002. (const find-file-other-window)
  1003. (const find-file-other-frame)))))
  1004. (defcustom org-usenet-links-prefer-google nil
  1005. "Non-nil means, `org-store-link' will create web links to Google groups.
  1006. When nil, Gnus will be used for such links.
  1007. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1008. negates this setting for the duration of the command."
  1009. :group 'org-link
  1010. :type 'boolean)
  1011. (defcustom org-open-non-existing-files nil
  1012. "Non-nil means, `org-open-file' will open non-existing file.
  1013. When nil, an error will be generated."
  1014. :group 'org-link
  1015. :type 'boolean)
  1016. (defcustom org-confirm-shell-links t
  1017. "Non-nil means, ask for confirmation before executing shell links.
  1018. The default is true, to keep new users from shooting into their own foot."
  1019. :group 'org-link
  1020. :type 'boolean)
  1021. (defconst org-file-apps-defaults-gnu
  1022. '((t . mailcap))
  1023. "Default file applications on a UNIX or GNU/Linux system.
  1024. See `org-file-apps'.")
  1025. (defconst org-file-apps-defaults-macosx
  1026. '((t . "open %s")
  1027. ("ps" . "gv %s")
  1028. ("ps.gz" . "gv %s")
  1029. ("eps" . "gv %s")
  1030. ("eps.gz" . "gv %s")
  1031. ("dvi" . "xdvi %s")
  1032. ("fig" . "xfig %s"))
  1033. "Default file applications on a MacOS X system.
  1034. The system \"open\" is known as a default, but we use X11 applications
  1035. for some files for which the OS does not have a good default.
  1036. See `org-file-apps'.")
  1037. (defconst org-file-apps-defaults-windowsnt
  1038. '((t . (w32-shell-execute "open" file)))
  1039. "Default file applications on a Windows NT system.
  1040. The system \"open\" is used for most files.
  1041. See `org-file-apps'.")
  1042. (defcustom org-file-apps
  1043. '(
  1044. ("txt" . emacs)
  1045. ("tex" . emacs)
  1046. ("ltx" . emacs)
  1047. ("org" . emacs)
  1048. ("el" . emacs)
  1049. )
  1050. "External applications for opening `file:path' items in a document.
  1051. Org-mode uses system defaults for different file types, but
  1052. you can use this variable to set the application for a given file
  1053. extension. The entries in this list are cons cells with a file extension
  1054. and the corresponding command. Possible values for the command are:
  1055. `emacs' The file will be visited by the current Emacs process.
  1056. `default' Use the default application for this file type.
  1057. string A command to be executed by a shell; %s will be replaced
  1058. by the path to the file.
  1059. sexp A Lisp form which will be evaluated. The file path will
  1060. be available in the Lisp variable `file'.
  1061. For more examples, see the system specific constants
  1062. `org-file-apps-defaults-macosx'
  1063. `org-file-apps-defaults-windowsnt'
  1064. `org-file-apps-defaults-gnu'."
  1065. :group 'org-link
  1066. :type '(repeat
  1067. (cons (choice :value ""
  1068. (string :tag "Extension")
  1069. (const :tag "Default for unrecognized files" t)
  1070. (const :tag "Links to a directory" directory))
  1071. (choice :value ""
  1072. (const :tag "Visit with Emacs" emacs)
  1073. (const :tag "Use system default" default)
  1074. (string :tag "Command")
  1075. (sexp :tag "Lisp form")))))
  1076. (defcustom org-mhe-search-all-folders nil
  1077. "Non-nil means, that the search for the mh-message will be extended to
  1078. all folders if the message cannot be found in the folder given in the link.
  1079. Searching all folders is very effective with one of the search engines
  1080. supported by MH-E, but will be slow with pick."
  1081. :group 'org-link
  1082. :type 'boolean)
  1083. (defgroup org-remember nil
  1084. "Options concerning interaction with remember.el."
  1085. :tag "Org Remember"
  1086. :group 'org)
  1087. (defcustom org-directory "~/org"
  1088. "Directory with org files.
  1089. This directory will be used as default to prompt for org files.
  1090. Used by the hooks for remember.el."
  1091. :group 'org-remember
  1092. :type 'directory)
  1093. (defcustom org-default-notes-file "~/.notes"
  1094. "Default target for storing notes.
  1095. Used by the hooks for remember.el. This can be a string, or nil to mean
  1096. the value of `remember-data-file'."
  1097. :group 'org-remember
  1098. :type '(choice
  1099. (const :tag "Default from remember-data-file" nil)
  1100. file))
  1101. (defcustom org-remember-templates nil
  1102. "Templates for the creation of remember buffers.
  1103. When nil, just let remember make the buffer.
  1104. When not nil, this is a list of 3-element lists. In each entry, the first
  1105. element is a character, a unique key to select this template.
  1106. The second element is the template. The third element is optional and can
  1107. specify a destination file for remember items created with this template.
  1108. The default file is given by `org-default-notes-file'.
  1109. The template specifies the structure of the remember buffer. It should have
  1110. a first line starting with a star, to act as the org-mode headline.
  1111. Furthermore, the following %-escapes will be replaced with content:
  1112. %t time stamp, date only
  1113. %T time stamp with date and time
  1114. %u inactive time stamp, date only
  1115. %U inactive time stamp with date and time
  1116. %n user name
  1117. %a annotation, normally the link created with org-store-link
  1118. %i initial content, the region when remember is called with C-u.
  1119. If %i is indented, the entire inserted text will be indented as well.
  1120. %? This will be removed, and the cursor placed at this position."
  1121. :group 'org-remember
  1122. :type '(repeat :tag "enabled"
  1123. (list :value (?a "\n" nil)
  1124. (character :tag "Selection Key")
  1125. (string :tag "Template")
  1126. (file :tag "Destination file (optional)"))))
  1127. (defcustom org-reverse-note-order nil
  1128. "Non-nil means, store new notes at the beginning of a file or entry.
  1129. When nil, new notes will be filed to the end of a file or entry."
  1130. :group 'org-remember
  1131. :type '(choice
  1132. (const :tag "Reverse always" t)
  1133. (const :tag "Reverse never" nil)
  1134. (repeat :tag "By file name regexp"
  1135. (cons regexp boolean))))
  1136. (defgroup org-table nil
  1137. "Options concerning tables in Org-mode."
  1138. :tag "Org Table"
  1139. :group 'org)
  1140. (defcustom org-enable-table-editor 'optimized
  1141. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  1142. When nil, such lines will be treated like ordinary lines.
  1143. When equal to the symbol `optimized', the table editor will be optimized to
  1144. do the following:
  1145. - Use automatic overwrite mode in front of whitespace in table fields.
  1146. This make the structure of the table stay in tact as long as the edited
  1147. field does not exceed the column width.
  1148. - Minimize the number of realigns. Normally, the table is aligned each time
  1149. TAB or RET are pressed to move to another field. With optimization this
  1150. happens only if changes to a field might have changed the column width.
  1151. Optimization requires replacing the functions `self-insert-command',
  1152. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1153. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1154. very good at guessing when a re-align will be necessary, but you can always
  1155. force one with \\[org-ctrl-c-ctrl-c].
  1156. If you would like to use the optimized version in Org-mode, but the
  1157. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1158. This variable can be used to turn on and off the table editor during a session,
  1159. but in order to toggle optimization, a restart is required.
  1160. See also the variable `org-table-auto-blank-field'."
  1161. :group 'org-table
  1162. :type '(choice
  1163. (const :tag "off" nil)
  1164. (const :tag "on" t)
  1165. (const :tag "on, optimized" optimized)))
  1166. ;; FIXME: We could have a third option which makes it jump only over the first
  1167. ;; hline in a table.
  1168. (defcustom org-table-tab-jumps-over-hlines t
  1169. "Non-nil means, tab in the last column of a table with jump over a hline.
  1170. If a horizontal separator line is following the current line,
  1171. `org-table-next-field' can either create a new row before that line, or jump
  1172. over the line. When this option is nil, a new line will be created before
  1173. this line."
  1174. :group 'org-table
  1175. :type 'boolean)
  1176. (defcustom org-table-auto-blank-field t
  1177. "Non-nil means, automatically blank table field when starting to type into it.
  1178. This only happens when typing immediately after a field motion
  1179. command (TAB, S-TAB or RET).
  1180. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  1181. :group 'org-table
  1182. :type 'boolean)
  1183. (defcustom org-table-default-size "5x2"
  1184. "The default size for newly created tables, Columns x Rows."
  1185. :group 'org-table
  1186. :type 'string)
  1187. (defcustom org-table-automatic-realign t
  1188. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  1189. When nil, aligning is only done with \\[org-table-align], or after column
  1190. removal/insertion."
  1191. :group 'org-table
  1192. :type 'boolean)
  1193. (defcustom org-table-spaces-around-separators '(1 . 1)
  1194. "The number of spaces to be placed before and after separators."
  1195. :group 'org-table
  1196. :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
  1197. (defcustom org-table-spaces-around-invisible-separators '(1 . 2)
  1198. "The number of spaces to be placed before and after separators.
  1199. This option applies when the column separators have been made invisible."
  1200. :group 'org-table
  1201. :type '(cons (number :tag "Before \"|\"") (number :tag " After \"|\"")))
  1202. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  1203. "Regular expression for recognizing numbers in table columns.
  1204. If a table column contains mostly numbers, it will be aligned to the
  1205. right. If not, it will be aligned to the left.
  1206. The default value of this option is a regular expression which allows
  1207. anything which looks remotely like a number as used in scientific
  1208. context. For example, all of the following will be considered a
  1209. number:
  1210. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  1211. Other options offered by the customize interface are more restrictive."
  1212. :group 'org-table
  1213. :type '(choice
  1214. (const :tag "Positive Integers"
  1215. "^[0-9]+$")
  1216. (const :tag "Integers"
  1217. "^[-+]?[0-9]+$")
  1218. (const :tag "Floating Point Numbers"
  1219. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  1220. (const :tag "Floating Point Number or Integer"
  1221. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  1222. (const :tag "Exponential, Floating point, Integer"
  1223. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  1224. (const :tag "Very General Number-Like"
  1225. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  1226. (string :tag "Regexp:")))
  1227. (defcustom org-table-number-fraction 0.5
  1228. "Fraction of numbers in a column required to make the column align right.
  1229. In a column all non-white fields are considered. If at least this
  1230. fraction of fields is matched by `org-table-number-fraction',
  1231. alignment to the right border applies."
  1232. :group 'org-table
  1233. :type 'number)
  1234. (defcustom org-export-highlight-first-table-line t
  1235. "Non-nil means, highlight the first table line.
  1236. In HTML export, this means use <th> instead of <td>.
  1237. In tables created with table.el, this applies to the first table line.
  1238. In Org-mode tables, all lines before the first horizontal separator
  1239. line will be formatted with <th> tags."
  1240. :group 'org-table
  1241. :type 'boolean)
  1242. (defcustom org-table-tab-recognizes-table.el t
  1243. "Non-nil means, TAB will automatically notice a table.el table.
  1244. When it sees such a table, it moves point into it and - if necessary -
  1245. calls `table-recognize-table'."
  1246. :group 'org-table
  1247. :type 'boolean)
  1248. (defcustom org-table-limit-column-width t
  1249. "Non-nil means, allow to limit the width of table columns with <N> fields."
  1250. :group 'org-table
  1251. :type 'boolean)
  1252. (defgroup org-table-calculation nil
  1253. "Options concerning tables in Org-mode."
  1254. :tag "Org Table Calculation"
  1255. :group 'org)
  1256. (defcustom org-table-copy-increment t
  1257. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  1258. :group 'org-table-calculation
  1259. :type 'boolean)
  1260. (defcustom org-calc-default-modes
  1261. '(calc-internal-prec 12
  1262. calc-float-format (float 5)
  1263. calc-angle-mode deg
  1264. calc-prefer-frac nil
  1265. calc-symbolic-mode nil
  1266. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  1267. calc-display-working-message t
  1268. )
  1269. "List with Calc mode settings for use in calc-eval for table formulas.
  1270. The list must contain alternating symbols (Calc modes variables and values).
  1271. Don't remove any of the default settings, just change the values. Org-mode
  1272. relies on the variables to be present in the list."
  1273. :group 'org-table-calculation
  1274. :type 'plist)
  1275. (defcustom org-table-formula-evaluate-inline t
  1276. "Non-nil means, TAB and RET evaluate a formula in current table field.
  1277. If the current field starts with an equal sign, it is assumed to be a formula
  1278. which should be evaluated as described in the manual and in the documentation
  1279. string of the command `org-table-eval-formula'. This feature requires the
  1280. Emacs calc package.
  1281. When this variable is nil, formula calculation is only available through
  1282. the command \\[org-table-eval-formula]."
  1283. :group 'org-table-calculation
  1284. :type 'boolean)
  1285. (defcustom org-table-formula-use-constants t
  1286. "Non-nil means, interpret constants in formulas in tables.
  1287. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  1288. by the value given in `org-table-formula-constants', or by a value obtained
  1289. from the `constants.el' package."
  1290. :group 'org-table-calculation
  1291. :type 'boolean)
  1292. (defcustom org-table-formula-constants nil
  1293. "Alist with constant names and values, for use in table formulas.
  1294. The car of each element is a name of a constant, without the `$' before it.
  1295. The cdr is the value as a string. For example, if you'd like to use the
  1296. speed of light in a formula, you would configure
  1297. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  1298. and then use it in an equation like `$1*$c'."
  1299. :group 'org-table-calculation
  1300. :type '(repeat
  1301. (cons (string :tag "name")
  1302. (string :tag "value"))))
  1303. (defcustom org-table-formula-numbers-only nil
  1304. "Non-nil means, calculate only with numbers in table formulas.
  1305. Then all input fields will be converted to a number, and the result
  1306. must also be a number. When nil, calc's full potential is available
  1307. in table calculations, including symbolics etc."
  1308. :group 'org-table-calculation
  1309. :type 'boolean)
  1310. (defcustom org-table-allow-automatic-line-recalculation t
  1311. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  1312. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  1313. :group 'org-table-calculation
  1314. :type 'boolean)
  1315. (defgroup org-export nil
  1316. "Options for exporting org-listings."
  1317. :tag "Org Export"
  1318. :group 'org)
  1319. (defcustom org-export-language-setup
  1320. '(("en" "Author" "Date" "Table of Contents")
  1321. ("da" "Ophavsmand" "Dato" "Indhold")
  1322. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1323. ("es" "Autor" "Fecha" "\xccndice")
  1324. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1325. ("it" "Autore" "Data" "Indice")
  1326. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1327. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1328. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1329. "Terms used in export text, translated to different languages.
  1330. Use the variable `org-export-default-language' to set the language,
  1331. or use the +OPTION lines for a per-file setting."
  1332. :group 'org-export
  1333. :type '(repeat
  1334. (list
  1335. (string :tag "HTML language tag")
  1336. (string :tag "Author")
  1337. (string :tag "Date")
  1338. (string :tag "Table of Contents"))))
  1339. (defcustom org-export-default-language "en"
  1340. "The default language of HTML export, as a string.
  1341. This should have an association in `org-export-language-setup'."
  1342. :group 'org-export
  1343. :type 'string)
  1344. (defcustom org-export-html-style
  1345. "<style type=\"text/css\">
  1346. html {
  1347. font-family: Times, serif;
  1348. font-size: 12pt;
  1349. }
  1350. .title { text-align: center; }
  1351. .todo, .deadline { color: red; }
  1352. .done { color: green; }
  1353. .target { background-color: lavender; }
  1354. pre {
  1355. border: 1pt solid #AEBDCC;
  1356. background-color: #F3F5F7;
  1357. padding: 5pt;
  1358. font-family: courier, monospace;
  1359. }
  1360. table { border-collapse: collapse; }
  1361. td, th {
  1362. vertical-align: top;
  1363. border: 1pt solid #ADB9CC;
  1364. }
  1365. </style>"
  1366. "The default style specification for exported HTML files.
  1367. Since there are different ways of setting style information, this variable
  1368. needs to contain the full HTML structure to provide a style, including the
  1369. surrounding HTML tags. The style specifications should include definitions
  1370. for new classes todo, done, title, and deadline. For example, legal values
  1371. would be:
  1372. <style type=\"text/css\">
  1373. p { font-weight: normal; color: gray; }
  1374. h1 { color: black; }
  1375. .title { text-align: center; }
  1376. .todo, .deadline { color: red; }
  1377. .done { color: green; }
  1378. </style>
  1379. or, if you want to keep the style in a file,
  1380. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1381. As the value of this option simply gets inserted into the HTML <head> header,
  1382. you can \"misuse\" it to add arbitrary text to the header."
  1383. :group 'org-export
  1384. :type 'string)
  1385. (defcustom org-export-headline-levels 3
  1386. "The last level which is still exported as a headline.
  1387. Inferior levels will produce itemize lists when exported.
  1388. Note that a numeric prefix argument to an exporter function overrides
  1389. this setting.
  1390. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1391. :group 'org-export
  1392. :type 'number)
  1393. (defcustom org-export-with-section-numbers t
  1394. "Non-nil means, add section numbers to headlines when exporting.
  1395. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1396. :group 'org-export
  1397. :type 'boolean)
  1398. (defcustom org-export-with-toc t
  1399. "Non-nil means, create a table of contents in exported files.
  1400. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1401. Headlines which contain any TODO items will be marked with \"(*)\" in
  1402. ASCII export, and with red color in HTML output.
  1403. In HTML output, the TOC will be clickable.
  1404. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1405. :group 'org-export
  1406. :type 'boolean)
  1407. (defcustom org-export-plain-list-max-depth 20
  1408. "Maximum depth of hand-formatted lists in HTML export.
  1409. Org-mode parses hand-formatted enumeration and bullet lists and
  1410. transforms them to HTML open export. Different indentation of the
  1411. bullet or number indicates different list nesting levels. To avoid
  1412. confusion, only a single level is allowed by default. When this is
  1413. larger than 1, deeper indentation leads to deeper list nesting. For
  1414. example, the default value of 3 allows the following list to be
  1415. formatted correctly in HTML:
  1416. * Fruit
  1417. - Apple
  1418. - Banana
  1419. 1. from Africa
  1420. 2. from South America
  1421. - Pineapple
  1422. * Bread
  1423. * Dairy products"
  1424. :group 'org-export
  1425. :type 'integer)
  1426. (defcustom org-export-preserve-breaks nil
  1427. "Non-nil means, preserve all line breaks when exporting.
  1428. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1429. export, line breaks will always be preserved, regardless of this variable.
  1430. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1431. :group 'org-export
  1432. :type 'boolean)
  1433. (defcustom org-export-html-inline-images t
  1434. "Non-nil means, inline images into exported HTML pages.
  1435. The link will still be to the original location of the image file.
  1436. So if you are moving the page, lets say to your public HTML site,
  1437. you will have to move the image and maybe change the link."
  1438. :group 'org-export
  1439. :type 'boolean)
  1440. (defcustom org-export-html-expand t
  1441. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1442. When nil, these tags will be exported as plain text and therefore
  1443. not be interpreted by a browser.
  1444. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1445. :group 'org-export
  1446. :type 'boolean)
  1447. (defcustom org-export-with-fixed-width t
  1448. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1449. This can be used to have pre-formatted text, fragments of code etc. For
  1450. example:
  1451. : ;; Some Lisp examples
  1452. : (while (defc cnt)
  1453. : (ding))
  1454. will be looking just like this in also HTML. In ASCII export, this option
  1455. has no effect.
  1456. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1457. :group 'org-export
  1458. :type 'boolean)
  1459. (defcustom org-export-with-tables t
  1460. "If non-nil, lines starting with \"|\" define a table.
  1461. For example:
  1462. | Name | Address | Birthday |
  1463. |-------------+----------+-----------|
  1464. | Arthur Dent | England | 29.2.2100 |
  1465. In ASCII export, this option has no effect.
  1466. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1467. :group 'org-export
  1468. :type 'boolean)
  1469. (defcustom org-export-table-remove-special-lines t
  1470. "Remove special lines and marking characters in calculating tables.
  1471. This removes the special marking character column from tables that are set
  1472. up for spreadsheet calculations. It also removes the entire lines
  1473. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1474. the values of constants may be useful to have."
  1475. :group 'org-export
  1476. :type 'boolean)
  1477. (defcustom org-export-prefer-native-exporter-for-tables nil
  1478. "Non-nil means, always export tables created with table.el natively.
  1479. Natively means, use the HTML code generator in table.el.
  1480. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1481. the table does not use row- or column-spanning). This has the
  1482. advantage, that the automatic HTML conversions for math symbols and
  1483. sub/superscripts can be applied. Org-mode's HTML generator is also
  1484. much faster."
  1485. :group 'org-export
  1486. :type 'boolean)
  1487. (defcustom org-export-html-table-tag
  1488. "<table border=1 cellspacing=0 cellpadding=6>"
  1489. "The HTML tag used to start a table.
  1490. This must be a <table> tag, but you may change the options like
  1491. borders and spacing."
  1492. :group 'org-export
  1493. :type 'string)
  1494. (defcustom org-export-with-emphasize t
  1495. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1496. If the export target supports emphasizing text, the word will be
  1497. typeset in bold, italic, or underlined, respectively. Works only for
  1498. single words, but you can say: I *really* *mean* *this*.
  1499. In ASCII export, this option has no effect.
  1500. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1501. :group 'org-export
  1502. :type 'boolean)
  1503. (defcustom org-match-sexp-depth 3
  1504. "Number of stacked braces for sub/superscript matching.
  1505. This has to be set before loading org.el to be effective."
  1506. :group 'org-export
  1507. :type 'integer)
  1508. ;; FIXME: Should () parens be removed as well in sub/superscripts?
  1509. (defcustom org-export-with-sub-superscripts t
  1510. "Non-nil means, interpret \"_\" and \"^\" for export.
  1511. When this option is turned on, you can use TeX-like syntax for sub- and
  1512. superscripts. Several characters after \"_\" or \"^\" will be
  1513. considered as a single item - so grouping with {} is normally not
  1514. needed. For example, the following things will be parsed as single
  1515. sub- or superscripts.
  1516. 10^24 or 10^tau several digits will be considered 1 item.
  1517. 10^-12 or 10^-tau a leading sign with digits or a word
  1518. x^2-y^3 will be read as x^2 - y^3, because items are
  1519. terminated by almost any nonword/nondigit char.
  1520. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1521. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1522. sub/superscript.
  1523. In ASCII export, this option has no effect.
  1524. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1525. :group 'org-export
  1526. :type 'boolean)
  1527. (defcustom org-export-with-TeX-macros t
  1528. "Non-nil means, interpret simple TeX-like macros when exporting.
  1529. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1530. No only real TeX macros will work here, but the standard HTML entities
  1531. for math can be used as macro names as well. For a list of supported
  1532. names in HTML export, see the constant `org-html-entities'.
  1533. In ASCII export, this option has no effect.
  1534. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1535. :group 'org-export
  1536. :type 'boolean)
  1537. (defcustom org-export-html-with-timestamp nil
  1538. "If non-nil, write `org-export-html-html-helper-timestamp'
  1539. into the exported HTML text. Otherwise, the buffer will just be saved
  1540. to a file."
  1541. :group 'org-export
  1542. :type 'boolean)
  1543. (defcustom org-export-html-html-helper-timestamp
  1544. "<br><br><hr><p><!-- hhmts start --> <!-- hhmts end -->\n"
  1545. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1546. :group 'org-export
  1547. :type 'string)
  1548. (defcustom org-export-ascii-show-new-buffer t
  1549. "Non-nil means, popup buffer containing the exported ASCII text.
  1550. Otherwise the buffer will just be saved to a file and stay hidden."
  1551. :group 'org-export
  1552. :type 'boolean)
  1553. (defcustom org-export-html-show-new-buffer nil
  1554. "Non-nil means, popup buffer containing the exported html text.
  1555. Otherwise, the buffer will just be saved to a file and stay hidden."
  1556. :group 'org-export
  1557. :type 'boolean)
  1558. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1559. "The file name for the iCalendar file covering all agenda files.
  1560. This file is created with the command \\[org-export-icalendar-all-agenda-files]."
  1561. :group 'org-export
  1562. :type 'file)
  1563. (defcustom org-icalendar-include-todo nil
  1564. "Non-nil means, export to iCalendar files should also cover TODO items."
  1565. :group 'org-export
  1566. :type 'boolean)
  1567. (defcustom org-icalendar-combined-name "OrgMode"
  1568. "Calendar name for the combined iCalendar representing all agenda files."
  1569. :group 'org-export
  1570. :type 'string)
  1571. (defgroup org-font-lock nil
  1572. "Faces and settings for highlighting in Org-mode."
  1573. :tag "Org Font Lock"
  1574. :group 'org)
  1575. (defcustom org-level-color-stars-only nil
  1576. "Non-nil means fontify only the stars in each headline.
  1577. When nil, the entire headline is fontified.
  1578. Changing it requires restart of `font-lock-mode' to become effective
  1579. also in regions already fontified."
  1580. :group 'org-font-lock
  1581. :type 'boolean)
  1582. (defcustom org-hide-leading-stars nil
  1583. "Non-nil means, hide the first N-1 stars in a headline.
  1584. This works by using the face `org-hide' for these stars. This
  1585. face is white for a light background, and black for a dark
  1586. background. You may have to customize the face `org-hide' to
  1587. make this work.
  1588. Changing it requires restart of `font-lock-mode' to become effective
  1589. also in regions already fontified."
  1590. :group 'org-font-lock
  1591. :type 'boolean)
  1592. (defcustom org-fontify-done-headline nil
  1593. "Non-nil means, change the face of a headline if it is marked DONE.
  1594. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1595. When this is non-nil, the headline after the keyword is set to the
  1596. `org-headline-done' as an additional indication."
  1597. :group 'org-font-lock
  1598. :type 'boolean)
  1599. (defcustom org-fontify-emphasized-text t
  1600. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1601. Changing this variable requires a restart of Emacs to take effect."
  1602. :group 'org-font-lock
  1603. :type 'boolean)
  1604. (defface org-hide
  1605. '(
  1606. (((type tty) (class color)) (:foreground "white"))
  1607. (((class color) (background light)) (:foreground "white"))
  1608. (((class color) (background dark)) (:foreground "black"))
  1609. (t (:inverse-video nil)))
  1610. "Face used for level 1 headlines."
  1611. :group 'org-font-lock)
  1612. (defface org-level-1 ;; font-lock-function-name-face
  1613. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1614. (((class color) (background light)) (:foreground "Blue"))
  1615. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1616. (t (:inverse-video t :bold t)))
  1617. "Face used for level 1 headlines."
  1618. :group 'org-font-lock)
  1619. (defface org-level-2 ;; font-lock-variable-name-face
  1620. '((((type tty) (class color)) (:foreground "yellow" :weight light))
  1621. (((class color) (background light)) (:foreground "DarkGoldenrod"))
  1622. (((class color) (background dark)) (:foreground "LightGoldenrod"))
  1623. (t (:bold t :italic t)))
  1624. "Face used for level 2 headlines."
  1625. :group 'org-font-lock)
  1626. (defface org-level-3 ;; font-lock-keyword-face
  1627. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1628. (((class color) (background light)) (:foreground "Purple"))
  1629. (((class color) (background dark)) (:foreground "Cyan"))
  1630. (t (:bold t)))
  1631. "Face used for level 3 headlines."
  1632. :group 'org-font-lock)
  1633. (defface org-level-4 ;; font-lock-comment-face
  1634. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1635. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1636. (((class color) (background light)) (:foreground "Firebrick"))
  1637. (((class color) (background dark)) (:foreground "chocolate1"))
  1638. (t (:bold t :italic t)))
  1639. "Face used for level 4 headlines."
  1640. :group 'org-font-lock)
  1641. (defface org-level-5 ;; font-lock-type-face
  1642. '((((type tty) (class color)) (:foreground "green"))
  1643. (((class color) (background light)) (:foreground "ForestGreen"))
  1644. (((class color) (background dark)) (:foreground "PaleGreen"))
  1645. (t (:bold t :underline t)))
  1646. "Face used for level 5 headlines."
  1647. :group 'org-font-lock)
  1648. (defface org-level-6 ;; font-lock-constant-face
  1649. '((((type tty) (class color)) (:foreground "magenta"))
  1650. (((class color) (background light)) (:foreground "CadetBlue"))
  1651. (((class color) (background dark)) (:foreground "Aquamarine"))
  1652. (t (:bold t :underline t)))
  1653. "Face used for level 6 headlines."
  1654. :group 'org-font-lock)
  1655. (defface org-level-7 ;; font-lock-builtin-face
  1656. '((((type tty) (class color)) (:foreground "blue" :weight light))
  1657. (((class color) (background light)) (:foreground "Orchid"))
  1658. (((class color) (background dark)) (:foreground "LightSteelBlue"))
  1659. (t (:bold t)))
  1660. "Face used for level 7 headlines."
  1661. :group 'org-font-lock)
  1662. (defface org-level-8 ;; font-lock-string-face
  1663. '((((type tty) (class color)) (:foreground "green"))
  1664. (((class color) (background light)) (:foreground "RosyBrown"))
  1665. (((class color) (background dark)) (:foreground "LightSalmon"))
  1666. (t (:italic t)))
  1667. "Face used for level 8 headlines."
  1668. :group 'org-font-lock)
  1669. (defface org-special-keyword ;; font-lock-string-face
  1670. '((((type tty) (class color)) (:foreground "green"))
  1671. (((class color) (background light)) (:foreground "RosyBrown"))
  1672. (((class color) (background dark)) (:foreground "LightSalmon"))
  1673. (t (:italic t)))
  1674. "Face used for special keywords."
  1675. :group 'org-font-lock)
  1676. (defface org-warning ;; font-lock-warning-face
  1677. '((((type tty) (class color)) (:foreground "red"))
  1678. (((class color) (background light)) (:foreground "Red" :bold t))
  1679. (((class color) (background dark)) (:foreground "Red1" :bold t))
  1680. ; (((class color) (background dark)) (:foreground "Pink" :bold t))
  1681. (t (:inverse-video t :bold t)))
  1682. "Face for deadlines and TODO keywords."
  1683. :group 'org-font-lock)
  1684. (defface org-headline-done ;; font-lock-string-face
  1685. '((((type tty) (class color)) (:foreground "green"))
  1686. (((class color) (background light)) (:foreground "RosyBrown"))
  1687. (((class color) (background dark)) (:foreground "LightSalmon"))
  1688. (t (:italic t)))
  1689. "Face used to indicate that a headline is DONE. See also the variable
  1690. `org-fontify-done-headline'."
  1691. :group 'org-font-lock)
  1692. ;; Inheritance does not work for xemacs. So we just copy...
  1693. (defface org-deadline-announce
  1694. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1695. (((class color) (background light)) (:foreground "Blue"))
  1696. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1697. (t (:inverse-video t :bold t)))
  1698. "Face for upcoming deadlines."
  1699. :group 'org-font-lock)
  1700. (defface org-scheduled-today
  1701. '((((type tty) (class color)) (:foreground "green"))
  1702. (((class color) (background light)) (:foreground "DarkGreen"))
  1703. (((class color) (background dark)) (:foreground "PaleGreen"))
  1704. (t (:bold t :underline t)))
  1705. "Face for items scheduled for a certain day."
  1706. :group 'org-font-lock)
  1707. (defface org-scheduled-previously
  1708. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1709. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1710. (((class color) (background light)) (:foreground "Firebrick"))
  1711. (((class color) (background dark)) (:foreground "chocolate1"))
  1712. (t (:bold t :italic t)))
  1713. "Face for items scheduled previously, and not yet done."
  1714. :group 'org-font-lock)
  1715. (defface org-formula
  1716. '((((type tty pc) (class color) (background light)) (:foreground "red"))
  1717. (((type tty pc) (class color) (background dark)) (:foreground "red1"))
  1718. (((class color) (background light)) (:foreground "Firebrick"))
  1719. (((class color) (background dark)) (:foreground "chocolate1"))
  1720. (t (:bold t :italic t)))
  1721. "Face for formulas."
  1722. :group 'org-font-lock)
  1723. (defface org-link
  1724. '((((type tty) (class color)) (:foreground "cyan" :weight bold))
  1725. (((class color) (background light)) (:foreground "Purple" :underline t))
  1726. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1727. (t (:bold t)))
  1728. "Face for links."
  1729. :group 'org-font-lock)
  1730. (defface org-tag
  1731. '((((type tty) (class color)) (:weight bold))
  1732. (((class color) (background light)) (:weight bold))
  1733. (((class color) (background dark)) (:weight bold))
  1734. (t (:bold t)))
  1735. "Face for tags."
  1736. :group 'org-font-lock)
  1737. (defface org-done ;; font-lock-type-face
  1738. '((((type tty) (class color)) (:foreground "green"))
  1739. (((class color) (background light)) (:foreground "ForestGreen" :bold t))
  1740. (((class color) (background dark)) (:foreground "PaleGreen" :bold t))
  1741. (t (:bold t :underline t)))
  1742. "Face used for DONE."
  1743. :group 'org-font-lock)
  1744. (defface org-table ;; font-lock-function-name-face
  1745. '((((type tty) (class color)) (:foreground "blue" :weight bold))
  1746. (((class color) (background light)) (:foreground "Blue"))
  1747. (((class color) (background dark)) (:foreground "LightSkyBlue"))
  1748. (t (:inverse-video t :bold t)))
  1749. "Face used for tables."
  1750. :group 'org-font-lock)
  1751. (defface org-time-grid ;; font-lock-variable-name-face
  1752. '((((type tty) (class color)) (:foreground "yellow" :weight light))
  1753. (((class color) (background light)) (:foreground "DarkGoldenrod"))
  1754. (((class color) (background dark)) (:foreground "LightGoldenrod"))
  1755. (t (:bold t :italic t)))
  1756. "Face used for time grids."
  1757. :group 'org-font-lock)
  1758. (defvar org-level-faces
  1759. '(org-level-1 org-level-2 org-level-3 org-level-4
  1760. org-level-5 org-level-6 org-level-7 org-level-8
  1761. ))
  1762. (defvar org-n-levels (length org-level-faces))
  1763. (defun org-set-regexps-and-options ()
  1764. "Precompute regular expressions for current buffer."
  1765. (when (eq major-mode 'org-mode)
  1766. (let ((re (org-make-options-regexp
  1767. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  1768. "STARTUP" "ARCHIVE")))
  1769. (splitre "[ \t]+")
  1770. kwds int key value cat arch)
  1771. (save-excursion
  1772. (save-restriction
  1773. (widen)
  1774. (goto-char (point-min))
  1775. (while (re-search-forward re nil t)
  1776. (setq key (match-string 1) value (match-string 2))
  1777. (cond
  1778. ((equal key "CATEGORY")
  1779. (if (string-match "[ \t]+$" value)
  1780. (setq value (replace-match "" t t value)))
  1781. (setq cat (intern value)))
  1782. ((equal key "SEQ_TODO")
  1783. (setq int 'sequence
  1784. kwds (append kwds (org-split-string value splitre))))
  1785. ((equal key "PRI_TODO")
  1786. (setq int 'priority
  1787. kwds (append kwds (org-split-string value splitre))))
  1788. ((equal key "TYP_TODO")
  1789. (setq int 'type
  1790. kwds (append kwds (org-split-string value splitre))))
  1791. ((equal key "STARTUP")
  1792. (let ((opts (org-split-string value splitre))
  1793. (set '(("fold" org-startup-folded t)
  1794. ("nofold" org-startup-folded nil)
  1795. ("content" org-startup-folded content)
  1796. ("hidestars" org-hide-leading-stars t)
  1797. ("showstars" org-hide-leading-stars nil)
  1798. ("odd" org-odd-levels-only t)
  1799. ("oddeven" org-odd-levels-only nil)
  1800. ("align" org-startup-align-all-tables t)
  1801. ("noalign" org-startup-align-all-tables nil)
  1802. ("dlcheck" org-startup-with-deadline-check t)
  1803. ("nodlcheck" org-startup-with-deadline-check nil)))
  1804. l var val)
  1805. (while (setq l (assoc (pop opts) set))
  1806. (setq var (nth 1 l) val (nth 2 l))
  1807. (set (make-local-variable var) val))))
  1808. ((equal key "ARCHIVE")
  1809. (string-match " *$" value)
  1810. (setq arch (replace-match "" t t value))
  1811. (remove-text-properties 0 (length arch)
  1812. '(face t fontified t) arch)))
  1813. )))
  1814. (and cat (set (make-local-variable 'org-category) cat))
  1815. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  1816. (and arch (set (make-local-variable 'org-archive-location) arch))
  1817. (and int (set (make-local-variable 'org-todo-interpretation) int)))
  1818. ;; Compute the regular expressions and other local variables
  1819. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  1820. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  1821. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  1822. (length org-scheduled-string)))
  1823. org-done-string
  1824. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  1825. org-todo-regexp
  1826. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  1827. "\\|") "\\)\\>")
  1828. org-not-done-regexp
  1829. (concat "\\<\\("
  1830. (mapconcat 'regexp-quote
  1831. (nreverse (cdr (reverse org-todo-keywords)))
  1832. "\\|")
  1833. "\\)\\>")
  1834. org-todo-line-regexp
  1835. (concat "^\\(\\*+\\)[ \t]*\\("
  1836. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  1837. "\\)? *\\(.*\\)")
  1838. org-nl-done-regexp
  1839. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  1840. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  1841. org-deadline-regexp (concat "\\<" org-deadline-string)
  1842. org-deadline-time-regexp
  1843. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  1844. org-deadline-line-regexp
  1845. (concat "\\<\\(" org-deadline-string "\\).*")
  1846. org-scheduled-regexp
  1847. (concat "\\<" org-scheduled-string)
  1848. org-scheduled-time-regexp
  1849. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  1850. (org-set-font-lock-defaults)))
  1851. ;; Tell the compiler about dynamically scoped variables,
  1852. ;; and variables from other packages
  1853. (defvar zmacs-regions) ; XEmacs regions
  1854. (defvar original-date) ; dynamically scoped in calendar
  1855. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  1856. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  1857. (defvar org-html-entities) ; defined later in this file
  1858. (defvar org-goto-start-pos) ; dynamically scoped parameter
  1859. (defvar org-time-was-given) ; dynamically scoped parameter
  1860. (defvar org-ts-what) ; dynamically scoped parameter
  1861. (defvar mark-active) ; Emacs only, not available in XEmacs.
  1862. (defvar timecnt) ; dynamically scoped parameter
  1863. (defvar levels-open) ; dynamically scoped parameter
  1864. (defvar title) ; dynamically scoped parameter
  1865. (defvar author) ; dynamically scoped parameter
  1866. (defvar email) ; dynamically scoped parameter
  1867. (defvar text) ; dynamically scoped parameter
  1868. (defvar entry) ; dynamically scoped parameter
  1869. (defvar date) ; dynamically scoped parameter
  1870. (defvar language) ; dynamically scoped parameter
  1871. (defvar options) ; dynamically scoped parameter
  1872. (defvar ans1) ; dynamically scoped parameter
  1873. (defvar ans2) ; dynamically scoped parameter
  1874. (defvar starting-day) ; local variable
  1875. (defvar include-all-loc) ; local variable
  1876. (defvar vm-message-pointer) ; from vm
  1877. (defvar vm-folder-directory) ; from vm
  1878. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  1879. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  1880. (defvar gnus-group-name) ; from gnus
  1881. (defvar gnus-article-current) ; from gnus
  1882. (defvar w3m-current-url) ; from w3m
  1883. (defvar mh-progs) ; from MH-E
  1884. (defvar mh-current-folder) ; from MH-E
  1885. (defvar mh-show-folder-buffer) ; from MH-E
  1886. (defvar mh-index-folder) ; from MH-E
  1887. (defvar mh-searcher) ; from MH-E
  1888. (defvar org-selected-point) ; dynamically scoped parameter
  1889. (defvar calendar-mode-map) ; from calendar.el
  1890. (defvar last-arg) ; local variable
  1891. (defvar remember-save-after-remembering) ; from remember.el
  1892. (defvar remember-data-file) ; from remember.el
  1893. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  1894. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  1895. ;;; Define the mode
  1896. (defvar org-mode-map (copy-keymap outline-mode-map)
  1897. "Keymap for Org-mode.")
  1898. (defvar org-struct-menu) ; defined later in this file
  1899. (defvar org-org-menu) ; defined later in this file
  1900. (defvar org-tbl-menu) ; defined later in this file
  1901. ;; We use a before-change function to check if a table might need
  1902. ;; an update.
  1903. (defvar org-table-may-need-update t
  1904. "Indicates that a table might need an update.
  1905. This variable is set by `org-before-change-function'.
  1906. `org-table-align' sets it back to nil.")
  1907. (defvar org-mode-hook nil)
  1908. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  1909. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  1910. ;;;###autoload
  1911. (define-derived-mode org-mode outline-mode "Org"
  1912. "Outline-based notes management and organizer, alias
  1913. \"Carsten's outline-mode for keeping track of everything.\"
  1914. Org-mode develops organizational tasks around a NOTES file which
  1915. contains information about projects as plain text. Org-mode is
  1916. implemented on top of outline-mode, which is ideal to keep the content
  1917. of large files well structured. It supports ToDo items, deadlines and
  1918. time stamps, which magically appear in the diary listing of the Emacs
  1919. calendar. Tables are easily created with a built-in table editor.
  1920. Plain text URL-like links connect to websites, emails (VM), Usenet
  1921. messages (Gnus), BBDB entries, and any files related to the project.
  1922. For printing and sharing of notes, an Org-mode file (or a part of it)
  1923. can be exported as a structured ASCII or HTML file.
  1924. The following commands are available:
  1925. \\{org-mode-map}"
  1926. (easy-menu-add org-org-menu)
  1927. (easy-menu-add org-tbl-menu)
  1928. (org-install-agenda-files-menu)
  1929. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link . nil)))
  1930. (org-add-to-invisibility-spec '(org-cwidth . nil))
  1931. (setq outline-regexp "\\*+")
  1932. ; (setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  1933. (setq outline-level 'org-outline-level)
  1934. (if org-startup-truncated (setq truncate-lines t))
  1935. (org-set-regexps-and-options)
  1936. (set (make-local-variable 'font-lock-unfontify-region-function)
  1937. 'org-unfontify-region)
  1938. ;; Activate before-change-function
  1939. (set (make-local-variable 'org-table-may-need-update) t)
  1940. (org-add-hook 'before-change-functions 'org-before-change-function nil
  1941. 'local)
  1942. ;; Paragraphs and auto-filling
  1943. (org-set-autofill-regexps)
  1944. (org-update-radio-target-regexp)
  1945. ;; Settings for Calc embedded mode
  1946. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  1947. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  1948. (if (and org-insert-mode-line-in-empty-file
  1949. (interactive-p)
  1950. (= (point-min) (point-max)))
  1951. (insert " -*- mode: org -*-\n\n"))
  1952. ;; Get rid of Outline menus, they are not needed
  1953. ;; Need to do this here because define-derived-mode sets up
  1954. ;; the keymap so late.
  1955. (if (featurep 'xemacs)
  1956. (progn
  1957. (delete-menu-item '("Headings"))
  1958. (delete-menu-item '("Show"))
  1959. (delete-menu-item '("Hide"))
  1960. (set-menubar-dirty-flag))
  1961. (define-key org-mode-map [menu-bar headings] 'undefined)
  1962. (define-key org-mode-map [menu-bar hide] 'undefined)
  1963. (define-key org-mode-map [menu-bar show] 'undefined))
  1964. (unless org-inhibit-startup
  1965. (if org-startup-align-all-tables
  1966. (org-table-map-tables 'org-table-align))
  1967. (if org-startup-with-deadline-check
  1968. (call-interactively 'org-check-deadlines)
  1969. (cond
  1970. ((eq org-startup-folded t)
  1971. (org-cycle '(4)))
  1972. ((eq org-startup-folded 'content)
  1973. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  1974. (org-cycle '(4)) (org-cycle '(4))))))))
  1975. (defsubst org-current-line (&optional pos)
  1976. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  1977. (defsubst org-match-string-no-properties (num &optional string)
  1978. (if (featurep 'xemacs)
  1979. (match-string num string)
  1980. (match-string-no-properties num string)))
  1981. (defun org-current-time ()
  1982. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  1983. (if (> org-time-stamp-rounding-minutes 0)
  1984. (let ((r org-time-stamp-rounding-minutes)
  1985. (time (decode-time)))
  1986. (apply 'encode-time
  1987. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  1988. (nthcdr 2 time))))
  1989. (current-time)))
  1990. ;; FIXME: Do we need to copy?
  1991. (defun org-string-props (string &rest properties)
  1992. "Add PROPERTIES to string."
  1993. (add-text-properties 0 (length string) properties string)
  1994. string)
  1995. ;;; Font-Lock stuff
  1996. (defvar org-mouse-map (make-sparse-keymap))
  1997. (define-key org-mouse-map
  1998. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  1999. (define-key org-mouse-map
  2000. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2001. (define-key org-mouse-map [follow-link] 'mouse-face)
  2002. (when org-tab-follows-link
  2003. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2004. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2005. (when org-return-follows-link
  2006. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2007. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2008. (require 'font-lock)
  2009. ;; FIXME: Can/should we add the brackets?
  2010. (defconst org-non-link-chars "\t\n\r|<>\000")
  2011. (defconst org-link-regexp
  2012. (if org-allow-space-in-links
  2013. (concat
  2014. "\\(https?\\|ftp\\|mailto\\|file\\|news\\|bbdb\\|vm\\|wl\\|mhe\\|rmail\\|gnus\\|shell\\):\\([^" org-non-link-chars "]+[^ " org-non-link-chars "]\\)")
  2015. (concat
  2016. "\\(https?\\|ftp\\|mailto\\|file\\|news\\|bbdb\\|vm\\|wl\\|mhe\\|rmail\\|gnus\\|shell\\):\\([^ " org-non-link-chars "]+\\)")
  2017. )
  2018. "Regular expression for matching links.")
  2019. (defconst org-link-maybe-angles-regexp
  2020. (concat "<?\\(" org-link-regexp "\\)>?")
  2021. "Matches a link and optionally surrounding angle brackets.")
  2022. (defconst org-protected-link-regexp
  2023. (concat "\000" org-link-regexp "\000")
  2024. "Matches a link and optionally surrounding angle brackets.")
  2025. (defconst org-bracket-link-regexp
  2026. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2027. "Matches a link in double brackets.")
  2028. (defconst org-ts-lengths
  2029. (cons (length (format-time-string (car org-time-stamp-formats)))
  2030. (length (format-time-string (cdr org-time-stamp-formats))))
  2031. "This holds the lengths of the two different time formats.")
  2032. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2033. "Regular expression for fast time stamp matching.")
  2034. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2035. "Regular expression for fast time stamp matching.")
  2036. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2037. "Regular expression matching time strings for analysis.")
  2038. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2039. "Regular expression matching time stamps, with groups.")
  2040. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2041. "Regular expression matching a time stamp range.")
  2042. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2043. org-ts-regexp "\\)?")
  2044. "Regular expression matching a time stamp or time stamp range.")
  2045. (defun org-activate-links (limit)
  2046. "Run through the buffer and add overlays to links."
  2047. (if (re-search-forward org-link-regexp limit t)
  2048. (progn
  2049. (add-text-properties (match-beginning 0) (match-end 0)
  2050. (list 'mouse-face 'highlight
  2051. 'keymap org-mouse-map
  2052. ))
  2053. t)))
  2054. (defun org-activate-links2 (limit)
  2055. "Run through the buffer and add overlays to bracketed links."
  2056. (if (re-search-forward org-bracket-link-regexp limit t)
  2057. (let* ((help (concat "LINK: " (org-match-string-no-properties 1)))
  2058. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2059. 'keymap org-mouse-map 'mouse-face 'highlight
  2060. 'help-echo help))
  2061. (vp (list 'rear-nonsticky t
  2062. 'keymap org-mouse-map 'mouse-face 'highlight
  2063. 'help-echo help)))
  2064. ;; We need to remove the invisible property here, because
  2065. ;; of the table width code.... FIXME: explain better.
  2066. (remove-text-properties (match-beginning 0) (match-end 0)
  2067. '(invisible nil))
  2068. (if (match-end 3)
  2069. (progn
  2070. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2071. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2072. (add-text-properties (match-end 3) (match-end 0) ip))
  2073. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2074. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2075. (add-text-properties (match-end 1) (match-end 0) ip))
  2076. t)))
  2077. (defun org-activate-dates (limit)
  2078. "Run through the buffer and add overlays to dates."
  2079. (if (re-search-forward org-tsr-regexp limit t)
  2080. (progn
  2081. (add-text-properties (match-beginning 0) (match-end 0)
  2082. (list 'mouse-face 'highlight
  2083. 'keymap org-mouse-map))
  2084. t)))
  2085. (defvar org-target-link-regexp nil
  2086. "Regular expression matching radio targets in plain text.")
  2087. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2088. "Regular expression matching a link target.")
  2089. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2090. "Regular expression matching a link target.")
  2091. (defun org-activate-target-links (limit)
  2092. "Run through the buffer and add overlays to target matches."
  2093. (when (and org-radio-targets org-target-link-regexp)
  2094. (let ((case-fold-search t))
  2095. (if (re-search-forward org-target-link-regexp limit t)
  2096. (progn
  2097. (add-text-properties (match-beginning 0) (match-end 0)
  2098. (list 'mouse-face 'highlight
  2099. 'keymap org-mouse-map
  2100. 'help-echo "Radio target link"
  2101. 'org-linked-text t))
  2102. t)))))
  2103. (defun org-update-radio-target-regexp ()
  2104. "Find all radio targets in this file and update the regular expression."
  2105. (interactive)
  2106. (when org-radio-targets
  2107. (setq org-target-link-regexp
  2108. (org-make-target-link-regexp (org-all-targets 'radio)))
  2109. (org-restart-font-lock)))
  2110. (defun org-hide-wide-columns (limit)
  2111. (when org-table-limit-column-width
  2112. (let (s e)
  2113. (setq s (text-property-any (point) (or limit (point-max))
  2114. 'org-cwidth t))
  2115. (when s
  2116. (setq e (next-single-property-change s 'org-cwidth))
  2117. (add-text-properties s e '(invisible org-cwidth intangible t))
  2118. (goto-char e)
  2119. t))))
  2120. (defun org-restart-font-lock ()
  2121. "Restart font-lock-mode, to force refontification."
  2122. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2123. (font-lock-mode -1)
  2124. (font-lock-mode 1)))
  2125. (defun org-all-targets (&optional radio)
  2126. "Return a list of all targets in this file.
  2127. With optional argument RADIO, only find radio targets."
  2128. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2129. rtn)
  2130. (save-excursion
  2131. (goto-char (point-min))
  2132. (while (re-search-forward re nil t)
  2133. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2134. rtn)))
  2135. (defun org-make-target-link-regexp (targets)
  2136. "Make regular expression matching all strings in TARGETS.
  2137. The regular expression finds the targets also if there is a line break
  2138. between words."
  2139. (and targets
  2140. (concat
  2141. "\\<\\("
  2142. (mapconcat
  2143. (lambda (x)
  2144. (while (string-match " +" x)
  2145. (setq x (replace-match "\\s-+" t t x)))
  2146. x)
  2147. targets
  2148. "\\|")
  2149. "\\)\\>")))
  2150. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2151. "Matches CamelCase words, possibly with a star before it.")
  2152. (defun org-activate-camels (limit)
  2153. "Run through the buffer and add overlays to dates."
  2154. (if org-activate-camels
  2155. (if (re-search-forward org-camel-regexp limit t)
  2156. (progn
  2157. (add-text-properties (match-beginning 0) (match-end 0)
  2158. (list 'mouse-face 'highlight
  2159. 'keymap org-mouse-map))
  2160. t))))
  2161. (defun org-activate-tags (limit)
  2162. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2163. (progn
  2164. (add-text-properties (match-beginning 1) (match-end 1)
  2165. (list 'mouse-face 'highlight
  2166. 'keymap org-mouse-map))
  2167. t)))
  2168. (defun org-font-lock-level ()
  2169. (save-excursion
  2170. (org-back-to-heading t)
  2171. (- (match-end 0) (match-beginning 0))))
  2172. (defun org-outline-level ()
  2173. (save-excursion
  2174. (looking-at outline-regexp)
  2175. (if (match-beginning 1)
  2176. (+ (org-get-string-indentation (match-string 1)) 1000)
  2177. (- (match-end 0) (match-beginning 0)))))
  2178. (defvar org-font-lock-keywords nil)
  2179. (defun org-set-font-lock-defaults ()
  2180. (let* ((em org-fontify-emphasized-text)
  2181. (org-font-lock-extra-keywords
  2182. (list
  2183. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2184. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2185. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2186. (1 'org-table))
  2187. '(org-activate-links (0 'org-link t))
  2188. '(org-activate-links2 (0 'org-link t))
  2189. '(org-activate-target-links (0 'org-link t))
  2190. '(org-activate-dates (0 'org-link t))
  2191. '(org-activate-camels (0 'org-link t))
  2192. '(org-activate-tags (1 'org-tag append))
  2193. '(org-hide-wide-columns (0 nil append))
  2194. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2195. '(1 'org-warning t))
  2196. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2197. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2198. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2199. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2200. (if em '("\\(\\W\\|^\\)\\(\\*\\w+\\*\\)\\(\\W\\|$\\)" 2 'bold append))
  2201. (if em '("\\(\\W\\|^\\)\\(/\\w+/\\)\\(\\W\\|$\\)" 2 'italic append))
  2202. (if em '("\\(\\W\\|^\\)\\(_\\w+_\\)\\(\\W\\|$\\)" 2 'underline append))
  2203. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2204. "\\|" org-quote-string "\\)\\>")
  2205. '(1 'org-special-keyword t))
  2206. '("^#.*" (0 'font-lock-comment-face t))
  2207. (if org-fontify-done-headline
  2208. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2209. '(1 'org-done t) '(2 'org-headline-done t))
  2210. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2211. '(1 'org-done t)))
  2212. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2213. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2214. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2215. (if org-format-transports-properties-p
  2216. '("| *\\(<[0-9]+>\\) *|" (1 'org-formula t)))
  2217. )))
  2218. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2219. ;; Now set the full font-lock-keywords
  2220. (set (make-local-variable 'org-font-lock-keywords)
  2221. org-font-lock-extra-keywords)
  2222. (set (make-local-variable 'font-lock-defaults)
  2223. '(org-font-lock-keywords t nil nil backward-paragraph))
  2224. (kill-local-variable 'font-lock-keywords) nil))
  2225. (defvar org-m nil)
  2226. (defvar org-l nil)
  2227. (defvar org-f nil)
  2228. (defun org-get-level-face (n)
  2229. "Get the right face for match N in font-lock matching of healdines."
  2230. (setq org-l (- (match-end 2) (match-beginning 1)))
  2231. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2232. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2233. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2234. (cond
  2235. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2236. ((eq n 2) org-f)
  2237. (t (if org-level-color-stars-only nil org-f))))
  2238. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2239. "Remove fontification and activation overlays from links."
  2240. (font-lock-default-unfontify-region beg end)
  2241. (let* ((buffer-undo-list t)
  2242. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2243. (inhibit-modification-hooks t)
  2244. deactivate-mark buffer-file-name buffer-file-truename)
  2245. (remove-text-properties beg end
  2246. '(mouse-face nil keymap nil org-linked-text nil
  2247. invisible nil intangible nil))))
  2248. ; intangible nil))))
  2249. ;;; FIXME: Is is a problem that we remove invisible here??????
  2250. ;;; Visibility cycling
  2251. (defvar org-cycle-global-status nil)
  2252. (defvar org-cycle-subtree-status nil)
  2253. (defun org-cycle (&optional arg)
  2254. "Visibility cycling for Org-mode.
  2255. - When this function is called with a prefix argument, rotate the entire
  2256. buffer through 3 states (global cycling)
  2257. 1. OVERVIEW: Show only top-level headlines.
  2258. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2259. 3. SHOW ALL: Show everything.
  2260. - When point is at the beginning of a headline, rotate the subtree started
  2261. by this line through 3 different states (local cycling)
  2262. 1. FOLDED: Only the main headline is shown.
  2263. 2. CHILDREN: The main headline and the direct children are shown.
  2264. From this state, you can move to one of the children
  2265. and zoom in further.
  2266. 3. SUBTREE: Show the entire subtree, including body text.
  2267. - When there is a numeric prefix, go up to a heading with level ARG, do
  2268. a `show-subtree' and return to the previous cursor position. If ARG
  2269. is negative, go up that many levels.
  2270. - When point is not at the beginning of a headline, execute
  2271. `indent-relative', like TAB normally does. See the option
  2272. `org-cycle-emulate-tab' for details.
  2273. - Special case: if point is the the beginning of the buffer and there is
  2274. no headline in line 1, this function will act as if called with prefix arg."
  2275. (interactive "P")
  2276. (if (or (and (bobp) (not (looking-at outline-regexp)))
  2277. (equal arg '(4)))
  2278. ;; special case: use global cycling
  2279. (setq arg t))
  2280. (let ((outline-regexp
  2281. (if org-cycle-include-plain-lists
  2282. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2283. outline-regexp)))
  2284. (cond
  2285. ((org-at-table-p 'any)
  2286. ;; Enter the table or move to the next field in the table
  2287. (or (org-table-recognize-table.el)
  2288. (progn
  2289. (org-table-justify-field-maybe)
  2290. (org-table-next-field))))
  2291. ((eq arg t) ;; Global cycling
  2292. (cond
  2293. ((and (eq last-command this-command)
  2294. (eq org-cycle-global-status 'overview))
  2295. ;; We just created the overview - now do table of contents
  2296. ;; This can be slow in very large buffers, so indicate action
  2297. (message "CONTENTS...")
  2298. (save-excursion
  2299. ;; Visit all headings and show their offspring
  2300. (goto-char (point-max))
  2301. (catch 'exit
  2302. (while (and (progn (condition-case nil
  2303. (outline-previous-visible-heading 1)
  2304. (error (goto-char (point-min))))
  2305. t)
  2306. (looking-at outline-regexp))
  2307. (show-branches)
  2308. (if (bobp) (throw 'exit nil))))
  2309. (message "CONTENTS...done"))
  2310. (setq org-cycle-global-status 'contents)
  2311. (run-hook-with-args 'org-cycle-hook 'contents))
  2312. ((and (eq last-command this-command)
  2313. (eq org-cycle-global-status 'contents))
  2314. ;; We just showed the table of contents - now show everything
  2315. (show-all)
  2316. (message "SHOW ALL")
  2317. (setq org-cycle-global-status 'all)
  2318. (run-hook-with-args 'org-cycle-hook 'all))
  2319. (t
  2320. ;; Default action: go to overview
  2321. (hide-sublevels 1)
  2322. (message "OVERVIEW")
  2323. (setq org-cycle-global-status 'overview)
  2324. (run-hook-with-args 'org-cycle-hook 'overview))))
  2325. ((integerp arg)
  2326. ;; Show-subtree, ARG levels up from here.
  2327. (save-excursion
  2328. (org-back-to-heading)
  2329. (outline-up-heading (if (< arg 0) (- arg)
  2330. (- (funcall outline-level) arg)))
  2331. (org-show-subtree)))
  2332. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2333. ;; At a heading: rotate between three different views
  2334. (org-back-to-heading)
  2335. (let ((goal-column 0) eoh eol eos)
  2336. ;; First, some boundaries
  2337. (save-excursion
  2338. (org-back-to-heading)
  2339. (save-excursion
  2340. (beginning-of-line 2)
  2341. (while (and (not (eobp)) ;; this is like `next-line'
  2342. (get-char-property (1- (point)) 'invisible))
  2343. (beginning-of-line 2)) (setq eol (point)))
  2344. (outline-end-of-heading) (setq eoh (point))
  2345. (org-end-of-subtree t) (setq eos (point))
  2346. (outline-next-heading))
  2347. ;; Find out what to do next and set `this-command'
  2348. (cond
  2349. ((= eos eoh)
  2350. ;; Nothing is hidden behind this heading
  2351. (message "EMPTY ENTRY")
  2352. (setq org-cycle-subtree-status nil))
  2353. ((>= eol eos)
  2354. ;; Entire subtree is hidden in one line: open it
  2355. (org-show-entry)
  2356. (show-children)
  2357. (message "CHILDREN")
  2358. (setq org-cycle-subtree-status 'children)
  2359. (run-hook-with-args 'org-cycle-hook 'children))
  2360. ((and (eq last-command this-command)
  2361. (eq org-cycle-subtree-status 'children))
  2362. ;; We just showed the children, now show everything.
  2363. (org-show-subtree)
  2364. (message "SUBTREE")
  2365. (setq org-cycle-subtree-status 'subtree)
  2366. (run-hook-with-args 'org-cycle-hook 'subtree))
  2367. (t
  2368. ;; Default action: hide the subtree.
  2369. (hide-subtree)
  2370. (message "FOLDED")
  2371. (setq org-cycle-subtree-status 'folded)
  2372. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2373. ;; TAB emulation
  2374. (buffer-read-only (org-back-to-heading))
  2375. ((if (and (eq org-cycle-emulate-tab 'white)
  2376. (save-excursion (beginning-of-line 1) (looking-at "[ \t]+$")))
  2377. t
  2378. (eq org-cycle-emulate-tab t))
  2379. (if (and (looking-at "[ \n\r\t]")
  2380. (string-match "^[ \t]*$" (buffer-substring
  2381. (point-at-bol) (point))))
  2382. (progn
  2383. (beginning-of-line 1)
  2384. (and (looking-at "[ \t]+") (replace-match ""))))
  2385. (indent-relative))
  2386. (t (save-excursion
  2387. (org-back-to-heading)
  2388. (org-cycle))))))
  2389. (defun org-optimize-window-after-visibility-change (state)
  2390. "Adjust the window after a change in outline visibility.
  2391. This function is the default value of the hook `org-cycle-hook'."
  2392. (cond
  2393. ((eq state 'overview) (org-first-headline-recenter 1))
  2394. ((eq state 'content) nil)
  2395. ((eq state 'all) nil)
  2396. ((eq state 'folded) nil)
  2397. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  2398. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1)))))
  2399. (defun org-subtree-end-visible-p ()
  2400. "Is the end of the current subtree visible?"
  2401. (pos-visible-in-window-p
  2402. (save-excursion (org-end-of-subtree t) (point))))
  2403. (defun org-first-headline-recenter (&optional N)
  2404. "Move cursor to the first headline and recenter the headline.
  2405. Optional argument N means, put the headline into the Nth line of the window."
  2406. (goto-char (point-min))
  2407. (when (re-search-forward (concat "^" outline-regexp) nil t)
  2408. (beginning-of-line)
  2409. (recenter (prefix-numeric-value N))))
  2410. (defvar org-goto-window-configuration nil)
  2411. (defvar org-goto-marker nil)
  2412. (defvar org-goto-map (make-sparse-keymap))
  2413. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  2414. (while (setq cmd (pop cmds))
  2415. (substitute-key-definition cmd cmd org-goto-map global-map)))
  2416. (define-key org-goto-map "\C-m" 'org-goto-ret)
  2417. (define-key org-goto-map [(left)] 'org-goto-left)
  2418. (define-key org-goto-map [(right)] 'org-goto-right)
  2419. (define-key org-goto-map [(?q)] 'org-goto-quit)
  2420. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  2421. (define-key org-goto-map "\C-i" 'org-cycle)
  2422. (define-key org-goto-map [(tab)] 'org-cycle)
  2423. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  2424. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  2425. (define-key org-goto-map "n" 'outline-next-visible-heading)
  2426. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  2427. (define-key org-goto-map "f" 'outline-forward-same-level)
  2428. (define-key org-goto-map "b" 'outline-backward-same-level)
  2429. (define-key org-goto-map "u" 'outline-up-heading)
  2430. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  2431. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  2432. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  2433. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  2434. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  2435. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2436. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  2437. (defconst org-goto-help
  2438. "Select a location to jump to, press RET
  2439. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  2440. (defun org-goto ()
  2441. "Go to a different location of the document, keeping current visibility.
  2442. When you want to go to a different location in a document, the fastest way
  2443. is often to fold the entire buffer and then dive into the tree. This
  2444. method has the disadvantage, that the previous location will be folded,
  2445. which may not be what you want.
  2446. This command works around this by showing a copy of the current buffer in
  2447. overview mode. You can dive into the tree in that copy, to find the
  2448. location you want to reach. When pressing RET, the command returns to the
  2449. original buffer in which the visibility is still unchanged. It then jumps
  2450. to the new location, making it and the headline hierarchy above it visible."
  2451. (interactive)
  2452. (let* ((org-goto-start-pos (point))
  2453. (selected-point
  2454. (org-get-location (current-buffer) org-goto-help)))
  2455. (if selected-point
  2456. (progn
  2457. (org-mark-ring-push org-goto-start-pos)
  2458. (goto-char selected-point)
  2459. (if (or (org-invisible-p) (org-invisible-p2))
  2460. (org-show-hierarchy-above)))
  2461. (error "Quit"))))
  2462. (defun org-get-location (buf help)
  2463. "Let the user select a location in the Org-mode buffer BUF.
  2464. This function uses a recursive edit. It returns the selected position
  2465. or nil."
  2466. (let (org-selected-point)
  2467. (save-excursion
  2468. (save-window-excursion
  2469. (delete-other-windows)
  2470. (switch-to-buffer (get-buffer-create "*org-goto*"))
  2471. (with-output-to-temp-buffer "*Help*"
  2472. (princ help))
  2473. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  2474. (setq buffer-read-only nil)
  2475. (erase-buffer)
  2476. (insert-buffer-substring buf)
  2477. (let ((org-startup-truncated t)
  2478. (org-startup-folded t)
  2479. (org-startup-align-all-tables nil)
  2480. (org-startup-with-deadline-check nil))
  2481. (org-mode))
  2482. (setq buffer-read-only t)
  2483. (if (boundp 'org-goto-start-pos)
  2484. (goto-char org-goto-start-pos)
  2485. (goto-char (point-min)))
  2486. (org-beginning-of-line)
  2487. (message "Select location and press RET")
  2488. ;; now we make sure that during selection, ony very few keys work
  2489. ;; and that it is impossible to switch to another window.
  2490. (let ((gm (current-global-map))
  2491. (overriding-local-map org-goto-map))
  2492. (unwind-protect
  2493. (progn
  2494. (use-global-map org-goto-map)
  2495. (recursive-edit))
  2496. (use-global-map gm)))))
  2497. (kill-buffer "*org-goto*")
  2498. org-selected-point))
  2499. ;; FIXME: It may not be a good idea to temper with the prefix argument...
  2500. (defun org-goto-ret (&optional arg)
  2501. "Finish `org-goto' by going to the new location."
  2502. (interactive "P")
  2503. (setq org-selected-point (point)
  2504. current-prefix-arg arg)
  2505. (throw 'exit nil))
  2506. (defun org-goto-left ()
  2507. "Finish `org-goto' by going to the new location."
  2508. (interactive)
  2509. (if (org-on-heading-p)
  2510. (progn
  2511. (beginning-of-line 1)
  2512. (setq org-selected-point (point)
  2513. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2514. (throw 'exit nil))
  2515. (error "Not on a heading")))
  2516. (defun org-goto-right ()
  2517. "Finish `org-goto' by going to the new location."
  2518. (interactive)
  2519. (if (org-on-heading-p)
  2520. (progn
  2521. (outline-end-of-subtree)
  2522. (or (eobp) (forward-char 1))
  2523. (setq org-selected-point (point)
  2524. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  2525. (throw 'exit nil))
  2526. (error "Not on a heading")))
  2527. (defun org-goto-quit ()
  2528. "Finish `org-goto' without cursor motion."
  2529. (interactive)
  2530. (setq org-selected-point nil)
  2531. (throw 'exit nil))
  2532. ;;; Promotion, Demotion, Inserting new headlines
  2533. (defvar org-ignore-region nil
  2534. "To temporarily disable the active region.")
  2535. (defun org-insert-heading (&optional force-heading)
  2536. "Insert a new heading or item with same depth at point."
  2537. (interactive "P")
  2538. (when (or force-heading (not (org-insert-item)))
  2539. (let* ((head (save-excursion
  2540. (condition-case nil
  2541. (org-back-to-heading)
  2542. (error (outline-next-heading)))
  2543. (prog1 (match-string 0)
  2544. (funcall outline-level)))))
  2545. (unless (bolp) (newline))
  2546. (insert head)
  2547. (unless (eolp)
  2548. (save-excursion (newline-and-indent)))
  2549. (unless (equal (char-before) ?\ )
  2550. (insert " "))
  2551. (run-hooks 'org-insert-heading-hook))))
  2552. (defun org-insert-item ()
  2553. "Insert a new item at the current level.
  2554. Return t when things worked, nil when we are not in an item."
  2555. (when (save-excursion
  2556. (condition-case nil
  2557. (progn
  2558. (org-beginning-of-item)
  2559. (org-at-item-p)
  2560. t)
  2561. (error nil)))
  2562. (unless (bolp) (newline))
  2563. (insert (match-string 0))
  2564. (org-maybe-renumber-ordered-list)
  2565. t))
  2566. (defun org-insert-todo-heading (arg)
  2567. "Insert a new heading with the same level and TODO state as current heading.
  2568. If the heading has no TODO state, or if the state is DONE, use the first
  2569. state (TODO by default). Also with prefix arg, force first state."
  2570. (interactive "P")
  2571. (org-insert-heading)
  2572. (save-excursion
  2573. (org-back-to-heading)
  2574. (outline-previous-heading)
  2575. (looking-at org-todo-line-regexp))
  2576. (if (or arg
  2577. (not (match-beginning 2))
  2578. (equal (match-string 2) org-done-string))
  2579. (insert (car org-todo-keywords) " ")
  2580. (insert (match-string 2) " ")))
  2581. (defun org-promote-subtree ()
  2582. "Promote the entire subtree.
  2583. See also `org-promote'."
  2584. (interactive)
  2585. (save-excursion
  2586. (org-map-tree 'org-promote)))
  2587. (defun org-demote-subtree ()
  2588. "Demote the entire subtree. See `org-demote'.
  2589. See also `org-promote'."
  2590. (interactive)
  2591. (save-excursion
  2592. (org-map-tree 'org-demote)))
  2593. (defun org-do-promote ()
  2594. "Promote the current heading higher up the tree.
  2595. If the region is active in `transient-mark-mode', promote all headings
  2596. in the region."
  2597. (interactive)
  2598. (save-excursion
  2599. (if (org-region-active-p)
  2600. (org-map-region 'org-promote (region-beginning) (region-end))
  2601. (org-promote)))
  2602. (org-fix-position-after-promote))
  2603. (defun org-do-demote ()
  2604. "Demote the current heading lower down the tree.
  2605. If the region is active in `transient-mark-mode', demote all headings
  2606. in the region."
  2607. (interactive)
  2608. (save-excursion
  2609. (if (org-region-active-p)
  2610. (org-map-region 'org-demote (region-beginning) (region-end))
  2611. (org-demote)))
  2612. (org-fix-position-after-promote))
  2613. (defun org-fix-position-after-promote ()
  2614. "Make sure that after pro/demotion cursor position is right."
  2615. (and (equal (char-after) ?\ )
  2616. (equal (char-before) ?*)
  2617. (forward-char 1)))
  2618. (defun org-get-legal-level (level change)
  2619. "Rectify a level change under the influence of `org-odd-levels-only'
  2620. LEVEL is a current level, CHANGE is by how much the level should be
  2621. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  2622. even level numbers will become the next higher odd number."
  2623. (if org-odd-levels-only
  2624. (cond ((not change) (1+ (* 2 (/ level 2))))
  2625. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  2626. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  2627. (max 1 (+ level change))))
  2628. (defun org-promote ()
  2629. "Promote the current heading higher up the tree.
  2630. If the region is active in `transient-mark-mode', promote all headings
  2631. in the region."
  2632. (org-back-to-heading t)
  2633. (let* ((level (save-match-data (funcall outline-level)))
  2634. (up-head (make-string (org-get-legal-level level -1) ?*))
  2635. (diff (abs (- level (length up-head)))))
  2636. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  2637. (replace-match up-head nil t)
  2638. ;; Fixup tag positioning
  2639. (and org-auto-align-tags (org-set-tags nil t))
  2640. (if org-adapt-indentation
  2641. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  2642. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  2643. (defun org-demote ()
  2644. "Demote the current heading lower down the tree.
  2645. If the region is active in `transient-mark-mode', demote all headings
  2646. in the region."
  2647. (org-back-to-heading t)
  2648. (let* ((level (save-match-data (funcall outline-level)))
  2649. (down-head (make-string (org-get-legal-level level 1) ?*))
  2650. (diff (abs (- level (length down-head)))))
  2651. (replace-match down-head nil t)
  2652. ;; Fixup tag positioning
  2653. (and org-auto-align-tags (org-set-tags nil t))
  2654. (if org-adapt-indentation
  2655. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  2656. (defun org-map-tree (fun)
  2657. "Call FUN for every heading underneath the current one."
  2658. (org-back-to-heading)
  2659. (let ((level (funcall outline-level)))
  2660. (save-excursion
  2661. (funcall fun)
  2662. (while (and (progn
  2663. (outline-next-heading)
  2664. (> (funcall outline-level) level))
  2665. (not (eobp)))
  2666. (funcall fun)))))
  2667. (defun org-map-region (fun beg end)
  2668. "Call FUN for every heading between BEG and END."
  2669. (let ((org-ignore-region t))
  2670. (save-excursion
  2671. (setq end (copy-marker end))
  2672. (goto-char beg)
  2673. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  2674. (< (point) end))
  2675. (funcall fun))
  2676. (while (and (progn
  2677. (outline-next-heading)
  2678. (< (point) end))
  2679. (not (eobp)))
  2680. (funcall fun)))))
  2681. (defun org-fixup-indentation (from to prohibit)
  2682. "Change the indentation in the current entry by re-replacing FROM with TO.
  2683. However, if the regexp PROHIBIT matches at all, don't do anything.
  2684. This is being used to change indentation along with the length of the
  2685. heading marker. But if there are any lines which are not indented, nothing
  2686. is changed at all."
  2687. (save-excursion
  2688. (let ((end (save-excursion (outline-next-heading)
  2689. (point-marker))))
  2690. (unless (save-excursion (re-search-forward prohibit end t))
  2691. (while (re-search-forward from end t)
  2692. (replace-match to)
  2693. (beginning-of-line 2)))
  2694. (move-marker end nil))))
  2695. ;;; Vertical tree motion, cutting and pasting of subtrees
  2696. (defun org-move-subtree-up (&optional arg)
  2697. "Move the current subtree up past ARG headlines of the same level."
  2698. (interactive "p")
  2699. (org-move-subtree-down (- (prefix-numeric-value arg))))
  2700. (defun org-move-subtree-down (&optional arg)
  2701. "Move the current subtree down past ARG headlines of the same level."
  2702. (interactive "p")
  2703. (setq arg (prefix-numeric-value arg))
  2704. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  2705. 'outline-get-last-sibling))
  2706. (ins-point (make-marker))
  2707. (cnt (abs arg))
  2708. beg end txt folded)
  2709. ;; Select the tree
  2710. (org-back-to-heading)
  2711. (setq beg (point))
  2712. (save-match-data
  2713. (save-excursion (outline-end-of-heading)
  2714. (setq folded (org-invisible-p)))
  2715. (outline-end-of-subtree))
  2716. (outline-next-heading)
  2717. (setq end (point))
  2718. ;; Find insertion point, with error handling
  2719. (goto-char beg)
  2720. (while (> cnt 0)
  2721. (or (and (funcall movfunc) (looking-at outline-regexp))
  2722. (progn (goto-char beg)
  2723. (error "Cannot move past superior level or buffer limit")))
  2724. (setq cnt (1- cnt)))
  2725. (if (> arg 0)
  2726. ;; Moving forward - still need to move over subtree
  2727. (progn (outline-end-of-subtree)
  2728. (outline-next-heading)
  2729. (if (not (or (looking-at (concat "^" outline-regexp))
  2730. (bolp)))
  2731. (newline))))
  2732. (move-marker ins-point (point))
  2733. (setq txt (buffer-substring beg end))
  2734. (delete-region beg end)
  2735. (insert txt)
  2736. (goto-char ins-point)
  2737. (if folded (hide-subtree))
  2738. (move-marker ins-point nil)))
  2739. (defvar org-subtree-clip ""
  2740. "Clipboard for cut and paste of subtrees.
  2741. This is actually only a copy of the kill, because we use the normal kill
  2742. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  2743. (defvar org-subtree-clip-folded nil
  2744. "Was the last copied subtree folded?
  2745. This is used to fold the tree back after pasting.")
  2746. (defun org-cut-subtree ()
  2747. "Cut the current subtree into the clipboard.
  2748. This is a short-hand for marking the subtree and then cutting it."
  2749. (interactive)
  2750. (org-copy-subtree 'cut))
  2751. (defun org-copy-subtree (&optional cut)
  2752. "Cut the current subtree into the clipboard.
  2753. This is a short-hand for marking the subtree and then copying it.
  2754. If CUT is non nil, actually cut the subtree."
  2755. (interactive)
  2756. (let (beg end folded)
  2757. (org-back-to-heading)
  2758. (setq beg (point))
  2759. (save-match-data
  2760. (save-excursion (outline-end-of-heading)
  2761. (setq folded (org-invisible-p)))
  2762. (outline-end-of-subtree))
  2763. (if (equal (char-after) ?\n) (forward-char 1))
  2764. (setq end (point))
  2765. (goto-char beg)
  2766. (when (> end beg)
  2767. (setq org-subtree-clip-folded folded)
  2768. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  2769. (setq org-subtree-clip (current-kill 0))
  2770. (message "%s: Subtree with %d characters"
  2771. (if cut "Cut" "Copied")
  2772. (length org-subtree-clip)))))
  2773. (defun org-paste-subtree (&optional level tree)
  2774. "Paste the clipboard as a subtree, with modification of headline level.
  2775. The entire subtree is promoted or demoted in order to match a new headline
  2776. level. By default, the new level is derived from the visible headings
  2777. before and after the insertion point, and taken to be the inferior headline
  2778. level of the two. So if the previous visible heading is level 3 and the
  2779. next is level 4 (or vice versa), level 4 will be used for insertion.
  2780. This makes sure that the subtree remains an independent subtree and does
  2781. not swallow low level entries.
  2782. You can also force a different level, either by using a numeric prefix
  2783. argument, or by inserting the heading marker by hand. For example, if the
  2784. cursor is after \"*****\", then the tree will be shifted to level 5.
  2785. If you want to insert the tree as is, just use \\[yank].
  2786. If optional TREE is given, use this text instead of the kill ring."
  2787. (interactive "P")
  2788. (unless (org-kill-is-subtree-p tree)
  2789. (error
  2790. (substitute-command-keys
  2791. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  2792. (let* ((txt (or tree (current-kill 0)))
  2793. (^re (concat "^\\(" outline-regexp "\\)"))
  2794. (re (concat "\\(" outline-regexp "\\)"))
  2795. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  2796. (old-level (if (string-match ^re txt)
  2797. (- (match-end 0) (match-beginning 0))
  2798. -1))
  2799. (force-level (cond (level (prefix-numeric-value level))
  2800. ((string-match
  2801. ^re_ (buffer-substring (point-at-bol) (point)))
  2802. (- (match-end 0) (match-beginning 0)))
  2803. (t nil)))
  2804. (previous-level (save-excursion
  2805. (condition-case nil
  2806. (progn
  2807. (outline-previous-visible-heading 1)
  2808. (if (looking-at re)
  2809. (- (match-end 0) (match-beginning 0))
  2810. 1))
  2811. (error 1))))
  2812. (next-level (save-excursion
  2813. (condition-case nil
  2814. (progn
  2815. (outline-next-visible-heading 1)
  2816. (if (looking-at re)
  2817. (- (match-end 0) (match-beginning 0))
  2818. 1))
  2819. (error 1))))
  2820. (new-level (or force-level (max previous-level next-level)))
  2821. (shift (if (or (= old-level -1)
  2822. (= new-level -1)
  2823. (= old-level new-level))
  2824. 0
  2825. (- new-level old-level)))
  2826. (shift1 shift)
  2827. (delta (if (> shift 0) -1 1))
  2828. (func (if (> shift 0) 'org-demote 'org-promote))
  2829. (org-odd-levels-only nil)
  2830. beg end)
  2831. ;; Remove the forces level indicator
  2832. (if force-level
  2833. (delete-region (point-at-bol) (point)))
  2834. ;; Make sure we start at the beginning of an empty line
  2835. (if (not (bolp)) (insert "\n"))
  2836. (if (not (looking-at "[ \t]*$"))
  2837. (progn (insert "\n") (backward-char 1)))
  2838. ;; Paste
  2839. (setq beg (point))
  2840. (insert txt)
  2841. (setq end (point))
  2842. (goto-char beg)
  2843. ;; Shift if necessary
  2844. (if (= shift 0)
  2845. (message "Pasted at level %d, without shift" new-level)
  2846. (save-restriction
  2847. (narrow-to-region beg end)
  2848. (while (not (= shift 0))
  2849. (org-map-region func (point-min) (point-max))
  2850. (setq shift (+ delta shift)))
  2851. (goto-char (point-min))
  2852. (message "Pasted at level %d, with shift by %d levels"
  2853. new-level shift1)))
  2854. (if (and (eq org-subtree-clip (current-kill 0))
  2855. org-subtree-clip-folded)
  2856. ;; The tree was folded before it was killed/copied
  2857. (hide-subtree))))
  2858. (defun org-kill-is-subtree-p (&optional txt)
  2859. "Check if the current kill is an outline subtree, or a set of trees.
  2860. Returns nil if kill does not start with a headline, or if the first
  2861. headline level is not the largest headline level in the tree.
  2862. So this will actually accept several entries of equal levels as well,
  2863. which is OK for `org-paste-subtree'.
  2864. If optional TXT is given, check this string instead of the current kill."
  2865. (let* ((kill (or txt (current-kill 0) ""))
  2866. (start-level (and (string-match (concat "\\`" outline-regexp) kill)
  2867. (- (match-end 0) (match-beginning 0))))
  2868. (re (concat "^" outline-regexp))
  2869. (start 1))
  2870. (if (not start-level)
  2871. nil ;; does not even start with a heading
  2872. (catch 'exit
  2873. (while (setq start (string-match re kill (1+ start)))
  2874. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  2875. (throw 'exit nil)))
  2876. t))))
  2877. ;;; Plain list items
  2878. (defun org-at-item-p ()
  2879. "Is point in a line starting a hand-formatted item?"
  2880. (let ((llt org-plain-list-ordered-item-terminator))
  2881. (save-excursion
  2882. (goto-char (point-at-bol))
  2883. (looking-at
  2884. (cond
  2885. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  2886. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  2887. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  2888. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  2889. (defun org-get-indentation ()
  2890. "Get the indentation of the current line, interpreting tabs."
  2891. (save-excursion
  2892. (beginning-of-line 1)
  2893. (skip-chars-forward " \t")
  2894. (current-column)))
  2895. (defun org-beginning-of-item ()
  2896. "Go to the beginning of the current hand-formatted item.
  2897. If the cursor is not in an item, throw an error."
  2898. (let ((pos (point))
  2899. (limit (save-excursion (org-back-to-heading)
  2900. (beginning-of-line 2) (point)))
  2901. ind ind1)
  2902. (if (org-at-item-p)
  2903. (beginning-of-line 1)
  2904. (beginning-of-line 1)
  2905. (skip-chars-forward " \t")
  2906. (setq ind (current-column))
  2907. (if (catch 'exit
  2908. (while t
  2909. (beginning-of-line 0)
  2910. (if (< (point) limit) (throw 'exit nil))
  2911. (unless (looking-at " \t]*$")
  2912. (skip-chars-forward " \t")
  2913. (setq ind1 (current-column))
  2914. (if (< ind1 ind)
  2915. (throw 'exit (org-at-item-p))))))
  2916. nil
  2917. (goto-char pos)
  2918. (error "Not in an item")))))
  2919. (defun org-end-of-item ()
  2920. "Go to the end of the current hand-formatted item.
  2921. If the cursor is not in an item, throw an error."
  2922. (let ((pos (point))
  2923. (limit (save-excursion (outline-next-heading) (point)))
  2924. (ind (save-excursion
  2925. (org-beginning-of-item)
  2926. (skip-chars-forward " \t")
  2927. (current-column)))
  2928. ind1)
  2929. (if (catch 'exit
  2930. (while t
  2931. (beginning-of-line 2)
  2932. (if (>= (point) limit) (throw 'exit t))
  2933. (unless (looking-at "[ \t]*$")
  2934. (skip-chars-forward " \t")
  2935. (setq ind1 (current-column))
  2936. (if (<= ind1 ind) (throw 'exit t)))))
  2937. (beginning-of-line 1)
  2938. (goto-char pos)
  2939. (error "Not in an item"))))
  2940. (defun org-move-item-down (arg)
  2941. "Move the plain list item at point down, i.e. swap with following item.
  2942. Subitems (items with larger indentation) are considered part of the item,
  2943. so this really moves item trees."
  2944. (interactive "p")
  2945. (let (beg end ind ind1 (pos (point)) txt)
  2946. (org-beginning-of-item)
  2947. (setq beg (point))
  2948. (setq ind (org-get-indentation))
  2949. (org-end-of-item)
  2950. (setq end (point))
  2951. (setq ind1 (org-get-indentation))
  2952. (if (and (org-at-item-p) (= ind ind1))
  2953. (progn
  2954. (org-end-of-item)
  2955. (setq txt (buffer-substring beg end))
  2956. (save-excursion
  2957. (delete-region beg end))
  2958. (setq pos (point))
  2959. (insert txt)
  2960. (goto-char pos)
  2961. (org-maybe-renumber-ordered-list))
  2962. (goto-char pos)
  2963. (error "Cannot move this item further down"))))
  2964. (defun org-move-item-up (arg)
  2965. "Move the plain list item at point up, i.e. swap with previous item.
  2966. Subitems (items with larger indentation) are considered part of the item,
  2967. so this really moves item trees."
  2968. (interactive "p")
  2969. (let (beg end ind ind1 (pos (point)) txt)
  2970. (org-beginning-of-item)
  2971. (setq beg (point))
  2972. (setq ind (org-get-indentation))
  2973. (org-end-of-item)
  2974. (setq end (point))
  2975. (goto-char beg)
  2976. (catch 'exit
  2977. (while t
  2978. (beginning-of-line 0)
  2979. (if (looking-at "[ \t]*$")
  2980. nil
  2981. (if (<= (setq ind1 (org-get-indentation)) ind)
  2982. (throw 'exit t)))))
  2983. (condition-case nil
  2984. (org-beginning-of-item)
  2985. (error (goto-char beg)
  2986. (error "Cannot move this item further up")))
  2987. (setq ind1 (org-get-indentation))
  2988. (if (and (org-at-item-p) (= ind ind1))
  2989. (progn
  2990. (setq txt (buffer-substring beg end))
  2991. (save-excursion
  2992. (delete-region beg end))
  2993. (setq pos (point))
  2994. (insert txt)
  2995. (goto-char pos)
  2996. (org-maybe-renumber-ordered-list))
  2997. (goto-char pos)
  2998. (error "Cannot move this item further up"))))
  2999. (defun org-maybe-renumber-ordered-list ()
  3000. "Renumber the ordered list at point if setup allows it.
  3001. This tests the user option `org-auto-renumber-ordered-lists' before
  3002. doing the renumbering."
  3003. (and org-auto-renumber-ordered-lists
  3004. (org-at-item-p)
  3005. (match-beginning 3)
  3006. (org-renumber-ordered-list 1)))
  3007. (defun org-get-string-indentation (s)
  3008. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3009. (let ((n -1) (i 0) (w tab-width) c)
  3010. (catch 'exit
  3011. (while (< (setq n (1+ n)) (length s))
  3012. (setq c (aref s n))
  3013. (cond ((= c ?\ ) (setq i (1+ i)))
  3014. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3015. (t (throw 'exit t)))))
  3016. i))
  3017. (defun org-renumber-ordered-list (arg)
  3018. "Renumber an ordered plain list.
  3019. Cursor next to be in the first line of an item, the line that starts
  3020. with something like \"1.\" or \"2)\"."
  3021. (interactive "p")
  3022. (unless (and (org-at-item-p)
  3023. (match-beginning 3))
  3024. (error "This is not an ordered list"))
  3025. (let ((line (org-current-line))
  3026. (col (current-column))
  3027. (ind (org-get-string-indentation
  3028. (buffer-substring (point-at-bol) (match-beginning 3))))
  3029. ;; (term (substring (match-string 3) -1))
  3030. ind1 (n (1- arg)))
  3031. ;; find where this list begins
  3032. (catch 'exit
  3033. (while t
  3034. (catch 'next
  3035. (beginning-of-line 0)
  3036. (if (looking-at "[ \t]*$") (throw 'next t))
  3037. (skip-chars-forward " \t") (setq ind1 (current-column))
  3038. (if (or (< ind1 ind)
  3039. (and (= ind1 ind)
  3040. (not (org-at-item-p))))
  3041. (throw 'exit t)))))
  3042. ;; Walk forward and replace these numbers
  3043. (catch 'exit
  3044. (while t
  3045. (catch 'next
  3046. (beginning-of-line 2)
  3047. (if (eobp) (throw 'exit nil))
  3048. (if (looking-at "[ \t]*$") (throw 'next nil))
  3049. (skip-chars-forward " \t") (setq ind1 (current-column))
  3050. (if (> ind1 ind) (throw 'next t))
  3051. (if (< ind1 ind) (throw 'exit t))
  3052. (if (not (org-at-item-p)) (throw 'exit nil))
  3053. (if (not (match-beginning 3))
  3054. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3055. (delete-region (match-beginning 3) (1- (match-end 3)))
  3056. (goto-char (match-beginning 3))
  3057. (insert (format "%d" (setq n (1+ n)))))))
  3058. (goto-line line)
  3059. (move-to-column col)))
  3060. (defvar org-last-indent-begin-marker (make-marker))
  3061. (defvar org-last-indent-end-marker (make-marker))
  3062. (defun org-outdent-item (arg)
  3063. "Outdent a local list item."
  3064. (interactive "p")
  3065. (org-indent-item (- arg)))
  3066. (defun org-indent-item (arg)
  3067. "Indent a local list item."
  3068. (interactive "p")
  3069. (unless (org-at-item-p)
  3070. (error "Not on an item"))
  3071. (let (beg end ind ind1)
  3072. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  3073. (setq beg org-last-indent-begin-marker
  3074. end org-last-indent-end-marker)
  3075. (org-beginning-of-item)
  3076. (setq beg (move-marker org-last-indent-begin-marker (point)))
  3077. (org-end-of-item)
  3078. (setq end (move-marker org-last-indent-end-marker (point))))
  3079. (goto-char beg)
  3080. (skip-chars-forward " \t") (setq ind (current-column))
  3081. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  3082. (while (< (point) end)
  3083. (beginning-of-line 1)
  3084. (skip-chars-forward " \t") (setq ind1 (current-column))
  3085. (delete-region (point-at-bol) (point))
  3086. (indent-to-column (+ ind1 arg))
  3087. (beginning-of-line 2))
  3088. (goto-char beg)))
  3089. ;;; Archiving
  3090. (defun org-archive-subtree ()
  3091. "Move the current subtree to the archive.
  3092. The archive can be a certain top-level heading in the current file, or in
  3093. a different file. The tree will be moved to that location, the subtree
  3094. heading be marked DONE, and the current time will be added."
  3095. (interactive)
  3096. ;; Save all relevant TODO keyword-relatex variables
  3097. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3098. (tr-org-todo-keywords org-todo-keywords)
  3099. (tr-org-todo-interpretation org-todo-interpretation)
  3100. (tr-org-done-string org-done-string)
  3101. (tr-org-todo-regexp org-todo-regexp)
  3102. (tr-org-todo-line-regexp org-todo-line-regexp)
  3103. (this-buffer (current-buffer))
  3104. file heading buffer level newfile-p)
  3105. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3106. (progn
  3107. (setq file (format (match-string 1 org-archive-location)
  3108. (file-name-nondirectory buffer-file-name))
  3109. heading (match-string 2 org-archive-location)))
  3110. (error "Invalid `org-archive-location'"))
  3111. (if (> (length file) 0)
  3112. (setq newfile-p (not (file-exists-p file))
  3113. buffer (find-file-noselect file))
  3114. (setq buffer (current-buffer)))
  3115. (unless buffer
  3116. (error "Cannot access file \"%s\"" file))
  3117. (if (and (> (length heading) 0)
  3118. (string-match "^\\*+" heading))
  3119. (setq level (match-end 0))
  3120. (setq heading nil level 0))
  3121. (save-excursion
  3122. ;; We first only copy, in case something goes wrong
  3123. ;; we need to protect this-command, to avoid kill-region sets it,
  3124. ;; which would lead to duplication of subtrees
  3125. (let (this-command) (org-copy-subtree))
  3126. (set-buffer buffer)
  3127. ;; Enforce org-mode for the archive buffer
  3128. (if (not (eq major-mode 'org-mode))
  3129. ;; Force the mode for future visits.
  3130. (let ((org-insert-mode-line-in-empty-file t))
  3131. (call-interactively 'org-mode)))
  3132. (when newfile-p
  3133. (goto-char (point-max))
  3134. (insert (format "\nArchived entries from file %s\n\n"
  3135. (buffer-file-name this-buffer))))
  3136. ;; Force the TODO keywords of the original buffer
  3137. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3138. (org-todo-keywords tr-org-todo-keywords)
  3139. (org-todo-interpretation tr-org-todo-interpretation)
  3140. (org-done-string tr-org-done-string)
  3141. (org-todo-regexp tr-org-todo-regexp)
  3142. (org-todo-line-regexp tr-org-todo-line-regexp))
  3143. (goto-char (point-min))
  3144. (if heading
  3145. (progn
  3146. (if (re-search-forward
  3147. (concat "\\(^\\|\r\\)"
  3148. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3149. nil t)
  3150. (goto-char (match-end 0))
  3151. ;; Heading not found, just insert it at the end
  3152. (goto-char (point-max))
  3153. (or (bolp) (insert "\n"))
  3154. (insert "\n" heading "\n")
  3155. (end-of-line 0))
  3156. ;; Make the heading visible, and the following as well
  3157. (let ((org-show-following-heading t)) (org-show-hierarchy-above))
  3158. (if (re-search-forward
  3159. (concat "^" (regexp-quote (make-string level ?*)) "[ \t]")
  3160. nil t)
  3161. (progn (goto-char (match-beginning 0)) (insert "\n")
  3162. (beginning-of-line 0))
  3163. (goto-char (point-max)) (insert "\n")))
  3164. (goto-char (point-max)) (insert "\n"))
  3165. ;; Paste
  3166. (org-paste-subtree (1+ level))
  3167. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3168. (if org-archive-mark-done
  3169. (org-todo (length org-todo-keywords)))
  3170. ;; Move cursor to right after the TODO keyword
  3171. (when org-archive-stamp-time
  3172. (beginning-of-line 1)
  3173. (looking-at org-todo-line-regexp)
  3174. (goto-char (or (match-end 2) (match-beginning 3)))
  3175. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3176. (org-current-time))
  3177. ")"))
  3178. ;; Save the buffer, if it is not the same buffer.
  3179. (if (not (eq this-buffer buffer)) (save-buffer))))
  3180. ;; Here we are back in the original buffer. Everything seems to have
  3181. ;; worked. So now cut the tree and finish up.
  3182. (let (this-command) (org-cut-subtree))
  3183. (if (looking-at "[ \t]*$") (kill-line))
  3184. (message "Subtree archived %s"
  3185. (if (eq this-buffer buffer)
  3186. (concat "under heading: " heading)
  3187. (concat "in file: " (abbreviate-file-name file))))))
  3188. ;;; Completion
  3189. (defun org-complete (&optional arg)
  3190. "Perform completion on word at point.
  3191. At the beginning of a headline, this completes TODO keywords as given in
  3192. `org-todo-keywords'.
  3193. If the current word is preceded by a backslash, completes the TeX symbols
  3194. that are supported for HTML support.
  3195. If the current word is preceded by \"#+\", completes special words for
  3196. setting file options.
  3197. At all other locations, this simply calls `ispell-complete-word'."
  3198. (interactive "P")
  3199. (catch 'exit
  3200. (let* ((end (point))
  3201. (beg1 (save-excursion
  3202. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3203. (skip-chars-backward "a-zA-Z_@0-9")
  3204. (point)))
  3205. (beg (save-excursion
  3206. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3207. (skip-chars-backward "a-zA-Z0-9_:$")
  3208. (point)))
  3209. (camel (equal (char-before beg) ?*))
  3210. (tag (equal (char-before beg1) ?:))
  3211. (texp (equal (char-before beg) ?\\))
  3212. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3213. beg)
  3214. "#+"))
  3215. (completion-ignore-case opt)
  3216. (type nil)
  3217. (tbl nil)
  3218. (table (cond
  3219. (opt
  3220. (setq type :opt)
  3221. (mapcar (lambda (x)
  3222. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3223. (cons (match-string 2 x) (match-string 1 x)))
  3224. (org-split-string (org-get-current-options) "\n")))
  3225. (texp
  3226. (setq type :tex)
  3227. org-html-entities)
  3228. ((string-match "\\`\\*+[ \t]*\\'"
  3229. (buffer-substring (point-at-bol) beg))
  3230. (setq type :todo)
  3231. (mapcar 'list org-todo-keywords))
  3232. (camel
  3233. (setq type :camel)
  3234. (save-excursion
  3235. (goto-char (point-min))
  3236. (while (re-search-forward org-todo-line-regexp nil t)
  3237. (push (list
  3238. (if org-file-link-context-use-camel-case
  3239. (org-make-org-heading-camel (match-string 3) t)
  3240. (org-make-org-heading-search-string
  3241. (match-string 3) t)))
  3242. tbl)))
  3243. tbl)
  3244. (tag (setq type :tag beg beg1)
  3245. (org-get-buffer-tags))
  3246. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3247. (pattern (buffer-substring-no-properties beg end))
  3248. (completion (try-completion pattern table)))
  3249. (cond ((eq completion t)
  3250. (if (equal type :opt)
  3251. (insert (substring (cdr (assoc (upcase pattern) table))
  3252. (length pattern)))))
  3253. ((null completion)
  3254. (message "Can't find completion for \"%s\"" pattern)
  3255. (ding))
  3256. ((not (string= pattern completion))
  3257. (delete-region beg end)
  3258. (if (string-match " +$" completion)
  3259. (setq completion (replace-match "" t t completion)))
  3260. (insert completion)
  3261. (if (get-buffer-window "*Completions*")
  3262. (delete-window (get-buffer-window "*Completions*")))
  3263. (if (assoc completion table)
  3264. (if (eq type :todo) (insert " ")
  3265. (if (eq type :tag) (insert ":"))))
  3266. (if (and (equal type :opt) (assoc completion table))
  3267. (message "%s" (substitute-command-keys
  3268. "Press \\[org-complete] again to insert example settings"))))
  3269. (t
  3270. (message "Making completion list...")
  3271. (let ((list (sort (all-completions pattern table) 'string<)))
  3272. (with-output-to-temp-buffer "*Completions*"
  3273. (condition-case nil
  3274. ;; Protection needed for XEmacs and emacs 21
  3275. (display-completion-list list pattern)
  3276. (error (display-completion-list list)))))
  3277. (message "Making completion list...%s" "done"))))))
  3278. ;;; Comments, TODO and DEADLINE
  3279. (defun org-toggle-comment ()
  3280. "Change the COMMENT state of an entry."
  3281. (interactive)
  3282. (save-excursion
  3283. (org-back-to-heading)
  3284. (if (looking-at (concat outline-regexp
  3285. "\\( +\\<" org-comment-string "\\>\\)"))
  3286. (replace-match "" t t nil 1)
  3287. (if (looking-at outline-regexp)
  3288. (progn
  3289. (goto-char (match-end 0))
  3290. (insert " " org-comment-string))))))
  3291. (defvar org-last-todo-state-is-todo nil
  3292. "This is non-nil when the last TODO state change led to a TODO state.
  3293. If the last change removed the TODO tag or switched to DONE, then
  3294. this is nil.")
  3295. (defun org-todo (&optional arg)
  3296. "Change the TODO state of an item.
  3297. The state of an item is given by a keyword at the start of the heading,
  3298. like
  3299. *** TODO Write paper
  3300. *** DONE Call mom
  3301. The different keywords are specified in the variable `org-todo-keywords'.
  3302. By default the available states are \"TODO\" and \"DONE\".
  3303. So for this example: when the item starts with TODO, it is changed to DONE.
  3304. When it starts with DONE, the DONE is removed. And when neither TODO nor
  3305. DONE are present, add TODO at the beginning of the heading.
  3306. With prefix arg, use completion to determine the new state. With numeric
  3307. prefix arg, switch to that state."
  3308. (interactive "P")
  3309. (save-excursion
  3310. (org-back-to-heading)
  3311. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  3312. (or (looking-at (concat " +" org-todo-regexp " *"))
  3313. (looking-at " *"))
  3314. (let* ((this (match-string 1))
  3315. (completion-ignore-case t)
  3316. (member (member this org-todo-keywords))
  3317. (tail (cdr member))
  3318. (state (cond
  3319. ((equal arg '(4))
  3320. ;; Read a state with completion
  3321. (completing-read "State: " (mapcar (lambda(x) (list x))
  3322. org-todo-keywords)
  3323. nil t))
  3324. ((eq arg 'right)
  3325. (if this
  3326. (if tail (car tail) nil)
  3327. (car org-todo-keywords)))
  3328. ((eq arg 'left)
  3329. (if (equal member org-todo-keywords)
  3330. nil
  3331. (if this
  3332. (nth (- (length org-todo-keywords) (length tail) 2)
  3333. org-todo-keywords)
  3334. org-done-string)))
  3335. (arg
  3336. ;; user requests a specific state
  3337. (nth (1- (prefix-numeric-value arg))
  3338. org-todo-keywords))
  3339. ((null member) (car org-todo-keywords))
  3340. ((null tail) nil) ;; -> first entry
  3341. ((eq org-todo-interpretation 'sequence)
  3342. (car tail))
  3343. ((memq org-todo-interpretation '(type priority))
  3344. (if (eq this-command last-command)
  3345. (car tail)
  3346. (if (> (length tail) 0) org-done-string nil)))
  3347. (t nil)))
  3348. (next (if state (concat " " state " ") " ")))
  3349. (replace-match next t t)
  3350. (setq org-last-todo-state-is-todo
  3351. (not (equal state org-done-string)))
  3352. (when org-log-done
  3353. (if (equal state org-done-string)
  3354. (org-log-done)
  3355. (if (not this)
  3356. (org-log-done t))))
  3357. ;; Fixup tag positioning
  3358. (and org-auto-align-tags (org-set-tags nil t))
  3359. (run-hooks 'org-after-todo-state-change-hook)))
  3360. ;; Fixup cursor location if close to the keyword
  3361. (if (and (outline-on-heading-p)
  3362. (not (bolp))
  3363. (save-excursion (beginning-of-line 1)
  3364. (looking-at org-todo-line-regexp))
  3365. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  3366. (progn
  3367. (goto-char (or (match-end 2) (match-end 1)))
  3368. (just-one-space))))
  3369. (defun org-log-done (&optional undone)
  3370. "Add a time stamp logging that a TODO entry has been closed.
  3371. When UNDONE is non-nil, remove such a time stamp again."
  3372. (interactive)
  3373. (let (beg end col)
  3374. (save-excursion
  3375. (org-back-to-heading t)
  3376. (setq beg (point))
  3377. (looking-at (concat outline-regexp " *"))
  3378. (goto-char (match-end 0))
  3379. (setq col (current-column))
  3380. (outline-next-heading)
  3381. (setq end (point))
  3382. (goto-char beg)
  3383. (when (re-search-forward (concat
  3384. "[\r\n]\\([ \t]*"
  3385. (regexp-quote org-closed-string)
  3386. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  3387. (delete-region (match-beginning 1) (match-end 1)))
  3388. (unless undone
  3389. (org-back-to-heading t)
  3390. (skip-chars-forward "^\n\r")
  3391. (goto-char (min (1+ (point)) (point-max)))
  3392. (when (not (member (char-before) '(?\r ?\n)))
  3393. (insert "\n"))
  3394. (indent-to col)
  3395. (insert org-closed-string " "
  3396. (format-time-string
  3397. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  3398. (org-current-time))
  3399. "\n")))))
  3400. (defun org-show-todo-tree (arg)
  3401. "Make a compact tree which shows all headlines marked with TODO.
  3402. The tree will show the lines where the regexp matches, and all higher
  3403. headlines above the match.
  3404. With \\[universal-argument] prefix, also show the DONE entries.
  3405. With a numeric prefix N, construct a sparse tree for the Nth element
  3406. of `org-todo-keywords'."
  3407. (interactive "P")
  3408. (let ((case-fold-search nil)
  3409. (kwd-re
  3410. (cond ((null arg) org-not-done-regexp)
  3411. ((equal arg '(4)) org-todo-regexp)
  3412. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  3413. (regexp-quote (nth (1- (prefix-numeric-value arg))
  3414. org-todo-keywords)))
  3415. (t (error "Invalid prefix argument: %s" arg)))))
  3416. (message "%d TODO entries found"
  3417. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  3418. (defun org-deadline ()
  3419. "Insert the DEADLINE: string to make a deadline.
  3420. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3421. to modify it to the correct date."
  3422. (interactive)
  3423. (insert
  3424. org-deadline-string " "
  3425. (format-time-string (car org-time-stamp-formats)
  3426. (org-read-date nil 'to-time)))
  3427. (message "%s" (substitute-command-keys
  3428. "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
  3429. (defun org-schedule ()
  3430. "Insert the SCHEDULED: string to schedule a TODO item.
  3431. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  3432. to modify it to the correct date."
  3433. (interactive)
  3434. (insert
  3435. org-scheduled-string " "
  3436. (format-time-string (car org-time-stamp-formats)
  3437. (org-read-date nil 'to-time)))
  3438. (message "%s" (substitute-command-keys
  3439. "Use \\[org-timestamp-up-day] and \\[org-timestamp-down-day] to change the date.")))
  3440. (defun org-occur (regexp &optional callback)
  3441. "Make a compact tree which shows all matches of REGEXP.
  3442. The tree will show the lines where the regexp matches, and all higher
  3443. headlines above the match. It will also show the heading after the match,
  3444. to make sure editing the matching entry is easy.
  3445. If CALLBACK is non-nil, it is a function which is called to confirm
  3446. that the match should indeed be shown."
  3447. (interactive "sRegexp: ")
  3448. (org-remove-occur-highlights nil nil t)
  3449. (setq regexp (org-check-occur-regexp regexp))
  3450. (let ((cnt 0))
  3451. (save-excursion
  3452. (goto-char (point-min))
  3453. (hide-sublevels 1)
  3454. (while (re-search-forward regexp nil t)
  3455. (when (or (not callback)
  3456. (save-match-data (funcall callback)))
  3457. (setq cnt (1+ cnt))
  3458. (org-highlight-new-match (match-beginning 0) (match-end 0))
  3459. (org-show-hierarchy-above))))
  3460. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  3461. nil 'local)
  3462. (run-hooks 'org-occur-hook)
  3463. (if (interactive-p)
  3464. (message "%d match(es) for regexp %s" cnt regexp))
  3465. cnt))
  3466. (defun org-show-hierarchy-above ()
  3467. "Make sure point and the headings hierarchy above is visible."
  3468. (catch 'exit
  3469. (if (org-on-heading-p t)
  3470. (org-flag-heading nil) ; only show the heading
  3471. (and (or (org-invisible-p) (org-invisible-p2))
  3472. (org-show-hidden-entry))) ; show entire entry
  3473. (save-excursion
  3474. (and org-show-following-heading
  3475. (outline-next-heading)
  3476. (org-flag-heading nil))) ; show the next heading
  3477. (when org-show-hierarchy-above
  3478. (save-excursion ; show all higher headings
  3479. (while (and (condition-case nil
  3480. (progn (org-up-heading-all 1) t)
  3481. (error nil))
  3482. (not (bobp)))
  3483. (org-flag-heading nil))))))
  3484. ;; Overlay compatibility functions
  3485. (defun org-make-overlay (beg end &optional buffer)
  3486. (if (featurep 'xemacs)
  3487. (make-extent beg end buffer)
  3488. (make-overlay beg end buffer)))
  3489. (defun org-delete-overlay (ovl)
  3490. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  3491. (defun org-detatch-overlay (ovl)
  3492. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  3493. (defun org-move-overlay (ovl beg end &optional buffer)
  3494. (if (featurep 'xemacs)
  3495. (set-extent-endpoints ovl beg end buffer)
  3496. (move-overlay ovl beg end buffer)))
  3497. (defun org-overlay-put (ovl prop value)
  3498. (if (featurep 'xemacs)
  3499. (set-extent-property ovl prop value)
  3500. (overlay-put ovl prop value)))
  3501. (defvar org-occur-highlights nil)
  3502. (defun org-highlight-new-match (beg end)
  3503. "Highlight from BEG to END and mark the highlight is an occur headline."
  3504. (let ((ov (org-make-overlay beg end)))
  3505. (org-overlay-put ov 'face 'secondary-selection)
  3506. (push ov org-occur-highlights)))
  3507. (defun org-remove-occur-highlights (&optional beg end noremove)
  3508. "Remove the occur highlights from the buffer.
  3509. BEG and END are ignored. If NOREMOVE is nil, remove this function
  3510. from the `before-change-functions' in the current buffer."
  3511. (interactive)
  3512. (mapc 'org-delete-overlay org-occur-highlights)
  3513. (setq org-occur-highlights nil)
  3514. (unless noremove
  3515. (remove-hook 'before-change-functions
  3516. 'org-remove-occur-highlights 'local)))
  3517. ;;; Priorities
  3518. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  3519. "Regular expression matching the priority indicator.")
  3520. (defvar org-remove-priority-next-time nil)
  3521. (defun org-priority-up ()
  3522. "Increase the priority of the current item."
  3523. (interactive)
  3524. (org-priority 'up))
  3525. (defun org-priority-down ()
  3526. "Decrease the priority of the current item."
  3527. (interactive)
  3528. (org-priority 'down))
  3529. (defun org-priority (&optional action)
  3530. "Change the priority of an item by ARG.
  3531. ACTION can be set, up, or down."
  3532. (interactive)
  3533. (setq action (or action 'set))
  3534. (let (current new news have remove)
  3535. (save-excursion
  3536. (org-back-to-heading)
  3537. (if (looking-at org-priority-regexp)
  3538. (setq current (string-to-char (match-string 2))
  3539. have t)
  3540. (setq current org-default-priority))
  3541. (cond
  3542. ((eq action 'set)
  3543. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  3544. (setq new (read-char-exclusive))
  3545. (cond ((equal new ?\ ) (setq remove t))
  3546. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  3547. (error "Priority must be between `%c' and `%c'"
  3548. ?A org-lowest-priority))))
  3549. ((eq action 'up)
  3550. (setq new (1- current)))
  3551. ((eq action 'down)
  3552. (setq new (1+ current)))
  3553. (t (error "Invalid action")))
  3554. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  3555. (setq news (format "%c" new))
  3556. (if have
  3557. (if remove
  3558. (replace-match "" t t nil 1)
  3559. (replace-match news t t nil 2))
  3560. (if remove
  3561. (error "No priority cookie found in line")
  3562. (looking-at org-todo-line-regexp)
  3563. (if (match-end 2)
  3564. (progn
  3565. (goto-char (match-end 2))
  3566. (insert " [#" news "]"))
  3567. (goto-char (match-beginning 3))
  3568. (insert "[#" news "] ")))))
  3569. (if remove
  3570. (message "Priority removed")
  3571. (message "Priority of current item set to %s" news))))
  3572. (defun org-get-priority (s)
  3573. "Find priority cookie and return priority."
  3574. (save-match-data
  3575. (if (not (string-match org-priority-regexp s))
  3576. (* 1000 (- org-lowest-priority org-default-priority))
  3577. (* 1000 (- org-lowest-priority
  3578. (string-to-char (match-string 2 s)))))))
  3579. ;;; Timestamps
  3580. (defvar org-last-changed-timestamp nil)
  3581. (defun org-time-stamp (arg)
  3582. "Prompt for a date/time and insert a time stamp.
  3583. If the user specifies a time like HH:MM, or if this command is called
  3584. with a prefix argument, the time stamp will contain date and time.
  3585. Otherwise, only the date will be included. All parts of a date not
  3586. specified by the user will be filled in from the current date/time.
  3587. So if you press just return without typing anything, the time stamp
  3588. will represent the current date/time. If there is already a timestamp
  3589. at the cursor, it will be modified."
  3590. (interactive "P")
  3591. (let ((fmt (if arg (cdr org-time-stamp-formats)
  3592. (car org-time-stamp-formats)))
  3593. (org-time-was-given nil)
  3594. time)
  3595. (cond
  3596. ((and (org-at-timestamp-p)
  3597. (eq last-command 'org-time-stamp)
  3598. (eq this-command 'org-time-stamp))
  3599. (insert "--")
  3600. (setq time (let ((this-command this-command))
  3601. (org-read-date arg 'totime)))
  3602. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3603. (insert (format-time-string fmt time)))
  3604. ((org-at-timestamp-p)
  3605. (setq time (let ((this-command this-command))
  3606. (org-read-date arg 'totime)))
  3607. (and (org-at-timestamp-p) (replace-match
  3608. (setq org-last-changed-timestamp
  3609. (format-time-string fmt time))
  3610. t t))
  3611. (message "Timestamp updated"))
  3612. (t
  3613. (setq time (let ((this-command this-command))
  3614. (org-read-date arg 'totime)))
  3615. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3616. (insert (format-time-string fmt time))))))
  3617. (defun org-time-stamp-inactive (&optional arg)
  3618. "Insert an inactive time stamp.
  3619. An inactive time stamp is enclosed in square brackets instead of angle
  3620. brackets. It is inactive in the sense that it does not trigger agenda entries,
  3621. does not link to the calendar and cannot be changed with the S-cursor keys.
  3622. So these are more for recording a certain time/date."
  3623. ;; FIXME: Would it be better not to ask for a date/time here?
  3624. (interactive "P")
  3625. (let ((fmt (if arg (cdr org-time-stamp-formats)
  3626. (car org-time-stamp-formats)))
  3627. (org-time-was-given nil)
  3628. time)
  3629. (setq time (org-read-date arg 'totime))
  3630. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  3631. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  3632. (insert (format-time-string fmt time))))
  3633. (defvar org-date-ovl (org-make-overlay 1 1))
  3634. (org-overlay-put org-date-ovl 'face 'org-warning)
  3635. (org-detatch-overlay org-date-ovl)
  3636. ;;; FIXME: Make the function take "Fri" as "next friday"
  3637. ;;; because these are mostly being used to record the current time.
  3638. (defun org-read-date (&optional with-time to-time)
  3639. "Read a date and make things smooth for the user.
  3640. The prompt will suggest to enter an ISO date, but you can also enter anything
  3641. which will at least partially be understood by `parse-time-string'.
  3642. Unrecognized parts of the date will default to the current day, month, year,
  3643. hour and minute. For example,
  3644. 3-2-5 --> 2003-02-05
  3645. feb 15 --> currentyear-02-15
  3646. sep 12 9 --> 2009-09-12
  3647. 12:45 --> today 12:45
  3648. 22 sept 0:34 --> currentyear-09-22 0:34
  3649. 12 --> currentyear-currentmonth-12
  3650. etc.
  3651. The function understands only English month and weekday abbreviations,
  3652. but this can be configured with the variables `parse-time-months' and
  3653. `parse-time-weekdays'.
  3654. While prompting, a calendar is popped up - you can also select the
  3655. date with the mouse (button 1). The calendar shows a period of three
  3656. months. To scroll it to other months, use the keys `>' and `<'.
  3657. If you don't like the calendar, turn it off with
  3658. \(setq org-popup-calendar-for-date-prompt nil)
  3659. With optional argument TO-TIME, the date will immediately be converted
  3660. to an internal time.
  3661. With an optional argument WITH-TIME, the prompt will suggest to also
  3662. insert a time. Note that when WITH-TIME is not set, you can still
  3663. enter a time, and this function will inform the calling routine about
  3664. this change. The calling routine may then choose to change the format
  3665. used to insert the time stamp into the buffer to include the time."
  3666. (require 'parse-time)
  3667. (let* ((org-time-stamp-rounding-minutes
  3668. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  3669. (ct (org-current-time))
  3670. (default-time
  3671. ;; Default time is either today, or, when entering a range,
  3672. ;; the range start.
  3673. (if (save-excursion
  3674. (re-search-backward
  3675. (concat org-ts-regexp "--\\=")
  3676. (- (point) 20) t))
  3677. (apply
  3678. 'encode-time
  3679. (mapcar (lambda(x) (or x 0)) ;; FIXME: Problem with timezone?
  3680. (parse-time-string (match-string 1))))
  3681. ct))
  3682. (calendar-move-hook nil)
  3683. (view-diary-entries-initially nil)
  3684. (timestr (format-time-string
  3685. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  3686. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  3687. ans ans1 ans2
  3688. second minute hour day month year tl)
  3689. (if org-popup-calendar-for-date-prompt
  3690. ;; Also show a calendar for date selection
  3691. ;; Copied (with modifications) from planner.el by John Wiegley
  3692. (save-excursion
  3693. (save-window-excursion
  3694. (calendar)
  3695. (calendar-forward-day (- (time-to-days default-time)
  3696. (calendar-absolute-from-gregorian
  3697. (calendar-current-date))))
  3698. (org-eval-in-calendar nil)
  3699. (let* ((old-map (current-local-map))
  3700. (map (copy-keymap calendar-mode-map))
  3701. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  3702. (define-key map (kbd "RET") 'org-calendar-select)
  3703. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  3704. 'org-calendar-select-mouse)
  3705. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  3706. 'org-calendar-select-mouse)
  3707. (define-key minibuffer-local-map [(meta shift left)]
  3708. (lambda () (interactive)
  3709. (org-eval-in-calendar '(calendar-backward-month 1))))
  3710. (define-key minibuffer-local-map [(meta shift right)]
  3711. (lambda () (interactive)
  3712. (org-eval-in-calendar '(calendar-forward-month 1))))
  3713. (define-key minibuffer-local-map [(shift up)]
  3714. (lambda () (interactive)
  3715. (org-eval-in-calendar '(calendar-backward-week 1))))
  3716. (define-key minibuffer-local-map [(shift down)]
  3717. (lambda () (interactive)
  3718. (org-eval-in-calendar '(calendar-forward-week 1))))
  3719. (define-key minibuffer-local-map [(shift left)]
  3720. (lambda () (interactive)
  3721. (org-eval-in-calendar '(calendar-backward-day 1))))
  3722. (define-key minibuffer-local-map [(shift right)]
  3723. (lambda () (interactive)
  3724. (org-eval-in-calendar '(calendar-forward-day 1))))
  3725. (define-key minibuffer-local-map ">"
  3726. (lambda () (interactive)
  3727. (org-eval-in-calendar '(scroll-calendar-left 1))))
  3728. (define-key minibuffer-local-map "<"
  3729. (lambda () (interactive)
  3730. (org-eval-in-calendar '(scroll-calendar-right 1))))
  3731. (unwind-protect
  3732. (progn
  3733. (use-local-map map)
  3734. (setq ans (read-string prompt "" nil nil))
  3735. (if (not (string-match "\\S-" ans)) (setq ans nil))
  3736. (setq ans (or ans1 ans ans2)))
  3737. (use-local-map old-map)))))
  3738. ;; Naked prompt only
  3739. (setq ans (read-string prompt "" nil timestr)))
  3740. (org-detatch-overlay org-date-ovl)
  3741. (if (string-match
  3742. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  3743. (progn
  3744. (setq year (if (match-end 2)
  3745. (string-to-number (match-string 2 ans))
  3746. (string-to-number (format-time-string "%Y")))
  3747. month (string-to-number (match-string 3 ans))
  3748. day (string-to-number (match-string 4 ans)))
  3749. (if (< year 100) (setq year (+ 2000 year)))
  3750. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  3751. t nil ans))))
  3752. (setq tl (parse-time-string ans)
  3753. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  3754. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  3755. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  3756. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  3757. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  3758. second (or (nth 0 tl) 0))
  3759. (if (and (boundp 'org-time-was-given)
  3760. (nth 2 tl))
  3761. (setq org-time-was-given t))
  3762. (if (< year 100) (setq year (+ 2000 year)))
  3763. (if to-time
  3764. (encode-time second minute hour day month year)
  3765. (if (or (nth 1 tl) (nth 2 tl))
  3766. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  3767. (format "%04d-%02d-%02d" year month day)))))
  3768. (defun org-eval-in-calendar (form)
  3769. "Eval FORM in the calendar window and return to current window.
  3770. Also, store the cursor date in variable ans2."
  3771. (let ((sw (selected-window)))
  3772. (select-window (get-buffer-window "*Calendar*"))
  3773. (eval form)
  3774. (when (calendar-cursor-to-date)
  3775. (let* ((date (calendar-cursor-to-date))
  3776. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3777. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  3778. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  3779. (select-window sw)))
  3780. (defun org-calendar-select ()
  3781. "Return to `org-read-date' with the date currently selected.
  3782. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  3783. (interactive)
  3784. (when (calendar-cursor-to-date)
  3785. (let* ((date (calendar-cursor-to-date))
  3786. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3787. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  3788. (if (active-minibuffer-window) (exit-minibuffer))))
  3789. (defun org-calendar-select-mouse (ev)
  3790. "Return to `org-read-date' with the date currently selected.
  3791. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  3792. (interactive "e")
  3793. (mouse-set-point ev)
  3794. (when (calendar-cursor-to-date)
  3795. (let* ((date (calendar-cursor-to-date))
  3796. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  3797. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  3798. (if (active-minibuffer-window) (exit-minibuffer))))
  3799. (defun org-check-deadlines (ndays)
  3800. "Check if there are any deadlines due or past due.
  3801. A deadline is considered due if it happens within `org-deadline-warning-days'
  3802. days from today's date. If the deadline appears in an entry marked DONE,
  3803. it is not shown. The prefix arg NDAYS can be used to test that many
  3804. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  3805. (interactive "P")
  3806. (let* ((org-warn-days
  3807. (cond
  3808. ((equal ndays '(4)) 100000)
  3809. (ndays (prefix-numeric-value ndays))
  3810. (t org-deadline-warning-days)))
  3811. (case-fold-search nil)
  3812. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  3813. (callback
  3814. (lambda ()
  3815. (and (let ((d1 (time-to-days (current-time)))
  3816. (d2 (time-to-days
  3817. (org-time-string-to-time (match-string 1)))))
  3818. (< (- d2 d1) org-warn-days))
  3819. (not (org-entry-is-done-p))))))
  3820. (message "%d deadlines past-due or due within %d days"
  3821. (org-occur regexp callback)
  3822. org-warn-days)))
  3823. (defun org-evaluate-time-range (&optional to-buffer)
  3824. "Evaluate a time range by computing the difference between start and end.
  3825. Normally the result is just printed in the echo area, but with prefix arg
  3826. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  3827. If the time range is actually in a table, the result is inserted into the
  3828. next column.
  3829. For time difference computation, a year is assumed to be exactly 365
  3830. days in order to avoid rounding problems."
  3831. (interactive "P")
  3832. (save-excursion
  3833. (unless (org-at-date-range-p)
  3834. (goto-char (point-at-bol))
  3835. (re-search-forward org-tr-regexp (point-at-eol) t))
  3836. (if (not (org-at-date-range-p))
  3837. (error "Not at a time-stamp range, and none found in current line")))
  3838. (let* ((ts1 (match-string 1))
  3839. (ts2 (match-string 2))
  3840. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  3841. (match-end (match-end 0))
  3842. (time1 (org-time-string-to-time ts1))
  3843. (time2 (org-time-string-to-time ts2))
  3844. (t1 (time-to-seconds time1))
  3845. (t2 (time-to-seconds time2))
  3846. (diff (abs (- t2 t1)))
  3847. (negative (< (- t2 t1) 0))
  3848. ;; (ys (floor (* 365 24 60 60)))
  3849. (ds (* 24 60 60))
  3850. (hs (* 60 60))
  3851. (fy "%dy %dd %02d:%02d")
  3852. (fy1 "%dy %dd")
  3853. (fd "%dd %02d:%02d")
  3854. (fd1 "%dd")
  3855. (fh "%02d:%02d")
  3856. y d h m align)
  3857. ;; FIXME: Should I re-introduce years, make year refer to same date?
  3858. ;; This would be the only useful way to have years, actually.
  3859. (if havetime
  3860. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  3861. y 0
  3862. d (floor (/ diff ds)) diff (mod diff ds)
  3863. h (floor (/ diff hs)) diff (mod diff hs)
  3864. m (floor (/ diff 60)))
  3865. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  3866. y 0
  3867. d (floor (+ (/ diff ds) 0.5))
  3868. h 0 m 0))
  3869. (if (not to-buffer)
  3870. (message (org-make-tdiff-string y d h m))
  3871. (when (org-at-table-p)
  3872. (goto-char match-end)
  3873. (setq align t)
  3874. (and (looking-at " *|") (goto-char (match-end 0))))
  3875. (if (looking-at
  3876. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  3877. (replace-match ""))
  3878. (if negative (insert " -"))
  3879. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  3880. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  3881. (insert " " (format fh h m))))
  3882. (if align (org-table-align))
  3883. (message "Time difference inserted"))))
  3884. (defun org-make-tdiff-string (y d h m)
  3885. (let ((fmt "")
  3886. (l nil))
  3887. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  3888. l (push y l)))
  3889. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  3890. l (push d l)))
  3891. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  3892. l (push h l)))
  3893. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  3894. l (push m l)))
  3895. (apply 'format fmt (nreverse l))))
  3896. (defun org-time-string-to-time (s)
  3897. (apply 'encode-time (org-parse-time-string s)))
  3898. (defun org-parse-time-string (s &optional nodefault)
  3899. "Parse the standard Org-mode time string.
  3900. This should be a lot faster than the normal `parse-time-string'.
  3901. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  3902. hour and minute fields will be nil if not given."
  3903. (if (string-match org-ts-regexp1 s)
  3904. (list 0
  3905. (if (or (match-beginning 8) (not nodefault))
  3906. (string-to-number (or (match-string 8 s) "0")))
  3907. (if (or (match-beginning 7) (not nodefault))
  3908. (string-to-number (or (match-string 7 s) "0")))
  3909. (string-to-number (match-string 4 s))
  3910. (string-to-number (match-string 3 s))
  3911. (string-to-number (match-string 2 s))
  3912. nil nil nil)
  3913. (make-list 9 0)))
  3914. (defun org-timestamp-up (&optional arg)
  3915. "Increase the date item at the cursor by one.
  3916. If the cursor is on the year, change the year. If it is on the month or
  3917. the day, change that.
  3918. With prefix ARG, change by that many units."
  3919. (interactive "p")
  3920. (org-timestamp-change (prefix-numeric-value arg)))
  3921. (defun org-timestamp-down (&optional arg)
  3922. "Decrease the date item at the cursor by one.
  3923. If the cursor is on the year, change the year. If it is on the month or
  3924. the day, change that.
  3925. With prefix ARG, change by that many units."
  3926. (interactive "p")
  3927. (org-timestamp-change (- (prefix-numeric-value arg))))
  3928. (defun org-timestamp-up-day (&optional arg)
  3929. "Increase the date in the time stamp by one day.
  3930. With prefix ARG, change that many days."
  3931. (interactive "p")
  3932. (if (and (not (org-at-timestamp-p))
  3933. (org-on-heading-p))
  3934. (org-todo 'up)
  3935. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  3936. (defun org-timestamp-down-day (&optional arg)
  3937. "Decrease the date in the time stamp by one day.
  3938. With prefix ARG, change that many days."
  3939. (interactive "p")
  3940. (if (and (not (org-at-timestamp-p))
  3941. (org-on-heading-p))
  3942. (org-todo 'down)
  3943. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  3944. (defsubst org-pos-in-match-range (pos n)
  3945. (and (match-beginning n)
  3946. (<= (match-beginning n) pos)
  3947. (>= (match-end n) pos)))
  3948. (defun org-at-timestamp-p ()
  3949. "Determine if the cursor is in or at a timestamp."
  3950. (interactive)
  3951. (let* ((tsr org-ts-regexp2)
  3952. (pos (point))
  3953. (ans (or (looking-at tsr)
  3954. (save-excursion
  3955. (skip-chars-backward "^<\n\r\t")
  3956. (if (> (point) 1) (backward-char 1))
  3957. (and (looking-at tsr)
  3958. (> (- (match-end 0) pos) -1))))))
  3959. (and (boundp 'org-ts-what)
  3960. (setq org-ts-what
  3961. (cond
  3962. ((org-pos-in-match-range pos 2) 'year)
  3963. ((org-pos-in-match-range pos 3) 'month)
  3964. ((org-pos-in-match-range pos 7) 'hour)
  3965. ((org-pos-in-match-range pos 8) 'minute)
  3966. ((or (org-pos-in-match-range pos 4)
  3967. (org-pos-in-match-range pos 5)) 'day)
  3968. (t 'day))))
  3969. ans))
  3970. (defun org-timestamp-change (n &optional what)
  3971. "Change the date in the time stamp at point.
  3972. The date will be changed by N times WHAT. WHAT can be `day', `month',
  3973. `year', `minute', `second'. If WHAT is not given, the cursor position
  3974. in the timestamp determines what will be changed."
  3975. (let ((fmt (car org-time-stamp-formats))
  3976. org-ts-what
  3977. (pos (point))
  3978. ts time time0)
  3979. (if (not (org-at-timestamp-p))
  3980. (error "Not at a timestamp"))
  3981. (setq org-ts-what (or what org-ts-what))
  3982. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  3983. (- (match-end 0) (match-beginning 0))))
  3984. 1)
  3985. (cdr org-time-stamp-formats)
  3986. (car org-time-stamp-formats)))
  3987. (setq ts (match-string 0))
  3988. (replace-match "")
  3989. (setq time0 (org-parse-time-string ts))
  3990. (setq time
  3991. (apply 'encode-time
  3992. (append
  3993. (list (or (car time0) 0))
  3994. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  3995. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  3996. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  3997. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  3998. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  3999. (nthcdr 6 time0))))
  4000. (if (eq what 'calendar)
  4001. (let ((cal-date
  4002. (save-excursion
  4003. (save-match-data
  4004. (set-buffer "*Calendar*")
  4005. (calendar-cursor-to-date)))))
  4006. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  4007. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  4008. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  4009. (setcar time0 (or (car time0) 0))
  4010. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  4011. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  4012. (setq time (apply 'encode-time time0))))
  4013. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  4014. (goto-char pos)
  4015. ;; Try to recenter the calendar window, if any
  4016. (if (and org-calendar-follow-timestamp-change
  4017. (get-buffer-window "*Calendar*" t)
  4018. (memq org-ts-what '(day month year)))
  4019. (org-recenter-calendar (time-to-days time)))))
  4020. (defun org-recenter-calendar (date)
  4021. "If the calendar is visible, recenter it to DATE."
  4022. (let* ((win (selected-window))
  4023. (cwin (get-buffer-window "*Calendar*" t))
  4024. (calendar-move-hook nil))
  4025. (when cwin
  4026. (select-window cwin)
  4027. (calendar-goto-date (if (listp date) date
  4028. (calendar-gregorian-from-absolute date)))
  4029. (select-window win))))
  4030. (defun org-goto-calendar (&optional arg)
  4031. "Go to the Emacs calendar at the current date.
  4032. If there is a time stamp in the current line, go to that date.
  4033. A prefix ARG can be used to force the current date."
  4034. (interactive "P")
  4035. (let ((tsr org-ts-regexp) diff
  4036. (calendar-move-hook nil)
  4037. (view-diary-entries-initially nil))
  4038. (if (or (org-at-timestamp-p)
  4039. (save-excursion
  4040. (beginning-of-line 1)
  4041. (looking-at (concat ".*" tsr))))
  4042. (let ((d1 (time-to-days (current-time)))
  4043. (d2 (time-to-days
  4044. (org-time-string-to-time (match-string 1)))))
  4045. (setq diff (- d2 d1))))
  4046. (calendar)
  4047. (calendar-goto-today)
  4048. (if (and diff (not arg)) (calendar-forward-day diff))))
  4049. (defun org-date-from-calendar ()
  4050. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  4051. If there is already a time stamp at the cursor position, update it."
  4052. (interactive)
  4053. (org-timestamp-change 0 'calendar))
  4054. ;;; Agenda, and Diary Integration
  4055. ;;; Define the mode
  4056. (defvar org-agenda-mode-map (make-sparse-keymap)
  4057. "Keymap for `org-agenda-mode'.")
  4058. (defvar org-agenda-menu) ; defined later in this file.
  4059. (defvar org-agenda-follow-mode nil)
  4060. (defvar org-agenda-show-log nil)
  4061. (defvar org-agenda-buffer-name "*Org Agenda*")
  4062. (defvar org-agenda-redo-command nil)
  4063. (defvar org-agenda-mode-hook nil)
  4064. (defvar org-agenda-type nil)
  4065. (defvar org-agenda-force-single-file nil)
  4066. ;;;###autoload
  4067. (defun org-agenda-mode ()
  4068. "Mode for time-sorted view on action items in Org-mode files.
  4069. The following commands are available:
  4070. \\{org-agenda-mode-map}"
  4071. (interactive)
  4072. (kill-all-local-variables)
  4073. (setq major-mode 'org-agenda-mode)
  4074. (setq mode-name "Org-Agenda")
  4075. (use-local-map org-agenda-mode-map)
  4076. (easy-menu-add org-agenda-menu)
  4077. (if org-startup-truncated (setq truncate-lines t))
  4078. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  4079. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  4080. (unless org-agenda-keep-modes
  4081. (setq org-agenda-follow-mode nil
  4082. org-agenda-show-log nil))
  4083. (easy-menu-change
  4084. '("Agenda") "Agenda Files"
  4085. (append
  4086. (list
  4087. (vector
  4088. (if (get 'org-agenda-files 'org-restrict)
  4089. "Restricted to single file"
  4090. "Edit File List")
  4091. '(org-edit-agenda-file-list)
  4092. (not (get 'org-agenda-files 'org-restrict)))
  4093. "--")
  4094. (mapcar 'org-file-menu-entry (org-agenda-files))))
  4095. (org-agenda-set-mode-name)
  4096. (apply
  4097. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  4098. (list 'org-agenda-mode-hook)))
  4099. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  4100. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  4101. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  4102. (define-key org-agenda-mode-map " " 'org-agenda-show)
  4103. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  4104. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  4105. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  4106. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  4107. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  4108. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  4109. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  4110. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  4111. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  4112. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  4113. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  4114. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  4115. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  4116. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4117. (while l (define-key org-agenda-mode-map
  4118. (int-to-string (pop l)) 'digit-argument)))
  4119. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  4120. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  4121. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  4122. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  4123. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  4124. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  4125. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  4126. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  4127. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  4128. (define-key org-agenda-mode-map "n" 'next-line)
  4129. (define-key org-agenda-mode-map "p" 'previous-line)
  4130. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  4131. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  4132. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  4133. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  4134. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  4135. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  4136. (eval-after-load "calendar"
  4137. '(define-key calendar-mode-map org-calendar-to-agenda-key
  4138. 'org-calendar-goto-agenda))
  4139. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  4140. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  4141. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  4142. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  4143. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  4144. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  4145. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  4146. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  4147. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  4148. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  4149. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  4150. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  4151. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  4152. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  4153. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  4154. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  4155. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  4156. "Local keymap for agenda entries from Org-mode.")
  4157. (define-key org-agenda-keymap
  4158. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  4159. (define-key org-agenda-keymap
  4160. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  4161. (define-key org-agenda-keymap [follow-link] 'mouse-face)
  4162. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  4163. '("Agenda"
  4164. ("Agenda Files")
  4165. "--"
  4166. ["Show" org-agenda-show t]
  4167. ["Go To (other window)" org-agenda-goto t]
  4168. ["Go To (one window)" org-agenda-switch-to t]
  4169. ["Follow Mode" org-agenda-follow-mode
  4170. :style toggle :selected org-agenda-follow-mode :active t]
  4171. "--"
  4172. ["Cycle TODO" org-agenda-todo t]
  4173. ("Tags"
  4174. ["Show all Tags" org-agenda-show-tags t]
  4175. ["Set Tags" org-agenda-set-tags t])
  4176. ("Reschedule"
  4177. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  4178. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  4179. "--"
  4180. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  4181. ("Priority"
  4182. ["Set Priority" org-agenda-priority t]
  4183. ["Increase Priority" org-agenda-priority-up t]
  4184. ["Decrease Priority" org-agenda-priority-down t]
  4185. ["Show Priority" org-agenda-show-priority t])
  4186. "--"
  4187. ;; ["New agenda command" org-agenda t]
  4188. ["Rebuild buffer" org-agenda-redo t]
  4189. "--"
  4190. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  4191. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  4192. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  4193. "--"
  4194. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  4195. :style radio :selected (equal org-agenda-ndays 1)]
  4196. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  4197. :style radio :selected (equal org-agenda-ndays 7)]
  4198. "--"
  4199. ["Show Logbook entries" org-agenda-log-mode
  4200. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  4201. ["Include Diary" org-agenda-toggle-diary
  4202. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  4203. ["Use Time Grid" org-agenda-toggle-time-grid
  4204. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  4205. "--"
  4206. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  4207. ("Calendar Commands"
  4208. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  4209. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  4210. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  4211. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  4212. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  4213. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  4214. "--"
  4215. ["Quit" org-agenda-quit t]
  4216. ["Exit and Release Buffers" org-agenda-exit t]
  4217. ))
  4218. ;;;###autoload
  4219. (defun org-agenda (arg)
  4220. "Dispatch agenda commands to collect entries to the agenda buffer.
  4221. Prompts for a character to select a command. Any prefix arg will be passed
  4222. on to the selected command. The default selections are:
  4223. a Call `org-agenda' to display the agenda for the current day or week.
  4224. t Call `org-todo-list' to display the global todo list.
  4225. T Call `org-todo-list' to display the global todo list, select only
  4226. entries with a specific TODO keyword (the user gets a prompt).
  4227. m Call `org-tags-view' to display headlines with tags matching
  4228. a condition (the user is prompted for the condition).
  4229. M Like `m', but select only TODO entries, no ordinary headlines.
  4230. More commands can be added by configuring the variable
  4231. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  4232. searches can be pre-defined in this way.
  4233. If the current buffer is in Org-mode and visiting a file, you can also
  4234. first press `1' to indicate that the agenda should be temporarily (until the
  4235. next use of \\[org-agenda]) restricted to the current file."
  4236. (interactive "P")
  4237. (catch 'exit
  4238. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  4239. (custom org-agenda-custom-commands)
  4240. c entry key type string)
  4241. (put 'org-agenda-files 'org-restrict nil)
  4242. (save-window-excursion
  4243. (delete-other-windows)
  4244. (switch-to-buffer-other-window " *Agenda Commands*")
  4245. (erase-buffer)
  4246. (insert
  4247. "Press key for an agenda command:
  4248. --------------------------------
  4249. a Agenda for current week or day
  4250. t List of all TODO entries T Entries with special TODO kwd
  4251. m Match a TAGS query M Like m, but only TODO entries
  4252. C Configure your own agenda commands")
  4253. (while (setq entry (pop custom))
  4254. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  4255. (insert (format "\n%-4s%-14s: %s"
  4256. key
  4257. (cond
  4258. ((eq type 'tags) "Tags query")
  4259. ((eq type 'todo) "TODO keyword")
  4260. ((eq type 'tags-tree) "Tags tree")
  4261. ((eq type 'todo-tree) "TODO kwd tree")
  4262. ((eq type 'occur-tree) "Occur tree")
  4263. (t "???"))
  4264. (org-string-props string 'face 'org-link))))
  4265. (goto-char (point-min))
  4266. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  4267. (message "Press key for agenda command%s"
  4268. (if restrict-ok ", or [1] to restrict to current file" ""))
  4269. (setq c (read-char-exclusive))
  4270. (message "")
  4271. (when (equal c ?1)
  4272. (if restrict-ok
  4273. (put 'org-agenda-files 'org-restrict (list buffer-file-name))
  4274. (error "Cannot restrict agenda to current buffer"))
  4275. (message "Press key for agenda command%s"
  4276. (if restrict-ok " (restricted to current file)" ""))
  4277. (setq c (read-char-exclusive))
  4278. (message "")))
  4279. (require 'calendar) ; FIXME: can we avoid this for some commands?
  4280. ;; For example the todo list should not need it (but does...)
  4281. (cond
  4282. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  4283. ((equal c ?a) (call-interactively 'org-agenda-list))
  4284. ((equal c ?t) (call-interactively 'org-todo-list))
  4285. ((equal c ?T)
  4286. (setq current-prefix-arg (or arg '(4)))
  4287. (call-interactively 'org-todo-list))
  4288. ((equal c ?m) (call-interactively 'org-tags-view))
  4289. ((equal c ?M)
  4290. (setq current-prefix-arg (or arg '(4)))
  4291. (call-interactively 'org-tags-view))
  4292. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  4293. (setq type (nth 1 entry) string (nth 2 entry))
  4294. (cond
  4295. ((eq type 'tags)
  4296. (org-tags-view current-prefix-arg string))
  4297. ((eq type 'todo)
  4298. (org-todo-list string))
  4299. ((eq type 'tags-tree)
  4300. (org-check-for-org-mode)
  4301. (org-tags-sparse-tree current-prefix-arg string))
  4302. ((eq type 'todo-tree)
  4303. (org-check-for-org-mode)
  4304. (org-occur (concat "^" outline-regexp "[ \t]*"
  4305. (regexp-quote string) "\\>")))
  4306. ((eq type 'occur-tree)
  4307. (org-check-for-org-mode)
  4308. (org-occur string))
  4309. (t (error "Invalid custom agenda command type %s" type))))
  4310. (t (error "Invalid key"))))))
  4311. (defun org-check-for-org-mode ()
  4312. "Make sure current buffer is in org-mode. Error if not."
  4313. (or (eq major-mode 'org-mode)
  4314. (error "Cannot execute org-mode agenda command on buffer in %s."
  4315. major-mode)))
  4316. (defun org-fit-agenda-window ()
  4317. "Fit the window to the buffer size."
  4318. (and org-fit-agenda-window
  4319. (fboundp 'fit-window-to-buffer)
  4320. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  4321. (/ (frame-height) 2))))
  4322. (defun org-agenda-files (&optional unrestricted)
  4323. "Get the list of agenda files.
  4324. Optional UNRESTRICTED means return the full list even if a restriction
  4325. is currently in place."
  4326. (cond
  4327. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  4328. ((stringp org-agenda-files) (org-read-agenda-file-list))
  4329. ((listp org-agenda-files) org-agenda-files)
  4330. (t (error "Invalid value of `org-agenda-files'"))))
  4331. (defvar org-window-configuration)
  4332. (defun org-edit-agenda-file-list ()
  4333. "Edit the list of agenda files.
  4334. Depending on setup, this either uses customize to edit the variable
  4335. `org-agenda-files', or it visits the file that is holding the list. In the
  4336. latter case, the buffer is set up in a way that saving it automatically kills
  4337. the buffer and restores the previous window configuration."
  4338. (interactive)
  4339. (if (stringp org-agenda-files)
  4340. (let ((cw (current-window-configuration)))
  4341. (find-file org-agenda-files)
  4342. (set (make-local-variable 'org-window-configuration) cw)
  4343. (org-add-hook 'after-save-hook
  4344. (lambda ()
  4345. (set-window-configuration
  4346. (prog1 org-window-configuration
  4347. (kill-buffer (current-buffer))))
  4348. (org-install-agenda-files-menu)
  4349. (message "New agenda file list installed"))
  4350. nil 'local)
  4351. (message (substitute-command-keys
  4352. "Edit list and finish with \\[save-buffer]")))
  4353. (customize-variable 'org-agenda-files)))
  4354. (defun org-store-new-agenda-file-list (list)
  4355. "Set new value for the agenda file list and save it correcly."
  4356. (if (stringp org-agenda-files)
  4357. (let ((f org-agenda-files) b)
  4358. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  4359. (with-temp-file f
  4360. (insert (mapconcat 'identity list) "\n") "\n"))
  4361. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  4362. (setq org-agenda-files list)
  4363. (customize-save-variable 'org-agenda-files org-agenda-files))))
  4364. (defun org-read-agenda-file-list ()
  4365. "Read the list of agenda files from a file."
  4366. (when (stringp org-agenda-files)
  4367. (with-temp-buffer
  4368. (insert-file-contents org-agenda-files)
  4369. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  4370. (defvar org-agenda-markers nil
  4371. "List of all currently active markers created by `org-agenda'.")
  4372. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  4373. "Creation time of the last agenda marker.")
  4374. (defun org-agenda-new-marker (&optional pos)
  4375. "Return a new agenda marker.
  4376. Org-mode keeps a list of these markers and resets them when they are
  4377. no longer in use."
  4378. (let ((m (copy-marker (or pos (point)))))
  4379. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  4380. (push m org-agenda-markers)
  4381. m))
  4382. (defun org-agenda-maybe-reset-markers (&optional force)
  4383. "Reset markers created by `org-agenda'. But only if they are old enough."
  4384. (if (or force
  4385. (> (- (time-to-seconds (current-time))
  4386. org-agenda-last-marker-time)
  4387. 5))
  4388. (while org-agenda-markers
  4389. (move-marker (pop org-agenda-markers) nil))))
  4390. (defvar org-agenda-new-buffers nil
  4391. "Buffers created to visit agenda files.")
  4392. (defun org-get-agenda-file-buffer (file)
  4393. "Get a buffer visiting FILE. If the buffer needs to be created, add
  4394. it to the list of buffers which might be released later."
  4395. (let ((buf (find-buffer-visiting file)))
  4396. (if buf
  4397. buf ; just return it
  4398. ;; Make a new buffer and remember it
  4399. (setq buf (find-file-noselect file))
  4400. (if buf (push buf org-agenda-new-buffers))
  4401. buf)))
  4402. (defun org-release-buffers (blist)
  4403. "Release all buffers in list, asking the user for confirmation when needed.
  4404. When a buffer is unmodified, it is just killed. When modified, it is saved
  4405. \(if the user agrees) and then killed."
  4406. (let (buf file)
  4407. (while (setq buf (pop blist))
  4408. (setq file (buffer-file-name buf))
  4409. (when (and (buffer-modified-p buf)
  4410. file
  4411. (y-or-n-p (format "Save file %s? " file)))
  4412. (with-current-buffer buf (save-buffer)))
  4413. (kill-buffer buf))))
  4414. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  4415. (defun org-timeline (&optional include-all keep-modes)
  4416. "Show a time-sorted view of the entries in the current org file.
  4417. Only entries with a time stamp of today or later will be listed. With
  4418. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  4419. under the current date.
  4420. If the buffer contains an active region, only check the region for
  4421. dates."
  4422. (interactive "P")
  4423. (require 'calendar)
  4424. (org-agenda-maybe-reset-markers 'force)
  4425. (org-compile-prefix-format org-timeline-prefix-format)
  4426. (let* ((dopast t)
  4427. (dotodo include-all)
  4428. (doclosed org-agenda-show-log)
  4429. (org-agenda-keep-modes keep-modes)
  4430. (entry buffer-file-name)
  4431. (org-agenda-files (list buffer-file-name))
  4432. (date (calendar-current-date))
  4433. (win (selected-window))
  4434. (pos1 (point))
  4435. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  4436. (end (if (org-region-active-p) (region-end) (point-max)))
  4437. (day-numbers (org-get-all-dates beg end 'no-ranges
  4438. t doclosed)) ; always include today
  4439. (today (time-to-days (current-time)))
  4440. (org-respect-restriction t)
  4441. (past t)
  4442. args
  4443. s e rtn d)
  4444. (setq org-agenda-redo-command
  4445. (list 'progn
  4446. (list 'switch-to-buffer-other-window (current-buffer))
  4447. (list 'org-timeline (list 'quote include-all) t)))
  4448. (if (not dopast)
  4449. ;; Remove past dates from the list of dates.
  4450. (setq day-numbers (delq nil (mapcar (lambda(x)
  4451. (if (>= x today) x nil))
  4452. day-numbers))))
  4453. (switch-to-buffer-other-window
  4454. (get-buffer-create org-agenda-buffer-name))
  4455. (setq buffer-read-only nil)
  4456. (erase-buffer)
  4457. (org-agenda-mode) (setq buffer-read-only nil)
  4458. (set (make-local-variable 'org-agenda-type) 'timeline)
  4459. (if doclosed (push :closed args))
  4460. (push :timestamp args)
  4461. (if dotodo (push :todo args))
  4462. (while (setq d (pop day-numbers))
  4463. (if (and (>= d today)
  4464. dopast
  4465. past)
  4466. (progn
  4467. (setq past nil)
  4468. (insert (make-string 79 ?-) "\n")))
  4469. (setq date (calendar-gregorian-from-absolute d))
  4470. (setq s (point))
  4471. (setq rtn (apply 'org-agenda-get-day-entries
  4472. entry date args))
  4473. (if (or rtn (equal d today))
  4474. (progn
  4475. (insert (calendar-day-name date) " "
  4476. (number-to-string (extract-calendar-day date)) " "
  4477. (calendar-month-name (extract-calendar-month date)) " "
  4478. (number-to-string (extract-calendar-year date)) "\n")
  4479. (put-text-property s (1- (point)) 'face
  4480. 'org-link)
  4481. (if (equal d today)
  4482. (put-text-property s (1- (point)) 'org-today t))
  4483. (insert (org-finalize-agenda-entries rtn) "\n")
  4484. (put-text-property s (1- (point)) 'day d))))
  4485. (goto-char (point-min))
  4486. (setq buffer-read-only t)
  4487. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  4488. (point-min)))
  4489. (when (not org-select-timeline-window)
  4490. (select-window win)
  4491. (goto-char pos1))))
  4492. ;;;###autoload
  4493. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  4494. "Produce a weekly view from all files in variable `org-agenda-files'.
  4495. The view will be for the current week, but from the overview buffer you
  4496. will be able to go to other weeks.
  4497. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  4498. also be shown, under the current date.
  4499. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  4500. on the days are also shown. See the variable `org-log-done' for how
  4501. to turn on logging.
  4502. START-DAY defaults to TODAY, or to the most recent match for the weekday
  4503. given in `org-agenda-start-on-weekday'.
  4504. NDAYS defaults to `org-agenda-ndays'."
  4505. (interactive "P")
  4506. (org-agenda-maybe-reset-markers 'force)
  4507. (org-compile-prefix-format org-agenda-prefix-format)
  4508. (require 'calendar)
  4509. (let* ((org-agenda-start-on-weekday
  4510. (if (or (equal ndays 1)
  4511. (and (null ndays) (equal 1 org-agenda-ndays)))
  4512. nil org-agenda-start-on-weekday))
  4513. (org-agenda-keep-modes keep-modes)
  4514. (thefiles (copy-sequence (org-agenda-files))) ;; FIXME: Why copy???
  4515. (files thefiles)
  4516. (win (selected-window))
  4517. (today (time-to-days (current-time)))
  4518. (sd (or start-day today))
  4519. (start (if (or (null org-agenda-start-on-weekday)
  4520. (< org-agenda-ndays 7))
  4521. sd
  4522. (let* ((nt (calendar-day-of-week
  4523. (calendar-gregorian-from-absolute sd)))
  4524. (n1 org-agenda-start-on-weekday)
  4525. (d (- nt n1)))
  4526. (- sd (+ (if (< d 0) 7 0) d)))))
  4527. (day-numbers (list start))
  4528. (inhibit-redisplay t)
  4529. s e rtn rtnall file date d start-pos end-pos todayp nd)
  4530. (setq org-agenda-redo-command
  4531. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  4532. ;; Make the list of days
  4533. (setq ndays (or ndays org-agenda-ndays)
  4534. nd ndays)
  4535. (while (> ndays 1)
  4536. (push (1+ (car day-numbers)) day-numbers)
  4537. (setq ndays (1- ndays)))
  4538. (setq day-numbers (nreverse day-numbers))
  4539. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  4540. (progn
  4541. (delete-other-windows)
  4542. (switch-to-buffer-other-window
  4543. (get-buffer-create org-agenda-buffer-name))))
  4544. (setq buffer-read-only nil)
  4545. (erase-buffer)
  4546. (org-agenda-mode) (setq buffer-read-only nil)
  4547. (set (make-local-variable 'org-agenda-type) 'agenda)
  4548. (set (make-local-variable 'starting-day) (car day-numbers))
  4549. (set (make-local-variable 'include-all-loc) include-all)
  4550. (when (and (or include-all org-agenda-include-all-todo)
  4551. (member today day-numbers))
  4552. (setq files thefiles
  4553. rtnall nil)
  4554. (while (setq file (pop files))
  4555. (catch 'nextfile
  4556. (org-check-agenda-file file)
  4557. (setq date (calendar-gregorian-from-absolute today)
  4558. rtn (org-agenda-get-day-entries
  4559. file date :todo))
  4560. (setq rtnall (append rtnall rtn))))
  4561. (when rtnall
  4562. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  4563. (add-text-properties (point-min) (1- (point))
  4564. (list 'face 'org-link))
  4565. (insert (org-finalize-agenda-entries rtnall) "\n")))
  4566. (while (setq d (pop day-numbers))
  4567. (setq date (calendar-gregorian-from-absolute d)
  4568. s (point))
  4569. (if (or (setq todayp (= d today))
  4570. (and (not start-pos) (= d sd)))
  4571. (setq start-pos (point))
  4572. (if (and start-pos (not end-pos))
  4573. (setq end-pos (point))))
  4574. (setq files thefiles
  4575. rtnall nil)
  4576. (while (setq file (pop files))
  4577. (catch 'nextfile
  4578. (org-check-agenda-file file)
  4579. (if org-agenda-show-log
  4580. (setq rtn (org-agenda-get-day-entries
  4581. file date
  4582. :deadline :scheduled :timestamp :closed))
  4583. (setq rtn (org-agenda-get-day-entries
  4584. file date
  4585. :deadline :scheduled :timestamp)))
  4586. (setq rtnall (append rtnall rtn))))
  4587. (if org-agenda-include-diary
  4588. (progn
  4589. (require 'diary-lib)
  4590. (setq rtn (org-get-entries-from-diary date))
  4591. (setq rtnall (append rtnall rtn))))
  4592. (if (or rtnall org-agenda-show-all-dates)
  4593. (progn
  4594. (insert (format "%-9s %2d %s %4d\n"
  4595. (calendar-day-name date)
  4596. (extract-calendar-day date)
  4597. (calendar-month-name (extract-calendar-month date))
  4598. (extract-calendar-year date)))
  4599. (put-text-property s (1- (point)) 'face
  4600. 'org-link)
  4601. (if rtnall (insert
  4602. (org-finalize-agenda-entries
  4603. (org-agenda-add-time-grid-maybe
  4604. rtnall nd todayp))
  4605. "\n"))
  4606. (put-text-property s (1- (point)) 'day d))))
  4607. (goto-char (point-min))
  4608. (setq buffer-read-only t)
  4609. (org-fit-agenda-window)
  4610. (unless (and (pos-visible-in-window-p (point-min))
  4611. (pos-visible-in-window-p (point-max)))
  4612. (goto-char (1- (point-max)))
  4613. (recenter -1)
  4614. (if (not (pos-visible-in-window-p (or start-pos 1)))
  4615. (progn
  4616. (goto-char (or start-pos 1))
  4617. (recenter 1))))
  4618. (goto-char (or start-pos 1))
  4619. (if (not org-select-agenda-window) (select-window win))
  4620. (message "")))
  4621. (defvar org-select-this-todo-keyword nil)
  4622. ;;;###autoload
  4623. (defun org-todo-list (arg &optional keep-modes)
  4624. "Show all TODO entries from all agenda file in a single list.
  4625. The prefix arg can be used to select a specific TODO keyword and limit
  4626. the list to these. When using \\[universal-argument], you will be prompted
  4627. for a keyword. A numeric prefix directly selects the Nth keyword in
  4628. `org-todo-keywords'."
  4629. (interactive "P")
  4630. (org-agenda-maybe-reset-markers 'force)
  4631. (org-compile-prefix-format org-agenda-prefix-format)
  4632. (let* ((org-agenda-keep-modes keep-modes)
  4633. (today (time-to-days (current-time)))
  4634. (date (calendar-gregorian-from-absolute today))
  4635. (win (selected-window))
  4636. (kwds org-todo-keywords)
  4637. (completion-ignore-case t)
  4638. (org-select-this-todo-keyword
  4639. (if (stringp arg) arg
  4640. (and arg (integerp arg) (nth (1- arg) org-todo-keywords))))
  4641. rtn rtnall files file pos)
  4642. (when (equal arg '(4))
  4643. (setq org-select-this-todo-keyword
  4644. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  4645. nil t)))
  4646. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  4647. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  4648. (progn
  4649. (delete-other-windows)
  4650. (switch-to-buffer-other-window
  4651. (get-buffer-create org-agenda-buffer-name))))
  4652. (setq buffer-read-only nil)
  4653. (erase-buffer)
  4654. (org-agenda-mode) (setq buffer-read-only nil)
  4655. (set (make-local-variable 'org-agenda-type) 'todo)
  4656. (set (make-local-variable 'last-arg) arg)
  4657. (set (make-local-variable 'org-todo-keywords) kwds)
  4658. (set (make-local-variable 'org-agenda-redo-command)
  4659. '(org-todo-list (or current-prefix-arg last-arg) t))
  4660. (setq files (org-agenda-files)
  4661. rtnall nil)
  4662. (while (setq file (pop files))
  4663. (catch 'nextfile
  4664. (org-check-agenda-file file)
  4665. (setq rtn (org-agenda-get-day-entries file date :todo))
  4666. (setq rtnall (append rtnall rtn))))
  4667. (insert "Global list of TODO items of type: ")
  4668. (add-text-properties (point-min) (1- (point))
  4669. (list 'face 'org-link))
  4670. (setq pos (point))
  4671. (insert (or org-select-this-todo-keyword "ALL") "\n")
  4672. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  4673. (setq pos (point))
  4674. (insert
  4675. "Available with `N r': (0)ALL "
  4676. (let ((n 0))
  4677. (mapconcat (lambda (x)
  4678. (format "(%d)%s" (setq n (1+ n)) x))
  4679. org-todo-keywords " "))
  4680. "\n")
  4681. (add-text-properties pos (1- (point)) (list 'face 'org-link))
  4682. (when rtnall
  4683. (insert (org-finalize-agenda-entries rtnall) "\n"))
  4684. (goto-char (point-min))
  4685. (setq buffer-read-only t)
  4686. (org-fit-agenda-window)
  4687. (if (not org-select-agenda-window) (select-window win))))
  4688. (defun org-check-agenda-file (file)
  4689. "Make sure FILE exists. If not, ask user what to do."
  4690. (when (not (file-exists-p file))
  4691. (message "non-existent file %s. [R]emove from list or [A]bort?"
  4692. (abbreviate-file-name file))
  4693. (let ((r (downcase (read-char-exclusive))))
  4694. (cond
  4695. ((equal r ?r)
  4696. (org-remove-file file)
  4697. (throw 'nextfile t))
  4698. (t (error "Abort"))))))
  4699. (defun org-agenda-check-type (error &rest types)
  4700. "Check if agenda buffer is of allowed type.
  4701. If ERROR is non-nil, throw an error, otherwise just return nil."
  4702. (if (memq org-agenda-type types)
  4703. t
  4704. (if error
  4705. (error "Now allowed in %s-type agenda buffers" org-agenda-type)
  4706. nil)))
  4707. (defun org-agenda-quit ()
  4708. "Exit agenda by removing the window or the buffer."
  4709. (interactive)
  4710. (let ((buf (current-buffer)))
  4711. (if (not (one-window-p)) (delete-window))
  4712. (kill-buffer buf)
  4713. (org-agenda-maybe-reset-markers 'force)))
  4714. (defun org-agenda-exit ()
  4715. "Exit agenda by removing the window or the buffer.
  4716. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  4717. Org-mode buffers visited directly by the user will not be touched."
  4718. (interactive)
  4719. (org-release-buffers org-agenda-new-buffers)
  4720. (setq org-agenda-new-buffers nil)
  4721. (org-agenda-quit))
  4722. (defun org-agenda-redo ()
  4723. "Rebuild Agenda.
  4724. When this is the global TODO list, a prefix argument will be interpreted."
  4725. (interactive)
  4726. (message "Rebuilding agenda buffer...")
  4727. (eval org-agenda-redo-command)
  4728. (message "Rebuilding agenda buffer...done"))
  4729. (defun org-agenda-goto-today ()
  4730. "Go to today."
  4731. (interactive)
  4732. (org-agenda-check-type t 'timeline 'agenda)
  4733. (if (boundp 'starting-day)
  4734. (let ((cmd (car org-agenda-redo-command))
  4735. (iall (nth 1 org-agenda-redo-command))
  4736. (nday (nth 3 org-agenda-redo-command))
  4737. (keep (nth 4 org-agenda-redo-command)))
  4738. (eval (list cmd iall nil nday keep)))
  4739. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  4740. (point-min)))))
  4741. (defun org-agenda-later (arg)
  4742. "Go forward in time by `org-agenda-ndays' days.
  4743. With prefix ARG, go forward that many times `org-agenda-ndays'."
  4744. (interactive "p")
  4745. (org-agenda-check-type t 'agenda)
  4746. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  4747. (+ starting-day (* arg org-agenda-ndays)) nil t))
  4748. (defun org-agenda-earlier (arg)
  4749. "Go back in time by `org-agenda-ndays' days.
  4750. With prefix ARG, go back that many times `org-agenda-ndays'."
  4751. (interactive "p")
  4752. (org-agenda-check-type t 'agenda)
  4753. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  4754. (- starting-day (* arg org-agenda-ndays)) nil t))
  4755. (defun org-agenda-week-view ()
  4756. "Switch to weekly view for agenda."
  4757. (interactive)
  4758. (org-agenda-check-type t 'agenda)
  4759. (setq org-agenda-ndays 7)
  4760. (org-agenda-list include-all-loc
  4761. (or (get-text-property (point) 'day)
  4762. starting-day)
  4763. nil t)
  4764. (org-agenda-set-mode-name)
  4765. (message "Switched to week view"))
  4766. (defun org-agenda-day-view ()
  4767. "Switch to daily view for agenda."
  4768. (interactive)
  4769. (org-agenda-check-type t 'agenda)
  4770. (setq org-agenda-ndays 1)
  4771. (org-agenda-list include-all-loc
  4772. (or (get-text-property (point) 'day)
  4773. starting-day)
  4774. nil t)
  4775. (org-agenda-set-mode-name)
  4776. (message "Switched to day view"))
  4777. (defun org-agenda-next-date-line (&optional arg)
  4778. "Jump to the next line indicating a date in agenda buffer."
  4779. (interactive "p")
  4780. (org-agenda-check-type t 'agenda 'timeline)
  4781. (beginning-of-line 1)
  4782. (if (looking-at "^\\S-") (forward-char 1))
  4783. (if (not (re-search-forward "^\\S-" nil t arg))
  4784. (progn
  4785. (backward-char 1)
  4786. (error "No next date after this line in this buffer")))
  4787. (goto-char (match-beginning 0)))
  4788. (defun org-agenda-previous-date-line (&optional arg)
  4789. "Jump to the previous line indicating a date in agenda buffer."
  4790. (interactive "p")
  4791. (org-agenda-check-type t 'agenda 'timeline)
  4792. (beginning-of-line 1)
  4793. (if (not (re-search-backward "^\\S-" nil t arg))
  4794. (error "No previous date before this line in this buffer")))
  4795. ;; Initialize the highlight
  4796. (defvar org-hl (org-make-overlay 1 1))
  4797. (org-overlay-put org-hl 'face 'highlight)
  4798. (defun org-highlight (begin end &optional buffer)
  4799. "Highlight a region with overlay."
  4800. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  4801. org-hl begin end (or buffer (current-buffer))))
  4802. (defun org-unhighlight ()
  4803. "Detach overlay INDEX."
  4804. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  4805. (defun org-agenda-follow-mode ()
  4806. "Toggle follow mode in an agenda buffer."
  4807. (interactive)
  4808. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  4809. (org-agenda-set-mode-name)
  4810. (message "Follow mode is %s"
  4811. (if org-agenda-follow-mode "on" "off")))
  4812. (defun org-agenda-log-mode ()
  4813. "Toggle log mode in an agenda buffer."
  4814. (interactive)
  4815. (org-agenda-check-type t 'agenda 'timeline)
  4816. (setq org-agenda-show-log (not org-agenda-show-log))
  4817. (org-agenda-set-mode-name)
  4818. (org-agenda-redo)
  4819. (message "Log mode is %s"
  4820. (if org-agenda-show-log "on" "off")))
  4821. (defun org-agenda-toggle-diary ()
  4822. "Toggle diary inclusion in an agenda buffer."
  4823. (interactive)
  4824. (org-agenda-check-type t 'agenda)
  4825. (setq org-agenda-include-diary (not org-agenda-include-diary))
  4826. (org-agenda-redo)
  4827. (org-agenda-set-mode-name)
  4828. (message "Diary inclusion turned %s"
  4829. (if org-agenda-include-diary "on" "off")))
  4830. (defun org-agenda-toggle-time-grid ()
  4831. "Toggle time grid in an agenda buffer."
  4832. (interactive)
  4833. (org-agenda-check-type t 'agenda)
  4834. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  4835. (org-agenda-redo)
  4836. (org-agenda-set-mode-name)
  4837. (message "Time-grid turned %s"
  4838. (if org-agenda-use-time-grid "on" "off")))
  4839. (defun org-agenda-set-mode-name ()
  4840. "Set the mode name to indicate all the small mode settings."
  4841. (setq mode-name
  4842. (concat "Org-Agenda"
  4843. (if (equal org-agenda-ndays 1) " Day" "")
  4844. (if (equal org-agenda-ndays 7) " Week" "")
  4845. (if org-agenda-follow-mode " Follow" "")
  4846. (if org-agenda-include-diary " Diary" "")
  4847. (if org-agenda-use-time-grid " Grid" "")
  4848. (if org-agenda-show-log " Log" "")))
  4849. (force-mode-line-update))
  4850. (defun org-agenda-post-command-hook ()
  4851. (and (eolp) (not (bolp)) (backward-char 1))
  4852. (if (and org-agenda-follow-mode
  4853. (get-text-property (point) 'org-marker))
  4854. (org-agenda-show)))
  4855. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  4856. (defun org-get-entries-from-diary (date)
  4857. "Get the (Emacs Calendar) diary entries for DATE."
  4858. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  4859. (diary-display-hook '(fancy-diary-display))
  4860. (list-diary-entries-hook
  4861. (cons 'org-diary-default-entry list-diary-entries-hook))
  4862. (diary-file-name-prefix-function nil) ; turn this feature off
  4863. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  4864. entries
  4865. (org-disable-agenda-to-diary t))
  4866. (save-excursion
  4867. (save-window-excursion
  4868. (list-diary-entries date 1)))
  4869. (if (not (get-buffer fancy-diary-buffer))
  4870. (setq entries nil)
  4871. (with-current-buffer fancy-diary-buffer
  4872. (setq buffer-read-only nil)
  4873. (if (= (point-max) 1)
  4874. ;; No entries
  4875. (setq entries nil)
  4876. ;; Omit the date and other unnecessary stuff
  4877. (org-agenda-cleanup-fancy-diary)
  4878. ;; Add prefix to each line and extend the text properties
  4879. (if (= (point-max) 1)
  4880. (setq entries nil)
  4881. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  4882. (set-buffer-modified-p nil)
  4883. (kill-buffer fancy-diary-buffer)))
  4884. (when entries
  4885. (setq entries (org-split-string entries "\n"))
  4886. (setq entries
  4887. (mapcar
  4888. (lambda (x)
  4889. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  4890. ;; Extend the text properties to the beginning of the line
  4891. (add-text-properties
  4892. 0 (length x)
  4893. (text-properties-at (1- (length x)) x)
  4894. x)
  4895. x)
  4896. entries)))))
  4897. (defun org-agenda-cleanup-fancy-diary ()
  4898. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  4899. This gets rid of the date, the underline under the date, and
  4900. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  4901. date. It also removes lines that contain only whitespace."
  4902. (goto-char (point-min))
  4903. (if (looking-at ".*?:[ \t]*")
  4904. (progn
  4905. (replace-match "")
  4906. (re-search-forward "\n=+$" nil t)
  4907. (replace-match "")
  4908. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  4909. (re-search-forward "\n=+$" nil t)
  4910. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  4911. (goto-char (point-min))
  4912. (while (re-search-forward "^ +\n" nil t)
  4913. (replace-match ""))
  4914. (goto-char (point-min))
  4915. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  4916. (replace-match "")))
  4917. ;; Make sure entries from the diary have the right text properties.
  4918. (eval-after-load "diary-lib"
  4919. '(if (boundp 'diary-modify-entry-list-string-function)
  4920. ;; We can rely on the hook, nothing to do
  4921. nil
  4922. ;; Hook not avaiable, must use advice to make this work
  4923. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  4924. "Make the position visible."
  4925. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  4926. (stringp string)
  4927. buffer-file-name)
  4928. (setq string (org-modify-diary-entry-string string))))))
  4929. (defun org-modify-diary-entry-string (string)
  4930. "Add text properties to string, allowing org-mode to act on it."
  4931. (add-text-properties
  4932. 0 (length string)
  4933. (list 'mouse-face 'highlight
  4934. 'keymap org-agenda-keymap
  4935. 'help-echo
  4936. (format
  4937. "mouse-2 or RET jump to diary file %s"
  4938. (abbreviate-file-name buffer-file-name))
  4939. 'org-agenda-diary-link t
  4940. 'org-marker (org-agenda-new-marker (point-at-bol)))
  4941. string)
  4942. string)
  4943. (defun org-diary-default-entry ()
  4944. "Add a dummy entry to the diary.
  4945. Needed to avoid empty dates which mess up holiday display."
  4946. ;; Catch the error if dealing with the new add-to-diary-alist
  4947. (when org-disable-agenda-to-diary
  4948. (condition-case nil
  4949. (add-to-diary-list original-date "Org-mode dummy" "")
  4950. (error
  4951. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  4952. (defun org-cycle-agenda-files ()
  4953. "Cycle through the files in `org-agenda-files'.
  4954. If the current buffer visits an agenda file, find the next one in the list.
  4955. If the current buffer does not, find the first agenda file."
  4956. (interactive)
  4957. (let* ((fs (org-agenda-files t))
  4958. (files (append fs (list (car fs))))
  4959. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  4960. file)
  4961. (unless files (error "No agenda files"))
  4962. (catch 'exit
  4963. (while (setq file (pop files))
  4964. (if (equal (file-truename file) tcf)
  4965. (when (car files)
  4966. (find-file (car files))
  4967. (throw 'exit t))))
  4968. (find-file (car fs)))))
  4969. (defun org-agenda-file-to-end ()
  4970. "Move/add the current file to the end of the agenda file list.
  4971. If the file is not present in the list, it is appended to the list. If it is
  4972. present, it is moved there."
  4973. (interactive)
  4974. (org-agenda-file-to-front 'to-end))
  4975. (defun org-agenda-file-to-front (&optional to-end)
  4976. "Move/add the current file to the top of the agenda file list.
  4977. If the file is not present in the list, it is added to the front. If it is
  4978. present, it is moved there. With optional argument TO-END, add/move to the
  4979. end of the list."
  4980. (interactive "P")
  4981. (let ((file-alist (mapcar (lambda (x)
  4982. (cons (file-truename x) x))
  4983. (org-agenda-files t)))
  4984. (ctf (file-truename buffer-file-name))
  4985. x had)
  4986. (setq x (assoc ctf file-alist) had x)
  4987. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  4988. (if to-end
  4989. (setq file-alist (append (delq x file-alist) (list x)))
  4990. (setq file-alist (cons x (delq x file-alist))))
  4991. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  4992. (org-install-agenda-files-menu)
  4993. (message "File %s to %s of agenda file list"
  4994. (if had "moved" "added") (if to-end "end" "front"))))
  4995. (defun org-remove-file (&optional file)
  4996. "Remove current file from the list of files in variable `org-agenda-files'.
  4997. These are the files which are being checked for agenda entries.
  4998. Optional argument FILE means, use this file instead of the current."
  4999. (interactive)
  5000. (let* ((file (or file buffer-file-name))
  5001. (true-file (file-truename file))
  5002. (afile (abbreviate-file-name file))
  5003. (files (delq nil (mapcar
  5004. (lambda (x)
  5005. (if (equal true-file
  5006. (file-truename x))
  5007. nil x))
  5008. (org-agenda-files t)))))
  5009. (if (not (= (length files) (length (org-agenda-files t))))
  5010. (progn
  5011. (org-store-new-agenda-file-list files)
  5012. (org-install-agenda-files-menu)
  5013. (message "Removed file: %s" afile))
  5014. (message "File was not in list: %s" afile))))
  5015. (defun org-file-menu-entry (file)
  5016. (vector file (list 'find-file file) t))
  5017. ;; FIXME: Maybe we removed a buffer visited through the menu from
  5018. ;; org-agenda-new-buffers, so that the buffer will not be removed
  5019. ;; when exiting the agenda????
  5020. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive)
  5021. "Return a list of all relevant day numbers from BEG to END buffer positions.
  5022. If NO-RANGES is non-nil, include only the start and end dates of a range,
  5023. not every single day in the range. If FORCE-TODAY is non-nil, make
  5024. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  5025. inactive time stamps (those in square brackets) are included."
  5026. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  5027. dates date day day1 day2 ts1 ts2)
  5028. (if force-today
  5029. (setq dates (list (time-to-days (current-time)))))
  5030. (save-excursion
  5031. (goto-char beg)
  5032. (while (re-search-forward re end t)
  5033. (setq day (time-to-days (org-time-string-to-time
  5034. (substring (match-string 1) 0 10))))
  5035. (or (memq day dates) (push day dates)))
  5036. (unless no-ranges
  5037. (goto-char beg)
  5038. (while (re-search-forward org-tr-regexp end t)
  5039. (setq ts1 (substring (match-string 1) 0 10)
  5040. ts2 (substring (match-string 2) 0 10)
  5041. day1 (time-to-days (org-time-string-to-time ts1))
  5042. day2 (time-to-days (org-time-string-to-time ts2)))
  5043. (while (< (setq day1 (1+ day1)) day2)
  5044. (or (memq day1 dates) (push day1 dates)))))
  5045. (sort dates '<))))
  5046. ;;;###autoload
  5047. (defun org-diary (&rest args)
  5048. "Return diary information from org-files.
  5049. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  5050. It accesses org files and extracts information from those files to be
  5051. listed in the diary. The function accepts arguments specifying what
  5052. items should be listed. The following arguments are allowed:
  5053. :timestamp List the headlines of items containing a date stamp or
  5054. date range matching the selected date. Deadlines will
  5055. also be listed, on the expiration day.
  5056. :deadline List any deadlines past due, or due within
  5057. `org-deadline-warning-days'. The listing occurs only
  5058. in the diary for *today*, not at any other date. If
  5059. an entry is marked DONE, it is no longer listed.
  5060. :scheduled List all items which are scheduled for the given date.
  5061. The diary for *today* also contains items which were
  5062. scheduled earlier and are not yet marked DONE.
  5063. :todo List all TODO items from the org-file. This may be a
  5064. long list - so this is not turned on by default.
  5065. Like deadlines, these entries only show up in the
  5066. diary for *today*, not at any other date.
  5067. The call in the diary file should look like this:
  5068. &%%(org-diary) ~/path/to/some/orgfile.org
  5069. Use a separate line for each org file to check. Or, if you omit the file name,
  5070. all files listed in `org-agenda-files' will be checked automatically:
  5071. &%%(org-diary)
  5072. If you don't give any arguments (as in the example above), the default
  5073. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  5074. also be written as
  5075. &%%(org-diary :deadline :timestamp :scheduled)
  5076. The function expects the lisp variables `entry' and `date' to be provided
  5077. by the caller, because this is how the calendar works. Don't use this
  5078. function from a program - use `org-agenda-get-day-entries' instead."
  5079. (org-agenda-maybe-reset-markers)
  5080. (org-compile-prefix-format org-agenda-prefix-format)
  5081. (setq args (or args '(:deadline :scheduled :timestamp)))
  5082. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  5083. (list entry)
  5084. (org-agenda-files t)))
  5085. file rtn results)
  5086. ;; If this is called during org-agenda, don't return any entries to
  5087. ;; the calendar. Org Agenda will list these entries itself.
  5088. (if org-disable-agenda-to-diary (setq files nil))
  5089. (while (setq file (pop files))
  5090. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  5091. (setq results (append results rtn)))
  5092. (if results
  5093. (concat (org-finalize-agenda-entries results) "\n"))))
  5094. (defvar org-category-table nil)
  5095. (defun org-get-category-table ()
  5096. "Get the table of categories and positions in current buffer."
  5097. (let (tbl)
  5098. (save-excursion
  5099. (goto-char (point-min))
  5100. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  5101. (push (cons (point) (org-trim (match-string 2))) tbl)))
  5102. tbl))
  5103. (defun org-get-category (&optional pos)
  5104. "Get the category applying to position POS."
  5105. (if (not org-category-table)
  5106. (cond
  5107. ((null org-category)
  5108. (setq org-category
  5109. (if buffer-file-name
  5110. (file-name-sans-extension
  5111. (file-name-nondirectory buffer-file-name))
  5112. "???")))
  5113. ((symbolp org-category) (symbol-name org-category))
  5114. (t org-category))
  5115. (let ((tbl org-category-table)
  5116. (pos (or pos (point))))
  5117. (while (and tbl (> (caar tbl) pos))
  5118. (pop tbl))
  5119. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  5120. org-category-table))))))
  5121. (defun org-agenda-get-day-entries (file date &rest args)
  5122. "Does the work for `org-diary' and `org-agenda'.
  5123. FILE is the path to a file to be checked for entries. DATE is date like
  5124. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5125. which kind of entries should be extracted. For details about these, see
  5126. the documentation of `org-diary'."
  5127. (setq args (or args '(:deadline :scheduled :timestamp)))
  5128. (let* ((org-startup-with-deadline-check nil)
  5129. (org-startup-folded nil)
  5130. (org-startup-align-all-tables nil)
  5131. (buffer (if (file-exists-p file)
  5132. (org-get-agenda-file-buffer file)
  5133. (error "No such file %s" file)))
  5134. arg results rtn)
  5135. (if (not buffer)
  5136. ;; If file does not exist, make sure an error message ends up in diary
  5137. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5138. (with-current-buffer buffer
  5139. (unless (eq major-mode 'org-mode)
  5140. (error "Agenda file %s is not in `org-mode'" file))
  5141. (setq org-category-table (org-get-category-table))
  5142. (let ((case-fold-search nil))
  5143. (save-excursion
  5144. (save-restriction
  5145. (if org-respect-restriction
  5146. (if (org-region-active-p)
  5147. ;; Respect a region to restrict search
  5148. (narrow-to-region (region-beginning) (region-end)))
  5149. ;; If we work for the calendar or many files,
  5150. ;; get rid of any restriction
  5151. (widen))
  5152. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  5153. (while (setq arg (pop args))
  5154. (cond
  5155. ((and (eq arg :todo)
  5156. (equal date (calendar-current-date)))
  5157. (setq rtn (org-agenda-get-todos))
  5158. (setq results (append results rtn)))
  5159. ((eq arg :timestamp)
  5160. (setq rtn (org-agenda-get-blocks))
  5161. (setq results (append results rtn))
  5162. (setq rtn (org-agenda-get-timestamps))
  5163. (setq results (append results rtn)))
  5164. ((eq arg :scheduled)
  5165. (setq rtn (org-agenda-get-scheduled))
  5166. (setq results (append results rtn)))
  5167. ((eq arg :closed)
  5168. (setq rtn (org-agenda-get-closed))
  5169. (setq results (append results rtn)))
  5170. ((and (eq arg :deadline)
  5171. (equal date (calendar-current-date)))
  5172. (setq rtn (org-agenda-get-deadlines))
  5173. (setq results (append results rtn))))))))
  5174. results))))
  5175. (defun org-entry-is-done-p ()
  5176. "Is the current entry marked DONE?"
  5177. (save-excursion
  5178. (and (re-search-backward "[\r\n]\\*" nil t)
  5179. (looking-at org-nl-done-regexp))))
  5180. (defun org-at-date-range-p ()
  5181. "Is the cursor inside a date range?"
  5182. (interactive)
  5183. (save-excursion
  5184. (catch 'exit
  5185. (let ((pos (point)))
  5186. (skip-chars-backward "^<\r\n")
  5187. (skip-chars-backward "<")
  5188. (and (looking-at org-tr-regexp)
  5189. (>= (match-end 0) pos)
  5190. (throw 'exit t))
  5191. (skip-chars-backward "^<\r\n")
  5192. (skip-chars-backward "<")
  5193. (and (looking-at org-tr-regexp)
  5194. (>= (match-end 0) pos)
  5195. (throw 'exit t)))
  5196. nil)))
  5197. (defun org-agenda-get-todos ()
  5198. "Return the TODO information for agenda display."
  5199. (let* ((props (list 'face nil
  5200. 'done-face 'org-done
  5201. 'mouse-face 'highlight
  5202. 'keymap org-agenda-keymap
  5203. 'help-echo
  5204. (format "mouse-2 or RET jump to org file %s"
  5205. (abbreviate-file-name buffer-file-name))))
  5206. (regexp (concat "[\n\r]\\*+ *\\("
  5207. (if org-select-this-todo-keyword
  5208. (concat "\\<\\(" org-select-this-todo-keyword
  5209. "\\)\\>")
  5210. org-not-done-regexp)
  5211. "[^\n\r]*\\)"))
  5212. marker priority category tags
  5213. ee txt)
  5214. (goto-char (point-min))
  5215. (while (re-search-forward regexp nil t)
  5216. (goto-char (match-beginning 1))
  5217. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  5218. category (org-get-category)
  5219. tags (org-get-tags-at (point))
  5220. txt (org-format-agenda-item "" (match-string 1) category tags)
  5221. priority
  5222. (+ (org-get-priority txt)
  5223. (if org-todo-kwd-priority-p
  5224. (- org-todo-kwd-max-priority -2
  5225. (length
  5226. (member (match-string 2) org-todo-keywords)))
  5227. 1)))
  5228. (add-text-properties
  5229. 0 (length txt) (append (list 'org-marker marker 'org-hd-marker marker
  5230. 'priority priority 'category category)
  5231. props)
  5232. txt)
  5233. (push txt ee)
  5234. (goto-char (match-end 1)))
  5235. (nreverse ee)))
  5236. (defconst org-agenda-no-heading-message
  5237. "No heading for this item in buffer or region.")
  5238. (defun org-agenda-get-timestamps ()
  5239. "Return the date stamp information for agenda display."
  5240. (let* ((props (list 'face nil
  5241. 'mouse-face 'highlight
  5242. 'keymap org-agenda-keymap
  5243. 'help-echo
  5244. (format "mouse-2 or RET jump to org file %s"
  5245. (abbreviate-file-name buffer-file-name))))
  5246. (regexp (regexp-quote
  5247. (substring
  5248. (format-time-string
  5249. (car org-time-stamp-formats)
  5250. (apply 'encode-time ; DATE bound by calendar
  5251. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5252. 0 11)))
  5253. marker hdmarker deadlinep scheduledp donep tmp priority category
  5254. ee txt timestr tags)
  5255. (goto-char (point-min))
  5256. (while (re-search-forward regexp nil t)
  5257. (if (not (save-match-data (org-at-date-range-p)))
  5258. (progn
  5259. (setq marker (org-agenda-new-marker (match-beginning 0))
  5260. category (org-get-category (match-beginning 0))
  5261. tmp (buffer-substring (max (point-min)
  5262. (- (match-beginning 0)
  5263. org-ds-keyword-length))
  5264. (match-beginning 0))
  5265. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5266. deadlinep (string-match org-deadline-regexp tmp)
  5267. scheduledp (string-match org-scheduled-regexp tmp)
  5268. donep (org-entry-is-done-p))
  5269. (if (string-match ">" timestr)
  5270. ;; substring should only run to end of time stamp
  5271. (setq timestr (substring timestr 0 (match-end 0))))
  5272. (save-excursion
  5273. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5274. (progn
  5275. (goto-char (match-end 1))
  5276. (setq hdmarker (org-agenda-new-marker)
  5277. tags (org-get-tags-at))
  5278. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5279. (setq txt (org-format-agenda-item
  5280. (format "%s%s"
  5281. (if deadlinep "Deadline: " "")
  5282. (if scheduledp "Scheduled: " ""))
  5283. (match-string 1) category tags timestr)))
  5284. (setq txt org-agenda-no-heading-message))
  5285. (setq priority (org-get-priority txt))
  5286. (add-text-properties
  5287. 0 (length txt) (append (list 'org-marker marker
  5288. 'org-hd-marker hdmarker) props)
  5289. txt)
  5290. (if deadlinep
  5291. (add-text-properties
  5292. 0 (length txt)
  5293. (list 'face
  5294. (if donep 'org-done 'org-warning)
  5295. 'undone-face 'org-warning
  5296. 'done-face 'org-done
  5297. 'category category
  5298. 'priority (+ 100 priority))
  5299. txt)
  5300. (if scheduledp
  5301. (add-text-properties
  5302. 0 (length txt)
  5303. (list 'face 'org-scheduled-today
  5304. 'undone-face 'org-scheduled-today
  5305. 'done-face 'org-done
  5306. 'category category
  5307. priority (+ 99 priority))
  5308. txt)
  5309. (add-text-properties
  5310. 0 (length txt)
  5311. (list 'priority priority 'category category) txt)))
  5312. (push txt ee))
  5313. (outline-next-heading))))
  5314. (nreverse ee)))
  5315. (defun org-agenda-get-closed ()
  5316. "Return the logged TODO entries for agenda display."
  5317. (let* ((props (list 'mouse-face 'highlight
  5318. 'keymap org-agenda-keymap
  5319. 'help-echo
  5320. (format "mouse-2 or RET jump to org file %s"
  5321. (abbreviate-file-name buffer-file-name))))
  5322. (regexp (concat
  5323. "\\<" org-closed-string " *\\["
  5324. (regexp-quote
  5325. (substring
  5326. (format-time-string
  5327. (car org-time-stamp-formats)
  5328. (apply 'encode-time ; DATE bound by calendar
  5329. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5330. 1 11))))
  5331. marker hdmarker priority category tags
  5332. ee txt timestr)
  5333. (goto-char (point-min))
  5334. (while (re-search-forward regexp nil t)
  5335. (if (not (save-match-data (org-at-date-range-p)))
  5336. (progn
  5337. (setq marker (org-agenda-new-marker (match-beginning 0))
  5338. category (org-get-category (match-beginning 0))
  5339. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5340. ;; donep (org-entry-is-done-p)
  5341. )
  5342. (if (string-match "\\]" timestr)
  5343. ;; substring should only run to end of time stamp
  5344. (setq timestr (substring timestr 0 (match-end 0))))
  5345. (save-excursion
  5346. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5347. (progn
  5348. (goto-char (match-end 1))
  5349. (setq hdmarker (org-agenda-new-marker)
  5350. tags (org-get-tags-at))
  5351. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5352. (setq txt (org-format-agenda-item
  5353. "Closed: "
  5354. (match-string 1) category tags timestr)))
  5355. (setq txt org-agenda-no-heading-message))
  5356. (setq priority 100000)
  5357. (add-text-properties
  5358. 0 (length txt) (append (list 'org-marker marker
  5359. 'org-hd-marker hdmarker
  5360. 'face 'org-done
  5361. 'priority priority
  5362. 'category category
  5363. 'undone-face 'org-warning
  5364. 'done-face 'org-done) props)
  5365. txt)
  5366. (push txt ee))
  5367. (outline-next-heading))))
  5368. (nreverse ee)))
  5369. (defun org-agenda-get-deadlines ()
  5370. "Return the deadline information for agenda display."
  5371. (let* ((wdays org-deadline-warning-days)
  5372. (props (list 'mouse-face 'highlight
  5373. 'keymap org-agenda-keymap
  5374. 'help-echo
  5375. (format "mouse-2 or RET jump to org file %s"
  5376. (abbreviate-file-name buffer-file-name))))
  5377. (regexp org-deadline-time-regexp)
  5378. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5379. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5380. d2 diff pos pos1 category tags
  5381. ee txt head)
  5382. (goto-char (point-min))
  5383. (while (re-search-forward regexp nil t)
  5384. (setq pos (1- (match-beginning 1))
  5385. d2 (time-to-days
  5386. (org-time-string-to-time (match-string 1)))
  5387. diff (- d2 d1))
  5388. ;; When to show a deadline in the calendar:
  5389. ;; If the expiration is within wdays warning time.
  5390. ;; Past-due deadlines are only shown on the current date
  5391. (if (and (< diff wdays) todayp (not (= diff 0)))
  5392. (save-excursion
  5393. (setq category (org-get-category))
  5394. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5395. (progn
  5396. (goto-char (match-end 0))
  5397. (setq pos1 (match-end 1))
  5398. (setq tags (org-get-tags-at pos1))
  5399. (setq head (buffer-substring-no-properties
  5400. (point)
  5401. (progn (skip-chars-forward "^\r\n")
  5402. (point))))
  5403. (if (string-match org-looking-at-done-regexp head)
  5404. (setq txt nil)
  5405. (setq txt (org-format-agenda-item
  5406. (format "In %3d d.: " diff) head category tags))))
  5407. (setq txt org-agenda-no-heading-message))
  5408. (when txt
  5409. (add-text-properties
  5410. 0 (length txt)
  5411. (append
  5412. (list 'org-marker (org-agenda-new-marker pos)
  5413. 'org-hd-marker (org-agenda-new-marker pos1)
  5414. 'priority (+ (- 10 diff) (org-get-priority txt))
  5415. 'category category
  5416. 'face (cond ((<= diff 0) 'org-warning)
  5417. ((<= diff 5) 'org-scheduled-previously)
  5418. (t nil))
  5419. 'undone-face (cond
  5420. ((<= diff 0) 'org-warning)
  5421. ((<= diff 5) 'org-scheduled-previously)
  5422. (t nil))
  5423. 'done-face 'org-done)
  5424. props)
  5425. txt)
  5426. (push txt ee)))))
  5427. ee))
  5428. (defun org-agenda-get-scheduled ()
  5429. "Return the scheduled information for agenda display."
  5430. (let* ((props (list 'face 'org-scheduled-previously
  5431. 'undone-face 'org-scheduled-previously
  5432. 'done-face 'org-done
  5433. 'mouse-face 'highlight
  5434. 'keymap org-agenda-keymap
  5435. 'help-echo
  5436. (format "mouse-2 or RET jump to org file %s"
  5437. (abbreviate-file-name buffer-file-name))))
  5438. (regexp org-scheduled-time-regexp)
  5439. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  5440. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  5441. d2 diff pos pos1 category tags
  5442. ee txt head)
  5443. (goto-char (point-min))
  5444. (while (re-search-forward regexp nil t)
  5445. (setq pos (1- (match-beginning 1))
  5446. d2 (time-to-days
  5447. (org-time-string-to-time (match-string 1)))
  5448. diff (- d2 d1))
  5449. ;; When to show a scheduled item in the calendar:
  5450. ;; If it is on or past the date.
  5451. (if (and (< diff 0) todayp)
  5452. (save-excursion
  5453. (setq category (org-get-category))
  5454. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  5455. (progn
  5456. (goto-char (match-end 0))
  5457. (setq pos1 (match-end 1))
  5458. (setq tags (org-get-tags-at))
  5459. (setq head (buffer-substring-no-properties
  5460. (point)
  5461. (progn (skip-chars-forward "^\r\n") (point))))
  5462. (if (string-match org-looking-at-done-regexp head)
  5463. (setq txt nil)
  5464. (setq txt (org-format-agenda-item
  5465. (format "Sched.%2dx: " (- 1 diff)) head
  5466. category tags))))
  5467. (setq txt org-agenda-no-heading-message))
  5468. (when txt
  5469. (add-text-properties
  5470. 0 (length txt)
  5471. (append (list 'org-marker (org-agenda-new-marker pos)
  5472. 'org-hd-marker (org-agenda-new-marker pos1)
  5473. 'priority (+ (- 5 diff) (org-get-priority txt))
  5474. 'category category)
  5475. props) txt)
  5476. (push txt ee)))))
  5477. ee))
  5478. (defun org-agenda-get-blocks ()
  5479. "Return the date-range information for agenda display."
  5480. (let* ((props (list 'face nil
  5481. 'mouse-face 'highlight
  5482. 'keymap org-agenda-keymap
  5483. 'help-echo
  5484. (format "mouse-2 or RET jump to org file %s"
  5485. (abbreviate-file-name buffer-file-name))))
  5486. (regexp org-tr-regexp)
  5487. (d0 (calendar-absolute-from-gregorian date))
  5488. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  5489. (goto-char (point-min))
  5490. (while (re-search-forward regexp nil t)
  5491. (setq timestr (match-string 0)
  5492. s1 (match-string 1)
  5493. s2 (match-string 2)
  5494. d1 (time-to-days (org-time-string-to-time s1))
  5495. d2 (time-to-days (org-time-string-to-time s2)))
  5496. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  5497. ;; Only allow days between the limits, because the normal
  5498. ;; date stamps will catch the limits.
  5499. (save-excursion
  5500. (setq marker (org-agenda-new-marker (point)))
  5501. (setq category (org-get-category))
  5502. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5503. (progn
  5504. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  5505. (goto-char (match-end 1))
  5506. (setq tags (org-get-tags-at))
  5507. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  5508. (setq txt (org-format-agenda-item
  5509. (format (if (= d1 d2) "" "(%d/%d): ")
  5510. (1+ (- d0 d1)) (1+ (- d2 d1)))
  5511. (match-string 1) category tags
  5512. (if (= d0 d1) timestr))))
  5513. (setq txt org-agenda-no-heading-message))
  5514. (add-text-properties
  5515. 0 (length txt) (append (list 'org-marker marker
  5516. 'org-hd-marker hdmarker
  5517. 'priority (org-get-priority txt)
  5518. 'category category)
  5519. props)
  5520. txt)
  5521. (push txt ee)))
  5522. (outline-next-heading))
  5523. ;; Sort the entries by expiration date.
  5524. (nreverse ee)))
  5525. (defconst org-plain-time-of-day-regexp
  5526. (concat
  5527. "\\(\\<[012]?[0-9]"
  5528. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  5529. "\\(--?"
  5530. "\\(\\<[012]?[0-9]"
  5531. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  5532. "\\)?")
  5533. "Regular expression to match a plain time or time range.
  5534. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  5535. groups carry important information:
  5536. 0 the full match
  5537. 1 the first time, range or not
  5538. 8 the second time, if it is a range.")
  5539. (defconst org-stamp-time-of-day-regexp
  5540. (concat
  5541. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  5542. "\\([012][0-9]:[0-5][0-9]\\)>"
  5543. "\\(--?"
  5544. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  5545. "Regular expression to match a timestamp time or time range.
  5546. After a match, the following groups carry important information:
  5547. 0 the full match
  5548. 1 date plus weekday, for backreferencing to make sure both times on same day
  5549. 2 the first time, range or not
  5550. 4 the second time, if it is a range.")
  5551. (defvar org-prefix-has-time nil
  5552. "A flag, set by `org-compile-prefix-format'.
  5553. The flag is set if the currently compiled format contains a `%t'.")
  5554. (defvar org-prefix-has-tag nil
  5555. "A flag, set by `org-compile-prefix-format'.
  5556. The flag is set if the currently compiled format contains a `%T'.")
  5557. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  5558. "Format TXT to be inserted into the agenda buffer.
  5559. In particular, it adds the prefix and corresponding text properties. EXTRA
  5560. must be a string and replaces the `%s' specifier in the prefix format.
  5561. CATEGORY (string, symbol or nil) may be used to overrule the default
  5562. category taken from local variable or file name. It will replace the `%c'
  5563. specifier in the format. DOTIME, when non-nil, indicates that a
  5564. time-of-day should be extracted from TXT for sorting of this entry, and for
  5565. the `%t' specifier in the format. When DOTIME is a string, this string is
  5566. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  5567. only the correctly processes TXT should be returned - this is used by
  5568. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  5569. (save-match-data
  5570. ;; Diary entries sometimes have extra whitespace at the beginning
  5571. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  5572. (let* ((category (or category
  5573. org-category
  5574. (if buffer-file-name
  5575. (file-name-sans-extension
  5576. (file-name-nondirectory buffer-file-name))
  5577. "")))
  5578. (tag (or (nth (1- (or (length tags) 0)) tags) ""))
  5579. time ;; needed for the eval of the prefix format
  5580. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  5581. (time-of-day (and dotime (org-get-time-of-day ts)))
  5582. stamp plain s0 s1 s2 rtn)
  5583. (when (and dotime time-of-day org-prefix-has-time)
  5584. ;; Extract starting and ending time and move them to prefix
  5585. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  5586. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  5587. (setq s0 (match-string 0 ts)
  5588. s1 (match-string (if plain 1 2) ts)
  5589. s2 (match-string (if plain 8 4) ts))
  5590. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  5591. ;; them, we might want to remove them there to avoid duplication.
  5592. ;; The user can turn this off with a variable.
  5593. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  5594. (string-match (concat (regexp-quote s0) " *") txt)
  5595. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  5596. (= (match-beginning 0) 0)
  5597. t))
  5598. (setq txt (replace-match "" nil nil txt))))
  5599. ;; Normalize the time(s) to 24 hour
  5600. (if s1 (setq s1 (org-get-time-of-day s1 'string)))
  5601. (if s2 (setq s2 (org-get-time-of-day s2 'string))))
  5602. (when (and (or (eq org-agenda-remove-tags-when-in-prefix t)
  5603. (and org-agenda-remove-tags-when-in-prefix
  5604. org-prefix-has-tag))
  5605. (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" txt))
  5606. (setq txt (replace-match "" t t txt)))
  5607. ;; Create the final string
  5608. (if noprefix
  5609. (setq rtn txt)
  5610. ;; Prepare the variables needed in the eval of the compiled format
  5611. (setq time (cond (s2 (concat s1 "-" s2))
  5612. (s1 (concat s1 "......"))
  5613. (t ""))
  5614. extra (or extra "")
  5615. category (if (symbolp category) (symbol-name category) category))
  5616. ;; Evaluate the compiled format
  5617. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  5618. ;; And finally add the text properties
  5619. (add-text-properties
  5620. 0 (length rtn) (list 'category (downcase category)
  5621. 'tags tags
  5622. 'prefix-length (- (length rtn) (length txt))
  5623. 'time-of-day time-of-day
  5624. 'dotime dotime)
  5625. rtn)
  5626. rtn)))
  5627. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  5628. (catch 'exit
  5629. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  5630. ((and todayp (member 'today (car org-agenda-time-grid))))
  5631. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  5632. ((member 'weekly (car org-agenda-time-grid)))
  5633. (t (throw 'exit list)))
  5634. (let* ((have (delq nil (mapcar
  5635. (lambda (x) (get-text-property 1 'time-of-day x))
  5636. list)))
  5637. (string (nth 1 org-agenda-time-grid))
  5638. (gridtimes (nth 2 org-agenda-time-grid))
  5639. (req (car org-agenda-time-grid))
  5640. (remove (member 'remove-match req))
  5641. new time)
  5642. (if (and (member 'require-timed req) (not have))
  5643. ;; don't show empty grid
  5644. (throw 'exit list))
  5645. (while (setq time (pop gridtimes))
  5646. (unless (and remove (member time have))
  5647. (setq time (int-to-string time))
  5648. (push (org-format-agenda-item
  5649. nil string "" nil ;; FIXME: put a category for the grid?
  5650. (concat (substring time 0 -2) ":" (substring time -2)))
  5651. new)
  5652. (put-text-property
  5653. 1 (length (car new)) 'face 'org-time-grid (car new))))
  5654. (if (member 'time-up org-agenda-sorting-strategy)
  5655. (append new list)
  5656. (append list new)))))
  5657. (defun org-compile-prefix-format (format)
  5658. "Compile the prefix format into a Lisp form that can be evaluated.
  5659. The resulting form is returned and stored in the variable
  5660. `org-prefix-format-compiled'."
  5661. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  5662. (let ((start 0) varform vars var (s format)e c f opt)
  5663. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  5664. s start)
  5665. (setq var (cdr (assoc (match-string 4 s)
  5666. '(("c" . category) ("t" . time) ("s" . extra)
  5667. ("T" . tag))))
  5668. c (or (match-string 3 s) "")
  5669. opt (match-beginning 1)
  5670. start (1+ (match-beginning 0)))
  5671. (if (equal var 'time) (setq org-prefix-has-time t))
  5672. (if (equal var 'tag) (setq org-prefix-has-tag t))
  5673. (setq f (concat "%" (match-string 2 s) "s"))
  5674. (if opt
  5675. (setq varform
  5676. `(if (equal "" ,var)
  5677. ""
  5678. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  5679. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  5680. (setq s (replace-match "%s" t nil s))
  5681. (push varform vars))
  5682. (setq vars (nreverse vars))
  5683. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  5684. (defun org-get-time-of-day (s &optional string)
  5685. "Check string S for a time of day.
  5686. If found, return it as a military time number between 0 and 2400.
  5687. If not found, return nil.
  5688. The optional STRING argument forces conversion into a 5 character wide string
  5689. HH:MM."
  5690. (save-match-data
  5691. (when
  5692. (or
  5693. (string-match
  5694. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  5695. (string-match
  5696. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  5697. (let* ((t0 (+ (* 100
  5698. (+ (string-to-number (match-string 1 s))
  5699. (if (and (match-beginning 4)
  5700. (equal (downcase (match-string 4 s)) "pm"))
  5701. 12 0)))
  5702. (if (match-beginning 3)
  5703. (string-to-number (match-string 3 s))
  5704. 0)))
  5705. (t1 (concat " "
  5706. (if (< t0 100) "0" "") (if (< t0 10) "0" "")
  5707. (int-to-string t0))))
  5708. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  5709. (defun org-finalize-agenda-entries (list)
  5710. "Sort and concatenate the agenda items."
  5711. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  5712. (defsubst org-cmp-priority (a b)
  5713. "Compare the priorities of string A and B."
  5714. (let ((pa (or (get-text-property 1 'priority a) 0))
  5715. (pb (or (get-text-property 1 'priority b) 0)))
  5716. (cond ((> pa pb) +1)
  5717. ((< pa pb) -1)
  5718. (t nil))))
  5719. (defsubst org-cmp-category (a b)
  5720. "Compare the string values of categories of strings A and B."
  5721. (let ((ca (or (get-text-property 1 'category a) ""))
  5722. (cb (or (get-text-property 1 'category b) "")))
  5723. (cond ((string-lessp ca cb) -1)
  5724. ((string-lessp cb ca) +1)
  5725. (t nil))))
  5726. (defsubst org-cmp-time (a b)
  5727. "Compare the time-of-day values of strings A and B."
  5728. (let* ((def (if org-sort-agenda-notime-is-late 2401 -1))
  5729. (ta (or (get-text-property 1 'time-of-day a) def))
  5730. (tb (or (get-text-property 1 'time-of-day b) def)))
  5731. (cond ((< ta tb) -1)
  5732. ((< tb ta) +1)
  5733. (t nil))))
  5734. (defun org-entries-lessp (a b)
  5735. "Predicate for sorting agenda entries."
  5736. ;; The following variables will be used when the form is evaluated.
  5737. (let* ((time-up (org-cmp-time a b))
  5738. (time-down (if time-up (- time-up) nil))
  5739. (priority-up (org-cmp-priority a b))
  5740. (priority-down (if priority-up (- priority-up) nil))
  5741. (category-up (org-cmp-category a b))
  5742. (category-down (if category-up (- category-up) nil))
  5743. (category-keep (if category-up +1 nil))) ; FIXME +1 or -1?
  5744. (cdr (assoc
  5745. (eval (cons 'or org-agenda-sorting-strategy))
  5746. '((-1 . t) (1 . nil) (nil . nil))))))
  5747. (defun org-agenda-show-priority ()
  5748. "Show the priority of the current item.
  5749. This priority is composed of the main priority given with the [#A] cookies,
  5750. and by additional input from the age of a schedules or deadline entry."
  5751. (interactive)
  5752. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  5753. (message "Priority is %d" (if pri pri -1000))))
  5754. (defun org-agenda-show-tags ()
  5755. "Show the tags applicable to the current item."
  5756. (interactive)
  5757. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  5758. (if tags
  5759. (message "Tags are :%s:" (mapconcat 'identity tags ":"))
  5760. (message "No tags associated with this line"))))
  5761. (defun org-agenda-goto (&optional highlight)
  5762. "Go to the Org-mode file which contains the item at point."
  5763. (interactive)
  5764. (let* ((marker (or (get-text-property (point) 'org-marker)
  5765. (org-agenda-error)))
  5766. (buffer (marker-buffer marker))
  5767. (pos (marker-position marker)))
  5768. (switch-to-buffer-other-window buffer)
  5769. (widen)
  5770. (goto-char pos)
  5771. (when (eq major-mode 'org-mode)
  5772. (org-show-hidden-entry)
  5773. (save-excursion
  5774. (and (outline-next-heading)
  5775. (org-flag-heading nil)))) ; show the next heading
  5776. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  5777. (defun org-agenda-switch-to ()
  5778. "Go to the Org-mode file which contains the item at point."
  5779. (interactive)
  5780. (let* ((marker (or (get-text-property (point) 'org-marker)
  5781. (org-agenda-error)))
  5782. (buffer (marker-buffer marker))
  5783. (pos (marker-position marker)))
  5784. (switch-to-buffer buffer)
  5785. (delete-other-windows)
  5786. (widen)
  5787. (goto-char pos)
  5788. (when (eq major-mode 'org-mode)
  5789. (org-show-hidden-entry)
  5790. (save-excursion
  5791. (and (outline-next-heading)
  5792. (org-flag-heading nil)))))) ; show the next heading
  5793. (defun org-agenda-goto-mouse (ev)
  5794. "Go to the Org-mode file which contains the item at the mouse click."
  5795. (interactive "e")
  5796. (mouse-set-point ev)
  5797. (org-agenda-goto))
  5798. (defun org-agenda-show ()
  5799. "Display the Org-mode file which contains the item at point."
  5800. (interactive)
  5801. (let ((win (selected-window)))
  5802. (org-agenda-goto t)
  5803. (select-window win)))
  5804. (defun org-agenda-recenter (arg)
  5805. "Display the Org-mode file which contains the item at point and recenter."
  5806. (interactive "P")
  5807. (let ((win (selected-window)))
  5808. (org-agenda-goto t)
  5809. (recenter arg)
  5810. (select-window win)))
  5811. (defun org-agenda-show-mouse (ev)
  5812. "Display the Org-mode file which contains the item at the mouse click."
  5813. (interactive "e")
  5814. (mouse-set-point ev)
  5815. (org-agenda-show))
  5816. (defun org-agenda-check-no-diary ()
  5817. "Check if the entry is a diary link and abort if yes."
  5818. (if (get-text-property (point) 'org-agenda-diary-link)
  5819. (org-agenda-error)))
  5820. (defun org-agenda-error ()
  5821. (error "Command not allowed in this line"))
  5822. (defvar org-last-heading-marker (make-marker)
  5823. "Marker pointing to the headline that last changed its TODO state
  5824. by a remote command from the agenda.")
  5825. (defun org-agenda-todo (&optional arg)
  5826. "Cycle TODO state of line at point, also in Org-mode file.
  5827. This changes the line at point, all other lines in the agenda referring to
  5828. the same tree node, and the headline of the tree node in the Org-mode file."
  5829. (interactive "P")
  5830. (org-agenda-check-no-diary)
  5831. (let* ((col (current-column))
  5832. (marker (or (get-text-property (point) 'org-marker)
  5833. (org-agenda-error)))
  5834. (buffer (marker-buffer marker))
  5835. (pos (marker-position marker))
  5836. (hdmarker (get-text-property (point) 'org-hd-marker))
  5837. (buffer-read-only nil)
  5838. newhead)
  5839. (with-current-buffer buffer
  5840. (widen)
  5841. (goto-char pos)
  5842. (org-show-hidden-entry)
  5843. (save-excursion
  5844. (and (outline-next-heading)
  5845. (org-flag-heading nil))) ; show the next heading
  5846. (org-todo arg)
  5847. (forward-char 1)
  5848. (setq newhead (org-get-heading))
  5849. (save-excursion
  5850. (org-back-to-heading)
  5851. (move-marker org-last-heading-marker (point))))
  5852. (beginning-of-line 1)
  5853. (save-excursion
  5854. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  5855. (move-to-column col)))
  5856. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  5857. "Change all lines in the agenda buffer which match HDMARKER.
  5858. The new content of the line will be NEWHEAD (as modified by
  5859. `org-format-agenda-item'). HDMARKER is checked with
  5860. `equal' against all `org-hd-marker' text properties in the file.
  5861. If FIXFACE is non-nil, the face of each item is modified acording to
  5862. the new TODO state."
  5863. (let* (props m pl undone-face done-face finish new dotime cat tags)
  5864. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  5865. (save-excursion
  5866. (goto-char (point-max))
  5867. (beginning-of-line 1)
  5868. (while (not finish)
  5869. (setq finish (bobp))
  5870. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  5871. (equal m hdmarker))
  5872. (setq props (text-properties-at (point))
  5873. dotime (get-text-property (point) 'dotime)
  5874. cat (get-text-property (point) 'category)
  5875. tags (get-text-property (point) 'tags)
  5876. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  5877. pl (get-text-property (point) 'prefix-length)
  5878. undone-face (get-text-property (point) 'undone-face)
  5879. done-face (get-text-property (point) 'done-face))
  5880. (move-to-column pl)
  5881. (if (looking-at ".*")
  5882. (progn
  5883. (replace-match new t t)
  5884. (beginning-of-line 1)
  5885. (add-text-properties (point-at-bol) (point-at-eol) props)
  5886. (if fixface
  5887. (add-text-properties
  5888. (point-at-bol) (point-at-eol)
  5889. (list 'face
  5890. (if org-last-todo-state-is-todo
  5891. undone-face done-face))))
  5892. (beginning-of-line 1))
  5893. (error "Line update did not work")))
  5894. (beginning-of-line 0)))))
  5895. (defun org-agenda-priority-up ()
  5896. "Increase the priority of line at point, also in Org-mode file."
  5897. (interactive)
  5898. (org-agenda-priority 'up))
  5899. (defun org-agenda-priority-down ()
  5900. "Decrease the priority of line at point, also in Org-mode file."
  5901. (interactive)
  5902. (org-agenda-priority 'down))
  5903. (defun org-agenda-priority (&optional force-direction)
  5904. "Set the priority of line at point, also in Org-mode file.
  5905. This changes the line at point, all other lines in the agenda referring to
  5906. the same tree node, and the headline of the tree node in the Org-mode file."
  5907. (interactive)
  5908. (org-agenda-check-no-diary)
  5909. (let* ((marker (or (get-text-property (point) 'org-marker)
  5910. (org-agenda-error)))
  5911. (buffer (marker-buffer marker))
  5912. (pos (marker-position marker))
  5913. (hdmarker (get-text-property (point) 'org-hd-marker))
  5914. (buffer-read-only nil)
  5915. newhead)
  5916. (with-current-buffer buffer
  5917. (widen)
  5918. (goto-char pos)
  5919. (org-show-hidden-entry)
  5920. (save-excursion
  5921. (and (outline-next-heading)
  5922. (org-flag-heading nil))) ; show the next heading
  5923. (funcall 'org-priority force-direction)
  5924. (end-of-line 1)
  5925. (setq newhead (org-get-heading)))
  5926. (org-agenda-change-all-lines newhead hdmarker)
  5927. (beginning-of-line 1)))
  5928. (defun org-get-tags-at (&optional pos)
  5929. "Get a list of all headline targs applicable at POS.
  5930. POS defaults to point. If tags are inherited, the list contains
  5931. the targets in the same sequence as the headlines appear, i.e.
  5932. the tags of the current headline come last."
  5933. (interactive)
  5934. (let (tags)
  5935. (save-excursion
  5936. (goto-char (or pos (point)))
  5937. (save-match-data
  5938. (org-back-to-heading t)
  5939. (condition-case nil
  5940. (while t
  5941. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  5942. (setq tags (append (org-split-string (match-string 1) ":") tags)))
  5943. (or org-use-tag-inheritance (error ""))
  5944. (org-up-heading-all 1))
  5945. (error nil))))
  5946. (message "%s" tags)
  5947. tags))
  5948. (defun org-agenda-set-tags ()
  5949. "Set tags for the current headline."
  5950. (interactive)
  5951. (org-agenda-check-no-diary)
  5952. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  5953. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  5954. (org-agenda-error)))
  5955. (buffer (marker-buffer hdmarker))
  5956. (pos (marker-position hdmarker))
  5957. (buffer-read-only nil)
  5958. newhead)
  5959. (with-current-buffer buffer
  5960. (widen)
  5961. (goto-char pos)
  5962. (org-show-hidden-entry)
  5963. (save-excursion
  5964. (and (outline-next-heading)
  5965. (org-flag-heading nil))) ; show the next heading
  5966. (call-interactively 'org-set-tags)
  5967. (end-of-line 1)
  5968. (setq newhead (org-get-heading)))
  5969. (org-agenda-change-all-lines newhead hdmarker)
  5970. (beginning-of-line 1)))
  5971. (defun org-agenda-date-later (arg &optional what)
  5972. "Change the date of this item to one day later."
  5973. (interactive "p")
  5974. (org-agenda-check-type t 'agenda 'timeline)
  5975. (org-agenda-check-no-diary)
  5976. (let* ((marker (or (get-text-property (point) 'org-marker)
  5977. (org-agenda-error)))
  5978. (buffer (marker-buffer marker))
  5979. (pos (marker-position marker)))
  5980. (with-current-buffer buffer
  5981. (widen)
  5982. (goto-char pos)
  5983. (if (not (org-at-timestamp-p))
  5984. (error "Cannot find time stamp"))
  5985. (org-timestamp-change arg (or what 'day))
  5986. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  5987. (defun org-agenda-date-earlier (arg &optional what)
  5988. "Change the date of this item to one day earlier."
  5989. (interactive "p")
  5990. (org-agenda-date-later (- arg) what))
  5991. (defun org-agenda-date-prompt (arg)
  5992. "Change the date of this item. Date is prompted for, with default today.
  5993. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  5994. be used to request time specification in the time stamp."
  5995. (interactive "P")
  5996. (org-agenda-check-type t 'agenda 'timeline)
  5997. (org-agenda-check-no-diary)
  5998. (let* ((marker (or (get-text-property (point) 'org-marker)
  5999. (org-agenda-error)))
  6000. (buffer (marker-buffer marker))
  6001. (pos (marker-position marker)))
  6002. (with-current-buffer buffer
  6003. (widen)
  6004. (goto-char pos)
  6005. (if (not (org-at-timestamp-p))
  6006. (error "Cannot find time stamp"))
  6007. (org-time-stamp arg)
  6008. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6009. (defun org-get-heading ()
  6010. "Return the heading of the current entry, without the stars."
  6011. (save-excursion
  6012. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  6013. (if (and (re-search-backward "[\r\n]\\*" nil t)
  6014. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  6015. (match-string 1)
  6016. "")))
  6017. (defun org-agenda-diary-entry ()
  6018. "Make a diary entry, like the `i' command from the calendar.
  6019. All the standard commands work: block, weekly etc."
  6020. (interactive)
  6021. (org-agenda-check-type t 'agenda 'timeline)
  6022. (require 'diary-lib)
  6023. (let* ((char (progn
  6024. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  6025. (read-char-exclusive)))
  6026. (cmd (cdr (assoc char
  6027. '((?d . insert-diary-entry)
  6028. (?w . insert-weekly-diary-entry)
  6029. (?m . insert-monthly-diary-entry)
  6030. (?y . insert-yearly-diary-entry)
  6031. (?a . insert-anniversary-diary-entry)
  6032. (?b . insert-block-diary-entry)
  6033. (?c . insert-cyclic-diary-entry)))))
  6034. (oldf (symbol-function 'calendar-cursor-to-date))
  6035. (point (point))
  6036. (mark (or (mark t) (point))))
  6037. (unless cmd
  6038. (error "No command associated with <%c>" char))
  6039. (unless (and (get-text-property point 'day)
  6040. (or (not (equal ?b char))
  6041. (get-text-property mark 'day)))
  6042. (error "Don't know which date to use for diary entry"))
  6043. ;; We implement this by hacking the `calendar-cursor-to-date' function
  6044. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  6045. (let ((calendar-mark-ring
  6046. (list (calendar-gregorian-from-absolute
  6047. (or (get-text-property mark 'day)
  6048. (get-text-property point 'day))))))
  6049. (unwind-protect
  6050. (progn
  6051. (fset 'calendar-cursor-to-date
  6052. (lambda (&optional error)
  6053. (calendar-gregorian-from-absolute
  6054. (get-text-property point 'day))))
  6055. (call-interactively cmd))
  6056. (fset 'calendar-cursor-to-date oldf)))))
  6057. (defun org-agenda-execute-calendar-command (cmd)
  6058. "Execute a calendar command from the agenda, with the date associated to
  6059. the cursor position."
  6060. (org-agenda-check-type t 'agenda 'timeline)
  6061. (require 'diary-lib)
  6062. (unless (get-text-property (point) 'day)
  6063. (error "Don't know which date to use for calendar command"))
  6064. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  6065. (point (point))
  6066. (date (calendar-gregorian-from-absolute
  6067. (get-text-property point 'day)))
  6068. (displayed-day (extract-calendar-day date))
  6069. (displayed-month (extract-calendar-month date))
  6070. (displayed-year (extract-calendar-year date)))
  6071. (unwind-protect
  6072. (progn
  6073. (fset 'calendar-cursor-to-date
  6074. (lambda (&optional error)
  6075. (calendar-gregorian-from-absolute
  6076. (get-text-property point 'day))))
  6077. (call-interactively cmd))
  6078. (fset 'calendar-cursor-to-date oldf))))
  6079. (defun org-agenda-phases-of-moon ()
  6080. "Display the phases of the moon for the 3 months around the cursor date."
  6081. (interactive)
  6082. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  6083. (defun org-agenda-holidays ()
  6084. "Display the holidays for the 3 months around the cursor date."
  6085. (interactive)
  6086. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  6087. (defun org-agenda-sunrise-sunset (arg)
  6088. "Display sunrise and sunset for the cursor date.
  6089. Latitude and longitude can be specified with the variables
  6090. `calendar-latitude' and `calendar-longitude'. When called with prefix
  6091. argument, latitude and longitude will be prompted for."
  6092. (interactive "P")
  6093. (let ((calendar-longitude (if arg nil calendar-longitude))
  6094. (calendar-latitude (if arg nil calendar-latitude))
  6095. (calendar-location-name
  6096. (if arg "the given coordinates" calendar-location-name)))
  6097. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  6098. (defun org-agenda-goto-calendar ()
  6099. "Open the Emacs calendar with the date at the cursor."
  6100. (interactive)
  6101. (org-agenda-check-type t 'agenda 'timeline)
  6102. (let* ((day (or (get-text-property (point) 'day)
  6103. (error "Don't know which date to open in calendar")))
  6104. (date (calendar-gregorian-from-absolute day))
  6105. (calendar-move-hook nil)
  6106. (view-diary-entries-initially nil))
  6107. (calendar)
  6108. (calendar-goto-date date)))
  6109. (defun org-calendar-goto-agenda ()
  6110. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  6111. This is a command that has to be installed in `calendar-mode-map'."
  6112. (interactive)
  6113. (org-agenda-list nil (calendar-absolute-from-gregorian
  6114. (calendar-cursor-to-date))
  6115. nil t))
  6116. (defun org-agenda-convert-date ()
  6117. (interactive)
  6118. (org-agenda-check-type t 'agenda 'timeline)
  6119. (let ((day (get-text-property (point) 'day))
  6120. date s)
  6121. (unless day
  6122. (error "Don't know which date to convert"))
  6123. (setq date (calendar-gregorian-from-absolute day))
  6124. (setq s (concat
  6125. "Gregorian: " (calendar-date-string date) "\n"
  6126. "ISO: " (calendar-iso-date-string date) "\n"
  6127. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  6128. "Julian: " (calendar-julian-date-string date) "\n"
  6129. "Astron. JD: " (calendar-astro-date-string date)
  6130. " (Julian date number at noon UTC)\n"
  6131. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  6132. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  6133. "French: " (calendar-french-date-string date) "\n"
  6134. "Mayan: " (calendar-mayan-date-string date) "\n"
  6135. "Coptic: " (calendar-coptic-date-string date) "\n"
  6136. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  6137. "Persian: " (calendar-persian-date-string date) "\n"
  6138. "Chinese: " (calendar-chinese-date-string date) "\n"))
  6139. (with-output-to-temp-buffer "*Dates*"
  6140. (princ s))
  6141. (if (fboundp 'fit-window-to-buffer)
  6142. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  6143. ;;; Tags
  6144. (defun org-scan-tags (action matcher &optional todo-only)
  6145. "Scan headline tags with inheritance and produce output ACTION.
  6146. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  6147. evaluated, testing if a given set of tags qualifies a headline for
  6148. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  6149. are included in the output."
  6150. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  6151. (mapconcat 'regexp-quote
  6152. (nreverse (cdr (reverse org-todo-keywords)))
  6153. "\\|")
  6154. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  6155. (props (list 'face nil
  6156. 'done-face 'org-done
  6157. 'undone-face nil
  6158. 'mouse-face 'highlight
  6159. 'keymap org-agenda-keymap
  6160. 'help-echo
  6161. (format "mouse-2 or RET jump to org file %s"
  6162. (abbreviate-file-name buffer-file-name))))
  6163. lspos
  6164. tags tags-list tags-alist (llast 0) rtn level category i txt
  6165. todo marker)
  6166. (save-excursion
  6167. (goto-char (point-min))
  6168. (when (eq action 'sparse-tree) (hide-sublevels 1))
  6169. (while (re-search-forward re nil t)
  6170. (setq todo (if (match-end 1) (match-string 2))
  6171. tags (if (match-end 4) (match-string 4)))
  6172. (goto-char (setq lspos (1+ (match-beginning 0))))
  6173. (setq level (funcall outline-level)
  6174. category (org-get-category))
  6175. (setq i llast llast level)
  6176. ;; remove tag lists from same and sublevels
  6177. (while (>= i level)
  6178. (when (setq entry (assoc i tags-alist))
  6179. (setq tags-alist (delete entry tags-alist)))
  6180. (setq i (1- i)))
  6181. ;; add the nex tags
  6182. (when tags
  6183. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  6184. tags-alist
  6185. (cons (cons level tags) tags-alist)))
  6186. ;; compile tags for current headline
  6187. (setq tags-list
  6188. (if org-use-tag-inheritance
  6189. (apply 'append (mapcar 'cdr tags-alist))
  6190. tags))
  6191. (when (and (or (not todo-only) todo)
  6192. (eval matcher))
  6193. ;; list this headline
  6194. (if (eq action 'sparse-tree)
  6195. (progn
  6196. (org-show-hierarchy-above))
  6197. (setq txt (org-format-agenda-item
  6198. ""
  6199. (concat
  6200. (if org-tags-match-list-sublevels
  6201. (make-string (1- level) ?.) "")
  6202. (org-get-heading))
  6203. category tags-list))
  6204. (goto-char lspos)
  6205. (setq marker (org-agenda-new-marker))
  6206. (add-text-properties
  6207. 0 (length txt)
  6208. (append (list 'org-marker marker 'org-hd-marker marker
  6209. 'category category)
  6210. props)
  6211. txt)
  6212. (push txt rtn))
  6213. ;; if we are to skip sublevels, jump to end of subtree
  6214. (point)
  6215. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  6216. (nreverse rtn)))
  6217. (defun org-tags-sparse-tree (&optional arg match)
  6218. "Create a sparse tree according to tags search string MATCH.
  6219. MATCH can contain positive and negative selection of tags, like
  6220. \"+WORK+URGENT-WITHBOSS\"."
  6221. (interactive "P")
  6222. (let ((org-show-following-heading nil)
  6223. (org-show-hierarchy-above nil))
  6224. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  6225. (defun org-make-tags-matcher (match)
  6226. "Create the TAGS matcher form for the tags-selecting string MATCH."
  6227. (unless match
  6228. ;; Get a new match request, with completion
  6229. (setq org-last-tags-completion-table
  6230. (or (org-get-buffer-tags)
  6231. org-last-tags-completion-table))
  6232. (setq match (completing-read
  6233. "Tags: " 'org-tags-completion-function nil nil nil
  6234. 'org-tags-history)))
  6235. ;; parse the string and create a lisp form
  6236. (let ((match0 match) minus tag mm matcher orterms term orlist)
  6237. (setq orterms (org-split-string match "|"))
  6238. (while (setq term (pop orterms))
  6239. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  6240. (setq minus (and (match-end 1)
  6241. (equal (match-string 1 term) "-"))
  6242. tag (match-string 2 term)
  6243. term (substring term (match-end 0))
  6244. mm (list 'member (downcase tag) 'tags-list)
  6245. mm (if minus (list 'not mm) mm))
  6246. (push mm matcher))
  6247. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  6248. orlist)
  6249. (setq matcher nil))
  6250. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  6251. ;; Return the string and lisp forms of the matcher
  6252. (cons match0 matcher)))
  6253. ;;;###autoload
  6254. (defun org-tags-view (&optional todo-only match keep-modes)
  6255. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  6256. The prefix arg TODO-ONLY limits the search to TODO entries."
  6257. (interactive "P")
  6258. (org-agenda-maybe-reset-markers 'force)
  6259. (org-compile-prefix-format org-agenda-prefix-format)
  6260. (let* ((org-agenda-keep-modes keep-modes)
  6261. (org-tags-match-list-sublevels
  6262. (if todo-only t org-tags-match-list-sublevels))
  6263. (win (selected-window))
  6264. (completion-ignore-case t)
  6265. rtn rtnall files file pos matcher
  6266. buffer)
  6267. (setq matcher (org-make-tags-matcher match)
  6268. match (car matcher) matcher (cdr matcher))
  6269. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  6270. (progn
  6271. (delete-other-windows)
  6272. (switch-to-buffer-other-window
  6273. (get-buffer-create org-agenda-buffer-name))))
  6274. (setq buffer-read-only nil)
  6275. (erase-buffer)
  6276. (org-agenda-mode) (setq buffer-read-only nil)
  6277. (set (make-local-variable 'org-agenda-type) 'tags)
  6278. (set (make-local-variable 'org-agenda-redo-command)
  6279. (list 'org-tags-view (list 'quote todo-only)
  6280. (list 'if 'current-prefix-arg nil match) t))
  6281. (setq files (org-agenda-files)
  6282. rtnall nil)
  6283. (while (setq file (pop files))
  6284. (catch 'nextfile
  6285. (org-check-agenda-file file)
  6286. (setq buffer (if (file-exists-p file)
  6287. (org-get-agenda-file-buffer file)
  6288. (error "No such file %s" file)))
  6289. (if (not buffer)
  6290. ;; If file does not exist, merror message to agenda
  6291. (setq rtn (list
  6292. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  6293. rtnall (append rtnall rtn))
  6294. (with-current-buffer buffer
  6295. (unless (eq major-mode 'org-mode)
  6296. (error "Agenda file %s is not in `org-mode'" file))
  6297. (save-excursion
  6298. (save-restriction
  6299. (if org-respect-restriction
  6300. (if (org-region-active-p)
  6301. ;; Respect a region to restrict search
  6302. (narrow-to-region (region-beginning) (region-end)))
  6303. ;; If we work for the calendar or many files,
  6304. ;; get rid of any restriction
  6305. (widen))
  6306. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  6307. (setq rtnall (append rtnall rtn))))))))
  6308. (insert "Headlines with TAGS match: ")
  6309. (add-text-properties (point-min) (1- (point))
  6310. (list 'face 'org-link))
  6311. (setq pos (point))
  6312. (insert match "\n")
  6313. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6314. (setq pos (point))
  6315. (insert "Press `C-u r' to search again with new search string\n")
  6316. (add-text-properties pos (1- (point)) (list 'face 'org-link))
  6317. (when rtnall
  6318. (insert (mapconcat 'identity rtnall "\n")))
  6319. (goto-char (point-min))
  6320. (setq buffer-read-only t)
  6321. (org-fit-agenda-window)
  6322. (if (not org-select-agenda-window) (select-window win))))
  6323. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  6324. (defun org-set-tags (&optional arg just-align)
  6325. "Set the tags for the current headline.
  6326. With prefix ARG, realign all tags in headings in the current buffer."
  6327. (interactive)
  6328. (let* (;(inherit (org-get-inherited-tags))
  6329. (re (concat "^" outline-regexp))
  6330. (col (current-column))
  6331. (current (org-get-tags))
  6332. tags hd empty invis)
  6333. (if arg
  6334. (save-excursion
  6335. (goto-char (point-min))
  6336. (while (re-search-forward re nil t)
  6337. (org-set-tags nil t))
  6338. (message "All tags realigned to column %d" org-tags-column))
  6339. (if just-align
  6340. (setq tags current)
  6341. (setq org-last-tags-completion-table
  6342. (or (org-get-buffer-tags)
  6343. org-last-tags-completion-table))
  6344. (setq tags
  6345. (let ((org-add-colon-after-tag-completion t))
  6346. (completing-read "Tags: " 'org-tags-completion-function
  6347. nil nil current 'org-tags-history)))
  6348. (while (string-match "[-+&]+" tags)
  6349. (setq tags (replace-match ":" t t tags))))
  6350. ;; FIXME: still optimize this by not checking when JUST-ALIGN?
  6351. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  6352. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  6353. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  6354. (if (equal current "")
  6355. (progn
  6356. (end-of-line 1)
  6357. (or empty (insert-before-markers " ")))
  6358. (beginning-of-line 1)
  6359. (setq invis (org-invisible-p))
  6360. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  6361. (setq hd (match-string 1))
  6362. (delete-region (match-beginning 0) (match-end 0))
  6363. (insert-before-markers (org-trim hd) (if empty "" " ")))
  6364. ;; FIXME: What happens when adding a new tag??? Seems OK!!!
  6365. (unless (equal tags "")
  6366. (move-to-column (max (current-column)
  6367. (if (> org-tags-column 0)
  6368. org-tags-column
  6369. (- (- org-tags-column) (length tags))))
  6370. t)
  6371. (insert-before-markers tags)
  6372. (if (and (not invis) (org-invisible-p))
  6373. (outline-flag-region (point-at-bol) (point) nil)))
  6374. (move-to-column col))))
  6375. (defun org-tags-completion-function (string predicate &optional flag)
  6376. (let (s1 s2 rtn (ctable org-last-tags-completion-table))
  6377. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  6378. (setq s1 (match-string 1 string)
  6379. s2 (match-string 2 string))
  6380. (setq s1 "" s2 string))
  6381. (cond
  6382. ((eq flag nil)
  6383. ;; try completion
  6384. (setq rtn (try-completion s2 ctable))
  6385. (if (stringp rtn)
  6386. (concat s1 s2 (substring rtn (length s2))
  6387. (if (and org-add-colon-after-tag-completion
  6388. (assoc rtn ctable))
  6389. ":" "")))
  6390. )
  6391. ((eq flag t)
  6392. ;; all-completions
  6393. (all-completions s2 ctable)
  6394. )
  6395. ((eq flag 'lambda)
  6396. ;; exact match?
  6397. (assoc s2 ctable)))
  6398. ))
  6399. (defun org-get-tags ()
  6400. "Get the TAGS string in the current headline."
  6401. (unless (org-on-heading-p)
  6402. (error "Not on a heading"))
  6403. (save-excursion
  6404. (beginning-of-line 1)
  6405. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  6406. (match-string 1)
  6407. "")))
  6408. (defun org-get-buffer-tags ()
  6409. "Get a table of all tags used in the buffer, for completion."
  6410. (let (tags)
  6411. (save-excursion
  6412. (goto-char (point-min))
  6413. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  6414. (mapc (lambda (x) (add-to-list 'tags x))
  6415. (org-split-string (match-string 1) ":"))))
  6416. (mapcar 'list tags)))
  6417. ;;; Link Stuff
  6418. (defun org-find-file-at-mouse (ev)
  6419. "Open file link or URL at mouse."
  6420. (interactive "e")
  6421. (mouse-set-point ev)
  6422. (org-open-at-point 'in-emacs))
  6423. (defun org-open-at-mouse (ev)
  6424. "Open file link or URL at mouse."
  6425. (interactive "e")
  6426. (mouse-set-point ev)
  6427. (org-open-at-point))
  6428. (defun org-open-at-point (&optional in-emacs)
  6429. "Open link at or after point.
  6430. If there is no link at point, this function will search forward up to
  6431. the end of the current subtree.
  6432. Normally, files will be opened by an appropriate application. If the
  6433. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6434. (interactive "P")
  6435. (org-remove-occur-highlights nil nil t)
  6436. (if (org-at-timestamp-p)
  6437. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6438. (substring (match-string 1) 0 10)))
  6439. 1)
  6440. (let (type path link line search (pos (point)))
  6441. (catch 'match
  6442. (save-excursion
  6443. (skip-chars-forward "^]\n\r")
  6444. (when (and (re-search-backward "\\[\\[" nil t)
  6445. (looking-at org-bracket-link-regexp)
  6446. (<= (match-beginning 0) pos)
  6447. (>= (match-end 0) pos))
  6448. (setq link (org-link-unescape (match-string 1)))
  6449. (while (string-match " *\n *" link)
  6450. (setq link (replace-match " " t t link)))
  6451. (if (string-match org-link-regexp link)
  6452. (setq type (match-string 1 link)
  6453. path (match-string 2 link))
  6454. (setq type "thisfile"
  6455. path link))
  6456. (throw 'match t)))
  6457. (when (get-text-property (point) 'org-linked-text)
  6458. (setq type "thisfile"
  6459. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6460. (1+ (point)) (point))
  6461. path (buffer-substring
  6462. (previous-single-property-change pos 'org-linked-text)
  6463. (next-single-property-change pos 'org-linked-text)))
  6464. (throw 'match t))
  6465. (save-excursion
  6466. (skip-chars-backward
  6467. (concat (if org-allow-space-in-links "^" "^ ")
  6468. org-non-link-chars))
  6469. (when (or (looking-at org-link-regexp)
  6470. (and (re-search-forward org-link-regexp (point-at-eol) t)
  6471. (<= (match-beginning 0) pos)
  6472. (>= (match-end 0) pos)))
  6473. (setq type (match-string 1)
  6474. path (match-string 2))
  6475. (throw 'match t)))
  6476. (save-excursion
  6477. (skip-chars-backward "^ \t\n\r")
  6478. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  6479. (setq type "tags"
  6480. path (match-string 1))
  6481. (while (string-match ":" path)
  6482. (setq path (replace-match "+" t t path)))
  6483. (throw 'match t)))
  6484. (save-excursion
  6485. (skip-chars-backward "a-zA-Z_")
  6486. (when (and org-activate-camels
  6487. (looking-at org-camel-regexp))
  6488. (setq type "camel" path (match-string 0))
  6489. (if (equal (char-before) ?*)
  6490. (setq path (concat "*" path))))
  6491. (throw 'match t))
  6492. (save-excursion
  6493. ;; FIXME: This does not find bracket links, and maybe should be
  6494. ;; turned off totally, as the results are not very predictable.
  6495. (when (re-search-forward
  6496. org-link-regexp
  6497. (save-excursion
  6498. (condition-case nil
  6499. (progn (outline-end-of-subtree) (max pos (point)))
  6500. (error (end-of-line 1) (point))))
  6501. t)
  6502. (setq type (match-string 1)
  6503. path (match-string 2)))))
  6504. (unless path
  6505. (error "No link found"))
  6506. ;; Remove any trailing spaces in path
  6507. (if (string-match " +\\'" path)
  6508. (setq path (replace-match "" t t path)))
  6509. (cond
  6510. ((member type '("http" "https" "ftp" "mailto" "news"))
  6511. (browse-url (concat type ":" path)))
  6512. ((string= type "tags")
  6513. (org-tags-view in-emacs path))
  6514. ((or (string= type "camel")
  6515. (string= type "thisfile"))
  6516. (org-mark-ring-push)
  6517. (org-link-search
  6518. path
  6519. (cond ((equal in-emacs '(4)) 'occur)
  6520. ((equal in-emacs '(16)) 'org-occur)
  6521. (t nil))))
  6522. ((string= type "file")
  6523. (if (string-match "::?\\([0-9]+\\)\\'" path) ;; second : optional
  6524. (setq line (string-to-number (match-string 1 path))
  6525. path (substring path 0 (match-beginning 0)))
  6526. (if (string-match "::\\(.+\\)\\'" path)
  6527. (setq search (match-string 1 path)
  6528. path (substring path 0 (match-beginning 0)))))
  6529. (org-open-file path in-emacs line search))
  6530. ((string= type "news")
  6531. (org-follow-gnus-link path))
  6532. ((string= type "bbdb")
  6533. (org-follow-bbdb-link path))
  6534. ((string= type "gnus")
  6535. (let (group article)
  6536. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6537. (error "Error in Gnus link"))
  6538. (setq group (match-string 1 path)
  6539. article (match-string 3 path))
  6540. (org-follow-gnus-link group article)))
  6541. ((string= type "vm")
  6542. (let (folder article)
  6543. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6544. (error "Error in VM link"))
  6545. (setq folder (match-string 1 path)
  6546. article (match-string 3 path))
  6547. ;; in-emacs is the prefix arg, will be interpreted as read-only
  6548. (org-follow-vm-link folder article in-emacs)))
  6549. ((string= type "wl")
  6550. (let (folder article)
  6551. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6552. (error "Error in Wanderlust link"))
  6553. (setq folder (match-string 1 path)
  6554. article (match-string 3 path))
  6555. (org-follow-wl-link folder article)))
  6556. ((string= type "mhe")
  6557. (let (folder article)
  6558. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6559. (error "Error in MHE link"))
  6560. (setq folder (match-string 1 path)
  6561. article (match-string 3 path))
  6562. (org-follow-mhe-link folder article)))
  6563. ((string= type "rmail")
  6564. (let (folder article)
  6565. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  6566. (error "Error in RMAIL link"))
  6567. (setq folder (match-string 1 path)
  6568. article (match-string 3 path))
  6569. (org-follow-rmail-link folder article)))
  6570. ((string= type "shell")
  6571. (let ((cmd path))
  6572. (while (string-match "@{" cmd)
  6573. (setq cmd (replace-match "<" t t cmd)))
  6574. (while (string-match "@}" cmd)
  6575. (setq cmd (replace-match ">" t t cmd)))
  6576. (if (or (not org-confirm-shell-links)
  6577. (yes-or-no-p (format "Execute \"%s\" in the shell? " cmd)))
  6578. (shell-command cmd)
  6579. (error "Abort"))))
  6580. (t
  6581. (browse-url-at-point))))))
  6582. (defun org-link-search (s &optional type)
  6583. "Search for a link search option.
  6584. When S is a CamelCaseWord, search for a target, or for a sentence containing
  6585. the words. If S is surrounded by forward slashes, it is interpreted as a
  6586. regular expression. In org-mode files, this will create an `org-occur'
  6587. sparse tree. In ordinary files, `occur' will be used to list matches.
  6588. If the current buffer is in `dired-mode', grep will be used to search
  6589. in all files."
  6590. (let ((case-fold-search t)
  6591. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6592. (pos (point))
  6593. (pre "") (post "")
  6594. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  6595. (cond ((save-excursion
  6596. (goto-char (point-min))
  6597. (and
  6598. (re-search-forward
  6599. (concat "<<" (regexp-quote s0) ">>") nil t)
  6600. (setq pos (match-beginning 0))))
  6601. ;; There is an exact target for this
  6602. (goto-char pos))
  6603. ((string-match "^/\\(.*\\)/$" s)
  6604. ;; A regular expression
  6605. (cond
  6606. ((eq major-mode 'org-mode)
  6607. (org-occur (match-string 1 s)))
  6608. ;;((eq major-mode 'dired-mode)
  6609. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6610. (t (org-do-occur (match-string 1 s)))))
  6611. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  6612. t)
  6613. ;; A camel or a normal search string
  6614. (when (equal (string-to-char s) ?*)
  6615. ;; Anchor on headlines, post may include tags.
  6616. (setq pre "^\\*+[ \t]*\\(\\sw+\\)?[ \t]*"
  6617. post "[ \t]*\\([ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  6618. s (substring s 1)))
  6619. (remove-text-properties
  6620. 0 (length s)
  6621. '(face nil mouse-face nil keymap nil fontified nil) s)
  6622. ;; Make a series of regular expressions to find a match
  6623. (setq words
  6624. (if camel
  6625. (org-camel-to-words s)
  6626. (org-split-string s "[ \n\r\t]+"))
  6627. re0 (concat "<<" (regexp-quote s0) ">>")
  6628. re2 (concat "\\<" (mapconcat 'downcase words "[ \t]+") "\\>")
  6629. re2a (concat "\\<" (mapconcat 'downcase words "[ \t\r\n]+") "\\>")
  6630. re4 (concat "\\<" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\>")
  6631. re1 (concat pre re2 post)
  6632. re3 (concat pre re4 post)
  6633. re5 (concat pre ".*" re4)
  6634. re2 (concat pre re2)
  6635. re2a (concat pre re2a)
  6636. re4 (concat pre re4)
  6637. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6638. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6639. re5 "\\)"
  6640. ))
  6641. (cond
  6642. ((eq type 'org-occur) (org-occur reall))
  6643. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6644. (t (goto-char (point-min))
  6645. (if (or (org-search-not-link re0 nil t)
  6646. (org-search-not-link re1 nil t)
  6647. (org-search-not-link re2 nil t)
  6648. (org-search-not-link re2a nil t) ;; FIXME: Right place???
  6649. (org-search-not-link re3 nil t)
  6650. (org-search-not-link re4 nil t)
  6651. (org-search-not-link re5 nil t)
  6652. )
  6653. (goto-char (match-beginning 0))
  6654. (goto-char pos)
  6655. (error "No match")))))
  6656. (t
  6657. ;; Normal string-search
  6658. (goto-char (point-min))
  6659. (if (search-forward s nil t)
  6660. (goto-char (match-beginning 0))
  6661. (error "No match"))))
  6662. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  6663. (defun org-search-not-link (&rest args)
  6664. "Execute `re-search-forward', but only accept matches that are not a link."
  6665. (catch 'exit
  6666. (let ((pos (point)) p1)
  6667. (while (apply 're-search-forward args)
  6668. (setq p1 (point))
  6669. (if (not (save-match-data
  6670. (and (re-search-backward "\\[\\[" nil t)
  6671. (looking-at org-bracket-link-regexp)
  6672. (<= (match-beginning 0) p1)
  6673. (>= (match-end 0) p1))))
  6674. (progn (goto-char (match-end 0))
  6675. (throw 'exit (point)))
  6676. (goto-char (match-end 0)))))))
  6677. (defun org-do-occur (regexp &optional cleanup)
  6678. "Call the Emacs command `occur'.
  6679. If CLEANUP is non-nil, remove the printout of the regular expression
  6680. in the *Occur* buffer. This is useful if the regex is long and not useful
  6681. to read."
  6682. (occur regexp)
  6683. (when cleanup
  6684. (let ((cwin (selected-window)) win beg end)
  6685. (when (setq win (get-buffer-window "*Occur*"))
  6686. (select-window win))
  6687. (goto-char (point-min))
  6688. (when (re-search-forward "match[a-z]+" nil t)
  6689. (setq beg (match-end 0))
  6690. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6691. (setq end (1- (match-beginning 0)))))
  6692. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  6693. (goto-char (point-min))
  6694. (select-window cwin))))
  6695. (defvar org-mark-ring nil
  6696. "Mark ring for positions before jumps in Org-mode.")
  6697. (defvar org-mark-ring-last-goto nil
  6698. "Last position in the mark ring used to go back.")
  6699. ;; Fill and close the ring
  6700. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6701. (loop for i from 1 to org-mark-ring-length do
  6702. (push (make-marker) org-mark-ring))
  6703. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6704. org-mark-ring)
  6705. (defun org-mark-ring-push (&optional pos buffer)
  6706. "Put the current position or POS into the mark ring and rotate it."
  6707. (interactive)
  6708. (setq pos (or pos (point)))
  6709. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6710. (move-marker (car org-mark-ring)
  6711. (or pos (point))
  6712. (or buffer (current-buffer)))
  6713. (message
  6714. (substitute-command-keys
  6715. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6716. (defun org-mark-ring-goto (&optional n)
  6717. "Jump to the previous position in the mark ring.
  6718. With prefix arg N, jump back that many stored positions. When
  6719. called several times in succession, walk through the entire ring.
  6720. Org-mode commands jumping to a different position in the current file,
  6721. or to another Org-mode file, automatically push the old position
  6722. onto the ring."
  6723. (interactive "p")
  6724. (let (p m)
  6725. (if (eq last-command this-command)
  6726. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6727. (setq p org-mark-ring))
  6728. (setq org-mark-ring-last-goto p)
  6729. (setq m (car p))
  6730. (switch-to-buffer (marker-buffer m))
  6731. (goto-char m)
  6732. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  6733. (defun org-camel-to-words (s)
  6734. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  6735. (let ((case-fold-search nil)
  6736. words)
  6737. (while (string-match "[a-z][A-Z]" s)
  6738. (push (substring s 0 (1+ (match-beginning 0))) words)
  6739. (setq s (substring s (1+ (match-beginning 0)))))
  6740. (nreverse (cons s words))))
  6741. (defun org-remove-angle-brackets (s)
  6742. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6743. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6744. s)
  6745. (defun org-add-angle-brackets (s)
  6746. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6747. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6748. s)
  6749. (defun org-follow-bbdb-link (name)
  6750. "Follow a BBDB link to NAME."
  6751. (require 'bbdb)
  6752. (let ((inhibit-redisplay t)
  6753. (bbdb-electric-p nil))
  6754. (catch 'exit
  6755. ;; Exact match on name
  6756. (bbdb-name (concat "\\`" name "\\'") nil)
  6757. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6758. ;; Exact match on name
  6759. (bbdb-company (concat "\\`" name "\\'") nil)
  6760. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6761. ;; Partial match on name
  6762. (bbdb-name name nil)
  6763. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6764. ;; Partial match on company
  6765. (bbdb-company name nil)
  6766. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  6767. ;; General match including network address and notes
  6768. (bbdb name nil)
  6769. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  6770. (delete-window (get-buffer-window "*BBDB*"))
  6771. (error "No matching BBDB record")))))
  6772. (defun org-follow-gnus-link (&optional group article)
  6773. "Follow a Gnus link to GROUP and ARTICLE."
  6774. (require 'gnus)
  6775. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  6776. (if group (gnus-fetch-group group))
  6777. (if article
  6778. (or (gnus-summary-goto-article article nil 'force)
  6779. (if (fboundp 'gnus-summary-insert-cached-articles)
  6780. (progn
  6781. (gnus-summary-insert-cached-articles)
  6782. (gnus-summary-goto-article article nil 'force))
  6783. (message "Message could not be found.")))))
  6784. (defun org-follow-vm-link (&optional folder article readonly)
  6785. "Follow a VM link to FOLDER and ARTICLE."
  6786. (require 'vm)
  6787. (setq article (org-add-angle-brackets article))
  6788. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  6789. ;; ange-ftp or efs or tramp access
  6790. (let ((user (or (match-string 1 folder) (user-login-name)))
  6791. (host (match-string 2 folder))
  6792. (file (match-string 3 folder)))
  6793. (cond
  6794. ((featurep 'tramp)
  6795. ;; use tramp to access the file
  6796. (if (featurep 'xemacs)
  6797. (setq folder (format "[%s@%s]%s" user host file))
  6798. (setq folder (format "/%s@%s:%s" user host file))))
  6799. (t
  6800. ;; use ange-ftp or efs
  6801. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  6802. (setq folder (format "/%s@%s:%s" user host file))))))
  6803. (when folder
  6804. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  6805. (sit-for 0.1)
  6806. (when article
  6807. (vm-select-folder-buffer)
  6808. (widen)
  6809. (let ((case-fold-search t))
  6810. (goto-char (point-min))
  6811. (if (not (re-search-forward
  6812. (concat "^" "message-id: *" (regexp-quote article))))
  6813. (error "Could not find the specified message in this folder"))
  6814. (vm-isearch-update)
  6815. (vm-isearch-narrow)
  6816. (vm-beginning-of-message)
  6817. (vm-summarize)))))
  6818. (defun org-follow-wl-link (folder article)
  6819. "Follow a Wanderlust link to FOLDER and ARTICLE."
  6820. (setq article (org-add-angle-brackets article))
  6821. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  6822. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  6823. (wl-summary-redisplay))
  6824. (defun org-follow-rmail-link (folder article)
  6825. "Follow an RMAIL link to FOLDER and ARTICLE."
  6826. (setq article (org-add-angle-brackets article))
  6827. (let (message-number)
  6828. (save-excursion
  6829. (save-window-excursion
  6830. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  6831. (setq message-number
  6832. (save-restriction
  6833. (widen)
  6834. (goto-char (point-max))
  6835. (if (re-search-backward
  6836. (concat "^Message-ID:\\s-+" (regexp-quote
  6837. (or article "")))
  6838. nil t)
  6839. (rmail-what-message))))))
  6840. (if message-number
  6841. (progn
  6842. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  6843. (rmail-show-message message-number)
  6844. message-number)
  6845. (error "Message not found"))))
  6846. ;; mh-e integration based on planner-mode
  6847. (defun org-mhe-get-message-real-folder ()
  6848. "Return the name of the current message real folder, so if you use
  6849. sequences, it will now work."
  6850. (save-excursion
  6851. (let* ((folder
  6852. (if (equal major-mode 'mh-folder-mode)
  6853. mh-current-folder
  6854. ;; Refer to the show buffer
  6855. mh-show-folder-buffer))
  6856. (end-index
  6857. (if (boundp 'mh-index-folder)
  6858. (min (length mh-index-folder) (length folder))))
  6859. )
  6860. ;; a simple test on mh-index-data does not work, because
  6861. ;; mh-index-data is always nil in a show buffer.
  6862. (if (and (boundp 'mh-index-folder)
  6863. (string= mh-index-folder (substring folder 0 end-index)))
  6864. (if (equal major-mode 'mh-show-mode)
  6865. (save-window-excursion
  6866. (when (buffer-live-p (get-buffer folder))
  6867. (progn
  6868. (pop-to-buffer folder)
  6869. (org-mhe-get-message-folder-from-index)
  6870. )
  6871. ))
  6872. (org-mhe-get-message-folder-from-index)
  6873. )
  6874. folder
  6875. )
  6876. )))
  6877. (defun org-mhe-get-message-folder-from-index ()
  6878. "Returns the name of the message folder in a index folder buffer."
  6879. (save-excursion
  6880. (mh-index-previous-folder)
  6881. (re-search-forward "^\\(+.*\\)$" nil t)
  6882. (message (match-string 1))))
  6883. (defun org-mhe-get-message-folder ()
  6884. "Return the name of the current message folder. Be careful if you
  6885. use sequences."
  6886. (save-excursion
  6887. (if (equal major-mode 'mh-folder-mode)
  6888. mh-current-folder
  6889. ;; Refer to the show buffer
  6890. mh-show-folder-buffer)))
  6891. (defun org-mhe-get-message-num ()
  6892. "Return the number of the current message. Be careful if you
  6893. use sequences."
  6894. (save-excursion
  6895. (if (equal major-mode 'mh-folder-mode)
  6896. (mh-get-msg-num nil)
  6897. ;; Refer to the show buffer
  6898. (mh-show-buffer-message-number))))
  6899. (defun org-mhe-get-header (header)
  6900. "Return a header of the message in folder mode. This will create a
  6901. show buffer for the corresponding message. If you have a more clever
  6902. idea..."
  6903. (let* ((folder (org-mhe-get-message-folder))
  6904. (num (org-mhe-get-message-num))
  6905. (buffer (get-buffer-create (concat "show-" folder)))
  6906. (header-field))
  6907. (with-current-buffer buffer
  6908. (mh-display-msg num folder)
  6909. (if (equal major-mode 'mh-folder-mode)
  6910. (mh-header-display)
  6911. (mh-show-header-display))
  6912. (set-buffer buffer)
  6913. (setq header-field (mh-get-header-field header))
  6914. (if (equal major-mode 'mh-folder-mode)
  6915. (mh-show)
  6916. (mh-show-show))
  6917. header-field)))
  6918. (defun org-follow-mhe-link (folder article)
  6919. "Follow an MHE link to FOLDER and ARTICLE.
  6920. If ARTICLE is nil FOLDER is shown. If the configuration variable
  6921. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  6922. ARTICLE is searched in all folders. Indexed searches (swish++,
  6923. namazu, and others supported by MH-E) will always search in all
  6924. folders."
  6925. (require 'mh-e)
  6926. (require 'mh-search)
  6927. (require 'mh-utils)
  6928. (mh-find-path)
  6929. (if (not article)
  6930. (mh-visit-folder (mh-normalize-folder-name folder))
  6931. (setq article (org-add-angle-brackets article))
  6932. (mh-search-choose)
  6933. (if (equal mh-searcher 'pick)
  6934. (progn
  6935. (mh-search folder (list "--message-id" article))
  6936. (when (and org-mhe-search-all-folders
  6937. (not (org-mhe-get-message-real-folder)))
  6938. (kill-this-buffer)
  6939. (mh-search "+" (list "--message-id" article))))
  6940. (mh-search "+" article))
  6941. (if (org-mhe-get-message-real-folder)
  6942. (mh-show-msg 1)
  6943. (kill-this-buffer)
  6944. (error "Message not found"))))
  6945. (defun org-open-file (path &optional in-emacs line search)
  6946. "Open the file at PATH.
  6947. First, this expands any special file name abbreviations. Then the
  6948. configuration variable `org-file-apps' is checked if it contains an
  6949. entry for this file type, and if yes, the corresponding command is launched.
  6950. If no application is found, Emacs simply visits the file.
  6951. With optional argument IN-EMACS, Emacs will visit the file.
  6952. Optional LINE specifies a line to go to, optional SEARCH a string to
  6953. search for. If LINE or SEARCH is given, the file will always be
  6954. opened in Emacs.
  6955. If the file does not exist, an error is thrown."
  6956. (setq in-emacs (or in-emacs line search))
  6957. (let* ((file (if (equal path "")
  6958. buffer-file-name
  6959. (convert-standard-filename (org-expand-file-name path))))
  6960. (dirp (file-directory-p file))
  6961. (dfile (downcase file))
  6962. (old-buffer (current-buffer))
  6963. (old-pos (point))
  6964. (old-mode major-mode)
  6965. ext cmd apps)
  6966. (if (and (not (file-exists-p file))
  6967. (not org-open-non-existing-files))
  6968. (error "No such file: %s" file))
  6969. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6970. (setq ext (match-string 1 dfile))
  6971. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6972. (setq ext (match-string 1 dfile))))
  6973. (setq apps (append org-file-apps (org-default-apps)))
  6974. (if in-emacs
  6975. (setq cmd 'emacs)
  6976. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  6977. (cdr (assoc ext apps))
  6978. (cdr (assoc t apps)))))
  6979. (when (eq cmd 'mailcap)
  6980. (require 'mailcap)
  6981. (mailcap-parse-mailcaps)
  6982. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6983. (command (mailcap-mime-info mime-type)))
  6984. (if (stringp command)
  6985. (setq cmd command)
  6986. (setq cmd 'emacs))))
  6987. (cond
  6988. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6989. (setq cmd (format cmd (concat "\"" file "\"")))
  6990. (save-window-excursion
  6991. (shell-command (concat cmd " &"))))
  6992. ((or (stringp cmd)
  6993. (eq cmd 'emacs))
  6994. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  6995. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  6996. (if line (goto-line line)
  6997. (if search (org-link-search search))))
  6998. ((consp cmd)
  6999. (eval cmd))
  7000. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7001. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  7002. (or (not (equal old-buffer (current-buffer)))
  7003. (not (equal old-pos (point))))
  7004. (org-mark-ring-push old-pos old-buffer))))
  7005. (defun org-default-apps ()
  7006. "Return the default applications for this operating system."
  7007. (cond
  7008. ((eq system-type 'darwin)
  7009. org-file-apps-defaults-macosx)
  7010. ((eq system-type 'windows-nt)
  7011. org-file-apps-defaults-windowsnt)
  7012. (t org-file-apps-defaults-gnu)))
  7013. (defun org-expand-file-name (path)
  7014. "Replace special path abbreviations and expand the file name."
  7015. (expand-file-name path))
  7016. (defvar org-insert-link-history nil
  7017. "Minibuffer history for links inserted with `org-insert-link'.")
  7018. (defvar org-stored-links nil
  7019. "Contains the links stored with `org-store-link'.")
  7020. ;;;###autoload
  7021. (defun org-store-link (arg)
  7022. "\\<org-mode-map>Store an org-link to the current location.
  7023. This link can later be inserted into an org-buffer with
  7024. \\[org-insert-link].
  7025. For some link types, a prefix arg is interpreted:
  7026. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  7027. For file links, arg negates `org-context-in-file-links'."
  7028. (interactive "P")
  7029. (let (link cpltxt desc txt (pos (point)))
  7030. (cond
  7031. ((eq major-mode 'bbdb-mode)
  7032. (setq cpltxt (concat
  7033. "bbdb:"
  7034. (or (bbdb-record-name (bbdb-current-record))
  7035. (bbdb-record-company (bbdb-current-record))))
  7036. link (org-make-link cpltxt)))
  7037. ((eq major-mode 'calendar-mode)
  7038. (let ((cd (calendar-cursor-to-date)))
  7039. (setq link
  7040. (format-time-string
  7041. (car org-time-stamp-formats)
  7042. (apply 'encode-time
  7043. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7044. nil nil nil))))))
  7045. ((or (eq major-mode 'vm-summary-mode)
  7046. (eq major-mode 'vm-presentation-mode))
  7047. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  7048. (vm-follow-summary-cursor)
  7049. (save-excursion
  7050. (vm-select-folder-buffer)
  7051. (let* ((message (car vm-message-pointer))
  7052. (folder buffer-file-name)
  7053. (subject (vm-su-subject message))
  7054. (author (vm-su-full-name message))
  7055. (message-id (vm-su-message-id message)))
  7056. (setq message-id (org-remove-angle-brackets message-id))
  7057. (setq folder (abbreviate-file-name folder))
  7058. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  7059. folder)
  7060. (setq folder (replace-match "" t t folder)))
  7061. (setq cpltxt (concat author " on: " subject))
  7062. (setq link (org-make-link "vm:" folder "#" message-id)))))
  7063. ((eq major-mode 'wl-summary-mode)
  7064. (let* ((msgnum (wl-summary-message-number))
  7065. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  7066. msgnum 'message-id))
  7067. (wl-message-entity (elmo-msgdb-overview-get-entity
  7068. msgnum (wl-summary-buffer-msgdb)))
  7069. (author (wl-summary-line-from)) ; FIXME: how to get author name?
  7070. (subject "???")) ; FIXME: How to get subject of email?
  7071. (setq message-id (org-remove-angle-brackets message-id))
  7072. (setq cpltxt (concat author " on: " subject))
  7073. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  7074. "#" message-id))))
  7075. ((or (equal major-mode 'mh-folder-mode)
  7076. (equal major-mode 'mh-show-mode))
  7077. (let ((from-header (org-mhe-get-header "From:"))
  7078. (to-header (org-mhe-get-header "To:"))
  7079. (subject (org-mhe-get-header "Subject:")))
  7080. (setq cpltxt (concat from-header " on: " subject))
  7081. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  7082. (org-remove-angle-brackets
  7083. (org-mhe-get-header "Message-Id:"))))))
  7084. ((eq major-mode 'rmail-mode)
  7085. (save-excursion
  7086. (save-restriction
  7087. (rmail-narrow-to-non-pruned-header)
  7088. (let ((folder buffer-file-name)
  7089. (message-id (mail-fetch-field "message-id"))
  7090. (author (mail-fetch-field "from"))
  7091. (subject (mail-fetch-field "subject")))
  7092. (setq message-id (org-remove-angle-brackets message-id))
  7093. (setq cpltxt (concat author " on: " subject))
  7094. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  7095. ((eq major-mode 'gnus-group-mode)
  7096. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  7097. (gnus-group-group-name)) ; version
  7098. ((fboundp 'gnus-group-name)
  7099. (gnus-group-name))
  7100. (t "???"))))
  7101. (setq cpltxt (concat
  7102. (if (org-xor arg org-usenet-links-prefer-google)
  7103. "http://groups.google.com/groups?group="
  7104. "gnus:")
  7105. group)
  7106. link (org-make-link cpltxt))))
  7107. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  7108. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  7109. (gnus-summary-beginning-of-article)
  7110. (let* ((group (car gnus-article-current))
  7111. (article (cdr gnus-article-current))
  7112. (header (gnus-summary-article-header article))
  7113. (author (mail-header-from header))
  7114. (message-id (mail-header-id header))
  7115. (date (mail-header-date header))
  7116. (subject (gnus-summary-subject-string)))
  7117. (setq cpltxt (concat author " on: " subject))
  7118. (if (org-xor arg org-usenet-links-prefer-google)
  7119. (setq link
  7120. (concat
  7121. cpltxt "\n "
  7122. (format "http://groups.google.com/groups?as_umsgid=%s"
  7123. (org-fixup-message-id-for-http message-id))))
  7124. (setq link (org-make-link "gnus:" group
  7125. "#" (number-to-string article))))))
  7126. ((eq major-mode 'w3-mode)
  7127. (setq cpltxt (url-view-url t)
  7128. link (org-make-link cpltxt)))
  7129. ((eq major-mode 'w3m-mode)
  7130. (setq cpltxt w3m-current-url
  7131. link (org-make-link cpltxt)))
  7132. ((eq major-mode 'org-mode)
  7133. ;; Just link to current headline
  7134. (setq cpltxt (concat "file:"
  7135. (abbreviate-file-name buffer-file-name)))
  7136. ;; Add a context search string
  7137. (when (org-xor org-context-in-file-links arg)
  7138. ;; Check if we are on a target
  7139. (if (save-excursion
  7140. (skip-chars-forward "^>\n\r")
  7141. (and (re-search-backward "<<" nil t)
  7142. (looking-at "<<\\(.*?\\)>>")
  7143. (<= (match-beginning 0) pos)
  7144. (>= (match-end 0) pos)))
  7145. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  7146. (setq txt (cond
  7147. ((org-on-heading-p) nil)
  7148. ((org-region-active-p)
  7149. (buffer-substring (region-beginning) (region-end)))
  7150. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  7151. (setq cpltxt
  7152. (concat cpltxt "::"
  7153. (if org-file-link-context-use-camel-case
  7154. (org-make-org-heading-camel txt)
  7155. (org-make-org-heading-search-string txt)))
  7156. desc "NONE")))
  7157. (if (string-match "::\\'" cpltxt)
  7158. (setq cpltxt (substring cpltxt 0 -2)))
  7159. (setq link (org-make-link cpltxt)))
  7160. (buffer-file-name
  7161. ;; Just link to this file here.
  7162. (setq cpltxt (concat "file:"
  7163. (abbreviate-file-name buffer-file-name)))
  7164. ;; Add a context string
  7165. (when (org-xor org-context-in-file-links arg)
  7166. (setq txt (if (org-region-active-p)
  7167. (buffer-substring (region-beginning) (region-end))
  7168. (buffer-substring (point-at-bol) (point-at-eol))))
  7169. (setq cpltxt
  7170. (concat cpltxt "::"
  7171. (if org-file-link-context-use-camel-case
  7172. (org-make-org-heading-camel txt)
  7173. (org-make-org-heading-search-string txt)))
  7174. desc "NONE"))
  7175. (setq link (org-make-link cpltxt)))
  7176. ((interactive-p)
  7177. (error "Cannot link to a buffer which is not visiting a file"))
  7178. (t (setq link nil)))
  7179. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7180. (setq link (or link cpltxt)
  7181. desc (or desc cpltxt))
  7182. (if (equal desc "NONE") (setq desc nil))
  7183. (if (and (interactive-p) link)
  7184. (progn
  7185. (setq org-stored-links
  7186. (cons (list cpltxt link desc) org-stored-links))
  7187. (message "Stored: %s" (or cpltxt link)))
  7188. (org-make-link-string link desc))))
  7189. (defun org-make-org-heading-search-string (&optional string heading)
  7190. "Make search string for STRING or current headline."
  7191. (interactive)
  7192. (let ((s (or string (org-get-heading))))
  7193. (unless (and string (not heading))
  7194. ;; We are using a headline, clean up garbage in there.
  7195. (if (string-match org-todo-regexp s)
  7196. (setq s (replace-match "" t t s)))
  7197. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  7198. (setq s (replace-match "" t t s)))
  7199. (setq s (org-trim s))
  7200. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7201. org-comment-string "\\)") s)
  7202. (setq s (replace-match "" t t s)))
  7203. (while (string-match org-ts-regexp s)
  7204. (setq s (replace-match "" t t s))))
  7205. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  7206. (setq s (replace-match " " t t s)))
  7207. (or string (setq s (concat "*" s))) ; Add * for headlines
  7208. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7209. (defun org-make-org-heading-camel (&optional string heading)
  7210. "Make a CamelCase string for STRING or the current headline."
  7211. (interactive)
  7212. (let ((s (or string (org-get-heading))))
  7213. (unless (and string (not heading))
  7214. ;; We are using a headline, clean up garbage in there.
  7215. (if (string-match org-todo-regexp s)
  7216. (setq s (replace-match "" t t s)))
  7217. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  7218. (setq s (replace-match "" t t s)))
  7219. (setq s (org-trim s))
  7220. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7221. org-comment-string "\\)") s)
  7222. (setq s (replace-match "" t t s)))
  7223. (while (string-match org-ts-regexp s)
  7224. (setq s (replace-match "" t t s))))
  7225. (while (string-match "[^a-zA-Z_ \t]+" s)
  7226. (setq s (replace-match " " t t s)))
  7227. (or string (setq s (concat "*" s))) ; Add * for headlines
  7228. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  7229. (defun org-make-link (&rest strings)
  7230. "Concatenate STRINGS, format resulting string with `org-link-format'."
  7231. (format org-link-format (apply 'concat strings)))
  7232. (defun org-make-link-string (link &optional description)
  7233. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7234. (if org-old-link-style
  7235. (concat description "\n" link)
  7236. (if (stringp description)
  7237. ;; Remove brackets from the description, they are fatal.
  7238. (while (string-match "\\[\\|\\]" description)
  7239. (setq description (replace-match "" t t description))))
  7240. (concat "[[" (org-link-escape link) "]"
  7241. (if description (concat "[" description "]") "")
  7242. "]")))
  7243. ;; FIXME: Do we need more characters here?
  7244. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  7245. "Association list of escapes for some characters problematic in links.")
  7246. (defun org-link-escape (text)
  7247. "Escape charaters in TEXT that are problematic for links."
  7248. (when text
  7249. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  7250. org-link-escape-chars "\\|")))
  7251. (while (string-match re text)
  7252. (setq text
  7253. (replace-match
  7254. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  7255. t t text)))
  7256. text)))
  7257. (defun org-link-unescape (text)
  7258. "Reverse the action of `org-link-escape'."
  7259. (when text
  7260. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  7261. org-link-escape-chars "\\|")))
  7262. (while (string-match re text)
  7263. (setq text
  7264. (replace-match
  7265. (car (rassoc (match-string 0 text) org-link-escape-chars))
  7266. t t text)))
  7267. text)))
  7268. (defun org-xor (a b)
  7269. "Exclusive or."
  7270. (if a (not b) b))
  7271. (defun org-get-header (header)
  7272. "Find a header field in the current buffer."
  7273. (save-excursion
  7274. (goto-char (point-min))
  7275. (let ((case-fold-search t) s)
  7276. (cond
  7277. ((eq header 'from)
  7278. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  7279. (setq s (match-string 1)))
  7280. (while (string-match "\"" s)
  7281. (setq s (replace-match "" t t s)))
  7282. (if (string-match "[<(].*" s)
  7283. (setq s (replace-match "" t t s))))
  7284. ((eq header 'message-id)
  7285. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  7286. (setq s (match-string 1))))
  7287. ((eq header 'subject)
  7288. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  7289. (setq s (match-string 1)))))
  7290. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  7291. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  7292. s)))
  7293. (defun org-fixup-message-id-for-http (s)
  7294. "Replace special characters in a message id, so it can be used in an http query."
  7295. (while (string-match "<" s)
  7296. (setq s (replace-match "%3C" t t s)))
  7297. (while (string-match ">" s)
  7298. (setq s (replace-match "%3E" t t s)))
  7299. (while (string-match "@" s)
  7300. (setq s (replace-match "%40" t t s)))
  7301. s)
  7302. (defun org-insert-link (&optional complete-file)
  7303. "Insert a link. At the prompt, enter the link.
  7304. Completion can be used to select a link previously stored with
  7305. `org-store-link'. When the empty string is entered (i.e. if you just
  7306. press RET at the prompt), the link defaults to the most recently
  7307. stored link. As SPC triggers completion in the minibuffer, you need to
  7308. use M-SPC or C-q SPC to force the insertion of a space character.
  7309. You will also be prompted for a description, and if one is given, it will
  7310. be displayed in the buffer instead of the link.
  7311. If there is already a link at point, this command will allow you to edit link
  7312. and description parts.
  7313. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  7314. selected using completion. The path to the file will be relative to
  7315. the current directory if the file is in the current directory or a
  7316. subdirectory. Otherwise, the link will be the absolute path as
  7317. completed in the minibuffer (i.e. normally ~/path/to/file).
  7318. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  7319. is in the current directory or below."
  7320. (interactive "P")
  7321. (let (link desc entry remove file (pos (point)))
  7322. (cond
  7323. ((save-excursion
  7324. (skip-chars-forward "^]\n\r")
  7325. (and (re-search-backward "\\[\\[" nil t)
  7326. (looking-at org-bracket-link-regexp)
  7327. (<= (match-beginning 0) pos)
  7328. (>= (match-end 0) pos)))
  7329. ;; We do have a link at point, and we are going to edit it.
  7330. (setq remove (list (match-beginning 0) (match-end 0)))
  7331. (setq desc (if (match-end 3) (match-string 3)))
  7332. (setq link (read-string "Link: " (org-match-string-no-properties 1))))
  7333. (complete-file
  7334. ;; Completing read for file names.
  7335. (setq file (read-file-name "File: "))
  7336. (let ((pwd (file-name-as-directory (expand-file-name "."))))
  7337. (cond
  7338. ((equal complete-file '(16))
  7339. (setq link (org-make-link
  7340. "file:"
  7341. (abbreviate-file-name (expand-file-name link)))))
  7342. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  7343. (expand-file-name link))
  7344. (setq link (org-make-link
  7345. "file:" (match-string 1 (expand-file-name link)))))
  7346. (t (setq link (org-make-link "file:" link))))))
  7347. (t
  7348. ;; Read link, with completion for stored links.
  7349. (setq link (completing-read
  7350. "Link: " org-stored-links nil nil nil
  7351. org-insert-link-history
  7352. (or (car (car org-stored-links)))))
  7353. (setq entry (assoc link org-stored-links))
  7354. (if (not org-keep-stored-link-after-insertion)
  7355. (setq org-stored-links (delq (assoc link org-stored-links)
  7356. org-stored-links)))
  7357. (setq link (if entry (nth 1 entry) link)
  7358. desc (or desc (nth 2 entry)))))
  7359. (if (string-match org-link-regexp link)
  7360. ;; URL-like link, normalize the use of angular brackets.
  7361. (setq link (org-make-link (org-remove-angle-brackets link))))
  7362. ;; Check if we are linking to the current file. If yes, simplify the link.
  7363. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  7364. (let* ((path (match-string 1 link))
  7365. (case-fold-search nil)
  7366. (search (match-string 2 link)))
  7367. (when (save-match-data
  7368. (equal (file-truename buffer-file-name)
  7369. (file-truename path)))
  7370. ;; We are linking to this same file, with a search option
  7371. (setq link search))))
  7372. ; FIXME: Do we need to re-enable camel case????? I prefer not...
  7373. ; (if (and org-file-link-context-use-camel-case
  7374. ; (save-match-data
  7375. ; (string-match (concat "^" org-camel-regexp "$") search)))
  7376. ; (setq link (replace-match search t t link)
  7377. ; matched t)
  7378. ; (setq link (replace-match (concat "[[" search "]]")
  7379. ; t t link)
  7380. ; matched t)))))
  7381. (setq desc (read-string "Description: " desc))
  7382. (unless (string-match "\\S-" desc) (setq desc nil))
  7383. (if remove (apply 'delete-region remove))
  7384. (insert (org-make-link-string link desc))))
  7385. ;;; Hooks for remember.el
  7386. (defvar org-finish-function nil)
  7387. ;;;###autoload
  7388. (defun org-remember-annotation ()
  7389. "Return a link to the current location as an annotation for remember.el.
  7390. If you are using Org-mode files as target for data storage with
  7391. remember.el, then the annotations should include a link compatible with the
  7392. conventions in Org-mode. This function returns such a link."
  7393. (org-store-link nil))
  7394. (defconst org-remember-help
  7395. "Select a destination location for the note.
  7396. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  7397. RET at beg-of-buf -> Append to file as level 2 headline
  7398. RET on headline -> Store as sublevel entry to current headline
  7399. <left>/<right> -> before/after current headline, same headings level")
  7400. ;;;###autoload
  7401. (defun org-remember-apply-template ()
  7402. "Initialize *remember* buffer with template, invode `org-mode'.
  7403. This function should be placed into `remember-mode-hook' and in fact requires
  7404. to be run from that hook to fucntion properly."
  7405. (when org-remember-templates
  7406. (let* ((entry (if (= (length org-remember-templates) 1)
  7407. (cdar org-remember-templates)
  7408. (message "Select template: %s"
  7409. (mapconcat
  7410. (lambda (x) (char-to-string (car x)))
  7411. org-remember-templates " "))
  7412. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  7413. (tpl (if (consp (cdr entry)) (cadr entry) (cdr entry)))
  7414. (file (if (consp (cdr entry)) (nth 2 entry)))
  7415. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  7416. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  7417. (v-u (concat "[" (substring v-t 1 -1) "]"))
  7418. (v-U (concat "[" (substring v-T 1 -1) "]"))
  7419. (v-a annotation) ; defined in `remember-mode'
  7420. (v-i initial) ; defined in `remember-mode'
  7421. (v-n user-full-name)
  7422. )
  7423. (unless tpl (setq tpl "") (message "No template") (ding))
  7424. (insert tpl) (goto-char (point-min))
  7425. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  7426. (when (and initial (equal (match-string 0) "%i"))
  7427. (save-match-data
  7428. (let* ((lead (buffer-substring
  7429. (point-at-bol) (match-beginning 0))))
  7430. (setq v-i (mapconcat 'identity
  7431. (org-split-string initial "\n")
  7432. (concat "\n" lead))))))
  7433. (replace-match
  7434. (or (eval (intern (concat "v-" (match-string 1)))) "")
  7435. t t))
  7436. (let ((org-startup-folded nil)
  7437. (org-startup-with-deadline-check nil))
  7438. (org-mode))
  7439. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  7440. (set (make-local-variable 'org-default-notes-file) file))
  7441. (goto-char (point-min))
  7442. (if (re-search-forward "%\\?" nil t) (replace-match ""))
  7443. (set (make-local-variable 'org-finish-function) 'remember-buffer))))
  7444. ;;;###autoload
  7445. (defun org-remember-handler ()
  7446. "Store stuff from remember.el into an org file.
  7447. First prompts for an org file. If the user just presses return, the value
  7448. of `org-default-notes-file' is used.
  7449. Then the command offers the headings tree of the selected file in order to
  7450. file the text at a specific location.
  7451. You can either immediately press RET to get the note appended to the
  7452. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  7453. find a better place. Then press RET or <left> or <right> in insert the note.
  7454. Key Cursor position Note gets inserted
  7455. -----------------------------------------------------------------------------
  7456. RET buffer-start as level 2 heading at end of file
  7457. RET on headline as sublevel of the heading at cursor
  7458. RET no heading at cursor position, level taken from context.
  7459. Or use prefix arg to specify level manually.
  7460. <left> on headline as same level, before current heading
  7461. <right> on headline as same level, after current heading
  7462. So the fastest way to store the note is to press RET RET to append it to
  7463. the default file. This way your current train of thought is not
  7464. interrupted, in accordance with the principles of remember.el. But with
  7465. little extra effort, you can push it directly to the correct location.
  7466. Before being stored away, the function ensures that the text has a
  7467. headline, i.e. a first line that starts with a \"*\". If not, a headline
  7468. is constructed from the current date and some additional data.
  7469. If the variable `org-adapt-indentation' is non-nil, the entire text is
  7470. also indented so that it starts in the same column as the headline
  7471. \(i.e. after the stars).
  7472. See also the variable `org-reverse-note-order'."
  7473. (catch 'quit
  7474. (let* ((txt (buffer-substring (point-min) (point-max)))
  7475. (fastp current-prefix-arg)
  7476. (file (if fastp org-default-notes-file (org-get-org-file)))
  7477. (visiting (find-buffer-visiting file))
  7478. (org-startup-with-deadline-check nil)
  7479. (org-startup-folded nil)
  7480. (org-startup-align-all-tables nil)
  7481. spos level indent reversed)
  7482. ;; Modify text so that it becomes a nice subtree which can be inserted
  7483. ;; into an org tree.
  7484. (let* ((lines (split-string txt "\n"))
  7485. first)
  7486. ;; remove empty lines at the beginning
  7487. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  7488. (setq lines (cdr lines)))
  7489. (setq first (car lines) lines (cdr lines))
  7490. (if (string-match "^\\*+" first)
  7491. ;; Is already a headline
  7492. (setq indent nil)
  7493. ;; We need to add a headline: Use time and first buffer line
  7494. (setq lines (cons first lines)
  7495. first (concat "* " (current-time-string)
  7496. " (" (remember-buffer-desc) ")")
  7497. indent " "))
  7498. (if (and org-adapt-indentation indent)
  7499. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  7500. (setq txt (concat first "\n"
  7501. (mapconcat 'identity lines "\n"))))
  7502. ;; Find the file
  7503. (if (not visiting)
  7504. (find-file-noselect file))
  7505. (with-current-buffer (get-file-buffer file)
  7506. (setq reversed (org-notes-order-reversed-p))
  7507. (save-excursion
  7508. (save-restriction
  7509. (widen)
  7510. ;; Ask the User for a location
  7511. (setq spos (if fastp 1 (org-get-location
  7512. (current-buffer)
  7513. org-remember-help)))
  7514. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  7515. ; not handle this note
  7516. (goto-char spos)
  7517. (cond ((bobp)
  7518. ;; Put it at the start or end, as level 2
  7519. (save-restriction
  7520. (widen)
  7521. (goto-char (if reversed (point-min) (point-max)))
  7522. (if (not (bolp)) (newline))
  7523. (org-paste-subtree 2 txt)))
  7524. ((and (org-on-heading-p nil) (not current-prefix-arg))
  7525. ;; Put it below this entry, at the beg/end of the subtree
  7526. (org-back-to-heading)
  7527. (setq level (funcall outline-level))
  7528. (if reversed
  7529. (outline-end-of-heading)
  7530. (outline-end-of-subtree))
  7531. (if (not (bolp)) (newline))
  7532. (beginning-of-line 1)
  7533. (org-paste-subtree (1+ level) txt))
  7534. (t
  7535. ;; Put it right there, with automatic level determined by
  7536. ;; org-paste-subtree or from prefix arg
  7537. (org-paste-subtree current-prefix-arg txt)))
  7538. (when remember-save-after-remembering
  7539. (save-buffer)
  7540. (if (not visiting) (kill-buffer (current-buffer)))))))))
  7541. t) ;; return t to indicate that we took care of this note.
  7542. (defun org-get-org-file ()
  7543. "Read a filename, with default directory `org-directory'."
  7544. (let ((default (or org-default-notes-file remember-data-file)))
  7545. (read-file-name (format "File name [%s]: " default)
  7546. (file-name-as-directory org-directory)
  7547. default)))
  7548. (defun org-notes-order-reversed-p ()
  7549. "Check if the current file should receive notes in reversed order."
  7550. (cond
  7551. ((not org-reverse-note-order) nil)
  7552. ((eq t org-reverse-note-order) t)
  7553. ((not (listp org-reverse-note-order)) nil)
  7554. (t (catch 'exit
  7555. (let ((all org-reverse-note-order)
  7556. entry)
  7557. (while (setq entry (pop all))
  7558. (if (string-match (car entry) buffer-file-name)
  7559. (throw 'exit (cdr entry))))
  7560. nil)))))
  7561. ;;; Tables
  7562. ;; Watch out: Here we are talking about two different kind of tables.
  7563. ;; Most of the code is for the tables created with the Org-mode table editor.
  7564. ;; Sometimes, we talk about tables created and edited with the table.el
  7565. ;; Emacs package. We call the former org-type tables, and the latter
  7566. ;; table.el-type tables.
  7567. (defun org-before-change-function (beg end)
  7568. "Every change indicates that a table might need an update."
  7569. (setq org-table-may-need-update t))
  7570. (defconst org-table-line-regexp "^[ \t]*|"
  7571. "Detects an org-type table line.")
  7572. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  7573. "Detects an org-type table line.")
  7574. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  7575. "Detects a table line marked for automatic recalculation.")
  7576. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  7577. "Detects a table line marked for automatic recalculation.")
  7578. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  7579. "Detects a table line marked for automatic recalculation.")
  7580. (defconst org-table-hline-regexp "^[ \t]*|-"
  7581. "Detects an org-type table hline.")
  7582. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  7583. "Detects a table-type table hline.")
  7584. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  7585. "Detects an org-type or table-type table.")
  7586. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  7587. "Searching from within a table (any type) this finds the first line
  7588. outside the table.")
  7589. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  7590. "Searching from within a table (any type) this finds the first line
  7591. outside the table.")
  7592. (defun org-table-create-with-table.el ()
  7593. "Use the table.el package to insert a new table.
  7594. If there is already a table at point, convert between Org-mode tables
  7595. and table.el tables."
  7596. (interactive)
  7597. (require 'table)
  7598. (cond
  7599. ((org-at-table.el-p)
  7600. (if (y-or-n-p "Convert table to Org-mode table? ")
  7601. (org-table-convert)))
  7602. ((org-at-table-p)
  7603. (if (y-or-n-p "Convert table to table.el table? ")
  7604. (org-table-convert)))
  7605. (t (call-interactively 'table-insert))))
  7606. (defun org-table-create (&optional size)
  7607. "Query for a size and insert a table skeleton.
  7608. SIZE is a string Columns x Rows like for example \"3x2\"."
  7609. (interactive "P")
  7610. (unless size
  7611. (setq size (read-string
  7612. (concat "Table size Columns x Rows [e.g. "
  7613. org-table-default-size "]: ")
  7614. "" nil org-table-default-size)))
  7615. (let* ((pos (point))
  7616. (indent (make-string (current-column) ?\ ))
  7617. (split (org-split-string size " *x *"))
  7618. (rows (string-to-number (nth 1 split)))
  7619. (columns (string-to-number (car split)))
  7620. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  7621. "\n")))
  7622. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  7623. (point-at-bol) (point)))
  7624. (beginning-of-line 1)
  7625. (newline))
  7626. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  7627. (dotimes (i rows) (insert line))
  7628. (goto-char pos)
  7629. (if (> rows 1)
  7630. ;; Insert a hline after the first row.
  7631. (progn
  7632. (end-of-line 1)
  7633. (insert "\n|-")
  7634. (goto-char pos)))
  7635. (org-table-align)))
  7636. (defun org-table-convert-region (beg0 end0 nspace)
  7637. "Convert region to a table.
  7638. The region goes from BEG0 to END0, but these borders will be moved
  7639. slightly, to make sure a beginning of line in the first line is included.
  7640. When NSPACE is non-nil, it indicates the minimum number of spaces that
  7641. separate columns (default: just one space)."
  7642. (let* ((beg (min beg0 end0))
  7643. (end (max beg0 end0))
  7644. (tabsep t)
  7645. re)
  7646. (goto-char beg)
  7647. (beginning-of-line 1)
  7648. (setq beg (move-marker (make-marker) (point)))
  7649. (goto-char end)
  7650. (if (bolp) (backward-char 1) (end-of-line 1))
  7651. (setq end (move-marker (make-marker) (point)))
  7652. ;; Lets see if this is tab-separated material. If every nonempty line
  7653. ;; contains a tab, we will assume that it is tab-separated material
  7654. (if nspace
  7655. (setq tabsep nil)
  7656. (goto-char beg)
  7657. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  7658. (if nspace (setq tabsep nil))
  7659. (if tabsep
  7660. (setq re "^\\|\t")
  7661. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  7662. (max 1 (prefix-numeric-value nspace)))))
  7663. (goto-char beg)
  7664. (while (re-search-forward re end t)
  7665. (replace-match "|" t t))
  7666. (goto-char beg)
  7667. (insert " ")
  7668. (org-table-align)))
  7669. (defun org-table-import (file arg)
  7670. "Import FILE as a table.
  7671. The file is assumed to be tab-separated. Such files can be produced by most
  7672. spreadsheet and database applications. If no tabs (at least one per line)
  7673. are found, lines will be split on whitespace into fields."
  7674. (interactive "f\nP")
  7675. (or (bolp) (newline))
  7676. (let ((beg (point))
  7677. (pm (point-max)))
  7678. (insert-file-contents file)
  7679. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  7680. (defun org-table-export ()
  7681. "Export table as a tab-separated file.
  7682. Such a file can be imported into a spreadsheet program like Excel."
  7683. (interactive)
  7684. (let* ((beg (org-table-begin))
  7685. (end (org-table-end))
  7686. (table (buffer-substring beg end))
  7687. (file (read-file-name "Export table to: "))
  7688. buf)
  7689. (unless (or (not (file-exists-p file))
  7690. (y-or-n-p (format "Overwrite file %s? " file)))
  7691. (error "Abort"))
  7692. (with-current-buffer (find-file-noselect file)
  7693. (setq buf (current-buffer))
  7694. (erase-buffer)
  7695. (fundamental-mode)
  7696. (insert table)
  7697. (goto-char (point-min))
  7698. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  7699. (replace-match "" t t)
  7700. (end-of-line 1))
  7701. (goto-char (point-min))
  7702. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  7703. (replace-match "" t t)
  7704. (goto-char (min (1+ (point)) (point-max))))
  7705. (goto-char (point-min))
  7706. (while (re-search-forward "^-[-+]*$" nil t)
  7707. (replace-match "")
  7708. (if (looking-at "\n")
  7709. (delete-char 1)))
  7710. (goto-char (point-min))
  7711. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  7712. (replace-match "\t" t t))
  7713. (save-buffer))
  7714. (kill-buffer buf)))
  7715. (defvar org-table-aligned-begin-marker (make-marker)
  7716. "Marker at the beginning of the table last aligned.
  7717. Used to check if cursor still is in that table, to minimize realignment.")
  7718. (defvar org-table-aligned-end-marker (make-marker)
  7719. "Marker at the end of the table last aligned.
  7720. Used to check if cursor still is in that table, to minimize realignment.")
  7721. (defvar org-table-last-alignment nil
  7722. "List of flags for flushright alignment, from the last re-alignment.
  7723. This is being used to correctly align a single field after TAB or RET.")
  7724. (defvar org-table-last-column-widths nil
  7725. "List of max width of fields in each column.
  7726. This is being used to correctly align a single field after TAB or RET.")
  7727. (defvar org-last-recalc-line nil)
  7728. (defun org-table-align ()
  7729. "Align the table at point by aligning all vertical bars."
  7730. (interactive)
  7731. (let* (
  7732. ;; Limits of table
  7733. (beg (org-table-begin))
  7734. (end (org-table-end))
  7735. ;; Current cursor position
  7736. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  7737. (colpos (org-table-current-column))
  7738. (winstart (window-start))
  7739. text lines (new "") lengths l typenums ty fields maxfields i
  7740. column
  7741. (indent "") cnt frac
  7742. rfmt hfmt
  7743. (spaces (if (org-in-invisibility-spec-p '(org-table))
  7744. org-table-spaces-around-invisible-separators
  7745. org-table-spaces-around-separators))
  7746. (sp1 (car spaces))
  7747. (sp2 (cdr spaces))
  7748. (rfmt1 (concat
  7749. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  7750. (hfmt1 (concat
  7751. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  7752. emptystrings xx links narrow fmax fmin f1 len c e)
  7753. (untabify beg end)
  7754. (remove-text-properties beg end '(org-cwidth t display t))
  7755. ;; Check if we have links
  7756. (goto-char beg)
  7757. (setq links (re-search-forward org-bracket-link-regexp end t))
  7758. ;; Make sure the link properties are right FIXME: Can this be optimized????
  7759. (when links (goto-char beg) (while (org-activate-links2 end)))
  7760. ;; Check if we are narrowing any columns
  7761. (goto-char beg)
  7762. (setq narrow (and org-format-transports-properties-p
  7763. (re-search-forward "<[0-9]+>" end t)))
  7764. ;; Get the rows
  7765. (setq lines (org-split-string
  7766. (buffer-substring beg end) "\n"))
  7767. ;; Store the indentation of the first line
  7768. (if (string-match "^ *" (car lines))
  7769. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  7770. ;; Mark the hlines by setting the corresponding element to nil
  7771. ;; At the same time, we remove trailing space.
  7772. (setq lines (mapcar (lambda (l)
  7773. (if (string-match "^ *|-" l)
  7774. nil
  7775. (if (string-match "[ \t]+$" l)
  7776. (substring l 0 (match-beginning 0))
  7777. l)))
  7778. lines))
  7779. ;; Get the data fields by splitting the lines.
  7780. (setq fields (mapcar
  7781. (lambda (l)
  7782. (org-split-string l " *| *"))
  7783. (delq nil (copy-sequence lines))))
  7784. ;; How many fields in the longest line?
  7785. (condition-case nil
  7786. (setq maxfields (apply 'max (mapcar 'length fields)))
  7787. (error
  7788. (kill-region beg end)
  7789. (org-table-create org-table-default-size)
  7790. (error "Empty table - created default table")))
  7791. ;; A list of empty string to fill any short rows on output
  7792. (setq emptystrings (make-list maxfields ""))
  7793. ;; Check for special formatting.
  7794. (setq i -1)
  7795. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  7796. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  7797. ;; Check if there is an explicit width specified
  7798. (when (and org-table-limit-column-width narrow)
  7799. (setq c column fmax nil)
  7800. (while c
  7801. (setq e (pop c))
  7802. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  7803. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  7804. ;; Find fields that are wider than fmax, and shorten them
  7805. (when fmax
  7806. (loop for xx in column do
  7807. (when (and (stringp xx)
  7808. (> (org-string-width xx) fmax))
  7809. (add-text-properties
  7810. 0 (length xx)
  7811. (list 'help-echo
  7812. (concat "Clipped table field, use C-c ` to edit. Full value is:\n"
  7813. (copy-sequence xx)))
  7814. xx)
  7815. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  7816. (unless (> f1 1)
  7817. (error "Cannot narrow field starting with wide link \"%s\""
  7818. (match-string 0 xx)))
  7819. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  7820. (add-text-properties (- f1 2) f1 (list 'display "=>") xx)))))
  7821. ;; Get the maximum width for each column
  7822. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  7823. ;; Get the fraction of numbers, to decide about alignment of the column
  7824. (setq cnt 0 frac 0.0)
  7825. (loop for x in column do
  7826. (if (equal x "")
  7827. nil
  7828. (setq frac ( / (+ (* frac cnt)
  7829. (if (string-match org-table-number-regexp x) 1 0))
  7830. (setq cnt (1+ cnt))))))
  7831. (push (>= frac org-table-number-fraction) typenums))
  7832. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  7833. ;; Store the alignment of this table, for later editing of single fields
  7834. (setq org-table-last-alignment typenums
  7835. org-table-last-column-widths lengths)
  7836. ;; With invisible characters, `format' does not get the field width right
  7837. ;; So we need to make these fields wide by hand.
  7838. (when links
  7839. (loop for i from 0 upto (1- maxfields) do
  7840. (setq len (nth i lengths))
  7841. (loop for j from 0 upto (1- (length fields)) do
  7842. (setq c (nthcdr i (car (nthcdr j fields))))
  7843. (if (and (stringp (car c))
  7844. (string-match org-bracket-link-regexp (car c))
  7845. (< (org-string-width (car c)) len))
  7846. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  7847. ;; Compute the formats needed for output of the table
  7848. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  7849. (while (setq l (pop lengths))
  7850. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  7851. (setq rfmt (concat rfmt (format rfmt1 ty l))
  7852. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  7853. (setq rfmt (concat rfmt "\n")
  7854. hfmt (concat (substring hfmt 0 -1) "|\n"))
  7855. (setq new (mapconcat
  7856. (lambda (l)
  7857. (if l (apply 'format rfmt
  7858. (append (pop fields) emptystrings))
  7859. hfmt))
  7860. lines ""))
  7861. ;; Replace the old one
  7862. (delete-region beg end)
  7863. (move-marker end nil)
  7864. (move-marker org-table-aligned-begin-marker (point))
  7865. (insert new)
  7866. (move-marker org-table-aligned-end-marker (point))
  7867. ;; Try to move to the old location (approximately)
  7868. (goto-line linepos)
  7869. (set-window-start (selected-window) winstart 'noforce)
  7870. (org-table-goto-column colpos)
  7871. (setq org-table-may-need-update nil)
  7872. (if (org-in-invisibility-spec-p '(org-table))
  7873. (org-table-add-invisible-to-vertical-lines))
  7874. ))
  7875. (defun org-string-width (s)
  7876. "Compute width of string, ignoring invisible characters.
  7877. This ignores character with invisibility property `org-link', and also
  7878. characters with property `org-cwidth', because these will become invisible
  7879. upon the next fontification round."
  7880. (let (b)
  7881. (when (or (eq t buffer-invisibility-spec)
  7882. (assq 'org-link buffer-invisibility-spec))
  7883. (while (setq b (text-property-any 0 (length s) 'invisible 'org-link s))
  7884. (setq s (concat (substring s 0 b) (substring s (or (next-single-property-change b 'invisible s) (length s)))))))
  7885. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  7886. (setq s (concat (substring s 0 b) (substring s (or (next-single-property-change b 'org-cwidth s) (length s))))))
  7887. (string-width s)))
  7888. (defun org-table-begin (&optional table-type)
  7889. "Find the beginning of the table and return its position.
  7890. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  7891. (save-excursion
  7892. (if (not (re-search-backward
  7893. (if table-type org-table-any-border-regexp
  7894. org-table-border-regexp)
  7895. nil t))
  7896. (error "Can't find beginning of table")
  7897. (goto-char (match-beginning 0))
  7898. (beginning-of-line 2)
  7899. (point))))
  7900. (defun org-table-end (&optional table-type)
  7901. "Find the end of the table and return its position.
  7902. With argument TABLE-TYPE, go to the end of a table.el-type table."
  7903. (save-excursion
  7904. (if (not (re-search-forward
  7905. (if table-type org-table-any-border-regexp
  7906. org-table-border-regexp)
  7907. nil t))
  7908. (goto-char (point-max))
  7909. (goto-char (match-beginning 0)))
  7910. (point-marker)))
  7911. (defun org-table-justify-field-maybe (&optional new)
  7912. "Justify the current field, text to left, number to right.
  7913. Optional argument NEW may specify text to replace the current field content."
  7914. (cond
  7915. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  7916. ((org-at-table-hline-p)
  7917. ;; FIXME: I used to enforce realign here, but I think this is not needed.
  7918. ;; (setq org-table-may-need-update t)
  7919. )
  7920. ((and (not new)
  7921. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  7922. (current-buffer)))
  7923. (< (point) org-table-aligned-begin-marker)
  7924. (>= (point) org-table-aligned-end-marker)))
  7925. ;; This is not the same table, force a full re-align
  7926. (setq org-table-may-need-update t))
  7927. (t ;; realign the current field, based on previous full realign
  7928. (let* ((pos (point)) s
  7929. (col (org-table-current-column))
  7930. (num (nth (1- col) org-table-last-alignment))
  7931. l f n o e)
  7932. (when (> col 0)
  7933. (skip-chars-backward "^|\n")
  7934. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  7935. (progn
  7936. (setq s (match-string 1)
  7937. o (match-string 0)
  7938. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  7939. e (not (= (match-beginning 2) (match-end 2))))
  7940. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  7941. l (if e "|" (setq org-table-may-need-update t) ""))
  7942. n (format f s t t))
  7943. (if new
  7944. (if (<= (length new) l)
  7945. (setq n (format f new t t)) ;; FIXME: why t t?????
  7946. (setq n (concat new "|") org-table-may-need-update t)))
  7947. (or (equal n o)
  7948. (let (org-table-may-need-update)
  7949. (replace-match n))))
  7950. (setq org-table-may-need-update t))
  7951. (goto-char pos))))))
  7952. (defun org-table-next-field ()
  7953. "Go to the next field in the current table, creating new lines as needed.
  7954. Before doing so, re-align the table if necessary."
  7955. (interactive)
  7956. (org-table-maybe-eval-formula)
  7957. (org-table-maybe-recalculate-line)
  7958. (if (and org-table-automatic-realign
  7959. org-table-may-need-update)
  7960. (org-table-align))
  7961. (let ((end (org-table-end)))
  7962. (if (org-at-table-hline-p)
  7963. (end-of-line 1))
  7964. (condition-case nil
  7965. (progn
  7966. (re-search-forward "|" end)
  7967. (if (looking-at "[ \t]*$")
  7968. (re-search-forward "|" end))
  7969. (if (and (looking-at "-")
  7970. org-table-tab-jumps-over-hlines
  7971. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  7972. (goto-char (match-beginning 1)))
  7973. (if (looking-at "-")
  7974. (progn
  7975. (beginning-of-line 0)
  7976. (org-table-insert-row 'below))
  7977. (if (looking-at " ") (forward-char 1))))
  7978. (error
  7979. (org-table-insert-row 'below)))))
  7980. (defun org-table-previous-field ()
  7981. "Go to the previous field in the table.
  7982. Before doing so, re-align the table if necessary."
  7983. (interactive)
  7984. (org-table-justify-field-maybe)
  7985. (org-table-maybe-recalculate-line)
  7986. (if (and org-table-automatic-realign
  7987. org-table-may-need-update)
  7988. (org-table-align))
  7989. (if (org-at-table-hline-p)
  7990. (end-of-line 1))
  7991. (re-search-backward "|" (org-table-begin))
  7992. (re-search-backward "|" (org-table-begin))
  7993. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  7994. (re-search-backward "|" (org-table-begin)))
  7995. (if (looking-at "| ?")
  7996. (goto-char (match-end 0))))
  7997. (defun org-table-next-row ()
  7998. "Go to the next row (same column) in the current table.
  7999. Before doing so, re-align the table if necessary."
  8000. (interactive)
  8001. (org-table-maybe-eval-formula)
  8002. (org-table-maybe-recalculate-line)
  8003. (if (or (looking-at "[ \t]*$")
  8004. (save-excursion (skip-chars-backward " \t") (bolp)))
  8005. (newline)
  8006. (if (and org-table-automatic-realign
  8007. org-table-may-need-update)
  8008. (org-table-align))
  8009. (let ((col (org-table-current-column)))
  8010. (beginning-of-line 2)
  8011. (if (or (not (org-at-table-p))
  8012. (org-at-table-hline-p))
  8013. (progn
  8014. (beginning-of-line 0)
  8015. (org-table-insert-row 'below)))
  8016. (org-table-goto-column col)
  8017. (skip-chars-backward "^|\n\r")
  8018. (if (looking-at " ") (forward-char 1)))))
  8019. (defun org-table-copy-down (n)
  8020. "Copy a field down in the current column.
  8021. If the field at the cursor is empty, copy into it the content of the nearest
  8022. non-empty field above. With argument N, use the Nth non-empty field.
  8023. If the current field is not empty, it is copied down to the next row, and
  8024. the cursor is moved with it. Therefore, repeating this command causes the
  8025. column to be filled row-by-row.
  8026. If the variable `org-table-copy-increment' is non-nil and the field is an
  8027. integer, it will be incremented while copying."
  8028. (interactive "p")
  8029. (let* ((colpos (org-table-current-column))
  8030. (field (org-table-get-field))
  8031. (non-empty (string-match "[^ \t]" field))
  8032. (beg (org-table-begin))
  8033. txt)
  8034. (org-table-check-inside-data-field)
  8035. (if non-empty
  8036. (progn
  8037. (setq txt (org-trim field))
  8038. (org-table-next-row)
  8039. (org-table-blank-field))
  8040. (save-excursion
  8041. (setq txt
  8042. (catch 'exit
  8043. (while (progn (beginning-of-line 1)
  8044. (re-search-backward org-table-dataline-regexp
  8045. beg t))
  8046. (org-table-goto-column colpos t)
  8047. (if (and (looking-at
  8048. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  8049. (= (setq n (1- n)) 0))
  8050. (throw 'exit (match-string 1))))))))
  8051. (if txt
  8052. (progn
  8053. (if (and org-table-copy-increment
  8054. (string-match "^[0-9]+$" txt))
  8055. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  8056. (insert txt)
  8057. (org-table-maybe-recalculate-line)
  8058. (org-table-align))
  8059. (error "No non-empty field found"))))
  8060. (defun org-table-check-inside-data-field ()
  8061. "Is point inside a table data field?
  8062. I.e. not on a hline or before the first or after the last column?
  8063. This actually throws an error, so it aborts the current command."
  8064. (if (or (not (org-at-table-p))
  8065. (= (org-table-current-column) 0)
  8066. (org-at-table-hline-p)
  8067. (looking-at "[ \t]*$"))
  8068. (error "Not in table data field")))
  8069. (defvar org-table-clip nil
  8070. "Clipboard for table regions.")
  8071. (defun org-table-blank-field ()
  8072. "Blank the current table field or active region."
  8073. (interactive)
  8074. (org-table-check-inside-data-field)
  8075. (if (and (interactive-p) (org-region-active-p))
  8076. (let (org-table-clip)
  8077. (org-table-cut-region (region-beginning) (region-end)))
  8078. (skip-chars-backward "^|")
  8079. (backward-char 1)
  8080. (if (looking-at "|[^|\n]+")
  8081. (let* ((pos (match-beginning 0))
  8082. (match (match-string 0))
  8083. (len (org-string-width match)))
  8084. (replace-match (concat "|" (make-string (1- len) ?\ )))
  8085. (goto-char (+ 2 pos))
  8086. (substring match 1)))))
  8087. (defun org-table-get-field (&optional n replace)
  8088. "Return the value of the field in column N of current row.
  8089. N defaults to current field.
  8090. If REPLACE is a string, replace field with this value. The return value
  8091. is always the old value."
  8092. (and n (org-table-goto-column n))
  8093. (skip-chars-backward "^|\n")
  8094. (backward-char 1)
  8095. (if (looking-at "|[^|\r\n]*")
  8096. (let* ((pos (match-beginning 0))
  8097. (val (buffer-substring (1+ pos) (match-end 0))))
  8098. (if replace
  8099. (replace-match (concat "|" replace)))
  8100. (goto-char (min (point-at-eol) (+ 2 pos)))
  8101. val)
  8102. (forward-char 1) ""))
  8103. (defun org-table-current-column ()
  8104. "Find out which column we are in.
  8105. When called interactively, column is also displayed in echo area."
  8106. (interactive)
  8107. (if (interactive-p) (org-table-check-inside-data-field))
  8108. (save-excursion
  8109. (let ((cnt 0) (pos (point)))
  8110. (beginning-of-line 1)
  8111. (while (search-forward "|" pos t)
  8112. (setq cnt (1+ cnt)))
  8113. (if (interactive-p) (message "This is table column %d" cnt))
  8114. cnt)))
  8115. (defun org-table-goto-column (n &optional on-delim force)
  8116. "Move the cursor to the Nth column in the current table line.
  8117. With optional argument ON-DELIM, stop with point before the left delimiter
  8118. of the field.
  8119. If there are less than N fields, just go to after the last delimiter.
  8120. However, when FORCE is non-nil, create new columns if necessary."
  8121. (interactive "p")
  8122. (let ((pos (point-at-eol)))
  8123. (beginning-of-line 1)
  8124. (when (> n 0)
  8125. (while (and (> (setq n (1- n)) -1)
  8126. (or (search-forward "|" pos t)
  8127. (and force
  8128. (progn (end-of-line 1)
  8129. (skip-chars-backward "^|")
  8130. (insert " | "))))))
  8131. ; (backward-char 2) t)))))
  8132. (when (and force (not (looking-at ".*|")))
  8133. (save-excursion (end-of-line 1) (insert " | ")))
  8134. (if on-delim
  8135. (backward-char 1)
  8136. (if (looking-at " ") (forward-char 1))))))
  8137. (defun org-at-table-p (&optional table-type)
  8138. "Return t if the cursor is inside an org-type table.
  8139. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  8140. (if org-enable-table-editor
  8141. (save-excursion
  8142. (beginning-of-line 1)
  8143. (looking-at (if table-type org-table-any-line-regexp
  8144. org-table-line-regexp)))
  8145. nil))
  8146. (defun org-at-table.el-p ()
  8147. "Return t if and only if we are at a table.el table."
  8148. (and (org-at-table-p 'any)
  8149. (save-excursion
  8150. (goto-char (org-table-begin 'any))
  8151. (looking-at org-table1-hline-regexp))))
  8152. (defun org-table-recognize-table.el ()
  8153. "If there is a table.el table nearby, recognize it and move into it."
  8154. (if org-table-tab-recognizes-table.el
  8155. (if (org-at-table.el-p)
  8156. (progn
  8157. (beginning-of-line 1)
  8158. (if (looking-at org-table-dataline-regexp)
  8159. nil
  8160. (if (looking-at org-table1-hline-regexp)
  8161. (progn
  8162. (beginning-of-line 2)
  8163. (if (looking-at org-table-any-border-regexp)
  8164. (beginning-of-line -1)))))
  8165. (if (re-search-forward "|" (org-table-end t) t)
  8166. (progn
  8167. (require 'table)
  8168. (if (table--at-cell-p (point))
  8169. t
  8170. (message "recognizing table.el table...")
  8171. (table-recognize-table)
  8172. (message "recognizing table.el table...done")))
  8173. (error "This should not happen..."))
  8174. t)
  8175. nil)
  8176. nil))
  8177. (defun org-at-table-hline-p ()
  8178. "Return t if the cursor is inside a hline in a table."
  8179. (if org-enable-table-editor
  8180. (save-excursion
  8181. (beginning-of-line 1)
  8182. (looking-at org-table-hline-regexp))
  8183. nil))
  8184. (defun org-table-insert-column ()
  8185. "Insert a new column into the table."
  8186. (interactive)
  8187. (if (not (org-at-table-p))
  8188. (error "Not at a table"))
  8189. (org-table-find-dataline)
  8190. (let* ((col (max 1 (org-table-current-column)))
  8191. (beg (org-table-begin))
  8192. (end (org-table-end))
  8193. ;; Current cursor position
  8194. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8195. (colpos col))
  8196. (goto-char beg)
  8197. (while (< (point) end)
  8198. (if (org-at-table-hline-p)
  8199. nil
  8200. (org-table-goto-column col t)
  8201. (insert "| "))
  8202. (beginning-of-line 2))
  8203. (move-marker end nil)
  8204. (goto-line linepos)
  8205. (org-table-goto-column colpos)
  8206. (org-table-align)
  8207. (org-table-modify-formulas 'insert col)))
  8208. (defun org-table-find-dataline ()
  8209. "Find a dataline in the current table, which is needed for column commands."
  8210. (if (and (org-at-table-p)
  8211. (not (org-at-table-hline-p)))
  8212. t
  8213. (let ((col (current-column))
  8214. (end (org-table-end)))
  8215. (move-to-column col)
  8216. (while (and (< (point) end)
  8217. (or (not (= (current-column) col))
  8218. (org-at-table-hline-p)))
  8219. (beginning-of-line 2)
  8220. (move-to-column col))
  8221. (if (and (org-at-table-p)
  8222. (not (org-at-table-hline-p)))
  8223. t
  8224. (error
  8225. "Please position cursor in a data line for column operations")))))
  8226. (defun org-table-delete-column ()
  8227. "Delete a column into the table."
  8228. (interactive)
  8229. (if (not (org-at-table-p))
  8230. (error "Not at a table"))
  8231. (org-table-find-dataline)
  8232. (org-table-check-inside-data-field)
  8233. (let* ((col (org-table-current-column))
  8234. (beg (org-table-begin))
  8235. (end (org-table-end))
  8236. ;; Current cursor position
  8237. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8238. (colpos col))
  8239. (goto-char beg)
  8240. (while (< (point) end)
  8241. (if (org-at-table-hline-p)
  8242. nil
  8243. (org-table-goto-column col t)
  8244. (and (looking-at "|[^|\n]+|")
  8245. (replace-match "|")))
  8246. (beginning-of-line 2))
  8247. (move-marker end nil)
  8248. (goto-line linepos)
  8249. (org-table-goto-column colpos)
  8250. (org-table-align)
  8251. (org-table-modify-formulas 'remove col)))
  8252. (defun org-table-move-column-right ()
  8253. "Move column to the right."
  8254. (interactive)
  8255. (org-table-move-column nil))
  8256. (defun org-table-move-column-left ()
  8257. "Move column to the left."
  8258. (interactive)
  8259. (org-table-move-column 'left))
  8260. (defun org-table-move-column (&optional left)
  8261. "Move the current column to the right. With arg LEFT, move to the left."
  8262. (interactive "P")
  8263. (if (not (org-at-table-p))
  8264. (error "Not at a table"))
  8265. (org-table-find-dataline)
  8266. (org-table-check-inside-data-field)
  8267. (let* ((col (org-table-current-column))
  8268. (col1 (if left (1- col) col))
  8269. (beg (org-table-begin))
  8270. (end (org-table-end))
  8271. ;; Current cursor position
  8272. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8273. (colpos (if left (1- col) (1+ col))))
  8274. (if (and left (= col 1))
  8275. (error "Cannot move column further left"))
  8276. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  8277. (error "Cannot move column further right"))
  8278. (goto-char beg)
  8279. (while (< (point) end)
  8280. (if (org-at-table-hline-p)
  8281. nil
  8282. (org-table-goto-column col1 t)
  8283. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  8284. (replace-match "|\\2|\\1|")))
  8285. (beginning-of-line 2))
  8286. (move-marker end nil)
  8287. (goto-line linepos)
  8288. (org-table-goto-column colpos)
  8289. (org-table-align)
  8290. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  8291. (defun org-table-move-row-down ()
  8292. "Move table row down."
  8293. (interactive)
  8294. (org-table-move-row nil))
  8295. (defun org-table-move-row-up ()
  8296. "Move table row up."
  8297. (interactive)
  8298. (org-table-move-row 'up))
  8299. (defun org-table-move-row (&optional up)
  8300. "Move the current table line down. With arg UP, move it up."
  8301. (interactive "P")
  8302. (let ((col (current-column))
  8303. (pos (point))
  8304. (tonew (if up 0 2))
  8305. txt)
  8306. (beginning-of-line tonew)
  8307. (if (not (org-at-table-p))
  8308. (progn
  8309. (goto-char pos)
  8310. (error "Cannot move row further")))
  8311. (goto-char pos)
  8312. (beginning-of-line 1)
  8313. (setq pos (point))
  8314. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  8315. (delete-region (point) (1+ (point-at-eol)))
  8316. (beginning-of-line tonew)
  8317. (insert txt)
  8318. (beginning-of-line 0)
  8319. (move-to-column col)))
  8320. (defun org-table-insert-row (&optional arg)
  8321. "Insert a new row above the current line into the table.
  8322. With prefix ARG, insert below the current line."
  8323. (interactive "P")
  8324. (if (not (org-at-table-p))
  8325. (error "Not at a table"))
  8326. (let* ((line
  8327. (org-expand-wide-chars
  8328. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  8329. new)
  8330. (if (string-match "^[ \t]*|-" line)
  8331. (setq new (mapcar (lambda (x) (if (member x '(?| ?+)) ?| ?\ )) line))
  8332. (setq new (mapcar (lambda (x) (if (equal x ?|) ?| ?\ )) line)))
  8333. ;; Fix the first field if necessary
  8334. (setq new (concat new))
  8335. (if (string-match "^[ \t]*| *[#$] *|" line)
  8336. (setq new (replace-match (match-string 0 line) t t new)))
  8337. (beginning-of-line (if arg 2 1))
  8338. (let (org-table-may-need-update)
  8339. (insert-before-markers new)
  8340. (insert-before-markers "\n"))
  8341. (beginning-of-line 0)
  8342. (re-search-forward "| ?" (point-at-eol) t)
  8343. (and org-table-may-need-update (org-table-align))))
  8344. (defun org-table-insert-hline (&optional arg)
  8345. "Insert a horizontal-line below the current line into the table.
  8346. With prefix ARG, insert above the current line."
  8347. (interactive "P")
  8348. (if (not (org-at-table-p))
  8349. (error "Not at a table"))
  8350. (let ((line
  8351. (org-expand-wide-chars
  8352. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  8353. (col (current-column))
  8354. start)
  8355. (if (string-match "^[ \t]*|-" line)
  8356. (setq line
  8357. (mapcar (lambda (x) (if (member x '(?| ?+))
  8358. (prog1 (if start ?+ ?|) (setq start t))
  8359. (if start ?- ?\ )))
  8360. line))
  8361. (setq line
  8362. (mapcar (lambda (x) (if (equal x ?|)
  8363. (prog1 (if start ?+ ?|) (setq start t))
  8364. (if start ?- ?\ )))
  8365. line)))
  8366. (beginning-of-line (if arg 1 2))
  8367. (apply 'insert line)
  8368. (if (equal (char-before (point)) ?+)
  8369. (progn (backward-delete-char 1) (insert "|")))
  8370. (insert "\n")
  8371. (beginning-of-line (if arg 1 -1))
  8372. (move-to-column col)))
  8373. (defun org-expand-wide-chars (s)
  8374. "Expand wide characters to spaces."
  8375. (let (w a)
  8376. (mapconcat
  8377. (lambda (x)
  8378. (if (> (setq w (string-width (setq a (char-to-string x)))) 1)
  8379. (make-string w ?\ )
  8380. a))
  8381. s "")))
  8382. (defun org-table-kill-row ()
  8383. "Delete the current row or horizontal line from the table."
  8384. (interactive)
  8385. (if (not (org-at-table-p))
  8386. (error "Not at a table"))
  8387. (let ((col (current-column)))
  8388. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  8389. (if (not (org-at-table-p)) (beginning-of-line 0))
  8390. (move-to-column col)))
  8391. (defun org-table-sort-lines (beg end numericp)
  8392. "Sort table lines in region.
  8393. Point and mark define the first and last line to include. Both point and
  8394. mark should be in the column that is used for sorting. For example, to
  8395. sort according to column 3, put the mark in the first line to sort, in
  8396. table column 3. Put point into the last line to be included in the sorting,
  8397. also in table column 3. The command will prompt for the sorting method
  8398. \(n for numerical, a for alphanumeric)."
  8399. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  8400. (setq numericp (string-match "[nN]" numericp))
  8401. (org-table-align) ;; Just to be safe
  8402. (let* (bcol ecol cmp column lns)
  8403. (goto-char beg)
  8404. (org-table-check-inside-data-field)
  8405. (setq column (org-table-current-column)
  8406. beg (move-marker (make-marker) (point-at-bol)))
  8407. (goto-char end)
  8408. (org-table-check-inside-data-field)
  8409. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  8410. (untabify beg end)
  8411. (goto-char beg)
  8412. (org-table-goto-column column)
  8413. (skip-chars-backward "^|")
  8414. (setq bcol (current-column))
  8415. (org-table-goto-column (1+ column))
  8416. (skip-chars-backward "^|")
  8417. (setq ecol (1- (current-column)))
  8418. (setq cmp (if numericp
  8419. (lambda (a b) (< (car a) (car b)))
  8420. (lambda (a b) (string< (car a) (car b)))))
  8421. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  8422. (split-string (buffer-substring beg end) "\n")))
  8423. (if numericp
  8424. (setq lns (mapcar (lambda(x)
  8425. (cons (string-to-number (car x)) (cdr x)))
  8426. lns)))
  8427. (delete-region beg end)
  8428. (move-marker beg nil)
  8429. (move-marker end nil)
  8430. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  8431. (message "%d lines sorted %s based on column %d"
  8432. (length lns)
  8433. (if numericp "numerically" "alphabetically") column)))
  8434. (defun org-table-cut-region (beg end)
  8435. "Copy region in table to the clipboard and blank all relevant fields."
  8436. (interactive "r")
  8437. (org-table-copy-region beg end 'cut))
  8438. (defun org-table-copy-region (beg end &optional cut)
  8439. "Copy rectangular region in table to clipboard.
  8440. A special clipboard is used which can only be accessed
  8441. with `org-table-paste-rectangle'."
  8442. (interactive "rP")
  8443. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  8444. region cols
  8445. (rpl (if cut " " nil)))
  8446. (goto-char beg)
  8447. (org-table-check-inside-data-field)
  8448. (setq l01 (count-lines (point-min) (point))
  8449. c01 (org-table-current-column))
  8450. (goto-char end)
  8451. (org-table-check-inside-data-field)
  8452. (setq l02 (count-lines (point-min) (point))
  8453. c02 (org-table-current-column))
  8454. (setq l1 (min l01 l02) l2 (max l01 l02)
  8455. c1 (min c01 c02) c2 (max c01 c02))
  8456. (catch 'exit
  8457. (while t
  8458. (catch 'nextline
  8459. (if (> l1 l2) (throw 'exit t))
  8460. (goto-line l1)
  8461. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  8462. (setq cols nil ic1 c1 ic2 c2)
  8463. (while (< ic1 (1+ ic2))
  8464. (push (org-table-get-field ic1 rpl) cols)
  8465. (setq ic1 (1+ ic1)))
  8466. (push (nreverse cols) region)
  8467. (setq l1 (1+ l1)))))
  8468. (setq org-table-clip (nreverse region))
  8469. (if cut (org-table-align))
  8470. org-table-clip))
  8471. (defun org-table-paste-rectangle ()
  8472. "Paste a rectangular region into a table.
  8473. The upper right corner ends up in the current field. All involved fields
  8474. will be overwritten. If the rectangle does not fit into the present table,
  8475. the table is enlarged as needed. The process ignores horizontal separator
  8476. lines."
  8477. (interactive)
  8478. (unless (and org-table-clip (listp org-table-clip))
  8479. (error "First cut/copy a region to paste!"))
  8480. (org-table-check-inside-data-field)
  8481. (let* ((clip org-table-clip)
  8482. (line (count-lines (point-min) (point)))
  8483. (col (org-table-current-column))
  8484. (org-enable-table-editor t)
  8485. (org-table-automatic-realign nil)
  8486. c cols field)
  8487. (while (setq cols (pop clip))
  8488. (while (org-at-table-hline-p) (beginning-of-line 2))
  8489. (if (not (org-at-table-p))
  8490. (progn (end-of-line 0) (org-table-next-field)))
  8491. (setq c col)
  8492. (while (setq field (pop cols))
  8493. (org-table-goto-column c nil 'force)
  8494. (org-table-get-field nil field)
  8495. (setq c (1+ c)))
  8496. (beginning-of-line 2))
  8497. (goto-line line)
  8498. (org-table-goto-column col)
  8499. (org-table-align)))
  8500. (defun org-table-convert ()
  8501. "Convert from `org-mode' table to table.el and back.
  8502. Obviously, this only works within limits. When an Org-mode table is
  8503. converted to table.el, all horizontal separator lines get lost, because
  8504. table.el uses these as cell boundaries and has no notion of horizontal lines.
  8505. A table.el table can be converted to an Org-mode table only if it does not
  8506. do row or column spanning. Multiline cells will become multiple cells.
  8507. Beware, Org-mode does not test if the table can be successfully converted - it
  8508. blindly applies a recipe that works for simple tables."
  8509. (interactive)
  8510. (require 'table)
  8511. (if (org-at-table.el-p)
  8512. ;; convert to Org-mode table
  8513. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  8514. (end (move-marker (make-marker) (org-table-end t))))
  8515. (table-unrecognize-region beg end)
  8516. (goto-char beg)
  8517. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  8518. (replace-match ""))
  8519. (goto-char beg))
  8520. (if (org-at-table-p)
  8521. ;; convert to table.el table
  8522. (let ((beg (move-marker (make-marker) (org-table-begin)))
  8523. (end (move-marker (make-marker) (org-table-end))))
  8524. ;; first, get rid of all horizontal lines
  8525. (goto-char beg)
  8526. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  8527. (replace-match ""))
  8528. ;; insert a hline before first
  8529. (goto-char beg)
  8530. (org-table-insert-hline 'above)
  8531. (beginning-of-line -1)
  8532. ;; insert a hline after each line
  8533. (while (progn (beginning-of-line 3) (< (point) end))
  8534. (org-table-insert-hline))
  8535. (goto-char beg)
  8536. (setq end (move-marker end (org-table-end)))
  8537. ;; replace "+" at beginning and ending of hlines
  8538. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  8539. (replace-match "\\1+-"))
  8540. (goto-char beg)
  8541. (while (re-search-forward "-|[ \t]*$" end t)
  8542. (replace-match "-+"))
  8543. (goto-char beg)))))
  8544. (defun org-table-wrap-region (arg)
  8545. "Wrap several fields in a column like a paragraph.
  8546. This is useful if you'd like to spread the contents of a field over several
  8547. lines, in order to keep the table compact.
  8548. If there is an active region, and both point and mark are in the same column,
  8549. the text in the column is wrapped to minimum width for the given number of
  8550. lines. Generally, this makes the table more compact. A prefix ARG may be
  8551. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  8552. formats the selected text to two lines. If the region was longer than two
  8553. lines, the remaining lines remain empty. A negative prefix argument reduces
  8554. the current number of lines by that amount. The wrapped text is pasted back
  8555. into the table. If you formatted it to more lines than it was before, fields
  8556. further down in the table get overwritten - so you might need to make space in
  8557. the table first.
  8558. If there is no region, the current field is split at the cursor position and
  8559. the text fragment to the right of the cursor is prepended to the field one
  8560. line down.
  8561. If there is no region, but you specify a prefix ARG, the current field gets
  8562. blank, and the content is appended to the field above."
  8563. (interactive "P")
  8564. (org-table-check-inside-data-field)
  8565. (if (org-region-active-p)
  8566. ;; There is a region: fill as a paragraph
  8567. (let ((beg (region-beginning))
  8568. nlines)
  8569. (org-table-cut-region (region-beginning) (region-end))
  8570. (if (> (length (car org-table-clip)) 1)
  8571. (error "Region must be limited to single column"))
  8572. (setq nlines (if arg
  8573. (if (< arg 1)
  8574. (+ (length org-table-clip) arg)
  8575. arg)
  8576. (length org-table-clip)))
  8577. (setq org-table-clip
  8578. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  8579. nil nlines)))
  8580. (goto-char beg)
  8581. (org-table-paste-rectangle))
  8582. ;; No region, split the current field at point
  8583. (if arg
  8584. ;; combine with field above
  8585. (let ((s (org-table-blank-field))
  8586. (col (org-table-current-column)))
  8587. (beginning-of-line 0)
  8588. (while (org-at-table-hline-p) (beginning-of-line 0))
  8589. (org-table-goto-column col)
  8590. (skip-chars-forward "^|")
  8591. (skip-chars-backward " ")
  8592. (insert " " (org-trim s))
  8593. (org-table-align))
  8594. ;; split field
  8595. (when (looking-at "\\([^|]+\\)+|")
  8596. (let ((s (match-string 1)))
  8597. (replace-match " |")
  8598. (goto-char (match-beginning 0))
  8599. (org-table-next-row)
  8600. (insert (org-trim s) " ")
  8601. (org-table-align))))))
  8602. (defvar org-field-marker nil)
  8603. ;; FIXME: Put documentation into the editing buffer.
  8604. (defun org-table-edit-field ()
  8605. "Edit table field in a different buffer.
  8606. Mainly useful for fields that contain hidden parts."
  8607. (interactive)
  8608. (let ((pos (move-marker (make-marker) (point)))
  8609. (field (org-table-get-field))
  8610. (cw (current-window-configuration)))
  8611. (switch-to-buffer-other-window "*Org tmp*")
  8612. (erase-buffer)
  8613. (org-mode)
  8614. (insert (org-trim field))
  8615. (remove-text-properties (point-min) (point-max)
  8616. '(invisible t org-cwidth t display t))
  8617. (goto-char (point-min))
  8618. (set (make-local-variable 'org-finish-function)
  8619. 'org-table-finish-edit-field)
  8620. (set (make-local-variable 'org-window-configuration) cw)
  8621. (set (make-local-variable 'org-field-marker) pos)
  8622. (message "Edit and finish with C-c C-c")))
  8623. (defun org-table-finish-edit-field ()
  8624. "Finish editing a table data field.
  8625. Remove all newline characters, insert the result into the table, realign
  8626. the table and kill the editing buffer."
  8627. (let ((pos org-field-marker)
  8628. (cw org-window-configuration)
  8629. (cb (current-buffer))
  8630. text)
  8631. (goto-char (point-min))
  8632. (while (re-search-forward "[ \t]*\n[ \t]*" nil t)
  8633. (replace-match " "))
  8634. (setq text (org-trim (buffer-string)))
  8635. (set-window-configuration cw)
  8636. (kill-buffer cb)
  8637. (select-window (get-buffer-window (marker-buffer pos)))
  8638. (goto-char pos)
  8639. (move-marker pos nil)
  8640. (org-table-check-inside-data-field)
  8641. (org-table-get-field nil text)
  8642. (org-table-align)
  8643. (message "New field value inserted")))
  8644. (defun org-trim (s)
  8645. "Remove whitespace at beginning and end of string."
  8646. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  8647. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  8648. s)
  8649. (defun org-wrap (string &optional width lines)
  8650. "Wrap string to either a number of lines, or a width in characters.
  8651. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  8652. that costs. If there is a word longer than WIDTH, the text is actually
  8653. wrapped to the length of that word.
  8654. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  8655. many lines, whatever width that takes.
  8656. The return value is a list of lines, without newlines at the end."
  8657. (let* ((words (org-split-string string "[ \t\n]+"))
  8658. (maxword (apply 'max (mapcar 'org-string-width words)))
  8659. w ll)
  8660. (cond (width
  8661. (org-do-wrap words (max maxword width)))
  8662. (lines
  8663. (setq w maxword)
  8664. (setq ll (org-do-wrap words maxword))
  8665. (if (<= (length ll) lines)
  8666. ll
  8667. (setq ll words)
  8668. (while (> (length ll) lines)
  8669. (setq w (1+ w))
  8670. (setq ll (org-do-wrap words w)))
  8671. ll))
  8672. (t (error "Cannot wrap this")))))
  8673. (defun org-do-wrap (words width)
  8674. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  8675. (let (lines line)
  8676. (while words
  8677. (setq line (pop words))
  8678. (while (and words (< (+ (length line) (length (car words))) width))
  8679. (setq line (concat line " " (pop words))))
  8680. (setq lines (push line lines)))
  8681. (nreverse lines)))
  8682. ;; FIXME: I think I can make this more efficient
  8683. (defun org-split-string (string &optional separators)
  8684. "Splits STRING into substrings at SEPARATORS.
  8685. No empty strings are returned if there are matches at the beginning
  8686. and end of string."
  8687. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  8688. (start 0)
  8689. notfirst
  8690. (list nil))
  8691. (while (and (string-match rexp string
  8692. (if (and notfirst
  8693. (= start (match-beginning 0))
  8694. (< start (length string)))
  8695. (1+ start) start))
  8696. (< (match-beginning 0) (length string)))
  8697. (setq notfirst t)
  8698. (or (eq (match-beginning 0) 0)
  8699. (and (eq (match-beginning 0) (match-end 0))
  8700. (eq (match-beginning 0) start))
  8701. (setq list
  8702. (cons (substring string start (match-beginning 0))
  8703. list)))
  8704. (setq start (match-end 0)))
  8705. (or (eq start (length string))
  8706. (setq list
  8707. (cons (substring string start)
  8708. list)))
  8709. (nreverse list)))
  8710. (defun org-table-add-invisible-to-vertical-lines ()
  8711. "Add an `invisible' property to vertical lines of current table."
  8712. (interactive)
  8713. (let* ((beg (org-table-begin))
  8714. (end (org-table-end))
  8715. (end1))
  8716. (save-excursion
  8717. (goto-char beg)
  8718. (while (< (point) end)
  8719. (setq end1 (point-at-eol))
  8720. (if (looking-at org-table-dataline-regexp)
  8721. (while (re-search-forward "|" end1 t)
  8722. (add-text-properties (1- (point)) (point)
  8723. '(invisible org-table)))
  8724. (while (re-search-forward "[+|]" end1 t)
  8725. (add-text-properties (1- (point)) (point)
  8726. '(invisible org-table))))
  8727. (beginning-of-line 2)))))
  8728. (defun org-table-toggle-vline-visibility (&optional arg)
  8729. "Toggle the visibility of table vertical lines.
  8730. The effect is immediate and on all tables in the file.
  8731. With prefix ARG, make lines invisible when ARG is positive, make lines
  8732. visible when ARG is not positive."
  8733. (interactive "P")
  8734. (let ((action (cond
  8735. ((and arg (> (prefix-numeric-value arg) 0)) 'on)
  8736. ((and arg (< (prefix-numeric-value arg) 1)) 'off)
  8737. (t (if (org-in-invisibility-spec-p '(org-table))
  8738. 'off
  8739. 'on)))))
  8740. (if (eq action 'off)
  8741. (progn
  8742. (org-remove-from-invisibility-spec '(org-table))
  8743. (org-table-map-tables 'org-table-align)
  8744. (message "Vertical table lines visible")
  8745. (if (org-at-table-p)
  8746. (org-table-align)))
  8747. (org-add-to-invisibility-spec '(org-table))
  8748. (org-table-map-tables 'org-table-align)
  8749. (message "Vertical table lines invisible"))
  8750. (redraw-frame (selected-frame))))
  8751. (defun org-table-map-tables (function)
  8752. "Apply FUNCTION to the start of all tables in the buffer."
  8753. (save-excursion
  8754. (save-restriction
  8755. (widen)
  8756. (goto-char (point-min))
  8757. (while (re-search-forward org-table-any-line-regexp nil t)
  8758. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  8759. (beginning-of-line 1)
  8760. (if (looking-at org-table-line-regexp)
  8761. (save-excursion (funcall function)))
  8762. (re-search-forward org-table-any-border-regexp nil 1))))
  8763. (message "Mapping tables: done"))
  8764. (defun org-table-sum (&optional beg end nlast)
  8765. "Sum numbers in region of current table column.
  8766. The result will be displayed in the echo area, and will be available
  8767. as kill to be inserted with \\[yank].
  8768. If there is an active region, it is interpreted as a rectangle and all
  8769. numbers in that rectangle will be summed. If there is no active
  8770. region and point is located in a table column, sum all numbers in that
  8771. column.
  8772. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  8773. numbers are assumed to be times as well (in decimal hours) and the
  8774. numbers are added as such.
  8775. If NLAST is a number, only the NLAST fields will actually be summed."
  8776. (interactive)
  8777. (save-excursion
  8778. (let (col (timecnt 0) diff h m s org-table-clip)
  8779. (cond
  8780. ((and beg end)) ; beg and end given explicitly
  8781. ((org-region-active-p)
  8782. (setq beg (region-beginning) end (region-end)))
  8783. (t
  8784. (setq col (org-table-current-column))
  8785. (goto-char (org-table-begin))
  8786. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  8787. (error "No table data"))
  8788. (org-table-goto-column col)
  8789. ;not needed? (skip-chars-backward "^|")
  8790. (setq beg (point))
  8791. (goto-char (org-table-end))
  8792. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  8793. (error "No table data"))
  8794. (org-table-goto-column col)
  8795. ;not needed? (skip-chars-forward "^|")
  8796. (setq end (point))))
  8797. (let* ((items (apply 'append (org-table-copy-region beg end)))
  8798. (items1 (cond ((not nlast) items)
  8799. ((>= nlast (length items)) items)
  8800. (t (setq items (reverse items))
  8801. (setcdr (nthcdr (1- nlast) items) nil)
  8802. (nreverse items))))
  8803. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  8804. items1)))
  8805. (res (apply '+ numbers))
  8806. (sres (if (= timecnt 0)
  8807. (format "%g" res)
  8808. (setq diff (* 3600 res)
  8809. h (floor (/ diff 3600)) diff (mod diff 3600)
  8810. m (floor (/ diff 60)) diff (mod diff 60)
  8811. s diff)
  8812. (format "%d:%02d:%02d" h m s))))
  8813. (kill-new sres)
  8814. (if (interactive-p)
  8815. (message "%s"
  8816. (substitute-command-keys
  8817. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  8818. (length numbers) sres))))
  8819. sres))))
  8820. (defun org-table-get-number-for-summing (s)
  8821. (let (n)
  8822. (if (string-match "^ *|? *" s)
  8823. (setq s (replace-match "" nil nil s)))
  8824. (if (string-match " *|? *$" s)
  8825. (setq s (replace-match "" nil nil s)))
  8826. (setq n (string-to-number s))
  8827. (cond
  8828. ((and (string-match "0" s)
  8829. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  8830. ((string-match "\\`[ \t]+\\'" s) nil)
  8831. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  8832. (let ((h (string-to-number (or (match-string 1 s) "0")))
  8833. (m (string-to-number (or (match-string 2 s) "0")))
  8834. (s (string-to-number (or (match-string 4 s) "0"))))
  8835. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  8836. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  8837. ((equal n 0) nil)
  8838. (t n))))
  8839. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  8840. "Get a calc vector from a column, accorting to descriptor DESC.
  8841. Optional arguments TBEG and COL can give the beginning of the table and
  8842. the current column, to avoid unnecessary parsing."
  8843. (save-excursion
  8844. (or tbeg (setq tbeg (org-table-begin)))
  8845. (or col (setq col (org-table-current-column)))
  8846. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  8847. (cond
  8848. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  8849. (setq n1 (- (match-end 1) (match-beginning 1)))
  8850. (if (match-beginning 3)
  8851. (setq n2 (- (match-end 2) (match-beginning 3))))
  8852. (setq n (if n2 (max n1 n2) n1))
  8853. (setq n1 (if n2 (min n1 n2)))
  8854. (setq nn n)
  8855. (while (and (> nn 0)
  8856. (re-search-backward org-table-hline-regexp tbeg t))
  8857. (push (org-current-line) hline-list)
  8858. (setq nn (1- nn)))
  8859. (setq hline-list (nreverse hline-list))
  8860. (goto-line (nth (1- n) hline-list))
  8861. (when (re-search-forward org-table-dataline-regexp)
  8862. (org-table-goto-column col)
  8863. (setq beg (point)))
  8864. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  8865. (when (re-search-backward org-table-dataline-regexp)
  8866. (org-table-goto-column col)
  8867. (setq end (point)))
  8868. (setq l (apply 'append (org-table-copy-region beg end)))
  8869. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  8870. (if (equal x "") "0" x))
  8871. l ",") "]"))
  8872. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  8873. (setq n1 (string-to-number (match-string 1 desc))
  8874. n2 (string-to-number (match-string 2 desc)))
  8875. (beginning-of-line 1)
  8876. (save-excursion
  8877. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  8878. (org-table-goto-column col)
  8879. (setq beg (point))))
  8880. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  8881. (org-table-goto-column col)
  8882. (setq end (point)))
  8883. (setq l (apply 'append (org-table-copy-region beg end)))
  8884. (concat "[" (mapconcat
  8885. (lambda (x) (setq x (org-trim x))
  8886. (if (equal x "") "0" x))
  8887. l ",") "]"))
  8888. ((string-match "\\([0-9]+\\)" desc)
  8889. (beginning-of-line 1)
  8890. (when (re-search-backward org-table-dataline-regexp tbeg t
  8891. (string-to-number (match-string 0 desc)))
  8892. (org-table-goto-column col)
  8893. (org-trim (org-table-get-field))))))))
  8894. (defvar org-table-formula-history nil)
  8895. (defvar org-table-column-names nil
  8896. "Alist with column names, derived from the `!' line.")
  8897. (defvar org-table-column-name-regexp nil
  8898. "Regular expression matching the current column names.")
  8899. (defvar org-table-local-parameters nil
  8900. "Alist with parameter names, derived from the `$' line.")
  8901. (defvar org-table-named-field-locations nil
  8902. "Alist with locations of named fields.")
  8903. (defun org-table-get-formula (&optional equation named)
  8904. "Read a formula from the minibuffer, offer stored formula as default."
  8905. (let* ((name (car (rassoc (list (org-current-line)
  8906. (org-table-current-column))
  8907. org-table-named-field-locations)))
  8908. (scol (if named
  8909. (if name name
  8910. (error "Not in a named field"))
  8911. (int-to-string (org-table-current-column))))
  8912. (dummy (and name (not named)
  8913. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  8914. (error "Abort")))
  8915. (org-table-may-need-update nil)
  8916. (stored-list (org-table-get-stored-formulas))
  8917. (stored (cdr (assoc scol stored-list)))
  8918. (eq (cond
  8919. ((and stored equation (string-match "^ *=? *$" equation))
  8920. stored)
  8921. ((stringp equation)
  8922. equation)
  8923. (t (read-string
  8924. (format "%s formula $%s=" (if named "Field" "Column") scol)
  8925. (or stored "") 'org-table-formula-history
  8926. ;stored
  8927. ))))
  8928. mustsave)
  8929. (when (not (string-match "\\S-" eq))
  8930. ;; remove formula
  8931. (setq stored-list (delq (assoc scol stored-list) stored-list))
  8932. (org-table-store-formulas stored-list)
  8933. (error "Formula removed"))
  8934. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  8935. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  8936. (if (and name (not named))
  8937. ;; We set the column equation, delete the named one.
  8938. (setq stored-list (delq (assoc name stored-list) stored-list)
  8939. mustsave t))
  8940. (if stored
  8941. (setcdr (assoc scol stored-list) eq)
  8942. (setq stored-list (cons (cons scol eq) stored-list)))
  8943. (if (or mustsave (not (equal stored eq)))
  8944. (org-table-store-formulas stored-list))
  8945. eq))
  8946. (defun org-table-store-formulas (alist)
  8947. "Store the list of formulas below the current table."
  8948. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  8949. (save-excursion
  8950. (goto-char (org-table-end))
  8951. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  8952. (delete-region (point) (match-end 0)))
  8953. (insert "#+TBLFM: "
  8954. (mapconcat (lambda (x)
  8955. (concat "$" (car x) "=" (cdr x)))
  8956. alist "::")
  8957. "\n")))
  8958. (defun org-table-get-stored-formulas ()
  8959. "Return an alist with the t=stored formulas directly after current table."
  8960. (interactive)
  8961. (let (scol eq eq-alist strings string seen)
  8962. (save-excursion
  8963. (goto-char (org-table-end))
  8964. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  8965. (setq strings (org-split-string (match-string 2) " *:: *"))
  8966. (while (setq string (pop strings))
  8967. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  8968. (setq scol (match-string 1 string)
  8969. eq (match-string 2 string)
  8970. eq-alist (cons (cons scol eq) eq-alist))
  8971. (if (member scol seen)
  8972. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  8973. (push scol seen))))))
  8974. (nreverse eq-alist)))
  8975. (defun org-table-modify-formulas (action &rest columns)
  8976. "Modify the formulas stored below the current table.
  8977. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  8978. expected, for the other actions only a single column number is needed."
  8979. (let ((list (org-table-get-stored-formulas))
  8980. (nmax (length (org-split-string
  8981. (buffer-substring (point-at-bol) (point-at-eol))
  8982. "|")))
  8983. col col1 col2 scol si sc1 sc2)
  8984. (cond
  8985. ((null list)) ; No action needed if there are no stored formulas
  8986. ((eq action 'remove)
  8987. (setq col (car columns)
  8988. scol (int-to-string col))
  8989. (org-table-replace-in-formulas list scol "INVALID")
  8990. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  8991. (loop for i from (1+ col) upto nmax by 1 do
  8992. (setq si (int-to-string i))
  8993. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  8994. (if (assoc si list) (setcar (assoc si list)
  8995. (int-to-string (1- i))))))
  8996. ((eq action 'insert)
  8997. (setq col (car columns))
  8998. (loop for i from nmax downto col by 1 do
  8999. (setq si (int-to-string i))
  9000. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  9001. (if (assoc si list) (setcar (assoc si list)
  9002. (int-to-string (1+ i))))))
  9003. ((eq action 'swap)
  9004. (setq col1 (car columns) col2 (nth 1 columns)
  9005. sc1 (int-to-string col1) sc2 (int-to-string col2))
  9006. ;; Hopefully, ZqZ will never be a name in a table... FIXME:
  9007. (org-table-replace-in-formulas list sc1 "ZqZ")
  9008. (org-table-replace-in-formulas list sc2 sc1)
  9009. (org-table-replace-in-formulas list "ZqZ" sc2)
  9010. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZ"))
  9011. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  9012. (if (assoc "ZqZ" list) (setcar (assoc "ZqZ" list) sc2)))
  9013. (t (error "Invalid action in `org-table-modify-formulas'")))
  9014. (if list (org-table-store-formulas list))))
  9015. (defun org-table-replace-in-formulas (list s1 s2)
  9016. (let (elt re s)
  9017. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  9018. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  9019. re (concat (regexp-quote s1) "\\>"))
  9020. (while (setq elt (pop list))
  9021. (setq s (cdr elt))
  9022. (while (string-match re s)
  9023. (setq s (replace-match s2 t t s)))
  9024. (setcdr elt s))))
  9025. (defun org-table-get-specials ()
  9026. "Get the column names and local parameters for this table."
  9027. (save-excursion
  9028. (let ((beg (org-table-begin)) (end (org-table-end))
  9029. names name fields fields1 field cnt c v line col)
  9030. (setq org-table-column-names nil
  9031. org-table-local-parameters nil
  9032. org-table-named-field-locations nil)
  9033. (goto-char beg)
  9034. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  9035. (setq names (org-split-string (match-string 1) " *| *")
  9036. cnt 1)
  9037. (while (setq name (pop names))
  9038. (setq cnt (1+ cnt))
  9039. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  9040. (push (cons name (int-to-string cnt)) org-table-column-names))))
  9041. (setq org-table-column-names (nreverse org-table-column-names))
  9042. (setq org-table-column-name-regexp
  9043. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  9044. (goto-char beg)
  9045. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  9046. (setq fields (org-split-string (match-string 1) " *| *"))
  9047. (while (setq field (pop fields))
  9048. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  9049. (push (cons (match-string 1 field) (match-string 2 field))
  9050. org-table-local-parameters))))
  9051. (goto-char beg)
  9052. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  9053. (setq c (match-string 1)
  9054. fields (org-split-string (match-string 2) " *| *"))
  9055. (save-excursion
  9056. (beginning-of-line (if (equal c "_") 2 0))
  9057. (setq line (org-current-line) col 1)
  9058. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  9059. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  9060. (while (and fields1 (setq field (pop fields)))
  9061. (setq v (pop fields1) col (1+ col))
  9062. (when (and (stringp field) (stringp v)
  9063. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  9064. (push (cons field v) org-table-local-parameters)
  9065. (push (list field line col) org-table-named-field-locations)))))))
  9066. (defun org-this-word ()
  9067. ;; Get the current word
  9068. (save-excursion
  9069. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  9070. (end (progn (skip-chars-forward "^ \t\n") (point))))
  9071. (buffer-substring-no-properties beg end))))
  9072. (defun org-table-maybe-eval-formula ()
  9073. "Check if the current field starts with \"=\" or \":=\".
  9074. If yes, store the formula and apply it."
  9075. ;; We already know we are in a table. Get field will only return a formula
  9076. ;; when appropriate. It might return a separator line, but no problem.
  9077. (when org-table-formula-evaluate-inline
  9078. (let* ((field (org-trim (or (org-table-get-field) "")))
  9079. named eq)
  9080. (when (string-match "^:?=\\(.*\\)" field)
  9081. (setq named (equal (string-to-char field) ?:)
  9082. eq (match-string 1 field))
  9083. (if (fboundp 'calc-eval)
  9084. (org-table-eval-formula (if named '(4) nil) eq))))))
  9085. (defvar org-recalc-commands nil
  9086. "List of commands triggering the recalculation of a line.
  9087. Will be filled automatically during use.")
  9088. (defvar org-recalc-marks
  9089. '((" " . "Unmarked: no special line, no automatic recalculation")
  9090. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  9091. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  9092. ("!" . "Column name definition line. Reference in formula as $name.")
  9093. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  9094. ("_" . "Names for values in row below this one.")
  9095. ("^" . "Names for values in row above this one.")))
  9096. (defun org-table-rotate-recalc-marks (&optional newchar)
  9097. "Rotate the recalculation mark in the first column.
  9098. If in any row, the first field is not consistent with a mark,
  9099. insert a new column for the markers.
  9100. When there is an active region, change all the lines in the region,
  9101. after prompting for the marking character.
  9102. After each change, a message will be displayed indicating the meaning
  9103. of the new mark."
  9104. (interactive)
  9105. (unless (org-at-table-p) (error "Not at a table"))
  9106. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  9107. (beg (org-table-begin))
  9108. (end (org-table-end))
  9109. (l (org-current-line))
  9110. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  9111. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  9112. (have-col
  9113. (save-excursion
  9114. (goto-char beg)
  9115. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  9116. (col (org-table-current-column))
  9117. (forcenew (car (assoc newchar org-recalc-marks)))
  9118. epos new)
  9119. (when l1
  9120. (message "Change region to what mark? Type # * ! $ or SPC: ")
  9121. (setq newchar (char-to-string (read-char-exclusive))
  9122. forcenew (car (assoc newchar org-recalc-marks))))
  9123. (if (and newchar (not forcenew))
  9124. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  9125. newchar))
  9126. (if l1 (goto-line l1))
  9127. (save-excursion
  9128. (beginning-of-line 1)
  9129. (unless (looking-at org-table-dataline-regexp)
  9130. (error "Not at a table data line")))
  9131. (unless have-col
  9132. (org-table-goto-column 1)
  9133. (org-table-insert-column)
  9134. (org-table-goto-column (1+ col)))
  9135. (setq epos (point-at-eol))
  9136. (save-excursion
  9137. (beginning-of-line 1)
  9138. (org-table-get-field
  9139. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  9140. (concat " "
  9141. (setq new (or forcenew
  9142. (cadr (member (match-string 1) marks))))
  9143. " ")
  9144. " # ")))
  9145. (if (and l1 l2)
  9146. (progn
  9147. (goto-line l1)
  9148. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  9149. (and (looking-at org-table-dataline-regexp)
  9150. (org-table-get-field 1 (concat " " new " "))))
  9151. (goto-line l1)))
  9152. (if (not (= epos (point-at-eol))) (org-table-align))
  9153. (goto-line l)
  9154. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  9155. (defun org-table-maybe-recalculate-line ()
  9156. "Recompute the current line if marked for it, and if we haven't just done it."
  9157. (interactive)
  9158. (and org-table-allow-automatic-line-recalculation
  9159. (not (and (memq last-command org-recalc-commands)
  9160. (equal org-last-recalc-line (org-current-line))))
  9161. (save-excursion (beginning-of-line 1)
  9162. (looking-at org-table-auto-recalculate-regexp))
  9163. (fboundp 'calc-eval)
  9164. (org-table-recalculate) t))
  9165. (defvar org-table-formula-debug nil
  9166. "Non-nil means, debug table formulas.
  9167. When nil, simply write \"#ERROR\" in corrupted fields.")
  9168. (defvar modes)
  9169. (defsubst org-set-calc-mode (var &optional value)
  9170. (if (stringp var)
  9171. (setq var (assoc var '(("D" calc-angle-mode deg)
  9172. ("R" calc-angle-mode rad)
  9173. ("F" calc-prefer-frac t)
  9174. ("S" calc-symbolic-mode t)))
  9175. value (nth 2 var) var (nth 1 var)))
  9176. (if (memq var modes)
  9177. (setcar (cdr (memq var modes)) value)
  9178. (cons var (cons value modes)))
  9179. modes)
  9180. (defun org-table-eval-formula (&optional arg equation
  9181. suppress-align suppress-const
  9182. suppress-store)
  9183. "Replace the table field value at the cursor by the result of a calculation.
  9184. This function makes use of Dave Gillespie's Calc package, in my view the
  9185. most exciting program ever written for GNU Emacs. So you need to have Calc
  9186. installed in order to use this function.
  9187. In a table, this command replaces the value in the current field with the
  9188. result of a formula. It also installs the formula as the \"current\" column
  9189. formula, by storing it in a special line below the table. When called
  9190. with a `C-u' prefix, the current field must ba a named field, and the
  9191. formula is installed as valid in only this specific field.
  9192. When called, the command first prompts for a formula, which is read in
  9193. the minibuffer. Previously entered formulas are available through the
  9194. history list, and the last used formula is offered as a default.
  9195. These stored formulas are adapted correctly when moving, inserting, or
  9196. deleting columns with the corresponding commands.
  9197. The formula can be any algebraic expression understood by the Calc package.
  9198. For details, see the Org-mode manual.
  9199. This function can also be called from Lisp programs and offers
  9200. additional arguments: EQUATION can be the formula to apply. If this
  9201. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  9202. used to speed-up recursive calls by by-passing unnecessary aligns.
  9203. SUPPRESS-CONST suppresses the interpretation of constants in the
  9204. formula, assuming that this has been done already outside the function.
  9205. SUPPRESS-STORE means the formula should not be stored, either because
  9206. it is already stored, or because it is a modified equation that should
  9207. not overwrite the stored one."
  9208. (interactive "P")
  9209. (require 'calc)
  9210. (org-table-check-inside-data-field)
  9211. (org-table-get-specials)
  9212. (let* (fields
  9213. (ndown (if (integerp arg) arg 1))
  9214. (org-table-automatic-realign nil)
  9215. (case-fold-search nil)
  9216. (down (> ndown 1))
  9217. (formula (if (and equation suppress-store)
  9218. equation
  9219. (org-table-get-formula equation (equal arg '(4)))))
  9220. (n0 (org-table-current-column))
  9221. (modes (copy-sequence org-calc-default-modes))
  9222. n form fmt x ev orig c)
  9223. ;; Parse the format string. Since we have a lot of modes, this is
  9224. ;; a lot of work. However, I think calc still uses most of the time.
  9225. (if (string-match ";" formula)
  9226. (let ((tmp (org-split-string formula ";")))
  9227. (setq formula (car tmp)
  9228. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  9229. (nth 1 tmp)))
  9230. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  9231. (setq c (string-to-char (match-string 1 fmt))
  9232. n (string-to-number (or (match-string 1 fmt) "")))
  9233. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  9234. (setq modes (org-set-calc-mode
  9235. 'calc-float-format
  9236. (list (cdr (assoc c '((?n . float) (?f . fix)
  9237. (?s . sci) (?e . eng))))
  9238. n))))
  9239. (setq fmt (replace-match "" t t fmt)))
  9240. (while (string-match "[DRFS]" fmt)
  9241. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  9242. (setq fmt (replace-match "" t t fmt)))
  9243. (unless (string-match "\\S-" fmt)
  9244. (setq fmt nil))))
  9245. (if (and (not suppress-const) org-table-formula-use-constants)
  9246. (setq formula (org-table-formula-substitute-names formula)))
  9247. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  9248. (while (> ndown 0)
  9249. (setq fields (org-split-string
  9250. (buffer-substring
  9251. (point-at-bol) (point-at-eol)) " *| *"))
  9252. (if org-table-formula-numbers-only
  9253. (setq fields (mapcar
  9254. (lambda (x) (number-to-string (string-to-number x)))
  9255. fields)))
  9256. (setq ndown (1- ndown))
  9257. (setq form (copy-sequence formula))
  9258. ;; Insert the references to fields in same row
  9259. (while (string-match "\\$\\([0-9]+\\)?" form)
  9260. (setq n (if (match-beginning 1)
  9261. (string-to-number (match-string 1 form))
  9262. n0)
  9263. x (nth (1- n) fields))
  9264. (unless x (error "Invalid field specifier \"%s\""
  9265. (match-string 0 form)))
  9266. (if (equal x "") (setq x "0"))
  9267. (setq form (replace-match (concat "(" x ")") t t form)))
  9268. ;; Insert ranges in current column
  9269. (while (string-match "\\&[-I0-9]+" form)
  9270. (setq form (replace-match
  9271. (save-match-data
  9272. (org-table-get-vertical-vector (match-string 0 form)
  9273. nil n0))
  9274. t t form)))
  9275. (setq ev (calc-eval (cons form modes)
  9276. (if org-table-formula-numbers-only 'num)))
  9277. (when org-table-formula-debug
  9278. (with-output-to-temp-buffer "*Help*"
  9279. (princ (format "Substitution history of formula
  9280. Orig: %s
  9281. $xyz-> %s
  9282. $1-> %s\n" orig formula form))
  9283. (if (listp ev)
  9284. (princ (format " %s^\nError: %s"
  9285. (make-string (car ev) ?\-) (nth 1 ev)))
  9286. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  9287. ev (or fmt "NONE")
  9288. (if fmt (format fmt (string-to-number ev)) ev)))))
  9289. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  9290. (unless (and (interactive-p) (not ndown))
  9291. (unless (let (inhibit-redisplay)
  9292. (y-or-n-p "Debugging Formula. Continue to next? "))
  9293. (org-table-align)
  9294. (error "Abort"))
  9295. (delete-window (get-buffer-window "*Help*"))
  9296. (message "")))
  9297. (if (listp ev) (setq fmt nil ev "#ERROR"))
  9298. (org-table-justify-field-maybe
  9299. (if fmt (format fmt (string-to-number ev)) ev))
  9300. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  9301. (call-interactively 'org-return)
  9302. (setq ndown 0)))
  9303. (and down (org-table-maybe-recalculate-line))
  9304. (or suppress-align (and org-table-may-need-update
  9305. (org-table-align)))))
  9306. (defun org-table-recalculate (&optional all noalign)
  9307. "Recalculate the current table line by applying all stored formulas."
  9308. (interactive "P")
  9309. (or (memq this-command org-recalc-commands)
  9310. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  9311. (unless (org-at-table-p) (error "Not at a table"))
  9312. (org-table-get-specials)
  9313. (let* ((eqlist (sort (org-table-get-stored-formulas)
  9314. (lambda (a b) (string< (car a) (car b)))))
  9315. (inhibit-redisplay t)
  9316. (line-re org-table-dataline-regexp)
  9317. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9318. (thiscol (org-table-current-column))
  9319. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  9320. ;; Insert constants in all formulas
  9321. (setq eqlist
  9322. (mapcar (lambda (x)
  9323. (setcdr x (org-table-formula-substitute-names (cdr x)))
  9324. x)
  9325. eqlist))
  9326. ;; Split the equation list
  9327. (while (setq eq (pop eqlist))
  9328. (if (<= (string-to-char (car eq)) ?9)
  9329. (push eq eqlnum)
  9330. (push eq eqlname)))
  9331. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  9332. (if all
  9333. (progn
  9334. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  9335. (goto-char (setq beg (org-table-begin)))
  9336. (if (re-search-forward org-table-calculate-mark-regexp end t)
  9337. ;; This is a table with marked lines, only compute selected lines
  9338. (setq line-re org-table-recalculate-regexp)
  9339. ;; Move forward to the first non-header line
  9340. (if (and (re-search-forward org-table-dataline-regexp end t)
  9341. (re-search-forward org-table-hline-regexp end t)
  9342. (re-search-forward org-table-dataline-regexp end t))
  9343. (setq beg (match-beginning 0))
  9344. nil))) ;; just leave beg where it is
  9345. (setq beg (point-at-bol)
  9346. end (move-marker (make-marker) (1+ (point-at-eol)))))
  9347. (goto-char beg)
  9348. (and all (message "Re-applying formulas to full table..."))
  9349. (while (re-search-forward line-re end t)
  9350. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  9351. ;; Unprotected line, recalculate
  9352. (and all (message "Re-applying formulas to full table...(line %d)"
  9353. (setq cnt (1+ cnt))))
  9354. (setq org-last-recalc-line (org-current-line))
  9355. (setq eql eqlnum)
  9356. (while (setq entry (pop eql))
  9357. (goto-line org-last-recalc-line)
  9358. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  9359. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  9360. (goto-line thisline)
  9361. (org-table-goto-column thiscol)
  9362. (or noalign (and org-table-may-need-update (org-table-align))
  9363. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  9364. ;; Now do the names fields
  9365. (while (setq eq (pop eqlname))
  9366. (setq name (car eq)
  9367. a (assoc name org-table-named-field-locations))
  9368. (when a
  9369. (message "Re-applying formula to named field: %s" name)
  9370. (goto-line (nth 1 a))
  9371. (org-table-goto-column (nth 2 a))
  9372. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  9373. ;; back to initial position
  9374. (goto-line thisline)
  9375. (org-table-goto-column thiscol)
  9376. (or noalign (and org-table-may-need-update (org-table-align))
  9377. (and all (message "Re-applying formulas...done")))))
  9378. (defun org-table-formula-substitute-names (f)
  9379. "Replace $const with values in string F."
  9380. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  9381. ;; First, check for column names
  9382. (while (setq start (string-match org-table-column-name-regexp f start))
  9383. (setq start (1+ start))
  9384. (setq a (assoc (match-string 1 f) org-table-column-names))
  9385. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  9386. ;; Expand ranges to vectors
  9387. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  9388. (setq n1 (string-to-number (match-string 1 f))
  9389. n2 (string-to-number (match-string 2 f))
  9390. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  9391. s (concat "[($" (number-to-string (1- nn1)) ")"))
  9392. (loop for i from nn1 upto nn2 do
  9393. (setq s (concat s ",($" (int-to-string i) ")")))
  9394. (setq s (concat s "]"))
  9395. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  9396. (setq f (replace-match s t t f)))
  9397. ;; Parameters and constants
  9398. (setq start 0)
  9399. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  9400. (setq start (1+ start))
  9401. (if (setq a (save-match-data
  9402. (org-table-get-constant (match-string 1 f))))
  9403. (setq f (replace-match (concat "(" a ")") t t f))))
  9404. (if org-table-formula-debug
  9405. (put-text-property 0 (length f) :orig-formula f1 f))
  9406. f))
  9407. (defun org-table-get-constant (const)
  9408. "Find the value for a parameter or constant in a formula.
  9409. Parameters get priority."
  9410. (or (cdr (assoc const org-table-local-parameters))
  9411. (cdr (assoc const org-table-formula-constants))
  9412. (and (fboundp 'constants-get) (constants-get const))
  9413. "#UNDEFINED_NAME"))
  9414. (defvar org-edit-formulas-map (make-sparse-keymap))
  9415. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  9416. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  9417. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  9418. (defvar org-pos)
  9419. (defun org-table-edit-formulas ()
  9420. "Edit the formulas of the current table in a separate buffer."
  9421. (interactive)
  9422. (unless (org-at-table-p)
  9423. (error "Not at a table"))
  9424. (org-table-get-specials)
  9425. (let ((eql (org-table-get-stored-formulas))
  9426. (pos (move-marker (make-marker) (point)))
  9427. (wc (current-window-configuration))
  9428. entry loc s)
  9429. (switch-to-buffer-other-window "*Edit Formulas*")
  9430. (erase-buffer)
  9431. (fundamental-mode)
  9432. (set (make-local-variable 'org-pos) pos)
  9433. (set (make-local-variable 'org-window-configuration) wc)
  9434. (use-local-map org-edit-formulas-map)
  9435. (setq s "# Edit formulas and finish with `C-c C-c'.
  9436. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  9437. # Use `C-c ?' to get information about $name at point.
  9438. # To cancel editing, press `C-c C-q'.\n")
  9439. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  9440. (insert s)
  9441. (while (setq entry (pop eql))
  9442. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  9443. (setq s (format "# Named formula, referring to column %d in line %d\n"
  9444. (nth 2 loc) (nth 1 loc)))
  9445. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  9446. (insert s))
  9447. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  9448. (remove-text-properties 0 (length s) '(face nil) s)
  9449. (insert s))
  9450. (goto-char (point-min))
  9451. (message "Edit formulas and finish with `C-c C-c'.")))
  9452. (defun org-show-variable ()
  9453. "Show the location/value of the $ expression at point."
  9454. (interactive)
  9455. (let (var (pos org-pos) (win (selected-window)) e)
  9456. (save-excursion
  9457. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  9458. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  9459. (setq var (match-string 1))
  9460. (error "No variable at point")))
  9461. (cond
  9462. ((setq e (assoc var org-table-named-field-locations))
  9463. (switch-to-buffer-other-window (marker-buffer pos))
  9464. (goto-line (nth 1 e))
  9465. (org-table-goto-column (nth 2 e))
  9466. (select-window win)
  9467. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  9468. ((setq e (assoc var org-table-column-names))
  9469. (switch-to-buffer-other-window (marker-buffer pos))
  9470. (goto-char pos)
  9471. (goto-char (org-table-begin))
  9472. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  9473. (org-table-end) t)
  9474. (progn
  9475. (goto-char (match-beginning 1))
  9476. (message "Named column (column %s)" (cdr e)))
  9477. (error "Column name not found"))
  9478. (select-window win))
  9479. ((string-match "^[0-9]$" var)
  9480. ;; column number
  9481. (switch-to-buffer-other-window (marker-buffer pos))
  9482. (goto-char pos)
  9483. (goto-char (org-table-begin))
  9484. (recenter 1)
  9485. (if (re-search-forward org-table-dataline-regexp
  9486. (org-table-end) t)
  9487. (progn
  9488. (goto-char (match-beginning 0))
  9489. (org-table-goto-column (string-to-number var))
  9490. (message "Column %s" var))
  9491. (error "Column name not found"))
  9492. (select-window win))
  9493. ((setq e (assoc var org-table-local-parameters))
  9494. (switch-to-buffer-other-window (marker-buffer pos))
  9495. (goto-char pos)
  9496. (goto-char (org-table-begin))
  9497. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  9498. (progn
  9499. (goto-char (match-beginning 1))
  9500. (message "Local parameter."))
  9501. (error "Parameter not found"))
  9502. (select-window win))
  9503. (t
  9504. (cond
  9505. ((setq e (assoc var org-table-formula-constants))
  9506. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  9507. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  9508. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  9509. (t (error "Undefined name $%s" var)))))))
  9510. (defun org-finish-edit-formulas (&optional arg)
  9511. "Parse the buffer for formula definitions and install them.
  9512. With prefix ARG, apply the new formulas to the table."
  9513. (interactive "P")
  9514. (let ((pos org-pos) eql)
  9515. (goto-char (point-min))
  9516. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  9517. (push (cons (match-string 1) (match-string 2)) eql))
  9518. (set-window-configuration org-window-configuration)
  9519. (select-window (get-buffer-window (marker-buffer pos)))
  9520. (goto-char pos)
  9521. (unless (org-at-table-p)
  9522. (error "Lost table position - cannot install formulae"))
  9523. (org-table-store-formulas eql)
  9524. (move-marker pos nil)
  9525. (kill-buffer "*Edit Formulas*")
  9526. (if arg
  9527. (org-table-recalculate 'all)
  9528. (message "New formulas installed - press C-u C-c C-c to apply."))))
  9529. (defun org-abort-edit-formulas ()
  9530. "Abort editing formulas, without installing the changes."
  9531. (interactive)
  9532. (let ((pos org-pos))
  9533. (set-window-configuration org-window-configuration)
  9534. (select-window (get-buffer-window (marker-buffer pos)))
  9535. (goto-char pos)
  9536. (message "Formula editing aborted without installing changes")))
  9537. ;;; The orgtbl minor mode
  9538. ;; Define a minor mode which can be used in other modes in order to
  9539. ;; integrate the org-mode table editor.
  9540. ;; This is really a hack, because the org-mode table editor uses several
  9541. ;; keys which normally belong to the major mode, for example the TAB and
  9542. ;; RET keys. Here is how it works: The minor mode defines all the keys
  9543. ;; necessary to operate the table editor, but wraps the commands into a
  9544. ;; function which tests if the cursor is currently inside a table. If that
  9545. ;; is the case, the table editor command is executed. However, when any of
  9546. ;; those keys is used outside a table, the function uses `key-binding' to
  9547. ;; look up if the key has an associated command in another currently active
  9548. ;; keymap (minor modes, major mode, global), and executes that command.
  9549. ;; There might be problems if any of the keys used by the table editor is
  9550. ;; otherwise used as a prefix key.
  9551. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  9552. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  9553. ;; addresses this by checking explicitly for both bindings.
  9554. ;; The optimized version (see variable `orgtbl-optimized') takes over
  9555. ;; all keys which are bound to `self-insert-command' in the *global map*.
  9556. ;; Some modes bind other commands to simple characters, for example
  9557. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  9558. ;; active, this binding is ignored inside tables and replaced with a
  9559. ;; modified self-insert.
  9560. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  9561. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  9562. In the optimized version, the table editor takes over all simple keys that
  9563. normally just insert a character. In tables, the characters are inserted
  9564. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  9565. for empty fields). Outside tables, the correct binding of the keys is
  9566. restored.
  9567. The default for this option is t if the optimized version is also used in
  9568. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  9569. this variable requires a restart of Emacs to become effective."
  9570. :group 'org-table
  9571. :type 'boolean)
  9572. (defvar orgtbl-mode nil
  9573. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  9574. table editor in arbitrary modes.")
  9575. (make-variable-buffer-local 'orgtbl-mode)
  9576. (defvar orgtbl-mode-map (make-keymap)
  9577. "Keymap for `orgtbl-mode'.")
  9578. ;;;###autoload
  9579. (defun turn-on-orgtbl ()
  9580. "Unconditionally turn on `orgtbl-mode'."
  9581. (orgtbl-mode 1))
  9582. ;;;###autoload
  9583. (defun orgtbl-mode (&optional arg)
  9584. "The `org-mode' table editor as a minor mode for use in other modes."
  9585. (interactive)
  9586. (if (eq major-mode 'org-mode)
  9587. ;; Exit without error, in case some hook functions calls this
  9588. ;; by accident in org-mode.
  9589. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  9590. (setq orgtbl-mode
  9591. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  9592. (if orgtbl-mode
  9593. (progn
  9594. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  9595. ;; Make sure we are first in minor-mode-map-alist
  9596. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  9597. (and c (setq minor-mode-map-alist
  9598. (cons c (delq c minor-mode-map-alist)))))
  9599. (set (make-local-variable (quote org-table-may-need-update)) t)
  9600. (org-add-hook 'before-change-functions 'org-before-change-function
  9601. nil 'local)
  9602. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  9603. auto-fill-inhibit-regexp)
  9604. (set (make-local-variable 'auto-fill-inhibit-regexp)
  9605. (if auto-fill-inhibit-regexp
  9606. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  9607. "[ \t]*|"))
  9608. (easy-menu-add orgtbl-mode-menu)
  9609. (run-hooks 'orgtbl-mode-hook))
  9610. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  9611. (remove-hook 'before-change-functions 'org-before-change-function t)
  9612. (easy-menu-remove orgtbl-mode-menu)
  9613. (force-mode-line-update 'all))))
  9614. ;; Install it as a minor mode.
  9615. (put 'orgtbl-mode :included t)
  9616. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  9617. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  9618. (defun orgtbl-make-binding (fun n &rest keys)
  9619. "Create a function for binding in the table minor mode.
  9620. FUN is the command to call inside a table. N is used to create a unique
  9621. command name. KEYS are keys that should be checked in for a command
  9622. to execute outside of tables."
  9623. (eval
  9624. (list 'defun
  9625. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  9626. '(arg)
  9627. (concat "In tables, run `" (symbol-name fun) "'.\n"
  9628. "Outside of tables, run the binding of `"
  9629. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  9630. "'.")
  9631. '(interactive "p")
  9632. (list 'if
  9633. '(org-at-table-p)
  9634. (list 'call-interactively (list 'quote fun))
  9635. (list 'let '(orgtbl-mode)
  9636. (list 'call-interactively
  9637. (append '(or)
  9638. (mapcar (lambda (k)
  9639. (list 'key-binding k))
  9640. keys)
  9641. '('orgtbl-error))))))))
  9642. (defun orgtbl-error ()
  9643. "Error when there is no default binding for a table key."
  9644. (interactive)
  9645. (error "This key is has no function outside tables"))
  9646. (defun orgtbl-setup ()
  9647. "Setup orgtbl keymaps."
  9648. (let ((nfunc 0)
  9649. (bindings
  9650. (list
  9651. '([(meta shift left)] org-table-delete-column)
  9652. '([(meta left)] org-table-move-column-left)
  9653. '([(meta right)] org-table-move-column-right)
  9654. '([(meta shift right)] org-table-insert-column)
  9655. '([(meta shift up)] org-table-kill-row)
  9656. '([(meta shift down)] org-table-insert-row)
  9657. '([(meta up)] org-table-move-row-up)
  9658. '([(meta down)] org-table-move-row-down)
  9659. '("\C-c\C-w" org-table-cut-region)
  9660. '("\C-c\M-w" org-table-copy-region)
  9661. '("\C-c\C-y" org-table-paste-rectangle)
  9662. '("\C-c-" org-table-insert-hline)
  9663. ; '([(shift tab)] org-table-previous-field)
  9664. '("\C-m" org-table-next-row)
  9665. (list (org-key 'S-return) 'org-table-copy-down)
  9666. '([(meta return)] org-table-wrap-region)
  9667. '("\C-c\C-q" org-table-wrap-region)
  9668. '("\C-c?" org-table-current-column)
  9669. '("\C-c " org-table-blank-field)
  9670. '("\C-c+" org-table-sum)
  9671. '("\C-c|" org-table-toggle-vline-visibility)
  9672. '("\C-c=" org-table-eval-formula)
  9673. '("\C-c'" org-table-edit-formulas)
  9674. '("\C-c`" org-table-edit-field)
  9675. '("\C-c*" org-table-recalculate)
  9676. '("\C-c^" org-table-sort-lines)
  9677. '([(control ?#)] org-table-rotate-recalc-marks)))
  9678. elt key fun cmd)
  9679. (while (setq elt (pop bindings))
  9680. (setq nfunc (1+ nfunc))
  9681. (setq key (car elt)
  9682. fun (nth 1 elt)
  9683. cmd (orgtbl-make-binding fun nfunc key))
  9684. (define-key orgtbl-mode-map key cmd))
  9685. ;; Special treatment needed for TAB and RET
  9686. (define-key orgtbl-mode-map [(return)]
  9687. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  9688. (define-key orgtbl-mode-map "\C-m"
  9689. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  9690. (define-key orgtbl-mode-map [(tab)]
  9691. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  9692. (define-key orgtbl-mode-map "\C-i"
  9693. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  9694. (define-key orgtbl-mode-map "\C-i"
  9695. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  9696. (define-key orgtbl-mode-map "\C-c\C-c"
  9697. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  9698. (when orgtbl-optimized
  9699. ;; If the user wants maximum table support, we need to hijack
  9700. ;; some standard editing functions
  9701. (org-remap orgtbl-mode-map
  9702. 'self-insert-command 'orgtbl-self-insert-command
  9703. 'delete-char 'orgtbl-delete-char
  9704. 'delete-backward-char 'orgtbl-delete-backward-char)
  9705. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  9706. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  9707. '("OrgTbl"
  9708. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  9709. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  9710. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  9711. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  9712. "--"
  9713. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  9714. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  9715. ["Copy Field from Above"
  9716. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  9717. "--"
  9718. ("Column"
  9719. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  9720. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  9721. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  9722. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  9723. "--"
  9724. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  9725. ("Row"
  9726. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  9727. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  9728. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  9729. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  9730. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  9731. "--"
  9732. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  9733. ("Rectangle"
  9734. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  9735. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  9736. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  9737. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  9738. "--"
  9739. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  9740. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  9741. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  9742. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  9743. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  9744. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  9745. ["Sum Column/Rectangle" org-table-sum
  9746. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  9747. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  9748. ["Debug Formulas"
  9749. (setq org-table-formula-debug (not org-table-formula-debug))
  9750. :style toggle :selected org-table-formula-debug]
  9751. ))
  9752. t)
  9753. (defun orgtbl-tab ()
  9754. "Justification and field motion for `orgtbl-mode'."
  9755. (interactive)
  9756. (org-table-justify-field-maybe)
  9757. (org-table-next-field))
  9758. (defun orgtbl-ret ()
  9759. "Justification and field motion for `orgtbl-mode'."
  9760. (interactive)
  9761. (org-table-justify-field-maybe)
  9762. (org-table-next-row))
  9763. (defun orgtbl-self-insert-command (N)
  9764. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  9765. If the cursor is in a table looking at whitespace, the whitespace is
  9766. overwritten, and the table is not marked as requiring realignment."
  9767. (interactive "p")
  9768. (if (and (org-at-table-p)
  9769. (or
  9770. (and org-table-auto-blank-field
  9771. (member last-command
  9772. '(orgtbl-hijacker-command-100
  9773. orgtbl-hijacker-command-101
  9774. orgtbl-hijacker-command-102
  9775. orgtbl-hijacker-command-103
  9776. orgtbl-hijacker-command-104
  9777. orgtbl-hijacker-command-105))
  9778. (org-table-blank-field))
  9779. t)
  9780. (eq N 1)
  9781. (looking-at "[^|\n]* +|"))
  9782. (let (org-table-may-need-update)
  9783. (goto-char (1- (match-end 0)))
  9784. (delete-backward-char 1)
  9785. (goto-char (match-beginning 0))
  9786. (self-insert-command N))
  9787. (setq org-table-may-need-update t)
  9788. (let (orgtbl-mode)
  9789. (call-interactively (key-binding (vector last-input-event))))))
  9790. (defun org-force-self-insert (N)
  9791. "Needed to enforce self-insert under remapping."
  9792. (interactive "p")
  9793. (self-insert-command N))
  9794. (defun orgtbl-delete-backward-char (N)
  9795. "Like `delete-backward-char', insert whitespace at field end in tables.
  9796. When deleting backwards, in tables this function will insert whitespace in
  9797. front of the next \"|\" separator, to keep the table aligned. The table will
  9798. still be marked for re-alignment, because a narrow field may lead to a
  9799. reduced column width."
  9800. (interactive "p")
  9801. (if (and (org-at-table-p)
  9802. (eq N 1)
  9803. (string-match "|" (buffer-substring (point-at-bol) (point)))
  9804. (looking-at ".*?|"))
  9805. (let ((pos (point)))
  9806. (backward-delete-char N)
  9807. (skip-chars-forward "^|")
  9808. (insert " ")
  9809. (goto-char (1- pos)))
  9810. (delete-backward-char N)))
  9811. (defun orgtbl-delete-char (N)
  9812. "Like `delete-char', but insert whitespace at field end in tables.
  9813. When deleting characters, in tables this function will insert whitespace in
  9814. front of the next \"|\" separator, to keep the table aligned. The table
  9815. will still be marked for re-alignment, because a narrow field may lead to
  9816. a reduced column width."
  9817. (interactive "p")
  9818. (if (and (org-at-table-p)
  9819. (not (bolp))
  9820. (not (= (char-after) ?|))
  9821. (eq N 1))
  9822. (if (looking-at ".*?|")
  9823. (let ((pos (point)))
  9824. (replace-match (concat
  9825. (substring (match-string 0) 1 -1)
  9826. " |"))
  9827. (goto-char pos)))
  9828. (delete-char N)))
  9829. ;;; Exporting
  9830. (defconst org-level-max 20)
  9831. (defun org-export-find-first-heading-line (list)
  9832. "Remove all lines from LIST which are before the first headline."
  9833. (let ((orig-list list)
  9834. (re (concat "^" outline-regexp)))
  9835. (while (and list
  9836. (not (string-match re (car list))))
  9837. (pop list))
  9838. (or list orig-list)))
  9839. (defun org-skip-comments (lines)
  9840. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  9841. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  9842. (re2 "^\\(\\*+\\)[ \t\n\r]")
  9843. rtn line level)
  9844. (while (setq line (pop lines))
  9845. (cond
  9846. ((and (string-match re1 line)
  9847. (setq level (- (match-end 1) (match-beginning 1))))
  9848. ;; Beginning of a COMMENT subtree. Skip it.
  9849. (while (and (setq line (pop lines))
  9850. (or (not (string-match re2 line))
  9851. (> (- (match-end 1) (match-beginning 1)) level))))
  9852. (setq lines (cons line lines)))
  9853. ((string-match "^#" line)
  9854. ;; an ordinary comment line
  9855. )
  9856. ((and org-export-table-remove-special-lines
  9857. (string-match "^[ \t]*| *[!_^] *|" line))
  9858. ;; a special table line that should be removed
  9859. )
  9860. (t (setq rtn (cons line rtn)))))
  9861. (nreverse rtn)))
  9862. ;; ASCII
  9863. (defconst org-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  9864. "Characters for underlining headings in ASCII export.")
  9865. (defconst org-html-entities
  9866. '(("nbsp")
  9867. ("iexcl")
  9868. ("cent")
  9869. ("pound")
  9870. ("curren")
  9871. ("yen")
  9872. ("brvbar")
  9873. ("sect")
  9874. ("uml")
  9875. ("copy")
  9876. ("ordf")
  9877. ("laquo")
  9878. ("not")
  9879. ("shy")
  9880. ("reg")
  9881. ("macr")
  9882. ("deg")
  9883. ("plusmn")
  9884. ("sup2")
  9885. ("sup3")
  9886. ("acute")
  9887. ("micro")
  9888. ("para")
  9889. ("middot")
  9890. ("odot"."o")
  9891. ("star"."*")
  9892. ("cedil")
  9893. ("sup1")
  9894. ("ordm")
  9895. ("raquo")
  9896. ("frac14")
  9897. ("frac12")
  9898. ("frac34")
  9899. ("iquest")
  9900. ("Agrave")
  9901. ("Aacute")
  9902. ("Acirc")
  9903. ("Atilde")
  9904. ("Auml")
  9905. ("Aring") ("AA"."&Aring;")
  9906. ("AElig")
  9907. ("Ccedil")
  9908. ("Egrave")
  9909. ("Eacute")
  9910. ("Ecirc")
  9911. ("Euml")
  9912. ("Igrave")
  9913. ("Iacute")
  9914. ("Icirc")
  9915. ("Iuml")
  9916. ("ETH")
  9917. ("Ntilde")
  9918. ("Ograve")
  9919. ("Oacute")
  9920. ("Ocirc")
  9921. ("Otilde")
  9922. ("Ouml")
  9923. ("times")
  9924. ("Oslash")
  9925. ("Ugrave")
  9926. ("Uacute")
  9927. ("Ucirc")
  9928. ("Uuml")
  9929. ("Yacute")
  9930. ("THORN")
  9931. ("szlig")
  9932. ("agrave")
  9933. ("aacute")
  9934. ("acirc")
  9935. ("atilde")
  9936. ("auml")
  9937. ("aring")
  9938. ("aelig")
  9939. ("ccedil")
  9940. ("egrave")
  9941. ("eacute")
  9942. ("ecirc")
  9943. ("euml")
  9944. ("igrave")
  9945. ("iacute")
  9946. ("icirc")
  9947. ("iuml")
  9948. ("eth")
  9949. ("ntilde")
  9950. ("ograve")
  9951. ("oacute")
  9952. ("ocirc")
  9953. ("otilde")
  9954. ("ouml")
  9955. ("divide")
  9956. ("oslash")
  9957. ("ugrave")
  9958. ("uacute")
  9959. ("ucirc")
  9960. ("uuml")
  9961. ("yacute")
  9962. ("thorn")
  9963. ("yuml")
  9964. ("fnof")
  9965. ("Alpha")
  9966. ("Beta")
  9967. ("Gamma")
  9968. ("Delta")
  9969. ("Epsilon")
  9970. ("Zeta")
  9971. ("Eta")
  9972. ("Theta")
  9973. ("Iota")
  9974. ("Kappa")
  9975. ("Lambda")
  9976. ("Mu")
  9977. ("Nu")
  9978. ("Xi")
  9979. ("Omicron")
  9980. ("Pi")
  9981. ("Rho")
  9982. ("Sigma")
  9983. ("Tau")
  9984. ("Upsilon")
  9985. ("Phi")
  9986. ("Chi")
  9987. ("Psi")
  9988. ("Omega")
  9989. ("alpha")
  9990. ("beta")
  9991. ("gamma")
  9992. ("delta")
  9993. ("epsilon")
  9994. ("varepsilon"."&epsilon;")
  9995. ("zeta")
  9996. ("eta")
  9997. ("theta")
  9998. ("iota")
  9999. ("kappa")
  10000. ("lambda")
  10001. ("mu")
  10002. ("nu")
  10003. ("xi")
  10004. ("omicron")
  10005. ("pi")
  10006. ("rho")
  10007. ("sigmaf") ("varsigma"."&sigmaf;")
  10008. ("sigma")
  10009. ("tau")
  10010. ("upsilon")
  10011. ("phi")
  10012. ("chi")
  10013. ("psi")
  10014. ("omega")
  10015. ("thetasym") ("vartheta"."&thetasym;")
  10016. ("upsih")
  10017. ("piv")
  10018. ("bull") ("bullet"."&bull;")
  10019. ("hellip") ("dots"."&hellip;")
  10020. ("prime")
  10021. ("Prime")
  10022. ("oline")
  10023. ("frasl")
  10024. ("weierp")
  10025. ("image")
  10026. ("real")
  10027. ("trade")
  10028. ("alefsym")
  10029. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  10030. ("uarr") ("uparrow"."&uarr;")
  10031. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  10032. ("darr")("downarrow"."&darr;")
  10033. ("harr") ("leftrightarrow"."&harr;")
  10034. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  10035. ("lArr") ("Leftarrow"."&lArr;")
  10036. ("uArr") ("Uparrow"."&uArr;")
  10037. ("rArr") ("Rightarrow"."&rArr;")
  10038. ("dArr") ("Downarrow"."&dArr;")
  10039. ("hArr") ("Leftrightarrow"."&hArr;")
  10040. ("forall")
  10041. ("part") ("partial"."&part;")
  10042. ("exist") ("exists"."&exist;")
  10043. ("empty") ("emptyset"."&empty;")
  10044. ("nabla")
  10045. ("isin") ("in"."&isin;")
  10046. ("notin")
  10047. ("ni")
  10048. ("prod")
  10049. ("sum")
  10050. ("minus")
  10051. ("lowast") ("ast"."&lowast;")
  10052. ("radic")
  10053. ("prop") ("proptp"."&prop;")
  10054. ("infin") ("infty"."&infin;")
  10055. ("ang") ("angle"."&ang;")
  10056. ("and") ("vee"."&and;")
  10057. ("or") ("wedge"."&or;")
  10058. ("cap")
  10059. ("cup")
  10060. ("int")
  10061. ("there4")
  10062. ("sim")
  10063. ("cong") ("simeq"."&cong;")
  10064. ("asymp")("approx"."&asymp;")
  10065. ("ne") ("neq"."&ne;")
  10066. ("equiv")
  10067. ("le")
  10068. ("ge")
  10069. ("sub") ("subset"."&sub;")
  10070. ("sup") ("supset"."&sup;")
  10071. ("nsub")
  10072. ("sube")
  10073. ("supe")
  10074. ("oplus")
  10075. ("otimes")
  10076. ("perp")
  10077. ("sdot") ("cdot"."&sdot;")
  10078. ("lceil")
  10079. ("rceil")
  10080. ("lfloor")
  10081. ("rfloor")
  10082. ("lang")
  10083. ("rang")
  10084. ("loz") ("Diamond"."&loz;")
  10085. ("spades") ("spadesuit"."&spades;")
  10086. ("clubs") ("clubsuit"."&clubs;")
  10087. ("hearts") ("diamondsuit"."&hearts;")
  10088. ("diams") ("diamondsuit"."&diams;")
  10089. ("quot")
  10090. ("amp")
  10091. ("lt")
  10092. ("gt")
  10093. ("OElig")
  10094. ("oelig")
  10095. ("Scaron")
  10096. ("scaron")
  10097. ("Yuml")
  10098. ("circ")
  10099. ("tilde")
  10100. ("ensp")
  10101. ("emsp")
  10102. ("thinsp")
  10103. ("zwnj")
  10104. ("zwj")
  10105. ("lrm")
  10106. ("rlm")
  10107. ("ndash")
  10108. ("mdash")
  10109. ("lsquo")
  10110. ("rsquo")
  10111. ("sbquo")
  10112. ("ldquo")
  10113. ("rdquo")
  10114. ("bdquo")
  10115. ("dagger")
  10116. ("Dagger")
  10117. ("permil")
  10118. ("lsaquo")
  10119. ("rsaquo")
  10120. ("euro")
  10121. ("arccos"."arccos")
  10122. ("arcsin"."arcsin")
  10123. ("arctan"."arctan")
  10124. ("arg"."arg")
  10125. ("cos"."cos")
  10126. ("cosh"."cosh")
  10127. ("cot"."cot")
  10128. ("coth"."coth")
  10129. ("csc"."csc")
  10130. ("deg"."deg")
  10131. ("det"."det")
  10132. ("dim"."dim")
  10133. ("exp"."exp")
  10134. ("gcd"."gcd")
  10135. ("hom"."hom")
  10136. ("inf"."inf")
  10137. ("ker"."ker")
  10138. ("lg"."lg")
  10139. ("lim"."lim")
  10140. ("liminf"."liminf")
  10141. ("limsup"."limsup")
  10142. ("ln"."ln")
  10143. ("log"."log")
  10144. ("max"."max")
  10145. ("min"."min")
  10146. ("Pr"."Pr")
  10147. ("sec"."sec")
  10148. ("sin"."sin")
  10149. ("sinh"."sinh")
  10150. ("sup"."sup")
  10151. ("tan"."tan")
  10152. ("tanh"."tanh")
  10153. )
  10154. "Entities for TeX->HTML translation.
  10155. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  10156. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  10157. In that case, \"\\ent\" will be translated to \"&other;\".
  10158. The list contains HTML entities for Latin-1, Greek and other symbols.
  10159. It is supplemented by a number of commonly used TeX macros with appropriate
  10160. translations. There is currently no way for users to extend this.")
  10161. (defun org-cleaned-string-for-export (string)
  10162. "Cleanup a buffer substring so that links can be created safely."
  10163. (interactive)
  10164. (let* ((cb (current-buffer))
  10165. (re-radio (and org-target-link-regexp
  10166. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  10167. rtn)
  10168. (save-excursion
  10169. (set-buffer (get-buffer-create " org-mode-tmp"))
  10170. (erase-buffer)
  10171. (insert string)
  10172. (org-mode)
  10173. ;; Find targets in comments and move them out of comments,
  10174. ;; but mark them as targets that should be invisible
  10175. (goto-char (point-min))
  10176. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  10177. (replace-match "\\1(INVISIBLE)"))
  10178. ;; Find matches for radio targets and turn them into links
  10179. (goto-char (point-min))
  10180. (when re-radio
  10181. (while (re-search-forward re-radio nil t)
  10182. (replace-match "\\1[[\\2]]")))
  10183. ;; Find all links that contain a newline and put them into a single line
  10184. (goto-char (point-min))
  10185. (while (re-search-forward "\\(\\[\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\]\\)" nil t)
  10186. (replace-match "\\1 \\2")
  10187. (goto-char (match-beginning 0)))
  10188. ;; Remove comments
  10189. (goto-char (point-min))
  10190. (while (re-search-forward "^#.*\n?" nil t)
  10191. (replace-match ""))
  10192. (setq rtn (buffer-string)))
  10193. (kill-buffer " org-mode-tmp")
  10194. rtn))
  10195. (defun org-solidify-link-text (s &optional alist)
  10196. "Take link text and make a safe target out of it."
  10197. (save-match-data
  10198. (let* ((rtn
  10199. (mapconcat
  10200. 'identity
  10201. (org-split-string s "[ \t\r\n]+") "--"))
  10202. (a (assoc rtn alist)))
  10203. (or (cdr a) rtn))))
  10204. (defun org-convert-to-odd-levels ()
  10205. "Convert an org-mode file with all levels allowed to one with odd levels.
  10206. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  10207. level 5 etc."
  10208. (interactive)
  10209. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  10210. (let ((org-odd-levels-only nil) n)
  10211. (save-excursion
  10212. (goto-char (point-min))
  10213. (while (re-search-forward "^\\*\\*+" nil t)
  10214. (setq n (1- (length (match-string 0))))
  10215. (while (>= (setq n (1- n)) 0)
  10216. (org-demote))
  10217. (end-of-line 1))))))
  10218. (defun org-convert-to-oddeven-levels ()
  10219. "Convert an org-mode file with only odd levels to one with odd and even levels.
  10220. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  10221. section with an even level, conversion would destroy the structure of the file. An error
  10222. is signaled in this case."
  10223. (interactive)
  10224. (goto-char (point-min))
  10225. ;; First check if there are no even levels
  10226. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  10227. (org-show-hierarchy-above)
  10228. (error "Not all levels are odd in this file. Conversion not possible."))
  10229. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  10230. (let ((org-odd-levels-only nil) n)
  10231. (save-excursion
  10232. (goto-char (point-min))
  10233. (while (re-search-forward "^\\*\\*+" nil t)
  10234. (setq n (/ (length (match-string 0)) 2))
  10235. (while (>= (setq n (1- n)) 0)
  10236. (org-promote))
  10237. (end-of-line 1))))))
  10238. (defun org-tr-level (n)
  10239. "Make N odd if required."
  10240. (if org-odd-levels-only (1+ (/ n 2)) n))
  10241. (defvar org-last-level nil) ; dynamically scoped variable
  10242. (defun org-export-as-ascii (arg)
  10243. "Export the outline as a pretty ASCII file.
  10244. If there is an active region, export only the region.
  10245. The prefix ARG specifies how many levels of the outline should become
  10246. underlined headlines. The default is 3."
  10247. (interactive "P")
  10248. (setq-default org-todo-line-regexp org-todo-line-regexp)
  10249. (let* ((region
  10250. (buffer-substring
  10251. (if (org-region-active-p) (region-beginning) (point-min))
  10252. (if (org-region-active-p) (region-end) (point-max))))
  10253. (lines (org-export-find-first-heading-line
  10254. (org-skip-comments
  10255. (org-split-string
  10256. (org-cleaned-string-for-export region)
  10257. "[\r\n]"))))
  10258. (org-startup-with-deadline-check nil)
  10259. (level 0) line txt
  10260. (umax nil)
  10261. (case-fold-search nil)
  10262. (filename (concat (file-name-sans-extension buffer-file-name)
  10263. ".txt"))
  10264. (buffer (find-file-noselect filename))
  10265. (levels-open (make-vector org-level-max nil))
  10266. (date (format-time-string "%Y/%m/%d" (current-time)))
  10267. (time (format-time-string "%X" (org-current-time)))
  10268. (author user-full-name)
  10269. (title (buffer-name))
  10270. (options nil)
  10271. (email user-mail-address)
  10272. (language org-export-default-language)
  10273. (text nil)
  10274. (todo nil)
  10275. (lang-words nil))
  10276. (setq org-last-level 1)
  10277. (org-init-section-numbers)
  10278. (find-file-noselect filename)
  10279. ;; Search for the export key lines
  10280. (org-parse-key-lines)
  10281. (setq lang-words (or (assoc language org-export-language-setup)
  10282. (assoc "en" org-export-language-setup)))
  10283. (if org-export-ascii-show-new-buffer
  10284. (switch-to-buffer-other-window buffer)
  10285. (set-buffer buffer))
  10286. (erase-buffer)
  10287. (fundamental-mode)
  10288. (if options (org-parse-export-options options))
  10289. (setq umax (if arg (prefix-numeric-value arg)
  10290. org-export-headline-levels))
  10291. ;; File header
  10292. (if title (org-insert-centered title ?=))
  10293. (insert "\n")
  10294. (if (or author email)
  10295. (insert (concat (nth 1 lang-words) ": " (or author "")
  10296. (if email (concat " <" email ">") "")
  10297. "\n")))
  10298. (if (and date time)
  10299. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  10300. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  10301. (insert "\n\n")
  10302. (if org-export-with-toc
  10303. (progn
  10304. (insert (nth 3 lang-words) "\n"
  10305. (make-string (length (nth 3 lang-words)) ?=) "\n")
  10306. (mapcar '(lambda (line)
  10307. (if (string-match org-todo-line-regexp
  10308. line)
  10309. ;; This is a headline
  10310. (progn
  10311. (setq level (- (match-end 1) (match-beginning 1))
  10312. level (org-tr-level level)
  10313. txt (match-string 3 line)
  10314. todo
  10315. (or (and (match-beginning 2)
  10316. (not (equal (match-string 2 line)
  10317. org-done-string)))
  10318. ; TODO, not DONE
  10319. (and (= level umax)
  10320. (org-search-todo-below
  10321. line lines level))))
  10322. (setq txt (org-html-expand-for-ascii txt))
  10323. (if org-export-with-section-numbers
  10324. (setq txt (concat (org-section-number level)
  10325. " " txt)))
  10326. (if (<= level umax)
  10327. (progn
  10328. (insert
  10329. (make-string (* (1- level) 4) ?\ )
  10330. (format (if todo "%s (*)\n" "%s\n") txt))
  10331. (setq org-last-level level))
  10332. ))))
  10333. lines)))
  10334. (org-init-section-numbers)
  10335. (while (setq line (pop lines))
  10336. ;; Remove the quoted HTML tags.
  10337. (setq line (org-html-expand-for-ascii line))
  10338. ;; Remove targets
  10339. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  10340. (setq line (replace-match "" t t line)))
  10341. ;; Replace internal links
  10342. (while (string-match org-bracket-link-regexp line)
  10343. (setq line (replace-match
  10344. (if (match-end 3) "[\\3]" "[\\1]")
  10345. t nil line)))
  10346. (cond
  10347. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  10348. ;; a Headline
  10349. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  10350. txt (match-string 2 line))
  10351. (org-ascii-level-start level txt umax))
  10352. (t (insert line "\n"))))
  10353. (normal-mode)
  10354. (save-buffer)
  10355. (goto-char (point-min))))
  10356. (defun org-search-todo-below (line lines level)
  10357. "Search the subtree below LINE for any TODO entries."
  10358. (let ((rest (cdr (memq line lines)))
  10359. (re org-todo-line-regexp)
  10360. line lv todo)
  10361. (catch 'exit
  10362. (while (setq line (pop rest))
  10363. (if (string-match re line)
  10364. (progn
  10365. (setq lv (- (match-end 1) (match-beginning 1))
  10366. todo (and (match-beginning 2)
  10367. (not (equal (match-string 2 line)
  10368. org-done-string))))
  10369. ; TODO, not DONE
  10370. (if (<= lv level) (throw 'exit nil))
  10371. (if todo (throw 'exit t))))))))
  10372. ;; FIXME: Try to handle <b> and <i> as faces via text properties.
  10373. ;; FIXME: Can I implement *bold*,/italic/ and _underline_ for ASCII export?
  10374. (defun org-html-expand-for-ascii (line)
  10375. "Handle quoted HTML for ASCII export."
  10376. (if org-export-html-expand
  10377. (while (string-match "@<[^<>\n]*>" line)
  10378. ;; We just remove the tags for now.
  10379. (setq line (replace-match "" nil nil line))))
  10380. line)
  10381. (defun org-insert-centered (s &optional underline)
  10382. "Insert the string S centered and underline it with character UNDERLINE."
  10383. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  10384. (insert (make-string ind ?\ ) s "\n")
  10385. (if underline
  10386. (insert (make-string ind ?\ )
  10387. (make-string (string-width s) underline)
  10388. "\n"))))
  10389. (defun org-ascii-level-start (level title umax)
  10390. "Insert a new level in ASCII export."
  10391. (let (char)
  10392. (if (> level umax)
  10393. (insert (make-string (* 2 (- level umax 1)) ?\ ) "* " title "\n")
  10394. (if (or (not (equal (char-before) ?\n))
  10395. (not (equal (char-before (1- (point))) ?\n)))
  10396. (insert "\n"))
  10397. (setq char (nth (- umax level) (reverse org-ascii-underline)))
  10398. (if org-export-with-section-numbers
  10399. (setq title (concat (org-section-number level) " " title)))
  10400. (insert title "\n" (make-string (string-width title) char) "\n"))))
  10401. (defun org-export-copy-visible ()
  10402. "Copy the visible part of the buffer to another buffer, for printing.
  10403. Also removes the first line of the buffer if it specifies a mode,
  10404. and all options lines."
  10405. (interactive)
  10406. (let* ((filename (concat (file-name-sans-extension buffer-file-name)
  10407. ".txt"))
  10408. (buffer (find-file-noselect filename))
  10409. (ore (concat
  10410. (org-make-options-regexp
  10411. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  10412. "STARTUP" "ARCHIVE"
  10413. "TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE"))
  10414. (if org-noutline-p "\\(\n\\|$\\)" "")))
  10415. s e)
  10416. (with-current-buffer buffer
  10417. (erase-buffer)
  10418. (text-mode))
  10419. (save-excursion
  10420. (setq s (goto-char (point-min)))
  10421. (while (not (= (point) (point-max)))
  10422. (goto-char (org-find-invisible))
  10423. (append-to-buffer buffer s (point))
  10424. (setq s (goto-char (org-find-visible)))))
  10425. (switch-to-buffer-other-window buffer)
  10426. (newline)
  10427. (goto-char (point-min))
  10428. (if (looking-at ".*-\\*- mode:.*\n")
  10429. (replace-match ""))
  10430. (while (re-search-forward ore nil t)
  10431. (replace-match ""))
  10432. (goto-char (point-min))))
  10433. (defun org-find-visible ()
  10434. (if (featurep 'noutline)
  10435. (let ((s (point)))
  10436. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  10437. (get-char-property s 'invisible)))
  10438. s)
  10439. (skip-chars-forward "^\n")
  10440. (point)))
  10441. (defun org-find-invisible ()
  10442. (if (featurep 'noutline)
  10443. (let ((s (point)))
  10444. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  10445. (not (get-char-property s 'invisible))))
  10446. s)
  10447. (skip-chars-forward "^\r")
  10448. (point)))
  10449. ;; HTML
  10450. (defun org-get-current-options ()
  10451. "Return a string with current options as keyword options.
  10452. Does include HTML export options as well as TODO and CATEGORY stuff."
  10453. (format
  10454. "#+TITLE: %s
  10455. #+AUTHOR: %s
  10456. #+EMAIL: %s
  10457. #+LANGUAGE: %s
  10458. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  10459. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  10460. #+CATEGORY: %s
  10461. #+SEQ_TODO: %s
  10462. #+TYP_TODO: %s
  10463. #+STARTUP: %s %s %s %s %s
  10464. #+ARCHIVE: %s
  10465. "
  10466. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  10467. org-export-headline-levels
  10468. org-export-with-section-numbers
  10469. org-export-with-toc
  10470. org-export-preserve-breaks
  10471. org-export-html-expand
  10472. org-export-with-fixed-width
  10473. org-export-with-tables
  10474. org-export-with-sub-superscripts
  10475. org-export-with-emphasize
  10476. org-export-with-TeX-macros
  10477. (file-name-nondirectory buffer-file-name)
  10478. (if (equal org-todo-interpretation 'sequence)
  10479. (mapconcat 'identity org-todo-keywords " ")
  10480. "TODO FEEDBACK VERIFY DONE")
  10481. (if (equal org-todo-interpretation 'type)
  10482. (mapconcat 'identity org-todo-keywords " ")
  10483. "Me Jason Marie DONE")
  10484. (cdr (assoc org-startup-folded
  10485. '((nil . "nofold")(t . "fold")(content . "content"))))
  10486. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  10487. (if org-odd-levels-only "odd" "oddeven")
  10488. (if org-hide-leading-stars "hidestars" "showstars")
  10489. (if org-startup-align-all-tables "align" "noalign")
  10490. org-archive-location
  10491. ))
  10492. (defun org-insert-export-options-template ()
  10493. "Insert into the buffer a template with information for exporting."
  10494. (interactive)
  10495. (if (not (bolp)) (newline))
  10496. (let ((s (org-get-current-options)))
  10497. (and (string-match "#\\+CATEGORY" s)
  10498. (setq s (substring s 0 (match-beginning 0))))
  10499. (insert s)))
  10500. (defun org-toggle-fixed-width-section (arg)
  10501. "Toggle the fixed-width export.
  10502. If there is no active region, the QUOTE keyword at the current headline is
  10503. inserted or removed. When present, it causes the text between this headline
  10504. and the next to be exported as fixed-width text, and unmodified.
  10505. If there is an active region, this command adds or removes a colon as the
  10506. first character of this line. If the first character of a line is a colon,
  10507. this line is also exported in fixed-width font."
  10508. (interactive "P")
  10509. (let* ((cc 0)
  10510. (regionp (org-region-active-p))
  10511. (beg (if regionp (region-beginning) (point)))
  10512. (end (if regionp (region-end)))
  10513. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  10514. (re "[ \t]*\\(:\\)")
  10515. off)
  10516. (if regionp
  10517. (save-excursion
  10518. (goto-char beg)
  10519. (setq cc (current-column))
  10520. (beginning-of-line 1)
  10521. (setq off (looking-at re))
  10522. (while (> nlines 0)
  10523. (setq nlines (1- nlines))
  10524. (beginning-of-line 1)
  10525. (cond
  10526. (arg
  10527. (move-to-column cc t)
  10528. (insert ":\n")
  10529. (forward-line -1))
  10530. ((and off (looking-at re))
  10531. (replace-match "" t t nil 1))
  10532. ((not off) (move-to-column cc t) (insert ":")))
  10533. (forward-line 1)))
  10534. (save-excursion
  10535. (org-back-to-heading)
  10536. (if (looking-at (concat outline-regexp
  10537. "\\( +\\<" org-quote-string "\\>\\)"))
  10538. (replace-match "" t t nil 1)
  10539. (if (looking-at outline-regexp)
  10540. (progn
  10541. (goto-char (match-end 0))
  10542. (insert " " org-quote-string))))))))
  10543. (defun org-export-as-html-and-open (arg)
  10544. "Export the outline as HTML and immediately open it with a browser.
  10545. If there is an active region, export only the region.
  10546. The prefix ARG specifies how many levels of the outline should become
  10547. headlines. The default is 3. Lower levels will become bulleted lists."
  10548. (interactive "P")
  10549. (org-export-as-html arg 'hidden)
  10550. (org-open-file buffer-file-name))
  10551. (defun org-export-as-html-batch ()
  10552. "Call `org-export-as-html', may be used in batch processing as
  10553. emacs --batch
  10554. --load=$HOME/lib/emacs/org.el
  10555. --eval \"(setq org-export-headline-levels 2)\"
  10556. --visit=MyFile --funcall org-export-as-html-batch"
  10557. (org-export-as-html org-export-headline-levels 'hidden))
  10558. (defun org-export-as-html (arg &optional hidden)
  10559. "Export the outline as a pretty HTML file.
  10560. If there is an active region, export only the region.
  10561. The prefix ARG specifies how many levels of the outline should become
  10562. headlines. The default is 3. Lower levels will become bulleted lists."
  10563. (interactive "P")
  10564. (setq-default org-todo-line-regexp org-todo-line-regexp)
  10565. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  10566. (setq-default org-done-string org-done-string)
  10567. (let* ((style org-export-html-style)
  10568. (odd org-odd-levels-only)
  10569. (region-p (org-region-active-p))
  10570. (region
  10571. (buffer-substring
  10572. (if region-p (region-beginning) (point-min))
  10573. (if region-p (region-end) (point-max))))
  10574. (all_lines
  10575. (org-skip-comments (org-split-string
  10576. (org-cleaned-string-for-export region)
  10577. "[\r\n]")))
  10578. (lines (org-export-find-first-heading-line all_lines))
  10579. (level 0) (line "") (origline "") txt todo
  10580. (umax nil)
  10581. (filename (concat (file-name-sans-extension buffer-file-name)
  10582. ".html"))
  10583. (buffer (find-file-noselect filename))
  10584. (levels-open (make-vector org-level-max nil))
  10585. (date (format-time-string "%Y/%m/%d" (current-time)))
  10586. (time (format-time-string "%X" (org-current-time)))
  10587. (author user-full-name)
  10588. (title (buffer-name))
  10589. (options nil)
  10590. (quote-re (concat "^\\*+[ \t]*" org-quote-string "\\>"))
  10591. (inquote nil)
  10592. (infixed nil)
  10593. (in-local-list nil)
  10594. (local-list-num nil)
  10595. (local-list-indent nil)
  10596. (llt org-plain-list-ordered-item-terminator)
  10597. (email user-mail-address)
  10598. (language org-export-default-language)
  10599. (text nil)
  10600. (lang-words nil)
  10601. (target-alist nil) tg
  10602. (head-count 0) cnt
  10603. (start 0)
  10604. ;; FIXME: The following returns always nil under XEmacs
  10605. (coding-system (and (fboundp 'coding-system-get)
  10606. (boundp 'buffer-file-coding-system)
  10607. buffer-file-coding-system))
  10608. (coding-system-for-write (or coding-system coding-system-for-write))
  10609. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  10610. (charset (and coding-system
  10611. (coding-system-get coding-system 'mime-charset)))
  10612. table-open type
  10613. table-buffer table-orig-buffer
  10614. ind start-is-num starter
  10615. )
  10616. (message "Exporting...")
  10617. (setq org-last-level 1)
  10618. (org-init-section-numbers)
  10619. ;; Search for the export key lines
  10620. (org-parse-key-lines)
  10621. (setq lang-words (or (assoc language org-export-language-setup)
  10622. (assoc "en" org-export-language-setup)))
  10623. ;; Switch to the output buffer
  10624. (if (or hidden (not org-export-html-show-new-buffer))
  10625. (set-buffer buffer)
  10626. (switch-to-buffer-other-window buffer))
  10627. (erase-buffer)
  10628. (fundamental-mode)
  10629. (let ((case-fold-search nil)
  10630. (org-odd-levels-only odd))
  10631. (if options (org-parse-export-options options))
  10632. (setq umax (if arg (prefix-numeric-value arg)
  10633. org-export-headline-levels))
  10634. ;; File header
  10635. (insert (format
  10636. "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\"
  10637. \"http://www.w3.org/TR/REC-html40/loose.dtd\">
  10638. <html lang=\"%s\"><head>
  10639. <title>%s</title>
  10640. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\">
  10641. <meta name=generator content=\"Org-mode\">
  10642. <meta name=generated content=\"%s %s\">
  10643. <meta name=author content=\"%s\">
  10644. %s
  10645. </head><body>
  10646. "
  10647. language (org-html-expand title) (or charset "iso-8859-1")
  10648. date time author style))
  10649. (if title (insert (concat "<H1 class=\"title\">"
  10650. (org-html-expand title) "</H1>\n")))
  10651. (if author (insert (concat (nth 1 lang-words) ": " author "\n")))
  10652. (if email (insert (concat "<a href=\"mailto:" email "\">&lt;"
  10653. email "&gt;</a>\n")))
  10654. (if (or author email) (insert "<br>\n"))
  10655. (if (and date time) (insert (concat (nth 2 lang-words) ": "
  10656. date " " time "<br>\n")))
  10657. (if text (insert (concat "<p>\n" (org-html-expand text))))
  10658. (if org-export-with-toc
  10659. (progn
  10660. (insert (format "<H2>%s</H2>\n" (nth 3 lang-words)))
  10661. (insert "<ul>\n")
  10662. (setq lines
  10663. (mapcar '(lambda (line)
  10664. (if (string-match org-todo-line-regexp line)
  10665. ;; This is a headline
  10666. (progn
  10667. (setq level (- (match-end 1) (match-beginning 1))
  10668. level (org-tr-level level)
  10669. txt (save-match-data
  10670. (org-html-expand
  10671. (match-string 3 line)))
  10672. todo
  10673. (or (and (match-beginning 2)
  10674. (not (equal (match-string 2 line)
  10675. org-done-string)))
  10676. ; TODO, not DONE
  10677. (and (= level umax)
  10678. (org-search-todo-below
  10679. line lines level))))
  10680. (if org-export-with-section-numbers
  10681. (setq txt (concat (org-section-number level)
  10682. " " txt)))
  10683. (if (<= level umax)
  10684. (progn
  10685. (setq head-count (+ head-count 1))
  10686. (if (> level org-last-level)
  10687. (progn
  10688. (setq cnt (- level org-last-level))
  10689. (while (>= (setq cnt (1- cnt)) 0)
  10690. (insert "<ul>"))
  10691. (insert "\n")))
  10692. (if (< level org-last-level)
  10693. (progn
  10694. (setq cnt (- org-last-level level))
  10695. (while (>= (setq cnt (1- cnt)) 0)
  10696. (insert "</ul>"))
  10697. (insert "\n")))
  10698. ;; Check for targets
  10699. (while (string-match org-target-regexp line)
  10700. (setq tg (match-string 1 line)
  10701. line (replace-match
  10702. (concat "@<span class=\"target\">" tg "@</span> ")
  10703. t t line))
  10704. (push (cons (org-solidify-link-text tg)
  10705. (format "sec-%d" head-count))
  10706. target-alist))
  10707. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  10708. (setq txt (replace-match "" t t txt)))
  10709. (insert
  10710. (format
  10711. (if todo
  10712. "<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>\n"
  10713. "<li><a href=\"#sec-%d\">%s</a>\n")
  10714. head-count txt))
  10715. (setq org-last-level level))
  10716. )))
  10717. line)
  10718. lines))
  10719. (while (> org-last-level 0)
  10720. (setq org-last-level (1- org-last-level))
  10721. (insert "</ul>\n"))
  10722. ))
  10723. (setq head-count 0)
  10724. (org-init-section-numbers)
  10725. (while (setq line (pop lines) origline line)
  10726. (catch 'nextline
  10727. ;; end of quote section?
  10728. (when (and inquote (string-match "^\\*+" line))
  10729. (insert "</pre>\n")
  10730. (setq inquote nil))
  10731. ;; inside a quote section?
  10732. (when inquote
  10733. (insert (org-html-protect line) "\n")
  10734. (throw 'nextline nil))
  10735. ;; verbatim lines
  10736. (when (and org-export-with-fixed-width
  10737. (string-match "^[ \t]*:\\(.*\\)" line))
  10738. (when (not infixed)
  10739. (setq infixed t)
  10740. (insert "<pre>\n"))
  10741. (insert (org-html-protect (match-string 1 line)) "\n")
  10742. (when (and lines
  10743. (not (string-match "^[ \t]*\\(:.*\\)"
  10744. (car lines))))
  10745. (setq infixed nil)
  10746. (insert "</pre>\n"))
  10747. (throw 'nextline nil))
  10748. ;; make targets to anchors
  10749. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  10750. (cond
  10751. ((match-end 2)
  10752. (setq line (replace-match
  10753. (concat "@<a name=\""
  10754. (org-solidify-link-text (match-string 1 line))
  10755. "\">\\nbsp@</a>")
  10756. t t line)))
  10757. ((and org-export-with-toc (equal (string-to-char line) ?*))
  10758. (setq line (replace-match
  10759. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  10760. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  10761. t t line)))
  10762. (t
  10763. (setq line (replace-match
  10764. (concat "@<a name=\""
  10765. (org-solidify-link-text (match-string 1 line))
  10766. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  10767. t t line)))))
  10768. ;; Replace internal links
  10769. (while (string-match org-bracket-link-regexp line)
  10770. (setq line (replace-match
  10771. (concat
  10772. "@<a href=\"#"
  10773. (org-solidify-link-text (match-string 1 line) target-alist)
  10774. "\">"
  10775. (match-string (if (match-end 3) 3 1) line)
  10776. "@</a>")
  10777. t t line)))
  10778. ;; Protect the external links
  10779. (setq start 0)
  10780. (while (string-match org-link-maybe-angles-regexp line start)
  10781. (setq start (match-end 0))
  10782. (setq line (replace-match
  10783. (concat "\000" (match-string 1 line) "\000")
  10784. t t line)))
  10785. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  10786. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  10787. (setq line (org-html-expand line))
  10788. ;; Format the links
  10789. (setq start 0)
  10790. (while (string-match org-protected-link-regexp line start)
  10791. (setq start (- (match-end 0) 2))
  10792. (setq type (match-string 1 line))
  10793. (cond
  10794. ((member type '("http" "https" "ftp" "mailto" "news"))
  10795. ;; standard URL
  10796. (setq line (replace-match
  10797. ; "<a href=\"\\1:\\2\">&lt;\\1:\\2&gt;</a>"
  10798. "<a href=\"\\1:\\2\">\\1:\\2</a>"
  10799. nil nil line)))
  10800. ((string= type "file")
  10801. ;; FILE link
  10802. (let* ((filename (match-string 2 line))
  10803. (abs-p (file-name-absolute-p filename))
  10804. (thefile (if abs-p (expand-file-name filename) filename))
  10805. (thefile (save-match-data
  10806. (if (string-match ":[0-9]+$" thefile)
  10807. (replace-match "" t t thefile)
  10808. thefile)))
  10809. (file-is-image-p
  10810. (save-match-data
  10811. (string-match (org-image-file-name-regexp) thefile))))
  10812. (setq line (replace-match
  10813. (if (and org-export-html-inline-images
  10814. file-is-image-p)
  10815. (concat "<img src=\"" thefile "\"/>")
  10816. (concat "<a href=\"" thefile "\">\\1:\\2</a>"))
  10817. nil nil line))))
  10818. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell"))
  10819. (setq line (replace-match
  10820. "<i>&lt;\\1:\\2&gt;</i>" nil nil line)))))
  10821. ;; TODO items
  10822. (if (and (string-match org-todo-line-regexp line)
  10823. (match-beginning 2))
  10824. (if (equal (match-string 2 line) org-done-string)
  10825. (setq line (replace-match
  10826. "<span class=\"done\">\\2</span>"
  10827. nil nil line 2))
  10828. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  10829. nil nil line 2))))
  10830. ;; DEADLINES
  10831. (if (string-match org-deadline-line-regexp line)
  10832. (progn
  10833. (if (save-match-data
  10834. (string-match "<a href"
  10835. (substring line 0 (match-beginning 0))))
  10836. nil ; Don't do the replacement - it is inside a link
  10837. (setq line (replace-match "<span class=\"deadline\">\\&</span>"
  10838. nil nil line 1)))))
  10839. (cond
  10840. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  10841. ;; This is a headline
  10842. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  10843. txt (match-string 2 line))
  10844. (if (<= level umax) (setq head-count (+ head-count 1)))
  10845. (when in-local-list
  10846. ;; Close any local lists before inserting a new header line
  10847. (while local-list-num
  10848. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  10849. (pop local-list-num))
  10850. (setq local-list-indent nil
  10851. in-local-list nil))
  10852. (org-html-level-start level txt umax
  10853. (and org-export-with-toc (<= level umax))
  10854. head-count)
  10855. ;; QUOTES
  10856. (when (string-match quote-re line)
  10857. (insert "<pre>")
  10858. (setq inquote t)))
  10859. ((and org-export-with-tables
  10860. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  10861. (if (not table-open)
  10862. ;; New table starts
  10863. (setq table-open t table-buffer nil table-orig-buffer nil))
  10864. ;; Accumulate lines
  10865. (setq table-buffer (cons line table-buffer)
  10866. table-orig-buffer (cons origline table-orig-buffer))
  10867. (when (or (not lines)
  10868. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  10869. (car lines))))
  10870. (setq table-open nil
  10871. table-buffer (nreverse table-buffer)
  10872. table-orig-buffer (nreverse table-orig-buffer))
  10873. (insert (org-format-table-html table-buffer table-orig-buffer))))
  10874. (t
  10875. ;; Normal lines
  10876. (when (and (> org-export-plain-list-max-depth 0)
  10877. (string-match
  10878. (cond
  10879. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10880. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10881. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\)\\)?\\( +[^ \t\n\r]\\|[ \t]*$\\)")
  10882. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  10883. line))
  10884. (setq ind (org-get-string-indentation line)
  10885. start-is-num (match-beginning 4)
  10886. starter (if (match-beginning 2) (match-string 2 line))
  10887. line (substring line (match-beginning 5)))
  10888. (unless (string-match "[^ \t]" line)
  10889. ;; empty line. Pretend indentation is large.
  10890. (setq ind (1+ (or (car local-list-indent) 1))))
  10891. (while (and in-local-list
  10892. (or (and (= ind (car local-list-indent))
  10893. (not starter))
  10894. (< ind (car local-list-indent))))
  10895. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  10896. (pop local-list-num) (pop local-list-indent)
  10897. (setq in-local-list local-list-indent))
  10898. (cond
  10899. ((and starter
  10900. (or (not in-local-list)
  10901. (> ind (car local-list-indent)))
  10902. (< (length local-list-indent)
  10903. org-export-plain-list-max-depth))
  10904. ;; Start new (level of ) list
  10905. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  10906. (push start-is-num local-list-num)
  10907. (push ind local-list-indent)
  10908. (setq in-local-list t))
  10909. (starter
  10910. ;; continue current list
  10911. (insert "<li>\n"))))
  10912. ;; Empty lines start a new paragraph. If hand-formatted lists
  10913. ;; are not fully interpreted, lines starting with "-", "+", "*"
  10914. ;; also start a new paragraph.
  10915. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (insert "<p>"))
  10916. (insert line (if org-export-preserve-breaks "<br>\n" "\n"))))
  10917. ))
  10918. (if org-export-html-with-timestamp
  10919. (insert org-export-html-html-helper-timestamp))
  10920. (insert "</body>\n</html>\n")
  10921. (normal-mode)
  10922. (save-buffer)
  10923. (goto-char (point-min)))))
  10924. (defun org-format-table-html (lines olines)
  10925. "Find out which HTML converter to use and return the HTML code."
  10926. (if (string-match "^[ \t]*|" (car lines))
  10927. ;; A normal org table
  10928. (org-format-org-table-html lines)
  10929. ;; Table made by table.el - test for spanning
  10930. (let* ((hlines (delq nil (mapcar
  10931. (lambda (x)
  10932. (if (string-match "^[ \t]*\\+-" x) x
  10933. nil))
  10934. lines)))
  10935. (first (car hlines))
  10936. (ll (and (string-match "\\S-+" first)
  10937. (match-string 0 first)))
  10938. (re (concat "^[ \t]*" (regexp-quote ll)))
  10939. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  10940. hlines))))
  10941. (if (and (not spanning)
  10942. (not org-export-prefer-native-exporter-for-tables))
  10943. ;; We can use my own converter with HTML conversions
  10944. (org-format-table-table-html lines)
  10945. ;; Need to use the code generator in table.el, with the original text.
  10946. (org-format-table-table-html-using-table-generate-source olines)))))
  10947. (defun org-format-org-table-html (lines)
  10948. "Format a table into HTML."
  10949. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  10950. (setq lines (nreverse lines))
  10951. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  10952. (setq lines (nreverse lines))
  10953. (when org-export-table-remove-special-lines
  10954. ;; Check if the table has a marking column. If yes remove the
  10955. ;; column and the special lines
  10956. (let* ((special
  10957. (not
  10958. (memq nil
  10959. (mapcar
  10960. (lambda (x)
  10961. (or (string-match "^[ \t]*|-" x)
  10962. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  10963. lines)))))
  10964. (if special
  10965. (setq lines
  10966. (delq nil
  10967. (mapcar
  10968. (lambda (x)
  10969. (if (string-match "^[ \t]*| *[!_^] *|" x)
  10970. nil ; ignore this line
  10971. (and (or (string-match "^[ \t]*|-+\\+" x)
  10972. (string-match "^[ \t]*|[^|]*|" x))
  10973. (replace-match "|" t t x))))
  10974. lines))))))
  10975. (let ((head (and org-export-highlight-first-table-line
  10976. (delq nil (mapcar
  10977. (lambda (x) (string-match "^[ \t]*|-" x))
  10978. (cdr lines)))))
  10979. line fields html)
  10980. (setq html (concat org-export-html-table-tag "\n"))
  10981. (while (setq line (pop lines))
  10982. (catch 'next-line
  10983. (if (string-match "^[ \t]*|-" line)
  10984. (progn
  10985. (setq head nil) ;; head ends here, first time around
  10986. ;; ignore this line
  10987. (throw 'next-line t)))
  10988. ;; Break the line into fields
  10989. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  10990. (setq html (concat
  10991. html
  10992. "<tr>"
  10993. (mapconcat (lambda (x)
  10994. (if head
  10995. (concat "<th>" x "</th>")
  10996. (concat "<td>" x "</td>")))
  10997. fields "")
  10998. "</tr>\n"))))
  10999. (setq html (concat html "</table>\n"))
  11000. html))
  11001. (defun org-fake-empty-table-line (line)
  11002. "Replace everything except \"|\" with spaces."
  11003. (let ((i (length line))
  11004. (newstr (copy-sequence line)))
  11005. (while (> i 0)
  11006. (setq i (1- i))
  11007. (if (not (eq (aref newstr i) ?|))
  11008. (aset newstr i ?\ )))
  11009. newstr))
  11010. (defun org-format-table-table-html (lines)
  11011. "Format a table generated by table.el into HTML.
  11012. This conversion does *not* use `table-generate-source' from table.el.
  11013. This has the advantage that Org-mode's HTML conversions can be used.
  11014. But it has the disadvantage, that no cell- or row-spanning is allowed."
  11015. (let (line field-buffer
  11016. (head org-export-highlight-first-table-line)
  11017. fields html empty)
  11018. (setq html (concat org-export-html-table-tag "\n"))
  11019. (while (setq line (pop lines))
  11020. (setq empty "&nbsp")
  11021. (catch 'next-line
  11022. (if (string-match "^[ \t]*\\+-" line)
  11023. (progn
  11024. (if field-buffer
  11025. (progn
  11026. (setq html (concat
  11027. html
  11028. "<tr>"
  11029. (mapconcat
  11030. (lambda (x)
  11031. (if (equal x "") (setq x empty))
  11032. (if head
  11033. (concat "<th>" x "</th>\n")
  11034. (concat "<td>" x "</td>\n")))
  11035. field-buffer "\n")
  11036. "</tr>\n"))
  11037. (setq head nil)
  11038. (setq field-buffer nil)))
  11039. ;; Ignore this line
  11040. (throw 'next-line t)))
  11041. ;; Break the line into fields and store the fields
  11042. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  11043. (if field-buffer
  11044. (setq field-buffer (mapcar
  11045. (lambda (x)
  11046. (concat x "<br>" (pop fields)))
  11047. field-buffer))
  11048. (setq field-buffer fields))))
  11049. (setq html (concat html "</table>\n"))
  11050. html))
  11051. (defun org-format-table-table-html-using-table-generate-source (lines)
  11052. "Format a table into html, using `table-generate-source' from table.el.
  11053. This has the advantage that cell- or row-spanning is allowed.
  11054. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  11055. (require 'table)
  11056. (with-current-buffer (get-buffer-create " org-tmp1 ")
  11057. (erase-buffer)
  11058. (insert (mapconcat 'identity lines "\n"))
  11059. (goto-char (point-min))
  11060. (if (not (re-search-forward "|[^+]" nil t))
  11061. (error "Error processing table"))
  11062. (table-recognize-table)
  11063. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  11064. (table-generate-source 'html " org-tmp2 ")
  11065. (set-buffer " org-tmp2 ")
  11066. (buffer-substring (point-min) (point-max))))
  11067. (defun org-html-protect (s)
  11068. ;; convert & to &amp;, < to &lt; and > to &gt;
  11069. (let ((start 0))
  11070. (while (string-match "&" s start)
  11071. (setq s (replace-match "&amp;" t t s)
  11072. start (1+ (match-beginning 0))))
  11073. (while (string-match "<" s)
  11074. (setq s (replace-match "&lt;" t t s)))
  11075. (while (string-match ">" s)
  11076. (setq s (replace-match "&gt;" t t s))))
  11077. s)
  11078. (defun org-html-expand (string)
  11079. "Prepare STRING for HTML export. Applies all active conversions."
  11080. ;; First check if there is a link in the line - if yes, apply conversions
  11081. ;; only before the start of the link.
  11082. ;; FIXME: This is no longer correct, because links now have an end.
  11083. (let* ((m (string-match org-link-regexp string))
  11084. (s (if m (substring string 0 m) string))
  11085. (r (if m (substring string m) "")))
  11086. ;; convert & to &amp;, < to &lt; and > to &gt;
  11087. (setq s (org-html-protect s))
  11088. (if org-export-html-expand
  11089. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  11090. (setq s (replace-match "<\\1>" nil nil s))))
  11091. (if org-export-with-emphasize
  11092. (setq s (org-export-html-convert-emphasize s)))
  11093. (if org-export-with-sub-superscripts
  11094. (setq s (org-export-html-convert-sub-super s)))
  11095. (if org-export-with-TeX-macros
  11096. (let ((start 0) wd ass)
  11097. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  11098. (setq wd (match-string 1 s))
  11099. (if (setq ass (assoc wd org-html-entities))
  11100. (setq s (replace-match (or (cdr ass)
  11101. (concat "&" (car ass) ";"))
  11102. t t s))
  11103. (setq start (+ start (length wd)))))))
  11104. (concat s r)))
  11105. (defun org-create-multibrace-regexp (left right n)
  11106. "Create a regular expression which will match a balanced sexp.
  11107. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  11108. as single character strings.
  11109. The regexp returned will match the entire expression including the
  11110. delimiters. It will also define a single group which contains the
  11111. match except for the outermost delimiters. The maximum depth of
  11112. stacked delimiters is N. Escaping delimiters is not possible."
  11113. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  11114. (or "\\|")
  11115. (re nothing)
  11116. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  11117. (while (> n 1)
  11118. (setq n (1- n)
  11119. re (concat re or next)
  11120. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  11121. (concat left "\\(" re "\\)" right)))
  11122. (defvar org-match-substring-regexp
  11123. (concat
  11124. "\\([^\\]\\)\\([_^]\\)\\("
  11125. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  11126. "\\|"
  11127. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  11128. "\\|"
  11129. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  11130. "The regular expression matching a sub- or superscript.")
  11131. (defun org-export-html-convert-sub-super (string)
  11132. "Convert sub- and superscripts in STRING to HTML."
  11133. (let (key c)
  11134. (while (string-match org-match-substring-regexp string)
  11135. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  11136. (setq c (or (match-string 8 string)
  11137. (match-string 6 string)
  11138. (match-string 5 string)))
  11139. (setq string (replace-match
  11140. (concat (match-string 1 string)
  11141. "<" key ">" c "</" key ">")
  11142. t t string)))
  11143. (while (string-match "\\\\\\([_^]\\)" string)
  11144. (setq string (replace-match (match-string 1 string) t t string))))
  11145. string)
  11146. (defun org-export-html-convert-emphasize (string)
  11147. (let (c (s 0))
  11148. (while (string-match "\\(\\W\\|^\\)\\([*/_]\\)\\(\\w+\\)\\2\\(\\W\\|$\\)" string s)
  11149. (setq c (cdr (assoc (match-string 2 string)
  11150. '(("*" . "b") ("/" . "i") ("_" . "u"))))
  11151. s (+ (match-end 0) 3)
  11152. string (replace-match
  11153. (concat "\\1<" c ">\\3</" c ">\\4") t nil string)))
  11154. string))
  11155. (defun org-parse-key-lines ()
  11156. "Find the special key lines with the information for exporters."
  11157. (save-excursion
  11158. (goto-char 0)
  11159. (let ((re (org-make-options-regexp
  11160. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  11161. key)
  11162. (while (re-search-forward re nil t)
  11163. (setq key (match-string 1))
  11164. (cond ((string-equal key "TITLE")
  11165. (setq title (match-string 2)))
  11166. ((string-equal key "AUTHOR")
  11167. (setq author (match-string 2)))
  11168. ((string-equal key "EMAIL")
  11169. (setq email (match-string 2)))
  11170. ((string-equal key "LANGUAGE")
  11171. (setq language (match-string 2)))
  11172. ((string-equal key "TEXT")
  11173. (setq text (concat text "\n" (match-string 2))))
  11174. ((string-equal key "OPTIONS")
  11175. (setq options (match-string 2))))))))
  11176. (defun org-parse-export-options (s)
  11177. "Parse the export options line."
  11178. (let ((op '(("H" . org-export-headline-levels)
  11179. ("num" . org-export-with-section-numbers)
  11180. ("toc" . org-export-with-toc)
  11181. ("\\n" . org-export-preserve-breaks)
  11182. ("@" . org-export-html-expand)
  11183. (":" . org-export-with-fixed-width)
  11184. ("|" . org-export-with-tables)
  11185. ("^" . org-export-with-sub-superscripts)
  11186. ("*" . org-export-with-emphasize)
  11187. ("TeX" . org-export-with-TeX-macros)))
  11188. o)
  11189. (while (setq o (pop op))
  11190. (if (string-match (concat (regexp-quote (car o)) ":\\([^ \t\n\r;,.]*\\)")
  11191. s)
  11192. (set (make-local-variable (cdr o))
  11193. (car (read-from-string (match-string 1 s))))))))
  11194. (defun org-html-level-start (level title umax with-toc head-count)
  11195. "Insert a new level in HTML export."
  11196. (let ((l (1+ (max level umax))))
  11197. (while (<= l org-level-max)
  11198. (if (aref levels-open (1- l))
  11199. (progn
  11200. (org-html-level-close l)
  11201. (aset levels-open (1- l) nil)))
  11202. (setq l (1+ l)))
  11203. (if (> level umax)
  11204. (progn
  11205. (if (aref levels-open (1- level))
  11206. (insert "<li>" title "<p>\n")
  11207. (aset levels-open (1- level) t)
  11208. (insert "<ul><li>" title "<p>\n")))
  11209. (if org-export-with-section-numbers
  11210. (setq title (concat (org-section-number level) " " title)))
  11211. (setq level (+ level 1))
  11212. (if with-toc
  11213. (insert (format "\n<H%d><a name=\"sec-%d\">%s</a></H%d>\n"
  11214. level head-count title level))
  11215. (insert (format "\n<H%d>%s</H%d>\n" level title level))))))
  11216. (defun org-html-level-close (&rest args)
  11217. "Terminate one level in HTML export."
  11218. (insert "</ul>"))
  11219. ;; Variable holding the vector with section numbers
  11220. (defvar org-section-numbers (make-vector org-level-max 0))
  11221. (defun org-init-section-numbers ()
  11222. "Initialize the vector for the section numbers."
  11223. (let* ((level -1)
  11224. (numbers (nreverse (org-split-string "" "\\.")))
  11225. (depth (1- (length org-section-numbers)))
  11226. (i depth) number-string)
  11227. (while (>= i 0)
  11228. (if (> i level)
  11229. (aset org-section-numbers i 0)
  11230. (setq number-string (or (car numbers) "0"))
  11231. (if (string-match "\\`[A-Z]\\'" number-string)
  11232. (aset org-section-numbers i
  11233. (- (string-to-char number-string) ?A -1))
  11234. (aset org-section-numbers i (string-to-number number-string)))
  11235. (pop numbers))
  11236. (setq i (1- i)))))
  11237. (defun org-section-number (&optional level)
  11238. "Return a string with the current section number.
  11239. When LEVEL is non-nil, increase section numbers on that level."
  11240. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  11241. (when level
  11242. (when (> level -1)
  11243. (aset org-section-numbers
  11244. level (1+ (aref org-section-numbers level))))
  11245. (setq idx (1+ level))
  11246. (while (<= idx depth)
  11247. (if (not (= idx 1))
  11248. (aset org-section-numbers idx 0))
  11249. (setq idx (1+ idx))))
  11250. (setq idx 0)
  11251. (while (<= idx depth)
  11252. (setq n (aref org-section-numbers idx))
  11253. (setq string (concat string (if (not (string= string "")) "." "")
  11254. (int-to-string n)))
  11255. (setq idx (1+ idx)))
  11256. (save-match-data
  11257. (if (string-match "\\`\\([@0]\\.\\)+" string)
  11258. (setq string (replace-match "" nil nil string)))
  11259. (if (string-match "\\(\\.0\\)+\\'" string)
  11260. (setq string (replace-match "" nil nil string))))
  11261. string))
  11262. (defun org-export-icalendar-this-file ()
  11263. "Export current file as an iCalendar file.
  11264. The iCalendar file will be located in the same directory as the Org-mode
  11265. file, but with extension `.ics'."
  11266. (interactive)
  11267. (org-export-icalendar nil buffer-file-name))
  11268. ;;;###autoload
  11269. (defun org-export-icalendar-all-agenda-files ()
  11270. "Export all files in `org-agenda-files' to iCalendar .ics files.
  11271. Each iCalendar file will be located in the same directory as the Org-mode
  11272. file, but with extension `.ics'."
  11273. (interactive)
  11274. (apply 'org-export-icalendar nil (org-agenda-files t)))
  11275. ;;;###autoload
  11276. (defun org-export-icalendar-combine-agenda-files ()
  11277. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  11278. The file is stored under the name `org-combined-agenda-icalendar-file'."
  11279. (interactive)
  11280. (apply 'org-export-icalendar t (org-agenda-files t)))
  11281. (defun org-export-icalendar (combine &rest files)
  11282. "Create iCalendar files for all elements of FILES.
  11283. If COMBINE is non-nil, combine all calendar entries into a single large
  11284. file and store it under the name `org-combined-agenda-icalendar-file'."
  11285. (save-excursion
  11286. (let* (file ical-file ical-buffer category started org-agenda-new-buffers)
  11287. (when combine
  11288. (setq ical-file org-combined-agenda-icalendar-file
  11289. ical-buffer (org-get-agenda-file-buffer ical-file))
  11290. (set-buffer ical-buffer) (erase-buffer))
  11291. (while (setq file (pop files))
  11292. (catch 'nextfile
  11293. (org-check-agenda-file file)
  11294. (unless combine
  11295. (setq ical-file (concat (file-name-sans-extension file) ".ics"))
  11296. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  11297. (set-buffer ical-buffer) (erase-buffer))
  11298. (set-buffer (org-get-agenda-file-buffer file))
  11299. (setq category (or org-category
  11300. (file-name-sans-extension
  11301. (file-name-nondirectory buffer-file-name))))
  11302. (if (symbolp category) (setq category (symbol-name category)))
  11303. (let ((standard-output ical-buffer))
  11304. (if combine
  11305. (and (not started) (setq started t)
  11306. (org-start-icalendar-file org-icalendar-combined-name))
  11307. (org-start-icalendar-file category))
  11308. (org-print-icalendar-entries combine category)
  11309. (when (or (and combine (not files)) (not combine))
  11310. (org-finish-icalendar-file)
  11311. (set-buffer ical-buffer)
  11312. (save-buffer)
  11313. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  11314. (org-release-buffers org-agenda-new-buffers))))
  11315. (defvar org-after-save-iCalendar-file-hook nil
  11316. "Hook run after an iCalendar file has been saved.
  11317. The iCalendar buffer is still current when this hook is run.
  11318. A good way to use this is to tell a desktop calenndar application to re-read
  11319. the iCalendar file.")
  11320. (defun org-print-icalendar-entries (&optional combine category)
  11321. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  11322. When COMBINE is non nil, add the category to each line."
  11323. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  11324. (dts (org-ical-ts-to-string
  11325. (format-time-string (cdr org-time-stamp-formats) (current-time))
  11326. "DTSTART"))
  11327. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  11328. (save-excursion
  11329. (goto-char (point-min))
  11330. (while (re-search-forward org-ts-regexp nil t)
  11331. (setq pos (match-beginning 0)
  11332. ts (match-string 0)
  11333. inc t
  11334. hd (org-get-heading))
  11335. (if (looking-at re2)
  11336. (progn
  11337. (goto-char (match-end 0))
  11338. (setq ts2 (match-string 1) inc nil))
  11339. (setq ts2 ts
  11340. tmp (buffer-substring (max (point-min)
  11341. (- pos org-ds-keyword-length))
  11342. pos)
  11343. deadlinep (string-match org-deadline-regexp tmp)
  11344. scheduledp (string-match org-scheduled-regexp tmp)
  11345. ;; donep (org-entry-is-done-p)
  11346. ))
  11347. (if (or (string-match org-tr-regexp hd)
  11348. (string-match org-ts-regexp hd))
  11349. (setq hd (replace-match "" t t hd)))
  11350. (if combine
  11351. (setq hd (concat hd " (category " category ")")))
  11352. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  11353. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  11354. (princ (format "BEGIN:VEVENT
  11355. %s
  11356. %s
  11357. SUMMARY:%s
  11358. END:VEVENT\n"
  11359. (org-ical-ts-to-string ts "DTSTART")
  11360. (org-ical-ts-to-string ts2 "DTEND" inc)
  11361. hd)))
  11362. (when org-icalendar-include-todo
  11363. (goto-char (point-min))
  11364. (while (re-search-forward org-todo-line-regexp nil t)
  11365. (setq state (match-string 1))
  11366. (unless (equal state org-done-string)
  11367. (setq hd (match-string 3))
  11368. (if (string-match org-priority-regexp hd)
  11369. (setq pri (string-to-char (match-string 2 hd))
  11370. hd (concat (substring hd 0 (match-beginning 1))
  11371. (substring hd (- (match-end 1)))))
  11372. (setq pri org-default-priority))
  11373. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  11374. (- org-lowest-priority ?A))))))
  11375. (princ (format "BEGIN:VTODO
  11376. %s
  11377. SUMMARY:%s
  11378. SEQUENCE:1
  11379. PRIORITY:%d
  11380. END:VTODO\n"
  11381. dts hd pri))))))))
  11382. (defun org-start-icalendar-file (name)
  11383. "Start an iCalendar file by inserting the header."
  11384. (let ((user user-full-name)
  11385. (name (or name "unknown"))
  11386. (timezone (cadr (current-time-zone))))
  11387. (princ
  11388. ;; FIXME: Should we use VERSION 1? MS Outlook would then work, it seems
  11389. (format "BEGIN:VCALENDAR
  11390. VERSION:2.0
  11391. X-WR-CALNAME:%s
  11392. PRODID:-//%s//Emacs with Org-mode//EN
  11393. X-WR-TIMEZONE:%s
  11394. CALSCALE:GREGORIAN\n" name user timezone))))
  11395. (defun org-finish-icalendar-file ()
  11396. "Finish an iCalendar file by inserting the END statement."
  11397. (princ "END:VCALENDAR\n"))
  11398. (defun org-ical-ts-to-string (s keyword &optional inc)
  11399. "Take a time string S and convert it to iCalendar format.
  11400. KEYWORD is added in front, to make a complete line like DTSTART....
  11401. When INC is non-nil, increase the hour by two (if time string contains
  11402. a time), or the day by one (if it does not contain a time)."
  11403. (let ((t1 (org-parse-time-string s 'nodefault))
  11404. t2 fmt have-time time)
  11405. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  11406. (setq t2 t1 have-time t)
  11407. (setq t2 (org-parse-time-string s)))
  11408. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  11409. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  11410. (when inc
  11411. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  11412. (setq time (encode-time s mi h d m y)))
  11413. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  11414. (concat keyword (format-time-string fmt time))))
  11415. ;;; Key bindings
  11416. ;; - Bindings in Org-mode map are currently
  11417. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  11418. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  11419. ;; e (?) useful from outline-mode
  11420. ;; i k @ expendable from outline-mode
  11421. ;; 0123456789 % & ()_{} " ` free
  11422. ;; Make `C-c C-x' a prefix key
  11423. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11424. ;; TAB key with modifiers
  11425. (define-key org-mode-map "\C-i" 'org-cycle)
  11426. (define-key org-mode-map [(tab)] 'org-cycle)
  11427. (define-key org-mode-map [(meta tab)] 'org-complete)
  11428. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  11429. ;; The following line is necessary under Suse GNU/Linux
  11430. (unless (featurep 'xemacs)
  11431. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  11432. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  11433. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  11434. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  11435. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11436. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  11437. (define-key org-mode-map [(meta return)] 'org-meta-return)
  11438. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  11439. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  11440. ;; Cursor keys with modifiers
  11441. (define-key org-mode-map [(meta left)] 'org-metaleft)
  11442. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  11443. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  11444. (define-key org-mode-map [(meta right)] 'org-metaright)
  11445. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  11446. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  11447. (define-key org-mode-map [(meta up)] 'org-metaup)
  11448. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  11449. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  11450. (define-key org-mode-map [(meta down)] 'org-metadown)
  11451. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  11452. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  11453. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11454. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  11455. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11456. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  11457. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11458. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  11459. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11460. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  11461. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  11462. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  11463. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  11464. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  11465. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  11466. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  11467. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  11468. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  11469. ;; All the other keys
  11470. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  11471. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  11472. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  11473. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  11474. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  11475. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  11476. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11477. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  11478. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  11479. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11480. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  11481. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  11482. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  11483. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  11484. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  11485. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  11486. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  11487. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11488. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11489. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11490. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11491. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  11492. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  11493. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11494. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11495. (define-key org-mode-map "\C-c]" 'org-remove-file)
  11496. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  11497. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  11498. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  11499. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11500. (define-key org-mode-map "\C-m" 'org-return)
  11501. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  11502. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  11503. (define-key org-mode-map "\C-c+" 'org-table-sum)
  11504. (define-key org-mode-map "\C-c|" 'org-table-toggle-vline-visibility)
  11505. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  11506. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  11507. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  11508. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  11509. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11510. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11511. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11512. (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  11513. (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  11514. (define-key org-mode-map "\C-c\C-xv" 'org-export-copy-visible)
  11515. (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-copy-visible)
  11516. ;; OPML support is only an option for the future
  11517. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  11518. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  11519. (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  11520. (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  11521. (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  11522. (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  11523. (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  11524. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11525. (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  11526. (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  11527. (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  11528. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11529. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11530. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11531. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11532. (defsubst org-table-p () (org-at-table-p))
  11533. (defun org-self-insert-command (N)
  11534. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11535. If the cursor is in a table looking at whitespace, the whitespace is
  11536. overwritten, and the table is not marked as requiring realignment."
  11537. (interactive "p")
  11538. (if (and (org-table-p)
  11539. (or
  11540. (and org-table-auto-blank-field
  11541. (member last-command
  11542. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11543. (org-table-blank-field))
  11544. t)
  11545. (eq N 1)
  11546. (looking-at "[^|\n]* +|"))
  11547. (let (org-table-may-need-update)
  11548. (goto-char (1- (match-end 0)))
  11549. (delete-backward-char 1)
  11550. (goto-char (match-beginning 0))
  11551. (self-insert-command N))
  11552. (setq org-table-may-need-update t)
  11553. (self-insert-command N)))
  11554. ;; FIXME:
  11555. ;; The following two functions might still be optimized to trigger
  11556. ;; re-alignment less frequently.
  11557. (defun org-delete-backward-char (N)
  11558. "Like `delete-backward-char', insert whitespace at field end in tables.
  11559. When deleting backwards, in tables this function will insert whitespace in
  11560. front of the next \"|\" separator, to keep the table aligned. The table will
  11561. still be marked for re-alignment, because a narrow field may lead to a
  11562. reduced column width."
  11563. (interactive "p")
  11564. (if (and (org-table-p)
  11565. (eq N 1)
  11566. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11567. (looking-at ".*?|"))
  11568. (let ((pos (point)))
  11569. (backward-delete-char N)
  11570. (skip-chars-forward "^|")
  11571. (insert " ")
  11572. (goto-char (1- pos)))
  11573. (backward-delete-char N)))
  11574. (defun org-delete-char (N)
  11575. "Like `delete-char', but insert whitespace at field end in tables.
  11576. When deleting characters, in tables this function will insert whitespace in
  11577. front of the next \"|\" separator, to keep the table aligned. The table
  11578. will still be marked for re-alignment, because a narrow field may lead to
  11579. a reduced column width."
  11580. (interactive "p")
  11581. (if (and (org-table-p)
  11582. (not (bolp))
  11583. (not (= (char-after) ?|))
  11584. (eq N 1))
  11585. (if (looking-at ".*?|")
  11586. (let ((pos (point)))
  11587. (replace-match (concat
  11588. (substring (match-string 0) 1 -1)
  11589. " |"))
  11590. (goto-char pos)))
  11591. (delete-char N)))
  11592. ;; How to do this: Measure non-white length of current string
  11593. ;; If equal to column width, we should realign.
  11594. (defun org-remap (map &rest commands)
  11595. "In MAP, remap the functions given in COMMANDS.
  11596. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11597. (let (new old)
  11598. (while commands
  11599. (setq old (pop commands) new (pop commands))
  11600. (if (fboundp 'command-remapping)
  11601. (define-key map (vector 'remap old) new)
  11602. (substitute-key-definition old new map global-map)))))
  11603. (when (eq org-enable-table-editor 'optimized)
  11604. ;; If the user wants maximum table support, we need to hijack
  11605. ;; some standard editing functions
  11606. (org-remap org-mode-map
  11607. 'self-insert-command 'org-self-insert-command
  11608. 'delete-char 'org-delete-char
  11609. 'delete-backward-char 'org-delete-backward-char)
  11610. (define-key org-mode-map "|" 'org-force-self-insert))
  11611. (defun org-shiftcursor-error ()
  11612. "Throw an error because Shift-Cursor command was applied in wrong context."
  11613. (error "This command is active in special context like tables, headlines or timestamps"))
  11614. (defun org-shifttab ()
  11615. "Global visibility cycling or move to previous table field.
  11616. Calls `(org-cycle t)' or `org-table-previous-field', depending on context.
  11617. See the individual commands for more information."
  11618. (interactive)
  11619. (cond
  11620. ((org-at-table-p) (org-table-previous-field))
  11621. (t (org-cycle '(4)))))
  11622. (defun org-shiftmetaleft ()
  11623. "Promote subtree or delete table column.
  11624. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  11625. See the individual commands for more information."
  11626. (interactive)
  11627. (cond
  11628. ((org-at-table-p) (org-table-delete-column))
  11629. ((org-on-heading-p) (org-promote-subtree))
  11630. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11631. (t (org-shiftcursor-error))))
  11632. (defun org-shiftmetaright ()
  11633. "Demote subtree or insert table column.
  11634. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  11635. See the individual commands for more information."
  11636. (interactive)
  11637. (cond
  11638. ((org-at-table-p) (org-table-insert-column))
  11639. ((org-on-heading-p) (org-demote-subtree))
  11640. ((org-at-item-p) (call-interactively 'org-indent-item))
  11641. (t (org-shiftcursor-error))))
  11642. (defun org-shiftmetaup (&optional arg)
  11643. "Move subtree up or kill table row.
  11644. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11645. `org-move-item-up' depending on context. See the individual commands
  11646. for more information."
  11647. (interactive "P")
  11648. (cond
  11649. ((org-at-table-p) (org-table-kill-row))
  11650. ((org-on-heading-p) (org-move-subtree-up arg))
  11651. ((org-at-item-p) (org-move-item-up arg))
  11652. (t (org-shiftcursor-error))))
  11653. (defun org-shiftmetadown (&optional arg)
  11654. "Move subtree down or insert table row.
  11655. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11656. `org-move-item-down', depending on context. See the individual
  11657. commands for more information."
  11658. (interactive "P")
  11659. (cond
  11660. ((org-at-table-p) (org-table-insert-row arg))
  11661. ((org-on-heading-p) (org-move-subtree-down arg))
  11662. ((org-at-item-p) (org-move-item-down arg))
  11663. (t (org-shiftcursor-error))))
  11664. (defun org-metaleft (&optional arg)
  11665. "Promote heading or move table column to left.
  11666. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11667. With no specific context, calls the Emacs default `backward-word'.
  11668. See the individual commands for more information."
  11669. (interactive "P")
  11670. (cond
  11671. ((org-at-table-p) (org-table-move-column 'left))
  11672. ((or (org-on-heading-p) (org-region-active-p)) (org-do-promote))
  11673. (t (backward-word (prefix-numeric-value arg)))))
  11674. (defun org-metaright (&optional arg)
  11675. "Demote subtree or move table column to right.
  11676. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11677. With no specific context, calls the Emacs default `forward-word'.
  11678. See the individual commands for more information."
  11679. (interactive "P")
  11680. (cond
  11681. ((org-at-table-p) (org-table-move-column nil))
  11682. ((or (org-on-heading-p) (org-region-active-p)) (org-do-demote))
  11683. (t (forward-word (prefix-numeric-value arg)))))
  11684. (defun org-metaup (&optional arg)
  11685. "Move subtree up or move table row up.
  11686. Calls `org-move-subtree-up' or `org-table-move-row' or
  11687. `org-move-item-up', depending on context. See the individual commands
  11688. for more information."
  11689. (interactive "P")
  11690. (cond
  11691. ((org-at-table-p) (org-table-move-row 'up))
  11692. ((org-on-heading-p) (org-move-subtree-up arg))
  11693. ((org-at-item-p) (org-move-item-up arg))
  11694. (t (org-shiftcursor-error))))
  11695. (defun org-metadown (&optional arg)
  11696. "Move subtree down or move table row down.
  11697. Calls `org-move-subtree-down' or `org-table-move-row' or
  11698. `org-move-item-down', depending on context. See the individual
  11699. commands for more information."
  11700. (interactive "P")
  11701. (cond
  11702. ((org-at-table-p) (org-table-move-row nil))
  11703. ((org-on-heading-p) (org-move-subtree-down arg))
  11704. ((org-at-item-p) (org-move-item-down arg))
  11705. (t (org-shiftcursor-error))))
  11706. (defun org-shiftup (&optional arg)
  11707. "Increase item in timestamp or increase priority of current item.
  11708. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  11709. See the individual commands for more information."
  11710. (interactive "P")
  11711. (cond
  11712. ((org-at-timestamp-p) (org-timestamp-up arg))
  11713. (t (org-priority-up))))
  11714. (defun org-shiftdown (&optional arg)
  11715. "Decrease item in timestamp or decrease priority of current item.
  11716. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  11717. See the individual commands for more information."
  11718. (interactive "P")
  11719. (cond
  11720. ((org-at-timestamp-p) (org-timestamp-down arg))
  11721. (t (org-priority-down))))
  11722. (defun org-shiftright ()
  11723. "Next TODO keyword or timestamp one day later, depending on context."
  11724. (interactive)
  11725. (cond
  11726. ((org-at-timestamp-p) (org-timestamp-up-day))
  11727. ((org-on-heading-p) (org-todo 'right))
  11728. (t (org-shiftcursor-error))))
  11729. (defun org-shiftleft ()
  11730. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11731. (interactive)
  11732. (cond
  11733. ((org-at-timestamp-p) (org-timestamp-down-day))
  11734. ((org-on-heading-p) (org-todo 'left))
  11735. (t (org-shiftcursor-error))))
  11736. (defun org-copy-special ()
  11737. "Copy region in table or copy current subtree.
  11738. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11739. See the individual commands for more information."
  11740. (interactive)
  11741. (call-interactively
  11742. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11743. (defun org-cut-special ()
  11744. "Cut region in table or cut current subtree.
  11745. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11746. See the individual commands for more information."
  11747. (interactive)
  11748. (call-interactively
  11749. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11750. (defun org-paste-special (arg)
  11751. "Paste rectangular region into table, or past subtree relative to level.
  11752. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11753. See the individual commands for more information."
  11754. (interactive "P")
  11755. (if (org-at-table-p)
  11756. (org-table-paste-rectangle)
  11757. (org-paste-subtree arg)))
  11758. (defun org-ctrl-c-ctrl-c (&optional arg)
  11759. "Call realign table, or recognize a table.el table, or update keywords.
  11760. When the cursor is inside a table created by the table.el package,
  11761. activate that table. Otherwise, if the cursor is at a normal table
  11762. created with org.el, re-align that table. This command works even if
  11763. the automatic table editor has been turned off.
  11764. If the cursor is in a headline, prompt for tags and insert them into
  11765. the current line, aligned to `org-tags-column'. When in a headline and
  11766. called with prefix arg, realign all tags in the current buffer.
  11767. If the cursor is in one of the special #+KEYWORD lines, this triggers
  11768. scanning the buffer for these lines and updating the information.
  11769. If the cursor is on a #+TBLFM line, re-apply the formulae to the table."
  11770. (interactive "P")
  11771. (let ((org-enable-table-editor t))
  11772. (cond
  11773. ((and (local-variable-p 'org-finish-function (current-buffer))
  11774. (fboundp org-finish-function))
  11775. (funcall org-finish-function))
  11776. ; ((and (boundp 'remember-buffer) (equal (buffer-name) remember-buffer))
  11777. ; (remember-buffer)) ;; FIXME: remove this??????
  11778. ((org-on-target-p) (org-update-radio-target-regexp))
  11779. ((org-on-heading-p) (org-set-tags arg))
  11780. ((org-at-table.el-p)
  11781. (require 'table)
  11782. (beginning-of-line 1)
  11783. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11784. (table-recognize-table))
  11785. ((org-at-table-p)
  11786. (org-table-maybe-eval-formula)
  11787. (if arg
  11788. (org-table-recalculate t)
  11789. (org-table-maybe-recalculate-line))
  11790. (org-table-align))
  11791. ((org-at-item-p)
  11792. (org-renumber-ordered-list (prefix-numeric-value arg)))
  11793. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11794. (cond
  11795. ((equal (match-string 1) "TBLFM")
  11796. ;; Recalculate the table before this line
  11797. (save-excursion
  11798. (beginning-of-line 1)
  11799. (skip-chars-backward " \r\n\t")
  11800. (if (org-at-table-p) (org-table-recalculate t))))
  11801. (t
  11802. (org-mode-restart))))
  11803. ((org-region-active-p)
  11804. (org-table-convert-region (region-beginning) (region-end) arg))
  11805. ((condition-case nil
  11806. (and (region-beginning) (region-end))
  11807. (error nil))
  11808. (if (y-or-n-p "Convert inactive region to table? ")
  11809. (org-table-convert-region (region-beginning) (region-end) arg)
  11810. (error "Abort")))
  11811. (t (error "C-c C-c can do nothing useful at this location.")))))
  11812. (defun org-mode-restart ()
  11813. "Restart Org-mode, to scan again for special lines.
  11814. Also updates the keyword regular expressions."
  11815. (interactive)
  11816. (let ((org-inhibit-startup t)) (org-mode))
  11817. (message "Org-mode restarted to refresh keyword and special line setup"))
  11818. (defun org-return ()
  11819. "Goto next table row or insert a newline.
  11820. Calls `org-table-next-row' or `newline', depending on context.
  11821. See the individual commands for more information."
  11822. (interactive)
  11823. (cond
  11824. ((org-at-table-p)
  11825. (org-table-justify-field-maybe)
  11826. (org-table-next-row))
  11827. (t (newline))))
  11828. (defun org-meta-return (&optional arg)
  11829. "Insert a new heading or wrap a region in a table.
  11830. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11831. See the individual commands for more information."
  11832. (interactive "P")
  11833. (cond
  11834. ((org-at-table-p)
  11835. (org-table-wrap-region arg))
  11836. (t (org-insert-heading arg))))
  11837. ;;; Menu entries
  11838. ;; Define the Org-mode menus
  11839. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11840. '("Tbl"
  11841. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  11842. ["Next Field" org-cycle (org-at-table-p)]
  11843. ["Previous Field" org-shifttab (org-at-table-p)]
  11844. ["Next Row" org-return (org-at-table-p)]
  11845. "--"
  11846. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11847. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11848. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11849. "--"
  11850. ("Column"
  11851. ["Move Column Left" org-metaleft (org-at-table-p)]
  11852. ["Move Column Right" org-metaright (org-at-table-p)]
  11853. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11854. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  11855. "--"
  11856. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  11857. ("Row"
  11858. ["Move Row Up" org-metaup (org-at-table-p)]
  11859. ["Move Row Down" org-metadown (org-at-table-p)]
  11860. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11861. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11862. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11863. "--"
  11864. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  11865. ("Rectangle"
  11866. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11867. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11868. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11869. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11870. "--"
  11871. ("Calculate"
  11872. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11873. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11874. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11875. "--"
  11876. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11877. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11878. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11879. "--"
  11880. ["Sum Column/Rectangle" org-table-sum
  11881. (or (org-at-table-p) (org-region-active-p))]
  11882. ["Which Column?" org-table-current-column (org-at-table-p)])
  11883. ["Debug Formulas"
  11884. (setq org-table-formula-debug (not org-table-formula-debug))
  11885. :style toggle :selected org-table-formula-debug]
  11886. "--"
  11887. ["Invisible Vlines" org-table-toggle-vline-visibility
  11888. :style toggle :selected (org-in-invisibility-spec-p '(org-table))]
  11889. "--"
  11890. ["Create" org-table-create (and (not (org-at-table-p))
  11891. org-enable-table-editor)]
  11892. ["Convert Region" org-ctrl-c-ctrl-c (not (org-at-table-p 'any))]
  11893. ["Import from File" org-table-import (not (org-at-table-p))]
  11894. ["Export to File" org-table-export (org-at-table-p)]
  11895. "--"
  11896. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11897. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11898. '("Org"
  11899. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  11900. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  11901. ["Sparse Tree" org-occur t]
  11902. ["Show All" show-all t]
  11903. "--"
  11904. ["New Heading" org-insert-heading t]
  11905. ("Navigate Headings"
  11906. ["Up" outline-up-heading t]
  11907. ["Next" outline-next-visible-heading t]
  11908. ["Previous" outline-previous-visible-heading t]
  11909. ["Next Same Level" outline-forward-same-level t]
  11910. ["Previous Same Level" outline-backward-same-level t]
  11911. "--"
  11912. ["Jump" org-goto t])
  11913. ("Edit Structure"
  11914. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11915. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11916. "--"
  11917. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11918. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11919. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11920. "--"
  11921. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11922. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11923. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11924. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11925. "--"
  11926. ["Archive Subtree" org-archive-subtree t]
  11927. "--"
  11928. ["Convert to odd levels" org-convert-to-odd-levels t]
  11929. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11930. "--"
  11931. ("TODO Lists"
  11932. ["TODO/DONE/-" org-todo t]
  11933. ["Show TODO Tree" org-show-todo-tree t]
  11934. ["Global TODO list" org-todo-list t]
  11935. "--"
  11936. ["Set Priority" org-priority t]
  11937. ["Priority Up" org-shiftup t]
  11938. ["Priority Down" org-shiftdown t])
  11939. ("Dates and Scheduling"
  11940. ["Timestamp" org-time-stamp t]
  11941. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11942. ("Change Date"
  11943. ["1 Day Later" org-timestamp-up-day t]
  11944. ["1 Day Earlier" org-timestamp-down-day t]
  11945. ["1 ... Later" org-shiftup t]
  11946. ["1 ... Earlier" org-shiftdown t])
  11947. ["Compute Time Range" org-evaluate-time-range t]
  11948. ["Schedule Item" org-schedule t]
  11949. ["Deadline" org-deadline t]
  11950. "--"
  11951. ["Goto Calendar" org-goto-calendar t]
  11952. ["Date from Calendar" org-date-from-calendar t])
  11953. "--"
  11954. ["Agenda Command" org-agenda t]
  11955. ("File List for Agenda")
  11956. ("Special views current file"
  11957. ["TODO Tree" org-show-todo-tree t]
  11958. ["Check Deadlines" org-check-deadlines t]
  11959. ["Timeline" org-timeline t]
  11960. ["Tags Tree" org-tags-sparse-tree t])
  11961. "--"
  11962. ("Hyperlinks"
  11963. ["Store Link (Global)" org-store-link t]
  11964. ["Insert Link" org-insert-link t]
  11965. ["Follow Link" org-open-at-point t]
  11966. "--"
  11967. ["Descriptive Links"
  11968. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11969. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  11970. ["Literal Links"
  11971. (progn
  11972. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11973. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  11974. )
  11975. "--"
  11976. ("Export"
  11977. ["ASCII" org-export-as-ascii t]
  11978. ["Extract Visible Text" org-export-copy-visible t]
  11979. ["HTML" org-export-as-html t]
  11980. ["HTML and Open" org-export-as-html-and-open t]
  11981. ; ["OPML" org-export-as-opml nil]
  11982. "--"
  11983. ["iCalendar this file" org-export-icalendar-this-file t]
  11984. ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
  11985. :active t :keys "C-c C-x C-i"]
  11986. ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
  11987. "--"
  11988. ["Option Template" org-insert-export-options-template t]
  11989. ["Toggle Fixed Width" org-toggle-fixed-width-section t])
  11990. "--"
  11991. ("Documentation"
  11992. ["Show Version" org-version t]
  11993. ["Info Documentation" org-info t])
  11994. ("Customize"
  11995. ["Browse Org Group" org-customize t]
  11996. "--"
  11997. ["Build Full Customize Menu" org-create-customize-menu
  11998. (fboundp 'customize-menu-create)])
  11999. "--"
  12000. ["Refresh setup" org-mode-restart t]
  12001. ))
  12002. (defun org-info (&optional node)
  12003. "Read documentation for Org-mode in the info system.
  12004. With optional NODE, go directly to that node."
  12005. (interactive)
  12006. (require 'info)
  12007. (Info-goto-node (format "(org)%s" (or node ""))))
  12008. (defun org-install-agenda-files-menu ()
  12009. (let ((bl (buffer-list)))
  12010. (save-excursion
  12011. (while bl
  12012. (set-buffer (pop bl))
  12013. (if (eq major-mode 'org-mode) (setq bl nil)))
  12014. (when (eq major-mode 'org-mode)
  12015. (easy-menu-change
  12016. '("Org") "File List for Agenda"
  12017. (append
  12018. (list
  12019. ["Edit File List" (org-edit-agenda-file-list) t]
  12020. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12021. ["Remove Current File from List" org-remove-file t]
  12022. ["Cycle through agenda files" org-cycle-agenda-files t]
  12023. "--")
  12024. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12025. ;;; Documentation
  12026. (defun org-customize ()
  12027. "Call the customize function with org as argument."
  12028. (interactive)
  12029. (customize-browse 'org))
  12030. (defun org-create-customize-menu ()
  12031. "Create a full customization menu for Org-mode, insert it into the menu."
  12032. (interactive)
  12033. (if (fboundp 'customize-menu-create)
  12034. (progn
  12035. (easy-menu-change
  12036. '("Org") "Customize"
  12037. `(["Browse Org group" org-customize t]
  12038. "--"
  12039. ,(customize-menu-create 'org)
  12040. ["Set" Custom-set t]
  12041. ["Save" Custom-save t]
  12042. ["Reset to Current" Custom-reset-current t]
  12043. ["Reset to Saved" Custom-reset-saved t]
  12044. ["Reset to Standard Settings" Custom-reset-standard t]))
  12045. (message "\"Org\"-menu now contains full customization menu"))
  12046. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12047. ;;; Miscellaneous stuff
  12048. (defun org-move-line-down (arg)
  12049. "Move the current line down. With prefix argument, move it past ARG lines."
  12050. (interactive "p")
  12051. (let ((col (current-column))
  12052. beg end pos)
  12053. (beginning-of-line 1) (setq beg (point))
  12054. (beginning-of-line 2) (setq end (point))
  12055. (beginning-of-line (+ 1 arg))
  12056. (setq pos (move-marker (make-marker) (point)))
  12057. (insert (delete-and-extract-region beg end))
  12058. (goto-char pos)
  12059. (move-to-column col)))
  12060. (defun org-move-line-up (arg)
  12061. "Move the current line up. With prefix argument, move it past ARG lines."
  12062. (interactive "p")
  12063. (let ((col (current-column))
  12064. beg end pos)
  12065. (beginning-of-line 1) (setq beg (point))
  12066. (beginning-of-line 2) (setq end (point))
  12067. (beginning-of-line (- arg))
  12068. (setq pos (move-marker (make-marker) (point)))
  12069. (insert (delete-and-extract-region beg end))
  12070. (goto-char pos)
  12071. (move-to-column col)))
  12072. ;; Paragraph filling stuff.
  12073. ;; We want this to be just right, so use the full arsenal.
  12074. ;; FIXME: This very likely does not work correctly for XEmacs, because the
  12075. ;; filladapt package works slightly differently.
  12076. (defun org-set-autofill-regexps ()
  12077. (interactive)
  12078. ;; In the paragraph separator we include headlines, because filling
  12079. ;; text in a line directly attached to a headline would otherwise
  12080. ;; fill the headline as well.
  12081. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  12082. ;; The paragraph starter includes hand-formatted lists.
  12083. (set (make-local-variable 'paragraph-start)
  12084. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12085. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12086. ;; But only if the user has not turned off tables or fixed-width regions
  12087. (set (make-local-variable 'auto-fill-inhibit-regexp)
  12088. (concat "\\*\\|#"
  12089. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12090. (concat
  12091. "\\|[ \t]*["
  12092. (if org-enable-table-editor "|" "")
  12093. (if org-enable-fixed-width-editor ":" "")
  12094. "]"))))
  12095. ;; We use our own fill-paragraph function, to make sure that tables
  12096. ;; and fixed-width regions are not wrapped. That function will pass
  12097. ;; through to `fill-paragraph' when appropriate.
  12098. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  12099. ;; Adaptive filling: To get full control, first make sure that
  12100. ;; `adaptive-fill-regexp' never matches. Then install our won matcher.
  12101. (setq adaptive-fill-regexp "\000")
  12102. (setq adaptive-fill-function 'org-adaptive-fill-function))
  12103. (defun org-fill-paragraph (&optional justify)
  12104. "Re-align a table, pass through to fill-paragraph if no table."
  12105. (let ((table-p (org-at-table-p))
  12106. (table.el-p (org-at-table.el-p)))
  12107. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  12108. (table.el-p t) ; skip table.el tables
  12109. (table-p (org-table-align) t) ; align org-mode tables
  12110. (t nil)))) ; call paragraph-fill
  12111. ;; For reference, this is the default value of adaptive-fill-regexp
  12112. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12113. (defun org-adaptive-fill-function ()
  12114. "Return a fill prefix for org-mode files.
  12115. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12116. work correctly."
  12117. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  12118. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  12119. ;; Functions needed for Emacs/XEmacs region compatibility
  12120. (defun org-add-hook (hook function &optional append local)
  12121. "Add-hook, compatible with both Emacsen."
  12122. (if (and local (featurep 'xemacs))
  12123. (add-local-hook hook function append)
  12124. (add-hook hook function append local)))
  12125. (defun org-region-active-p ()
  12126. "Is `transient-mark-mode' on and the region active?
  12127. Works on both Emacs and XEmacs."
  12128. (if org-ignore-region
  12129. nil
  12130. (if (featurep 'xemacs)
  12131. (and zmacs-regions (region-active-p))
  12132. (and transient-mark-mode mark-active))))
  12133. (defun org-add-to-invisibility-spec (arg)
  12134. "Add elements to `buffer-invisibility-spec'.
  12135. See documentation for `buffer-invisibility-spec' for the kind of elements
  12136. that can be added."
  12137. (cond
  12138. ((fboundp 'add-to-invisibility-spec)
  12139. (add-to-invisibility-spec arg))
  12140. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  12141. (setq buffer-invisibility-spec (list arg)))
  12142. (t
  12143. (setq buffer-invisibility-spec
  12144. (cons arg buffer-invisibility-spec)))))
  12145. (defun org-remove-from-invisibility-spec (arg)
  12146. "Remove elements from `buffer-invisibility-spec'."
  12147. (if (fboundp 'remove-from-invisibility-spec)
  12148. (remove-from-invisibility-spec arg)
  12149. (if (consp buffer-invisibility-spec)
  12150. (setq buffer-invisibility-spec
  12151. (delete arg buffer-invisibility-spec)))))
  12152. (defun org-in-invisibility-spec-p (arg)
  12153. "Is ARG a member of `buffer-invisibility-spec'?"
  12154. (if (consp buffer-invisibility-spec)
  12155. (member arg buffer-invisibility-spec)
  12156. nil))
  12157. (defun org-image-file-name-regexp ()
  12158. "Return regexp matching the file names of images."
  12159. (if (fboundp 'image-file-name-regexp)
  12160. (image-file-name-regexp)
  12161. (let ((image-file-name-extensions
  12162. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12163. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12164. (concat "\\."
  12165. (regexp-opt (nconc (mapcar 'upcase
  12166. image-file-name-extensions)
  12167. image-file-name-extensions)
  12168. t)
  12169. "\\'"))))
  12170. ;; Functions needed for compatibility with old outline.el.
  12171. ;; Programming for the old outline.el (that uses selective display
  12172. ;; instead of `invisible' text properties) is a nightmare, mostly
  12173. ;; because regular expressions can no longer be anchored at
  12174. ;; beginning/end of line. Therefore a number of function need special
  12175. ;; treatment when the old outline.el is being used.
  12176. ;; The following functions capture almost the entire compatibility code
  12177. ;; between the different versions of outline-mode. The only other
  12178. ;; places where this is important are the font-lock-keywords, and in
  12179. ;; `org-export-copy-visible'. Search for `org-noutline-p' to find them.
  12180. ;; C-a should go to the beginning of a *visible* line, also in the
  12181. ;; new outline.el. I guess this should be patched into Emacs?
  12182. (defun org-beginning-of-line ()
  12183. "Go to the beginning of the current line. If that is invisible, continue
  12184. to a visible line beginning. This makes the function of C-a more intuitive."
  12185. (interactive)
  12186. (beginning-of-line 1)
  12187. (if (bobp)
  12188. nil
  12189. (backward-char 1)
  12190. (if (org-invisible-p)
  12191. (while (and (not (bobp)) (org-invisible-p))
  12192. (backward-char 1)
  12193. (beginning-of-line 1))
  12194. (forward-char 1))))
  12195. (when org-noutline-p
  12196. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  12197. ;; FIXME: should I use substitute-key-definition to reach other bindings
  12198. ;; of beginning-of-line?
  12199. (defun org-invisible-p ()
  12200. "Check if point is at a character currently not visible."
  12201. (if org-noutline-p
  12202. ;; Early versions of noutline don't have `outline-invisible-p'.
  12203. (if (fboundp 'outline-invisible-p)
  12204. (outline-invisible-p)
  12205. (get-char-property (point) 'invisible))
  12206. (save-excursion
  12207. (skip-chars-backward "^\r\n")
  12208. (equal (char-before) ?\r))))
  12209. (defun org-invisible-p2 ()
  12210. "Check if point is at a character currently not visible."
  12211. (save-excursion
  12212. (if org-noutline-p
  12213. (progn
  12214. (if (and (eolp) (not (bobp))) (backward-char 1))
  12215. ;; Early versions of noutline don't have `outline-invisible-p'.
  12216. (if (fboundp 'outline-invisible-p)
  12217. (outline-invisible-p)
  12218. (get-char-property (point) 'invisible)))
  12219. (skip-chars-backward "^\r\n")
  12220. (equal (char-before) ?\r))))
  12221. (defun org-back-to-heading (&optional invisible-ok)
  12222. "Move to previous heading line, or beg of this line if it's a heading.
  12223. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  12224. (if org-noutline-p
  12225. (outline-back-to-heading invisible-ok)
  12226. (if (and (memq (char-before) '(?\n ?\r))
  12227. (looking-at outline-regexp))
  12228. t
  12229. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  12230. outline-regexp)
  12231. nil t)
  12232. (if invisible-ok
  12233. (progn (goto-char (match-end 1))
  12234. (looking-at outline-regexp)))
  12235. (error "Before first heading")))))
  12236. (defun org-on-heading-p (&optional invisible-ok)
  12237. "Return t if point is on a (visible) heading line.
  12238. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  12239. (if org-noutline-p
  12240. (outline-on-heading-p 'invisible-ok)
  12241. (save-excursion
  12242. (skip-chars-backward "^\n\r")
  12243. (and (looking-at outline-regexp)
  12244. (or invisible-ok
  12245. (bobp)
  12246. (equal (char-before) ?\n))))))
  12247. (defun org-on-target-p ()
  12248. (let ((pos (point)))
  12249. (save-excursion
  12250. (skip-chars-forward "<")
  12251. (and (re-search-backward "<<" nil t)
  12252. (or (looking-at org-radio-target-regexp)
  12253. (looking-at org-target-regexp))
  12254. (<= (match-beginning 0) pos)
  12255. (>= (1+ (match-end 0)) pos)))))
  12256. (defun org-up-heading-all (arg)
  12257. "Move to the heading line of which the present line is a subheading.
  12258. This function considers both visible and invisible heading lines.
  12259. With argument, move up ARG levels."
  12260. (if org-noutline-p
  12261. (if (fboundp 'outline-up-heading-all)
  12262. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12263. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  12264. (org-back-to-heading t)
  12265. (looking-at outline-regexp)
  12266. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  12267. (error "Cannot move up %d levels" arg)
  12268. (re-search-backward
  12269. (concat "[\n\r]" (regexp-quote
  12270. (make-string (- (match-end 0) (match-beginning 0) arg)
  12271. ?*))
  12272. "[^*]"))
  12273. (forward-char 1))))
  12274. (defun org-show-hidden-entry ()
  12275. "Show an entry where even the heading is hidden."
  12276. (save-excursion
  12277. (if (not org-noutline-p)
  12278. (progn
  12279. (org-back-to-heading t)
  12280. (org-flag-heading nil)))
  12281. (org-show-entry)))
  12282. (defun org-check-occur-regexp (regexp)
  12283. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  12284. Of course, only for the old outline mode."
  12285. (if org-noutline-p
  12286. regexp
  12287. (if (string-match "^\\^" regexp)
  12288. (concat "[\n\r]" (substring regexp 1))
  12289. regexp)))
  12290. (defun org-flag-heading (flag &optional entry)
  12291. "Flag the current heading. FLAG non-nil means make invisible.
  12292. When ENTRY is non-nil, show the entire entry."
  12293. (save-excursion
  12294. (org-back-to-heading t)
  12295. (if (not org-noutline-p)
  12296. ;; Make the current headline visible
  12297. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  12298. ;; Check if we should show the entire entry
  12299. (if entry
  12300. (progn
  12301. (org-show-entry)
  12302. (save-excursion ;; FIXME: Is this the fix for points in the -|
  12303. ;; middle of text? |
  12304. (and (outline-next-heading) ;; |
  12305. (org-flag-heading nil)))) ; show the next heading _|
  12306. (outline-flag-region (max 1 (1- (point)))
  12307. (save-excursion (outline-end-of-heading) (point))
  12308. (if org-noutline-p
  12309. flag
  12310. (if flag ?\r ?\n))))))
  12311. (defun org-end-of-subtree (&optional invisible-OK)
  12312. ;; This is an exact copy of the original function, but it uses
  12313. ;; `org-back-to-heading', to make it work also in invisible
  12314. ;; trees. And is uses an invisible-OK argument.
  12315. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12316. (org-back-to-heading invisible-OK)
  12317. (let ((first t)
  12318. (level (funcall outline-level)))
  12319. (while (and (not (eobp))
  12320. (or first (> (funcall outline-level) level)))
  12321. (setq first nil)
  12322. (outline-next-heading))
  12323. (if (memq (preceding-char) '(?\n ?\^M))
  12324. (progn
  12325. ;; Go to end of line before heading
  12326. (forward-char -1)
  12327. (if (memq (preceding-char) '(?\n ?\^M))
  12328. ;; leave blank line before heading
  12329. (forward-char -1))))))
  12330. (defun org-show-subtree ()
  12331. "Show everything after this heading at deeper levels."
  12332. (outline-flag-region
  12333. (point)
  12334. (save-excursion
  12335. (outline-end-of-subtree) (outline-next-heading) (point))
  12336. (if org-noutline-p nil ?\n)))
  12337. (defun org-show-entry ()
  12338. "Show the body directly following this heading.
  12339. Show the heading too, if it is currently invisible."
  12340. (interactive)
  12341. (save-excursion
  12342. (org-back-to-heading t)
  12343. (outline-flag-region
  12344. (1- (point))
  12345. (save-excursion
  12346. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12347. (or (match-beginning 1) (point-max)))
  12348. (if org-noutline-p nil ?\n))))
  12349. (defun org-make-options-regexp (kwds)
  12350. "Make a regular expression for keyword lines."
  12351. (concat
  12352. (if org-noutline-p "^" "[\n\r]")
  12353. "#?[ \t]*\\+\\("
  12354. (mapconcat 'regexp-quote kwds "\\|")
  12355. "\\):[ \t]*"
  12356. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  12357. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12358. (eval-after-load "bookmark"
  12359. '(if (boundp 'bookmark-after-jump-hook)
  12360. ;; We can use the hook
  12361. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12362. ;; Hook not available, use advice
  12363. (defadvice bookmark-jump (after org-make-visible activate)
  12364. "Make the position visible."
  12365. (org-bookmark-jump-unhide))))
  12366. (defun org-bookmark-jump-unhide ()
  12367. "Unhide the current position, to show the bookmark location."
  12368. (and (eq major-mode 'org-mode)
  12369. (or (org-invisible-p)
  12370. (save-excursion (goto-char (max (point-min) (1- (point))))
  12371. (org-invisible-p)))
  12372. (org-show-hierarchy-above)))
  12373. ;;; Finish up
  12374. (provide 'org)
  12375. (run-hooks 'org-load-hook)
  12376. ;; Experimental code
  12377. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12378. ;;; org.el ends here