org.el 614 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722
  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.46
  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 NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://staff.science.uva.nl/~dominik/Tools/org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; Recent changes
  61. ;; --------------
  62. ;; Version 4.46
  63. ;; - Custom commands may produce an agenda which contains several blocks,
  64. ;; each block created by a different agenda command.
  65. ;; - Agenda commands can be restricted to the current file, region, subtree.
  66. ;; - The timeline command must now be called through the agenda
  67. ;; dispatcher (C-c a L). `C-c C-r' no longer works.
  68. ;; - Agenda items can be sorted by tag. The *last* tag is used for this.
  69. ;; - The prefix and the sorting strategy for agenda items can depend
  70. ;; upon the agenda type.
  71. ;; - The handling of `mailto:' links can be customized, see the new
  72. ;; variable `org-link-mailto-program'.
  73. ;; - `mailto' links can specify a subject after a double colon,
  74. ;; like [[mailto:carsten@orgmode.org::Org-mode is buggy]].
  75. ;; - In the #+STARTUP line, M-TAB completes valid keywords.
  76. ;; - In the #+TAGS: line, M-TAB after ":" inserts all currently used tags.
  77. ;; - Again full Emacs 21 support: Checkboxes and publishing are fixed.
  78. ;; - More minor bug fixes.
  79. ;;
  80. ;; Version 4.45
  81. ;; - Checkbox lists can show statistics about checked items.
  82. ;; - C-TAB will cycle the visibility of archived subtrees.
  83. ;;; - Documentation about checkboxes has been moved to chapter 5.
  84. ;; - Bux fixes.
  85. ;;
  86. ;; Version 4.44
  87. ;; - Clock table can be done for a limited time interval.
  88. ;; - Obsolete support for the old outline mode has been removed.
  89. ;; - Bug fixes and code cleaning.
  90. ;;
  91. ;; Version 4.43
  92. ;; - Bug fixes
  93. ;; - `s' key in the agenda saves all org-mode buffers.
  94. ;;
  95. ;; Version 4.41
  96. ;; - Shift-curser keys can modify inactive time stamps (inactive time
  97. ;; stamps are the ones in [...] brackets.
  98. ;; - Toggle all checkboxes in a region/below a headline.
  99. ;; - Bug fixes.
  100. ;;
  101. ;;; Code:
  102. (eval-when-compile
  103. (require 'cl)
  104. (require 'calendar))
  105. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  106. ;; the file noutline.el being loaded.
  107. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  108. ;; We require noutline, which might be provided in outline.el
  109. (require 'outline) (require 'noutline)
  110. ;; Other stuff we need.
  111. (require 'time-date)
  112. (require 'easymenu)
  113. ;;; Customization variables
  114. (defvar org-version "4.46"
  115. "The version number of the file org.el.")
  116. (defun org-version ()
  117. (interactive)
  118. (message "Org-mode version %s" org-version))
  119. ;; Compatibility constants
  120. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  121. (defconst org-format-transports-properties-p
  122. (let ((x "a"))
  123. (add-text-properties 0 1 '(test t) x)
  124. (get-text-property 0 'test (format "%s" x)))
  125. "Does format transport text properties?")
  126. (defgroup org nil
  127. "Outline-based notes management and organizer."
  128. :tag "Org"
  129. :group 'outlines
  130. :group 'hypermedia
  131. :group 'calendar)
  132. (defgroup org-startup nil
  133. "Options concerning startup of Org-mode."
  134. :tag "Org Startup"
  135. :group 'org)
  136. (defcustom org-startup-folded t
  137. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  138. This can also be configured on a per-file basis by adding one of
  139. the following lines anywhere in the buffer:
  140. #+STARTUP: fold
  141. #+STARTUP: nofold
  142. #+STARTUP: content"
  143. :group 'org-startup
  144. :type '(choice
  145. (const :tag "nofold: show all" nil)
  146. (const :tag "fold: overview" t)
  147. (const :tag "content: all headlines" content)))
  148. (defcustom org-startup-truncated t
  149. "Non-nil means, entering Org-mode will set `truncate-lines'.
  150. This is useful since some lines containing links can be very long and
  151. uninteresting. Also tables look terrible when wrapped."
  152. :group 'org-startup
  153. :type 'boolean)
  154. (defcustom org-startup-align-all-tables nil
  155. "Non-nil means, align all tables when visiting a file.
  156. This is useful when the column width in tables is forced with <N> cookies
  157. in table fields. Such tables will look correct only after the first re-align.
  158. This can also be configured on a per-file basis by adding one of
  159. the following lines anywhere in the buffer:
  160. #+STARTUP: align
  161. #+STARTUP: noalign"
  162. :group 'org-startup
  163. :type 'boolean)
  164. (defcustom org-startup-with-deadline-check nil
  165. "Non-nil means, entering Org-mode will run the deadline check.
  166. This means, if you start editing an org file, you will get an
  167. immediate reminder of any due deadlines.
  168. This can also be configured on a per-file basis by adding one of
  169. the following lines anywhere in the buffer:
  170. #+STARTUP: dlcheck
  171. #+STARTUP: nodlcheck"
  172. :group 'org-startup
  173. :type 'boolean)
  174. (defcustom org-insert-mode-line-in-empty-file nil
  175. "Non-nil means insert the first line setting Org-mode in empty files.
  176. When the function `org-mode' is called interactively in an empty file, this
  177. normally means that the file name does not automatically trigger Org-mode.
  178. To ensure that the file will always be in Org-mode in the future, a
  179. line enforcing Org-mode will be inserted into the buffer, if this option
  180. has been set."
  181. :group 'org-startup
  182. :type 'boolean)
  183. (defcustom org-CUA-compatible nil
  184. "Non-nil means use alternative key bindings for S-<cursor movement>.
  185. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  186. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  187. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  188. alternative bindings. Setting this variable to t will replace the following
  189. keys both in Org-mode and in the Org-agenda buffer.
  190. S-RET -> C-S-RET
  191. S-up -> M-p
  192. S-down -> M-n
  193. S-left -> M--
  194. S-right -> M-+
  195. If you do not like the alternative keys, take a look at the variable
  196. `org-disputed-keys'.
  197. This option is only relevant at load-time of Org-mode. Changing it requires
  198. a restart of Emacs to become effective."
  199. :group 'org-startup
  200. :type 'boolean)
  201. (defvar org-disputed-keys
  202. '((S-up [(shift up)] [(meta ?p)])
  203. (S-down [(shift down)] [(meta ?n)])
  204. (S-left [(shift left)] [(meta ?-)])
  205. (S-right [(shift right)] [(meta ?+)])
  206. (S-return [(shift return)] [(control shift return)]))
  207. "Keys for which Org-mode and other modes compete.
  208. This is an alist, cars are symbols for lookup, 1st element is the default key,
  209. second element will be used when `org-CUA-compatible' is t.")
  210. (defun org-key (key)
  211. "Select a key according to `org-CUA-compatible'."
  212. (nth (if org-CUA-compatible 2 1)
  213. (or (assq key org-disputed-keys)
  214. (error "Invalid Key %s in `org-key'" key))))
  215. (defcustom org-ellipsis nil
  216. "The ellipsis to use in the Org-mode outline.
  217. When nil, just use the standard three dots. When a string, use that instead,
  218. and just in Org-mode (which will then use its own display table).
  219. Changing this requires executing `M-x org-mode' in a buffer to become
  220. effective."
  221. :group 'org-startup
  222. :type '(choice (const :tag "Default" nil)
  223. (string :tag "String" :value "...#")))
  224. (defvar org-display-table nil
  225. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  226. (defgroup org-keywords nil
  227. "Keywords in Org-mode."
  228. :tag "Org Keywords"
  229. :group 'org)
  230. (defcustom org-deadline-string "DEADLINE:"
  231. "String to mark deadline entries.
  232. A deadline is this string, followed by a time stamp. Should be a word,
  233. terminated by a colon. You can insert a schedule keyword and
  234. a timestamp with \\[org-deadline].
  235. Changes become only effective after restarting Emacs."
  236. :group 'org-keywords
  237. :type 'string)
  238. (defcustom org-scheduled-string "SCHEDULED:"
  239. "String to mark scheduled TODO entries.
  240. A schedule is this string, followed by a time stamp. Should be a word,
  241. terminated by a colon. You can insert a schedule keyword and
  242. a timestamp with \\[org-schedule].
  243. Changes become only effective after restarting Emacs."
  244. :group 'org-keywords
  245. :type 'string)
  246. (defcustom org-closed-string "CLOSED:"
  247. "String used as the prefix for timestamps logging closing a TODO entry."
  248. :group 'org-keywords
  249. :type 'string)
  250. (defcustom org-clock-string "CLOCK:"
  251. "String used as prefix for timestamps clocking work hours on an item."
  252. :group 'org-keywords
  253. :type 'string)
  254. (defcustom org-comment-string "COMMENT"
  255. "Entries starting with this keyword will never be exported.
  256. An entry can be toggled between COMMENT and normal with
  257. \\[org-toggle-comment].
  258. Changes become only effective after restarting Emacs."
  259. :group 'org-keywords
  260. :type 'string)
  261. (defcustom org-quote-string "QUOTE"
  262. "Entries starting with this keyword will be exported in fixed-width font.
  263. Quoting applies only to the text in the entry following the headline, and does
  264. not extend beyond the next headline, even if that is lower level.
  265. An entry can be toggled between QUOTE and normal with
  266. \\[org-toggle-fixed-width-section]."
  267. :group 'org-keywords
  268. :type 'string)
  269. (defgroup org-structure nil
  270. "Options concerning the general structure of Org-mode files."
  271. :tag "Org Structure"
  272. :group 'org)
  273. (defgroup org-cycle nil
  274. "Options concerning visibility cycling in Org-mode."
  275. :tag "Org Cycle"
  276. :group 'org-structure)
  277. (defcustom org-cycle-global-at-bob t
  278. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  279. This makes it possible to do global cycling without having to use S-TAB or
  280. C-u TAB. For this special case to work, the first line of the buffer
  281. must not be a headline - it may be empty ot some other text. When used in
  282. this way, `org-cycle-hook' is disables temporarily, to make sure the
  283. cursor stays at the beginning of the buffer.
  284. When this option is nil, don't do anything special at the beginning
  285. of the buffer."
  286. :group 'org-cycle
  287. :type 'boolean)
  288. (defcustom org-cycle-emulate-tab t
  289. "Where should `org-cycle' emulate TAB.
  290. nil Never
  291. white Only in completely white lines
  292. whitestart Only at the beginning of lines, before the first non-white char.
  293. t Everywhere except in headlines
  294. If TAB is used in a place where it does not emulate TAB, the current subtree
  295. visibility is cycled."
  296. :group 'org-cycle
  297. :type '(choice (const :tag "Never" nil)
  298. (const :tag "Only in completely white lines" white)
  299. (const :tag "Before first char in a line" whitestart)
  300. (const :tag "Everywhere except in headlines" t)
  301. ))
  302. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  303. org-optimize-window-after-visibility-change)
  304. "Hook that is run after `org-cycle' has changed the buffer visibility.
  305. The function(s) in this hook must accept a single argument which indicates
  306. the new state that was set by the most recent `org-cycle' command. The
  307. argument is a symbol. After a global state change, it can have the values
  308. `overview', `content', or `all'. After a local state change, it can have
  309. the values `folded', `children', or `subtree'."
  310. :group 'org-cycle
  311. :type 'hook)
  312. (defgroup org-edit-structure nil
  313. "Options concerning structure editing in Org-mode."
  314. :tag "Org Edit Structure"
  315. :group 'org-structure)
  316. (defcustom org-odd-levels-only nil
  317. "Non-nil means, skip even levels and only use odd levels for the outline.
  318. This has the effect that two stars are being added/taken away in
  319. promotion/demotion commands. It also influences how levels are
  320. handled by the exporters.
  321. Changing it requires restart of `font-lock-mode' to become effective
  322. for fontification also in regions already fontified.
  323. You may also set this on a per-file basis by adding one of the following
  324. lines to the buffer:
  325. #+STARTUP: odd
  326. #+STARTUP: oddeven"
  327. :group 'org-edit-structure
  328. :group 'org-font-lock
  329. :type 'boolean)
  330. (defcustom org-adapt-indentation t
  331. "Non-nil means, adapt indentation when promoting and demoting.
  332. When this is set and the *entire* text in an entry is indented, the
  333. indentation is increased by one space in a demotion command, and
  334. decreased by one in a promotion command. If any line in the entry
  335. body starts at column 0, indentation is not changed at all."
  336. :group 'org-edit-structure
  337. :type 'boolean)
  338. (defcustom org-insert-heading-hook nil
  339. "Hook being run after inserting a new heading."
  340. :group 'org-edit-structure
  341. :type 'boolean)
  342. (defcustom org-enable-fixed-width-editor t
  343. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  344. This currently only means, they are never auto-wrapped.
  345. When nil, such lines will be treated like ordinary lines.
  346. See also the QUOTE keyword."
  347. :group 'org-edit-structure
  348. :type 'boolean)
  349. (defgroup org-sparse-trees nil
  350. "Options concerning sparse trees in Org-mode."
  351. :tag "Org Sparse Trees"
  352. :group 'org-structure)
  353. (defcustom org-highlight-sparse-tree-matches t
  354. "Non-nil means, highlight all matches that define a sparse tree.
  355. The highlights will automatically disappear the next time the buffer is
  356. changed by an edit command."
  357. :group 'org-sparse-trees
  358. :type 'boolean)
  359. (defcustom org-show-hierarchy-above t
  360. "Non-nil means, show full hierarchy when showing a spot in the tree.
  361. Turning this off makes sparse trees more compact, but also less clear."
  362. :group 'org-sparse-trees
  363. :type 'boolean)
  364. (defcustom org-show-following-heading t
  365. "Non-nil means, show heading following match in `org-occur'.
  366. When doing an `org-occur' it is useful to show the headline which
  367. follows the match, even if they do not match the regexp. This makes it
  368. easier to edit directly inside the sparse tree. However, if you use
  369. `org-occur' mainly as an overview, the following headlines are
  370. unnecessary clutter."
  371. :group 'org-sparse-trees
  372. :type 'boolean)
  373. (defcustom org-occur-hook '(org-first-headline-recenter)
  374. "Hook that is run after `org-occur' has constructed a sparse tree.
  375. This can be used to recenter the window to show as much of the structure
  376. as possible."
  377. :group 'org-sparse-trees
  378. :type 'hook)
  379. (defgroup org-plain-lists nil
  380. "Options concerning plain lists in Org-mode."
  381. :tag "Org Plain lists"
  382. :group 'org-structure)
  383. (defcustom org-cycle-include-plain-lists t
  384. "Non-nil means, include plain lists into visibility cycling.
  385. This means that during cycling, plain list items will *temporarily* be
  386. interpreted as outline headlines with a level given by 1000+i where i is the
  387. indentation of the bullet. In all other operations, plain list items are
  388. not seen as headlines. For example, you cannot assign a TODO keyword to
  389. such an item."
  390. :group 'org-plain-lists
  391. :type 'boolean)
  392. (defcustom org-plain-list-ordered-item-terminator t
  393. "The character that makes a line with leading number an ordered list item.
  394. Valid values are ?. and ?\). To get both terminators, use t. While
  395. ?. may look nicer, it creates the danger that a line with leading
  396. number may be incorrectly interpreted as an item. ?\) therefore is
  397. the safe choice."
  398. :group 'org-plain-lists
  399. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  400. (const :tag "paren like in \"2)\"" ?\))
  401. (const :tab "both" t)))
  402. (defcustom org-auto-renumber-ordered-lists t
  403. "Non-nil means, automatically renumber ordered plain lists.
  404. Renumbering happens when the sequence have been changed with
  405. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  406. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  407. :group 'org-plain-lists
  408. :type 'boolean)
  409. (defcustom org-provide-checkbox-statistics t
  410. "Non-nil means, update checkbox statistics after insert and toggle.
  411. When this is set, checkbox statistics is updated each time you either insert
  412. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  413. with \\[org-ctrl-c-ctrl-c\\]."
  414. :group 'org
  415. :type 'boolean)
  416. (defgroup org-archive nil
  417. "Options concerning archiving in Org-mode."
  418. :tag "Org Archive"
  419. :group 'org-structure)
  420. (defcustom org-archive-tag "ARCHIVE"
  421. "The tag that marks a subtree as archived.
  422. An archived subtree does not open during visibility cycling, and does
  423. not contribute to the agenda listings."
  424. :group 'org-archive
  425. :group 'org-keywords
  426. :type 'string)
  427. (defcustom org-agenda-skip-archived-trees t
  428. "Non-nil means, the agenda will skip any items located in archived trees.
  429. An archived tree is a tree marked with the tag ARCHIVE."
  430. :group 'org-archive
  431. :group 'org-agenda-display
  432. :type 'boolean)
  433. (defcustom org-cycle-open-archived-trees nil
  434. "Non-nil means, `org-cycle' will open archived trees.
  435. An archived tree is a tree marked with the tag ARCHIVE.
  436. When nil, archived trees will stay folded. You can still open them with
  437. normal outline commands like `show-all', but not with the cycling commands."
  438. :group 'org-archive
  439. :group 'org-cycle
  440. :type 'boolean)
  441. (defcustom org-sparse-tree-open-archived-trees nil
  442. "Non-nil means sparse tree construction shows matches in archived trees.
  443. When nil, matches in these trees are highlighted, but the trees are kept in
  444. collapsed state."
  445. :group 'org-archive
  446. :group 'org-sparse-trees
  447. :type 'boolean)
  448. (defcustom org-archive-location "%s_archive::"
  449. "The location where subtrees should be archived.
  450. This string consists of two parts, separated by a double-colon.
  451. The first part is a file name - when omitted, archiving happens in the same
  452. file. %s will be replaced by the current file name (without directory part).
  453. Archiving to a different file is useful to keep archived entries from
  454. contributing to the Org-mode Agenda.
  455. The part after the double colon is a headline. The archived entries will be
  456. filed under that headline. When omitted, the subtrees are simply filed away
  457. at the end of the file, as top-level entries.
  458. Here are a few examples:
  459. \"%s_archive::\"
  460. If the current file is Projects.org, archive in file
  461. Projects.org_archive, as top-level trees. This is the default.
  462. \"::* Archived Tasks\"
  463. Archive in the current file, under the top-level headline
  464. \"* Archived Tasks\".
  465. \"~/org/archive.org::\"
  466. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  467. \"basement::** Finished Tasks\"
  468. Archive in file ./basement (relative path), as level 3 trees
  469. below the level 2 heading \"** Finished Tasks\".
  470. You may set this option on a per-file basis by adding to the buffer a
  471. line like
  472. #+ARCHIVE: basement::** Finished Tasks"
  473. :group 'org-archive
  474. :type 'string)
  475. (defcustom org-archive-mark-done t
  476. "Non-nil means, mark entries as DONE when they are moved to the archive file."
  477. :group 'org-archive
  478. :type 'boolean)
  479. (defcustom org-archive-stamp-time t
  480. "Non-nil means, add a time stamp to entries moved to an archive file.
  481. The time stamp will be added directly after the TODO state keyword in the
  482. first line, so it is probably best to use this in combinations with
  483. `org-archive-mark-done'."
  484. :group 'org-archive
  485. :type 'boolean)
  486. (defgroup org-table nil
  487. "Options concerning tables in Org-mode."
  488. :tag "Org Table"
  489. :group 'org)
  490. (defcustom org-enable-table-editor 'optimized
  491. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  492. When nil, such lines will be treated like ordinary lines.
  493. When equal to the symbol `optimized', the table editor will be optimized to
  494. do the following:
  495. - Use automatic overwrite mode in front of whitespace in table fields.
  496. This make the structure of the table stay in tact as long as the edited
  497. field does not exceed the column width.
  498. - Minimize the number of realigns. Normally, the table is aligned each time
  499. TAB or RET are pressed to move to another field. With optimization this
  500. happens only if changes to a field might have changed the column width.
  501. Optimization requires replacing the functions `self-insert-command',
  502. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  503. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  504. very good at guessing when a re-align will be necessary, but you can always
  505. force one with \\[org-ctrl-c-ctrl-c].
  506. If you would like to use the optimized version in Org-mode, but the
  507. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  508. This variable can be used to turn on and off the table editor during a session,
  509. but in order to toggle optimization, a restart is required.
  510. See also the variable `org-table-auto-blank-field'."
  511. :group 'org-table
  512. :type '(choice
  513. (const :tag "off" nil)
  514. (const :tag "on" t)
  515. (const :tag "on, optimized" optimized)))
  516. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  517. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  518. In the optimized version, the table editor takes over all simple keys that
  519. normally just insert a character. In tables, the characters are inserted
  520. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  521. for empty fields). Outside tables, the correct binding of the keys is
  522. restored.
  523. The default for this option is t if the optimized version is also used in
  524. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  525. this variable requires a restart of Emacs to become effective."
  526. :group 'org-table
  527. :type 'boolean)
  528. (defgroup org-table-settings nil
  529. "Settings for tables in Org-mode."
  530. :tag "Org Table Settings"
  531. :group 'org-table)
  532. (defcustom org-table-default-size "5x2"
  533. "The default size for newly created tables, Columns x Rows."
  534. :group 'org-table-settings
  535. :type 'string)
  536. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  537. "Regular expression for recognizing numbers in table columns.
  538. If a table column contains mostly numbers, it will be aligned to the
  539. right. If not, it will be aligned to the left.
  540. The default value of this option is a regular expression which allows
  541. anything which looks remotely like a number as used in scientific
  542. context. For example, all of the following will be considered a
  543. number:
  544. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  545. Other options offered by the customize interface are more restrictive."
  546. :group 'org-table-settings
  547. :type '(choice
  548. (const :tag "Positive Integers"
  549. "^[0-9]+$")
  550. (const :tag "Integers"
  551. "^[-+]?[0-9]+$")
  552. (const :tag "Floating Point Numbers"
  553. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  554. (const :tag "Floating Point Number or Integer"
  555. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  556. (const :tag "Exponential, Floating point, Integer"
  557. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  558. (const :tag "Very General Number-Like"
  559. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  560. (string :tag "Regexp:")))
  561. (defcustom org-table-number-fraction 0.5
  562. "Fraction of numbers in a column required to make the column align right.
  563. In a column all non-white fields are considered. If at least this
  564. fraction of fields is matched by `org-table-number-fraction',
  565. alignment to the right border applies."
  566. :group 'org-table-settings
  567. :type 'number)
  568. (defgroup org-table-editing nil
  569. "Bahavior of tables during editing in Org-mode."
  570. :tag "Org Table Editing"
  571. :group 'org-table)
  572. (defcustom org-table-automatic-realign t
  573. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  574. When nil, aligning is only done with \\[org-table-align], or after column
  575. removal/insertion."
  576. :group 'org-table-editing
  577. :type 'boolean)
  578. (defcustom org-table-limit-column-width t ;kw
  579. "Non-nil means, allow to limit the width of table columns with <N> fields."
  580. :group 'org-table-editing
  581. :type 'boolean)
  582. (defcustom org-table-auto-blank-field t
  583. "Non-nil means, automatically blank table field when starting to type into it.
  584. This only happens when typing immediately after a field motion
  585. command (TAB, S-TAB or RET).
  586. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  587. :group 'org-table-editing
  588. :type 'boolean)
  589. (defcustom org-table-tab-jumps-over-hlines t
  590. "Non-nil means, tab in the last column of a table with jump over a hline.
  591. If a horizontal separator line is following the current line,
  592. `org-table-next-field' can either create a new row before that line, or jump
  593. over the line. When this option is nil, a new line will be created before
  594. this line."
  595. :group 'org-table-editing
  596. :type 'boolean)
  597. (defcustom org-table-tab-recognizes-table.el t
  598. "Non-nil means, TAB will automatically notice a table.el table.
  599. When it sees such a table, it moves point into it and - if necessary -
  600. calls `table-recognize-table'."
  601. :group 'org-table-editing
  602. :type 'boolean)
  603. (defgroup org-table-calculation nil
  604. "Options concerning tables in Org-mode."
  605. :tag "Org Table Calculation"
  606. :group 'org-table)
  607. (defcustom org-table-copy-increment t
  608. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  609. :group 'org-table-calculation
  610. :type 'boolean)
  611. (defcustom org-calc-default-modes
  612. '(calc-internal-prec 12
  613. calc-float-format (float 5)
  614. calc-angle-mode deg
  615. calc-prefer-frac nil
  616. calc-symbolic-mode nil
  617. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  618. calc-display-working-message t
  619. )
  620. "List with Calc mode settings for use in calc-eval for table formulas.
  621. The list must contain alternating symbols (Calc modes variables and values).
  622. Don't remove any of the default settings, just change the values. Org-mode
  623. relies on the variables to be present in the list."
  624. :group 'org-table-calculation
  625. :type 'plist)
  626. (defcustom org-table-formula-evaluate-inline t
  627. "Non-nil means, TAB and RET evaluate a formula in current table field.
  628. If the current field starts with an equal sign, it is assumed to be a formula
  629. which should be evaluated as described in the manual and in the documentation
  630. string of the command `org-table-eval-formula'. This feature requires the
  631. Emacs calc package.
  632. When this variable is nil, formula calculation is only available through
  633. the command \\[org-table-eval-formula]."
  634. :group 'org-table-calculation
  635. :type 'boolean)
  636. (defcustom org-table-formula-use-constants t
  637. "Non-nil means, interpret constants in formulas in tables.
  638. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  639. by the value given in `org-table-formula-constants', or by a value obtained
  640. from the `constants.el' package."
  641. :group 'org-table-calculation
  642. :type 'boolean)
  643. (defcustom org-table-formula-constants nil
  644. "Alist with constant names and values, for use in table formulas.
  645. The car of each element is a name of a constant, without the `$' before it.
  646. The cdr is the value as a string. For example, if you'd like to use the
  647. speed of light in a formula, you would configure
  648. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  649. and then use it in an equation like `$1*$c'."
  650. :group 'org-table-calculation
  651. :type '(repeat
  652. (cons (string :tag "name")
  653. (string :tag "value"))))
  654. (defcustom org-table-formula-numbers-only nil
  655. "Non-nil means, calculate only with numbers in table formulas.
  656. Then all input fields will be converted to a number, and the result
  657. must also be a number. When nil, calc's full potential is available
  658. in table calculations, including symbolics etc."
  659. :group 'org-table-calculation
  660. :type 'boolean)
  661. (defcustom org-table-allow-automatic-line-recalculation t
  662. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  663. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  664. :group 'org-table-calculation
  665. :type 'boolean)
  666. (defgroup org-link nil
  667. "Options concerning links in Org-mode."
  668. :tag "Org Link"
  669. :group 'org)
  670. (defcustom org-descriptive-links t
  671. "Non-nil means, hide link part and only show description of bracket links.
  672. Bracket links are like [[link][descritpion]]. This variable sets the initial
  673. state in new org-mode buffers. The setting can then be toggled on a
  674. per-buffer basis from the Org->Hyperlinks menu."
  675. :group 'org-link
  676. :type 'boolean)
  677. (defcustom org-link-style 'bracket
  678. "The style of links to be inserted with \\[org-insert-link].
  679. Possible values are:
  680. bracket [[link][description]]. This is recommended
  681. plain Description \\n link. The old way, no longer recommended."
  682. :group 'org-link
  683. :type '(choice
  684. (const :tag "Bracket (recommended)" bracket)
  685. (const :tag "Plain (no longer recommended)" plain)))
  686. (defcustom org-link-format "%s"
  687. "Default format for external, URL-like linkes in the buffer.
  688. This is a format string for printf, %s will be replaced by the link text.
  689. The recommended value is just \"%s\", since links will be protected by
  690. enclosing them in double brackets. If you prefer plain links (see variable
  691. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  692. additional URL: prefix, so the format would be \"<URL:%s>\"."
  693. :group 'org-link
  694. :type '(choice
  695. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  696. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  697. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  698. (string :tag "Other" :value "<%s>")))
  699. (defcustom org-link-file-path-type 'adaptive
  700. "How the path name in file links should be stored.
  701. Valid values are:
  702. relative relative to the current directory, i.e. the directory of the file
  703. into which the link is being inserted.
  704. absolute absolute path, if possible with ~ for home directory.
  705. noabbrev absolute path, no abbreviation of home directory.
  706. adaptive Use relative path for files in the current directory and sub-
  707. directories of it. For other files, use an absolute path."
  708. :group 'org-link
  709. :type '(choice
  710. (const relative)
  711. (const absolute)
  712. (const noabbrev)
  713. (const adaptive)))
  714. (defcustom org-activate-links '(bracket angle plain radio tag date)
  715. "Types of links that should be activated in Org-mode files.
  716. This is a list of symbols, each leading to the activation of a certain link
  717. type. In principle, it does not hurt to turn on most link types - there may
  718. be a small gain when turning off unused link types. The types are:
  719. bracket The recommended [[link][description]] or [[link]] links with hiding.
  720. angular Links in angular brackes that may contain whitespace like
  721. <bbdb:Carsten Dominik>.
  722. plain Plain links in normal text, no whitespace, like http://google.com.
  723. radio Text that is matched by a radio target, see manual for details.
  724. tag Tag settings in a headline (link to tag search).
  725. date Time stamps (link to calendar).
  726. camel CamelCase words defining text searches.
  727. Changing this variable requires a restart of Emacs to become effective."
  728. :group 'org-link
  729. :type '(set (const :tag "Double bracket links (new style)" bracket)
  730. (const :tag "Angular bracket links (old style)" angular)
  731. (const :tag "plain text links" plain)
  732. (const :tag "Radio target matches" radio)
  733. (const :tag "Tags" tag)
  734. (const :tag "Timestamps" date)
  735. (const :tag "CamelCase words" camel)))
  736. (defgroup org-link-store nil
  737. "Options concerning storing links in Org-mode"
  738. :tag "Org Store Link"
  739. :group 'org-link)
  740. (defcustom org-context-in-file-links t
  741. "Non-nil means, file links from `org-store-link' contain context.
  742. A search string will be added to the file name with :: as separator and
  743. used to find the context when the link is activated by the command
  744. `org-open-at-point'.
  745. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  746. negates this setting for the duration of the command."
  747. :group 'org-link-store
  748. :type 'boolean)
  749. (defcustom org-file-link-context-use-camel-case nil
  750. "Non-nil means, use CamelCase to store a search context in a file link.
  751. When nil, the search string simply consists of the words of the string.
  752. CamelCase is deprecated, and support for it may be dropped in the future."
  753. :group 'org-link-store
  754. :type 'boolean)
  755. (defcustom org-keep-stored-link-after-insertion nil
  756. "Non-nil means, keep link in list for entire session.
  757. The command `org-store-link' adds a link pointing to the current
  758. location to an internal list. These links accumulate during a session.
  759. The command `org-insert-link' can be used to insert links into any
  760. Org-mode file (offering completion for all stored links). When this
  761. option is nil, every link which has been inserted once using \\[org-insert-link]
  762. will be removed from the list, to make completing the unused links
  763. more efficient."
  764. :group 'org-link-store
  765. :type 'boolean)
  766. (defcustom org-usenet-links-prefer-google nil
  767. "Non-nil means, `org-store-link' will create web links to Google groups.
  768. When nil, Gnus will be used for such links.
  769. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  770. negates this setting for the duration of the command."
  771. :group 'org-link-store
  772. :type 'boolean)
  773. (defgroup org-link-follow nil
  774. "Options concerning following links in Org-mode"
  775. :tag "Org Follow Link"
  776. :group 'org-link)
  777. (defcustom org-tab-follows-link nil
  778. "Non-nil means, on links TAB will follow the link.
  779. Needs to be set before org.el is loaded."
  780. :group 'org-link-follow
  781. :type 'boolean)
  782. (defcustom org-return-follows-link nil
  783. "Non-nil means, on links RET will follow the link.
  784. Needs to be set before org.el is loaded."
  785. :group 'org-link-follow
  786. :type 'boolean)
  787. (defcustom org-mouse-1-follows-link t
  788. "Non-nil means, mouse-1 on a link will follow the link.
  789. A longer mouse click will still set point. Does not wortk on XEmacs.
  790. Needs to be set before org.el is loaded."
  791. :group 'org-link-follow
  792. :type 'boolean)
  793. (defcustom org-mark-ring-length 4
  794. "Number of different positions to be recorded in the ring
  795. Changing this requires a restart of Emacs to work correctly."
  796. :group 'org-link-follow
  797. :type 'interger)
  798. (defcustom org-link-frame-setup
  799. '((vm . vm-visit-folder-other-frame)
  800. (gnus . gnus-other-frame)
  801. (file . find-file-other-window))
  802. "Setup the frame configuration for following links.
  803. When following a link with Emacs, it may often be useful to display
  804. this link in another window or frame. This variable can be used to
  805. set this up for the different types of links.
  806. For VM, use any of
  807. `vm-visit-folder'
  808. `vm-visit-folder-other-frame'
  809. For Gnus, use any of
  810. `gnus'
  811. `gnus-other-frame'
  812. For FILE, use any of
  813. `find-file'
  814. `find-file-other-window'
  815. `find-file-other-frame'
  816. For the calendar, use the variable `calendar-setup'.
  817. For BBDB, it is currently only possible to display the matches in
  818. another window."
  819. :group 'org-link-follow
  820. :type '(list
  821. (cons (const vm)
  822. (choice
  823. (const vm-visit-folder)
  824. (const vm-visit-folder-other-window)
  825. (const vm-visit-folder-other-frame)))
  826. (cons (const gnus)
  827. (choice
  828. (const gnus)
  829. (const gnus-other-frame)))
  830. (cons (const file)
  831. (choice
  832. (const find-file)
  833. (const find-file-other-window)
  834. (const find-file-other-frame)))))
  835. (defcustom org-open-non-existing-files nil
  836. "Non-nil means, `org-open-file' will open non-existing file.
  837. When nil, an error will be generated."
  838. :group 'org-link-follow
  839. :type 'boolean)
  840. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  841. "Function and arguments to call for following mailto links.
  842. This is a list with the first element being a lisp function, and the
  843. remaining elements being arguments to the function. In string arguments,
  844. %a will be replaced by the address, and %s will be replaced by the subject
  845. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  846. :group 'org-link-follow
  847. :type '(choice
  848. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  849. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  850. (const :tag "message-mail" (message-mail "%a" "%s"))
  851. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  852. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  853. "Non-nil means, ask for confirmation before executing shell links.
  854. Shell links can be dangerous, just thing about a link
  855. [[shell:rm -rf ~/*][Google Search]]
  856. This link would show up in your Org-mode document as \"Google Search\"
  857. but really it would remove your entire home directory.
  858. Therefore I *definitely* advise against setting this variable to nil.
  859. Just change it to `y-or-n-p' of you want to confirm with a single key press
  860. rather than having to type \"yes\"."
  861. :group 'org-link-follow
  862. :type '(choice
  863. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  864. (const :tag "with y-or-n (faster)" y-or-n-p)
  865. (const :tag "no confirmation (dangerous)" nil)))
  866. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  867. "Non-nil means, ask for confirmation before executing elisp links.
  868. Elisp links can be dangerous, just thing about a link
  869. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  870. This link would show up in your Org-mode document as \"Google Search\"
  871. but really it would remove your entire home directory.
  872. Therefore I *definitely* advise against setting this variable to nil.
  873. Just change it to `y-or-n-p' of you want to confirm with a single key press
  874. rather than having to type \"yes\"."
  875. :group 'org-link-follow
  876. :type '(choice
  877. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  878. (const :tag "with y-or-n (faster)" y-or-n-p)
  879. (const :tag "no confirmation (dangerous)" nil)))
  880. (defconst org-file-apps-defaults-gnu
  881. '((remote . emacs)
  882. (t . mailcap))
  883. "Default file applications on a UNIX or GNU/Linux system.
  884. See `org-file-apps'.")
  885. (defconst org-file-apps-defaults-macosx
  886. '((remote . emacs)
  887. (t . "open %s")
  888. ("ps" . "gv %s")
  889. ("ps.gz" . "gv %s")
  890. ("eps" . "gv %s")
  891. ("eps.gz" . "gv %s")
  892. ("dvi" . "xdvi %s")
  893. ("fig" . "xfig %s"))
  894. "Default file applications on a MacOS X system.
  895. The system \"open\" is known as a default, but we use X11 applications
  896. for some files for which the OS does not have a good default.
  897. See `org-file-apps'.")
  898. (defconst org-file-apps-defaults-windowsnt
  899. (list
  900. '(remote . emacs)
  901. (cons t
  902. (list (if (featurep 'xemacs)
  903. 'mswindows-shell-execute
  904. 'w32-shell-execute)
  905. "open" 'file)))
  906. "Default file applications on a Windows NT system.
  907. The system \"open\" is used for most files.
  908. See `org-file-apps'.")
  909. (defcustom org-file-apps
  910. '(
  911. ("txt" . emacs)
  912. ("tex" . emacs)
  913. ("ltx" . emacs)
  914. ("org" . emacs)
  915. ("el" . emacs)
  916. ("bib" . emacs)
  917. )
  918. "External applications for opening `file:path' items in a document.
  919. Org-mode uses system defaults for different file types, but
  920. you can use this variable to set the application for a given file
  921. extension. The entries in this list are cons cells where the car identifies
  922. files and the cdr the corresponding command. Possible values for the
  923. file identifier are
  924. \"ext\" A string identifying an extension
  925. `directory' Matches a directory
  926. `remote' Matches a remote file, accessible through tramp or efs.
  927. Remote files most likely should be visited through emacs
  928. because external applications cannot handle such paths.
  929. t Default for all remaining files
  930. Possible values for the command are:
  931. `emacs' The file will be visited by the current Emacs process.
  932. `default' Use the default application for this file type.
  933. string A command to be executed by a shell; %s will be replaced
  934. by the path to the file.
  935. sexp A Lisp form which will be evaluated. The file path will
  936. be available in the Lisp variable `file'.
  937. For more examples, see the system specific constants
  938. `org-file-apps-defaults-macosx'
  939. `org-file-apps-defaults-windowsnt'
  940. `org-file-apps-defaults-gnu'."
  941. :group 'org-link-follow
  942. :type '(repeat
  943. (cons (choice :value ""
  944. (string :tag "Extension")
  945. (const :tag "Default for unrecognized files" t)
  946. (const :tag "Remote file" remote)
  947. (const :tag "Links to a directory" directory))
  948. (choice :value ""
  949. (const :tag "Visit with Emacs" emacs)
  950. (const :tag "Use system default" default)
  951. (string :tag "Command")
  952. (sexp :tag "Lisp form")))))
  953. (defcustom org-mhe-search-all-folders nil
  954. "Non-nil means, that the search for the mh-message will be extended to
  955. all folders if the message cannot be found in the folder given in the link.
  956. Searching all folders is very effective with one of the search engines
  957. supported by MH-E, but will be slow with pick."
  958. :group 'org-link-follow
  959. :type 'boolean)
  960. (defgroup org-remember nil
  961. "Options concerning interaction with remember.el."
  962. :tag "Org Remember"
  963. :group 'org)
  964. (defcustom org-directory "~/org"
  965. "Directory with org files.
  966. This directory will be used as default to prompt for org files.
  967. Used by the hooks for remember.el."
  968. :group 'org-remember
  969. :type 'directory)
  970. (defcustom org-default-notes-file "~/.notes"
  971. "Default target for storing notes.
  972. Used by the hooks for remember.el. This can be a string, or nil to mean
  973. the value of `remember-data-file'."
  974. :group 'org-remember
  975. :type '(choice
  976. (const :tag "Default from remember-data-file" nil)
  977. file))
  978. (defcustom org-remember-templates nil
  979. "Templates for the creation of remember buffers.
  980. When nil, just let remember make the buffer.
  981. When not nil, this is a list of 3-element lists. In each entry, the first
  982. element is a character, a unique key to select this template.
  983. The second element is the template. The third element is optional and can
  984. specify a destination file for remember items created with this template.
  985. The default file is given by `org-default-notes-file'.
  986. The template specifies the structure of the remember buffer. It should have
  987. a first line starting with a star, to act as the org-mode headline.
  988. Furthermore, the following %-escapes will be replaced with content:
  989. %t time stamp, date only
  990. %T time stamp with date and time
  991. %u inactive time stamp, date only
  992. %U inactive time stamp with date and time
  993. %n user name
  994. %a annotation, normally the link created with org-store-link
  995. %i initial content, the region when remember is called with C-u.
  996. If %i is indented, the entire inserted text will be indented as well.
  997. %? This will be removed, and the cursor placed at this position."
  998. :group 'org-remember
  999. :type '(repeat :tag "enabled"
  1000. (list :value (?a "\n" nil)
  1001. (character :tag "Selection Key")
  1002. (string :tag "Template")
  1003. (file :tag "Destination file (optional)"))))
  1004. (defcustom org-reverse-note-order nil
  1005. "Non-nil means, store new notes at the beginning of a file or entry.
  1006. When nil, new notes will be filed to the end of a file or entry."
  1007. :group 'org-remember
  1008. :type '(choice
  1009. (const :tag "Reverse always" t)
  1010. (const :tag "Reverse never" nil)
  1011. (repeat :tag "By file name regexp"
  1012. (cons regexp boolean))))
  1013. (defgroup org-todo nil
  1014. "Options concerning TODO items in Org-mode."
  1015. :tag "Org TODO"
  1016. :group 'org)
  1017. (defcustom org-todo-keywords '("TODO" "DONE")
  1018. "List of TODO entry keywords.
  1019. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1020. considered to mean that the entry is \"done\". All the other mean that
  1021. action is required, and will make the entry show up in todo lists, diaries
  1022. etc.
  1023. The command \\[org-todo] cycles an entry through these states, and an
  1024. additional state where no keyword is present. For details about this
  1025. cycling, see also the variable `org-todo-interpretation'
  1026. Changes become only effective after restarting Emacs."
  1027. :group 'org-todo
  1028. :group 'org-keywords
  1029. :type '(repeat (string :tag "Keyword")))
  1030. (defcustom org-todo-interpretation 'sequence
  1031. "Controls how TODO keywords are interpreted.
  1032. This variable is only relevant if `org-todo-keywords' contains more than two
  1033. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1034. When `sequence', \\[org-todo] will always switch to the next state in the
  1035. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1036. to state when executed several times in direct succession. Otherwise, it
  1037. switches directly to DONE from any state.
  1038. See the manual for more information."
  1039. :group 'org-todo
  1040. :group 'org-keywords
  1041. :type '(choice (const sequence)
  1042. (const type)))
  1043. (defcustom org-after-todo-state-change-hook nil
  1044. "Hook which is run after the state of a TODO item was changed.
  1045. The new state (a string with a TODO keyword, or nil) is available in the
  1046. Lisp variable `state'."
  1047. :group 'org-todo
  1048. :type 'hook)
  1049. (defcustom org-log-done nil
  1050. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1051. When the state of an entry is changed from nothing to TODO, remove a previous
  1052. closing date.
  1053. This can also be configured on a per-file basis by adding one of
  1054. the following lines anywhere in the buffer:
  1055. #+STARTUP: logging
  1056. #+STARTUP: nologging"
  1057. :group 'org-todo
  1058. :type 'boolean)
  1059. (defgroup org-priorities nil
  1060. "Priorities in Org-mode."
  1061. :tag "Org Priorities"
  1062. :group 'org-todo)
  1063. (defcustom org-default-priority ?B
  1064. "The default priority of TODO items.
  1065. This is the priority an item get if no explicit priority is given."
  1066. :group 'org-priorities
  1067. :type 'character)
  1068. (defcustom org-lowest-priority ?C
  1069. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1070. :group 'org-priorities
  1071. :type 'character)
  1072. (defgroup org-time nil
  1073. "Options concerning time stamps and deadlines in Org-mode."
  1074. :tag "Org Time"
  1075. :group 'org)
  1076. (defcustom org-insert-labeled-timestamps-at-point nil
  1077. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1078. When nil, these labeled time stamps are forces into the second line of an
  1079. entry, just after the headline. When scheduling from the global TODO list,
  1080. the time stamp will always be forced into the second line."
  1081. :group 'org-time
  1082. :type 'boolean)
  1083. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1084. "Formats for `format-time-string' which are used for time stamps.
  1085. It is not recommended to change this constant.")
  1086. (defcustom org-time-stamp-rounding-minutes 0
  1087. "Number of minutes to round time stamps to upon insertion.
  1088. When zero, insert the time unmodified. Useful rounding numbers
  1089. should be factors of 60, so for example 5, 10, 15.
  1090. When this is not zero, you can still force an exact time-stamp by using
  1091. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1092. :group 'org-time
  1093. :type 'integer)
  1094. (defcustom org-deadline-warning-days 30
  1095. "No. of days before expiration during which a deadline becomes active.
  1096. This variable governs the display in the org file."
  1097. :group 'org-time
  1098. :type 'number)
  1099. (defcustom org-popup-calendar-for-date-prompt t
  1100. "Non-nil means, pop up a calendar when prompting for a date.
  1101. In the calendar, the date can be selected with mouse-1. However, the
  1102. minibuffer will also be active, and you can simply enter the date as well.
  1103. When nil, only the minibuffer will be available."
  1104. :group 'org-time
  1105. :type 'boolean)
  1106. (defcustom org-calendar-follow-timestamp-change t
  1107. "Non-nil means, make the calendar window follow timestamp changes.
  1108. When a timestamp is modified and the calendar window is visible, it will be
  1109. moved to the new date."
  1110. :group 'org-time
  1111. :type 'boolean)
  1112. (defgroup org-tags nil
  1113. "Options concerning tags in Org-mode."
  1114. :tag "Org Tags"
  1115. :group 'org)
  1116. (defcustom org-tag-alist nil
  1117. "List of tags allowed in Org-mode files.
  1118. When this list is nil, Org-mode will base TAG input on what is already in the
  1119. buffer.
  1120. The value of this variable is an alist, the car may be (and should) be a
  1121. character that is used to select that tag through the fast-tag-selection
  1122. interface. See the manual for details."
  1123. :group 'org-tags
  1124. :type '(repeat
  1125. (choice
  1126. (cons (string :tag "Tag name")
  1127. (character :tag "Access char"))
  1128. (const :tag "Start radio group" (:startgroup))
  1129. (const :tag "End radio group" (:endgroup)))))
  1130. (defcustom org-use-fast-tag-selection 'auto
  1131. "Non-nil means, use fast tag selection scheme.
  1132. This is a special interface to select and deselect tags with single keys.
  1133. When nil, fast selection is never used.
  1134. When the symbol `auto', fast selection is used if and only if selection
  1135. characters for tags have been configured, either through the variable
  1136. `org-tag-alist' or through a #+TAGS line in the buffer.
  1137. When t, fast selection is always used and selection keys are assigned
  1138. automatically if necessary."
  1139. :group 'org-tags
  1140. :type '(choice
  1141. (const :tag "Always" t)
  1142. (const :tag "Never" nil)
  1143. (const :tag "When selection characters are configured" 'auto)))
  1144. (defcustom org-tags-column 48
  1145. "The column to which tags should be indented in a headline.
  1146. If this number is positive, it specifies the column. If it is negative,
  1147. it means that the tags should be flushright to that column. For example,
  1148. -79 works well for a normal 80 character screen."
  1149. :group 'org-tags
  1150. :type 'integer)
  1151. (defcustom org-auto-align-tags t
  1152. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1153. These operations change the length of a headline and therefore shift
  1154. the tags around. With this options turned on, after each such operation
  1155. the tags are again aligned to `org-tags-column'."
  1156. :group 'org-tags
  1157. :type 'boolean)
  1158. (defcustom org-use-tag-inheritance t
  1159. "Non-nil means, tags in levels apply also for sublevels.
  1160. When nil, only the tags directly given in a specific line apply there.
  1161. If you turn off this option, you very likely want to turn on the
  1162. companion option `org-tags-match-list-sublevels'."
  1163. :group 'org-tags
  1164. :type 'boolean)
  1165. (defcustom org-tags-match-list-sublevels nil
  1166. "Non-nil means list also sublevels of headlines matching tag search.
  1167. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1168. the sublevels of a headline matching a tag search often also match
  1169. the same search. Listing all of them can create very long lists.
  1170. Setting this variable to nil causes subtrees of a match to be skipped.
  1171. This option is off by default, because inheritance in on. If you turn
  1172. inheritance off, you very likely want to turn this option on.
  1173. As a special case, if the tag search is restricted to TODO items, the
  1174. value of this variable is ignored and sublevels are always checked, to
  1175. make sure all corresponding TODO items find their way into the list."
  1176. :group 'org-tags
  1177. :type 'boolean)
  1178. (defvar org-tags-history nil
  1179. "History of minibuffer reads for tags.")
  1180. (defvar org-last-tags-completion-table nil
  1181. "The last used completion table for tags.")
  1182. (defgroup org-agenda nil
  1183. "Options concerning agenda display Org-mode."
  1184. :tag "Org Agenda"
  1185. :group 'org)
  1186. (defvar org-category nil
  1187. "Variable used by org files to set a category for agenda display.
  1188. Such files should use a file variable to set it, for example
  1189. -*- mode: org; org-category: \"ELisp\"
  1190. or contain a special line
  1191. #+CATEGORY: ELisp
  1192. If the file does not specify a category, then file's base name
  1193. is used instead.")
  1194. (make-variable-buffer-local 'org-category)
  1195. (defcustom org-agenda-files nil
  1196. "The files to be used for agenda display.
  1197. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1198. \\[org-remove-file]. You can also use customize to edit the list.
  1199. If the value of the variable is not a list but a single file name, then
  1200. the list of agenda files is actually stored and maintained in that file, one
  1201. agenda file per line."
  1202. :group 'org-agenda
  1203. :type '(choice
  1204. (repeat :tag "List of files" file)
  1205. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1206. (defcustom org-agenda-custom-commands ;'(("w" todo "WAITING"))
  1207. '(("w" todo "WAITING" ((aaa 1) (bbb 2))))
  1208. "Custom commands for the agenda.
  1209. These commands will be offered on the splash screen displayed by the
  1210. agenda dispatcher \\[org-agenda]. Each entry is a list like this:
  1211. (key type match options)
  1212. key The key (a single char as a string) to be associated with the command.
  1213. type The command type, any of the following symbols:
  1214. todo Entries with a specific TODO keyword, in all agenda files.
  1215. tags Tags match in all agenda files.
  1216. tags-todo Tags match in all agenda files, TODO entries only.
  1217. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1218. tags-tree Sparse tree with all tags matches in *current* file.
  1219. occur-tree Occur sparse tree for *current* file.
  1220. match What to search for:
  1221. - a single keyword for TODO keyword searches
  1222. - a tags match expression for tags searches
  1223. - a regular expression for occur searches
  1224. options A list of option setttings, similar to that in a let form, so like
  1225. this: ((opt1 val1) (opt2 val2) ...)
  1226. You can also define a set of commands, to create a composite agenda buffer.
  1227. In this case, an entry looks like this:
  1228. (key desc (cmd1 cmd2 ...) general-options)
  1229. where
  1230. desc A description string to be displayed in the dispatcher menu.
  1231. cmd An agenda command, similar to the above. However, tree commands
  1232. are no allowed, but instead you can get agenda and global todo list.
  1233. So valid commands for a set are:
  1234. (agenda)
  1235. (alltodo)
  1236. (todo \"match\" options)
  1237. (tags \"match\" options )
  1238. (tags-todo \"match\" options)
  1239. Each command can carry a list of options, and another set of options can be
  1240. given for the whole set of commands. Individual command options take
  1241. precedence over the general options."
  1242. :group 'org-agenda
  1243. :type '(repeat
  1244. (choice
  1245. (list :tag "Single command"
  1246. (string :tag "Key")
  1247. (choice
  1248. (const :tag "Tags search (all agenda files)" tags)
  1249. (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
  1250. (const :tag "TODO keyword search (all agenda files)" todo)
  1251. (const :tag "Tags sparse tree (current buffer)" tags-tree)
  1252. (const :tag "TODO keyword tree (current buffer)" todo-tree)
  1253. (const :tag "Occur tree (current buffer)" occur-tree))
  1254. (string :tag "Match")
  1255. (repeat :tag "Local options"
  1256. (list (variable :tag "Option") (sexp :tag "Value"))))
  1257. (list :tag "Command series, all agenda files"
  1258. (string :tag "Key")
  1259. (string :tag "Description")
  1260. (repeat
  1261. (choice
  1262. (const :tag "Agenda" (agenda))
  1263. (const :tag "TODO list" (alltodo))
  1264. (list :tag "Tags search"
  1265. (const :format "" tags)
  1266. (string :tag "Match")
  1267. (repeat :tag "Local options"
  1268. (list (variable :tag "Option")
  1269. (sexp :tag "Value"))))
  1270. (list :tag "Tags search, TODO entries only"
  1271. (const :format "" tags-todo)
  1272. (string :tag "Match")
  1273. (repeat :tag "Local options"
  1274. (list (variable :tag "Option")
  1275. (sexp :tag "Value"))))
  1276. (list :tag "TODO keyword search"
  1277. (const :format "" todo)
  1278. (string :tag "Match")
  1279. (repeat :tag "Local options"
  1280. (list (variable :tag "Option")
  1281. (sexp :tag "Value"))))))
  1282. (repeat :tag "General options"
  1283. (list (variable :tag "Option")
  1284. (sexp :tag "Value")))))))
  1285. (defcustom org-agenda-todo-list-sublevels t
  1286. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1287. When nil, the sublevels of a TODO entry are not checked, resulting in
  1288. potentially much shorter TODO lists."
  1289. :group 'org-agenda
  1290. :group 'org-todo
  1291. :type 'boolean)
  1292. (defcustom org-agenda-todo-ignore-scheduled nil
  1293. "Non-nil means, don't show scheduled entries in the global todo list.
  1294. The idea behind this is that by scheduling it, you have already taken care
  1295. of this item."
  1296. :group 'org-agenda
  1297. :group 'org-todo
  1298. :type 'boolean)
  1299. (defcustom org-timeline-show-empty-dates 3
  1300. "Non-nil means, `org-timeline' also shows dates without an entry.
  1301. When nil, only the days which actually have entries are shown.
  1302. When t, all days between the first and the last date are shown.
  1303. When an integer, show also empty dates, but if there is a gap of more than
  1304. N days, just insert a special line indicating the size of the gap."
  1305. :group 'org-agenda
  1306. :type '(choice
  1307. (const :tag "None" nil)
  1308. (const :tag "All" t)
  1309. (number :tag "at most")))
  1310. (defcustom org-agenda-include-all-todo nil
  1311. "Set means weekly/daily agenda will always contain all TODO entries.
  1312. The TODO entries will be listed at the top of the agenda, before
  1313. the entries for specific days."
  1314. :group 'org-agenda
  1315. :type 'boolean)
  1316. (defcustom org-agenda-include-diary nil
  1317. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1318. :group 'org-agenda
  1319. :type 'boolean)
  1320. (defcustom org-calendar-to-agenda-key [?c]
  1321. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1322. The command `org-calendar-goto-agenda' will be bound to this key. The
  1323. default is the character `c' because then `c' can be used to switch back and
  1324. forth between agenda and calendar."
  1325. :group 'org-agenda
  1326. :type 'sexp)
  1327. (defgroup org-agenda-setup nil
  1328. "Options concerning setting up the Agenda window in Org Mode."
  1329. :tag "Org Agenda Window Setup"
  1330. :group 'org-agenda)
  1331. (defcustom org-agenda-mouse-1-follows-link nil
  1332. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1333. A longer mouse click will still set point. Does not wortk on XEmacs.
  1334. Needs to be set before org.el is loaded."
  1335. :group 'org-agenda-setup
  1336. :type 'boolean)
  1337. (defcustom org-agenda-start-with-follow-mode nil
  1338. "The initial value of follwo-mode in a newly created agenda window."
  1339. :group 'org-agenda-setup
  1340. :type 'boolean)
  1341. (defcustom org-select-agenda-window t
  1342. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1343. When nil, cursor will remain in the current window."
  1344. :group 'org-agenda-setup
  1345. :type 'boolean)
  1346. (defcustom org-fit-agenda-window t
  1347. "Non-nil means, change window size of agenda to fit content."
  1348. :group 'org-agenda-setup
  1349. :type 'boolean)
  1350. (defgroup org-agenda-display nil
  1351. "Options concerning what to display initially in Agenda."
  1352. :tag "Org Agenda Display"
  1353. :group 'org-agenda)
  1354. (defcustom org-agenda-show-all-dates t
  1355. "Non-nil means, `org-agenda' shows every day in the selected range.
  1356. When nil, only the days which actually have entries are shown."
  1357. :group 'org-agenda-display
  1358. :type 'boolean)
  1359. (defcustom org-agenda-start-on-weekday 1
  1360. "Non-nil means, start the overview always on the specified weekday.
  1361. 0 denotes Sunday, 1 denotes Monday etc.
  1362. When nil, always start on the current day."
  1363. :group 'org-agenda-display
  1364. :type '(choice (const :tag "Today" nil)
  1365. (number :tag "Weekday No.")))
  1366. (defcustom org-agenda-ndays 7
  1367. "Number of days to include in overview display.
  1368. Should be 1 or 7."
  1369. :group 'org-agenda-display
  1370. :type 'number)
  1371. (defcustom org-agenda-use-time-grid t
  1372. "Non-nil means, show a time grid in the agenda schedule.
  1373. A time grid is a set of lines for specific times (like every two hours between
  1374. 8:00 and 20:00). The items scheduled for a day at specific times are
  1375. sorted in between these lines.
  1376. For details about when the grid will be shown, and what it will look like, see
  1377. the variable `org-agenda-time-grid'."
  1378. :group 'org-agenda-display
  1379. :type 'boolean)
  1380. (defcustom org-agenda-time-grid
  1381. '((daily today require-timed)
  1382. "----------------"
  1383. (800 1000 1200 1400 1600 1800 2000))
  1384. "The settings for time grid for agenda display.
  1385. This is a list of three items. The first item is again a list. It contains
  1386. symbols specifying conditions when the grid should be displayed:
  1387. daily if the agenda shows a single day
  1388. weekly if the agenda shows an entire week
  1389. today show grid on current date, independent of daily/weekly display
  1390. require-timed show grid only if at least one item has a time specification
  1391. The second item is a string which will be places behing the grid time.
  1392. The third item is a list of integers, indicating the times that should have
  1393. a grid line."
  1394. :group 'org-agenda-display
  1395. :type
  1396. '(list
  1397. (set :greedy t :tag "Grid Display Options"
  1398. (const :tag "Show grid in single day agenda display" daily)
  1399. (const :tag "Show grid in weekly agenda display" weekly)
  1400. (const :tag "Always show grid for today" today)
  1401. (const :tag "Show grid only if any timed entries are present"
  1402. require-timed)
  1403. (const :tag "Skip grid times already present in an entry"
  1404. remove-match))
  1405. (string :tag "Grid String")
  1406. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1407. (let ((sorting-choice
  1408. '(choice
  1409. (const time-up) (const time-down)
  1410. (const category-keep) (const category-up) (const category-down)
  1411. (const tag-down) (const tag-up)
  1412. (const priority-up) (const priority-down))))
  1413. (defcustom org-agenda-sorting-strategy
  1414. '((agenda time-up category-keep priority-down)
  1415. (todo category-keep priority-down)
  1416. (tags category-keep))
  1417. "Sorting structure for the agenda items of a single day.
  1418. This is a list of symbols which will be used in sequence to determine
  1419. if an entry should be listed before another entry. The following
  1420. symbols are recognized:
  1421. time-up Put entries with time-of-day indications first, early first
  1422. time-down Put entries with time-of-day indications first, late first
  1423. category-keep Keep the default order of categories, corresponding to the
  1424. sequence in `org-agenda-files'.
  1425. category-up Sort alphabetically by category, A-Z.
  1426. category-down Sort alphabetically by category, Z-A.
  1427. tag-up Sort alphabetically by last tag, A-Z.
  1428. tag-down Sort alphabetically by last tag, Z-A.
  1429. priority-up Sort numerically by priority, high priority last.
  1430. priority-down Sort numerically by priority, high priority first.
  1431. The different possibilities will be tried in sequence, and testing stops
  1432. if one comparison returns a \"not-equal\". For example, the default
  1433. '(time-up category-keep priority-down)
  1434. means: Pull out all entries having a specified time of day and sort them,
  1435. in order to make a time schedule for the current day the first thing in the
  1436. agenda listing for the day. Of the entries without a time indication, keep
  1437. the grouped in categories, don't sort the categories, but keep them in
  1438. the sequence given in `org-agenda-files'. Within each category sort by
  1439. priority.
  1440. Leaving out `category-keep' would mean that items will be sorted across
  1441. categories by priority."
  1442. :group 'org-agenda-display
  1443. :type `(choice
  1444. (repeat :tag "General" ,sorting-choice)
  1445. (list :tag "Individually"
  1446. (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
  1447. (repeat ,sorting-choice))
  1448. (cons (const :tag "Strategy for TODO lists" todo)
  1449. (repeat ,sorting-choice))
  1450. (cons (const :tag "Strategy for Tags matches" tags)
  1451. (repeat ,sorting-choice))))))
  1452. (defcustom org-sort-agenda-notime-is-late t
  1453. "Non-nil means, items without time are considered late.
  1454. This is only relevant for sorting. When t, items which have no explicit
  1455. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1456. do have a time. When nil, the default time is before 0:00. You can use this
  1457. option to decide if the schedule for today should come before or after timeless
  1458. agenda entries."
  1459. :group 'org-agenda-display
  1460. :type 'boolean)
  1461. (defgroup org-agenda-prefix nil
  1462. "Options concerning the entry prefix in the Org-mode agenda display."
  1463. :tag "Org Agenda Prefix"
  1464. :group 'org-agenda)
  1465. (defcustom org-agenda-prefix-format
  1466. '((agenda . " %-12:c%?-12t% s")
  1467. (timeline . " % s")
  1468. (todo . " %-12:c")
  1469. (tags . " %-12:c"))
  1470. "Format specifications for the prefix of items in the agenda views.
  1471. An alist with four entries, for the different agenda types. The keys to the
  1472. sublists are `agenda', `timeline', `todo', and `tags'. The values
  1473. are format strings.
  1474. This format works similar to a printf format, with the following meaning:
  1475. %c the category of the item, \"Diary\" for entries from the diary, or
  1476. as given by the CATEGORY keyword or derived from the file name.
  1477. %T the *last* tag of the item. Last because inherited tags come
  1478. first in the list.
  1479. %t the time-of-day specification if one applies to the entry, in the
  1480. format HH:MM
  1481. %s Scheduling/Deadline information, a short string
  1482. All specifiers work basically like the standard `%s' of printf, but may
  1483. contain two additional characters: A question mark just after the `%' and
  1484. a whitespace/punctuation character just before the final letter.
  1485. If the first character after `%' is a question mark, the entire field
  1486. will only be included if the corresponding value applies to the
  1487. current entry. This is useful for fields which should have fixed
  1488. width when present, but zero width when absent. For example,
  1489. \"%?-12t\" will result in a 12 character time field if a time of the
  1490. day is specified, but will completely disappear in entries which do
  1491. not contain a time.
  1492. If there is punctuation or whitespace character just before the final
  1493. format letter, this character will be appended to the field value if
  1494. the value is not empty. For example, the format \"%-12:c\" leads to
  1495. \"Diary: \" if the category is \"Diary\". If the category were be
  1496. empty, no additional colon would be interted.
  1497. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1498. - Indent the line with two space characters
  1499. - Give the category in a 12 chars wide field, padded with whitespace on
  1500. the right (because of `-'). Append a colon if there is a category
  1501. (because of `:').
  1502. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1503. time, don't put in an empty field, just skip it (because of '?').
  1504. - Finally, put the scheduling information and append a whitespace.
  1505. As another example, if you don't want the time-of-day of entries in
  1506. the prefix, you could use:
  1507. (setq org-agenda-prefix-format \" %-11:c% s\")
  1508. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1509. `org-agenda-remove-tags-when-in-prefix'."
  1510. :type '(choice
  1511. (string :tag "General format")
  1512. (list :greedy t :tag "View dependent"
  1513. (cons (const agenda) (string :tag "Format"))
  1514. (cons (const timeline) (string :tag "Format"))
  1515. (cons (const todo) (string :tag "Format"))
  1516. (cons (const tags) (string :tag "Format"))))
  1517. :group 'org-agenda-prefix)
  1518. (defvar org-prefix-format-compiled nil
  1519. "The compiled version of the most recently used prefix format.
  1520. See the variable `org-agenda-prefix-format'.")
  1521. (defcustom org-agenda-remove-times-when-in-prefix t
  1522. "Non-nil means, remove duplicate time specifications in agenda items.
  1523. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1524. time-of-day specification in a headline or diary entry is extracted and
  1525. placed into the prefix. If this option is non-nil, the original specification
  1526. \(a timestamp or -range, or just a plain time(range) specification like
  1527. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1528. cluttered.
  1529. The option can be t or nil. It may also be the symbol `beg', indicating
  1530. that the time should only be removed what it is located at the beginning of
  1531. the headline/diary entry."
  1532. :group 'org-agenda-prefix
  1533. :type '(choice
  1534. (const :tag "Always" t)
  1535. (const :tag "Never" nil)
  1536. (const :tag "When at beginning of entry" beg)))
  1537. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1538. "Non-nil means, remove the tags from the headline copy in the agenda.
  1539. When this is the symbol `prefix', only remove tags when
  1540. `org-agenda-prefix-format' contains a `%T' specifier."
  1541. :group 'org-agenda-prefix
  1542. :type '(choice
  1543. (const :tag "Always" t)
  1544. (const :tag "Never" nil)
  1545. (const :tag "When prefix format contains %T" prefix)))
  1546. (defcustom org-agenda-align-tags-to-column 65
  1547. "Shift tags in agenda items to this column."
  1548. :group 'org-agenda-prefix ;; FIXME
  1549. :type 'integer)
  1550. (defgroup org-latex nil
  1551. "Options for embedding LaTeX code into Org-mode"
  1552. :tag "Org LaTeX"
  1553. :group 'org)
  1554. (defcustom org-format-latex-options
  1555. '(:foreground "Black" :background "Transparent" :scale 1.0
  1556. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1557. "Options for creating images from LaTeX fragments.
  1558. This is a property list with the following properties:
  1559. :foreground the foreground color, for example \"Black\".
  1560. :background the background color, or \"Transparent\".
  1561. :scale a scaling factor for the size of the images
  1562. :matchers a list indicating which matchers should be used to
  1563. find LaTeX fragments. Valid members of this list are:
  1564. \"begin\" find environments
  1565. \"$\" find math expressions surrounded by $...$
  1566. \"$$\" find math expressions surrounded by $$....$$
  1567. \"\\(\" find math expressions surrounded by \\(...\\)
  1568. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1569. :group 'org-latex
  1570. :type 'plist)
  1571. (defgroup org-export nil
  1572. "Options for exporting org-listings."
  1573. :tag "Org Export"
  1574. :group 'org)
  1575. (defgroup org-export-general nil
  1576. "General options for exporting Org-mode files."
  1577. :tag "Org Export General"
  1578. :group 'org-export)
  1579. (defcustom org-export-publishing-directory "."
  1580. "Path to the location where exported files should be located.
  1581. This path may be relative to the directory where the Org-mode file lives.
  1582. The default is to put them into the same directory as the Org-mode file.
  1583. The variable may also be an alist with export types `:html', `:ascii',
  1584. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1585. is relative, it is interpreted relative to the directory where the exported
  1586. Org-mode files lives."
  1587. :group 'org-export-general
  1588. :type '(choice
  1589. (directory)
  1590. (repeat
  1591. (cons
  1592. (choice :tag "Type"
  1593. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1594. (directory)))))
  1595. (defcustom org-export-language-setup
  1596. '(("en" "Author" "Date" "Table of Contents")
  1597. ("cs" "Autor" "Datum" "Obsah")
  1598. ("da" "Ophavsmand" "Dato" "Indhold")
  1599. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1600. ("es" "Autor" "Fecha" "\xccndice")
  1601. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1602. ("it" "Autore" "Data" "Indice")
  1603. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1604. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1605. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1606. "Terms used in export text, translated to different languages.
  1607. Use the variable `org-export-default-language' to set the language,
  1608. or use the +OPTION lines for a per-file setting."
  1609. :group 'org-export-general
  1610. :type '(repeat
  1611. (list
  1612. (string :tag "HTML language tag")
  1613. (string :tag "Author")
  1614. (string :tag "Date")
  1615. (string :tag "Table of Contents"))))
  1616. (defcustom org-export-default-language "en"
  1617. "The default language of HTML export, as a string.
  1618. This should have an association in `org-export-language-setup'."
  1619. :group 'org-export-general
  1620. :type 'string)
  1621. (defcustom org-export-headline-levels 3
  1622. "The last level which is still exported as a headline.
  1623. Inferior levels will produce itemize lists when exported.
  1624. Note that a numeric prefix argument to an exporter function overrides
  1625. this setting.
  1626. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1627. :group 'org-export-general
  1628. :type 'number)
  1629. (defcustom org-export-with-section-numbers t
  1630. "Non-nil means, add section numbers to headlines when exporting.
  1631. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1632. :group 'org-export-general
  1633. :type 'boolean)
  1634. (defcustom org-export-with-toc t
  1635. "Non-nil means, create a table of contents in exported files.
  1636. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1637. Headlines which contain any TODO items will be marked with \"(*)\" in
  1638. ASCII export, and with red color in HTML output.
  1639. In HTML output, the TOC will be clickable.
  1640. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1641. :group 'org-export-general
  1642. :type 'boolean)
  1643. (defcustom org-export-mark-todo-in-toc nil
  1644. "Non-nil means, mark TOC lines that contain any open TODO items."
  1645. :group 'org-export-general
  1646. :type 'boolean)
  1647. (defcustom org-export-preserve-breaks nil
  1648. "Non-nil means, preserve all line breaks when exporting.
  1649. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1650. export, line breaks will always be preserved, regardless of this variable.
  1651. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1652. :group 'org-export-general
  1653. :type 'boolean)
  1654. (defcustom org-export-with-archived-trees 'headline
  1655. "Whether subtrees with the ARCHIVE tag should be exported.
  1656. This can have three different values
  1657. nil Do not export, pretend this tree is not present
  1658. t Do export the entire tree
  1659. headline Only export the headline, but skip the tree below it."
  1660. :group 'org-export-general
  1661. :group 'org-archive
  1662. :type '(choice
  1663. (const :tag "not at all" nil)
  1664. (const :tag "headline only" 'headline)
  1665. (const :tag "entirely" t)))
  1666. (defcustom org-export-with-timestamps t
  1667. "Nil means, do not export time stamps and associated keywords."
  1668. :group 'org-export-general
  1669. :type 'boolean)
  1670. (defcustom org-export-remove-timestamps-from-toc t
  1671. "Nil means, remove timestamps from the table of contents entries."
  1672. :group 'org-export-general
  1673. :type 'boolean)
  1674. (defcustom org-export-with-tags t
  1675. "Nil means, do not export tags, just remove them from headlines."
  1676. :group 'org-export-general
  1677. :type 'boolean)
  1678. (defgroup org-export-translation nil
  1679. "Options for translating special ascii sequences for the export backends."
  1680. :tag "Org Export Translation"
  1681. :group 'org-export)
  1682. (defcustom org-export-with-emphasize t
  1683. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1684. If the export target supports emphasizing text, the word will be
  1685. typeset in bold, italic, or underlined, respectively. Works only for
  1686. single words, but you can say: I *really* *mean* *this*.
  1687. Not all export backends support this.
  1688. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1689. :group 'org-export-translation
  1690. :type 'boolean)
  1691. (defcustom org-export-with-sub-superscripts t
  1692. "Non-nil means, interpret \"_\" and \"^\" for export.
  1693. When this option is turned on, you can use TeX-like syntax for sub- and
  1694. superscripts. Several characters after \"_\" or \"^\" will be
  1695. considered as a single item - so grouping with {} is normally not
  1696. needed. For example, the following things will be parsed as single
  1697. sub- or superscripts.
  1698. 10^24 or 10^tau several digits will be considered 1 item.
  1699. 10^-12 or 10^-tau a leading sign with digits or a word
  1700. x^2-y^3 will be read as x^2 - y^3, because items are
  1701. terminated by almost any nonword/nondigit char.
  1702. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1703. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1704. sub/superscript.
  1705. Not all export backends support this, but HTML does.
  1706. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1707. :group 'org-export-translation
  1708. :type 'boolean)
  1709. (defcustom org-export-with-TeX-macros t
  1710. "Non-nil means, interpret simple TeX-like macros when exporting.
  1711. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1712. No only real TeX macros will work here, but the standard HTML entities
  1713. for math can be used as macro names as well. For a list of supported
  1714. names in HTML export, see the constant `org-html-entities'.
  1715. Not all export backends support this.
  1716. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1717. :group 'org-export-translation
  1718. :group 'org-latex
  1719. :type 'boolean)
  1720. (defcustom org-export-with-LaTeX-fragments nil
  1721. "Non-nil means, convert LaTeX fragments to images when exporting to HTML.
  1722. When set, the exporter will find LaTeX environments if the \\begin line is
  1723. the first non-white thing on a line. It will also find the math delimiters
  1724. like $a=b$ and \\( a=b \\) for inline math, $$a=b$$ and \\[ a=b \\] for
  1725. display math.
  1726. This option can also be set with the +OPTIONS line, e.g. \"LaTeX:t\"."
  1727. :group 'org-export-translation
  1728. :group 'org-latex
  1729. :type 'boolean)
  1730. (defcustom org-export-with-fixed-width t
  1731. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1732. This can be used to have pre-formatted text, fragments of code etc. For
  1733. example:
  1734. : ;; Some Lisp examples
  1735. : (while (defc cnt)
  1736. : (ding))
  1737. will be looking just like this in also HTML. See also the QUOTE keyword.
  1738. Not all export backends support this.
  1739. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1740. :group 'org-export-translation
  1741. :type 'boolean)
  1742. (defcustom org-match-sexp-depth 3
  1743. "Number of stacked braces for sub/superscript matching.
  1744. This has to be set before loading org.el to be effective."
  1745. :group 'org-export-translation
  1746. :type 'integer)
  1747. (defgroup org-export-tables nil
  1748. "Options for exporting tables in Org-mode."
  1749. :tag "Org Export Tables"
  1750. :group 'org-export)
  1751. (defcustom org-export-with-tables t
  1752. "If non-nil, lines starting with \"|\" define a table.
  1753. For example:
  1754. | Name | Address | Birthday |
  1755. |-------------+----------+-----------|
  1756. | Arthur Dent | England | 29.2.2100 |
  1757. Not all export backends support this.
  1758. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1759. :group 'org-export-tables
  1760. :type 'boolean)
  1761. (defcustom org-export-highlight-first-table-line t
  1762. "Non-nil means, highlight the first table line.
  1763. In HTML export, this means use <th> instead of <td>.
  1764. In tables created with table.el, this applies to the first table line.
  1765. In Org-mode tables, all lines before the first horizontal separator
  1766. line will be formatted with <th> tags."
  1767. :group 'org-export-tables
  1768. :type 'boolean)
  1769. (defcustom org-export-table-remove-special-lines t
  1770. "Remove special lines and marking characters in calculating tables.
  1771. This removes the special marking character column from tables that are set
  1772. up for spreadsheet calculations. It also removes the entire lines
  1773. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1774. the values of constants may be useful to have."
  1775. :group 'org-export-tables
  1776. :type 'boolean)
  1777. (defcustom org-export-prefer-native-exporter-for-tables nil
  1778. "Non-nil means, always export tables created with table.el natively.
  1779. Natively means, use the HTML code generator in table.el.
  1780. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1781. the table does not use row- or column-spanning). This has the
  1782. advantage, that the automatic HTML conversions for math symbols and
  1783. sub/superscripts can be applied. Org-mode's HTML generator is also
  1784. much faster."
  1785. :group 'org-export-tables
  1786. :type 'boolean)
  1787. (defgroup org-export-ascii nil
  1788. "Options specific for ASCII export of Org-mode files."
  1789. :tag "Org Export ASCII"
  1790. :group 'org-export)
  1791. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  1792. "Characters for underlining headings in ASCII export.
  1793. In the given sequence, these characters will be used for level 1, 2, ..."
  1794. :group 'org-export-ascii
  1795. :type '(repeat character))
  1796. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  1797. "Bullet characters for headlines converted to lists in ASCII export.
  1798. The first character is is used for the first lest level generated in this
  1799. way, and so on. If there are more levels than characters given here,
  1800. the list will be repeated.
  1801. Note that plain lists will keep the same bullets as the have in the
  1802. Org-mode file."
  1803. :group 'org-export-ascii
  1804. :type '(repeat character))
  1805. (defcustom org-export-ascii-show-new-buffer t
  1806. "Non-nil means, popup buffer containing the exported ASCII text.
  1807. Otherwise the buffer will just be saved to a file and stay hidden."
  1808. :group 'org-export-ascii
  1809. :type 'boolean)
  1810. (defgroup org-export-xml nil
  1811. "Options specific for XML export of Org-mode files."
  1812. :tag "Org Export XML"
  1813. :group 'org-export)
  1814. (defgroup org-export-html nil
  1815. "Options specific for HTML export of Org-mode files."
  1816. :tag "Org Export HTML"
  1817. :group 'org-export)
  1818. (defcustom org-export-html-style
  1819. "<style type=\"text/css\">
  1820. html {
  1821. font-family: Times, serif;
  1822. font-size: 12pt;
  1823. }
  1824. .title { text-align: center; }
  1825. .todo { color: red; }
  1826. .done { color: green; }
  1827. .timestamp { color: grey }
  1828. .timestamp-kwd { color: CadetBlue }
  1829. .tag { background-color:lightblue; font-weight:normal }
  1830. .target { background-color: lavender; }
  1831. pre {
  1832. border: 1pt solid #AEBDCC;
  1833. background-color: #F3F5F7;
  1834. padding: 5pt;
  1835. font-family: courier, monospace;
  1836. }
  1837. table { border-collapse: collapse; }
  1838. td, th {
  1839. vertical-align: top;
  1840. border: 1pt solid #ADB9CC;
  1841. }
  1842. </style>"
  1843. "The default style specification for exported HTML files.
  1844. Since there are different ways of setting style information, this variable
  1845. needs to contain the full HTML structure to provide a style, including the
  1846. surrounding HTML tags. The style specifications should include definitions
  1847. for new classes todo, done, title, and deadline. For example, legal values
  1848. would be:
  1849. <style type=\"text/css\">
  1850. p { font-weight: normal; color: gray; }
  1851. h1 { color: black; }
  1852. .title { text-align: center; }
  1853. .todo, .deadline { color: red; }
  1854. .done { color: green; }
  1855. </style>
  1856. or, if you want to keep the style in a file,
  1857. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1858. As the value of this option simply gets inserted into the HTML <head> header,
  1859. you can \"misuse\" it to add arbitrary text to the header."
  1860. :group 'org-export-html
  1861. :type 'string)
  1862. (defcustom org-export-html-title-format "<h1 class=\"title\">%s</h1>\n"
  1863. "Format for typesetting the document title in HTML export."
  1864. :group 'org-export-html
  1865. :type 'string)
  1866. (defcustom org-export-html-toplevel-hlevel 2
  1867. "The <H> level for level 1 headings in HTML export."
  1868. :group 'org-export-html
  1869. :type 'string)
  1870. (defcustom org-export-html-link-org-files-as-html t
  1871. "Non-nil means, make file links to `file.org' point to `file.html'.
  1872. When org-mode is exporting an org-mode file to HTML, links to
  1873. non-html files are directly put into a href tag in HTML.
  1874. However, links to other Org-mode files (recognized by the
  1875. extension `.org.) should become links to the corresponding html
  1876. file, assuming that the linked org-mode file will also be
  1877. converted to HTML.
  1878. When nil, the links still point to the plain `.org' file."
  1879. :group 'org-export-html
  1880. :type 'boolean)
  1881. (defcustom org-export-html-inline-images 'maybe
  1882. "Non-nil means, inline images into exported HTML pages.
  1883. This is done using an <img> tag. When nil, an anchor with href is used to
  1884. link to the image. If this option is `maybe', then images in links with
  1885. an empty description will be inlined, while images with a description will
  1886. be linked only."
  1887. :group 'org-export-html
  1888. :type '(choice (const :tag "Never" nil)
  1889. (const :tag "Always" t)
  1890. (const :tag "When there is no description" maybe)))
  1891. (defcustom org-export-html-expand t
  1892. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1893. When nil, these tags will be exported as plain text and therefore
  1894. not be interpreted by a browser.
  1895. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1896. :group 'org-export-html
  1897. :type 'boolean)
  1898. (defcustom org-export-html-table-tag
  1899. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  1900. "The HTML tag used to start a table.
  1901. This must be a <table> tag, but you may change the options like
  1902. borders and spacing."
  1903. :group 'org-export-html
  1904. :type 'string)
  1905. (defcustom org-export-html-with-timestamp nil
  1906. "If non-nil, write `org-export-html-html-helper-timestamp'
  1907. into the exported HTML text. Otherwise, the buffer will just be saved
  1908. to a file."
  1909. :group 'org-export-html
  1910. :type 'boolean)
  1911. (defcustom org-export-html-html-helper-timestamp
  1912. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  1913. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1914. :group 'org-export-html
  1915. :type 'string)
  1916. (defcustom org-export-html-show-new-buffer nil
  1917. "Non-nil means, popup buffer containing the exported html text.
  1918. Otherwise, the buffer will just be saved to a file and stay hidden."
  1919. :group 'org-export-html
  1920. :type 'boolean)
  1921. (defgroup org-export-icalendar nil
  1922. "Options specific for iCalendar export of Org-mode files."
  1923. :tag "Org Export iCalendar"
  1924. :group 'org-export)
  1925. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1926. "The file name for the iCalendar file covering all agenda files.
  1927. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1928. The file name should be absolute."
  1929. :group 'org-export-icalendar
  1930. :type 'file)
  1931. (defcustom org-icalendar-include-todo nil
  1932. "Non-nil means, export to iCalendar files should also cover TODO items."
  1933. :group 'org-export-icalendar
  1934. :type 'boolean)
  1935. (defcustom org-icalendar-combined-name "OrgMode"
  1936. "Calendar name for the combined iCalendar representing all agenda files."
  1937. :group 'org-export-icalendar
  1938. :type 'string)
  1939. (defgroup org-font-lock nil
  1940. "Font-lock settings for highlighting in Org-mode."
  1941. :tag "Org Font Lock"
  1942. :group 'org)
  1943. (defcustom org-level-color-stars-only nil
  1944. "Non-nil means fontify only the stars in each headline.
  1945. When nil, the entire headline is fontified.
  1946. Changing it requires restart of `font-lock-mode' to become effective
  1947. also in regions already fontified."
  1948. :group 'org-font-lock
  1949. :type 'boolean)
  1950. (defcustom org-hide-leading-stars nil
  1951. "Non-nil means, hide the first N-1 stars in a headline.
  1952. This works by using the face `org-hide' for these stars. This
  1953. face is white for a light background, and black for a dark
  1954. background. You may have to customize the face `org-hide' to
  1955. make this work.
  1956. Changing it requires restart of `font-lock-mode' to become effective
  1957. also in regions already fontified.
  1958. You may also set this on a per-file basis by adding one of the following
  1959. lines to the buffer:
  1960. #+STARTUP: hidestars
  1961. #+STARTUP: showstars"
  1962. :group 'org-font-lock
  1963. :type 'boolean)
  1964. (defcustom org-fontify-done-headline nil
  1965. "Non-nil means, change the face of a headline if it is marked DONE.
  1966. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1967. When this is non-nil, the headline after the keyword is set to the
  1968. `org-headline-done' as an additional indication."
  1969. :group 'org-font-lock
  1970. :type 'boolean)
  1971. (defcustom org-fontify-emphasized-text t
  1972. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1973. Changing this variable requires a restart of Emacs to take effect."
  1974. :group 'org-font-lock
  1975. :type 'boolean)
  1976. (defvar org-emph-re nil
  1977. "Regular expression for matching emphasis.")
  1978. (defvar org-emphasis-regexp-components) ; defined just below
  1979. (defvar org-emphasis-alist) ; defined just below
  1980. (defun org-set-emph-re (var val)
  1981. "Set variable and compute the emphasis regular expression."
  1982. (set var val)
  1983. (when (and (boundp 'org-emphasis-alist)
  1984. (boundp 'org-emphasis-regexp-components)
  1985. org-emphasis-alist org-emphasis-regexp-components)
  1986. (let* ((e org-emphasis-regexp-components)
  1987. (pre (car e))
  1988. (post (nth 1 e))
  1989. (border (nth 2 e))
  1990. (body (nth 3 e))
  1991. (nl (nth 4 e))
  1992. (stacked (nth 5 e))
  1993. (body1 (concat body "*?"))
  1994. (markers (mapconcat 'car org-emphasis-alist "")))
  1995. ;; make sure special characters appear at the right position in the class
  1996. (if (string-match "\\^" markers)
  1997. (setq markers (concat (replace-match "" t t markers) "^")))
  1998. (if (string-match "-" markers)
  1999. (setq markers (concat (replace-match "" t t markers) "-")))
  2000. (while (>= (setq nl (1- nl)) 0) (setq body1 (concat body1 "\n?" body "*?")))
  2001. ;; Make the regexp
  2002. (setq org-emph-re
  2003. (concat "\\([" pre (if stacked markers) "]\\|^\\)"
  2004. "\\("
  2005. "\\([" markers "]\\)"
  2006. "\\("
  2007. "[^" border markers "]"
  2008. body1
  2009. "[^" border markers "]"
  2010. "\\)"
  2011. "\\3\\)"
  2012. "\\([" post (if stacked markers) "]\\|$\\)")))))
  2013. (defcustom org-emphasis-regexp-components
  2014. '(" \t(" " \t.,?;:'\")" " \t\r\n,." "." 1 nil)
  2015. "Components used to build the reqular expression for emphasis.
  2016. This is a list with 6 entries. Terminology: In an emphasis string
  2017. like \" *strong word* \", we call the initial space PREMATCH, the final
  2018. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2019. and \"trong wor\" is the body. The different components in this variable
  2020. specify what is allowed/forbidden in each part:
  2021. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2022. post Chars allowed as postmatch. End of line will be allowed too.
  2023. border The chars *forbidden* as border characters. In addition to the
  2024. characters given here, all marker characters are forbidden too.
  2025. body-regexp A regexp like \".\" to match a body character. Don't use
  2026. non-shy groups here, and don't allow newline here.
  2027. newline The maximum number of newlines allowed in an emphasis exp.
  2028. stacked Non-nil means, allow stacked styles. This works only in HTML
  2029. export. When this is set, all marker characters (as given in
  2030. `org-emphasis-alist') will be allowed as pre/post, aiding
  2031. inside-out matching.
  2032. Use customize to modify this, or restart emacs after changing it."
  2033. :group 'org-font-lock
  2034. :set 'org-set-emph-re
  2035. :type '(list
  2036. (sexp :tag "Allowed chars in pre ")
  2037. (sexp :tag "Allowed chars in post ")
  2038. (sexp :tag "Forbidden chars in border ")
  2039. (sexp :tag "Regexp for body ")
  2040. (integer :tag "number of newlines allowed")
  2041. (boolean :tag "Stacking allowed ")))
  2042. (defcustom org-emphasis-alist
  2043. '(("*" bold "<b>" "</b>")
  2044. ("/" italic "<i>" "</i>")
  2045. ("_" underline "<u>" "</u>")
  2046. ("=" shadow "<code>" "</code>")
  2047. ("+" (:strike-through t) "<del>" "</del>")
  2048. )
  2049. "Special syntax for emphasised text.
  2050. Text starting and ending with a special character will be emphasized, for
  2051. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2052. characters, the face to bbe used by font-lock for highlighting in Org-mode
  2053. emacs buffers, and the HTML tags to be used for this.
  2054. Use customize to modify this, or restart emacs after changing it."
  2055. :group 'org-font-lock
  2056. :set 'org-set-emph-re
  2057. :type '(repeat
  2058. (list
  2059. (string :tag "Marker character")
  2060. (choice
  2061. (face :tag "Font-lock-face")
  2062. (plist :tag "Face property list"))
  2063. (string :tag "HTML start tag")
  2064. (string :tag "HTML end tag"))))
  2065. (defgroup org-faces nil
  2066. "Faces in Org-mode."
  2067. :tag "Org Faces"
  2068. :group 'org-font-lock)
  2069. (defun org-compatible-face (specs)
  2070. "Make a compatible face specification.
  2071. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  2072. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  2073. to the top of the list. The `min-colors' attribute will be removed from
  2074. any other entries, and any resulting duplicates will be removed entirely."
  2075. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  2076. (let (r e a)
  2077. (while (setq e (pop specs))
  2078. (cond
  2079. ((memq (car e) '(t default)) (push e r))
  2080. ((setq a (member '(min-colors 8) (car e)))
  2081. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  2082. (cdr e)))))
  2083. ((setq a (assq 'min-colors (car e)))
  2084. (setq e (cons (delq a (car e)) (cdr e)))
  2085. (or (assoc (car e) r) (push e r)))
  2086. (t (or (assoc (car e) r) (push e r)))))
  2087. (nreverse r))
  2088. specs))
  2089. (defface org-hide
  2090. '((((background light)) (:foreground "white"))
  2091. (((background dark)) (:foreground "black")))
  2092. "Face used to hide leading stars in headlines.
  2093. The forground color of this face should be equal to the background
  2094. color of the frame."
  2095. :group 'org-faces)
  2096. (defface org-level-1 ;; font-lock-function-name-face
  2097. (org-compatible-face
  2098. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2099. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2100. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2101. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2102. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  2103. (t (:bold t))))
  2104. "Face used for level 1 headlines."
  2105. :group 'org-faces)
  2106. (defface org-level-2 ;; font-lock-variable-name-face
  2107. (org-compatible-face
  2108. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2109. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2110. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  2111. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  2112. (t (:bold t))))
  2113. "Face used for level 2 headlines."
  2114. :group 'org-faces)
  2115. (defface org-level-3 ;; font-lock-keyword-face
  2116. (org-compatible-face
  2117. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  2118. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  2119. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  2120. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  2121. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  2122. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  2123. (t (:bold t))))
  2124. "Face used for level 3 headlines."
  2125. :group 'org-faces)
  2126. (defface org-level-4 ;; font-lock-comment-face
  2127. (org-compatible-face
  2128. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2129. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2130. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  2131. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  2132. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2133. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2134. (t (:bold t))))
  2135. "Face used for level 4 headlines."
  2136. :group 'org-faces)
  2137. (defface org-level-5 ;; font-lock-type-face
  2138. (org-compatible-face
  2139. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2140. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2141. (((class color) (min-colors 8)) (:foreground "green"))))
  2142. "Face used for level 5 headlines."
  2143. :group 'org-faces)
  2144. (defface org-level-6 ;; font-lock-constant-face
  2145. (org-compatible-face
  2146. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  2147. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  2148. (((class color) (min-colors 8)) (:foreground "magenta"))))
  2149. "Face used for level 6 headlines."
  2150. :group 'org-faces)
  2151. (defface org-level-7 ;; font-lock-builtin-face
  2152. (org-compatible-face
  2153. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  2154. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  2155. (((class color) (min-colors 8)) (:foreground "blue"))))
  2156. "Face used for level 7 headlines."
  2157. :group 'org-faces)
  2158. (defface org-level-8 ;; font-lock-string-face
  2159. (org-compatible-face
  2160. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2161. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2162. (((class color) (min-colors 8)) (:foreground "green"))))
  2163. "Face used for level 8 headlines."
  2164. :group 'org-faces)
  2165. (defface org-special-keyword ;; font-lock-string-face
  2166. (org-compatible-face
  2167. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2168. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2169. (t (:italic t))))
  2170. "Face used for special keywords."
  2171. :group 'org-faces)
  2172. (defface org-warning ;; font-lock-warning-face
  2173. (org-compatible-face
  2174. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2175. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2176. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2177. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2178. (t (:bold t))))
  2179. "Face for deadlines and TODO keywords."
  2180. :group 'org-faces)
  2181. (defface org-headline-done ;; font-lock-string-face
  2182. (org-compatible-face
  2183. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  2184. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  2185. (((class color) (min-colors 8) (background light)) (:bold nil))))
  2186. "Face used to indicate that a headline is DONE.
  2187. This face is only used if `org-fontify-done-headline' is set."
  2188. :group 'org-faces)
  2189. (defface org-archived ; similar to shadow
  2190. (org-compatible-face
  2191. '((((class color grayscale) (min-colors 88) (background light))
  2192. (:foreground "grey50"))
  2193. (((class color grayscale) (min-colors 88) (background dark))
  2194. (:foreground "grey70"))
  2195. (((class color) (min-colors 8) (background light))
  2196. (:foreground "green"))
  2197. (((class color) (min-colors 8) (background dark))
  2198. (:foreground "yellow"))))
  2199. "Face for headline with the ARCHIVE tag."
  2200. :group 'org-faces)
  2201. (defface org-link
  2202. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2203. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2204. (t (:underline t)))
  2205. "Face for links."
  2206. :group 'org-faces)
  2207. (defface org-date
  2208. '((((class color) (background light)) (:foreground "Purple" :underline t))
  2209. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  2210. (t (:underline t)))
  2211. "Face for links."
  2212. :group 'org-faces)
  2213. (defface org-tag
  2214. '((t (:bold t)))
  2215. "Face for tags."
  2216. :group 'org-faces)
  2217. (defface org-todo ;; font-lock-warning-face
  2218. (org-compatible-face
  2219. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2220. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2221. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2222. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2223. (t (:inverse-video t :bold t))))
  2224. "Face for TODO keywords."
  2225. :group 'org-faces)
  2226. (defface org-done ;; font-lock-type-face
  2227. (org-compatible-face
  2228. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen" :bold t))
  2229. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen" :bold t))
  2230. (((class color) (min-colors 8)) (:foreground "green"))
  2231. (t (:bold t))))
  2232. "Face used for DONE."
  2233. :group 'org-faces)
  2234. (defface org-table ;; font-lock-function-name-face
  2235. (org-compatible-face
  2236. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2237. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2238. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2239. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2240. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2241. (((class color) (min-colors 8) (background dark)))))
  2242. "Face used for tables."
  2243. :group 'org-faces)
  2244. (defface org-formula
  2245. (org-compatible-face
  2246. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2247. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2248. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2249. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2250. (t (:bold t :italic t))))
  2251. "Face for formulas."
  2252. :group 'org-faces)
  2253. (defface org-scheduled-today
  2254. (org-compatible-face
  2255. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2256. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2257. (((class color) (min-colors 8)) (:foreground "green"))
  2258. (t (:bold t :italic t))))
  2259. "Face for items scheduled for a certain day."
  2260. :group 'org-faces)
  2261. (defface org-scheduled-previously
  2262. (org-compatible-face
  2263. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2264. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2265. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2266. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2267. (t (:bold t))))
  2268. "Face for items scheduled previously, and not yet done."
  2269. :group 'org-faces)
  2270. (defface org-upcoming-deadline
  2271. (org-compatible-face
  2272. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2273. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2274. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2275. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2276. (t (:bold t))))
  2277. "Face for items scheduled previously, and not yet done."
  2278. :group 'org-faces)
  2279. (defface org-time-grid ;; font-lock-variable-name-face
  2280. (org-compatible-face
  2281. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2282. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2283. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2284. "Face used for time grids."
  2285. :group 'org-faces)
  2286. (defconst org-level-faces
  2287. '(org-level-1 org-level-2 org-level-3 org-level-4
  2288. org-level-5 org-level-6 org-level-7 org-level-8
  2289. ))
  2290. (defconst org-n-levels (length org-level-faces))
  2291. ;; Variables for pre-computed regular expressions, all buffer local
  2292. (defvar org-done-string nil
  2293. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2294. (make-variable-buffer-local 'org-done-string)
  2295. (defvar org-todo-regexp nil
  2296. "Matches any of the TODO state keywords.")
  2297. (make-variable-buffer-local 'org-todo-regexp)
  2298. (defvar org-not-done-regexp nil
  2299. "Matches any of the TODO state keywords except the last one.")
  2300. (make-variable-buffer-local 'org-not-done-regexp)
  2301. (defvar org-todo-line-regexp nil
  2302. "Matches a headline and puts TODO state into group 2 if present.")
  2303. (make-variable-buffer-local 'org-todo-line-regexp)
  2304. (defvar org-todo-line-tags-regexp nil
  2305. "Matches a headline and puts TODO state into group 2 if present.
  2306. Also put tags into group 4 if tags are present.")
  2307. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2308. (defvar org-nl-done-regexp nil
  2309. "Matches newline followed by a headline with the DONE keyword.")
  2310. (make-variable-buffer-local 'org-nl-done-regexp)
  2311. (defvar org-looking-at-done-regexp nil
  2312. "Matches the DONE keyword a point.")
  2313. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2314. (defvar org-todo-kwd-priority-p nil
  2315. "Do TODO items have priorities?")
  2316. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2317. (defvar org-todo-kwd-max-priority nil
  2318. "Maximum priority of TODO items.")
  2319. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2320. (defvar org-ds-keyword-length 12
  2321. "Maximum length of the Deadline and SCHEDULED keywords.")
  2322. (make-variable-buffer-local 'org-ds-keyword-length)
  2323. (defvar org-deadline-regexp nil
  2324. "Matches the DEADLINE keyword.")
  2325. (make-variable-buffer-local 'org-deadline-regexp)
  2326. (defvar org-deadline-time-regexp nil
  2327. "Matches the DEADLINE keyword together with a time stamp.")
  2328. (make-variable-buffer-local 'org-deadline-time-regexp)
  2329. (defvar org-deadline-line-regexp nil
  2330. "Matches the DEADLINE keyword and the rest of the line.")
  2331. (make-variable-buffer-local 'org-deadline-line-regexp)
  2332. (defvar org-scheduled-regexp nil
  2333. "Matches the SCHEDULED keyword.")
  2334. (make-variable-buffer-local 'org-scheduled-regexp)
  2335. (defvar org-scheduled-time-regexp nil
  2336. "Matches the SCHEDULED keyword together with a time stamp.")
  2337. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2338. (defvar org-closed-time-regexp nil
  2339. "Matches the CLOSED keyword together with a time stamp.")
  2340. (make-variable-buffer-local 'org-closed-time-regexp)
  2341. (defvar org-keyword-time-regexp nil
  2342. "Matches any of the 3 keywords, together with the time stamp.")
  2343. (make-variable-buffer-local 'org-keyword-time-regexp)
  2344. (defvar org-maybe-keyword-time-regexp nil
  2345. "Matches a timestamp, possibly preceeded by a keyword.")
  2346. (make-variable-buffer-local 'org-keyword-time-regexp)
  2347. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2348. mouse-map t)
  2349. "Properties to remove when a string without properties is wanted.")
  2350. (defsubst org-match-string-no-properties (num &optional string)
  2351. (if (featurep 'xemacs)
  2352. (let ((s (match-string num string)))
  2353. (remove-text-properties 0 (length s) org-rm-props s)
  2354. s)
  2355. (match-string-no-properties num string)))
  2356. (defsubst org-no-properties (s)
  2357. (remove-text-properties 0 (length s) org-rm-props s)
  2358. s)
  2359. (defsubst org-set-local (var value)
  2360. "Make VAR local in current buffer and set it to VALUE."
  2361. (set (make-variable-buffer-local var) value))
  2362. (defsubst org-mode-p ()
  2363. "Check if the current buffer is in Org-mode."
  2364. (eq major-mode 'org-mode))
  2365. (defun org-let (list &rest body)
  2366. (eval (cons 'let (cons list body))))
  2367. (put 'org-let 'lisp-indent-function 1)
  2368. (defun org-let2 (list1 list2 &rest body)
  2369. (eval (cons 'let (cons list1 (list (cons 'let (cons list2 body)))))))
  2370. (put 'org-let2 'lisp-indent-function 2)
  2371. (defconst org-startup-options
  2372. '(("fold" org-startup-folded t)
  2373. ("overview" org-startup-folded t)
  2374. ("nofold" org-startup-folded nil)
  2375. ("showall" org-startup-folded nil)
  2376. ("content" org-startup-folded content)
  2377. ("hidestars" org-hide-leading-stars t)
  2378. ("showstars" org-hide-leading-stars nil)
  2379. ("odd" org-odd-levels-only t)
  2380. ("oddeven" org-odd-levels-only nil)
  2381. ("align" org-startup-align-all-tables t)
  2382. ("noalign" org-startup-align-all-tables nil)
  2383. ("logging" org-log-done t)
  2384. ("nologging" org-log-done nil)
  2385. ("dlcheck" org-startup-with-deadline-check t)
  2386. ("nodlcheck" org-startup-with-deadline-check nil)))
  2387. (defun org-set-regexps-and-options ()
  2388. "Precompute regular expressions for current buffer."
  2389. (when (org-mode-p)
  2390. (let ((re (org-make-options-regexp
  2391. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2392. "STARTUP" "ARCHIVE" "TAGS" "CALC")))
  2393. (splitre "[ \t]+")
  2394. kwds int key value cat arch tags)
  2395. (save-excursion
  2396. (save-restriction
  2397. (widen)
  2398. (goto-char (point-min))
  2399. (while (re-search-forward re nil t)
  2400. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2401. (cond
  2402. ((equal key "CATEGORY")
  2403. (if (string-match "[ \t]+$" value)
  2404. (setq value (replace-match "" t t value)))
  2405. (setq cat (intern value)))
  2406. ((equal key "SEQ_TODO")
  2407. (setq int 'sequence
  2408. kwds (append kwds (org-split-string value splitre))))
  2409. ((equal key "PRI_TODO")
  2410. (setq int 'priority
  2411. kwds (append kwds (org-split-string value splitre))))
  2412. ((equal key "TYP_TODO")
  2413. (setq int 'type
  2414. kwds (append kwds (org-split-string value splitre))))
  2415. ((equal key "TAGS")
  2416. (setq tags (append tags (org-split-string value splitre))))
  2417. ((equal key "STARTUP")
  2418. (let ((opts (org-split-string value splitre))
  2419. l var val)
  2420. (while (setq l (assoc (pop opts) org-startup-options))
  2421. (setq var (nth 1 l) val (nth 2 l))
  2422. (set (make-local-variable var) val))))
  2423. ((equal key "ARCHIVE")
  2424. (string-match " *$" value)
  2425. (setq arch (replace-match "" t t value))
  2426. (remove-text-properties 0 (length arch)
  2427. '(face t fontified t) arch)))
  2428. )))
  2429. (and cat (org-set-local 'org-category cat))
  2430. (and kwds (org-set-local 'org-todo-keywords kwds))
  2431. (and arch (org-set-local 'org-archive-location arch))
  2432. (and int (org-set-local 'org-todo-interpretation int))
  2433. (when tags
  2434. (let (e tgs)
  2435. (while (setq e (pop tags))
  2436. (cond
  2437. ((equal e "{") (push '(:startgroup) tgs))
  2438. ((equal e "}") (push '(:endgroup) tgs))
  2439. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2440. (push (cons (match-string 1 e)
  2441. (string-to-char (match-string 2 e)))
  2442. tgs))
  2443. (t (push (list e) tgs))))
  2444. (org-set-local 'org-tag-alist nil)
  2445. (while (setq e (pop tgs))
  2446. (or (and (stringp (car e))
  2447. (assoc (car e) org-tag-alist))
  2448. (push e org-tag-alist))))))
  2449. ;; Compute the regular expressions and other local variables
  2450. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2451. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2452. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2453. (length org-scheduled-string)))
  2454. org-done-string
  2455. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2456. org-todo-regexp
  2457. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2458. "\\|") "\\)\\>")
  2459. org-not-done-regexp
  2460. (concat "\\<\\("
  2461. (mapconcat 'regexp-quote
  2462. (nreverse (cdr (reverse org-todo-keywords)))
  2463. "\\|")
  2464. "\\)\\>")
  2465. org-todo-line-regexp
  2466. (concat "^\\(\\*+\\)[ \t]*\\("
  2467. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2468. "\\)? *\\(.*\\)")
  2469. org-nl-done-regexp
  2470. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2471. org-todo-line-tags-regexp
  2472. (concat "^\\(\\*+\\)[ \t]*\\("
  2473. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2474. "\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2475. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2476. org-deadline-regexp (concat "\\<" org-deadline-string)
  2477. org-deadline-time-regexp
  2478. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2479. org-deadline-line-regexp
  2480. (concat "\\<\\(" org-deadline-string "\\).*")
  2481. org-scheduled-regexp
  2482. (concat "\\<" org-scheduled-string)
  2483. org-scheduled-time-regexp
  2484. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2485. org-closed-time-regexp
  2486. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2487. org-keyword-time-regexp
  2488. (concat "\\<\\(" org-scheduled-string
  2489. "\\|" org-deadline-string
  2490. "\\|" org-closed-string
  2491. "\\|" org-clock-string "\\)"
  2492. " *[[<]\\([^]>]+\\)[]>]")
  2493. org-maybe-keyword-time-regexp
  2494. (concat "\\(\\<\\(" org-scheduled-string
  2495. "\\|" org-deadline-string
  2496. "\\|" org-closed-string
  2497. "\\|" org-clock-string "\\)\\)?"
  2498. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2499. (org-set-font-lock-defaults)))
  2500. ;; Tell the compiler about dynamically scoped variables,
  2501. ;; and variables from other packages
  2502. (defvar calc-embedded-close-formula) ; defined by the calc package
  2503. (defvar calc-embedded-open-formula) ; defined by the calc package
  2504. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2505. (defvar zmacs-regions) ; XEmacs regions
  2506. (defvar original-date) ; dynamically scoped in calendar
  2507. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2508. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2509. (defvar org-html-entities) ; defined later in this file
  2510. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2511. (defvar org-time-was-given) ; dynamically scoped parameter
  2512. (defvar org-ts-what) ; dynamically scoped parameter
  2513. (defvar org-current-export-file) ; dynamically scoped parameter
  2514. (defvar org-current-export-dir) ; dynamically scoped parameter
  2515. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2516. (defvar timecnt) ; dynamically scoped parameter
  2517. (defvar levels-open) ; dynamically scoped parameter
  2518. (defvar entry) ; dynamically scoped parameter
  2519. (defvar state) ; dynamically scoped into `org-after-todo-state-change-hook'
  2520. (defvar date) ; dynamically scoped parameter
  2521. (defvar description) ; dynamically scoped parameter
  2522. (defvar ans1) ; dynamically scoped parameter
  2523. (defvar ans2) ; dynamically scoped parameter
  2524. (defvar starting-day) ; local variable
  2525. (defvar include-all-loc) ; local variable
  2526. (defvar vm-message-pointer) ; from vm
  2527. (defvar vm-folder-directory) ; from vm
  2528. (defvar gnus-other-frame-object) ; from gnus
  2529. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2530. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2531. (defvar gnus-group-name) ; from gnus
  2532. (defvar gnus-article-current) ; from gnus
  2533. (defvar w3m-current-url) ; from w3m
  2534. (defvar w3m-current-title) ; from w3m
  2535. (defvar mh-progs) ; from MH-E
  2536. (defvar mh-current-folder) ; from MH-E
  2537. (defvar mh-show-folder-buffer) ; from MH-E
  2538. (defvar mh-index-folder) ; from MH-E
  2539. (defvar mh-searcher) ; from MH-E
  2540. (defvar org-selected-point) ; dynamically scoped parameter
  2541. (defvar calendar-mode-map) ; from calendar.el
  2542. (defvar last-arg) ; local variable
  2543. (defvar remember-save-after-remembering) ; from remember.el
  2544. (defvar remember-data-file) ; from remember.el
  2545. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2546. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2547. (defvar orgtbl-mode) ; defined later in this file
  2548. (defvar Info-current-file) ; from info.el
  2549. (defvar Info-current-node) ; from info.el
  2550. (defvar texmathp-why) ; from texmathp.el
  2551. (defvar org-latex-regexps)
  2552. ;;; Define the mode
  2553. (defvar org-mode-map
  2554. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2555. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22.")
  2556. (copy-keymap outline-mode-map))
  2557. "Keymap for Org-mode.")
  2558. (defvar org-struct-menu) ; defined later in this file
  2559. (defvar org-org-menu) ; defined later in this file
  2560. (defvar org-tbl-menu) ; defined later in this file
  2561. ;; We use a before-change function to check if a table might need
  2562. ;; an update.
  2563. (defvar org-table-may-need-update t
  2564. "Indicates that a table might need an update.
  2565. This variable is set by `org-before-change-function'.
  2566. `org-table-align' sets it back to nil.")
  2567. (defvar org-mode-hook nil)
  2568. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2569. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2570. ;;;###autoload
  2571. (define-derived-mode org-mode outline-mode "Org"
  2572. "Outline-based notes management and organizer, alias
  2573. \"Carsten's outline-mode for keeping track of everything.\"
  2574. Org-mode develops organizational tasks around a NOTES file which
  2575. contains information about projects as plain text. Org-mode is
  2576. implemented on top of outline-mode, which is ideal to keep the content
  2577. of large files well structured. It supports ToDo items, deadlines and
  2578. time stamps, which magically appear in the diary listing of the Emacs
  2579. calendar. Tables are easily created with a built-in table editor.
  2580. Plain text URL-like links connect to websites, emails (VM), Usenet
  2581. messages (Gnus), BBDB entries, and any files related to the project.
  2582. For printing and sharing of notes, an Org-mode file (or a part of it)
  2583. can be exported as a structured ASCII or HTML file.
  2584. The following commands are available:
  2585. \\{org-mode-map}"
  2586. ;; Get rid of Outline menus, they are not needed
  2587. ;; Need to do this here because define-derived-mode sets up
  2588. ;; the keymap so late.
  2589. (if (featurep 'xemacs)
  2590. (progn
  2591. ;; Assume this is Greg's port, it used easymenu
  2592. (easy-menu-remove outline-mode-menu-heading)
  2593. (easy-menu-remove outline-mode-menu-show)
  2594. (easy-menu-remove outline-mode-menu-hide))
  2595. (define-key org-mode-map [menu-bar headings] 'undefined)
  2596. (define-key org-mode-map [menu-bar hide] 'undefined)
  2597. (define-key org-mode-map [menu-bar show] 'undefined))
  2598. (easy-menu-add org-org-menu)
  2599. (easy-menu-add org-tbl-menu)
  2600. (org-install-agenda-files-menu)
  2601. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2602. (org-add-to-invisibility-spec '(org-cwidth))
  2603. (when (featurep 'xemacs)
  2604. (org-set-local 'line-move-ignore-invisible t))
  2605. (setq outline-regexp "\\*+")
  2606. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2607. (setq outline-level 'org-outline-level)
  2608. (when (and org-ellipsis (stringp org-ellipsis))
  2609. (unless org-display-table
  2610. (setq org-display-table (make-display-table)))
  2611. (set-display-table-slot org-display-table
  2612. 4 (string-to-vector org-ellipsis))
  2613. (setq buffer-display-table org-display-table))
  2614. (org-set-regexps-and-options)
  2615. ;; Calc embedded
  2616. (org-set-local 'calc-embedded-open-mode "# ")
  2617. (modify-syntax-entry ?# "<")
  2618. (if org-startup-truncated (setq truncate-lines t))
  2619. (org-set-local 'font-lock-unfontify-region-function
  2620. 'org-unfontify-region)
  2621. ;; Activate before-change-function
  2622. (org-set-local 'org-table-may-need-update t)
  2623. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2624. 'local)
  2625. ;; Check for running clock before killing a buffer
  2626. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2627. ;; Paragraphs and auto-filling
  2628. (org-set-autofill-regexps)
  2629. (org-update-radio-target-regexp)
  2630. (if (and org-insert-mode-line-in-empty-file
  2631. (interactive-p)
  2632. (= (point-min) (point-max)))
  2633. (insert " -*- mode: org -*-\n\n"))
  2634. (unless org-inhibit-startup
  2635. (when org-startup-align-all-tables
  2636. (let ((bmp (buffer-modified-p)))
  2637. (org-table-map-tables 'org-table-align)
  2638. (set-buffer-modified-p bmp)))
  2639. (if org-startup-with-deadline-check
  2640. (call-interactively 'org-check-deadlines)
  2641. (cond
  2642. ((eq org-startup-folded t)
  2643. (org-cycle '(4)))
  2644. ((eq org-startup-folded 'content)
  2645. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2646. (org-cycle '(4)) (org-cycle '(4))))))))
  2647. (defsubst org-call-with-arg (command arg)
  2648. "Call COMMAND interactively, but pretend prefix are was ARG."
  2649. (let ((current-prefix-arg arg)) (call-interactively command)))
  2650. (defsubst org-current-line (&optional pos)
  2651. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2652. (defun org-current-time ()
  2653. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2654. (if (> org-time-stamp-rounding-minutes 0)
  2655. (let ((r org-time-stamp-rounding-minutes)
  2656. (time (decode-time)))
  2657. (apply 'encode-time
  2658. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2659. (nthcdr 2 time))))
  2660. (current-time)))
  2661. (defun org-add-props (string plist &rest props)
  2662. "Add text properties to entire string, from beginning to end.
  2663. PLIST may be a list of properties, PROPS are individual properties and values
  2664. that will be added to PLIST. Returns the string that was modified."
  2665. (add-text-properties
  2666. 0 (length string) (if props (append plist props) plist) string)
  2667. string)
  2668. (put 'org-add-props 'lisp-indent-function 2)
  2669. ;;; Font-Lock stuff
  2670. (defvar org-mouse-map (make-sparse-keymap))
  2671. (define-key org-mouse-map
  2672. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2673. (define-key org-mouse-map
  2674. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2675. (when org-mouse-1-follows-link
  2676. (define-key org-mouse-map [follow-link] 'mouse-face))
  2677. (when org-tab-follows-link
  2678. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2679. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2680. (when org-return-follows-link
  2681. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2682. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2683. (require 'font-lock)
  2684. (defconst org-non-link-chars "]\t\n\r<>")
  2685. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2686. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2687. (defconst org-link-re-with-space
  2688. (concat
  2689. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2690. "\\([^" org-non-link-chars " ]"
  2691. "[^" org-non-link-chars "]*"
  2692. "[^" org-non-link-chars " ]\\)>?")
  2693. "Matches a link with spaces, optional angular brackets around it.")
  2694. (defconst org-link-re-with-space2
  2695. (concat
  2696. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2697. "\\([^" org-non-link-chars " ]"
  2698. "[^]\t\n\r]*"
  2699. "[^" org-non-link-chars " ]\\)>?")
  2700. "Matches a link with spaces, optional angular brackets around it.")
  2701. (defconst org-angle-link-re
  2702. (concat
  2703. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2704. "\\([^" org-non-link-chars " ]"
  2705. "[^" org-non-link-chars "]*"
  2706. "\\)>")
  2707. "Matches link with angular brackets, spaces are allowed.")
  2708. (defconst org-plain-link-re
  2709. (concat
  2710. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2711. "\\([^]\t\n\r<>,;() ]+\\)")
  2712. "Matches plain link, without spaces.")
  2713. (defconst org-bracket-link-regexp
  2714. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2715. "Matches a link in double brackets.")
  2716. (defconst org-bracket-link-analytic-regexp
  2717. (concat
  2718. "\\[\\["
  2719. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2720. "\\([^]]+\\)"
  2721. "\\]"
  2722. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2723. "\\]"))
  2724. ; 1: http:
  2725. ; 2: http
  2726. ; 3: path
  2727. ; 4: [desc]
  2728. ; 5: desc
  2729. (defconst org-ts-lengths
  2730. (cons (length (format-time-string (car org-time-stamp-formats)))
  2731. (length (format-time-string (cdr org-time-stamp-formats))))
  2732. "This holds the lengths of the two different time formats.")
  2733. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2734. "Regular expression for fast time stamp matching.")
  2735. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2736. "Regular expression for fast time stamp matching.")
  2737. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^]0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2738. "Regular expression matching time strings for analysis.")
  2739. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2740. "Regular expression matching time stamps, with groups.")
  2741. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[]>]")
  2742. "Regular expression matching time stamps (also [..]), with groups.")
  2743. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2744. "Regular expression matching a time stamp range.")
  2745. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2746. org-ts-regexp "\\)?")
  2747. "Regular expression matching a time stamp or time stamp range.")
  2748. (defvar org-�§emph-face nil)
  2749. (defun org-do-emphasis-faces (limit)
  2750. "Run through the buffer and add overlays to links."
  2751. (if (re-search-forward org-emph-re limit t)
  2752. (progn
  2753. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2754. 'face
  2755. (nth 1 (assoc (match-string 3)
  2756. org-emphasis-alist)))
  2757. (add-text-properties (match-beginning 2) (match-end 2)
  2758. '(font-lock-multiline t))
  2759. (backward-char 1)
  2760. t)))
  2761. (defun org-activate-plain-links (limit)
  2762. "Run through the buffer and add overlays to links."
  2763. (if (re-search-forward org-plain-link-re limit t)
  2764. (progn
  2765. (add-text-properties (match-beginning 0) (match-end 0)
  2766. (list 'mouse-face 'highlight
  2767. 'keymap org-mouse-map
  2768. ))
  2769. t)))
  2770. (defun org-activate-angle-links (limit)
  2771. "Run through the buffer and add overlays to links."
  2772. (if (re-search-forward org-angle-link-re limit t)
  2773. (progn
  2774. (add-text-properties (match-beginning 0) (match-end 0)
  2775. (list 'mouse-face 'highlight
  2776. 'keymap org-mouse-map
  2777. ))
  2778. t)))
  2779. (defun org-activate-bracket-links (limit)
  2780. "Run through the buffer and add overlays to bracketed links."
  2781. (if (re-search-forward org-bracket-link-regexp limit t)
  2782. (let* ((help (concat "LINK: "
  2783. (org-match-string-no-properties 1)))
  2784. ;; FIXME: above we should remove the escapes.
  2785. ;; but that requires another match, protecting match data,
  2786. ;; a lot of overhead for font-lock.
  2787. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2788. 'keymap org-mouse-map 'mouse-face 'highlight
  2789. 'help-echo help))
  2790. (vp (list 'rear-nonsticky t
  2791. 'keymap org-mouse-map 'mouse-face 'highlight
  2792. 'help-echo help)))
  2793. ;; We need to remove the invisible property here. Table narrowing
  2794. ;; may have made some of this invisible.
  2795. (remove-text-properties (match-beginning 0) (match-end 0)
  2796. '(invisible nil))
  2797. (if (match-end 3)
  2798. (progn
  2799. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2800. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2801. (add-text-properties (match-end 3) (match-end 0) ip))
  2802. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2803. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2804. (add-text-properties (match-end 1) (match-end 0) ip))
  2805. t)))
  2806. (defun org-activate-dates (limit)
  2807. "Run through the buffer and add overlays to dates."
  2808. (if (re-search-forward org-tsr-regexp limit t)
  2809. (progn
  2810. (add-text-properties (match-beginning 0) (match-end 0)
  2811. (list 'mouse-face 'highlight
  2812. 'keymap org-mouse-map))
  2813. t)))
  2814. (defvar org-target-link-regexp nil
  2815. "Regular expression matching radio targets in plain text.")
  2816. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2817. "Regular expression matching a link target.")
  2818. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2819. "Regular expression matching a link target.")
  2820. (defun org-activate-target-links (limit)
  2821. "Run through the buffer and add overlays to target matches."
  2822. (when org-target-link-regexp
  2823. (let ((case-fold-search t))
  2824. (if (re-search-forward org-target-link-regexp limit t)
  2825. (progn
  2826. (add-text-properties (match-beginning 0) (match-end 0)
  2827. (list 'mouse-face 'highlight
  2828. 'keymap org-mouse-map
  2829. 'help-echo "Radio target link"
  2830. 'org-linked-text t))
  2831. t)))))
  2832. (defun org-update-radio-target-regexp ()
  2833. "Find all radio targets in this file and update the regular expression."
  2834. (interactive)
  2835. (when (memq 'radio org-activate-links)
  2836. (setq org-target-link-regexp
  2837. (org-make-target-link-regexp (org-all-targets 'radio)))
  2838. (org-restart-font-lock)))
  2839. (defun org-hide-wide-columns (limit)
  2840. (let (s e)
  2841. (setq s (text-property-any (point) (or limit (point-max))
  2842. 'org-cwidth t))
  2843. (when s
  2844. (setq e (next-single-property-change s 'org-cwidth))
  2845. (add-text-properties s e '(invisible org-cwidth intangible t))
  2846. (goto-char e)
  2847. t)))
  2848. (defun org-restart-font-lock ()
  2849. "Restart font-lock-mode, to force refontification."
  2850. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2851. (font-lock-mode -1)
  2852. (font-lock-mode 1)))
  2853. (defun org-all-targets (&optional radio)
  2854. "Return a list of all targets in this file.
  2855. With optional argument RADIO, only find radio targets."
  2856. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2857. rtn)
  2858. (save-excursion
  2859. (goto-char (point-min))
  2860. (while (re-search-forward re nil t)
  2861. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2862. rtn)))
  2863. (defun org-make-target-link-regexp (targets)
  2864. "Make regular expression matching all strings in TARGETS.
  2865. The regular expression finds the targets also if there is a line break
  2866. between words."
  2867. (and targets
  2868. (concat
  2869. "\\<\\("
  2870. (mapconcat
  2871. (lambda (x)
  2872. (while (string-match " +" x)
  2873. (setq x (replace-match "\\s-+" t t x)))
  2874. x)
  2875. targets
  2876. "\\|")
  2877. "\\)\\>")))
  2878. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2879. "Matches CamelCase words, possibly with a star before it.")
  2880. (defun org-activate-camels (limit)
  2881. "Run through the buffer and add overlays to dates."
  2882. (if (re-search-forward org-camel-regexp limit t)
  2883. (progn
  2884. (add-text-properties (match-beginning 0) (match-end 0)
  2885. (list 'mouse-face 'highlight
  2886. 'keymap org-mouse-map))
  2887. t)))
  2888. (defun org-activate-tags (limit)
  2889. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2890. (progn
  2891. (add-text-properties (match-beginning 1) (match-end 1)
  2892. (list 'mouse-face 'highlight
  2893. 'keymap org-mouse-map))
  2894. t)))
  2895. (defun org-font-lock-level ()
  2896. (save-excursion
  2897. (org-back-to-heading t)
  2898. (- (match-end 0) (match-beginning 0))))
  2899. (defun org-outline-level ()
  2900. (save-excursion
  2901. (looking-at outline-regexp)
  2902. (if (match-beginning 1)
  2903. (+ (org-get-string-indentation (match-string 1)) 1000)
  2904. (- (match-end 0) (match-beginning 0)))))
  2905. (defvar org-font-lock-keywords nil)
  2906. (defun org-set-font-lock-defaults ()
  2907. (let* ((em org-fontify-emphasized-text)
  2908. (lk org-activate-links)
  2909. (org-font-lock-extra-keywords
  2910. ;; Headlines
  2911. (list
  2912. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2913. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2914. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2915. (1 'org-table))
  2916. ;; Links
  2917. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2918. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2919. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2920. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2921. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2922. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2923. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2924. (if org-table-limit-column-width
  2925. '(org-hide-wide-columns (0 nil append)))
  2926. ;; TODO lines
  2927. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2928. '(1 'org-todo t))
  2929. ;; Priorities
  2930. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2931. ;; Special keywords
  2932. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2933. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2934. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2935. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  2936. ;; Emphasis
  2937. (if em '(org-do-emphasis-faces))
  2938. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2939. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2940. 2 'bold prepend)
  2941. (if org-provide-checkbox-statistics
  2942. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  2943. (0 (org-get-checkbox-statistics-face) t)))
  2944. ;; COMMENT
  2945. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2946. "\\|" org-quote-string "\\)\\>")
  2947. '(1 'org-special-keyword t))
  2948. '("^#.*" (0 'font-lock-comment-face t))
  2949. ;; DONE
  2950. (if org-fontify-done-headline
  2951. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2952. '(1 'org-done t) '(2 'org-headline-done t))
  2953. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2954. '(1 'org-done t)))
  2955. ;; Table stuff
  2956. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2957. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2958. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2959. (if org-format-transports-properties-p
  2960. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2961. '("^\\*+ \\(.*:ARCHIVE:.*\\)" (1 'org-archived prepend))
  2962. )))
  2963. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2964. ;; Now set the full font-lock-keywords
  2965. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  2966. (org-set-local 'font-lock-defaults
  2967. '(org-font-lock-keywords t nil nil backward-paragraph))
  2968. (kill-local-variable 'font-lock-keywords) nil))
  2969. (defvar org-m nil)
  2970. (defvar org-l nil)
  2971. (defvar org-f nil)
  2972. (defun org-get-level-face (n)
  2973. "Get the right face for match N in font-lock matching of healdines."
  2974. (setq org-l (- (match-end 2) (match-beginning 1)))
  2975. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2976. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2977. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2978. (cond
  2979. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2980. ((eq n 2) org-f)
  2981. (t (if org-level-color-stars-only nil org-f))))
  2982. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2983. "Remove fontification and activation overlays from links."
  2984. (font-lock-default-unfontify-region beg end)
  2985. (let* ((buffer-undo-list t)
  2986. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2987. (inhibit-modification-hooks t)
  2988. deactivate-mark buffer-file-name buffer-file-truename)
  2989. (remove-text-properties beg end
  2990. '(mouse-face nil keymap nil org-linked-text nil
  2991. invisible nil intangible nil))))
  2992. ;;; Visibility cycling
  2993. (defvar org-cycle-global-status nil)
  2994. (make-variable-buffer-local 'org-cycle-global-status)
  2995. (defvar org-cycle-subtree-status nil)
  2996. (make-variable-buffer-local 'org-cycle-subtree-status)
  2997. ;;;###autoload
  2998. (defun org-cycle (&optional arg)
  2999. "Visibility cycling for Org-mode.
  3000. - When this function is called with a prefix argument, rotate the entire
  3001. buffer through 3 states (global cycling)
  3002. 1. OVERVIEW: Show only top-level headlines.
  3003. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3004. 3. SHOW ALL: Show everything.
  3005. - When point is at the beginning of a headline, rotate the subtree started
  3006. by this line through 3 different states (local cycling)
  3007. 1. FOLDED: Only the main headline is shown.
  3008. 2. CHILDREN: The main headline and the direct children are shown.
  3009. From this state, you can move to one of the children
  3010. and zoom in further.
  3011. 3. SUBTREE: Show the entire subtree, including body text.
  3012. - When there is a numeric prefix, go up to a heading with level ARG, do
  3013. a `show-subtree' and return to the previous cursor position. If ARG
  3014. is negative, go up that many levels.
  3015. - When point is not at the beginning of a headline, execute
  3016. `indent-relative', like TAB normally does. See the option
  3017. `org-cycle-emulate-tab' for details.
  3018. - Special case: if point is the the beginning of the buffer and there is
  3019. no headline in line 1, this function will act as if called with prefix arg."
  3020. (interactive "P")
  3021. (let* ((outline-regexp
  3022. (if org-cycle-include-plain-lists
  3023. "\\(?:\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3024. outline-regexp))
  3025. (bob-special (and org-cycle-global-at-bob (bobp)
  3026. (not (looking-at outline-regexp))))
  3027. (org-cycle-hook
  3028. (if bob-special
  3029. (delq 'org-optimize-window-after-visibility-change
  3030. (copy-sequence org-cycle-hook))
  3031. org-cycle-hook))
  3032. (pos (point)))
  3033. (if (or bob-special (equal arg '(4)))
  3034. ;; special case: use global cycling
  3035. (setq arg t))
  3036. (cond
  3037. ((org-at-table-p 'any)
  3038. ;; Enter the table or move to the next field in the table
  3039. (or (org-table-recognize-table.el)
  3040. (progn
  3041. (if arg (org-table-edit-field t)
  3042. (org-table-justify-field-maybe)
  3043. (call-interactively 'org-table-next-field)))))
  3044. ((eq arg t) ;; Global cycling
  3045. (cond
  3046. ((and (eq last-command this-command)
  3047. (eq org-cycle-global-status 'overview))
  3048. ;; We just created the overview - now do table of contents
  3049. ;; This can be slow in very large buffers, so indicate action
  3050. (message "CONTENTS...")
  3051. (org-content)
  3052. (message "CONTENTS...done")
  3053. (setq org-cycle-global-status 'contents)
  3054. (run-hook-with-args 'org-cycle-hook 'contents))
  3055. ((and (eq last-command this-command)
  3056. (eq org-cycle-global-status 'contents))
  3057. ;; We just showed the table of contents - now show everything
  3058. (show-all)
  3059. (message "SHOW ALL")
  3060. (setq org-cycle-global-status 'all)
  3061. (run-hook-with-args 'org-cycle-hook 'all))
  3062. (t
  3063. ;; Default action: go to overview
  3064. (org-overview)
  3065. (message "OVERVIEW")
  3066. (setq org-cycle-global-status 'overview)
  3067. (run-hook-with-args 'org-cycle-hook 'overview))))
  3068. ((integerp arg)
  3069. ;; Show-subtree, ARG levels up from here.
  3070. (save-excursion
  3071. (org-back-to-heading)
  3072. (outline-up-heading (if (< arg 0) (- arg)
  3073. (- (funcall outline-level) arg)))
  3074. (org-show-subtree)))
  3075. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3076. ;; At a heading: rotate between three different views
  3077. (org-back-to-heading)
  3078. (let ((goal-column 0) eoh eol eos)
  3079. ;; First, some boundaries
  3080. (save-excursion
  3081. (org-back-to-heading)
  3082. (save-excursion
  3083. (beginning-of-line 2)
  3084. (while (and (not (eobp)) ;; this is like `next-line'
  3085. (get-char-property (1- (point)) 'invisible))
  3086. (beginning-of-line 2)) (setq eol (point)))
  3087. (outline-end-of-heading) (setq eoh (point))
  3088. (org-end-of-subtree t) (setq eos (point))
  3089. (outline-next-heading))
  3090. ;; Find out what to do next and set `this-command'
  3091. (cond
  3092. ((and (= eos eoh)
  3093. ;; Nothing is hidden behind this heading
  3094. (message "EMPTY ENTRY")
  3095. (setq org-cycle-subtree-status nil)))
  3096. ((>= eol eos)
  3097. ;; Entire subtree is hidden in one line: open it
  3098. (org-show-entry)
  3099. (show-children)
  3100. (message "CHILDREN")
  3101. (setq org-cycle-subtree-status 'children)
  3102. (run-hook-with-args 'org-cycle-hook 'children))
  3103. ((and (eq last-command this-command)
  3104. (eq org-cycle-subtree-status 'children))
  3105. ;; We just showed the children, now show everything.
  3106. (org-show-subtree)
  3107. (message "SUBTREE")
  3108. (setq org-cycle-subtree-status 'subtree)
  3109. (run-hook-with-args 'org-cycle-hook 'subtree))
  3110. (t
  3111. ;; Default action: hide the subtree.
  3112. (hide-subtree)
  3113. (message "FOLDED")
  3114. (setq org-cycle-subtree-status 'folded)
  3115. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3116. ;; TAB emulation
  3117. (buffer-read-only (org-back-to-heading))
  3118. ((org-try-cdlatex-tab))
  3119. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3120. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3121. (or (and (eq org-cycle-emulate-tab 'white)
  3122. (= (match-end 0) (point-at-eol)))
  3123. (and (eq org-cycle-emulate-tab 'whitestart)
  3124. (>= (match-end 0) pos))))
  3125. t
  3126. (eq org-cycle-emulate-tab t))
  3127. (if (and (looking-at "[ \n\r\t]")
  3128. (string-match "^[ \t]*$" (buffer-substring
  3129. (point-at-bol) (point))))
  3130. (progn
  3131. (beginning-of-line 1)
  3132. (and (looking-at "[ \t]+") (replace-match ""))))
  3133. (indent-relative))
  3134. (t (save-excursion
  3135. (org-back-to-heading)
  3136. (org-cycle))))))
  3137. ;;;###autoload
  3138. (defun org-global-cycle (&optional arg)
  3139. "Cycle the global visibility. For details see `org-cycle'."
  3140. (interactive "P")
  3141. (if (integerp arg)
  3142. (progn
  3143. (show-all)
  3144. (hide-sublevels arg)
  3145. (setq org-cycle-global-status 'contents))
  3146. (org-cycle '(4))))
  3147. (defun org-overview ()
  3148. "Switch to overview mode, shoing only top-level headlines.
  3149. Really, this shows all headlines with level equal or greater than the level
  3150. of the first headline in the buffer. This is important, because if the
  3151. first headline is not level one, then (hide-sublevels 1) gives confusing
  3152. results."
  3153. (interactive)
  3154. (hide-sublevels (save-excursion
  3155. (goto-char (point-min))
  3156. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3157. (progn
  3158. (goto-char (match-beginning 0))
  3159. (funcall outline-level))
  3160. 1))))
  3161. ;; FIXME: allow an argument to give a limiting level for this.
  3162. (defun org-content ()
  3163. "Show all headlines in the buffer, like a table of contents"
  3164. (interactive)
  3165. (save-excursion
  3166. ;; Visit all headings and show their offspring
  3167. (goto-char (point-max))
  3168. (catch 'exit
  3169. (while (and (progn (condition-case nil
  3170. (outline-previous-visible-heading 1)
  3171. (error (goto-char (point-min))))
  3172. t)
  3173. (looking-at outline-regexp))
  3174. (show-branches)
  3175. (if (bobp) (throw 'exit nil))))))
  3176. (defun org-optimize-window-after-visibility-change (state)
  3177. "Adjust the window after a change in outline visibility.
  3178. This function is the default value of the hook `org-cycle-hook'."
  3179. (when (get-buffer-window (current-buffer))
  3180. (cond
  3181. ((eq state 'overview) (org-first-headline-recenter 1))
  3182. ((eq state 'content) nil)
  3183. ((eq state 'all) nil)
  3184. ((eq state 'folded) nil)
  3185. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3186. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3187. (defun org-subtree-end-visible-p ()
  3188. "Is the end of the current subtree visible?"
  3189. (pos-visible-in-window-p
  3190. (save-excursion (org-end-of-subtree t) (point))))
  3191. (defun org-first-headline-recenter (&optional N)
  3192. "Move cursor to the first headline and recenter the headline.
  3193. Optional argument N means, put the headline into the Nth line of the window."
  3194. (goto-char (point-min))
  3195. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3196. (beginning-of-line)
  3197. (recenter (prefix-numeric-value N))))
  3198. (defvar org-goto-window-configuration nil)
  3199. (defvar org-goto-marker nil)
  3200. (defvar org-goto-map (make-sparse-keymap))
  3201. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  3202. (while (setq cmd (pop cmds))
  3203. (substitute-key-definition cmd cmd org-goto-map global-map)))
  3204. (define-key org-goto-map "\C-m" 'org-goto-ret)
  3205. (define-key org-goto-map [(left)] 'org-goto-left)
  3206. (define-key org-goto-map [(right)] 'org-goto-right)
  3207. (define-key org-goto-map [(?q)] 'org-goto-quit)
  3208. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  3209. (define-key org-goto-map "\C-i" 'org-cycle)
  3210. (define-key org-goto-map [(tab)] 'org-cycle)
  3211. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  3212. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  3213. (define-key org-goto-map "n" 'outline-next-visible-heading)
  3214. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  3215. (define-key org-goto-map "f" 'outline-forward-same-level)
  3216. (define-key org-goto-map "b" 'outline-backward-same-level)
  3217. (define-key org-goto-map "u" 'outline-up-heading)
  3218. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  3219. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  3220. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  3221. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  3222. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  3223. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  3224. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  3225. (defconst org-goto-help
  3226. "Select a location to jump to, press RET
  3227. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  3228. (defun org-goto ()
  3229. "Go to a different location of the document, keeping current visibility.
  3230. When you want to go to a different location in a document, the fastest way
  3231. is often to fold the entire buffer and then dive into the tree. This
  3232. method has the disadvantage, that the previous location will be folded,
  3233. which may not be what you want.
  3234. This command works around this by showing a copy of the current buffer in
  3235. overview mode. You can dive into the tree in that copy, to find the
  3236. location you want to reach. When pressing RET, the command returns to the
  3237. original buffer in which the visibility is still unchanged. It then jumps
  3238. to the new location, making it and the headline hierarchy above it visible."
  3239. (interactive)
  3240. (let* ((org-goto-start-pos (point))
  3241. (selected-point
  3242. (org-get-location (current-buffer) org-goto-help)))
  3243. (if selected-point
  3244. (progn
  3245. (org-mark-ring-push org-goto-start-pos)
  3246. (goto-char selected-point)
  3247. (if (or (org-invisible-p) (org-invisible-p2))
  3248. (org-show-hierarchy-above)))
  3249. (error "Quit"))))
  3250. (defun org-get-location (buf help)
  3251. "Let the user select a location in the Org-mode buffer BUF.
  3252. This function uses a recursive edit. It returns the selected position
  3253. or nil."
  3254. (let (org-selected-point)
  3255. (save-excursion
  3256. (save-window-excursion
  3257. (delete-other-windows)
  3258. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3259. (with-output-to-temp-buffer "*Help*"
  3260. (princ help))
  3261. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3262. (setq buffer-read-only nil)
  3263. (erase-buffer)
  3264. (insert-buffer-substring buf)
  3265. (let ((org-startup-truncated t)
  3266. (org-startup-folded t)
  3267. (org-startup-align-all-tables nil)
  3268. (org-startup-with-deadline-check nil))
  3269. (org-mode))
  3270. (setq buffer-read-only t)
  3271. (if (boundp 'org-goto-start-pos)
  3272. (goto-char org-goto-start-pos)
  3273. (goto-char (point-min)))
  3274. (org-beginning-of-line)
  3275. (message "Select location and press RET")
  3276. ;; now we make sure that during selection, ony very few keys work
  3277. ;; and that it is impossible to switch to another window.
  3278. (let ((gm (current-global-map))
  3279. (overriding-local-map org-goto-map))
  3280. (unwind-protect
  3281. (progn
  3282. (use-global-map org-goto-map)
  3283. (recursive-edit))
  3284. (use-global-map gm)))))
  3285. (kill-buffer "*org-goto*")
  3286. org-selected-point))
  3287. (defun org-goto-ret (&optional arg)
  3288. "Finish `org-goto' by going to the new location."
  3289. (interactive "P")
  3290. (setq org-selected-point (point)
  3291. current-prefix-arg arg)
  3292. (throw 'exit nil))
  3293. (defun org-goto-left ()
  3294. "Finish `org-goto' by going to the new location."
  3295. (interactive)
  3296. (if (org-on-heading-p)
  3297. (progn
  3298. (beginning-of-line 1)
  3299. (setq org-selected-point (point)
  3300. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3301. (throw 'exit nil))
  3302. (error "Not on a heading")))
  3303. (defun org-goto-right ()
  3304. "Finish `org-goto' by going to the new location."
  3305. (interactive)
  3306. (if (org-on-heading-p)
  3307. (progn
  3308. (outline-end-of-subtree)
  3309. (or (eobp) (forward-char 1))
  3310. (setq org-selected-point (point)
  3311. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3312. (throw 'exit nil))
  3313. (error "Not on a heading")))
  3314. (defun org-goto-quit ()
  3315. "Finish `org-goto' without cursor motion."
  3316. (interactive)
  3317. (setq org-selected-point nil)
  3318. (throw 'exit nil))
  3319. ;;; Promotion, Demotion, Inserting new headlines
  3320. (defvar org-ignore-region nil
  3321. "To temporarily disable the active region.")
  3322. (defun org-insert-heading (&optional force-heading)
  3323. "Insert a new heading or item with same depth at point.
  3324. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3325. If point is at the beginning of a headline, insert a sibling before the
  3326. current headline. If point is in the middle of a headline, split the headline
  3327. at that position and make the rest of the headline part of the sibling below
  3328. the current headline."
  3329. (interactive "P")
  3330. (if (= (buffer-size) 0)
  3331. (insert "\n* ")
  3332. (when (or force-heading (not (org-insert-item)))
  3333. (let* ((head (save-excursion
  3334. (condition-case nil
  3335. (progn
  3336. (org-back-to-heading)
  3337. (match-string 0))
  3338. (error "*"))))
  3339. pos)
  3340. (cond
  3341. ((and (org-on-heading-p) (bolp)
  3342. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3343. (open-line 1))
  3344. ((and (bolp) (save-excursion
  3345. (backward-char 1) (not (org-invisible-p))))
  3346. nil)
  3347. (t (newline)))
  3348. (insert head) (just-one-space)
  3349. (setq pos (point))
  3350. (end-of-line 1)
  3351. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3352. (run-hooks 'org-insert-heading-hook)))))
  3353. (defun org-in-item-p ()
  3354. "It the cursor inside a plain list item.
  3355. Does not have to be the first line."
  3356. (save-excursion
  3357. (condition-case nil
  3358. (progn
  3359. (org-beginning-of-item)
  3360. (org-at-item-p)
  3361. t)
  3362. (error nil))))
  3363. (defun org-insert-item (&optional checkbox)
  3364. "Insert a new item at the current level.
  3365. Return t when things worked, nil when we are not in an item."
  3366. (when (save-excursion
  3367. (condition-case nil
  3368. (progn
  3369. (org-beginning-of-item)
  3370. (org-at-item-p)
  3371. t)
  3372. (error nil)))
  3373. (let* ((bul (match-string 0))
  3374. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3375. (match-end 0)))
  3376. pos)
  3377. (cond
  3378. ((and (org-at-item-p) (<= (point) eow))
  3379. ;; before the bullet
  3380. (beginning-of-line 1)
  3381. (open-line 1))
  3382. ((<= (point) eow)
  3383. (beginning-of-line 1))
  3384. (t (newline)))
  3385. (insert bul (if checkbox "[ ]" ""))
  3386. (just-one-space)
  3387. (setq pos (point))
  3388. (end-of-line 1)
  3389. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3390. (org-maybe-renumber-ordered-list)
  3391. (and checkbox (org-update-checkbox-count-maybe))
  3392. t))
  3393. (defun org-insert-todo-heading (arg)
  3394. "Insert a new heading with the same level and TODO state as current heading.
  3395. If the heading has no TODO state, or if the state is DONE, use the first
  3396. state (TODO by default). Also with prefix arg, force first state."
  3397. (interactive "P")
  3398. (when (not (org-insert-item 'checkbox))
  3399. (org-insert-heading)
  3400. (save-excursion
  3401. (org-back-to-heading)
  3402. (outline-previous-heading)
  3403. (looking-at org-todo-line-regexp))
  3404. (if (or arg
  3405. (not (match-beginning 2))
  3406. (equal (match-string 2) org-done-string))
  3407. (insert (car org-todo-keywords) " ")
  3408. (insert (match-string 2) " "))))
  3409. (defun org-promote-subtree ()
  3410. "Promote the entire subtree.
  3411. See also `org-promote'."
  3412. (interactive)
  3413. (save-excursion
  3414. (org-map-tree 'org-promote))
  3415. (org-fix-position-after-promote))
  3416. (defun org-demote-subtree ()
  3417. "Demote the entire subtree. See `org-demote'.
  3418. See also `org-promote'."
  3419. (interactive)
  3420. (save-excursion
  3421. (org-map-tree 'org-demote))
  3422. (org-fix-position-after-promote))
  3423. (defun org-do-promote ()
  3424. "Promote the current heading higher up the tree.
  3425. If the region is active in `transient-mark-mode', promote all headings
  3426. in the region."
  3427. (interactive)
  3428. (save-excursion
  3429. (if (org-region-active-p)
  3430. (org-map-region 'org-promote (region-beginning) (region-end))
  3431. (org-promote)))
  3432. (org-fix-position-after-promote))
  3433. (defun org-do-demote ()
  3434. "Demote the current heading lower down the tree.
  3435. If the region is active in `transient-mark-mode', demote all headings
  3436. in the region."
  3437. (interactive)
  3438. (save-excursion
  3439. (if (org-region-active-p)
  3440. (org-map-region 'org-demote (region-beginning) (region-end))
  3441. (org-demote)))
  3442. (org-fix-position-after-promote))
  3443. (defun org-fix-position-after-promote ()
  3444. "Make sure that after pro/demotion cursor position is right."
  3445. (if (and (equal (char-after) ?\n)
  3446. (save-excursion
  3447. (skip-chars-backward "a-zA-Z0-9_@")
  3448. (looking-at org-todo-regexp)))
  3449. (insert " "))
  3450. (and (equal (char-after) ?\ )
  3451. (equal (char-before) ?*)
  3452. (forward-char 1)))
  3453. (defun org-get-legal-level (level change)
  3454. "Rectify a level change under the influence of `org-odd-levels-only'
  3455. LEVEL is a current level, CHANGE is by how much the level should be
  3456. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3457. even level numbers will become the next higher odd number."
  3458. (if org-odd-levels-only
  3459. (cond ((not change) (1+ (* 2 (/ level 2))))
  3460. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3461. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3462. (max 1 (+ level change))))
  3463. (defun org-promote ()
  3464. "Promote the current heading higher up the tree.
  3465. If the region is active in `transient-mark-mode', promote all headings
  3466. in the region."
  3467. (org-back-to-heading t)
  3468. (let* ((level (save-match-data (funcall outline-level)))
  3469. (up-head (make-string (org-get-legal-level level -1) ?*))
  3470. (diff (abs (- level (length up-head)))))
  3471. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3472. (replace-match up-head nil t)
  3473. ;; Fixup tag positioning
  3474. (and org-auto-align-tags (org-set-tags nil t))
  3475. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  3476. (defun org-demote ()
  3477. "Demote the current heading lower down the tree.
  3478. If the region is active in `transient-mark-mode', demote all headings
  3479. in the region."
  3480. (org-back-to-heading t)
  3481. (let* ((level (save-match-data (funcall outline-level)))
  3482. (down-head (make-string (org-get-legal-level level 1) ?*))
  3483. (diff (abs (- level (length down-head)))))
  3484. (replace-match down-head nil t)
  3485. ;; Fixup tag positioning
  3486. (and org-auto-align-tags (org-set-tags nil t))
  3487. (if org-adapt-indentation (org-fixup-indentation diff))))
  3488. (defun org-map-tree (fun)
  3489. "Call FUN for every heading underneath the current one."
  3490. (org-back-to-heading)
  3491. (let ((level (funcall outline-level)))
  3492. (save-excursion
  3493. (funcall fun)
  3494. (while (and (progn
  3495. (outline-next-heading)
  3496. (> (funcall outline-level) level))
  3497. (not (eobp)))
  3498. (funcall fun)))))
  3499. (defun org-map-region (fun beg end)
  3500. "Call FUN for every heading between BEG and END."
  3501. (let ((org-ignore-region t))
  3502. (save-excursion
  3503. (setq end (copy-marker end))
  3504. (goto-char beg)
  3505. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3506. (< (point) end))
  3507. (funcall fun))
  3508. (while (and (progn
  3509. (outline-next-heading)
  3510. (< (point) end))
  3511. (not (eobp)))
  3512. (funcall fun)))))
  3513. (defun org-fixup-indentation (diff)
  3514. "Change the indentation in the current entry by DIFF
  3515. However, if any line in the current entry has no indentation, or if it
  3516. would end up with no indentation after the change, nothing at all is done."
  3517. (save-excursion
  3518. (let ((end (save-excursion (outline-next-heading)
  3519. (point-marker)))
  3520. (prohibit (if (> diff 0)
  3521. "^\\S-"
  3522. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  3523. col)
  3524. (unless (save-excursion (re-search-forward prohibit end t))
  3525. (while (re-search-forward "^[ \t]+" end t)
  3526. (goto-char (match-end 0))
  3527. (setq col (current-column))
  3528. (if (< diff 0) (replace-match ""))
  3529. (indent-to (+ diff col))))
  3530. (move-marker end nil))))
  3531. ;;; Vertical tree motion, cutting and pasting of subtrees
  3532. (defun org-move-subtree-up (&optional arg)
  3533. "Move the current subtree up past ARG headlines of the same level."
  3534. (interactive "p")
  3535. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3536. (defun org-move-subtree-down (&optional arg)
  3537. "Move the current subtree down past ARG headlines of the same level."
  3538. (interactive "p")
  3539. (setq arg (prefix-numeric-value arg))
  3540. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3541. 'outline-get-last-sibling))
  3542. (ins-point (make-marker))
  3543. (cnt (abs arg))
  3544. beg end txt folded)
  3545. ;; Select the tree
  3546. (org-back-to-heading)
  3547. (setq beg (point))
  3548. (save-match-data
  3549. (save-excursion (outline-end-of-heading)
  3550. (setq folded (org-invisible-p)))
  3551. (outline-end-of-subtree))
  3552. (outline-next-heading)
  3553. (setq end (point))
  3554. ;; Find insertion point, with error handling
  3555. (goto-char beg)
  3556. (while (> cnt 0)
  3557. (or (and (funcall movfunc) (looking-at outline-regexp))
  3558. (progn (goto-char beg)
  3559. (error "Cannot move past superior level or buffer limit")))
  3560. (setq cnt (1- cnt)))
  3561. (if (> arg 0)
  3562. ;; Moving forward - still need to move over subtree
  3563. (progn (outline-end-of-subtree)
  3564. (outline-next-heading)
  3565. (if (not (or (looking-at (concat "^" outline-regexp))
  3566. (bolp)))
  3567. (newline))))
  3568. (move-marker ins-point (point))
  3569. (setq txt (buffer-substring beg end))
  3570. (delete-region beg end)
  3571. (insert txt)
  3572. (goto-char ins-point)
  3573. (if folded (hide-subtree))
  3574. (move-marker ins-point nil)))
  3575. (defvar org-subtree-clip ""
  3576. "Clipboard for cut and paste of subtrees.
  3577. This is actually only a copy of the kill, because we use the normal kill
  3578. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3579. (defvar org-subtree-clip-folded nil
  3580. "Was the last copied subtree folded?
  3581. This is used to fold the tree back after pasting.")
  3582. (defun org-cut-subtree ()
  3583. "Cut the current subtree into the clipboard.
  3584. This is a short-hand for marking the subtree and then cutting it."
  3585. (interactive)
  3586. (org-copy-subtree 'cut))
  3587. (defun org-copy-subtree (&optional cut)
  3588. "Cut the current subtree into the clipboard.
  3589. This is a short-hand for marking the subtree and then copying it.
  3590. If CUT is non nil, actually cut the subtree."
  3591. (interactive)
  3592. (let (beg end folded)
  3593. (org-back-to-heading)
  3594. (setq beg (point))
  3595. (save-match-data
  3596. (save-excursion (outline-end-of-heading)
  3597. (setq folded (org-invisible-p)))
  3598. (outline-end-of-subtree))
  3599. (if (equal (char-after) ?\n) (forward-char 1))
  3600. (setq end (point))
  3601. (goto-char beg)
  3602. (when (> end beg)
  3603. (setq org-subtree-clip-folded folded)
  3604. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3605. (setq org-subtree-clip (current-kill 0))
  3606. (message "%s: Subtree with %d characters"
  3607. (if cut "Cut" "Copied")
  3608. (length org-subtree-clip)))))
  3609. (defun org-paste-subtree (&optional level tree)
  3610. "Paste the clipboard as a subtree, with modification of headline level.
  3611. The entire subtree is promoted or demoted in order to match a new headline
  3612. level. By default, the new level is derived from the visible headings
  3613. before and after the insertion point, and taken to be the inferior headline
  3614. level of the two. So if the previous visible heading is level 3 and the
  3615. next is level 4 (or vice versa), level 4 will be used for insertion.
  3616. This makes sure that the subtree remains an independent subtree and does
  3617. not swallow low level entries.
  3618. You can also force a different level, either by using a numeric prefix
  3619. argument, or by inserting the heading marker by hand. For example, if the
  3620. cursor is after \"*****\", then the tree will be shifted to level 5.
  3621. If you want to insert the tree as is, just use \\[yank].
  3622. If optional TREE is given, use this text instead of the kill ring."
  3623. (interactive "P")
  3624. (unless (org-kill-is-subtree-p tree)
  3625. (error
  3626. (substitute-command-keys
  3627. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3628. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3629. (^re (concat "^\\(" outline-regexp "\\)"))
  3630. (re (concat "\\(" outline-regexp "\\)"))
  3631. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3632. (old-level (if (string-match ^re txt)
  3633. (- (match-end 0) (match-beginning 0))
  3634. -1))
  3635. (force-level (cond (level (prefix-numeric-value level))
  3636. ((string-match
  3637. ^re_ (buffer-substring (point-at-bol) (point)))
  3638. (- (match-end 0) (match-beginning 0)))
  3639. (t nil)))
  3640. (previous-level (save-excursion
  3641. (condition-case nil
  3642. (progn
  3643. (outline-previous-visible-heading 1)
  3644. (if (looking-at re)
  3645. (- (match-end 0) (match-beginning 0))
  3646. 1))
  3647. (error 1))))
  3648. (next-level (save-excursion
  3649. (condition-case nil
  3650. (progn
  3651. (outline-next-visible-heading 1)
  3652. (if (looking-at re)
  3653. (- (match-end 0) (match-beginning 0))
  3654. 1))
  3655. (error 1))))
  3656. (new-level (or force-level (max previous-level next-level)))
  3657. (shift (if (or (= old-level -1)
  3658. (= new-level -1)
  3659. (= old-level new-level))
  3660. 0
  3661. (- new-level old-level)))
  3662. (shift1 shift)
  3663. (delta (if (> shift 0) -1 1))
  3664. (func (if (> shift 0) 'org-demote 'org-promote))
  3665. (org-odd-levels-only nil)
  3666. beg end)
  3667. ;; Remove the forces level indicator
  3668. (if force-level
  3669. (delete-region (point-at-bol) (point)))
  3670. ;; Make sure we start at the beginning of an empty line
  3671. (if (not (bolp)) (insert "\n"))
  3672. (if (not (looking-at "[ \t]*$"))
  3673. (progn (insert "\n") (backward-char 1)))
  3674. ;; Paste
  3675. (setq beg (point))
  3676. (if (string-match "[ \t\r\n]+\\'" txt)
  3677. (setq txt (replace-match "\n" t t txt)))
  3678. (insert txt)
  3679. (setq end (point))
  3680. (if (looking-at "[ \t\r\n]+")
  3681. (replace-match "\n"))
  3682. (goto-char beg)
  3683. ;; Shift if necessary
  3684. (if (= shift 0)
  3685. (message "Pasted at level %d, without shift" new-level)
  3686. (save-restriction
  3687. (narrow-to-region beg end)
  3688. (while (not (= shift 0))
  3689. (org-map-region func (point-min) (point-max))
  3690. (setq shift (+ delta shift)))
  3691. (goto-char (point-min))
  3692. (message "Pasted at level %d, with shift by %d levels"
  3693. new-level shift1)))
  3694. (if (and kill-ring
  3695. (eq org-subtree-clip (current-kill 0))
  3696. org-subtree-clip-folded)
  3697. ;; The tree was folded before it was killed/copied
  3698. (hide-subtree))))
  3699. (defun org-kill-is-subtree-p (&optional txt)
  3700. "Check if the current kill is an outline subtree, or a set of trees.
  3701. Returns nil if kill does not start with a headline, or if the first
  3702. headline level is not the largest headline level in the tree.
  3703. So this will actually accept several entries of equal levels as well,
  3704. which is OK for `org-paste-subtree'.
  3705. If optional TXT is given, check this string instead of the current kill."
  3706. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3707. (start-level (and kill
  3708. (string-match (concat "\\`" outline-regexp) kill)
  3709. (- (match-end 0) (match-beginning 0))))
  3710. (re (concat "^" outline-regexp))
  3711. (start 1))
  3712. (if (not start-level)
  3713. nil ;; does not even start with a heading
  3714. (catch 'exit
  3715. (while (setq start (string-match re kill (1+ start)))
  3716. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3717. (throw 'exit nil)))
  3718. t))))
  3719. (defun org-narrow-to-subtree ()
  3720. "Narrow buffer to the current subtree."
  3721. (interactive)
  3722. (save-excursion
  3723. (narrow-to-region
  3724. (progn (org-back-to-heading) (point))
  3725. (progn (org-end-of-subtree t) (point)))))
  3726. ;;; Plain list items
  3727. (defun org-at-item-p ()
  3728. "Is point in a line starting a hand-formatted item?"
  3729. (let ((llt org-plain-list-ordered-item-terminator))
  3730. (save-excursion
  3731. (goto-char (point-at-bol))
  3732. (looking-at
  3733. (cond
  3734. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3735. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3736. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3737. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3738. (defun org-at-item-checkbox-p ()
  3739. "Is point at a line starting a plain-list item with a checklet?"
  3740. (and (org-at-item-p)
  3741. (save-excursion
  3742. (goto-char (match-end 0))
  3743. (skip-chars-forward " \t")
  3744. (looking-at "\\[[ X]\\]"))))
  3745. (defun org-toggle-checkbox (&optional arg)
  3746. "Toggle the checkbox in the current line."
  3747. (interactive "P")
  3748. (catch 'exit
  3749. (let (beg end status (firstnew 'unknown))
  3750. (cond
  3751. ((org-region-active-p)
  3752. (setq beg (region-beginning) end (region-end)))
  3753. ((org-on-heading-p)
  3754. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  3755. ((org-at-item-checkbox-p)
  3756. (save-excursion
  3757. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))
  3758. (throw 'exit t))
  3759. (t (error "Not at a checkbox or heading, and no active region")))
  3760. (save-excursion
  3761. (goto-char beg)
  3762. (while (< (point) end)
  3763. (when (org-at-item-checkbox-p)
  3764. (setq status (equal (match-string 0) "[X]"))
  3765. (when (eq firstnew 'unknown)
  3766. (setq firstnew (not status)))
  3767. (replace-match
  3768. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  3769. (beginning-of-line 2)))))
  3770. (org-update-checkbox-count-maybe))
  3771. (defun org-update-checkbox-count-maybe ()
  3772. "Update checkbox statistics unless turned off by user."
  3773. (when org-provide-checkbox-statistics
  3774. (org-update-checkbox-count)))
  3775. (defun org-update-checkbox-count (&optional all)
  3776. "Update the checkbox statistics in the current section.
  3777. This will find all statistic cookies like [57%] and [6/12] and update them
  3778. with the current numbers. With optional prefix argument ALL, do this for
  3779. the whole buffer."
  3780. (interactive "P")
  3781. (save-excursion
  3782. (let* ((buffer-invisibility-spec nil)
  3783. (beg (progn (outline-back-to-heading) (point)))
  3784. (end (move-marker (make-marker)
  3785. (progn (outline-next-heading) (point))))
  3786. (re "\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)")
  3787. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)")
  3788. b1 e1 f1 c-on c-off lim (cstat 0))
  3789. (when all
  3790. (goto-char (point-min))
  3791. (outline-next-heading)
  3792. (setq beg (point) end (point-max)))
  3793. (goto-char beg)
  3794. (while (re-search-forward re end t)
  3795. (setq cstat (1+ cstat)
  3796. b1 (match-beginning 0)
  3797. e1 (match-end 0)
  3798. f1 (match-beginning 1)
  3799. lim (cond
  3800. ((org-on-heading-p) (outline-next-heading) (point))
  3801. ((org-at-item-p) (org-end-of-item) (point))
  3802. (t nil))
  3803. c-on 0 c-off 0)
  3804. (goto-char e1)
  3805. (when lim
  3806. (while (re-search-forward re-box lim t)
  3807. (if (equal (match-string 2) "[ ]")
  3808. (setq c-off (1+ c-off))
  3809. (setq c-on (1+ c-on))))
  3810. (delete-region b1 e1)
  3811. (goto-char b1)
  3812. (insert (if f1
  3813. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  3814. (format "[%d/%d]" c-on (+ c-on c-off))))))
  3815. (when (interactive-p)
  3816. (message "Checkbox satistics updated %s (%d places)"
  3817. (if all "in entire file" "in current outline entry") cstat)))))
  3818. (defun org-get-checkbox-statistics-face ()
  3819. "Select the face for checkbox statistics.
  3820. The face will be `org-done' when all relevant boxes are checked. Otherwise
  3821. it will be `org-todo'."
  3822. (if (match-end 1)
  3823. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  3824. (if (and (> (match-end 2) (match-beginning 2))
  3825. (equal (match-string 2) (match-string 3)))
  3826. 'org-done
  3827. 'org-todo)))
  3828. (defun org-get-indentation (&optional line)
  3829. "Get the indentation of the current line, interpreting tabs.
  3830. When LINE is given, assume it represents a line and compute its indentation."
  3831. (if line
  3832. (if (string-match "^ *" (org-remove-tabs line))
  3833. (match-end 0))
  3834. (save-excursion
  3835. (beginning-of-line 1)
  3836. (skip-chars-forward " \t")
  3837. (current-column))))
  3838. (defun org-remove-tabs (s &optional width)
  3839. "Replace tabulators in S with spaces.
  3840. Assumes that s is a single line, starting in column 0."
  3841. (setq width (or width tab-width))
  3842. (while (string-match "\t" s)
  3843. (setq s (replace-match
  3844. (make-string
  3845. (- (* width (/ (+ (match-beginning 0) width) width))
  3846. (match-beginning 0)) ?\ )
  3847. t t s)))
  3848. s)
  3849. ;; FIXME: document properly.
  3850. (defun org-fix-indentation (line ind)
  3851. "If the current indenation is smaller than ind1, leave it alone.
  3852. If it is larger than ind, reduce it by ind."
  3853. (let* ((l (org-remove-tabs line))
  3854. (i (org-get-indentation l))
  3855. (i1 (car ind)) (i2 (cdr ind)))
  3856. (if (>= i i2) (setq l (substring line i2)))
  3857. (if (> i1 0)
  3858. (concat (make-string i1 ?\ ) l)
  3859. l)))
  3860. (defun org-beginning-of-item ()
  3861. "Go to the beginning of the current hand-formatted item.
  3862. If the cursor is not in an item, throw an error."
  3863. (interactive)
  3864. (let ((pos (point))
  3865. (limit (save-excursion (org-back-to-heading)
  3866. (beginning-of-line 2) (point)))
  3867. ind ind1)
  3868. (if (org-at-item-p)
  3869. (beginning-of-line 1)
  3870. (beginning-of-line 1)
  3871. (skip-chars-forward " \t")
  3872. (setq ind (current-column))
  3873. (if (catch 'exit
  3874. (while t
  3875. (beginning-of-line 0)
  3876. (if (< (point) limit) (throw 'exit nil))
  3877. (unless (looking-at "[ \t]*$")
  3878. (skip-chars-forward " \t")
  3879. (setq ind1 (current-column))
  3880. (if (< ind1 ind)
  3881. (throw 'exit (org-at-item-p))))))
  3882. nil
  3883. (goto-char pos)
  3884. (error "Not in an item")))))
  3885. (defun org-end-of-item ()
  3886. "Go to the end of the current hand-formatted item.
  3887. If the cursor is not in an item, throw an error."
  3888. (interactive)
  3889. (let ((pos (point))
  3890. (limit (save-excursion (outline-next-heading) (point)))
  3891. (ind (save-excursion
  3892. (org-beginning-of-item)
  3893. (skip-chars-forward " \t")
  3894. (current-column)))
  3895. ind1)
  3896. (if (catch 'exit
  3897. (while t
  3898. (beginning-of-line 2)
  3899. (if (>= (point) limit) (throw 'exit t))
  3900. (unless (looking-at "[ \t]*$")
  3901. (skip-chars-forward " \t")
  3902. (setq ind1 (current-column))
  3903. (if (<= ind1 ind) (throw 'exit t)))))
  3904. (beginning-of-line 1)
  3905. (goto-char pos)
  3906. (error "Not in an item"))))
  3907. (defun org-next-item ()
  3908. "Move to the beginning of the next item in the current plain list.
  3909. Error if not at a plain list, or if this is the last item in the list."
  3910. (interactive)
  3911. (let (ind ind1 (pos (point)))
  3912. (org-beginning-of-item)
  3913. (setq ind (org-get-indentation))
  3914. (org-end-of-item)
  3915. (setq ind1 (org-get-indentation))
  3916. (unless (and (org-at-item-p) (= ind ind1))
  3917. (goto-char pos)
  3918. (error "On last item"))))
  3919. (defun org-previous-item ()
  3920. "Move to the beginning of the previous item in the current plain list.
  3921. Error if not at a plain list, or if this is the last item in the list."
  3922. (interactive)
  3923. (let (beg ind (pos (point)))
  3924. (org-beginning-of-item)
  3925. (setq beg (point))
  3926. (setq ind (org-get-indentation))
  3927. (goto-char beg)
  3928. (catch 'exit
  3929. (while t
  3930. (beginning-of-line 0)
  3931. (if (looking-at "[ \t]*$")
  3932. nil
  3933. (if (<= (org-get-indentation) ind)
  3934. (throw 'exit t)))))
  3935. (condition-case nil
  3936. (org-beginning-of-item)
  3937. (error (goto-char pos)
  3938. (error "On first item")))))
  3939. (defun org-move-item-down ()
  3940. "Move the plain list item at point down, i.e. swap with following item.
  3941. Subitems (items with larger indentation) are considered part of the item,
  3942. so this really moves item trees."
  3943. (interactive)
  3944. (let (beg end ind ind1 (pos (point)) txt)
  3945. (org-beginning-of-item)
  3946. (setq beg (point))
  3947. (setq ind (org-get-indentation))
  3948. (org-end-of-item)
  3949. (setq end (point))
  3950. (setq ind1 (org-get-indentation))
  3951. (if (and (org-at-item-p) (= ind ind1))
  3952. (progn
  3953. (org-end-of-item)
  3954. (setq txt (buffer-substring beg end))
  3955. (save-excursion
  3956. (delete-region beg end))
  3957. (setq pos (point))
  3958. (insert txt)
  3959. (goto-char pos)
  3960. (org-maybe-renumber-ordered-list))
  3961. (goto-char pos)
  3962. (error "Cannot move this item further down"))))
  3963. (defun org-move-item-up (arg)
  3964. "Move the plain list item at point up, i.e. swap with previous item.
  3965. Subitems (items with larger indentation) are considered part of the item,
  3966. so this really moves item trees."
  3967. (interactive "p")
  3968. (let (beg end ind ind1 (pos (point)) txt)
  3969. (org-beginning-of-item)
  3970. (setq beg (point))
  3971. (setq ind (org-get-indentation))
  3972. (org-end-of-item)
  3973. (setq end (point))
  3974. (goto-char beg)
  3975. (catch 'exit
  3976. (while t
  3977. (beginning-of-line 0)
  3978. (if (looking-at "[ \t]*$")
  3979. nil
  3980. (if (<= (setq ind1 (org-get-indentation)) ind)
  3981. (throw 'exit t)))))
  3982. (condition-case nil
  3983. (org-beginning-of-item)
  3984. (error (goto-char beg)
  3985. (error "Cannot move this item further up")))
  3986. (setq ind1 (org-get-indentation))
  3987. (if (and (org-at-item-p) (= ind ind1))
  3988. (progn
  3989. (setq txt (buffer-substring beg end))
  3990. (save-excursion
  3991. (delete-region beg end))
  3992. (setq pos (point))
  3993. (insert txt)
  3994. (goto-char pos)
  3995. (org-maybe-renumber-ordered-list))
  3996. (goto-char pos)
  3997. (error "Cannot move this item further up"))))
  3998. (defun org-maybe-renumber-ordered-list ()
  3999. "Renumber the ordered list at point if setup allows it.
  4000. This tests the user option `org-auto-renumber-ordered-lists' before
  4001. doing the renumbering."
  4002. (and org-auto-renumber-ordered-lists
  4003. (org-at-item-p)
  4004. (match-beginning 3)
  4005. (org-renumber-ordered-list 1)))
  4006. (defun org-get-string-indentation (s)
  4007. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  4008. (let ((n -1) (i 0) (w tab-width) c)
  4009. (catch 'exit
  4010. (while (< (setq n (1+ n)) (length s))
  4011. (setq c (aref s n))
  4012. (cond ((= c ?\ ) (setq i (1+ i)))
  4013. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  4014. (t (throw 'exit t)))))
  4015. i))
  4016. (defun org-renumber-ordered-list (arg)
  4017. "Renumber an ordered plain list.
  4018. Cursor needs to be in the first line of an item, the line that starts
  4019. with something like \"1.\" or \"2)\"."
  4020. (interactive "p")
  4021. (unless (and (org-at-item-p)
  4022. (match-beginning 3))
  4023. (error "This is not an ordered list"))
  4024. (let ((line (org-current-line))
  4025. (col (current-column))
  4026. (ind (org-get-string-indentation
  4027. (buffer-substring (point-at-bol) (match-beginning 3))))
  4028. ;; (term (substring (match-string 3) -1))
  4029. ind1 (n (1- arg)))
  4030. ;; find where this list begins
  4031. (catch 'exit
  4032. (while t
  4033. (catch 'next
  4034. (beginning-of-line 0)
  4035. (if (looking-at "[ \t]*$") (throw 'next t))
  4036. (skip-chars-forward " \t") (setq ind1 (current-column))
  4037. (if (or (< ind1 ind)
  4038. (and (= ind1 ind)
  4039. (not (org-at-item-p))))
  4040. (throw 'exit t)))))
  4041. ;; Walk forward and replace these numbers
  4042. (catch 'exit
  4043. (while t
  4044. (catch 'next
  4045. (beginning-of-line 2)
  4046. (if (eobp) (throw 'exit nil))
  4047. (if (looking-at "[ \t]*$") (throw 'next nil))
  4048. (skip-chars-forward " \t") (setq ind1 (current-column))
  4049. (if (> ind1 ind) (throw 'next t))
  4050. (if (< ind1 ind) (throw 'exit t))
  4051. (if (not (org-at-item-p)) (throw 'exit nil))
  4052. (if (not (match-beginning 3))
  4053. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  4054. (delete-region (match-beginning 3) (1- (match-end 3)))
  4055. (goto-char (match-beginning 3))
  4056. (insert (format "%d" (setq n (1+ n)))))))
  4057. (goto-line line)
  4058. (move-to-column col)))
  4059. (defvar org-last-indent-begin-marker (make-marker))
  4060. (defvar org-last-indent-end-marker (make-marker))
  4061. (defun org-outdent-item (arg)
  4062. "Outdent a local list item."
  4063. (interactive "p")
  4064. (org-indent-item (- arg)))
  4065. (defun org-indent-item (arg)
  4066. "Indent a local list item."
  4067. (interactive "p")
  4068. (unless (org-at-item-p)
  4069. (error "Not on an item"))
  4070. (save-excursion
  4071. (let (beg end ind ind1)
  4072. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  4073. (setq beg org-last-indent-begin-marker
  4074. end org-last-indent-end-marker)
  4075. (org-beginning-of-item)
  4076. (setq beg (move-marker org-last-indent-begin-marker (point)))
  4077. (org-end-of-item)
  4078. (setq end (move-marker org-last-indent-end-marker (point))))
  4079. (goto-char beg)
  4080. (skip-chars-forward " \t") (setq ind (current-column))
  4081. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  4082. (while (< (point) end)
  4083. (beginning-of-line 1)
  4084. (skip-chars-forward " \t") (setq ind1 (current-column))
  4085. (delete-region (point-at-bol) (point))
  4086. (indent-to-column (+ ind1 arg))
  4087. (beginning-of-line 2)))))
  4088. ;;; Archiving
  4089. (defun org-archive-subtree (&optional find-done)
  4090. "Move the current subtree to the archive.
  4091. The archive can be a certain top-level heading in the current file, or in
  4092. a different file. The tree will be moved to that location, the subtree
  4093. heading be marked DONE, and the current time will be added.
  4094. When called with prefix argument FIND-DONE, find whole trees without any
  4095. open TODO items and archive them (after getting confirmation from the user).
  4096. If the cursor is not at a headline when this comand is called, try all level
  4097. 1 trees. If the cursor is on a headline, only try the direct children of
  4098. this heading. "
  4099. (interactive "P")
  4100. (if find-done
  4101. (org-archive-all-done)
  4102. ;; Save all relevant TODO keyword-relatex variables
  4103. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  4104. (tr-org-todo-keywords org-todo-keywords)
  4105. (tr-org-todo-interpretation org-todo-interpretation)
  4106. (tr-org-done-string org-done-string)
  4107. (tr-org-todo-regexp org-todo-regexp)
  4108. (tr-org-todo-line-regexp org-todo-line-regexp)
  4109. (this-buffer (current-buffer))
  4110. file heading buffer level newfile-p)
  4111. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  4112. (progn
  4113. (setq file (format (match-string 1 org-archive-location)
  4114. (file-name-nondirectory buffer-file-name))
  4115. heading (match-string 2 org-archive-location)))
  4116. (error "Invalid `org-archive-location'"))
  4117. (if (> (length file) 0)
  4118. (setq newfile-p (not (file-exists-p file))
  4119. buffer (find-file-noselect file))
  4120. (setq buffer (current-buffer)))
  4121. (unless buffer
  4122. (error "Cannot access file \"%s\"" file))
  4123. (if (and (> (length heading) 0)
  4124. (string-match "^\\*+" heading))
  4125. (setq level (match-end 0))
  4126. (setq heading nil level 0))
  4127. (save-excursion
  4128. ;; We first only copy, in case something goes wrong
  4129. ;; we need to protect this-command, to avoid kill-region sets it,
  4130. ;; which would lead to duplication of subtrees
  4131. (let (this-command) (org-copy-subtree))
  4132. (set-buffer buffer)
  4133. ;; Enforce org-mode for the archive buffer
  4134. (if (not (org-mode-p))
  4135. ;; Force the mode for future visits.
  4136. (let ((org-insert-mode-line-in-empty-file t))
  4137. (call-interactively 'org-mode)))
  4138. (when newfile-p
  4139. (goto-char (point-max))
  4140. (insert (format "\nArchived entries from file %s\n\n"
  4141. (buffer-file-name this-buffer))))
  4142. ;; Force the TODO keywords of the original buffer
  4143. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  4144. (org-todo-keywords tr-org-todo-keywords)
  4145. (org-todo-interpretation tr-org-todo-interpretation)
  4146. (org-done-string tr-org-done-string)
  4147. (org-todo-regexp tr-org-todo-regexp)
  4148. (org-todo-line-regexp tr-org-todo-line-regexp))
  4149. (goto-char (point-min))
  4150. (if heading
  4151. (progn
  4152. (if (re-search-forward
  4153. (concat "\\(^\\|\r\\)"
  4154. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  4155. nil t)
  4156. (goto-char (match-end 0))
  4157. ;; Heading not found, just insert it at the end
  4158. (goto-char (point-max))
  4159. (or (bolp) (insert "\n"))
  4160. (insert "\n" heading "\n")
  4161. (end-of-line 0))
  4162. ;; Make the subtree visible
  4163. (show-subtree)
  4164. (org-end-of-subtree t)
  4165. (skip-chars-backward " \t\r\n]")
  4166. (and (looking-at "[ \t\r\n]*")
  4167. (replace-match "\n\n")))
  4168. ;; No specific heading, just go to end of file.
  4169. (goto-char (point-max)) (insert "\n"))
  4170. ;; Paste
  4171. (org-paste-subtree (1+ level))
  4172. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  4173. (if org-archive-mark-done
  4174. (org-todo (length org-todo-keywords)))
  4175. ;; Move cursor to right after the TODO keyword
  4176. (when org-archive-stamp-time
  4177. (beginning-of-line 1)
  4178. (looking-at org-todo-line-regexp)
  4179. (goto-char (or (match-end 2) (match-beginning 3)))
  4180. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  4181. (org-current-time))
  4182. ")"))
  4183. ;; Save the buffer, if it is not the same buffer.
  4184. (if (not (eq this-buffer buffer)) (save-buffer))))
  4185. ;; Here we are back in the original buffer. Everything seems to have
  4186. ;; worked. So now cut the tree and finish up.
  4187. (let (this-command) (org-cut-subtree))
  4188. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  4189. (message "Subtree archived %s"
  4190. (if (eq this-buffer buffer)
  4191. (concat "under heading: " heading)
  4192. (concat "in file: " (abbreviate-file-name file)))))))
  4193. (defun org-archive-all-done (&optional tag)
  4194. "Archive sublevels of the current tree without open TODO items.
  4195. If the cursor is not on a headline, try all level 1 trees. If
  4196. it is on a headline, try all direct children.
  4197. When TAG is non-nil, don't move trees, but mark them with the ARCHIVE tag."
  4198. (let ((re (concat "^\\*+ +" org-not-done-regexp)) re1
  4199. (rea (concat ".*:" org-archive-tag ":"))
  4200. (begm (make-marker))
  4201. (endm (make-marker))
  4202. (question (if tag "Set ARCHIVE tag (no open TODO items)? "
  4203. "Move subtree to archive (no open TODO items)? "))
  4204. beg end (cntarch 0))
  4205. (if (org-on-heading-p)
  4206. (progn
  4207. (setq re1 (concat "^" (regexp-quote
  4208. (make-string
  4209. (1+ (- (match-end 0) (match-beginning 0)))
  4210. ?*))
  4211. " "))
  4212. (move-marker begm (point))
  4213. (move-marker endm (org-end-of-subtree)))
  4214. (setq re1 "^* ")
  4215. (move-marker begm (point-min))
  4216. (move-marker endm (point-max)))
  4217. (save-excursion
  4218. (goto-char begm)
  4219. (while (re-search-forward re1 endm t)
  4220. (setq beg (match-beginning 0)
  4221. end (save-excursion (org-end-of-subtree t) (point)))
  4222. (goto-char beg)
  4223. (if (re-search-forward re end t)
  4224. (goto-char end)
  4225. (goto-char beg)
  4226. (if (and (or (not tag) (not (looking-at rea)))
  4227. (y-or-n-p question))
  4228. (progn
  4229. (if tag
  4230. (org-toggle-tag org-archive-tag 'on)
  4231. (org-archive-subtree))
  4232. (setq cntarch (1+ cntarch)))
  4233. (goto-char end)))))
  4234. (message "%d trees archived" cntarch)))
  4235. (defun org-cycle-hide-archived-subtrees (state)
  4236. "Re-hide all archived subtrees after a visibility state change."
  4237. (when (and (not org-cycle-open-archived-trees)
  4238. (not (memq state '(overview folded))))
  4239. (save-excursion
  4240. (let* ((globalp (memq state '(contents all)))
  4241. (beg (if globalp (point-min) (point)))
  4242. (end (if globalp (point-max) (org-end-of-subtree))))
  4243. (org-hide-archived-subtrees beg end)
  4244. (goto-char beg)
  4245. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4246. (message (substitute-command-keys
  4247. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4248. (defun org-force-cycle-archived ()
  4249. "Cycle subtree even if it is archived."
  4250. (interactive)
  4251. (setq this-command 'org-cycle)
  4252. (let ((org-cycle-open-archived-trees t))
  4253. (call-interactively 'org-cycle)))
  4254. (defun org-hide-archived-subtrees (beg end)
  4255. "Re-hide all archived subtrees after a visibility state change."
  4256. (save-excursion
  4257. (let* ((re (concat ":" org-archive-tag ":")))
  4258. (goto-char beg)
  4259. (while (re-search-forward re end t)
  4260. (and (org-on-heading-p) (hide-subtree))
  4261. (org-end-of-subtree)))))
  4262. (defun org-toggle-tag (tag &optional onoff)
  4263. "Toggle the tag TAG for the current line.
  4264. If ONOFF is `on' or `off', don't toggle but set to this state."
  4265. (unless (org-on-heading-p) (error "Not on headling"))
  4266. (let (res current)
  4267. (save-excursion
  4268. (beginning-of-line)
  4269. (if (re-search-forward "[ \t]:\\([a-zA-Z0-9_@:]+\\):[ \t]*$"
  4270. (point-at-eol) t)
  4271. (progn
  4272. (setq current (match-string 1))
  4273. (replace-match ""))
  4274. (setq current ""))
  4275. (setq current (nreverse (org-split-string current ":")))
  4276. (cond
  4277. ((eq onoff 'on)
  4278. (setq res t)
  4279. (or (member tag current) (push tag current)))
  4280. ((eq onoff 'off)
  4281. (or (not (member tag current)) (setq current (delete tag current))))
  4282. (t (if (member tag current)
  4283. (setq current (delete tag current))
  4284. (setq res t)
  4285. (push tag current))))
  4286. (end-of-line 1)
  4287. (when current
  4288. (insert " :" (mapconcat 'identity (nreverse current) ":") ":"))
  4289. (org-set-tags nil t))
  4290. res))
  4291. (defun org-toggle-archive-tag (&optional arg)
  4292. "Toggle the archive tag for the current headline.
  4293. With prefix ARG, check all children of current headline and offer tagging
  4294. the children that do not contain any open TODO items."
  4295. (interactive "P")
  4296. (if arg
  4297. (org-archive-all-done 'tag)
  4298. (let (set)
  4299. (save-excursion
  4300. (org-back-to-heading t)
  4301. (setq set (org-toggle-tag org-archive-tag))
  4302. (when set (hide-subtree)))
  4303. (and set (beginning-of-line 1))
  4304. (message "Subtree %s" (if set "archived" "unarchived")))))
  4305. (defvar org-agenda-multi nil) ; dynammically scoped
  4306. (defvar org-agenda-buffer-name "*Org Agenda*")
  4307. (defun org-prepare-agenda ()
  4308. (if org-agenda-multi
  4309. (progn
  4310. (setq buffer-read-only nil)
  4311. (goto-char (point-max))
  4312. (unless (= (point) 1)
  4313. (insert "\n" (make-string (window-width) ?=) "\n"))
  4314. (narrow-to-region (point) (point-max)))
  4315. (org-agenda-maybe-reset-markers 'force)
  4316. (org-prepare-agenda-buffers (org-agenda-files))
  4317. (unless (equal (current-buffer) (get-buffer org-agenda-buffer-name))
  4318. (delete-other-windows)
  4319. (switch-to-buffer-other-window
  4320. (get-buffer-create org-agenda-buffer-name)))
  4321. (setq buffer-read-only nil)
  4322. (erase-buffer)
  4323. (org-agenda-mode))
  4324. (setq buffer-read-only nil)) ;;; FIXME: do we need all these occasions????
  4325. (defun org-prepare-agenda-buffers (files)
  4326. "Create buffers for all agenda files, protect archived trees and comments."
  4327. (interactive)
  4328. (let ((pa '(:org-archived t))
  4329. (pc '(:org-comment t))
  4330. (pall '(:org-archived t :org-comment t))
  4331. (rea (concat ":" org-archive-tag ":"))
  4332. bmp file re)
  4333. (save-excursion
  4334. (while (setq file (pop files))
  4335. (org-check-agenda-file file)
  4336. (set-buffer (org-get-agenda-file-buffer file))
  4337. (widen)
  4338. (setq bmp (buffer-modified-p))
  4339. (save-excursion
  4340. (remove-text-properties (point-min) (point-max) pall)
  4341. (when org-agenda-skip-archived-trees
  4342. (goto-char (point-min))
  4343. (while (re-search-forward rea nil t)
  4344. (if (org-on-heading-p)
  4345. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  4346. (goto-char (point-min))
  4347. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  4348. (while (re-search-forward re nil t)
  4349. (add-text-properties
  4350. (match-beginning 0) (org-end-of-subtree t) pc)))
  4351. (set-buffer-modified-p bmp)))))
  4352. (defun org-agenda-skip ()
  4353. "Throw to `:skip' in places that should be skipped."
  4354. (let ((p (point-at-bol)))
  4355. (and org-agenda-skip-archived-trees
  4356. (get-text-property p :org-archived)
  4357. (org-end-of-subtree)
  4358. (throw :skip t))
  4359. (and (get-text-property p :org-comment)
  4360. (org-end-of-subtree)
  4361. (throw :skip t))
  4362. (if (equal (char-after p) ?#) (throw :skip t))))
  4363. (defun org-agenda-toggle-archive-tag ()
  4364. "Toggle the archive tag for the current entry."
  4365. (interactive)
  4366. (org-agenda-check-no-diary)
  4367. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  4368. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  4369. (org-agenda-error)))
  4370. (buffer (marker-buffer hdmarker))
  4371. (pos (marker-position hdmarker))
  4372. (buffer-read-only nil)
  4373. newhead)
  4374. (with-current-buffer buffer
  4375. (widen)
  4376. (goto-char pos)
  4377. (org-show-hidden-entry)
  4378. (save-excursion
  4379. (and (outline-next-heading)
  4380. (org-flag-heading nil))) ; show the next heading
  4381. (call-interactively 'org-toggle-archive-tag)
  4382. (end-of-line 1)
  4383. (setq newhead (org-get-heading)))
  4384. (org-agenda-change-all-lines newhead hdmarker)
  4385. (beginning-of-line 1)))
  4386. ;;; Dynamic blocks
  4387. (defun org-find-dblock (name)
  4388. "Find the first dynamic block with name NAME in the buffer.
  4389. If not found, stay at current position and return nil."
  4390. (let (pos)
  4391. (save-excursion
  4392. (goto-char (point-min))
  4393. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  4394. nil t)
  4395. (match-beginning 0))))
  4396. (if pos (goto-char pos))
  4397. pos))
  4398. (defconst org-dblock-start-re
  4399. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  4400. "Matches the startline of a dynamic block, with parameters.")
  4401. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  4402. "Matches the end of a dyhamic block.")
  4403. (defun org-create-dblock (plist)
  4404. "Create a dynamic block section, with parameters taken from PLIST.
  4405. PLIST must containe a :name entry which is used as name of the block."
  4406. (unless (bolp) (newline))
  4407. (let ((name (plist-get plist :name)))
  4408. (insert "#+BEGIN: " name)
  4409. (while plist
  4410. (if (eq (car plist) :name)
  4411. (setq plist (cddr plist))
  4412. (insert " " (prin1-to-string (pop plist)))))
  4413. (insert "\n\n#+END:\n")
  4414. (beginning-of-line -2)))
  4415. (defun org-prepare-dblock ()
  4416. "Prepare dynamic block for refresh.
  4417. This empties the block, puts the cursor at the insert position and returns
  4418. the property list including an extra property :name with the block name."
  4419. (unless (looking-at org-dblock-start-re)
  4420. (error "Not at a dynamic block"))
  4421. (let* ((begdel (1+ (match-end 0)))
  4422. (name (match-string 1))
  4423. (params (append (list :name name)
  4424. (read (concat "(" (match-string 3) ")")))))
  4425. (unless (re-search-forward org-dblock-end-re nil t)
  4426. (error "Dynamic block not terminated"))
  4427. (delete-region begdel (match-beginning 0))
  4428. (goto-char begdel)
  4429. (open-line 1)
  4430. params))
  4431. (defun org-map-dblocks (&optional command)
  4432. "Apply COMMAND to all dynamic blocks in the current buffer.
  4433. If COMMAND is not given, use `org-update-dblock'."
  4434. (let ((cmd (or command 'org-update-dblock))
  4435. pos)
  4436. (save-excursion
  4437. (goto-char (point-min))
  4438. (while (re-search-forward org-dblock-start-re nil t)
  4439. (goto-char (setq pos (match-beginning 0)))
  4440. (condition-case nil
  4441. (funcall cmd)
  4442. (error (message "Error during update of dynamic block")))
  4443. (goto-char pos)
  4444. (unless (re-search-forward org-dblock-end-re nil t)
  4445. (error "Dynamic block not terminated"))))))
  4446. (defun org-dblock-update (&optional arg)
  4447. "User command for updating dynamic blocks.
  4448. Update the dynamic block at point. With prefix ARG, update all dynamic
  4449. blocks in the buffer."
  4450. (interactive "P")
  4451. (if arg
  4452. (org-update-all-dblocks)
  4453. (or (looking-at org-dblock-start-re)
  4454. (org-beginning-of-dblock))
  4455. (org-update-dblock)))
  4456. (defun org-update-dblock ()
  4457. "Update the dynamic block at point
  4458. This means to empty the block, parse for parameters and then call
  4459. the correct writing function."
  4460. (let* ((pos (point))
  4461. (params (org-prepare-dblock))
  4462. (name (plist-get params :name))
  4463. (cmd (intern (concat "org-dblock-write:" name))))
  4464. (funcall cmd params)
  4465. (goto-char pos)))
  4466. (defun org-beginning-of-dblock ()
  4467. "Find the beginning of the dynamic block at point.
  4468. Error if there is no scuh block at point."
  4469. (let ((pos (point))
  4470. beg)
  4471. (end-of-line 1)
  4472. (if (and (re-search-backward org-dblock-start-re nil t)
  4473. (setq beg (match-beginning 0))
  4474. (re-search-forward org-dblock-end-re nil t)
  4475. (> (match-end 0) pos))
  4476. (goto-char beg)
  4477. (goto-char pos)
  4478. (error "Not in a dynamic block"))))
  4479. (defun org-update-all-dblocks ()
  4480. "Update all dynamic blocks in the buffer.
  4481. This function can be used in a hook."
  4482. (when (org-mode-p)
  4483. (org-map-dblocks 'org-update-dblock)))
  4484. ;;; Completion
  4485. (defun org-complete (&optional arg)
  4486. "Perform completion on word at point.
  4487. At the beginning of a headline, this completes TODO keywords as given in
  4488. `org-todo-keywords'.
  4489. If the current word is preceded by a backslash, completes the TeX symbols
  4490. that are supported for HTML support.
  4491. If the current word is preceded by \"#+\", completes special words for
  4492. setting file options.
  4493. In the line after \"#+STARTUP:, complete valid keywords.\"
  4494. At all other locations, this simply calls `ispell-complete-word'."
  4495. (interactive "P")
  4496. (catch 'exit
  4497. (let* ((end (point))
  4498. (beg1 (save-excursion
  4499. ;FIXME??? (if (equal (char-before (point)) ?\ ) (backward-char 1))
  4500. (skip-chars-backward "a-zA-Z_@0-9")
  4501. (point)))
  4502. (beg (save-excursion
  4503. ;FIXME??? (if (equal (char-before (point)) ?\ ) (backward-char 1))
  4504. (skip-chars-backward "a-zA-Z0-9_:$")
  4505. (point)))
  4506. (confirm (lambda (x) (stringp (car x))))
  4507. (camel (equal (char-before beg) ?*))
  4508. (tag (equal (char-before beg1) ?:))
  4509. (texp (equal (char-before beg) ?\\))
  4510. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  4511. beg)
  4512. "#+"))
  4513. (startup (string-match "^#\\+STARTUP:.*"
  4514. (buffer-substring (point-at-bol) (point))))
  4515. (completion-ignore-case opt)
  4516. (type nil)
  4517. (tbl nil)
  4518. (table (cond
  4519. (opt
  4520. (setq type :opt)
  4521. (mapcar (lambda (x)
  4522. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  4523. (cons (match-string 2 x) (match-string 1 x)))
  4524. (org-split-string (org-get-current-options) "\n")))
  4525. (startup
  4526. (setq type :startup)
  4527. org-startup-options)
  4528. (texp
  4529. (setq type :tex)
  4530. org-html-entities)
  4531. ((string-match "\\`\\*+[ \t]*\\'"
  4532. (buffer-substring (point-at-bol) beg))
  4533. (setq type :todo)
  4534. (mapcar 'list org-todo-keywords))
  4535. (camel
  4536. (setq type :camel)
  4537. (save-excursion
  4538. (goto-char (point-min))
  4539. (while (re-search-forward org-todo-line-regexp nil t)
  4540. (push (list
  4541. (if org-file-link-context-use-camel-case
  4542. (org-make-org-heading-camel (match-string 3) t)
  4543. (org-make-org-heading-search-string
  4544. (match-string 3) t)))
  4545. tbl)))
  4546. tbl)
  4547. (tag (setq type :tag beg beg1)
  4548. (or org-tag-alist (org-get-buffer-tags)))
  4549. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  4550. (pattern (buffer-substring-no-properties beg end))
  4551. (completion (try-completion pattern table confirm)))
  4552. (cond ((eq completion t)
  4553. (if (equal type :opt)
  4554. (insert (substring (cdr (assoc (upcase pattern) table))
  4555. (length pattern)))))
  4556. ((null completion)
  4557. (message "Can't find completion for \"%s\"" pattern)
  4558. (ding))
  4559. ((not (string= pattern completion))
  4560. (delete-region beg end)
  4561. (if (string-match " +$" completion)
  4562. (setq completion (replace-match "" t t completion)))
  4563. (insert completion)
  4564. (if (get-buffer-window "*Completions*")
  4565. (delete-window (get-buffer-window "*Completions*")))
  4566. (if (assoc completion table)
  4567. (if (eq type :todo) (insert " ")
  4568. (if (eq type :tag) (insert ":"))))
  4569. (if (and (equal type :opt) (assoc completion table))
  4570. (message "%s" (substitute-command-keys
  4571. "Press \\[org-complete] again to insert example settings"))))
  4572. (t
  4573. (message "Making completion list...")
  4574. (let ((list (sort (all-completions pattern table confirm)
  4575. 'string<)))
  4576. (with-output-to-temp-buffer "*Completions*"
  4577. (condition-case nil
  4578. ;; Protection needed for XEmacs and emacs 21
  4579. (display-completion-list list pattern)
  4580. (error (display-completion-list list)))))
  4581. (message "Making completion list...%s" "done"))))))
  4582. ;;; Comments, TODO and DEADLINE
  4583. (defun org-toggle-comment ()
  4584. "Change the COMMENT state of an entry."
  4585. (interactive)
  4586. (save-excursion
  4587. (org-back-to-heading)
  4588. (if (looking-at (concat outline-regexp
  4589. "\\( +\\<" org-comment-string "\\>\\)"))
  4590. (replace-match "" t t nil 1)
  4591. (if (looking-at outline-regexp)
  4592. (progn
  4593. (goto-char (match-end 0))
  4594. (insert " " org-comment-string))))))
  4595. (defvar org-last-todo-state-is-todo nil
  4596. "This is non-nil when the last TODO state change led to a TODO state.
  4597. If the last change removed the TODO tag or switched to DONE, then
  4598. this is nil.")
  4599. (defun org-todo (&optional arg)
  4600. "Change the TODO state of an item.
  4601. The state of an item is given by a keyword at the start of the heading,
  4602. like
  4603. *** TODO Write paper
  4604. *** DONE Call mom
  4605. The different keywords are specified in the variable `org-todo-keywords'.
  4606. By default the available states are \"TODO\" and \"DONE\".
  4607. So for this example: when the item starts with TODO, it is changed to DONE.
  4608. When it starts with DONE, the DONE is removed. And when neither TODO nor
  4609. DONE are present, add TODO at the beginning of the heading.
  4610. With prefix arg, use completion to determine the new state. With numeric
  4611. prefix arg, switch to that state."
  4612. (interactive "P")
  4613. (save-excursion
  4614. (org-back-to-heading)
  4615. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  4616. (or (looking-at (concat " +" org-todo-regexp " *"))
  4617. (looking-at " *"))
  4618. (let* ((this (match-string 1))
  4619. (completion-ignore-case t)
  4620. (member (member this org-todo-keywords))
  4621. (tail (cdr member))
  4622. (state (cond
  4623. ((equal arg '(4))
  4624. ;; Read a state with completion
  4625. (completing-read "State: " (mapcar (lambda(x) (list x))
  4626. org-todo-keywords)
  4627. nil t))
  4628. ((eq arg 'right)
  4629. (if this
  4630. (if tail (car tail) nil)
  4631. (car org-todo-keywords)))
  4632. ((eq arg 'left)
  4633. (if (equal member org-todo-keywords)
  4634. nil
  4635. (if this
  4636. (nth (- (length org-todo-keywords) (length tail) 2)
  4637. org-todo-keywords)
  4638. org-done-string)))
  4639. (arg
  4640. ;; user requests a specific state
  4641. (nth (1- (prefix-numeric-value arg))
  4642. org-todo-keywords))
  4643. ((null member) (car org-todo-keywords))
  4644. ((null tail) nil) ;; -> first entry
  4645. ((eq org-todo-interpretation 'sequence)
  4646. (car tail))
  4647. ((memq org-todo-interpretation '(type priority))
  4648. (if (eq this-command last-command)
  4649. (car tail)
  4650. (if (> (length tail) 0) org-done-string nil)))
  4651. (t nil)))
  4652. (next (if state (concat " " state " ") " ")))
  4653. (replace-match next t t)
  4654. (setq org-last-todo-state-is-todo
  4655. (not (equal state org-done-string)))
  4656. (when org-log-done
  4657. (if (equal state org-done-string)
  4658. (org-add-planning-info 'closed (current-time) 'scheduled)
  4659. (if (not this)
  4660. (org-add-planning-info nil nil 'closed))))
  4661. ;; Fixup tag positioning
  4662. (and org-auto-align-tags (org-set-tags nil t))
  4663. (run-hooks 'org-after-todo-state-change-hook)))
  4664. ;; Fixup cursor location if close to the keyword
  4665. (if (and (outline-on-heading-p)
  4666. (not (bolp))
  4667. (save-excursion (beginning-of-line 1)
  4668. (looking-at org-todo-line-regexp))
  4669. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4670. (progn
  4671. (goto-char (or (match-end 2) (match-end 1)))
  4672. (just-one-space))))
  4673. (defun org-log-done (&optional undone)
  4674. "Add a time stamp logging that a TODO entry has been closed.
  4675. When UNDONE is non-nil, remove such a time stamp again."
  4676. (interactive)
  4677. (let (beg end col)
  4678. (save-excursion
  4679. (org-back-to-heading t)
  4680. (setq beg (point))
  4681. (looking-at (concat outline-regexp " *"))
  4682. (goto-char (match-end 0))
  4683. (setq col (current-column))
  4684. (outline-next-heading)
  4685. (setq end (point))
  4686. (goto-char beg)
  4687. (when (re-search-forward (concat
  4688. "[\r\n]\\([ \t]*"
  4689. (regexp-quote org-closed-string)
  4690. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  4691. (delete-region (match-beginning 1) (match-end 1)))
  4692. (unless undone
  4693. (org-back-to-heading t)
  4694. (skip-chars-forward "^\n\r")
  4695. (goto-char (min (1+ (point)) (point-max)))
  4696. (when (not (member (char-before) '(?\r ?\n)))
  4697. (insert "\n"))
  4698. (indent-to col)
  4699. (insert org-closed-string " "
  4700. (format-time-string
  4701. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4702. (org-current-time))
  4703. "\n")))))
  4704. (defun org-show-todo-tree (arg)
  4705. "Make a compact tree which shows all headlines marked with TODO.
  4706. The tree will show the lines where the regexp matches, and all higher
  4707. headlines above the match.
  4708. With \\[universal-argument] prefix, also show the DONE entries.
  4709. With a numeric prefix N, construct a sparse tree for the Nth element
  4710. of `org-todo-keywords'."
  4711. (interactive "P")
  4712. (let ((case-fold-search nil)
  4713. (kwd-re
  4714. (cond ((null arg) org-not-done-regexp)
  4715. ((equal arg '(4)) org-todo-regexp)
  4716. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4717. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4718. org-todo-keywords)))
  4719. (t (error "Invalid prefix argument: %s" arg)))))
  4720. (message "%d TODO entries found"
  4721. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4722. (defun org-deadline ()
  4723. "Insert the DEADLINE: string to make a deadline.
  4724. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4725. to modify it to the correct date."
  4726. (interactive)
  4727. (org-add-planning-info 'deadline nil 'closed))
  4728. (defun org-schedule ()
  4729. "Insert the SCHEDULED: string to schedule a TODO item.
  4730. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4731. to modify it to the correct date."
  4732. (interactive)
  4733. (org-add-planning-info 'scheduled nil 'closed))
  4734. (defun org-add-planning-info (what &optional time &rest remove)
  4735. "Insert new timestamp with keyword in the line directly after the headline.
  4736. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4737. If non is given, the user is prompted for a date.
  4738. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4739. be removed."
  4740. (interactive)
  4741. (when what (setq time (or time (org-read-date nil 'to-time))))
  4742. (when (and org-insert-labeled-timestamps-at-point
  4743. (member what '(scheduled deadline)))
  4744. (insert
  4745. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4746. " "
  4747. (format-time-string (car org-time-stamp-formats) time))
  4748. (setq what nil))
  4749. (save-excursion
  4750. (let (col list elt (buffer-invisibility-spec nil) ts)
  4751. (org-back-to-heading t)
  4752. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4753. (goto-char (match-end 1))
  4754. (setq col (current-column))
  4755. (goto-char (1+ (match-end 0)))
  4756. (if (and (not (looking-at outline-regexp))
  4757. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4758. "[^\r\n]*"))
  4759. (not (equal (match-string 1) org-clock-string)))
  4760. (narrow-to-region (match-beginning 0) (match-end 0))
  4761. (insert "\n")
  4762. (backward-char 1)
  4763. (narrow-to-region (point) (point))
  4764. (indent-to-column col))
  4765. ;; Check if we have to remove something.
  4766. (setq list (cons what remove))
  4767. (while list
  4768. (setq elt (pop list))
  4769. (goto-char (point-min))
  4770. (when (or (and (eq elt 'scheduled)
  4771. (re-search-forward org-scheduled-time-regexp nil t))
  4772. (and (eq elt 'deadline)
  4773. (re-search-forward org-deadline-time-regexp nil t))
  4774. (and (eq elt 'closed)
  4775. (re-search-forward org-closed-time-regexp nil t)))
  4776. (replace-match "")
  4777. (if (looking-at " +") (replace-match ""))))
  4778. (goto-char (point-max))
  4779. (when what
  4780. (insert
  4781. (if (not (equal (char-before) ?\ )) " " "")
  4782. (cond ((eq what 'scheduled) org-scheduled-string)
  4783. ((eq what 'deadline) org-deadline-string)
  4784. ((eq what 'closed) org-closed-string))
  4785. " ")
  4786. (insert
  4787. (setq ts
  4788. (format-time-string
  4789. (if (eq what 'closed)
  4790. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4791. (car org-time-stamp-formats))
  4792. time))))
  4793. (goto-char (point-min))
  4794. (widen)
  4795. (if (looking-at "[ \t]+\r?\n")
  4796. (replace-match ""))
  4797. ts)))
  4798. (defun org-occur (regexp &optional callback)
  4799. "Make a compact tree which shows all matches of REGEXP.
  4800. The tree will show the lines where the regexp matches, and all higher
  4801. headlines above the match. It will also show the heading after the match,
  4802. to make sure editing the matching entry is easy.
  4803. If CALLBACK is non-nil, it is a function which is called to confirm
  4804. that the match should indeed be shown."
  4805. (interactive "sRegexp: ")
  4806. (org-remove-occur-highlights nil nil t)
  4807. (let ((cnt 0))
  4808. (save-excursion
  4809. (goto-char (point-min))
  4810. (org-overview)
  4811. (while (re-search-forward regexp nil t)
  4812. (when (or (not callback)
  4813. (save-match-data (funcall callback)))
  4814. (setq cnt (1+ cnt))
  4815. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4816. (org-show-hierarchy-above))))
  4817. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4818. nil 'local)
  4819. (unless org-sparse-tree-open-archived-trees
  4820. (org-hide-archived-subtrees (point-min) (point-max)))
  4821. (run-hooks 'org-occur-hook)
  4822. (if (interactive-p)
  4823. (message "%d match(es) for regexp %s" cnt regexp))
  4824. cnt))
  4825. (defun org-show-hierarchy-above ()
  4826. "Make sure point and the headings hierarchy above is visible."
  4827. (catch 'exit
  4828. (if (org-on-heading-p t)
  4829. (org-flag-heading nil) ; only show the heading
  4830. (and (or (org-invisible-p) (org-invisible-p2))
  4831. (org-show-hidden-entry))) ; show entire entry
  4832. (save-excursion
  4833. (and org-show-following-heading
  4834. (outline-next-heading)
  4835. (org-flag-heading nil))) ; show the next heading
  4836. (when org-show-hierarchy-above
  4837. (save-excursion ; show all higher headings
  4838. (while (and (condition-case nil
  4839. (progn (org-up-heading-all 1) t)
  4840. (error nil))
  4841. (not (bobp)))
  4842. (org-flag-heading nil))))))
  4843. ;; Overlay compatibility functions
  4844. (defun org-make-overlay (beg end &optional buffer)
  4845. (if (featurep 'xemacs)
  4846. (make-extent beg end buffer)
  4847. (make-overlay beg end buffer)))
  4848. (defun org-delete-overlay (ovl)
  4849. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4850. (defun org-detatch-overlay (ovl)
  4851. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4852. (defun org-move-overlay (ovl beg end &optional buffer)
  4853. (if (featurep 'xemacs)
  4854. (set-extent-endpoints ovl beg end buffer)
  4855. (move-overlay ovl beg end buffer)))
  4856. (defun org-overlay-put (ovl prop value)
  4857. (if (featurep 'xemacs)
  4858. (set-extent-property ovl prop value)
  4859. (overlay-put ovl prop value)))
  4860. (defun org-overlays-at (pos)
  4861. (if (featurep 'xemacs) (extents-at pos) (overlays-at pos)))
  4862. (defun org-overlay-start (o)
  4863. (if (featurep 'xemacs) (extent-start-position o) (overlay-start o)))
  4864. (defun org-overlay-end (o)
  4865. (if (featurep 'xemacs) (extent-end-position o) (overlay-end o)))
  4866. (defvar org-occur-highlights nil)
  4867. (make-variable-buffer-local 'org-occur-highlights)
  4868. (defun org-highlight-new-match (beg end)
  4869. "Highlight from BEG to END and mark the highlight is an occur headline."
  4870. (let ((ov (org-make-overlay beg end)))
  4871. (org-overlay-put ov 'face 'secondary-selection)
  4872. (push ov org-occur-highlights)))
  4873. (defvar org-inhibit-highlight-removal nil)
  4874. (defun org-remove-occur-highlights (&optional beg end noremove)
  4875. "Remove the occur highlights from the buffer.
  4876. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4877. from the `before-change-functions' in the current buffer."
  4878. (interactive)
  4879. (unless org-inhibit-highlight-removal
  4880. (mapc 'org-delete-overlay org-occur-highlights)
  4881. (setq org-occur-highlights nil)
  4882. (unless noremove
  4883. (remove-hook 'before-change-functions
  4884. 'org-remove-occur-highlights 'local))))
  4885. ;;; Priorities
  4886. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4887. "Regular expression matching the priority indicator.")
  4888. (defvar org-remove-priority-next-time nil)
  4889. (defun org-priority-up ()
  4890. "Increase the priority of the current item."
  4891. (interactive)
  4892. (org-priority 'up))
  4893. (defun org-priority-down ()
  4894. "Decrease the priority of the current item."
  4895. (interactive)
  4896. (org-priority 'down))
  4897. (defun org-priority (&optional action)
  4898. "Change the priority of an item by ARG.
  4899. ACTION can be set, up, or down."
  4900. (interactive)
  4901. (setq action (or action 'set))
  4902. (let (current new news have remove)
  4903. (save-excursion
  4904. (org-back-to-heading)
  4905. (if (looking-at org-priority-regexp)
  4906. (setq current (string-to-char (match-string 2))
  4907. have t)
  4908. (setq current org-default-priority))
  4909. (cond
  4910. ((eq action 'set)
  4911. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4912. (setq new (read-char-exclusive))
  4913. (cond ((equal new ?\ ) (setq remove t))
  4914. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4915. (error "Priority must be between `%c' and `%c'"
  4916. ?A org-lowest-priority))))
  4917. ((eq action 'up)
  4918. (setq new (1- current)))
  4919. ((eq action 'down)
  4920. (setq new (1+ current)))
  4921. (t (error "Invalid action")))
  4922. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4923. (setq news (format "%c" new))
  4924. (if have
  4925. (if remove
  4926. (replace-match "" t t nil 1)
  4927. (replace-match news t t nil 2))
  4928. (if remove
  4929. (error "No priority cookie found in line")
  4930. (looking-at org-todo-line-regexp)
  4931. (if (match-end 2)
  4932. (progn
  4933. (goto-char (match-end 2))
  4934. (insert " [#" news "]"))
  4935. (goto-char (match-beginning 3))
  4936. (insert "[#" news "] ")))))
  4937. (if remove
  4938. (message "Priority removed")
  4939. (message "Priority of current item set to %s" news))))
  4940. (defun org-get-priority (s)
  4941. "Find priority cookie and return priority."
  4942. (save-match-data
  4943. (if (not (string-match org-priority-regexp s))
  4944. (* 1000 (- org-lowest-priority org-default-priority))
  4945. (* 1000 (- org-lowest-priority
  4946. (string-to-char (match-string 2 s)))))))
  4947. ;;; Timestamps
  4948. (defvar org-last-changed-timestamp nil)
  4949. (defun org-time-stamp (arg)
  4950. "Prompt for a date/time and insert a time stamp.
  4951. If the user specifies a time like HH:MM, or if this command is called
  4952. with a prefix argument, the time stamp will contain date and time.
  4953. Otherwise, only the date will be included. All parts of a date not
  4954. specified by the user will be filled in from the current date/time.
  4955. So if you press just return without typing anything, the time stamp
  4956. will represent the current date/time. If there is already a timestamp
  4957. at the cursor, it will be modified."
  4958. (interactive "P")
  4959. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4960. (car org-time-stamp-formats)))
  4961. (org-time-was-given nil)
  4962. time)
  4963. (cond
  4964. ((and (org-at-timestamp-p)
  4965. (eq last-command 'org-time-stamp)
  4966. (eq this-command 'org-time-stamp))
  4967. (insert "--")
  4968. (setq time (let ((this-command this-command))
  4969. (org-read-date arg 'totime)))
  4970. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4971. (insert (format-time-string fmt time)))
  4972. ((org-at-timestamp-p)
  4973. (setq time (let ((this-command this-command))
  4974. (org-read-date arg 'totime)))
  4975. (and (org-at-timestamp-p) (replace-match
  4976. (setq org-last-changed-timestamp
  4977. (format-time-string fmt time))
  4978. t t))
  4979. (message "Timestamp updated"))
  4980. (t
  4981. (setq time (let ((this-command this-command))
  4982. (org-read-date arg 'totime)))
  4983. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4984. (insert (format-time-string fmt time))))))
  4985. (defun org-time-stamp-inactive (&optional arg)
  4986. "Insert an inactive time stamp.
  4987. An inactive time stamp is enclosed in square brackets instead of angle
  4988. brackets. It is inactive in the sense that it does not trigger agenda entries,
  4989. does not link to the calendar and cannot be changed with the S-cursor keys.
  4990. So these are more for recording a certain time/date."
  4991. (interactive "P")
  4992. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4993. (car org-time-stamp-formats)))
  4994. (org-time-was-given nil)
  4995. time)
  4996. (setq time (org-read-date arg 'totime))
  4997. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4998. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  4999. (insert (format-time-string fmt time))))
  5000. (defvar org-date-ovl (org-make-overlay 1 1))
  5001. (org-overlay-put org-date-ovl 'face 'org-warning)
  5002. (org-detatch-overlay org-date-ovl)
  5003. (defun org-read-date (&optional with-time to-time)
  5004. "Read a date and make things smooth for the user.
  5005. The prompt will suggest to enter an ISO date, but you can also enter anything
  5006. which will at least partially be understood by `parse-time-string'.
  5007. Unrecognized parts of the date will default to the current day, month, year,
  5008. hour and minute. For example,
  5009. 3-2-5 --> 2003-02-05
  5010. feb 15 --> currentyear-02-15
  5011. sep 12 9 --> 2009-09-12
  5012. 12:45 --> today 12:45
  5013. 22 sept 0:34 --> currentyear-09-22 0:34
  5014. 12 --> currentyear-currentmonth-12
  5015. Fri --> nearest Friday (today or later)
  5016. etc.
  5017. The function understands only English month and weekday abbreviations,
  5018. but this can be configured with the variables `parse-time-months' and
  5019. `parse-time-weekdays'.
  5020. While prompting, a calendar is popped up - you can also select the
  5021. date with the mouse (button 1). The calendar shows a period of three
  5022. months. To scroll it to other months, use the keys `>' and `<'.
  5023. If you don't like the calendar, turn it off with
  5024. \(setq org-popup-calendar-for-date-prompt nil)
  5025. With optional argument TO-TIME, the date will immediately be converted
  5026. to an internal time.
  5027. With an optional argument WITH-TIME, the prompt will suggest to also
  5028. insert a time. Note that when WITH-TIME is not set, you can still
  5029. enter a time, and this function will inform the calling routine about
  5030. this change. The calling routine may then choose to change the format
  5031. used to insert the time stamp into the buffer to include the time."
  5032. (require 'parse-time)
  5033. (let* ((org-time-stamp-rounding-minutes
  5034. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  5035. (ct (org-current-time))
  5036. (default-time
  5037. ;; Default time is either today, or, when entering a range,
  5038. ;; the range start.
  5039. (if (save-excursion
  5040. (re-search-backward
  5041. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  5042. (- (point) 20) t))
  5043. (apply
  5044. 'encode-time
  5045. (mapcar (lambda(x) (or x 0))
  5046. (parse-time-string (match-string 1))))
  5047. ct))
  5048. (calendar-move-hook nil)
  5049. (view-diary-entries-initially nil)
  5050. (view-calendar-holidays-initially nil)
  5051. (timestr (format-time-string
  5052. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  5053. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  5054. ans ans1 ans2
  5055. second minute hour day month year tl wday wday1)
  5056. (if org-popup-calendar-for-date-prompt
  5057. (save-excursion
  5058. (save-window-excursion
  5059. (calendar)
  5060. (calendar-forward-day (- (time-to-days default-time)
  5061. (calendar-absolute-from-gregorian
  5062. (calendar-current-date))))
  5063. (org-eval-in-calendar nil)
  5064. (let* ((old-map (current-local-map))
  5065. (map (copy-keymap calendar-mode-map))
  5066. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  5067. (define-key map (kbd "RET") 'org-calendar-select)
  5068. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  5069. 'org-calendar-select-mouse)
  5070. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  5071. 'org-calendar-select-mouse)
  5072. (define-key minibuffer-local-map [(meta shift left)]
  5073. (lambda () (interactive)
  5074. (org-eval-in-calendar '(calendar-backward-month 1))))
  5075. (define-key minibuffer-local-map [(meta shift right)]
  5076. (lambda () (interactive)
  5077. (org-eval-in-calendar '(calendar-forward-month 1))))
  5078. (define-key minibuffer-local-map [(shift up)]
  5079. (lambda () (interactive)
  5080. (org-eval-in-calendar '(calendar-backward-week 1))))
  5081. (define-key minibuffer-local-map [(shift down)]
  5082. (lambda () (interactive)
  5083. (org-eval-in-calendar '(calendar-forward-week 1))))
  5084. (define-key minibuffer-local-map [(shift left)]
  5085. (lambda () (interactive)
  5086. (org-eval-in-calendar '(calendar-backward-day 1))))
  5087. (define-key minibuffer-local-map [(shift right)]
  5088. (lambda () (interactive)
  5089. (org-eval-in-calendar '(calendar-forward-day 1))))
  5090. (define-key minibuffer-local-map ">"
  5091. (lambda () (interactive)
  5092. (org-eval-in-calendar '(scroll-calendar-left 1))))
  5093. (define-key minibuffer-local-map "<"
  5094. (lambda () (interactive)
  5095. (org-eval-in-calendar '(scroll-calendar-right 1))))
  5096. (unwind-protect
  5097. (progn
  5098. (use-local-map map)
  5099. (setq ans (read-string prompt "" nil nil))
  5100. (if (not (string-match "\\S-" ans)) (setq ans nil))
  5101. (setq ans (or ans1 ans ans2)))
  5102. (use-local-map old-map)))))
  5103. ;; Naked prompt only
  5104. (setq ans (read-string prompt "" nil timestr)))
  5105. (org-detatch-overlay org-date-ovl)
  5106. (if (string-match
  5107. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  5108. (progn
  5109. (setq year (if (match-end 2)
  5110. (string-to-number (match-string 2 ans))
  5111. (string-to-number (format-time-string "%Y")))
  5112. month (string-to-number (match-string 3 ans))
  5113. day (string-to-number (match-string 4 ans)))
  5114. (if (< year 100) (setq year (+ 2000 year)))
  5115. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  5116. t nil ans))))
  5117. (setq tl (parse-time-string ans)
  5118. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  5119. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  5120. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  5121. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  5122. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  5123. second (or (nth 0 tl) 0)
  5124. wday (nth 6 tl))
  5125. (when (and wday (not (nth 3 tl)))
  5126. ;; Weekday was given, but no day, so pick that day in the week
  5127. ;; on or after the derived date.
  5128. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  5129. (unless (equal wday wday1)
  5130. (setq day (+ day (% (- wday wday1 -7) 7)))))
  5131. (if (and (boundp 'org-time-was-given)
  5132. (nth 2 tl))
  5133. (setq org-time-was-given t))
  5134. (if (< year 100) (setq year (+ 2000 year)))
  5135. (if to-time
  5136. (encode-time second minute hour day month year)
  5137. (if (or (nth 1 tl) (nth 2 tl))
  5138. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  5139. (format "%04d-%02d-%02d" year month day)))))
  5140. (defun org-eval-in-calendar (form)
  5141. "Eval FORM in the calendar window and return to current window.
  5142. Also, store the cursor date in variable ans2."
  5143. (let ((sw (selected-window)))
  5144. (select-window (get-buffer-window "*Calendar*"))
  5145. (eval form)
  5146. (when (calendar-cursor-to-date)
  5147. (let* ((date (calendar-cursor-to-date))
  5148. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5149. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  5150. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  5151. (select-window sw)))
  5152. (defun org-calendar-select ()
  5153. "Return to `org-read-date' with the date currently selected.
  5154. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5155. (interactive)
  5156. (when (calendar-cursor-to-date)
  5157. (let* ((date (calendar-cursor-to-date))
  5158. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5159. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5160. (if (active-minibuffer-window) (exit-minibuffer))))
  5161. (defun org-calendar-select-mouse (ev)
  5162. "Return to `org-read-date' with the date currently selected.
  5163. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  5164. (interactive "e")
  5165. (mouse-set-point ev)
  5166. (when (calendar-cursor-to-date)
  5167. (let* ((date (calendar-cursor-to-date))
  5168. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  5169. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  5170. (if (active-minibuffer-window) (exit-minibuffer))))
  5171. (defun org-check-deadlines (ndays)
  5172. "Check if there are any deadlines due or past due.
  5173. A deadline is considered due if it happens within `org-deadline-warning-days'
  5174. days from today's date. If the deadline appears in an entry marked DONE,
  5175. it is not shown. The prefix arg NDAYS can be used to test that many
  5176. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  5177. (interactive "P")
  5178. (let* ((org-warn-days
  5179. (cond
  5180. ((equal ndays '(4)) 100000)
  5181. (ndays (prefix-numeric-value ndays))
  5182. (t org-deadline-warning-days)))
  5183. (case-fold-search nil)
  5184. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  5185. (callback
  5186. (lambda ()
  5187. (and (let ((d1 (time-to-days (current-time)))
  5188. (d2 (time-to-days
  5189. (org-time-string-to-time (match-string 1)))))
  5190. (< (- d2 d1) org-warn-days))
  5191. (not (org-entry-is-done-p))))))
  5192. (message "%d deadlines past-due or due within %d days"
  5193. (org-occur regexp callback)
  5194. org-warn-days)))
  5195. (defun org-evaluate-time-range (&optional to-buffer)
  5196. "Evaluate a time range by computing the difference between start and end.
  5197. Normally the result is just printed in the echo area, but with prefix arg
  5198. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  5199. If the time range is actually in a table, the result is inserted into the
  5200. next column.
  5201. For time difference computation, a year is assumed to be exactly 365
  5202. days in order to avoid rounding problems."
  5203. (interactive "P")
  5204. (or
  5205. (org-clock-update-time-maybe)
  5206. (save-excursion
  5207. (unless (org-at-date-range-p)
  5208. (goto-char (point-at-bol))
  5209. (re-search-forward org-tr-regexp (point-at-eol) t))
  5210. (if (not (org-at-date-range-p))
  5211. (error "Not at a time-stamp range, and none found in current line")))
  5212. (let* ((ts1 (match-string 1))
  5213. (ts2 (match-string 2))
  5214. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  5215. (match-end (match-end 0))
  5216. (time1 (org-time-string-to-time ts1))
  5217. (time2 (org-time-string-to-time ts2))
  5218. (t1 (time-to-seconds time1))
  5219. (t2 (time-to-seconds time2))
  5220. (diff (abs (- t2 t1)))
  5221. (negative (< (- t2 t1) 0))
  5222. ;; (ys (floor (* 365 24 60 60)))
  5223. (ds (* 24 60 60))
  5224. (hs (* 60 60))
  5225. (fy "%dy %dd %02d:%02d")
  5226. (fy1 "%dy %dd")
  5227. (fd "%dd %02d:%02d")
  5228. (fd1 "%dd")
  5229. (fh "%02d:%02d")
  5230. y d h m align)
  5231. (if havetime
  5232. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5233. y 0
  5234. d (floor (/ diff ds)) diff (mod diff ds)
  5235. h (floor (/ diff hs)) diff (mod diff hs)
  5236. m (floor (/ diff 60)))
  5237. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  5238. y 0
  5239. d (floor (+ (/ diff ds) 0.5))
  5240. h 0 m 0))
  5241. (if (not to-buffer)
  5242. (message (org-make-tdiff-string y d h m))
  5243. (when (org-at-table-p)
  5244. (goto-char match-end)
  5245. (setq align t)
  5246. (and (looking-at " *|") (goto-char (match-end 0))))
  5247. (if (looking-at
  5248. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  5249. (replace-match ""))
  5250. (if negative (insert " -"))
  5251. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  5252. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  5253. (insert " " (format fh h m))))
  5254. (if align (org-table-align))
  5255. (message "Time difference inserted")))))
  5256. (defun org-make-tdiff-string (y d h m)
  5257. (let ((fmt "")
  5258. (l nil))
  5259. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  5260. l (push y l)))
  5261. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  5262. l (push d l)))
  5263. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  5264. l (push h l)))
  5265. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  5266. l (push m l)))
  5267. (apply 'format fmt (nreverse l))))
  5268. (defun org-time-string-to-time (s)
  5269. (apply 'encode-time (org-parse-time-string s)))
  5270. (defun org-parse-time-string (s &optional nodefault)
  5271. "Parse the standard Org-mode time string.
  5272. This should be a lot faster than the normal `parse-time-string'.
  5273. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  5274. hour and minute fields will be nil if not given."
  5275. (if (string-match org-ts-regexp1 s)
  5276. (list 0
  5277. (if (or (match-beginning 8) (not nodefault))
  5278. (string-to-number (or (match-string 8 s) "0")))
  5279. (if (or (match-beginning 7) (not nodefault))
  5280. (string-to-number (or (match-string 7 s) "0")))
  5281. (string-to-number (match-string 4 s))
  5282. (string-to-number (match-string 3 s))
  5283. (string-to-number (match-string 2 s))
  5284. nil nil nil)
  5285. (make-list 9 0)))
  5286. (defun org-timestamp-up (&optional arg)
  5287. "Increase the date item at the cursor by one.
  5288. If the cursor is on the year, change the year. If it is on the month or
  5289. the day, change that.
  5290. With prefix ARG, change by that many units."
  5291. (interactive "p")
  5292. (org-timestamp-change (prefix-numeric-value arg)))
  5293. (defun org-timestamp-down (&optional arg)
  5294. "Decrease the date item at the cursor by one.
  5295. If the cursor is on the year, change the year. If it is on the month or
  5296. the day, change that.
  5297. With prefix ARG, change by that many units."
  5298. (interactive "p")
  5299. (org-timestamp-change (- (prefix-numeric-value arg))))
  5300. (defun org-timestamp-up-day (&optional arg)
  5301. "Increase the date in the time stamp by one day.
  5302. With prefix ARG, change that many days."
  5303. (interactive "p")
  5304. (if (and (not (org-at-timestamp-p t))
  5305. (org-on-heading-p))
  5306. (org-todo 'up)
  5307. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  5308. (defun org-timestamp-down-day (&optional arg)
  5309. "Decrease the date in the time stamp by one day.
  5310. With prefix ARG, change that many days."
  5311. (interactive "p")
  5312. (if (and (not (org-at-timestamp-p t))
  5313. (org-on-heading-p))
  5314. (org-todo 'down)
  5315. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  5316. (defsubst org-pos-in-match-range (pos n)
  5317. (and (match-beginning n)
  5318. (<= (match-beginning n) pos)
  5319. (>= (match-end n) pos)))
  5320. (defun org-at-timestamp-p (&optional also-inactive)
  5321. "Determine if the cursor is in or at a timestamp."
  5322. (interactive)
  5323. (let* ((tsr (if also-inactive org-ts-regexp3 org-ts-regexp2))
  5324. (pos (point))
  5325. (ans (or (looking-at tsr)
  5326. (save-excursion
  5327. (skip-chars-backward "^[<\n\r\t")
  5328. (if (> (point) 1) (backward-char 1))
  5329. (and (looking-at tsr)
  5330. (> (- (match-end 0) pos) -1))))))
  5331. (and (boundp 'org-ts-what)
  5332. (setq org-ts-what
  5333. (cond
  5334. ((org-pos-in-match-range pos 2) 'year)
  5335. ((org-pos-in-match-range pos 3) 'month)
  5336. ((org-pos-in-match-range pos 7) 'hour)
  5337. ((org-pos-in-match-range pos 8) 'minute)
  5338. ((or (org-pos-in-match-range pos 4)
  5339. (org-pos-in-match-range pos 5)) 'day)
  5340. (t 'day))))
  5341. ans))
  5342. (defun org-timestamp-change (n &optional what)
  5343. "Change the date in the time stamp at point.
  5344. The date will be changed by N times WHAT. WHAT can be `day', `month',
  5345. `year', `minute', `second'. If WHAT is not given, the cursor position
  5346. in the timestamp determines what will be changed."
  5347. (let ((fmt (car org-time-stamp-formats))
  5348. org-ts-what
  5349. (pos (point))
  5350. ts time time0)
  5351. (if (not (org-at-timestamp-p t))
  5352. (error "Not at a timestamp"))
  5353. (setq org-ts-what (or what org-ts-what))
  5354. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  5355. (- (match-end 0) (match-beginning 0))))
  5356. 1)
  5357. (cdr org-time-stamp-formats)
  5358. (car org-time-stamp-formats)))
  5359. (if (= (char-after (match-beginning 0)) ?\[)
  5360. (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  5361. (setq ts (match-string 0))
  5362. (replace-match "")
  5363. (setq time0 (org-parse-time-string ts))
  5364. (setq time
  5365. (apply 'encode-time
  5366. (append
  5367. (list (or (car time0) 0))
  5368. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  5369. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  5370. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  5371. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  5372. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  5373. (nthcdr 6 time0))))
  5374. (if (eq what 'calendar)
  5375. (let ((cal-date
  5376. (save-excursion
  5377. (save-match-data
  5378. (set-buffer "*Calendar*")
  5379. (calendar-cursor-to-date)))))
  5380. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  5381. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  5382. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  5383. (setcar time0 (or (car time0) 0))
  5384. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  5385. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  5386. (setq time (apply 'encode-time time0))))
  5387. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  5388. (org-clock-update-time-maybe)
  5389. (goto-char pos)
  5390. ;; Try to recenter the calendar window, if any
  5391. (if (and org-calendar-follow-timestamp-change
  5392. (get-buffer-window "*Calendar*" t)
  5393. (memq org-ts-what '(day month year)))
  5394. (org-recenter-calendar (time-to-days time)))))
  5395. (defun org-recenter-calendar (date)
  5396. "If the calendar is visible, recenter it to DATE."
  5397. (let* ((win (selected-window))
  5398. (cwin (get-buffer-window "*Calendar*" t))
  5399. (calendar-move-hook nil))
  5400. (when cwin
  5401. (select-window cwin)
  5402. (calendar-goto-date (if (listp date) date
  5403. (calendar-gregorian-from-absolute date)))
  5404. (select-window win))))
  5405. (defun org-goto-calendar (&optional arg)
  5406. "Go to the Emacs calendar at the current date.
  5407. If there is a time stamp in the current line, go to that date.
  5408. A prefix ARG can be used to force the current date."
  5409. (interactive "P")
  5410. (let ((tsr org-ts-regexp) diff
  5411. (calendar-move-hook nil)
  5412. (view-calendar-holidays-initially nil)
  5413. (view-diary-entries-initially nil))
  5414. (if (or (org-at-timestamp-p)
  5415. (save-excursion
  5416. (beginning-of-line 1)
  5417. (looking-at (concat ".*" tsr))))
  5418. (let ((d1 (time-to-days (current-time)))
  5419. (d2 (time-to-days
  5420. (org-time-string-to-time (match-string 1)))))
  5421. (setq diff (- d2 d1))))
  5422. (calendar)
  5423. (calendar-goto-today)
  5424. (if (and diff (not arg)) (calendar-forward-day diff))))
  5425. (defun org-date-from-calendar ()
  5426. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  5427. If there is already a time stamp at the cursor position, update it."
  5428. (interactive)
  5429. (org-timestamp-change 0 'calendar))
  5430. ;;; The clock for measuring work time.
  5431. (defvar org-clock-marker (make-marker)
  5432. "Marker recording the last clock-in.")
  5433. (defun org-clock-in ()
  5434. "Start the clock on the current item.
  5435. If necessary, clock-out of the currently active clock."
  5436. (interactive)
  5437. (org-clock-out t)
  5438. (let (ts)
  5439. (save-excursion
  5440. (org-back-to-heading t)
  5441. (beginning-of-line 2)
  5442. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5443. (not (equal (match-string 1) org-clock-string)))
  5444. (beginning-of-line 1))
  5445. (insert "\n") (backward-char 1)
  5446. (indent-relative)
  5447. (insert org-clock-string " "
  5448. (setq ts (concat "[" (format-time-string
  5449. (substring
  5450. (cdr org-time-stamp-formats) 1 -1)
  5451. (current-time))
  5452. "]")))
  5453. (move-marker org-clock-marker (point))
  5454. (message "Clock started at %s" ts))))
  5455. (defun org-clock-out (&optional fail-quietly)
  5456. "Stop the currently running clock.
  5457. If there is no running clock, throw an error, unless FAIL-QUIETLY is set."
  5458. (interactive)
  5459. (catch 'exit
  5460. (if (not (marker-buffer org-clock-marker))
  5461. (if fail-quietly (throw 'exit t) (error "No active clock")))
  5462. (let (ts te s h m)
  5463. (save-excursion
  5464. (set-buffer (marker-buffer org-clock-marker))
  5465. (goto-char org-clock-marker)
  5466. (beginning-of-line 1)
  5467. (if (and (looking-at (concat "[ \t]*" org-keyword-time-regexp))
  5468. (equal (match-string 1) org-clock-string))
  5469. (setq ts (match-string 2))
  5470. (if fail-quietly (throw 'exit nil) (error "Clock start time is gone")))
  5471. (goto-char org-clock-marker)
  5472. (setq te (concat "[" (format-time-string
  5473. (substring
  5474. (cdr org-time-stamp-formats) 1 -1)
  5475. (current-time))
  5476. "]"))
  5477. (setq s (- (time-to-seconds (apply 'encode-time (org-parse-time-string te)))
  5478. (time-to-seconds (apply 'encode-time (org-parse-time-string ts))))
  5479. h (floor (/ s 3600))
  5480. s (- s (* 3600 h))
  5481. m (floor (/ s 60))
  5482. s (- s (* 60 s)))
  5483. (insert "--" te " => " (format "%2d:%02d" h m))
  5484. (move-marker org-clock-marker nil)
  5485. (message "Clock stopped at %s after HH:MM = %d:%02d" te h m)))))
  5486. (defun org-clock-cancel ()
  5487. "Cancel the running clock be removing the start timestamp."
  5488. (interactive)
  5489. (if (not (marker-buffer org-clock-marker))
  5490. (error "No active clock"))
  5491. (save-excursion
  5492. (set-buffer (marker-buffer org-clock-marker))
  5493. (goto-char org-clock-marker)
  5494. (delete-region (1- (point-at-bol)) (point-at-eol)))
  5495. (message "Clock canceled"))
  5496. (defvar org-clock-file-total-minutes nil
  5497. "Holds the file total time in minutes, after a call to `org-clock-sum'.")
  5498. (make-variable-buffer-local 'org-clock-file-total-minutes)
  5499. (defun org-clock-sum (&optional tstart tend)
  5500. "Sum the times for each subtree.
  5501. Puts the resulting times in minutes as a text property on each headline."
  5502. (interactive)
  5503. (let* ((bmp (buffer-modified-p))
  5504. (re (concat "^\\(\\*+\\)[ \t]\\|^[ \t]*"
  5505. org-clock-string
  5506. "[ \t]*\\(\\[.*?\\]\\)-+\\(\\[.*?\\]\\)"))
  5507. (lmax 30)
  5508. (ltimes (make-vector lmax 0))
  5509. (t1 0)
  5510. (level 0)
  5511. ts te dt
  5512. time)
  5513. (remove-text-properties (point-min) (point-max) '(:org-clock-minutes t))
  5514. (save-excursion
  5515. (goto-char (point-max))
  5516. (while (re-search-backward re nil t)
  5517. (if (match-end 2)
  5518. ;; A time
  5519. (setq ts (match-string 2)
  5520. te (match-string 3)
  5521. ts (time-to-seconds
  5522. (apply 'encode-time (org-parse-time-string ts)))
  5523. te (time-to-seconds
  5524. (apply 'encode-time (org-parse-time-string te)))
  5525. ts (if tstart (max ts tstart) ts)
  5526. te (if tend (min te tend) te)
  5527. dt (- te ts)
  5528. t1 (if (> dt 0) (+ t1 (floor (/ dt 60))) t1))
  5529. ;; A headline
  5530. (setq level (- (match-end 1) (match-beginning 1)))
  5531. (when (or (> t1 0) (> (aref ltimes level) 0))
  5532. (loop for l from 0 to level do
  5533. (aset ltimes l (+ (aref ltimes l) t1)))
  5534. (setq t1 0 time (aref ltimes level))
  5535. (loop for l from level to (1- lmax) do
  5536. (aset ltimes l 0))
  5537. (goto-char (match-beginning 0))
  5538. (put-text-property (point) (point-at-eol) :org-clock-minutes time))))
  5539. (setq org-clock-file-total-minutes (aref ltimes 0)))
  5540. (set-buffer-modified-p bmp)))
  5541. (defun org-clock-display (&optional total-only)
  5542. "Show subtree times in the entire buffer.
  5543. If TOTAL-ONLY is non-nil, only show the total time for the entire file
  5544. in the echo area."
  5545. (interactive)
  5546. (org-remove-clock-overlays)
  5547. (let (time h m p)
  5548. (org-clock-sum)
  5549. (unless total-only
  5550. (save-excursion
  5551. (goto-char (point-min))
  5552. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5553. (goto-char p)
  5554. (when (setq time (get-text-property p :org-clock-minutes))
  5555. (org-put-clock-overlay time (funcall outline-level))))
  5556. (setq h (/ org-clock-file-total-minutes 60)
  5557. m (- org-clock-file-total-minutes (* 60 h)))
  5558. ;; Arrange to remove the overlays upon next change.
  5559. (org-add-hook 'before-change-functions 'org-remove-clock-overlays
  5560. nil 'local)))
  5561. (message "Total file time: %d:%02d (%d hours and %d minutes)" h m h m)))
  5562. (defvar org-clock-overlays nil)
  5563. (make-variable-buffer-local 'org-clock-overlays)
  5564. (defun org-put-clock-overlay (time &optional level)
  5565. "Put an overlays on the current line, displaying TIME.
  5566. If LEVEL is given, prefix time with a corresponding number of stars.
  5567. This creates a new overlay and stores it in `org-clock-overlays', so that it
  5568. will be easy to remove."
  5569. (let* ((c 60) (h (floor (/ time 60))) (m (- time (* 60 h)))
  5570. (l (if level (org-get-legal-level level 0) 0))
  5571. (off 0)
  5572. ov tx)
  5573. (move-to-column c)
  5574. (unless (eolp) (skip-chars-backward "^ \t"))
  5575. (skip-chars-backward " \t")
  5576. (setq ov (org-make-overlay (1- (point)) (point-at-eol))
  5577. tx (concat (buffer-substring (1- (point)) (point))
  5578. (make-string (+ off (max 0 (- c (current-column)))) ?.)
  5579. (org-add-props (format "%s %2d:%02d%s"
  5580. (make-string l ?*) h m
  5581. (make-string (- 10 l) ?\ ))
  5582. '(face secondary-selection))
  5583. ""))
  5584. (org-overlay-put ov 'display tx)
  5585. (push ov org-clock-overlays)))
  5586. (defun org-remove-clock-overlays (&optional beg end noremove)
  5587. "Remove the occur highlights from the buffer.
  5588. BEG and END are ignored. If NOREMOVE is nil, remove this function
  5589. from the `before-change-functions' in the current buffer."
  5590. (interactive)
  5591. (unless org-inhibit-highlight-removal
  5592. (mapc 'org-delete-overlay org-clock-overlays)
  5593. (setq org-clock-overlays nil)
  5594. (unless noremove
  5595. (remove-hook 'before-change-functions
  5596. 'org-remove-clock-overlays 'local))))
  5597. (defun org-clock-out-if-current ()
  5598. "Clock out if the current entry contains the running clock.
  5599. This is used to stop the clock after a TODO entry is marked DONE."
  5600. (when (and (equal state org-done-string)
  5601. (equal (marker-buffer org-clock-marker) (current-buffer))
  5602. (< (point) org-clock-marker)
  5603. (> (save-excursion (outline-next-heading) (point))
  5604. org-clock-marker))
  5605. (org-clock-out)))
  5606. (add-hook 'org-after-todo-state-change-hook
  5607. 'org-clock-out-if-current)
  5608. (defun org-check-running-clock ()
  5609. "Check if the current buffer contains the running clock.
  5610. If yes, offer to stop it and to save the buffer with the changes."
  5611. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  5612. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  5613. (buffer-name))))
  5614. (org-clock-out)
  5615. (when (y-or-n-p "Save changed buffer?")
  5616. (save-buffer))))
  5617. (defun org-clock-report ()
  5618. "Create a table containing a report about clocked time.
  5619. If the buffer contains lines
  5620. #+BEGIN: clocktable :maxlevel 3 :emphasize nil
  5621. #+END: clocktable
  5622. then the table will be inserted between these lines, replacing whatever
  5623. is was there before. If these lines are not in the buffer, the table
  5624. is inserted at point, surrounded by the special lines.
  5625. The BEGIN line can contain parameters. Allowed are:
  5626. :maxlevel The maximum level to be included in the table. Default is 3.
  5627. :emphasize t/nil, if levell 1 and level 2 should be bold/italic in the table."
  5628. (interactive)
  5629. (org-remove-clock-overlays)
  5630. (unless (org-find-dblock "clocktable")
  5631. (org-create-dblock (list :name "clocktable"
  5632. :maxlevel 2 :emphasize nil)))
  5633. (org-update-dblock))
  5634. (defun org-clock-update-time-maybe ()
  5635. "If this is a CLOCK line, update it and return t.
  5636. Otherwise, return nil."
  5637. (interactive)
  5638. (save-excursion
  5639. (beginning-of-line 1)
  5640. (skip-chars-forward " \t")
  5641. (when (looking-at org-clock-string)
  5642. (let ((re (concat "[ \t]*" org-clock-string
  5643. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  5644. "\\([ \t]*=>.*\\)?"))
  5645. ts te h m s)
  5646. (if (not (looking-at re))
  5647. nil
  5648. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  5649. (end-of-line 1)
  5650. (setq ts (match-string 1)
  5651. te (match-string 2))
  5652. (setq s (- (time-to-seconds
  5653. (apply 'encode-time (org-parse-time-string te)))
  5654. (time-to-seconds
  5655. (apply 'encode-time (org-parse-time-string ts))))
  5656. h (floor (/ s 3600))
  5657. s (- s (* 3600 h))
  5658. m (floor (/ s 60))
  5659. s (- s (* 60 s)))
  5660. (insert " => " (format "%2d:%02d" h m))
  5661. t)))))
  5662. (defun org-clock-special-range (key &optional time as-strings)
  5663. "Return two times bordering a special time range.
  5664. Key is a symbol specifying the range and can be one of `today', `yesterday',
  5665. `thisweek', `lastweek', `thismonth', `lastmonth', `thisyear', `lastyear'.
  5666. A week starts Monday 0:00 and ends Sunday 24:00.
  5667. The range is determined relative to TIME. TIME defaults to the current time.
  5668. The return value is a cons cell with two internal times like the ones
  5669. returned by `current time' or `encode-time'. if AS-STRINGS is non-nil,
  5670. the returned times will be formatted strings."
  5671. (let* ((tm (decode-time (or time (current-time))))
  5672. (s 0) (m (nth 1 tm)) (h (nth 2 tm))
  5673. (d (nth 3 tm)) (month (nth 4 tm)) (y (nth 5 tm))
  5674. (dow (nth 6 tm))
  5675. s1 m1 h1 d1 month1 y1 diff ts te fm)
  5676. (cond
  5677. ((eq key 'today)
  5678. (setq h 0 m 0 h1 24 m1 0))
  5679. ((eq key 'yesterday)
  5680. (setq d (1- d) h 0 m 0 h1 24 m1 0))
  5681. ((eq key 'thisweek)
  5682. (setq diff (if (= dow 0) 6 (1- dow))
  5683. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5684. ((eq key 'lastweek)
  5685. (setq diff (+ 7 (if (= dow 0) 6 (1- dow)))
  5686. m 0 h 0 d (- d diff) d1 (+ 7 d)))
  5687. ((eq key 'thismonth)
  5688. (setq d 1 h 0 m 0 d1 1 month1 (1+ month) h1 0 m1 0))
  5689. ((eq key 'lastmonth)
  5690. (setq d 1 h 0 m 0 d1 1 month (1- month) month1 (1+ month) h1 0 m1 0))
  5691. ((eq key 'thisyear)
  5692. (setq m 0 h 0 d 1 month 1 y1 (1+ y)))
  5693. ((eq key 'lastyear)
  5694. (setq m 0 h 0 d 1 month 1 y (1- y) y1 (1+ y)))
  5695. (t (error "No such time block %s" key)))
  5696. (setq ts (encode-time s m h d month y)
  5697. te (encode-time (or s1 s) (or m1 m) (or h1 h)
  5698. (or d1 d) (or month1 month) (or y1 y)))
  5699. (setq fm (cdr org-time-stamp-formats))
  5700. (if as-strings
  5701. (cons (format-time-string fm ts) (format-time-string fm te))
  5702. (cons ts te))))
  5703. (defun org-dblock-write:clocktable (params)
  5704. "Write the standard clocktable."
  5705. (let ((hlchars '((1 . "*") (2 . ?/)))
  5706. (emph nil)
  5707. (ins (make-marker))
  5708. ipos time h m p level hlc hdl maxlevel
  5709. ts te cc block)
  5710. (setq maxlevel (or (plist-get params :maxlevel) 3)
  5711. emph (plist-get params :emphasize)
  5712. ts (plist-get params :tstart)
  5713. te (plist-get params :tend)
  5714. block (plist-get params :block))
  5715. (when block
  5716. (setq cc (org-clock-special-range block nil t)
  5717. ts (car cc) te (cdr cc)))
  5718. (if ts (setq ts (time-to-seconds
  5719. (apply 'encode-time (org-parse-time-string ts)))))
  5720. (if te (setq te (time-to-seconds
  5721. (apply 'encode-time (org-parse-time-string te)))))
  5722. (move-marker ins (point))
  5723. (setq ipos (point))
  5724. (insert-before-markers "Clock summary at ["
  5725. (substring
  5726. (format-time-string (cdr org-time-stamp-formats))
  5727. 1 -1)
  5728. "]."
  5729. (if block
  5730. (format " Considered range is /%s/." block)
  5731. "")
  5732. "\n\n|L|Headline|Time|\n")
  5733. (org-clock-sum ts te)
  5734. (setq h (/ org-clock-file-total-minutes 60)
  5735. m (- org-clock-file-total-minutes (* 60 h)))
  5736. (insert-before-markers "|-\n|0|" "*Total file time*| "
  5737. (format "*%d:%02d*" h m)
  5738. "|\n")
  5739. (goto-char (point-min))
  5740. (while (setq p (next-single-property-change (point) :org-clock-minutes))
  5741. (goto-char p)
  5742. (when (setq time (get-text-property p :org-clock-minutes))
  5743. (beginning-of-line 1)
  5744. (when (and (looking-at "\\(\\*+\\)[ \t]+\\(.*?\\)\\([ \t]+:[0-9a-zA-Z_@:]+:\\)?[ \t]*$")
  5745. (setq level (- (match-end 1) (match-beginning 1)))
  5746. (<= level maxlevel))
  5747. (setq hlc (if emph (or (cdr (assoc level hlchars)) "") "")
  5748. hdl (match-string 2)
  5749. h (/ time 60)
  5750. m (- time (* 60 h)))
  5751. (save-excursion
  5752. (goto-char ins)
  5753. (if (= level 1) (insert-before-markers "|-\n"))
  5754. (insert-before-markers
  5755. "| " (int-to-string level) "|" hlc hdl hlc " |"
  5756. (make-string (1- level) ?|)
  5757. hlc
  5758. (format "%d:%02d" h m)
  5759. hlc
  5760. " |\n")))))
  5761. (goto-char ins)
  5762. (backward-delete-char 1)
  5763. (goto-char ipos)
  5764. (skip-chars-forward "^|")
  5765. (org-table-align)))
  5766. (defun org-collect-clock-time-entries ()
  5767. "Return an internal list with clocking information.
  5768. This list has one entry for each CLOCK interval.
  5769. FIXME: describe the elements."
  5770. (interactive)
  5771. (let ((re (concat "^[ \t]*" org-clock-string
  5772. " *\\[\\(.*?\\)\\]--\\[\\(.*?\\)\\]"))
  5773. rtn beg end next cont level title total closedp leafp
  5774. clockpos titlepos h m donep)
  5775. (save-excursion
  5776. (org-clock-sum)
  5777. (goto-char (point-min))
  5778. (while (re-search-forward re nil t)
  5779. (setq clockpos (match-beginning 0)
  5780. beg (match-string 1) end (match-string 2)
  5781. cont (match-end 0))
  5782. (setq beg (apply 'encode-time (org-parse-time-string beg))
  5783. end (apply 'encode-time (org-parse-time-string end)))
  5784. (org-back-to-heading t)
  5785. (setq donep (org-entry-is-done-p))
  5786. (setq titlepos (point)
  5787. total (or (get-text-property (1+ (point)) :org-clock-minutes) 0)
  5788. h (/ total 60) m (- total (* 60 h))
  5789. total (cons h m))
  5790. (looking-at "\\(\\*+\\) +\\(.*\\)")
  5791. (setq level (- (match-end 1) (match-beginning 1))
  5792. title (org-match-string-no-properties 2))
  5793. (save-excursion (outline-next-heading) (setq next (point)))
  5794. (setq closedp (re-search-forward org-closed-time-regexp next t))
  5795. (goto-char next)
  5796. (setq leafp (and (looking-at "^\\*+ ")
  5797. (<= (- (match-end 0) (point)) level)))
  5798. (push (list beg end clockpos closedp donep
  5799. total title titlepos level leafp)
  5800. rtn)
  5801. (goto-char cont)))
  5802. (nreverse rtn)))
  5803. ;;; Agenda, and Diary Integration
  5804. ;;; Define the mode
  5805. (defvar org-agenda-mode-map (make-sparse-keymap)
  5806. "Keymap for `org-agenda-mode'.")
  5807. (defvar org-agenda-menu) ; defined later in this file.
  5808. (defvar org-agenda-follow-mode nil)
  5809. (defvar org-agenda-show-log nil)
  5810. (defvar org-agenda-redo-command nil)
  5811. (defvar org-agenda-mode-hook nil)
  5812. (defvar org-agenda-type nil)
  5813. (defvar org-agenda-force-single-file nil)
  5814. (defun org-agenda-mode ()
  5815. "Mode for time-sorted view on action items in Org-mode files.
  5816. The following commands are available:
  5817. \\{org-agenda-mode-map}"
  5818. (interactive)
  5819. (kill-all-local-variables)
  5820. (setq major-mode 'org-agenda-mode)
  5821. (setq mode-name "Org-Agenda")
  5822. (use-local-map org-agenda-mode-map)
  5823. (easy-menu-add org-agenda-menu)
  5824. (if org-startup-truncated (setq truncate-lines t))
  5825. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  5826. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  5827. (unless org-agenda-keep-modes
  5828. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  5829. org-agenda-show-log nil))
  5830. (easy-menu-change
  5831. '("Agenda") "Agenda Files"
  5832. (append
  5833. (list
  5834. (vector
  5835. (if (get 'org-agenda-files 'org-restrict)
  5836. "Restricted to single file"
  5837. "Edit File List")
  5838. '(org-edit-agenda-file-list)
  5839. (not (get 'org-agenda-files 'org-restrict)))
  5840. "--")
  5841. (mapcar 'org-file-menu-entry (org-agenda-files))))
  5842. (org-agenda-set-mode-name)
  5843. (apply
  5844. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  5845. (list 'org-agenda-mode-hook)))
  5846. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  5847. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  5848. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  5849. (define-key org-agenda-mode-map " " 'org-agenda-show)
  5850. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  5851. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  5852. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  5853. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  5854. (define-key org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
  5855. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  5856. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  5857. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  5858. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  5859. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  5860. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  5861. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  5862. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  5863. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  5864. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  5865. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  5866. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  5867. (while l (define-key org-agenda-mode-map
  5868. (int-to-string (pop l)) 'digit-argument)))
  5869. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  5870. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  5871. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  5872. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  5873. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  5874. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  5875. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  5876. (define-key org-agenda-mode-map "s" 'org-save-all-org-buffers)
  5877. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  5878. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  5879. (define-key org-agenda-mode-map "n" 'next-line)
  5880. (define-key org-agenda-mode-map "p" 'previous-line)
  5881. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  5882. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  5883. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  5884. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  5885. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  5886. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  5887. (eval-after-load "calendar"
  5888. '(define-key calendar-mode-map org-calendar-to-agenda-key
  5889. 'org-calendar-goto-agenda))
  5890. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  5891. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  5892. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  5893. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  5894. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  5895. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  5896. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  5897. (define-key org-agenda-mode-map "I" 'org-agenda-clock-in)
  5898. (define-key org-agenda-mode-map "O" 'org-clock-out)
  5899. (define-key org-agenda-mode-map "X" 'org-clock-cancel)
  5900. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  5901. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  5902. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  5903. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  5904. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  5905. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  5906. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  5907. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  5908. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  5909. "Local keymap for agenda entries from Org-mode.")
  5910. (define-key org-agenda-keymap
  5911. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  5912. (define-key org-agenda-keymap
  5913. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  5914. (when org-agenda-mouse-1-follows-link
  5915. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  5916. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  5917. '("Agenda"
  5918. ("Agenda Files")
  5919. "--"
  5920. ["Show" org-agenda-show t]
  5921. ["Go To (other window)" org-agenda-goto t]
  5922. ["Go To (one window)" org-agenda-switch-to t]
  5923. ["Follow Mode" org-agenda-follow-mode
  5924. :style toggle :selected org-agenda-follow-mode :active t]
  5925. "--"
  5926. ["Cycle TODO" org-agenda-todo t]
  5927. ("Tags"
  5928. ["Show all Tags" org-agenda-show-tags t]
  5929. ["Set Tags" org-agenda-set-tags t])
  5930. ("Schedule"
  5931. ["Schedule" org-agenda-schedule t]
  5932. ["Set Deadline" org-agenda-deadline t]
  5933. "--"
  5934. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  5935. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  5936. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  5937. ("Priority"
  5938. ["Set Priority" org-agenda-priority t]
  5939. ["Increase Priority" org-agenda-priority-up t]
  5940. ["Decrease Priority" org-agenda-priority-down t]
  5941. ["Show Priority" org-agenda-show-priority t])
  5942. "--"
  5943. ;; ["New agenda command" org-agenda t]
  5944. ["Rebuild buffer" org-agenda-redo t]
  5945. ["Save all Org-mode Buffers" org-save-all-org-buffers t]
  5946. "--"
  5947. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  5948. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  5949. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  5950. "--"
  5951. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  5952. :style radio :selected (equal org-agenda-ndays 1)]
  5953. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  5954. :style radio :selected (equal org-agenda-ndays 7)]
  5955. "--"
  5956. ["Show Logbook entries" org-agenda-log-mode
  5957. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  5958. ["Include Diary" org-agenda-toggle-diary
  5959. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  5960. ["Use Time Grid" org-agenda-toggle-time-grid
  5961. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  5962. "--"
  5963. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  5964. ("Calendar Commands"
  5965. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  5966. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  5967. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  5968. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  5969. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  5970. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  5971. "--"
  5972. ["Quit" org-agenda-quit t]
  5973. ["Exit and Release Buffers" org-agenda-exit t]
  5974. ))
  5975. (defvar org-agenda-restrict nil)
  5976. (defvar org-agenda-restrict-begin (make-marker))
  5977. (defvar org-agenda-restrict-end (make-marker))
  5978. (defvar org-agenda-last-dispatch-buffer nil)
  5979. ;;;###autoload
  5980. (defun org-agenda (arg)
  5981. "Dispatch agenda commands to collect entries to the agenda buffer.
  5982. Prompts for a character to select a command. Any prefix arg will be passed
  5983. on to the selected command. The default selections are:
  5984. g
  5985. a Call `org-agenda-list' to display the agenda for current day or week.
  5986. t Call `org-todo-list' to display the global todo list.
  5987. T Call `org-todo-list' to display the global todo list, select only
  5988. entries with a specific TODO keyword (the user gets a prompt).
  5989. m Call `org-tags-view' to display headlines with tags matching
  5990. a condition (the user is prompted for the condition).
  5991. M Like `m', but select only TODO entries, no ordinary headlines.
  5992. l Create a timeeline for the current buffer.
  5993. More commands can be added by configuring the variable
  5994. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  5995. searches can be pre-defined in this way.
  5996. If the current buffer is in Org-mode and visiting a file, you can also
  5997. first press `1' to indicate that the agenda should be temporarily (until the
  5998. next use of \\[org-agenda]) restricted to the current file."
  5999. (interactive "P")
  6000. (catch 'exit
  6001. (let ((restrict-ok (and buffer-file-name (org-mode-p)))
  6002. (buf (current-buffer))
  6003. (bfn buffer-file-name)
  6004. (custom org-agenda-custom-commands)
  6005. c entry key type match lprops)
  6006. ;; Turn off restriction
  6007. (put 'org-agenda-files 'org-restrict nil)
  6008. (setq org-agenda-restrict nil)
  6009. (move-marker org-agenda-restrict-begin nil)
  6010. (move-marker org-agenda-restrict-end nil)
  6011. ;; Remember where this call originated
  6012. (setq org-agenda-last-dispatch-buffer (current-buffer))
  6013. (save-window-excursion
  6014. (delete-other-windows)
  6015. (switch-to-buffer-other-window " *Agenda Commands*")
  6016. (erase-buffer)
  6017. (insert
  6018. "Press key for an agenda command:
  6019. --------------------------------
  6020. a Agenda for current week or day
  6021. t List of all TODO entries T Entries with special TODO kwd
  6022. m Match a TAGS query M Like m, but only TODO entries
  6023. L Timeline for current buffer C Configure custom agenda commands")
  6024. (while (setq entry (pop custom))
  6025. (setq key (car entry) type (nth 1 entry) match (nth 2 entry))
  6026. (insert (format "\n%-4s%-14s: %s"
  6027. key
  6028. (cond
  6029. ((stringp type) type)
  6030. ((eq type 'tags) "Tags query")
  6031. ((eq type 'todo) "TODO keyword")
  6032. ((eq type 'tags-tree) "Tags tree")
  6033. ((eq type 'todo-tree) "TODO kwd tree")
  6034. ((eq type 'occur-tree) "Occur tree")
  6035. (t "???"))
  6036. (if (stringp match)
  6037. (org-add-props match nil 'face 'org-warning)
  6038. (format "set of %d commands" (+ -2 (length entry)))))))
  6039. (if restrict-ok
  6040. (insert "\n"
  6041. (org-add-props "1 Restrict call to current buffer 0 Restrict call to region or subtree" nil 'face 'org-table)))
  6042. (goto-char (point-min))
  6043. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  6044. (message "Press key for agenda command%s"
  6045. (if restrict-ok ", or [1] or [0] to restrict" ""))
  6046. (setq c (read-char-exclusive))
  6047. (message "")
  6048. (when (memq c '(?L ?1 ?0))
  6049. (if restrict-ok
  6050. (put 'org-agenda-files 'org-restrict (list bfn))
  6051. (error "Cannot restrict agenda to current buffer"))
  6052. (with-current-buffer " *Agenda Commands*"
  6053. (goto-char (point-max))
  6054. (delete-region (point-at-bol) (point))
  6055. (goto-char (point-min)))
  6056. (when (eq c ?0)
  6057. (setq org-agenda-restrict t)
  6058. (with-current-buffer buf
  6059. (if (org-region-active-p)
  6060. (progn
  6061. (move-marker org-agenda-restrict-begin (region-beginning))
  6062. (move-marker org-agenda-restrict-end (region-end)))
  6063. (save-excursion
  6064. (org-back-to-heading t)
  6065. (move-marker org-agenda-restrict-begin (point))
  6066. (move-marker org-agenda-restrict-end
  6067. (progn (org-end-of-subtree t)))))))
  6068. (unless (eq c ?L)
  6069. (message "Press key for agenda command%s"
  6070. (if restrict-ok " (restricted to current file)" ""))
  6071. (setq c (read-char-exclusive)))
  6072. (message "")))
  6073. (require 'calendar) ; FIXME: can we avoid this for some commands?
  6074. ;; For example the todo list should not need it (but does...)
  6075. (cond
  6076. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  6077. ((equal c ?a) (call-interactively 'org-agenda-list))
  6078. ((equal c ?t) (call-interactively 'org-todo-list))
  6079. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  6080. ((equal c ?m) (call-interactively 'org-tags-view))
  6081. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  6082. ((equal c ?L)
  6083. (unless restrict-ok
  6084. (error "This is not an Org-mode file"))
  6085. (org-call-with-arg 'org-timeline arg))
  6086. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  6087. (if (symbolp (nth 1 entry))
  6088. (progn
  6089. (setq type (nth 1 entry) match (nth 2 entry) lprops (nth 3 entry)
  6090. lprops (nth 3 entry))
  6091. (cond
  6092. ((eq type 'tags)
  6093. (org-let lprops '(org-tags-view current-prefix-arg match)))
  6094. ((eq type 'tags-todo)
  6095. (org-let lprops '(org-tags-view '(4) match)))
  6096. ((eq type 'todo)
  6097. (org-let lprops '(org-todo-list match)))
  6098. ((eq type 'tags-tree)
  6099. (org-check-for-org-mode)
  6100. (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
  6101. ((eq type 'todo-tree)
  6102. (org-check-for-org-mode)
  6103. (org-let lprops
  6104. '(org-occur (concat "^" outline-regexp "[ \t]*"
  6105. (regexp-quote match) "\\>"))))
  6106. ((eq type 'occur-tree)
  6107. (org-check-for-org-mode)
  6108. (org-let lprops '(org-occur match)))
  6109. (t (error "Invalid custom agenda command type %s" type))))
  6110. (org-run-agenda-series (cddr entry))))
  6111. (t (error "Invalid key"))))))
  6112. (defun org-run-agenda-series (series &optional window)
  6113. (org-prepare-agenda)
  6114. (let* ((org-agenda-multi t)
  6115. (redo (list 'org-run-agenda-series (list 'quote series)))
  6116. (org-select-agenda-window t)
  6117. (cmds (car series))
  6118. (gprops (nth 1 series))
  6119. cmd type match lprops)
  6120. (while (setq cmd (pop cmds))
  6121. (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
  6122. (cond
  6123. ((eq type 'agenda)
  6124. (call-interactively 'org-agenda-list))
  6125. ((eq type 'alltodo)
  6126. (call-interactively 'org-todo-list))
  6127. ((eq type 'tags)
  6128. (org-let2 gprops lprops
  6129. '(org-tags-view current-prefix-arg match)))
  6130. ((eq type 'tags-todo)
  6131. (org-let2 gprops lprops
  6132. '(org-tags-view '(4) match)))
  6133. ((eq type 'todo)
  6134. (org-let2 gprops lprops
  6135. '(org-todo-list match)))
  6136. (t (error "Invalid type in command series"))))
  6137. (widen)
  6138. (setq org-agenda-redo-command redo)
  6139. (goto-char (point-min))))
  6140. (defun org-check-for-org-mode ()
  6141. "Make sure current buffer is in org-mode. Error if not."
  6142. (or (org-mode-p)
  6143. (error "Cannot execute org-mode agenda command on buffer in %s."
  6144. major-mode)))
  6145. (defun org-fit-agenda-window ()
  6146. "Fit the window to the buffer size."
  6147. (and org-fit-agenda-window
  6148. (fboundp 'fit-window-to-buffer)
  6149. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  6150. (/ (frame-height) 2))))
  6151. (defun org-agenda-files (&optional unrestricted)
  6152. "Get the list of agenda files.
  6153. Optional UNRESTRICTED means return the full list even if a restriction
  6154. is currently in place."
  6155. (cond
  6156. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  6157. ((stringp org-agenda-files) (org-read-agenda-file-list))
  6158. ((listp org-agenda-files) org-agenda-files)
  6159. (t (error "Invalid value of `org-agenda-files'"))))
  6160. (defvar org-window-configuration)
  6161. (defun org-edit-agenda-file-list ()
  6162. "Edit the list of agenda files.
  6163. Depending on setup, this either uses customize to edit the variable
  6164. `org-agenda-files', or it visits the file that is holding the list. In the
  6165. latter case, the buffer is set up in a way that saving it automatically kills
  6166. the buffer and restores the previous window configuration."
  6167. (interactive)
  6168. (if (stringp org-agenda-files)
  6169. (let ((cw (current-window-configuration)))
  6170. (find-file org-agenda-files)
  6171. (org-set-local 'org-window-configuration cw)
  6172. (org-add-hook 'after-save-hook
  6173. (lambda ()
  6174. (set-window-configuration
  6175. (prog1 org-window-configuration
  6176. (kill-buffer (current-buffer))))
  6177. (org-install-agenda-files-menu)
  6178. (message "New agenda file list installed"))
  6179. nil 'local)
  6180. (message (substitute-command-keys
  6181. "Edit list and finish with \\[save-buffer]")))
  6182. (customize-variable 'org-agenda-files)))
  6183. (defun org-store-new-agenda-file-list (list)
  6184. "Set new value for the agenda file list and save it correcly."
  6185. (if (stringp org-agenda-files)
  6186. (let ((f org-agenda-files) b)
  6187. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  6188. (with-temp-file f
  6189. (insert (mapconcat 'identity list "\n") "\n")))
  6190. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  6191. (setq org-agenda-files list)
  6192. (customize-save-variable 'org-agenda-files org-agenda-files))))
  6193. (defun org-read-agenda-file-list ()
  6194. "Read the list of agenda files from a file."
  6195. (when (stringp org-agenda-files)
  6196. (with-temp-buffer
  6197. (insert-file-contents org-agenda-files)
  6198. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  6199. (defvar org-agenda-markers nil
  6200. "List of all currently active markers created by `org-agenda'.")
  6201. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  6202. "Creation time of the last agenda marker.")
  6203. (defun org-agenda-new-marker (&optional pos)
  6204. "Return a new agenda marker.
  6205. Org-mode keeps a list of these markers and resets them when they are
  6206. no longer in use."
  6207. (let ((m (copy-marker (or pos (point)))))
  6208. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  6209. (push m org-agenda-markers)
  6210. m))
  6211. (defun org-agenda-maybe-reset-markers (&optional force)
  6212. "Reset markers created by `org-agenda'. But only if they are old enough."
  6213. (if (or (and force (not org-agenda-multi))
  6214. (> (- (time-to-seconds (current-time))
  6215. org-agenda-last-marker-time)
  6216. 5))
  6217. (while org-agenda-markers
  6218. (move-marker (pop org-agenda-markers) nil))))
  6219. (defvar org-agenda-new-buffers nil
  6220. "Buffers created to visit agenda files.")
  6221. (defun org-get-agenda-file-buffer (file)
  6222. "Get a buffer visiting FILE. If the buffer needs to be created, add
  6223. it to the list of buffers which might be released later."
  6224. (let ((buf (find-buffer-visiting file)))
  6225. (if buf
  6226. buf ; just return it
  6227. ;; Make a new buffer and remember it
  6228. (setq buf (find-file-noselect file))
  6229. (if buf (push buf org-agenda-new-buffers))
  6230. buf)))
  6231. (defun org-release-buffers (blist)
  6232. "Release all buffers in list, asking the user for confirmation when needed.
  6233. When a buffer is unmodified, it is just killed. When modified, it is saved
  6234. \(if the user agrees) and then killed."
  6235. (let (buf file)
  6236. (while (setq buf (pop blist))
  6237. (setq file (buffer-file-name buf))
  6238. (when (and (buffer-modified-p buf)
  6239. file
  6240. (y-or-n-p (format "Save file %s? " file)))
  6241. (with-current-buffer buf (save-buffer)))
  6242. (kill-buffer buf))))
  6243. (defun org-timeline (&optional include-all keep-modes)
  6244. "Show a time-sorted view of the entries in the current org file.
  6245. Only entries with a time stamp of today or later will be listed. With
  6246. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  6247. under the current date.
  6248. If the buffer contains an active region, only check the region for
  6249. dates."
  6250. (interactive "P")
  6251. (require 'calendar)
  6252. (org-compile-prefix-format 'timeline)
  6253. (org-set-sorting-strategy 'timeline)
  6254. (let* ((dopast t)
  6255. (dotodo include-all)
  6256. (doclosed org-agenda-show-log)
  6257. (org-agenda-keep-modes keep-modes)
  6258. (entry buffer-file-name)
  6259. (date (calendar-current-date))
  6260. (win (selected-window))
  6261. (pos1 (point))
  6262. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  6263. (end (if (org-region-active-p) (region-end) (point-max)))
  6264. (day-numbers (org-get-all-dates beg end 'no-ranges
  6265. t doclosed ; always include today
  6266. org-timeline-show-empty-dates))
  6267. (today (time-to-days (current-time)))
  6268. (past t)
  6269. args
  6270. s e rtn d emptyp)
  6271. (setq org-agenda-redo-command
  6272. (list 'progn
  6273. (list 'switch-to-buffer-other-window (current-buffer))
  6274. (list 'org-timeline (list 'quote include-all) t)))
  6275. (if (not dopast)
  6276. ;; Remove past dates from the list of dates.
  6277. (setq day-numbers (delq nil (mapcar (lambda(x)
  6278. (if (>= x today) x nil))
  6279. day-numbers))))
  6280. (org-prepare-agenda)
  6281. (if doclosed (push :closed args))
  6282. (push :timestamp args)
  6283. (if dotodo (push :todo args))
  6284. (while (setq d (pop day-numbers))
  6285. (if (and (listp d) (eq (car d) :omitted))
  6286. (progn
  6287. (setq s (point))
  6288. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  6289. (put-text-property s (1- (point)) 'face 'org-level-3))
  6290. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  6291. (if (and (>= d today)
  6292. dopast
  6293. past)
  6294. (progn
  6295. (setq past nil)
  6296. (insert (make-string 79 ?-) "\n")))
  6297. (setq date (calendar-gregorian-from-absolute d))
  6298. (setq s (point))
  6299. (setq rtn (and (not emptyp)
  6300. (apply 'org-agenda-get-day-entries
  6301. entry date args)))
  6302. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  6303. (progn
  6304. (insert (calendar-day-name date) " "
  6305. (number-to-string (extract-calendar-day date)) " "
  6306. (calendar-month-name (extract-calendar-month date)) " "
  6307. (number-to-string (extract-calendar-year date)) "\n")
  6308. (put-text-property s (1- (point)) 'face
  6309. 'org-level-3)
  6310. (if (equal d today)
  6311. (put-text-property s (1- (point)) 'org-today t))
  6312. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  6313. (put-text-property s (1- (point)) 'day d)))))
  6314. (goto-char (point-min))
  6315. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  6316. (point-min)))
  6317. (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
  6318. (org-agenda-align-tags)
  6319. (setq buffer-read-only t)
  6320. (when (not org-select-agenda-window)
  6321. (select-window win)
  6322. (goto-char pos1))))
  6323. (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
  6324. (defvar org-agenda-last-arguments nil
  6325. "The arguments of the previous call to org-agenda")
  6326. ;;;###autoload
  6327. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  6328. "Produce a weekly view from all files in variable `org-agenda-files'.
  6329. The view will be for the current week, but from the overview buffer you
  6330. will be able to go to other weeks.
  6331. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  6332. also be shown, under the current date.
  6333. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  6334. on the days are also shown. See the variable `org-log-done' for how
  6335. to turn on logging.
  6336. START-DAY defaults to TODAY, or to the most recent match for the weekday
  6337. given in `org-agenda-start-on-weekday'.
  6338. NDAYS defaults to `org-agenda-ndays'."
  6339. (interactive "P")
  6340. (if org-agenda-overriding-arguments
  6341. (setq include-all (car org-agenda-overriding-arguments)
  6342. start-day (nth 1 org-agenda-overriding-arguments)
  6343. ndays (nth 2 org-agenda-overriding-arguments)
  6344. keep-modes (nth 3 org-agenda-overriding-arguments)))
  6345. (setq org-agenda-last-arguments (list include-all start-day ndays keep-modes))
  6346. (org-compile-prefix-format 'agenda)
  6347. (org-set-sorting-strategy 'agenda)
  6348. (require 'calendar)
  6349. (let* ((org-agenda-start-on-weekday
  6350. (if (or (equal ndays 1)
  6351. (and (null ndays) (equal 1 org-agenda-ndays)))
  6352. nil org-agenda-start-on-weekday))
  6353. (org-agenda-keep-modes keep-modes)
  6354. (thefiles (org-agenda-files))
  6355. (files thefiles)
  6356. (win (selected-window))
  6357. (today (time-to-days (current-time)))
  6358. (sd (or start-day today))
  6359. (start (if (or (null org-agenda-start-on-weekday)
  6360. (< org-agenda-ndays 7))
  6361. sd
  6362. (let* ((nt (calendar-day-of-week
  6363. (calendar-gregorian-from-absolute sd)))
  6364. (n1 org-agenda-start-on-weekday)
  6365. (d (- nt n1)))
  6366. (- sd (+ (if (< d 0) 7 0) d)))))
  6367. (day-numbers (list start))
  6368. (inhibit-redisplay t)
  6369. s e rtn rtnall file date d start-pos end-pos todayp nd)
  6370. (setq org-agenda-redo-command
  6371. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  6372. ;; Make the list of days
  6373. (setq ndays (or ndays org-agenda-ndays)
  6374. nd ndays)
  6375. (while (> ndays 1)
  6376. (push (1+ (car day-numbers)) day-numbers)
  6377. (setq ndays (1- ndays)))
  6378. (setq day-numbers (nreverse day-numbers))
  6379. (org-prepare-agenda)
  6380. (org-set-local 'starting-day (car day-numbers))
  6381. (org-set-local 'include-all-loc include-all)
  6382. (when (and (or include-all org-agenda-include-all-todo)
  6383. (member today day-numbers))
  6384. (setq files thefiles
  6385. rtnall nil)
  6386. (while (setq file (pop files))
  6387. (catch 'nextfile
  6388. (org-check-agenda-file file)
  6389. (setq date (calendar-gregorian-from-absolute today)
  6390. rtn (org-agenda-get-day-entries
  6391. file date :todo))
  6392. (setq rtnall (append rtnall rtn))))
  6393. (when rtnall
  6394. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  6395. (add-text-properties (point-min) (1- (point))
  6396. (list 'face 'org-level-3))
  6397. (insert (org-finalize-agenda-entries rtnall) "\n")))
  6398. (setq s (point))
  6399. (insert (if (= nd 7) "Week-" "Day-") "agenda:\n")
  6400. (add-text-properties s (1- (point)) (list 'face 'org-level-3))
  6401. (while (setq d (pop day-numbers))
  6402. (setq date (calendar-gregorian-from-absolute d)
  6403. s (point))
  6404. (if (or (setq todayp (= d today))
  6405. (and (not start-pos) (= d sd)))
  6406. (setq start-pos (point))
  6407. (if (and start-pos (not end-pos))
  6408. (setq end-pos (point))))
  6409. (setq files thefiles
  6410. rtnall nil)
  6411. (while (setq file (pop files))
  6412. (catch 'nextfile
  6413. (org-check-agenda-file file)
  6414. (if org-agenda-show-log
  6415. (setq rtn (org-agenda-get-day-entries
  6416. file date
  6417. :deadline :scheduled :timestamp :closed))
  6418. (setq rtn (org-agenda-get-day-entries
  6419. file date
  6420. :deadline :scheduled :timestamp)))
  6421. (setq rtnall (append rtnall rtn))))
  6422. (if org-agenda-include-diary
  6423. (progn
  6424. (require 'diary-lib)
  6425. (setq rtn (org-get-entries-from-diary date))
  6426. (setq rtnall (append rtnall rtn))))
  6427. (if (or rtnall org-agenda-show-all-dates)
  6428. (progn
  6429. (insert (format "%-9s %2d %s %4d\n"
  6430. (calendar-day-name date)
  6431. (extract-calendar-day date)
  6432. (calendar-month-name (extract-calendar-month date))
  6433. (extract-calendar-year date)))
  6434. (put-text-property s (1- (point)) 'face
  6435. 'org-level-3)
  6436. (if todayp (put-text-property s (1- (point)) 'org-today t))
  6437. (if rtnall (insert
  6438. (org-finalize-agenda-entries
  6439. (org-agenda-add-time-grid-maybe
  6440. rtnall nd todayp))
  6441. "\n"))
  6442. (put-text-property s (1- (point)) 'day d))))
  6443. (goto-char (point-min))
  6444. (org-fit-agenda-window)
  6445. (unless (and (pos-visible-in-window-p (point-min))
  6446. (pos-visible-in-window-p (point-max)))
  6447. (goto-char (1- (point-max)))
  6448. (recenter -1)
  6449. (if (not (pos-visible-in-window-p (or start-pos 1)))
  6450. (progn
  6451. (goto-char (or start-pos 1))
  6452. (recenter 1))))
  6453. (goto-char (or start-pos 1))
  6454. (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
  6455. (org-agenda-align-tags)
  6456. (setq buffer-read-only t)
  6457. (if (not org-select-agenda-window) (select-window win))
  6458. (message "")))
  6459. (defvar org-select-this-todo-keyword nil)
  6460. ;;;###autoload
  6461. (defun org-todo-list (arg &optional keep-modes)
  6462. "Show all TODO entries from all agenda file in a single list.
  6463. The prefix arg can be used to select a specific TODO keyword and limit
  6464. the list to these. When using \\[universal-argument], you will be prompted
  6465. for a keyword. A numeric prefix directly selects the Nth keyword in
  6466. `org-todo-keywords'."
  6467. (interactive "P")
  6468. (org-compile-prefix-format 'todo)
  6469. (org-set-sorting-strategy 'todo)
  6470. (let* ((org-agenda-keep-modes keep-modes)
  6471. (today (time-to-days (current-time)))
  6472. (date (calendar-gregorian-from-absolute today))
  6473. (win (selected-window))
  6474. (kwds org-todo-keywords)
  6475. (completion-ignore-case t)
  6476. (org-select-this-todo-keyword
  6477. (if (stringp arg) arg
  6478. (and arg (integerp arg) (> arg 0)
  6479. (nth (1- arg) org-todo-keywords))))
  6480. rtn rtnall files file pos)
  6481. (when (equal arg '(4))
  6482. (setq org-select-this-todo-keyword
  6483. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  6484. nil t)))
  6485. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  6486. (org-prepare-agenda)
  6487. (org-set-local 'last-arg arg)
  6488. (org-set-local 'org-todo-keywords kwds)
  6489. (setq org-agenda-redo-command
  6490. '(org-todo-list (or current-prefix-arg last-arg) t))
  6491. (setq files (org-agenda-files)
  6492. rtnall nil)
  6493. (while (setq file (pop files))
  6494. (catch 'nextfile
  6495. (org-check-agenda-file file)
  6496. (setq rtn (org-agenda-get-day-entries file date :todo))
  6497. (setq rtnall (append rtnall rtn))))
  6498. (insert "Global list of TODO items of type: ")
  6499. (add-text-properties (point-min) (1- (point))
  6500. (list 'face 'org-level-3))
  6501. (setq pos (point))
  6502. (insert (or org-select-this-todo-keyword "ALL") "\n")
  6503. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  6504. (setq pos (point))
  6505. (unless org-agenda-multi
  6506. (insert
  6507. "Available with `N r': (0)ALL "
  6508. (let ((n 0))
  6509. (mapconcat (lambda (x)
  6510. (format "(%d)%s" (setq n (1+ n)) x))
  6511. org-todo-keywords " "))
  6512. "\n"))
  6513. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  6514. (when rtnall
  6515. (insert (org-finalize-agenda-entries rtnall) "\n"))
  6516. (goto-char (point-min))
  6517. (org-fit-agenda-window)
  6518. (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
  6519. (org-agenda-align-tags)
  6520. (setq buffer-read-only t)
  6521. (if (not org-select-agenda-window) (select-window win))))
  6522. (defun org-check-agenda-file (file)
  6523. "Make sure FILE exists. If not, ask user what to do."
  6524. (when (not (file-exists-p file))
  6525. (message "non-existent file %s. [R]emove from list or [A]bort?"
  6526. (abbreviate-file-name file))
  6527. (let ((r (downcase (read-char-exclusive))))
  6528. (cond
  6529. ((equal r ?r)
  6530. (org-remove-file file)
  6531. (throw 'nextfile t))
  6532. (t (error "Abort"))))))
  6533. (defun org-agenda-check-type (error &rest types)
  6534. "Check if agenda buffer is of allowed type.
  6535. If ERROR is non-nil, throw an error, otherwise just return nil."
  6536. (if (memq org-agenda-type types)
  6537. t
  6538. (if error
  6539. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  6540. nil)))
  6541. (defun org-agenda-quit ()
  6542. "Exit agenda by removing the window or the buffer."
  6543. (interactive)
  6544. (let ((buf (current-buffer)))
  6545. (if (not (one-window-p)) (delete-window))
  6546. (kill-buffer buf)
  6547. (org-agenda-maybe-reset-markers 'force)))
  6548. (defun org-agenda-exit ()
  6549. "Exit agenda by removing the window or the buffer.
  6550. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  6551. Org-mode buffers visited directly by the user will not be touched."
  6552. (interactive)
  6553. (org-release-buffers org-agenda-new-buffers)
  6554. (setq org-agenda-new-buffers nil)
  6555. (org-agenda-quit))
  6556. ;; FIXME: move this function.
  6557. (defun org-save-all-org-buffers ()
  6558. "Save all Org-mode buffers without user confirmation."
  6559. (interactive)
  6560. (message "Saving all Org-mode buffers...")
  6561. (save-some-buffers t 'org-mode-p)
  6562. (message "Saving all Org-mode buffers... done"))
  6563. (defun org-agenda-redo ()
  6564. "Rebuild Agenda.
  6565. When this is the global TODO list, a prefix argument will be interpreted."
  6566. (interactive)
  6567. (let* ((line (org-current-line))
  6568. (window-line (- line (org-current-line (window-start)))))
  6569. (message "Rebuilding agenda buffer...")
  6570. (eval org-agenda-redo-command)
  6571. (message "Rebuilding agenda buffer...done")
  6572. (goto-line line)
  6573. (recenter window-line)))
  6574. (defun org-agenda-goto-today ()
  6575. "Go to today."
  6576. (interactive)
  6577. (org-agenda-check-type t 'timeline 'agenda)
  6578. (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
  6579. (cond
  6580. (tdpos (goto-char tdpos))
  6581. ((eq org-agenda-type 'agenda)
  6582. (let ((org-agenda-overriding-arguments org-agenda-last-arguments))
  6583. (setf (nth 1 org-agenda-overriding-arguments) nil)
  6584. (org-agenda-redo)
  6585. (org-agenda-find-today-or-agenda)))
  6586. (t (error "Cannot find today")))))
  6587. (defun org-agenda-find-today-or-agenda ()
  6588. (goto-char
  6589. (or (text-property-any (point-min) (point-max) 'org-today t)
  6590. (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
  6591. (point-min))))
  6592. (defun org-agenda-later (arg)
  6593. "Go forward in time by `org-agenda-ndays' days.
  6594. With prefix ARG, go forward that many times `org-agenda-ndays'."
  6595. (interactive "p")
  6596. (org-agenda-check-type t 'agenda)
  6597. (let ((org-agenda-overriding-arguments
  6598. (list (car org-agenda-last-arguments)
  6599. (+ starting-day (* arg org-agenda-ndays))
  6600. nil t)))
  6601. (org-agenda-redo)
  6602. (org-agenda-find-today-or-agenda)))
  6603. (defun org-agenda-earlier (arg)
  6604. "Go back in time by `org-agenda-ndays' days.
  6605. With prefix ARG, go back that many times `org-agenda-ndays'."
  6606. (interactive "p")
  6607. (org-agenda-check-type t 'agenda)
  6608. (let ((org-agenda-overriding-arguments
  6609. (list (car org-agenda-last-arguments)
  6610. (- starting-day (* arg org-agenda-ndays))
  6611. nil t)))
  6612. (org-agenda-redo)
  6613. (org-agenda-find-today-or-agenda)))
  6614. (defun org-agenda-week-view ()
  6615. "Switch to weekly view for agenda."
  6616. (interactive)
  6617. (org-agenda-check-type t 'agenda)
  6618. (if (= org-agenda-ndays 7)
  6619. (error "This is already the week view"))
  6620. (setq org-agenda-ndays 7)
  6621. (let ((org-agenda-overriding-arguments
  6622. (list (car org-agenda-last-arguments)
  6623. (or (get-text-property (point) 'day)
  6624. starting-day)
  6625. nil t)))
  6626. (org-agenda-redo)
  6627. (org-agenda-find-today-or-agenda))
  6628. (org-agenda-set-mode-name)
  6629. (message "Switched to week view"))
  6630. (defun org-agenda-day-view ()
  6631. "Switch to daily view for agenda."
  6632. (interactive)
  6633. (org-agenda-check-type t 'agenda)
  6634. (if (= org-agenda-ndays 1)
  6635. (error "This is already the day view"))
  6636. (setq org-agenda-ndays 1)
  6637. (let ((org-agenda-overriding-arguments
  6638. (list (car org-agenda-last-arguments)
  6639. (or (get-text-property (point) 'day)
  6640. starting-day)
  6641. nil t)))
  6642. (org-agenda-redo)
  6643. (org-agenda-find-today-or-agenda))
  6644. (org-agenda-set-mode-name)
  6645. (message "Switched to day view"))
  6646. (defun org-agenda-next-date-line (&optional arg)
  6647. "Jump to the next line indicating a date in agenda buffer."
  6648. (interactive "p")
  6649. (org-agenda-check-type t 'agenda 'timeline)
  6650. (beginning-of-line 1)
  6651. (if (looking-at "^\\S-") (forward-char 1))
  6652. (if (not (re-search-forward "^\\S-" nil t arg))
  6653. (progn
  6654. (backward-char 1)
  6655. (error "No next date after this line in this buffer")))
  6656. (goto-char (match-beginning 0)))
  6657. (defun org-agenda-previous-date-line (&optional arg)
  6658. "Jump to the previous line indicating a date in agenda buffer."
  6659. (interactive "p")
  6660. (org-agenda-check-type t 'agenda 'timeline)
  6661. (beginning-of-line 1)
  6662. (if (not (re-search-backward "^\\S-" nil t arg))
  6663. (error "No previous date before this line in this buffer")))
  6664. ;; Initialize the highlight
  6665. (defvar org-hl (org-make-overlay 1 1))
  6666. (org-overlay-put org-hl 'face 'highlight)
  6667. (defun org-highlight (begin end &optional buffer)
  6668. "Highlight a region with overlay."
  6669. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  6670. org-hl begin end (or buffer (current-buffer))))
  6671. (defun org-unhighlight ()
  6672. "Detach overlay INDEX."
  6673. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  6674. (defun org-agenda-follow-mode ()
  6675. "Toggle follow mode in an agenda buffer."
  6676. (interactive)
  6677. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  6678. (org-agenda-set-mode-name)
  6679. (message "Follow mode is %s"
  6680. (if org-agenda-follow-mode "on" "off")))
  6681. (defun org-agenda-log-mode ()
  6682. "Toggle log mode in an agenda buffer."
  6683. (interactive)
  6684. (org-agenda-check-type t 'agenda 'timeline)
  6685. (setq org-agenda-show-log (not org-agenda-show-log))
  6686. (org-agenda-set-mode-name)
  6687. (org-agenda-redo)
  6688. (message "Log mode is %s"
  6689. (if org-agenda-show-log "on" "off")))
  6690. (defun org-agenda-toggle-diary ()
  6691. "Toggle diary inclusion in an agenda buffer."
  6692. (interactive)
  6693. (org-agenda-check-type t 'agenda)
  6694. (setq org-agenda-include-diary (not org-agenda-include-diary))
  6695. (org-agenda-redo)
  6696. (org-agenda-set-mode-name)
  6697. (message "Diary inclusion turned %s"
  6698. (if org-agenda-include-diary "on" "off")))
  6699. (defun org-agenda-toggle-time-grid ()
  6700. "Toggle time grid in an agenda buffer."
  6701. (interactive)
  6702. (org-agenda-check-type t 'agenda)
  6703. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  6704. (org-agenda-redo)
  6705. (org-agenda-set-mode-name)
  6706. (message "Time-grid turned %s"
  6707. (if org-agenda-use-time-grid "on" "off")))
  6708. (defun org-agenda-set-mode-name ()
  6709. "Set the mode name to indicate all the small mode settings."
  6710. (setq mode-name
  6711. (concat "Org-Agenda"
  6712. (if (equal org-agenda-ndays 1) " Day" "")
  6713. (if (equal org-agenda-ndays 7) " Week" "")
  6714. (if org-agenda-follow-mode " Follow" "")
  6715. (if org-agenda-include-diary " Diary" "")
  6716. (if org-agenda-use-time-grid " Grid" "")
  6717. (if org-agenda-show-log " Log" "")))
  6718. (force-mode-line-update))
  6719. (defun org-agenda-post-command-hook ()
  6720. (and (eolp) (not (bolp)) (backward-char 1))
  6721. (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
  6722. (if (and org-agenda-follow-mode
  6723. (get-text-property (point) 'org-marker))
  6724. (org-agenda-show)))
  6725. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  6726. (defun org-get-entries-from-diary (date)
  6727. "Get the (Emacs Calendar) diary entries for DATE."
  6728. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  6729. (diary-display-hook '(fancy-diary-display))
  6730. (list-diary-entries-hook
  6731. (cons 'org-diary-default-entry list-diary-entries-hook))
  6732. (diary-file-name-prefix-function nil) ; turn this feature off
  6733. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  6734. entries
  6735. (org-disable-agenda-to-diary t))
  6736. (save-excursion
  6737. (save-window-excursion
  6738. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  6739. (if (not (get-buffer fancy-diary-buffer))
  6740. (setq entries nil)
  6741. (with-current-buffer fancy-diary-buffer
  6742. (setq buffer-read-only nil)
  6743. (if (= (point-max) 1)
  6744. ;; No entries
  6745. (setq entries nil)
  6746. ;; Omit the date and other unnecessary stuff
  6747. (org-agenda-cleanup-fancy-diary)
  6748. ;; Add prefix to each line and extend the text properties
  6749. (if (= (point-max) 1)
  6750. (setq entries nil)
  6751. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  6752. (set-buffer-modified-p nil)
  6753. (kill-buffer fancy-diary-buffer)))
  6754. (when entries
  6755. (setq entries (org-split-string entries "\n"))
  6756. (setq entries
  6757. (mapcar
  6758. (lambda (x)
  6759. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  6760. ;; Extend the text properties to the beginning of the line
  6761. (org-add-props x (text-properties-at (1- (length x)) x)))
  6762. entries)))))
  6763. (defun org-agenda-cleanup-fancy-diary ()
  6764. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  6765. This gets rid of the date, the underline under the date, and
  6766. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  6767. date. It also removes lines that contain only whitespace."
  6768. (goto-char (point-min))
  6769. (if (looking-at ".*?:[ \t]*")
  6770. (progn
  6771. (replace-match "")
  6772. (re-search-forward "\n=+$" nil t)
  6773. (replace-match "")
  6774. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  6775. (re-search-forward "\n=+$" nil t)
  6776. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  6777. (goto-char (point-min))
  6778. (while (re-search-forward "^ +\n" nil t)
  6779. (replace-match ""))
  6780. (goto-char (point-min))
  6781. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  6782. (replace-match "")))
  6783. ;; Make sure entries from the diary have the right text properties.
  6784. (eval-after-load "diary-lib"
  6785. '(if (boundp 'diary-modify-entry-list-string-function)
  6786. ;; We can rely on the hook, nothing to do
  6787. nil
  6788. ;; Hook not avaiable, must use advice to make this work
  6789. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  6790. "Make the position visible."
  6791. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  6792. (stringp string)
  6793. buffer-file-name)
  6794. (setq string (org-modify-diary-entry-string string))))))
  6795. (defun org-modify-diary-entry-string (string)
  6796. "Add text properties to string, allowing org-mode to act on it."
  6797. (org-add-props string nil
  6798. 'mouse-face 'highlight
  6799. 'keymap org-agenda-keymap
  6800. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  6801. (abbreviate-file-name buffer-file-name))
  6802. 'org-agenda-diary-link t
  6803. 'org-marker (org-agenda-new-marker (point-at-bol))))
  6804. (defun org-diary-default-entry ()
  6805. "Add a dummy entry to the diary.
  6806. Needed to avoid empty dates which mess up holiday display."
  6807. ;; Catch the error if dealing with the new add-to-diary-alist
  6808. (when org-disable-agenda-to-diary
  6809. (condition-case nil
  6810. (add-to-diary-list original-date "Org-mode dummy" "")
  6811. (error
  6812. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  6813. (defun org-cycle-agenda-files ()
  6814. "Cycle through the files in `org-agenda-files'.
  6815. If the current buffer visits an agenda file, find the next one in the list.
  6816. If the current buffer does not, find the first agenda file."
  6817. (interactive)
  6818. (let* ((fs (org-agenda-files t))
  6819. (files (append fs (list (car fs))))
  6820. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  6821. file)
  6822. (unless files (error "No agenda files"))
  6823. (catch 'exit
  6824. (while (setq file (pop files))
  6825. (if (equal (file-truename file) tcf)
  6826. (when (car files)
  6827. (find-file (car files))
  6828. (throw 'exit t))))
  6829. (find-file (car fs)))))
  6830. (defun org-agenda-file-to-end ()
  6831. "Move/add the current file to the end of the agenda file list.
  6832. If the file is not present in the list, it is appended to the list. If it is
  6833. present, it is moved there."
  6834. (interactive)
  6835. (org-agenda-file-to-front 'to-end))
  6836. (defun org-agenda-file-to-front (&optional to-end)
  6837. "Move/add the current file to the top of the agenda file list.
  6838. If the file is not present in the list, it is added to the front. If it is
  6839. present, it is moved there. With optional argument TO-END, add/move to the
  6840. end of the list."
  6841. (interactive "P")
  6842. (let ((file-alist (mapcar (lambda (x)
  6843. (cons (file-truename x) x))
  6844. (org-agenda-files t)))
  6845. (ctf (file-truename buffer-file-name))
  6846. x had)
  6847. (setq x (assoc ctf file-alist) had x)
  6848. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  6849. (if to-end
  6850. (setq file-alist (append (delq x file-alist) (list x)))
  6851. (setq file-alist (cons x (delq x file-alist))))
  6852. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  6853. (org-install-agenda-files-menu)
  6854. (message "File %s to %s of agenda file list"
  6855. (if had "moved" "added") (if to-end "end" "front"))))
  6856. (defun org-remove-file (&optional file)
  6857. "Remove current file from the list of files in variable `org-agenda-files'.
  6858. These are the files which are being checked for agenda entries.
  6859. Optional argument FILE means, use this file instead of the current."
  6860. (interactive)
  6861. (let* ((file (or file buffer-file-name))
  6862. (true-file (file-truename file))
  6863. (afile (abbreviate-file-name file))
  6864. (files (delq nil (mapcar
  6865. (lambda (x)
  6866. (if (equal true-file
  6867. (file-truename x))
  6868. nil x))
  6869. (org-agenda-files t)))))
  6870. (if (not (= (length files) (length (org-agenda-files t))))
  6871. (progn
  6872. (org-store-new-agenda-file-list files)
  6873. (org-install-agenda-files-menu)
  6874. (message "Removed file: %s" afile))
  6875. (message "File was not in list: %s" afile))))
  6876. (defun org-file-menu-entry (file)
  6877. (vector file (list 'find-file file) t))
  6878. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  6879. "Return a list of all relevant day numbers from BEG to END buffer positions.
  6880. If NO-RANGES is non-nil, include only the start and end dates of a range,
  6881. not every single day in the range. If FORCE-TODAY is non-nil, make
  6882. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  6883. inactive time stamps (those in square brackets) are included.
  6884. When EMPTY is non-nil, also include days without any entries."
  6885. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  6886. dates dates1 date day day1 day2 ts1 ts2)
  6887. (if force-today
  6888. (setq dates (list (time-to-days (current-time)))))
  6889. (save-excursion
  6890. (goto-char beg)
  6891. (while (re-search-forward re end t)
  6892. (setq day (time-to-days (org-time-string-to-time
  6893. (substring (match-string 1) 0 10))))
  6894. (or (memq day dates) (push day dates)))
  6895. (unless no-ranges
  6896. (goto-char beg)
  6897. (while (re-search-forward org-tr-regexp end t)
  6898. (setq ts1 (substring (match-string 1) 0 10)
  6899. ts2 (substring (match-string 2) 0 10)
  6900. day1 (time-to-days (org-time-string-to-time ts1))
  6901. day2 (time-to-days (org-time-string-to-time ts2)))
  6902. (while (< (setq day1 (1+ day1)) day2)
  6903. (or (memq day1 dates) (push day1 dates)))))
  6904. (setq dates (sort dates '<))
  6905. (when empty
  6906. (while (setq day (pop dates))
  6907. (setq day2 (car dates))
  6908. (push day dates1)
  6909. (when (and day2 empty)
  6910. (if (or (eq empty t)
  6911. (and (numberp empty) (<= (- day2 day) empty)))
  6912. (while (< (setq day (1+ day)) day2)
  6913. (push (list day) dates1))
  6914. (push (cons :omitted (- day2 day)) dates1))))
  6915. (setq dates (nreverse dates1)))
  6916. dates)))
  6917. ;;;###autoload
  6918. (defun org-diary (&rest args)
  6919. "Return diary information from org-files.
  6920. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  6921. It accesses org files and extracts information from those files to be
  6922. listed in the diary. The function accepts arguments specifying what
  6923. items should be listed. The following arguments are allowed:
  6924. :timestamp List the headlines of items containing a date stamp or
  6925. date range matching the selected date. Deadlines will
  6926. also be listed, on the expiration day.
  6927. :deadline List any deadlines past due, or due within
  6928. `org-deadline-warning-days'. The listing occurs only
  6929. in the diary for *today*, not at any other date. If
  6930. an entry is marked DONE, it is no longer listed.
  6931. :scheduled List all items which are scheduled for the given date.
  6932. The diary for *today* also contains items which were
  6933. scheduled earlier and are not yet marked DONE.
  6934. :todo List all TODO items from the org-file. This may be a
  6935. long list - so this is not turned on by default.
  6936. Like deadlines, these entries only show up in the
  6937. diary for *today*, not at any other date.
  6938. The call in the diary file should look like this:
  6939. &%%(org-diary) ~/path/to/some/orgfile.org
  6940. Use a separate line for each org file to check. Or, if you omit the file name,
  6941. all files listed in `org-agenda-files' will be checked automatically:
  6942. &%%(org-diary)
  6943. If you don't give any arguments (as in the example above), the default
  6944. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  6945. also be written as
  6946. &%%(org-diary :deadline :timestamp :scheduled)
  6947. The function expects the lisp variables `entry' and `date' to be provided
  6948. by the caller, because this is how the calendar works. Don't use this
  6949. function from a program - use `org-agenda-get-day-entries' instead."
  6950. (org-agenda-maybe-reset-markers) ;; FIXME: does this still do the right thing?
  6951. (org-compile-prefix-format 'agenda)
  6952. (org-set-sorting-strategy 'agenda)
  6953. (setq args (or args '(:deadline :scheduled :timestamp)))
  6954. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  6955. (list entry)
  6956. (org-agenda-files t)))
  6957. file rtn results)
  6958. ;; If this is called during org-agenda, don't return any entries to
  6959. ;; the calendar. Org Agenda will list these entries itself.
  6960. (if org-disable-agenda-to-diary (setq files nil))
  6961. (while (setq file (pop files))
  6962. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  6963. (setq results (append results rtn)))
  6964. (if results
  6965. (concat (org-finalize-agenda-entries results) "\n"))))
  6966. (defvar org-category-table nil)
  6967. (defun org-get-category-table ()
  6968. "Get the table of categories and positions in current buffer."
  6969. (let (tbl)
  6970. (save-excursion
  6971. (goto-char (point-min))
  6972. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  6973. (push (cons (point) (org-trim (match-string 2))) tbl)))
  6974. tbl))
  6975. (defun org-get-category (&optional pos)
  6976. "Get the category applying to position POS."
  6977. (if (not org-category-table)
  6978. (cond
  6979. ((null org-category)
  6980. (setq org-category
  6981. (if buffer-file-name
  6982. (file-name-sans-extension
  6983. (file-name-nondirectory buffer-file-name))
  6984. "???")))
  6985. ((symbolp org-category) (symbol-name org-category))
  6986. (t org-category))
  6987. (let ((tbl org-category-table)
  6988. (pos (or pos (point))))
  6989. (while (and tbl (> (caar tbl) pos))
  6990. (pop tbl))
  6991. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  6992. org-category-table))))))
  6993. (defun org-agenda-get-day-entries (file date &rest args)
  6994. "Does the work for `org-diary' and `org-agenda'.
  6995. FILE is the path to a file to be checked for entries. DATE is date like
  6996. the one returned by `calendar-current-date'. ARGS are symbols indicating
  6997. which kind of entries should be extracted. For details about these, see
  6998. the documentation of `org-diary'."
  6999. (setq args (or args '(:deadline :scheduled :timestamp)))
  7000. (let* ((org-startup-with-deadline-check nil)
  7001. (org-startup-folded nil)
  7002. (org-startup-align-all-tables nil)
  7003. (buffer (if (file-exists-p file)
  7004. (org-get-agenda-file-buffer file)
  7005. (error "No such file %s" file)))
  7006. arg results rtn)
  7007. (if (not buffer)
  7008. ;; If file does not exist, make sure an error message ends up in diary
  7009. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7010. (with-current-buffer buffer
  7011. (unless (org-mode-p)
  7012. (error "Agenda file %s is not in `org-mode'" file))
  7013. (setq org-category-table (org-get-category-table))
  7014. (let ((case-fold-search nil))
  7015. (save-excursion
  7016. (save-restriction
  7017. (if org-agenda-restrict
  7018. (narrow-to-region org-agenda-restrict-begin
  7019. org-agenda-restrict-end)
  7020. (widen))
  7021. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  7022. (while (setq arg (pop args))
  7023. (cond
  7024. ((and (eq arg :todo)
  7025. (equal date (calendar-current-date)))
  7026. (setq rtn (org-agenda-get-todos))
  7027. (setq results (append results rtn)))
  7028. ((eq arg :timestamp)
  7029. (setq rtn (org-agenda-get-blocks))
  7030. (setq results (append results rtn))
  7031. (setq rtn (org-agenda-get-timestamps))
  7032. (setq results (append results rtn)))
  7033. ((eq arg :scheduled)
  7034. (setq rtn (org-agenda-get-scheduled))
  7035. (setq results (append results rtn)))
  7036. ((eq arg :closed)
  7037. (setq rtn (org-agenda-get-closed))
  7038. (setq results (append results rtn)))
  7039. ((and (eq arg :deadline)
  7040. (equal date (calendar-current-date)))
  7041. (setq rtn (org-agenda-get-deadlines))
  7042. (setq results (append results rtn))))))))
  7043. results))))
  7044. (defun org-entry-is-done-p ()
  7045. "Is the current entry marked DONE?"
  7046. (save-excursion
  7047. (and (re-search-backward "[\r\n]\\*" nil t)
  7048. (looking-at org-nl-done-regexp))))
  7049. (defun org-at-date-range-p ()
  7050. "Is the cursor inside a date range?"
  7051. (interactive)
  7052. (save-excursion
  7053. (catch 'exit
  7054. (let ((pos (point)))
  7055. (skip-chars-backward "^<\r\n")
  7056. (skip-chars-backward "<")
  7057. (and (looking-at org-tr-regexp)
  7058. (>= (match-end 0) pos)
  7059. (throw 'exit t))
  7060. (skip-chars-backward "^<\r\n")
  7061. (skip-chars-backward "<")
  7062. (and (looking-at org-tr-regexp)
  7063. (>= (match-end 0) pos)
  7064. (throw 'exit t)))
  7065. nil)))
  7066. (defun org-agenda-get-todos ()
  7067. "Return the TODO information for agenda display."
  7068. (let* ((props (list 'face nil
  7069. 'done-face 'org-done
  7070. 'org-not-done-regexp org-not-done-regexp
  7071. 'mouse-face 'highlight
  7072. 'keymap org-agenda-keymap
  7073. 'help-echo
  7074. (format "mouse-2 or RET jump to org file %s"
  7075. (abbreviate-file-name buffer-file-name))))
  7076. (regexp (concat "[\n\r]\\*+ *\\("
  7077. (if org-select-this-todo-keyword
  7078. (concat "\\<\\(" org-select-this-todo-keyword
  7079. "\\)\\>")
  7080. org-not-done-regexp)
  7081. "[^\n\r]*\\)"))
  7082. (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  7083. marker priority category tags
  7084. ee txt)
  7085. (goto-char (point-min))
  7086. (while (re-search-forward regexp nil t)
  7087. (catch :skip
  7088. (when (and org-agenda-todo-ignore-scheduled
  7089. (looking-at sched-re))
  7090. ;; FIXME: the following test also happens below, but we need it here
  7091. (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
  7092. (throw :skip nil))
  7093. (org-agenda-skip)
  7094. (goto-char (match-beginning 1))
  7095. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  7096. category (org-get-category)
  7097. tags (org-get-tags-at (point))
  7098. txt (org-format-agenda-item "" (match-string 1) category tags)
  7099. priority
  7100. (+ (org-get-priority txt)
  7101. (if org-todo-kwd-priority-p
  7102. (- org-todo-kwd-max-priority -2
  7103. (length
  7104. (member (match-string 2) org-todo-keywords)))
  7105. 1)))
  7106. (org-add-props txt props
  7107. 'org-marker marker 'org-hd-marker marker
  7108. 'priority priority 'category category)
  7109. (push txt ee)
  7110. (if org-agenda-todo-list-sublevels
  7111. (goto-char (match-end 1))
  7112. (org-end-of-subtree 'invisible))))
  7113. (nreverse ee)))
  7114. (defconst org-agenda-no-heading-message
  7115. "No heading for this item in buffer or region.")
  7116. (defun org-agenda-get-timestamps ()
  7117. "Return the date stamp information for agenda display."
  7118. (let* ((props (list 'face nil
  7119. 'org-not-done-regexp org-not-done-regexp
  7120. 'mouse-face 'highlight
  7121. 'keymap org-agenda-keymap
  7122. 'help-echo
  7123. (format "mouse-2 or RET jump to org file %s"
  7124. (abbreviate-file-name buffer-file-name))))
  7125. (regexp (regexp-quote
  7126. (substring
  7127. (format-time-string
  7128. (car org-time-stamp-formats)
  7129. (apply 'encode-time ; DATE bound by calendar
  7130. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7131. 0 11)))
  7132. marker hdmarker deadlinep scheduledp donep tmp priority category
  7133. ee txt timestr tags)
  7134. (goto-char (point-min))
  7135. (while (re-search-forward regexp nil t)
  7136. (catch :skip
  7137. (and (save-match-data (org-at-date-range-p)) (throw :skip nil))
  7138. (org-agenda-skip)
  7139. (setq marker (org-agenda-new-marker (match-beginning 0))
  7140. category (org-get-category (match-beginning 0))
  7141. tmp (buffer-substring (max (point-min)
  7142. (- (match-beginning 0)
  7143. org-ds-keyword-length))
  7144. (match-beginning 0))
  7145. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7146. deadlinep (string-match org-deadline-regexp tmp)
  7147. scheduledp (string-match org-scheduled-regexp tmp)
  7148. donep (org-entry-is-done-p))
  7149. (if (string-match ">" timestr)
  7150. ;; substring should only run to end of time stamp
  7151. (setq timestr (substring timestr 0 (match-end 0))))
  7152. (save-excursion
  7153. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7154. (progn
  7155. (goto-char (match-end 1))
  7156. (setq hdmarker (org-agenda-new-marker)
  7157. tags (org-get-tags-at))
  7158. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7159. (setq txt (org-format-agenda-item
  7160. (format "%s%s"
  7161. (if deadlinep "Deadline: " "")
  7162. (if scheduledp "Scheduled: " ""))
  7163. (match-string 1) category tags timestr)))
  7164. (setq txt org-agenda-no-heading-message))
  7165. (setq priority (org-get-priority txt))
  7166. (org-add-props txt props
  7167. 'org-marker marker 'org-hd-marker hdmarker)
  7168. (if deadlinep
  7169. (org-add-props txt nil
  7170. 'face (if donep 'org-done 'org-warning)
  7171. 'undone-face 'org-warning 'done-face 'org-done
  7172. 'category category 'priority (+ 100 priority))
  7173. (if scheduledp
  7174. (org-add-props txt nil
  7175. 'face 'org-scheduled-today
  7176. 'undone-face 'org-scheduled-today 'done-face 'org-done
  7177. 'category category 'priority (+ 99 priority))
  7178. (org-add-props txt nil 'priority priority 'category category)))
  7179. (push txt ee))
  7180. (outline-next-heading)))
  7181. (nreverse ee)))
  7182. (defun org-agenda-get-closed ()
  7183. "Return the logged TODO entries for agenda display."
  7184. (let* ((props (list 'mouse-face 'highlight
  7185. 'org-not-done-regexp org-not-done-regexp
  7186. 'keymap org-agenda-keymap
  7187. 'help-echo
  7188. (format "mouse-2 or RET jump to org file %s"
  7189. (abbreviate-file-name buffer-file-name))))
  7190. (regexp (concat
  7191. "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
  7192. (regexp-quote
  7193. (substring
  7194. (format-time-string
  7195. (car org-time-stamp-formats)
  7196. (apply 'encode-time ; DATE bound by calendar
  7197. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  7198. 1 11))))
  7199. marker hdmarker priority category tags closedp
  7200. ee txt timestr)
  7201. (goto-char (point-min))
  7202. (while (re-search-forward regexp nil t)
  7203. (catch :skip
  7204. (org-agenda-skip)
  7205. (setq marker (org-agenda-new-marker (match-beginning 0))
  7206. closedp (equal (match-string 1) org-closed-string)
  7207. category (org-get-category (match-beginning 0))
  7208. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  7209. ;; donep (org-entry-is-done-p)
  7210. )
  7211. (if (string-match "\\]" timestr)
  7212. ;; substring should only run to end of time stamp
  7213. (setq timestr (substring timestr 0 (match-end 0))))
  7214. (save-excursion
  7215. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7216. (progn
  7217. (goto-char (match-end 1))
  7218. (setq hdmarker (org-agenda-new-marker)
  7219. tags (org-get-tags-at))
  7220. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7221. (setq txt (org-format-agenda-item
  7222. (if closedp "Closed: " "Clocked: ")
  7223. (match-string 1) category tags timestr)))
  7224. (setq txt org-agenda-no-heading-message))
  7225. (setq priority 100000)
  7226. (org-add-props txt props
  7227. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  7228. 'priority priority 'category category
  7229. 'undone-face 'org-warning 'done-face 'org-done)
  7230. (push txt ee))
  7231. (outline-next-heading)))
  7232. (nreverse ee)))
  7233. (defun org-agenda-get-deadlines ()
  7234. "Return the deadline information for agenda display."
  7235. (let* ((wdays org-deadline-warning-days)
  7236. (props (list 'mouse-face 'highlight
  7237. 'org-not-done-regexp org-not-done-regexp
  7238. 'keymap org-agenda-keymap
  7239. 'help-echo
  7240. (format "mouse-2 or RET jump to org file %s"
  7241. (abbreviate-file-name buffer-file-name))))
  7242. (regexp org-deadline-time-regexp)
  7243. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7244. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7245. d2 diff pos pos1 category tags
  7246. ee txt head face)
  7247. (goto-char (point-min))
  7248. (while (re-search-forward regexp nil t)
  7249. (catch :skip
  7250. (org-agenda-skip)
  7251. (setq pos (1- (match-beginning 1))
  7252. d2 (time-to-days
  7253. (org-time-string-to-time (match-string 1)))
  7254. diff (- d2 d1))
  7255. ;; When to show a deadline in the calendar:
  7256. ;; If the expiration is within wdays warning time.
  7257. ;; Past-due deadlines are only shown on the current date
  7258. (if (and (< diff wdays) todayp (not (= diff 0)))
  7259. (save-excursion
  7260. (setq category (org-get-category))
  7261. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7262. (progn
  7263. (goto-char (match-end 0))
  7264. (setq pos1 (match-end 1))
  7265. (setq tags (org-get-tags-at pos1))
  7266. (setq head (buffer-substring-no-properties
  7267. (point)
  7268. (progn (skip-chars-forward "^\r\n")
  7269. (point))))
  7270. (if (string-match org-looking-at-done-regexp head)
  7271. (setq txt nil)
  7272. (setq txt (org-format-agenda-item
  7273. (format "In %3d d.: " diff) head category tags))))
  7274. (setq txt org-agenda-no-heading-message))
  7275. (when txt
  7276. (setq face (cond ((<= diff 0) 'org-warning)
  7277. ((<= diff 5) 'org-upcoming-deadline)
  7278. (t nil)))
  7279. (org-add-props txt props
  7280. 'org-marker (org-agenda-new-marker pos)
  7281. 'org-hd-marker (org-agenda-new-marker pos1)
  7282. 'priority (+ (- 10 diff) (org-get-priority txt))
  7283. 'category category
  7284. 'face face 'undone-face face 'done-face 'org-done)
  7285. (push txt ee))))))
  7286. ee))
  7287. (defun org-agenda-get-scheduled ()
  7288. "Return the scheduled information for agenda display."
  7289. (let* ((props (list 'face 'org-scheduled-previously
  7290. 'org-not-done-regexp org-not-done-regexp
  7291. 'undone-face 'org-scheduled-previously
  7292. 'done-face 'org-done
  7293. 'mouse-face 'highlight
  7294. 'keymap org-agenda-keymap
  7295. 'help-echo
  7296. (format "mouse-2 or RET jump to org file %s"
  7297. (abbreviate-file-name buffer-file-name))))
  7298. (regexp org-scheduled-time-regexp)
  7299. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  7300. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  7301. d2 diff pos pos1 category tags
  7302. ee txt head)
  7303. (goto-char (point-min))
  7304. (while (re-search-forward regexp nil t)
  7305. (catch :skip
  7306. (org-agenda-skip)
  7307. (setq pos (1- (match-beginning 1))
  7308. d2 (time-to-days
  7309. (org-time-string-to-time (match-string 1)))
  7310. diff (- d2 d1))
  7311. ;; When to show a scheduled item in the calendar:
  7312. ;; If it is on or past the date.
  7313. (if (and (< diff 0) todayp)
  7314. (save-excursion
  7315. (setq category (org-get-category))
  7316. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  7317. (progn
  7318. (goto-char (match-end 0))
  7319. (setq pos1 (match-end 1))
  7320. (setq tags (org-get-tags-at))
  7321. (setq head (buffer-substring-no-properties
  7322. (point)
  7323. (progn (skip-chars-forward "^\r\n") (point))))
  7324. (if (string-match org-looking-at-done-regexp head)
  7325. (setq txt nil)
  7326. (setq txt (org-format-agenda-item
  7327. (format "Sched.%2dx: " (- 1 diff)) head
  7328. category tags))))
  7329. (setq txt org-agenda-no-heading-message))
  7330. (when txt
  7331. (org-add-props txt props
  7332. 'org-marker (org-agenda-new-marker pos)
  7333. 'org-hd-marker (org-agenda-new-marker pos1)
  7334. 'priority (+ (- 5 diff) (org-get-priority txt))
  7335. 'category category)
  7336. (push txt ee))))))
  7337. ee))
  7338. (defun org-agenda-get-blocks ()
  7339. "Return the date-range information for agenda display."
  7340. (let* ((props (list 'face nil
  7341. 'org-not-done-regexp org-not-done-regexp
  7342. 'mouse-face 'highlight
  7343. 'keymap org-agenda-keymap
  7344. 'help-echo
  7345. (format "mouse-2 or RET jump to org file %s"
  7346. (abbreviate-file-name buffer-file-name))))
  7347. (regexp org-tr-regexp)
  7348. (d0 (calendar-absolute-from-gregorian date))
  7349. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags pos)
  7350. (goto-char (point-min))
  7351. (while (re-search-forward regexp nil t)
  7352. (catch :skip
  7353. (org-agenda-skip)
  7354. (setq pos (point))
  7355. (setq timestr (match-string 0)
  7356. s1 (match-string 1)
  7357. s2 (match-string 2)
  7358. d1 (time-to-days (org-time-string-to-time s1))
  7359. d2 (time-to-days (org-time-string-to-time s2)))
  7360. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  7361. ;; Only allow days between the limits, because the normal
  7362. ;; date stamps will catch the limits.
  7363. (save-excursion
  7364. (setq marker (org-agenda-new-marker (point)))
  7365. (setq category (org-get-category))
  7366. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  7367. (progn
  7368. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  7369. (goto-char (match-end 1))
  7370. (setq tags (org-get-tags-at))
  7371. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  7372. (setq txt (org-format-agenda-item
  7373. (format (if (= d1 d2) "" "(%d/%d): ")
  7374. (1+ (- d0 d1)) (1+ (- d2 d1)))
  7375. (match-string 1) category tags
  7376. (if (= d0 d1) timestr))))
  7377. (setq txt org-agenda-no-heading-message))
  7378. (org-add-props txt props
  7379. 'org-marker marker 'org-hd-marker hdmarker
  7380. 'priority (org-get-priority txt) 'category category)
  7381. (push txt ee)))
  7382. (goto-char pos)))
  7383. ; (outline-next-heading))) ;FIXME: correct to be removed??????
  7384. ;; Sort the entries by expiration date.
  7385. (nreverse ee)))
  7386. (defconst org-plain-time-of-day-regexp
  7387. (concat
  7388. "\\(\\<[012]?[0-9]"
  7389. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7390. "\\(--?"
  7391. "\\(\\<[012]?[0-9]"
  7392. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  7393. "\\)?")
  7394. "Regular expression to match a plain time or time range.
  7395. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  7396. groups carry important information:
  7397. 0 the full match
  7398. 1 the first time, range or not
  7399. 8 the second time, if it is a range.")
  7400. (defconst org-stamp-time-of-day-regexp
  7401. (concat
  7402. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  7403. "\\([012][0-9]:[0-5][0-9]\\)>"
  7404. "\\(--?"
  7405. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  7406. "Regular expression to match a timestamp time or time range.
  7407. After a match, the following groups carry important information:
  7408. 0 the full match
  7409. 1 date plus weekday, for backreferencing to make sure both times on same day
  7410. 2 the first time, range or not
  7411. 4 the second time, if it is a range.")
  7412. (defvar org-prefix-has-time nil
  7413. "A flag, set by `org-compile-prefix-format'.
  7414. The flag is set if the currently compiled format contains a `%t'.")
  7415. (defvar org-prefix-has-tag nil
  7416. "A flag, set by `org-compile-prefix-format'.
  7417. The flag is set if the currently compiled format contains a `%T'.")
  7418. (defun org-format-agenda-item (extra txt &optional category tags dotime
  7419. noprefix)
  7420. "Format TXT to be inserted into the agenda buffer.
  7421. In particular, it adds the prefix and corresponding text properties. EXTRA
  7422. must be a string and replaces the `%s' specifier in the prefix format.
  7423. CATEGORY (string, symbol or nil) may be used to overrule the default
  7424. category taken from local variable or file name. It will replace the `%c'
  7425. specifier in the format. DOTIME, when non-nil, indicates that a
  7426. time-of-day should be extracted from TXT for sorting of this entry, and for
  7427. the `%t' specifier in the format. When DOTIME is a string, this string is
  7428. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  7429. only the correctly processes TXT should be returned - this is used by
  7430. `org-agenda-change-all-lines'. TAGS can be the tags of the headline."
  7431. (save-match-data
  7432. ;; Diary entries sometimes have extra whitespace at the beginning
  7433. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  7434. (let* ((category (or category
  7435. org-category
  7436. (if buffer-file-name
  7437. (file-name-sans-extension
  7438. (file-name-nondirectory buffer-file-name))
  7439. "")))
  7440. (tag (if tags (nth (1- (length tags)) tags) ""))
  7441. time ;; needed for the eval of the prefix format
  7442. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  7443. (time-of-day (and dotime (org-get-time-of-day ts)))
  7444. stamp plain s0 s1 s2 rtn)
  7445. (when (and dotime time-of-day org-prefix-has-time)
  7446. ;; Extract starting and ending time and move them to prefix
  7447. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  7448. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  7449. (setq s0 (match-string 0 ts)
  7450. s1 (match-string (if plain 1 2) ts)
  7451. s2 (match-string (if plain 8 4) ts))
  7452. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  7453. ;; them, we might want to remove them there to avoid duplication.
  7454. ;; The user can turn this off with a variable.
  7455. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  7456. (string-match (concat (regexp-quote s0) " *") txt)
  7457. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  7458. (= (match-beginning 0) 0)
  7459. t))
  7460. (setq txt (replace-match "" nil nil txt))))
  7461. ;; Normalize the time(s) to 24 hour
  7462. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  7463. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  7464. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  7465. ;; Tags are in the string
  7466. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  7467. (and org-agenda-remove-tags-when-in-prefix
  7468. org-prefix-has-tag))
  7469. (setq txt (replace-match "" t t txt))
  7470. (setq txt (replace-match
  7471. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  7472. (match-string 2 txt))
  7473. t t txt))))
  7474. ;; Create the final string
  7475. (if noprefix
  7476. (setq rtn txt)
  7477. ;; Prepare the variables needed in the eval of the compiled format
  7478. (setq time (cond (s2 (concat s1 "-" s2))
  7479. (s1 (concat s1 "......"))
  7480. (t ""))
  7481. extra (or extra "")
  7482. category (if (symbolp category) (symbol-name category) category))
  7483. ;; Evaluate the compiled format
  7484. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  7485. ;; And finally add the text properties
  7486. (org-add-props rtn nil
  7487. 'category (downcase category) 'tags tags
  7488. 'prefix-length (- (length rtn) (length txt))
  7489. 'time-of-day time-of-day
  7490. 'dotime dotime))))
  7491. (defvar org-agenda-sorting-strategy)
  7492. (defvar org-agenda-sorting-strategy-selected nil)
  7493. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  7494. (catch 'exit
  7495. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  7496. ((and todayp (member 'today (car org-agenda-time-grid))))
  7497. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  7498. ((member 'weekly (car org-agenda-time-grid)))
  7499. (t (throw 'exit list)))
  7500. (let* ((have (delq nil (mapcar
  7501. (lambda (x) (get-text-property 1 'time-of-day x))
  7502. list)))
  7503. (string (nth 1 org-agenda-time-grid))
  7504. (gridtimes (nth 2 org-agenda-time-grid))
  7505. (req (car org-agenda-time-grid))
  7506. (remove (member 'remove-match req))
  7507. new time)
  7508. (if (and (member 'require-timed req) (not have))
  7509. ;; don't show empty grid
  7510. (throw 'exit list))
  7511. (while (setq time (pop gridtimes))
  7512. (unless (and remove (member time have))
  7513. (setq time (int-to-string time))
  7514. (push (org-format-agenda-item
  7515. nil string "" nil
  7516. (concat (substring time 0 -2) ":" (substring time -2)))
  7517. new)
  7518. (put-text-property
  7519. 1 (length (car new)) 'face 'org-time-grid (car new))))
  7520. (if (member 'time-up org-agenda-sorting-strategy-selected)
  7521. (append new list)
  7522. (append list new)))))
  7523. (defun org-compile-prefix-format (key)
  7524. "Compile the prefix format into a Lisp form that can be evaluated.
  7525. The resulting form is returned and stored in the variable
  7526. `org-prefix-format-compiled'."
  7527. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  7528. (let ((s (cond
  7529. ((stringp org-agenda-prefix-format)
  7530. org-agenda-prefix-format)
  7531. ((assq key org-agenda-prefix-format)
  7532. (cdr (assq key org-agenda-prefix-format)))
  7533. (t " %-12:c%?-12t% s")))
  7534. (start 0)
  7535. varform vars var e c f opt)
  7536. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  7537. s start)
  7538. (setq var (cdr (assoc (match-string 4 s)
  7539. '(("c" . category) ("t" . time) ("s" . extra)
  7540. ("T" . tag))))
  7541. c (or (match-string 3 s) "")
  7542. opt (match-beginning 1)
  7543. start (1+ (match-beginning 0)))
  7544. (if (equal var 'time) (setq org-prefix-has-time t))
  7545. (if (equal var 'tag) (setq org-prefix-has-tag t))
  7546. (setq f (concat "%" (match-string 2 s) "s"))
  7547. (if opt
  7548. (setq varform
  7549. `(if (equal "" ,var)
  7550. ""
  7551. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  7552. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  7553. (setq s (replace-match "%s" t nil s))
  7554. (push varform vars))
  7555. (setq vars (nreverse vars))
  7556. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  7557. (defun org-set-sorting-strategy (key)
  7558. (if (symbolp (car org-agenda-sorting-strategy))
  7559. ;; the old format
  7560. (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
  7561. (setq org-agenda-sorting-strategy-selected
  7562. (or (cdr (assq key org-agenda-sorting-strategy))
  7563. (cdr (assq 'agenda org-agenda-sorting-strategy))
  7564. '(time-up category-keep priority-down)))))
  7565. (defun org-get-time-of-day (s &optional string mod24)
  7566. "Check string S for a time of day.
  7567. If found, return it as a military time number between 0 and 2400.
  7568. If not found, return nil.
  7569. The optional STRING argument forces conversion into a 5 character wide string
  7570. HH:MM."
  7571. (save-match-data
  7572. (when
  7573. (or
  7574. (string-match
  7575. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  7576. (string-match
  7577. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  7578. (let* ((h (string-to-number (match-string 1 s)))
  7579. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  7580. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  7581. (am-p (equal ampm "am"))
  7582. (h1 (cond ((not ampm) h)
  7583. ((= h 12) (if am-p 0 12))
  7584. (t (+ h (if am-p 0 12)))))
  7585. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  7586. (mod h1 24) h1))
  7587. (t0 (+ (* 100 h2) m))
  7588. (t1 (concat (if (>= h1 24) "+" " ")
  7589. (if (< t0 100) "0" "")
  7590. (if (< t0 10) "0" "")
  7591. (int-to-string t0))))
  7592. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  7593. (defun org-finalize-agenda-entries (list &optional nosort)
  7594. "Sort and concatenate the agenda items."
  7595. (setq list (mapcar 'org-agenda-highlight-todo list))
  7596. (if nosort
  7597. list
  7598. (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
  7599. (defun org-agenda-highlight-todo (x)
  7600. (let (re pl)
  7601. (if (eq x 'line)
  7602. (save-excursion
  7603. (beginning-of-line 1)
  7604. (setq re (get-text-property (point) 'org-not-done-regexp))
  7605. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  7606. (and (looking-at (concat "[ \t]*" re))
  7607. (add-text-properties (match-beginning 0) (match-end 0)
  7608. '(face org-todo))))
  7609. (setq re (get-text-property 0 'org-not-done-regexp x)
  7610. pl (get-text-property 0 'prefix-length x))
  7611. (and re (equal (string-match re x pl) pl)
  7612. (add-text-properties (match-beginning 0) (match-end 0)
  7613. '(face org-todo) x))
  7614. x)))
  7615. (defsubst org-cmp-priority (a b)
  7616. "Compare the priorities of string A and B."
  7617. (let ((pa (or (get-text-property 1 'priority a) 0))
  7618. (pb (or (get-text-property 1 'priority b) 0)))
  7619. (cond ((> pa pb) +1)
  7620. ((< pa pb) -1)
  7621. (t nil))))
  7622. (defsubst org-cmp-category (a b)
  7623. "Compare the string values of categories of strings A and B."
  7624. (let ((ca (or (get-text-property 1 'category a) ""))
  7625. (cb (or (get-text-property 1 'category b) "")))
  7626. (cond ((string-lessp ca cb) -1)
  7627. ((string-lessp cb ca) +1)
  7628. (t nil))))
  7629. (defsubst org-cmp-tag (a b)
  7630. "Compare the string values of categories of strings A and B."
  7631. (let ((ta (car (last (get-text-property 1 'tags a))))
  7632. (tb (car (last (get-text-property 1 'tags b)))))
  7633. (cond ((not ta) +1)
  7634. ((not tb) -1)
  7635. ((string-lessp ta tb) -1)
  7636. ((string-lessp tb ta) +1)
  7637. (t nil))))
  7638. (defsubst org-cmp-time (a b)
  7639. "Compare the time-of-day values of strings A and B."
  7640. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  7641. (ta (or (get-text-property 1 'time-of-day a) def))
  7642. (tb (or (get-text-property 1 'time-of-day b) def)))
  7643. (cond ((< ta tb) -1)
  7644. ((< tb ta) +1)
  7645. (t nil))))
  7646. (defun org-entries-lessp (a b)
  7647. "Predicate for sorting agenda entries."
  7648. ;; The following variables will be used when the form is evaluated.
  7649. (let* ((time-up (org-cmp-time a b))
  7650. (time-down (if time-up (- time-up) nil))
  7651. (priority-up (org-cmp-priority a b))
  7652. (priority-down (if priority-up (- priority-up) nil))
  7653. (category-up (org-cmp-category a b))
  7654. (category-down (if category-up (- category-up) nil))
  7655. (category-keep (if category-up +1 nil))
  7656. (tag-up (org-cmp-tag a b))
  7657. (tag-down (if tag-up (- tag-up) nil)))
  7658. (cdr (assoc
  7659. (eval (cons 'or org-agenda-sorting-strategy-selected))
  7660. '((-1 . t) (1 . nil) (nil . nil))))))
  7661. (defun org-agenda-show-priority ()
  7662. "Show the priority of the current item.
  7663. This priority is composed of the main priority given with the [#A] cookies,
  7664. and by additional input from the age of a schedules or deadline entry."
  7665. (interactive)
  7666. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  7667. (message "Priority is %d" (if pri pri -1000))))
  7668. (defun org-agenda-show-tags ()
  7669. "Show the tags applicable to the current item."
  7670. (interactive)
  7671. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  7672. (if tags
  7673. (message "Tags are :%s:"
  7674. (org-no-properties (mapconcat 'identity tags ":")))
  7675. (message "No tags associated with this line"))))
  7676. (defun org-agenda-goto (&optional highlight)
  7677. "Go to the Org-mode file which contains the item at point."
  7678. (interactive)
  7679. (let* ((marker (or (get-text-property (point) 'org-marker)
  7680. (org-agenda-error)))
  7681. (buffer (marker-buffer marker))
  7682. (pos (marker-position marker)))
  7683. (switch-to-buffer-other-window buffer)
  7684. (widen)
  7685. (goto-char pos)
  7686. (when (org-mode-p)
  7687. (org-show-hidden-entry)
  7688. (save-excursion
  7689. (and (outline-next-heading)
  7690. (org-flag-heading nil)))) ; show the next heading
  7691. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  7692. (defun org-agenda-switch-to ()
  7693. "Go to the Org-mode file which contains the item at point."
  7694. (interactive)
  7695. (let* ((marker (or (get-text-property (point) 'org-marker)
  7696. (org-agenda-error)))
  7697. (buffer (marker-buffer marker))
  7698. (pos (marker-position marker)))
  7699. (switch-to-buffer buffer)
  7700. (delete-other-windows)
  7701. (widen)
  7702. (goto-char pos)
  7703. (when (org-mode-p)
  7704. (org-show-hidden-entry)
  7705. (save-excursion
  7706. (and (outline-next-heading)
  7707. (org-flag-heading nil)))))) ; show the next heading
  7708. (defun org-agenda-goto-mouse (ev)
  7709. "Go to the Org-mode file which contains the item at the mouse click."
  7710. (interactive "e")
  7711. (mouse-set-point ev)
  7712. (org-agenda-goto))
  7713. (defun org-agenda-show ()
  7714. "Display the Org-mode file which contains the item at point."
  7715. (interactive)
  7716. (let ((win (selected-window)))
  7717. (org-agenda-goto t)
  7718. (select-window win)))
  7719. (defun org-agenda-recenter (arg)
  7720. "Display the Org-mode file which contains the item at point and recenter."
  7721. (interactive "P")
  7722. (let ((win (selected-window)))
  7723. (org-agenda-goto t)
  7724. (recenter arg)
  7725. (select-window win)))
  7726. (defun org-agenda-show-mouse (ev)
  7727. "Display the Org-mode file which contains the item at the mouse click."
  7728. (interactive "e")
  7729. (mouse-set-point ev)
  7730. (org-agenda-show))
  7731. (defun org-agenda-check-no-diary ()
  7732. "Check if the entry is a diary link and abort if yes."
  7733. (if (get-text-property (point) 'org-agenda-diary-link)
  7734. (org-agenda-error)))
  7735. (defun org-agenda-error ()
  7736. (error "Command not allowed in this line"))
  7737. (defvar org-last-heading-marker (make-marker)
  7738. "Marker pointing to the headline that last changed its TODO state
  7739. by a remote command from the agenda.")
  7740. (defun org-agenda-todo (&optional arg)
  7741. "Cycle TODO state of line at point, also in Org-mode file.
  7742. This changes the line at point, all other lines in the agenda referring to
  7743. the same tree node, and the headline of the tree node in the Org-mode file."
  7744. (interactive "P")
  7745. (org-agenda-check-no-diary)
  7746. (let* ((col (current-column))
  7747. (marker (or (get-text-property (point) 'org-marker)
  7748. (org-agenda-error)))
  7749. (buffer (marker-buffer marker))
  7750. (pos (marker-position marker))
  7751. (hdmarker (get-text-property (point) 'org-hd-marker))
  7752. (buffer-read-only nil)
  7753. newhead)
  7754. (with-current-buffer buffer
  7755. (widen)
  7756. (goto-char pos)
  7757. (org-show-hidden-entry)
  7758. (save-excursion
  7759. (and (outline-next-heading)
  7760. (org-flag-heading nil))) ; show the next heading
  7761. (org-todo arg)
  7762. (and (bolp) (forward-char 1))
  7763. (setq newhead (org-get-heading))
  7764. (save-excursion
  7765. (org-back-to-heading)
  7766. (move-marker org-last-heading-marker (point))))
  7767. (beginning-of-line 1)
  7768. (save-excursion
  7769. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  7770. (move-to-column col)))
  7771. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  7772. "Change all lines in the agenda buffer which match HDMARKER.
  7773. The new content of the line will be NEWHEAD (as modified by
  7774. `org-format-agenda-item'). HDMARKER is checked with
  7775. `equal' against all `org-hd-marker' text properties in the file.
  7776. If FIXFACE is non-nil, the face of each item is modified acording to
  7777. the new TODO state."
  7778. (let* (props m pl undone-face done-face finish new dotime cat tags)
  7779. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  7780. (save-excursion
  7781. (goto-char (point-max))
  7782. (beginning-of-line 1)
  7783. (while (not finish)
  7784. (setq finish (bobp))
  7785. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  7786. (equal m hdmarker))
  7787. (setq props (text-properties-at (point))
  7788. dotime (get-text-property (point) 'dotime)
  7789. cat (get-text-property (point) 'category)
  7790. tags (get-text-property (point) 'tags)
  7791. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  7792. pl (get-text-property (point) 'prefix-length)
  7793. undone-face (get-text-property (point) 'undone-face)
  7794. done-face (get-text-property (point) 'done-face))
  7795. (move-to-column pl)
  7796. (if (looking-at ".*")
  7797. (progn
  7798. (replace-match new t t)
  7799. (beginning-of-line 1)
  7800. (add-text-properties (point-at-bol) (point-at-eol) props)
  7801. (when fixface
  7802. (add-text-properties
  7803. (point-at-bol) (point-at-eol)
  7804. (list 'face
  7805. (if org-last-todo-state-is-todo
  7806. undone-face done-face)))
  7807. (org-agenda-highlight-todo 'line))
  7808. (org-agenda-align-tags 'line)
  7809. (beginning-of-line 1))
  7810. (error "Line update did not work")))
  7811. (beginning-of-line 0)))))
  7812. (defun org-agenda-align-tags (&optional line)
  7813. "Align all tags in agenda items to `org-agenda-align-tags-to-column'."
  7814. (let ((buffer-read-only))
  7815. (save-excursion
  7816. (goto-char (if line (point-at-bol) (point-min)))
  7817. (while (re-search-forward "\\([ \t]+\\):[a-zA-Z0-9_@:]+:[ \t]*$"
  7818. (if line (point-at-eol) nil) t)
  7819. (delete-region (match-beginning 1) (match-end 1))
  7820. (goto-char (match-beginning 1))
  7821. (insert (org-add-props
  7822. (make-string (max 1 (- org-agenda-align-tags-to-column
  7823. (current-column))) ?\ )
  7824. (text-properties-at (point))))))))
  7825. (defun org-agenda-priority-up ()
  7826. "Increase the priority of line at point, also in Org-mode file."
  7827. (interactive)
  7828. (org-agenda-priority 'up))
  7829. (defun org-agenda-priority-down ()
  7830. "Decrease the priority of line at point, also in Org-mode file."
  7831. (interactive)
  7832. (org-agenda-priority 'down))
  7833. (defun org-agenda-priority (&optional force-direction)
  7834. "Set the priority of line at point, also in Org-mode file.
  7835. This changes the line at point, all other lines in the agenda referring to
  7836. the same tree node, and the headline of the tree node in the Org-mode file."
  7837. (interactive)
  7838. (org-agenda-check-no-diary)
  7839. (let* ((marker (or (get-text-property (point) 'org-marker)
  7840. (org-agenda-error)))
  7841. (buffer (marker-buffer marker))
  7842. (pos (marker-position marker))
  7843. (hdmarker (get-text-property (point) 'org-hd-marker))
  7844. (buffer-read-only nil)
  7845. newhead)
  7846. (with-current-buffer buffer
  7847. (widen)
  7848. (goto-char pos)
  7849. (org-show-hidden-entry)
  7850. (save-excursion
  7851. (and (outline-next-heading)
  7852. (org-flag-heading nil))) ; show the next heading
  7853. (funcall 'org-priority force-direction)
  7854. (end-of-line 1)
  7855. (setq newhead (org-get-heading)))
  7856. (org-agenda-change-all-lines newhead hdmarker)
  7857. (beginning-of-line 1)))
  7858. (defun org-get-tags-at (&optional pos)
  7859. "Get a list of all headline tags applicable at POS.
  7860. POS defaults to point. If tags are inherited, the list contains
  7861. the targets in the same sequence as the headlines appear, i.e.
  7862. the tags of the current headline come last."
  7863. (interactive)
  7864. (let (tags)
  7865. (save-excursion
  7866. (goto-char (or pos (point)))
  7867. (save-match-data
  7868. (org-back-to-heading t)
  7869. (condition-case nil
  7870. (while t
  7871. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  7872. (setq tags (append (org-split-string
  7873. (org-match-string-no-properties 1) ":")
  7874. tags)))
  7875. (or org-use-tag-inheritance (error ""))
  7876. (org-up-heading-all 1))
  7877. (error nil))))
  7878. tags))
  7879. (defun org-agenda-set-tags ()
  7880. "Set tags for the current headline."
  7881. (interactive)
  7882. (org-agenda-check-no-diary)
  7883. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  7884. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  7885. (org-agenda-error)))
  7886. (buffer (marker-buffer hdmarker))
  7887. (pos (marker-position hdmarker))
  7888. (buffer-read-only nil)
  7889. newhead)
  7890. (with-current-buffer buffer
  7891. (widen)
  7892. (goto-char pos)
  7893. (org-show-hidden-entry)
  7894. (save-excursion
  7895. (and (outline-next-heading)
  7896. (org-flag-heading nil))) ; show the next heading
  7897. (call-interactively 'org-set-tags)
  7898. (end-of-line 1)
  7899. (setq newhead (org-get-heading)))
  7900. (org-agenda-change-all-lines newhead hdmarker)
  7901. (beginning-of-line 1)))
  7902. (defun org-agenda-date-later (arg &optional what)
  7903. "Change the date of this item to one day later."
  7904. (interactive "p")
  7905. (org-agenda-check-type t 'agenda 'timeline)
  7906. (org-agenda-check-no-diary)
  7907. (let* ((marker (or (get-text-property (point) 'org-marker)
  7908. (org-agenda-error)))
  7909. (buffer (marker-buffer marker))
  7910. (pos (marker-position marker)))
  7911. (with-current-buffer buffer
  7912. (widen)
  7913. (goto-char pos)
  7914. (if (not (org-at-timestamp-p))
  7915. (error "Cannot find time stamp"))
  7916. (org-timestamp-change arg (or what 'day))
  7917. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7918. (defun org-agenda-date-earlier (arg &optional what)
  7919. "Change the date of this item to one day earlier."
  7920. (interactive "p")
  7921. (org-agenda-date-later (- arg) what))
  7922. (defun org-agenda-date-prompt (arg)
  7923. "Change the date of this item. Date is prompted for, with default today.
  7924. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  7925. be used to request time specification in the time stamp."
  7926. (interactive "P")
  7927. (org-agenda-check-type t 'agenda 'timeline)
  7928. (org-agenda-check-no-diary)
  7929. (let* ((marker (or (get-text-property (point) 'org-marker)
  7930. (org-agenda-error)))
  7931. (buffer (marker-buffer marker))
  7932. (pos (marker-position marker)))
  7933. (with-current-buffer buffer
  7934. (widen)
  7935. (goto-char pos)
  7936. (if (not (org-at-timestamp-p))
  7937. (error "Cannot find time stamp"))
  7938. (org-time-stamp arg)
  7939. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  7940. (defun org-agenda-schedule (arg)
  7941. "Schedule the item at point."
  7942. (interactive "P")
  7943. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7944. (org-agenda-check-no-diary)
  7945. (let* ((marker (or (get-text-property (point) 'org-marker)
  7946. (org-agenda-error)))
  7947. (buffer (marker-buffer marker))
  7948. (pos (marker-position marker))
  7949. (org-insert-labeled-timestamps-at-point nil)
  7950. ts)
  7951. (with-current-buffer buffer
  7952. (widen)
  7953. (goto-char pos)
  7954. (setq ts (org-schedule))
  7955. (message "Item scheduled for %s" ts))))
  7956. (defun org-agenda-deadline (arg)
  7957. "Schedule the item at point."
  7958. (interactive "P")
  7959. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  7960. (org-agenda-check-no-diary)
  7961. (let* ((marker (or (get-text-property (point) 'org-marker)
  7962. (org-agenda-error)))
  7963. (buffer (marker-buffer marker))
  7964. (pos (marker-position marker))
  7965. (org-insert-labeled-timestamps-at-point nil)
  7966. ts)
  7967. (with-current-buffer buffer
  7968. (widen)
  7969. (goto-char pos)
  7970. (setq ts (org-deadline))
  7971. (message "Deadline for this item set to %s" ts))))
  7972. (defun org-get-heading ()
  7973. "Return the heading of the current entry, without the stars."
  7974. (save-excursion
  7975. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  7976. (if (and (re-search-backward "[\r\n]\\*" nil t)
  7977. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  7978. (match-string 1)
  7979. "")))
  7980. (defun org-agenda-clock-in (&optional arg)
  7981. "Start the clock on the currently selected item."
  7982. (interactive "P")
  7983. (org-agenda-check-no-diary)
  7984. (let* ((marker (or (get-text-property (point) 'org-marker)
  7985. (org-agenda-error)))
  7986. (pos (marker-position marker)))
  7987. (with-current-buffer (marker-buffer marker)
  7988. (widen)
  7989. (goto-char pos)
  7990. (org-clock-in))))
  7991. (defun org-agenda-diary-entry ()
  7992. "Make a diary entry, like the `i' command from the calendar.
  7993. All the standard commands work: block, weekly etc."
  7994. (interactive)
  7995. (org-agenda-check-type t 'agenda 'timeline)
  7996. (require 'diary-lib)
  7997. (let* ((char (progn
  7998. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  7999. (read-char-exclusive)))
  8000. (cmd (cdr (assoc char
  8001. '((?d . insert-diary-entry)
  8002. (?w . insert-weekly-diary-entry)
  8003. (?m . insert-monthly-diary-entry)
  8004. (?y . insert-yearly-diary-entry)
  8005. (?a . insert-anniversary-diary-entry)
  8006. (?b . insert-block-diary-entry)
  8007. (?c . insert-cyclic-diary-entry)))))
  8008. (oldf (symbol-function 'calendar-cursor-to-date))
  8009. (point (point))
  8010. (mark (or (mark t) (point))))
  8011. (unless cmd
  8012. (error "No command associated with <%c>" char))
  8013. (unless (and (get-text-property point 'day)
  8014. (or (not (equal ?b char))
  8015. (get-text-property mark 'day)))
  8016. (error "Don't know which date to use for diary entry"))
  8017. ;; We implement this by hacking the `calendar-cursor-to-date' function
  8018. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  8019. (let ((calendar-mark-ring
  8020. (list (calendar-gregorian-from-absolute
  8021. (or (get-text-property mark 'day)
  8022. (get-text-property point 'day))))))
  8023. (unwind-protect
  8024. (progn
  8025. (fset 'calendar-cursor-to-date
  8026. (lambda (&optional error)
  8027. (calendar-gregorian-from-absolute
  8028. (get-text-property point 'day))))
  8029. (call-interactively cmd))
  8030. (fset 'calendar-cursor-to-date oldf)))))
  8031. (defun org-agenda-execute-calendar-command (cmd)
  8032. "Execute a calendar command from the agenda, with the date associated to
  8033. the cursor position."
  8034. (org-agenda-check-type t 'agenda 'timeline)
  8035. (require 'diary-lib)
  8036. (unless (get-text-property (point) 'day)
  8037. (error "Don't know which date to use for calendar command"))
  8038. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  8039. (point (point))
  8040. (date (calendar-gregorian-from-absolute
  8041. (get-text-property point 'day)))
  8042. (displayed-day (extract-calendar-day date))
  8043. (displayed-month (extract-calendar-month date))
  8044. (displayed-year (extract-calendar-year date)))
  8045. (unwind-protect
  8046. (progn
  8047. (fset 'calendar-cursor-to-date
  8048. (lambda (&optional error)
  8049. (calendar-gregorian-from-absolute
  8050. (get-text-property point 'day))))
  8051. (call-interactively cmd))
  8052. (fset 'calendar-cursor-to-date oldf))))
  8053. (defun org-agenda-phases-of-moon ()
  8054. "Display the phases of the moon for the 3 months around the cursor date."
  8055. (interactive)
  8056. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  8057. (defun org-agenda-holidays ()
  8058. "Display the holidays for the 3 months around the cursor date."
  8059. (interactive)
  8060. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  8061. (defun org-agenda-sunrise-sunset (arg)
  8062. "Display sunrise and sunset for the cursor date.
  8063. Latitude and longitude can be specified with the variables
  8064. `calendar-latitude' and `calendar-longitude'. When called with prefix
  8065. argument, latitude and longitude will be prompted for."
  8066. (interactive "P")
  8067. (let ((calendar-longitude (if arg nil calendar-longitude))
  8068. (calendar-latitude (if arg nil calendar-latitude))
  8069. (calendar-location-name
  8070. (if arg "the given coordinates" calendar-location-name)))
  8071. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  8072. (defun org-agenda-goto-calendar ()
  8073. "Open the Emacs calendar with the date at the cursor."
  8074. (interactive)
  8075. (org-agenda-check-type t 'agenda 'timeline)
  8076. (let* ((day (or (get-text-property (point) 'day)
  8077. (error "Don't know which date to open in calendar")))
  8078. (date (calendar-gregorian-from-absolute day))
  8079. (calendar-move-hook nil)
  8080. (view-calendar-holidays-initially nil)
  8081. (view-diary-entries-initially nil))
  8082. (calendar)
  8083. (calendar-goto-date date)))
  8084. (defun org-calendar-goto-agenda ()
  8085. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  8086. This is a command that has to be installed in `calendar-mode-map'."
  8087. (interactive)
  8088. (org-agenda-list nil (calendar-absolute-from-gregorian
  8089. (calendar-cursor-to-date))
  8090. nil t))
  8091. (defun org-agenda-convert-date ()
  8092. (interactive)
  8093. (org-agenda-check-type t 'agenda 'timeline)
  8094. (let ((day (get-text-property (point) 'day))
  8095. date s)
  8096. (unless day
  8097. (error "Don't know which date to convert"))
  8098. (setq date (calendar-gregorian-from-absolute day))
  8099. (setq s (concat
  8100. "Gregorian: " (calendar-date-string date) "\n"
  8101. "ISO: " (calendar-iso-date-string date) "\n"
  8102. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  8103. "Julian: " (calendar-julian-date-string date) "\n"
  8104. "Astron. JD: " (calendar-astro-date-string date)
  8105. " (Julian date number at noon UTC)\n"
  8106. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  8107. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  8108. "French: " (calendar-french-date-string date) "\n"
  8109. "Mayan: " (calendar-mayan-date-string date) "\n"
  8110. "Coptic: " (calendar-coptic-date-string date) "\n"
  8111. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  8112. "Persian: " (calendar-persian-date-string date) "\n"
  8113. "Chinese: " (calendar-chinese-date-string date) "\n"))
  8114. (with-output-to-temp-buffer "*Dates*"
  8115. (princ s))
  8116. (if (fboundp 'fit-window-to-buffer)
  8117. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  8118. ;;; Tags
  8119. (defun org-scan-tags (action matcher &optional todo-only)
  8120. "Scan headline tags with inheritance and produce output ACTION.
  8121. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8122. evaluated, testing if a given set of tags qualifies a headline for
  8123. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8124. are included in the output."
  8125. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8126. (mapconcat 'regexp-quote
  8127. (nreverse (cdr (reverse org-todo-keywords)))
  8128. "\\|")
  8129. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  8130. (props (list 'face nil
  8131. 'done-face 'org-done
  8132. 'undone-face nil
  8133. 'mouse-face 'highlight
  8134. 'org-not-done-regexp org-not-done-regexp
  8135. 'keymap org-agenda-keymap
  8136. 'help-echo
  8137. (format "mouse-2 or RET jump to org file %s"
  8138. (abbreviate-file-name buffer-file-name))))
  8139. lspos
  8140. tags tags-list tags-alist (llast 0) rtn level category i txt
  8141. todo marker)
  8142. (save-excursion
  8143. (goto-char (point-min))
  8144. (when (eq action 'sparse-tree) (org-overview))
  8145. (while (re-search-forward re nil t)
  8146. (catch :skip
  8147. (and (eq action 'agenda) (org-agenda-skip))
  8148. (setq todo (if (match-end 1) (match-string 2))
  8149. tags (if (match-end 4) (match-string 4)))
  8150. (goto-char (setq lspos (1+ (match-beginning 0))))
  8151. (setq level (funcall outline-level)
  8152. category (org-get-category))
  8153. (setq i llast llast level)
  8154. ;; remove tag lists from same and sublevels
  8155. (while (>= i level)
  8156. (when (setq entry (assoc i tags-alist))
  8157. (setq tags-alist (delete entry tags-alist)))
  8158. (setq i (1- i)))
  8159. ;; add the nex tags
  8160. (when tags
  8161. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8162. tags-alist
  8163. (cons (cons level tags) tags-alist)))
  8164. ;; compile tags for current headline
  8165. (setq tags-list
  8166. (if org-use-tag-inheritance
  8167. (apply 'append (mapcar 'cdr tags-alist))
  8168. tags))
  8169. (when (and (or (not todo-only) todo)
  8170. (eval matcher)
  8171. (or (not org-agenda-skip-archived-trees)
  8172. (not (member org-archive-tag tags-list))))
  8173. ;; list this headline
  8174. (if (eq action 'sparse-tree)
  8175. (progn
  8176. (org-show-hierarchy-above))
  8177. (setq txt (org-format-agenda-item
  8178. ""
  8179. (concat
  8180. (if org-tags-match-list-sublevels
  8181. (make-string (1- level) ?.) "")
  8182. (org-get-heading))
  8183. category tags-list))
  8184. (goto-char lspos)
  8185. (setq marker (org-agenda-new-marker))
  8186. (org-add-props txt props
  8187. 'org-marker marker 'org-hd-marker marker 'category category)
  8188. (push txt rtn))
  8189. ;; if we are to skip sublevels, jump to end of subtree
  8190. (point)
  8191. (or org-tags-match-list-sublevels (org-end-of-subtree))))))
  8192. (when (and (eq action 'sparse-tree)
  8193. (not org-sparse-tree-open-archived-trees))
  8194. (org-hide-archived-subtrees (point-min) (point-max)))
  8195. (nreverse rtn)))
  8196. (defun org-tags-sparse-tree (&optional arg match)
  8197. "Create a sparse tree according to tags search string MATCH.
  8198. MATCH can contain positive and negative selection of tags, like
  8199. \"+WORK+URGENT-WITHBOSS\"."
  8200. (interactive "P")
  8201. (let ((org-show-following-heading nil)
  8202. (org-show-hierarchy-above nil))
  8203. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  8204. (defun org-make-tags-matcher (match)
  8205. "Create the TAGS matcher form for the tags-selecting string MATCH."
  8206. (unless match
  8207. ;; Get a new match request, with completion
  8208. (setq org-last-tags-completion-table
  8209. (or (org-get-buffer-tags)
  8210. org-last-tags-completion-table))
  8211. (setq match (completing-read
  8212. "Tags: " 'org-tags-completion-function nil nil nil
  8213. 'org-tags-history)))
  8214. ;; parse the string and create a lisp form
  8215. (let ((match0 match) minus tag mm matcher orterms term orlist)
  8216. (setq orterms (org-split-string match "|"))
  8217. (while (setq term (pop orterms))
  8218. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  8219. (setq minus (and (match-end 1)
  8220. (equal (match-string 1 term) "-"))
  8221. tag (match-string 2 term)
  8222. term (substring term (match-end 0))
  8223. mm (list 'member (downcase tag) 'tags-list)
  8224. mm (if minus (list 'not mm) mm))
  8225. (push mm matcher))
  8226. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  8227. orlist)
  8228. (setq matcher nil))
  8229. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8230. ;; Return the string and lisp forms of the matcher
  8231. (cons match0 matcher)))
  8232. ;;;###autoload
  8233. (defun org-tags-view (&optional todo-only match keep-modes)
  8234. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  8235. The prefix arg TODO-ONLY limits the search to TODO entries."
  8236. (interactive "P")
  8237. (org-compile-prefix-format 'tags)
  8238. (org-set-sorting-strategy 'tags)
  8239. (let* ((org-agenda-keep-modes keep-modes)
  8240. (org-tags-match-list-sublevels
  8241. (if todo-only t org-tags-match-list-sublevels))
  8242. (win (selected-window))
  8243. (completion-ignore-case t)
  8244. rtn rtnall files file pos matcher
  8245. buffer)
  8246. (setq matcher (org-make-tags-matcher match)
  8247. match (car matcher) matcher (cdr matcher))
  8248. (org-prepare-agenda)
  8249. (setq org-agenda-redo-command
  8250. (list 'org-tags-view (list 'quote todo-only)
  8251. (list 'if 'current-prefix-arg nil match) t))
  8252. (setq files (org-agenda-files)
  8253. rtnall nil)
  8254. (while (setq file (pop files))
  8255. (catch 'nextfile
  8256. (org-check-agenda-file file)
  8257. (setq buffer (if (file-exists-p file)
  8258. (org-get-agenda-file-buffer file)
  8259. (error "No such file %s" file)))
  8260. (if (not buffer)
  8261. ;; If file does not exist, merror message to agenda
  8262. (setq rtn (list
  8263. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  8264. rtnall (append rtnall rtn))
  8265. (with-current-buffer buffer
  8266. (unless (org-mode-p)
  8267. (error "Agenda file %s is not in `org-mode'" file))
  8268. (setq org-category-table (org-get-category-table))
  8269. (save-excursion
  8270. (save-restriction
  8271. (if org-agenda-restrict
  8272. (narrow-to-region org-agenda-restrict-begin
  8273. org-agenda-restrict-end)
  8274. (widen))
  8275. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  8276. (setq rtnall (append rtnall rtn))))))))
  8277. (insert "Headlines with TAGS match: ")
  8278. (add-text-properties (point-min) (1- (point))
  8279. (list 'face 'org-level-3))
  8280. (setq pos (point))
  8281. (insert match "\n")
  8282. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  8283. (setq pos (point))
  8284. (unless org-agenda-multi
  8285. (insert "Press `C-u r' to search again with new search string\n"))
  8286. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  8287. (when rtnall
  8288. (insert (org-finalize-agenda-entries rtnall) "\n"))
  8289. (goto-char (point-min))
  8290. (org-fit-agenda-window)
  8291. (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
  8292. (org-agenda-align-tags)
  8293. (setq buffer-read-only t)
  8294. (if (not org-select-agenda-window) (select-window win))))
  8295. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8296. (defun org-set-tags (&optional arg just-align)
  8297. "Set the tags for the current headline.
  8298. With prefix ARG, realign all tags in headings in the current buffer."
  8299. (interactive "P")
  8300. (let* ((re (concat "^" outline-regexp))
  8301. (col (current-column))
  8302. (current (org-get-tags))
  8303. table current-tags inherited-tags ; computed below when needed
  8304. tags hd empty invis)
  8305. (if arg
  8306. (save-excursion
  8307. (goto-char (point-min))
  8308. (while (re-search-forward re nil t)
  8309. (org-set-tags nil t))
  8310. (message "All tags realigned to column %d" org-tags-column))
  8311. (if just-align
  8312. (setq tags current)
  8313. (setq table (or org-tag-alist (org-get-buffer-tags))
  8314. org-last-tags-completion-table table
  8315. current-tags (org-split-string current ":")
  8316. inherited-tags (nreverse
  8317. (nthcdr (length current-tags)
  8318. (nreverse (org-get-tags-at))))
  8319. tags
  8320. (if (or (eq t org-use-fast-tag-selection)
  8321. (and org-use-fast-tag-selection
  8322. (delq nil (mapcar 'cdr table))))
  8323. (org-fast-tag-selection current-tags inherited-tags table)
  8324. (let ((org-add-colon-after-tag-completion t))
  8325. (completing-read "Tags: " 'org-tags-completion-function
  8326. nil nil current 'org-tags-history))))
  8327. (while (string-match "[-+&]+" tags)
  8328. (setq tags (replace-match ":" t t tags))))
  8329. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  8330. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8331. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8332. (if (equal current "")
  8333. (progn
  8334. (end-of-line 1)
  8335. (or empty (insert " ")))
  8336. (beginning-of-line 1)
  8337. (setq invis (org-invisible-p))
  8338. (looking-at (concat ".*?\\([ \t]*" (regexp-quote current) "\\)[ \t]*"))
  8339. (delete-region (match-beginning 1) (match-end 1))
  8340. (goto-char (match-beginning 1))
  8341. (insert (if empty "" " ")))
  8342. (if (equal tags "")
  8343. (save-excursion
  8344. (beginning-of-line 1)
  8345. (skip-chars-forward "*")
  8346. (if (= (char-after) ?\ ) (forward-char 1))
  8347. (and (re-search-forward "[ \t]+$" (point-at-eol) t)
  8348. (replace-match "")))
  8349. (move-to-column (max (current-column)
  8350. (if (> org-tags-column 0)
  8351. org-tags-column
  8352. (- (- org-tags-column) (length tags))))
  8353. t)
  8354. (insert tags)
  8355. (if (and (not invis) (org-invisible-p))
  8356. (outline-flag-region (point-at-bol) (point) nil)))
  8357. (move-to-column col))))
  8358. (defun org-tags-completion-function (string predicate &optional flag)
  8359. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8360. (confirm (lambda (x) (stringp (car x)))))
  8361. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8362. (setq s1 (match-string 1 string)
  8363. s2 (match-string 2 string))
  8364. (setq s1 "" s2 string))
  8365. (cond
  8366. ((eq flag nil)
  8367. ;; try completion
  8368. (setq rtn (try-completion s2 ctable confirm))
  8369. (if (stringp rtn)
  8370. (concat s1 s2 (substring rtn (length s2))
  8371. (if (and org-add-colon-after-tag-completion
  8372. (assoc rtn ctable))
  8373. ":" "")))
  8374. )
  8375. ((eq flag t)
  8376. ;; all-completions
  8377. (all-completions s2 ctable confirm)
  8378. )
  8379. ((eq flag 'lambda)
  8380. ;; exact match?
  8381. (assoc s2 ctable)))
  8382. ))
  8383. (defun org-fast-tag-insert (kwd tags face &optional end)
  8384. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8385. (insert (format "%-12s" (concat kwd ":"))
  8386. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8387. (or end "")))
  8388. (defun org-fast-tag-selection (current inherited table)
  8389. "Fast tag selection with single keys.
  8390. CURRENT is the current list of tags in the headline, INHERITED is the
  8391. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8392. possibly with grouping information.
  8393. If the keys are nil, a-z are automatically assigned.
  8394. Returns the new tags string, or nil to not change the current settings."
  8395. (let* ((maxlen (apply 'max (mapcar
  8396. (lambda (x)
  8397. (if (stringp (car x)) (string-width (car x)) 0))
  8398. table)))
  8399. (fwidth (+ maxlen 3 1 3))
  8400. (ncol (/ (- (window-width) 4) fwidth))
  8401. (i-face 'org-done)
  8402. (c-face 'org-tag)
  8403. tg cnt e c char c1 c2 ntable tbl rtn
  8404. groups ingroup)
  8405. (save-window-excursion
  8406. (delete-other-windows)
  8407. (split-window-vertically)
  8408. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  8409. (erase-buffer)
  8410. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8411. (org-fast-tag-insert "Current" current c-face "\n\n")
  8412. (setq tbl table char ?a cnt 0)
  8413. (while (setq e (pop tbl))
  8414. (cond
  8415. ((equal e '(:startgroup))
  8416. (push '() groups) (setq ingroup t)
  8417. (when (not (= cnt 0))
  8418. (setq cnt 0)
  8419. (insert "\n"))
  8420. (insert "{ "))
  8421. ((equal e '(:endgroup))
  8422. (setq ingroup nil cnt 0)
  8423. (insert "}\n"))
  8424. (t
  8425. (setq tg (car e) c2 nil)
  8426. (if (cdr e)
  8427. (setq c (cdr e))
  8428. ;; automatically assign a character.
  8429. (setq c1 (string-to-char
  8430. (downcase (substring
  8431. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8432. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8433. (while (or (rassoc char ntable) (rassoc char table))
  8434. (setq char (1+ char)))
  8435. (setq c2 c1))
  8436. (setq c (or c2 char)))
  8437. (if ingroup (push tg (car groups)))
  8438. (setq tg (org-add-props tg nil 'face
  8439. (cond
  8440. ((member tg current) c-face)
  8441. ((member tg inherited) i-face)
  8442. (t nil))))
  8443. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8444. (insert "[" c "] " tg (make-string
  8445. (- fwidth 4 (length tg)) ?\ ))
  8446. (push (cons tg c) ntable)
  8447. (when (= (setq cnt (1+ cnt)) ncol)
  8448. (insert "\n")
  8449. (if ingroup (insert " "))
  8450. (setq cnt 0)))))
  8451. (setq ntable (nreverse ntable))
  8452. (insert "\n")
  8453. (goto-char (point-min))
  8454. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  8455. (setq rtn
  8456. (catch 'exit
  8457. (while t
  8458. (message "[key]:Toggle SPC: clear current RET accept%s"
  8459. (if groups " [!] ignore goups" ""))
  8460. (setq c (read-char-exclusive))
  8461. (cond
  8462. ((= c ?\r) (throw 'exit t))
  8463. ((= c ?!)
  8464. (setq groups nil)
  8465. (goto-char (point-min))
  8466. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8467. ((or (= c ?\C-g)
  8468. (and (= c ?q) (not (rassoc c ntable))))
  8469. (setq quit-flag t))
  8470. ((= c ?\ ) (setq current nil))
  8471. ((setq e (rassoc c ntable) tg (car e))
  8472. (if (member tg current)
  8473. (setq current (delete tg current))
  8474. (loop for g in groups do
  8475. (if (member tg g)
  8476. (mapcar (lambda (x)
  8477. (setq current (delete x current)))
  8478. g)))
  8479. (setq current (cons tg current)))))
  8480. ;; Create a sorted list
  8481. (setq current
  8482. (sort current
  8483. (lambda (a b)
  8484. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8485. (goto-char (point-min))
  8486. (beginning-of-line 2)
  8487. (delete-region (point) (point-at-eol))
  8488. (org-fast-tag-insert "Current" current c-face)
  8489. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  8490. (setq tg (match-string 1))
  8491. (add-text-properties (match-beginning 1) (match-end 1)
  8492. (list 'face
  8493. (cond
  8494. ((member tg current) c-face)
  8495. ((member tg inherited) i-face)
  8496. (t nil)))))
  8497. (goto-char (point-min)))))
  8498. (if rtn
  8499. (mapconcat 'identity current ":")
  8500. nil))))
  8501. (defun org-get-tags ()
  8502. "Get the TAGS string in the current headline."
  8503. (unless (org-on-heading-p t)
  8504. (error "Not on a heading"))
  8505. (save-excursion
  8506. (beginning-of-line 1)
  8507. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  8508. (org-match-string-no-properties 1)
  8509. "")))
  8510. (defun org-get-buffer-tags ()
  8511. "Get a table of all tags used in the buffer, for completion."
  8512. (let (tags)
  8513. (save-excursion
  8514. (goto-char (point-min))
  8515. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  8516. (mapc (lambda (x) (add-to-list 'tags x))
  8517. (org-split-string (org-match-string-no-properties 1) ":"))))
  8518. (mapcar 'list tags)))
  8519. ;;; Link Stuff
  8520. (defvar org-create-file-search-functions nil
  8521. "List of functions to construct the right search string for a file link.
  8522. These functions are called in turn with point at the location to
  8523. which the link should point.
  8524. A function in the hook should first test if it would like to
  8525. handle this file type, for example by checking the major-mode or
  8526. the file extension. If it decides not to handle this file, it
  8527. should just return nil to give other functions a chance. If it
  8528. does handle the file, it must return the search string to be used
  8529. when following the link. The search string will be part of the
  8530. file link, given after a double colon, and `org-open-at-point'
  8531. will automatically search for it. If special measures must be
  8532. taken to make the search successful, another function should be
  8533. added to the companion hook `org-execute-file-search-functions',
  8534. which see.
  8535. A function in this hook may also use `setq' to set the variable
  8536. `description' to provide a suggestion for the descriptive text to
  8537. be used for this link when it gets inserted into an Org-mode
  8538. buffer with \\[org-insert-link].")
  8539. (defvar org-execute-file-search-functions nil
  8540. "List of functions to execute a file search triggered by a link.
  8541. Functions added to this hook must accept a single argument, the
  8542. search string that was part of the file link, the part after the
  8543. double colon. The function must first check if it would like to
  8544. handle this search, for example by checking the major-mode or the
  8545. file extension. If it decides not to handle this search, it
  8546. should just return nil to give other functions a chance. If it
  8547. does handle the search, it must return a non-nil value to keep
  8548. other functions from trying.
  8549. Each function can access the current prefix argument through the
  8550. variable `current-prefix-argument'. Note that a single prefix is
  8551. used to force opening a link in Emacs, so it may be good to only
  8552. use a numeric or double prefix to guide the search function.
  8553. In case this is needed, a function in this hook can also restore
  8554. the window configuration before `org-open-at-point' was called using:
  8555. (set-window-configuration org-window-config-before-follow-link)")
  8556. (defun org-find-file-at-mouse (ev)
  8557. "Open file link or URL at mouse."
  8558. (interactive "e")
  8559. (mouse-set-point ev)
  8560. (org-open-at-point 'in-emacs))
  8561. (defun org-open-at-mouse (ev)
  8562. "Open file link or URL at mouse."
  8563. (interactive "e")
  8564. (mouse-set-point ev)
  8565. (org-open-at-point))
  8566. (defvar org-window-config-before-follow-link nil
  8567. "The window configuration before following a link.
  8568. This is saved in case the need arises to restore it.")
  8569. (defun org-open-at-point (&optional in-emacs)
  8570. "Open link at or after point.
  8571. If there is no link at point, this function will search forward up to
  8572. the end of the current subtree.
  8573. Normally, files will be opened by an appropriate application. If the
  8574. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  8575. (interactive "P")
  8576. (setq org-window-config-before-follow-link (current-window-configuration))
  8577. (org-remove-occur-highlights nil nil t)
  8578. (if (org-at-timestamp-p)
  8579. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8580. (substring (match-string 1) 0 10)))
  8581. 1)
  8582. (let (type path link line search (pos (point)))
  8583. (catch 'match
  8584. (save-excursion
  8585. (skip-chars-forward "^]\n\r")
  8586. (when (and (re-search-backward "\\[\\[" nil t)
  8587. (looking-at org-bracket-link-regexp)
  8588. (<= (match-beginning 0) pos)
  8589. (>= (match-end 0) pos))
  8590. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  8591. (while (string-match " *\n *" link)
  8592. (setq link (replace-match " " t t link)))
  8593. (if (string-match org-link-re-with-space2 link)
  8594. (setq type (match-string 1 link)
  8595. path (match-string 2 link))
  8596. (setq type "thisfile"
  8597. path link))
  8598. (throw 'match t)))
  8599. (when (get-text-property (point) 'org-linked-text)
  8600. (setq type "thisfile"
  8601. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8602. (1+ (point)) (point))
  8603. path (buffer-substring
  8604. (previous-single-property-change pos 'org-linked-text)
  8605. (next-single-property-change pos 'org-linked-text)))
  8606. (throw 'match t))
  8607. (save-excursion
  8608. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  8609. (if (equal (char-before) ?<) (backward-char 1))
  8610. (when (or (looking-at org-angle-link-re)
  8611. (looking-at org-plain-link-re)
  8612. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  8613. (re-search-forward org-plain-link-re (point-at-eol) t))
  8614. (<= (match-beginning 0) pos)
  8615. (>= (match-end 0) pos)))
  8616. (setq type (match-string 1)
  8617. path (match-string 2))
  8618. (throw 'match t)))
  8619. (save-excursion
  8620. (skip-chars-backward "^ \t\n\r")
  8621. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  8622. (setq type "tags"
  8623. path (match-string 1))
  8624. (while (string-match ":" path)
  8625. (setq path (replace-match "+" t t path)))
  8626. (throw 'match t)))
  8627. (save-excursion
  8628. (skip-chars-backward "a-zA-Z_")
  8629. (when (and (memq 'camel org-activate-links)
  8630. (looking-at org-camel-regexp))
  8631. (setq type "camel" path (match-string 0))
  8632. (if (equal (char-before) ?*)
  8633. (setq path (concat "*" path))))
  8634. (throw 'match t)))
  8635. (unless path
  8636. (error "No link found"))
  8637. ;; Remove any trailing spaces in path
  8638. (if (string-match " +\\'" path)
  8639. (setq path (replace-match "" t t path)))
  8640. (cond
  8641. ((equal type "mailto")
  8642. (let ((cmd (car org-link-mailto-program))
  8643. (args (cdr org-link-mailto-program)) args1
  8644. (address path) (subject "") a)
  8645. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8646. (setq address (match-string 1 path)
  8647. subject (org-link-escape (match-string 2 path))))
  8648. (while args
  8649. (cond
  8650. ((not (stringp (car args))) (push (pop args) args1))
  8651. (t (setq a (pop args))
  8652. (if (string-match "%a" a)
  8653. (setq a (replace-match address t t a)))
  8654. (if (string-match "%s" a)
  8655. (setq a (replace-match subject t t a)))
  8656. (push a args1))))
  8657. (apply cmd (nreverse args1))))
  8658. ((member type '("http" "https" "ftp" "news"))
  8659. (browse-url (concat type ":" path)))
  8660. ((string= type "tags")
  8661. (org-tags-view in-emacs path))
  8662. ((or (string= type "camel")
  8663. (string= type "thisfile"))
  8664. (org-mark-ring-push)
  8665. (org-link-search
  8666. path
  8667. (cond ((equal in-emacs '(4)) 'occur)
  8668. ((equal in-emacs '(16)) 'org-occur)
  8669. (t nil))))
  8670. ((string= type "file")
  8671. (if (string-match "::\\([0-9]+\\)\\'" path)
  8672. (setq line (string-to-number (match-string 1 path))
  8673. path (substring path 0 (match-beginning 0)))
  8674. (if (string-match "::\\(.+\\)\\'" path)
  8675. (setq search (match-string 1 path)
  8676. path (substring path 0 (match-beginning 0)))))
  8677. (org-open-file path in-emacs line search))
  8678. ((string= type "news")
  8679. (org-follow-gnus-link path))
  8680. ((string= type "bbdb")
  8681. (org-follow-bbdb-link path))
  8682. ((string= type "info")
  8683. (org-follow-info-link path))
  8684. ((string= type "gnus")
  8685. (let (group article)
  8686. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8687. (error "Error in Gnus link"))
  8688. (setq group (match-string 1 path)
  8689. article (match-string 3 path))
  8690. (org-follow-gnus-link group article)))
  8691. ((string= type "vm")
  8692. (let (folder article)
  8693. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8694. (error "Error in VM link"))
  8695. (setq folder (match-string 1 path)
  8696. article (match-string 3 path))
  8697. ;; in-emacs is the prefix arg, will be interpreted as read-only
  8698. (org-follow-vm-link folder article in-emacs)))
  8699. ((string= type "wl")
  8700. (let (folder article)
  8701. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8702. (error "Error in Wanderlust link"))
  8703. (setq folder (match-string 1 path)
  8704. article (match-string 3 path))
  8705. (org-follow-wl-link folder article)))
  8706. ((string= type "mhe")
  8707. (let (folder article)
  8708. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8709. (error "Error in MHE link"))
  8710. (setq folder (match-string 1 path)
  8711. article (match-string 3 path))
  8712. (org-follow-mhe-link folder article)))
  8713. ((string= type "rmail")
  8714. (let (folder article)
  8715. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  8716. (error "Error in RMAIL link"))
  8717. (setq folder (match-string 1 path)
  8718. article (match-string 3 path))
  8719. (org-follow-rmail-link folder article)))
  8720. ((string= type "shell")
  8721. (let ((cmd path))
  8722. (while (string-match "@{" cmd)
  8723. (setq cmd (replace-match "<" t t cmd)))
  8724. (while (string-match "@}" cmd)
  8725. (setq cmd (replace-match ">" t t cmd)))
  8726. (if (or (not org-confirm-shell-link-function)
  8727. (funcall org-confirm-shell-link-function
  8728. (format "Execute \"%s\" in shell? "
  8729. (org-add-props cmd nil
  8730. 'face 'org-warning))))
  8731. (progn
  8732. (message "Executing %s" cmd)
  8733. (shell-command cmd))
  8734. (error "Abort"))))
  8735. ((string= type "elisp")
  8736. (let ((cmd path))
  8737. (if (or (not org-confirm-elisp-link-function)
  8738. (funcall org-confirm-elisp-link-function
  8739. (format "Execute \"%s\" as elisp? "
  8740. (org-add-props cmd nil
  8741. 'face 'org-warning))))
  8742. (message "%s => %s" cmd (eval (read cmd)))
  8743. (error "Abort"))))
  8744. (t
  8745. (browse-url-at-point))))))
  8746. (defun org-link-search (s &optional type)
  8747. "Search for a link search option.
  8748. When S is a CamelCaseWord, search for a target, or for a sentence containing
  8749. the words. If S is surrounded by forward slashes, it is interpreted as a
  8750. regular expression. In org-mode files, this will create an `org-occur'
  8751. sparse tree. In ordinary files, `occur' will be used to list matches.
  8752. If the current buffer is in `dired-mode', grep will be used to search
  8753. in all files."
  8754. (let ((case-fold-search t)
  8755. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8756. (pos (point))
  8757. (pre "") (post "")
  8758. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  8759. (cond
  8760. ;; First check if there are any special
  8761. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8762. ;; Now try the builtin stuff
  8763. ((save-excursion
  8764. (goto-char (point-min))
  8765. (and
  8766. (re-search-forward
  8767. (concat "<<" (regexp-quote s0) ">>") nil t)
  8768. (setq pos (match-beginning 0))))
  8769. ;; There is an exact target for this
  8770. (goto-char pos))
  8771. ((string-match "^/\\(.*\\)/$" s)
  8772. ;; A regular expression
  8773. (cond
  8774. ((org-mode-p)
  8775. (org-occur (match-string 1 s)))
  8776. ;;((eq major-mode 'dired-mode)
  8777. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8778. (t (org-do-occur (match-string 1 s)))))
  8779. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  8780. t)
  8781. ;; A camel or a normal search string
  8782. (when (equal (string-to-char s) ?*)
  8783. ;; Anchor on headlines, post may include tags.
  8784. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  8785. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  8786. s (substring s 1)))
  8787. (remove-text-properties
  8788. 0 (length s)
  8789. '(face nil mouse-face nil keymap nil fontified nil) s)
  8790. ;; Make a series of regular expressions to find a match
  8791. (setq words
  8792. (if camel
  8793. (org-camel-to-words s)
  8794. (org-split-string s "[ \n\r\t]+"))
  8795. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8796. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  8797. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8798. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8799. re1 (concat pre re2 post)
  8800. re3 (concat pre re4 post)
  8801. re5 (concat pre ".*" re4)
  8802. re2 (concat pre re2)
  8803. re2a (concat pre re2a)
  8804. re4 (concat pre re4)
  8805. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8806. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8807. re5 "\\)"
  8808. ))
  8809. (cond
  8810. ((eq type 'org-occur) (org-occur reall))
  8811. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8812. (t (goto-char (point-min))
  8813. (if (or (org-search-not-link re0 nil t)
  8814. (org-search-not-link re1 nil t)
  8815. (org-search-not-link re2 nil t)
  8816. (org-search-not-link re2a nil t)
  8817. (org-search-not-link re3 nil t)
  8818. (org-search-not-link re4 nil t)
  8819. (org-search-not-link re5 nil t)
  8820. )
  8821. (goto-char (match-beginning 1))
  8822. (goto-char pos)
  8823. (error "No match")))))
  8824. (t
  8825. ;; Normal string-search
  8826. (goto-char (point-min))
  8827. (if (search-forward s nil t)
  8828. (goto-char (match-beginning 0))
  8829. (error "No match"))))
  8830. (and (org-mode-p) (org-show-hierarchy-above))))
  8831. (defun org-search-not-link (&rest args)
  8832. "Execute `re-search-forward', but only accept matches that are not a link."
  8833. (catch 'exit
  8834. (let (p1)
  8835. (while (apply 're-search-forward args)
  8836. (setq p1 (point))
  8837. (if (not (save-match-data
  8838. (and (re-search-backward "\\[\\[" nil t)
  8839. (looking-at org-bracket-link-regexp)
  8840. (<= (match-beginning 0) p1)
  8841. (>= (match-end 0) p1))))
  8842. (progn (goto-char (match-end 0))
  8843. (throw 'exit (point)))
  8844. (goto-char (match-end 0)))))))
  8845. (defun org-do-occur (regexp &optional cleanup)
  8846. "Call the Emacs command `occur'.
  8847. If CLEANUP is non-nil, remove the printout of the regular expression
  8848. in the *Occur* buffer. This is useful if the regex is long and not useful
  8849. to read."
  8850. (occur regexp)
  8851. (when cleanup
  8852. (let ((cwin (selected-window)) win beg end)
  8853. (when (setq win (get-buffer-window "*Occur*"))
  8854. (select-window win))
  8855. (goto-char (point-min))
  8856. (when (re-search-forward "match[a-z]+" nil t)
  8857. (setq beg (match-end 0))
  8858. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8859. (setq end (1- (match-beginning 0)))))
  8860. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  8861. (goto-char (point-min))
  8862. (select-window cwin))))
  8863. (defvar org-mark-ring nil
  8864. "Mark ring for positions before jumps in Org-mode.")
  8865. (defvar org-mark-ring-last-goto nil
  8866. "Last position in the mark ring used to go back.")
  8867. ;; Fill and close the ring
  8868. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8869. (loop for i from 1 to org-mark-ring-length do
  8870. (push (make-marker) org-mark-ring))
  8871. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8872. org-mark-ring)
  8873. (defun org-mark-ring-push (&optional pos buffer)
  8874. "Put the current position or POS into the mark ring and rotate it."
  8875. (interactive)
  8876. (setq pos (or pos (point)))
  8877. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8878. (move-marker (car org-mark-ring)
  8879. (or pos (point))
  8880. (or buffer (current-buffer)))
  8881. (message
  8882. (substitute-command-keys
  8883. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8884. (defun org-mark-ring-goto (&optional n)
  8885. "Jump to the previous position in the mark ring.
  8886. With prefix arg N, jump back that many stored positions. When
  8887. called several times in succession, walk through the entire ring.
  8888. Org-mode commands jumping to a different position in the current file,
  8889. or to another Org-mode file, automatically push the old position
  8890. onto the ring."
  8891. (interactive "p")
  8892. (let (p m)
  8893. (if (eq last-command this-command)
  8894. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8895. (setq p org-mark-ring))
  8896. (setq org-mark-ring-last-goto p)
  8897. (setq m (car p))
  8898. (switch-to-buffer (marker-buffer m))
  8899. (goto-char m)
  8900. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  8901. (defun org-camel-to-words (s)
  8902. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  8903. (let ((case-fold-search nil)
  8904. words)
  8905. (while (string-match "[a-z][A-Z]" s)
  8906. (push (substring s 0 (1+ (match-beginning 0))) words)
  8907. (setq s (substring s (1+ (match-beginning 0)))))
  8908. (nreverse (cons s words))))
  8909. (defun org-remove-angle-brackets (s)
  8910. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8911. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8912. s)
  8913. (defun org-add-angle-brackets (s)
  8914. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8915. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8916. s)
  8917. (defun org-follow-bbdb-link (name)
  8918. "Follow a BBDB link to NAME."
  8919. (require 'bbdb)
  8920. (let ((inhibit-redisplay t)
  8921. (bbdb-electric-p nil))
  8922. (catch 'exit
  8923. ;; Exact match on name
  8924. (bbdb-name (concat "\\`" name "\\'") nil)
  8925. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8926. ;; Exact match on name
  8927. (bbdb-company (concat "\\`" name "\\'") nil)
  8928. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8929. ;; Partial match on name
  8930. (bbdb-name name nil)
  8931. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8932. ;; Partial match on company
  8933. (bbdb-company name nil)
  8934. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  8935. ;; General match including network address and notes
  8936. (bbdb name nil)
  8937. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  8938. (delete-window (get-buffer-window "*BBDB*"))
  8939. (error "No matching BBDB record")))))
  8940. (defun org-follow-info-link (name)
  8941. "Follow an info file & node link to NAME."
  8942. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  8943. (string-match "\\(.*\\)" name))
  8944. (progn
  8945. (require 'info)
  8946. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  8947. (Info-find-node (match-string 1 name) (match-string 2 name))
  8948. (Info-find-node (match-string 1 name) "Top")))
  8949. (message (concat "Could not open: " name))))
  8950. (defun org-follow-gnus-link (&optional group article)
  8951. "Follow a Gnus link to GROUP and ARTICLE."
  8952. (require 'gnus)
  8953. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  8954. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  8955. (if group (gnus-fetch-group group))
  8956. (if article
  8957. (or (gnus-summary-goto-article article nil 'force)
  8958. (if (fboundp 'gnus-summary-insert-cached-articles)
  8959. (progn
  8960. (gnus-summary-insert-cached-articles)
  8961. (gnus-summary-goto-article article nil 'force))
  8962. (message "Message could not be found.")))))
  8963. (defun org-follow-vm-link (&optional folder article readonly)
  8964. "Follow a VM link to FOLDER and ARTICLE."
  8965. (require 'vm)
  8966. (setq article (org-add-angle-brackets article))
  8967. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  8968. ;; ange-ftp or efs or tramp access
  8969. (let ((user (or (match-string 1 folder) (user-login-name)))
  8970. (host (match-string 2 folder))
  8971. (file (match-string 3 folder)))
  8972. (cond
  8973. ((featurep 'tramp)
  8974. ;; use tramp to access the file
  8975. (if (featurep 'xemacs)
  8976. (setq folder (format "[%s@%s]%s" user host file))
  8977. (setq folder (format "/%s@%s:%s" user host file))))
  8978. (t
  8979. ;; use ange-ftp or efs
  8980. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  8981. (setq folder (format "/%s@%s:%s" user host file))))))
  8982. (when folder
  8983. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  8984. (sit-for 0.1)
  8985. (when article
  8986. (vm-select-folder-buffer)
  8987. (widen)
  8988. (let ((case-fold-search t))
  8989. (goto-char (point-min))
  8990. (if (not (re-search-forward
  8991. (concat "^" "message-id: *" (regexp-quote article))))
  8992. (error "Could not find the specified message in this folder"))
  8993. (vm-isearch-update)
  8994. (vm-isearch-narrow)
  8995. (vm-beginning-of-message)
  8996. (vm-summarize)))))
  8997. (defun org-follow-wl-link (folder article)
  8998. "Follow a Wanderlust link to FOLDER and ARTICLE."
  8999. (setq article (org-add-angle-brackets article))
  9000. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  9001. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  9002. (wl-summary-redisplay))
  9003. (defun org-follow-rmail-link (folder article)
  9004. "Follow an RMAIL link to FOLDER and ARTICLE."
  9005. (setq article (org-add-angle-brackets article))
  9006. (let (message-number)
  9007. (save-excursion
  9008. (save-window-excursion
  9009. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9010. (setq message-number
  9011. (save-restriction
  9012. (widen)
  9013. (goto-char (point-max))
  9014. (if (re-search-backward
  9015. (concat "^Message-ID:\\s-+" (regexp-quote
  9016. (or article "")))
  9017. nil t)
  9018. (rmail-what-message))))))
  9019. (if message-number
  9020. (progn
  9021. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  9022. (rmail-show-message message-number)
  9023. message-number)
  9024. (error "Message not found"))))
  9025. ;; mh-e integration based on planner-mode
  9026. (defun org-mhe-get-message-real-folder ()
  9027. "Return the name of the current message real folder, so if you use
  9028. sequences, it will now work."
  9029. (save-excursion
  9030. (let* ((folder
  9031. (if (equal major-mode 'mh-folder-mode)
  9032. mh-current-folder
  9033. ;; Refer to the show buffer
  9034. mh-show-folder-buffer))
  9035. (end-index
  9036. (if (boundp 'mh-index-folder)
  9037. (min (length mh-index-folder) (length folder))))
  9038. )
  9039. ;; a simple test on mh-index-data does not work, because
  9040. ;; mh-index-data is always nil in a show buffer.
  9041. (if (and (boundp 'mh-index-folder)
  9042. (string= mh-index-folder (substring folder 0 end-index)))
  9043. (if (equal major-mode 'mh-show-mode)
  9044. (save-window-excursion
  9045. (when (buffer-live-p (get-buffer folder))
  9046. (progn
  9047. (pop-to-buffer folder)
  9048. (org-mhe-get-message-folder-from-index)
  9049. )
  9050. ))
  9051. (org-mhe-get-message-folder-from-index)
  9052. )
  9053. folder
  9054. )
  9055. )))
  9056. (defun org-mhe-get-message-folder-from-index ()
  9057. "Returns the name of the message folder in a index folder buffer."
  9058. (save-excursion
  9059. (mh-index-previous-folder)
  9060. (re-search-forward "^\\(+.*\\)$" nil t)
  9061. (message (match-string 1))))
  9062. (defun org-mhe-get-message-folder ()
  9063. "Return the name of the current message folder. Be careful if you
  9064. use sequences."
  9065. (save-excursion
  9066. (if (equal major-mode 'mh-folder-mode)
  9067. mh-current-folder
  9068. ;; Refer to the show buffer
  9069. mh-show-folder-buffer)))
  9070. (defun org-mhe-get-message-num ()
  9071. "Return the number of the current message. Be careful if you
  9072. use sequences."
  9073. (save-excursion
  9074. (if (equal major-mode 'mh-folder-mode)
  9075. (mh-get-msg-num nil)
  9076. ;; Refer to the show buffer
  9077. (mh-show-buffer-message-number))))
  9078. (defun org-mhe-get-header (header)
  9079. "Return a header of the message in folder mode. This will create a
  9080. show buffer for the corresponding message. If you have a more clever
  9081. idea..."
  9082. (let* ((folder (org-mhe-get-message-folder))
  9083. (num (org-mhe-get-message-num))
  9084. (buffer (get-buffer-create (concat "show-" folder)))
  9085. (header-field))
  9086. (with-current-buffer buffer
  9087. (mh-display-msg num folder)
  9088. (if (equal major-mode 'mh-folder-mode)
  9089. (mh-header-display)
  9090. (mh-show-header-display))
  9091. (set-buffer buffer)
  9092. (setq header-field (mh-get-header-field header))
  9093. (if (equal major-mode 'mh-folder-mode)
  9094. (mh-show)
  9095. (mh-show-show))
  9096. header-field)))
  9097. (defun org-follow-mhe-link (folder article)
  9098. "Follow an MHE link to FOLDER and ARTICLE.
  9099. If ARTICLE is nil FOLDER is shown. If the configuration variable
  9100. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  9101. ARTICLE is searched in all folders. Indexed searches (swish++,
  9102. namazu, and others supported by MH-E) will always search in all
  9103. folders."
  9104. (require 'mh-e)
  9105. (require 'mh-search)
  9106. (require 'mh-utils)
  9107. (mh-find-path)
  9108. (if (not article)
  9109. (mh-visit-folder (mh-normalize-folder-name folder))
  9110. (setq article (org-add-angle-brackets article))
  9111. (mh-search-choose)
  9112. (if (equal mh-searcher 'pick)
  9113. (progn
  9114. (mh-search folder (list "--message-id" article))
  9115. (when (and org-mhe-search-all-folders
  9116. (not (org-mhe-get-message-real-folder)))
  9117. (kill-this-buffer)
  9118. (mh-search "+" (list "--message-id" article))))
  9119. (mh-search "+" article))
  9120. (if (org-mhe-get-message-real-folder)
  9121. (mh-show-msg 1)
  9122. (kill-this-buffer)
  9123. (error "Message not found"))))
  9124. ;; BibTeX links
  9125. ;; Use the custom search meachnism to construct and use search strings for
  9126. ;; file links to BibTeX database entries.
  9127. (defun org-create-file-search-in-bibtex ()
  9128. "Create the search string and description for a BibTeX database entry."
  9129. (when (eq major-mode 'bibtex-mode)
  9130. ;; yes, we want to construct this search string.
  9131. ;; Make a good description for this entry, using names, year and the title
  9132. ;; Put it into the `description' variable which is dynamically scoped.
  9133. (let ((bibtex-autokey-names 1)
  9134. (bibtex-autokey-names-stretch 1)
  9135. (bibtex-autokey-name-case-convert-function 'identity)
  9136. (bibtex-autokey-name-separator " & ")
  9137. (bibtex-autokey-additional-names " et al.")
  9138. (bibtex-autokey-year-length 4)
  9139. (bibtex-autokey-name-year-separator " ")
  9140. (bibtex-autokey-titlewords 3)
  9141. (bibtex-autokey-titleword-separator " ")
  9142. (bibtex-autokey-titleword-case-convert-function 'identity)
  9143. (bibtex-autokey-titleword-length 'infty)
  9144. (bibtex-autokey-year-title-separator ": "))
  9145. (setq description (bibtex-generate-autokey)))
  9146. ;; Now parse the entry, get the key and return it.
  9147. (save-excursion
  9148. (bibtex-beginning-of-entry)
  9149. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  9150. (defun org-execute-file-search-in-bibtex (s)
  9151. "Find the link search string S as a key for a database entry."
  9152. (when (eq major-mode 'bibtex-mode)
  9153. ;; Yes, we want to do the search in this file.
  9154. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  9155. (goto-char (point-min))
  9156. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  9157. (regexp-quote s) "[ \t\n]*,") nil t)
  9158. (goto-char (match-beginning 0)))
  9159. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  9160. ;; Use double prefix to indicate that any web link should be browsed
  9161. (let ((b (current-buffer)) (p (point)))
  9162. ;; Restore the window configuration because we just use the web link
  9163. (set-window-configuration org-window-config-before-follow-link)
  9164. (save-excursion (set-buffer b) (goto-char p)
  9165. (bibtex-url)))
  9166. (recenter 0)) ; Move entry start to beginning of window
  9167. ;; return t to indicate that the search is done.
  9168. t))
  9169. ;; Finally add the functions to the right hooks.
  9170. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  9171. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  9172. ;; end of Bibtex link setup
  9173. (defun org-upgrade-old-links (&optional query-description)
  9174. "Transfer old <...> style links to new [[...]] style links.
  9175. With arg query-description, ask at each match for a description text to use
  9176. for this link."
  9177. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  9178. (save-excursion
  9179. (goto-char (point-min))
  9180. (let ((re (concat "\\([^[]\\)<\\("
  9181. "\\(" (mapconcat 'identity org-link-types "\\|")
  9182. "\\):"
  9183. "[^" org-non-link-chars "]+\\)>"))
  9184. l1 l2 (cnt 0))
  9185. (while (re-search-forward re nil t)
  9186. (setq cnt (1+ cnt)
  9187. l1 (org-match-string-no-properties 2)
  9188. l2 (save-match-data (org-link-escape l1)))
  9189. (when query-description (setq l1 (read-string "Desc: " l1)))
  9190. (if (equal l1 l2)
  9191. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  9192. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  9193. (message "%d matches have beed treated" cnt))))
  9194. (defun org-open-file (path &optional in-emacs line search)
  9195. "Open the file at PATH.
  9196. First, this expands any special file name abbreviations. Then the
  9197. configuration variable `org-file-apps' is checked if it contains an
  9198. entry for this file type, and if yes, the corresponding command is launched.
  9199. If no application is found, Emacs simply visits the file.
  9200. With optional argument IN-EMACS, Emacs will visit the file.
  9201. Optional LINE specifies a line to go to, optional SEARCH a string to
  9202. search for. If LINE or SEARCH is given, the file will always be
  9203. opened in Emacs.
  9204. If the file does not exist, an error is thrown."
  9205. (setq in-emacs (or in-emacs line search))
  9206. (let* ((file (if (equal path "")
  9207. buffer-file-name
  9208. path))
  9209. (apps (append org-file-apps (org-default-apps)))
  9210. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9211. (dirp (if remp nil (file-directory-p file)))
  9212. (dfile (downcase file))
  9213. (old-buffer (current-buffer))
  9214. (old-pos (point))
  9215. (old-mode major-mode)
  9216. ext cmd)
  9217. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9218. (setq ext (match-string 1 dfile))
  9219. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9220. (setq ext (match-string 1 dfile))))
  9221. (if in-emacs
  9222. (setq cmd 'emacs)
  9223. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9224. (and dirp (cdr (assoc 'directory apps)))
  9225. (cdr (assoc ext apps))
  9226. (cdr (assoc t apps)))))
  9227. (when (eq cmd 'mailcap)
  9228. (require 'mailcap)
  9229. (mailcap-parse-mailcaps)
  9230. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9231. (command (mailcap-mime-info mime-type)))
  9232. (if (stringp command)
  9233. (setq cmd command)
  9234. (setq cmd 'emacs))))
  9235. (if (and (not (eq cmd 'emacs)) ; Emacs has not problems with non-ex files
  9236. (not (file-exists-p file))
  9237. (not org-open-non-existing-files))
  9238. (error "No such file: %s" file))
  9239. (cond
  9240. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9241. ;; Normalize use of quote, this can vary.
  9242. (if (string-match "['\"]%s['\"]" cmd)
  9243. (setq cmd (replace-match "'%s'" t t cmd)))
  9244. (setq cmd (format cmd file))
  9245. (save-window-excursion
  9246. (shell-command (concat cmd " &"))))
  9247. ((or (stringp cmd)
  9248. (eq cmd 'emacs))
  9249. ; (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  9250. ; (funcall (cdr (assq 'file org-link-frame-setup)) file))
  9251. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9252. (if line (goto-line line)
  9253. (if search (org-link-search search))))
  9254. ((consp cmd)
  9255. (eval cmd))
  9256. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9257. (and (org-mode-p) (eq old-mode 'org-mode)
  9258. (or (not (equal old-buffer (current-buffer)))
  9259. (not (equal old-pos (point))))
  9260. (org-mark-ring-push old-pos old-buffer))))
  9261. (defun org-default-apps ()
  9262. "Return the default applications for this operating system."
  9263. (cond
  9264. ((eq system-type 'darwin)
  9265. org-file-apps-defaults-macosx)
  9266. ((eq system-type 'windows-nt)
  9267. org-file-apps-defaults-windowsnt)
  9268. (t org-file-apps-defaults-gnu)))
  9269. (defun org-expand-file-name (path)
  9270. "Replace special path abbreviations and expand the file name."
  9271. (expand-file-name path))
  9272. (defun org-file-remote-p (file)
  9273. "Test whether FILE specifies a location on a remote system.
  9274. Return non-nil if the location is indeed remote.
  9275. For example, the filename \"/user@host:/foo\" specifies a location
  9276. on the system \"/user@host:\"."
  9277. (cond ((fboundp 'file-remote-p)
  9278. (file-remote-p file))
  9279. ((fboundp 'tramp-handle-file-remote-p)
  9280. (tramp-handle-file-remote-p file))
  9281. ((and (boundp 'ange-ftp-name-format)
  9282. (string-match ange-ftp-name-format file))
  9283. t)
  9284. (t nil)))
  9285. (defvar org-insert-link-history nil
  9286. "Minibuffer history for links inserted with `org-insert-link'.")
  9287. (defvar org-stored-links nil
  9288. "Contains the links stored with `org-store-link'.")
  9289. ;;;###autoload
  9290. (defun org-store-link (arg)
  9291. "\\<org-mode-map>Store an org-link to the current location.
  9292. This link can later be inserted into an org-buffer with
  9293. \\[org-insert-link].
  9294. For some link types, a prefix arg is interpreted:
  9295. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  9296. For file links, arg negates `org-context-in-file-links'."
  9297. (interactive "P")
  9298. (let (link cpltxt desc description search txt (pos (point)))
  9299. (cond
  9300. ((eq major-mode 'bbdb-mode)
  9301. (setq cpltxt (concat
  9302. "bbdb:"
  9303. (or (bbdb-record-name (bbdb-current-record))
  9304. (bbdb-record-company (bbdb-current-record))))
  9305. link (org-make-link cpltxt)))
  9306. ((eq major-mode 'Info-mode)
  9307. (setq link (org-make-link "info:"
  9308. (file-name-nondirectory Info-current-file)
  9309. ":" Info-current-node))
  9310. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  9311. ":" Info-current-node)))
  9312. ((eq major-mode 'calendar-mode)
  9313. (let ((cd (calendar-cursor-to-date)))
  9314. (setq link
  9315. (format-time-string
  9316. (car org-time-stamp-formats)
  9317. (apply 'encode-time
  9318. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  9319. nil nil nil))))))
  9320. ((or (eq major-mode 'vm-summary-mode)
  9321. (eq major-mode 'vm-presentation-mode))
  9322. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  9323. (vm-follow-summary-cursor)
  9324. (save-excursion
  9325. (vm-select-folder-buffer)
  9326. (let* ((message (car vm-message-pointer))
  9327. (folder buffer-file-name)
  9328. (subject (vm-su-subject message))
  9329. (author (vm-su-full-name message))
  9330. (message-id (vm-su-message-id message)))
  9331. (setq message-id (org-remove-angle-brackets message-id))
  9332. (setq folder (abbreviate-file-name folder))
  9333. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  9334. folder)
  9335. (setq folder (replace-match "" t t folder)))
  9336. (setq cpltxt (concat author " on: " subject))
  9337. (setq link (org-make-link "vm:" folder "#" message-id)))))
  9338. ((eq major-mode 'wl-summary-mode)
  9339. (let* ((msgnum (wl-summary-message-number))
  9340. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  9341. msgnum 'message-id))
  9342. (wl-message-entity (elmo-msgdb-overview-get-entity
  9343. msgnum (wl-summary-buffer-msgdb)))
  9344. (author (wl-summary-line-from)) ; FIXME: correct?
  9345. (subject "???")) ; FIXME:
  9346. (setq message-id (org-remove-angle-brackets message-id))
  9347. (setq cpltxt (concat author " on: " subject))
  9348. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  9349. "#" message-id))))
  9350. ((or (equal major-mode 'mh-folder-mode)
  9351. (equal major-mode 'mh-show-mode))
  9352. (let ((from-header (org-mhe-get-header "From:"))
  9353. (to-header (org-mhe-get-header "To:"))
  9354. (subject (org-mhe-get-header "Subject:")))
  9355. (setq cpltxt (concat from-header " on: " subject))
  9356. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  9357. (org-remove-angle-brackets
  9358. (org-mhe-get-header "Message-Id:"))))))
  9359. ((eq major-mode 'rmail-mode)
  9360. (save-excursion
  9361. (save-restriction
  9362. (rmail-narrow-to-non-pruned-header)
  9363. (let ((folder buffer-file-name)
  9364. (message-id (mail-fetch-field "message-id"))
  9365. (author (mail-fetch-field "from"))
  9366. (subject (mail-fetch-field "subject")))
  9367. (setq message-id (org-remove-angle-brackets message-id))
  9368. (setq cpltxt (concat author " on: " subject))
  9369. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  9370. ((eq major-mode 'gnus-group-mode)
  9371. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  9372. (gnus-group-group-name)) ; version
  9373. ((fboundp 'gnus-group-name)
  9374. (gnus-group-name))
  9375. (t "???"))))
  9376. (setq cpltxt (concat
  9377. (if (org-xor arg org-usenet-links-prefer-google)
  9378. "http://groups.google.com/groups?group="
  9379. "gnus:")
  9380. group)
  9381. link (org-make-link cpltxt))))
  9382. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  9383. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  9384. (gnus-summary-beginning-of-article)
  9385. (let* ((group (car gnus-article-current))
  9386. (article (cdr gnus-article-current))
  9387. (header (gnus-summary-article-header article))
  9388. (author (mail-header-from header))
  9389. (message-id (mail-header-id header))
  9390. (date (mail-header-date header))
  9391. (subject (gnus-summary-subject-string)))
  9392. (setq cpltxt (concat author " on: " subject))
  9393. (if (org-xor arg org-usenet-links-prefer-google)
  9394. (setq link
  9395. (concat
  9396. cpltxt "\n "
  9397. (format "http://groups.google.com/groups?as_umsgid=%s"
  9398. (org-fixup-message-id-for-http message-id))))
  9399. (setq link (org-make-link "gnus:" group
  9400. "#" (number-to-string article))))))
  9401. ((eq major-mode 'w3-mode)
  9402. (setq cpltxt (url-view-url t)
  9403. link (org-make-link cpltxt)))
  9404. ((eq major-mode 'w3m-mode)
  9405. (setq cpltxt (or w3m-current-title w3m-current-url)
  9406. link (org-make-link w3m-current-url)))
  9407. ((setq search (run-hook-with-args-until-success
  9408. 'org-create-file-search-functions))
  9409. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  9410. "::" search))
  9411. (setq cpltxt (or description link)))
  9412. ((eq major-mode 'image-mode)
  9413. (setq cpltxt (concat "file:"
  9414. (abbreviate-file-name buffer-file-name))
  9415. link (org-make-link cpltxt)))
  9416. ((org-mode-p)
  9417. ;; Just link to current headline
  9418. (setq cpltxt (concat "file:"
  9419. (abbreviate-file-name buffer-file-name)))
  9420. ;; Add a context search string
  9421. (when (org-xor org-context-in-file-links arg)
  9422. ;; Check if we are on a target
  9423. (if (save-excursion
  9424. (skip-chars-forward "^>\n\r")
  9425. (and (re-search-backward "<<" nil t)
  9426. (looking-at "<<\\(.*?\\)>>")
  9427. (<= (match-beginning 0) pos)
  9428. (>= (match-end 0) pos)))
  9429. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  9430. (setq txt (cond
  9431. ((org-on-heading-p) nil)
  9432. ((org-region-active-p)
  9433. (buffer-substring (region-beginning) (region-end)))
  9434. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  9435. (when (or (null txt) (string-match "\\S-" txt))
  9436. (setq cpltxt
  9437. (concat cpltxt "::"
  9438. (if org-file-link-context-use-camel-case
  9439. (org-make-org-heading-camel txt)
  9440. (org-make-org-heading-search-string txt)))
  9441. desc "NONE"))))
  9442. (if (string-match "::\\'" cpltxt)
  9443. (setq cpltxt (substring cpltxt 0 -2)))
  9444. (setq link (org-make-link cpltxt)))
  9445. (buffer-file-name
  9446. ;; Just link to this file here.
  9447. (setq cpltxt (concat "file:"
  9448. (abbreviate-file-name buffer-file-name)))
  9449. ;; Add a context string
  9450. (when (org-xor org-context-in-file-links arg)
  9451. (setq txt (if (org-region-active-p)
  9452. (buffer-substring (region-beginning) (region-end))
  9453. (buffer-substring (point-at-bol) (point-at-eol))))
  9454. ;; Only use search option if there is some text.
  9455. (when (string-match "\\S-" txt)
  9456. (setq cpltxt
  9457. (concat cpltxt "::"
  9458. (if org-file-link-context-use-camel-case
  9459. (org-make-org-heading-camel txt)
  9460. (org-make-org-heading-search-string txt)))
  9461. desc "NONE")))
  9462. (setq link (org-make-link cpltxt)))
  9463. ((interactive-p)
  9464. (error "Cannot link to a buffer which is not visiting a file"))
  9465. (t (setq link nil)))
  9466. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  9467. (setq link (or link cpltxt)
  9468. desc (or desc cpltxt))
  9469. (if (equal desc "NONE") (setq desc nil))
  9470. (if (and (interactive-p) link)
  9471. (progn
  9472. (setq org-stored-links
  9473. (cons (list cpltxt link desc) org-stored-links))
  9474. (message "Stored: %s" (or cpltxt link)))
  9475. (org-make-link-string link desc))))
  9476. (defun org-make-org-heading-search-string (&optional string heading)
  9477. "Make search string for STRING or current headline."
  9478. (interactive)
  9479. (let ((s (or string (org-get-heading))))
  9480. (unless (and string (not heading))
  9481. ;; We are using a headline, clean up garbage in there.
  9482. (if (string-match org-todo-regexp s)
  9483. (setq s (replace-match "" t t s)))
  9484. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9485. (setq s (replace-match "" t t s)))
  9486. (setq s (org-trim s))
  9487. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9488. org-comment-string "\\)") s)
  9489. (setq s (replace-match "" t t s)))
  9490. (while (string-match org-ts-regexp s)
  9491. (setq s (replace-match "" t t s))))
  9492. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  9493. (setq s (replace-match " " t t s)))
  9494. (or string (setq s (concat "*" s))) ; Add * for headlines
  9495. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9496. (defun org-make-org-heading-camel (&optional string heading)
  9497. "Make a CamelCase string for STRING or the current headline."
  9498. (interactive)
  9499. (let ((s (or string (org-get-heading))))
  9500. (unless (and string (not heading))
  9501. ;; We are using a headline, clean up garbage in there.
  9502. (if (string-match org-todo-regexp s)
  9503. (setq s (replace-match "" t t s)))
  9504. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  9505. (setq s (replace-match "" t t s)))
  9506. (setq s (org-trim s))
  9507. (if (string-match (concat "^\\(" org-quote-string "\\|"
  9508. org-comment-string "\\)") s)
  9509. (setq s (replace-match "" t t s)))
  9510. (while (string-match org-ts-regexp s)
  9511. (setq s (replace-match "" t t s))))
  9512. (while (string-match "[^a-zA-Z_ \t]+" s)
  9513. (setq s (replace-match " " t t s)))
  9514. (or string (setq s (concat "*" s))) ; Add * for headlines
  9515. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  9516. (defun org-make-link (&rest strings)
  9517. "Concatenate STRINGS, format resulting string with `org-link-format'."
  9518. (format org-link-format (apply 'concat strings)))
  9519. (defun org-make-link-string (link &optional description)
  9520. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9521. (if (eq org-link-style 'plain)
  9522. (if (equal description link)
  9523. link
  9524. (concat description "\n" link))
  9525. (when (stringp description)
  9526. ;; Remove brackets from the description, they are fatal.
  9527. (while (string-match "\\[\\|\\]" description)
  9528. (setq description (replace-match "" t t description))))
  9529. (when (equal (org-link-escape link) description)
  9530. ;; No description needed, it is identical
  9531. (setq description nil))
  9532. (when (and (not description)
  9533. (not (equal link (org-link-escape link))))
  9534. (setq description link))
  9535. (concat "[[" (org-link-escape link) "]"
  9536. (if description (concat "[" description "]") "")
  9537. "]")))
  9538. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  9539. "Association list of escapes for some characters problematic in links.")
  9540. (defun org-link-escape (text)
  9541. "Escape charaters in TEXT that are problematic for links."
  9542. (when text
  9543. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  9544. org-link-escape-chars "\\|")))
  9545. (while (string-match re text)
  9546. (setq text
  9547. (replace-match
  9548. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  9549. t t text)))
  9550. text)))
  9551. (defun org-link-unescape (text)
  9552. "Reverse the action of `org-link-escape'."
  9553. (when text
  9554. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  9555. org-link-escape-chars "\\|")))
  9556. (while (string-match re text)
  9557. (setq text
  9558. (replace-match
  9559. (car (rassoc (match-string 0 text) org-link-escape-chars))
  9560. t t text)))
  9561. text)))
  9562. (defun org-xor (a b)
  9563. "Exclusive or."
  9564. (if a (not b) b))
  9565. (defun org-get-header (header)
  9566. "Find a header field in the current buffer."
  9567. (save-excursion
  9568. (goto-char (point-min))
  9569. (let ((case-fold-search t) s)
  9570. (cond
  9571. ((eq header 'from)
  9572. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  9573. (setq s (match-string 1)))
  9574. (while (string-match "\"" s)
  9575. (setq s (replace-match "" t t s)))
  9576. (if (string-match "[<(].*" s)
  9577. (setq s (replace-match "" t t s))))
  9578. ((eq header 'message-id)
  9579. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  9580. (setq s (match-string 1))))
  9581. ((eq header 'subject)
  9582. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  9583. (setq s (match-string 1)))))
  9584. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  9585. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  9586. s)))
  9587. (defun org-fixup-message-id-for-http (s)
  9588. "Replace special characters in a message id, so it can be used in an http query."
  9589. (while (string-match "<" s)
  9590. (setq s (replace-match "%3C" t t s)))
  9591. (while (string-match ">" s)
  9592. (setq s (replace-match "%3E" t t s)))
  9593. (while (string-match "@" s)
  9594. (setq s (replace-match "%40" t t s)))
  9595. s)
  9596. (defun org-insert-link (&optional complete-file)
  9597. "Insert a link. At the prompt, enter the link.
  9598. Completion can be used to select a link previously stored with
  9599. `org-store-link'. When the empty string is entered (i.e. if you just
  9600. press RET at the prompt), the link defaults to the most recently
  9601. stored link. As SPC triggers completion in the minibuffer, you need to
  9602. use M-SPC or C-q SPC to force the insertion of a space character.
  9603. You will also be prompted for a description, and if one is given, it will
  9604. be displayed in the buffer instead of the link.
  9605. If there is already a link at point, this command will allow you to edit link
  9606. and description parts.
  9607. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  9608. selected using completion. The path to the file will be relative to
  9609. the current directory if the file is in the current directory or a
  9610. subdirectory. Otherwise, the link will be the absolute path as
  9611. completed in the minibuffer (i.e. normally ~/path/to/file).
  9612. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  9613. is in the current directory or below.
  9614. With three \\[universal-argument] prefixes, negate the meaning of
  9615. `org-keep-stored-link-after-insertion'."
  9616. (interactive "P")
  9617. (let (link desc entry remove file (pos (point)))
  9618. (cond
  9619. ((save-excursion
  9620. (skip-chars-forward "^]\n\r")
  9621. (and (re-search-backward "\\[\\[" nil t)
  9622. (looking-at org-bracket-link-regexp)
  9623. (<= (match-beginning 0) pos)
  9624. (>= (match-end 0) pos)))
  9625. ;; We do have a link at point, and we are going to edit it.
  9626. (setq remove (list (match-beginning 0) (match-end 0)))
  9627. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9628. (setq link (read-string "Link: "
  9629. (org-link-unescape
  9630. (org-match-string-no-properties 1)))))
  9631. ((equal complete-file '(4))
  9632. ;; Completing read for file names.
  9633. (setq file (read-file-name "File: "))
  9634. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9635. (pwd1 (file-name-as-directory (abbreviate-file-name
  9636. (expand-file-name ".")))))
  9637. (cond
  9638. ((equal complete-file '(16))
  9639. (setq link (org-make-link
  9640. "file:"
  9641. (abbreviate-file-name (expand-file-name file)))))
  9642. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9643. (setq link (org-make-link "file:" (match-string 1 file))))
  9644. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9645. (expand-file-name file))
  9646. (setq link (org-make-link
  9647. "file:" (match-string 1 (expand-file-name file)))))
  9648. (t (setq link (org-make-link "file:" file))))))
  9649. (t
  9650. ;; Read link, with completion for stored links.
  9651. (setq link (org-completing-read
  9652. "Link: " org-stored-links nil nil nil
  9653. org-insert-link-history
  9654. (or (car (car org-stored-links)))))
  9655. (setq entry (assoc link org-stored-links))
  9656. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9657. (not org-keep-stored-link-after-insertion))
  9658. (setq org-stored-links (delq (assoc link org-stored-links)
  9659. org-stored-links)))
  9660. (setq link (if entry (nth 1 entry) link)
  9661. desc (or desc (nth 2 entry)))))
  9662. (if (string-match org-plain-link-re link)
  9663. ;; URL-like link, normalize the use of angular brackets.
  9664. (setq link (org-make-link (org-remove-angle-brackets link))))
  9665. ;; Check if we are linking to the current file with a search option
  9666. ;; If yes, simplify the link by using only the search option.
  9667. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  9668. (let* ((path (match-string 1 link))
  9669. (case-fold-search nil)
  9670. (search (match-string 2 link)))
  9671. (save-match-data
  9672. (if (equal (file-truename buffer-file-name) (file-truename path))
  9673. ;; We are linking to this same file, with a search option
  9674. (setq link search)))))
  9675. ;; Check if we can/should use a relative path. If yes, simplify the link
  9676. (when (string-match "\\<file:\\(.*\\)" link)
  9677. (let* ((path (match-string 1 link))
  9678. (case-fold-search nil))
  9679. (cond
  9680. ((eq org-link-file-path-type 'absolute)
  9681. (setq path (abbreviate-file-name (expand-file-name path))))
  9682. ((eq org-link-file-path-type 'noabbrev)
  9683. (setq path (expand-file-name path)))
  9684. ((eq org-link-file-path-type 'relative)
  9685. (setq path (file-relative-name path)))
  9686. (t
  9687. (save-match-data
  9688. (if (string-match (concat "^" (regexp-quote
  9689. (file-name-as-directory
  9690. (expand-file-name "."))))
  9691. (expand-file-name path))
  9692. ;; We are linking a file with relative path name.
  9693. (setq path (substring (expand-file-name path)
  9694. (match-end 0)))))))
  9695. (setq link (concat "file:" path))))
  9696. (setq desc (read-string "Description: " desc))
  9697. (unless (string-match "\\S-" desc) (setq desc nil))
  9698. (if remove (apply 'delete-region remove))
  9699. (insert (org-make-link-string link desc))))
  9700. (defun org-completing-read (&rest args)
  9701. (let ((minibuffer-local-completion-map
  9702. (copy-keymap minibuffer-local-completion-map)))
  9703. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  9704. (apply 'completing-read args)))
  9705. ;;; Hooks for remember.el
  9706. (defvar org-finish-function nil)
  9707. ;;;###autoload
  9708. (defun org-remember-annotation ()
  9709. "Return a link to the current location as an annotation for remember.el.
  9710. If you are using Org-mode files as target for data storage with
  9711. remember.el, then the annotations should include a link compatible with the
  9712. conventions in Org-mode. This function returns such a link."
  9713. (org-store-link nil))
  9714. (defconst org-remember-help
  9715. "Select a destination location for the note.
  9716. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  9717. RET at beg-of-buf -> Append to file as level 2 headline
  9718. RET on headline -> Store as sublevel entry to current headline
  9719. <left>/<right> -> before/after current headline, same headings level")
  9720. ;;;###autoload
  9721. (defun org-remember-apply-template ()
  9722. "Initialize *remember* buffer with template, invoke `org-mode'.
  9723. This function should be placed into `remember-mode-hook' and in fact requires
  9724. to be run from that hook to fucntion properly."
  9725. (if org-remember-templates
  9726. (let* ((entry (if (= (length org-remember-templates) 1)
  9727. (cdar org-remember-templates)
  9728. (message "Select template: %s"
  9729. (mapconcat
  9730. (lambda (x) (char-to-string (car x)))
  9731. org-remember-templates " "))
  9732. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  9733. (tpl (car entry))
  9734. (file (if (consp (cdr entry)) (nth 1 entry)))
  9735. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  9736. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  9737. (v-u (concat "[" (substring v-t 1 -1) "]"))
  9738. (v-U (concat "[" (substring v-T 1 -1) "]"))
  9739. (v-a annotation) ; defined in `remember-mode'
  9740. (v-i initial) ; defined in `remember-mode'
  9741. (v-n user-full-name)
  9742. )
  9743. (unless tpl (setq tpl "") (message "No template") (ding))
  9744. (insert tpl) (goto-char (point-min))
  9745. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  9746. (when (and initial (equal (match-string 0) "%i"))
  9747. (save-match-data
  9748. (let* ((lead (buffer-substring
  9749. (point-at-bol) (match-beginning 0))))
  9750. (setq v-i (mapconcat 'identity
  9751. (org-split-string initial "\n")
  9752. (concat "\n" lead))))))
  9753. (replace-match
  9754. (or (eval (intern (concat "v-" (match-string 1)))) "")
  9755. t t))
  9756. (let ((org-startup-folded nil)
  9757. (org-startup-with-deadline-check nil))
  9758. (org-mode))
  9759. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  9760. (org-set-local 'org-default-notes-file file))
  9761. (goto-char (point-min))
  9762. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  9763. (let ((org-startup-folded nil)
  9764. (org-startup-with-deadline-check nil))
  9765. (org-mode)))
  9766. (org-set-local 'org-finish-function 'remember-buffer))
  9767. ;;;###autoload
  9768. (defun org-remember-handler ()
  9769. "Store stuff from remember.el into an org file.
  9770. First prompts for an org file. If the user just presses return, the value
  9771. of `org-default-notes-file' is used.
  9772. Then the command offers the headings tree of the selected file in order to
  9773. file the text at a specific location.
  9774. You can either immediately press RET to get the note appended to the
  9775. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  9776. find a better place. Then press RET or <left> or <right> in insert the note.
  9777. Key Cursor position Note gets inserted
  9778. -----------------------------------------------------------------------------
  9779. RET buffer-start as level 2 heading at end of file
  9780. RET on headline as sublevel of the heading at cursor
  9781. RET no heading at cursor position, level taken from context.
  9782. Or use prefix arg to specify level manually.
  9783. <left> on headline as same level, before current heading
  9784. <right> on headline as same level, after current heading
  9785. So the fastest way to store the note is to press RET RET to append it to
  9786. the default file. This way your current train of thought is not
  9787. interrupted, in accordance with the principles of remember.el. But with
  9788. little extra effort, you can push it directly to the correct location.
  9789. Before being stored away, the function ensures that the text has a
  9790. headline, i.e. a first line that starts with a \"*\". If not, a headline
  9791. is constructed from the current date and some additional data.
  9792. If the variable `org-adapt-indentation' is non-nil, the entire text is
  9793. also indented so that it starts in the same column as the headline
  9794. \(i.e. after the stars).
  9795. See also the variable `org-reverse-note-order'."
  9796. (catch 'quit
  9797. (let* ((txt (buffer-substring (point-min) (point-max)))
  9798. (fastp current-prefix-arg)
  9799. (file (if fastp org-default-notes-file (org-get-org-file)))
  9800. (visiting (find-buffer-visiting file))
  9801. (org-startup-with-deadline-check nil)
  9802. (org-startup-folded nil)
  9803. (org-startup-align-all-tables nil)
  9804. spos level indent reversed)
  9805. ;; Modify text so that it becomes a nice subtree which can be inserted
  9806. ;; into an org tree.
  9807. (let* ((lines (split-string txt "\n"))
  9808. first)
  9809. ;; remove empty lines at the beginning
  9810. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  9811. (setq lines (cdr lines)))
  9812. (setq first (car lines) lines (cdr lines))
  9813. (if (string-match "^\\*+" first)
  9814. ;; Is already a headline
  9815. (setq indent nil)
  9816. ;; We need to add a headline: Use time and first buffer line
  9817. (setq lines (cons first lines)
  9818. first (concat "* " (current-time-string)
  9819. " (" (remember-buffer-desc) ")")
  9820. indent " "))
  9821. (if (and org-adapt-indentation indent)
  9822. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  9823. (setq txt (concat first "\n"
  9824. (mapconcat 'identity lines "\n"))))
  9825. ;; Find the file
  9826. (if (not visiting)
  9827. (find-file-noselect file))
  9828. (with-current-buffer (get-file-buffer file)
  9829. (save-excursion (and (goto-char (point-min))
  9830. (not (re-search-forward "^\\* " nil t))
  9831. (insert "\n* Notes\n")))
  9832. (setq reversed (org-notes-order-reversed-p))
  9833. (save-excursion
  9834. (save-restriction
  9835. (widen)
  9836. ;; Ask the User for a location
  9837. (setq spos (if fastp 1 (org-get-location
  9838. (current-buffer)
  9839. org-remember-help)))
  9840. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  9841. ; not handle this note
  9842. (goto-char spos)
  9843. (cond ((bobp)
  9844. ;; Put it at the start or end, as level 2
  9845. (save-restriction
  9846. (widen)
  9847. (goto-char (if reversed (point-min) (point-max)))
  9848. (if (not (bolp)) (newline))
  9849. (org-paste-subtree 2 txt)))
  9850. ((and (org-on-heading-p nil) (not current-prefix-arg))
  9851. ;; Put it below this entry, at the beg/end of the subtree
  9852. (org-back-to-heading)
  9853. (setq level (funcall outline-level))
  9854. (if reversed
  9855. (outline-end-of-heading)
  9856. (outline-end-of-subtree))
  9857. (if (not (bolp)) (newline))
  9858. (beginning-of-line 1)
  9859. (org-paste-subtree (1+ level) txt))
  9860. (t
  9861. ;; Put it right there, with automatic level determined by
  9862. ;; org-paste-subtree or from prefix arg
  9863. (org-paste-subtree current-prefix-arg txt)))
  9864. (when remember-save-after-remembering
  9865. (save-buffer)
  9866. (if (not visiting) (kill-buffer (current-buffer)))))))))
  9867. t) ;; return t to indicate that we took care of this note.
  9868. (defun org-get-org-file ()
  9869. "Read a filename, with default directory `org-directory'."
  9870. (let ((default (or org-default-notes-file remember-data-file)))
  9871. (read-file-name (format "File name [%s]: " default)
  9872. (file-name-as-directory org-directory)
  9873. default)))
  9874. (defun org-notes-order-reversed-p ()
  9875. "Check if the current file should receive notes in reversed order."
  9876. (cond
  9877. ((not org-reverse-note-order) nil)
  9878. ((eq t org-reverse-note-order) t)
  9879. ((not (listp org-reverse-note-order)) nil)
  9880. (t (catch 'exit
  9881. (let ((all org-reverse-note-order)
  9882. entry)
  9883. (while (setq entry (pop all))
  9884. (if (string-match (car entry) buffer-file-name)
  9885. (throw 'exit (cdr entry))))
  9886. nil)))))
  9887. ;;; Tables
  9888. ;; Watch out: Here we are talking about two different kind of tables.
  9889. ;; Most of the code is for the tables created with the Org-mode table editor.
  9890. ;; Sometimes, we talk about tables created and edited with the table.el
  9891. ;; Emacs package. We call the former org-type tables, and the latter
  9892. ;; table.el-type tables.
  9893. (defun org-before-change-function (beg end)
  9894. "Every change indicates that a table might need an update."
  9895. (setq org-table-may-need-update t))
  9896. (defconst org-table-line-regexp "^[ \t]*|"
  9897. "Detects an org-type table line.")
  9898. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  9899. "Detects an org-type table line.")
  9900. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  9901. "Detects a table line marked for automatic recalculation.")
  9902. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  9903. "Detects a table line marked for automatic recalculation.")
  9904. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  9905. "Detects a table line marked for automatic recalculation.")
  9906. (defconst org-table-hline-regexp "^[ \t]*|-"
  9907. "Detects an org-type table hline.")
  9908. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  9909. "Detects a table-type table hline.")
  9910. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  9911. "Detects an org-type or table-type table.")
  9912. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  9913. "Searching from within a table (any type) this finds the first line
  9914. outside the table.")
  9915. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  9916. "Searching from within a table (any type) this finds the first line
  9917. outside the table.")
  9918. (defun org-table-create-with-table.el ()
  9919. "Use the table.el package to insert a new table.
  9920. If there is already a table at point, convert between Org-mode tables
  9921. and table.el tables."
  9922. (interactive)
  9923. (require 'table)
  9924. (cond
  9925. ((org-at-table.el-p)
  9926. (if (y-or-n-p "Convert table to Org-mode table? ")
  9927. (org-table-convert)))
  9928. ((org-at-table-p)
  9929. (if (y-or-n-p "Convert table to table.el table? ")
  9930. (org-table-convert)))
  9931. (t (call-interactively 'table-insert))))
  9932. (defun org-table-create-or-convert-from-region (arg)
  9933. "Convert region to table, or create an empty table.
  9934. If there is an active region, convert it to a table. If there is no such
  9935. region, create an empty table."
  9936. (interactive "P")
  9937. (if (org-region-active-p)
  9938. (org-table-convert-region (region-beginning) (region-end) arg)
  9939. (org-table-create arg)))
  9940. (defun org-table-create (&optional size)
  9941. "Query for a size and insert a table skeleton.
  9942. SIZE is a string Columns x Rows like for example \"3x2\"."
  9943. (interactive "P")
  9944. (unless size
  9945. (setq size (read-string
  9946. (concat "Table size Columns x Rows [e.g. "
  9947. org-table-default-size "]: ")
  9948. "" nil org-table-default-size)))
  9949. (let* ((pos (point))
  9950. (indent (make-string (current-column) ?\ ))
  9951. (split (org-split-string size " *x *"))
  9952. (rows (string-to-number (nth 1 split)))
  9953. (columns (string-to-number (car split)))
  9954. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  9955. "\n")))
  9956. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  9957. (point-at-bol) (point)))
  9958. (beginning-of-line 1)
  9959. (newline))
  9960. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  9961. (dotimes (i rows) (insert line))
  9962. (goto-char pos)
  9963. (if (> rows 1)
  9964. ;; Insert a hline after the first row.
  9965. (progn
  9966. (end-of-line 1)
  9967. (insert "\n|-")
  9968. (goto-char pos)))
  9969. (org-table-align)))
  9970. (defun org-table-convert-region (beg0 end0 &optional nspace)
  9971. "Convert region to a table.
  9972. The region goes from BEG0 to END0, but these borders will be moved
  9973. slightly, to make sure a beginning of line in the first line is included.
  9974. When NSPACE is non-nil, it indicates the minimum number of spaces that
  9975. separate columns (default: just one space)."
  9976. (interactive "rP")
  9977. (let* ((beg (min beg0 end0))
  9978. (end (max beg0 end0))
  9979. (tabsep t)
  9980. re)
  9981. (goto-char beg)
  9982. (beginning-of-line 1)
  9983. (setq beg (move-marker (make-marker) (point)))
  9984. (goto-char end)
  9985. (if (bolp) (backward-char 1) (end-of-line 1))
  9986. (setq end (move-marker (make-marker) (point)))
  9987. ;; Lets see if this is tab-separated material. If every nonempty line
  9988. ;; contains a tab, we will assume that it is tab-separated material
  9989. (if nspace
  9990. (setq tabsep nil)
  9991. (goto-char beg)
  9992. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  9993. (if nspace (setq tabsep nil))
  9994. (if tabsep
  9995. (setq re "^\\|\t")
  9996. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  9997. (max 1 (prefix-numeric-value nspace)))))
  9998. (goto-char beg)
  9999. (while (re-search-forward re end t)
  10000. (replace-match "|" t t))
  10001. (goto-char beg)
  10002. (insert " ")
  10003. (org-table-align)))
  10004. (defun org-table-import (file arg)
  10005. "Import FILE as a table.
  10006. The file is assumed to be tab-separated. Such files can be produced by most
  10007. spreadsheet and database applications. If no tabs (at least one per line)
  10008. are found, lines will be split on whitespace into fields."
  10009. (interactive "f\nP")
  10010. (or (bolp) (newline))
  10011. (let ((beg (point))
  10012. (pm (point-max)))
  10013. (insert-file-contents file)
  10014. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  10015. (defun org-table-export ()
  10016. "Export table as a tab-separated file.
  10017. Such a file can be imported into a spreadsheet program like Excel."
  10018. (interactive)
  10019. (let* ((beg (org-table-begin))
  10020. (end (org-table-end))
  10021. (table (buffer-substring beg end))
  10022. (file (read-file-name "Export table to: "))
  10023. buf)
  10024. (unless (or (not (file-exists-p file))
  10025. (y-or-n-p (format "Overwrite file %s? " file)))
  10026. (error "Abort"))
  10027. (with-current-buffer (find-file-noselect file)
  10028. (setq buf (current-buffer))
  10029. (erase-buffer)
  10030. (fundamental-mode)
  10031. (insert table)
  10032. (goto-char (point-min))
  10033. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  10034. (replace-match "" t t)
  10035. (end-of-line 1))
  10036. (goto-char (point-min))
  10037. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  10038. (replace-match "" t t)
  10039. (goto-char (min (1+ (point)) (point-max))))
  10040. (goto-char (point-min))
  10041. (while (re-search-forward "^-[-+]*$" nil t)
  10042. (replace-match "")
  10043. (if (looking-at "\n")
  10044. (delete-char 1)))
  10045. (goto-char (point-min))
  10046. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  10047. (replace-match "\t" t t))
  10048. (save-buffer))
  10049. (kill-buffer buf)))
  10050. (defvar org-table-aligned-begin-marker (make-marker)
  10051. "Marker at the beginning of the table last aligned.
  10052. Used to check if cursor still is in that table, to minimize realignment.")
  10053. (defvar org-table-aligned-end-marker (make-marker)
  10054. "Marker at the end of the table last aligned.
  10055. Used to check if cursor still is in that table, to minimize realignment.")
  10056. (defvar org-table-last-alignment nil
  10057. "List of flags for flushright alignment, from the last re-alignment.
  10058. This is being used to correctly align a single field after TAB or RET.")
  10059. (defvar org-table-last-column-widths nil
  10060. "List of max width of fields in each column.
  10061. This is being used to correctly align a single field after TAB or RET.")
  10062. (defvar org-last-recalc-line nil)
  10063. (defconst org-narrow-column-arrow "=>"
  10064. "Used as display property in narrowed table columns.")
  10065. (defun org-table-align ()
  10066. "Align the table at point by aligning all vertical bars."
  10067. (interactive)
  10068. (let* (
  10069. ;; Limits of table
  10070. (beg (org-table-begin))
  10071. (end (org-table-end))
  10072. ;; Current cursor position
  10073. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10074. (colpos (org-table-current-column))
  10075. (winstart (window-start))
  10076. lines (new "") lengths l typenums ty fields maxfields i
  10077. column
  10078. (indent "") cnt frac
  10079. rfmt hfmt
  10080. (spaces '(1 . 1))
  10081. (sp1 (car spaces))
  10082. (sp2 (cdr spaces))
  10083. (rfmt1 (concat
  10084. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  10085. (hfmt1 (concat
  10086. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  10087. emptystrings links narrow fmax f1 len c e)
  10088. (untabify beg end)
  10089. (remove-text-properties beg end '(org-cwidth t display t))
  10090. ;; Check if we have links
  10091. (goto-char beg)
  10092. (setq links (re-search-forward org-bracket-link-regexp end t))
  10093. ;; Make sure the link properties are right
  10094. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  10095. ;; Check if we are narrowing any columns
  10096. (goto-char beg)
  10097. (setq narrow (and org-format-transports-properties-p
  10098. (re-search-forward "<[0-9]+>" end t)))
  10099. ;; Get the rows
  10100. (setq lines (org-split-string
  10101. (buffer-substring beg end) "\n"))
  10102. ;; Store the indentation of the first line
  10103. (if (string-match "^ *" (car lines))
  10104. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  10105. ;; Mark the hlines by setting the corresponding element to nil
  10106. ;; At the same time, we remove trailing space.
  10107. (setq lines (mapcar (lambda (l)
  10108. (if (string-match "^ *|-" l)
  10109. nil
  10110. (if (string-match "[ \t]+$" l)
  10111. (substring l 0 (match-beginning 0))
  10112. l)))
  10113. lines))
  10114. ;; Get the data fields by splitting the lines.
  10115. (setq fields (mapcar
  10116. (lambda (l)
  10117. (org-split-string l " *| *"))
  10118. (delq nil (copy-sequence lines))))
  10119. ;; How many fields in the longest line?
  10120. (condition-case nil
  10121. (setq maxfields (apply 'max (mapcar 'length fields)))
  10122. (error
  10123. (kill-region beg end)
  10124. (org-table-create org-table-default-size)
  10125. (error "Empty table - created default table")))
  10126. ;; A list of empty string to fill any short rows on output
  10127. (setq emptystrings (make-list maxfields ""))
  10128. ;; Check for special formatting.
  10129. (setq i -1)
  10130. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  10131. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  10132. ;; Check if there is an explicit width specified
  10133. (when (and org-table-limit-column-width narrow)
  10134. (setq c column fmax nil)
  10135. (while c
  10136. (setq e (pop c))
  10137. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  10138. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  10139. ;; Find fields that are wider than fmax, and shorten them
  10140. (when fmax
  10141. (loop for xx in column do
  10142. (when (and (stringp xx)
  10143. (> (org-string-width xx) fmax))
  10144. (org-add-props xx nil
  10145. 'help-echo
  10146. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  10147. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  10148. (unless (> f1 1)
  10149. (error "Cannot narrow field starting with wide link \"%s\""
  10150. (match-string 0 xx)))
  10151. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  10152. (add-text-properties (- f1 2) f1
  10153. (list 'display org-narrow-column-arrow)
  10154. xx)))))
  10155. ;; Get the maximum width for each column
  10156. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  10157. ;; Get the fraction of numbers, to decide about alignment of the column
  10158. (setq cnt 0 frac 0.0)
  10159. (loop for x in column do
  10160. (if (equal x "")
  10161. nil
  10162. (setq frac ( / (+ (* frac cnt)
  10163. (if (string-match org-table-number-regexp x) 1 0))
  10164. (setq cnt (1+ cnt))))))
  10165. (push (>= frac org-table-number-fraction) typenums))
  10166. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  10167. ;; Store the alignment of this table, for later editing of single fields
  10168. (setq org-table-last-alignment typenums
  10169. org-table-last-column-widths lengths)
  10170. ;; With invisible characters, `format' does not get the field width right
  10171. ;; So we need to make these fields wide by hand.
  10172. (when links
  10173. (loop for i from 0 upto (1- maxfields) do
  10174. (setq len (nth i lengths))
  10175. (loop for j from 0 upto (1- (length fields)) do
  10176. (setq c (nthcdr i (car (nthcdr j fields))))
  10177. (if (and (stringp (car c))
  10178. (string-match org-bracket-link-regexp (car c))
  10179. (< (org-string-width (car c)) len))
  10180. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  10181. ;; Compute the formats needed for output of the table
  10182. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  10183. (while (setq l (pop lengths))
  10184. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  10185. (setq rfmt (concat rfmt (format rfmt1 ty l))
  10186. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  10187. (setq rfmt (concat rfmt "\n")
  10188. hfmt (concat (substring hfmt 0 -1) "|\n"))
  10189. (setq new (mapconcat
  10190. (lambda (l)
  10191. (if l (apply 'format rfmt
  10192. (append (pop fields) emptystrings))
  10193. hfmt))
  10194. lines ""))
  10195. ;; Replace the old one
  10196. (delete-region beg end)
  10197. (move-marker end nil)
  10198. (move-marker org-table-aligned-begin-marker (point))
  10199. (insert new)
  10200. (move-marker org-table-aligned-end-marker (point))
  10201. (when (and orgtbl-mode (not (org-mode-p)))
  10202. (goto-char org-table-aligned-begin-marker)
  10203. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  10204. ;; Try to move to the old location (approximately)
  10205. (goto-line linepos)
  10206. (set-window-start (selected-window) winstart 'noforce)
  10207. (org-table-goto-column colpos)
  10208. (setq org-table-may-need-update nil)
  10209. ))
  10210. (defun org-string-width (s)
  10211. "Compute width of string, ignoring invisible characters.
  10212. This ignores character with invisibility property `org-link', and also
  10213. characters with property `org-cwidth', because these will become invisible
  10214. upon the next fontification round."
  10215. (let (b)
  10216. (when (or (eq t buffer-invisibility-spec)
  10217. (assq 'org-link buffer-invisibility-spec))
  10218. (while (setq b (text-property-any 0 (length s)
  10219. 'invisible 'org-link s))
  10220. (setq s (concat (substring s 0 b)
  10221. (substring s (or (next-single-property-change
  10222. b 'invisible s) (length s)))))))
  10223. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  10224. (setq s (concat (substring s 0 b)
  10225. (substring s (or (next-single-property-change
  10226. b 'org-cwidth s) (length s))))))
  10227. (string-width s)))
  10228. (defun org-table-begin (&optional table-type)
  10229. "Find the beginning of the table and return its position.
  10230. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  10231. (save-excursion
  10232. (if (not (re-search-backward
  10233. (if table-type org-table-any-border-regexp
  10234. org-table-border-regexp)
  10235. nil t))
  10236. (progn (goto-char (point-min)) (point))
  10237. (goto-char (match-beginning 0))
  10238. (beginning-of-line 2)
  10239. (point))))
  10240. (defun org-table-end (&optional table-type)
  10241. "Find the end of the table and return its position.
  10242. With argument TABLE-TYPE, go to the end of a table.el-type table."
  10243. (save-excursion
  10244. (if (not (re-search-forward
  10245. (if table-type org-table-any-border-regexp
  10246. org-table-border-regexp)
  10247. nil t))
  10248. (goto-char (point-max))
  10249. (goto-char (match-beginning 0)))
  10250. (point-marker)))
  10251. (defun org-table-justify-field-maybe (&optional new)
  10252. "Justify the current field, text to left, number to right.
  10253. Optional argument NEW may specify text to replace the current field content."
  10254. (cond
  10255. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  10256. ((org-at-table-hline-p))
  10257. ((and (not new)
  10258. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  10259. (current-buffer)))
  10260. (< (point) org-table-aligned-begin-marker)
  10261. (>= (point) org-table-aligned-end-marker)))
  10262. ;; This is not the same table, force a full re-align
  10263. (setq org-table-may-need-update t))
  10264. (t ;; realign the current field, based on previous full realign
  10265. (let* ((pos (point)) s
  10266. (col (org-table-current-column))
  10267. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  10268. l f n o e)
  10269. (when (> col 0)
  10270. (skip-chars-backward "^|\n")
  10271. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  10272. (progn
  10273. (setq s (match-string 1)
  10274. o (match-string 0)
  10275. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  10276. e (not (= (match-beginning 2) (match-end 2))))
  10277. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  10278. l (if e "|" (setq org-table-may-need-update t) ""))
  10279. n (format f s))
  10280. (if new
  10281. (if (<= (length new) l) ;; FIXME: length -> str-width?
  10282. (setq n (format f new))
  10283. (setq n (concat new "|") org-table-may-need-update t)))
  10284. (or (equal n o)
  10285. (let (org-table-may-need-update)
  10286. (replace-match n))))
  10287. (setq org-table-may-need-update t))
  10288. (goto-char pos))))))
  10289. (defun org-table-next-field ()
  10290. "Go to the next field in the current table, creating new lines as needed.
  10291. Before doing so, re-align the table if necessary."
  10292. (interactive)
  10293. (org-table-maybe-eval-formula)
  10294. (org-table-maybe-recalculate-line)
  10295. (if (and org-table-automatic-realign
  10296. org-table-may-need-update)
  10297. (org-table-align))
  10298. (let ((end (org-table-end)))
  10299. (if (org-at-table-hline-p)
  10300. (end-of-line 1))
  10301. (condition-case nil
  10302. (progn
  10303. (re-search-forward "|" end)
  10304. (if (looking-at "[ \t]*$")
  10305. (re-search-forward "|" end))
  10306. (if (and (looking-at "-")
  10307. org-table-tab-jumps-over-hlines
  10308. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  10309. (goto-char (match-beginning 1)))
  10310. (if (looking-at "-")
  10311. (progn
  10312. (beginning-of-line 0)
  10313. (org-table-insert-row 'below))
  10314. (if (looking-at " ") (forward-char 1))))
  10315. (error
  10316. (org-table-insert-row 'below)))))
  10317. (defun org-table-previous-field ()
  10318. "Go to the previous field in the table.
  10319. Before doing so, re-align the table if necessary."
  10320. (interactive)
  10321. (org-table-justify-field-maybe)
  10322. (org-table-maybe-recalculate-line)
  10323. (if (and org-table-automatic-realign
  10324. org-table-may-need-update)
  10325. (org-table-align))
  10326. (if (org-at-table-hline-p)
  10327. (end-of-line 1))
  10328. (re-search-backward "|" (org-table-begin))
  10329. (re-search-backward "|" (org-table-begin))
  10330. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  10331. (re-search-backward "|" (org-table-begin)))
  10332. (if (looking-at "| ?")
  10333. (goto-char (match-end 0))))
  10334. (defun org-table-next-row ()
  10335. "Go to the next row (same column) in the current table.
  10336. Before doing so, re-align the table if necessary."
  10337. (interactive)
  10338. (org-table-maybe-eval-formula)
  10339. (org-table-maybe-recalculate-line)
  10340. (if (or (looking-at "[ \t]*$")
  10341. (save-excursion (skip-chars-backward " \t") (bolp)))
  10342. (newline)
  10343. (if (and org-table-automatic-realign
  10344. org-table-may-need-update)
  10345. (org-table-align))
  10346. (let ((col (org-table-current-column)))
  10347. (beginning-of-line 2)
  10348. (if (or (not (org-at-table-p))
  10349. (org-at-table-hline-p))
  10350. (progn
  10351. (beginning-of-line 0)
  10352. (org-table-insert-row 'below)))
  10353. (org-table-goto-column col)
  10354. (skip-chars-backward "^|\n\r")
  10355. (if (looking-at " ") (forward-char 1)))))
  10356. (defun org-table-copy-down (n)
  10357. "Copy a field down in the current column.
  10358. If the field at the cursor is empty, copy into it the content of the nearest
  10359. non-empty field above. With argument N, use the Nth non-empty field.
  10360. If the current field is not empty, it is copied down to the next row, and
  10361. the cursor is moved with it. Therefore, repeating this command causes the
  10362. column to be filled row-by-row.
  10363. If the variable `org-table-copy-increment' is non-nil and the field is an
  10364. integer, it will be incremented while copying."
  10365. (interactive "p")
  10366. (let* ((colpos (org-table-current-column))
  10367. (field (org-table-get-field))
  10368. (non-empty (string-match "[^ \t]" field))
  10369. (beg (org-table-begin))
  10370. txt)
  10371. (org-table-check-inside-data-field)
  10372. (if non-empty
  10373. (progn
  10374. (setq txt (org-trim field))
  10375. (org-table-next-row)
  10376. (org-table-blank-field))
  10377. (save-excursion
  10378. (setq txt
  10379. (catch 'exit
  10380. (while (progn (beginning-of-line 1)
  10381. (re-search-backward org-table-dataline-regexp
  10382. beg t))
  10383. (org-table-goto-column colpos t)
  10384. (if (and (looking-at
  10385. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  10386. (= (setq n (1- n)) 0))
  10387. (throw 'exit (match-string 1))))))))
  10388. (if txt
  10389. (progn
  10390. (if (and org-table-copy-increment
  10391. (string-match "^[0-9]+$" txt))
  10392. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  10393. (insert txt)
  10394. (org-table-maybe-recalculate-line)
  10395. (org-table-align))
  10396. (error "No non-empty field found"))))
  10397. (defun org-table-check-inside-data-field ()
  10398. "Is point inside a table data field?
  10399. I.e. not on a hline or before the first or after the last column?
  10400. This actually throws an error, so it aborts the current command."
  10401. (if (or (not (org-at-table-p))
  10402. (= (org-table-current-column) 0)
  10403. (org-at-table-hline-p)
  10404. (looking-at "[ \t]*$"))
  10405. (error "Not in table data field")))
  10406. (defvar org-table-clip nil
  10407. "Clipboard for table regions.")
  10408. (defun org-table-blank-field ()
  10409. "Blank the current table field or active region."
  10410. (interactive)
  10411. (org-table-check-inside-data-field)
  10412. (if (and (interactive-p) (org-region-active-p))
  10413. (let (org-table-clip)
  10414. (org-table-cut-region (region-beginning) (region-end)))
  10415. (skip-chars-backward "^|")
  10416. (backward-char 1)
  10417. (if (looking-at "|[^|\n]+")
  10418. (let* ((pos (match-beginning 0))
  10419. (match (match-string 0))
  10420. (len (org-string-width match)))
  10421. (replace-match (concat "|" (make-string (1- len) ?\ )))
  10422. (goto-char (+ 2 pos))
  10423. (substring match 1)))))
  10424. (defun org-table-get-field (&optional n replace)
  10425. "Return the value of the field in column N of current row.
  10426. N defaults to current field.
  10427. If REPLACE is a string, replace field with this value. The return value
  10428. is always the old value."
  10429. (and n (org-table-goto-column n))
  10430. (skip-chars-backward "^|\n")
  10431. (backward-char 1)
  10432. (if (looking-at "|[^|\r\n]*")
  10433. (let* ((pos (match-beginning 0))
  10434. (val (buffer-substring (1+ pos) (match-end 0))))
  10435. (if replace
  10436. (replace-match (concat "|" replace)))
  10437. (goto-char (min (point-at-eol) (+ 2 pos)))
  10438. val)
  10439. (forward-char 1) ""))
  10440. (defun org-table-current-column ()
  10441. "Find out which column we are in.
  10442. When called interactively, column is also displayed in echo area."
  10443. (interactive)
  10444. (if (interactive-p) (org-table-check-inside-data-field))
  10445. (save-excursion
  10446. (let ((cnt 0) (pos (point)))
  10447. (beginning-of-line 1)
  10448. (while (search-forward "|" pos t)
  10449. (setq cnt (1+ cnt)))
  10450. (if (interactive-p) (message "This is table column %d" cnt))
  10451. cnt)))
  10452. (defun org-table-goto-column (n &optional on-delim force)
  10453. "Move the cursor to the Nth column in the current table line.
  10454. With optional argument ON-DELIM, stop with point before the left delimiter
  10455. of the field.
  10456. If there are less than N fields, just go to after the last delimiter.
  10457. However, when FORCE is non-nil, create new columns if necessary."
  10458. (interactive "p")
  10459. (let ((pos (point-at-eol)))
  10460. (beginning-of-line 1)
  10461. (when (> n 0)
  10462. (while (and (> (setq n (1- n)) -1)
  10463. (or (search-forward "|" pos t)
  10464. (and force
  10465. (progn (end-of-line 1)
  10466. (skip-chars-backward "^|")
  10467. (insert " | "))))))
  10468. ; (backward-char 2) t)))))
  10469. (when (and force (not (looking-at ".*|")))
  10470. (save-excursion (end-of-line 1) (insert " | ")))
  10471. (if on-delim
  10472. (backward-char 1)
  10473. (if (looking-at " ") (forward-char 1))))))
  10474. (defun org-at-table-p (&optional table-type)
  10475. "Return t if the cursor is inside an org-type table.
  10476. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  10477. (if org-enable-table-editor
  10478. (save-excursion
  10479. (beginning-of-line 1)
  10480. (looking-at (if table-type org-table-any-line-regexp
  10481. org-table-line-regexp)))
  10482. nil))
  10483. (defun org-at-table.el-p ()
  10484. "Return t if and only if we are at a table.el table."
  10485. (and (org-at-table-p 'any)
  10486. (save-excursion
  10487. (goto-char (org-table-begin 'any))
  10488. (looking-at org-table1-hline-regexp))))
  10489. (defun org-table-recognize-table.el ()
  10490. "If there is a table.el table nearby, recognize it and move into it."
  10491. (if org-table-tab-recognizes-table.el
  10492. (if (org-at-table.el-p)
  10493. (progn
  10494. (beginning-of-line 1)
  10495. (if (looking-at org-table-dataline-regexp)
  10496. nil
  10497. (if (looking-at org-table1-hline-regexp)
  10498. (progn
  10499. (beginning-of-line 2)
  10500. (if (looking-at org-table-any-border-regexp)
  10501. (beginning-of-line -1)))))
  10502. (if (re-search-forward "|" (org-table-end t) t)
  10503. (progn
  10504. (require 'table)
  10505. (if (table--at-cell-p (point))
  10506. t
  10507. (message "recognizing table.el table...")
  10508. (table-recognize-table)
  10509. (message "recognizing table.el table...done")))
  10510. (error "This should not happen..."))
  10511. t)
  10512. nil)
  10513. nil))
  10514. (defun org-at-table-hline-p ()
  10515. "Return t if the cursor is inside a hline in a table."
  10516. (if org-enable-table-editor
  10517. (save-excursion
  10518. (beginning-of-line 1)
  10519. (looking-at org-table-hline-regexp))
  10520. nil))
  10521. (defun org-table-insert-column ()
  10522. "Insert a new column into the table."
  10523. (interactive)
  10524. (if (not (org-at-table-p))
  10525. (error "Not at a table"))
  10526. (org-table-find-dataline)
  10527. (let* ((col (max 1 (org-table-current-column)))
  10528. (beg (org-table-begin))
  10529. (end (org-table-end))
  10530. ;; Current cursor position
  10531. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10532. (colpos col))
  10533. (goto-char beg)
  10534. (while (< (point) end)
  10535. (if (org-at-table-hline-p)
  10536. nil
  10537. (org-table-goto-column col t)
  10538. (insert "| "))
  10539. (beginning-of-line 2))
  10540. (move-marker end nil)
  10541. (goto-line linepos)
  10542. (org-table-goto-column colpos)
  10543. (org-table-align)
  10544. (org-table-modify-formulas 'insert col)))
  10545. (defun org-table-find-dataline ()
  10546. "Find a dataline in the current table, which is needed for column commands."
  10547. (if (and (org-at-table-p)
  10548. (not (org-at-table-hline-p)))
  10549. t
  10550. (let ((col (current-column))
  10551. (end (org-table-end)))
  10552. (move-to-column col)
  10553. (while (and (< (point) end)
  10554. (or (not (= (current-column) col))
  10555. (org-at-table-hline-p)))
  10556. (beginning-of-line 2)
  10557. (move-to-column col))
  10558. (if (and (org-at-table-p)
  10559. (not (org-at-table-hline-p)))
  10560. t
  10561. (error
  10562. "Please position cursor in a data line for column operations")))))
  10563. (defun org-table-delete-column ()
  10564. "Delete a column from the table."
  10565. (interactive)
  10566. (if (not (org-at-table-p))
  10567. (error "Not at a table"))
  10568. (org-table-find-dataline)
  10569. (org-table-check-inside-data-field)
  10570. (let* ((col (org-table-current-column))
  10571. (beg (org-table-begin))
  10572. (end (org-table-end))
  10573. ;; Current cursor position
  10574. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10575. (colpos col))
  10576. (goto-char beg)
  10577. (while (< (point) end)
  10578. (if (org-at-table-hline-p)
  10579. nil
  10580. (org-table-goto-column col t)
  10581. (and (looking-at "|[^|\n]+|")
  10582. (replace-match "|")))
  10583. (beginning-of-line 2))
  10584. (move-marker end nil)
  10585. (goto-line linepos)
  10586. (org-table-goto-column colpos)
  10587. (org-table-align)
  10588. (org-table-modify-formulas 'remove col)))
  10589. (defun org-table-move-column-right ()
  10590. "Move column to the right."
  10591. (interactive)
  10592. (org-table-move-column nil))
  10593. (defun org-table-move-column-left ()
  10594. "Move column to the left."
  10595. (interactive)
  10596. (org-table-move-column 'left))
  10597. (defun org-table-move-column (&optional left)
  10598. "Move the current column to the right. With arg LEFT, move to the left."
  10599. (interactive "P")
  10600. (if (not (org-at-table-p))
  10601. (error "Not at a table"))
  10602. (org-table-find-dataline)
  10603. (org-table-check-inside-data-field)
  10604. (let* ((col (org-table-current-column))
  10605. (col1 (if left (1- col) col))
  10606. (beg (org-table-begin))
  10607. (end (org-table-end))
  10608. ;; Current cursor position
  10609. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10610. (colpos (if left (1- col) (1+ col))))
  10611. (if (and left (= col 1))
  10612. (error "Cannot move column further left"))
  10613. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  10614. (error "Cannot move column further right"))
  10615. (goto-char beg)
  10616. (while (< (point) end)
  10617. (if (org-at-table-hline-p)
  10618. nil
  10619. (org-table-goto-column col1 t)
  10620. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  10621. (replace-match "|\\2|\\1|")))
  10622. (beginning-of-line 2))
  10623. (move-marker end nil)
  10624. (goto-line linepos)
  10625. (org-table-goto-column colpos)
  10626. (org-table-align)
  10627. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  10628. (defun org-table-move-row-down ()
  10629. "Move table row down."
  10630. (interactive)
  10631. (org-table-move-row nil))
  10632. (defun org-table-move-row-up ()
  10633. "Move table row up."
  10634. (interactive)
  10635. (org-table-move-row 'up))
  10636. (defun org-table-move-row (&optional up)
  10637. "Move the current table line down. With arg UP, move it up."
  10638. (interactive "P")
  10639. (let ((col (current-column))
  10640. (pos (point))
  10641. (tonew (if up 0 2))
  10642. txt)
  10643. (beginning-of-line tonew)
  10644. (if (not (org-at-table-p))
  10645. (progn
  10646. (goto-char pos)
  10647. (error "Cannot move row further")))
  10648. (goto-char pos)
  10649. (beginning-of-line 1)
  10650. (setq pos (point))
  10651. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  10652. (delete-region (point) (1+ (point-at-eol)))
  10653. (beginning-of-line tonew)
  10654. (insert txt)
  10655. (beginning-of-line 0)
  10656. (move-to-column col)))
  10657. (defun org-table-insert-row (&optional arg)
  10658. "Insert a new row above the current line into the table.
  10659. With prefix ARG, insert below the current line."
  10660. (interactive "P")
  10661. (if (not (org-at-table-p))
  10662. (error "Not at a table"))
  10663. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  10664. (new (org-table-clean-line line)))
  10665. ;; Fix the first field if necessary
  10666. (if (string-match "^[ \t]*| *[#$] *|" line)
  10667. (setq new (replace-match (match-string 0 line) t t new)))
  10668. (beginning-of-line (if arg 2 1))
  10669. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  10670. (beginning-of-line 0)
  10671. (re-search-forward "| ?" (point-at-eol) t)
  10672. (and org-table-may-need-update (org-table-align))))
  10673. (defun org-table-insert-hline (&optional arg)
  10674. "Insert a horizontal-line below the current line into the table.
  10675. With prefix ARG, insert above the current line."
  10676. (interactive "P")
  10677. (if (not (org-at-table-p))
  10678. (error "Not at a table"))
  10679. (let ((line (org-table-clean-line
  10680. (buffer-substring (point-at-bol) (point-at-eol))))
  10681. (col (current-column)))
  10682. (while (string-match "|\\( +\\)|" line)
  10683. (setq line (replace-match
  10684. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  10685. ?-) "|") t t line)))
  10686. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  10687. (beginning-of-line (if arg 1 2))
  10688. (insert line "\n")
  10689. (beginning-of-line (if arg 1 -1))
  10690. (move-to-column col)))
  10691. (defun org-table-clean-line (s)
  10692. "Convert a table line S into a string with only \"|\" and space.
  10693. In particular, this does handle wide and invisible characters."
  10694. (if (string-match "^[ \t]*|-" s)
  10695. ;; It's a hline, just map the characters
  10696. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  10697. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  10698. (setq s (replace-match
  10699. (concat "|" (make-string (org-string-width (match-string 1 s))
  10700. ?\ ) "|")
  10701. t t s)))
  10702. s))
  10703. (defun org-table-kill-row ()
  10704. "Delete the current row or horizontal line from the table."
  10705. (interactive)
  10706. (if (not (org-at-table-p))
  10707. (error "Not at a table"))
  10708. (let ((col (current-column)))
  10709. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  10710. (if (not (org-at-table-p)) (beginning-of-line 0))
  10711. (move-to-column col)))
  10712. (defun org-table-sort-lines (beg end numericp)
  10713. "Sort table lines in region.
  10714. Point and mark define the first and last line to include. Both point and
  10715. mark should be in the column that is used for sorting. For example, to
  10716. sort according to column 3, put the mark in the first line to sort, in
  10717. table column 3. Put point into the last line to be included in the sorting,
  10718. also in table column 3. The command will prompt for the sorting method
  10719. \(n for numerical, a for alphanumeric)."
  10720. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  10721. (setq numericp (string-match "[nN]" numericp))
  10722. (org-table-align) ;; Just to be safe
  10723. (let* (bcol ecol cmp column lns)
  10724. (goto-char beg)
  10725. (org-table-check-inside-data-field)
  10726. (setq column (org-table-current-column)
  10727. beg (move-marker (make-marker) (point-at-bol)))
  10728. (goto-char end)
  10729. (org-table-check-inside-data-field)
  10730. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  10731. (untabify beg end)
  10732. (goto-char beg)
  10733. (org-table-goto-column column)
  10734. (skip-chars-backward "^|")
  10735. (setq bcol (current-column))
  10736. (org-table-goto-column (1+ column))
  10737. (skip-chars-backward "^|")
  10738. (setq ecol (1- (current-column)))
  10739. (setq cmp (if numericp
  10740. (lambda (a b) (< (car a) (car b)))
  10741. (lambda (a b) (string< (car a) (car b)))))
  10742. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  10743. (org-split-string (buffer-substring beg end) "\n")))
  10744. (if numericp
  10745. (setq lns (mapcar (lambda(x)
  10746. (cons (string-to-number (car x)) (cdr x)))
  10747. lns)))
  10748. (delete-region beg end)
  10749. (move-marker beg nil)
  10750. (move-marker end nil)
  10751. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  10752. (message "%d lines sorted %s based on column %d"
  10753. (length lns)
  10754. (if numericp "numerically" "alphabetically") column)))
  10755. (defun org-table-cut-region (beg end)
  10756. "Copy region in table to the clipboard and blank all relevant fields."
  10757. (interactive "r")
  10758. (org-table-copy-region beg end 'cut))
  10759. (defun org-table-copy-region (beg end &optional cut)
  10760. "Copy rectangular region in table to clipboard.
  10761. A special clipboard is used which can only be accessed
  10762. with `org-table-paste-rectangle'."
  10763. (interactive "rP")
  10764. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  10765. region cols
  10766. (rpl (if cut " " nil)))
  10767. (goto-char beg)
  10768. (org-table-check-inside-data-field)
  10769. (setq l01 (count-lines (point-min) (point))
  10770. c01 (org-table-current-column))
  10771. (goto-char end)
  10772. (org-table-check-inside-data-field)
  10773. (setq l02 (count-lines (point-min) (point))
  10774. c02 (org-table-current-column))
  10775. (setq l1 (min l01 l02) l2 (max l01 l02)
  10776. c1 (min c01 c02) c2 (max c01 c02))
  10777. (catch 'exit
  10778. (while t
  10779. (catch 'nextline
  10780. (if (> l1 l2) (throw 'exit t))
  10781. (goto-line l1)
  10782. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  10783. (setq cols nil ic1 c1 ic2 c2)
  10784. (while (< ic1 (1+ ic2))
  10785. (push (org-table-get-field ic1 rpl) cols)
  10786. (setq ic1 (1+ ic1)))
  10787. (push (nreverse cols) region)
  10788. (setq l1 (1+ l1)))))
  10789. (setq org-table-clip (nreverse region))
  10790. (if cut (org-table-align))
  10791. org-table-clip))
  10792. (defun org-table-paste-rectangle ()
  10793. "Paste a rectangular region into a table.
  10794. The upper right corner ends up in the current field. All involved fields
  10795. will be overwritten. If the rectangle does not fit into the present table,
  10796. the table is enlarged as needed. The process ignores horizontal separator
  10797. lines."
  10798. (interactive)
  10799. (unless (and org-table-clip (listp org-table-clip))
  10800. (error "First cut/copy a region to paste!"))
  10801. (org-table-check-inside-data-field)
  10802. (let* ((clip org-table-clip)
  10803. (line (count-lines (point-min) (point)))
  10804. (col (org-table-current-column))
  10805. (org-enable-table-editor t)
  10806. (org-table-automatic-realign nil)
  10807. c cols field)
  10808. (while (setq cols (pop clip))
  10809. (while (org-at-table-hline-p) (beginning-of-line 2))
  10810. (if (not (org-at-table-p))
  10811. (progn (end-of-line 0) (org-table-next-field)))
  10812. (setq c col)
  10813. (while (setq field (pop cols))
  10814. (org-table-goto-column c nil 'force)
  10815. (org-table-get-field nil field)
  10816. (setq c (1+ c)))
  10817. (beginning-of-line 2))
  10818. (goto-line line)
  10819. (org-table-goto-column col)
  10820. (org-table-align)))
  10821. (defun org-table-convert ()
  10822. "Convert from `org-mode' table to table.el and back.
  10823. Obviously, this only works within limits. When an Org-mode table is
  10824. converted to table.el, all horizontal separator lines get lost, because
  10825. table.el uses these as cell boundaries and has no notion of horizontal lines.
  10826. A table.el table can be converted to an Org-mode table only if it does not
  10827. do row or column spanning. Multiline cells will become multiple cells.
  10828. Beware, Org-mode does not test if the table can be successfully converted - it
  10829. blindly applies a recipe that works for simple tables."
  10830. (interactive)
  10831. (require 'table)
  10832. (if (org-at-table.el-p)
  10833. ;; convert to Org-mode table
  10834. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  10835. (end (move-marker (make-marker) (org-table-end t))))
  10836. (table-unrecognize-region beg end)
  10837. (goto-char beg)
  10838. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  10839. (replace-match ""))
  10840. (goto-char beg))
  10841. (if (org-at-table-p)
  10842. ;; convert to table.el table
  10843. (let ((beg (move-marker (make-marker) (org-table-begin)))
  10844. (end (move-marker (make-marker) (org-table-end))))
  10845. ;; first, get rid of all horizontal lines
  10846. (goto-char beg)
  10847. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  10848. (replace-match ""))
  10849. ;; insert a hline before first
  10850. (goto-char beg)
  10851. (org-table-insert-hline 'above)
  10852. (beginning-of-line -1)
  10853. ;; insert a hline after each line
  10854. (while (progn (beginning-of-line 3) (< (point) end))
  10855. (org-table-insert-hline))
  10856. (goto-char beg)
  10857. (setq end (move-marker end (org-table-end)))
  10858. ;; replace "+" at beginning and ending of hlines
  10859. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  10860. (replace-match "\\1+-"))
  10861. (goto-char beg)
  10862. (while (re-search-forward "-|[ \t]*$" end t)
  10863. (replace-match "-+"))
  10864. (goto-char beg)))))
  10865. (defun org-table-wrap-region (arg)
  10866. "Wrap several fields in a column like a paragraph.
  10867. This is useful if you'd like to spread the contents of a field over several
  10868. lines, in order to keep the table compact.
  10869. If there is an active region, and both point and mark are in the same column,
  10870. the text in the column is wrapped to minimum width for the given number of
  10871. lines. Generally, this makes the table more compact. A prefix ARG may be
  10872. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  10873. formats the selected text to two lines. If the region was longer than two
  10874. lines, the remaining lines remain empty. A negative prefix argument reduces
  10875. the current number of lines by that amount. The wrapped text is pasted back
  10876. into the table. If you formatted it to more lines than it was before, fields
  10877. further down in the table get overwritten - so you might need to make space in
  10878. the table first.
  10879. If there is no region, the current field is split at the cursor position and
  10880. the text fragment to the right of the cursor is prepended to the field one
  10881. line down.
  10882. If there is no region, but you specify a prefix ARG, the current field gets
  10883. blank, and the content is appended to the field above."
  10884. (interactive "P")
  10885. (org-table-check-inside-data-field)
  10886. (if (org-region-active-p)
  10887. ;; There is a region: fill as a paragraph
  10888. (let ((beg (region-beginning))
  10889. nlines)
  10890. (org-table-cut-region (region-beginning) (region-end))
  10891. (if (> (length (car org-table-clip)) 1)
  10892. (error "Region must be limited to single column"))
  10893. (setq nlines (if arg
  10894. (if (< arg 1)
  10895. (+ (length org-table-clip) arg)
  10896. arg)
  10897. (length org-table-clip)))
  10898. (setq org-table-clip
  10899. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  10900. nil nlines)))
  10901. (goto-char beg)
  10902. (org-table-paste-rectangle))
  10903. ;; No region, split the current field at point
  10904. (if arg
  10905. ;; combine with field above
  10906. (let ((s (org-table-blank-field))
  10907. (col (org-table-current-column)))
  10908. (beginning-of-line 0)
  10909. (while (org-at-table-hline-p) (beginning-of-line 0))
  10910. (org-table-goto-column col)
  10911. (skip-chars-forward "^|")
  10912. (skip-chars-backward " ")
  10913. (insert " " (org-trim s))
  10914. (org-table-align))
  10915. ;; split field
  10916. (when (looking-at "\\([^|]+\\)+|")
  10917. (let ((s (match-string 1)))
  10918. (replace-match " |")
  10919. (goto-char (match-beginning 0))
  10920. (org-table-next-row)
  10921. (insert (org-trim s) " ")
  10922. (org-table-align))))))
  10923. (defvar org-field-marker nil)
  10924. (defun org-table-edit-field (arg)
  10925. "Edit table field in a different window.
  10926. This is mainly useful for fields that contain hidden parts.
  10927. When called with a \\[universal-argument] prefix, just make the full field visible so that
  10928. it can be edited in place."
  10929. (interactive "P")
  10930. (if arg
  10931. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  10932. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  10933. (remove-text-properties b e '(org-cwidth t invisible t
  10934. display t intangible t))
  10935. (if (and (boundp 'font-lock-mode) font-lock-mode)
  10936. (font-lock-fontify-block)))
  10937. (let ((pos (move-marker (make-marker) (point)))
  10938. (field (org-table-get-field))
  10939. (cw (current-window-configuration))
  10940. p)
  10941. (switch-to-buffer-other-window "*Org tmp*")
  10942. (erase-buffer)
  10943. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  10944. (org-mode)
  10945. (goto-char (setq p (point-max)))
  10946. (insert (org-trim field))
  10947. (remove-text-properties p (point-max)
  10948. '(invisible t org-cwidth t display t
  10949. intangible t))
  10950. (goto-char p)
  10951. (org-set-local 'org-finish-function
  10952. 'org-table-finish-edit-field)
  10953. (org-set-local 'org-window-configuration cw)
  10954. (org-set-local 'org-field-marker pos)
  10955. (message "Edit and finish with C-c C-c"))))
  10956. (defun org-table-finish-edit-field ()
  10957. "Finish editing a table data field.
  10958. Remove all newline characters, insert the result into the table, realign
  10959. the table and kill the editing buffer."
  10960. (let ((pos org-field-marker)
  10961. (cw org-window-configuration)
  10962. (cb (current-buffer))
  10963. text)
  10964. (goto-char (point-min))
  10965. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  10966. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  10967. (replace-match " "))
  10968. (setq text (org-trim (buffer-string)))
  10969. (set-window-configuration cw)
  10970. (kill-buffer cb)
  10971. (select-window (get-buffer-window (marker-buffer pos)))
  10972. (goto-char pos)
  10973. (move-marker pos nil)
  10974. (org-table-check-inside-data-field)
  10975. (org-table-get-field nil text)
  10976. (org-table-align)
  10977. (message "New field value inserted")))
  10978. (defun org-trim (s)
  10979. "Remove whitespace at beginning and end of string."
  10980. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  10981. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  10982. s)
  10983. (defun org-wrap (string &optional width lines)
  10984. "Wrap string to either a number of lines, or a width in characters.
  10985. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  10986. that costs. If there is a word longer than WIDTH, the text is actually
  10987. wrapped to the length of that word.
  10988. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  10989. many lines, whatever width that takes.
  10990. The return value is a list of lines, without newlines at the end."
  10991. (let* ((words (org-split-string string "[ \t\n]+"))
  10992. (maxword (apply 'max (mapcar 'org-string-width words)))
  10993. w ll)
  10994. (cond (width
  10995. (org-do-wrap words (max maxword width)))
  10996. (lines
  10997. (setq w maxword)
  10998. (setq ll (org-do-wrap words maxword))
  10999. (if (<= (length ll) lines)
  11000. ll
  11001. (setq ll words)
  11002. (while (> (length ll) lines)
  11003. (setq w (1+ w))
  11004. (setq ll (org-do-wrap words w)))
  11005. ll))
  11006. (t (error "Cannot wrap this")))))
  11007. (defun org-do-wrap (words width)
  11008. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11009. (let (lines line)
  11010. (while words
  11011. (setq line (pop words))
  11012. (while (and words (< (+ (length line) (length (car words))) width))
  11013. (setq line (concat line " " (pop words))))
  11014. (setq lines (push line lines)))
  11015. (nreverse lines)))
  11016. (defun org-split-string (string &optional separators)
  11017. "Splits STRING into substrings at SEPARATORS.
  11018. No empty strings are returned if there are matches at the beginning
  11019. and end of string."
  11020. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11021. (start 0)
  11022. notfirst
  11023. (list nil))
  11024. (while (and (string-match rexp string
  11025. (if (and notfirst
  11026. (= start (match-beginning 0))
  11027. (< start (length string)))
  11028. (1+ start) start))
  11029. (< (match-beginning 0) (length string)))
  11030. (setq notfirst t)
  11031. (or (eq (match-beginning 0) 0)
  11032. (and (eq (match-beginning 0) (match-end 0))
  11033. (eq (match-beginning 0) start))
  11034. (setq list
  11035. (cons (substring string start (match-beginning 0))
  11036. list)))
  11037. (setq start (match-end 0)))
  11038. (or (eq start (length string))
  11039. (setq list
  11040. (cons (substring string start)
  11041. list)))
  11042. (nreverse list)))
  11043. (defun org-table-map-tables (function)
  11044. "Apply FUNCTION to the start of all tables in the buffer."
  11045. (save-excursion
  11046. (save-restriction
  11047. (widen)
  11048. (goto-char (point-min))
  11049. (while (re-search-forward org-table-any-line-regexp nil t)
  11050. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  11051. (beginning-of-line 1)
  11052. (if (looking-at org-table-line-regexp)
  11053. (save-excursion (funcall function)))
  11054. (re-search-forward org-table-any-border-regexp nil 1))))
  11055. (message "Mapping tables: done"))
  11056. (defun org-table-sum (&optional beg end nlast)
  11057. "Sum numbers in region of current table column.
  11058. The result will be displayed in the echo area, and will be available
  11059. as kill to be inserted with \\[yank].
  11060. If there is an active region, it is interpreted as a rectangle and all
  11061. numbers in that rectangle will be summed. If there is no active
  11062. region and point is located in a table column, sum all numbers in that
  11063. column.
  11064. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  11065. numbers are assumed to be times as well (in decimal hours) and the
  11066. numbers are added as such.
  11067. If NLAST is a number, only the NLAST fields will actually be summed."
  11068. (interactive)
  11069. (save-excursion
  11070. (let (col (timecnt 0) diff h m s org-table-clip)
  11071. (cond
  11072. ((and beg end)) ; beg and end given explicitly
  11073. ((org-region-active-p)
  11074. (setq beg (region-beginning) end (region-end)))
  11075. (t
  11076. (setq col (org-table-current-column))
  11077. (goto-char (org-table-begin))
  11078. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  11079. (error "No table data"))
  11080. (org-table-goto-column col)
  11081. ;not needed? (skip-chars-backward "^|")
  11082. (setq beg (point))
  11083. (goto-char (org-table-end))
  11084. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  11085. (error "No table data"))
  11086. (org-table-goto-column col)
  11087. ;not needed? (skip-chars-forward "^|")
  11088. (setq end (point))))
  11089. (let* ((items (apply 'append (org-table-copy-region beg end)))
  11090. (items1 (cond ((not nlast) items)
  11091. ((>= nlast (length items)) items)
  11092. (t (setq items (reverse items))
  11093. (setcdr (nthcdr (1- nlast) items) nil)
  11094. (nreverse items))))
  11095. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  11096. items1)))
  11097. (res (apply '+ numbers))
  11098. (sres (if (= timecnt 0)
  11099. (format "%g" res)
  11100. (setq diff (* 3600 res)
  11101. h (floor (/ diff 3600)) diff (mod diff 3600)
  11102. m (floor (/ diff 60)) diff (mod diff 60)
  11103. s diff)
  11104. (format "%d:%02d:%02d" h m s))))
  11105. (kill-new sres)
  11106. (if (interactive-p)
  11107. (message "%s"
  11108. (substitute-command-keys
  11109. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  11110. (length numbers) sres))))
  11111. sres))))
  11112. (defun org-table-get-number-for-summing (s)
  11113. (let (n)
  11114. (if (string-match "^ *|? *" s)
  11115. (setq s (replace-match "" nil nil s)))
  11116. (if (string-match " *|? *$" s)
  11117. (setq s (replace-match "" nil nil s)))
  11118. (setq n (string-to-number s))
  11119. (cond
  11120. ((and (string-match "0" s)
  11121. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  11122. ((string-match "\\`[ \t]+\\'" s) nil)
  11123. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  11124. (let ((h (string-to-number (or (match-string 1 s) "0")))
  11125. (m (string-to-number (or (match-string 2 s) "0")))
  11126. (s (string-to-number (or (match-string 4 s) "0"))))
  11127. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  11128. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  11129. ((equal n 0) nil)
  11130. (t n))))
  11131. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  11132. "Get a calc vector from a column, accorting to descriptor DESC.
  11133. Optional arguments TBEG and COL can give the beginning of the table and
  11134. the current column, to avoid unnecessary parsing."
  11135. (save-excursion
  11136. (or tbeg (setq tbeg (org-table-begin)))
  11137. (or col (setq col (org-table-current-column)))
  11138. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  11139. (cond
  11140. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  11141. (setq n1 (- (match-end 1) (match-beginning 1)))
  11142. (if (match-beginning 3)
  11143. (setq n2 (- (match-end 2) (match-beginning 3))))
  11144. (setq n (if n2 (max n1 n2) n1))
  11145. (setq n1 (if n2 (min n1 n2)))
  11146. (setq nn n)
  11147. (while (and (> nn 0)
  11148. (re-search-backward org-table-hline-regexp tbeg t))
  11149. (push (org-current-line) hline-list)
  11150. (setq nn (1- nn)))
  11151. (setq hline-list (nreverse hline-list))
  11152. (goto-line (nth (1- n) hline-list))
  11153. (when (re-search-forward org-table-dataline-regexp)
  11154. (org-table-goto-column col)
  11155. (setq beg (point)))
  11156. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  11157. (when (re-search-backward org-table-dataline-regexp)
  11158. (org-table-goto-column col)
  11159. (setq end (point)))
  11160. (setq l (apply 'append (org-table-copy-region beg end)))
  11161. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  11162. (if (equal x "") "0" x))
  11163. l ",") "]"))
  11164. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  11165. (setq n1 (string-to-number (match-string 1 desc))
  11166. n2 (string-to-number (match-string 2 desc)))
  11167. (beginning-of-line 1)
  11168. (save-excursion
  11169. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  11170. (org-table-goto-column col)
  11171. (setq beg (point))))
  11172. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  11173. (org-table-goto-column col)
  11174. (setq end (point)))
  11175. (setq l (apply 'append (org-table-copy-region beg end)))
  11176. (concat "[" (mapconcat
  11177. (lambda (x) (setq x (org-trim x))
  11178. (if (equal x "") "0" x))
  11179. l ",") "]"))
  11180. ((string-match "\\([0-9]+\\)" desc)
  11181. (beginning-of-line 1)
  11182. (when (re-search-backward org-table-dataline-regexp tbeg t
  11183. (string-to-number (match-string 0 desc)))
  11184. (org-table-goto-column col)
  11185. (org-trim (org-table-get-field))))))))
  11186. (defvar org-table-formula-history nil)
  11187. (defvar org-table-column-names nil
  11188. "Alist with column names, derived from the `!' line.")
  11189. (defvar org-table-column-name-regexp nil
  11190. "Regular expression matching the current column names.")
  11191. (defvar org-table-local-parameters nil
  11192. "Alist with parameter names, derived from the `$' line.")
  11193. (defvar org-table-named-field-locations nil
  11194. "Alist with locations of named fields.")
  11195. (defun org-table-get-formula (&optional equation named)
  11196. "Read a formula from the minibuffer, offer stored formula as default."
  11197. (let* ((name (car (rassoc (list (org-current-line)
  11198. (org-table-current-column))
  11199. org-table-named-field-locations)))
  11200. (scol (if named
  11201. (if name name
  11202. (error "Not in a named field"))
  11203. (int-to-string (org-table-current-column))))
  11204. (dummy (and name (not named)
  11205. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  11206. (error "Abort")))
  11207. (org-table-may-need-update nil)
  11208. (stored-list (org-table-get-stored-formulas))
  11209. (stored (cdr (assoc scol stored-list)))
  11210. (eq (cond
  11211. ((and stored equation (string-match "^ *=? *$" equation))
  11212. stored)
  11213. ((stringp equation)
  11214. equation)
  11215. (t (read-string
  11216. (format "%s formula $%s=" (if named "Field" "Column") scol)
  11217. (or stored "") 'org-table-formula-history
  11218. ;stored
  11219. ))))
  11220. mustsave)
  11221. (when (not (string-match "\\S-" eq))
  11222. ;; remove formula
  11223. (setq stored-list (delq (assoc scol stored-list) stored-list))
  11224. (org-table-store-formulas stored-list)
  11225. (error "Formula removed"))
  11226. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  11227. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  11228. (if (and name (not named))
  11229. ;; We set the column equation, delete the named one.
  11230. (setq stored-list (delq (assoc name stored-list) stored-list)
  11231. mustsave t))
  11232. (if stored
  11233. (setcdr (assoc scol stored-list) eq)
  11234. (setq stored-list (cons (cons scol eq) stored-list)))
  11235. (if (or mustsave (not (equal stored eq)))
  11236. (org-table-store-formulas stored-list))
  11237. eq))
  11238. (defun org-table-store-formulas (alist)
  11239. "Store the list of formulas below the current table."
  11240. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  11241. (save-excursion
  11242. (goto-char (org-table-end))
  11243. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  11244. (delete-region (point) (match-end 0)))
  11245. (insert "#+TBLFM: "
  11246. (mapconcat (lambda (x)
  11247. (concat "$" (car x) "=" (cdr x)))
  11248. alist "::")
  11249. "\n")))
  11250. (defun org-table-get-stored-formulas ()
  11251. "Return an alist with the stored formulas directly after current table."
  11252. (interactive)
  11253. (let (scol eq eq-alist strings string seen)
  11254. (save-excursion
  11255. (goto-char (org-table-end))
  11256. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  11257. (setq strings (org-split-string (match-string 2) " *:: *"))
  11258. (while (setq string (pop strings))
  11259. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  11260. (setq scol (match-string 1 string)
  11261. eq (match-string 2 string)
  11262. eq-alist (cons (cons scol eq) eq-alist))
  11263. (if (member scol seen)
  11264. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  11265. (push scol seen))))))
  11266. (nreverse eq-alist)))
  11267. (defun org-table-modify-formulas (action &rest columns)
  11268. "Modify the formulas stored below the current table.
  11269. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  11270. expected, for the other actions only a single column number is needed."
  11271. (let ((list (org-table-get-stored-formulas))
  11272. (nmax (length (org-split-string
  11273. (buffer-substring (point-at-bol) (point-at-eol))
  11274. "|")))
  11275. col col1 col2 scol si sc1 sc2)
  11276. (cond
  11277. ((null list)) ; No action needed if there are no stored formulas
  11278. ((eq action 'remove)
  11279. (setq col (car columns)
  11280. scol (int-to-string col))
  11281. (org-table-replace-in-formulas list scol "INVALID")
  11282. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  11283. (loop for i from (1+ col) upto nmax by 1 do
  11284. (setq si (int-to-string i))
  11285. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  11286. (if (assoc si list) (setcar (assoc si list)
  11287. (int-to-string (1- i))))))
  11288. ((eq action 'insert)
  11289. (setq col (car columns))
  11290. (loop for i from nmax downto col by 1 do
  11291. (setq si (int-to-string i))
  11292. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  11293. (if (assoc si list) (setcar (assoc si list)
  11294. (int-to-string (1+ i))))))
  11295. ((eq action 'swap)
  11296. (setq col1 (car columns) col2 (nth 1 columns)
  11297. sc1 (int-to-string col1) sc2 (int-to-string col2))
  11298. ;; Hopefully, ZqZtZ will never be a name in a table
  11299. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  11300. (org-table-replace-in-formulas list sc2 sc1)
  11301. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  11302. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  11303. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  11304. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  11305. (t (error "Invalid action in `org-table-modify-formulas'")))
  11306. (if list (org-table-store-formulas list))))
  11307. (defun org-table-replace-in-formulas (list s1 s2)
  11308. (let (elt re s)
  11309. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  11310. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  11311. re (concat (regexp-quote s1) "\\>"))
  11312. (while (setq elt (pop list))
  11313. (setq s (cdr elt))
  11314. (while (string-match re s)
  11315. (setq s (replace-match s2 t t s)))
  11316. (setcdr elt s))))
  11317. (defun org-table-get-specials ()
  11318. "Get the column names and local parameters for this table."
  11319. (save-excursion
  11320. (let ((beg (org-table-begin)) (end (org-table-end))
  11321. names name fields fields1 field cnt c v line col)
  11322. (setq org-table-column-names nil
  11323. org-table-local-parameters nil
  11324. org-table-named-field-locations nil)
  11325. (goto-char beg)
  11326. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  11327. (setq names (org-split-string (match-string 1) " *| *")
  11328. cnt 1)
  11329. (while (setq name (pop names))
  11330. (setq cnt (1+ cnt))
  11331. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  11332. (push (cons name (int-to-string cnt)) org-table-column-names))))
  11333. (setq org-table-column-names (nreverse org-table-column-names))
  11334. (setq org-table-column-name-regexp
  11335. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  11336. (goto-char beg)
  11337. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  11338. (setq fields (org-split-string (match-string 1) " *| *"))
  11339. (while (setq field (pop fields))
  11340. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  11341. (push (cons (match-string 1 field) (match-string 2 field))
  11342. org-table-local-parameters))))
  11343. (goto-char beg)
  11344. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  11345. (setq c (match-string 1)
  11346. fields (org-split-string (match-string 2) " *| *"))
  11347. (save-excursion
  11348. (beginning-of-line (if (equal c "_") 2 0))
  11349. (setq line (org-current-line) col 1)
  11350. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  11351. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  11352. (while (and fields1 (setq field (pop fields)))
  11353. (setq v (pop fields1) col (1+ col))
  11354. (when (and (stringp field) (stringp v)
  11355. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  11356. (push (cons field v) org-table-local-parameters)
  11357. (push (list field line col) org-table-named-field-locations)))))))
  11358. (defun org-this-word ()
  11359. ;; Get the current word
  11360. (save-excursion
  11361. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  11362. (end (progn (skip-chars-forward "^ \t\n") (point))))
  11363. (buffer-substring-no-properties beg end))))
  11364. (defun org-table-maybe-eval-formula ()
  11365. "Check if the current field starts with \"=\" or \":=\".
  11366. If yes, store the formula and apply it."
  11367. ;; We already know we are in a table. Get field will only return a formula
  11368. ;; when appropriate. It might return a separator line, but no problem.
  11369. (when org-table-formula-evaluate-inline
  11370. (let* ((field (org-trim (or (org-table-get-field) "")))
  11371. named eq)
  11372. (when (string-match "^:?=\\(.*\\)" field)
  11373. (setq named (equal (string-to-char field) ?:)
  11374. eq (match-string 1 field))
  11375. (if (fboundp 'calc-eval)
  11376. (org-table-eval-formula (if named '(4) nil) eq))))))
  11377. (defvar org-recalc-commands nil
  11378. "List of commands triggering the recalculation of a line.
  11379. Will be filled automatically during use.")
  11380. (defvar org-recalc-marks
  11381. '((" " . "Unmarked: no special line, no automatic recalculation")
  11382. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  11383. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  11384. ("!" . "Column name definition line. Reference in formula as $name.")
  11385. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  11386. ("_" . "Names for values in row below this one.")
  11387. ("^" . "Names for values in row above this one.")))
  11388. (defun org-table-rotate-recalc-marks (&optional newchar)
  11389. "Rotate the recalculation mark in the first column.
  11390. If in any row, the first field is not consistent with a mark,
  11391. insert a new column for the markers.
  11392. When there is an active region, change all the lines in the region,
  11393. after prompting for the marking character.
  11394. After each change, a message will be displayed indicating the meaning
  11395. of the new mark."
  11396. (interactive)
  11397. (unless (org-at-table-p) (error "Not at a table"))
  11398. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  11399. (beg (org-table-begin))
  11400. (end (org-table-end))
  11401. (l (org-current-line))
  11402. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  11403. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  11404. (have-col
  11405. (save-excursion
  11406. (goto-char beg)
  11407. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  11408. (col (org-table-current-column))
  11409. (forcenew (car (assoc newchar org-recalc-marks)))
  11410. epos new)
  11411. (when l1
  11412. (message "Change region to what mark? Type # * ! $ or SPC: ")
  11413. (setq newchar (char-to-string (read-char-exclusive))
  11414. forcenew (car (assoc newchar org-recalc-marks))))
  11415. (if (and newchar (not forcenew))
  11416. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  11417. newchar))
  11418. (if l1 (goto-line l1))
  11419. (save-excursion
  11420. (beginning-of-line 1)
  11421. (unless (looking-at org-table-dataline-regexp)
  11422. (error "Not at a table data line")))
  11423. (unless have-col
  11424. (org-table-goto-column 1)
  11425. (org-table-insert-column)
  11426. (org-table-goto-column (1+ col)))
  11427. (setq epos (point-at-eol))
  11428. (save-excursion
  11429. (beginning-of-line 1)
  11430. (org-table-get-field
  11431. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  11432. (concat " "
  11433. (setq new (or forcenew
  11434. (cadr (member (match-string 1) marks))))
  11435. " ")
  11436. " # ")))
  11437. (if (and l1 l2)
  11438. (progn
  11439. (goto-line l1)
  11440. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  11441. (and (looking-at org-table-dataline-regexp)
  11442. (org-table-get-field 1 (concat " " new " "))))
  11443. (goto-line l1)))
  11444. (if (not (= epos (point-at-eol))) (org-table-align))
  11445. (goto-line l)
  11446. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  11447. (defun org-table-maybe-recalculate-line ()
  11448. "Recompute the current line if marked for it, and if we haven't just done it."
  11449. (interactive)
  11450. (and org-table-allow-automatic-line-recalculation
  11451. (not (and (memq last-command org-recalc-commands)
  11452. (equal org-last-recalc-line (org-current-line))))
  11453. (save-excursion (beginning-of-line 1)
  11454. (looking-at org-table-auto-recalculate-regexp))
  11455. (fboundp 'calc-eval)
  11456. (org-table-recalculate) t))
  11457. (defvar org-table-formula-debug nil
  11458. "Non-nil means, debug table formulas.
  11459. When nil, simply write \"#ERROR\" in corrupted fields.")
  11460. (defvar modes)
  11461. (defsubst org-set-calc-mode (var &optional value)
  11462. (if (stringp var)
  11463. (setq var (assoc var '(("D" calc-angle-mode deg)
  11464. ("R" calc-angle-mode rad)
  11465. ("F" calc-prefer-frac t)
  11466. ("S" calc-symbolic-mode t)))
  11467. value (nth 2 var) var (nth 1 var)))
  11468. (if (memq var modes)
  11469. (setcar (cdr (memq var modes)) value)
  11470. (cons var (cons value modes)))
  11471. modes)
  11472. (defun org-table-eval-formula (&optional arg equation
  11473. suppress-align suppress-const
  11474. suppress-store)
  11475. "Replace the table field value at the cursor by the result of a calculation.
  11476. This function makes use of Dave Gillespie's Calc package, in my view the
  11477. most exciting program ever written for GNU Emacs. So you need to have Calc
  11478. installed in order to use this function.
  11479. In a table, this command replaces the value in the current field with the
  11480. result of a formula. It also installs the formula as the \"current\" column
  11481. formula, by storing it in a special line below the table. When called
  11482. with a `C-u' prefix, the current field must ba a named field, and the
  11483. formula is installed as valid in only this specific field.
  11484. When called, the command first prompts for a formula, which is read in
  11485. the minibuffer. Previously entered formulas are available through the
  11486. history list, and the last used formula is offered as a default.
  11487. These stored formulas are adapted correctly when moving, inserting, or
  11488. deleting columns with the corresponding commands.
  11489. The formula can be any algebraic expression understood by the Calc package.
  11490. For details, see the Org-mode manual.
  11491. This function can also be called from Lisp programs and offers
  11492. additional arguments: EQUATION can be the formula to apply. If this
  11493. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  11494. used to speed-up recursive calls by by-passing unnecessary aligns.
  11495. SUPPRESS-CONST suppresses the interpretation of constants in the
  11496. formula, assuming that this has been done already outside the function.
  11497. SUPPRESS-STORE means the formula should not be stored, either because
  11498. it is already stored, or because it is a modified equation that should
  11499. not overwrite the stored one."
  11500. (interactive "P")
  11501. (require 'calc)
  11502. (org-table-check-inside-data-field)
  11503. (org-table-get-specials)
  11504. (let* (fields
  11505. (ndown (if (integerp arg) arg 1))
  11506. (org-table-automatic-realign nil)
  11507. (case-fold-search nil)
  11508. (down (> ndown 1))
  11509. (formula (if (and equation suppress-store)
  11510. equation
  11511. (org-table-get-formula equation (equal arg '(4)))))
  11512. (n0 (org-table-current-column))
  11513. (modes (copy-sequence org-calc-default-modes))
  11514. n form fmt x ev orig c lispp)
  11515. ;; Parse the format string. Since we have a lot of modes, this is
  11516. ;; a lot of work. However, I think calc still uses most of the time.
  11517. (if (string-match ";" formula)
  11518. (let ((tmp (org-split-string formula ";")))
  11519. (setq formula (car tmp)
  11520. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  11521. (nth 1 tmp)))
  11522. (while (string-match "\\([pnfse]\\)\\(-?[0-9]+\\)" fmt)
  11523. (setq c (string-to-char (match-string 1 fmt))
  11524. n (string-to-number (match-string 2 fmt)))
  11525. (if (= c ?p)
  11526. (setq modes (org-set-calc-mode 'calc-internal-prec n))
  11527. (setq modes (org-set-calc-mode
  11528. 'calc-float-format
  11529. (list (cdr (assoc c '((?n . float) (?f . fix)
  11530. (?s . sci) (?e . eng))))
  11531. n))))
  11532. (setq fmt (replace-match "" t t fmt)))
  11533. (while (string-match "[DRFS]" fmt)
  11534. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  11535. (setq fmt (replace-match "" t t fmt)))
  11536. (unless (string-match "\\S-" fmt)
  11537. (setq fmt nil))))
  11538. (if (and (not suppress-const) org-table-formula-use-constants)
  11539. (setq formula (org-table-formula-substitute-names formula)))
  11540. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  11541. (while (> ndown 0)
  11542. (setq fields (org-split-string
  11543. (buffer-substring
  11544. (point-at-bol) (point-at-eol)) " *| *"))
  11545. (if org-table-formula-numbers-only
  11546. (setq fields (mapcar
  11547. (lambda (x) (number-to-string (string-to-number x)))
  11548. fields)))
  11549. (setq ndown (1- ndown))
  11550. (setq form (copy-sequence formula)
  11551. lispp (equal (substring form 0 2) "'("))
  11552. ;; Insert the references to fields in same row
  11553. (while (string-match "\\$\\([0-9]+\\)?" form)
  11554. (setq n (if (match-beginning 1)
  11555. (string-to-number (match-string 1 form))
  11556. n0)
  11557. x (nth (1- n) fields))
  11558. (unless x (error "Invalid field specifier \"%s\""
  11559. (match-string 0 form)))
  11560. (if (equal x "") (setq x "0"))
  11561. (setq form (replace-match
  11562. (if lispp x (concat "(" x ")"))
  11563. t t form)))
  11564. ;; Insert ranges in current column
  11565. (while (string-match "\\&[-I0-9]+" form)
  11566. (setq form (replace-match
  11567. (save-match-data
  11568. (org-table-get-vertical-vector (match-string 0 form)
  11569. nil n0))
  11570. t t form)))
  11571. (if lispp
  11572. (setq ev (eval (eval (read form)))
  11573. ev (if (numberp ev) (number-to-string ev) ev))
  11574. (setq ev (calc-eval (cons form modes)
  11575. (if org-table-formula-numbers-only 'num))))
  11576. (when org-table-formula-debug
  11577. (with-output-to-temp-buffer "*Help*"
  11578. (princ (format "Substitution history of formula
  11579. Orig: %s
  11580. $xyz-> %s
  11581. $1-> %s\n" orig formula form))
  11582. (if (listp ev)
  11583. (princ (format " %s^\nError: %s"
  11584. (make-string (car ev) ?\-) (nth 1 ev)))
  11585. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  11586. ev (or fmt "NONE")
  11587. (if fmt (format fmt (string-to-number ev)) ev)))))
  11588. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  11589. (unless (and (interactive-p) (not ndown))
  11590. (unless (let (inhibit-redisplay)
  11591. (y-or-n-p "Debugging Formula. Continue to next? "))
  11592. (org-table-align)
  11593. (error "Abort"))
  11594. (delete-window (get-buffer-window "*Help*"))
  11595. (message "")))
  11596. (if (listp ev) (setq fmt nil ev "#ERROR"))
  11597. (org-table-justify-field-maybe
  11598. (if fmt (format fmt (string-to-number ev)) ev))
  11599. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  11600. (call-interactively 'org-return)
  11601. (setq ndown 0)))
  11602. (and down (org-table-maybe-recalculate-line))
  11603. (or suppress-align (and org-table-may-need-update
  11604. (org-table-align)))))
  11605. (defun org-table-recalculate (&optional all noalign)
  11606. "Recalculate the current table line by applying all stored formulas.
  11607. With prefix arg ALL, do this for all lines in the table."
  11608. (interactive "P")
  11609. (or (memq this-command org-recalc-commands)
  11610. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  11611. (unless (org-at-table-p) (error "Not at a table"))
  11612. (org-table-get-specials)
  11613. (let* ((eqlist (sort (org-table-get-stored-formulas)
  11614. (lambda (a b) (string< (car a) (car b)))))
  11615. (inhibit-redisplay t)
  11616. (line-re org-table-dataline-regexp)
  11617. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  11618. (thiscol (org-table-current-column))
  11619. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  11620. ;; Insert constants in all formulas
  11621. (setq eqlist
  11622. (mapcar (lambda (x)
  11623. (setcdr x (org-table-formula-substitute-names (cdr x)))
  11624. x)
  11625. eqlist))
  11626. ;; Split the equation list
  11627. (while (setq eq (pop eqlist))
  11628. (if (<= (string-to-char (car eq)) ?9)
  11629. (push eq eqlnum)
  11630. (push eq eqlname)))
  11631. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  11632. (if all
  11633. (progn
  11634. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  11635. (goto-char (setq beg (org-table-begin)))
  11636. (if (re-search-forward org-table-calculate-mark-regexp end t)
  11637. ;; This is a table with marked lines, only compute selected lines
  11638. (setq line-re org-table-recalculate-regexp)
  11639. ;; Move forward to the first non-header line
  11640. (if (and (re-search-forward org-table-dataline-regexp end t)
  11641. (re-search-forward org-table-hline-regexp end t)
  11642. (re-search-forward org-table-dataline-regexp end t))
  11643. (setq beg (match-beginning 0))
  11644. nil))) ;; just leave beg where it is
  11645. (setq beg (point-at-bol)
  11646. end (move-marker (make-marker) (1+ (point-at-eol)))))
  11647. (goto-char beg)
  11648. (and all (message "Re-applying formulas to full table..."))
  11649. (while (re-search-forward line-re end t)
  11650. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  11651. ;; Unprotected line, recalculate
  11652. (and all (message "Re-applying formulas to full table...(line %d)"
  11653. (setq cnt (1+ cnt))))
  11654. (setq org-last-recalc-line (org-current-line))
  11655. (setq eql eqlnum)
  11656. (while (setq entry (pop eql))
  11657. (goto-line org-last-recalc-line)
  11658. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  11659. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  11660. (goto-line thisline)
  11661. (org-table-goto-column thiscol)
  11662. (or noalign (and org-table-may-need-update (org-table-align))
  11663. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  11664. ;; Now do the names fields
  11665. (while (setq eq (pop eqlname))
  11666. (setq name (car eq)
  11667. a (assoc name org-table-named-field-locations))
  11668. (when a
  11669. (message "Re-applying formula to named field: %s" name)
  11670. (goto-line (nth 1 a))
  11671. (org-table-goto-column (nth 2 a))
  11672. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  11673. ;; back to initial position
  11674. (goto-line thisline)
  11675. (org-table-goto-column thiscol)
  11676. (or noalign (and org-table-may-need-update (org-table-align))
  11677. (and all (message "Re-applying formulas...done")))))
  11678. (defun org-table-formula-substitute-names (f)
  11679. "Replace $const with values in string F."
  11680. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  11681. ;; First, check for column names
  11682. (while (setq start (string-match org-table-column-name-regexp f start))
  11683. (setq start (1+ start))
  11684. (setq a (assoc (match-string 1 f) org-table-column-names))
  11685. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  11686. ;; Expand ranges to vectors
  11687. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  11688. (setq n1 (string-to-number (match-string 1 f))
  11689. n2 (string-to-number (match-string 2 f))
  11690. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  11691. s (concat "[($" (number-to-string (1- nn1)) ")"))
  11692. (loop for i from nn1 upto nn2 do
  11693. (setq s (concat s ",($" (int-to-string i) ")")))
  11694. (setq s (concat s "]"))
  11695. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  11696. (setq f (replace-match s t t f)))
  11697. ;; Parameters and constants
  11698. (setq start 0)
  11699. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  11700. (setq start (1+ start))
  11701. (if (setq a (save-match-data
  11702. (org-table-get-constant (match-string 1 f))))
  11703. (setq f (replace-match (concat "(" a ")") t t f))))
  11704. (if org-table-formula-debug
  11705. (put-text-property 0 (length f) :orig-formula f1 f))
  11706. f))
  11707. (defun org-table-get-constant (const)
  11708. "Find the value for a parameter or constant in a formula.
  11709. Parameters get priority."
  11710. (or (cdr (assoc const org-table-local-parameters))
  11711. (cdr (assoc const org-table-formula-constants))
  11712. (and (fboundp 'constants-get) (constants-get const))
  11713. "#UNDEFINED_NAME"))
  11714. (defvar org-edit-formulas-map (make-sparse-keymap))
  11715. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  11716. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  11717. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  11718. (defvar org-pos)
  11719. (defun org-table-edit-formulas ()
  11720. "Edit the formulas of the current table in a separate buffer."
  11721. (interactive)
  11722. (unless (org-at-table-p)
  11723. (error "Not at a table"))
  11724. (org-table-get-specials)
  11725. (let ((eql (org-table-get-stored-formulas))
  11726. (pos (move-marker (make-marker) (point)))
  11727. (wc (current-window-configuration))
  11728. entry loc s)
  11729. (switch-to-buffer-other-window "*Edit Formulas*")
  11730. (erase-buffer)
  11731. (fundamental-mode)
  11732. (org-set-local 'org-pos pos)
  11733. (org-set-local 'org-window-configuration wc)
  11734. (use-local-map org-edit-formulas-map)
  11735. (setq s "# Edit formulas and finish with `C-c C-c'.
  11736. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  11737. # Use `C-c ?' to get information about $name at point.
  11738. # To cancel editing, press `C-c C-q'.\n")
  11739. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11740. (insert s)
  11741. (while (setq entry (pop eql))
  11742. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  11743. (setq s (format "# Named formula, referring to column %d in line %d\n"
  11744. (nth 2 loc) (nth 1 loc)))
  11745. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  11746. (insert s))
  11747. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  11748. (remove-text-properties 0 (length s) '(face nil) s)
  11749. (insert s))
  11750. (goto-char (point-min))
  11751. (message "Edit formulas and finish with `C-c C-c'.")))
  11752. (defun org-show-variable ()
  11753. "Show the location/value of the $ expression at point."
  11754. (interactive)
  11755. (let (var (pos org-pos) (win (selected-window)) e)
  11756. (save-excursion
  11757. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  11758. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  11759. (setq var (match-string 1))
  11760. (error "No variable at point")))
  11761. (cond
  11762. ((setq e (assoc var org-table-named-field-locations))
  11763. (switch-to-buffer-other-window (marker-buffer pos))
  11764. (goto-line (nth 1 e))
  11765. (org-table-goto-column (nth 2 e))
  11766. (select-window win)
  11767. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  11768. ((setq e (assoc var org-table-column-names))
  11769. (switch-to-buffer-other-window (marker-buffer pos))
  11770. (goto-char pos)
  11771. (goto-char (org-table-begin))
  11772. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  11773. (org-table-end) t)
  11774. (progn
  11775. (goto-char (match-beginning 1))
  11776. (message "Named column (column %s)" (cdr e)))
  11777. (error "Column name not found"))
  11778. (select-window win))
  11779. ((string-match "^[0-9]$" var)
  11780. ;; column number
  11781. (switch-to-buffer-other-window (marker-buffer pos))
  11782. (goto-char pos)
  11783. (goto-char (org-table-begin))
  11784. (recenter 1)
  11785. (if (re-search-forward org-table-dataline-regexp
  11786. (org-table-end) t)
  11787. (progn
  11788. (goto-char (match-beginning 0))
  11789. (org-table-goto-column (string-to-number var))
  11790. (message "Column %s" var))
  11791. (error "Column name not found"))
  11792. (select-window win))
  11793. ((setq e (assoc var org-table-local-parameters))
  11794. (switch-to-buffer-other-window (marker-buffer pos))
  11795. (goto-char pos)
  11796. (goto-char (org-table-begin))
  11797. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  11798. (progn
  11799. (goto-char (match-beginning 1))
  11800. (message "Local parameter."))
  11801. (error "Parameter not found"))
  11802. (select-window win))
  11803. (t
  11804. (cond
  11805. ((setq e (assoc var org-table-formula-constants))
  11806. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  11807. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  11808. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  11809. (t (error "Undefined name $%s" var)))))))
  11810. (defun org-finish-edit-formulas (&optional arg)
  11811. "Parse the buffer for formula definitions and install them.
  11812. With prefix ARG, apply the new formulas to the table."
  11813. (interactive "P")
  11814. (let ((pos org-pos) eql)
  11815. (goto-char (point-min))
  11816. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  11817. (push (cons (match-string 1) (match-string 2)) eql))
  11818. (set-window-configuration org-window-configuration)
  11819. (select-window (get-buffer-window (marker-buffer pos)))
  11820. (goto-char pos)
  11821. (unless (org-at-table-p)
  11822. (error "Lost table position - cannot install formulae"))
  11823. (org-table-store-formulas eql)
  11824. (move-marker pos nil)
  11825. (kill-buffer "*Edit Formulas*")
  11826. (if arg
  11827. (org-table-recalculate 'all)
  11828. (message "New formulas installed - press C-u C-c C-c to apply."))))
  11829. (defun org-abort-edit-formulas ()
  11830. "Abort editing formulas, without installing the changes."
  11831. (interactive)
  11832. (let ((pos org-pos))
  11833. (set-window-configuration org-window-configuration)
  11834. (select-window (get-buffer-window (marker-buffer pos)))
  11835. (goto-char pos)
  11836. (message "Formula editing aborted without installing changes")))
  11837. ;;; The orgtbl minor mode
  11838. ;; Define a minor mode which can be used in other modes in order to
  11839. ;; integrate the org-mode table editor.
  11840. ;; This is really a hack, because the org-mode table editor uses several
  11841. ;; keys which normally belong to the major mode, for example the TAB and
  11842. ;; RET keys. Here is how it works: The minor mode defines all the keys
  11843. ;; necessary to operate the table editor, but wraps the commands into a
  11844. ;; function which tests if the cursor is currently inside a table. If that
  11845. ;; is the case, the table editor command is executed. However, when any of
  11846. ;; those keys is used outside a table, the function uses `key-binding' to
  11847. ;; look up if the key has an associated command in another currently active
  11848. ;; keymap (minor modes, major mode, global), and executes that command.
  11849. ;; There might be problems if any of the keys used by the table editor is
  11850. ;; otherwise used as a prefix key.
  11851. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  11852. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  11853. ;; addresses this by checking explicitly for both bindings.
  11854. ;; The optimized version (see variable `orgtbl-optimized') takes over
  11855. ;; all keys which are bound to `self-insert-command' in the *global map*.
  11856. ;; Some modes bind other commands to simple characters, for example
  11857. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  11858. ;; active, this binding is ignored inside tables and replaced with a
  11859. ;; modified self-insert.
  11860. (defvar orgtbl-mode nil
  11861. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  11862. table editor in arbitrary modes.")
  11863. (make-variable-buffer-local 'orgtbl-mode)
  11864. (defvar orgtbl-mode-map (make-keymap)
  11865. "Keymap for `orgtbl-mode'.")
  11866. ;;;###autoload
  11867. (defun turn-on-orgtbl ()
  11868. "Unconditionally turn on `orgtbl-mode'."
  11869. (orgtbl-mode 1))
  11870. ;;;###autoload
  11871. (defun orgtbl-mode (&optional arg)
  11872. "The `org-mode' table editor as a minor mode for use in other modes."
  11873. (interactive)
  11874. (if (org-mode-p)
  11875. ;; Exit without error, in case some hook functions calls this
  11876. ;; by accident in org-mode.
  11877. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  11878. (setq orgtbl-mode
  11879. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  11880. (if orgtbl-mode
  11881. (progn
  11882. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  11883. ;; Make sure we are first in minor-mode-map-alist
  11884. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  11885. (and c (setq minor-mode-map-alist
  11886. (cons c (delq c minor-mode-map-alist)))))
  11887. (org-set-local (quote org-table-may-need-update) t)
  11888. (org-add-hook 'before-change-functions 'org-before-change-function
  11889. nil 'local)
  11890. (org-set-local 'org-old-auto-fill-inhibit-regexp
  11891. auto-fill-inhibit-regexp)
  11892. (org-set-local 'auto-fill-inhibit-regexp
  11893. (if auto-fill-inhibit-regexp
  11894. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  11895. "[ \t]*|"))
  11896. (org-add-to-invisibility-spec '(org-cwidth))
  11897. (easy-menu-add orgtbl-mode-menu)
  11898. (run-hooks 'orgtbl-mode-hook))
  11899. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  11900. (org-cleanup-narrow-column-properties)
  11901. (org-remove-from-invisibility-spec '(org-cwidth))
  11902. (remove-hook 'before-change-functions 'org-before-change-function t)
  11903. (easy-menu-remove orgtbl-mode-menu)
  11904. (force-mode-line-update 'all))))
  11905. (defun org-cleanup-narrow-column-properties ()
  11906. "Remove all properties related to narrow-column invisibility."
  11907. (let ((s 1))
  11908. (while (setq s (text-property-any s (point-max)
  11909. 'display org-narrow-column-arrow))
  11910. (remove-text-properties s (1+ s) '(display t)))
  11911. (setq s 1)
  11912. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  11913. (remove-text-properties s (1+ s) '(org-cwidth t)))
  11914. (setq s 1)
  11915. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  11916. (remove-text-properties s (1+ s) '(invisible t)))))
  11917. ;; Install it as a minor mode.
  11918. (put 'orgtbl-mode :included t)
  11919. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  11920. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  11921. (defun orgtbl-make-binding (fun n &rest keys)
  11922. "Create a function for binding in the table minor mode.
  11923. FUN is the command to call inside a table. N is used to create a unique
  11924. command name. KEYS are keys that should be checked in for a command
  11925. to execute outside of tables."
  11926. (eval
  11927. (list 'defun
  11928. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  11929. '(arg)
  11930. (concat "In tables, run `" (symbol-name fun) "'.\n"
  11931. "Outside of tables, run the binding of `"
  11932. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  11933. "'.")
  11934. '(interactive "p")
  11935. (list 'if
  11936. '(org-at-table-p)
  11937. (list 'call-interactively (list 'quote fun))
  11938. (list 'let '(orgtbl-mode)
  11939. (list 'call-interactively
  11940. (append '(or)
  11941. (mapcar (lambda (k)
  11942. (list 'key-binding k))
  11943. keys)
  11944. '('orgtbl-error))))))))
  11945. (defun orgtbl-error ()
  11946. "Error when there is no default binding for a table key."
  11947. (interactive)
  11948. (error "This key is has no function outside tables"))
  11949. (defun orgtbl-setup ()
  11950. "Setup orgtbl keymaps."
  11951. (let ((nfunc 0)
  11952. (bindings
  11953. (list
  11954. '([(meta shift left)] org-table-delete-column)
  11955. '([(meta left)] org-table-move-column-left)
  11956. '([(meta right)] org-table-move-column-right)
  11957. '([(meta shift right)] org-table-insert-column)
  11958. '([(meta shift up)] org-table-kill-row)
  11959. '([(meta shift down)] org-table-insert-row)
  11960. '([(meta up)] org-table-move-row-up)
  11961. '([(meta down)] org-table-move-row-down)
  11962. '("\C-c\C-w" org-table-cut-region)
  11963. '("\C-c\M-w" org-table-copy-region)
  11964. '("\C-c\C-y" org-table-paste-rectangle)
  11965. '("\C-c-" org-table-insert-hline)
  11966. ; '([(shift tab)] org-table-previous-field)
  11967. '("\C-m" org-table-next-row)
  11968. (list (org-key 'S-return) 'org-table-copy-down)
  11969. '([(meta return)] org-table-wrap-region)
  11970. '("\C-c\C-q" org-table-wrap-region)
  11971. '("\C-c?" org-table-current-column)
  11972. '("\C-c " org-table-blank-field)
  11973. '("\C-c+" org-table-sum)
  11974. '("\C-c=" org-table-eval-formula)
  11975. '("\C-c'" org-table-edit-formulas)
  11976. '("\C-c`" org-table-edit-field)
  11977. '("\C-c*" org-table-recalculate)
  11978. '("\C-c|" org-table-create-or-convert-from-region)
  11979. '("\C-c^" org-table-sort-lines)
  11980. '([(control ?#)] org-table-rotate-recalc-marks)))
  11981. elt key fun cmd)
  11982. (while (setq elt (pop bindings))
  11983. (setq nfunc (1+ nfunc))
  11984. (setq key (car elt)
  11985. fun (nth 1 elt)
  11986. cmd (orgtbl-make-binding fun nfunc key))
  11987. (define-key orgtbl-mode-map key cmd))
  11988. ;; Special treatment needed for TAB and RET
  11989. (define-key orgtbl-mode-map [(return)]
  11990. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  11991. (define-key orgtbl-mode-map "\C-m"
  11992. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  11993. (define-key orgtbl-mode-map [(tab)]
  11994. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  11995. (define-key orgtbl-mode-map "\C-i"
  11996. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  11997. (define-key orgtbl-mode-map "\C-i"
  11998. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  11999. (define-key orgtbl-mode-map "\C-c\C-c"
  12000. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  12001. (when orgtbl-optimized
  12002. ;; If the user wants maximum table support, we need to hijack
  12003. ;; some standard editing functions
  12004. (org-remap orgtbl-mode-map
  12005. 'self-insert-command 'orgtbl-self-insert-command
  12006. 'delete-char 'org-delete-char
  12007. 'delete-backward-char 'org-delete-backward-char)
  12008. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  12009. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  12010. '("OrgTbl"
  12011. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  12012. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  12013. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  12014. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  12015. "--"
  12016. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  12017. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  12018. ["Copy Field from Above"
  12019. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  12020. "--"
  12021. ("Column"
  12022. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  12023. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  12024. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  12025. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  12026. "--"
  12027. ["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])
  12028. ("Row"
  12029. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  12030. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  12031. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  12032. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  12033. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  12034. "--"
  12035. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  12036. ("Rectangle"
  12037. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  12038. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  12039. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  12040. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  12041. "--"
  12042. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  12043. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12044. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  12045. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  12046. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  12047. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  12048. ["Sum Column/Rectangle" org-table-sum
  12049. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  12050. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  12051. ["Debug Formulas"
  12052. (setq org-table-formula-debug (not org-table-formula-debug))
  12053. :style toggle :selected org-table-formula-debug]
  12054. ))
  12055. t)
  12056. (defun orgtbl-tab (arg)
  12057. "Justification and field motion for `orgtbl-mode'."
  12058. (interactive "P")
  12059. (if arg (org-table-edit-field t)
  12060. (org-table-justify-field-maybe)
  12061. (org-table-next-field)))
  12062. (defun orgtbl-ret ()
  12063. "Justification and field motion for `orgtbl-mode'."
  12064. (interactive)
  12065. (org-table-justify-field-maybe)
  12066. (org-table-next-row))
  12067. (defun orgtbl-self-insert-command (N)
  12068. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12069. If the cursor is in a table looking at whitespace, the whitespace is
  12070. overwritten, and the table is not marked as requiring realignment."
  12071. (interactive "p")
  12072. (if (and (org-at-table-p)
  12073. (or
  12074. (and org-table-auto-blank-field
  12075. (member last-command
  12076. '(orgtbl-hijacker-command-100
  12077. orgtbl-hijacker-command-101
  12078. orgtbl-hijacker-command-102
  12079. orgtbl-hijacker-command-103
  12080. orgtbl-hijacker-command-104
  12081. orgtbl-hijacker-command-105))
  12082. (org-table-blank-field))
  12083. t)
  12084. (eq N 1)
  12085. (looking-at "[^|\n]* +|"))
  12086. (let (org-table-may-need-update)
  12087. (goto-char (1- (match-end 0)))
  12088. (delete-backward-char 1)
  12089. (goto-char (match-beginning 0))
  12090. (self-insert-command N))
  12091. (setq org-table-may-need-update t)
  12092. (let (orgtbl-mode)
  12093. (call-interactively (key-binding (vector last-input-event))))))
  12094. (defun org-force-self-insert (N)
  12095. "Needed to enforce self-insert under remapping."
  12096. (interactive "p")
  12097. (self-insert-command N))
  12098. ;;; Exporting
  12099. (defconst org-level-max 20)
  12100. (defvar org-export-html-preamble nil
  12101. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  12102. (defvar org-export-html-postamble nil
  12103. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  12104. (defvar org-export-html-auto-preamble t
  12105. "Should default preamble be inserted? Set by publishing functions.")
  12106. (defvar org-export-html-auto-postamble t
  12107. "Should default postamble be inserted? Set by publishing functions.")
  12108. (defconst org-export-plist-vars
  12109. '((:language . org-export-default-language)
  12110. (:headline-levels . org-export-headline-levels)
  12111. (:section-numbers . org-export-with-section-numbers)
  12112. (:table-of-contents . org-export-with-toc)
  12113. (:archived-trees . org-export-with-archived-trees)
  12114. (:emphasize . org-export-with-emphasize)
  12115. (:sub-superscript . org-export-with-sub-superscripts)
  12116. (:TeX-macros . org-export-with-TeX-macros)
  12117. (:LaTeX-fragments . org-export-with-LaTeX-fragments)
  12118. (:fixed-width . org-export-with-fixed-width)
  12119. (:timestamps . org-export-with-timestamps)
  12120. (:tables . org-export-with-tables)
  12121. (:table-auto-headline . org-export-highlight-first-table-line)
  12122. (:style . org-export-html-style)
  12123. (:convert-org-links . org-export-html-link-org-files-as-html)
  12124. (:inline-images . org-export-html-inline-images)
  12125. (:expand-quoted-html . org-export-html-expand)
  12126. (:timestamp . org-export-html-with-timestamp)
  12127. (:publishing-directory . org-export-publishing-directory)
  12128. (:preamble . org-export-html-preamble)
  12129. (:postamble . org-export-html-postamble)
  12130. (:auto-preamble . org-export-html-auto-preamble)
  12131. (:auto-postamble . org-export-html-auto-postamble)
  12132. (:author . user-full-name)
  12133. (:email . user-mail-address)))
  12134. (defun org-default-export-plist ()
  12135. "Return the property list with default settings for the export variables."
  12136. (let ((l org-export-plist-vars) rtn e)
  12137. (while (setq e (pop l))
  12138. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  12139. rtn))
  12140. (defun org-infile-export-plist ()
  12141. "Return the property list with file-local settings for export."
  12142. (save-excursion
  12143. (goto-char 0)
  12144. (let ((re (org-make-options-regexp
  12145. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  12146. p key val text options)
  12147. (while (re-search-forward re nil t)
  12148. (setq key (org-match-string-no-properties 1)
  12149. val (org-match-string-no-properties 2))
  12150. (cond
  12151. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  12152. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  12153. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  12154. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  12155. ((string-equal key "TEXT")
  12156. (setq text (if text (concat text "\n" val) val)))
  12157. ((string-equal key "OPTIONS") (setq options val))))
  12158. (setq p (plist-put p :text text))
  12159. (when options
  12160. (let ((op '(("H" . :headline-levels)
  12161. ("num" . :section-numbers)
  12162. ("toc" . :table-of-contents)
  12163. ("\\n" . :preserve-breaks)
  12164. ("@" . :expand-quoted-html)
  12165. (":" . :fixed-width)
  12166. ("|" . :tables)
  12167. ("^" . :sub-superscript)
  12168. ("*" . :emphasize)
  12169. ("TeX" . :TeX-macros)
  12170. ("LaTeX" . :LaTeX-fragments)))
  12171. o)
  12172. (while (setq o (pop op))
  12173. (if (string-match (concat (regexp-quote (car o))
  12174. ":\\([^ \t\n\r;,.]*\\)")
  12175. options)
  12176. (setq p (plist-put p (cdr o)
  12177. (car (read-from-string
  12178. (match-string 1 options)))))))))
  12179. p)))
  12180. (defun org-combine-plists (&rest plists)
  12181. "Create a single property list from all plists in PLISTS.
  12182. The process starts by copying the last list, and then setting properties
  12183. from the other lists. Settings in the first list are the most significant
  12184. ones and overrule settings in the other lists."
  12185. (let ((rtn (copy-sequence (pop plists)))
  12186. p v ls)
  12187. (while plists
  12188. (setq ls (pop plists))
  12189. (while ls
  12190. (setq p (pop ls) v (pop ls))
  12191. (setq rtn (plist-put rtn p v))))
  12192. rtn))
  12193. (defun org-export-directory (type plist)
  12194. (let* ((val (plist-get plist :publishing-directory))
  12195. (dir (if (listp val)
  12196. (or (cdr (assoc type val)) ".")
  12197. val)))
  12198. dir))
  12199. (defun org-export-find-first-heading-line (list)
  12200. "Remove all lines from LIST which are before the first headline."
  12201. (let ((orig-list list)
  12202. (re (concat "^" outline-regexp)))
  12203. (while (and list
  12204. (not (string-match re (car list))))
  12205. (pop list))
  12206. (or list orig-list)))
  12207. (defun org-skip-comments (lines)
  12208. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  12209. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  12210. (re2 "^\\(\\*+\\)[ \t\n\r]")
  12211. rtn line level)
  12212. (while (setq line (pop lines))
  12213. (cond
  12214. ((and (string-match re1 line)
  12215. (setq level (- (match-end 1) (match-beginning 1))))
  12216. ;; Beginning of a COMMENT subtree. Skip it.
  12217. (while (and (setq line (pop lines))
  12218. (or (not (string-match re2 line))
  12219. (> (- (match-end 1) (match-beginning 1)) level))))
  12220. (setq lines (cons line lines)))
  12221. ((string-match "^#" line)
  12222. ;; an ordinary comment line
  12223. )
  12224. ((and org-export-table-remove-special-lines
  12225. (string-match "^[ \t]*|" line)
  12226. (or (string-match "^[ \t]*| *[!_^] *|" line)
  12227. (and (string-match "| *<[0-9]+> *|" line)
  12228. (not (string-match "| *[^ <|]" line)))))
  12229. ;; a special table line that should be removed
  12230. )
  12231. (t (setq rtn (cons line rtn)))))
  12232. (nreverse rtn)))
  12233. (defun org-export (&optional arg)
  12234. (interactive)
  12235. (let ((help "[t] insert the export option template
  12236. \[v] limit export to visible part of outline tree
  12237. \[a] export as ASCII
  12238. \[h] export as HTML
  12239. \[b] export as HTML and browse immediately
  12240. \[x] export as XOXO
  12241. \[i] export current file as iCalendar file
  12242. \[I] export all agenda files as iCalendar files
  12243. \[c] export agenda files into combined iCalendar file
  12244. \[F] publish current file
  12245. \[P] publish current project
  12246. \[X] publish... (project will be prompted for)
  12247. \[A] publish all projects")
  12248. (cmds
  12249. '((?t . org-insert-export-options-template)
  12250. (?v . org-export-visible)
  12251. (?a . org-export-as-ascii)
  12252. (?h . org-export-as-html)
  12253. (?b . org-export-as-html-and-open)
  12254. (?x . org-export-as-xoxo)
  12255. (?i . org-export-icalendar-this-file)
  12256. (?I . org-export-icalendar-all-agenda-files)
  12257. (?c . org-export-icalendar-combine-agenda-files)
  12258. (?F . org-publish-current-file)
  12259. (?P . org-publish-current-project)
  12260. (?X . org-publish)
  12261. (?A . org-publish-all)))
  12262. r1 r2 ass)
  12263. (save-window-excursion
  12264. (delete-other-windows)
  12265. (with-output-to-temp-buffer "*Org Export/Publishing Help*"
  12266. (princ help))
  12267. (message "Select command: ")
  12268. (setq r1 (read-char-exclusive)))
  12269. (setq r2 (if (< r1 27) (+ r1 96) r1))
  12270. (if (setq ass (assq r2 cmds))
  12271. (call-interactively (cdr ass))
  12272. (error "No command associated with key %c" r1))))
  12273. ;; ASCII
  12274. (defconst org-html-entities
  12275. '(("nbsp")
  12276. ("iexcl")
  12277. ("cent")
  12278. ("pound")
  12279. ("curren")
  12280. ("yen")
  12281. ("brvbar")
  12282. ("sect")
  12283. ("uml")
  12284. ("copy")
  12285. ("ordf")
  12286. ("laquo")
  12287. ("not")
  12288. ("shy")
  12289. ("reg")
  12290. ("macr")
  12291. ("deg")
  12292. ("plusmn")
  12293. ("sup2")
  12294. ("sup3")
  12295. ("acute")
  12296. ("micro")
  12297. ("para")
  12298. ("middot")
  12299. ("odot"."o")
  12300. ("star"."*")
  12301. ("cedil")
  12302. ("sup1")
  12303. ("ordm")
  12304. ("raquo")
  12305. ("frac14")
  12306. ("frac12")
  12307. ("frac34")
  12308. ("iquest")
  12309. ("Agrave")
  12310. ("Aacute")
  12311. ("Acirc")
  12312. ("Atilde")
  12313. ("Auml")
  12314. ("Aring") ("AA"."&Aring;")
  12315. ("AElig")
  12316. ("Ccedil")
  12317. ("Egrave")
  12318. ("Eacute")
  12319. ("Ecirc")
  12320. ("Euml")
  12321. ("Igrave")
  12322. ("Iacute")
  12323. ("Icirc")
  12324. ("Iuml")
  12325. ("ETH")
  12326. ("Ntilde")
  12327. ("Ograve")
  12328. ("Oacute")
  12329. ("Ocirc")
  12330. ("Otilde")
  12331. ("Ouml")
  12332. ("times")
  12333. ("Oslash")
  12334. ("Ugrave")
  12335. ("Uacute")
  12336. ("Ucirc")
  12337. ("Uuml")
  12338. ("Yacute")
  12339. ("THORN")
  12340. ("szlig")
  12341. ("agrave")
  12342. ("aacute")
  12343. ("acirc")
  12344. ("atilde")
  12345. ("auml")
  12346. ("aring")
  12347. ("aelig")
  12348. ("ccedil")
  12349. ("egrave")
  12350. ("eacute")
  12351. ("ecirc")
  12352. ("euml")
  12353. ("igrave")
  12354. ("iacute")
  12355. ("icirc")
  12356. ("iuml")
  12357. ("eth")
  12358. ("ntilde")
  12359. ("ograve")
  12360. ("oacute")
  12361. ("ocirc")
  12362. ("otilde")
  12363. ("ouml")
  12364. ("divide")
  12365. ("oslash")
  12366. ("ugrave")
  12367. ("uacute")
  12368. ("ucirc")
  12369. ("uuml")
  12370. ("yacute")
  12371. ("thorn")
  12372. ("yuml")
  12373. ("fnof")
  12374. ("Alpha")
  12375. ("Beta")
  12376. ("Gamma")
  12377. ("Delta")
  12378. ("Epsilon")
  12379. ("Zeta")
  12380. ("Eta")
  12381. ("Theta")
  12382. ("Iota")
  12383. ("Kappa")
  12384. ("Lambda")
  12385. ("Mu")
  12386. ("Nu")
  12387. ("Xi")
  12388. ("Omicron")
  12389. ("Pi")
  12390. ("Rho")
  12391. ("Sigma")
  12392. ("Tau")
  12393. ("Upsilon")
  12394. ("Phi")
  12395. ("Chi")
  12396. ("Psi")
  12397. ("Omega")
  12398. ("alpha")
  12399. ("beta")
  12400. ("gamma")
  12401. ("delta")
  12402. ("epsilon")
  12403. ("varepsilon"."&epsilon;")
  12404. ("zeta")
  12405. ("eta")
  12406. ("theta")
  12407. ("iota")
  12408. ("kappa")
  12409. ("lambda")
  12410. ("mu")
  12411. ("nu")
  12412. ("xi")
  12413. ("omicron")
  12414. ("pi")
  12415. ("rho")
  12416. ("sigmaf") ("varsigma"."&sigmaf;")
  12417. ("sigma")
  12418. ("tau")
  12419. ("upsilon")
  12420. ("phi")
  12421. ("chi")
  12422. ("psi")
  12423. ("omega")
  12424. ("thetasym") ("vartheta"."&thetasym;")
  12425. ("upsih")
  12426. ("piv")
  12427. ("bull") ("bullet"."&bull;")
  12428. ("hellip") ("dots"."&hellip;")
  12429. ("prime")
  12430. ("Prime")
  12431. ("oline")
  12432. ("frasl")
  12433. ("weierp")
  12434. ("image")
  12435. ("real")
  12436. ("trade")
  12437. ("alefsym")
  12438. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  12439. ("uarr") ("uparrow"."&uarr;")
  12440. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  12441. ("darr")("downarrow"."&darr;")
  12442. ("harr") ("leftrightarrow"."&harr;")
  12443. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  12444. ("lArr") ("Leftarrow"."&lArr;")
  12445. ("uArr") ("Uparrow"."&uArr;")
  12446. ("rArr") ("Rightarrow"."&rArr;")
  12447. ("dArr") ("Downarrow"."&dArr;")
  12448. ("hArr") ("Leftrightarrow"."&hArr;")
  12449. ("forall")
  12450. ("part") ("partial"."&part;")
  12451. ("exist") ("exists"."&exist;")
  12452. ("empty") ("emptyset"."&empty;")
  12453. ("nabla")
  12454. ("isin") ("in"."&isin;")
  12455. ("notin")
  12456. ("ni")
  12457. ("prod")
  12458. ("sum")
  12459. ("minus")
  12460. ("lowast") ("ast"."&lowast;")
  12461. ("radic")
  12462. ("prop") ("proptp"."&prop;")
  12463. ("infin") ("infty"."&infin;")
  12464. ("ang") ("angle"."&ang;")
  12465. ("and") ("vee"."&and;")
  12466. ("or") ("wedge"."&or;")
  12467. ("cap")
  12468. ("cup")
  12469. ("int")
  12470. ("there4")
  12471. ("sim")
  12472. ("cong") ("simeq"."&cong;")
  12473. ("asymp")("approx"."&asymp;")
  12474. ("ne") ("neq"."&ne;")
  12475. ("equiv")
  12476. ("le")
  12477. ("ge")
  12478. ("sub") ("subset"."&sub;")
  12479. ("sup") ("supset"."&sup;")
  12480. ("nsub")
  12481. ("sube")
  12482. ("supe")
  12483. ("oplus")
  12484. ("otimes")
  12485. ("perp")
  12486. ("sdot") ("cdot"."&sdot;")
  12487. ("lceil")
  12488. ("rceil")
  12489. ("lfloor")
  12490. ("rfloor")
  12491. ("lang")
  12492. ("rang")
  12493. ("loz") ("Diamond"."&loz;")
  12494. ("spades") ("spadesuit"."&spades;")
  12495. ("clubs") ("clubsuit"."&clubs;")
  12496. ("hearts") ("diamondsuit"."&hearts;")
  12497. ("diams") ("diamondsuit"."&diams;")
  12498. ("quot")
  12499. ("amp")
  12500. ("lt")
  12501. ("gt")
  12502. ("OElig")
  12503. ("oelig")
  12504. ("Scaron")
  12505. ("scaron")
  12506. ("Yuml")
  12507. ("circ")
  12508. ("tilde")
  12509. ("ensp")
  12510. ("emsp")
  12511. ("thinsp")
  12512. ("zwnj")
  12513. ("zwj")
  12514. ("lrm")
  12515. ("rlm")
  12516. ("ndash")
  12517. ("mdash")
  12518. ("lsquo")
  12519. ("rsquo")
  12520. ("sbquo")
  12521. ("ldquo")
  12522. ("rdquo")
  12523. ("bdquo")
  12524. ("dagger")
  12525. ("Dagger")
  12526. ("permil")
  12527. ("lsaquo")
  12528. ("rsaquo")
  12529. ("euro")
  12530. ("arccos"."arccos")
  12531. ("arcsin"."arcsin")
  12532. ("arctan"."arctan")
  12533. ("arg"."arg")
  12534. ("cos"."cos")
  12535. ("cosh"."cosh")
  12536. ("cot"."cot")
  12537. ("coth"."coth")
  12538. ("csc"."csc")
  12539. ("deg"."deg")
  12540. ("det"."det")
  12541. ("dim"."dim")
  12542. ("exp"."exp")
  12543. ("gcd"."gcd")
  12544. ("hom"."hom")
  12545. ("inf"."inf")
  12546. ("ker"."ker")
  12547. ("lg"."lg")
  12548. ("lim"."lim")
  12549. ("liminf"."liminf")
  12550. ("limsup"."limsup")
  12551. ("ln"."ln")
  12552. ("log"."log")
  12553. ("max"."max")
  12554. ("min"."min")
  12555. ("Pr"."Pr")
  12556. ("sec"."sec")
  12557. ("sin"."sin")
  12558. ("sinh"."sinh")
  12559. ("sup"."sup")
  12560. ("tan"."tan")
  12561. ("tanh"."tanh")
  12562. )
  12563. "Entities for TeX->HTML translation.
  12564. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  12565. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  12566. In that case, \"\\ent\" will be translated to \"&other;\".
  12567. The list contains HTML entities for Latin-1, Greek and other symbols.
  12568. It is supplemented by a number of commonly used TeX macros with appropriate
  12569. translations. There is currently no way for users to extend this.")
  12570. (defun org-cleaned-string-for-export (string &rest parameters)
  12571. "Cleanup a buffer substring so that links can be created safely."
  12572. (interactive)
  12573. (let* ((re-radio (and org-target-link-regexp
  12574. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  12575. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  12576. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  12577. (re-archive (concat ":" org-archive-tag ":"))
  12578. rtn)
  12579. (save-excursion
  12580. (set-buffer (get-buffer-create " org-mode-tmp"))
  12581. (erase-buffer)
  12582. (insert string)
  12583. (let ((org-inhibit-startup t)) (org-mode))
  12584. ;; Get rid of archived trees
  12585. (when (not (eq org-export-with-archived-trees t))
  12586. (goto-char (point-min))
  12587. (while (re-search-forward re-archive nil t)
  12588. (if (not (org-on-heading-p))
  12589. (org-end-of-subtree t)
  12590. (beginning-of-line 1)
  12591. (delete-region
  12592. (if org-export-with-archived-trees (1+ (point-at-eol)) (point))
  12593. (org-end-of-subtree)))))
  12594. ;; Find targets in comments and move them out of comments,
  12595. ;; but mark them as targets that should be invisible
  12596. (goto-char (point-min))
  12597. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  12598. (replace-match "\\1(INVISIBLE)"))
  12599. ;; Find matches for radio targets and turn them into internal links
  12600. (goto-char (point-min))
  12601. (when re-radio
  12602. (while (re-search-forward re-radio nil t)
  12603. (replace-match "\\1[[\\2]]")))
  12604. ;; Find all links that contain a newline and put them into a single line
  12605. (goto-char (point-min))
  12606. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  12607. (replace-match "\\1 \\3")
  12608. (goto-char (match-beginning 0)))
  12609. ;; Convert LaTeX fragments to images
  12610. (when (memq :LaTeX-fragments parameters)
  12611. (org-format-latex
  12612. (concat "ltxpng/" (file-name-sans-extension
  12613. (file-name-nondirectory
  12614. org-current-export-file)))
  12615. org-current-export-dir nil "Creating LaTeX image %s"))
  12616. (message "Exporting...")
  12617. ;; Normalize links: Convert angle and plain links into bracket links
  12618. (goto-char (point-min))
  12619. (while (re-search-forward re-plain-link nil t)
  12620. (replace-match
  12621. (concat
  12622. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12623. t t))
  12624. (goto-char (point-min))
  12625. (while (re-search-forward re-angle-link nil t)
  12626. (replace-match
  12627. (concat
  12628. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  12629. t t))
  12630. ;; Find multiline emphasis and put them into single line
  12631. (when (memq :emph-multiline parameters)
  12632. (goto-char (point-min))
  12633. (while (re-search-forward org-emph-re nil t)
  12634. (subst-char-in-region (match-beginning 0) (match-end 0) ?\n ?\ t)
  12635. (goto-char (1- (match-end 0)))))
  12636. ;; Remove comments
  12637. (goto-char (point-min))
  12638. (while (re-search-forward "^#.*\n?" nil t)
  12639. (replace-match ""))
  12640. (setq rtn (buffer-string)))
  12641. (kill-buffer " org-mode-tmp")
  12642. rtn))
  12643. (defun org-solidify-link-text (s &optional alist)
  12644. "Take link text and make a safe target out of it."
  12645. (save-match-data
  12646. (let* ((rtn
  12647. (mapconcat
  12648. 'identity
  12649. (org-split-string s "[ \t\r\n]+") "--"))
  12650. (a (assoc rtn alist)))
  12651. (or (cdr a) rtn))))
  12652. (defun org-convert-to-odd-levels ()
  12653. "Convert an org-mode file with all levels allowed to one with odd levels.
  12654. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  12655. level 5 etc."
  12656. (interactive)
  12657. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  12658. (let ((org-odd-levels-only nil) n)
  12659. (save-excursion
  12660. (goto-char (point-min))
  12661. (while (re-search-forward "^\\*\\*+" nil t)
  12662. (setq n (1- (length (match-string 0))))
  12663. (while (>= (setq n (1- n)) 0)
  12664. (org-demote))
  12665. (end-of-line 1))))))
  12666. (defun org-convert-to-oddeven-levels ()
  12667. "Convert an org-mode file with only odd levels to one with odd and even levels.
  12668. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  12669. section with an even level, conversion would destroy the structure of the file. An error
  12670. is signaled in this case."
  12671. (interactive)
  12672. (goto-char (point-min))
  12673. ;; First check if there are no even levels
  12674. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  12675. (org-show-hierarchy-above)
  12676. (error "Not all levels are odd in this file. Conversion not possible."))
  12677. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  12678. (let ((org-odd-levels-only nil) n)
  12679. (save-excursion
  12680. (goto-char (point-min))
  12681. (while (re-search-forward "^\\*\\*+" nil t)
  12682. (setq n (/ (length (match-string 0)) 2))
  12683. (while (>= (setq n (1- n)) 0)
  12684. (org-promote))
  12685. (end-of-line 1))))))
  12686. (defun org-tr-level (n)
  12687. "Make N odd if required."
  12688. (if org-odd-levels-only (1+ (/ n 2)) n))
  12689. (defvar org-last-level nil) ; dynamically scoped variable
  12690. (defvar org-ascii-current-indentation nil) ; For communication
  12691. (defun org-export-as-ascii (arg)
  12692. "Export the outline as a pretty ASCII file.
  12693. If there is an active region, export only the region.
  12694. The prefix ARG specifies how many levels of the outline should become
  12695. underlined headlines. The default is 3."
  12696. (interactive "P")
  12697. (setq-default org-todo-line-regexp org-todo-line-regexp)
  12698. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12699. (org-infile-export-plist)))
  12700. (region
  12701. (buffer-substring
  12702. (if (org-region-active-p) (region-beginning) (point-min))
  12703. (if (org-region-active-p) (region-end) (point-max))))
  12704. (lines (org-export-find-first-heading-line
  12705. (org-skip-comments
  12706. (org-split-string
  12707. (org-cleaned-string-for-export region)
  12708. "[\r\n]"))))
  12709. (org-ascii-current-indentation '(0 . 0))
  12710. (org-startup-with-deadline-check nil)
  12711. (level 0) line txt
  12712. (umax nil)
  12713. (case-fold-search nil)
  12714. (filename (concat (file-name-as-directory
  12715. (org-export-directory :ascii opt-plist))
  12716. (file-name-sans-extension
  12717. (file-name-nondirectory buffer-file-name))
  12718. ".txt"))
  12719. (buffer (find-file-noselect filename))
  12720. (levels-open (make-vector org-level-max nil))
  12721. (odd org-odd-levels-only)
  12722. (date (format-time-string "%Y/%m/%d" (current-time)))
  12723. (time (format-time-string "%X" (org-current-time)))
  12724. (author (plist-get opt-plist :author))
  12725. (title (or (plist-get opt-plist :title)
  12726. (file-name-sans-extension
  12727. (file-name-nondirectory buffer-file-name))))
  12728. (email (plist-get opt-plist :email))
  12729. (language (plist-get opt-plist :language))
  12730. (text nil)
  12731. (todo nil)
  12732. (lang-words nil))
  12733. (setq org-last-level 1)
  12734. (org-init-section-numbers)
  12735. (find-file-noselect filename)
  12736. (setq lang-words (or (assoc language org-export-language-setup)
  12737. (assoc "en" org-export-language-setup)))
  12738. (if org-export-ascii-show-new-buffer
  12739. (switch-to-buffer-other-window buffer)
  12740. (set-buffer buffer))
  12741. (erase-buffer)
  12742. (fundamental-mode)
  12743. ;; create local variables for all options, to make sure all called
  12744. ;; functions get the correct information
  12745. (mapcar (lambda (x)
  12746. (set (make-local-variable (cdr x))
  12747. (plist-get opt-plist (car x))))
  12748. org-export-plist-vars)
  12749. (org-set-local 'org-odd-levels-only odd)
  12750. (setq umax (if arg (prefix-numeric-value arg)
  12751. org-export-headline-levels))
  12752. ;; File header
  12753. (if title (org-insert-centered title ?=))
  12754. (insert "\n")
  12755. (if (or author email)
  12756. (insert (concat (nth 1 lang-words) ": " (or author "")
  12757. (if email (concat " <" email ">") "")
  12758. "\n")))
  12759. (if (and date time)
  12760. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  12761. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  12762. (insert "\n\n")
  12763. (if org-export-with-toc
  12764. (progn
  12765. (insert (nth 3 lang-words) "\n"
  12766. (make-string (length (nth 3 lang-words)) ?=) "\n")
  12767. (mapcar '(lambda (line)
  12768. (if (string-match org-todo-line-regexp
  12769. line)
  12770. ;; This is a headline
  12771. (progn
  12772. (setq level (- (match-end 1) (match-beginning 1))
  12773. level (org-tr-level level)
  12774. txt (match-string 3 line)
  12775. todo
  12776. (or (and org-export-mark-todo-in-toc
  12777. (match-beginning 2)
  12778. (not (equal (match-string 2 line)
  12779. org-done-string)))
  12780. ; TODO, not DONE
  12781. (and org-export-mark-todo-in-toc
  12782. (= level umax)
  12783. (org-search-todo-below
  12784. line lines level))))
  12785. (setq txt (org-html-expand-for-ascii txt))
  12786. (if org-export-with-section-numbers
  12787. (setq txt (concat (org-section-number level)
  12788. " " txt)))
  12789. (if (<= level umax)
  12790. (progn
  12791. (insert
  12792. (make-string (* (1- level) 4) ?\ )
  12793. (format (if todo "%s (*)\n" "%s\n") txt))
  12794. (setq org-last-level level))
  12795. ))))
  12796. lines)))
  12797. (org-init-section-numbers)
  12798. (while (setq line (pop lines))
  12799. ;; Remove the quoted HTML tags.
  12800. (setq line (org-html-expand-for-ascii line))
  12801. ;; Remove targets
  12802. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  12803. (setq line (replace-match "" t t line)))
  12804. ;; Replace internal links
  12805. (while (string-match org-bracket-link-regexp line)
  12806. (setq line (replace-match
  12807. (if (match-end 3) "[\\3]" "[\\1]")
  12808. t nil line)))
  12809. (cond
  12810. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  12811. ;; a Headline
  12812. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  12813. txt (match-string 2 line))
  12814. (org-ascii-level-start level txt umax lines))
  12815. (t
  12816. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  12817. (normal-mode)
  12818. (save-buffer)
  12819. ;; remove display and invisible chars
  12820. (let (beg end)
  12821. (goto-char (point-min))
  12822. (while (setq beg (next-single-property-change (point) 'display))
  12823. (setq end (next-single-property-change beg 'display))
  12824. (delete-region beg end)
  12825. (goto-char beg)
  12826. (insert "=>"))
  12827. (goto-char (point-min))
  12828. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  12829. (setq end (next-single-property-change beg 'org-cwidth))
  12830. (delete-region beg end)
  12831. (goto-char beg)))
  12832. (goto-char (point-min))))
  12833. (defun org-search-todo-below (line lines level)
  12834. "Search the subtree below LINE for any TODO entries."
  12835. (let ((rest (cdr (memq line lines)))
  12836. (re org-todo-line-regexp)
  12837. line lv todo)
  12838. (catch 'exit
  12839. (while (setq line (pop rest))
  12840. (if (string-match re line)
  12841. (progn
  12842. (setq lv (- (match-end 1) (match-beginning 1))
  12843. todo (and (match-beginning 2)
  12844. (not (equal (match-string 2 line)
  12845. org-done-string))))
  12846. ; TODO, not DONE
  12847. (if (<= lv level) (throw 'exit nil))
  12848. (if todo (throw 'exit t))))))))
  12849. (defun org-html-expand-for-ascii (line)
  12850. "Handle quoted HTML for ASCII export."
  12851. (if org-export-html-expand
  12852. (while (string-match "@<[^<>\n]*>" line)
  12853. ;; We just remove the tags for now.
  12854. (setq line (replace-match "" nil nil line))))
  12855. line)
  12856. (defun org-insert-centered (s &optional underline)
  12857. "Insert the string S centered and underline it with character UNDERLINE."
  12858. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  12859. (insert (make-string ind ?\ ) s "\n")
  12860. (if underline
  12861. (insert (make-string ind ?\ )
  12862. (make-string (string-width s) underline)
  12863. "\n"))))
  12864. (defun org-ascii-level-start (level title umax &optional lines)
  12865. "Insert a new level in ASCII export."
  12866. (let (char (n (- level umax 1)) (ind 0))
  12867. (if (> level umax)
  12868. (progn
  12869. (insert (make-string (* 2 n) ?\ )
  12870. (char-to-string (nth (% n (length org-export-ascii-bullets))
  12871. org-export-ascii-bullets))
  12872. " " title "\n")
  12873. ;; find the indentation of the next non-empty line
  12874. (catch 'stop
  12875. (while lines
  12876. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  12877. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  12878. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  12879. (pop lines)))
  12880. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  12881. (if (or (not (equal (char-before) ?\n))
  12882. (not (equal (char-before (1- (point))) ?\n)))
  12883. (insert "\n"))
  12884. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  12885. (if org-export-with-section-numbers
  12886. (setq title (concat (org-section-number level) " " title)))
  12887. (insert title "\n" (make-string (string-width title) char) "\n")
  12888. (setq org-ascii-current-indentation '(0 . 0)))))
  12889. (defun org-export-visible (type arg)
  12890. "Create a copy of the visible part of the current buffer, and export it.
  12891. The copy is created in a temporary buffer and removed after use.
  12892. TYPE is the final key (as a string) that also select the export command in
  12893. the `C-c C-e' export dispatcher.
  12894. As a special case, if the you type SPC at the prompt, the temporary
  12895. org-mode file will not be removed but presented to you so that you can
  12896. continue to use it. The prefix arg ARG is passed through to the exporting
  12897. command."
  12898. (interactive
  12899. (list (progn
  12900. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  12901. (read-char-exclusive))
  12902. current-prefix-arg))
  12903. (if (not (member type '(?a ?\C-a ?b ?\C-b ?h ?x ?\ )))
  12904. (error "Invalid export key"))
  12905. (let* ((binding (cdr (assoc type
  12906. '((?a . org-export-as-ascii)
  12907. (?\C-a . org-export-as-ascii)
  12908. (?b . org-export-as-html-and-open)
  12909. (?\C-b . org-export-as-html-and-open)
  12910. (?h . org-export-as-html)
  12911. (?x . org-export-as-xoxo)))))
  12912. (keepp (equal type ?\ ))
  12913. (file buffer-file-name)
  12914. (buffer (get-buffer-create "*Org Export Visible*"))
  12915. s e)
  12916. (with-current-buffer buffer (erase-buffer))
  12917. (save-excursion
  12918. (setq s (goto-char (point-min)))
  12919. (while (not (= (point) (point-max)))
  12920. (goto-char (org-find-invisible))
  12921. (append-to-buffer buffer s (point))
  12922. (setq s (goto-char (org-find-visible))))
  12923. (goto-char (point-min))
  12924. (unless keepp
  12925. ;; Copy all comment lines to the end, to make sure #+ settings are
  12926. ;; still available for the second export step. Kind of a hack, but
  12927. ;; does do the trick.
  12928. (if (looking-at "#[^\r\n]*")
  12929. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  12930. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  12931. (append-to-buffer buffer (1+ (match-beginning 0))
  12932. (min (point-max) (1+ (match-end 0))))))
  12933. (set-buffer buffer)
  12934. (let ((buffer-file-name file)
  12935. (org-inhibit-startup t))
  12936. (org-mode)
  12937. (show-all)
  12938. (unless keepp (funcall binding arg))))
  12939. (if (not keepp)
  12940. (kill-buffer buffer)
  12941. (switch-to-buffer-other-window buffer)
  12942. (goto-char (point-min)))))
  12943. (defun org-find-visible ()
  12944. (let ((s (point)))
  12945. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  12946. (get-char-property s 'invisible)))
  12947. s))
  12948. (defun org-find-invisible ()
  12949. (let ((s (point)))
  12950. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  12951. (not (get-char-property s 'invisible))))
  12952. s))
  12953. ;; HTML
  12954. (defun org-get-current-options ()
  12955. "Return a string with current options as keyword options.
  12956. Does include HTML export options as well as TODO and CATEGORY stuff."
  12957. (format
  12958. "#+TITLE: %s
  12959. #+AUTHOR: %s
  12960. #+EMAIL: %s
  12961. #+LANGUAGE: %s
  12962. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  12963. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s LaTeX:%s
  12964. #+CATEGORY: %s
  12965. #+SEQ_TODO: %s
  12966. #+TYP_TODO: %s
  12967. #+STARTUP: %s %s %s %s %s %s
  12968. #+TAGS: %s
  12969. #+ARCHIVE: %s
  12970. "
  12971. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  12972. org-export-headline-levels
  12973. org-export-with-section-numbers
  12974. org-export-with-toc
  12975. org-export-preserve-breaks
  12976. org-export-html-expand
  12977. org-export-with-fixed-width
  12978. org-export-with-tables
  12979. org-export-with-sub-superscripts
  12980. org-export-with-emphasize
  12981. org-export-with-TeX-macros
  12982. org-export-with-LaTeX-fragments
  12983. (file-name-nondirectory buffer-file-name)
  12984. (if (equal org-todo-interpretation 'sequence)
  12985. (mapconcat 'identity org-todo-keywords " ")
  12986. "TODO FEEDBACK VERIFY DONE")
  12987. (if (equal org-todo-interpretation 'type)
  12988. (mapconcat 'identity org-todo-keywords " ")
  12989. "Me Jason Marie DONE")
  12990. (cdr (assoc org-startup-folded
  12991. '((nil . "showall") (t . "overview") (content . "content"))))
  12992. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  12993. (if org-odd-levels-only "odd" "oddeven")
  12994. (if org-hide-leading-stars "hidestars" "showstars")
  12995. (if org-startup-align-all-tables "align" "noalign")
  12996. (if org-log-done "logging" "nologging")
  12997. (or (mapconcat (lambda (x)
  12998. (cond
  12999. ((equal '(:startgroup) x) "{")
  13000. ((equal '(:endgroup) x) "}")
  13001. ((cdr x) (format "%s(%c)" (car x) (cdr x)))
  13002. (t (car x))))
  13003. (or org-tag-alist (org-get-buffer-tags)) " ") "")
  13004. org-archive-location
  13005. ))
  13006. (defun org-insert-export-options-template ()
  13007. "Insert into the buffer a template with information for exporting."
  13008. (interactive)
  13009. (if (not (bolp)) (newline))
  13010. (let ((s (org-get-current-options)))
  13011. (and (string-match "#\\+CATEGORY" s)
  13012. (setq s (substring s 0 (match-beginning 0))))
  13013. (insert s)))
  13014. (defun org-toggle-fixed-width-section (arg)
  13015. "Toggle the fixed-width export.
  13016. If there is no active region, the QUOTE keyword at the current headline is
  13017. inserted or removed. When present, it causes the text between this headline
  13018. and the next to be exported as fixed-width text, and unmodified.
  13019. If there is an active region, this command adds or removes a colon as the
  13020. first character of this line. If the first character of a line is a colon,
  13021. this line is also exported in fixed-width font."
  13022. (interactive "P")
  13023. (let* ((cc 0)
  13024. (regionp (org-region-active-p))
  13025. (beg (if regionp (region-beginning) (point)))
  13026. (end (if regionp (region-end)))
  13027. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13028. (re "[ \t]*\\(:\\)")
  13029. off)
  13030. (if regionp
  13031. (save-excursion
  13032. (goto-char beg)
  13033. (setq cc (current-column))
  13034. (beginning-of-line 1)
  13035. (setq off (looking-at re))
  13036. (while (> nlines 0)
  13037. (setq nlines (1- nlines))
  13038. (beginning-of-line 1)
  13039. (cond
  13040. (arg
  13041. (move-to-column cc t)
  13042. (insert ":\n")
  13043. (forward-line -1))
  13044. ((and off (looking-at re))
  13045. (replace-match "" t t nil 1))
  13046. ((not off) (move-to-column cc t) (insert ":")))
  13047. (forward-line 1)))
  13048. (save-excursion
  13049. (org-back-to-heading)
  13050. (if (looking-at (concat outline-regexp
  13051. "\\( +\\<" org-quote-string "\\>\\)"))
  13052. (replace-match "" t t nil 1)
  13053. (if (looking-at outline-regexp)
  13054. (progn
  13055. (goto-char (match-end 0))
  13056. (insert " " org-quote-string))))))))
  13057. (defun org-export-as-html-and-open (arg)
  13058. "Export the outline as HTML and immediately open it with a browser.
  13059. If there is an active region, export only the region.
  13060. The prefix ARG specifies how many levels of the outline should become
  13061. headlines. The default is 3. Lower levels will become bulleted lists."
  13062. (interactive "P")
  13063. (org-export-as-html arg 'hidden)
  13064. (org-open-file buffer-file-name))
  13065. (defun org-export-as-html-batch ()
  13066. "Call `org-export-as-html', may be used in batch processing as
  13067. emacs --batch
  13068. --load=$HOME/lib/emacs/org.el
  13069. --eval \"(setq org-export-headline-levels 2)\"
  13070. --visit=MyFile --funcall org-export-as-html-batch"
  13071. (org-export-as-html org-export-headline-levels 'hidden))
  13072. (defun org-export-as-html (arg &optional hidden ext-plist)
  13073. "Export the outline as a pretty HTML file.
  13074. If there is an active region, export only the region.
  13075. The prefix ARG specifies how many levels of the outline should become
  13076. headlines. The default is 3. Lower levels will become bulleted lists.
  13077. When HIDDEN is non-nil, don't display the HTML buffer.
  13078. EXT-PLIST is a property list with external parameters overriding
  13079. org-mode's default settings, but still inferior to file-local settings."
  13080. (interactive "P")
  13081. (message "Exporting...")
  13082. (setq-default org-todo-line-regexp org-todo-line-regexp)
  13083. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  13084. (setq-default org-done-string org-done-string)
  13085. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13086. ext-plist
  13087. (org-infile-export-plist)))
  13088. (style (plist-get opt-plist :style))
  13089. (link-validate (plist-get opt-plist :link-validation-function))
  13090. valid
  13091. (odd org-odd-levels-only)
  13092. (region-p (org-region-active-p))
  13093. (region
  13094. (buffer-substring
  13095. (if region-p (region-beginning) (point-min))
  13096. (if region-p (region-end) (point-max))))
  13097. ;; The following two are dynamically scoped into other
  13098. ;; routines below.
  13099. (org-current-export-dir (org-export-directory :html opt-plist))
  13100. (org-current-export-file buffer-file-name)
  13101. (all_lines
  13102. (org-skip-comments (org-split-string
  13103. (org-cleaned-string-for-export
  13104. region :emph-multiline
  13105. (if (plist-get opt-plist :LaTeX-fragments)
  13106. :LaTeX-fragments))
  13107. "[\r\n]")))
  13108. (lines (org-export-find-first-heading-line all_lines))
  13109. (level 0) (line "") (origline "") txt todo
  13110. (umax nil)
  13111. (filename (concat (file-name-as-directory
  13112. (org-export-directory :html opt-plist))
  13113. (file-name-sans-extension
  13114. (file-name-nondirectory buffer-file-name))
  13115. ".html"))
  13116. (current-dir (file-name-directory buffer-file-name))
  13117. (buffer (find-file-noselect filename))
  13118. (levels-open (make-vector org-level-max nil))
  13119. (date (format-time-string "%Y/%m/%d" (current-time)))
  13120. (time (format-time-string "%X" (org-current-time)))
  13121. (author (plist-get opt-plist :author))
  13122. (title (or (plist-get opt-plist :title)
  13123. (file-name-sans-extension
  13124. (file-name-nondirectory buffer-file-name))))
  13125. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  13126. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  13127. (inquote nil)
  13128. (infixed nil)
  13129. (in-local-list nil)
  13130. (local-list-num nil)
  13131. (local-list-indent nil)
  13132. (llt org-plain-list-ordered-item-terminator)
  13133. (email (plist-get opt-plist :email))
  13134. (language (plist-get opt-plist :language))
  13135. (text (plist-get opt-plist :text))
  13136. (lang-words nil)
  13137. (target-alist nil) tg
  13138. (head-count 0) cnt
  13139. (start 0)
  13140. (coding-system (and (boundp 'buffer-file-coding-system)
  13141. buffer-file-coding-system))
  13142. (coding-system-for-write coding-system)
  13143. (save-buffer-coding-system coding-system)
  13144. (charset (and coding-system
  13145. (fboundp 'coding-system-get)
  13146. (coding-system-get coding-system 'mime-charset)))
  13147. table-open type
  13148. table-buffer table-orig-buffer
  13149. ind start-is-num starter
  13150. rpl path desc descp desc1 desc2 link
  13151. )
  13152. (message "Exporting...")
  13153. (setq org-last-level 1)
  13154. (org-init-section-numbers)
  13155. ;; Get the language-dependent settings
  13156. (setq lang-words (or (assoc language org-export-language-setup)
  13157. (assoc "en" org-export-language-setup)))
  13158. ;; Switch to the output buffer
  13159. (if (or hidden (not org-export-html-show-new-buffer))
  13160. (set-buffer buffer)
  13161. (switch-to-buffer-other-window buffer))
  13162. (erase-buffer)
  13163. (fundamental-mode)
  13164. (let ((case-fold-search nil)
  13165. (org-odd-levels-only odd))
  13166. ;; create local variables for all options, to make sure all called
  13167. ;; functions get the correct information
  13168. (mapcar (lambda (x)
  13169. (set (make-local-variable (cdr x))
  13170. (plist-get opt-plist (car x))))
  13171. org-export-plist-vars)
  13172. (setq umax (if arg (prefix-numeric-value arg)
  13173. org-export-headline-levels))
  13174. ;; File header
  13175. (insert (format
  13176. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  13177. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  13178. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  13179. lang=\"%s\" xml:lang=\"%s\">
  13180. <head>
  13181. <title>%s</title>
  13182. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  13183. <meta name=\"generator\" content=\"Org-mode\"/>
  13184. <meta name=\"generated\" content=\"%s %s\"/>
  13185. <meta name=\"author\" content=\"%s\"/>
  13186. %s
  13187. </head><body>
  13188. "
  13189. language language (org-html-expand title) (or charset "iso-8859-1")
  13190. date time author style))
  13191. (insert (or (plist-get opt-plist :preamble) ""))
  13192. (when (plist-get opt-plist :auto-preamble)
  13193. (if title (insert (format org-export-html-title-format
  13194. (org-html-expand title))))
  13195. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  13196. (if org-export-with-toc
  13197. (progn
  13198. (insert (format "<h%d>%s</h%d>\n"
  13199. org-export-html-toplevel-hlevel
  13200. (nth 3 lang-words)
  13201. org-export-html-toplevel-hlevel))
  13202. (insert "<ul>\n<li>")
  13203. (setq lines
  13204. (mapcar '(lambda (line)
  13205. (if (string-match org-todo-line-regexp line)
  13206. ;; This is a headline
  13207. (progn
  13208. (setq level (- (match-end 1) (match-beginning 1))
  13209. level (org-tr-level level)
  13210. txt (save-match-data
  13211. (org-html-expand
  13212. (org-export-cleanup-toc-line
  13213. (match-string 3 line))))
  13214. todo
  13215. (or (and org-export-mark-todo-in-toc
  13216. (match-beginning 2)
  13217. (not (equal (match-string 2 line)
  13218. org-done-string)))
  13219. ; TODO, not DONE
  13220. (and org-export-mark-todo-in-toc
  13221. (= level umax)
  13222. (org-search-todo-below
  13223. line lines level))))
  13224. (if org-export-with-section-numbers
  13225. (setq txt (concat (org-section-number level)
  13226. " " txt)))
  13227. (if (<= level umax)
  13228. (progn
  13229. (setq head-count (+ head-count 1))
  13230. (if (> level org-last-level)
  13231. (progn
  13232. (setq cnt (- level org-last-level))
  13233. (while (>= (setq cnt (1- cnt)) 0)
  13234. (insert "\n<ul>\n<li>"))
  13235. (insert "\n")))
  13236. (if (< level org-last-level)
  13237. (progn
  13238. (setq cnt (- org-last-level level))
  13239. (while (>= (setq cnt (1- cnt)) 0)
  13240. (insert "</li>\n</ul>"))
  13241. (insert "\n")))
  13242. ;; Check for targets
  13243. (while (string-match org-target-regexp line)
  13244. (setq tg (match-string 1 line)
  13245. line (replace-match
  13246. (concat "@<span class=\"target\">" tg "@</span> ")
  13247. t t line))
  13248. (push (cons (org-solidify-link-text tg)
  13249. (format "sec-%d" head-count))
  13250. target-alist))
  13251. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  13252. (setq txt (replace-match "" t t txt)))
  13253. (insert
  13254. (format
  13255. (if todo
  13256. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  13257. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  13258. head-count txt))
  13259. (setq org-last-level level))
  13260. )))
  13261. line)
  13262. lines))
  13263. (while (> org-last-level 0)
  13264. (setq org-last-level (1- org-last-level))
  13265. (insert "</li>\n</ul>\n"))
  13266. ))
  13267. (setq head-count 0)
  13268. (org-init-section-numbers)
  13269. (while (setq line (pop lines) origline line)
  13270. (catch 'nextline
  13271. ;; end of quote section?
  13272. (when (and inquote (string-match "^\\*+" line))
  13273. (insert "</pre>\n")
  13274. (setq inquote nil))
  13275. ;; inside a quote section?
  13276. (when inquote
  13277. (insert (org-html-protect line) "\n")
  13278. (throw 'nextline nil))
  13279. ;; verbatim lines
  13280. (when (and org-export-with-fixed-width
  13281. (string-match "^[ \t]*:\\(.*\\)" line))
  13282. (when (not infixed)
  13283. (setq infixed t)
  13284. (insert "<pre>\n"))
  13285. (insert (org-html-protect (match-string 1 line)) "\n")
  13286. (when (and lines
  13287. (not (string-match "^[ \t]*\\(:.*\\)"
  13288. (car lines))))
  13289. (setq infixed nil)
  13290. (insert "</pre>\n"))
  13291. (throw 'nextline nil))
  13292. ;; make targets to anchors
  13293. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  13294. (cond
  13295. ((match-end 2)
  13296. (setq line (replace-match
  13297. (concat "@<a name=\""
  13298. (org-solidify-link-text (match-string 1 line))
  13299. "\">\\nbsp@</a>")
  13300. t t line)))
  13301. ((and org-export-with-toc (equal (string-to-char line) ?*))
  13302. (setq line (replace-match
  13303. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  13304. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  13305. t t line)))
  13306. (t
  13307. (setq line (replace-match
  13308. (concat "@<a name=\""
  13309. (org-solidify-link-text (match-string 1 line))
  13310. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  13311. t t line)))))
  13312. (setq line (org-html-handle-time-stamps line))
  13313. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  13314. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  13315. ;; Also handle sub_superscripts and checkboxes
  13316. (setq line (org-html-expand line))
  13317. ;; Format the links
  13318. (setq start 0)
  13319. (while (string-match org-bracket-link-analytic-regexp line start)
  13320. (setq start (match-beginning 0))
  13321. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  13322. (setq path (match-string 3 line))
  13323. (setq desc1 (if (match-end 5) (match-string 5 line))
  13324. desc2 (if (match-end 2) (concat type ":" path) path)
  13325. descp (and desc1 (not (equal desc1 desc2)))
  13326. desc (or desc1 desc2))
  13327. ;; FIXME: do we need to unescape here somewhere?
  13328. (cond
  13329. ((equal type "internal")
  13330. (setq rpl
  13331. (concat
  13332. "<a href=\"#"
  13333. (org-solidify-link-text path target-alist)
  13334. "\">" desc "</a>")))
  13335. ((member type '("http" "https" "ftp" "mailto" "news"))
  13336. ;; standard URL
  13337. (setq link (concat type ":" path))
  13338. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  13339. ((string= type "file")
  13340. ;; FILE link
  13341. (let* ((filename path)
  13342. (abs-p (file-name-absolute-p filename))
  13343. thefile file-is-image-p search)
  13344. (save-match-data
  13345. (if (string-match "::\\(.*\\)" filename)
  13346. (setq search (match-string 1 filename)
  13347. filename (replace-match "" t nil filename)))
  13348. (setq valid
  13349. (if (functionp link-validate)
  13350. (funcall link-validate filename current-dir)
  13351. t))
  13352. (setq file-is-image-p
  13353. (string-match (org-image-file-name-regexp) filename))
  13354. (setq thefile (if abs-p (expand-file-name filename) filename))
  13355. (when (and org-export-html-link-org-files-as-html
  13356. (string-match "\\.org$" thefile))
  13357. (setq thefile (concat (substring thefile 0
  13358. (match-beginning 0))
  13359. ".html"))
  13360. (if (and search
  13361. ;; make sure this is can be used as target search
  13362. (not (string-match "^[0-9]*$" search))
  13363. (not (string-match "^\\*" search))
  13364. (not (string-match "^/.*/$" search)))
  13365. (setq thefile (concat thefile "#"
  13366. (org-solidify-link-text
  13367. (org-link-unescape search)))))
  13368. (when (string-match "^file:" desc)
  13369. (setq desc (replace-match "" t t desc))
  13370. (if (string-match "\\.org$" desc)
  13371. (setq desc (replace-match "" t t desc))))))
  13372. (setq rpl (if (and file-is-image-p
  13373. (or (eq t org-export-html-inline-images)
  13374. (and org-export-html-inline-images
  13375. (not descp))))
  13376. (concat "<img src=\"" thefile "\"/>")
  13377. (concat "<a href=\"" thefile "\">" desc "</a>")))
  13378. (if (not valid) (setq rpl desc))))
  13379. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  13380. (setq rpl (concat "<i>&lt;" type ":"
  13381. (save-match-data (org-link-unescape path))
  13382. "&gt;</i>"))))
  13383. (setq line (replace-match rpl t t line)
  13384. start (+ start (length rpl))))
  13385. ;; TODO items
  13386. (if (and (string-match org-todo-line-regexp line)
  13387. (match-beginning 2))
  13388. (if (equal (match-string 2 line) org-done-string)
  13389. (setq line (replace-match
  13390. "<span class=\"done\">\\2</span>"
  13391. t nil line 2))
  13392. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  13393. t nil line 2))))
  13394. (cond
  13395. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  13396. ;; This is a headline
  13397. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  13398. txt (match-string 2 line))
  13399. (if (string-match quote-re0 txt)
  13400. (setq txt (replace-match "" t t txt)))
  13401. (if (<= level umax) (setq head-count (+ head-count 1)))
  13402. (when in-local-list
  13403. ;; Close any local lists before inserting a new header line
  13404. (while local-list-num
  13405. (org-close-li)
  13406. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13407. (pop local-list-num))
  13408. (setq local-list-indent nil
  13409. in-local-list nil))
  13410. (org-html-level-start level txt umax
  13411. (and org-export-with-toc (<= level umax))
  13412. head-count)
  13413. ;; QUOTES
  13414. (when (string-match quote-re line)
  13415. (insert "<pre>")
  13416. (setq inquote t)))
  13417. ((and org-export-with-tables
  13418. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  13419. (if (not table-open)
  13420. ;; New table starts
  13421. (setq table-open t table-buffer nil table-orig-buffer nil))
  13422. ;; Accumulate lines
  13423. (setq table-buffer (cons line table-buffer)
  13424. table-orig-buffer (cons origline table-orig-buffer))
  13425. (when (or (not lines)
  13426. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  13427. (car lines))))
  13428. (setq table-open nil
  13429. table-buffer (nreverse table-buffer)
  13430. table-orig-buffer (nreverse table-orig-buffer))
  13431. (org-close-par-maybe)
  13432. (insert (org-format-table-html table-buffer table-orig-buffer))))
  13433. (t
  13434. ;; Normal lines
  13435. (when (string-match
  13436. (cond
  13437. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13438. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13439. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*] \\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  13440. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  13441. line)
  13442. (setq ind (org-get-string-indentation line)
  13443. start-is-num (match-beginning 4)
  13444. starter (if (match-beginning 2)
  13445. (substring (match-string 2 line) 0 -1))
  13446. line (substring line (match-beginning 5)))
  13447. (unless (string-match "[^ \t]" line)
  13448. ;; empty line. Pretend indentation is large.
  13449. (setq ind (1+ (or (car local-list-indent) 1))))
  13450. (while (and in-local-list
  13451. (or (and (= ind (car local-list-indent))
  13452. (not starter))
  13453. (< ind (car local-list-indent))))
  13454. (org-close-li)
  13455. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  13456. (pop local-list-num) (pop local-list-indent)
  13457. (setq in-local-list local-list-indent))
  13458. (cond
  13459. ((and starter
  13460. (or (not in-local-list)
  13461. (> ind (car local-list-indent))))
  13462. ;; Start new (level of ) list
  13463. (org-close-par-maybe)
  13464. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  13465. (push start-is-num local-list-num)
  13466. (push ind local-list-indent)
  13467. (setq in-local-list t))
  13468. (starter
  13469. ;; continue current list
  13470. (org-close-li)
  13471. (insert "<li>\n")))
  13472. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  13473. (setq line
  13474. (replace-match
  13475. (if (equal (match-string 1 line) "X")
  13476. "<b>[X]</b>"
  13477. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  13478. t t line))))
  13479. ;; Empty lines start a new paragraph. If hand-formatted lists
  13480. ;; are not fully interpreted, lines starting with "-", "+", "*"
  13481. ;; also start a new paragraph.
  13482. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  13483. ;; Check if the line break needs to be conserved
  13484. (cond
  13485. ((string-match "\\\\\\\\[ \t]*$" line)
  13486. (setq line (replace-match "<br/>" t t line)))
  13487. (org-export-preserve-breaks
  13488. (setq line (concat line "<br/>"))))
  13489. (insert line "\n")))))
  13490. ;; Properly close all local lists and other lists
  13491. (when inquote (insert "</pre>\n"))
  13492. (when in-local-list
  13493. ;; Close any local lists before inserting a new header line
  13494. (while local-list-num
  13495. (org-close-li)
  13496. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  13497. (pop local-list-num))
  13498. (setq local-list-indent nil
  13499. in-local-list nil))
  13500. (org-html-level-start 1 nil umax
  13501. (and org-export-with-toc (<= level umax))
  13502. head-count)
  13503. (when (plist-get opt-plist :auto-postamble)
  13504. (when author
  13505. (insert "<p class=\"author\"> "
  13506. (nth 1 lang-words) ": " author "\n")
  13507. (when email
  13508. (insert "<a href=\"mailto:" email "\">&lt;"
  13509. email "&gt;</a>\n"))
  13510. (insert "</p>\n"))
  13511. (when (and date time)
  13512. (insert "<p class=\"date\"> "
  13513. (nth 2 lang-words) ": "
  13514. date " " time "</p>\n")))
  13515. (if org-export-html-with-timestamp
  13516. (insert org-export-html-html-helper-timestamp))
  13517. (insert (or (plist-get opt-plist :postamble) ""))
  13518. (insert "</body>\n</html>\n")
  13519. (normal-mode)
  13520. ;; remove empty paragraphs and lists
  13521. (goto-char (point-min))
  13522. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  13523. (replace-match ""))
  13524. (goto-char (point-min))
  13525. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  13526. (replace-match ""))
  13527. (save-buffer)
  13528. (goto-char (point-min))
  13529. (message "Exporting... done"))))
  13530. (defun org-format-table-html (lines olines)
  13531. "Find out which HTML converter to use and return the HTML code."
  13532. (if (string-match "^[ \t]*|" (car lines))
  13533. ;; A normal org table
  13534. (org-format-org-table-html lines)
  13535. ;; Table made by table.el - test for spanning
  13536. (let* ((hlines (delq nil (mapcar
  13537. (lambda (x)
  13538. (if (string-match "^[ \t]*\\+-" x) x
  13539. nil))
  13540. lines)))
  13541. (first (car hlines))
  13542. (ll (and (string-match "\\S-+" first)
  13543. (match-string 0 first)))
  13544. (re (concat "^[ \t]*" (regexp-quote ll)))
  13545. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  13546. hlines))))
  13547. (if (and (not spanning)
  13548. (not org-export-prefer-native-exporter-for-tables))
  13549. ;; We can use my own converter with HTML conversions
  13550. (org-format-table-table-html lines)
  13551. ;; Need to use the code generator in table.el, with the original text.
  13552. (org-format-table-table-html-using-table-generate-source olines)))))
  13553. (defun org-format-org-table-html (lines)
  13554. "Format a table into HTML."
  13555. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13556. (setq lines (nreverse lines))
  13557. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  13558. (setq lines (nreverse lines))
  13559. (when org-export-table-remove-special-lines
  13560. ;; Check if the table has a marking column. If yes remove the
  13561. ;; column and the special lines
  13562. (let* ((special
  13563. (not
  13564. (memq nil
  13565. (mapcar
  13566. (lambda (x)
  13567. (or (string-match "^[ \t]*|-" x)
  13568. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  13569. lines)))))
  13570. (if special
  13571. (setq lines
  13572. (delq nil
  13573. (mapcar
  13574. (lambda (x)
  13575. (if (string-match "^[ \t]*| *[!_^] *|" x)
  13576. nil ; ignore this line
  13577. (and (or (string-match "^[ \t]*|-+\\+" x)
  13578. (string-match "^[ \t]*|[^|]*|" x))
  13579. (replace-match "|" t t x))))
  13580. lines))))))
  13581. (let ((head (and org-export-highlight-first-table-line
  13582. (delq nil (mapcar
  13583. (lambda (x) (string-match "^[ \t]*|-" x))
  13584. (cdr lines)))))
  13585. line fields html)
  13586. (setq html (concat org-export-html-table-tag "\n"))
  13587. (while (setq line (pop lines))
  13588. (catch 'next-line
  13589. (if (string-match "^[ \t]*|-" line)
  13590. (progn
  13591. (setq head nil) ;; head ends here, first time around
  13592. ;; ignore this line
  13593. (throw 'next-line t)))
  13594. ;; Break the line into fields
  13595. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13596. (setq html (concat
  13597. html
  13598. "<tr>"
  13599. (mapconcat (lambda (x)
  13600. (if head
  13601. (concat "<th>" x "</th>")
  13602. (concat "<td>" x "</td>")))
  13603. fields "")
  13604. "</tr>\n"))))
  13605. (setq html (concat html "</table>\n"))
  13606. html))
  13607. (defun org-fake-empty-table-line (line)
  13608. "Replace everything except \"|\" with spaces."
  13609. (let ((i (length line))
  13610. (newstr (copy-sequence line)))
  13611. (while (> i 0)
  13612. (setq i (1- i))
  13613. (if (not (eq (aref newstr i) ?|))
  13614. (aset newstr i ?\ )))
  13615. newstr))
  13616. (defun org-format-table-table-html (lines)
  13617. "Format a table generated by table.el into HTML.
  13618. This conversion does *not* use `table-generate-source' from table.el.
  13619. This has the advantage that Org-mode's HTML conversions can be used.
  13620. But it has the disadvantage, that no cell- or row-spanning is allowed."
  13621. (let (line field-buffer
  13622. (head org-export-highlight-first-table-line)
  13623. fields html empty)
  13624. (setq html (concat org-export-html-table-tag "\n"))
  13625. (while (setq line (pop lines))
  13626. (setq empty "&nbsp;")
  13627. (catch 'next-line
  13628. (if (string-match "^[ \t]*\\+-" line)
  13629. (progn
  13630. (if field-buffer
  13631. (progn
  13632. (setq html (concat
  13633. html
  13634. "<tr>"
  13635. (mapconcat
  13636. (lambda (x)
  13637. (if (equal x "") (setq x empty))
  13638. (if head
  13639. (concat "<th>" x "</th>\n")
  13640. (concat "<td>" x "</td>\n")))
  13641. field-buffer "\n")
  13642. "</tr>\n"))
  13643. (setq head nil)
  13644. (setq field-buffer nil)))
  13645. ;; Ignore this line
  13646. (throw 'next-line t)))
  13647. ;; Break the line into fields and store the fields
  13648. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  13649. (if field-buffer
  13650. (setq field-buffer (mapcar
  13651. (lambda (x)
  13652. (concat x "<br/>" (pop fields)))
  13653. field-buffer))
  13654. (setq field-buffer fields))))
  13655. (setq html (concat html "</table>\n"))
  13656. html))
  13657. (defun org-format-table-table-html-using-table-generate-source (lines)
  13658. "Format a table into html, using `table-generate-source' from table.el.
  13659. This has the advantage that cell- or row-spanning is allowed.
  13660. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  13661. (require 'table)
  13662. (with-current-buffer (get-buffer-create " org-tmp1 ")
  13663. (erase-buffer)
  13664. (insert (mapconcat 'identity lines "\n"))
  13665. (goto-char (point-min))
  13666. (if (not (re-search-forward "|[^+]" nil t))
  13667. (error "Error processing table"))
  13668. (table-recognize-table)
  13669. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  13670. (table-generate-source 'html " org-tmp2 ")
  13671. (set-buffer " org-tmp2 ")
  13672. (buffer-substring (point-min) (point-max))))
  13673. (defun org-html-handle-time-stamps (s)
  13674. "Format time stamps in string S, or remove them."
  13675. (catch 'exit
  13676. (let (r b)
  13677. (while (string-match org-maybe-keyword-time-regexp s)
  13678. ;; FIXME: is it good to never export CLOCK, or do we need control?
  13679. (if (and (match-end 1) (equal (match-string 1 s) org-clock-string))
  13680. (throw 'exit ""))
  13681. (or b (setq b (substring s 0 (match-beginning 0))))
  13682. (if (not org-export-with-timestamps)
  13683. (setq r (concat r (substring s 0 (match-beginning 0)))
  13684. s (substring s (match-end 0)))
  13685. (setq r (concat
  13686. r (substring s 0 (match-beginning 0))
  13687. (if (match-end 1)
  13688. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  13689. (match-string 1 s)))
  13690. (format " @<span class=\"timestamp\">%s@</span>"
  13691. (substring (match-string 3 s) 1 -1)))
  13692. s (substring s (match-end 0)))))
  13693. ;; Line break if line started and ended with time stamp stuff
  13694. (if (not r)
  13695. s
  13696. (setq r (concat r s))
  13697. (unless (string-match "\\S-" (concat b s))
  13698. (setq r (concat r "@<br/>")))
  13699. r))))
  13700. (defun org-html-protect (s)
  13701. ;; convert & to &amp;, < to &lt; and > to &gt;
  13702. (let ((start 0))
  13703. (while (string-match "&" s start)
  13704. (setq s (replace-match "&amp;" t t s)
  13705. start (1+ (match-beginning 0))))
  13706. (while (string-match "<" s)
  13707. (setq s (replace-match "&lt;" t t s)))
  13708. (while (string-match ">" s)
  13709. (setq s (replace-match "&gt;" t t s))))
  13710. s)
  13711. (defun org-export-cleanup-toc-line (s)
  13712. "Remove tags and time staps from lines going into the toc."
  13713. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  13714. (setq s (replace-match "" t t s)))
  13715. (when org-export-remove-timestamps-from-toc
  13716. (while (string-match org-maybe-keyword-time-regexp s)
  13717. (setq s (replace-match "" t t s))))
  13718. s)
  13719. (defun org-html-expand (string)
  13720. "Prepare STRING for HTML export. Applies all active conversions.
  13721. If there are links in the string, don't modify these."
  13722. (let* (m s l res)
  13723. (while (setq m (string-match org-bracket-link-regexp string))
  13724. (setq s (substring string 0 m)
  13725. l (match-string 0 string)
  13726. string (substring string (match-end 0)))
  13727. (push (org-html-do-expand s) res)
  13728. (push l res))
  13729. (push (org-html-do-expand string) res)
  13730. (apply 'concat (nreverse res))))
  13731. (defun org-html-do-expand (s)
  13732. "Apply all active conversions to translate special ASCII to HTML."
  13733. (setq s (org-html-protect s))
  13734. (if org-export-html-expand
  13735. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  13736. (setq s (replace-match "<\\1>" t nil s))))
  13737. (if org-export-with-emphasize
  13738. (setq s (org-export-html-convert-emphasize s)))
  13739. (if org-export-with-sub-superscripts
  13740. (setq s (org-export-html-convert-sub-super s)))
  13741. (if org-export-with-TeX-macros
  13742. (let ((start 0) wd ass)
  13743. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  13744. (setq wd (match-string 1 s))
  13745. (if (setq ass (assoc wd org-html-entities))
  13746. (setq s (replace-match (or (cdr ass)
  13747. (concat "&" (car ass) ";"))
  13748. t t s))
  13749. (setq start (+ start (length wd)))))))
  13750. s)
  13751. (defun org-create-multibrace-regexp (left right n)
  13752. "Create a regular expression which will match a balanced sexp.
  13753. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  13754. as single character strings.
  13755. The regexp returned will match the entire expression including the
  13756. delimiters. It will also define a single group which contains the
  13757. match except for the outermost delimiters. The maximum depth of
  13758. stacked delimiters is N. Escaping delimiters is not possible."
  13759. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  13760. (or "\\|")
  13761. (re nothing)
  13762. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  13763. (while (> n 1)
  13764. (setq n (1- n)
  13765. re (concat re or next)
  13766. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  13767. (concat left "\\(" re "\\)" right)))
  13768. (defvar org-match-substring-regexp
  13769. (concat
  13770. "\\([^\\]\\)\\([_^]\\)\\("
  13771. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  13772. "\\|"
  13773. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  13774. "\\|"
  13775. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  13776. "The regular expression matching a sub- or superscript.")
  13777. (defun org-export-html-convert-sub-super (string)
  13778. "Convert sub- and superscripts in STRING to HTML."
  13779. (let (key c)
  13780. (while (string-match org-match-substring-regexp string)
  13781. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  13782. (setq c (or (match-string 8 string)
  13783. (match-string 6 string)
  13784. (match-string 5 string)))
  13785. (setq string (replace-match
  13786. (concat (match-string 1 string)
  13787. "<" key ">" c "</" key ">")
  13788. t t string)))
  13789. (while (string-match "\\\\\\([_^]\\)" string)
  13790. (setq string (replace-match (match-string 1 string) t t string))))
  13791. string)
  13792. (defun org-export-html-convert-emphasize (string)
  13793. "Apply emphasis."
  13794. (while (string-match org-emph-re string)
  13795. (setq string (replace-match (concat "\\1" (nth 2 (assoc (match-string 3 string) org-emphasis-alist)) "\\4" (nth 3 (assoc (match-string 3 string) org-emphasis-alist)) "\\5") t nil string)))
  13796. string)
  13797. (defvar org-par-open nil)
  13798. (defun org-open-par ()
  13799. "Insert <p>, but first close previous paragraph if any."
  13800. (org-close-par-maybe)
  13801. (insert "\n<p>")
  13802. (setq org-par-open t))
  13803. (defun org-close-par-maybe ()
  13804. "Close paragraph if there is one open."
  13805. (when org-par-open
  13806. (insert "</p>")
  13807. (setq org-par-open nil)))
  13808. (defun org-close-li ()
  13809. "Close <li> if necessary."
  13810. (org-close-par-maybe)
  13811. (insert "</li>\n"))
  13812. ; (when (save-excursion
  13813. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  13814. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  13815. ; (insert "</li>"))))
  13816. (defun org-html-level-start (level title umax with-toc head-count)
  13817. "Insert a new level in HTML export.
  13818. When TITLE is nil, just close all open levels."
  13819. (org-close-par-maybe)
  13820. (let ((l (1+ (max level umax))))
  13821. (while (<= l org-level-max)
  13822. (if (aref levels-open (1- l))
  13823. (progn
  13824. (org-html-level-close l)
  13825. (aset levels-open (1- l) nil)))
  13826. (setq l (1+ l)))
  13827. (when title
  13828. ;; If title is nil, this means this function is called to close
  13829. ;; all levels, so the rest is done only if title is given
  13830. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  13831. (setq title (replace-match
  13832. (if org-export-with-tags
  13833. (save-match-data
  13834. (concat
  13835. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  13836. (mapconcat 'identity (org-split-string
  13837. (match-string 1 title) ":")
  13838. "&nbsp;")
  13839. "</span>"))
  13840. "")
  13841. t t title)))
  13842. (if (> level umax)
  13843. (progn
  13844. (if (aref levels-open (1- level))
  13845. (progn
  13846. (org-close-li)
  13847. (insert "<li>" title "<br/>\n"))
  13848. (aset levels-open (1- level) t)
  13849. (org-close-par-maybe)
  13850. (insert "<ul>\n<li>" title "<br/>\n")))
  13851. (if org-export-with-section-numbers
  13852. (setq title (concat (org-section-number level) " " title)))
  13853. (setq level (+ level org-export-html-toplevel-hlevel -1))
  13854. (if with-toc
  13855. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  13856. level head-count title level))
  13857. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  13858. (org-open-par)))))
  13859. (defun org-html-level-close (&rest args)
  13860. "Terminate one level in HTML export."
  13861. (org-close-li)
  13862. (insert "</ul>"))
  13863. ;; Variable holding the vector with section numbers
  13864. (defvar org-section-numbers (make-vector org-level-max 0))
  13865. (defun org-init-section-numbers ()
  13866. "Initialize the vector for the section numbers."
  13867. (let* ((level -1)
  13868. (numbers (nreverse (org-split-string "" "\\.")))
  13869. (depth (1- (length org-section-numbers)))
  13870. (i depth) number-string)
  13871. (while (>= i 0)
  13872. (if (> i level)
  13873. (aset org-section-numbers i 0)
  13874. (setq number-string (or (car numbers) "0"))
  13875. (if (string-match "\\`[A-Z]\\'" number-string)
  13876. (aset org-section-numbers i
  13877. (- (string-to-char number-string) ?A -1))
  13878. (aset org-section-numbers i (string-to-number number-string)))
  13879. (pop numbers))
  13880. (setq i (1- i)))))
  13881. (defun org-section-number (&optional level)
  13882. "Return a string with the current section number.
  13883. When LEVEL is non-nil, increase section numbers on that level."
  13884. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  13885. (when level
  13886. (when (> level -1)
  13887. (aset org-section-numbers
  13888. level (1+ (aref org-section-numbers level))))
  13889. (setq idx (1+ level))
  13890. (while (<= idx depth)
  13891. (if (not (= idx 1))
  13892. (aset org-section-numbers idx 0))
  13893. (setq idx (1+ idx))))
  13894. (setq idx 0)
  13895. (while (<= idx depth)
  13896. (setq n (aref org-section-numbers idx))
  13897. (setq string (concat string (if (not (string= string "")) "." "")
  13898. (int-to-string n)))
  13899. (setq idx (1+ idx)))
  13900. (save-match-data
  13901. (if (string-match "\\`\\([@0]\\.\\)+" string)
  13902. (setq string (replace-match "" t nil string)))
  13903. (if (string-match "\\(\\.0\\)+\\'" string)
  13904. (setq string (replace-match "" t nil string))))
  13905. string))
  13906. ;;;###autoload
  13907. (defun org-export-icalendar-this-file ()
  13908. "Export current file as an iCalendar file.
  13909. The iCalendar file will be located in the same directory as the Org-mode
  13910. file, but with extension `.ics'."
  13911. (interactive)
  13912. (org-export-icalendar nil buffer-file-name))
  13913. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  13914. (with-current-buffer buffer
  13915. (apply 'insert output)))
  13916. (put 'org-export-as-xoxo-insert-into 'lisp-indent-function 1)
  13917. (defun org-export-as-xoxo (&optional buffer)
  13918. "Export the org buffer as XOXO.
  13919. The XOXO buffer is named *xoxo-<source buffer name>*"
  13920. (interactive (list (current-buffer)))
  13921. ;; A quickie abstraction
  13922. ;; Output everything as XOXO
  13923. (with-current-buffer (get-buffer buffer)
  13924. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  13925. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  13926. (org-infile-export-plist)))
  13927. (filename (concat (file-name-as-directory
  13928. (org-export-directory :xoxo opt-plist))
  13929. (file-name-sans-extension
  13930. (file-name-nondirectory buffer-file-name))
  13931. ".html"))
  13932. (out (find-file-noselect filename))
  13933. (last-level 1)
  13934. (hanging-li nil))
  13935. ;; Check the output buffer is empty.
  13936. (with-current-buffer out (erase-buffer))
  13937. ;; Kick off the output
  13938. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  13939. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  13940. (let* ((hd (match-string-no-properties 1))
  13941. (level (length hd))
  13942. (text (concat
  13943. (match-string-no-properties 2)
  13944. (save-excursion
  13945. (goto-char (match-end 0))
  13946. (let ((str ""))
  13947. (catch 'loop
  13948. (while 't
  13949. (forward-line)
  13950. (if (looking-at "^[ \t]\\(.*\\)")
  13951. (setq str (concat str (match-string-no-properties 1)))
  13952. (throw 'loop str)))))))))
  13953. ;; Handle level rendering
  13954. (cond
  13955. ((> level last-level)
  13956. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  13957. ((< level last-level)
  13958. (dotimes (- (- last-level level) 1)
  13959. (if hanging-li
  13960. (org-export-as-xoxo-insert-into out "</li>\n"))
  13961. (org-export-as-xoxo-insert-into out "</ol>\n"))
  13962. (when hanging-li
  13963. (org-export-as-xoxo-insert-into out "</li>\n")
  13964. (setq hanging-li nil)))
  13965. ((equal level last-level)
  13966. (if hanging-li
  13967. (org-export-as-xoxo-insert-into out "</li>\n")))
  13968. )
  13969. (setq last-level level)
  13970. ;; And output the new li
  13971. (setq hanging-li 't)
  13972. (if (equal ?+ (elt text 0))
  13973. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  13974. (org-export-as-xoxo-insert-into out "<li>" text))))
  13975. ;; Finally finish off the ol
  13976. (dotimes (- last-level 1)
  13977. (if hanging-li
  13978. (org-export-as-xoxo-insert-into out "</li>\n"))
  13979. (org-export-as-xoxo-insert-into out "</ol>\n"))
  13980. ;; Finish the buffer off and clean it up.
  13981. (switch-to-buffer-other-window out)
  13982. (indent-region (point-min) (point-max) nil)
  13983. (save-buffer)
  13984. (goto-char (point-min))
  13985. )))
  13986. ;;;###autoload
  13987. (defun org-export-icalendar-all-agenda-files ()
  13988. "Export all files in `org-agenda-files' to iCalendar .ics files.
  13989. Each iCalendar file will be located in the same directory as the Org-mode
  13990. file, but with extension `.ics'."
  13991. (interactive)
  13992. (apply 'org-export-icalendar nil (org-agenda-files t)))
  13993. ;;;###autoload
  13994. (defun org-export-icalendar-combine-agenda-files ()
  13995. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  13996. The file is stored under the name `org-combined-agenda-icalendar-file'."
  13997. (interactive)
  13998. (apply 'org-export-icalendar t (org-agenda-files t)))
  13999. (defun org-export-icalendar (combine &rest files)
  14000. "Create iCalendar files for all elements of FILES.
  14001. If COMBINE is non-nil, combine all calendar entries into a single large
  14002. file and store it under the name `org-combined-agenda-icalendar-file'."
  14003. (save-excursion
  14004. (let* ((dir (org-export-directory
  14005. :ical (list :publishing-directory
  14006. org-export-publishing-directory)))
  14007. file ical-file ical-buffer category started org-agenda-new-buffers)
  14008. (when combine
  14009. (setq ical-file
  14010. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  14011. org-combined-agenda-icalendar-file
  14012. (expand-file-name org-combined-agenda-icalendar-file dir))
  14013. ical-buffer (org-get-agenda-file-buffer ical-file))
  14014. (set-buffer ical-buffer) (erase-buffer))
  14015. (while (setq file (pop files))
  14016. (catch 'nextfile
  14017. (org-check-agenda-file file)
  14018. (set-buffer (org-get-agenda-file-buffer file))
  14019. (unless combine
  14020. (setq ical-file (concat (file-name-as-directory dir)
  14021. (file-name-sans-extension
  14022. (file-name-nondirectory buffer-file-name))
  14023. ".ics"))
  14024. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  14025. (with-current-buffer ical-buffer (erase-buffer)))
  14026. (setq category (or org-category
  14027. (file-name-sans-extension
  14028. (file-name-nondirectory buffer-file-name))))
  14029. (if (symbolp category) (setq category (symbol-name category)))
  14030. (let ((standard-output ical-buffer))
  14031. (if combine
  14032. (and (not started) (setq started t)
  14033. (org-start-icalendar-file org-icalendar-combined-name))
  14034. (org-start-icalendar-file category))
  14035. (org-print-icalendar-entries combine category)
  14036. (when (or (and combine (not files)) (not combine))
  14037. (org-finish-icalendar-file)
  14038. (set-buffer ical-buffer)
  14039. (save-buffer)
  14040. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  14041. (org-release-buffers org-agenda-new-buffers))))
  14042. (defvar org-after-save-iCalendar-file-hook nil
  14043. "Hook run after an iCalendar file has been saved.
  14044. The iCalendar buffer is still current when this hook is run.
  14045. A good way to use this is to tell a desktop calenndar application to re-read
  14046. the iCalendar file.")
  14047. (defun org-print-icalendar-entries (&optional combine category)
  14048. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  14049. When COMBINE is non nil, add the category to each line."
  14050. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  14051. (dts (org-ical-ts-to-string
  14052. (format-time-string (cdr org-time-stamp-formats) (current-time))
  14053. "DTSTART"))
  14054. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  14055. (save-excursion
  14056. (goto-char (point-min))
  14057. (while (re-search-forward org-ts-regexp nil t)
  14058. (setq pos (match-beginning 0)
  14059. ts (match-string 0)
  14060. inc t
  14061. hd (org-get-heading))
  14062. (if (looking-at re2)
  14063. (progn
  14064. (goto-char (match-end 0))
  14065. (setq ts2 (match-string 1) inc nil))
  14066. (setq ts2 ts
  14067. tmp (buffer-substring (max (point-min)
  14068. (- pos org-ds-keyword-length))
  14069. pos)
  14070. deadlinep (string-match org-deadline-regexp tmp)
  14071. scheduledp (string-match org-scheduled-regexp tmp)
  14072. ;; donep (org-entry-is-done-p)
  14073. ))
  14074. (if (or (string-match org-tr-regexp hd)
  14075. (string-match org-ts-regexp hd))
  14076. (setq hd (replace-match "" t t hd)))
  14077. (if combine
  14078. (setq hd (concat hd " (category " category ")")))
  14079. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  14080. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  14081. (princ (format "BEGIN:VEVENT
  14082. %s
  14083. %s
  14084. SUMMARY:%s
  14085. END:VEVENT\n"
  14086. (org-ical-ts-to-string ts "DTSTART")
  14087. (org-ical-ts-to-string ts2 "DTEND" inc)
  14088. hd)))
  14089. (when org-icalendar-include-todo
  14090. (goto-char (point-min))
  14091. (while (re-search-forward org-todo-line-regexp nil t)
  14092. (setq state (match-string 1))
  14093. (unless (equal state org-done-string)
  14094. (setq hd (match-string 3))
  14095. (if (string-match org-priority-regexp hd)
  14096. (setq pri (string-to-char (match-string 2 hd))
  14097. hd (concat (substring hd 0 (match-beginning 1))
  14098. (substring hd (- (match-end 1)))))
  14099. (setq pri org-default-priority))
  14100. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  14101. (- org-lowest-priority ?A))))))
  14102. (princ (format "BEGIN:VTODO
  14103. %s
  14104. SUMMARY:%s
  14105. SEQUENCE:1
  14106. PRIORITY:%d
  14107. END:VTODO\n"
  14108. dts hd pri))))))))
  14109. (defun org-start-icalendar-file (name)
  14110. "Start an iCalendar file by inserting the header."
  14111. (let ((user user-full-name)
  14112. (name (or name "unknown"))
  14113. (timezone (cadr (current-time-zone))))
  14114. (princ
  14115. (format "BEGIN:VCALENDAR
  14116. VERSION:2.0
  14117. X-WR-CALNAME:%s
  14118. PRODID:-//%s//Emacs with Org-mode//EN
  14119. X-WR-TIMEZONE:%s
  14120. CALSCALE:GREGORIAN\n" name user timezone))))
  14121. (defun org-finish-icalendar-file ()
  14122. "Finish an iCalendar file by inserting the END statement."
  14123. (princ "END:VCALENDAR\n"))
  14124. (defun org-ical-ts-to-string (s keyword &optional inc)
  14125. "Take a time string S and convert it to iCalendar format.
  14126. KEYWORD is added in front, to make a complete line like DTSTART....
  14127. When INC is non-nil, increase the hour by two (if time string contains
  14128. a time), or the day by one (if it does not contain a time)."
  14129. (let ((t1 (org-parse-time-string s 'nodefault))
  14130. t2 fmt have-time time)
  14131. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  14132. (setq t2 t1 have-time t)
  14133. (setq t2 (org-parse-time-string s)))
  14134. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  14135. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  14136. (when inc
  14137. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  14138. (setq time (encode-time s mi h d m y)))
  14139. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  14140. (concat keyword (format-time-string fmt time))))
  14141. ;;; LaTeX stuff
  14142. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14143. "Keymap for the minor `org-cdlatex-mode'.")
  14144. (define-key org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14145. (define-key org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14146. (define-key org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14147. (define-key org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14148. (define-key org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14149. (defvar org-cdlatex-texmathp-advice-is-done nil
  14150. "Flag remembering if we have applied the advice to texmathp already.")
  14151. (define-minor-mode org-cdlatex-mode
  14152. "Toggle the minor `org-cdlatex-mode'.
  14153. This mode supports entering LaTeX environment and math in LaTeX fragments
  14154. in Org-mode.
  14155. \\{org-cdlatex-mode-map}"
  14156. nil " OCDL" nil
  14157. (when org-cdlatex-mode (require 'cdlatex))
  14158. (unless org-cdlatex-texmathp-advice-is-done
  14159. (setq org-cdlatex-texmathp-advice-is-done t)
  14160. (defadvice texmathp (around org-math-always-on activate)
  14161. "Always return t in org-mode buffers.
  14162. This is because we want to insert math symbols without dollars even outside
  14163. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14164. en embedded LaTeX fragement, let texmathp do its job.
  14165. \\[org-cdlatex-mode-map]"
  14166. (interactive)
  14167. (let (p)
  14168. (cond
  14169. ((not (org-mode-p)) ad-do-it)
  14170. ((eq this-command 'cdlatex-math-symbol)
  14171. (setq ad-return-value t
  14172. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14173. (t
  14174. (let ((p (org-inside-LaTeX-fragment-p)))
  14175. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14176. (setq ad-return-value t
  14177. texmathp-why '("Org-mode embedded math" . 0))
  14178. (if p ad-do-it)))))))))
  14179. (defun turn-on-org-cdlatex ()
  14180. "Unconditionally turn on `org-cdlatex-mode'."
  14181. (org-cdlatex-mode 1))
  14182. (defun org-inside-LaTeX-fragment-p ()
  14183. "Test if point is inside a LaTeX fragment.
  14184. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14185. sequence appearing also before point.
  14186. Even though the matchers for math are configurable, this function assumes
  14187. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14188. delimiters are skipped when they have been removed by customization.
  14189. The return value is nil, or a cons cell with the delimiter and
  14190. and the position of this delimiter.
  14191. This function does a reasonably good job, but can locally be fooled by
  14192. for example currency specifications. For example it will assume being in
  14193. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14194. fragments that are properly closed, but during editing, we have to live
  14195. with the uncertainty caused by missing closing delimiters. This function
  14196. looks only before point, not after."
  14197. (catch 'exit
  14198. (let ((pos (point))
  14199. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14200. (lim (progn
  14201. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14202. (point)))
  14203. dd-on str (start 0) m re)
  14204. (goto-char pos)
  14205. (when dodollar
  14206. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14207. re (nth 1 (assoc "$" org-latex-regexps)))
  14208. (while (string-match re str start)
  14209. (cond
  14210. ((= (match-end 0) (length str))
  14211. (throw 'exit (cons "$" (+ lim (match-beginning 0)))))
  14212. ((= (match-end 0) (- (length str) 5))
  14213. (throw 'exit nil))
  14214. (t (setq start (match-end 0))))))
  14215. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14216. (goto-char pos)
  14217. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14218. (and (match-beginning 2) (throw 'exit nil))
  14219. ;; count $$
  14220. (while (re-search-backward "\\$\\$" lim t)
  14221. (setq dd-on (not dd-on)))
  14222. (goto-char pos)
  14223. (if dd-on (cons "$$" m))))))
  14224. (defun org-try-cdlatex-tab ()
  14225. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14226. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14227. - inside a LaTeX fragment, or
  14228. - after the first word in a line, where an abbreviation expansion could
  14229. insert a LaTeX environment."
  14230. ;; FIXME: This may still need refinement.
  14231. (when org-cdlatex-mode
  14232. (cond
  14233. ((save-excursion
  14234. (skip-chars-backward "a-zA-Z0-9*")
  14235. (skip-chars-backward " \t")
  14236. (bolp))
  14237. (cdlatex-tab) t)
  14238. ((org-inside-LaTeX-fragment-p)
  14239. (cdlatex-tab) t)
  14240. (t nil))))
  14241. (defun org-cdlatex-underscore-caret (&optional arg)
  14242. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14243. Revert to the normal definition outside of these fragments."
  14244. (interactive "P")
  14245. (if (org-inside-LaTeX-fragment-p)
  14246. (call-interactively 'cdlatex-sub-superscript)
  14247. (let (org-cdlatex-mode)
  14248. (call-interactively (key-binding (vector last-input-event))))))
  14249. (defun org-cdlatex-math-modify (&optional arg)
  14250. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14251. Revert to the normal definition outside of these fragments."
  14252. (interactive "P")
  14253. (if (org-inside-LaTeX-fragment-p)
  14254. (call-interactively 'cdlatex-math-modify)
  14255. (let (org-cdlatex-mode)
  14256. (call-interactively (key-binding (vector last-input-event))))))
  14257. (defvar org-latex-fragment-image-overlays nil
  14258. "List of overlays carrying the images of latex fragments.")
  14259. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14260. (defun org-remove-latex-fragment-image-overlays ()
  14261. "Remove all overlays with LaTeX fragment images in current buffer."
  14262. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  14263. (setq org-latex-fragment-image-overlays nil))
  14264. (defun org-preview-latex-fragment (&optional subtree)
  14265. "Preview the LaTeX fragment at point, or all locally or globally.
  14266. If the cursor is in a LaTeX fragment, create the image and overlay
  14267. it over the source code. If there is no fragment at point, display
  14268. all fragments in the current text, from one headline to the next. With
  14269. prefix SUBTREE, display all fragments in the current subtree. With a
  14270. double prefix `C-u C-u', or when the cursor is before the first headline,
  14271. display all fragments in the buffer.
  14272. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14273. (interactive "P")
  14274. (org-remove-latex-fragment-image-overlays)
  14275. (save-excursion
  14276. (save-restriction
  14277. (let (beg end at msg)
  14278. (cond
  14279. ((or (equal subtree '(16))
  14280. (not (save-excursion
  14281. (re-search-backward (concat "^" outline-regexp) nil t))))
  14282. (setq beg (point-min) end (point-max)
  14283. msg "Creating images for buffer...%s"))
  14284. ((equal subtree '(4))
  14285. (org-back-to-heading)
  14286. (setq beg (point) end (org-end-of-subtree)
  14287. msg "Creating images for subtree...%s"))
  14288. (t
  14289. (if (setq at (org-inside-LaTeX-fragment-p))
  14290. (goto-char (max (point-min) (- (cdr at) 2)))
  14291. (org-back-to-heading))
  14292. (setq beg (point) end (progn (outline-next-heading) (point))
  14293. msg (if at "Creating image...%s"
  14294. "Creating images for entry...%s"))))
  14295. (message msg "")
  14296. (narrow-to-region beg end)
  14297. (org-format-latex
  14298. (concat "ltxpng/" (file-name-sans-extension
  14299. (file-name-nondirectory
  14300. buffer-file-name)))
  14301. default-directory 'overlays msg at)
  14302. (message msg "done. Use `C-c C-c' to remove images.")))))
  14303. (defvar org-latex-regexps
  14304. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14305. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14306. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14307. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  14308. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14309. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  14310. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  14311. "Regular expressions for matching embedded LaTeX.")
  14312. (defun org-format-latex (prefix &optional dir overlays msg at)
  14313. "Replace LaTeX fragments with links to an image, and produce images."
  14314. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14315. (let* ((prefixnodir (file-name-nondirectory prefix))
  14316. (absprefix (expand-file-name prefix dir))
  14317. (todir (file-name-directory absprefix))
  14318. (opt org-format-latex-options)
  14319. (matchers (plist-get opt :matchers))
  14320. (re-list org-latex-regexps)
  14321. (cnt 0) txt link beg end re e oldfiles
  14322. m n block linkfile movefile ov)
  14323. ;; Make sure the directory exists
  14324. (or (file-directory-p todir) (make-directory todir))
  14325. ;; Check if there are old images files with this prefix, and remove them
  14326. (setq oldfiles (directory-files
  14327. todir 'full
  14328. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$")))
  14329. (while oldfiles (delete-file (pop oldfiles)))
  14330. ;; Check the different regular expressions
  14331. (while (setq e (pop re-list))
  14332. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14333. block (if (nth 3 e) "\n\n" ""))
  14334. (when (member m matchers)
  14335. (goto-char (point-min))
  14336. (while (re-search-forward re nil t)
  14337. (when (or (not at) (equal (cdr at) (match-beginning n)))
  14338. (setq txt (match-string n)
  14339. beg (match-beginning n) end (match-end n)
  14340. cnt (1+ cnt)
  14341. linkfile (format "%s_%04d.png" prefix cnt)
  14342. movefile (format "%s_%04d.png" absprefix cnt)
  14343. link (concat block "[[file:" linkfile "]]" block))
  14344. (if msg (message msg cnt))
  14345. (goto-char beg)
  14346. (org-create-formula-image
  14347. txt movefile opt)
  14348. (if overlays
  14349. (progn
  14350. (setq ov (org-make-overlay beg end))
  14351. (if (featurep 'xemacs)
  14352. (progn
  14353. (org-overlay-put ov 'invisible t)
  14354. (org-overlay-put
  14355. ov 'end-glyph
  14356. (make-glyph (vector 'png :file movefile))))
  14357. (org-overlay-put
  14358. ov 'display
  14359. (list 'image :type 'png :file movefile :ascent 'center)))
  14360. (push ov org-latex-fragment-image-overlays)
  14361. (goto-char end))
  14362. (delete-region beg end)
  14363. (insert link))))))))
  14364. ;; This function borrows from Ganesh Swami's latex2png.el
  14365. (defun org-create-formula-image (string tofile options)
  14366. (let* ((tmpdir (if (featurep 'xemacs)
  14367. (temp-directory)
  14368. temporary-file-directory))
  14369. (texfilebase (make-temp-name
  14370. (expand-file-name "orgtex" tmpdir)))
  14371. ;(texfilebase (make-temp-file "orgtex"))
  14372. ; (dummy (delete-file texfilebase))
  14373. (texfile (concat texfilebase ".tex"))
  14374. (dvifile (concat texfilebase ".dvi"))
  14375. (pngfile (concat texfilebase ".png"))
  14376. (scale (number-to-string (* 1000 (or (plist-get options :scale) 1.0))))
  14377. (fg (or (plist-get options :foreground) "Black"))
  14378. (bg (or (plist-get options :background) "Transparent")))
  14379. (with-temp-file texfile
  14380. (insert "\\documentclass{article}
  14381. \\usepackage{fullpage}
  14382. \\usepackage{amssymb}
  14383. \\usepackage[usenames]{color}
  14384. \\usepackage{amsmath}
  14385. \\usepackage{latexsym}
  14386. \\usepackage[mathscr]{eucal}
  14387. \\pagestyle{empty}
  14388. \\begin{document}\n" string "\n\\end{document}\n"))
  14389. (let ((dir default-directory))
  14390. (condition-case nil
  14391. (progn
  14392. (cd tmpdir)
  14393. (call-process "latex" nil nil nil texfile))
  14394. (error nil))
  14395. (cd dir))
  14396. (if (not (file-exists-p dvifile))
  14397. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14398. (call-process "dvipng" nil nil nil
  14399. "-E" "-fg" fg "-bg" bg
  14400. "-x" scale "-y" scale "-T" "tight"
  14401. "-o" pngfile
  14402. dvifile)
  14403. (if (not (file-exists-p pngfile))
  14404. (progn (message "Failed to create png file from %s" texfile) nil)
  14405. ;; Use the requested file name and clean up
  14406. (copy-file pngfile tofile 'replace)
  14407. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14408. (delete-file (concat texfilebase e)))
  14409. pngfile))))
  14410. ;;; Key bindings
  14411. ;; - Bindings in Org-mode map are currently
  14412. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  14413. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  14414. ;; e (?) useful from outline-mode
  14415. ;; i k @ expendable from outline-mode
  14416. ;; 0123456789 % & ()_{} " ` free
  14417. ;; Make `C-c C-x' a prefix key
  14418. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  14419. ;; TAB key with modifiers
  14420. (define-key org-mode-map "\C-i" 'org-cycle)
  14421. (define-key org-mode-map [(tab)] 'org-cycle)
  14422. (define-key org-mode-map [(control tab)] 'org-force-cycle-archived)
  14423. (define-key org-mode-map [(meta tab)] 'org-complete)
  14424. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  14425. ;; The following line is necessary under Suse GNU/Linux
  14426. (unless (featurep 'xemacs)
  14427. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  14428. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  14429. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  14430. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  14431. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  14432. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  14433. (define-key org-mode-map [(meta return)] 'org-meta-return)
  14434. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  14435. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  14436. ;; Cursor keys with modifiers
  14437. (define-key org-mode-map [(meta left)] 'org-metaleft)
  14438. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  14439. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  14440. (define-key org-mode-map [(meta right)] 'org-metaright)
  14441. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  14442. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  14443. (define-key org-mode-map [(meta up)] 'org-metaup)
  14444. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  14445. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  14446. (define-key org-mode-map [(meta down)] 'org-metadown)
  14447. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  14448. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  14449. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  14450. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  14451. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  14452. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  14453. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  14454. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  14455. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  14456. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  14457. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  14458. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  14459. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  14460. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  14461. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  14462. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  14463. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  14464. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  14465. ;; All the other keys
  14466. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  14467. (define-key org-mode-map "\C-xns" 'org-narrow-to-subtree)
  14468. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  14469. (define-key org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  14470. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  14471. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  14472. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  14473. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  14474. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  14475. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  14476. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  14477. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  14478. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  14479. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  14480. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  14481. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  14482. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  14483. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  14484. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  14485. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  14486. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  14487. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  14488. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  14489. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  14490. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  14491. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  14492. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  14493. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  14494. (define-key org-mode-map "\C-c]" 'org-remove-file)
  14495. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  14496. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  14497. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  14498. (define-key org-mode-map "\C-c#" 'org-update-checkbox-count)
  14499. (define-key org-mode-map "\C-m" 'org-return)
  14500. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  14501. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  14502. (define-key org-mode-map "\C-c+" 'org-table-sum)
  14503. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  14504. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  14505. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  14506. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  14507. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  14508. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  14509. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  14510. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  14511. (define-key org-mode-map "\C-c\C-e" 'org-export)
  14512. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  14513. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  14514. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  14515. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  14516. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  14517. (define-key org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  14518. (define-key org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  14519. (define-key org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  14520. (define-key org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  14521. (define-key org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  14522. (define-key org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  14523. (define-key org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  14524. (define-key org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  14525. (when (featurep 'xemacs)
  14526. (define-key org-mode-map 'button3 'popup-mode-menu))
  14527. (defsubst org-table-p () (org-at-table-p))
  14528. (defun org-self-insert-command (N)
  14529. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  14530. If the cursor is in a table looking at whitespace, the whitespace is
  14531. overwritten, and the table is not marked as requiring realignment."
  14532. (interactive "p")
  14533. (if (and (org-table-p)
  14534. (progn
  14535. ;; check if we blank the field, and if that triggers align
  14536. (and org-table-auto-blank-field
  14537. (member last-command
  14538. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  14539. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  14540. ;; got extra space, this field does not determine column width
  14541. (let (org-table-may-need-update) (org-table-blank-field))
  14542. ;; no extra space, this field may determine column width
  14543. (org-table-blank-field)))
  14544. t)
  14545. (eq N 1)
  14546. (looking-at "[^|\n]* |"))
  14547. (let (org-table-may-need-update)
  14548. (goto-char (1- (match-end 0)))
  14549. (delete-backward-char 1)
  14550. (goto-char (match-beginning 0))
  14551. (self-insert-command N))
  14552. (setq org-table-may-need-update t)
  14553. (self-insert-command N)))
  14554. (defun org-delete-backward-char (N)
  14555. "Like `delete-backward-char', insert whitespace at field end in tables.
  14556. When deleting backwards, in tables this function will insert whitespace in
  14557. front of the next \"|\" separator, to keep the table aligned. The table will
  14558. still be marked for re-alignment if the field did fill the entire column,
  14559. because, in this case the deletion might narrow the column."
  14560. (interactive "p")
  14561. (if (and (org-table-p)
  14562. (eq N 1)
  14563. (string-match "|" (buffer-substring (point-at-bol) (point)))
  14564. (looking-at ".*?|"))
  14565. (let ((pos (point))
  14566. (noalign (looking-at "[^|\n\r]* |"))
  14567. (c org-table-may-need-update))
  14568. (backward-delete-char N)
  14569. (skip-chars-forward "^|")
  14570. (insert " ")
  14571. (goto-char (1- pos))
  14572. ;; noalign: if there were two spaces at the end, this field
  14573. ;; does not determine the width of the column.
  14574. (if noalign (setq org-table-may-need-update c)))
  14575. (backward-delete-char N)))
  14576. (defun org-delete-char (N)
  14577. "Like `delete-char', but insert whitespace at field end in tables.
  14578. When deleting characters, in tables this function will insert whitespace in
  14579. front of the next \"|\" separator, to keep the table aligned. The table will
  14580. still be marked for re-alignment if the field did fill the entire column,
  14581. because, in this case the deletion might narrow the column."
  14582. (interactive "p")
  14583. (if (and (org-table-p)
  14584. (not (bolp))
  14585. (not (= (char-after) ?|))
  14586. (eq N 1))
  14587. (if (looking-at ".*?|")
  14588. (let ((pos (point))
  14589. (noalign (looking-at "[^|\n\r]* |"))
  14590. (c org-table-may-need-update))
  14591. (replace-match (concat
  14592. (substring (match-string 0) 1 -1)
  14593. " |"))
  14594. (goto-char pos)
  14595. ;; noalign: if there were two spaces at the end, this field
  14596. ;; does not determine the width of the column.
  14597. (if noalign (setq org-table-may-need-update c)))
  14598. (delete-char N))
  14599. (delete-char N)))
  14600. ;; How to do this: Measure non-white length of current string
  14601. ;; If equal to column width, we should realign.
  14602. (defun org-remap (map &rest commands)
  14603. "In MAP, remap the functions given in COMMANDS.
  14604. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  14605. (let (new old)
  14606. (while commands
  14607. (setq old (pop commands) new (pop commands))
  14608. (if (fboundp 'command-remapping)
  14609. (define-key map (vector 'remap old) new)
  14610. (substitute-key-definition old new map global-map)))))
  14611. (when (eq org-enable-table-editor 'optimized)
  14612. ;; If the user wants maximum table support, we need to hijack
  14613. ;; some standard editing functions
  14614. (org-remap org-mode-map
  14615. 'self-insert-command 'org-self-insert-command
  14616. 'delete-char 'org-delete-char
  14617. 'delete-backward-char 'org-delete-backward-char)
  14618. (define-key org-mode-map "|" 'org-force-self-insert))
  14619. (defun org-shiftcursor-error ()
  14620. "Throw an error because Shift-Cursor command was applied in wrong context."
  14621. (error "This command is active in special context like tables, headlines or timestamps"))
  14622. (defun org-shifttab (&optional arg)
  14623. "Global visibility cycling or move to previous table field.
  14624. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  14625. on context.
  14626. See the individual commands for more information."
  14627. (interactive "P")
  14628. (cond
  14629. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  14630. (t (call-interactively 'org-global-cycle))))
  14631. (defun org-shiftmetaleft ()
  14632. "Promote subtree or delete table column.
  14633. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  14634. See the individual commands for more information."
  14635. (interactive)
  14636. (cond
  14637. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  14638. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  14639. ((org-at-item-p) (call-interactively 'org-outdent-item))
  14640. (t (org-shiftcursor-error))))
  14641. (defun org-shiftmetaright ()
  14642. "Demote subtree or insert table column.
  14643. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  14644. See the individual commands for more information."
  14645. (interactive)
  14646. (cond
  14647. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  14648. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  14649. ((org-at-item-p) (call-interactively 'org-indent-item))
  14650. (t (org-shiftcursor-error))))
  14651. (defun org-shiftmetaup (&optional arg)
  14652. "Move subtree up or kill table row.
  14653. Calls `org-move-subtree-up' or `org-table-kill-row' or
  14654. `org-move-item-up' depending on context. See the individual commands
  14655. for more information."
  14656. (interactive "P")
  14657. (cond
  14658. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  14659. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14660. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14661. (t (org-shiftcursor-error))))
  14662. (defun org-shiftmetadown (&optional arg)
  14663. "Move subtree down or insert table row.
  14664. Calls `org-move-subtree-down' or `org-table-insert-row' or
  14665. `org-move-item-down', depending on context. See the individual
  14666. commands for more information."
  14667. (interactive "P")
  14668. (cond
  14669. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  14670. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14671. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14672. (t (org-shiftcursor-error))))
  14673. (defun org-metaleft (&optional arg)
  14674. "Promote heading or move table column to left.
  14675. Calls `org-do-promote' or `org-table-move-column', depending on context.
  14676. With no specific context, calls the Emacs default `backward-word'.
  14677. See the individual commands for more information."
  14678. (interactive "P")
  14679. (cond
  14680. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  14681. ((or (org-on-heading-p) (org-region-active-p))
  14682. (call-interactively 'org-do-promote))
  14683. (t (call-interactively 'backward-word))))
  14684. (defun org-metaright (&optional arg)
  14685. "Demote subtree or move table column to right.
  14686. Calls `org-do-demote' or `org-table-move-column', depending on context.
  14687. With no specific context, calls the Emacs default `forward-word'.
  14688. See the individual commands for more information."
  14689. (interactive "P")
  14690. (cond
  14691. ((org-at-table-p) (call-interactively 'org-table-move-column))
  14692. ((or (org-on-heading-p) (org-region-active-p))
  14693. (call-interactively 'org-do-demote))
  14694. (t (call-interactively 'forward-word))))
  14695. (defun org-metaup (&optional arg)
  14696. "Move subtree up or move table row up.
  14697. Calls `org-move-subtree-up' or `org-table-move-row' or
  14698. `org-move-item-up', depending on context. See the individual commands
  14699. for more information."
  14700. (interactive "P")
  14701. (cond
  14702. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  14703. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  14704. ((org-at-item-p) (call-interactively 'org-move-item-up))
  14705. (t (org-shiftcursor-error))))
  14706. (defun org-metadown (&optional arg)
  14707. "Move subtree down or move table row down.
  14708. Calls `org-move-subtree-down' or `org-table-move-row' or
  14709. `org-move-item-down', depending on context. See the individual
  14710. commands for more information."
  14711. (interactive "P")
  14712. (cond
  14713. ((org-at-table-p) (call-interactively 'org-table-move-row))
  14714. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  14715. ((org-at-item-p) (call-interactively 'org-move-item-down))
  14716. (t (org-shiftcursor-error))))
  14717. (defun org-shiftup (&optional arg)
  14718. "Increase item in timestamp or increase priority of current headline.
  14719. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  14720. See the individual commands for more information."
  14721. (interactive "P")
  14722. (cond
  14723. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up))
  14724. ((org-on-heading-p) (call-interactively 'org-priority-up))
  14725. ((org-at-item-p) (call-interactively 'org-previous-item))
  14726. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  14727. (defun org-shiftdown (&optional arg)
  14728. "Decrease item in timestamp or decrease priority of current headline.
  14729. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  14730. See the individual commands for more information."
  14731. (interactive "P")
  14732. (cond
  14733. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down))
  14734. ((org-on-heading-p) (call-interactively 'org-priority-down))
  14735. (t (call-interactively 'org-next-item))))
  14736. (defun org-shiftright ()
  14737. "Next TODO keyword or timestamp one day later, depending on context."
  14738. (interactive)
  14739. (cond
  14740. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  14741. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  14742. (t (org-shiftcursor-error))))
  14743. (defun org-shiftleft ()
  14744. "Previous TODO keyword or timestamp one day earlier, depending on context."
  14745. (interactive)
  14746. (cond
  14747. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  14748. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  14749. (t (org-shiftcursor-error))))
  14750. (defun org-copy-special ()
  14751. "Copy region in table or copy current subtree.
  14752. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  14753. See the individual commands for more information."
  14754. (interactive)
  14755. (call-interactively
  14756. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  14757. (defun org-cut-special ()
  14758. "Cut region in table or cut current subtree.
  14759. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  14760. See the individual commands for more information."
  14761. (interactive)
  14762. (call-interactively
  14763. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  14764. (defun org-paste-special (arg)
  14765. "Paste rectangular region into table, or past subtree relative to level.
  14766. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  14767. See the individual commands for more information."
  14768. (interactive "P")
  14769. (if (org-at-table-p)
  14770. (org-table-paste-rectangle)
  14771. (org-paste-subtree arg)))
  14772. (defun org-ctrl-c-ctrl-c (&optional arg)
  14773. "Set tags in headline, or update according to changed information at point.
  14774. This command does many different things, depending on context:
  14775. - If the cursor is in a headline, prompt for tags and insert them
  14776. into the current line, aligned to `org-tags-column'. When called
  14777. with prefix arg, realign all tags in the current buffer.
  14778. - If the cursor is in one of the special #+KEYWORD lines, this
  14779. triggers scanning the buffer for these lines and updating the
  14780. information.
  14781. - If the cursor is inside a table, realign the table. This command
  14782. works even if the automatic table editor has been turned off.
  14783. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  14784. the entire table.
  14785. - If the cursor is inside a table created by the table.el package,
  14786. activate that table.
  14787. - If the current buffer is a remember buffer, close note and file it.
  14788. with a prefix argument, file it without further interaction to the default
  14789. location.
  14790. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  14791. links in this buffer.
  14792. - If the cursor is on a numbered item in a plain list, renumber the
  14793. ordered list."
  14794. (interactive "P")
  14795. (let ((org-enable-table-editor t))
  14796. (cond
  14797. ((or org-clock-overlays org-occur-highlights
  14798. org-latex-fragment-image-overlays)
  14799. (org-remove-clock-overlays)
  14800. (org-remove-occur-highlights)
  14801. (org-remove-latex-fragment-image-overlays)
  14802. (message "Temporary highlights/overlays removed from current buffer"))
  14803. ((and (local-variable-p 'org-finish-function (current-buffer))
  14804. (fboundp org-finish-function))
  14805. (funcall org-finish-function))
  14806. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  14807. ((org-on-heading-p) (call-interactively 'org-set-tags))
  14808. ((org-at-table.el-p)
  14809. (require 'table)
  14810. (beginning-of-line 1)
  14811. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  14812. (call-interactively 'table-recognize-table))
  14813. ((org-at-table-p)
  14814. (org-table-maybe-eval-formula)
  14815. (if arg
  14816. (call-interactively 'org-table-recalculate)
  14817. (org-table-maybe-recalculate-line))
  14818. (call-interactively 'org-table-align))
  14819. ((org-at-item-checkbox-p)
  14820. (call-interactively 'org-toggle-checkbox))
  14821. ((org-at-item-p)
  14822. (call-interactively 'org-renumber-ordered-list))
  14823. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  14824. (cond
  14825. ((equal (match-string 1) "TBLFM")
  14826. ;; Recalculate the table before this line
  14827. (save-excursion
  14828. (beginning-of-line 1)
  14829. (skip-chars-backward " \r\n\t")
  14830. (if (org-at-table-p)
  14831. (org-call-with-arg 'org-table-recalculate t))))
  14832. (t
  14833. (call-interactively 'org-mode-restart))))
  14834. (t (error "C-c C-c can do nothing useful at this location.")))))
  14835. (defun org-mode-restart ()
  14836. "Restart Org-mode, to scan again for special lines.
  14837. Also updates the keyword regular expressions."
  14838. (interactive)
  14839. (let ((org-inhibit-startup t)) (org-mode))
  14840. (message "Org-mode restarted to refresh keyword and special line setup"))
  14841. (defun org-return ()
  14842. "Goto next table row or insert a newline.
  14843. Calls `org-table-next-row' or `newline', depending on context.
  14844. See the individual commands for more information."
  14845. (interactive)
  14846. (cond
  14847. ((org-at-table-p)
  14848. (org-table-justify-field-maybe)
  14849. (call-interactively 'org-table-next-row))
  14850. (t (newline))))
  14851. (defun org-meta-return (&optional arg)
  14852. "Insert a new heading or wrap a region in a table.
  14853. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  14854. See the individual commands for more information."
  14855. (interactive "P")
  14856. (cond
  14857. ((org-at-table-p)
  14858. (call-interactively 'org-table-wrap-region))
  14859. (t (call-interactively 'org-insert-heading))))
  14860. ;;; Menu entries
  14861. ;; Define the Org-mode menus
  14862. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  14863. '("Tbl"
  14864. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  14865. ["Next Field" org-cycle (org-at-table-p)]
  14866. ["Previous Field" org-shifttab (org-at-table-p)]
  14867. ["Next Row" org-return (org-at-table-p)]
  14868. "--"
  14869. ["Blank Field" org-table-blank-field (org-at-table-p)]
  14870. ["Edit Field" org-table-edit-field (org-at-table-p)]
  14871. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  14872. "--"
  14873. ("Column"
  14874. ["Move Column Left" org-metaleft (org-at-table-p)]
  14875. ["Move Column Right" org-metaright (org-at-table-p)]
  14876. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  14877. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  14878. "--"
  14879. ["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])
  14880. ("Row"
  14881. ["Move Row Up" org-metaup (org-at-table-p)]
  14882. ["Move Row Down" org-metadown (org-at-table-p)]
  14883. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  14884. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  14885. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  14886. "--"
  14887. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  14888. ("Rectangle"
  14889. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  14890. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  14891. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  14892. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  14893. "--"
  14894. ("Calculate"
  14895. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  14896. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  14897. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  14898. "--"
  14899. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  14900. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  14901. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  14902. "--"
  14903. ["Sum Column/Rectangle" org-table-sum
  14904. (or (org-at-table-p) (org-region-active-p))]
  14905. ["Which Column?" org-table-current-column (org-at-table-p)])
  14906. ["Debug Formulas"
  14907. (setq org-table-formula-debug (not org-table-formula-debug))
  14908. :style toggle :selected org-table-formula-debug]
  14909. "--"
  14910. ["Create" org-table-create (and (not (org-at-table-p))
  14911. org-enable-table-editor)]
  14912. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  14913. ["Import from File" org-table-import (not (org-at-table-p))]
  14914. ["Export to File" org-table-export (org-at-table-p)]
  14915. "--"
  14916. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  14917. (easy-menu-define org-org-menu org-mode-map "Org menu"
  14918. '("Org"
  14919. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  14920. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  14921. ["Sparse Tree" org-occur t]
  14922. ["Show All" show-all t]
  14923. "--"
  14924. ["New Heading" org-insert-heading t]
  14925. ("Navigate Headings"
  14926. ["Up" outline-up-heading t]
  14927. ["Next" outline-next-visible-heading t]
  14928. ["Previous" outline-previous-visible-heading t]
  14929. ["Next Same Level" outline-forward-same-level t]
  14930. ["Previous Same Level" outline-backward-same-level t]
  14931. "--"
  14932. ["Jump" org-goto t])
  14933. ("Edit Structure"
  14934. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  14935. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  14936. "--"
  14937. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  14938. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  14939. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  14940. "--"
  14941. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  14942. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  14943. ["Demote Heading" org-metaright (not (org-at-table-p))]
  14944. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  14945. "--"
  14946. ["Convert to odd levels" org-convert-to-odd-levels t]
  14947. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  14948. ("Archive"
  14949. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  14950. ["Check and Tag Children" (org-toggle-archive-tag (4))
  14951. :active t :keys "C-u C-c C-x C-a"]
  14952. ["Sparse trees open ARCHIVE trees"
  14953. (setq org-sparse-tree-open-archived-trees
  14954. (not org-sparse-tree-open-archived-trees))
  14955. :style toggle :selected org-sparse-tree-open-archived-trees]
  14956. ["Cycling opens ARCHIVE trees"
  14957. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  14958. :style toggle :selected org-cycle-open-archived-trees]
  14959. ["Agenda includes ARCHIVE trees"
  14960. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  14961. :style toggle :selected (not org-agenda-skip-archived-trees)]
  14962. "--"
  14963. ["Move Subtree to Archive" org-archive-subtree t]
  14964. ["Check and Move Children" (org-archive-subtree '(4))
  14965. :active t :keys "C-u C-c $"])
  14966. "--"
  14967. ("TODO Lists"
  14968. ["TODO/DONE/-" org-todo t]
  14969. ("Select keyword"
  14970. ["Next keyword" org-shiftright (org-on-heading-p)]
  14971. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  14972. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))])
  14973. ["Show TODO Tree" org-show-todo-tree t]
  14974. ["Global TODO list" org-todo-list t]
  14975. "--"
  14976. ["Set Priority" org-priority t]
  14977. ["Priority Up" org-shiftup t]
  14978. ["Priority Down" org-shiftdown t]
  14979. "--"
  14980. ["Insert Checkbox" org-insert-todo-heading (org-in-item-p) t]
  14981. ["Toggle Checkbox" org-ctrl-c-ctrl-c (org-at-item-checkbox-p)]
  14982. ["Insert [n/m] cookie" (progn (insert "[/]") (org-update-checkbox-count))
  14983. (or (org-on-heading-p) (org-at-item-p))]
  14984. ["Insert [%] cookie" (progn (insert "[%]") (org-update-checkbox-count))
  14985. (or (org-on-heading-p) (org-at-item-p))]
  14986. ["Update Statistics" org-update-checkbox-count t])
  14987. ("Dates and Scheduling"
  14988. ["Timestamp" org-time-stamp t]
  14989. ["Timestamp (inactive)" org-time-stamp-inactive t]
  14990. ("Change Date"
  14991. ["1 Day Later" org-timestamp-up-day t]
  14992. ["1 Day Earlier" org-timestamp-down-day t]
  14993. ["1 ... Later" org-shiftup t]
  14994. ["1 ... Earlier" org-shiftdown t])
  14995. ["Compute Time Range" org-evaluate-time-range t]
  14996. ["Schedule Item" org-schedule t]
  14997. ["Deadline" org-deadline t]
  14998. "--"
  14999. ["Goto Calendar" org-goto-calendar t]
  15000. ["Date from Calendar" org-date-from-calendar t])
  15001. ("Logging work"
  15002. ["Clock in" org-clock-in t]
  15003. ["Clock out" org-clock-out t]
  15004. ["Clock cancel" org-clock-cancel t]
  15005. ["Display times" org-clock-display t]
  15006. ["Create clock table" org-clock-report t]
  15007. "--"
  15008. ["Record DONE time"
  15009. (progn (setq org-log-done (not org-log-done))
  15010. (message "Switching to %s will %s record a timestamp"
  15011. org-done-string
  15012. (if org-log-done "automatically" "not")))
  15013. :style toggle :selected org-log-done])
  15014. "--"
  15015. ["Agenda Command..." org-agenda t]
  15016. ("File List for Agenda")
  15017. ("Special views current file"
  15018. ["TODO Tree" org-show-todo-tree t]
  15019. ["Check Deadlines" org-check-deadlines t]
  15020. ["Timeline" org-timeline t]
  15021. ["Tags Tree" org-tags-sparse-tree t])
  15022. "--"
  15023. ("Hyperlinks"
  15024. ["Store Link (Global)" org-store-link t]
  15025. ["Insert Link" org-insert-link t]
  15026. ["Follow Link" org-open-at-point t]
  15027. "--"
  15028. ["Descriptive Links"
  15029. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  15030. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  15031. ["Literal Links"
  15032. (progn
  15033. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  15034. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  15035. "--"
  15036. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  15037. (save-excursion (goto-char (point-min))
  15038. (re-search-forward "<[a-z]+:" nil t))])
  15039. "--"
  15040. ["Export/Publish..." org-export t]
  15041. ("LaTeX"
  15042. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  15043. :selected org-cdlatex-mode]
  15044. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  15045. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  15046. ["Modify math symbol" org-cdlatex-math-modify
  15047. (org-inside-LaTeX-fragment-p)]
  15048. ["Export LaTeX fragments as images"
  15049. (setq org-export-with-LaTeX-fragments (not org-export-with-LaTeX-fragments))
  15050. :style toggle :selected org-export-with-LaTeX-fragments])
  15051. "--"
  15052. ("Documentation"
  15053. ["Show Version" org-version t]
  15054. ["Info Documentation" org-info t])
  15055. ("Customize"
  15056. ["Browse Org Group" org-customize t]
  15057. "--"
  15058. ["Expand This Menu" org-create-customize-menu
  15059. (fboundp 'customize-menu-create)])
  15060. "--"
  15061. ["Refresh setup" org-mode-restart t]
  15062. ))
  15063. (defun org-info (&optional node)
  15064. "Read documentation for Org-mode in the info system.
  15065. With optional NODE, go directly to that node."
  15066. (interactive)
  15067. (require 'info)
  15068. (Info-goto-node (format "(org)%s" (or node ""))))
  15069. (defun org-install-agenda-files-menu ()
  15070. (let ((bl (buffer-list)))
  15071. (save-excursion
  15072. (while bl
  15073. (set-buffer (pop bl))
  15074. (if (org-mode-p) (setq bl nil)))
  15075. (when (org-mode-p)
  15076. (easy-menu-change
  15077. '("Org") "File List for Agenda"
  15078. (append
  15079. (list
  15080. ["Edit File List" (org-edit-agenda-file-list) t]
  15081. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  15082. ["Remove Current File from List" org-remove-file t]
  15083. ["Cycle through agenda files" org-cycle-agenda-files t]
  15084. "--")
  15085. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  15086. ;;; Documentation
  15087. (defun org-customize ()
  15088. "Call the customize function with org as argument."
  15089. (interactive)
  15090. (customize-browse 'org))
  15091. (defun org-create-customize-menu ()
  15092. "Create a full customization menu for Org-mode, insert it into the menu."
  15093. (interactive)
  15094. (if (fboundp 'customize-menu-create)
  15095. (progn
  15096. (easy-menu-change
  15097. '("Org") "Customize"
  15098. `(["Browse Org group" org-customize t]
  15099. "--"
  15100. ,(customize-menu-create 'org)
  15101. ["Set" Custom-set t]
  15102. ["Save" Custom-save t]
  15103. ["Reset to Current" Custom-reset-current t]
  15104. ["Reset to Saved" Custom-reset-saved t]
  15105. ["Reset to Standard Settings" Custom-reset-standard t]))
  15106. (message "\"Org\"-menu now contains full customization menu"))
  15107. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  15108. ;;; Miscellaneous stuff
  15109. (defun org-context ()
  15110. "Return a list of contexts of the current cursor position.
  15111. If several contexts apply, all are returned.
  15112. Each context entry is a list with a symbol naming the context, and
  15113. two positions indicating start and end of the context. Possible
  15114. contexts are:
  15115. :headline anywhere in a headline
  15116. :headline-stars on the leading stars in a headline
  15117. :todo-keyword on a TODO keyword (including DONE) in a headline
  15118. :tags on the TAGS in a headline
  15119. :priority on the priority cookie in a headline
  15120. :item on the first line of a plain list item
  15121. :item-bullet on the bullet/number of a plain list item
  15122. :checkbox on the checkbox in a plain list item
  15123. :table in an org-mode table
  15124. :table-special on a special filed in a table
  15125. :table-table in a table.el table
  15126. :link on a hyperline
  15127. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  15128. :target on a <<target>>
  15129. :radio-target on a <<<radio-target>>>
  15130. :latex-fragment on a LaTeX fragment
  15131. :latex-preview on a LaTeX fragment with overlayed preview image
  15132. This function expects the position to be visible because it uses font-lock
  15133. faces as a help to recognize the following contexts: :table-special, :link,
  15134. and :keyword."
  15135. (let* ((f (get-text-property (point) 'face))
  15136. (faces (if (listp f) f (list f)))
  15137. (p (point)) clist o)
  15138. ;; First the large context
  15139. (cond
  15140. ((org-on-heading-p)
  15141. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  15142. (when (progn
  15143. (beginning-of-line 1)
  15144. (looking-at org-todo-line-tags-regexp))
  15145. (push (org-point-in-group p 1 :headline-stars) clist)
  15146. (push (org-point-in-group p 2 :todo-keyword) clist)
  15147. (push (org-point-in-group p 4 :tags) clist))
  15148. (goto-char p)
  15149. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  15150. (if (looking-at "\\[#[A-Z]\\]")
  15151. (push (org-point-in-group p 0 :priority) clist)))
  15152. ((org-at-item-p)
  15153. (push (org-point-in-group p 2 :item-bullet) clist)
  15154. (push (list :item (point-at-bol)
  15155. (save-excursion (org-end-of-item) (point)))
  15156. clist)
  15157. (and (org-at-item-checkbox-p)
  15158. (push (org-point-in-group p 0 :checkbox) clist)))
  15159. ((org-at-table-p)
  15160. (push (list :table (org-table-begin) (org-table-end)) clist)
  15161. (if (memq 'org-formula faces)
  15162. (push (list :table-special
  15163. (previous-single-property-change p 'face)
  15164. (next-single-property-change p 'face)) clist)))
  15165. ((org-at-table-p 'any)
  15166. (push (list :table-table) clist)))
  15167. (goto-char p)
  15168. ;; Now the small context
  15169. (cond
  15170. ((org-at-timestamp-p)
  15171. (push (org-point-in-group p 0 :timestamp) clist))
  15172. ((memq 'org-link faces)
  15173. (push (list :link
  15174. (previous-single-property-change p 'face)
  15175. (next-single-property-change p 'face)) clist))
  15176. ((memq 'org-special-keyword faces)
  15177. (push (list :keyword
  15178. (previous-single-property-change p 'face)
  15179. (next-single-property-change p 'face)) clist))
  15180. ((org-on-target-p)
  15181. (push (org-point-in-group p 0 :target) clist)
  15182. (goto-char (1- (match-beginning 0)))
  15183. (if (looking-at org-radio-target-regexp)
  15184. (push (org-point-in-group p 0 :radio-target) clist))
  15185. (goto-char p))
  15186. ((setq o (car (delq nil
  15187. (mapcar
  15188. (lambda (x)
  15189. (if (memq x org-latex-fragment-image-overlays) x))
  15190. (org-overlays-at (point))))))
  15191. (push (list :latex-fragment
  15192. (org-overlay-start o) (org-overlay-end o)) clist)
  15193. (push (list :latex-preview
  15194. (org-overlay-start o) (org-overlay-end o)) clist))
  15195. ((org-inside-LaTeX-fragment-p)
  15196. ;; FIXME: positions wring.
  15197. (push (list :latex-fragment (point) (point)) clist)))
  15198. (setq clist (nreverse (delq nil clist)))
  15199. clist))
  15200. (defun org-point-in-group (point group &optional context)
  15201. "Check if POINT is in match-group GROUP.
  15202. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  15203. match. If the match group does ot exist or point is not inside it,
  15204. return nil."
  15205. (and (match-beginning group)
  15206. (>= point (match-beginning group))
  15207. (<= point (match-end group))
  15208. (if context
  15209. (list context (match-beginning group) (match-end group))
  15210. t)))
  15211. (defun org-move-line-down (arg)
  15212. "Move the current line down. With prefix argument, move it past ARG lines."
  15213. (interactive "p")
  15214. (let ((col (current-column))
  15215. beg end pos)
  15216. (beginning-of-line 1) (setq beg (point))
  15217. (beginning-of-line 2) (setq end (point))
  15218. (beginning-of-line (+ 1 arg))
  15219. (setq pos (move-marker (make-marker) (point)))
  15220. (insert (delete-and-extract-region beg end))
  15221. (goto-char pos)
  15222. (move-to-column col)))
  15223. (defun org-move-line-up (arg)
  15224. "Move the current line up. With prefix argument, move it past ARG lines."
  15225. (interactive "p")
  15226. (let ((col (current-column))
  15227. beg end pos)
  15228. (beginning-of-line 1) (setq beg (point))
  15229. (beginning-of-line 2) (setq end (point))
  15230. (beginning-of-line (- arg))
  15231. (setq pos (move-marker (make-marker) (point)))
  15232. (insert (delete-and-extract-region beg end))
  15233. (goto-char pos)
  15234. (move-to-column col)))
  15235. ;; Paragraph filling stuff.
  15236. ;; We want this to be just right, so use the full arsenal.
  15237. (defun org-set-autofill-regexps ()
  15238. (interactive)
  15239. ;; In the paragraph separator we include headlines, because filling
  15240. ;; text in a line directly attached to a headline would otherwise
  15241. ;; fill the headline as well.
  15242. (org-set-local 'comment-start-skip "^#+[ \t]*")
  15243. (org-set-local 'paragraph-separate "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  15244. ;; The paragraph starter includes hand-formatted lists.
  15245. (org-set-local 'paragraph-start
  15246. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  15247. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  15248. ;; But only if the user has not turned off tables or fixed-width regions
  15249. (org-set-local
  15250. 'auto-fill-inhibit-regexp
  15251. (concat "\\*\\|#"
  15252. "\\|[ \t]*" org-keyword-time-regexp
  15253. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  15254. (concat
  15255. "\\|[ \t]*["
  15256. (if org-enable-table-editor "|" "")
  15257. (if org-enable-fixed-width-editor ":" "")
  15258. "]"))))
  15259. ;; We use our own fill-paragraph function, to make sure that tables
  15260. ;; and fixed-width regions are not wrapped. That function will pass
  15261. ;; through to `fill-paragraph' when appropriate.
  15262. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  15263. ; Adaptive filling: To get full control, first make sure that
  15264. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  15265. (org-set-local 'adaptive-fill-regexp "\000")
  15266. (org-set-local 'adaptive-fill-function
  15267. 'org-adaptive-fill-function))
  15268. (defun org-fill-paragraph (&optional justify)
  15269. "Re-align a table, pass through to fill-paragraph if no table."
  15270. (let ((table-p (org-at-table-p))
  15271. (table.el-p (org-at-table.el-p)))
  15272. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  15273. (table.el-p t) ; skip table.el tables
  15274. (table-p (org-table-align) t) ; align org-mode tables
  15275. (t nil)))) ; call paragraph-fill
  15276. ;; For reference, this is the default value of adaptive-fill-regexp
  15277. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  15278. (defun org-adaptive-fill-function ()
  15279. "Return a fill prefix for org-mode files.
  15280. In particular, this makes sure hanging paragraphs for hand-formatted lists
  15281. work correctly."
  15282. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  15283. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  15284. ;; Functions needed for Emacs/XEmacs region compatibility
  15285. (defun org-add-hook (hook function &optional append local)
  15286. "Add-hook, compatible with both Emacsen."
  15287. (if (and local (featurep 'xemacs))
  15288. (add-local-hook hook function append)
  15289. (add-hook hook function append local)))
  15290. (defun org-region-active-p ()
  15291. "Is `transient-mark-mode' on and the region active?
  15292. Works on both Emacs and XEmacs."
  15293. (if org-ignore-region
  15294. nil
  15295. (if (featurep 'xemacs)
  15296. (and zmacs-regions (region-active-p))
  15297. (and transient-mark-mode mark-active))))
  15298. (defun org-add-to-invisibility-spec (arg)
  15299. "Add elements to `buffer-invisibility-spec'.
  15300. See documentation for `buffer-invisibility-spec' for the kind of elements
  15301. that can be added."
  15302. (cond
  15303. ((fboundp 'add-to-invisibility-spec)
  15304. (add-to-invisibility-spec arg))
  15305. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  15306. (setq buffer-invisibility-spec (list arg)))
  15307. (t
  15308. (setq buffer-invisibility-spec
  15309. (cons arg buffer-invisibility-spec)))))
  15310. (defun org-remove-from-invisibility-spec (arg)
  15311. "Remove elements from `buffer-invisibility-spec'."
  15312. (if (fboundp 'remove-from-invisibility-spec)
  15313. (remove-from-invisibility-spec arg)
  15314. (if (consp buffer-invisibility-spec)
  15315. (setq buffer-invisibility-spec
  15316. (delete arg buffer-invisibility-spec)))))
  15317. (defun org-in-invisibility-spec-p (arg)
  15318. "Is ARG a member of `buffer-invisibility-spec'?"
  15319. (if (consp buffer-invisibility-spec)
  15320. (member arg buffer-invisibility-spec)
  15321. nil))
  15322. (defun org-image-file-name-regexp ()
  15323. "Return regexp matching the file names of images."
  15324. (if (fboundp 'image-file-name-regexp)
  15325. (image-file-name-regexp)
  15326. (let ((image-file-name-extensions
  15327. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  15328. "xbm" "xpm" "pbm" "pgm" "ppm")))
  15329. (concat "\\."
  15330. (regexp-opt (nconc (mapcar 'upcase
  15331. image-file-name-extensions)
  15332. image-file-name-extensions)
  15333. t)
  15334. "\\'"))))
  15335. ;; Functions extending outline functionality
  15336. ;; C-a should go to the beginning of a *visible* line, also in the
  15337. ;; new outline.el. I guess this should be patched into Emacs?
  15338. (defun org-beginning-of-line ()
  15339. "Go to the beginning of the current line. If that is invisible, continue
  15340. to a visible line beginning. This makes the function of C-a more intuitive."
  15341. (interactive)
  15342. (beginning-of-line 1)
  15343. (if (bobp)
  15344. nil
  15345. (backward-char 1)
  15346. (if (org-invisible-p)
  15347. (while (and (not (bobp)) (org-invisible-p))
  15348. (backward-char 1)
  15349. (beginning-of-line 1))
  15350. (forward-char 1))))
  15351. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  15352. (defun org-invisible-p ()
  15353. "Check if point is at a character currently not visible."
  15354. ;; Early versions of noutline don't have `outline-invisible-p'.
  15355. (if (fboundp 'outline-invisible-p)
  15356. (outline-invisible-p)
  15357. (get-char-property (point) 'invisible)))
  15358. (defun org-invisible-p2 ()
  15359. "Check if point is at a character currently not visible."
  15360. (save-excursion
  15361. (if (and (eolp) (not (bobp))) (backward-char 1))
  15362. ;; Early versions of noutline don't have `outline-invisible-p'.
  15363. (if (fboundp 'outline-invisible-p)
  15364. (outline-invisible-p)
  15365. (get-char-property (point) 'invisible))))
  15366. (defalias 'org-back-to-heading 'outline-back-to-heading)
  15367. (defalias 'org-on-heading-p 'outline-on-heading-p)
  15368. (defun org-on-target-p ()
  15369. (let ((pos (point)))
  15370. (save-excursion
  15371. (skip-chars-forward "<")
  15372. (and (re-search-backward "<<" nil t)
  15373. (or (looking-at org-radio-target-regexp)
  15374. (looking-at org-target-regexp))
  15375. (<= (match-beginning 0) pos)
  15376. (>= (1+ (match-end 0)) pos)))))
  15377. (defun org-up-heading-all (arg)
  15378. "Move to the heading line of which the present line is a subheading.
  15379. This function considers both visible and invisible heading lines.
  15380. With argument, move up ARG levels."
  15381. (if (fboundp 'outline-up-heading-all)
  15382. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  15383. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  15384. (defun org-show-hidden-entry ()
  15385. "Show an entry where even the heading is hidden."
  15386. (save-excursion
  15387. (org-show-entry)))
  15388. (defun org-flag-heading (flag &optional entry)
  15389. "Flag the current heading. FLAG non-nil means make invisible.
  15390. When ENTRY is non-nil, show the entire entry."
  15391. (save-excursion
  15392. (org-back-to-heading t)
  15393. ;; Check if we should show the entire entry
  15394. (if entry
  15395. (progn
  15396. (org-show-entry)
  15397. (save-excursion
  15398. (and (outline-next-heading)
  15399. (org-flag-heading nil))))
  15400. (outline-flag-region (max 1 (1- (point)))
  15401. (save-excursion (outline-end-of-heading) (point))
  15402. flag))))
  15403. (defun org-end-of-subtree (&optional invisible-OK)
  15404. ;; This is an exact copy of the original function, but it uses
  15405. ;; `org-back-to-heading', to make it work also in invisible
  15406. ;; trees. And is uses an invisible-OK argument.
  15407. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  15408. (org-back-to-heading invisible-OK)
  15409. (let ((first t)
  15410. (level (funcall outline-level)))
  15411. (while (and (not (eobp))
  15412. (or first (> (funcall outline-level) level)))
  15413. (setq first nil)
  15414. (outline-next-heading))
  15415. (if (memq (preceding-char) '(?\n ?\^M))
  15416. (progn
  15417. ;; Go to end of line before heading
  15418. (forward-char -1)
  15419. (if (memq (preceding-char) '(?\n ?\^M))
  15420. ;; leave blank line before heading
  15421. (forward-char -1)))))
  15422. (point))
  15423. (defun org-show-subtree ()
  15424. "Show everything after this heading at deeper levels."
  15425. (outline-flag-region
  15426. (point)
  15427. (save-excursion
  15428. (outline-end-of-subtree) (outline-next-heading) (point))
  15429. nil))
  15430. (defun org-show-entry ()
  15431. "Show the body directly following this heading.
  15432. Show the heading too, if it is currently invisible."
  15433. (interactive)
  15434. (save-excursion
  15435. (org-back-to-heading t)
  15436. (outline-flag-region
  15437. (max 1 (1- (point)))
  15438. (save-excursion
  15439. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  15440. (or (match-beginning 1) (point-max)))
  15441. nil)))
  15442. (defun org-make-options-regexp (kwds)
  15443. "Make a regular expression for keyword lines."
  15444. (concat
  15445. "^"
  15446. "#?[ \t]*\\+\\("
  15447. (mapconcat 'regexp-quote kwds "\\|")
  15448. "\\):[ \t]*"
  15449. "\\(.+\\)"))
  15450. ;; Make `bookmark-jump' show the jump location if it was hidden.
  15451. (eval-after-load "bookmark"
  15452. '(if (boundp 'bookmark-after-jump-hook)
  15453. ;; We can use the hook
  15454. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  15455. ;; Hook not available, use advice
  15456. (defadvice bookmark-jump (after org-make-visible activate)
  15457. "Make the position visible."
  15458. (org-bookmark-jump-unhide))))
  15459. (defun org-bookmark-jump-unhide ()
  15460. "Unhide the current position, to show the bookmark location."
  15461. (and (org-mode-p)
  15462. (or (org-invisible-p)
  15463. (save-excursion (goto-char (max (point-min) (1- (point))))
  15464. (org-invisible-p)))
  15465. (org-show-hierarchy-above)))
  15466. ;;; Experimental code
  15467. ;(add-hook 'org-load-hook
  15468. ; (lambda ()
  15469. ; (defun outline-show-heading ()
  15470. ; "Show the current heading and move to its end."
  15471. ; (outline-flag-region
  15472. ; (- (point)
  15473. ; (cond
  15474. ; ((bobp) 0)
  15475. ; ((equal (buffer-substring
  15476. ; (max (point-min) (- (point) 3)) (point))
  15477. ; "\n\n\n")
  15478. ; 2)
  15479. ; (t 1)))
  15480. ; (progn (outline-end-of-heading) (point))
  15481. ; nil))))
  15482. ;;; Finish up
  15483. (provide 'org)
  15484. (run-hooks 'org-load-hook)
  15485. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  15486. ;;; org.el ends here